Skip to content

Commit

Permalink
Use Devlink when possible for NewPciNetDevice
Browse files Browse the repository at this point in the history
Try to use devlink api to configure the linkType to be `ether`.
If the card doesn't support this we fall back to netlink.

This commit is a partial fix for k8snetworkplumbingwg#392
There are still cards the doesn't support the netlink api like intel xv710

Signed-off-by: Sebastian Sch <[email protected]>
  • Loading branch information
SchSeba committed Nov 23, 2021
1 parent 7488066 commit 05af4d4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/netdevice/pciNetDevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func NewPciNetDevice(dev *ghw.PCIDevice, rFactory types.ResourceFactory, rc *typ
}
}
}

pciDev, err := resources.NewPciDevice(dev, rFactory, infoProviders)
if err != nil {
return nil, err
Expand All @@ -82,7 +81,13 @@ func NewPciNetDevice(dev *ghw.PCIDevice, rFactory types.ResourceFactory, rc *typ
}

linkType := ""
if len(ifName) > 0 {
if _, err = utils.GetNetlinkProvider().GetDevLinkDevice(pciAddr); err == nil {
linkType = "ether"
}

if err != nil && len(ifName) > 0 {
glog.Warningf("Devlink query for device %s named %s is not supported trying netlink", pciAddr, ifName)

la, err := utils.GetNetlinkProvider().GetLinkAttrs(ifName)
if err != nil {
return nil, err
Expand Down
23 changes: 23 additions & 0 deletions pkg/utils/mocks/NetlinkProvider.go

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

11 changes: 11 additions & 0 deletions pkg/utils/netlink_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type NetlinkProvider interface {
GetLinkAttrs(ifName string) (*nl.LinkAttrs, error)
// GetDevLinkDeviceEswitchAttrs returns a devlink device's attributes
GetDevLinkDeviceEswitchAttrs(ifName string) (*nl.DevlinkDevEswitchAttr, error)
// GetDevLinkDevice returns a devlink device
GetDevLinkDevice(pfAddr string) (*nl.DevlinkDevice, error)
}

type defaultNetlinkProvider struct {
Expand All @@ -47,6 +49,15 @@ func (defaultNetlinkProvider) GetLinkAttrs(ifName string) (*nl.LinkAttrs, error)
return link.Attrs(), nil
}

// GetDevLinkDevice returns a devlink device
func (defaultNetlinkProvider) GetDevLinkDevice(pfAddr string) (*nl.DevlinkDevice, error) {
dev, err := nl.DevLinkGetDeviceByName("pci", pfAddr)
if err != nil {
return nil, fmt.Errorf("error getting devlink device for net device %s %v", pfAddr, err)
}
return dev, nil
}

// GetDevLinkDeviceEswitchAttrs returns a devlink device's attributes
func (defaultNetlinkProvider) GetDevLinkDeviceEswitchAttrs(pfAddr string) (*nl.DevlinkDevEswitchAttr, error) {
dev, err := nl.DevLinkGetDeviceByName("pci", pfAddr)
Expand Down
3 changes: 3 additions & 0 deletions pkg/utils/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func SetDefaultMockNetlinkProvider() {
mockProvider.
On("GetDevLinkDeviceEswitchAttrs", mock.AnythingOfType("string")).
Return(&nl.DevlinkDevEswitchAttr{Mode: "fakeMode"}, nil)
mockProvider.
On("GetDevLinkDevice", mock.AnythingOfType("string")).
Return(&nl.DevlinkDevice{}, nil)

SetNetlinkProviderInst(&mockProvider)
}

0 comments on commit 05af4d4

Please sign in to comment.