2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright(c) 2010-2014 Intel Corporation
13 from os.path import exists, basename
14 from os.path import join as path_join
16 # The PCI base class for all devices
17 network_class = {'Class': '02', 'Vendor': None, 'Device': None,
18 'SVendor': None, 'SDevice': None}
19 acceleration_class = {'Class': '12', 'Vendor': None, 'Device': None,
20 'SVendor': None, 'SDevice': None}
21 ifpga_class = {'Class': '12', 'Vendor': '8086', 'Device': '0b30',
22 'SVendor': None, 'SDevice': None}
23 encryption_class = {'Class': '10', 'Vendor': None, 'Device': None,
24 'SVendor': None, 'SDevice': None}
25 intel_processor_class = {'Class': '0b', 'Vendor': '8086', 'Device': None,
26 'SVendor': None, 'SDevice': None}
27 cavium_sso = {'Class': '08', 'Vendor': '177d', 'Device': 'a04b,a04d',
28 'SVendor': None, 'SDevice': None}
29 cavium_fpa = {'Class': '08', 'Vendor': '177d', 'Device': 'a053',
30 'SVendor': None, 'SDevice': None}
31 cavium_pkx = {'Class': '08', 'Vendor': '177d', 'Device': 'a0dd,a049',
32 'SVendor': None, 'SDevice': None}
33 cavium_tim = {'Class': '08', 'Vendor': '177d', 'Device': 'a051',
34 'SVendor': None, 'SDevice': None}
35 cavium_zip = {'Class': '12', 'Vendor': '177d', 'Device': 'a037',
36 'SVendor': None, 'SDevice': None}
37 avp_vnic = {'Class': '05', 'Vendor': '1af4', 'Device': '1110',
38 'SVendor': None, 'SDevice': None}
40 octeontx2_sso = {'Class': '08', 'Vendor': '177d', 'Device': 'a0f9,a0fa',
41 'SVendor': None, 'SDevice': None}
42 octeontx2_npa = {'Class': '08', 'Vendor': '177d', 'Device': 'a0fb,a0fc',
43 'SVendor': None, 'SDevice': None}
44 octeontx2_dma = {'Class': '08', 'Vendor': '177d', 'Device': 'a081',
45 'SVendor': None, 'SDevice': None}
46 octeontx2_ree = {'Class': '08', 'Vendor': '177d', 'Device': 'a0f4',
47 'SVendor': None, 'SDevice': None}
49 intel_ioat_bdw = {'Class': '08', 'Vendor': '8086',
50 'Device': '6f20,6f21,6f22,6f23,6f24,6f25,6f26,6f27,6f2e,6f2f',
51 'SVendor': None, 'SDevice': None}
52 intel_ioat_skx = {'Class': '08', 'Vendor': '8086', 'Device': '2021',
53 'SVendor': None, 'SDevice': None}
54 intel_ioat_icx = {'Class': '08', 'Vendor': '8086', 'Device': '0b00',
55 'SVendor': None, 'SDevice': None}
56 intel_idxd_spr = {'Class': '08', 'Vendor': '8086', 'Device': '0b25',
57 'SVendor': None, 'SDevice': None}
58 intel_ntb_skx = {'Class': '06', 'Vendor': '8086', 'Device': '201c',
59 'SVendor': None, 'SDevice': None}
60 intel_ntb_icx = {'Class': '06', 'Vendor': '8086', 'Device': '347e',
61 'SVendor': None, 'SDevice': None}
63 network_devices = [network_class, cavium_pkx, avp_vnic, ifpga_class]
64 baseband_devices = [acceleration_class]
65 crypto_devices = [encryption_class, intel_processor_class]
66 eventdev_devices = [cavium_sso, cavium_tim, octeontx2_sso]
67 mempool_devices = [cavium_fpa, octeontx2_npa]
68 compress_devices = [cavium_zip]
69 regex_devices = [octeontx2_ree]
70 misc_devices = [intel_ioat_bdw, intel_ioat_skx, intel_ioat_icx, intel_idxd_spr,
71 intel_ntb_skx, intel_ntb_icx,
74 # global dict ethernet devices present. Dictionary indexed by PCI address.
75 # Each device within this is itself a dictionary of device properties
77 # list of supported DPDK drivers
78 dpdk_drivers = ["igb_uio", "vfio-pci", "uio_pci_generic"]
79 # list of currently loaded kernel modules
82 # command-line arg flags
88 # check if a specific kernel module is loaded
89 def module_is_loaded(module):
92 if module == 'vfio_pci':
96 return module in loaded_modules
98 # Get list of sysfs modules (both built-in and dynamically loaded)
99 sysfs_path = '/sys/module/'
101 # Get the list of directories in sysfs_path
102 sysfs_mods = [m for m in os.listdir(sysfs_path)
103 if os.path.isdir(os.path.join(sysfs_path, m))]
105 # special case for vfio_pci (module is named vfio-pci,
106 # but its .ko is named vfio_pci)
107 sysfs_mods = [a if a != 'vfio_pci' else 'vfio-pci' for a in sysfs_mods]
109 loaded_modules = sysfs_mods
111 # add built-in modules as loaded
112 release = platform.uname().release
113 filename = os.path.join("/lib/modules/", release, "modules.builtin")
114 if os.path.exists(filename):
116 with open(filename) as f:
117 loaded_modules += [os.path.splitext(os.path.basename(mod))[0] for mod in f]
119 print("Warning: cannot read list of built-in kernel modules")
121 return module in loaded_modules
125 '''Checks that igb_uio is loaded'''
128 # list of supported modules
129 mods = [{"Name": driver, "Found": False} for driver in dpdk_drivers]
131 # first check if module is loaded
133 if module_is_loaded(mod["Name"]):
136 # check if we have at least one loaded module
137 if True not in [mod["Found"] for mod in mods] and b_flag is not None:
138 print("Warning: no supported DPDK kernel modules are loaded", file=sys.stderr)
140 # change DPDK driver list to only contain drivers that are loaded
141 dpdk_drivers = [mod["Name"] for mod in mods if mod["Found"]]
144 def has_driver(dev_id):
145 '''return true if a device is assigned to a driver. False otherwise'''
146 return "Driver_str" in devices[dev_id]
149 def get_pci_device_details(dev_id, probe_lspci):
150 '''This function gets additional details for a PCI device'''
154 extra_info = subprocess.check_output(["lspci", "-vmmks", dev_id]).splitlines()
155 # parse lspci details
156 for line in extra_info:
159 name, value = line.decode("utf8").split("\t", 1)
160 name = name.strip(":") + "_str"
162 # check for a unix interface name
163 device["Interface"] = ""
164 for base, dirs, _ in os.walk("/sys/bus/pci/devices/%s/" % dev_id):
166 device["Interface"] = \
167 ",".join(os.listdir(os.path.join(base, "net")))
169 # check if a port is used for ssh connection
170 device["Ssh_if"] = False
171 device["Active"] = ""
176 '''This function clears any old data'''
180 def get_device_details(devices_type):
181 '''This function populates the "devices" dictionary. The keys used are
182 the pci addresses (domain:bus:slot.func). The values are themselves
183 dictionaries - one for each NIC.'''
187 # first loop through and read details for all devices
188 # request machine readable format, with numeric IDs and String
190 dev_lines = subprocess.check_output(["lspci", "-Dvmmnnk"]).splitlines()
191 for dev_line in dev_lines:
193 if device_type_match(dev, devices_type):
194 # Replace "Driver" with "Driver_str" to have consistency of
195 # of dictionary key names
196 if "Driver" in dev.keys():
197 dev["Driver_str"] = dev.pop("Driver")
198 if "Module" in dev.keys():
199 dev["Module_str"] = dev.pop("Module")
200 # use dict to make copy of dev
201 devices[dev["Slot"]] = dict(dev)
202 # Clear previous device's data
205 name, value = dev_line.decode("utf8").split("\t", 1)
206 value_list = value.rsplit(' ', 1)
208 # String stored in <name>_str
209 dev[name.rstrip(":") + '_str'] = value_list[0]
211 dev[name.rstrip(":")] = value_list[len(value_list) - 1] \
212 .rstrip("]").lstrip("[")
214 if devices_type == network_devices:
215 # check what is the interface if any for an ssh connection if
216 # any to this host, so we can mark it later.
218 route = subprocess.check_output(["ip", "-o", "route"])
219 # filter out all lines for 169.254 routes
220 route = "\n".join(filter(lambda ln: not ln.startswith("169.254"),
221 route.decode().splitlines()))
222 rt_info = route.split()
223 for i in range(len(rt_info) - 1):
224 if rt_info[i] == "dev":
225 ssh_if.append(rt_info[i+1])
227 # based on the basic info, get extended text details
228 for d in devices.keys():
229 if not device_type_match(devices[d], devices_type):
232 # get additional info and add it to existing data
233 devices[d] = devices[d].copy()
234 # No need to probe lspci
235 devices[d].update(get_pci_device_details(d, False).items())
237 if devices_type == network_devices:
239 if _if in devices[d]["Interface"].split(","):
240 devices[d]["Ssh_if"] = True
241 devices[d]["Active"] = "*Active*"
244 # add igb_uio to list of supporting modules if needed
245 if "Module_str" in devices[d]:
246 for driver in dpdk_drivers:
247 if driver not in devices[d]["Module_str"]:
248 devices[d]["Module_str"] = \
249 devices[d]["Module_str"] + ",%s" % driver
251 devices[d]["Module_str"] = ",".join(dpdk_drivers)
253 # make sure the driver and module strings do not have any duplicates
255 modules = devices[d]["Module_str"].split(",")
256 if devices[d]["Driver_str"] in modules:
257 modules.remove(devices[d]["Driver_str"])
258 devices[d]["Module_str"] = ",".join(modules)
261 def device_type_match(dev, devices_type):
262 for i in range(len(devices_type)):
264 [x for x in devices_type[i].values() if x is not None])
266 if dev["Class"][0:2] == devices_type[i]["Class"]:
267 match_count = match_count + 1
268 for key in devices_type[i].keys():
269 if key != 'Class' and devices_type[i][key]:
270 value_list = devices_type[i][key].split(',')
271 for value in value_list:
272 if value.strip(' ') == dev[key]:
273 match_count = match_count + 1
274 # count must be the number of non None parameters to match
275 if match_count == param_count:
279 def dev_id_from_dev_name(dev_name):
280 '''Take a device "name" - a string passed in by user to identify a NIC
281 device, and determine the device id - i.e. the domain:bus:slot.func - for
282 it, which can then be used to index into the devices array'''
284 # check if it's already a suitable index
285 if dev_name in devices:
287 # check if it's an index just missing the domain part
288 if "0000:" + dev_name in devices:
289 return "0000:" + dev_name
291 # check if it's an interface name, e.g. eth1
292 for d in devices.keys():
293 if dev_name in devices[d]["Interface"].split(","):
294 return devices[d]["Slot"]
295 # if nothing else matches - error
296 raise ValueError("Unknown device: %s. "
297 "Please specify device in \"bus:slot.func\" format" % dev_name)
300 def unbind_one(dev_id, force):
301 '''Unbind the device identified by "dev_id" from its current driver'''
302 dev = devices[dev_id]
303 if not has_driver(dev_id):
304 print("Notice: %s %s %s is not currently managed by any driver" %
305 (dev["Slot"], dev["Device_str"], dev["Interface"]), file=sys.stderr)
308 # prevent us disconnecting ourselves
309 if dev["Ssh_if"] and not force:
310 print("Warning: routing table indicates that interface %s is active. "
311 "Skipping unbind" % dev_id, file=sys.stderr)
314 # write to /sys to unbind
315 filename = "/sys/bus/pci/drivers/%s/unbind" % dev["Driver_str"]
317 f = open(filename, "a")
319 sys.exit("Error: unbind failed for %s - Cannot open %s" %
325 def bind_one(dev_id, driver, force):
326 '''Bind the device given by "dev_id" to the driver "driver". If the device
327 is already bound to a different driver, it will be unbound first'''
328 dev = devices[dev_id]
329 saved_driver = None # used to rollback any unbind in case of failure
331 # prevent disconnection of our ssh session
332 if dev["Ssh_if"] and not force:
333 print("Warning: routing table indicates that interface %s is active. "
334 "Not modifying" % dev_id, file=sys.stderr)
337 # unbind any existing drivers we don't want
338 if has_driver(dev_id):
339 if dev["Driver_str"] == driver:
340 print("Notice: %s already bound to driver %s, skipping" %
341 (dev_id, driver), file=sys.stderr)
343 saved_driver = dev["Driver_str"]
344 unbind_one(dev_id, force)
345 dev["Driver_str"] = "" # clear driver string
347 # For kernels >= 3.15 driver_override can be used to specify the driver
348 # for a device rather than relying on the driver to provide a positive
349 # match of the device. The existing process of looking up
350 # the vendor and device ID, adding them to the driver new_id,
351 # will erroneously bind other devices too which has the additional burden
352 # of unbinding those devices
353 if driver in dpdk_drivers:
354 filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id
357 f = open(filename, "w")
359 print("Error: bind failed for %s - Cannot open %s"
360 % (dev_id, filename), file=sys.stderr)
363 f.write("%s" % driver)
366 print("Error: bind failed for %s - Cannot write driver %s to "
367 "PCI ID " % (dev_id, driver), file=sys.stderr)
369 # For kernels < 3.15 use new_id to add PCI id's to the driver
371 filename = "/sys/bus/pci/drivers/%s/new_id" % driver
373 f = open(filename, "w")
375 print("Error: bind failed for %s - Cannot open %s"
376 % (dev_id, filename), file=sys.stderr)
379 # Convert Device and Vendor Id to int to write to new_id
380 f.write("%04x %04x" % (int(dev["Vendor"], 16),
381 int(dev["Device"], 16)))
384 print("Error: bind failed for %s - Cannot write new PCI ID to "
385 "driver %s" % (dev_id, driver), file=sys.stderr)
388 # do the bind by writing to /sys
389 filename = "/sys/bus/pci/drivers/%s/bind" % driver
391 f = open(filename, "a")
393 print("Error: bind failed for %s - Cannot open %s"
394 % (dev_id, filename), file=sys.stderr)
395 if saved_driver is not None: # restore any previous driver
396 bind_one(dev_id, saved_driver, force)
402 # for some reason, closing dev_id after adding a new PCI ID to new_id
403 # results in IOError. however, if the device was successfully bound,
404 # we don't care for any errors and can safely ignore IOError
405 tmp = get_pci_device_details(dev_id, True)
406 if "Driver_str" in tmp and tmp["Driver_str"] == driver:
408 print("Error: bind failed for %s - Cannot bind to driver %s"
409 % (dev_id, driver), file=sys.stderr)
410 if saved_driver is not None: # restore any previous driver
411 bind_one(dev_id, saved_driver, force)
414 # For kernels > 3.15 driver_override is used to bind a device to a driver.
415 # Before unbinding it, overwrite driver_override with empty string so that
416 # the device can be bound to any other driver
417 filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id
420 f = open(filename, "w")
422 sys.exit("Error: unbind failed for %s - Cannot open %s"
423 % (dev_id, filename))
428 sys.exit("Error: unbind failed for %s - Cannot open %s"
429 % (dev_id, filename))
432 def unbind_all(dev_list, force=False):
433 """Unbind method, takes a list of device locations"""
435 if dev_list[0] == "dpdk":
436 for d in devices.keys():
437 if "Driver_str" in devices[d]:
438 if devices[d]["Driver_str"] in dpdk_drivers:
439 unbind_one(devices[d]["Slot"], force)
443 dev_list = map(dev_id_from_dev_name, dev_list)
444 except ValueError as ex:
452 def bind_all(dev_list, driver, force=False):
453 """Bind method, takes a list of device locations"""
456 # a common user error is to forget to specify the driver the devices need to
457 # be bound to. check if the driver is a valid device, and if it is, show
458 # a meaningful error.
460 dev_id_from_dev_name(driver)
461 # if we've made it this far, this means that the "driver" was a valid
462 # device string, so it's probably not a valid driver name.
463 sys.exit("Error: Driver '%s' does not look like a valid driver. " \
464 "Did you forget to specify the driver to bind devices to?" % driver)
466 # driver generated error - it's not a valid device ID, so all is well
469 # check if we're attempting to bind to a driver that isn't loaded
470 if not module_is_loaded(driver.replace('-', '_')):
471 sys.exit("Error: Driver '%s' is not loaded." % driver)
474 dev_list = map(dev_id_from_dev_name, dev_list)
475 except ValueError as ex:
479 bind_one(d, driver, force)
481 # For kernels < 3.15 when binding devices to a generic driver
482 # (i.e. one that doesn't have a PCI ID table) using new_id, some devices
483 # that are not bound to any other driver could be bound even if no one has
484 # asked them to. hence, we check the list of drivers again, and see if
485 # some of the previously-unbound devices were erroneously bound.
486 if not exists("/sys/bus/pci/devices/%s/driver_override" % d):
487 for d in devices.keys():
488 # skip devices that were already bound or that we know should be bound
489 if "Driver_str" in devices[d] or d in dev_list:
492 # update information about this device
493 devices[d] = dict(devices[d].items() +
494 get_pci_device_details(d, True).items())
496 # check if updated information indicates that the device was bound
497 if "Driver_str" in devices[d]:
501 def display_devices(title, dev_list, extra_params=None):
502 '''Displays to the user the details of a list of devices given in
503 "dev_list". The "extra_params" parameter, if given, should contain a string
504 with %()s fields in it for replacement by the named fields in each
505 device's dictionary.'''
506 strings = [] # this holds the strings to print. We sort before printing
507 print("\n%s" % title)
508 print("="*len(title))
510 strings.append("<none>")
513 if extra_params is not None:
514 strings.append("%s '%s %s' %s" % (dev["Slot"],
519 strings.append("%s '%s'" % (dev["Slot"], dev["Device_str"]))
520 # sort before printing, so that the entries appear in PCI order
522 print("\n".join(strings)) # print one per line
524 def show_device_status(devices_type, device_name, if_field=False):
530 # split our list of network devices into the three categories above
531 for d in devices.keys():
532 if device_type_match(devices[d], devices_type):
533 if not has_driver(d):
534 no_drv.append(devices[d])
536 if devices[d]["Driver_str"] in dpdk_drivers:
537 dpdk_drv.append(devices[d])
539 kernel_drv.append(devices[d])
541 n_devs = len(dpdk_drv) + len(kernel_drv) + len(no_drv)
543 # don't bother displaying anything if there are no devices
545 msg = "No '%s' devices detected" % device_name
548 print("".join('=' * len(msg)))
551 # print each category separately, so we can clearly see what's used by DPDK
553 display_devices("%s devices using DPDK-compatible driver" % device_name,
554 dpdk_drv, "drv=%(Driver_str)s unused=%(Module_str)s")
558 if_text = "if=%(Interface)s "
559 display_devices("%s devices using kernel driver" % device_name, kernel_drv,
560 if_text + "drv=%(Driver_str)s "
561 "unused=%(Module_str)s %(Active)s")
563 display_devices("Other %s devices" % device_name, no_drv,
564 "unused=%(Module_str)s")
567 '''Function called when the script is passed the "--status" option.
568 Displays to the user what devices are bound to the igb_uio driver, the
569 kernel driver or to no driver'''
571 if status_dev in ["net", "all"]:
572 show_device_status(network_devices, "Network", if_field=True)
574 if status_dev in ["baseband", "all"]:
575 show_device_status(baseband_devices, "Baseband")
577 if status_dev in ["crypto", "all"]:
578 show_device_status(crypto_devices, "Crypto")
580 if status_dev in ["event", "all"]:
581 show_device_status(eventdev_devices, "Eventdev")
583 if status_dev in ["mempool", "all"]:
584 show_device_status(mempool_devices, "Mempool")
586 if status_dev in ["compress", "all"]:
587 show_device_status(compress_devices, "Compress")
589 if status_dev in ["misc", "all"]:
590 show_device_status(misc_devices, "Misc (rawdev)")
592 if status_dev in ["regex", "all"]:
593 show_device_status(regex_devices, "Regex")
597 '''Returns a list containing either:
598 * List of PCI B:D:F matching arg, using shell wildcards e.g. 80:04.*
599 * Only the passed arg if matching list is empty'''
600 sysfs_path = "/sys/bus/pci/devices"
601 for _glob in [arg, '0000:' + arg]:
602 paths = [basename(path) for path in glob(path_join(sysfs_path, _glob))]
609 '''Parses the command-line arguments given by the user and takes the
610 appropriate action for each'''
617 parser = argparse.ArgumentParser(
618 description='Utility to bind and unbind devices from Linux kernel',
619 formatter_class=argparse.RawDescriptionHelpFormatter,
624 To display current device status:
627 To display current network device status:
628 %(prog)s --status-dev net
630 To bind eth1 from the current driver and move to use vfio-pci
631 %(prog)s --bind=vfio-pci eth1
633 To unbind 0000:01:00.0 from using any driver
634 %(prog)s -u 0000:01:00.0
636 To bind 0000:02:00.0 and 0000:02:00.1 to the ixgbe kernel driver
637 %(prog)s -b ixgbe 02:00.0 02:00.1
644 help="Print the current status of all known devices.")
647 help="Print the status of given device group.",
648 choices=['baseband', 'compress', 'crypto', 'event',
649 'mempool', 'misc', 'net', 'regex'])
650 bind_group = parser.add_mutually_exclusive_group()
651 bind_group.add_argument(
655 help="Select the driver to use or \"none\" to unbind the device")
656 bind_group.add_argument(
660 help="Unbind a device (equivalent to \"-b none\")")
665 Override restriction on binding devices in use by Linux"
666 WARNING: This can lead to loss of network connection and should be used with caution.
673 Device specified as PCI "domain:bus:slot.func" syntax or "bus:slot.func" syntax.
674 For devices bound to Linux kernel drivers, they may be referred to by interface name.
677 opt = parser.parse_args()
681 status_dev = opt.status_dev
693 if not b_flag and not status_flag:
694 print("Error: No action specified for devices. "
695 "Please give a --bind, --ubind or --status option",
700 if b_flag and not args:
701 print("Error: No devices specified.", file=sys.stderr)
705 # resolve any PCI globs in the args
708 new_args.extend(pci_glob(arg))
711 def do_arg_actions():
712 '''do the actual action requested by the user'''
718 if b_flag in ["none", "None"]:
719 unbind_all(args, force_flag)
720 elif b_flag is not None:
721 bind_all(args, b_flag, force_flag)
723 if b_flag is not None:
725 # refresh if we have changed anything
726 get_device_details(network_devices)
727 get_device_details(baseband_devices)
728 get_device_details(crypto_devices)
729 get_device_details(eventdev_devices)
730 get_device_details(mempool_devices)
731 get_device_details(compress_devices)
732 get_device_details(regex_devices)
733 get_device_details(misc_devices)
738 '''program main function'''
739 # check if lspci is installed, suppress any output
740 with open(os.devnull, 'w') as devnull:
741 ret = subprocess.call(['which', 'lspci'],
742 stdout=devnull, stderr=devnull)
744 sys.exit("'lspci' not found - please install 'pciutils'")
748 get_device_details(network_devices)
749 get_device_details(baseband_devices)
750 get_device_details(crypto_devices)
751 get_device_details(eventdev_devices)
752 get_device_details(mempool_devices)
753 get_device_details(compress_devices)
754 get_device_details(regex_devices)
755 get_device_details(misc_devices)
758 if __name__ == "__main__":