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