aff97e
From 5237e9b27b8102c6f91015f33bab8e892c5fa82b Mon Sep 17 00:00:00 2001
aff97e
From: Cole Robinson <crobinso@redhat.com>
aff97e
Date: Tue, 23 Sep 2014 11:35:57 -0400
aff97e
Subject: [PATCH] qemu_command: Split qemuBuildCpuArgStr
aff97e
aff97e
Move the CPU mode/model handling to its own function. This is just
aff97e
code movement and re-indentation.
aff97e
aff97e
(cherry picked from commit e1d872dc77c80d43036f928f83f560f2e9286148)
aff97e
---
aff97e
 src/qemu/qemu_command.c | 226 ++++++++++++++++++++++++++----------------------
aff97e
 1 file changed, 122 insertions(+), 104 deletions(-)
aff97e
aff97e
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
aff97e
index 718533b..67ff000 100644
aff97e
--- a/src/qemu/qemu_command.c
aff97e
+++ b/src/qemu/qemu_command.c
aff97e
@@ -6052,139 +6052,162 @@ qemuBuildClockArgStr(virDomainClockDefPtr def)
aff97e
     return NULL;
aff97e
 }
aff97e
 
aff97e
-
aff97e
 static int
aff97e
-qemuBuildCpuArgStr(virQEMUDriverPtr driver,
aff97e
-                   const virDomainDef *def,
aff97e
-                   const char *emulator,
aff97e
-                   virQEMUCapsPtr qemuCaps,
aff97e
-                   virArch hostarch,
aff97e
-                   char **opt,
aff97e
-                   bool *hasHwVirt,
aff97e
-                   bool migrating)
aff97e
+qemuBuildCpuModelArgStr(virQEMUDriverPtr driver,
aff97e
+                        const virDomainDef *def,
aff97e
+                        virBufferPtr buf,
aff97e
+                        virQEMUCapsPtr qemuCaps,
aff97e
+                        bool *hasHwVirt,
aff97e
+                        bool migrating)
aff97e
 {
aff97e
+    int ret = -1;
aff97e
+    size_t i;
aff97e
     virCPUDefPtr host = NULL;
aff97e
     virCPUDefPtr guest = NULL;
aff97e
     virCPUDefPtr cpu = NULL;
aff97e
     size_t ncpus = 0;
aff97e
     char **cpus = NULL;
aff97e
-    const char *default_model;
aff97e
     virCPUDataPtr data = NULL;
aff97e
-    bool have_cpu = false;
aff97e
     char *compare_msg = NULL;
aff97e
-    int ret = -1;
aff97e
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
aff97e
-    size_t i;
aff97e
+    virCPUCompareResult cmp;
aff97e
+    const char *preferred;
aff97e
     virCapsPtr caps = NULL;
aff97e
 
aff97e
-    *hasHwVirt = false;
aff97e
-
aff97e
     if (!(caps = virQEMUDriverGetCapabilities(driver, false)))
aff97e
         goto cleanup;
aff97e
 
aff97e
     host = caps->host.cpu;
aff97e
 
aff97e
-    if (def->os.arch == VIR_ARCH_I686)
aff97e
-        default_model = "qemu32";
aff97e
-    else
aff97e
-        default_model = "qemu64";
aff97e
+    if (!host ||
aff97e
+        !host->model ||
aff97e
+        (ncpus = virQEMUCapsGetCPUDefinitions(qemuCaps, &cpus)) == 0) {
aff97e
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
aff97e
+                       _("CPU specification not supported by hypervisor"));
aff97e
+        goto cleanup;
aff97e
+    }
aff97e
 
aff97e
-    if (def->cpu &&
aff97e
-        (def->cpu->mode != VIR_CPU_MODE_CUSTOM || def->cpu->model)) {
aff97e
-        virCPUCompareResult cmp;
aff97e
-        const char *preferred;
aff97e
+    if (!(cpu = virCPUDefCopy(def->cpu)))
aff97e
+        goto cleanup;
aff97e
+
aff97e
+    if (cpu->mode != VIR_CPU_MODE_CUSTOM &&
aff97e
+        !migrating &&
aff97e
+        cpuUpdate(cpu, host) < 0)
aff97e
+        goto cleanup;
aff97e
 
aff97e
-        if (!host ||
aff97e
-            !host->model ||
aff97e
-            (ncpus = virQEMUCapsGetCPUDefinitions(qemuCaps, &cpus)) == 0) {
aff97e
+    cmp = cpuGuestData(host, cpu, &data, &compare_msg);
aff97e
+    switch (cmp) {
aff97e
+    case VIR_CPU_COMPARE_INCOMPATIBLE:
aff97e
+        if (compare_msg) {
aff97e
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
aff97e
+                           _("guest and host CPU are not compatible: %s"),
aff97e
+                           compare_msg);
aff97e
+        } else {
aff97e
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
aff97e
-                           _("CPU specification not supported by hypervisor"));
aff97e
-            goto cleanup;
aff97e
+                           _("guest CPU is not compatible with host CPU"));
aff97e
         }
aff97e
+        /* fall through */
aff97e
+    case VIR_CPU_COMPARE_ERROR:
aff97e
+        goto cleanup;
aff97e
 
aff97e
-        if (!(cpu = virCPUDefCopy(def->cpu)))
aff97e
+    default:
aff97e
+        break;
aff97e
+    }
aff97e
+
aff97e
+    /* Only 'svm' requires --enable-nesting. The nested
aff97e
+     * 'vmx' patches now simply hook off the CPU features
aff97e
+     */
aff97e
+    if (def->os.arch == VIR_ARCH_X86_64 ||
aff97e
+        def->os.arch == VIR_ARCH_I686) {
aff97e
+        int hasSVM = cpuHasFeature(data, "svm");
aff97e
+        if (hasSVM < 0)
aff97e
             goto cleanup;
aff97e
+        *hasHwVirt = hasSVM > 0 ? true : false;
aff97e
+    }
aff97e
 
aff97e
-        if (cpu->mode != VIR_CPU_MODE_CUSTOM &&
aff97e
-            !migrating &&
aff97e
-            cpuUpdate(cpu, host) < 0)
aff97e
+    if (cpu->mode == VIR_CPU_MODE_HOST_PASSTHROUGH) {
aff97e
+        const char *mode = virCPUModeTypeToString(cpu->mode);
aff97e
+        if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_CPU_HOST)) {
aff97e
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
aff97e
+                           _("CPU mode '%s' is not supported by QEMU"
aff97e
+                             " binary"), mode);
aff97e
+            goto cleanup;
aff97e
+        }
aff97e
+        if (def->virtType != VIR_DOMAIN_VIRT_KVM) {
aff97e
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
aff97e
+                           _("CPU mode '%s' is only supported with kvm"),
aff97e
+                           mode);
aff97e
+            goto cleanup;
aff97e
+        }
aff97e
+        virBufferAddLit(buf, "host");
aff97e
+    } else {
aff97e
+        if (VIR_ALLOC(guest) < 0)
aff97e
+            goto cleanup;
aff97e
+        if (VIR_STRDUP(guest->vendor_id, cpu->vendor_id) < 0)
aff97e
             goto cleanup;
aff97e
 
aff97e
-        cmp = cpuGuestData(host, cpu, &data, &compare_msg);
aff97e
-        switch (cmp) {
aff97e
-        case VIR_CPU_COMPARE_INCOMPATIBLE:
aff97e
-            if (compare_msg) {
aff97e
-                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
aff97e
-                               _("guest and host CPU are not compatible: %s"),
aff97e
-                               compare_msg);
aff97e
-            } else {
aff97e
-                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
aff97e
-                               _("guest CPU is not compatible with host CPU"));
aff97e
-            }
aff97e
-            /* fall through */
aff97e
-        case VIR_CPU_COMPARE_ERROR:
aff97e
+        guest->arch = host->arch;
aff97e
+        if (cpu->match == VIR_CPU_MATCH_MINIMUM)
aff97e
+            preferred = host->model;
aff97e
+        else
aff97e
+            preferred = cpu->model;
aff97e
+
aff97e
+        guest->type = VIR_CPU_TYPE_GUEST;
aff97e
+        guest->fallback = cpu->fallback;
aff97e
+        if (cpuDecode(guest, data, (const char **)cpus, ncpus, preferred) < 0)
aff97e
             goto cleanup;
aff97e
 
aff97e
-        default:
aff97e
-            break;
aff97e
-        }
aff97e
+        virBufferAdd(buf, guest->model, -1);
aff97e
+        if (guest->vendor_id)
aff97e
+            virBufferAsprintf(buf, ",vendor=%s", guest->vendor_id);
aff97e
+        for (i = 0; i < guest->nfeatures; i++) {
aff97e
+            char sign;
aff97e
+            if (guest->features[i].policy == VIR_CPU_FEATURE_DISABLE)
aff97e
+                sign = '-';
aff97e
+            else
aff97e
+                sign = '+';
aff97e
 
aff97e
-        /* Only 'svm' requires --enable-nesting. The nested
aff97e
-         * 'vmx' patches now simply hook off the CPU features
aff97e
-         */
aff97e
-        if (def->os.arch == VIR_ARCH_X86_64 ||
aff97e
-            def->os.arch == VIR_ARCH_I686) {
aff97e
-            int hasSVM = cpuHasFeature(data, "svm");
aff97e
-            if (hasSVM < 0)
aff97e
-                goto cleanup;
aff97e
-            *hasHwVirt = hasSVM > 0 ? true : false;
aff97e
+            virBufferAsprintf(buf, ",%c%s", sign, guest->features[i].name);
aff97e
         }
aff97e
+    }
aff97e
 
aff97e
-        if (cpu->mode == VIR_CPU_MODE_HOST_PASSTHROUGH) {
aff97e
-            const char *mode = virCPUModeTypeToString(cpu->mode);
aff97e
-            if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_CPU_HOST)) {
aff97e
-                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
aff97e
-                               _("CPU mode '%s' is not supported by QEMU"
aff97e
-                                 " binary"), mode);
aff97e
-                goto cleanup;
aff97e
-            }
aff97e
-            if (def->virtType != VIR_DOMAIN_VIRT_KVM) {
aff97e
-                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
aff97e
-                               _("CPU mode '%s' is only supported with kvm"),
aff97e
-                               mode);
aff97e
-                goto cleanup;
aff97e
-            }
aff97e
-            virBufferAddLit(&buf, "host");
aff97e
-        } else {
aff97e
-            if (VIR_ALLOC(guest) < 0)
aff97e
-                goto cleanup;
aff97e
-            if (VIR_STRDUP(guest->vendor_id, cpu->vendor_id) < 0)
aff97e
-                goto cleanup;
aff97e
+    ret = 0;
aff97e
+cleanup:
aff97e
+    virObjectUnref(caps);
aff97e
+    VIR_FREE(compare_msg);
aff97e
+    cpuDataFree(data);
aff97e
+    virCPUDefFree(guest);
aff97e
+    virCPUDefFree(cpu);
aff97e
+    return ret;
aff97e
+}
aff97e
 
aff97e
-            guest->arch = host->arch;
aff97e
-            if (cpu->match == VIR_CPU_MATCH_MINIMUM)
aff97e
-                preferred = host->model;
aff97e
-            else
aff97e
-                preferred = cpu->model;
aff97e
+static int
aff97e
+qemuBuildCpuArgStr(virQEMUDriverPtr driver,
aff97e
+                   const virDomainDef *def,
aff97e
+                   const char *emulator,
aff97e
+                   virQEMUCapsPtr qemuCaps,
aff97e
+                   virArch hostarch,
aff97e
+                   char **opt,
aff97e
+                   bool *hasHwVirt,
aff97e
+                   bool migrating)
aff97e
+{
aff97e
+    const char *default_model;
aff97e
+    bool have_cpu = false;
aff97e
+    int ret = -1;
aff97e
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
aff97e
+    size_t i;
aff97e
 
aff97e
-            guest->type = VIR_CPU_TYPE_GUEST;
aff97e
-            guest->fallback = cpu->fallback;
aff97e
-            if (cpuDecode(guest, data, (const char **)cpus, ncpus, preferred) < 0)
aff97e
-                goto cleanup;
aff97e
+    *hasHwVirt = false;
aff97e
 
aff97e
-            virBufferAdd(&buf, guest->model, -1);
aff97e
-            if (guest->vendor_id)
aff97e
-                virBufferAsprintf(&buf, ",vendor=%s", guest->vendor_id);
aff97e
-            for (i = 0; i < guest->nfeatures; i++) {
aff97e
-                char sign;
aff97e
-                if (guest->features[i].policy == VIR_CPU_FEATURE_DISABLE)
aff97e
-                    sign = '-';
aff97e
-                else
aff97e
-                    sign = '+';
aff97e
+    if (def->os.arch == VIR_ARCH_I686)
aff97e
+        default_model = "qemu32";
aff97e
+    else
aff97e
+        default_model = "qemu64";
aff97e
 
aff97e
-                virBufferAsprintf(&buf, ",%c%s", sign, guest->features[i].name);
aff97e
-            }
aff97e
-        }
aff97e
+    if (def->cpu &&
aff97e
+        (def->cpu->mode != VIR_CPU_MODE_CUSTOM || def->cpu->model)) {
aff97e
+        if (qemuBuildCpuModelArgStr(driver, def, &buf, qemuCaps,
aff97e
+                                    hasHwVirt, migrating) < 0)
aff97e
+            goto cleanup;
aff97e
         have_cpu = true;
aff97e
     } else {
aff97e
         /*
aff97e
@@ -6309,11 +6332,6 @@ qemuBuildCpuArgStr(virQEMUDriverPtr driver,
aff97e
     ret = 0;
aff97e
 
aff97e
  cleanup:
aff97e
-    VIR_FREE(compare_msg);
aff97e
-    cpuDataFree(data);
aff97e
-    virCPUDefFree(guest);
aff97e
-    virCPUDefFree(cpu);
aff97e
-    virObjectUnref(caps);
aff97e
     return ret;
aff97e
 }
aff97e