Listing Network interface, MAC address and PCI bus/device/function

Mon 28 March 2016

Filed under Linux Networking

Tags Linux Networking

New hardware for BufferUpr has arrived! I'm working on adding Wifi capability into the product and with new hardware comes new challenges.

Challenge: Find a simple way to list all the network interfaces in the system, including the PCIe slot and MAC address for each.

Solution:

find /sys/devices/pci0000* \
-wholename "*net/*/address" \
-printf "%h/%f " \
-exec cat '{}' \;

This depends on a proper find, of course, so Busybox won't do.

Example output:

/sys/devices/pci0000:00/0000:00:03.0/0000:03:00.0/net/p3p0/address 90:e2:ba:7f:97:00
/sys/devices/pci0000:00/0000:00:03.0/0000:03:00.1/net/p3p1/address 90:e2:ba:7f:97:01
/sys/devices/pci0000:00/0000:00:03.0/0000:03:00.2/net/p3p2/address 90:e2:ba:7f:97:02
/sys/devices/pci0000:00/0000:00:03.0/0000:03:00.3/net/p3p3/address 90:e2:ba:7f:97:03
/sys/devices/pci0000:00/0000:00:19.0/net/em1/address 88:88:88:88:87:88

Notice from the path to the address files that some of the interfaces on this system are named according to their PCI BDF (bus/device/function) identifiers. For example, the p3p2 interface is bus 3, device 0, function 2.

From these BDF numbers, we can guess that there is a 4-port card in PCIe slot 3. However, it may not be easy to tell which physical connector on the motherboard maps to slot 3. It might even involve screwdrivers and dust... shudder.

There are some more examples and a better explanation of device naming rules over there.

If the disappearance of eth0 is news to you and you're frustrated that nobody asked you whether you wanted interface naming fixed, you might want to read about why this change was made.

The Xen wiki has a good page on BDF numbers and Wikipedia has more detail.



rationali.st © Andrew Cooks