Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 8 additions & 125 deletions devlink_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,114 +3,15 @@
import (
"errors"
"fmt"
"net"
"strings"
"syscall"

"github.com/vishvananda/netlink/nl"
"golang.org/x/sys/unix"
)

// DevlinkDevEswitchAttr represents device's eswitch attributes
type DevlinkDevEswitchAttr struct {
Mode string
InlineMode string
EncapMode string
}

// DevlinkDevAttrs represents device attributes
type DevlinkDevAttrs struct {
Eswitch DevlinkDevEswitchAttr
}

// DevlinkDevice represents device and its attributes
type DevlinkDevice struct {
BusName string
DeviceName string
Attrs DevlinkDevAttrs
}

// DevlinkPortFn represents port function and its attributes
type DevlinkPortFn struct {
HwAddr net.HardwareAddr
State uint8
OpState uint8
}

// DevlinkPortFnSetAttrs represents attributes to set
type DevlinkPortFnSetAttrs struct {
FnAttrs DevlinkPortFn
HwAddrValid bool
StateValid bool
}

// DevlinkPort represents port and its attributes
type DevlinkPort struct {
BusName string
DeviceName string
PortIndex uint32
PortType uint16
NetdeviceName string
NetdevIfIndex uint32
RdmaDeviceName string
PortFlavour uint16
Fn *DevlinkPortFn
PortNumber *uint32
PfNumber *uint16
VfNumber *uint16
SfNumber *uint32
ControllerNumber *uint32
External *bool
}

type DevLinkPortAddAttrs struct {
Controller uint32
SfNumber uint32
PortIndex uint32
PfNumber uint16
SfNumberValid bool
PortIndexValid bool
ControllerValid bool
}

// DevlinkDeviceInfo represents devlink info
type DevlinkDeviceInfo struct {
Driver string
SerialNumber string
BoardID string
FwApp string
FwAppBoundleID string
FwAppName string
FwBoundleID string
FwMgmt string
FwMgmtAPI string
FwMgmtBuild string
FwNetlist string
FwNetlistBuild string
FwPsidAPI string
FwUndi string
}

// DevlinkResource represents a device resource
type DevlinkResource struct {
Name string
ID uint64
Size uint64
SizeNew uint64
SizeMin uint64
SizeMax uint64
SizeGranularity uint64
PendingChange bool
Unit uint8
SizeValid bool
OCCValid bool
OCCSize uint64
Parent *DevlinkResource
Children []DevlinkResource
}

// parseAttributes parses provided Netlink Attributes and populates DevlinkResource, returns error if occured
func (dlr *DevlinkResource) parseAttributes(attrs map[uint16]syscall.NetlinkRouteAttr) error {

Check failure on line 14 in devlink_linux.go

View workflow job for this annotation

GitHub Actions / build

undefined: DevlinkResource
var attr syscall.NetlinkRouteAttr
var ok bool

Expand Down Expand Up @@ -201,15 +102,8 @@
return nil
}

// DevlinkResources represents all devlink resources of a devlink device
type DevlinkResources struct {
Bus string
Device string
Resources []DevlinkResource
}

// parseAttributes parses provided Netlink Attributes and populates DevlinkResources, returns error if occured
func (dlrs *DevlinkResources) parseAttributes(attrs map[uint16]syscall.NetlinkRouteAttr) error {

Check failure on line 106 in devlink_linux.go

View workflow job for this annotation

GitHub Actions / build

undefined: DevlinkResources
var attr syscall.NetlinkRouteAttr
var ok bool

Expand Down Expand Up @@ -254,24 +148,8 @@
return nil
}

// DevlinkParam represents parameter of the device
type DevlinkParam struct {
Name string
IsGeneric bool
Type uint8 // possible values are in nl.DEVLINK_PARAM_TYPE_* constants
Values []DevlinkParamValue
}

// DevlinkParamValue contains values of the parameter
// Data field contains specific type which can be casted by unsing info from the DevlinkParam.Type field
type DevlinkParamValue struct {
rawData []byte
Data interface{}
CMODE uint8 // possible values are in nl.DEVLINK_PARAM_CMODE_* constants
}

// parseAttributes parses provided Netlink Attributes and populates DevlinkParam, returns error if occured
func (dlp *DevlinkParam) parseAttributes(attrs []syscall.NetlinkRouteAttr) error {

Check failure on line 152 in devlink_linux.go

View workflow job for this annotation

GitHub Actions / build

undefined: DevlinkParam
var valuesList [][]syscall.NetlinkRouteAttr
for _, attr := range attrs {
switch attr.Attr.Type {
Expand Down Expand Up @@ -310,7 +188,7 @@
return nil
}

func (dlpv *DevlinkParamValue) parseAttributes(attrs []syscall.NetlinkRouteAttr, paramType uint8) error {

Check failure on line 191 in devlink_linux.go

View workflow job for this annotation

GitHub Actions / build

undefined: DevlinkParamValue
for _, attr := range attrs {
nattrs, err := nl.ParseRouteAttr(attr.Value)
if err != nil {
Expand Down Expand Up @@ -355,7 +233,7 @@
return nil
}

func parseDevLinkDeviceList(msgs [][]byte) ([]*DevlinkDevice, error) {

Check failure on line 236 in devlink_linux.go

View workflow job for this annotation

GitHub Actions / build

undefined: DevlinkDevice
devices := make([]*DevlinkDevice, 0, len(msgs))
for _, m := range msgs {
attrs, err := nl.ParseRouteAttr(m[nl.SizeofGenlmsg:])
Expand Down Expand Up @@ -419,7 +297,7 @@
}
}

func (d *DevlinkDevice) parseAttributes(attrs []syscall.NetlinkRouteAttr) error {

Check failure on line 300 in devlink_linux.go

View workflow job for this annotation

GitHub Actions / build

undefined: DevlinkDevice
for _, a := range attrs {
switch a.Attr.Type {
case nl.DEVLINK_ATTR_BUS_NAME:
Expand All @@ -437,7 +315,7 @@
return nil
}

func (dev *DevlinkDevice) parseEswitchAttrs(msgs [][]byte) {

Check failure on line 318 in devlink_linux.go

View workflow job for this annotation

GitHub Actions / build

undefined: DevlinkDevice
m := msgs[0]
attrs, err := nl.ParseRouteAttr(m[nl.SizeofGenlmsg:])
if err != nil {
Expand All @@ -446,7 +324,7 @@
dev.parseAttributes(attrs)
}

func (h *Handle) getEswitchAttrs(family *GenlFamily, dev *DevlinkDevice) {

Check failure on line 327 in devlink_linux.go

View workflow job for this annotation

GitHub Actions / build

undefined: DevlinkDevice
msg := &nl.Genlmsg{
Command: nl.DEVLINK_CMD_ESWITCH_GET,
Version: nl.GENL_DEVLINK_VERSION,
Expand Down Expand Up @@ -475,7 +353,7 @@
// otherwise returns an error code.
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
// or incomplete.
func (h *Handle) DevLinkGetDeviceList() ([]*DevlinkDevice, error) {

Check failure on line 356 in devlink_linux.go

View workflow job for this annotation

GitHub Actions / build

undefined: DevlinkDevice
f, err := h.GenlFamilyGet(nl.GENL_DEVLINK_NAME)
if err != nil {
return nil, err
Expand Down Expand Up @@ -506,7 +384,7 @@
//
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
// or incomplete.
func DevLinkGetDeviceList() ([]*DevlinkDevice, error) {

Check failure on line 387 in devlink_linux.go

View workflow job for this annotation

GitHub Actions / build

undefined: DevlinkDevice
return pkgHandle.DevLinkGetDeviceList()
}

Expand Down Expand Up @@ -857,8 +735,10 @@
return err
}

// DevlinkSplitPort splits the specified devlink port into count logical ports.
// It returns an error if the operation fails.
func DevlinkSplitPort(port *DevlinkPort, count uint32) error {
return pkgHandle.DevlinkSplitPort(port, count);
return pkgHandle.DevlinkSplitPort(port, count)
}

// DevlinkUnsplitPort: unsplit devlink port
Expand All @@ -875,8 +755,11 @@
return err
}

// DevlinkUnsplitPort unsplits a previously split devlink port represented by p.
// It reverses a prior split operation for the provided port and returns an error
// if the operation fails.
func DevlinkUnsplitPort(port *DevlinkPort) error {
return pkgHandle.DevlinkUnsplitPort(port);
return pkgHandle.DevlinkUnsplitPort(port)
}

// DevlinkSetDeviceParam set specific parameter for devlink device
Expand Down Expand Up @@ -1233,4 +1116,4 @@
func parseInfoValue(value []byte) string {
v := strings.ReplaceAll(string(value), "\x00", "")
return strings.TrimSpace(v)
}
}
107 changes: 107 additions & 0 deletions devlink_unspecified.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//go:build !linux
// +build !linux

package netlink

// DevLinkGetDeviceList retrieves the list of devlink devices available on the system.
// On non-Linux builds this function is not implemented and returns nil, ErrNotImplemented.
func DevLinkGetDeviceList() ([]*DevlinkDevice, error) {

Check failure on line 8 in devlink_unspecified.go

View workflow job for this annotation

GitHub Actions / build-macos

undefined: DevlinkDevice
return nil, ErrNotImplemented
}

// DevLinkGetDeviceByName looks up the Devlink device identified by the bus and device names.
// On non-Linux builds this always returns nil and ErrNotImplemented.
func DevLinkGetDeviceByName(Bus string, Device string) (*DevlinkDevice, error) {

Check failure on line 14 in devlink_unspecified.go

View workflow job for this annotation

GitHub Actions / build-macos

undefined: DevlinkDevice
return nil, ErrNotImplemented
}

// DevLinkSetEswitchMode sets the eswitch mode of the specified DevlinkDevice to NewMode.
// On platforms where Devlink is not implemented this returns ErrNotImplemented.
func DevLinkSetEswitchMode(Dev *DevlinkDevice, NewMode string) error {

Check failure on line 20 in devlink_unspecified.go

View workflow job for this annotation

GitHub Actions / build-macos

undefined: DevlinkDevice
return ErrNotImplemented
}

// DevLinkGetAllPortList retrieves the list of all devlink ports available on the system.
// On platforms where devlink functionality is unavailable this returns nil and ErrNotImplemented.
func DevLinkGetAllPortList() ([]*DevlinkPort, error) {

Check failure on line 26 in devlink_unspecified.go

View workflow job for this annotation

GitHub Actions / build-macos

undefined: DevlinkPort
return nil, ErrNotImplemented
}

// DevlinkGetDeviceResources retrieves the resource information for the devlink device specified by bus and device.
// On non-Linux builds this function is unimplemented and returns ErrNotImplemented.
func DevlinkGetDeviceResources(bus string, device string) (*DevlinkResources, error) {

Check failure on line 32 in devlink_unspecified.go

View workflow job for this annotation

GitHub Actions / build-macos

undefined: DevlinkResources
return nil, ErrNotImplemented
}

// DevlinkGetDeviceParams retrieves the devlink parameters for the device specified by bus and device.
// On non-Linux builds this function is not implemented and returns ErrNotImplemented.
func DevlinkGetDeviceParams(bus string, device string) ([]*DevlinkParam, error) {

Check failure on line 38 in devlink_unspecified.go

View workflow job for this annotation

GitHub Actions / build-macos

undefined: DevlinkParam
return nil, ErrNotImplemented
}

// DevlinkGetDeviceParamByName retrieves the devlink parameter with the given name for the specified bus and device.
// It returns a pointer to the matching DevlinkParam, or nil and an error if the parameter cannot be obtained.
func DevlinkGetDeviceParamByName(bus string, device string, param string) (*DevlinkParam, error) {

Check failure on line 44 in devlink_unspecified.go

View workflow job for this annotation

GitHub Actions / build-macos

undefined: DevlinkParam
return nil, ErrNotImplemented
}

// DevlinkSplitPort splits the specified devlink port into the given number of logical ports.
// The function returns ErrNotImplemented on platforms where devlink operations are unavailable.
func DevlinkSplitPort(port *DevlinkPort, count uint32) error {

Check failure on line 50 in devlink_unspecified.go

View workflow job for this annotation

GitHub Actions / build-macos

undefined: DevlinkPort
return ErrNotImplemented
}

// DevlinkUnsplitPort attempts to unsplit the specified DevlinkPort.
// It always returns ErrNotImplemented on non-Linux platforms.
func DevlinkUnsplitPort(port *DevlinkPort) error {

Check failure on line 56 in devlink_unspecified.go

View workflow job for this annotation

GitHub Actions / build-macos

undefined: DevlinkPort
return ErrNotImplemented
}

// DevlinkSetDeviceParam sets the named parameter for a devlink device identified by bus and device,
// using cmode to indicate the change mode and value as the new parameter value.
// It returns an error if the parameter could not be set.
func DevlinkSetDeviceParam(bus string, device string, param string, cmode uint8, value interface{}) error {
return ErrNotImplemented
}

// DevLinkGetPortByIndex retrieves the devlink port identified by the bus, device, and port index.
// On non-Linux builds this function returns nil and ErrNotImplemented.
func DevLinkGetPortByIndex(Bus string, Device string, PortIndex uint32) (*DevlinkPort, error) {

Check failure on line 69 in devlink_unspecified.go

View workflow job for this annotation

GitHub Actions / build-macos

undefined: DevlinkPort
return nil, ErrNotImplemented
}

// DevLinkPortAdd adds a new devlink port to the specified device.
//
// Bus and Device identify the target device. Flavour selects the port flavour to create;
// Attrs contains creation attributes for the new port.
//
// On success returns the created *DevlinkPort. On unsupported platforms returns nil and ErrNotImplemented.
func DevLinkPortAdd(Bus string, Device string, Flavour uint16, Attrs DevLinkPortAddAttrs) (*DevlinkPort, error) {
return nil, ErrNotImplemented
}

// DevLinkPortDel deletes the devlink port identified by bus, device, and port index.
// On non-Linux builds this always returns ErrNotImplemented.
func DevLinkPortDel(Bus string, Device string, PortIndex uint32) error {
return ErrNotImplemented
}

// DevlinkPortFnSet sets function-specific attributes for the devlink port identified
// by the given PCI `Bus` and `Device` strings and `PortIndex`.
// The `FnAttrs` parameter specifies the attributes to apply.
// On non-Linux builds this function is not implemented and returns ErrNotImplemented.
func DevlinkPortFnSet(Bus string, Device string, PortIndex uint32, FnAttrs DevlinkPortFnSetAttrs) error {
return ErrNotImplemented
}

// DevlinkGetDeviceInfoByName retrieves the DevlinkDeviceInfo for a devlink device identified by its bus and device names.
// On non-Linux builds this function is not implemented and returns ErrNotImplemented.
func DevlinkGetDeviceInfoByName(Bus string, Device string) (*DevlinkDeviceInfo, error) {
return nil, ErrNotImplemented
}

// DevlinkGetDeviceInfoByNameAsMap returns a map of device information keyed by attribute name for the device identified by bus and device.
// On non-Linux platforms this function returns ErrNotImplemented.
func DevlinkGetDeviceInfoByNameAsMap(Bus string, Device string) (map[string]string, error) {
return nil, ErrNotImplemented
}