components/openstack/neutron/files/neutron-l3-agent
branchs11u2-sru
changeset 4156 4b1def16fe9b
parent 3364 25975ce9e810
child 4049 150852e281c4
equal deleted inserted replaced
4146:097063f324c0 4156:4b1def16fe9b
     1 #!/usr/bin/python2.6
     1 #!/usr/bin/python2.6
     2 
     2 
     3 # Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     3 # Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
     4 #
     4 #
     5 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
     5 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
     6 #    not use this file except in compliance with the License. You may obtain
     6 #    not use this file except in compliance with the License. You may obtain
     7 #    a copy of the License at
     7 #    a copy of the License at
     8 #
     8 #
    22 import smf_include
    22 import smf_include
    23 
    23 
    24 from subprocess import CalledProcessError, Popen, PIPE, check_call
    24 from subprocess import CalledProcessError, Popen, PIPE, check_call
    25 
    25 
    26 
    26 
       
    27 def set_hostmodel(value):
       
    28     cmd = ["/usr/sbin/ipadm", "show-prop", "-p", "hostmodel",
       
    29            "-co", "current", "ipv4"]
       
    30     p = Popen(cmd, stdout=PIPE, stderr=PIPE)
       
    31     output, error = p.communicate()
       
    32     if p.returncode != 0:
       
    33         print "failed to retrieve hostmodel ipadm property"
       
    34         return False
       
    35     if output.strip() == value:
       
    36         return True
       
    37     cmd = ["/usr/sbin/ipadm", "set-prop", "-t", "-p", "hostmodel=%s" % value,
       
    38            "ipv4"]
       
    39     p = Popen(cmd, stdout=PIPE, stderr=PIPE)
       
    40     output, error = p.communicate()
       
    41     if p.returncode != 0:
       
    42         print "failed to set ipadm hostmodel property to %s" % value
       
    43         return False
       
    44     return True
       
    45 
       
    46 
    27 def start():
    47 def start():
    28     # verify paths are valid
    48     # verify paths are valid
    29     for f in sys.argv[2:4]:
    49     for f in sys.argv[2:4]:
    30         if not os.path.exists(f) or not os.access(f, os.R_OK):
    50         if not os.path.exists(f) or not os.access(f, os.R_OK):
    31             print '%s does not exist or is not readable' % f
    51             print '%s does not exist or is not readable' % f
    53 
    73 
    54     if not any((v4fwding, v6fwding)):
    74     if not any((v4fwding, v6fwding)):
    55         print "System-wide IPv4 or IPv6 (or both) forwarding must be " \
    75         print "System-wide IPv4 or IPv6 (or both) forwarding must be " \
    56               "enabled before enabling neutron-l3-agent"
    76               "enabled before enabling neutron-l3-agent"
    57         return smf_include.SMF_EXIT_ERR_CONFIG
    77         return smf_include.SMF_EXIT_ERR_CONFIG
       
    78 
       
    79     # set the hostmodel property if necessary
       
    80     if not set_hostmodel("src-priority"):
       
    81         return smf_include.SMF_EXIT_ERR_FATAL
    58 
    82 
    59     cmd = "/usr/lib/neutron/neutron-l3-agent --config-file %s " \
    83     cmd = "/usr/lib/neutron/neutron-l3-agent --config-file %s " \
    60         "--config-file %s" % tuple(sys.argv[2:4])
    84         "--config-file %s" % tuple(sys.argv[2:4])
    61     smf_include.smf_subprocess(cmd)
    85     smf_include.smf_subprocess(cmd)
    62 
    86 
   135         print "failed to retrieve IP NAT rules"
   159         print "failed to retrieve IP NAT rules"
   136         return smf_include.SMF_EXIT_ERR_FATAL
   160         return smf_include.SMF_EXIT_ERR_FATAL
   137 
   161 
   138     ipnat_rules = output.splitlines()
   162     ipnat_rules = output.splitlines()
   139     # L3 agent IP NAT rules are of the form
   163     # L3 agent IP NAT rules are of the form
   140     # bimap l3e64ccc496_a_0 192.168.1.3/32 -> 172.16.10.3/32
   164     # bimap l3e64ccc496_a_0 .... OR
   141     prog = re.compile('l3e[0-9A-Fa-f\_]{10}_0')
   165     # rdr l3iedf345cc96_a_0 ....
       
   166     prog = re.compile('l3[ie][0-9A-Fa-f\_]{10}_0')
   142     for ipnat_rule in ipnat_rules:
   167     for ipnat_rule in ipnat_rules:
   143         if not prog.search(ipnat_rule):
   168         if not prog.search(ipnat_rule):
   144             continue
   169             continue
   145         # remove the IP NAT rule
   170         # remove the IP NAT rule
   146         try:
   171         try:
   176                         ifname])
   201                         ifname])
   177         except CalledProcessError as err:
   202         except CalledProcessError as err:
   178             print "failed to remove datalinks used by L3 agent: %s" % (err)
   203             print "failed to remove datalinks used by L3 agent: %s" % (err)
   179             return smf_include.SMF_EXIT_ERR_FATAL
   204             return smf_include.SMF_EXIT_ERR_FATAL
   180 
   205 
       
   206     # finally reset the hostmodel property
       
   207     if not set_hostmodel("weak"):
       
   208         return smf_include.SMF_EXIT_ERR_FATAL
   181     return smf_include.SMF_EXIT_OK
   209     return smf_include.SMF_EXIT_OK
   182 
   210 
   183 if __name__ == "__main__":
   211 if __name__ == "__main__":
   184     os.putenv("LC_ALL", "C")
   212     os.putenv("LC_ALL", "C")
   185     smf_include.smf_main()
   213     smf_include.smf_main()