]> git.proxmox.com Git - mirror_ifupdown2.git/commitdiff
addons: when comparing mac addresses use integer representation
authorJulien Fortin <julien@cumulusnetworks.com>
Tue, 11 Jun 2019 14:51:07 +0000 (22:51 +0800)
committerJulien Fortin <julien@cumulusnetworks.com>
Wed, 12 Jun 2019 15:26:20 +0000 (23:26 +0800)
Signed-off-by: Julien Fortin <julien@cumulusnetworks.com>
ifupdown2/addons/address.py
ifupdown2/addons/addressvirtual.py
ifupdown2/addons/bridge.py
ifupdown2/ifupdownaddons/LinkUtils.py

index f088d5fda7fab9a3d1861617af1dfcf7c929c16f..f576dc02dc081b44fdbea6a64e105785fa161baa 100644 (file)
@@ -864,13 +864,25 @@ class address(moduleBase):
         except Exception as e:
             self.log_error('%s: %s' % (ifaceobj.name, str(e)), ifaceobj, raise_error=False)
 
+        self.up_hwaddress(ifaceobj)
+
+        gateways = ifaceobj.get_attr_value('gateway')
+        if not gateways:
+            gateways = []
+        prev_gw = self._get_prev_gateway(ifaceobj, gateways)
+        self._add_delete_gateway(ifaceobj, gateways, prev_gw)
+
+    def up_hwaddress(self, ifaceobj):
         try:
             hwaddress = self._get_hwaddress(ifaceobj)
+
             if hwaddress:
-                running_hwaddress = None
-                if not ifupdownflags.flags.PERFMODE: # system is clean
+                if not ifupdownflags.flags.PERFMODE:  # system is clean
                     running_hwaddress = self.ipcmd.link_get_hwaddress(ifaceobj.name)
-                if hwaddress != running_hwaddress:
+                else:
+                    running_hwaddress = None
+
+                if self.ipcmd.mac_str_to_int(running_hwaddress) != self.ipcmd.mac_str_to_int(hwaddress):
                     slave_down = False
                     netlink.link_set_updown(ifaceobj.name, "down")
                     if ifaceobj.link_kind & ifaceLinkKind.BOND:
@@ -880,7 +892,7 @@ class address(moduleBase):
                                 netlink.link_set_updown(l, "down")
                             slave_down = True
                     try:
-                        self.ipcmd.link_set(ifaceobj.name, 'address', hwaddress)
+                        self.ipcmd.link_set_hwaddress(ifaceobj.name, hwaddress, force=True)
                     finally:
                         netlink.link_set_updown(ifaceobj.name, "up")
                         if slave_down:
@@ -890,13 +902,7 @@ class address(moduleBase):
             # Handle special things on a bridge
             self._process_bridge(ifaceobj, True)
         except Exception, e:
-            self.log_error('%s: %s' %(ifaceobj.name, str(e)), ifaceobj)
-
-        gateways = ifaceobj.get_attr_value('gateway')
-        if not gateways:
-            gateways = []
-        prev_gw = self._get_prev_gateway(ifaceobj, gateways)
-        self._add_delete_gateway(ifaceobj, gateways, prev_gw)
+            self.log_error('%s: %s' % (ifaceobj.name, str(e)), ifaceobj)
 
     def _down(self, ifaceobj, ifaceobj_getfunc=None):
         try:
@@ -965,9 +971,12 @@ class address(moduleBase):
             bridgename = ifaceobj.lowerifaces[0]
             vlan = self._get_vlan_id(ifaceobj)
             if self.ipcmd.bridge_is_vlan_aware(bridgename):
-                fdb_addrs = self._get_bridge_fdbs(bridgename, str(vlan))
-                if not fdb_addrs or hwaddress not in fdb_addrs:
-                   return False
+                fdb_addrs = [self.ipcmd.mac_str_to_int(fdb_addr) for fdb_addr in self._get_bridge_fdbs(bridgename, str(vlan))]
+                if not fdb_addrs:
+                    return False
+                hwaddress_int = self.ipcmd.mac_str_to_int(hwaddress)
+                if hwaddress_int not in fdb_addrs:
+                    return False
         return True
 
     def _query_sysctl(self, ifaceobj, ifaceobjcurr):
@@ -1050,7 +1059,7 @@ class address(moduleBase):
         hwaddress = self._get_hwaddress(ifaceobj)
         if hwaddress:
             rhwaddress = self.ipcmd.link_get_hwaddress(ifaceobj.name)
-            if not rhwaddress  or rhwaddress != hwaddress:
+            if not rhwaddress or self.ipcmd.mac_str_to_int(rhwaddress) != self.ipcmd.mac_str_to_int(hwaddress):
                ifaceobjcurr.update_config_with_status('hwaddress', rhwaddress,
                        1)
             elif not self._check_addresses_in_bridge(ifaceobj, hwaddress):
index a387a478f6680d56e2c1fd65bb1fbd6440871063..5566c0acad69504c3acac81fa3167e88fc46b5c8 100644 (file)
@@ -7,7 +7,6 @@
 import os
 import glob
 
-from string import maketrans
 from collections import deque
 from ipaddr import IPNetwork, IPv6Network
 
@@ -89,7 +88,6 @@ class addressvirtual(moduleBase):
         )
 
         self.address_virtual_ipv6_addrgen_value_dict = {'on': 0, 'yes': 0, '0': 0, 'off': 1, 'no': 1, '1': 1}
-        self.mac_translate_tab = maketrans(":.-,", "    ")
 
     def get_dependent_ifacenames(self, ifaceobj, ifacenames_all=None):
         if ifaceobj.get_attr_value('address-virtual') or ifaceobj.get_attr_value("vrrp"):
@@ -153,9 +151,9 @@ class addressvirtual(moduleBase):
                 fdb_addrs = self._get_bridge_fdbs(bridgename, str(vlan))
                 if not fdb_addrs:
                    return False
-                hwaddress_int = self.mac_str_to_int(hwaddress)
+                hwaddress_int = self.ipcmd.mac_str_to_int(hwaddress)
                 for mac in fdb_addrs:
-                    if self.mac_str_to_int(mac) == hwaddress_int:
+                    if self.ipcmd.mac_str_to_int(mac) == hwaddress_int:
                         return True
                 return False
         return True
@@ -376,12 +374,6 @@ class addressvirtual(moduleBase):
                     self.ipcmd.link_set(u, 'master', vrfname,
                                         state='up')
 
-    def mac_str_to_int(self, mac):
-        mac_int = 0
-        for n in mac.translate(self.mac_translate_tab).split():
-            mac_int += int(n, 16)
-        return mac_int
-
     def create_macvlan_and_apply_config(self, ifaceobj, intf_config_list, vrrp=False):
         """
         intf_config_list = [
@@ -600,7 +592,7 @@ class addressvirtual(moduleBase):
             if ip4 or ifquery:
                 merged_with_existing_obj = False
                 macvlan_ip4_mac = "00:00:5e:00:01:%s" % hex_id
-                macvlan_ip4_mac_int = self.mac_str_to_int(macvlan_ip4_mac)
+                macvlan_ip4_mac_int = self.ipcmd.mac_str_to_int(macvlan_ip4_mac)
                 # if the vrr config is defined in different lines for the same ID
                 # we need to save the ip4 and ip6 in the objects we previously
                 # created, example:
@@ -631,7 +623,7 @@ class addressvirtual(moduleBase):
             if ip6 or ifquery:
                 merged_with_existing_obj = False
                 macvlan_ip6_mac = "00:00:5e:00:02:%s" % hex_id
-                macvlan_ip6_mac_int = self.mac_str_to_int(macvlan_ip6_mac)
+                macvlan_ip6_mac_int = self.ipcmd.mac_str_to_int(macvlan_ip6_mac)
                 # if the vrr config is defined in different lines for the same ID
                 # we need to save the ip4 and ip6 in the objects we previously
                 # created, example:
@@ -738,7 +730,7 @@ class addressvirtual(moduleBase):
 
             if mac != "none":
                 config["hwaddress"] = mac
-                config["hwaddress_int"] = self.mac_str_to_int(mac)
+                config["hwaddress_int"] = self.ipcmd.mac_str_to_int(mac)
 
             ip_network_obj_list = []
             for ip in av_attrs[1:]:
@@ -882,7 +874,7 @@ class addressvirtual(moduleBase):
                     continue
 
                 try:
-                    if self.mac_str_to_int(rhwaddress) == macvlan_hwaddress_int \
+                    if self.ipcmd.mac_str_to_int(rhwaddress) == macvlan_hwaddress_int \
                             and self.ipcmd.compare_user_config_vs_running_state(raddrs, ips) \
                             and self._check_addresses_in_bridge(ifaceobj, macvlan_hwaddress):
                         ifaceobjcurr.update_config_with_status(
index 527740dae5a9ec09f9ed22d43e25fc424e17a833..a157dd6063e9683eb3d9c4e1b03e5853238800bd 100644 (file)
@@ -2205,7 +2205,7 @@ class bridge(moduleBase):
             # and compare the ints, it will increase perfs and be safer.
             cached_value = self.ipcmd.cache_get('link', [ifname, 'hwaddress'])
             self.logger.debug('%s: cached hwaddress value: %s' % (ifname, cached_value))
-            if cached_value and cached_value == bridge_mac:
+            if cached_value and self.ipcmd.mac_str_to_int(cached_value) == self.ipcmd.mac_str_to_int(bridge_mac):
                 # the bridge mac is already set to the bridge_mac_intf's mac
                 return
 
index d973cbc2ba8219740bbb67abb2eb6982aeefee70..8078093e6dcc20ead410539de92dff3eb7e63b2d 100644 (file)
@@ -13,6 +13,7 @@ import signal
 import socket
 import subprocess
 
+from string import maketrans
 from ipaddr import IPNetwork, IPv6Network
 
 try:
@@ -59,6 +60,8 @@ class LinkUtils(utilsBase):
     DEFAULT_IP_METRIC = 1024
     ADDR_METRIC_SUPPORT = None
 
+    mac_translate_tab = maketrans(":.-,", "    ")
+
     def __init__(self, *args, **kargs):
         utilsBase.__init__(self, *args, **kargs)
 
@@ -85,6 +88,14 @@ class LinkUtils(utilsBase):
                 LinkUtils.ADDR_METRIC_SUPPORT = False
                 self.logger.info('address metric support: KO')
 
+    @classmethod
+    def mac_str_to_int(cls, mac):
+        mac_int = 0
+        if mac:
+            for n in mac.translate(cls.mac_translate_tab).split():
+                mac_int += int(n, 16)
+        return mac_int
+
     @classmethod
     def addr_metric_support(cls):
         return cls.ADDR_METRIC_SUPPORT
@@ -1081,8 +1092,11 @@ class LinkUtils(utilsBase):
 
     def link_set_hwaddress(self, ifacename, hwaddress, force=False):
         if not force:
-            if self._cache_check('link', [ifacename, 'hwaddress'], hwaddress):
-                return
+            link_hwaddress = self.link_get_hwaddress(ifacename)
+
+            if self.mac_str_to_int(link_hwaddress) == self.mac_str_to_int(hwaddress):
+                return False
+
         self.link_down(ifacename)
         cmd = 'link set dev %s address %s' % (ifacename, hwaddress)
         if LinkUtils.ipbatch:
@@ -1091,6 +1105,7 @@ class LinkUtils(utilsBase):
             utils.exec_command('%s %s' % (utils.ip_cmd, cmd))
         self.link_up(ifacename)
         self._cache_update([ifacename, 'hwaddress'], hwaddress)
+        return True
 
     def link_set_mtu(self, ifacename, mtu):
         if ifupdownflags.flags.DRYRUN: