[PATCH 3/4] qemu_monitor: Switch to virDomainMemoryModel enum in qemuMonitorJSONGetMemoryDeviceInfo()

Michal Privoznik mprivozn at redhat.com
Mon Feb 27 11:35:02 UTC 2023


When processing memory devices (as a reply from QEMU), a bunch of
STREQ()-s is used. Fortunately, the set of strings we process is
the same as virDomainMemoryModel enum. Therefore, we can use
virDomainMemoryModelTypeFromString() and when use integer
comparison (well, switch()). This has an up side, that
introducing a new memory model let's us see immediately at
compile time, what places need adjusting.

NB, this is in contrast with cmd line generator
(qemuBuildMemoryDeviceProps()), where more specific models are
generated (e.g. "pc-dimm", "virtio-mem-pci", etc.). But QEMU
reports back the parent model, instead of specific child
instance.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/qemu/qemu_monitor_json.c | 52 +++++++++++++++++++++++++-----------
 1 file changed, 37 insertions(+), 15 deletions(-)

diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index d76fea8b00..a5e525b7ce 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -7211,15 +7211,21 @@ qemuMonitorJSONGetMemoryDeviceInfo(qemuMonitor *mon,
         g_autofree qemuMonitorMemoryDeviceInfo *meminfo = NULL;
         virJSONValue *dimminfo;
         const char *devalias;
-        const char *type;
+        const char *modelStr;
+        int model;
 
-        if (!(type = virJSONValueObjectGetString(elem, "type"))) {
+        if (!(modelStr = virJSONValueObjectGetString(elem, "type"))) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("query-memory-devices reply data doesn't contain "
                              "enum type discriminator"));
             return -1;
         }
 
+        if ((model = virDomainMemoryModelTypeFromString(modelStr)) < 0) {
+            VIR_WARN("Unknown memory model: %s", modelStr);
+            continue;
+        }
+
         if (!(dimminfo = virJSONValueObjectGetObject(elem, "data"))) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("query-memory-devices reply data doesn't "
@@ -7227,30 +7233,40 @@ qemuMonitorJSONGetMemoryDeviceInfo(qemuMonitor *mon,
             return -1;
         }
 
-        if (STREQ(type, "dimm") || STREQ(type, "nvdimm") || STREQ(type, "virtio-mem")) {
+        switch ((virDomainMemoryModel) model) {
+        case VIR_DOMAIN_MEMORY_MODEL_DIMM:
+        case VIR_DOMAIN_MEMORY_MODEL_NVDIMM:
+        case VIR_DOMAIN_MEMORY_MODEL_VIRTIO_MEM:
             /* While 'id' attribute is marked as optional in QEMU's QAPI
-            * specification, Libvirt always sets it. Thus we can fail if not
-            * present. */
+             * specification, Libvirt always sets it. Thus we can fail if not
+             * present. */
             if (!(devalias = virJSONValueObjectGetString(dimminfo, "id"))) {
                 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                            _("dimm memory info data is missing 'id'"));
+                               _("dimm memory info data is missing 'id'"));
                 return -1;
             }
-        } else if (STREQ(type, "sgx-epc")) {
+            break;
+
+        case VIR_DOMAIN_MEMORY_MODEL_SGX_EPC:
             if (!(devalias = virJSONValueObjectGetString(dimminfo, "memdev"))) {
                 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                            _("sgx-epc memory info data is missing 'memdev'"));
+                               _("sgx-epc memory info data is missing 'memdev'"));
                 return -1;
             }
-        } else {
+            break;
+
+        case VIR_DOMAIN_MEMORY_MODEL_VIRTIO_PMEM:
+        case VIR_DOMAIN_MEMORY_MODEL_NONE:
+        case VIR_DOMAIN_MEMORY_MODEL_LAST:
             /* type not handled yet */
             continue;
         }
 
         meminfo = g_new0(qemuMonitorMemoryDeviceInfo, 1);
 
-        /* dimm memory devices */
-        if (STREQ(type, "dimm") || STREQ(type, "nvdimm")) {
+        switch ((virDomainMemoryModel) model) {
+        case VIR_DOMAIN_MEMORY_MODEL_DIMM:
+        case VIR_DOMAIN_MEMORY_MODEL_NVDIMM:
             if (virJSONValueObjectGetNumberUlong(dimminfo, "addr",
                                                  &meminfo->address) < 0) {
                 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -7280,16 +7296,18 @@ qemuMonitorJSONGetMemoryDeviceInfo(qemuMonitor *mon,
                 return -1;
 
             }
+            break;
 
-        } else if (STREQ(type, "virtio-mem")) {
+        case VIR_DOMAIN_MEMORY_MODEL_VIRTIO_MEM:
             if (virJSONValueObjectGetNumberUlong(dimminfo, "size",
                                                  &meminfo->size) < 0) {
                 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                                _("malformed/missing size in virtio memory info"));
                 return -1;
             }
-        } else if (STREQ(type, "sgx-epc")) {
-            /* sgx-epc memory devices */
+            break;
+
+        case VIR_DOMAIN_MEMORY_MODEL_SGX_EPC:
             if (virJSONValueObjectGetNumberUlong(dimminfo, "memaddr",
                                                  &meminfo->address) < 0) {
                 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -7303,7 +7321,11 @@ qemuMonitorJSONGetMemoryDeviceInfo(qemuMonitor *mon,
                                _("malformed/missing size in sgx-epc memory info"));
                 return -1;
             }
-        } else {
+            break;
+
+        case VIR_DOMAIN_MEMORY_MODEL_VIRTIO_PMEM:
+        case VIR_DOMAIN_MEMORY_MODEL_NONE:
+        case VIR_DOMAIN_MEMORY_MODEL_LAST:
             /* type not handled yet */
             continue;
         }
-- 
2.39.2



More information about the libvir-list mailing list