2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright(c) 2016 Neil Horman <nhorman@tuxdriver.com>
5 # -------------------------------------------------------------------------
7 # Utility to dump PMD_INFO_STRING support from an object file
9 # -------------------------------------------------------------------------
10 from __future__ import print_function
11 from __future__ import unicode_literals
18 from elftools.common.exceptions import ELFError
19 from elftools.common.py3compat import byte2int
20 from elftools.elf.elffile import ELFFile
21 from optparse import OptionParser
23 # For running from development directory. It should take precedence over the
24 # installed pyelftools.
25 sys.path.insert(0, '.')
30 # ===========================================
31 if sys.version_info.major < 3:
32 print("WARNING: Python 2 is deprecated for use in DPDK, and will not work in future releases.", file=sys.stderr)
33 print("Please use Python 3 instead", file=sys.stderr)
37 Class for vendors. This is the top level class
38 for the devices belong to a specific vendor.
39 self.devices is the device dictionary
40 subdevices are in each device.
43 def __init__(self, vendorStr):
45 Class initializes with the raw line from pci.ids
46 Parsing takes place inside __init__
48 self.ID = vendorStr.split()[0]
49 self.name = vendorStr.replace("%s " % self.ID, "").rstrip()
52 def addDevice(self, deviceStr):
54 Adds a device to self.devices
55 takes the raw line from pci.ids
59 if devID in self.devices:
62 self.devices[devID] = Device(deviceStr)
65 print(self.ID, self.name)
66 for id, dev in self.devices.items():
69 def find_device(self, devid):
70 # convert to a hex string and remove 0x
71 devid = hex(devid)[2:]
73 return self.devices[devid]
75 return Device("%s Unknown Device" % devid)
80 def __init__(self, deviceStr):
82 Class for each device.
83 Each vendor has its own devices dictionary.
86 self.ID = s.split()[0]
87 self.name = s.replace("%s " % self.ID, "")
91 print("\t%s\t%s" % (self.ID, self.name))
92 for subID, subdev in self.subdevices.items():
95 def addSubDevice(self, subDeviceStr):
97 Adds a subvendor, subdevice to device.
98 Uses raw line from pci.ids
100 s = subDeviceStr.strip()
104 subDeviceName = s.split(" ")[-1]
105 devID = "%s:%s" % (subVendorID, subDeviceID)
106 self.subdevices[devID] = SubDevice(
107 subVendorID, subDeviceID, subDeviceName)
109 def find_subid(self, subven, subdev):
110 subven = hex(subven)[2:]
111 subdev = hex(subdev)[2:]
112 devid = "%s:%s" % (subven, subdev)
115 return self.subdevices[devid]
117 if (subven == "ffff" and subdev == "ffff"):
118 return SubDevice("ffff", "ffff", "(All Subdevices)")
120 return SubDevice(subven, subdev, "(Unknown Subdevice)")
125 Class for subdevices.
128 def __init__(self, vendor, device, name):
130 Class initializes with vendorid, deviceid and name
132 self.vendorID = vendor
133 self.deviceID = device
137 print("\t\t%s\t%s\t%s" % (self.vendorID, self.deviceID, self.name))
142 Top class for all pci.ids entries.
143 All queries will be asked to this class.
144 PCIIds.vendors["0e11"].devices["0046"].\
145 subdevices["0e11:4091"].name = "Smart Array 6i"
148 def __init__(self, filename):
150 Prepares the directories.
151 Checks local data file.
152 Tries to load from local, if not found, downloads from web
158 self.readLocal(filename)
161 def reportVendors(self):
162 """Reports the vendors
164 for vid, v in self.vendors.items():
167 def report(self, vendor=None):
169 Reports everything for all vendors or a specific vendor
170 PCIIds.report() reports everything
171 PCIIDs.report("0e11") reports only "Compaq Computer Corporation"
173 if vendor is not None:
174 self.vendors[vendor].report()
176 for vID, v in self.vendors.items():
179 def find_vendor(self, vid):
180 # convert vid to a hex string and remove the 0x
184 return self.vendors[vid]
186 return Vendor("%s Unknown Vendor" % (vid))
188 def findDate(self, content):
190 if l.find("Date:") > -1:
191 return l.split()[-2].replace("-", "")
195 if len(self.contents) < 1:
196 print("data/%s-pci.ids not found" % self.date)
200 for l in self.contents:
203 elif len(l.strip()) == 0:
206 if l.find("\t\t") == 0:
207 self.vendors[vendorID].devices[
208 deviceID].addSubDevice(l)
209 elif l.find("\t") == 0:
210 deviceID = l.strip().split()[0]
211 self.vendors[vendorID].addDevice(l)
213 vendorID = l.split()[0]
214 self.vendors[vendorID] = Vendor(l)
216 def readLocal(self, filename):
220 with io.open(filename, 'r', encoding='utf-8') as f:
221 self.contents = f.readlines()
222 self.date = self.findDate(self.contents)
226 Loads database from local. If there is no file,
227 it creates a new one from web
229 self.date = idsfile[0].split("/")[1].split("-")[0]
233 # =======================================
235 def search_file(filename, search_path):
236 """ Given a search path, find file with requested name """
237 for path in string.split(search_path, ":"):
238 candidate = os.path.join(path, filename)
239 if os.path.exists(candidate):
240 return os.path.abspath(candidate)
244 class ReadElf(object):
245 """ display_* methods are used to emit output into the output stream
248 def __init__(self, file, output):
250 stream object with the ELF file to read
253 output stream to write to
255 self.elffile = ELFFile(file)
258 # Lazily initialized if a debug dump is requested
259 self._dwarfinfo = None
261 self._versioninfo = None
263 def _section_from_spec(self, spec):
264 """ Retrieve a section given a "spec" (either number or name).
265 Return None if no such section exists in the file.
269 if num < self.elffile.num_sections():
270 return self.elffile.get_section(num)
274 # Not a number. Must be a name then
275 section = self.elffile.get_section_by_name(force_unicode(spec))
277 # No match with a unicode name.
278 # Some versions of pyelftools (<= 0.23) store internal strings
279 # as bytes. Try again with the name encoded as bytes.
280 section = self.elffile.get_section_by_name(force_bytes(spec))
283 def pretty_print_pmdinfo(self, pmdinfo):
286 for i in pmdinfo["pci_ids"]:
287 vendor = pcidb.find_vendor(i[0])
288 device = vendor.find_device(i[1])
289 subdev = device.find_subid(i[2], i[3])
290 print("%s (%s) : %s (%s) %s" %
291 (vendor.name, vendor.ID, device.name,
292 device.ID, subdev.name))
294 def parse_pmd_info_string(self, mystring):
298 optional_pmd_info = [
299 {'id': 'params', 'tag': 'PMD PARAMETERS'},
300 {'id': 'kmod', 'tag': 'PMD KMOD DEPENDENCIES'}
303 i = mystring.index("=")
304 mystring = mystring[i + 2:]
305 pmdinfo = json.loads(mystring)
308 print(json.dumps(pmdinfo))
311 print("PMD NAME: " + pmdinfo["name"])
312 for i in optional_pmd_info:
314 print("%s: %s" % (i['tag'], pmdinfo[i['id']]))
318 if (len(pmdinfo["pci_ids"]) != 0):
319 print("PMD HW SUPPORT:")
320 if pcidb is not None:
321 self.pretty_print_pmdinfo(pmdinfo)
323 print("VENDOR\t DEVICE\t SUBVENDOR\t SUBDEVICE")
324 for i in pmdinfo["pci_ids"]:
325 print("0x%04x\t 0x%04x\t 0x%04x\t\t 0x%04x" %
326 (i[0], i[1], i[2], i[3]))
330 def display_pmd_info_strings(self, section_spec):
331 """ Display a strings dump of a section. section_spec is either a
332 section number or a name.
334 section = self._section_from_spec(section_spec)
338 data = section.data()
341 while dataptr < len(data):
342 while (dataptr < len(data) and
343 not (32 <= byte2int(data[dataptr]) <= 127)):
346 if dataptr >= len(data):
350 while endptr < len(data) and byte2int(data[endptr]) != 0:
353 # pyelftools may return byte-strings, force decode them
354 mystring = force_unicode(data[dataptr:endptr])
355 rc = mystring.find("PMD_INFO_STRING")
357 self.parse_pmd_info_string(mystring)
361 def find_librte_eal(self, section):
362 for tag in section.iter_tags():
363 # pyelftools may return byte-strings, force decode them
364 if force_unicode(tag.entry.d_tag) == 'DT_NEEDED':
365 if "librte_eal" in force_unicode(tag.needed):
366 return force_unicode(tag.needed)
369 def search_for_autoload_path(self):
374 section = self._section_from_spec(".dynamic")
376 eallib = self.find_librte_eal(section)
377 if eallib is not None:
378 ldlibpath = os.environ.get('LD_LIBRARY_PATH')
379 if ldlibpath is None:
381 dtr = self.get_dt_runpath(section)
382 library = search_file(eallib,
383 dtr + ":" + ldlibpath +
384 ":/usr/lib64:/lib64:/usr/lib:/lib")
387 if raw_output is False:
388 print("Scanning for autoload path in %s" % library)
389 scanfile = io.open(library, 'rb')
390 scanelf = ReadElf(scanfile, sys.stdout)
391 except AttributeError:
392 # Not a dynamic binary
398 section = scanelf._section_from_spec(".rodata")
400 if scanfile is not None:
404 data = section.data()
407 while dataptr < len(data):
408 while (dataptr < len(data) and
409 not (32 <= byte2int(data[dataptr]) <= 127)):
412 if dataptr >= len(data):
416 while endptr < len(data) and byte2int(data[endptr]) != 0:
419 # pyelftools may return byte-strings, force decode them
420 mystring = force_unicode(data[dataptr:endptr])
421 rc = mystring.find("DPDK_PLUGIN_PATH")
423 rc = mystring.find("=")
424 return (mystring[rc + 1:], library)
427 if scanfile is not None:
431 def get_dt_runpath(self, dynsec):
432 for tag in dynsec.iter_tags():
433 # pyelftools may return byte-strings, force decode them
434 if force_unicode(tag.entry.d_tag) == 'DT_RUNPATH':
435 return force_unicode(tag.runpath)
438 def process_dt_needed_entries(self):
439 """ Look to see if there are any DT_NEEDED entries in the binary
440 And process those if there are
444 ldlibpath = os.environ.get('LD_LIBRARY_PATH')
445 if ldlibpath is None:
448 dynsec = self._section_from_spec(".dynamic")
450 runpath = self.get_dt_runpath(dynsec)
451 except AttributeError:
452 # dynsec is None, just return
455 for tag in dynsec.iter_tags():
456 # pyelftools may return byte-strings, force decode them
457 if force_unicode(tag.entry.d_tag) == 'DT_NEEDED':
458 if 'librte_pmd' in force_unicode(tag.needed):
459 library = search_file(force_unicode(tag.needed),
460 runpath + ":" + ldlibpath +
461 ":/usr/lib64:/lib64:/usr/lib:/lib")
462 if library is not None:
463 if raw_output is False:
464 print("Scanning %s for pmd information" % library)
465 with io.open(library, 'rb') as file:
467 libelf = ReadElf(file, sys.stdout)
469 print("%s is no an ELF file" % library)
471 libelf.process_dt_needed_entries()
472 libelf.display_pmd_info_strings(".rodata")
476 # compat: remove force_unicode & force_bytes when pyelftools<=0.23 support is
478 def force_unicode(s):
479 if hasattr(s, 'decode') and callable(s.decode):
480 s = s.decode('latin-1') # same encoding used in pyelftools py3compat
485 if hasattr(s, 'encode') and callable(s.encode):
486 s = s.encode('latin-1') # same encoding used in pyelftools py3compat
490 def scan_autoload_path(autoload_path):
493 if os.path.exists(autoload_path) is False:
497 dirs = os.listdir(autoload_path)
499 # Couldn't read the directory, give up
503 dpath = os.path.join(autoload_path, d)
504 if os.path.isdir(dpath):
505 scan_autoload_path(dpath)
506 if os.path.isfile(dpath):
508 file = io.open(dpath, 'rb')
509 readelf = ReadElf(file, sys.stdout)
511 # this is likely not an elf file, skip it
514 # No permission to read the file, skip it
517 if raw_output is False:
518 print("Hw Support for library %s" % d)
519 readelf.display_pmd_info_strings(".rodata")
523 def scan_for_autoload_pmds(dpdk_path):
525 search the specified application or path for a pmd autoload path
526 then scan said path for pmds and report hw support
530 if (os.path.isfile(dpdk_path) is False):
531 if raw_output is False:
532 print("Must specify a file name")
535 file = io.open(dpdk_path, 'rb')
537 readelf = ReadElf(file, sys.stdout)
539 if raw_output is False:
540 print("Unable to parse %s" % file)
543 (autoload_path, scannedfile) = readelf.search_for_autoload_path()
544 if not autoload_path:
545 if (raw_output is False):
546 print("No autoload path configured in %s" % dpdk_path)
548 if (raw_output is False):
549 if (scannedfile is None):
550 scannedfile = dpdk_path
551 print("Found autoload path %s in %s" % (autoload_path, scannedfile))
554 if (raw_output is False):
555 print("Discovered Autoload HW Support:")
556 scan_autoload_path(autoload_path)
560 def main(stream=None):
564 pcifile_default = "./pci.ids" # For unknown OS's assume local file
565 if platform.system() == 'Linux':
566 # hwdata is the legacy location, misc is supported going forward
567 pcifile_default = "/usr/share/misc/pci.ids"
568 if not os.path.exists(pcifile_default):
569 pcifile_default = "/usr/share/hwdata/pci.ids"
570 elif platform.system() == 'FreeBSD':
571 pcifile_default = "/usr/local/share/pciids/pci.ids"
572 if not os.path.exists(pcifile_default):
573 pcifile_default = "/usr/share/misc/pci_vendors"
575 optparser = OptionParser(
576 usage='usage: %prog [-hrtp] [-d <pci id file] <elf-file>',
577 description="Dump pmd hardware support info",
578 add_help_option=True)
579 optparser.add_option('-r', '--raw',
580 action='store_true', dest='raw_output',
581 help='Dump raw json strings')
582 optparser.add_option("-d", "--pcidb", dest="pcifile",
583 help="specify a pci database "
584 "to get vendor names from",
585 default=pcifile_default, metavar="FILE")
586 optparser.add_option("-t", "--table", dest="tblout",
587 help="output information on hw support as a "
590 optparser.add_option("-p", "--plugindir", dest="pdir",
591 help="scan dpdk for autoload plugins",
594 options, args = optparser.parse_args()
596 if options.raw_output:
600 pcidb = PCIIds(options.pcifile)
602 print("Pci DB file not found")
606 options.pcifile = None
610 optparser.print_usage()
613 if options.pdir is True:
614 exit(scan_for_autoload_pmds(args[0]))
616 ldlibpath = os.environ.get('LD_LIBRARY_PATH')
617 if (ldlibpath is None):
620 if (os.path.exists(args[0]) is True):
623 myelffile = search_file(
624 args[0], ldlibpath + ":/usr/lib64:/lib64:/usr/lib:/lib")
626 if (myelffile is None):
627 print("File not found")
630 with io.open(myelffile, 'rb') as file:
632 readelf = ReadElf(file, sys.stdout)
633 readelf.process_dt_needed_entries()
634 readelf.display_pmd_info_strings(".rodata")
637 except ELFError as ex:
638 sys.stderr.write('ELF error: %s\n' % ex)
642 # -------------------------------------------------------------------------
643 if __name__ == '__main__':