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/migration.h"
21 #include "migration/qemu-file.h"
22 #include "sysemu/sysemu.h"
23 #include "block/block.h"
24 #include "qapi/qmp/qerror.h"
25 #include "qapi/util.h"
26 #include "qemu/sockets.h"
28 #include "migration/block.h"
29 #include "migration/postcopy-ram.h"
30 #include "qemu/thread.h"
31 #include "qmp-commands.h"
33 #include "qapi-event.h"
35 #include "exec/memory.h"
36 #include "exec/address-spaces.h"
37 #include "io/channel-buffer.h"
38 #include "io/channel-tls.h"
39 #include "migration/colo.h"
41 #define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
43 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
45 #define BUFFER_DELAY 100
46 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
48 /* Time in milliseconds we are allowed to stop the source,
49 * for sending the last part */
50 #define DEFAULT_MIGRATE_SET_DOWNTIME 300
52 /* Default compression thread count */
53 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
54 /* Default decompression thread count, usually decompression is at
55 * least 4 times as fast as compression.*/
56 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
57 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
58 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
59 /* Define default autoconverge cpu throttle migration parameters */
60 #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
61 #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
63 /* Migration XBZRLE default cache size */
64 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
66 static NotifierList migration_state_notifiers
=
67 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers
);
69 static bool deferred_incoming
;
72 * Current state of incoming postcopy; note this is not part of
73 * MigrationIncomingState since it's state is used during cleanup
74 * at the end as MIS is being freed.
76 static PostcopyState incoming_postcopy_state
;
78 /* When we add fault tolerance, we could have several
79 migrations at once. For now we don't need to add
80 dynamic creation of migration */
83 MigrationState
*migrate_get_current(void)
86 static MigrationState current_migration
= {
87 .state
= MIGRATION_STATUS_NONE
,
88 .xbzrle_cache_size
= DEFAULT_MIGRATE_CACHE_SIZE
,
91 .compress_level
= DEFAULT_MIGRATE_COMPRESS_LEVEL
,
92 .compress_threads
= DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT
,
93 .decompress_threads
= DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT
,
94 .cpu_throttle_initial
= DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL
,
95 .cpu_throttle_increment
= DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT
,
96 .max_bandwidth
= MAX_THROTTLE
,
97 .downtime_limit
= DEFAULT_MIGRATE_SET_DOWNTIME
,
102 qemu_mutex_init(¤t_migration
.src_page_req_mutex
);
105 return ¤t_migration
;
109 static MigrationIncomingState
*mis_current
;
111 MigrationIncomingState
*migration_incoming_get_current(void)
116 MigrationIncomingState
*migration_incoming_state_new(QEMUFile
* f
)
118 mis_current
= g_new0(MigrationIncomingState
, 1);
119 mis_current
->from_src_file
= f
;
120 mis_current
->state
= MIGRATION_STATUS_NONE
;
121 QLIST_INIT(&mis_current
->loadvm_handlers
);
122 qemu_mutex_init(&mis_current
->rp_mutex
);
123 qemu_event_init(&mis_current
->main_thread_load_event
, false);
128 void migration_incoming_state_destroy(void)
130 qemu_event_destroy(&mis_current
->main_thread_load_event
);
131 loadvm_free_handlers(mis_current
);
140 uint8_t runstate
[100];
145 static GlobalState global_state
;
147 int global_state_store(void)
149 if (!runstate_store((char *)global_state
.runstate
,
150 sizeof(global_state
.runstate
))) {
151 error_report("runstate name too big: %s", global_state
.runstate
);
152 trace_migrate_state_too_big();
158 void global_state_store_running(void)
160 const char *state
= RunState_lookup
[RUN_STATE_RUNNING
];
161 strncpy((char *)global_state
.runstate
,
162 state
, sizeof(global_state
.runstate
));
165 static bool global_state_received(void)
167 return global_state
.received
;
170 static RunState
global_state_get_runstate(void)
172 return global_state
.state
;
175 void global_state_set_optional(void)
177 global_state
.optional
= true;
180 static bool global_state_needed(void *opaque
)
182 GlobalState
*s
= opaque
;
183 char *runstate
= (char *)s
->runstate
;
185 /* If it is not optional, it is mandatory */
187 if (s
->optional
== false) {
191 /* If state is running or paused, it is not needed */
193 if (strcmp(runstate
, "running") == 0 ||
194 strcmp(runstate
, "paused") == 0) {
198 /* for any other state it is needed */
202 static int global_state_post_load(void *opaque
, int version_id
)
204 GlobalState
*s
= opaque
;
205 Error
*local_err
= NULL
;
207 char *runstate
= (char *)s
->runstate
;
210 trace_migrate_global_state_post_load(runstate
);
212 r
= qapi_enum_parse(RunState_lookup
, runstate
, RUN_STATE__MAX
,
217 error_report_err(local_err
);
226 static void global_state_pre_save(void *opaque
)
228 GlobalState
*s
= opaque
;
230 trace_migrate_global_state_pre_save((char *)s
->runstate
);
231 s
->size
= strlen((char *)s
->runstate
) + 1;
234 static const VMStateDescription vmstate_globalstate
= {
235 .name
= "globalstate",
237 .minimum_version_id
= 1,
238 .post_load
= global_state_post_load
,
239 .pre_save
= global_state_pre_save
,
240 .needed
= global_state_needed
,
241 .fields
= (VMStateField
[]) {
242 VMSTATE_UINT32(size
, GlobalState
),
243 VMSTATE_BUFFER(runstate
, GlobalState
),
244 VMSTATE_END_OF_LIST()
248 void register_global_state(void)
250 /* We would use it independently that we receive it */
251 strcpy((char *)&global_state
.runstate
, "");
252 global_state
.received
= false;
253 vmstate_register(NULL
, 0, &vmstate_globalstate
, &global_state
);
256 static void migrate_generate_event(int new_state
)
258 if (migrate_use_events()) {
259 qapi_event_send_migration(new_state
, &error_abort
);
264 * Called on -incoming with a defer: uri.
265 * The migration can be started later after any parameters have been
268 static void deferred_incoming_migration(Error
**errp
)
270 if (deferred_incoming
) {
271 error_setg(errp
, "Incoming migration already deferred");
273 deferred_incoming
= true;
276 /* Request a range of pages from the source VM at the given
278 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
279 * as the last request (a name must have been given previously)
280 * Start: Address offset within the RB
281 * Len: Length in bytes required - must be a multiple of pagesize
283 void migrate_send_rp_req_pages(MigrationIncomingState
*mis
, const char *rbname
,
284 ram_addr_t start
, size_t len
)
286 uint8_t bufc
[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
287 size_t msglen
= 12; /* start + len */
289 *(uint64_t *)bufc
= cpu_to_be64((uint64_t)start
);
290 *(uint32_t *)(bufc
+ 8) = cpu_to_be32((uint32_t)len
);
293 int rbname_len
= strlen(rbname
);
294 assert(rbname_len
< 256);
296 bufc
[msglen
++] = rbname_len
;
297 memcpy(bufc
+ msglen
, rbname
, rbname_len
);
298 msglen
+= rbname_len
;
299 migrate_send_rp_message(mis
, MIG_RP_MSG_REQ_PAGES_ID
, msglen
, bufc
);
301 migrate_send_rp_message(mis
, MIG_RP_MSG_REQ_PAGES
, msglen
, bufc
);
305 void qemu_start_incoming_migration(const char *uri
, Error
**errp
)
309 qapi_event_send_migration(MIGRATION_STATUS_SETUP
, &error_abort
);
310 if (!strcmp(uri
, "defer")) {
311 deferred_incoming_migration(errp
);
312 } else if (strstart(uri
, "tcp:", &p
)) {
313 tcp_start_incoming_migration(p
, errp
);
315 } else if (strstart(uri
, "rdma:", &p
)) {
316 rdma_start_incoming_migration(p
, errp
);
318 } else if (strstart(uri
, "exec:", &p
)) {
319 exec_start_incoming_migration(p
, errp
);
320 } else if (strstart(uri
, "unix:", &p
)) {
321 unix_start_incoming_migration(p
, errp
);
322 } else if (strstart(uri
, "fd:", &p
)) {
323 fd_start_incoming_migration(p
, errp
);
325 error_setg(errp
, "unknown migration protocol: %s", uri
);
329 static void process_incoming_migration_bh(void *opaque
)
331 Error
*local_err
= NULL
;
332 MigrationIncomingState
*mis
= opaque
;
334 /* Make sure all file formats flush their mutable metadata */
335 bdrv_invalidate_cache_all(&local_err
);
337 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
338 MIGRATION_STATUS_FAILED
);
339 error_report_err(local_err
);
340 migrate_decompress_threads_join();
345 * This must happen after all error conditions are dealt with and
346 * we're sure the VM is going to be running on this host.
348 qemu_announce_self();
350 /* If global state section was not received or we are in running
351 state, we need to obey autostart. Any other state is set with
354 if (!global_state_received() ||
355 global_state_get_runstate() == RUN_STATE_RUNNING
) {
359 runstate_set(RUN_STATE_PAUSED
);
362 runstate_set(global_state_get_runstate());
364 migrate_decompress_threads_join();
366 * This must happen after any state changes since as soon as an external
367 * observer sees this event they might start to prod at the VM assuming
370 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
371 MIGRATION_STATUS_COMPLETED
);
372 qemu_bh_delete(mis
->bh
);
373 migration_incoming_state_destroy();
376 static void process_incoming_migration_co(void *opaque
)
378 QEMUFile
*f
= opaque
;
379 MigrationIncomingState
*mis
;
383 mis
= migration_incoming_state_new(f
);
384 postcopy_state_set(POSTCOPY_INCOMING_NONE
);
385 migrate_set_state(&mis
->state
, MIGRATION_STATUS_NONE
,
386 MIGRATION_STATUS_ACTIVE
);
387 ret
= qemu_loadvm_state(f
);
389 ps
= postcopy_state_get();
390 trace_process_incoming_migration_co_end(ret
, ps
);
391 if (ps
!= POSTCOPY_INCOMING_NONE
) {
392 if (ps
== POSTCOPY_INCOMING_ADVISE
) {
394 * Where a migration had postcopy enabled (and thus went to advise)
395 * but managed to complete within the precopy period, we can use
398 postcopy_ram_incoming_cleanup(mis
);
399 } else if (ret
>= 0) {
401 * Postcopy was started, cleanup should happen at the end of the
404 trace_process_incoming_migration_co_postcopy_end_main();
407 /* Else if something went wrong then just fall out of the normal exit */
410 /* we get COLO info, and know if we are in COLO mode */
411 if (!ret
&& migration_incoming_enable_colo()) {
412 mis
->migration_incoming_co
= qemu_coroutine_self();
413 qemu_thread_create(&mis
->colo_incoming_thread
, "COLO incoming",
414 colo_process_incoming_thread
, mis
, QEMU_THREAD_JOINABLE
);
415 mis
->have_colo_incoming_thread
= true;
416 qemu_coroutine_yield();
418 /* Wait checkpoint incoming thread exit before free resource */
419 qemu_thread_join(&mis
->colo_incoming_thread
);
423 free_xbzrle_decoded_buf();
426 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
427 MIGRATION_STATUS_FAILED
);
428 error_report("load of migration failed: %s", strerror(-ret
));
429 migrate_decompress_threads_join();
433 mis
->bh
= qemu_bh_new(process_incoming_migration_bh
, mis
);
434 qemu_bh_schedule(mis
->bh
);
437 void migration_fd_process_incoming(QEMUFile
*f
)
439 Coroutine
*co
= qemu_coroutine_create(process_incoming_migration_co
, f
);
441 migrate_decompress_threads_create();
442 qemu_file_set_blocking(f
, false);
443 qemu_coroutine_enter(co
);
447 void migration_channel_process_incoming(MigrationState
*s
,
450 trace_migration_set_incoming_channel(
451 ioc
, object_get_typename(OBJECT(ioc
)));
453 if (s
->parameters
.tls_creds
&&
454 !object_dynamic_cast(OBJECT(ioc
),
455 TYPE_QIO_CHANNEL_TLS
)) {
456 Error
*local_err
= NULL
;
457 migration_tls_channel_process_incoming(s
, ioc
, &local_err
);
459 error_report_err(local_err
);
462 QEMUFile
*f
= qemu_fopen_channel_input(ioc
);
463 migration_fd_process_incoming(f
);
468 void migration_channel_connect(MigrationState
*s
,
470 const char *hostname
)
472 trace_migration_set_outgoing_channel(
473 ioc
, object_get_typename(OBJECT(ioc
)), hostname
);
475 if (s
->parameters
.tls_creds
&&
476 !object_dynamic_cast(OBJECT(ioc
),
477 TYPE_QIO_CHANNEL_TLS
)) {
478 Error
*local_err
= NULL
;
479 migration_tls_channel_connect(s
, ioc
, hostname
, &local_err
);
481 migrate_fd_error(s
, local_err
);
482 error_free(local_err
);
485 QEMUFile
*f
= qemu_fopen_channel_output(ioc
);
489 migrate_fd_connect(s
);
495 * Send a message on the return channel back to the source
498 void migrate_send_rp_message(MigrationIncomingState
*mis
,
499 enum mig_rp_message_type message_type
,
500 uint16_t len
, void *data
)
502 trace_migrate_send_rp_message((int)message_type
, len
);
503 qemu_mutex_lock(&mis
->rp_mutex
);
504 qemu_put_be16(mis
->to_src_file
, (unsigned int)message_type
);
505 qemu_put_be16(mis
->to_src_file
, len
);
506 qemu_put_buffer(mis
->to_src_file
, data
, len
);
507 qemu_fflush(mis
->to_src_file
);
508 qemu_mutex_unlock(&mis
->rp_mutex
);
512 * Send a 'SHUT' message on the return channel with the given value
513 * to indicate that we've finished with the RP. Non-0 value indicates
516 void migrate_send_rp_shut(MigrationIncomingState
*mis
,
521 buf
= cpu_to_be32(value
);
522 migrate_send_rp_message(mis
, MIG_RP_MSG_SHUT
, sizeof(buf
), &buf
);
526 * Send a 'PONG' message on the return channel with the given value
527 * (normally in response to a 'PING')
529 void migrate_send_rp_pong(MigrationIncomingState
*mis
,
534 buf
= cpu_to_be32(value
);
535 migrate_send_rp_message(mis
, MIG_RP_MSG_PONG
, sizeof(buf
), &buf
);
538 MigrationCapabilityStatusList
*qmp_query_migrate_capabilities(Error
**errp
)
540 MigrationCapabilityStatusList
*head
= NULL
;
541 MigrationCapabilityStatusList
*caps
;
542 MigrationState
*s
= migrate_get_current();
545 caps
= NULL
; /* silence compiler warning */
546 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
547 if (i
== MIGRATION_CAPABILITY_X_COLO
&& !colo_supported()) {
551 head
= g_malloc0(sizeof(*caps
));
554 caps
->next
= g_malloc0(sizeof(*caps
));
558 g_malloc(sizeof(*caps
->value
));
559 caps
->value
->capability
= i
;
560 caps
->value
->state
= s
->enabled_capabilities
[i
];
566 MigrationParameters
*qmp_query_migrate_parameters(Error
**errp
)
568 MigrationParameters
*params
;
569 MigrationState
*s
= migrate_get_current();
571 params
= g_malloc0(sizeof(*params
));
572 params
->has_compress_level
= true;
573 params
->compress_level
= s
->parameters
.compress_level
;
574 params
->has_compress_threads
= true;
575 params
->compress_threads
= s
->parameters
.compress_threads
;
576 params
->has_decompress_threads
= true;
577 params
->decompress_threads
= s
->parameters
.decompress_threads
;
578 params
->has_cpu_throttle_initial
= true;
579 params
->cpu_throttle_initial
= s
->parameters
.cpu_throttle_initial
;
580 params
->has_cpu_throttle_increment
= true;
581 params
->cpu_throttle_increment
= s
->parameters
.cpu_throttle_increment
;
582 params
->has_tls_creds
= !!s
->parameters
.tls_creds
;
583 params
->tls_creds
= g_strdup(s
->parameters
.tls_creds
);
584 params
->has_tls_hostname
= !!s
->parameters
.tls_hostname
;
585 params
->tls_hostname
= g_strdup(s
->parameters
.tls_hostname
);
586 params
->has_max_bandwidth
= true;
587 params
->max_bandwidth
= s
->parameters
.max_bandwidth
;
588 params
->has_downtime_limit
= true;
589 params
->downtime_limit
= s
->parameters
.downtime_limit
;
595 * Return true if we're already in the middle of a migration
596 * (i.e. any of the active or setup states)
598 static bool migration_is_setup_or_active(int state
)
601 case MIGRATION_STATUS_ACTIVE
:
602 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
603 case MIGRATION_STATUS_SETUP
:
612 static void get_xbzrle_cache_stats(MigrationInfo
*info
)
614 if (migrate_use_xbzrle()) {
615 info
->has_xbzrle_cache
= true;
616 info
->xbzrle_cache
= g_malloc0(sizeof(*info
->xbzrle_cache
));
617 info
->xbzrle_cache
->cache_size
= migrate_xbzrle_cache_size();
618 info
->xbzrle_cache
->bytes
= xbzrle_mig_bytes_transferred();
619 info
->xbzrle_cache
->pages
= xbzrle_mig_pages_transferred();
620 info
->xbzrle_cache
->cache_miss
= xbzrle_mig_pages_cache_miss();
621 info
->xbzrle_cache
->cache_miss_rate
= xbzrle_mig_cache_miss_rate();
622 info
->xbzrle_cache
->overflow
= xbzrle_mig_pages_overflow();
626 static void populate_ram_info(MigrationInfo
*info
, MigrationState
*s
)
628 info
->has_ram
= true;
629 info
->ram
= g_malloc0(sizeof(*info
->ram
));
630 info
->ram
->transferred
= ram_bytes_transferred();
631 info
->ram
->total
= ram_bytes_total();
632 info
->ram
->duplicate
= dup_mig_pages_transferred();
633 info
->ram
->skipped
= skipped_mig_pages_transferred();
634 info
->ram
->normal
= norm_mig_pages_transferred();
635 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
636 info
->ram
->mbps
= s
->mbps
;
637 info
->ram
->dirty_sync_count
= s
->dirty_sync_count
;
638 info
->ram
->postcopy_requests
= s
->postcopy_requests
;
640 if (s
->state
!= MIGRATION_STATUS_COMPLETED
) {
641 info
->ram
->remaining
= ram_bytes_remaining();
642 info
->ram
->dirty_pages_rate
= s
->dirty_pages_rate
;
646 MigrationInfo
*qmp_query_migrate(Error
**errp
)
648 MigrationInfo
*info
= g_malloc0(sizeof(*info
));
649 MigrationState
*s
= migrate_get_current();
652 case MIGRATION_STATUS_NONE
:
653 /* no migration has happened ever */
655 case MIGRATION_STATUS_SETUP
:
656 info
->has_status
= true;
657 info
->has_total_time
= false;
659 case MIGRATION_STATUS_ACTIVE
:
660 case MIGRATION_STATUS_CANCELLING
:
661 info
->has_status
= true;
662 info
->has_total_time
= true;
663 info
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
665 info
->has_expected_downtime
= true;
666 info
->expected_downtime
= s
->expected_downtime
;
667 info
->has_setup_time
= true;
668 info
->setup_time
= s
->setup_time
;
670 populate_ram_info(info
, s
);
672 if (blk_mig_active()) {
673 info
->has_disk
= true;
674 info
->disk
= g_malloc0(sizeof(*info
->disk
));
675 info
->disk
->transferred
= blk_mig_bytes_transferred();
676 info
->disk
->remaining
= blk_mig_bytes_remaining();
677 info
->disk
->total
= blk_mig_bytes_total();
680 if (cpu_throttle_active()) {
681 info
->has_cpu_throttle_percentage
= true;
682 info
->cpu_throttle_percentage
= cpu_throttle_get_percentage();
685 get_xbzrle_cache_stats(info
);
687 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
688 /* Mostly the same as active; TODO add some postcopy stats */
689 info
->has_status
= true;
690 info
->has_total_time
= true;
691 info
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
693 info
->has_expected_downtime
= true;
694 info
->expected_downtime
= s
->expected_downtime
;
695 info
->has_setup_time
= true;
696 info
->setup_time
= s
->setup_time
;
698 populate_ram_info(info
, s
);
700 if (blk_mig_active()) {
701 info
->has_disk
= true;
702 info
->disk
= g_malloc0(sizeof(*info
->disk
));
703 info
->disk
->transferred
= blk_mig_bytes_transferred();
704 info
->disk
->remaining
= blk_mig_bytes_remaining();
705 info
->disk
->total
= blk_mig_bytes_total();
708 get_xbzrle_cache_stats(info
);
710 case MIGRATION_STATUS_COLO
:
711 info
->has_status
= true;
712 /* TODO: display COLO specific information (checkpoint info etc.) */
714 case MIGRATION_STATUS_COMPLETED
:
715 get_xbzrle_cache_stats(info
);
717 info
->has_status
= true;
718 info
->has_total_time
= true;
719 info
->total_time
= s
->total_time
;
720 info
->has_downtime
= true;
721 info
->downtime
= s
->downtime
;
722 info
->has_setup_time
= true;
723 info
->setup_time
= s
->setup_time
;
725 populate_ram_info(info
, s
);
727 case MIGRATION_STATUS_FAILED
:
728 info
->has_status
= true;
730 info
->has_error_desc
= true;
731 info
->error_desc
= g_strdup(error_get_pretty(s
->error
));
734 case MIGRATION_STATUS_CANCELLED
:
735 info
->has_status
= true;
738 info
->status
= s
->state
;
743 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList
*params
,
746 MigrationState
*s
= migrate_get_current();
747 MigrationCapabilityStatusList
*cap
;
748 bool old_postcopy_cap
= migrate_postcopy_ram();
750 if (migration_is_setup_or_active(s
->state
)) {
751 error_setg(errp
, QERR_MIGRATION_ACTIVE
);
755 for (cap
= params
; cap
; cap
= cap
->next
) {
756 if (cap
->value
->capability
== MIGRATION_CAPABILITY_X_COLO
) {
757 if (!colo_supported()) {
758 error_setg(errp
, "COLO is not currently supported, please"
759 " configure with --enable-colo option in order to"
760 " support COLO feature");
764 s
->enabled_capabilities
[cap
->value
->capability
] = cap
->value
->state
;
767 if (migrate_postcopy_ram()) {
768 if (migrate_use_compression()) {
769 /* The decompression threads asynchronously write into RAM
770 * rather than use the atomic copies needed to avoid
771 * userfaulting. It should be possible to fix the decompression
772 * threads for compatibility in future.
774 error_report("Postcopy is not currently compatible with "
776 s
->enabled_capabilities
[MIGRATION_CAPABILITY_POSTCOPY_RAM
] =
779 /* This check is reasonably expensive, so only when it's being
780 * set the first time, also it's only the destination that needs
783 if (!old_postcopy_cap
&& runstate_check(RUN_STATE_INMIGRATE
) &&
784 !postcopy_ram_supported_by_host()) {
785 /* postcopy_ram_supported_by_host will have emitted a more
788 error_report("Postcopy is not supported");
789 s
->enabled_capabilities
[MIGRATION_CAPABILITY_POSTCOPY_RAM
] =
795 void qmp_migrate_set_parameters(MigrationParameters
*params
, Error
**errp
)
797 MigrationState
*s
= migrate_get_current();
799 if (params
->has_compress_level
&&
800 (params
->compress_level
< 0 || params
->compress_level
> 9)) {
801 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "compress_level",
802 "is invalid, it should be in the range of 0 to 9");
805 if (params
->has_compress_threads
&&
806 (params
->compress_threads
< 1 || params
->compress_threads
> 255)) {
807 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
809 "is invalid, it should be in the range of 1 to 255");
812 if (params
->has_decompress_threads
&&
813 (params
->decompress_threads
< 1 || params
->decompress_threads
> 255)) {
814 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
815 "decompress_threads",
816 "is invalid, it should be in the range of 1 to 255");
819 if (params
->has_cpu_throttle_initial
&&
820 (params
->cpu_throttle_initial
< 1 ||
821 params
->cpu_throttle_initial
> 99)) {
822 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
823 "cpu_throttle_initial",
824 "an integer in the range of 1 to 99");
827 if (params
->has_cpu_throttle_increment
&&
828 (params
->cpu_throttle_increment
< 1 ||
829 params
->cpu_throttle_increment
> 99)) {
830 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
831 "cpu_throttle_increment",
832 "an integer in the range of 1 to 99");
835 if (params
->has_max_bandwidth
&&
836 (params
->max_bandwidth
< 0 || params
->max_bandwidth
> SIZE_MAX
)) {
837 error_setg(errp
, "Parameter 'max_bandwidth' expects an integer in the"
838 " range of 0 to %zu bytes/second", SIZE_MAX
);
841 if (params
->has_downtime_limit
&&
842 (params
->downtime_limit
< 0 || params
->downtime_limit
> 2000000)) {
843 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
845 "an integer in the range of 0 to 2000000 milliseconds");
849 if (params
->has_compress_level
) {
850 s
->parameters
.compress_level
= params
->compress_level
;
852 if (params
->has_compress_threads
) {
853 s
->parameters
.compress_threads
= params
->compress_threads
;
855 if (params
->has_decompress_threads
) {
856 s
->parameters
.decompress_threads
= params
->decompress_threads
;
858 if (params
->has_cpu_throttle_initial
) {
859 s
->parameters
.cpu_throttle_initial
= params
->cpu_throttle_initial
;
861 if (params
->has_cpu_throttle_increment
) {
862 s
->parameters
.cpu_throttle_increment
= params
->cpu_throttle_increment
;
864 if (params
->has_tls_creds
) {
865 g_free(s
->parameters
.tls_creds
);
866 s
->parameters
.tls_creds
= g_strdup(params
->tls_creds
);
868 if (params
->has_tls_hostname
) {
869 g_free(s
->parameters
.tls_hostname
);
870 s
->parameters
.tls_hostname
= g_strdup(params
->tls_hostname
);
872 if (params
->has_max_bandwidth
) {
873 s
->parameters
.max_bandwidth
= params
->max_bandwidth
;
874 if (s
->to_dst_file
) {
875 qemu_file_set_rate_limit(s
->to_dst_file
,
876 s
->parameters
.max_bandwidth
/ XFER_LIMIT_RATIO
);
879 if (params
->has_downtime_limit
) {
880 s
->parameters
.downtime_limit
= params
->downtime_limit
;
885 void qmp_migrate_start_postcopy(Error
**errp
)
887 MigrationState
*s
= migrate_get_current();
889 if (!migrate_postcopy_ram()) {
890 error_setg(errp
, "Enable postcopy with migrate_set_capability before"
891 " the start of migration");
895 if (s
->state
== MIGRATION_STATUS_NONE
) {
896 error_setg(errp
, "Postcopy must be started after migration has been"
901 * we don't error if migration has finished since that would be racy
902 * with issuing this command.
904 atomic_set(&s
->start_postcopy
, true);
907 /* shared migration helpers */
909 void migrate_set_state(int *state
, int old_state
, int new_state
)
911 if (atomic_cmpxchg(state
, old_state
, new_state
) == old_state
) {
912 trace_migrate_set_state(new_state
);
913 migrate_generate_event(new_state
);
917 static void migrate_fd_cleanup(void *opaque
)
919 MigrationState
*s
= opaque
;
921 qemu_bh_delete(s
->cleanup_bh
);
922 s
->cleanup_bh
= NULL
;
926 if (s
->to_dst_file
) {
927 trace_migrate_fd_cleanup();
928 qemu_mutex_unlock_iothread();
929 if (s
->migration_thread_running
) {
930 qemu_thread_join(&s
->thread
);
931 s
->migration_thread_running
= false;
933 qemu_mutex_lock_iothread();
935 migrate_compress_threads_join();
936 qemu_fclose(s
->to_dst_file
);
937 s
->to_dst_file
= NULL
;
940 assert((s
->state
!= MIGRATION_STATUS_ACTIVE
) &&
941 (s
->state
!= MIGRATION_STATUS_POSTCOPY_ACTIVE
));
943 if (s
->state
== MIGRATION_STATUS_CANCELLING
) {
944 migrate_set_state(&s
->state
, MIGRATION_STATUS_CANCELLING
,
945 MIGRATION_STATUS_CANCELLED
);
948 notifier_list_notify(&migration_state_notifiers
, s
);
951 void migrate_fd_error(MigrationState
*s
, const Error
*error
)
953 trace_migrate_fd_error(error
? error_get_pretty(error
) : "");
954 assert(s
->to_dst_file
== NULL
);
955 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
956 MIGRATION_STATUS_FAILED
);
958 s
->error
= error_copy(error
);
960 notifier_list_notify(&migration_state_notifiers
, s
);
963 static void migrate_fd_cancel(MigrationState
*s
)
966 QEMUFile
*f
= migrate_get_current()->to_dst_file
;
967 trace_migrate_fd_cancel();
969 if (s
->rp_state
.from_dst_file
) {
970 /* shutdown the rp socket, so causing the rp thread to shutdown */
971 qemu_file_shutdown(s
->rp_state
.from_dst_file
);
975 old_state
= s
->state
;
976 if (!migration_is_setup_or_active(old_state
)) {
979 migrate_set_state(&s
->state
, old_state
, MIGRATION_STATUS_CANCELLING
);
980 } while (s
->state
!= MIGRATION_STATUS_CANCELLING
);
983 * If we're unlucky the migration code might be stuck somewhere in a
984 * send/write while the network has failed and is waiting to timeout;
985 * if we've got shutdown(2) available then we can force it to quit.
986 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
987 * called in a bh, so there is no race against this cancel.
989 if (s
->state
== MIGRATION_STATUS_CANCELLING
&& f
) {
990 qemu_file_shutdown(f
);
994 void add_migration_state_change_notifier(Notifier
*notify
)
996 notifier_list_add(&migration_state_notifiers
, notify
);
999 void remove_migration_state_change_notifier(Notifier
*notify
)
1001 notifier_remove(notify
);
1004 bool migration_in_setup(MigrationState
*s
)
1006 return s
->state
== MIGRATION_STATUS_SETUP
;
1009 bool migration_has_finished(MigrationState
*s
)
1011 return s
->state
== MIGRATION_STATUS_COMPLETED
;
1014 bool migration_has_failed(MigrationState
*s
)
1016 return (s
->state
== MIGRATION_STATUS_CANCELLED
||
1017 s
->state
== MIGRATION_STATUS_FAILED
);
1020 bool migration_in_postcopy(MigrationState
*s
)
1022 return (s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
);
1025 bool migration_in_postcopy_after_devices(MigrationState
*s
)
1027 return migration_in_postcopy(s
) && s
->postcopy_after_devices
;
1030 MigrationState
*migrate_init(const MigrationParams
*params
)
1032 MigrationState
*s
= migrate_get_current();
1035 * Reinitialise all migration state, except
1036 * parameters/capabilities that the user set, and
1042 s
->to_dst_file
= NULL
;
1043 s
->state
= MIGRATION_STATUS_NONE
;
1044 s
->params
= *params
;
1045 s
->rp_state
.from_dst_file
= NULL
;
1046 s
->rp_state
.error
= false;
1049 s
->expected_downtime
= 0;
1050 s
->dirty_pages_rate
= 0;
1051 s
->dirty_bytes_rate
= 0;
1053 s
->dirty_sync_count
= 0;
1054 s
->start_postcopy
= false;
1055 s
->postcopy_after_devices
= false;
1056 s
->postcopy_requests
= 0;
1057 s
->migration_thread_running
= false;
1058 s
->last_req_rb
= NULL
;
1059 error_free(s
->error
);
1062 migrate_set_state(&s
->state
, MIGRATION_STATUS_NONE
, MIGRATION_STATUS_SETUP
);
1064 QSIMPLEQ_INIT(&s
->src_page_requests
);
1066 s
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1070 static GSList
*migration_blockers
;
1072 void migrate_add_blocker(Error
*reason
)
1074 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
1077 void migrate_del_blocker(Error
*reason
)
1079 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
1082 void qmp_migrate_incoming(const char *uri
, Error
**errp
)
1084 Error
*local_err
= NULL
;
1085 static bool once
= true;
1087 if (!deferred_incoming
) {
1088 error_setg(errp
, "For use with '-incoming defer'");
1092 error_setg(errp
, "The incoming migration has already been started");
1095 qemu_start_incoming_migration(uri
, &local_err
);
1098 error_propagate(errp
, local_err
);
1105 bool migration_is_blocked(Error
**errp
)
1107 if (qemu_savevm_state_blocked(errp
)) {
1111 if (migration_blockers
) {
1112 *errp
= error_copy(migration_blockers
->data
);
1119 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
1120 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
1123 Error
*local_err
= NULL
;
1124 MigrationState
*s
= migrate_get_current();
1125 MigrationParams params
;
1128 params
.blk
= has_blk
&& blk
;
1129 params
.shared
= has_inc
&& inc
;
1131 if (migration_is_setup_or_active(s
->state
) ||
1132 s
->state
== MIGRATION_STATUS_CANCELLING
||
1133 s
->state
== MIGRATION_STATUS_COLO
) {
1134 error_setg(errp
, QERR_MIGRATION_ACTIVE
);
1137 if (runstate_check(RUN_STATE_INMIGRATE
)) {
1138 error_setg(errp
, "Guest is waiting for an incoming migration");
1142 if (migration_is_blocked(errp
)) {
1146 s
= migrate_init(¶ms
);
1148 if (strstart(uri
, "tcp:", &p
)) {
1149 tcp_start_outgoing_migration(s
, p
, &local_err
);
1151 } else if (strstart(uri
, "rdma:", &p
)) {
1152 rdma_start_outgoing_migration(s
, p
, &local_err
);
1154 } else if (strstart(uri
, "exec:", &p
)) {
1155 exec_start_outgoing_migration(s
, p
, &local_err
);
1156 } else if (strstart(uri
, "unix:", &p
)) {
1157 unix_start_outgoing_migration(s
, p
, &local_err
);
1158 } else if (strstart(uri
, "fd:", &p
)) {
1159 fd_start_outgoing_migration(s
, p
, &local_err
);
1161 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri",
1162 "a valid migration protocol");
1163 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
1164 MIGRATION_STATUS_FAILED
);
1169 migrate_fd_error(s
, local_err
);
1170 error_propagate(errp
, local_err
);
1175 void qmp_migrate_cancel(Error
**errp
)
1177 migrate_fd_cancel(migrate_get_current());
1180 void qmp_migrate_set_cache_size(int64_t value
, Error
**errp
)
1182 MigrationState
*s
= migrate_get_current();
1185 /* Check for truncation */
1186 if (value
!= (size_t)value
) {
1187 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
1188 "exceeding address space");
1192 /* Cache should not be larger than guest ram size */
1193 if (value
> ram_bytes_total()) {
1194 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
1195 "exceeds guest ram size ");
1199 new_size
= xbzrle_cache_resize(value
);
1201 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
1202 "is smaller than page size");
1206 s
->xbzrle_cache_size
= new_size
;
1209 int64_t qmp_query_migrate_cache_size(Error
**errp
)
1211 return migrate_xbzrle_cache_size();
1214 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
1216 MigrationParameters p
= {
1217 .has_max_bandwidth
= true,
1218 .max_bandwidth
= value
,
1221 qmp_migrate_set_parameters(&p
, errp
);
1224 void qmp_migrate_set_downtime(double value
, Error
**errp
)
1226 value
*= 1000; /* Convert to milliseconds */
1227 value
= MAX(0, MIN(INT64_MAX
, value
));
1229 MigrationParameters p
= {
1230 .has_downtime_limit
= true,
1231 .downtime_limit
= value
,
1234 qmp_migrate_set_parameters(&p
, errp
);
1237 bool migrate_postcopy_ram(void)
1241 s
= migrate_get_current();
1243 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_POSTCOPY_RAM
];
1246 bool migrate_auto_converge(void)
1250 s
= migrate_get_current();
1252 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_AUTO_CONVERGE
];
1255 bool migrate_zero_blocks(void)
1259 s
= migrate_get_current();
1261 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_ZERO_BLOCKS
];
1264 bool migrate_use_compression(void)
1268 s
= migrate_get_current();
1270 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_COMPRESS
];
1273 int migrate_compress_level(void)
1277 s
= migrate_get_current();
1279 return s
->parameters
.compress_level
;
1282 int migrate_compress_threads(void)
1286 s
= migrate_get_current();
1288 return s
->parameters
.compress_threads
;
1291 int migrate_decompress_threads(void)
1295 s
= migrate_get_current();
1297 return s
->parameters
.decompress_threads
;
1300 bool migrate_use_events(void)
1304 s
= migrate_get_current();
1306 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_EVENTS
];
1309 int migrate_use_xbzrle(void)
1313 s
= migrate_get_current();
1315 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_XBZRLE
];
1318 int64_t migrate_xbzrle_cache_size(void)
1322 s
= migrate_get_current();
1324 return s
->xbzrle_cache_size
;
1327 /* migration thread support */
1329 * Something bad happened to the RP stream, mark an error
1330 * The caller shall print or trace something to indicate why
1332 static void mark_source_rp_bad(MigrationState
*s
)
1334 s
->rp_state
.error
= true;
1337 static struct rp_cmd_args
{
1338 ssize_t len
; /* -1 = variable */
1341 [MIG_RP_MSG_INVALID
] = { .len
= -1, .name
= "INVALID" },
1342 [MIG_RP_MSG_SHUT
] = { .len
= 4, .name
= "SHUT" },
1343 [MIG_RP_MSG_PONG
] = { .len
= 4, .name
= "PONG" },
1344 [MIG_RP_MSG_REQ_PAGES
] = { .len
= 12, .name
= "REQ_PAGES" },
1345 [MIG_RP_MSG_REQ_PAGES_ID
] = { .len
= -1, .name
= "REQ_PAGES_ID" },
1346 [MIG_RP_MSG_MAX
] = { .len
= -1, .name
= "MAX" },
1350 * Process a request for pages received on the return path,
1351 * We're allowed to send more than requested (e.g. to round to our page size)
1352 * and we don't need to send pages that have already been sent.
1354 static void migrate_handle_rp_req_pages(MigrationState
*ms
, const char* rbname
,
1355 ram_addr_t start
, size_t len
)
1357 long our_host_ps
= getpagesize();
1359 trace_migrate_handle_rp_req_pages(rbname
, start
, len
);
1362 * Since we currently insist on matching page sizes, just sanity check
1363 * we're being asked for whole host pages.
1365 if (start
& (our_host_ps
-1) ||
1366 (len
& (our_host_ps
-1))) {
1367 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1368 " len: %zd", __func__
, start
, len
);
1369 mark_source_rp_bad(ms
);
1373 if (ram_save_queue_pages(ms
, rbname
, start
, len
)) {
1374 mark_source_rp_bad(ms
);
1379 * Handles messages sent on the return path towards the source VM
1382 static void *source_return_path_thread(void *opaque
)
1384 MigrationState
*ms
= opaque
;
1385 QEMUFile
*rp
= ms
->rp_state
.from_dst_file
;
1386 uint16_t header_len
, header_type
;
1388 uint32_t tmp32
, sibling_error
;
1389 ram_addr_t start
= 0; /* =0 to silence warning */
1390 size_t len
= 0, expected_len
;
1393 trace_source_return_path_thread_entry();
1394 while (!ms
->rp_state
.error
&& !qemu_file_get_error(rp
) &&
1395 migration_is_setup_or_active(ms
->state
)) {
1396 trace_source_return_path_thread_loop_top();
1397 header_type
= qemu_get_be16(rp
);
1398 header_len
= qemu_get_be16(rp
);
1400 if (header_type
>= MIG_RP_MSG_MAX
||
1401 header_type
== MIG_RP_MSG_INVALID
) {
1402 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1403 header_type
, header_len
);
1404 mark_source_rp_bad(ms
);
1408 if ((rp_cmd_args
[header_type
].len
!= -1 &&
1409 header_len
!= rp_cmd_args
[header_type
].len
) ||
1410 header_len
> sizeof(buf
)) {
1411 error_report("RP: Received '%s' message (0x%04x) with"
1412 "incorrect length %d expecting %zu",
1413 rp_cmd_args
[header_type
].name
, header_type
, header_len
,
1414 (size_t)rp_cmd_args
[header_type
].len
);
1415 mark_source_rp_bad(ms
);
1419 /* We know we've got a valid header by this point */
1420 res
= qemu_get_buffer(rp
, buf
, header_len
);
1421 if (res
!= header_len
) {
1422 error_report("RP: Failed reading data for message 0x%04x"
1423 " read %d expected %d",
1424 header_type
, res
, header_len
);
1425 mark_source_rp_bad(ms
);
1429 /* OK, we have the message and the data */
1430 switch (header_type
) {
1431 case MIG_RP_MSG_SHUT
:
1432 sibling_error
= ldl_be_p(buf
);
1433 trace_source_return_path_thread_shut(sibling_error
);
1434 if (sibling_error
) {
1435 error_report("RP: Sibling indicated error %d", sibling_error
);
1436 mark_source_rp_bad(ms
);
1439 * We'll let the main thread deal with closing the RP
1440 * we could do a shutdown(2) on it, but we're the only user
1441 * anyway, so there's nothing gained.
1445 case MIG_RP_MSG_PONG
:
1446 tmp32
= ldl_be_p(buf
);
1447 trace_source_return_path_thread_pong(tmp32
);
1450 case MIG_RP_MSG_REQ_PAGES
:
1451 start
= ldq_be_p(buf
);
1452 len
= ldl_be_p(buf
+ 8);
1453 migrate_handle_rp_req_pages(ms
, NULL
, start
, len
);
1456 case MIG_RP_MSG_REQ_PAGES_ID
:
1457 expected_len
= 12 + 1; /* header + termination */
1459 if (header_len
>= expected_len
) {
1460 start
= ldq_be_p(buf
);
1461 len
= ldl_be_p(buf
+ 8);
1462 /* Now we expect an idstr */
1463 tmp32
= buf
[12]; /* Length of the following idstr */
1464 buf
[13 + tmp32
] = '\0';
1465 expected_len
+= tmp32
;
1467 if (header_len
!= expected_len
) {
1468 error_report("RP: Req_Page_id with length %d expecting %zd",
1469 header_len
, expected_len
);
1470 mark_source_rp_bad(ms
);
1473 migrate_handle_rp_req_pages(ms
, (char *)&buf
[13], start
, len
);
1480 if (qemu_file_get_error(rp
)) {
1481 trace_source_return_path_thread_bad_end();
1482 mark_source_rp_bad(ms
);
1485 trace_source_return_path_thread_end();
1487 ms
->rp_state
.from_dst_file
= NULL
;
1492 static int open_return_path_on_source(MigrationState
*ms
)
1495 ms
->rp_state
.from_dst_file
= qemu_file_get_return_path(ms
->to_dst_file
);
1496 if (!ms
->rp_state
.from_dst_file
) {
1500 trace_open_return_path_on_source();
1501 qemu_thread_create(&ms
->rp_state
.rp_thread
, "return path",
1502 source_return_path_thread
, ms
, QEMU_THREAD_JOINABLE
);
1504 trace_open_return_path_on_source_continue();
1509 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1510 static int await_return_path_close_on_source(MigrationState
*ms
)
1513 * If this is a normal exit then the destination will send a SHUT and the
1514 * rp_thread will exit, however if there's an error we need to cause
1517 if (qemu_file_get_error(ms
->to_dst_file
) && ms
->rp_state
.from_dst_file
) {
1519 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1520 * waiting for the destination.
1522 qemu_file_shutdown(ms
->rp_state
.from_dst_file
);
1523 mark_source_rp_bad(ms
);
1525 trace_await_return_path_close_on_source_joining();
1526 qemu_thread_join(&ms
->rp_state
.rp_thread
);
1527 trace_await_return_path_close_on_source_close();
1528 return ms
->rp_state
.error
;
1532 * Switch from normal iteration to postcopy
1533 * Returns non-0 on error
1535 static int postcopy_start(MigrationState
*ms
, bool *old_vm_running
)
1538 QIOChannelBuffer
*bioc
;
1540 int64_t time_at_stop
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1541 migrate_set_state(&ms
->state
, MIGRATION_STATUS_ACTIVE
,
1542 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
1544 trace_postcopy_start();
1545 qemu_mutex_lock_iothread();
1546 trace_postcopy_start_set_run();
1548 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
1549 *old_vm_running
= runstate_is_running();
1550 global_state_store();
1551 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
1556 ret
= bdrv_inactivate_all();
1562 * Cause any non-postcopiable, but iterative devices to
1563 * send out their final data.
1565 qemu_savevm_state_complete_precopy(ms
->to_dst_file
, true);
1568 * in Finish migrate and with the io-lock held everything should
1569 * be quiet, but we've potentially still got dirty pages and we
1570 * need to tell the destination to throw any pages it's already received
1573 if (ram_postcopy_send_discard_bitmap(ms
)) {
1574 error_report("postcopy send discard bitmap failed");
1579 * send rest of state - note things that are doing postcopy
1580 * will notice we're in POSTCOPY_ACTIVE and not actually
1581 * wrap their state up here
1583 qemu_file_set_rate_limit(ms
->to_dst_file
, INT64_MAX
);
1584 /* Ping just for debugging, helps line traces up */
1585 qemu_savevm_send_ping(ms
->to_dst_file
, 2);
1588 * While loading the device state we may trigger page transfer
1589 * requests and the fd must be free to process those, and thus
1590 * the destination must read the whole device state off the fd before
1591 * it starts processing it. Unfortunately the ad-hoc migration format
1592 * doesn't allow the destination to know the size to read without fully
1593 * parsing it through each devices load-state code (especially the open
1594 * coded devices that use get/put).
1595 * So we wrap the device state up in a package with a length at the start;
1596 * to do this we use a qemu_buf to hold the whole of the device state.
1598 bioc
= qio_channel_buffer_new(4096);
1599 qio_channel_set_name(QIO_CHANNEL(bioc
), "migration-postcopy-buffer");
1600 fb
= qemu_fopen_channel_output(QIO_CHANNEL(bioc
));
1601 object_unref(OBJECT(bioc
));
1604 * Make sure the receiver can get incoming pages before we send the rest
1607 qemu_savevm_send_postcopy_listen(fb
);
1609 qemu_savevm_state_complete_precopy(fb
, false);
1610 qemu_savevm_send_ping(fb
, 3);
1612 qemu_savevm_send_postcopy_run(fb
);
1614 /* <><> end of stuff going into the package */
1616 /* Now send that blob */
1617 if (qemu_savevm_send_packaged(ms
->to_dst_file
, bioc
->data
, bioc
->usage
)) {
1622 /* Send a notify to give a chance for anything that needs to happen
1623 * at the transition to postcopy and after the device state; in particular
1624 * spice needs to trigger a transition now
1626 ms
->postcopy_after_devices
= true;
1627 notifier_list_notify(&migration_state_notifiers
, ms
);
1629 ms
->downtime
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) - time_at_stop
;
1631 qemu_mutex_unlock_iothread();
1634 * Although this ping is just for debug, it could potentially be
1635 * used for getting a better measurement of downtime at the source.
1637 qemu_savevm_send_ping(ms
->to_dst_file
, 4);
1639 ret
= qemu_file_get_error(ms
->to_dst_file
);
1641 error_report("postcopy_start: Migration stream errored");
1642 migrate_set_state(&ms
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
1643 MIGRATION_STATUS_FAILED
);
1651 migrate_set_state(&ms
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
1652 MIGRATION_STATUS_FAILED
);
1653 qemu_mutex_unlock_iothread();
1658 * migration_completion: Used by migration_thread when there's not much left.
1659 * The caller 'breaks' the loop when this returns.
1661 * @s: Current migration state
1662 * @current_active_state: The migration state we expect to be in
1663 * @*old_vm_running: Pointer to old_vm_running flag
1664 * @*start_time: Pointer to time to update
1666 static void migration_completion(MigrationState
*s
, int current_active_state
,
1667 bool *old_vm_running
,
1668 int64_t *start_time
)
1672 if (s
->state
== MIGRATION_STATUS_ACTIVE
) {
1673 qemu_mutex_lock_iothread();
1674 *start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1675 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
1676 *old_vm_running
= runstate_is_running();
1677 ret
= global_state_store();
1680 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
1682 * Don't mark the image with BDRV_O_INACTIVE flag if
1683 * we will go into COLO stage later.
1685 if (ret
>= 0 && !migrate_colo_enabled()) {
1686 ret
= bdrv_inactivate_all();
1689 qemu_file_set_rate_limit(s
->to_dst_file
, INT64_MAX
);
1690 qemu_savevm_state_complete_precopy(s
->to_dst_file
, false);
1693 qemu_mutex_unlock_iothread();
1698 } else if (s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
1699 trace_migration_completion_postcopy_end();
1701 qemu_savevm_state_complete_postcopy(s
->to_dst_file
);
1702 trace_migration_completion_postcopy_end_after_complete();
1706 * If rp was opened we must clean up the thread before
1707 * cleaning everything else up (since if there are no failures
1708 * it will wait for the destination to send it's status in
1710 * Postcopy opens rp if enabled (even if it's not avtivated)
1712 if (migrate_postcopy_ram()) {
1714 trace_migration_completion_postcopy_end_before_rp();
1715 rp_error
= await_return_path_close_on_source(s
);
1716 trace_migration_completion_postcopy_end_after_rp(rp_error
);
1718 goto fail_invalidate
;
1722 if (qemu_file_get_error(s
->to_dst_file
)) {
1723 trace_migration_completion_file_err();
1724 goto fail_invalidate
;
1727 if (!migrate_colo_enabled()) {
1728 migrate_set_state(&s
->state
, current_active_state
,
1729 MIGRATION_STATUS_COMPLETED
);
1735 /* If not doing postcopy, vm_start() will be called: let's regain
1736 * control on images.
1738 if (s
->state
== MIGRATION_STATUS_ACTIVE
) {
1739 Error
*local_err
= NULL
;
1741 bdrv_invalidate_cache_all(&local_err
);
1743 error_report_err(local_err
);
1748 migrate_set_state(&s
->state
, current_active_state
,
1749 MIGRATION_STATUS_FAILED
);
1752 bool migrate_colo_enabled(void)
1754 MigrationState
*s
= migrate_get_current();
1755 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_X_COLO
];
1759 * Master migration thread on the source VM.
1760 * It drives the migration and pumps the data down the outgoing channel.
1762 static void *migration_thread(void *opaque
)
1764 MigrationState
*s
= opaque
;
1765 /* Used by the bandwidth calcs, updated later */
1766 int64_t initial_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1767 int64_t setup_start
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
1768 int64_t initial_bytes
= 0;
1769 int64_t max_size
= 0;
1770 int64_t start_time
= initial_time
;
1772 bool old_vm_running
= false;
1773 bool entered_postcopy
= false;
1774 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1775 enum MigrationStatus current_active_state
= MIGRATION_STATUS_ACTIVE
;
1776 bool enable_colo
= migrate_colo_enabled();
1778 rcu_register_thread();
1780 qemu_savevm_state_header(s
->to_dst_file
);
1782 if (migrate_postcopy_ram()) {
1783 /* Now tell the dest that it should open its end so it can reply */
1784 qemu_savevm_send_open_return_path(s
->to_dst_file
);
1786 /* And do a ping that will make stuff easier to debug */
1787 qemu_savevm_send_ping(s
->to_dst_file
, 1);
1790 * Tell the destination that we *might* want to do postcopy later;
1791 * if the other end can't do postcopy it should fail now, nice and
1794 qemu_savevm_send_postcopy_advise(s
->to_dst_file
);
1797 qemu_savevm_state_begin(s
->to_dst_file
, &s
->params
);
1799 s
->setup_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
) - setup_start
;
1800 current_active_state
= MIGRATION_STATUS_ACTIVE
;
1801 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
1802 MIGRATION_STATUS_ACTIVE
);
1804 trace_migration_thread_setup_complete();
1806 while (s
->state
== MIGRATION_STATUS_ACTIVE
||
1807 s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
1808 int64_t current_time
;
1809 uint64_t pending_size
;
1811 if (!qemu_file_rate_limit(s
->to_dst_file
)) {
1812 uint64_t pend_post
, pend_nonpost
;
1814 qemu_savevm_state_pending(s
->to_dst_file
, max_size
, &pend_nonpost
,
1816 pending_size
= pend_nonpost
+ pend_post
;
1817 trace_migrate_pending(pending_size
, max_size
,
1818 pend_post
, pend_nonpost
);
1819 if (pending_size
&& pending_size
>= max_size
) {
1820 /* Still a significant amount to transfer */
1822 if (migrate_postcopy_ram() &&
1823 s
->state
!= MIGRATION_STATUS_POSTCOPY_ACTIVE
&&
1824 pend_nonpost
<= max_size
&&
1825 atomic_read(&s
->start_postcopy
)) {
1827 if (!postcopy_start(s
, &old_vm_running
)) {
1828 current_active_state
= MIGRATION_STATUS_POSTCOPY_ACTIVE
;
1829 entered_postcopy
= true;
1834 /* Just another iteration step */
1835 qemu_savevm_state_iterate(s
->to_dst_file
, entered_postcopy
);
1837 trace_migration_thread_low_pending(pending_size
);
1838 migration_completion(s
, current_active_state
,
1839 &old_vm_running
, &start_time
);
1844 if (qemu_file_get_error(s
->to_dst_file
)) {
1845 migrate_set_state(&s
->state
, current_active_state
,
1846 MIGRATION_STATUS_FAILED
);
1847 trace_migration_thread_file_err();
1850 current_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1851 if (current_time
>= initial_time
+ BUFFER_DELAY
) {
1852 uint64_t transferred_bytes
= qemu_ftell(s
->to_dst_file
) -
1854 uint64_t time_spent
= current_time
- initial_time
;
1855 double bandwidth
= (double)transferred_bytes
/ time_spent
;
1856 max_size
= bandwidth
* s
->parameters
.downtime_limit
;
1858 s
->mbps
= (((double) transferred_bytes
* 8.0) /
1859 ((double) time_spent
/ 1000.0)) / 1000.0 / 1000.0;
1861 trace_migrate_transferred(transferred_bytes
, time_spent
,
1862 bandwidth
, max_size
);
1863 /* if we haven't sent anything, we don't want to recalculate
1864 10000 is a small enough number for our purposes */
1865 if (s
->dirty_bytes_rate
&& transferred_bytes
> 10000) {
1866 s
->expected_downtime
= s
->dirty_bytes_rate
/ bandwidth
;
1869 qemu_file_reset_rate_limit(s
->to_dst_file
);
1870 initial_time
= current_time
;
1871 initial_bytes
= qemu_ftell(s
->to_dst_file
);
1873 if (qemu_file_rate_limit(s
->to_dst_file
)) {
1874 /* usleep expects microseconds */
1875 g_usleep((initial_time
+ BUFFER_DELAY
- current_time
)*1000);
1879 trace_migration_thread_after_loop();
1880 /* If we enabled cpu throttling for auto-converge, turn it off. */
1881 cpu_throttle_stop();
1882 end_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1884 qemu_mutex_lock_iothread();
1886 * The resource has been allocated by migration will be reused in COLO
1887 * process, so don't release them.
1890 qemu_savevm_state_cleanup();
1892 if (s
->state
== MIGRATION_STATUS_COMPLETED
) {
1893 uint64_t transferred_bytes
= qemu_ftell(s
->to_dst_file
);
1894 s
->total_time
= end_time
- s
->total_time
;
1895 if (!entered_postcopy
) {
1896 s
->downtime
= end_time
- start_time
;
1898 if (s
->total_time
) {
1899 s
->mbps
= (((double) transferred_bytes
* 8.0) /
1900 ((double) s
->total_time
)) / 1000;
1902 runstate_set(RUN_STATE_POSTMIGRATE
);
1904 if (s
->state
== MIGRATION_STATUS_ACTIVE
&& enable_colo
) {
1905 migrate_start_colo_process(s
);
1906 qemu_savevm_state_cleanup();
1908 * Fixme: we will run VM in COLO no matter its old running state.
1909 * After exited COLO, we will keep running.
1911 old_vm_running
= true;
1913 if (old_vm_running
&& !entered_postcopy
) {
1916 if (runstate_check(RUN_STATE_FINISH_MIGRATE
)) {
1917 runstate_set(RUN_STATE_POSTMIGRATE
);
1921 qemu_bh_schedule(s
->cleanup_bh
);
1922 qemu_mutex_unlock_iothread();
1924 rcu_unregister_thread();
1928 void migrate_fd_connect(MigrationState
*s
)
1930 s
->expected_downtime
= s
->parameters
.downtime_limit
;
1931 s
->cleanup_bh
= qemu_bh_new(migrate_fd_cleanup
, s
);
1933 qemu_file_set_blocking(s
->to_dst_file
, true);
1934 qemu_file_set_rate_limit(s
->to_dst_file
,
1935 s
->parameters
.max_bandwidth
/ XFER_LIMIT_RATIO
);
1937 /* Notify before starting migration thread */
1938 notifier_list_notify(&migration_state_notifiers
, s
);
1941 * Open the return path; currently for postcopy but other things might
1944 if (migrate_postcopy_ram()) {
1945 if (open_return_path_on_source(s
)) {
1946 error_report("Unable to open return-path for postcopy");
1947 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
1948 MIGRATION_STATUS_FAILED
);
1949 migrate_fd_cleanup(s
);
1954 migrate_compress_threads_create();
1955 qemu_thread_create(&s
->thread
, "migration", migration_thread
, s
,
1956 QEMU_THREAD_JOINABLE
);
1957 s
->migration_thread_running
= true;
1960 PostcopyState
postcopy_state_get(void)
1962 return atomic_mb_read(&incoming_postcopy_state
);
1965 /* Set the state and return the old state */
1966 PostcopyState
postcopy_state_set(PostcopyState new_state
)
1968 return atomic_xchg(&incoming_postcopy_state
, new_state
);