[v3] usertools: add check for IOMMU support in dpdk-devbind

Message ID 20220321122727.614290-1-fidaullah.noonari@emumba.com (mailing list archive)
State Changes Requested, archived
Delegated to: Thomas Monjalon
Headers
Series [v3] usertools: add check for IOMMU support in dpdk-devbind |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/github-robot: build success github build: passed
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-broadcom-Functional fail Functional Testing issues

Commit Message

Fidaullah Noonari March 21, 2022, 12:27 p.m. UTC
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

Burakov, Anatoly March 31, 2022, 2:37 p.m. UTC | #1
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>
  
Bruce Richardson March 31, 2022, 2:44 p.m. UTC | #2
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?
  
Thomas Monjalon Oct. 10, 2022, 11:02 p.m. UTC | #3
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?
  
Stephen Hemminger July 6, 2023, 6:22 p.m. UTC | #4
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
  
Stephen Hemminger Oct. 31, 2023, 6:34 p.m. UTC | #5
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>
  

Patch

diff --git a/usertools/dpdk-devbind.py b/usertools/dpdk-devbind.py
index ace4627218..e2181efac8 100755
--- a/usertools/dpdk-devbind.py
+++ b/usertools/dpdk-devbind.py
@@ -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: