From dfaa8a2d1991560cbdcc7a727448498f68517ccc Mon Sep 17 00:00:00 2001 From: Julien Fortin Date: Sat, 16 Jun 2018 19:56:57 +0200 Subject: [PATCH] addressvirtual: address: add default metric to ip4 and ip6 (if available) Reviewed-by: Roopa Prabhu Reviewed-by: David Ahern Signed-off-by: Julien Fortin --- ifupdown2/addons/addressvirtual.py | 14 ++++++++--- ifupdown2/ifupdownaddons/LinkUtils.py | 34 ++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/ifupdown2/addons/addressvirtual.py b/ifupdown2/addons/addressvirtual.py index 0c644e4..01b7ed4 100644 --- a/ifupdown2/addons/addressvirtual.py +++ b/ifupdown2/addons/addressvirtual.py @@ -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) diff --git a/ifupdown2/ifupdownaddons/LinkUtils.py b/ifupdown2/ifupdownaddons/LinkUtils.py index e4b4de9..a15ba77 100644 --- a/ifupdown2/ifupdownaddons/LinkUtils.py +++ b/ifupdown2/ifupdownaddons/LinkUtils.py @@ -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)) -- 2.39.5