2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright(c) 2010-2014 Intel Corporation
12 from os.path import exists, basename
13 from os.path import join as path_join
15 # The PCI base class for all devices
16 network_class = {'Class': '02', 'Vendor': None, 'Device': None,
17 'SVendor': None, 'SDevice': None}
18 acceleration_class = {'Class': '12', 'Vendor': None, 'Device': None,
19 'SVendor': None, 'SDevice': None}
20 ifpga_class = {'Class': '12', 'Vendor': '8086', 'Device': '0b30',
21 'SVendor': None, 'SDevice': None}
22 encryption_class = {'Class': '10', 'Vendor': None, 'Device': None,
23 'SVendor': None, 'SDevice': None}
24 intel_processor_class = {'Class': '0b', 'Vendor': '8086', 'Device': None,
25 'SVendor': None, 'SDevice': None}
26 cavium_sso = {'Class': '08', 'Vendor': '177d', 'Device': 'a04b,a04d',
27 'SVendor': None, 'SDevice': None}
28 cavium_fpa = {'Class': '08', 'Vendor': '177d', 'Device': 'a053',
29 'SVendor': None, 'SDevice': None}
30 cavium_pkx = {'Class': '08', 'Vendor': '177d', 'Device': 'a0dd,a049',
31 'SVendor': None, 'SDevice': None}
32 cavium_tim = {'Class': '08', 'Vendor': '177d', 'Device': 'a051',
33 'SVendor': None, 'SDevice': None}
34 cavium_zip = {'Class': '12', 'Vendor': '177d', 'Device': 'a037',
35 'SVendor': None, 'SDevice': None}
36 avp_vnic = {'Class': '05', 'Vendor': '1af4', 'Device': '1110',
37 'SVendor': None, 'SDevice': None}
39 octeontx2_sso = {'Class': '08', 'Vendor': '177d', 'Device': 'a0f9,a0fa',
40 'SVendor': None, 'SDevice': None}
41 octeontx2_npa = {'Class': '08', 'Vendor': '177d', 'Device': 'a0fb,a0fc',
42 'SVendor': None, 'SDevice': None}
43 octeontx2_dma = {'Class': '08', 'Vendor': '177d', 'Device': 'a081',
44 'SVendor': None, 'SDevice': None}
45 octeontx2_ree = {'Class': '08', 'Vendor': '177d', 'Device': 'a0f4',
46 'SVendor': None, 'SDevice': None}
48 intel_ioat_bdw = {'Class': '08', 'Vendor': '8086',
49 'Device': '6f20,6f21,6f22,6f23,6f24,6f25,6f26,6f27,6f2e,6f2f',
50 'SVendor': None, 'SDevice': None}
51 intel_ioat_skx = {'Class': '08', 'Vendor': '8086', 'Device': '2021',
52 'SVendor': None, 'SDevice': None}
53 intel_ioat_icx = {'Class': '08', 'Vendor': '8086', 'Device': '0b00',
54 'SVendor': None, 'SDevice': None}
55 intel_idxd_spr = {'Class': '08', 'Vendor': '8086', 'Device': '0b25',
56 'SVendor': None, 'SDevice': None}
57 intel_ntb_skx = {'Class': '06', 'Vendor': '8086', 'Device': '201c',
58 'SVendor': None, 'SDevice': None}
59 intel_ntb_icx = {'Class': '06', 'Vendor': '8086', 'Device': '347e',
60 'SVendor': None, 'SDevice': None}
62 network_devices = [network_class, cavium_pkx, avp_vnic, ifpga_class]
63 baseband_devices = [acceleration_class]
64 crypto_devices = [encryption_class, intel_processor_class]
65 eventdev_devices = [cavium_sso, cavium_tim, octeontx2_sso]
66 mempool_devices = [cavium_fpa, octeontx2_npa]
67 compress_devices = [cavium_zip]
68 regex_devices = [octeontx2_ree]
69 misc_devices = [intel_ioat_bdw, intel_ioat_skx, intel_ioat_icx, intel_idxd_spr,
70 intel_ntb_skx, intel_ntb_icx,
73 # global dict ethernet devices present. Dictionary indexed by PCI address.
74 # Each device within this is itself a dictionary of device properties
76 # list of supported DPDK drivers
77 dpdk_drivers = ["igb_uio", "vfio-pci", "uio_pci_generic"]
78 # list of currently loaded kernel modules
81 # command-line arg flags
87 # check if a specific kernel module is loaded
88 def module_is_loaded(module):
91 if module == 'vfio_pci':
95 return module in loaded_modules
97 # Get list of sysfs modules (both built-in and dynamically loaded)
98 sysfs_path = '/sys/module/'
100 # Get the list of directories in sysfs_path
101 sysfs_mods = [m for m in os.listdir(sysfs_path)
102 if os.path.isdir(os.path.join(sysfs_path, m))]
104 # special case for vfio_pci (module is named vfio-pci,
105 # but its .ko is named vfio_pci)
106 sysfs_mods = [a if a != 'vfio_pci' else 'vfio-pci' for a in sysfs_mods]
108 loaded_modules = sysfs_mods
110 return module in sysfs_mods
114 '''Checks that igb_uio is loaded'''
117 # list of supported modules
118 mods = [{"Name": driver, "Found": False} for driver in dpdk_drivers]
120 # first check if module is loaded
122 if module_is_loaded(mod["Name"]):
125 # check if we have at least one loaded module
126 if True not in [mod["Found"] for mod in mods] and b_flag is not None:
127 print("Warning: no supported DPDK kernel modules are loaded", file=sys.stderr)
129 # change DPDK driver list to only contain drivers that are loaded
130 dpdk_drivers = [mod["Name"] for mod in mods if mod["Found"]]
133 def has_driver(dev_id):
134 '''return true if a device is assigned to a driver. False otherwise'''
135 return "Driver_str" in devices[dev_id]
138 def get_pci_device_details(dev_id, probe_lspci):
139 '''This function gets additional details for a PCI device'''
143 extra_info = subprocess.check_output(["lspci", "-vmmks", dev_id]).splitlines()
144 # parse lspci details
145 for line in extra_info:
148 name, value = line.decode("utf8").split("\t", 1)
149 name = name.strip(":") + "_str"
151 # check for a unix interface name
152 device["Interface"] = ""
153 for base, dirs, _ in os.walk("/sys/bus/pci/devices/%s/" % dev_id):
155 device["Interface"] = \
156 ",".join(os.listdir(os.path.join(base, "net")))
158 # check if a port is used for ssh connection
159 device["Ssh_if"] = False
160 device["Active"] = ""
165 '''This function clears any old data'''
169 def get_device_details(devices_type):
170 '''This function populates the "devices" dictionary. The keys used are
171 the pci addresses (domain:bus:slot.func). The values are themselves
172 dictionaries - one for each NIC.'''
176 # first loop through and read details for all devices
177 # request machine readable format, with numeric IDs and String
179 dev_lines = subprocess.check_output(["lspci", "-Dvmmnnk"]).splitlines()
180 for dev_line in dev_lines:
182 if device_type_match(dev, devices_type):
183 # Replace "Driver" with "Driver_str" to have consistency of
184 # of dictionary key names
185 if "Driver" in dev.keys():
186 dev["Driver_str"] = dev.pop("Driver")
187 if "Module" in dev.keys():
188 dev["Module_str"] = dev.pop("Module")
189 # use dict to make copy of dev
190 devices[dev["Slot"]] = dict(dev)
191 # Clear previous device's data
194 name, value = dev_line.decode("utf8").split("\t", 1)
195 value_list = value.rsplit(' ', 1)
197 # String stored in <name>_str
198 dev[name.rstrip(":") + '_str'] = value_list[0]
200 dev[name.rstrip(":")] = value_list[len(value_list) - 1] \
201 .rstrip("]").lstrip("[")
203 if devices_type == network_devices:
204 # check what is the interface if any for an ssh connection if
205 # any to this host, so we can mark it later.
207 route = subprocess.check_output(["ip", "-o", "route"])
208 # filter out all lines for 169.254 routes
209 route = "\n".join(filter(lambda ln: not ln.startswith("169.254"),
210 route.decode().splitlines()))
211 rt_info = route.split()
212 for i in range(len(rt_info) - 1):
213 if rt_info[i] == "dev":
214 ssh_if.append(rt_info[i+1])
216 # based on the basic info, get extended text details
217 for d in devices.keys():
218 if not device_type_match(devices[d], devices_type):
221 # get additional info and add it to existing data
222 devices[d] = devices[d].copy()
223 # No need to probe lspci
224 devices[d].update(get_pci_device_details(d, False).items())
226 if devices_type == network_devices:
228 if _if in devices[d]["Interface"].split(","):
229 devices[d]["Ssh_if"] = True
230 devices[d]["Active"] = "*Active*"
233 # add igb_uio to list of supporting modules if needed
234 if "Module_str" in devices[d]:
235 for driver in dpdk_drivers:
236 if driver not in devices[d]["Module_str"]:
237 devices[d]["Module_str"] = \
238 devices[d]["Module_str"] + ",%s" % driver
240 devices[d]["Module_str"] = ",".join(dpdk_drivers)
242 # make sure the driver and module strings do not have any duplicates
244 modules = devices[d]["Module_str"].split(",")
245 if devices[d]["Driver_str"] in modules:
246 modules.remove(devices[d]["Driver_str"])
247 devices[d]["Module_str"] = ",".join(modules)
250 def device_type_match(dev, devices_type):
251 for i in range(len(devices_type)):
253 [x for x in devices_type[i].values() if x is not None])
255 if dev["Class"][0:2] == devices_type[i]["Class"]:
256 match_count = match_count + 1
257 for key in devices_type[i].keys():
258 if key != 'Class' and devices_type[i][key]:
259 value_list = devices_type[i][key].split(',')
260 for value in value_list:
261 if value.strip(' ') == dev[key]:
262 match_count = match_count + 1
263 # count must be the number of non None parameters to match
264 if match_count == param_count:
268 def dev_id_from_dev_name(dev_name):
269 '''Take a device "name" - a string passed in by user to identify a NIC
270 device, and determine the device id - i.e. the domain:bus:slot.func - for
271 it, which can then be used to index into the devices array'''
273 # check if it's already a suitable index
274 if dev_name in devices:
276 # check if it's an index just missing the domain part
277 if "0000:" + dev_name in devices:
278 return "0000:" + dev_name
280 # check if it's an interface name, e.g. eth1
281 for d in devices.keys():
282 if dev_name in devices[d]["Interface"].split(","):
283 return devices[d]["Slot"]
284 # if nothing else matches - error
285 raise ValueError("Unknown device: %s. "
286 "Please specify device in \"bus:slot.func\" format" % dev_name)
289 def unbind_one(dev_id, force):
290 '''Unbind the device identified by "dev_id" from its current driver'''
291 dev = devices[dev_id]
292 if not has_driver(dev_id):
293 print("Notice: %s %s %s is not currently managed by any driver" %
294 (dev["Slot"], dev["Device_str"], dev["Interface"]), file=sys.stderr)
297 # prevent us disconnecting ourselves
298 if dev["Ssh_if"] and not force:
299 print("Warning: routing table indicates that interface %s is active. "
300 "Skipping unbind" % dev_id, file=sys.stderr)
303 # write to /sys to unbind
304 filename = "/sys/bus/pci/drivers/%s/unbind" % dev["Driver_str"]
306 f = open(filename, "a")
308 sys.exit("Error: unbind failed for %s - Cannot open %s" %
314 def bind_one(dev_id, driver, force):
315 '''Bind the device given by "dev_id" to the driver "driver". If the device
316 is already bound to a different driver, it will be unbound first'''
317 dev = devices[dev_id]
318 saved_driver = None # used to rollback any unbind in case of failure
320 # prevent disconnection of our ssh session
321 if dev["Ssh_if"] and not force:
322 print("Warning: routing table indicates that interface %s is active. "
323 "Not modifying" % dev_id, file=sys.stderr)
326 # unbind any existing drivers we don't want
327 if has_driver(dev_id):
328 if dev["Driver_str"] == driver:
329 print("Notice: %s already bound to driver %s, skipping" %
330 (dev_id, driver), file=sys.stderr)
332 saved_driver = dev["Driver_str"]
333 unbind_one(dev_id, force)
334 dev["Driver_str"] = "" # clear driver string
336 # For kernels >= 3.15 driver_override can be used to specify the driver
337 # for a device rather than relying on the driver to provide a positive
338 # match of the device. The existing process of looking up
339 # the vendor and device ID, adding them to the driver new_id,
340 # will erroneously bind other devices too which has the additional burden
341 # of unbinding those devices
342 if driver in dpdk_drivers:
343 filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id
346 f = open(filename, "w")
348 print("Error: bind failed for %s - Cannot open %s"
349 % (dev_id, filename), file=sys.stderr)
352 f.write("%s" % driver)
355 print("Error: bind failed for %s - Cannot write driver %s to "
356 "PCI ID " % (dev_id, driver), file=sys.stderr)
358 # For kernels < 3.15 use new_id to add PCI id's to the driver
360 filename = "/sys/bus/pci/drivers/%s/new_id" % driver
362 f = open(filename, "w")
364 print("Error: bind failed for %s - Cannot open %s"
365 % (dev_id, filename), file=sys.stderr)
368 # Convert Device and Vendor Id to int to write to new_id
369 f.write("%04x %04x" % (int(dev["Vendor"], 16),
370 int(dev["Device"], 16)))
373 print("Error: bind failed for %s - Cannot write new PCI ID to "
374 "driver %s" % (dev_id, driver), file=sys.stderr)
377 # do the bind by writing to /sys
378 filename = "/sys/bus/pci/drivers/%s/bind" % driver
380 f = open(filename, "a")
382 print("Error: bind failed for %s - Cannot open %s"
383 % (dev_id, filename), file=sys.stderr)
384 if saved_driver is not None: # restore any previous driver
385 bind_one(dev_id, saved_driver, force)
391 # for some reason, closing dev_id after adding a new PCI ID to new_id
392 # results in IOError. however, if the device was successfully bound,
393 # we don't care for any errors and can safely ignore IOError
394 tmp = get_pci_device_details(dev_id, True)
395 if "Driver_str" in tmp and tmp["Driver_str"] == driver:
397 print("Error: bind failed for %s - Cannot bind to driver %s"
398 % (dev_id, driver), file=sys.stderr)
399 if saved_driver is not None: # restore any previous driver
400 bind_one(dev_id, saved_driver, force)
403 # For kernels > 3.15 driver_override is used to bind a device to a driver.
404 # Before unbinding it, overwrite driver_override with empty string so that
405 # the device can be bound to any other driver
406 filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id
409 f = open(filename, "w")
411 sys.exit("Error: unbind failed for %s - Cannot open %s"
412 % (dev_id, filename))
417 sys.exit("Error: unbind failed for %s - Cannot open %s"
418 % (dev_id, filename))
421 def unbind_all(dev_list, force=False):
422 """Unbind method, takes a list of device locations"""
424 if dev_list[0] == "dpdk":
425 for d in devices.keys():
426 if "Driver_str" in devices[d]:
427 if devices[d]["Driver_str"] in dpdk_drivers:
428 unbind_one(devices[d]["Slot"], force)
432 dev_list = map(dev_id_from_dev_name, dev_list)
433 except ValueError as ex:
441 def bind_all(dev_list, driver, force=False):
442 """Bind method, takes a list of device locations"""
445 # a common user error is to forget to specify the driver the devices need to
446 # be bound to. check if the driver is a valid device, and if it is, show
447 # a meaningful error.
449 dev_id_from_dev_name(driver)
450 # if we've made it this far, this means that the "driver" was a valid
451 # device string, so it's probably not a valid driver name.
452 sys.exit("Error: Driver '%s' does not look like a valid driver. " \
453 "Did you forget to specify the driver to bind devices to?" % driver)
455 # driver generated error - it's not a valid device ID, so all is well
458 # check if we're attempting to bind to a driver that isn't loaded
459 if not module_is_loaded(driver.replace('-', '_')):
460 sys.exit("Error: Driver '%s' is not loaded." % driver)
463 dev_list = map(dev_id_from_dev_name, dev_list)
464 except ValueError as ex:
468 bind_one(d, driver, force)
470 # For kernels < 3.15 when binding devices to a generic driver
471 # (i.e. one that doesn't have a PCI ID table) using new_id, some devices
472 # that are not bound to any other driver could be bound even if no one has
473 # asked them to. hence, we check the list of drivers again, and see if
474 # some of the previously-unbound devices were erroneously bound.
475 if not exists("/sys/bus/pci/devices/%s/driver_override" % d):
476 for d in devices.keys():
477 # skip devices that were already bound or that we know should be bound
478 if "Driver_str" in devices[d] or d in dev_list:
481 # update information about this device
482 devices[d] = dict(devices[d].items() +
483 get_pci_device_details(d, True).items())
485 # check if updated information indicates that the device was bound
486 if "Driver_str" in devices[d]:
490 def display_devices(title, dev_list, extra_params=None):
491 '''Displays to the user the details of a list of devices given in
492 "dev_list". The "extra_params" parameter, if given, should contain a string
493 with %()s fields in it for replacement by the named fields in each
494 device's dictionary.'''
495 strings = [] # this holds the strings to print. We sort before printing
496 print("\n%s" % title)
497 print("="*len(title))
499 strings.append("<none>")
502 if extra_params is not None:
503 strings.append("%s '%s %s' %s" % (dev["Slot"],
508 strings.append("%s '%s'" % (dev["Slot"], dev["Device_str"]))
509 # sort before printing, so that the entries appear in PCI order
511 print("\n".join(strings)) # print one per line
513 def show_device_status(devices_type, device_name, if_field=False):
519 # split our list of network devices into the three categories above
520 for d in devices.keys():
521 if device_type_match(devices[d], devices_type):
522 if not has_driver(d):
523 no_drv.append(devices[d])
525 if devices[d]["Driver_str"] in dpdk_drivers:
526 dpdk_drv.append(devices[d])
528 kernel_drv.append(devices[d])
530 n_devs = len(dpdk_drv) + len(kernel_drv) + len(no_drv)
532 # don't bother displaying anything if there are no devices
534 msg = "No '%s' devices detected" % device_name
537 print("".join('=' * len(msg)))
540 # print each category separately, so we can clearly see what's used by DPDK
542 display_devices("%s devices using DPDK-compatible driver" % device_name,
543 dpdk_drv, "drv=%(Driver_str)s unused=%(Module_str)s")
547 if_text = "if=%(Interface)s "
548 display_devices("%s devices using kernel driver" % device_name, kernel_drv,
549 if_text + "drv=%(Driver_str)s "
550 "unused=%(Module_str)s %(Active)s")
552 display_devices("Other %s devices" % device_name, no_drv,
553 "unused=%(Module_str)s")
556 '''Function called when the script is passed the "--status" option.
557 Displays to the user what devices are bound to the igb_uio driver, the
558 kernel driver or to no driver'''
560 if status_dev in ["net", "all"]:
561 show_device_status(network_devices, "Network", if_field=True)
563 if status_dev in ["baseband", "all"]:
564 show_device_status(baseband_devices, "Baseband")
566 if status_dev in ["crypto", "all"]:
567 show_device_status(crypto_devices, "Crypto")
569 if status_dev in ["event", "all"]:
570 show_device_status(eventdev_devices, "Eventdev")
572 if status_dev in ["mempool", "all"]:
573 show_device_status(mempool_devices, "Mempool")
575 if status_dev in ["compress", "all"]:
576 show_device_status(compress_devices, "Compress")
578 if status_dev in ["misc", "all"]:
579 show_device_status(misc_devices, "Misc (rawdev)")
581 if status_dev in ["regex", "all"]:
582 show_device_status(regex_devices, "Regex")
586 '''Returns a list containing either:
587 * List of PCI B:D:F matching arg, using shell wildcards e.g. 80:04.*
588 * Only the passed arg if matching list is empty'''
589 sysfs_path = "/sys/bus/pci/devices"
590 for _glob in [arg, '0000:' + arg]:
591 paths = [basename(path) for path in glob(path_join(sysfs_path, _glob))]
598 '''Parses the command-line arguments given by the user and takes the
599 appropriate action for each'''
606 parser = argparse.ArgumentParser(
607 description='Utility to bind and unbind devices from Linux kernel',
608 formatter_class=argparse.RawDescriptionHelpFormatter,
613 To display current device status:
616 To display current network device status:
617 %(prog)s --status-dev net
619 To bind eth1 from the current driver and move to use vfio-pci
620 %(prog)s --bind=vfio-pci eth1
622 To unbind 0000:01:00.0 from using any driver
623 %(prog)s -u 0000:01:00.0
625 To bind 0000:02:00.0 and 0000:02:00.1 to the ixgbe kernel driver
626 %(prog)s -b ixgbe 02:00.0 02:00.1
633 help="Print the current status of all known devices.")
636 help="Print the status of given device group.",
637 choices=['baseband', 'compress', 'crypto', 'event',
638 'mempool', 'misc', 'net', 'regex'])
639 bind_group = parser.add_mutually_exclusive_group()
640 bind_group.add_argument(
644 help="Select the driver to use or \"none\" to unbind the device")
645 bind_group.add_argument(
649 help="Unbind a device (equivalent to \"-b none\")")
654 Override restriction on binding devices in use by Linux"
655 WARNING: This can lead to loss of network connection and should be used with caution.
662 Device specified as PCI "domain:bus:slot.func" syntax or "bus:slot.func" syntax.
663 For devices bound to Linux kernel drivers, they may be referred to by interface name.
666 opt = parser.parse_args()
670 status_dev = opt.status_dev
682 if not b_flag and not status_flag:
683 print("Error: No action specified for devices. "
684 "Please give a --bind, --ubind or --status option",
689 if b_flag and not args:
690 print("Error: No devices specified.", file=sys.stderr)
694 # resolve any PCI globs in the args
697 new_args.extend(pci_glob(arg))
700 def do_arg_actions():
701 '''do the actual action requested by the user'''
707 if b_flag in ["none", "None"]:
708 unbind_all(args, force_flag)
709 elif b_flag is not None:
710 bind_all(args, b_flag, force_flag)
712 if b_flag is not None:
714 # refresh if we have changed anything
715 get_device_details(network_devices)
716 get_device_details(baseband_devices)
717 get_device_details(crypto_devices)
718 get_device_details(eventdev_devices)
719 get_device_details(mempool_devices)
720 get_device_details(compress_devices)
721 get_device_details(regex_devices)
722 get_device_details(misc_devices)
727 '''program main function'''
728 # check if lspci is installed, suppress any output
729 with open(os.devnull, 'w') as devnull:
730 ret = subprocess.call(['which', 'lspci'],
731 stdout=devnull, stderr=devnull)
733 sys.exit("'lspci' not found - please install 'pciutils'")
737 get_device_details(network_devices)
738 get_device_details(baseband_devices)
739 get_device_details(crypto_devices)
740 get_device_details(eventdev_devices)
741 get_device_details(mempool_devices)
742 get_device_details(compress_devices)
743 get_device_details(regex_devices)
744 get_device_details(misc_devices)
747 if __name__ == "__main__":