]>
git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/dpdk/usertools/dpdk-pmdinfo.py
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 # ===========================================
35 Class for vendors. This is the top level class
36 for the devices belong to a specific vendor.
37 self.devices is the device dictionary
38 subdevices are in each device.
41 def __init__(self
, vendorStr
):
43 Class initializes with the raw line from pci.ids
44 Parsing takes place inside __init__
46 self
.ID
= vendorStr
.split()[0]
47 self
.name
= vendorStr
.replace("%s " % self
.ID
, "").rstrip()
50 def addDevice(self
, deviceStr
):
52 Adds a device to self.devices
53 takes the raw line from pci.ids
57 if devID
in self
.devices
:
60 self
.devices
[devID
] = Device(deviceStr
)
63 print(self
.ID
, self
.name
)
64 for id, dev
in self
.devices
.items():
67 def find_device(self
, devid
):
68 # convert to a hex string and remove 0x
69 devid
= hex(devid
)[2:]
71 return self
.devices
[devid
]
73 return Device("%s Unknown Device" % devid
)
78 def __init__(self
, deviceStr
):
80 Class for each device.
81 Each vendor has its own devices dictionary.
84 self
.ID
= s
.split()[0]
85 self
.name
= s
.replace("%s " % self
.ID
, "")
89 print("\t%s\t%s" % (self
.ID
, self
.name
))
90 for subID
, subdev
in self
.subdevices
.items():
93 def addSubDevice(self
, subDeviceStr
):
95 Adds a subvendor, subdevice to device.
96 Uses raw line from pci.ids
98 s
= subDeviceStr
.strip()
102 subDeviceName
= s
.split(" ")[-1]
103 devID
= "%s:%s" % (subVendorID
, subDeviceID
)
104 self
.subdevices
[devID
] = SubDevice(
105 subVendorID
, subDeviceID
, subDeviceName
)
107 def find_subid(self
, subven
, subdev
):
108 subven
= hex(subven
)[2:]
109 subdev
= hex(subdev
)[2:]
110 devid
= "%s:%s" % (subven
, subdev
)
113 return self
.subdevices
[devid
]
115 if (subven
== "ffff" and subdev
== "ffff"):
116 return SubDevice("ffff", "ffff", "(All Subdevices)")
118 return SubDevice(subven
, subdev
, "(Unknown Subdevice)")
123 Class for subdevices.
126 def __init__(self
, vendor
, device
, name
):
128 Class initializes with vendorid, deviceid and name
130 self
.vendorID
= vendor
131 self
.deviceID
= device
135 print("\t\t%s\t%s\t%s" % (self
.vendorID
, self
.deviceID
, self
.name
))
140 Top class for all pci.ids entries.
141 All queries will be asked to this class.
142 PCIIds.vendors["0e11"].devices["0046"].\
143 subdevices["0e11:4091"].name = "Smart Array 6i"
146 def __init__(self
, filename
):
148 Prepares the directories.
149 Checks local data file.
150 Tries to load from local, if not found, downloads from web
156 self
.readLocal(filename
)
159 def reportVendors(self
):
160 """Reports the vendors
162 for vid
, v
in self
.vendors
.items():
165 def report(self
, vendor
=None):
167 Reports everything for all vendors or a specific vendor
168 PCIIds.report() reports everything
169 PCIIDs.report("0e11") reports only "Compaq Computer Corporation"
171 if vendor
is not None:
172 self
.vendors
[vendor
].report()
174 for vID
, v
in self
.vendors
.items():
177 def find_vendor(self
, vid
):
178 # convert vid to a hex string and remove the 0x
182 return self
.vendors
[vid
]
184 return Vendor("%s Unknown Vendor" % (vid
))
186 def findDate(self
, content
):
188 if l
.find("Date:") > -1:
189 return l
.split()[-2].replace("-", "")
193 if len(self
.contents
) < 1:
194 print("data/%s-pci.ids not found" % self
.date
)
198 for l
in self
.contents
:
201 elif len(l
.strip()) == 0:
204 if l
.find("\t\t") == 0:
205 self
.vendors
[vendorID
].devices
[
206 deviceID
].addSubDevice(l
)
207 elif l
.find("\t") == 0:
208 deviceID
= l
.strip().split()[0]
209 self
.vendors
[vendorID
].addDevice(l
)
211 vendorID
= l
.split()[0]
212 self
.vendors
[vendorID
] = Vendor(l
)
214 def readLocal(self
, filename
):
218 with io
.open(filename
, 'r', encoding
='utf-8') as f
:
219 self
.contents
= f
.readlines()
220 self
.date
= self
.findDate(self
.contents
)
224 Loads database from local. If there is no file,
225 it creates a new one from web
227 self
.date
= idsfile
[0].split("/")[1].split("-")[0]
231 # =======================================
233 def search_file(filename
, search_path
):
234 """ Given a search path, find file with requested name """
235 for path
in string
.split(search_path
, ":"):
236 candidate
= os
.path
.join(path
, filename
)
237 if os
.path
.exists(candidate
):
238 return os
.path
.abspath(candidate
)
242 class ReadElf(object):
243 """ display_* methods are used to emit output into the output stream
246 def __init__(self
, file, output
):
248 stream object with the ELF file to read
251 output stream to write to
253 self
.elffile
= ELFFile(file)
256 # Lazily initialized if a debug dump is requested
257 self
._dwarfinfo
= None
259 self
._versioninfo
= None
261 def _section_from_spec(self
, spec
):
262 """ Retrieve a section given a "spec" (either number or name).
263 Return None if no such section exists in the file.
267 if num
< self
.elffile
.num_sections():
268 return self
.elffile
.get_section(num
)
272 # Not a number. Must be a name then
273 section
= self
.elffile
.get_section_by_name(force_unicode(spec
))
275 # No match with a unicode name.
276 # Some versions of pyelftools (<= 0.23) store internal strings
277 # as bytes. Try again with the name encoded as bytes.
278 section
= self
.elffile
.get_section_by_name(force_bytes(spec
))
281 def pretty_print_pmdinfo(self
, pmdinfo
):
284 for i
in pmdinfo
["pci_ids"]:
285 vendor
= pcidb
.find_vendor(i
[0])
286 device
= vendor
.find_device(i
[1])
287 subdev
= device
.find_subid(i
[2], i
[3])
288 print("%s (%s) : %s (%s) %s" %
289 (vendor
.name
, vendor
.ID
, device
.name
,
290 device
.ID
, subdev
.name
))
292 def parse_pmd_info_string(self
, mystring
):
296 optional_pmd_info
= [
297 {'id': 'params', 'tag': 'PMD PARAMETERS'},
298 {'id': 'kmod', 'tag': 'PMD KMOD DEPENDENCIES'}
301 i
= mystring
.index("=")
302 mystring
= mystring
[i
+ 2:]
303 pmdinfo
= json
.loads(mystring
)
306 print(json
.dumps(pmdinfo
))
309 print("PMD NAME: " + pmdinfo
["name"])
310 for i
in optional_pmd_info
:
312 print("%s: %s" % (i
['tag'], pmdinfo
[i
['id']]))
316 if (len(pmdinfo
["pci_ids"]) != 0):
317 print("PMD HW SUPPORT:")
318 if pcidb
is not None:
319 self
.pretty_print_pmdinfo(pmdinfo
)
321 print("VENDOR\t DEVICE\t SUBVENDOR\t SUBDEVICE")
322 for i
in pmdinfo
["pci_ids"]:
323 print("0x%04x\t 0x%04x\t 0x%04x\t\t 0x%04x" %
324 (i
[0], i
[1], i
[2], i
[3]))
328 def display_pmd_info_strings(self
, section_spec
):
329 """ Display a strings dump of a section. section_spec is either a
330 section number or a name.
332 section
= self
._section
_from
_spec
(section_spec
)
336 data
= section
.data()
339 while dataptr
< len(data
):
340 while (dataptr
< len(data
) and
341 not (32 <= byte2int(data
[dataptr
]) <= 127)):
344 if dataptr
>= len(data
):
348 while endptr
< len(data
) and byte2int(data
[endptr
]) != 0:
351 # pyelftools may return byte-strings, force decode them
352 mystring
= force_unicode(data
[dataptr
:endptr
])
353 rc
= mystring
.find("PMD_INFO_STRING")
355 self
.parse_pmd_info_string(mystring
)
359 def find_librte_eal(self
, section
):
360 for tag
in section
.iter_tags():
361 # pyelftools may return byte-strings, force decode them
362 if force_unicode(tag
.entry
.d_tag
) == 'DT_NEEDED':
363 if "librte_eal" in force_unicode(tag
.needed
):
364 return force_unicode(tag
.needed
)
367 def search_for_autoload_path(self
):
372 section
= self
._section
_from
_spec
(".dynamic")
374 eallib
= self
.find_librte_eal(section
)
375 if eallib
is not None:
376 ldlibpath
= os
.environ
.get('LD_LIBRARY_PATH')
377 if ldlibpath
is None:
379 dtr
= self
.get_dt_runpath(section
)
380 library
= search_file(eallib
,
381 dtr
+ ":" + ldlibpath
+
382 ":/usr/lib64:/lib64:/usr/lib:/lib")
385 if raw_output
is False:
386 print("Scanning for autoload path in %s" % library
)
387 scanfile
= io
.open(library
, 'rb')
388 scanelf
= ReadElf(scanfile
, sys
.stdout
)
389 except AttributeError:
390 # Not a dynamic binary
396 section
= scanelf
._section
_from
_spec
(".rodata")
398 if scanfile
is not None:
402 data
= section
.data()
405 while dataptr
< len(data
):
406 while (dataptr
< len(data
) and
407 not (32 <= byte2int(data
[dataptr
]) <= 127)):
410 if dataptr
>= len(data
):
414 while endptr
< len(data
) and byte2int(data
[endptr
]) != 0:
417 # pyelftools may return byte-strings, force decode them
418 mystring
= force_unicode(data
[dataptr
:endptr
])
419 rc
= mystring
.find("DPDK_PLUGIN_PATH")
421 rc
= mystring
.find("=")
422 return (mystring
[rc
+ 1:], library
)
425 if scanfile
is not None:
429 def get_dt_runpath(self
, dynsec
):
430 for tag
in dynsec
.iter_tags():
431 # pyelftools may return byte-strings, force decode them
432 if force_unicode(tag
.entry
.d_tag
) == 'DT_RUNPATH':
433 return force_unicode(tag
.runpath
)
436 def process_dt_needed_entries(self
):
437 """ Look to see if there are any DT_NEEDED entries in the binary
438 And process those if there are
442 ldlibpath
= os
.environ
.get('LD_LIBRARY_PATH')
443 if ldlibpath
is None:
446 dynsec
= self
._section
_from
_spec
(".dynamic")
448 runpath
= self
.get_dt_runpath(dynsec
)
449 except AttributeError:
450 # dynsec is None, just return
453 for tag
in dynsec
.iter_tags():
454 # pyelftools may return byte-strings, force decode them
455 if force_unicode(tag
.entry
.d_tag
) == 'DT_NEEDED':
456 if 'librte_pmd' in force_unicode(tag
.needed
):
457 library
= search_file(force_unicode(tag
.needed
),
458 runpath
+ ":" + ldlibpath
+
459 ":/usr/lib64:/lib64:/usr/lib:/lib")
460 if library
is not None:
461 if raw_output
is False:
462 print("Scanning %s for pmd information" % library
)
463 with io
.open(library
, 'rb') as file:
465 libelf
= ReadElf(file, sys
.stdout
)
467 print("%s is no an ELF file" % library
)
469 libelf
.process_dt_needed_entries()
470 libelf
.display_pmd_info_strings(".rodata")
474 # compat: remove force_unicode & force_bytes when pyelftools<=0.23 support is
476 def force_unicode(s
):
477 if hasattr(s
, 'decode') and callable(s
.decode
):
478 s
= s
.decode('latin-1') # same encoding used in pyelftools py3compat
483 if hasattr(s
, 'encode') and callable(s
.encode
):
484 s
= s
.encode('latin-1') # same encoding used in pyelftools py3compat
488 def scan_autoload_path(autoload_path
):
491 if os
.path
.exists(autoload_path
) is False:
495 dirs
= os
.listdir(autoload_path
)
497 # Couldn't read the directory, give up
501 dpath
= os
.path
.join(autoload_path
, d
)
502 if os
.path
.isdir(dpath
):
503 scan_autoload_path(dpath
)
504 if os
.path
.isfile(dpath
):
506 file = io
.open(dpath
, 'rb')
507 readelf
= ReadElf(file, sys
.stdout
)
509 # this is likely not an elf file, skip it
512 # No permission to read the file, skip it
515 if raw_output
is False:
516 print("Hw Support for library %s" % d
)
517 readelf
.display_pmd_info_strings(".rodata")
521 def scan_for_autoload_pmds(dpdk_path
):
523 search the specified application or path for a pmd autoload path
524 then scan said path for pmds and report hw support
528 if (os
.path
.isfile(dpdk_path
) is False):
529 if raw_output
is False:
530 print("Must specify a file name")
533 file = io
.open(dpdk_path
, 'rb')
535 readelf
= ReadElf(file, sys
.stdout
)
537 if raw_output
is False:
538 print("Unable to parse %s" % file)
541 (autoload_path
, scannedfile
) = readelf
.search_for_autoload_path()
542 if not autoload_path
:
543 if (raw_output
is False):
544 print("No autoload path configured in %s" % dpdk_path
)
546 if (raw_output
is False):
547 if (scannedfile
is None):
548 scannedfile
= dpdk_path
549 print("Found autoload path %s in %s" % (autoload_path
, scannedfile
))
552 if (raw_output
is False):
553 print("Discovered Autoload HW Support:")
554 scan_autoload_path(autoload_path
)
558 def main(stream
=None):
562 pcifile_default
= "./pci.ids" # For unknown OS's assume local file
563 if platform
.system() == 'Linux':
564 # hwdata is the legacy location, misc is supported going forward
565 pcifile_default
= "/usr/share/misc/pci.ids"
566 if not os
.path
.exists(pcifile_default
):
567 pcifile_default
= "/usr/share/hwdata/pci.ids"
568 elif platform
.system() == 'FreeBSD':
569 pcifile_default
= "/usr/local/share/pciids/pci.ids"
570 if not os
.path
.exists(pcifile_default
):
571 pcifile_default
= "/usr/share/misc/pci_vendors"
573 optparser
= OptionParser(
574 usage
='usage: %prog [-hrtp] [-d <pci id file] <elf-file>',
575 description
="Dump pmd hardware support info",
576 add_help_option
=True)
577 optparser
.add_option('-r', '--raw',
578 action
='store_true', dest
='raw_output',
579 help='Dump raw json strings')
580 optparser
.add_option("-d", "--pcidb", dest
="pcifile",
581 help="specify a pci database "
582 "to get vendor names from",
583 default
=pcifile_default
, metavar
="FILE")
584 optparser
.add_option("-t", "--table", dest
="tblout",
585 help="output information on hw support as a "
588 optparser
.add_option("-p", "--plugindir", dest
="pdir",
589 help="scan dpdk for autoload plugins",
592 options
, args
= optparser
.parse_args()
594 if options
.raw_output
:
598 pcidb
= PCIIds(options
.pcifile
)
600 print("Pci DB file not found")
604 options
.pcifile
= None
608 optparser
.print_usage()
611 if options
.pdir
is True:
612 exit(scan_for_autoload_pmds(args
[0]))
614 ldlibpath
= os
.environ
.get('LD_LIBRARY_PATH')
615 if (ldlibpath
is None):
618 if (os
.path
.exists(args
[0]) is True):
621 myelffile
= search_file(
622 args
[0], ldlibpath
+ ":/usr/lib64:/lib64:/usr/lib:/lib")
624 if (myelffile
is None):
625 print("File not found")
628 with io
.open(myelffile
, 'rb') as file:
630 readelf
= ReadElf(file, sys
.stdout
)
631 readelf
.process_dt_needed_entries()
632 readelf
.display_pmd_info_strings(".rodata")
635 except ELFError
as ex
:
636 sys
.stderr
.write('ELF error: %s\n' % ex
)
640 # -------------------------------------------------------------------------
641 if __name__
== '__main__':