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