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