2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright(c) 2010-2014 Intel Corporation
6 from __future__ import print_function
11 from os.path import exists, abspath, dirname, basename
13 # The PCI base class for all devices
14 network_class = {'Class': '02', 'Vendor': None, 'Device': None,
15 'SVendor': None, 'SDevice': None}
16 acceleration_class = {'Class': '12', 'Vendor': None, 'Device': None,
17 'SVendor': None, 'SDevice': None}
18 ifpga_class = {'Class': '12', 'Vendor': '8086', 'Device': '0b30',
19 'SVendor': None, 'SDevice': None}
20 encryption_class = {'Class': '10', 'Vendor': None, 'Device': None,
21 'SVendor': None, 'SDevice': None}
22 intel_processor_class = {'Class': '0b', 'Vendor': '8086', 'Device': None,
23 'SVendor': None, 'SDevice': None}
24 cavium_sso = {'Class': '08', 'Vendor': '177d', 'Device': 'a04b,a04d',
25 'SVendor': None, 'SDevice': None}
26 cavium_fpa = {'Class': '08', 'Vendor': '177d', 'Device': 'a053',
27 'SVendor': None, 'SDevice': None}
28 cavium_pkx = {'Class': '08', 'Vendor': '177d', 'Device': 'a0dd,a049',
29 'SVendor': None, 'SDevice': None}
30 cavium_tim = {'Class': '08', 'Vendor': '177d', 'Device': 'a051',
31 'SVendor': None, 'SDevice': None}
32 cavium_zip = {'Class': '12', 'Vendor': '177d', 'Device': 'a037',
33 'SVendor': None, 'SDevice': None}
34 avp_vnic = {'Class': '05', 'Vendor': '1af4', 'Device': '1110',
35 'SVendor': None, 'SDevice': None}
37 octeontx2_sso = {'Class': '08', 'Vendor': '177d', 'Device': 'a0f9,a0fa',
38 'SVendor': None, 'SDevice': None}
39 octeontx2_npa = {'Class': '08', 'Vendor': '177d', 'Device': 'a0fb,a0fc',
40 'SVendor': None, 'SDevice': None}
41 octeontx2_dma = {'Class': '08', 'Vendor': '177d', 'Device': 'a081',
42 'SVendor': None, 'SDevice': None}
44 intel_ioat_bdw = {'Class': '08', 'Vendor': '8086', 'Device': '6f20,6f21,6f22,6f23,6f24,6f25,6f26,6f27,6f2e,6f2f',
45 'SVendor': None, 'SDevice': None}
46 intel_ioat_skx = {'Class': '08', 'Vendor': '8086', 'Device': '2021',
47 'SVendor': None, 'SDevice': None}
48 intel_ntb_skx = {'Class': '06', 'Vendor': '8086', 'Device': '201c',
49 'SVendor': None, 'SDevice': None}
51 network_devices = [network_class, cavium_pkx, avp_vnic, ifpga_class]
52 baseband_devices = [acceleration_class]
53 crypto_devices = [encryption_class, intel_processor_class]
54 eventdev_devices = [cavium_sso, cavium_tim, octeontx2_sso]
55 mempool_devices = [cavium_fpa, octeontx2_npa]
56 compress_devices = [cavium_zip]
57 misc_devices = [intel_ioat_bdw, intel_ioat_skx, intel_ntb_skx, octeontx2_dma]
59 # global dict ethernet devices present. Dictionary indexed by PCI address.
60 # Each device within this is itself a dictionary of device properties
62 # list of supported DPDK drivers
63 dpdk_drivers = ["igb_uio", "vfio-pci", "uio_pci_generic"]
64 # list of currently loaded kernel modules
67 # command-line arg flags
75 '''Print usage information for the program'''
76 argv0 = basename(sys.argv[0])
81 %(argv0)s [options] DEVICE1 DEVICE2 ....
83 where DEVICE1, DEVICE2 etc, are specified via PCI "domain:bus:slot.func" syntax
84 or "bus:slot.func" syntax. For devices bound to Linux kernel drivers, they may
85 also be referred to by Linux interface name e.g. eth0, eth1, em0, em1, etc.
89 Display usage information and quit
92 Print the current status of all known network, crypto, event
94 For each device, it displays the PCI domain, bus, slot and function,
95 along with a text description of the device. Depending upon whether the
96 device is being used by a kernel driver, the igb_uio driver, or no
97 driver, other relevant information will be displayed:
98 * the Linux interface name e.g. if=eth0
99 * the driver being used e.g. drv=igb_uio
100 * any suitable drivers not currently using that device
102 NOTE: if this flag is passed along with a bind/unbind option, the
103 status display will always occur after the other operations have taken
107 Print the status of given device group. Supported device groups are:
108 "net", "baseband", "crypto", "event", "mempool" and "compress"
110 -b driver, --bind=driver:
111 Select the driver to use or \"none\" to unbind the device
114 Unbind a device (Equivalent to \"-b none\")
117 By default, network devices which are used by Linux - as indicated by
118 having routes in the routing table - cannot be modified. Using the
119 --force flag overrides this behavior, allowing active links to be
121 WARNING: This can lead to loss of network connection and should be used
127 To display current device status:
130 To display current network device status:
131 %(argv0)s --status-dev net
133 To bind eth1 from the current driver and move to use igb_uio
134 %(argv0)s --bind=igb_uio eth1
136 To unbind 0000:01:00.0 from using any driver
137 %(argv0)s -u 0000:01:00.0
139 To bind 0000:02:00.0 and 0000:02:00.1 to the ixgbe kernel driver
140 %(argv0)s -b ixgbe 02:00.0 02:00.1
142 """ % locals()) # replace items from local variables
145 # This is roughly compatible with check_output function in subprocess module
146 # which is only available in python 2.7.
147 def check_output(args, stderr=None):
148 '''Run a command and capture its output'''
149 return subprocess.Popen(args, stdout=subprocess.PIPE,
150 stderr=stderr).communicate()[0]
152 # check if a specific kernel module is loaded
153 def module_is_loaded(module):
154 global loaded_modules
157 return module in loaded_modules
159 # Get list of sysfs modules (both built-in and dynamically loaded)
160 sysfs_path = '/sys/module/'
162 # Get the list of directories in sysfs_path
163 sysfs_mods = [m for m in os.listdir(sysfs_path)
164 if os.path.isdir(os.path.join(sysfs_path, m))]
166 # special case for vfio_pci (module is named vfio-pci,
167 # but its .ko is named vfio_pci)
168 sysfs_mods = [a if a != 'vfio_pci' else 'vfio-pci' for a in sysfs_mods]
170 loaded_modules = sysfs_mods
172 return module in sysfs_mods
176 '''Checks that igb_uio is loaded'''
179 # list of supported modules
180 mods = [{"Name": driver, "Found": False} for driver in dpdk_drivers]
182 # first check if module is loaded
184 if module_is_loaded(mod["Name"]):
187 # check if we have at least one loaded module
188 if True not in [mod["Found"] for mod in mods] and b_flag is not None:
189 print("Warning: no supported DPDK kernel modules are loaded", file=sys.stderr)
191 # change DPDK driver list to only contain drivers that are loaded
192 dpdk_drivers = [mod["Name"] for mod in mods if mod["Found"]]
195 def has_driver(dev_id):
196 '''return true if a device is assigned to a driver. False otherwise'''
197 return "Driver_str" in devices[dev_id]
200 def get_pci_device_details(dev_id, probe_lspci):
201 '''This function gets additional details for a PCI device'''
205 extra_info = check_output(["lspci", "-vmmks", dev_id]).splitlines()
207 # parse lspci details
208 for line in extra_info:
211 name, value = line.decode().split("\t", 1)
212 name = name.strip(":") + "_str"
214 # check for a unix interface name
215 device["Interface"] = ""
216 for base, dirs, _ in os.walk("/sys/bus/pci/devices/%s/" % dev_id):
218 device["Interface"] = \
219 ",".join(os.listdir(os.path.join(base, "net")))
221 # check if a port is used for ssh connection
222 device["Ssh_if"] = False
223 device["Active"] = ""
228 '''This function clears any old data'''
232 def get_device_details(devices_type):
233 '''This function populates the "devices" dictionary. The keys used are
234 the pci addresses (domain:bus:slot.func). The values are themselves
235 dictionaries - one for each NIC.'''
239 # first loop through and read details for all devices
240 # request machine readable format, with numeric IDs and String
242 dev_lines = check_output(["lspci", "-Dvmmnnk"]).splitlines()
243 for dev_line in dev_lines:
244 if len(dev_line) == 0:
245 if device_type_match(dev, devices_type):
246 # Replace "Driver" with "Driver_str" to have consistency of
247 # of dictionary key names
248 if "Driver" in dev.keys():
249 dev["Driver_str"] = dev.pop("Driver")
250 if "Module" in dev.keys():
251 dev["Module_str"] = dev.pop("Module")
252 # use dict to make copy of dev
253 devices[dev["Slot"]] = dict(dev)
254 # Clear previous device's data
257 name, value = dev_line.decode().split("\t", 1)
258 value_list = value.rsplit(' ', 1)
259 if len(value_list) > 1:
260 # String stored in <name>_str
261 dev[name.rstrip(":") + '_str'] = value_list[0]
263 dev[name.rstrip(":")] = value_list[len(value_list) - 1] \
264 .rstrip("]").lstrip("[")
266 if devices_type == network_devices:
267 # check what is the interface if any for an ssh connection if
268 # any to this host, so we can mark it later.
270 route = check_output(["ip", "-o", "route"])
271 # filter out all lines for 169.254 routes
272 route = "\n".join(filter(lambda ln: not ln.startswith("169.254"),
273 route.decode().splitlines()))
274 rt_info = route.split()
275 for i in range(len(rt_info) - 1):
276 if rt_info[i] == "dev":
277 ssh_if.append(rt_info[i+1])
279 # based on the basic info, get extended text details
280 for d in devices.keys():
281 if not device_type_match(devices[d], devices_type):
284 # get additional info and add it to existing data
285 devices[d] = devices[d].copy()
286 # No need to probe lspci
287 devices[d].update(get_pci_device_details(d, False).items())
289 if devices_type == network_devices:
291 if _if in devices[d]["Interface"].split(","):
292 devices[d]["Ssh_if"] = True
293 devices[d]["Active"] = "*Active*"
296 # add igb_uio to list of supporting modules if needed
297 if "Module_str" in devices[d]:
298 for driver in dpdk_drivers:
299 if driver not in devices[d]["Module_str"]:
300 devices[d]["Module_str"] = \
301 devices[d]["Module_str"] + ",%s" % driver
303 devices[d]["Module_str"] = ",".join(dpdk_drivers)
305 # make sure the driver and module strings do not have any duplicates
307 modules = devices[d]["Module_str"].split(",")
308 if devices[d]["Driver_str"] in modules:
309 modules.remove(devices[d]["Driver_str"])
310 devices[d]["Module_str"] = ",".join(modules)
313 def device_type_match(dev, devices_type):
314 for i in range(len(devices_type)):
316 [x for x in devices_type[i].values() if x is not None])
318 if dev["Class"][0:2] == devices_type[i]["Class"]:
319 match_count = match_count + 1
320 for key in devices_type[i].keys():
321 if key != 'Class' and devices_type[i][key]:
322 value_list = devices_type[i][key].split(',')
323 for value in value_list:
324 if value.strip(' ') == dev[key]:
325 match_count = match_count + 1
326 # count must be the number of non None parameters to match
327 if match_count == param_count:
331 def dev_id_from_dev_name(dev_name):
332 '''Take a device "name" - a string passed in by user to identify a NIC
333 device, and determine the device id - i.e. the domain:bus:slot.func - for
334 it, which can then be used to index into the devices array'''
336 # check if it's already a suitable index
337 if dev_name in devices:
339 # check if it's an index just missing the domain part
340 elif "0000:" + dev_name in devices:
341 return "0000:" + dev_name
343 # check if it's an interface name, e.g. eth1
344 for d in devices.keys():
345 if dev_name in devices[d]["Interface"].split(","):
346 return devices[d]["Slot"]
347 # if nothing else matches - error
348 raise ValueError("Unknown device: %s. "
349 "Please specify device in \"bus:slot.func\" format" % dev_name)
352 def unbind_one(dev_id, force):
353 '''Unbind the device identified by "dev_id" from its current driver'''
354 dev = devices[dev_id]
355 if not has_driver(dev_id):
356 print("Notice: %s %s %s is not currently managed by any driver" %
357 (dev["Slot"], dev["Device_str"], dev["Interface"]), file=sys.stderr)
360 # prevent us disconnecting ourselves
361 if dev["Ssh_if"] and not force:
362 print("Warning: routing table indicates that interface %s is active. "
363 "Skipping unbind" % dev_id, file=sys.stderr)
366 # write to /sys to unbind
367 filename = "/sys/bus/pci/drivers/%s/unbind" % dev["Driver_str"]
369 f = open(filename, "a")
371 sys.exit("Error: unbind failed for %s - Cannot open %s" %
377 def bind_one(dev_id, driver, force):
378 '''Bind the device given by "dev_id" to the driver "driver". If the device
379 is already bound to a different driver, it will be unbound first'''
380 dev = devices[dev_id]
381 saved_driver = None # used to rollback any unbind in case of failure
383 # prevent disconnection of our ssh session
384 if dev["Ssh_if"] and not force:
385 print("Warning: routing table indicates that interface %s is active. "
386 "Not modifying" % dev_id, file=sys.stderr)
389 # unbind any existing drivers we don't want
390 if has_driver(dev_id):
391 if dev["Driver_str"] == driver:
392 print("Notice: %s already bound to driver %s, skipping" %
393 (dev_id, driver), file=sys.stderr)
396 saved_driver = dev["Driver_str"]
397 unbind_one(dev_id, force)
398 dev["Driver_str"] = "" # clear driver string
400 # For kernels >= 3.15 driver_override can be used to specify the driver
401 # for a device rather than relying on the driver to provide a positive
402 # match of the device. The existing process of looking up
403 # the vendor and device ID, adding them to the driver new_id,
404 # will erroneously bind other devices too which has the additional burden
405 # of unbinding those devices
406 if driver in dpdk_drivers:
407 filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id
408 if os.path.exists(filename):
410 f = open(filename, "w")
412 print("Error: bind failed for %s - Cannot open %s"
413 % (dev_id, filename), file=sys.stderr)
416 f.write("%s" % driver)
419 print("Error: bind failed for %s - Cannot write driver %s to "
420 "PCI ID " % (dev_id, driver), file=sys.stderr)
422 # For kernels < 3.15 use new_id to add PCI id's to the driver
424 filename = "/sys/bus/pci/drivers/%s/new_id" % driver
426 f = open(filename, "w")
428 print("Error: bind failed for %s - Cannot open %s"
429 % (dev_id, filename), file=sys.stderr)
432 # Convert Device and Vendor Id to int to write to new_id
433 f.write("%04x %04x" % (int(dev["Vendor"],16),
434 int(dev["Device"], 16)))
437 print("Error: bind failed for %s - Cannot write new PCI ID to "
438 "driver %s" % (dev_id, driver), file=sys.stderr)
441 # do the bind by writing to /sys
442 filename = "/sys/bus/pci/drivers/%s/bind" % driver
444 f = open(filename, "a")
446 print("Error: bind failed for %s - Cannot open %s"
447 % (dev_id, filename), file=sys.stderr)
448 if saved_driver is not None: # restore any previous driver
449 bind_one(dev_id, saved_driver, force)
455 # for some reason, closing dev_id after adding a new PCI ID to new_id
456 # results in IOError. however, if the device was successfully bound,
457 # we don't care for any errors and can safely ignore IOError
458 tmp = get_pci_device_details(dev_id, True)
459 if "Driver_str" in tmp and tmp["Driver_str"] == driver:
461 print("Error: bind failed for %s - Cannot bind to driver %s"
462 % (dev_id, driver), file=sys.stderr)
463 if saved_driver is not None: # restore any previous driver
464 bind_one(dev_id, saved_driver, force)
467 # For kernels > 3.15 driver_override is used to bind a device to a driver.
468 # Before unbinding it, overwrite driver_override with empty string so that
469 # the device can be bound to any other driver
470 filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id
471 if os.path.exists(filename):
473 f = open(filename, "w")
475 sys.exit("Error: unbind failed for %s - Cannot open %s"
476 % (dev_id, filename))
481 sys.exit("Error: unbind failed for %s - Cannot open %s"
482 % (dev_id, filename))
485 def unbind_all(dev_list, force=False):
486 """Unbind method, takes a list of device locations"""
488 if dev_list[0] == "dpdk":
489 for d in devices.keys():
490 if "Driver_str" in devices[d]:
491 if devices[d]["Driver_str"] in dpdk_drivers:
492 unbind_one(devices[d]["Slot"], force)
496 dev_list = map(dev_id_from_dev_name, dev_list)
497 except ValueError as ex:
505 def bind_all(dev_list, driver, force=False):
506 """Bind method, takes a list of device locations"""
509 # a common user error is to forget to specify the driver the devices need to
510 # be bound to. check if the driver is a valid device, and if it is, show
511 # a meaningful error.
513 dev_id_from_dev_name(driver)
514 # if we've made it this far, this means that the "driver" was a valid
515 # device string, so it's probably not a valid driver name.
516 sys.exit("Error: Driver '%s' does not look like a valid driver. " \
517 "Did you forget to specify the driver to bind devices to?" % driver)
519 # driver generated error - it's not a valid device ID, so all is well
522 # check if we're attempting to bind to a driver that isn't loaded
523 if not module_is_loaded(driver):
524 sys.exit("Error: Driver '%s' is not loaded." % driver)
527 dev_list = map(dev_id_from_dev_name, dev_list)
528 except ValueError as ex:
532 bind_one(d, driver, force)
534 # For kernels < 3.15 when binding devices to a generic driver
535 # (i.e. one that doesn't have a PCI ID table) using new_id, some devices
536 # that are not bound to any other driver could be bound even if no one has
537 # asked them to. hence, we check the list of drivers again, and see if
538 # some of the previously-unbound devices were erroneously bound.
539 if not os.path.exists("/sys/bus/pci/devices/%s/driver_override" % d):
540 for d in devices.keys():
541 # skip devices that were already bound or that we know should be bound
542 if "Driver_str" in devices[d] or d in dev_list:
545 # update information about this device
546 devices[d] = dict(devices[d].items() +
547 get_pci_device_details(d, True).items())
549 # check if updated information indicates that the device was bound
550 if "Driver_str" in devices[d]:
554 def display_devices(title, dev_list, extra_params=None):
555 '''Displays to the user the details of a list of devices given in
556 "dev_list". The "extra_params" parameter, if given, should contain a string
557 with %()s fields in it for replacement by the named fields in each
558 device's dictionary.'''
559 strings = [] # this holds the strings to print. We sort before printing
560 print("\n%s" % title)
561 print("="*len(title))
562 if len(dev_list) == 0:
563 strings.append("<none>")
566 if extra_params is not None:
567 strings.append("%s '%s %s' %s" % (dev["Slot"],
572 strings.append("%s '%s'" % (dev["Slot"], dev["Device_str"]))
573 # sort before printing, so that the entries appear in PCI order
575 print("\n".join(strings)) # print one per line
577 def show_device_status(devices_type, device_name):
583 # split our list of network devices into the three categories above
584 for d in devices.keys():
585 if device_type_match(devices[d], devices_type):
586 if not has_driver(d):
587 no_drv.append(devices[d])
589 if devices[d]["Driver_str"] in dpdk_drivers:
590 dpdk_drv.append(devices[d])
592 kernel_drv.append(devices[d])
594 n_devs = len(dpdk_drv) + len(kernel_drv) + len(no_drv)
596 # don't bother displaying anything if there are no devices
598 msg = "No '%s' devices detected" % device_name
601 print("".join('=' * len(msg)))
604 # print each category separately, so we can clearly see what's used by DPDK
605 if len(dpdk_drv) != 0:
606 display_devices("%s devices using DPDK-compatible driver" % device_name,
607 dpdk_drv, "drv=%(Driver_str)s unused=%(Module_str)s")
608 if len(kernel_drv) != 0:
609 display_devices("%s devices using kernel driver" % device_name, kernel_drv,
610 "if=%(Interface)s drv=%(Driver_str)s "
611 "unused=%(Module_str)s %(Active)s")
613 display_devices("Other %s devices" % device_name, no_drv,
614 "unused=%(Module_str)s")
617 '''Function called when the script is passed the "--status" option.
618 Displays to the user what devices are bound to the igb_uio driver, the
619 kernel driver or to no driver'''
621 if status_dev == "net" or status_dev == "all":
622 show_device_status(network_devices, "Network")
624 if status_dev == "baseband" or status_dev == "all":
625 show_device_status(baseband_devices, "Baseband")
627 if status_dev == "crypto" or status_dev == "all":
628 show_device_status(crypto_devices, "Crypto")
630 if status_dev == "event" or status_dev == "all":
631 show_device_status(eventdev_devices, "Eventdev")
633 if status_dev == "mempool" or status_dev == "all":
634 show_device_status(mempool_devices, "Mempool")
636 if status_dev == "compress" or status_dev == "all":
637 show_device_status(compress_devices , "Compress")
639 if status_dev == "misc" or status_dev == "all":
640 show_device_status(misc_devices, "Misc (rawdev)")
643 '''Parses the command-line arguments given by the user and takes the
644 appropriate action for each'''
650 if len(sys.argv) <= 1:
655 opts, args = getopt.getopt(sys.argv[1:], "b:us",
656 ["help", "usage", "status", "status-dev=",
657 "force", "bind=", "unbind", ])
658 except getopt.GetoptError as error:
660 print("Run '%s --usage' for further information" % sys.argv[0])
663 for opt, arg in opts:
664 if opt == "--help" or opt == "--usage":
667 if opt == "--status-dev":
670 if opt == "--status" or opt == "-s":
675 if opt == "-b" or opt == "-u" or opt == "--bind" or opt == "--unbind":
676 if b_flag is not None:
677 sys.exit("Error: binding and unbinding are mutually exclusive")
678 if opt == "-u" or opt == "--unbind":
684 def do_arg_actions():
685 '''do the actual action requested by the user'''
691 if b_flag is None and not status_flag:
692 print("Error: No action specified for devices. "
693 "Please give a -b or -u option", file=sys.stderr)
697 if b_flag is not None and len(args) == 0:
698 print("Error: No devices specified.", file=sys.stderr)
702 if b_flag == "none" or b_flag == "None":
703 unbind_all(args, force_flag)
704 elif b_flag is not None:
705 bind_all(args, b_flag, force_flag)
707 if b_flag is not None:
709 # refresh if we have changed anything
710 get_device_details(network_devices)
711 get_device_details(baseband_devices)
712 get_device_details(crypto_devices)
713 get_device_details(eventdev_devices)
714 get_device_details(mempool_devices)
715 get_device_details(compress_devices)
716 get_device_details(misc_devices)
721 '''program main function'''
722 # check if lspci is installed, suppress any output
723 with open(os.devnull, 'w') as devnull:
724 ret = subprocess.call(['which', 'lspci'],
725 stdout=devnull, stderr=devnull)
727 sys.exit("'lspci' not found - please install 'pciutils'")
731 get_device_details(network_devices)
732 get_device_details(baseband_devices)
733 get_device_details(crypto_devices)
734 get_device_details(eventdev_devices)
735 get_device_details(mempool_devices)
736 get_device_details(compress_devices)
737 get_device_details(misc_devices)
740 if __name__ == "__main__":