diff --git a/handle_linux.go b/handle_linux.go index 4cb0116..df1a9ba 100644 --- a/handle_linux.go +++ b/handle_linux.go @@ -12,13 +12,24 @@ import ( // Empty handle used by the netlink package methods var pkgHandle = &Handle{} -// Handle is an handle for the netlink requests on a +type HandleOptions struct { + lookupByDump bool + collectVFInfo bool +} + +// Handle is a handle for the netlink requests on a // specific network namespace. All the requests on the // same netlink family share the same netlink socket, // which gets released when the handle is Close'd. type Handle struct { - sockets map[int]*nl.SocketHandle - lookupByDump bool + sockets map[int]*nl.SocketHandle + options HandleOptions +} + +// DisableVFInfoCollection configures the handle to skip VF information fetching +func (h *Handle) DisableVFInfoCollection() *Handle { + h.options.collectVFInfo = false + return h } // SetSocketTimeout configures timeout for default netlink sockets @@ -136,7 +147,10 @@ func NewHandleAtFrom(newNs, curNs netns.NsHandle) (*Handle, error) { } func newHandle(newNs, curNs netns.NsHandle, nlFamilies ...int) (*Handle, error) { - h := &Handle{sockets: map[int]*nl.SocketHandle{}} + h := &Handle{ + sockets: map[int]*nl.SocketHandle{}, + options: HandleOptions{collectVFInfo: true}, + } fams := nl.SupportedNlFamilies if len(nlFamilies) != 0 { fams = nlFamilies diff --git a/link_linux.go b/link_linux.go index e26efb4..b2b9da3 100644 --- a/link_linux.go +++ b/link_linux.go @@ -1933,7 +1933,7 @@ func LinkByName(name string) (Link, error) { // filtering a dump of all link names. In this case, if the returned error is // [ErrDumpInterrupted] the result may be missing or outdated. func (h *Handle) LinkByName(name string) (Link, error) { - if h.lookupByDump { + if h.options.lookupByDump { return h.linkByNameDump(name) } @@ -1942,8 +1942,10 @@ func (h *Handle) LinkByName(name string) (Link, error) { 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.collectVFInfo { + attr := nl.NewRtAttr(unix.IFLA_EXT_MASK, nl.Uint32Attr(nl.RTEXT_FILTER_VF)) + req.AddData(attr) + } nameData := nl.NewRtAttr(unix.IFLA_IFNAME, nl.ZeroTerminated(name)) if len(name) > 15 { @@ -1955,7 +1957,7 @@ func (h *Handle) LinkByName(name string) (Link, error) { if err == unix.EINVAL { // older kernels don't support looking up via IFLA_IFNAME // so fall back to dumping all links - h.lookupByDump = true + h.options.lookupByDump = true return h.linkByNameDump(name) } @@ -1979,7 +1981,7 @@ func LinkByAlias(alias string) (Link, error) { // filtering a dump of all link names. In this case, if the returned error is // [ErrDumpInterrupted] the result may be missing or outdated. func (h *Handle) LinkByAlias(alias string) (Link, error) { - if h.lookupByDump { + if h.options.lookupByDump { return h.linkByAliasDump(alias) } @@ -1988,8 +1990,10 @@ func (h *Handle) LinkByAlias(alias string) (Link, error) { 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.collectVFInfo { + attr := nl.NewRtAttr(unix.IFLA_EXT_MASK, nl.Uint32Attr(nl.RTEXT_FILTER_VF)) + req.AddData(attr) + } nameData := nl.NewRtAttr(unix.IFLA_IFALIAS, nl.ZeroTerminated(alias)) req.AddData(nameData) @@ -1998,7 +2002,7 @@ func (h *Handle) LinkByAlias(alias string) (Link, error) { if err == unix.EINVAL { // older kernels don't support looking up via IFLA_IFALIAS // so fall back to dumping all links - h.lookupByDump = true + h.options.lookupByDump = true return h.linkByAliasDump(alias) } @@ -2017,8 +2021,10 @@ func (h *Handle) LinkByIndex(index int) (Link, error) { msg := nl.NewIfInfomsg(unix.AF_UNSPEC) msg.Index = int32(index) req.AddData(msg) - attr := nl.NewRtAttr(unix.IFLA_EXT_MASK, nl.Uint32Attr(nl.RTEXT_FILTER_VF)) - req.AddData(attr) + if h.options.collectVFInfo { + attr := nl.NewRtAttr(unix.IFLA_EXT_MASK, nl.Uint32Attr(nl.RTEXT_FILTER_VF)) + req.AddData(attr) + } return execGetLink(req) }