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