Add command to set devlink device switchdev mode

Devlink device currently has legacy and switchdev mode.
Add an API to set devlink device mode for discovered devlink device.

Signed-off-by: Parav Pandit <parav@mellanox.com>
This commit is contained in:
Parav Pandit 2019-03-23 20:41:55 -05:00 committed by Flavio Crisciani
parent bcb80b237c
commit fd97bf4e47
3 changed files with 58 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package netlink
import (
"syscall"
"fmt"
"github.com/vishvananda/netlink/nl"
"golang.org/x/sys/unix"
)
@ -42,6 +43,16 @@ func parseDevLinkDeviceList(msgs [][]byte) ([]*DevlinkDevice, error) {
return devices, nil
}
func eswitchStringToMode(modeName string) (uint16, error) {
if modeName == "legacy" {
return nl.DEVLINK_ESWITCH_MODE_LEGACY, nil
} else if modeName == "switchdev" {
return nl.DEVLINK_ESWITCH_MODE_SWITCHDEV, nil
} else {
return 0xffff, fmt.Errorf("invalid switchdev mode")
}
}
func parseEswitchMode(mode uint16) string {
var eswitchMode = map[uint16]string{
nl.DEVLINK_ESWITCH_MODE_LEGACY: "legacy",
@ -230,3 +241,32 @@ func (h *Handle) DevLinkGetDeviceByName(Bus string, Device string) (*DevlinkDevi
func DevLinkGetDeviceByName(Bus string, Device string) (*DevlinkDevice, error) {
return pkgHandle.DevLinkGetDeviceByName(Bus, Device)
}
// DevLinkSetEswitchMode sets eswitch mode if able to set successfully or
// returns an error code.
// Equivalent to: `devlink dev eswitch set $dev mode switchdev`
// Equivalent to: `devlink dev eswitch set $dev mode legacy`
func (h *Handle) DevLinkSetEswitchMode(Dev *DevlinkDevice, NewMode string) error {
mode, err := eswitchStringToMode(NewMode)
if err != nil {
return err
}
_, req, err := h.createCmdReq(nl.DEVLINK_CMD_ESWITCH_SET, Dev.BusName, Dev.DeviceName)
if err != nil {
return err
}
req.AddData(nl.NewRtAttr(nl.DEVLINK_ATTR_ESWITCH_MODE, nl.Uint16Attr(mode)))
_, err = req.Execute(unix.NETLINK_GENERIC, 0)
return err
}
// DevLinkSetEswitchMode sets eswitch mode if able to set successfully or
// returns an error code.
// Equivalent to: `devlink dev eswitch set $dev mode switchdev`
// Equivalent to: `devlink dev eswitch set $dev mode legacy`
func DevLinkSetEswitchMode(Dev *DevlinkDevice, NewMode string) error {
return pkgHandle.DevLinkSetEswitchMode(Dev, NewMode)
}

View File

@ -23,3 +23,20 @@ func TestDevLinkGetDeviceByName(t *testing.T) {
t.Fatal(err)
}
}
func TestDevLinkSetEswitchMode(t *testing.T) {
minKernelRequired(t, 4, 12)
setUpNetlinkTestWithKModule(t, "devlink")
dev, err := DevLinkGetDeviceByName("foo", "bar")
if err != nil {
t.Fatal(err)
}
err = DevLinkSetEswitchMode(dev, "switchdev")
if err != nil {
t.Fatal(err)
}
err = DevLinkSetEswitchMode(dev, "legacy")
if err != nil {
t.Fatal(err)
}
}

View File

@ -11,6 +11,7 @@ const (
const (
DEVLINK_CMD_GET = 1
DEVLINK_CMD_ESWITCH_GET = 29
DEVLINK_CMD_ESWITCH_SET = 30
)
const (