From 16c6b60cbd4da96fe3ccaa33e152175dada3aa6c Mon Sep 17 00:00:00 2001 From: Jiri Denemark Date: Wed, 19 Dec 2012 12:08:32 +0100 Subject: [PATCH] cpu: Introduce cpuModelIsAllowed internal API The API can be used to check if the model is on the supported models list, which needs to be done in several places. --- src/cpu/cpu.c | 17 +++++++++++++++++ src/cpu/cpu.h | 5 +++++ src/cpu/cpu_generic.c | 19 +++++-------------- src/cpu/cpu_x86.c | 11 +---------- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c index 6beab5508c..68125a540e 100644 --- a/src/cpu/cpu.c +++ b/src/cpu/cpu.c @@ -442,3 +442,20 @@ cpuHasFeature(virArch arch, return driver->hasFeature(data, feature); } + +bool +cpuModelIsAllowed(const char *model, + const char **models, + unsigned int nmodels) +{ + unsigned int i; + + if (!models || !nmodels) + return true; + + for (i = 0; i < nmodels; i++) { + if (models[i] && STREQ(models[i], model)) + return true; + } + return false; +} diff --git a/src/cpu/cpu.h b/src/cpu/cpu.h index e279072da6..cba7149e91 100644 --- a/src/cpu/cpu.h +++ b/src/cpu/cpu.h @@ -163,4 +163,9 @@ cpuHasFeature(virArch arch, const char *feature); +bool +cpuModelIsAllowed(const char *model, + const char **models, + unsigned int nmodels); + #endif /* __VIR_CPU_H__ */ diff --git a/src/cpu/cpu_generic.c b/src/cpu/cpu_generic.c index 5dfa6a21a4..9d5117f2b7 100644 --- a/src/cpu/cpu_generic.c +++ b/src/cpu/cpu_generic.c @@ -123,20 +123,11 @@ genericBaseline(virCPUDefPtr *cpus, unsigned int count; unsigned int i, j; - if (models) { - bool found = false; - for (i = 0; i < nmodels; i++) { - if (STREQ(cpus[0]->model, models[i])) { - found = true; - break; - } - } - if (!found) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("CPU model '%s' is not support by hypervisor"), - cpus[0]->model); - goto error; - } + if (!cpuModelIsAllowed(cpus[0]->model, models, nmodels)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("CPU model %s is not supported by hypervisor"), + cpus[0]->model); + goto error; } if (VIR_ALLOC(cpu) < 0 || diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c index c750afd9a6..4940c23703 100644 --- a/src/cpu/cpu_x86.c +++ b/src/cpu/cpu_x86.c @@ -1317,16 +1317,7 @@ x86Decode(virCPUDefPtr cpu, candidate = map->models; while (candidate != NULL) { - bool allowed = (models == NULL); - - for (i = 0; i < nmodels; i++) { - if (models && models[i] && STREQ(models[i], candidate->name)) { - allowed = true; - break; - } - } - - if (!allowed) { + if (!cpuModelIsAllowed(candidate->name, models, nmodels)) { if (preferred && STREQ(candidate->name, preferred)) { if (cpu->fallback != VIR_CPU_FALLBACK_ALLOW) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED,