[dpdk-dev] [PATCH v2] usertools/dpdk-devbind: add support for PCI wildcards

Bruce Richardson bruce.richardson at intel.com
Mon Aug 24 18:19:24 CEST 2020


On Fri, Aug 21, 2020 at 10:17:14AM +0100, Burakov, Anatoly wrote:
> On 20-Aug-20 4:43 PM, Bruce Richardson wrote:
> > 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 <bruce.richardson at intel.com>
> > Tested-by: Ferruh Yigit <ferruh.yigit at intel.com>
> > ---
> > V2: added help text additions
> > ---
> >   usertools/dpdk-devbind.py | 16 ++++++++++++++++
> >   1 file changed, 16 insertions(+)
> > 
> > diff --git a/usertools/dpdk-devbind.py b/usertools/dpdk-devbind.py
> > index 86b6b53c40..d13defbe1a 100755
> > --- a/usertools/dpdk-devbind.py
> > +++ b/usertools/dpdk-devbind.py
> > @@ -8,6 +8,7 @@
> >   import os
> >   import getopt
> >   import subprocess
> > +from glob import glob
> >   from os.path import exists, abspath, dirname, basename
> >   if sys.version_info.major < 3:
> > @@ -89,6 +90,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 <domain:>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 +148,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 funcions on device 0000:02:00 to ixgbe kernel driver
> > +        %(argv0)s -b ixgbe 02:00.*
> > +
> >       """ % locals())  # replace items from local variables
> > @@ -689,6 +695,16 @@ def parse_args():
> >               else:
> >                   b_flag = arg
> > +    # resolve any PCI globs in the args
> > +    new_args = []
> > +    sysfs_path = "/sys/bus/pci/devices/"
> > +    for arg in args:
> > +        globbed_arg = glob(sysfs_path + arg) + glob(sysfs_path + "0000:" + arg)
> 
> Also, could be
> 
> glob_path = arg if arg.startswith("0000:") else "0000:" + arg
> globbed_arg = glob(os.path.join(sysfs_path, glob_path))
> 
> No need to glob twice :)
> 

Well, the two are not quite equivalent if one assumes that the domain part
can start with something other than 0000. If the domain is e.g. FFFF:, you
don't want to prefix that with 0000 - only if no domain is specified.
Therefore it's safer to glob twice, especially since we are not
particularly concerned about performance here (or else we wouldn't be using
python!).

/Bruce


More information about the dev mailing list