xref: /openbmc/qemu/migration/options.c (revision 61c4e39f)
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) "
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 uint32_t migrate_checkpoint_delay(void)
767 {
768     MigrationState *s = migrate_get_current();
769 
770     return s->parameters.x_checkpoint_delay;
771 }
772 
773 int migrate_compress_level(void)
774 {
775     MigrationState *s = migrate_get_current();
776 
777     return s->parameters.compress_level;
778 }
779 
780 int migrate_compress_threads(void)
781 {
782     MigrationState *s = migrate_get_current();
783 
784     return s->parameters.compress_threads;
785 }
786 
787 int migrate_compress_wait_thread(void)
788 {
789     MigrationState *s = migrate_get_current();
790 
791     return s->parameters.compress_wait_thread;
792 }
793 
794 uint8_t migrate_cpu_throttle_increment(void)
795 {
796     MigrationState *s = migrate_get_current();
797 
798     return s->parameters.cpu_throttle_increment;
799 }
800 
801 uint8_t migrate_cpu_throttle_initial(void)
802 {
803     MigrationState *s = migrate_get_current();
804 
805     return s->parameters.cpu_throttle_initial;
806 }
807 
808 bool migrate_cpu_throttle_tailslow(void)
809 {
810     MigrationState *s = migrate_get_current();
811 
812     return s->parameters.cpu_throttle_tailslow;
813 }
814 
815 int migrate_decompress_threads(void)
816 {
817     MigrationState *s = migrate_get_current();
818 
819     return s->parameters.decompress_threads;
820 }
821 
822 uint64_t migrate_downtime_limit(void)
823 {
824     MigrationState *s = migrate_get_current();
825 
826     return s->parameters.downtime_limit;
827 }
828 
829 uint8_t migrate_max_cpu_throttle(void)
830 {
831     MigrationState *s = migrate_get_current();
832 
833     return s->parameters.max_cpu_throttle;
834 }
835 
836 uint64_t migrate_max_bandwidth(void)
837 {
838     MigrationState *s = migrate_get_current();
839 
840     return s->parameters.max_bandwidth;
841 }
842 
843 uint64_t migrate_avail_switchover_bandwidth(void)
844 {
845     MigrationState *s = migrate_get_current();
846 
847     return s->parameters.avail_switchover_bandwidth;
848 }
849 
850 uint64_t migrate_max_postcopy_bandwidth(void)
851 {
852     MigrationState *s = migrate_get_current();
853 
854     return s->parameters.max_postcopy_bandwidth;
855 }
856 
857 MigMode migrate_mode(void)
858 {
859     MigrationState *s = migrate_get_current();
860     MigMode mode = s->parameters.mode;
861 
862     assert(mode >= 0 && mode < MIG_MODE__MAX);
863     return mode;
864 }
865 
866 int migrate_multifd_channels(void)
867 {
868     MigrationState *s = migrate_get_current();
869 
870     return s->parameters.multifd_channels;
871 }
872 
873 MultiFDCompression migrate_multifd_compression(void)
874 {
875     MigrationState *s = migrate_get_current();
876 
877     assert(s->parameters.multifd_compression < MULTIFD_COMPRESSION__MAX);
878     return s->parameters.multifd_compression;
879 }
880 
881 int migrate_multifd_zlib_level(void)
882 {
883     MigrationState *s = migrate_get_current();
884 
885     return s->parameters.multifd_zlib_level;
886 }
887 
888 int migrate_multifd_zstd_level(void)
889 {
890     MigrationState *s = migrate_get_current();
891 
892     return s->parameters.multifd_zstd_level;
893 }
894 
895 uint8_t migrate_throttle_trigger_threshold(void)
896 {
897     MigrationState *s = migrate_get_current();
898 
899     return s->parameters.throttle_trigger_threshold;
900 }
901 
902 const char *migrate_tls_authz(void)
903 {
904     MigrationState *s = migrate_get_current();
905 
906     return s->parameters.tls_authz;
907 }
908 
909 const char *migrate_tls_creds(void)
910 {
911     MigrationState *s = migrate_get_current();
912 
913     return s->parameters.tls_creds;
914 }
915 
916 const char *migrate_tls_hostname(void)
917 {
918     MigrationState *s = migrate_get_current();
919 
920     return s->parameters.tls_hostname;
921 }
922 
923 uint64_t migrate_vcpu_dirty_limit_period(void)
924 {
925     MigrationState *s = migrate_get_current();
926 
927     return s->parameters.x_vcpu_dirty_limit_period;
928 }
929 
930 uint64_t migrate_xbzrle_cache_size(void)
931 {
932     MigrationState *s = migrate_get_current();
933 
934     return s->parameters.xbzrle_cache_size;
935 }
936 
937 ZeroPageDetection migrate_zero_page_detection(void)
938 {
939     MigrationState *s = migrate_get_current();
940 
941     return s->parameters.zero_page_detection;
942 }
943 
944 /* parameters helpers */
945 
946 void block_cleanup_parameters(void)
947 {
948     MigrationState *s = migrate_get_current();
949 
950     if (s->must_remove_block_options) {
951         /* setting to false can never fail */
952         migrate_cap_set(MIGRATION_CAPABILITY_BLOCK, false, &error_abort);
953         s->must_remove_block_options = false;
954     }
955 }
956 
957 AnnounceParameters *migrate_announce_params(void)
958 {
959     static AnnounceParameters ap;
960 
961     MigrationState *s = migrate_get_current();
962 
963     ap.initial = s->parameters.announce_initial;
964     ap.max = s->parameters.announce_max;
965     ap.rounds = s->parameters.announce_rounds;
966     ap.step = s->parameters.announce_step;
967 
968     return &ap;
969 }
970 
971 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
972 {
973     MigrationParameters *params;
974     MigrationState *s = migrate_get_current();
975 
976     /* TODO use QAPI_CLONE() instead of duplicating it inline */
977     params = g_malloc0(sizeof(*params));
978     params->has_compress_level = true;
979     params->compress_level = s->parameters.compress_level;
980     params->has_compress_threads = true;
981     params->compress_threads = s->parameters.compress_threads;
982     params->has_compress_wait_thread = true;
983     params->compress_wait_thread = s->parameters.compress_wait_thread;
984     params->has_decompress_threads = true;
985     params->decompress_threads = s->parameters.decompress_threads;
986     params->has_throttle_trigger_threshold = true;
987     params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
988     params->has_cpu_throttle_initial = true;
989     params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
990     params->has_cpu_throttle_increment = true;
991     params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
992     params->has_cpu_throttle_tailslow = true;
993     params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
994     params->tls_creds = g_strdup(s->parameters.tls_creds);
995     params->tls_hostname = g_strdup(s->parameters.tls_hostname);
996     params->tls_authz = g_strdup(s->parameters.tls_authz ?
997                                  s->parameters.tls_authz : "");
998     params->has_max_bandwidth = true;
999     params->max_bandwidth = s->parameters.max_bandwidth;
1000     params->has_avail_switchover_bandwidth = true;
1001     params->avail_switchover_bandwidth = s->parameters.avail_switchover_bandwidth;
1002     params->has_downtime_limit = true;
1003     params->downtime_limit = s->parameters.downtime_limit;
1004     params->has_x_checkpoint_delay = true;
1005     params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
1006     params->has_multifd_channels = true;
1007     params->multifd_channels = s->parameters.multifd_channels;
1008     params->has_multifd_compression = true;
1009     params->multifd_compression = s->parameters.multifd_compression;
1010     params->has_multifd_zlib_level = true;
1011     params->multifd_zlib_level = s->parameters.multifd_zlib_level;
1012     params->has_multifd_zstd_level = true;
1013     params->multifd_zstd_level = s->parameters.multifd_zstd_level;
1014     params->has_xbzrle_cache_size = true;
1015     params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
1016     params->has_max_postcopy_bandwidth = true;
1017     params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
1018     params->has_max_cpu_throttle = true;
1019     params->max_cpu_throttle = s->parameters.max_cpu_throttle;
1020     params->has_announce_initial = true;
1021     params->announce_initial = s->parameters.announce_initial;
1022     params->has_announce_max = true;
1023     params->announce_max = s->parameters.announce_max;
1024     params->has_announce_rounds = true;
1025     params->announce_rounds = s->parameters.announce_rounds;
1026     params->has_announce_step = true;
1027     params->announce_step = s->parameters.announce_step;
1028 
1029     if (s->parameters.has_block_bitmap_mapping) {
1030         params->has_block_bitmap_mapping = true;
1031         params->block_bitmap_mapping =
1032             QAPI_CLONE(BitmapMigrationNodeAliasList,
1033                        s->parameters.block_bitmap_mapping);
1034     }
1035 
1036     params->has_x_vcpu_dirty_limit_period = true;
1037     params->x_vcpu_dirty_limit_period = s->parameters.x_vcpu_dirty_limit_period;
1038     params->has_vcpu_dirty_limit = true;
1039     params->vcpu_dirty_limit = s->parameters.vcpu_dirty_limit;
1040     params->has_mode = true;
1041     params->mode = s->parameters.mode;
1042     params->has_zero_page_detection = true;
1043     params->zero_page_detection = s->parameters.zero_page_detection;
1044 
1045     return params;
1046 }
1047 
1048 void migrate_params_init(MigrationParameters *params)
1049 {
1050     params->tls_hostname = g_strdup("");
1051     params->tls_creds = g_strdup("");
1052 
1053     /* Set has_* up only for parameter checks */
1054     params->has_compress_level = true;
1055     params->has_compress_threads = true;
1056     params->has_compress_wait_thread = true;
1057     params->has_decompress_threads = true;
1058     params->has_throttle_trigger_threshold = true;
1059     params->has_cpu_throttle_initial = true;
1060     params->has_cpu_throttle_increment = true;
1061     params->has_cpu_throttle_tailslow = true;
1062     params->has_max_bandwidth = true;
1063     params->has_downtime_limit = true;
1064     params->has_x_checkpoint_delay = true;
1065     params->has_multifd_channels = true;
1066     params->has_multifd_compression = true;
1067     params->has_multifd_zlib_level = true;
1068     params->has_multifd_zstd_level = true;
1069     params->has_xbzrle_cache_size = true;
1070     params->has_max_postcopy_bandwidth = true;
1071     params->has_max_cpu_throttle = true;
1072     params->has_announce_initial = true;
1073     params->has_announce_max = true;
1074     params->has_announce_rounds = true;
1075     params->has_announce_step = true;
1076     params->has_x_vcpu_dirty_limit_period = true;
1077     params->has_vcpu_dirty_limit = true;
1078     params->has_mode = true;
1079     params->has_zero_page_detection = true;
1080 }
1081 
1082 /*
1083  * Check whether the parameters are valid. Error will be put into errp
1084  * (if provided). Return true if valid, otherwise false.
1085  */
1086 bool migrate_params_check(MigrationParameters *params, Error **errp)
1087 {
1088     ERRP_GUARD();
1089 
1090     if (params->has_compress_level &&
1091         (params->compress_level > 9)) {
1092         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
1093                    "a value between 0 and 9");
1094         return false;
1095     }
1096 
1097     if (params->has_compress_threads && (params->compress_threads < 1)) {
1098         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1099                    "compress_threads",
1100                    "a value between 1 and 255");
1101         return false;
1102     }
1103 
1104     if (params->has_decompress_threads && (params->decompress_threads < 1)) {
1105         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1106                    "decompress_threads",
1107                    "a value between 1 and 255");
1108         return false;
1109     }
1110 
1111     if (params->has_throttle_trigger_threshold &&
1112         (params->throttle_trigger_threshold < 1 ||
1113          params->throttle_trigger_threshold > 100)) {
1114         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1115                    "throttle_trigger_threshold",
1116                    "an integer in the range of 1 to 100");
1117         return false;
1118     }
1119 
1120     if (params->has_cpu_throttle_initial &&
1121         (params->cpu_throttle_initial < 1 ||
1122          params->cpu_throttle_initial > 99)) {
1123         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1124                    "cpu_throttle_initial",
1125                    "an integer in the range of 1 to 99");
1126         return false;
1127     }
1128 
1129     if (params->has_cpu_throttle_increment &&
1130         (params->cpu_throttle_increment < 1 ||
1131          params->cpu_throttle_increment > 99)) {
1132         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1133                    "cpu_throttle_increment",
1134                    "an integer in the range of 1 to 99");
1135         return false;
1136     }
1137 
1138     if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
1139         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1140                    "max_bandwidth",
1141                    "an integer in the range of 0 to "stringify(SIZE_MAX)
1142                    " bytes/second");
1143         return false;
1144     }
1145 
1146     if (params->has_avail_switchover_bandwidth &&
1147         (params->avail_switchover_bandwidth > SIZE_MAX)) {
1148         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1149                    "avail_switchover_bandwidth",
1150                    "an integer in the range of 0 to "stringify(SIZE_MAX)
1151                    " bytes/second");
1152         return false;
1153     }
1154 
1155     if (params->has_downtime_limit &&
1156         (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
1157         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1158                    "downtime_limit",
1159                    "an integer in the range of 0 to "
1160                     stringify(MAX_MIGRATE_DOWNTIME)" ms");
1161         return false;
1162     }
1163 
1164     /* x_checkpoint_delay is now always positive */
1165 
1166     if (params->has_multifd_channels && (params->multifd_channels < 1)) {
1167         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1168                    "multifd_channels",
1169                    "a value between 1 and 255");
1170         return false;
1171     }
1172 
1173     if (params->has_multifd_zlib_level &&
1174         (params->multifd_zlib_level > 9)) {
1175         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
1176                    "a value between 0 and 9");
1177         return false;
1178     }
1179 
1180     if (params->has_multifd_zstd_level &&
1181         (params->multifd_zstd_level > 20)) {
1182         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
1183                    "a value between 0 and 20");
1184         return false;
1185     }
1186 
1187     if (params->has_xbzrle_cache_size &&
1188         (params->xbzrle_cache_size < qemu_target_page_size() ||
1189          !is_power_of_2(params->xbzrle_cache_size))) {
1190         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1191                    "xbzrle_cache_size",
1192                    "a power of two no less than the target page size");
1193         return false;
1194     }
1195 
1196     if (params->has_max_cpu_throttle &&
1197         (params->max_cpu_throttle < params->cpu_throttle_initial ||
1198          params->max_cpu_throttle > 99)) {
1199         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1200                    "max_cpu_throttle",
1201                    "an integer in the range of cpu_throttle_initial to 99");
1202         return false;
1203     }
1204 
1205     if (params->has_announce_initial &&
1206         params->announce_initial > 100000) {
1207         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1208                    "announce_initial",
1209                    "a value between 0 and 100000");
1210         return false;
1211     }
1212     if (params->has_announce_max &&
1213         params->announce_max > 100000) {
1214         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1215                    "announce_max",
1216                    "a value between 0 and 100000");
1217        return false;
1218     }
1219     if (params->has_announce_rounds &&
1220         params->announce_rounds > 1000) {
1221         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1222                    "announce_rounds",
1223                    "a value between 0 and 1000");
1224        return false;
1225     }
1226     if (params->has_announce_step &&
1227         (params->announce_step < 1 ||
1228         params->announce_step > 10000)) {
1229         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1230                    "announce_step",
1231                    "a value between 0 and 10000");
1232        return false;
1233     }
1234 
1235     if (params->has_block_bitmap_mapping &&
1236         !check_dirty_bitmap_mig_alias_map(params->block_bitmap_mapping, errp)) {
1237         error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: ");
1238         return false;
1239     }
1240 
1241 #ifdef CONFIG_LINUX
1242     if (migrate_zero_copy_send() &&
1243         ((params->has_multifd_compression && params->multifd_compression) ||
1244          (params->tls_creds && *params->tls_creds))) {
1245         error_setg(errp,
1246                    "Zero copy only available for non-compressed non-TLS multifd migration");
1247         return false;
1248     }
1249 #endif
1250 
1251     if (migrate_mapped_ram() &&
1252         (migrate_multifd_compression() || migrate_tls())) {
1253         error_setg(errp,
1254                    "Mapped-ram only available for non-compressed non-TLS multifd migration");
1255         return false;
1256     }
1257 
1258     if (params->has_x_vcpu_dirty_limit_period &&
1259         (params->x_vcpu_dirty_limit_period < 1 ||
1260          params->x_vcpu_dirty_limit_period > 1000)) {
1261         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1262                    "x-vcpu-dirty-limit-period",
1263                    "a value between 1 and 1000");
1264         return false;
1265     }
1266 
1267     if (params->has_vcpu_dirty_limit &&
1268         (params->vcpu_dirty_limit < 1)) {
1269         error_setg(errp,
1270                    "Parameter 'vcpu_dirty_limit' must be greater than 1 MB/s");
1271         return false;
1272     }
1273 
1274     return true;
1275 }
1276 
1277 static void migrate_params_test_apply(MigrateSetParameters *params,
1278                                       MigrationParameters *dest)
1279 {
1280     *dest = migrate_get_current()->parameters;
1281 
1282     /* TODO use QAPI_CLONE() instead of duplicating it inline */
1283 
1284     if (params->has_compress_level) {
1285         dest->compress_level = params->compress_level;
1286     }
1287 
1288     if (params->has_compress_threads) {
1289         dest->compress_threads = params->compress_threads;
1290     }
1291 
1292     if (params->has_compress_wait_thread) {
1293         dest->compress_wait_thread = params->compress_wait_thread;
1294     }
1295 
1296     if (params->has_decompress_threads) {
1297         dest->decompress_threads = params->decompress_threads;
1298     }
1299 
1300     if (params->has_throttle_trigger_threshold) {
1301         dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
1302     }
1303 
1304     if (params->has_cpu_throttle_initial) {
1305         dest->cpu_throttle_initial = params->cpu_throttle_initial;
1306     }
1307 
1308     if (params->has_cpu_throttle_increment) {
1309         dest->cpu_throttle_increment = params->cpu_throttle_increment;
1310     }
1311 
1312     if (params->has_cpu_throttle_tailslow) {
1313         dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1314     }
1315 
1316     if (params->tls_creds) {
1317         assert(params->tls_creds->type == QTYPE_QSTRING);
1318         dest->tls_creds = params->tls_creds->u.s;
1319     }
1320 
1321     if (params->tls_hostname) {
1322         assert(params->tls_hostname->type == QTYPE_QSTRING);
1323         dest->tls_hostname = params->tls_hostname->u.s;
1324     }
1325 
1326     if (params->has_max_bandwidth) {
1327         dest->max_bandwidth = params->max_bandwidth;
1328     }
1329 
1330     if (params->has_avail_switchover_bandwidth) {
1331         dest->avail_switchover_bandwidth = params->avail_switchover_bandwidth;
1332     }
1333 
1334     if (params->has_downtime_limit) {
1335         dest->downtime_limit = params->downtime_limit;
1336     }
1337 
1338     if (params->has_x_checkpoint_delay) {
1339         dest->x_checkpoint_delay = params->x_checkpoint_delay;
1340     }
1341 
1342     if (params->has_multifd_channels) {
1343         dest->multifd_channels = params->multifd_channels;
1344     }
1345     if (params->has_multifd_compression) {
1346         dest->multifd_compression = params->multifd_compression;
1347     }
1348     if (params->has_multifd_zlib_level) {
1349         dest->multifd_zlib_level = params->multifd_zlib_level;
1350     }
1351     if (params->has_multifd_zstd_level) {
1352         dest->multifd_zstd_level = params->multifd_zstd_level;
1353     }
1354     if (params->has_xbzrle_cache_size) {
1355         dest->xbzrle_cache_size = params->xbzrle_cache_size;
1356     }
1357     if (params->has_max_postcopy_bandwidth) {
1358         dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1359     }
1360     if (params->has_max_cpu_throttle) {
1361         dest->max_cpu_throttle = params->max_cpu_throttle;
1362     }
1363     if (params->has_announce_initial) {
1364         dest->announce_initial = params->announce_initial;
1365     }
1366     if (params->has_announce_max) {
1367         dest->announce_max = params->announce_max;
1368     }
1369     if (params->has_announce_rounds) {
1370         dest->announce_rounds = params->announce_rounds;
1371     }
1372     if (params->has_announce_step) {
1373         dest->announce_step = params->announce_step;
1374     }
1375 
1376     if (params->has_block_bitmap_mapping) {
1377         dest->has_block_bitmap_mapping = true;
1378         dest->block_bitmap_mapping = params->block_bitmap_mapping;
1379     }
1380 
1381     if (params->has_x_vcpu_dirty_limit_period) {
1382         dest->x_vcpu_dirty_limit_period =
1383             params->x_vcpu_dirty_limit_period;
1384     }
1385     if (params->has_vcpu_dirty_limit) {
1386         dest->vcpu_dirty_limit = params->vcpu_dirty_limit;
1387     }
1388 
1389     if (params->has_mode) {
1390         dest->mode = params->mode;
1391     }
1392 
1393     if (params->has_zero_page_detection) {
1394         dest->zero_page_detection = params->zero_page_detection;
1395     }
1396 }
1397 
1398 static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
1399 {
1400     MigrationState *s = migrate_get_current();
1401 
1402     /* TODO use QAPI_CLONE() instead of duplicating it inline */
1403 
1404     if (params->has_compress_level) {
1405         warn_report("old compression is deprecated;"
1406                     " use multifd compression methods instead");
1407         s->parameters.compress_level = params->compress_level;
1408     }
1409 
1410     if (params->has_compress_threads) {
1411         warn_report("old compression is deprecated;"
1412                     " use multifd compression methods instead");
1413         s->parameters.compress_threads = params->compress_threads;
1414     }
1415 
1416     if (params->has_compress_wait_thread) {
1417         warn_report("old compression is deprecated;"
1418                     " use multifd compression methods instead");
1419         s->parameters.compress_wait_thread = params->compress_wait_thread;
1420     }
1421 
1422     if (params->has_decompress_threads) {
1423         warn_report("old compression is deprecated;"
1424                     " use multifd compression methods instead");
1425         s->parameters.decompress_threads = params->decompress_threads;
1426     }
1427 
1428     if (params->has_throttle_trigger_threshold) {
1429         s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
1430     }
1431 
1432     if (params->has_cpu_throttle_initial) {
1433         s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
1434     }
1435 
1436     if (params->has_cpu_throttle_increment) {
1437         s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
1438     }
1439 
1440     if (params->has_cpu_throttle_tailslow) {
1441         s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1442     }
1443 
1444     if (params->tls_creds) {
1445         g_free(s->parameters.tls_creds);
1446         assert(params->tls_creds->type == QTYPE_QSTRING);
1447         s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
1448     }
1449 
1450     if (params->tls_hostname) {
1451         g_free(s->parameters.tls_hostname);
1452         assert(params->tls_hostname->type == QTYPE_QSTRING);
1453         s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
1454     }
1455 
1456     if (params->tls_authz) {
1457         g_free(s->parameters.tls_authz);
1458         assert(params->tls_authz->type == QTYPE_QSTRING);
1459         s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1460     }
1461 
1462     if (params->has_max_bandwidth) {
1463         s->parameters.max_bandwidth = params->max_bandwidth;
1464         if (s->to_dst_file && !migration_in_postcopy()) {
1465             migration_rate_set(s->parameters.max_bandwidth);
1466         }
1467     }
1468 
1469     if (params->has_avail_switchover_bandwidth) {
1470         s->parameters.avail_switchover_bandwidth = params->avail_switchover_bandwidth;
1471     }
1472 
1473     if (params->has_downtime_limit) {
1474         s->parameters.downtime_limit = params->downtime_limit;
1475     }
1476 
1477     if (params->has_x_checkpoint_delay) {
1478         s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
1479         colo_checkpoint_delay_set();
1480     }
1481 
1482     if (params->has_multifd_channels) {
1483         s->parameters.multifd_channels = params->multifd_channels;
1484     }
1485     if (params->has_multifd_compression) {
1486         s->parameters.multifd_compression = params->multifd_compression;
1487     }
1488     if (params->has_multifd_zlib_level) {
1489         s->parameters.multifd_zlib_level = params->multifd_zlib_level;
1490     }
1491     if (params->has_multifd_zstd_level) {
1492         s->parameters.multifd_zstd_level = params->multifd_zstd_level;
1493     }
1494     if (params->has_xbzrle_cache_size) {
1495         s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1496         xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1497     }
1498     if (params->has_max_postcopy_bandwidth) {
1499         s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1500         if (s->to_dst_file && migration_in_postcopy()) {
1501             migration_rate_set(s->parameters.max_postcopy_bandwidth);
1502         }
1503     }
1504     if (params->has_max_cpu_throttle) {
1505         s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1506     }
1507     if (params->has_announce_initial) {
1508         s->parameters.announce_initial = params->announce_initial;
1509     }
1510     if (params->has_announce_max) {
1511         s->parameters.announce_max = params->announce_max;
1512     }
1513     if (params->has_announce_rounds) {
1514         s->parameters.announce_rounds = params->announce_rounds;
1515     }
1516     if (params->has_announce_step) {
1517         s->parameters.announce_step = params->announce_step;
1518     }
1519 
1520     if (params->has_block_bitmap_mapping) {
1521         qapi_free_BitmapMigrationNodeAliasList(
1522             s->parameters.block_bitmap_mapping);
1523 
1524         s->parameters.has_block_bitmap_mapping = true;
1525         s->parameters.block_bitmap_mapping =
1526             QAPI_CLONE(BitmapMigrationNodeAliasList,
1527                        params->block_bitmap_mapping);
1528     }
1529 
1530     if (params->has_x_vcpu_dirty_limit_period) {
1531         s->parameters.x_vcpu_dirty_limit_period =
1532             params->x_vcpu_dirty_limit_period;
1533     }
1534     if (params->has_vcpu_dirty_limit) {
1535         s->parameters.vcpu_dirty_limit = params->vcpu_dirty_limit;
1536     }
1537 
1538     if (params->has_mode) {
1539         s->parameters.mode = params->mode;
1540     }
1541 
1542     if (params->has_zero_page_detection) {
1543         s->parameters.zero_page_detection = params->zero_page_detection;
1544     }
1545 }
1546 
1547 void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
1548 {
1549     MigrationParameters tmp;
1550 
1551     /* TODO Rewrite "" to null instead for all three tls_* parameters */
1552     if (params->tls_creds
1553         && params->tls_creds->type == QTYPE_QNULL) {
1554         qobject_unref(params->tls_creds->u.n);
1555         params->tls_creds->type = QTYPE_QSTRING;
1556         params->tls_creds->u.s = strdup("");
1557     }
1558     if (params->tls_hostname
1559         && params->tls_hostname->type == QTYPE_QNULL) {
1560         qobject_unref(params->tls_hostname->u.n);
1561         params->tls_hostname->type = QTYPE_QSTRING;
1562         params->tls_hostname->u.s = strdup("");
1563     }
1564     if (params->tls_authz
1565         && params->tls_authz->type == QTYPE_QNULL) {
1566         qobject_unref(params->tls_authz->u.n);
1567         params->tls_authz->type = QTYPE_QSTRING;
1568         params->tls_authz->u.s = strdup("");
1569     }
1570 
1571     migrate_params_test_apply(params, &tmp);
1572 
1573     if (!migrate_params_check(&tmp, errp)) {
1574         /* Invalid parameter */
1575         return;
1576     }
1577 
1578     migrate_params_apply(params, errp);
1579 }
1580