diff --git a/pkg/netdevice/pciNetDevice.go b/pkg/netdevice/pciNetDevice.go index a49bd6dbd..91d881384 100644 --- a/pkg/netdevice/pciNetDevice.go +++ b/pkg/netdevice/pciNetDevice.go @@ -82,7 +82,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 diff --git a/pkg/utils/mocks/NetlinkProvider.go b/pkg/utils/mocks/NetlinkProvider.go index 9bf64738f..7330f22bf 100644 --- a/pkg/utils/mocks/NetlinkProvider.go +++ b/pkg/utils/mocks/NetlinkProvider.go @@ -12,6 +12,29 @@ type NetlinkProvider struct { mock.Mock } +// GetDevLinkDevice provides a mock function with given fields: pfAddr +func (_m *NetlinkProvider) GetDevLinkDevice(pfAddr string) (*netlink.DevlinkDevice, error) { + ret := _m.Called(pfAddr) + + var r0 *netlink.DevlinkDevice + if rf, ok := ret.Get(0).(func(string) *netlink.DevlinkDevice); ok { + r0 = rf(pfAddr) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*netlink.DevlinkDevice) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(pfAddr) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // GetDevLinkDeviceEswitchAttrs provides a mock function with given fields: ifName func (_m *NetlinkProvider) GetDevLinkDeviceEswitchAttrs(ifName string) (*netlink.DevlinkDevEswitchAttr, error) { ret := _m.Called(ifName) diff --git a/pkg/utils/netlink_provider.go b/pkg/utils/netlink_provider.go index a0c526f06..ea9ec7f54 100644 --- a/pkg/utils/netlink_provider.go +++ b/pkg/utils/netlink_provider.go @@ -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 { @@ -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) diff --git a/pkg/utils/testing.go b/pkg/utils/testing.go index cde466319..83edf23f4 100644 --- a/pkg/utils/testing.go +++ b/pkg/utils/testing.go @@ -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(nil, fmt.Errorf("error getting devlink device for net device")) SetNetlinkProviderInst(&mockProvider) }