When parsing a TPM device plenty of virXMLPropString() + enum2int() combos are used. These can be replaced with virXMLPropEnum(). Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx> --- src/conf/domain_conf.c | 39 ++++++++++++--------------------------- src/conf/domain_conf.h | 6 +++--- src/qemu/qemu_command.c | 2 +- src/qemu/qemu_domain.c | 2 +- 4 files changed, 17 insertions(+), 32 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 28f0e75e60..6263d90fdb 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -10347,9 +10347,6 @@ virDomainTPMDefParseXML(virDomainXMLOption *xmlopt, int nnodes; size_t i; g_autofree char *path = NULL; - g_autofree char *model = NULL; - g_autofree char *backend = NULL; - g_autofree char *version = NULL; g_autofree char *secretuuid = NULL; g_autofree char *persistent_state = NULL; g_autofree xmlNodePtr *backends = NULL; @@ -10358,13 +10355,11 @@ virDomainTPMDefParseXML(virDomainXMLOption *xmlopt, def = g_new0(virDomainTPMDef, 1); - model = virXMLPropString(node, "model"); - if (model != NULL && - (def->model = virDomainTPMModelTypeFromString(model)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("Unknown TPM frontend model '%s'"), model); + if (virXMLPropEnum(node, "model", + virDomainTPMModelTypeFromString, + VIR_XML_PROP_NONZERO, + &def->model) < 0) goto error; - } ctxt->node = node; @@ -10383,18 +10378,11 @@ virDomainTPMDefParseXML(virDomainXMLOption *xmlopt, goto error; } - if (!(backend = virXMLPropString(backends[0], "type"))) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("missing TPM device backend type")); + if (virXMLPropEnum(backends[0], "type", + virDomainTPMBackendTypeFromString, + VIR_XML_PROP_REQUIRED, + &def->type) < 0) goto error; - } - - if ((def->type = virDomainTPMBackendTypeFromString(backend)) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("Unknown TPM backend type '%s'"), - backend); - goto error; - } switch (def->type) { case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: @@ -10407,14 +10395,11 @@ virDomainTPMDefParseXML(virDomainXMLOption *xmlopt, def->data.passthrough.source->data.file.path = g_steal_pointer(&path); break; case VIR_DOMAIN_TPM_TYPE_EMULATOR: - version = virXMLPropString(backends[0], "version"); - if (version && - (def->data.emulator.version = virDomainTPMVersionTypeFromString(version)) <= 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("Unsupported TPM version '%s'"), - version); + if (virXMLPropEnum(backends[0], "version", + virDomainTPMVersionTypeFromString, + VIR_XML_PROP_NONZERO, + &def->data.emulator.version) < 0) goto error; - } if (!(def->data.emulator.source = virDomainChrSourceDefNew(xmlopt))) goto error; diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 3362042db5..bab667d026 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -1436,15 +1436,15 @@ typedef enum { #define VIR_DOMAIN_TPM_DEFAULT_DEVICE "/dev/tpm0" struct _virDomainTPMDef { - int type; /* virDomainTPMBackendType */ + virDomainTPMModel model; + virDomainTPMBackendType type; virDomainDeviceInfo info; - int model; /* virDomainTPMModel */ union { struct { virDomainChrSourceDef *source; } passthrough; struct { - int version; /* virDomainTPMVersion */ + virDomainTPMVersion version; virDomainChrSourceDef *source; char *storagepath; char *logfile; diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 262fffe5fe..93ea8748c7 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -9840,7 +9840,7 @@ qemuBuildTPMCommandLine(virCommand *cmd, g_autoptr(qemuFDPass) passtpm = NULL; g_autoptr(qemuFDPass) passcancel = NULL; - switch ((virDomainTPMBackendType) tpm->type) { + switch (tpm->type) { case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: { VIR_AUTOCLOSE fdtpm = -1; VIR_AUTOCLOSE fdcancel = -1; diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 0343fd3597..09fc88e7fa 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -11752,7 +11752,7 @@ qemuDomainDeviceBackendChardevForeachOne(virDomainDeviceDef *dev, return cb(dev, dev->data.rng->source.chardev, opaque); case VIR_DOMAIN_DEVICE_TPM: - switch ((virDomainTPMBackendType) dev->data.tpm->type) { + switch (dev->data.tpm->type) { case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: return cb(dev, dev->data.tpm->data.passthrough.source, opaque); -- 2.35.1