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