[libvirt] [PATCH v2 53/73] qemu: Move migration parameters JSON formatting

Jiri Denemark jdenemar at redhat.com
Wed Apr 11 14:41:43 UTC 2018


We want to have all migration parameters parsing and formatting at one
place, i.e., in qemu_migration_params.c.

Signed-off-by: Jiri Denemark <jdenemar at redhat.com>
---
 src/qemu/qemu_migration_params.c | 68 +++++++++++++++++++++++++++++++-
 src/qemu/qemu_monitor.c          | 35 ++++++++--------
 src/qemu/qemu_monitor.h          |  2 +-
 src/qemu/qemu_monitor_json.c     | 56 ++------------------------
 src/qemu/qemu_monitor_json.h     |  2 +-
 5 files changed, 90 insertions(+), 73 deletions(-)

diff --git a/src/qemu/qemu_migration_params.c b/src/qemu/qemu_migration_params.c
index 845234c34b..560c2d3ea5 100644
--- a/src/qemu/qemu_migration_params.c
+++ b/src/qemu/qemu_migration_params.c
@@ -375,6 +375,61 @@ qemuMigrationParamsFromJSON(virJSONValuePtr params)
 }
 
 
+static virJSONValuePtr
+qemuMigrationParamsToJSON(qemuMigrationParamsPtr migParams)
+{
+    virJSONValuePtr params = NULL;
+
+    if (!(params = virJSONValueNewObject()))
+        return NULL;
+
+#define APPEND(VALID, API, VAR, FIELD) \
+    do { \
+        if (VALID && API(params, FIELD, migParams->params.VAR) < 0) \
+            goto error; \
+    } while (0)
+
+#define APPEND_INT(VAR, FIELD) \
+    APPEND(migParams->params.VAR ## _set, \
+           virJSONValueObjectAppendNumberInt, VAR, FIELD)
+
+#define APPEND_STR(VAR, FIELD) \
+    APPEND(migParams->params.VAR, \
+           virJSONValueObjectAppendString, VAR, FIELD)
+
+#define APPEND_ULONG(VAR, FIELD) \
+    APPEND(migParams->params.VAR ## _set, \
+           virJSONValueObjectAppendNumberUlong, VAR, FIELD)
+
+#define APPEND_BOOL(VAR, FIELD) \
+    APPEND(migParams->params.VAR ## _set, \
+           virJSONValueObjectAppendBoolean, VAR, FIELD)
+
+    APPEND_INT(compressLevel, "compress-level");
+    APPEND_INT(compressThreads, "compress-threads");
+    APPEND_INT(decompressThreads, "decompress-threads");
+    APPEND_INT(cpuThrottleInitial, "cpu-throttle-initial");
+    APPEND_INT(cpuThrottleIncrement, "cpu-throttle-increment");
+    APPEND_STR(tlsCreds, "tls-creds");
+    APPEND_STR(tlsHostname, "tls-hostname");
+    APPEND_ULONG(maxBandwidth, "max-bandwidth");
+    APPEND_ULONG(downtimeLimit, "downtime-limit");
+    APPEND_BOOL(blockIncremental, "block-incremental");
+    APPEND_ULONG(xbzrleCacheSize, "xbzrle-cache-size");
+
+#undef APPEND
+#undef APPEND_INT
+#undef APPEND_STR
+#undef APPEND_ULONG
+
+    return params;
+
+ error:
+    virJSONValueFree(params);
+    return NULL;
+}
+
+
 /**
  * qemuMigrationParamsApply
  * @driver: qemu driver
@@ -394,7 +449,9 @@ qemuMigrationParamsApply(virQEMUDriverPtr driver,
 {
     qemuDomainObjPrivatePtr priv = vm->privateData;
     bool xbzrleCacheSize_old = false;
+    virJSONValuePtr params = NULL;
     int ret = -1;
+    int rc;
 
     if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
         return -1;
@@ -417,9 +474,16 @@ qemuMigrationParamsApply(virQEMUDriverPtr driver,
         migParams->params.xbzrleCacheSize_set = false;
     }
 
-    if (qemuMonitorSetMigrationParams(priv->mon, &migParams->params) < 0)
+    if (!(params = qemuMigrationParamsToJSON(migParams)))
         goto cleanup;
 
+    if (virJSONValueObjectKeysNumber(params) > 0) {
+        rc = qemuMonitorSetMigrationParams(priv->mon, params);
+        params = NULL;
+        if (rc < 0)
+            goto cleanup;
+    }
+
     ret = 0;
 
  cleanup:
@@ -429,6 +493,8 @@ qemuMigrationParamsApply(virQEMUDriverPtr driver,
     if (xbzrleCacheSize_old)
         migParams->params.xbzrleCacheSize_set = true;
 
+    virJSONValueFree(params);
+
     return ret;
 }
 
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index 411ce28787..641465f227 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -2642,29 +2642,28 @@ qemuMonitorGetMigrationParams(qemuMonitorPtr mon,
     return qemuMonitorJSONGetMigrationParams(mon, params);
 }
 
+
+/**
+ * qemuMonitorSetMigrationParams:
+ * @mon: Pointer to the monitor object.
+ * @params: Migration parameters.
+ *
+ * The @params object is consumed and should not be referenced by the caller
+ * after this function returns.
+ *
+ * Returns 0 on success, -1 on error.
+ */
 int
 qemuMonitorSetMigrationParams(qemuMonitorPtr mon,
-                              qemuMonitorMigrationParamsPtr params)
+                              virJSONValuePtr params)
 {
-    VIR_DEBUG("compressLevel=%d:%d compressThreads=%d:%d "
-              "decompressThreads=%d:%d cpuThrottleInitial=%d:%d "
-              "cpuThrottleIncrement=%d:%d tlsCreds=%s tlsHostname=%s "
-              "maxBandwidth=%d:%llu downtimeLimit=%d:%llu "
-              "blockIncremental=%d:%d xbzrleCacheSize=%d:%llu",
-              params->compressLevel_set, params->compressLevel,
-              params->compressThreads_set, params->compressThreads,
-              params->decompressThreads_set, params->decompressThreads,
-              params->cpuThrottleInitial_set, params->cpuThrottleInitial,
-              params->cpuThrottleIncrement_set, params->cpuThrottleIncrement,
-              NULLSTR(params->tlsCreds), NULLSTR(params->tlsHostname),
-              params->maxBandwidth_set, params->maxBandwidth,
-              params->downtimeLimit_set, params->downtimeLimit,
-              params->blockIncremental_set, params->blockIncremental,
-              params->xbzrleCacheSize_set, params->xbzrleCacheSize);
-
-    QEMU_CHECK_MONITOR_JSON(mon);
+    QEMU_CHECK_MONITOR_JSON_GOTO(mon, error);
 
     return qemuMonitorJSONSetMigrationParams(mon, params);
+
+ error:
+    virJSONValueFree(params);
+    return -1;
 }
 
 
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index 261f3192f5..fd3c767dcf 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -681,7 +681,7 @@ struct _qemuMonitorMigrationParams {
 int qemuMonitorGetMigrationParams(qemuMonitorPtr mon,
                                   virJSONValuePtr *params);
 int qemuMonitorSetMigrationParams(qemuMonitorPtr mon,
-                                  qemuMonitorMigrationParamsPtr params);
+                                  virJSONValuePtr params);
 
 typedef enum {
     QEMU_MONITOR_MIGRATION_STATUS_INACTIVE,
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 7de1ade28e..b00bca7d46 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -2807,11 +2807,10 @@ qemuMonitorJSONGetMigrationParams(qemuMonitorPtr mon,
 
 int
 qemuMonitorJSONSetMigrationParams(qemuMonitorPtr mon,
-                                  qemuMonitorMigrationParamsPtr params)
+                                  virJSONValuePtr params)
 {
     int ret = -1;
     virJSONValuePtr cmd = NULL;
-    virJSONValuePtr args = NULL;
     virJSONValuePtr reply = NULL;
 
     if (!(cmd = virJSONValueNewObject()))
@@ -2821,56 +2820,9 @@ qemuMonitorJSONSetMigrationParams(qemuMonitorPtr mon,
                                        "migrate-set-parameters") < 0)
         goto cleanup;
 
-    if (!(args = virJSONValueNewObject()))
+    if (virJSONValueObjectAppend(cmd, "arguments", params) < 0)
         goto cleanup;
-
-#define APPEND(VALID, API, VAR, FIELD) \
-    do { \
-        if (VALID && API(args, FIELD, params->VAR) < 0) \
-            goto cleanup; \
-    } while (0)
-
-#define APPEND_INT(VAR, FIELD) \
-    APPEND(params->VAR ## _set, \
-           virJSONValueObjectAppendNumberInt, VAR, FIELD)
-
-#define APPEND_STR(VAR, FIELD) \
-    APPEND(params->VAR, \
-           virJSONValueObjectAppendString, VAR, FIELD)
-
-#define APPEND_ULONG(VAR, FIELD) \
-    APPEND(params->VAR ## _set, \
-           virJSONValueObjectAppendNumberUlong, VAR, FIELD)
-
-#define APPEND_BOOL(VAR, FIELD) \
-    APPEND(params->VAR ## _set, \
-           virJSONValueObjectAppendBoolean, VAR, FIELD)
-
-    APPEND_INT(compressLevel, "compress-level");
-    APPEND_INT(compressThreads, "compress-threads");
-    APPEND_INT(decompressThreads, "decompress-threads");
-    APPEND_INT(cpuThrottleInitial, "cpu-throttle-initial");
-    APPEND_INT(cpuThrottleIncrement, "cpu-throttle-increment");
-    APPEND_STR(tlsCreds, "tls-creds");
-    APPEND_STR(tlsHostname, "tls-hostname");
-    APPEND_ULONG(maxBandwidth, "max-bandwidth");
-    APPEND_ULONG(downtimeLimit, "downtime-limit");
-    APPEND_BOOL(blockIncremental, "block-incremental");
-    APPEND_ULONG(xbzrleCacheSize, "xbzrle-cache-size");
-
-#undef APPEND
-#undef APPEND_INT
-#undef APPEND_STR
-#undef APPEND_ULONG
-
-    if (virJSONValueObjectKeysNumber(args) == 0) {
-        ret = 0;
-        goto cleanup;
-    }
-
-    if (virJSONValueObjectAppend(cmd, "arguments", args) < 0)
-        goto cleanup;
-    args = NULL;
+    params = NULL;
 
     if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
         goto cleanup;
@@ -2881,7 +2833,7 @@ qemuMonitorJSONSetMigrationParams(qemuMonitorPtr mon,
     ret = 0;
  cleanup:
     virJSONValueFree(cmd);
-    virJSONValueFree(args);
+    virJSONValueFree(params);
     virJSONValueFree(reply);
     return ret;
 }
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
index a52f0a1955..5ada38b9fa 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -137,7 +137,7 @@ int qemuMonitorJSONSetMigrationCacheSize(qemuMonitorPtr mon,
 int qemuMonitorJSONGetMigrationParams(qemuMonitorPtr mon,
                                       virJSONValuePtr *params);
 int qemuMonitorJSONSetMigrationParams(qemuMonitorPtr mon,
-                                      qemuMonitorMigrationParamsPtr params);
+                                      virJSONValuePtr params);
 
 int qemuMonitorJSONGetMigrationStats(qemuMonitorPtr mon,
                                      qemuMonitorMigrationStatsPtr stats,
-- 
2.17.0




More information about the libvir-list mailing list