1 #! /usr/bin/env python3
5 # Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in
16 # the documentation and/or other materials provided with the
18 # * Neither the name of Intel Corporation nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 from os.path import exists, abspath, dirname, basename
41 # The PCI base class for NETWORK devices
42 NETWORK_BASE_CLASS = "02"
43 CRYPTO_BASE_CLASS = "0b"
45 # global dict ethernet devices present. Dictionary indexed by PCI address.
46 # Each device within this is itself a dictionary of device properties
48 # list of supported DPDK drivers
49 dpdk_drivers = ["igb_uio", "vfio-pci", "uio_pci_generic"]
51 # command-line arg flags
59 """Print usage information for the program"""
60 argv0 = basename(sys.argv[0])
66 %(argv0)s [options] DEVICE1 DEVICE2 ....
68 where DEVICE1, DEVICE2 etc, are specified via PCI "domain:bus:slot.func" syntax
69 or "bus:slot.func" syntax. For devices bound to Linux kernel drivers, they may
70 also be referred to by Linux interface name e.g. eth0, eth1, em0, em1, etc.
74 Display usage information and quit
77 Print the current status of all known network and crypto devices.
78 For each device, it displays the PCI domain, bus, slot and function,
79 along with a text description of the device. Depending upon whether the
80 device is being used by a kernel driver, the igb_uio driver, or no
81 driver, other relevant information will be displayed:
82 * the Linux interface name e.g. if=eth0
83 * the driver being used e.g. drv=igb_uio
84 * any suitable drivers not currently using that device
86 NOTE: if this flag is passed along with a bind/unbind option, the
87 status display will always occur after the other operations have taken
90 -b driver, --bind=driver:
91 Select the driver to use or \"none\" to unbind the device
94 Unbind a device (Equivalent to \"-b none\")
97 By default, network devices which are used by Linux - as indicated by having
98 routes in the routing table - cannot be modified. Using the --force
99 flag overrides this behavior, allowing active links to be forcibly
101 WARNING: This can lead to loss of network connection and should be used
107 To display current device status:
110 To bind eth1 from the current driver and move to use igb_uio
111 %(argv0)s --bind=igb_uio eth1
113 To unbind 0000:01:00.0 from using any driver
114 %(argv0)s -u 0000:01:00.0
116 To 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
121 ) # replace items from local variables
124 # This is roughly compatible with check_output function in subprocess module
125 # which is only available in python 2.7.
126 def check_output(args, stderr=None):
127 """Run a command and capture its output"""
128 return subprocess.Popen(args, stdout=subprocess.PIPE, stderr=stderr).communicate()[
133 def find_module(mod):
134 """find the .ko file for kernel module named mod.
135 Searches the $RTE_SDK/$RTE_TARGET directory, the kernel
136 modules directory and finally under the parent directory of
138 # check $RTE_SDK/$RTE_TARGET directory
139 if "RTE_SDK" in os.environ and "RTE_TARGET" in os.environ:
140 path = "%s/%s/kmod/%s.ko" % (
141 os.environ["RTE_SDK"],
142 os.environ["RTE_TARGET"],
150 depmod_out = check_output(
151 ["modinfo", "-n", mod], stderr=subprocess.STDOUT
153 if "error" not in depmod_out:
154 path = depmod_out.strip()
157 except: # if modinfo can't find module, it fails, so continue
160 # check for a copy based off current path
161 tools_dir = dirname(abspath(sys.argv[0]))
162 if tools_dir.endswith("tools"):
163 base_dir = dirname(tools_dir)
164 find_out = check_output(["find", base_dir, "-name", mod + ".ko"])
165 if len(find_out) > 0: # something matched
166 path = find_out.splitlines()[0]
172 """Checks that igb_uio is loaded"""
175 # list of supported modules
176 mods = [{"Name": driver, "Found": False} for driver in dpdk_drivers]
178 # first check if module is loaded
180 # Get list of sysfs modules (both built-in and dynamically loaded)
181 sysfs_path = "/sys/module/"
183 # Get the list of directories in sysfs_path
185 os.path.join(sysfs_path, o)
186 for o in os.listdir(sysfs_path)
187 if os.path.isdir(os.path.join(sysfs_path, o))
190 # Extract the last element of '/sys/module/abc' in the array
191 sysfs_mods = [a.split("/")[-1] for a in sysfs_mods]
193 # special case for vfio_pci (module is named vfio-pci,
194 # but its .ko is named vfio_pci)
195 sysfs_mods = map(lambda a: a if a != "vfio_pci" else "vfio-pci", sysfs_mods)
198 if mod["Name"] in sysfs_mods:
203 # check if we have at least one loaded module
204 if True not in [mod["Found"] for mod in mods] and b_flag is not None:
205 if b_flag in dpdk_drivers:
206 print("Error - no supported modules(DPDK driver) are loaded")
209 print("Warning - no supported modules(DPDK driver) are loaded")
211 # change DPDK driver list to only contain drivers that are loaded
212 dpdk_drivers = [mod["Name"] for mod in mods if mod["Found"]]
215 def has_driver(dev_id):
216 """return true if a device is assigned to a driver. False otherwise"""
217 return "Driver_str" in devices[dev_id]
220 def get_pci_device_details(dev_id):
221 """This function gets additional details for a PCI device"""
224 extra_info = check_output(["lspci", "-vmmks", dev_id]).splitlines()
226 # parse lspci details
227 for line in extra_info:
230 name, value = line.decode().split("\t", 1)
231 name = name.strip(":") + "_str"
233 # check for a unix interface name
234 device["Interface"] = ""
235 for base, dirs, _ in os.walk("/sys/bus/pci/devices/%s/" % dev_id):
237 device["Interface"] = ",".join(os.listdir(os.path.join(base, "net")))
239 # check if a port is used for ssh connection
240 device["Ssh_if"] = False
241 device["Active"] = ""
246 def get_nic_details():
247 """This function populates the "devices" dictionary. The keys used are
248 the pci addresses (domain:bus:slot.func). The values are themselves
249 dictionaries - one for each NIC."""
255 # first loop through and read details for all devices
256 # request machine readable format, with numeric IDs
258 dev_lines = check_output(["lspci", "-Dvmmn"]).splitlines()
259 for dev_line in dev_lines:
260 if len(dev_line) == 0:
261 if dev["Class"][0:2] == NETWORK_BASE_CLASS:
262 # convert device and vendor ids to numbers, then add to global
263 dev["Vendor"] = int(dev["Vendor"], 16)
264 dev["Device"] = int(dev["Device"], 16)
265 # use dict to make copy of dev
266 devices[dev["Slot"]] = dict(dev)
268 name, value = dev_line.decode().split("\t", 1)
269 dev[name.rstrip(":")] = value
271 # check what is the interface if any for an ssh connection if
272 # any to this host, so we can mark it later.
274 route = check_output(["ip", "-o", "route"])
275 # filter out all lines for 169.254 routes
277 filter(lambda ln: not ln.startswith("169.254"), route.decode().splitlines())
279 rt_info = route.split()
280 for i in range(len(rt_info) - 1):
281 if rt_info[i] == "dev":
282 ssh_if.append(rt_info[i + 1])
284 # based on the basic info, get extended text details
285 for d in devices.keys():
286 # get additional info and add it to existing data
287 devices[d] = devices[d].copy()
288 devices[d].update(get_pci_device_details(d).items())
291 if _if in devices[d]["Interface"].split(","):
292 devices[d]["Ssh_if"] = True
293 devices[d]["Active"] = "*Active*"
296 # add igb_uio to list of supporting modules if needed
297 if "Module_str" in devices[d]:
298 for driver in dpdk_drivers:
299 if driver not in devices[d]["Module_str"]:
300 devices[d]["Module_str"] = devices[d]["Module_str"] + ",%s" % driver
302 devices[d]["Module_str"] = ",".join(dpdk_drivers)
304 # make sure the driver and module strings do not have any duplicates
306 modules = devices[d]["Module_str"].split(",")
307 if devices[d]["Driver_str"] in modules:
308 modules.remove(devices[d]["Driver_str"])
309 devices[d]["Module_str"] = ",".join(modules)
312 def get_crypto_details():
313 """This function populates the "devices" dictionary. The keys used are
314 the pci addresses (domain:bus:slot.func). The values are themselves
315 dictionaries - one for each NIC."""
321 # first loop through and read details for all devices
322 # request machine readable format, with numeric IDs
324 dev_lines = check_output(["lspci", "-Dvmmn"]).splitlines()
325 for dev_line in dev_lines:
326 if len(dev_line) == 0:
327 if dev["Class"][0:2] == CRYPTO_BASE_CLASS:
328 # convert device and vendor ids to numbers, then add to global
329 dev["Vendor"] = int(dev["Vendor"], 16)
330 dev["Device"] = int(dev["Device"], 16)
331 # use dict to make copy of dev
332 devices[dev["Slot"]] = dict(dev)
334 name, value = dev_line.decode().split("\t", 1)
335 dev[name.rstrip(":")] = value
337 # based on the basic info, get extended text details
338 for d in devices.keys():
339 # get additional info and add it to existing data
340 devices[d] = devices[d].copy()
341 devices[d].update(get_pci_device_details(d).items())
343 # add igb_uio to list of supporting modules if needed
344 if "Module_str" in devices[d]:
345 for driver in dpdk_drivers:
346 if driver not in devices[d]["Module_str"]:
347 devices[d]["Module_str"] = devices[d]["Module_str"] + ",%s" % driver
349 devices[d]["Module_str"] = ",".join(dpdk_drivers)
351 # make sure the driver and module strings do not have any duplicates
353 modules = devices[d]["Module_str"].split(",")
354 if devices[d]["Driver_str"] in modules:
355 modules.remove(devices[d]["Driver_str"])
356 devices[d]["Module_str"] = ",".join(modules)
359 def dev_id_from_dev_name(dev_name):
360 """Take a device "name" - a string passed in by user to identify a NIC
361 device, and determine the device id - i.e. the domain:bus:slot.func - for
362 it, which can then be used to index into the devices array"""
364 # check if it's already a suitable index
365 if dev_name in devices:
367 # check if it's an index just missing the domain part
368 elif "0000:" + dev_name in devices:
369 return "0000:" + dev_name
371 # check if it's an interface name, e.g. eth1
372 for d in devices.keys():
373 if dev_name in devices[d]["Interface"].split(","):
374 return devices[d]["Slot"]
375 # if nothing else matches - error
377 "Unknown device: %s. "
378 'Please specify device in "bus:slot.func" format' % dev_name
383 def unbind_one(dev_id, force):
384 """Unbind the device identified by "dev_id" from its current driver"""
385 dev = devices[dev_id]
386 if not has_driver(dev_id):
388 "%s %s %s is not currently managed by any driver\n"
389 % (dev["Slot"], dev["Device_str"], dev["Interface"])
393 # prevent us disconnecting ourselves
394 if dev["Ssh_if"] and not force:
396 "Routing table indicates that interface %s is active. "
397 "Skipping unbind" % (dev_id)
401 # write to /sys to unbind
402 filename = "/sys/bus/pci/drivers/%s/unbind" % dev["Driver_str"]
404 f = open(filename, "a")
406 print("Error: unbind failed for %s - Cannot open %s" % (dev_id, filename))
412 def bind_one(dev_id, driver, force):
413 """Bind the device given by "dev_id" to the driver "driver". If the device
414 is already bound to a different driver, it will be unbound first"""
415 dev = devices[dev_id]
416 saved_driver = None # used to rollback any unbind in case of failure
418 # prevent disconnection of our ssh session
419 if dev["Ssh_if"] and not force:
421 "Routing table indicates that interface %s is active. "
422 "Not modifying" % (dev_id)
426 # unbind any existing drivers we don't want
427 if has_driver(dev_id):
428 if dev["Driver_str"] == driver:
429 print("%s already bound to driver %s, skipping\n" % (dev_id, driver))
432 saved_driver = dev["Driver_str"]
433 unbind_one(dev_id, force)
434 dev["Driver_str"] = "" # clear driver string
436 # if we are binding to one of DPDK drivers, add PCI id's to that driver
437 if driver in dpdk_drivers:
438 filename = "/sys/bus/pci/drivers/%s/new_id" % driver
440 f = open(filename, "w")
442 print("Error: bind failed for %s - Cannot open %s" % (dev_id, filename))
445 f.write("%04x %04x" % (dev["Vendor"], dev["Device"]))
449 "Error: bind failed for %s - Cannot write new PCI ID to "
450 "driver %s" % (dev_id, driver)
454 # do the bind by writing to /sys
455 filename = "/sys/bus/pci/drivers/%s/bind" % driver
457 f = open(filename, "a")
459 print("Error: bind failed for %s - Cannot open %s" % (dev_id, filename))
460 if saved_driver is not None: # restore any previous driver
461 bind_one(dev_id, saved_driver, force)
467 # for some reason, closing dev_id after adding a new PCI ID to new_id
468 # results in IOError. however, if the device was successfully bound,
469 # we don't care for any errors and can safely ignore IOError
470 tmp = get_pci_device_details(dev_id)
471 if "Driver_str" in tmp and tmp["Driver_str"] == driver:
473 print("Error: bind failed for %s - Cannot bind to driver %s" % (dev_id, driver))
474 if saved_driver is not None: # restore any previous driver
475 bind_one(dev_id, saved_driver, force)
479 def unbind_all(dev_list, force=False):
480 """Unbind method, takes a list of device locations"""
481 dev_list = map(dev_id_from_dev_name, dev_list)
486 def bind_all(dev_list, driver, force=False):
487 """Bind method, takes a list of device locations"""
490 dev_list = map(dev_id_from_dev_name, dev_list)
493 bind_one(d, driver, force)
495 # when binding devices to a generic driver (i.e. one that doesn't have a
496 # PCI ID table), some devices that are not bound to any other driver could
497 # be bound even if no one has asked them to. hence, we check the list of
498 # drivers again, and see if some of the previously-unbound devices were
500 for d in devices.keys():
501 # skip devices that were already bound or that we know should be bound
502 if "Driver_str" in devices[d] or d in dev_list:
505 # update information about this device
506 devices[d] = dict(devices[d].items() + get_pci_device_details(d).items())
508 # check if updated information indicates that the device was bound
509 if "Driver_str" in devices[d]:
513 def display_devices(title, dev_list, extra_params=None):
514 """Displays to the user the details of a list of devices given in
515 "dev_list". The "extra_params" parameter, if given, should contain a string
516 with %()s fields in it for replacement by the named fields in each
517 device's dictionary."""
518 strings = [] # this holds the strings to print. We sort before printing
519 print("\n%s" % title)
520 print("=" * len(title))
521 if len(dev_list) == 0:
522 strings.append("<none>")
525 if extra_params is not None:
527 "%s '%s' %s" % (dev["Slot"], dev["Device_str"], extra_params % dev)
530 strings.append("%s '%s'" % (dev["Slot"], dev["Device_str"]))
531 # sort before printing, so that the entries appear in PCI order
533 print("\n".join(strings)) # print one per line
537 """Function called when the script is passed the "--status" option.
538 Displays to the user what devices are bound to the igb_uio driver, the
539 kernel driver or to no driver"""
545 # split our list of network devices into the three categories above
546 for d in devices.keys():
547 if NETWORK_BASE_CLASS in devices[d]["Class"]:
548 if not has_driver(d):
549 no_drv.append(devices[d])
551 if devices[d]["Driver_str"] in dpdk_drivers:
552 dpdk_drv.append(devices[d])
554 kernel_drv.append(devices[d])
556 # print each category separately, so we can clearly see what's used by DPDK
558 "Network devices using DPDK-compatible driver",
560 "drv=%(Driver_str)s unused=%(Module_str)s",
563 "Network devices using kernel driver",
565 "if=%(Interface)s drv=%(Driver_str)s " "unused=%(Module_str)s %(Active)s",
567 display_devices("Other network devices", no_drv, "unused=%(Module_str)s")
569 # split our list of crypto devices into the three categories above
574 for d in devices.keys():
575 if CRYPTO_BASE_CLASS in devices[d]["Class"]:
576 if not has_driver(d):
577 no_drv.append(devices[d])
579 if devices[d]["Driver_str"] in dpdk_drivers:
580 dpdk_drv.append(devices[d])
582 kernel_drv.append(devices[d])
585 "Crypto devices using DPDK-compatible driver",
587 "drv=%(Driver_str)s unused=%(Module_str)s",
590 "Crypto devices using kernel driver",
592 "drv=%(Driver_str)s " "unused=%(Module_str)s",
594 display_devices("Other crypto devices", no_drv, "unused=%(Module_str)s")
598 """Parses the command-line arguments given by the user and takes the
599 appropriate action for each"""
604 if len(sys.argv) <= 1:
609 opts, args = getopt.getopt(
612 ["help", "usage", "status", "force", "bind=", "unbind"],
614 except getopt.GetoptError as error:
616 print("Run '%s --usage' for further information" % sys.argv[0])
619 for opt, arg in opts:
620 if opt == "--help" or opt == "--usage":
623 if opt == "--status" or opt == "-s":
627 if opt == "-b" or opt == "-u" or opt == "--bind" or opt == "--unbind":
628 if b_flag is not None:
629 print("Error - Only one bind or unbind may be specified\n")
631 if opt == "-u" or opt == "--unbind":
637 def do_arg_actions():
638 """do the actual action requested by the user"""
644 if b_flag is None and not status_flag:
645 print("Error: No action specified for devices." "Please give a -b or -u option")
646 print("Run '%s --usage' for further information" % sys.argv[0])
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])
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)
659 if b_flag is not None:
660 get_nic_details() # refresh if we have changed anything
661 get_crypto_details() # refresh if we have changed anything
666 """program main function"""
674 if __name__ == "__main__":