From d52869b6f51df97eb9630699555db4b0dbf6ecc6 Mon Sep 17 00:00:00 2001 From: Benjamin Somers Date: Wed, 28 Oct 2026 12:17:36 +0000 Subject: [PATCH 1/2] devlink: Add support for function capabilities Signed-off-by: Benjamin Somers --- devlink.go | 24 ++++++++++++++++++------ devlink_linux.go | 40 ++++++++++++++++++++++++++++++++++++++++ nl/devlink_linux.go | 8 ++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/devlink.go b/devlink.go index 860ef0812..97915b8fd 100644 --- a/devlink.go +++ b/devlink.go @@ -23,16 +23,28 @@ type DevlinkDevice struct { // DevlinkPortFn represents port function and its attributes type DevlinkPortFn struct { - HwAddr net.HardwareAddr - State uint8 - OpState uint8 + HwAddr net.HardwareAddr + State uint8 + OpState uint8 + RoCE bool + RoCESupported bool + Migratable bool + MigratableSupported bool + IPsecCrypto bool + IPsecCryptoSupported bool + IPsecPacket bool + IPsecPacketSupported bool } // DevlinkPortFnSetAttrs represents attributes to set type DevlinkPortFnSetAttrs struct { - FnAttrs DevlinkPortFn - HwAddrValid bool - StateValid bool + FnAttrs DevlinkPortFn + HwAddrValid bool + StateValid bool + RoCEValid bool + MigratableValid bool + IPsecCryptoValid bool + IPsecPacketValid bool } // DevlinkPort represents port and its attributes diff --git a/devlink_linux.go b/devlink_linux.go index b70e7ef28..6397a8d0c 100644 --- a/devlink_linux.go +++ b/devlink_linux.go @@ -511,6 +511,16 @@ func (port *DevlinkPort) parseAttributes(attrs []syscall.NetlinkRouteAttr) error port.Fn.State = uint8(nested.Value[0]) case nl.DEVLINK_PORT_FN_ATTR_OPSTATE: port.Fn.OpState = uint8(nested.Value[0]) + case nl.DEVLINK_PORT_FN_ATTR_CAPS: + caps := nl.DeserializeUint32Bitfield(nested.Value) + port.Fn.RoCESupported = caps.Selector&nl.DEVLINK_PORT_FN_CAP_ROCE != 0 + port.Fn.RoCE = port.Fn.RoCESupported && caps.Value&nl.DEVLINK_PORT_FN_CAP_ROCE != 0 + port.Fn.MigratableSupported = caps.Selector&nl.DEVLINK_PORT_FN_CAP_MIGRATABLE != 0 + port.Fn.Migratable = port.Fn.MigratableSupported && caps.Value&nl.DEVLINK_PORT_FN_CAP_MIGRATABLE != 0 + port.Fn.IPsecCryptoSupported = caps.Selector&nl.DEVLINK_PORT_FN_CAP_IPSEC_CRYPTO != 0 + port.Fn.IPsecCrypto = port.Fn.IPsecCryptoSupported && caps.Value&nl.DEVLINK_PORT_FN_CAP_IPSEC_CRYPTO != 0 + port.Fn.IPsecPacketSupported = caps.Selector&nl.DEVLINK_PORT_FN_CAP_IPSEC_PACKET != 0 + port.Fn.IPsecPacket = port.Fn.IPsecPacketSupported && caps.Value&nl.DEVLINK_PORT_FN_CAP_IPSEC_PACKET != 0 } } case nl.DEVLINK_ATTR_PORT_NUMBER: @@ -905,6 +915,36 @@ func (h *Handle) DevlinkPortFnSet(Bus string, Device string, PortIndex uint32, F if FnAttrs.StateValid { fnAttr.AddRtAttr(nl.DEVLINK_PORT_FN_ATTR_STATE, nl.Uint8Attr(FnAttrs.FnAttrs.State)) } + + var caps nl.Uint32Bitfield + if FnAttrs.RoCEValid { + caps.Selector |= nl.DEVLINK_PORT_FN_CAP_ROCE + if FnAttrs.FnAttrs.RoCE { + caps.Value |= nl.DEVLINK_PORT_FN_CAP_ROCE + } + } + if FnAttrs.MigratableValid { + caps.Selector |= nl.DEVLINK_PORT_FN_CAP_MIGRATABLE + if FnAttrs.FnAttrs.Migratable { + caps.Value |= nl.DEVLINK_PORT_FN_CAP_MIGRATABLE + } + } + if FnAttrs.IPsecCryptoValid { + caps.Selector |= nl.DEVLINK_PORT_FN_CAP_IPSEC_CRYPTO + if FnAttrs.FnAttrs.IPsecCrypto { + caps.Value |= nl.DEVLINK_PORT_FN_CAP_IPSEC_CRYPTO + } + } + if FnAttrs.IPsecPacketValid { + caps.Selector |= nl.DEVLINK_PORT_FN_CAP_IPSEC_PACKET + if FnAttrs.FnAttrs.IPsecPacket { + caps.Value |= nl.DEVLINK_PORT_FN_CAP_IPSEC_PACKET + } + } + if caps.Selector != 0 { + fnAttr.AddRtAttr(nl.DEVLINK_PORT_FN_ATTR_CAPS, caps.Serialize()) + } + req.AddData(fnAttr) _, err = req.Execute(unix.NETLINK_GENERIC, 0) diff --git a/nl/devlink_linux.go b/nl/devlink_linux.go index 940dd70e4..df1e500e4 100644 --- a/nl/devlink_linux.go +++ b/nl/devlink_linux.go @@ -106,6 +106,7 @@ const ( DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR = 1 DEVLINK_PORT_FN_ATTR_STATE = 2 DEVLINK_PORT_FN_ATTR_OPSTATE = 3 + DEVLINK_PORT_FN_ATTR_CAPS = 4 ) const ( @@ -118,6 +119,13 @@ const ( DEVLINK_PORT_FN_OPSTATE_ATTACHED = 1 ) +const ( + DEVLINK_PORT_FN_CAP_ROCE = 1 << 0 + DEVLINK_PORT_FN_CAP_MIGRATABLE = 1 << 1 + DEVLINK_PORT_FN_CAP_IPSEC_CRYPTO = 1 << 2 + DEVLINK_PORT_FN_CAP_IPSEC_PACKET = 1 << 3 +) + const ( DEVLINK_RESOURCE_UNIT_ENTRY uint8 = 0 ) From 29f6ae62bb2065f19d69eb6b2f056d9b504cedc0 Mon Sep 17 00:00:00 2001 From: Benjamin Somers Date: Wed, 28 Oct 2026 12:26:32 +0000 Subject: [PATCH 2/2] devlink: Support device-scoped port list Signed-off-by: Benjamin Somers --- devlink_linux.go | 49 +++++++++++++++++++++++++++++++++++++++--- devlink_unspecified.go | 4 ++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/devlink_linux.go b/devlink_linux.go index 6397a8d0c..4a0d6d088 100644 --- a/devlink_linux.go +++ b/devlink_linux.go @@ -562,7 +562,7 @@ func parseDevLinkAllPortList(msgs [][]byte) ([]*DevlinkPort, error) { return ports, nil } -// DevLinkGetPortList provides a pointer to devlink ports and nil error, +// DevLinkGetAllPortList provides a pointer to devlink ports and nil error, // otherwise returns an error code. // If the returned error is [ErrDumpInterrupted], results may be inconsistent // or incomplete. @@ -589,7 +589,7 @@ func (h *Handle) DevLinkGetAllPortList() ([]*DevlinkPort, error) { return ports, executeErr } -// DevLinkGetPortList provides a pointer to devlink ports and nil error, +// DevLinkGetAllPortList provides a pointer to devlink ports and nil error, // otherwise returns an error code. // If the returned error is [ErrDumpInterrupted], results may be inconsistent // or incomplete. @@ -597,6 +597,49 @@ func DevLinkGetAllPortList() ([]*DevlinkPort, error) { return pkgHandle().DevLinkGetAllPortList() } +// DevLinkGetPortList provides a pointer to devlink ports and nil error, +// otherwise returns an error code. The ports are scoped to the provided +// device. +// If the returned error is [ErrDumpInterrupted], results may be inconsistent +// or incomplete. +func (h *Handle) DevLinkGetPortList(Bus string, Device string) ([]*DevlinkPort, error) { + _, req, err := h.createCmdReq(nl.DEVLINK_CMD_PORT_GET, Bus, Device) + if err != nil { + return nil, err + } + + req.Flags |= unix.NLM_F_DUMP + msgs, executeErr := req.Execute(unix.NETLINK_GENERIC, 0) + if executeErr != nil && !errors.Is(executeErr, ErrDumpInterrupted) { + return nil, executeErr + } + + ports, err := parseDevLinkAllPortList(msgs) + if err != nil { + return nil, err + } + + // Linux kernels prior to 6.6 ignored attributes passed to the dump command, + // leading to a full dump. + scoped := make([]*DevlinkPort, 0, len(ports)) + for _, port := range ports { + if port.BusName == Bus && port.DeviceName == Device { + scoped = append(scoped, port) + } + } + + return scoped, executeErr +} + +// DevLinkGetPortList provides a pointer to devlink ports and nil error, +// otherwise returns an error code. The ports are scoped to the provided +// device. +// If the returned error is [ErrDumpInterrupted], results may be inconsistent +// or incomplete. +func DevLinkGetPortList(Bus string, Device string) ([]*DevlinkPort, error) { + return pkgHandle().DevLinkGetPortList(Bus, Device) +} + func parseDevlinkPortMsg(msgs [][]byte) (*DevlinkPort, error) { m := msgs[0] attrs, err := nl.ParseRouteAttr(m[nl.SizeofGenlmsg:]) @@ -610,7 +653,7 @@ func parseDevlinkPortMsg(msgs [][]byte) (*DevlinkPort, error) { return port, nil } -// DevLinkGetPortByIndexprovides a pointer to devlink device and nil error, +// DevLinkGetPortByIndex provides a pointer to devlink device and nil error, // otherwise returns an error code. func (h *Handle) DevLinkGetPortByIndex(Bus string, Device string, PortIndex uint32) (*DevlinkPort, error) { diff --git a/devlink_unspecified.go b/devlink_unspecified.go index 056ae667a..d23819c6c 100644 --- a/devlink_unspecified.go +++ b/devlink_unspecified.go @@ -19,6 +19,10 @@ func DevLinkGetAllPortList() ([]*DevlinkPort, error) { return nil, ErrNotImplemented } +func DevLinkGetPortList(Bus string, Device string) ([]*DevlinkPort, error) { + return nil, ErrNotImplemented +} + func DevlinkGetDeviceResources(bus string, device string) (*DevlinkResources, error) { return nil, ErrNotImplemented }