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