From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id ED2EEA0350; Thu, 2 Jul 2020 02:02:59 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 359761D5D1; Thu, 2 Jul 2020 02:02:51 +0200 (CEST) Received: from mail-lj1-f173.google.com (mail-lj1-f173.google.com [209.85.208.173]) by dpdk.org (Postfix) with ESMTP id AB3D01D5A6 for ; Thu, 2 Jul 2020 02:02:48 +0200 (CEST) Received: by mail-lj1-f173.google.com with SMTP id d17so14643640ljl.3 for ; Wed, 01 Jul 2020 17:02:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cj78+bsEtIKedIm3xULBWyTH7wvdqf3TdD1maWxm4pI=; b=QxiIROrKYgIe3dP1I7V1u7jJauokRHMwiBhnkvc8kiXJaoM46fiQEFTlKPFIp7fFn5 LilC9ixQu/v0j5pUKJqzWZyL0xlwvq1GcwkaJVEHbQ29LRBg8heH/XHnA2qELxZ1kMl5 ZxFP0Gndwh8orS9zQ3rytbW3kmQaotN7BQ+/omGEkry+R4vqonUzbbBl6X/UCtPzuKnk GzgcU9DcwrQZW0my5hz0Z9Y8yUfp0gZKBOPzrvPBDPkFzEMSdk44tafVaci2OeY+o3T8 LBR0hTRhrldOFoWlQixsVdjiPGwYfU7o30IVBGSy7ArWTtQjrJ5/MQCP/f27AzPSsmLJ BLiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cj78+bsEtIKedIm3xULBWyTH7wvdqf3TdD1maWxm4pI=; b=Xw9Y2tXjssC9pjIE/7gaf1J7Qhbqvkb3wRDjGlqn0pbLi980TpoBmb/FVZC3BLpFyt 78TNRc/R6n/o46XKEI8UbOmMvtjASOG2Fa48gB2TBP77D8EMvzwyXvOAWZrkYFKkTz3u /gv880Xl3+FnuwFwchIK31bbKx1FEIXy04PTyuEpwQhzNclF94zaXIbPXSFesuf7HVff jWkeAHSnCs48KQhRB+nomf1UfFHIv97xDmdYBp124tzZCmQmW/MB1/46wdUoamuQQdpM K2DLL4UzmHRSqL30C0Zp/DEkc3cy2R45C12JFT5PsZYKRa+mezFK3QdbuKoWrrSbiRMW j+bw== X-Gm-Message-State: AOAM5322dNo7yNIKMh/A2V12s11OkBiIYJYm9Y0NSYXQb2RnAf5fvNcd 6GKD3tw6SuOb9CrhLL8VU/Lzvnib1WM= X-Google-Smtp-Source: ABdhPJzzsHdFYqcLepV7mSl2YQg05pu1ZMKwJzHuEMaQpcKNxcanv9vGgkqKq7ZB1Kyt3hQDROBG/Q== X-Received: by 2002:a2e:8699:: with SMTP id l25mr6010467lji.81.1593648167841; Wed, 01 Jul 2020 17:02:47 -0700 (PDT) Received: from localhost.localdomain (broadband-37-110-65-23.ip.moscow.rt.ru. [37.110.65.23]) by smtp.gmail.com with ESMTPSA id u26sm2875550lfq.72.2020.07.01.17.02.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jul 2020 17:02:47 -0700 (PDT) From: Dmitry Kozlyuk To: dev@dpdk.org Cc: Neil Horman , Bruce Richardson , Thomas Monjalon , robin.jarry@6wind.com, Jie Zhou , Tal Shnaiderman , Dmitry Kozlyuk Date: Thu, 2 Jul 2020 03:02:30 +0300 Message-Id: <20200702000232.10761-2-dmitry.kozliuk@gmail.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20200702000232.10761-1-dmitry.kozliuk@gmail.com> References: <20200622004503.29036-1-dmitry.kozliuk@gmail.com> <20200702000232.10761-1-dmitry.kozliuk@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [RFC PATCH v2 1/3] pmdinfogen: prototype in Python X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Requires pyelfutils, but no docs yet. Signed-off-by: Dmitry Kozlyuk --- buildtools/pmdinfogen.py | 191 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100755 buildtools/pmdinfogen.py diff --git a/buildtools/pmdinfogen.py b/buildtools/pmdinfogen.py new file mode 100755 index 000000000..05252e7ce --- /dev/null +++ b/buildtools/pmdinfogen.py @@ -0,0 +1,191 @@ +#!/usr/bin/env python3 + +import argparse +import ctypes +import json +import sys +import tempfile + +from elftools.elf.elffile import ELFFile +from elftools.elf.sections import SymbolTableSection + + +class ELFSymbol: + def __init__(self, image, symbol): + self._image = image + self._symbol = symbol + + @property + def size(self): + return self._symbol["st_size"] + + @property + def value(self): + data = self._image._image.get_section(self._symbol["st_shndx"]).data() + base = self._symbol["st_value"] + return data[base : base + self.size] + + @property + def string_value(self): + value = self.value + if len(value): + return value[:-1].decode() + return "" + + +class ELFImage: + def __init__(self, data): + self._image = ELFFile(data) + self._symtab = self._image.get_section_by_name(".symtab") + if not isinstance(self._symtab, SymbolTableSection): + raise Exception(".symtab section is not a symbol table") + + @property + def is_big_endian(self): + return not self._image.little_endian + + def find_by_name(self, name): + symbol = self._symtab.get_symbol_by_name(name) + if symbol: + return ELFSymbol(self, symbol[0]) + + def find_by_prefix(self, prefix): + for i in range(self._symtab.num_symbols()): + symbol = self._symtab.get_symbol(i) + if symbol.name.startswith(prefix): + yield ELFSymbol(self, symbol) + + +def define_rte_pci_id(is_big_endian): + base_type = ctypes.LittleEndianStructure + if is_big_endian: + base_type = ctypes.BigEndianStructure + + class rte_pci_id(base_type): + _pack_ = True + _fields_ = [ + ("class_id", ctypes.c_uint32), + ("vendor_id", ctypes.c_uint16), + ("device_id", ctypes.c_uint16), + ("subsystem_vendor_id", ctypes.c_uint16), + ("subsystem_device_id", ctypes.c_uint16), + ] + + return rte_pci_id + + +class Driver: + OPTIONS = [ + ("params", "_param_string_export"), + ("kmod", "_kmod_dep_export"), + ] + + def __init__(self, name, options): + self.name = name + for key, value in options.items(): + setattr(self, key, value) + self.pci_ids = [] + + @classmethod + def load(cls, image, symbol): + name = symbol.string_value + + options = {} + for key, suffix in cls.OPTIONS: + option_symbol = image.find_by_name("__%s%s" % (name, suffix)) + if option_symbol: + value = option_symbol.string_value + options[key] = value + + driver = cls(name, options) + + pci_table_name_symbol = image.find_by_name("__%s_pci_tbl_export" % name) + if pci_table_name_symbol: + driver._load_pci_ids(image, pci_table_name_symbol) + + return driver + + def _load_pci_ids(self, image, table_name_symbol): + table_name = table_name_symbol.string_value + table_symbol = image.find_by_name(table_name) + if not table_symbol: + raise Exception("PCI table declared but not defined: %d" % table_name) + + rte_pci_id = define_rte_pci_id(image.is_big_endian) + + pci_id_size = ctypes.sizeof(rte_pci_id) + if table_symbol.size % pci_id_size != 0: + raise Exception( + "PCI table size (%d) is not a multiple of rte_pci_id size (%d)" + % (table_symbol.size, pci_id_size) + ) + + pci_ids_desc = rte_pci_id * (table_symbol.size // pci_id_size) + pci_ids = pci_ids_desc.from_buffer_copy(table_symbol.value) + for pci_id in pci_ids: + if not pci_id.device_id: + break + self.pci_ids.append( + [ + pci_id.vendor_id, + pci_id.device_id, + pci_id.subsystem_vendor_id, + pci_id.subsystem_device_id, + ] + ) + + def dump(self, file): + dumped = json.dumps(self.__dict__) + escaped = dumped.replace('"', '\\"') + print( + 'const char %s_pmd_info[] __attribute__((used)) = "PMD_INFO_STRING= %s";' + % (self.name, escaped), + file=file, + ) + + +def load_drivers(image): + drivers = [] + for symbol in image.find_by_prefix("this_pmd_name"): + drivers.append(Driver.load(image, symbol)) + return drivers + + +def dump_drivers(drivers, file): + # Keep legacy order of definitions. + for driver in reversed(drivers): + driver.dump(file) + + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument("input", help="input object file path or '-' for stdin") + parser.add_argument("output", help="output C file path or '-' for stdout") + return parser.parse_args() + + +def open_input(path): + if path == "-": + temp = tempfile.TemporaryFile() + temp.write(sys.stdin.buffer.read()) + return temp + return open(path, "rb") + + +def open_output(path): + if path == "-": + return sys.stdout + return open(path, "w") + + +def main(): + args = parse_args() + infile = open_input(args.input) + image = ELFImage(infile) + drivers = load_drivers(image) + output = open_output(args.output) + dump_drivers(drivers, output) + + +if __name__ == "__main__": + main() -- 2.25.4