xref: /openbmc/qemu/migration/options.c (revision e1fde0e038bafd0bd05db7d43305b9b2f03c0683)
11f0776f1SJuan Quintela /*
21f0776f1SJuan Quintela  * QEMU migration capabilities
31f0776f1SJuan Quintela  *
41f0776f1SJuan Quintela  * Copyright (c) 2012-2023 Red Hat Inc
51f0776f1SJuan Quintela  *
61f0776f1SJuan Quintela  * Authors:
71f0776f1SJuan Quintela  *   Orit Wasserman <owasserm@redhat.com>
81f0776f1SJuan Quintela  *   Juan Quintela <quintela@redhat.com>
91f0776f1SJuan Quintela  *
101f0776f1SJuan Quintela  * This work is licensed under the terms of the GNU GPL, version 2 or later.
111f0776f1SJuan Quintela  * See the COPYING file in the top-level directory.
121f0776f1SJuan Quintela  */
131f0776f1SJuan Quintela 
141f0776f1SJuan Quintela #include "qemu/osdep.h"
1509d6c965SJuan Quintela #include "exec/target_page.h"
169c894df3SJuan Quintela #include "qapi/clone-visitor.h"
1777608706SJuan Quintela #include "qapi/error.h"
184d0c6b69SJuan Quintela #include "qapi/qapi-commands-migration.h"
199c894df3SJuan Quintela #include "qapi/qapi-visit-migration.h"
20f80196b7SJuan Quintela #include "qapi/qmp/qerror.h"
2109d6c965SJuan Quintela #include "qapi/qmp/qnull.h"
2277608706SJuan Quintela #include "sysemu/runstate.h"
2309d6c965SJuan Quintela #include "migration/colo.h"
242682c4eeSJuan Quintela #include "migration/misc.h"
251f0776f1SJuan Quintela #include "migration.h"
26*e1fde0e0SJuan Quintela #include "migration-stats.h"
2709d6c965SJuan Quintela #include "qemu-file.h"
2877608706SJuan Quintela #include "ram.h"
291f0776f1SJuan Quintela #include "options.h"
301f0776f1SJuan Quintela 
3109d6c965SJuan Quintela /* Maximum migrate downtime set to 2000 seconds */
3209d6c965SJuan Quintela #define MAX_MIGRATE_DOWNTIME_SECONDS 2000
3309d6c965SJuan Quintela #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
3409d6c965SJuan Quintela 
35f9436522SJuan Quintela #define MAX_THROTTLE  (128 << 20)      /* Migration transfer speed throttling */
36f9436522SJuan Quintela 
37f9436522SJuan Quintela /* Time in milliseconds we are allowed to stop the source,
38f9436522SJuan Quintela  * for sending the last part */
39f9436522SJuan Quintela #define DEFAULT_MIGRATE_SET_DOWNTIME 300
40f9436522SJuan Quintela 
41f9436522SJuan Quintela /* Default compression thread count */
42f9436522SJuan Quintela #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
43f9436522SJuan Quintela /* Default decompression thread count, usually decompression is at
44f9436522SJuan Quintela  * least 4 times as fast as compression.*/
45f9436522SJuan Quintela #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
46f9436522SJuan Quintela /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
47f9436522SJuan Quintela #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
48f9436522SJuan Quintela /* Define default autoconverge cpu throttle migration parameters */
49f9436522SJuan Quintela #define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50
50f9436522SJuan Quintela #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
51f9436522SJuan Quintela #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
52f9436522SJuan Quintela #define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
53f9436522SJuan Quintela 
54f9436522SJuan Quintela /* Migration XBZRLE default cache size */
55f9436522SJuan Quintela #define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
56f9436522SJuan Quintela 
57f9436522SJuan Quintela /* The delay time (in ms) between two COLO checkpoints */
58f9436522SJuan Quintela #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
59f9436522SJuan Quintela #define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
60f9436522SJuan Quintela #define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
61f9436522SJuan Quintela /* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
62f9436522SJuan Quintela #define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
63f9436522SJuan Quintela /* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
64f9436522SJuan Quintela #define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
65f9436522SJuan Quintela 
66f9436522SJuan Quintela /* Background transfer rate for postcopy, 0 means unlimited, note
67f9436522SJuan Quintela  * that page requests can still exceed this limit.
68f9436522SJuan Quintela  */
69f9436522SJuan Quintela #define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
70f9436522SJuan Quintela 
71f9436522SJuan Quintela /*
72f9436522SJuan Quintela  * Parameters for self_announce_delay giving a stream of RARP/ARP
73f9436522SJuan Quintela  * packets after migration.
74f9436522SJuan Quintela  */
75f9436522SJuan Quintela #define DEFAULT_MIGRATE_ANNOUNCE_INITIAL  50
76f9436522SJuan Quintela #define DEFAULT_MIGRATE_ANNOUNCE_MAX     550
77f9436522SJuan Quintela #define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS    5
78f9436522SJuan Quintela #define DEFAULT_MIGRATE_ANNOUNCE_STEP    100
79f9436522SJuan Quintela 
80f9436522SJuan Quintela #define DEFINE_PROP_MIG_CAP(name, x)             \
81f9436522SJuan Quintela     DEFINE_PROP_BOOL(name, MigrationState, capabilities[x], false)
82f9436522SJuan Quintela 
83f9436522SJuan Quintela Property migration_properties[] = {
84f9436522SJuan Quintela     DEFINE_PROP_BOOL("store-global-state", MigrationState,
85f9436522SJuan Quintela                      store_global_state, true),
86f9436522SJuan Quintela     DEFINE_PROP_BOOL("send-configuration", MigrationState,
87f9436522SJuan Quintela                      send_configuration, true),
88f9436522SJuan Quintela     DEFINE_PROP_BOOL("send-section-footer", MigrationState,
89f9436522SJuan Quintela                      send_section_footer, true),
90f9436522SJuan Quintela     DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
91f9436522SJuan Quintela                       decompress_error_check, true),
9277c259a4SJuan Quintela     DEFINE_PROP_BOOL("multifd-flush-after-each-section", MigrationState,
93294e5a40SJuan Quintela                       multifd_flush_after_each_section, false),
94f9436522SJuan Quintela     DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
95f9436522SJuan Quintela                       clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
96f9436522SJuan Quintela     DEFINE_PROP_BOOL("x-preempt-pre-7-2", MigrationState,
97f9436522SJuan Quintela                      preempt_pre_7_2, false),
98f9436522SJuan Quintela 
99f9436522SJuan Quintela     /* Migration parameters */
100f9436522SJuan Quintela     DEFINE_PROP_UINT8("x-compress-level", MigrationState,
101f9436522SJuan Quintela                       parameters.compress_level,
102f9436522SJuan Quintela                       DEFAULT_MIGRATE_COMPRESS_LEVEL),
103f9436522SJuan Quintela     DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
104f9436522SJuan Quintela                       parameters.compress_threads,
105f9436522SJuan Quintela                       DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
106f9436522SJuan Quintela     DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
107f9436522SJuan Quintela                       parameters.compress_wait_thread, true),
108f9436522SJuan Quintela     DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
109f9436522SJuan Quintela                       parameters.decompress_threads,
110f9436522SJuan Quintela                       DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
111f9436522SJuan Quintela     DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState,
112f9436522SJuan Quintela                       parameters.throttle_trigger_threshold,
113f9436522SJuan Quintela                       DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
114f9436522SJuan Quintela     DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
115f9436522SJuan Quintela                       parameters.cpu_throttle_initial,
116f9436522SJuan Quintela                       DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
117f9436522SJuan Quintela     DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
118f9436522SJuan Quintela                       parameters.cpu_throttle_increment,
119f9436522SJuan Quintela                       DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
120f9436522SJuan Quintela     DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
121f9436522SJuan Quintela                       parameters.cpu_throttle_tailslow, false),
122f9436522SJuan Quintela     DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
123f9436522SJuan Quintela                       parameters.max_bandwidth, MAX_THROTTLE),
124f9436522SJuan Quintela     DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
125f9436522SJuan Quintela                       parameters.downtime_limit,
126f9436522SJuan Quintela                       DEFAULT_MIGRATE_SET_DOWNTIME),
127f9436522SJuan Quintela     DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
128f9436522SJuan Quintela                       parameters.x_checkpoint_delay,
129f9436522SJuan Quintela                       DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
130f9436522SJuan Quintela     DEFINE_PROP_UINT8("multifd-channels", MigrationState,
131f9436522SJuan Quintela                       parameters.multifd_channels,
132f9436522SJuan Quintela                       DEFAULT_MIGRATE_MULTIFD_CHANNELS),
133f9436522SJuan Quintela     DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
134f9436522SJuan Quintela                       parameters.multifd_compression,
135f9436522SJuan Quintela                       DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
136f9436522SJuan Quintela     DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
137f9436522SJuan Quintela                       parameters.multifd_zlib_level,
138f9436522SJuan Quintela                       DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
139f9436522SJuan Quintela     DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
140f9436522SJuan Quintela                       parameters.multifd_zstd_level,
141f9436522SJuan Quintela                       DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
142f9436522SJuan Quintela     DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
143f9436522SJuan Quintela                       parameters.xbzrle_cache_size,
144f9436522SJuan Quintela                       DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
145f9436522SJuan Quintela     DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
146f9436522SJuan Quintela                       parameters.max_postcopy_bandwidth,
147f9436522SJuan Quintela                       DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
148f9436522SJuan Quintela     DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
149f9436522SJuan Quintela                       parameters.max_cpu_throttle,
150f9436522SJuan Quintela                       DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
151f9436522SJuan Quintela     DEFINE_PROP_SIZE("announce-initial", MigrationState,
152f9436522SJuan Quintela                       parameters.announce_initial,
153f9436522SJuan Quintela                       DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
154f9436522SJuan Quintela     DEFINE_PROP_SIZE("announce-max", MigrationState,
155f9436522SJuan Quintela                       parameters.announce_max,
156f9436522SJuan Quintela                       DEFAULT_MIGRATE_ANNOUNCE_MAX),
157f9436522SJuan Quintela     DEFINE_PROP_SIZE("announce-rounds", MigrationState,
158f9436522SJuan Quintela                       parameters.announce_rounds,
159f9436522SJuan Quintela                       DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
160f9436522SJuan Quintela     DEFINE_PROP_SIZE("announce-step", MigrationState,
161f9436522SJuan Quintela                       parameters.announce_step,
162f9436522SJuan Quintela                       DEFAULT_MIGRATE_ANNOUNCE_STEP),
163f9436522SJuan Quintela     DEFINE_PROP_STRING("tls-creds", MigrationState, parameters.tls_creds),
164f9436522SJuan Quintela     DEFINE_PROP_STRING("tls-hostname", MigrationState, parameters.tls_hostname),
165f9436522SJuan Quintela     DEFINE_PROP_STRING("tls-authz", MigrationState, parameters.tls_authz),
166f9436522SJuan Quintela 
167f9436522SJuan Quintela     /* Migration capabilities */
168f9436522SJuan Quintela     DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
169f9436522SJuan Quintela     DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
170f9436522SJuan Quintela     DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
171f9436522SJuan Quintela     DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
172f9436522SJuan Quintela     DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
173f9436522SJuan Quintela     DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
174f9436522SJuan Quintela     DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
175f9436522SJuan Quintela     DEFINE_PROP_MIG_CAP("x-postcopy-preempt",
176f9436522SJuan Quintela                         MIGRATION_CAPABILITY_POSTCOPY_PREEMPT),
177f9436522SJuan Quintela     DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
178f9436522SJuan Quintela     DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
179f9436522SJuan Quintela     DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
180f9436522SJuan Quintela     DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
181f9436522SJuan Quintela     DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
182f9436522SJuan Quintela     DEFINE_PROP_MIG_CAP("x-background-snapshot",
183f9436522SJuan Quintela             MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT),
184f9436522SJuan Quintela #ifdef CONFIG_LINUX
185f9436522SJuan Quintela     DEFINE_PROP_MIG_CAP("x-zero-copy-send",
186f9436522SJuan Quintela             MIGRATION_CAPABILITY_ZERO_COPY_SEND),
187f9436522SJuan Quintela #endif
188f9436522SJuan Quintela 
189f9436522SJuan Quintela     DEFINE_PROP_END_OF_LIST(),
190f9436522SJuan Quintela };
191f9436522SJuan Quintela 
1921f0776f1SJuan Quintela bool migrate_auto_converge(void)
1931f0776f1SJuan Quintela {
1948f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
1951f0776f1SJuan Quintela 
1961f0776f1SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1971f0776f1SJuan Quintela }
1981f0776f1SJuan Quintela 
1991f0776f1SJuan Quintela bool migrate_background_snapshot(void)
2001f0776f1SJuan Quintela {
2018f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
2021f0776f1SJuan Quintela 
2031f0776f1SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT];
2041f0776f1SJuan Quintela }
2051f0776f1SJuan Quintela 
2069d4b1e5fSJuan Quintela bool migrate_block(void)
2079d4b1e5fSJuan Quintela {
2088f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
2099d4b1e5fSJuan Quintela 
2109d4b1e5fSJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_BLOCK];
2119d4b1e5fSJuan Quintela }
2129d4b1e5fSJuan Quintela 
2135e804644SJuan Quintela bool migrate_colo(void)
2145e804644SJuan Quintela {
2155e804644SJuan Quintela     MigrationState *s = migrate_get_current();
2168f9c5327SJuan Quintela 
2175e804644SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_X_COLO];
2185e804644SJuan Quintela }
2195e804644SJuan Quintela 
220a7a94d14SJuan Quintela bool migrate_compress(void)
221a7a94d14SJuan Quintela {
2228f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
223a7a94d14SJuan Quintela 
224a7a94d14SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_COMPRESS];
225a7a94d14SJuan Quintela }
226a7a94d14SJuan Quintela 
2271f0776f1SJuan Quintela bool migrate_dirty_bitmaps(void)
2281f0776f1SJuan Quintela {
2298f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
2301f0776f1SJuan Quintela 
2311f0776f1SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
2321f0776f1SJuan Quintela }
2331f0776f1SJuan Quintela 
234b890902cSJuan Quintela bool migrate_events(void)
235b890902cSJuan Quintela {
2368f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
237b890902cSJuan Quintela 
238b890902cSJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_EVENTS];
239b890902cSJuan Quintela }
240b890902cSJuan Quintela 
2411f0776f1SJuan Quintela bool migrate_ignore_shared(void)
2421f0776f1SJuan Quintela {
2438f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
2441f0776f1SJuan Quintela 
2451f0776f1SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
2461f0776f1SJuan Quintela }
2471f0776f1SJuan Quintela 
2481f0776f1SJuan Quintela bool migrate_late_block_activate(void)
2491f0776f1SJuan Quintela {
2508f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
2511f0776f1SJuan Quintela 
2521f0776f1SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
2531f0776f1SJuan Quintela }
2541f0776f1SJuan Quintela 
25551b07548SJuan Quintela bool migrate_multifd(void)
25651b07548SJuan Quintela {
2578f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
25851b07548SJuan Quintela 
25951b07548SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_MULTIFD];
26051b07548SJuan Quintela }
26151b07548SJuan Quintela 
2621f0776f1SJuan Quintela bool migrate_pause_before_switchover(void)
2631f0776f1SJuan Quintela {
2648f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
2651f0776f1SJuan Quintela 
2661f0776f1SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
2671f0776f1SJuan Quintela }
2681f0776f1SJuan Quintela 
2691f0776f1SJuan Quintela bool migrate_postcopy_blocktime(void)
2701f0776f1SJuan Quintela {
2718f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
2721f0776f1SJuan Quintela 
2731f0776f1SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
2741f0776f1SJuan Quintela }
2751f0776f1SJuan Quintela 
2761f0776f1SJuan Quintela bool migrate_postcopy_preempt(void)
2771f0776f1SJuan Quintela {
2788f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
2791f0776f1SJuan Quintela 
2801f0776f1SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT];
2811f0776f1SJuan Quintela }
2821f0776f1SJuan Quintela 
2831f0776f1SJuan Quintela bool migrate_postcopy_ram(void)
2841f0776f1SJuan Quintela {
2858f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
2861f0776f1SJuan Quintela 
2871f0776f1SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
2881f0776f1SJuan Quintela }
2891f0776f1SJuan Quintela 
29017cba690SJuan Quintela bool migrate_rdma_pin_all(void)
29117cba690SJuan Quintela {
29217cba690SJuan Quintela     MigrationState *s = migrate_get_current();
29317cba690SJuan Quintela 
29417cba690SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_RDMA_PIN_ALL];
29517cba690SJuan Quintela }
29617cba690SJuan Quintela 
2971f0776f1SJuan Quintela bool migrate_release_ram(void)
2981f0776f1SJuan Quintela {
2998f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
3001f0776f1SJuan Quintela 
3011f0776f1SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
3021f0776f1SJuan Quintela }
3031f0776f1SJuan Quintela 
30438ad1110SJuan Quintela bool migrate_return_path(void)
30538ad1110SJuan Quintela {
3068f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
30738ad1110SJuan Quintela 
30838ad1110SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
30938ad1110SJuan Quintela }
31038ad1110SJuan Quintela 
3111f0776f1SJuan Quintela bool migrate_validate_uuid(void)
3121f0776f1SJuan Quintela {
3138f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
3141f0776f1SJuan Quintela 
3151f0776f1SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
3161f0776f1SJuan Quintela }
3171f0776f1SJuan Quintela 
31887dca0c9SJuan Quintela bool migrate_xbzrle(void)
31987dca0c9SJuan Quintela {
3208f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
32187dca0c9SJuan Quintela 
32287dca0c9SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_XBZRLE];
32387dca0c9SJuan Quintela }
32487dca0c9SJuan Quintela 
3251f0776f1SJuan Quintela bool migrate_zero_blocks(void)
3261f0776f1SJuan Quintela {
3278f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
3281f0776f1SJuan Quintela 
3291f0776f1SJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
3301f0776f1SJuan Quintela }
331b4bc342cSJuan Quintela 
332b4bc342cSJuan Quintela bool migrate_zero_copy_send(void)
333b4bc342cSJuan Quintela {
3348f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
335b4bc342cSJuan Quintela 
336b4bc342cSJuan Quintela     return s->capabilities[MIGRATION_CAPABILITY_ZERO_COPY_SEND];
337b4bc342cSJuan Quintela }
338f774fde5SJuan Quintela 
339f774fde5SJuan Quintela /* pseudo capabilities */
340f774fde5SJuan Quintela 
34177c259a4SJuan Quintela bool migrate_multifd_flush_after_each_section(void)
34277c259a4SJuan Quintela {
34377c259a4SJuan Quintela     MigrationState *s = migrate_get_current();
34477c259a4SJuan Quintela 
345294e5a40SJuan Quintela     return s->multifd_flush_after_each_section;
34677c259a4SJuan Quintela }
34777c259a4SJuan Quintela 
348f774fde5SJuan Quintela bool migrate_postcopy(void)
349f774fde5SJuan Quintela {
350f774fde5SJuan Quintela     return migrate_postcopy_ram() || migrate_dirty_bitmaps();
351f774fde5SJuan Quintela }
352f774fde5SJuan Quintela 
35310d4703bSJuan Quintela bool migrate_tls(void)
35410d4703bSJuan Quintela {
3558f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
35610d4703bSJuan Quintela 
35710d4703bSJuan Quintela     return s->parameters.tls_creds && *s->parameters.tls_creds;
35810d4703bSJuan Quintela }
35910d4703bSJuan Quintela 
36077608706SJuan Quintela typedef enum WriteTrackingSupport {
36177608706SJuan Quintela     WT_SUPPORT_UNKNOWN = 0,
36277608706SJuan Quintela     WT_SUPPORT_ABSENT,
36377608706SJuan Quintela     WT_SUPPORT_AVAILABLE,
36477608706SJuan Quintela     WT_SUPPORT_COMPATIBLE
36577608706SJuan Quintela } WriteTrackingSupport;
36677608706SJuan Quintela 
36777608706SJuan Quintela static
36877608706SJuan Quintela WriteTrackingSupport migrate_query_write_tracking(void)
36977608706SJuan Quintela {
37077608706SJuan Quintela     /* Check if kernel supports required UFFD features */
37177608706SJuan Quintela     if (!ram_write_tracking_available()) {
37277608706SJuan Quintela         return WT_SUPPORT_ABSENT;
37377608706SJuan Quintela     }
37477608706SJuan Quintela     /*
37577608706SJuan Quintela      * Check if current memory configuration is
37677608706SJuan Quintela      * compatible with required UFFD features.
37777608706SJuan Quintela      */
37877608706SJuan Quintela     if (!ram_write_tracking_compatible()) {
37977608706SJuan Quintela         return WT_SUPPORT_AVAILABLE;
38077608706SJuan Quintela     }
38177608706SJuan Quintela 
38277608706SJuan Quintela     return WT_SUPPORT_COMPATIBLE;
38377608706SJuan Quintela }
38477608706SJuan Quintela 
38577608706SJuan Quintela /* Migration capabilities set */
38677608706SJuan Quintela struct MigrateCapsSet {
38777608706SJuan Quintela     int size;                       /* Capability set size */
38877608706SJuan Quintela     MigrationCapability caps[];     /* Variadic array of capabilities */
38977608706SJuan Quintela };
39077608706SJuan Quintela typedef struct MigrateCapsSet MigrateCapsSet;
39177608706SJuan Quintela 
39277608706SJuan Quintela /* Define and initialize MigrateCapsSet */
39377608706SJuan Quintela #define INITIALIZE_MIGRATE_CAPS_SET(_name, ...)   \
39477608706SJuan Quintela     MigrateCapsSet _name = {    \
39577608706SJuan Quintela         .size = sizeof((int []) { __VA_ARGS__ }) / sizeof(int), \
39677608706SJuan Quintela         .caps = { __VA_ARGS__ } \
39777608706SJuan Quintela     }
39877608706SJuan Quintela 
39977608706SJuan Quintela /* Background-snapshot compatibility check list */
40077608706SJuan Quintela static const
40177608706SJuan Quintela INITIALIZE_MIGRATE_CAPS_SET(check_caps_background_snapshot,
40277608706SJuan Quintela     MIGRATION_CAPABILITY_POSTCOPY_RAM,
40377608706SJuan Quintela     MIGRATION_CAPABILITY_DIRTY_BITMAPS,
40477608706SJuan Quintela     MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME,
40577608706SJuan Quintela     MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE,
40677608706SJuan Quintela     MIGRATION_CAPABILITY_RETURN_PATH,
40777608706SJuan Quintela     MIGRATION_CAPABILITY_MULTIFD,
40877608706SJuan Quintela     MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER,
40977608706SJuan Quintela     MIGRATION_CAPABILITY_AUTO_CONVERGE,
41077608706SJuan Quintela     MIGRATION_CAPABILITY_RELEASE_RAM,
41177608706SJuan Quintela     MIGRATION_CAPABILITY_RDMA_PIN_ALL,
41277608706SJuan Quintela     MIGRATION_CAPABILITY_COMPRESS,
41377608706SJuan Quintela     MIGRATION_CAPABILITY_XBZRLE,
41477608706SJuan Quintela     MIGRATION_CAPABILITY_X_COLO,
41577608706SJuan Quintela     MIGRATION_CAPABILITY_VALIDATE_UUID,
41677608706SJuan Quintela     MIGRATION_CAPABILITY_ZERO_COPY_SEND);
41777608706SJuan Quintela 
41877608706SJuan Quintela /**
41977608706SJuan Quintela  * @migration_caps_check - check capability compatibility
42077608706SJuan Quintela  *
42177608706SJuan Quintela  * @old_caps: old capability list
42277608706SJuan Quintela  * @new_caps: new capability list
42377608706SJuan Quintela  * @errp: set *errp if the check failed, with reason
42477608706SJuan Quintela  *
42577608706SJuan Quintela  * Returns true if check passed, otherwise false.
42677608706SJuan Quintela  */
42777608706SJuan Quintela bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
42877608706SJuan Quintela {
42977608706SJuan Quintela     MigrationIncomingState *mis = migration_incoming_get_current();
43077608706SJuan Quintela 
43174c38cf7SPeter Xu     ERRP_GUARD();
43277608706SJuan Quintela #ifndef CONFIG_LIVE_BLOCK_MIGRATION
43377608706SJuan Quintela     if (new_caps[MIGRATION_CAPABILITY_BLOCK]) {
43477608706SJuan Quintela         error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
43577608706SJuan Quintela                    "block migration");
43677608706SJuan Quintela         error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
43777608706SJuan Quintela         return false;
43877608706SJuan Quintela     }
43977608706SJuan Quintela #endif
44077608706SJuan Quintela 
44177608706SJuan Quintela #ifndef CONFIG_REPLICATION
44277608706SJuan Quintela     if (new_caps[MIGRATION_CAPABILITY_X_COLO]) {
44377608706SJuan Quintela         error_setg(errp, "QEMU compiled without replication module"
44477608706SJuan Quintela                    " can't enable COLO");
44577608706SJuan Quintela         error_append_hint(errp, "Please enable replication before COLO.\n");
44677608706SJuan Quintela         return false;
44777608706SJuan Quintela     }
44877608706SJuan Quintela #endif
44977608706SJuan Quintela 
45077608706SJuan Quintela     if (new_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
45177608706SJuan Quintela         /* This check is reasonably expensive, so only when it's being
45277608706SJuan Quintela          * set the first time, also it's only the destination that needs
45377608706SJuan Quintela          * special support.
45477608706SJuan Quintela          */
45577608706SJuan Quintela         if (!old_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM] &&
45677608706SJuan Quintela             runstate_check(RUN_STATE_INMIGRATE) &&
45774c38cf7SPeter Xu             !postcopy_ram_supported_by_host(mis, errp)) {
45874c38cf7SPeter Xu             error_prepend(errp, "Postcopy is not supported: ");
45977608706SJuan Quintela             return false;
46077608706SJuan Quintela         }
46177608706SJuan Quintela 
46277608706SJuan Quintela         if (new_caps[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
46377608706SJuan Quintela             error_setg(errp, "Postcopy is not compatible with ignore-shared");
46477608706SJuan Quintela             return false;
46577608706SJuan Quintela         }
466b405dfffSLeonardo Bras 
467b405dfffSLeonardo Bras         if (new_caps[MIGRATION_CAPABILITY_MULTIFD]) {
468b405dfffSLeonardo Bras             error_setg(errp, "Postcopy is not yet compatible with multifd");
469b405dfffSLeonardo Bras             return false;
470b405dfffSLeonardo Bras         }
47177608706SJuan Quintela     }
47277608706SJuan Quintela 
47377608706SJuan Quintela     if (new_caps[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT]) {
47477608706SJuan Quintela         WriteTrackingSupport wt_support;
47577608706SJuan Quintela         int idx;
47677608706SJuan Quintela         /*
47777608706SJuan Quintela          * Check if 'background-snapshot' capability is supported by
47877608706SJuan Quintela          * host kernel and compatible with guest memory configuration.
47977608706SJuan Quintela          */
48077608706SJuan Quintela         wt_support = migrate_query_write_tracking();
48177608706SJuan Quintela         if (wt_support < WT_SUPPORT_AVAILABLE) {
48277608706SJuan Quintela             error_setg(errp, "Background-snapshot is not supported by host kernel");
48377608706SJuan Quintela             return false;
48477608706SJuan Quintela         }
48577608706SJuan Quintela         if (wt_support < WT_SUPPORT_COMPATIBLE) {
48677608706SJuan Quintela             error_setg(errp, "Background-snapshot is not compatible "
48777608706SJuan Quintela                     "with guest memory configuration");
48877608706SJuan Quintela             return false;
48977608706SJuan Quintela         }
49077608706SJuan Quintela 
49177608706SJuan Quintela         /*
49277608706SJuan Quintela          * Check if there are any migration capabilities
49377608706SJuan Quintela          * incompatible with 'background-snapshot'.
49477608706SJuan Quintela          */
49577608706SJuan Quintela         for (idx = 0; idx < check_caps_background_snapshot.size; idx++) {
49677608706SJuan Quintela             int incomp_cap = check_caps_background_snapshot.caps[idx];
49777608706SJuan Quintela             if (new_caps[incomp_cap]) {
49877608706SJuan Quintela                 error_setg(errp,
49977608706SJuan Quintela                         "Background-snapshot is not compatible with %s",
50077608706SJuan Quintela                         MigrationCapability_str(incomp_cap));
50177608706SJuan Quintela                 return false;
50277608706SJuan Quintela             }
50377608706SJuan Quintela         }
50477608706SJuan Quintela     }
50577608706SJuan Quintela 
50677608706SJuan Quintela #ifdef CONFIG_LINUX
50777608706SJuan Quintela     if (new_caps[MIGRATION_CAPABILITY_ZERO_COPY_SEND] &&
50877608706SJuan Quintela         (!new_caps[MIGRATION_CAPABILITY_MULTIFD] ||
50977608706SJuan Quintela          new_caps[MIGRATION_CAPABILITY_COMPRESS] ||
51077608706SJuan Quintela          new_caps[MIGRATION_CAPABILITY_XBZRLE] ||
51177608706SJuan Quintela          migrate_multifd_compression() ||
51210d4703bSJuan Quintela          migrate_tls())) {
51377608706SJuan Quintela         error_setg(errp,
51477608706SJuan Quintela                    "Zero copy only available for non-compressed non-TLS multifd migration");
51577608706SJuan Quintela         return false;
51677608706SJuan Quintela     }
51777608706SJuan Quintela #else
51877608706SJuan Quintela     if (new_caps[MIGRATION_CAPABILITY_ZERO_COPY_SEND]) {
51977608706SJuan Quintela         error_setg(errp,
52077608706SJuan Quintela                    "Zero copy currently only available on Linux");
52177608706SJuan Quintela         return false;
52277608706SJuan Quintela     }
52377608706SJuan Quintela #endif
52477608706SJuan Quintela 
52577608706SJuan Quintela     if (new_caps[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT]) {
52677608706SJuan Quintela         if (!new_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
52777608706SJuan Quintela             error_setg(errp, "Postcopy preempt requires postcopy-ram");
52877608706SJuan Quintela             return false;
52977608706SJuan Quintela         }
53077608706SJuan Quintela 
53177608706SJuan Quintela         /*
53277608706SJuan Quintela          * Preempt mode requires urgent pages to be sent in separate
53377608706SJuan Quintela          * channel, OTOH compression logic will disorder all pages into
53477608706SJuan Quintela          * different compression channels, which is not compatible with the
53577608706SJuan Quintela          * preempt assumptions on channel assignments.
53677608706SJuan Quintela          */
53777608706SJuan Quintela         if (new_caps[MIGRATION_CAPABILITY_COMPRESS]) {
53877608706SJuan Quintela             error_setg(errp, "Postcopy preempt not compatible with compress");
53977608706SJuan Quintela             return false;
54077608706SJuan Quintela         }
54177608706SJuan Quintela     }
54277608706SJuan Quintela 
54377608706SJuan Quintela     if (new_caps[MIGRATION_CAPABILITY_MULTIFD]) {
54477608706SJuan Quintela         if (new_caps[MIGRATION_CAPABILITY_COMPRESS]) {
54577608706SJuan Quintela             error_setg(errp, "Multifd is not compatible with compress");
54677608706SJuan Quintela             return false;
54777608706SJuan Quintela         }
54877608706SJuan Quintela     }
54977608706SJuan Quintela 
55077608706SJuan Quintela     return true;
55177608706SJuan Quintela }
5524d0c6b69SJuan Quintela 
553f80196b7SJuan Quintela bool migrate_cap_set(int cap, bool value, Error **errp)
554f80196b7SJuan Quintela {
555f80196b7SJuan Quintela     MigrationState *s = migrate_get_current();
556f80196b7SJuan Quintela     bool new_caps[MIGRATION_CAPABILITY__MAX];
557f80196b7SJuan Quintela 
558f80196b7SJuan Quintela     if (migration_is_running(s->state)) {
559f80196b7SJuan Quintela         error_setg(errp, QERR_MIGRATION_ACTIVE);
560f80196b7SJuan Quintela         return false;
561f80196b7SJuan Quintela     }
562f80196b7SJuan Quintela 
563f80196b7SJuan Quintela     memcpy(new_caps, s->capabilities, sizeof(new_caps));
564f80196b7SJuan Quintela     new_caps[cap] = value;
565f80196b7SJuan Quintela 
566f80196b7SJuan Quintela     if (!migrate_caps_check(s->capabilities, new_caps, errp)) {
567f80196b7SJuan Quintela         return false;
568f80196b7SJuan Quintela     }
569f80196b7SJuan Quintela     s->capabilities[cap] = value;
570f80196b7SJuan Quintela     return true;
571f80196b7SJuan Quintela }
572f80196b7SJuan Quintela 
5734d0c6b69SJuan Quintela MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
5744d0c6b69SJuan Quintela {
5754d0c6b69SJuan Quintela     MigrationCapabilityStatusList *head = NULL, **tail = &head;
5764d0c6b69SJuan Quintela     MigrationCapabilityStatus *caps;
5774d0c6b69SJuan Quintela     MigrationState *s = migrate_get_current();
5784d0c6b69SJuan Quintela     int i;
5794d0c6b69SJuan Quintela 
5804d0c6b69SJuan Quintela     for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
5814d0c6b69SJuan Quintela #ifndef CONFIG_LIVE_BLOCK_MIGRATION
5824d0c6b69SJuan Quintela         if (i == MIGRATION_CAPABILITY_BLOCK) {
5834d0c6b69SJuan Quintela             continue;
5844d0c6b69SJuan Quintela         }
5854d0c6b69SJuan Quintela #endif
5864d0c6b69SJuan Quintela         caps = g_malloc0(sizeof(*caps));
5874d0c6b69SJuan Quintela         caps->capability = i;
5884d0c6b69SJuan Quintela         caps->state = s->capabilities[i];
5894d0c6b69SJuan Quintela         QAPI_LIST_APPEND(tail, caps);
5904d0c6b69SJuan Quintela     }
5914d0c6b69SJuan Quintela 
5924d0c6b69SJuan Quintela     return head;
5934d0c6b69SJuan Quintela }
59445c1de13SJuan Quintela 
59545c1de13SJuan Quintela void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
59645c1de13SJuan Quintela                                   Error **errp)
59745c1de13SJuan Quintela {
59845c1de13SJuan Quintela     MigrationState *s = migrate_get_current();
59945c1de13SJuan Quintela     MigrationCapabilityStatusList *cap;
60045c1de13SJuan Quintela     bool new_caps[MIGRATION_CAPABILITY__MAX];
60145c1de13SJuan Quintela 
602d70178a8SVladimir Sementsov-Ogievskiy     if (migration_is_running(s->state) || migration_in_colo_state()) {
60345c1de13SJuan Quintela         error_setg(errp, QERR_MIGRATION_ACTIVE);
60445c1de13SJuan Quintela         return;
60545c1de13SJuan Quintela     }
60645c1de13SJuan Quintela 
60745c1de13SJuan Quintela     memcpy(new_caps, s->capabilities, sizeof(new_caps));
60845c1de13SJuan Quintela     for (cap = params; cap; cap = cap->next) {
60945c1de13SJuan Quintela         new_caps[cap->value->capability] = cap->value->state;
61045c1de13SJuan Quintela     }
61145c1de13SJuan Quintela 
61245c1de13SJuan Quintela     if (!migrate_caps_check(s->capabilities, new_caps, errp)) {
61345c1de13SJuan Quintela         return;
61445c1de13SJuan Quintela     }
61545c1de13SJuan Quintela 
61645c1de13SJuan Quintela     for (cap = params; cap; cap = cap->next) {
61745c1de13SJuan Quintela         s->capabilities[cap->value->capability] = cap->value->state;
61845c1de13SJuan Quintela     }
61945c1de13SJuan Quintela }
6201dfc4b9eSJuan Quintela 
6211dfc4b9eSJuan Quintela /* parameters */
6221dfc4b9eSJuan Quintela 
623b804b35bSJuan Quintela const BitmapMigrationNodeAliasList *migrate_block_bitmap_mapping(void)
624b804b35bSJuan Quintela {
625b804b35bSJuan Quintela     MigrationState *s = migrate_get_current();
626b804b35bSJuan Quintela 
627b804b35bSJuan Quintela     return s->parameters.block_bitmap_mapping;
628b804b35bSJuan Quintela }
629b804b35bSJuan Quintela 
6303cba22c9SJuan Quintela bool migrate_has_block_bitmap_mapping(void)
6313cba22c9SJuan Quintela {
6323cba22c9SJuan Quintela     MigrationState *s = migrate_get_current();
6333cba22c9SJuan Quintela 
6343cba22c9SJuan Quintela     return s->parameters.has_block_bitmap_mapping;
6353cba22c9SJuan Quintela }
6363cba22c9SJuan Quintela 
6376f8be708SJuan Quintela bool migrate_block_incremental(void)
6386f8be708SJuan Quintela {
6398f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
6406f8be708SJuan Quintela 
6416f8be708SJuan Quintela     return s->parameters.block_incremental;
6426f8be708SJuan Quintela }
6436f8be708SJuan Quintela 
644f94a858fSJuan Quintela uint32_t migrate_checkpoint_delay(void)
645f94a858fSJuan Quintela {
6468f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
647f94a858fSJuan Quintela 
648f94a858fSJuan Quintela     return s->parameters.x_checkpoint_delay;
649f94a858fSJuan Quintela }
650f94a858fSJuan Quintela 
6511dfc4b9eSJuan Quintela int migrate_compress_level(void)
6521dfc4b9eSJuan Quintela {
6538f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
6541dfc4b9eSJuan Quintela 
6551dfc4b9eSJuan Quintela     return s->parameters.compress_level;
6561dfc4b9eSJuan Quintela }
6571dfc4b9eSJuan Quintela 
6581dfc4b9eSJuan Quintela int migrate_compress_threads(void)
6591dfc4b9eSJuan Quintela {
6608f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
6611dfc4b9eSJuan Quintela 
6621dfc4b9eSJuan Quintela     return s->parameters.compress_threads;
6631dfc4b9eSJuan Quintela }
6641dfc4b9eSJuan Quintela 
6651dfc4b9eSJuan Quintela int migrate_compress_wait_thread(void)
6661dfc4b9eSJuan Quintela {
6678f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
6681dfc4b9eSJuan Quintela 
6691dfc4b9eSJuan Quintela     return s->parameters.compress_wait_thread;
6701dfc4b9eSJuan Quintela }
6711dfc4b9eSJuan Quintela 
6729605c2acSJuan Quintela uint8_t migrate_cpu_throttle_increment(void)
6739605c2acSJuan Quintela {
6748f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
6759605c2acSJuan Quintela 
6769605c2acSJuan Quintela     return s->parameters.cpu_throttle_increment;
6779605c2acSJuan Quintela }
6789605c2acSJuan Quintela 
6792a8ec380SJuan Quintela uint8_t migrate_cpu_throttle_initial(void)
6802a8ec380SJuan Quintela {
6818f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
6822a8ec380SJuan Quintela 
6832a8ec380SJuan Quintela     return s->parameters.cpu_throttle_initial;
6842a8ec380SJuan Quintela }
6852a8ec380SJuan Quintela 
686873f674cSJuan Quintela bool migrate_cpu_throttle_tailslow(void)
687873f674cSJuan Quintela {
6888f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
689873f674cSJuan Quintela 
690873f674cSJuan Quintela     return s->parameters.cpu_throttle_tailslow;
691873f674cSJuan Quintela }
692873f674cSJuan Quintela 
6931dfc4b9eSJuan Quintela int migrate_decompress_threads(void)
6941dfc4b9eSJuan Quintela {
6958f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
6961dfc4b9eSJuan Quintela 
6971dfc4b9eSJuan Quintela     return s->parameters.decompress_threads;
6981dfc4b9eSJuan Quintela }
6991dfc4b9eSJuan Quintela 
700f5da8ba4SJuan Quintela uint64_t migrate_downtime_limit(void)
701f5da8ba4SJuan Quintela {
702f5da8ba4SJuan Quintela     MigrationState *s = migrate_get_current();
703f5da8ba4SJuan Quintela 
704f5da8ba4SJuan Quintela     return s->parameters.downtime_limit;
705f5da8ba4SJuan Quintela }
706f5da8ba4SJuan Quintela 
70724155bd0SJuan Quintela uint8_t migrate_max_cpu_throttle(void)
70824155bd0SJuan Quintela {
7098f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
71024155bd0SJuan Quintela 
71124155bd0SJuan Quintela     return s->parameters.max_cpu_throttle;
71224155bd0SJuan Quintela }
71324155bd0SJuan Quintela 
7149c894df3SJuan Quintela uint64_t migrate_max_bandwidth(void)
7159c894df3SJuan Quintela {
7168f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
7179c894df3SJuan Quintela 
7189c894df3SJuan Quintela     return s->parameters.max_bandwidth;
7199c894df3SJuan Quintela }
7209c894df3SJuan Quintela 
72152033349SJuan Quintela uint64_t migrate_max_postcopy_bandwidth(void)
7221dfc4b9eSJuan Quintela {
7238f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
7241dfc4b9eSJuan Quintela 
7251dfc4b9eSJuan Quintela     return s->parameters.max_postcopy_bandwidth;
7261dfc4b9eSJuan Quintela }
7271dfc4b9eSJuan Quintela 
7281dfc4b9eSJuan Quintela int migrate_multifd_channels(void)
7291dfc4b9eSJuan Quintela {
7308f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
7311dfc4b9eSJuan Quintela 
7321dfc4b9eSJuan Quintela     return s->parameters.multifd_channels;
7331dfc4b9eSJuan Quintela }
7341dfc4b9eSJuan Quintela 
7351dfc4b9eSJuan Quintela MultiFDCompression migrate_multifd_compression(void)
7361dfc4b9eSJuan Quintela {
7378f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
7381dfc4b9eSJuan Quintela 
7391dfc4b9eSJuan Quintela     assert(s->parameters.multifd_compression < MULTIFD_COMPRESSION__MAX);
7401dfc4b9eSJuan Quintela     return s->parameters.multifd_compression;
7411dfc4b9eSJuan Quintela }
7421dfc4b9eSJuan Quintela 
7431dfc4b9eSJuan Quintela int migrate_multifd_zlib_level(void)
7441dfc4b9eSJuan Quintela {
7458f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
7461dfc4b9eSJuan Quintela 
7471dfc4b9eSJuan Quintela     return s->parameters.multifd_zlib_level;
7481dfc4b9eSJuan Quintela }
7491dfc4b9eSJuan Quintela 
7501dfc4b9eSJuan Quintela int migrate_multifd_zstd_level(void)
7511dfc4b9eSJuan Quintela {
7528f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
7531dfc4b9eSJuan Quintela 
7541dfc4b9eSJuan Quintela     return s->parameters.multifd_zstd_level;
7551dfc4b9eSJuan Quintela }
7561dfc4b9eSJuan Quintela 
7576499efdbSJuan Quintela uint8_t migrate_throttle_trigger_threshold(void)
7586499efdbSJuan Quintela {
7598f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
7606499efdbSJuan Quintela 
7616499efdbSJuan Quintela     return s->parameters.throttle_trigger_threshold;
7626499efdbSJuan Quintela }
7636499efdbSJuan Quintela 
7642eb0308bSJuan Quintela const char *migrate_tls_authz(void)
7652eb0308bSJuan Quintela {
7662eb0308bSJuan Quintela     MigrationState *s = migrate_get_current();
7672eb0308bSJuan Quintela 
7682eb0308bSJuan Quintela     return s->parameters.tls_authz;
7692eb0308bSJuan Quintela }
7702eb0308bSJuan Quintela 
771d5c3e195SJuan Quintela const char *migrate_tls_creds(void)
772d5c3e195SJuan Quintela {
773d5c3e195SJuan Quintela     MigrationState *s = migrate_get_current();
774d5c3e195SJuan Quintela 
775d5c3e195SJuan Quintela     return s->parameters.tls_creds;
776d5c3e195SJuan Quintela }
777d5c3e195SJuan Quintela 
7781f2f366cSJuan Quintela const char *migrate_tls_hostname(void)
7791f2f366cSJuan Quintela {
7801f2f366cSJuan Quintela     MigrationState *s = migrate_get_current();
7811f2f366cSJuan Quintela 
7821f2f366cSJuan Quintela     return s->parameters.tls_hostname;
7831f2f366cSJuan Quintela }
7841f2f366cSJuan Quintela 
7851dfc4b9eSJuan Quintela uint64_t migrate_xbzrle_cache_size(void)
7861dfc4b9eSJuan Quintela {
7878f9c5327SJuan Quintela     MigrationState *s = migrate_get_current();
7881dfc4b9eSJuan Quintela 
7891dfc4b9eSJuan Quintela     return s->parameters.xbzrle_cache_size;
7901dfc4b9eSJuan Quintela }
7912682c4eeSJuan Quintela 
79287c22901SJuan Quintela /* parameter setters */
79387c22901SJuan Quintela 
79487c22901SJuan Quintela void migrate_set_block_incremental(bool value)
79587c22901SJuan Quintela {
79687c22901SJuan Quintela     MigrationState *s = migrate_get_current();
79787c22901SJuan Quintela 
79887c22901SJuan Quintela     s->parameters.block_incremental = value;
79987c22901SJuan Quintela }
80087c22901SJuan Quintela 
8012682c4eeSJuan Quintela /* parameters helpers */
8022682c4eeSJuan Quintela 
803b1a87956SJuan Quintela void block_cleanup_parameters(void)
804b7b73122SJuan Quintela {
805b1a87956SJuan Quintela     MigrationState *s = migrate_get_current();
806b1a87956SJuan Quintela 
807b7b73122SJuan Quintela     if (s->must_remove_block_options) {
808b7b73122SJuan Quintela         /* setting to false can never fail */
809b7b73122SJuan Quintela         migrate_cap_set(MIGRATION_CAPABILITY_BLOCK, false, &error_abort);
810b7b73122SJuan Quintela         migrate_set_block_incremental(false);
811b7b73122SJuan Quintela         s->must_remove_block_options = false;
812b7b73122SJuan Quintela     }
813b7b73122SJuan Quintela }
814b7b73122SJuan Quintela 
8152682c4eeSJuan Quintela AnnounceParameters *migrate_announce_params(void)
8162682c4eeSJuan Quintela {
8172682c4eeSJuan Quintela     static AnnounceParameters ap;
8182682c4eeSJuan Quintela 
8192682c4eeSJuan Quintela     MigrationState *s = migrate_get_current();
8202682c4eeSJuan Quintela 
8212682c4eeSJuan Quintela     ap.initial = s->parameters.announce_initial;
8222682c4eeSJuan Quintela     ap.max = s->parameters.announce_max;
8232682c4eeSJuan Quintela     ap.rounds = s->parameters.announce_rounds;
8242682c4eeSJuan Quintela     ap.step = s->parameters.announce_step;
8252682c4eeSJuan Quintela 
8262682c4eeSJuan Quintela     return &ap;
8272682c4eeSJuan Quintela }
8289c894df3SJuan Quintela 
8299c894df3SJuan Quintela MigrationParameters *qmp_query_migrate_parameters(Error **errp)
8309c894df3SJuan Quintela {
8319c894df3SJuan Quintela     MigrationParameters *params;
8329c894df3SJuan Quintela     MigrationState *s = migrate_get_current();
8339c894df3SJuan Quintela 
8349c894df3SJuan Quintela     /* TODO use QAPI_CLONE() instead of duplicating it inline */
8359c894df3SJuan Quintela     params = g_malloc0(sizeof(*params));
8369c894df3SJuan Quintela     params->has_compress_level = true;
8379c894df3SJuan Quintela     params->compress_level = s->parameters.compress_level;
8389c894df3SJuan Quintela     params->has_compress_threads = true;
8399c894df3SJuan Quintela     params->compress_threads = s->parameters.compress_threads;
8409c894df3SJuan Quintela     params->has_compress_wait_thread = true;
8419c894df3SJuan Quintela     params->compress_wait_thread = s->parameters.compress_wait_thread;
8429c894df3SJuan Quintela     params->has_decompress_threads = true;
8439c894df3SJuan Quintela     params->decompress_threads = s->parameters.decompress_threads;
8449c894df3SJuan Quintela     params->has_throttle_trigger_threshold = true;
8459c894df3SJuan Quintela     params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
8469c894df3SJuan Quintela     params->has_cpu_throttle_initial = true;
8479c894df3SJuan Quintela     params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
8489c894df3SJuan Quintela     params->has_cpu_throttle_increment = true;
8499c894df3SJuan Quintela     params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
8509c894df3SJuan Quintela     params->has_cpu_throttle_tailslow = true;
8519c894df3SJuan Quintela     params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
8529c894df3SJuan Quintela     params->tls_creds = g_strdup(s->parameters.tls_creds);
8539c894df3SJuan Quintela     params->tls_hostname = g_strdup(s->parameters.tls_hostname);
8549c894df3SJuan Quintela     params->tls_authz = g_strdup(s->parameters.tls_authz ?
8559c894df3SJuan Quintela                                  s->parameters.tls_authz : "");
8569c894df3SJuan Quintela     params->has_max_bandwidth = true;
8579c894df3SJuan Quintela     params->max_bandwidth = s->parameters.max_bandwidth;
8589c894df3SJuan Quintela     params->has_downtime_limit = true;
8599c894df3SJuan Quintela     params->downtime_limit = s->parameters.downtime_limit;
8609c894df3SJuan Quintela     params->has_x_checkpoint_delay = true;
8619c894df3SJuan Quintela     params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
8629c894df3SJuan Quintela     params->has_block_incremental = true;
8639c894df3SJuan Quintela     params->block_incremental = s->parameters.block_incremental;
8649c894df3SJuan Quintela     params->has_multifd_channels = true;
8659c894df3SJuan Quintela     params->multifd_channels = s->parameters.multifd_channels;
8669c894df3SJuan Quintela     params->has_multifd_compression = true;
8679c894df3SJuan Quintela     params->multifd_compression = s->parameters.multifd_compression;
8689c894df3SJuan Quintela     params->has_multifd_zlib_level = true;
8699c894df3SJuan Quintela     params->multifd_zlib_level = s->parameters.multifd_zlib_level;
8709c894df3SJuan Quintela     params->has_multifd_zstd_level = true;
8719c894df3SJuan Quintela     params->multifd_zstd_level = s->parameters.multifd_zstd_level;
8729c894df3SJuan Quintela     params->has_xbzrle_cache_size = true;
8739c894df3SJuan Quintela     params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
8749c894df3SJuan Quintela     params->has_max_postcopy_bandwidth = true;
8759c894df3SJuan Quintela     params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
8769c894df3SJuan Quintela     params->has_max_cpu_throttle = true;
8779c894df3SJuan Quintela     params->max_cpu_throttle = s->parameters.max_cpu_throttle;
8789c894df3SJuan Quintela     params->has_announce_initial = true;
8799c894df3SJuan Quintela     params->announce_initial = s->parameters.announce_initial;
8809c894df3SJuan Quintela     params->has_announce_max = true;
8819c894df3SJuan Quintela     params->announce_max = s->parameters.announce_max;
8829c894df3SJuan Quintela     params->has_announce_rounds = true;
8839c894df3SJuan Quintela     params->announce_rounds = s->parameters.announce_rounds;
8849c894df3SJuan Quintela     params->has_announce_step = true;
8859c894df3SJuan Quintela     params->announce_step = s->parameters.announce_step;
8869c894df3SJuan Quintela 
8879c894df3SJuan Quintela     if (s->parameters.has_block_bitmap_mapping) {
8889c894df3SJuan Quintela         params->has_block_bitmap_mapping = true;
8899c894df3SJuan Quintela         params->block_bitmap_mapping =
8909c894df3SJuan Quintela             QAPI_CLONE(BitmapMigrationNodeAliasList,
8919c894df3SJuan Quintela                        s->parameters.block_bitmap_mapping);
8929c894df3SJuan Quintela     }
8939c894df3SJuan Quintela 
8949c894df3SJuan Quintela     return params;
8959c894df3SJuan Quintela }
89609d6c965SJuan Quintela 
89761a174e2SJuan Quintela void migrate_params_init(MigrationParameters *params)
89861a174e2SJuan Quintela {
89961a174e2SJuan Quintela     params->tls_hostname = g_strdup("");
90061a174e2SJuan Quintela     params->tls_creds = g_strdup("");
90161a174e2SJuan Quintela 
90261a174e2SJuan Quintela     /* Set has_* up only for parameter checks */
90361a174e2SJuan Quintela     params->has_compress_level = true;
90461a174e2SJuan Quintela     params->has_compress_threads = true;
90561a174e2SJuan Quintela     params->has_compress_wait_thread = true;
90661a174e2SJuan Quintela     params->has_decompress_threads = true;
90761a174e2SJuan Quintela     params->has_throttle_trigger_threshold = true;
90861a174e2SJuan Quintela     params->has_cpu_throttle_initial = true;
90961a174e2SJuan Quintela     params->has_cpu_throttle_increment = true;
91061a174e2SJuan Quintela     params->has_cpu_throttle_tailslow = true;
91161a174e2SJuan Quintela     params->has_max_bandwidth = true;
91261a174e2SJuan Quintela     params->has_downtime_limit = true;
91361a174e2SJuan Quintela     params->has_x_checkpoint_delay = true;
91461a174e2SJuan Quintela     params->has_block_incremental = true;
91561a174e2SJuan Quintela     params->has_multifd_channels = true;
91661a174e2SJuan Quintela     params->has_multifd_compression = true;
91761a174e2SJuan Quintela     params->has_multifd_zlib_level = true;
91861a174e2SJuan Quintela     params->has_multifd_zstd_level = true;
91961a174e2SJuan Quintela     params->has_xbzrle_cache_size = true;
92061a174e2SJuan Quintela     params->has_max_postcopy_bandwidth = true;
92161a174e2SJuan Quintela     params->has_max_cpu_throttle = true;
92261a174e2SJuan Quintela     params->has_announce_initial = true;
92361a174e2SJuan Quintela     params->has_announce_max = true;
92461a174e2SJuan Quintela     params->has_announce_rounds = true;
92561a174e2SJuan Quintela     params->has_announce_step = true;
92661a174e2SJuan Quintela }
92761a174e2SJuan Quintela 
92809d6c965SJuan Quintela /*
92909d6c965SJuan Quintela  * Check whether the parameters are valid. Error will be put into errp
93009d6c965SJuan Quintela  * (if provided). Return true if valid, otherwise false.
93109d6c965SJuan Quintela  */
93209d6c965SJuan Quintela bool migrate_params_check(MigrationParameters *params, Error **errp)
93309d6c965SJuan Quintela {
93409d6c965SJuan Quintela     if (params->has_compress_level &&
93509d6c965SJuan Quintela         (params->compress_level > 9)) {
93609d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
93709d6c965SJuan Quintela                    "a value between 0 and 9");
93809d6c965SJuan Quintela         return false;
93909d6c965SJuan Quintela     }
94009d6c965SJuan Quintela 
94109d6c965SJuan Quintela     if (params->has_compress_threads && (params->compress_threads < 1)) {
94209d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
94309d6c965SJuan Quintela                    "compress_threads",
94409d6c965SJuan Quintela                    "a value between 1 and 255");
94509d6c965SJuan Quintela         return false;
94609d6c965SJuan Quintela     }
94709d6c965SJuan Quintela 
94809d6c965SJuan Quintela     if (params->has_decompress_threads && (params->decompress_threads < 1)) {
94909d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
95009d6c965SJuan Quintela                    "decompress_threads",
95109d6c965SJuan Quintela                    "a value between 1 and 255");
95209d6c965SJuan Quintela         return false;
95309d6c965SJuan Quintela     }
95409d6c965SJuan Quintela 
95509d6c965SJuan Quintela     if (params->has_throttle_trigger_threshold &&
95609d6c965SJuan Quintela         (params->throttle_trigger_threshold < 1 ||
95709d6c965SJuan Quintela          params->throttle_trigger_threshold > 100)) {
95809d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
95909d6c965SJuan Quintela                    "throttle_trigger_threshold",
96009d6c965SJuan Quintela                    "an integer in the range of 1 to 100");
96109d6c965SJuan Quintela         return false;
96209d6c965SJuan Quintela     }
96309d6c965SJuan Quintela 
96409d6c965SJuan Quintela     if (params->has_cpu_throttle_initial &&
96509d6c965SJuan Quintela         (params->cpu_throttle_initial < 1 ||
96609d6c965SJuan Quintela          params->cpu_throttle_initial > 99)) {
96709d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
96809d6c965SJuan Quintela                    "cpu_throttle_initial",
96909d6c965SJuan Quintela                    "an integer in the range of 1 to 99");
97009d6c965SJuan Quintela         return false;
97109d6c965SJuan Quintela     }
97209d6c965SJuan Quintela 
97309d6c965SJuan Quintela     if (params->has_cpu_throttle_increment &&
97409d6c965SJuan Quintela         (params->cpu_throttle_increment < 1 ||
97509d6c965SJuan Quintela          params->cpu_throttle_increment > 99)) {
97609d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
97709d6c965SJuan Quintela                    "cpu_throttle_increment",
97809d6c965SJuan Quintela                    "an integer in the range of 1 to 99");
97909d6c965SJuan Quintela         return false;
98009d6c965SJuan Quintela     }
98109d6c965SJuan Quintela 
98209d6c965SJuan Quintela     if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
98309d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
98409d6c965SJuan Quintela                    "max_bandwidth",
98509d6c965SJuan Quintela                    "an integer in the range of 0 to "stringify(SIZE_MAX)
98609d6c965SJuan Quintela                    " bytes/second");
98709d6c965SJuan Quintela         return false;
98809d6c965SJuan Quintela     }
98909d6c965SJuan Quintela 
99009d6c965SJuan Quintela     if (params->has_downtime_limit &&
99109d6c965SJuan Quintela         (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
99209d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
99309d6c965SJuan Quintela                    "downtime_limit",
99409d6c965SJuan Quintela                    "an integer in the range of 0 to "
99509d6c965SJuan Quintela                     stringify(MAX_MIGRATE_DOWNTIME)" ms");
99609d6c965SJuan Quintela         return false;
99709d6c965SJuan Quintela     }
99809d6c965SJuan Quintela 
99909d6c965SJuan Quintela     /* x_checkpoint_delay is now always positive */
100009d6c965SJuan Quintela 
100109d6c965SJuan Quintela     if (params->has_multifd_channels && (params->multifd_channels < 1)) {
100209d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
100309d6c965SJuan Quintela                    "multifd_channels",
100409d6c965SJuan Quintela                    "a value between 1 and 255");
100509d6c965SJuan Quintela         return false;
100609d6c965SJuan Quintela     }
100709d6c965SJuan Quintela 
100809d6c965SJuan Quintela     if (params->has_multifd_zlib_level &&
100909d6c965SJuan Quintela         (params->multifd_zlib_level > 9)) {
101009d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
101109d6c965SJuan Quintela                    "a value between 0 and 9");
101209d6c965SJuan Quintela         return false;
101309d6c965SJuan Quintela     }
101409d6c965SJuan Quintela 
101509d6c965SJuan Quintela     if (params->has_multifd_zstd_level &&
101609d6c965SJuan Quintela         (params->multifd_zstd_level > 20)) {
101709d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
101809d6c965SJuan Quintela                    "a value between 0 and 20");
101909d6c965SJuan Quintela         return false;
102009d6c965SJuan Quintela     }
102109d6c965SJuan Quintela 
102209d6c965SJuan Quintela     if (params->has_xbzrle_cache_size &&
102309d6c965SJuan Quintela         (params->xbzrle_cache_size < qemu_target_page_size() ||
102409d6c965SJuan Quintela          !is_power_of_2(params->xbzrle_cache_size))) {
102509d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
102609d6c965SJuan Quintela                    "xbzrle_cache_size",
102709d6c965SJuan Quintela                    "a power of two no less than the target page size");
102809d6c965SJuan Quintela         return false;
102909d6c965SJuan Quintela     }
103009d6c965SJuan Quintela 
103109d6c965SJuan Quintela     if (params->has_max_cpu_throttle &&
103209d6c965SJuan Quintela         (params->max_cpu_throttle < params->cpu_throttle_initial ||
103309d6c965SJuan Quintela          params->max_cpu_throttle > 99)) {
103409d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
103509d6c965SJuan Quintela                    "max_cpu_throttle",
103609d6c965SJuan Quintela                    "an integer in the range of cpu_throttle_initial to 99");
103709d6c965SJuan Quintela         return false;
103809d6c965SJuan Quintela     }
103909d6c965SJuan Quintela 
104009d6c965SJuan Quintela     if (params->has_announce_initial &&
104109d6c965SJuan Quintela         params->announce_initial > 100000) {
104209d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
104309d6c965SJuan Quintela                    "announce_initial",
104409d6c965SJuan Quintela                    "a value between 0 and 100000");
104509d6c965SJuan Quintela         return false;
104609d6c965SJuan Quintela     }
104709d6c965SJuan Quintela     if (params->has_announce_max &&
104809d6c965SJuan Quintela         params->announce_max > 100000) {
104909d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
105009d6c965SJuan Quintela                    "announce_max",
105109d6c965SJuan Quintela                    "a value between 0 and 100000");
105209d6c965SJuan Quintela        return false;
105309d6c965SJuan Quintela     }
105409d6c965SJuan Quintela     if (params->has_announce_rounds &&
105509d6c965SJuan Quintela         params->announce_rounds > 1000) {
105609d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
105709d6c965SJuan Quintela                    "announce_rounds",
105809d6c965SJuan Quintela                    "a value between 0 and 1000");
105909d6c965SJuan Quintela        return false;
106009d6c965SJuan Quintela     }
106109d6c965SJuan Quintela     if (params->has_announce_step &&
106209d6c965SJuan Quintela         (params->announce_step < 1 ||
106309d6c965SJuan Quintela         params->announce_step > 10000)) {
106409d6c965SJuan Quintela         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
106509d6c965SJuan Quintela                    "announce_step",
106609d6c965SJuan Quintela                    "a value between 0 and 10000");
106709d6c965SJuan Quintela        return false;
106809d6c965SJuan Quintela     }
106909d6c965SJuan Quintela 
107009d6c965SJuan Quintela     if (params->has_block_bitmap_mapping &&
107109d6c965SJuan Quintela         !check_dirty_bitmap_mig_alias_map(params->block_bitmap_mapping, errp)) {
107209d6c965SJuan Quintela         error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: ");
107309d6c965SJuan Quintela         return false;
107409d6c965SJuan Quintela     }
107509d6c965SJuan Quintela 
107609d6c965SJuan Quintela #ifdef CONFIG_LINUX
107709d6c965SJuan Quintela     if (migrate_zero_copy_send() &&
107809d6c965SJuan Quintela         ((params->has_multifd_compression && params->multifd_compression) ||
107909d6c965SJuan Quintela          (params->tls_creds && *params->tls_creds))) {
108009d6c965SJuan Quintela         error_setg(errp,
108109d6c965SJuan Quintela                    "Zero copy only available for non-compressed non-TLS multifd migration");
108209d6c965SJuan Quintela         return false;
108309d6c965SJuan Quintela     }
108409d6c965SJuan Quintela #endif
108509d6c965SJuan Quintela 
108609d6c965SJuan Quintela     return true;
108709d6c965SJuan Quintela }
108809d6c965SJuan Quintela 
108909d6c965SJuan Quintela static void migrate_params_test_apply(MigrateSetParameters *params,
109009d6c965SJuan Quintela                                       MigrationParameters *dest)
109109d6c965SJuan Quintela {
109209d6c965SJuan Quintela     *dest = migrate_get_current()->parameters;
109309d6c965SJuan Quintela 
109409d6c965SJuan Quintela     /* TODO use QAPI_CLONE() instead of duplicating it inline */
109509d6c965SJuan Quintela 
109609d6c965SJuan Quintela     if (params->has_compress_level) {
109709d6c965SJuan Quintela         dest->compress_level = params->compress_level;
109809d6c965SJuan Quintela     }
109909d6c965SJuan Quintela 
110009d6c965SJuan Quintela     if (params->has_compress_threads) {
110109d6c965SJuan Quintela         dest->compress_threads = params->compress_threads;
110209d6c965SJuan Quintela     }
110309d6c965SJuan Quintela 
110409d6c965SJuan Quintela     if (params->has_compress_wait_thread) {
110509d6c965SJuan Quintela         dest->compress_wait_thread = params->compress_wait_thread;
110609d6c965SJuan Quintela     }
110709d6c965SJuan Quintela 
110809d6c965SJuan Quintela     if (params->has_decompress_threads) {
110909d6c965SJuan Quintela         dest->decompress_threads = params->decompress_threads;
111009d6c965SJuan Quintela     }
111109d6c965SJuan Quintela 
111209d6c965SJuan Quintela     if (params->has_throttle_trigger_threshold) {
111309d6c965SJuan Quintela         dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
111409d6c965SJuan Quintela     }
111509d6c965SJuan Quintela 
111609d6c965SJuan Quintela     if (params->has_cpu_throttle_initial) {
111709d6c965SJuan Quintela         dest->cpu_throttle_initial = params->cpu_throttle_initial;
111809d6c965SJuan Quintela     }
111909d6c965SJuan Quintela 
112009d6c965SJuan Quintela     if (params->has_cpu_throttle_increment) {
112109d6c965SJuan Quintela         dest->cpu_throttle_increment = params->cpu_throttle_increment;
112209d6c965SJuan Quintela     }
112309d6c965SJuan Quintela 
112409d6c965SJuan Quintela     if (params->has_cpu_throttle_tailslow) {
112509d6c965SJuan Quintela         dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
112609d6c965SJuan Quintela     }
112709d6c965SJuan Quintela 
112809d6c965SJuan Quintela     if (params->tls_creds) {
112909d6c965SJuan Quintela         assert(params->tls_creds->type == QTYPE_QSTRING);
113009d6c965SJuan Quintela         dest->tls_creds = params->tls_creds->u.s;
113109d6c965SJuan Quintela     }
113209d6c965SJuan Quintela 
113309d6c965SJuan Quintela     if (params->tls_hostname) {
113409d6c965SJuan Quintela         assert(params->tls_hostname->type == QTYPE_QSTRING);
113509d6c965SJuan Quintela         dest->tls_hostname = params->tls_hostname->u.s;
113609d6c965SJuan Quintela     }
113709d6c965SJuan Quintela 
113809d6c965SJuan Quintela     if (params->has_max_bandwidth) {
113909d6c965SJuan Quintela         dest->max_bandwidth = params->max_bandwidth;
114009d6c965SJuan Quintela     }
114109d6c965SJuan Quintela 
114209d6c965SJuan Quintela     if (params->has_downtime_limit) {
114309d6c965SJuan Quintela         dest->downtime_limit = params->downtime_limit;
114409d6c965SJuan Quintela     }
114509d6c965SJuan Quintela 
114609d6c965SJuan Quintela     if (params->has_x_checkpoint_delay) {
114709d6c965SJuan Quintela         dest->x_checkpoint_delay = params->x_checkpoint_delay;
114809d6c965SJuan Quintela     }
114909d6c965SJuan Quintela 
115009d6c965SJuan Quintela     if (params->has_block_incremental) {
115109d6c965SJuan Quintela         dest->block_incremental = params->block_incremental;
115209d6c965SJuan Quintela     }
115309d6c965SJuan Quintela     if (params->has_multifd_channels) {
115409d6c965SJuan Quintela         dest->multifd_channels = params->multifd_channels;
115509d6c965SJuan Quintela     }
115609d6c965SJuan Quintela     if (params->has_multifd_compression) {
115709d6c965SJuan Quintela         dest->multifd_compression = params->multifd_compression;
115809d6c965SJuan Quintela     }
115909d6c965SJuan Quintela     if (params->has_xbzrle_cache_size) {
116009d6c965SJuan Quintela         dest->xbzrle_cache_size = params->xbzrle_cache_size;
116109d6c965SJuan Quintela     }
116209d6c965SJuan Quintela     if (params->has_max_postcopy_bandwidth) {
116309d6c965SJuan Quintela         dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
116409d6c965SJuan Quintela     }
116509d6c965SJuan Quintela     if (params->has_max_cpu_throttle) {
116609d6c965SJuan Quintela         dest->max_cpu_throttle = params->max_cpu_throttle;
116709d6c965SJuan Quintela     }
116809d6c965SJuan Quintela     if (params->has_announce_initial) {
116909d6c965SJuan Quintela         dest->announce_initial = params->announce_initial;
117009d6c965SJuan Quintela     }
117109d6c965SJuan Quintela     if (params->has_announce_max) {
117209d6c965SJuan Quintela         dest->announce_max = params->announce_max;
117309d6c965SJuan Quintela     }
117409d6c965SJuan Quintela     if (params->has_announce_rounds) {
117509d6c965SJuan Quintela         dest->announce_rounds = params->announce_rounds;
117609d6c965SJuan Quintela     }
117709d6c965SJuan Quintela     if (params->has_announce_step) {
117809d6c965SJuan Quintela         dest->announce_step = params->announce_step;
117909d6c965SJuan Quintela     }
118009d6c965SJuan Quintela 
118109d6c965SJuan Quintela     if (params->has_block_bitmap_mapping) {
118209d6c965SJuan Quintela         dest->has_block_bitmap_mapping = true;
118309d6c965SJuan Quintela         dest->block_bitmap_mapping = params->block_bitmap_mapping;
118409d6c965SJuan Quintela     }
118509d6c965SJuan Quintela }
118609d6c965SJuan Quintela 
118709d6c965SJuan Quintela static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
118809d6c965SJuan Quintela {
118909d6c965SJuan Quintela     MigrationState *s = migrate_get_current();
119009d6c965SJuan Quintela 
119109d6c965SJuan Quintela     /* TODO use QAPI_CLONE() instead of duplicating it inline */
119209d6c965SJuan Quintela 
119309d6c965SJuan Quintela     if (params->has_compress_level) {
119409d6c965SJuan Quintela         s->parameters.compress_level = params->compress_level;
119509d6c965SJuan Quintela     }
119609d6c965SJuan Quintela 
119709d6c965SJuan Quintela     if (params->has_compress_threads) {
119809d6c965SJuan Quintela         s->parameters.compress_threads = params->compress_threads;
119909d6c965SJuan Quintela     }
120009d6c965SJuan Quintela 
120109d6c965SJuan Quintela     if (params->has_compress_wait_thread) {
120209d6c965SJuan Quintela         s->parameters.compress_wait_thread = params->compress_wait_thread;
120309d6c965SJuan Quintela     }
120409d6c965SJuan Quintela 
120509d6c965SJuan Quintela     if (params->has_decompress_threads) {
120609d6c965SJuan Quintela         s->parameters.decompress_threads = params->decompress_threads;
120709d6c965SJuan Quintela     }
120809d6c965SJuan Quintela 
120909d6c965SJuan Quintela     if (params->has_throttle_trigger_threshold) {
121009d6c965SJuan Quintela         s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
121109d6c965SJuan Quintela     }
121209d6c965SJuan Quintela 
121309d6c965SJuan Quintela     if (params->has_cpu_throttle_initial) {
121409d6c965SJuan Quintela         s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
121509d6c965SJuan Quintela     }
121609d6c965SJuan Quintela 
121709d6c965SJuan Quintela     if (params->has_cpu_throttle_increment) {
121809d6c965SJuan Quintela         s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
121909d6c965SJuan Quintela     }
122009d6c965SJuan Quintela 
122109d6c965SJuan Quintela     if (params->has_cpu_throttle_tailslow) {
122209d6c965SJuan Quintela         s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
122309d6c965SJuan Quintela     }
122409d6c965SJuan Quintela 
122509d6c965SJuan Quintela     if (params->tls_creds) {
122609d6c965SJuan Quintela         g_free(s->parameters.tls_creds);
122709d6c965SJuan Quintela         assert(params->tls_creds->type == QTYPE_QSTRING);
122809d6c965SJuan Quintela         s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
122909d6c965SJuan Quintela     }
123009d6c965SJuan Quintela 
123109d6c965SJuan Quintela     if (params->tls_hostname) {
123209d6c965SJuan Quintela         g_free(s->parameters.tls_hostname);
123309d6c965SJuan Quintela         assert(params->tls_hostname->type == QTYPE_QSTRING);
123409d6c965SJuan Quintela         s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
123509d6c965SJuan Quintela     }
123609d6c965SJuan Quintela 
123709d6c965SJuan Quintela     if (params->tls_authz) {
123809d6c965SJuan Quintela         g_free(s->parameters.tls_authz);
123909d6c965SJuan Quintela         assert(params->tls_authz->type == QTYPE_QSTRING);
124009d6c965SJuan Quintela         s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
124109d6c965SJuan Quintela     }
124209d6c965SJuan Quintela 
124309d6c965SJuan Quintela     if (params->has_max_bandwidth) {
124409d6c965SJuan Quintela         s->parameters.max_bandwidth = params->max_bandwidth;
124509d6c965SJuan Quintela         if (s->to_dst_file && !migration_in_postcopy()) {
1246*e1fde0e0SJuan Quintela             migration_rate_set(s->parameters.max_bandwidth);
124709d6c965SJuan Quintela         }
124809d6c965SJuan Quintela     }
124909d6c965SJuan Quintela 
125009d6c965SJuan Quintela     if (params->has_downtime_limit) {
125109d6c965SJuan Quintela         s->parameters.downtime_limit = params->downtime_limit;
125209d6c965SJuan Quintela     }
125309d6c965SJuan Quintela 
125409d6c965SJuan Quintela     if (params->has_x_checkpoint_delay) {
125509d6c965SJuan Quintela         s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
12564332ffcdSVladimir Sementsov-Ogievskiy         colo_checkpoint_delay_set();
125709d6c965SJuan Quintela     }
125809d6c965SJuan Quintela 
125909d6c965SJuan Quintela     if (params->has_block_incremental) {
126009d6c965SJuan Quintela         s->parameters.block_incremental = params->block_incremental;
126109d6c965SJuan Quintela     }
126209d6c965SJuan Quintela     if (params->has_multifd_channels) {
126309d6c965SJuan Quintela         s->parameters.multifd_channels = params->multifd_channels;
126409d6c965SJuan Quintela     }
126509d6c965SJuan Quintela     if (params->has_multifd_compression) {
126609d6c965SJuan Quintela         s->parameters.multifd_compression = params->multifd_compression;
126709d6c965SJuan Quintela     }
126809d6c965SJuan Quintela     if (params->has_xbzrle_cache_size) {
126909d6c965SJuan Quintela         s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
127009d6c965SJuan Quintela         xbzrle_cache_resize(params->xbzrle_cache_size, errp);
127109d6c965SJuan Quintela     }
127209d6c965SJuan Quintela     if (params->has_max_postcopy_bandwidth) {
127309d6c965SJuan Quintela         s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
127409d6c965SJuan Quintela         if (s->to_dst_file && migration_in_postcopy()) {
1275*e1fde0e0SJuan Quintela             migration_rate_set(s->parameters.max_postcopy_bandwidth);
127609d6c965SJuan Quintela         }
127709d6c965SJuan Quintela     }
127809d6c965SJuan Quintela     if (params->has_max_cpu_throttle) {
127909d6c965SJuan Quintela         s->parameters.max_cpu_throttle = params->max_cpu_throttle;
128009d6c965SJuan Quintela     }
128109d6c965SJuan Quintela     if (params->has_announce_initial) {
128209d6c965SJuan Quintela         s->parameters.announce_initial = params->announce_initial;
128309d6c965SJuan Quintela     }
128409d6c965SJuan Quintela     if (params->has_announce_max) {
128509d6c965SJuan Quintela         s->parameters.announce_max = params->announce_max;
128609d6c965SJuan Quintela     }
128709d6c965SJuan Quintela     if (params->has_announce_rounds) {
128809d6c965SJuan Quintela         s->parameters.announce_rounds = params->announce_rounds;
128909d6c965SJuan Quintela     }
129009d6c965SJuan Quintela     if (params->has_announce_step) {
129109d6c965SJuan Quintela         s->parameters.announce_step = params->announce_step;
129209d6c965SJuan Quintela     }
129309d6c965SJuan Quintela 
129409d6c965SJuan Quintela     if (params->has_block_bitmap_mapping) {
129509d6c965SJuan Quintela         qapi_free_BitmapMigrationNodeAliasList(
129609d6c965SJuan Quintela             s->parameters.block_bitmap_mapping);
129709d6c965SJuan Quintela 
129809d6c965SJuan Quintela         s->parameters.has_block_bitmap_mapping = true;
129909d6c965SJuan Quintela         s->parameters.block_bitmap_mapping =
130009d6c965SJuan Quintela             QAPI_CLONE(BitmapMigrationNodeAliasList,
130109d6c965SJuan Quintela                        params->block_bitmap_mapping);
130209d6c965SJuan Quintela     }
130309d6c965SJuan Quintela }
130409d6c965SJuan Quintela 
130509d6c965SJuan Quintela void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
130609d6c965SJuan Quintela {
130709d6c965SJuan Quintela     MigrationParameters tmp;
130809d6c965SJuan Quintela 
130909d6c965SJuan Quintela     /* TODO Rewrite "" to null instead */
131009d6c965SJuan Quintela     if (params->tls_creds
131109d6c965SJuan Quintela         && params->tls_creds->type == QTYPE_QNULL) {
131209d6c965SJuan Quintela         qobject_unref(params->tls_creds->u.n);
131309d6c965SJuan Quintela         params->tls_creds->type = QTYPE_QSTRING;
131409d6c965SJuan Quintela         params->tls_creds->u.s = strdup("");
131509d6c965SJuan Quintela     }
131609d6c965SJuan Quintela     /* TODO Rewrite "" to null instead */
131709d6c965SJuan Quintela     if (params->tls_hostname
131809d6c965SJuan Quintela         && params->tls_hostname->type == QTYPE_QNULL) {
131909d6c965SJuan Quintela         qobject_unref(params->tls_hostname->u.n);
132009d6c965SJuan Quintela         params->tls_hostname->type = QTYPE_QSTRING;
132109d6c965SJuan Quintela         params->tls_hostname->u.s = strdup("");
132209d6c965SJuan Quintela     }
132309d6c965SJuan Quintela 
132409d6c965SJuan Quintela     migrate_params_test_apply(params, &tmp);
132509d6c965SJuan Quintela 
132609d6c965SJuan Quintela     if (!migrate_params_check(&tmp, errp)) {
132709d6c965SJuan Quintela         /* Invalid parameter */
132809d6c965SJuan Quintela         return;
132909d6c965SJuan Quintela     }
133009d6c965SJuan Quintela 
133109d6c965SJuan Quintela     migrate_params_apply(params, errp);
133209d6c965SJuan Quintela }
1333