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