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