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