Skip to content

Commit 66385c6

Browse files
authored
Merge pull request #623 from glasnostic/fix-pci
fix device/binder can't handle correctly with network interface with `virtio-pci` driver on KVM
2 parents 376f7e7 + b49d4a0 commit 66385c6

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

devices/consts.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ var (
7575

7676
var (
7777
pathSysClassNetDeviceDriver stringBuilder = PathSysClassNet + "/%s/device/driver"
78-
pathSysClassNetDevice stringBuilder = PathSysClassNet + "/%s/device"
78+
pathSysClassNetDevice stringBuilder = PathSysClassNet + "/%s"
7979
)
8080

8181
var (

devices/misc.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,18 @@ func FindDefaultDpdkDriver(nicName string) string {
3434

3535
// GetDeviceID returns the device ID of given NIC name.
3636
func GetDeviceID(nicName string) (string, error) {
37-
// DEV_ID=$(basename $(readlink /sys/class/net/<nicName>/device))
38-
return readlinkBaseCmd(pathSysClassNetDevice.With(nicName))
37+
// DEV_ID=$(basename $(readlink /sys/class/net/<nicName>))
38+
raw, err := readlinkCmd(pathSysClassNetDevice.With(nicName))
39+
if err != nil {
40+
return "", err
41+
}
42+
// raw should be like /sys/devices/pci0002:00/0000:00:08.0/virtio2/net/ens8
43+
raws := strings.Split(raw, "/")
44+
if len(raws) < 5 {
45+
return "", fmt.Errorf("path not correct")
46+
}
47+
return raws[4], nil
48+
3949
}
4050

4151
// IsModuleLoaded checks if the kernel has already loaded the driver or not.
@@ -69,12 +79,20 @@ func writeToTargetWithData(sysfs string, flag int, mode os.FileMode, data string
6979
return nil
7080
}
7181

72-
func readlinkBaseCmd(path string) (string, error) {
73-
output, err := cmdOutputWithTimeout(defaultTimeoutLimitation, "readlink", path)
82+
func readlinkCmd(path string) (string, error) {
83+
output, err := cmdOutputWithTimeout(defaultTimeoutLimitation, "readlink", "-f", path)
7484
if err != nil {
7585
return "", fmt.Errorf("Cmd Execute readlink failed: %s", err.Error())
7686
}
7787
outputStr := strings.Trim(string(output), "\n")
88+
return outputStr, nil
89+
}
90+
91+
func readlinkBaseCmd(path string) (string, error) {
92+
outputStr, err := readlinkCmd(path)
93+
if err != nil {
94+
return "", fmt.Errorf("Cmd Execute readlink failed: %s", err.Error())
95+
}
7896
result := filepath.Base(outputStr)
7997
return result, nil
8098
}

0 commit comments

Comments
 (0)