Blob Blame History Raw
From 3dfc4bd4183907b922c4f154720826605ac4c51e Mon Sep 17 00:00:00 2001
Message-Id: <3dfc4bd4183907b922c4f154720826605ac4c51e.1382534060.git.jdenemar@redhat.com>
From: Peter Krempa <pkrempa@redhat.com>
Date: Thu, 10 Oct 2013 13:56:34 +0200
Subject: [PATCH] qemu: hostdev: Refactor PCI passhrough handling

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

To simplify future patches dealing with this code, simplify and refactor
some conditions to switch statements.

(cherry picked from commit 9d13298901f2edb375d54a710480a6bf994cb0e5)

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/qemu/qemu_command.c | 27 ++++++++++++++++++---------
 src/qemu/qemu_hotplug.c | 27 ++++++++++++++++-----------
 2 files changed, 34 insertions(+), 20 deletions(-)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index d455847..9c9b956 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -5348,14 +5348,25 @@ qemuBuildPCIHostdevDevStr(virDomainDefPtr def,
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;
 
-    if (dev->source.subsys.u.pci.backend
-        == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
-        virBufferAddLit(&buf, "vfio-pci");
-    } else {
+    switch ((virDomainHostdevSubsysPciBackendType)
+            dev->source.subsys.u.pci.backend) {
+    case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_DEFAULT:
+    case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_KVM:
         virBufferAddLit(&buf, "pci-assign");
         if (configfd && *configfd)
             virBufferAsprintf(&buf, ",configfd=%s", configfd);
+        break;
+
+    case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO:
+        virBufferAddLit(&buf, "vfio-pci");
+        break;
+
+    case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_TYPE_LAST:
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("PCI passhthrough type needs to be specified"));
+        break;
     }
+
     virBufferAsprintf(&buf, ",host=%.2x:%.2x.%.1x",
                       dev->source.subsys.u.pci.addr.bus,
                       dev->source.subsys.u.pci.addr.slot,
@@ -9036,7 +9047,6 @@ qemuBuildCommandLine(virConnectPtr conn,
         VIR_FREE(devstr);
     }
 
-
     /* Add host passthrough hardware */
     for (i = 0; i < def->nhostdevs; i++) {
         virDomainHostdevDefPtr hostdev = def->hostdevs[i];
@@ -9109,9 +9119,9 @@ qemuBuildCommandLine(virConnectPtr conn,
         /* PCI */
         if (hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
             hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) {
+            int backend = hostdev->source.subsys.u.pci.backend;
 
-            if (hostdev->source.subsys.u.pci.backend
-                == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
+            if (backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
                 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VFIO_PCI)) {
                     virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                                    _("VFIO PCI device assignment is not "
@@ -9125,8 +9135,7 @@ qemuBuildCommandLine(virConnectPtr conn,
 
             if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
                 char *configfd_name = NULL;
-                if ((hostdev->source.subsys.u.pci.backend
-                     != VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) &&
+                if ((backend != VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) &&
                     virQEMUCapsGet(qemuCaps, QEMU_CAPS_PCI_CONFIGFD)) {
                     int configfd = qemuOpenPCIConfig(hostdev);
 
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 7e884c0..0c42b0a 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -1012,6 +1012,7 @@ int qemuDomainAttachHostPciDevice(virQEMUDriverPtr driver,
     int configfd = -1;
     char *configfd_name = NULL;
     bool releaseaddr = false;
+    int backend = hostdev->source.subsys.u.pci.backend;
 
     if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs+1) < 0)
         return -1;
@@ -1020,10 +1021,8 @@ int qemuDomainAttachHostPciDevice(virQEMUDriverPtr driver,
                                      &hostdev, 1) < 0)
         return -1;
 
-    if (hostdev->source.subsys.u.pci.backend
-        == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
-        unsigned long long memKB;
-
+    switch ((virDomainHostdevSubsysPciBackendType) backend) {
+    case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO:
         if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE_VFIO_PCI)) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                            _("VFIO PCI device assignment is not "
@@ -1035,11 +1034,18 @@ int qemuDomainAttachHostPciDevice(virQEMUDriverPtr driver,
          * In this case, the guest's memory may already be locked, but it
          * doesn't hurt to "change" the limit to the same value.
          */
-        vm->def->hostdevs[vm->def->nhostdevs++] = hostdev;
-        memKB = vm->def->mem.hard_limit ?
-            vm->def->mem.hard_limit : vm->def->mem.max_balloon + 1024 * 1024;
-        virProcessSetMaxMemLock(vm->pid, memKB);
-        vm->def->hostdevs[vm->def->nhostdevs--] = NULL;
+        if (vm->def->mem.hard_limit)
+            virProcessSetMaxMemLock(vm->pid, vm->def->mem.hard_limit);
+        else
+            virProcessSetMaxMemLock(vm->pid,
+                                    vm->def->mem.max_balloon + (1024 * 1024));
+
+        break;
+
+    case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_DEFAULT:
+    case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_KVM:
+    case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_TYPE_LAST:
+        break;
     }
 
     if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
@@ -1048,8 +1054,7 @@ int qemuDomainAttachHostPciDevice(virQEMUDriverPtr driver,
         if (qemuDomainPCIAddressEnsureAddr(priv->pciaddrs, hostdev->info) < 0)
             goto error;
         releaseaddr = true;
-        if ((hostdev->source.subsys.u.pci.backend
-             != VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) &&
+        if (backend != VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO &&
             virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_PCI_CONFIGFD)) {
             configfd = qemuOpenPCIConfig(hostdev);
             if (configfd >= 0) {
-- 
1.8.4