[libvirt] [PATCH v2 05/22] cpu: Use virDomainCapsCPUModelsPtr in cpu driver APIs

Jiri Denemark jdenemar at redhat.com
Fri Oct 13 18:14:37 UTC 2017


All APIs which expect a list of CPU models supported by hypervisors were
switched from char **models and int models to just accept a pointer to
virDomainCapsCPUModels object stored in domain capabilities. This avoids
the need to transform virDomainCapsCPUModelsPtr into a NULL-terminated
list of model names and also allows the various cpu driver APIs to
access additional details (such as its usability) about each CPU model.

Signed-off-by: Jiri Denemark <jdenemar at redhat.com>
Reviewed-by: John Ferlan <jferlan at redhat.com>
---

Notes:
    Version 2:
    - updated debug message
    - dropped unreachable return statement

 src/cpu/cpu.c                  | 78 +++++++++++++-----------------------
 src/cpu/cpu.h                  | 28 +++++--------
 src/cpu/cpu_arm.c              |  3 +-
 src/cpu/cpu_ppc64.c            | 13 +++---
 src/cpu/cpu_x86.c              | 25 +++++-------
 src/libxl/libxl_capabilities.c |  2 +-
 src/libxl/libxl_driver.c       |  2 +-
 src/qemu/qemu_capabilities.c   | 63 +++++-------------------------
 src/qemu/qemu_capabilities.h   |  6 +--
 src/qemu/qemu_driver.c         |  2 +-
 src/qemu/qemu_process.c        |  9 +----
 src/test/test_driver.c         |  2 +-
 tests/cputest.c                | 89 ++++++++++++++++++++++++++++++------------
 13 files changed, 137 insertions(+), 185 deletions(-)

diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c
index dc72ed42d8..42b836354d 100644
--- a/src/cpu/cpu.c
+++ b/src/cpu/cpu.c
@@ -192,7 +192,6 @@ virCPUCompare(virArch arch,
  * @cpu: CPU definition stub to be filled in
  * @data: internal CPU data to be decoded into @cpu definition
  * @models: list of CPU models that can be considered when decoding @data
- * @nmodels: number of CPU models in @models
  * @preferred: CPU models that should be used if possible
  *
  * Decodes internal CPU data into a CPU definition consisting of a CPU model
@@ -214,24 +213,17 @@ virCPUCompare(virArch arch,
 int
 cpuDecode(virCPUDefPtr cpu,
           const virCPUData *data,
-          const char **models,
-          unsigned int nmodels,
+          virDomainCapsCPUModelsPtr models,
           const char *preferred)
 {
     struct cpuArchDriver *driver;
 
-    VIR_DEBUG("cpu=%p, data=%p, nmodels=%u, preferred=%s",
-              cpu, data, nmodels, NULLSTR(preferred));
+    VIR_DEBUG("cpu=%p, data=%p, models=%p, preferred=%s",
+              cpu, data, models, NULLSTR(preferred));
     if (models) {
         size_t i;
-        for (i = 0; i < nmodels; i++)
-            VIR_DEBUG("models[%zu]=%s", i, NULLSTR(models[i]));
-    }
-
-    if (models == NULL && nmodels != 0) {
-        virReportError(VIR_ERR_INVALID_ARG, "%s",
-                       _("nonzero nmodels doesn't match with NULL models"));
-        return -1;
+        for (i = 0; i < models->nmodels; i++)
+            VIR_DEBUG("models[%zu]=%s", i, models->models[i].name);
     }
 
     if (cpu->type > VIR_CPU_TYPE_GUEST ||
@@ -251,7 +243,7 @@ cpuDecode(virCPUDefPtr cpu,
         return -1;
     }
 
-    return driver->decode(cpu, data, models, nmodels, preferred);
+    return driver->decode(cpu, data, models, preferred);
 }
 
 
@@ -384,7 +376,6 @@ virCPUGetHostIsSupported(virArch arch)
  * @type: requested type of the CPU
  * @nodeInfo: simplified CPU topology (optional)
  * @models: list of CPU models that can be considered for host CPU
- * @nmodels: number of CPU models in @models
  *
  * Create CPU definition describing the host's CPU.
  *
@@ -412,15 +403,14 @@ virCPUDefPtr
 virCPUGetHost(virArch arch,
               virCPUType type,
               virNodeInfoPtr nodeInfo,
-              const char **models,
-              unsigned int nmodels)
+              virDomainCapsCPUModelsPtr models)
 {
     struct cpuArchDriver *driver;
     virCPUDefPtr cpu = NULL;
 
-    VIR_DEBUG("arch=%s, type=%s, nodeInfo=%p, models=%p, nmodels=%u",
+    VIR_DEBUG("arch=%s, type=%s, nodeInfo=%p, models=%p",
               virArchToString(arch), virCPUTypeToString(type), nodeInfo,
-              models, nmodels);
+              models);
 
     if (!(driver = cpuGetSubDriver(arch)))
         return NULL;
@@ -462,7 +452,7 @@ virCPUGetHost(virArch arch,
      * filled in.
      */
     if (driver->getHost) {
-        if (driver->getHost(cpu, models, nmodels) < 0 &&
+        if (driver->getHost(cpu, models) < 0 &&
             !nodeInfo)
             goto error;
     } else if (nodeInfo) {
@@ -491,7 +481,7 @@ virCPUProbeHost(virArch arch)
     if (virCapabilitiesGetNodeInfo(&nodeinfo))
         return NULL;
 
-    return virCPUGetHost(arch, VIR_CPU_TYPE_HOST, &nodeinfo, NULL, 0);
+    return virCPUGetHost(arch, VIR_CPU_TYPE_HOST, &nodeinfo, NULL);
 }
 
 
@@ -501,11 +491,10 @@ virCPUProbeHost(virArch arch)
  * @cpus: list of host CPU definitions
  * @ncpus: number of CPUs in @cpus
  * @models: list of CPU models that can be considered for the baseline CPU
- * @nmodels: number of CPU models in @models
  * @migratable: requests non-migratable features to be removed from the result
  *
  * Computes the most feature-rich CPU which is compatible with all given
- * host CPUs. If @models array is NULL, all models supported by libvirt will
+ * host CPUs. If @models is NULL, all models supported by libvirt will
  * be considered when computing the baseline CPU model, otherwise the baseline
  * CPU model will be one of the provided CPU @models.
  *
@@ -514,21 +503,20 @@ virCPUProbeHost(virArch arch)
 virCPUDefPtr
 cpuBaseline(virCPUDefPtr *cpus,
             unsigned int ncpus,
-            const char **models,
-            unsigned int nmodels,
+            virDomainCapsCPUModelsPtr models,
             bool migratable)
 {
     struct cpuArchDriver *driver;
     size_t i;
 
-    VIR_DEBUG("ncpus=%u, nmodels=%u", ncpus, nmodels);
+    VIR_DEBUG("ncpus=%u, models=%p, migratable=%d", ncpus, models, migratable);
     if (cpus) {
         for (i = 0; i < ncpus; i++)
             VIR_DEBUG("cpus[%zu]=%p", i, cpus[i]);
     }
     if (models) {
-        for (i = 0; i < nmodels; i++)
-            VIR_DEBUG("models[%zu]=%s", i, NULLSTR(models[i]));
+        for (i = 0; i < models->nmodels; i++)
+            VIR_DEBUG("models[%zu]=%s", i, models->models[i].name);
     }
 
     if (cpus == NULL && ncpus != 0) {
@@ -555,12 +543,6 @@ cpuBaseline(virCPUDefPtr *cpus,
         }
     }
 
-    if (models == NULL && nmodels != 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       "%s", _("nonzero nmodels doesn't match with NULL models"));
-        return NULL;
-    }
-
     if ((driver = cpuGetSubDriver(cpus[0]->arch)) == NULL)
         return NULL;
 
@@ -571,7 +553,7 @@ cpuBaseline(virCPUDefPtr *cpus,
         return NULL;
     }
 
-    return driver->baseline(cpus, ncpus, models, nmodels, migratable);
+    return driver->baseline(cpus, ncpus, models, migratable);
 }
 
 
@@ -844,25 +826,23 @@ virCPUDataParse(const char *xmlStr)
  *
  * @model: CPU model to be checked
  * @models: list of supported CPU models
- * @nmodels: number of models in @models
  *
  * Checks whether @model can be found in the list of supported @models.
- * If @models is empty, all models are supported.
+ * If @models is NULL, all models are supported.
  *
  * Returns true if @model is supported, false otherwise.
  */
 bool
 virCPUModelIsAllowed(const char *model,
-                     const char **models,
-                     unsigned int nmodels)
+                     virDomainCapsCPUModelsPtr models)
 {
     size_t i;
 
-    if (!models || !nmodels)
+    if (!models)
         return true;
 
-    for (i = 0; i < nmodels; i++) {
-        if (models[i] && STREQ(models[i], model))
+    for (i = 0; i < models->nmodels; i++) {
+        if (STREQ(models->models[i].name, model))
             return true;
     }
     return false;
@@ -908,8 +888,7 @@ virCPUGetModels(virArch arch, char ***models)
  *
  * @arch: CPU architecture
  * @cpu: CPU definition to be translated
- * @models: NULL-terminated list of allowed CPU models (NULL if all are allowed)
- * @nmodels: number of CPU models in @models
+ * @models: list of allowed CPU models (NULL if all are allowed)
  *
  * Translates @cpu model (if allowed by @cpu->fallback) to a closest CPU model
  * from @models list.
@@ -922,13 +901,12 @@ virCPUGetModels(virArch arch, char ***models)
 int
 virCPUTranslate(virArch arch,
                 virCPUDefPtr cpu,
-                const char **models,
-                unsigned int nmodels)
+                virDomainCapsCPUModelsPtr models)
 {
     struct cpuArchDriver *driver;
 
-    VIR_DEBUG("arch=%s, cpu=%p, model=%s, models=%p, nmodels=%u",
-              virArchToString(arch), cpu, NULLSTR(cpu->model), models, nmodels);
+    VIR_DEBUG("arch=%s, cpu=%p, model=%s, models=%p",
+              virArchToString(arch), cpu, NULLSTR(cpu->model), models);
 
     if (!(driver = cpuGetSubDriver(arch)))
         return -1;
@@ -937,7 +915,7 @@ virCPUTranslate(virArch arch,
         cpu->mode == VIR_CPU_MODE_HOST_PASSTHROUGH)
         return 0;
 
-    if (virCPUModelIsAllowed(cpu->model, models, nmodels))
+    if (virCPUModelIsAllowed(cpu->model, models))
         return 0;
 
     if (cpu->fallback != VIR_CPU_FALLBACK_ALLOW) {
@@ -954,7 +932,7 @@ virCPUTranslate(virArch arch,
         return -1;
     }
 
-    if (driver->translate(cpu, models, nmodels) < 0)
+    if (driver->translate(cpu, models) < 0)
         return -1;
 
     VIR_DEBUG("model=%s", NULLSTR(cpu->model));
diff --git a/src/cpu/cpu.h b/src/cpu/cpu.h
index 5daff186c4..d325fe3d04 100644
--- a/src/cpu/cpu.h
+++ b/src/cpu/cpu.h
@@ -27,6 +27,7 @@
 # include "virerror.h"
 # include "datatypes.h"
 # include "virarch.h"
+# include "domain_capabilities.h"
 # include "cpu_conf.h"
 # include "cpu_x86_data.h"
 # include "cpu_ppc64_data.h"
@@ -52,8 +53,7 @@ typedef virCPUCompareResult
 typedef int
 (*cpuArchDecode)    (virCPUDefPtr cpu,
                      const virCPUData *data,
-                     const char **models,
-                     unsigned int nmodels,
+                     virDomainCapsCPUModelsPtr models,
                      const char *preferred);
 
 typedef int
@@ -71,14 +71,12 @@ typedef void
 
 typedef int
 (*virCPUArchGetHost)(virCPUDefPtr cpu,
-                     const char **models,
-                     unsigned int nmodels);
+                     virDomainCapsCPUModelsPtr models);
 
 typedef virCPUDefPtr
 (*cpuArchBaseline)  (virCPUDefPtr *cpus,
                      unsigned int ncpus,
-                     const char **models,
-                     unsigned int nmodels,
+                     virDomainCapsCPUModelsPtr models,
                      bool migratable);
 
 typedef int
@@ -109,8 +107,7 @@ typedef int
 
 typedef int
 (*virCPUArchTranslate)(virCPUDefPtr cpu,
-                       const char **models,
-                       unsigned int nmodels);
+                       virDomainCapsCPUModelsPtr models);
 
 typedef int
 (*virCPUArchConvertLegacy)(virCPUDefPtr cpu);
@@ -165,8 +162,7 @@ virCPUCompare(virArch arch,
 int
 cpuDecode   (virCPUDefPtr cpu,
              const virCPUData *data,
-             const char **models,
-             unsigned int nmodels,
+             virDomainCapsCPUModelsPtr models,
              const char *preferred)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
 
@@ -194,8 +190,7 @@ virCPUDefPtr
 virCPUGetHost(virArch arch,
               virCPUType type,
               virNodeInfoPtr nodeInfo,
-              const char **models,
-              unsigned int nmodels);
+              virDomainCapsCPUModelsPtr models);
 
 virCPUDefPtr
 virCPUProbeHost(virArch arch);
@@ -203,8 +198,7 @@ virCPUProbeHost(virArch arch);
 virCPUDefPtr
 cpuBaseline (virCPUDefPtr *cpus,
              unsigned int ncpus,
-             const char **models,
-             unsigned int nmodels,
+             virDomainCapsCPUModelsPtr models,
              bool migratable);
 
 int
@@ -235,8 +229,7 @@ virCPUDataCheckFeature(const virCPUData *data,
 
 bool
 virCPUModelIsAllowed(const char *model,
-                     const char **models,
-                     unsigned int nmodels)
+                     virDomainCapsCPUModelsPtr models)
     ATTRIBUTE_NONNULL(1);
 
 int
@@ -245,8 +238,7 @@ virCPUGetModels(virArch arch, char ***models);
 int
 virCPUTranslate(virArch arch,
                 virCPUDefPtr cpu,
-                const char **models,
-                unsigned int nmodels)
+                virDomainCapsCPUModelsPtr models)
     ATTRIBUTE_NONNULL(2);
 
 int
diff --git a/src/cpu/cpu_arm.c b/src/cpu/cpu_arm.c
index 474777656c..44cb4fea68 100644
--- a/src/cpu/cpu_arm.c
+++ b/src/cpu/cpu_arm.c
@@ -75,8 +75,7 @@ virCPUarmUpdate(virCPUDefPtr guest,
 static virCPUDefPtr
 armBaseline(virCPUDefPtr *cpus,
             unsigned int ncpus ATTRIBUTE_UNUSED,
-            const char **models ATTRIBUTE_UNUSED,
-            unsigned int nmodels ATTRIBUTE_UNUSED,
+            virDomainCapsCPUModelsPtr models ATTRIBUTE_UNUSED,
             bool migratable ATTRIBUTE_UNUSED)
 {
     virCPUDefPtr cpu = NULL;
diff --git a/src/cpu/cpu_ppc64.c b/src/cpu/cpu_ppc64.c
index 5c5fc0fec6..902c465cf4 100644
--- a/src/cpu/cpu_ppc64.c
+++ b/src/cpu/cpu_ppc64.c
@@ -669,8 +669,7 @@ virCPUppc64Compare(virCPUDefPtr host,
 static int
 ppc64DriverDecode(virCPUDefPtr cpu,
                   const virCPUData *data,
-                  const char **models,
-                  unsigned int nmodels,
+                  virDomainCapsCPUModelsPtr models,
                   const char *preferred ATTRIBUTE_UNUSED)
 {
     int ret = -1;
@@ -687,7 +686,7 @@ ppc64DriverDecode(virCPUDefPtr cpu,
         goto cleanup;
     }
 
-    if (!virCPUModelIsAllowed(model->name, models, nmodels)) {
+    if (!virCPUModelIsAllowed(model->name, models)) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("CPU model %s is not supported by hypervisor"),
                        model->name);
@@ -720,8 +719,7 @@ virCPUppc64DataFree(virCPUDataPtr data)
 
 static int
 virCPUppc64GetHost(virCPUDefPtr cpu,
-                   const char **models,
-                   unsigned int nmodels)
+                   virDomainCapsCPUModelsPtr models)
 {
     virCPUDataPtr cpuData = NULL;
     virCPUppc64Data *data;
@@ -743,7 +741,7 @@ virCPUppc64GetHost(virCPUDefPtr cpu,
 #endif
     data->pvr[0].mask = 0xfffffffful;
 
-    ret = ppc64DriverDecode(cpu, cpuData, models, nmodels, NULL);
+    ret = ppc64DriverDecode(cpu, cpuData, models, NULL);
 
  cleanup:
     virCPUppc64DataFree(cpuData);
@@ -772,8 +770,7 @@ virCPUppc64Update(virCPUDefPtr guest,
 static virCPUDefPtr
 ppc64DriverBaseline(virCPUDefPtr *cpus,
                     unsigned int ncpus,
-                    const char **models ATTRIBUTE_UNUSED,
-                    unsigned int nmodels ATTRIBUTE_UNUSED,
+                    virDomainCapsCPUModelsPtr models ATTRIBUTE_UNUSED,
                     bool migratable ATTRIBUTE_UNUSED)
 {
     struct ppc64_map *map;
diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c
index 5ce205f9c1..3f9e83ca72 100644
--- a/src/cpu/cpu_x86.c
+++ b/src/cpu/cpu_x86.c
@@ -1820,8 +1820,7 @@ x86DataFilterTSX(virCPUx86Data *data,
 static int
 x86Decode(virCPUDefPtr cpu,
           const virCPUx86Data *cpuData,
-          const char **models,
-          unsigned int nmodels,
+          virDomainCapsCPUModelsPtr models,
           const char *preferred,
           bool migratable)
 {
@@ -1855,7 +1854,7 @@ x86Decode(virCPUDefPtr cpu,
      */
     for (i = map->nmodels - 1; i >= 0; i--) {
         candidate = map->models[i];
-        if (!virCPUModelIsAllowed(candidate->name, models, nmodels)) {
+        if (!virCPUModelIsAllowed(candidate->name, models)) {
             if (preferred && STREQ(candidate->name, preferred)) {
                 if (cpu->fallback != VIR_CPU_FALLBACK_ALLOW) {
                     virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@@ -1946,11 +1945,10 @@ x86Decode(virCPUDefPtr cpu,
 static int
 x86DecodeCPUData(virCPUDefPtr cpu,
                  const virCPUData *data,
-                 const char **models,
-                 unsigned int nmodels,
+                 virDomainCapsCPUModelsPtr models,
                  const char *preferred)
 {
-    return x86Decode(cpu, &data->data.x86, models, nmodels, preferred, false);
+    return x86Decode(cpu, &data->data.x86, models, preferred, false);
 }
 
 
@@ -2402,8 +2400,7 @@ cpuidSet(uint32_t base, virCPUDataPtr data)
 
 static int
 virCPUx86GetHost(virCPUDefPtr cpu,
-                 const char **models,
-                 unsigned int nmodels)
+                 virDomainCapsCPUModelsPtr models)
 {
     virCPUDataPtr cpuData = NULL;
     int ret = -1;
@@ -2415,7 +2412,7 @@ virCPUx86GetHost(virCPUDefPtr cpu,
         cpuidSet(CPUX86_EXTENDED, cpuData) < 0)
         goto cleanup;
 
-    ret = x86DecodeCPUData(cpu, cpuData, models, nmodels, NULL);
+    ret = x86DecodeCPUData(cpu, cpuData, models, NULL);
 
  cleanup:
     virCPUx86DataFree(cpuData);
@@ -2427,8 +2424,7 @@ virCPUx86GetHost(virCPUDefPtr cpu,
 static virCPUDefPtr
 x86Baseline(virCPUDefPtr *cpus,
             unsigned int ncpus,
-            const char **models,
-            unsigned int nmodels,
+            virDomainCapsCPUModelsPtr models,
             bool migratable)
 {
     virCPUx86MapPtr map = NULL;
@@ -2523,7 +2519,7 @@ x86Baseline(virCPUDefPtr *cpus,
         virCPUx86DataAddCPUIDInt(&base_model->data, &vendor->cpuid) < 0)
         goto error;
 
-    if (x86Decode(cpu, &base_model->data, models, nmodels, modelName, migratable) < 0)
+    if (x86Decode(cpu, &base_model->data, models, modelName, migratable) < 0)
         goto error;
 
     if (STREQ_NULLABLE(cpu->model, modelName))
@@ -2805,8 +2801,7 @@ virCPUx86GetModels(char ***models)
 
 static int
 virCPUx86Translate(virCPUDefPtr cpu,
-                   const char **models,
-                   unsigned int nmodels)
+                   virDomainCapsCPUModelsPtr models)
 {
     virCPUDefPtr translated = NULL;
     virCPUx86MapPtr map;
@@ -2830,7 +2825,7 @@ virCPUx86Translate(virCPUDefPtr cpu,
     if (!(translated = virCPUDefCopyWithoutModel(cpu)))
         goto cleanup;
 
-    if (x86Decode(translated, &model->data, models, nmodels, NULL, false) < 0)
+    if (x86Decode(translated, &model->data, models, NULL, false) < 0)
         goto cleanup;
 
     for (i = 0; i < cpu->nfeatures; i++) {
diff --git a/src/libxl/libxl_capabilities.c b/src/libxl/libxl_capabilities.c
index e0959202b3..4def561143 100644
--- a/src/libxl/libxl_capabilities.c
+++ b/src/libxl/libxl_capabilities.c
@@ -192,7 +192,7 @@ libxlCapsInitCPU(virCapsPtr caps, libxl_physinfo *phy_info,
     ret = 0;
 
     if (!(data = libxlCapsNodeData(cpu, phy_info->hw_cap, version)) ||
-        cpuDecode(cpu, data, NULL, 0, NULL) < 0) {
+        cpuDecode(cpu, data, NULL, NULL) < 0) {
         VIR_WARN("Failed to initialize host cpu features");
         goto error;
     }
diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
index 8483d6ecf7..ad171f09c3 100644
--- a/src/libxl/libxl_driver.c
+++ b/src/libxl/libxl_driver.c
@@ -6460,7 +6460,7 @@ libxlConnectBaselineCPU(virConnectPtr conn,
     if (!(cpus = virCPUDefListParse(xmlCPUs, ncpus, VIR_CPU_TYPE_HOST)))
         goto cleanup;
 
-    if (!(cpu = cpuBaseline(cpus, ncpus, NULL, 0,
+    if (!(cpu = cpuBaseline(cpus, ncpus, NULL,
                             !!(flags & VIR_CONNECT_BASELINE_CPU_MIGRATABLE))))
         goto cleanup;
 
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 5b532b247c..4bdc3f26c9 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -1168,18 +1168,8 @@ virQEMUCapsProbeHostCPUForEmulator(virArch hostArch,
                                    virQEMUCapsPtr qemuCaps,
                                    virDomainVirtType type)
 {
-    size_t nmodels;
-    char **models;
-    virCPUDefPtr cpu;
-
-    if (virQEMUCapsGetCPUDefinitions(qemuCaps, type, &models, &nmodels) < 0)
-        return NULL;
-
-    cpu = virCPUGetHost(hostArch, VIR_CPU_TYPE_GUEST, NULL,
-                        (const char **) models, nmodels);
-
-    virStringListFreeCount(models, nmodels);
-    return cpu;
+    return virCPUGetHost(hostArch, VIR_CPU_TYPE_GUEST, NULL,
+                         virQEMUCapsGetCPUDefinitions(qemuCaps, type));
 }
 
 
@@ -2534,45 +2524,14 @@ virQEMUCapsAddCPUDefinitions(virQEMUCapsPtr qemuCaps,
 }
 
 
-int
+virDomainCapsCPUModelsPtr
 virQEMUCapsGetCPUDefinitions(virQEMUCapsPtr qemuCaps,
-                             virDomainVirtType type,
-                             char ***names,
-                             size_t *count)
+                             virDomainVirtType type)
 {
-    size_t i;
-    char **models = NULL;
-    virDomainCapsCPUModelsPtr cpus;
-
-    *count = 0;
-    if (names)
-        *names = NULL;
-
     if (type == VIR_DOMAIN_VIRT_KVM)
-        cpus = qemuCaps->kvmCPUModels;
+        return qemuCaps->kvmCPUModels;
     else
-        cpus = qemuCaps->tcgCPUModels;
-
-    if (!cpus)
-        return 0;
-
-    if (names && VIR_ALLOC_N(models, cpus->nmodels) < 0)
-        return -1;
-
-    for (i = 0; i < cpus->nmodels; i++) {
-        virDomainCapsCPUModelPtr cpu = cpus->models + i;
-        if (models && VIR_STRDUP(models[i], cpu->name) < 0)
-            goto error;
-    }
-
-    if (names)
-        *names = models;
-    *count = cpus->nmodels;
-    return 0;
-
- error:
-    virStringListFreeCount(models, i);
-    return -1;
+        return qemuCaps->tcgCPUModels;
 }
 
 
@@ -3394,8 +3353,6 @@ virQEMUCapsInitCPUModelX86(virQEMUCapsPtr qemuCaps,
     virCPUDataPtr data = NULL;
     unsigned long long sigFamily = 0;
     unsigned long long sigModel = 0;
-    size_t nmodels = 0;
-    char **models = NULL;
     int ret = -1;
     size_t i;
 
@@ -3440,15 +3397,15 @@ virQEMUCapsInitCPUModelX86(virQEMUCapsPtr qemuCaps,
     if (virCPUx86DataSetSignature(data, sigFamily, sigModel) < 0)
         goto cleanup;
 
-    if (virQEMUCapsGetCPUDefinitions(qemuCaps, type, &models, &nmodels) < 0 ||
-        cpuDecode(cpu, data, (const char **) models, nmodels, NULL) < 0)
+    if (cpuDecode(cpu, data,
+                  virQEMUCapsGetCPUDefinitions(qemuCaps, type),
+                  NULL) < 0)
         goto cleanup;
 
     ret = 0;
 
  cleanup:
     virCPUDataFree(data);
-    virStringListFreeCount(models, nmodels);
     return ret;
 }
 
@@ -3534,7 +3491,7 @@ virQEMUCapsInitHostCPUModel(virQEMUCapsPtr qemuCaps,
     } else if (type == VIR_DOMAIN_VIRT_KVM &&
                virCPUGetHostIsSupported(qemuCaps->arch)) {
         if (!(fullCPU = virCPUGetHost(qemuCaps->arch, VIR_CPU_TYPE_GUEST,
-                                      NULL, NULL, 0)))
+                                      NULL, NULL)))
             goto error;
 
         for (i = 0; i < cpu->nfeatures; i++) {
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
index 2d16e5b0ef..cacc2b77ed 100644
--- a/src/qemu/qemu_capabilities.h
+++ b/src/qemu/qemu_capabilities.h
@@ -470,10 +470,8 @@ int virQEMUCapsAddCPUDefinitions(virQEMUCapsPtr qemuCaps,
                                  const char **name,
                                  size_t count,
                                  virDomainCapsCPUUsable usable);
-int virQEMUCapsGetCPUDefinitions(virQEMUCapsPtr qemuCaps,
-                                 virDomainVirtType type,
-                                 char ***names,
-                                 size_t *count);
+virDomainCapsCPUModelsPtr virQEMUCapsGetCPUDefinitions(virQEMUCapsPtr qemuCaps,
+                                                       virDomainVirtType type);
 
 typedef enum {
     /* Host CPU definition reported in domain capabilities. */
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 7c6f1674a9..842e088519 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -13055,7 +13055,7 @@ qemuConnectBaselineCPU(virConnectPtr conn ATTRIBUTE_UNUSED,
     if (!(cpus = virCPUDefListParse(xmlCPUs, ncpus, VIR_CPU_TYPE_HOST)))
         goto cleanup;
 
-    if (!(baseline = cpuBaseline(cpus, ncpus, NULL, 0,
+    if (!(baseline = cpuBaseline(cpus, ncpus, NULL,
                                  !!(flags & VIR_CONNECT_BASELINE_CPU_MIGRATABLE))))
         goto cleanup;
 
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 9f26dfccf8..0cb023095b 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -5166,8 +5166,6 @@ qemuProcessUpdateGuestCPU(virDomainDefPtr def,
                           unsigned int flags)
 {
     int ret = -1;
-    size_t nmodels = 0;
-    char **models = NULL;
 
     if (!def->cpu)
         return 0;
@@ -5219,17 +5217,14 @@ qemuProcessUpdateGuestCPU(virDomainDefPtr def,
                                              VIR_QEMU_CAPS_HOST_CPU_MIGRATABLE)) < 0)
         goto cleanup;
 
-    if (virQEMUCapsGetCPUDefinitions(qemuCaps, def->virtType,
-                                     &models, &nmodels) < 0 ||
-        virCPUTranslate(def->os.arch, def->cpu,
-                        (const char **) models, nmodels) < 0)
+    if (virCPUTranslate(def->os.arch, def->cpu,
+                        virQEMUCapsGetCPUDefinitions(qemuCaps, def->virtType)) < 0)
         goto cleanup;
 
     def->cpu->fallback = VIR_CPU_FALLBACK_FORBID;
     ret = 0;
 
  cleanup:
-    virStringListFreeCount(models, nmodels);
     return ret;
 }
 
diff --git a/src/test/test_driver.c b/src/test/test_driver.c
index 1c48347994..5508a79230 100644
--- a/src/test/test_driver.c
+++ b/src/test/test_driver.c
@@ -1536,7 +1536,7 @@ testConnectBaselineCPU(virConnectPtr conn ATTRIBUTE_UNUSED,
     if (!(cpus = virCPUDefListParse(xmlCPUs, ncpus, VIR_CPU_TYPE_HOST)))
         goto cleanup;
 
-    if (!(cpu = cpuBaseline(cpus, ncpus, NULL, 0, false)))
+    if (!(cpu = cpuBaseline(cpus, ncpus, NULL, false)))
         goto cleanup;
 
     if ((flags & VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES) &&
diff --git a/tests/cputest.c b/tests/cputest.c
index 913ca77231..552c07e2c5 100644
--- a/tests/cputest.c
+++ b/tests/cputest.c
@@ -61,9 +61,8 @@ struct data {
     virArch arch;
     const char *host;
     const char *name;
-    const char **models;
+    virDomainCapsCPUModelsPtr models;
     const char *modelsName;
-    unsigned int nmodels;
     unsigned int flags;
     int result;
 };
@@ -264,13 +263,13 @@ cpuTestGuestCPU(const void *arg)
     }
 
     if (virCPUUpdate(host->arch, cpu, host) < 0 ||
-        virCPUTranslate(host->arch, cpu, data->models, data->nmodels) < 0) {
+        virCPUTranslate(host->arch, cpu, data->models) < 0) {
         ret = -1;
         goto cleanup;
     }
 
     virBufferAsprintf(&buf, "%s+%s", data->host, data->name);
-    if (data->nmodels)
+    if (data->modelsName)
         virBufferAsprintf(&buf, ",%s", data->modelsName);
     virBufferAddLit(&buf, "-result");
 
@@ -322,7 +321,7 @@ cpuTestBaseline(const void *arg)
     if (!(cpus = cpuTestLoadMultiXML(data->arch, data->name, &ncpus)))
         goto cleanup;
 
-    baseline = cpuBaseline(cpus, ncpus, NULL, 0,
+    baseline = cpuBaseline(cpus, ncpus, NULL,
                            !!(data->flags & VIR_CONNECT_BASELINE_CPU_MIGRATABLE));
 
     if (baseline &&
@@ -492,7 +491,7 @@ cpuTestCPUID(bool guest, const void *arg)
         cpu->type = VIR_CPU_TYPE_HOST;
     }
 
-    if (cpuDecode(cpu, hostData, NULL, 0, NULL) < 0)
+    if (cpuDecode(cpu, hostData, NULL, NULL) < 0)
         goto cleanup;
 
     if (virAsprintf(&result, "cpuid-%s-%s",
@@ -729,15 +728,43 @@ cpuTestJSONCPUID(const void *arg)
 #endif
 
 
-static const char *model486[]   = { "486" };
-static const char *nomodel[]    = { "nomodel" };
-static const char *models[]     = { "qemu64", "core2duo", "Nehalem" };
-static const char *haswell[]    = { "SandyBridge", "Haswell" };
-static const char *ppc_models[] = { "POWER6", "POWER7", "POWER8" };
+static const char *model486_list[]   = { "486", NULL };
+static const char *nomodel_list[]    = { "nomodel", NULL };
+static const char *models_list[]     = { "qemu64", "core2duo", "Nehalem", NULL };
+static const char *haswell_list[]    = { "SandyBridge", "Haswell", NULL };
+static const char *ppc_models_list[] = { "POWER6", "POWER7", "POWER8", NULL };
+
+static virDomainCapsCPUModelsPtr
+cpuTestInitModels(const char **list)
+{
+    virDomainCapsCPUModelsPtr cpus;
+    const char **model;
+
+    if (!(cpus = virDomainCapsCPUModelsNew(0)))
+        return NULL;
+
+    for (model = list; *model; model++) {
+        if (virDomainCapsCPUModelsAdd(cpus, *model, -1,
+                                      VIR_DOMCAPS_CPU_USABLE_UNKNOWN, NULL) < 0)
+            goto error;
+    }
+
+    return cpus;
+
+ error:
+    virObjectUnref(cpus);
+    return NULL;
+}
+
 
 static int
 mymain(void)
 {
+    virDomainCapsCPUModelsPtr model486 = NULL;
+    virDomainCapsCPUModelsPtr nomodel = NULL;
+    virDomainCapsCPUModelsPtr models = NULL;
+    virDomainCapsCPUModelsPtr haswell = NULL;
+    virDomainCapsCPUModelsPtr ppc_models = NULL;
     int ret = 0;
 
 #if WITH_QEMU && WITH_YAJL
@@ -747,13 +774,22 @@ mymain(void)
     virEventRegisterDefaultImpl();
 #endif
 
+    if (!(model486 = cpuTestInitModels(model486_list)) ||
+        !(nomodel = cpuTestInitModels(nomodel_list)) ||
+        !(models = cpuTestInitModels(models_list)) ||
+        !(haswell = cpuTestInitModels(haswell_list)) ||
+        !(ppc_models = cpuTestInitModels(ppc_models_list))) {
+        ret = -1;
+        goto cleanup;
+    }
+
 #define DO_TEST(arch, api, name, host, cpu,                             \
-                models, nmodels, flags, result)                         \
+                models, flags, result)                                  \
     do {                                                                \
         struct data data = {                                            \
             arch, host, cpu, models,                                    \
             models == NULL ? NULL : #models,                            \
-            nmodels, flags, result                                      \
+            flags, result                                               \
         };                                                              \
         char *testLabel;                                                \
         char *tmp;                                                      \
@@ -784,12 +820,12 @@ mymain(void)
 #define DO_TEST_COMPARE(arch, host, cpu, result)                        \
     DO_TEST(arch, cpuTestCompare,                                       \
             host "/" cpu " (" #result ")",                              \
-            host, cpu, NULL, 0, 0, result)
+            host, cpu, NULL, 0, result)
 
 #define DO_TEST_UPDATE_ONLY(arch, host, cpu)                            \
     DO_TEST(arch, cpuTestUpdate,                                        \
             cpu " on " host,                                            \
-            host, cpu, NULL, 0, 0, 0)
+            host, cpu, NULL, 0, 0)
 
 #define DO_TEST_UPDATE(arch, host, cpu, result)                         \
     do {                                                                \
@@ -809,7 +845,7 @@ mymain(void)
             ret = -1;                                                   \
         } else {                                                        \
             DO_TEST(arch, cpuTestBaseline, label, NULL,                 \
-                    "baseline-" name, NULL, 0, flags, result);          \
+                    "baseline-" name, NULL, flags, result);             \
         }                                                               \
         VIR_FREE(label);                                                \
     } while (0)
@@ -817,21 +853,19 @@ mymain(void)
 #define DO_TEST_HASFEATURE(arch, host, feature, result)                 \
     DO_TEST(arch, cpuTestHasFeature,                                    \
             host "/" feature " (" #result ")",                          \
-            host, feature, NULL, 0, 0, result)
+            host, feature, NULL, 0, result)
 
 #define DO_TEST_GUESTCPU(arch, host, cpu, models, result)               \
     DO_TEST(arch, cpuTestGuestCPU,                                      \
             host "/" cpu " (" #models ")",                              \
-            host, cpu, models,                                          \
-            models == NULL ? 0 : sizeof(models) / sizeof(char *),       \
-            0, result)
+            host, cpu, models, 0, result)
 
 #if WITH_QEMU && WITH_YAJL
 # define DO_TEST_CPUID_JSON(arch, host, json)                           \
     do {                                                                \
         if (json) {                                                     \
             DO_TEST(arch, cpuTestJSONCPUID, host, host,                 \
-                    NULL, NULL, 0, 0, 0);                               \
+                    NULL, NULL, 0, 0);                                  \
         }                                                               \
     } while (0)
 #else
@@ -841,13 +875,13 @@ mymain(void)
 #define DO_TEST_CPUID(arch, host, json)                                 \
     do {                                                                \
         DO_TEST(arch, cpuTestHostCPUID, host, host,                     \
-                NULL, NULL, 0, 0, 0);                                   \
+                NULL, NULL, 0, 0);                                      \
         DO_TEST(arch, cpuTestGuestCPUID, host, host,                    \
-                NULL, NULL, 0, 0, 0);                                   \
+                NULL, NULL, 0, 0);                                      \
         DO_TEST_CPUID_JSON(arch, host, json);                           \
         if (json) {                                                     \
             DO_TEST(arch, cpuTestUpdateLive, host, host,                \
-                    NULL, NULL, 0, 0, 0);                               \
+                    NULL, NULL, 0, 0);                                  \
         }                                                               \
     } while (0)
 
@@ -1012,10 +1046,17 @@ mymain(void)
     DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-W3520", true);
     DO_TEST_CPUID(VIR_ARCH_X86_64, "Xeon-X5460", false);
 
+ cleanup:
 #if WITH_QEMU && WITH_YAJL
     qemuTestDriverFree(&driver);
 #endif
 
+    virObjectUnref(model486);
+    virObjectUnref(nomodel);
+    virObjectUnref(models);
+    virObjectUnref(haswell);
+    virObjectUnref(ppc_models);
+
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
-- 
2.14.2




More information about the libvir-list mailing list