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