Skip to content

Commit

Permalink
network pkg to use netlink instead of sysfs
Browse files Browse the repository at this point in the history
- Add Set link MTU in netlink pkg
- Add netlink lib dependency for network pkg
- Set/Get MTU changed to use netink
- Get MAC address changed to use netlink
- Regenerate mocks

Signed-off-by: adrianc <[email protected]>
  • Loading branch information
adrianchiris committed Feb 21, 2024
1 parent f2fb31e commit 56530b9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 30 deletions.
14 changes: 14 additions & 0 deletions pkg/host/internal/lib/netlink/mock/mock_netlink.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions pkg/host/internal/lib/netlink/netlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
53 changes: 25 additions & 28 deletions pkg/host/internal/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"

Expand All @@ -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"
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions pkg/host/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 56530b9

Please sign in to comment.