Skip to content

Commit

Permalink
Merge pull request #67 from adrianchiris/add-aux-dev-by-index-and-pci
Browse files Browse the repository at this point in the history
Add GetAuxSFDevByPciAndSfIndex
  • Loading branch information
moshe010 authored Jul 2, 2023
2 parents 0ebdf60 + 6c9c1bd commit 684547d
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 6 deletions.
25 changes: 25 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright 2023 NVIDIA CORPORATION & AFFILIATES
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package sriovnet

import (
"errors"
)

var (
ErrDeviceNotFound = errors.New("device not found")
)
34 changes: 34 additions & 0 deletions sriovnet_aux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
utilfs "github.com/k8snetworkplumbingwg/sriovnet/pkg/utils/filesystem"
)

const (
u32Mask = 0xffffffff
)

// GetNetDeviceFromAux gets auxiliary device name (e.g 'mlx5_core.sf.2') and
// returns the correlate netdevice
func GetNetDevicesFromAux(auxDev string) ([]string, error) {
Expand Down Expand Up @@ -100,9 +104,39 @@ func GetAuxNetDevicesFromPci(pciAddr string) ([]string, error) {

auxDevs := make([]string, 0)
for _, file := range files {
if !file.IsDir() {
// auxiliary devices appear as directory here.
continue
}
if auxiliaryDeviceRe.MatchString(file.Name()) {
auxDevs = append(auxDevs, file.Name())
}
}
return auxDevs, nil
}

// GetAuxSFDevByPciAndSFIndex returns auxiliary SF device name which is associated with the given parent PCI address
// and SF index. returns error if an error occurred. returns ErrDeviceNotFound error if device is not found.
func GetAuxSFDevByPciAndSFIndex(pciAddress string, sfIndex uint32) (string, error) {
devs, err := GetAuxNetDevicesFromPci(pciAddress)
if err != nil {
return "", err
}

for _, dev := range devs {
// skip non sf devices
if !strings.Contains(dev, ".sf.") {
continue
}

idx, err := GetSfIndexByAuxDev(dev)
if err != nil || idx < 0 {
continue
}

if uint32(idx&u32Mask) == sfIndex {
return dev, nil
}
}
return "", ErrDeviceNotFound
}
101 changes: 95 additions & 6 deletions sriovnet_aux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,33 @@ import (
utilfs "github.com/k8snetworkplumbingwg/sriovnet/pkg/utils/filesystem"
)

type auxDevContext struct {
parent string
sfNum string
name string
}

// setupAuxDevEnv creates (fake) auxiliary devices, returns error if setup failed.
func setUpAuxDevEnv(t *testing.T, auxDevs []auxDevContext) {
var err error

err = utilfs.Fs.MkdirAll(AuxSysDir, os.FileMode(0755))
assert.NoError(t, err)

for _, dev := range auxDevs {
auxDevPathPCI := filepath.Join(PciSysDir, dev.parent, dev.name)
auxDevPathAux := filepath.Join(AuxSysDir, dev.name)

err = utilfs.Fs.MkdirAll(auxDevPathPCI, os.FileMode(0755))
assert.NoError(t, err)
if dev.sfNum != "" {
err = utilfs.Fs.WriteFile(filepath.Join(auxDevPathPCI, "sfnum"), []byte(dev.sfNum), os.FileMode(0655))
assert.NoError(t, err)
}
utilfs.Fs.Symlink(auxDevPathPCI, auxDevPathAux)
}
}

func TestGetNetDevicesFromAuxSuccess(t *testing.T) {
teardown := setupFakeFs(t)
defer teardown()
Expand Down Expand Up @@ -183,10 +210,10 @@ func TestGetUplinkRepresentorFromAuxNoSuchDevice(t *testing.T) {
assert.Equal(t, "", pf)
}

func createPciDevicePaths(pciAddr string, dirs []string) {
func createPciDevicePaths(t *testing.T, pciAddr string, dirs []string) {
for _, dir := range dirs {
path := filepath.Join(PciSysDir, pciAddr, dir)
_ = utilfs.Fs.MkdirAll(path, os.FileMode(0755))
assert.NoError(t, utilfs.Fs.MkdirAll(path, os.FileMode(0755)))
}
}

Expand All @@ -196,9 +223,9 @@ func TestGetAuxNetDevicesFromPciSuccess(t *testing.T) {
pciAddr := "0000:00:01.0"
devs := []string{"foo.bar.0", "foo.bar.1", "foo.baz.0"}

createPciDevicePaths(pciAddr, devs)
createPciDevicePaths(t, pciAddr, devs)
// create few regular directories
createPciDevicePaths(pciAddr, []string{"infiniband", "net"})
createPciDevicePaths(t, pciAddr, []string{"infiniband", "net"})

auxDevs, err := GetAuxNetDevicesFromPci(pciAddr)
assert.NoError(t, err)
Expand All @@ -210,7 +237,7 @@ func TestGetAuxNetDevicesFromPciSuccessNoDevices(t *testing.T) {
defer teardown()
pciAddr := "0000:00:01.0"

createPciDevicePaths(pciAddr, []string{"infiniband", "net"})
createPciDevicePaths(t, pciAddr, []string{"infiniband", "net"})

auxDevs, err := GetAuxNetDevicesFromPci(pciAddr)
assert.NoError(t, err)
Expand All @@ -229,9 +256,71 @@ func TestGetAuxNetDevicesFromPciFailureNotANetworkDevice(t *testing.T) {
defer teardown()
pciAddr := "0000:00:01.0"

createPciDevicePaths(pciAddr, []string{"infiniband"})
createPciDevicePaths(t, pciAddr, []string{"infiniband"})

auxDevs, err := GetAuxNetDevicesFromPci(pciAddr)
assert.Error(t, err)
assert.Equal(t, auxDevs, []string(nil))
}

func TestGetAuxSFDevByPciAndSFIndex(t *testing.T) {
teardown := setupFakeFs(t)
defer teardown()

pciAddr := "0000:03:00.0"
devs := []auxDevContext{
{
parent: pciAddr,
sfNum: "",
name: "mlx5_core.eth.0",
},
{
parent: pciAddr,
sfNum: "",
name: "mlx5_core.eth-rep.0",
},
{
parent: pciAddr,
sfNum: "123",
name: "mlx5_core.sf.3",
},
}
setUpAuxDevEnv(t, devs)
createPciDevicePaths(t, pciAddr, []string{"infiniband", "net"})

device, err := GetAuxSFDevByPciAndSFIndex(pciAddr, 123)
assert.NoError(t, err)
assert.Equal(t, "mlx5_core.sf.3", device)
}

func TestGetAuxSFDevByPciAndSFIndexSFIndexNotFound(t *testing.T) {
teardown := setupFakeFs(t)
defer teardown()

pciAddr := "0000:03:00.0"
devs := []auxDevContext{
{
parent: pciAddr,
sfNum: "123",
name: "mlx5_core.sf.3",
},
}
setUpAuxDevEnv(t, devs)
createPciDevicePaths(t, pciAddr, []string{"infiniband", "net"})

device, err := GetAuxSFDevByPciAndSFIndex(pciAddr, 122)
assert.Error(t, err)
assert.Equal(t, ErrDeviceNotFound, err)
assert.Equal(t, "", device)
}

func TestGetAuxSFDevByPciAndSFIndexPCIAddressNotFound(t *testing.T) {
teardown := setupFakeFs(t)
defer teardown()

createPciDevicePaths(t, "0000:03:00.0", []string{"infiniband", "net"})

_, err := GetAuxSFDevByPciAndSFIndex("0000:04:00.0", 4)
assert.Error(t, err)
assert.NotEqual(t, ErrDeviceNotFound, err)
}

0 comments on commit 684547d

Please sign in to comment.