s390x: Fix QEMU abort by selecting S390_FLIC_KVM
[qemu/kevin.git] / migration / options.c
blob5a9505adf7a874cd00a0c91267bbf63ce5e95f58
1 /*
2 * QEMU migration capabilities
4 * Copyright (c) 2012-2023 Red Hat Inc
6 * Authors:
7 * Orit Wasserman <owasserm@redhat.com>
8 * Juan Quintela <quintela@redhat.com>
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.
14 #include "qemu/osdep.h"
15 #include "exec/target_page.h"
16 #include "qapi/clone-visitor.h"
17 #include "qapi/error.h"
18 #include "qapi/qapi-commands-migration.h"
19 #include "qapi/qapi-visit-migration.h"
20 #include "qapi/qmp/qerror.h"
21 #include "qapi/qmp/qnull.h"
22 #include "sysemu/runstate.h"
23 #include "migration/colo.h"
24 #include "migration/misc.h"
25 #include "migration.h"
26 #include "migration-stats.h"
27 #include "qemu-file.h"
28 #include "ram.h"
29 #include "options.h"
31 /* Maximum migrate downtime set to 2000 seconds */
32 #define MAX_MIGRATE_DOWNTIME_SECONDS 2000
33 #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
35 #define MAX_THROTTLE (128 << 20) /* Migration transfer speed throttling */
37 /* Time in milliseconds we are allowed to stop the source,
38 * for sending the last part */
39 #define DEFAULT_MIGRATE_SET_DOWNTIME 300
41 /* Default compression thread count */
42 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
43 /* Default decompression thread count, usually decompression is at
44 * least 4 times as fast as compression.*/
45 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
46 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
47 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
48 /* Define default autoconverge cpu throttle migration parameters */
49 #define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50
50 #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
51 #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
52 #define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
54 /* Migration XBZRLE default cache size */
55 #define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
57 /* The delay time (in ms) between two COLO checkpoints */
58 #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
59 #define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
60 #define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
61 /* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
62 #define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
63 /* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
64 #define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
66 /* Background transfer rate for postcopy, 0 means unlimited, note
67 * that page requests can still exceed this limit.
69 #define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
72 * Parameters for self_announce_delay giving a stream of RARP/ARP
73 * packets after migration.
75 #define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50
76 #define DEFAULT_MIGRATE_ANNOUNCE_MAX 550
77 #define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5
78 #define DEFAULT_MIGRATE_ANNOUNCE_STEP 100
80 #define DEFINE_PROP_MIG_CAP(name, x) \
81 DEFINE_PROP_BOOL(name, MigrationState, capabilities[x], false)
83 Property migration_properties[] = {
84 DEFINE_PROP_BOOL("store-global-state", MigrationState,
85 store_global_state, true),
86 DEFINE_PROP_BOOL("send-configuration", MigrationState,
87 send_configuration, true),
88 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
89 send_section_footer, true),
90 DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
91 decompress_error_check, true),
92 DEFINE_PROP_BOOL("multifd-flush-after-each-section", MigrationState,
93 multifd_flush_after_each_section, false),
94 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
95 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
96 DEFINE_PROP_BOOL("x-preempt-pre-7-2", MigrationState,
97 preempt_pre_7_2, false),
99 /* Migration parameters */
100 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
101 parameters.compress_level,
102 DEFAULT_MIGRATE_COMPRESS_LEVEL),
103 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
104 parameters.compress_threads,
105 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
106 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
107 parameters.compress_wait_thread, true),
108 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
109 parameters.decompress_threads,
110 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
111 DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState,
112 parameters.throttle_trigger_threshold,
113 DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
114 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
115 parameters.cpu_throttle_initial,
116 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
117 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
118 parameters.cpu_throttle_increment,
119 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
120 DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
121 parameters.cpu_throttle_tailslow, false),
122 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
123 parameters.max_bandwidth, MAX_THROTTLE),
124 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
125 parameters.downtime_limit,
126 DEFAULT_MIGRATE_SET_DOWNTIME),
127 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
128 parameters.x_checkpoint_delay,
129 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
130 DEFINE_PROP_UINT8("multifd-channels", MigrationState,
131 parameters.multifd_channels,
132 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
133 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
134 parameters.multifd_compression,
135 DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
136 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
137 parameters.multifd_zlib_level,
138 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
139 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
140 parameters.multifd_zstd_level,
141 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
142 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
143 parameters.xbzrle_cache_size,
144 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
145 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
146 parameters.max_postcopy_bandwidth,
147 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
148 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
149 parameters.max_cpu_throttle,
150 DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
151 DEFINE_PROP_SIZE("announce-initial", MigrationState,
152 parameters.announce_initial,
153 DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
154 DEFINE_PROP_SIZE("announce-max", MigrationState,
155 parameters.announce_max,
156 DEFAULT_MIGRATE_ANNOUNCE_MAX),
157 DEFINE_PROP_SIZE("announce-rounds", MigrationState,
158 parameters.announce_rounds,
159 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
160 DEFINE_PROP_SIZE("announce-step", MigrationState,
161 parameters.announce_step,
162 DEFAULT_MIGRATE_ANNOUNCE_STEP),
163 DEFINE_PROP_STRING("tls-creds", MigrationState, parameters.tls_creds),
164 DEFINE_PROP_STRING("tls-hostname", MigrationState, parameters.tls_hostname),
165 DEFINE_PROP_STRING("tls-authz", MigrationState, parameters.tls_authz),
167 /* Migration capabilities */
168 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
169 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
170 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
171 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
172 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
173 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
174 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
175 DEFINE_PROP_MIG_CAP("x-postcopy-preempt",
176 MIGRATION_CAPABILITY_POSTCOPY_PREEMPT),
177 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
178 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
179 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
180 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
181 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
182 DEFINE_PROP_MIG_CAP("x-background-snapshot",
183 MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT),
184 #ifdef CONFIG_LINUX
185 DEFINE_PROP_MIG_CAP("x-zero-copy-send",
186 MIGRATION_CAPABILITY_ZERO_COPY_SEND),
187 #endif
188 DEFINE_PROP_MIG_CAP("x-switchover-ack",
189 MIGRATION_CAPABILITY_SWITCHOVER_ACK),
191 DEFINE_PROP_END_OF_LIST(),
194 bool migrate_auto_converge(void)
196 MigrationState *s = migrate_get_current();
198 return s->capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
201 bool migrate_background_snapshot(void)
203 MigrationState *s = migrate_get_current();
205 return s->capabilities[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT];
208 bool migrate_block(void)
210 MigrationState *s = migrate_get_current();
212 return s->capabilities[MIGRATION_CAPABILITY_BLOCK];
215 bool migrate_colo(void)
217 MigrationState *s = migrate_get_current();
219 return s->capabilities[MIGRATION_CAPABILITY_X_COLO];
222 bool migrate_compress(void)
224 MigrationState *s = migrate_get_current();
226 return s->capabilities[MIGRATION_CAPABILITY_COMPRESS];
229 bool migrate_dirty_bitmaps(void)
231 MigrationState *s = migrate_get_current();
233 return s->capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
236 bool migrate_events(void)
238 MigrationState *s = migrate_get_current();
240 return s->capabilities[MIGRATION_CAPABILITY_EVENTS];
243 bool migrate_ignore_shared(void)
245 MigrationState *s = migrate_get_current();
247 return s->capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
250 bool migrate_late_block_activate(void)
252 MigrationState *s = migrate_get_current();
254 return s->capabilities[MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
257 bool migrate_multifd(void)
259 MigrationState *s = migrate_get_current();
261 return s->capabilities[MIGRATION_CAPABILITY_MULTIFD];
264 bool migrate_pause_before_switchover(void)
266 MigrationState *s = migrate_get_current();
268 return s->capabilities[MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
271 bool migrate_postcopy_blocktime(void)
273 MigrationState *s = migrate_get_current();
275 return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
278 bool migrate_postcopy_preempt(void)
280 MigrationState *s = migrate_get_current();
282 return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT];
285 bool migrate_postcopy_ram(void)
287 MigrationState *s = migrate_get_current();
289 return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
292 bool migrate_rdma_pin_all(void)
294 MigrationState *s = migrate_get_current();
296 return s->capabilities[MIGRATION_CAPABILITY_RDMA_PIN_ALL];
299 bool migrate_release_ram(void)
301 MigrationState *s = migrate_get_current();
303 return s->capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
306 bool migrate_return_path(void)
308 MigrationState *s = migrate_get_current();
310 return s->capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
313 bool migrate_switchover_ack(void)
315 MigrationState *s = migrate_get_current();
317 return s->capabilities[MIGRATION_CAPABILITY_SWITCHOVER_ACK];
320 bool migrate_validate_uuid(void)
322 MigrationState *s = migrate_get_current();
324 return s->capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
327 bool migrate_xbzrle(void)
329 MigrationState *s = migrate_get_current();
331 return s->capabilities[MIGRATION_CAPABILITY_XBZRLE];
334 bool migrate_zero_blocks(void)
336 MigrationState *s = migrate_get_current();
338 return s->capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
341 bool migrate_zero_copy_send(void)
343 MigrationState *s = migrate_get_current();
345 return s->capabilities[MIGRATION_CAPABILITY_ZERO_COPY_SEND];
348 /* pseudo capabilities */
350 bool migrate_multifd_flush_after_each_section(void)
352 MigrationState *s = migrate_get_current();
354 return s->multifd_flush_after_each_section;
357 bool migrate_postcopy(void)
359 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
362 bool migrate_tls(void)
364 MigrationState *s = migrate_get_current();
366 return s->parameters.tls_creds && *s->parameters.tls_creds;
369 typedef enum WriteTrackingSupport {
370 WT_SUPPORT_UNKNOWN = 0,
371 WT_SUPPORT_ABSENT,
372 WT_SUPPORT_AVAILABLE,
373 WT_SUPPORT_COMPATIBLE
374 } WriteTrackingSupport;
376 static
377 WriteTrackingSupport migrate_query_write_tracking(void)
379 /* Check if kernel supports required UFFD features */
380 if (!ram_write_tracking_available()) {
381 return WT_SUPPORT_ABSENT;
384 * Check if current memory configuration is
385 * compatible with required UFFD features.
387 if (!ram_write_tracking_compatible()) {
388 return WT_SUPPORT_AVAILABLE;
391 return WT_SUPPORT_COMPATIBLE;
394 /* Migration capabilities set */
395 struct MigrateCapsSet {
396 int size; /* Capability set size */
397 MigrationCapability caps[]; /* Variadic array of capabilities */
399 typedef struct MigrateCapsSet MigrateCapsSet;
401 /* Define and initialize MigrateCapsSet */
402 #define INITIALIZE_MIGRATE_CAPS_SET(_name, ...) \
403 MigrateCapsSet _name = { \
404 .size = sizeof((int []) { __VA_ARGS__ }) / sizeof(int), \
405 .caps = { __VA_ARGS__ } \
408 /* Background-snapshot compatibility check list */
409 static const
410 INITIALIZE_MIGRATE_CAPS_SET(check_caps_background_snapshot,
411 MIGRATION_CAPABILITY_POSTCOPY_RAM,
412 MIGRATION_CAPABILITY_DIRTY_BITMAPS,
413 MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME,
414 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE,
415 MIGRATION_CAPABILITY_RETURN_PATH,
416 MIGRATION_CAPABILITY_MULTIFD,
417 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER,
418 MIGRATION_CAPABILITY_AUTO_CONVERGE,
419 MIGRATION_CAPABILITY_RELEASE_RAM,
420 MIGRATION_CAPABILITY_RDMA_PIN_ALL,
421 MIGRATION_CAPABILITY_COMPRESS,
422 MIGRATION_CAPABILITY_XBZRLE,
423 MIGRATION_CAPABILITY_X_COLO,
424 MIGRATION_CAPABILITY_VALIDATE_UUID,
425 MIGRATION_CAPABILITY_ZERO_COPY_SEND);
428 * @migration_caps_check - check capability compatibility
430 * @old_caps: old capability list
431 * @new_caps: new capability list
432 * @errp: set *errp if the check failed, with reason
434 * Returns true if check passed, otherwise false.
436 bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
438 MigrationIncomingState *mis = migration_incoming_get_current();
440 ERRP_GUARD();
441 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
442 if (new_caps[MIGRATION_CAPABILITY_BLOCK]) {
443 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
444 "block migration");
445 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
446 return false;
448 #endif
450 #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;
457 #endif
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.
464 if (!old_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM] &&
465 runstate_check(RUN_STATE_INMIGRATE) &&
466 !postcopy_ram_supported_by_host(mis, errp)) {
467 error_prepend(errp, "Postcopy is not supported: ");
468 return false;
471 if (new_caps[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
472 error_setg(errp, "Postcopy is not compatible with ignore-shared");
473 return false;
476 if (new_caps[MIGRATION_CAPABILITY_MULTIFD]) {
477 error_setg(errp, "Postcopy is not yet compatible with multifd");
478 return false;
482 if (new_caps[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT]) {
483 WriteTrackingSupport wt_support;
484 int idx;
486 * Check if 'background-snapshot' capability is supported by
487 * host kernel and compatible with guest memory configuration.
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;
494 if (wt_support < WT_SUPPORT_COMPATIBLE) {
495 error_setg(errp, "Background-snapshot is not compatible "
496 "with guest memory configuration");
497 return false;
501 * Check if there are any migration capabilities
502 * incompatible with 'background-snapshot'.
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;
515 #ifdef CONFIG_LINUX
516 if (new_caps[MIGRATION_CAPABILITY_ZERO_COPY_SEND] &&
517 (!new_caps[MIGRATION_CAPABILITY_MULTIFD] ||
518 new_caps[MIGRATION_CAPABILITY_COMPRESS] ||
519 new_caps[MIGRATION_CAPABILITY_XBZRLE] ||
520 migrate_multifd_compression() ||
521 migrate_tls())) {
522 error_setg(errp,
523 "Zero copy only available for non-compressed non-TLS multifd migration");
524 return false;
526 #else
527 if (new_caps[MIGRATION_CAPABILITY_ZERO_COPY_SEND]) {
528 error_setg(errp,
529 "Zero copy currently only available on Linux");
530 return false;
532 #endif
534 if (new_caps[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT]) {
535 if (!new_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
536 error_setg(errp, "Postcopy preempt requires postcopy-ram");
537 return false;
541 * Preempt mode requires urgent pages to be sent in separate
542 * channel, OTOH compression logic will disorder all pages into
543 * different compression channels, which is not compatible with the
544 * preempt assumptions on channel assignments.
546 if (new_caps[MIGRATION_CAPABILITY_COMPRESS]) {
547 error_setg(errp, "Postcopy preempt not compatible with compress");
548 return false;
552 if (new_caps[MIGRATION_CAPABILITY_MULTIFD]) {
553 if (new_caps[MIGRATION_CAPABILITY_COMPRESS]) {
554 error_setg(errp, "Multifd is not compatible with compress");
555 return false;
559 if (new_caps[MIGRATION_CAPABILITY_SWITCHOVER_ACK]) {
560 if (!new_caps[MIGRATION_CAPABILITY_RETURN_PATH]) {
561 error_setg(errp, "Capability 'switchover-ack' requires capability "
562 "'return-path'");
563 return false;
567 return true;
570 bool migrate_cap_set(int cap, bool value, Error **errp)
572 MigrationState *s = migrate_get_current();
573 bool new_caps[MIGRATION_CAPABILITY__MAX];
575 if (migration_is_running(s->state)) {
576 error_setg(errp, QERR_MIGRATION_ACTIVE);
577 return false;
580 memcpy(new_caps, s->capabilities, sizeof(new_caps));
581 new_caps[cap] = value;
583 if (!migrate_caps_check(s->capabilities, new_caps, errp)) {
584 return false;
586 s->capabilities[cap] = value;
587 return true;
590 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
592 MigrationCapabilityStatusList *head = NULL, **tail = &head;
593 MigrationCapabilityStatus *caps;
594 MigrationState *s = migrate_get_current();
595 int i;
597 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
598 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
599 if (i == MIGRATION_CAPABILITY_BLOCK) {
600 continue;
602 #endif
603 caps = g_malloc0(sizeof(*caps));
604 caps->capability = i;
605 caps->state = s->capabilities[i];
606 QAPI_LIST_APPEND(tail, caps);
609 return head;
612 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
613 Error **errp)
615 MigrationState *s = migrate_get_current();
616 MigrationCapabilityStatusList *cap;
617 bool new_caps[MIGRATION_CAPABILITY__MAX];
619 if (migration_is_running(s->state) || migration_in_colo_state()) {
620 error_setg(errp, QERR_MIGRATION_ACTIVE);
621 return;
624 memcpy(new_caps, s->capabilities, sizeof(new_caps));
625 for (cap = params; cap; cap = cap->next) {
626 new_caps[cap->value->capability] = cap->value->state;
629 if (!migrate_caps_check(s->capabilities, new_caps, errp)) {
630 return;
633 for (cap = params; cap; cap = cap->next) {
634 s->capabilities[cap->value->capability] = cap->value->state;
638 /* parameters */
640 const BitmapMigrationNodeAliasList *migrate_block_bitmap_mapping(void)
642 MigrationState *s = migrate_get_current();
644 return s->parameters.block_bitmap_mapping;
647 bool migrate_has_block_bitmap_mapping(void)
649 MigrationState *s = migrate_get_current();
651 return s->parameters.has_block_bitmap_mapping;
654 bool migrate_block_incremental(void)
656 MigrationState *s = migrate_get_current();
658 return s->parameters.block_incremental;
661 uint32_t migrate_checkpoint_delay(void)
663 MigrationState *s = migrate_get_current();
665 return s->parameters.x_checkpoint_delay;
668 int migrate_compress_level(void)
670 MigrationState *s = migrate_get_current();
672 return s->parameters.compress_level;
675 int migrate_compress_threads(void)
677 MigrationState *s = migrate_get_current();
679 return s->parameters.compress_threads;
682 int migrate_compress_wait_thread(void)
684 MigrationState *s = migrate_get_current();
686 return s->parameters.compress_wait_thread;
689 uint8_t migrate_cpu_throttle_increment(void)
691 MigrationState *s = migrate_get_current();
693 return s->parameters.cpu_throttle_increment;
696 uint8_t migrate_cpu_throttle_initial(void)
698 MigrationState *s = migrate_get_current();
700 return s->parameters.cpu_throttle_initial;
703 bool migrate_cpu_throttle_tailslow(void)
705 MigrationState *s = migrate_get_current();
707 return s->parameters.cpu_throttle_tailslow;
710 int migrate_decompress_threads(void)
712 MigrationState *s = migrate_get_current();
714 return s->parameters.decompress_threads;
717 uint64_t migrate_downtime_limit(void)
719 MigrationState *s = migrate_get_current();
721 return s->parameters.downtime_limit;
724 uint8_t migrate_max_cpu_throttle(void)
726 MigrationState *s = migrate_get_current();
728 return s->parameters.max_cpu_throttle;
731 uint64_t migrate_max_bandwidth(void)
733 MigrationState *s = migrate_get_current();
735 return s->parameters.max_bandwidth;
738 uint64_t migrate_max_postcopy_bandwidth(void)
740 MigrationState *s = migrate_get_current();
742 return s->parameters.max_postcopy_bandwidth;
745 int migrate_multifd_channels(void)
747 MigrationState *s = migrate_get_current();
749 return s->parameters.multifd_channels;
752 MultiFDCompression migrate_multifd_compression(void)
754 MigrationState *s = migrate_get_current();
756 assert(s->parameters.multifd_compression < MULTIFD_COMPRESSION__MAX);
757 return s->parameters.multifd_compression;
760 int migrate_multifd_zlib_level(void)
762 MigrationState *s = migrate_get_current();
764 return s->parameters.multifd_zlib_level;
767 int migrate_multifd_zstd_level(void)
769 MigrationState *s = migrate_get_current();
771 return s->parameters.multifd_zstd_level;
774 uint8_t migrate_throttle_trigger_threshold(void)
776 MigrationState *s = migrate_get_current();
778 return s->parameters.throttle_trigger_threshold;
781 const char *migrate_tls_authz(void)
783 MigrationState *s = migrate_get_current();
785 return s->parameters.tls_authz;
788 const char *migrate_tls_creds(void)
790 MigrationState *s = migrate_get_current();
792 return s->parameters.tls_creds;
795 const char *migrate_tls_hostname(void)
797 MigrationState *s = migrate_get_current();
799 return s->parameters.tls_hostname;
802 uint64_t migrate_xbzrle_cache_size(void)
804 MigrationState *s = migrate_get_current();
806 return s->parameters.xbzrle_cache_size;
809 /* parameter setters */
811 void migrate_set_block_incremental(bool value)
813 MigrationState *s = migrate_get_current();
815 s->parameters.block_incremental = value;
818 /* parameters helpers */
820 void block_cleanup_parameters(void)
822 MigrationState *s = migrate_get_current();
824 if (s->must_remove_block_options) {
825 /* setting to false can never fail */
826 migrate_cap_set(MIGRATION_CAPABILITY_BLOCK, false, &error_abort);
827 migrate_set_block_incremental(false);
828 s->must_remove_block_options = false;
832 AnnounceParameters *migrate_announce_params(void)
834 static AnnounceParameters ap;
836 MigrationState *s = migrate_get_current();
838 ap.initial = s->parameters.announce_initial;
839 ap.max = s->parameters.announce_max;
840 ap.rounds = s->parameters.announce_rounds;
841 ap.step = s->parameters.announce_step;
843 return &ap;
846 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
848 MigrationParameters *params;
849 MigrationState *s = migrate_get_current();
851 /* TODO use QAPI_CLONE() instead of duplicating it inline */
852 params = g_malloc0(sizeof(*params));
853 params->has_compress_level = true;
854 params->compress_level = s->parameters.compress_level;
855 params->has_compress_threads = true;
856 params->compress_threads = s->parameters.compress_threads;
857 params->has_compress_wait_thread = true;
858 params->compress_wait_thread = s->parameters.compress_wait_thread;
859 params->has_decompress_threads = true;
860 params->decompress_threads = s->parameters.decompress_threads;
861 params->has_throttle_trigger_threshold = true;
862 params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
863 params->has_cpu_throttle_initial = true;
864 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
865 params->has_cpu_throttle_increment = true;
866 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
867 params->has_cpu_throttle_tailslow = true;
868 params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
869 params->tls_creds = g_strdup(s->parameters.tls_creds);
870 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
871 params->tls_authz = g_strdup(s->parameters.tls_authz ?
872 s->parameters.tls_authz : "");
873 params->has_max_bandwidth = true;
874 params->max_bandwidth = s->parameters.max_bandwidth;
875 params->has_downtime_limit = true;
876 params->downtime_limit = s->parameters.downtime_limit;
877 params->has_x_checkpoint_delay = true;
878 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
879 params->has_block_incremental = true;
880 params->block_incremental = s->parameters.block_incremental;
881 params->has_multifd_channels = true;
882 params->multifd_channels = s->parameters.multifd_channels;
883 params->has_multifd_compression = true;
884 params->multifd_compression = s->parameters.multifd_compression;
885 params->has_multifd_zlib_level = true;
886 params->multifd_zlib_level = s->parameters.multifd_zlib_level;
887 params->has_multifd_zstd_level = true;
888 params->multifd_zstd_level = s->parameters.multifd_zstd_level;
889 params->has_xbzrle_cache_size = true;
890 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
891 params->has_max_postcopy_bandwidth = true;
892 params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
893 params->has_max_cpu_throttle = true;
894 params->max_cpu_throttle = s->parameters.max_cpu_throttle;
895 params->has_announce_initial = true;
896 params->announce_initial = s->parameters.announce_initial;
897 params->has_announce_max = true;
898 params->announce_max = s->parameters.announce_max;
899 params->has_announce_rounds = true;
900 params->announce_rounds = s->parameters.announce_rounds;
901 params->has_announce_step = true;
902 params->announce_step = s->parameters.announce_step;
904 if (s->parameters.has_block_bitmap_mapping) {
905 params->has_block_bitmap_mapping = true;
906 params->block_bitmap_mapping =
907 QAPI_CLONE(BitmapMigrationNodeAliasList,
908 s->parameters.block_bitmap_mapping);
911 return params;
914 void migrate_params_init(MigrationParameters *params)
916 params->tls_hostname = g_strdup("");
917 params->tls_creds = g_strdup("");
919 /* Set has_* up only for parameter checks */
920 params->has_compress_level = true;
921 params->has_compress_threads = true;
922 params->has_compress_wait_thread = true;
923 params->has_decompress_threads = true;
924 params->has_throttle_trigger_threshold = true;
925 params->has_cpu_throttle_initial = true;
926 params->has_cpu_throttle_increment = true;
927 params->has_cpu_throttle_tailslow = true;
928 params->has_max_bandwidth = true;
929 params->has_downtime_limit = true;
930 params->has_x_checkpoint_delay = true;
931 params->has_block_incremental = true;
932 params->has_multifd_channels = true;
933 params->has_multifd_compression = true;
934 params->has_multifd_zlib_level = true;
935 params->has_multifd_zstd_level = true;
936 params->has_xbzrle_cache_size = true;
937 params->has_max_postcopy_bandwidth = true;
938 params->has_max_cpu_throttle = true;
939 params->has_announce_initial = true;
940 params->has_announce_max = true;
941 params->has_announce_rounds = true;
942 params->has_announce_step = true;
946 * Check whether the parameters are valid. Error will be put into errp
947 * (if provided). Return true if valid, otherwise false.
949 bool migrate_params_check(MigrationParameters *params, Error **errp)
951 if (params->has_compress_level &&
952 (params->compress_level > 9)) {
953 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
954 "a value between 0 and 9");
955 return false;
958 if (params->has_compress_threads && (params->compress_threads < 1)) {
959 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
960 "compress_threads",
961 "a value between 1 and 255");
962 return false;
965 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
966 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
967 "decompress_threads",
968 "a value between 1 and 255");
969 return false;
972 if (params->has_throttle_trigger_threshold &&
973 (params->throttle_trigger_threshold < 1 ||
974 params->throttle_trigger_threshold > 100)) {
975 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
976 "throttle_trigger_threshold",
977 "an integer in the range of 1 to 100");
978 return false;
981 if (params->has_cpu_throttle_initial &&
982 (params->cpu_throttle_initial < 1 ||
983 params->cpu_throttle_initial > 99)) {
984 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
985 "cpu_throttle_initial",
986 "an integer in the range of 1 to 99");
987 return false;
990 if (params->has_cpu_throttle_increment &&
991 (params->cpu_throttle_increment < 1 ||
992 params->cpu_throttle_increment > 99)) {
993 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
994 "cpu_throttle_increment",
995 "an integer in the range of 1 to 99");
996 return false;
999 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
1000 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1001 "max_bandwidth",
1002 "an integer in the range of 0 to "stringify(SIZE_MAX)
1003 " bytes/second");
1004 return false;
1007 if (params->has_downtime_limit &&
1008 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
1009 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1010 "downtime_limit",
1011 "an integer in the range of 0 to "
1012 stringify(MAX_MIGRATE_DOWNTIME)" ms");
1013 return false;
1016 /* x_checkpoint_delay is now always positive */
1018 if (params->has_multifd_channels && (params->multifd_channels < 1)) {
1019 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1020 "multifd_channels",
1021 "a value between 1 and 255");
1022 return false;
1025 if (params->has_multifd_zlib_level &&
1026 (params->multifd_zlib_level > 9)) {
1027 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
1028 "a value between 0 and 9");
1029 return false;
1032 if (params->has_multifd_zstd_level &&
1033 (params->multifd_zstd_level > 20)) {
1034 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
1035 "a value between 0 and 20");
1036 return false;
1039 if (params->has_xbzrle_cache_size &&
1040 (params->xbzrle_cache_size < qemu_target_page_size() ||
1041 !is_power_of_2(params->xbzrle_cache_size))) {
1042 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1043 "xbzrle_cache_size",
1044 "a power of two no less than the target page size");
1045 return false;
1048 if (params->has_max_cpu_throttle &&
1049 (params->max_cpu_throttle < params->cpu_throttle_initial ||
1050 params->max_cpu_throttle > 99)) {
1051 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1052 "max_cpu_throttle",
1053 "an integer in the range of cpu_throttle_initial to 99");
1054 return false;
1057 if (params->has_announce_initial &&
1058 params->announce_initial > 100000) {
1059 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1060 "announce_initial",
1061 "a value between 0 and 100000");
1062 return false;
1064 if (params->has_announce_max &&
1065 params->announce_max > 100000) {
1066 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1067 "announce_max",
1068 "a value between 0 and 100000");
1069 return false;
1071 if (params->has_announce_rounds &&
1072 params->announce_rounds > 1000) {
1073 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1074 "announce_rounds",
1075 "a value between 0 and 1000");
1076 return false;
1078 if (params->has_announce_step &&
1079 (params->announce_step < 1 ||
1080 params->announce_step > 10000)) {
1081 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1082 "announce_step",
1083 "a value between 0 and 10000");
1084 return false;
1087 if (params->has_block_bitmap_mapping &&
1088 !check_dirty_bitmap_mig_alias_map(params->block_bitmap_mapping, errp)) {
1089 error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: ");
1090 return false;
1093 #ifdef CONFIG_LINUX
1094 if (migrate_zero_copy_send() &&
1095 ((params->has_multifd_compression && params->multifd_compression) ||
1096 (params->tls_creds && *params->tls_creds))) {
1097 error_setg(errp,
1098 "Zero copy only available for non-compressed non-TLS multifd migration");
1099 return false;
1101 #endif
1103 return true;
1106 static void migrate_params_test_apply(MigrateSetParameters *params,
1107 MigrationParameters *dest)
1109 *dest = migrate_get_current()->parameters;
1111 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1113 if (params->has_compress_level) {
1114 dest->compress_level = params->compress_level;
1117 if (params->has_compress_threads) {
1118 dest->compress_threads = params->compress_threads;
1121 if (params->has_compress_wait_thread) {
1122 dest->compress_wait_thread = params->compress_wait_thread;
1125 if (params->has_decompress_threads) {
1126 dest->decompress_threads = params->decompress_threads;
1129 if (params->has_throttle_trigger_threshold) {
1130 dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
1133 if (params->has_cpu_throttle_initial) {
1134 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1137 if (params->has_cpu_throttle_increment) {
1138 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1141 if (params->has_cpu_throttle_tailslow) {
1142 dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1145 if (params->tls_creds) {
1146 assert(params->tls_creds->type == QTYPE_QSTRING);
1147 dest->tls_creds = params->tls_creds->u.s;
1150 if (params->tls_hostname) {
1151 assert(params->tls_hostname->type == QTYPE_QSTRING);
1152 dest->tls_hostname = params->tls_hostname->u.s;
1155 if (params->has_max_bandwidth) {
1156 dest->max_bandwidth = params->max_bandwidth;
1159 if (params->has_downtime_limit) {
1160 dest->downtime_limit = params->downtime_limit;
1163 if (params->has_x_checkpoint_delay) {
1164 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1167 if (params->has_block_incremental) {
1168 dest->block_incremental = params->block_incremental;
1170 if (params->has_multifd_channels) {
1171 dest->multifd_channels = params->multifd_channels;
1173 if (params->has_multifd_compression) {
1174 dest->multifd_compression = params->multifd_compression;
1176 if (params->has_xbzrle_cache_size) {
1177 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1179 if (params->has_max_postcopy_bandwidth) {
1180 dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1182 if (params->has_max_cpu_throttle) {
1183 dest->max_cpu_throttle = params->max_cpu_throttle;
1185 if (params->has_announce_initial) {
1186 dest->announce_initial = params->announce_initial;
1188 if (params->has_announce_max) {
1189 dest->announce_max = params->announce_max;
1191 if (params->has_announce_rounds) {
1192 dest->announce_rounds = params->announce_rounds;
1194 if (params->has_announce_step) {
1195 dest->announce_step = params->announce_step;
1198 if (params->has_block_bitmap_mapping) {
1199 dest->has_block_bitmap_mapping = true;
1200 dest->block_bitmap_mapping = params->block_bitmap_mapping;
1204 static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
1206 MigrationState *s = migrate_get_current();
1208 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1210 if (params->has_compress_level) {
1211 s->parameters.compress_level = params->compress_level;
1214 if (params->has_compress_threads) {
1215 s->parameters.compress_threads = params->compress_threads;
1218 if (params->has_compress_wait_thread) {
1219 s->parameters.compress_wait_thread = params->compress_wait_thread;
1222 if (params->has_decompress_threads) {
1223 s->parameters.decompress_threads = params->decompress_threads;
1226 if (params->has_throttle_trigger_threshold) {
1227 s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
1230 if (params->has_cpu_throttle_initial) {
1231 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
1234 if (params->has_cpu_throttle_increment) {
1235 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
1238 if (params->has_cpu_throttle_tailslow) {
1239 s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1242 if (params->tls_creds) {
1243 g_free(s->parameters.tls_creds);
1244 assert(params->tls_creds->type == QTYPE_QSTRING);
1245 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
1248 if (params->tls_hostname) {
1249 g_free(s->parameters.tls_hostname);
1250 assert(params->tls_hostname->type == QTYPE_QSTRING);
1251 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
1254 if (params->tls_authz) {
1255 g_free(s->parameters.tls_authz);
1256 assert(params->tls_authz->type == QTYPE_QSTRING);
1257 s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1260 if (params->has_max_bandwidth) {
1261 s->parameters.max_bandwidth = params->max_bandwidth;
1262 if (s->to_dst_file && !migration_in_postcopy()) {
1263 migration_rate_set(s->parameters.max_bandwidth);
1267 if (params->has_downtime_limit) {
1268 s->parameters.downtime_limit = params->downtime_limit;
1271 if (params->has_x_checkpoint_delay) {
1272 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
1273 colo_checkpoint_delay_set();
1276 if (params->has_block_incremental) {
1277 s->parameters.block_incremental = params->block_incremental;
1279 if (params->has_multifd_channels) {
1280 s->parameters.multifd_channels = params->multifd_channels;
1282 if (params->has_multifd_compression) {
1283 s->parameters.multifd_compression = params->multifd_compression;
1285 if (params->has_xbzrle_cache_size) {
1286 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1287 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1289 if (params->has_max_postcopy_bandwidth) {
1290 s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1291 if (s->to_dst_file && migration_in_postcopy()) {
1292 migration_rate_set(s->parameters.max_postcopy_bandwidth);
1295 if (params->has_max_cpu_throttle) {
1296 s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1298 if (params->has_announce_initial) {
1299 s->parameters.announce_initial = params->announce_initial;
1301 if (params->has_announce_max) {
1302 s->parameters.announce_max = params->announce_max;
1304 if (params->has_announce_rounds) {
1305 s->parameters.announce_rounds = params->announce_rounds;
1307 if (params->has_announce_step) {
1308 s->parameters.announce_step = params->announce_step;
1311 if (params->has_block_bitmap_mapping) {
1312 qapi_free_BitmapMigrationNodeAliasList(
1313 s->parameters.block_bitmap_mapping);
1315 s->parameters.has_block_bitmap_mapping = true;
1316 s->parameters.block_bitmap_mapping =
1317 QAPI_CLONE(BitmapMigrationNodeAliasList,
1318 params->block_bitmap_mapping);
1322 void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
1324 MigrationParameters tmp;
1326 /* TODO Rewrite "" to null instead */
1327 if (params->tls_creds
1328 && params->tls_creds->type == QTYPE_QNULL) {
1329 qobject_unref(params->tls_creds->u.n);
1330 params->tls_creds->type = QTYPE_QSTRING;
1331 params->tls_creds->u.s = strdup("");
1333 /* TODO Rewrite "" to null instead */
1334 if (params->tls_hostname
1335 && params->tls_hostname->type == QTYPE_QNULL) {
1336 qobject_unref(params->tls_hostname->u.n);
1337 params->tls_hostname->type = QTYPE_QSTRING;
1338 params->tls_hostname->u.s = strdup("");
1341 migrate_params_test_apply(params, &tmp);
1343 if (!migrate_params_check(&tmp, errp)) {
1344 /* Invalid parameter */
1345 return;
1348 migrate_params_apply(params, errp);