blob: 5ab5b6d85d1526475453939927a5119ae28a4e80 [file] [log] [blame]
Juan Quintela1f0776f2023-03-01 21:18:45 +01001/*
2 * QEMU migration capabilities
3 *
4 * Copyright (c) 2012-2023 Red Hat Inc
5 *
6 * Authors:
7 * Orit Wasserman <owasserm@redhat.com>
8 * Juan Quintela <quintela@redhat.com>
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
12 */
13
14#include "qemu/osdep.h"
Juan Quintela66db46c2023-10-18 13:55:12 +020015#include "qemu/error-report.h"
Juan Quintela09d6c962023-03-02 10:13:16 +010016#include "exec/target_page.h"
Juan Quintela9c894df2023-03-02 11:00:12 +010017#include "qapi/clone-visitor.h"
Juan Quintela77608702023-03-01 23:05:53 +010018#include "qapi/error.h"
Juan Quintela4d0c6b62023-03-01 23:15:59 +010019#include "qapi/qapi-commands-migration.h"
Juan Quintela9c894df2023-03-02 11:00:12 +010020#include "qapi/qapi-visit-migration.h"
Juan Quintelaf80196b2023-03-01 23:25:44 +010021#include "qapi/qmp/qerror.h"
Juan Quintela09d6c962023-03-02 10:13:16 +010022#include "qapi/qmp/qnull.h"
Juan Quintela77608702023-03-01 23:05:53 +010023#include "sysemu/runstate.h"
Juan Quintela09d6c962023-03-02 10:13:16 +010024#include "migration/colo.h"
Juan Quintela2682c4e2023-03-02 01:17:23 +010025#include "migration/misc.h"
Juan Quintela1f0776f2023-03-01 21:18:45 +010026#include "migration.h"
Juan Quintelae1fde0e2023-05-15 21:56:58 +020027#include "migration-stats.h"
Juan Quintela09d6c962023-03-02 10:13:16 +010028#include "qemu-file.h"
Juan Quintela77608702023-03-01 23:05:53 +010029#include "ram.h"
Juan Quintela1f0776f2023-03-01 21:18:45 +010030#include "options.h"
Hyman Huang(黄勇)dc623952023-06-07 23:30:50 +080031#include "sysemu/kvm.h"
Juan Quintela1f0776f2023-03-01 21:18:45 +010032
Juan Quintela09d6c962023-03-02 10:13:16 +010033/* Maximum migrate downtime set to 2000 seconds */
34#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
35#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
36
Juan Quintelaf9436522023-03-02 12:55:57 +010037#define MAX_THROTTLE (128 << 20) /* Migration transfer speed throttling */
38
39/* Time in milliseconds we are allowed to stop the source,
40 * for sending the last part */
41#define DEFAULT_MIGRATE_SET_DOWNTIME 300
42
Juan Quintelaf9436522023-03-02 12:55:57 +010043/* Define default autoconverge cpu throttle migration parameters */
44#define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50
45#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
46#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
47#define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
48
49/* Migration XBZRLE default cache size */
50#define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
51
52/* The delay time (in ms) between two COLO checkpoints */
53#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
54#define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
55#define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
56/* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
57#define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
58/* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
59#define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
60
61/* Background transfer rate for postcopy, 0 means unlimited, note
62 * that page requests can still exceed this limit.
63 */
64#define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
65
66/*
67 * Parameters for self_announce_delay giving a stream of RARP/ARP
68 * packets after migration.
69 */
70#define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50
71#define DEFAULT_MIGRATE_ANNOUNCE_MAX 550
72#define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5
73#define DEFAULT_MIGRATE_ANNOUNCE_STEP 100
74
75#define DEFINE_PROP_MIG_CAP(name, x) \
76 DEFINE_PROP_BOOL(name, MigrationState, capabilities[x], false)
77
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +080078#define DEFAULT_MIGRATE_VCPU_DIRTY_LIMIT_PERIOD 1000 /* milliseconds */
Hyman Huang(黄勇)09f9ec92023-06-07 22:58:32 +080079#define DEFAULT_MIGRATE_VCPU_DIRTY_LIMIT 1 /* MB/s */
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +080080
Juan Quintelaf9436522023-03-02 12:55:57 +010081Property migration_properties[] = {
82 DEFINE_PROP_BOOL("store-global-state", MigrationState,
83 store_global_state, true),
84 DEFINE_PROP_BOOL("send-configuration", MigrationState,
85 send_configuration, true),
86 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
87 send_section_footer, true),
Juan Quintela77c259a2022-06-21 12:13:14 +020088 DEFINE_PROP_BOOL("multifd-flush-after-each-section", MigrationState,
Juan Quintela294e5a42022-06-21 13:36:11 +020089 multifd_flush_after_each_section, false),
Juan Quintelaf9436522023-03-02 12:55:57 +010090 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
91 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
92 DEFINE_PROP_BOOL("x-preempt-pre-7-2", MigrationState,
93 preempt_pre_7_2, false),
94
95 /* Migration parameters */
Juan Quintelaf9436522023-03-02 12:55:57 +010096 DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState,
97 parameters.throttle_trigger_threshold,
98 DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
99 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
100 parameters.cpu_throttle_initial,
101 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
102 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
103 parameters.cpu_throttle_increment,
104 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
105 DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
106 parameters.cpu_throttle_tailslow, false),
107 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
108 parameters.max_bandwidth, MAX_THROTTLE),
Peter Xu8b239592023-10-10 18:19:22 -0400109 DEFINE_PROP_SIZE("avail-switchover-bandwidth", MigrationState,
110 parameters.avail_switchover_bandwidth, 0),
Juan Quintelaf9436522023-03-02 12:55:57 +0100111 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
112 parameters.downtime_limit,
113 DEFAULT_MIGRATE_SET_DOWNTIME),
114 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
115 parameters.x_checkpoint_delay,
116 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
117 DEFINE_PROP_UINT8("multifd-channels", MigrationState,
118 parameters.multifd_channels,
119 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
120 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
121 parameters.multifd_compression,
122 DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
123 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
124 parameters.multifd_zlib_level,
125 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
126 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
127 parameters.multifd_zstd_level,
128 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
129 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
130 parameters.xbzrle_cache_size,
131 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
132 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
133 parameters.max_postcopy_bandwidth,
134 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
135 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
136 parameters.max_cpu_throttle,
137 DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
138 DEFINE_PROP_SIZE("announce-initial", MigrationState,
139 parameters.announce_initial,
140 DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
141 DEFINE_PROP_SIZE("announce-max", MigrationState,
142 parameters.announce_max,
143 DEFAULT_MIGRATE_ANNOUNCE_MAX),
144 DEFINE_PROP_SIZE("announce-rounds", MigrationState,
145 parameters.announce_rounds,
146 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
147 DEFINE_PROP_SIZE("announce-step", MigrationState,
148 parameters.announce_step,
149 DEFAULT_MIGRATE_ANNOUNCE_STEP),
150 DEFINE_PROP_STRING("tls-creds", MigrationState, parameters.tls_creds),
151 DEFINE_PROP_STRING("tls-hostname", MigrationState, parameters.tls_hostname),
152 DEFINE_PROP_STRING("tls-authz", MigrationState, parameters.tls_authz),
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +0800153 DEFINE_PROP_UINT64("x-vcpu-dirty-limit-period", MigrationState,
154 parameters.x_vcpu_dirty_limit_period,
155 DEFAULT_MIGRATE_VCPU_DIRTY_LIMIT_PERIOD),
Hyman Huang(黄勇)09f9ec92023-06-07 22:58:32 +0800156 DEFINE_PROP_UINT64("vcpu-dirty-limit", MigrationState,
157 parameters.vcpu_dirty_limit,
158 DEFAULT_MIGRATE_VCPU_DIRTY_LIMIT),
Steve Sistareeea1e5c2023-10-25 12:44:24 -0700159 DEFINE_PROP_MIG_MODE("mode", MigrationState,
160 parameters.mode,
161 MIG_MODE_NORMAL),
Hao Xiang5fdbb1d2024-03-11 18:00:11 +0000162 DEFINE_PROP_ZERO_PAGE_DETECTION("zero-page-detection", MigrationState,
163 parameters.zero_page_detection,
Hao Xiang70c25c92024-03-11 18:00:14 +0000164 ZERO_PAGE_DETECTION_MULTIFD),
Juan Quintelaf9436522023-03-02 12:55:57 +0100165
166 /* Migration capabilities */
167 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
168 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
169 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
170 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
Juan Quintelaf9436522023-03-02 12:55:57 +0100171 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
172 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
173 DEFINE_PROP_MIG_CAP("x-postcopy-preempt",
174 MIGRATION_CAPABILITY_POSTCOPY_PREEMPT),
175 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
176 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
Juan Quintelaf9436522023-03-02 12:55:57 +0100177 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
178 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
179 DEFINE_PROP_MIG_CAP("x-background-snapshot",
180 MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT),
181#ifdef CONFIG_LINUX
182 DEFINE_PROP_MIG_CAP("x-zero-copy-send",
183 MIGRATION_CAPABILITY_ZERO_COPY_SEND),
184#endif
Avihai Horon65742322023-06-21 14:11:54 +0300185 DEFINE_PROP_MIG_CAP("x-switchover-ack",
186 MIGRATION_CAPABILITY_SWITCHOVER_ACK),
Hyman Huang(黄勇)dc623952023-06-07 23:30:50 +0800187 DEFINE_PROP_MIG_CAP("x-dirty-limit", MIGRATION_CAPABILITY_DIRTY_LIMIT),
Fabiano Rosas4ed49fe2024-02-29 12:30:01 -0300188 DEFINE_PROP_MIG_CAP("mapped-ram", MIGRATION_CAPABILITY_MAPPED_RAM),
Juan Quintelaf9436522023-03-02 12:55:57 +0100189 DEFINE_PROP_END_OF_LIST(),
190};
191
Juan Quintela1f0776f2023-03-01 21:18:45 +0100192bool migrate_auto_converge(void)
193{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100194 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100195
196 return s->capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
197}
198
199bool migrate_background_snapshot(void)
200{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100201 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100202
203 return s->capabilities[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT];
204}
205
Juan Quintela5e804642023-03-01 22:00:16 +0100206bool migrate_colo(void)
207{
208 MigrationState *s = migrate_get_current();
Juan Quintela8f9c5322023-03-01 23:11:08 +0100209
Juan Quintela5e804642023-03-01 22:00:16 +0100210 return s->capabilities[MIGRATION_CAPABILITY_X_COLO];
211}
212
Juan Quintela1f0776f2023-03-01 21:18:45 +0100213bool migrate_dirty_bitmaps(void)
214{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100215 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100216
217 return s->capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
218}
219
Hyman Huang(黄勇)dc623952023-06-07 23:30:50 +0800220bool migrate_dirty_limit(void)
221{
222 MigrationState *s = migrate_get_current();
223
224 return s->capabilities[MIGRATION_CAPABILITY_DIRTY_LIMIT];
225}
226
Juan Quintelab8909022023-03-01 22:08:09 +0100227bool migrate_events(void)
228{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100229 MigrationState *s = migrate_get_current();
Juan Quintelab8909022023-03-01 22:08:09 +0100230
231 return s->capabilities[MIGRATION_CAPABILITY_EVENTS];
232}
233
Fabiano Rosas4ed49fe2024-02-29 12:30:01 -0300234bool migrate_mapped_ram(void)
235{
236 MigrationState *s = migrate_get_current();
237
238 return s->capabilities[MIGRATION_CAPABILITY_MAPPED_RAM];
239}
240
Juan Quintela1f0776f2023-03-01 21:18:45 +0100241bool migrate_ignore_shared(void)
242{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100243 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100244
245 return s->capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
246}
247
248bool migrate_late_block_activate(void)
249{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100250 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100251
252 return s->capabilities[MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
253}
254
Juan Quintela51b07542023-03-01 22:10:29 +0100255bool migrate_multifd(void)
256{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100257 MigrationState *s = migrate_get_current();
Juan Quintela51b07542023-03-01 22:10:29 +0100258
259 return s->capabilities[MIGRATION_CAPABILITY_MULTIFD];
260}
261
Juan Quintela1f0776f2023-03-01 21:18:45 +0100262bool migrate_pause_before_switchover(void)
263{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100264 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100265
266 return s->capabilities[MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
267}
268
269bool migrate_postcopy_blocktime(void)
270{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100271 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100272
273 return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
274}
275
276bool migrate_postcopy_preempt(void)
277{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100278 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100279
280 return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT];
281}
282
283bool migrate_postcopy_ram(void)
284{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100285 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100286
287 return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
288}
289
Juan Quintela17cba692023-03-01 22:41:55 +0100290bool migrate_rdma_pin_all(void)
291{
292 MigrationState *s = migrate_get_current();
293
294 return s->capabilities[MIGRATION_CAPABILITY_RDMA_PIN_ALL];
295}
296
Juan Quintela1f0776f2023-03-01 21:18:45 +0100297bool migrate_release_ram(void)
298{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100299 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100300
301 return s->capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
302}
303
Juan Quintela38ad1112023-03-01 22:25:47 +0100304bool migrate_return_path(void)
305{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100306 MigrationState *s = migrate_get_current();
Juan Quintela38ad1112023-03-01 22:25:47 +0100307
308 return s->capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
309}
310
Avihai Horon65742322023-06-21 14:11:54 +0300311bool migrate_switchover_ack(void)
312{
313 MigrationState *s = migrate_get_current();
314
315 return s->capabilities[MIGRATION_CAPABILITY_SWITCHOVER_ACK];
316}
317
Juan Quintela1f0776f2023-03-01 21:18:45 +0100318bool migrate_validate_uuid(void)
319{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100320 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100321
322 return s->capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
323}
324
Juan Quintela87dca0c2023-03-01 22:20:13 +0100325bool migrate_xbzrle(void)
326{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100327 MigrationState *s = migrate_get_current();
Juan Quintela87dca0c2023-03-01 22:20:13 +0100328
329 return s->capabilities[MIGRATION_CAPABILITY_XBZRLE];
330}
331
Juan Quintela1f0776f2023-03-01 21:18:45 +0100332bool migrate_zero_blocks(void)
333{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100334 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100335
336 return s->capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
337}
Juan Quintelab4bc3422023-03-01 22:17:14 +0100338
339bool migrate_zero_copy_send(void)
340{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100341 MigrationState *s = migrate_get_current();
Juan Quintelab4bc3422023-03-01 22:17:14 +0100342
343 return s->capabilities[MIGRATION_CAPABILITY_ZERO_COPY_SEND];
344}
Juan Quintelaf774fde2023-03-02 10:44:20 +0100345
346/* pseudo capabilities */
347
Juan Quintela77c259a2022-06-21 12:13:14 +0200348bool migrate_multifd_flush_after_each_section(void)
349{
350 MigrationState *s = migrate_get_current();
351
Juan Quintela294e5a42022-06-21 13:36:11 +0200352 return s->multifd_flush_after_each_section;
Juan Quintela77c259a2022-06-21 12:13:14 +0200353}
354
Juan Quintelaf774fde2023-03-02 10:44:20 +0100355bool migrate_postcopy(void)
356{
357 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
358}
359
Juan Quintela27fd25b2023-10-11 22:35:15 +0200360bool migrate_rdma(void)
361{
362 MigrationState *s = migrate_get_current();
363
364 return s->rdma_migration;
365}
366
Juan Quintela10d47032023-03-02 10:41:23 +0100367bool migrate_tls(void)
368{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100369 MigrationState *s = migrate_get_current();
Juan Quintela10d47032023-03-02 10:41:23 +0100370
371 return s->parameters.tls_creds && *s->parameters.tls_creds;
372}
373
Juan Quintela77608702023-03-01 23:05:53 +0100374typedef enum WriteTrackingSupport {
375 WT_SUPPORT_UNKNOWN = 0,
376 WT_SUPPORT_ABSENT,
377 WT_SUPPORT_AVAILABLE,
378 WT_SUPPORT_COMPATIBLE
379} WriteTrackingSupport;
380
381static
382WriteTrackingSupport migrate_query_write_tracking(void)
383{
384 /* Check if kernel supports required UFFD features */
385 if (!ram_write_tracking_available()) {
386 return WT_SUPPORT_ABSENT;
387 }
388 /*
389 * Check if current memory configuration is
390 * compatible with required UFFD features.
391 */
392 if (!ram_write_tracking_compatible()) {
393 return WT_SUPPORT_AVAILABLE;
394 }
395
396 return WT_SUPPORT_COMPATIBLE;
397}
398
399/* Migration capabilities set */
400struct MigrateCapsSet {
401 int size; /* Capability set size */
402 MigrationCapability caps[]; /* Variadic array of capabilities */
403};
404typedef struct MigrateCapsSet MigrateCapsSet;
405
406/* Define and initialize MigrateCapsSet */
407#define INITIALIZE_MIGRATE_CAPS_SET(_name, ...) \
408 MigrateCapsSet _name = { \
409 .size = sizeof((int []) { __VA_ARGS__ }) / sizeof(int), \
410 .caps = { __VA_ARGS__ } \
411 }
412
413/* Background-snapshot compatibility check list */
414static const
415INITIALIZE_MIGRATE_CAPS_SET(check_caps_background_snapshot,
416 MIGRATION_CAPABILITY_POSTCOPY_RAM,
417 MIGRATION_CAPABILITY_DIRTY_BITMAPS,
418 MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME,
419 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE,
420 MIGRATION_CAPABILITY_RETURN_PATH,
421 MIGRATION_CAPABILITY_MULTIFD,
422 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER,
423 MIGRATION_CAPABILITY_AUTO_CONVERGE,
424 MIGRATION_CAPABILITY_RELEASE_RAM,
425 MIGRATION_CAPABILITY_RDMA_PIN_ALL,
Juan Quintela77608702023-03-01 23:05:53 +0100426 MIGRATION_CAPABILITY_XBZRLE,
427 MIGRATION_CAPABILITY_X_COLO,
428 MIGRATION_CAPABILITY_VALIDATE_UUID,
429 MIGRATION_CAPABILITY_ZERO_COPY_SEND);
430
Wei Wang82137e62023-06-06 18:19:09 +0800431static bool migrate_incoming_started(void)
432{
433 return !!migration_incoming_get_current()->transport_data;
434}
435
Juan Quintela77608702023-03-01 23:05:53 +0100436/**
437 * @migration_caps_check - check capability compatibility
438 *
439 * @old_caps: old capability list
440 * @new_caps: new capability list
441 * @errp: set *errp if the check failed, with reason
442 *
443 * Returns true if check passed, otherwise false.
444 */
445bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
446{
Zhao Liu46ff64a2024-03-12 14:03:37 +0800447 ERRP_GUARD();
Juan Quintela77608702023-03-01 23:05:53 +0100448 MigrationIncomingState *mis = migration_incoming_get_current();
449
Juan Quintela77608702023-03-01 23:05:53 +0100450#ifndef CONFIG_REPLICATION
451 if (new_caps[MIGRATION_CAPABILITY_X_COLO]) {
452 error_setg(errp, "QEMU compiled without replication module"
453 " can't enable COLO");
454 error_append_hint(errp, "Please enable replication before COLO.\n");
455 return false;
456 }
457#endif
458
459 if (new_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
460 /* This check is reasonably expensive, so only when it's being
461 * set the first time, also it's only the destination that needs
462 * special support.
463 */
464 if (!old_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM] &&
465 runstate_check(RUN_STATE_INMIGRATE) &&
Peter Xu74c38cf2023-04-25 21:15:14 -0400466 !postcopy_ram_supported_by_host(mis, errp)) {
467 error_prepend(errp, "Postcopy is not supported: ");
Juan Quintela77608702023-03-01 23:05:53 +0100468 return false;
469 }
470
471 if (new_caps[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
472 error_setg(errp, "Postcopy is not compatible with ignore-shared");
473 return false;
474 }
Leonardo Brasb405dff2023-04-25 00:33:08 -0300475
476 if (new_caps[MIGRATION_CAPABILITY_MULTIFD]) {
477 error_setg(errp, "Postcopy is not yet compatible with multifd");
478 return false;
479 }
Juan Quintela77608702023-03-01 23:05:53 +0100480 }
481
482 if (new_caps[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT]) {
483 WriteTrackingSupport wt_support;
484 int idx;
485 /*
486 * Check if 'background-snapshot' capability is supported by
487 * host kernel and compatible with guest memory configuration.
488 */
489 wt_support = migrate_query_write_tracking();
490 if (wt_support < WT_SUPPORT_AVAILABLE) {
491 error_setg(errp, "Background-snapshot is not supported by host kernel");
492 return false;
493 }
494 if (wt_support < WT_SUPPORT_COMPATIBLE) {
495 error_setg(errp, "Background-snapshot is not compatible "
496 "with guest memory configuration");
497 return false;
498 }
499
500 /*
501 * Check if there are any migration capabilities
502 * incompatible with 'background-snapshot'.
503 */
504 for (idx = 0; idx < check_caps_background_snapshot.size; idx++) {
505 int incomp_cap = check_caps_background_snapshot.caps[idx];
506 if (new_caps[incomp_cap]) {
507 error_setg(errp,
508 "Background-snapshot is not compatible with %s",
509 MigrationCapability_str(incomp_cap));
510 return false;
511 }
512 }
513 }
514
515#ifdef CONFIG_LINUX
516 if (new_caps[MIGRATION_CAPABILITY_ZERO_COPY_SEND] &&
517 (!new_caps[MIGRATION_CAPABILITY_MULTIFD] ||
Juan Quintela77608702023-03-01 23:05:53 +0100518 new_caps[MIGRATION_CAPABILITY_XBZRLE] ||
519 migrate_multifd_compression() ||
Juan Quintela10d47032023-03-02 10:41:23 +0100520 migrate_tls())) {
Juan Quintela77608702023-03-01 23:05:53 +0100521 error_setg(errp,
522 "Zero copy only available for non-compressed non-TLS multifd migration");
523 return false;
524 }
525#else
526 if (new_caps[MIGRATION_CAPABILITY_ZERO_COPY_SEND]) {
527 error_setg(errp,
528 "Zero copy currently only available on Linux");
529 return false;
530 }
531#endif
532
533 if (new_caps[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT]) {
534 if (!new_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
535 error_setg(errp, "Postcopy preempt requires postcopy-ram");
536 return false;
537 }
538
Wei Wang82137e62023-06-06 18:19:09 +0800539 if (migrate_incoming_started()) {
540 error_setg(errp,
541 "Postcopy preempt must be set before incoming starts");
542 return false;
543 }
Juan Quintela77608702023-03-01 23:05:53 +0100544 }
545
546 if (new_caps[MIGRATION_CAPABILITY_MULTIFD]) {
Wei Wang82137e62023-06-06 18:19:09 +0800547 if (migrate_incoming_started()) {
548 error_setg(errp, "Multifd must be set before incoming starts");
549 return false;
550 }
Juan Quintela77608702023-03-01 23:05:53 +0100551 }
552
Avihai Horon65742322023-06-21 14:11:54 +0300553 if (new_caps[MIGRATION_CAPABILITY_SWITCHOVER_ACK]) {
554 if (!new_caps[MIGRATION_CAPABILITY_RETURN_PATH]) {
555 error_setg(errp, "Capability 'switchover-ack' requires capability "
556 "'return-path'");
557 return false;
558 }
Avihai Horon65742322023-06-21 14:11:54 +0300559 }
Hyman Huang(黄勇)dc623952023-06-07 23:30:50 +0800560 if (new_caps[MIGRATION_CAPABILITY_DIRTY_LIMIT]) {
561 if (new_caps[MIGRATION_CAPABILITY_AUTO_CONVERGE]) {
562 error_setg(errp, "dirty-limit conflicts with auto-converge"
563 " either of then available currently");
564 return false;
565 }
566
567 if (!kvm_enabled() || !kvm_dirty_ring_enabled()) {
568 error_setg(errp, "dirty-limit requires KVM with accelerator"
569 " property 'dirty-ring-size' set");
570 return false;
571 }
572 }
Avihai Horon65742322023-06-21 14:11:54 +0300573
Juan Quintelad869f622023-10-19 13:07:14 +0200574 if (new_caps[MIGRATION_CAPABILITY_MULTIFD]) {
575 if (new_caps[MIGRATION_CAPABILITY_XBZRLE]) {
576 error_setg(errp, "Multifd is not compatible with xbzrle");
577 return false;
578 }
579 }
580
Fabiano Rosas4ed49fe2024-02-29 12:30:01 -0300581 if (new_caps[MIGRATION_CAPABILITY_MAPPED_RAM]) {
Fabiano Rosas4ed49fe2024-02-29 12:30:01 -0300582 if (new_caps[MIGRATION_CAPABILITY_XBZRLE]) {
583 error_setg(errp,
584 "Mapped-ram migration is incompatible with xbzrle");
585 return false;
586 }
587
Fabiano Rosas4ed49fe2024-02-29 12:30:01 -0300588 if (new_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
589 error_setg(errp,
590 "Mapped-ram migration is incompatible with postcopy");
591 return false;
592 }
593 }
594
Juan Quintela77608702023-03-01 23:05:53 +0100595 return true;
596}
Juan Quintela4d0c6b62023-03-01 23:15:59 +0100597
Juan Quintelaf80196b2023-03-01 23:25:44 +0100598bool migrate_cap_set(int cap, bool value, Error **errp)
599{
600 MigrationState *s = migrate_get_current();
601 bool new_caps[MIGRATION_CAPABILITY__MAX];
602
Steve Sistareaeaafb12024-03-11 10:48:51 -0700603 if (migration_is_running()) {
Philippe Mathieu-Daudé00580782024-03-12 15:13:42 +0100604 error_setg(errp, "There's a migration process in progress");
Juan Quintelaf80196b2023-03-01 23:25:44 +0100605 return false;
606 }
607
608 memcpy(new_caps, s->capabilities, sizeof(new_caps));
609 new_caps[cap] = value;
610
611 if (!migrate_caps_check(s->capabilities, new_caps, errp)) {
612 return false;
613 }
614 s->capabilities[cap] = value;
615 return true;
616}
617
Juan Quintela4d0c6b62023-03-01 23:15:59 +0100618MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
619{
620 MigrationCapabilityStatusList *head = NULL, **tail = &head;
621 MigrationCapabilityStatus *caps;
622 MigrationState *s = migrate_get_current();
623 int i;
624
625 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Juan Quintela4d0c6b62023-03-01 23:15:59 +0100626 caps = g_malloc0(sizeof(*caps));
627 caps->capability = i;
628 caps->state = s->capabilities[i];
629 QAPI_LIST_APPEND(tail, caps);
630 }
631
632 return head;
633}
Juan Quintela45c1de12023-03-01 23:18:02 +0100634
635void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
636 Error **errp)
637{
638 MigrationState *s = migrate_get_current();
639 MigrationCapabilityStatusList *cap;
640 bool new_caps[MIGRATION_CAPABILITY__MAX];
641
Steve Sistareaeaafb12024-03-11 10:48:51 -0700642 if (migration_is_running() || migration_in_colo_state()) {
Philippe Mathieu-Daudé00580782024-03-12 15:13:42 +0100643 error_setg(errp, "There's a migration process in progress");
Juan Quintela45c1de12023-03-01 23:18:02 +0100644 return;
645 }
646
647 memcpy(new_caps, s->capabilities, sizeof(new_caps));
648 for (cap = params; cap; cap = cap->next) {
649 new_caps[cap->value->capability] = cap->value->state;
650 }
651
652 if (!migrate_caps_check(s->capabilities, new_caps, errp)) {
653 return;
654 }
655
656 for (cap = params; cap; cap = cap->next) {
657 s->capabilities[cap->value->capability] = cap->value->state;
658 }
659}
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100660
661/* parameters */
662
Juan Quintelab804b352023-03-02 12:49:16 +0100663const BitmapMigrationNodeAliasList *migrate_block_bitmap_mapping(void)
664{
665 MigrationState *s = migrate_get_current();
666
667 return s->parameters.block_bitmap_mapping;
668}
669
Juan Quintela3cba22c2023-05-03 20:10:36 +0200670bool migrate_has_block_bitmap_mapping(void)
671{
672 MigrationState *s = migrate_get_current();
673
674 return s->parameters.has_block_bitmap_mapping;
675}
676
Juan Quintelaf94a8582023-03-02 01:04:55 +0100677uint32_t migrate_checkpoint_delay(void)
678{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100679 MigrationState *s = migrate_get_current();
Juan Quintelaf94a8582023-03-02 01:04:55 +0100680
681 return s->parameters.x_checkpoint_delay;
682}
683
Juan Quintela9605c2a2023-03-02 10:20:49 +0100684uint8_t migrate_cpu_throttle_increment(void)
685{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100686 MigrationState *s = migrate_get_current();
Juan Quintela9605c2a2023-03-02 10:20:49 +0100687
688 return s->parameters.cpu_throttle_increment;
689}
690
Juan Quintela2a8ec382023-03-02 01:22:44 +0100691uint8_t migrate_cpu_throttle_initial(void)
692{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100693 MigrationState *s = migrate_get_current();
Juan Quintela2a8ec382023-03-02 01:22:44 +0100694
695 return s->parameters.cpu_throttle_initial;
696}
697
Juan Quintela873f6742023-03-02 10:29:51 +0100698bool migrate_cpu_throttle_tailslow(void)
699{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100700 MigrationState *s = migrate_get_current();
Juan Quintela873f6742023-03-02 10:29:51 +0100701
702 return s->parameters.cpu_throttle_tailslow;
703}
704
Juan Quintelaf5da8ba2023-03-02 12:00:43 +0100705uint64_t migrate_downtime_limit(void)
706{
707 MigrationState *s = migrate_get_current();
708
709 return s->parameters.downtime_limit;
710}
711
Juan Quintela24155bd2023-03-02 01:13:01 +0100712uint8_t migrate_max_cpu_throttle(void)
713{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100714 MigrationState *s = migrate_get_current();
Juan Quintela24155bd2023-03-02 01:13:01 +0100715
716 return s->parameters.max_cpu_throttle;
717}
718
Juan Quintela9c894df2023-03-02 11:00:12 +0100719uint64_t migrate_max_bandwidth(void)
720{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100721 MigrationState *s = migrate_get_current();
Juan Quintela9c894df2023-03-02 11:00:12 +0100722
723 return s->parameters.max_bandwidth;
724}
725
Peter Xu8b239592023-10-10 18:19:22 -0400726uint64_t migrate_avail_switchover_bandwidth(void)
727{
728 MigrationState *s = migrate_get_current();
729
730 return s->parameters.avail_switchover_bandwidth;
731}
732
Juan Quintela52033342023-05-04 13:38:33 +0200733uint64_t migrate_max_postcopy_bandwidth(void)
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100734{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100735 MigrationState *s = migrate_get_current();
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100736
737 return s->parameters.max_postcopy_bandwidth;
738}
739
Steve Sistareeea1e5c2023-10-25 12:44:24 -0700740MigMode migrate_mode(void)
741{
742 MigrationState *s = migrate_get_current();
Steve Sistareb12635f2023-11-13 12:23:45 -0800743 MigMode mode = s->parameters.mode;
Steve Sistareeea1e5c2023-10-25 12:44:24 -0700744
Steve Sistareb12635f2023-11-13 12:23:45 -0800745 assert(mode >= 0 && mode < MIG_MODE__MAX);
746 return mode;
Steve Sistareeea1e5c2023-10-25 12:44:24 -0700747}
748
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100749int migrate_multifd_channels(void)
750{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100751 MigrationState *s = migrate_get_current();
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100752
753 return s->parameters.multifd_channels;
754}
755
756MultiFDCompression migrate_multifd_compression(void)
757{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100758 MigrationState *s = migrate_get_current();
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100759
760 assert(s->parameters.multifd_compression < MULTIFD_COMPRESSION__MAX);
761 return s->parameters.multifd_compression;
762}
763
764int migrate_multifd_zlib_level(void)
765{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100766 MigrationState *s = migrate_get_current();
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100767
768 return s->parameters.multifd_zlib_level;
769}
770
771int migrate_multifd_zstd_level(void)
772{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100773 MigrationState *s = migrate_get_current();
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100774
775 return s->parameters.multifd_zstd_level;
776}
777
Juan Quintela6499efd2023-03-02 00:59:13 +0100778uint8_t migrate_throttle_trigger_threshold(void)
779{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100780 MigrationState *s = migrate_get_current();
Juan Quintela6499efd2023-03-02 00:59:13 +0100781
782 return s->parameters.throttle_trigger_threshold;
783}
784
Juan Quintela2eb03082023-03-02 12:33:50 +0100785const char *migrate_tls_authz(void)
786{
787 MigrationState *s = migrate_get_current();
788
789 return s->parameters.tls_authz;
790}
791
Juan Quintelad5c3e192023-03-02 12:17:06 +0100792const char *migrate_tls_creds(void)
793{
794 MigrationState *s = migrate_get_current();
795
796 return s->parameters.tls_creds;
797}
798
Juan Quintela1f2f3662023-03-02 12:37:21 +0100799const char *migrate_tls_hostname(void)
800{
801 MigrationState *s = migrate_get_current();
802
803 return s->parameters.tls_hostname;
804}
805
Steve Sistare714f3312024-03-11 10:48:52 -0700806uint64_t migrate_vcpu_dirty_limit_period(void)
807{
808 MigrationState *s = migrate_get_current();
809
810 return s->parameters.x_vcpu_dirty_limit_period;
811}
812
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100813uint64_t migrate_xbzrle_cache_size(void)
814{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100815 MigrationState *s = migrate_get_current();
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100816
817 return s->parameters.xbzrle_cache_size;
818}
Juan Quintela2682c4e2023-03-02 01:17:23 +0100819
Hao Xiang5fdbb1d2024-03-11 18:00:11 +0000820ZeroPageDetection migrate_zero_page_detection(void)
821{
822 MigrationState *s = migrate_get_current();
823
824 return s->parameters.zero_page_detection;
825}
826
Juan Quintela2682c4e2023-03-02 01:17:23 +0100827/* parameters helpers */
828
829AnnounceParameters *migrate_announce_params(void)
830{
831 static AnnounceParameters ap;
832
833 MigrationState *s = migrate_get_current();
834
835 ap.initial = s->parameters.announce_initial;
836 ap.max = s->parameters.announce_max;
837 ap.rounds = s->parameters.announce_rounds;
838 ap.step = s->parameters.announce_step;
839
840 return &ap;
841}
Juan Quintela9c894df2023-03-02 11:00:12 +0100842
843MigrationParameters *qmp_query_migrate_parameters(Error **errp)
844{
845 MigrationParameters *params;
846 MigrationState *s = migrate_get_current();
847
848 /* TODO use QAPI_CLONE() instead of duplicating it inline */
849 params = g_malloc0(sizeof(*params));
Juan Quintela9c894df2023-03-02 11:00:12 +0100850 params->has_throttle_trigger_threshold = true;
851 params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
852 params->has_cpu_throttle_initial = true;
853 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
854 params->has_cpu_throttle_increment = true;
855 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
856 params->has_cpu_throttle_tailslow = true;
857 params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
858 params->tls_creds = g_strdup(s->parameters.tls_creds);
859 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
860 params->tls_authz = g_strdup(s->parameters.tls_authz ?
861 s->parameters.tls_authz : "");
862 params->has_max_bandwidth = true;
863 params->max_bandwidth = s->parameters.max_bandwidth;
Peter Xu8b239592023-10-10 18:19:22 -0400864 params->has_avail_switchover_bandwidth = true;
865 params->avail_switchover_bandwidth = s->parameters.avail_switchover_bandwidth;
Juan Quintela9c894df2023-03-02 11:00:12 +0100866 params->has_downtime_limit = true;
867 params->downtime_limit = s->parameters.downtime_limit;
868 params->has_x_checkpoint_delay = true;
869 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
Juan Quintela9c894df2023-03-02 11:00:12 +0100870 params->has_multifd_channels = true;
871 params->multifd_channels = s->parameters.multifd_channels;
872 params->has_multifd_compression = true;
873 params->multifd_compression = s->parameters.multifd_compression;
874 params->has_multifd_zlib_level = true;
875 params->multifd_zlib_level = s->parameters.multifd_zlib_level;
876 params->has_multifd_zstd_level = true;
877 params->multifd_zstd_level = s->parameters.multifd_zstd_level;
878 params->has_xbzrle_cache_size = true;
879 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
880 params->has_max_postcopy_bandwidth = true;
881 params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
882 params->has_max_cpu_throttle = true;
883 params->max_cpu_throttle = s->parameters.max_cpu_throttle;
884 params->has_announce_initial = true;
885 params->announce_initial = s->parameters.announce_initial;
886 params->has_announce_max = true;
887 params->announce_max = s->parameters.announce_max;
888 params->has_announce_rounds = true;
889 params->announce_rounds = s->parameters.announce_rounds;
890 params->has_announce_step = true;
891 params->announce_step = s->parameters.announce_step;
892
893 if (s->parameters.has_block_bitmap_mapping) {
894 params->has_block_bitmap_mapping = true;
895 params->block_bitmap_mapping =
896 QAPI_CLONE(BitmapMigrationNodeAliasList,
897 s->parameters.block_bitmap_mapping);
898 }
899
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +0800900 params->has_x_vcpu_dirty_limit_period = true;
901 params->x_vcpu_dirty_limit_period = s->parameters.x_vcpu_dirty_limit_period;
Hyman Huang(黄勇)09f9ec92023-06-07 22:58:32 +0800902 params->has_vcpu_dirty_limit = true;
903 params->vcpu_dirty_limit = s->parameters.vcpu_dirty_limit;
Steve Sistareeea1e5c2023-10-25 12:44:24 -0700904 params->has_mode = true;
905 params->mode = s->parameters.mode;
Hao Xiang5fdbb1d2024-03-11 18:00:11 +0000906 params->has_zero_page_detection = true;
907 params->zero_page_detection = s->parameters.zero_page_detection;
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +0800908
Juan Quintela9c894df2023-03-02 11:00:12 +0100909 return params;
910}
Juan Quintela09d6c962023-03-02 10:13:16 +0100911
Juan Quintela61a174e2023-03-02 10:35:24 +0100912void migrate_params_init(MigrationParameters *params)
913{
914 params->tls_hostname = g_strdup("");
915 params->tls_creds = g_strdup("");
916
917 /* Set has_* up only for parameter checks */
Juan Quintela61a174e2023-03-02 10:35:24 +0100918 params->has_throttle_trigger_threshold = true;
919 params->has_cpu_throttle_initial = true;
920 params->has_cpu_throttle_increment = true;
921 params->has_cpu_throttle_tailslow = true;
922 params->has_max_bandwidth = true;
923 params->has_downtime_limit = true;
924 params->has_x_checkpoint_delay = true;
Juan Quintela61a174e2023-03-02 10:35:24 +0100925 params->has_multifd_channels = true;
926 params->has_multifd_compression = true;
927 params->has_multifd_zlib_level = true;
928 params->has_multifd_zstd_level = true;
929 params->has_xbzrle_cache_size = true;
930 params->has_max_postcopy_bandwidth = true;
931 params->has_max_cpu_throttle = true;
932 params->has_announce_initial = true;
933 params->has_announce_max = true;
934 params->has_announce_rounds = true;
935 params->has_announce_step = true;
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +0800936 params->has_x_vcpu_dirty_limit_period = true;
Hyman Huang(黄勇)09f9ec92023-06-07 22:58:32 +0800937 params->has_vcpu_dirty_limit = true;
Steve Sistareeea1e5c2023-10-25 12:44:24 -0700938 params->has_mode = true;
Hao Xiang5fdbb1d2024-03-11 18:00:11 +0000939 params->has_zero_page_detection = true;
Juan Quintela61a174e2023-03-02 10:35:24 +0100940}
941
Juan Quintela09d6c962023-03-02 10:13:16 +0100942/*
943 * Check whether the parameters are valid. Error will be put into errp
944 * (if provided). Return true if valid, otherwise false.
945 */
946bool migrate_params_check(MigrationParameters *params, Error **errp)
947{
Zhao Liu35e83a92024-03-11 11:38:20 +0800948 ERRP_GUARD();
949
Juan Quintela09d6c962023-03-02 10:13:16 +0100950 if (params->has_throttle_trigger_threshold &&
951 (params->throttle_trigger_threshold < 1 ||
952 params->throttle_trigger_threshold > 100)) {
953 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
954 "throttle_trigger_threshold",
955 "an integer in the range of 1 to 100");
956 return false;
957 }
958
959 if (params->has_cpu_throttle_initial &&
960 (params->cpu_throttle_initial < 1 ||
961 params->cpu_throttle_initial > 99)) {
962 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
963 "cpu_throttle_initial",
964 "an integer in the range of 1 to 99");
965 return false;
966 }
967
968 if (params->has_cpu_throttle_increment &&
969 (params->cpu_throttle_increment < 1 ||
970 params->cpu_throttle_increment > 99)) {
971 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
972 "cpu_throttle_increment",
973 "an integer in the range of 1 to 99");
974 return false;
975 }
976
977 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
978 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
979 "max_bandwidth",
980 "an integer in the range of 0 to "stringify(SIZE_MAX)
981 " bytes/second");
982 return false;
983 }
984
Peter Xu8b239592023-10-10 18:19:22 -0400985 if (params->has_avail_switchover_bandwidth &&
986 (params->avail_switchover_bandwidth > SIZE_MAX)) {
987 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
988 "avail_switchover_bandwidth",
989 "an integer in the range of 0 to "stringify(SIZE_MAX)
990 " bytes/second");
991 return false;
992 }
993
Juan Quintela09d6c962023-03-02 10:13:16 +0100994 if (params->has_downtime_limit &&
995 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
996 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
997 "downtime_limit",
998 "an integer in the range of 0 to "
999 stringify(MAX_MIGRATE_DOWNTIME)" ms");
1000 return false;
1001 }
1002
1003 /* x_checkpoint_delay is now always positive */
1004
1005 if (params->has_multifd_channels && (params->multifd_channels < 1)) {
1006 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1007 "multifd_channels",
1008 "a value between 1 and 255");
1009 return false;
1010 }
1011
1012 if (params->has_multifd_zlib_level &&
1013 (params->multifd_zlib_level > 9)) {
1014 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
1015 "a value between 0 and 9");
1016 return false;
1017 }
1018
1019 if (params->has_multifd_zstd_level &&
1020 (params->multifd_zstd_level > 20)) {
1021 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
1022 "a value between 0 and 20");
1023 return false;
1024 }
1025
1026 if (params->has_xbzrle_cache_size &&
1027 (params->xbzrle_cache_size < qemu_target_page_size() ||
1028 !is_power_of_2(params->xbzrle_cache_size))) {
1029 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1030 "xbzrle_cache_size",
1031 "a power of two no less than the target page size");
1032 return false;
1033 }
1034
1035 if (params->has_max_cpu_throttle &&
1036 (params->max_cpu_throttle < params->cpu_throttle_initial ||
1037 params->max_cpu_throttle > 99)) {
1038 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1039 "max_cpu_throttle",
1040 "an integer in the range of cpu_throttle_initial to 99");
1041 return false;
1042 }
1043
1044 if (params->has_announce_initial &&
1045 params->announce_initial > 100000) {
1046 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1047 "announce_initial",
1048 "a value between 0 and 100000");
1049 return false;
1050 }
1051 if (params->has_announce_max &&
1052 params->announce_max > 100000) {
1053 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1054 "announce_max",
1055 "a value between 0 and 100000");
1056 return false;
1057 }
1058 if (params->has_announce_rounds &&
1059 params->announce_rounds > 1000) {
1060 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1061 "announce_rounds",
1062 "a value between 0 and 1000");
1063 return false;
1064 }
1065 if (params->has_announce_step &&
1066 (params->announce_step < 1 ||
1067 params->announce_step > 10000)) {
1068 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1069 "announce_step",
1070 "a value between 0 and 10000");
1071 return false;
1072 }
1073
1074 if (params->has_block_bitmap_mapping &&
1075 !check_dirty_bitmap_mig_alias_map(params->block_bitmap_mapping, errp)) {
1076 error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: ");
1077 return false;
1078 }
1079
1080#ifdef CONFIG_LINUX
1081 if (migrate_zero_copy_send() &&
1082 ((params->has_multifd_compression && params->multifd_compression) ||
1083 (params->tls_creds && *params->tls_creds))) {
1084 error_setg(errp,
1085 "Zero copy only available for non-compressed non-TLS multifd migration");
1086 return false;
1087 }
1088#endif
1089
Fabiano Rosasf427d902024-02-29 12:30:14 -03001090 if (migrate_mapped_ram() &&
1091 (migrate_multifd_compression() || migrate_tls())) {
1092 error_setg(errp,
1093 "Mapped-ram only available for non-compressed non-TLS multifd migration");
1094 return false;
1095 }
1096
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +08001097 if (params->has_x_vcpu_dirty_limit_period &&
1098 (params->x_vcpu_dirty_limit_period < 1 ||
1099 params->x_vcpu_dirty_limit_period > 1000)) {
1100 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1101 "x-vcpu-dirty-limit-period",
1102 "a value between 1 and 1000");
1103 return false;
1104 }
1105
Hyman Huang(黄勇)09f9ec92023-06-07 22:58:32 +08001106 if (params->has_vcpu_dirty_limit &&
1107 (params->vcpu_dirty_limit < 1)) {
Philippe Mathieu-Daudé45d19d92024-03-12 15:13:41 +01001108 error_setg(errp,
1109 "Parameter 'vcpu_dirty_limit' must be greater than 1 MB/s");
Hyman Huang(黄勇)09f9ec92023-06-07 22:58:32 +08001110 return false;
1111 }
1112
Juan Quintela09d6c962023-03-02 10:13:16 +01001113 return true;
1114}
1115
1116static void migrate_params_test_apply(MigrateSetParameters *params,
1117 MigrationParameters *dest)
1118{
1119 *dest = migrate_get_current()->parameters;
1120
1121 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1122
Juan Quintela09d6c962023-03-02 10:13:16 +01001123 if (params->has_throttle_trigger_threshold) {
1124 dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
1125 }
1126
1127 if (params->has_cpu_throttle_initial) {
1128 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1129 }
1130
1131 if (params->has_cpu_throttle_increment) {
1132 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1133 }
1134
1135 if (params->has_cpu_throttle_tailslow) {
1136 dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1137 }
1138
1139 if (params->tls_creds) {
1140 assert(params->tls_creds->type == QTYPE_QSTRING);
1141 dest->tls_creds = params->tls_creds->u.s;
1142 }
1143
1144 if (params->tls_hostname) {
1145 assert(params->tls_hostname->type == QTYPE_QSTRING);
1146 dest->tls_hostname = params->tls_hostname->u.s;
1147 }
1148
1149 if (params->has_max_bandwidth) {
1150 dest->max_bandwidth = params->max_bandwidth;
1151 }
1152
Peter Xu8b239592023-10-10 18:19:22 -04001153 if (params->has_avail_switchover_bandwidth) {
1154 dest->avail_switchover_bandwidth = params->avail_switchover_bandwidth;
1155 }
1156
Juan Quintela09d6c962023-03-02 10:13:16 +01001157 if (params->has_downtime_limit) {
1158 dest->downtime_limit = params->downtime_limit;
1159 }
1160
1161 if (params->has_x_checkpoint_delay) {
1162 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1163 }
1164
Juan Quintela09d6c962023-03-02 10:13:16 +01001165 if (params->has_multifd_channels) {
1166 dest->multifd_channels = params->multifd_channels;
1167 }
1168 if (params->has_multifd_compression) {
1169 dest->multifd_compression = params->multifd_compression;
1170 }
Bryan Zhangb4014a22024-03-01 03:59:00 +00001171 if (params->has_multifd_zlib_level) {
1172 dest->multifd_zlib_level = params->multifd_zlib_level;
1173 }
1174 if (params->has_multifd_zstd_level) {
1175 dest->multifd_zstd_level = params->multifd_zstd_level;
1176 }
Juan Quintela09d6c962023-03-02 10:13:16 +01001177 if (params->has_xbzrle_cache_size) {
1178 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1179 }
1180 if (params->has_max_postcopy_bandwidth) {
1181 dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1182 }
1183 if (params->has_max_cpu_throttle) {
1184 dest->max_cpu_throttle = params->max_cpu_throttle;
1185 }
1186 if (params->has_announce_initial) {
1187 dest->announce_initial = params->announce_initial;
1188 }
1189 if (params->has_announce_max) {
1190 dest->announce_max = params->announce_max;
1191 }
1192 if (params->has_announce_rounds) {
1193 dest->announce_rounds = params->announce_rounds;
1194 }
1195 if (params->has_announce_step) {
1196 dest->announce_step = params->announce_step;
1197 }
1198
1199 if (params->has_block_bitmap_mapping) {
1200 dest->has_block_bitmap_mapping = true;
1201 dest->block_bitmap_mapping = params->block_bitmap_mapping;
1202 }
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +08001203
1204 if (params->has_x_vcpu_dirty_limit_period) {
1205 dest->x_vcpu_dirty_limit_period =
1206 params->x_vcpu_dirty_limit_period;
1207 }
Hyman Huang(黄勇)09f9ec92023-06-07 22:58:32 +08001208 if (params->has_vcpu_dirty_limit) {
1209 dest->vcpu_dirty_limit = params->vcpu_dirty_limit;
1210 }
Steve Sistareeea1e5c2023-10-25 12:44:24 -07001211
1212 if (params->has_mode) {
1213 dest->mode = params->mode;
1214 }
Hao Xiang5fdbb1d2024-03-11 18:00:11 +00001215
1216 if (params->has_zero_page_detection) {
1217 dest->zero_page_detection = params->zero_page_detection;
1218 }
Juan Quintela09d6c962023-03-02 10:13:16 +01001219}
1220
1221static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
1222{
1223 MigrationState *s = migrate_get_current();
1224
1225 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1226
Juan Quintela09d6c962023-03-02 10:13:16 +01001227 if (params->has_throttle_trigger_threshold) {
1228 s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
1229 }
1230
1231 if (params->has_cpu_throttle_initial) {
1232 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
1233 }
1234
1235 if (params->has_cpu_throttle_increment) {
1236 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
1237 }
1238
1239 if (params->has_cpu_throttle_tailslow) {
1240 s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1241 }
1242
1243 if (params->tls_creds) {
1244 g_free(s->parameters.tls_creds);
1245 assert(params->tls_creds->type == QTYPE_QSTRING);
1246 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
1247 }
1248
1249 if (params->tls_hostname) {
1250 g_free(s->parameters.tls_hostname);
1251 assert(params->tls_hostname->type == QTYPE_QSTRING);
1252 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
1253 }
1254
1255 if (params->tls_authz) {
1256 g_free(s->parameters.tls_authz);
1257 assert(params->tls_authz->type == QTYPE_QSTRING);
1258 s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1259 }
1260
1261 if (params->has_max_bandwidth) {
1262 s->parameters.max_bandwidth = params->max_bandwidth;
1263 if (s->to_dst_file && !migration_in_postcopy()) {
Juan Quintelae1fde0e2023-05-15 21:56:58 +02001264 migration_rate_set(s->parameters.max_bandwidth);
Juan Quintela09d6c962023-03-02 10:13:16 +01001265 }
1266 }
1267
Peter Xu8b239592023-10-10 18:19:22 -04001268 if (params->has_avail_switchover_bandwidth) {
1269 s->parameters.avail_switchover_bandwidth = params->avail_switchover_bandwidth;
1270 }
1271
Juan Quintela09d6c962023-03-02 10:13:16 +01001272 if (params->has_downtime_limit) {
1273 s->parameters.downtime_limit = params->downtime_limit;
1274 }
1275
1276 if (params->has_x_checkpoint_delay) {
1277 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
Vladimir Sementsov-Ogievskiy4332ffc2023-04-28 22:49:20 +03001278 colo_checkpoint_delay_set();
Juan Quintela09d6c962023-03-02 10:13:16 +01001279 }
1280
Juan Quintela09d6c962023-03-02 10:13:16 +01001281 if (params->has_multifd_channels) {
1282 s->parameters.multifd_channels = params->multifd_channels;
1283 }
1284 if (params->has_multifd_compression) {
1285 s->parameters.multifd_compression = params->multifd_compression;
1286 }
Bryan Zhangb4014a22024-03-01 03:59:00 +00001287 if (params->has_multifd_zlib_level) {
1288 s->parameters.multifd_zlib_level = params->multifd_zlib_level;
1289 }
1290 if (params->has_multifd_zstd_level) {
1291 s->parameters.multifd_zstd_level = params->multifd_zstd_level;
1292 }
Juan Quintela09d6c962023-03-02 10:13:16 +01001293 if (params->has_xbzrle_cache_size) {
1294 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1295 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1296 }
1297 if (params->has_max_postcopy_bandwidth) {
1298 s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1299 if (s->to_dst_file && migration_in_postcopy()) {
Juan Quintelae1fde0e2023-05-15 21:56:58 +02001300 migration_rate_set(s->parameters.max_postcopy_bandwidth);
Juan Quintela09d6c962023-03-02 10:13:16 +01001301 }
1302 }
1303 if (params->has_max_cpu_throttle) {
1304 s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1305 }
1306 if (params->has_announce_initial) {
1307 s->parameters.announce_initial = params->announce_initial;
1308 }
1309 if (params->has_announce_max) {
1310 s->parameters.announce_max = params->announce_max;
1311 }
1312 if (params->has_announce_rounds) {
1313 s->parameters.announce_rounds = params->announce_rounds;
1314 }
1315 if (params->has_announce_step) {
1316 s->parameters.announce_step = params->announce_step;
1317 }
1318
1319 if (params->has_block_bitmap_mapping) {
1320 qapi_free_BitmapMigrationNodeAliasList(
1321 s->parameters.block_bitmap_mapping);
1322
1323 s->parameters.has_block_bitmap_mapping = true;
1324 s->parameters.block_bitmap_mapping =
1325 QAPI_CLONE(BitmapMigrationNodeAliasList,
1326 params->block_bitmap_mapping);
1327 }
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +08001328
1329 if (params->has_x_vcpu_dirty_limit_period) {
1330 s->parameters.x_vcpu_dirty_limit_period =
1331 params->x_vcpu_dirty_limit_period;
1332 }
Hyman Huang(黄勇)09f9ec92023-06-07 22:58:32 +08001333 if (params->has_vcpu_dirty_limit) {
1334 s->parameters.vcpu_dirty_limit = params->vcpu_dirty_limit;
1335 }
Steve Sistareeea1e5c2023-10-25 12:44:24 -07001336
1337 if (params->has_mode) {
1338 s->parameters.mode = params->mode;
1339 }
Hao Xiang5fdbb1d2024-03-11 18:00:11 +00001340
1341 if (params->has_zero_page_detection) {
1342 s->parameters.zero_page_detection = params->zero_page_detection;
1343 }
Juan Quintela09d6c962023-03-02 10:13:16 +01001344}
1345
1346void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
1347{
1348 MigrationParameters tmp;
1349
Peter Xu86dec712023-09-05 12:23:32 -04001350 /* TODO Rewrite "" to null instead for all three tls_* parameters */
Juan Quintela09d6c962023-03-02 10:13:16 +01001351 if (params->tls_creds
1352 && params->tls_creds->type == QTYPE_QNULL) {
1353 qobject_unref(params->tls_creds->u.n);
1354 params->tls_creds->type = QTYPE_QSTRING;
1355 params->tls_creds->u.s = strdup("");
1356 }
Juan Quintela09d6c962023-03-02 10:13:16 +01001357 if (params->tls_hostname
1358 && params->tls_hostname->type == QTYPE_QNULL) {
1359 qobject_unref(params->tls_hostname->u.n);
1360 params->tls_hostname->type = QTYPE_QSTRING;
1361 params->tls_hostname->u.s = strdup("");
1362 }
Peter Xu86dec712023-09-05 12:23:32 -04001363 if (params->tls_authz
1364 && params->tls_authz->type == QTYPE_QNULL) {
1365 qobject_unref(params->tls_authz->u.n);
1366 params->tls_authz->type = QTYPE_QSTRING;
1367 params->tls_authz->u.s = strdup("");
1368 }
Juan Quintela09d6c962023-03-02 10:13:16 +01001369
1370 migrate_params_test_apply(params, &tmp);
1371
1372 if (!migrate_params_check(&tmp, errp)) {
1373 /* Invalid parameter */
1374 return;
1375 }
1376
1377 migrate_params_apply(params, errp);
1378}