[v3] usertools: add check for IOMMU support in dpdk-devbind
Checks
Commit Message
binding with vfio driver, when IOMMU is disabled, causes program to crash.
this patch adds a flag for noiommmu-mode. when this is set, if IOMMU is
disabled, it changes vfio into unsafe noiommu mode and prints warning
message.
Signed-off-by: Fidaullah Noonari <fidaullah.noonari@emumba.com>
---
usertools/dpdk-devbind.py | 47 +++++++++++++++++++++++++++++++++++++++
1 file changed, 47 insertions(+)
Comments
On 21-Mar-22 12:27 PM, Fidaullah Noonari wrote:
> binding with vfio driver, when IOMMU is disabled, causes program to crash.
> this patch adds a flag for noiommmu-mode. when this is set, if IOMMU is
> disabled, it changes vfio into unsafe noiommu mode and prints warning
> message.
>
> Signed-off-by: Fidaullah Noonari <fidaullah.noonari@emumba.com>
> ---
Hi,
> +def check_noiommu_mode():
> + """checks and enables the noiommu mode for vfio drivers"""
> + global noiommu_flag
> + filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
> +
> + try:
> + with open(filename,"r") as f:
> + if f.read(1) == "1":
> + return
> + except OSError as err:
> + sys.exit("Error: failed to enable unsafe noiommu mode - Cannot open %s: %s"
> + % (filename, err))
Nitpick, but maybe use f-strings instead of old-school string interpolation?
> +
> + if not noiommu_flag:
> + print("Error: failed to bind vfio-pci - IOMMU support is disabled")
> + print("Info: use --noiommu-mode for binding in noiommu mode")
> + sys.exit()
> +
> + try:
> + with open(filename, "w") as f:
> + f.write("1")
> + f.close()
> + except OSError as err:
> + sys.exit("Error: failed to enable unsafe noiommu mode - Cannot open %s: %s"
> + % (filename, err))
Same as above.
Otherwise LGTM,
Acked-by: Anatoly Burakov <anatoly.burakov@intel.com>
On Thu, Mar 31, 2022 at 03:37:40PM +0100, Burakov, Anatoly wrote:
> On 21-Mar-22 12:27 PM, Fidaullah Noonari wrote:
> > binding with vfio driver, when IOMMU is disabled, causes program to crash.
> > this patch adds a flag for noiommmu-mode. when this is set, if IOMMU is
> > disabled, it changes vfio into unsafe noiommu mode and prints warning
> > message.
> >
> > Signed-off-by: Fidaullah Noonari <fidaullah.noonari@emumba.com>
> > ---
>
> Hi,
>
> > +def check_noiommu_mode():
> > + """checks and enables the noiommu mode for vfio drivers"""
> > + global noiommu_flag
> > + filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
> > +
> > + try:
> > + with open(filename,"r") as f:
> > + if f.read(1) == "1":
> > + return
> > + except OSError as err:
> > + sys.exit("Error: failed to enable unsafe noiommu mode - Cannot open %s: %s"
> > + % (filename, err))
>
> Nitpick, but maybe use f-strings instead of old-school string interpolation?
>
Do we still not need to support some distros that don't have Python earlier
than 3.6, where this support was added?
31/03/2022 16:44, Bruce Richardson:
> On Thu, Mar 31, 2022 at 03:37:40PM +0100, Burakov, Anatoly wrote:
> > On 21-Mar-22 12:27 PM, Fidaullah Noonari wrote:
> > > binding with vfio driver, when IOMMU is disabled, causes program to crash.
> > > this patch adds a flag for noiommmu-mode. when this is set, if IOMMU is
> > > disabled, it changes vfio into unsafe noiommu mode and prints warning
> > > message.
> > >
> > > Signed-off-by: Fidaullah Noonari <fidaullah.noonari@emumba.com>
> > > ---
> >
> > Hi,
> >
> > > +def check_noiommu_mode():
> > > + """checks and enables the noiommu mode for vfio drivers"""
> > > + global noiommu_flag
> > > + filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
> > > +
> > > + try:
> > > + with open(filename,"r") as f:
> > > + if f.read(1) == "1":
> > > + return
> > > + except OSError as err:
> > > + sys.exit("Error: failed to enable unsafe noiommu mode - Cannot open %s: %s"
> > > + % (filename, err))
> >
> > Nitpick, but maybe use f-strings instead of old-school string interpolation?
> >
>
> Do we still not need to support some distros that don't have Python earlier
> than 3.6, where this support was added?
The requirements have been upgraded to Python 3.6 with the new pmdinfo.
Are we waiting for a new version?
On Mon, 21 Mar 2022 17:27:27 +0500
Fidaullah Noonari <fidaullah.noonari@emumba.com> wrote:
> binding with vfio driver, when IOMMU is disabled, causes program to crash.
> this patch adds a flag for noiommmu-mode. when this is set, if IOMMU is
> disabled, it changes vfio into unsafe noiommu mode and prints warning
> message.
>
> Signed-off-by: Fidaullah Noonari <fidaullah.noonari@emumba.com>
> ---
Minor indentation issues reported by flake8 python checker:
./usertools/dpdk-devbind.py:489:27: E231 missing whitespace after ','
./usertools/dpdk-devbind.py:494:17: E128 continuation line under-indented for visual indent
./usertools/dpdk-devbind.py:507:17: E128 continuation line under-indented for visual indent
On Mon, 21 Mar 2022 17:27:27 +0500
Fidaullah Noonari <fidaullah.noonari@emumba.com> wrote:
> +
> +def check_noiommu_mode():
> + """checks and enables the noiommu mode for vfio drivers"""
> + global noiommu_flag
> + filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
> +
> + try:
> + with open(filename,"r") as f:
> + if f.read(1) == "1":
> + return
Note: flake8 reports missing comma after filename in the open() statement.
Overall
Acked-by: Stephen Hemminger <stephen@networkplumber.org>
@@ -96,6 +96,7 @@
b_flag = None
status_flag = False
force_flag = False
+noiommu_flag = False
args = []
@@ -466,6 +467,39 @@ def unbind_all(dev_list, force=False):
unbind_one(d, force)
+def has_iommu():
+ """Check if IOMMU is enabled on system"""
+ return len(os.listdir("/sys/class/iommu")) > 0
+
+
+def check_noiommu_mode():
+ """checks and enables the noiommu mode for vfio drivers"""
+ global noiommu_flag
+ filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
+
+ try:
+ with open(filename,"r") as f:
+ if f.read(1) == "1":
+ return
+ except OSError as err:
+ sys.exit("Error: failed to enable unsafe noiommu mode - Cannot open %s: %s"
+ % (filename, err))
+
+ if not noiommu_flag:
+ print("Error: failed to bind vfio-pci - IOMMU support is disabled")
+ print("Info: use --noiommu-mode for binding in noiommu mode")
+ sys.exit()
+
+ try:
+ with open(filename, "w") as f:
+ f.write("1")
+ f.close()
+ except OSError as err:
+ sys.exit("Error: failed to enable unsafe noiommu mode - Cannot open %s: %s"
+ % (filename, err))
+ print("Warning: enabling unsafe no IOMMU mode for vfio drivers")
+
+
def bind_all(dev_list, driver, force=False):
"""Bind method, takes a list of device locations"""
global devices
@@ -492,6 +526,10 @@ def bind_all(dev_list, driver, force=False):
except ValueError as ex:
sys.exit(ex)
+ # check for IOMMU support
+ if driver == "vfio-pci" and not has_iommu():
+ check_noiommu_mode()
+
for d in dev_list:
bind_one(d, driver, force)
@@ -634,6 +672,7 @@ def parse_args():
global status_flag
global status_dev
global force_flag
+ global noiommu_flag
global args
parser = argparse.ArgumentParser(
@@ -687,6 +726,12 @@ def parse_args():
Override restriction on binding devices in use by Linux"
WARNING: This can lead to loss of network connection and should be used with caution.
""")
+ parser.add_argument(
+ '--noiommu-mode',
+ action='store_true',
+ help="""
+if IOMMU is not available, Enables no IOMMU mode for vfio drivers.
+ """)
parser.add_argument(
'devices',
metavar='DEVICE',
@@ -706,6 +751,8 @@ def parse_args():
status_dev = "all"
if opt.force:
force_flag = True
+ if opt.noiommu_mode:
+ noiommu_flag = True
if opt.bind:
b_flag = opt.bind
elif opt.unbind: