Skip to content

Commit

Permalink
Use dputils wrapper in network and sriov packages
Browse files Browse the repository at this point in the history
Signed-off-by: Yury Kulazhenkov <[email protected]>
  • Loading branch information
ykulazhenkov committed Jan 30, 2024
1 parent d739284 commit a7a885a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 38 deletions.
15 changes: 9 additions & 6 deletions pkg/host/internal/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,23 @@ import (
"github.com/cenkalti/backoff"
"sigs.k8s.io/controller-runtime/pkg/log"

dputils "github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/utils"

"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
dputilsPkg "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/lib/dputils"
"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"
)

type network struct {
utilsHelper utils.CmdInterface
dputilsLib dputilsPkg.DPUtilsLib
}

func New(utilsHelper utils.CmdInterface) types.NetworkInterface {
return &network{utilsHelper: utilsHelper}
func New(utilsHelper utils.CmdInterface, dputilsLib dputilsPkg.DPUtilsLib) types.NetworkInterface {
return &network{
utilsHelper: utilsHelper,
dputilsLib: dputilsLib,
}
}

// TryToGetVirtualInterfaceName get the interface name of a virtio interface
Expand Down Expand Up @@ -61,7 +64,7 @@ func (n *network) TryToGetVirtualInterfaceName(pciAddr string) string {
}

func (n *network) TryGetInterfaceName(pciAddr string) string {
names, err := dputils.GetNetNames(pciAddr)
names, err := n.dputilsLib.GetNetNames(pciAddr)
if err != nil || len(names) < 1 {
return ""
}
Expand Down Expand Up @@ -152,7 +155,7 @@ func (n *network) SetNetdevMTU(pciAddr string, mtu int) error {
}
b := backoff.NewConstantBackOff(1 * time.Second)
err := backoff.Retry(func() error {
ifaceName, err := dputils.GetNetNames(pciAddr)
ifaceName, err := n.dputilsLib.GetNetNames(pciAddr)
if err != nil {
log.Log.Error(err, "SetNetdevMTU(): fail to get interface name", "device", pciAddr)
return err
Expand Down
36 changes: 19 additions & 17 deletions pkg/host/internal/sriov/sriov.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/log"

dputils "github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/utils"

sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"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/store"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types"
Expand All @@ -33,18 +32,21 @@ type sriov struct {
networkHelper types.NetworkInterface
udevHelper types.UdevInterface
netlinkLib netlinkPkg.NetlinkLib
dputilsLib dputilsPkg.DPUtilsLib
}

func New(utilsHelper utils.CmdInterface,
kernelHelper types.KernelInterface,
networkHelper types.NetworkInterface,
udevHelper types.UdevInterface,
netlinkLib netlinkPkg.NetlinkLib) types.SriovInterface {
netlinkLib netlinkPkg.NetlinkLib,
dputilsLib dputilsPkg.DPUtilsLib) types.SriovInterface {
return &sriov{utilsHelper: utilsHelper,
kernelHelper: kernelHelper,
networkHelper: networkHelper,
udevHelper: udevHelper,
netlinkLib: netlinkLib,
dputilsLib: dputilsLib,
}
}

Expand Down Expand Up @@ -94,11 +96,11 @@ func (s *sriov) ResetSriovDevice(ifaceStatus sriovnetworkv1.InterfaceExt) error
}

func (s *sriov) GetVfInfo(pciAddr string, devices []*ghw.PCIDevice) sriovnetworkv1.VirtualFunction {
driver, err := dputils.GetDriverName(pciAddr)
driver, err := s.dputilsLib.GetDriverName(pciAddr)
if err != nil {
log.Log.Error(err, "getVfInfo(): unable to parse device driver", "device", pciAddr)
}
id, err := dputils.GetVFID(pciAddr)
id, err := s.dputilsLib.GetVFID(pciAddr)
if err != nil {
log.Log.Error(err, "getVfInfo(): unable to get VF index", "device", pciAddr)
}
Expand Down Expand Up @@ -129,7 +131,7 @@ func (s *sriov) GetVfInfo(pciAddr string, devices []*ghw.PCIDevice) sriovnetwork

func (s *sriov) SetVfGUID(vfAddr string, pfLink netlink.Link) error {
log.Log.Info("SetVfGUID()", "vf", vfAddr)
vfID, err := dputils.GetVFID(vfAddr)
vfID, err := s.dputilsLib.GetVFID(vfAddr)
if err != nil {
log.Log.Error(err, "SetVfGUID(): unable to get VF id", "address", vfAddr)
return err
Expand Down Expand Up @@ -169,7 +171,7 @@ func (s *sriov) VFIsReady(pciAddr string) (netlink.Link, error) {
func (s *sriov) SetVfAdminMac(vfAddr string, pfLink, vfLink netlink.Link) error {
log.Log.Info("SetVfAdminMac()", "vf", vfAddr)

vfID, err := dputils.GetVFID(vfAddr)
vfID, err := s.dputilsLib.GetVFID(vfAddr)
if err != nil {
log.Log.Error(err, "SetVfAdminMac(): unable to get VF id", "address", vfAddr)
return err
Expand Down Expand Up @@ -210,17 +212,17 @@ func (s *sriov) DiscoverSriovDevices(storeManager store.ManagerInterface) ([]sri

// TODO: exclude devices used by host system

if dputils.IsSriovVF(device.Address) {
if s.dputilsLib.IsSriovVF(device.Address) {
continue
}

driver, err := dputils.GetDriverName(device.Address)
driver, err := s.dputilsLib.GetDriverName(device.Address)
if err != nil {
log.Log.Error(err, "DiscoverSriovDevices(): unable to parse device driver for device, skipping", "device", device)
continue
}

deviceNames, err := dputils.GetNetNames(device.Address)
deviceNames, err := s.dputilsLib.GetNetNames(device.Address)
if err != nil {
log.Log.Error(err, "DiscoverSriovDevices(): unable to get device names for device, skipping", "device", device)
continue
Expand Down Expand Up @@ -263,15 +265,15 @@ func (s *sriov) DiscoverSriovDevices(storeManager store.ManagerInterface) ([]sri
}
}

if dputils.IsSriovPF(device.Address) {
iface.TotalVfs = dputils.GetSriovVFcapacity(device.Address)
iface.NumVfs = dputils.GetVFconfigured(device.Address)
if s.dputilsLib.IsSriovPF(device.Address) {
iface.TotalVfs = s.dputilsLib.GetSriovVFcapacity(device.Address)
iface.NumVfs = s.dputilsLib.GetVFconfigured(device.Address)
if iface.EswitchMode, err = s.GetNicSriovMode(device.Address); err != nil {
log.Log.Error(err, "DiscoverSriovDevices(): warning, unable to get device eswitch mode",
"device", device.Address)
}
if dputils.SriovConfigured(device.Address) {
vfs, err := dputils.GetVFList(device.Address)
if s.dputilsLib.SriovConfigured(device.Address) {
vfs, err := s.dputilsLib.GetVFList(device.Address)
if err != nil {
log.Log.Error(err, "DiscoverSriovDevices(): unable to parse VFs for device, skipping",
"device", device)
Expand Down Expand Up @@ -340,7 +342,7 @@ func (s *sriov) ConfigSriovDevice(iface *sriovnetworkv1.Interface, ifaceStatus *
}
// Config VFs
if iface.NumVfs > 0 {
vfAddrs, err := dputils.GetVFList(iface.PciAddress)
vfAddrs, err := s.dputilsLib.GetVFList(iface.PciAddress)
if err != nil {
log.Log.Error(err, "configSriovDevice(): unable to parse VFs for device", "device", iface.PciAddress)
}
Expand All @@ -353,7 +355,7 @@ func (s *sriov) ConfigSriovDevice(iface *sriovnetworkv1.Interface, ifaceStatus *
for _, addr := range vfAddrs {
var group *sriovnetworkv1.VfGroup

vfID, err := dputils.GetVFID(addr)
vfID, err := s.dputilsLib.GetVFID(addr)
if err != nil {
log.Log.Error(err, "configSriovDevice(): unable to get VF id", "device", iface.PciAddress)
return err
Expand Down
29 changes: 16 additions & 13 deletions pkg/host/internal/sriov/sriov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

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"
hostMockPkg "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/mock"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types"
Expand All @@ -20,19 +21,21 @@ import (

var _ = Describe("SRIOV", func() {
var (
s types.SriovInterface
libMock *netlinkMockPkg.MockNetlinkLib
hostMock *hostMockPkg.MockHostManagerInterface
s types.SriovInterface
netlinkLibMock *netlinkMockPkg.MockNetlinkLib
dputilsLibMock *dputilsMockPkg.MockDPUtilsLib
hostMock *hostMockPkg.MockHostManagerInterface

testCtrl *gomock.Controller

testError = fmt.Errorf("test")
)
BeforeEach(func() {
testCtrl = gomock.NewController(GinkgoT())
libMock = netlinkMockPkg.NewMockNetlinkLib(testCtrl)
netlinkLibMock = netlinkMockPkg.NewMockNetlinkLib(testCtrl)
dputilsLibMock = dputilsMockPkg.NewMockDPUtilsLib(testCtrl)
hostMock = hostMockPkg.NewMockHostManagerInterface(testCtrl)
s = New(nil, hostMock, hostMock, hostMock, libMock)
s = New(nil, hostMock, hostMock, hostMock, netlinkLibMock, dputilsLibMock)
})

AfterEach(func() {
Expand All @@ -55,20 +58,20 @@ var _ = Describe("SRIOV", func() {

Context("GetNicSriovMode", func() {
It("devlink returns info", func() {
libMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(
netlinkLibMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(
&netlink.DevlinkDevice{Attrs: netlink.DevlinkDevAttrs{Eswitch: netlink.DevlinkDevEswitchAttr{Mode: "switchdev"}}},
nil)
mode, err := s.GetNicSriovMode("0000:d8:00.0")
Expect(err).NotTo(HaveOccurred())
Expect(mode).To(Equal("switchdev"))
})
It("devlink returns error", func() {
libMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(nil, testError)
netlinkLibMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(nil, testError)
_, err := s.GetNicSriovMode("0000:d8:00.0")
Expect(err).To(MatchError(testError))
})
It("devlink not supported - fail to get name", func() {
libMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(nil, syscall.ENODEV)
netlinkLibMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(nil, syscall.ENODEV)
mode, err := s.GetNicSriovMode("0000:d8:00.0")
Expect(err).NotTo(HaveOccurred())
Expect(mode).To(BeEmpty())
Expand All @@ -78,18 +81,18 @@ var _ = Describe("SRIOV", func() {
Context("SetNicSriovMode", func() {
It("set", func() {
testDev := &netlink.DevlinkDevice{}
libMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(&netlink.DevlinkDevice{}, nil)
libMock.EXPECT().DevLinkSetEswitchMode(testDev, "legacy").Return(nil)
netlinkLibMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(&netlink.DevlinkDevice{}, nil)
netlinkLibMock.EXPECT().DevLinkSetEswitchMode(testDev, "legacy").Return(nil)
Expect(s.SetNicSriovMode("0000:d8:00.0", "legacy")).NotTo(HaveOccurred())
})
It("fail to get dev", func() {
libMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(nil, testError)
netlinkLibMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(nil, testError)
Expect(s.SetNicSriovMode("0000:d8:00.0", "legacy")).To(MatchError(testError))
})
It("fail to set mode", func() {
testDev := &netlink.DevlinkDevice{}
libMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(&netlink.DevlinkDevice{}, nil)
libMock.EXPECT().DevLinkSetEswitchMode(testDev, "legacy").Return(testError)
netlinkLibMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(&netlink.DevlinkDevice{}, nil)
netlinkLibMock.EXPECT().DevLinkSetEswitchMode(testDev, "legacy").Return(testError)
Expect(s.SetNicSriovMode("0000:d8:00.0", "legacy")).To(MatchError(testError))
})
})
Expand Down
6 changes: 4 additions & 2 deletions pkg/host/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package host

import (
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/kernel"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/lib/dputils"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/lib/govdpa"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/lib/netlink"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/network"
Expand Down Expand Up @@ -36,11 +37,12 @@ type hostManager struct {
}

func NewHostManager(utilsInterface utils.CmdInterface) HostManagerInterface {
dpUtils := dputils.New()
k := kernel.New(utilsInterface)
n := network.New(utilsInterface)
n := network.New(utilsInterface, dpUtils)
sv := service.New(utilsInterface)
u := udev.New(utilsInterface)
sr := sriov.New(utilsInterface, k, n, u, netlink.New())
sr := sriov.New(utilsInterface, k, n, u, netlink.New(), dpUtils)
v := vdpa.New(k, govdpa.New())

return &hostManager{
Expand Down

0 comments on commit a7a885a

Please sign in to comment.