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