@@ -12,9 +12,22 @@ import (
1212// Empty handle used by the netlink package methods
1313var pkgHandle = & Handle {}
1414
15+ // HandleOptions defines the options for creating a netlink Handle, allowing the
16+ // caller to customize its behaviour.
1517type HandleOptions struct {
16- collectVFInfo bool
17- retryInterrupted bool
18+ // DisableVFInfoCollection controls whether to fetch VF information for each
19+ // link. This is an expensive operation and should be disabled if the caller
20+ // does not need the VF information.
21+ DisableVFInfoCollection bool
22+
23+ // RetryInterrupted controls whether to automatically retry dump operations a
24+ // number of times if they fail with EINTR before finally returning
25+ // [ErrDumpInterrupted].
26+ RetryInterrupted bool
27+
28+ // NetNS specifies the network namespace to operate on. If not set, the
29+ // current network namespace will be used.
30+ NetNS * netns.NsHandle
1831}
1932
2033// Handle is a handle for the netlink requests on a
@@ -29,16 +42,11 @@ type Handle struct {
2942}
3043
3144// DisableVFInfoCollection configures the handle to skip VF information fetching
45+ //
46+ // Deprecated: Use [NewHandleWithOptions] and set
47+ // [HandleOptions.DisableVFInfoCollection] instead.
3248func (h * Handle ) DisableVFInfoCollection () * Handle {
33- h .options .collectVFInfo = false
34- return h
35- }
36-
37- // RetryInterrupted configures the Handle to automatically retry dump operations
38- // a number of times if they fail with EINTR before finally returning
39- // [ErrDumpInterrupted].
40- func (h * Handle ) RetryInterrupted () * Handle {
41- h .options .retryInterrupted = true
49+ h .options .DisableVFInfoCollection = true
4250 return h
4351}
4452
@@ -69,7 +77,8 @@ func (h *Handle) SupportsNetlinkFamily(nlFamily int) bool {
6977// If no families are specified, all the families the netlink package
7078// supports will be automatically added.
7179func NewHandle (nlFamilies ... int ) (* Handle , error ) {
72- return newHandle (netns .None (), netns .None (), nlFamilies ... )
80+ none := netns .None ()
81+ return newHandle (none , HandleOptions {NetNS : & none }, nlFamilies ... )
7382}
7483
7584// SetSocketTimeout sets the send and receive timeout for each socket in the
@@ -147,24 +156,36 @@ func (h *Handle) SetStrictCheck(state bool) error {
147156// specified by ns. If ns=netns.None(), current network namespace
148157// will be assumed
149158func NewHandleAt (ns netns.NsHandle , nlFamilies ... int ) (* Handle , error ) {
150- return newHandle (ns , netns .None (), nlFamilies ... )
159+ return newHandle (netns .None (), HandleOptions { NetNS : & ns } , nlFamilies ... )
151160}
152161
153162// NewHandleAtFrom works as NewHandle but allows client to specify the
154163// new and the origin netns Handle.
155164func NewHandleAtFrom (newNs , curNs netns.NsHandle ) (* Handle , error ) {
156- return newHandle (newNs , curNs )
165+ return newHandle (curNs , HandleOptions { NetNS : & newNs } )
157166}
158167
159- func newHandle (newNs , curNs netns.NsHandle , nlFamilies ... int ) (* Handle , error ) {
168+ // NewHandleWithOptions returns a Handle created using the specified options. If
169+ // opts is nil, default options will be used.
170+ func NewHandleWithOptions (opts HandleOptions , nlFamilies ... int ) (* Handle , error ) {
171+ return newHandle (netns .None (), opts , nlFamilies ... )
172+ }
173+
174+ func newHandle (curNs netns.NsHandle , opts HandleOptions , nlFamilies ... int ) (* Handle , error ) {
160175 h := & Handle {
161176 sockets : map [int ]* nl.SocketHandle {},
162- options : HandleOptions { collectVFInfo : true } ,
177+ options : opts ,
163178 }
164179 fams := nl .SupportedNlFamilies
165180 if len (nlFamilies ) != 0 {
166181 fams = nlFamilies
167182 }
183+
184+ newNs := netns .None ()
185+ if opts .NetNS != nil {
186+ newNs = * opts .NetNS
187+ }
188+
168189 for _ , f := range fams {
169190 s , err := nl .GetNetlinkSocketAt (newNs , curNs , f )
170191 if err != nil {
@@ -208,6 +229,6 @@ func (h *Handle) newNetlinkRequest(proto, flags int) *nl.NetlinkRequest {
208229 },
209230 Sockets : h .sockets ,
210231
211- RetryInterrupted : h .options .retryInterrupted ,
232+ RetryInterrupted : h .options .RetryInterrupted ,
212233 }
213234}
0 commit comments