Skip to content

Commit 5797a29

Browse files
committed
handle: add NewHandleWithOptions(), deprecate DisableVFInfoCollection()
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>
1 parent 5bcad1d commit 5797a29

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

handle_linux.go

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,19 @@ import (
1313
var pkgHandle = &Handle{}
1414

1515
type 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.
3246
func (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.
7177
func 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
149155
func 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.
155161
func 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
}

link_linux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,7 +2029,7 @@ func (h *Handle) LinkByName(name string) (Link, error) {
20292029
msg := nl.NewIfInfomsg(unix.AF_UNSPEC)
20302030
req.AddData(msg)
20312031

2032-
if h.options.collectVFInfo {
2032+
if !h.options.DisableVFInfoCollection {
20332033
attr := nl.NewRtAttr(unix.IFLA_EXT_MASK, nl.Uint32Attr(nl.RTEXT_FILTER_VF))
20342034
req.AddData(attr)
20352035
}
@@ -2077,7 +2077,7 @@ func (h *Handle) LinkByAlias(alias string) (Link, error) {
20772077
msg := nl.NewIfInfomsg(unix.AF_UNSPEC)
20782078
req.AddData(msg)
20792079

2080-
if h.options.collectVFInfo {
2080+
if !h.options.DisableVFInfoCollection {
20812081
attr := nl.NewRtAttr(unix.IFLA_EXT_MASK, nl.Uint32Attr(nl.RTEXT_FILTER_VF))
20822082
req.AddData(attr)
20832083
}
@@ -2108,7 +2108,7 @@ func (h *Handle) LinkByIndex(index int) (Link, error) {
21082108
msg := nl.NewIfInfomsg(unix.AF_UNSPEC)
21092109
msg.Index = int32(index)
21102110
req.AddData(msg)
2111-
if h.options.collectVFInfo {
2111+
if !h.options.DisableVFInfoCollection {
21122112
attr := nl.NewRtAttr(unix.IFLA_EXT_MASK, nl.Uint32Attr(nl.RTEXT_FILTER_VF))
21132113
req.AddData(attr)
21142114
}

0 commit comments

Comments
 (0)