1#! /usr/bin/env python 2# SPDX-License-Identifier: BSD-3-Clause 3# Copyright(c) 2010-2014 Intel Corporation 4# 5 6import sys 7import os 8import getopt 9import subprocess 10from os.path import exists, abspath, dirname, basename 11 12# The PCI base class for all devices 13network_class = {'Class': '02', 'Vendor': None, 'Device': None, 14 'SVendor': None, 'SDevice': None} 15encryption_class = {'Class': '10', 'Vendor': None, 'Device': None, 16 'SVendor': None, 'SDevice': None} 17intel_processor_class = {'Class': '0b', 'Vendor': '8086', 'Device': None, 18 'SVendor': None, 'SDevice': None} 19cavium_sso = {'Class': '08', 'Vendor': '177d', 'Device': 'a04b,a04d', 20 'SVendor': None, 'SDevice': None} 21cavium_fpa = {'Class': '08', 'Vendor': '177d', 'Device': 'a053', 22 'SVendor': None, 'SDevice': None} 23cavium_pkx = {'Class': '08', 'Vendor': '177d', 'Device': 'a0dd,a049', 24 'SVendor': None, 'SDevice': None} 25cavium_tim = {'Class': '08', 'Vendor': '177d', 'Device': 'a051', 26 'SVendor': None, 'SDevice': None} 27cavium_zip = {'Class': '12', 'Vendor': '177d', 'Device': 'a037', 28 'SVendor': None, 'SDevice': None} 29avp_vnic = {'Class': '05', 'Vendor': '1af4', 'Device': '1110', 30 'SVendor': None, 'SDevice': None} 31 32network_devices = [network_class, cavium_pkx, avp_vnic] 33crypto_devices = [encryption_class, intel_processor_class] 34eventdev_devices = [cavium_sso, cavium_tim] 35mempool_devices = [cavium_fpa] 36compress_devices = [cavium_zip] 37 38# global dict ethernet devices present. Dictionary indexed by PCI address. 39# Each device within this is itself a dictionary of device properties 40devices = {} 41# list of supported DPDK drivers 42dpdk_drivers = ["igb_uio", "vfio-pci", "uio_pci_generic"] 43 44# command-line arg flags 45b_flag = None 46status_flag = False 47force_flag = False 48args = [] 49 50 51def usage(): 52 '''Print usage information for the program''' 53 argv0 = basename(sys.argv[0]) 54 print(""" 55Usage: 56------ 57 58 %(argv0)s [options] DEVICE1 DEVICE2 .... 59 60where DEVICE1, DEVICE2 etc, are specified via PCI "domain:bus:slot.func" syntax 61or "bus:slot.func" syntax. For devices bound to Linux kernel drivers, they may 62also be referred to by Linux interface name e.g. eth0, eth1, em0, em1, etc. 63 64Options: 65 --help, --usage: 66 Display usage information and quit 67 68 -s, --status: 69 Print the current status of all known network, crypto, event 70 and mempool devices. 71 For each device, it displays the PCI domain, bus, slot and function, 72 along with a text description of the device. Depending upon whether the 73 device is being used by a kernel driver, the igb_uio driver, or no 74 driver, other relevant information will be displayed: 75 * the Linux interface name e.g. if=eth0 76 * the driver being used e.g. drv=igb_uio 77 * any suitable drivers not currently using that device 78 e.g. unused=igb_uio 79 NOTE: if this flag is passed along with a bind/unbind option, the 80 status display will always occur after the other operations have taken 81 place. 82 83 --status-dev: 84 Print the status of given device group. Supported device groups are: 85 "net", "crypto", "event", "mempool" and "compress" 86 87 -b driver, --bind=driver: 88 Select the driver to use or \"none\" to unbind the device 89 90 -u, --unbind: 91 Unbind a device (Equivalent to \"-b none\") 92 93 --force: 94 By default, network devices which are used by Linux - as indicated by 95 having routes in the routing table - cannot be modified. Using the 96 --force flag overrides this behavior, allowing active links to be 97 forcibly unbound. 98 WARNING: This can lead to loss of network connection and should be used 99 with caution. 100 101Examples: 102--------- 103 104To display current device status: 105 %(argv0)s --status 106 107To display current network device status: 108 %(argv0)s --status-dev net 109 110To bind eth1 from the current driver and move to use igb_uio 111 %(argv0)s --bind=igb_uio eth1 112 113To unbind 0000:01:00.0 from using any driver 114 %(argv0)s -u 0000:01:00.0 115 116To bind 0000:02:00.0 and 0000:02:00.1 to the ixgbe kernel driver 117 %(argv0)s -b ixgbe 02:00.0 02:00.1 118 119 """ % locals()) # replace items from local variables 120 121 122# This is roughly compatible with check_output function in subprocess module 123# which is only available in python 2.7. 124def check_output(args, stderr=None): 125 '''Run a command and capture its output''' 126 return subprocess.Popen(args, stdout=subprocess.PIPE, 127 stderr=stderr).communicate()[0] 128 129 130def check_modules(): 131 '''Checks that igb_uio is loaded''' 132 global dpdk_drivers 133 134 # list of supported modules 135 mods = [{"Name": driver, "Found": False} for driver in dpdk_drivers] 136 137 # first check if module is loaded 138 try: 139 # Get list of sysfs modules (both built-in and dynamically loaded) 140 sysfs_path = '/sys/module/' 141 142 # Get the list of directories in sysfs_path 143 sysfs_mods = [os.path.join(sysfs_path, o) for o 144 in os.listdir(sysfs_path) 145 if os.path.isdir(os.path.join(sysfs_path, o))] 146 147 # Extract the last element of '/sys/module/abc' in the array 148 sysfs_mods = [a.split('/')[-1] for a in sysfs_mods] 149 150 # special case for vfio_pci (module is named vfio-pci, 151 # but its .ko is named vfio_pci) 152 sysfs_mods = [a if a != 'vfio_pci' else 'vfio-pci' for a in sysfs_mods] 153 154 for mod in mods: 155 if mod["Name"] in sysfs_mods: 156 mod["Found"] = True 157 except: 158 pass 159 160 # check if we have at least one loaded module 161 if True not in [mod["Found"] for mod in mods] and b_flag is not None: 162 if b_flag in dpdk_drivers: 163 print("Error - no supported modules(DPDK driver) are loaded") 164 sys.exit(1) 165 else: 166 print("Warning - no supported modules(DPDK driver) are loaded") 167 168 # change DPDK driver list to only contain drivers that are loaded 169 dpdk_drivers = [mod["Name"] for mod in mods if mod["Found"]] 170 171 172def has_driver(dev_id): 173 '''return true if a device is assigned to a driver. False otherwise''' 174 return "Driver_str" in devices[dev_id] 175 176 177def get_pci_device_details(dev_id, probe_lspci): 178 '''This function gets additional details for a PCI device''' 179 device = {} 180 181 if probe_lspci: 182 extra_info = check_output(["lspci", "-vmmks", dev_id]).splitlines() 183 184 # parse lspci details 185 for line in extra_info: 186 if len(line) == 0: 187 continue 188 name, value = line.decode().split("\t", 1) 189 name = name.strip(":") + "_str" 190 device[name] = value 191 # check for a unix interface name 192 device["Interface"] = "" 193 for base, dirs, _ in os.walk("/sys/bus/pci/devices/%s/" % dev_id): 194 if "net" in dirs: 195 device["Interface"] = \ 196 ",".join(os.listdir(os.path.join(base, "net"))) 197 break 198 # check if a port is used for ssh connection 199 device["Ssh_if"] = False 200 device["Active"] = "" 201 202 return device 203 204def clear_data(): 205 '''This function clears any old data''' 206 global devices 207 devices = {} 208 209def get_device_details(devices_type): 210 '''This function populates the "devices" dictionary. The keys used are 211 the pci addresses (domain:bus:slot.func). The values are themselves 212 dictionaries - one for each NIC.''' 213 global devices 214 global dpdk_drivers 215 216 # first loop through and read details for all devices 217 # request machine readable format, with numeric IDs and String 218 dev = {} 219 dev_lines = check_output(["lspci", "-Dvmmnnk"]).splitlines() 220 for dev_line in dev_lines: 221 if len(dev_line) == 0: 222 if device_type_match(dev, devices_type): 223 # Replace "Driver" with "Driver_str" to have consistency of 224 # of dictionary key names 225 if "Driver" in dev.keys(): 226 dev["Driver_str"] = dev.pop("Driver") 227 if "Module" in dev.keys(): 228 dev["Module_str"] = dev.pop("Module") 229 # use dict to make copy of dev 230 devices[dev["Slot"]] = dict(dev) 231 # Clear previous device's data 232 dev = {} 233 else: 234 name, value = dev_line.decode().split("\t", 1) 235 value_list = value.rsplit(' ', 1) 236 if len(value_list) > 1: 237 # String stored in <name>_str 238 dev[name.rstrip(":") + '_str'] = value_list[0] 239 # Numeric IDs 240 dev[name.rstrip(":")] = value_list[len(value_list) - 1] \ 241 .rstrip("]").lstrip("[") 242 243 if devices_type == network_devices: 244 # check what is the interface if any for an ssh connection if 245 # any to this host, so we can mark it later. 246 ssh_if = [] 247 route = check_output(["ip", "-o", "route"]) 248 # filter out all lines for 169.254 routes 249 route = "\n".join(filter(lambda ln: not ln.startswith("169.254"), 250 route.decode().splitlines())) 251 rt_info = route.split() 252 for i in range(len(rt_info) - 1): 253 if rt_info[i] == "dev": 254 ssh_if.append(rt_info[i+1]) 255 256 # based on the basic info, get extended text details 257 for d in devices.keys(): 258 if not device_type_match(devices[d], devices_type): 259 continue 260 261 # get additional info and add it to existing data 262 devices[d] = devices[d].copy() 263 # No need to probe lspci 264 devices[d].update(get_pci_device_details(d, False).items()) 265 266 if devices_type == network_devices: 267 for _if in ssh_if: 268 if _if in devices[d]["Interface"].split(","): 269 devices[d]["Ssh_if"] = True 270 devices[d]["Active"] = "*Active*" 271 break 272 273 # add igb_uio to list of supporting modules if needed 274 if "Module_str" in devices[d]: 275 for driver in dpdk_drivers: 276 if driver not in devices[d]["Module_str"]: 277 devices[d]["Module_str"] = \ 278 devices[d]["Module_str"] + ",%s" % driver 279 else: 280 devices[d]["Module_str"] = ",".join(dpdk_drivers) 281 282 # make sure the driver and module strings do not have any duplicates 283 if has_driver(d): 284 modules = devices[d]["Module_str"].split(",") 285 if devices[d]["Driver_str"] in modules: 286 modules.remove(devices[d]["Driver_str"]) 287 devices[d]["Module_str"] = ",".join(modules) 288 289 290def device_type_match(dev, devices_type): 291 for i in range(len(devices_type)): 292 param_count = len( 293 [x for x in devices_type[i].values() if x is not None]) 294 match_count = 0 295 if dev["Class"][0:2] == devices_type[i]["Class"]: 296 match_count = match_count + 1 297 for key in devices_type[i].keys(): 298 if key != 'Class' and devices_type[i][key]: 299 value_list = devices_type[i][key].split(',') 300 for value in value_list: 301 if value.strip(' ') == dev[key]: 302 match_count = match_count + 1 303 # count must be the number of non None parameters to match 304 if match_count == param_count: 305 return True 306 return False 307 308def dev_id_from_dev_name(dev_name): 309 '''Take a device "name" - a string passed in by user to identify a NIC 310 device, and determine the device id - i.e. the domain:bus:slot.func - for 311 it, which can then be used to index into the devices array''' 312 313 # check if it's already a suitable index 314 if dev_name in devices: 315 return dev_name 316 # check if it's an index just missing the domain part 317 elif "0000:" + dev_name in devices: 318 return "0000:" + dev_name 319 else: 320 # check if it's an interface name, e.g. eth1 321 for d in devices.keys(): 322 if dev_name in devices[d]["Interface"].split(","): 323 return devices[d]["Slot"] 324 # if nothing else matches - error 325 print("Unknown device: %s. " 326 "Please specify device in \"bus:slot.func\" format" % dev_name) 327 sys.exit(1) 328 329 330def unbind_one(dev_id, force): 331 '''Unbind the device identified by "dev_id" from its current driver''' 332 dev = devices[dev_id] 333 if not has_driver(dev_id): 334 print("%s %s %s is not currently managed by any driver\n" % 335 (dev["Slot"], dev["Device_str"], dev["Interface"])) 336 return 337 338 # prevent us disconnecting ourselves 339 if dev["Ssh_if"] and not force: 340 print("Routing table indicates that interface %s is active. " 341 "Skipping unbind" % (dev_id)) 342 return 343 344 # write to /sys to unbind 345 filename = "/sys/bus/pci/drivers/%s/unbind" % dev["Driver_str"] 346 try: 347 f = open(filename, "a") 348 except: 349 print("Error: unbind failed for %s - Cannot open %s" 350 % (dev_id, filename)) 351 sys.exit(1) 352 f.write(dev_id) 353 f.close() 354 355 356def bind_one(dev_id, driver, force): 357 '''Bind the device given by "dev_id" to the driver "driver". If the device 358 is already bound to a different driver, it will be unbound first''' 359 dev = devices[dev_id] 360 saved_driver = None # used to rollback any unbind in case of failure 361 362 # prevent disconnection of our ssh session 363 if dev["Ssh_if"] and not force: 364 print("Routing table indicates that interface %s is active. " 365 "Not modifying" % (dev_id)) 366 return 367 368 # unbind any existing drivers we don't want 369 if has_driver(dev_id): 370 if dev["Driver_str"] == driver: 371 print("%s already bound to driver %s, skipping\n" 372 % (dev_id, driver)) 373 return 374 else: 375 saved_driver = dev["Driver_str"] 376 unbind_one(dev_id, force) 377 dev["Driver_str"] = "" # clear driver string 378 379 # For kernels >= 3.15 driver_override can be used to specify the driver 380 # for a device rather than relying on the driver to provide a positive 381 # match of the device. The existing process of looking up 382 # the vendor and device ID, adding them to the driver new_id, 383 # will erroneously bind other devices too which has the additional burden 384 # of unbinding those devices 385 if driver in dpdk_drivers: 386 filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id 387 if os.path.exists(filename): 388 try: 389 f = open(filename, "w") 390 except: 391 print("Error: bind failed for %s - Cannot open %s" 392 % (dev_id, filename)) 393 return 394 try: 395 f.write("%s" % driver) 396 f.close() 397 except: 398 print("Error: bind failed for %s - Cannot write driver %s to " 399 "PCI ID " % (dev_id, driver)) 400 return 401 # For kernels < 3.15 use new_id to add PCI id's to the driver 402 else: 403 filename = "/sys/bus/pci/drivers/%s/new_id" % driver 404 try: 405 f = open(filename, "w") 406 except: 407 print("Error: bind failed for %s - Cannot open %s" 408 % (dev_id, filename)) 409 return 410 try: 411 # Convert Device and Vendor Id to int to write to new_id 412 f.write("%04x %04x" % (int(dev["Vendor"],16), 413 int(dev["Device"], 16))) 414 f.close() 415 except: 416 print("Error: bind failed for %s - Cannot write new PCI ID to " 417 "driver %s" % (dev_id, driver)) 418 return 419 420 # do the bind by writing to /sys 421 filename = "/sys/bus/pci/drivers/%s/bind" % driver 422 try: 423 f = open(filename, "a") 424 except: 425 print("Error: bind failed for %s - Cannot open %s" 426 % (dev_id, filename)) 427 if saved_driver is not None: # restore any previous driver 428 bind_one(dev_id, saved_driver, force) 429 return 430 try: 431 f.write(dev_id) 432 f.close() 433 except: 434 # for some reason, closing dev_id after adding a new PCI ID to new_id 435 # results in IOError. however, if the device was successfully bound, 436 # we don't care for any errors and can safely ignore IOError 437 tmp = get_pci_device_details(dev_id, True) 438 if "Driver_str" in tmp and tmp["Driver_str"] == driver: 439 return 440 print("Error: bind failed for %s - Cannot bind to driver %s" 441 % (dev_id, driver)) 442 if saved_driver is not None: # restore any previous driver 443 bind_one(dev_id, saved_driver, force) 444 return 445 446 # For kernels > 3.15 driver_override is used to bind a device to a driver. 447 # Before unbinding it, overwrite driver_override with empty string so that 448 # the device can be bound to any other driver 449 filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id 450 if os.path.exists(filename): 451 try: 452 f = open(filename, "w") 453 except: 454 print("Error: unbind failed for %s - Cannot open %s" 455 % (dev_id, filename)) 456 sys.exit(1) 457 try: 458 f.write("\00") 459 f.close() 460 except: 461 print("Error: unbind failed for %s - Cannot open %s" 462 % (dev_id, filename)) 463 sys.exit(1) 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 dev_list = map(dev_id_from_dev_name, dev_list) 477 for d in dev_list: 478 unbind_one(d, force) 479 480 481def bind_all(dev_list, driver, force=False): 482 """Bind method, takes a list of device locations""" 483 global devices 484 485 dev_list = map(dev_id_from_dev_name, dev_list) 486 487 for d in dev_list: 488 bind_one(d, driver, force) 489 490 # For kernels < 3.15 when binding devices to a generic driver 491 # (i.e. one that doesn't have a PCI ID table) using new_id, some devices 492 # that are not bound to any other driver could be bound even if no one has 493 # asked them to. hence, we check the list of drivers again, and see if 494 # some of the previously-unbound devices were erroneously bound. 495 if not os.path.exists("/sys/bus/pci/devices/%s/driver_override" % d): 496 for d in devices.keys(): 497 # skip devices that were already bound or that we know should be bound 498 if "Driver_str" in devices[d] or d in dev_list: 499 continue 500 501 # update information about this device 502 devices[d] = dict(devices[d].items() + 503 get_pci_device_details(d, True).items()) 504 505 # check if updated information indicates that the device was bound 506 if "Driver_str" in devices[d]: 507 unbind_one(d, force) 508 509 510def display_devices(title, dev_list, extra_params=None): 511 '''Displays to the user the details of a list of devices given in 512 "dev_list". The "extra_params" parameter, if given, should contain a string 513 with %()s fields in it for replacement by the named fields in each 514 device's dictionary.''' 515 strings = [] # this holds the strings to print. We sort before printing 516 print("\n%s" % title) 517 print("="*len(title)) 518 if len(dev_list) == 0: 519 strings.append("<none>") 520 else: 521 for dev in dev_list: 522 if extra_params is not None: 523 strings.append("%s '%s %s' %s" % (dev["Slot"], 524 dev["Device_str"], 525 dev["Device"], 526 extra_params % dev)) 527 else: 528 strings.append("%s '%s'" % (dev["Slot"], dev["Device_str"])) 529 # sort before printing, so that the entries appear in PCI order 530 strings.sort() 531 print("\n".join(strings)) # print one per line 532 533def show_device_status(devices_type, device_name): 534 global dpdk_drivers 535 kernel_drv = [] 536 dpdk_drv = [] 537 no_drv = [] 538 539 # split our list of network devices into the three categories above 540 for d in devices.keys(): 541 if device_type_match(devices[d], devices_type): 542 if not has_driver(d): 543 no_drv.append(devices[d]) 544 continue 545 if devices[d]["Driver_str"] in dpdk_drivers: 546 dpdk_drv.append(devices[d]) 547 else: 548 kernel_drv.append(devices[d]) 549 550 n_devs = len(dpdk_drv) + len(kernel_drv) + len(no_drv) 551 552 # don't bother displaying anything if there are no devices 553 if n_devs == 0: 554 msg = "No '%s' devices detected" % device_name 555 print("") 556 print(msg) 557 print("".join('=' * len(msg))) 558 return 559 560 # print each category separately, so we can clearly see what's used by DPDK 561 if len(dpdk_drv) != 0: 562 display_devices("%s devices using DPDK-compatible driver" % device_name, 563 dpdk_drv, "drv=%(Driver_str)s unused=%(Module_str)s") 564 if len(kernel_drv) != 0: 565 display_devices("%s devices using kernel driver" % device_name, kernel_drv, 566 "if=%(Interface)s drv=%(Driver_str)s " 567 "unused=%(Module_str)s %(Active)s") 568 if len(no_drv) != 0: 569 display_devices("Other %s devices" % device_name, no_drv, 570 "unused=%(Module_str)s") 571 572def show_status(): 573 '''Function called when the script is passed the "--status" option. 574 Displays to the user what devices are bound to the igb_uio driver, the 575 kernel driver or to no driver''' 576 577 if status_dev == "net" or status_dev == "all": 578 show_device_status(network_devices, "Network") 579 580 if status_dev == "crypto" or status_dev == "all": 581 show_device_status(crypto_devices, "Crypto") 582 583 if status_dev == "event" or status_dev == "all": 584 show_device_status(eventdev_devices, "Eventdev") 585 586 if status_dev == "mempool" or status_dev == "all": 587 show_device_status(mempool_devices, "Mempool") 588 589 if status_dev == "compress" or status_dev == "all": 590 show_device_status(compress_devices , "Compress") 591 592 593def parse_args(): 594 '''Parses the command-line arguments given by the user and takes the 595 appropriate action for each''' 596 global b_flag 597 global status_flag 598 global status_dev 599 global force_flag 600 global args 601 if len(sys.argv) <= 1: 602 usage() 603 sys.exit(0) 604 605 try: 606 opts, args = getopt.getopt(sys.argv[1:], "b:us", 607 ["help", "usage", "status", "status-dev=", 608 "force", "bind=", "unbind", ]) 609 except getopt.GetoptError as error: 610 print(str(error)) 611 print("Run '%s --usage' for further information" % sys.argv[0]) 612 sys.exit(1) 613 614 for opt, arg in opts: 615 if opt == "--help" or opt == "--usage": 616 usage() 617 sys.exit(0) 618 if opt == "--status-dev": 619 status_flag = True 620 status_dev = arg 621 if opt == "--status" or opt == "-s": 622 status_flag = True 623 status_dev = "all" 624 if opt == "--force": 625 force_flag = True 626 if opt == "-b" or opt == "-u" or opt == "--bind" or opt == "--unbind": 627 if b_flag is not None: 628 print("Error - Only one bind or unbind may be specified\n") 629 sys.exit(1) 630 if opt == "-u" or opt == "--unbind": 631 b_flag = "none" 632 else: 633 b_flag = arg 634 635 636def do_arg_actions(): 637 '''do the actual action requested by the user''' 638 global b_flag 639 global status_flag 640 global force_flag 641 global args 642 643 if b_flag is None and not status_flag: 644 print("Error: No action specified for devices." 645 "Please give a -b or -u option") 646 print("Run '%s --usage' for further information" % sys.argv[0]) 647 sys.exit(1) 648 649 if b_flag is not None and len(args) == 0: 650 print("Error: No devices specified.") 651 print("Run '%s --usage' for further information" % sys.argv[0]) 652 sys.exit(1) 653 654 if b_flag == "none" or b_flag == "None": 655 unbind_all(args, force_flag) 656 elif b_flag is not None: 657 bind_all(args, b_flag, force_flag) 658 if status_flag: 659 if b_flag is not None: 660 clear_data() 661 # refresh if we have changed anything 662 get_device_details(network_devices) 663 get_device_details(crypto_devices) 664 get_device_details(eventdev_devices) 665 get_device_details(mempool_devices) 666 get_device_details(compress_devices) 667 show_status() 668 669 670def main(): 671 '''program main function''' 672 # check if lspci is installed, suppress any output 673 with open(os.devnull, 'w') as devnull: 674 ret = subprocess.call(['which', 'lspci'], 675 stdout=devnull, stderr=devnull) 676 if ret != 0: 677 print("'lspci' not found - please install 'pciutils'") 678 sys.exit(1) 679 parse_args() 680 check_modules() 681 clear_data() 682 get_device_details(network_devices) 683 get_device_details(crypto_devices) 684 get_device_details(eventdev_devices) 685 get_device_details(mempool_devices) 686 get_device_details(compress_devices) 687 do_arg_actions() 688 689if __name__ == "__main__": 690 main() 691