From 3b3405cb683aac692414de890f52437a55d9897a Mon Sep 17 00:00:00 2001 From: Nikita Koptev Date: Fri, 13 Nov 2026 19:44:48 +0000 Subject: [PATCH v2 thread-next] Nest: Unlink unused shut-down interfaces from the global list Removed interfaces are never freed and stay in global_iface_list forever. On hosts where interfaces with unique names are created and deleted all the time (VM ports), the list grows without limit, and if_find_by_index() and if_update() walk all the dead entries on every link message. After several months we have seen tens of thousands of dead entries and link scans taking seconds. After each full scan in if_end_update(), unlink interfaces from the list which are shut down and not used by anybody: uc == 0, no addresses and no neighbors. The memory is intentionally not freed, so any stale pointer still points to a valid object. Interfaces obtained by if_get_by_name() (VRF in the config) take a reference, so they are never unlinked. When an interface disappears without a netlink delete message (e.g. the message was lost) and is shut down by the full scan, also delete its addresses. Otherwise they stay forever and the interface would never be unlinked. This is meant as an interim workaround until the planned rework of interface data structures. --- nest/iface.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/nest/iface.c b/nest/iface.c index 9aea0da..266ad3c 100644 --- a/nest/iface.c +++ b/nest/iface.c @@ -547,6 +547,23 @@ if_end_partial_update(struct iface *i) IFACE_UNLOCK; } +/* + * Unlink unused shut-down interfaces, so that lookups do not walk all + * interfaces ever seen. Not freed, so stale pointers stay valid. + */ +static void +if_purge_dead(void) +{ + struct iface *i, *x; + + IFACE_LOCK; + WALK_LIST_DELSAFE(i, x, global_iface_list) + if ((i->flags & IF_SHUTDOWN) && !i->uc && + EMPTY_LIST(i->addrs) && EMPTY_LIST(i->neighbors)) + rem_node(&i->n); + IFACE_UNLOCK; +} + void if_end_update(void) { @@ -555,7 +572,13 @@ if_end_update(void) IFACE_WALK(i) { if (!(i->flags & IF_UPDATED)) - if_change_flags(i, (i->flags & ~IF_ADMIN_UP) | IF_SHUTDOWN); + { + if_change_flags(i, (i->flags & ~IF_ADMIN_UP) | IF_SHUTDOWN); + + /* The interface is gone, so are its addresses */ + WALK_LIST_DELSAFE(a, b, i->addrs) + ifa_delete_locked(a); + } else { WALK_LIST_DELSAFE(a, b, i->addrs) @@ -564,6 +587,8 @@ if_end_update(void) if_end_partial_update_locked(i); } } + + if_purge_dead(); } static void @@ -767,6 +792,7 @@ if_get_by_name(const char *name) WALK_LIST(i, global_iface_list) if (!strcmp(i->name, name)) { + i->uc++; /* Held by config, never unlinked */ IFACE_UNLOCK; return i; } @@ -778,6 +804,7 @@ if_get_by_name(const char *name) init_list(&i->addrs); init_list(&i->neighbors); add_tail(&global_iface_list, &i->n); + i->uc++; /* Held by config, never unlinked */ IFACE_UNLOCK; return i; -- 2.43.0