1#!/usr/bin/env python3 2# SPDX-License-Identifier: BSD-3-Clause 3# Copyright(c) 2010-2014 Intel Corporation 4# 5 6import sys 7import os 8import subprocess 9import argparse 10import platform 11 12from glob import glob 13from os.path import exists, basename 14from os.path import join as path_join 15 16# The PCI base class for all devices 17network_class = {'Class': '02', 'Vendor': None, 'Device': None, 18 'SVendor': None, 'SDevice': None} 19acceleration_class = {'Class': '12', 'Vendor': None, 'Device': None, 20 'SVendor': None, 'SDevice': None} 21ifpga_class = {'Class': '12', 'Vendor': '8086', 'Device': '0b30', 22 'SVendor': None, 'SDevice': None} 23encryption_class = {'Class': '10', 'Vendor': None, 'Device': None, 24 'SVendor': None, 'SDevice': None} 25intel_processor_class = {'Class': '0b', 'Vendor': '8086', 'Device': None, 26 'SVendor': None, 'SDevice': None} 27cavium_sso = {'Class': '08', 'Vendor': '177d', 'Device': 'a04b,a04d', 28 'SVendor': None, 'SDevice': None} 29cavium_fpa = {'Class': '08', 'Vendor': '177d', 'Device': 'a053', 30 'SVendor': None, 'SDevice': None} 31cavium_pkx = {'Class': '08', 'Vendor': '177d', 'Device': 'a0dd,a049', 32 'SVendor': None, 'SDevice': None} 33cavium_tim = {'Class': '08', 'Vendor': '177d', 'Device': 'a051', 34 'SVendor': None, 'SDevice': None} 35cavium_zip = {'Class': '12', 'Vendor': '177d', 'Device': 'a037', 36 'SVendor': None, 'SDevice': None} 37avp_vnic = {'Class': '05', 'Vendor': '1af4', 'Device': '1110', 38 'SVendor': None, 'SDevice': None} 39 40cnxk_bphy = {'Class': '08', 'Vendor': '177d', 'Device': 'a089', 41 'SVendor': None, 'SDevice': None} 42cnxk_bphy_cgx = {'Class': '08', 'Vendor': '177d', 'Device': 'a059,a060', 43 'SVendor': None, 'SDevice': None} 44cnxk_dma = {'Class': '08', 'Vendor': '177d', 'Device': 'a081', 45 'SVendor': None, 'SDevice': None} 46cnxk_inl_dev = {'Class': '08', 'Vendor': '177d', 'Device': 'a0f0,a0f1', 47 'SVendor': None, 'SDevice': None} 48 49hisilicon_dma = {'Class': '08', 'Vendor': '19e5', 'Device': 'a122', 50 'SVendor': None, 'SDevice': None} 51odm_dma = {'Class': '08', 'Vendor': '177d', 'Device': 'a08c', 52 'SVendor': None, 'SDevice': None} 53 54intel_dlb = {'Class': '0b', 'Vendor': '8086', 'Device': '270b,2710,2714', 55 'SVendor': None, 'SDevice': None} 56intel_ioat_bdw = {'Class': '08', 'Vendor': '8086', 57 'Device': '6f20,6f21,6f22,6f23,6f24,6f25,6f26,6f27,6f2e,6f2f', 58 'SVendor': None, 'SDevice': None} 59intel_ioat_skx = {'Class': '08', 'Vendor': '8086', 'Device': '2021', 60 'SVendor': None, 'SDevice': None} 61intel_ioat_icx = {'Class': '08', 'Vendor': '8086', 'Device': '0b00', 62 'SVendor': None, 'SDevice': None} 63intel_idxd_spr = {'Class': '08', 'Vendor': '8086', 'Device': '0b25', 64 'SVendor': None, 'SDevice': None} 65intel_ntb_skx = {'Class': '06', 'Vendor': '8086', 'Device': '201c', 66 'SVendor': None, 'SDevice': None} 67intel_ntb_icx = {'Class': '06', 'Vendor': '8086', 'Device': '347e', 68 'SVendor': None, 'SDevice': None} 69 70cnxk_sso = {'Class': '08', 'Vendor': '177d', 'Device': 'a0f9,a0fa', 71 'SVendor': None, 'SDevice': None} 72cnxk_npa = {'Class': '08', 'Vendor': '177d', 'Device': 'a0fb,a0fc', 73 'SVendor': None, 'SDevice': None} 74cn9k_ree = {'Class': '08', 'Vendor': '177d', 'Device': 'a0f4', 75 'SVendor': None, 'SDevice': None} 76 77virtio_blk = {'Class': '01', 'Vendor': "1af4", 'Device': '1001,1042', 78 'SVendor': None, 'SDevice': None} 79 80cnxk_ml = {'Class': '08', 'Vendor': '177d', 'Device': 'a092', 81 'SVendor': None, 'SDevice': None} 82 83network_devices = [network_class, cavium_pkx, avp_vnic, ifpga_class] 84baseband_devices = [acceleration_class] 85crypto_devices = [encryption_class, intel_processor_class] 86dma_devices = [cnxk_dma, hisilicon_dma, 87 intel_idxd_spr, intel_ioat_bdw, intel_ioat_icx, intel_ioat_skx, 88 odm_dma] 89eventdev_devices = [cavium_sso, cavium_tim, intel_dlb, cnxk_sso] 90mempool_devices = [cavium_fpa, cnxk_npa] 91compress_devices = [cavium_zip] 92regex_devices = [cn9k_ree] 93ml_devices = [cnxk_ml] 94misc_devices = [cnxk_bphy, cnxk_bphy_cgx, cnxk_inl_dev, 95 intel_ntb_skx, intel_ntb_icx, 96 virtio_blk] 97 98# global dict ethernet devices present. Dictionary indexed by PCI address. 99# Each device within this is itself a dictionary of device properties 100devices = {} 101# list of supported DPDK drivers 102dpdk_drivers = ["igb_uio", "vfio-pci", "uio_pci_generic"] 103# list of currently loaded kernel modules 104loaded_modules = None 105 106# command-line arg flags 107b_flag = None 108status_flag = False 109force_flag = False 110noiommu_flag = False 111args = [] 112 113 114# check if this system has NUMA support 115def is_numa(): 116 return os.path.exists('/sys/devices/system/node') 117 118 119# check if a specific kernel module is loaded 120def module_is_loaded(module): 121 global loaded_modules 122 123 if module == 'vfio_pci': 124 module = 'vfio-pci' 125 126 if loaded_modules: 127 return module in loaded_modules 128 129 # Get list of sysfs modules (both built-in and dynamically loaded) 130 sysfs_path = '/sys/module/' 131 132 # Get the list of directories in sysfs_path 133 sysfs_mods = [m for m in os.listdir(sysfs_path) 134 if os.path.isdir(os.path.join(sysfs_path, m))] 135 136 # special case for vfio_pci (module is named vfio-pci, 137 # but its .ko is named vfio_pci) 138 sysfs_mods = [a if a != 'vfio_pci' else 'vfio-pci' for a in sysfs_mods] 139 140 loaded_modules = sysfs_mods 141 142 # add built-in modules as loaded 143 release = platform.uname().release 144 filename = os.path.join("/lib/modules/", release, "modules.builtin") 145 if os.path.exists(filename): 146 try: 147 with open(filename) as f: 148 loaded_modules += [os.path.splitext(os.path.basename(mod))[0] for mod in f] 149 except IOError: 150 print("Warning: cannot read list of built-in kernel modules") 151 152 return module in loaded_modules 153 154 155def check_modules(): 156 '''Checks that igb_uio is loaded''' 157 global dpdk_drivers 158 159 # list of supported modules 160 mods = [{"Name": driver, "Found": False} for driver in dpdk_drivers] 161 162 # first check if module is loaded 163 for mod in mods: 164 if module_is_loaded(mod["Name"]): 165 mod["Found"] = True 166 167 # check if we have at least one loaded module 168 if True not in [mod["Found"] for mod in mods] and b_flag is not None: 169 print("Warning: no supported DPDK kernel modules are loaded", file=sys.stderr) 170 171 # change DPDK driver list to only contain drivers that are loaded 172 dpdk_drivers = [mod["Name"] for mod in mods if mod["Found"]] 173 174 175def has_driver(dev_id): 176 '''return true if a device is assigned to a driver. False otherwise''' 177 return "Driver_str" in devices[dev_id] 178 179 180def get_pci_device_details(dev_id, probe_lspci): 181 '''This function gets additional details for a PCI device''' 182 device = {} 183 184 if probe_lspci: 185 extra_info = subprocess.check_output(["lspci", "-vmmks", dev_id]).splitlines() 186 # parse lspci details 187 for line in extra_info: 188 if not line: 189 continue 190 name, value = line.decode("utf8").split("\t", 1) 191 name = name.strip(":") + "_str" 192 device[name] = value 193 # check for a unix interface name 194 device["Interface"] = "" 195 for base, dirs, _ in os.walk("/sys/bus/pci/devices/%s/" % dev_id): 196 if "net" in dirs: 197 device["Interface"] = \ 198 ",".join(os.listdir(os.path.join(base, "net"))) 199 break 200 # check if a port is used for ssh connection 201 device["Ssh_if"] = False 202 device["Active"] = "" 203 204 return device 205 206 207def clear_data(): 208 '''This function clears any old data''' 209 global devices 210 devices = {} 211 212 213def get_device_details(devices_type): 214 '''This function populates the "devices" dictionary. The keys used are 215 the pci addresses (domain:bus:slot.func). The values are themselves 216 dictionaries - one for each NIC.''' 217 global devices 218 global dpdk_drivers 219 220 # first loop through and read details for all devices 221 # request machine readable format, with numeric IDs and String 222 dev = {} 223 dev_lines = subprocess.check_output(["lspci", "-Dvmmnnk"]).splitlines() 224 for dev_line in dev_lines: 225 if not dev_line: 226 if device_type_match(dev, devices_type): 227 # Replace "Driver" with "Driver_str" to have consistency of 228 # of dictionary key names 229 if "Driver" in dev.keys(): 230 dev["Driver_str"] = dev.pop("Driver") 231 if "Module" in dev.keys(): 232 dev["Module_str"] = dev.pop("Module") 233 # use dict to make copy of dev 234 devices[dev["Slot"]] = dict(dev) 235 # Clear previous device's data 236 dev = {} 237 else: 238 name, value = dev_line.decode("utf8").split("\t", 1) 239 value_list = value.rsplit(' ', 1) 240 if value_list: 241 # String stored in <name>_str 242 dev[name.rstrip(":") + '_str'] = value_list[0] 243 # Numeric IDs 244 dev[name.rstrip(":")] = value_list[len(value_list) - 1] \ 245 .rstrip("]").lstrip("[") 246 247 if devices_type == network_devices: 248 # check what is the interface if any for an ssh connection if 249 # any to this host, so we can mark it later. 250 ssh_if = [] 251 route = subprocess.check_output(["ip", "-o", "route"]) 252 # filter out all lines for 169.254 routes 253 route = "\n".join(filter(lambda ln: not ln.startswith("169.254"), 254 route.decode().splitlines())) 255 rt_info = route.split() 256 for i in range(len(rt_info) - 1): 257 if rt_info[i] == "dev": 258 ssh_if.append(rt_info[i + 1]) 259 260 # based on the basic info, get extended text details 261 for d in devices.keys(): 262 if not device_type_match(devices[d], devices_type): 263 continue 264 265 # get additional info and add it to existing data 266 devices[d] = devices[d].copy() 267 # No need to probe lspci 268 devices[d].update(get_pci_device_details(d, False).items()) 269 270 if devices_type == network_devices: 271 for _if in ssh_if: 272 if _if in devices[d]["Interface"].split(","): 273 devices[d]["Ssh_if"] = True 274 devices[d]["Active"] = "*Active*" 275 break 276 277 # add igb_uio to list of supporting modules if needed 278 if "Module_str" in devices[d]: 279 for driver in dpdk_drivers: 280 if driver not in devices[d]["Module_str"]: 281 devices[d]["Module_str"] = \ 282 devices[d]["Module_str"] + ",%s" % driver 283 else: 284 devices[d]["Module_str"] = ",".join(dpdk_drivers) 285 286 # make sure the driver and module strings do not have any duplicates 287 if has_driver(d): 288 modules = devices[d]["Module_str"].split(",") 289 if devices[d]["Driver_str"] in modules: 290 modules.remove(devices[d]["Driver_str"]) 291 devices[d]["Module_str"] = ",".join(modules) 292 293 294def device_type_match(dev, devices_type): 295 for i in range(len(devices_type)): 296 param_count = len( 297 [x for x in devices_type[i].values() if x is not None]) 298 match_count = 0 299 if dev["Class"][0:2] == devices_type[i]["Class"]: 300 match_count = match_count + 1 301 for key in devices_type[i].keys(): 302 if key != 'Class' and devices_type[i][key]: 303 value_list = devices_type[i][key].split(',') 304 for value in value_list: 305 if value.strip(' ') == dev[key]: 306 match_count = match_count + 1 307 # count must be the number of non None parameters to match 308 if match_count == param_count: 309 return True 310 return False 311 312 313def dev_id_from_dev_name(dev_name): 314 '''Take a device "name" - a string passed in by user to identify a NIC 315 device, and determine the device id - i.e. the domain:bus:slot.func - for 316 it, which can then be used to index into the devices array''' 317 318 # check if it's already a suitable index 319 if dev_name in devices: 320 return dev_name 321 # check if it's an index just missing the domain part 322 if "0000:" + dev_name in devices: 323 return "0000:" + dev_name 324 325 # check if it's an interface name, e.g. eth1 326 for d in devices.keys(): 327 if dev_name in devices[d]["Interface"].split(","): 328 return devices[d]["Slot"] 329 # if nothing else matches - error 330 raise ValueError("Unknown device: %s. " 331 "Please specify device in \"bus:slot.func\" format" % dev_name) 332 333 334def unbind_one(dev_id, force): 335 '''Unbind the device identified by "dev_id" from its current driver''' 336 dev = devices[dev_id] 337 if not has_driver(dev_id): 338 print("Notice: %s %s %s is not currently managed by any driver" % 339 (dev["Slot"], dev["Device_str"], dev["Interface"]), file=sys.stderr) 340 return 341 342 # prevent us disconnecting ourselves 343 if dev["Ssh_if"] and not force: 344 print("Warning: routing table indicates that interface %s is active. " 345 "Skipping unbind" % dev_id, file=sys.stderr) 346 return 347 348 # write to /sys to unbind 349 filename = "/sys/bus/pci/drivers/%s/unbind" % dev["Driver_str"] 350 try: 351 f = open(filename, "a") 352 except OSError as err: 353 sys.exit("Error: unbind failed for %s - Cannot open %s: %s" % 354 (dev_id, filename, err)) 355 f.write(dev_id) 356 f.close() 357 358 359def bind_one(dev_id, driver, force): 360 '''Bind the device given by "dev_id" to the driver "driver". If the device 361 is already bound to a different driver, it will be unbound first''' 362 dev = devices[dev_id] 363 saved_driver = None # used to rollback any unbind in case of failure 364 365 # prevent disconnection of our ssh session 366 if dev["Ssh_if"] and not force: 367 print("Warning: routing table indicates that interface %s is active. " 368 "Not modifying" % dev_id, file=sys.stderr) 369 return 370 371 # unbind any existing drivers we don't want 372 if has_driver(dev_id): 373 if dev["Driver_str"] == driver: 374 print("Notice: %s already bound to driver %s, skipping" % 375 (dev_id, driver), file=sys.stderr) 376 return 377 saved_driver = dev["Driver_str"] 378 unbind_one(dev_id, force) 379 dev["Driver_str"] = "" # clear driver string 380 381 # For kernels >= 3.15 driver_override can be used to specify the driver 382 # for a device rather than relying on the driver to provide a positive 383 # match of the device. The existing process of looking up 384 # the vendor and device ID, adding them to the driver new_id, 385 # will erroneously bind other devices too which has the additional burden 386 # of unbinding those devices 387 if driver in dpdk_drivers: 388 filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id 389 if exists(filename): 390 try: 391 f = open(filename, "w") 392 except OSError as err: 393 print("Error: bind failed for %s - Cannot open %s: %s" 394 % (dev_id, filename, err), file=sys.stderr) 395 return 396 try: 397 f.write("%s" % driver) 398 f.close() 399 except OSError as err: 400 print("Error: bind failed for %s - Cannot write driver %s to " 401 "PCI ID: %s" % (dev_id, driver, err), file=sys.stderr) 402 return 403 # For kernels < 3.15 use new_id to add PCI id's to the driver 404 else: 405 filename = "/sys/bus/pci/drivers/%s/new_id" % driver 406 try: 407 f = open(filename, "w") 408 except OSError as err: 409 print("Error: bind failed for %s - Cannot open %s: %s" 410 % (dev_id, filename, err), file=sys.stderr) 411 return 412 try: 413 # Convert Device and Vendor Id to int to write to new_id 414 f.write("%04x %04x" % (int(dev["Vendor"], 16), 415 int(dev["Device"], 16))) 416 f.close() 417 except OSError as err: 418 print("Error: bind failed for %s - Cannot write new PCI ID to " 419 "driver %s: %s" % (dev_id, driver, err), file=sys.stderr) 420 return 421 422 # do the bind by writing to /sys 423 filename = "/sys/bus/pci/drivers/%s/bind" % driver 424 try: 425 f = open(filename, "a") 426 except OSError as err: 427 print("Error: bind failed for %s - Cannot open %s: %s" 428 % (dev_id, filename, err), file=sys.stderr) 429 if saved_driver is not None: # restore any previous driver 430 bind_one(dev_id, saved_driver, force) 431 return 432 try: 433 f.write(dev_id) 434 f.close() 435 except OSError as err: 436 # for some reason, closing dev_id after adding a new PCI ID to new_id 437 # results in IOError. however, if the device was successfully bound, 438 # we don't care for any errors and can safely ignore IOError 439 tmp = get_pci_device_details(dev_id, True) 440 if "Driver_str" in tmp and tmp["Driver_str"] == driver: 441 return 442 print("Error: bind failed for %s - Cannot bind to driver %s: %s" 443 % (dev_id, driver, err), file=sys.stderr) 444 if saved_driver is not None: # restore any previous driver 445 bind_one(dev_id, saved_driver, force) 446 return 447 448 # For kernels > 3.15 driver_override is used to bind a device to a driver. 449 # Before unbinding it, overwrite driver_override with empty string so that 450 # the device can be bound to any other driver 451 filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id 452 if exists(filename): 453 try: 454 f = open(filename, "w") 455 except OSError as err: 456 sys.exit("Error: unbind failed for %s - Cannot open %s: %s" 457 % (dev_id, filename, err)) 458 try: 459 f.write("\00") 460 f.close() 461 except OSError as err: 462 sys.exit("Error: unbind failed for %s - Cannot write %s: %s" 463 % (dev_id, filename, err)) 464 465 466def unbind_all(dev_list, force=False): 467 """Unbind method, takes a list of device locations""" 468 469 if dev_list[0] == "dpdk": 470 for d in devices.keys(): 471 if "Driver_str" in devices[d]: 472 if devices[d]["Driver_str"] in dpdk_drivers: 473 unbind_one(devices[d]["Slot"], force) 474 return 475 476 try: 477 dev_list = map(dev_id_from_dev_name, dev_list) 478 except ValueError as ex: 479 print(ex) 480 sys.exit(1) 481 482 for d in dev_list: 483 unbind_one(d, force) 484 485 486def has_iommu(): 487 """Check if IOMMU is enabled on system""" 488 return len(os.listdir("/sys/class/iommu")) > 0 489 490 491def check_noiommu_mode(): 492 """Check and enable the noiommu mode for VFIO drivers""" 493 global noiommu_flag 494 filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode" 495 496 try: 497 with open(filename, "r") as f: 498 value = f.read(1) 499 if value in ("1", "y" ,"Y"): 500 return 501 except OSError as err: 502 sys.exit(f"Error: failed to check unsafe noiommu mode - Cannot open {filename}: {err}") 503 504 if not noiommu_flag: 505 sys.exit("Error: IOMMU support is disabled, use --noiommu-mode for binding in noiommu mode") 506 507 try: 508 with open(filename, "w") as f: 509 f.write("1") 510 except OSError as err: 511 sys.exit(f"Error: failed to enable unsafe noiommu mode - Cannot open {filename}: {err}") 512 print("Warning: enabling unsafe no IOMMU mode for VFIO drivers") 513 514 515def bind_all(dev_list, driver, force=False): 516 """Bind method, takes a list of device locations""" 517 global devices 518 519 # a common user error is to forget to specify the driver the devices need to 520 # be bound to. check if the driver is a valid device, and if it is, show 521 # a meaningful error. 522 try: 523 dev_id_from_dev_name(driver) 524 # if we've made it this far, this means that the "driver" was a valid 525 # device string, so it's probably not a valid driver name. 526 sys.exit("Error: Driver '%s' does not look like a valid driver. " 527 "Did you forget to specify the driver to bind devices to?" % driver) 528 except ValueError: 529 # driver generated error - it's not a valid device ID, so all is well 530 pass 531 532 # check if we're attempting to bind to a driver that isn't loaded 533 if not module_is_loaded(driver.replace('-', '_')): 534 sys.exit("Error: Driver '%s' is not loaded." % driver) 535 536 try: 537 dev_list = map(dev_id_from_dev_name, dev_list) 538 except ValueError as ex: 539 sys.exit(ex) 540 541 # check for IOMMU support 542 if driver == "vfio-pci" and not has_iommu(): 543 check_noiommu_mode() 544 545 for d in dev_list: 546 bind_one(d, driver, force) 547 548 # For kernels < 3.15 when binding devices to a generic driver 549 # (i.e. one that doesn't have a PCI ID table) using new_id, some devices 550 # that are not bound to any other driver could be bound even if no one has 551 # asked them to. hence, we check the list of drivers again, and see if 552 # some of the previously-unbound devices were erroneously bound. 553 if not exists("/sys/bus/pci/devices/%s/driver_override" % d): 554 for d in devices.keys(): 555 # skip devices that were already bound or that we know should be bound 556 if "Driver_str" in devices[d] or d in dev_list: 557 continue 558 559 # update information about this device 560 devices[d] = dict(devices[d].items() 561 + get_pci_device_details(d, True).items()) 562 563 # check if updated information indicates that the device was bound 564 if "Driver_str" in devices[d]: 565 unbind_one(d, force) 566 567 568def display_devices(title, dev_list, extra_params=None): 569 '''Displays to the user the details of a list of devices given in 570 "dev_list". The "extra_params" parameter, if given, should contain a string 571 with %()s fields in it for replacement by the named fields in each 572 device's dictionary.''' 573 strings = [] # this holds the strings to print. We sort before printing 574 print("\n%s" % title) 575 print("=" * len(title)) 576 if not dev_list: 577 strings.append("<none>") 578 else: 579 for dev in dev_list: 580 if extra_params is not None: 581 strings.append("%s '%s %s' %s" % (dev["Slot"], 582 dev["Device_str"], 583 dev["Device"], 584 extra_params % dev)) 585 else: 586 strings.append("%s '%s'" % (dev["Slot"], dev["Device_str"])) 587 # sort before printing, so that the entries appear in PCI order 588 strings.sort() 589 print("\n".join(strings)) # print one per line 590 591 592def show_device_status(devices_type, device_name, if_field=False): 593 global dpdk_drivers 594 kernel_drv = [] 595 dpdk_drv = [] 596 no_drv = [] 597 598 # split our list of network devices into the three categories above 599 for d in devices.keys(): 600 if device_type_match(devices[d], devices_type): 601 if not has_driver(d): 602 no_drv.append(devices[d]) 603 continue 604 if devices[d]["Driver_str"] in dpdk_drivers: 605 dpdk_drv.append(devices[d]) 606 else: 607 kernel_drv.append(devices[d]) 608 609 n_devs = len(dpdk_drv) + len(kernel_drv) + len(no_drv) 610 611 # don't bother displaying anything if there are no devices 612 if n_devs == 0: 613 msg = "No '%s' devices detected" % device_name 614 print("") 615 print(msg) 616 print("".join('=' * len(msg))) 617 return 618 619 print_numa = is_numa() 620 621 # print each category separately, so we can clearly see what's used by DPDK 622 if dpdk_drv: 623 extra_param = "drv=%(Driver_str)s unused=%(Module_str)s" 624 if print_numa: 625 extra_param = "numa_node=%(NUMANode)s " + extra_param 626 display_devices("%s devices using DPDK-compatible driver" % device_name, 627 dpdk_drv, extra_param) 628 if kernel_drv: 629 extra_param = "drv=%(Driver_str)s unused=%(Module_str)s" 630 if if_field: 631 extra_param = "if=%(Interface)s " + extra_param 632 if print_numa: 633 extra_param = "numa_node=%(NUMANode)s " + extra_param 634 display_devices("%s devices using kernel driver" % device_name, 635 kernel_drv, extra_param) 636 if no_drv: 637 extra_param = "unused=%(Module_str)s" 638 if print_numa: 639 extra_param = "numa_node=%(NUMANode)s " + extra_param 640 display_devices("Other %s devices" % device_name, no_drv, extra_param) 641 642 643def show_status(): 644 '''Function called when the script is passed the "--status" option. 645 Displays to the user what devices are bound to the igb_uio driver, the 646 kernel driver or to no driver''' 647 648 if status_dev in ["net", "all"]: 649 show_device_status(network_devices, "Network", if_field=True) 650 651 if status_dev in ["baseband", "all"]: 652 show_device_status(baseband_devices, "Baseband") 653 654 if status_dev in ["crypto", "all"]: 655 show_device_status(crypto_devices, "Crypto") 656 657 if status_dev in ["dma", "all"]: 658 show_device_status(dma_devices, "DMA") 659 660 if status_dev in ["event", "all"]: 661 show_device_status(eventdev_devices, "Eventdev") 662 663 if status_dev in ["mempool", "all"]: 664 show_device_status(mempool_devices, "Mempool") 665 666 if status_dev in ["compress", "all"]: 667 show_device_status(compress_devices, "Compress") 668 669 if status_dev in ["misc", "all"]: 670 show_device_status(misc_devices, "Misc (rawdev)") 671 672 if status_dev in ["regex", "all"]: 673 show_device_status(regex_devices, "Regex") 674 675 if status_dev in ["ml", "all"]: 676 show_device_status(ml_devices, "ML") 677 678 679def pci_glob(arg): 680 '''Returns a list containing either: 681 * List of PCI B:D:F matching arg, using shell wildcards e.g. 80:04.* 682 * Only the passed arg if matching list is empty''' 683 sysfs_path = "/sys/bus/pci/devices" 684 for _glob in [arg, '0000:' + arg]: 685 paths = [basename(path) for path in glob(path_join(sysfs_path, _glob))] 686 if paths: 687 return paths 688 return [arg] 689 690 691def parse_args(): 692 '''Parses the command-line arguments given by the user and takes the 693 appropriate action for each''' 694 global b_flag 695 global status_flag 696 global status_dev 697 global force_flag 698 global noiommu_flag 699 global args 700 701 parser = argparse.ArgumentParser( 702 description='Utility to bind and unbind devices from Linux kernel', 703 formatter_class=argparse.RawDescriptionHelpFormatter, 704 epilog=""" 705Examples: 706--------- 707 708To display current device status: 709 %(prog)s --status 710 711To display current network device status: 712 %(prog)s --status-dev net 713 714To bind eth1 from the current driver and move to use vfio-pci 715 %(prog)s --bind=vfio-pci eth1 716 717To unbind 0000:01:00.0 from using any driver 718 %(prog)s -u 0000:01:00.0 719 720To bind 0000:02:00.0 and 0000:02:00.1 to the ixgbe kernel driver 721 %(prog)s -b ixgbe 02:00.0 02:00.1 722""") 723 724 parser.add_argument( 725 '-s', 726 '--status', 727 action='store_true', 728 help="Print the current status of all known devices.") 729 parser.add_argument( 730 '--status-dev', 731 help="Print the status of given device group.", 732 choices=['baseband', 'compress', 'crypto', 'dma', 'event', 733 'mempool', 'misc', 'net', 'regex', 'ml']) 734 bind_group = parser.add_mutually_exclusive_group() 735 bind_group.add_argument( 736 '-b', 737 '--bind', 738 metavar='DRIVER', 739 help="Select the driver to use or \"none\" to unbind the device") 740 bind_group.add_argument( 741 '-u', 742 '--unbind', 743 action='store_true', 744 help="Unbind a device (equivalent to \"-b none\")") 745 parser.add_argument( 746 '--noiommu-mode', 747 action='store_true', 748 help="If IOMMU is not available, enable no IOMMU mode for VFIO drivers") 749 parser.add_argument( 750 '--force', 751 action='store_true', 752 help=""" 753Override restriction on binding devices in use by Linux" 754WARNING: This can lead to loss of network connection and should be used with caution. 755""") 756 parser.add_argument( 757 'devices', 758 metavar='DEVICE', 759 nargs='*', 760 help=""" 761Device specified as PCI "domain:bus:slot.func" syntax or "bus:slot.func" syntax. 762For devices bound to Linux kernel drivers, they may be referred to by interface name. 763""") 764 765 opt = parser.parse_args() 766 767 if opt.status_dev: 768 status_flag = True 769 status_dev = opt.status_dev 770 if opt.status: 771 status_flag = True 772 status_dev = "all" 773 if opt.force: 774 force_flag = True 775 if opt.noiommu_mode: 776 noiommu_flag = True 777 if opt.bind: 778 b_flag = opt.bind 779 elif opt.unbind: 780 b_flag = "none" 781 args = opt.devices 782 783 if not b_flag and not status_flag: 784 print("Error: No action specified for devices. " 785 "Please give a --bind, --ubind or --status option", 786 file=sys.stderr) 787 parser.print_usage() 788 sys.exit(1) 789 790 if b_flag and not args: 791 print("Error: No devices specified.", file=sys.stderr) 792 parser.print_usage() 793 sys.exit(1) 794 795 # resolve any PCI globs in the args 796 new_args = [] 797 for arg in args: 798 new_args.extend(pci_glob(arg)) 799 args = new_args 800 801 802def do_arg_actions(): 803 '''do the actual action requested by the user''' 804 global b_flag 805 global status_flag 806 global force_flag 807 global args 808 809 if b_flag in ["none", "None"]: 810 unbind_all(args, force_flag) 811 elif b_flag is not None: 812 bind_all(args, b_flag, force_flag) 813 if status_flag: 814 if b_flag is not None: 815 clear_data() 816 # refresh if we have changed anything 817 get_device_details(network_devices) 818 get_device_details(baseband_devices) 819 get_device_details(crypto_devices) 820 get_device_details(dma_devices) 821 get_device_details(eventdev_devices) 822 get_device_details(mempool_devices) 823 get_device_details(compress_devices) 824 get_device_details(regex_devices) 825 get_device_details(ml_devices) 826 get_device_details(misc_devices) 827 show_status() 828 829 830def main(): 831 '''program main function''' 832 # check if lspci is installed, suppress any output 833 with open(os.devnull, 'w') as devnull: 834 ret = subprocess.call(['which', 'lspci'], 835 stdout=devnull, stderr=devnull) 836 if ret != 0: 837 sys.exit("'lspci' not found - please install 'pciutils'") 838 parse_args() 839 check_modules() 840 clear_data() 841 get_device_details(network_devices) 842 get_device_details(baseband_devices) 843 get_device_details(crypto_devices) 844 get_device_details(dma_devices) 845 get_device_details(eventdev_devices) 846 get_device_details(mempool_devices) 847 get_device_details(compress_devices) 848 get_device_details(regex_devices) 849 get_device_details(ml_devices) 850 get_device_details(misc_devices) 851 do_arg_actions() 852 853 854if __name__ == "__main__": 855 main() 856