[PATCH 2/2] conf: Move generation of NVDIMM UUID into post parse callback

Michal Privoznik posted 2 patches 5 years ago
[PATCH 2/2] conf: Move generation of NVDIMM UUID into post parse callback
Posted by Michal Privoznik 5 years ago
It's better to fill in missing values in post parse callbacks
than during parsing.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/conf/domain_conf.c | 35 ++++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index a2ddfcf947..4f0798de45 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -5334,7 +5334,8 @@ virDomainVsockDefPostParse(virDomainVsockDefPtr vsock)
 
 
 static int
-virDomainMemoryDefPostParse(virDomainMemoryDefPtr mem)
+virDomainMemoryDefPostParse(virDomainMemoryDefPtr mem,
+                            const virDomainDef *def)
 {
     switch (mem->model) {
     case VIR_DOMAIN_MEMORY_MODEL_VIRTIO_PMEM:
@@ -5345,6 +5346,19 @@ virDomainMemoryDefPostParse(virDomainMemoryDefPtr mem)
         break;
 
     case VIR_DOMAIN_MEMORY_MODEL_NVDIMM:
+        /* If no NVDIMM UUID was provided in XML, generate one. */
+        if (ARCH_IS_PPC64(def->os.arch) &&
+            !mem->uuid) {
+
+            mem->uuid = g_new0(unsigned char, VIR_UUID_BUFLEN);
+            if (virUUIDGenerate(mem->uuid) < 0) {
+                virReportError(VIR_ERR_INTERNAL_ERROR,
+                               "%s", _("Failed to generate UUID"));
+                return -1;
+            }
+        }
+        break;
+
     case VIR_DOMAIN_MEMORY_MODEL_DIMM:
     case VIR_DOMAIN_MEMORY_MODEL_NONE:
     case VIR_DOMAIN_MEMORY_MODEL_LAST:
@@ -5401,7 +5415,7 @@ virDomainDeviceDefPostParseCommon(virDomainDeviceDefPtr dev,
         break;
 
     case VIR_DOMAIN_DEVICE_MEMORY:
-        ret = virDomainMemoryDefPostParse(dev->data.memory);
+        ret = virDomainMemoryDefPostParse(dev->data.memory, def);
         break;
 
     case VIR_DOMAIN_DEVICE_LEASE:
@@ -15526,24 +15540,19 @@ virDomainMemoryDefParseXML(virDomainXMLOptionPtr xmlopt,
     }
     VIR_FREE(tmp);
 
+    /* Extract NVDIMM UUID. */
     if (def->model == VIR_DOMAIN_MEMORY_MODEL_NVDIMM &&
-        ARCH_IS_PPC64(dom->os.arch)) {
-        /* Extract nvdimm uuid or generate a new one */
-        tmp = virXPathString("string(./uuid[1])", ctxt);
-
+        ARCH_IS_PPC64(dom->os.arch) &&
+        (tmp = virXPathString("string(./uuid[1])", ctxt))) {
         def->uuid = g_new0(unsigned char, VIR_UUID_BUFLEN);
-        if (!tmp) {
-            if (virUUIDGenerate(def->uuid) < 0) {
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               "%s", _("Failed to generate UUID"));
-                goto error;
-            }
-        } else if (virUUIDParse(tmp, def->uuid) < 0) {
+
+        if (virUUIDParse(tmp, def->uuid) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            "%s", _("malformed uuid element"));
             goto error;
         }
     }
+    VIR_FREE(tmp);
 
     /* source */
     if ((node = virXPathNode("./source", ctxt)) &&
-- 
2.26.2

Re: [PATCH 2/2] conf: Move generation of NVDIMM UUID into post parse callback
Posted by Peter Krempa 5 years ago
On Mon, Jan 18, 2021 at 15:15:54 +0100, Michal Privoznik wrote:
> It's better to fill in missing values in post parse callbacks
> than during parsing.
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  src/conf/domain_conf.c | 35 ++++++++++++++++++++++-------------
>  1 file changed, 22 insertions(+), 13 deletions(-)
> 
> diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
> index a2ddfcf947..4f0798de45 100644
> --- a/src/conf/domain_conf.c
> +++ b/src/conf/domain_conf.c

[...]

> @@ -5345,6 +5346,19 @@ virDomainMemoryDefPostParse(virDomainMemoryDefPtr mem)
>          break;
>  
>      case VIR_DOMAIN_MEMORY_MODEL_NVDIMM:
> +        /* If no NVDIMM UUID was provided in XML, generate one. */
> +        if (ARCH_IS_PPC64(def->os.arch) &&
> +            !mem->uuid) {
> +
> +            mem->uuid = g_new0(unsigned char, VIR_UUID_BUFLEN);
> +            if (virUUIDGenerate(mem->uuid) < 0) {
> +                virReportError(VIR_ERR_INTERNAL_ERROR,
> +                               "%s", _("Failed to generate UUID"));
> +                return -1;
> +            }
> +        }


You can also reject if an UUID is present but the architecture isn't
PPC64 here, rather than ...

> +        break;
> +
>      case VIR_DOMAIN_MEMORY_MODEL_DIMM:
>      case VIR_DOMAIN_MEMORY_MODEL_NONE:
>      case VIR_DOMAIN_MEMORY_MODEL_LAST:

[...]

> @@ -15526,24 +15540,19 @@ virDomainMemoryDefParseXML(virDomainXMLOptionPtr xmlopt,
>      }
>      VIR_FREE(tmp);
>  
> +    /* Extract NVDIMM UUID. */
>      if (def->model == VIR_DOMAIN_MEMORY_MODEL_NVDIMM &&
> -        ARCH_IS_PPC64(dom->os.arch)) {
> -        /* Extract nvdimm uuid or generate a new one */
> -        tmp = virXPathString("string(./uuid[1])", ctxt);
> -
> +        ARCH_IS_PPC64(dom->os.arch) &&

... keeping it in the parser, where it doesn't make much sense.

> +        (tmp = virXPathString("string(./uuid[1])", ctxt))) {
>          def->uuid = g_new0(unsigned char, VIR_UUID_BUFLEN);
> -        if (!tmp) {
> -            if (virUUIDGenerate(def->uuid) < 0) {
> -                virReportError(VIR_ERR_INTERNAL_ERROR,
> -                               "%s", _("Failed to generate UUID"));
> -                goto error;
> -            }
> -        } else if (virUUIDParse(tmp, def->uuid) < 0) {
> +

Reviewed-by: Peter Krempa <pkrempa@redhat.com>