Skip to content

Commit

Permalink
Add vlan remote mechanism support (see networkservicemesh/sdk-vpp#399)
Browse files Browse the repository at this point in the history
Fixed search for link by PCI address in pci devices subdirectories

Signed-off-by: Laszlo Kiraly <laszlo.kiraly@est.tech>
  • Loading branch information
ljkiraly committed Oct 21, 2021
1 parent 08c76a5 commit 61538b1
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions pkg/kernel/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ func FindHostDevice(pciAddress, name string, namespaces ...netns.NsHandle) (Link
}
}
}

return nil, errors.Errorf("failed to obtain netlink link matching criteria: name=%s or pciAddress=%s", name, pciAddress)
}

Expand All @@ -211,8 +210,9 @@ func searchByPCIAddress(ns netns.NsHandle, name, pciAddress string) (netlink.Lin
return nil, errors.Errorf("failed to enter namespace: %s", err)
}

netDir := filepath.Join("/sys/bus/pci/devices", pciAddress, "net")
if _, err = os.Lstat(netDir); err != nil {
pciDevicePath := filepath.Join("/sys/bus/pci/devices", pciAddress)
netDir, err := findNetDir(pciDevicePath)
if err != nil {
return nil, errors.Errorf("no net directory under pci device %s: %q", pciAddress, err)
}

Expand All @@ -238,6 +238,33 @@ func searchByPCIAddress(ns netns.NsHandle, name, pciAddress string) (netlink.Lin
return link, nil
}

func findNetDir(basePath string) (string, error) {
subDir := filepath.Join(basePath, "net")
if _, err := os.Lstat(subDir); err == nil {
return subDir, nil
}
files, err := ioutil.ReadDir(basePath)
if err != nil {
return "", errors.Wrapf(err, "failed to read directory %s", basePath)
}
for _, file := range files {
if file.IsDir() {
subDir = filepath.Join(basePath, file.Name())
subdirFiles, err := ioutil.ReadDir(subDir)
if err != nil {
return "", errors.Wrapf(err, "failed to read subdirectory %s", subDir)
}
for _, subdirFile := range subdirFiles {
if subdirFile.IsDir() && subdirFile.Name() == "net" {
subDir = filepath.Join(subDir, subdirFile.Name())
return subDir, nil
}
}
}
}
return "", errors.Errorf("failed to find net directory")
}

func searchByName(ns netns.NsHandle, name, pciAddress string) (netlink.Link, error) {
// execute in context of the pod's namespace
err := netns.Set(ns)
Expand Down

0 comments on commit 61538b1

Please sign in to comment.