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