blob: 140e4346894374cce585de52bdf6a172a7aa5fbe [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#! /usr/bin/env python3
John DeNisco68b0ee32017-09-27 16:35:23 -04002#
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
35import sys
36import os
37import getopt
38import subprocess
39from os.path import exists, abspath, dirname, basename
40
41# The PCI base class for NETWORK devices
42NETWORK_BASE_CLASS = "02"
43CRYPTO_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
47devices = {}
48# list of supported DPDK drivers
49dpdk_drivers = ["igb_uio", "vfio-pci", "uio_pci_generic"]
50
51# command-line arg flags
52b_flag = None
53status_flag = False
54force_flag = False
55args = []
56
57
58def usage():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020059 """Print usage information for the program"""
John DeNisco68b0ee32017-09-27 16:35:23 -040060 argv0 = basename(sys.argv[0])
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020061 print(
62 """
John DeNisco68b0ee32017-09-27 16:35:23 -040063Usage:
64------
65
66 %(argv0)s [options] DEVICE1 DEVICE2 ....
67
68where DEVICE1, DEVICE2 etc, are specified via PCI "domain:bus:slot.func" syntax
69or "bus:slot.func" syntax. For devices bound to Linux kernel drivers, they may
70also be referred to by Linux interface name e.g. eth0, eth1, em0, em1, etc.
71
72Options:
73 --help, --usage:
74 Display usage information and quit
75
76 -s, --status:
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
85 e.g. unused=igb_uio
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
88 place.
89
90 -b driver, --bind=driver:
91 Select the driver to use or \"none\" to unbind the device
92
93 -u, --unbind:
94 Unbind a device (Equivalent to \"-b none\")
95
96 --force:
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
100 unbound.
101 WARNING: This can lead to loss of network connection and should be used
102 with caution.
103
104Examples:
105---------
106
107To display current device status:
108 %(argv0)s --status
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
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200119 """
120 % locals()
121 ) # replace items from local variables
John DeNisco68b0ee32017-09-27 16:35:23 -0400122
123
124# This is roughly compatible with check_output function in subprocess module
125# which is only available in python 2.7.
126def check_output(args, stderr=None):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200127 """Run a command and capture its output"""
128 return subprocess.Popen(args, stdout=subprocess.PIPE, stderr=stderr).communicate()[
129 0
130 ]
John DeNisco68b0ee32017-09-27 16:35:23 -0400131
132
133def find_module(mod):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200134 """find the .ko file for kernel module named mod.
John DeNisco68b0ee32017-09-27 16:35:23 -0400135 Searches the $RTE_SDK/$RTE_TARGET directory, the kernel
136 modules directory and finally under the parent directory of
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200137 the script"""
John DeNisco68b0ee32017-09-27 16:35:23 -0400138 # check $RTE_SDK/$RTE_TARGET directory
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200139 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"],
143 mod,
144 )
John DeNisco68b0ee32017-09-27 16:35:23 -0400145 if exists(path):
146 return path
147
148 # check using depmod
149 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200150 depmod_out = check_output(
151 ["modinfo", "-n", mod], stderr=subprocess.STDOUT
152 ).lower()
John DeNisco68b0ee32017-09-27 16:35:23 -0400153 if "error" not in depmod_out:
154 path = depmod_out.strip()
155 if exists(path):
156 return path
157 except: # if modinfo can't find module, it fails, so continue
158 pass
159
160 # check for a copy based off current path
161 tools_dir = dirname(abspath(sys.argv[0]))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200162 if tools_dir.endswith("tools"):
John DeNisco68b0ee32017-09-27 16:35:23 -0400163 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]
167 if exists(path):
168 return path
169
170
171def check_modules():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200172 """Checks that igb_uio is loaded"""
John DeNisco68b0ee32017-09-27 16:35:23 -0400173 global dpdk_drivers
174
175 # list of supported modules
176 mods = [{"Name": driver, "Found": False} for driver in dpdk_drivers]
177
178 # first check if module is loaded
179 try:
180 # Get list of sysfs modules (both built-in and dynamically loaded)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200181 sysfs_path = "/sys/module/"
John DeNisco68b0ee32017-09-27 16:35:23 -0400182
183 # Get the list of directories in sysfs_path
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200184 sysfs_mods = [
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))
188 ]
John DeNisco68b0ee32017-09-27 16:35:23 -0400189
190 # Extract the last element of '/sys/module/abc' in the array
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200191 sysfs_mods = [a.split("/")[-1] for a in sysfs_mods]
John DeNisco68b0ee32017-09-27 16:35:23 -0400192
193 # special case for vfio_pci (module is named vfio-pci,
194 # but its .ko is named vfio_pci)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200195 sysfs_mods = map(lambda a: a if a != "vfio_pci" else "vfio-pci", sysfs_mods)
John DeNisco68b0ee32017-09-27 16:35:23 -0400196
197 for mod in mods:
198 if mod["Name"] in sysfs_mods:
199 mod["Found"] = True
200 except:
201 pass
202
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")
207 sys.exit(1)
208 else:
209 print("Warning - no supported modules(DPDK driver) are loaded")
210
211 # change DPDK driver list to only contain drivers that are loaded
212 dpdk_drivers = [mod["Name"] for mod in mods if mod["Found"]]
213
214
215def has_driver(dev_id):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200216 """return true if a device is assigned to a driver. False otherwise"""
John DeNisco68b0ee32017-09-27 16:35:23 -0400217 return "Driver_str" in devices[dev_id]
218
219
220def get_pci_device_details(dev_id):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200221 """This function gets additional details for a PCI device"""
John DeNisco68b0ee32017-09-27 16:35:23 -0400222 device = {}
223
224 extra_info = check_output(["lspci", "-vmmks", dev_id]).splitlines()
225
226 # parse lspci details
227 for line in extra_info:
228 if len(line) == 0:
229 continue
230 name, value = line.decode().split("\t", 1)
231 name = name.strip(":") + "_str"
232 device[name] = value
233 # check for a unix interface name
234 device["Interface"] = ""
235 for base, dirs, _ in os.walk("/sys/bus/pci/devices/%s/" % dev_id):
236 if "net" in dirs:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200237 device["Interface"] = ",".join(os.listdir(os.path.join(base, "net")))
John DeNisco68b0ee32017-09-27 16:35:23 -0400238 break
239 # check if a port is used for ssh connection
240 device["Ssh_if"] = False
241 device["Active"] = ""
242
243 return device
244
245
246def get_nic_details():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200247 """This function populates the "devices" dictionary. The keys used are
John DeNisco68b0ee32017-09-27 16:35:23 -0400248 the pci addresses (domain:bus:slot.func). The values are themselves
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200249 dictionaries - one for each NIC."""
John DeNisco68b0ee32017-09-27 16:35:23 -0400250 global devices
251 global dpdk_drivers
252
253 # clear any old data
254 devices = {}
255 # first loop through and read details for all devices
256 # request machine readable format, with numeric IDs
257 dev = {}
258 dev_lines = check_output(["lspci", "-Dvmmn"]).splitlines()
259 for dev_line in dev_lines:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200260 if len(dev_line) == 0:
John DeNisco68b0ee32017-09-27 16:35:23 -0400261 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)
267 else:
268 name, value = dev_line.decode().split("\t", 1)
269 dev[name.rstrip(":")] = value
270
271 # check what is the interface if any for an ssh connection if
272 # any to this host, so we can mark it later.
273 ssh_if = []
274 route = check_output(["ip", "-o", "route"])
275 # filter out all lines for 169.254 routes
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200276 route = "\n".join(
277 filter(lambda ln: not ln.startswith("169.254"), route.decode().splitlines())
278 )
John DeNisco68b0ee32017-09-27 16:35:23 -0400279 rt_info = route.split()
280 for i in range(len(rt_info) - 1):
281 if rt_info[i] == "dev":
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200282 ssh_if.append(rt_info[i + 1])
John DeNisco68b0ee32017-09-27 16:35:23 -0400283
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())
289
290 for _if in ssh_if:
291 if _if in devices[d]["Interface"].split(","):
292 devices[d]["Ssh_if"] = True
293 devices[d]["Active"] = "*Active*"
294 break
295
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"]:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200300 devices[d]["Module_str"] = devices[d]["Module_str"] + ",%s" % driver
John DeNisco68b0ee32017-09-27 16:35:23 -0400301 else:
302 devices[d]["Module_str"] = ",".join(dpdk_drivers)
303
304 # make sure the driver and module strings do not have any duplicates
305 if has_driver(d):
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)
310
311
312def get_crypto_details():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200313 """This function populates the "devices" dictionary. The keys used are
John DeNisco68b0ee32017-09-27 16:35:23 -0400314 the pci addresses (domain:bus:slot.func). The values are themselves
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200315 dictionaries - one for each NIC."""
John DeNisco68b0ee32017-09-27 16:35:23 -0400316 global devices
317 global dpdk_drivers
318
319 # clear any old data
320 # devices = {}
321 # first loop through and read details for all devices
322 # request machine readable format, with numeric IDs
323 dev = {}
324 dev_lines = check_output(["lspci", "-Dvmmn"]).splitlines()
325 for dev_line in dev_lines:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200326 if len(dev_line) == 0:
327 if dev["Class"][0:2] == CRYPTO_BASE_CLASS:
John DeNisco68b0ee32017-09-27 16:35:23 -0400328 # 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)
333 else:
334 name, value = dev_line.decode().split("\t", 1)
335 dev[name.rstrip(":")] = value
336
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())
342
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"]:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200347 devices[d]["Module_str"] = devices[d]["Module_str"] + ",%s" % driver
John DeNisco68b0ee32017-09-27 16:35:23 -0400348 else:
349 devices[d]["Module_str"] = ",".join(dpdk_drivers)
350
351 # make sure the driver and module strings do not have any duplicates
352 if has_driver(d):
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)
357
358
359def dev_id_from_dev_name(dev_name):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200360 """Take a device "name" - a string passed in by user to identify a NIC
John DeNisco68b0ee32017-09-27 16:35:23 -0400361 device, and determine the device id - i.e. the domain:bus:slot.func - for
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200362 it, which can then be used to index into the devices array"""
John DeNisco68b0ee32017-09-27 16:35:23 -0400363
364 # check if it's already a suitable index
365 if dev_name in devices:
366 return dev_name
367 # check if it's an index just missing the domain part
368 elif "0000:" + dev_name in devices:
369 return "0000:" + dev_name
370 else:
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
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200376 print(
377 "Unknown device: %s. "
378 'Please specify device in "bus:slot.func" format' % dev_name
379 )
John DeNisco68b0ee32017-09-27 16:35:23 -0400380 sys.exit(1)
381
382
383def unbind_one(dev_id, force):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200384 """Unbind the device identified by "dev_id" from its current driver"""
John DeNisco68b0ee32017-09-27 16:35:23 -0400385 dev = devices[dev_id]
386 if not has_driver(dev_id):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200387 print(
388 "%s %s %s is not currently managed by any driver\n"
389 % (dev["Slot"], dev["Device_str"], dev["Interface"])
390 )
John DeNisco68b0ee32017-09-27 16:35:23 -0400391 return
392
393 # prevent us disconnecting ourselves
394 if dev["Ssh_if"] and not force:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200395 print(
396 "Routing table indicates that interface %s is active. "
397 "Skipping unbind" % (dev_id)
398 )
John DeNisco68b0ee32017-09-27 16:35:23 -0400399 return
400
401 # write to /sys to unbind
402 filename = "/sys/bus/pci/drivers/%s/unbind" % dev["Driver_str"]
403 try:
404 f = open(filename, "a")
405 except:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200406 print("Error: unbind failed for %s - Cannot open %s" % (dev_id, filename))
John DeNisco68b0ee32017-09-27 16:35:23 -0400407 sys.exit(1)
408 f.write(dev_id)
409 f.close()
410
411
412def bind_one(dev_id, driver, force):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200413 """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"""
John DeNisco68b0ee32017-09-27 16:35:23 -0400415 dev = devices[dev_id]
416 saved_driver = None # used to rollback any unbind in case of failure
417
418 # prevent disconnection of our ssh session
419 if dev["Ssh_if"] and not force:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200420 print(
421 "Routing table indicates that interface %s is active. "
422 "Not modifying" % (dev_id)
423 )
John DeNisco68b0ee32017-09-27 16:35:23 -0400424 return
425
426 # unbind any existing drivers we don't want
427 if has_driver(dev_id):
428 if dev["Driver_str"] == driver:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200429 print("%s already bound to driver %s, skipping\n" % (dev_id, driver))
John DeNisco68b0ee32017-09-27 16:35:23 -0400430 return
431 else:
432 saved_driver = dev["Driver_str"]
433 unbind_one(dev_id, force)
434 dev["Driver_str"] = "" # clear driver string
435
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
439 try:
440 f = open(filename, "w")
441 except:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200442 print("Error: bind failed for %s - Cannot open %s" % (dev_id, filename))
John DeNisco68b0ee32017-09-27 16:35:23 -0400443 return
444 try:
445 f.write("%04x %04x" % (dev["Vendor"], dev["Device"]))
446 f.close()
447 except:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200448 print(
449 "Error: bind failed for %s - Cannot write new PCI ID to "
450 "driver %s" % (dev_id, driver)
451 )
John DeNisco68b0ee32017-09-27 16:35:23 -0400452 return
453
454 # do the bind by writing to /sys
455 filename = "/sys/bus/pci/drivers/%s/bind" % driver
456 try:
457 f = open(filename, "a")
458 except:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200459 print("Error: bind failed for %s - Cannot open %s" % (dev_id, filename))
John DeNisco68b0ee32017-09-27 16:35:23 -0400460 if saved_driver is not None: # restore any previous driver
461 bind_one(dev_id, saved_driver, force)
462 return
463 try:
464 f.write(dev_id)
465 f.close()
466 except:
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:
472 return
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200473 print("Error: bind failed for %s - Cannot bind to driver %s" % (dev_id, driver))
John DeNisco68b0ee32017-09-27 16:35:23 -0400474 if saved_driver is not None: # restore any previous driver
475 bind_one(dev_id, saved_driver, force)
476 return
477
478
479def 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)
482 for d in dev_list:
483 unbind_one(d, force)
484
485
486def bind_all(dev_list, driver, force=False):
487 """Bind method, takes a list of device locations"""
488 global devices
489
490 dev_list = map(dev_id_from_dev_name, dev_list)
491
492 for d in dev_list:
493 bind_one(d, driver, force)
494
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
499 # erroneously bound.
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
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200506 devices[d] = dict(devices[d].items() + get_pci_device_details(d).items())
John DeNisco68b0ee32017-09-27 16:35:23 -0400507
508 # check if updated information indicates that the device was bound
509 if "Driver_str" in devices[d]:
510 unbind_one(d, force)
511
512
513def display_devices(title, dev_list, extra_params=None):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200514 """Displays to the user the details of a list of devices given in
John DeNisco68b0ee32017-09-27 16:35:23 -0400515 "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
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200517 device's dictionary."""
John DeNisco68b0ee32017-09-27 16:35:23 -0400518 strings = [] # this holds the strings to print. We sort before printing
519 print("\n%s" % title)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200520 print("=" * len(title))
John DeNisco68b0ee32017-09-27 16:35:23 -0400521 if len(dev_list) == 0:
522 strings.append("<none>")
523 else:
524 for dev in dev_list:
525 if extra_params is not None:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200526 strings.append(
527 "%s '%s' %s" % (dev["Slot"], dev["Device_str"], extra_params % dev)
528 )
John DeNisco68b0ee32017-09-27 16:35:23 -0400529 else:
530 strings.append("%s '%s'" % (dev["Slot"], dev["Device_str"]))
531 # sort before printing, so that the entries appear in PCI order
532 strings.sort()
533 print("\n".join(strings)) # print one per line
534
535
536def show_status():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200537 """Function called when the script is passed the "--status" option.
John DeNisco68b0ee32017-09-27 16:35:23 -0400538 Displays to the user what devices are bound to the igb_uio driver, the
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200539 kernel driver or to no driver"""
John DeNisco68b0ee32017-09-27 16:35:23 -0400540 global dpdk_drivers
541 kernel_drv = []
542 dpdk_drv = []
543 no_drv = []
544
545 # split our list of network devices into the three categories above
546 for d in devices.keys():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200547 if NETWORK_BASE_CLASS in devices[d]["Class"]:
John DeNisco68b0ee32017-09-27 16:35:23 -0400548 if not has_driver(d):
549 no_drv.append(devices[d])
550 continue
551 if devices[d]["Driver_str"] in dpdk_drivers:
552 dpdk_drv.append(devices[d])
553 else:
554 kernel_drv.append(devices[d])
555
556 # print each category separately, so we can clearly see what's used by DPDK
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200557 display_devices(
558 "Network devices using DPDK-compatible driver",
559 dpdk_drv,
560 "drv=%(Driver_str)s unused=%(Module_str)s",
561 )
562 display_devices(
563 "Network devices using kernel driver",
564 kernel_drv,
565 "if=%(Interface)s drv=%(Driver_str)s " "unused=%(Module_str)s %(Active)s",
566 )
John DeNisco68b0ee32017-09-27 16:35:23 -0400567 display_devices("Other network devices", no_drv, "unused=%(Module_str)s")
568
569 # split our list of crypto devices into the three categories above
570 kernel_drv = []
571 dpdk_drv = []
572 no_drv = []
573
574 for d in devices.keys():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200575 if CRYPTO_BASE_CLASS in devices[d]["Class"]:
John DeNisco68b0ee32017-09-27 16:35:23 -0400576 if not has_driver(d):
577 no_drv.append(devices[d])
578 continue
579 if devices[d]["Driver_str"] in dpdk_drivers:
580 dpdk_drv.append(devices[d])
581 else:
582 kernel_drv.append(devices[d])
583
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200584 display_devices(
585 "Crypto devices using DPDK-compatible driver",
586 dpdk_drv,
587 "drv=%(Driver_str)s unused=%(Module_str)s",
588 )
589 display_devices(
590 "Crypto devices using kernel driver",
591 kernel_drv,
592 "drv=%(Driver_str)s " "unused=%(Module_str)s",
593 )
John DeNisco68b0ee32017-09-27 16:35:23 -0400594 display_devices("Other crypto devices", no_drv, "unused=%(Module_str)s")
595
596
597def parse_args():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200598 """Parses the command-line arguments given by the user and takes the
599 appropriate action for each"""
John DeNisco68b0ee32017-09-27 16:35:23 -0400600 global b_flag
601 global status_flag
602 global force_flag
603 global args
604 if len(sys.argv) <= 1:
605 usage()
606 sys.exit(0)
607
608 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200609 opts, args = getopt.getopt(
610 sys.argv[1:],
611 "b:us",
612 ["help", "usage", "status", "force", "bind=", "unbind"],
613 )
John DeNisco68b0ee32017-09-27 16:35:23 -0400614 except getopt.GetoptError as error:
615 print(str(error))
616 print("Run '%s --usage' for further information" % sys.argv[0])
617 sys.exit(1)
618
619 for opt, arg in opts:
620 if opt == "--help" or opt == "--usage":
621 usage()
622 sys.exit(0)
623 if opt == "--status" or opt == "-s":
624 status_flag = True
625 if opt == "--force":
626 force_flag = True
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")
630 sys.exit(1)
631 if opt == "-u" or opt == "--unbind":
632 b_flag = "none"
633 else:
634 b_flag = arg
635
636
637def do_arg_actions():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200638 """do the actual action requested by the user"""
John DeNisco68b0ee32017-09-27 16:35:23 -0400639 global b_flag
640 global status_flag
641 global force_flag
642 global args
643
644 if b_flag is None and not status_flag:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200645 print("Error: No action specified for devices." "Please give a -b or -u option")
John DeNisco68b0ee32017-09-27 16:35:23 -0400646 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 get_nic_details() # refresh if we have changed anything
661 get_crypto_details() # refresh if we have changed anything
662 show_status()
663
664
665def main():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200666 """program main function"""
John DeNisco68b0ee32017-09-27 16:35:23 -0400667 parse_args()
668 check_modules()
669 get_nic_details()
670 get_crypto_details()
671 do_arg_actions()
672
Paul Vinciguerra339bc6b2018-12-19 02:05:25 -0800673
John DeNisco68b0ee32017-09-27 16:35:23 -0400674if __name__ == "__main__":
675 main()