]>
git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/dpdk/usertools/dpdk-pmdinfo.py
3 # -------------------------------------------------------------------------
5 # Utility to dump PMD_INFO_STRING support from an object file
7 # -------------------------------------------------------------------------
8 from __future__
import print_function
14 from elftools
.common
.exceptions
import ELFError
15 from elftools
.common
.py3compat
import (byte2int
, bytes2str
, str2bytes
)
16 from elftools
.elf
.elffile
import ELFFile
17 from optparse
import OptionParser
19 # For running from development directory. It should take precedence over the
20 # installed pyelftools.
21 sys
.path
.insert(0, '.')
26 # ===========================================
31 Class for vendors. This is the top level class
32 for the devices belong to a specific vendor.
33 self.devices is the device dictionary
34 subdevices are in each device.
37 def __init__(self
, vendorStr
):
39 Class initializes with the raw line from pci.ids
40 Parsing takes place inside __init__
42 self
.ID
= vendorStr
.split()[0]
43 self
.name
= vendorStr
.replace("%s " % self
.ID
, "").rstrip()
46 def addDevice(self
, deviceStr
):
48 Adds a device to self.devices
49 takes the raw line from pci.ids
53 if devID
in self
.devices
:
56 self
.devices
[devID
] = Device(deviceStr
)
59 print(self
.ID
, self
.name
)
60 for id, dev
in self
.devices
.items():
63 def find_device(self
, devid
):
64 # convert to a hex string and remove 0x
65 devid
= hex(devid
)[2:]
67 return self
.devices
[devid
]
69 return Device("%s Unknown Device" % devid
)
74 def __init__(self
, deviceStr
):
76 Class for each device.
77 Each vendor has its own devices dictionary.
80 self
.ID
= s
.split()[0]
81 self
.name
= s
.replace("%s " % self
.ID
, "")
85 print("\t%s\t%s" % (self
.ID
, self
.name
))
86 for subID
, subdev
in self
.subdevices
.items():
89 def addSubDevice(self
, subDeviceStr
):
91 Adds a subvendor, subdevice to device.
92 Uses raw line from pci.ids
94 s
= subDeviceStr
.strip()
98 subDeviceName
= s
.split(" ")[-1]
99 devID
= "%s:%s" % (subVendorID
, subDeviceID
)
100 self
.subdevices
[devID
] = SubDevice(
101 subVendorID
, subDeviceID
, subDeviceName
)
103 def find_subid(self
, subven
, subdev
):
104 subven
= hex(subven
)[2:]
105 subdev
= hex(subdev
)[2:]
106 devid
= "%s:%s" % (subven
, subdev
)
109 return self
.subdevices
[devid
]
111 if (subven
== "ffff" and subdev
== "ffff"):
112 return SubDevice("ffff", "ffff", "(All Subdevices)")
114 return SubDevice(subven
, subdev
, "(Unknown Subdevice)")
119 Class for subdevices.
122 def __init__(self
, vendor
, device
, name
):
124 Class initializes with vendorid, deviceid and name
126 self
.vendorID
= vendor
127 self
.deviceID
= device
131 print("\t\t%s\t%s\t%s" % (self
.vendorID
, self
.deviceID
, self
.name
))
136 Top class for all pci.ids entries.
137 All queries will be asked to this class.
138 PCIIds.vendors["0e11"].devices["0046"].\
139 subdevices["0e11:4091"].name = "Smart Array 6i"
142 def __init__(self
, filename
):
144 Prepares the directories.
145 Checks local data file.
146 Tries to load from local, if not found, downloads from web
152 self
.readLocal(filename
)
155 def reportVendors(self
):
156 """Reports the vendors
158 for vid
, v
in self
.vendors
.items():
161 def report(self
, vendor
=None):
163 Reports everything for all vendors or a specific vendor
164 PCIIds.report() reports everything
165 PCIIDs.report("0e11") reports only "Compaq Computer Corporation"
167 if vendor
is not None:
168 self
.vendors
[vendor
].report()
170 for vID
, v
in self
.vendors
.items():
173 def find_vendor(self
, vid
):
174 # convert vid to a hex string and remove the 0x
178 return self
.vendors
[vid
]
180 return Vendor("%s Unknown Vendor" % (vid
))
182 def findDate(self
, content
):
184 if l
.find("Date:") > -1:
185 return l
.split()[-2].replace("-", "")
189 if len(self
.contents
) < 1:
190 print("data/%s-pci.ids not found" % self
.date
)
194 for l
in self
.contents
:
197 elif len(l
.strip()) == 0:
200 if l
.find("\t\t") == 0:
201 self
.vendors
[vendorID
].devices
[
202 deviceID
].addSubDevice(l
)
203 elif l
.find("\t") == 0:
204 deviceID
= l
.strip().split()[0]
205 self
.vendors
[vendorID
].addDevice(l
)
207 vendorID
= l
.split()[0]
208 self
.vendors
[vendorID
] = Vendor(l
)
210 def readLocal(self
, filename
):
214 self
.contents
= open(filename
).readlines()
215 self
.date
= self
.findDate(self
.contents
)
219 Loads database from local. If there is no file,
220 it creates a new one from web
222 self
.date
= idsfile
[0].split("/")[1].split("-")[0]
226 # =======================================
228 def search_file(filename
, search_path
):
229 """ Given a search path, find file with requested name """
230 for path
in string
.split(search_path
, ":"):
231 candidate
= os
.path
.join(path
, filename
)
232 if os
.path
.exists(candidate
):
233 return os
.path
.abspath(candidate
)
237 class ReadElf(object):
238 """ display_* methods are used to emit output into the output stream
241 def __init__(self
, file, output
):
243 stream object with the ELF file to read
246 output stream to write to
248 self
.elffile
= ELFFile(file)
251 # Lazily initialized if a debug dump is requested
252 self
._dwarfinfo
= None
254 self
._versioninfo
= None
256 def _section_from_spec(self
, spec
):
257 """ Retrieve a section given a "spec" (either number or name).
258 Return None if no such section exists in the file.
262 if num
< self
.elffile
.num_sections():
263 return self
.elffile
.get_section(num
)
267 # Not a number. Must be a name then
268 return self
.elffile
.get_section_by_name(str2bytes(spec
))
270 def pretty_print_pmdinfo(self
, pmdinfo
):
273 for i
in pmdinfo
["pci_ids"]:
274 vendor
= pcidb
.find_vendor(i
[0])
275 device
= vendor
.find_device(i
[1])
276 subdev
= device
.find_subid(i
[2], i
[3])
277 print("%s (%s) : %s (%s) %s" %
278 (vendor
.name
, vendor
.ID
, device
.name
,
279 device
.ID
, subdev
.name
))
281 def parse_pmd_info_string(self
, mystring
):
285 optional_pmd_info
= [
286 {'id': 'params', 'tag': 'PMD PARAMETERS'},
287 {'id': 'kmod', 'tag': 'PMD KMOD DEPENDENCIES'}
290 i
= mystring
.index("=")
291 mystring
= mystring
[i
+ 2:]
292 pmdinfo
= json
.loads(mystring
)
295 print(json
.dumps(pmdinfo
))
298 print("PMD NAME: " + pmdinfo
["name"])
299 for i
in optional_pmd_info
:
301 print("%s: %s" % (i
['tag'], pmdinfo
[i
['id']]))
305 if (len(pmdinfo
["pci_ids"]) != 0):
306 print("PMD HW SUPPORT:")
307 if pcidb
is not None:
308 self
.pretty_print_pmdinfo(pmdinfo
)
310 print("VENDOR\t DEVICE\t SUBVENDOR\t SUBDEVICE")
311 for i
in pmdinfo
["pci_ids"]:
312 print("0x%04x\t 0x%04x\t 0x%04x\t\t 0x%04x" %
313 (i
[0], i
[1], i
[2], i
[3]))
317 def display_pmd_info_strings(self
, section_spec
):
318 """ Display a strings dump of a section. section_spec is either a
319 section number or a name.
321 section
= self
._section
_from
_spec
(section_spec
)
325 data
= section
.data()
328 while dataptr
< len(data
):
329 while (dataptr
< len(data
) and
330 not (32 <= byte2int(data
[dataptr
]) <= 127)):
333 if dataptr
>= len(data
):
337 while endptr
< len(data
) and byte2int(data
[endptr
]) != 0:
340 mystring
= bytes2str(data
[dataptr
:endptr
])
341 rc
= mystring
.find("PMD_INFO_STRING")
343 self
.parse_pmd_info_string(mystring
)
347 def find_librte_eal(self
, section
):
348 for tag
in section
.iter_tags():
349 if tag
.entry
.d_tag
== 'DT_NEEDED':
350 if "librte_eal" in tag
.needed
:
354 def search_for_autoload_path(self
):
359 section
= self
._section
_from
_spec
(".dynamic")
361 eallib
= self
.find_librte_eal(section
)
362 if eallib
is not None:
363 ldlibpath
= os
.environ
.get('LD_LIBRARY_PATH')
364 if ldlibpath
is None:
366 dtr
= self
.get_dt_runpath(section
)
367 library
= search_file(eallib
,
368 dtr
+ ":" + ldlibpath
+
369 ":/usr/lib64:/lib64:/usr/lib:/lib")
372 if raw_output
is False:
373 print("Scanning for autoload path in %s" % library
)
374 scanfile
= open(library
, 'rb')
375 scanelf
= ReadElf(scanfile
, sys
.stdout
)
376 except AttributeError:
377 # Not a dynamic binary
383 section
= scanelf
._section
_from
_spec
(".rodata")
385 if scanfile
is not None:
389 data
= section
.data()
392 while dataptr
< len(data
):
393 while (dataptr
< len(data
) and
394 not (32 <= byte2int(data
[dataptr
]) <= 127)):
397 if dataptr
>= len(data
):
401 while endptr
< len(data
) and byte2int(data
[endptr
]) != 0:
404 mystring
= bytes2str(data
[dataptr
:endptr
])
405 rc
= mystring
.find("DPDK_PLUGIN_PATH")
407 rc
= mystring
.find("=")
408 return (mystring
[rc
+ 1:], library
)
411 if scanfile
is not None:
415 def get_dt_runpath(self
, dynsec
):
416 for tag
in dynsec
.iter_tags():
417 if tag
.entry
.d_tag
== 'DT_RUNPATH':
421 def process_dt_needed_entries(self
):
422 """ Look to see if there are any DT_NEEDED entries in the binary
423 And process those if there are
427 ldlibpath
= os
.environ
.get('LD_LIBRARY_PATH')
428 if ldlibpath
is None:
431 dynsec
= self
._section
_from
_spec
(".dynamic")
433 runpath
= self
.get_dt_runpath(dynsec
)
434 except AttributeError:
435 # dynsec is None, just return
438 for tag
in dynsec
.iter_tags():
439 if tag
.entry
.d_tag
== 'DT_NEEDED':
440 rc
= tag
.needed
.find(b
"librte_pmd")
442 library
= search_file(tag
.needed
,
443 runpath
+ ":" + ldlibpath
+
444 ":/usr/lib64:/lib64:/usr/lib:/lib")
445 if library
is not None:
446 if raw_output
is False:
447 print("Scanning %s for pmd information" % library
)
448 with
open(library
, 'rb') as file:
450 libelf
= ReadElf(file, sys
.stdout
)
452 print("%s is no an ELF file" % library
)
454 libelf
.process_dt_needed_entries()
455 libelf
.display_pmd_info_strings(".rodata")
459 def scan_autoload_path(autoload_path
):
462 if os
.path
.exists(autoload_path
) is False:
466 dirs
= os
.listdir(autoload_path
)
468 # Couldn't read the directory, give up
472 dpath
= os
.path
.join(autoload_path
, d
)
473 if os
.path
.isdir(dpath
):
474 scan_autoload_path(dpath
)
475 if os
.path
.isfile(dpath
):
477 file = open(dpath
, 'rb')
478 readelf
= ReadElf(file, sys
.stdout
)
480 # this is likely not an elf file, skip it
483 # No permission to read the file, skip it
486 if raw_output
is False:
487 print("Hw Support for library %s" % d
)
488 readelf
.display_pmd_info_strings(".rodata")
492 def scan_for_autoload_pmds(dpdk_path
):
494 search the specified application or path for a pmd autoload path
495 then scan said path for pmds and report hw support
499 if (os
.path
.isfile(dpdk_path
) is False):
500 if raw_output
is False:
501 print("Must specify a file name")
504 file = open(dpdk_path
, 'rb')
506 readelf
= ReadElf(file, sys
.stdout
)
508 if raw_output
is False:
509 print("Unable to parse %s" % file)
512 (autoload_path
, scannedfile
) = readelf
.search_for_autoload_path()
513 if (autoload_path
is None or autoload_path
is ""):
514 if (raw_output
is False):
515 print("No autoload path configured in %s" % dpdk_path
)
517 if (raw_output
is False):
518 if (scannedfile
is None):
519 scannedfile
= dpdk_path
520 print("Found autoload path %s in %s" % (autoload_path
, scannedfile
))
523 if (raw_output
is False):
524 print("Discovered Autoload HW Support:")
525 scan_autoload_path(autoload_path
)
529 def main(stream
=None):
533 pcifile_default
= "./pci.ids" # For unknown OS's assume local file
534 if platform
.system() == 'Linux':
535 pcifile_default
= "/usr/share/hwdata/pci.ids"
536 elif platform
.system() == 'FreeBSD':
537 pcifile_default
= "/usr/local/share/pciids/pci.ids"
538 if not os
.path
.exists(pcifile_default
):
539 pcifile_default
= "/usr/share/misc/pci_vendors"
541 optparser
= OptionParser(
542 usage
='usage: %prog [-hrtp] [-d <pci id file] <elf-file>',
543 description
="Dump pmd hardware support info",
544 add_help_option
=True)
545 optparser
.add_option('-r', '--raw',
546 action
='store_true', dest
='raw_output',
547 help='Dump raw json strings')
548 optparser
.add_option("-d", "--pcidb", dest
="pcifile",
549 help="specify a pci database "
550 "to get vendor names from",
551 default
=pcifile_default
, metavar
="FILE")
552 optparser
.add_option("-t", "--table", dest
="tblout",
553 help="output information on hw support as a "
556 optparser
.add_option("-p", "--plugindir", dest
="pdir",
557 help="scan dpdk for autoload plugins",
560 options
, args
= optparser
.parse_args()
562 if options
.raw_output
:
566 pcidb
= PCIIds(options
.pcifile
)
568 print("Pci DB file not found")
572 options
.pcifile
= None
576 optparser
.print_usage()
579 if options
.pdir
is True:
580 exit(scan_for_autoload_pmds(args
[0]))
582 ldlibpath
= os
.environ
.get('LD_LIBRARY_PATH')
583 if (ldlibpath
is None):
586 if (os
.path
.exists(args
[0]) is True):
589 myelffile
= search_file(
590 args
[0], ldlibpath
+ ":/usr/lib64:/lib64:/usr/lib:/lib")
592 if (myelffile
is None):
593 print("File not found")
596 with
open(myelffile
, 'rb') as file:
598 readelf
= ReadElf(file, sys
.stdout
)
599 readelf
.process_dt_needed_entries()
600 readelf
.display_pmd_info_strings(".rodata")
603 except ELFError
as ex
:
604 sys
.stderr
.write('ELF error: %s\n' % ex
)
608 # -------------------------------------------------------------------------
609 if __name__
== '__main__':