]>
Commit | Line | Data |
---|---|---|
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 | * | |
6b620ca3 PB |
12 | * Contributions after 2012-01-13 are licensed under the terms of the |
13 | * GNU GPL, version 2 or (at your option) any later version. | |
5bb7910a AL |
14 | */ |
15 | ||
1393a485 | 16 | #include "qemu/osdep.h" |
f348b6d1 | 17 | #include "qemu/cutils.h" |
d49b6836 | 18 | #include "qemu/error-report.h" |
db725815 | 19 | #include "qemu/main-loop.h" |
795c40b8 | 20 | #include "migration/blocker.h" |
f4dbe1bf | 21 | #include "exec.h" |
7fcac4a2 | 22 | #include "fd.h" |
61e8b148 | 23 | #include "socket.h" |
54d31236 | 24 | #include "sysemu/runstate.h" |
46517dd4 | 25 | #include "sysemu/sysemu.h" |
b0c3cf94 | 26 | #include "sysemu/cpu-throttle.h" |
e1a3ecee | 27 | #include "rdma.h" |
7b1e1a22 | 28 | #include "ram.h" |
84a899de | 29 | #include "migration/global_state.h" |
c4b63b7c | 30 | #include "migration/misc.h" |
6666c96a | 31 | #include "migration.h" |
20a519a0 | 32 | #include "savevm.h" |
08a0aee1 | 33 | #include "qemu-file.h" |
6720c2b3 | 34 | #include "channel.h" |
987772d9 | 35 | #include "migration/vmstate.h" |
737e150e | 36 | #include "block/block.h" |
e688df6b | 37 | #include "qapi/error.h" |
9aca82ba | 38 | #include "qapi/clone-visitor.h" |
31e4c354 | 39 | #include "qapi/qapi-visit-migration.h" |
9aca82ba | 40 | #include "qapi/qapi-visit-sockets.h" |
9af23989 MA |
41 | #include "qapi/qapi-commands-migration.h" |
42 | #include "qapi/qapi-events-migration.h" | |
cc7a8ea7 | 43 | #include "qapi/qmp/qerror.h" |
15280c36 | 44 | #include "qapi/qmp/qnull.h" |
ab28bd23 | 45 | #include "qemu/rcu.h" |
2c9e6fec | 46 | #include "block.h" |
be07b0ac | 47 | #include "postcopy-ram.h" |
766bd176 | 48 | #include "qemu/thread.h" |
c09e5bb1 | 49 | #include "trace.h" |
51180423 | 50 | #include "exec/target_page.h" |
61b67d47 | 51 | #include "io/channel-buffer.h" |
85a8578e | 52 | #include "io/channel-tls.h" |
35a6ed4f | 53 | #include "migration/colo.h" |
4ffdb337 | 54 | #include "hw/boards.h" |
a27bd6c7 | 55 | #include "hw/qdev-properties.h" |
ce35e229 | 56 | #include "hw/qdev-properties-system.h" |
9d18af93 | 57 | #include "monitor/monitor.h" |
50510ea2 | 58 | #include "net/announce.h" |
c7e0acd5 | 59 | #include "qemu/queue.h" |
d32ca5ad | 60 | #include "multifd.h" |
1b1f4ab6 | 61 | #include "threadinfo.h" |
b5eea99e | 62 | #include "qemu/yank.h" |
6e8c25b4 | 63 | #include "sysemu/cpus.h" |
39675fff | 64 | #include "yank_functions.h" |
1b529d90 | 65 | #include "sysemu/qtest.h" |
1f0776f1 | 66 | #include "options.h" |
065e2813 | 67 | |
7590a2ae | 68 | #define MAX_THROTTLE (128 << 20) /* Migration transfer speed throttling */ |
5bb7910a | 69 | |
2ff30257 AA |
70 | /* Time in milliseconds we are allowed to stop the source, |
71 | * for sending the last part */ | |
72 | #define DEFAULT_MIGRATE_SET_DOWNTIME 300 | |
73 | ||
8706d2d5 LL |
74 | /* Default compression thread count */ |
75 | #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8 | |
3fcb38c2 LL |
76 | /* Default decompression thread count, usually decompression is at |
77 | * least 4 times as fast as compression.*/ | |
78 | #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2 | |
8706d2d5 LL |
79 | /*0: means nocompress, 1: best speed, ... 9: best compress ratio */ |
80 | #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1 | |
1626fee3 | 81 | /* Define default autoconverge cpu throttle migration parameters */ |
dc14a470 | 82 | #define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50 |
d85a31d1 JH |
83 | #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20 |
84 | #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10 | |
4cbc9c7f | 85 | #define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99 |
8706d2d5 | 86 | |
17ad9b35 | 87 | /* Migration XBZRLE default cache size */ |
73af8dd8 | 88 | #define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024) |
17ad9b35 | 89 | |
131b2153 ZC |
90 | /* The delay time (in ms) between two COLO checkpoints */ |
91 | #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100) | |
4075fb1c | 92 | #define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2 |
96eef042 | 93 | #define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE |
9004db48 JQ |
94 | /* 0: means nocompress, 1: best speed, ... 9: best compress ratio */ |
95 | #define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1 | |
6a9ad154 JQ |
96 | /* 0: means nocompress, 1: best speed, ... 20: best compress ratio */ |
97 | #define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1 | |
68b53591 | 98 | |
7e555c6c DDAG |
99 | /* Background transfer rate for postcopy, 0 means unlimited, note |
100 | * that page requests can still exceed this limit. | |
101 | */ | |
102 | #define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0 | |
103 | ||
ee3d96ba DDAG |
104 | /* |
105 | * Parameters for self_announce_delay giving a stream of RARP/ARP | |
106 | * packets after migration. | |
107 | */ | |
108 | #define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50 | |
109 | #define DEFAULT_MIGRATE_ANNOUNCE_MAX 550 | |
110 | #define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5 | |
111 | #define DEFAULT_MIGRATE_ANNOUNCE_STEP 100 | |
112 | ||
99a0db9b GH |
113 | static NotifierList migration_state_notifiers = |
114 | NOTIFIER_LIST_INITIALIZER(migration_state_notifiers); | |
115 | ||
da6f1790 JQ |
116 | /* Messages sent on the return path from destination to source */ |
117 | enum mig_rp_message_type { | |
118 | MIG_RP_MSG_INVALID = 0, /* Must be 0 */ | |
119 | MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */ | |
120 | MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */ | |
121 | ||
122 | MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */ | |
123 | MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */ | |
a335debb | 124 | MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */ |
13955b89 | 125 | MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */ |
da6f1790 JQ |
126 | |
127 | MIG_RP_MSG_MAX | |
128 | }; | |
129 | ||
17549e84 JQ |
130 | /* When we add fault tolerance, we could have several |
131 | migrations at once. For now we don't need to add | |
132 | dynamic creation of migration */ | |
133 | ||
e5cb7e76 | 134 | static MigrationState *current_migration; |
e1b1b1bc | 135 | static MigrationIncomingState *current_incoming; |
e5cb7e76 | 136 | |
3af8554b DDAG |
137 | static GSList *migration_blockers; |
138 | ||
8b0b29dc | 139 | static bool migration_object_check(MigrationState *ms, Error **errp); |
0331c8ca DDAG |
140 | static int migration_maybe_pause(MigrationState *s, |
141 | int *current_active_state, | |
142 | int new_state); | |
892ae715 | 143 | static void migrate_fd_cancel(MigrationState *s); |
8b0b29dc | 144 | |
d6f74fd1 PX |
145 | static bool migration_needs_multiple_sockets(void) |
146 | { | |
51b07548 | 147 | return migrate_multifd() || migrate_postcopy_preempt(); |
d6f74fd1 | 148 | } |
f444eeda | 149 | |
d6f74fd1 | 150 | static bool uri_supports_multi_channels(const char *uri) |
f444eeda | 151 | { |
d6f74fd1 PX |
152 | return strstart(uri, "tcp:", NULL) || strstart(uri, "unix:", NULL) || |
153 | strstart(uri, "vsock:", NULL); | |
f444eeda PX |
154 | } |
155 | ||
d6f74fd1 PX |
156 | static bool |
157 | migration_channels_and_uri_compatible(const char *uri, Error **errp) | |
f444eeda | 158 | { |
d6f74fd1 PX |
159 | if (migration_needs_multiple_sockets() && |
160 | !uri_supports_multi_channels(uri)) { | |
161 | error_setg(errp, "Migration requires multi-channel URIs (e.g. tcp)"); | |
162 | return false; | |
163 | } | |
164 | ||
165 | return true; | |
f444eeda PX |
166 | } |
167 | ||
8f8bfffc PX |
168 | static gint page_request_addr_cmp(gconstpointer ap, gconstpointer bp) |
169 | { | |
170 | uintptr_t a = (uintptr_t) ap, b = (uintptr_t) bp; | |
171 | ||
172 | return (a > b) - (a < b); | |
173 | } | |
174 | ||
e5cb7e76 PX |
175 | void migration_object_init(void) |
176 | { | |
177 | /* This can only be called once. */ | |
178 | assert(!current_migration); | |
179 | current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION)); | |
4ffdb337 | 180 | |
e1b1b1bc PX |
181 | /* |
182 | * Init the migrate incoming object as well no matter whether | |
183 | * we'll use it or not. | |
184 | */ | |
185 | assert(!current_incoming); | |
186 | current_incoming = g_new0(MigrationIncomingState, 1); | |
187 | current_incoming->state = MIGRATION_STATUS_NONE; | |
188 | current_incoming->postcopy_remote_fds = | |
189 | g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD)); | |
190 | qemu_mutex_init(¤t_incoming->rp_mutex); | |
60bb3c58 | 191 | qemu_mutex_init(¤t_incoming->postcopy_prio_thread_mutex); |
e1b1b1bc PX |
192 | qemu_event_init(¤t_incoming->main_thread_load_event, false); |
193 | qemu_sem_init(¤t_incoming->postcopy_pause_sem_dst, 0); | |
194 | qemu_sem_init(¤t_incoming->postcopy_pause_sem_fault, 0); | |
60bb3c58 | 195 | qemu_sem_init(¤t_incoming->postcopy_pause_sem_fast_load, 0); |
5655aab0 PX |
196 | qemu_sem_init(¤t_incoming->postcopy_qemufile_dst_done, 0); |
197 | ||
8f8bfffc PX |
198 | qemu_mutex_init(¤t_incoming->page_request_mutex); |
199 | current_incoming->page_requested = g_tree_new(page_request_addr_cmp); | |
e1b1b1bc | 200 | |
f9734d5d | 201 | migration_object_check(current_migration, &error_fatal); |
e0d17dfd PB |
202 | |
203 | blk_mig_init(); | |
204 | ram_mig_init(); | |
205 | dirty_bitmap_mig_init(); | |
e5cb7e76 PX |
206 | } |
207 | ||
458fecca | 208 | void migration_cancel(const Error *error) |
c7c0e724 | 209 | { |
458fecca LV |
210 | if (error) { |
211 | migrate_set_error(current_migration, error); | |
212 | } | |
c7c0e724 DH |
213 | migrate_fd_cancel(current_migration); |
214 | } | |
215 | ||
892ae715 | 216 | void migration_shutdown(void) |
1f895604 | 217 | { |
795969ab RL |
218 | /* |
219 | * When the QEMU main thread exit, the COLO thread | |
220 | * may wait a semaphore. So, we should wakeup the | |
221 | * COLO thread before migration shutdown. | |
222 | */ | |
223 | colo_shutdown(); | |
892ae715 DDAG |
224 | /* |
225 | * Cancel the current migration - that will (eventually) | |
226 | * stop the migration using this structure | |
227 | */ | |
458fecca | 228 | migration_cancel(NULL); |
1f895604 | 229 | object_unref(OBJECT(current_migration)); |
1499ab09 VSO |
230 | |
231 | /* | |
232 | * Cancel outgoing migration of dirty bitmaps. It should | |
233 | * at least unref used block nodes. | |
234 | */ | |
235 | dirty_bitmap_mig_cancel_outgoing(); | |
236 | ||
237 | /* | |
238 | * Cancel incoming migration of dirty bitmaps. Dirty bitmaps | |
239 | * are non-critical data, and their loss never considered as | |
240 | * something serious. | |
241 | */ | |
242 | dirty_bitmap_mig_cancel_incoming(); | |
1f895604 VSO |
243 | } |
244 | ||
bca7856a | 245 | /* For outgoing */ |
859bc756 | 246 | MigrationState *migrate_get_current(void) |
17549e84 | 247 | { |
e5cb7e76 PX |
248 | /* This can only be called after the object created. */ |
249 | assert(current_migration); | |
250 | return current_migration; | |
17549e84 JQ |
251 | } |
252 | ||
bca7856a DDAG |
253 | MigrationIncomingState *migration_incoming_get_current(void) |
254 | { | |
e1b1b1bc PX |
255 | assert(current_incoming); |
256 | return current_incoming; | |
bca7856a DDAG |
257 | } |
258 | ||
e031149c PX |
259 | void migration_incoming_transport_cleanup(MigrationIncomingState *mis) |
260 | { | |
261 | if (mis->socket_address_list) { | |
262 | qapi_free_SocketAddressList(mis->socket_address_list); | |
263 | mis->socket_address_list = NULL; | |
264 | } | |
265 | ||
266 | if (mis->transport_cleanup) { | |
267 | mis->transport_cleanup(mis->transport_data); | |
268 | mis->transport_data = mis->transport_cleanup = NULL; | |
269 | } | |
270 | } | |
271 | ||
bca7856a DDAG |
272 | void migration_incoming_state_destroy(void) |
273 | { | |
b4b076da JQ |
274 | struct MigrationIncomingState *mis = migration_incoming_get_current(); |
275 | ||
cfc3bcf3 LB |
276 | multifd_load_cleanup(); |
277 | ||
3482655b | 278 | if (mis->to_src_file) { |
660819b1 PX |
279 | /* Tell source that we are done */ |
280 | migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0); | |
3482655b PX |
281 | qemu_fclose(mis->to_src_file); |
282 | mis->to_src_file = NULL; | |
283 | } | |
284 | ||
660819b1 | 285 | if (mis->from_src_file) { |
39675fff | 286 | migration_ioc_unregister_yank_from_file(mis->from_src_file); |
660819b1 PX |
287 | qemu_fclose(mis->from_src_file); |
288 | mis->from_src_file = NULL; | |
289 | } | |
00fa4fc8 DDAG |
290 | if (mis->postcopy_remote_fds) { |
291 | g_array_free(mis->postcopy_remote_fds, TRUE); | |
292 | mis->postcopy_remote_fds = NULL; | |
293 | } | |
660819b1 | 294 | |
e031149c | 295 | migration_incoming_transport_cleanup(mis); |
1783c00f DDAG |
296 | qemu_event_reset(&mis->main_thread_load_event); |
297 | ||
8f8bfffc PX |
298 | if (mis->page_requested) { |
299 | g_tree_destroy(mis->page_requested); | |
300 | mis->page_requested = NULL; | |
301 | } | |
302 | ||
36f62f11 PX |
303 | if (mis->postcopy_qemufile_dst) { |
304 | migration_ioc_unregister_yank_from_file(mis->postcopy_qemufile_dst); | |
305 | qemu_fclose(mis->postcopy_qemufile_dst); | |
306 | mis->postcopy_qemufile_dst = NULL; | |
307 | } | |
308 | ||
b5eea99e | 309 | yank_unregister_instance(MIGRATION_YANK_INSTANCE); |
bca7856a DDAG |
310 | } |
311 | ||
b05dc723 JQ |
312 | static void migrate_generate_event(int new_state) |
313 | { | |
b890902c | 314 | if (migrate_events()) { |
3ab72385 | 315 | qapi_event_send_migration(new_state); |
b05dc723 JQ |
316 | } |
317 | } | |
318 | ||
da6f1790 JQ |
319 | /* |
320 | * Send a message on the return channel back to the source | |
321 | * of the migration. | |
322 | */ | |
d6208e35 PX |
323 | static int migrate_send_rp_message(MigrationIncomingState *mis, |
324 | enum mig_rp_message_type message_type, | |
325 | uint16_t len, void *data) | |
da6f1790 | 326 | { |
d6208e35 PX |
327 | int ret = 0; |
328 | ||
da6f1790 | 329 | trace_migrate_send_rp_message((int)message_type, len); |
37396950 | 330 | QEMU_LOCK_GUARD(&mis->rp_mutex); |
d6208e35 PX |
331 | |
332 | /* | |
333 | * It's possible that the file handle got lost due to network | |
334 | * failures. | |
335 | */ | |
336 | if (!mis->to_src_file) { | |
337 | ret = -EIO; | |
37396950 | 338 | return ret; |
d6208e35 PX |
339 | } |
340 | ||
da6f1790 JQ |
341 | qemu_put_be16(mis->to_src_file, (unsigned int)message_type); |
342 | qemu_put_be16(mis->to_src_file, len); | |
343 | qemu_put_buffer(mis->to_src_file, data, len); | |
344 | qemu_fflush(mis->to_src_file); | |
d6208e35 PX |
345 | |
346 | /* It's possible that qemu file got error during sending */ | |
347 | ret = qemu_file_get_error(mis->to_src_file); | |
348 | ||
d6208e35 | 349 | return ret; |
da6f1790 JQ |
350 | } |
351 | ||
2e2bce16 PX |
352 | /* Request one page from the source VM at the given start address. |
353 | * rb: the RAMBlock to request the page in | |
1e2d90eb DDAG |
354 | * Start: Address offset within the RB |
355 | * Len: Length in bytes required - must be a multiple of pagesize | |
356 | */ | |
7a267fc4 PX |
357 | int migrate_send_rp_message_req_pages(MigrationIncomingState *mis, |
358 | RAMBlock *rb, ram_addr_t start) | |
1e2d90eb | 359 | { |
cb8d4c8f | 360 | uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */ |
1e2d90eb | 361 | size_t msglen = 12; /* start + len */ |
2e2bce16 | 362 | size_t len = qemu_ram_pagesize(rb); |
d6208e35 | 363 | enum mig_rp_message_type msg_type; |
2e2bce16 PX |
364 | const char *rbname; |
365 | int rbname_len; | |
1e2d90eb DDAG |
366 | |
367 | *(uint64_t *)bufc = cpu_to_be64((uint64_t)start); | |
368 | *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len); | |
369 | ||
2e2bce16 PX |
370 | /* |
371 | * We maintain the last ramblock that we requested for page. Note that we | |
372 | * don't need locking because this function will only be called within the | |
373 | * postcopy ram fault thread. | |
374 | */ | |
375 | if (rb != mis->last_rb) { | |
376 | mis->last_rb = rb; | |
377 | ||
378 | rbname = qemu_ram_get_idstr(rb); | |
379 | rbname_len = strlen(rbname); | |
380 | ||
1e2d90eb DDAG |
381 | assert(rbname_len < 256); |
382 | ||
383 | bufc[msglen++] = rbname_len; | |
384 | memcpy(bufc + msglen, rbname, rbname_len); | |
385 | msglen += rbname_len; | |
d6208e35 | 386 | msg_type = MIG_RP_MSG_REQ_PAGES_ID; |
1e2d90eb | 387 | } else { |
d6208e35 | 388 | msg_type = MIG_RP_MSG_REQ_PAGES; |
1e2d90eb | 389 | } |
d6208e35 PX |
390 | |
391 | return migrate_send_rp_message(mis, msg_type, msglen, bufc); | |
1e2d90eb DDAG |
392 | } |
393 | ||
7a267fc4 | 394 | int migrate_send_rp_req_pages(MigrationIncomingState *mis, |
8f8bfffc | 395 | RAMBlock *rb, ram_addr_t start, uint64_t haddr) |
7a267fc4 | 396 | { |
7648297d | 397 | void *aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, qemu_ram_pagesize(rb)); |
a2429283 | 398 | bool received = false; |
8f8bfffc PX |
399 | |
400 | WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) { | |
401 | received = ramblock_recv_bitmap_test_byte_offset(rb, start); | |
402 | if (!received && !g_tree_lookup(mis->page_requested, aligned)) { | |
403 | /* | |
404 | * The page has not been received, and it's not yet in the page | |
405 | * request list. Queue it. Set the value of element to 1, so that | |
406 | * things like g_tree_lookup() will return TRUE (1) when found. | |
407 | */ | |
408 | g_tree_insert(mis->page_requested, aligned, (gpointer)1); | |
409 | mis->page_requested_count++; | |
410 | trace_postcopy_page_req_add(aligned, mis->page_requested_count); | |
411 | } | |
412 | } | |
413 | ||
414 | /* | |
415 | * If the page is there, skip sending the message. We don't even need the | |
416 | * lock because as long as the page arrived, it'll be there forever. | |
417 | */ | |
418 | if (received) { | |
419 | return 0; | |
420 | } | |
421 | ||
7a267fc4 PX |
422 | return migrate_send_rp_message_req_pages(mis, rb, start); |
423 | } | |
424 | ||
aad555c2 ZC |
425 | static bool migration_colo_enabled; |
426 | bool migration_incoming_colo_enabled(void) | |
427 | { | |
428 | return migration_colo_enabled; | |
429 | } | |
430 | ||
431 | void migration_incoming_disable_colo(void) | |
432 | { | |
18b1d3c9 | 433 | ram_block_discard_disable(false); |
aad555c2 ZC |
434 | migration_colo_enabled = false; |
435 | } | |
436 | ||
18b1d3c9 | 437 | int migration_incoming_enable_colo(void) |
aad555c2 | 438 | { |
18b1d3c9 DH |
439 | if (ram_block_discard_disable(true)) { |
440 | error_report("COLO: cannot disable RAM discard"); | |
441 | return -EBUSY; | |
442 | } | |
aad555c2 | 443 | migration_colo_enabled = true; |
18b1d3c9 | 444 | return 0; |
aad555c2 ZC |
445 | } |
446 | ||
9aca82ba JQ |
447 | void migrate_add_address(SocketAddress *address) |
448 | { | |
449 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
9aca82ba | 450 | |
54aa3de7 EB |
451 | QAPI_LIST_PREPEND(mis->socket_address_list, |
452 | QAPI_CLONE(SocketAddress, address)); | |
9aca82ba JQ |
453 | } |
454 | ||
e69d50d6 | 455 | static void qemu_start_incoming_migration(const char *uri, Error **errp) |
5bb7910a | 456 | { |
d658f65c | 457 | const char *p = NULL; |
34c9dd8e | 458 | |
d6f74fd1 PX |
459 | /* URI is not suitable for migration? */ |
460 | if (!migration_channels_and_uri_compatible(uri, errp)) { | |
461 | return; | |
462 | } | |
463 | ||
3ab72385 | 464 | qapi_event_send_migration(MIGRATION_STATUS_SETUP); |
e69d50d6 PB |
465 | if (strstart(uri, "tcp:", &p) || |
466 | strstart(uri, "unix:", NULL) || | |
467 | strstart(uri, "vsock:", NULL)) { | |
d658f65c | 468 | socket_start_incoming_migration(p ? p : uri, errp); |
2da776db | 469 | #ifdef CONFIG_RDMA |
adde220a | 470 | } else if (strstart(uri, "rdma:", &p)) { |
2da776db MH |
471 | rdma_start_incoming_migration(p, errp); |
472 | #endif | |
adde220a | 473 | } else if (strstart(uri, "exec:", &p)) { |
43eaae28 | 474 | exec_start_incoming_migration(p, errp); |
adde220a | 475 | } else if (strstart(uri, "fd:", &p)) { |
43eaae28 | 476 | fd_start_incoming_migration(p, errp); |
adde220a | 477 | } else { |
312fd5f2 | 478 | error_setg(errp, "unknown migration protocol: %s", uri); |
8ca5e801 | 479 | } |
5bb7910a AL |
480 | } |
481 | ||
0aa6aefc DL |
482 | static void process_incoming_migration_bh(void *opaque) |
483 | { | |
484 | Error *local_err = NULL; | |
485 | MigrationIncomingState *mis = opaque; | |
486 | ||
0f073f44 DDAG |
487 | /* If capability late_block_activate is set: |
488 | * Only fire up the block code now if we're going to restart the | |
489 | * VM, else 'cont' will do it. | |
490 | * This causes file locking to happen; so we don't want it to happen | |
491 | * unless we really are starting the VM. | |
492 | */ | |
493 | if (!migrate_late_block_activate() || | |
494 | (autostart && (!global_state_received() || | |
495 | global_state_get_runstate() == RUN_STATE_RUNNING))) { | |
3b717194 | 496 | /* Make sure all file formats throw away their mutable metadata. |
0f073f44 | 497 | * If we get an error here, just don't restart the VM yet. */ |
3b717194 | 498 | bdrv_activate_all(&local_err); |
0f073f44 DDAG |
499 | if (local_err) { |
500 | error_report_err(local_err); | |
501 | local_err = NULL; | |
502 | autostart = false; | |
503 | } | |
d35ff5e6 KW |
504 | } |
505 | ||
0aa6aefc DL |
506 | /* |
507 | * This must happen after all error conditions are dealt with and | |
508 | * we're sure the VM is going to be running on this host. | |
509 | */ | |
7659505c | 510 | qemu_announce_self(&mis->announce_timer, migrate_announce_params()); |
0aa6aefc | 511 | |
cfc3bcf3 | 512 | multifd_load_shutdown(); |
0aa6aefc | 513 | |
b35ebdf0 VSO |
514 | dirty_bitmap_mig_before_vm_start(); |
515 | ||
0aa6aefc DL |
516 | if (!global_state_received() || |
517 | global_state_get_runstate() == RUN_STATE_RUNNING) { | |
518 | if (autostart) { | |
519 | vm_start(); | |
520 | } else { | |
521 | runstate_set(RUN_STATE_PAUSED); | |
522 | } | |
db009729 ZC |
523 | } else if (migration_incoming_colo_enabled()) { |
524 | migration_incoming_disable_colo(); | |
525 | vm_start(); | |
0aa6aefc DL |
526 | } else { |
527 | runstate_set(global_state_get_runstate()); | |
528 | } | |
0aa6aefc DL |
529 | /* |
530 | * This must happen after any state changes since as soon as an external | |
531 | * observer sees this event they might start to prod at the VM assuming | |
532 | * it's ready to use. | |
533 | */ | |
534 | migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, | |
535 | MIGRATION_STATUS_COMPLETED); | |
536 | qemu_bh_delete(mis->bh); | |
537 | migration_incoming_state_destroy(); | |
538 | } | |
539 | ||
38e8f9af MAL |
540 | static void coroutine_fn |
541 | process_incoming_migration_co(void *opaque) | |
511c0231 | 542 | { |
b4b076da | 543 | MigrationIncomingState *mis = migration_incoming_get_current(); |
e9bef235 | 544 | PostcopyState ps; |
1c12e1f5 | 545 | int ret; |
8e48ac95 | 546 | Error *local_err = NULL; |
1c12e1f5 | 547 | |
4f0fae7f | 548 | assert(mis->from_src_file); |
92370989 | 549 | mis->migration_incoming_co = qemu_coroutine_self(); |
67f11b5c | 550 | mis->largest_page_size = qemu_ram_pagesize_largest(); |
093e3c42 | 551 | postcopy_state_set(POSTCOPY_INCOMING_NONE); |
93d7af6f HZ |
552 | migrate_set_state(&mis->state, MIGRATION_STATUS_NONE, |
553 | MIGRATION_STATUS_ACTIVE); | |
4f0fae7f | 554 | ret = qemu_loadvm_state(mis->from_src_file); |
bca7856a | 555 | |
e9bef235 DDAG |
556 | ps = postcopy_state_get(); |
557 | trace_process_incoming_migration_co_end(ret, ps); | |
558 | if (ps != POSTCOPY_INCOMING_NONE) { | |
559 | if (ps == POSTCOPY_INCOMING_ADVISE) { | |
560 | /* | |
561 | * Where a migration had postcopy enabled (and thus went to advise) | |
562 | * but managed to complete within the precopy period, we can use | |
563 | * the normal exit. | |
564 | */ | |
565 | postcopy_ram_incoming_cleanup(mis); | |
566 | } else if (ret >= 0) { | |
567 | /* | |
568 | * Postcopy was started, cleanup should happen at the end of the | |
569 | * postcopy thread. | |
570 | */ | |
571 | trace_process_incoming_migration_co_postcopy_end_main(); | |
572 | return; | |
573 | } | |
574 | /* Else if something went wrong then just fall out of the normal exit */ | |
575 | } | |
576 | ||
25d0c16f | 577 | /* we get COLO info, and know if we are in COLO mode */ |
aad555c2 | 578 | if (!ret && migration_incoming_colo_enabled()) { |
3b717194 EGE |
579 | /* Make sure all file formats throw away their mutable metadata */ |
580 | bdrv_activate_all(&local_err); | |
8e48ac95 | 581 | if (local_err) { |
8e48ac95 | 582 | error_report_err(local_err); |
6d99c2d4 | 583 | goto fail; |
8e48ac95 ZC |
584 | } |
585 | ||
25d0c16f HZ |
586 | qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming", |
587 | colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE); | |
588 | mis->have_colo_incoming_thread = true; | |
589 | qemu_coroutine_yield(); | |
590 | ||
684bfd18 | 591 | qemu_mutex_unlock_iothread(); |
25d0c16f HZ |
592 | /* Wait checkpoint incoming thread exit before free resource */ |
593 | qemu_thread_join(&mis->colo_incoming_thread); | |
684bfd18 | 594 | qemu_mutex_lock_iothread(); |
13af18f2 ZC |
595 | /* We hold the global iothread lock, so it is safe here */ |
596 | colo_release_ram_cache(); | |
25d0c16f HZ |
597 | } |
598 | ||
1c12e1f5 | 599 | if (ret < 0) { |
db80face | 600 | error_report("load of migration failed: %s", strerror(-ret)); |
6d99c2d4 | 601 | goto fail; |
511c0231 | 602 | } |
0aa6aefc DL |
603 | mis->bh = qemu_bh_new(process_incoming_migration_bh, mis); |
604 | qemu_bh_schedule(mis->bh); | |
92370989 | 605 | mis->migration_incoming_co = NULL; |
6d99c2d4 FL |
606 | return; |
607 | fail: | |
608 | local_err = NULL; | |
609 | migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, | |
610 | MIGRATION_STATUS_FAILED); | |
611 | qemu_fclose(mis->from_src_file); | |
e5bac1f5 LB |
612 | |
613 | multifd_load_cleanup(); | |
614 | ||
6d99c2d4 | 615 | exit(EXIT_FAILURE); |
511c0231 JQ |
616 | } |
617 | ||
b673eab4 | 618 | /** |
7d6f6933 | 619 | * migration_incoming_setup: Setup incoming migration |
b673eab4 JQ |
620 | * @f: file for main migration channel |
621 | * @errp: where to put errors | |
7d6f6933 MA |
622 | * |
623 | * Returns: %true on success, %false on error. | |
b673eab4 | 624 | */ |
7d6f6933 | 625 | static bool migration_incoming_setup(QEMUFile *f, Error **errp) |
82a4da79 | 626 | { |
4f0fae7f | 627 | MigrationIncomingState *mis = migration_incoming_get_current(); |
82a4da79 | 628 | |
4f0fae7f JQ |
629 | if (!mis->from_src_file) { |
630 | mis->from_src_file = f; | |
631 | } | |
06ad5135 | 632 | qemu_file_set_blocking(f, false); |
7d6f6933 | 633 | return true; |
e595a01a JQ |
634 | } |
635 | ||
36c2f8be | 636 | void migration_incoming_process(void) |
e595a01a JQ |
637 | { |
638 | Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL); | |
0b8b8753 | 639 | qemu_coroutine_enter(co); |
82a4da79 PB |
640 | } |
641 | ||
884835fa | 642 | /* Returns true if recovered from a paused migration, otherwise false */ |
a39e9339 | 643 | static bool postcopy_try_recover(void) |
e595a01a | 644 | { |
d96c9e8d PX |
645 | MigrationIncomingState *mis = migration_incoming_get_current(); |
646 | ||
647 | if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) { | |
648 | /* Resumed from a paused postcopy migration */ | |
649 | ||
a39e9339 PX |
650 | /* This should be set already in migration_incoming_setup() */ |
651 | assert(mis->from_src_file); | |
d96c9e8d | 652 | /* Postcopy has standalone thread to do vm load */ |
a39e9339 | 653 | qemu_file_set_blocking(mis->from_src_file, true); |
d96c9e8d PX |
654 | |
655 | /* Re-configure the return path */ | |
a39e9339 | 656 | mis->to_src_file = qemu_file_get_return_path(mis->from_src_file); |
d96c9e8d PX |
657 | |
658 | migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED, | |
659 | MIGRATION_STATUS_POSTCOPY_RECOVER); | |
660 | ||
661 | /* | |
662 | * Here, we only wake up the main loading thread (while the | |
60bb3c58 | 663 | * rest threads will still be waiting), so that we can receive |
d96c9e8d | 664 | * commands from source now, and answer it if needed. The |
60bb3c58 | 665 | * rest threads will be woken up afterwards until we are sure |
d96c9e8d PX |
666 | * that source is ready to reply to page requests. |
667 | */ | |
668 | qemu_sem_post(&mis->postcopy_pause_sem_dst); | |
884835fa PX |
669 | return true; |
670 | } | |
671 | ||
672 | return false; | |
673 | } | |
674 | ||
b673eab4 | 675 | void migration_fd_process_incoming(QEMUFile *f, Error **errp) |
884835fa | 676 | { |
a39e9339 | 677 | if (!migration_incoming_setup(f, errp)) { |
884835fa | 678 | return; |
d96c9e8d | 679 | } |
a39e9339 | 680 | if (postcopy_try_recover()) { |
b673eab4 JQ |
681 | return; |
682 | } | |
884835fa | 683 | migration_incoming_process(); |
e595a01a JQ |
684 | } |
685 | ||
5655aab0 PX |
686 | /* |
687 | * Returns true when we want to start a new incoming migration process, | |
688 | * false otherwise. | |
689 | */ | |
690 | static bool migration_should_start_incoming(bool main_channel) | |
691 | { | |
692 | /* Multifd doesn't start unless all channels are established */ | |
51b07548 | 693 | if (migrate_multifd()) { |
5655aab0 PX |
694 | return migration_has_all_channels(); |
695 | } | |
696 | ||
697 | /* Preempt channel only starts when the main channel is created */ | |
698 | if (migrate_postcopy_preempt()) { | |
699 | return main_channel; | |
700 | } | |
701 | ||
702 | /* | |
703 | * For all the rest types of migration, we should only reach here when | |
704 | * it's the main channel that's being created, and we should always | |
705 | * proceed with this channel. | |
706 | */ | |
707 | assert(main_channel); | |
708 | return true; | |
709 | } | |
710 | ||
49ed0d24 | 711 | void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp) |
4f0fae7f JQ |
712 | { |
713 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
b673eab4 | 714 | Error *local_err = NULL; |
36f62f11 | 715 | QEMUFile *f; |
6720c2b3 | 716 | bool default_channel = true; |
717 | uint32_t channel_magic = 0; | |
718 | int ret = 0; | |
4f0fae7f | 719 | |
51b07548 | 720 | if (migrate_multifd() && !migrate_postcopy_ram() && |
6720c2b3 | 721 | qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_READ_MSG_PEEK)) { |
722 | /* | |
723 | * With multiple channels, it is possible that we receive channels | |
724 | * out of order on destination side, causing incorrect mapping of | |
725 | * source channels on destination side. Check channel MAGIC to | |
726 | * decide type of channel. Please note this is best effort, postcopy | |
727 | * preempt channel does not send any magic number so avoid it for | |
728 | * postcopy live migration. Also tls live migration already does | |
729 | * tls handshake while initializing main channel so with tls this | |
730 | * issue is not possible. | |
731 | */ | |
732 | ret = migration_channel_read_peek(ioc, (void *)&channel_magic, | |
733 | sizeof(channel_magic), &local_err); | |
734 | ||
735 | if (ret != 0) { | |
736 | error_propagate(errp, local_err); | |
737 | return; | |
738 | } | |
739 | ||
740 | default_channel = (channel_magic == cpu_to_be32(QEMU_VM_FILE_MAGIC)); | |
741 | } else { | |
742 | default_channel = !mis->from_src_file; | |
743 | } | |
744 | ||
745 | if (multifd_load_setup(errp) != 0) { | |
746 | error_setg(errp, "Failed to setup multifd channels"); | |
747 | return; | |
748 | } | |
749 | ||
750 | if (default_channel) { | |
36f62f11 | 751 | f = qemu_file_new_input(ioc); |
a429e7f4 | 752 | |
7d6f6933 | 753 | if (!migration_incoming_setup(f, errp)) { |
b673eab4 JQ |
754 | return; |
755 | } | |
a429e7f4 PX |
756 | } else { |
757 | /* Multiple connections */ | |
36f62f11 | 758 | assert(migration_needs_multiple_sockets()); |
51b07548 | 759 | if (migrate_multifd()) { |
6720c2b3 | 760 | multifd_recv_new_channel(ioc, &local_err); |
36f62f11 PX |
761 | } else { |
762 | assert(migrate_postcopy_preempt()); | |
763 | f = qemu_file_new_input(ioc); | |
6720c2b3 | 764 | postcopy_preempt_new_channel(mis, f); |
36f62f11 | 765 | } |
49ed0d24 FL |
766 | if (local_err) { |
767 | error_propagate(errp, local_err); | |
768 | return; | |
769 | } | |
4f0fae7f | 770 | } |
81e62053 | 771 | |
5655aab0 | 772 | if (migration_should_start_incoming(default_channel)) { |
a39e9339 PX |
773 | /* If it's a recovery, we're done */ |
774 | if (postcopy_try_recover()) { | |
775 | return; | |
776 | } | |
81e62053 PX |
777 | migration_incoming_process(); |
778 | } | |
4f0fae7f JQ |
779 | } |
780 | ||
428d8908 JQ |
781 | /** |
782 | * @migration_has_all_channels: We have received all channels that we need | |
783 | * | |
784 | * Returns true when we have got connections to all the channels that | |
785 | * we need for migration. | |
786 | */ | |
787 | bool migration_has_all_channels(void) | |
788 | { | |
ca273df3 | 789 | MigrationIncomingState *mis = migration_incoming_get_current(); |
62c1e0ca | 790 | |
36f62f11 PX |
791 | if (!mis->from_src_file) { |
792 | return false; | |
793 | } | |
62c1e0ca | 794 | |
51b07548 | 795 | if (migrate_multifd()) { |
36f62f11 PX |
796 | return multifd_recv_all_channels_created(); |
797 | } | |
798 | ||
799 | if (migrate_postcopy_preempt()) { | |
800 | return mis->postcopy_qemufile_dst != NULL; | |
801 | } | |
802 | ||
803 | return true; | |
428d8908 JQ |
804 | } |
805 | ||
6decec93 DDAG |
806 | /* |
807 | * Send a 'SHUT' message on the return channel with the given value | |
808 | * to indicate that we've finished with the RP. Non-0 value indicates | |
809 | * error. | |
810 | */ | |
811 | void migrate_send_rp_shut(MigrationIncomingState *mis, | |
812 | uint32_t value) | |
813 | { | |
814 | uint32_t buf; | |
815 | ||
816 | buf = cpu_to_be32(value); | |
817 | migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf); | |
818 | } | |
819 | ||
820 | /* | |
821 | * Send a 'PONG' message on the return channel with the given value | |
822 | * (normally in response to a 'PING') | |
823 | */ | |
824 | void migrate_send_rp_pong(MigrationIncomingState *mis, | |
825 | uint32_t value) | |
826 | { | |
827 | uint32_t buf; | |
828 | ||
829 | buf = cpu_to_be32(value); | |
830 | migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf); | |
831 | } | |
832 | ||
a335debb PX |
833 | void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis, |
834 | char *block_name) | |
835 | { | |
836 | char buf[512]; | |
837 | int len; | |
838 | int64_t res; | |
839 | ||
840 | /* | |
841 | * First, we send the header part. It contains only the len of | |
842 | * idstr, and the idstr itself. | |
843 | */ | |
844 | len = strlen(block_name); | |
845 | buf[0] = len; | |
846 | memcpy(buf + 1, block_name, len); | |
847 | ||
848 | if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) { | |
849 | error_report("%s: MSG_RP_RECV_BITMAP only used for recovery", | |
850 | __func__); | |
851 | return; | |
852 | } | |
853 | ||
854 | migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf); | |
855 | ||
856 | /* | |
857 | * Next, we dump the received bitmap to the stream. | |
858 | * | |
859 | * TODO: currently we are safe since we are the only one that is | |
860 | * using the to_src_file handle (fault thread is still paused), | |
861 | * and it's ok even not taking the mutex. However the best way is | |
862 | * to take the lock before sending the message header, and release | |
863 | * the lock after sending the bitmap. | |
864 | */ | |
865 | qemu_mutex_lock(&mis->rp_mutex); | |
866 | res = ramblock_recv_bitmap_send(mis->to_src_file, block_name); | |
867 | qemu_mutex_unlock(&mis->rp_mutex); | |
868 | ||
869 | trace_migrate_send_rp_recv_bitmap(block_name, res); | |
870 | } | |
871 | ||
13955b89 PX |
872 | void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value) |
873 | { | |
874 | uint32_t buf; | |
875 | ||
876 | buf = cpu_to_be32(value); | |
877 | migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf); | |
878 | } | |
879 | ||
f6844b99 DDAG |
880 | /* |
881 | * Return true if we're already in the middle of a migration | |
882 | * (i.e. any of the active or setup states) | |
883 | */ | |
3d63da16 | 884 | bool migration_is_setup_or_active(int state) |
f6844b99 DDAG |
885 | { |
886 | switch (state) { | |
887 | case MIGRATION_STATUS_ACTIVE: | |
9ec055ae | 888 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: |
a688d2c1 | 889 | case MIGRATION_STATUS_POSTCOPY_PAUSED: |
135b87b4 | 890 | case MIGRATION_STATUS_POSTCOPY_RECOVER: |
f6844b99 | 891 | case MIGRATION_STATUS_SETUP: |
31e06077 DDAG |
892 | case MIGRATION_STATUS_PRE_SWITCHOVER: |
893 | case MIGRATION_STATUS_DEVICE: | |
c7e0acd5 | 894 | case MIGRATION_STATUS_WAIT_UNPLUG: |
19dd408a | 895 | case MIGRATION_STATUS_COLO: |
f6844b99 DDAG |
896 | return true; |
897 | ||
898 | default: | |
899 | return false; | |
900 | ||
901 | } | |
902 | } | |
903 | ||
392d87e2 JQ |
904 | bool migration_is_running(int state) |
905 | { | |
906 | switch (state) { | |
907 | case MIGRATION_STATUS_ACTIVE: | |
908 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: | |
909 | case MIGRATION_STATUS_POSTCOPY_PAUSED: | |
910 | case MIGRATION_STATUS_POSTCOPY_RECOVER: | |
911 | case MIGRATION_STATUS_SETUP: | |
912 | case MIGRATION_STATUS_PRE_SWITCHOVER: | |
913 | case MIGRATION_STATUS_DEVICE: | |
914 | case MIGRATION_STATUS_WAIT_UNPLUG: | |
915 | case MIGRATION_STATUS_CANCELLING: | |
392d87e2 JQ |
916 | return true; |
917 | ||
918 | default: | |
919 | return false; | |
920 | ||
921 | } | |
922 | } | |
923 | ||
db18dee7 PX |
924 | static bool migrate_show_downtime(MigrationState *s) |
925 | { | |
926 | return (s->state == MIGRATION_STATUS_COMPLETED) || migration_in_postcopy(); | |
927 | } | |
928 | ||
640dfb14 WY |
929 | static void populate_time_info(MigrationInfo *info, MigrationState *s) |
930 | { | |
931 | info->has_status = true; | |
932 | info->has_setup_time = true; | |
933 | info->setup_time = s->setup_time; | |
db18dee7 | 934 | |
640dfb14 WY |
935 | if (s->state == MIGRATION_STATUS_COMPLETED) { |
936 | info->has_total_time = true; | |
937 | info->total_time = s->total_time; | |
640dfb14 WY |
938 | } else { |
939 | info->has_total_time = true; | |
940 | info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - | |
941 | s->start_time; | |
db18dee7 PX |
942 | } |
943 | ||
944 | if (migrate_show_downtime(s)) { | |
945 | info->has_downtime = true; | |
946 | info->downtime = s->downtime; | |
947 | } else { | |
640dfb14 WY |
948 | info->has_expected_downtime = true; |
949 | info->expected_downtime = s->expected_downtime; | |
950 | } | |
951 | } | |
952 | ||
a22463a5 DDAG |
953 | static void populate_ram_info(MigrationInfo *info, MigrationState *s) |
954 | { | |
144fa06b JQ |
955 | size_t page_size = qemu_target_page_size(); |
956 | ||
a22463a5 | 957 | info->ram = g_malloc0(sizeof(*info->ram)); |
abce5fa1 | 958 | info->ram->transferred = stat64_get(&ram_counters.transferred); |
a22463a5 | 959 | info->ram->total = ram_bytes_total(); |
1a386e8d | 960 | info->ram->duplicate = stat64_get(&ram_counters.zero_pages); |
bedf53c1 JQ |
961 | /* legacy value. It is not used anymore */ |
962 | info->ram->skipped = 0; | |
8c0cda8f | 963 | info->ram->normal = stat64_get(&ram_counters.normal_pages); |
23b7576d | 964 | info->ram->normal_bytes = info->ram->normal * page_size; |
a22463a5 | 965 | info->ram->mbps = s->mbps; |
536b5a4e JQ |
966 | info->ram->dirty_sync_count = |
967 | stat64_get(&ram_counters.dirty_sync_count); | |
cf20c897 | 968 | info->ram->dirty_sync_missed_zero_copy = |
42918236 | 969 | stat64_get(&ram_counters.dirty_sync_missed_zero_copy); |
3c764f9b JQ |
970 | info->ram->postcopy_requests = |
971 | stat64_get(&ram_counters.postcopy_requests); | |
144fa06b | 972 | info->ram->page_size = page_size; |
cf671116 | 973 | info->ram->multifd_bytes = stat64_get(&ram_counters.multifd_bytes); |
aecbfe9c | 974 | info->ram->pages_per_second = s->pages_per_second; |
b013b5d1 | 975 | info->ram->precopy_bytes = stat64_get(&ram_counters.precopy_bytes); |
296a4ac2 | 976 | info->ram->downtime_bytes = stat64_get(&ram_counters.downtime_bytes); |
abce5fa1 | 977 | info->ram->postcopy_bytes = stat64_get(&ram_counters.postcopy_bytes); |
a22463a5 | 978 | |
87dca0c9 | 979 | if (migrate_xbzrle()) { |
114f5aee JQ |
980 | info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache)); |
981 | info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size(); | |
9360447d JQ |
982 | info->xbzrle_cache->bytes = xbzrle_counters.bytes; |
983 | info->xbzrle_cache->pages = xbzrle_counters.pages; | |
984 | info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss; | |
985 | info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate; | |
e460a4b1 | 986 | info->xbzrle_cache->encoding_rate = xbzrle_counters.encoding_rate; |
9360447d | 987 | info->xbzrle_cache->overflow = xbzrle_counters.overflow; |
114f5aee JQ |
988 | } |
989 | ||
a7a94d14 | 990 | if (migrate_compress()) { |
76e03000 XG |
991 | info->compression = g_malloc0(sizeof(*info->compression)); |
992 | info->compression->pages = compression_counters.pages; | |
993 | info->compression->busy = compression_counters.busy; | |
994 | info->compression->busy_rate = compression_counters.busy_rate; | |
995 | info->compression->compressed_size = | |
996 | compression_counters.compressed_size; | |
997 | info->compression->compression_rate = | |
998 | compression_counters.compression_rate; | |
999 | } | |
1000 | ||
338182c8 JQ |
1001 | if (cpu_throttle_active()) { |
1002 | info->has_cpu_throttle_percentage = true; | |
1003 | info->cpu_throttle_percentage = cpu_throttle_get_percentage(); | |
1004 | } | |
1005 | ||
a22463a5 DDAG |
1006 | if (s->state != MIGRATION_STATUS_COMPLETED) { |
1007 | info->ram->remaining = ram_bytes_remaining(); | |
9360447d | 1008 | info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate; |
a22463a5 DDAG |
1009 | } |
1010 | } | |
1011 | ||
930ac04c JQ |
1012 | static void populate_disk_info(MigrationInfo *info) |
1013 | { | |
1014 | if (blk_mig_active()) { | |
930ac04c JQ |
1015 | info->disk = g_malloc0(sizeof(*info->disk)); |
1016 | info->disk->transferred = blk_mig_bytes_transferred(); | |
1017 | info->disk->remaining = blk_mig_bytes_remaining(); | |
1018 | info->disk->total = blk_mig_bytes_total(); | |
1019 | } | |
1020 | } | |
1021 | ||
65ace060 | 1022 | static void fill_source_migration_info(MigrationInfo *info) |
5bb7910a | 1023 | { |
17549e84 | 1024 | MigrationState *s = migrate_get_current(); |
552de79b | 1025 | int state = qatomic_read(&s->state); |
372043f3 | 1026 | GSList *cur_blocker = migration_blockers; |
17549e84 | 1027 | |
3af8554b | 1028 | info->blocked_reasons = NULL; |
3af8554b | 1029 | |
372043f3 MA |
1030 | /* |
1031 | * There are two types of reasons a migration might be blocked; | |
1032 | * a) devices marked in VMState as non-migratable, and | |
1033 | * b) Explicit migration blockers | |
1034 | * We need to add both of them here. | |
1035 | */ | |
1036 | qemu_savevm_non_migratable_list(&info->blocked_reasons); | |
3af8554b | 1037 | |
372043f3 MA |
1038 | while (cur_blocker) { |
1039 | QAPI_LIST_PREPEND(info->blocked_reasons, | |
1040 | g_strdup(error_get_pretty(cur_blocker->data))); | |
1041 | cur_blocker = g_slist_next(cur_blocker); | |
3af8554b | 1042 | } |
372043f3 | 1043 | info->has_blocked_reasons = info->blocked_reasons != NULL; |
3af8554b | 1044 | |
552de79b | 1045 | switch (state) { |
31194731 | 1046 | case MIGRATION_STATUS_NONE: |
17549e84 | 1047 | /* no migration has happened ever */ |
65ace060 AP |
1048 | /* do not overwrite destination migration status */ |
1049 | return; | |
31194731 | 1050 | case MIGRATION_STATUS_SETUP: |
29ae8a41 | 1051 | info->has_status = true; |
ed4fbd10 | 1052 | info->has_total_time = false; |
29ae8a41 | 1053 | break; |
31194731 HZ |
1054 | case MIGRATION_STATUS_ACTIVE: |
1055 | case MIGRATION_STATUS_CANCELLING: | |
9ec055ae | 1056 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: |
31e06077 DDAG |
1057 | case MIGRATION_STATUS_PRE_SWITCHOVER: |
1058 | case MIGRATION_STATUS_DEVICE: | |
a688d2c1 | 1059 | case MIGRATION_STATUS_POSTCOPY_PAUSED: |
135b87b4 | 1060 | case MIGRATION_STATUS_POSTCOPY_RECOVER: |
640dfb14 WY |
1061 | /* TODO add some postcopy stats */ |
1062 | populate_time_info(info, s); | |
a22463a5 | 1063 | populate_ram_info(info, s); |
930ac04c | 1064 | populate_disk_info(info); |
3710586c | 1065 | populate_vfio_info(info); |
17549e84 | 1066 | break; |
0b827d5e HZ |
1067 | case MIGRATION_STATUS_COLO: |
1068 | info->has_status = true; | |
1069 | /* TODO: display COLO specific information (checkpoint info etc.) */ | |
1070 | break; | |
31194731 | 1071 | case MIGRATION_STATUS_COMPLETED: |
640dfb14 | 1072 | populate_time_info(info, s); |
a22463a5 | 1073 | populate_ram_info(info, s); |
3710586c | 1074 | populate_vfio_info(info); |
17549e84 | 1075 | break; |
31194731 | 1076 | case MIGRATION_STATUS_FAILED: |
791e7c82 | 1077 | info->has_status = true; |
d59ce6f3 | 1078 | if (s->error) { |
d59ce6f3 DB |
1079 | info->error_desc = g_strdup(error_get_pretty(s->error)); |
1080 | } | |
17549e84 | 1081 | break; |
31194731 | 1082 | case MIGRATION_STATUS_CANCELLED: |
791e7c82 | 1083 | info->has_status = true; |
17549e84 | 1084 | break; |
c7e0acd5 JF |
1085 | case MIGRATION_STATUS_WAIT_UNPLUG: |
1086 | info->has_status = true; | |
1087 | break; | |
5bb7910a | 1088 | } |
552de79b | 1089 | info->status = state; |
5bb7910a AL |
1090 | } |
1091 | ||
65ace060 AP |
1092 | static void fill_destination_migration_info(MigrationInfo *info) |
1093 | { | |
1094 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
1095 | ||
9aca82ba JQ |
1096 | if (mis->socket_address_list) { |
1097 | info->has_socket_address = true; | |
1098 | info->socket_address = | |
1099 | QAPI_CLONE(SocketAddressList, mis->socket_address_list); | |
1100 | } | |
1101 | ||
65ace060 AP |
1102 | switch (mis->state) { |
1103 | case MIGRATION_STATUS_NONE: | |
1104 | return; | |
65ace060 AP |
1105 | case MIGRATION_STATUS_SETUP: |
1106 | case MIGRATION_STATUS_CANCELLING: | |
1107 | case MIGRATION_STATUS_CANCELLED: | |
1108 | case MIGRATION_STATUS_ACTIVE: | |
1109 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: | |
3c9928d9 PX |
1110 | case MIGRATION_STATUS_POSTCOPY_PAUSED: |
1111 | case MIGRATION_STATUS_POSTCOPY_RECOVER: | |
65ace060 AP |
1112 | case MIGRATION_STATUS_FAILED: |
1113 | case MIGRATION_STATUS_COLO: | |
1114 | info->has_status = true; | |
1115 | break; | |
1116 | case MIGRATION_STATUS_COMPLETED: | |
1117 | info->has_status = true; | |
1118 | fill_destination_postcopy_migration_info(info); | |
1119 | break; | |
1120 | } | |
1121 | info->status = mis->state; | |
1122 | } | |
1123 | ||
1124 | MigrationInfo *qmp_query_migrate(Error **errp) | |
1125 | { | |
1126 | MigrationInfo *info = g_malloc0(sizeof(*info)); | |
1127 | ||
1128 | fill_destination_migration_info(info); | |
1129 | fill_source_migration_info(info); | |
1130 | ||
1131 | return info; | |
1132 | } | |
1133 | ||
4886a1bc DDAG |
1134 | void qmp_migrate_start_postcopy(Error **errp) |
1135 | { | |
1136 | MigrationState *s = migrate_get_current(); | |
1137 | ||
16b0fd32 | 1138 | if (!migrate_postcopy()) { |
a54d340b | 1139 | error_setg(errp, "Enable postcopy with migrate_set_capability before" |
4886a1bc DDAG |
1140 | " the start of migration"); |
1141 | return; | |
1142 | } | |
1143 | ||
1144 | if (s->state == MIGRATION_STATUS_NONE) { | |
1145 | error_setg(errp, "Postcopy must be started after migration has been" | |
1146 | " started"); | |
1147 | return; | |
1148 | } | |
1149 | /* | |
1150 | * we don't error if migration has finished since that would be racy | |
1151 | * with issuing this command. | |
1152 | */ | |
d73415a3 | 1153 | qatomic_set(&s->start_postcopy, true); |
4886a1bc DDAG |
1154 | } |
1155 | ||
065e2813 AL |
1156 | /* shared migration helpers */ |
1157 | ||
48781e5b | 1158 | void migrate_set_state(int *state, int old_state, int new_state) |
51cf4c1a | 1159 | { |
a31fedee | 1160 | assert(new_state < MIGRATION_STATUS__MAX); |
d73415a3 | 1161 | if (qatomic_cmpxchg(state, old_state, new_state) == old_state) { |
a31fedee | 1162 | trace_migrate_set_state(MigrationStatus_str(new_state)); |
b05dc723 | 1163 | migrate_generate_event(new_state); |
51cf4c1a Z |
1164 | } |
1165 | } | |
1166 | ||
2833c59b JQ |
1167 | static void migrate_set_block_incremental(MigrationState *s, bool value) |
1168 | { | |
1169 | s->parameters.block_incremental = value; | |
1170 | } | |
1171 | ||
1172 | static void block_cleanup_parameters(MigrationState *s) | |
1173 | { | |
1174 | if (s->must_remove_block_options) { | |
1175 | /* setting to false can never fail */ | |
9eb1109c | 1176 | migrate_cap_set(MIGRATION_CAPABILITY_BLOCK, false, &error_abort); |
2833c59b JQ |
1177 | migrate_set_block_incremental(s, false); |
1178 | s->must_remove_block_options = false; | |
1179 | } | |
1180 | } | |
1181 | ||
fd392cfa | 1182 | static void migrate_fd_cleanup(MigrationState *s) |
065e2813 | 1183 | { |
bb1fadc4 PB |
1184 | qemu_bh_delete(s->cleanup_bh); |
1185 | s->cleanup_bh = NULL; | |
1186 | ||
83174765 PX |
1187 | g_free(s->hostname); |
1188 | s->hostname = NULL; | |
e3bf5e68 DH |
1189 | json_writer_free(s->vmdesc); |
1190 | s->vmdesc = NULL; | |
83174765 | 1191 | |
0ceccd85 PX |
1192 | qemu_savevm_state_cleanup(); |
1193 | ||
89a02a9f | 1194 | if (s->to_dst_file) { |
62df066f | 1195 | QEMUFile *tmp; |
f986c3d2 | 1196 | |
9013dca5 | 1197 | trace_migrate_fd_cleanup(); |
404a7c05 | 1198 | qemu_mutex_unlock_iothread(); |
1d34e4bf DDAG |
1199 | if (s->migration_thread_running) { |
1200 | qemu_thread_join(&s->thread); | |
1201 | s->migration_thread_running = false; | |
1202 | } | |
404a7c05 PB |
1203 | qemu_mutex_lock_iothread(); |
1204 | ||
1398b2e3 | 1205 | multifd_save_cleanup(); |
62df066f PX |
1206 | qemu_mutex_lock(&s->qemu_file_lock); |
1207 | tmp = s->to_dst_file; | |
89a02a9f | 1208 | s->to_dst_file = NULL; |
62df066f PX |
1209 | qemu_mutex_unlock(&s->qemu_file_lock); |
1210 | /* | |
1211 | * Close the file handle without the lock to make sure the | |
1212 | * critical section won't block for long. | |
1213 | */ | |
39675fff | 1214 | migration_ioc_unregister_yank_from_file(tmp); |
62df066f | 1215 | qemu_fclose(tmp); |
065e2813 AL |
1216 | } |
1217 | ||
36f62f11 PX |
1218 | if (s->postcopy_qemufile_src) { |
1219 | migration_ioc_unregister_yank_from_file(s->postcopy_qemufile_src); | |
1220 | qemu_fclose(s->postcopy_qemufile_src); | |
1221 | s->postcopy_qemufile_src = NULL; | |
1222 | } | |
1223 | ||
8f8d528e | 1224 | assert(!migration_is_active(s)); |
7a2c1721 | 1225 | |
94f5a437 | 1226 | if (s->state == MIGRATION_STATUS_CANCELLING) { |
48781e5b | 1227 | migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING, |
94f5a437 | 1228 | MIGRATION_STATUS_CANCELLED); |
7a2c1721 | 1229 | } |
a3fa1d78 | 1230 | |
87db1a7d JQ |
1231 | if (s->error) { |
1232 | /* It is used on info migrate. We can't free it */ | |
1233 | error_report_err(error_copy(s->error)); | |
1234 | } | |
a3fa1d78 | 1235 | notifier_list_notify(&migration_state_notifiers, s); |
2833c59b | 1236 | block_cleanup_parameters(s); |
b5eea99e | 1237 | yank_unregister_instance(MIGRATION_YANK_INSTANCE); |
065e2813 AL |
1238 | } |
1239 | ||
fd392cfa YK |
1240 | static void migrate_fd_cleanup_schedule(MigrationState *s) |
1241 | { | |
1242 | /* | |
1243 | * Ref the state for bh, because it may be called when | |
1244 | * there're already no other refs | |
1245 | */ | |
1246 | object_ref(OBJECT(s)); | |
1247 | qemu_bh_schedule(s->cleanup_bh); | |
1248 | } | |
1249 | ||
1250 | static void migrate_fd_cleanup_bh(void *opaque) | |
1251 | { | |
1252 | MigrationState *s = opaque; | |
1253 | migrate_fd_cleanup(s); | |
1254 | object_unref(OBJECT(s)); | |
1255 | } | |
1256 | ||
87db1a7d JQ |
1257 | void migrate_set_error(MigrationState *s, const Error *error) |
1258 | { | |
6e8a355d | 1259 | QEMU_LOCK_GUARD(&s->error_mutex); |
87db1a7d JQ |
1260 | if (!s->error) { |
1261 | s->error = error_copy(error); | |
1262 | } | |
87db1a7d JQ |
1263 | } |
1264 | ||
ca7bd082 PX |
1265 | static void migrate_error_free(MigrationState *s) |
1266 | { | |
1267 | QEMU_LOCK_GUARD(&s->error_mutex); | |
1268 | if (s->error) { | |
1269 | error_free(s->error); | |
1270 | s->error = NULL; | |
1271 | } | |
1272 | } | |
1273 | ||
d59ce6f3 | 1274 | void migrate_fd_error(MigrationState *s, const Error *error) |
065e2813 | 1275 | { |
25174055 | 1276 | trace_migrate_fd_error(error_get_pretty(error)); |
89a02a9f | 1277 | assert(s->to_dst_file == NULL); |
48781e5b HZ |
1278 | migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, |
1279 | MIGRATION_STATUS_FAILED); | |
87db1a7d | 1280 | migrate_set_error(s, error); |
458cf28e JQ |
1281 | } |
1282 | ||
0edda1c4 | 1283 | static void migrate_fd_cancel(MigrationState *s) |
065e2813 | 1284 | { |
6f2b811a | 1285 | int old_state ; |
89a02a9f | 1286 | QEMUFile *f = migrate_get_current()->to_dst_file; |
9013dca5 | 1287 | trace_migrate_fd_cancel(); |
065e2813 | 1288 | |
43044ac0 PX |
1289 | WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) { |
1290 | if (s->rp_state.from_dst_file) { | |
1291 | /* shutdown the rp socket, so causing the rp thread to shutdown */ | |
1292 | qemu_file_shutdown(s->rp_state.from_dst_file); | |
1293 | } | |
70b20477 DDAG |
1294 | } |
1295 | ||
6f2b811a Z |
1296 | do { |
1297 | old_state = s->state; | |
392d87e2 | 1298 | if (!migration_is_running(old_state)) { |
6f2b811a Z |
1299 | break; |
1300 | } | |
a7b36b48 DDAG |
1301 | /* If the migration is paused, kick it out of the pause */ |
1302 | if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) { | |
1303 | qemu_sem_post(&s->pause_sem); | |
1304 | } | |
48781e5b | 1305 | migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING); |
31194731 | 1306 | } while (s->state != MIGRATION_STATUS_CANCELLING); |
a26ba26e DDAG |
1307 | |
1308 | /* | |
1309 | * If we're unlucky the migration code might be stuck somewhere in a | |
1310 | * send/write while the network has failed and is waiting to timeout; | |
1311 | * if we've got shutdown(2) available then we can force it to quit. | |
1312 | * The outgoing qemu file gets closed in migrate_fd_cleanup that is | |
1313 | * called in a bh, so there is no race against this cancel. | |
1314 | */ | |
31194731 | 1315 | if (s->state == MIGRATION_STATUS_CANCELLING && f) { |
a26ba26e DDAG |
1316 | qemu_file_shutdown(f); |
1317 | } | |
1d2acc31 HZ |
1318 | if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) { |
1319 | Error *local_err = NULL; | |
1320 | ||
3b717194 | 1321 | bdrv_activate_all(&local_err); |
1d2acc31 HZ |
1322 | if (local_err) { |
1323 | error_report_err(local_err); | |
1324 | } else { | |
1325 | s->block_inactive = false; | |
1326 | } | |
1327 | } | |
065e2813 AL |
1328 | } |
1329 | ||
99a0db9b GH |
1330 | void add_migration_state_change_notifier(Notifier *notify) |
1331 | { | |
1332 | notifier_list_add(&migration_state_notifiers, notify); | |
1333 | } | |
1334 | ||
1335 | void remove_migration_state_change_notifier(Notifier *notify) | |
1336 | { | |
31552529 | 1337 | notifier_remove(notify); |
99a0db9b GH |
1338 | } |
1339 | ||
02edd2e7 | 1340 | bool migration_in_setup(MigrationState *s) |
afe2df69 | 1341 | { |
31194731 | 1342 | return s->state == MIGRATION_STATUS_SETUP; |
afe2df69 GH |
1343 | } |
1344 | ||
7073693b | 1345 | bool migration_has_finished(MigrationState *s) |
99a0db9b | 1346 | { |
31194731 | 1347 | return s->state == MIGRATION_STATUS_COMPLETED; |
99a0db9b | 1348 | } |
0edda1c4 | 1349 | |
afe2df69 GH |
1350 | bool migration_has_failed(MigrationState *s) |
1351 | { | |
31194731 HZ |
1352 | return (s->state == MIGRATION_STATUS_CANCELLED || |
1353 | s->state == MIGRATION_STATUS_FAILED); | |
afe2df69 GH |
1354 | } |
1355 | ||
5727309d | 1356 | bool migration_in_postcopy(void) |
9ec055ae | 1357 | { |
5727309d JQ |
1358 | MigrationState *s = migrate_get_current(); |
1359 | ||
3748fef9 DDAG |
1360 | switch (s->state) { |
1361 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: | |
1362 | case MIGRATION_STATUS_POSTCOPY_PAUSED: | |
1363 | case MIGRATION_STATUS_POSTCOPY_RECOVER: | |
1364 | return true; | |
1365 | default: | |
1366 | return false; | |
1367 | } | |
9ec055ae DDAG |
1368 | } |
1369 | ||
b82fc321 DDAG |
1370 | bool migration_in_postcopy_after_devices(MigrationState *s) |
1371 | { | |
5727309d | 1372 | return migration_in_postcopy() && s->postcopy_after_devices; |
b82fc321 DDAG |
1373 | } |
1374 | ||
06df2e69 DH |
1375 | bool migration_in_incoming_postcopy(void) |
1376 | { | |
1377 | PostcopyState ps = postcopy_state_get(); | |
1378 | ||
1379 | return ps >= POSTCOPY_INCOMING_DISCARD && ps < POSTCOPY_INCOMING_END; | |
1380 | } | |
1381 | ||
80fe315c DH |
1382 | bool migration_incoming_postcopy_advised(void) |
1383 | { | |
1384 | PostcopyState ps = postcopy_state_get(); | |
1385 | ||
1386 | return ps >= POSTCOPY_INCOMING_ADVISE && ps < POSTCOPY_INCOMING_END; | |
1387 | } | |
1388 | ||
1a8e44a8 AG |
1389 | bool migration_in_bg_snapshot(void) |
1390 | { | |
1391 | MigrationState *s = migrate_get_current(); | |
1392 | ||
1393 | return migrate_background_snapshot() && | |
1394 | migration_is_setup_or_active(s->state); | |
1395 | } | |
1396 | ||
fab35005 | 1397 | bool migration_is_idle(void) |
fe44dc91 | 1398 | { |
daff7f0b MA |
1399 | MigrationState *s = current_migration; |
1400 | ||
1401 | if (!s) { | |
1402 | return true; | |
1403 | } | |
fe44dc91 AA |
1404 | |
1405 | switch (s->state) { | |
1406 | case MIGRATION_STATUS_NONE: | |
1407 | case MIGRATION_STATUS_CANCELLED: | |
1408 | case MIGRATION_STATUS_COMPLETED: | |
1409 | case MIGRATION_STATUS_FAILED: | |
1410 | return true; | |
1411 | case MIGRATION_STATUS_SETUP: | |
1412 | case MIGRATION_STATUS_CANCELLING: | |
1413 | case MIGRATION_STATUS_ACTIVE: | |
1414 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: | |
1415 | case MIGRATION_STATUS_COLO: | |
31e06077 DDAG |
1416 | case MIGRATION_STATUS_PRE_SWITCHOVER: |
1417 | case MIGRATION_STATUS_DEVICE: | |
c7e0acd5 | 1418 | case MIGRATION_STATUS_WAIT_UNPLUG: |
fe44dc91 AA |
1419 | return false; |
1420 | case MIGRATION_STATUS__MAX: | |
1421 | g_assert_not_reached(); | |
1422 | } | |
1423 | ||
1424 | return false; | |
1425 | } | |
1426 | ||
8f8d528e WY |
1427 | bool migration_is_active(MigrationState *s) |
1428 | { | |
1429 | return (s->state == MIGRATION_STATUS_ACTIVE || | |
1430 | s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE); | |
1431 | } | |
1432 | ||
3e0c8050 | 1433 | void migrate_init(MigrationState *s) |
0edda1c4 | 1434 | { |
389775d1 DDAG |
1435 | /* |
1436 | * Reinitialise all migration state, except | |
1437 | * parameters/capabilities that the user set, and | |
1438 | * locks. | |
1439 | */ | |
389775d1 | 1440 | s->cleanup_bh = 0; |
8518278a | 1441 | s->vm_start_bh = 0; |
89a02a9f | 1442 | s->to_dst_file = NULL; |
389775d1 | 1443 | s->state = MIGRATION_STATUS_NONE; |
389775d1 DDAG |
1444 | s->rp_state.from_dst_file = NULL; |
1445 | s->rp_state.error = false; | |
1446 | s->mbps = 0.0; | |
aecbfe9c | 1447 | s->pages_per_second = 0.0; |
389775d1 DDAG |
1448 | s->downtime = 0; |
1449 | s->expected_downtime = 0; | |
389775d1 | 1450 | s->setup_time = 0; |
389775d1 | 1451 | s->start_postcopy = false; |
b82fc321 | 1452 | s->postcopy_after_devices = false; |
389775d1 | 1453 | s->migration_thread_running = false; |
d59ce6f3 DB |
1454 | error_free(s->error); |
1455 | s->error = NULL; | |
d8053e73 | 1456 | s->hostname = NULL; |
389775d1 | 1457 | |
48781e5b | 1458 | migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP); |
0edda1c4 | 1459 | |
4af246a3 PX |
1460 | s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
1461 | s->total_time = 0; | |
7287cbd4 | 1462 | s->vm_was_running = false; |
b15df1ae PX |
1463 | s->iteration_initial_bytes = 0; |
1464 | s->threshold_size = 0; | |
0edda1c4 | 1465 | } |
cab30143 | 1466 | |
60fd6801 | 1467 | int migrate_add_blocker_internal(Error *reason, Error **errp) |
fa2756b7 | 1468 | { |
4c170330 PX |
1469 | /* Snapshots are similar to migrations, so check RUN_STATE_SAVE_VM too. */ |
1470 | if (runstate_check(RUN_STATE_SAVE_VM) || !migration_is_idle()) { | |
1471 | error_propagate_prepend(errp, error_copy(reason), | |
1472 | "disallowing migration blocker " | |
1473 | "(migration/snapshot in progress) for: "); | |
1474 | return -EBUSY; | |
fe44dc91 AA |
1475 | } |
1476 | ||
4c170330 PX |
1477 | migration_blockers = g_slist_prepend(migration_blockers, reason); |
1478 | return 0; | |
fa2756b7 AL |
1479 | } |
1480 | ||
60fd6801 PX |
1481 | int migrate_add_blocker(Error *reason, Error **errp) |
1482 | { | |
1483 | if (only_migratable) { | |
1484 | error_propagate_prepend(errp, error_copy(reason), | |
1485 | "disallowing migration blocker " | |
1486 | "(--only-migratable) for: "); | |
1487 | return -EACCES; | |
1488 | } | |
1489 | ||
1490 | return migrate_add_blocker_internal(reason, errp); | |
1491 | } | |
1492 | ||
fa2756b7 AL |
1493 | void migrate_del_blocker(Error *reason) |
1494 | { | |
1495 | migration_blockers = g_slist_remove(migration_blockers, reason); | |
1496 | } | |
1497 | ||
bf1ae1f4 DDAG |
1498 | void qmp_migrate_incoming(const char *uri, Error **errp) |
1499 | { | |
1500 | Error *local_err = NULL; | |
4debb5f5 | 1501 | static bool once = true; |
bf1ae1f4 | 1502 | |
4debb5f5 DDAG |
1503 | if (!once) { |
1504 | error_setg(errp, "The incoming migration has already been started"); | |
603d5a42 | 1505 | return; |
4debb5f5 | 1506 | } |
e69d50d6 PB |
1507 | if (!runstate_check(RUN_STATE_INMIGRATE)) { |
1508 | error_setg(errp, "'-incoming' was not specified on the command line"); | |
1509 | return; | |
1510 | } | |
bf1ae1f4 | 1511 | |
cc48c587 PX |
1512 | if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) { |
1513 | return; | |
1514 | } | |
1515 | ||
bf1ae1f4 DDAG |
1516 | qemu_start_incoming_migration(uri, &local_err); |
1517 | ||
1518 | if (local_err) { | |
cc48c587 | 1519 | yank_unregister_instance(MIGRATION_YANK_INSTANCE); |
bf1ae1f4 DDAG |
1520 | error_propagate(errp, local_err); |
1521 | return; | |
1522 | } | |
1523 | ||
4debb5f5 | 1524 | once = false; |
bf1ae1f4 DDAG |
1525 | } |
1526 | ||
02affd41 PX |
1527 | void qmp_migrate_recover(const char *uri, Error **errp) |
1528 | { | |
1529 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
1530 | ||
b7f9afd4 PX |
1531 | /* |
1532 | * Don't even bother to use ERRP_GUARD() as it _must_ always be set by | |
1533 | * callers (no one should ignore a recover failure); if there is, it's a | |
1534 | * programming error. | |
1535 | */ | |
1536 | assert(errp); | |
1537 | ||
02affd41 PX |
1538 | if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) { |
1539 | error_setg(errp, "Migrate recover can only be run " | |
1540 | "when postcopy is paused."); | |
1541 | return; | |
1542 | } | |
1543 | ||
08401c04 PX |
1544 | /* If there's an existing transport, release it */ |
1545 | migration_incoming_transport_cleanup(mis); | |
02affd41 PX |
1546 | |
1547 | /* | |
1548 | * Note that this call will never start a real migration; it will | |
1549 | * only re-setup the migration stream and poke existing migration | |
1550 | * to continue using that newly established channel. | |
1551 | */ | |
1552 | qemu_start_incoming_migration(uri, errp); | |
1553 | } | |
1554 | ||
bfbf89c2 PX |
1555 | void qmp_migrate_pause(Error **errp) |
1556 | { | |
1557 | MigrationState *ms = migrate_get_current(); | |
1558 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
1559 | int ret; | |
1560 | ||
1561 | if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { | |
1562 | /* Source side, during postcopy */ | |
1563 | qemu_mutex_lock(&ms->qemu_file_lock); | |
1564 | ret = qemu_file_shutdown(ms->to_dst_file); | |
1565 | qemu_mutex_unlock(&ms->qemu_file_lock); | |
1566 | if (ret) { | |
1567 | error_setg(errp, "Failed to pause source migration"); | |
1568 | } | |
1569 | return; | |
1570 | } | |
1571 | ||
1572 | if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { | |
1573 | ret = qemu_file_shutdown(mis->from_src_file); | |
1574 | if (ret) { | |
1575 | error_setg(errp, "Failed to pause destination migration"); | |
1576 | } | |
1577 | return; | |
1578 | } | |
1579 | ||
1580 | error_setg(errp, "migrate-pause is currently only supported " | |
1581 | "during postcopy-active state"); | |
1582 | } | |
1583 | ||
24f3902b GK |
1584 | bool migration_is_blocked(Error **errp) |
1585 | { | |
1586 | if (qemu_savevm_state_blocked(errp)) { | |
1587 | return true; | |
1588 | } | |
1589 | ||
1590 | if (migration_blockers) { | |
250561e1 | 1591 | error_propagate(errp, error_copy(migration_blockers->data)); |
24f3902b GK |
1592 | return true; |
1593 | } | |
1594 | ||
1595 | return false; | |
1596 | } | |
1597 | ||
d3e35b8f PX |
1598 | /* Returns true if continue to migrate, or false if error detected */ |
1599 | static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc, | |
1600 | bool resume, Error **errp) | |
cab30143 | 1601 | { |
be7059cd | 1602 | Error *local_err = NULL; |
d3e35b8f PX |
1603 | |
1604 | if (resume) { | |
1605 | if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) { | |
1606 | error_setg(errp, "Cannot resume if there is no " | |
1607 | "paused migration"); | |
1608 | return false; | |
1609 | } | |
97ca211c PX |
1610 | |
1611 | /* | |
1612 | * Postcopy recovery won't work well with release-ram | |
1613 | * capability since release-ram will drop the page buffer as | |
1614 | * long as the page is put into the send buffer. So if there | |
1615 | * is a network failure happened, any page buffers that have | |
1616 | * not yet reached the destination VM but have already been | |
1617 | * sent from the source VM will be lost forever. Let's refuse | |
1618 | * the client from resuming such a postcopy migration. | |
1619 | * Luckily release-ram was designed to only be used when src | |
1620 | * and destination VMs are on the same host, so it should be | |
1621 | * fine. | |
1622 | */ | |
1623 | if (migrate_release_ram()) { | |
1624 | error_setg(errp, "Postcopy recovery cannot work " | |
1625 | "when release-ram capability is set"); | |
1626 | return false; | |
1627 | } | |
1628 | ||
d3e35b8f PX |
1629 | /* This is a resume, skip init status */ |
1630 | return true; | |
1631 | } | |
cab30143 | 1632 | |
392d87e2 | 1633 | if (migration_is_running(s->state)) { |
c6bd8c70 | 1634 | error_setg(errp, QERR_MIGRATION_ACTIVE); |
d3e35b8f | 1635 | return false; |
cab30143 | 1636 | } |
d3e35b8f | 1637 | |
ca99993a DDAG |
1638 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
1639 | error_setg(errp, "Guest is waiting for an incoming migration"); | |
d3e35b8f | 1640 | return false; |
ca99993a DDAG |
1641 | } |
1642 | ||
36d0fe65 T |
1643 | if (runstate_check(RUN_STATE_POSTMIGRATE)) { |
1644 | error_setg(errp, "Can't migrate the vm that was paused due to " | |
1645 | "previous migration"); | |
1646 | return false; | |
1647 | } | |
1648 | ||
24f3902b | 1649 | if (migration_is_blocked(errp)) { |
d3e35b8f | 1650 | return false; |
fa2756b7 AL |
1651 | } |
1652 | ||
d3e35b8f | 1653 | if (blk || blk_inc) { |
5e804644 | 1654 | if (migrate_colo()) { |
3ba02445 RL |
1655 | error_setg(errp, "No disk migration is required in COLO mode"); |
1656 | return false; | |
1657 | } | |
6f8be708 | 1658 | if (migrate_block() || migrate_block_incremental()) { |
2833c59b JQ |
1659 | error_setg(errp, "Command options are incompatible with " |
1660 | "current migration capabilities"); | |
d3e35b8f | 1661 | return false; |
2833c59b | 1662 | } |
9eb1109c | 1663 | if (!migrate_cap_set(MIGRATION_CAPABILITY_BLOCK, true, &local_err)) { |
2833c59b | 1664 | error_propagate(errp, local_err); |
d3e35b8f | 1665 | return false; |
2833c59b JQ |
1666 | } |
1667 | s->must_remove_block_options = true; | |
1668 | } | |
1669 | ||
d3e35b8f | 1670 | if (blk_inc) { |
2833c59b JQ |
1671 | migrate_set_block_incremental(s, true); |
1672 | } | |
1673 | ||
3e0c8050 | 1674 | migrate_init(s); |
87f3bd87 | 1675 | /* |
fa0b31d5 | 1676 | * set ram_counters compression_counters memory to zero for a |
87f3bd87 IR |
1677 | * new migration |
1678 | */ | |
1679 | memset(&ram_counters, 0, sizeof(ram_counters)); | |
fa0b31d5 | 1680 | memset(&compression_counters, 0, sizeof(compression_counters)); |
cab30143 | 1681 | |
d3e35b8f PX |
1682 | return true; |
1683 | } | |
1684 | ||
1685 | void qmp_migrate(const char *uri, bool has_blk, bool blk, | |
1686 | bool has_inc, bool inc, bool has_detach, bool detach, | |
1687 | bool has_resume, bool resume, Error **errp) | |
1688 | { | |
1689 | Error *local_err = NULL; | |
1690 | MigrationState *s = migrate_get_current(); | |
d658f65c | 1691 | const char *p = NULL; |
d3e35b8f | 1692 | |
d6f74fd1 PX |
1693 | /* URI is not suitable for migration? */ |
1694 | if (!migration_channels_and_uri_compatible(uri, errp)) { | |
1695 | return; | |
1696 | } | |
1697 | ||
d3e35b8f PX |
1698 | if (!migrate_prepare(s, has_blk && blk, has_inc && inc, |
1699 | has_resume && resume, errp)) { | |
1700 | /* Error detected, put into errp */ | |
1701 | return; | |
1702 | } | |
1703 | ||
b5eea99e LS |
1704 | if (!(has_resume && resume)) { |
1705 | if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) { | |
1706 | return; | |
1707 | } | |
1708 | } | |
1709 | ||
d658f65c | 1710 | if (strstart(uri, "tcp:", &p) || |
9ba3b2ba LM |
1711 | strstart(uri, "unix:", NULL) || |
1712 | strstart(uri, "vsock:", NULL)) { | |
d658f65c | 1713 | socket_start_outgoing_migration(s, p ? p : uri, &local_err); |
2da776db | 1714 | #ifdef CONFIG_RDMA |
41310c68 | 1715 | } else if (strstart(uri, "rdma:", &p)) { |
2da776db MH |
1716 | rdma_start_outgoing_migration(s, p, &local_err); |
1717 | #endif | |
cab30143 | 1718 | } else if (strstart(uri, "exec:", &p)) { |
f37afb5a | 1719 | exec_start_outgoing_migration(s, p, &local_err); |
cab30143 | 1720 | } else if (strstart(uri, "fd:", &p)) { |
f37afb5a | 1721 | fd_start_outgoing_migration(s, p, &local_err); |
99a0db9b | 1722 | } else { |
b5eea99e LS |
1723 | if (!(has_resume && resume)) { |
1724 | yank_unregister_instance(MIGRATION_YANK_INSTANCE); | |
1725 | } | |
c6bd8c70 MA |
1726 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri", |
1727 | "a valid migration protocol"); | |
48781e5b HZ |
1728 | migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, |
1729 | MIGRATION_STATUS_FAILED); | |
09576e74 | 1730 | block_cleanup_parameters(s); |
e1c37d0e | 1731 | return; |
cab30143 JQ |
1732 | } |
1733 | ||
f37afb5a | 1734 | if (local_err) { |
b5eea99e LS |
1735 | if (!(has_resume && resume)) { |
1736 | yank_unregister_instance(MIGRATION_YANK_INSTANCE); | |
1737 | } | |
d59ce6f3 | 1738 | migrate_fd_error(s, local_err); |
f37afb5a | 1739 | error_propagate(errp, local_err); |
e1c37d0e | 1740 | return; |
1299c631 | 1741 | } |
cab30143 JQ |
1742 | } |
1743 | ||
6cdedb07 | 1744 | void qmp_migrate_cancel(Error **errp) |
cab30143 | 1745 | { |
458fecca | 1746 | migration_cancel(NULL); |
cab30143 JQ |
1747 | } |
1748 | ||
89cfc02c DDAG |
1749 | void qmp_migrate_continue(MigrationStatus state, Error **errp) |
1750 | { | |
1751 | MigrationState *s = migrate_get_current(); | |
1752 | if (s->state != state) { | |
1753 | error_setg(errp, "Migration not in expected state: %s", | |
1754 | MigrationStatus_str(s->state)); | |
1755 | return; | |
1756 | } | |
1757 | qemu_sem_post(&s->pause_sem); | |
1758 | } | |
1759 | ||
70b20477 DDAG |
1760 | /* migration thread support */ |
1761 | /* | |
1762 | * Something bad happened to the RP stream, mark an error | |
1763 | * The caller shall print or trace something to indicate why | |
1764 | */ | |
1765 | static void mark_source_rp_bad(MigrationState *s) | |
1766 | { | |
1767 | s->rp_state.error = true; | |
1768 | } | |
1769 | ||
1770 | static struct rp_cmd_args { | |
1771 | ssize_t len; /* -1 = variable */ | |
1772 | const char *name; | |
1773 | } rp_cmd_args[] = { | |
1774 | [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" }, | |
1775 | [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" }, | |
1776 | [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" }, | |
1e2d90eb DDAG |
1777 | [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" }, |
1778 | [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" }, | |
a335debb | 1779 | [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" }, |
13955b89 | 1780 | [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" }, |
70b20477 DDAG |
1781 | [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" }, |
1782 | }; | |
1783 | ||
1e2d90eb DDAG |
1784 | /* |
1785 | * Process a request for pages received on the return path, | |
1786 | * We're allowed to send more than requested (e.g. to round to our page size) | |
1787 | * and we don't need to send pages that have already been sent. | |
1788 | */ | |
1789 | static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname, | |
1790 | ram_addr_t start, size_t len) | |
1791 | { | |
8e3b0cbb | 1792 | long our_host_ps = qemu_real_host_page_size(); |
6c595cde | 1793 | |
1e2d90eb | 1794 | trace_migrate_handle_rp_req_pages(rbname, start, len); |
6c595cde DDAG |
1795 | |
1796 | /* | |
1797 | * Since we currently insist on matching page sizes, just sanity check | |
1798 | * we're being asked for whole host pages. | |
1799 | */ | |
7648297d DH |
1800 | if (!QEMU_IS_ALIGNED(start, our_host_ps) || |
1801 | !QEMU_IS_ALIGNED(len, our_host_ps)) { | |
6c595cde DDAG |
1802 | error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT |
1803 | " len: %zd", __func__, start, len); | |
1804 | mark_source_rp_bad(ms); | |
1805 | return; | |
1806 | } | |
1807 | ||
96506894 | 1808 | if (ram_save_queue_pages(rbname, start, len)) { |
6c595cde DDAG |
1809 | mark_source_rp_bad(ms); |
1810 | } | |
1e2d90eb DDAG |
1811 | } |
1812 | ||
14b1742e PX |
1813 | /* Return true to retry, false to quit */ |
1814 | static bool postcopy_pause_return_path_thread(MigrationState *s) | |
1815 | { | |
1816 | trace_postcopy_pause_return_path(); | |
1817 | ||
1818 | qemu_sem_wait(&s->postcopy_pause_rp_sem); | |
1819 | ||
1820 | trace_postcopy_pause_return_path_continued(); | |
1821 | ||
1822 | return true; | |
1823 | } | |
1824 | ||
a335debb PX |
1825 | static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name) |
1826 | { | |
1827 | RAMBlock *block = qemu_ram_block_by_name(block_name); | |
1828 | ||
1829 | if (!block) { | |
1830 | error_report("%s: invalid block name '%s'", __func__, block_name); | |
1831 | return -EINVAL; | |
1832 | } | |
1833 | ||
1834 | /* Fetch the received bitmap and refresh the dirty bitmap */ | |
1835 | return ram_dirty_bitmap_reload(s, block); | |
1836 | } | |
1837 | ||
13955b89 PX |
1838 | static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value) |
1839 | { | |
1840 | trace_source_return_path_thread_resume_ack(value); | |
1841 | ||
1842 | if (value != MIGRATION_RESUME_ACK_VALUE) { | |
1843 | error_report("%s: illegal resume_ack value %"PRIu32, | |
1844 | __func__, value); | |
1845 | return -1; | |
1846 | } | |
1847 | ||
1848 | /* Now both sides are active. */ | |
1849 | migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER, | |
1850 | MIGRATION_STATUS_POSTCOPY_ACTIVE); | |
1851 | ||
94190696 PX |
1852 | /* Notify send thread that time to continue send pages */ |
1853 | qemu_sem_post(&s->rp_state.rp_sem); | |
13955b89 PX |
1854 | |
1855 | return 0; | |
1856 | } | |
1857 | ||
93589827 PX |
1858 | /* |
1859 | * Release ms->rp_state.from_dst_file (and postcopy_qemufile_src if | |
1860 | * existed) in a safe way. | |
1861 | */ | |
1862 | static void migration_release_dst_files(MigrationState *ms) | |
43044ac0 PX |
1863 | { |
1864 | QEMUFile *file; | |
1865 | ||
1866 | WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) { | |
1867 | /* | |
1868 | * Reset the from_dst_file pointer first before releasing it, as we | |
1869 | * can't block within lock section | |
1870 | */ | |
1871 | file = ms->rp_state.from_dst_file; | |
1872 | ms->rp_state.from_dst_file = NULL; | |
1873 | } | |
1874 | ||
93589827 PX |
1875 | /* |
1876 | * Do the same to postcopy fast path socket too if there is. No | |
1877 | * locking needed because this qemufile should only be managed by | |
1878 | * return path thread. | |
1879 | */ | |
1880 | if (ms->postcopy_qemufile_src) { | |
1881 | migration_ioc_unregister_yank_from_file(ms->postcopy_qemufile_src); | |
1882 | qemu_file_shutdown(ms->postcopy_qemufile_src); | |
1883 | qemu_fclose(ms->postcopy_qemufile_src); | |
1884 | ms->postcopy_qemufile_src = NULL; | |
1885 | } | |
1886 | ||
43044ac0 PX |
1887 | qemu_fclose(file); |
1888 | } | |
1889 | ||
70b20477 DDAG |
1890 | /* |
1891 | * Handles messages sent on the return path towards the source VM | |
1892 | * | |
1893 | */ | |
1894 | static void *source_return_path_thread(void *opaque) | |
1895 | { | |
1896 | MigrationState *ms = opaque; | |
1897 | QEMUFile *rp = ms->rp_state.from_dst_file; | |
1898 | uint16_t header_len, header_type; | |
568b01ca | 1899 | uint8_t buf[512]; |
70b20477 | 1900 | uint32_t tmp32, sibling_error; |
1e2d90eb DDAG |
1901 | ram_addr_t start = 0; /* =0 to silence warning */ |
1902 | size_t len = 0, expected_len; | |
70b20477 DDAG |
1903 | int res; |
1904 | ||
1905 | trace_source_return_path_thread_entry(); | |
74637e6f | 1906 | rcu_register_thread(); |
14b1742e PX |
1907 | |
1908 | retry: | |
70b20477 DDAG |
1909 | while (!ms->rp_state.error && !qemu_file_get_error(rp) && |
1910 | migration_is_setup_or_active(ms->state)) { | |
1911 | trace_source_return_path_thread_loop_top(); | |
1912 | header_type = qemu_get_be16(rp); | |
1913 | header_len = qemu_get_be16(rp); | |
1914 | ||
7a9ddfbf PX |
1915 | if (qemu_file_get_error(rp)) { |
1916 | mark_source_rp_bad(ms); | |
1917 | goto out; | |
1918 | } | |
1919 | ||
70b20477 DDAG |
1920 | if (header_type >= MIG_RP_MSG_MAX || |
1921 | header_type == MIG_RP_MSG_INVALID) { | |
1922 | error_report("RP: Received invalid message 0x%04x length 0x%04x", | |
ed8b2828 | 1923 | header_type, header_len); |
70b20477 DDAG |
1924 | mark_source_rp_bad(ms); |
1925 | goto out; | |
1926 | } | |
1927 | ||
1928 | if ((rp_cmd_args[header_type].len != -1 && | |
1929 | header_len != rp_cmd_args[header_type].len) || | |
568b01ca | 1930 | header_len > sizeof(buf)) { |
70b20477 | 1931 | error_report("RP: Received '%s' message (0x%04x) with" |
ed8b2828 MZ |
1932 | "incorrect length %d expecting %zu", |
1933 | rp_cmd_args[header_type].name, header_type, header_len, | |
1934 | (size_t)rp_cmd_args[header_type].len); | |
70b20477 DDAG |
1935 | mark_source_rp_bad(ms); |
1936 | goto out; | |
1937 | } | |
1938 | ||
1939 | /* We know we've got a valid header by this point */ | |
1940 | res = qemu_get_buffer(rp, buf, header_len); | |
1941 | if (res != header_len) { | |
1942 | error_report("RP: Failed reading data for message 0x%04x" | |
1943 | " read %d expected %d", | |
1944 | header_type, res, header_len); | |
1945 | mark_source_rp_bad(ms); | |
1946 | goto out; | |
1947 | } | |
1948 | ||
1949 | /* OK, we have the message and the data */ | |
1950 | switch (header_type) { | |
1951 | case MIG_RP_MSG_SHUT: | |
4d885131 | 1952 | sibling_error = ldl_be_p(buf); |
70b20477 DDAG |
1953 | trace_source_return_path_thread_shut(sibling_error); |
1954 | if (sibling_error) { | |
1955 | error_report("RP: Sibling indicated error %d", sibling_error); | |
1956 | mark_source_rp_bad(ms); | |
1957 | } | |
1958 | /* | |
1959 | * We'll let the main thread deal with closing the RP | |
1960 | * we could do a shutdown(2) on it, but we're the only user | |
1961 | * anyway, so there's nothing gained. | |
1962 | */ | |
1963 | goto out; | |
1964 | ||
1965 | case MIG_RP_MSG_PONG: | |
4d885131 | 1966 | tmp32 = ldl_be_p(buf); |
70b20477 | 1967 | trace_source_return_path_thread_pong(tmp32); |
b28fb582 | 1968 | qemu_sem_post(&ms->rp_state.rp_pong_acks); |
70b20477 DDAG |
1969 | break; |
1970 | ||
1e2d90eb | 1971 | case MIG_RP_MSG_REQ_PAGES: |
4d885131 PM |
1972 | start = ldq_be_p(buf); |
1973 | len = ldl_be_p(buf + 8); | |
1e2d90eb DDAG |
1974 | migrate_handle_rp_req_pages(ms, NULL, start, len); |
1975 | break; | |
1976 | ||
1977 | case MIG_RP_MSG_REQ_PAGES_ID: | |
1978 | expected_len = 12 + 1; /* header + termination */ | |
1979 | ||
1980 | if (header_len >= expected_len) { | |
4d885131 PM |
1981 | start = ldq_be_p(buf); |
1982 | len = ldl_be_p(buf + 8); | |
1e2d90eb DDAG |
1983 | /* Now we expect an idstr */ |
1984 | tmp32 = buf[12]; /* Length of the following idstr */ | |
1985 | buf[13 + tmp32] = '\0'; | |
1986 | expected_len += tmp32; | |
1987 | } | |
1988 | if (header_len != expected_len) { | |
1989 | error_report("RP: Req_Page_id with length %d expecting %zd", | |
ed8b2828 | 1990 | header_len, expected_len); |
1e2d90eb DDAG |
1991 | mark_source_rp_bad(ms); |
1992 | goto out; | |
1993 | } | |
1994 | migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len); | |
1995 | break; | |
1996 | ||
a335debb PX |
1997 | case MIG_RP_MSG_RECV_BITMAP: |
1998 | if (header_len < 1) { | |
1999 | error_report("%s: missing block name", __func__); | |
2000 | mark_source_rp_bad(ms); | |
2001 | goto out; | |
2002 | } | |
2003 | /* Format: len (1B) + idstr (<255B). This ends the idstr. */ | |
2004 | buf[buf[0] + 1] = '\0'; | |
2005 | if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) { | |
2006 | mark_source_rp_bad(ms); | |
2007 | goto out; | |
2008 | } | |
2009 | break; | |
2010 | ||
13955b89 PX |
2011 | case MIG_RP_MSG_RESUME_ACK: |
2012 | tmp32 = ldl_be_p(buf); | |
2013 | if (migrate_handle_rp_resume_ack(ms, tmp32)) { | |
2014 | mark_source_rp_bad(ms); | |
2015 | goto out; | |
2016 | } | |
2017 | break; | |
2018 | ||
70b20477 DDAG |
2019 | default: |
2020 | break; | |
2021 | } | |
2022 | } | |
14b1742e PX |
2023 | |
2024 | out: | |
2025 | res = qemu_file_get_error(rp); | |
2026 | if (res) { | |
d5c8f2af | 2027 | if (res && migration_in_postcopy()) { |
14b1742e PX |
2028 | /* |
2029 | * Maybe there is something we can do: it looks like a | |
2030 | * network down issue, and we pause for a recovery. | |
2031 | */ | |
93589827 | 2032 | migration_release_dst_files(ms); |
2e3e3da3 | 2033 | rp = NULL; |
14b1742e | 2034 | if (postcopy_pause_return_path_thread(ms)) { |
43044ac0 PX |
2035 | /* |
2036 | * Reload rp, reset the rest. Referencing it is safe since | |
2037 | * it's reset only by us above, or when migration completes | |
2038 | */ | |
2e3e3da3 | 2039 | rp = ms->rp_state.from_dst_file; |
14b1742e PX |
2040 | ms->rp_state.error = false; |
2041 | goto retry; | |
2042 | } | |
2043 | } | |
2044 | ||
70b20477 DDAG |
2045 | trace_source_return_path_thread_bad_end(); |
2046 | mark_source_rp_bad(ms); | |
2047 | } | |
2048 | ||
2049 | trace_source_return_path_thread_end(); | |
93589827 | 2050 | migration_release_dst_files(ms); |
74637e6f | 2051 | rcu_unregister_thread(); |
70b20477 DDAG |
2052 | return NULL; |
2053 | } | |
2054 | ||
d3e35b8f PX |
2055 | static int open_return_path_on_source(MigrationState *ms, |
2056 | bool create_thread) | |
70b20477 | 2057 | { |
89a02a9f | 2058 | ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file); |
70b20477 DDAG |
2059 | if (!ms->rp_state.from_dst_file) { |
2060 | return -1; | |
2061 | } | |
2062 | ||
2063 | trace_open_return_path_on_source(); | |
d3e35b8f PX |
2064 | |
2065 | if (!create_thread) { | |
2066 | /* We're done */ | |
2067 | return 0; | |
2068 | } | |
2069 | ||
70b20477 DDAG |
2070 | qemu_thread_create(&ms->rp_state.rp_thread, "return path", |
2071 | source_return_path_thread, ms, QEMU_THREAD_JOINABLE); | |
53021ea1 | 2072 | ms->rp_state.rp_thread_created = true; |
70b20477 DDAG |
2073 | |
2074 | trace_open_return_path_on_source_continue(); | |
2075 | ||
2076 | return 0; | |
2077 | } | |
2078 | ||
70b20477 DDAG |
2079 | /* Returns 0 if the RP was ok, otherwise there was an error on the RP */ |
2080 | static int await_return_path_close_on_source(MigrationState *ms) | |
2081 | { | |
2082 | /* | |
2083 | * If this is a normal exit then the destination will send a SHUT and the | |
2084 | * rp_thread will exit, however if there's an error we need to cause | |
2085 | * it to exit. | |
2086 | */ | |
89a02a9f | 2087 | if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) { |
70b20477 DDAG |
2088 | /* |
2089 | * shutdown(2), if we have it, will cause it to unblock if it's stuck | |
2090 | * waiting for the destination. | |
2091 | */ | |
2092 | qemu_file_shutdown(ms->rp_state.from_dst_file); | |
2093 | mark_source_rp_bad(ms); | |
2094 | } | |
2095 | trace_await_return_path_close_on_source_joining(); | |
2096 | qemu_thread_join(&ms->rp_state.rp_thread); | |
53021ea1 | 2097 | ms->rp_state.rp_thread_created = false; |
70b20477 DDAG |
2098 | trace_await_return_path_close_on_source_close(); |
2099 | return ms->rp_state.error; | |
2100 | } | |
2101 | ||
5655aab0 PX |
2102 | static inline void |
2103 | migration_wait_main_channel(MigrationState *ms) | |
2104 | { | |
2105 | /* Wait until one PONG message received */ | |
2106 | qemu_sem_wait(&ms->rp_state.rp_pong_acks); | |
2107 | } | |
2108 | ||
1d34e4bf DDAG |
2109 | /* |
2110 | * Switch from normal iteration to postcopy | |
2111 | * Returns non-0 on error | |
2112 | */ | |
7287cbd4 | 2113 | static int postcopy_start(MigrationState *ms) |
1d34e4bf DDAG |
2114 | { |
2115 | int ret; | |
61b67d47 DB |
2116 | QIOChannelBuffer *bioc; |
2117 | QEMUFile *fb; | |
1d34e4bf | 2118 | int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
7e555c6c | 2119 | int64_t bandwidth = migrate_max_postcopy_bandwidth(); |
ef8d6488 | 2120 | bool restart_block = false; |
0331c8ca | 2121 | int cur_state = MIGRATION_STATUS_ACTIVE; |
d0edb8a1 | 2122 | |
5655aab0 PX |
2123 | if (migrate_postcopy_preempt()) { |
2124 | migration_wait_main_channel(ms); | |
2125 | if (postcopy_preempt_establish_channel(ms)) { | |
2126 | migrate_set_state(&ms->state, ms->state, MIGRATION_STATUS_FAILED); | |
2127 | return -1; | |
2128 | } | |
d0edb8a1 PX |
2129 | } |
2130 | ||
0331c8ca DDAG |
2131 | if (!migrate_pause_before_switchover()) { |
2132 | migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE, | |
2133 | MIGRATION_STATUS_POSTCOPY_ACTIVE); | |
2134 | } | |
1d34e4bf DDAG |
2135 | |
2136 | trace_postcopy_start(); | |
2137 | qemu_mutex_lock_iothread(); | |
2138 | trace_postcopy_start_set_run(); | |
2139 | ||
fb064112 | 2140 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL); |
1d34e4bf DDAG |
2141 | global_state_store(); |
2142 | ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); | |
76b1c7fe KW |
2143 | if (ret < 0) { |
2144 | goto fail; | |
2145 | } | |
1d34e4bf | 2146 | |
0331c8ca DDAG |
2147 | ret = migration_maybe_pause(ms, &cur_state, |
2148 | MIGRATION_STATUS_POSTCOPY_ACTIVE); | |
2149 | if (ret < 0) { | |
2150 | goto fail; | |
2151 | } | |
2152 | ||
76b1c7fe | 2153 | ret = bdrv_inactivate_all(); |
1d34e4bf DDAG |
2154 | if (ret < 0) { |
2155 | goto fail; | |
2156 | } | |
ef8d6488 | 2157 | restart_block = true; |
1d34e4bf | 2158 | |
1c0d249d DDAG |
2159 | /* |
2160 | * Cause any non-postcopiable, but iterative devices to | |
2161 | * send out their final data. | |
2162 | */ | |
a1fbe750 | 2163 | qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false); |
1c0d249d | 2164 | |
1d34e4bf DDAG |
2165 | /* |
2166 | * in Finish migrate and with the io-lock held everything should | |
2167 | * be quiet, but we've potentially still got dirty pages and we | |
2168 | * need to tell the destination to throw any pages it's already received | |
2169 | * that are dirty | |
2170 | */ | |
58110f0a | 2171 | if (migrate_postcopy_ram()) { |
739fcc1b | 2172 | ram_postcopy_send_discard_bitmap(ms); |
1d34e4bf DDAG |
2173 | } |
2174 | ||
2175 | /* | |
2176 | * send rest of state - note things that are doing postcopy | |
2177 | * will notice we're in POSTCOPY_ACTIVE and not actually | |
2178 | * wrap their state up here | |
2179 | */ | |
7e555c6c DDAG |
2180 | /* 0 max-postcopy-bandwidth means unlimited */ |
2181 | if (!bandwidth) { | |
2182 | qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX); | |
2183 | } else { | |
2184 | qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO); | |
2185 | } | |
58110f0a VSO |
2186 | if (migrate_postcopy_ram()) { |
2187 | /* Ping just for debugging, helps line traces up */ | |
2188 | qemu_savevm_send_ping(ms->to_dst_file, 2); | |
2189 | } | |
1d34e4bf DDAG |
2190 | |
2191 | /* | |
2192 | * While loading the device state we may trigger page transfer | |
2193 | * requests and the fd must be free to process those, and thus | |
2194 | * the destination must read the whole device state off the fd before | |
2195 | * it starts processing it. Unfortunately the ad-hoc migration format | |
2196 | * doesn't allow the destination to know the size to read without fully | |
2197 | * parsing it through each devices load-state code (especially the open | |
2198 | * coded devices that use get/put). | |
2199 | * So we wrap the device state up in a package with a length at the start; | |
2200 | * to do this we use a qemu_buf to hold the whole of the device state. | |
2201 | */ | |
61b67d47 | 2202 | bioc = qio_channel_buffer_new(4096); |
6f01f136 | 2203 | qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer"); |
77ef2dc1 | 2204 | fb = qemu_file_new_output(QIO_CHANNEL(bioc)); |
61b67d47 | 2205 | object_unref(OBJECT(bioc)); |
1d34e4bf | 2206 | |
c76201ab DDAG |
2207 | /* |
2208 | * Make sure the receiver can get incoming pages before we send the rest | |
2209 | * of the state | |
2210 | */ | |
2211 | qemu_savevm_send_postcopy_listen(fb); | |
2212 | ||
a1fbe750 | 2213 | qemu_savevm_state_complete_precopy(fb, false, false); |
58110f0a VSO |
2214 | if (migrate_postcopy_ram()) { |
2215 | qemu_savevm_send_ping(fb, 3); | |
2216 | } | |
1d34e4bf DDAG |
2217 | |
2218 | qemu_savevm_send_postcopy_run(fb); | |
2219 | ||
2220 | /* <><> end of stuff going into the package */ | |
1d34e4bf | 2221 | |
ef8d6488 DDAG |
2222 | /* Last point of recovery; as soon as we send the package the destination |
2223 | * can open devices and potentially start running. | |
2224 | * Lets just check again we've not got any errors. | |
2225 | */ | |
2226 | ret = qemu_file_get_error(ms->to_dst_file); | |
2227 | if (ret) { | |
2228 | error_report("postcopy_start: Migration stream errored (pre package)"); | |
2229 | goto fail_closefb; | |
2230 | } | |
2231 | ||
2232 | restart_block = false; | |
2233 | ||
1d34e4bf | 2234 | /* Now send that blob */ |
61b67d47 | 2235 | if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) { |
1d34e4bf DDAG |
2236 | goto fail_closefb; |
2237 | } | |
2238 | qemu_fclose(fb); | |
b82fc321 DDAG |
2239 | |
2240 | /* Send a notify to give a chance for anything that needs to happen | |
2241 | * at the transition to postcopy and after the device state; in particular | |
2242 | * spice needs to trigger a transition now | |
2243 | */ | |
2244 | ms->postcopy_after_devices = true; | |
2245 | notifier_list_notify(&migration_state_notifiers, ms); | |
2246 | ||
1d34e4bf DDAG |
2247 | ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop; |
2248 | ||
2249 | qemu_mutex_unlock_iothread(); | |
2250 | ||
58110f0a VSO |
2251 | if (migrate_postcopy_ram()) { |
2252 | /* | |
2253 | * Although this ping is just for debug, it could potentially be | |
2254 | * used for getting a better measurement of downtime at the source. | |
2255 | */ | |
2256 | qemu_savevm_send_ping(ms->to_dst_file, 4); | |
2257 | } | |
1d34e4bf | 2258 | |
ced1c616 PB |
2259 | if (migrate_release_ram()) { |
2260 | ram_postcopy_migrated_memory_release(ms); | |
2261 | } | |
2262 | ||
89a02a9f | 2263 | ret = qemu_file_get_error(ms->to_dst_file); |
1d34e4bf DDAG |
2264 | if (ret) { |
2265 | error_report("postcopy_start: Migration stream errored"); | |
48781e5b | 2266 | migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, |
1d34e4bf DDAG |
2267 | MIGRATION_STATUS_FAILED); |
2268 | } | |
2269 | ||
c01b16ed PX |
2270 | trace_postcopy_preempt_enabled(migrate_postcopy_preempt()); |
2271 | ||
1d34e4bf DDAG |
2272 | return ret; |
2273 | ||
2274 | fail_closefb: | |
2275 | qemu_fclose(fb); | |
2276 | fail: | |
48781e5b | 2277 | migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, |
1d34e4bf | 2278 | MIGRATION_STATUS_FAILED); |
ef8d6488 DDAG |
2279 | if (restart_block) { |
2280 | /* A failure happened early enough that we know the destination hasn't | |
2281 | * accessed block devices, so we're safe to recover. | |
2282 | */ | |
2283 | Error *local_err = NULL; | |
2284 | ||
3b717194 | 2285 | bdrv_activate_all(&local_err); |
ef8d6488 DDAG |
2286 | if (local_err) { |
2287 | error_report_err(local_err); | |
2288 | } | |
2289 | } | |
1d34e4bf DDAG |
2290 | qemu_mutex_unlock_iothread(); |
2291 | return -1; | |
2292 | } | |
2293 | ||
e91d8951 DDAG |
2294 | /** |
2295 | * migration_maybe_pause: Pause if required to by | |
2296 | * migrate_pause_before_switchover called with the iothread locked | |
2297 | * Returns: 0 on success | |
2298 | */ | |
0331c8ca DDAG |
2299 | static int migration_maybe_pause(MigrationState *s, |
2300 | int *current_active_state, | |
2301 | int new_state) | |
e91d8951 DDAG |
2302 | { |
2303 | if (!migrate_pause_before_switchover()) { | |
2304 | return 0; | |
2305 | } | |
2306 | ||
2307 | /* Since leaving this state is not atomic with posting the semaphore | |
2308 | * it's possible that someone could have issued multiple migrate_continue | |
2309 | * and the semaphore is incorrectly positive at this point; | |
2310 | * the docs say it's undefined to reinit a semaphore that's already | |
2311 | * init'd, so use timedwait to eat up any existing posts. | |
2312 | */ | |
2313 | while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) { | |
2314 | /* This block intentionally left blank */ | |
2315 | } | |
2316 | ||
8958338b ZF |
2317 | /* |
2318 | * If the migration is cancelled when it is in the completion phase, | |
2319 | * the migration state is set to MIGRATION_STATUS_CANCELLING. | |
2320 | * So we don't need to wait a semaphore, otherwise we would always | |
2321 | * wait for the 'pause_sem' semaphore. | |
2322 | */ | |
2323 | if (s->state != MIGRATION_STATUS_CANCELLING) { | |
2324 | qemu_mutex_unlock_iothread(); | |
2325 | migrate_set_state(&s->state, *current_active_state, | |
2326 | MIGRATION_STATUS_PRE_SWITCHOVER); | |
2327 | qemu_sem_wait(&s->pause_sem); | |
2328 | migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER, | |
2329 | new_state); | |
2330 | *current_active_state = new_state; | |
2331 | qemu_mutex_lock_iothread(); | |
2332 | } | |
e91d8951 | 2333 | |
0331c8ca | 2334 | return s->state == new_state ? 0 : -EINVAL; |
e91d8951 DDAG |
2335 | } |
2336 | ||
09f6c85e DDAG |
2337 | /** |
2338 | * migration_completion: Used by migration_thread when there's not much left. | |
2339 | * The caller 'breaks' the loop when this returns. | |
2340 | * | |
2341 | * @s: Current migration state | |
09f6c85e | 2342 | */ |
2ad87305 | 2343 | static void migration_completion(MigrationState *s) |
09f6c85e DDAG |
2344 | { |
2345 | int ret; | |
2ad87305 | 2346 | int current_active_state = s->state; |
09f6c85e | 2347 | |
b10ac0c4 DDAG |
2348 | if (s->state == MIGRATION_STATUS_ACTIVE) { |
2349 | qemu_mutex_lock_iothread(); | |
64909f97 | 2350 | s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
fb064112 | 2351 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL); |
7287cbd4 | 2352 | s->vm_was_running = runstate_is_running(); |
b10ac0c4 DDAG |
2353 | ret = global_state_store(); |
2354 | ||
2355 | if (!ret) { | |
2356 | ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); | |
626ff651 | 2357 | trace_migration_completion_vm_stop(ret); |
e91d8951 | 2358 | if (ret >= 0) { |
0331c8ca DDAG |
2359 | ret = migration_maybe_pause(s, ¤t_active_state, |
2360 | MIGRATION_STATUS_DEVICE); | |
e91d8951 | 2361 | } |
f07fa4cb | 2362 | if (ret >= 0) { |
5e804644 | 2363 | s->block_inactive = !migrate_colo(); |
f07fa4cb | 2364 | qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX); |
a1fbe750 | 2365 | ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false, |
5d39f44d | 2366 | s->block_inactive); |
f07fa4cb | 2367 | } |
b10ac0c4 DDAG |
2368 | } |
2369 | qemu_mutex_unlock_iothread(); | |
09f6c85e | 2370 | |
b10ac0c4 DDAG |
2371 | if (ret < 0) { |
2372 | goto fail; | |
09f6c85e | 2373 | } |
b10ac0c4 DDAG |
2374 | } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { |
2375 | trace_migration_completion_postcopy_end(); | |
2376 | ||
68b88468 | 2377 | qemu_mutex_lock_iothread(); |
89a02a9f | 2378 | qemu_savevm_state_complete_postcopy(s->to_dst_file); |
68b88468 EGE |
2379 | qemu_mutex_unlock_iothread(); |
2380 | ||
6621883f PX |
2381 | /* |
2382 | * Shutdown the postcopy fast path thread. This is only needed | |
2383 | * when dest QEMU binary is old (7.1/7.2). QEMU 8.0+ doesn't need | |
2384 | * this. | |
2385 | */ | |
2386 | if (migrate_postcopy_preempt() && s->preempt_pre_7_2) { | |
36f62f11 PX |
2387 | postcopy_preempt_shutdown_file(s); |
2388 | } | |
2389 | ||
b10ac0c4 | 2390 | trace_migration_completion_postcopy_end_after_complete(); |
444252b9 | 2391 | } else { |
6ba11211 | 2392 | goto fail; |
09f6c85e | 2393 | } |
09f6c85e | 2394 | |
b10ac0c4 DDAG |
2395 | /* |
2396 | * If rp was opened we must clean up the thread before | |
2397 | * cleaning everything else up (since if there are no failures | |
2398 | * it will wait for the destination to send it's status in | |
2399 | * a SHUT command). | |
b10ac0c4 | 2400 | */ |
53021ea1 | 2401 | if (s->rp_state.rp_thread_created) { |
b10ac0c4 | 2402 | int rp_error; |
0425dc97 | 2403 | trace_migration_return_path_end_before(); |
b10ac0c4 | 2404 | rp_error = await_return_path_close_on_source(s); |
0425dc97 | 2405 | trace_migration_return_path_end_after(rp_error); |
b10ac0c4 | 2406 | if (rp_error) { |
fe904ea8 | 2407 | goto fail_invalidate; |
b10ac0c4 | 2408 | } |
09f6c85e DDAG |
2409 | } |
2410 | ||
89a02a9f | 2411 | if (qemu_file_get_error(s->to_dst_file)) { |
09f6c85e | 2412 | trace_migration_completion_file_err(); |
fe904ea8 | 2413 | goto fail_invalidate; |
09f6c85e DDAG |
2414 | } |
2415 | ||
5e804644 | 2416 | if (migrate_colo() && s->state == MIGRATION_STATUS_ACTIVE) { |
eeeb48ee ZC |
2417 | /* COLO does not support postcopy */ |
2418 | migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE, | |
2419 | MIGRATION_STATUS_COLO); | |
2420 | } else { | |
0b827d5e HZ |
2421 | migrate_set_state(&s->state, current_active_state, |
2422 | MIGRATION_STATUS_COMPLETED); | |
2423 | } | |
2424 | ||
09f6c85e DDAG |
2425 | return; |
2426 | ||
fe904ea8 GK |
2427 | fail_invalidate: |
2428 | /* If not doing postcopy, vm_start() will be called: let's regain | |
2429 | * control on images. | |
2430 | */ | |
6039dd5b DDAG |
2431 | if (s->state == MIGRATION_STATUS_ACTIVE || |
2432 | s->state == MIGRATION_STATUS_DEVICE) { | |
fe904ea8 GK |
2433 | Error *local_err = NULL; |
2434 | ||
1d2acc31 | 2435 | qemu_mutex_lock_iothread(); |
3b717194 | 2436 | bdrv_activate_all(&local_err); |
fe904ea8 GK |
2437 | if (local_err) { |
2438 | error_report_err(local_err); | |
403d18ae | 2439 | s->block_inactive = true; |
1d2acc31 HZ |
2440 | } else { |
2441 | s->block_inactive = false; | |
fe904ea8 | 2442 | } |
1d2acc31 | 2443 | qemu_mutex_unlock_iothread(); |
fe904ea8 GK |
2444 | } |
2445 | ||
09f6c85e | 2446 | fail: |
48781e5b HZ |
2447 | migrate_set_state(&s->state, current_active_state, |
2448 | MIGRATION_STATUS_FAILED); | |
09f6c85e DDAG |
2449 | } |
2450 | ||
8518278a AG |
2451 | /** |
2452 | * bg_migration_completion: Used by bg_migration_thread when after all the | |
2453 | * RAM has been saved. The caller 'breaks' the loop when this returns. | |
2454 | * | |
2455 | * @s: Current migration state | |
2456 | */ | |
2457 | static void bg_migration_completion(MigrationState *s) | |
2458 | { | |
2459 | int current_active_state = s->state; | |
2460 | ||
2461 | /* | |
2462 | * Stop tracking RAM writes - un-protect memory, un-register UFFD | |
2463 | * memory ranges, flush kernel wait queues and wake up threads | |
2464 | * waiting for write fault to be resolved. | |
2465 | */ | |
2466 | ram_write_tracking_stop(); | |
2467 | ||
2468 | if (s->state == MIGRATION_STATUS_ACTIVE) { | |
2469 | /* | |
2470 | * By this moment we have RAM content saved into the migration stream. | |
2471 | * The next step is to flush the non-RAM content (device state) | |
2472 | * right after the ram content. The device state has been stored into | |
2473 | * the temporary buffer before RAM saving started. | |
2474 | */ | |
2475 | qemu_put_buffer(s->to_dst_file, s->bioc->data, s->bioc->usage); | |
2476 | qemu_fflush(s->to_dst_file); | |
2477 | } else if (s->state == MIGRATION_STATUS_CANCELLING) { | |
2478 | goto fail; | |
2479 | } | |
2480 | ||
2481 | if (qemu_file_get_error(s->to_dst_file)) { | |
2482 | trace_migration_completion_file_err(); | |
2483 | goto fail; | |
2484 | } | |
2485 | ||
2486 | migrate_set_state(&s->state, current_active_state, | |
2487 | MIGRATION_STATUS_COMPLETED); | |
2488 | return; | |
2489 | ||
2490 | fail: | |
2491 | migrate_set_state(&s->state, current_active_state, | |
2492 | MIGRATION_STATUS_FAILED); | |
2493 | } | |
2494 | ||
b23c2ade PX |
2495 | typedef enum MigThrError { |
2496 | /* No error detected */ | |
2497 | MIG_THR_ERR_NONE = 0, | |
2498 | /* Detected error, but resumed successfully */ | |
2499 | MIG_THR_ERR_RECOVERED = 1, | |
2500 | /* Detected fatal error, need to exit */ | |
2501 | MIG_THR_ERR_FATAL = 2, | |
2502 | } MigThrError; | |
2503 | ||
94190696 PX |
2504 | static int postcopy_resume_handshake(MigrationState *s) |
2505 | { | |
2506 | qemu_savevm_send_postcopy_resume(s->to_dst_file); | |
2507 | ||
2508 | while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) { | |
2509 | qemu_sem_wait(&s->rp_state.rp_sem); | |
2510 | } | |
2511 | ||
2512 | if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { | |
2513 | return 0; | |
2514 | } | |
2515 | ||
2516 | return -1; | |
2517 | } | |
2518 | ||
135b87b4 PX |
2519 | /* Return zero if success, or <0 for error */ |
2520 | static int postcopy_do_resume(MigrationState *s) | |
2521 | { | |
d1b8eadb PX |
2522 | int ret; |
2523 | ||
2524 | /* | |
2525 | * Call all the resume_prepare() hooks, so that modules can be | |
2526 | * ready for the migration resume. | |
2527 | */ | |
2528 | ret = qemu_savevm_state_resume_prepare(s); | |
2529 | if (ret) { | |
2530 | error_report("%s: resume_prepare() failure detected: %d", | |
2531 | __func__, ret); | |
2532 | return ret; | |
2533 | } | |
2534 | ||
5655aab0 PX |
2535 | /* |
2536 | * If preempt is enabled, re-establish the preempt channel. Note that | |
2537 | * we do it after resume prepare to make sure the main channel will be | |
2538 | * created before the preempt channel. E.g. with weak network, the | |
2539 | * dest QEMU may get messed up with the preempt and main channels on | |
2540 | * the order of connection setup. This guarantees the correct order. | |
2541 | */ | |
2542 | ret = postcopy_preempt_establish_channel(s); | |
2543 | if (ret) { | |
2544 | error_report("%s: postcopy_preempt_establish_channel(): %d", | |
2545 | __func__, ret); | |
2546 | return ret; | |
2547 | } | |
2548 | ||
d1b8eadb | 2549 | /* |
94190696 PX |
2550 | * Last handshake with destination on the resume (destination will |
2551 | * switch to postcopy-active afterwards) | |
d1b8eadb | 2552 | */ |
94190696 PX |
2553 | ret = postcopy_resume_handshake(s); |
2554 | if (ret) { | |
2555 | error_report("%s: handshake failed: %d", __func__, ret); | |
2556 | return ret; | |
2557 | } | |
d1b8eadb | 2558 | |
135b87b4 PX |
2559 | return 0; |
2560 | } | |
2561 | ||
b23c2ade PX |
2562 | /* |
2563 | * We don't return until we are in a safe state to continue current | |
2564 | * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or | |
2565 | * MIG_THR_ERR_FATAL if unrecovery failure happened. | |
2566 | */ | |
2567 | static MigThrError postcopy_pause(MigrationState *s) | |
2568 | { | |
2569 | assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE); | |
b23c2ade | 2570 | |
135b87b4 | 2571 | while (true) { |
62df066f PX |
2572 | QEMUFile *file; |
2573 | ||
39675fff PX |
2574 | /* |
2575 | * Current channel is possibly broken. Release it. Note that this is | |
2576 | * guaranteed even without lock because to_dst_file should only be | |
2577 | * modified by the migration thread. That also guarantees that the | |
2578 | * unregister of yank is safe too without the lock. It should be safe | |
2579 | * even to be within the qemu_file_lock, but we didn't do that to avoid | |
2580 | * taking more mutex (yank_lock) within qemu_file_lock. TL;DR: we make | |
2581 | * the qemu_file_lock critical section as small as possible. | |
2582 | */ | |
135b87b4 | 2583 | assert(s->to_dst_file); |
39675fff | 2584 | migration_ioc_unregister_yank_from_file(s->to_dst_file); |
62df066f PX |
2585 | qemu_mutex_lock(&s->qemu_file_lock); |
2586 | file = s->to_dst_file; | |
135b87b4 | 2587 | s->to_dst_file = NULL; |
62df066f PX |
2588 | qemu_mutex_unlock(&s->qemu_file_lock); |
2589 | ||
2590 | qemu_file_shutdown(file); | |
2591 | qemu_fclose(file); | |
b23c2ade | 2592 | |
d246ea50 PX |
2593 | migrate_set_state(&s->state, s->state, |
2594 | MIGRATION_STATUS_POSTCOPY_PAUSED); | |
2595 | ||
135b87b4 PX |
2596 | error_report("Detected IO failure for postcopy. " |
2597 | "Migration paused."); | |
b23c2ade | 2598 | |
135b87b4 PX |
2599 | /* |
2600 | * We wait until things fixed up. Then someone will setup the | |
2601 | * status back for us. | |
2602 | */ | |
2603 | while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) { | |
2604 | qemu_sem_wait(&s->postcopy_pause_sem); | |
2605 | } | |
2606 | ||
2607 | if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) { | |
2608 | /* Woken up by a recover procedure. Give it a shot */ | |
b23c2ade | 2609 | |
135b87b4 PX |
2610 | /* |
2611 | * Firstly, let's wake up the return path now, with a new | |
2612 | * return path channel. | |
2613 | */ | |
2614 | qemu_sem_post(&s->postcopy_pause_rp_sem); | |
2615 | ||
2616 | /* Do the resume logic */ | |
2617 | if (postcopy_do_resume(s) == 0) { | |
2618 | /* Let's continue! */ | |
2619 | trace_postcopy_pause_continued(); | |
2620 | return MIG_THR_ERR_RECOVERED; | |
2621 | } else { | |
2622 | /* | |
2623 | * Something wrong happened during the recovery, let's | |
2624 | * pause again. Pause is always better than throwing | |
2625 | * data away. | |
2626 | */ | |
2627 | continue; | |
2628 | } | |
2629 | } else { | |
2630 | /* This is not right... Time to quit. */ | |
2631 | return MIG_THR_ERR_FATAL; | |
2632 | } | |
2633 | } | |
b23c2ade PX |
2634 | } |
2635 | ||
2636 | static MigThrError migration_detect_error(MigrationState *s) | |
2637 | { | |
2638 | int ret; | |
c3c5eae6 | 2639 | int state = s->state; |
3d661c8a | 2640 | Error *local_error = NULL; |
c3c5eae6 DDAG |
2641 | |
2642 | if (state == MIGRATION_STATUS_CANCELLING || | |
2643 | state == MIGRATION_STATUS_CANCELLED) { | |
2644 | /* End the migration, but don't set the state to failed */ | |
2645 | return MIG_THR_ERR_FATAL; | |
2646 | } | |
b23c2ade | 2647 | |
60bb3c58 PX |
2648 | /* |
2649 | * Try to detect any file errors. Note that postcopy_qemufile_src will | |
2650 | * be NULL when postcopy preempt is not enabled. | |
2651 | */ | |
2652 | ret = qemu_file_get_error_obj_any(s->to_dst_file, | |
2653 | s->postcopy_qemufile_src, | |
2654 | &local_error); | |
b23c2ade PX |
2655 | if (!ret) { |
2656 | /* Everything is fine */ | |
3d661c8a | 2657 | assert(!local_error); |
b23c2ade PX |
2658 | return MIG_THR_ERR_NONE; |
2659 | } | |
2660 | ||
3d661c8a YK |
2661 | if (local_error) { |
2662 | migrate_set_error(s, local_error); | |
2663 | error_free(local_error); | |
2664 | } | |
2665 | ||
d5c8f2af | 2666 | if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret) { |
b23c2ade PX |
2667 | /* |
2668 | * For postcopy, we allow the network to be down for a | |
2669 | * while. After that, it can be continued by a | |
2670 | * recovery phase. | |
2671 | */ | |
2672 | return postcopy_pause(s); | |
2673 | } else { | |
2674 | /* | |
2675 | * For precopy (or postcopy with error outside IO), we fail | |
2676 | * with no time. | |
2677 | */ | |
c3c5eae6 | 2678 | migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED); |
b23c2ade PX |
2679 | trace_migration_thread_file_err(); |
2680 | ||
2681 | /* Time to stop the migration, now. */ | |
2682 | return MIG_THR_ERR_FATAL; | |
2683 | } | |
2684 | } | |
2685 | ||
268dcd46 | 2686 | /* How many bytes have we transferred since the beginning of the migration */ |
0c8f0efd JQ |
2687 | static uint64_t migration_total_bytes(MigrationState *s) |
2688 | { | |
fbfa6404 | 2689 | return qemu_file_total_transferred(s->to_dst_file) + |
cf671116 | 2690 | stat64_get(&ram_counters.multifd_bytes); |
0c8f0efd JQ |
2691 | } |
2692 | ||
cf011f08 PX |
2693 | static void migration_calculate_complete(MigrationState *s) |
2694 | { | |
0c8f0efd | 2695 | uint64_t bytes = migration_total_bytes(s); |
cf011f08 | 2696 | int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
6cde6fbe | 2697 | int64_t transfer_time; |
cf011f08 PX |
2698 | |
2699 | s->total_time = end_time - s->start_time; | |
2700 | if (!s->downtime) { | |
2701 | /* | |
2702 | * It's still not set, so we are precopy migration. For | |
2703 | * postcopy, downtime is calculated during postcopy_start(). | |
2704 | */ | |
2705 | s->downtime = end_time - s->downtime_start; | |
2706 | } | |
2707 | ||
6cde6fbe JQ |
2708 | transfer_time = s->total_time - s->setup_time; |
2709 | if (transfer_time) { | |
2710 | s->mbps = ((double) bytes * 8.0) / transfer_time / 1000; | |
cf011f08 PX |
2711 | } |
2712 | } | |
2713 | ||
87f3bd87 IR |
2714 | static void update_iteration_initial_status(MigrationState *s) |
2715 | { | |
2716 | /* | |
2717 | * Update these three fields at the same time to avoid mismatch info lead | |
2718 | * wrong speed calculation. | |
2719 | */ | |
2720 | s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); | |
2721 | s->iteration_initial_bytes = migration_total_bytes(s); | |
2722 | s->iteration_initial_pages = ram_get_total_transferred_pages(); | |
2723 | } | |
2724 | ||
b15df1ae PX |
2725 | static void migration_update_counters(MigrationState *s, |
2726 | int64_t current_time) | |
2727 | { | |
aecbfe9c | 2728 | uint64_t transferred, transferred_pages, time_spent; |
0c8f0efd | 2729 | uint64_t current_bytes; /* bytes transferred since the beginning */ |
b15df1ae PX |
2730 | double bandwidth; |
2731 | ||
2732 | if (current_time < s->iteration_start_time + BUFFER_DELAY) { | |
2733 | return; | |
2734 | } | |
2735 | ||
0c8f0efd JQ |
2736 | current_bytes = migration_total_bytes(s); |
2737 | transferred = current_bytes - s->iteration_initial_bytes; | |
b15df1ae PX |
2738 | time_spent = current_time - s->iteration_start_time; |
2739 | bandwidth = (double)transferred / time_spent; | |
0781c1ed | 2740 | s->threshold_size = bandwidth * s->parameters.downtime_limit; |
b15df1ae PX |
2741 | |
2742 | s->mbps = (((double) transferred * 8.0) / | |
2743 | ((double) time_spent / 1000.0)) / 1000.0 / 1000.0; | |
2744 | ||
aecbfe9c XG |
2745 | transferred_pages = ram_get_total_transferred_pages() - |
2746 | s->iteration_initial_pages; | |
2747 | s->pages_per_second = (double) transferred_pages / | |
2748 | (((double) time_spent / 1000.0)); | |
2749 | ||
b15df1ae PX |
2750 | /* |
2751 | * if we haven't sent anything, we don't want to | |
2752 | * recalculate. 10000 is a small enough number for our purposes | |
2753 | */ | |
2754 | if (ram_counters.dirty_pages_rate && transferred > 10000) { | |
650af890 | 2755 | s->expected_downtime = ram_counters.remaining / bandwidth; |
b15df1ae PX |
2756 | } |
2757 | ||
2758 | qemu_file_reset_rate_limit(s->to_dst_file); | |
2759 | ||
87f3bd87 | 2760 | update_iteration_initial_status(s); |
b15df1ae PX |
2761 | |
2762 | trace_migrate_transferred(transferred, time_spent, | |
0781c1ed | 2763 | bandwidth, s->threshold_size); |
b15df1ae PX |
2764 | } |
2765 | ||
2ad87305 PX |
2766 | /* Migration thread iteration status */ |
2767 | typedef enum { | |
2768 | MIG_ITERATE_RESUME, /* Resume current iteration */ | |
2769 | MIG_ITERATE_SKIP, /* Skip current iteration */ | |
2770 | MIG_ITERATE_BREAK, /* Break the loop */ | |
2771 | } MigIterateState; | |
2772 | ||
2773 | /* | |
2774 | * Return true if continue to the next iteration directly, false | |
2775 | * otherwise. | |
2776 | */ | |
2777 | static MigIterateState migration_iteration_run(MigrationState *s) | |
2778 | { | |
24beea4e | 2779 | uint64_t must_precopy, can_postcopy; |
2ad87305 PX |
2780 | bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE; |
2781 | ||
24beea4e JQ |
2782 | qemu_savevm_state_pending_estimate(&must_precopy, &can_postcopy); |
2783 | uint64_t pending_size = must_precopy + can_postcopy; | |
2ad87305 | 2784 | |
24beea4e | 2785 | trace_migrate_pending_estimate(pending_size, must_precopy, can_postcopy); |
c8df4a7a | 2786 | |
24beea4e JQ |
2787 | if (must_precopy <= s->threshold_size) { |
2788 | qemu_savevm_state_pending_exact(&must_precopy, &can_postcopy); | |
2789 | pending_size = must_precopy + can_postcopy; | |
2790 | trace_migrate_pending_exact(pending_size, must_precopy, can_postcopy); | |
c8df4a7a | 2791 | } |
2ad87305 | 2792 | |
d9df9292 | 2793 | if (!pending_size || pending_size < s->threshold_size) { |
2ad87305 PX |
2794 | trace_migration_thread_low_pending(pending_size); |
2795 | migration_completion(s); | |
2796 | return MIG_ITERATE_BREAK; | |
2797 | } | |
2798 | ||
d9df9292 | 2799 | /* Still a significant amount to transfer */ |
24beea4e | 2800 | if (!in_postcopy && must_precopy <= s->threshold_size && |
d9df9292 JQ |
2801 | qatomic_read(&s->start_postcopy)) { |
2802 | if (postcopy_start(s)) { | |
2803 | error_report("%s: postcopy failed to start", __func__); | |
2804 | } | |
2805 | return MIG_ITERATE_SKIP; | |
2806 | } | |
2807 | ||
2808 | /* Just another iteration step */ | |
2809 | qemu_savevm_state_iterate(s->to_dst_file, in_postcopy); | |
2ad87305 PX |
2810 | return MIG_ITERATE_RESUME; |
2811 | } | |
2812 | ||
199aa6d4 PX |
2813 | static void migration_iteration_finish(MigrationState *s) |
2814 | { | |
2815 | /* If we enabled cpu throttling for auto-converge, turn it off. */ | |
2816 | cpu_throttle_stop(); | |
2817 | ||
2818 | qemu_mutex_lock_iothread(); | |
2819 | switch (s->state) { | |
2820 | case MIGRATION_STATUS_COMPLETED: | |
2821 | migration_calculate_complete(s); | |
2822 | runstate_set(RUN_STATE_POSTMIGRATE); | |
2823 | break; | |
751fe4c6 | 2824 | case MIGRATION_STATUS_COLO: |
5e804644 | 2825 | if (!migrate_colo()) { |
199aa6d4 PX |
2826 | error_report("%s: critical error: calling COLO code without " |
2827 | "COLO enabled", __func__); | |
2828 | } | |
2829 | migrate_start_colo_process(s); | |
199aa6d4 PX |
2830 | s->vm_was_running = true; |
2831 | /* Fallthrough */ | |
2832 | case MIGRATION_STATUS_FAILED: | |
2833 | case MIGRATION_STATUS_CANCELLED: | |
57225e5f | 2834 | case MIGRATION_STATUS_CANCELLING: |
199aa6d4 | 2835 | if (s->vm_was_running) { |
aa505f8e RL |
2836 | if (!runstate_check(RUN_STATE_SHUTDOWN)) { |
2837 | vm_start(); | |
2838 | } | |
199aa6d4 PX |
2839 | } else { |
2840 | if (runstate_check(RUN_STATE_FINISH_MIGRATE)) { | |
2841 | runstate_set(RUN_STATE_POSTMIGRATE); | |
2842 | } | |
2843 | } | |
2844 | break; | |
2845 | ||
2846 | default: | |
2847 | /* Should not reach here, but if so, forgive the VM. */ | |
2848 | error_report("%s: Unknown ending state %d", __func__, s->state); | |
2849 | break; | |
2850 | } | |
fd392cfa | 2851 | migrate_fd_cleanup_schedule(s); |
199aa6d4 PX |
2852 | qemu_mutex_unlock_iothread(); |
2853 | } | |
2854 | ||
8518278a AG |
2855 | static void bg_migration_iteration_finish(MigrationState *s) |
2856 | { | |
2857 | qemu_mutex_lock_iothread(); | |
2858 | switch (s->state) { | |
2859 | case MIGRATION_STATUS_COMPLETED: | |
2860 | migration_calculate_complete(s); | |
2861 | break; | |
2862 | ||
2863 | case MIGRATION_STATUS_ACTIVE: | |
2864 | case MIGRATION_STATUS_FAILED: | |
2865 | case MIGRATION_STATUS_CANCELLED: | |
2866 | case MIGRATION_STATUS_CANCELLING: | |
2867 | break; | |
2868 | ||
2869 | default: | |
2870 | /* Should not reach here, but if so, forgive the VM. */ | |
2871 | error_report("%s: Unknown ending state %d", __func__, s->state); | |
2872 | break; | |
2873 | } | |
2874 | ||
2875 | migrate_fd_cleanup_schedule(s); | |
2876 | qemu_mutex_unlock_iothread(); | |
2877 | } | |
2878 | ||
2879 | /* | |
2880 | * Return true if continue to the next iteration directly, false | |
2881 | * otherwise. | |
2882 | */ | |
2883 | static MigIterateState bg_migration_iteration_run(MigrationState *s) | |
2884 | { | |
2885 | int res; | |
2886 | ||
2887 | res = qemu_savevm_state_iterate(s->to_dst_file, false); | |
2888 | if (res > 0) { | |
2889 | bg_migration_completion(s); | |
2890 | return MIG_ITERATE_BREAK; | |
2891 | } | |
2892 | ||
2893 | return MIG_ITERATE_RESUME; | |
2894 | } | |
2895 | ||
ad767bed DDAG |
2896 | void migration_make_urgent_request(void) |
2897 | { | |
2898 | qemu_sem_post(&migrate_get_current()->rate_limit_sem); | |
2899 | } | |
2900 | ||
2901 | void migration_consume_urgent_request(void) | |
2902 | { | |
2903 | qemu_sem_wait(&migrate_get_current()->rate_limit_sem); | |
2904 | } | |
2905 | ||
97e1e067 DDAG |
2906 | /* Returns true if the rate limiting was broken by an urgent request */ |
2907 | bool migration_rate_limit(void) | |
2908 | { | |
2909 | int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); | |
2910 | MigrationState *s = migrate_get_current(); | |
2911 | ||
2912 | bool urgent = false; | |
2913 | migration_update_counters(s, now); | |
2914 | if (qemu_file_rate_limit(s->to_dst_file)) { | |
77386127 LS |
2915 | |
2916 | if (qemu_file_get_error(s->to_dst_file)) { | |
2917 | return false; | |
2918 | } | |
97e1e067 DDAG |
2919 | /* |
2920 | * Wait for a delay to do rate limiting OR | |
2921 | * something urgent to post the semaphore. | |
2922 | */ | |
2923 | int ms = s->iteration_start_time + BUFFER_DELAY - now; | |
2924 | trace_migration_rate_limit_pre(ms); | |
2925 | if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) { | |
2926 | /* | |
2927 | * We were woken by one or more urgent things but | |
2928 | * the timedwait will have consumed one of them. | |
2929 | * The service routine for the urgent wake will dec | |
2930 | * the semaphore itself for each item it consumes, | |
2931 | * so add this one we just eat back. | |
2932 | */ | |
2933 | qemu_sem_post(&s->rate_limit_sem); | |
2934 | urgent = true; | |
2935 | } | |
2936 | trace_migration_rate_limit_post(urgent); | |
2937 | } | |
2938 | return urgent; | |
2939 | } | |
2940 | ||
fde93d99 LV |
2941 | /* |
2942 | * if failover devices are present, wait they are completely | |
2943 | * unplugged | |
2944 | */ | |
2945 | ||
2946 | static void qemu_savevm_wait_unplug(MigrationState *s, int old_state, | |
2947 | int new_state) | |
2948 | { | |
2949 | if (qemu_savevm_state_guest_unplug_pending()) { | |
2950 | migrate_set_state(&s->state, old_state, MIGRATION_STATUS_WAIT_UNPLUG); | |
2951 | ||
2952 | while (s->state == MIGRATION_STATUS_WAIT_UNPLUG && | |
2953 | qemu_savevm_state_guest_unplug_pending()) { | |
2954 | qemu_sem_timedwait(&s->wait_unplug_sem, 250); | |
2955 | } | |
944bc528 LV |
2956 | if (s->state != MIGRATION_STATUS_WAIT_UNPLUG) { |
2957 | int timeout = 120; /* 30 seconds */ | |
2958 | /* | |
2959 | * migration has been canceled | |
2960 | * but as we have started an unplug we must wait the end | |
2961 | * to be able to plug back the card | |
2962 | */ | |
2963 | while (timeout-- && qemu_savevm_state_guest_unplug_pending()) { | |
2964 | qemu_sem_timedwait(&s->wait_unplug_sem, 250); | |
2965 | } | |
1b529d90 LV |
2966 | if (qemu_savevm_state_guest_unplug_pending() && |
2967 | !qtest_enabled()) { | |
a51dcef0 LV |
2968 | warn_report("migration: partially unplugged device on " |
2969 | "failure"); | |
2970 | } | |
944bc528 | 2971 | } |
fde93d99 LV |
2972 | |
2973 | migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG, new_state); | |
2974 | } else { | |
2975 | migrate_set_state(&s->state, old_state, new_state); | |
2976 | } | |
2977 | } | |
2978 | ||
70b20477 DDAG |
2979 | /* |
2980 | * Master migration thread on the source VM. | |
2981 | * It drives the migration and pumps the data down the outgoing channel. | |
2982 | */ | |
5f496a1b | 2983 | static void *migration_thread(void *opaque) |
0d82d0e8 | 2984 | { |
9848a404 | 2985 | MigrationState *s = opaque; |
1b1f4ab6 | 2986 | MigrationThread *thread = NULL; |
bc72ad67 | 2987 | int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST); |
b23c2ade | 2988 | MigThrError thr_error; |
ad767bed | 2989 | bool urgent = false; |
76f5933a | 2990 | |
1b1f4ab6 JJ |
2991 | thread = MigrationThreadAdd("live_migration", qemu_get_thread_id()); |
2992 | ||
ab28bd23 PB |
2993 | rcu_register_thread(); |
2994 | ||
892ae715 | 2995 | object_ref(OBJECT(s)); |
87f3bd87 | 2996 | update_iteration_initial_status(s); |
b15df1ae | 2997 | |
89a02a9f | 2998 | qemu_savevm_state_header(s->to_dst_file); |
1d34e4bf | 2999 | |
62a02658 PX |
3000 | /* |
3001 | * If we opened the return path, we need to make sure dst has it | |
3002 | * opened as well. | |
3003 | */ | |
43044ac0 | 3004 | if (s->rp_state.rp_thread_created) { |
1d34e4bf | 3005 | /* Now tell the dest that it should open its end so it can reply */ |
89a02a9f | 3006 | qemu_savevm_send_open_return_path(s->to_dst_file); |
1d34e4bf DDAG |
3007 | |
3008 | /* And do a ping that will make stuff easier to debug */ | |
89a02a9f | 3009 | qemu_savevm_send_ping(s->to_dst_file, 1); |
0425dc97 | 3010 | } |
1d34e4bf | 3011 | |
58110f0a | 3012 | if (migrate_postcopy()) { |
1d34e4bf DDAG |
3013 | /* |
3014 | * Tell the destination that we *might* want to do postcopy later; | |
3015 | * if the other end can't do postcopy it should fail now, nice and | |
3016 | * early. | |
3017 | */ | |
89a02a9f | 3018 | qemu_savevm_send_postcopy_advise(s->to_dst_file); |
1d34e4bf DDAG |
3019 | } |
3020 | ||
5e804644 | 3021 | if (migrate_colo()) { |
aad555c2 ZC |
3022 | /* Notify migration destination that we enable COLO */ |
3023 | qemu_savevm_send_colo_enable(s->to_dst_file); | |
3024 | } | |
3025 | ||
9907e842 | 3026 | qemu_savevm_state_setup(s->to_dst_file); |
0d82d0e8 | 3027 | |
fde93d99 LV |
3028 | qemu_savevm_wait_unplug(s, MIGRATION_STATUS_SETUP, |
3029 | MIGRATION_STATUS_ACTIVE); | |
c7e0acd5 | 3030 | |
bc72ad67 | 3031 | s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start; |
29ae8a41 | 3032 | |
9ec055ae DDAG |
3033 | trace_migration_thread_setup_complete(); |
3034 | ||
8f8d528e | 3035 | while (migration_is_active(s)) { |
ad767bed | 3036 | if (urgent || !qemu_file_rate_limit(s->to_dst_file)) { |
2ad87305 PX |
3037 | MigIterateState iter_state = migration_iteration_run(s); |
3038 | if (iter_state == MIG_ITERATE_SKIP) { | |
3039 | continue; | |
3040 | } else if (iter_state == MIG_ITERATE_BREAK) { | |
09f6c85e | 3041 | break; |
c369f40d JQ |
3042 | } |
3043 | } | |
f4410a5d | 3044 | |
b23c2ade PX |
3045 | /* |
3046 | * Try to detect any kind of failures, and see whether we | |
3047 | * should stop the migration now. | |
3048 | */ | |
3049 | thr_error = migration_detect_error(s); | |
3050 | if (thr_error == MIG_THR_ERR_FATAL) { | |
3051 | /* Stop migration */ | |
fd45ee2c | 3052 | break; |
b23c2ade PX |
3053 | } else if (thr_error == MIG_THR_ERR_RECOVERED) { |
3054 | /* | |
3055 | * Just recovered from a e.g. network failure, reset all | |
3056 | * the local variables. This is important to avoid | |
3057 | * breaking transferred_bytes and bandwidth calculation | |
3058 | */ | |
87f3bd87 | 3059 | update_iteration_initial_status(s); |
fd45ee2c | 3060 | } |
b15df1ae | 3061 | |
97e1e067 | 3062 | urgent = migration_rate_limit(); |
a3fa1d78 PB |
3063 | } |
3064 | ||
1d34e4bf | 3065 | trace_migration_thread_after_loop(); |
199aa6d4 | 3066 | migration_iteration_finish(s); |
892ae715 | 3067 | object_unref(OBJECT(s)); |
ab28bd23 | 3068 | rcu_unregister_thread(); |
1b1f4ab6 | 3069 | MigrationThreadDel(thread); |
0d82d0e8 JQ |
3070 | return NULL; |
3071 | } | |
3072 | ||
8518278a AG |
3073 | static void bg_migration_vm_start_bh(void *opaque) |
3074 | { | |
3075 | MigrationState *s = opaque; | |
3076 | ||
3077 | qemu_bh_delete(s->vm_start_bh); | |
3078 | s->vm_start_bh = NULL; | |
3079 | ||
3080 | vm_start(); | |
3081 | s->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - s->downtime_start; | |
3082 | } | |
3083 | ||
3084 | /** | |
3085 | * Background snapshot thread, based on live migration code. | |
3086 | * This is an alternative implementation of live migration mechanism | |
3087 | * introduced specifically to support background snapshots. | |
3088 | * | |
3089 | * It takes advantage of userfault_fd write protection mechanism introduced | |
3090 | * in v5.7 kernel. Compared to existing dirty page logging migration much | |
3091 | * lesser stream traffic is produced resulting in smaller snapshot images, | |
3092 | * simply cause of no page duplicates can get into the stream. | |
3093 | * | |
3094 | * Another key point is that generated vmstate stream reflects machine state | |
3095 | * 'frozen' at the beginning of snapshot creation compared to dirty page logging | |
3096 | * mechanism, which effectively results in that saved snapshot is the state of VM | |
3097 | * at the end of the process. | |
3098 | */ | |
3099 | static void *bg_migration_thread(void *opaque) | |
3100 | { | |
3101 | MigrationState *s = opaque; | |
3102 | int64_t setup_start; | |
3103 | MigThrError thr_error; | |
3104 | QEMUFile *fb; | |
3105 | bool early_fail = true; | |
3106 | ||
3107 | rcu_register_thread(); | |
3108 | object_ref(OBJECT(s)); | |
3109 | ||
3110 | qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX); | |
3111 | ||
3112 | setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST); | |
3113 | /* | |
3114 | * We want to save vmstate for the moment when migration has been | |
3115 | * initiated but also we want to save RAM content while VM is running. | |
3116 | * The RAM content should appear first in the vmstate. So, we first | |
3117 | * stash the non-RAM part of the vmstate to the temporary buffer, | |
3118 | * then write RAM part of the vmstate to the migration stream | |
3119 | * with vCPUs running and, finally, write stashed non-RAM part of | |
3120 | * the vmstate from the buffer to the migration stream. | |
3121 | */ | |
ecb23efe | 3122 | s->bioc = qio_channel_buffer_new(512 * 1024); |
8518278a | 3123 | qio_channel_set_name(QIO_CHANNEL(s->bioc), "vmstate-buffer"); |
77ef2dc1 | 3124 | fb = qemu_file_new_output(QIO_CHANNEL(s->bioc)); |
8518278a AG |
3125 | object_unref(OBJECT(s->bioc)); |
3126 | ||
3127 | update_iteration_initial_status(s); | |
3128 | ||
eeccb99c AG |
3129 | /* |
3130 | * Prepare for tracking memory writes with UFFD-WP - populate | |
3131 | * RAM pages before protecting. | |
3132 | */ | |
3133 | #ifdef __linux__ | |
3134 | ram_write_tracking_prepare(); | |
3135 | #endif | |
3136 | ||
8518278a AG |
3137 | qemu_savevm_state_header(s->to_dst_file); |
3138 | qemu_savevm_state_setup(s->to_dst_file); | |
3139 | ||
fde93d99 LV |
3140 | qemu_savevm_wait_unplug(s, MIGRATION_STATUS_SETUP, |
3141 | MIGRATION_STATUS_ACTIVE); | |
8518278a | 3142 | |
8518278a AG |
3143 | s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start; |
3144 | ||
3145 | trace_migration_thread_setup_complete(); | |
3146 | s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); | |
3147 | ||
3148 | qemu_mutex_lock_iothread(); | |
3149 | ||
3150 | /* | |
3151 | * If VM is currently in suspended state, then, to make a valid runstate | |
3152 | * transition in vm_stop_force_state() we need to wakeup it up. | |
3153 | */ | |
3154 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL); | |
3155 | s->vm_was_running = runstate_is_running(); | |
3156 | ||
3157 | if (global_state_store()) { | |
3158 | goto fail; | |
3159 | } | |
3160 | /* Forcibly stop VM before saving state of vCPUs and devices */ | |
3161 | if (vm_stop_force_state(RUN_STATE_PAUSED)) { | |
3162 | goto fail; | |
3163 | } | |
3164 | /* | |
3165 | * Put vCPUs in sync with shadow context structures, then | |
3166 | * save their state to channel-buffer along with devices. | |
3167 | */ | |
3168 | cpu_synchronize_all_states(); | |
3169 | if (qemu_savevm_state_complete_precopy_non_iterable(fb, false, false)) { | |
3170 | goto fail; | |
3171 | } | |
ecb23efe AG |
3172 | /* |
3173 | * Since we are going to get non-iterable state data directly | |
3174 | * from s->bioc->data, explicit flush is needed here. | |
3175 | */ | |
3176 | qemu_fflush(fb); | |
3177 | ||
8518278a AG |
3178 | /* Now initialize UFFD context and start tracking RAM writes */ |
3179 | if (ram_write_tracking_start()) { | |
3180 | goto fail; | |
3181 | } | |
3182 | early_fail = false; | |
3183 | ||
3184 | /* | |
3185 | * Start VM from BH handler to avoid write-fault lock here. | |
3186 | * UFFD-WP protection for the whole RAM is already enabled so | |
3187 | * calling VM state change notifiers from vm_start() would initiate | |
3188 | * writes to virtio VQs memory which is in write-protected region. | |
3189 | */ | |
3190 | s->vm_start_bh = qemu_bh_new(bg_migration_vm_start_bh, s); | |
3191 | qemu_bh_schedule(s->vm_start_bh); | |
3192 | ||
3193 | qemu_mutex_unlock_iothread(); | |
3194 | ||
3195 | while (migration_is_active(s)) { | |
3196 | MigIterateState iter_state = bg_migration_iteration_run(s); | |
3197 | if (iter_state == MIG_ITERATE_SKIP) { | |
3198 | continue; | |
3199 | } else if (iter_state == MIG_ITERATE_BREAK) { | |
3200 | break; | |
3201 | } | |
3202 | ||
3203 | /* | |
3204 | * Try to detect any kind of failures, and see whether we | |
3205 | * should stop the migration now. | |
3206 | */ | |
3207 | thr_error = migration_detect_error(s); | |
3208 | if (thr_error == MIG_THR_ERR_FATAL) { | |
3209 | /* Stop migration */ | |
3210 | break; | |
3211 | } | |
3212 | ||
3213 | migration_update_counters(s, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)); | |
3214 | } | |
3215 | ||
3216 | trace_migration_thread_after_loop(); | |
3217 | ||
3218 | fail: | |
3219 | if (early_fail) { | |
3220 | migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE, | |
3221 | MIGRATION_STATUS_FAILED); | |
3222 | qemu_mutex_unlock_iothread(); | |
3223 | } | |
3224 | ||
3225 | bg_migration_iteration_finish(s); | |
3226 | ||
3227 | qemu_fclose(fb); | |
3228 | object_unref(OBJECT(s)); | |
3229 | rcu_unregister_thread(); | |
3230 | ||
3231 | return NULL; | |
3232 | } | |
3233 | ||
cce8040b | 3234 | void migrate_fd_connect(MigrationState *s, Error *error_in) |
0d82d0e8 | 3235 | { |
00f4b572 | 3236 | Error *local_err = NULL; |
d3e35b8f PX |
3237 | int64_t rate_limit; |
3238 | bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED; | |
3239 | ||
ca7bd082 PX |
3240 | /* |
3241 | * If there's a previous error, free it and prepare for another one. | |
3242 | * Meanwhile if migration completes successfully, there won't have an error | |
3243 | * dumped when calling migrate_fd_cleanup(). | |
3244 | */ | |
3245 | migrate_error_free(s); | |
3246 | ||
2ff30257 | 3247 | s->expected_downtime = s->parameters.downtime_limit; |
9cbc3649 MAL |
3248 | if (resume) { |
3249 | assert(s->cleanup_bh); | |
3250 | } else { | |
3251 | assert(!s->cleanup_bh); | |
3252 | s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s); | |
3253 | } | |
cce8040b DDAG |
3254 | if (error_in) { |
3255 | migrate_fd_error(s, error_in); | |
ca30f24d PX |
3256 | if (resume) { |
3257 | /* | |
3258 | * Don't do cleanup for resume if channel is invalid, but only dump | |
3259 | * the error. We wait for another channel connect from the user. | |
3260 | * The error_report still gives HMP user a hint on what failed. | |
3261 | * It's normally done in migrate_fd_cleanup(), but call it here | |
3262 | * explicitly. | |
3263 | */ | |
3264 | error_report_err(error_copy(s->error)); | |
3265 | } else { | |
3266 | migrate_fd_cleanup(s); | |
3267 | } | |
cce8040b DDAG |
3268 | return; |
3269 | } | |
0d82d0e8 | 3270 | |
d3e35b8f PX |
3271 | if (resume) { |
3272 | /* This is a resumed migration */ | |
5390adec | 3273 | rate_limit = migrate_max_postcopy_bandwidth() / |
8504ddec | 3274 | XFER_LIMIT_RATIO; |
d3e35b8f PX |
3275 | } else { |
3276 | /* This is a fresh new migration */ | |
9c894df3 | 3277 | rate_limit = migrate_max_bandwidth() / XFER_LIMIT_RATIO; |
442773ce | 3278 | |
d3e35b8f PX |
3279 | /* Notify before starting migration thread */ |
3280 | notifier_list_notify(&migration_state_notifiers, s); | |
3281 | } | |
3282 | ||
3283 | qemu_file_set_rate_limit(s->to_dst_file, rate_limit); | |
3284 | qemu_file_set_blocking(s->to_dst_file, true); | |
9287ac27 | 3285 | |
1d34e4bf | 3286 | /* |
c788ada8 PX |
3287 | * Open the return path. For postcopy, it is used exclusively. For |
3288 | * precopy, only if user specified "return-path" capability would | |
3289 | * QEMU uses the return path. | |
1d34e4bf | 3290 | */ |
38ad1110 | 3291 | if (migrate_postcopy_ram() || migrate_return_path()) { |
d3e35b8f | 3292 | if (open_return_path_on_source(s, !resume)) { |
1d34e4bf | 3293 | error_report("Unable to open return-path for postcopy"); |
d3e35b8f | 3294 | migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED); |
1d34e4bf DDAG |
3295 | migrate_fd_cleanup(s); |
3296 | return; | |
3297 | } | |
3298 | } | |
3299 | ||
06064a67 PX |
3300 | /* |
3301 | * This needs to be done before resuming a postcopy. Note: for newer | |
3302 | * QEMUs we will delay the channel creation until postcopy_start(), to | |
3303 | * avoid disorder of channel creations. | |
3304 | */ | |
3305 | if (migrate_postcopy_preempt() && s->preempt_pre_7_2) { | |
3306 | postcopy_preempt_setup(s); | |
3307 | } | |
3308 | ||
d3e35b8f | 3309 | if (resume) { |
135b87b4 PX |
3310 | /* Wakeup the main migration thread to do the recovery */ |
3311 | migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED, | |
3312 | MIGRATION_STATUS_POSTCOPY_RECOVER); | |
3313 | qemu_sem_post(&s->postcopy_pause_sem); | |
d3e35b8f PX |
3314 | return; |
3315 | } | |
3316 | ||
00f4b572 JQ |
3317 | if (multifd_save_setup(&local_err) != 0) { |
3318 | error_report_err(local_err); | |
f986c3d2 JQ |
3319 | migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, |
3320 | MIGRATION_STATUS_FAILED); | |
3321 | migrate_fd_cleanup(s); | |
3322 | return; | |
3323 | } | |
8518278a AG |
3324 | |
3325 | if (migrate_background_snapshot()) { | |
3326 | qemu_thread_create(&s->thread, "bg_snapshot", | |
3327 | bg_migration_thread, s, QEMU_THREAD_JOINABLE); | |
3328 | } else { | |
3329 | qemu_thread_create(&s->thread, "live_migration", | |
3330 | migration_thread, s, QEMU_THREAD_JOINABLE); | |
3331 | } | |
1d34e4bf | 3332 | s->migration_thread_running = true; |
0d82d0e8 | 3333 | } |
093e3c42 | 3334 | |
20814758 | 3335 | #define DEFINE_PROP_MIG_CAP(name, x) \ |
0cec2056 | 3336 | DEFINE_PROP_BOOL(name, MigrationState, capabilities[x], false) |
20814758 | 3337 | |
5272298c PX |
3338 | static Property migration_properties[] = { |
3339 | DEFINE_PROP_BOOL("store-global-state", MigrationState, | |
3340 | store_global_state, true), | |
71dd4c1a PX |
3341 | DEFINE_PROP_BOOL("send-configuration", MigrationState, |
3342 | send_configuration, true), | |
15c38503 PX |
3343 | DEFINE_PROP_BOOL("send-section-footer", MigrationState, |
3344 | send_section_footer, true), | |
f548222c XG |
3345 | DEFINE_PROP_BOOL("decompress-error-check", MigrationState, |
3346 | decompress_error_check, true), | |
002cad6b PX |
3347 | DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState, |
3348 | clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT), | |
6621883f PX |
3349 | DEFINE_PROP_BOOL("x-preempt-pre-7-2", MigrationState, |
3350 | preempt_pre_7_2, false), | |
89632faf PX |
3351 | |
3352 | /* Migration parameters */ | |
741d4086 | 3353 | DEFINE_PROP_UINT8("x-compress-level", MigrationState, |
89632faf PX |
3354 | parameters.compress_level, |
3355 | DEFAULT_MIGRATE_COMPRESS_LEVEL), | |
741d4086 | 3356 | DEFINE_PROP_UINT8("x-compress-threads", MigrationState, |
89632faf PX |
3357 | parameters.compress_threads, |
3358 | DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT), | |
1d58872a XG |
3359 | DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState, |
3360 | parameters.compress_wait_thread, true), | |
741d4086 | 3361 | DEFINE_PROP_UINT8("x-decompress-threads", MigrationState, |
89632faf PX |
3362 | parameters.decompress_threads, |
3363 | DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT), | |
dc14a470 KZ |
3364 | DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState, |
3365 | parameters.throttle_trigger_threshold, | |
3366 | DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD), | |
741d4086 | 3367 | DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState, |
89632faf PX |
3368 | parameters.cpu_throttle_initial, |
3369 | DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL), | |
741d4086 | 3370 | DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState, |
89632faf PX |
3371 | parameters.cpu_throttle_increment, |
3372 | DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT), | |
cbbf8182 KZ |
3373 | DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState, |
3374 | parameters.cpu_throttle_tailslow, false), | |
741d4086 | 3375 | DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState, |
89632faf | 3376 | parameters.max_bandwidth, MAX_THROTTLE), |
741d4086 | 3377 | DEFINE_PROP_UINT64("x-downtime-limit", MigrationState, |
89632faf PX |
3378 | parameters.downtime_limit, |
3379 | DEFAULT_MIGRATE_SET_DOWNTIME), | |
741d4086 | 3380 | DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState, |
89632faf PX |
3381 | parameters.x_checkpoint_delay, |
3382 | DEFAULT_MIGRATE_X_CHECKPOINT_DELAY), | |
cbfd6c95 JQ |
3383 | DEFINE_PROP_UINT8("multifd-channels", MigrationState, |
3384 | parameters.multifd_channels, | |
4075fb1c | 3385 | DEFAULT_MIGRATE_MULTIFD_CHANNELS), |
96eef042 JQ |
3386 | DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState, |
3387 | parameters.multifd_compression, | |
3388 | DEFAULT_MIGRATE_MULTIFD_COMPRESSION), | |
9004db48 JQ |
3389 | DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState, |
3390 | parameters.multifd_zlib_level, | |
3391 | DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL), | |
6a9ad154 JQ |
3392 | DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState, |
3393 | parameters.multifd_zstd_level, | |
3394 | DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL), | |
73af8dd8 JQ |
3395 | DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState, |
3396 | parameters.xbzrle_cache_size, | |
3397 | DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE), | |
7e555c6c DDAG |
3398 | DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState, |
3399 | parameters.max_postcopy_bandwidth, | |
3400 | DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH), | |
4cbc9c7f LQ |
3401 | DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState, |
3402 | parameters.max_cpu_throttle, | |
3403 | DEFAULT_MIGRATE_MAX_CPU_THROTTLE), | |
ee3d96ba DDAG |
3404 | DEFINE_PROP_SIZE("announce-initial", MigrationState, |
3405 | parameters.announce_initial, | |
3406 | DEFAULT_MIGRATE_ANNOUNCE_INITIAL), | |
3407 | DEFINE_PROP_SIZE("announce-max", MigrationState, | |
3408 | parameters.announce_max, | |
3409 | DEFAULT_MIGRATE_ANNOUNCE_MAX), | |
3410 | DEFINE_PROP_SIZE("announce-rounds", MigrationState, | |
3411 | parameters.announce_rounds, | |
3412 | DEFAULT_MIGRATE_ANNOUNCE_ROUNDS), | |
3413 | DEFINE_PROP_SIZE("announce-step", MigrationState, | |
3414 | parameters.announce_step, | |
3415 | DEFAULT_MIGRATE_ANNOUNCE_STEP), | |
9a266627 PX |
3416 | DEFINE_PROP_STRING("tls-creds", MigrationState, parameters.tls_creds), |
3417 | DEFINE_PROP_STRING("tls-hostname", MigrationState, parameters.tls_hostname), | |
3418 | DEFINE_PROP_STRING("tls-authz", MigrationState, parameters.tls_authz), | |
20814758 PX |
3419 | |
3420 | /* Migration capabilities */ | |
3421 | DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE), | |
3422 | DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL), | |
3423 | DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE), | |
3424 | DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS), | |
3425 | DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS), | |
3426 | DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS), | |
3427 | DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM), | |
ce5b0f4a PX |
3428 | DEFINE_PROP_MIG_CAP("x-postcopy-preempt", |
3429 | MIGRATION_CAPABILITY_POSTCOPY_PREEMPT), | |
20814758 PX |
3430 | DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO), |
3431 | DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM), | |
3432 | DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK), | |
3433 | DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH), | |
cbfd6c95 | 3434 | DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD), |
6e8c25b4 AG |
3435 | DEFINE_PROP_MIG_CAP("x-background-snapshot", |
3436 | MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT), | |
1abaec9a LB |
3437 | #ifdef CONFIG_LINUX |
3438 | DEFINE_PROP_MIG_CAP("x-zero-copy-send", | |
3439 | MIGRATION_CAPABILITY_ZERO_COPY_SEND), | |
3440 | #endif | |
20814758 | 3441 | |
5272298c PX |
3442 | DEFINE_PROP_END_OF_LIST(), |
3443 | }; | |
3444 | ||
e5cb7e76 PX |
3445 | static void migration_class_init(ObjectClass *klass, void *data) |
3446 | { | |
3447 | DeviceClass *dc = DEVICE_CLASS(klass); | |
3448 | ||
3449 | dc->user_creatable = false; | |
4f67d30b | 3450 | device_class_set_props(dc, migration_properties); |
e5cb7e76 PX |
3451 | } |
3452 | ||
b91bf5e4 MAL |
3453 | static void migration_instance_finalize(Object *obj) |
3454 | { | |
3455 | MigrationState *ms = MIGRATION_OBJ(obj); | |
b91bf5e4 | 3456 | |
87db1a7d | 3457 | qemu_mutex_destroy(&ms->error_mutex); |
62df066f | 3458 | qemu_mutex_destroy(&ms->qemu_file_lock); |
c7e0acd5 | 3459 | qemu_sem_destroy(&ms->wait_unplug_sem); |
ad767bed | 3460 | qemu_sem_destroy(&ms->rate_limit_sem); |
e91d8951 | 3461 | qemu_sem_destroy(&ms->pause_sem); |
b23c2ade | 3462 | qemu_sem_destroy(&ms->postcopy_pause_sem); |
14b1742e | 3463 | qemu_sem_destroy(&ms->postcopy_pause_rp_sem); |
edd090c7 | 3464 | qemu_sem_destroy(&ms->rp_state.rp_sem); |
b28fb582 | 3465 | qemu_sem_destroy(&ms->rp_state.rp_pong_acks); |
d0edb8a1 | 3466 | qemu_sem_destroy(&ms->postcopy_qemufile_src_sem); |
ab105cc1 | 3467 | error_free(ms->error); |
b91bf5e4 MAL |
3468 | } |
3469 | ||
e5cb7e76 PX |
3470 | static void migration_instance_init(Object *obj) |
3471 | { | |
3472 | MigrationState *ms = MIGRATION_OBJ(obj); | |
3473 | ||
3474 | ms->state = MIGRATION_STATUS_NONE; | |
e5cb7e76 | 3475 | ms->mbps = -1; |
aecbfe9c | 3476 | ms->pages_per_second = -1; |
e91d8951 | 3477 | qemu_sem_init(&ms->pause_sem, 0); |
87db1a7d | 3478 | qemu_mutex_init(&ms->error_mutex); |
8b0b29dc | 3479 | |
61a174e2 | 3480 | migrate_params_init(&ms->parameters); |
b23c2ade PX |
3481 | |
3482 | qemu_sem_init(&ms->postcopy_pause_sem, 0); | |
14b1742e | 3483 | qemu_sem_init(&ms->postcopy_pause_rp_sem, 0); |
edd090c7 | 3484 | qemu_sem_init(&ms->rp_state.rp_sem, 0); |
b28fb582 | 3485 | qemu_sem_init(&ms->rp_state.rp_pong_acks, 0); |
ad767bed | 3486 | qemu_sem_init(&ms->rate_limit_sem, 0); |
c7e0acd5 | 3487 | qemu_sem_init(&ms->wait_unplug_sem, 0); |
d0edb8a1 | 3488 | qemu_sem_init(&ms->postcopy_qemufile_src_sem, 0); |
62df066f | 3489 | qemu_mutex_init(&ms->qemu_file_lock); |
8b0b29dc PX |
3490 | } |
3491 | ||
3492 | /* | |
3493 | * Return true if check pass, false otherwise. Error will be put | |
3494 | * inside errp if provided. | |
3495 | */ | |
3496 | static bool migration_object_check(MigrationState *ms, Error **errp) | |
3497 | { | |
6b19a7d9 | 3498 | /* Assuming all off */ |
b02c7fc9 | 3499 | bool old_caps[MIGRATION_CAPABILITY__MAX] = { 0 }; |
6b19a7d9 | 3500 | |
8b0b29dc PX |
3501 | if (!migrate_params_check(&ms->parameters, errp)) { |
3502 | return false; | |
3503 | } | |
3504 | ||
b02c7fc9 | 3505 | return migrate_caps_check(old_caps, ms->capabilities, errp); |
e5cb7e76 PX |
3506 | } |
3507 | ||
3508 | static const TypeInfo migration_type = { | |
3509 | .name = TYPE_MIGRATION, | |
01f6e14c | 3510 | /* |
c8d3ff38 | 3511 | * NOTE: TYPE_MIGRATION is not really a device, as the object is |
2194abd6 | 3512 | * not created using qdev_new(), it is not attached to the qdev |
c8d3ff38 PX |
3513 | * device tree, and it is never realized. |
3514 | * | |
3515 | * TODO: Make this TYPE_OBJECT once QOM provides something like | |
3516 | * TYPE_DEVICE's "-global" properties. | |
01f6e14c | 3517 | */ |
e5cb7e76 PX |
3518 | .parent = TYPE_DEVICE, |
3519 | .class_init = migration_class_init, | |
3520 | .class_size = sizeof(MigrationClass), | |
3521 | .instance_size = sizeof(MigrationState), | |
3522 | .instance_init = migration_instance_init, | |
b91bf5e4 | 3523 | .instance_finalize = migration_instance_finalize, |
e5cb7e76 PX |
3524 | }; |
3525 | ||
3526 | static void register_migration_types(void) | |
3527 | { | |
3528 | type_register_static(&migration_type); | |
3529 | } | |
3530 | ||
3531 | type_init(register_migration_types); |