From d72c84f9369152d0ca69fb201d4ae41ee559a94b Mon Sep 17 00:00:00 2001
From: Jiri Denemark <jdenemar@redhat.com>
Date: Wed, 10 Jun 2020 16:13:15 +0200
Subject: [PATCH 2/8] qemu: Avoid deprecated migrate_set_speed QMP command

The same functionality can be achieved using migrate-set-parameters QMP
command with max-bandwidth parameter.

https://bugzilla.redhat.com/show_bug.cgi?id=1829545

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
---
 src/qemu/qemu_driver.c    | 18 ++++++++++---
 src/qemu/qemu_migration.c | 53 +++++++++++++++++++++++++++++++--------
 2 files changed, 57 insertions(+), 14 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 21e9a8760e..1521bc6b2b 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -14086,6 +14086,7 @@ qemuDomainMigrateSetMaxSpeed(virDomainPtr dom,
     qemuDomainObjPrivatePtr priv;
     bool postcopy = !!(flags & VIR_DOMAIN_MIGRATE_MAX_SPEED_POSTCOPY);
     g_autoptr(qemuMigrationParams) migParams = NULL;
+    bool bwParam;
     unsigned long long max;
     int ret = -1;
 
@@ -14124,12 +14125,20 @@ qemuDomainMigrateSetMaxSpeed(virDomainPtr dom,
 
     VIR_DEBUG("Setting migration bandwidth to %luMbs", bandwidth);
 
-    if (postcopy) {
+    bwParam = virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_MIGRATION_PARAM_BANDWIDTH);
+
+    if (postcopy || bwParam) {
+        qemuMigrationParam param;
+
         if (!(migParams = qemuMigrationParamsNew()))
             goto endjob;
 
-        if (qemuMigrationParamsSetULL(migParams,
-                                      QEMU_MIGRATION_PARAM_MAX_POSTCOPY_BANDWIDTH,
+        if (postcopy)
+            param = QEMU_MIGRATION_PARAM_MAX_POSTCOPY_BANDWIDTH;
+        else
+            param = QEMU_MIGRATION_PARAM_MAX_BANDWIDTH;
+
+        if (qemuMigrationParamsSetULL(migParams, param,
                                       bandwidth * 1024 * 1024) < 0)
             goto endjob;
 
@@ -14143,9 +14152,10 @@ qemuDomainMigrateSetMaxSpeed(virDomainPtr dom,
         rc = qemuMonitorSetMigrationSpeed(priv->mon, bandwidth);
         if (qemuDomainObjExitMonitor(driver, vm) < 0 || rc < 0)
             goto endjob;
+    }
 
+    if (!postcopy)
         priv->migMaxBandwidth = bandwidth;
-    }
 
     ret = 0;
 
diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index 1665071eb3..cd40a886e3 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -3507,6 +3507,7 @@ qemuMigrationSrcRun(virQEMUDriverPtr driver,
     unsigned int cookieFlags = 0;
     bool abort_on_error = !!(flags & VIR_MIGRATE_ABORT_ON_ERROR);
     bool events = virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_MIGRATION_EVENT);
+    bool bwParam = virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_MIGRATION_PARAM_BANDWIDTH);
     bool cancel = false;
     unsigned int waitFlags;
     virDomainDefPtr persistDef = NULL;
@@ -3594,6 +3595,11 @@ qemuMigrationSrcRun(virQEMUDriverPtr driver,
             goto error;
     }
 
+    if (bwParam &&
+        qemuMigrationParamsSetULL(migParams, QEMU_MIGRATION_PARAM_MAX_BANDWIDTH,
+                                  migrate_speed * 1024 * 1024) < 0)
+        goto error;
+
     if (qemuMigrationParamsApply(driver, vm, QEMU_ASYNC_JOB_MIGRATION_OUT,
                                  migParams) < 0)
         goto error;
@@ -3656,7 +3662,8 @@ qemuMigrationSrcRun(virQEMUDriverPtr driver,
         goto exit_monitor;
     }
 
-    if (qemuMonitorSetMigrationSpeed(priv->mon, migrate_speed) < 0)
+    if (!bwParam &&
+        qemuMonitorSetMigrationSpeed(priv->mon, migrate_speed) < 0)
         goto exit_monitor;
 
     /* connect to the destination qemu if needed */
@@ -5311,24 +5318,41 @@ qemuMigrationSrcToFile(virQEMUDriverPtr driver, virDomainObjPtr vm,
                        qemuDomainAsyncJob asyncJob)
 {
     qemuDomainObjPrivatePtr priv = vm->privateData;
+    bool bwParam = virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_MIGRATION_PARAM_BANDWIDTH);
     int rc;
     int ret = -1;
     int pipeFD[2] = { -1, -1 };
     unsigned long saveMigBandwidth = priv->migMaxBandwidth;
     char *errbuf = NULL;
     virErrorPtr orig_err = NULL;
+    g_autoptr(qemuMigrationParams) migParams = NULL;
 
     if (qemuMigrationSetDBusVMState(driver, vm) < 0)
         return -1;
 
     /* Increase migration bandwidth to unlimited since target is a file.
      * Failure to change migration speed is not fatal. */
-    if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) == 0) {
-        qemuMonitorSetMigrationSpeed(priv->mon,
-                                     QEMU_DOMAIN_MIG_BANDWIDTH_MAX);
-        priv->migMaxBandwidth = QEMU_DOMAIN_MIG_BANDWIDTH_MAX;
-        if (qemuDomainObjExitMonitor(driver, vm) < 0)
+    if (bwParam) {
+        if (!(migParams = qemuMigrationParamsNew()))
+            return -1;
+
+        if (qemuMigrationParamsSetULL(migParams,
+                                      QEMU_MIGRATION_PARAM_MAX_BANDWIDTH,
+                                      QEMU_DOMAIN_MIG_BANDWIDTH_MAX * 1024 * 1024) < 0)
+            return -1;
+
+        if (qemuMigrationParamsApply(driver, vm, asyncJob, migParams) < 0)
             return -1;
+
+        priv->migMaxBandwidth = QEMU_DOMAIN_MIG_BANDWIDTH_MAX;
+    } else {
+        if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) == 0) {
+            qemuMonitorSetMigrationSpeed(priv->mon,
+                                         QEMU_DOMAIN_MIG_BANDWIDTH_MAX);
+            priv->migMaxBandwidth = QEMU_DOMAIN_MIG_BANDWIDTH_MAX;
+            if (qemuDomainObjExitMonitor(driver, vm) < 0)
+                return -1;
+        }
     }
 
     if (!virDomainObjIsActive(vm)) {
@@ -5409,11 +5433,20 @@ qemuMigrationSrcToFile(virQEMUDriverPtr driver, virDomainObjPtr vm,
         virErrorPreserveLast(&orig_err);
 
     /* Restore max migration bandwidth */
-    if (virDomainObjIsActive(vm) &&
-        qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) == 0) {
-        qemuMonitorSetMigrationSpeed(priv->mon, saveMigBandwidth);
+    if (virDomainObjIsActive(vm)) {
+        if (bwParam) {
+            if (qemuMigrationParamsSetULL(migParams,
+                                          QEMU_MIGRATION_PARAM_MAX_BANDWIDTH,
+                                          saveMigBandwidth * 1024 * 1024) == 0)
+                ignore_value(qemuMigrationParamsApply(driver, vm, asyncJob,
+                                                      migParams));
+        } else {
+            if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) == 0) {
+                qemuMonitorSetMigrationSpeed(priv->mon, saveMigBandwidth);
+                ignore_value(qemuDomainObjExitMonitor(driver, vm));
+            }
+        }
         priv->migMaxBandwidth = saveMigBandwidth;
-        ignore_value(qemuDomainObjExitMonitor(driver, vm));
     }
 
     VIR_FORCE_CLOSE(pipeFD[0]);
-- 
2.27.0

