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-common.h"
17 #include "qemu/error-report.h"
18 #include "qemu/main-loop.h"
19 #include "migration/migration.h"
20 #include "migration/qemu-file.h"
21 #include "sysemu/sysemu.h"
22 #include "block/block.h"
23 #include "qapi/qmp/qerror.h"
24 #include "qemu/sockets.h"
26 #include "migration/block.h"
27 #include "migration/postcopy-ram.h"
28 #include "qemu/thread.h"
29 #include "qmp-commands.h"
31 #include "qapi/util.h"
32 #include "qapi-event.h"
35 #define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
37 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
39 #define BUFFER_DELAY 100
40 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
42 /* Default compression thread count */
43 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
44 /* Default decompression thread count, usually decompression is at
45 * least 4 times as fast as compression.*/
46 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
47 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
48 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
49 /* Define default autoconverge cpu throttle migration parameters */
50 #define DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL 20
51 #define DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT 10
53 /* Migration XBZRLE default cache size */
54 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
56 static NotifierList migration_state_notifiers
=
57 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers
);
59 static bool deferred_incoming
;
62 * Current state of incoming postcopy; note this is not part of
63 * MigrationIncomingState since it's state is used during cleanup
64 * at the end as MIS is being freed.
66 static PostcopyState incoming_postcopy_state
;
68 /* When we add fault tolerance, we could have several
69 migrations at once. For now we don't need to add
70 dynamic creation of migration */
73 MigrationState
*migrate_get_current(void)
75 static MigrationState current_migration
= {
76 .state
= MIGRATION_STATUS_NONE
,
77 .bandwidth_limit
= MAX_THROTTLE
,
78 .xbzrle_cache_size
= DEFAULT_MIGRATE_CACHE_SIZE
,
80 .parameters
[MIGRATION_PARAMETER_COMPRESS_LEVEL
] =
81 DEFAULT_MIGRATE_COMPRESS_LEVEL
,
82 .parameters
[MIGRATION_PARAMETER_COMPRESS_THREADS
] =
83 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT
,
84 .parameters
[MIGRATION_PARAMETER_DECOMPRESS_THREADS
] =
85 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT
,
86 .parameters
[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL
] =
87 DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL
,
88 .parameters
[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT
] =
89 DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT
,
92 return ¤t_migration
;
96 static MigrationIncomingState
*mis_current
;
98 MigrationIncomingState
*migration_incoming_get_current(void)
103 MigrationIncomingState
*migration_incoming_state_new(QEMUFile
* f
)
105 mis_current
= g_new0(MigrationIncomingState
, 1);
106 mis_current
->from_src_file
= f
;
107 QLIST_INIT(&mis_current
->loadvm_handlers
);
108 qemu_mutex_init(&mis_current
->rp_mutex
);
109 qemu_event_init(&mis_current
->main_thread_load_event
, false);
114 void migration_incoming_state_destroy(void)
116 qemu_event_destroy(&mis_current
->main_thread_load_event
);
117 loadvm_free_handlers(mis_current
);
126 uint8_t runstate
[100];
131 static GlobalState global_state
;
133 int global_state_store(void)
135 if (!runstate_store((char *)global_state
.runstate
,
136 sizeof(global_state
.runstate
))) {
137 error_report("runstate name too big: %s", global_state
.runstate
);
138 trace_migrate_state_too_big();
144 void global_state_store_running(void)
146 const char *state
= RunState_lookup
[RUN_STATE_RUNNING
];
147 strncpy((char *)global_state
.runstate
,
148 state
, sizeof(global_state
.runstate
));
151 static bool global_state_received(void)
153 return global_state
.received
;
156 static RunState
global_state_get_runstate(void)
158 return global_state
.state
;
161 void global_state_set_optional(void)
163 global_state
.optional
= true;
166 static bool global_state_needed(void *opaque
)
168 GlobalState
*s
= opaque
;
169 char *runstate
= (char *)s
->runstate
;
171 /* If it is not optional, it is mandatory */
173 if (s
->optional
== false) {
177 /* If state is running or paused, it is not needed */
179 if (strcmp(runstate
, "running") == 0 ||
180 strcmp(runstate
, "paused") == 0) {
184 /* for any other state it is needed */
188 static int global_state_post_load(void *opaque
, int version_id
)
190 GlobalState
*s
= opaque
;
191 Error
*local_err
= NULL
;
193 char *runstate
= (char *)s
->runstate
;
196 trace_migrate_global_state_post_load(runstate
);
198 r
= qapi_enum_parse(RunState_lookup
, runstate
, RUN_STATE_MAX
,
203 error_report_err(local_err
);
212 static void global_state_pre_save(void *opaque
)
214 GlobalState
*s
= opaque
;
216 trace_migrate_global_state_pre_save((char *)s
->runstate
);
217 s
->size
= strlen((char *)s
->runstate
) + 1;
220 static const VMStateDescription vmstate_globalstate
= {
221 .name
= "globalstate",
223 .minimum_version_id
= 1,
224 .post_load
= global_state_post_load
,
225 .pre_save
= global_state_pre_save
,
226 .needed
= global_state_needed
,
227 .fields
= (VMStateField
[]) {
228 VMSTATE_UINT32(size
, GlobalState
),
229 VMSTATE_BUFFER(runstate
, GlobalState
),
230 VMSTATE_END_OF_LIST()
234 void register_global_state(void)
236 /* We would use it independently that we receive it */
237 strcpy((char *)&global_state
.runstate
, "");
238 global_state
.received
= false;
239 vmstate_register(NULL
, 0, &vmstate_globalstate
, &global_state
);
242 static void migrate_generate_event(int new_state
)
244 if (migrate_use_events()) {
245 qapi_event_send_migration(new_state
, &error_abort
);
250 * Called on -incoming with a defer: uri.
251 * The migration can be started later after any parameters have been
254 static void deferred_incoming_migration(Error
**errp
)
256 if (deferred_incoming
) {
257 error_setg(errp
, "Incoming migration already deferred");
259 deferred_incoming
= true;
262 void qemu_start_incoming_migration(const char *uri
, Error
**errp
)
266 qapi_event_send_migration(MIGRATION_STATUS_SETUP
, &error_abort
);
267 if (!strcmp(uri
, "defer")) {
268 deferred_incoming_migration(errp
);
269 } else if (strstart(uri
, "tcp:", &p
)) {
270 tcp_start_incoming_migration(p
, errp
);
272 } else if (strstart(uri
, "rdma:", &p
)) {
273 rdma_start_incoming_migration(p
, errp
);
276 } else if (strstart(uri
, "exec:", &p
)) {
277 exec_start_incoming_migration(p
, errp
);
278 } else if (strstart(uri
, "unix:", &p
)) {
279 unix_start_incoming_migration(p
, errp
);
280 } else if (strstart(uri
, "fd:", &p
)) {
281 fd_start_incoming_migration(p
, errp
);
284 error_setg(errp
, "unknown migration protocol: %s", uri
);
288 static void process_incoming_migration_co(void *opaque
)
290 QEMUFile
*f
= opaque
;
291 Error
*local_err
= NULL
;
294 migration_incoming_state_new(f
);
295 postcopy_state_set(POSTCOPY_INCOMING_NONE
);
296 migrate_generate_event(MIGRATION_STATUS_ACTIVE
);
297 ret
= qemu_loadvm_state(f
);
300 free_xbzrle_decoded_buf();
301 migration_incoming_state_destroy();
304 migrate_generate_event(MIGRATION_STATUS_FAILED
);
305 error_report("load of migration failed: %s", strerror(-ret
));
306 migrate_decompress_threads_join();
310 /* Make sure all file formats flush their mutable metadata */
311 bdrv_invalidate_cache_all(&local_err
);
313 migrate_generate_event(MIGRATION_STATUS_FAILED
);
314 error_report_err(local_err
);
315 migrate_decompress_threads_join();
320 * This must happen after all error conditions are dealt with and
321 * we're sure the VM is going to be running on this host.
323 qemu_announce_self();
325 /* If global state section was not received or we are in running
326 state, we need to obey autostart. Any other state is set with
329 if (!global_state_received() ||
330 global_state_get_runstate() == RUN_STATE_RUNNING
) {
334 runstate_set(RUN_STATE_PAUSED
);
337 runstate_set(global_state_get_runstate());
339 migrate_decompress_threads_join();
341 * This must happen after any state changes since as soon as an external
342 * observer sees this event they might start to prod at the VM assuming
345 migrate_generate_event(MIGRATION_STATUS_COMPLETED
);
348 void process_incoming_migration(QEMUFile
*f
)
350 Coroutine
*co
= qemu_coroutine_create(process_incoming_migration_co
);
351 int fd
= qemu_get_fd(f
);
354 migrate_decompress_threads_create();
355 qemu_set_nonblock(fd
);
356 qemu_coroutine_enter(co
, f
);
360 * Send a message on the return channel back to the source
363 void migrate_send_rp_message(MigrationIncomingState
*mis
,
364 enum mig_rp_message_type message_type
,
365 uint16_t len
, void *data
)
367 trace_migrate_send_rp_message((int)message_type
, len
);
368 qemu_mutex_lock(&mis
->rp_mutex
);
369 qemu_put_be16(mis
->to_src_file
, (unsigned int)message_type
);
370 qemu_put_be16(mis
->to_src_file
, len
);
371 qemu_put_buffer(mis
->to_src_file
, data
, len
);
372 qemu_fflush(mis
->to_src_file
);
373 qemu_mutex_unlock(&mis
->rp_mutex
);
377 * Send a 'SHUT' message on the return channel with the given value
378 * to indicate that we've finished with the RP. Non-0 value indicates
381 void migrate_send_rp_shut(MigrationIncomingState
*mis
,
386 buf
= cpu_to_be32(value
);
387 migrate_send_rp_message(mis
, MIG_RP_MSG_SHUT
, sizeof(buf
), &buf
);
391 * Send a 'PONG' message on the return channel with the given value
392 * (normally in response to a 'PING')
394 void migrate_send_rp_pong(MigrationIncomingState
*mis
,
399 buf
= cpu_to_be32(value
);
400 migrate_send_rp_message(mis
, MIG_RP_MSG_PONG
, sizeof(buf
), &buf
);
403 /* amount of nanoseconds we are willing to wait for migration to be down.
404 * the choice of nanoseconds is because it is the maximum resolution that
405 * get_clock() can achieve. It is an internal measure. All user-visible
406 * units must be in seconds */
407 static uint64_t max_downtime
= 300000000;
409 uint64_t migrate_max_downtime(void)
414 MigrationCapabilityStatusList
*qmp_query_migrate_capabilities(Error
**errp
)
416 MigrationCapabilityStatusList
*head
= NULL
;
417 MigrationCapabilityStatusList
*caps
;
418 MigrationState
*s
= migrate_get_current();
421 caps
= NULL
; /* silence compiler warning */
422 for (i
= 0; i
< MIGRATION_CAPABILITY_MAX
; i
++) {
424 head
= g_malloc0(sizeof(*caps
));
427 caps
->next
= g_malloc0(sizeof(*caps
));
431 g_malloc(sizeof(*caps
->value
));
432 caps
->value
->capability
= i
;
433 caps
->value
->state
= s
->enabled_capabilities
[i
];
439 MigrationParameters
*qmp_query_migrate_parameters(Error
**errp
)
441 MigrationParameters
*params
;
442 MigrationState
*s
= migrate_get_current();
444 params
= g_malloc0(sizeof(*params
));
445 params
->compress_level
= s
->parameters
[MIGRATION_PARAMETER_COMPRESS_LEVEL
];
446 params
->compress_threads
=
447 s
->parameters
[MIGRATION_PARAMETER_COMPRESS_THREADS
];
448 params
->decompress_threads
=
449 s
->parameters
[MIGRATION_PARAMETER_DECOMPRESS_THREADS
];
450 params
->x_cpu_throttle_initial
=
451 s
->parameters
[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL
];
452 params
->x_cpu_throttle_increment
=
453 s
->parameters
[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT
];
459 * Return true if we're already in the middle of a migration
460 * (i.e. any of the active or setup states)
462 static bool migration_is_setup_or_active(int state
)
465 case MIGRATION_STATUS_ACTIVE
:
466 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
467 case MIGRATION_STATUS_SETUP
:
476 static void get_xbzrle_cache_stats(MigrationInfo
*info
)
478 if (migrate_use_xbzrle()) {
479 info
->has_xbzrle_cache
= true;
480 info
->xbzrle_cache
= g_malloc0(sizeof(*info
->xbzrle_cache
));
481 info
->xbzrle_cache
->cache_size
= migrate_xbzrle_cache_size();
482 info
->xbzrle_cache
->bytes
= xbzrle_mig_bytes_transferred();
483 info
->xbzrle_cache
->pages
= xbzrle_mig_pages_transferred();
484 info
->xbzrle_cache
->cache_miss
= xbzrle_mig_pages_cache_miss();
485 info
->xbzrle_cache
->cache_miss_rate
= xbzrle_mig_cache_miss_rate();
486 info
->xbzrle_cache
->overflow
= xbzrle_mig_pages_overflow();
490 MigrationInfo
*qmp_query_migrate(Error
**errp
)
492 MigrationInfo
*info
= g_malloc0(sizeof(*info
));
493 MigrationState
*s
= migrate_get_current();
496 case MIGRATION_STATUS_NONE
:
497 /* no migration has happened ever */
499 case MIGRATION_STATUS_SETUP
:
500 info
->has_status
= true;
501 info
->has_total_time
= false;
503 case MIGRATION_STATUS_ACTIVE
:
504 case MIGRATION_STATUS_CANCELLING
:
505 info
->has_status
= true;
506 info
->has_total_time
= true;
507 info
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
509 info
->has_expected_downtime
= true;
510 info
->expected_downtime
= s
->expected_downtime
;
511 info
->has_setup_time
= true;
512 info
->setup_time
= s
->setup_time
;
514 info
->has_ram
= true;
515 info
->ram
= g_malloc0(sizeof(*info
->ram
));
516 info
->ram
->transferred
= ram_bytes_transferred();
517 info
->ram
->remaining
= ram_bytes_remaining();
518 info
->ram
->total
= ram_bytes_total();
519 info
->ram
->duplicate
= dup_mig_pages_transferred();
520 info
->ram
->skipped
= skipped_mig_pages_transferred();
521 info
->ram
->normal
= norm_mig_pages_transferred();
522 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
523 info
->ram
->dirty_pages_rate
= s
->dirty_pages_rate
;
524 info
->ram
->mbps
= s
->mbps
;
525 info
->ram
->dirty_sync_count
= s
->dirty_sync_count
;
527 if (blk_mig_active()) {
528 info
->has_disk
= true;
529 info
->disk
= g_malloc0(sizeof(*info
->disk
));
530 info
->disk
->transferred
= blk_mig_bytes_transferred();
531 info
->disk
->remaining
= blk_mig_bytes_remaining();
532 info
->disk
->total
= blk_mig_bytes_total();
535 if (cpu_throttle_active()) {
536 info
->has_x_cpu_throttle_percentage
= true;
537 info
->x_cpu_throttle_percentage
= cpu_throttle_get_percentage();
540 get_xbzrle_cache_stats(info
);
542 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
543 /* Mostly the same as active; TODO add some postcopy stats */
544 info
->has_status
= true;
545 info
->has_total_time
= true;
546 info
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
548 info
->has_expected_downtime
= true;
549 info
->expected_downtime
= s
->expected_downtime
;
550 info
->has_setup_time
= true;
551 info
->setup_time
= s
->setup_time
;
553 info
->has_ram
= true;
554 info
->ram
= g_malloc0(sizeof(*info
->ram
));
555 info
->ram
->transferred
= ram_bytes_transferred();
556 info
->ram
->remaining
= ram_bytes_remaining();
557 info
->ram
->total
= ram_bytes_total();
558 info
->ram
->duplicate
= dup_mig_pages_transferred();
559 info
->ram
->skipped
= skipped_mig_pages_transferred();
560 info
->ram
->normal
= norm_mig_pages_transferred();
561 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
562 info
->ram
->dirty_pages_rate
= s
->dirty_pages_rate
;
563 info
->ram
->mbps
= s
->mbps
;
565 if (blk_mig_active()) {
566 info
->has_disk
= true;
567 info
->disk
= g_malloc0(sizeof(*info
->disk
));
568 info
->disk
->transferred
= blk_mig_bytes_transferred();
569 info
->disk
->remaining
= blk_mig_bytes_remaining();
570 info
->disk
->total
= blk_mig_bytes_total();
573 get_xbzrle_cache_stats(info
);
575 case MIGRATION_STATUS_COMPLETED
:
576 get_xbzrle_cache_stats(info
);
578 info
->has_status
= true;
579 info
->has_total_time
= true;
580 info
->total_time
= s
->total_time
;
581 info
->has_downtime
= true;
582 info
->downtime
= s
->downtime
;
583 info
->has_setup_time
= true;
584 info
->setup_time
= s
->setup_time
;
586 info
->has_ram
= true;
587 info
->ram
= g_malloc0(sizeof(*info
->ram
));
588 info
->ram
->transferred
= ram_bytes_transferred();
589 info
->ram
->remaining
= 0;
590 info
->ram
->total
= ram_bytes_total();
591 info
->ram
->duplicate
= dup_mig_pages_transferred();
592 info
->ram
->skipped
= skipped_mig_pages_transferred();
593 info
->ram
->normal
= norm_mig_pages_transferred();
594 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
595 info
->ram
->mbps
= s
->mbps
;
596 info
->ram
->dirty_sync_count
= s
->dirty_sync_count
;
598 case MIGRATION_STATUS_FAILED
:
599 info
->has_status
= true;
601 case MIGRATION_STATUS_CANCELLED
:
602 info
->has_status
= true;
605 info
->status
= s
->state
;
610 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList
*params
,
613 MigrationState
*s
= migrate_get_current();
614 MigrationCapabilityStatusList
*cap
;
616 if (migration_is_setup_or_active(s
->state
)) {
617 error_setg(errp
, QERR_MIGRATION_ACTIVE
);
621 for (cap
= params
; cap
; cap
= cap
->next
) {
622 s
->enabled_capabilities
[cap
->value
->capability
] = cap
->value
->state
;
625 if (migrate_postcopy_ram()) {
626 if (migrate_use_compression()) {
627 /* The decompression threads asynchronously write into RAM
628 * rather than use the atomic copies needed to avoid
629 * userfaulting. It should be possible to fix the decompression
630 * threads for compatibility in future.
632 error_report("Postcopy is not currently compatible with "
634 s
->enabled_capabilities
[MIGRATION_CAPABILITY_X_POSTCOPY_RAM
] =
640 void qmp_migrate_set_parameters(bool has_compress_level
,
641 int64_t compress_level
,
642 bool has_compress_threads
,
643 int64_t compress_threads
,
644 bool has_decompress_threads
,
645 int64_t decompress_threads
,
646 bool has_x_cpu_throttle_initial
,
647 int64_t x_cpu_throttle_initial
,
648 bool has_x_cpu_throttle_increment
,
649 int64_t x_cpu_throttle_increment
, Error
**errp
)
651 MigrationState
*s
= migrate_get_current();
653 if (has_compress_level
&& (compress_level
< 0 || compress_level
> 9)) {
654 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "compress_level",
655 "is invalid, it should be in the range of 0 to 9");
658 if (has_compress_threads
&&
659 (compress_threads
< 1 || compress_threads
> 255)) {
660 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
662 "is invalid, it should be in the range of 1 to 255");
665 if (has_decompress_threads
&&
666 (decompress_threads
< 1 || decompress_threads
> 255)) {
667 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
668 "decompress_threads",
669 "is invalid, it should be in the range of 1 to 255");
672 if (has_x_cpu_throttle_initial
&&
673 (x_cpu_throttle_initial
< 1 || x_cpu_throttle_initial
> 99)) {
674 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
675 "x_cpu_throttle_initial",
676 "an integer in the range of 1 to 99");
678 if (has_x_cpu_throttle_increment
&&
679 (x_cpu_throttle_increment
< 1 || x_cpu_throttle_increment
> 99)) {
680 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
681 "x_cpu_throttle_increment",
682 "an integer in the range of 1 to 99");
685 if (has_compress_level
) {
686 s
->parameters
[MIGRATION_PARAMETER_COMPRESS_LEVEL
] = compress_level
;
688 if (has_compress_threads
) {
689 s
->parameters
[MIGRATION_PARAMETER_COMPRESS_THREADS
] = compress_threads
;
691 if (has_decompress_threads
) {
692 s
->parameters
[MIGRATION_PARAMETER_DECOMPRESS_THREADS
] =
695 if (has_x_cpu_throttle_initial
) {
696 s
->parameters
[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL
] =
697 x_cpu_throttle_initial
;
700 if (has_x_cpu_throttle_increment
) {
701 s
->parameters
[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT
] =
702 x_cpu_throttle_increment
;
706 void qmp_migrate_start_postcopy(Error
**errp
)
708 MigrationState
*s
= migrate_get_current();
710 if (!migrate_postcopy_ram()) {
711 error_setg(errp
, "Enable postcopy with migration_set_capability before"
712 " the start of migration");
716 if (s
->state
== MIGRATION_STATUS_NONE
) {
717 error_setg(errp
, "Postcopy must be started after migration has been"
722 * we don't error if migration has finished since that would be racy
723 * with issuing this command.
725 atomic_set(&s
->start_postcopy
, true);
728 /* shared migration helpers */
730 static void migrate_set_state(MigrationState
*s
, int old_state
, int new_state
)
732 if (atomic_cmpxchg(&s
->state
, old_state
, new_state
) == old_state
) {
733 trace_migrate_set_state(new_state
);
734 migrate_generate_event(new_state
);
738 static void migrate_fd_cleanup(void *opaque
)
740 MigrationState
*s
= opaque
;
742 qemu_bh_delete(s
->cleanup_bh
);
743 s
->cleanup_bh
= NULL
;
746 trace_migrate_fd_cleanup();
747 qemu_mutex_unlock_iothread();
748 if (s
->migration_thread_running
) {
749 qemu_thread_join(&s
->thread
);
750 s
->migration_thread_running
= false;
752 qemu_mutex_lock_iothread();
754 migrate_compress_threads_join();
755 qemu_fclose(s
->file
);
759 assert((s
->state
!= MIGRATION_STATUS_ACTIVE
) &&
760 (s
->state
!= MIGRATION_STATUS_POSTCOPY_ACTIVE
));
762 if (s
->state
== MIGRATION_STATUS_CANCELLING
) {
763 migrate_set_state(s
, MIGRATION_STATUS_CANCELLING
,
764 MIGRATION_STATUS_CANCELLED
);
767 notifier_list_notify(&migration_state_notifiers
, s
);
770 void migrate_fd_error(MigrationState
*s
)
772 trace_migrate_fd_error();
773 assert(s
->file
== NULL
);
774 migrate_set_state(s
, MIGRATION_STATUS_SETUP
, MIGRATION_STATUS_FAILED
);
775 notifier_list_notify(&migration_state_notifiers
, s
);
778 static void migrate_fd_cancel(MigrationState
*s
)
781 QEMUFile
*f
= migrate_get_current()->file
;
782 trace_migrate_fd_cancel();
784 if (s
->rp_state
.from_dst_file
) {
785 /* shutdown the rp socket, so causing the rp thread to shutdown */
786 qemu_file_shutdown(s
->rp_state
.from_dst_file
);
790 old_state
= s
->state
;
791 if (!migration_is_setup_or_active(old_state
)) {
794 migrate_set_state(s
, old_state
, MIGRATION_STATUS_CANCELLING
);
795 } while (s
->state
!= MIGRATION_STATUS_CANCELLING
);
798 * If we're unlucky the migration code might be stuck somewhere in a
799 * send/write while the network has failed and is waiting to timeout;
800 * if we've got shutdown(2) available then we can force it to quit.
801 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
802 * called in a bh, so there is no race against this cancel.
804 if (s
->state
== MIGRATION_STATUS_CANCELLING
&& f
) {
805 qemu_file_shutdown(f
);
809 void add_migration_state_change_notifier(Notifier
*notify
)
811 notifier_list_add(&migration_state_notifiers
, notify
);
814 void remove_migration_state_change_notifier(Notifier
*notify
)
816 notifier_remove(notify
);
819 bool migration_in_setup(MigrationState
*s
)
821 return s
->state
== MIGRATION_STATUS_SETUP
;
824 bool migration_has_finished(MigrationState
*s
)
826 return s
->state
== MIGRATION_STATUS_COMPLETED
;
829 bool migration_has_failed(MigrationState
*s
)
831 return (s
->state
== MIGRATION_STATUS_CANCELLED
||
832 s
->state
== MIGRATION_STATUS_FAILED
);
835 bool migration_in_postcopy(MigrationState
*s
)
837 return (s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
);
840 MigrationState
*migrate_init(const MigrationParams
*params
)
842 MigrationState
*s
= migrate_get_current();
843 int64_t bandwidth_limit
= s
->bandwidth_limit
;
844 bool enabled_capabilities
[MIGRATION_CAPABILITY_MAX
];
845 int64_t xbzrle_cache_size
= s
->xbzrle_cache_size
;
846 int compress_level
= s
->parameters
[MIGRATION_PARAMETER_COMPRESS_LEVEL
];
847 int compress_thread_count
=
848 s
->parameters
[MIGRATION_PARAMETER_COMPRESS_THREADS
];
849 int decompress_thread_count
=
850 s
->parameters
[MIGRATION_PARAMETER_DECOMPRESS_THREADS
];
851 int x_cpu_throttle_initial
=
852 s
->parameters
[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL
];
853 int x_cpu_throttle_increment
=
854 s
->parameters
[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT
];
856 memcpy(enabled_capabilities
, s
->enabled_capabilities
,
857 sizeof(enabled_capabilities
));
859 memset(s
, 0, sizeof(*s
));
861 memcpy(s
->enabled_capabilities
, enabled_capabilities
,
862 sizeof(enabled_capabilities
));
863 s
->xbzrle_cache_size
= xbzrle_cache_size
;
865 s
->parameters
[MIGRATION_PARAMETER_COMPRESS_LEVEL
] = compress_level
;
866 s
->parameters
[MIGRATION_PARAMETER_COMPRESS_THREADS
] =
867 compress_thread_count
;
868 s
->parameters
[MIGRATION_PARAMETER_DECOMPRESS_THREADS
] =
869 decompress_thread_count
;
870 s
->parameters
[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL
] =
871 x_cpu_throttle_initial
;
872 s
->parameters
[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT
] =
873 x_cpu_throttle_increment
;
874 s
->bandwidth_limit
= bandwidth_limit
;
875 migrate_set_state(s
, MIGRATION_STATUS_NONE
, MIGRATION_STATUS_SETUP
);
877 s
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
881 static GSList
*migration_blockers
;
883 void migrate_add_blocker(Error
*reason
)
885 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
888 void migrate_del_blocker(Error
*reason
)
890 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
893 void qmp_migrate_incoming(const char *uri
, Error
**errp
)
895 Error
*local_err
= NULL
;
896 static bool once
= true;
898 if (!deferred_incoming
) {
899 error_setg(errp
, "For use with '-incoming defer'");
903 error_setg(errp
, "The incoming migration has already been started");
906 qemu_start_incoming_migration(uri
, &local_err
);
909 error_propagate(errp
, local_err
);
916 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
917 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
920 Error
*local_err
= NULL
;
921 MigrationState
*s
= migrate_get_current();
922 MigrationParams params
;
925 params
.blk
= has_blk
&& blk
;
926 params
.shared
= has_inc
&& inc
;
928 if (migration_is_setup_or_active(s
->state
) ||
929 s
->state
== MIGRATION_STATUS_CANCELLING
) {
930 error_setg(errp
, QERR_MIGRATION_ACTIVE
);
933 if (runstate_check(RUN_STATE_INMIGRATE
)) {
934 error_setg(errp
, "Guest is waiting for an incoming migration");
938 if (qemu_savevm_state_blocked(errp
)) {
942 if (migration_blockers
) {
943 *errp
= error_copy(migration_blockers
->data
);
947 /* We are starting a new migration, so we want to start in a clean
948 state. This change is only needed if previous migration
949 failed/was cancelled. We don't use migrate_set_state() because
950 we are setting the initial state, not changing it. */
951 s
->state
= MIGRATION_STATUS_NONE
;
953 s
= migrate_init(¶ms
);
955 if (strstart(uri
, "tcp:", &p
)) {
956 tcp_start_outgoing_migration(s
, p
, &local_err
);
958 } else if (strstart(uri
, "rdma:", &p
)) {
959 rdma_start_outgoing_migration(s
, p
, &local_err
);
962 } else if (strstart(uri
, "exec:", &p
)) {
963 exec_start_outgoing_migration(s
, p
, &local_err
);
964 } else if (strstart(uri
, "unix:", &p
)) {
965 unix_start_outgoing_migration(s
, p
, &local_err
);
966 } else if (strstart(uri
, "fd:", &p
)) {
967 fd_start_outgoing_migration(s
, p
, &local_err
);
970 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri",
971 "a valid migration protocol");
972 migrate_set_state(s
, MIGRATION_STATUS_SETUP
, MIGRATION_STATUS_FAILED
);
978 error_propagate(errp
, local_err
);
983 void qmp_migrate_cancel(Error
**errp
)
985 migrate_fd_cancel(migrate_get_current());
988 void qmp_migrate_set_cache_size(int64_t value
, Error
**errp
)
990 MigrationState
*s
= migrate_get_current();
993 /* Check for truncation */
994 if (value
!= (size_t)value
) {
995 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
996 "exceeding address space");
1000 /* Cache should not be larger than guest ram size */
1001 if (value
> ram_bytes_total()) {
1002 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
1003 "exceeds guest ram size ");
1007 new_size
= xbzrle_cache_resize(value
);
1009 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
1010 "is smaller than page size");
1014 s
->xbzrle_cache_size
= new_size
;
1017 int64_t qmp_query_migrate_cache_size(Error
**errp
)
1019 return migrate_xbzrle_cache_size();
1022 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
1029 if (value
> SIZE_MAX
) {
1033 s
= migrate_get_current();
1034 s
->bandwidth_limit
= value
;
1036 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
/ XFER_LIMIT_RATIO
);
1040 void qmp_migrate_set_downtime(double value
, Error
**errp
)
1043 value
= MAX(0, MIN(UINT64_MAX
, value
));
1044 max_downtime
= (uint64_t)value
;
1047 bool migrate_postcopy_ram(void)
1051 s
= migrate_get_current();
1053 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_X_POSTCOPY_RAM
];
1056 bool migrate_auto_converge(void)
1060 s
= migrate_get_current();
1062 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_AUTO_CONVERGE
];
1065 bool migrate_zero_blocks(void)
1069 s
= migrate_get_current();
1071 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_ZERO_BLOCKS
];
1074 bool migrate_use_compression(void)
1078 s
= migrate_get_current();
1080 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_COMPRESS
];
1083 int migrate_compress_level(void)
1087 s
= migrate_get_current();
1089 return s
->parameters
[MIGRATION_PARAMETER_COMPRESS_LEVEL
];
1092 int migrate_compress_threads(void)
1096 s
= migrate_get_current();
1098 return s
->parameters
[MIGRATION_PARAMETER_COMPRESS_THREADS
];
1101 int migrate_decompress_threads(void)
1105 s
= migrate_get_current();
1107 return s
->parameters
[MIGRATION_PARAMETER_DECOMPRESS_THREADS
];
1110 bool migrate_use_events(void)
1114 s
= migrate_get_current();
1116 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_EVENTS
];
1119 int migrate_use_xbzrle(void)
1123 s
= migrate_get_current();
1125 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_XBZRLE
];
1128 int64_t migrate_xbzrle_cache_size(void)
1132 s
= migrate_get_current();
1134 return s
->xbzrle_cache_size
;
1137 /* migration thread support */
1139 * Something bad happened to the RP stream, mark an error
1140 * The caller shall print or trace something to indicate why
1142 static void mark_source_rp_bad(MigrationState
*s
)
1144 s
->rp_state
.error
= true;
1147 static struct rp_cmd_args
{
1148 ssize_t len
; /* -1 = variable */
1151 [MIG_RP_MSG_INVALID
] = { .len
= -1, .name
= "INVALID" },
1152 [MIG_RP_MSG_SHUT
] = { .len
= 4, .name
= "SHUT" },
1153 [MIG_RP_MSG_PONG
] = { .len
= 4, .name
= "PONG" },
1154 [MIG_RP_MSG_MAX
] = { .len
= -1, .name
= "MAX" },
1158 * Handles messages sent on the return path towards the source VM
1161 static void *source_return_path_thread(void *opaque
)
1163 MigrationState
*ms
= opaque
;
1164 QEMUFile
*rp
= ms
->rp_state
.from_dst_file
;
1165 uint16_t header_len
, header_type
;
1166 const int max_len
= 512;
1167 uint8_t buf
[max_len
];
1168 uint32_t tmp32
, sibling_error
;
1171 trace_source_return_path_thread_entry();
1172 while (!ms
->rp_state
.error
&& !qemu_file_get_error(rp
) &&
1173 migration_is_setup_or_active(ms
->state
)) {
1174 trace_source_return_path_thread_loop_top();
1175 header_type
= qemu_get_be16(rp
);
1176 header_len
= qemu_get_be16(rp
);
1178 if (header_type
>= MIG_RP_MSG_MAX
||
1179 header_type
== MIG_RP_MSG_INVALID
) {
1180 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1181 header_type
, header_len
);
1182 mark_source_rp_bad(ms
);
1186 if ((rp_cmd_args
[header_type
].len
!= -1 &&
1187 header_len
!= rp_cmd_args
[header_type
].len
) ||
1188 header_len
> max_len
) {
1189 error_report("RP: Received '%s' message (0x%04x) with"
1190 "incorrect length %d expecting %zu",
1191 rp_cmd_args
[header_type
].name
, header_type
, header_len
,
1192 (size_t)rp_cmd_args
[header_type
].len
);
1193 mark_source_rp_bad(ms
);
1197 /* We know we've got a valid header by this point */
1198 res
= qemu_get_buffer(rp
, buf
, header_len
);
1199 if (res
!= header_len
) {
1200 error_report("RP: Failed reading data for message 0x%04x"
1201 " read %d expected %d",
1202 header_type
, res
, header_len
);
1203 mark_source_rp_bad(ms
);
1207 /* OK, we have the message and the data */
1208 switch (header_type
) {
1209 case MIG_RP_MSG_SHUT
:
1210 sibling_error
= be32_to_cpup((uint32_t *)buf
);
1211 trace_source_return_path_thread_shut(sibling_error
);
1212 if (sibling_error
) {
1213 error_report("RP: Sibling indicated error %d", sibling_error
);
1214 mark_source_rp_bad(ms
);
1217 * We'll let the main thread deal with closing the RP
1218 * we could do a shutdown(2) on it, but we're the only user
1219 * anyway, so there's nothing gained.
1223 case MIG_RP_MSG_PONG
:
1224 tmp32
= be32_to_cpup((uint32_t *)buf
);
1225 trace_source_return_path_thread_pong(tmp32
);
1232 if (rp
&& qemu_file_get_error(rp
)) {
1233 trace_source_return_path_thread_bad_end();
1234 mark_source_rp_bad(ms
);
1237 trace_source_return_path_thread_end();
1239 ms
->rp_state
.from_dst_file
= NULL
;
1244 static int open_return_path_on_source(MigrationState
*ms
)
1247 ms
->rp_state
.from_dst_file
= qemu_file_get_return_path(ms
->file
);
1248 if (!ms
->rp_state
.from_dst_file
) {
1252 trace_open_return_path_on_source();
1253 qemu_thread_create(&ms
->rp_state
.rp_thread
, "return path",
1254 source_return_path_thread
, ms
, QEMU_THREAD_JOINABLE
);
1256 trace_open_return_path_on_source_continue();
1261 __attribute__ (( unused
)) /* Until later in patch series */
1262 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1263 static int await_return_path_close_on_source(MigrationState
*ms
)
1266 * If this is a normal exit then the destination will send a SHUT and the
1267 * rp_thread will exit, however if there's an error we need to cause
1270 if (qemu_file_get_error(ms
->file
) && ms
->rp_state
.from_dst_file
) {
1272 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1273 * waiting for the destination.
1275 qemu_file_shutdown(ms
->rp_state
.from_dst_file
);
1276 mark_source_rp_bad(ms
);
1278 trace_await_return_path_close_on_source_joining();
1279 qemu_thread_join(&ms
->rp_state
.rp_thread
);
1280 trace_await_return_path_close_on_source_close();
1281 return ms
->rp_state
.error
;
1285 * Switch from normal iteration to postcopy
1286 * Returns non-0 on error
1288 static int postcopy_start(MigrationState
*ms
, bool *old_vm_running
)
1291 const QEMUSizedBuffer
*qsb
;
1292 int64_t time_at_stop
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1293 migrate_set_state(ms
, MIGRATION_STATUS_ACTIVE
,
1294 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
1296 trace_postcopy_start();
1297 qemu_mutex_lock_iothread();
1298 trace_postcopy_start_set_run();
1300 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
1301 *old_vm_running
= runstate_is_running();
1302 global_state_store();
1303 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
1310 * in Finish migrate and with the io-lock held everything should
1311 * be quiet, but we've potentially still got dirty pages and we
1312 * need to tell the destination to throw any pages it's already received
1315 if (ram_postcopy_send_discard_bitmap(ms
)) {
1316 error_report("postcopy send discard bitmap failed");
1321 * send rest of state - note things that are doing postcopy
1322 * will notice we're in POSTCOPY_ACTIVE and not actually
1323 * wrap their state up here
1325 qemu_file_set_rate_limit(ms
->file
, INT64_MAX
);
1326 /* Ping just for debugging, helps line traces up */
1327 qemu_savevm_send_ping(ms
->file
, 2);
1330 * While loading the device state we may trigger page transfer
1331 * requests and the fd must be free to process those, and thus
1332 * the destination must read the whole device state off the fd before
1333 * it starts processing it. Unfortunately the ad-hoc migration format
1334 * doesn't allow the destination to know the size to read without fully
1335 * parsing it through each devices load-state code (especially the open
1336 * coded devices that use get/put).
1337 * So we wrap the device state up in a package with a length at the start;
1338 * to do this we use a qemu_buf to hold the whole of the device state.
1340 QEMUFile
*fb
= qemu_bufopen("w", NULL
);
1342 error_report("Failed to create buffered file");
1346 qemu_savevm_state_complete_precopy(fb
);
1347 qemu_savevm_send_ping(fb
, 3);
1349 qemu_savevm_send_postcopy_run(fb
);
1351 /* <><> end of stuff going into the package */
1352 qsb
= qemu_buf_get(fb
);
1354 /* Now send that blob */
1355 if (qemu_savevm_send_packaged(ms
->file
, qsb
)) {
1359 ms
->downtime
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) - time_at_stop
;
1361 qemu_mutex_unlock_iothread();
1364 * Although this ping is just for debug, it could potentially be
1365 * used for getting a better measurement of downtime at the source.
1367 qemu_savevm_send_ping(ms
->file
, 4);
1369 ret
= qemu_file_get_error(ms
->file
);
1371 error_report("postcopy_start: Migration stream errored");
1372 migrate_set_state(ms
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
1373 MIGRATION_STATUS_FAILED
);
1381 migrate_set_state(ms
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
1382 MIGRATION_STATUS_FAILED
);
1383 qemu_mutex_unlock_iothread();
1388 * migration_completion: Used by migration_thread when there's not much left.
1389 * The caller 'breaks' the loop when this returns.
1391 * @s: Current migration state
1392 * @current_active_state: The migration state we expect to be in
1393 * @*old_vm_running: Pointer to old_vm_running flag
1394 * @*start_time: Pointer to time to update
1396 static void migration_completion(MigrationState
*s
, int current_active_state
,
1397 bool *old_vm_running
,
1398 int64_t *start_time
)
1402 qemu_mutex_lock_iothread();
1403 *start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1404 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
1405 *old_vm_running
= runstate_is_running();
1407 ret
= global_state_store();
1409 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
1411 qemu_file_set_rate_limit(s
->file
, INT64_MAX
);
1412 qemu_savevm_state_complete_precopy(s
->file
);
1415 qemu_mutex_unlock_iothread();
1421 if (qemu_file_get_error(s
->file
)) {
1422 trace_migration_completion_file_err();
1426 migrate_set_state(s
, current_active_state
, MIGRATION_STATUS_COMPLETED
);
1430 migrate_set_state(s
, current_active_state
, MIGRATION_STATUS_FAILED
);
1434 * Master migration thread on the source VM.
1435 * It drives the migration and pumps the data down the outgoing channel.
1437 static void *migration_thread(void *opaque
)
1439 MigrationState
*s
= opaque
;
1440 /* Used by the bandwidth calcs, updated later */
1441 int64_t initial_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1442 int64_t setup_start
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
1443 int64_t initial_bytes
= 0;
1444 int64_t max_size
= 0;
1445 int64_t start_time
= initial_time
;
1447 bool old_vm_running
= false;
1448 bool entered_postcopy
= false;
1449 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1450 enum MigrationStatus current_active_state
= MIGRATION_STATUS_ACTIVE
;
1452 rcu_register_thread();
1454 qemu_savevm_state_header(s
->file
);
1456 if (migrate_postcopy_ram()) {
1457 /* Now tell the dest that it should open its end so it can reply */
1458 qemu_savevm_send_open_return_path(s
->file
);
1460 /* And do a ping that will make stuff easier to debug */
1461 qemu_savevm_send_ping(s
->file
, 1);
1464 * Tell the destination that we *might* want to do postcopy later;
1465 * if the other end can't do postcopy it should fail now, nice and
1468 qemu_savevm_send_postcopy_advise(s
->file
);
1471 qemu_savevm_state_begin(s
->file
, &s
->params
);
1473 s
->setup_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
) - setup_start
;
1474 current_active_state
= MIGRATION_STATUS_ACTIVE
;
1475 migrate_set_state(s
, MIGRATION_STATUS_SETUP
, MIGRATION_STATUS_ACTIVE
);
1477 trace_migration_thread_setup_complete();
1479 while (s
->state
== MIGRATION_STATUS_ACTIVE
||
1480 s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
1481 int64_t current_time
;
1482 uint64_t pending_size
;
1484 if (!qemu_file_rate_limit(s
->file
)) {
1485 uint64_t pend_post
, pend_nonpost
;
1487 qemu_savevm_state_pending(s
->file
, max_size
, &pend_nonpost
,
1489 pending_size
= pend_nonpost
+ pend_post
;
1490 trace_migrate_pending(pending_size
, max_size
,
1491 pend_post
, pend_nonpost
);
1492 if (pending_size
&& pending_size
>= max_size
) {
1493 /* Still a significant amount to transfer */
1495 current_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1496 if (migrate_postcopy_ram() &&
1497 s
->state
!= MIGRATION_STATUS_POSTCOPY_ACTIVE
&&
1498 pend_nonpost
<= max_size
&&
1499 atomic_read(&s
->start_postcopy
)) {
1501 if (!postcopy_start(s
, &old_vm_running
)) {
1502 current_active_state
= MIGRATION_STATUS_POSTCOPY_ACTIVE
;
1503 entered_postcopy
= true;
1508 /* Just another iteration step */
1509 qemu_savevm_state_iterate(s
->file
);
1511 trace_migration_thread_low_pending(pending_size
);
1512 migration_completion(s
, current_active_state
,
1513 &old_vm_running
, &start_time
);
1518 if (qemu_file_get_error(s
->file
)) {
1519 migrate_set_state(s
, current_active_state
, MIGRATION_STATUS_FAILED
);
1520 trace_migration_thread_file_err();
1523 current_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1524 if (current_time
>= initial_time
+ BUFFER_DELAY
) {
1525 uint64_t transferred_bytes
= qemu_ftell(s
->file
) - initial_bytes
;
1526 uint64_t time_spent
= current_time
- initial_time
;
1527 double bandwidth
= transferred_bytes
/ time_spent
;
1528 max_size
= bandwidth
* migrate_max_downtime() / 1000000;
1530 s
->mbps
= time_spent
? (((double) transferred_bytes
* 8.0) /
1531 ((double) time_spent
/ 1000.0)) / 1000.0 / 1000.0 : -1;
1533 trace_migrate_transferred(transferred_bytes
, time_spent
,
1534 bandwidth
, max_size
);
1535 /* if we haven't sent anything, we don't want to recalculate
1536 10000 is a small enough number for our purposes */
1537 if (s
->dirty_bytes_rate
&& transferred_bytes
> 10000) {
1538 s
->expected_downtime
= s
->dirty_bytes_rate
/ bandwidth
;
1541 qemu_file_reset_rate_limit(s
->file
);
1542 initial_time
= current_time
;
1543 initial_bytes
= qemu_ftell(s
->file
);
1545 if (qemu_file_rate_limit(s
->file
)) {
1546 /* usleep expects microseconds */
1547 g_usleep((initial_time
+ BUFFER_DELAY
- current_time
)*1000);
1551 trace_migration_thread_after_loop();
1552 /* If we enabled cpu throttling for auto-converge, turn it off. */
1553 cpu_throttle_stop();
1554 end_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1556 qemu_mutex_lock_iothread();
1557 qemu_savevm_state_cleanup();
1558 if (s
->state
== MIGRATION_STATUS_COMPLETED
) {
1559 uint64_t transferred_bytes
= qemu_ftell(s
->file
);
1560 s
->total_time
= end_time
- s
->total_time
;
1561 if (!entered_postcopy
) {
1562 s
->downtime
= end_time
- start_time
;
1564 if (s
->total_time
) {
1565 s
->mbps
= (((double) transferred_bytes
* 8.0) /
1566 ((double) s
->total_time
)) / 1000;
1568 runstate_set(RUN_STATE_POSTMIGRATE
);
1570 if (old_vm_running
&& !entered_postcopy
) {
1574 qemu_bh_schedule(s
->cleanup_bh
);
1575 qemu_mutex_unlock_iothread();
1577 rcu_unregister_thread();
1581 void migrate_fd_connect(MigrationState
*s
)
1583 /* This is a best 1st approximation. ns to ms */
1584 s
->expected_downtime
= max_downtime
/1000000;
1585 s
->cleanup_bh
= qemu_bh_new(migrate_fd_cleanup
, s
);
1587 qemu_file_set_rate_limit(s
->file
,
1588 s
->bandwidth_limit
/ XFER_LIMIT_RATIO
);
1590 /* Notify before starting migration thread */
1591 notifier_list_notify(&migration_state_notifiers
, s
);
1594 * Open the return path; currently for postcopy but other things might
1597 if (migrate_postcopy_ram()) {
1598 if (open_return_path_on_source(s
)) {
1599 error_report("Unable to open return-path for postcopy");
1600 migrate_set_state(s
, MIGRATION_STATUS_SETUP
,
1601 MIGRATION_STATUS_FAILED
);
1602 migrate_fd_cleanup(s
);
1607 migrate_compress_threads_create();
1608 qemu_thread_create(&s
->thread
, "migration", migration_thread
, s
,
1609 QEMU_THREAD_JOINABLE
);
1610 s
->migration_thread_running
= true;
1613 PostcopyState
postcopy_state_get(void)
1615 return atomic_mb_read(&incoming_postcopy_state
);
1618 /* Set the state and return the old state */
1619 PostcopyState
postcopy_state_set(PostcopyState new_state
)
1621 return atomic_xchg(&incoming_postcopy_state
, new_state
);