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 "qemu/thread.h"
28 #include "qmp-commands.h"
30 #include "qapi/util.h"
31 #include "qapi-event.h"
34 #define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
36 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
38 #define BUFFER_DELAY 100
39 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
41 /* Default compression thread count */
42 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
43 /* Default decompression thread count, usually decompression is at
44 * least 4 times as fast as compression.*/
45 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
46 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
47 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
48 /* Define default autoconverge cpu throttle migration parameters */
49 #define DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL 20
50 #define DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT 10
52 /* Migration XBZRLE default cache size */
53 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
55 static NotifierList migration_state_notifiers =
56 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
58 static bool deferred_incoming;
61 * Current state of incoming postcopy; note this is not part of
62 * MigrationIncomingState since it's state is used during cleanup
63 * at the end as MIS is being freed.
65 static PostcopyState incoming_postcopy_state;
67 /* When we add fault tolerance, we could have several
68 migrations at once. For now we don't need to add
69 dynamic creation of migration */
72 MigrationState *migrate_get_current(void)
74 static MigrationState current_migration = {
75 .state = MIGRATION_STATUS_NONE,
76 .bandwidth_limit = MAX_THROTTLE,
77 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
79 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
80 DEFAULT_MIGRATE_COMPRESS_LEVEL,
81 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
82 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
83 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
84 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
85 .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
86 DEFAULT_MIGRATE_X_CPU_THROTTLE_INITIAL,
87 .parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
88 DEFAULT_MIGRATE_X_CPU_THROTTLE_INCREMENT,
91 return ¤t_migration;
95 static MigrationIncomingState *mis_current;
97 MigrationIncomingState *migration_incoming_get_current(void)
102 MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
104 mis_current = g_new0(MigrationIncomingState, 1);
105 mis_current->from_src_file = f;
106 QLIST_INIT(&mis_current->loadvm_handlers);
107 qemu_mutex_init(&mis_current->rp_mutex);
108 qemu_event_init(&mis_current->main_thread_load_event, false);
113 void migration_incoming_state_destroy(void)
115 qemu_event_destroy(&mis_current->main_thread_load_event);
116 loadvm_free_handlers(mis_current);
125 uint8_t runstate[100];
130 static GlobalState global_state;
132 int global_state_store(void)
134 if (!runstate_store((char *)global_state.runstate,
135 sizeof(global_state.runstate))) {
136 error_report("runstate name too big: %s", global_state.runstate);
137 trace_migrate_state_too_big();
143 void global_state_store_running(void)
145 const char *state = RunState_lookup[RUN_STATE_RUNNING];
146 strncpy((char *)global_state.runstate,
147 state, sizeof(global_state.runstate));
150 static bool global_state_received(void)
152 return global_state.received;
155 static RunState global_state_get_runstate(void)
157 return global_state.state;
160 void global_state_set_optional(void)
162 global_state.optional = true;
165 static bool global_state_needed(void *opaque)
167 GlobalState *s = opaque;
168 char *runstate = (char *)s->runstate;
170 /* If it is not optional, it is mandatory */
172 if (s->optional == false) {
176 /* If state is running or paused, it is not needed */
178 if (strcmp(runstate, "running") == 0 ||
179 strcmp(runstate, "paused") == 0) {
183 /* for any other state it is needed */
187 static int global_state_post_load(void *opaque, int version_id)
189 GlobalState *s = opaque;
190 Error *local_err = NULL;
192 char *runstate = (char *)s->runstate;
195 trace_migrate_global_state_post_load(runstate);
197 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE_MAX,
202 error_report_err(local_err);
211 static void global_state_pre_save(void *opaque)
213 GlobalState *s = opaque;
215 trace_migrate_global_state_pre_save((char *)s->runstate);
216 s->size = strlen((char *)s->runstate) + 1;
219 static const VMStateDescription vmstate_globalstate = {
220 .name = "globalstate",
222 .minimum_version_id = 1,
223 .post_load = global_state_post_load,
224 .pre_save = global_state_pre_save,
225 .needed = global_state_needed,
226 .fields = (VMStateField[]) {
227 VMSTATE_UINT32(size, GlobalState),
228 VMSTATE_BUFFER(runstate, GlobalState),
229 VMSTATE_END_OF_LIST()
233 void register_global_state(void)
235 /* We would use it independently that we receive it */
236 strcpy((char *)&global_state.runstate, "");
237 global_state.received = false;
238 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
241 static void migrate_generate_event(int new_state)
243 if (migrate_use_events()) {
244 qapi_event_send_migration(new_state, &error_abort);
249 * Called on -incoming with a defer: uri.
250 * The migration can be started later after any parameters have been
253 static void deferred_incoming_migration(Error **errp)
255 if (deferred_incoming) {
256 error_setg(errp, "Incoming migration already deferred");
258 deferred_incoming = true;
261 void qemu_start_incoming_migration(const char *uri, Error **errp)
265 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
266 if (!strcmp(uri, "defer")) {
267 deferred_incoming_migration(errp);
268 } else if (strstart(uri, "tcp:", &p)) {
269 tcp_start_incoming_migration(p, errp);
271 } else if (strstart(uri, "rdma:", &p)) {
272 rdma_start_incoming_migration(p, errp);
275 } else if (strstart(uri, "exec:", &p)) {
276 exec_start_incoming_migration(p, errp);
277 } else if (strstart(uri, "unix:", &p)) {
278 unix_start_incoming_migration(p, errp);
279 } else if (strstart(uri, "fd:", &p)) {
280 fd_start_incoming_migration(p, errp);
283 error_setg(errp, "unknown migration protocol: %s", uri);
287 static void process_incoming_migration_co(void *opaque)
289 QEMUFile *f = opaque;
290 Error *local_err = NULL;
293 migration_incoming_state_new(f);
294 postcopy_state_set(POSTCOPY_INCOMING_NONE);
295 migrate_generate_event(MIGRATION_STATUS_ACTIVE);
296 ret = qemu_loadvm_state(f);
299 free_xbzrle_decoded_buf();
300 migration_incoming_state_destroy();
303 migrate_generate_event(MIGRATION_STATUS_FAILED);
304 error_report("load of migration failed: %s", strerror(-ret));
305 migrate_decompress_threads_join();
309 /* Make sure all file formats flush their mutable metadata */
310 bdrv_invalidate_cache_all(&local_err);
312 migrate_generate_event(MIGRATION_STATUS_FAILED);
313 error_report_err(local_err);
314 migrate_decompress_threads_join();
319 * This must happen after all error conditions are dealt with and
320 * we're sure the VM is going to be running on this host.
322 qemu_announce_self();
324 /* If global state section was not received or we are in running
325 state, we need to obey autostart. Any other state is set with
328 if (!global_state_received() ||
329 global_state_get_runstate() == RUN_STATE_RUNNING) {
333 runstate_set(RUN_STATE_PAUSED);
336 runstate_set(global_state_get_runstate());
338 migrate_decompress_threads_join();
340 * This must happen after any state changes since as soon as an external
341 * observer sees this event they might start to prod at the VM assuming
344 migrate_generate_event(MIGRATION_STATUS_COMPLETED);
347 void process_incoming_migration(QEMUFile *f)
349 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
350 int fd = qemu_get_fd(f);
353 migrate_decompress_threads_create();
354 qemu_set_nonblock(fd);
355 qemu_coroutine_enter(co, f);
359 * Send a message on the return channel back to the source
362 void migrate_send_rp_message(MigrationIncomingState *mis,
363 enum mig_rp_message_type message_type,
364 uint16_t len, void *data)
366 trace_migrate_send_rp_message((int)message_type, len);
367 qemu_mutex_lock(&mis->rp_mutex);
368 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
369 qemu_put_be16(mis->to_src_file, len);
370 qemu_put_buffer(mis->to_src_file, data, len);
371 qemu_fflush(mis->to_src_file);
372 qemu_mutex_unlock(&mis->rp_mutex);
376 * Send a 'SHUT' message on the return channel with the given value
377 * to indicate that we've finished with the RP. Non-0 value indicates
380 void migrate_send_rp_shut(MigrationIncomingState *mis,
385 buf = cpu_to_be32(value);
386 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
390 * Send a 'PONG' message on the return channel with the given value
391 * (normally in response to a 'PING')
393 void migrate_send_rp_pong(MigrationIncomingState *mis,
398 buf = cpu_to_be32(value);
399 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
402 /* amount of nanoseconds we are willing to wait for migration to be down.
403 * the choice of nanoseconds is because it is the maximum resolution that
404 * get_clock() can achieve. It is an internal measure. All user-visible
405 * units must be in seconds */
406 static uint64_t max_downtime = 300000000;
408 uint64_t migrate_max_downtime(void)
413 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
415 MigrationCapabilityStatusList *head = NULL;
416 MigrationCapabilityStatusList *caps;
417 MigrationState *s = migrate_get_current();
420 caps = NULL; /* silence compiler warning */
421 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
423 head = g_malloc0(sizeof(*caps));
426 caps->next = g_malloc0(sizeof(*caps));
430 g_malloc(sizeof(*caps->value));
431 caps->value->capability = i;
432 caps->value->state = s->enabled_capabilities[i];
438 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
440 MigrationParameters *params;
441 MigrationState *s = migrate_get_current();
443 params = g_malloc0(sizeof(*params));
444 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
445 params->compress_threads =
446 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
447 params->decompress_threads =
448 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
449 params->x_cpu_throttle_initial =
450 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
451 params->x_cpu_throttle_increment =
452 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
458 * Return true if we're already in the middle of a migration
459 * (i.e. any of the active or setup states)
461 static bool migration_is_setup_or_active(int state)
464 case MIGRATION_STATUS_ACTIVE:
465 case MIGRATION_STATUS_SETUP:
474 static void get_xbzrle_cache_stats(MigrationInfo *info)
476 if (migrate_use_xbzrle()) {
477 info->has_xbzrle_cache = true;
478 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
479 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
480 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
481 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
482 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
483 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
484 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
488 MigrationInfo *qmp_query_migrate(Error **errp)
490 MigrationInfo *info = g_malloc0(sizeof(*info));
491 MigrationState *s = migrate_get_current();
494 case MIGRATION_STATUS_NONE:
495 /* no migration has happened ever */
497 case MIGRATION_STATUS_SETUP:
498 info->has_status = true;
499 info->has_total_time = false;
501 case MIGRATION_STATUS_ACTIVE:
502 case MIGRATION_STATUS_CANCELLING:
503 info->has_status = true;
504 info->has_total_time = true;
505 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
507 info->has_expected_downtime = true;
508 info->expected_downtime = s->expected_downtime;
509 info->has_setup_time = true;
510 info->setup_time = s->setup_time;
512 info->has_ram = true;
513 info->ram = g_malloc0(sizeof(*info->ram));
514 info->ram->transferred = ram_bytes_transferred();
515 info->ram->remaining = ram_bytes_remaining();
516 info->ram->total = ram_bytes_total();
517 info->ram->duplicate = dup_mig_pages_transferred();
518 info->ram->skipped = skipped_mig_pages_transferred();
519 info->ram->normal = norm_mig_pages_transferred();
520 info->ram->normal_bytes = norm_mig_bytes_transferred();
521 info->ram->dirty_pages_rate = s->dirty_pages_rate;
522 info->ram->mbps = s->mbps;
523 info->ram->dirty_sync_count = s->dirty_sync_count;
525 if (blk_mig_active()) {
526 info->has_disk = true;
527 info->disk = g_malloc0(sizeof(*info->disk));
528 info->disk->transferred = blk_mig_bytes_transferred();
529 info->disk->remaining = blk_mig_bytes_remaining();
530 info->disk->total = blk_mig_bytes_total();
533 if (cpu_throttle_active()) {
534 info->has_x_cpu_throttle_percentage = true;
535 info->x_cpu_throttle_percentage = cpu_throttle_get_percentage();
538 get_xbzrle_cache_stats(info);
540 case MIGRATION_STATUS_COMPLETED:
541 get_xbzrle_cache_stats(info);
543 info->has_status = true;
544 info->has_total_time = true;
545 info->total_time = s->total_time;
546 info->has_downtime = true;
547 info->downtime = s->downtime;
548 info->has_setup_time = true;
549 info->setup_time = s->setup_time;
551 info->has_ram = true;
552 info->ram = g_malloc0(sizeof(*info->ram));
553 info->ram->transferred = ram_bytes_transferred();
554 info->ram->remaining = 0;
555 info->ram->total = ram_bytes_total();
556 info->ram->duplicate = dup_mig_pages_transferred();
557 info->ram->skipped = skipped_mig_pages_transferred();
558 info->ram->normal = norm_mig_pages_transferred();
559 info->ram->normal_bytes = norm_mig_bytes_transferred();
560 info->ram->mbps = s->mbps;
561 info->ram->dirty_sync_count = s->dirty_sync_count;
563 case MIGRATION_STATUS_FAILED:
564 info->has_status = true;
566 case MIGRATION_STATUS_CANCELLED:
567 info->has_status = true;
570 info->status = s->state;
575 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
578 MigrationState *s = migrate_get_current();
579 MigrationCapabilityStatusList *cap;
581 if (migration_is_setup_or_active(s->state)) {
582 error_setg(errp, QERR_MIGRATION_ACTIVE);
586 for (cap = params; cap; cap = cap->next) {
587 s->enabled_capabilities[cap->value->capability] = cap->value->state;
590 if (migrate_postcopy_ram()) {
591 if (migrate_use_compression()) {
592 /* The decompression threads asynchronously write into RAM
593 * rather than use the atomic copies needed to avoid
594 * userfaulting. It should be possible to fix the decompression
595 * threads for compatibility in future.
597 error_report("Postcopy is not currently compatible with "
599 s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM] =
605 void qmp_migrate_set_parameters(bool has_compress_level,
606 int64_t compress_level,
607 bool has_compress_threads,
608 int64_t compress_threads,
609 bool has_decompress_threads,
610 int64_t decompress_threads,
611 bool has_x_cpu_throttle_initial,
612 int64_t x_cpu_throttle_initial,
613 bool has_x_cpu_throttle_increment,
614 int64_t x_cpu_throttle_increment, Error **errp)
616 MigrationState *s = migrate_get_current();
618 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
619 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
620 "is invalid, it should be in the range of 0 to 9");
623 if (has_compress_threads &&
624 (compress_threads < 1 || compress_threads > 255)) {
625 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
627 "is invalid, it should be in the range of 1 to 255");
630 if (has_decompress_threads &&
631 (decompress_threads < 1 || decompress_threads > 255)) {
632 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
633 "decompress_threads",
634 "is invalid, it should be in the range of 1 to 255");
637 if (has_x_cpu_throttle_initial &&
638 (x_cpu_throttle_initial < 1 || x_cpu_throttle_initial > 99)) {
639 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
640 "x_cpu_throttle_initial",
641 "an integer in the range of 1 to 99");
643 if (has_x_cpu_throttle_increment &&
644 (x_cpu_throttle_increment < 1 || x_cpu_throttle_increment > 99)) {
645 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
646 "x_cpu_throttle_increment",
647 "an integer in the range of 1 to 99");
650 if (has_compress_level) {
651 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
653 if (has_compress_threads) {
654 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
656 if (has_decompress_threads) {
657 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
660 if (has_x_cpu_throttle_initial) {
661 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
662 x_cpu_throttle_initial;
665 if (has_x_cpu_throttle_increment) {
666 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
667 x_cpu_throttle_increment;
671 void qmp_migrate_start_postcopy(Error **errp)
673 MigrationState *s = migrate_get_current();
675 if (!migrate_postcopy_ram()) {
676 error_setg(errp, "Enable postcopy with migration_set_capability before"
677 " the start of migration");
681 if (s->state == MIGRATION_STATUS_NONE) {
682 error_setg(errp, "Postcopy must be started after migration has been"
687 * we don't error if migration has finished since that would be racy
688 * with issuing this command.
690 atomic_set(&s->start_postcopy, true);
693 /* shared migration helpers */
695 static void migrate_set_state(MigrationState *s, int old_state, int new_state)
697 if (atomic_cmpxchg(&s->state, old_state, new_state) == old_state) {
698 trace_migrate_set_state(new_state);
699 migrate_generate_event(new_state);
703 static void migrate_fd_cleanup(void *opaque)
705 MigrationState *s = opaque;
707 qemu_bh_delete(s->cleanup_bh);
708 s->cleanup_bh = NULL;
711 trace_migrate_fd_cleanup();
712 qemu_mutex_unlock_iothread();
713 qemu_thread_join(&s->thread);
714 qemu_mutex_lock_iothread();
716 migrate_compress_threads_join();
717 qemu_fclose(s->file);
721 assert(s->state != MIGRATION_STATUS_ACTIVE);
723 if (s->state == MIGRATION_STATUS_CANCELLING) {
724 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
725 MIGRATION_STATUS_CANCELLED);
728 notifier_list_notify(&migration_state_notifiers, s);
731 void migrate_fd_error(MigrationState *s)
733 trace_migrate_fd_error();
734 assert(s->file == NULL);
735 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
736 notifier_list_notify(&migration_state_notifiers, s);
739 static void migrate_fd_cancel(MigrationState *s)
742 QEMUFile *f = migrate_get_current()->file;
743 trace_migrate_fd_cancel();
745 if (s->rp_state.from_dst_file) {
746 /* shutdown the rp socket, so causing the rp thread to shutdown */
747 qemu_file_shutdown(s->rp_state.from_dst_file);
751 old_state = s->state;
752 if (!migration_is_setup_or_active(old_state)) {
755 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
756 } while (s->state != MIGRATION_STATUS_CANCELLING);
759 * If we're unlucky the migration code might be stuck somewhere in a
760 * send/write while the network has failed and is waiting to timeout;
761 * if we've got shutdown(2) available then we can force it to quit.
762 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
763 * called in a bh, so there is no race against this cancel.
765 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
766 qemu_file_shutdown(f);
770 void add_migration_state_change_notifier(Notifier *notify)
772 notifier_list_add(&migration_state_notifiers, notify);
775 void remove_migration_state_change_notifier(Notifier *notify)
777 notifier_remove(notify);
780 bool migration_in_setup(MigrationState *s)
782 return s->state == MIGRATION_STATUS_SETUP;
785 bool migration_has_finished(MigrationState *s)
787 return s->state == MIGRATION_STATUS_COMPLETED;
790 bool migration_has_failed(MigrationState *s)
792 return (s->state == MIGRATION_STATUS_CANCELLED ||
793 s->state == MIGRATION_STATUS_FAILED);
796 MigrationState *migrate_init(const MigrationParams *params)
798 MigrationState *s = migrate_get_current();
799 int64_t bandwidth_limit = s->bandwidth_limit;
800 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
801 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
802 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
803 int compress_thread_count =
804 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
805 int decompress_thread_count =
806 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
807 int x_cpu_throttle_initial =
808 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL];
809 int x_cpu_throttle_increment =
810 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT];
812 memcpy(enabled_capabilities, s->enabled_capabilities,
813 sizeof(enabled_capabilities));
815 memset(s, 0, sizeof(*s));
817 memcpy(s->enabled_capabilities, enabled_capabilities,
818 sizeof(enabled_capabilities));
819 s->xbzrle_cache_size = xbzrle_cache_size;
821 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
822 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
823 compress_thread_count;
824 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
825 decompress_thread_count;
826 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL] =
827 x_cpu_throttle_initial;
828 s->parameters[MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT] =
829 x_cpu_throttle_increment;
830 s->bandwidth_limit = bandwidth_limit;
831 migrate_set_state(s, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
833 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
837 static GSList *migration_blockers;
839 void migrate_add_blocker(Error *reason)
841 migration_blockers = g_slist_prepend(migration_blockers, reason);
844 void migrate_del_blocker(Error *reason)
846 migration_blockers = g_slist_remove(migration_blockers, reason);
849 void qmp_migrate_incoming(const char *uri, Error **errp)
851 Error *local_err = NULL;
852 static bool once = true;
854 if (!deferred_incoming) {
855 error_setg(errp, "For use with '-incoming defer'");
859 error_setg(errp, "The incoming migration has already been started");
862 qemu_start_incoming_migration(uri, &local_err);
865 error_propagate(errp, local_err);
872 void qmp_migrate(const char *uri, bool has_blk, bool blk,
873 bool has_inc, bool inc, bool has_detach, bool detach,
876 Error *local_err = NULL;
877 MigrationState *s = migrate_get_current();
878 MigrationParams params;
881 params.blk = has_blk && blk;
882 params.shared = has_inc && inc;
884 if (migration_is_setup_or_active(s->state) ||
885 s->state == MIGRATION_STATUS_CANCELLING) {
886 error_setg(errp, QERR_MIGRATION_ACTIVE);
889 if (runstate_check(RUN_STATE_INMIGRATE)) {
890 error_setg(errp, "Guest is waiting for an incoming migration");
894 if (qemu_savevm_state_blocked(errp)) {
898 if (migration_blockers) {
899 *errp = error_copy(migration_blockers->data);
903 /* We are starting a new migration, so we want to start in a clean
904 state. This change is only needed if previous migration
905 failed/was cancelled. We don't use migrate_set_state() because
906 we are setting the initial state, not changing it. */
907 s->state = MIGRATION_STATUS_NONE;
909 s = migrate_init(¶ms);
911 if (strstart(uri, "tcp:", &p)) {
912 tcp_start_outgoing_migration(s, p, &local_err);
914 } else if (strstart(uri, "rdma:", &p)) {
915 rdma_start_outgoing_migration(s, p, &local_err);
918 } else if (strstart(uri, "exec:", &p)) {
919 exec_start_outgoing_migration(s, p, &local_err);
920 } else if (strstart(uri, "unix:", &p)) {
921 unix_start_outgoing_migration(s, p, &local_err);
922 } else if (strstart(uri, "fd:", &p)) {
923 fd_start_outgoing_migration(s, p, &local_err);
926 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
927 "a valid migration protocol");
928 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_FAILED);
934 error_propagate(errp, local_err);
939 void qmp_migrate_cancel(Error **errp)
941 migrate_fd_cancel(migrate_get_current());
944 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
946 MigrationState *s = migrate_get_current();
949 /* Check for truncation */
950 if (value != (size_t)value) {
951 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
952 "exceeding address space");
956 /* Cache should not be larger than guest ram size */
957 if (value > ram_bytes_total()) {
958 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
959 "exceeds guest ram size ");
963 new_size = xbzrle_cache_resize(value);
965 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
966 "is smaller than page size");
970 s->xbzrle_cache_size = new_size;
973 int64_t qmp_query_migrate_cache_size(Error **errp)
975 return migrate_xbzrle_cache_size();
978 void qmp_migrate_set_speed(int64_t value, Error **errp)
985 if (value > SIZE_MAX) {
989 s = migrate_get_current();
990 s->bandwidth_limit = value;
992 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
996 void qmp_migrate_set_downtime(double value, Error **errp)
999 value = MAX(0, MIN(UINT64_MAX, value));
1000 max_downtime = (uint64_t)value;
1003 bool migrate_postcopy_ram(void)
1007 s = migrate_get_current();
1009 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM];
1012 bool migrate_auto_converge(void)
1016 s = migrate_get_current();
1018 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1021 bool migrate_zero_blocks(void)
1025 s = migrate_get_current();
1027 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1030 bool migrate_use_compression(void)
1034 s = migrate_get_current();
1036 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
1039 int migrate_compress_level(void)
1043 s = migrate_get_current();
1045 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
1048 int migrate_compress_threads(void)
1052 s = migrate_get_current();
1054 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
1057 int migrate_decompress_threads(void)
1061 s = migrate_get_current();
1063 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
1066 bool migrate_use_events(void)
1070 s = migrate_get_current();
1072 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1075 int migrate_use_xbzrle(void)
1079 s = migrate_get_current();
1081 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1084 int64_t migrate_xbzrle_cache_size(void)
1088 s = migrate_get_current();
1090 return s->xbzrle_cache_size;
1093 /* migration thread support */
1095 * Something bad happened to the RP stream, mark an error
1096 * The caller shall print or trace something to indicate why
1098 static void mark_source_rp_bad(MigrationState *s)
1100 s->rp_state.error = true;
1103 static struct rp_cmd_args {
1104 ssize_t len; /* -1 = variable */
1107 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1108 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1109 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
1110 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1114 * Handles messages sent on the return path towards the source VM
1117 static void *source_return_path_thread(void *opaque)
1119 MigrationState *ms = opaque;
1120 QEMUFile *rp = ms->rp_state.from_dst_file;
1121 uint16_t header_len, header_type;
1122 const int max_len = 512;
1123 uint8_t buf[max_len];
1124 uint32_t tmp32, sibling_error;
1127 trace_source_return_path_thread_entry();
1128 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1129 migration_is_setup_or_active(ms->state)) {
1130 trace_source_return_path_thread_loop_top();
1131 header_type = qemu_get_be16(rp);
1132 header_len = qemu_get_be16(rp);
1134 if (header_type >= MIG_RP_MSG_MAX ||
1135 header_type == MIG_RP_MSG_INVALID) {
1136 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1137 header_type, header_len);
1138 mark_source_rp_bad(ms);
1142 if ((rp_cmd_args[header_type].len != -1 &&
1143 header_len != rp_cmd_args[header_type].len) ||
1144 header_len > max_len) {
1145 error_report("RP: Received '%s' message (0x%04x) with"
1146 "incorrect length %d expecting %zu",
1147 rp_cmd_args[header_type].name, header_type, header_len,
1148 (size_t)rp_cmd_args[header_type].len);
1149 mark_source_rp_bad(ms);
1153 /* We know we've got a valid header by this point */
1154 res = qemu_get_buffer(rp, buf, header_len);
1155 if (res != header_len) {
1156 error_report("RP: Failed reading data for message 0x%04x"
1157 " read %d expected %d",
1158 header_type, res, header_len);
1159 mark_source_rp_bad(ms);
1163 /* OK, we have the message and the data */
1164 switch (header_type) {
1165 case MIG_RP_MSG_SHUT:
1166 sibling_error = be32_to_cpup((uint32_t *)buf);
1167 trace_source_return_path_thread_shut(sibling_error);
1168 if (sibling_error) {
1169 error_report("RP: Sibling indicated error %d", sibling_error);
1170 mark_source_rp_bad(ms);
1173 * We'll let the main thread deal with closing the RP
1174 * we could do a shutdown(2) on it, but we're the only user
1175 * anyway, so there's nothing gained.
1179 case MIG_RP_MSG_PONG:
1180 tmp32 = be32_to_cpup((uint32_t *)buf);
1181 trace_source_return_path_thread_pong(tmp32);
1188 if (rp && qemu_file_get_error(rp)) {
1189 trace_source_return_path_thread_bad_end();
1190 mark_source_rp_bad(ms);
1193 trace_source_return_path_thread_end();
1195 ms->rp_state.from_dst_file = NULL;
1200 __attribute__ (( unused )) /* Until later in patch series */
1201 static int open_return_path_on_source(MigrationState *ms)
1204 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->file);
1205 if (!ms->rp_state.from_dst_file) {
1209 trace_open_return_path_on_source();
1210 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1211 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1213 trace_open_return_path_on_source_continue();
1218 __attribute__ (( unused )) /* Until later in patch series */
1219 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1220 static int await_return_path_close_on_source(MigrationState *ms)
1223 * If this is a normal exit then the destination will send a SHUT and the
1224 * rp_thread will exit, however if there's an error we need to cause
1227 if (qemu_file_get_error(ms->file) && ms->rp_state.from_dst_file) {
1229 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1230 * waiting for the destination.
1232 qemu_file_shutdown(ms->rp_state.from_dst_file);
1233 mark_source_rp_bad(ms);
1235 trace_await_return_path_close_on_source_joining();
1236 qemu_thread_join(&ms->rp_state.rp_thread);
1237 trace_await_return_path_close_on_source_close();
1238 return ms->rp_state.error;
1242 * migration_completion: Used by migration_thread when there's not much left.
1243 * The caller 'breaks' the loop when this returns.
1245 * @s: Current migration state
1246 * @*old_vm_running: Pointer to old_vm_running flag
1247 * @*start_time: Pointer to time to update
1249 static void migration_completion(MigrationState *s, bool *old_vm_running,
1250 int64_t *start_time)
1254 qemu_mutex_lock_iothread();
1255 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1256 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1257 *old_vm_running = runstate_is_running();
1259 ret = global_state_store();
1261 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1263 qemu_file_set_rate_limit(s->file, INT64_MAX);
1264 qemu_savevm_state_complete_precopy(s->file);
1267 qemu_mutex_unlock_iothread();
1273 if (qemu_file_get_error(s->file)) {
1274 trace_migration_completion_file_err();
1278 migrate_set_state(s, MIGRATION_STATUS_ACTIVE, MIGRATION_STATUS_COMPLETED);
1282 migrate_set_state(s, MIGRATION_STATUS_ACTIVE, MIGRATION_STATUS_FAILED);
1286 * Master migration thread on the source VM.
1287 * It drives the migration and pumps the data down the outgoing channel.
1289 static void *migration_thread(void *opaque)
1291 MigrationState *s = opaque;
1292 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1293 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
1294 int64_t initial_bytes = 0;
1295 int64_t max_size = 0;
1296 int64_t start_time = initial_time;
1298 bool old_vm_running = false;
1300 rcu_register_thread();
1302 qemu_savevm_state_header(s->file);
1303 qemu_savevm_state_begin(s->file, &s->params);
1305 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
1306 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
1308 while (s->state == MIGRATION_STATUS_ACTIVE) {
1309 int64_t current_time;
1310 uint64_t pending_size;
1312 if (!qemu_file_rate_limit(s->file)) {
1313 uint64_t pend_post, pend_nonpost;
1315 qemu_savevm_state_pending(s->file, max_size, &pend_nonpost,
1317 pending_size = pend_nonpost + pend_post;
1318 trace_migrate_pending(pending_size, max_size,
1319 pend_post, pend_nonpost);
1320 if (pending_size && pending_size >= max_size) {
1321 qemu_savevm_state_iterate(s->file);
1323 trace_migration_thread_low_pending(pending_size);
1324 migration_completion(s, &old_vm_running, &start_time);
1329 if (qemu_file_get_error(s->file)) {
1330 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
1331 MIGRATION_STATUS_FAILED);
1334 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1335 if (current_time >= initial_time + BUFFER_DELAY) {
1336 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
1337 uint64_t time_spent = current_time - initial_time;
1338 double bandwidth = transferred_bytes / time_spent;
1339 max_size = bandwidth * migrate_max_downtime() / 1000000;
1341 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
1342 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
1344 trace_migrate_transferred(transferred_bytes, time_spent,
1345 bandwidth, max_size);
1346 /* if we haven't sent anything, we don't want to recalculate
1347 10000 is a small enough number for our purposes */
1348 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1349 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1352 qemu_file_reset_rate_limit(s->file);
1353 initial_time = current_time;
1354 initial_bytes = qemu_ftell(s->file);
1356 if (qemu_file_rate_limit(s->file)) {
1357 /* usleep expects microseconds */
1358 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1362 /* If we enabled cpu throttling for auto-converge, turn it off. */
1363 cpu_throttle_stop();
1364 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1366 qemu_mutex_lock_iothread();
1367 qemu_savevm_state_cleanup();
1368 if (s->state == MIGRATION_STATUS_COMPLETED) {
1369 uint64_t transferred_bytes = qemu_ftell(s->file);
1370 s->total_time = end_time - s->total_time;
1371 s->downtime = end_time - start_time;
1372 if (s->total_time) {
1373 s->mbps = (((double) transferred_bytes * 8.0) /
1374 ((double) s->total_time)) / 1000;
1376 runstate_set(RUN_STATE_POSTMIGRATE);
1378 if (old_vm_running) {
1382 qemu_bh_schedule(s->cleanup_bh);
1383 qemu_mutex_unlock_iothread();
1385 rcu_unregister_thread();
1389 void migrate_fd_connect(MigrationState *s)
1391 /* This is a best 1st approximation. ns to ms */
1392 s->expected_downtime = max_downtime/1000000;
1393 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
1395 qemu_file_set_rate_limit(s->file,
1396 s->bandwidth_limit / XFER_LIMIT_RATIO);
1398 /* Notify before starting migration thread */
1399 notifier_list_notify(&migration_state_notifiers, s);
1401 migrate_compress_threads_create();
1402 qemu_thread_create(&s->thread, "migration", migration_thread, s,
1403 QEMU_THREAD_JOINABLE);
1406 PostcopyState postcopy_state_get(void)
1408 return atomic_mb_read(&incoming_postcopy_state);
1411 /* Set the state and return the old state */
1412 PostcopyState postcopy_state_set(PostcopyState new_state)
1414 return atomic_xchg(&incoming_postcopy_state, new_state);