@@ -13,8 +13,19 @@ import (
1313var pkgHandle = & Handle {}
1414
1515type HandleOptions struct {
16- collectVFInfo bool
17- retryInterrupted bool
16+ // DisableVFInfoCollection controls whether to fetch VF information for each
17+ // link. This is an expensive operation and should be disabled if the caller
18+ // does not need the VF information.
19+ DisableVFInfoCollection bool
20+
21+ // RetryInterrupted controls whether to automatically retry dump operations a
22+ // number of times if they fail with EINTR before finally returning
23+ // [ErrDumpInterrupted].
24+ RetryInterrupted bool
25+
26+ // NetNS specifies the network namespace to operate on. If not set, the
27+ // current network namespace will be used.
28+ NetNS netns.NsHandle
1829}
1930
2031// Handle is a handle for the netlink requests on a
@@ -29,16 +40,11 @@ type Handle struct {
2940}
3041
3142// DisableVFInfoCollection configures the handle to skip VF information fetching
43+ //
44+ // Deprecated: Use [NewHandleWithOptions] and set
45+ // [HandleOptions.DisableVFInfoCollection] instead.
3246func (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
47+ h .options .DisableVFInfoCollection = true
4248 return h
4349}
4450
@@ -69,7 +75,7 @@ func (h *Handle) SupportsNetlinkFamily(nlFamily int) bool {
6975// If no families are specified, all the families the netlink package
7076// supports will be automatically added.
7177func NewHandle (nlFamilies ... int ) (* Handle , error ) {
72- return newHandle (netns .None (), netns . None () , nlFamilies ... )
78+ return newHandle (netns .None (), nil , nlFamilies ... )
7379}
7480
7581// SetSocketTimeout sets the send and receive timeout for each socket in the
@@ -147,26 +153,39 @@ func (h *Handle) SetStrictCheck(state bool) error {
147153// specified by ns. If ns=netns.None(), current network namespace
148154// will be assumed
149155func NewHandleAt (ns netns.NsHandle , nlFamilies ... int ) (* Handle , error ) {
150- return newHandle (ns , netns .None (), nlFamilies ... )
156+ return newHandle (netns .None (), & HandleOptions { NetNS : ns } , nlFamilies ... )
151157}
152158
153159// NewHandleAtFrom works as NewHandle but allows client to specify the
154160// new and the origin netns Handle.
155161func NewHandleAtFrom (newNs , curNs netns.NsHandle ) (* Handle , error ) {
156- return newHandle (newNs , curNs )
162+ return newHandle (curNs , & HandleOptions { NetNS : newNs } )
157163}
158164
159- func newHandle (newNs , curNs netns.NsHandle , nlFamilies ... int ) (* Handle , error ) {
165+ // NewHandleWithOptions returns a Handle created using the specified options. If
166+ // opts is nil, default options will be used.
167+ func NewHandleWithOptions (opts * HandleOptions , nlFamilies ... int ) (* Handle , error ) {
168+ if opts == nil {
169+ opts = & HandleOptions {}
170+ }
171+ return newHandle (netns .None (), opts , nlFamilies ... )
172+ }
173+
174+ func newHandle (curNs netns.NsHandle , opts * HandleOptions , nlFamilies ... int ) (* Handle , error ) {
175+ if opts == nil {
176+ opts = & HandleOptions {}
177+ }
178+
160179 h := & Handle {
161180 sockets : map [int ]* nl.SocketHandle {},
162- options : HandleOptions { collectVFInfo : true } ,
181+ options : * opts ,
163182 }
164183 fams := nl .SupportedNlFamilies
165184 if len (nlFamilies ) != 0 {
166185 fams = nlFamilies
167186 }
168187 for _ , f := range fams {
169- s , err := nl .GetNetlinkSocketAt (newNs , curNs , f )
188+ s , err := nl .GetNetlinkSocketAt (opts . NetNS , curNs , f )
170189 if err != nil {
171190 return nil , err
172191 }
@@ -208,6 +227,6 @@ func (h *Handle) newNetlinkRequest(proto, flags int) *nl.NetlinkRequest {
208227 },
209228 Sockets : h .sockets ,
210229
211- RetryInterrupted : h .options .retryInterrupted ,
230+ RetryInterrupted : h .options .RetryInterrupted ,
212231 }
213232}
0 commit comments