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