qemu: Refactor qemuMonitorJSONGetCPUx86Data

This patch splits qemuMonitorJSONGetCPUx86Data in three functions:

- qemuMonitorJSONCheckCPUx86 checks if QEMU supports reporting CPUID
  features for a guest CPU

- qemuMonitorJSONParseCPUx86Features parses CPUID features from a JSON
  array

- qemuMonitorJSONGetCPUx86Data gets the requested guest CPU property
  from QOM and uses qemuMonitorJSONParseCPUx86Features to parse it

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
This commit is contained in:
Jiri Denemark 2016-06-03 16:59:59 +02:00
parent 90f4bc34b5
commit 2a8d40f4ec
2 changed files with 108 additions and 77 deletions

View File

@ -6404,94 +6404,23 @@ qemuMonitorJSONParseCPUx86FeatureWord(virJSONValuePtr data,
static int static int
qemuMonitorJSONGetCPUx86Data(qemuMonitorPtr mon, qemuMonitorJSONParseCPUx86Features(virJSONValuePtr data,
const char *property, virCPUDataPtr *cpudata)
virCPUDataPtr *cpudata)
{ {
virJSONValuePtr cmd = NULL;
virJSONValuePtr reply = NULL;
virJSONValuePtr data;
virJSONValuePtr element;
virCPUx86Data *x86Data = NULL; virCPUx86Data *x86Data = NULL;
virCPUx86CPUID cpuid; virCPUx86CPUID cpuid;
size_t i; size_t i;
ssize_t n; ssize_t n;
int ret = -1; int ret = -1;
/* look up if the property exists before asking */
if (!(cmd = qemuMonitorJSONMakeCommand("qom-list",
"s:path", QOM_CPU_PATH,
NULL)))
goto cleanup;
if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
goto cleanup;
/* check if device exists */
if ((data = virJSONValueObjectGet(reply, "error"))) {
const char *klass = virJSONValueObjectGetString(data, "class");
if (STREQ_NULLABLE(klass, "DeviceNotFound") ||
STREQ_NULLABLE(klass, "CommandNotFound")) {
ret = -2;
goto cleanup;
}
}
if (qemuMonitorJSONCheckError(cmd, reply))
goto cleanup;
data = virJSONValueObjectGetArray(reply, "return");
if ((n = virJSONValueArraySize(data)) < 0) { if ((n = virJSONValueArraySize(data)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s CPU property did not return an array"),
property);
goto cleanup;
}
for (i = 0; i < n; i++) {
element = virJSONValueArrayGet(data, i);
if (STREQ_NULLABLE(virJSONValueObjectGetString(element, "name"),
property))
break;
}
/* "property" was not found */
if (i == n) {
ret = -2;
goto cleanup;
}
virJSONValueFree(cmd);
virJSONValueFree(reply);
if (!(cmd = qemuMonitorJSONMakeCommand("qom-get",
"s:path", QOM_CPU_PATH,
"s:property", property,
NULL)))
goto cleanup;
if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
goto cleanup;
if (qemuMonitorJSONCheckError(cmd, reply))
goto cleanup;
if (!(data = virJSONValueObjectGetArray(reply, "return"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("qom-get reply was missing return data")); _("invalid array of CPUID features"));
goto cleanup; return -1;
}
if ((n = virJSONValueArraySize(data)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s CPU property did not return an array"),
property);
goto cleanup;
} }
if (VIR_ALLOC(x86Data) < 0) if (VIR_ALLOC(x86Data) < 0)
goto cleanup; return -1;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
if (qemuMonitorJSONParseCPUx86FeatureWord(virJSONValueArrayGet(data, i), if (qemuMonitorJSONParseCPUx86FeatureWord(virJSONValueArrayGet(data, i),
@ -6505,10 +6434,101 @@ qemuMonitorJSONGetCPUx86Data(qemuMonitorPtr mon,
ret = 0; ret = 0;
cleanup:
virCPUx86DataFree(x86Data);
return ret;
}
int
qemuMonitorJSONGetCPUx86Data(qemuMonitorPtr mon,
const char *property,
virCPUDataPtr *cpudata)
{
virJSONValuePtr cmd = NULL;
virJSONValuePtr reply = NULL;
virJSONValuePtr data;
int ret = -1;
if (!(cmd = qemuMonitorJSONMakeCommand("qom-get",
"s:path", QOM_CPU_PATH,
"s:property", property,
NULL)))
goto cleanup;
if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
goto cleanup;
if (qemuMonitorJSONCheckError(cmd, reply))
goto cleanup;
data = virJSONValueObjectGetArray(reply, "return");
ret = qemuMonitorJSONParseCPUx86Features(data, cpudata);
cleanup:
virJSONValueFree(cmd);
virJSONValueFree(reply);
return ret;
}
/*
* Returns -1 on error, 0 if QEMU does not support reporting CPUID features
* of a guest CPU, and 1 if the feature is supported.
*/
static int
qemuMonitorJSONCheckCPUx86(qemuMonitorPtr mon)
{
virJSONValuePtr cmd = NULL;
virJSONValuePtr reply = NULL;
virJSONValuePtr data;
size_t i;
ssize_t n;
int ret = -1;
if (!(cmd = qemuMonitorJSONMakeCommand("qom-list",
"s:path", QOM_CPU_PATH,
NULL)))
goto cleanup;
if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
goto cleanup;
if ((data = virJSONValueObjectGet(reply, "error"))) {
const char *klass = virJSONValueObjectGetString(data, "class");
if (STREQ_NULLABLE(klass, "DeviceNotFound") ||
STREQ_NULLABLE(klass, "CommandNotFound")) {
ret = 0;
goto cleanup;
}
}
if (qemuMonitorJSONCheckError(cmd, reply))
goto cleanup;
data = virJSONValueObjectGetArray(reply, "return");
if ((n = virJSONValueArraySize(data)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("qom-list reply data was not an array"));
goto cleanup;
}
for (i = 0; i < n; i++) {
virJSONValuePtr element = virJSONValueArrayGet(data, i);
if (STREQ_NULLABLE(virJSONValueObjectGetString(element, "name"),
"feature-words"))
break;
}
if (i == n)
ret = 0;
else
ret = 1;
cleanup: cleanup:
virJSONValueFree(cmd); virJSONValueFree(cmd);
virJSONValueFree(reply); virJSONValueFree(reply);
virCPUx86DataFree(x86Data);
return ret; return ret;
} }
@ -6529,9 +6549,16 @@ qemuMonitorJSONGetGuestCPU(qemuMonitorPtr mon,
virArch arch, virArch arch,
virCPUDataPtr *data) virCPUDataPtr *data)
{ {
int rc;
switch (arch) { switch (arch) {
case VIR_ARCH_X86_64: case VIR_ARCH_X86_64:
case VIR_ARCH_I686: case VIR_ARCH_I686:
if ((rc = qemuMonitorJSONCheckCPUx86(mon)) < 0)
return -1;
else if (!rc)
return -2;
return qemuMonitorJSONGetCPUx86Data(mon, "feature-words", data); return qemuMonitorJSONGetCPUx86Data(mon, "feature-words", data);
default: default:

View File

@ -457,6 +457,10 @@ int qemuMonitorJSONDetachCharDev(qemuMonitorPtr mon,
int qemuMonitorJSONGetDeviceAliases(qemuMonitorPtr mon, int qemuMonitorJSONGetDeviceAliases(qemuMonitorPtr mon,
char ***aliases); char ***aliases);
int qemuMonitorJSONGetCPUx86Data(qemuMonitorPtr mon,
const char *property,
virCPUDataPtr *cpudata);
int qemuMonitorJSONGetGuestCPU(qemuMonitorPtr mon, int qemuMonitorJSONGetGuestCPU(qemuMonitorPtr mon,
virArch arch, virArch arch,
virCPUDataPtr *data); virCPUDataPtr *data);