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

Message ID 20221012123817.112225-1-fidaullah.noonari@emumba.com (mailing list archive)
State New
Delegated to: Thomas Monjalon
Headers
Series [v4] 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-mellanox-Performance success Performance Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-aarch64-unit-testing success Testing PASS
ci/github-robot: build success github build: passed

Commit Message

Fidaullah Noonari Oct. 12, 2022, 12:38 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 | 45 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
  

Comments

Stephen Hemminger July 1, 2024, 5:51 p.m. UTC | #1
On Wed, 12 Oct 2022 17:38:17 +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>

Not sure why this patch got stuck.
The flake8 tool reports:


usertools/dpdk-devbind.py:489:27: E231 missing whitespace after ','
Fix by:

diff --git a/usertools/dpdk-devbind.py b/usertools/dpdk-devbind.py
index e7a5665fa2..a089313db1 100755
--- a/usertools/dpdk-devbind.py
+++ b/usertools/dpdk-devbind.py
@@ -486,7 +486,7 @@ def check_noiommu_mode():
     filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
 
     try:
-        with open(filename,"r") as f:
+        with open(filename, "r") as f:
             if f.read(1) == "1":
                 return
     except OSError as err:
  

Patch

diff --git a/usertools/dpdk-devbind.py b/usertools/dpdk-devbind.py
index 4d9c1be666..d875ff23d6 100755
--- a/usertools/dpdk-devbind.py
+++ b/usertools/dpdk-devbind.py
@@ -100,6 +100,7 @@ 
 b_flag = None
 status_flag = False
 force_flag = False
+noiommu_flag = False
 args = []
 
 
@@ -470,6 +471,37 @@  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(f"Error: failed to enable unsafe noiommu mode - Cannot open {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(f"Error: failed to enable unsafe noiommu mode - Cannot open {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
@@ -496,6 +528,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)
 
@@ -638,6 +674,7 @@  def parse_args():
     global status_flag
     global status_dev
     global force_flag
+    global noiommu_flag
     global args
 
     parser = argparse.ArgumentParser(
@@ -691,6 +728,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',
@@ -710,6 +753,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: