Skip to content

Commit

Permalink
Extend GetUplinkRepresentor to work with PF argument
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Maslennikov <[email protected]>
  • Loading branch information
almaslennikov committed Nov 8, 2021
1 parent dcb83c4 commit e25fd1f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
26 changes: 26 additions & 0 deletions sriovnet_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,29 @@ func TestIntegrationGetVfRepresentor(t *testing.T) {
t.Log("GetVfRepresentor", "uplink: ", tcase.uplink, " VF Index: ", tcase.vfIndex, " Rep: ", rep)
}
}

func TestIntegrationGetUplinkRepresentor(t *testing.T) {
uplink := "enp3s0f0np0"
pfPciAddress := "0000:03:00.0"
vfPciAddress := "0000:03:00.2"

rep, err := GetUplinkRepresentor(pfPciAddress)

if err != nil {
t.Fatal("GetUplinkRepresentor failed with error: ", err)
}

if rep != uplink {
t.Fatal("Actual Representor does not match expected Representor", rep, "!=", uplink)
}

rep, err := GetUplinkRepresentor(vfPciAddress)

if err != nil {
t.Fatal("GetUplinkRepresentor failed with error: ", err)
}

if rep != uplink {
t.Fatal("Actual Representor does not match expected Representor", rep, "!=", uplink)
}
}
19 changes: 13 additions & 6 deletions sriovnet_switchdev.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package sriovnet

import (
"errors"
"fmt"
"net"
"os"
"path/filepath"
"regexp"
"strconv"
Expand Down Expand Up @@ -79,13 +81,18 @@ func isSwitchdev(netdevice string) bool {
return false
}

// GetUplinkRepresentor gets a VF PCI address (e.g '0000:03:00.4') and
// returns the uplink represntor netdev name for that VF.
func GetUplinkRepresentor(vfPciAddress string) (string, error) {
devicePath := filepath.Join(PciSysDir, vfPciAddress, "physfn", "net")
// GetUplinkRepresentor gets a VF or PF PCI address (e.g '0000:03:00.4') and
// returns the uplink represntor netdev name for that VF or PF.
func GetUplinkRepresentor(pciAddress string) (string, error) {
devicePath := filepath.Join(PciSysDir, pciAddress, "physfn", "net")
if _, err := utilfs.Fs.Stat(devicePath); errors.Is(err, os.ErrNotExist) {
// If physfn symlink to the parent PF doesn't exist, use the current device's dir
devicePath = filepath.Join(PciSysDir, pciAddress, "net")
}

devices, err := utilfs.Fs.ReadDir(devicePath)
if err != nil {
return "", fmt.Errorf("failed to lookup %s: %v", vfPciAddress, err)
return "", fmt.Errorf("failed to lookup %s: %v", pciAddress, err)
}
for _, device := range devices {
if isSwitchdev(device.Name()) {
Expand All @@ -100,7 +107,7 @@ func GetUplinkRepresentor(vfPciAddress string) (string, error) {
return device.Name(), nil
}
}
return "", fmt.Errorf("uplink for %s not found", vfPciAddress)
return "", fmt.Errorf("uplink for %s not found", pciAddress)
}

func GetVfRepresentor(uplink string, vfIndex int) (string, error) {
Expand Down
15 changes: 15 additions & 0 deletions sriovnet_switchdev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@ func TestGetUplinkRepresentorWithoutPhysPortNameSuccess(t *testing.T) {
assert.Equal(t, "eth0", uplinkNetdev)
}

func TestGetUplinkRepresentorForPfSuccess(t *testing.T) {
pfPciAddress := "0000:03:00.0"
uplinkRep := &repContext{"eth0", "p0", "111111"}

vfPciAddress := ""
var vfsReps []*repContext

teardown := setupUplinkRepresentorEnv(t, uplinkRep, vfPciAddress, vfsReps)
_ = utilfs.Fs.MkdirAll(filepath.Join(PciSysDir, pfPciAddress, "net", uplinkRep.Name), os.FileMode(0755))
defer teardown()
uplinkNetdev, err := GetUplinkRepresentor(pfPciAddress)
assert.NoError(t, err)
assert.Equal(t, "eth0", uplinkNetdev)
}

func TestGetUplinkRepresentorWithPhysPortNameFailed(t *testing.T) {
vfPciAddress := "0000:03:00.4"
uplinkRep := &repContext{"eth0", "invalid", "111111"}
Expand Down

0 comments on commit e25fd1f

Please sign in to comment.