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 # -------------------------------------------------------------------------
16 from elftools.common.exceptions import ELFError
17 from elftools.common.py3compat import byte2int
18 from elftools.elf.elffile import ELFFile
19 from optparse import OptionParser
21 # For running from development directory. It should take precedence over the
22 # installed pyelftools.
23 sys.path.insert(0, '.')
28 # ===========================================
32 Class for vendors. This is the top level class
33 for the devices belong to a specific vendor.
34 self.devices is the device dictionary
35 subdevices are in each device.
38 def __init__(self, vendorStr):
40 Class initializes with the raw line from pci.ids
41 Parsing takes place inside __init__
43 self.ID = vendorStr.split()[0]
44 self.name = vendorStr.replace("%s " % self.ID, "").rstrip()
47 def addDevice(self, deviceStr):
49 Adds a device to self.devices
50 takes the raw line from pci.ids
54 if devID in self.devices:
57 self.devices[devID] = Device(deviceStr)
60 print(self.ID, self.name)
61 for id, dev in self.devices.items():
64 def find_device(self, devid):
65 # convert to a hex string and remove 0x
66 devid = hex(devid)[2:]
68 return self.devices[devid]
70 return Device("%s Unknown Device" % devid)
75 def __init__(self, deviceStr):
77 Class for each device.
78 Each vendor has its own devices dictionary.
81 self.ID = s.split()[0]
82 self.name = s.replace("%s " % self.ID, "")
86 print("\t%s\t%s" % (self.ID, self.name))
87 for subID, subdev in self.subdevices.items():
90 def addSubDevice(self, subDeviceStr):
92 Adds a subvendor, subdevice to device.
93 Uses raw line from pci.ids
95 s = subDeviceStr.strip()
99 subDeviceName = s.split(" ")[-1]
100 devID = "%s:%s" % (subVendorID, subDeviceID)
101 self.subdevices[devID] = SubDevice(
102 subVendorID, subDeviceID, subDeviceName)
104 def find_subid(self, subven, subdev):
105 subven = hex(subven)[2:]
106 subdev = hex(subdev)[2:]
107 devid = "%s:%s" % (subven, subdev)
110 return self.subdevices[devid]
112 if (subven == "ffff" and subdev == "ffff"):
113 return SubDevice("ffff", "ffff", "(All Subdevices)")
115 return SubDevice(subven, subdev, "(Unknown Subdevice)")
120 Class for subdevices.
123 def __init__(self, vendor, device, name):
125 Class initializes with vendorid, deviceid and name
127 self.vendorID = vendor
128 self.deviceID = device
132 print("\t\t%s\t%s\t%s" % (self.vendorID, self.deviceID, self.name))
137 Top class for all pci.ids entries.
138 All queries will be asked to this class.
139 PCIIds.vendors["0e11"].devices["0046"].\
140 subdevices["0e11:4091"].name = "Smart Array 6i"
143 def __init__(self, filename):
145 Prepares the directories.
146 Checks local data file.
147 Tries to load from local, if not found, downloads from web
153 self.readLocal(filename)
156 def reportVendors(self):
157 """Reports the vendors
159 for vid, v in self.vendors.items():
162 def report(self, vendor=None):
164 Reports everything for all vendors or a specific vendor
165 PCIIds.report() reports everything
166 PCIIDs.report("0e11") reports only "Compaq Computer Corporation"
168 if vendor is not None:
169 self.vendors[vendor].report()
171 for vID, v in self.vendors.items():
174 def find_vendor(self, vid):
175 # convert vid to a hex string and remove the 0x
179 return self.vendors[vid]
181 return Vendor("%s Unknown Vendor" % (vid))
183 def findDate(self, content):
185 if l.find("Date:") > -1:
186 return l.split()[-2].replace("-", "")
190 if len(self.contents) < 1:
191 print("data/%s-pci.ids not found" % self.date)
195 for l in self.contents:
198 elif len(l.strip()) == 0:
201 if l.find("\t\t") == 0:
202 self.vendors[vendorID].devices[
203 deviceID].addSubDevice(l)
204 elif l.find("\t") == 0:
205 deviceID = l.strip().split()[0]
206 self.vendors[vendorID].addDevice(l)
208 vendorID = l.split()[0]
209 self.vendors[vendorID] = Vendor(l)
211 def readLocal(self, filename):
215 with io.open(filename, 'r', encoding='utf-8') as f:
216 self.contents = f.readlines()
217 self.date = self.findDate(self.contents)
221 Loads database from local. If there is no file,
222 it creates a new one from web
224 self.date = idsfile[0].split("/")[1].split("-")[0]
228 # =======================================
230 def search_file(filename, search_path):
231 """ Given a search path, find file with requested name """
232 for path in string.split(search_path, ":"):
233 candidate = os.path.join(path, filename)
234 if os.path.exists(candidate):
235 return os.path.abspath(candidate)
239 class ReadElf(object):
240 """ display_* methods are used to emit output into the output stream
243 def __init__(self, file, output):
245 stream object with the ELF file to read
248 output stream to write to
250 self.elffile = ELFFile(file)
253 # Lazily initialized if a debug dump is requested
254 self._dwarfinfo = None
256 self._versioninfo = None
258 def _section_from_spec(self, spec):
259 """ Retrieve a section given a "spec" (either number or name).
260 Return None if no such section exists in the file.
264 if num < self.elffile.num_sections():
265 return self.elffile.get_section(num)
269 # Not a number. Must be a name then
270 section = self.elffile.get_section_by_name(force_unicode(spec))
272 # No match with a unicode name.
273 # Some versions of pyelftools (<= 0.23) store internal strings
274 # as bytes. Try again with the name encoded as bytes.
275 section = self.elffile.get_section_by_name(force_bytes(spec))
278 def pretty_print_pmdinfo(self, pmdinfo):
281 for i in pmdinfo["pci_ids"]:
282 vendor = pcidb.find_vendor(i[0])
283 device = vendor.find_device(i[1])
284 subdev = device.find_subid(i[2], i[3])
285 print("%s (%s) : %s (%s) %s" %
286 (vendor.name, vendor.ID, device.name,
287 device.ID, subdev.name))
289 def parse_pmd_info_string(self, mystring):
293 optional_pmd_info = [
294 {'id': 'params', 'tag': 'PMD PARAMETERS'},
295 {'id': 'kmod', 'tag': 'PMD KMOD DEPENDENCIES'}
298 i = mystring.index("=")
299 mystring = mystring[i + 2:]
300 pmdinfo = json.loads(mystring)
303 print(json.dumps(pmdinfo))
306 print("PMD NAME: " + pmdinfo["name"])
307 for i in optional_pmd_info:
309 print("%s: %s" % (i['tag'], pmdinfo[i['id']]))
313 if (len(pmdinfo["pci_ids"]) != 0):
314 print("PMD HW SUPPORT:")
315 if pcidb is not None:
316 self.pretty_print_pmdinfo(pmdinfo)
318 print("VENDOR\t DEVICE\t SUBVENDOR\t SUBDEVICE")
319 for i in pmdinfo["pci_ids"]:
320 print("0x%04x\t 0x%04x\t 0x%04x\t\t 0x%04x" %
321 (i[0], i[1], i[2], i[3]))
325 def display_pmd_info_strings(self, section_spec):
326 """ Display a strings dump of a section. section_spec is either a
327 section number or a name.
329 section = self._section_from_spec(section_spec)
333 data = section.data()
336 while dataptr < len(data):
337 while (dataptr < len(data) and
338 not (32 <= byte2int(data[dataptr]) <= 127)):
341 if dataptr >= len(data):
345 while endptr < len(data) and byte2int(data[endptr]) != 0:
348 # pyelftools may return byte-strings, force decode them
349 mystring = force_unicode(data[dataptr:endptr])
350 rc = mystring.find("PMD_INFO_STRING")
352 self.parse_pmd_info_string(mystring)
356 def find_librte_eal(self, section):
357 for tag in section.iter_tags():
358 # pyelftools may return byte-strings, force decode them
359 if force_unicode(tag.entry.d_tag) == 'DT_NEEDED':
360 if "librte_eal" in force_unicode(tag.needed):
361 return force_unicode(tag.needed)
364 def search_for_autoload_path(self):
369 section = self._section_from_spec(".dynamic")
371 eallib = self.find_librte_eal(section)
372 if eallib is not None:
373 ldlibpath = os.environ.get('LD_LIBRARY_PATH')
374 if ldlibpath is None:
376 dtr = self.get_dt_runpath(section)
377 library = search_file(eallib,
378 dtr + ":" + ldlibpath +
379 ":/usr/lib64:/lib64:/usr/lib:/lib")
382 if raw_output is False:
383 print("Scanning for autoload path in %s" % library)
384 scanfile = io.open(library, 'rb')
385 scanelf = ReadElf(scanfile, sys.stdout)
386 except AttributeError:
387 # Not a dynamic binary
393 section = scanelf._section_from_spec(".rodata")
395 if scanfile is not None:
399 data = section.data()
402 while dataptr < len(data):
403 while (dataptr < len(data) and
404 not (32 <= byte2int(data[dataptr]) <= 127)):
407 if dataptr >= len(data):
411 while endptr < len(data) and byte2int(data[endptr]) != 0:
414 # pyelftools may return byte-strings, force decode them
415 mystring = force_unicode(data[dataptr:endptr])
416 rc = mystring.find("DPDK_PLUGIN_PATH")
418 rc = mystring.find("=")
419 return (mystring[rc + 1:], library)
422 if scanfile is not None:
426 def get_dt_runpath(self, dynsec):
427 for tag in dynsec.iter_tags():
428 # pyelftools may return byte-strings, force decode them
429 if force_unicode(tag.entry.d_tag) == 'DT_RUNPATH':
430 return force_unicode(tag.runpath)
433 def process_dt_needed_entries(self):
434 """ Look to see if there are any DT_NEEDED entries in the binary
435 And process those if there are
438 ldlibpath = os.environ.get('LD_LIBRARY_PATH')
439 if ldlibpath is None:
442 dynsec = self._section_from_spec(".dynamic")
444 runpath = self.get_dt_runpath(dynsec)
445 except AttributeError:
446 # dynsec is None, just return
449 for tag in dynsec.iter_tags():
450 # pyelftools may return byte-strings, force decode them
451 if force_unicode(tag.entry.d_tag) == 'DT_NEEDED':
452 if 'librte_' in force_unicode(tag.needed):
453 library = search_file(force_unicode(tag.needed),
454 runpath + ":" + ldlibpath +
455 ":/usr/lib64:/lib64:/usr/lib:/lib")
456 if library is not None:
457 with io.open(library, 'rb') as file:
459 libelf = ReadElf(file, sys.stdout)
461 print("%s is no an ELF file" % library)
463 libelf.process_dt_needed_entries()
464 libelf.display_pmd_info_strings(".rodata")
468 # compat: remove force_unicode & force_bytes when pyelftools<=0.23 support is
470 def force_unicode(s):
471 if hasattr(s, 'decode') and callable(s.decode):
472 s = s.decode('latin-1') # same encoding used in pyelftools py3compat
477 if hasattr(s, 'encode') and callable(s.encode):
478 s = s.encode('latin-1') # same encoding used in pyelftools py3compat
482 def scan_autoload_path(autoload_path):
485 if os.path.exists(autoload_path) is False:
489 dirs = os.listdir(autoload_path)
491 # Couldn't read the directory, give up
495 dpath = os.path.join(autoload_path, d)
496 if os.path.isdir(dpath):
497 scan_autoload_path(dpath)
498 if os.path.isfile(dpath):
500 file = io.open(dpath, 'rb')
501 readelf = ReadElf(file, sys.stdout)
503 # this is likely not an elf file, skip it
506 # No permission to read the file, skip it
509 if raw_output is False:
510 print("Hw Support for library %s" % d)
511 readelf.display_pmd_info_strings(".rodata")
515 def scan_for_autoload_pmds(dpdk_path):
517 search the specified application or path for a pmd autoload path
518 then scan said path for pmds and report hw support
522 if (os.path.isfile(dpdk_path) is False):
523 if raw_output is False:
524 print("Must specify a file name")
527 file = io.open(dpdk_path, 'rb')
529 readelf = ReadElf(file, sys.stdout)
531 if raw_output is False:
532 print("Unable to parse %s" % file)
535 (autoload_path, scannedfile) = readelf.search_for_autoload_path()
536 if not autoload_path:
537 if (raw_output is False):
538 print("No autoload path configured in %s" % dpdk_path)
540 if (raw_output is False):
541 if (scannedfile is None):
542 scannedfile = dpdk_path
543 print("Found autoload path %s in %s" % (autoload_path, scannedfile))
546 if (raw_output is False):
547 print("Discovered Autoload HW Support:")
548 scan_autoload_path(autoload_path)
552 def main(stream=None):
556 pcifile_default = "./pci.ids" # For unknown OS's assume local file
557 if platform.system() == 'Linux':
558 # hwdata is the legacy location, misc is supported going forward
559 pcifile_default = "/usr/share/misc/pci.ids"
560 if not os.path.exists(pcifile_default):
561 pcifile_default = "/usr/share/hwdata/pci.ids"
562 elif platform.system() == 'FreeBSD':
563 pcifile_default = "/usr/local/share/pciids/pci.ids"
564 if not os.path.exists(pcifile_default):
565 pcifile_default = "/usr/share/misc/pci_vendors"
567 optparser = OptionParser(
568 usage='usage: %prog [-hrtp] [-d <pci id file] <elf-file>',
569 description="Dump pmd hardware support info",
570 add_help_option=True)
571 optparser.add_option('-r', '--raw',
572 action='store_true', dest='raw_output',
573 help='Dump raw json strings')
574 optparser.add_option("-d", "--pcidb", dest="pcifile",
575 help="specify a pci database "
576 "to get vendor names from",
577 default=pcifile_default, metavar="FILE")
578 optparser.add_option("-t", "--table", dest="tblout",
579 help="output information on hw support as a "
582 optparser.add_option("-p", "--plugindir", dest="pdir",
583 help="scan dpdk for autoload plugins",
586 options, args = optparser.parse_args()
588 if options.raw_output:
592 pcidb = PCIIds(options.pcifile)
594 print("Pci DB file not found")
598 options.pcifile = None
602 optparser.print_usage()
605 if options.pdir is True:
606 exit(scan_for_autoload_pmds(args[0]))
608 ldlibpath = os.environ.get('LD_LIBRARY_PATH')
609 if (ldlibpath is None):
612 if (os.path.exists(args[0]) is True):
615 myelffile = search_file(
616 args[0], ldlibpath + ":/usr/lib64:/lib64:/usr/lib:/lib")
618 if (myelffile is None):
619 print("File not found")
622 with io.open(myelffile, 'rb') as file:
624 readelf = ReadElf(file, sys.stdout)
625 readelf.process_dt_needed_entries()
626 readelf.display_pmd_info_strings(".rodata")
629 except ELFError as ex:
630 sys.stderr.write('ELF error: %s\n' % ex)
634 # -------------------------------------------------------------------------
635 if __name__ == '__main__':