From: "Collin L. Walling" <walling@xxxxxxxxxxxxxxxxxx> query-cpu-model-expansion is used to get a list of features for a given cpu model name or to get the model and features of the host hardware/environment as seen by Qemu/kvm. Signed-off-by: Collin L. Walling <walling@xxxxxxxxxxxxxxxxxx> Signed-off-by: Jason J. Herne <jjherne@xxxxxxxxxxxxxxxxxx> --- src/qemu/qemu_monitor.c | 62 ++++++++++++++++++++++ src/qemu/qemu_monitor.h | 22 ++++++++ src/qemu/qemu_monitor_json.c | 121 +++++++++++++++++++++++++++++++++++++++++++ src/qemu/qemu_monitor_json.h | 12 +++++ 4 files changed, 217 insertions(+) diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c index 648168d..1a9665f 100644 --- a/src/qemu/qemu_monitor.c +++ b/src/qemu/qemu_monitor.c @@ -3660,6 +3660,68 @@ qemuMonitorCPUDefInfoFree(qemuMonitorCPUDefInfoPtr cpu) int +qemuMonitorGetCPUModelExpansion(qemuMonitorPtr mon, + const char *type, + const char *model_name, + qemuMonitorCPUModelInfoPtr *model_info) +{ + VIR_DEBUG("type=%s model_name=%s", type, model_name); + + QEMU_CHECK_MONITOR_JSON(mon); + + return qemuMonitorJSONGetCPUModelExpansion(mon, type, model_name, model_info); +} + + +void +qemuMonitorCPUModelInfoFree(qemuMonitorCPUModelInfoPtr model_info) +{ + size_t i; + + if (!model_info) + return; + + for (i = 0; i < model_info->nprops; i++) + VIR_FREE(model_info->props[i].name); + + VIR_FREE(model_info->name); + VIR_FREE(model_info); +} + + +qemuMonitorCPUModelInfoPtr +qemuMonitorCPUModelInfoCopy(const qemuMonitorCPUModelInfo *orig) +{ + qemuMonitorCPUModelInfoPtr copy; + size_t i; + + if (VIR_ALLOC(copy) < 0) + goto cleanup; + + if (VIR_ALLOC_N(copy->props, orig->nprops) < 0) + goto cleanup; + + if (VIR_STRDUP(copy->name, orig->name) < 0) + goto cleanup; + + copy->nprops = orig->nprops; + + for (i = 0; i < orig->nprops; i++) { + if (VIR_STRDUP(copy->props[i].name, orig->props[i].name) < 0) + goto cleanup; + + copy->props[i].supported = orig->props[i].supported; + } + + return copy; + + cleanup: + qemuMonitorCPUModelInfoFree(copy); + return NULL; +} + + +int qemuMonitorGetCommands(qemuMonitorPtr mon, char ***commands) { diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h index 4d7fb9f..c36c3ea 100644 --- a/src/qemu/qemu_monitor.h +++ b/src/qemu/qemu_monitor.h @@ -925,6 +925,28 @@ int qemuMonitorGetCPUDefinitions(qemuMonitorPtr mon, qemuMonitorCPUDefInfoPtr **cpus); void qemuMonitorCPUDefInfoFree(qemuMonitorCPUDefInfoPtr cpu); +typedef struct _qemuMonitorCPUModelInfo qemuMonitorCPUModelInfo; +typedef qemuMonitorCPUModelInfo *qemuMonitorCPUModelInfoPtr; + +struct _qemuMonitorCPUModelInfo { + char *name; + size_t nprops; + struct { + char *name; + bool supported; + } *props; +}; + +int qemuMonitorGetCPUModelExpansion(qemuMonitorPtr mon, + const char *type, + const char *model_name, + qemuMonitorCPUModelInfoPtr *model_info); + +void qemuMonitorCPUModelInfoFree(qemuMonitorCPUModelInfoPtr model_info); + +qemuMonitorCPUModelInfoPtr +qemuMonitorCPUModelInfoCopy(const qemuMonitorCPUModelInfo *orig); + int qemuMonitorGetCommands(qemuMonitorPtr mon, char ***commands); int qemuMonitorGetEvents(qemuMonitorPtr mon, diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index 0c38b8f..9189a8b 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -4973,6 +4973,127 @@ qemuMonitorJSONGetCPUDefinitions(qemuMonitorPtr mon, return ret; } +int +qemuMonitorJSONParseCPUModelProperty(const char *key, + const virJSONValue *value, + void *opaque) +{ + qemuMonitorCPUModelInfoPtr machine_model = opaque; + size_t n = machine_model->nprops; + bool supported; + + if (!key) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("query-cpu-model-expansion reply data is missing a" + " property name")); + return -1; + } + if (VIR_STRDUP(machine_model->props[n].name, key) < 0) + return -1; + + if (virJSONValueGetBoolean(virJSONValueCopy(value), &supported) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("query-cpu-model-expansion reply data is missing a" + " feature support value")); + return -1; + } + machine_model->props[n].supported = supported; + + machine_model->nprops++; + return 0; +} + +int +qemuMonitorJSONGetCPUModelExpansion(qemuMonitorPtr mon, + const char *type, + const char *model_name, + qemuMonitorCPUModelInfoPtr *model_info) +{ + int ret = -1; + virJSONValuePtr model; + virJSONValuePtr cmd = NULL; + virJSONValuePtr reply = NULL; + virJSONValuePtr data; + virJSONValuePtr cpu_model; + virJSONValuePtr cpu_props; + qemuMonitorCPUModelInfoPtr machine_model = NULL; + char const *cpu_name; + + *model_info = NULL; + + if (!(model = virJSONValueNewObject())) + goto cleanup; + + if (virJSONValueObjectAppendString(model, "name", model_name) < 0) + goto cleanup; + + if (!(cmd = qemuMonitorJSONMakeCommand("query-cpu-model-expansion", + "s:type", type, + "a:model", model, + NULL))) + goto cleanup; + + /* model will be freed when cmd is freed. we set model + * to NULL to avoid double freeing. + */ + model = NULL; + + if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0) + goto cleanup; + + if (qemuMonitorJSONCheckError(cmd, reply) < 0) + goto cleanup; + + if (!(data = virJSONValueObjectGetObject(reply, "return"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("query-cpu-model-expansion reply data was missing return")); + goto cleanup; + } + + if (!(cpu_model = virJSONValueObjectGetObject(data, "model"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("query-cpu-model-expansion reply data was missing 'model'")); + goto cleanup; + } + + if (!(cpu_name = virJSONValueObjectGetString(cpu_model, "name"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("query-cpu-model-expansion reply data was missing 'name'")); + goto cleanup; + } + + if (!(cpu_props = virJSONValueObjectGetObject(cpu_model, "props"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("query-cpu-model-expansion reply data was missing 'props'")); + goto cleanup; + } + + if (VIR_ALLOC(machine_model) < 0) + goto cleanup; + + if (VIR_STRDUP(machine_model->name, cpu_name) < 0) + goto cleanup; + + if (VIR_ALLOC_N(machine_model->props, cpu_props->data.object.npairs) < 0) + goto cleanup; + + if (virJSONValueObjectForeachKeyValue(cpu_props, + qemuMonitorJSONParseCPUModelProperty, + machine_model) < 0) + goto cleanup; + + ret = 0; + *model_info = machine_model; + machine_model = NULL; + + cleanup: + qemuMonitorCPUModelInfoFree(machine_model); + virJSONValueFree(cmd); + virJSONValueFree(reply); + virJSONValueFree(model); + return ret; +} + int qemuMonitorJSONGetCommands(qemuMonitorPtr mon, char ***commands) diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h index adff0c3..10e4955 100644 --- a/src/qemu/qemu_monitor_json.h +++ b/src/qemu/qemu_monitor_json.h @@ -352,6 +352,18 @@ int qemuMonitorJSONGetCPUDefinitions(qemuMonitorPtr mon, qemuMonitorCPUDefInfoPtr **cpus) ATTRIBUTE_NONNULL(2); +int +qemuMonitorJSONParseCPUModelProperty(const char *key, + const virJSONValue *value, + void *opaque) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); + +int qemuMonitorJSONGetCPUModelExpansion(qemuMonitorPtr mon, + const char *type, + const char *model_name, + qemuMonitorCPUModelInfoPtr *model_info) + ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4); + int qemuMonitorJSONGetCommands(qemuMonitorPtr mon, char ***commands) ATTRIBUTE_NONNULL(2); -- 2.7.4 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list