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 A8E24A04DB for ; Wed, 14 Oct 2020 20:32:13 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id BA9651B686; Wed, 14 Oct 2020 20:31:56 +0200 (CEST) Received: from mail-lj1-f196.google.com (mail-lj1-f196.google.com [209.85.208.196]) by dpdk.org (Postfix) with ESMTP id B127D1DAF3; Wed, 14 Oct 2020 20:31:46 +0200 (CEST) Received: by mail-lj1-f196.google.com with SMTP id x16so512552ljh.2; Wed, 14 Oct 2020 11:31:46 -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=ROploo1ZevuCz2kwJ9Pu3DAisjp/NP2wfdGOoOHAGZY=; b=auut6whCjHnMZLwzIzRtTpbpgMBi1O2j+fYJuW2Q7WNyxiR33zfd5UYrFIU/03L4WC Twpf9ws3drwuzaUjCrZDPa+Gzy1awCBDyaM8EEasHAdAAPBjSbWZAtrLE48A0HVPkrhz ttjHylhEa0oIhBianJj0hE+emNx1Qbl8xoLHpFTDM1iUVypB5/RnL0ym8sIJMU+pmSBv 95PkZp/frKz7c5/1P9zdyoGhCubsT0q9LvbsnSnQTKXGiUIHMzfhfaqAbIlxSOLGc4OF ih2omUKTUpvdw8SRyAHLhXEssfTXuuBMYXxJ/dS9+NaSQoPXEwKbBGpVFUch8zDquXXx yUmg== 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=ROploo1ZevuCz2kwJ9Pu3DAisjp/NP2wfdGOoOHAGZY=; b=p2AJxtFaD5IyGZXAf2JkTUe7KRmNjPQhbPybrfDkFesJxXNkcaHcN1wd3J7OZqWH+H AoBq93NmJPUaep8k1CqOcRHB2XlWYTz/kG80/BfKPQVgLDTFqgpWaoVgCNasbNsOC5/4 m4wCG5+TS3te1Kks8OcU7T+toF+8PY6TwCMi4a4EktEWHzDqWsDQQEzF5Blsusb5LZUq 2tuwbKKWZohNGB0DW0KV5KjzvtXgy1ppLBdd8igFcw3dnV3d3v7mYAToACPXUc9aYKZf XtU55+Sg8lKMq9dS+9KvRkOkp3i0btdONwumQeOWyw2bnH4KAZnHLykNgKTCqRxaLsi/ v2iQ== X-Gm-Message-State: AOAM531GLVpcZhqR1uVffPyIuVJQWE0RnbT6TLxVQZ+OLyGddzrW+MZM 4RLEXH2ZRMfS8SEoq59tg1Jb65y9u+l78Q== X-Google-Smtp-Source: ABdhPJxNV5LO9XwZ7wJu61IGrUyQkMLfqk0HRx5ggTDq+Zmm2SxkKuCczki23JshhJP63W9vzjuK8Q== X-Received: by 2002:a2e:90c1:: with SMTP id o1mr11977ljg.268.1602700304967; Wed, 14 Oct 2020 11:31:44 -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 d21sm32942lfl.62.2020.10.14.11.31.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Oct 2020 11:31:44 -0700 (PDT) From: Dmitry Kozlyuk To: dev@dpdk.org Cc: ci@dpdk.org, Stephen Hemminger , David Marchand , Thomas Monjalon , Maxime Coquelin , Dmitry Kozlyuk , Neil Horman Date: Wed, 14 Oct 2020 21:31:34 +0300 Message-Id: <20201014183136.22239-2-dmitry.kozliuk@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201014183136.22239-1-dmitry.kozliuk@gmail.com> References: <20201004015947.9012-1-dmitry.kozliuk@gmail.com> <20201014183136.22239-1-dmitry.kozliuk@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-ci] [PATCH v7 1/3] pmdinfogen: add Python implementation X-BeenThere: ci@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK CI discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ci-bounces@dpdk.org Sender: "ci" Using a high-level, interpreted language simplifies maintenance and build process. Furthermore, ELF handling is delegated to pyelftools package. Original logic is kept, the copyright recognizes that. Signed-off-by: Dmitry Kozlyuk --- buildtools/pmdinfogen.py | 189 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100755 buildtools/pmdinfogen.py diff --git a/buildtools/pmdinfogen.py b/buildtools/pmdinfogen.py new file mode 100755 index 000000000..0cca47ff1 --- /dev/null +++ b/buildtools/pmdinfogen.py @@ -0,0 +1,189 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: BSD-3-Clause +# Copyright (c) 2016 Neil Horman +# Copyright (c) 2020 Dmitry Kozlyuk + +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.get_section_data(self._symbol["st_shndx"]) + base = self._symbol["st_value"] + return data[base:base + self.size] + + @property + def string_value(self): + value = self.value + return value[:-1].decode() if value else "" + + +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 get_section_data(self, name): + return self._image.get_section(name).data() + + def find_by_name(self, name): + symbol = self._symtab.get_symbol_by_name(name) + return ELFSymbol(self, symbol[0]) if symbol else None + + 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.pci_ids = cls._load_pci_ids(image, pci_table_name_symbol) + + return driver + + @staticmethod + def _load_pci_ids(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) + pci_ids_desc = rte_pci_id * (table_symbol.size // pci_id_size) + pci_ids = pci_ids_desc.from_buffer_copy(table_symbol.value) + result = [] + for pci_id in pci_ids: + if not pci_id.device_id: + break + result.append([ + pci_id.vendor_id, + pci_id.device_id, + pci_id.subsystem_vendor_id, + pci_id.subsystem_device_id, + ]) + return result + + 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.28.0