4 * Copyright IBM, Corp. 2008
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
16 #include "qemu/osdep.h"
17 #include "qemu/cutils.h"
18 #include "qemu/error-report.h"
19 #include "qemu/main-loop.h"
20 #include "migration/blocker.h"
24 #include "sysemu/runstate.h"
25 #include "sysemu/sysemu.h"
26 #include "sysemu/cpu-throttle.h"
29 #include "ram-compress.h"
30 #include "migration/global_state.h"
31 #include "migration/misc.h"
32 #include "migration.h"
33 #include "migration-stats.h"
35 #include "qemu-file.h"
37 #include "migration/vmstate.h"
38 #include "block/block.h"
39 #include "qapi/error.h"
40 #include "qapi/clone-visitor.h"
41 #include "qapi/qapi-visit-migration.h"
42 #include "qapi/qapi-visit-sockets.h"
43 #include "qapi/qapi-commands-migration.h"
44 #include "qapi/qapi-events-migration.h"
45 #include "qapi/qmp/qerror.h"
46 #include "qapi/qmp/qnull.h"
49 #include "postcopy-ram.h"
50 #include "qemu/thread.h"
52 #include "exec/target_page.h"
53 #include "io/channel-buffer.h"
54 #include "io/channel-tls.h"
55 #include "migration/colo.h"
56 #include "hw/boards.h"
57 #include "monitor/monitor.h"
58 #include "net/announce.h"
59 #include "qemu/queue.h"
61 #include "threadinfo.h"
62 #include "qemu/yank.h"
63 #include "sysemu/cpus.h"
64 #include "yank_functions.h"
65 #include "sysemu/qtest.h"
67 #include "sysemu/dirtylimit.h"
69 static NotifierList migration_state_notifiers
=
70 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers
);
72 /* Messages sent on the return path from destination to source */
73 enum mig_rp_message_type
{
74 MIG_RP_MSG_INVALID
= 0, /* Must be 0 */
75 MIG_RP_MSG_SHUT
, /* sibling will not send any more RP messages */
76 MIG_RP_MSG_PONG
, /* Response to a PING; data (seq: be32 ) */
78 MIG_RP_MSG_REQ_PAGES_ID
, /* data (start: be64, len: be32, id: string) */
79 MIG_RP_MSG_REQ_PAGES
, /* data (start: be64, len: be32) */
80 MIG_RP_MSG_RECV_BITMAP
, /* send recved_bitmap back to source */
81 MIG_RP_MSG_RESUME_ACK
, /* tell source that we are ready to resume */
82 MIG_RP_MSG_SWITCHOVER_ACK
, /* Tell source it's OK to do switchover */
87 /* When we add fault tolerance, we could have several
88 migrations at once. For now we don't need to add
89 dynamic creation of migration */
91 static MigrationState
*current_migration
;
92 static MigrationIncomingState
*current_incoming
;
94 static GSList
*migration_blockers
;
96 static bool migration_object_check(MigrationState
*ms
, Error
**errp
);
97 static int migration_maybe_pause(MigrationState
*s
,
98 int *current_active_state
,
100 static void migrate_fd_cancel(MigrationState
*s
);
101 static int await_return_path_close_on_source(MigrationState
*s
);
103 static bool migration_needs_multiple_sockets(void)
105 return migrate_multifd() || migrate_postcopy_preempt();
108 static bool uri_supports_multi_channels(const char *uri
)
110 return strstart(uri
, "tcp:", NULL
) || strstart(uri
, "unix:", NULL
) ||
111 strstart(uri
, "vsock:", NULL
);
115 migration_channels_and_uri_compatible(const char *uri
, Error
**errp
)
117 if (migration_needs_multiple_sockets() &&
118 !uri_supports_multi_channels(uri
)) {
119 error_setg(errp
, "Migration requires multi-channel URIs (e.g. tcp)");
126 static gint
page_request_addr_cmp(gconstpointer ap
, gconstpointer bp
)
128 uintptr_t a
= (uintptr_t) ap
, b
= (uintptr_t) bp
;
130 return (a
> b
) - (a
< b
);
133 void migration_object_init(void)
135 /* This can only be called once. */
136 assert(!current_migration
);
137 current_migration
= MIGRATION_OBJ(object_new(TYPE_MIGRATION
));
140 * Init the migrate incoming object as well no matter whether
141 * we'll use it or not.
143 assert(!current_incoming
);
144 current_incoming
= g_new0(MigrationIncomingState
, 1);
145 current_incoming
->state
= MIGRATION_STATUS_NONE
;
146 current_incoming
->postcopy_remote_fds
=
147 g_array_new(FALSE
, TRUE
, sizeof(struct PostCopyFD
));
148 qemu_mutex_init(¤t_incoming
->rp_mutex
);
149 qemu_mutex_init(¤t_incoming
->postcopy_prio_thread_mutex
);
150 qemu_event_init(¤t_incoming
->main_thread_load_event
, false);
151 qemu_sem_init(¤t_incoming
->postcopy_pause_sem_dst
, 0);
152 qemu_sem_init(¤t_incoming
->postcopy_pause_sem_fault
, 0);
153 qemu_sem_init(¤t_incoming
->postcopy_pause_sem_fast_load
, 0);
154 qemu_sem_init(¤t_incoming
->postcopy_qemufile_dst_done
, 0);
156 qemu_mutex_init(¤t_incoming
->page_request_mutex
);
157 qemu_cond_init(¤t_incoming
->page_request_cond
);
158 current_incoming
->page_requested
= g_tree_new(page_request_addr_cmp
);
160 migration_object_check(current_migration
, &error_fatal
);
164 dirty_bitmap_mig_init();
167 void migration_cancel(const Error
*error
)
170 migrate_set_error(current_migration
, error
);
172 if (migrate_dirty_limit()) {
173 qmp_cancel_vcpu_dirty_limit(false, -1, NULL
);
175 migrate_fd_cancel(current_migration
);
178 void migration_shutdown(void)
181 * When the QEMU main thread exit, the COLO thread
182 * may wait a semaphore. So, we should wakeup the
183 * COLO thread before migration shutdown.
187 * Cancel the current migration - that will (eventually)
188 * stop the migration using this structure
190 migration_cancel(NULL
);
191 object_unref(OBJECT(current_migration
));
194 * Cancel outgoing migration of dirty bitmaps. It should
195 * at least unref used block nodes.
197 dirty_bitmap_mig_cancel_outgoing();
200 * Cancel incoming migration of dirty bitmaps. Dirty bitmaps
201 * are non-critical data, and their loss never considered as
204 dirty_bitmap_mig_cancel_incoming();
208 MigrationState
*migrate_get_current(void)
210 /* This can only be called after the object created. */
211 assert(current_migration
);
212 return current_migration
;
215 MigrationIncomingState
*migration_incoming_get_current(void)
217 assert(current_incoming
);
218 return current_incoming
;
221 void migration_incoming_transport_cleanup(MigrationIncomingState
*mis
)
223 if (mis
->socket_address_list
) {
224 qapi_free_SocketAddressList(mis
->socket_address_list
);
225 mis
->socket_address_list
= NULL
;
228 if (mis
->transport_cleanup
) {
229 mis
->transport_cleanup(mis
->transport_data
);
230 mis
->transport_data
= mis
->transport_cleanup
= NULL
;
234 void migration_incoming_state_destroy(void)
236 struct MigrationIncomingState
*mis
= migration_incoming_get_current();
238 multifd_load_cleanup();
239 compress_threads_load_cleanup();
241 if (mis
->to_src_file
) {
242 /* Tell source that we are done */
243 migrate_send_rp_shut(mis
, qemu_file_get_error(mis
->from_src_file
) != 0);
244 qemu_fclose(mis
->to_src_file
);
245 mis
->to_src_file
= NULL
;
248 if (mis
->from_src_file
) {
249 migration_ioc_unregister_yank_from_file(mis
->from_src_file
);
250 qemu_fclose(mis
->from_src_file
);
251 mis
->from_src_file
= NULL
;
253 if (mis
->postcopy_remote_fds
) {
254 g_array_free(mis
->postcopy_remote_fds
, TRUE
);
255 mis
->postcopy_remote_fds
= NULL
;
258 migration_incoming_transport_cleanup(mis
);
259 qemu_event_reset(&mis
->main_thread_load_event
);
261 if (mis
->page_requested
) {
262 g_tree_destroy(mis
->page_requested
);
263 mis
->page_requested
= NULL
;
266 if (mis
->postcopy_qemufile_dst
) {
267 migration_ioc_unregister_yank_from_file(mis
->postcopy_qemufile_dst
);
268 qemu_fclose(mis
->postcopy_qemufile_dst
);
269 mis
->postcopy_qemufile_dst
= NULL
;
272 yank_unregister_instance(MIGRATION_YANK_INSTANCE
);
275 static void migrate_generate_event(int new_state
)
277 if (migrate_events()) {
278 qapi_event_send_migration(new_state
);
283 * Send a message on the return channel back to the source
286 static int migrate_send_rp_message(MigrationIncomingState
*mis
,
287 enum mig_rp_message_type message_type
,
288 uint16_t len
, void *data
)
292 trace_migrate_send_rp_message((int)message_type
, len
);
293 QEMU_LOCK_GUARD(&mis
->rp_mutex
);
296 * It's possible that the file handle got lost due to network
299 if (!mis
->to_src_file
) {
304 qemu_put_be16(mis
->to_src_file
, (unsigned int)message_type
);
305 qemu_put_be16(mis
->to_src_file
, len
);
306 qemu_put_buffer(mis
->to_src_file
, data
, len
);
307 qemu_fflush(mis
->to_src_file
);
309 /* It's possible that qemu file got error during sending */
310 ret
= qemu_file_get_error(mis
->to_src_file
);
315 /* Request one page from the source VM at the given start address.
316 * rb: the RAMBlock to request the page in
317 * Start: Address offset within the RB
318 * Len: Length in bytes required - must be a multiple of pagesize
320 int migrate_send_rp_message_req_pages(MigrationIncomingState
*mis
,
321 RAMBlock
*rb
, ram_addr_t start
)
323 uint8_t bufc
[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
324 size_t msglen
= 12; /* start + len */
325 size_t len
= qemu_ram_pagesize(rb
);
326 enum mig_rp_message_type msg_type
;
330 *(uint64_t *)bufc
= cpu_to_be64((uint64_t)start
);
331 *(uint32_t *)(bufc
+ 8) = cpu_to_be32((uint32_t)len
);
334 * We maintain the last ramblock that we requested for page. Note that we
335 * don't need locking because this function will only be called within the
336 * postcopy ram fault thread.
338 if (rb
!= mis
->last_rb
) {
341 rbname
= qemu_ram_get_idstr(rb
);
342 rbname_len
= strlen(rbname
);
344 assert(rbname_len
< 256);
346 bufc
[msglen
++] = rbname_len
;
347 memcpy(bufc
+ msglen
, rbname
, rbname_len
);
348 msglen
+= rbname_len
;
349 msg_type
= MIG_RP_MSG_REQ_PAGES_ID
;
351 msg_type
= MIG_RP_MSG_REQ_PAGES
;
354 return migrate_send_rp_message(mis
, msg_type
, msglen
, bufc
);
357 int migrate_send_rp_req_pages(MigrationIncomingState
*mis
,
358 RAMBlock
*rb
, ram_addr_t start
, uint64_t haddr
)
360 void *aligned
= (void *)(uintptr_t)ROUND_DOWN(haddr
, qemu_ram_pagesize(rb
));
361 bool received
= false;
363 WITH_QEMU_LOCK_GUARD(&mis
->page_request_mutex
) {
364 received
= ramblock_recv_bitmap_test_byte_offset(rb
, start
);
365 if (!received
&& !g_tree_lookup(mis
->page_requested
, aligned
)) {
367 * The page has not been received, and it's not yet in the page
368 * request list. Queue it. Set the value of element to 1, so that
369 * things like g_tree_lookup() will return TRUE (1) when found.
371 g_tree_insert(mis
->page_requested
, aligned
, (gpointer
)1);
372 qatomic_inc(&mis
->page_requested_count
);
373 trace_postcopy_page_req_add(aligned
, mis
->page_requested_count
);
378 * If the page is there, skip sending the message. We don't even need the
379 * lock because as long as the page arrived, it'll be there forever.
385 return migrate_send_rp_message_req_pages(mis
, rb
, start
);
388 static bool migration_colo_enabled
;
389 bool migration_incoming_colo_enabled(void)
391 return migration_colo_enabled
;
394 void migration_incoming_disable_colo(void)
396 ram_block_discard_disable(false);
397 migration_colo_enabled
= false;
400 int migration_incoming_enable_colo(void)
402 #ifndef CONFIG_REPLICATION
403 error_report("ENABLE_COLO command come in migration stream, but COLO "
404 "module is not built in");
408 if (!migrate_colo()) {
409 error_report("ENABLE_COLO command come in migration stream, but c-colo "
410 "capability is not set");
414 if (ram_block_discard_disable(true)) {
415 error_report("COLO: cannot disable RAM discard");
418 migration_colo_enabled
= true;
422 void migrate_add_address(SocketAddress
*address
)
424 MigrationIncomingState
*mis
= migration_incoming_get_current();
426 QAPI_LIST_PREPEND(mis
->socket_address_list
,
427 QAPI_CLONE(SocketAddress
, address
));
430 static void qemu_start_incoming_migration(const char *uri
, Error
**errp
)
432 const char *p
= NULL
;
434 /* URI is not suitable for migration? */
435 if (!migration_channels_and_uri_compatible(uri
, errp
)) {
439 qapi_event_send_migration(MIGRATION_STATUS_SETUP
);
440 if (strstart(uri
, "tcp:", &p
) ||
441 strstart(uri
, "unix:", NULL
) ||
442 strstart(uri
, "vsock:", NULL
)) {
443 socket_start_incoming_migration(p
? p
: uri
, errp
);
445 } else if (strstart(uri
, "rdma:", &p
)) {
446 rdma_start_incoming_migration(p
, errp
);
448 } else if (strstart(uri
, "exec:", &p
)) {
449 exec_start_incoming_migration(p
, errp
);
450 } else if (strstart(uri
, "fd:", &p
)) {
451 fd_start_incoming_migration(p
, errp
);
453 error_setg(errp
, "unknown migration protocol: %s", uri
);
457 static void process_incoming_migration_bh(void *opaque
)
459 Error
*local_err
= NULL
;
460 MigrationIncomingState
*mis
= opaque
;
462 /* If capability late_block_activate is set:
463 * Only fire up the block code now if we're going to restart the
464 * VM, else 'cont' will do it.
465 * This causes file locking to happen; so we don't want it to happen
466 * unless we really are starting the VM.
468 if (!migrate_late_block_activate() ||
469 (autostart
&& (!global_state_received() ||
470 global_state_get_runstate() == RUN_STATE_RUNNING
))) {
471 /* Make sure all file formats throw away their mutable metadata.
472 * If we get an error here, just don't restart the VM yet. */
473 bdrv_activate_all(&local_err
);
475 error_report_err(local_err
);
482 * This must happen after all error conditions are dealt with and
483 * we're sure the VM is going to be running on this host.
485 qemu_announce_self(&mis
->announce_timer
, migrate_announce_params());
487 multifd_load_shutdown();
489 dirty_bitmap_mig_before_vm_start();
491 if (!global_state_received() ||
492 global_state_get_runstate() == RUN_STATE_RUNNING
) {
496 runstate_set(RUN_STATE_PAUSED
);
498 } else if (migration_incoming_colo_enabled()) {
499 migration_incoming_disable_colo();
502 runstate_set(global_state_get_runstate());
505 * This must happen after any state changes since as soon as an external
506 * observer sees this event they might start to prod at the VM assuming
509 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
510 MIGRATION_STATUS_COMPLETED
);
511 qemu_bh_delete(mis
->bh
);
512 migration_incoming_state_destroy();
515 static void coroutine_fn
516 process_incoming_migration_co(void *opaque
)
518 MigrationIncomingState
*mis
= migration_incoming_get_current();
522 assert(mis
->from_src_file
);
524 if (compress_threads_load_setup(mis
->from_src_file
)) {
525 error_report("Failed to setup decompress threads");
529 mis
->largest_page_size
= qemu_ram_pagesize_largest();
530 postcopy_state_set(POSTCOPY_INCOMING_NONE
);
531 migrate_set_state(&mis
->state
, MIGRATION_STATUS_NONE
,
532 MIGRATION_STATUS_ACTIVE
);
534 mis
->loadvm_co
= qemu_coroutine_self();
535 ret
= qemu_loadvm_state(mis
->from_src_file
);
536 mis
->loadvm_co
= NULL
;
538 ps
= postcopy_state_get();
539 trace_process_incoming_migration_co_end(ret
, ps
);
540 if (ps
!= POSTCOPY_INCOMING_NONE
) {
541 if (ps
== POSTCOPY_INCOMING_ADVISE
) {
543 * Where a migration had postcopy enabled (and thus went to advise)
544 * but managed to complete within the precopy period, we can use
547 postcopy_ram_incoming_cleanup(mis
);
548 } else if (ret
>= 0) {
550 * Postcopy was started, cleanup should happen at the end of the
553 trace_process_incoming_migration_co_postcopy_end_main();
556 /* Else if something went wrong then just fall out of the normal exit */
560 error_report("load of migration failed: %s", strerror(-ret
));
564 if (colo_incoming_co() < 0) {
568 mis
->bh
= qemu_bh_new(process_incoming_migration_bh
, mis
);
569 qemu_bh_schedule(mis
->bh
);
572 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
573 MIGRATION_STATUS_FAILED
);
574 qemu_fclose(mis
->from_src_file
);
576 multifd_load_cleanup();
577 compress_threads_load_cleanup();
583 * migration_incoming_setup: Setup incoming migration
584 * @f: file for main migration channel
585 * @errp: where to put errors
587 * Returns: %true on success, %false on error.
589 static bool migration_incoming_setup(QEMUFile
*f
, Error
**errp
)
591 MigrationIncomingState
*mis
= migration_incoming_get_current();
593 if (!mis
->from_src_file
) {
594 mis
->from_src_file
= f
;
596 qemu_file_set_blocking(f
, false);
600 void migration_incoming_process(void)
602 Coroutine
*co
= qemu_coroutine_create(process_incoming_migration_co
, NULL
);
603 qemu_coroutine_enter(co
);
606 /* Returns true if recovered from a paused migration, otherwise false */
607 static bool postcopy_try_recover(void)
609 MigrationIncomingState
*mis
= migration_incoming_get_current();
611 if (mis
->state
== MIGRATION_STATUS_POSTCOPY_PAUSED
) {
612 /* Resumed from a paused postcopy migration */
614 /* This should be set already in migration_incoming_setup() */
615 assert(mis
->from_src_file
);
616 /* Postcopy has standalone thread to do vm load */
617 qemu_file_set_blocking(mis
->from_src_file
, true);
619 /* Re-configure the return path */
620 mis
->to_src_file
= qemu_file_get_return_path(mis
->from_src_file
);
622 migrate_set_state(&mis
->state
, MIGRATION_STATUS_POSTCOPY_PAUSED
,
623 MIGRATION_STATUS_POSTCOPY_RECOVER
);
626 * Here, we only wake up the main loading thread (while the
627 * rest threads will still be waiting), so that we can receive
628 * commands from source now, and answer it if needed. The
629 * rest threads will be woken up afterwards until we are sure
630 * that source is ready to reply to page requests.
632 qemu_sem_post(&mis
->postcopy_pause_sem_dst
);
639 void migration_fd_process_incoming(QEMUFile
*f
, Error
**errp
)
641 if (!migration_incoming_setup(f
, errp
)) {
644 if (postcopy_try_recover()) {
647 migration_incoming_process();
651 * Returns true when we want to start a new incoming migration process,
654 static bool migration_should_start_incoming(bool main_channel
)
656 /* Multifd doesn't start unless all channels are established */
657 if (migrate_multifd()) {
658 return migration_has_all_channels();
661 /* Preempt channel only starts when the main channel is created */
662 if (migrate_postcopy_preempt()) {
667 * For all the rest types of migration, we should only reach here when
668 * it's the main channel that's being created, and we should always
669 * proceed with this channel.
671 assert(main_channel
);
675 void migration_ioc_process_incoming(QIOChannel
*ioc
, Error
**errp
)
677 MigrationIncomingState
*mis
= migration_incoming_get_current();
678 Error
*local_err
= NULL
;
680 bool default_channel
= true;
681 uint32_t channel_magic
= 0;
684 if (migrate_multifd() && !migrate_postcopy_ram() &&
685 qio_channel_has_feature(ioc
, QIO_CHANNEL_FEATURE_READ_MSG_PEEK
)) {
687 * With multiple channels, it is possible that we receive channels
688 * out of order on destination side, causing incorrect mapping of
689 * source channels on destination side. Check channel MAGIC to
690 * decide type of channel. Please note this is best effort, postcopy
691 * preempt channel does not send any magic number so avoid it for
692 * postcopy live migration. Also tls live migration already does
693 * tls handshake while initializing main channel so with tls this
694 * issue is not possible.
696 ret
= migration_channel_read_peek(ioc
, (void *)&channel_magic
,
697 sizeof(channel_magic
), &local_err
);
700 error_propagate(errp
, local_err
);
704 default_channel
= (channel_magic
== cpu_to_be32(QEMU_VM_FILE_MAGIC
));
706 default_channel
= !mis
->from_src_file
;
709 if (multifd_load_setup(errp
) != 0) {
710 error_setg(errp
, "Failed to setup multifd channels");
714 if (default_channel
) {
715 f
= qemu_file_new_input(ioc
);
717 if (!migration_incoming_setup(f
, errp
)) {
721 /* Multiple connections */
722 assert(migration_needs_multiple_sockets());
723 if (migrate_multifd()) {
724 multifd_recv_new_channel(ioc
, &local_err
);
726 assert(migrate_postcopy_preempt());
727 f
= qemu_file_new_input(ioc
);
728 postcopy_preempt_new_channel(mis
, f
);
731 error_propagate(errp
, local_err
);
736 if (migration_should_start_incoming(default_channel
)) {
737 /* If it's a recovery, we're done */
738 if (postcopy_try_recover()) {
741 migration_incoming_process();
746 * @migration_has_all_channels: We have received all channels that we need
748 * Returns true when we have got connections to all the channels that
749 * we need for migration.
751 bool migration_has_all_channels(void)
753 MigrationIncomingState
*mis
= migration_incoming_get_current();
755 if (!mis
->from_src_file
) {
759 if (migrate_multifd()) {
760 return multifd_recv_all_channels_created();
763 if (migrate_postcopy_preempt()) {
764 return mis
->postcopy_qemufile_dst
!= NULL
;
770 int migrate_send_rp_switchover_ack(MigrationIncomingState
*mis
)
772 return migrate_send_rp_message(mis
, MIG_RP_MSG_SWITCHOVER_ACK
, 0, NULL
);
776 * Send a 'SHUT' message on the return channel with the given value
777 * to indicate that we've finished with the RP. Non-0 value indicates
780 void migrate_send_rp_shut(MigrationIncomingState
*mis
,
785 buf
= cpu_to_be32(value
);
786 migrate_send_rp_message(mis
, MIG_RP_MSG_SHUT
, sizeof(buf
), &buf
);
790 * Send a 'PONG' message on the return channel with the given value
791 * (normally in response to a 'PING')
793 void migrate_send_rp_pong(MigrationIncomingState
*mis
,
798 buf
= cpu_to_be32(value
);
799 migrate_send_rp_message(mis
, MIG_RP_MSG_PONG
, sizeof(buf
), &buf
);
802 void migrate_send_rp_recv_bitmap(MigrationIncomingState
*mis
,
810 * First, we send the header part. It contains only the len of
811 * idstr, and the idstr itself.
813 len
= strlen(block_name
);
815 memcpy(buf
+ 1, block_name
, len
);
817 if (mis
->state
!= MIGRATION_STATUS_POSTCOPY_RECOVER
) {
818 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
823 migrate_send_rp_message(mis
, MIG_RP_MSG_RECV_BITMAP
, len
+ 1, buf
);
826 * Next, we dump the received bitmap to the stream.
828 * TODO: currently we are safe since we are the only one that is
829 * using the to_src_file handle (fault thread is still paused),
830 * and it's ok even not taking the mutex. However the best way is
831 * to take the lock before sending the message header, and release
832 * the lock after sending the bitmap.
834 qemu_mutex_lock(&mis
->rp_mutex
);
835 res
= ramblock_recv_bitmap_send(mis
->to_src_file
, block_name
);
836 qemu_mutex_unlock(&mis
->rp_mutex
);
838 trace_migrate_send_rp_recv_bitmap(block_name
, res
);
841 void migrate_send_rp_resume_ack(MigrationIncomingState
*mis
, uint32_t value
)
845 buf
= cpu_to_be32(value
);
846 migrate_send_rp_message(mis
, MIG_RP_MSG_RESUME_ACK
, sizeof(buf
), &buf
);
850 * Return true if we're already in the middle of a migration
851 * (i.e. any of the active or setup states)
853 bool migration_is_setup_or_active(int state
)
856 case MIGRATION_STATUS_ACTIVE
:
857 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
858 case MIGRATION_STATUS_POSTCOPY_PAUSED
:
859 case MIGRATION_STATUS_POSTCOPY_RECOVER
:
860 case MIGRATION_STATUS_SETUP
:
861 case MIGRATION_STATUS_PRE_SWITCHOVER
:
862 case MIGRATION_STATUS_DEVICE
:
863 case MIGRATION_STATUS_WAIT_UNPLUG
:
864 case MIGRATION_STATUS_COLO
:
873 bool migration_is_running(int state
)
876 case MIGRATION_STATUS_ACTIVE
:
877 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
878 case MIGRATION_STATUS_POSTCOPY_PAUSED
:
879 case MIGRATION_STATUS_POSTCOPY_RECOVER
:
880 case MIGRATION_STATUS_SETUP
:
881 case MIGRATION_STATUS_PRE_SWITCHOVER
:
882 case MIGRATION_STATUS_DEVICE
:
883 case MIGRATION_STATUS_WAIT_UNPLUG
:
884 case MIGRATION_STATUS_CANCELLING
:
893 static bool migrate_show_downtime(MigrationState
*s
)
895 return (s
->state
== MIGRATION_STATUS_COMPLETED
) || migration_in_postcopy();
898 static void populate_time_info(MigrationInfo
*info
, MigrationState
*s
)
900 info
->has_status
= true;
901 info
->has_setup_time
= true;
902 info
->setup_time
= s
->setup_time
;
904 if (s
->state
== MIGRATION_STATUS_COMPLETED
) {
905 info
->has_total_time
= true;
906 info
->total_time
= s
->total_time
;
908 info
->has_total_time
= true;
909 info
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) -
913 if (migrate_show_downtime(s
)) {
914 info
->has_downtime
= true;
915 info
->downtime
= s
->downtime
;
917 info
->has_expected_downtime
= true;
918 info
->expected_downtime
= s
->expected_downtime
;
922 static void populate_ram_info(MigrationInfo
*info
, MigrationState
*s
)
924 size_t page_size
= qemu_target_page_size();
926 info
->ram
= g_malloc0(sizeof(*info
->ram
));
927 info
->ram
->transferred
= stat64_get(&mig_stats
.transferred
);
928 info
->ram
->total
= ram_bytes_total();
929 info
->ram
->duplicate
= stat64_get(&mig_stats
.zero_pages
);
930 /* legacy value. It is not used anymore */
931 info
->ram
->skipped
= 0;
932 info
->ram
->normal
= stat64_get(&mig_stats
.normal_pages
);
933 info
->ram
->normal_bytes
= info
->ram
->normal
* page_size
;
934 info
->ram
->mbps
= s
->mbps
;
935 info
->ram
->dirty_sync_count
=
936 stat64_get(&mig_stats
.dirty_sync_count
);
937 info
->ram
->dirty_sync_missed_zero_copy
=
938 stat64_get(&mig_stats
.dirty_sync_missed_zero_copy
);
939 info
->ram
->postcopy_requests
=
940 stat64_get(&mig_stats
.postcopy_requests
);
941 info
->ram
->page_size
= page_size
;
942 info
->ram
->multifd_bytes
= stat64_get(&mig_stats
.multifd_bytes
);
943 info
->ram
->pages_per_second
= s
->pages_per_second
;
944 info
->ram
->precopy_bytes
= stat64_get(&mig_stats
.precopy_bytes
);
945 info
->ram
->downtime_bytes
= stat64_get(&mig_stats
.downtime_bytes
);
946 info
->ram
->postcopy_bytes
= stat64_get(&mig_stats
.postcopy_bytes
);
948 if (migrate_xbzrle()) {
949 info
->xbzrle_cache
= g_malloc0(sizeof(*info
->xbzrle_cache
));
950 info
->xbzrle_cache
->cache_size
= migrate_xbzrle_cache_size();
951 info
->xbzrle_cache
->bytes
= xbzrle_counters
.bytes
;
952 info
->xbzrle_cache
->pages
= xbzrle_counters
.pages
;
953 info
->xbzrle_cache
->cache_miss
= xbzrle_counters
.cache_miss
;
954 info
->xbzrle_cache
->cache_miss_rate
= xbzrle_counters
.cache_miss_rate
;
955 info
->xbzrle_cache
->encoding_rate
= xbzrle_counters
.encoding_rate
;
956 info
->xbzrle_cache
->overflow
= xbzrle_counters
.overflow
;
959 if (migrate_compress()) {
960 info
->compression
= g_malloc0(sizeof(*info
->compression
));
961 info
->compression
->pages
= compression_counters
.pages
;
962 info
->compression
->busy
= compression_counters
.busy
;
963 info
->compression
->busy_rate
= compression_counters
.busy_rate
;
964 info
->compression
->compressed_size
=
965 compression_counters
.compressed_size
;
966 info
->compression
->compression_rate
=
967 compression_counters
.compression_rate
;
970 if (cpu_throttle_active()) {
971 info
->has_cpu_throttle_percentage
= true;
972 info
->cpu_throttle_percentage
= cpu_throttle_get_percentage();
975 if (s
->state
!= MIGRATION_STATUS_COMPLETED
) {
976 info
->ram
->remaining
= ram_bytes_remaining();
977 info
->ram
->dirty_pages_rate
=
978 stat64_get(&mig_stats
.dirty_pages_rate
);
981 if (migrate_dirty_limit() && dirtylimit_in_service()) {
982 info
->has_dirty_limit_throttle_time_per_round
= true;
983 info
->dirty_limit_throttle_time_per_round
=
984 dirtylimit_throttle_time_per_round();
986 info
->has_dirty_limit_ring_full_time
= true;
987 info
->dirty_limit_ring_full_time
= dirtylimit_ring_full_time();
991 static void populate_disk_info(MigrationInfo
*info
)
993 if (blk_mig_active()) {
994 info
->disk
= g_malloc0(sizeof(*info
->disk
));
995 info
->disk
->transferred
= blk_mig_bytes_transferred();
996 info
->disk
->remaining
= blk_mig_bytes_remaining();
997 info
->disk
->total
= blk_mig_bytes_total();
1001 static void fill_source_migration_info(MigrationInfo
*info
)
1003 MigrationState
*s
= migrate_get_current();
1004 int state
= qatomic_read(&s
->state
);
1005 GSList
*cur_blocker
= migration_blockers
;
1007 info
->blocked_reasons
= NULL
;
1010 * There are two types of reasons a migration might be blocked;
1011 * a) devices marked in VMState as non-migratable, and
1012 * b) Explicit migration blockers
1013 * We need to add both of them here.
1015 qemu_savevm_non_migratable_list(&info
->blocked_reasons
);
1017 while (cur_blocker
) {
1018 QAPI_LIST_PREPEND(info
->blocked_reasons
,
1019 g_strdup(error_get_pretty(cur_blocker
->data
)));
1020 cur_blocker
= g_slist_next(cur_blocker
);
1022 info
->has_blocked_reasons
= info
->blocked_reasons
!= NULL
;
1025 case MIGRATION_STATUS_NONE
:
1026 /* no migration has happened ever */
1027 /* do not overwrite destination migration status */
1029 case MIGRATION_STATUS_SETUP
:
1030 info
->has_status
= true;
1031 info
->has_total_time
= false;
1033 case MIGRATION_STATUS_ACTIVE
:
1034 case MIGRATION_STATUS_CANCELLING
:
1035 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
1036 case MIGRATION_STATUS_PRE_SWITCHOVER
:
1037 case MIGRATION_STATUS_DEVICE
:
1038 case MIGRATION_STATUS_POSTCOPY_PAUSED
:
1039 case MIGRATION_STATUS_POSTCOPY_RECOVER
:
1040 /* TODO add some postcopy stats */
1041 populate_time_info(info
, s
);
1042 populate_ram_info(info
, s
);
1043 populate_disk_info(info
);
1044 migration_populate_vfio_info(info
);
1046 case MIGRATION_STATUS_COLO
:
1047 info
->has_status
= true;
1048 /* TODO: display COLO specific information (checkpoint info etc.) */
1050 case MIGRATION_STATUS_COMPLETED
:
1051 populate_time_info(info
, s
);
1052 populate_ram_info(info
, s
);
1053 migration_populate_vfio_info(info
);
1055 case MIGRATION_STATUS_FAILED
:
1056 info
->has_status
= true;
1058 info
->error_desc
= g_strdup(error_get_pretty(s
->error
));
1061 case MIGRATION_STATUS_CANCELLED
:
1062 info
->has_status
= true;
1064 case MIGRATION_STATUS_WAIT_UNPLUG
:
1065 info
->has_status
= true;
1068 info
->status
= state
;
1071 static void fill_destination_migration_info(MigrationInfo
*info
)
1073 MigrationIncomingState
*mis
= migration_incoming_get_current();
1075 if (mis
->socket_address_list
) {
1076 info
->has_socket_address
= true;
1077 info
->socket_address
=
1078 QAPI_CLONE(SocketAddressList
, mis
->socket_address_list
);
1081 switch (mis
->state
) {
1082 case MIGRATION_STATUS_NONE
:
1084 case MIGRATION_STATUS_SETUP
:
1085 case MIGRATION_STATUS_CANCELLING
:
1086 case MIGRATION_STATUS_CANCELLED
:
1087 case MIGRATION_STATUS_ACTIVE
:
1088 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
1089 case MIGRATION_STATUS_POSTCOPY_PAUSED
:
1090 case MIGRATION_STATUS_POSTCOPY_RECOVER
:
1091 case MIGRATION_STATUS_FAILED
:
1092 case MIGRATION_STATUS_COLO
:
1093 info
->has_status
= true;
1095 case MIGRATION_STATUS_COMPLETED
:
1096 info
->has_status
= true;
1097 fill_destination_postcopy_migration_info(info
);
1100 info
->status
= mis
->state
;
1103 MigrationInfo
*qmp_query_migrate(Error
**errp
)
1105 MigrationInfo
*info
= g_malloc0(sizeof(*info
));
1107 fill_destination_migration_info(info
);
1108 fill_source_migration_info(info
);
1113 void qmp_migrate_start_postcopy(Error
**errp
)
1115 MigrationState
*s
= migrate_get_current();
1117 if (!migrate_postcopy()) {
1118 error_setg(errp
, "Enable postcopy with migrate_set_capability before"
1119 " the start of migration");
1123 if (s
->state
== MIGRATION_STATUS_NONE
) {
1124 error_setg(errp
, "Postcopy must be started after migration has been"
1129 * we don't error if migration has finished since that would be racy
1130 * with issuing this command.
1132 qatomic_set(&s
->start_postcopy
, true);
1135 /* shared migration helpers */
1137 void migrate_set_state(int *state
, int old_state
, int new_state
)
1139 assert(new_state
< MIGRATION_STATUS__MAX
);
1140 if (qatomic_cmpxchg(state
, old_state
, new_state
) == old_state
) {
1141 trace_migrate_set_state(MigrationStatus_str(new_state
));
1142 migrate_generate_event(new_state
);
1146 static void migrate_fd_cleanup(MigrationState
*s
)
1148 qemu_bh_delete(s
->cleanup_bh
);
1149 s
->cleanup_bh
= NULL
;
1151 g_free(s
->hostname
);
1153 json_writer_free(s
->vmdesc
);
1156 qemu_savevm_state_cleanup();
1158 if (s
->to_dst_file
) {
1161 trace_migrate_fd_cleanup();
1162 qemu_mutex_unlock_iothread();
1163 if (s
->migration_thread_running
) {
1164 qemu_thread_join(&s
->thread
);
1165 s
->migration_thread_running
= false;
1167 qemu_mutex_lock_iothread();
1169 multifd_save_cleanup();
1170 qemu_mutex_lock(&s
->qemu_file_lock
);
1171 tmp
= s
->to_dst_file
;
1172 s
->to_dst_file
= NULL
;
1173 qemu_mutex_unlock(&s
->qemu_file_lock
);
1175 * Close the file handle without the lock to make sure the
1176 * critical section won't block for long.
1178 migration_ioc_unregister_yank_from_file(tmp
);
1183 * We already cleaned up to_dst_file, so errors from the return
1184 * path might be due to that, ignore them.
1186 await_return_path_close_on_source(s
);
1188 assert(!migration_is_active(s
));
1190 if (s
->state
== MIGRATION_STATUS_CANCELLING
) {
1191 migrate_set_state(&s
->state
, MIGRATION_STATUS_CANCELLING
,
1192 MIGRATION_STATUS_CANCELLED
);
1196 /* It is used on info migrate. We can't free it */
1197 error_report_err(error_copy(s
->error
));
1199 notifier_list_notify(&migration_state_notifiers
, s
);
1200 block_cleanup_parameters();
1201 yank_unregister_instance(MIGRATION_YANK_INSTANCE
);
1204 static void migrate_fd_cleanup_schedule(MigrationState
*s
)
1207 * Ref the state for bh, because it may be called when
1208 * there're already no other refs
1210 object_ref(OBJECT(s
));
1211 qemu_bh_schedule(s
->cleanup_bh
);
1214 static void migrate_fd_cleanup_bh(void *opaque
)
1216 MigrationState
*s
= opaque
;
1217 migrate_fd_cleanup(s
);
1218 object_unref(OBJECT(s
));
1221 void migrate_set_error(MigrationState
*s
, const Error
*error
)
1223 QEMU_LOCK_GUARD(&s
->error_mutex
);
1225 s
->error
= error_copy(error
);
1229 static void migrate_error_free(MigrationState
*s
)
1231 QEMU_LOCK_GUARD(&s
->error_mutex
);
1233 error_free(s
->error
);
1238 static void migrate_fd_error(MigrationState
*s
, const Error
*error
)
1240 trace_migrate_fd_error(error_get_pretty(error
));
1241 assert(s
->to_dst_file
== NULL
);
1242 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
1243 MIGRATION_STATUS_FAILED
);
1244 migrate_set_error(s
, error
);
1247 static void migrate_fd_cancel(MigrationState
*s
)
1251 trace_migrate_fd_cancel();
1253 WITH_QEMU_LOCK_GUARD(&s
->qemu_file_lock
) {
1254 if (s
->rp_state
.from_dst_file
) {
1255 /* shutdown the rp socket, so causing the rp thread to shutdown */
1256 qemu_file_shutdown(s
->rp_state
.from_dst_file
);
1261 old_state
= s
->state
;
1262 if (!migration_is_running(old_state
)) {
1265 /* If the migration is paused, kick it out of the pause */
1266 if (old_state
== MIGRATION_STATUS_PRE_SWITCHOVER
) {
1267 qemu_sem_post(&s
->pause_sem
);
1269 migrate_set_state(&s
->state
, old_state
, MIGRATION_STATUS_CANCELLING
);
1270 } while (s
->state
!= MIGRATION_STATUS_CANCELLING
);
1273 * If we're unlucky the migration code might be stuck somewhere in a
1274 * send/write while the network has failed and is waiting to timeout;
1275 * if we've got shutdown(2) available then we can force it to quit.
1277 if (s
->state
== MIGRATION_STATUS_CANCELLING
) {
1278 WITH_QEMU_LOCK_GUARD(&s
->qemu_file_lock
) {
1279 if (s
->to_dst_file
) {
1280 qemu_file_shutdown(s
->to_dst_file
);
1284 if (s
->state
== MIGRATION_STATUS_CANCELLING
&& s
->block_inactive
) {
1285 Error
*local_err
= NULL
;
1287 bdrv_activate_all(&local_err
);
1289 error_report_err(local_err
);
1291 s
->block_inactive
= false;
1296 void add_migration_state_change_notifier(Notifier
*notify
)
1298 notifier_list_add(&migration_state_notifiers
, notify
);
1301 void remove_migration_state_change_notifier(Notifier
*notify
)
1303 notifier_remove(notify
);
1306 bool migration_in_setup(MigrationState
*s
)
1308 return s
->state
== MIGRATION_STATUS_SETUP
;
1311 bool migration_has_finished(MigrationState
*s
)
1313 return s
->state
== MIGRATION_STATUS_COMPLETED
;
1316 bool migration_has_failed(MigrationState
*s
)
1318 return (s
->state
== MIGRATION_STATUS_CANCELLED
||
1319 s
->state
== MIGRATION_STATUS_FAILED
);
1322 bool migration_in_postcopy(void)
1324 MigrationState
*s
= migrate_get_current();
1327 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
1328 case MIGRATION_STATUS_POSTCOPY_PAUSED
:
1329 case MIGRATION_STATUS_POSTCOPY_RECOVER
:
1336 bool migration_in_postcopy_after_devices(MigrationState
*s
)
1338 return migration_in_postcopy() && s
->postcopy_after_devices
;
1341 bool migration_in_incoming_postcopy(void)
1343 PostcopyState ps
= postcopy_state_get();
1345 return ps
>= POSTCOPY_INCOMING_DISCARD
&& ps
< POSTCOPY_INCOMING_END
;
1348 bool migration_incoming_postcopy_advised(void)
1350 PostcopyState ps
= postcopy_state_get();
1352 return ps
>= POSTCOPY_INCOMING_ADVISE
&& ps
< POSTCOPY_INCOMING_END
;
1355 bool migration_in_bg_snapshot(void)
1357 MigrationState
*s
= migrate_get_current();
1359 return migrate_background_snapshot() &&
1360 migration_is_setup_or_active(s
->state
);
1363 bool migration_is_idle(void)
1365 MigrationState
*s
= current_migration
;
1372 case MIGRATION_STATUS_NONE
:
1373 case MIGRATION_STATUS_CANCELLED
:
1374 case MIGRATION_STATUS_COMPLETED
:
1375 case MIGRATION_STATUS_FAILED
:
1377 case MIGRATION_STATUS_SETUP
:
1378 case MIGRATION_STATUS_CANCELLING
:
1379 case MIGRATION_STATUS_ACTIVE
:
1380 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
1381 case MIGRATION_STATUS_COLO
:
1382 case MIGRATION_STATUS_PRE_SWITCHOVER
:
1383 case MIGRATION_STATUS_DEVICE
:
1384 case MIGRATION_STATUS_WAIT_UNPLUG
:
1386 case MIGRATION_STATUS__MAX
:
1387 g_assert_not_reached();
1393 bool migration_is_active(MigrationState
*s
)
1395 return (s
->state
== MIGRATION_STATUS_ACTIVE
||
1396 s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
);
1399 int migrate_init(MigrationState
*s
, Error
**errp
)
1403 ret
= qemu_savevm_state_prepare(errp
);
1409 * Reinitialise all migration state, except
1410 * parameters/capabilities that the user set, and
1415 s
->to_dst_file
= NULL
;
1416 s
->state
= MIGRATION_STATUS_NONE
;
1417 s
->rp_state
.from_dst_file
= NULL
;
1418 s
->rp_state
.error
= false;
1420 s
->pages_per_second
= 0.0;
1422 s
->expected_downtime
= 0;
1424 s
->start_postcopy
= false;
1425 s
->postcopy_after_devices
= false;
1426 s
->migration_thread_running
= false;
1427 error_free(s
->error
);
1431 migrate_set_state(&s
->state
, MIGRATION_STATUS_NONE
, MIGRATION_STATUS_SETUP
);
1433 s
->start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1435 s
->vm_old_state
= -1;
1436 s
->iteration_initial_bytes
= 0;
1437 s
->threshold_size
= 0;
1438 s
->switchover_acked
= false;
1440 * set mig_stats compression_counters memory to zero for a
1443 memset(&mig_stats
, 0, sizeof(mig_stats
));
1444 memset(&compression_counters
, 0, sizeof(compression_counters
));
1445 migration_reset_vfio_bytes_transferred();
1450 int migrate_add_blocker_internal(Error
*reason
, Error
**errp
)
1452 /* Snapshots are similar to migrations, so check RUN_STATE_SAVE_VM too. */
1453 if (runstate_check(RUN_STATE_SAVE_VM
) || !migration_is_idle()) {
1454 error_propagate_prepend(errp
, error_copy(reason
),
1455 "disallowing migration blocker "
1456 "(migration/snapshot in progress) for: ");
1460 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
1464 int migrate_add_blocker(Error
*reason
, Error
**errp
)
1466 if (only_migratable
) {
1467 error_propagate_prepend(errp
, error_copy(reason
),
1468 "disallowing migration blocker "
1469 "(--only-migratable) for: ");
1473 return migrate_add_blocker_internal(reason
, errp
);
1476 void migrate_del_blocker(Error
*reason
)
1478 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
1481 void qmp_migrate_incoming(const char *uri
, Error
**errp
)
1483 Error
*local_err
= NULL
;
1484 static bool once
= true;
1487 error_setg(errp
, "The incoming migration has already been started");
1490 if (!runstate_check(RUN_STATE_INMIGRATE
)) {
1491 error_setg(errp
, "'-incoming' was not specified on the command line");
1495 if (!yank_register_instance(MIGRATION_YANK_INSTANCE
, errp
)) {
1499 qemu_start_incoming_migration(uri
, &local_err
);
1502 yank_unregister_instance(MIGRATION_YANK_INSTANCE
);
1503 error_propagate(errp
, local_err
);
1510 void qmp_migrate_recover(const char *uri
, Error
**errp
)
1512 MigrationIncomingState
*mis
= migration_incoming_get_current();
1515 * Don't even bother to use ERRP_GUARD() as it _must_ always be set by
1516 * callers (no one should ignore a recover failure); if there is, it's a
1517 * programming error.
1521 if (mis
->state
!= MIGRATION_STATUS_POSTCOPY_PAUSED
) {
1522 error_setg(errp
, "Migrate recover can only be run "
1523 "when postcopy is paused.");
1527 /* If there's an existing transport, release it */
1528 migration_incoming_transport_cleanup(mis
);
1531 * Note that this call will never start a real migration; it will
1532 * only re-setup the migration stream and poke existing migration
1533 * to continue using that newly established channel.
1535 qemu_start_incoming_migration(uri
, errp
);
1538 void qmp_migrate_pause(Error
**errp
)
1540 MigrationState
*ms
= migrate_get_current();
1541 MigrationIncomingState
*mis
= migration_incoming_get_current();
1544 if (ms
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
1545 /* Source side, during postcopy */
1546 qemu_mutex_lock(&ms
->qemu_file_lock
);
1547 if (ms
->to_dst_file
) {
1548 ret
= qemu_file_shutdown(ms
->to_dst_file
);
1550 qemu_mutex_unlock(&ms
->qemu_file_lock
);
1552 error_setg(errp
, "Failed to pause source migration");
1557 if (mis
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
1558 ret
= qemu_file_shutdown(mis
->from_src_file
);
1560 error_setg(errp
, "Failed to pause destination migration");
1565 error_setg(errp
, "migrate-pause is currently only supported "
1566 "during postcopy-active state");
1569 bool migration_is_blocked(Error
**errp
)
1571 if (qemu_savevm_state_blocked(errp
)) {
1575 if (migration_blockers
) {
1576 error_propagate(errp
, error_copy(migration_blockers
->data
));
1583 /* Returns true if continue to migrate, or false if error detected */
1584 static bool migrate_prepare(MigrationState
*s
, bool blk
, bool blk_inc
,
1585 bool resume
, Error
**errp
)
1587 Error
*local_err
= NULL
;
1590 if (s
->state
!= MIGRATION_STATUS_POSTCOPY_PAUSED
) {
1591 error_setg(errp
, "Cannot resume if there is no "
1592 "paused migration");
1597 * Postcopy recovery won't work well with release-ram
1598 * capability since release-ram will drop the page buffer as
1599 * long as the page is put into the send buffer. So if there
1600 * is a network failure happened, any page buffers that have
1601 * not yet reached the destination VM but have already been
1602 * sent from the source VM will be lost forever. Let's refuse
1603 * the client from resuming such a postcopy migration.
1604 * Luckily release-ram was designed to only be used when src
1605 * and destination VMs are on the same host, so it should be
1608 if (migrate_release_ram()) {
1609 error_setg(errp
, "Postcopy recovery cannot work "
1610 "when release-ram capability is set");
1614 /* This is a resume, skip init status */
1618 if (migration_is_running(s
->state
)) {
1619 error_setg(errp
, QERR_MIGRATION_ACTIVE
);
1623 if (runstate_check(RUN_STATE_INMIGRATE
)) {
1624 error_setg(errp
, "Guest is waiting for an incoming migration");
1628 if (runstate_check(RUN_STATE_POSTMIGRATE
)) {
1629 error_setg(errp
, "Can't migrate the vm that was paused due to "
1630 "previous migration");
1634 if (migration_is_blocked(errp
)) {
1638 if (blk
|| blk_inc
) {
1639 if (migrate_colo()) {
1640 error_setg(errp
, "No disk migration is required in COLO mode");
1643 if (migrate_block() || migrate_block_incremental()) {
1644 error_setg(errp
, "Command options are incompatible with "
1645 "current migration capabilities");
1648 if (!migrate_cap_set(MIGRATION_CAPABILITY_BLOCK
, true, &local_err
)) {
1649 error_propagate(errp
, local_err
);
1652 s
->must_remove_block_options
= true;
1656 migrate_set_block_incremental(true);
1659 if (migrate_init(s
, errp
)) {
1666 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
1667 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
1668 bool has_resume
, bool resume
, Error
**errp
)
1670 bool resume_requested
;
1671 Error
*local_err
= NULL
;
1672 MigrationState
*s
= migrate_get_current();
1673 const char *p
= NULL
;
1675 /* URI is not suitable for migration? */
1676 if (!migration_channels_and_uri_compatible(uri
, errp
)) {
1680 resume_requested
= has_resume
&& resume
;
1681 if (!migrate_prepare(s
, has_blk
&& blk
, has_inc
&& inc
,
1682 resume_requested
, errp
)) {
1683 /* Error detected, put into errp */
1687 if (!resume_requested
) {
1688 if (!yank_register_instance(MIGRATION_YANK_INSTANCE
, errp
)) {
1693 if (strstart(uri
, "tcp:", &p
) ||
1694 strstart(uri
, "unix:", NULL
) ||
1695 strstart(uri
, "vsock:", NULL
)) {
1696 socket_start_outgoing_migration(s
, p
? p
: uri
, &local_err
);
1698 } else if (strstart(uri
, "rdma:", &p
)) {
1699 rdma_start_outgoing_migration(s
, p
, &local_err
);
1701 } else if (strstart(uri
, "exec:", &p
)) {
1702 exec_start_outgoing_migration(s
, p
, &local_err
);
1703 } else if (strstart(uri
, "fd:", &p
)) {
1704 fd_start_outgoing_migration(s
, p
, &local_err
);
1706 error_setg(&local_err
, QERR_INVALID_PARAMETER_VALUE
, "uri",
1707 "a valid migration protocol");
1708 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
1709 MIGRATION_STATUS_FAILED
);
1710 block_cleanup_parameters();
1714 if (!resume_requested
) {
1715 yank_unregister_instance(MIGRATION_YANK_INSTANCE
);
1717 migrate_fd_error(s
, local_err
);
1718 error_propagate(errp
, local_err
);
1723 void qmp_migrate_cancel(Error
**errp
)
1725 migration_cancel(NULL
);
1728 void qmp_migrate_continue(MigrationStatus state
, Error
**errp
)
1730 MigrationState
*s
= migrate_get_current();
1731 if (s
->state
!= state
) {
1732 error_setg(errp
, "Migration not in expected state: %s",
1733 MigrationStatus_str(s
->state
));
1736 qemu_sem_post(&s
->pause_sem
);
1739 /* migration thread support */
1741 * Something bad happened to the RP stream, mark an error
1742 * The caller shall print or trace something to indicate why
1744 static void mark_source_rp_bad(MigrationState
*s
)
1746 s
->rp_state
.error
= true;
1749 static struct rp_cmd_args
{
1750 ssize_t len
; /* -1 = variable */
1753 [MIG_RP_MSG_INVALID
] = { .len
= -1, .name
= "INVALID" },
1754 [MIG_RP_MSG_SHUT
] = { .len
= 4, .name
= "SHUT" },
1755 [MIG_RP_MSG_PONG
] = { .len
= 4, .name
= "PONG" },
1756 [MIG_RP_MSG_REQ_PAGES
] = { .len
= 12, .name
= "REQ_PAGES" },
1757 [MIG_RP_MSG_REQ_PAGES_ID
] = { .len
= -1, .name
= "REQ_PAGES_ID" },
1758 [MIG_RP_MSG_RECV_BITMAP
] = { .len
= -1, .name
= "RECV_BITMAP" },
1759 [MIG_RP_MSG_RESUME_ACK
] = { .len
= 4, .name
= "RESUME_ACK" },
1760 [MIG_RP_MSG_SWITCHOVER_ACK
] = { .len
= 0, .name
= "SWITCHOVER_ACK" },
1761 [MIG_RP_MSG_MAX
] = { .len
= -1, .name
= "MAX" },
1765 * Process a request for pages received on the return path,
1766 * We're allowed to send more than requested (e.g. to round to our page size)
1767 * and we don't need to send pages that have already been sent.
1769 static void migrate_handle_rp_req_pages(MigrationState
*ms
, const char* rbname
,
1770 ram_addr_t start
, size_t len
)
1772 long our_host_ps
= qemu_real_host_page_size();
1774 trace_migrate_handle_rp_req_pages(rbname
, start
, len
);
1777 * Since we currently insist on matching page sizes, just sanity check
1778 * we're being asked for whole host pages.
1780 if (!QEMU_IS_ALIGNED(start
, our_host_ps
) ||
1781 !QEMU_IS_ALIGNED(len
, our_host_ps
)) {
1782 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1783 " len: %zd", __func__
, start
, len
);
1784 mark_source_rp_bad(ms
);
1788 if (ram_save_queue_pages(rbname
, start
, len
)) {
1789 mark_source_rp_bad(ms
);
1793 static int migrate_handle_rp_recv_bitmap(MigrationState
*s
, char *block_name
)
1795 RAMBlock
*block
= qemu_ram_block_by_name(block_name
);
1798 error_report("%s: invalid block name '%s'", __func__
, block_name
);
1802 /* Fetch the received bitmap and refresh the dirty bitmap */
1803 return ram_dirty_bitmap_reload(s
, block
);
1806 static int migrate_handle_rp_resume_ack(MigrationState
*s
, uint32_t value
)
1808 trace_source_return_path_thread_resume_ack(value
);
1810 if (value
!= MIGRATION_RESUME_ACK_VALUE
) {
1811 error_report("%s: illegal resume_ack value %"PRIu32
,
1816 /* Now both sides are active. */
1817 migrate_set_state(&s
->state
, MIGRATION_STATUS_POSTCOPY_RECOVER
,
1818 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
1820 /* Notify send thread that time to continue send pages */
1821 qemu_sem_post(&s
->rp_state
.rp_sem
);
1827 * Release ms->rp_state.from_dst_file (and postcopy_qemufile_src if
1828 * existed) in a safe way.
1830 static void migration_release_dst_files(MigrationState
*ms
)
1834 WITH_QEMU_LOCK_GUARD(&ms
->qemu_file_lock
) {
1836 * Reset the from_dst_file pointer first before releasing it, as we
1837 * can't block within lock section
1839 file
= ms
->rp_state
.from_dst_file
;
1840 ms
->rp_state
.from_dst_file
= NULL
;
1844 * Do the same to postcopy fast path socket too if there is. No
1845 * locking needed because this qemufile should only be managed by
1846 * return path thread.
1848 if (ms
->postcopy_qemufile_src
) {
1849 migration_ioc_unregister_yank_from_file(ms
->postcopy_qemufile_src
);
1850 qemu_file_shutdown(ms
->postcopy_qemufile_src
);
1851 qemu_fclose(ms
->postcopy_qemufile_src
);
1852 ms
->postcopy_qemufile_src
= NULL
;
1859 * Handles messages sent on the return path towards the source VM
1862 static void *source_return_path_thread(void *opaque
)
1864 MigrationState
*ms
= opaque
;
1865 QEMUFile
*rp
= ms
->rp_state
.from_dst_file
;
1866 uint16_t header_len
, header_type
;
1868 uint32_t tmp32
, sibling_error
;
1869 ram_addr_t start
= 0; /* =0 to silence warning */
1870 size_t len
= 0, expected_len
;
1873 trace_source_return_path_thread_entry();
1874 rcu_register_thread();
1876 while (!ms
->rp_state
.error
&& !qemu_file_get_error(rp
) &&
1877 migration_is_setup_or_active(ms
->state
)) {
1878 trace_source_return_path_thread_loop_top();
1879 header_type
= qemu_get_be16(rp
);
1880 header_len
= qemu_get_be16(rp
);
1882 if (qemu_file_get_error(rp
)) {
1883 mark_source_rp_bad(ms
);
1887 if (header_type
>= MIG_RP_MSG_MAX
||
1888 header_type
== MIG_RP_MSG_INVALID
) {
1889 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1890 header_type
, header_len
);
1891 mark_source_rp_bad(ms
);
1895 if ((rp_cmd_args
[header_type
].len
!= -1 &&
1896 header_len
!= rp_cmd_args
[header_type
].len
) ||
1897 header_len
> sizeof(buf
)) {
1898 error_report("RP: Received '%s' message (0x%04x) with"
1899 "incorrect length %d expecting %zu",
1900 rp_cmd_args
[header_type
].name
, header_type
, header_len
,
1901 (size_t)rp_cmd_args
[header_type
].len
);
1902 mark_source_rp_bad(ms
);
1906 /* We know we've got a valid header by this point */
1907 res
= qemu_get_buffer(rp
, buf
, header_len
);
1908 if (res
!= header_len
) {
1909 error_report("RP: Failed reading data for message 0x%04x"
1910 " read %d expected %d",
1911 header_type
, res
, header_len
);
1912 mark_source_rp_bad(ms
);
1916 /* OK, we have the message and the data */
1917 switch (header_type
) {
1918 case MIG_RP_MSG_SHUT
:
1919 sibling_error
= ldl_be_p(buf
);
1920 trace_source_return_path_thread_shut(sibling_error
);
1921 if (sibling_error
) {
1922 error_report("RP: Sibling indicated error %d", sibling_error
);
1923 mark_source_rp_bad(ms
);
1926 * We'll let the main thread deal with closing the RP
1927 * we could do a shutdown(2) on it, but we're the only user
1928 * anyway, so there's nothing gained.
1932 case MIG_RP_MSG_PONG
:
1933 tmp32
= ldl_be_p(buf
);
1934 trace_source_return_path_thread_pong(tmp32
);
1935 qemu_sem_post(&ms
->rp_state
.rp_pong_acks
);
1938 case MIG_RP_MSG_REQ_PAGES
:
1939 start
= ldq_be_p(buf
);
1940 len
= ldl_be_p(buf
+ 8);
1941 migrate_handle_rp_req_pages(ms
, NULL
, start
, len
);
1944 case MIG_RP_MSG_REQ_PAGES_ID
:
1945 expected_len
= 12 + 1; /* header + termination */
1947 if (header_len
>= expected_len
) {
1948 start
= ldq_be_p(buf
);
1949 len
= ldl_be_p(buf
+ 8);
1950 /* Now we expect an idstr */
1951 tmp32
= buf
[12]; /* Length of the following idstr */
1952 buf
[13 + tmp32
] = '\0';
1953 expected_len
+= tmp32
;
1955 if (header_len
!= expected_len
) {
1956 error_report("RP: Req_Page_id with length %d expecting %zd",
1957 header_len
, expected_len
);
1958 mark_source_rp_bad(ms
);
1961 migrate_handle_rp_req_pages(ms
, (char *)&buf
[13], start
, len
);
1964 case MIG_RP_MSG_RECV_BITMAP
:
1965 if (header_len
< 1) {
1966 error_report("%s: missing block name", __func__
);
1967 mark_source_rp_bad(ms
);
1970 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
1971 buf
[buf
[0] + 1] = '\0';
1972 if (migrate_handle_rp_recv_bitmap(ms
, (char *)(buf
+ 1))) {
1973 mark_source_rp_bad(ms
);
1978 case MIG_RP_MSG_RESUME_ACK
:
1979 tmp32
= ldl_be_p(buf
);
1980 if (migrate_handle_rp_resume_ack(ms
, tmp32
)) {
1981 mark_source_rp_bad(ms
);
1986 case MIG_RP_MSG_SWITCHOVER_ACK
:
1987 ms
->switchover_acked
= true;
1988 trace_source_return_path_thread_switchover_acked();
1997 if (qemu_file_get_error(rp
)) {
1998 trace_source_return_path_thread_bad_end();
1999 mark_source_rp_bad(ms
);
2002 trace_source_return_path_thread_end();
2003 rcu_unregister_thread();
2007 static int open_return_path_on_source(MigrationState
*ms
)
2009 ms
->rp_state
.from_dst_file
= qemu_file_get_return_path(ms
->to_dst_file
);
2010 if (!ms
->rp_state
.from_dst_file
) {
2014 trace_open_return_path_on_source();
2016 qemu_thread_create(&ms
->rp_state
.rp_thread
, "return path",
2017 source_return_path_thread
, ms
, QEMU_THREAD_JOINABLE
);
2018 ms
->rp_state
.rp_thread_created
= true;
2020 trace_open_return_path_on_source_continue();
2025 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2026 static int await_return_path_close_on_source(MigrationState
*ms
)
2030 if (!ms
->rp_state
.rp_thread_created
) {
2034 trace_migration_return_path_end_before();
2037 * If this is a normal exit then the destination will send a SHUT
2038 * and the rp_thread will exit, however if there's an error we
2039 * need to cause it to exit. shutdown(2), if we have it, will
2040 * cause it to unblock if it's stuck waiting for the destination.
2042 WITH_QEMU_LOCK_GUARD(&ms
->qemu_file_lock
) {
2043 if (ms
->to_dst_file
&& ms
->rp_state
.from_dst_file
&&
2044 qemu_file_get_error(ms
->to_dst_file
)) {
2045 qemu_file_shutdown(ms
->rp_state
.from_dst_file
);
2049 trace_await_return_path_close_on_source_joining();
2050 qemu_thread_join(&ms
->rp_state
.rp_thread
);
2051 ms
->rp_state
.rp_thread_created
= false;
2052 trace_await_return_path_close_on_source_close();
2054 ret
= ms
->rp_state
.error
;
2055 ms
->rp_state
.error
= false;
2057 migration_release_dst_files(ms
);
2059 trace_migration_return_path_end_after(ret
);
2064 migration_wait_main_channel(MigrationState
*ms
)
2066 /* Wait until one PONG message received */
2067 qemu_sem_wait(&ms
->rp_state
.rp_pong_acks
);
2071 * Switch from normal iteration to postcopy
2072 * Returns non-0 on error
2074 static int postcopy_start(MigrationState
*ms
, Error
**errp
)
2077 QIOChannelBuffer
*bioc
;
2079 int64_t time_at_stop
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
2080 uint64_t bandwidth
= migrate_max_postcopy_bandwidth();
2081 bool restart_block
= false;
2082 int cur_state
= MIGRATION_STATUS_ACTIVE
;
2084 if (migrate_postcopy_preempt()) {
2085 migration_wait_main_channel(ms
);
2086 if (postcopy_preempt_establish_channel(ms
)) {
2087 migrate_set_state(&ms
->state
, ms
->state
, MIGRATION_STATUS_FAILED
);
2092 if (!migrate_pause_before_switchover()) {
2093 migrate_set_state(&ms
->state
, MIGRATION_STATUS_ACTIVE
,
2094 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
2097 trace_postcopy_start();
2098 qemu_mutex_lock_iothread();
2099 trace_postcopy_start_set_run();
2101 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, NULL
);
2102 global_state_store();
2103 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
2108 ret
= migration_maybe_pause(ms
, &cur_state
,
2109 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
2114 ret
= bdrv_inactivate_all();
2118 restart_block
= true;
2121 * Cause any non-postcopiable, but iterative devices to
2122 * send out their final data.
2124 qemu_savevm_state_complete_precopy(ms
->to_dst_file
, true, false);
2127 * in Finish migrate and with the io-lock held everything should
2128 * be quiet, but we've potentially still got dirty pages and we
2129 * need to tell the destination to throw any pages it's already received
2132 if (migrate_postcopy_ram()) {
2133 ram_postcopy_send_discard_bitmap(ms
);
2137 * send rest of state - note things that are doing postcopy
2138 * will notice we're in POSTCOPY_ACTIVE and not actually
2139 * wrap their state up here
2141 migration_rate_set(bandwidth
);
2142 if (migrate_postcopy_ram()) {
2143 /* Ping just for debugging, helps line traces up */
2144 qemu_savevm_send_ping(ms
->to_dst_file
, 2);
2148 * While loading the device state we may trigger page transfer
2149 * requests and the fd must be free to process those, and thus
2150 * the destination must read the whole device state off the fd before
2151 * it starts processing it. Unfortunately the ad-hoc migration format
2152 * doesn't allow the destination to know the size to read without fully
2153 * parsing it through each devices load-state code (especially the open
2154 * coded devices that use get/put).
2155 * So we wrap the device state up in a package with a length at the start;
2156 * to do this we use a qemu_buf to hold the whole of the device state.
2158 bioc
= qio_channel_buffer_new(4096);
2159 qio_channel_set_name(QIO_CHANNEL(bioc
), "migration-postcopy-buffer");
2160 fb
= qemu_file_new_output(QIO_CHANNEL(bioc
));
2161 object_unref(OBJECT(bioc
));
2164 * Make sure the receiver can get incoming pages before we send the rest
2167 qemu_savevm_send_postcopy_listen(fb
);
2169 qemu_savevm_state_complete_precopy(fb
, false, false);
2170 if (migrate_postcopy_ram()) {
2171 qemu_savevm_send_ping(fb
, 3);
2174 qemu_savevm_send_postcopy_run(fb
);
2176 /* <><> end of stuff going into the package */
2178 /* Last point of recovery; as soon as we send the package the destination
2179 * can open devices and potentially start running.
2180 * Lets just check again we've not got any errors.
2182 ret
= qemu_file_get_error(ms
->to_dst_file
);
2184 error_setg(errp
, "postcopy_start: Migration stream errored (pre package)");
2188 restart_block
= false;
2190 /* Now send that blob */
2191 if (qemu_savevm_send_packaged(ms
->to_dst_file
, bioc
->data
, bioc
->usage
)) {
2196 /* Send a notify to give a chance for anything that needs to happen
2197 * at the transition to postcopy and after the device state; in particular
2198 * spice needs to trigger a transition now
2200 ms
->postcopy_after_devices
= true;
2201 notifier_list_notify(&migration_state_notifiers
, ms
);
2203 ms
->downtime
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) - time_at_stop
;
2205 qemu_mutex_unlock_iothread();
2207 if (migrate_postcopy_ram()) {
2209 * Although this ping is just for debug, it could potentially be
2210 * used for getting a better measurement of downtime at the source.
2212 qemu_savevm_send_ping(ms
->to_dst_file
, 4);
2215 if (migrate_release_ram()) {
2216 ram_postcopy_migrated_memory_release(ms
);
2219 ret
= qemu_file_get_error(ms
->to_dst_file
);
2221 error_setg(errp
, "postcopy_start: Migration stream errored");
2222 migrate_set_state(&ms
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
2223 MIGRATION_STATUS_FAILED
);
2226 trace_postcopy_preempt_enabled(migrate_postcopy_preempt());
2233 migrate_set_state(&ms
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
2234 MIGRATION_STATUS_FAILED
);
2235 if (restart_block
) {
2236 /* A failure happened early enough that we know the destination hasn't
2237 * accessed block devices, so we're safe to recover.
2239 Error
*local_err
= NULL
;
2241 bdrv_activate_all(&local_err
);
2243 error_report_err(local_err
);
2246 qemu_mutex_unlock_iothread();
2251 * migration_maybe_pause: Pause if required to by
2252 * migrate_pause_before_switchover called with the iothread locked
2253 * Returns: 0 on success
2255 static int migration_maybe_pause(MigrationState
*s
,
2256 int *current_active_state
,
2259 if (!migrate_pause_before_switchover()) {
2263 /* Since leaving this state is not atomic with posting the semaphore
2264 * it's possible that someone could have issued multiple migrate_continue
2265 * and the semaphore is incorrectly positive at this point;
2266 * the docs say it's undefined to reinit a semaphore that's already
2267 * init'd, so use timedwait to eat up any existing posts.
2269 while (qemu_sem_timedwait(&s
->pause_sem
, 1) == 0) {
2270 /* This block intentionally left blank */
2274 * If the migration is cancelled when it is in the completion phase,
2275 * the migration state is set to MIGRATION_STATUS_CANCELLING.
2276 * So we don't need to wait a semaphore, otherwise we would always
2277 * wait for the 'pause_sem' semaphore.
2279 if (s
->state
!= MIGRATION_STATUS_CANCELLING
) {
2280 qemu_mutex_unlock_iothread();
2281 migrate_set_state(&s
->state
, *current_active_state
,
2282 MIGRATION_STATUS_PRE_SWITCHOVER
);
2283 qemu_sem_wait(&s
->pause_sem
);
2284 migrate_set_state(&s
->state
, MIGRATION_STATUS_PRE_SWITCHOVER
,
2286 *current_active_state
= new_state
;
2287 qemu_mutex_lock_iothread();
2290 return s
->state
== new_state
? 0 : -EINVAL
;
2294 * migration_completion: Used by migration_thread when there's not much left.
2295 * The caller 'breaks' the loop when this returns.
2297 * @s: Current migration state
2299 static void migration_completion(MigrationState
*s
)
2302 int current_active_state
= s
->state
;
2304 if (s
->state
== MIGRATION_STATUS_ACTIVE
) {
2305 qemu_mutex_lock_iothread();
2306 s
->downtime_start
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
2307 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, NULL
);
2309 s
->vm_old_state
= runstate_get();
2310 global_state_store();
2312 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
2313 trace_migration_completion_vm_stop(ret
);
2315 ret
= migration_maybe_pause(s
, ¤t_active_state
,
2316 MIGRATION_STATUS_DEVICE
);
2320 * Inactivate disks except in COLO, and track that we
2321 * have done so in order to remember to reactivate
2322 * them if migration fails or is cancelled.
2324 s
->block_inactive
= !migrate_colo();
2325 migration_rate_set(RATE_LIMIT_DISABLED
);
2326 ret
= qemu_savevm_state_complete_precopy(s
->to_dst_file
, false,
2330 qemu_mutex_unlock_iothread();
2335 } else if (s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
2336 trace_migration_completion_postcopy_end();
2338 qemu_mutex_lock_iothread();
2339 qemu_savevm_state_complete_postcopy(s
->to_dst_file
);
2340 qemu_mutex_unlock_iothread();
2343 * Shutdown the postcopy fast path thread. This is only needed
2344 * when dest QEMU binary is old (7.1/7.2). QEMU 8.0+ doesn't need
2347 if (migrate_postcopy_preempt() && s
->preempt_pre_7_2
) {
2348 postcopy_preempt_shutdown_file(s
);
2351 trace_migration_completion_postcopy_end_after_complete();
2356 if (await_return_path_close_on_source(s
)) {
2360 if (qemu_file_get_error(s
->to_dst_file
)) {
2361 trace_migration_completion_file_err();
2365 if (migrate_colo() && s
->state
== MIGRATION_STATUS_ACTIVE
) {
2366 /* COLO does not support postcopy */
2367 migrate_set_state(&s
->state
, MIGRATION_STATUS_ACTIVE
,
2368 MIGRATION_STATUS_COLO
);
2370 migrate_set_state(&s
->state
, current_active_state
,
2371 MIGRATION_STATUS_COMPLETED
);
2377 if (s
->block_inactive
&& (s
->state
== MIGRATION_STATUS_ACTIVE
||
2378 s
->state
== MIGRATION_STATUS_DEVICE
)) {
2380 * If not doing postcopy, vm_start() will be called: let's
2381 * regain control on images.
2383 Error
*local_err
= NULL
;
2385 qemu_mutex_lock_iothread();
2386 bdrv_activate_all(&local_err
);
2388 error_report_err(local_err
);
2390 s
->block_inactive
= false;
2392 qemu_mutex_unlock_iothread();
2395 migrate_set_state(&s
->state
, current_active_state
,
2396 MIGRATION_STATUS_FAILED
);
2400 * bg_migration_completion: Used by bg_migration_thread when after all the
2401 * RAM has been saved. The caller 'breaks' the loop when this returns.
2403 * @s: Current migration state
2405 static void bg_migration_completion(MigrationState
*s
)
2407 int current_active_state
= s
->state
;
2409 if (s
->state
== MIGRATION_STATUS_ACTIVE
) {
2411 * By this moment we have RAM content saved into the migration stream.
2412 * The next step is to flush the non-RAM content (device state)
2413 * right after the ram content. The device state has been stored into
2414 * the temporary buffer before RAM saving started.
2416 qemu_put_buffer(s
->to_dst_file
, s
->bioc
->data
, s
->bioc
->usage
);
2417 qemu_fflush(s
->to_dst_file
);
2418 } else if (s
->state
== MIGRATION_STATUS_CANCELLING
) {
2422 if (qemu_file_get_error(s
->to_dst_file
)) {
2423 trace_migration_completion_file_err();
2427 migrate_set_state(&s
->state
, current_active_state
,
2428 MIGRATION_STATUS_COMPLETED
);
2432 migrate_set_state(&s
->state
, current_active_state
,
2433 MIGRATION_STATUS_FAILED
);
2436 typedef enum MigThrError
{
2437 /* No error detected */
2438 MIG_THR_ERR_NONE
= 0,
2439 /* Detected error, but resumed successfully */
2440 MIG_THR_ERR_RECOVERED
= 1,
2441 /* Detected fatal error, need to exit */
2442 MIG_THR_ERR_FATAL
= 2,
2445 static int postcopy_resume_handshake(MigrationState
*s
)
2447 qemu_savevm_send_postcopy_resume(s
->to_dst_file
);
2449 while (s
->state
== MIGRATION_STATUS_POSTCOPY_RECOVER
) {
2450 qemu_sem_wait(&s
->rp_state
.rp_sem
);
2453 if (s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
2460 /* Return zero if success, or <0 for error */
2461 static int postcopy_do_resume(MigrationState
*s
)
2466 * Call all the resume_prepare() hooks, so that modules can be
2467 * ready for the migration resume.
2469 ret
= qemu_savevm_state_resume_prepare(s
);
2471 error_report("%s: resume_prepare() failure detected: %d",
2477 * If preempt is enabled, re-establish the preempt channel. Note that
2478 * we do it after resume prepare to make sure the main channel will be
2479 * created before the preempt channel. E.g. with weak network, the
2480 * dest QEMU may get messed up with the preempt and main channels on
2481 * the order of connection setup. This guarantees the correct order.
2483 ret
= postcopy_preempt_establish_channel(s
);
2485 error_report("%s: postcopy_preempt_establish_channel(): %d",
2491 * Last handshake with destination on the resume (destination will
2492 * switch to postcopy-active afterwards)
2494 ret
= postcopy_resume_handshake(s
);
2496 error_report("%s: handshake failed: %d", __func__
, ret
);
2504 * We don't return until we are in a safe state to continue current
2505 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
2506 * MIG_THR_ERR_FATAL if unrecovery failure happened.
2508 static MigThrError
postcopy_pause(MigrationState
*s
)
2510 assert(s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
);
2516 * Current channel is possibly broken. Release it. Note that this is
2517 * guaranteed even without lock because to_dst_file should only be
2518 * modified by the migration thread. That also guarantees that the
2519 * unregister of yank is safe too without the lock. It should be safe
2520 * even to be within the qemu_file_lock, but we didn't do that to avoid
2521 * taking more mutex (yank_lock) within qemu_file_lock. TL;DR: we make
2522 * the qemu_file_lock critical section as small as possible.
2524 assert(s
->to_dst_file
);
2525 migration_ioc_unregister_yank_from_file(s
->to_dst_file
);
2526 qemu_mutex_lock(&s
->qemu_file_lock
);
2527 file
= s
->to_dst_file
;
2528 s
->to_dst_file
= NULL
;
2529 qemu_mutex_unlock(&s
->qemu_file_lock
);
2531 qemu_file_shutdown(file
);
2535 * We're already pausing, so ignore any errors on the return
2536 * path and just wait for the thread to finish. It will be
2537 * re-created when we resume.
2539 await_return_path_close_on_source(s
);
2541 migrate_set_state(&s
->state
, s
->state
,
2542 MIGRATION_STATUS_POSTCOPY_PAUSED
);
2544 error_report("Detected IO failure for postcopy. "
2545 "Migration paused.");
2548 * We wait until things fixed up. Then someone will setup the
2549 * status back for us.
2551 while (s
->state
== MIGRATION_STATUS_POSTCOPY_PAUSED
) {
2552 qemu_sem_wait(&s
->postcopy_pause_sem
);
2555 if (s
->state
== MIGRATION_STATUS_POSTCOPY_RECOVER
) {
2556 /* Woken up by a recover procedure. Give it a shot */
2558 /* Do the resume logic */
2559 if (postcopy_do_resume(s
) == 0) {
2560 /* Let's continue! */
2561 trace_postcopy_pause_continued();
2562 return MIG_THR_ERR_RECOVERED
;
2565 * Something wrong happened during the recovery, let's
2566 * pause again. Pause is always better than throwing
2572 /* This is not right... Time to quit. */
2573 return MIG_THR_ERR_FATAL
;
2578 static MigThrError
migration_detect_error(MigrationState
*s
)
2581 int state
= s
->state
;
2582 Error
*local_error
= NULL
;
2584 if (state
== MIGRATION_STATUS_CANCELLING
||
2585 state
== MIGRATION_STATUS_CANCELLED
) {
2586 /* End the migration, but don't set the state to failed */
2587 return MIG_THR_ERR_FATAL
;
2591 * Try to detect any file errors. Note that postcopy_qemufile_src will
2592 * be NULL when postcopy preempt is not enabled.
2594 ret
= qemu_file_get_error_obj_any(s
->to_dst_file
,
2595 s
->postcopy_qemufile_src
,
2598 /* Everything is fine */
2599 assert(!local_error
);
2600 return MIG_THR_ERR_NONE
;
2604 migrate_set_error(s
, local_error
);
2605 error_free(local_error
);
2608 if (state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
&& ret
) {
2610 * For postcopy, we allow the network to be down for a
2611 * while. After that, it can be continued by a
2614 return postcopy_pause(s
);
2617 * For precopy (or postcopy with error outside IO), we fail
2620 migrate_set_state(&s
->state
, state
, MIGRATION_STATUS_FAILED
);
2621 trace_migration_thread_file_err();
2623 /* Time to stop the migration, now. */
2624 return MIG_THR_ERR_FATAL
;
2628 static void migration_calculate_complete(MigrationState
*s
)
2630 uint64_t bytes
= migration_transferred_bytes(s
->to_dst_file
);
2631 int64_t end_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
2632 int64_t transfer_time
;
2634 s
->total_time
= end_time
- s
->start_time
;
2637 * It's still not set, so we are precopy migration. For
2638 * postcopy, downtime is calculated during postcopy_start().
2640 s
->downtime
= end_time
- s
->downtime_start
;
2643 transfer_time
= s
->total_time
- s
->setup_time
;
2644 if (transfer_time
) {
2645 s
->mbps
= ((double) bytes
* 8.0) / transfer_time
/ 1000;
2649 static void update_iteration_initial_status(MigrationState
*s
)
2652 * Update these three fields at the same time to avoid mismatch info lead
2653 * wrong speed calculation.
2655 s
->iteration_start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
2656 s
->iteration_initial_bytes
= migration_transferred_bytes(s
->to_dst_file
);
2657 s
->iteration_initial_pages
= ram_get_total_transferred_pages();
2660 static void migration_update_counters(MigrationState
*s
,
2661 int64_t current_time
)
2663 uint64_t transferred
, transferred_pages
, time_spent
;
2664 uint64_t current_bytes
; /* bytes transferred since the beginning */
2667 if (current_time
< s
->iteration_start_time
+ BUFFER_DELAY
) {
2671 current_bytes
= migration_transferred_bytes(s
->to_dst_file
);
2672 transferred
= current_bytes
- s
->iteration_initial_bytes
;
2673 time_spent
= current_time
- s
->iteration_start_time
;
2674 bandwidth
= (double)transferred
/ time_spent
;
2675 s
->threshold_size
= bandwidth
* migrate_downtime_limit();
2677 s
->mbps
= (((double) transferred
* 8.0) /
2678 ((double) time_spent
/ 1000.0)) / 1000.0 / 1000.0;
2680 transferred_pages
= ram_get_total_transferred_pages() -
2681 s
->iteration_initial_pages
;
2682 s
->pages_per_second
= (double) transferred_pages
/
2683 (((double) time_spent
/ 1000.0));
2686 * if we haven't sent anything, we don't want to
2687 * recalculate. 10000 is a small enough number for our purposes
2689 if (stat64_get(&mig_stats
.dirty_pages_rate
) &&
2690 transferred
> 10000) {
2691 s
->expected_downtime
=
2692 stat64_get(&mig_stats
.dirty_bytes_last_sync
) / bandwidth
;
2695 migration_rate_reset(s
->to_dst_file
);
2697 update_iteration_initial_status(s
);
2699 trace_migrate_transferred(transferred
, time_spent
,
2700 bandwidth
, s
->threshold_size
);
2703 static bool migration_can_switchover(MigrationState
*s
)
2705 if (!migrate_switchover_ack()) {
2709 /* No reason to wait for switchover ACK if VM is stopped */
2710 if (!runstate_is_running()) {
2714 return s
->switchover_acked
;
2717 /* Migration thread iteration status */
2719 MIG_ITERATE_RESUME
, /* Resume current iteration */
2720 MIG_ITERATE_SKIP
, /* Skip current iteration */
2721 MIG_ITERATE_BREAK
, /* Break the loop */
2725 * Return true if continue to the next iteration directly, false
2728 static MigIterateState
migration_iteration_run(MigrationState
*s
)
2730 uint64_t must_precopy
, can_postcopy
;
2731 Error
*local_err
= NULL
;
2732 bool in_postcopy
= s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
;
2733 bool can_switchover
= migration_can_switchover(s
);
2735 qemu_savevm_state_pending_estimate(&must_precopy
, &can_postcopy
);
2736 uint64_t pending_size
= must_precopy
+ can_postcopy
;
2738 trace_migrate_pending_estimate(pending_size
, must_precopy
, can_postcopy
);
2740 if (must_precopy
<= s
->threshold_size
) {
2741 qemu_savevm_state_pending_exact(&must_precopy
, &can_postcopy
);
2742 pending_size
= must_precopy
+ can_postcopy
;
2743 trace_migrate_pending_exact(pending_size
, must_precopy
, can_postcopy
);
2746 if ((!pending_size
|| pending_size
< s
->threshold_size
) && can_switchover
) {
2747 trace_migration_thread_low_pending(pending_size
);
2748 migration_completion(s
);
2749 return MIG_ITERATE_BREAK
;
2752 /* Still a significant amount to transfer */
2753 if (!in_postcopy
&& must_precopy
<= s
->threshold_size
&& can_switchover
&&
2754 qatomic_read(&s
->start_postcopy
)) {
2755 if (postcopy_start(s
, &local_err
)) {
2756 migrate_set_error(s
, local_err
);
2757 error_report_err(local_err
);
2759 return MIG_ITERATE_SKIP
;
2762 /* Just another iteration step */
2763 qemu_savevm_state_iterate(s
->to_dst_file
, in_postcopy
);
2764 return MIG_ITERATE_RESUME
;
2767 static void migration_iteration_finish(MigrationState
*s
)
2769 /* If we enabled cpu throttling for auto-converge, turn it off. */
2770 cpu_throttle_stop();
2772 qemu_mutex_lock_iothread();
2774 case MIGRATION_STATUS_COMPLETED
:
2775 migration_calculate_complete(s
);
2776 runstate_set(RUN_STATE_POSTMIGRATE
);
2778 case MIGRATION_STATUS_COLO
:
2779 assert(migrate_colo());
2780 migrate_start_colo_process(s
);
2781 s
->vm_old_state
= RUN_STATE_RUNNING
;
2783 case MIGRATION_STATUS_FAILED
:
2784 case MIGRATION_STATUS_CANCELLED
:
2785 case MIGRATION_STATUS_CANCELLING
:
2786 if (s
->vm_old_state
== RUN_STATE_RUNNING
) {
2787 if (!runstate_check(RUN_STATE_SHUTDOWN
)) {
2791 if (runstate_check(RUN_STATE_FINISH_MIGRATE
)) {
2792 runstate_set(s
->vm_old_state
);
2798 /* Should not reach here, but if so, forgive the VM. */
2799 error_report("%s: Unknown ending state %d", __func__
, s
->state
);
2802 migrate_fd_cleanup_schedule(s
);
2803 qemu_mutex_unlock_iothread();
2806 static void bg_migration_iteration_finish(MigrationState
*s
)
2809 * Stop tracking RAM writes - un-protect memory, un-register UFFD
2810 * memory ranges, flush kernel wait queues and wake up threads
2811 * waiting for write fault to be resolved.
2813 ram_write_tracking_stop();
2815 qemu_mutex_lock_iothread();
2817 case MIGRATION_STATUS_COMPLETED
:
2818 migration_calculate_complete(s
);
2821 case MIGRATION_STATUS_ACTIVE
:
2822 case MIGRATION_STATUS_FAILED
:
2823 case MIGRATION_STATUS_CANCELLED
:
2824 case MIGRATION_STATUS_CANCELLING
:
2828 /* Should not reach here, but if so, forgive the VM. */
2829 error_report("%s: Unknown ending state %d", __func__
, s
->state
);
2833 migrate_fd_cleanup_schedule(s
);
2834 qemu_mutex_unlock_iothread();
2838 * Return true if continue to the next iteration directly, false
2841 static MigIterateState
bg_migration_iteration_run(MigrationState
*s
)
2845 res
= qemu_savevm_state_iterate(s
->to_dst_file
, false);
2847 bg_migration_completion(s
);
2848 return MIG_ITERATE_BREAK
;
2851 return MIG_ITERATE_RESUME
;
2854 void migration_make_urgent_request(void)
2856 qemu_sem_post(&migrate_get_current()->rate_limit_sem
);
2859 void migration_consume_urgent_request(void)
2861 qemu_sem_wait(&migrate_get_current()->rate_limit_sem
);
2864 /* Returns true if the rate limiting was broken by an urgent request */
2865 bool migration_rate_limit(void)
2867 int64_t now
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
2868 MigrationState
*s
= migrate_get_current();
2870 bool urgent
= false;
2871 migration_update_counters(s
, now
);
2872 if (migration_rate_exceeded(s
->to_dst_file
)) {
2874 if (qemu_file_get_error(s
->to_dst_file
)) {
2878 * Wait for a delay to do rate limiting OR
2879 * something urgent to post the semaphore.
2881 int ms
= s
->iteration_start_time
+ BUFFER_DELAY
- now
;
2882 trace_migration_rate_limit_pre(ms
);
2883 if (qemu_sem_timedwait(&s
->rate_limit_sem
, ms
) == 0) {
2885 * We were woken by one or more urgent things but
2886 * the timedwait will have consumed one of them.
2887 * The service routine for the urgent wake will dec
2888 * the semaphore itself for each item it consumes,
2889 * so add this one we just eat back.
2891 qemu_sem_post(&s
->rate_limit_sem
);
2894 trace_migration_rate_limit_post(urgent
);
2900 * if failover devices are present, wait they are completely
2904 static void qemu_savevm_wait_unplug(MigrationState
*s
, int old_state
,
2907 if (qemu_savevm_state_guest_unplug_pending()) {
2908 migrate_set_state(&s
->state
, old_state
, MIGRATION_STATUS_WAIT_UNPLUG
);
2910 while (s
->state
== MIGRATION_STATUS_WAIT_UNPLUG
&&
2911 qemu_savevm_state_guest_unplug_pending()) {
2912 qemu_sem_timedwait(&s
->wait_unplug_sem
, 250);
2914 if (s
->state
!= MIGRATION_STATUS_WAIT_UNPLUG
) {
2915 int timeout
= 120; /* 30 seconds */
2917 * migration has been canceled
2918 * but as we have started an unplug we must wait the end
2919 * to be able to plug back the card
2921 while (timeout
-- && qemu_savevm_state_guest_unplug_pending()) {
2922 qemu_sem_timedwait(&s
->wait_unplug_sem
, 250);
2924 if (qemu_savevm_state_guest_unplug_pending() &&
2926 warn_report("migration: partially unplugged device on "
2931 migrate_set_state(&s
->state
, MIGRATION_STATUS_WAIT_UNPLUG
, new_state
);
2933 migrate_set_state(&s
->state
, old_state
, new_state
);
2938 * Master migration thread on the source VM.
2939 * It drives the migration and pumps the data down the outgoing channel.
2941 static void *migration_thread(void *opaque
)
2943 MigrationState
*s
= opaque
;
2944 MigrationThread
*thread
= NULL
;
2945 int64_t setup_start
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
2946 MigThrError thr_error
;
2947 bool urgent
= false;
2949 thread
= migration_threads_add("live_migration", qemu_get_thread_id());
2951 rcu_register_thread();
2953 object_ref(OBJECT(s
));
2954 update_iteration_initial_status(s
);
2956 qemu_savevm_state_header(s
->to_dst_file
);
2959 * If we opened the return path, we need to make sure dst has it
2962 if (s
->rp_state
.rp_thread_created
) {
2963 /* Now tell the dest that it should open its end so it can reply */
2964 qemu_savevm_send_open_return_path(s
->to_dst_file
);
2966 /* And do a ping that will make stuff easier to debug */
2967 qemu_savevm_send_ping(s
->to_dst_file
, 1);
2970 if (migrate_postcopy()) {
2972 * Tell the destination that we *might* want to do postcopy later;
2973 * if the other end can't do postcopy it should fail now, nice and
2976 qemu_savevm_send_postcopy_advise(s
->to_dst_file
);
2979 if (migrate_colo()) {
2980 /* Notify migration destination that we enable COLO */
2981 qemu_savevm_send_colo_enable(s
->to_dst_file
);
2984 qemu_savevm_state_setup(s
->to_dst_file
);
2986 qemu_savevm_wait_unplug(s
, MIGRATION_STATUS_SETUP
,
2987 MIGRATION_STATUS_ACTIVE
);
2989 s
->setup_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
) - setup_start
;
2991 trace_migration_thread_setup_complete();
2993 while (migration_is_active(s
)) {
2994 if (urgent
|| !migration_rate_exceeded(s
->to_dst_file
)) {
2995 MigIterateState iter_state
= migration_iteration_run(s
);
2996 if (iter_state
== MIG_ITERATE_SKIP
) {
2998 } else if (iter_state
== MIG_ITERATE_BREAK
) {
3004 * Try to detect any kind of failures, and see whether we
3005 * should stop the migration now.
3007 thr_error
= migration_detect_error(s
);
3008 if (thr_error
== MIG_THR_ERR_FATAL
) {
3009 /* Stop migration */
3011 } else if (thr_error
== MIG_THR_ERR_RECOVERED
) {
3013 * Just recovered from a e.g. network failure, reset all
3014 * the local variables. This is important to avoid
3015 * breaking transferred_bytes and bandwidth calculation
3017 update_iteration_initial_status(s
);
3020 urgent
= migration_rate_limit();
3023 trace_migration_thread_after_loop();
3024 migration_iteration_finish(s
);
3025 object_unref(OBJECT(s
));
3026 rcu_unregister_thread();
3027 migration_threads_remove(thread
);
3031 static void bg_migration_vm_start_bh(void *opaque
)
3033 MigrationState
*s
= opaque
;
3035 qemu_bh_delete(s
->vm_start_bh
);
3036 s
->vm_start_bh
= NULL
;
3039 s
->downtime
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) - s
->downtime_start
;
3043 * Background snapshot thread, based on live migration code.
3044 * This is an alternative implementation of live migration mechanism
3045 * introduced specifically to support background snapshots.
3047 * It takes advantage of userfault_fd write protection mechanism introduced
3048 * in v5.7 kernel. Compared to existing dirty page logging migration much
3049 * lesser stream traffic is produced resulting in smaller snapshot images,
3050 * simply cause of no page duplicates can get into the stream.
3052 * Another key point is that generated vmstate stream reflects machine state
3053 * 'frozen' at the beginning of snapshot creation compared to dirty page logging
3054 * mechanism, which effectively results in that saved snapshot is the state of VM
3055 * at the end of the process.
3057 static void *bg_migration_thread(void *opaque
)
3059 MigrationState
*s
= opaque
;
3060 int64_t setup_start
;
3061 MigThrError thr_error
;
3063 bool early_fail
= true;
3065 rcu_register_thread();
3066 object_ref(OBJECT(s
));
3068 migration_rate_set(RATE_LIMIT_DISABLED
);
3070 setup_start
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
3072 * We want to save vmstate for the moment when migration has been
3073 * initiated but also we want to save RAM content while VM is running.
3074 * The RAM content should appear first in the vmstate. So, we first
3075 * stash the non-RAM part of the vmstate to the temporary buffer,
3076 * then write RAM part of the vmstate to the migration stream
3077 * with vCPUs running and, finally, write stashed non-RAM part of
3078 * the vmstate from the buffer to the migration stream.
3080 s
->bioc
= qio_channel_buffer_new(512 * 1024);
3081 qio_channel_set_name(QIO_CHANNEL(s
->bioc
), "vmstate-buffer");
3082 fb
= qemu_file_new_output(QIO_CHANNEL(s
->bioc
));
3083 object_unref(OBJECT(s
->bioc
));
3085 update_iteration_initial_status(s
);
3088 * Prepare for tracking memory writes with UFFD-WP - populate
3089 * RAM pages before protecting.
3092 ram_write_tracking_prepare();
3095 qemu_savevm_state_header(s
->to_dst_file
);
3096 qemu_savevm_state_setup(s
->to_dst_file
);
3098 qemu_savevm_wait_unplug(s
, MIGRATION_STATUS_SETUP
,
3099 MIGRATION_STATUS_ACTIVE
);
3101 s
->setup_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
) - setup_start
;
3103 trace_migration_thread_setup_complete();
3104 s
->downtime_start
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
3106 qemu_mutex_lock_iothread();
3109 * If VM is currently in suspended state, then, to make a valid runstate
3110 * transition in vm_stop_force_state() we need to wakeup it up.
3112 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, NULL
);
3113 s
->vm_old_state
= runstate_get();
3115 global_state_store();
3116 /* Forcibly stop VM before saving state of vCPUs and devices */
3117 if (vm_stop_force_state(RUN_STATE_PAUSED
)) {
3121 * Put vCPUs in sync with shadow context structures, then
3122 * save their state to channel-buffer along with devices.
3124 cpu_synchronize_all_states();
3125 if (qemu_savevm_state_complete_precopy_non_iterable(fb
, false, false)) {
3129 * Since we are going to get non-iterable state data directly
3130 * from s->bioc->data, explicit flush is needed here.
3134 /* Now initialize UFFD context and start tracking RAM writes */
3135 if (ram_write_tracking_start()) {
3141 * Start VM from BH handler to avoid write-fault lock here.
3142 * UFFD-WP protection for the whole RAM is already enabled so
3143 * calling VM state change notifiers from vm_start() would initiate
3144 * writes to virtio VQs memory which is in write-protected region.
3146 s
->vm_start_bh
= qemu_bh_new(bg_migration_vm_start_bh
, s
);
3147 qemu_bh_schedule(s
->vm_start_bh
);
3149 qemu_mutex_unlock_iothread();
3151 while (migration_is_active(s
)) {
3152 MigIterateState iter_state
= bg_migration_iteration_run(s
);
3153 if (iter_state
== MIG_ITERATE_SKIP
) {
3155 } else if (iter_state
== MIG_ITERATE_BREAK
) {
3160 * Try to detect any kind of failures, and see whether we
3161 * should stop the migration now.
3163 thr_error
= migration_detect_error(s
);
3164 if (thr_error
== MIG_THR_ERR_FATAL
) {
3165 /* Stop migration */
3169 migration_update_counters(s
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
));
3172 trace_migration_thread_after_loop();
3176 migrate_set_state(&s
->state
, MIGRATION_STATUS_ACTIVE
,
3177 MIGRATION_STATUS_FAILED
);
3178 qemu_mutex_unlock_iothread();
3181 bg_migration_iteration_finish(s
);
3184 object_unref(OBJECT(s
));
3185 rcu_unregister_thread();
3190 void migrate_fd_connect(MigrationState
*s
, Error
*error_in
)
3192 Error
*local_err
= NULL
;
3193 uint64_t rate_limit
;
3194 bool resume
= s
->state
== MIGRATION_STATUS_POSTCOPY_PAUSED
;
3197 * If there's a previous error, free it and prepare for another one.
3198 * Meanwhile if migration completes successfully, there won't have an error
3199 * dumped when calling migrate_fd_cleanup().
3201 migrate_error_free(s
);
3203 s
->expected_downtime
= migrate_downtime_limit();
3205 assert(s
->cleanup_bh
);
3207 assert(!s
->cleanup_bh
);
3208 s
->cleanup_bh
= qemu_bh_new(migrate_fd_cleanup_bh
, s
);
3211 migrate_fd_error(s
, error_in
);
3214 * Don't do cleanup for resume if channel is invalid, but only dump
3215 * the error. We wait for another channel connect from the user.
3216 * The error_report still gives HMP user a hint on what failed.
3217 * It's normally done in migrate_fd_cleanup(), but call it here
3220 error_report_err(error_copy(s
->error
));
3222 migrate_fd_cleanup(s
);
3228 /* This is a resumed migration */
3229 rate_limit
= migrate_max_postcopy_bandwidth();
3231 /* This is a fresh new migration */
3232 rate_limit
= migrate_max_bandwidth();
3234 /* Notify before starting migration thread */
3235 notifier_list_notify(&migration_state_notifiers
, s
);
3238 migration_rate_set(rate_limit
);
3239 qemu_file_set_blocking(s
->to_dst_file
, true);
3242 * Open the return path. For postcopy, it is used exclusively. For
3243 * precopy, only if user specified "return-path" capability would
3244 * QEMU uses the return path.
3246 if (migrate_postcopy_ram() || migrate_return_path()) {
3247 if (open_return_path_on_source(s
)) {
3248 error_setg(&local_err
, "Unable to open return-path for postcopy");
3249 migrate_set_state(&s
->state
, s
->state
, MIGRATION_STATUS_FAILED
);
3250 migrate_set_error(s
, local_err
);
3251 error_report_err(local_err
);
3252 migrate_fd_cleanup(s
);
3258 * This needs to be done before resuming a postcopy. Note: for newer
3259 * QEMUs we will delay the channel creation until postcopy_start(), to
3260 * avoid disorder of channel creations.
3262 if (migrate_postcopy_preempt() && s
->preempt_pre_7_2
) {
3263 postcopy_preempt_setup(s
);
3267 /* Wakeup the main migration thread to do the recovery */
3268 migrate_set_state(&s
->state
, MIGRATION_STATUS_POSTCOPY_PAUSED
,
3269 MIGRATION_STATUS_POSTCOPY_RECOVER
);
3270 qemu_sem_post(&s
->postcopy_pause_sem
);
3274 if (multifd_save_setup(&local_err
) != 0) {
3275 migrate_set_error(s
, local_err
);
3276 error_report_err(local_err
);
3277 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
3278 MIGRATION_STATUS_FAILED
);
3279 migrate_fd_cleanup(s
);
3283 if (migrate_background_snapshot()) {
3284 qemu_thread_create(&s
->thread
, "bg_snapshot",
3285 bg_migration_thread
, s
, QEMU_THREAD_JOINABLE
);
3287 qemu_thread_create(&s
->thread
, "live_migration",
3288 migration_thread
, s
, QEMU_THREAD_JOINABLE
);
3290 s
->migration_thread_running
= true;
3293 static void migration_class_init(ObjectClass
*klass
, void *data
)
3295 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3297 dc
->user_creatable
= false;
3298 device_class_set_props(dc
, migration_properties
);
3301 static void migration_instance_finalize(Object
*obj
)
3303 MigrationState
*ms
= MIGRATION_OBJ(obj
);
3305 qemu_mutex_destroy(&ms
->error_mutex
);
3306 qemu_mutex_destroy(&ms
->qemu_file_lock
);
3307 qemu_sem_destroy(&ms
->wait_unplug_sem
);
3308 qemu_sem_destroy(&ms
->rate_limit_sem
);
3309 qemu_sem_destroy(&ms
->pause_sem
);
3310 qemu_sem_destroy(&ms
->postcopy_pause_sem
);
3311 qemu_sem_destroy(&ms
->rp_state
.rp_sem
);
3312 qemu_sem_destroy(&ms
->rp_state
.rp_pong_acks
);
3313 qemu_sem_destroy(&ms
->postcopy_qemufile_src_sem
);
3314 error_free(ms
->error
);
3317 static void migration_instance_init(Object
*obj
)
3319 MigrationState
*ms
= MIGRATION_OBJ(obj
);
3321 ms
->state
= MIGRATION_STATUS_NONE
;
3323 ms
->pages_per_second
= -1;
3324 qemu_sem_init(&ms
->pause_sem
, 0);
3325 qemu_mutex_init(&ms
->error_mutex
);
3327 migrate_params_init(&ms
->parameters
);
3329 qemu_sem_init(&ms
->postcopy_pause_sem
, 0);
3330 qemu_sem_init(&ms
->rp_state
.rp_sem
, 0);
3331 qemu_sem_init(&ms
->rp_state
.rp_pong_acks
, 0);
3332 qemu_sem_init(&ms
->rate_limit_sem
, 0);
3333 qemu_sem_init(&ms
->wait_unplug_sem
, 0);
3334 qemu_sem_init(&ms
->postcopy_qemufile_src_sem
, 0);
3335 qemu_mutex_init(&ms
->qemu_file_lock
);
3339 * Return true if check pass, false otherwise. Error will be put
3340 * inside errp if provided.
3342 static bool migration_object_check(MigrationState
*ms
, Error
**errp
)
3344 /* Assuming all off */
3345 bool old_caps
[MIGRATION_CAPABILITY__MAX
] = { 0 };
3347 if (!migrate_params_check(&ms
->parameters
, errp
)) {
3351 return migrate_caps_check(old_caps
, ms
->capabilities
, errp
);
3354 static const TypeInfo migration_type
= {
3355 .name
= TYPE_MIGRATION
,
3357 * NOTE: TYPE_MIGRATION is not really a device, as the object is
3358 * not created using qdev_new(), it is not attached to the qdev
3359 * device tree, and it is never realized.
3361 * TODO: Make this TYPE_OBJECT once QOM provides something like
3362 * TYPE_DEVICE's "-global" properties.
3364 .parent
= TYPE_DEVICE
,
3365 .class_init
= migration_class_init
,
3366 .class_size
= sizeof(MigrationClass
),
3367 .instance_size
= sizeof(MigrationState
),
3368 .instance_init
= migration_instance_init
,
3369 .instance_finalize
= migration_instance_finalize
,
3372 static void register_migration_types(void)
3374 type_register_static(&migration_type
);
3377 type_init(register_migration_types
);