[libvirt] [PATCH 4/8] qemu: command: Refactor memballoon command line formatting

Peter Krempa pkrempa at redhat.com
Wed Apr 6 16:02:24 UTC 2016


Now that there is just one format of the memory balloon command line
used the code can be merged into a single function.

Additionally with some tweaks to the control flow the code is easier to
read.
---
 src/qemu/qemu_command.c | 76 +++++++++++++++++++------------------------------
 src/qemu/qemu_command.h |  4 ---
 2 files changed, 29 insertions(+), 51 deletions(-)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 5843516..a2448bf 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -3397,14 +3397,29 @@ qemuBuildWatchdogCommandLine(virCommandPtr cmd,
 }


-char *
-qemuBuildMemballoonDevStr(const virDomainDef *def,
-                          virDomainMemballoonDefPtr dev,
-                          virQEMUCapsPtr qemuCaps)
+static int
+qemuBuildMemballoonCommandLine(virCommandPtr cmd,
+                               const virDomainDef *def,
+                               virQEMUCapsPtr qemuCaps)
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;

-    switch (dev->info.type) {
+    if (STREQLEN(def->os.machine, "s390-virtio", 10) &&
+        virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_S390) && def->memballoon)
+        def->memballoon->model = VIR_DOMAIN_MEMBALLOON_MODEL_NONE;
+
+    if (!def->memballoon ||
+        def->memballoon->model == VIR_DOMAIN_MEMBALLOON_MODEL_NONE)
+        return 0;
+
+    if (def->memballoon->model != VIR_DOMAIN_MEMBALLOON_MODEL_VIRTIO) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("Memory balloon device type '%s' is not supported by this version of qemu"),
+                       virDomainMemballoonModelTypeToString(def->memballoon->model));
+        return -1;
+    }
+
+    switch (def->memballoon->info.type) {
         case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI:
             virBufferAddLit(&buf, "virtio-balloon-pci");
             break;
@@ -3417,15 +3432,15 @@ qemuBuildMemballoonDevStr(const virDomainDef *def,
         default:
             virReportError(VIR_ERR_XML_ERROR,
                            _("memballoon unsupported with address type '%s'"),
-                           virDomainDeviceAddressTypeToString(dev->info.type));
+                           virDomainDeviceAddressTypeToString(def->memballoon->info.type));
             goto error;
     }

-    virBufferAsprintf(&buf, ",id=%s", dev->info.alias);
-    if (qemuBuildDeviceAddressStr(&buf, def, &dev->info, qemuCaps) < 0)
+    virBufferAsprintf(&buf, ",id=%s", def->memballoon->info.alias);
+    if (qemuBuildDeviceAddressStr(&buf, def, &def->memballoon->info, qemuCaps) < 0)
         goto error;

-    if (dev->autodeflate != VIR_TRISTATE_SWITCH_ABSENT) {
+    if (def->memballoon->autodeflate != VIR_TRISTATE_SWITCH_ABSENT) {
         if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_BALLOON_AUTODEFLATE)) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                            _("deflate-on-oom is not supported by this QEMU binary"));
@@ -3433,49 +3448,16 @@ qemuBuildMemballoonDevStr(const virDomainDef *def,
         }

         virBufferAsprintf(&buf, ",deflate-on-oom=%s",
-                          virTristateSwitchTypeToString(dev->autodeflate));
+                          virTristateSwitchTypeToString(def->memballoon->autodeflate));
     }

-    if (virBufferCheckError(&buf) < 0)
-        goto error;
-
-    return virBufferContentAndReset(&buf);
+    virCommandAddArg(cmd, "-device");
+    virCommandAddArgBuffer(cmd, &buf);
+    return 0;

  error:
     virBufferFreeAndReset(&buf);
-    return NULL;
-}
-
-
-static int
-qemuBuildMemballoonCommandLine(virCommandPtr cmd,
-                               const virDomainDef *def,
-                               virQEMUCapsPtr qemuCaps)
-{
-    char *optstr;
-
-    if (STREQLEN(def->os.machine, "s390-virtio", 10) &&
-        virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_S390) && def->memballoon)
-        def->memballoon->model = VIR_DOMAIN_MEMBALLOON_MODEL_NONE;
-
-    if (def->memballoon &&
-        def->memballoon->model != VIR_DOMAIN_MEMBALLOON_MODEL_NONE) {
-        if (def->memballoon->model != VIR_DOMAIN_MEMBALLOON_MODEL_VIRTIO) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                           _("Memory balloon device type '%s' is not supported by this version of qemu"),
-                           virDomainMemballoonModelTypeToString(def->memballoon->model));
-            return -1;
-        }
-
-        virCommandAddArg(cmd, "-device");
-
-        optstr = qemuBuildMemballoonDevStr(def, def->memballoon, qemuCaps);
-        if (!optstr)
-            return -1;
-        virCommandAddArg(cmd, optstr);
-        VIR_FREE(optstr);
-    }
-    return 0;
+    return -1;
 }


diff --git a/src/qemu/qemu_command.h b/src/qemu/qemu_command.h
index a3e6a00..3e8ccd8 100644
--- a/src/qemu/qemu_command.h
+++ b/src/qemu/qemu_command.h
@@ -130,10 +130,6 @@ char *qemuBuildControllerDevStr(const virDomainDef *domainDef,
                                 virQEMUCapsPtr qemuCaps,
                                 int *nusbcontroller);

-char *qemuBuildMemballoonDevStr(const virDomainDef *domainDef,
-                                virDomainMemballoonDefPtr dev,
-                                virQEMUCapsPtr qemuCaps);
-
 int qemuBuildMemoryBackendStr(unsigned long long size,
                               unsigned long long pagesize,
                               int guestNode,
-- 
2.8.0




More information about the libvir-list mailing list