blob: 6bbfd4853d12f93eb591508fd479f9b7bdc0e8c7 [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 Quintela09d6c962023-03-02 10:13:16 +010015#include "exec/target_page.h"
Juan Quintela9c894df2023-03-02 11:00:12 +010016#include "qapi/clone-visitor.h"
Juan Quintela77608702023-03-01 23:05:53 +010017#include "qapi/error.h"
Juan Quintela4d0c6b62023-03-01 23:15:59 +010018#include "qapi/qapi-commands-migration.h"
Juan Quintela9c894df2023-03-02 11:00:12 +010019#include "qapi/qapi-visit-migration.h"
Juan Quintelaf80196b2023-03-01 23:25:44 +010020#include "qapi/qmp/qerror.h"
Juan Quintela09d6c962023-03-02 10:13:16 +010021#include "qapi/qmp/qnull.h"
Juan Quintela77608702023-03-01 23:05:53 +010022#include "sysemu/runstate.h"
Juan Quintela09d6c962023-03-02 10:13:16 +010023#include "migration/colo.h"
Juan Quintela2682c4e2023-03-02 01:17:23 +010024#include "migration/misc.h"
Juan Quintela1f0776f2023-03-01 21:18:45 +010025#include "migration.h"
Juan Quintelae1fde0e2023-05-15 21:56:58 +020026#include "migration-stats.h"
Juan Quintela09d6c962023-03-02 10:13:16 +010027#include "qemu-file.h"
Juan Quintela77608702023-03-01 23:05:53 +010028#include "ram.h"
Juan Quintela1f0776f2023-03-01 21:18:45 +010029#include "options.h"
Hyman Huang(黄勇)dc623952023-06-07 23:30:50 +080030#include "sysemu/kvm.h"
Juan Quintela1f0776f2023-03-01 21:18:45 +010031
Juan Quintela09d6c962023-03-02 10:13:16 +010032/* Maximum migrate downtime set to 2000 seconds */
33#define MAX_MIGRATE_DOWNTIME_SECONDS 2000
34#define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
35
Juan Quintelaf9436522023-03-02 12:55:57 +010036#define MAX_THROTTLE (128 << 20) /* Migration transfer speed throttling */
37
38/* Time in milliseconds we are allowed to stop the source,
39 * for sending the last part */
40#define DEFAULT_MIGRATE_SET_DOWNTIME 300
41
42/* Default compression thread count */
43#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
44/* Default decompression thread count, usually decompression is at
45 * least 4 times as fast as compression.*/
46#define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
47/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
48#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
49/* Define default autoconverge cpu throttle migration parameters */
50#define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50
51#define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
52#define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
53#define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
54
55/* Migration XBZRLE default cache size */
56#define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
57
58/* The delay time (in ms) between two COLO checkpoints */
59#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
60#define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
61#define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
62/* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
63#define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
64/* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
65#define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
66
67/* Background transfer rate for postcopy, 0 means unlimited, note
68 * that page requests can still exceed this limit.
69 */
70#define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
71
72/*
73 * Parameters for self_announce_delay giving a stream of RARP/ARP
74 * packets after migration.
75 */
76#define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50
77#define DEFAULT_MIGRATE_ANNOUNCE_MAX 550
78#define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5
79#define DEFAULT_MIGRATE_ANNOUNCE_STEP 100
80
81#define DEFINE_PROP_MIG_CAP(name, x) \
82 DEFINE_PROP_BOOL(name, MigrationState, capabilities[x], false)
83
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +080084#define DEFAULT_MIGRATE_VCPU_DIRTY_LIMIT_PERIOD 1000 /* milliseconds */
Hyman Huang(黄勇)09f9ec92023-06-07 22:58:32 +080085#define DEFAULT_MIGRATE_VCPU_DIRTY_LIMIT 1 /* MB/s */
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +080086
Juan Quintelaf9436522023-03-02 12:55:57 +010087Property migration_properties[] = {
88 DEFINE_PROP_BOOL("store-global-state", MigrationState,
89 store_global_state, true),
90 DEFINE_PROP_BOOL("send-configuration", MigrationState,
91 send_configuration, true),
92 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
93 send_section_footer, true),
94 DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
95 decompress_error_check, true),
Juan Quintela77c259a2022-06-21 12:13:14 +020096 DEFINE_PROP_BOOL("multifd-flush-after-each-section", MigrationState,
Juan Quintela294e5a42022-06-21 13:36:11 +020097 multifd_flush_after_each_section, false),
Juan Quintelaf9436522023-03-02 12:55:57 +010098 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
99 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
100 DEFINE_PROP_BOOL("x-preempt-pre-7-2", MigrationState,
101 preempt_pre_7_2, false),
102
103 /* Migration parameters */
104 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
105 parameters.compress_level,
106 DEFAULT_MIGRATE_COMPRESS_LEVEL),
107 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
108 parameters.compress_threads,
109 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
110 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
111 parameters.compress_wait_thread, true),
112 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
113 parameters.decompress_threads,
114 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
115 DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState,
116 parameters.throttle_trigger_threshold,
117 DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
118 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
119 parameters.cpu_throttle_initial,
120 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
121 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
122 parameters.cpu_throttle_increment,
123 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
124 DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
125 parameters.cpu_throttle_tailslow, false),
126 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
127 parameters.max_bandwidth, MAX_THROTTLE),
128 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
129 parameters.downtime_limit,
130 DEFAULT_MIGRATE_SET_DOWNTIME),
131 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
132 parameters.x_checkpoint_delay,
133 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
134 DEFINE_PROP_UINT8("multifd-channels", MigrationState,
135 parameters.multifd_channels,
136 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
137 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
138 parameters.multifd_compression,
139 DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
140 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
141 parameters.multifd_zlib_level,
142 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
143 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
144 parameters.multifd_zstd_level,
145 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
146 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
147 parameters.xbzrle_cache_size,
148 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
149 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
150 parameters.max_postcopy_bandwidth,
151 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
152 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
153 parameters.max_cpu_throttle,
154 DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
155 DEFINE_PROP_SIZE("announce-initial", MigrationState,
156 parameters.announce_initial,
157 DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
158 DEFINE_PROP_SIZE("announce-max", MigrationState,
159 parameters.announce_max,
160 DEFAULT_MIGRATE_ANNOUNCE_MAX),
161 DEFINE_PROP_SIZE("announce-rounds", MigrationState,
162 parameters.announce_rounds,
163 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
164 DEFINE_PROP_SIZE("announce-step", MigrationState,
165 parameters.announce_step,
166 DEFAULT_MIGRATE_ANNOUNCE_STEP),
167 DEFINE_PROP_STRING("tls-creds", MigrationState, parameters.tls_creds),
168 DEFINE_PROP_STRING("tls-hostname", MigrationState, parameters.tls_hostname),
169 DEFINE_PROP_STRING("tls-authz", MigrationState, parameters.tls_authz),
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +0800170 DEFINE_PROP_UINT64("x-vcpu-dirty-limit-period", MigrationState,
171 parameters.x_vcpu_dirty_limit_period,
172 DEFAULT_MIGRATE_VCPU_DIRTY_LIMIT_PERIOD),
Hyman Huang(黄勇)09f9ec92023-06-07 22:58:32 +0800173 DEFINE_PROP_UINT64("vcpu-dirty-limit", MigrationState,
174 parameters.vcpu_dirty_limit,
175 DEFAULT_MIGRATE_VCPU_DIRTY_LIMIT),
Juan Quintelaf9436522023-03-02 12:55:57 +0100176
177 /* Migration capabilities */
178 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
179 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
180 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
181 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
182 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
183 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
184 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
185 DEFINE_PROP_MIG_CAP("x-postcopy-preempt",
186 MIGRATION_CAPABILITY_POSTCOPY_PREEMPT),
187 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
188 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
189 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
190 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
191 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
192 DEFINE_PROP_MIG_CAP("x-background-snapshot",
193 MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT),
194#ifdef CONFIG_LINUX
195 DEFINE_PROP_MIG_CAP("x-zero-copy-send",
196 MIGRATION_CAPABILITY_ZERO_COPY_SEND),
197#endif
Avihai Horon65742322023-06-21 14:11:54 +0300198 DEFINE_PROP_MIG_CAP("x-switchover-ack",
199 MIGRATION_CAPABILITY_SWITCHOVER_ACK),
Hyman Huang(黄勇)dc623952023-06-07 23:30:50 +0800200 DEFINE_PROP_MIG_CAP("x-dirty-limit", MIGRATION_CAPABILITY_DIRTY_LIMIT),
Juan Quintelaf9436522023-03-02 12:55:57 +0100201 DEFINE_PROP_END_OF_LIST(),
202};
203
Juan Quintela1f0776f2023-03-01 21:18:45 +0100204bool migrate_auto_converge(void)
205{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100206 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100207
208 return s->capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
209}
210
211bool migrate_background_snapshot(void)
212{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100213 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100214
215 return s->capabilities[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT];
216}
217
Juan Quintela9d4b1e52023-03-01 22:23:57 +0100218bool migrate_block(void)
219{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100220 MigrationState *s = migrate_get_current();
Juan Quintela9d4b1e52023-03-01 22:23:57 +0100221
222 return s->capabilities[MIGRATION_CAPABILITY_BLOCK];
223}
224
Juan Quintela5e804642023-03-01 22:00:16 +0100225bool migrate_colo(void)
226{
227 MigrationState *s = migrate_get_current();
Juan Quintela8f9c5322023-03-01 23:11:08 +0100228
Juan Quintela5e804642023-03-01 22:00:16 +0100229 return s->capabilities[MIGRATION_CAPABILITY_X_COLO];
230}
231
Juan Quintelaa7a94d12023-03-01 22:03:48 +0100232bool migrate_compress(void)
233{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100234 MigrationState *s = migrate_get_current();
Juan Quintelaa7a94d12023-03-01 22:03:48 +0100235
236 return s->capabilities[MIGRATION_CAPABILITY_COMPRESS];
237}
238
Juan Quintela1f0776f2023-03-01 21:18:45 +0100239bool migrate_dirty_bitmaps(void)
240{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100241 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100242
243 return s->capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
244}
245
Hyman Huang(黄勇)dc623952023-06-07 23:30:50 +0800246bool migrate_dirty_limit(void)
247{
248 MigrationState *s = migrate_get_current();
249
250 return s->capabilities[MIGRATION_CAPABILITY_DIRTY_LIMIT];
251}
252
Juan Quintelab8909022023-03-01 22:08:09 +0100253bool migrate_events(void)
254{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100255 MigrationState *s = migrate_get_current();
Juan Quintelab8909022023-03-01 22:08:09 +0100256
257 return s->capabilities[MIGRATION_CAPABILITY_EVENTS];
258}
259
Juan Quintela1f0776f2023-03-01 21:18:45 +0100260bool migrate_ignore_shared(void)
261{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100262 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100263
264 return s->capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
265}
266
267bool migrate_late_block_activate(void)
268{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100269 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100270
271 return s->capabilities[MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
272}
273
Juan Quintela51b07542023-03-01 22:10:29 +0100274bool migrate_multifd(void)
275{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100276 MigrationState *s = migrate_get_current();
Juan Quintela51b07542023-03-01 22:10:29 +0100277
278 return s->capabilities[MIGRATION_CAPABILITY_MULTIFD];
279}
280
Juan Quintela1f0776f2023-03-01 21:18:45 +0100281bool migrate_pause_before_switchover(void)
282{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100283 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100284
285 return s->capabilities[MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
286}
287
288bool migrate_postcopy_blocktime(void)
289{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100290 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100291
292 return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
293}
294
295bool migrate_postcopy_preempt(void)
296{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100297 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100298
299 return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT];
300}
301
302bool migrate_postcopy_ram(void)
303{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100304 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100305
306 return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
307}
308
Juan Quintela17cba692023-03-01 22:41:55 +0100309bool migrate_rdma_pin_all(void)
310{
311 MigrationState *s = migrate_get_current();
312
313 return s->capabilities[MIGRATION_CAPABILITY_RDMA_PIN_ALL];
314}
315
Juan Quintela1f0776f2023-03-01 21:18:45 +0100316bool migrate_release_ram(void)
317{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100318 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100319
320 return s->capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
321}
322
Juan Quintela38ad1112023-03-01 22:25:47 +0100323bool migrate_return_path(void)
324{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100325 MigrationState *s = migrate_get_current();
Juan Quintela38ad1112023-03-01 22:25:47 +0100326
327 return s->capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
328}
329
Avihai Horon65742322023-06-21 14:11:54 +0300330bool migrate_switchover_ack(void)
331{
332 MigrationState *s = migrate_get_current();
333
334 return s->capabilities[MIGRATION_CAPABILITY_SWITCHOVER_ACK];
335}
336
Juan Quintela1f0776f2023-03-01 21:18:45 +0100337bool migrate_validate_uuid(void)
338{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100339 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100340
341 return s->capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
342}
343
Juan Quintela87dca0c2023-03-01 22:20:13 +0100344bool migrate_xbzrle(void)
345{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100346 MigrationState *s = migrate_get_current();
Juan Quintela87dca0c2023-03-01 22:20:13 +0100347
348 return s->capabilities[MIGRATION_CAPABILITY_XBZRLE];
349}
350
Juan Quintela1f0776f2023-03-01 21:18:45 +0100351bool migrate_zero_blocks(void)
352{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100353 MigrationState *s = migrate_get_current();
Juan Quintela1f0776f2023-03-01 21:18:45 +0100354
355 return s->capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
356}
Juan Quintelab4bc3422023-03-01 22:17:14 +0100357
358bool migrate_zero_copy_send(void)
359{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100360 MigrationState *s = migrate_get_current();
Juan Quintelab4bc3422023-03-01 22:17:14 +0100361
362 return s->capabilities[MIGRATION_CAPABILITY_ZERO_COPY_SEND];
363}
Juan Quintelaf774fde2023-03-02 10:44:20 +0100364
365/* pseudo capabilities */
366
Juan Quintela77c259a2022-06-21 12:13:14 +0200367bool migrate_multifd_flush_after_each_section(void)
368{
369 MigrationState *s = migrate_get_current();
370
Juan Quintela294e5a42022-06-21 13:36:11 +0200371 return s->multifd_flush_after_each_section;
Juan Quintela77c259a2022-06-21 12:13:14 +0200372}
373
Juan Quintelaf774fde2023-03-02 10:44:20 +0100374bool migrate_postcopy(void)
375{
376 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
377}
378
Juan Quintela10d47032023-03-02 10:41:23 +0100379bool migrate_tls(void)
380{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100381 MigrationState *s = migrate_get_current();
Juan Quintela10d47032023-03-02 10:41:23 +0100382
383 return s->parameters.tls_creds && *s->parameters.tls_creds;
384}
385
Juan Quintela77608702023-03-01 23:05:53 +0100386typedef enum WriteTrackingSupport {
387 WT_SUPPORT_UNKNOWN = 0,
388 WT_SUPPORT_ABSENT,
389 WT_SUPPORT_AVAILABLE,
390 WT_SUPPORT_COMPATIBLE
391} WriteTrackingSupport;
392
393static
394WriteTrackingSupport migrate_query_write_tracking(void)
395{
396 /* Check if kernel supports required UFFD features */
397 if (!ram_write_tracking_available()) {
398 return WT_SUPPORT_ABSENT;
399 }
400 /*
401 * Check if current memory configuration is
402 * compatible with required UFFD features.
403 */
404 if (!ram_write_tracking_compatible()) {
405 return WT_SUPPORT_AVAILABLE;
406 }
407
408 return WT_SUPPORT_COMPATIBLE;
409}
410
411/* Migration capabilities set */
412struct MigrateCapsSet {
413 int size; /* Capability set size */
414 MigrationCapability caps[]; /* Variadic array of capabilities */
415};
416typedef struct MigrateCapsSet MigrateCapsSet;
417
418/* Define and initialize MigrateCapsSet */
419#define INITIALIZE_MIGRATE_CAPS_SET(_name, ...) \
420 MigrateCapsSet _name = { \
421 .size = sizeof((int []) { __VA_ARGS__ }) / sizeof(int), \
422 .caps = { __VA_ARGS__ } \
423 }
424
425/* Background-snapshot compatibility check list */
426static const
427INITIALIZE_MIGRATE_CAPS_SET(check_caps_background_snapshot,
428 MIGRATION_CAPABILITY_POSTCOPY_RAM,
429 MIGRATION_CAPABILITY_DIRTY_BITMAPS,
430 MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME,
431 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE,
432 MIGRATION_CAPABILITY_RETURN_PATH,
433 MIGRATION_CAPABILITY_MULTIFD,
434 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER,
435 MIGRATION_CAPABILITY_AUTO_CONVERGE,
436 MIGRATION_CAPABILITY_RELEASE_RAM,
437 MIGRATION_CAPABILITY_RDMA_PIN_ALL,
438 MIGRATION_CAPABILITY_COMPRESS,
439 MIGRATION_CAPABILITY_XBZRLE,
440 MIGRATION_CAPABILITY_X_COLO,
441 MIGRATION_CAPABILITY_VALIDATE_UUID,
442 MIGRATION_CAPABILITY_ZERO_COPY_SEND);
443
Wei Wang82137e62023-06-06 18:19:09 +0800444static bool migrate_incoming_started(void)
445{
446 return !!migration_incoming_get_current()->transport_data;
447}
448
Juan Quintela77608702023-03-01 23:05:53 +0100449/**
450 * @migration_caps_check - check capability compatibility
451 *
452 * @old_caps: old capability list
453 * @new_caps: new capability list
454 * @errp: set *errp if the check failed, with reason
455 *
456 * Returns true if check passed, otherwise false.
457 */
458bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
459{
460 MigrationIncomingState *mis = migration_incoming_get_current();
461
Peter Xu74c38cf2023-04-25 21:15:14 -0400462 ERRP_GUARD();
Juan Quintela77608702023-03-01 23:05:53 +0100463#ifndef CONFIG_LIVE_BLOCK_MIGRATION
464 if (new_caps[MIGRATION_CAPABILITY_BLOCK]) {
465 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
466 "block migration");
467 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
468 return false;
469 }
470#endif
471
472#ifndef CONFIG_REPLICATION
473 if (new_caps[MIGRATION_CAPABILITY_X_COLO]) {
474 error_setg(errp, "QEMU compiled without replication module"
475 " can't enable COLO");
476 error_append_hint(errp, "Please enable replication before COLO.\n");
477 return false;
478 }
479#endif
480
481 if (new_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
482 /* This check is reasonably expensive, so only when it's being
483 * set the first time, also it's only the destination that needs
484 * special support.
485 */
486 if (!old_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM] &&
487 runstate_check(RUN_STATE_INMIGRATE) &&
Peter Xu74c38cf2023-04-25 21:15:14 -0400488 !postcopy_ram_supported_by_host(mis, errp)) {
489 error_prepend(errp, "Postcopy is not supported: ");
Juan Quintela77608702023-03-01 23:05:53 +0100490 return false;
491 }
492
493 if (new_caps[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
494 error_setg(errp, "Postcopy is not compatible with ignore-shared");
495 return false;
496 }
Leonardo Brasb405dff2023-04-25 00:33:08 -0300497
498 if (new_caps[MIGRATION_CAPABILITY_MULTIFD]) {
499 error_setg(errp, "Postcopy is not yet compatible with multifd");
500 return false;
501 }
Juan Quintela77608702023-03-01 23:05:53 +0100502 }
503
504 if (new_caps[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT]) {
505 WriteTrackingSupport wt_support;
506 int idx;
507 /*
508 * Check if 'background-snapshot' capability is supported by
509 * host kernel and compatible with guest memory configuration.
510 */
511 wt_support = migrate_query_write_tracking();
512 if (wt_support < WT_SUPPORT_AVAILABLE) {
513 error_setg(errp, "Background-snapshot is not supported by host kernel");
514 return false;
515 }
516 if (wt_support < WT_SUPPORT_COMPATIBLE) {
517 error_setg(errp, "Background-snapshot is not compatible "
518 "with guest memory configuration");
519 return false;
520 }
521
522 /*
523 * Check if there are any migration capabilities
524 * incompatible with 'background-snapshot'.
525 */
526 for (idx = 0; idx < check_caps_background_snapshot.size; idx++) {
527 int incomp_cap = check_caps_background_snapshot.caps[idx];
528 if (new_caps[incomp_cap]) {
529 error_setg(errp,
530 "Background-snapshot is not compatible with %s",
531 MigrationCapability_str(incomp_cap));
532 return false;
533 }
534 }
535 }
536
537#ifdef CONFIG_LINUX
538 if (new_caps[MIGRATION_CAPABILITY_ZERO_COPY_SEND] &&
539 (!new_caps[MIGRATION_CAPABILITY_MULTIFD] ||
540 new_caps[MIGRATION_CAPABILITY_COMPRESS] ||
541 new_caps[MIGRATION_CAPABILITY_XBZRLE] ||
542 migrate_multifd_compression() ||
Juan Quintela10d47032023-03-02 10:41:23 +0100543 migrate_tls())) {
Juan Quintela77608702023-03-01 23:05:53 +0100544 error_setg(errp,
545 "Zero copy only available for non-compressed non-TLS multifd migration");
546 return false;
547 }
548#else
549 if (new_caps[MIGRATION_CAPABILITY_ZERO_COPY_SEND]) {
550 error_setg(errp,
551 "Zero copy currently only available on Linux");
552 return false;
553 }
554#endif
555
556 if (new_caps[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT]) {
557 if (!new_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
558 error_setg(errp, "Postcopy preempt requires postcopy-ram");
559 return false;
560 }
561
562 /*
563 * Preempt mode requires urgent pages to be sent in separate
564 * channel, OTOH compression logic will disorder all pages into
565 * different compression channels, which is not compatible with the
566 * preempt assumptions on channel assignments.
567 */
568 if (new_caps[MIGRATION_CAPABILITY_COMPRESS]) {
569 error_setg(errp, "Postcopy preempt not compatible with compress");
570 return false;
571 }
Wei Wang82137e62023-06-06 18:19:09 +0800572
573 if (migrate_incoming_started()) {
574 error_setg(errp,
575 "Postcopy preempt must be set before incoming starts");
576 return false;
577 }
Juan Quintela77608702023-03-01 23:05:53 +0100578 }
579
580 if (new_caps[MIGRATION_CAPABILITY_MULTIFD]) {
581 if (new_caps[MIGRATION_CAPABILITY_COMPRESS]) {
582 error_setg(errp, "Multifd is not compatible with compress");
583 return false;
584 }
Wei Wang82137e62023-06-06 18:19:09 +0800585 if (migrate_incoming_started()) {
586 error_setg(errp, "Multifd must be set before incoming starts");
587 return false;
588 }
Juan Quintela77608702023-03-01 23:05:53 +0100589 }
590
Avihai Horon65742322023-06-21 14:11:54 +0300591 if (new_caps[MIGRATION_CAPABILITY_SWITCHOVER_ACK]) {
592 if (!new_caps[MIGRATION_CAPABILITY_RETURN_PATH]) {
593 error_setg(errp, "Capability 'switchover-ack' requires capability "
594 "'return-path'");
595 return false;
596 }
Avihai Horon65742322023-06-21 14:11:54 +0300597 }
Hyman Huang(黄勇)dc623952023-06-07 23:30:50 +0800598 if (new_caps[MIGRATION_CAPABILITY_DIRTY_LIMIT]) {
599 if (new_caps[MIGRATION_CAPABILITY_AUTO_CONVERGE]) {
600 error_setg(errp, "dirty-limit conflicts with auto-converge"
601 " either of then available currently");
602 return false;
603 }
604
605 if (!kvm_enabled() || !kvm_dirty_ring_enabled()) {
606 error_setg(errp, "dirty-limit requires KVM with accelerator"
607 " property 'dirty-ring-size' set");
608 return false;
609 }
610 }
Avihai Horon65742322023-06-21 14:11:54 +0300611
Juan Quintela77608702023-03-01 23:05:53 +0100612 return true;
613}
Juan Quintela4d0c6b62023-03-01 23:15:59 +0100614
Juan Quintelaf80196b2023-03-01 23:25:44 +0100615bool migrate_cap_set(int cap, bool value, Error **errp)
616{
617 MigrationState *s = migrate_get_current();
618 bool new_caps[MIGRATION_CAPABILITY__MAX];
619
620 if (migration_is_running(s->state)) {
621 error_setg(errp, QERR_MIGRATION_ACTIVE);
622 return false;
623 }
624
625 memcpy(new_caps, s->capabilities, sizeof(new_caps));
626 new_caps[cap] = value;
627
628 if (!migrate_caps_check(s->capabilities, new_caps, errp)) {
629 return false;
630 }
631 s->capabilities[cap] = value;
632 return true;
633}
634
Juan Quintela4d0c6b62023-03-01 23:15:59 +0100635MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
636{
637 MigrationCapabilityStatusList *head = NULL, **tail = &head;
638 MigrationCapabilityStatus *caps;
639 MigrationState *s = migrate_get_current();
640 int i;
641
642 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
643#ifndef CONFIG_LIVE_BLOCK_MIGRATION
644 if (i == MIGRATION_CAPABILITY_BLOCK) {
645 continue;
646 }
647#endif
648 caps = g_malloc0(sizeof(*caps));
649 caps->capability = i;
650 caps->state = s->capabilities[i];
651 QAPI_LIST_APPEND(tail, caps);
652 }
653
654 return head;
655}
Juan Quintela45c1de12023-03-01 23:18:02 +0100656
657void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
658 Error **errp)
659{
660 MigrationState *s = migrate_get_current();
661 MigrationCapabilityStatusList *cap;
662 bool new_caps[MIGRATION_CAPABILITY__MAX];
663
Vladimir Sementsov-Ogievskiyd70178a2023-04-28 22:49:27 +0300664 if (migration_is_running(s->state) || migration_in_colo_state()) {
Juan Quintela45c1de12023-03-01 23:18:02 +0100665 error_setg(errp, QERR_MIGRATION_ACTIVE);
666 return;
667 }
668
669 memcpy(new_caps, s->capabilities, sizeof(new_caps));
670 for (cap = params; cap; cap = cap->next) {
671 new_caps[cap->value->capability] = cap->value->state;
672 }
673
674 if (!migrate_caps_check(s->capabilities, new_caps, errp)) {
675 return;
676 }
677
678 for (cap = params; cap; cap = cap->next) {
679 s->capabilities[cap->value->capability] = cap->value->state;
680 }
681}
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100682
683/* parameters */
684
Juan Quintelab804b352023-03-02 12:49:16 +0100685const BitmapMigrationNodeAliasList *migrate_block_bitmap_mapping(void)
686{
687 MigrationState *s = migrate_get_current();
688
689 return s->parameters.block_bitmap_mapping;
690}
691
Juan Quintela3cba22c2023-05-03 20:10:36 +0200692bool migrate_has_block_bitmap_mapping(void)
693{
694 MigrationState *s = migrate_get_current();
695
696 return s->parameters.has_block_bitmap_mapping;
697}
698
Juan Quintela6f8be702023-03-02 00:49:47 +0100699bool migrate_block_incremental(void)
700{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100701 MigrationState *s = migrate_get_current();
Juan Quintela6f8be702023-03-02 00:49:47 +0100702
703 return s->parameters.block_incremental;
704}
705
Juan Quintelaf94a8582023-03-02 01:04:55 +0100706uint32_t migrate_checkpoint_delay(void)
707{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100708 MigrationState *s = migrate_get_current();
Juan Quintelaf94a8582023-03-02 01:04:55 +0100709
710 return s->parameters.x_checkpoint_delay;
711}
712
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100713int migrate_compress_level(void)
714{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100715 MigrationState *s = migrate_get_current();
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100716
717 return s->parameters.compress_level;
718}
719
720int migrate_compress_threads(void)
721{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100722 MigrationState *s = migrate_get_current();
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100723
724 return s->parameters.compress_threads;
725}
726
727int migrate_compress_wait_thread(void)
728{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100729 MigrationState *s = migrate_get_current();
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100730
731 return s->parameters.compress_wait_thread;
732}
733
Juan Quintela9605c2a2023-03-02 10:20:49 +0100734uint8_t migrate_cpu_throttle_increment(void)
735{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100736 MigrationState *s = migrate_get_current();
Juan Quintela9605c2a2023-03-02 10:20:49 +0100737
738 return s->parameters.cpu_throttle_increment;
739}
740
Juan Quintela2a8ec382023-03-02 01:22:44 +0100741uint8_t migrate_cpu_throttle_initial(void)
742{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100743 MigrationState *s = migrate_get_current();
Juan Quintela2a8ec382023-03-02 01:22:44 +0100744
745 return s->parameters.cpu_throttle_initial;
746}
747
Juan Quintela873f6742023-03-02 10:29:51 +0100748bool migrate_cpu_throttle_tailslow(void)
749{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100750 MigrationState *s = migrate_get_current();
Juan Quintela873f6742023-03-02 10:29:51 +0100751
752 return s->parameters.cpu_throttle_tailslow;
753}
754
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100755int migrate_decompress_threads(void)
756{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100757 MigrationState *s = migrate_get_current();
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100758
759 return s->parameters.decompress_threads;
760}
761
Juan Quintelaf5da8ba2023-03-02 12:00:43 +0100762uint64_t migrate_downtime_limit(void)
763{
764 MigrationState *s = migrate_get_current();
765
766 return s->parameters.downtime_limit;
767}
768
Juan Quintela24155bd2023-03-02 01:13:01 +0100769uint8_t migrate_max_cpu_throttle(void)
770{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100771 MigrationState *s = migrate_get_current();
Juan Quintela24155bd2023-03-02 01:13:01 +0100772
773 return s->parameters.max_cpu_throttle;
774}
775
Juan Quintela9c894df2023-03-02 11:00:12 +0100776uint64_t migrate_max_bandwidth(void)
777{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100778 MigrationState *s = migrate_get_current();
Juan Quintela9c894df2023-03-02 11:00:12 +0100779
780 return s->parameters.max_bandwidth;
781}
782
Juan Quintela52033342023-05-04 13:38:33 +0200783uint64_t migrate_max_postcopy_bandwidth(void)
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100784{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100785 MigrationState *s = migrate_get_current();
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100786
787 return s->parameters.max_postcopy_bandwidth;
788}
789
790int migrate_multifd_channels(void)
791{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100792 MigrationState *s = migrate_get_current();
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100793
794 return s->parameters.multifd_channels;
795}
796
797MultiFDCompression migrate_multifd_compression(void)
798{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100799 MigrationState *s = migrate_get_current();
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100800
801 assert(s->parameters.multifd_compression < MULTIFD_COMPRESSION__MAX);
802 return s->parameters.multifd_compression;
803}
804
805int migrate_multifd_zlib_level(void)
806{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100807 MigrationState *s = migrate_get_current();
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100808
809 return s->parameters.multifd_zlib_level;
810}
811
812int migrate_multifd_zstd_level(void)
813{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100814 MigrationState *s = migrate_get_current();
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100815
816 return s->parameters.multifd_zstd_level;
817}
818
Juan Quintela6499efd2023-03-02 00:59:13 +0100819uint8_t migrate_throttle_trigger_threshold(void)
820{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100821 MigrationState *s = migrate_get_current();
Juan Quintela6499efd2023-03-02 00:59:13 +0100822
823 return s->parameters.throttle_trigger_threshold;
824}
825
Juan Quintela2eb03082023-03-02 12:33:50 +0100826const char *migrate_tls_authz(void)
827{
828 MigrationState *s = migrate_get_current();
829
830 return s->parameters.tls_authz;
831}
832
Juan Quintelad5c3e192023-03-02 12:17:06 +0100833const char *migrate_tls_creds(void)
834{
835 MigrationState *s = migrate_get_current();
836
837 return s->parameters.tls_creds;
838}
839
Juan Quintela1f2f3662023-03-02 12:37:21 +0100840const char *migrate_tls_hostname(void)
841{
842 MigrationState *s = migrate_get_current();
843
844 return s->parameters.tls_hostname;
845}
846
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100847uint64_t migrate_xbzrle_cache_size(void)
848{
Juan Quintela8f9c5322023-03-01 23:11:08 +0100849 MigrationState *s = migrate_get_current();
Juan Quintela1dfc4b92023-03-02 00:39:03 +0100850
851 return s->parameters.xbzrle_cache_size;
852}
Juan Quintela2682c4e2023-03-02 01:17:23 +0100853
Juan Quintela87c22902023-03-02 12:05:03 +0100854/* parameter setters */
855
856void migrate_set_block_incremental(bool value)
857{
858 MigrationState *s = migrate_get_current();
859
860 s->parameters.block_incremental = value;
861}
862
Juan Quintela2682c4e2023-03-02 01:17:23 +0100863/* parameters helpers */
864
Juan Quintelab1a87952023-03-02 12:10:33 +0100865void block_cleanup_parameters(void)
Juan Quintelab7b73122023-03-02 12:08:45 +0100866{
Juan Quintelab1a87952023-03-02 12:10:33 +0100867 MigrationState *s = migrate_get_current();
868
Juan Quintelab7b73122023-03-02 12:08:45 +0100869 if (s->must_remove_block_options) {
870 /* setting to false can never fail */
871 migrate_cap_set(MIGRATION_CAPABILITY_BLOCK, false, &error_abort);
872 migrate_set_block_incremental(false);
873 s->must_remove_block_options = false;
874 }
875}
876
Juan Quintela2682c4e2023-03-02 01:17:23 +0100877AnnounceParameters *migrate_announce_params(void)
878{
879 static AnnounceParameters ap;
880
881 MigrationState *s = migrate_get_current();
882
883 ap.initial = s->parameters.announce_initial;
884 ap.max = s->parameters.announce_max;
885 ap.rounds = s->parameters.announce_rounds;
886 ap.step = s->parameters.announce_step;
887
888 return &ap;
889}
Juan Quintela9c894df2023-03-02 11:00:12 +0100890
891MigrationParameters *qmp_query_migrate_parameters(Error **errp)
892{
893 MigrationParameters *params;
894 MigrationState *s = migrate_get_current();
895
896 /* TODO use QAPI_CLONE() instead of duplicating it inline */
897 params = g_malloc0(sizeof(*params));
898 params->has_compress_level = true;
899 params->compress_level = s->parameters.compress_level;
900 params->has_compress_threads = true;
901 params->compress_threads = s->parameters.compress_threads;
902 params->has_compress_wait_thread = true;
903 params->compress_wait_thread = s->parameters.compress_wait_thread;
904 params->has_decompress_threads = true;
905 params->decompress_threads = s->parameters.decompress_threads;
906 params->has_throttle_trigger_threshold = true;
907 params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
908 params->has_cpu_throttle_initial = true;
909 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
910 params->has_cpu_throttle_increment = true;
911 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
912 params->has_cpu_throttle_tailslow = true;
913 params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
914 params->tls_creds = g_strdup(s->parameters.tls_creds);
915 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
916 params->tls_authz = g_strdup(s->parameters.tls_authz ?
917 s->parameters.tls_authz : "");
918 params->has_max_bandwidth = true;
919 params->max_bandwidth = s->parameters.max_bandwidth;
920 params->has_downtime_limit = true;
921 params->downtime_limit = s->parameters.downtime_limit;
922 params->has_x_checkpoint_delay = true;
923 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
924 params->has_block_incremental = true;
925 params->block_incremental = s->parameters.block_incremental;
926 params->has_multifd_channels = true;
927 params->multifd_channels = s->parameters.multifd_channels;
928 params->has_multifd_compression = true;
929 params->multifd_compression = s->parameters.multifd_compression;
930 params->has_multifd_zlib_level = true;
931 params->multifd_zlib_level = s->parameters.multifd_zlib_level;
932 params->has_multifd_zstd_level = true;
933 params->multifd_zstd_level = s->parameters.multifd_zstd_level;
934 params->has_xbzrle_cache_size = true;
935 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
936 params->has_max_postcopy_bandwidth = true;
937 params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
938 params->has_max_cpu_throttle = true;
939 params->max_cpu_throttle = s->parameters.max_cpu_throttle;
940 params->has_announce_initial = true;
941 params->announce_initial = s->parameters.announce_initial;
942 params->has_announce_max = true;
943 params->announce_max = s->parameters.announce_max;
944 params->has_announce_rounds = true;
945 params->announce_rounds = s->parameters.announce_rounds;
946 params->has_announce_step = true;
947 params->announce_step = s->parameters.announce_step;
948
949 if (s->parameters.has_block_bitmap_mapping) {
950 params->has_block_bitmap_mapping = true;
951 params->block_bitmap_mapping =
952 QAPI_CLONE(BitmapMigrationNodeAliasList,
953 s->parameters.block_bitmap_mapping);
954 }
955
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +0800956 params->has_x_vcpu_dirty_limit_period = true;
957 params->x_vcpu_dirty_limit_period = s->parameters.x_vcpu_dirty_limit_period;
Hyman Huang(黄勇)09f9ec92023-06-07 22:58:32 +0800958 params->has_vcpu_dirty_limit = true;
959 params->vcpu_dirty_limit = s->parameters.vcpu_dirty_limit;
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +0800960
Juan Quintela9c894df2023-03-02 11:00:12 +0100961 return params;
962}
Juan Quintela09d6c962023-03-02 10:13:16 +0100963
Juan Quintela61a174e2023-03-02 10:35:24 +0100964void migrate_params_init(MigrationParameters *params)
965{
966 params->tls_hostname = g_strdup("");
967 params->tls_creds = g_strdup("");
968
969 /* Set has_* up only for parameter checks */
970 params->has_compress_level = true;
971 params->has_compress_threads = true;
972 params->has_compress_wait_thread = true;
973 params->has_decompress_threads = true;
974 params->has_throttle_trigger_threshold = true;
975 params->has_cpu_throttle_initial = true;
976 params->has_cpu_throttle_increment = true;
977 params->has_cpu_throttle_tailslow = true;
978 params->has_max_bandwidth = true;
979 params->has_downtime_limit = true;
980 params->has_x_checkpoint_delay = true;
981 params->has_block_incremental = true;
982 params->has_multifd_channels = true;
983 params->has_multifd_compression = true;
984 params->has_multifd_zlib_level = true;
985 params->has_multifd_zstd_level = true;
986 params->has_xbzrle_cache_size = true;
987 params->has_max_postcopy_bandwidth = true;
988 params->has_max_cpu_throttle = true;
989 params->has_announce_initial = true;
990 params->has_announce_max = true;
991 params->has_announce_rounds = true;
992 params->has_announce_step = true;
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +0800993 params->has_x_vcpu_dirty_limit_period = true;
Hyman Huang(黄勇)09f9ec92023-06-07 22:58:32 +0800994 params->has_vcpu_dirty_limit = true;
Juan Quintela61a174e2023-03-02 10:35:24 +0100995}
996
Juan Quintela09d6c962023-03-02 10:13:16 +0100997/*
998 * Check whether the parameters are valid. Error will be put into errp
999 * (if provided). Return true if valid, otherwise false.
1000 */
1001bool migrate_params_check(MigrationParameters *params, Error **errp)
1002{
1003 if (params->has_compress_level &&
1004 (params->compress_level > 9)) {
1005 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
1006 "a value between 0 and 9");
1007 return false;
1008 }
1009
1010 if (params->has_compress_threads && (params->compress_threads < 1)) {
1011 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1012 "compress_threads",
1013 "a value between 1 and 255");
1014 return false;
1015 }
1016
1017 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
1018 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1019 "decompress_threads",
1020 "a value between 1 and 255");
1021 return false;
1022 }
1023
1024 if (params->has_throttle_trigger_threshold &&
1025 (params->throttle_trigger_threshold < 1 ||
1026 params->throttle_trigger_threshold > 100)) {
1027 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1028 "throttle_trigger_threshold",
1029 "an integer in the range of 1 to 100");
1030 return false;
1031 }
1032
1033 if (params->has_cpu_throttle_initial &&
1034 (params->cpu_throttle_initial < 1 ||
1035 params->cpu_throttle_initial > 99)) {
1036 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1037 "cpu_throttle_initial",
1038 "an integer in the range of 1 to 99");
1039 return false;
1040 }
1041
1042 if (params->has_cpu_throttle_increment &&
1043 (params->cpu_throttle_increment < 1 ||
1044 params->cpu_throttle_increment > 99)) {
1045 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1046 "cpu_throttle_increment",
1047 "an integer in the range of 1 to 99");
1048 return false;
1049 }
1050
1051 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
1052 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1053 "max_bandwidth",
1054 "an integer in the range of 0 to "stringify(SIZE_MAX)
1055 " bytes/second");
1056 return false;
1057 }
1058
1059 if (params->has_downtime_limit &&
1060 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
1061 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1062 "downtime_limit",
1063 "an integer in the range of 0 to "
1064 stringify(MAX_MIGRATE_DOWNTIME)" ms");
1065 return false;
1066 }
1067
1068 /* x_checkpoint_delay is now always positive */
1069
1070 if (params->has_multifd_channels && (params->multifd_channels < 1)) {
1071 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1072 "multifd_channels",
1073 "a value between 1 and 255");
1074 return false;
1075 }
1076
1077 if (params->has_multifd_zlib_level &&
1078 (params->multifd_zlib_level > 9)) {
1079 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
1080 "a value between 0 and 9");
1081 return false;
1082 }
1083
1084 if (params->has_multifd_zstd_level &&
1085 (params->multifd_zstd_level > 20)) {
1086 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
1087 "a value between 0 and 20");
1088 return false;
1089 }
1090
1091 if (params->has_xbzrle_cache_size &&
1092 (params->xbzrle_cache_size < qemu_target_page_size() ||
1093 !is_power_of_2(params->xbzrle_cache_size))) {
1094 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1095 "xbzrle_cache_size",
1096 "a power of two no less than the target page size");
1097 return false;
1098 }
1099
1100 if (params->has_max_cpu_throttle &&
1101 (params->max_cpu_throttle < params->cpu_throttle_initial ||
1102 params->max_cpu_throttle > 99)) {
1103 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1104 "max_cpu_throttle",
1105 "an integer in the range of cpu_throttle_initial to 99");
1106 return false;
1107 }
1108
1109 if (params->has_announce_initial &&
1110 params->announce_initial > 100000) {
1111 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1112 "announce_initial",
1113 "a value between 0 and 100000");
1114 return false;
1115 }
1116 if (params->has_announce_max &&
1117 params->announce_max > 100000) {
1118 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1119 "announce_max",
1120 "a value between 0 and 100000");
1121 return false;
1122 }
1123 if (params->has_announce_rounds &&
1124 params->announce_rounds > 1000) {
1125 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1126 "announce_rounds",
1127 "a value between 0 and 1000");
1128 return false;
1129 }
1130 if (params->has_announce_step &&
1131 (params->announce_step < 1 ||
1132 params->announce_step > 10000)) {
1133 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1134 "announce_step",
1135 "a value between 0 and 10000");
1136 return false;
1137 }
1138
1139 if (params->has_block_bitmap_mapping &&
1140 !check_dirty_bitmap_mig_alias_map(params->block_bitmap_mapping, errp)) {
1141 error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: ");
1142 return false;
1143 }
1144
1145#ifdef CONFIG_LINUX
1146 if (migrate_zero_copy_send() &&
1147 ((params->has_multifd_compression && params->multifd_compression) ||
1148 (params->tls_creds && *params->tls_creds))) {
1149 error_setg(errp,
1150 "Zero copy only available for non-compressed non-TLS multifd migration");
1151 return false;
1152 }
1153#endif
1154
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +08001155 if (params->has_x_vcpu_dirty_limit_period &&
1156 (params->x_vcpu_dirty_limit_period < 1 ||
1157 params->x_vcpu_dirty_limit_period > 1000)) {
1158 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1159 "x-vcpu-dirty-limit-period",
1160 "a value between 1 and 1000");
1161 return false;
1162 }
1163
Hyman Huang(黄勇)09f9ec92023-06-07 22:58:32 +08001164 if (params->has_vcpu_dirty_limit &&
1165 (params->vcpu_dirty_limit < 1)) {
1166 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1167 "vcpu_dirty_limit",
1168 "is invalid, it must greater then 1 MB/s");
1169 return false;
1170 }
1171
Juan Quintela09d6c962023-03-02 10:13:16 +01001172 return true;
1173}
1174
1175static void migrate_params_test_apply(MigrateSetParameters *params,
1176 MigrationParameters *dest)
1177{
1178 *dest = migrate_get_current()->parameters;
1179
1180 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1181
1182 if (params->has_compress_level) {
1183 dest->compress_level = params->compress_level;
1184 }
1185
1186 if (params->has_compress_threads) {
1187 dest->compress_threads = params->compress_threads;
1188 }
1189
1190 if (params->has_compress_wait_thread) {
1191 dest->compress_wait_thread = params->compress_wait_thread;
1192 }
1193
1194 if (params->has_decompress_threads) {
1195 dest->decompress_threads = params->decompress_threads;
1196 }
1197
1198 if (params->has_throttle_trigger_threshold) {
1199 dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
1200 }
1201
1202 if (params->has_cpu_throttle_initial) {
1203 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1204 }
1205
1206 if (params->has_cpu_throttle_increment) {
1207 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1208 }
1209
1210 if (params->has_cpu_throttle_tailslow) {
1211 dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1212 }
1213
1214 if (params->tls_creds) {
1215 assert(params->tls_creds->type == QTYPE_QSTRING);
1216 dest->tls_creds = params->tls_creds->u.s;
1217 }
1218
1219 if (params->tls_hostname) {
1220 assert(params->tls_hostname->type == QTYPE_QSTRING);
1221 dest->tls_hostname = params->tls_hostname->u.s;
1222 }
1223
1224 if (params->has_max_bandwidth) {
1225 dest->max_bandwidth = params->max_bandwidth;
1226 }
1227
1228 if (params->has_downtime_limit) {
1229 dest->downtime_limit = params->downtime_limit;
1230 }
1231
1232 if (params->has_x_checkpoint_delay) {
1233 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1234 }
1235
1236 if (params->has_block_incremental) {
1237 dest->block_incremental = params->block_incremental;
1238 }
1239 if (params->has_multifd_channels) {
1240 dest->multifd_channels = params->multifd_channels;
1241 }
1242 if (params->has_multifd_compression) {
1243 dest->multifd_compression = params->multifd_compression;
1244 }
1245 if (params->has_xbzrle_cache_size) {
1246 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1247 }
1248 if (params->has_max_postcopy_bandwidth) {
1249 dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1250 }
1251 if (params->has_max_cpu_throttle) {
1252 dest->max_cpu_throttle = params->max_cpu_throttle;
1253 }
1254 if (params->has_announce_initial) {
1255 dest->announce_initial = params->announce_initial;
1256 }
1257 if (params->has_announce_max) {
1258 dest->announce_max = params->announce_max;
1259 }
1260 if (params->has_announce_rounds) {
1261 dest->announce_rounds = params->announce_rounds;
1262 }
1263 if (params->has_announce_step) {
1264 dest->announce_step = params->announce_step;
1265 }
1266
1267 if (params->has_block_bitmap_mapping) {
1268 dest->has_block_bitmap_mapping = true;
1269 dest->block_bitmap_mapping = params->block_bitmap_mapping;
1270 }
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +08001271
1272 if (params->has_x_vcpu_dirty_limit_period) {
1273 dest->x_vcpu_dirty_limit_period =
1274 params->x_vcpu_dirty_limit_period;
1275 }
Hyman Huang(黄勇)09f9ec92023-06-07 22:58:32 +08001276 if (params->has_vcpu_dirty_limit) {
1277 dest->vcpu_dirty_limit = params->vcpu_dirty_limit;
1278 }
Juan Quintela09d6c962023-03-02 10:13:16 +01001279}
1280
1281static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
1282{
1283 MigrationState *s = migrate_get_current();
1284
1285 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1286
1287 if (params->has_compress_level) {
1288 s->parameters.compress_level = params->compress_level;
1289 }
1290
1291 if (params->has_compress_threads) {
1292 s->parameters.compress_threads = params->compress_threads;
1293 }
1294
1295 if (params->has_compress_wait_thread) {
1296 s->parameters.compress_wait_thread = params->compress_wait_thread;
1297 }
1298
1299 if (params->has_decompress_threads) {
1300 s->parameters.decompress_threads = params->decompress_threads;
1301 }
1302
1303 if (params->has_throttle_trigger_threshold) {
1304 s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
1305 }
1306
1307 if (params->has_cpu_throttle_initial) {
1308 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
1309 }
1310
1311 if (params->has_cpu_throttle_increment) {
1312 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
1313 }
1314
1315 if (params->has_cpu_throttle_tailslow) {
1316 s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1317 }
1318
1319 if (params->tls_creds) {
1320 g_free(s->parameters.tls_creds);
1321 assert(params->tls_creds->type == QTYPE_QSTRING);
1322 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
1323 }
1324
1325 if (params->tls_hostname) {
1326 g_free(s->parameters.tls_hostname);
1327 assert(params->tls_hostname->type == QTYPE_QSTRING);
1328 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
1329 }
1330
1331 if (params->tls_authz) {
1332 g_free(s->parameters.tls_authz);
1333 assert(params->tls_authz->type == QTYPE_QSTRING);
1334 s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1335 }
1336
1337 if (params->has_max_bandwidth) {
1338 s->parameters.max_bandwidth = params->max_bandwidth;
1339 if (s->to_dst_file && !migration_in_postcopy()) {
Juan Quintelae1fde0e2023-05-15 21:56:58 +02001340 migration_rate_set(s->parameters.max_bandwidth);
Juan Quintela09d6c962023-03-02 10:13:16 +01001341 }
1342 }
1343
1344 if (params->has_downtime_limit) {
1345 s->parameters.downtime_limit = params->downtime_limit;
1346 }
1347
1348 if (params->has_x_checkpoint_delay) {
1349 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
Vladimir Sementsov-Ogievskiy4332ffc2023-04-28 22:49:20 +03001350 colo_checkpoint_delay_set();
Juan Quintela09d6c962023-03-02 10:13:16 +01001351 }
1352
1353 if (params->has_block_incremental) {
1354 s->parameters.block_incremental = params->block_incremental;
1355 }
1356 if (params->has_multifd_channels) {
1357 s->parameters.multifd_channels = params->multifd_channels;
1358 }
1359 if (params->has_multifd_compression) {
1360 s->parameters.multifd_compression = params->multifd_compression;
1361 }
1362 if (params->has_xbzrle_cache_size) {
1363 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1364 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1365 }
1366 if (params->has_max_postcopy_bandwidth) {
1367 s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1368 if (s->to_dst_file && migration_in_postcopy()) {
Juan Quintelae1fde0e2023-05-15 21:56:58 +02001369 migration_rate_set(s->parameters.max_postcopy_bandwidth);
Juan Quintela09d6c962023-03-02 10:13:16 +01001370 }
1371 }
1372 if (params->has_max_cpu_throttle) {
1373 s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1374 }
1375 if (params->has_announce_initial) {
1376 s->parameters.announce_initial = params->announce_initial;
1377 }
1378 if (params->has_announce_max) {
1379 s->parameters.announce_max = params->announce_max;
1380 }
1381 if (params->has_announce_rounds) {
1382 s->parameters.announce_rounds = params->announce_rounds;
1383 }
1384 if (params->has_announce_step) {
1385 s->parameters.announce_step = params->announce_step;
1386 }
1387
1388 if (params->has_block_bitmap_mapping) {
1389 qapi_free_BitmapMigrationNodeAliasList(
1390 s->parameters.block_bitmap_mapping);
1391
1392 s->parameters.has_block_bitmap_mapping = true;
1393 s->parameters.block_bitmap_mapping =
1394 QAPI_CLONE(BitmapMigrationNodeAliasList,
1395 params->block_bitmap_mapping);
1396 }
Hyman Huang(黄勇)4d807852023-06-07 21:32:59 +08001397
1398 if (params->has_x_vcpu_dirty_limit_period) {
1399 s->parameters.x_vcpu_dirty_limit_period =
1400 params->x_vcpu_dirty_limit_period;
1401 }
Hyman Huang(黄勇)09f9ec92023-06-07 22:58:32 +08001402 if (params->has_vcpu_dirty_limit) {
1403 s->parameters.vcpu_dirty_limit = params->vcpu_dirty_limit;
1404 }
Juan Quintela09d6c962023-03-02 10:13:16 +01001405}
1406
1407void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
1408{
1409 MigrationParameters tmp;
1410
Peter Xu86dec712023-09-05 12:23:32 -04001411 /* TODO Rewrite "" to null instead for all three tls_* parameters */
Juan Quintela09d6c962023-03-02 10:13:16 +01001412 if (params->tls_creds
1413 && params->tls_creds->type == QTYPE_QNULL) {
1414 qobject_unref(params->tls_creds->u.n);
1415 params->tls_creds->type = QTYPE_QSTRING;
1416 params->tls_creds->u.s = strdup("");
1417 }
Juan Quintela09d6c962023-03-02 10:13:16 +01001418 if (params->tls_hostname
1419 && params->tls_hostname->type == QTYPE_QNULL) {
1420 qobject_unref(params->tls_hostname->u.n);
1421 params->tls_hostname->type = QTYPE_QSTRING;
1422 params->tls_hostname->u.s = strdup("");
1423 }
Peter Xu86dec712023-09-05 12:23:32 -04001424 if (params->tls_authz
1425 && params->tls_authz->type == QTYPE_QNULL) {
1426 qobject_unref(params->tls_authz->u.n);
1427 params->tls_authz->type = QTYPE_QSTRING;
1428 params->tls_authz->u.s = strdup("");
1429 }
Juan Quintela09d6c962023-03-02 10:13:16 +01001430
1431 migrate_params_test_apply(params, &tmp);
1432
1433 if (!migrate_params_check(&tmp, errp)) {
1434 /* Invalid parameter */
1435 return;
1436 }
1437
1438 migrate_params_apply(params, errp);
1439}