43fe83
From 3dfc4bd4183907b922c4f154720826605ac4c51e Mon Sep 17 00:00:00 2001
43fe83
Message-Id: <3dfc4bd4183907b922c4f154720826605ac4c51e.1382534060.git.jdenemar@redhat.com>
43fe83
From: Peter Krempa <pkrempa@redhat.com>
43fe83
Date: Thu, 10 Oct 2013 13:56:34 +0200
43fe83
Subject: [PATCH] qemu: hostdev: Refactor PCI passhrough handling
43fe83
43fe83
https://bugzilla.redhat.com/show_bug.cgi?id=1001738
43fe83
43fe83
To simplify future patches dealing with this code, simplify and refactor
43fe83
some conditions to switch statements.
43fe83
43fe83
(cherry picked from commit 9d13298901f2edb375d54a710480a6bf994cb0e5)
43fe83
43fe83
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
43fe83
---
43fe83
 src/qemu/qemu_command.c | 27 ++++++++++++++++++---------
43fe83
 src/qemu/qemu_hotplug.c | 27 ++++++++++++++++-----------
43fe83
 2 files changed, 34 insertions(+), 20 deletions(-)
43fe83
43fe83
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
43fe83
index d455847..9c9b956 100644
43fe83
--- a/src/qemu/qemu_command.c
43fe83
+++ b/src/qemu/qemu_command.c
43fe83
@@ -5348,14 +5348,25 @@ qemuBuildPCIHostdevDevStr(virDomainDefPtr def,
43fe83
 {
43fe83
     virBuffer buf = VIR_BUFFER_INITIALIZER;
43fe83
 
43fe83
-    if (dev->source.subsys.u.pci.backend
43fe83
-        == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
43fe83
-        virBufferAddLit(&buf, "vfio-pci");
43fe83
-    } else {
43fe83
+    switch ((virDomainHostdevSubsysPciBackendType)
43fe83
+            dev->source.subsys.u.pci.backend) {
43fe83
+    case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_DEFAULT:
43fe83
+    case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_KVM:
43fe83
         virBufferAddLit(&buf, "pci-assign");
43fe83
         if (configfd && *configfd)
43fe83
             virBufferAsprintf(&buf, ",configfd=%s", configfd);
43fe83
+        break;
43fe83
+
43fe83
+    case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO:
43fe83
+        virBufferAddLit(&buf, "vfio-pci");
43fe83
+        break;
43fe83
+
43fe83
+    case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_TYPE_LAST:
43fe83
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
43fe83
+                       _("PCI passhthrough type needs to be specified"));
43fe83
+        break;
43fe83
     }
43fe83
+
43fe83
     virBufferAsprintf(&buf, ",host=%.2x:%.2x.%.1x",
43fe83
                       dev->source.subsys.u.pci.addr.bus,
43fe83
                       dev->source.subsys.u.pci.addr.slot,
43fe83
@@ -9036,7 +9047,6 @@ qemuBuildCommandLine(virConnectPtr conn,
43fe83
         VIR_FREE(devstr);
43fe83
     }
43fe83
 
43fe83
-
43fe83
     /* Add host passthrough hardware */
43fe83
     for (i = 0; i < def->nhostdevs; i++) {
43fe83
         virDomainHostdevDefPtr hostdev = def->hostdevs[i];
43fe83
@@ -9109,9 +9119,9 @@ qemuBuildCommandLine(virConnectPtr conn,
43fe83
         /* PCI */
43fe83
         if (hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
43fe83
             hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) {
43fe83
+            int backend = hostdev->source.subsys.u.pci.backend;
43fe83
 
43fe83
-            if (hostdev->source.subsys.u.pci.backend
43fe83
-                == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
43fe83
+            if (backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
43fe83
                 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VFIO_PCI)) {
43fe83
                     virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
43fe83
                                    _("VFIO PCI device assignment is not "
43fe83
@@ -9125,8 +9135,7 @@ qemuBuildCommandLine(virConnectPtr conn,
43fe83
 
43fe83
             if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
43fe83
                 char *configfd_name = NULL;
43fe83
-                if ((hostdev->source.subsys.u.pci.backend
43fe83
-                     != VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) &&
43fe83
+                if ((backend != VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) &&
43fe83
                     virQEMUCapsGet(qemuCaps, QEMU_CAPS_PCI_CONFIGFD)) {
43fe83
                     int configfd = qemuOpenPCIConfig(hostdev);
43fe83
 
43fe83
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
43fe83
index 7e884c0..0c42b0a 100644
43fe83
--- a/src/qemu/qemu_hotplug.c
43fe83
+++ b/src/qemu/qemu_hotplug.c
43fe83
@@ -1012,6 +1012,7 @@ int qemuDomainAttachHostPciDevice(virQEMUDriverPtr driver,
43fe83
     int configfd = -1;
43fe83
     char *configfd_name = NULL;
43fe83
     bool releaseaddr = false;
43fe83
+    int backend = hostdev->source.subsys.u.pci.backend;
43fe83
 
43fe83
     if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs+1) < 0)
43fe83
         return -1;
43fe83
@@ -1020,10 +1021,8 @@ int qemuDomainAttachHostPciDevice(virQEMUDriverPtr driver,
43fe83
                                      &hostdev, 1) < 0)
43fe83
         return -1;
43fe83
 
43fe83
-    if (hostdev->source.subsys.u.pci.backend
43fe83
-        == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
43fe83
-        unsigned long long memKB;
43fe83
-
43fe83
+    switch ((virDomainHostdevSubsysPciBackendType) backend) {
43fe83
+    case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO:
43fe83
         if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE_VFIO_PCI)) {
43fe83
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
43fe83
                            _("VFIO PCI device assignment is not "
43fe83
@@ -1035,11 +1034,18 @@ int qemuDomainAttachHostPciDevice(virQEMUDriverPtr driver,
43fe83
          * In this case, the guest's memory may already be locked, but it
43fe83
          * doesn't hurt to "change" the limit to the same value.
43fe83
          */
43fe83
-        vm->def->hostdevs[vm->def->nhostdevs++] = hostdev;
43fe83
-        memKB = vm->def->mem.hard_limit ?
43fe83
-            vm->def->mem.hard_limit : vm->def->mem.max_balloon + 1024 * 1024;
43fe83
-        virProcessSetMaxMemLock(vm->pid, memKB);
43fe83
-        vm->def->hostdevs[vm->def->nhostdevs--] = NULL;
43fe83
+        if (vm->def->mem.hard_limit)
43fe83
+            virProcessSetMaxMemLock(vm->pid, vm->def->mem.hard_limit);
43fe83
+        else
43fe83
+            virProcessSetMaxMemLock(vm->pid,
43fe83
+                                    vm->def->mem.max_balloon + (1024 * 1024));
43fe83
+
43fe83
+        break;
43fe83
+
43fe83
+    case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_DEFAULT:
43fe83
+    case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_KVM:
43fe83
+    case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_TYPE_LAST:
43fe83
+        break;
43fe83
     }
43fe83
 
43fe83
     if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
43fe83
@@ -1048,8 +1054,7 @@ int qemuDomainAttachHostPciDevice(virQEMUDriverPtr driver,
43fe83
         if (qemuDomainPCIAddressEnsureAddr(priv->pciaddrs, hostdev->info) < 0)
43fe83
             goto error;
43fe83
         releaseaddr = true;
43fe83
-        if ((hostdev->source.subsys.u.pci.backend
43fe83
-             != VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) &&
43fe83
+        if (backend != VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO &&
43fe83
             virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_PCI_CONFIGFD)) {
43fe83
             configfd = qemuOpenPCIConfig(hostdev);
43fe83
             if (configfd >= 0) {
43fe83
-- 
43fe83
1.8.4
43fe83