Skip to content

Commit

Permalink
Move constants to pkg and add IsLinkAdminStateUp in netlinklib
Browse files Browse the repository at this point in the history
Signed-off-by: Vasilis Remmas <[email protected]>
  • Loading branch information
vasrem committed May 23, 2024
1 parent b8b6940 commit 5f3c4e9
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion api/v1/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/consts/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const (
LinkTypeIB = "IB"
LinkTypeETH = "ETH"

LinkAdminStateUp = "up"
LinkAdminStateDown = "down"

UninitializedNodeGUID = "0000:0000:0000:0000"

DeviceTypeVfioPci = "vfio-pci"
Expand Down
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.

7 changes: 7 additions & 0 deletions pkg/host/internal/lib/netlink/netlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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
}
7 changes: 3 additions & 4 deletions pkg/host/internal/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"io/fs"
"net"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -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
}
3 changes: 1 addition & 2 deletions pkg/host/internal/sriov/sriov.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package sriov
import (
"errors"
"fmt"
"net"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions pkg/host/internal/sriov/sriov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5f3c4e9

Please sign in to comment.