2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright(c) 2010-2014 Intel Corporation
10 from os.path import exists, abspath, dirname, basename
12 # The PCI base class for all devices
13 network_class = {'Class': '02', 'Vendor': None, 'Device': None,
14 'SVendor': None, 'SDevice': None}
15 ifpga_class = {'Class': '12', 'Vendor': '8086', 'Device': '0b30',
16 'SVendor': None, 'SDevice': None}
17 encryption_class = {'Class': '10', 'Vendor': None, 'Device': None,
18 'SVendor': None, 'SDevice': None}
19 intel_processor_class = {'Class': '0b', 'Vendor': '8086', 'Device': None,
20 'SVendor': None, 'SDevice': None}
21 cavium_sso = {'Class': '08', 'Vendor': '177d', 'Device': 'a04b,a04d',
22 'SVendor': None, 'SDevice': None}
23 cavium_fpa = {'Class': '08', 'Vendor': '177d', 'Device': 'a053',
24 'SVendor': None, 'SDevice': None}
25 cavium_pkx = {'Class': '08', 'Vendor': '177d', 'Device': 'a0dd,a049',
26 'SVendor': None, 'SDevice': None}
27 cavium_tim = {'Class': '08', 'Vendor': '177d', 'Device': 'a051',
28 'SVendor': None, 'SDevice': None}
29 cavium_zip = {'Class': '12', 'Vendor': '177d', 'Device': 'a037',
30 'SVendor': None, 'SDevice': None}
31 avp_vnic = {'Class': '05', 'Vendor': '1af4', 'Device': '1110',
32 'SVendor': None, 'SDevice': None}
34 octeontx2_sso = {'Class': '08', 'Vendor': '177d', 'Device': 'a0f9,a0fa',
35 'SVendor': None, 'SDevice': None}
36 octeontx2_npa = {'Class': '08', 'Vendor': '177d', 'Device': 'a0fb,a0fc',
37 'SVendor': None, 'SDevice': None}
39 intel_ioat_bdw = {'Class': '08', 'Vendor': '8086', 'Device': '6f20,6f21,6f22,6f23,6f24,6f25,6f26,6f27,6f2e,6f2f',
40 'SVendor': None, 'SDevice': None}
41 intel_ioat_skx = {'Class': '08', 'Vendor': '8086', 'Device': '2021',
42 'SVendor': None, 'SDevice': None}
44 network_devices = [network_class, cavium_pkx, avp_vnic, ifpga_class]
45 crypto_devices = [encryption_class, intel_processor_class]
46 eventdev_devices = [cavium_sso, cavium_tim, octeontx2_sso]
47 mempool_devices = [cavium_fpa, octeontx2_npa]
48 compress_devices = [cavium_zip]
49 misc_devices = [intel_ioat_bdw, intel_ioat_skx]
51 # global dict ethernet devices present. Dictionary indexed by PCI address.
52 # Each device within this is itself a dictionary of device properties
54 # list of supported DPDK drivers
55 dpdk_drivers = ["igb_uio", "vfio-pci", "uio_pci_generic"]
57 # command-line arg flags
65 '''Print usage information for the program'''
66 argv0 = basename(sys.argv[0])
71 %(argv0)s [options] DEVICE1 DEVICE2 ....
73 where DEVICE1, DEVICE2 etc, are specified via PCI "domain:bus:slot.func" syntax
74 or "bus:slot.func" syntax. For devices bound to Linux kernel drivers, they may
75 also be referred to by Linux interface name e.g. eth0, eth1, em0, em1, etc.
79 Display usage information and quit
82 Print the current status of all known network, crypto, event
84 For each device, it displays the PCI domain, bus, slot and function,
85 along with a text description of the device. Depending upon whether the
86 device is being used by a kernel driver, the igb_uio driver, or no
87 driver, other relevant information will be displayed:
88 * the Linux interface name e.g. if=eth0
89 * the driver being used e.g. drv=igb_uio
90 * any suitable drivers not currently using that device
92 NOTE: if this flag is passed along with a bind/unbind option, the
93 status display will always occur after the other operations have taken
97 Print the status of given device group. Supported device groups are:
98 "net", "crypto", "event", "mempool" and "compress"
100 -b driver, --bind=driver:
101 Select the driver to use or \"none\" to unbind the device
104 Unbind a device (Equivalent to \"-b none\")
107 By default, network devices which are used by Linux - as indicated by
108 having routes in the routing table - cannot be modified. Using the
109 --force flag overrides this behavior, allowing active links to be
111 WARNING: This can lead to loss of network connection and should be used
117 To display current device status:
120 To display current network device status:
121 %(argv0)s --status-dev net
123 To bind eth1 from the current driver and move to use igb_uio
124 %(argv0)s --bind=igb_uio eth1
126 To unbind 0000:01:00.0 from using any driver
127 %(argv0)s -u 0000:01:00.0
129 To bind 0000:02:00.0 and 0000:02:00.1 to the ixgbe kernel driver
130 %(argv0)s -b ixgbe 02:00.0 02:00.1
132 """ % locals()) # replace items from local variables
135 # This is roughly compatible with check_output function in subprocess module
136 # which is only available in python 2.7.
137 def check_output(args, stderr=None):
138 '''Run a command and capture its output'''
139 return subprocess.Popen(args, stdout=subprocess.PIPE,
140 stderr=stderr).communicate()[0]
144 '''Checks that igb_uio is loaded'''
147 # list of supported modules
148 mods = [{"Name": driver, "Found": False} for driver in dpdk_drivers]
150 # first check if module is loaded
152 # Get list of sysfs modules (both built-in and dynamically loaded)
153 sysfs_path = '/sys/module/'
155 # Get the list of directories in sysfs_path
156 sysfs_mods = [os.path.join(sysfs_path, o) for o
157 in os.listdir(sysfs_path)
158 if os.path.isdir(os.path.join(sysfs_path, o))]
160 # Extract the last element of '/sys/module/abc' in the array
161 sysfs_mods = [a.split('/')[-1] for a in sysfs_mods]
163 # special case for vfio_pci (module is named vfio-pci,
164 # but its .ko is named vfio_pci)
165 sysfs_mods = [a if a != 'vfio_pci' else 'vfio-pci' for a in sysfs_mods]
168 if mod["Name"] in sysfs_mods:
173 # check if we have at least one loaded module
174 if True not in [mod["Found"] for mod in mods] and b_flag is not None:
175 if b_flag in dpdk_drivers:
176 print("Error - no supported modules(DPDK driver) are loaded")
179 print("Warning - no supported modules(DPDK driver) are loaded")
181 # change DPDK driver list to only contain drivers that are loaded
182 dpdk_drivers = [mod["Name"] for mod in mods if mod["Found"]]
185 def has_driver(dev_id):
186 '''return true if a device is assigned to a driver. False otherwise'''
187 return "Driver_str" in devices[dev_id]
190 def get_pci_device_details(dev_id, probe_lspci):
191 '''This function gets additional details for a PCI device'''
195 extra_info = check_output(["lspci", "-vmmks", dev_id]).splitlines()
197 # parse lspci details
198 for line in extra_info:
201 name, value = line.decode().split("\t", 1)
202 name = name.strip(":") + "_str"
204 # check for a unix interface name
205 device["Interface"] = ""
206 for base, dirs, _ in os.walk("/sys/bus/pci/devices/%s/" % dev_id):
208 device["Interface"] = \
209 ",".join(os.listdir(os.path.join(base, "net")))
211 # check if a port is used for ssh connection
212 device["Ssh_if"] = False
213 device["Active"] = ""
218 '''This function clears any old data'''
221 def get_device_details(devices_type):
222 '''This function populates the "devices" dictionary. The keys used are
223 the pci addresses (domain:bus:slot.func). The values are themselves
224 dictionaries - one for each NIC.'''
228 # first loop through and read details for all devices
229 # request machine readable format, with numeric IDs and String
231 dev_lines = check_output(["lspci", "-Dvmmnnk"]).splitlines()
232 for dev_line in dev_lines:
233 if len(dev_line) == 0:
234 if device_type_match(dev, devices_type):
235 # Replace "Driver" with "Driver_str" to have consistency of
236 # of dictionary key names
237 if "Driver" in dev.keys():
238 dev["Driver_str"] = dev.pop("Driver")
239 if "Module" in dev.keys():
240 dev["Module_str"] = dev.pop("Module")
241 # use dict to make copy of dev
242 devices[dev["Slot"]] = dict(dev)
243 # Clear previous device's data
246 name, value = dev_line.decode().split("\t", 1)
247 value_list = value.rsplit(' ', 1)
248 if len(value_list) > 1:
249 # String stored in <name>_str
250 dev[name.rstrip(":") + '_str'] = value_list[0]
252 dev[name.rstrip(":")] = value_list[len(value_list) - 1] \
253 .rstrip("]").lstrip("[")
255 if devices_type == network_devices:
256 # check what is the interface if any for an ssh connection if
257 # any to this host, so we can mark it later.
259 route = check_output(["ip", "-o", "route"])
260 # filter out all lines for 169.254 routes
261 route = "\n".join(filter(lambda ln: not ln.startswith("169.254"),
262 route.decode().splitlines()))
263 rt_info = route.split()
264 for i in range(len(rt_info) - 1):
265 if rt_info[i] == "dev":
266 ssh_if.append(rt_info[i+1])
268 # based on the basic info, get extended text details
269 for d in devices.keys():
270 if not device_type_match(devices[d], devices_type):
273 # get additional info and add it to existing data
274 devices[d] = devices[d].copy()
275 # No need to probe lspci
276 devices[d].update(get_pci_device_details(d, False).items())
278 if devices_type == network_devices:
280 if _if in devices[d]["Interface"].split(","):
281 devices[d]["Ssh_if"] = True
282 devices[d]["Active"] = "*Active*"
285 # add igb_uio to list of supporting modules if needed
286 if "Module_str" in devices[d]:
287 for driver in dpdk_drivers:
288 if driver not in devices[d]["Module_str"]:
289 devices[d]["Module_str"] = \
290 devices[d]["Module_str"] + ",%s" % driver
292 devices[d]["Module_str"] = ",".join(dpdk_drivers)
294 # make sure the driver and module strings do not have any duplicates
296 modules = devices[d]["Module_str"].split(",")
297 if devices[d]["Driver_str"] in modules:
298 modules.remove(devices[d]["Driver_str"])
299 devices[d]["Module_str"] = ",".join(modules)
302 def device_type_match(dev, devices_type):
303 for i in range(len(devices_type)):
305 [x for x in devices_type[i].values() if x is not None])
307 if dev["Class"][0:2] == devices_type[i]["Class"]:
308 match_count = match_count + 1
309 for key in devices_type[i].keys():
310 if key != 'Class' and devices_type[i][key]:
311 value_list = devices_type[i][key].split(',')
312 for value in value_list:
313 if value.strip(' ') == dev[key]:
314 match_count = match_count + 1
315 # count must be the number of non None parameters to match
316 if match_count == param_count:
320 def dev_id_from_dev_name(dev_name):
321 '''Take a device "name" - a string passed in by user to identify a NIC
322 device, and determine the device id - i.e. the domain:bus:slot.func - for
323 it, which can then be used to index into the devices array'''
325 # check if it's already a suitable index
326 if dev_name in devices:
328 # check if it's an index just missing the domain part
329 elif "0000:" + dev_name in devices:
330 return "0000:" + dev_name
332 # check if it's an interface name, e.g. eth1
333 for d in devices.keys():
334 if dev_name in devices[d]["Interface"].split(","):
335 return devices[d]["Slot"]
336 # if nothing else matches - error
337 print("Unknown device: %s. "
338 "Please specify device in \"bus:slot.func\" format" % dev_name)
342 def unbind_one(dev_id, force):
343 '''Unbind the device identified by "dev_id" from its current driver'''
344 dev = devices[dev_id]
345 if not has_driver(dev_id):
346 print("%s %s %s is not currently managed by any driver\n" %
347 (dev["Slot"], dev["Device_str"], dev["Interface"]))
350 # prevent us disconnecting ourselves
351 if dev["Ssh_if"] and not force:
352 print("Routing table indicates that interface %s is active. "
353 "Skipping unbind" % (dev_id))
356 # write to /sys to unbind
357 filename = "/sys/bus/pci/drivers/%s/unbind" % dev["Driver_str"]
359 f = open(filename, "a")
361 print("Error: unbind failed for %s - Cannot open %s"
362 % (dev_id, filename))
368 def bind_one(dev_id, driver, force):
369 '''Bind the device given by "dev_id" to the driver "driver". If the device
370 is already bound to a different driver, it will be unbound first'''
371 dev = devices[dev_id]
372 saved_driver = None # used to rollback any unbind in case of failure
374 # prevent disconnection of our ssh session
375 if dev["Ssh_if"] and not force:
376 print("Routing table indicates that interface %s is active. "
377 "Not modifying" % (dev_id))
380 # unbind any existing drivers we don't want
381 if has_driver(dev_id):
382 if dev["Driver_str"] == driver:
383 print("%s already bound to driver %s, skipping\n"
387 saved_driver = dev["Driver_str"]
388 unbind_one(dev_id, force)
389 dev["Driver_str"] = "" # clear driver string
391 # For kernels >= 3.15 driver_override can be used to specify the driver
392 # for a device rather than relying on the driver to provide a positive
393 # match of the device. The existing process of looking up
394 # the vendor and device ID, adding them to the driver new_id,
395 # will erroneously bind other devices too which has the additional burden
396 # of unbinding those devices
397 if driver in dpdk_drivers:
398 filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id
399 if os.path.exists(filename):
401 f = open(filename, "w")
403 print("Error: bind failed for %s - Cannot open %s"
404 % (dev_id, filename))
407 f.write("%s" % driver)
410 print("Error: bind failed for %s - Cannot write driver %s to "
411 "PCI ID " % (dev_id, driver))
413 # For kernels < 3.15 use new_id to add PCI id's to the driver
415 filename = "/sys/bus/pci/drivers/%s/new_id" % driver
417 f = open(filename, "w")
419 print("Error: bind failed for %s - Cannot open %s"
420 % (dev_id, filename))
423 # Convert Device and Vendor Id to int to write to new_id
424 f.write("%04x %04x" % (int(dev["Vendor"],16),
425 int(dev["Device"], 16)))
428 print("Error: bind failed for %s - Cannot write new PCI ID to "
429 "driver %s" % (dev_id, driver))
432 # do the bind by writing to /sys
433 filename = "/sys/bus/pci/drivers/%s/bind" % driver
435 f = open(filename, "a")
437 print("Error: bind failed for %s - Cannot open %s"
438 % (dev_id, filename))
439 if saved_driver is not None: # restore any previous driver
440 bind_one(dev_id, saved_driver, force)
446 # for some reason, closing dev_id after adding a new PCI ID to new_id
447 # results in IOError. however, if the device was successfully bound,
448 # we don't care for any errors and can safely ignore IOError
449 tmp = get_pci_device_details(dev_id, True)
450 if "Driver_str" in tmp and tmp["Driver_str"] == driver:
452 print("Error: bind failed for %s - Cannot bind to driver %s"
454 if saved_driver is not None: # restore any previous driver
455 bind_one(dev_id, saved_driver, force)
458 # For kernels > 3.15 driver_override is used to bind a device to a driver.
459 # Before unbinding it, overwrite driver_override with empty string so that
460 # the device can be bound to any other driver
461 filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id
462 if os.path.exists(filename):
464 f = open(filename, "w")
466 print("Error: unbind failed for %s - Cannot open %s"
467 % (dev_id, filename))
473 print("Error: unbind failed for %s - Cannot open %s"
474 % (dev_id, filename))
478 def unbind_all(dev_list, force=False):
479 """Unbind method, takes a list of device locations"""
481 if dev_list[0] == "dpdk":
482 for d in devices.keys():
483 if "Driver_str" in devices[d]:
484 if devices[d]["Driver_str"] in dpdk_drivers:
485 unbind_one(devices[d]["Slot"], force)
488 dev_list = map(dev_id_from_dev_name, dev_list)
493 def bind_all(dev_list, driver, force=False):
494 """Bind method, takes a list of device locations"""
497 dev_list = map(dev_id_from_dev_name, dev_list)
500 bind_one(d, driver, force)
502 # For kernels < 3.15 when binding devices to a generic driver
503 # (i.e. one that doesn't have a PCI ID table) using new_id, some devices
504 # that are not bound to any other driver could be bound even if no one has
505 # asked them to. hence, we check the list of drivers again, and see if
506 # some of the previously-unbound devices were erroneously bound.
507 if not os.path.exists("/sys/bus/pci/devices/%s/driver_override" % d):
508 for d in devices.keys():
509 # skip devices that were already bound or that we know should be bound
510 if "Driver_str" in devices[d] or d in dev_list:
513 # update information about this device
514 devices[d] = dict(devices[d].items() +
515 get_pci_device_details(d, True).items())
517 # check if updated information indicates that the device was bound
518 if "Driver_str" in devices[d]:
522 def display_devices(title, dev_list, extra_params=None):
523 '''Displays to the user the details of a list of devices given in
524 "dev_list". The "extra_params" parameter, if given, should contain a string
525 with %()s fields in it for replacement by the named fields in each
526 device's dictionary.'''
527 strings = [] # this holds the strings to print. We sort before printing
528 print("\n%s" % title)
529 print("="*len(title))
530 if len(dev_list) == 0:
531 strings.append("<none>")
534 if extra_params is not None:
535 strings.append("%s '%s %s' %s" % (dev["Slot"],
540 strings.append("%s '%s'" % (dev["Slot"], dev["Device_str"]))
541 # sort before printing, so that the entries appear in PCI order
543 print("\n".join(strings)) # print one per line
545 def show_device_status(devices_type, device_name):
551 # split our list of network devices into the three categories above
552 for d in devices.keys():
553 if device_type_match(devices[d], devices_type):
554 if not has_driver(d):
555 no_drv.append(devices[d])
557 if devices[d]["Driver_str"] in dpdk_drivers:
558 dpdk_drv.append(devices[d])
560 kernel_drv.append(devices[d])
562 n_devs = len(dpdk_drv) + len(kernel_drv) + len(no_drv)
564 # don't bother displaying anything if there are no devices
566 msg = "No '%s' devices detected" % device_name
569 print("".join('=' * len(msg)))
572 # print each category separately, so we can clearly see what's used by DPDK
573 if len(dpdk_drv) != 0:
574 display_devices("%s devices using DPDK-compatible driver" % device_name,
575 dpdk_drv, "drv=%(Driver_str)s unused=%(Module_str)s")
576 if len(kernel_drv) != 0:
577 display_devices("%s devices using kernel driver" % device_name, kernel_drv,
578 "if=%(Interface)s drv=%(Driver_str)s "
579 "unused=%(Module_str)s %(Active)s")
581 display_devices("Other %s devices" % device_name, no_drv,
582 "unused=%(Module_str)s")
585 '''Function called when the script is passed the "--status" option.
586 Displays to the user what devices are bound to the igb_uio driver, the
587 kernel driver or to no driver'''
589 if status_dev == "net" or status_dev == "all":
590 show_device_status(network_devices, "Network")
592 if status_dev == "crypto" or status_dev == "all":
593 show_device_status(crypto_devices, "Crypto")
595 if status_dev == "event" or status_dev == "all":
596 show_device_status(eventdev_devices, "Eventdev")
598 if status_dev == "mempool" or status_dev == "all":
599 show_device_status(mempool_devices, "Mempool")
601 if status_dev == "compress" or status_dev == "all":
602 show_device_status(compress_devices , "Compress")
604 if status_dev == "misc" or status_dev == "all":
605 show_device_status(misc_devices, "Misc (rawdev)")
608 '''Parses the command-line arguments given by the user and takes the
609 appropriate action for each'''
615 if len(sys.argv) <= 1:
620 opts, args = getopt.getopt(sys.argv[1:], "b:us",
621 ["help", "usage", "status", "status-dev=",
622 "force", "bind=", "unbind", ])
623 except getopt.GetoptError as error:
625 print("Run '%s --usage' for further information" % sys.argv[0])
628 for opt, arg in opts:
629 if opt == "--help" or opt == "--usage":
632 if opt == "--status-dev":
635 if opt == "--status" or opt == "-s":
640 if opt == "-b" or opt == "-u" or opt == "--bind" or opt == "--unbind":
641 if b_flag is not None:
642 print("Error - Only one bind or unbind may be specified\n")
644 if opt == "-u" or opt == "--unbind":
650 def do_arg_actions():
651 '''do the actual action requested by the user'''
657 if b_flag is None and not status_flag:
658 print("Error: No action specified for devices."
659 "Please give a -b or -u option")
660 print("Run '%s --usage' for further information" % sys.argv[0])
663 if b_flag is not None and len(args) == 0:
664 print("Error: No devices specified.")
665 print("Run '%s --usage' for further information" % sys.argv[0])
668 if b_flag == "none" or b_flag == "None":
669 unbind_all(args, force_flag)
670 elif b_flag is not None:
671 bind_all(args, b_flag, force_flag)
673 if b_flag is not None:
675 # refresh if we have changed anything
676 get_device_details(network_devices)
677 get_device_details(crypto_devices)
678 get_device_details(eventdev_devices)
679 get_device_details(mempool_devices)
680 get_device_details(compress_devices)
681 get_device_details(misc_devices)
686 '''program main function'''
687 # check if lspci is installed, suppress any output
688 with open(os.devnull, 'w') as devnull:
689 ret = subprocess.call(['which', 'lspci'],
690 stdout=devnull, stderr=devnull)
692 print("'lspci' not found - please install 'pciutils'")
697 get_device_details(network_devices)
698 get_device_details(crypto_devices)
699 get_device_details(eventdev_devices)
700 get_device_details(mempool_devices)
701 get_device_details(compress_devices)
702 get_device_details(misc_devices)
705 if __name__ == "__main__":