From e15c8287ccb787d60b221ab7daa0bc1df3c80f0f Mon Sep 17 00:00:00 2001 From: Julien Fortin Date: Thu, 3 Aug 2023 02:33:15 +0200 Subject: [PATCH] SCALE: addressvirtual: replace glob.glob with listdir using glob.glob is way to costly, use os.listdir to increase performances. Test with timeit show the following for 10k loop: time_glob executed in 39.16788899600215 seconds time_listdir executed in 5.625843115005409 seconds Signed-off-by: Julien Fortin --- debian/changelog | 1 + ifupdown2/addons/addressvirtual.py | 35 +++++++++++++++++++----------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/debian/changelog b/debian/changelog index 367a73b..f9cac35 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,5 +1,6 @@ ifupdown2 (3.2.1) unstable; urgency=medium + * New: performance improvement: replace glob.glob with os.listdir * New: Attribute: "disable-ipv6" to control ipv6 on an interface * New: Policy: "default_loopback_scope" control loopback ip scope * Fix: keep link down after mac change if 'link-down yes' is specified diff --git a/ifupdown2/addons/addressvirtual.py b/ifupdown2/addons/addressvirtual.py index 4118a86..d52115e 100644 --- a/ifupdown2/addons/addressvirtual.py +++ b/ifupdown2/addons/addressvirtual.py @@ -274,24 +274,33 @@ class addressvirtual(AddonWithIpBlackList, moduleBase): return False, None + def _get_macvlan_ifnames(self, ifaceobj): + macvlan_prefixes = ( + self._get_macvlan_prefix(ifaceobj), + self.get_vrrp_prefix(ifaceobj.name, "4"), + self.get_vrrp_prefix(ifaceobj.name, "6") + ) + + ifnames = set() + + for f in os.listdir("/sys/class/net/"): + if f.startswith(macvlan_prefixes): + ifnames.add(f) + + return ifnames + def _remove_running_address_config(self, ifaceobj): if not self.cache.link_exists(ifaceobj.name): return hwaddress = [] - for macvlan_prefix in [ - self._get_macvlan_prefix(ifaceobj), - self.get_vrrp_prefix(ifaceobj.name, "4"), - self.get_vrrp_prefix(ifaceobj.name, "6") - ]: - for macvlan_ifacename in glob.glob("/sys/class/net/%s*" % macvlan_prefix): - macvlan_ifacename = os.path.basename(macvlan_ifacename) - if not self.cache.link_exists(macvlan_ifacename) or self.cache.get_link_kind(macvlan_ifacename) != "macvlan": - continue - hwaddress.append(self.cache.get_link_address(macvlan_ifacename)) - self.netlink.link_del(os.path.basename(macvlan_ifacename)) - # XXX: Also delete any fdb addresses. This requires, checking mac address - # on individual macvlan interfaces and deleting the vlan from that. + for macvlan_ifacename in self._get_macvlan_ifnames(ifaceobj): + if not self.cache.link_exists(macvlan_ifacename) or self.cache.get_link_kind(macvlan_ifacename) != "macvlan": + continue + hwaddress.append(self.cache.get_link_address(macvlan_ifacename)) + self.netlink.link_del(macvlan_ifacename) + # XXX: Also delete any fdb addresses. This requires, checking mac address + # on individual macvlan interfaces and deleting the vlan from that. if any(hwaddress): self._remove_addresses_from_bridge(ifaceobj, hwaddress)