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