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