[PATCH v2 05/21] conf: qemu: validate multifunction hostdevice domain configs

Daniel Henrique Barboza danielhb413 at gmail.com
Thu Jan 30 16:44:17 UTC 2020


From: Shivaprasad G Bhat <sbhat at linux.vnet.ibm.com>

It is invalid to have secondary functions without the primary
functions part of the domain. Prevents new domain define, but
existing ones would not vanish.

Signed-off-by: Shivaprasad G Bhat <sbhat at linux.vnet.ibm.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413 at gmail.com>
---
 src/qemu/qemu_domain.c                        |  3 ++
 src/qemu/qemu_domain_address.c                | 54 +++++++++++++++++++
 src/qemu/qemu_domain_address.h                |  2 +
 .../hostdev-pci-no-primary-function.xml       | 23 ++++++++
 .../hostdev-pci-validate.args                 | 30 +++++++++++
 .../qemuxml2argvdata/hostdev-pci-validate.xml | 29 ++++++++++
 tests/qemuxml2argvtest.c                      |  8 +++
 7 files changed, 149 insertions(+)
 create mode 100644 tests/qemuxml2argvdata/hostdev-pci-no-primary-function.xml
 create mode 100644 tests/qemuxml2argvdata/hostdev-pci-validate.args
 create mode 100644 tests/qemuxml2argvdata/hostdev-pci-validate.xml

diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 05a8d3de38..2de7343882 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -5888,6 +5888,9 @@ qemuDomainDefValidate(const virDomainDef *def,
     if (qemuDomainDefValidateFeatures(def, qemuCaps) < 0)
         goto cleanup;
 
+    if (qemuDomainDefValidatePCIHostdevs(def) < 0)
+        goto cleanup;
+
     if (qemuDomainDefValidateMemory(def, qemuCaps) < 0)
         goto cleanup;
 
diff --git a/src/qemu/qemu_domain_address.c b/src/qemu/qemu_domain_address.c
index 3fed22d720..d9e36867bd 100644
--- a/src/qemu/qemu_domain_address.c
+++ b/src/qemu/qemu_domain_address.c
@@ -2266,6 +2266,60 @@ qemuDomainValidateDevicePCISlotsChipsets(virDomainDefPtr def,
 }
 
 
+static int
+qemuDomainDefPCIHostdevIsPrimaryFunction(qemuDomainPCIHostdevDataPtr data,
+                                         virDomainHostdevDefPtr hostdev)
+{
+    if (!data->device || !hostdev)
+        return 0;
+
+    if ((hostdev->source.subsys.u.pci.addr.function == 0) &&
+        (virHostdevPCIDevicesBelongToSameSlot(data->device, hostdev)))
+        return 1;
+
+    return 0;
+}
+
+
+static int qemuDomainDefValidatePCIMultifunctionHostdev(qemuDomainPCIHostdevDataPtr data,
+                                                        virDomainHostdevDefPtr hostdev)
+{
+    int ret = 0;
+    qemuDomainPCIHostdevdata hostdevIterData = {data->def, NULL, hostdev};
+
+    if (!virHostdevIsPCIMultifunctionDevice(hostdev) ||
+        hostdev->source.subsys.u.pci.addr.function == 0)
+        goto skip;
+
+    /* If the device is non-zero function but its Primary function is not
+     * part of the domain, then error out.
+     */
+    if (!qemuDomainPCIHostDevicesIter(&hostdevIterData,
+                                      qemuDomainDefPCIHostdevIsPrimaryFunction)) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Secondary functions of a PCI multifunction card "
+                         "cannot be assigned to a domain without the "
+                         "Primary function."));
+        ret = -1;
+    }
+
+ skip:
+    return ret;
+}
+
+int qemuDomainDefValidatePCIHostdevs(const virDomainDef *def)
+{
+    qemuDomainPCIHostdevdata hostdevdata = {def, NULL, NULL};
+
+    if (qemuDomainPCIHostDevicesIter(&hostdevdata,
+                                     qemuDomainDefValidatePCIMultifunctionHostdev)) {
+        return -1;
+    }
+
+    return 0;
+}
+
+
 /*
  * This assigns static PCI slots to all configured devices.
  * The ordering here is chosen to match the ordering used
diff --git a/src/qemu/qemu_domain_address.h b/src/qemu/qemu_domain_address.h
index d7f06dd430..a17245dbde 100644
--- a/src/qemu/qemu_domain_address.h
+++ b/src/qemu/qemu_domain_address.h
@@ -57,6 +57,8 @@ void
 qemuDomainSetDeviceSlotAggregateIdx(virDomainDefPtr def,
                                      virDomainDeviceDefPtr dev);
 
+int qemuDomainDefValidatePCIHostdevs(const virDomainDef *def);
+
 int
 qemuDomainDefDeviceFindSlotAggregateIdx(virDomainDefPtr def,
                                         virDomainDeviceDefPtr dev);
diff --git a/tests/qemuxml2argvdata/hostdev-pci-no-primary-function.xml b/tests/qemuxml2argvdata/hostdev-pci-no-primary-function.xml
new file mode 100644
index 0000000000..9cd844bbe7
--- /dev/null
+++ b/tests/qemuxml2argvdata/hostdev-pci-no-primary-function.xml
@@ -0,0 +1,23 @@
+<domain type='kvm'>
+  <name>delete</name>
+  <uuid>583a8e8e-f0ce-4f53-89ab-092862148b25</uuid>
+  <memory unit='KiB'>262144</memory>
+  <vcpu placement='static'>4</vcpu>
+  <os>
+    <type arch='x86_64' machine='pc'>hvm</type>
+  </os>
+  <devices>
+    <emulator>/usr/bin/qemu-system-x86_64</emulator>
+    <controller type='usb' index='0'/>
+    <controller type='ide' index='0'/>
+    <controller type='pci' index='0' model='pci-root'/>
+    <input type='mouse' bus='ps2'/>
+    <input type='keyboard' bus='ps2'/>
+    <hostdev mode='subsystem' type='pci' managed='yes'>
+      <driver name='vfio'/>
+      <source>
+        <address domain='0x0005' bus='0x90' slot='0x01' function='0x1'/>
+      </source>
+    </hostdev>
+  </devices>
+</domain>
diff --git a/tests/qemuxml2argvdata/hostdev-pci-validate.args b/tests/qemuxml2argvdata/hostdev-pci-validate.args
new file mode 100644
index 0000000000..2a8508991e
--- /dev/null
+++ b/tests/qemuxml2argvdata/hostdev-pci-validate.args
@@ -0,0 +1,30 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/tmp/lib/domain--1-delete \
+USER=test \
+LOGNAME=test \
+XDG_DATA_HOME=/tmp/lib/domain--1-delete/.local/share \
+XDG_CACHE_HOME=/tmp/lib/domain--1-delete/.cache \
+XDG_CONFIG_HOME=/tmp/lib/domain--1-delete/.config \
+QEMU_AUDIO_DRV=none \
+/usr/bin/qemu-system-x86_64 \
+-name delete \
+-S \
+-machine pc,accel=kvm,usb=off,dump-guest-core=off \
+-m 256 \
+-realtime mlock=off \
+-smp 4,sockets=4,cores=1,threads=1 \
+-uuid 583a8e8e-f0ce-4f53-89ab-092862148b25 \
+-display none \
+-no-user-config \
+-nodefaults \
+-chardev socket,id=charmonitor,path=/tmp/lib/domain--1-delete/monitor.sock,\
+server,nowait \
+-mon chardev=charmonitor,id=monitor,mode=control \
+-rtc base=utc \
+-no-shutdown \
+-no-acpi \
+-usb \
+-device vfio-pci,host=0000:06:12.1,id=hostdev0,bus=pci.0,addr=0x3 \
+-device vfio-pci,host=0000:06:12.2,id=hostdev1,bus=pci.0,addr=0x4 \
+-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x5
diff --git a/tests/qemuxml2argvdata/hostdev-pci-validate.xml b/tests/qemuxml2argvdata/hostdev-pci-validate.xml
new file mode 100644
index 0000000000..54797c2dda
--- /dev/null
+++ b/tests/qemuxml2argvdata/hostdev-pci-validate.xml
@@ -0,0 +1,29 @@
+<domain type='kvm'>
+  <name>delete</name>
+  <uuid>583a8e8e-f0ce-4f53-89ab-092862148b25</uuid>
+  <memory unit='KiB'>262144</memory>
+  <vcpu placement='static'>4</vcpu>
+  <os>
+    <type arch='x86_64' machine='pc'>hvm</type>
+  </os>
+  <devices>
+    <emulator>/usr/bin/qemu-system-x86_64</emulator>
+    <controller type='usb' index='0'/>
+    <controller type='ide' index='0'/>
+    <controller type='pci' index='0' model='pci-root'/>
+    <input type='mouse' bus='ps2'/>
+    <input type='keyboard' bus='ps2'/>
+    <hostdev mode='subsystem' type='pci' managed='yes'>
+      <driver name='vfio'/>
+      <source>
+        <address domain='0x0000' bus='0x06' slot='0x12' function='0x1'/>
+      </source>
+    </hostdev>
+    <hostdev mode='subsystem' type='pci' managed='yes'>
+      <driver name='vfio'/>
+      <source>
+        <address domain='0x0000' bus='0x06' slot='0x12' function='0x2'/>
+      </source>
+    </hostdev>
+  </devices>
+</domain>
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index dad03f1dec..02375b2611 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -1606,6 +1606,14 @@ mymain(void)
             QEMU_CAPS_DEVICE_VFIO_PCI);
     DO_TEST("hostdev-vfio-multidomain",
             QEMU_CAPS_DEVICE_VFIO_PCI);
+    DO_TEST("hostdev-pci-validate",
+            QEMU_CAPS_KVM,
+            X_QEMU_CAPS_NODEFCONFIG, X_QEMU_CAPS_HOST_PCI_MULTIDOMAIN,
+            QEMU_CAPS_DEVICE_VFIO_PCI);
+    DO_TEST_PARSE_ERROR("hostdev-pci-no-primary-function",
+            QEMU_CAPS_KVM,
+            X_QEMU_CAPS_NODEFCONFIG, X_QEMU_CAPS_HOST_PCI_MULTIDOMAIN,
+            QEMU_CAPS_DEVICE_VFIO_PCI);
     DO_TEST("hostdev-mdev-precreated",
             QEMU_CAPS_DEVICE_VFIO_PCI);
     DO_TEST_PARSE_ERROR("hostdev-mdev-src-address-invalid",
-- 
2.24.1





More information about the libvir-list mailing list