]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemu: Enforce guest CPU specification
authorJiri Denemark <jdenemar@redhat.com>
Tue, 14 Mar 2017 14:05:02 +0000 (15:05 +0100)
committerJiri Denemark <jdenemar@redhat.com>
Fri, 17 Mar 2017 10:50:48 +0000 (11:50 +0100)
When guest CPU definition uses VIR_CPU_CHECK_FULL checks, we need to
make sure QEMU does not add or remove any features.

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

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
src/cpu/cpu.c
src/cpu/cpu_x86.c

index 992a0339cc7f6a9e085651b3bc5c1253ae0c0d55..1461190bac2a08541804c4d5961ac65136ddbc1b 100644 (file)
@@ -722,7 +722,8 @@ virCPUUpdate(virArch arch,
  *                hypervisor
  *
  * Update custom mode CPU according to the virtual CPU created by the
- * hypervisor.
+ * hypervisor. The function refuses to update the CPU in case cpu->check is set
+ * to VIR_CPU_CHECK_FULL.
  *
  * Returns -1 on error,
  *          0 when the CPU was successfully updated,
index a43bb2bdf20f22b8015d5e0e2bfe19f03f7d46b5..9e208b0948258d82e4b24a3a23dd858048f34245 100644 (file)
@@ -2686,6 +2686,10 @@ virCPUx86UpdateLive(virCPUDefPtr cpu,
     virCPUx86ModelPtr model = NULL;
     virCPUx86Data enabled = VIR_CPU_X86_DATA_INIT;
     virCPUx86Data disabled = VIR_CPU_X86_DATA_INIT;
+    virBuffer bufAdded = VIR_BUFFER_INITIALIZER;
+    virBuffer bufRemoved = VIR_BUFFER_INITIALIZER;
+    char *added = NULL;
+    char *removed = NULL;
     size_t i;
     int ret = -1;
 
@@ -2709,28 +2713,70 @@ virCPUx86UpdateLive(virCPUDefPtr cpu,
         virCPUx86FeaturePtr feature = map->features[i];
 
         if (x86DataIsSubset(&enabled, &feature->data)) {
-            VIR_DEBUG("Adding feature '%s' enabled by the hypervisor",
-                      feature->name);
-            if (virCPUDefUpdateFeature(cpu, feature->name,
-                                       VIR_CPU_FEATURE_REQUIRE) < 0)
+            VIR_DEBUG("Feature '%s' enabled by the hypervisor", feature->name);
+            if (cpu->check == VIR_CPU_CHECK_FULL)
+                virBufferAsprintf(&bufAdded, "%s,", feature->name);
+            else if (virCPUDefUpdateFeature(cpu, feature->name,
+                                            VIR_CPU_FEATURE_REQUIRE) < 0)
                 goto cleanup;
         }
 
         if (x86DataIsSubset(&disabled, &feature->data)) {
-            VIR_DEBUG("Removing feature '%s' disabled by the hypervisor",
-                      feature->name);
-            if (virCPUDefUpdateFeature(cpu, feature->name,
-                                       VIR_CPU_FEATURE_DISABLE) < 0)
+            VIR_DEBUG("Feature '%s' disabled by the hypervisor", feature->name);
+            if (cpu->check == VIR_CPU_CHECK_FULL)
+                virBufferAsprintf(&bufRemoved, "%s,", feature->name);
+            else if (virCPUDefUpdateFeature(cpu, feature->name,
+                                            VIR_CPU_FEATURE_DISABLE) < 0)
                 goto cleanup;
         }
     }
 
+    virBufferTrim(&bufAdded, ",", -1);
+    virBufferTrim(&bufRemoved, ",", -1);
+
+    if (virBufferCheckError(&bufAdded) < 0 ||
+        virBufferCheckError(&bufRemoved) < 0)
+        goto cleanup;
+
+    added = virBufferContentAndReset(&bufAdded);
+    removed = virBufferContentAndReset(&bufRemoved);
+
+    if (added || removed) {
+        if (added && removed)
+            virReportError(VIR_ERR_OPERATION_FAILED,
+                           _("guest CPU doesn't match specification: "
+                             "extra features: %s, missing features: %s"),
+                           added, removed);
+        else if (added)
+            virReportError(VIR_ERR_OPERATION_FAILED,
+                           _("guest CPU doesn't match specification: "
+                             "extra features: %s"),
+                           added);
+        else
+            virReportError(VIR_ERR_OPERATION_FAILED,
+                           _("guest CPU doesn't match specification: "
+                             "missing features: %s"),
+                           removed);
+        goto cleanup;
+    }
+
+    if (cpu->check == VIR_CPU_CHECK_FULL &&
+        !x86DataIsEmpty(&disabled)) {
+        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+                       _("guest CPU doesn't match specification"));
+        goto cleanup;
+    }
+
     ret = 0;
 
  cleanup:
     x86ModelFree(model);
     virCPUx86DataClear(&enabled);
     virCPUx86DataClear(&disabled);
+    VIR_FREE(added);
+    VIR_FREE(removed);
+    virBufferFreeAndReset(&bufAdded);
+    virBufferFreeAndReset(&bufRemoved);
     return ret;
 }