]> git.proxmox.com Git - mirror_ifupdown2.git/commitdiff
addressvirtual: address: add default metric to ip4 and ip6 (if available)
authorJulien Fortin <julien@cumulusnetworks.com>
Sat, 16 Jun 2018 17:56:57 +0000 (19:56 +0200)
committerJulien Fortin <julien@cumulusnetworks.com>
Mon, 18 Jun 2018 11:47:03 +0000 (13:47 +0200)
Reviewed-by: Roopa Prabhu <roopa@cumulusnetworks.com>
Reviewed-by: David Ahern <dsa@cumulusnetworks.com>
Signed-off-by: Julien Fortin <julien@cumulusnetworks.com>
ifupdown2/addons/addressvirtual.py
ifupdown2/ifupdownaddons/LinkUtils.py

index 0c644e44d5ba436dcf76e9d23586e6ad9480c121..01b7ed48aa880079c957493cb3c5db44bcf9346c 100644 (file)
@@ -223,8 +223,13 @@ class addressvirtual(moduleBase):
                 # customer could have used UPPERCASE for MAC
                 self.ipcmd.link_set_hwaddress(macvlan_ifacename, mac)
                 hwaddress.append(mac)
-            self.ipcmd.addr_add_multiple(ifaceobj, macvlan_ifacename, ips,
-                                         purge_existing)
+            self.ipcmd.addr_add_multiple(
+                ifaceobj,
+                macvlan_ifacename,
+                ips,
+                purge_existing,
+                metric=self.ipcmd.get_default_ip_metric() if self.ipcmd.addr_metric_support() else None
+            )
 
             # If link existed before, flap the link
             if not link_created:
@@ -249,7 +254,10 @@ class addressvirtual(moduleBase):
                 netlink.link_set_updown(macvlan_ifacename, "up")
             else:
                 try:
-                    self.ipcmd.fix_ipv6_route_metric(ifaceobj, macvlan_ifacename, ips)
+                    if not self.ipcmd.addr_metric_support():
+                        # if the system doesn't support ip addr set METRIC
+                        # we need to do manually check the ordering of the ip6 routes
+                        self.ipcmd.fix_ipv6_route_metric(ifaceobj, macvlan_ifacename, ips)
                 except Exception as e:
                     self.logger.debug('fix_vrf_slave_ipv6_route_metric: failed: %s' % e)
 
index e4b4de9fb66708fea8c4465b85b874cdd023d19d..a15ba774e5f487434eb42853cafd207c2c6a0e37 100644 (file)
@@ -55,6 +55,9 @@ class LinkUtils(utilsBase):
     bridge_utils_is_installed = os.path.exists(utils.brctl_cmd)
     bridge_utils_missing_warning = True
 
+    DEFAULT_IP_METRIC = 1024
+    ADDR_METRIC_SUPPORT = None
+
     def __init__(self, *args, **kargs):
         utilsBase.__init__(self, *args, **kargs)
 
@@ -68,6 +71,27 @@ class LinkUtils(utilsBase):
         if not ifupdownflags.flags.PERFMODE and not LinkUtils._CACHE_FILL_DONE:
             self._fill_cache()
 
+        if LinkUtils.ADDR_METRIC_SUPPORT is None:
+            try:
+                cmd = [utils.ip_cmd, 'addr', 'help']
+                self.logger.info('executing %s addr help' % utils.ip_cmd)
+
+                process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+                stdout, stderr = process.communicate()
+                LinkUtils.ADDR_METRIC_SUPPORT = '[ metric METRIC ]' in stderr or ''
+                self.logger.info('address metric support: %s' % ('OK' if LinkUtils.ADDR_METRIC_SUPPORT else 'KO'))
+            except Exception:
+                LinkUtils.ADDR_METRIC_SUPPORT = False
+                self.logger.info('address metric support: KO')
+
+    @classmethod
+    def addr_metric_support(cls):
+        return cls.ADDR_METRIC_SUPPORT
+
+    @classmethod
+    def get_default_ip_metric(cls):
+        return cls.DEFAULT_IP_METRIC
+
     @classmethod
     def reset(cls):
         LinkUtils._CACHE_FILL_DONE = False
@@ -789,7 +813,7 @@ class LinkUtils(utilsBase):
                                         '-o', '-d', 'link', 'show'])
 
     def addr_add(self, ifacename, address, broadcast=None,
-                 peer=None, scope=None, preferred_lifetime=None):
+                 peer=None, scope=None, preferred_lifetime=None, metric=None):
         if not address:
             return
         cmd = 'addr add %s' % address
@@ -802,6 +826,10 @@ class LinkUtils(utilsBase):
         if preferred_lifetime:
             cmd += ' preferred_lft %s' % preferred_lifetime
         cmd += ' dev %s' % ifacename
+
+        if metric:
+            cmd += ' metric %s' % metric
+
         if LinkUtils.ipbatch and not LinkUtils.ipbatch_pause:
             self.add_to_batch(cmd)
         else:
@@ -954,7 +982,7 @@ class LinkUtils(utilsBase):
 
         return running_ipobj == (ip4 + ip6)
 
-    def addr_add_multiple(self, ifaceobj, ifacename, addrs, purge_existing=False):
+    def addr_add_multiple(self, ifaceobj, ifacename, addrs, purge_existing=False, metric=None):
         # purges address
         if purge_existing:
             # if perfmode is not set and also if iface has no sibling
@@ -981,7 +1009,7 @@ class LinkUtils(utilsBase):
                 self.logger.warning('%s: %s' % (ifacename, str(e)))
         for a in addrs:
             try:
-                self.addr_add(ifacename, a)
+                self.addr_add(ifacename, a, metric=metric)
             except Exception, e:
                 self.logger.error(str(e))