xref: /qemu/migration/options.c (revision 476d6e4c9c4965734d6f47ee299ac9f84440a9b3)
1 /*
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"
15 #include "qemu/error-report.h"
16 #include "exec/target_page.h"
17 #include "qapi/clone-visitor.h"
18 #include "qapi/error.h"
19 #include "qapi/qapi-commands-migration.h"
20 #include "qapi/qapi-visit-migration.h"
21 #include "qapi/qmp/qerror.h"
22 #include "qapi/qmp/qnull.h"
23 #include "system/runstate.h"
24 #include "migration/colo.h"
25 #include "migration/cpr.h"
26 #include "migration/misc.h"
27 #include "migration.h"
28 #include "migration-stats.h"
29 #include "qemu-file.h"
30 #include "ram.h"
31 #include "options.h"
32 #include "system/kvm.h"
33 
34 /* Maximum migrate downtime set to 2000 seconds */
35 #define MAX_MIGRATE_DOWNTIME_SECONDS 2000
36 #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
37 
38 #define MAX_THROTTLE  (128 << 20)      /* Migration transfer speed throttling */
39 
40 /* Time in milliseconds we are allowed to stop the source,
41  * for sending the last part */
42 #define DEFAULT_MIGRATE_SET_DOWNTIME 300
43 
44 /* Define default autoconverge cpu throttle migration parameters */
45 #define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50
46 #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
47 #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
48 #define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
49 
50 /* Migration XBZRLE default cache size */
51 #define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
52 
53 /* The delay time (in ms) between two COLO checkpoints */
54 #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
55 #define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
56 #define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
57 /* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
58 #define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
59 /*
60  * 1: best speed, ... 9: best compress ratio
61  * There is some nuance here. Refer to QATzip documentation to understand
62  * the mapping of QATzip levels to standard deflate levels.
63  */
64 #define DEFAULT_MIGRATE_MULTIFD_QATZIP_LEVEL 1
65 
66 /* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
67 #define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
68 
69 /* Background transfer rate for postcopy, 0 means unlimited, note
70  * that page requests can still exceed this limit.
71  */
72 #define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
73 
74 /*
75  * Parameters for self_announce_delay giving a stream of RARP/ARP
76  * packets after migration.
77  */
78 #define DEFAULT_MIGRATE_ANNOUNCE_INITIAL  50
79 #define DEFAULT_MIGRATE_ANNOUNCE_MAX     550
80 #define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS    5
81 #define DEFAULT_MIGRATE_ANNOUNCE_STEP    100
82 
83 #define DEFINE_PROP_MIG_CAP(name, x)             \
84     DEFINE_PROP_BOOL(name, MigrationState, capabilities[x], false)
85 
86 #define DEFAULT_MIGRATE_VCPU_DIRTY_LIMIT_PERIOD     1000    /* milliseconds */
87 #define DEFAULT_MIGRATE_VCPU_DIRTY_LIMIT            1       /* MB/s */
88 
89 const Property migration_properties[] = {
90     DEFINE_PROP_BOOL("store-global-state", MigrationState,
91                      store_global_state, true),
92     DEFINE_PROP_BOOL("send-configuration", MigrationState,
93                      send_configuration, true),
94     DEFINE_PROP_BOOL("send-section-footer", MigrationState,
95                      send_section_footer, true),
96     DEFINE_PROP_BOOL("multifd-flush-after-each-section", MigrationState,
97                       multifd_flush_after_each_section, false),
98     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-throttle-trigger-threshold", MigrationState,
105                       parameters.throttle_trigger_threshold,
106                       DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
107     DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
108                       parameters.cpu_throttle_initial,
109                       DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
110     DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
111                       parameters.cpu_throttle_increment,
112                       DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
113     DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
114                       parameters.cpu_throttle_tailslow, false),
115     DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
116                       parameters.max_bandwidth, MAX_THROTTLE),
117     DEFINE_PROP_SIZE("avail-switchover-bandwidth", MigrationState,
118                       parameters.avail_switchover_bandwidth, 0),
119     DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
120                       parameters.downtime_limit,
121                       DEFAULT_MIGRATE_SET_DOWNTIME),
122     DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
123                       parameters.x_checkpoint_delay,
124                       DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
125     DEFINE_PROP_UINT8("multifd-channels", MigrationState,
126                       parameters.multifd_channels,
127                       DEFAULT_MIGRATE_MULTIFD_CHANNELS),
128     DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
129                       parameters.multifd_compression,
130                       DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
131     DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
132                       parameters.multifd_zlib_level,
133                       DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
134     DEFINE_PROP_UINT8("multifd-qatzip-level", MigrationState,
135                       parameters.multifd_qatzip_level,
136                       DEFAULT_MIGRATE_MULTIFD_QATZIP_LEVEL),
137     DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
138                       parameters.multifd_zstd_level,
139                       DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
140     DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
141                       parameters.xbzrle_cache_size,
142                       DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
143     DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
144                       parameters.max_postcopy_bandwidth,
145                       DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
146     DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
147                       parameters.max_cpu_throttle,
148                       DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
149     DEFINE_PROP_SIZE("announce-initial", MigrationState,
150                       parameters.announce_initial,
151                       DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
152     DEFINE_PROP_SIZE("announce-max", MigrationState,
153                       parameters.announce_max,
154                       DEFAULT_MIGRATE_ANNOUNCE_MAX),
155     DEFINE_PROP_SIZE("announce-rounds", MigrationState,
156                       parameters.announce_rounds,
157                       DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
158     DEFINE_PROP_SIZE("announce-step", MigrationState,
159                       parameters.announce_step,
160                       DEFAULT_MIGRATE_ANNOUNCE_STEP),
161     DEFINE_PROP_STRING("tls-creds", MigrationState, parameters.tls_creds),
162     DEFINE_PROP_STRING("tls-hostname", MigrationState, parameters.tls_hostname),
163     DEFINE_PROP_STRING("tls-authz", MigrationState, parameters.tls_authz),
164     DEFINE_PROP_UINT64("x-vcpu-dirty-limit-period", MigrationState,
165                        parameters.x_vcpu_dirty_limit_period,
166                        DEFAULT_MIGRATE_VCPU_DIRTY_LIMIT_PERIOD),
167     DEFINE_PROP_UINT64("vcpu-dirty-limit", MigrationState,
168                        parameters.vcpu_dirty_limit,
169                        DEFAULT_MIGRATE_VCPU_DIRTY_LIMIT),
170     DEFINE_PROP_MIG_MODE("mode", MigrationState,
171                       parameters.mode,
172                       MIG_MODE_NORMAL),
173     DEFINE_PROP_ZERO_PAGE_DETECTION("zero-page-detection", MigrationState,
174                        parameters.zero_page_detection,
175                        ZERO_PAGE_DETECTION_MULTIFD),
176 
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-events", MIGRATION_CAPABILITY_EVENTS),
183     DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
184     DEFINE_PROP_MIG_CAP("x-postcopy-preempt",
185                         MIGRATION_CAPABILITY_POSTCOPY_PREEMPT),
186     DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
187     DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
188     DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
189     DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
190     DEFINE_PROP_MIG_CAP("x-background-snapshot",
191             MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT),
192 #ifdef CONFIG_LINUX
193     DEFINE_PROP_MIG_CAP("x-zero-copy-send",
194             MIGRATION_CAPABILITY_ZERO_COPY_SEND),
195 #endif
196     DEFINE_PROP_MIG_CAP("x-switchover-ack",
197                         MIGRATION_CAPABILITY_SWITCHOVER_ACK),
198     DEFINE_PROP_MIG_CAP("x-dirty-limit", MIGRATION_CAPABILITY_DIRTY_LIMIT),
199     DEFINE_PROP_MIG_CAP("mapped-ram", MIGRATION_CAPABILITY_MAPPED_RAM),
200 };
201 const size_t migration_properties_count = ARRAY_SIZE(migration_properties);
202 
203 bool migrate_auto_converge(void)
204 {
205     MigrationState *s = migrate_get_current();
206 
207     return s->capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
208 }
209 
210 bool migrate_background_snapshot(void)
211 {
212     MigrationState *s = migrate_get_current();
213 
214     return s->capabilities[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT];
215 }
216 
217 bool migrate_colo(void)
218 {
219     MigrationState *s = migrate_get_current();
220 
221     return s->capabilities[MIGRATION_CAPABILITY_X_COLO];
222 }
223 
224 bool migrate_dirty_bitmaps(void)
225 {
226     MigrationState *s = migrate_get_current();
227 
228     return s->capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
229 }
230 
231 bool migrate_dirty_limit(void)
232 {
233     MigrationState *s = migrate_get_current();
234 
235     return s->capabilities[MIGRATION_CAPABILITY_DIRTY_LIMIT];
236 }
237 
238 bool migrate_events(void)
239 {
240     MigrationState *s = migrate_get_current();
241 
242     return s->capabilities[MIGRATION_CAPABILITY_EVENTS];
243 }
244 
245 bool migrate_mapped_ram(void)
246 {
247     MigrationState *s = migrate_get_current();
248 
249     return s->capabilities[MIGRATION_CAPABILITY_MAPPED_RAM];
250 }
251 
252 bool migrate_ignore_shared(void)
253 {
254     MigrationState *s = migrate_get_current();
255 
256     return s->capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
257 }
258 
259 bool migrate_late_block_activate(void)
260 {
261     MigrationState *s = migrate_get_current();
262 
263     return s->capabilities[MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
264 }
265 
266 bool migrate_multifd(void)
267 {
268     MigrationState *s = migrate_get_current();
269 
270     return s->capabilities[MIGRATION_CAPABILITY_MULTIFD];
271 }
272 
273 bool migrate_pause_before_switchover(void)
274 {
275     MigrationState *s = migrate_get_current();
276 
277     return s->capabilities[MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
278 }
279 
280 bool migrate_postcopy_blocktime(void)
281 {
282     MigrationState *s = migrate_get_current();
283 
284     return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
285 }
286 
287 bool migrate_postcopy_preempt(void)
288 {
289     MigrationState *s = migrate_get_current();
290 
291     return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT];
292 }
293 
294 bool migrate_postcopy_ram(void)
295 {
296     MigrationState *s = migrate_get_current();
297 
298     return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
299 }
300 
301 bool migrate_rdma_pin_all(void)
302 {
303     MigrationState *s = migrate_get_current();
304 
305     return s->capabilities[MIGRATION_CAPABILITY_RDMA_PIN_ALL];
306 }
307 
308 bool migrate_release_ram(void)
309 {
310     MigrationState *s = migrate_get_current();
311 
312     return s->capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
313 }
314 
315 bool migrate_return_path(void)
316 {
317     MigrationState *s = migrate_get_current();
318 
319     return s->capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
320 }
321 
322 bool migrate_switchover_ack(void)
323 {
324     MigrationState *s = migrate_get_current();
325 
326     return s->capabilities[MIGRATION_CAPABILITY_SWITCHOVER_ACK];
327 }
328 
329 bool migrate_validate_uuid(void)
330 {
331     MigrationState *s = migrate_get_current();
332 
333     return s->capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
334 }
335 
336 bool migrate_xbzrle(void)
337 {
338     MigrationState *s = migrate_get_current();
339 
340     return s->capabilities[MIGRATION_CAPABILITY_XBZRLE];
341 }
342 
343 bool migrate_zero_copy_send(void)
344 {
345     MigrationState *s = migrate_get_current();
346 
347     return s->capabilities[MIGRATION_CAPABILITY_ZERO_COPY_SEND];
348 }
349 
350 /* pseudo capabilities */
351 
352 bool migrate_multifd_flush_after_each_section(void)
353 {
354     MigrationState *s = migrate_get_current();
355 
356     return s->multifd_flush_after_each_section;
357 }
358 
359 bool migrate_postcopy(void)
360 {
361     return migrate_postcopy_ram() || migrate_dirty_bitmaps();
362 }
363 
364 bool migrate_rdma(void)
365 {
366     MigrationState *s = migrate_get_current();
367 
368     return s->rdma_migration;
369 }
370 
371 bool migrate_tls(void)
372 {
373     MigrationState *s = migrate_get_current();
374 
375     return s->parameters.tls_creds && *s->parameters.tls_creds;
376 }
377 
378 typedef enum WriteTrackingSupport {
379     WT_SUPPORT_UNKNOWN = 0,
380     WT_SUPPORT_ABSENT,
381     WT_SUPPORT_AVAILABLE,
382     WT_SUPPORT_COMPATIBLE
383 } WriteTrackingSupport;
384 
385 static
386 WriteTrackingSupport migrate_query_write_tracking(void)
387 {
388     /* Check if kernel supports required UFFD features */
389     if (!ram_write_tracking_available()) {
390         return WT_SUPPORT_ABSENT;
391     }
392     /*
393      * Check if current memory configuration is
394      * compatible with required UFFD features.
395      */
396     if (!ram_write_tracking_compatible()) {
397         return WT_SUPPORT_AVAILABLE;
398     }
399 
400     return WT_SUPPORT_COMPATIBLE;
401 }
402 
403 /* Migration capabilities set */
404 struct MigrateCapsSet {
405     int size;                       /* Capability set size */
406     MigrationCapability caps[];     /* Variadic array of capabilities */
407 };
408 typedef struct MigrateCapsSet MigrateCapsSet;
409 
410 /* Define and initialize MigrateCapsSet */
411 #define INITIALIZE_MIGRATE_CAPS_SET(_name, ...)   \
412     MigrateCapsSet _name = {    \
413         .size = sizeof((int []) { __VA_ARGS__ }) / sizeof(int), \
414         .caps = { __VA_ARGS__ } \
415     }
416 
417 /* Background-snapshot compatibility check list */
418 static const
419 INITIALIZE_MIGRATE_CAPS_SET(check_caps_background_snapshot,
420     MIGRATION_CAPABILITY_POSTCOPY_RAM,
421     MIGRATION_CAPABILITY_DIRTY_BITMAPS,
422     MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME,
423     MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE,
424     MIGRATION_CAPABILITY_RETURN_PATH,
425     MIGRATION_CAPABILITY_MULTIFD,
426     MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER,
427     MIGRATION_CAPABILITY_AUTO_CONVERGE,
428     MIGRATION_CAPABILITY_RELEASE_RAM,
429     MIGRATION_CAPABILITY_RDMA_PIN_ALL,
430     MIGRATION_CAPABILITY_XBZRLE,
431     MIGRATION_CAPABILITY_X_COLO,
432     MIGRATION_CAPABILITY_VALIDATE_UUID,
433     MIGRATION_CAPABILITY_ZERO_COPY_SEND);
434 
435 static bool migrate_incoming_started(void)
436 {
437     return !!migration_incoming_get_current()->transport_data;
438 }
439 
440 /**
441  * @migration_caps_check - check capability compatibility
442  *
443  * @old_caps: old capability list
444  * @new_caps: new capability list
445  * @errp: set *errp if the check failed, with reason
446  *
447  * Returns true if check passed, otherwise false.
448  */
449 bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
450 {
451     ERRP_GUARD();
452     MigrationIncomingState *mis = migration_incoming_get_current();
453 
454     if (new_caps[MIGRATION_CAPABILITY_ZERO_BLOCKS]) {
455         warn_report("zero-blocks capability is deprecated");
456     }
457 
458 #ifndef CONFIG_REPLICATION
459     if (new_caps[MIGRATION_CAPABILITY_X_COLO]) {
460         error_setg(errp, "QEMU compiled without replication module"
461                    " can't enable COLO");
462         error_append_hint(errp, "Please enable replication before COLO.\n");
463         return false;
464     }
465 #endif
466 
467     if (new_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
468         /* This check is reasonably expensive, so only when it's being
469          * set the first time, also it's only the destination that needs
470          * special support.
471          */
472         if (!old_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM] &&
473             runstate_check(RUN_STATE_INMIGRATE) &&
474             !postcopy_ram_supported_by_host(mis, errp)) {
475             error_prepend(errp, "Postcopy is not supported: ");
476             return false;
477         }
478 
479         if (new_caps[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
480             error_setg(errp, "Postcopy is not compatible with ignore-shared");
481             return false;
482         }
483 
484         if (new_caps[MIGRATION_CAPABILITY_MULTIFD]) {
485             error_setg(errp, "Postcopy is not yet compatible with multifd");
486             return false;
487         }
488     }
489 
490     if (new_caps[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT]) {
491         WriteTrackingSupport wt_support;
492         int idx;
493         /*
494          * Check if 'background-snapshot' capability is supported by
495          * host kernel and compatible with guest memory configuration.
496          */
497         wt_support = migrate_query_write_tracking();
498         if (wt_support < WT_SUPPORT_AVAILABLE) {
499             error_setg(errp, "Background-snapshot is not supported by host kernel");
500             return false;
501         }
502         if (wt_support < WT_SUPPORT_COMPATIBLE) {
503             error_setg(errp, "Background-snapshot is not compatible "
504                     "with guest memory configuration");
505             return false;
506         }
507 
508         /*
509          * Check if there are any migration capabilities
510          * incompatible with 'background-snapshot'.
511          */
512         for (idx = 0; idx < check_caps_background_snapshot.size; idx++) {
513             int incomp_cap = check_caps_background_snapshot.caps[idx];
514             if (new_caps[incomp_cap]) {
515                 error_setg(errp,
516                         "Background-snapshot is not compatible with %s",
517                         MigrationCapability_str(incomp_cap));
518                 return false;
519             }
520         }
521     }
522 
523 #ifdef CONFIG_LINUX
524     if (new_caps[MIGRATION_CAPABILITY_ZERO_COPY_SEND] &&
525         (!new_caps[MIGRATION_CAPABILITY_MULTIFD] ||
526          new_caps[MIGRATION_CAPABILITY_XBZRLE] ||
527          migrate_multifd_compression() ||
528          migrate_tls())) {
529         error_setg(errp,
530                    "Zero copy only available for non-compressed non-TLS multifd migration");
531         return false;
532     }
533 #else
534     if (new_caps[MIGRATION_CAPABILITY_ZERO_COPY_SEND]) {
535         error_setg(errp,
536                    "Zero copy currently only available on Linux");
537         return false;
538     }
539 #endif
540 
541     if (new_caps[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT]) {
542         if (!new_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
543             error_setg(errp, "Postcopy preempt requires postcopy-ram");
544             return false;
545         }
546 
547         if (migrate_incoming_started()) {
548             error_setg(errp,
549                        "Postcopy preempt must be set before incoming starts");
550             return false;
551         }
552     }
553 
554     if (new_caps[MIGRATION_CAPABILITY_MULTIFD]) {
555         if (migrate_incoming_started()) {
556             error_setg(errp, "Multifd must be set before incoming starts");
557             return false;
558         }
559     }
560 
561     if (new_caps[MIGRATION_CAPABILITY_SWITCHOVER_ACK]) {
562         if (!new_caps[MIGRATION_CAPABILITY_RETURN_PATH]) {
563             error_setg(errp, "Capability 'switchover-ack' requires capability "
564                              "'return-path'");
565             return false;
566         }
567     }
568     if (new_caps[MIGRATION_CAPABILITY_DIRTY_LIMIT]) {
569         if (new_caps[MIGRATION_CAPABILITY_AUTO_CONVERGE]) {
570             error_setg(errp, "dirty-limit conflicts with auto-converge"
571                        " either of then available currently");
572             return false;
573         }
574 
575         if (!kvm_enabled() || !kvm_dirty_ring_enabled()) {
576             error_setg(errp, "dirty-limit requires KVM with accelerator"
577                    " property 'dirty-ring-size' set");
578             return false;
579         }
580     }
581 
582     if (new_caps[MIGRATION_CAPABILITY_MULTIFD]) {
583         if (new_caps[MIGRATION_CAPABILITY_XBZRLE]) {
584             error_setg(errp, "Multifd is not compatible with xbzrle");
585             return false;
586         }
587     }
588 
589     if (new_caps[MIGRATION_CAPABILITY_MAPPED_RAM]) {
590         if (new_caps[MIGRATION_CAPABILITY_XBZRLE]) {
591             error_setg(errp,
592                        "Mapped-ram migration is incompatible with xbzrle");
593             return false;
594         }
595 
596         if (new_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
597             error_setg(errp,
598                        "Mapped-ram migration is incompatible with postcopy");
599             return false;
600         }
601     }
602 
603     return true;
604 }
605 
606 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
607 {
608     MigrationCapabilityStatusList *head = NULL, **tail = &head;
609     MigrationCapabilityStatus *caps;
610     MigrationState *s = migrate_get_current();
611     int i;
612 
613     for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
614         caps = g_malloc0(sizeof(*caps));
615         caps->capability = i;
616         caps->state = s->capabilities[i];
617         QAPI_LIST_APPEND(tail, caps);
618     }
619 
620     return head;
621 }
622 
623 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
624                                   Error **errp)
625 {
626     MigrationState *s = migrate_get_current();
627     MigrationCapabilityStatusList *cap;
628     bool new_caps[MIGRATION_CAPABILITY__MAX];
629 
630     if (migration_is_running() || migration_in_colo_state()) {
631         error_setg(errp, "There's a migration process in progress");
632         return;
633     }
634 
635     memcpy(new_caps, s->capabilities, sizeof(new_caps));
636     for (cap = params; cap; cap = cap->next) {
637         new_caps[cap->value->capability] = cap->value->state;
638     }
639 
640     if (!migrate_caps_check(s->capabilities, new_caps, errp)) {
641         return;
642     }
643 
644     for (cap = params; cap; cap = cap->next) {
645         s->capabilities[cap->value->capability] = cap->value->state;
646     }
647 }
648 
649 /* parameters */
650 
651 const BitmapMigrationNodeAliasList *migrate_block_bitmap_mapping(void)
652 {
653     MigrationState *s = migrate_get_current();
654 
655     return s->parameters.block_bitmap_mapping;
656 }
657 
658 bool migrate_has_block_bitmap_mapping(void)
659 {
660     MigrationState *s = migrate_get_current();
661 
662     return s->parameters.has_block_bitmap_mapping;
663 }
664 
665 uint32_t migrate_checkpoint_delay(void)
666 {
667     MigrationState *s = migrate_get_current();
668 
669     return s->parameters.x_checkpoint_delay;
670 }
671 
672 uint8_t migrate_cpu_throttle_increment(void)
673 {
674     MigrationState *s = migrate_get_current();
675 
676     return s->parameters.cpu_throttle_increment;
677 }
678 
679 uint8_t migrate_cpu_throttle_initial(void)
680 {
681     MigrationState *s = migrate_get_current();
682 
683     return s->parameters.cpu_throttle_initial;
684 }
685 
686 bool migrate_cpu_throttle_tailslow(void)
687 {
688     MigrationState *s = migrate_get_current();
689 
690     return s->parameters.cpu_throttle_tailslow;
691 }
692 
693 bool migrate_direct_io(void)
694 {
695     MigrationState *s = migrate_get_current();
696 
697     /*
698      * O_DIRECT is only supported with mapped-ram and multifd.
699      *
700      * mapped-ram is needed because filesystems impose restrictions on
701      * O_DIRECT IO alignment (see MAPPED_RAM_FILE_OFFSET_ALIGNMENT).
702      *
703      * multifd is needed to keep the unaligned portion of the stream
704      * isolated to the main migration thread while multifd channels
705      * process the aligned data with O_DIRECT enabled.
706      */
707     return s->parameters.direct_io &&
708         s->capabilities[MIGRATION_CAPABILITY_MAPPED_RAM] &&
709         s->capabilities[MIGRATION_CAPABILITY_MULTIFD];
710 }
711 
712 uint64_t migrate_downtime_limit(void)
713 {
714     MigrationState *s = migrate_get_current();
715 
716     return s->parameters.downtime_limit;
717 }
718 
719 uint8_t migrate_max_cpu_throttle(void)
720 {
721     MigrationState *s = migrate_get_current();
722 
723     return s->parameters.max_cpu_throttle;
724 }
725 
726 uint64_t migrate_max_bandwidth(void)
727 {
728     MigrationState *s = migrate_get_current();
729 
730     return s->parameters.max_bandwidth;
731 }
732 
733 uint64_t migrate_avail_switchover_bandwidth(void)
734 {
735     MigrationState *s = migrate_get_current();
736 
737     return s->parameters.avail_switchover_bandwidth;
738 }
739 
740 uint64_t migrate_max_postcopy_bandwidth(void)
741 {
742     MigrationState *s = migrate_get_current();
743 
744     return s->parameters.max_postcopy_bandwidth;
745 }
746 
747 MigMode migrate_mode(void)
748 {
749     MigMode mode = cpr_get_incoming_mode();
750 
751     if (mode == MIG_MODE_NONE) {
752         mode = migrate_get_current()->parameters.mode;
753     }
754 
755     assert(mode >= 0 && mode < MIG_MODE__MAX);
756     return mode;
757 }
758 
759 int migrate_multifd_channels(void)
760 {
761     MigrationState *s = migrate_get_current();
762 
763     return s->parameters.multifd_channels;
764 }
765 
766 MultiFDCompression migrate_multifd_compression(void)
767 {
768     MigrationState *s = migrate_get_current();
769 
770     assert(s->parameters.multifd_compression < MULTIFD_COMPRESSION__MAX);
771     return s->parameters.multifd_compression;
772 }
773 
774 int migrate_multifd_zlib_level(void)
775 {
776     MigrationState *s = migrate_get_current();
777 
778     return s->parameters.multifd_zlib_level;
779 }
780 
781 int migrate_multifd_qatzip_level(void)
782 {
783     MigrationState *s = migrate_get_current();
784 
785     return s->parameters.multifd_qatzip_level;
786 }
787 
788 int migrate_multifd_zstd_level(void)
789 {
790     MigrationState *s = migrate_get_current();
791 
792     return s->parameters.multifd_zstd_level;
793 }
794 
795 uint8_t migrate_throttle_trigger_threshold(void)
796 {
797     MigrationState *s = migrate_get_current();
798 
799     return s->parameters.throttle_trigger_threshold;
800 }
801 
802 const char *migrate_tls_authz(void)
803 {
804     MigrationState *s = migrate_get_current();
805 
806     return s->parameters.tls_authz;
807 }
808 
809 const char *migrate_tls_creds(void)
810 {
811     MigrationState *s = migrate_get_current();
812 
813     return s->parameters.tls_creds;
814 }
815 
816 const char *migrate_tls_hostname(void)
817 {
818     MigrationState *s = migrate_get_current();
819 
820     return s->parameters.tls_hostname;
821 }
822 
823 uint64_t migrate_vcpu_dirty_limit_period(void)
824 {
825     MigrationState *s = migrate_get_current();
826 
827     return s->parameters.x_vcpu_dirty_limit_period;
828 }
829 
830 uint64_t migrate_xbzrle_cache_size(void)
831 {
832     MigrationState *s = migrate_get_current();
833 
834     return s->parameters.xbzrle_cache_size;
835 }
836 
837 ZeroPageDetection migrate_zero_page_detection(void)
838 {
839     MigrationState *s = migrate_get_current();
840 
841     return s->parameters.zero_page_detection;
842 }
843 
844 /* parameters helpers */
845 
846 AnnounceParameters *migrate_announce_params(void)
847 {
848     static AnnounceParameters ap;
849 
850     MigrationState *s = migrate_get_current();
851 
852     ap.initial = s->parameters.announce_initial;
853     ap.max = s->parameters.announce_max;
854     ap.rounds = s->parameters.announce_rounds;
855     ap.step = s->parameters.announce_step;
856 
857     return &ap;
858 }
859 
860 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
861 {
862     MigrationParameters *params;
863     MigrationState *s = migrate_get_current();
864 
865     /* TODO use QAPI_CLONE() instead of duplicating it inline */
866     params = g_malloc0(sizeof(*params));
867     params->has_throttle_trigger_threshold = true;
868     params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
869     params->has_cpu_throttle_initial = true;
870     params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
871     params->has_cpu_throttle_increment = true;
872     params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
873     params->has_cpu_throttle_tailslow = true;
874     params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
875     params->tls_creds = g_strdup(s->parameters.tls_creds);
876     params->tls_hostname = g_strdup(s->parameters.tls_hostname);
877     params->tls_authz = g_strdup(s->parameters.tls_authz ?
878                                  s->parameters.tls_authz : "");
879     params->has_max_bandwidth = true;
880     params->max_bandwidth = s->parameters.max_bandwidth;
881     params->has_avail_switchover_bandwidth = true;
882     params->avail_switchover_bandwidth = s->parameters.avail_switchover_bandwidth;
883     params->has_downtime_limit = true;
884     params->downtime_limit = s->parameters.downtime_limit;
885     params->has_x_checkpoint_delay = true;
886     params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
887     params->has_multifd_channels = true;
888     params->multifd_channels = s->parameters.multifd_channels;
889     params->has_multifd_compression = true;
890     params->multifd_compression = s->parameters.multifd_compression;
891     params->has_multifd_zlib_level = true;
892     params->multifd_zlib_level = s->parameters.multifd_zlib_level;
893     params->has_multifd_qatzip_level = true;
894     params->multifd_qatzip_level = s->parameters.multifd_qatzip_level;
895     params->has_multifd_zstd_level = true;
896     params->multifd_zstd_level = s->parameters.multifd_zstd_level;
897     params->has_xbzrle_cache_size = true;
898     params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
899     params->has_max_postcopy_bandwidth = true;
900     params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
901     params->has_max_cpu_throttle = true;
902     params->max_cpu_throttle = s->parameters.max_cpu_throttle;
903     params->has_announce_initial = true;
904     params->announce_initial = s->parameters.announce_initial;
905     params->has_announce_max = true;
906     params->announce_max = s->parameters.announce_max;
907     params->has_announce_rounds = true;
908     params->announce_rounds = s->parameters.announce_rounds;
909     params->has_announce_step = true;
910     params->announce_step = s->parameters.announce_step;
911 
912     if (s->parameters.has_block_bitmap_mapping) {
913         params->has_block_bitmap_mapping = true;
914         params->block_bitmap_mapping =
915             QAPI_CLONE(BitmapMigrationNodeAliasList,
916                        s->parameters.block_bitmap_mapping);
917     }
918 
919     params->has_x_vcpu_dirty_limit_period = true;
920     params->x_vcpu_dirty_limit_period = s->parameters.x_vcpu_dirty_limit_period;
921     params->has_vcpu_dirty_limit = true;
922     params->vcpu_dirty_limit = s->parameters.vcpu_dirty_limit;
923     params->has_mode = true;
924     params->mode = s->parameters.mode;
925     params->has_zero_page_detection = true;
926     params->zero_page_detection = s->parameters.zero_page_detection;
927     params->has_direct_io = true;
928     params->direct_io = s->parameters.direct_io;
929 
930     return params;
931 }
932 
933 void migrate_params_init(MigrationParameters *params)
934 {
935     params->tls_hostname = g_strdup("");
936     params->tls_creds = g_strdup("");
937 
938     /* Set has_* up only for parameter checks */
939     params->has_throttle_trigger_threshold = true;
940     params->has_cpu_throttle_initial = true;
941     params->has_cpu_throttle_increment = true;
942     params->has_cpu_throttle_tailslow = true;
943     params->has_max_bandwidth = true;
944     params->has_downtime_limit = true;
945     params->has_x_checkpoint_delay = true;
946     params->has_multifd_channels = true;
947     params->has_multifd_compression = true;
948     params->has_multifd_zlib_level = true;
949     params->has_multifd_qatzip_level = true;
950     params->has_multifd_zstd_level = true;
951     params->has_xbzrle_cache_size = true;
952     params->has_max_postcopy_bandwidth = true;
953     params->has_max_cpu_throttle = true;
954     params->has_announce_initial = true;
955     params->has_announce_max = true;
956     params->has_announce_rounds = true;
957     params->has_announce_step = true;
958     params->has_x_vcpu_dirty_limit_period = true;
959     params->has_vcpu_dirty_limit = true;
960     params->has_mode = true;
961     params->has_zero_page_detection = true;
962     params->has_direct_io = true;
963 }
964 
965 /*
966  * Check whether the parameters are valid. Error will be put into errp
967  * (if provided). Return true if valid, otherwise false.
968  */
969 bool migrate_params_check(MigrationParameters *params, Error **errp)
970 {
971     ERRP_GUARD();
972 
973     if (params->has_throttle_trigger_threshold &&
974         (params->throttle_trigger_threshold < 1 ||
975          params->throttle_trigger_threshold > 100)) {
976         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
977                    "throttle_trigger_threshold",
978                    "an integer in the range of 1 to 100");
979         return false;
980     }
981 
982     if (params->has_cpu_throttle_initial &&
983         (params->cpu_throttle_initial < 1 ||
984          params->cpu_throttle_initial > 99)) {
985         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
986                    "cpu_throttle_initial",
987                    "an integer in the range of 1 to 99");
988         return false;
989     }
990 
991     if (params->has_cpu_throttle_increment &&
992         (params->cpu_throttle_increment < 1 ||
993          params->cpu_throttle_increment > 99)) {
994         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
995                    "cpu_throttle_increment",
996                    "an integer in the range of 1 to 99");
997         return false;
998     }
999 
1000     if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
1001         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1002                    "max_bandwidth",
1003                    "an integer in the range of 0 to "stringify(SIZE_MAX)
1004                    " bytes/second");
1005         return false;
1006     }
1007 
1008     if (params->has_avail_switchover_bandwidth &&
1009         (params->avail_switchover_bandwidth > SIZE_MAX)) {
1010         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1011                    "avail_switchover_bandwidth",
1012                    "an integer in the range of 0 to "stringify(SIZE_MAX)
1013                    " bytes/second");
1014         return false;
1015     }
1016 
1017     if (params->has_downtime_limit &&
1018         (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
1019         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1020                    "downtime_limit",
1021                    "an integer in the range of 0 to "
1022                     stringify(MAX_MIGRATE_DOWNTIME)" ms");
1023         return false;
1024     }
1025 
1026     /* x_checkpoint_delay is now always positive */
1027 
1028     if (params->has_multifd_channels && (params->multifd_channels < 1)) {
1029         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1030                    "multifd_channels",
1031                    "a value between 1 and 255");
1032         return false;
1033     }
1034 
1035     if (params->has_multifd_zlib_level &&
1036         (params->multifd_zlib_level > 9)) {
1037         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
1038                    "a value between 0 and 9");
1039         return false;
1040     }
1041 
1042     if (params->has_multifd_qatzip_level &&
1043         ((params->multifd_qatzip_level > 9) ||
1044         (params->multifd_qatzip_level < 1))) {
1045         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_qatzip_level",
1046                    "a value between 1 and 9");
1047         return false;
1048     }
1049 
1050     if (params->has_multifd_zstd_level &&
1051         (params->multifd_zstd_level > 20)) {
1052         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
1053                    "a value between 0 and 20");
1054         return false;
1055     }
1056 
1057     if (params->has_xbzrle_cache_size &&
1058         (params->xbzrle_cache_size < qemu_target_page_size() ||
1059          !is_power_of_2(params->xbzrle_cache_size))) {
1060         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1061                    "xbzrle_cache_size",
1062                    "a power of two no less than the target page size");
1063         return false;
1064     }
1065 
1066     if (params->has_max_cpu_throttle &&
1067         (params->max_cpu_throttle < params->cpu_throttle_initial ||
1068          params->max_cpu_throttle > 99)) {
1069         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1070                    "max_cpu_throttle",
1071                    "an integer in the range of cpu_throttle_initial to 99");
1072         return false;
1073     }
1074 
1075     if (params->has_announce_initial &&
1076         params->announce_initial > 100000) {
1077         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1078                    "announce_initial",
1079                    "a value between 0 and 100000");
1080         return false;
1081     }
1082     if (params->has_announce_max &&
1083         params->announce_max > 100000) {
1084         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1085                    "announce_max",
1086                    "a value between 0 and 100000");
1087        return false;
1088     }
1089     if (params->has_announce_rounds &&
1090         params->announce_rounds > 1000) {
1091         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1092                    "announce_rounds",
1093                    "a value between 0 and 1000");
1094        return false;
1095     }
1096     if (params->has_announce_step &&
1097         (params->announce_step < 1 ||
1098         params->announce_step > 10000)) {
1099         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1100                    "announce_step",
1101                    "a value between 0 and 10000");
1102        return false;
1103     }
1104 
1105     if (params->has_block_bitmap_mapping &&
1106         !check_dirty_bitmap_mig_alias_map(params->block_bitmap_mapping, errp)) {
1107         error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: ");
1108         return false;
1109     }
1110 
1111 #ifdef CONFIG_LINUX
1112     if (migrate_zero_copy_send() &&
1113         ((params->has_multifd_compression && params->multifd_compression) ||
1114          (params->tls_creds && *params->tls_creds))) {
1115         error_setg(errp,
1116                    "Zero copy only available for non-compressed non-TLS multifd migration");
1117         return false;
1118     }
1119 #endif
1120 
1121     if (migrate_mapped_ram() &&
1122         (migrate_multifd_compression() || migrate_tls())) {
1123         error_setg(errp,
1124                    "Mapped-ram only available for non-compressed non-TLS multifd migration");
1125         return false;
1126     }
1127 
1128     if (params->has_x_vcpu_dirty_limit_period &&
1129         (params->x_vcpu_dirty_limit_period < 1 ||
1130          params->x_vcpu_dirty_limit_period > 1000)) {
1131         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1132                    "x-vcpu-dirty-limit-period",
1133                    "a value between 1 and 1000");
1134         return false;
1135     }
1136 
1137     if (params->has_vcpu_dirty_limit &&
1138         (params->vcpu_dirty_limit < 1)) {
1139         error_setg(errp,
1140                    "Parameter 'vcpu_dirty_limit' must be greater than 1 MB/s");
1141         return false;
1142     }
1143 
1144     if (params->has_direct_io && params->direct_io && !qemu_has_direct_io()) {
1145         error_setg(errp, "No build-time support for direct-io");
1146         return false;
1147     }
1148 
1149     return true;
1150 }
1151 
1152 static void migrate_params_test_apply(MigrateSetParameters *params,
1153                                       MigrationParameters *dest)
1154 {
1155     *dest = migrate_get_current()->parameters;
1156 
1157     /* TODO use QAPI_CLONE() instead of duplicating it inline */
1158 
1159     if (params->has_throttle_trigger_threshold) {
1160         dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
1161     }
1162 
1163     if (params->has_cpu_throttle_initial) {
1164         dest->cpu_throttle_initial = params->cpu_throttle_initial;
1165     }
1166 
1167     if (params->has_cpu_throttle_increment) {
1168         dest->cpu_throttle_increment = params->cpu_throttle_increment;
1169     }
1170 
1171     if (params->has_cpu_throttle_tailslow) {
1172         dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1173     }
1174 
1175     if (params->tls_creds) {
1176         assert(params->tls_creds->type == QTYPE_QSTRING);
1177         dest->tls_creds = params->tls_creds->u.s;
1178     }
1179 
1180     if (params->tls_hostname) {
1181         assert(params->tls_hostname->type == QTYPE_QSTRING);
1182         dest->tls_hostname = params->tls_hostname->u.s;
1183     }
1184 
1185     if (params->has_max_bandwidth) {
1186         dest->max_bandwidth = params->max_bandwidth;
1187     }
1188 
1189     if (params->has_avail_switchover_bandwidth) {
1190         dest->avail_switchover_bandwidth = params->avail_switchover_bandwidth;
1191     }
1192 
1193     if (params->has_downtime_limit) {
1194         dest->downtime_limit = params->downtime_limit;
1195     }
1196 
1197     if (params->has_x_checkpoint_delay) {
1198         dest->x_checkpoint_delay = params->x_checkpoint_delay;
1199     }
1200 
1201     if (params->has_multifd_channels) {
1202         dest->multifd_channels = params->multifd_channels;
1203     }
1204     if (params->has_multifd_compression) {
1205         dest->multifd_compression = params->multifd_compression;
1206     }
1207     if (params->has_multifd_qatzip_level) {
1208         dest->multifd_qatzip_level = params->multifd_qatzip_level;
1209     }
1210     if (params->has_multifd_zlib_level) {
1211         dest->multifd_zlib_level = params->multifd_zlib_level;
1212     }
1213     if (params->has_multifd_zstd_level) {
1214         dest->multifd_zstd_level = params->multifd_zstd_level;
1215     }
1216     if (params->has_xbzrle_cache_size) {
1217         dest->xbzrle_cache_size = params->xbzrle_cache_size;
1218     }
1219     if (params->has_max_postcopy_bandwidth) {
1220         dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1221     }
1222     if (params->has_max_cpu_throttle) {
1223         dest->max_cpu_throttle = params->max_cpu_throttle;
1224     }
1225     if (params->has_announce_initial) {
1226         dest->announce_initial = params->announce_initial;
1227     }
1228     if (params->has_announce_max) {
1229         dest->announce_max = params->announce_max;
1230     }
1231     if (params->has_announce_rounds) {
1232         dest->announce_rounds = params->announce_rounds;
1233     }
1234     if (params->has_announce_step) {
1235         dest->announce_step = params->announce_step;
1236     }
1237 
1238     if (params->has_block_bitmap_mapping) {
1239         dest->has_block_bitmap_mapping = true;
1240         dest->block_bitmap_mapping = params->block_bitmap_mapping;
1241     }
1242 
1243     if (params->has_x_vcpu_dirty_limit_period) {
1244         dest->x_vcpu_dirty_limit_period =
1245             params->x_vcpu_dirty_limit_period;
1246     }
1247     if (params->has_vcpu_dirty_limit) {
1248         dest->vcpu_dirty_limit = params->vcpu_dirty_limit;
1249     }
1250 
1251     if (params->has_mode) {
1252         dest->mode = params->mode;
1253     }
1254 
1255     if (params->has_zero_page_detection) {
1256         dest->zero_page_detection = params->zero_page_detection;
1257     }
1258 
1259     if (params->has_direct_io) {
1260         dest->direct_io = params->direct_io;
1261     }
1262 }
1263 
1264 static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
1265 {
1266     MigrationState *s = migrate_get_current();
1267 
1268     /* TODO use QAPI_CLONE() instead of duplicating it inline */
1269 
1270     if (params->has_throttle_trigger_threshold) {
1271         s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
1272     }
1273 
1274     if (params->has_cpu_throttle_initial) {
1275         s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
1276     }
1277 
1278     if (params->has_cpu_throttle_increment) {
1279         s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
1280     }
1281 
1282     if (params->has_cpu_throttle_tailslow) {
1283         s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1284     }
1285 
1286     if (params->tls_creds) {
1287         g_free(s->parameters.tls_creds);
1288         assert(params->tls_creds->type == QTYPE_QSTRING);
1289         s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
1290     }
1291 
1292     if (params->tls_hostname) {
1293         g_free(s->parameters.tls_hostname);
1294         assert(params->tls_hostname->type == QTYPE_QSTRING);
1295         s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
1296     }
1297 
1298     if (params->tls_authz) {
1299         g_free(s->parameters.tls_authz);
1300         assert(params->tls_authz->type == QTYPE_QSTRING);
1301         s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1302     }
1303 
1304     if (params->has_max_bandwidth) {
1305         s->parameters.max_bandwidth = params->max_bandwidth;
1306         if (s->to_dst_file && !migration_in_postcopy()) {
1307             migration_rate_set(s->parameters.max_bandwidth);
1308         }
1309     }
1310 
1311     if (params->has_avail_switchover_bandwidth) {
1312         s->parameters.avail_switchover_bandwidth = params->avail_switchover_bandwidth;
1313     }
1314 
1315     if (params->has_downtime_limit) {
1316         s->parameters.downtime_limit = params->downtime_limit;
1317     }
1318 
1319     if (params->has_x_checkpoint_delay) {
1320         s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
1321         colo_checkpoint_delay_set();
1322     }
1323 
1324     if (params->has_multifd_channels) {
1325         s->parameters.multifd_channels = params->multifd_channels;
1326     }
1327     if (params->has_multifd_compression) {
1328         s->parameters.multifd_compression = params->multifd_compression;
1329     }
1330     if (params->has_multifd_qatzip_level) {
1331         s->parameters.multifd_qatzip_level = params->multifd_qatzip_level;
1332     }
1333     if (params->has_multifd_zlib_level) {
1334         s->parameters.multifd_zlib_level = params->multifd_zlib_level;
1335     }
1336     if (params->has_multifd_zstd_level) {
1337         s->parameters.multifd_zstd_level = params->multifd_zstd_level;
1338     }
1339     if (params->has_xbzrle_cache_size) {
1340         s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1341         xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1342     }
1343     if (params->has_max_postcopy_bandwidth) {
1344         s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1345         if (s->to_dst_file && migration_in_postcopy()) {
1346             migration_rate_set(s->parameters.max_postcopy_bandwidth);
1347         }
1348     }
1349     if (params->has_max_cpu_throttle) {
1350         s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1351     }
1352     if (params->has_announce_initial) {
1353         s->parameters.announce_initial = params->announce_initial;
1354     }
1355     if (params->has_announce_max) {
1356         s->parameters.announce_max = params->announce_max;
1357     }
1358     if (params->has_announce_rounds) {
1359         s->parameters.announce_rounds = params->announce_rounds;
1360     }
1361     if (params->has_announce_step) {
1362         s->parameters.announce_step = params->announce_step;
1363     }
1364 
1365     if (params->has_block_bitmap_mapping) {
1366         qapi_free_BitmapMigrationNodeAliasList(
1367             s->parameters.block_bitmap_mapping);
1368 
1369         s->parameters.has_block_bitmap_mapping = true;
1370         s->parameters.block_bitmap_mapping =
1371             QAPI_CLONE(BitmapMigrationNodeAliasList,
1372                        params->block_bitmap_mapping);
1373     }
1374 
1375     if (params->has_x_vcpu_dirty_limit_period) {
1376         s->parameters.x_vcpu_dirty_limit_period =
1377             params->x_vcpu_dirty_limit_period;
1378     }
1379     if (params->has_vcpu_dirty_limit) {
1380         s->parameters.vcpu_dirty_limit = params->vcpu_dirty_limit;
1381     }
1382 
1383     if (params->has_mode) {
1384         s->parameters.mode = params->mode;
1385     }
1386 
1387     if (params->has_zero_page_detection) {
1388         s->parameters.zero_page_detection = params->zero_page_detection;
1389     }
1390 
1391     if (params->has_direct_io) {
1392         s->parameters.direct_io = params->direct_io;
1393     }
1394 }
1395 
1396 void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
1397 {
1398     MigrationParameters tmp;
1399 
1400     /* TODO Rewrite "" to null instead for all three tls_* parameters */
1401     if (params->tls_creds
1402         && params->tls_creds->type == QTYPE_QNULL) {
1403         qobject_unref(params->tls_creds->u.n);
1404         params->tls_creds->type = QTYPE_QSTRING;
1405         params->tls_creds->u.s = strdup("");
1406     }
1407     if (params->tls_hostname
1408         && params->tls_hostname->type == QTYPE_QNULL) {
1409         qobject_unref(params->tls_hostname->u.n);
1410         params->tls_hostname->type = QTYPE_QSTRING;
1411         params->tls_hostname->u.s = strdup("");
1412     }
1413     if (params->tls_authz
1414         && params->tls_authz->type == QTYPE_QNULL) {
1415         qobject_unref(params->tls_authz->u.n);
1416         params->tls_authz->type = QTYPE_QSTRING;
1417         params->tls_authz->u.s = strdup("");
1418     }
1419 
1420     migrate_params_test_apply(params, &tmp);
1421 
1422     if (!migrate_params_check(&tmp, errp)) {
1423         /* Invalid parameter */
1424         return;
1425     }
1426 
1427     migrate_params_apply(params, errp);
1428 }
1429