Skip to content

Commit 03c80dd

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 03c80dd

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

handle_linux.go

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,22 @@ import (
1212
// Empty handle used by the netlink package methods
1313
var pkgHandle = &Handle{}
1414

15+
// HandleOptions defines the options for creating a netlink Handle, allowing the
16+
// caller to customize its behaviour.
1517
type 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.
3248
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
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.
7179
func 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
149158
func 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.
155164
func 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
}

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)