From 4483bab5e3d6b031c851e6b10323a37818f147e4 Mon Sep 17 00:00:00 2001
From: zhengchuan <zhengchuan@huawei.com>
Date: Wed, 30 Nov 2022 15:01:12 +0800
Subject: [PATCH 3/6] migration/migration-pin: add migrationpin for migration
 parameters

Add a migrationpin to the migration parameters of live migration to bind cores
to the migration thread during VM migration.

Signed-off-by:zhengchuan<zhengchuan@huawei.com>
---
 include/libvirt/libvirt-domain.h |  8 ++++++++
 src/qemu/qemu_domain.c           |  1 +
 src/qemu/qemu_domain.h           |  1 +
 src/qemu/qemu_migration.c        |  3 +++
 src/qemu/qemu_migration.h        |  1 +
 src/qemu/qemu_migration_params.c | 20 ++++++++++++++++++++
 src/qemu/qemu_migration_params.h |  4 ++++
 7 files changed, 38 insertions(+)

diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
index c7956c81d6..8fe723198d 100644
--- a/include/libvirt/libvirt-domain.h
+++ b/include/libvirt/libvirt-domain.h
@@ -1016,6 +1016,14 @@ typedef enum {
  */
 # define VIR_MIGRATE_PARAM_COMPRESSION_MT_DTHREADS "compression.mt.dthreads"
 
+/**
+ * VIR_MIGRATE_PARAM_MIGRATIONPIN:
+ *
+ * virDomainMigrate* params field: the pin of migration threads for
+ * migration as VIR_TYPED_PARAM_STRING.
+ */
+# define VIR_MIGRATE_PARAM_MIGRATIONPIN "migration.pin"
+
 /**
  * VIR_MIGRATE_PARAM_COMPRESSION_XBZRLE_CACHE:
  *
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 71d034fa42..b7fb4eb9f9 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -2323,6 +2323,7 @@ qemuDomainObjPrivateFree(void *data)
     VIR_FREE(priv->lockState);
     VIR_FREE(priv->origname);
     VIR_FREE(priv->migrationPids);
+    VIR_FREE(priv->migrationThreadPinList);
     virBitmapFree(priv->pcpumap);
 
     VIR_FREE(priv->migrationMultiFdPids);
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index 38a842ebf6..7603724ccd 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -333,6 +333,7 @@ struct _qemuDomainObjPrivate {
     char *origname;
     int nbdPort; /* Port used for migration with NBD */
     char *migrationPids;
+    char *migrationThreadPinList;
     unsigned short migrationPort;
     int preMigrationState;
     char *migrationMultiFdPids;
diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index faf4e223a8..b47e69c642 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -2533,6 +2533,7 @@ qemuMigrationDstPrepareAny(virQEMUDriverPtr driver,
 
     priv = vm->privateData;
     priv->origname = g_strdup(origname);
+    VIR_FREE(priv->migrationPids);
 
     if (taint_hook) {
         /* Domain XML has been altered by a hook script. */
@@ -3601,6 +3602,8 @@ qemuMigrationSrcRun(virQEMUDriverPtr driver,
                                   migrate_speed * 1024 * 1024) < 0)
         goto error;
 
+    qemuMigrationMigrationParamsToVM(migParams, vm);
+
     if (qemuMigrationParamsApply(driver, vm, QEMU_ASYNC_JOB_MIGRATION_OUT,
                                  migParams) < 0)
         goto error;
diff --git a/src/qemu/qemu_migration.h b/src/qemu/qemu_migration.h
index b6f88d3fd9..316a1a10f4 100644
--- a/src/qemu/qemu_migration.h
+++ b/src/qemu/qemu_migration.h
@@ -84,6 +84,7 @@
     VIR_MIGRATE_PARAM_BANDWIDTH_POSTCOPY, VIR_TYPED_PARAM_ULLONG, \
     VIR_MIGRATE_PARAM_PARALLEL_CONNECTIONS, VIR_TYPED_PARAM_INT, \
     VIR_MIGRATE_PARAM_TLS_DESTINATION, VIR_TYPED_PARAM_STRING, \
+    VIR_MIGRATE_PARAM_MIGRATIONPIN,    VIR_TYPED_PARAM_STRING, \
     NULL
 
 
diff --git a/src/qemu/qemu_migration_params.c b/src/qemu/qemu_migration_params.c
index df9d5d205a..4001fb2b34 100644
--- a/src/qemu/qemu_migration_params.c
+++ b/src/qemu/qemu_migration_params.c
@@ -108,6 +108,7 @@ VIR_ENUM_IMPL(qemuMigrationParam,
               "xbzrle-cache-size",
               "max-postcopy-bandwidth",
               "multifd-channels",
+              "migrationpin",
 );
 
 typedef struct _qemuMigrationParamsAlwaysOnItem qemuMigrationParamsAlwaysOnItem;
@@ -201,6 +202,10 @@ static const qemuMigrationParamsTPMapItem qemuMigrationParamsTPMap[] = {
     {.typedParam = VIR_MIGRATE_PARAM_TLS_DESTINATION,
      .param = QEMU_MIGRATION_PARAM_TLS_HOSTNAME,
      .party = QEMU_MIGRATION_SOURCE},
+
+    {.typedParam = VIR_MIGRATE_PARAM_MIGRATIONPIN,
+     .param = QEMU_MIGRATION_PARAM_MIGRATIONPIN,
+     .party = QEMU_MIGRATION_SOURCE},
 };
 
 static const qemuMigrationParamType qemuMigrationParamTypes[] = {
@@ -217,6 +222,7 @@ static const qemuMigrationParamType qemuMigrationParamTypes[] = {
     [QEMU_MIGRATION_PARAM_XBZRLE_CACHE_SIZE] = QEMU_MIGRATION_PARAM_TYPE_ULL,
     [QEMU_MIGRATION_PARAM_MAX_POSTCOPY_BANDWIDTH] = QEMU_MIGRATION_PARAM_TYPE_ULL,
     [QEMU_MIGRATION_PARAM_MULTIFD_CHANNELS] = QEMU_MIGRATION_PARAM_TYPE_INT,
+    [QEMU_MIGRATION_PARAM_MIGRATIONPIN] = QEMU_MIGRATION_PARAM_TYPE_STRING,
 };
 G_STATIC_ASSERT(G_N_ELEMENTS(qemuMigrationParamTypes) == QEMU_MIGRATION_PARAM_LAST);
 
@@ -534,6 +540,16 @@ qemuMigrationParamsSetCompression(virTypedParameterPtr params,
 }
 
 
+void
+qemuMigrationMigrationParamsToVM(const qemuMigrationParamsPtr migParams, const virDomainObjPtr vm)
+{
+    if (migParams && migParams->params[QEMU_MIGRATION_PARAM_MIGRATIONPIN].set) {
+        qemuDomainObjPrivatePtr priv = vm->privateData;
+        priv->migrationThreadPinList = g_strdup(migParams->params[QEMU_MIGRATION_PARAM_MIGRATIONPIN].value.s);
+    }
+}
+
+
 qemuMigrationParamsPtr
 qemuMigrationParamsFromFlags(virTypedParameterPtr params,
                              int nparams,
@@ -741,6 +757,10 @@ qemuMigrationParamsToJSON(qemuMigrationParamsPtr migParams)
         if (!pv->set)
             continue;
 
+        if (i == QEMU_MIGRATION_PARAM_MIGRATIONPIN) {
+            continue;
+        }
+
         rc = 0;
         switch (qemuMigrationParamTypes[i]) {
         case QEMU_MIGRATION_PARAM_TYPE_INT:
diff --git a/src/qemu/qemu_migration_params.h b/src/qemu/qemu_migration_params.h
index 9aea24725f..f3deffb4cc 100644
--- a/src/qemu/qemu_migration_params.h
+++ b/src/qemu/qemu_migration_params.h
@@ -58,6 +58,7 @@ typedef enum {
     QEMU_MIGRATION_PARAM_XBZRLE_CACHE_SIZE,
     QEMU_MIGRATION_PARAM_MAX_POSTCOPY_BANDWIDTH,
     QEMU_MIGRATION_PARAM_MULTIFD_CHANNELS,
+    QEMU_MIGRATION_PARAM_MIGRATIONPIN,
 
     QEMU_MIGRATION_PARAM_LAST
 } qemuMigrationParam;
@@ -74,6 +75,9 @@ typedef enum {
 virBitmapPtr
 qemuMigrationParamsGetAlwaysOnCaps(qemuMigrationParty party);
 
+void
+qemuMigrationMigrationParamsToVM(const qemuMigrationParamsPtr migParams, const virDomainObjPtr vm);
+
 qemuMigrationParamsPtr
 qemuMigrationParamsFromFlags(virTypedParameterPtr params,
                              int nparams,
-- 
2.25.1

