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