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 E69C2A04B1; Mon, 24 Aug 2020 19:04:53 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 02BFEF72; Mon, 24 Aug 2020 19:04:53 +0200 (CEST) Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by dpdk.org (Postfix) with ESMTP id 230BDF12 for ; Mon, 24 Aug 2020 19:04:50 +0200 (CEST) IronPort-SDR: OtuzjmqaQ6WqMSDqkI9qOfkza+MtPkbJaiYLffLPaMiuPsMfTbLn6eSDXbAPRE7//RqfYai7mS u6m/Q2yoKVQA== X-IronPort-AV: E=McAfee;i="6000,8403,9723"; a="153529890" X-IronPort-AV: E=Sophos;i="5.76,349,1592895600"; d="scan'208";a="153529890" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Aug 2020 10:04:49 -0700 IronPort-SDR: yCZBlqUKJYKH6Y3q0OtB0uhRVe4ICHP3In8Ssp5k2Nt6cWHFt2lNhlKpDxN+RkqCC26yhHGuK1 LzdibIdl21CA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.76,349,1592895600"; d="scan'208";a="322400831" Received: from silpixa00399126.ir.intel.com ([10.237.222.56]) by fmsmga004.fm.intel.com with ESMTP; 24 Aug 2020 10:04:48 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: Ferruh Yigit , Anatoly Burakov , Bruce Richardson Date: Mon, 24 Aug 2020 18:04:39 +0100 Message-Id: <20200824170439.62564-1-bruce.richardson@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200820122355.3357-1-bruce.richardson@intel.com> References: <20200820122355.3357-1-bruce.richardson@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v4] usertools/dpdk-devbind: add support for PCI wildcards 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" When binding or unbinding a range of devices, it can be useful to use wildcards to specify the devices rather than repeating the same prefix multiple times. We can use the python "glob" module to give us this functionality - at least for PCI devices - by checking /sys for matching files. Examples of use from my system: ./dpdk-devbind.py -b vfio-pci 80:04.* ./dpdk-devbind.py -u 80:04.[2-7] The first example binds eight devices, 80:04.0..80:04.7, to vfio-pci. The second then unbinds six of those devices, 80:04.2..80:04.7, from any driver. Signed-off-by: Bruce Richardson --- V4: change path manipulation to use os.path.join and basename separated pci_glob out into separate function with doc string [dropped Ferruh's test-by due to scope of changes] V3: fix typo in help text additions V2: added help text additions --- usertools/dpdk-devbind.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/usertools/dpdk-devbind.py b/usertools/dpdk-devbind.py index 86b6b53c40..6c23d243fa 100755 --- a/usertools/dpdk-devbind.py +++ b/usertools/dpdk-devbind.py @@ -8,7 +8,9 @@ import os import getopt import subprocess +from glob import glob from os.path import exists, abspath, dirname, basename +from os.path import join as path_join if sys.version_info.major < 3: print("WARNING: Python 2 is deprecated for use in DPDK, and will not work in future releases.", file=sys.stderr) @@ -89,6 +91,8 @@ def usage(): where DEVICE1, DEVICE2 etc, are specified via PCI "domain:bus:slot.func" syntax or "bus:slot.func" syntax. For devices bound to Linux kernel drivers, they may also be referred to by Linux interface name e.g. eth0, eth1, em0, em1, etc. +If devices are specified using PCI bus:device:func format, then +shell wildcards and ranges may be used, e.g. 80:04.*, 80:04.[0-3] Options: --help, --usage: @@ -145,6 +149,9 @@ def usage(): To bind 0000:02:00.0 and 0000:02:00.1 to the ixgbe kernel driver %(argv0)s -b ixgbe 02:00.0 02:00.1 +To bind all functions on device 0000:02:00 to ixgbe kernel driver + %(argv0)s -b ixgbe 02:00.* + """ % locals()) # replace items from local variables @@ -648,6 +655,19 @@ def show_status(): if status_dev == "misc" or status_dev == "all": show_device_status(misc_devices, "Misc (rawdev)") + +def pci_glob(arg): + '''Returns a list containing either: + * List of PCI B:D:F matching arg, using shell wildcards e.g. 80:04.* + * Only the passed arg if matching list is empty''' + sysfs_path = "/sys/bus/pci/devices" + for _glob in [arg, '0000:' + arg]: + paths = [basename(path) for path in glob(path_join(sysfs_path, _glob))] + if paths: + return paths + return [arg] + + def parse_args(): '''Parses the command-line arguments given by the user and takes the appropriate action for each''' @@ -689,6 +709,11 @@ def parse_args(): else: b_flag = arg + # resolve any PCI globs in the args + new_args = [] + for arg in args: + new_args.extend(pci_glob(arg)) + args = new_args def do_arg_actions(): '''do the actual action requested by the user''' -- 2.25.1