From 528a63d9cc4da678482e05de3c41dfdfe649dcfe Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 29 Mar 2022 17:23:52 +0200 Subject: [PATCH] platform: avoid unnecessary configuration of IP address in nm_platform_ip_address_sync() We call sync many times. Often there is nothing to update. Check the cache first, before (re) adding it. Note that many addresses have a limited lifetime, that is, a lifetime that keeps counting down with seconds granularity. For those (common) cases we will only avoid the call to kernel if there are two syncs within less than a second. --- src/libnm-platform/nm-platform.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/libnm-platform/nm-platform.c b/src/libnm-platform/nm-platform.c index 558bb869e..95c2e5b46 100644 --- a/src/libnm-platform/nm-platform.c +++ b/src/libnm-platform/nm-platform.c @@ -4348,6 +4348,7 @@ next_plat:; * priority. */ for (i_know = 0; i_know < known_addresses->len; i_know++) { + const NMPObject *plat_obj; const NMPObject *known_obj; const NMPlatformIPXAddress *known_address; guint32 lifetime; @@ -4368,6 +4369,23 @@ next_plat:; &preferred); nm_assert(lifetime > 0); + plat_obj = nm_platform_ip_address_get(self, addr_family, ifindex, known_address); + if (plat_obj + && nm_platform_vtable_address.vx[IS_IPv4].address_cmp( + known_address, + NMP_OBJECT_CAST_IPX_ADDRESS(plat_obj), + NM_PLATFORM_IP_ADDRESS_CMP_TYPE_SEMANTICALLY) + == 0) { + char sbuf[sizeof(_nm_utils_to_string_buffer)]; + + /* The object is already added. Skip update. */ + _LOG3t( + "address: skip updating IPv%c address: %s", + nm_utils_addr_family_to_char(addr_family), + nmp_object_to_string(known_obj, NMP_OBJECT_TO_STRING_PUBLIC, sbuf, sizeof(sbuf))); + continue; + } + if (IS_IPv4) { if (!nm_platform_ip4_address_add( self,