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