]> git.proxmox.com Git - mirror_qemu.git/blame - migration/migration.h
migration: Report the error returned when save_live_iterate fails
[mirror_qemu.git] / migration / migration.h
CommitLineData
5bb7910a
AL
1/*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14#ifndef QEMU_MIGRATION_H
15#define QEMU_MIGRATION_H
16
d4842052 17#include "exec/cpu-common.h"
a27bd6c7 18#include "hw/qdev-core.h"
9af23989 19#include "qapi/qapi-types-migration.h"
9848a404 20#include "qemu/thread.h"
25d0c16f 21#include "qemu/coroutine_int.h"
4f0fae7f 22#include "io/channel.h"
8518278a 23#include "io/channel-buffer.h"
7659505c 24#include "net/announce.h"
db1015e9 25#include "qom/object.h"
376253ec 26
2a4c42f1
AP
27struct PostcopyBlocktimeContext;
28
13955b89
PX
29#define MIGRATION_RESUME_ACK_VALUE (1)
30
002cad6b
PX
31/*
32 * 1<<6=64 pages -> 256K chunk when page size is 4K. This gives us
33 * the benefit that all the chunks are 64 pages aligned then the
34 * bitmaps are always aligned to LONG.
35 */
36#define CLEAR_BITMAP_SHIFT_MIN 6
37/*
38 * 1<<18=256K pages -> 1G chunk when page size is 4K. This is the
39 * default value to use if no one specified.
40 */
41#define CLEAR_BITMAP_SHIFT_DEFAULT 18
42/*
43 * 1<<31=2G pages -> 8T chunk when page size is 4K. This should be
44 * big enough and make sure we won't overflow easily.
45 */
46#define CLEAR_BITMAP_SHIFT_MAX 31
47
bca7856a
DDAG
48/* State for the incoming migration */
49struct MigrationIncomingState {
42e2aa56 50 QEMUFile *from_src_file;
1a8f46f8 51
1df6ddb4
DDAG
52 /* A hook to allow cleanup at the end of incoming migration */
53 void *transport_data;
54 void (*transport_cleanup)(void *data);
55
7b89bf27
DDAG
56 /*
57 * Free at the start of the main state load, set as the main thread finishes
58 * loading state.
59 */
60 QemuEvent main_thread_load_event;
61
7659505c
DDAG
62 /* For network announces */
63 AnnounceTimer announce_timer;
64
67f11b5c 65 size_t largest_page_size;
c4faeed2 66 bool have_fault_thread;
f0a227ad
DDAG
67 QemuThread fault_thread;
68 QemuSemaphore fault_thread_sem;
64f615fe
PX
69 /* Set this when we want the fault thread to quit */
70 bool fault_thread_quit;
f0a227ad 71
c76201ab
DDAG
72 bool have_listen_thread;
73 QemuThread listen_thread;
74 QemuSemaphore listen_thread_sem;
75
1caddf8a
DDAG
76 /* For the kernel to send us notifications */
77 int userfault_fd;
64f615fe
PX
78 /* To notify the fault_thread to wake, e.g., when need to quit */
79 int userfault_event_fd;
2e37701e 80 QEMUFile *to_src_file;
6decec93 81 QemuMutex rp_mutex; /* We send replies from multiple threads */
096bf4c8
DDAG
82 /* RAMBlock of last request sent to source */
83 RAMBlock *last_rb;
696ed9a9 84 void *postcopy_tmp_page;
41d84210 85 void *postcopy_tmp_zero_page;
00fa4fc8
DDAG
86 /* PostCopyFD's for external userfaultfds & handlers of shared memory */
87 GArray *postcopy_remote_fds;
2e37701e 88
0aa6aefc
DL
89 QEMUBH *bh;
90
93d7af6f 91 int state;
25d0c16f
HZ
92
93 bool have_colo_incoming_thread;
94 QemuThread colo_incoming_thread;
95 /* The coroutine we should enter (back) after failover */
96 Coroutine *migration_incoming_co;
c937b9a6 97 QemuSemaphore colo_incoming_sem;
2a4c42f1
AP
98
99 /*
100 * PostcopyBlocktimeContext to keep information for postcopy
101 * live migration, to calculate vCPU block time
102 * */
103 struct PostcopyBlocktimeContext *blocktime_ctx;
b411b844
PX
104
105 /* notify PAUSED postcopy incoming migrations to try to continue */
02affd41 106 bool postcopy_recover_triggered;
b411b844 107 QemuSemaphore postcopy_pause_sem_dst;
3a7804c3 108 QemuSemaphore postcopy_pause_sem_fault;
9aca82ba
JQ
109
110 /* List of listening socket addresses */
111 SocketAddressList *socket_address_list;
8f8bfffc
PX
112
113 /* A tree of pages that we requested to the source VM */
114 GTree *page_requested;
115 /* For debugging purpose only, but would be nice to keep */
116 int page_requested_count;
117 /*
118 * The mutex helps to maintain the requested pages that we sent to the
119 * source, IOW, to guarantee coherent between the page_requests tree and
120 * the per-ramblock receivedmap. Note! This does not guarantee consistency
121 * of the real page copy procedures (using UFFDIO_[ZERO]COPY). E.g., even
122 * if one bit in receivedmap is cleared, UFFDIO_COPY could have happened
123 * for that page already. This is intended so that the mutex won't
124 * serialize and blocked by slow operations like UFFDIO_* ioctls. However
125 * this should be enough to make sure the page_requested tree always
126 * contains valid information.
127 */
128 QemuMutex page_request_mutex;
bca7856a
DDAG
129};
130
131MigrationIncomingState *migration_incoming_get_current(void);
bca7856a 132void migration_incoming_state_destroy(void);
65ace060
AP
133/*
134 * Functions to work with blocktime context
135 */
136void fill_destination_postcopy_migration_info(MigrationInfo *info);
bca7856a 137
e5cb7e76
PX
138#define TYPE_MIGRATION "migration"
139
db1015e9 140typedef struct MigrationClass MigrationClass;
8110fa1d
EH
141DECLARE_OBJ_CHECKERS(MigrationState, MigrationClass,
142 MIGRATION_OBJ, TYPE_MIGRATION)
e5cb7e76 143
db1015e9 144struct MigrationClass {
e5cb7e76
PX
145 /*< private >*/
146 DeviceClass parent_class;
db1015e9 147};
e5cb7e76 148
f16aee44 149struct MigrationState {
e5cb7e76
PX
150 /*< private >*/
151 DeviceState parent_obj;
152
153 /*< public >*/
9848a404 154 QemuThread thread;
8518278a 155 QEMUBH *vm_start_bh;
bb1fadc4 156 QEMUBH *cleanup_bh;
43044ac0 157 /* Protected by qemu_file_lock */
89a02a9f 158 QEMUFile *to_dst_file;
8518278a 159 QIOChannelBuffer *bioc;
62df066f 160 /*
43044ac0
PX
161 * Protects to_dst_file/from_dst_file pointers. We need to make sure we
162 * won't yield or hang during the critical section, since this lock will be
163 * used in OOB command handler.
62df066f
PX
164 */
165 QemuMutex qemu_file_lock;
2594f56d 166
ad767bed
DDAG
167 /*
168 * Used to allow urgent requests to override rate limiting.
169 */
170 QemuSemaphore rate_limit_sem;
171
aecbfe9c
XG
172 /* pages already send at the beginning of current iteration */
173 uint64_t iteration_initial_pages;
174
175 /* pages transferred per second */
176 double pages_per_second;
177
178 /* bytes already send at the beginning of current iteration */
b15df1ae
PX
179 uint64_t iteration_initial_bytes;
180 /* time at the start of current iteration */
181 int64_t iteration_start_time;
182 /*
183 * The final stage happens when the remaining data is smaller than
184 * this threshold; it's calculated from the requested downtime and
185 * measured bandwidth
186 */
187 int64_t threshold_size;
188
a0762d9e 189 /* params from 'migrate-set-parameters' */
2594f56d 190 MigrationParameters parameters;
f8bbc128 191
f8bbc128 192 int state;
70b20477
DDAG
193
194 /* State related to return path */
195 struct {
43044ac0 196 /* Protected by qemu_file_lock */
70b20477
DDAG
197 QEMUFile *from_dst_file;
198 QemuThread rp_thread;
199 bool error;
53021ea1
PX
200 /*
201 * We can also check non-zero of rp_thread, but there's no "official"
202 * way to do this, so this bool makes it slightly more elegant.
203 * Checking from_dst_file for this is racy because from_dst_file will
204 * be cleared in the rp_thread!
205 */
206 bool rp_thread_created;
edd090c7 207 QemuSemaphore rp_sem;
70b20477
DDAG
208 } rp_state;
209
7e114f8c 210 double mbps;
4af246a3
PX
211 /* Timestamp when recent migration starts (ms) */
212 int64_t start_time;
213 /* Total time used by latest migration (ms) */
d5f8a570 214 int64_t total_time;
64909f97
PX
215 /* Timestamp when VM is down (ms) to migrate the last stuff */
216 int64_t downtime_start;
9c5a9fcf 217 int64_t downtime;
2c52ddf1 218 int64_t expected_downtime;
7fb1cf16 219 bool enabled_capabilities[MIGRATION_CAPABILITY__MAX];
ed4fbd10 220 int64_t setup_time;
7287cbd4
PX
221 /*
222 * Whether guest was running when we enter the completion stage.
223 * If migration is interrupted by any reason, we need to continue
224 * running the guest on source.
225 */
226 bool vm_was_running;
4886a1bc
DDAG
227
228 /* Flag set once the migration has been asked to enter postcopy */
229 bool start_postcopy;
b82fc321
DDAG
230 /* Flag set after postcopy has sent the device state */
231 bool postcopy_after_devices;
1d34e4bf
DDAG
232
233 /* Flag set once the migration thread is running (and needs joining) */
234 bool migration_thread_running;
6c595cde 235
1d2acc31
HZ
236 /* Flag set once the migration thread called bdrv_inactivate_all */
237 bool block_inactive;
238
c7e0acd5
JF
239 /* Migration is waiting for guest to unplug device */
240 QemuSemaphore wait_unplug_sem;
241
e91d8951
DDAG
242 /* Migration is paused due to pause-before-switchover */
243 QemuSemaphore pause_sem;
244
c937b9a6
HZ
245 /* The semaphore is used to notify COLO thread that failover is finished */
246 QemuSemaphore colo_exit_sem;
d59ce6f3 247
bb70b66e
LS
248 /* The event is used to notify COLO thread to do checkpoint */
249 QemuEvent colo_checkpoint_event;
479125d5
HZ
250 int64_t colo_checkpoint_time;
251 QEMUTimer *colo_delay_timer;
252
87db1a7d
JQ
253 /* The first error that has occurred.
254 We used the mutex to be able to return the 1st error message */
d59ce6f3 255 Error *error;
87db1a7d
JQ
256 /* mutex to protect errp */
257 QemuMutex error_mutex;
258
2833c59b
JQ
259 /* Do we have to clean up -b/-i from old migrate parameters */
260 /* This feature is deprecated and will be removed */
261 bool must_remove_block_options;
5272298c
PX
262
263 /*
264 * Global switch on whether we need to store the global state
265 * during migration.
266 */
267 bool store_global_state;
3df663e5 268
71dd4c1a
PX
269 /* Whether we send QEMU_VM_CONFIGURATION during migration */
270 bool send_configuration;
15c38503
PX
271 /* Whether we send section footer during migration */
272 bool send_section_footer;
b23c2ade
PX
273
274 /* Needed by postcopy-pause state */
275 QemuSemaphore postcopy_pause_sem;
14b1742e 276 QemuSemaphore postcopy_pause_rp_sem;
f548222c
XG
277 /*
278 * Whether we abort the migration if decompression errors are
279 * detected at the destination. It is left at false for qemu
280 * older than 3.0, since only newer qemu sends streams that
281 * do not trigger spurious decompression errors.
282 */
283 bool decompress_error_check;
002cad6b
PX
284
285 /*
286 * This decides the size of guest memory chunk that will be used
287 * to track dirty bitmap clearing. The size of memory chunk will
288 * be GUEST_PAGE_SIZE << N. Say, N=0 means we will clear dirty
289 * bitmap for each page to send (1<<0=1); N=10 means we will clear
290 * dirty bitmap only once for 1<<10=1K continuous guest pages
291 * (which is in 4M chunk).
292 */
293 uint8_t clear_bitmap_shift;
d8053e73
CZ
294
295 /*
296 * This save hostname when out-going migration starts
297 */
298 char *hostname;
065e2813
AL
299};
300
48781e5b
HZ
301void migrate_set_state(int *state, int old_state, int new_state);
302
b673eab4 303void migration_fd_process_incoming(QEMUFile *f, Error **errp);
49ed0d24 304void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp);
36c2f8be 305void migration_incoming_process(void);
511c0231 306
428d8908
JQ
307bool migration_has_all_channels(void);
308
a0a3fd60
GC
309uint64_t migrate_max_downtime(void);
310
87db1a7d 311void migrate_set_error(MigrationState *s, const Error *error);
d59ce6f3 312void migrate_fd_error(MigrationState *s, const Error *error);
065e2813 313
cce8040b 314void migrate_fd_connect(MigrationState *s, Error *error_in);
065e2813 315
3d63da16 316bool migration_is_setup_or_active(int state);
392d87e2 317bool migration_is_running(int state);
3d63da16 318
3e0c8050 319void migrate_init(MigrationState *s);
24f3902b 320bool migration_is_blocked(Error **errp);
9ec055ae 321/* True if outgoing migration has entered postcopy phase */
5727309d 322bool migration_in_postcopy(void);
859bc756 323MigrationState *migrate_get_current(void);
99a0db9b 324
58110f0a
VSO
325bool migrate_postcopy(void);
326
53f09a10 327bool migrate_release_ram(void);
53dd370c 328bool migrate_postcopy_ram(void);
323004a3 329bool migrate_zero_blocks(void);
55efc8c2 330bool migrate_dirty_bitmaps(void);
18269069 331bool migrate_ignore_shared(void);
b9d68df6 332bool migrate_validate_uuid(void);
60d9222c 333
bde1e2ec 334bool migrate_auto_converge(void);
30126bbf 335bool migrate_use_multifd(void);
93fbd031 336bool migrate_pause_before_switchover(void);
4075fb1c 337int migrate_multifd_channels(void);
ab7cbb0b 338MultiFDCompression migrate_multifd_compression(void);
9004db48 339int migrate_multifd_zlib_level(void);
6a9ad154 340int migrate_multifd_zstd_level(void);
bde1e2ec 341
17ad9b35 342int migrate_use_xbzrle(void);
8b9407a0 343uint64_t migrate_xbzrle_cache_size(void);
35a6ed4f 344bool migrate_colo_enabled(void);
17ad9b35 345
2833c59b
JQ
346bool migrate_use_block(void);
347bool migrate_use_block_incremental(void);
4cbc9c7f 348int migrate_max_cpu_throttle(void);
c788ada8 349bool migrate_use_return_path(void);
2833c59b 350
aecbfe9c
XG
351uint64_t ram_get_total_transferred_pages(void);
352
8706d2d5
LL
353bool migrate_use_compression(void);
354int migrate_compress_level(void);
355int migrate_compress_threads(void);
1d58872a 356int migrate_compress_wait_thread(void);
3fcb38c2 357int migrate_decompress_threads(void);
b05dc723 358bool migrate_use_events(void);
f22f928e 359bool migrate_postcopy_blocktime(void);
6e8c25b4 360bool migrate_background_snapshot(void);
8706d2d5 361
6decec93 362/* Sending on the return path - generic and then for each message type */
6decec93
DDAG
363void migrate_send_rp_shut(MigrationIncomingState *mis,
364 uint32_t value);
365void migrate_send_rp_pong(MigrationIncomingState *mis,
366 uint32_t value);
2e2bce16 367int migrate_send_rp_req_pages(MigrationIncomingState *mis, RAMBlock *rb,
8f8bfffc 368 ram_addr_t start, uint64_t haddr);
7a267fc4
PX
369int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
370 RAMBlock *rb, ram_addr_t start);
a335debb
PX
371void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
372 char *block_name);
13955b89 373void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);
6decec93 374
b35ebdf0 375void dirty_bitmap_mig_before_vm_start(void);
1499ab09
VSO
376void dirty_bitmap_mig_cancel_outgoing(void);
377void dirty_bitmap_mig_cancel_incoming(void);
31e4c354
HR
378bool check_dirty_bitmap_mig_alias_map(const BitmapMigrationNodeAliasList *bbm,
379 Error **errp);
380
9aca82ba 381void migrate_add_address(SocketAddress *address);
b35ebdf0 382
fbd162e6
YK
383int foreach_not_ignored_block(RAMBlockIterFunc func, void *opaque);
384
343f632c 385#define qemu_ram_foreach_block \
fbd162e6 386 #warning "Use foreach_not_ignored_block in migration code"
343f632c 387
ad767bed
DDAG
388void migration_make_urgent_request(void);
389void migration_consume_urgent_request(void);
97e1e067 390bool migration_rate_limit(void);
458fecca 391void migration_cancel(const Error *error);
ad767bed 392
43bd0bf3
TH
393void populate_vfio_info(MigrationInfo *info);
394
5bb7910a 395#endif