Signed-off-by: Jiri Denemark <jdene...@redhat.com>
---

Notes:
    Version 2:
    - adapted to changes in previous patches
    - removed redundant check for qemuCaps->cpuDefinitions != NULL

 src/qemu/qemu_capabilities.c | 54 ++++++++++++++++++++++++++++++++++----------
 src/qemu/qemu_capabilities.h |  4 ++++
 2 files changed, 46 insertions(+), 12 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 18927cb..985b585 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -2371,6 +2371,32 @@ virQEMUCapsGetHostModel(virQEMUCapsPtr qemuCaps)
 }
 
 
+bool
+virQEMUCapsIsCPUModeSupported(virQEMUCapsPtr qemuCaps,
+                              virCapsPtr caps,
+                              virDomainVirtType type,
+                              virCPUMode mode)
+{
+    switch (mode) {
+    case VIR_CPU_MODE_HOST_PASSTHROUGH:
+        return type == VIR_DOMAIN_VIRT_KVM &&
+               virQEMUCapsGuestIsNative(caps->host.arch, qemuCaps->arch);
+
+    case VIR_CPU_MODE_HOST_MODEL:
+        return !!qemuCaps->hostCPUModel;
+
+    case VIR_CPU_MODE_CUSTOM:
+        return qemuCaps->cpuDefinitions &&
+               qemuCaps->cpuDefinitions->nmodels > 0;
+
+    case VIR_CPU_MODE_LAST:
+        break;
+    }
+
+    return false;
+}
+
+
 int virQEMUCapsGetMachineTypesCaps(virQEMUCapsPtr qemuCaps,
                                    size_t *nmachines,
                                    virCapsGuestMachinePtr **machines)
@@ -4358,22 +4384,26 @@ virQEMUCapsFillDomainCPUCaps(virCapsPtr caps,
                              virQEMUCapsPtr qemuCaps,
                              virDomainCapsPtr domCaps)
 {
-    virDomainCapsCPUModelsPtr filtered = NULL;
-    char **models = NULL;
-
-    if (domCaps->virttype == VIR_DOMAIN_VIRT_KVM &&
-        virQEMUCapsGuestIsNative(caps->host.arch, qemuCaps->arch))
+    if (virQEMUCapsIsCPUModeSupported(qemuCaps, caps, domCaps->virttype,
+                                      VIR_CPU_MODE_HOST_PASSTHROUGH))
         domCaps->cpu.hostPassthrough = true;
 
-    domCaps->cpu.hostModel = virCPUDefCopy(qemuCaps->hostCPUModel);
+    if (virQEMUCapsIsCPUModeSupported(qemuCaps, caps, domCaps->virttype,
+                                      VIR_CPU_MODE_HOST_MODEL))
+        domCaps->cpu.hostModel = virCPUDefCopy(qemuCaps->hostCPUModel);
 
-    if (qemuCaps->cpuDefinitions &&
-        cpuGetModels(domCaps->arch, &models) >= 0) {
-        filtered = virDomainCapsCPUModelsFilter(qemuCaps->cpuDefinitions,
-                                                (const char **) models);
-        virStringFreeList(models);
+    if (virQEMUCapsIsCPUModeSupported(qemuCaps, caps, domCaps->virttype,
+                                      VIR_CPU_MODE_CUSTOM)) {
+        virDomainCapsCPUModelsPtr filtered = NULL;
+        char **models = NULL;
+
+        if (cpuGetModels(domCaps->arch, &models) >= 0) {
+            filtered = virDomainCapsCPUModelsFilter(qemuCaps->cpuDefinitions,
+                                                    (const char **) models);
+            virStringFreeList(models);
+        }
+        domCaps->cpu.custom = filtered;
     }
-    domCaps->cpu.custom = filtered;
 
     return 0;
 }
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
index b1d512e..ba0ef48 100644
--- a/src/qemu/qemu_capabilities.h
+++ b/src/qemu/qemu_capabilities.h
@@ -430,6 +430,10 @@ int virQEMUCapsGetCPUDefinitions(virQEMUCapsPtr qemuCaps,
                                  char ***names,
                                  size_t *count);
 virCPUDefPtr virQEMUCapsGetHostModel(virQEMUCapsPtr qemuCaps);
+bool virQEMUCapsIsCPUModeSupported(virQEMUCapsPtr qemuCaps,
+                                   virCapsPtr caps,
+                                   virDomainVirtType type,
+                                   virCPUMode mode);
 const char *virQEMUCapsGetCanonicalMachine(virQEMUCapsPtr qemuCaps,
                                            const char *name);
 int virQEMUCapsGetMachineMaxCpus(virQEMUCapsPtr qemuCaps,
-- 
2.10.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Reply via email to