Blob Blame History Raw
From 5245be196682b78c225e67f211c02e3802ca4bd6 Mon Sep 17 00:00:00 2001
From: Shalini Chellathurai Saroja <shalini@linux.ibm.com>
Date: Wed, 14 Apr 2021 17:18:55 +0200
Subject: [PATCH] virt-install: add mediated device

Add support to install a virtual server with passed-through mediated
device. Mediated device can be created using vGPU attached to
vfio_pci driver or DASD attached to vfio_ccw driver or APQNs attached
to vfio_ap driver.

Reviewed-by: Cole Robinson <crobinso@redhat.com>
Signed-off-by: Shalini Chellathurai Saroja <shalini@linux.ibm.com>
(cherry picked from commit 965480e8bc85caf8a4f36b4a2f07963067b63cf6)

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1995125

Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
---
 .../cli/compare/virt-install-many-devices.xml | 19 +++++++++
 tests/data/testdriver/testdriver.xml          | 40 +++++++++++++++++++
 tests/test_cli.py                             |  3 ++
 tests/test_nodedev.py                         | 30 ++++++++++++++
 virtinst/devices/hostdev.py                   | 36 ++++++++++++++++-
 virtinst/nodedev.py                           |  4 ++
 6 files changed, 131 insertions(+), 1 deletion(-)

diff --git a/tests/data/cli/compare/virt-install-many-devices.xml b/tests/data/cli/compare/virt-install-many-devices.xml
index 49e9dcc7..3bd756b3 100644
--- a/tests/data/cli/compare/virt-install-many-devices.xml
+++ b/tests/data/cli/compare/virt-install-many-devices.xml
@@ -592,6 +592,25 @@
         <char>/dev/pty7</char>
       </source>
     </hostdev>
+    <hostdev mode="subsystem" type="mdev" managed="no" model="vfio-ccw">
+      <address type="ccw" cssid="0xfe" ssid="0x1" devno="0x0008"/>
+      <source>
+        <address uuid="8e37ee90-2b51-45e3-9b25-bf8283c03110"/>
+      </source>
+    </hostdev>
+    <hostdev mode="subsystem" type="mdev" managed="no" model="vfio-ap">
+      <source>
+        <address uuid="11f92c9d-b0b0-4016-b306-a8071277f8b9"/>
+      </source>
+    </hostdev>
+    <hostdev mode="subsystem" type="mdev" managed="yes" model="vfio-pci" display="off" ramfb="off">
+      <address type="pci" domain="0" bus="1" slot="1" function="0">
+        <zpci uid="0x0001" fid="0x00000001"/>
+      </address>
+      <source>
+        <address uuid="4b20d080-1b54-4048-85b3-a6a62d165c01"/>
+      </source>
+    </hostdev>
     <redirdev bus="usb" type="spicevmc"/>
     <redirdev bus="usb" type="tcp">
       <source host="localhost" service="4000"/>
diff --git a/tests/data/testdriver/testdriver.xml b/tests/data/testdriver/testdriver.xml
index ea90f0f7..b8d67bac 100644
--- a/tests/data/testdriver/testdriver.xml
+++ b/tests/data/testdriver/testdriver.xml
@@ -3645,5 +3645,45 @@ ba</description>
   </capability>
 </device>
 
+<device>
+  <name>mdev_8e37ee90_2b51_45e3_9b25_bf8283c03110</name>
+  <path>/sys/devices/css0/0.0.0023/8e37ee90-2b51-45e3-9b25-bf8283c03110</path>
+  <parent>css_0_0_0023</parent>
+  <driver>
+    <name>vfio_mdev</name>
+  </driver>
+  <capability type='mdev'>
+    <type id='vfio_ccw-io'/>
+    <iommuGroup number='0'/>
+  </capability>
+</device>
+
+<device>
+  <name>mdev_11f92c9d_b0b0_4016_b306_a8071277f8b9</name>
+  <path>/sys/devices/vfio_ap/matrix/11f92c9d-b0b0-4016-b306-a8071277f8b9</path>
+  <parent>ap_matrix</parent>
+  <driver>
+    <name>vfio_mdev</name>
+  </driver>
+  <capability type='mdev'>
+    <type id='vfio_ap-passthrough'/>
+    <iommuGroup number='2'/>
+    <attr name='assign_adapter' value='1'/>
+    <attr name='assign_domain' value='2'/>
+  </capability>
+</device>
+
+<device>
+  <name>mdev_4b20d080_1b54_4048_85b3_a6a62d165c01</name>
+  <path>/sys/devices/pci0000:00/0000:00:02.0/4b20d080-1b54-4048-85b3-a6a62d165c01</path>
+  <parent>pci_0000_06_00_0</parent>
+  <driver>
+    <name>vfio_mdev</name>
+  </driver>
+ <capability type='mdev'>
+    <type id='nvidia-11'/>
+    <iommuGroup number='12'/>
+  </capability>
+</device>
 
 </node>
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 5b174933..5f94e009 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -675,6 +675,9 @@ source.reservations.managed=no,source.reservations.source.type=unix,source.reser
 --hostdev wlan0,type=net
 --hostdev /dev/vdz,type=storage
 --hostdev /dev/pty7,type=misc
+--hostdev mdev_8e37ee90_2b51_45e3_9b25_bf8283c03110,address.type=ccw,address.cssid=0xfe,address.ssid=0x1,address.devno=0x0008
+--hostdev mdev_11f92c9d_b0b0_4016_b306_a8071277f8b9
+--hostdev mdev_4b20d080_1b54_4048_85b3_a6a62d165c01,address.type=pci,address.domain=0x0000,address.bus=0x01,address.slot=0x01,address.function=0x0,address.zpci.uid=0x0001,address.zpci.fid=0x00000001
 
 
 --filesystem /source,/target,alias.name=testfsalias,driver.ats=on,driver.iommu=off,driver.packed=on
diff --git a/tests/test_nodedev.py b/tests/test_nodedev.py
index 406e321f..79678bc8 100644
--- a/tests/test_nodedev.py
+++ b/tests/test_nodedev.py
@@ -126,6 +126,36 @@ def testDRMDevice():
     assert dev.get_devnode("frob")
 
 
+def testDASDMdev():
+    conn = utils.URIs.open_testdriver_cached()
+    devname = "mdev_8e37ee90_2b51_45e3_9b25_bf8283c03110"
+    dev = _nodeDevFromName(conn, devname)
+    assert dev.name == devname
+    assert dev.parent == "css_0_0_0023"
+    assert dev.device_type == "mdev"
+    assert dev.type_id == "vfio_ccw-io"
+
+
+def testAPQNMdev():
+    conn = utils.URIs.open_testdriver_cached()
+    devname = "mdev_11f92c9d_b0b0_4016_b306_a8071277f8b9"
+    dev = _nodeDevFromName(conn, devname)
+    assert dev.name == devname
+    assert dev.parent == "ap_matrix"
+    assert dev.device_type == "mdev"
+    assert dev.type_id == "vfio_ap-passthrough"
+
+
+def testPCIMdev():
+    conn = utils.URIs.open_testdriver_cached()
+    devname = "mdev_4b20d080_1b54_4048_85b3_a6a62d165c01"
+    dev = _nodeDevFromName(conn, devname)
+    assert dev.name == devname
+    assert dev.parent == "pci_0000_06_00_0"
+    assert dev.device_type == "mdev"
+    assert dev.type_id == "nvidia-11"
+
+
 # NodeDevice 2 Device XML tests
 
 def testNodeDev2USB1():
diff --git a/virtinst/devices/hostdev.py b/virtinst/devices/hostdev.py
index 3e9de2ad..e8d0fae2 100644
--- a/virtinst/devices/hostdev.py
+++ b/virtinst/devices/hostdev.py
@@ -4,6 +4,8 @@
 # This work is licensed under the GNU GPLv2 or later.
 # See the COPYING file in the top-level directory.
 
+import re
+
 from .device import Device
 from ..nodedev import NodeDevice
 from ..xmlbuilder import XMLProperty
@@ -53,16 +55,45 @@ class DeviceHostdev(Device):
             self.scsi_unit = nodedev.lun
             self.managed = False
 
+        elif nodedev.device_type == nodedev.CAPABILITY_TYPE_MDEV:
+            self.type = "mdev"
+
+            if nodedev.type_id == "vfio_ccw-io":
+                self.model = "vfio-ccw"
+                self.managed = "no"
+
+            elif nodedev.type_id == "vfio_ap-passthrough":
+                self.model = "vfio-ap"
+                self.managed = "no"
+
+            elif (re.match("^nvidia-[0-9]{2}", nodedev.type_id) or
+                  re.match("^i915-GVTg_V[0-9]_[0-9]", nodedev.type_id)):
+                self.model = "vfio-pci"
+                self.managed = "yes"
+                self.display = "off"
+                self.ramfb = "off"
+
+            else:
+                raise ValueError(  # pragma: no cover
+                        _("Don't know how to generate nodedev for mdev type id '%s'") %
+                        nodedev.type_id)
+
+            self.uuid = nodedev.name[5:].replace('_', '-')
+
         else:
             raise ValueError(_("Unknown node device type %s") % nodedev)
 
 
     _XML_PROP_ORDER = ["mode", "type", "managed", "vendor", "product",
-                       "domain", "bus", "slot", "function"]
+                       "domain", "bus", "slot", "function", "model",
+                       "display", "ramfb"]
 
     mode = XMLProperty("./@mode")
     type = XMLProperty("./@type")
     managed = XMLProperty("./@managed", is_yesno=True)
+    model = XMLProperty("./@model")
+    display = XMLProperty("./@display")
+    ramfb = XMLProperty("./@ramfb")
 
     vendor = XMLProperty("./source/vendor/@id")
     product = XMLProperty("./source/product/@id")
@@ -92,6 +123,9 @@ class DeviceHostdev(Device):
     # type=misc handling
     storage_block = XMLProperty("./source/block")
 
+    # type=mdev
+    uuid = XMLProperty("./source/address/@uuid")
+
 
     ##################
     # Default config #
diff --git a/virtinst/nodedev.py b/virtinst/nodedev.py
index 9d2c8f10..97841794 100644
--- a/virtinst/nodedev.py
+++ b/virtinst/nodedev.py
@@ -40,6 +40,7 @@ class NodeDevice(XMLBuilder):
     CAPABILITY_TYPE_SCSIBUS = "scsi_host"
     CAPABILITY_TYPE_SCSIDEV = "scsi"
     CAPABILITY_TYPE_DRM = "drm"
+    CAPABILITY_TYPE_MDEV = "mdev"
 
     @staticmethod
     def lookupNodedevFromString(conn, idstring):
@@ -168,6 +169,9 @@ class NodeDevice(XMLBuilder):
         if len(self.devnodes) > 0:
             return self.devnodes[0]
 
+    # type='mdev' options
+    type_id = XMLProperty("./capability/type/@id")
+
 
 def _AddressStringToHostdev(conn, addrstr):
     from .devices import DeviceHostdev
-- 
2.31.1