Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wrapper for utils package from sriov-device-plugin #598

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions pkg/host/internal/lib/dputils/dputils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package dputils

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

func New() DPUtilsLib {
return &libWrapper{}
}

//go:generate ../../../../../bin/mockgen -destination mock/mock_dputils.go -source dputils.go
type DPUtilsLib interface {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These might be good candidates for sriovnet (not related to this PR)

// GetNetNames returns host net interface names as string for a PCI device from its pci address
GetNetNames(pciAddr string) ([]string, error)
// GetDriverName returns current driver attached to a pci device from its pci address
GetDriverName(pciAddr string) (string, error)
// GetVFID returns VF ID index (within specific PF) based on PCI address
GetVFID(pciAddr string) (vfID int, err error)
// IsSriovVF check if a pci device has link to a PF
IsSriovVF(pciAddr string) bool
// IsSriovPF check if a pci device SRIOV capable given its pci address
IsSriovPF(pciAddr string) bool
// GetSriovVFcapacity returns SRIOV VF capacity
GetSriovVFcapacity(pf string) int
// GetVFconfigured returns number of VF configured for a PF
GetVFconfigured(pf string) int
// SriovConfigured returns true if sriov_numvfs reads > 0 else false
SriovConfigured(addr string) bool
// GetVFList returns a List containing PCI addr for all VF discovered in a given PF
GetVFList(pf string) (vfList []string, err error)
}

type libWrapper struct{}

// GetNetNames returns host net interface names as string for a PCI device from its pci address
func (w *libWrapper) GetNetNames(pciAddr string) ([]string, error) {
return dputils.GetNetNames(pciAddr)
}

// GetDriverName returns current driver attached to a pci device from its pci address
func (w *libWrapper) GetDriverName(pciAddr string) (string, error) {
return dputils.GetDriverName(pciAddr)
}

// GetVFID returns VF ID index (within specific PF) based on PCI address
func (w *libWrapper) GetVFID(pciAddr string) (vfID int, err error) {
return dputils.GetVFID(pciAddr)
}

// IsSriovVF check if a pci device has link to a PF
func (w *libWrapper) IsSriovVF(pciAddr string) bool {
return dputils.IsSriovVF(pciAddr)
}

// IsSriovPF check if a pci device SRIOV capable given its pci address
func (w *libWrapper) IsSriovPF(pciAddr string) bool {
return dputils.IsSriovPF(pciAddr)
}

// GetSriovVFcapacity returns SRIOV VF capacity
func (w *libWrapper) GetSriovVFcapacity(pf string) int {
return dputils.GetSriovVFcapacity(pf)
}

// GetVFconfigured returns number of VF configured for a PF
func (w *libWrapper) GetVFconfigured(pf string) int {
return dputils.GetVFconfigured(pf)
}

// SriovConfigured returns true if sriov_numvfs reads > 0 else false
func (w *libWrapper) SriovConfigured(addr string) bool {
return dputils.SriovConfigured(addr)
}

// GetVFList returns a List containing PCI addr for all VF discovered in a given PF
func (w *libWrapper) GetVFList(pf string) (vfList []string, err error) {
return dputils.GetVFList(pf)
}
164 changes: 164 additions & 0 deletions pkg/host/internal/lib/dputils/mock/mock_dputils.go

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

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
Loading
Loading