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"
60 #define MAX_THROTTLE (128 << 20) /* Migration transfer speed throttling */
62 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
64 #define BUFFER_DELAY 100
65 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
67 /* Time in milliseconds we are allowed to stop the source,
68 * for sending the last part */
69 #define DEFAULT_MIGRATE_SET_DOWNTIME 300
71 /* Maximum migrate downtime set to 2000 seconds */
72 #define MAX_MIGRATE_DOWNTIME_SECONDS 2000
73 #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
75 /* Default compression thread count */
76 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
77 /* Default decompression thread count, usually decompression is at
78 * least 4 times as fast as compression.*/
79 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
80 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
81 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
82 /* Define default autoconverge cpu throttle migration parameters */
83 #define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50
84 #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
85 #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
86 #define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
88 /* Migration XBZRLE default cache size */
89 #define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
91 /* The delay time (in ms) between two COLO checkpoints */
92 #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
93 #define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
94 #define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
95 /* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
96 #define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
97 /* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
98 #define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
100 /* Background transfer rate for postcopy, 0 means unlimited, note
101 * that page requests can still exceed this limit.
103 #define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
106 * Parameters for self_announce_delay giving a stream of RARP/ARP
107 * packets after migration.
109 #define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50
110 #define DEFAULT_MIGRATE_ANNOUNCE_MAX 550
111 #define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5
112 #define DEFAULT_MIGRATE_ANNOUNCE_STEP 100
114 static NotifierList migration_state_notifiers
=
115 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers
);
117 static bool deferred_incoming
;
119 /* Messages sent on the return path from destination to source */
120 enum mig_rp_message_type
{
121 MIG_RP_MSG_INVALID
= 0, /* Must be 0 */
122 MIG_RP_MSG_SHUT
, /* sibling will not send any more RP messages */
123 MIG_RP_MSG_PONG
, /* Response to a PING; data (seq: be32 ) */
125 MIG_RP_MSG_REQ_PAGES_ID
, /* data (start: be64, len: be32, id: string) */
126 MIG_RP_MSG_REQ_PAGES
, /* data (start: be64, len: be32) */
127 MIG_RP_MSG_RECV_BITMAP
, /* send recved_bitmap back to source */
128 MIG_RP_MSG_RESUME_ACK
, /* tell source that we are ready to resume */
133 /* When we add fault tolerance, we could have several
134 migrations at once. For now we don't need to add
135 dynamic creation of migration */
137 static MigrationState
*current_migration
;
138 static MigrationIncomingState
*current_incoming
;
140 static bool migration_object_check(MigrationState
*ms
, Error
**errp
);
141 static int migration_maybe_pause(MigrationState
*s
,
142 int *current_active_state
,
144 static void migrate_fd_cancel(MigrationState
*s
);
146 static gint
page_request_addr_cmp(gconstpointer ap
, gconstpointer bp
)
148 uintptr_t a
= (uintptr_t) ap
, b
= (uintptr_t) bp
;
150 return (a
> b
) - (a
< b
);
153 void migration_object_init(void)
157 /* This can only be called once. */
158 assert(!current_migration
);
159 current_migration
= MIGRATION_OBJ(object_new(TYPE_MIGRATION
));
162 * Init the migrate incoming object as well no matter whether
163 * we'll use it or not.
165 assert(!current_incoming
);
166 current_incoming
= g_new0(MigrationIncomingState
, 1);
167 current_incoming
->state
= MIGRATION_STATUS_NONE
;
168 current_incoming
->postcopy_remote_fds
=
169 g_array_new(FALSE
, TRUE
, sizeof(struct PostCopyFD
));
170 qemu_mutex_init(¤t_incoming
->rp_mutex
);
171 qemu_event_init(¤t_incoming
->main_thread_load_event
, false);
172 qemu_sem_init(¤t_incoming
->postcopy_pause_sem_dst
, 0);
173 qemu_sem_init(¤t_incoming
->postcopy_pause_sem_fault
, 0);
174 qemu_mutex_init(¤t_incoming
->page_request_mutex
);
175 current_incoming
->page_requested
= g_tree_new(page_request_addr_cmp
);
177 if (!migration_object_check(current_migration
, &err
)) {
178 error_report_err(err
);
183 void migration_shutdown(void)
186 * Cancel the current migration - that will (eventually)
187 * stop the migration using this structure
189 migrate_fd_cancel(current_migration
);
190 object_unref(OBJECT(current_migration
));
193 * Cancel outgoing migration of dirty bitmaps. It should
194 * at least unref used block nodes.
196 dirty_bitmap_mig_cancel_outgoing();
199 * Cancel incoming migration of dirty bitmaps. Dirty bitmaps
200 * are non-critical data, and their loss never considered as
203 dirty_bitmap_mig_cancel_incoming();
207 MigrationState
*migrate_get_current(void)
209 /* This can only be called after the object created. */
210 assert(current_migration
);
211 return current_migration
;
214 MigrationIncomingState
*migration_incoming_get_current(void)
216 assert(current_incoming
);
217 return current_incoming
;
220 void migration_incoming_state_destroy(void)
222 struct MigrationIncomingState
*mis
= migration_incoming_get_current();
224 if (mis
->to_src_file
) {
225 /* Tell source that we are done */
226 migrate_send_rp_shut(mis
, qemu_file_get_error(mis
->from_src_file
) != 0);
227 qemu_fclose(mis
->to_src_file
);
228 mis
->to_src_file
= NULL
;
231 if (mis
->from_src_file
) {
232 qemu_fclose(mis
->from_src_file
);
233 mis
->from_src_file
= NULL
;
235 if (mis
->postcopy_remote_fds
) {
236 g_array_free(mis
->postcopy_remote_fds
, TRUE
);
237 mis
->postcopy_remote_fds
= NULL
;
240 qemu_event_reset(&mis
->main_thread_load_event
);
242 if (mis
->page_requested
) {
243 g_tree_destroy(mis
->page_requested
);
244 mis
->page_requested
= NULL
;
247 if (mis
->socket_address_list
) {
248 qapi_free_SocketAddressList(mis
->socket_address_list
);
249 mis
->socket_address_list
= NULL
;
253 static void migrate_generate_event(int new_state
)
255 if (migrate_use_events()) {
256 qapi_event_send_migration(new_state
);
260 static bool migrate_late_block_activate(void)
264 s
= migrate_get_current();
266 return s
->enabled_capabilities
[
267 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE
];
271 * Called on -incoming with a defer: uri.
272 * The migration can be started later after any parameters have been
275 static void deferred_incoming_migration(Error
**errp
)
277 if (deferred_incoming
) {
278 error_setg(errp
, "Incoming migration already deferred");
280 deferred_incoming
= true;
284 * Send a message on the return channel back to the source
287 static int migrate_send_rp_message(MigrationIncomingState
*mis
,
288 enum mig_rp_message_type message_type
,
289 uint16_t len
, void *data
)
293 trace_migrate_send_rp_message((int)message_type
, len
);
294 qemu_mutex_lock(&mis
->rp_mutex
);
297 * It's possible that the file handle got lost due to network
300 if (!mis
->to_src_file
) {
305 qemu_put_be16(mis
->to_src_file
, (unsigned int)message_type
);
306 qemu_put_be16(mis
->to_src_file
, len
);
307 qemu_put_buffer(mis
->to_src_file
, data
, len
);
308 qemu_fflush(mis
->to_src_file
);
310 /* It's possible that qemu file got error during sending */
311 ret
= qemu_file_get_error(mis
->to_src_file
);
314 qemu_mutex_unlock(&mis
->rp_mutex
);
318 /* Request one page from the source VM at the given start address.
319 * rb: the RAMBlock to request the page in
320 * Start: Address offset within the RB
321 * Len: Length in bytes required - must be a multiple of pagesize
323 int migrate_send_rp_message_req_pages(MigrationIncomingState
*mis
,
324 RAMBlock
*rb
, ram_addr_t start
)
326 uint8_t bufc
[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
327 size_t msglen
= 12; /* start + len */
328 size_t len
= qemu_ram_pagesize(rb
);
329 enum mig_rp_message_type msg_type
;
333 *(uint64_t *)bufc
= cpu_to_be64((uint64_t)start
);
334 *(uint32_t *)(bufc
+ 8) = cpu_to_be32((uint32_t)len
);
337 * We maintain the last ramblock that we requested for page. Note that we
338 * don't need locking because this function will only be called within the
339 * postcopy ram fault thread.
341 if (rb
!= mis
->last_rb
) {
344 rbname
= qemu_ram_get_idstr(rb
);
345 rbname_len
= strlen(rbname
);
347 assert(rbname_len
< 256);
349 bufc
[msglen
++] = rbname_len
;
350 memcpy(bufc
+ msglen
, rbname
, rbname_len
);
351 msglen
+= rbname_len
;
352 msg_type
= MIG_RP_MSG_REQ_PAGES_ID
;
354 msg_type
= MIG_RP_MSG_REQ_PAGES
;
357 return migrate_send_rp_message(mis
, msg_type
, msglen
, bufc
);
360 int migrate_send_rp_req_pages(MigrationIncomingState
*mis
,
361 RAMBlock
*rb
, ram_addr_t start
, uint64_t haddr
)
363 void *aligned
= (void *)(uintptr_t)(haddr
& (-qemu_ram_pagesize(rb
)));
366 WITH_QEMU_LOCK_GUARD(&mis
->page_request_mutex
) {
367 received
= ramblock_recv_bitmap_test_byte_offset(rb
, start
);
368 if (!received
&& !g_tree_lookup(mis
->page_requested
, aligned
)) {
370 * The page has not been received, and it's not yet in the page
371 * request list. Queue it. Set the value of element to 1, so that
372 * things like g_tree_lookup() will return TRUE (1) when found.
374 g_tree_insert(mis
->page_requested
, aligned
, (gpointer
)1);
375 mis
->page_requested_count
++;
376 trace_postcopy_page_req_add(aligned
, mis
->page_requested_count
);
381 * If the page is there, skip sending the message. We don't even need the
382 * lock because as long as the page arrived, it'll be there forever.
388 return migrate_send_rp_message_req_pages(mis
, rb
, start
);
391 static bool migration_colo_enabled
;
392 bool migration_incoming_colo_enabled(void)
394 return migration_colo_enabled
;
397 void migration_incoming_disable_colo(void)
399 ram_block_discard_disable(false);
400 migration_colo_enabled
= false;
403 int migration_incoming_enable_colo(void)
405 if (ram_block_discard_disable(true)) {
406 error_report("COLO: cannot disable RAM discard");
409 migration_colo_enabled
= true;
413 void migrate_add_address(SocketAddress
*address
)
415 MigrationIncomingState
*mis
= migration_incoming_get_current();
416 SocketAddressList
*addrs
;
418 addrs
= g_new0(SocketAddressList
, 1);
419 addrs
->next
= mis
->socket_address_list
;
420 mis
->socket_address_list
= addrs
;
421 addrs
->value
= QAPI_CLONE(SocketAddress
, address
);
424 void qemu_start_incoming_migration(const char *uri
, Error
**errp
)
426 const char *p
= NULL
;
428 qapi_event_send_migration(MIGRATION_STATUS_SETUP
);
429 if (!strcmp(uri
, "defer")) {
430 deferred_incoming_migration(errp
);
431 } else if (strstart(uri
, "tcp:", &p
) ||
432 strstart(uri
, "unix:", NULL
) ||
433 strstart(uri
, "vsock:", NULL
)) {
434 socket_start_incoming_migration(p
? p
: uri
, errp
);
436 } else if (strstart(uri
, "rdma:", &p
)) {
437 rdma_start_incoming_migration(p
, errp
);
439 } else if (strstart(uri
, "exec:", &p
)) {
440 exec_start_incoming_migration(p
, errp
);
441 } else if (strstart(uri
, "fd:", &p
)) {
442 fd_start_incoming_migration(p
, errp
);
444 error_setg(errp
, "unknown migration protocol: %s", uri
);
448 static void process_incoming_migration_bh(void *opaque
)
450 Error
*local_err
= NULL
;
451 MigrationIncomingState
*mis
= opaque
;
453 /* If capability late_block_activate is set:
454 * Only fire up the block code now if we're going to restart the
455 * VM, else 'cont' will do it.
456 * This causes file locking to happen; so we don't want it to happen
457 * unless we really are starting the VM.
459 if (!migrate_late_block_activate() ||
460 (autostart
&& (!global_state_received() ||
461 global_state_get_runstate() == RUN_STATE_RUNNING
))) {
462 /* Make sure all file formats flush their mutable metadata.
463 * If we get an error here, just don't restart the VM yet. */
464 bdrv_invalidate_cache_all(&local_err
);
466 error_report_err(local_err
);
473 * This must happen after all error conditions are dealt with and
474 * we're sure the VM is going to be running on this host.
476 qemu_announce_self(&mis
->announce_timer
, migrate_announce_params());
478 if (multifd_load_cleanup(&local_err
) != 0) {
479 error_report_err(local_err
);
482 /* If global state section was not received or we are in running
483 state, we need to obey autostart. Any other state is set with
486 dirty_bitmap_mig_before_vm_start();
488 if (!global_state_received() ||
489 global_state_get_runstate() == RUN_STATE_RUNNING
) {
493 runstate_set(RUN_STATE_PAUSED
);
495 } else if (migration_incoming_colo_enabled()) {
496 migration_incoming_disable_colo();
499 runstate_set(global_state_get_runstate());
502 * This must happen after any state changes since as soon as an external
503 * observer sees this event they might start to prod at the VM assuming
506 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
507 MIGRATION_STATUS_COMPLETED
);
508 qemu_bh_delete(mis
->bh
);
509 migration_incoming_state_destroy();
512 static void process_incoming_migration_co(void *opaque
)
514 MigrationIncomingState
*mis
= migration_incoming_get_current();
517 Error
*local_err
= NULL
;
519 assert(mis
->from_src_file
);
520 mis
->migration_incoming_co
= qemu_coroutine_self();
521 mis
->largest_page_size
= qemu_ram_pagesize_largest();
522 postcopy_state_set(POSTCOPY_INCOMING_NONE
);
523 migrate_set_state(&mis
->state
, MIGRATION_STATUS_NONE
,
524 MIGRATION_STATUS_ACTIVE
);
525 ret
= qemu_loadvm_state(mis
->from_src_file
);
527 ps
= postcopy_state_get();
528 trace_process_incoming_migration_co_end(ret
, ps
);
529 if (ps
!= POSTCOPY_INCOMING_NONE
) {
530 if (ps
== POSTCOPY_INCOMING_ADVISE
) {
532 * Where a migration had postcopy enabled (and thus went to advise)
533 * but managed to complete within the precopy period, we can use
536 postcopy_ram_incoming_cleanup(mis
);
537 } else if (ret
>= 0) {
539 * Postcopy was started, cleanup should happen at the end of the
542 trace_process_incoming_migration_co_postcopy_end_main();
545 /* Else if something went wrong then just fall out of the normal exit */
548 /* we get COLO info, and know if we are in COLO mode */
549 if (!ret
&& migration_incoming_colo_enabled()) {
550 /* Make sure all file formats flush their mutable metadata */
551 bdrv_invalidate_cache_all(&local_err
);
553 error_report_err(local_err
);
557 qemu_thread_create(&mis
->colo_incoming_thread
, "COLO incoming",
558 colo_process_incoming_thread
, mis
, QEMU_THREAD_JOINABLE
);
559 mis
->have_colo_incoming_thread
= true;
560 qemu_coroutine_yield();
562 /* Wait checkpoint incoming thread exit before free resource */
563 qemu_thread_join(&mis
->colo_incoming_thread
);
564 /* We hold the global iothread lock, so it is safe here */
565 colo_release_ram_cache();
569 error_report("load of migration failed: %s", strerror(-ret
));
572 mis
->bh
= qemu_bh_new(process_incoming_migration_bh
, mis
);
573 qemu_bh_schedule(mis
->bh
);
574 mis
->migration_incoming_co
= NULL
;
578 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
579 MIGRATION_STATUS_FAILED
);
580 qemu_fclose(mis
->from_src_file
);
581 if (multifd_load_cleanup(&local_err
) != 0) {
582 error_report_err(local_err
);
588 * @migration_incoming_setup: Setup incoming migration
590 * Returns 0 for no error or 1 for error
592 * @f: file for main migration channel
593 * @errp: where to put errors
595 static int migration_incoming_setup(QEMUFile
*f
, Error
**errp
)
597 MigrationIncomingState
*mis
= migration_incoming_get_current();
598 Error
*local_err
= NULL
;
600 if (multifd_load_setup(&local_err
) != 0) {
601 /* We haven't been able to create multifd threads
602 nothing better to do */
603 error_report_err(local_err
);
607 if (!mis
->from_src_file
) {
608 mis
->from_src_file
= f
;
610 qemu_file_set_blocking(f
, false);
614 void migration_incoming_process(void)
616 Coroutine
*co
= qemu_coroutine_create(process_incoming_migration_co
, NULL
);
617 qemu_coroutine_enter(co
);
620 /* Returns true if recovered from a paused migration, otherwise false */
621 static bool postcopy_try_recover(QEMUFile
*f
)
623 MigrationIncomingState
*mis
= migration_incoming_get_current();
625 if (mis
->state
== MIGRATION_STATUS_POSTCOPY_PAUSED
) {
626 /* Resumed from a paused postcopy migration */
628 mis
->from_src_file
= f
;
629 /* Postcopy has standalone thread to do vm load */
630 qemu_file_set_blocking(f
, true);
632 /* Re-configure the return path */
633 mis
->to_src_file
= qemu_file_get_return_path(f
);
635 migrate_set_state(&mis
->state
, MIGRATION_STATUS_POSTCOPY_PAUSED
,
636 MIGRATION_STATUS_POSTCOPY_RECOVER
);
639 * Here, we only wake up the main loading thread (while the
640 * fault thread will still be waiting), so that we can receive
641 * commands from source now, and answer it if needed. The
642 * fault thread will be woken up afterwards until we are sure
643 * that source is ready to reply to page requests.
645 qemu_sem_post(&mis
->postcopy_pause_sem_dst
);
652 void migration_fd_process_incoming(QEMUFile
*f
, Error
**errp
)
654 Error
*local_err
= NULL
;
656 if (postcopy_try_recover(f
)) {
660 if (migration_incoming_setup(f
, &local_err
)) {
661 error_propagate(errp
, local_err
);
664 migration_incoming_process();
667 void migration_ioc_process_incoming(QIOChannel
*ioc
, Error
**errp
)
669 MigrationIncomingState
*mis
= migration_incoming_get_current();
670 Error
*local_err
= NULL
;
671 bool start_migration
;
673 if (!mis
->from_src_file
) {
674 /* The first connection (multifd may have multiple) */
675 QEMUFile
*f
= qemu_fopen_channel_input(ioc
);
677 /* If it's a recovery, we're done */
678 if (postcopy_try_recover(f
)) {
682 if (migration_incoming_setup(f
, &local_err
)) {
683 error_propagate(errp
, local_err
);
688 * Common migration only needs one channel, so we can start
689 * right now. Multifd needs more than one channel, we wait.
691 start_migration
= !migrate_use_multifd();
693 /* Multiple connections */
694 assert(migrate_use_multifd());
695 start_migration
= multifd_recv_new_channel(ioc
, &local_err
);
697 error_propagate(errp
, local_err
);
702 if (start_migration
) {
703 migration_incoming_process();
708 * @migration_has_all_channels: We have received all channels that we need
710 * Returns true when we have got connections to all the channels that
711 * we need for migration.
713 bool migration_has_all_channels(void)
715 MigrationIncomingState
*mis
= migration_incoming_get_current();
718 all_channels
= multifd_recv_all_channels_created();
720 return all_channels
&& mis
->from_src_file
!= NULL
;
724 * Send a 'SHUT' message on the return channel with the given value
725 * to indicate that we've finished with the RP. Non-0 value indicates
728 void migrate_send_rp_shut(MigrationIncomingState
*mis
,
733 buf
= cpu_to_be32(value
);
734 migrate_send_rp_message(mis
, MIG_RP_MSG_SHUT
, sizeof(buf
), &buf
);
738 * Send a 'PONG' message on the return channel with the given value
739 * (normally in response to a 'PING')
741 void migrate_send_rp_pong(MigrationIncomingState
*mis
,
746 buf
= cpu_to_be32(value
);
747 migrate_send_rp_message(mis
, MIG_RP_MSG_PONG
, sizeof(buf
), &buf
);
750 void migrate_send_rp_recv_bitmap(MigrationIncomingState
*mis
,
758 * First, we send the header part. It contains only the len of
759 * idstr, and the idstr itself.
761 len
= strlen(block_name
);
763 memcpy(buf
+ 1, block_name
, len
);
765 if (mis
->state
!= MIGRATION_STATUS_POSTCOPY_RECOVER
) {
766 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
771 migrate_send_rp_message(mis
, MIG_RP_MSG_RECV_BITMAP
, len
+ 1, buf
);
774 * Next, we dump the received bitmap to the stream.
776 * TODO: currently we are safe since we are the only one that is
777 * using the to_src_file handle (fault thread is still paused),
778 * and it's ok even not taking the mutex. However the best way is
779 * to take the lock before sending the message header, and release
780 * the lock after sending the bitmap.
782 qemu_mutex_lock(&mis
->rp_mutex
);
783 res
= ramblock_recv_bitmap_send(mis
->to_src_file
, block_name
);
784 qemu_mutex_unlock(&mis
->rp_mutex
);
786 trace_migrate_send_rp_recv_bitmap(block_name
, res
);
789 void migrate_send_rp_resume_ack(MigrationIncomingState
*mis
, uint32_t value
)
793 buf
= cpu_to_be32(value
);
794 migrate_send_rp_message(mis
, MIG_RP_MSG_RESUME_ACK
, sizeof(buf
), &buf
);
797 MigrationCapabilityStatusList
*qmp_query_migrate_capabilities(Error
**errp
)
799 MigrationCapabilityStatusList
*head
= NULL
;
800 MigrationCapabilityStatusList
*caps
;
801 MigrationState
*s
= migrate_get_current();
804 caps
= NULL
; /* silence compiler warning */
805 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
806 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
807 if (i
== MIGRATION_CAPABILITY_BLOCK
) {
812 head
= g_malloc0(sizeof(*caps
));
815 caps
->next
= g_malloc0(sizeof(*caps
));
819 g_malloc(sizeof(*caps
->value
));
820 caps
->value
->capability
= i
;
821 caps
->value
->state
= s
->enabled_capabilities
[i
];
827 MigrationParameters
*qmp_query_migrate_parameters(Error
**errp
)
829 MigrationParameters
*params
;
830 MigrationState
*s
= migrate_get_current();
832 /* TODO use QAPI_CLONE() instead of duplicating it inline */
833 params
= g_malloc0(sizeof(*params
));
834 params
->has_compress_level
= true;
835 params
->compress_level
= s
->parameters
.compress_level
;
836 params
->has_compress_threads
= true;
837 params
->compress_threads
= s
->parameters
.compress_threads
;
838 params
->has_compress_wait_thread
= true;
839 params
->compress_wait_thread
= s
->parameters
.compress_wait_thread
;
840 params
->has_decompress_threads
= true;
841 params
->decompress_threads
= s
->parameters
.decompress_threads
;
842 params
->has_throttle_trigger_threshold
= true;
843 params
->throttle_trigger_threshold
= s
->parameters
.throttle_trigger_threshold
;
844 params
->has_cpu_throttle_initial
= true;
845 params
->cpu_throttle_initial
= s
->parameters
.cpu_throttle_initial
;
846 params
->has_cpu_throttle_increment
= true;
847 params
->cpu_throttle_increment
= s
->parameters
.cpu_throttle_increment
;
848 params
->has_cpu_throttle_tailslow
= true;
849 params
->cpu_throttle_tailslow
= s
->parameters
.cpu_throttle_tailslow
;
850 params
->has_tls_creds
= true;
851 params
->tls_creds
= g_strdup(s
->parameters
.tls_creds
);
852 params
->has_tls_hostname
= true;
853 params
->tls_hostname
= g_strdup(s
->parameters
.tls_hostname
);
854 params
->has_tls_authz
= true;
855 params
->tls_authz
= g_strdup(s
->parameters
.tls_authz
?
856 s
->parameters
.tls_authz
: "");
857 params
->has_max_bandwidth
= true;
858 params
->max_bandwidth
= s
->parameters
.max_bandwidth
;
859 params
->has_downtime_limit
= true;
860 params
->downtime_limit
= s
->parameters
.downtime_limit
;
861 params
->has_x_checkpoint_delay
= true;
862 params
->x_checkpoint_delay
= s
->parameters
.x_checkpoint_delay
;
863 params
->has_block_incremental
= true;
864 params
->block_incremental
= s
->parameters
.block_incremental
;
865 params
->has_multifd_channels
= true;
866 params
->multifd_channels
= s
->parameters
.multifd_channels
;
867 params
->has_multifd_compression
= true;
868 params
->multifd_compression
= s
->parameters
.multifd_compression
;
869 params
->has_multifd_zlib_level
= true;
870 params
->multifd_zlib_level
= s
->parameters
.multifd_zlib_level
;
871 params
->has_multifd_zstd_level
= true;
872 params
->multifd_zstd_level
= s
->parameters
.multifd_zstd_level
;
873 params
->has_xbzrle_cache_size
= true;
874 params
->xbzrle_cache_size
= s
->parameters
.xbzrle_cache_size
;
875 params
->has_max_postcopy_bandwidth
= true;
876 params
->max_postcopy_bandwidth
= s
->parameters
.max_postcopy_bandwidth
;
877 params
->has_max_cpu_throttle
= true;
878 params
->max_cpu_throttle
= s
->parameters
.max_cpu_throttle
;
879 params
->has_announce_initial
= true;
880 params
->announce_initial
= s
->parameters
.announce_initial
;
881 params
->has_announce_max
= true;
882 params
->announce_max
= s
->parameters
.announce_max
;
883 params
->has_announce_rounds
= true;
884 params
->announce_rounds
= s
->parameters
.announce_rounds
;
885 params
->has_announce_step
= true;
886 params
->announce_step
= s
->parameters
.announce_step
;
888 if (s
->parameters
.has_block_bitmap_mapping
) {
889 params
->has_block_bitmap_mapping
= true;
890 params
->block_bitmap_mapping
=
891 QAPI_CLONE(BitmapMigrationNodeAliasList
,
892 s
->parameters
.block_bitmap_mapping
);
898 AnnounceParameters
*migrate_announce_params(void)
900 static AnnounceParameters ap
;
902 MigrationState
*s
= migrate_get_current();
904 ap
.initial
= s
->parameters
.announce_initial
;
905 ap
.max
= s
->parameters
.announce_max
;
906 ap
.rounds
= s
->parameters
.announce_rounds
;
907 ap
.step
= s
->parameters
.announce_step
;
913 * Return true if we're already in the middle of a migration
914 * (i.e. any of the active or setup states)
916 bool migration_is_setup_or_active(int state
)
919 case MIGRATION_STATUS_ACTIVE
:
920 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
921 case MIGRATION_STATUS_POSTCOPY_PAUSED
:
922 case MIGRATION_STATUS_POSTCOPY_RECOVER
:
923 case MIGRATION_STATUS_SETUP
:
924 case MIGRATION_STATUS_PRE_SWITCHOVER
:
925 case MIGRATION_STATUS_DEVICE
:
926 case MIGRATION_STATUS_WAIT_UNPLUG
:
927 case MIGRATION_STATUS_COLO
:
936 bool migration_is_running(int state
)
939 case MIGRATION_STATUS_ACTIVE
:
940 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
941 case MIGRATION_STATUS_POSTCOPY_PAUSED
:
942 case MIGRATION_STATUS_POSTCOPY_RECOVER
:
943 case MIGRATION_STATUS_SETUP
:
944 case MIGRATION_STATUS_PRE_SWITCHOVER
:
945 case MIGRATION_STATUS_DEVICE
:
946 case MIGRATION_STATUS_WAIT_UNPLUG
:
947 case MIGRATION_STATUS_CANCELLING
:
956 static void populate_time_info(MigrationInfo
*info
, MigrationState
*s
)
958 info
->has_status
= true;
959 info
->has_setup_time
= true;
960 info
->setup_time
= s
->setup_time
;
961 if (s
->state
== MIGRATION_STATUS_COMPLETED
) {
962 info
->has_total_time
= true;
963 info
->total_time
= s
->total_time
;
964 info
->has_downtime
= true;
965 info
->downtime
= s
->downtime
;
967 info
->has_total_time
= true;
968 info
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) -
970 info
->has_expected_downtime
= true;
971 info
->expected_downtime
= s
->expected_downtime
;
975 static void populate_ram_info(MigrationInfo
*info
, MigrationState
*s
)
977 info
->has_ram
= true;
978 info
->ram
= g_malloc0(sizeof(*info
->ram
));
979 info
->ram
->transferred
= ram_counters
.transferred
;
980 info
->ram
->total
= ram_bytes_total();
981 info
->ram
->duplicate
= ram_counters
.duplicate
;
982 /* legacy value. It is not used anymore */
983 info
->ram
->skipped
= 0;
984 info
->ram
->normal
= ram_counters
.normal
;
985 info
->ram
->normal_bytes
= ram_counters
.normal
*
986 qemu_target_page_size();
987 info
->ram
->mbps
= s
->mbps
;
988 info
->ram
->dirty_sync_count
= ram_counters
.dirty_sync_count
;
989 info
->ram
->postcopy_requests
= ram_counters
.postcopy_requests
;
990 info
->ram
->page_size
= qemu_target_page_size();
991 info
->ram
->multifd_bytes
= ram_counters
.multifd_bytes
;
992 info
->ram
->pages_per_second
= s
->pages_per_second
;
994 if (migrate_use_xbzrle()) {
995 info
->has_xbzrle_cache
= true;
996 info
->xbzrle_cache
= g_malloc0(sizeof(*info
->xbzrle_cache
));
997 info
->xbzrle_cache
->cache_size
= migrate_xbzrle_cache_size();
998 info
->xbzrle_cache
->bytes
= xbzrle_counters
.bytes
;
999 info
->xbzrle_cache
->pages
= xbzrle_counters
.pages
;
1000 info
->xbzrle_cache
->cache_miss
= xbzrle_counters
.cache_miss
;
1001 info
->xbzrle_cache
->cache_miss_rate
= xbzrle_counters
.cache_miss_rate
;
1002 info
->xbzrle_cache
->encoding_rate
= xbzrle_counters
.encoding_rate
;
1003 info
->xbzrle_cache
->overflow
= xbzrle_counters
.overflow
;
1006 if (migrate_use_compression()) {
1007 info
->has_compression
= true;
1008 info
->compression
= g_malloc0(sizeof(*info
->compression
));
1009 info
->compression
->pages
= compression_counters
.pages
;
1010 info
->compression
->busy
= compression_counters
.busy
;
1011 info
->compression
->busy_rate
= compression_counters
.busy_rate
;
1012 info
->compression
->compressed_size
=
1013 compression_counters
.compressed_size
;
1014 info
->compression
->compression_rate
=
1015 compression_counters
.compression_rate
;
1018 if (cpu_throttle_active()) {
1019 info
->has_cpu_throttle_percentage
= true;
1020 info
->cpu_throttle_percentage
= cpu_throttle_get_percentage();
1023 if (s
->state
!= MIGRATION_STATUS_COMPLETED
) {
1024 info
->ram
->remaining
= ram_bytes_remaining();
1025 info
->ram
->dirty_pages_rate
= ram_counters
.dirty_pages_rate
;
1029 static void populate_disk_info(MigrationInfo
*info
)
1031 if (blk_mig_active()) {
1032 info
->has_disk
= true;
1033 info
->disk
= g_malloc0(sizeof(*info
->disk
));
1034 info
->disk
->transferred
= blk_mig_bytes_transferred();
1035 info
->disk
->remaining
= blk_mig_bytes_remaining();
1036 info
->disk
->total
= blk_mig_bytes_total();
1040 static void fill_source_migration_info(MigrationInfo
*info
)
1042 MigrationState
*s
= migrate_get_current();
1045 case MIGRATION_STATUS_NONE
:
1046 /* no migration has happened ever */
1047 /* do not overwrite destination migration status */
1049 case MIGRATION_STATUS_SETUP
:
1050 info
->has_status
= true;
1051 info
->has_total_time
= false;
1053 case MIGRATION_STATUS_ACTIVE
:
1054 case MIGRATION_STATUS_CANCELLING
:
1055 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
1056 case MIGRATION_STATUS_PRE_SWITCHOVER
:
1057 case MIGRATION_STATUS_DEVICE
:
1058 case MIGRATION_STATUS_POSTCOPY_PAUSED
:
1059 case MIGRATION_STATUS_POSTCOPY_RECOVER
:
1060 /* TODO add some postcopy stats */
1061 populate_time_info(info
, s
);
1062 populate_ram_info(info
, s
);
1063 populate_disk_info(info
);
1065 case MIGRATION_STATUS_COLO
:
1066 info
->has_status
= true;
1067 /* TODO: display COLO specific information (checkpoint info etc.) */
1069 case MIGRATION_STATUS_COMPLETED
:
1070 populate_time_info(info
, s
);
1071 populate_ram_info(info
, s
);
1073 case MIGRATION_STATUS_FAILED
:
1074 info
->has_status
= true;
1076 info
->has_error_desc
= true;
1077 info
->error_desc
= g_strdup(error_get_pretty(s
->error
));
1080 case MIGRATION_STATUS_CANCELLED
:
1081 info
->has_status
= true;
1083 case MIGRATION_STATUS_WAIT_UNPLUG
:
1084 info
->has_status
= true;
1087 info
->status
= s
->state
;
1091 * @migration_caps_check - check capability validity
1093 * @cap_list: old capability list, array of bool
1094 * @params: new capabilities to be applied soon
1095 * @errp: set *errp if the check failed, with reason
1097 * Returns true if check passed, otherwise false.
1099 static bool migrate_caps_check(bool *cap_list
,
1100 MigrationCapabilityStatusList
*params
,
1103 MigrationCapabilityStatusList
*cap
;
1104 bool old_postcopy_cap
;
1105 MigrationIncomingState
*mis
= migration_incoming_get_current();
1107 old_postcopy_cap
= cap_list
[MIGRATION_CAPABILITY_POSTCOPY_RAM
];
1109 for (cap
= params
; cap
; cap
= cap
->next
) {
1110 cap_list
[cap
->value
->capability
] = cap
->value
->state
;
1113 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
1114 if (cap_list
[MIGRATION_CAPABILITY_BLOCK
]) {
1115 error_setg(errp
, "QEMU compiled without old-style (blk/-b, inc/-i) "
1117 error_append_hint(errp
, "Use drive_mirror+NBD instead.\n");
1122 #ifndef CONFIG_REPLICATION
1123 if (cap_list
[MIGRATION_CAPABILITY_X_COLO
]) {
1124 error_setg(errp
, "QEMU compiled without replication module"
1125 " can't enable COLO");
1126 error_append_hint(errp
, "Please enable replication before COLO.\n");
1131 if (cap_list
[MIGRATION_CAPABILITY_POSTCOPY_RAM
]) {
1132 /* This check is reasonably expensive, so only when it's being
1133 * set the first time, also it's only the destination that needs
1136 if (!old_postcopy_cap
&& runstate_check(RUN_STATE_INMIGRATE
) &&
1137 !postcopy_ram_supported_by_host(mis
)) {
1138 /* postcopy_ram_supported_by_host will have emitted a more
1141 error_setg(errp
, "Postcopy is not supported");
1145 if (cap_list
[MIGRATION_CAPABILITY_X_IGNORE_SHARED
]) {
1146 error_setg(errp
, "Postcopy is not compatible with ignore-shared");
1154 static void fill_destination_migration_info(MigrationInfo
*info
)
1156 MigrationIncomingState
*mis
= migration_incoming_get_current();
1158 if (mis
->socket_address_list
) {
1159 info
->has_socket_address
= true;
1160 info
->socket_address
=
1161 QAPI_CLONE(SocketAddressList
, mis
->socket_address_list
);
1164 switch (mis
->state
) {
1165 case MIGRATION_STATUS_NONE
:
1167 case MIGRATION_STATUS_SETUP
:
1168 case MIGRATION_STATUS_CANCELLING
:
1169 case MIGRATION_STATUS_CANCELLED
:
1170 case MIGRATION_STATUS_ACTIVE
:
1171 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
1172 case MIGRATION_STATUS_POSTCOPY_PAUSED
:
1173 case MIGRATION_STATUS_POSTCOPY_RECOVER
:
1174 case MIGRATION_STATUS_FAILED
:
1175 case MIGRATION_STATUS_COLO
:
1176 info
->has_status
= true;
1178 case MIGRATION_STATUS_COMPLETED
:
1179 info
->has_status
= true;
1180 fill_destination_postcopy_migration_info(info
);
1183 info
->status
= mis
->state
;
1186 MigrationInfo
*qmp_query_migrate(Error
**errp
)
1188 MigrationInfo
*info
= g_malloc0(sizeof(*info
));
1190 fill_destination_migration_info(info
);
1191 fill_source_migration_info(info
);
1196 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList
*params
,
1199 MigrationState
*s
= migrate_get_current();
1200 MigrationCapabilityStatusList
*cap
;
1201 bool cap_list
[MIGRATION_CAPABILITY__MAX
];
1203 if (migration_is_running(s
->state
)) {
1204 error_setg(errp
, QERR_MIGRATION_ACTIVE
);
1208 memcpy(cap_list
, s
->enabled_capabilities
, sizeof(cap_list
));
1209 if (!migrate_caps_check(cap_list
, params
, errp
)) {
1213 for (cap
= params
; cap
; cap
= cap
->next
) {
1214 s
->enabled_capabilities
[cap
->value
->capability
] = cap
->value
->state
;
1219 * Check whether the parameters are valid. Error will be put into errp
1220 * (if provided). Return true if valid, otherwise false.
1222 static bool migrate_params_check(MigrationParameters
*params
, Error
**errp
)
1224 if (params
->has_compress_level
&&
1225 (params
->compress_level
> 9)) {
1226 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "compress_level",
1227 "is invalid, it should be in the range of 0 to 9");
1231 if (params
->has_compress_threads
&& (params
->compress_threads
< 1)) {
1232 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1234 "is invalid, it should be in the range of 1 to 255");
1238 if (params
->has_decompress_threads
&& (params
->decompress_threads
< 1)) {
1239 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1240 "decompress_threads",
1241 "is invalid, it should be in the range of 1 to 255");
1245 if (params
->has_throttle_trigger_threshold
&&
1246 (params
->throttle_trigger_threshold
< 1 ||
1247 params
->throttle_trigger_threshold
> 100)) {
1248 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1249 "throttle_trigger_threshold",
1250 "an integer in the range of 1 to 100");
1254 if (params
->has_cpu_throttle_initial
&&
1255 (params
->cpu_throttle_initial
< 1 ||
1256 params
->cpu_throttle_initial
> 99)) {
1257 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1258 "cpu_throttle_initial",
1259 "an integer in the range of 1 to 99");
1263 if (params
->has_cpu_throttle_increment
&&
1264 (params
->cpu_throttle_increment
< 1 ||
1265 params
->cpu_throttle_increment
> 99)) {
1266 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1267 "cpu_throttle_increment",
1268 "an integer in the range of 1 to 99");
1272 if (params
->has_max_bandwidth
&& (params
->max_bandwidth
> SIZE_MAX
)) {
1273 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1275 "an integer in the range of 0 to "stringify(SIZE_MAX
)
1280 if (params
->has_downtime_limit
&&
1281 (params
->downtime_limit
> MAX_MIGRATE_DOWNTIME
)) {
1282 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1284 "an integer in the range of 0 to "
1285 stringify(MAX_MIGRATE_DOWNTIME
)" ms");
1289 /* x_checkpoint_delay is now always positive */
1291 if (params
->has_multifd_channels
&& (params
->multifd_channels
< 1)) {
1292 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1294 "is invalid, it should be in the range of 1 to 255");
1298 if (params
->has_multifd_zlib_level
&&
1299 (params
->multifd_zlib_level
> 9)) {
1300 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "multifd_zlib_level",
1301 "is invalid, it should be in the range of 0 to 9");
1305 if (params
->has_multifd_zstd_level
&&
1306 (params
->multifd_zstd_level
> 20)) {
1307 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "multifd_zstd_level",
1308 "is invalid, it should be in the range of 0 to 20");
1312 if (params
->has_xbzrle_cache_size
&&
1313 (params
->xbzrle_cache_size
< qemu_target_page_size() ||
1314 !is_power_of_2(params
->xbzrle_cache_size
))) {
1315 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1316 "xbzrle_cache_size",
1317 "is invalid, it should be bigger than target page size"
1318 " and a power of 2");
1322 if (params
->has_max_cpu_throttle
&&
1323 (params
->max_cpu_throttle
< params
->cpu_throttle_initial
||
1324 params
->max_cpu_throttle
> 99)) {
1325 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1327 "an integer in the range of cpu_throttle_initial to 99");
1331 if (params
->has_announce_initial
&&
1332 params
->announce_initial
> 100000) {
1333 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1335 "is invalid, it must be less than 100000 ms");
1338 if (params
->has_announce_max
&&
1339 params
->announce_max
> 100000) {
1340 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1342 "is invalid, it must be less than 100000 ms");
1345 if (params
->has_announce_rounds
&&
1346 params
->announce_rounds
> 1000) {
1347 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1349 "is invalid, it must be in the range of 0 to 1000");
1352 if (params
->has_announce_step
&&
1353 (params
->announce_step
< 1 ||
1354 params
->announce_step
> 10000)) {
1355 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
1357 "is invalid, it must be in the range of 1 to 10000 ms");
1361 if (params
->has_block_bitmap_mapping
&&
1362 !check_dirty_bitmap_mig_alias_map(params
->block_bitmap_mapping
, errp
)) {
1363 error_prepend(errp
, "Invalid mapping given for block-bitmap-mapping: ");
1370 static void migrate_params_test_apply(MigrateSetParameters
*params
,
1371 MigrationParameters
*dest
)
1373 *dest
= migrate_get_current()->parameters
;
1375 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1377 if (params
->has_compress_level
) {
1378 dest
->compress_level
= params
->compress_level
;
1381 if (params
->has_compress_threads
) {
1382 dest
->compress_threads
= params
->compress_threads
;
1385 if (params
->has_compress_wait_thread
) {
1386 dest
->compress_wait_thread
= params
->compress_wait_thread
;
1389 if (params
->has_decompress_threads
) {
1390 dest
->decompress_threads
= params
->decompress_threads
;
1393 if (params
->has_throttle_trigger_threshold
) {
1394 dest
->throttle_trigger_threshold
= params
->throttle_trigger_threshold
;
1397 if (params
->has_cpu_throttle_initial
) {
1398 dest
->cpu_throttle_initial
= params
->cpu_throttle_initial
;
1401 if (params
->has_cpu_throttle_increment
) {
1402 dest
->cpu_throttle_increment
= params
->cpu_throttle_increment
;
1405 if (params
->has_cpu_throttle_tailslow
) {
1406 dest
->cpu_throttle_tailslow
= params
->cpu_throttle_tailslow
;
1409 if (params
->has_tls_creds
) {
1410 assert(params
->tls_creds
->type
== QTYPE_QSTRING
);
1411 dest
->tls_creds
= params
->tls_creds
->u
.s
;
1414 if (params
->has_tls_hostname
) {
1415 assert(params
->tls_hostname
->type
== QTYPE_QSTRING
);
1416 dest
->tls_hostname
= params
->tls_hostname
->u
.s
;
1419 if (params
->has_max_bandwidth
) {
1420 dest
->max_bandwidth
= params
->max_bandwidth
;
1423 if (params
->has_downtime_limit
) {
1424 dest
->downtime_limit
= params
->downtime_limit
;
1427 if (params
->has_x_checkpoint_delay
) {
1428 dest
->x_checkpoint_delay
= params
->x_checkpoint_delay
;
1431 if (params
->has_block_incremental
) {
1432 dest
->block_incremental
= params
->block_incremental
;
1434 if (params
->has_multifd_channels
) {
1435 dest
->multifd_channels
= params
->multifd_channels
;
1437 if (params
->has_multifd_compression
) {
1438 dest
->multifd_compression
= params
->multifd_compression
;
1440 if (params
->has_xbzrle_cache_size
) {
1441 dest
->xbzrle_cache_size
= params
->xbzrle_cache_size
;
1443 if (params
->has_max_postcopy_bandwidth
) {
1444 dest
->max_postcopy_bandwidth
= params
->max_postcopy_bandwidth
;
1446 if (params
->has_max_cpu_throttle
) {
1447 dest
->max_cpu_throttle
= params
->max_cpu_throttle
;
1449 if (params
->has_announce_initial
) {
1450 dest
->announce_initial
= params
->announce_initial
;
1452 if (params
->has_announce_max
) {
1453 dest
->announce_max
= params
->announce_max
;
1455 if (params
->has_announce_rounds
) {
1456 dest
->announce_rounds
= params
->announce_rounds
;
1458 if (params
->has_announce_step
) {
1459 dest
->announce_step
= params
->announce_step
;
1462 if (params
->has_block_bitmap_mapping
) {
1463 dest
->has_block_bitmap_mapping
= true;
1464 dest
->block_bitmap_mapping
= params
->block_bitmap_mapping
;
1468 static void migrate_params_apply(MigrateSetParameters
*params
, Error
**errp
)
1470 MigrationState
*s
= migrate_get_current();
1472 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1474 if (params
->has_compress_level
) {
1475 s
->parameters
.compress_level
= params
->compress_level
;
1478 if (params
->has_compress_threads
) {
1479 s
->parameters
.compress_threads
= params
->compress_threads
;
1482 if (params
->has_compress_wait_thread
) {
1483 s
->parameters
.compress_wait_thread
= params
->compress_wait_thread
;
1486 if (params
->has_decompress_threads
) {
1487 s
->parameters
.decompress_threads
= params
->decompress_threads
;
1490 if (params
->has_throttle_trigger_threshold
) {
1491 s
->parameters
.throttle_trigger_threshold
= params
->throttle_trigger_threshold
;
1494 if (params
->has_cpu_throttle_initial
) {
1495 s
->parameters
.cpu_throttle_initial
= params
->cpu_throttle_initial
;
1498 if (params
->has_cpu_throttle_increment
) {
1499 s
->parameters
.cpu_throttle_increment
= params
->cpu_throttle_increment
;
1502 if (params
->has_cpu_throttle_tailslow
) {
1503 s
->parameters
.cpu_throttle_tailslow
= params
->cpu_throttle_tailslow
;
1506 if (params
->has_tls_creds
) {
1507 g_free(s
->parameters
.tls_creds
);
1508 assert(params
->tls_creds
->type
== QTYPE_QSTRING
);
1509 s
->parameters
.tls_creds
= g_strdup(params
->tls_creds
->u
.s
);
1512 if (params
->has_tls_hostname
) {
1513 g_free(s
->parameters
.tls_hostname
);
1514 assert(params
->tls_hostname
->type
== QTYPE_QSTRING
);
1515 s
->parameters
.tls_hostname
= g_strdup(params
->tls_hostname
->u
.s
);
1518 if (params
->has_tls_authz
) {
1519 g_free(s
->parameters
.tls_authz
);
1520 assert(params
->tls_authz
->type
== QTYPE_QSTRING
);
1521 s
->parameters
.tls_authz
= g_strdup(params
->tls_authz
->u
.s
);
1524 if (params
->has_max_bandwidth
) {
1525 s
->parameters
.max_bandwidth
= params
->max_bandwidth
;
1526 if (s
->to_dst_file
&& !migration_in_postcopy()) {
1527 qemu_file_set_rate_limit(s
->to_dst_file
,
1528 s
->parameters
.max_bandwidth
/ XFER_LIMIT_RATIO
);
1532 if (params
->has_downtime_limit
) {
1533 s
->parameters
.downtime_limit
= params
->downtime_limit
;
1536 if (params
->has_x_checkpoint_delay
) {
1537 s
->parameters
.x_checkpoint_delay
= params
->x_checkpoint_delay
;
1538 if (migration_in_colo_state()) {
1539 colo_checkpoint_notify(s
);
1543 if (params
->has_block_incremental
) {
1544 s
->parameters
.block_incremental
= params
->block_incremental
;
1546 if (params
->has_multifd_channels
) {
1547 s
->parameters
.multifd_channels
= params
->multifd_channels
;
1549 if (params
->has_multifd_compression
) {
1550 s
->parameters
.multifd_compression
= params
->multifd_compression
;
1552 if (params
->has_xbzrle_cache_size
) {
1553 s
->parameters
.xbzrle_cache_size
= params
->xbzrle_cache_size
;
1554 xbzrle_cache_resize(params
->xbzrle_cache_size
, errp
);
1556 if (params
->has_max_postcopy_bandwidth
) {
1557 s
->parameters
.max_postcopy_bandwidth
= params
->max_postcopy_bandwidth
;
1558 if (s
->to_dst_file
&& migration_in_postcopy()) {
1559 qemu_file_set_rate_limit(s
->to_dst_file
,
1560 s
->parameters
.max_postcopy_bandwidth
/ XFER_LIMIT_RATIO
);
1563 if (params
->has_max_cpu_throttle
) {
1564 s
->parameters
.max_cpu_throttle
= params
->max_cpu_throttle
;
1566 if (params
->has_announce_initial
) {
1567 s
->parameters
.announce_initial
= params
->announce_initial
;
1569 if (params
->has_announce_max
) {
1570 s
->parameters
.announce_max
= params
->announce_max
;
1572 if (params
->has_announce_rounds
) {
1573 s
->parameters
.announce_rounds
= params
->announce_rounds
;
1575 if (params
->has_announce_step
) {
1576 s
->parameters
.announce_step
= params
->announce_step
;
1579 if (params
->has_block_bitmap_mapping
) {
1580 qapi_free_BitmapMigrationNodeAliasList(
1581 s
->parameters
.block_bitmap_mapping
);
1583 s
->parameters
.has_block_bitmap_mapping
= true;
1584 s
->parameters
.block_bitmap_mapping
=
1585 QAPI_CLONE(BitmapMigrationNodeAliasList
,
1586 params
->block_bitmap_mapping
);
1590 void qmp_migrate_set_parameters(MigrateSetParameters
*params
, Error
**errp
)
1592 MigrationParameters tmp
;
1594 /* TODO Rewrite "" to null instead */
1595 if (params
->has_tls_creds
1596 && params
->tls_creds
->type
== QTYPE_QNULL
) {
1597 qobject_unref(params
->tls_creds
->u
.n
);
1598 params
->tls_creds
->type
= QTYPE_QSTRING
;
1599 params
->tls_creds
->u
.s
= strdup("");
1601 /* TODO Rewrite "" to null instead */
1602 if (params
->has_tls_hostname
1603 && params
->tls_hostname
->type
== QTYPE_QNULL
) {
1604 qobject_unref(params
->tls_hostname
->u
.n
);
1605 params
->tls_hostname
->type
= QTYPE_QSTRING
;
1606 params
->tls_hostname
->u
.s
= strdup("");
1609 migrate_params_test_apply(params
, &tmp
);
1611 if (!migrate_params_check(&tmp
, errp
)) {
1612 /* Invalid parameter */
1616 migrate_params_apply(params
, errp
);
1620 void qmp_migrate_start_postcopy(Error
**errp
)
1622 MigrationState
*s
= migrate_get_current();
1624 if (!migrate_postcopy()) {
1625 error_setg(errp
, "Enable postcopy with migrate_set_capability before"
1626 " the start of migration");
1630 if (s
->state
== MIGRATION_STATUS_NONE
) {
1631 error_setg(errp
, "Postcopy must be started after migration has been"
1636 * we don't error if migration has finished since that would be racy
1637 * with issuing this command.
1639 qatomic_set(&s
->start_postcopy
, true);
1642 /* shared migration helpers */
1644 void migrate_set_state(int *state
, int old_state
, int new_state
)
1646 assert(new_state
< MIGRATION_STATUS__MAX
);
1647 if (qatomic_cmpxchg(state
, old_state
, new_state
) == old_state
) {
1648 trace_migrate_set_state(MigrationStatus_str(new_state
));
1649 migrate_generate_event(new_state
);
1653 static MigrationCapabilityStatusList
*migrate_cap_add(
1654 MigrationCapabilityStatusList
*list
,
1655 MigrationCapability index
,
1658 MigrationCapabilityStatusList
*cap
;
1660 cap
= g_new0(MigrationCapabilityStatusList
, 1);
1661 cap
->value
= g_new0(MigrationCapabilityStatus
, 1);
1662 cap
->value
->capability
= index
;
1663 cap
->value
->state
= state
;
1669 void migrate_set_block_enabled(bool value
, Error
**errp
)
1671 MigrationCapabilityStatusList
*cap
;
1673 cap
= migrate_cap_add(NULL
, MIGRATION_CAPABILITY_BLOCK
, value
);
1674 qmp_migrate_set_capabilities(cap
, errp
);
1675 qapi_free_MigrationCapabilityStatusList(cap
);
1678 static void migrate_set_block_incremental(MigrationState
*s
, bool value
)
1680 s
->parameters
.block_incremental
= value
;
1683 static void block_cleanup_parameters(MigrationState
*s
)
1685 if (s
->must_remove_block_options
) {
1686 /* setting to false can never fail */
1687 migrate_set_block_enabled(false, &error_abort
);
1688 migrate_set_block_incremental(s
, false);
1689 s
->must_remove_block_options
= false;
1693 static void migrate_fd_cleanup(MigrationState
*s
)
1695 qemu_bh_delete(s
->cleanup_bh
);
1696 s
->cleanup_bh
= NULL
;
1698 qemu_savevm_state_cleanup();
1700 if (s
->to_dst_file
) {
1703 trace_migrate_fd_cleanup();
1704 qemu_mutex_unlock_iothread();
1705 if (s
->migration_thread_running
) {
1706 qemu_thread_join(&s
->thread
);
1707 s
->migration_thread_running
= false;
1709 qemu_mutex_lock_iothread();
1711 multifd_save_cleanup();
1712 qemu_mutex_lock(&s
->qemu_file_lock
);
1713 tmp
= s
->to_dst_file
;
1714 s
->to_dst_file
= NULL
;
1715 qemu_mutex_unlock(&s
->qemu_file_lock
);
1717 * Close the file handle without the lock to make sure the
1718 * critical section won't block for long.
1723 assert(!migration_is_active(s
));
1725 if (s
->state
== MIGRATION_STATUS_CANCELLING
) {
1726 migrate_set_state(&s
->state
, MIGRATION_STATUS_CANCELLING
,
1727 MIGRATION_STATUS_CANCELLED
);
1731 /* It is used on info migrate. We can't free it */
1732 error_report_err(error_copy(s
->error
));
1734 notifier_list_notify(&migration_state_notifiers
, s
);
1735 block_cleanup_parameters(s
);
1738 static void migrate_fd_cleanup_schedule(MigrationState
*s
)
1741 * Ref the state for bh, because it may be called when
1742 * there're already no other refs
1744 object_ref(OBJECT(s
));
1745 qemu_bh_schedule(s
->cleanup_bh
);
1748 static void migrate_fd_cleanup_bh(void *opaque
)
1750 MigrationState
*s
= opaque
;
1751 migrate_fd_cleanup(s
);
1752 object_unref(OBJECT(s
));
1755 void migrate_set_error(MigrationState
*s
, const Error
*error
)
1757 QEMU_LOCK_GUARD(&s
->error_mutex
);
1759 s
->error
= error_copy(error
);
1763 void migrate_fd_error(MigrationState
*s
, const Error
*error
)
1765 trace_migrate_fd_error(error_get_pretty(error
));
1766 assert(s
->to_dst_file
== NULL
);
1767 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
1768 MIGRATION_STATUS_FAILED
);
1769 migrate_set_error(s
, error
);
1772 static void migrate_fd_cancel(MigrationState
*s
)
1775 QEMUFile
*f
= migrate_get_current()->to_dst_file
;
1776 trace_migrate_fd_cancel();
1778 if (s
->rp_state
.from_dst_file
) {
1779 /* shutdown the rp socket, so causing the rp thread to shutdown */
1780 qemu_file_shutdown(s
->rp_state
.from_dst_file
);
1784 old_state
= s
->state
;
1785 if (!migration_is_running(old_state
)) {
1788 /* If the migration is paused, kick it out of the pause */
1789 if (old_state
== MIGRATION_STATUS_PRE_SWITCHOVER
) {
1790 qemu_sem_post(&s
->pause_sem
);
1792 migrate_set_state(&s
->state
, old_state
, MIGRATION_STATUS_CANCELLING
);
1793 } while (s
->state
!= MIGRATION_STATUS_CANCELLING
);
1796 * If we're unlucky the migration code might be stuck somewhere in a
1797 * send/write while the network has failed and is waiting to timeout;
1798 * if we've got shutdown(2) available then we can force it to quit.
1799 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1800 * called in a bh, so there is no race against this cancel.
1802 if (s
->state
== MIGRATION_STATUS_CANCELLING
&& f
) {
1803 qemu_file_shutdown(f
);
1805 if (s
->state
== MIGRATION_STATUS_CANCELLING
&& s
->block_inactive
) {
1806 Error
*local_err
= NULL
;
1808 bdrv_invalidate_cache_all(&local_err
);
1810 error_report_err(local_err
);
1812 s
->block_inactive
= false;
1817 void add_migration_state_change_notifier(Notifier
*notify
)
1819 notifier_list_add(&migration_state_notifiers
, notify
);
1822 void remove_migration_state_change_notifier(Notifier
*notify
)
1824 notifier_remove(notify
);
1827 bool migration_in_setup(MigrationState
*s
)
1829 return s
->state
== MIGRATION_STATUS_SETUP
;
1832 bool migration_has_finished(MigrationState
*s
)
1834 return s
->state
== MIGRATION_STATUS_COMPLETED
;
1837 bool migration_has_failed(MigrationState
*s
)
1839 return (s
->state
== MIGRATION_STATUS_CANCELLED
||
1840 s
->state
== MIGRATION_STATUS_FAILED
);
1843 bool migration_in_postcopy(void)
1845 MigrationState
*s
= migrate_get_current();
1848 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
1849 case MIGRATION_STATUS_POSTCOPY_PAUSED
:
1850 case MIGRATION_STATUS_POSTCOPY_RECOVER
:
1857 bool migration_in_postcopy_after_devices(MigrationState
*s
)
1859 return migration_in_postcopy() && s
->postcopy_after_devices
;
1862 bool migration_in_incoming_postcopy(void)
1864 PostcopyState ps
= postcopy_state_get();
1866 return ps
>= POSTCOPY_INCOMING_DISCARD
&& ps
< POSTCOPY_INCOMING_END
;
1869 bool migration_is_idle(void)
1871 MigrationState
*s
= current_migration
;
1878 case MIGRATION_STATUS_NONE
:
1879 case MIGRATION_STATUS_CANCELLED
:
1880 case MIGRATION_STATUS_COMPLETED
:
1881 case MIGRATION_STATUS_FAILED
:
1883 case MIGRATION_STATUS_SETUP
:
1884 case MIGRATION_STATUS_CANCELLING
:
1885 case MIGRATION_STATUS_ACTIVE
:
1886 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
1887 case MIGRATION_STATUS_COLO
:
1888 case MIGRATION_STATUS_PRE_SWITCHOVER
:
1889 case MIGRATION_STATUS_DEVICE
:
1890 case MIGRATION_STATUS_WAIT_UNPLUG
:
1892 case MIGRATION_STATUS__MAX
:
1893 g_assert_not_reached();
1899 bool migration_is_active(MigrationState
*s
)
1901 return (s
->state
== MIGRATION_STATUS_ACTIVE
||
1902 s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
);
1905 void migrate_init(MigrationState
*s
)
1908 * Reinitialise all migration state, except
1909 * parameters/capabilities that the user set, and
1913 s
->to_dst_file
= NULL
;
1914 s
->state
= MIGRATION_STATUS_NONE
;
1915 s
->rp_state
.from_dst_file
= NULL
;
1916 s
->rp_state
.error
= false;
1918 s
->pages_per_second
= 0.0;
1920 s
->expected_downtime
= 0;
1922 s
->start_postcopy
= false;
1923 s
->postcopy_after_devices
= false;
1924 s
->migration_thread_running
= false;
1925 error_free(s
->error
);
1929 migrate_set_state(&s
->state
, MIGRATION_STATUS_NONE
, MIGRATION_STATUS_SETUP
);
1931 s
->start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1933 s
->vm_was_running
= false;
1934 s
->iteration_initial_bytes
= 0;
1935 s
->threshold_size
= 0;
1938 static GSList
*migration_blockers
;
1940 int migrate_add_blocker(Error
*reason
, Error
**errp
)
1942 if (only_migratable
) {
1943 error_propagate_prepend(errp
, error_copy(reason
),
1944 "disallowing migration blocker "
1945 "(--only-migratable) for: ");
1949 if (migration_is_idle()) {
1950 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
1954 error_propagate_prepend(errp
, error_copy(reason
),
1955 "disallowing migration blocker "
1956 "(migration in progress) for: ");
1960 void migrate_del_blocker(Error
*reason
)
1962 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
1965 void qmp_migrate_incoming(const char *uri
, Error
**errp
)
1967 Error
*local_err
= NULL
;
1968 static bool once
= true;
1970 if (!deferred_incoming
) {
1971 error_setg(errp
, "For use with '-incoming defer'");
1975 error_setg(errp
, "The incoming migration has already been started");
1979 qemu_start_incoming_migration(uri
, &local_err
);
1982 error_propagate(errp
, local_err
);
1989 void qmp_migrate_recover(const char *uri
, Error
**errp
)
1991 MigrationIncomingState
*mis
= migration_incoming_get_current();
1993 if (mis
->state
!= MIGRATION_STATUS_POSTCOPY_PAUSED
) {
1994 error_setg(errp
, "Migrate recover can only be run "
1995 "when postcopy is paused.");
1999 if (qatomic_cmpxchg(&mis
->postcopy_recover_triggered
,
2000 false, true) == true) {
2001 error_setg(errp
, "Migrate recovery is triggered already");
2006 * Note that this call will never start a real migration; it will
2007 * only re-setup the migration stream and poke existing migration
2008 * to continue using that newly established channel.
2010 qemu_start_incoming_migration(uri
, errp
);
2013 void qmp_migrate_pause(Error
**errp
)
2015 MigrationState
*ms
= migrate_get_current();
2016 MigrationIncomingState
*mis
= migration_incoming_get_current();
2019 if (ms
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
2020 /* Source side, during postcopy */
2021 qemu_mutex_lock(&ms
->qemu_file_lock
);
2022 ret
= qemu_file_shutdown(ms
->to_dst_file
);
2023 qemu_mutex_unlock(&ms
->qemu_file_lock
);
2025 error_setg(errp
, "Failed to pause source migration");
2030 if (mis
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
2031 ret
= qemu_file_shutdown(mis
->from_src_file
);
2033 error_setg(errp
, "Failed to pause destination migration");
2038 error_setg(errp
, "migrate-pause is currently only supported "
2039 "during postcopy-active state");
2042 bool migration_is_blocked(Error
**errp
)
2044 if (qemu_savevm_state_blocked(errp
)) {
2048 if (migration_blockers
) {
2049 error_propagate(errp
, error_copy(migration_blockers
->data
));
2056 /* Returns true if continue to migrate, or false if error detected */
2057 static bool migrate_prepare(MigrationState
*s
, bool blk
, bool blk_inc
,
2058 bool resume
, Error
**errp
)
2060 Error
*local_err
= NULL
;
2063 if (s
->state
!= MIGRATION_STATUS_POSTCOPY_PAUSED
) {
2064 error_setg(errp
, "Cannot resume if there is no "
2065 "paused migration");
2070 * Postcopy recovery won't work well with release-ram
2071 * capability since release-ram will drop the page buffer as
2072 * long as the page is put into the send buffer. So if there
2073 * is a network failure happened, any page buffers that have
2074 * not yet reached the destination VM but have already been
2075 * sent from the source VM will be lost forever. Let's refuse
2076 * the client from resuming such a postcopy migration.
2077 * Luckily release-ram was designed to only be used when src
2078 * and destination VMs are on the same host, so it should be
2081 if (migrate_release_ram()) {
2082 error_setg(errp
, "Postcopy recovery cannot work "
2083 "when release-ram capability is set");
2087 /* This is a resume, skip init status */
2091 if (migration_is_running(s
->state
)) {
2092 error_setg(errp
, QERR_MIGRATION_ACTIVE
);
2096 if (runstate_check(RUN_STATE_INMIGRATE
)) {
2097 error_setg(errp
, "Guest is waiting for an incoming migration");
2101 if (migration_is_blocked(errp
)) {
2105 if (blk
|| blk_inc
) {
2106 if (migrate_use_block() || migrate_use_block_incremental()) {
2107 error_setg(errp
, "Command options are incompatible with "
2108 "current migration capabilities");
2111 migrate_set_block_enabled(true, &local_err
);
2113 error_propagate(errp
, local_err
);
2116 s
->must_remove_block_options
= true;
2120 migrate_set_block_incremental(s
, true);
2125 * set ram_counters memory to zero for a
2128 memset(&ram_counters
, 0, sizeof(ram_counters
));
2133 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
2134 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
2135 bool has_resume
, bool resume
, Error
**errp
)
2137 Error
*local_err
= NULL
;
2138 MigrationState
*s
= migrate_get_current();
2139 const char *p
= NULL
;
2141 if (!migrate_prepare(s
, has_blk
&& blk
, has_inc
&& inc
,
2142 has_resume
&& resume
, errp
)) {
2143 /* Error detected, put into errp */
2147 if (strstart(uri
, "tcp:", &p
) ||
2148 strstart(uri
, "unix:", NULL
) ||
2149 strstart(uri
, "vsock:", NULL
)) {
2150 socket_start_outgoing_migration(s
, p
? p
: uri
, &local_err
);
2152 } else if (strstart(uri
, "rdma:", &p
)) {
2153 rdma_start_outgoing_migration(s
, p
, &local_err
);
2155 } else if (strstart(uri
, "exec:", &p
)) {
2156 exec_start_outgoing_migration(s
, p
, &local_err
);
2157 } else if (strstart(uri
, "fd:", &p
)) {
2158 fd_start_outgoing_migration(s
, p
, &local_err
);
2160 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri",
2161 "a valid migration protocol");
2162 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
2163 MIGRATION_STATUS_FAILED
);
2164 block_cleanup_parameters(s
);
2169 migrate_fd_error(s
, local_err
);
2170 error_propagate(errp
, local_err
);
2175 void qmp_migrate_cancel(Error
**errp
)
2177 migrate_fd_cancel(migrate_get_current());
2180 void qmp_migrate_continue(MigrationStatus state
, Error
**errp
)
2182 MigrationState
*s
= migrate_get_current();
2183 if (s
->state
!= state
) {
2184 error_setg(errp
, "Migration not in expected state: %s",
2185 MigrationStatus_str(s
->state
));
2188 qemu_sem_post(&s
->pause_sem
);
2191 void qmp_migrate_set_cache_size(int64_t value
, Error
**errp
)
2193 MigrateSetParameters p
= {
2194 .has_xbzrle_cache_size
= true,
2195 .xbzrle_cache_size
= value
,
2198 qmp_migrate_set_parameters(&p
, errp
);
2201 int64_t qmp_query_migrate_cache_size(Error
**errp
)
2203 return migrate_xbzrle_cache_size();
2206 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
2208 MigrateSetParameters p
= {
2209 .has_max_bandwidth
= true,
2210 .max_bandwidth
= value
,
2213 qmp_migrate_set_parameters(&p
, errp
);
2216 void qmp_migrate_set_downtime(double value
, Error
**errp
)
2218 if (value
< 0 || value
> MAX_MIGRATE_DOWNTIME_SECONDS
) {
2219 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
2221 "an integer in the range of 0 to "
2222 stringify(MAX_MIGRATE_DOWNTIME_SECONDS
)" seconds");
2226 value
*= 1000; /* Convert to milliseconds */
2228 MigrateSetParameters p
= {
2229 .has_downtime_limit
= true,
2230 .downtime_limit
= (int64_t)value
,
2233 qmp_migrate_set_parameters(&p
, errp
);
2236 bool migrate_release_ram(void)
2240 s
= migrate_get_current();
2242 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_RELEASE_RAM
];
2245 bool migrate_postcopy_ram(void)
2249 s
= migrate_get_current();
2251 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_POSTCOPY_RAM
];
2254 bool migrate_postcopy(void)
2256 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
2259 bool migrate_auto_converge(void)
2263 s
= migrate_get_current();
2265 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_AUTO_CONVERGE
];
2268 bool migrate_zero_blocks(void)
2272 s
= migrate_get_current();
2274 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_ZERO_BLOCKS
];
2277 bool migrate_postcopy_blocktime(void)
2281 s
= migrate_get_current();
2283 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME
];
2286 bool migrate_use_compression(void)
2290 s
= migrate_get_current();
2292 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_COMPRESS
];
2295 int migrate_compress_level(void)
2299 s
= migrate_get_current();
2301 return s
->parameters
.compress_level
;
2304 int migrate_compress_threads(void)
2308 s
= migrate_get_current();
2310 return s
->parameters
.compress_threads
;
2313 int migrate_compress_wait_thread(void)
2317 s
= migrate_get_current();
2319 return s
->parameters
.compress_wait_thread
;
2322 int migrate_decompress_threads(void)
2326 s
= migrate_get_current();
2328 return s
->parameters
.decompress_threads
;
2331 bool migrate_dirty_bitmaps(void)
2335 s
= migrate_get_current();
2337 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_DIRTY_BITMAPS
];
2340 bool migrate_ignore_shared(void)
2344 s
= migrate_get_current();
2346 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_X_IGNORE_SHARED
];
2349 bool migrate_validate_uuid(void)
2353 s
= migrate_get_current();
2355 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_VALIDATE_UUID
];
2358 bool migrate_use_events(void)
2362 s
= migrate_get_current();
2364 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_EVENTS
];
2367 bool migrate_use_multifd(void)
2371 s
= migrate_get_current();
2373 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_MULTIFD
];
2376 bool migrate_pause_before_switchover(void)
2380 s
= migrate_get_current();
2382 return s
->enabled_capabilities
[
2383 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER
];
2386 int migrate_multifd_channels(void)
2390 s
= migrate_get_current();
2392 return s
->parameters
.multifd_channels
;
2395 MultiFDCompression
migrate_multifd_compression(void)
2399 s
= migrate_get_current();
2401 return s
->parameters
.multifd_compression
;
2404 int migrate_multifd_zlib_level(void)
2408 s
= migrate_get_current();
2410 return s
->parameters
.multifd_zlib_level
;
2413 int migrate_multifd_zstd_level(void)
2417 s
= migrate_get_current();
2419 return s
->parameters
.multifd_zstd_level
;
2422 int migrate_use_xbzrle(void)
2426 s
= migrate_get_current();
2428 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_XBZRLE
];
2431 int64_t migrate_xbzrle_cache_size(void)
2435 s
= migrate_get_current();
2437 return s
->parameters
.xbzrle_cache_size
;
2440 static int64_t migrate_max_postcopy_bandwidth(void)
2444 s
= migrate_get_current();
2446 return s
->parameters
.max_postcopy_bandwidth
;
2449 bool migrate_use_block(void)
2453 s
= migrate_get_current();
2455 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_BLOCK
];
2458 bool migrate_use_return_path(void)
2462 s
= migrate_get_current();
2464 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_RETURN_PATH
];
2467 bool migrate_use_block_incremental(void)
2471 s
= migrate_get_current();
2473 return s
->parameters
.block_incremental
;
2476 /* migration thread support */
2478 * Something bad happened to the RP stream, mark an error
2479 * The caller shall print or trace something to indicate why
2481 static void mark_source_rp_bad(MigrationState
*s
)
2483 s
->rp_state
.error
= true;
2486 static struct rp_cmd_args
{
2487 ssize_t len
; /* -1 = variable */
2490 [MIG_RP_MSG_INVALID
] = { .len
= -1, .name
= "INVALID" },
2491 [MIG_RP_MSG_SHUT
] = { .len
= 4, .name
= "SHUT" },
2492 [MIG_RP_MSG_PONG
] = { .len
= 4, .name
= "PONG" },
2493 [MIG_RP_MSG_REQ_PAGES
] = { .len
= 12, .name
= "REQ_PAGES" },
2494 [MIG_RP_MSG_REQ_PAGES_ID
] = { .len
= -1, .name
= "REQ_PAGES_ID" },
2495 [MIG_RP_MSG_RECV_BITMAP
] = { .len
= -1, .name
= "RECV_BITMAP" },
2496 [MIG_RP_MSG_RESUME_ACK
] = { .len
= 4, .name
= "RESUME_ACK" },
2497 [MIG_RP_MSG_MAX
] = { .len
= -1, .name
= "MAX" },
2501 * Process a request for pages received on the return path,
2502 * We're allowed to send more than requested (e.g. to round to our page size)
2503 * and we don't need to send pages that have already been sent.
2505 static void migrate_handle_rp_req_pages(MigrationState
*ms
, const char* rbname
,
2506 ram_addr_t start
, size_t len
)
2508 long our_host_ps
= qemu_real_host_page_size
;
2510 trace_migrate_handle_rp_req_pages(rbname
, start
, len
);
2513 * Since we currently insist on matching page sizes, just sanity check
2514 * we're being asked for whole host pages.
2516 if (start
& (our_host_ps
- 1) ||
2517 (len
& (our_host_ps
- 1))) {
2518 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
2519 " len: %zd", __func__
, start
, len
);
2520 mark_source_rp_bad(ms
);
2524 if (ram_save_queue_pages(rbname
, start
, len
)) {
2525 mark_source_rp_bad(ms
);
2529 /* Return true to retry, false to quit */
2530 static bool postcopy_pause_return_path_thread(MigrationState
*s
)
2532 trace_postcopy_pause_return_path();
2534 qemu_sem_wait(&s
->postcopy_pause_rp_sem
);
2536 trace_postcopy_pause_return_path_continued();
2541 static int migrate_handle_rp_recv_bitmap(MigrationState
*s
, char *block_name
)
2543 RAMBlock
*block
= qemu_ram_block_by_name(block_name
);
2546 error_report("%s: invalid block name '%s'", __func__
, block_name
);
2550 /* Fetch the received bitmap and refresh the dirty bitmap */
2551 return ram_dirty_bitmap_reload(s
, block
);
2554 static int migrate_handle_rp_resume_ack(MigrationState
*s
, uint32_t value
)
2556 trace_source_return_path_thread_resume_ack(value
);
2558 if (value
!= MIGRATION_RESUME_ACK_VALUE
) {
2559 error_report("%s: illegal resume_ack value %"PRIu32
,
2564 /* Now both sides are active. */
2565 migrate_set_state(&s
->state
, MIGRATION_STATUS_POSTCOPY_RECOVER
,
2566 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
2568 /* Notify send thread that time to continue send pages */
2569 qemu_sem_post(&s
->rp_state
.rp_sem
);
2575 * Handles messages sent on the return path towards the source VM
2578 static void *source_return_path_thread(void *opaque
)
2580 MigrationState
*ms
= opaque
;
2581 QEMUFile
*rp
= ms
->rp_state
.from_dst_file
;
2582 uint16_t header_len
, header_type
;
2584 uint32_t tmp32
, sibling_error
;
2585 ram_addr_t start
= 0; /* =0 to silence warning */
2586 size_t len
= 0, expected_len
;
2589 trace_source_return_path_thread_entry();
2590 rcu_register_thread();
2593 while (!ms
->rp_state
.error
&& !qemu_file_get_error(rp
) &&
2594 migration_is_setup_or_active(ms
->state
)) {
2595 trace_source_return_path_thread_loop_top();
2596 header_type
= qemu_get_be16(rp
);
2597 header_len
= qemu_get_be16(rp
);
2599 if (qemu_file_get_error(rp
)) {
2600 mark_source_rp_bad(ms
);
2604 if (header_type
>= MIG_RP_MSG_MAX
||
2605 header_type
== MIG_RP_MSG_INVALID
) {
2606 error_report("RP: Received invalid message 0x%04x length 0x%04x",
2607 header_type
, header_len
);
2608 mark_source_rp_bad(ms
);
2612 if ((rp_cmd_args
[header_type
].len
!= -1 &&
2613 header_len
!= rp_cmd_args
[header_type
].len
) ||
2614 header_len
> sizeof(buf
)) {
2615 error_report("RP: Received '%s' message (0x%04x) with"
2616 "incorrect length %d expecting %zu",
2617 rp_cmd_args
[header_type
].name
, header_type
, header_len
,
2618 (size_t)rp_cmd_args
[header_type
].len
);
2619 mark_source_rp_bad(ms
);
2623 /* We know we've got a valid header by this point */
2624 res
= qemu_get_buffer(rp
, buf
, header_len
);
2625 if (res
!= header_len
) {
2626 error_report("RP: Failed reading data for message 0x%04x"
2627 " read %d expected %d",
2628 header_type
, res
, header_len
);
2629 mark_source_rp_bad(ms
);
2633 /* OK, we have the message and the data */
2634 switch (header_type
) {
2635 case MIG_RP_MSG_SHUT
:
2636 sibling_error
= ldl_be_p(buf
);
2637 trace_source_return_path_thread_shut(sibling_error
);
2638 if (sibling_error
) {
2639 error_report("RP: Sibling indicated error %d", sibling_error
);
2640 mark_source_rp_bad(ms
);
2643 * We'll let the main thread deal with closing the RP
2644 * we could do a shutdown(2) on it, but we're the only user
2645 * anyway, so there's nothing gained.
2649 case MIG_RP_MSG_PONG
:
2650 tmp32
= ldl_be_p(buf
);
2651 trace_source_return_path_thread_pong(tmp32
);
2654 case MIG_RP_MSG_REQ_PAGES
:
2655 start
= ldq_be_p(buf
);
2656 len
= ldl_be_p(buf
+ 8);
2657 migrate_handle_rp_req_pages(ms
, NULL
, start
, len
);
2660 case MIG_RP_MSG_REQ_PAGES_ID
:
2661 expected_len
= 12 + 1; /* header + termination */
2663 if (header_len
>= expected_len
) {
2664 start
= ldq_be_p(buf
);
2665 len
= ldl_be_p(buf
+ 8);
2666 /* Now we expect an idstr */
2667 tmp32
= buf
[12]; /* Length of the following idstr */
2668 buf
[13 + tmp32
] = '\0';
2669 expected_len
+= tmp32
;
2671 if (header_len
!= expected_len
) {
2672 error_report("RP: Req_Page_id with length %d expecting %zd",
2673 header_len
, expected_len
);
2674 mark_source_rp_bad(ms
);
2677 migrate_handle_rp_req_pages(ms
, (char *)&buf
[13], start
, len
);
2680 case MIG_RP_MSG_RECV_BITMAP
:
2681 if (header_len
< 1) {
2682 error_report("%s: missing block name", __func__
);
2683 mark_source_rp_bad(ms
);
2686 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2687 buf
[buf
[0] + 1] = '\0';
2688 if (migrate_handle_rp_recv_bitmap(ms
, (char *)(buf
+ 1))) {
2689 mark_source_rp_bad(ms
);
2694 case MIG_RP_MSG_RESUME_ACK
:
2695 tmp32
= ldl_be_p(buf
);
2696 if (migrate_handle_rp_resume_ack(ms
, tmp32
)) {
2697 mark_source_rp_bad(ms
);
2708 res
= qemu_file_get_error(rp
);
2710 if (res
== -EIO
&& migration_in_postcopy()) {
2712 * Maybe there is something we can do: it looks like a
2713 * network down issue, and we pause for a recovery.
2715 if (postcopy_pause_return_path_thread(ms
)) {
2716 /* Reload rp, reset the rest */
2717 if (rp
!= ms
->rp_state
.from_dst_file
) {
2719 rp
= ms
->rp_state
.from_dst_file
;
2721 ms
->rp_state
.error
= false;
2726 trace_source_return_path_thread_bad_end();
2727 mark_source_rp_bad(ms
);
2730 trace_source_return_path_thread_end();
2731 ms
->rp_state
.from_dst_file
= NULL
;
2733 rcu_unregister_thread();
2737 static int open_return_path_on_source(MigrationState
*ms
,
2741 ms
->rp_state
.from_dst_file
= qemu_file_get_return_path(ms
->to_dst_file
);
2742 if (!ms
->rp_state
.from_dst_file
) {
2746 trace_open_return_path_on_source();
2748 if (!create_thread
) {
2753 qemu_thread_create(&ms
->rp_state
.rp_thread
, "return path",
2754 source_return_path_thread
, ms
, QEMU_THREAD_JOINABLE
);
2756 trace_open_return_path_on_source_continue();
2761 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2762 static int await_return_path_close_on_source(MigrationState
*ms
)
2765 * If this is a normal exit then the destination will send a SHUT and the
2766 * rp_thread will exit, however if there's an error we need to cause
2769 if (qemu_file_get_error(ms
->to_dst_file
) && ms
->rp_state
.from_dst_file
) {
2771 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2772 * waiting for the destination.
2774 qemu_file_shutdown(ms
->rp_state
.from_dst_file
);
2775 mark_source_rp_bad(ms
);
2777 trace_await_return_path_close_on_source_joining();
2778 qemu_thread_join(&ms
->rp_state
.rp_thread
);
2779 trace_await_return_path_close_on_source_close();
2780 return ms
->rp_state
.error
;
2784 * Switch from normal iteration to postcopy
2785 * Returns non-0 on error
2787 static int postcopy_start(MigrationState
*ms
)
2790 QIOChannelBuffer
*bioc
;
2792 int64_t time_at_stop
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
2793 int64_t bandwidth
= migrate_max_postcopy_bandwidth();
2794 bool restart_block
= false;
2795 int cur_state
= MIGRATION_STATUS_ACTIVE
;
2796 if (!migrate_pause_before_switchover()) {
2797 migrate_set_state(&ms
->state
, MIGRATION_STATUS_ACTIVE
,
2798 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
2801 trace_postcopy_start();
2802 qemu_mutex_lock_iothread();
2803 trace_postcopy_start_set_run();
2805 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, NULL
);
2806 global_state_store();
2807 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
2812 ret
= migration_maybe_pause(ms
, &cur_state
,
2813 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
2818 ret
= bdrv_inactivate_all();
2822 restart_block
= true;
2825 * Cause any non-postcopiable, but iterative devices to
2826 * send out their final data.
2828 qemu_savevm_state_complete_precopy(ms
->to_dst_file
, true, false);
2831 * in Finish migrate and with the io-lock held everything should
2832 * be quiet, but we've potentially still got dirty pages and we
2833 * need to tell the destination to throw any pages it's already received
2836 if (migrate_postcopy_ram()) {
2837 if (ram_postcopy_send_discard_bitmap(ms
)) {
2838 error_report("postcopy send discard bitmap failed");
2844 * send rest of state - note things that are doing postcopy
2845 * will notice we're in POSTCOPY_ACTIVE and not actually
2846 * wrap their state up here
2848 /* 0 max-postcopy-bandwidth means unlimited */
2850 qemu_file_set_rate_limit(ms
->to_dst_file
, INT64_MAX
);
2852 qemu_file_set_rate_limit(ms
->to_dst_file
, bandwidth
/ XFER_LIMIT_RATIO
);
2854 if (migrate_postcopy_ram()) {
2855 /* Ping just for debugging, helps line traces up */
2856 qemu_savevm_send_ping(ms
->to_dst_file
, 2);
2860 * While loading the device state we may trigger page transfer
2861 * requests and the fd must be free to process those, and thus
2862 * the destination must read the whole device state off the fd before
2863 * it starts processing it. Unfortunately the ad-hoc migration format
2864 * doesn't allow the destination to know the size to read without fully
2865 * parsing it through each devices load-state code (especially the open
2866 * coded devices that use get/put).
2867 * So we wrap the device state up in a package with a length at the start;
2868 * to do this we use a qemu_buf to hold the whole of the device state.
2870 bioc
= qio_channel_buffer_new(4096);
2871 qio_channel_set_name(QIO_CHANNEL(bioc
), "migration-postcopy-buffer");
2872 fb
= qemu_fopen_channel_output(QIO_CHANNEL(bioc
));
2873 object_unref(OBJECT(bioc
));
2876 * Make sure the receiver can get incoming pages before we send the rest
2879 qemu_savevm_send_postcopy_listen(fb
);
2881 qemu_savevm_state_complete_precopy(fb
, false, false);
2882 if (migrate_postcopy_ram()) {
2883 qemu_savevm_send_ping(fb
, 3);
2886 qemu_savevm_send_postcopy_run(fb
);
2888 /* <><> end of stuff going into the package */
2890 /* Last point of recovery; as soon as we send the package the destination
2891 * can open devices and potentially start running.
2892 * Lets just check again we've not got any errors.
2894 ret
= qemu_file_get_error(ms
->to_dst_file
);
2896 error_report("postcopy_start: Migration stream errored (pre package)");
2900 restart_block
= false;
2902 /* Now send that blob */
2903 if (qemu_savevm_send_packaged(ms
->to_dst_file
, bioc
->data
, bioc
->usage
)) {
2908 /* Send a notify to give a chance for anything that needs to happen
2909 * at the transition to postcopy and after the device state; in particular
2910 * spice needs to trigger a transition now
2912 ms
->postcopy_after_devices
= true;
2913 notifier_list_notify(&migration_state_notifiers
, ms
);
2915 ms
->downtime
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) - time_at_stop
;
2917 qemu_mutex_unlock_iothread();
2919 if (migrate_postcopy_ram()) {
2921 * Although this ping is just for debug, it could potentially be
2922 * used for getting a better measurement of downtime at the source.
2924 qemu_savevm_send_ping(ms
->to_dst_file
, 4);
2927 if (migrate_release_ram()) {
2928 ram_postcopy_migrated_memory_release(ms
);
2931 ret
= qemu_file_get_error(ms
->to_dst_file
);
2933 error_report("postcopy_start: Migration stream errored");
2934 migrate_set_state(&ms
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
2935 MIGRATION_STATUS_FAILED
);
2943 migrate_set_state(&ms
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
2944 MIGRATION_STATUS_FAILED
);
2945 if (restart_block
) {
2946 /* A failure happened early enough that we know the destination hasn't
2947 * accessed block devices, so we're safe to recover.
2949 Error
*local_err
= NULL
;
2951 bdrv_invalidate_cache_all(&local_err
);
2953 error_report_err(local_err
);
2956 qemu_mutex_unlock_iothread();
2961 * migration_maybe_pause: Pause if required to by
2962 * migrate_pause_before_switchover called with the iothread locked
2963 * Returns: 0 on success
2965 static int migration_maybe_pause(MigrationState
*s
,
2966 int *current_active_state
,
2969 if (!migrate_pause_before_switchover()) {
2973 /* Since leaving this state is not atomic with posting the semaphore
2974 * it's possible that someone could have issued multiple migrate_continue
2975 * and the semaphore is incorrectly positive at this point;
2976 * the docs say it's undefined to reinit a semaphore that's already
2977 * init'd, so use timedwait to eat up any existing posts.
2979 while (qemu_sem_timedwait(&s
->pause_sem
, 1) == 0) {
2980 /* This block intentionally left blank */
2984 * If the migration is cancelled when it is in the completion phase,
2985 * the migration state is set to MIGRATION_STATUS_CANCELLING.
2986 * So we don't need to wait a semaphore, otherwise we would always
2987 * wait for the 'pause_sem' semaphore.
2989 if (s
->state
!= MIGRATION_STATUS_CANCELLING
) {
2990 qemu_mutex_unlock_iothread();
2991 migrate_set_state(&s
->state
, *current_active_state
,
2992 MIGRATION_STATUS_PRE_SWITCHOVER
);
2993 qemu_sem_wait(&s
->pause_sem
);
2994 migrate_set_state(&s
->state
, MIGRATION_STATUS_PRE_SWITCHOVER
,
2996 *current_active_state
= new_state
;
2997 qemu_mutex_lock_iothread();
3000 return s
->state
== new_state
? 0 : -EINVAL
;
3004 * migration_completion: Used by migration_thread when there's not much left.
3005 * The caller 'breaks' the loop when this returns.
3007 * @s: Current migration state
3009 static void migration_completion(MigrationState
*s
)
3012 int current_active_state
= s
->state
;
3014 if (s
->state
== MIGRATION_STATUS_ACTIVE
) {
3015 qemu_mutex_lock_iothread();
3016 s
->downtime_start
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
3017 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, NULL
);
3018 s
->vm_was_running
= runstate_is_running();
3019 ret
= global_state_store();
3022 bool inactivate
= !migrate_colo_enabled();
3023 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
3025 ret
= migration_maybe_pause(s
, ¤t_active_state
,
3026 MIGRATION_STATUS_DEVICE
);
3029 qemu_file_set_rate_limit(s
->to_dst_file
, INT64_MAX
);
3030 ret
= qemu_savevm_state_complete_precopy(s
->to_dst_file
, false,
3033 if (inactivate
&& ret
>= 0) {
3034 s
->block_inactive
= true;
3037 qemu_mutex_unlock_iothread();
3042 } else if (s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
3043 trace_migration_completion_postcopy_end();
3045 qemu_savevm_state_complete_postcopy(s
->to_dst_file
);
3046 trace_migration_completion_postcopy_end_after_complete();
3050 * If rp was opened we must clean up the thread before
3051 * cleaning everything else up (since if there are no failures
3052 * it will wait for the destination to send it's status in
3055 if (s
->rp_state
.from_dst_file
) {
3057 trace_migration_return_path_end_before();
3058 rp_error
= await_return_path_close_on_source(s
);
3059 trace_migration_return_path_end_after(rp_error
);
3061 goto fail_invalidate
;
3065 if (qemu_file_get_error(s
->to_dst_file
)) {
3066 trace_migration_completion_file_err();
3067 goto fail_invalidate
;
3070 if (!migrate_colo_enabled()) {
3071 migrate_set_state(&s
->state
, current_active_state
,
3072 MIGRATION_STATUS_COMPLETED
);
3078 /* If not doing postcopy, vm_start() will be called: let's regain
3079 * control on images.
3081 if (s
->state
== MIGRATION_STATUS_ACTIVE
||
3082 s
->state
== MIGRATION_STATUS_DEVICE
) {
3083 Error
*local_err
= NULL
;
3085 qemu_mutex_lock_iothread();
3086 bdrv_invalidate_cache_all(&local_err
);
3088 error_report_err(local_err
);
3090 s
->block_inactive
= false;
3092 qemu_mutex_unlock_iothread();
3096 migrate_set_state(&s
->state
, current_active_state
,
3097 MIGRATION_STATUS_FAILED
);
3100 bool migrate_colo_enabled(void)
3102 MigrationState
*s
= migrate_get_current();
3103 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_X_COLO
];
3106 typedef enum MigThrError
{
3107 /* No error detected */
3108 MIG_THR_ERR_NONE
= 0,
3109 /* Detected error, but resumed successfully */
3110 MIG_THR_ERR_RECOVERED
= 1,
3111 /* Detected fatal error, need to exit */
3112 MIG_THR_ERR_FATAL
= 2,
3115 static int postcopy_resume_handshake(MigrationState
*s
)
3117 qemu_savevm_send_postcopy_resume(s
->to_dst_file
);
3119 while (s
->state
== MIGRATION_STATUS_POSTCOPY_RECOVER
) {
3120 qemu_sem_wait(&s
->rp_state
.rp_sem
);
3123 if (s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
3130 /* Return zero if success, or <0 for error */
3131 static int postcopy_do_resume(MigrationState
*s
)
3136 * Call all the resume_prepare() hooks, so that modules can be
3137 * ready for the migration resume.
3139 ret
= qemu_savevm_state_resume_prepare(s
);
3141 error_report("%s: resume_prepare() failure detected: %d",
3147 * Last handshake with destination on the resume (destination will
3148 * switch to postcopy-active afterwards)
3150 ret
= postcopy_resume_handshake(s
);
3152 error_report("%s: handshake failed: %d", __func__
, ret
);
3160 * We don't return until we are in a safe state to continue current
3161 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
3162 * MIG_THR_ERR_FATAL if unrecovery failure happened.
3164 static MigThrError
postcopy_pause(MigrationState
*s
)
3166 assert(s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
);
3171 /* Current channel is possibly broken. Release it. */
3172 assert(s
->to_dst_file
);
3173 qemu_mutex_lock(&s
->qemu_file_lock
);
3174 file
= s
->to_dst_file
;
3175 s
->to_dst_file
= NULL
;
3176 qemu_mutex_unlock(&s
->qemu_file_lock
);
3178 qemu_file_shutdown(file
);
3181 migrate_set_state(&s
->state
, s
->state
,
3182 MIGRATION_STATUS_POSTCOPY_PAUSED
);
3184 error_report("Detected IO failure for postcopy. "
3185 "Migration paused.");
3188 * We wait until things fixed up. Then someone will setup the
3189 * status back for us.
3191 while (s
->state
== MIGRATION_STATUS_POSTCOPY_PAUSED
) {
3192 qemu_sem_wait(&s
->postcopy_pause_sem
);
3195 if (s
->state
== MIGRATION_STATUS_POSTCOPY_RECOVER
) {
3196 /* Woken up by a recover procedure. Give it a shot */
3199 * Firstly, let's wake up the return path now, with a new
3200 * return path channel.
3202 qemu_sem_post(&s
->postcopy_pause_rp_sem
);
3204 /* Do the resume logic */
3205 if (postcopy_do_resume(s
) == 0) {
3206 /* Let's continue! */
3207 trace_postcopy_pause_continued();
3208 return MIG_THR_ERR_RECOVERED
;
3211 * Something wrong happened during the recovery, let's
3212 * pause again. Pause is always better than throwing
3218 /* This is not right... Time to quit. */
3219 return MIG_THR_ERR_FATAL
;
3224 static MigThrError
migration_detect_error(MigrationState
*s
)
3227 int state
= s
->state
;
3228 Error
*local_error
= NULL
;
3230 if (state
== MIGRATION_STATUS_CANCELLING
||
3231 state
== MIGRATION_STATUS_CANCELLED
) {
3232 /* End the migration, but don't set the state to failed */
3233 return MIG_THR_ERR_FATAL
;
3236 /* Try to detect any file errors */
3237 ret
= qemu_file_get_error_obj(s
->to_dst_file
, &local_error
);
3239 /* Everything is fine */
3240 assert(!local_error
);
3241 return MIG_THR_ERR_NONE
;
3245 migrate_set_error(s
, local_error
);
3246 error_free(local_error
);
3249 if (state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
&& ret
== -EIO
) {
3251 * For postcopy, we allow the network to be down for a
3252 * while. After that, it can be continued by a
3255 return postcopy_pause(s
);
3258 * For precopy (or postcopy with error outside IO), we fail
3261 migrate_set_state(&s
->state
, state
, MIGRATION_STATUS_FAILED
);
3262 trace_migration_thread_file_err();
3264 /* Time to stop the migration, now. */
3265 return MIG_THR_ERR_FATAL
;
3269 /* How many bytes have we transferred since the beginning of the migration */
3270 static uint64_t migration_total_bytes(MigrationState
*s
)
3272 return qemu_ftell(s
->to_dst_file
) + ram_counters
.multifd_bytes
;
3275 static void migration_calculate_complete(MigrationState
*s
)
3277 uint64_t bytes
= migration_total_bytes(s
);
3278 int64_t end_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
3279 int64_t transfer_time
;
3281 s
->total_time
= end_time
- s
->start_time
;
3284 * It's still not set, so we are precopy migration. For
3285 * postcopy, downtime is calculated during postcopy_start().
3287 s
->downtime
= end_time
- s
->downtime_start
;
3290 transfer_time
= s
->total_time
- s
->setup_time
;
3291 if (transfer_time
) {
3292 s
->mbps
= ((double) bytes
* 8.0) / transfer_time
/ 1000;
3296 static void update_iteration_initial_status(MigrationState
*s
)
3299 * Update these three fields at the same time to avoid mismatch info lead
3300 * wrong speed calculation.
3302 s
->iteration_start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
3303 s
->iteration_initial_bytes
= migration_total_bytes(s
);
3304 s
->iteration_initial_pages
= ram_get_total_transferred_pages();
3307 static void migration_update_counters(MigrationState
*s
,
3308 int64_t current_time
)
3310 uint64_t transferred
, transferred_pages
, time_spent
;
3311 uint64_t current_bytes
; /* bytes transferred since the beginning */
3314 if (current_time
< s
->iteration_start_time
+ BUFFER_DELAY
) {
3318 current_bytes
= migration_total_bytes(s
);
3319 transferred
= current_bytes
- s
->iteration_initial_bytes
;
3320 time_spent
= current_time
- s
->iteration_start_time
;
3321 bandwidth
= (double)transferred
/ time_spent
;
3322 s
->threshold_size
= bandwidth
* s
->parameters
.downtime_limit
;
3324 s
->mbps
= (((double) transferred
* 8.0) /
3325 ((double) time_spent
/ 1000.0)) / 1000.0 / 1000.0;
3327 transferred_pages
= ram_get_total_transferred_pages() -
3328 s
->iteration_initial_pages
;
3329 s
->pages_per_second
= (double) transferred_pages
/
3330 (((double) time_spent
/ 1000.0));
3333 * if we haven't sent anything, we don't want to
3334 * recalculate. 10000 is a small enough number for our purposes
3336 if (ram_counters
.dirty_pages_rate
&& transferred
> 10000) {
3337 s
->expected_downtime
= ram_counters
.remaining
/ bandwidth
;
3340 qemu_file_reset_rate_limit(s
->to_dst_file
);
3342 update_iteration_initial_status(s
);
3344 trace_migrate_transferred(transferred
, time_spent
,
3345 bandwidth
, s
->threshold_size
);
3348 /* Migration thread iteration status */
3350 MIG_ITERATE_RESUME
, /* Resume current iteration */
3351 MIG_ITERATE_SKIP
, /* Skip current iteration */
3352 MIG_ITERATE_BREAK
, /* Break the loop */
3356 * Return true if continue to the next iteration directly, false
3359 static MigIterateState
migration_iteration_run(MigrationState
*s
)
3361 uint64_t pending_size
, pend_pre
, pend_compat
, pend_post
;
3362 bool in_postcopy
= s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
;
3364 qemu_savevm_state_pending(s
->to_dst_file
, s
->threshold_size
, &pend_pre
,
3365 &pend_compat
, &pend_post
);
3366 pending_size
= pend_pre
+ pend_compat
+ pend_post
;
3368 trace_migrate_pending(pending_size
, s
->threshold_size
,
3369 pend_pre
, pend_compat
, pend_post
);
3371 if (pending_size
&& pending_size
>= s
->threshold_size
) {
3372 /* Still a significant amount to transfer */
3373 if (!in_postcopy
&& pend_pre
<= s
->threshold_size
&&
3374 qatomic_read(&s
->start_postcopy
)) {
3375 if (postcopy_start(s
)) {
3376 error_report("%s: postcopy failed to start", __func__
);
3378 return MIG_ITERATE_SKIP
;
3380 /* Just another iteration step */
3381 qemu_savevm_state_iterate(s
->to_dst_file
, in_postcopy
);
3383 trace_migration_thread_low_pending(pending_size
);
3384 migration_completion(s
);
3385 return MIG_ITERATE_BREAK
;
3388 return MIG_ITERATE_RESUME
;
3391 static void migration_iteration_finish(MigrationState
*s
)
3393 /* If we enabled cpu throttling for auto-converge, turn it off. */
3394 cpu_throttle_stop();
3396 qemu_mutex_lock_iothread();
3398 case MIGRATION_STATUS_COMPLETED
:
3399 migration_calculate_complete(s
);
3400 runstate_set(RUN_STATE_POSTMIGRATE
);
3403 case MIGRATION_STATUS_ACTIVE
:
3405 * We should really assert here, but since it's during
3406 * migration, let's try to reduce the usage of assertions.
3408 if (!migrate_colo_enabled()) {
3409 error_report("%s: critical error: calling COLO code without "
3410 "COLO enabled", __func__
);
3412 migrate_start_colo_process(s
);
3414 * Fixme: we will run VM in COLO no matter its old running state.
3415 * After exited COLO, we will keep running.
3417 s
->vm_was_running
= true;
3419 case MIGRATION_STATUS_FAILED
:
3420 case MIGRATION_STATUS_CANCELLED
:
3421 case MIGRATION_STATUS_CANCELLING
:
3422 if (s
->vm_was_running
) {
3425 if (runstate_check(RUN_STATE_FINISH_MIGRATE
)) {
3426 runstate_set(RUN_STATE_POSTMIGRATE
);
3432 /* Should not reach here, but if so, forgive the VM. */
3433 error_report("%s: Unknown ending state %d", __func__
, s
->state
);
3436 migrate_fd_cleanup_schedule(s
);
3437 qemu_mutex_unlock_iothread();
3440 void migration_make_urgent_request(void)
3442 qemu_sem_post(&migrate_get_current()->rate_limit_sem
);
3445 void migration_consume_urgent_request(void)
3447 qemu_sem_wait(&migrate_get_current()->rate_limit_sem
);
3450 /* Returns true if the rate limiting was broken by an urgent request */
3451 bool migration_rate_limit(void)
3453 int64_t now
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
3454 MigrationState
*s
= migrate_get_current();
3456 bool urgent
= false;
3457 migration_update_counters(s
, now
);
3458 if (qemu_file_rate_limit(s
->to_dst_file
)) {
3460 if (qemu_file_get_error(s
->to_dst_file
)) {
3464 * Wait for a delay to do rate limiting OR
3465 * something urgent to post the semaphore.
3467 int ms
= s
->iteration_start_time
+ BUFFER_DELAY
- now
;
3468 trace_migration_rate_limit_pre(ms
);
3469 if (qemu_sem_timedwait(&s
->rate_limit_sem
, ms
) == 0) {
3471 * We were woken by one or more urgent things but
3472 * the timedwait will have consumed one of them.
3473 * The service routine for the urgent wake will dec
3474 * the semaphore itself for each item it consumes,
3475 * so add this one we just eat back.
3477 qemu_sem_post(&s
->rate_limit_sem
);
3480 trace_migration_rate_limit_post(urgent
);
3486 * Master migration thread on the source VM.
3487 * It drives the migration and pumps the data down the outgoing channel.
3489 static void *migration_thread(void *opaque
)
3491 MigrationState
*s
= opaque
;
3492 int64_t setup_start
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
3493 MigThrError thr_error
;
3494 bool urgent
= false;
3496 rcu_register_thread();
3498 object_ref(OBJECT(s
));
3499 update_iteration_initial_status(s
);
3501 qemu_savevm_state_header(s
->to_dst_file
);
3504 * If we opened the return path, we need to make sure dst has it
3507 if (s
->rp_state
.from_dst_file
) {
3508 /* Now tell the dest that it should open its end so it can reply */
3509 qemu_savevm_send_open_return_path(s
->to_dst_file
);
3511 /* And do a ping that will make stuff easier to debug */
3512 qemu_savevm_send_ping(s
->to_dst_file
, 1);
3515 if (migrate_postcopy()) {
3517 * Tell the destination that we *might* want to do postcopy later;
3518 * if the other end can't do postcopy it should fail now, nice and
3521 qemu_savevm_send_postcopy_advise(s
->to_dst_file
);
3524 if (migrate_colo_enabled()) {
3525 /* Notify migration destination that we enable COLO */
3526 qemu_savevm_send_colo_enable(s
->to_dst_file
);
3529 qemu_savevm_state_setup(s
->to_dst_file
);
3531 if (qemu_savevm_state_guest_unplug_pending()) {
3532 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
3533 MIGRATION_STATUS_WAIT_UNPLUG
);
3535 while (s
->state
== MIGRATION_STATUS_WAIT_UNPLUG
&&
3536 qemu_savevm_state_guest_unplug_pending()) {
3537 qemu_sem_timedwait(&s
->wait_unplug_sem
, 250);
3540 migrate_set_state(&s
->state
, MIGRATION_STATUS_WAIT_UNPLUG
,
3541 MIGRATION_STATUS_ACTIVE
);
3544 s
->setup_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
) - setup_start
;
3545 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
3546 MIGRATION_STATUS_ACTIVE
);
3548 trace_migration_thread_setup_complete();
3550 while (migration_is_active(s
)) {
3551 if (urgent
|| !qemu_file_rate_limit(s
->to_dst_file
)) {
3552 MigIterateState iter_state
= migration_iteration_run(s
);
3553 if (iter_state
== MIG_ITERATE_SKIP
) {
3555 } else if (iter_state
== MIG_ITERATE_BREAK
) {
3561 * Try to detect any kind of failures, and see whether we
3562 * should stop the migration now.
3564 thr_error
= migration_detect_error(s
);
3565 if (thr_error
== MIG_THR_ERR_FATAL
) {
3566 /* Stop migration */
3568 } else if (thr_error
== MIG_THR_ERR_RECOVERED
) {
3570 * Just recovered from a e.g. network failure, reset all
3571 * the local variables. This is important to avoid
3572 * breaking transferred_bytes and bandwidth calculation
3574 update_iteration_initial_status(s
);
3577 urgent
= migration_rate_limit();
3580 trace_migration_thread_after_loop();
3581 migration_iteration_finish(s
);
3582 object_unref(OBJECT(s
));
3583 rcu_unregister_thread();
3587 void migrate_fd_connect(MigrationState
*s
, Error
*error_in
)
3589 Error
*local_err
= NULL
;
3591 bool resume
= s
->state
== MIGRATION_STATUS_POSTCOPY_PAUSED
;
3593 s
->expected_downtime
= s
->parameters
.downtime_limit
;
3595 assert(s
->cleanup_bh
);
3597 assert(!s
->cleanup_bh
);
3598 s
->cleanup_bh
= qemu_bh_new(migrate_fd_cleanup_bh
, s
);
3601 migrate_fd_error(s
, error_in
);
3602 migrate_fd_cleanup(s
);
3607 /* This is a resumed migration */
3608 rate_limit
= s
->parameters
.max_postcopy_bandwidth
/
3611 /* This is a fresh new migration */
3612 rate_limit
= s
->parameters
.max_bandwidth
/ XFER_LIMIT_RATIO
;
3614 /* Notify before starting migration thread */
3615 notifier_list_notify(&migration_state_notifiers
, s
);
3618 qemu_file_set_rate_limit(s
->to_dst_file
, rate_limit
);
3619 qemu_file_set_blocking(s
->to_dst_file
, true);
3622 * Open the return path. For postcopy, it is used exclusively. For
3623 * precopy, only if user specified "return-path" capability would
3624 * QEMU uses the return path.
3626 if (migrate_postcopy_ram() || migrate_use_return_path()) {
3627 if (open_return_path_on_source(s
, !resume
)) {
3628 error_report("Unable to open return-path for postcopy");
3629 migrate_set_state(&s
->state
, s
->state
, MIGRATION_STATUS_FAILED
);
3630 migrate_fd_cleanup(s
);
3636 /* Wakeup the main migration thread to do the recovery */
3637 migrate_set_state(&s
->state
, MIGRATION_STATUS_POSTCOPY_PAUSED
,
3638 MIGRATION_STATUS_POSTCOPY_RECOVER
);
3639 qemu_sem_post(&s
->postcopy_pause_sem
);
3643 if (multifd_save_setup(&local_err
) != 0) {
3644 error_report_err(local_err
);
3645 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
3646 MIGRATION_STATUS_FAILED
);
3647 migrate_fd_cleanup(s
);
3650 qemu_thread_create(&s
->thread
, "live_migration", migration_thread
, s
,
3651 QEMU_THREAD_JOINABLE
);
3652 s
->migration_thread_running
= true;
3655 void migration_global_dump(Monitor
*mon
)
3657 MigrationState
*ms
= migrate_get_current();
3659 monitor_printf(mon
, "globals:\n");
3660 monitor_printf(mon
, "store-global-state: %s\n",
3661 ms
->store_global_state
? "on" : "off");
3662 monitor_printf(mon
, "only-migratable: %s\n",
3663 only_migratable
? "on" : "off");
3664 monitor_printf(mon
, "send-configuration: %s\n",
3665 ms
->send_configuration
? "on" : "off");
3666 monitor_printf(mon
, "send-section-footer: %s\n",
3667 ms
->send_section_footer
? "on" : "off");
3668 monitor_printf(mon
, "decompress-error-check: %s\n",
3669 ms
->decompress_error_check
? "on" : "off");
3670 monitor_printf(mon
, "clear-bitmap-shift: %u\n",
3671 ms
->clear_bitmap_shift
);
3674 #define DEFINE_PROP_MIG_CAP(name, x) \
3675 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
3677 static Property migration_properties
[] = {
3678 DEFINE_PROP_BOOL("store-global-state", MigrationState
,
3679 store_global_state
, true),
3680 DEFINE_PROP_BOOL("send-configuration", MigrationState
,
3681 send_configuration
, true),
3682 DEFINE_PROP_BOOL("send-section-footer", MigrationState
,
3683 send_section_footer
, true),
3684 DEFINE_PROP_BOOL("decompress-error-check", MigrationState
,
3685 decompress_error_check
, true),
3686 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState
,
3687 clear_bitmap_shift
, CLEAR_BITMAP_SHIFT_DEFAULT
),
3689 /* Migration parameters */
3690 DEFINE_PROP_UINT8("x-compress-level", MigrationState
,
3691 parameters
.compress_level
,
3692 DEFAULT_MIGRATE_COMPRESS_LEVEL
),
3693 DEFINE_PROP_UINT8("x-compress-threads", MigrationState
,
3694 parameters
.compress_threads
,
3695 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT
),
3696 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState
,
3697 parameters
.compress_wait_thread
, true),
3698 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState
,
3699 parameters
.decompress_threads
,
3700 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT
),
3701 DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState
,
3702 parameters
.throttle_trigger_threshold
,
3703 DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD
),
3704 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState
,
3705 parameters
.cpu_throttle_initial
,
3706 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL
),
3707 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState
,
3708 parameters
.cpu_throttle_increment
,
3709 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT
),
3710 DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState
,
3711 parameters
.cpu_throttle_tailslow
, false),
3712 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState
,
3713 parameters
.max_bandwidth
, MAX_THROTTLE
),
3714 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState
,
3715 parameters
.downtime_limit
,
3716 DEFAULT_MIGRATE_SET_DOWNTIME
),
3717 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState
,
3718 parameters
.x_checkpoint_delay
,
3719 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY
),
3720 DEFINE_PROP_UINT8("multifd-channels", MigrationState
,
3721 parameters
.multifd_channels
,
3722 DEFAULT_MIGRATE_MULTIFD_CHANNELS
),
3723 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState
,
3724 parameters
.multifd_compression
,
3725 DEFAULT_MIGRATE_MULTIFD_COMPRESSION
),
3726 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState
,
3727 parameters
.multifd_zlib_level
,
3728 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL
),
3729 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState
,
3730 parameters
.multifd_zstd_level
,
3731 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL
),
3732 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState
,
3733 parameters
.xbzrle_cache_size
,
3734 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE
),
3735 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState
,
3736 parameters
.max_postcopy_bandwidth
,
3737 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH
),
3738 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState
,
3739 parameters
.max_cpu_throttle
,
3740 DEFAULT_MIGRATE_MAX_CPU_THROTTLE
),
3741 DEFINE_PROP_SIZE("announce-initial", MigrationState
,
3742 parameters
.announce_initial
,
3743 DEFAULT_MIGRATE_ANNOUNCE_INITIAL
),
3744 DEFINE_PROP_SIZE("announce-max", MigrationState
,
3745 parameters
.announce_max
,
3746 DEFAULT_MIGRATE_ANNOUNCE_MAX
),
3747 DEFINE_PROP_SIZE("announce-rounds", MigrationState
,
3748 parameters
.announce_rounds
,
3749 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS
),
3750 DEFINE_PROP_SIZE("announce-step", MigrationState
,
3751 parameters
.announce_step
,
3752 DEFAULT_MIGRATE_ANNOUNCE_STEP
),
3754 /* Migration capabilities */
3755 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE
),
3756 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL
),
3757 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE
),
3758 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS
),
3759 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS
),
3760 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS
),
3761 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM
),
3762 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO
),
3763 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM
),
3764 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK
),
3765 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH
),
3766 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD
),
3768 DEFINE_PROP_END_OF_LIST(),
3771 static void migration_class_init(ObjectClass
*klass
, void *data
)
3773 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3775 dc
->user_creatable
= false;
3776 device_class_set_props(dc
, migration_properties
);
3779 static void migration_instance_finalize(Object
*obj
)
3781 MigrationState
*ms
= MIGRATION_OBJ(obj
);
3782 MigrationParameters
*params
= &ms
->parameters
;
3784 qemu_mutex_destroy(&ms
->error_mutex
);
3785 qemu_mutex_destroy(&ms
->qemu_file_lock
);
3786 g_free(params
->tls_hostname
);
3787 g_free(params
->tls_creds
);
3788 qemu_sem_destroy(&ms
->wait_unplug_sem
);
3789 qemu_sem_destroy(&ms
->rate_limit_sem
);
3790 qemu_sem_destroy(&ms
->pause_sem
);
3791 qemu_sem_destroy(&ms
->postcopy_pause_sem
);
3792 qemu_sem_destroy(&ms
->postcopy_pause_rp_sem
);
3793 qemu_sem_destroy(&ms
->rp_state
.rp_sem
);
3794 error_free(ms
->error
);
3797 static void migration_instance_init(Object
*obj
)
3799 MigrationState
*ms
= MIGRATION_OBJ(obj
);
3800 MigrationParameters
*params
= &ms
->parameters
;
3802 ms
->state
= MIGRATION_STATUS_NONE
;
3804 ms
->pages_per_second
= -1;
3805 qemu_sem_init(&ms
->pause_sem
, 0);
3806 qemu_mutex_init(&ms
->error_mutex
);
3808 params
->tls_hostname
= g_strdup("");
3809 params
->tls_creds
= g_strdup("");
3811 /* Set has_* up only for parameter checks */
3812 params
->has_compress_level
= true;
3813 params
->has_compress_threads
= true;
3814 params
->has_decompress_threads
= true;
3815 params
->has_throttle_trigger_threshold
= true;
3816 params
->has_cpu_throttle_initial
= true;
3817 params
->has_cpu_throttle_increment
= true;
3818 params
->has_cpu_throttle_tailslow
= true;
3819 params
->has_max_bandwidth
= true;
3820 params
->has_downtime_limit
= true;
3821 params
->has_x_checkpoint_delay
= true;
3822 params
->has_block_incremental
= true;
3823 params
->has_multifd_channels
= true;
3824 params
->has_multifd_compression
= true;
3825 params
->has_multifd_zlib_level
= true;
3826 params
->has_multifd_zstd_level
= true;
3827 params
->has_xbzrle_cache_size
= true;
3828 params
->has_max_postcopy_bandwidth
= true;
3829 params
->has_max_cpu_throttle
= true;
3830 params
->has_announce_initial
= true;
3831 params
->has_announce_max
= true;
3832 params
->has_announce_rounds
= true;
3833 params
->has_announce_step
= true;
3835 qemu_sem_init(&ms
->postcopy_pause_sem
, 0);
3836 qemu_sem_init(&ms
->postcopy_pause_rp_sem
, 0);
3837 qemu_sem_init(&ms
->rp_state
.rp_sem
, 0);
3838 qemu_sem_init(&ms
->rate_limit_sem
, 0);
3839 qemu_sem_init(&ms
->wait_unplug_sem
, 0);
3840 qemu_mutex_init(&ms
->qemu_file_lock
);
3844 * Return true if check pass, false otherwise. Error will be put
3845 * inside errp if provided.
3847 static bool migration_object_check(MigrationState
*ms
, Error
**errp
)
3849 MigrationCapabilityStatusList
*head
= NULL
;
3850 /* Assuming all off */
3851 bool cap_list
[MIGRATION_CAPABILITY__MAX
] = { 0 }, ret
;
3854 if (!migrate_params_check(&ms
->parameters
, errp
)) {
3858 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
3859 if (ms
->enabled_capabilities
[i
]) {
3860 head
= migrate_cap_add(head
, i
, true);
3864 ret
= migrate_caps_check(cap_list
, head
, errp
);
3866 /* It works with head == NULL */
3867 qapi_free_MigrationCapabilityStatusList(head
);
3872 static const TypeInfo migration_type
= {
3873 .name
= TYPE_MIGRATION
,
3875 * NOTE: TYPE_MIGRATION is not really a device, as the object is
3876 * not created using qdev_new(), it is not attached to the qdev
3877 * device tree, and it is never realized.
3879 * TODO: Make this TYPE_OBJECT once QOM provides something like
3880 * TYPE_DEVICE's "-global" properties.
3882 .parent
= TYPE_DEVICE
,
3883 .class_init
= migration_class_init
,
3884 .class_size
= sizeof(MigrationClass
),
3885 .instance_size
= sizeof(MigrationState
),
3886 .instance_init
= migration_instance_init
,
3887 .instance_finalize
= migration_instance_finalize
,
3890 static void register_migration_types(void)
3892 type_register_static(&migration_type
);
3895 type_init(register_migration_types
);