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