From 6bbc469a2a5ff5e3ad28d8b265d917385ee196b7 Mon Sep 17 00:00:00 2001 From: Yury Kulazhenkov Date: Tue, 13 Feb 2024 14:41:25 +0200 Subject: [PATCH] Add functions to work with devlink dev parameters Signed-off-by: Yury Kulazhenkov --- pkg/helper/mock/mock_helper.go | 29 ++++ .../internal/lib/netlink/mock/mock_netlink.go | 29 ++++ pkg/host/internal/lib/netlink/netlink.go | 22 +++ pkg/host/internal/network/network.go | 97 ++++++++++- pkg/host/internal/network/network_test.go | 155 ++++++++++++++++++ pkg/host/internal/network/suite_test.go | 21 +++ pkg/host/manager.go | 2 +- pkg/host/mock/mock_host.go | 29 ++++ pkg/host/types/interfaces.go | 7 + 9 files changed, 389 insertions(+), 2 deletions(-) create mode 100644 pkg/host/internal/network/network_test.go create mode 100644 pkg/host/internal/network/suite_test.go diff --git a/pkg/helper/mock/mock_helper.go b/pkg/helper/mock/mock_helper.go index 95053f4709..2b967572d3 100644 --- a/pkg/helper/mock/mock_helper.go +++ b/pkg/helper/mock/mock_helper.go @@ -313,6 +313,21 @@ func (mr *MockHostHelpersInterfaceMockRecorder) GetCurrentKernelArgs() *gomock.C return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCurrentKernelArgs", reflect.TypeOf((*MockHostHelpersInterface)(nil).GetCurrentKernelArgs)) } +// GetDevlinkDeviceParam mocks base method. +func (m *MockHostHelpersInterface) GetDevlinkDeviceParam(pciAddr, paramName string) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetDevlinkDeviceParam", pciAddr, paramName) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetDevlinkDeviceParam indicates an expected call of GetDevlinkDeviceParam. +func (mr *MockHostHelpersInterfaceMockRecorder) GetDevlinkDeviceParam(pciAddr, paramName interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetDevlinkDeviceParam", reflect.TypeOf((*MockHostHelpersInterface)(nil).GetDevlinkDeviceParam), pciAddr, paramName) +} + // GetDriverByBusAndDevice mocks base method. func (m *MockHostHelpersInterface) GetDriverByBusAndDevice(bus, device string) (string, error) { m.ctrl.T.Helper() @@ -929,6 +944,20 @@ func (mr *MockHostHelpersInterfaceMockRecorder) SaveLastPfAppliedStatus(PfInfo i return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SaveLastPfAppliedStatus", reflect.TypeOf((*MockHostHelpersInterface)(nil).SaveLastPfAppliedStatus), PfInfo) } +// SetDevlinkDeviceParam mocks base method. +func (m *MockHostHelpersInterface) SetDevlinkDeviceParam(pciAddr, paramName, value string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SetDevlinkDeviceParam", pciAddr, paramName, value) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetDevlinkDeviceParam indicates an expected call of SetDevlinkDeviceParam. +func (mr *MockHostHelpersInterfaceMockRecorder) SetDevlinkDeviceParam(pciAddr, paramName, value interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetDevlinkDeviceParam", reflect.TypeOf((*MockHostHelpersInterface)(nil).SetDevlinkDeviceParam), pciAddr, paramName, value) +} + // SetNetdevMTU mocks base method. func (m *MockHostHelpersInterface) SetNetdevMTU(pciAddr string, mtu int) error { m.ctrl.T.Helper() diff --git a/pkg/host/internal/lib/netlink/mock/mock_netlink.go b/pkg/host/internal/lib/netlink/mock/mock_netlink.go index e054a0472d..5a7b955eec 100644 --- a/pkg/host/internal/lib/netlink/mock/mock_netlink.go +++ b/pkg/host/internal/lib/netlink/mock/mock_netlink.go @@ -116,6 +116,35 @@ func (mr *MockNetlinkLibMockRecorder) DevLinkSetEswitchMode(dev, newMode interfa return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DevLinkSetEswitchMode", reflect.TypeOf((*MockNetlinkLib)(nil).DevLinkSetEswitchMode), dev, newMode) } +// DevlinkGetDeviceParamByName mocks base method. +func (m *MockNetlinkLib) DevlinkGetDeviceParamByName(bus, device, param string) (*netlink0.DevlinkParam, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DevlinkGetDeviceParamByName", bus, device, param) + ret0, _ := ret[0].(*netlink0.DevlinkParam) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// DevlinkGetDeviceParamByName indicates an expected call of DevlinkGetDeviceParamByName. +func (mr *MockNetlinkLibMockRecorder) DevlinkGetDeviceParamByName(bus, device, param interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DevlinkGetDeviceParamByName", reflect.TypeOf((*MockNetlinkLib)(nil).DevlinkGetDeviceParamByName), bus, device, param) +} + +// DevlinkSetDeviceParam mocks base method. +func (m *MockNetlinkLib) DevlinkSetDeviceParam(bus, device, param string, cmode uint8, value interface{}) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DevlinkSetDeviceParam", bus, device, param, cmode, value) + ret0, _ := ret[0].(error) + return ret0 +} + +// DevlinkSetDeviceParam indicates an expected call of DevlinkSetDeviceParam. +func (mr *MockNetlinkLibMockRecorder) DevlinkSetDeviceParam(bus, device, param, cmode, value interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DevlinkSetDeviceParam", reflect.TypeOf((*MockNetlinkLib)(nil).DevlinkSetDeviceParam), bus, device, param, cmode, value) +} + // LinkByName mocks base method. func (m *MockNetlinkLib) LinkByName(name string) (netlink.Link, error) { m.ctrl.T.Helper() diff --git a/pkg/host/internal/lib/netlink/netlink.go b/pkg/host/internal/lib/netlink/netlink.go index 07933698e2..5cb61a3a55 100644 --- a/pkg/host/internal/lib/netlink/netlink.go +++ b/pkg/host/internal/lib/netlink/netlink.go @@ -47,6 +47,14 @@ type NetlinkLib interface { // VDPANewDev adds new VDPA device // Equivalent to: `vdpa dev add name mgmtdev /mgmtName [params]` VDPANewDev(name, mgmtBus, mgmtName string, params netlink.VDPANewDevParams) error + // DevlinkGetDeviceParamByName returns specific parameter for devlink device + // Equivalent to: `devlink dev param show / name ` + DevlinkGetDeviceParamByName(bus string, device string, param string) (*netlink.DevlinkParam, error) + // DevlinkSetDeviceParam set specific parameter for devlink device + // Equivalent to: `devlink dev param set / name cmode value ` + // cmode argument should contain valid cmode value as uint8, modes are define in nl.DEVLINK_PARAM_CMODE_* constants + // value argument should have one of the following types: uint8, uint16, uint32, string, bool + DevlinkSetDeviceParam(bus string, device string, param string, cmode uint8, value interface{}) error } type libWrapper struct{} @@ -111,3 +119,17 @@ func (w *libWrapper) VDPADelDev(name string) error { func (w *libWrapper) VDPANewDev(name, mgmtBus, mgmtName string, params netlink.VDPANewDevParams) error { return netlink.VDPANewDev(name, mgmtBus, mgmtName, params) } + +// DevlinkGetDeviceParamByName returns specific parameter for devlink device +// Equivalent to: `devlink dev param show / name ` +func (w *libWrapper) DevlinkGetDeviceParamByName(bus string, device string, param string) (*netlink.DevlinkParam, error) { + return netlink.DevlinkGetDeviceParamByName(bus, device, param) +} + +// DevlinkSetDeviceParam set specific parameter for devlink device +// Equivalent to: `devlink dev param set / name cmode value ` +// cmode argument should contain valid cmode value as uint8, modes are define in nl.DEVLINK_PARAM_CMODE_* constants +// value argument should have one of the following types: uint8, uint16, uint32, string, bool +func (w *libWrapper) DevlinkSetDeviceParam(bus string, device string, param string, cmode uint8, value interface{}) error { + return netlink.DevlinkSetDeviceParam(bus, device, param, cmode, value) +} diff --git a/pkg/host/internal/network/network.go b/pkg/host/internal/network/network.go index f9ede5fc89..e5882d66bc 100644 --- a/pkg/host/internal/network/network.go +++ b/pkg/host/internal/network/network.go @@ -9,10 +9,12 @@ import ( "time" "github.com/cenkalti/backoff" + "github.com/vishvananda/netlink/nl" "sigs.k8s.io/controller-runtime/pkg/log" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" dputilsPkg "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/lib/dputils" + netlinkPkg "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/lib/netlink" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars" @@ -21,12 +23,14 @@ import ( type network struct { utilsHelper utils.CmdInterface dputilsLib dputilsPkg.DPUtilsLib + netlinkLib netlinkPkg.NetlinkLib } -func New(utilsHelper utils.CmdInterface, dputilsLib dputilsPkg.DPUtilsLib) types.NetworkInterface { +func New(utilsHelper utils.CmdInterface, dputilsLib dputilsPkg.DPUtilsLib, netlinkLib netlinkPkg.NetlinkLib) types.NetworkInterface { return &network{ utilsHelper: utilsHelper, dputilsLib: dputilsLib, + netlinkLib: netlinkLib, } } @@ -196,3 +200,94 @@ func (n *network) GetNetDevLinkSpeed(ifaceName string) string { return fmt.Sprintf("%s Mb/s", strings.TrimSpace(string(data))) } + +// GetDevlinkDeviceParam returns devlink parameter for the device as a string, if the parameter has multiple values +// then the function will return only first one from the list. +func (n *network) GetDevlinkDeviceParam(pciAddr, paramName string) (string, error) { + funcLog := log.Log.WithValues("device", pciAddr, "param", paramName) + funcLog.V(2).Info("GetDevlinkDeviceParam(): get device parameter") + param, err := n.netlinkLib.DevlinkGetDeviceParamByName(consts.BusPci, pciAddr, paramName) + if err != nil { + funcLog.Error(err, "GetDevlinkDeviceParam(): fail to get devlink device param") + return "", err + } + if len(param.Values) == 0 { + err = fmt.Errorf("param %s has no value", paramName) + funcLog.Error(err, "GetDevlinkDeviceParam(): error") + return "", err + } + var value string + switch param.Type { + case nl.DEVLINK_PARAM_TYPE_U8, nl.DEVLINK_PARAM_TYPE_U16, nl.DEVLINK_PARAM_TYPE_U32: + var valData uint64 + switch v := param.Values[0].Data.(type) { + case uint8: + valData = uint64(v) + case uint16: + valData = uint64(v) + case uint32: + valData = uint64(v) + default: + return "", fmt.Errorf("unexpected uint type type") + } + value = strconv.FormatUint(valData, 10) + + case nl.DEVLINK_PARAM_TYPE_STRING: + value = param.Values[0].Data.(string) + case nl.DEVLINK_PARAM_TYPE_BOOL: + value = strconv.FormatBool(param.Values[0].Data.(bool)) + default: + return "", fmt.Errorf("unknown value type: %d", param.Type) + } + funcLog.V(2).Info("GetDevlinkDeviceParam(): result", "value", value) + return value, nil +} + +// SetDevlinkDeviceParam set devlink parameter for the device, accepts paramName and value +// as a string. Automatically set CMODE for the parameter and converts the value to the right +// type before submitting it. +func (n *network) SetDevlinkDeviceParam(pciAddr, paramName, value string) error { + funcLog := log.Log.WithValues("device", pciAddr, "param", paramName, "value", value) + funcLog.V(2).Info("SetDevlinkDeviceParam(): set device parameter") + param, err := n.netlinkLib.DevlinkGetDeviceParamByName(consts.BusPci, pciAddr, paramName) + if err != nil { + funcLog.Error(err, "SetDevlinkDeviceParam(): can't get existing param data") + return err + } + if len(param.Values) == 0 { + err = fmt.Errorf("param %s has no value", paramName) + funcLog.Error(err, "SetDevlinkDeviceParam(): error") + return err + } + targetCMOD := param.Values[0].CMODE + var typedValue interface{} + var v uint64 + switch param.Type { + case nl.DEVLINK_PARAM_TYPE_U8: + v, err = strconv.ParseUint(value, 10, 8) + typedValue = uint8(v) + case nl.DEVLINK_PARAM_TYPE_U16: + v, err = strconv.ParseUint(value, 10, 16) + typedValue = uint16(v) + case nl.DEVLINK_PARAM_TYPE_U32: + v, err = strconv.ParseUint(value, 10, 32) + typedValue = uint32(v) + case nl.DEVLINK_PARAM_TYPE_STRING: + err = nil + typedValue = value + case nl.DEVLINK_PARAM_TYPE_BOOL: + typedValue, err = strconv.ParseBool(value) + default: + return fmt.Errorf("parameter has unknown value type: %d", param.Type) + } + if err != nil { + err = fmt.Errorf("failed to convert value %s to the required type: %T, devlink paramType is: %d", value, typedValue, param.Type) + funcLog.Error(err, "SetDevlinkDeviceParam(): error") + return err + } + if err := n.netlinkLib.DevlinkSetDeviceParam(consts.BusPci, pciAddr, paramName, targetCMOD, typedValue); err != nil { + funcLog.Error(err, "SetDevlinkDeviceParam(): failed to set parameter") + return err + } + return nil +} diff --git a/pkg/host/internal/network/network_test.go b/pkg/host/internal/network/network_test.go new file mode 100644 index 0000000000..cd604aeae5 --- /dev/null +++ b/pkg/host/internal/network/network_test.go @@ -0,0 +1,155 @@ +package network + +import ( + "fmt" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/vishvananda/netlink" + "github.com/vishvananda/netlink/nl" + + "github.com/golang/mock/gomock" + + hostMockPkg "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/helper/mock" + dputilsMockPkg "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/lib/dputils/mock" + netlinkMockPkg "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/lib/netlink/mock" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types" +) + +func getDevlinkParam(t uint8, value interface{}) *netlink.DevlinkParam { + return &netlink.DevlinkParam{ + Name: "test_param", + Type: t, + Values: []netlink.DevlinkParamValue{ + {Data: value, CMODE: nl.DEVLINK_PARAM_CMODE_DRIVERINIT}}, + } +} + +var _ = Describe("Network", func() { + var ( + n types.NetworkInterface + netlinkLibMock *netlinkMockPkg.MockNetlinkLib + dputilsLibMock *dputilsMockPkg.MockDPUtilsLib + hostMock *hostMockPkg.MockHostHelpersInterface + + testCtrl *gomock.Controller + testErr = fmt.Errorf("test") + ) + BeforeEach(func() { + testCtrl = gomock.NewController(GinkgoT()) + netlinkLibMock = netlinkMockPkg.NewMockNetlinkLib(testCtrl) + dputilsLibMock = dputilsMockPkg.NewMockDPUtilsLib(testCtrl) + hostMock = hostMockPkg.NewMockHostHelpersInterface(testCtrl) + + n = New(hostMock, dputilsLibMock, netlinkLibMock) + }) + + AfterEach(func() { + testCtrl.Finish() + }) + Context("GetDevlinkDeviceParam", func() { + It("get - string", func() { + netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( + getDevlinkParam(nl.DEVLINK_PARAM_TYPE_STRING, "test_value"), nil) + result, err := n.GetDevlinkDeviceParam("0000:d8:00.1", "param_name") + Expect(err).NotTo(HaveOccurred()) + Expect(result).To(Equal("test_value")) + }) + It("get - uint8", func() { + netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( + getDevlinkParam(nl.DEVLINK_PARAM_TYPE_U8, uint8(8)), nil) + result, err := n.GetDevlinkDeviceParam("0000:d8:00.1", "param_name") + Expect(err).NotTo(HaveOccurred()) + Expect(result).To(Equal("8")) + }) + It("get - uint16", func() { + netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( + getDevlinkParam(nl.DEVLINK_PARAM_TYPE_U16, uint16(16)), nil) + result, err := n.GetDevlinkDeviceParam("0000:d8:00.1", "param_name") + Expect(err).NotTo(HaveOccurred()) + Expect(result).To(Equal("16")) + }) + It("get - uint32", func() { + netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( + getDevlinkParam(nl.DEVLINK_PARAM_TYPE_U32, uint32(32)), nil) + result, err := n.GetDevlinkDeviceParam("0000:d8:00.1", "param_name") + Expect(err).NotTo(HaveOccurred()) + Expect(result).To(Equal("32")) + }) + It("get - bool", func() { + netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( + getDevlinkParam(nl.DEVLINK_PARAM_TYPE_BOOL, false), nil) + result, err := n.GetDevlinkDeviceParam("0000:d8:00.1", "param_name") + Expect(err).NotTo(HaveOccurred()) + Expect(result).To(Equal("false")) + }) + It("failed", func() { + netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return(nil, testErr) + _, err := n.GetDevlinkDeviceParam("0000:d8:00.1", "param_name") + Expect(err).To(HaveOccurred()) + }) + }) + Context("SetDevlinkDeviceParam", func() { + It("set - string", func() { + netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( + getDevlinkParam(nl.DEVLINK_PARAM_TYPE_STRING, "test_value"), nil) + netlinkLibMock.EXPECT().DevlinkSetDeviceParam("pci", "0000:d8:00.1", "param_name", + uint8(nl.DEVLINK_PARAM_CMODE_DRIVERINIT), "test_value").Return(nil) + err := n.SetDevlinkDeviceParam("0000:d8:00.1", "param_name", "test_value") + Expect(err).NotTo(HaveOccurred()) + }) + It("set - uint8", func() { + netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( + getDevlinkParam(nl.DEVLINK_PARAM_TYPE_U8, uint8(8)), nil) + netlinkLibMock.EXPECT().DevlinkSetDeviceParam("pci", "0000:d8:00.1", "param_name", + uint8(nl.DEVLINK_PARAM_CMODE_DRIVERINIT), uint8(100)).Return(nil) + err := n.SetDevlinkDeviceParam("0000:d8:00.1", "param_name", "100") + Expect(err).NotTo(HaveOccurred()) + }) + It("set - uint16", func() { + netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( + getDevlinkParam(nl.DEVLINK_PARAM_TYPE_U16, uint16(16)), nil) + netlinkLibMock.EXPECT().DevlinkSetDeviceParam("pci", "0000:d8:00.1", "param_name", + uint8(nl.DEVLINK_PARAM_CMODE_DRIVERINIT), uint16(100)).Return(nil) + err := n.SetDevlinkDeviceParam("0000:d8:00.1", "param_name", "100") + Expect(err).NotTo(HaveOccurred()) + }) + It("set - uint32", func() { + netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( + getDevlinkParam(nl.DEVLINK_PARAM_TYPE_U32, uint32(32)), nil) + netlinkLibMock.EXPECT().DevlinkSetDeviceParam("pci", "0000:d8:00.1", "param_name", + uint8(nl.DEVLINK_PARAM_CMODE_DRIVERINIT), uint32(100)).Return(nil) + err := n.SetDevlinkDeviceParam("0000:d8:00.1", "param_name", "100") + Expect(err).NotTo(HaveOccurred()) + }) + It("set - bool", func() { + netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( + getDevlinkParam(nl.DEVLINK_PARAM_TYPE_BOOL, false), nil) + netlinkLibMock.EXPECT().DevlinkSetDeviceParam("pci", "0000:d8:00.1", "param_name", + uint8(nl.DEVLINK_PARAM_CMODE_DRIVERINIT), true).Return(nil) + err := n.SetDevlinkDeviceParam("0000:d8:00.1", "param_name", "true") + Expect(err).NotTo(HaveOccurred()) + }) + It("failed to get", func() { + netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( + nil, testErr) + err := n.SetDevlinkDeviceParam("0000:d8:00.1", "param_name", "true") + Expect(err).To(HaveOccurred()) + }) + It("failed to set", func() { + netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( + getDevlinkParam(nl.DEVLINK_PARAM_TYPE_BOOL, false), nil) + netlinkLibMock.EXPECT().DevlinkSetDeviceParam("pci", "0000:d8:00.1", "param_name", + uint8(nl.DEVLINK_PARAM_CMODE_DRIVERINIT), true).Return(testErr) + err := n.SetDevlinkDeviceParam("0000:d8:00.1", "param_name", "true") + Expect(err).To(HaveOccurred()) + }) + It("failed to convert type on set", func() { + netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( + getDevlinkParam(nl.DEVLINK_PARAM_TYPE_U8, 10), nil) + // uint8 overflow + err := n.SetDevlinkDeviceParam("0000:d8:00.1", "param_name", "10000") + Expect(err).To(HaveOccurred()) + }) + }) +}) diff --git a/pkg/host/internal/network/suite_test.go b/pkg/host/internal/network/suite_test.go new file mode 100644 index 0000000000..b990eb503a --- /dev/null +++ b/pkg/host/internal/network/suite_test.go @@ -0,0 +1,21 @@ +package network + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "go.uber.org/zap/zapcore" + "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/log/zap" +) + +func TestNetwork(t *testing.T) { + log.SetLogger(zap.New( + zap.WriteTo(GinkgoWriter), + zap.Level(zapcore.Level(-2)), + zap.UseDevMode(true))) + RegisterFailHandler(Fail) + RunSpecs(t, "Package Network Suite") +} diff --git a/pkg/host/manager.go b/pkg/host/manager.go index faeb123310..7e5a920011 100644 --- a/pkg/host/manager.go +++ b/pkg/host/manager.go @@ -39,7 +39,7 @@ func NewHostManager(utilsInterface utils.CmdInterface) HostManagerInterface { dpUtils := dputils.New() netlinkLib := netlink.New() k := kernel.New(utilsInterface) - n := network.New(utilsInterface, dpUtils) + n := network.New(utilsInterface, dpUtils, netlinkLib) sv := service.New(utilsInterface) u := udev.New(utilsInterface) sr := sriov.New(utilsInterface, k, n, u, netlinkLib, dpUtils) diff --git a/pkg/host/mock/mock_host.go b/pkg/host/mock/mock_host.go index ea69d9f423..992a813b99 100644 --- a/pkg/host/mock/mock_host.go +++ b/pkg/host/mock/mock_host.go @@ -268,6 +268,21 @@ func (mr *MockHostManagerInterfaceMockRecorder) GetCurrentKernelArgs() *gomock.C return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCurrentKernelArgs", reflect.TypeOf((*MockHostManagerInterface)(nil).GetCurrentKernelArgs)) } +// GetDevlinkDeviceParam mocks base method. +func (m *MockHostManagerInterface) GetDevlinkDeviceParam(pciAddr, paramName string) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetDevlinkDeviceParam", pciAddr, paramName) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetDevlinkDeviceParam indicates an expected call of GetDevlinkDeviceParam. +func (mr *MockHostManagerInterfaceMockRecorder) GetDevlinkDeviceParam(pciAddr, paramName interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetDevlinkDeviceParam", reflect.TypeOf((*MockHostManagerInterface)(nil).GetDevlinkDeviceParam), pciAddr, paramName) +} + // GetDriverByBusAndDevice mocks base method. func (m *MockHostManagerInterface) GetDriverByBusAndDevice(bus, device string) (string, error) { m.ctrl.T.Helper() @@ -772,6 +787,20 @@ func (mr *MockHostManagerInterfaceMockRecorder) ResetSriovDevice(ifaceStatus int return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ResetSriovDevice", reflect.TypeOf((*MockHostManagerInterface)(nil).ResetSriovDevice), ifaceStatus) } +// SetDevlinkDeviceParam mocks base method. +func (m *MockHostManagerInterface) SetDevlinkDeviceParam(pciAddr, paramName, value string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SetDevlinkDeviceParam", pciAddr, paramName, value) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetDevlinkDeviceParam indicates an expected call of SetDevlinkDeviceParam. +func (mr *MockHostManagerInterfaceMockRecorder) SetDevlinkDeviceParam(pciAddr, paramName, value interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetDevlinkDeviceParam", reflect.TypeOf((*MockHostManagerInterface)(nil).SetDevlinkDeviceParam), pciAddr, paramName, value) +} + // SetNetdevMTU mocks base method. func (m *MockHostManagerInterface) SetNetdevMTU(pciAddr string, mtu int) error { m.ctrl.T.Helper() diff --git a/pkg/host/types/interfaces.go b/pkg/host/types/interfaces.go index be0c824ad3..06a9aedacd 100644 --- a/pkg/host/types/interfaces.go +++ b/pkg/host/types/interfaces.go @@ -97,6 +97,13 @@ type NetworkInterface interface { GetNetDevMac(name string) string // GetNetDevLinkSpeed returns the network interface link speed GetNetDevLinkSpeed(name string) string + // GetDevlinkDeviceParam returns devlink parameter for the device as a string, if the parameter has multiple values + // then the function will return only first one from the list. + GetDevlinkDeviceParam(pciAddr, paramName string) (string, error) + // SetDevlinkDeviceParam set devlink parameter for the device, accepts paramName and value + // as a string. Automatically set CMODE for the parameter and converts the value to the right + // type before submitting it. + SetDevlinkDeviceParam(pciAddr, paramName, value string) error } type ServiceInterface interface {