Extend devlink port for function attributes

Extend devlink port for optional port function attributes.

Signed-off-by: Parav Pandit <parav@nvidia.com>
This commit is contained in:
Parav Pandit 2021-04-03 09:59:20 +03:00 committed by Alessandro Boch
parent aa68aabe05
commit 83b6143251
3 changed files with 44 additions and 2 deletions

View File

@ -1,11 +1,11 @@
package netlink package netlink
import ( import (
"syscall"
"fmt" "fmt"
"github.com/vishvananda/netlink/nl" "github.com/vishvananda/netlink/nl"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"net"
"syscall"
) )
// DevlinkDevEswitchAttr represents device's eswitch attributes // DevlinkDevEswitchAttr represents device's eswitch attributes
@ -27,6 +27,13 @@ type DevlinkDevice struct {
Attrs DevlinkDevAttrs Attrs DevlinkDevAttrs
} }
// DevlinkPortFn represents port function and its attributes
type DevlinkPortFn struct {
HwAddr net.HardwareAddr
State uint8
OpState uint8
}
// DevlinkPort represents port and its attributes // DevlinkPort represents port and its attributes
type DevlinkPort struct { type DevlinkPort struct {
BusName string BusName string
@ -37,6 +44,7 @@ type DevlinkPort struct {
NetdevIfIndex uint32 NetdevIfIndex uint32
RdmaDeviceName string RdmaDeviceName string
PortFlavour uint16 PortFlavour uint16
Fn *DevlinkPortFn
} }
func parseDevLinkDeviceList(msgs [][]byte) ([]*DevlinkDevice, error) { func parseDevLinkDeviceList(msgs [][]byte) ([]*DevlinkDevice, error) {
@ -302,6 +310,18 @@ func (port *DevlinkPort) parseAttributes(attrs []syscall.NetlinkRouteAttr) error
port.RdmaDeviceName = string(a.Value) port.RdmaDeviceName = string(a.Value)
case nl.DEVLINK_ATTR_PORT_FLAVOUR: case nl.DEVLINK_ATTR_PORT_FLAVOUR:
port.PortFlavour = native.Uint16(a.Value) port.PortFlavour = native.Uint16(a.Value)
case nl.DEVLINK_ATTR_PORT_FUNCTION:
port.Fn = &DevlinkPortFn{}
for nested := range nl.ParseAttributes(a.Value) {
switch nested.Type {
case nl.DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR:
port.Fn.HwAddr = nested.Value[:]
case nl.DEVLINK_PORT_FN_ATTR_STATE:
port.Fn.State = uint8(nested.Value[0])
case nl.DEVLINK_PORT_FN_ATTR_OPSTATE:
port.Fn.OpState = uint8(nested.Value[0])
}
}
} }
} }
return nil return nil

View File

@ -66,5 +66,8 @@ func TestDevLinkGetPortByIndex(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
t.Log(*p) t.Log(*p)
if p.Fn != nil {
t.Log("function attributes = " , *p.Fn)
}
} }
} }

View File

@ -27,6 +27,7 @@ const (
DEVLINK_ATTR_ESWITCH_INLINE_MODE = 26 DEVLINK_ATTR_ESWITCH_INLINE_MODE = 26
DEVLINK_ATTR_ESWITCH_ENCAP_MODE = 62 DEVLINK_ATTR_ESWITCH_ENCAP_MODE = 62
DEVLINK_ATTR_PORT_FLAVOUR = 77 DEVLINK_ATTR_PORT_FLAVOUR = 77
DEVLINK_ATTR_PORT_FUNCTION = 145
) )
const ( const (
@ -53,6 +54,8 @@ const (
DEVLINK_PORT_FLAVOUR_PCI_PF = 3 DEVLINK_PORT_FLAVOUR_PCI_PF = 3
DEVLINK_PORT_FLAVOUR_PCI_VF = 4 DEVLINK_PORT_FLAVOUR_PCI_VF = 4
DEVLINK_PORT_FLAVOUR_VIRTUAL = 5 DEVLINK_PORT_FLAVOUR_VIRTUAL = 5
DEVLINK_PORT_FLAVOUR_UNUSED = 6
DEVLINK_PORT_FLAVOUR_PCI_SF = 7
) )
const ( const (
@ -61,3 +64,19 @@ const (
DEVLINK_PORT_TYPE_ETH = 2 DEVLINK_PORT_TYPE_ETH = 2
DEVLINK_PORT_TYPE_IB = 3 DEVLINK_PORT_TYPE_IB = 3
) )
const (
DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR = 1
DEVLINK_PORT_FN_ATTR_STATE = 2
DEVLINK_PORT_FN_ATTR_OPSTATE = 3
)
const (
DEVLINK_PORT_FN_STATE_INACTIVE = 0
DEVLINK_PORT_FN_STATE_ACTIVE = 1
)
const (
DEVLINK_PORT_FN_OPSTATE_DETACHED = 0
DEVLINK_PORT_FN_OPSTATE_ATTACHED = 1
)