Skip to content

handle: add NewHandleWithOptions(), deprecate DisableVFInfoCollection()#1174

Merged
aboch merged 3 commits into
vishvananda:mainfrom
ti-mo:tb/handle-options
Jun 24, 2026
Merged

handle: add NewHandleWithOptions(), deprecate DisableVFInfoCollection()#1174
aboch merged 3 commits into
vishvananda:mainfrom
ti-mo:tb/handle-options

Conversation

@ti-mo

@ti-mo ti-mo commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

Repurpose HandleOptions as a declarative configuration mechanism for Handle. It can be passed to NewHandleWithOptions.

It also removes the unreleased Handle.RetryInterrupted and deprecates DisableVFInfoCollection in favor of its HandleOptions counterpart.

Summary by CodeRabbit

  • New Features

    • One-time global configuration for handle initialization; subsequent attempts now return an error.
    • New options to control retry behavior, explicit network-namespace selection, and VF-info collection.
  • Changes

    • Consolidated handle creation through an options-based path.
    • Lookup logic now tracks dump-based lookups atomically and respects the VF-info collection toggle when listing/looking up links.
  • Bug Fixes

    • Test added to validate one-time configuration semantics.
  • Deprecated

    • Fluent VF-info disable method replaced by the new option.

@coderabbitai

coderabbitai Bot commented Mar 18, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 175d95f8-b97b-4194-b61a-bfbda0909b6a

📥 Commits

Reviewing files that changed from the base of the PR and between 9d05034 and 6b21e75.

📒 Files selected for processing (2)
  • handle_linux.go
  • handle_linux_test.go
✅ Files skipped from review due to trivial changes (1)
  • handle_linux_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • handle_linux.go

📝 Walkthrough

Walkthrough

Adds a one-time global configurator ConfigureHandle(HandleOptions) error, promotes several HandleOptions fields to exported names (DisableVFInfoCollection, RetryInterrupted, NetNS), introduces NewHandleWithOptions, funnels constructors through options, and updates link lookup and VF-info gating to use the new options and an atomic lookupByDump flag.

Changes

Cohort / File(s) Summary
Handle initialization & API
handle_linux.go
Added ConfigureHandle(HandleOptions) error (mutex-guarded, one-time); reworked HandleOptions (removed internal booleans; added DisableVFInfoCollection, RetryInterrupted, NetNS); added NewHandleWithOptions; updated NewHandle, NewHandleAt, NewHandleAtFrom to use options; (*Handle).DisableVFInfoCollection() now sets h.options.DisableVFInfoCollection and is deprecated.
Link lookup & VF info logic
link_linux.go
Link lookup now reads h.lookupByDump.Load() (atomic) for dump-based lookup decisions; VF-info extension (IFLA_EXT_MASK/RTEXT_FILTER_VF) is added only when !h.options.DisableVFInfoCollection; unsupported-name/alias fallback sets h.lookupByDump.Store(true); dump retry behavior uses h.options.RetryInterrupted.
Tests
handle_linux_test.go
Added TestConfigureHandle (uses testify/assert): saves/restores global state, verifies ConfigureHandle(HandleOptions{DisableVFInfoCollection: true}) succeeds and updates pkgHandle, closing the handle succeeds, and a subsequent ConfigureHandle(HandleOptions{}) returns an error.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 I hopped through options, snug and light,
A mutex here, a namespace right,
VF flags tucked and lookups tuned,
One-time configure — changes pruned,
I twitched my nose and coded bright.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 62.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main changes: adding NewHandleWithOptions() and deprecating DisableVFInfoCollection(), which are the primary API modifications in this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@ti-mo ti-mo force-pushed the tb/handle-options branch from 5797a29 to 03c80dd Compare March 18, 2026 19:01
@ti-mo ti-mo force-pushed the tb/handle-options branch 2 times, most recently from 1c0d4e6 to 1b5de88 Compare April 3, 2026 09:32
@ti-mo ti-mo marked this pull request as ready for review April 3, 2026 09:32
Copilot AI review requested due to automatic review settings April 3, 2026 09:32

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@handle_linux_test.go`:
- Around line 21-27: TestConfigureHandle mutates package globals (pkgHandle and
configDone) and never restores them, breaking isolation; update the test to save
orig := pkgHandle and defer restoring pkgHandle and clearing configDone under
configMu (acquire configMu, set configDone = false, release) so the global state
is returned to its original condition after the test, then run ConfigureHandle
assertions as before. Ensure you reference and restore pkgHandle, and protect
writes to configDone using configMu to avoid races.

In `@handle_linux.go`:
- Around line 197-201: The comment for NewHandleWithOptions is misleading
because it states "If opts is nil" although opts is passed by value as
HandleOptions; update the docstring to reflect that the zero value or empty
HandleOptions will trigger defaults instead of nil. Edit the comment above
NewHandleWithOptions to say something like "If opts is the zero value, default
options will be used" and ensure references to opts elsewhere (if any) use the
same non-nil wording; target the NewHandleWithOptions function and the
HandleOptions type in your update.
- Around line 69-70: The lookupByDump boolean must be made concurrency-safe:
change the Handle.lookupByDump field from a plain bool to sync/atomic.Bool and
add the "sync/atomic" import; then replace direct reads/writes in LinkByName and
LinkByAlias with h.lookupByDump.Load() and h.lookupByDump.Store(true/false)
respectively (use Load where the code currently reads the flag and Store where
it writes it) so concurrent goroutines no longer race on that field.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1aebd270-4458-430c-a1b4-86a5efc154a6

📥 Commits

Reviewing files that changed from the base of the PR and between 188504c and 1b5de88.

📒 Files selected for processing (3)
  • handle_linux.go
  • handle_linux_test.go
  • link_linux.go

Comment thread handle_linux_test.go
Comment thread handle_linux.go Outdated
Comment thread handle_linux.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a declarative HandleOptions configuration flow for netlink Handle creation via NewHandleWithOptions, and adds a one-time configuration entrypoint for the package-global default handle used by the package-level APIs.

Changes:

  • Added NewHandleWithOptions(opts HandleOptions, ...) and repurposed HandleOptions to carry exported, user-facing settings (DisableVFInfoCollection, RetryInterrupted, NetNS).
  • Added ConfigureHandle(opts HandleOptions) to configure the package-wide pkgHandle once per process.
  • Deprecated (*Handle).DisableVFInfoCollection() in favor of HandleOptions.DisableVFInfoCollection, removed the unreleased RetryInterrupted() handle mutator, and updated link lookup code paths accordingly.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
handle_linux.go Adds new handle configuration APIs and reshapes HandleOptions usage across handle creation/request construction.
link_linux.go Switches VF info fetching to the new DisableVFInfoCollection option and moves dump-fallback state to Handle.lookupByDump.
handle_linux_test.go Adds a unit test covering the new one-time ConfigureHandle behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread handle_linux.go Outdated
Comment thread handle_linux.go
Comment thread handle_linux_test.go
This is not really a configuration option, but rather an internal cache for
remembering the lookup fallback behaviour.

Signed-off-by: Timo Beckers <timo@incline.eu>
@ti-mo ti-mo force-pushed the tb/handle-options branch from 1b5de88 to 40804be Compare April 13, 2026 11:41

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
link_linux.go (1)

2032-2047: ⚠️ Potential issue | 🟠 Major

Honor DisableVFInfoCollection in dump-based lookup paths too.

This only skips RTEXT_FILTER_VF on the direct RTM_GETLINK request. If the kernel falls back to linkByNameDump()/linkByAliasDump(), those paths go through Handle.LinkList(), and link_linux.go Line 2532 still unconditionally adds the VF mask. So the new option is ignored on older kernels, and LinkList() itself still pays the VF-info cost.

Suggested fix
 func (h *Handle) LinkList() ([]Link, error) {
 	// NOTE(vish): This duplicates functionality in net/iface_linux.go, but we need
 	//             to get the message ourselves to parse link type.
 	req := h.newNetlinkRequest(unix.RTM_GETLINK, unix.NLM_F_DUMP)

 	msg := nl.NewIfInfomsg(unix.AF_UNSPEC)
 	req.AddData(msg)
-	attr := nl.NewRtAttr(unix.IFLA_EXT_MASK, nl.Uint32Attr(nl.RTEXT_FILTER_VF))
-	req.AddData(attr)
+	if !h.options.DisableVFInfoCollection {
+		attr := nl.NewRtAttr(unix.IFLA_EXT_MASK, nl.Uint32Attr(nl.RTEXT_FILTER_VF))
+		req.AddData(attr)
+	}

 	msgs, executeErr := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWLINK)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@link_linux.go` around lines 2032 - 2047, The VF info filter (RTEXT_FILTER_VF)
is only skipped for the RTM_GETLINK path but still unconditionally added in
dump-based paths (linkByNameDump/linkByAliasDump/Handle.LinkList), so update the
dump request builders to respect h.options.DisableVFInfoCollection: wherever
nl.NewRtAttr(unix.IFLA_EXT_MASK, nl.Uint32Attr(nl.RTEXT_FILTER_VF)) is added in
the dump code paths (e.g., inside linkByNameDump, linkByAliasDump and the
LinkList request construction), guard that addition with a check like if
!h.options.DisableVFInfoCollection before creating/adding the attribute so
older-kernel dump lookups honor the option and avoid collecting VF info.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@handle_linux_test.go`:
- Around line 24-34: The cleanup must close the real handle and signal its done
channel before restoring originals to avoid leaking FDs/goroutines: inside the
t.Cleanup closure (where ConfigureHandle was used) check if pkgHandle is the
configured (non-nil and not equal to orig) and call its Close method, and if
configDone is the configured channel (not equal to origDone) either close it or
send the done signal expected by the code under test; only after
closing/signaling the configured resources restore pkgHandle=orig and
configDone=origDone while holding configMu. Ensure you reference the
ConfigureHandle installation flow and the pkgHandle/configDone variables when
implementing this.

---

Outside diff comments:
In `@link_linux.go`:
- Around line 2032-2047: The VF info filter (RTEXT_FILTER_VF) is only skipped
for the RTM_GETLINK path but still unconditionally added in dump-based paths
(linkByNameDump/linkByAliasDump/Handle.LinkList), so update the dump request
builders to respect h.options.DisableVFInfoCollection: wherever
nl.NewRtAttr(unix.IFLA_EXT_MASK, nl.Uint32Attr(nl.RTEXT_FILTER_VF)) is added in
the dump code paths (e.g., inside linkByNameDump, linkByAliasDump and the
LinkList request construction), guard that addition with a check like if
!h.options.DisableVFInfoCollection before creating/adding the attribute so
older-kernel dump lookups honor the option and avoid collecting VF info.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d17c85f9-e76e-446b-b0c4-55f20bc898f1

📥 Commits

Reviewing files that changed from the base of the PR and between 1b5de88 and 40804be.

📒 Files selected for processing (3)
  • handle_linux.go
  • handle_linux_test.go
  • link_linux.go

Comment thread handle_linux_test.go
This commit repurposes HandleOptions as a declarative configuration mechanism
for Handle. It can be passed to NewHandleWithOptions.

It also removes the unreleased Handle.RetryInterrupted and deprecates
DisableVFInfoCollection in favor of its HandleOptions counterpart.

Signed-off-by: Timo Beckers <timo@incline.eu>
@ti-mo ti-mo force-pushed the tb/handle-options branch from 40804be to 9d05034 Compare April 13, 2026 12:11

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@handle_linux.go`:
- Around line 29-38: ConfigureHandle currently assigns opts into a bare Handle
so fields like NetNS and RetryInterrupted that are applied during socket
creation (see newHandle and newNetlinkRequest) are not honored; update
ConfigureHandle to create a fully-initialized package handle (use the same
initialization logic as newHandle) instead of simply assigning options to an
empty Handle, propagate and return any error from that initialization, and then
set pkgHandle and configDone only after successful initialization so global APIs
observe NetNS, RetryInterrupted, and any socket setup correctly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2b756e14-a8dc-48bd-8333-43dfc65a9e93

📥 Commits

Reviewing files that changed from the base of the PR and between 40804be and 9d05034.

📒 Files selected for processing (3)
  • handle_linux.go
  • handle_linux_test.go
  • link_linux.go
✅ Files skipped from review due to trivial changes (1)
  • handle_linux_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • link_linux.go

Comment thread handle_linux.go
This commit introduces the funcion ConfigureHandle to replace the global package
handle with one configured using the given HandleOptions. It can only be called
once per process.

Signed-off-by: Timo Beckers <timo@incline.eu>
@ti-mo ti-mo force-pushed the tb/handle-options branch from 9d05034 to 6b21e75 Compare April 13, 2026 12:21
@SchSeba

SchSeba commented May 14, 2026

Copy link
Copy Markdown
Contributor

Hi @ti-mo any update on this one?
it's blocking as from bump the lib on k8snetworkplumbingwg/sriov-cni#404 (comment)

Thanks!

@SchSeba

SchSeba commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Hi @ti-mo any update on this PR it's blocking as from bump the package

@ti-mo

ti-mo commented Jun 10, 2026

Copy link
Copy Markdown
Contributor Author

@SchSeba Why are you pinging me? :) I don't maintain this library, so I can't merge this. I also can't request reviews from maintainers, apparently no one got assigned.

@aboch ?

@SchSeba

SchSeba commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Hi @aboch can I ask your help reviewing this one as it blocks us in k8snetworkplumbingwg/sriov-cni#404

Thanks!

@aboch

aboch commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

LGTM

3 independent changes commits, merging as is.

@aboch aboch merged commit bab08b3 into vishvananda:main Jun 24, 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.

5 participants