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
1 change: 1 addition & 0 deletions nl/vdpa_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
VDPA_CMD_DEV_GET /* can dump */
VDPA_CMD_DEV_CONFIG_GET /* can dump */
VDPA_CMD_DEV_VSTATS_GET
VDPA_CMD_DEV_ATTR_SET
)

const (
Expand Down
31 changes: 31 additions & 0 deletions vdpa_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ func VDPAGetMGMTDevByBusAndName(bus, name string) (*VDPAMGMTDev, error) {
return pkgHandle.VDPAGetMGMTDevByBusAndName(bus, name)
}

func VDPASetAttr(name string, params VDPANewDevParams) error {
return pkgHandle.VDPASetAttr(name, params)
}

type vdpaNetlinkMessage []syscall.NetlinkRouteAttr

func (id *vdpaDevID) parseIDAttribute(attr syscall.NetlinkRouteAttr) {
Expand Down Expand Up @@ -489,3 +493,30 @@ func (h *Handle) VDPAGetMGMTDevByBusAndName(bus, name string) (*VDPAMGMTDev, err
}
return devs[0], nil
}

// VDPASetAttr sets other values to attributes of a VDPA device
// Equivalent to: `vdpa dev set name <name> ...`
func (h *Handle) VDPASetAttr(name string, params VDPANewDevParams) error {
attrs := []*nl.RtAttr{}

if len(params.MACAddr) != 0 {
attrs = append(attrs, nl.NewRtAttr(nl.VDPA_ATTR_DEV_NET_CFG_MACADDR, params.MACAddr))
}
if params.MaxVQP > 0 {
attrs = append(attrs, nl.NewRtAttr(nl.VDPA_ATTR_DEV_NET_CFG_MAX_VQP, nl.Uint16Attr(params.MaxVQP)))
}
if params.MTU > 0 {
attrs = append(attrs, nl.NewRtAttr(nl.VDPA_ATTR_DEV_NET_CFG_MTU, nl.Uint16Attr(params.MTU)))
}
if params.Features > 0 {
attrs = append(attrs, nl.NewRtAttr(nl.VDPA_ATTR_DEV_FEATURES, nl.Uint64Attr(params.Features)))
}

if len(attrs) == 0 {
return fmt.Errorf("parameters were not provided")
}
attrs = append(attrs, nl.NewRtAttr(nl.VDPA_ATTR_DEV_NAME, nl.ZeroTerminated(name)))

_, err := h.vdpaRequest(nl.VDPA_CMD_DEV_ATTR_SET, 0, attrs)
return err
}
87 changes: 87 additions & 0 deletions vdpa_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package netlink

import (
"errors"
"net"
"syscall"
"testing"

Expand Down Expand Up @@ -222,6 +223,88 @@ func TestVDPAGetDevConfigByName_Unknowm(t *testing.T) {
}
}

func TestVDPASetMAC(t *testing.T) {
defer setupVDPATest(t, nl.VDPA_CMD_DEV_GET, nl.VDPA_CMD_DEV_CONFIG_GET, nl.VDPA_CMD_DEV_ATTR_SET)()
firstMac, _ := net.ParseMAC("02:11:22:33:44:68")
lastMacStr := "72:84:65:12:09:77"
lastMac, _ := net.ParseMAC(lastMacStr)
if err := createVDPATestDevWithParams(VDPANewDevParams{MACAddr: firstMac}); err != nil {
t.Fatalf("failed to create VDPA device: %v", err)
}
if err := VDPASetAttr(vdpaTestDeviceName, VDPANewDevParams{MACAddr: lastMac}); err != nil {
t.Fatalf("failed to set mac to VDPA device: %v", err)
}
dev, err := VDPAGetDevConfigByName(vdpaTestDeviceName)
if err != nil {
t.Fatalf("VDPAGetDevConfigByName failed: %v", err)
}
checkVDPADevConf(t, dev)
if dev.Name != vdpaTestDeviceName {
t.Fatalf("Invalid device received for Get call, expected: %s, actual: %s", vdpaTestDeviceName, dev.Name)
}
newMacStr := dev.Net.Cfg.MACAddr.String()
if newMacStr != lastMacStr {
t.Fatalf("mac address was not properly set. Expected: %s, actual: %s", lastMac, newMacStr)
}
}

func TestVDPASetMACWrongName(t *testing.T) {
defer setupVDPATest(t, nl.VDPA_CMD_DEV_GET, nl.VDPA_CMD_DEV_CONFIG_GET, nl.VDPA_CMD_DEV_ATTR_SET)()
mac, _ := net.ParseMAC("72:84:65:12:09:77")
if err := createVDPATestDev(); err != nil {
t.Fatalf("failed to create VDPA device: %v", err)
}
err := VDPASetAttr("__unknown_device__", VDPANewDevParams{MACAddr: mac})
if !errors.Is(err, syscall.ENODEV) {
t.Fatalf("VDPASetAttr returned unexpected error setting mac to non existing device: %v", err)
}
}

func TestVDPASetEmptyParams(t *testing.T) {
defer setupVDPATest(t, nl.VDPA_CMD_DEV_GET, nl.VDPA_CMD_DEV_CONFIG_GET, nl.VDPA_CMD_DEV_ATTR_SET)()
if err := createVDPATestDev(); err != nil {
t.Fatalf("failed to create VDPA device: %v", err)
}
err := VDPASetAttr(vdpaTestDeviceName, VDPANewDevParams{})
if err == nil {
t.Fatalf("VDPASetAttr did not return an error when params were empty: %v", err)
}
}

func TestVDPASetMTU(t *testing.T) {
defer setupVDPATest(t, nl.VDPA_CMD_DEV_GET, nl.VDPA_CMD_DEV_CONFIG_GET, nl.VDPA_CMD_DEV_ATTR_SET)()
if err := createVDPATestDev(); err != nil {
t.Fatalf("failed to create VDPA device: %v", err)
}
err := VDPASetAttr(vdpaTestDeviceName, VDPANewDevParams{MTU: 3000})
if !errors.Is(err, syscall.ENOTSUP) {
t.Fatalf("VDPASetAttr returned an unexpected error setting mtu to vdpa device: %v", err)
}
}

func TestVDPASetMaxVQP(t *testing.T) {
defer setupVDPATest(t, nl.VDPA_CMD_DEV_GET, nl.VDPA_CMD_DEV_CONFIG_GET, nl.VDPA_CMD_DEV_ATTR_SET)()
if err := createVDPATestDev(); err != nil {
t.Fatalf("failed to create VDPA device: %v", err)
}
err := VDPASetAttr(vdpaTestDeviceName, VDPANewDevParams{MaxVQP: 3000})
if !errors.Is(err, syscall.ENOTSUP) {
t.Fatalf("VDPASetAttr returned an unexpected error setting maxvqp to vdpa device: %v", err)
}
}

func TestVDPASetFeatures(t *testing.T) {
defer setupVDPATest(t, nl.VDPA_CMD_DEV_GET, nl.VDPA_CMD_DEV_CONFIG_GET, nl.VDPA_CMD_DEV_ATTR_SET)()
if err := createVDPATestDev(); err != nil {
t.Fatalf("failed to create VDPA device: %v", err)
}
// Default vdpa_sim_net features without VIRTIO_NET_F_CTRL_MAC_ADDR
err := VDPASetAttr(vdpaTestDeviceName, VDPANewDevParams{Features: 13019316265})
if !errors.Is(err, syscall.ENOTSUP) {
t.Fatalf("VDPASetAttr returned an unexpected error setting features to vdpa device: %v", err)
}
}

func TestSetGetBits(t *testing.T) {
features := SetBits(0, VIRTIO_NET_F_CSUM, VIRTIO_NET_F_MQ)
if !IsBitSet(features, VIRTIO_NET_F_CSUM) || !IsBitSet(features, VIRTIO_NET_F_MQ) {
Expand All @@ -236,6 +319,10 @@ func createVDPATestDev() error {
return VDPANewDev(vdpaTestDeviceName, "", vdpaSimMGMTDev, VDPANewDevParams{})
}

func createVDPATestDevWithParams(params VDPANewDevParams) error {
return VDPANewDev(vdpaTestDeviceName, "", vdpaSimMGMTDev, params)
}

func checkVDPAMGMTDev(t *testing.T, d *VDPAMGMTDev) {
if d == nil {
t.Fatal("VDPA MGMT dev is nil")
Expand Down