diff --git a/api/v1/helper.go b/api/v1/helper.go index ab012ccdd..8e830c307 100644 --- a/api/v1/helper.go +++ b/api/v1/helper.go @@ -273,7 +273,7 @@ func NeedToUpdateSriov(ifaceSpec *Interface, ifaceStatus *InterfaceExt) bool { return true } - if ifaceStatus.LinkAdminState == "down" { + if ifaceStatus.LinkAdminState == consts.LinkAdminStateDown { log.V(2).Info("NeedToUpdateSriov(): PF link status needs update", "desired to include", "up", "current", ifaceStatus.LinkAdminState) return true } diff --git a/pkg/consts/constants.go b/pkg/consts/constants.go index 3123107a8..7ae5ab41a 100644 --- a/pkg/consts/constants.go +++ b/pkg/consts/constants.go @@ -43,6 +43,9 @@ const ( LinkTypeIB = "IB" LinkTypeETH = "ETH" + LinkAdminStateUp = "up" + LinkAdminStateDown = "down" + UninitializedNodeGUID = "0000:0000:0000:0000" DeviceTypeVfioPci = "vfio-pci" diff --git a/pkg/host/internal/lib/netlink/mock/mock_netlink.go b/pkg/host/internal/lib/netlink/mock/mock_netlink.go index 882be4942..e3e7d649f 100644 --- a/pkg/host/internal/lib/netlink/mock/mock_netlink.go +++ b/pkg/host/internal/lib/netlink/mock/mock_netlink.go @@ -145,6 +145,20 @@ func (mr *MockNetlinkLibMockRecorder) DevlinkSetDeviceParam(bus, device, param, return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DevlinkSetDeviceParam", reflect.TypeOf((*MockNetlinkLib)(nil).DevlinkSetDeviceParam), bus, device, param, cmode, value) } +// IsLinkAdminStateUp mocks base method. +func (m *MockNetlinkLib) IsLinkAdminStateUp(link netlink.Link) bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "IsLinkAdminStateUp", link) + ret0, _ := ret[0].(bool) + return ret0 +} + +// IsLinkAdminStateUp indicates an expected call of IsLinkAdminStateUp. +func (mr *MockNetlinkLibMockRecorder) IsLinkAdminStateUp(link interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsLinkAdminStateUp", reflect.TypeOf((*MockNetlinkLib)(nil).IsLinkAdminStateUp), link) +} + // 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 19980400d..95b620bba 100644 --- a/pkg/host/internal/lib/netlink/netlink.go +++ b/pkg/host/internal/lib/netlink/netlink.go @@ -61,6 +61,8 @@ type NetlinkLib interface { // RdmaLinkByName finds a link by name and returns a pointer to the object if // found and nil error, otherwise returns error code. RdmaLinkByName(name string) (*netlink.RdmaLink, error) + // IsLinkAdminStateUp checks if the admin state of a link is up + IsLinkAdminStateUp(link Link) bool } type libWrapper struct{} @@ -151,3 +153,8 @@ func (w *libWrapper) DevlinkSetDeviceParam(bus string, device string, param stri func (w *libWrapper) RdmaLinkByName(name string) (*netlink.RdmaLink, error) { return netlink.RdmaLinkByName(name) } + +// IsLinkAdminStateUp checks if the admin state of a link is up +func (w *libWrapper) IsLinkAdminStateUp(link Link) bool { + return link.Attrs().Flags&net.FlagUp == 1 +} diff --git a/pkg/host/internal/network/network.go b/pkg/host/internal/network/network.go index 370d03e6d..5c026f217 100644 --- a/pkg/host/internal/network/network.go +++ b/pkg/host/internal/network/network.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "io/fs" - "net" "os" "path/filepath" "strconv" @@ -375,9 +374,9 @@ func (n *network) GetNetDevLinkAdminState(ifaceName string) string { return "" } - if link.Attrs().Flags&net.FlagUp == 0 { - return "down" + if n.netlinkLib.IsLinkAdminStateUp(link) { + return consts.LinkAdminStateUp } - return "up" + return consts.LinkAdminStateDown } diff --git a/pkg/host/internal/sriov/sriov.go b/pkg/host/internal/sriov/sriov.go index 0f0e60624..ecc5ca48e 100644 --- a/pkg/host/internal/sriov/sriov.go +++ b/pkg/host/internal/sriov/sriov.go @@ -3,7 +3,6 @@ package sriov import ( "errors" "fmt" - "net" "os" "path/filepath" "strconv" @@ -574,7 +573,7 @@ func (s *sriov) configSriovDevice(iface *sriovnetworkv1.Interface, skipVFConfigu if err != nil { return err } - if pfLink.Attrs().Flags&net.FlagUp == 0 { + if !s.netlinkLib.IsLinkAdminStateUp(pfLink) { err = s.netlinkLib.LinkSetUp(pfLink) if err != nil { return err diff --git a/pkg/host/internal/sriov/sriov_test.go b/pkg/host/internal/sriov/sriov_test.go index 15faebe9c..92b79604e 100644 --- a/pkg/host/internal/sriov/sriov_test.go +++ b/pkg/host/internal/sriov/sriov_test.go @@ -120,7 +120,8 @@ var _ = Describe("SRIOV", func() { dputilsLibMock.EXPECT().GetVFList("0000:d8:00.0").Return([]string{"0000:d8:00.2", "0000:d8:00.3"}, nil) pfLinkMock := netlinkMockPkg.NewMockLink(testCtrl) netlinkLibMock.EXPECT().LinkByName("enp216s0f0np0").Return(pfLinkMock, nil).Times(3) - pfLinkMock.EXPECT().Attrs().Return(&netlink.LinkAttrs{OperState: netlink.OperDown, EncapType: "ether"}).Times(2) + pfLinkMock.EXPECT().Attrs().Return(&netlink.LinkAttrs{Flags: 0, EncapType: "ether"}) + netlinkLibMock.EXPECT().IsLinkAdminStateUp(pfLinkMock).Return(false) netlinkLibMock.EXPECT().LinkSetUp(pfLinkMock).Return(nil) dputilsLibMock.EXPECT().GetVFID("0000:d8:00.2").Return(0, nil).Times(2) @@ -187,7 +188,7 @@ var _ = Describe("SRIOV", func() { dputilsLibMock.EXPECT().GetVFList("0000:d8:00.0").Return([]string{"0000:d8:00.2"}, nil) pfLinkMock := netlinkMockPkg.NewMockLink(testCtrl) netlinkLibMock.EXPECT().LinkByName("enp216s0f0np0").Return(pfLinkMock, nil).Times(2) - pfLinkMock.EXPECT().Attrs().Return(&netlink.LinkAttrs{OperState: netlink.OperDown}) + netlinkLibMock.EXPECT().IsLinkAdminStateUp(pfLinkMock).Return(false) netlinkLibMock.EXPECT().LinkSetUp(pfLinkMock).Return(nil) dputilsLibMock.EXPECT().GetVFID("0000:d8:00.2").Return(0, nil).Times(2) @@ -240,7 +241,7 @@ var _ = Describe("SRIOV", func() { dputilsLibMock.EXPECT().GetVFList("0000:d8:00.0").Return([]string{"0000:d8:00.2"}, nil).Times(2) pfLinkMock := netlinkMockPkg.NewMockLink(testCtrl) netlinkLibMock.EXPECT().LinkByName("enp216s0f0np0").Return(pfLinkMock, nil).Times(2) - pfLinkMock.EXPECT().Attrs().Return(&netlink.LinkAttrs{OperState: netlink.OperDown}) + netlinkLibMock.EXPECT().IsLinkAdminStateUp(pfLinkMock).Return(false) netlinkLibMock.EXPECT().LinkSetUp(pfLinkMock).Return(nil) netlinkLibMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(&netlink.DevlinkDevice{ Attrs: netlink.DevlinkDevAttrs{Eswitch: netlink.DevlinkDevEswitchAttr{Mode: "legacy"}}}, nil).Times(2)