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 "migration/global_state.h"
30 #include "migration/misc.h"
31 #include "migration.h"
33 #include "qemu-file-channel.h"
34 #include "qemu-file.h"
35 #include "migration/vmstate.h"
36 #include "block/block.h"
37 #include "qapi/error.h"
38 #include "qapi/clone-visitor.h"
39 #include "qapi/qapi-visit-migration.h"
40 #include "qapi/qapi-visit-sockets.h"
41 #include "qapi/qapi-commands-migration.h"
42 #include "qapi/qapi-events-migration.h"
43 #include "qapi/qmp/qerror.h"
44 #include "qapi/qmp/qnull.h"
47 #include "postcopy-ram.h"
48 #include "qemu/thread.h"
50 #include "exec/target_page.h"
51 #include "io/channel-buffer.h"
52 #include "migration/colo.h"
53 #include "hw/boards.h"
54 #include "hw/qdev-properties.h"
55 #include "monitor/monitor.h"
56 #include "net/announce.h"
57 #include "qemu/queue.h"
61 #include "hw/vfio/vfio-common.h"
64 #define MAX_THROTTLE (128 << 20) /* Migration transfer speed throttling */
66 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
68 #define BUFFER_DELAY 100
69 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
71 /* Time in milliseconds we are allowed to stop the source,
72 * for sending the last part */
73 #define DEFAULT_MIGRATE_SET_DOWNTIME 300
75 /* Maximum migrate downtime set to 2000 seconds */
76 #define MAX_MIGRATE_DOWNTIME_SECONDS 2000
77 #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
79 /* Default compression thread count */
80 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
81 /* Default decompression thread count, usually decompression is at
82 * least 4 times as fast as compression.*/
83 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
84 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
85 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
86 /* Define default autoconverge cpu throttle migration parameters */
87 #define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50
88 #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
89 #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
90 #define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
92 /* Migration XBZRLE default cache size */
93 #define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
95 /* The delay time (in ms) between two COLO checkpoints */
96 #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
97 #define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
98 #define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
99 /* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
100 #define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
101 /* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
102 #define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
104 /* Background transfer rate for postcopy, 0 means unlimited, note
105 * that page requests can still exceed this limit.
107 #define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
110 * Parameters for self_announce_delay giving a stream of RARP/ARP
111 * packets after migration.
113 #define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50
114 #define DEFAULT_MIGRATE_ANNOUNCE_MAX 550
115 #define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5
116 #define DEFAULT_MIGRATE_ANNOUNCE_STEP 100
118 static NotifierList migration_state_notifiers
=
119 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers
);
121 static bool deferred_incoming
;
123 /* Messages sent on the return path from destination to source */
124 enum mig_rp_message_type
{
125 MIG_RP_MSG_INVALID
= 0, /* Must be 0 */
126 MIG_RP_MSG_SHUT
, /* sibling will not send any more RP messages */
127 MIG_RP_MSG_PONG
, /* Response to a PING; data (seq: be32 ) */
129 MIG_RP_MSG_REQ_PAGES_ID
, /* data (start: be64, len: be32, id: string) */
130 MIG_RP_MSG_REQ_PAGES
, /* data (start: be64, len: be32) */
131 MIG_RP_MSG_RECV_BITMAP
, /* send recved_bitmap back to source */
132 MIG_RP_MSG_RESUME_ACK
, /* tell source that we are ready to resume */
137 /* When we add fault tolerance, we could have several
138 migrations at once. For now we don't need to add
139 dynamic creation of migration */
141 static MigrationState
*current_migration
;
142 static MigrationIncomingState
*current_incoming
;
144 static bool migration_object_check(MigrationState
*ms
, Error
**errp
);
145 static int migration_maybe_pause(MigrationState
*s
,
146 int *current_active_state
,
148 static void migrate_fd_cancel(MigrationState
*s
);
150 static gint
page_request_addr_cmp(gconstpointer ap
, gconstpointer bp
)
152 uintptr_t a
= (uintptr_t) ap
, b
= (uintptr_t) bp
;
154 return (a
> b
) - (a
< b
);
157 void migration_object_init(void)
161 /* This can only be called once. */
162 assert(!current_migration
);
163 current_migration
= MIGRATION_OBJ(object_new(TYPE_MIGRATION
));
166 * Init the migrate incoming object as well no matter whether
167 * we'll use it or not.
169 assert(!current_incoming
);
170 current_incoming
= g_new0(MigrationIncomingState
, 1);
171 current_incoming
->state
= MIGRATION_STATUS_NONE
;
172 current_incoming
->postcopy_remote_fds
=
173 g_array_new(FALSE
, TRUE
, sizeof(struct PostCopyFD
));
174 qemu_mutex_init(¤t_incoming
->rp_mutex
);
175 qemu_event_init(¤t_incoming
->main_thread_load_event
, false);
176 qemu_sem_init(¤t_incoming
->postcopy_pause_sem_dst
, 0);
177 qemu_sem_init(¤t_incoming
->postcopy_pause_sem_fault
, 0);
178 qemu_mutex_init(¤t_incoming
->page_request_mutex
);
179 current_incoming
->page_requested
= g_tree_new(page_request_addr_cmp
);
181 if (!migration_object_check(current_migration
, &err
)) {
182 error_report_err(err
);
187 void migration_shutdown(void)
190 * Cancel the current migration - that will (eventually)
191 * stop the migration using this structure
193 migrate_fd_cancel(current_migration
);
194 object_unref(OBJECT(current_migration
));
197 * Cancel outgoing migration of dirty bitmaps. It should
198 * at least unref used block nodes.
200 dirty_bitmap_mig_cancel_outgoing();
203 * Cancel incoming migration of dirty bitmaps. Dirty bitmaps
204 * are non-critical data, and their loss never considered as
207 dirty_bitmap_mig_cancel_incoming();
211 MigrationState
*migrate_get_current(void)
213 /* This can only be called after the object created. */
214 assert(current_migration
);
215 return current_migration
;
218 MigrationIncomingState
*migration_incoming_get_current(void)
220 assert(current_incoming
);
221 return current_incoming
;
224 void migration_incoming_state_destroy(void)
226 struct MigrationIncomingState
*mis
= migration_incoming_get_current();
228 if (mis
->to_src_file
) {
229 /* Tell source that we are done */
230 migrate_send_rp_shut(mis
, qemu_file_get_error(mis
->from_src_file
) != 0);
231 qemu_fclose(mis
->to_src_file
);
232 mis
->to_src_file
= NULL
;
235 if (mis
->from_src_file
) {
236 qemu_fclose(mis
->from_src_file
);
237 mis
->from_src_file
= NULL
;
239 if (mis
->postcopy_remote_fds
) {
240 g_array_free(mis
->postcopy_remote_fds
, TRUE
);
241 mis
->postcopy_remote_fds
= NULL
;
244 qemu_event_reset(&mis
->main_thread_load_event
);
246 if (mis
->page_requested
) {
247 g_tree_destroy(mis
->page_requested
);
248 mis
->page_requested
= NULL
;
251 if (mis
->socket_address_list
) {
252 qapi_free_SocketAddressList(mis
->socket_address_list
);
253 mis
->socket_address_list
= NULL
;
257 static void migrate_generate_event(int new_state
)
259 if (migrate_use_events()) {
260 qapi_event_send_migration(new_state
);
264 static bool migrate_late_block_activate(void)
268 s
= migrate_get_current();
270 return s
->enabled_capabilities
[
271 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE
];
275 * Called on -incoming with a defer: uri.
276 * The migration can be started later after any parameters have been
279 static void deferred_incoming_migration(Error
**errp
)
281 if (deferred_incoming
) {
282 error_setg(errp
, "Incoming migration already deferred");
284 deferred_incoming
= true;
288 * Send a message on the return channel back to the source
291 static int migrate_send_rp_message(MigrationIncomingState
*mis
,
292 enum mig_rp_message_type message_type
,
293 uint16_t len
, void *data
)
297 trace_migrate_send_rp_message((int)message_type
, len
);
298 qemu_mutex_lock(&mis
->rp_mutex
);
301 * It's possible that the file handle got lost due to network
304 if (!mis
->to_src_file
) {
309 qemu_put_be16(mis
->to_src_file
, (unsigned int)message_type
);
310 qemu_put_be16(mis
->to_src_file
, len
);
311 qemu_put_buffer(mis
->to_src_file
, data
, len
);
312 qemu_fflush(mis
->to_src_file
);
314 /* It's possible that qemu file got error during sending */
315 ret
= qemu_file_get_error(mis
->to_src_file
);
318 qemu_mutex_unlock(&mis
->rp_mutex
);
322 /* Request one page from the source VM at the given start address.
323 * rb: the RAMBlock to request the page in
324 * Start: Address offset within the RB
325 * Len: Length in bytes required - must be a multiple of pagesize
327 int migrate_send_rp_message_req_pages(MigrationIncomingState
*mis
,
328 RAMBlock
*rb
, ram_addr_t start
)
330 uint8_t bufc
[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
331 size_t msglen
= 12; /* start + len */
332 size_t len
= qemu_ram_pagesize(rb
);
333 enum mig_rp_message_type msg_type
;
337 *(uint64_t *)bufc
= cpu_to_be64((uint64_t)start
);
338 *(uint32_t *)(bufc
+ 8) = cpu_to_be32((uint32_t)len
);
341 * We maintain the last ramblock that we requested for page. Note that we
342 * don't need locking because this function will only be called within the
343 * postcopy ram fault thread.
345 if (rb
!= mis
->last_rb
) {
348 rbname
= qemu_ram_get_idstr(rb
);
349 rbname_len
= strlen(rbname
);
351 assert(rbname_len
< 256);
353 bufc
[msglen
++] = rbname_len
;
354 memcpy(bufc
+ msglen
, rbname
, rbname_len
);
355 msglen
+= rbname_len
;
356 msg_type
= MIG_RP_MSG_REQ_PAGES_ID
;
358 msg_type
= MIG_RP_MSG_REQ_PAGES
;
361 return migrate_send_rp_message(mis
, msg_type
, msglen
, bufc
);
364 int migrate_send_rp_req_pages(MigrationIncomingState
*mis
,
365 RAMBlock
*rb
, ram_addr_t start
, uint64_t haddr
)
367 void *aligned
= (void *)(uintptr_t)(haddr
& (-qemu_ram_pagesize(rb
)));
368 bool received
= false;
370 WITH_QEMU_LOCK_GUARD(&mis
->page_request_mutex
) {
371 received
= ramblock_recv_bitmap_test_byte_offset(rb
, start
);
372 if (!received
&& !g_tree_lookup(mis
->page_requested
, aligned
)) {
374 * The page has not been received, and it's not yet in the page
375 * request list. Queue it. Set the value of element to 1, so that
376 * things like g_tree_lookup() will return TRUE (1) when found.
378 g_tree_insert(mis
->page_requested
, aligned
, (gpointer
)1);
379 mis
->page_requested_count
++;
380 trace_postcopy_page_req_add(aligned
, mis
->page_requested_count
);
385 * If the page is there, skip sending the message. We don't even need the
386 * lock because as long as the page arrived, it'll be there forever.
392 return migrate_send_rp_message_req_pages(mis
, rb
, start
);
395 static bool migration_colo_enabled
;
396 bool migration_incoming_colo_enabled(void)
398 return migration_colo_enabled
;
401 void migration_incoming_disable_colo(void)
403 ram_block_discard_disable(false);
404 migration_colo_enabled
= false;
407 int migration_incoming_enable_colo(void)
409 if (ram_block_discard_disable(true)) {
410 error_report("COLO: cannot disable RAM discard");
413 migration_colo_enabled
= true;
417 void migrate_add_address(SocketAddress
*address
)
419 MigrationIncomingState
*mis
= migration_incoming_get_current();
420 SocketAddressList
*addrs
;
422 addrs
= g_new0(SocketAddressList
, 1);
423 addrs
->next
= mis
->socket_address_list
;
424 mis
->socket_address_list
= addrs
;
425 addrs
->value
= QAPI_CLONE(SocketAddress
, address
);
428 void qemu_start_incoming_migration(const char *uri
, Error
**errp
)
430 const char *p
= NULL
;
432 qapi_event_send_migration(MIGRATION_STATUS_SETUP
);
433 if (!strcmp(uri
, "defer")) {
434 deferred_incoming_migration(errp
);
435 } else if (strstart(uri
, "tcp:", &p
) ||
436 strstart(uri
, "unix:", NULL
) ||
437 strstart(uri
, "vsock:", NULL
)) {
438 socket_start_incoming_migration(p
? p
: uri
, errp
);
440 } else if (strstart(uri
, "rdma:", &p
)) {
441 rdma_start_incoming_migration(p
, errp
);
443 } else if (strstart(uri
, "exec:", &p
)) {
444 exec_start_incoming_migration(p
, errp
);
445 } else if (strstart(uri
, "fd:", &p
)) {
446 fd_start_incoming_migration(p
, errp
);
448 error_setg(errp
, "unknown migration protocol: %s", uri
);
452 static void process_incoming_migration_bh(void *opaque
)
454 Error
*local_err
= NULL
;
455 MigrationIncomingState
*mis
= opaque
;
457 /* If capability late_block_activate is set:
458 * Only fire up the block code now if we're going to restart the
459 * VM, else 'cont' will do it.
460 * This causes file locking to happen; so we don't want it to happen
461 * unless we really are starting the VM.
463 if (!migrate_late_block_activate() ||
464 (autostart
&& (!global_state_received() ||
465 global_state_get_runstate() == RUN_STATE_RUNNING
))) {
466 /* Make sure all file formats flush their mutable metadata.
467 * If we get an error here, just don't restart the VM yet. */
468 bdrv_invalidate_cache_all(&local_err
);
470 error_report_err(local_err
);
477 * This must happen after all error conditions are dealt with and
478 * we're sure the VM is going to be running on this host.
480 qemu_announce_self(&mis
->announce_timer
, migrate_announce_params());
482 if (multifd_load_cleanup(&local_err
) != 0) {
483 error_report_err(local_err
);
486 /* If global state section was not received or we are in running
487 state, we need to obey autostart. Any other state is set with
490 dirty_bitmap_mig_before_vm_start();
492 if (!global_state_received() ||
493 global_state_get_runstate() == RUN_STATE_RUNNING
) {
497 runstate_set(RUN_STATE_PAUSED
);
499 } else if (migration_incoming_colo_enabled()) {
500 migration_incoming_disable_colo();
503 runstate_set(global_state_get_runstate());
506 * This must happen after any state changes since as soon as an external
507 * observer sees this event they might start to prod at the VM assuming
510 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
511 MIGRATION_STATUS_COMPLETED
);
512 qemu_bh_delete(mis
->bh
);
513 migration_incoming_state_destroy();
516 static void process_incoming_migration_co(void *opaque
)
518 MigrationIncomingState
*mis
= migration_incoming_get_current();
521 Error
*local_err
= NULL
;
523 assert(mis
->from_src_file
);
524 mis
->migration_incoming_co
= qemu_coroutine_self();
525 mis
->largest_page_size
= qemu_ram_pagesize_largest();
526 postcopy_state_set(POSTCOPY_INCOMING_NONE
);
527 migrate_set_state(&mis
->state
, MIGRATION_STATUS_NONE
,
528 MIGRATION_STATUS_ACTIVE
);
529 ret
= qemu_loadvm_state(mis
->from_src_file
);
531 ps
= postcopy_state_get();
532 trace_process_incoming_migration_co_end(ret
, ps
);
533 if (ps
!= POSTCOPY_INCOMING_NONE
) {
534 if (ps
== POSTCOPY_INCOMING_ADVISE
) {
536 * Where a migration had postcopy enabled (and thus went to advise)
537 * but managed to complete within the precopy period, we can use
540 postcopy_ram_incoming_cleanup(mis
);
541 } else if (ret
>= 0) {
543 * Postcopy was started, cleanup should happen at the end of the
546 trace_process_incoming_migration_co_postcopy_end_main();
549 /* Else if something went wrong then just fall out of the normal exit */
552 /* we get COLO info, and know if we are in COLO mode */
553 if (!ret
&& migration_incoming_colo_enabled()) {
554 /* Make sure all file formats flush their mutable metadata */
555 bdrv_invalidate_cache_all(&local_err
);
557 error_report_err(local_err
);
561 qemu_thread_create(&mis
->colo_incoming_thread
, "COLO incoming",
562 colo_process_incoming_thread
, mis
, QEMU_THREAD_JOINABLE
);
563 mis
->have_colo_incoming_thread
= true;
564 qemu_coroutine_yield();
566 /* Wait checkpoint incoming thread exit before free resource */
567 qemu_thread_join(&mis
->colo_incoming_thread
);
568 /* We hold the global iothread lock, so it is safe here */
569 colo_release_ram_cache();
573 error_report("load of migration failed: %s", strerror(-ret
));
576 mis
->bh
= qemu_bh_new(process_incoming_migration_bh
, mis
);
577 qemu_bh_schedule(mis
->bh
);
578 mis
->migration_incoming_co
= NULL
;
582 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
583 MIGRATION_STATUS_FAILED
);
584 qemu_fclose(mis
->from_src_file
);
585 if (multifd_load_cleanup(&local_err
) != 0) {
586 error_report_err(local_err
);
592 * @migration_incoming_setup: Setup incoming migration
594 * Returns 0 for no error or 1 for error
596 * @f: file for main migration channel
597 * @errp: where to put errors
599 static int migration_incoming_setup(QEMUFile
*f
, Error
**errp
)
601 MigrationIncomingState
*mis
= migration_incoming_get_current();
602 Error
*local_err
= NULL
;
604 if (multifd_load_setup(&local_err
) != 0) {
605 /* We haven't been able to create multifd threads
606 nothing better to do */
607 error_report_err(local_err
);
611 if (!mis
->from_src_file
) {
612 mis
->from_src_file
= f
;
614 qemu_file_set_blocking(f
, false);
618 void migration_incoming_process(void)
620 Coroutine
*co
= qemu_coroutine_create(process_incoming_migration_co
, NULL
);
621 qemu_coroutine_enter(co
);
624 /* Returns true if recovered from a paused migration, otherwise false */
625 static bool postcopy_try_recover(QEMUFile
*f
)
627 MigrationIncomingState
*mis
= migration_incoming_get_current();
629 if (mis
->state
== MIGRATION_STATUS_POSTCOPY_PAUSED
) {
630 /* Resumed from a paused postcopy migration */
632 mis
->from_src_file
= f
;
633 /* Postcopy has standalone thread to do vm load */
634 qemu_file_set_blocking(f
, true);
636 /* Re-configure the return path */
637 mis
->to_src_file
= qemu_file_get_return_path(f
);
639 migrate_set_state(&mis
->state
, MIGRATION_STATUS_POSTCOPY_PAUSED
,
640 MIGRATION_STATUS_POSTCOPY_RECOVER
);
643 * Here, we only wake up the main loading thread (while the
644 * fault thread will still be waiting), so that we can receive
645 * commands from source now, and answer it if needed. The
646 * fault thread will be woken up afterwards until we are sure
647 * that source is ready to reply to page requests.
649 qemu_sem_post(&mis
->postcopy_pause_sem_dst
);
656 void migration_fd_process_incoming(QEMUFile
*f
, Error
**errp
)
658 Error
*local_err
= NULL
;
660 if (postcopy_try_recover(f
)) {
664 if (migration_incoming_setup(f
, &local_err
)) {
665 error_propagate(errp
, local_err
);
668 migration_incoming_process();
671 void migration_ioc_process_incoming(QIOChannel
*ioc
, Error
**errp
)
673 MigrationIncomingState
*mis
= migration_incoming_get_current();
674 Error
*local_err
= NULL
;
675 bool start_migration
;
677 if (!mis
->from_src_file
) {
678 /* The first connection (multifd may have multiple) */
679 QEMUFile
*f
= qemu_fopen_channel_input(ioc
);
681 /* If it's a recovery, we're done */
682 if (postcopy_try_recover(f
)) {
686 if (migration_incoming_setup(f
, &local_err
)) {
687 error_propagate(errp
, local_err
);
692 * Common migration only needs one channel, so we can start
693 * right now. Multifd needs more than one channel, we wait.
695 start_migration
= !migrate_use_multifd();
697 /* Multiple connections */
698 assert(migrate_use_multifd());
699 start_migration
= multifd_recv_new_channel(ioc
, &local_err
);
701 error_propagate(errp
, local_err
);
706 if (start_migration
) {
707 migration_incoming_process();
712 * @migration_has_all_channels: We have received all channels that we need
714 * Returns true when we have got connections to all the channels that
715 * we need for migration.
717 bool migration_has_all_channels(void)
719 MigrationIncomingState
*mis
= migration_incoming_get_current();
722 all_channels
= multifd_recv_all_channels_created();
724 return all_channels
&& mis
->from_src_file
!= NULL
;
728 * Send a 'SHUT' message on the return channel with the given value
729 * to indicate that we've finished with the RP. Non-0 value indicates
732 void migrate_send_rp_shut(MigrationIncomingState
*mis
,
737 buf
= cpu_to_be32(value
);
738 migrate_send_rp_message(mis
, MIG_RP_MSG_SHUT
, sizeof(buf
), &buf
);
742 * Send a 'PONG' message on the return channel with the given value
743 * (normally in response to a 'PING')
745 void migrate_send_rp_pong(MigrationIncomingState
*mis
,
750 buf
= cpu_to_be32(value
);
751 migrate_send_rp_message(mis
, MIG_RP_MSG_PONG
, sizeof(buf
), &buf
);
754 void migrate_send_rp_recv_bitmap(MigrationIncomingState
*mis
,
762 * First, we send the header part. It contains only the len of
763 * idstr, and the idstr itself.
765 len
= strlen(block_name
);
767 memcpy(buf
+ 1, block_name
, len
);
769 if (mis
->state
!= MIGRATION_STATUS_POSTCOPY_RECOVER
) {
770 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
775 migrate_send_rp_message(mis
, MIG_RP_MSG_RECV_BITMAP
, len
+ 1, buf
);
778 * Next, we dump the received bitmap to the stream.
780 * TODO: currently we are safe since we are the only one that is
781 * using the to_src_file handle (fault thread is still paused),
782 * and it's ok even not taking the mutex. However the best way is
783 * to take the lock before sending the message header, and release
784 * the lock after sending the bitmap.
786 qemu_mutex_lock(&mis
->rp_mutex
);
787 res
= ramblock_recv_bitmap_send(mis
->to_src_file
, block_name
);
788 qemu_mutex_unlock(&mis
->rp_mutex
);
790 trace_migrate_send_rp_recv_bitmap(block_name
, res
);
793 void migrate_send_rp_resume_ack(MigrationIncomingState
*mis
, uint32_t value
)
797 buf
= cpu_to_be32(value
);
798 migrate_send_rp_message(mis
, MIG_RP_MSG_RESUME_ACK
, sizeof(buf
), &buf
);
801 MigrationCapabilityStatusList
*qmp_query_migrate_capabilities(Error
**errp
)
803 MigrationCapabilityStatusList
*head
= NULL
;
804 MigrationCapabilityStatusList
*caps
;
805 MigrationState
*s
= migrate_get_current();
808 caps
= NULL
; /* silence compiler warning */
809 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
810 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
811 if (i
== MIGRATION_CAPABILITY_BLOCK
) {
816 head
= g_malloc0(sizeof(*caps
));
819 caps
->next
= g_malloc0(sizeof(*caps
));
823 g_malloc(sizeof(*caps
->value
));
824 caps
->value
->capability
= i
;
825 caps
->value
->state
= s
->enabled_capabilities
[i
];
831 MigrationParameters
*qmp_query_migrate_parameters(Error
**errp
)
833 MigrationParameters
*params
;
834 MigrationState
*s
= migrate_get_current();
836 /* TODO use QAPI_CLONE() instead of duplicating it inline */
837 params
= g_malloc0(sizeof(*params
));
838 params
->has_compress_level
= true;
839 params
->compress_level
= s
->parameters
.compress_level
;
840 params
->has_compress_threads
= true;
841 params
->compress_threads
= s
->parameters
.compress_threads
;
842 params
->has_compress_wait_thread
= true;
843 params
->compress_wait_thread
= s
->parameters
.compress_wait_thread
;
844 params
->has_decompress_threads
= true;
845 params
->decompress_threads
= s
->parameters
.decompress_threads
;
846 params
->has_throttle_trigger_threshold
= true;
847 params
->throttle_trigger_threshold
= s
->parameters
.throttle_trigger_threshold
;
848 params
->has_cpu_throttle_initial
= true;
849 params
->cpu_throttle_initial
= s
->parameters
.cpu_throttle_initial
;
850 params
->has_cpu_throttle_increment
= true;
851 params
->cpu_throttle_increment
= s
->parameters
.cpu_throttle_increment
;
852 params
->has_cpu_throttle_tailslow
= true;
853 params
->cpu_throttle_tailslow
= s
->parameters
.cpu_throttle_tailslow
;
854 params
->has_tls_creds
= true;
855 params
->tls_creds
= g_strdup(s
->parameters
.tls_creds
);
856 params
->has_tls_hostname
= true;
857 params
->tls_hostname
= g_strdup(s
->parameters
.tls_hostname
);
858 params
->has_tls_authz
= true;
859 params
->tls_authz
= g_strdup(s
->parameters
.tls_authz
?
860 s
->parameters
.tls_authz
: "");
861 params
->has_max_bandwidth
= true;
862 params
->max_bandwidth
= s
->parameters
.max_bandwidth
;
863 params
->has_downtime_limit
= true;
864 params
->downtime_limit
= s
->parameters
.downtime_limit
;
865 params
->has_x_checkpoint_delay
= true;
866 params
->x_checkpoint_delay
= s
->parameters
.x_checkpoint_delay
;
867 params
->has_block_incremental
= true;
868 params
->block_incremental
= s
->parameters
.block_incremental
;
869 params
->has_multifd_channels
= true;
870 params
->multifd_channels
= s
->parameters
.multifd_channels
;
871 params
->has_multifd_compression
= true;
872 params
->multifd_compression
= s
->parameters
.multifd_compression
;
873 params
->has_multifd_zlib_level
= true;
874 params
->multifd_zlib_level
= s
->parameters
.multifd_zlib_level
;
875 params
->has_multifd_zstd_level
= true;
876 params
->multifd_zstd_level
= s
->parameters
.multifd_zstd_level
;
877 params
->has_xbzrle_cache_size
= true;
878 params
->xbzrle_cache_size
= s
->parameters
.xbzrle_cache_size
;
879 params
->has_max_postcopy_bandwidth
= true;
880 params
->max_postcopy_bandwidth
= s
->parameters
.max_postcopy_bandwidth
;
881 params
->has_max_cpu_throttle
= true;
882 params
->max_cpu_throttle
= s
->parameters
.max_cpu_throttle
;
883 params
->has_announce_initial
= true;
884 params
->announce_initial
= s
->parameters
.announce_initial
;
885 params
->has_announce_max
= true;
886 params
->announce_max
= s
->parameters
.announce_max
;
887 params
->has_announce_rounds
= true;
888 params
->announce_rounds
= s
->parameters
.announce_rounds
;
889 params
->has_announce_step
= true;
890 params
->announce_step
= s
->parameters
.announce_step
;
892 if (s
->parameters
.has_block_bitmap_mapping
) {
893 params
->has_block_bitmap_mapping
= true;
894 params
->block_bitmap_mapping
=
895 QAPI_CLONE(BitmapMigrationNodeAliasList
,
896 s
->parameters
.block_bitmap_mapping
);
902 AnnounceParameters
*migrate_announce_params(void)
904 static AnnounceParameters ap
;
906 MigrationState
*s
= migrate_get_current();
908 ap
.initial
= s
->parameters
.announce_initial
;
909 ap
.max
= s
->parameters
.announce_max
;
910 ap
.rounds
= s
->parameters
.announce_rounds
;
911 ap
.step
= s
->parameters
.announce_step
;
917 * Return true if we're already in the middle of a migration
918 * (i.e. any of the active or setup states)
920 bool migration_is_setup_or_active(int state
)
923 case MIGRATION_STATUS_ACTIVE
:
924 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
925 case MIGRATION_STATUS_POSTCOPY_PAUSED
:
926 case MIGRATION_STATUS_POSTCOPY_RECOVER
:
927 case MIGRATION_STATUS_SETUP
:
928 case MIGRATION_STATUS_PRE_SWITCHOVER
:
929 case MIGRATION_STATUS_DEVICE
:
930 case MIGRATION_STATUS_WAIT_UNPLUG
:
931 case MIGRATION_STATUS_COLO
:
940 bool migration_is_running(int state
)
943 case MIGRATION_STATUS_ACTIVE
:
944 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
945 case MIGRATION_STATUS_POSTCOPY_PAUSED
:
946 case MIGRATION_STATUS_POSTCOPY_RECOVER
:
947 case MIGRATION_STATUS_SETUP
:
948 case MIGRATION_STATUS_PRE_SWITCHOVER
:
949 case MIGRATION_STATUS_DEVICE
:
950 case MIGRATION_STATUS_WAIT_UNPLUG
:
951 case MIGRATION_STATUS_CANCELLING
:
960 static void populate_time_info(MigrationInfo
*info
, MigrationState
*s
)
962 info
->has_status
= true;
963 info
->has_setup_time
= true;
964 info
->setup_time
= s
->setup_time
;
965 if (s
->state
== MIGRATION_STATUS_COMPLETED
) {
966 info
->has_total_time
= true;
967 info
->total_time
= s
->total_time
;
968 info
->has_downtime
= true;
969 info
->downtime
= s
->downtime
;
971 info
->has_total_time
= true;
972 info
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) -
974 info
->has_expected_downtime
= true;
975 info
->expected_downtime
= s
->expected_downtime
;
979 static void populate_ram_info(MigrationInfo
*info
, MigrationState
*s
)
981 info
->has_ram
= true;
982 info
->ram
= g_malloc0(sizeof(*info
->ram
));
983 info
->ram
->transferred
= ram_counters
.transferred
;
984 info
->ram
->total
= ram_bytes_total();
985 info
->ram
->duplicate
= ram_counters
.duplicate
;
986 /* legacy value. It is not used anymore */
987 info
->ram
->skipped
= 0;
988 info
->ram
->normal
= ram_counters
.normal
;
989 info
->ram
->normal_bytes
= ram_counters
.normal
*
990 qemu_target_page_size();
991 info
->ram
->mbps
= s
->mbps
;
992 info
->ram
->dirty_sync_count
= ram_counters
.dirty_sync_count
;
993 info
->ram
->postcopy_requests
= ram_counters
.postcopy_requests
;
994 info
->ram
->page_size
= qemu_target_page_size();
995 info
->ram
->multifd_bytes
= ram_counters
.multifd_bytes
;
996 info
->ram
->pages_per_second
= s
->pages_per_second
;
998 if (migrate_use_xbzrle()) {
999 info
->has_xbzrle_cache
= true;
1000 info
->xbzrle_cache
= g_malloc0(sizeof(*info
->xbzrle_cache
));
1001 info
->xbzrle_cache
->cache_size
= migrate_xbzrle_cache_size();
1002 info
->xbzrle_cache
->bytes
= xbzrle_counters
.bytes
;
1003 info
->xbzrle_cache
->pages
= xbzrle_counters
.pages
;
1004 info
->xbzrle_cache
->cache_miss
= xbzrle_counters
.cache_miss
;
1005 info
->xbzrle_cache
->cache_miss_rate
= xbzrle_counters
.cache_miss_rate
;
1006 info
->xbzrle_cache
->encoding_rate
= xbzrle_counters
.encoding_rate
;
1007 info
->xbzrle_cache
->overflow
= xbzrle_counters
.overflow
;
1010 if (migrate_use_compression()) {
1011 info
->has_compression
= true;
1012 info
->compression
= g_malloc0(sizeof(*info
->compression
));
1013 info
->compression
->pages
= compression_counters
.pages
;
1014 info
->compression
->busy
= compression_counters
.busy
;
1015 info
->compression
->busy_rate
= compression_counters
.busy_rate
;
1016 info
->compression
->compressed_size
=
1017 compression_counters
.compressed_size
;
1018 info
->compression
->compression_rate
=
1019 compression_counters
.compression_rate
;
1022 if (cpu_throttle_active()) {
1023 info
->has_cpu_throttle_percentage
= true;
1024 info
->cpu_throttle_percentage
= cpu_throttle_get_percentage();
1027 if (s
->state
!= MIGRATION_STATUS_COMPLETED
) {
1028 info
->ram
->remaining
= ram_bytes_remaining();
1029 info
->ram
->dirty_pages_rate
= ram_counters
.dirty_pages_rate
;
1033 static void populate_disk_info(MigrationInfo
*info
)
1035 if (blk_mig_active()) {
1036 info
->has_disk
= true;
1037 info
->disk
= g_malloc0(sizeof(*info
->disk
));
1038 info
->disk
->transferred
= blk_mig_bytes_transferred();
1039 info
->disk
->remaining
= blk_mig_bytes_remaining();
1040 info
->disk
->total
= blk_mig_bytes_total();
1044 static void populate_vfio_info(MigrationInfo
*info
)
1047 if (vfio_mig_active()) {
1048 info
->has_vfio
= true;
1049 info
->vfio
= g_malloc0(sizeof(*info
->vfio
));
1050 info
->vfio
->transferred
= vfio_mig_bytes_transferred();
1055 static void fill_source_migration_info(MigrationInfo
*info
)
1057 MigrationState
*s
= migrate_get_current();
1060 case MIGRATION_STATUS_NONE
:
1061 /* no migration has happened ever */
1062 /* do not overwrite destination migration status */
1064 case MIGRATION_STATUS_SETUP
:
1065 info
->has_status
= true;
1066 info
->has_total_time
= false;
1068 case MIGRATION_STATUS_ACTIVE
:
1069 case MIGRATION_STATUS_CANCELLING
:
1070 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
1071 case MIGRATION_STATUS_PRE_SWITCHOVER
:
1072 case MIGRATION_STATUS_DEVICE
:
1073 case MIGRATION_STATUS_POSTCOPY_PAUSED
:
1074 case MIGRATION_STATUS_POSTCOPY_RECOVER
:
1075 /* TODO add some postcopy stats */
1076 populate_time_info(info
, s
);
1077 populate_ram_info(info
, s
);
1078 populate_disk_info(info
);
1079 populate_vfio_info(info
);
1081 case MIGRATION_STATUS_COLO
:
1082 info
->has_status
= true;
1083 /* TODO: display COLO specific information (checkpoint info etc.) */
1085 case MIGRATION_STATUS_COMPLETED
:
1086 populate_time_info(info
, s
);
1087 populate_ram_info(info
, s
);
1088 populate_vfio_info(info
);
1090 case MIGRATION_STATUS_FAILED
:
1091 info
->has_status
= true;
1093 info
->has_error_desc
= true;
1094 info
->error_desc
= g_strdup(error_get_pretty(s
->error
));
1097 case MIGRATION_STATUS_CANCELLED
:
1098 info
->has_status
= true;
1100 case MIGRATION_STATUS_WAIT_UNPLUG
:
1101 info
->has_status
= true;
1104 info
->status
= s
->state
;
1108 * @migration_caps_check - check capability validity
1110 * @cap_list: old capability list, array of bool
1111 * @params: new capabilities to be applied soon
1112 * @errp: set *errp if the check failed, with reason
1114 * Returns true if check passed, otherwise false.
1116 static bool migrate_caps_check(bool *cap_list
,
1117 MigrationCapabilityStatusList
*params
,
1120 MigrationCapabilityStatusList
*cap
;
1121 bool old_postcopy_cap
;
1122 MigrationIncomingState
*mis
= migration_incoming_get_current();
1124 old_postcopy_cap
= cap_list
[MIGRATION_CAPABILITY_POSTCOPY_RAM
];
1126 for (cap
= params
; cap
; cap
= cap
->next
) {
1127 cap_list
[cap
->value
->capability
] = cap
->value
->state
;
1130 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
1131 if (cap_list
[MIGRATION_CAPABILITY_BLOCK
]) {
1132 error_setg(errp
, "QEMU compiled without old-style (blk/-b, inc/-i) "
1134 error_append_hint(errp
, "Use drive_mirror+NBD instead.\n");
1139 #ifndef CONFIG_REPLICATION
1140 if (cap_list
[MIGRATION_CAPABILITY_X_COLO
]) {
1141 error_setg(errp
, "QEMU compiled without replication module"
1142 " can't enable COLO");
1143 error_append_hint(errp
, "Please enable replication before COLO.\n");
1148 if (cap_list
[MIGRATION_CAPABILITY_POSTCOPY_RAM
]) {
1149 /* This check is reasonably expensive, so only when it's being
1150 * set the first time, also it's only the destination that needs
1153 if (!old_postcopy_cap
&& runstate_check(RUN_STATE_INMIGRATE
) &&
1154 !postcopy_ram_supported_by_host(mis
)) {
1155 /* postcopy_ram_supported_by_host will have emitted a more
1158 error_setg(errp
, "Postcopy is not supported");
1162 if (cap_list
[MIGRATION_CAPABILITY_X_IGNORE_SHARED
]) {
1163 error_setg(errp
, "Postcopy is not compatible with ignore-shared");
1171 static void fill_destination_migration_info(MigrationInfo
*info
)
1173 MigrationIncomingState
*mis
= migration_incoming_get_current();
1175 if (mis
->socket_address_list
) {
1176 info
->has_socket_address
= true;
1177 info
->socket_address
=
1178 QAPI_CLONE(SocketAddressList
, mis
->socket_address_list
);
1181 switch (mis
->state
) {
1182 case MIGRATION_STATUS_NONE
:
1184 case MIGRATION_STATUS_SETUP
:
1185 case MIGRATION_STATUS_CANCELLING
:
1186 case MIGRATION_STATUS_CANCELLED
:
1187 case MIGRATION_STATUS_ACTIVE
:
1188 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
1189 case MIGRATION_STATUS_POSTCOPY_PAUSED
:
1190 case MIGRATION_STATUS_POSTCOPY_RECOVER
:
1191 case MIGRATION_STATUS_FAILED
:
1192 case MIGRATION_STATUS_COLO
:
1193 info
->has_status
= true;
1195 case MIGRATION_STATUS_COMPLETED
:
1196 info
->has_status
= true;
1197 fill_destination_postcopy_migration_info(info
);
1200 info
->status
= mis
->state
;
1203 MigrationInfo
*qmp_query_migrate(Error
**errp
)
1205 MigrationInfo
*info
= g_malloc0(sizeof(*info
));
1207 fill_destination_migration_info(info
);
1208 fill_source_migration_info(info
);
1213 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList
*params
,
1216 MigrationState
*s
= migrate_get_current();
1217 MigrationCapabilityStatusList
*cap
;
1218 bool cap_list
[MIGRATION_CAPABILITY__MAX
];
1220 if (migration_is_running(s
->state
)) {
1221 error_setg(errp
, QERR_MIGRATION_ACTIVE
);
1225 memcpy(cap_list
, s
->enabled_capabilities
, sizeof(cap_list
));
1226 if (!migrate_caps_check(cap_list
, params
, errp
)) {
1230 for (cap
= params
; cap
; cap
= cap
->next
) {
1231 s
->enabled_capabilities
[cap
->value
->capability
] = cap
->value
->state
;
1236 * Check whether the parameters are valid. Error will be put into errp
1237 * (if provided). Return true if valid, otherwise false.
1239 static bool migrate_params_check(MigrationParameters
*params
, Error
**errp
)
1241 if (params
->has_compress_level
&&
1242 (params
->compress_level
> 9)) {
1243 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "compress_level",
1244 "is invalid, it should be in the range of 0 to 9");
1248 if (params
->has_compress_threads
&& (params
->compress_threads
< 1)) {
1249 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1251 "is invalid, it should be in the range of 1 to 255");
1255 if (params
->has_decompress_threads
&& (params
->decompress_threads
< 1)) {
1256 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1257 "decompress_threads",
1258 "is invalid, it should be in the range of 1 to 255");
1262 if (params
->has_throttle_trigger_threshold
&&
1263 (params
->throttle_trigger_threshold
< 1 ||
1264 params
->throttle_trigger_threshold
> 100)) {
1265 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1266 "throttle_trigger_threshold",
1267 "an integer in the range of 1 to 100");
1271 if (params
->has_cpu_throttle_initial
&&
1272 (params
->cpu_throttle_initial
< 1 ||
1273 params
->cpu_throttle_initial
> 99)) {
1274 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1275 "cpu_throttle_initial",
1276 "an integer in the range of 1 to 99");
1280 if (params
->has_cpu_throttle_increment
&&
1281 (params
->cpu_throttle_increment
< 1 ||
1282 params
->cpu_throttle_increment
> 99)) {
1283 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1284 "cpu_throttle_increment",
1285 "an integer in the range of 1 to 99");
1289 if (params
->has_max_bandwidth
&& (params
->max_bandwidth
> SIZE_MAX
)) {
1290 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1292 "an integer in the range of 0 to "stringify(SIZE_MAX
)
1297 if (params
->has_downtime_limit
&&
1298 (params
->downtime_limit
> MAX_MIGRATE_DOWNTIME
)) {
1299 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1301 "an integer in the range of 0 to "
1302 stringify(MAX_MIGRATE_DOWNTIME
)" ms");
1306 /* x_checkpoint_delay is now always positive */
1308 if (params
->has_multifd_channels
&& (params
->multifd_channels
< 1)) {
1309 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1311 "is invalid, it should be in the range of 1 to 255");
1315 if (params
->has_multifd_zlib_level
&&
1316 (params
->multifd_zlib_level
> 9)) {
1317 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "multifd_zlib_level",
1318 "is invalid, it should be in the range of 0 to 9");
1322 if (params
->has_multifd_zstd_level
&&
1323 (params
->multifd_zstd_level
> 20)) {
1324 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "multifd_zstd_level",
1325 "is invalid, it should be in the range of 0 to 20");
1329 if (params
->has_xbzrle_cache_size
&&
1330 (params
->xbzrle_cache_size
< qemu_target_page_size() ||
1331 !is_power_of_2(params
->xbzrle_cache_size
))) {
1332 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1333 "xbzrle_cache_size",
1334 "is invalid, it should be bigger than target page size"
1335 " and a power of 2");
1339 if (params
->has_max_cpu_throttle
&&
1340 (params
->max_cpu_throttle
< params
->cpu_throttle_initial
||
1341 params
->max_cpu_throttle
> 99)) {
1342 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1344 "an integer in the range of cpu_throttle_initial to 99");
1348 if (params
->has_announce_initial
&&
1349 params
->announce_initial
> 100000) {
1350 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1352 "is invalid, it must be less than 100000 ms");
1355 if (params
->has_announce_max
&&
1356 params
->announce_max
> 100000) {
1357 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1359 "is invalid, it must be less than 100000 ms");
1362 if (params
->has_announce_rounds
&&
1363 params
->announce_rounds
> 1000) {
1364 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1366 "is invalid, it must be in the range of 0 to 1000");
1369 if (params
->has_announce_step
&&
1370 (params
->announce_step
< 1 ||
1371 params
->announce_step
> 10000)) {
1372 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1374 "is invalid, it must be in the range of 1 to 10000 ms");
1378 if (params
->has_block_bitmap_mapping
&&
1379 !check_dirty_bitmap_mig_alias_map(params
->block_bitmap_mapping
, errp
)) {
1380 error_prepend(errp
, "Invalid mapping given for block-bitmap-mapping: ");
1387 static void migrate_params_test_apply(MigrateSetParameters
*params
,
1388 MigrationParameters
*dest
)
1390 *dest
= migrate_get_current()->parameters
;
1392 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1394 if (params
->has_compress_level
) {
1395 dest
->compress_level
= params
->compress_level
;
1398 if (params
->has_compress_threads
) {
1399 dest
->compress_threads
= params
->compress_threads
;
1402 if (params
->has_compress_wait_thread
) {
1403 dest
->compress_wait_thread
= params
->compress_wait_thread
;
1406 if (params
->has_decompress_threads
) {
1407 dest
->decompress_threads
= params
->decompress_threads
;
1410 if (params
->has_throttle_trigger_threshold
) {
1411 dest
->throttle_trigger_threshold
= params
->throttle_trigger_threshold
;
1414 if (params
->has_cpu_throttle_initial
) {
1415 dest
->cpu_throttle_initial
= params
->cpu_throttle_initial
;
1418 if (params
->has_cpu_throttle_increment
) {
1419 dest
->cpu_throttle_increment
= params
->cpu_throttle_increment
;
1422 if (params
->has_cpu_throttle_tailslow
) {
1423 dest
->cpu_throttle_tailslow
= params
->cpu_throttle_tailslow
;
1426 if (params
->has_tls_creds
) {
1427 assert(params
->tls_creds
->type
== QTYPE_QSTRING
);
1428 dest
->tls_creds
= params
->tls_creds
->u
.s
;
1431 if (params
->has_tls_hostname
) {
1432 assert(params
->tls_hostname
->type
== QTYPE_QSTRING
);
1433 dest
->tls_hostname
= params
->tls_hostname
->u
.s
;
1436 if (params
->has_max_bandwidth
) {
1437 dest
->max_bandwidth
= params
->max_bandwidth
;
1440 if (params
->has_downtime_limit
) {
1441 dest
->downtime_limit
= params
->downtime_limit
;
1444 if (params
->has_x_checkpoint_delay
) {
1445 dest
->x_checkpoint_delay
= params
->x_checkpoint_delay
;
1448 if (params
->has_block_incremental
) {
1449 dest
->block_incremental
= params
->block_incremental
;
1451 if (params
->has_multifd_channels
) {
1452 dest
->multifd_channels
= params
->multifd_channels
;
1454 if (params
->has_multifd_compression
) {
1455 dest
->multifd_compression
= params
->multifd_compression
;
1457 if (params
->has_xbzrle_cache_size
) {
1458 dest
->xbzrle_cache_size
= params
->xbzrle_cache_size
;
1460 if (params
->has_max_postcopy_bandwidth
) {
1461 dest
->max_postcopy_bandwidth
= params
->max_postcopy_bandwidth
;
1463 if (params
->has_max_cpu_throttle
) {
1464 dest
->max_cpu_throttle
= params
->max_cpu_throttle
;
1466 if (params
->has_announce_initial
) {
1467 dest
->announce_initial
= params
->announce_initial
;
1469 if (params
->has_announce_max
) {
1470 dest
->announce_max
= params
->announce_max
;
1472 if (params
->has_announce_rounds
) {
1473 dest
->announce_rounds
= params
->announce_rounds
;
1475 if (params
->has_announce_step
) {
1476 dest
->announce_step
= params
->announce_step
;
1479 if (params
->has_block_bitmap_mapping
) {
1480 dest
->has_block_bitmap_mapping
= true;
1481 dest
->block_bitmap_mapping
= params
->block_bitmap_mapping
;
1485 static void migrate_params_apply(MigrateSetParameters
*params
, Error
**errp
)
1487 MigrationState
*s
= migrate_get_current();
1489 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1491 if (params
->has_compress_level
) {
1492 s
->parameters
.compress_level
= params
->compress_level
;
1495 if (params
->has_compress_threads
) {
1496 s
->parameters
.compress_threads
= params
->compress_threads
;
1499 if (params
->has_compress_wait_thread
) {
1500 s
->parameters
.compress_wait_thread
= params
->compress_wait_thread
;
1503 if (params
->has_decompress_threads
) {
1504 s
->parameters
.decompress_threads
= params
->decompress_threads
;
1507 if (params
->has_throttle_trigger_threshold
) {
1508 s
->parameters
.throttle_trigger_threshold
= params
->throttle_trigger_threshold
;
1511 if (params
->has_cpu_throttle_initial
) {
1512 s
->parameters
.cpu_throttle_initial
= params
->cpu_throttle_initial
;
1515 if (params
->has_cpu_throttle_increment
) {
1516 s
->parameters
.cpu_throttle_increment
= params
->cpu_throttle_increment
;
1519 if (params
->has_cpu_throttle_tailslow
) {
1520 s
->parameters
.cpu_throttle_tailslow
= params
->cpu_throttle_tailslow
;
1523 if (params
->has_tls_creds
) {
1524 g_free(s
->parameters
.tls_creds
);
1525 assert(params
->tls_creds
->type
== QTYPE_QSTRING
);
1526 s
->parameters
.tls_creds
= g_strdup(params
->tls_creds
->u
.s
);
1529 if (params
->has_tls_hostname
) {
1530 g_free(s
->parameters
.tls_hostname
);
1531 assert(params
->tls_hostname
->type
== QTYPE_QSTRING
);
1532 s
->parameters
.tls_hostname
= g_strdup(params
->tls_hostname
->u
.s
);
1535 if (params
->has_tls_authz
) {
1536 g_free(s
->parameters
.tls_authz
);
1537 assert(params
->tls_authz
->type
== QTYPE_QSTRING
);
1538 s
->parameters
.tls_authz
= g_strdup(params
->tls_authz
->u
.s
);
1541 if (params
->has_max_bandwidth
) {
1542 s
->parameters
.max_bandwidth
= params
->max_bandwidth
;
1543 if (s
->to_dst_file
&& !migration_in_postcopy()) {
1544 qemu_file_set_rate_limit(s
->to_dst_file
,
1545 s
->parameters
.max_bandwidth
/ XFER_LIMIT_RATIO
);
1549 if (params
->has_downtime_limit
) {
1550 s
->parameters
.downtime_limit
= params
->downtime_limit
;
1553 if (params
->has_x_checkpoint_delay
) {
1554 s
->parameters
.x_checkpoint_delay
= params
->x_checkpoint_delay
;
1555 if (migration_in_colo_state()) {
1556 colo_checkpoint_notify(s
);
1560 if (params
->has_block_incremental
) {
1561 s
->parameters
.block_incremental
= params
->block_incremental
;
1563 if (params
->has_multifd_channels
) {
1564 s
->parameters
.multifd_channels
= params
->multifd_channels
;
1566 if (params
->has_multifd_compression
) {
1567 s
->parameters
.multifd_compression
= params
->multifd_compression
;
1569 if (params
->has_xbzrle_cache_size
) {
1570 s
->parameters
.xbzrle_cache_size
= params
->xbzrle_cache_size
;
1571 xbzrle_cache_resize(params
->xbzrle_cache_size
, errp
);
1573 if (params
->has_max_postcopy_bandwidth
) {
1574 s
->parameters
.max_postcopy_bandwidth
= params
->max_postcopy_bandwidth
;
1575 if (s
->to_dst_file
&& migration_in_postcopy()) {
1576 qemu_file_set_rate_limit(s
->to_dst_file
,
1577 s
->parameters
.max_postcopy_bandwidth
/ XFER_LIMIT_RATIO
);
1580 if (params
->has_max_cpu_throttle
) {
1581 s
->parameters
.max_cpu_throttle
= params
->max_cpu_throttle
;
1583 if (params
->has_announce_initial
) {
1584 s
->parameters
.announce_initial
= params
->announce_initial
;
1586 if (params
->has_announce_max
) {
1587 s
->parameters
.announce_max
= params
->announce_max
;
1589 if (params
->has_announce_rounds
) {
1590 s
->parameters
.announce_rounds
= params
->announce_rounds
;
1592 if (params
->has_announce_step
) {
1593 s
->parameters
.announce_step
= params
->announce_step
;
1596 if (params
->has_block_bitmap_mapping
) {
1597 qapi_free_BitmapMigrationNodeAliasList(
1598 s
->parameters
.block_bitmap_mapping
);
1600 s
->parameters
.has_block_bitmap_mapping
= true;
1601 s
->parameters
.block_bitmap_mapping
=
1602 QAPI_CLONE(BitmapMigrationNodeAliasList
,
1603 params
->block_bitmap_mapping
);
1607 void qmp_migrate_set_parameters(MigrateSetParameters
*params
, Error
**errp
)
1609 MigrationParameters tmp
;
1611 /* TODO Rewrite "" to null instead */
1612 if (params
->has_tls_creds
1613 && params
->tls_creds
->type
== QTYPE_QNULL
) {
1614 qobject_unref(params
->tls_creds
->u
.n
);
1615 params
->tls_creds
->type
= QTYPE_QSTRING
;
1616 params
->tls_creds
->u
.s
= strdup("");
1618 /* TODO Rewrite "" to null instead */
1619 if (params
->has_tls_hostname
1620 && params
->tls_hostname
->type
== QTYPE_QNULL
) {
1621 qobject_unref(params
->tls_hostname
->u
.n
);
1622 params
->tls_hostname
->type
= QTYPE_QSTRING
;
1623 params
->tls_hostname
->u
.s
= strdup("");
1626 migrate_params_test_apply(params
, &tmp
);
1628 if (!migrate_params_check(&tmp
, errp
)) {
1629 /* Invalid parameter */
1633 migrate_params_apply(params
, errp
);
1637 void qmp_migrate_start_postcopy(Error
**errp
)
1639 MigrationState
*s
= migrate_get_current();
1641 if (!migrate_postcopy()) {
1642 error_setg(errp
, "Enable postcopy with migrate_set_capability before"
1643 " the start of migration");
1647 if (s
->state
== MIGRATION_STATUS_NONE
) {
1648 error_setg(errp
, "Postcopy must be started after migration has been"
1653 * we don't error if migration has finished since that would be racy
1654 * with issuing this command.
1656 qatomic_set(&s
->start_postcopy
, true);
1659 /* shared migration helpers */
1661 void migrate_set_state(int *state
, int old_state
, int new_state
)
1663 assert(new_state
< MIGRATION_STATUS__MAX
);
1664 if (qatomic_cmpxchg(state
, old_state
, new_state
) == old_state
) {
1665 trace_migrate_set_state(MigrationStatus_str(new_state
));
1666 migrate_generate_event(new_state
);
1670 static MigrationCapabilityStatusList
*migrate_cap_add(
1671 MigrationCapabilityStatusList
*list
,
1672 MigrationCapability index
,
1675 MigrationCapabilityStatusList
*cap
;
1677 cap
= g_new0(MigrationCapabilityStatusList
, 1);
1678 cap
->value
= g_new0(MigrationCapabilityStatus
, 1);
1679 cap
->value
->capability
= index
;
1680 cap
->value
->state
= state
;
1686 void migrate_set_block_enabled(bool value
, Error
**errp
)
1688 MigrationCapabilityStatusList
*cap
;
1690 cap
= migrate_cap_add(NULL
, MIGRATION_CAPABILITY_BLOCK
, value
);
1691 qmp_migrate_set_capabilities(cap
, errp
);
1692 qapi_free_MigrationCapabilityStatusList(cap
);
1695 static void migrate_set_block_incremental(MigrationState
*s
, bool value
)
1697 s
->parameters
.block_incremental
= value
;
1700 static void block_cleanup_parameters(MigrationState
*s
)
1702 if (s
->must_remove_block_options
) {
1703 /* setting to false can never fail */
1704 migrate_set_block_enabled(false, &error_abort
);
1705 migrate_set_block_incremental(s
, false);
1706 s
->must_remove_block_options
= false;
1710 static void migrate_fd_cleanup(MigrationState
*s
)
1712 qemu_bh_delete(s
->cleanup_bh
);
1713 s
->cleanup_bh
= NULL
;
1715 qemu_savevm_state_cleanup();
1717 if (s
->to_dst_file
) {
1720 trace_migrate_fd_cleanup();
1721 qemu_mutex_unlock_iothread();
1722 if (s
->migration_thread_running
) {
1723 qemu_thread_join(&s
->thread
);
1724 s
->migration_thread_running
= false;
1726 qemu_mutex_lock_iothread();
1728 multifd_save_cleanup();
1729 qemu_mutex_lock(&s
->qemu_file_lock
);
1730 tmp
= s
->to_dst_file
;
1731 s
->to_dst_file
= NULL
;
1732 qemu_mutex_unlock(&s
->qemu_file_lock
);
1734 * Close the file handle without the lock to make sure the
1735 * critical section won't block for long.
1740 assert(!migration_is_active(s
));
1742 if (s
->state
== MIGRATION_STATUS_CANCELLING
) {
1743 migrate_set_state(&s
->state
, MIGRATION_STATUS_CANCELLING
,
1744 MIGRATION_STATUS_CANCELLED
);
1748 /* It is used on info migrate. We can't free it */
1749 error_report_err(error_copy(s
->error
));
1751 notifier_list_notify(&migration_state_notifiers
, s
);
1752 block_cleanup_parameters(s
);
1755 static void migrate_fd_cleanup_schedule(MigrationState
*s
)
1758 * Ref the state for bh, because it may be called when
1759 * there're already no other refs
1761 object_ref(OBJECT(s
));
1762 qemu_bh_schedule(s
->cleanup_bh
);
1765 static void migrate_fd_cleanup_bh(void *opaque
)
1767 MigrationState
*s
= opaque
;
1768 migrate_fd_cleanup(s
);
1769 object_unref(OBJECT(s
));
1772 void migrate_set_error(MigrationState
*s
, const Error
*error
)
1774 QEMU_LOCK_GUARD(&s
->error_mutex
);
1776 s
->error
= error_copy(error
);
1780 void migrate_fd_error(MigrationState
*s
, const Error
*error
)
1782 trace_migrate_fd_error(error_get_pretty(error
));
1783 assert(s
->to_dst_file
== NULL
);
1784 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
1785 MIGRATION_STATUS_FAILED
);
1786 migrate_set_error(s
, error
);
1789 static void migrate_fd_cancel(MigrationState
*s
)
1792 QEMUFile
*f
= migrate_get_current()->to_dst_file
;
1793 trace_migrate_fd_cancel();
1795 if (s
->rp_state
.from_dst_file
) {
1796 /* shutdown the rp socket, so causing the rp thread to shutdown */
1797 qemu_file_shutdown(s
->rp_state
.from_dst_file
);
1801 old_state
= s
->state
;
1802 if (!migration_is_running(old_state
)) {
1805 /* If the migration is paused, kick it out of the pause */
1806 if (old_state
== MIGRATION_STATUS_PRE_SWITCHOVER
) {
1807 qemu_sem_post(&s
->pause_sem
);
1809 migrate_set_state(&s
->state
, old_state
, MIGRATION_STATUS_CANCELLING
);
1810 } while (s
->state
!= MIGRATION_STATUS_CANCELLING
);
1813 * If we're unlucky the migration code might be stuck somewhere in a
1814 * send/write while the network has failed and is waiting to timeout;
1815 * if we've got shutdown(2) available then we can force it to quit.
1816 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1817 * called in a bh, so there is no race against this cancel.
1819 if (s
->state
== MIGRATION_STATUS_CANCELLING
&& f
) {
1820 qemu_file_shutdown(f
);
1822 if (s
->state
== MIGRATION_STATUS_CANCELLING
&& s
->block_inactive
) {
1823 Error
*local_err
= NULL
;
1825 bdrv_invalidate_cache_all(&local_err
);
1827 error_report_err(local_err
);
1829 s
->block_inactive
= false;
1834 void add_migration_state_change_notifier(Notifier
*notify
)
1836 notifier_list_add(&migration_state_notifiers
, notify
);
1839 void remove_migration_state_change_notifier(Notifier
*notify
)
1841 notifier_remove(notify
);
1844 bool migration_in_setup(MigrationState
*s
)
1846 return s
->state
== MIGRATION_STATUS_SETUP
;
1849 bool migration_has_finished(MigrationState
*s
)
1851 return s
->state
== MIGRATION_STATUS_COMPLETED
;
1854 bool migration_has_failed(MigrationState
*s
)
1856 return (s
->state
== MIGRATION_STATUS_CANCELLED
||
1857 s
->state
== MIGRATION_STATUS_FAILED
);
1860 bool migration_in_postcopy(void)
1862 MigrationState
*s
= migrate_get_current();
1865 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
1866 case MIGRATION_STATUS_POSTCOPY_PAUSED
:
1867 case MIGRATION_STATUS_POSTCOPY_RECOVER
:
1874 bool migration_in_postcopy_after_devices(MigrationState
*s
)
1876 return migration_in_postcopy() && s
->postcopy_after_devices
;
1879 bool migration_in_incoming_postcopy(void)
1881 PostcopyState ps
= postcopy_state_get();
1883 return ps
>= POSTCOPY_INCOMING_DISCARD
&& ps
< POSTCOPY_INCOMING_END
;
1886 bool migration_is_idle(void)
1888 MigrationState
*s
= current_migration
;
1895 case MIGRATION_STATUS_NONE
:
1896 case MIGRATION_STATUS_CANCELLED
:
1897 case MIGRATION_STATUS_COMPLETED
:
1898 case MIGRATION_STATUS_FAILED
:
1900 case MIGRATION_STATUS_SETUP
:
1901 case MIGRATION_STATUS_CANCELLING
:
1902 case MIGRATION_STATUS_ACTIVE
:
1903 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
1904 case MIGRATION_STATUS_COLO
:
1905 case MIGRATION_STATUS_PRE_SWITCHOVER
:
1906 case MIGRATION_STATUS_DEVICE
:
1907 case MIGRATION_STATUS_WAIT_UNPLUG
:
1909 case MIGRATION_STATUS__MAX
:
1910 g_assert_not_reached();
1916 bool migration_is_active(MigrationState
*s
)
1918 return (s
->state
== MIGRATION_STATUS_ACTIVE
||
1919 s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
);
1922 void migrate_init(MigrationState
*s
)
1925 * Reinitialise all migration state, except
1926 * parameters/capabilities that the user set, and
1930 s
->to_dst_file
= NULL
;
1931 s
->state
= MIGRATION_STATUS_NONE
;
1932 s
->rp_state
.from_dst_file
= NULL
;
1933 s
->rp_state
.error
= false;
1935 s
->pages_per_second
= 0.0;
1937 s
->expected_downtime
= 0;
1939 s
->start_postcopy
= false;
1940 s
->postcopy_after_devices
= false;
1941 s
->migration_thread_running
= false;
1942 error_free(s
->error
);
1946 migrate_set_state(&s
->state
, MIGRATION_STATUS_NONE
, MIGRATION_STATUS_SETUP
);
1948 s
->start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1950 s
->vm_was_running
= false;
1951 s
->iteration_initial_bytes
= 0;
1952 s
->threshold_size
= 0;
1955 static GSList
*migration_blockers
;
1957 int migrate_add_blocker(Error
*reason
, Error
**errp
)
1959 if (only_migratable
) {
1960 error_propagate_prepend(errp
, error_copy(reason
),
1961 "disallowing migration blocker "
1962 "(--only-migratable) for: ");
1966 if (migration_is_idle()) {
1967 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
1971 error_propagate_prepend(errp
, error_copy(reason
),
1972 "disallowing migration blocker "
1973 "(migration in progress) for: ");
1977 void migrate_del_blocker(Error
*reason
)
1979 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
1982 void qmp_migrate_incoming(const char *uri
, Error
**errp
)
1984 Error
*local_err
= NULL
;
1985 static bool once
= true;
1987 if (!deferred_incoming
) {
1988 error_setg(errp
, "For use with '-incoming defer'");
1992 error_setg(errp
, "The incoming migration has already been started");
1996 qemu_start_incoming_migration(uri
, &local_err
);
1999 error_propagate(errp
, local_err
);
2006 void qmp_migrate_recover(const char *uri
, Error
**errp
)
2008 MigrationIncomingState
*mis
= migration_incoming_get_current();
2010 if (mis
->state
!= MIGRATION_STATUS_POSTCOPY_PAUSED
) {
2011 error_setg(errp
, "Migrate recover can only be run "
2012 "when postcopy is paused.");
2016 if (qatomic_cmpxchg(&mis
->postcopy_recover_triggered
,
2017 false, true) == true) {
2018 error_setg(errp
, "Migrate recovery is triggered already");
2023 * Note that this call will never start a real migration; it will
2024 * only re-setup the migration stream and poke existing migration
2025 * to continue using that newly established channel.
2027 qemu_start_incoming_migration(uri
, errp
);
2030 void qmp_migrate_pause(Error
**errp
)
2032 MigrationState
*ms
= migrate_get_current();
2033 MigrationIncomingState
*mis
= migration_incoming_get_current();
2036 if (ms
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
2037 /* Source side, during postcopy */
2038 qemu_mutex_lock(&ms
->qemu_file_lock
);
2039 ret
= qemu_file_shutdown(ms
->to_dst_file
);
2040 qemu_mutex_unlock(&ms
->qemu_file_lock
);
2042 error_setg(errp
, "Failed to pause source migration");
2047 if (mis
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
2048 ret
= qemu_file_shutdown(mis
->from_src_file
);
2050 error_setg(errp
, "Failed to pause destination migration");
2055 error_setg(errp
, "migrate-pause is currently only supported "
2056 "during postcopy-active state");
2059 bool migration_is_blocked(Error
**errp
)
2061 if (qemu_savevm_state_blocked(errp
)) {
2065 if (migration_blockers
) {
2066 error_propagate(errp
, error_copy(migration_blockers
->data
));
2073 /* Returns true if continue to migrate, or false if error detected */
2074 static bool migrate_prepare(MigrationState
*s
, bool blk
, bool blk_inc
,
2075 bool resume
, Error
**errp
)
2077 Error
*local_err
= NULL
;
2080 if (s
->state
!= MIGRATION_STATUS_POSTCOPY_PAUSED
) {
2081 error_setg(errp
, "Cannot resume if there is no "
2082 "paused migration");
2087 * Postcopy recovery won't work well with release-ram
2088 * capability since release-ram will drop the page buffer as
2089 * long as the page is put into the send buffer. So if there
2090 * is a network failure happened, any page buffers that have
2091 * not yet reached the destination VM but have already been
2092 * sent from the source VM will be lost forever. Let's refuse
2093 * the client from resuming such a postcopy migration.
2094 * Luckily release-ram was designed to only be used when src
2095 * and destination VMs are on the same host, so it should be
2098 if (migrate_release_ram()) {
2099 error_setg(errp
, "Postcopy recovery cannot work "
2100 "when release-ram capability is set");
2104 /* This is a resume, skip init status */
2108 if (migration_is_running(s
->state
)) {
2109 error_setg(errp
, QERR_MIGRATION_ACTIVE
);
2113 if (runstate_check(RUN_STATE_INMIGRATE
)) {
2114 error_setg(errp
, "Guest is waiting for an incoming migration");
2118 if (migration_is_blocked(errp
)) {
2122 if (blk
|| blk_inc
) {
2123 if (migrate_use_block() || migrate_use_block_incremental()) {
2124 error_setg(errp
, "Command options are incompatible with "
2125 "current migration capabilities");
2128 migrate_set_block_enabled(true, &local_err
);
2130 error_propagate(errp
, local_err
);
2133 s
->must_remove_block_options
= true;
2137 migrate_set_block_incremental(s
, true);
2142 * set ram_counters memory to zero for a
2145 memset(&ram_counters
, 0, sizeof(ram_counters
));
2150 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
2151 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
2152 bool has_resume
, bool resume
, Error
**errp
)
2154 Error
*local_err
= NULL
;
2155 MigrationState
*s
= migrate_get_current();
2156 const char *p
= NULL
;
2158 if (!migrate_prepare(s
, has_blk
&& blk
, has_inc
&& inc
,
2159 has_resume
&& resume
, errp
)) {
2160 /* Error detected, put into errp */
2164 if (strstart(uri
, "tcp:", &p
) ||
2165 strstart(uri
, "unix:", NULL
) ||
2166 strstart(uri
, "vsock:", NULL
)) {
2167 socket_start_outgoing_migration(s
, p
? p
: uri
, &local_err
);
2169 } else if (strstart(uri
, "rdma:", &p
)) {
2170 rdma_start_outgoing_migration(s
, p
, &local_err
);
2172 } else if (strstart(uri
, "exec:", &p
)) {
2173 exec_start_outgoing_migration(s
, p
, &local_err
);
2174 } else if (strstart(uri
, "fd:", &p
)) {
2175 fd_start_outgoing_migration(s
, p
, &local_err
);
2177 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri",
2178 "a valid migration protocol");
2179 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
2180 MIGRATION_STATUS_FAILED
);
2181 block_cleanup_parameters(s
);
2186 migrate_fd_error(s
, local_err
);
2187 error_propagate(errp
, local_err
);
2192 void qmp_migrate_cancel(Error
**errp
)
2194 migrate_fd_cancel(migrate_get_current());
2197 void qmp_migrate_continue(MigrationStatus state
, Error
**errp
)
2199 MigrationState
*s
= migrate_get_current();
2200 if (s
->state
!= state
) {
2201 error_setg(errp
, "Migration not in expected state: %s",
2202 MigrationStatus_str(s
->state
));
2205 qemu_sem_post(&s
->pause_sem
);
2208 void qmp_migrate_set_cache_size(int64_t value
, Error
**errp
)
2210 MigrateSetParameters p
= {
2211 .has_xbzrle_cache_size
= true,
2212 .xbzrle_cache_size
= value
,
2215 qmp_migrate_set_parameters(&p
, errp
);
2218 int64_t qmp_query_migrate_cache_size(Error
**errp
)
2220 return migrate_xbzrle_cache_size();
2223 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
2225 MigrateSetParameters p
= {
2226 .has_max_bandwidth
= true,
2227 .max_bandwidth
= value
,
2230 qmp_migrate_set_parameters(&p
, errp
);
2233 void qmp_migrate_set_downtime(double value
, Error
**errp
)
2235 if (value
< 0 || value
> MAX_MIGRATE_DOWNTIME_SECONDS
) {
2236 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
2238 "an integer in the range of 0 to "
2239 stringify(MAX_MIGRATE_DOWNTIME_SECONDS
)" seconds");
2243 value
*= 1000; /* Convert to milliseconds */
2245 MigrateSetParameters p
= {
2246 .has_downtime_limit
= true,
2247 .downtime_limit
= (int64_t)value
,
2250 qmp_migrate_set_parameters(&p
, errp
);
2253 bool migrate_release_ram(void)
2257 s
= migrate_get_current();
2259 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_RELEASE_RAM
];
2262 bool migrate_postcopy_ram(void)
2266 s
= migrate_get_current();
2268 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_POSTCOPY_RAM
];
2271 bool migrate_postcopy(void)
2273 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
2276 bool migrate_auto_converge(void)
2280 s
= migrate_get_current();
2282 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_AUTO_CONVERGE
];
2285 bool migrate_zero_blocks(void)
2289 s
= migrate_get_current();
2291 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_ZERO_BLOCKS
];
2294 bool migrate_postcopy_blocktime(void)
2298 s
= migrate_get_current();
2300 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME
];
2303 bool migrate_use_compression(void)
2307 s
= migrate_get_current();
2309 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_COMPRESS
];
2312 int migrate_compress_level(void)
2316 s
= migrate_get_current();
2318 return s
->parameters
.compress_level
;
2321 int migrate_compress_threads(void)
2325 s
= migrate_get_current();
2327 return s
->parameters
.compress_threads
;
2330 int migrate_compress_wait_thread(void)
2334 s
= migrate_get_current();
2336 return s
->parameters
.compress_wait_thread
;
2339 int migrate_decompress_threads(void)
2343 s
= migrate_get_current();
2345 return s
->parameters
.decompress_threads
;
2348 bool migrate_dirty_bitmaps(void)
2352 s
= migrate_get_current();
2354 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_DIRTY_BITMAPS
];
2357 bool migrate_ignore_shared(void)
2361 s
= migrate_get_current();
2363 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_X_IGNORE_SHARED
];
2366 bool migrate_validate_uuid(void)
2370 s
= migrate_get_current();
2372 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_VALIDATE_UUID
];
2375 bool migrate_use_events(void)
2379 s
= migrate_get_current();
2381 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_EVENTS
];
2384 bool migrate_use_multifd(void)
2388 s
= migrate_get_current();
2390 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_MULTIFD
];
2393 bool migrate_pause_before_switchover(void)
2397 s
= migrate_get_current();
2399 return s
->enabled_capabilities
[
2400 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER
];
2403 int migrate_multifd_channels(void)
2407 s
= migrate_get_current();
2409 return s
->parameters
.multifd_channels
;
2412 MultiFDCompression
migrate_multifd_compression(void)
2416 s
= migrate_get_current();
2418 return s
->parameters
.multifd_compression
;
2421 int migrate_multifd_zlib_level(void)
2425 s
= migrate_get_current();
2427 return s
->parameters
.multifd_zlib_level
;
2430 int migrate_multifd_zstd_level(void)
2434 s
= migrate_get_current();
2436 return s
->parameters
.multifd_zstd_level
;
2439 int migrate_use_xbzrle(void)
2443 s
= migrate_get_current();
2445 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_XBZRLE
];
2448 int64_t migrate_xbzrle_cache_size(void)
2452 s
= migrate_get_current();
2454 return s
->parameters
.xbzrle_cache_size
;
2457 static int64_t migrate_max_postcopy_bandwidth(void)
2461 s
= migrate_get_current();
2463 return s
->parameters
.max_postcopy_bandwidth
;
2466 bool migrate_use_block(void)
2470 s
= migrate_get_current();
2472 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_BLOCK
];
2475 bool migrate_use_return_path(void)
2479 s
= migrate_get_current();
2481 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_RETURN_PATH
];
2484 bool migrate_use_block_incremental(void)
2488 s
= migrate_get_current();
2490 return s
->parameters
.block_incremental
;
2493 /* migration thread support */
2495 * Something bad happened to the RP stream, mark an error
2496 * The caller shall print or trace something to indicate why
2498 static void mark_source_rp_bad(MigrationState
*s
)
2500 s
->rp_state
.error
= true;
2503 static struct rp_cmd_args
{
2504 ssize_t len
; /* -1 = variable */
2507 [MIG_RP_MSG_INVALID
] = { .len
= -1, .name
= "INVALID" },
2508 [MIG_RP_MSG_SHUT
] = { .len
= 4, .name
= "SHUT" },
2509 [MIG_RP_MSG_PONG
] = { .len
= 4, .name
= "PONG" },
2510 [MIG_RP_MSG_REQ_PAGES
] = { .len
= 12, .name
= "REQ_PAGES" },
2511 [MIG_RP_MSG_REQ_PAGES_ID
] = { .len
= -1, .name
= "REQ_PAGES_ID" },
2512 [MIG_RP_MSG_RECV_BITMAP
] = { .len
= -1, .name
= "RECV_BITMAP" },
2513 [MIG_RP_MSG_RESUME_ACK
] = { .len
= 4, .name
= "RESUME_ACK" },
2514 [MIG_RP_MSG_MAX
] = { .len
= -1, .name
= "MAX" },
2518 * Process a request for pages received on the return path,
2519 * We're allowed to send more than requested (e.g. to round to our page size)
2520 * and we don't need to send pages that have already been sent.
2522 static void migrate_handle_rp_req_pages(MigrationState
*ms
, const char* rbname
,
2523 ram_addr_t start
, size_t len
)
2525 long our_host_ps
= qemu_real_host_page_size
;
2527 trace_migrate_handle_rp_req_pages(rbname
, start
, len
);
2530 * Since we currently insist on matching page sizes, just sanity check
2531 * we're being asked for whole host pages.
2533 if (start
& (our_host_ps
- 1) ||
2534 (len
& (our_host_ps
- 1))) {
2535 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
2536 " len: %zd", __func__
, start
, len
);
2537 mark_source_rp_bad(ms
);
2541 if (ram_save_queue_pages(rbname
, start
, len
)) {
2542 mark_source_rp_bad(ms
);
2546 /* Return true to retry, false to quit */
2547 static bool postcopy_pause_return_path_thread(MigrationState
*s
)
2549 trace_postcopy_pause_return_path();
2551 qemu_sem_wait(&s
->postcopy_pause_rp_sem
);
2553 trace_postcopy_pause_return_path_continued();
2558 static int migrate_handle_rp_recv_bitmap(MigrationState
*s
, char *block_name
)
2560 RAMBlock
*block
= qemu_ram_block_by_name(block_name
);
2563 error_report("%s: invalid block name '%s'", __func__
, block_name
);
2567 /* Fetch the received bitmap and refresh the dirty bitmap */
2568 return ram_dirty_bitmap_reload(s
, block
);
2571 static int migrate_handle_rp_resume_ack(MigrationState
*s
, uint32_t value
)
2573 trace_source_return_path_thread_resume_ack(value
);
2575 if (value
!= MIGRATION_RESUME_ACK_VALUE
) {
2576 error_report("%s: illegal resume_ack value %"PRIu32
,
2581 /* Now both sides are active. */
2582 migrate_set_state(&s
->state
, MIGRATION_STATUS_POSTCOPY_RECOVER
,
2583 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
2585 /* Notify send thread that time to continue send pages */
2586 qemu_sem_post(&s
->rp_state
.rp_sem
);
2592 * Handles messages sent on the return path towards the source VM
2595 static void *source_return_path_thread(void *opaque
)
2597 MigrationState
*ms
= opaque
;
2598 QEMUFile
*rp
= ms
->rp_state
.from_dst_file
;
2599 uint16_t header_len
, header_type
;
2601 uint32_t tmp32
, sibling_error
;
2602 ram_addr_t start
= 0; /* =0 to silence warning */
2603 size_t len
= 0, expected_len
;
2606 trace_source_return_path_thread_entry();
2607 rcu_register_thread();
2610 while (!ms
->rp_state
.error
&& !qemu_file_get_error(rp
) &&
2611 migration_is_setup_or_active(ms
->state
)) {
2612 trace_source_return_path_thread_loop_top();
2613 header_type
= qemu_get_be16(rp
);
2614 header_len
= qemu_get_be16(rp
);
2616 if (qemu_file_get_error(rp
)) {
2617 mark_source_rp_bad(ms
);
2621 if (header_type
>= MIG_RP_MSG_MAX
||
2622 header_type
== MIG_RP_MSG_INVALID
) {
2623 error_report("RP: Received invalid message 0x%04x length 0x%04x",
2624 header_type
, header_len
);
2625 mark_source_rp_bad(ms
);
2629 if ((rp_cmd_args
[header_type
].len
!= -1 &&
2630 header_len
!= rp_cmd_args
[header_type
].len
) ||
2631 header_len
> sizeof(buf
)) {
2632 error_report("RP: Received '%s' message (0x%04x) with"
2633 "incorrect length %d expecting %zu",
2634 rp_cmd_args
[header_type
].name
, header_type
, header_len
,
2635 (size_t)rp_cmd_args
[header_type
].len
);
2636 mark_source_rp_bad(ms
);
2640 /* We know we've got a valid header by this point */
2641 res
= qemu_get_buffer(rp
, buf
, header_len
);
2642 if (res
!= header_len
) {
2643 error_report("RP: Failed reading data for message 0x%04x"
2644 " read %d expected %d",
2645 header_type
, res
, header_len
);
2646 mark_source_rp_bad(ms
);
2650 /* OK, we have the message and the data */
2651 switch (header_type
) {
2652 case MIG_RP_MSG_SHUT
:
2653 sibling_error
= ldl_be_p(buf
);
2654 trace_source_return_path_thread_shut(sibling_error
);
2655 if (sibling_error
) {
2656 error_report("RP: Sibling indicated error %d", sibling_error
);
2657 mark_source_rp_bad(ms
);
2660 * We'll let the main thread deal with closing the RP
2661 * we could do a shutdown(2) on it, but we're the only user
2662 * anyway, so there's nothing gained.
2666 case MIG_RP_MSG_PONG
:
2667 tmp32
= ldl_be_p(buf
);
2668 trace_source_return_path_thread_pong(tmp32
);
2671 case MIG_RP_MSG_REQ_PAGES
:
2672 start
= ldq_be_p(buf
);
2673 len
= ldl_be_p(buf
+ 8);
2674 migrate_handle_rp_req_pages(ms
, NULL
, start
, len
);
2677 case MIG_RP_MSG_REQ_PAGES_ID
:
2678 expected_len
= 12 + 1; /* header + termination */
2680 if (header_len
>= expected_len
) {
2681 start
= ldq_be_p(buf
);
2682 len
= ldl_be_p(buf
+ 8);
2683 /* Now we expect an idstr */
2684 tmp32
= buf
[12]; /* Length of the following idstr */
2685 buf
[13 + tmp32
] = '\0';
2686 expected_len
+= tmp32
;
2688 if (header_len
!= expected_len
) {
2689 error_report("RP: Req_Page_id with length %d expecting %zd",
2690 header_len
, expected_len
);
2691 mark_source_rp_bad(ms
);
2694 migrate_handle_rp_req_pages(ms
, (char *)&buf
[13], start
, len
);
2697 case MIG_RP_MSG_RECV_BITMAP
:
2698 if (header_len
< 1) {
2699 error_report("%s: missing block name", __func__
);
2700 mark_source_rp_bad(ms
);
2703 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2704 buf
[buf
[0] + 1] = '\0';
2705 if (migrate_handle_rp_recv_bitmap(ms
, (char *)(buf
+ 1))) {
2706 mark_source_rp_bad(ms
);
2711 case MIG_RP_MSG_RESUME_ACK
:
2712 tmp32
= ldl_be_p(buf
);
2713 if (migrate_handle_rp_resume_ack(ms
, tmp32
)) {
2714 mark_source_rp_bad(ms
);
2725 res
= qemu_file_get_error(rp
);
2727 if (res
== -EIO
&& migration_in_postcopy()) {
2729 * Maybe there is something we can do: it looks like a
2730 * network down issue, and we pause for a recovery.
2732 if (postcopy_pause_return_path_thread(ms
)) {
2733 /* Reload rp, reset the rest */
2734 if (rp
!= ms
->rp_state
.from_dst_file
) {
2736 rp
= ms
->rp_state
.from_dst_file
;
2738 ms
->rp_state
.error
= false;
2743 trace_source_return_path_thread_bad_end();
2744 mark_source_rp_bad(ms
);
2747 trace_source_return_path_thread_end();
2748 ms
->rp_state
.from_dst_file
= NULL
;
2750 rcu_unregister_thread();
2754 static int open_return_path_on_source(MigrationState
*ms
,
2758 ms
->rp_state
.from_dst_file
= qemu_file_get_return_path(ms
->to_dst_file
);
2759 if (!ms
->rp_state
.from_dst_file
) {
2763 trace_open_return_path_on_source();
2765 if (!create_thread
) {
2770 qemu_thread_create(&ms
->rp_state
.rp_thread
, "return path",
2771 source_return_path_thread
, ms
, QEMU_THREAD_JOINABLE
);
2773 trace_open_return_path_on_source_continue();
2778 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2779 static int await_return_path_close_on_source(MigrationState
*ms
)
2782 * If this is a normal exit then the destination will send a SHUT and the
2783 * rp_thread will exit, however if there's an error we need to cause
2786 if (qemu_file_get_error(ms
->to_dst_file
) && ms
->rp_state
.from_dst_file
) {
2788 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2789 * waiting for the destination.
2791 qemu_file_shutdown(ms
->rp_state
.from_dst_file
);
2792 mark_source_rp_bad(ms
);
2794 trace_await_return_path_close_on_source_joining();
2795 qemu_thread_join(&ms
->rp_state
.rp_thread
);
2796 trace_await_return_path_close_on_source_close();
2797 return ms
->rp_state
.error
;
2801 * Switch from normal iteration to postcopy
2802 * Returns non-0 on error
2804 static int postcopy_start(MigrationState
*ms
)
2807 QIOChannelBuffer
*bioc
;
2809 int64_t time_at_stop
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
2810 int64_t bandwidth
= migrate_max_postcopy_bandwidth();
2811 bool restart_block
= false;
2812 int cur_state
= MIGRATION_STATUS_ACTIVE
;
2813 if (!migrate_pause_before_switchover()) {
2814 migrate_set_state(&ms
->state
, MIGRATION_STATUS_ACTIVE
,
2815 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
2818 trace_postcopy_start();
2819 qemu_mutex_lock_iothread();
2820 trace_postcopy_start_set_run();
2822 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, NULL
);
2823 global_state_store();
2824 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
2829 ret
= migration_maybe_pause(ms
, &cur_state
,
2830 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
2835 ret
= bdrv_inactivate_all();
2839 restart_block
= true;
2842 * Cause any non-postcopiable, but iterative devices to
2843 * send out their final data.
2845 qemu_savevm_state_complete_precopy(ms
->to_dst_file
, true, false);
2848 * in Finish migrate and with the io-lock held everything should
2849 * be quiet, but we've potentially still got dirty pages and we
2850 * need to tell the destination to throw any pages it's already received
2853 if (migrate_postcopy_ram()) {
2854 if (ram_postcopy_send_discard_bitmap(ms
)) {
2855 error_report("postcopy send discard bitmap failed");
2861 * send rest of state - note things that are doing postcopy
2862 * will notice we're in POSTCOPY_ACTIVE and not actually
2863 * wrap their state up here
2865 /* 0 max-postcopy-bandwidth means unlimited */
2867 qemu_file_set_rate_limit(ms
->to_dst_file
, INT64_MAX
);
2869 qemu_file_set_rate_limit(ms
->to_dst_file
, bandwidth
/ XFER_LIMIT_RATIO
);
2871 if (migrate_postcopy_ram()) {
2872 /* Ping just for debugging, helps line traces up */
2873 qemu_savevm_send_ping(ms
->to_dst_file
, 2);
2877 * While loading the device state we may trigger page transfer
2878 * requests and the fd must be free to process those, and thus
2879 * the destination must read the whole device state off the fd before
2880 * it starts processing it. Unfortunately the ad-hoc migration format
2881 * doesn't allow the destination to know the size to read without fully
2882 * parsing it through each devices load-state code (especially the open
2883 * coded devices that use get/put).
2884 * So we wrap the device state up in a package with a length at the start;
2885 * to do this we use a qemu_buf to hold the whole of the device state.
2887 bioc
= qio_channel_buffer_new(4096);
2888 qio_channel_set_name(QIO_CHANNEL(bioc
), "migration-postcopy-buffer");
2889 fb
= qemu_fopen_channel_output(QIO_CHANNEL(bioc
));
2890 object_unref(OBJECT(bioc
));
2893 * Make sure the receiver can get incoming pages before we send the rest
2896 qemu_savevm_send_postcopy_listen(fb
);
2898 qemu_savevm_state_complete_precopy(fb
, false, false);
2899 if (migrate_postcopy_ram()) {
2900 qemu_savevm_send_ping(fb
, 3);
2903 qemu_savevm_send_postcopy_run(fb
);
2905 /* <><> end of stuff going into the package */
2907 /* Last point of recovery; as soon as we send the package the destination
2908 * can open devices and potentially start running.
2909 * Lets just check again we've not got any errors.
2911 ret
= qemu_file_get_error(ms
->to_dst_file
);
2913 error_report("postcopy_start: Migration stream errored (pre package)");
2917 restart_block
= false;
2919 /* Now send that blob */
2920 if (qemu_savevm_send_packaged(ms
->to_dst_file
, bioc
->data
, bioc
->usage
)) {
2925 /* Send a notify to give a chance for anything that needs to happen
2926 * at the transition to postcopy and after the device state; in particular
2927 * spice needs to trigger a transition now
2929 ms
->postcopy_after_devices
= true;
2930 notifier_list_notify(&migration_state_notifiers
, ms
);
2932 ms
->downtime
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) - time_at_stop
;
2934 qemu_mutex_unlock_iothread();
2936 if (migrate_postcopy_ram()) {
2938 * Although this ping is just for debug, it could potentially be
2939 * used for getting a better measurement of downtime at the source.
2941 qemu_savevm_send_ping(ms
->to_dst_file
, 4);
2944 if (migrate_release_ram()) {
2945 ram_postcopy_migrated_memory_release(ms
);
2948 ret
= qemu_file_get_error(ms
->to_dst_file
);
2950 error_report("postcopy_start: Migration stream errored");
2951 migrate_set_state(&ms
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
2952 MIGRATION_STATUS_FAILED
);
2960 migrate_set_state(&ms
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
2961 MIGRATION_STATUS_FAILED
);
2962 if (restart_block
) {
2963 /* A failure happened early enough that we know the destination hasn't
2964 * accessed block devices, so we're safe to recover.
2966 Error
*local_err
= NULL
;
2968 bdrv_invalidate_cache_all(&local_err
);
2970 error_report_err(local_err
);
2973 qemu_mutex_unlock_iothread();
2978 * migration_maybe_pause: Pause if required to by
2979 * migrate_pause_before_switchover called with the iothread locked
2980 * Returns: 0 on success
2982 static int migration_maybe_pause(MigrationState
*s
,
2983 int *current_active_state
,
2986 if (!migrate_pause_before_switchover()) {
2990 /* Since leaving this state is not atomic with posting the semaphore
2991 * it's possible that someone could have issued multiple migrate_continue
2992 * and the semaphore is incorrectly positive at this point;
2993 * the docs say it's undefined to reinit a semaphore that's already
2994 * init'd, so use timedwait to eat up any existing posts.
2996 while (qemu_sem_timedwait(&s
->pause_sem
, 1) == 0) {
2997 /* This block intentionally left blank */
3001 * If the migration is cancelled when it is in the completion phase,
3002 * the migration state is set to MIGRATION_STATUS_CANCELLING.
3003 * So we don't need to wait a semaphore, otherwise we would always
3004 * wait for the 'pause_sem' semaphore.
3006 if (s
->state
!= MIGRATION_STATUS_CANCELLING
) {
3007 qemu_mutex_unlock_iothread();
3008 migrate_set_state(&s
->state
, *current_active_state
,
3009 MIGRATION_STATUS_PRE_SWITCHOVER
);
3010 qemu_sem_wait(&s
->pause_sem
);
3011 migrate_set_state(&s
->state
, MIGRATION_STATUS_PRE_SWITCHOVER
,
3013 *current_active_state
= new_state
;
3014 qemu_mutex_lock_iothread();
3017 return s
->state
== new_state
? 0 : -EINVAL
;
3021 * migration_completion: Used by migration_thread when there's not much left.
3022 * The caller 'breaks' the loop when this returns.
3024 * @s: Current migration state
3026 static void migration_completion(MigrationState
*s
)
3029 int current_active_state
= s
->state
;
3031 if (s
->state
== MIGRATION_STATUS_ACTIVE
) {
3032 qemu_mutex_lock_iothread();
3033 s
->downtime_start
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
3034 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, NULL
);
3035 s
->vm_was_running
= runstate_is_running();
3036 ret
= global_state_store();
3039 bool inactivate
= !migrate_colo_enabled();
3040 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
3042 ret
= migration_maybe_pause(s
, ¤t_active_state
,
3043 MIGRATION_STATUS_DEVICE
);
3046 qemu_file_set_rate_limit(s
->to_dst_file
, INT64_MAX
);
3047 ret
= qemu_savevm_state_complete_precopy(s
->to_dst_file
, false,
3050 if (inactivate
&& ret
>= 0) {
3051 s
->block_inactive
= true;
3054 qemu_mutex_unlock_iothread();
3059 } else if (s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
3060 trace_migration_completion_postcopy_end();
3062 qemu_savevm_state_complete_postcopy(s
->to_dst_file
);
3063 trace_migration_completion_postcopy_end_after_complete();
3064 } else if (s
->state
== MIGRATION_STATUS_CANCELLING
) {
3069 * If rp was opened we must clean up the thread before
3070 * cleaning everything else up (since if there are no failures
3071 * it will wait for the destination to send it's status in
3074 if (s
->rp_state
.from_dst_file
) {
3076 trace_migration_return_path_end_before();
3077 rp_error
= await_return_path_close_on_source(s
);
3078 trace_migration_return_path_end_after(rp_error
);
3080 goto fail_invalidate
;
3084 if (qemu_file_get_error(s
->to_dst_file
)) {
3085 trace_migration_completion_file_err();
3086 goto fail_invalidate
;
3089 if (!migrate_colo_enabled()) {
3090 migrate_set_state(&s
->state
, current_active_state
,
3091 MIGRATION_STATUS_COMPLETED
);
3097 /* If not doing postcopy, vm_start() will be called: let's regain
3098 * control on images.
3100 if (s
->state
== MIGRATION_STATUS_ACTIVE
||
3101 s
->state
== MIGRATION_STATUS_DEVICE
) {
3102 Error
*local_err
= NULL
;
3104 qemu_mutex_lock_iothread();
3105 bdrv_invalidate_cache_all(&local_err
);
3107 error_report_err(local_err
);
3109 s
->block_inactive
= false;
3111 qemu_mutex_unlock_iothread();
3115 migrate_set_state(&s
->state
, current_active_state
,
3116 MIGRATION_STATUS_FAILED
);
3119 bool migrate_colo_enabled(void)
3121 MigrationState
*s
= migrate_get_current();
3122 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_X_COLO
];
3125 typedef enum MigThrError
{
3126 /* No error detected */
3127 MIG_THR_ERR_NONE
= 0,
3128 /* Detected error, but resumed successfully */
3129 MIG_THR_ERR_RECOVERED
= 1,
3130 /* Detected fatal error, need to exit */
3131 MIG_THR_ERR_FATAL
= 2,
3134 static int postcopy_resume_handshake(MigrationState
*s
)
3136 qemu_savevm_send_postcopy_resume(s
->to_dst_file
);
3138 while (s
->state
== MIGRATION_STATUS_POSTCOPY_RECOVER
) {
3139 qemu_sem_wait(&s
->rp_state
.rp_sem
);
3142 if (s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
3149 /* Return zero if success, or <0 for error */
3150 static int postcopy_do_resume(MigrationState
*s
)
3155 * Call all the resume_prepare() hooks, so that modules can be
3156 * ready for the migration resume.
3158 ret
= qemu_savevm_state_resume_prepare(s
);
3160 error_report("%s: resume_prepare() failure detected: %d",
3166 * Last handshake with destination on the resume (destination will
3167 * switch to postcopy-active afterwards)
3169 ret
= postcopy_resume_handshake(s
);
3171 error_report("%s: handshake failed: %d", __func__
, ret
);
3179 * We don't return until we are in a safe state to continue current
3180 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
3181 * MIG_THR_ERR_FATAL if unrecovery failure happened.
3183 static MigThrError
postcopy_pause(MigrationState
*s
)
3185 assert(s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
);
3190 /* Current channel is possibly broken. Release it. */
3191 assert(s
->to_dst_file
);
3192 qemu_mutex_lock(&s
->qemu_file_lock
);
3193 file
= s
->to_dst_file
;
3194 s
->to_dst_file
= NULL
;
3195 qemu_mutex_unlock(&s
->qemu_file_lock
);
3197 qemu_file_shutdown(file
);
3200 migrate_set_state(&s
->state
, s
->state
,
3201 MIGRATION_STATUS_POSTCOPY_PAUSED
);
3203 error_report("Detected IO failure for postcopy. "
3204 "Migration paused.");
3207 * We wait until things fixed up. Then someone will setup the
3208 * status back for us.
3210 while (s
->state
== MIGRATION_STATUS_POSTCOPY_PAUSED
) {
3211 qemu_sem_wait(&s
->postcopy_pause_sem
);
3214 if (s
->state
== MIGRATION_STATUS_POSTCOPY_RECOVER
) {
3215 /* Woken up by a recover procedure. Give it a shot */
3218 * Firstly, let's wake up the return path now, with a new
3219 * return path channel.
3221 qemu_sem_post(&s
->postcopy_pause_rp_sem
);
3223 /* Do the resume logic */
3224 if (postcopy_do_resume(s
) == 0) {
3225 /* Let's continue! */
3226 trace_postcopy_pause_continued();
3227 return MIG_THR_ERR_RECOVERED
;
3230 * Something wrong happened during the recovery, let's
3231 * pause again. Pause is always better than throwing
3237 /* This is not right... Time to quit. */
3238 return MIG_THR_ERR_FATAL
;
3243 static MigThrError
migration_detect_error(MigrationState
*s
)
3246 int state
= s
->state
;
3247 Error
*local_error
= NULL
;
3249 if (state
== MIGRATION_STATUS_CANCELLING
||
3250 state
== MIGRATION_STATUS_CANCELLED
) {
3251 /* End the migration, but don't set the state to failed */
3252 return MIG_THR_ERR_FATAL
;
3255 /* Try to detect any file errors */
3256 ret
= qemu_file_get_error_obj(s
->to_dst_file
, &local_error
);
3258 /* Everything is fine */
3259 assert(!local_error
);
3260 return MIG_THR_ERR_NONE
;
3264 migrate_set_error(s
, local_error
);
3265 error_free(local_error
);
3268 if (state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
&& ret
== -EIO
) {
3270 * For postcopy, we allow the network to be down for a
3271 * while. After that, it can be continued by a
3274 return postcopy_pause(s
);
3277 * For precopy (or postcopy with error outside IO), we fail
3280 migrate_set_state(&s
->state
, state
, MIGRATION_STATUS_FAILED
);
3281 trace_migration_thread_file_err();
3283 /* Time to stop the migration, now. */
3284 return MIG_THR_ERR_FATAL
;
3288 /* How many bytes have we transferred since the beginning of the migration */
3289 static uint64_t migration_total_bytes(MigrationState
*s
)
3291 return qemu_ftell(s
->to_dst_file
) + ram_counters
.multifd_bytes
;
3294 static void migration_calculate_complete(MigrationState
*s
)
3296 uint64_t bytes
= migration_total_bytes(s
);
3297 int64_t end_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
3298 int64_t transfer_time
;
3300 s
->total_time
= end_time
- s
->start_time
;
3303 * It's still not set, so we are precopy migration. For
3304 * postcopy, downtime is calculated during postcopy_start().
3306 s
->downtime
= end_time
- s
->downtime_start
;
3309 transfer_time
= s
->total_time
- s
->setup_time
;
3310 if (transfer_time
) {
3311 s
->mbps
= ((double) bytes
* 8.0) / transfer_time
/ 1000;
3315 static void update_iteration_initial_status(MigrationState
*s
)
3318 * Update these three fields at the same time to avoid mismatch info lead
3319 * wrong speed calculation.
3321 s
->iteration_start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
3322 s
->iteration_initial_bytes
= migration_total_bytes(s
);
3323 s
->iteration_initial_pages
= ram_get_total_transferred_pages();
3326 static void migration_update_counters(MigrationState
*s
,
3327 int64_t current_time
)
3329 uint64_t transferred
, transferred_pages
, time_spent
;
3330 uint64_t current_bytes
; /* bytes transferred since the beginning */
3333 if (current_time
< s
->iteration_start_time
+ BUFFER_DELAY
) {
3337 current_bytes
= migration_total_bytes(s
);
3338 transferred
= current_bytes
- s
->iteration_initial_bytes
;
3339 time_spent
= current_time
- s
->iteration_start_time
;
3340 bandwidth
= (double)transferred
/ time_spent
;
3341 s
->threshold_size
= bandwidth
* s
->parameters
.downtime_limit
;
3343 s
->mbps
= (((double) transferred
* 8.0) /
3344 ((double) time_spent
/ 1000.0)) / 1000.0 / 1000.0;
3346 transferred_pages
= ram_get_total_transferred_pages() -
3347 s
->iteration_initial_pages
;
3348 s
->pages_per_second
= (double) transferred_pages
/
3349 (((double) time_spent
/ 1000.0));
3352 * if we haven't sent anything, we don't want to
3353 * recalculate. 10000 is a small enough number for our purposes
3355 if (ram_counters
.dirty_pages_rate
&& transferred
> 10000) {
3356 s
->expected_downtime
= ram_counters
.remaining
/ bandwidth
;
3359 qemu_file_reset_rate_limit(s
->to_dst_file
);
3361 update_iteration_initial_status(s
);
3363 trace_migrate_transferred(transferred
, time_spent
,
3364 bandwidth
, s
->threshold_size
);
3367 /* Migration thread iteration status */
3369 MIG_ITERATE_RESUME
, /* Resume current iteration */
3370 MIG_ITERATE_SKIP
, /* Skip current iteration */
3371 MIG_ITERATE_BREAK
, /* Break the loop */
3375 * Return true if continue to the next iteration directly, false
3378 static MigIterateState
migration_iteration_run(MigrationState
*s
)
3380 uint64_t pending_size
, pend_pre
, pend_compat
, pend_post
;
3381 bool in_postcopy
= s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
;
3383 qemu_savevm_state_pending(s
->to_dst_file
, s
->threshold_size
, &pend_pre
,
3384 &pend_compat
, &pend_post
);
3385 pending_size
= pend_pre
+ pend_compat
+ pend_post
;
3387 trace_migrate_pending(pending_size
, s
->threshold_size
,
3388 pend_pre
, pend_compat
, pend_post
);
3390 if (pending_size
&& pending_size
>= s
->threshold_size
) {
3391 /* Still a significant amount to transfer */
3392 if (!in_postcopy
&& pend_pre
<= s
->threshold_size
&&
3393 qatomic_read(&s
->start_postcopy
)) {
3394 if (postcopy_start(s
)) {
3395 error_report("%s: postcopy failed to start", __func__
);
3397 return MIG_ITERATE_SKIP
;
3399 /* Just another iteration step */
3400 qemu_savevm_state_iterate(s
->to_dst_file
, in_postcopy
);
3402 trace_migration_thread_low_pending(pending_size
);
3403 migration_completion(s
);
3404 return MIG_ITERATE_BREAK
;
3407 return MIG_ITERATE_RESUME
;
3410 static void migration_iteration_finish(MigrationState
*s
)
3412 /* If we enabled cpu throttling for auto-converge, turn it off. */
3413 cpu_throttle_stop();
3415 qemu_mutex_lock_iothread();
3417 case MIGRATION_STATUS_COMPLETED
:
3418 migration_calculate_complete(s
);
3419 runstate_set(RUN_STATE_POSTMIGRATE
);
3422 case MIGRATION_STATUS_ACTIVE
:
3424 * We should really assert here, but since it's during
3425 * migration, let's try to reduce the usage of assertions.
3427 if (!migrate_colo_enabled()) {
3428 error_report("%s: critical error: calling COLO code without "
3429 "COLO enabled", __func__
);
3431 migrate_start_colo_process(s
);
3433 * Fixme: we will run VM in COLO no matter its old running state.
3434 * After exited COLO, we will keep running.
3436 s
->vm_was_running
= true;
3438 case MIGRATION_STATUS_FAILED
:
3439 case MIGRATION_STATUS_CANCELLED
:
3440 case MIGRATION_STATUS_CANCELLING
:
3441 if (s
->vm_was_running
) {
3444 if (runstate_check(RUN_STATE_FINISH_MIGRATE
)) {
3445 runstate_set(RUN_STATE_POSTMIGRATE
);
3451 /* Should not reach here, but if so, forgive the VM. */
3452 error_report("%s: Unknown ending state %d", __func__
, s
->state
);
3455 migrate_fd_cleanup_schedule(s
);
3456 qemu_mutex_unlock_iothread();
3459 void migration_make_urgent_request(void)
3461 qemu_sem_post(&migrate_get_current()->rate_limit_sem
);
3464 void migration_consume_urgent_request(void)
3466 qemu_sem_wait(&migrate_get_current()->rate_limit_sem
);
3469 /* Returns true if the rate limiting was broken by an urgent request */
3470 bool migration_rate_limit(void)
3472 int64_t now
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
3473 MigrationState
*s
= migrate_get_current();
3475 bool urgent
= false;
3476 migration_update_counters(s
, now
);
3477 if (qemu_file_rate_limit(s
->to_dst_file
)) {
3479 if (qemu_file_get_error(s
->to_dst_file
)) {
3483 * Wait for a delay to do rate limiting OR
3484 * something urgent to post the semaphore.
3486 int ms
= s
->iteration_start_time
+ BUFFER_DELAY
- now
;
3487 trace_migration_rate_limit_pre(ms
);
3488 if (qemu_sem_timedwait(&s
->rate_limit_sem
, ms
) == 0) {
3490 * We were woken by one or more urgent things but
3491 * the timedwait will have consumed one of them.
3492 * The service routine for the urgent wake will dec
3493 * the semaphore itself for each item it consumes,
3494 * so add this one we just eat back.
3496 qemu_sem_post(&s
->rate_limit_sem
);
3499 trace_migration_rate_limit_post(urgent
);
3505 * Master migration thread on the source VM.
3506 * It drives the migration and pumps the data down the outgoing channel.
3508 static void *migration_thread(void *opaque
)
3510 MigrationState
*s
= opaque
;
3511 int64_t setup_start
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
3512 MigThrError thr_error
;
3513 bool urgent
= false;
3515 rcu_register_thread();
3517 object_ref(OBJECT(s
));
3518 update_iteration_initial_status(s
);
3520 qemu_savevm_state_header(s
->to_dst_file
);
3523 * If we opened the return path, we need to make sure dst has it
3526 if (s
->rp_state
.from_dst_file
) {
3527 /* Now tell the dest that it should open its end so it can reply */
3528 qemu_savevm_send_open_return_path(s
->to_dst_file
);
3530 /* And do a ping that will make stuff easier to debug */
3531 qemu_savevm_send_ping(s
->to_dst_file
, 1);
3534 if (migrate_postcopy()) {
3536 * Tell the destination that we *might* want to do postcopy later;
3537 * if the other end can't do postcopy it should fail now, nice and
3540 qemu_savevm_send_postcopy_advise(s
->to_dst_file
);
3543 if (migrate_colo_enabled()) {
3544 /* Notify migration destination that we enable COLO */
3545 qemu_savevm_send_colo_enable(s
->to_dst_file
);
3548 qemu_savevm_state_setup(s
->to_dst_file
);
3550 if (qemu_savevm_state_guest_unplug_pending()) {
3551 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
3552 MIGRATION_STATUS_WAIT_UNPLUG
);
3554 while (s
->state
== MIGRATION_STATUS_WAIT_UNPLUG
&&
3555 qemu_savevm_state_guest_unplug_pending()) {
3556 qemu_sem_timedwait(&s
->wait_unplug_sem
, 250);
3559 migrate_set_state(&s
->state
, MIGRATION_STATUS_WAIT_UNPLUG
,
3560 MIGRATION_STATUS_ACTIVE
);
3563 s
->setup_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
) - setup_start
;
3564 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
3565 MIGRATION_STATUS_ACTIVE
);
3567 trace_migration_thread_setup_complete();
3569 while (migration_is_active(s
)) {
3570 if (urgent
|| !qemu_file_rate_limit(s
->to_dst_file
)) {
3571 MigIterateState iter_state
= migration_iteration_run(s
);
3572 if (iter_state
== MIG_ITERATE_SKIP
) {
3574 } else if (iter_state
== MIG_ITERATE_BREAK
) {
3580 * Try to detect any kind of failures, and see whether we
3581 * should stop the migration now.
3583 thr_error
= migration_detect_error(s
);
3584 if (thr_error
== MIG_THR_ERR_FATAL
) {
3585 /* Stop migration */
3587 } else if (thr_error
== MIG_THR_ERR_RECOVERED
) {
3589 * Just recovered from a e.g. network failure, reset all
3590 * the local variables. This is important to avoid
3591 * breaking transferred_bytes and bandwidth calculation
3593 update_iteration_initial_status(s
);
3596 urgent
= migration_rate_limit();
3599 trace_migration_thread_after_loop();
3600 migration_iteration_finish(s
);
3601 object_unref(OBJECT(s
));
3602 rcu_unregister_thread();
3606 void migrate_fd_connect(MigrationState
*s
, Error
*error_in
)
3608 Error
*local_err
= NULL
;
3610 bool resume
= s
->state
== MIGRATION_STATUS_POSTCOPY_PAUSED
;
3612 s
->expected_downtime
= s
->parameters
.downtime_limit
;
3614 assert(s
->cleanup_bh
);
3616 assert(!s
->cleanup_bh
);
3617 s
->cleanup_bh
= qemu_bh_new(migrate_fd_cleanup_bh
, s
);
3620 migrate_fd_error(s
, error_in
);
3621 migrate_fd_cleanup(s
);
3626 /* This is a resumed migration */
3627 rate_limit
= s
->parameters
.max_postcopy_bandwidth
/
3630 /* This is a fresh new migration */
3631 rate_limit
= s
->parameters
.max_bandwidth
/ XFER_LIMIT_RATIO
;
3633 /* Notify before starting migration thread */
3634 notifier_list_notify(&migration_state_notifiers
, s
);
3637 qemu_file_set_rate_limit(s
->to_dst_file
, rate_limit
);
3638 qemu_file_set_blocking(s
->to_dst_file
, true);
3641 * Open the return path. For postcopy, it is used exclusively. For
3642 * precopy, only if user specified "return-path" capability would
3643 * QEMU uses the return path.
3645 if (migrate_postcopy_ram() || migrate_use_return_path()) {
3646 if (open_return_path_on_source(s
, !resume
)) {
3647 error_report("Unable to open return-path for postcopy");
3648 migrate_set_state(&s
->state
, s
->state
, MIGRATION_STATUS_FAILED
);
3649 migrate_fd_cleanup(s
);
3655 /* Wakeup the main migration thread to do the recovery */
3656 migrate_set_state(&s
->state
, MIGRATION_STATUS_POSTCOPY_PAUSED
,
3657 MIGRATION_STATUS_POSTCOPY_RECOVER
);
3658 qemu_sem_post(&s
->postcopy_pause_sem
);
3662 if (multifd_save_setup(&local_err
) != 0) {
3663 error_report_err(local_err
);
3664 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
3665 MIGRATION_STATUS_FAILED
);
3666 migrate_fd_cleanup(s
);
3669 qemu_thread_create(&s
->thread
, "live_migration", migration_thread
, s
,
3670 QEMU_THREAD_JOINABLE
);
3671 s
->migration_thread_running
= true;
3674 void migration_global_dump(Monitor
*mon
)
3676 MigrationState
*ms
= migrate_get_current();
3678 monitor_printf(mon
, "globals:\n");
3679 monitor_printf(mon
, "store-global-state: %s\n",
3680 ms
->store_global_state
? "on" : "off");
3681 monitor_printf(mon
, "only-migratable: %s\n",
3682 only_migratable
? "on" : "off");
3683 monitor_printf(mon
, "send-configuration: %s\n",
3684 ms
->send_configuration
? "on" : "off");
3685 monitor_printf(mon
, "send-section-footer: %s\n",
3686 ms
->send_section_footer
? "on" : "off");
3687 monitor_printf(mon
, "decompress-error-check: %s\n",
3688 ms
->decompress_error_check
? "on" : "off");
3689 monitor_printf(mon
, "clear-bitmap-shift: %u\n",
3690 ms
->clear_bitmap_shift
);
3693 #define DEFINE_PROP_MIG_CAP(name, x) \
3694 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
3696 static Property migration_properties
[] = {
3697 DEFINE_PROP_BOOL("store-global-state", MigrationState
,
3698 store_global_state
, true),
3699 DEFINE_PROP_BOOL("send-configuration", MigrationState
,
3700 send_configuration
, true),
3701 DEFINE_PROP_BOOL("send-section-footer", MigrationState
,
3702 send_section_footer
, true),
3703 DEFINE_PROP_BOOL("decompress-error-check", MigrationState
,
3704 decompress_error_check
, true),
3705 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState
,
3706 clear_bitmap_shift
, CLEAR_BITMAP_SHIFT_DEFAULT
),
3708 /* Migration parameters */
3709 DEFINE_PROP_UINT8("x-compress-level", MigrationState
,
3710 parameters
.compress_level
,
3711 DEFAULT_MIGRATE_COMPRESS_LEVEL
),
3712 DEFINE_PROP_UINT8("x-compress-threads", MigrationState
,
3713 parameters
.compress_threads
,
3714 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT
),
3715 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState
,
3716 parameters
.compress_wait_thread
, true),
3717 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState
,
3718 parameters
.decompress_threads
,
3719 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT
),
3720 DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState
,
3721 parameters
.throttle_trigger_threshold
,
3722 DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD
),
3723 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState
,
3724 parameters
.cpu_throttle_initial
,
3725 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL
),
3726 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState
,
3727 parameters
.cpu_throttle_increment
,
3728 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT
),
3729 DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState
,
3730 parameters
.cpu_throttle_tailslow
, false),
3731 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState
,
3732 parameters
.max_bandwidth
, MAX_THROTTLE
),
3733 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState
,
3734 parameters
.downtime_limit
,
3735 DEFAULT_MIGRATE_SET_DOWNTIME
),
3736 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState
,
3737 parameters
.x_checkpoint_delay
,
3738 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY
),
3739 DEFINE_PROP_UINT8("multifd-channels", MigrationState
,
3740 parameters
.multifd_channels
,
3741 DEFAULT_MIGRATE_MULTIFD_CHANNELS
),
3742 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState
,
3743 parameters
.multifd_compression
,
3744 DEFAULT_MIGRATE_MULTIFD_COMPRESSION
),
3745 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState
,
3746 parameters
.multifd_zlib_level
,
3747 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL
),
3748 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState
,
3749 parameters
.multifd_zstd_level
,
3750 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL
),
3751 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState
,
3752 parameters
.xbzrle_cache_size
,
3753 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE
),
3754 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState
,
3755 parameters
.max_postcopy_bandwidth
,
3756 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH
),
3757 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState
,
3758 parameters
.max_cpu_throttle
,
3759 DEFAULT_MIGRATE_MAX_CPU_THROTTLE
),
3760 DEFINE_PROP_SIZE("announce-initial", MigrationState
,
3761 parameters
.announce_initial
,
3762 DEFAULT_MIGRATE_ANNOUNCE_INITIAL
),
3763 DEFINE_PROP_SIZE("announce-max", MigrationState
,
3764 parameters
.announce_max
,
3765 DEFAULT_MIGRATE_ANNOUNCE_MAX
),
3766 DEFINE_PROP_SIZE("announce-rounds", MigrationState
,
3767 parameters
.announce_rounds
,
3768 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS
),
3769 DEFINE_PROP_SIZE("announce-step", MigrationState
,
3770 parameters
.announce_step
,
3771 DEFAULT_MIGRATE_ANNOUNCE_STEP
),
3773 /* Migration capabilities */
3774 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE
),
3775 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL
),
3776 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE
),
3777 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS
),
3778 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS
),
3779 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS
),
3780 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM
),
3781 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO
),
3782 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM
),
3783 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK
),
3784 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH
),
3785 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD
),
3787 DEFINE_PROP_END_OF_LIST(),
3790 static void migration_class_init(ObjectClass
*klass
, void *data
)
3792 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3794 dc
->user_creatable
= false;
3795 device_class_set_props(dc
, migration_properties
);
3798 static void migration_instance_finalize(Object
*obj
)
3800 MigrationState
*ms
= MIGRATION_OBJ(obj
);
3801 MigrationParameters
*params
= &ms
->parameters
;
3803 qemu_mutex_destroy(&ms
->error_mutex
);
3804 qemu_mutex_destroy(&ms
->qemu_file_lock
);
3805 g_free(params
->tls_hostname
);
3806 g_free(params
->tls_creds
);
3807 qemu_sem_destroy(&ms
->wait_unplug_sem
);
3808 qemu_sem_destroy(&ms
->rate_limit_sem
);
3809 qemu_sem_destroy(&ms
->pause_sem
);
3810 qemu_sem_destroy(&ms
->postcopy_pause_sem
);
3811 qemu_sem_destroy(&ms
->postcopy_pause_rp_sem
);
3812 qemu_sem_destroy(&ms
->rp_state
.rp_sem
);
3813 error_free(ms
->error
);
3816 static void migration_instance_init(Object
*obj
)
3818 MigrationState
*ms
= MIGRATION_OBJ(obj
);
3819 MigrationParameters
*params
= &ms
->parameters
;
3821 ms
->state
= MIGRATION_STATUS_NONE
;
3823 ms
->pages_per_second
= -1;
3824 qemu_sem_init(&ms
->pause_sem
, 0);
3825 qemu_mutex_init(&ms
->error_mutex
);
3827 params
->tls_hostname
= g_strdup("");
3828 params
->tls_creds
= g_strdup("");
3830 /* Set has_* up only for parameter checks */
3831 params
->has_compress_level
= true;
3832 params
->has_compress_threads
= true;
3833 params
->has_decompress_threads
= true;
3834 params
->has_throttle_trigger_threshold
= true;
3835 params
->has_cpu_throttle_initial
= true;
3836 params
->has_cpu_throttle_increment
= true;
3837 params
->has_cpu_throttle_tailslow
= true;
3838 params
->has_max_bandwidth
= true;
3839 params
->has_downtime_limit
= true;
3840 params
->has_x_checkpoint_delay
= true;
3841 params
->has_block_incremental
= true;
3842 params
->has_multifd_channels
= true;
3843 params
->has_multifd_compression
= true;
3844 params
->has_multifd_zlib_level
= true;
3845 params
->has_multifd_zstd_level
= true;
3846 params
->has_xbzrle_cache_size
= true;
3847 params
->has_max_postcopy_bandwidth
= true;
3848 params
->has_max_cpu_throttle
= true;
3849 params
->has_announce_initial
= true;
3850 params
->has_announce_max
= true;
3851 params
->has_announce_rounds
= true;
3852 params
->has_announce_step
= true;
3854 qemu_sem_init(&ms
->postcopy_pause_sem
, 0);
3855 qemu_sem_init(&ms
->postcopy_pause_rp_sem
, 0);
3856 qemu_sem_init(&ms
->rp_state
.rp_sem
, 0);
3857 qemu_sem_init(&ms
->rate_limit_sem
, 0);
3858 qemu_sem_init(&ms
->wait_unplug_sem
, 0);
3859 qemu_mutex_init(&ms
->qemu_file_lock
);
3863 * Return true if check pass, false otherwise. Error will be put
3864 * inside errp if provided.
3866 static bool migration_object_check(MigrationState
*ms
, Error
**errp
)
3868 MigrationCapabilityStatusList
*head
= NULL
;
3869 /* Assuming all off */
3870 bool cap_list
[MIGRATION_CAPABILITY__MAX
] = { 0 }, ret
;
3873 if (!migrate_params_check(&ms
->parameters
, errp
)) {
3877 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
3878 if (ms
->enabled_capabilities
[i
]) {
3879 head
= migrate_cap_add(head
, i
, true);
3883 ret
= migrate_caps_check(cap_list
, head
, errp
);
3885 /* It works with head == NULL */
3886 qapi_free_MigrationCapabilityStatusList(head
);
3891 static const TypeInfo migration_type
= {
3892 .name
= TYPE_MIGRATION
,
3894 * NOTE: TYPE_MIGRATION is not really a device, as the object is
3895 * not created using qdev_new(), it is not attached to the qdev
3896 * device tree, and it is never realized.
3898 * TODO: Make this TYPE_OBJECT once QOM provides something like
3899 * TYPE_DEVICE's "-global" properties.
3901 .parent
= TYPE_DEVICE
,
3902 .class_init
= migration_class_init
,
3903 .class_size
= sizeof(MigrationClass
),
3904 .instance_size
= sizeof(MigrationState
),
3905 .instance_init
= migration_instance_init
,
3906 .instance_finalize
= migration_instance_finalize
,
3909 static void register_migration_types(void)
3911 type_register_static(&migration_type
);
3914 type_init(register_migration_types
);