diff --git a/pkg/host/internal/lib/netlink/mock/mock_netlink.go b/pkg/host/internal/lib/netlink/mock/mock_netlink.go index 072ba8980..c31e78207 100644 --- a/pkg/host/internal/lib/netlink/mock/mock_netlink.go +++ b/pkg/host/internal/lib/netlink/mock/mock_netlink.go @@ -131,6 +131,20 @@ func (mr *MockNetlinkLibMockRecorder) LinkByName(name interface{}) *gomock.Call return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LinkByName", reflect.TypeOf((*MockNetlinkLib)(nil).LinkByName), name) } +// LinkSetMTU mocks base method. +func (m *MockNetlinkLib) LinkSetMTU(link netlink.Link, mtu int) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "LinkSetMTU", link, mtu) + ret0, _ := ret[0].(error) + return ret0 +} + +// LinkSetMTU indicates an expected call of LinkSetMTU. +func (mr *MockNetlinkLibMockRecorder) LinkSetMTU(link, mtu interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LinkSetMTU", reflect.TypeOf((*MockNetlinkLib)(nil).LinkSetMTU), link, mtu) +} + // LinkSetUp mocks base method. func (m *MockNetlinkLib) LinkSetUp(link 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 4871abe95..f5a86f61c 100644 --- a/pkg/host/internal/lib/netlink/netlink.go +++ b/pkg/host/internal/lib/netlink/netlink.go @@ -30,6 +30,9 @@ type NetlinkLib interface { // LinkSetUp enables the link device. // Equivalent to: `ip link set $link up` LinkSetUp(link Link) error + // LinkSetMTU sets the mtu of the link device. + // Equivalent to: `ip link set $link mtu $mtu` + LinkSetMTU(link Link, mtu int) error // DevlinkGetDeviceByName provides a pointer to devlink device and nil error, // otherwise returns an error code. DevLinkGetDeviceByName(bus string, device string) (*netlink.DevlinkDevice, error) @@ -71,6 +74,12 @@ func (w *libWrapper) LinkSetUp(link Link) error { return netlink.LinkSetUp(link) } +// LinkSetMTU sets the mtu of the link device. +// Equivalent to: `ip link set $link mtu $mtu` +func (w *libWrapper) LinkSetMTU(link Link, mtu int) error { + return netlink.LinkSetMTU(link, mtu) +} + // DevlinkGetDeviceByName provides a pointer to devlink device and nil error, // otherwise returns an error code. func (w *libWrapper) DevLinkGetDeviceByName(bus string, device string) (*netlink.DevlinkDevice, error) { diff --git a/pkg/host/internal/network/network.go b/pkg/host/internal/network/network.go index f9ede5fc8..75f938228 100644 --- a/pkg/host/internal/network/network.go +++ b/pkg/host/internal/network/network.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "path/filepath" - "strconv" "strings" "time" @@ -13,6 +12,7 @@ import ( "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 +21,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, } } @@ -123,28 +125,20 @@ func (n *network) IsSwitchdev(name string) bool { return true } -func mtuFilePath(ifaceName string, pciAddr string) string { - mtuFile := "net/" + ifaceName + "/mtu" - return filepath.Join(vars.FilesystemRoot, consts.SysBusPciDevices, pciAddr, mtuFile) -} - func (n *network) GetNetdevMTU(pciAddr string) int { log.Log.V(2).Info("GetNetdevMTU(): get MTU", "device", pciAddr) ifaceName := n.TryGetInterfaceName(pciAddr) if ifaceName == "" { return 0 } - data, err := os.ReadFile(mtuFilePath(ifaceName, pciAddr)) - if err != nil { - log.Log.Error(err, "GetNetdevMTU(): fail to read mtu file", "path", mtuFilePath) - return 0 - } - mtu, err := strconv.Atoi(strings.TrimSpace(string(data))) + + link, err := n.netlinkLib.LinkByName(ifaceName) if err != nil { - log.Log.Error(err, "GetNetdevMTU(): fail to convert mtu to int", "raw-mtu", strings.TrimSpace(string(data))) + log.Log.Error(err, "GetNetdevMTU(): fail to get Link ", "device", ifaceName) return 0 } - return mtu + + return link.Attrs().MTU } func (n *network) SetNetdevMTU(pciAddr string, mtu int) error { @@ -155,34 +149,37 @@ func (n *network) SetNetdevMTU(pciAddr string, mtu int) error { } b := backoff.NewConstantBackOff(1 * time.Second) err := backoff.Retry(func() error { - ifaceName, err := n.dputilsLib.GetNetNames(pciAddr) + ifaceName := n.TryGetInterfaceName(pciAddr) + if ifaceName == "" { + log.Log.Error(nil, "SetNetdevMTU(): fail to get interface name", "device", pciAddr) + return fmt.Errorf("failed to get netdevice for device %s", pciAddr) + } + + link, err := n.netlinkLib.LinkByName(ifaceName) if err != nil { - log.Log.Error(err, "SetNetdevMTU(): fail to get interface name", "device", pciAddr) + log.Log.Error(err, "SetNetdevMTU(): fail to get Link ", "device", ifaceName) return err } - if len(ifaceName) < 1 { - return fmt.Errorf("SetNetdevMTU(): interface name is empty") - } - mtuFilePath := mtuFilePath(ifaceName[0], pciAddr) - return os.WriteFile(mtuFilePath, []byte(strconv.Itoa(mtu)), os.ModeAppend) + return n.netlinkLib.LinkSetMTU(link, mtu) }, backoff.WithMaxRetries(b, 10)) + if err != nil { - log.Log.Error(err, "SetNetdevMTU(): fail to write mtu file after retrying") + log.Log.Error(err, "SetNetdevMTU(): fail to set mtu after retrying") return err } return nil } +// GetNetDevMac returns network device MAC address or empty string if address cannot be +// retrieved. func (n *network) GetNetDevMac(ifaceName string) string { log.Log.V(2).Info("GetNetDevMac(): get Mac", "device", ifaceName) - macFilePath := filepath.Join(vars.FilesystemRoot, consts.SysClassNet, ifaceName, "address") - data, err := os.ReadFile(macFilePath) + link, err := n.netlinkLib.LinkByName(ifaceName) if err != nil { - log.Log.Error(err, "GetNetDevMac(): fail to read Mac file", "path", macFilePath) + log.Log.Error(err, "GetNetDevMac(): failed to get Link", "device", ifaceName) return "" } - - return strings.TrimSpace(string(data)) + return link.Attrs().HardwareAddr.String() } func (n *network) GetNetDevLinkSpeed(ifaceName string) string { diff --git a/pkg/host/manager.go b/pkg/host/manager.go index 83399ab90..5b397dbb9 100644 --- a/pkg/host/manager.go +++ b/pkg/host/manager.go @@ -38,11 +38,12 @@ type hostManager struct { func NewHostManager(utilsInterface utils.CmdInterface) HostManagerInterface { dpUtils := dputils.New() + nl := netlink.New() k := kernel.New(utilsInterface) - n := network.New(utilsInterface, dpUtils) + n := network.New(utilsInterface, dpUtils, nl) sv := service.New(utilsInterface) u := udev.New(utilsInterface) - sr := sriov.New(utilsInterface, k, n, u, netlink.New(), dpUtils) + sr := sriov.New(utilsInterface, k, n, u, nl, dpUtils) v := vdpa.New(k, govdpa.New()) return &hostManager{