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