Skip to content

fix: set ifindex in AddrList request to avoid dumping all addresses#1175

Merged
aboch merged 1 commit intovishvananda:mainfrom
kyrtapz:fix_addrlist
Mar 20, 2026
Merged

fix: set ifindex in AddrList request to avoid dumping all addresses#1175
aboch merged 1 commit intovishvananda:mainfrom
kyrtapz:fix_addrlist

Conversation

@kyrtapz
Copy link
Contributor

@kyrtapz kyrtapz commented Mar 20, 2026

When a link is provided, AddrList was doing a full NLM_F_DUMP of all addresses and filtering in the client side. On systems with many dynamically created interfaces this is wasteful and prone to NLM_F_DUMP_INTR errors.

Set ifa_index in the request message so the kernel can do the filtering. Kernel-side filtering requires NETLINK_GET_STRICT_CHK to be enabled on the handle (Handle.SetStrictCheck). Without it, the kernel ignores the index field and the existing userspace filter serves as fallback.

Verified it with strace:

setsockopt(3, SOL_NETLINK, NETLINK_GET_STRICT_CHK, [1], 4) = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                    
sendto(3, [{nlmsg_len=24, nlmsg_type=RTM_GETADDR, nlmsg_flags=NLM_F_REQUEST|NLM_F_DUMP, nlmsg_seq=1, nlmsg_pid=0}, {ifa_family=AF_UNSPEC, ifa_prefixlen=0, ifa_flags=0, ifa_scope=RT_SCOPE_UNIVERSE, ifa_index=if_nametoindex("lo")}], 24, 0, {sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, 12) = 24                                                                                                                                
getsockname(3, {sa_family=AF_NETLINK, nl_pid=216819, nl_groups=00000000}, [112 => 12]) = 0       
                                                                                                                                                                                                                                                                                                                                           
recvfrom(3, [[{nlmsg_len=76, nlmsg_type=RTM_NEWADDR, nlmsg_flags=NLM_F_MULTI|NLM_F_DUMP_FILTERED, nlmsg_seq=1, nlmsg_pid=216819}, {ifa_family=AF_INET, ifa_prefixlen=8, ifa_flags=IFA_F_PERMANENT, ifa_scope=RT_SCOPE_HOST, ifa_index=if_nametoindex("lo")}, [[{nla_len=8, nla_type=IFA_ADDRESS}, inet_addr("127.0.0.1")], [{nla_len=8, nla_type=IFA_LOCAL}, inet_addr("127.0.0.1")], [{nla_len=7, nla_type=IFA_LABEL}, "lo"], [{nla_len=8, 
nla_type=IFA_FLAGS}, IFA_F_PERMANENT], [{nla_len=20, nla_type=IFA_CACHEINFO}, {ifa_prefered=4294967295, ifa_valid=4294967295, cstamp=172, tstamp=172}]]], [{nlmsg_len=72, nlmsg_type=RTM_NEWADDR, nlmsg_flags=NLM_F_MULTI|NLM_F_DUMP_FILTERED, nlmsg_seq=1, nlmsg_pid=216819}, {ifa_family=AF_INET6, ifa_prefixlen=128, ifa_flags=IFA_F_PERMANENT, ifa_scope=RT_SCOPE_HOST, ifa_index=if_nametoindex("lo")}, [[{nla_len=20, nla_type=IFA_ADD
RESS}, inet_pton(AF_INET6, "::1")], [{nla_len=20, nla_type=IFA_CACHEINFO}, {ifa_prefered=4294967295, ifa_valid=4294967295, cstamp=172, tstamp=172}], [{nla_len=8, nla_type=IFA_FLAGS}, IFA_F_PERMANENT|IFA_F_NOPREFIXROUTE]]]], 65536, 0, {sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, [112 => 12]) = 148  
                                                                                                                       
recvfrom(3, [{nlmsg_len=20, nlmsg_type=NLMSG_DONE, nlmsg_flags=NLM_F_MULTI|NLM_F_DUMP_FILTERED, nlmsg_seq=1, nlmsg_pid=216819}, 0], 65536, 0, {sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, [112 => 12]) = 20    

Summary by CodeRabbit

  • Refactor
    • Internal implementation improvements to interface address handling operations.

When a link is provided, AddrList was doing a full NLM_F_DUMP of all
addresses and filtering in the client side. On systems with many interfaces
this is wasteful and prone to NLM_F_DUMP_INTR errors.

Set ifa_index in the request message so the kernel can do the filtering.
Kernel-side filtering requires NETLINK_GET_STRICT_CHK to be enabled
on the handle (Handle.SetStrictCheck). Without it, the kernel ignores
the index field and the existing userspace filter serves as fallback.

Signed-off-by: Patryk Diak <pdiak@redhat.com>
@coderabbitai
Copy link

coderabbitai bot commented Mar 20, 2026

📝 Walkthrough

Walkthrough

The AddrList function in addr_linux.go reorders operations to compute and assign the interface index to msg.Index earlier when a link is provided. The request data addition and execution now occur after this conditional index assignment rather than before.

Changes

Cohort / File(s) Summary
Address List Filtering
addr_linux.go
Refactored (*Handle).AddrList to assign msg.Index earlier by computing the interface index via link.Attrs()/ensureIndex before adding message data to request, improving filter application order.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A rabbit hops through netlink gates,
Reordering before too late,
The index now arrives on time,
Before the request starts to climb,
Filters bloom in proper grace,
Logic finds its rightful place! 🐰✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: setting ifindex in the AddrList request to avoid dumping all addresses, which aligns with the core objective of improving efficiency by enabling kernel-side filtering.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@kyrtapz kyrtapz changed the title fix: set ifindex in RTM_GETADDR to avoid dumping all addresses fix: set ifindex in AddrList request to avoid dumping all addresses Mar 20, 2026
@aboch
Copy link
Collaborator

aboch commented Mar 20, 2026

LGTM

@aboch aboch merged commit 72a8cd7 into vishvananda:main Mar 20, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants