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