aregm / nff-go

NFF-Go -Network Function Framework for GO (former YANFF)
BSD 3-Clause "New" or "Revised" License
1.38k stars 154 forks source link

Wrong deviceID on ubuntu 18.04 cause bind failure #639

Closed zartbot closed 5 years ago

zartbot commented 5 years ago

GetDeviceID function is wrong on some platform

// GetDeviceID returns the device ID of given NIC name.
func GetDeviceID(nicName string) (string, error) {
    // DEV_ID=$(basename $(readlink /sys/class/net/<nicName>))
    raw, err := readlinkCmd(pathSysClassNetDevice.With(nicName))
    if err != nil {
        return "", err
    }
    // raw should be like /sys/devices/pci0002:00/0000:00:08.0/virtio2/net/ens8
    raws := strings.Split(raw, "/")
    if len(raws) < 5 {
        return "", fmt.Errorf("path not correct")
    }
    return raws[4], nil
}

My platform Raw string is :

 /sys/devices/pci0000:00/0000:00:01.0/0000:03:00.2/net/ens4f2

My Hardware and software version

Ubuntu 18.04.2LTS
Xeon E5 v4
ASUS X99M WS
nVidia GTX-1070
build-in Intel I210 Gigabit NIC
build-in I218--LM
build-in BCM4360 Wireless NIC
2x Intel I350 NIC

dpdk-bind.py output...

Network devices using DPDK-compatible driver
============================================
0000:03:00.1 'I350 Gigabit Network Connection 1521' drv=igb_uio unused=igb
0000:03:00.2 'I350 Gigabit Network Connection 1521' drv=igb_uio unused=igb

Network devices using kernel driver
===================================
0000:00:19.0 'Ethernet Connection (2) I218-LM 15a0' if=eno1 drv=e1000e unused=igb_uio *Active*
0000:02:00.3 'I350 Gigabit Network Connection 1521' if=ens3f3 drv=igb unused=igb_uio 
0000:03:00.0 'I350 Gigabit Network Connection 1521' if=ens4f0 drv=igb unused=igb_uio 
0000:03:00.3 'I350 Gigabit Network Connection 1521' if=ens4f3 drv=igb unused=igb_uio 
0000:05:00.0 'I210 Gigabit Network Connection 1533' if=enp5s0 drv=igb unused=igb_uio *Active*
0000:07:00.0 'BCM4360 802.11ac Wireless Network Adapter 43a0' if=wlp7s0 drv=wl unused=igb_uio *Active*

Other Network devices
=====================
0000:02:00.0 'I350 Gigabit Network Connection 1521' unused=igb,igb_uio
0000:02:00.1 'I350 Gigabit Network Connection 1521' unused=igb,igb_uio
0000:02:00.2 'I350 Gigabit Network Connection 1521' unused=igb,igb_uio

No 'Baseband' devices detected
==============================

No 'Crypto' devices detected
============================

No 'Eventdev' devices detected
==============================

No 'Mempool' devices detected
=============================

No 'Compress' devices detected
==============================

No 'Misc (rawdev)' devices detected
===================================
zartbot commented 5 years ago
diff --git a/devices/misc.go b/devices/misc.go
index ee6b14e..e52afb6 100644
--- a/devices/misc.go
+++ b/devices/misc.go
@@ -40,12 +40,22 @@ func GetDeviceID(nicName string) (string, error) {
                return "", err
        }
        // raw should be like /sys/devices/pci0002:00/0000:00:08.0/virtio2/net/ens8
+       // or /sys/devices/pci0000:00/0000:00:01.0/0000:03:00.2/net/ens4f2
        raws := strings.Split(raw, "/")
        if len(raws) < 5 {
                return "", fmt.Errorf("path not correct")
        }
-       return raws[4], nil

+       // search and validate deviceID
+       for idx := len(raws) - 1; idx >= 0; idx-- {
+               v := strings.Split(raws[idx], ":")
+               if len(v) == 3 {
+                       if len(v[0]) == 4 && len(v[1]) == 2 && len(v[2]) == 4 {
+                               return raws[idx], nil
+                       }
+               }
+       }
+       return "", fmt.Errorf("path not correct")
 }

 // IsModuleLoaded checks if the kernel has already loaded the driver or not.
gshimansky commented 5 years ago

I applied your patch untested. Reopen the issue if something is wrong with it.