Skip to content

Commit

Permalink
Merge pull request #526 from adrianchiris/add-fallback-for-auxdev
Browse files Browse the repository at this point in the history
Add fallback to get PF Name for AuxNetDevice
  • Loading branch information
adrianchiris authored Jan 25, 2024
2 parents cb2802b + 75f5fa1 commit 32b8ca7
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/devices/gen_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ func NewGenNetDevice(deviceID string, dt types.DeviceType, isRdma bool) (*GenNet
}
netNames, _ = utils.GetNetNames(deviceID)
case types.AuxNetDeviceType:
if pfName, err = utils.GetSriovnetProvider().GetUplinkRepresentorFromAux(deviceID); err != nil {
if pfName, err = utils.GetPfNameFromAuxDev(deviceID); err != nil {
// AuxNetDeviceType by design should have PF, return error if failed to get PF name
return nil, err
}

if pfAddr, err = utils.GetSriovnetProvider().GetPfPciFromAux(deviceID); err != nil {
return nil, err
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,30 @@ func GetPfName(pciAddr string) (string, error) {
return "", fmt.Errorf("the PF name is not found for device %s", pciAddr)
}

// GetPfNameFromAuxDev returns netdevice name of the PF associated with the
// provided auxiliary device name.
func GetPfNameFromAuxDev(auxDevName string) (string, error) {
pfName, err := GetSriovnetProvider().GetUplinkRepresentorFromAux(auxDevName)
if err != nil {
// fallback, try to get PF netdev name from PF PCI Address
pfAddr, err := GetSriovnetProvider().GetPfPciFromAux(auxDevName)
if err != nil {
return "", err
}
netdevs, err := GetNetNames(pfAddr)
if err != nil {
return "", err
}

if len(netdevs) == 0 {
return "", fmt.Errorf("no netdevs for PF address %s", pfAddr)
}

return netdevs[0], nil
}
return pfName, err
}

// IsSriovPF check if a pci device SRIOV capable given its pci address
func IsSriovPF(pciAddr string) bool {
totalVfFilePath := filepath.Join(sysBusPci, pciAddr, totalVfFile)
Expand Down
55 changes: 55 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"errors"
"fmt"
"net"

Expand Down Expand Up @@ -596,4 +597,58 @@ var _ = Describe("In the utils package", func() {
Entry("wrong deviceID string layout (negative id number)", "driver_name.type.-4", ""),
Entry("valid deviceID string", "driver_name.type.123", "type"),
)

Context("GetPfNameFromAuxDev", func() {
var (
mockSriovnet *mocks.SriovnetProvider
)
BeforeEach(func() {
mockSriovnet = mocks.NewSriovnetProvider(GinkgoT())
SetSriovnetProviderInst(mockSriovnet)
})

It("Returns uplink representor netdev name if found", func() {
mockSriovnet.On("GetUplinkRepresentorFromAux", mock.AnythingOfType("string")).Return("enp3s0f0np0", nil)

pfName, err := GetPfNameFromAuxDev("mlx5_core.sf.4")
Expect(err).ToNot(HaveOccurred())
Expect(pfName).To(Equal("enp3s0f0np0"))
})

It("Returns PF netdev name from sysfs if uplink representor not found", func() {
mockSriovnet.On("GetUplinkRepresentorFromAux", mock.AnythingOfType("string")).Return("", errors.New("uplink not found"))
mockSriovnet.On("GetPfPciFromAux", mock.AnythingOfType("string")).Return("0000:03:00.0", nil)

fakeFs := &FakeFilesystem{
Dirs: []string{"/sys/bus/pci/devices/0000:03:00.0/net"},
Files: map[string][]byte{"/sys/bus/pci/devices/0000:03:00.0/net/enp3s0f0np0": nil},
}
defer fakeFs.Use()()

pfName, err := GetPfNameFromAuxDev("mlx5_core.sf.4")
Expect(err).ToNot(HaveOccurred())
Expect(pfName).To(Equal("enp3s0f0np0"))
})

It("Fails if no uplink representor or PF netdev found", func() {
mockSriovnet.On("GetUplinkRepresentorFromAux", mock.AnythingOfType("string")).Return("", errors.New("uplink not found"))
mockSriovnet.On("GetPfPciFromAux", mock.AnythingOfType("string")).Return("0000:03:00.0", nil)

fakeFs := &FakeFilesystem{
Dirs: []string{"/sys/bus/pci/devices/0000:03:00.0"},
}
defer fakeFs.Use()()

_, err := GetPfNameFromAuxDev("mlx5_core.sf.4")
Expect(err).To(HaveOccurred())
})

It("Fails if PF PCI not found", func() {
mockSriovnet.On("GetUplinkRepresentorFromAux", mock.AnythingOfType("string")).Return("", errors.New("uplink not found"))
mockSriovnet.On("GetPfPciFromAux", mock.AnythingOfType("string")).Return("", errors.New("PF PCI address not found"))

_, err := GetPfNameFromAuxDev("mlx5_core.sf.4")
Expect(err).To(HaveOccurred())
})
})
})

0 comments on commit 32b8ca7

Please sign in to comment.