migration: convert exec socket protocol to use QIOChannel
[qemu/cris-port.git] / migration / migration.c
blob695384badaa4b38b20de3410cf23c931c6aabe09
1 /*
2 * QEMU live migration
4 * Copyright IBM, Corp. 2008
6 * Authors:
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"
27 #include "qemu/rcu.h"
28 #include "migration/block.h"
29 #include "migration/postcopy-ram.h"
30 #include "qemu/thread.h"
31 #include "qmp-commands.h"
32 #include "trace.h"
33 #include "qapi-event.h"
34 #include "qom/cpu.h"
35 #include "exec/memory.h"
36 #include "exec/address-spaces.h"
37 #include "io/channel-buffer.h"
39 #define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
41 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
42 * data. */
43 #define BUFFER_DELAY 100
44 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
46 /* Default compression thread count */
47 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
48 /* Default decompression thread count, usually decompression is at
49 * least 4 times as fast as compression.*/
50 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
51 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
52 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
53 /* Define default autoconverge cpu throttle migration parameters */
54 #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
55 #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
57 /* Migration XBZRLE default cache size */
58 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
60 static NotifierList migration_state_notifiers =
61 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
63 static bool deferred_incoming;
66 * Current state of incoming postcopy; note this is not part of
67 * MigrationIncomingState since it's state is used during cleanup
68 * at the end as MIS is being freed.
70 static PostcopyState incoming_postcopy_state;
72 /* When we add fault tolerance, we could have several
73 migrations at once. For now we don't need to add
74 dynamic creation of migration */
76 /* For outgoing */
77 MigrationState *migrate_get_current(void)
79 static bool once;
80 static MigrationState current_migration = {
81 .state = MIGRATION_STATUS_NONE,
82 .bandwidth_limit = MAX_THROTTLE,
83 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
84 .mbps = -1,
85 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
86 DEFAULT_MIGRATE_COMPRESS_LEVEL,
87 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
88 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
89 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
90 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
91 .parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL] =
92 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL,
93 .parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT] =
94 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT,
97 if (!once) {
98 qemu_mutex_init(&current_migration.src_page_req_mutex);
99 once = true;
101 return &current_migration;
104 /* For incoming */
105 static MigrationIncomingState *mis_current;
107 MigrationIncomingState *migration_incoming_get_current(void)
109 return mis_current;
112 MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
114 mis_current = g_new0(MigrationIncomingState, 1);
115 mis_current->from_src_file = f;
116 mis_current->state = MIGRATION_STATUS_NONE;
117 QLIST_INIT(&mis_current->loadvm_handlers);
118 qemu_mutex_init(&mis_current->rp_mutex);
119 qemu_event_init(&mis_current->main_thread_load_event, false);
121 return mis_current;
124 void migration_incoming_state_destroy(void)
126 qemu_event_destroy(&mis_current->main_thread_load_event);
127 loadvm_free_handlers(mis_current);
128 g_free(mis_current);
129 mis_current = NULL;
133 typedef struct {
134 bool optional;
135 uint32_t size;
136 uint8_t runstate[100];
137 RunState state;
138 bool received;
139 } GlobalState;
141 static GlobalState global_state;
143 int global_state_store(void)
145 if (!runstate_store((char *)global_state.runstate,
146 sizeof(global_state.runstate))) {
147 error_report("runstate name too big: %s", global_state.runstate);
148 trace_migrate_state_too_big();
149 return -EINVAL;
151 return 0;
154 void global_state_store_running(void)
156 const char *state = RunState_lookup[RUN_STATE_RUNNING];
157 strncpy((char *)global_state.runstate,
158 state, sizeof(global_state.runstate));
161 static bool global_state_received(void)
163 return global_state.received;
166 static RunState global_state_get_runstate(void)
168 return global_state.state;
171 void global_state_set_optional(void)
173 global_state.optional = true;
176 static bool global_state_needed(void *opaque)
178 GlobalState *s = opaque;
179 char *runstate = (char *)s->runstate;
181 /* If it is not optional, it is mandatory */
183 if (s->optional == false) {
184 return true;
187 /* If state is running or paused, it is not needed */
189 if (strcmp(runstate, "running") == 0 ||
190 strcmp(runstate, "paused") == 0) {
191 return false;
194 /* for any other state it is needed */
195 return true;
198 static int global_state_post_load(void *opaque, int version_id)
200 GlobalState *s = opaque;
201 Error *local_err = NULL;
202 int r;
203 char *runstate = (char *)s->runstate;
205 s->received = true;
206 trace_migrate_global_state_post_load(runstate);
208 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE__MAX,
209 -1, &local_err);
211 if (r == -1) {
212 if (local_err) {
213 error_report_err(local_err);
215 return -EINVAL;
217 s->state = r;
219 return 0;
222 static void global_state_pre_save(void *opaque)
224 GlobalState *s = opaque;
226 trace_migrate_global_state_pre_save((char *)s->runstate);
227 s->size = strlen((char *)s->runstate) + 1;
230 static const VMStateDescription vmstate_globalstate = {
231 .name = "globalstate",
232 .version_id = 1,
233 .minimum_version_id = 1,
234 .post_load = global_state_post_load,
235 .pre_save = global_state_pre_save,
236 .needed = global_state_needed,
237 .fields = (VMStateField[]) {
238 VMSTATE_UINT32(size, GlobalState),
239 VMSTATE_BUFFER(runstate, GlobalState),
240 VMSTATE_END_OF_LIST()
244 void register_global_state(void)
246 /* We would use it independently that we receive it */
247 strcpy((char *)&global_state.runstate, "");
248 global_state.received = false;
249 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
252 static void migrate_generate_event(int new_state)
254 if (migrate_use_events()) {
255 qapi_event_send_migration(new_state, &error_abort);
260 * Called on -incoming with a defer: uri.
261 * The migration can be started later after any parameters have been
262 * changed.
264 static void deferred_incoming_migration(Error **errp)
266 if (deferred_incoming) {
267 error_setg(errp, "Incoming migration already deferred");
269 deferred_incoming = true;
272 /* Request a range of pages from the source VM at the given
273 * start address.
274 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
275 * as the last request (a name must have been given previously)
276 * Start: Address offset within the RB
277 * Len: Length in bytes required - must be a multiple of pagesize
279 void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
280 ram_addr_t start, size_t len)
282 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
283 size_t msglen = 12; /* start + len */
285 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
286 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
288 if (rbname) {
289 int rbname_len = strlen(rbname);
290 assert(rbname_len < 256);
292 bufc[msglen++] = rbname_len;
293 memcpy(bufc + msglen, rbname, rbname_len);
294 msglen += rbname_len;
295 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
296 } else {
297 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
301 void qemu_start_incoming_migration(const char *uri, Error **errp)
303 const char *p;
305 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
306 if (!strcmp(uri, "defer")) {
307 deferred_incoming_migration(errp);
308 } else if (strstart(uri, "tcp:", &p)) {
309 tcp_start_incoming_migration(p, errp);
310 #ifdef CONFIG_RDMA
311 } else if (strstart(uri, "rdma:", &p)) {
312 rdma_start_incoming_migration(p, errp);
313 #endif
314 } else if (strstart(uri, "exec:", &p)) {
315 exec_start_incoming_migration(p, errp);
316 } else if (strstart(uri, "unix:", &p)) {
317 unix_start_incoming_migration(p, errp);
318 } else if (strstart(uri, "fd:", &p)) {
319 fd_start_incoming_migration(p, errp);
320 } else {
321 error_setg(errp, "unknown migration protocol: %s", uri);
325 static void process_incoming_migration_bh(void *opaque)
327 Error *local_err = NULL;
328 MigrationIncomingState *mis = opaque;
330 /* Make sure all file formats flush their mutable metadata */
331 bdrv_invalidate_cache_all(&local_err);
332 if (local_err) {
333 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
334 MIGRATION_STATUS_FAILED);
335 error_report_err(local_err);
336 migrate_decompress_threads_join();
337 exit(EXIT_FAILURE);
341 * This must happen after all error conditions are dealt with and
342 * we're sure the VM is going to be running on this host.
344 qemu_announce_self();
346 /* If global state section was not received or we are in running
347 state, we need to obey autostart. Any other state is set with
348 runstate_set. */
350 if (!global_state_received() ||
351 global_state_get_runstate() == RUN_STATE_RUNNING) {
352 if (autostart) {
353 vm_start();
354 } else {
355 runstate_set(RUN_STATE_PAUSED);
357 } else {
358 runstate_set(global_state_get_runstate());
360 migrate_decompress_threads_join();
362 * This must happen after any state changes since as soon as an external
363 * observer sees this event they might start to prod at the VM assuming
364 * it's ready to use.
366 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
367 MIGRATION_STATUS_COMPLETED);
368 qemu_bh_delete(mis->bh);
369 migration_incoming_state_destroy();
372 static void process_incoming_migration_co(void *opaque)
374 QEMUFile *f = opaque;
375 MigrationIncomingState *mis;
376 PostcopyState ps;
377 int ret;
379 mis = migration_incoming_state_new(f);
380 postcopy_state_set(POSTCOPY_INCOMING_NONE);
381 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
382 MIGRATION_STATUS_ACTIVE);
383 ret = qemu_loadvm_state(f);
385 ps = postcopy_state_get();
386 trace_process_incoming_migration_co_end(ret, ps);
387 if (ps != POSTCOPY_INCOMING_NONE) {
388 if (ps == POSTCOPY_INCOMING_ADVISE) {
390 * Where a migration had postcopy enabled (and thus went to advise)
391 * but managed to complete within the precopy period, we can use
392 * the normal exit.
394 postcopy_ram_incoming_cleanup(mis);
395 } else if (ret >= 0) {
397 * Postcopy was started, cleanup should happen at the end of the
398 * postcopy thread.
400 trace_process_incoming_migration_co_postcopy_end_main();
401 return;
403 /* Else if something went wrong then just fall out of the normal exit */
406 qemu_fclose(f);
407 free_xbzrle_decoded_buf();
409 if (ret < 0) {
410 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
411 MIGRATION_STATUS_FAILED);
412 error_report("load of migration failed: %s", strerror(-ret));
413 migrate_decompress_threads_join();
414 exit(EXIT_FAILURE);
417 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
418 qemu_bh_schedule(mis->bh);
421 void process_incoming_migration(QEMUFile *f)
423 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
425 migrate_decompress_threads_create();
426 qemu_file_set_blocking(f, false);
427 qemu_coroutine_enter(co, f);
431 void migration_set_incoming_channel(MigrationState *s,
432 QIOChannel *ioc)
434 QEMUFile *f = qemu_fopen_channel_input(ioc);
436 process_incoming_migration(f);
440 void migration_set_outgoing_channel(MigrationState *s,
441 QIOChannel *ioc)
443 QEMUFile *f = qemu_fopen_channel_output(ioc);
445 s->to_dst_file = f;
447 migrate_fd_connect(s);
452 * Send a message on the return channel back to the source
453 * of the migration.
455 void migrate_send_rp_message(MigrationIncomingState *mis,
456 enum mig_rp_message_type message_type,
457 uint16_t len, void *data)
459 trace_migrate_send_rp_message((int)message_type, len);
460 qemu_mutex_lock(&mis->rp_mutex);
461 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
462 qemu_put_be16(mis->to_src_file, len);
463 qemu_put_buffer(mis->to_src_file, data, len);
464 qemu_fflush(mis->to_src_file);
465 qemu_mutex_unlock(&mis->rp_mutex);
469 * Send a 'SHUT' message on the return channel with the given value
470 * to indicate that we've finished with the RP. Non-0 value indicates
471 * error.
473 void migrate_send_rp_shut(MigrationIncomingState *mis,
474 uint32_t value)
476 uint32_t buf;
478 buf = cpu_to_be32(value);
479 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
483 * Send a 'PONG' message on the return channel with the given value
484 * (normally in response to a 'PING')
486 void migrate_send_rp_pong(MigrationIncomingState *mis,
487 uint32_t value)
489 uint32_t buf;
491 buf = cpu_to_be32(value);
492 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
495 /* amount of nanoseconds we are willing to wait for migration to be down.
496 * the choice of nanoseconds is because it is the maximum resolution that
497 * get_clock() can achieve. It is an internal measure. All user-visible
498 * units must be in seconds */
499 static uint64_t max_downtime = 300000000;
501 uint64_t migrate_max_downtime(void)
503 return max_downtime;
506 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
508 MigrationCapabilityStatusList *head = NULL;
509 MigrationCapabilityStatusList *caps;
510 MigrationState *s = migrate_get_current();
511 int i;
513 caps = NULL; /* silence compiler warning */
514 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
515 if (head == NULL) {
516 head = g_malloc0(sizeof(*caps));
517 caps = head;
518 } else {
519 caps->next = g_malloc0(sizeof(*caps));
520 caps = caps->next;
522 caps->value =
523 g_malloc(sizeof(*caps->value));
524 caps->value->capability = i;
525 caps->value->state = s->enabled_capabilities[i];
528 return head;
531 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
533 MigrationParameters *params;
534 MigrationState *s = migrate_get_current();
536 params = g_malloc0(sizeof(*params));
537 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
538 params->compress_threads =
539 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
540 params->decompress_threads =
541 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
542 params->cpu_throttle_initial =
543 s->parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL];
544 params->cpu_throttle_increment =
545 s->parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT];
547 return params;
551 * Return true if we're already in the middle of a migration
552 * (i.e. any of the active or setup states)
554 static bool migration_is_setup_or_active(int state)
556 switch (state) {
557 case MIGRATION_STATUS_ACTIVE:
558 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
559 case MIGRATION_STATUS_SETUP:
560 return true;
562 default:
563 return false;
568 static void get_xbzrle_cache_stats(MigrationInfo *info)
570 if (migrate_use_xbzrle()) {
571 info->has_xbzrle_cache = true;
572 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
573 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
574 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
575 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
576 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
577 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
578 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
582 MigrationInfo *qmp_query_migrate(Error **errp)
584 MigrationInfo *info = g_malloc0(sizeof(*info));
585 MigrationState *s = migrate_get_current();
587 switch (s->state) {
588 case MIGRATION_STATUS_NONE:
589 /* no migration has happened ever */
590 break;
591 case MIGRATION_STATUS_SETUP:
592 info->has_status = true;
593 info->has_total_time = false;
594 break;
595 case MIGRATION_STATUS_ACTIVE:
596 case MIGRATION_STATUS_CANCELLING:
597 info->has_status = true;
598 info->has_total_time = true;
599 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
600 - s->total_time;
601 info->has_expected_downtime = true;
602 info->expected_downtime = s->expected_downtime;
603 info->has_setup_time = true;
604 info->setup_time = s->setup_time;
606 info->has_ram = true;
607 info->ram = g_malloc0(sizeof(*info->ram));
608 info->ram->transferred = ram_bytes_transferred();
609 info->ram->remaining = ram_bytes_remaining();
610 info->ram->total = ram_bytes_total();
611 info->ram->duplicate = dup_mig_pages_transferred();
612 info->ram->skipped = skipped_mig_pages_transferred();
613 info->ram->normal = norm_mig_pages_transferred();
614 info->ram->normal_bytes = norm_mig_bytes_transferred();
615 info->ram->dirty_pages_rate = s->dirty_pages_rate;
616 info->ram->mbps = s->mbps;
617 info->ram->dirty_sync_count = s->dirty_sync_count;
619 if (blk_mig_active()) {
620 info->has_disk = true;
621 info->disk = g_malloc0(sizeof(*info->disk));
622 info->disk->transferred = blk_mig_bytes_transferred();
623 info->disk->remaining = blk_mig_bytes_remaining();
624 info->disk->total = blk_mig_bytes_total();
627 if (cpu_throttle_active()) {
628 info->has_cpu_throttle_percentage = true;
629 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
632 get_xbzrle_cache_stats(info);
633 break;
634 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
635 /* Mostly the same as active; TODO add some postcopy stats */
636 info->has_status = true;
637 info->has_total_time = true;
638 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
639 - s->total_time;
640 info->has_expected_downtime = true;
641 info->expected_downtime = s->expected_downtime;
642 info->has_setup_time = true;
643 info->setup_time = s->setup_time;
645 info->has_ram = true;
646 info->ram = g_malloc0(sizeof(*info->ram));
647 info->ram->transferred = ram_bytes_transferred();
648 info->ram->remaining = ram_bytes_remaining();
649 info->ram->total = ram_bytes_total();
650 info->ram->duplicate = dup_mig_pages_transferred();
651 info->ram->skipped = skipped_mig_pages_transferred();
652 info->ram->normal = norm_mig_pages_transferred();
653 info->ram->normal_bytes = norm_mig_bytes_transferred();
654 info->ram->dirty_pages_rate = s->dirty_pages_rate;
655 info->ram->mbps = s->mbps;
656 info->ram->dirty_sync_count = s->dirty_sync_count;
658 if (blk_mig_active()) {
659 info->has_disk = true;
660 info->disk = g_malloc0(sizeof(*info->disk));
661 info->disk->transferred = blk_mig_bytes_transferred();
662 info->disk->remaining = blk_mig_bytes_remaining();
663 info->disk->total = blk_mig_bytes_total();
666 get_xbzrle_cache_stats(info);
667 break;
668 case MIGRATION_STATUS_COMPLETED:
669 get_xbzrle_cache_stats(info);
671 info->has_status = true;
672 info->has_total_time = true;
673 info->total_time = s->total_time;
674 info->has_downtime = true;
675 info->downtime = s->downtime;
676 info->has_setup_time = true;
677 info->setup_time = s->setup_time;
679 info->has_ram = true;
680 info->ram = g_malloc0(sizeof(*info->ram));
681 info->ram->transferred = ram_bytes_transferred();
682 info->ram->remaining = 0;
683 info->ram->total = ram_bytes_total();
684 info->ram->duplicate = dup_mig_pages_transferred();
685 info->ram->skipped = skipped_mig_pages_transferred();
686 info->ram->normal = norm_mig_pages_transferred();
687 info->ram->normal_bytes = norm_mig_bytes_transferred();
688 info->ram->mbps = s->mbps;
689 info->ram->dirty_sync_count = s->dirty_sync_count;
690 break;
691 case MIGRATION_STATUS_FAILED:
692 info->has_status = true;
693 if (s->error) {
694 info->has_error_desc = true;
695 info->error_desc = g_strdup(error_get_pretty(s->error));
697 break;
698 case MIGRATION_STATUS_CANCELLED:
699 info->has_status = true;
700 break;
702 info->status = s->state;
704 return info;
707 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
708 Error **errp)
710 MigrationState *s = migrate_get_current();
711 MigrationCapabilityStatusList *cap;
713 if (migration_is_setup_or_active(s->state)) {
714 error_setg(errp, QERR_MIGRATION_ACTIVE);
715 return;
718 for (cap = params; cap; cap = cap->next) {
719 s->enabled_capabilities[cap->value->capability] = cap->value->state;
722 if (migrate_postcopy_ram()) {
723 if (migrate_use_compression()) {
724 /* The decompression threads asynchronously write into RAM
725 * rather than use the atomic copies needed to avoid
726 * userfaulting. It should be possible to fix the decompression
727 * threads for compatibility in future.
729 error_report("Postcopy is not currently compatible with "
730 "compression");
731 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] =
732 false;
737 void qmp_migrate_set_parameters(bool has_compress_level,
738 int64_t compress_level,
739 bool has_compress_threads,
740 int64_t compress_threads,
741 bool has_decompress_threads,
742 int64_t decompress_threads,
743 bool has_cpu_throttle_initial,
744 int64_t cpu_throttle_initial,
745 bool has_cpu_throttle_increment,
746 int64_t cpu_throttle_increment, Error **errp)
748 MigrationState *s = migrate_get_current();
750 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
751 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
752 "is invalid, it should be in the range of 0 to 9");
753 return;
755 if (has_compress_threads &&
756 (compress_threads < 1 || compress_threads > 255)) {
757 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
758 "compress_threads",
759 "is invalid, it should be in the range of 1 to 255");
760 return;
762 if (has_decompress_threads &&
763 (decompress_threads < 1 || decompress_threads > 255)) {
764 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
765 "decompress_threads",
766 "is invalid, it should be in the range of 1 to 255");
767 return;
769 if (has_cpu_throttle_initial &&
770 (cpu_throttle_initial < 1 || cpu_throttle_initial > 99)) {
771 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
772 "cpu_throttle_initial",
773 "an integer in the range of 1 to 99");
775 if (has_cpu_throttle_increment &&
776 (cpu_throttle_increment < 1 || cpu_throttle_increment > 99)) {
777 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
778 "cpu_throttle_increment",
779 "an integer in the range of 1 to 99");
782 if (has_compress_level) {
783 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
785 if (has_compress_threads) {
786 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
788 if (has_decompress_threads) {
789 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
790 decompress_threads;
792 if (has_cpu_throttle_initial) {
793 s->parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL] =
794 cpu_throttle_initial;
797 if (has_cpu_throttle_increment) {
798 s->parameters[MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT] =
799 cpu_throttle_increment;
803 void qmp_migrate_start_postcopy(Error **errp)
805 MigrationState *s = migrate_get_current();
807 if (!migrate_postcopy_ram()) {
808 error_setg(errp, "Enable postcopy with migrate_set_capability before"
809 " the start of migration");
810 return;
813 if (s->state == MIGRATION_STATUS_NONE) {
814 error_setg(errp, "Postcopy must be started after migration has been"
815 " started");
816 return;
819 * we don't error if migration has finished since that would be racy
820 * with issuing this command.
822 atomic_set(&s->start_postcopy, true);
825 /* shared migration helpers */
827 void migrate_set_state(int *state, int old_state, int new_state)
829 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
830 trace_migrate_set_state(new_state);
831 migrate_generate_event(new_state);
835 static void migrate_fd_cleanup(void *opaque)
837 MigrationState *s = opaque;
839 qemu_bh_delete(s->cleanup_bh);
840 s->cleanup_bh = NULL;
842 flush_page_queue(s);
844 if (s->to_dst_file) {
845 trace_migrate_fd_cleanup();
846 qemu_mutex_unlock_iothread();
847 if (s->migration_thread_running) {
848 qemu_thread_join(&s->thread);
849 s->migration_thread_running = false;
851 qemu_mutex_lock_iothread();
853 migrate_compress_threads_join();
854 qemu_fclose(s->to_dst_file);
855 s->to_dst_file = NULL;
858 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
859 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
861 if (s->state == MIGRATION_STATUS_CANCELLING) {
862 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
863 MIGRATION_STATUS_CANCELLED);
866 notifier_list_notify(&migration_state_notifiers, s);
869 void migrate_fd_error(MigrationState *s, const Error *error)
871 trace_migrate_fd_error(error ? error_get_pretty(error) : "");
872 assert(s->to_dst_file == NULL);
873 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
874 MIGRATION_STATUS_FAILED);
875 if (!s->error) {
876 s->error = error_copy(error);
878 notifier_list_notify(&migration_state_notifiers, s);
881 static void migrate_fd_cancel(MigrationState *s)
883 int old_state ;
884 QEMUFile *f = migrate_get_current()->to_dst_file;
885 trace_migrate_fd_cancel();
887 if (s->rp_state.from_dst_file) {
888 /* shutdown the rp socket, so causing the rp thread to shutdown */
889 qemu_file_shutdown(s->rp_state.from_dst_file);
892 do {
893 old_state = s->state;
894 if (!migration_is_setup_or_active(old_state)) {
895 break;
897 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
898 } while (s->state != MIGRATION_STATUS_CANCELLING);
901 * If we're unlucky the migration code might be stuck somewhere in a
902 * send/write while the network has failed and is waiting to timeout;
903 * if we've got shutdown(2) available then we can force it to quit.
904 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
905 * called in a bh, so there is no race against this cancel.
907 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
908 qemu_file_shutdown(f);
912 void add_migration_state_change_notifier(Notifier *notify)
914 notifier_list_add(&migration_state_notifiers, notify);
917 void remove_migration_state_change_notifier(Notifier *notify)
919 notifier_remove(notify);
922 bool migration_in_setup(MigrationState *s)
924 return s->state == MIGRATION_STATUS_SETUP;
927 bool migration_has_finished(MigrationState *s)
929 return s->state == MIGRATION_STATUS_COMPLETED;
932 bool migration_has_failed(MigrationState *s)
934 return (s->state == MIGRATION_STATUS_CANCELLED ||
935 s->state == MIGRATION_STATUS_FAILED);
938 bool migration_in_postcopy(MigrationState *s)
940 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
943 bool migration_in_postcopy_after_devices(MigrationState *s)
945 return migration_in_postcopy(s) && s->postcopy_after_devices;
948 MigrationState *migrate_init(const MigrationParams *params)
950 MigrationState *s = migrate_get_current();
953 * Reinitialise all migration state, except
954 * parameters/capabilities that the user set, and
955 * locks.
957 s->bytes_xfer = 0;
958 s->xfer_limit = 0;
959 s->cleanup_bh = 0;
960 s->to_dst_file = NULL;
961 s->state = MIGRATION_STATUS_NONE;
962 s->params = *params;
963 s->rp_state.from_dst_file = NULL;
964 s->rp_state.error = false;
965 s->mbps = 0.0;
966 s->downtime = 0;
967 s->expected_downtime = 0;
968 s->dirty_pages_rate = 0;
969 s->dirty_bytes_rate = 0;
970 s->setup_time = 0;
971 s->dirty_sync_count = 0;
972 s->start_postcopy = false;
973 s->postcopy_after_devices = false;
974 s->migration_thread_running = false;
975 s->last_req_rb = NULL;
976 error_free(s->error);
977 s->error = NULL;
979 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
981 QSIMPLEQ_INIT(&s->src_page_requests);
983 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
984 return s;
987 static GSList *migration_blockers;
989 void migrate_add_blocker(Error *reason)
991 migration_blockers = g_slist_prepend(migration_blockers, reason);
994 void migrate_del_blocker(Error *reason)
996 migration_blockers = g_slist_remove(migration_blockers, reason);
999 void qmp_migrate_incoming(const char *uri, Error **errp)
1001 Error *local_err = NULL;
1002 static bool once = true;
1004 if (!deferred_incoming) {
1005 error_setg(errp, "For use with '-incoming defer'");
1006 return;
1008 if (!once) {
1009 error_setg(errp, "The incoming migration has already been started");
1012 qemu_start_incoming_migration(uri, &local_err);
1014 if (local_err) {
1015 error_propagate(errp, local_err);
1016 return;
1019 once = false;
1022 bool migration_is_blocked(Error **errp)
1024 if (qemu_savevm_state_blocked(errp)) {
1025 return true;
1028 if (migration_blockers) {
1029 *errp = error_copy(migration_blockers->data);
1030 return true;
1033 return false;
1036 void qmp_migrate(const char *uri, bool has_blk, bool blk,
1037 bool has_inc, bool inc, bool has_detach, bool detach,
1038 Error **errp)
1040 Error *local_err = NULL;
1041 MigrationState *s = migrate_get_current();
1042 MigrationParams params;
1043 const char *p;
1045 params.blk = has_blk && blk;
1046 params.shared = has_inc && inc;
1048 if (migration_is_setup_or_active(s->state) ||
1049 s->state == MIGRATION_STATUS_CANCELLING) {
1050 error_setg(errp, QERR_MIGRATION_ACTIVE);
1051 return;
1053 if (runstate_check(RUN_STATE_INMIGRATE)) {
1054 error_setg(errp, "Guest is waiting for an incoming migration");
1055 return;
1058 if (migration_is_blocked(errp)) {
1059 return;
1062 s = migrate_init(&params);
1064 if (strstart(uri, "tcp:", &p)) {
1065 tcp_start_outgoing_migration(s, p, &local_err);
1066 #ifdef CONFIG_RDMA
1067 } else if (strstart(uri, "rdma:", &p)) {
1068 rdma_start_outgoing_migration(s, p, &local_err);
1069 #endif
1070 } else if (strstart(uri, "exec:", &p)) {
1071 exec_start_outgoing_migration(s, p, &local_err);
1072 } else if (strstart(uri, "unix:", &p)) {
1073 unix_start_outgoing_migration(s, p, &local_err);
1074 } else if (strstart(uri, "fd:", &p)) {
1075 fd_start_outgoing_migration(s, p, &local_err);
1076 } else {
1077 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1078 "a valid migration protocol");
1079 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1080 MIGRATION_STATUS_FAILED);
1081 return;
1084 if (local_err) {
1085 migrate_fd_error(s, local_err);
1086 error_propagate(errp, local_err);
1087 return;
1091 void qmp_migrate_cancel(Error **errp)
1093 migrate_fd_cancel(migrate_get_current());
1096 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1098 MigrationState *s = migrate_get_current();
1099 int64_t new_size;
1101 /* Check for truncation */
1102 if (value != (size_t)value) {
1103 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1104 "exceeding address space");
1105 return;
1108 /* Cache should not be larger than guest ram size */
1109 if (value > ram_bytes_total()) {
1110 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1111 "exceeds guest ram size ");
1112 return;
1115 new_size = xbzrle_cache_resize(value);
1116 if (new_size < 0) {
1117 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1118 "is smaller than page size");
1119 return;
1122 s->xbzrle_cache_size = new_size;
1125 int64_t qmp_query_migrate_cache_size(Error **errp)
1127 return migrate_xbzrle_cache_size();
1130 void qmp_migrate_set_speed(int64_t value, Error **errp)
1132 MigrationState *s;
1134 if (value < 0) {
1135 value = 0;
1137 if (value > SIZE_MAX) {
1138 value = SIZE_MAX;
1141 s = migrate_get_current();
1142 s->bandwidth_limit = value;
1143 if (s->to_dst_file) {
1144 qemu_file_set_rate_limit(s->to_dst_file,
1145 s->bandwidth_limit / XFER_LIMIT_RATIO);
1149 void qmp_migrate_set_downtime(double value, Error **errp)
1151 value *= 1e9;
1152 value = MAX(0, MIN(UINT64_MAX, value));
1153 max_downtime = (uint64_t)value;
1156 bool migrate_postcopy_ram(void)
1158 MigrationState *s;
1160 s = migrate_get_current();
1162 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
1165 bool migrate_auto_converge(void)
1167 MigrationState *s;
1169 s = migrate_get_current();
1171 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1174 bool migrate_zero_blocks(void)
1176 MigrationState *s;
1178 s = migrate_get_current();
1180 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1183 bool migrate_use_compression(void)
1185 MigrationState *s;
1187 s = migrate_get_current();
1189 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
1192 int migrate_compress_level(void)
1194 MigrationState *s;
1196 s = migrate_get_current();
1198 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
1201 int migrate_compress_threads(void)
1203 MigrationState *s;
1205 s = migrate_get_current();
1207 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
1210 int migrate_decompress_threads(void)
1212 MigrationState *s;
1214 s = migrate_get_current();
1216 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
1219 bool migrate_use_events(void)
1221 MigrationState *s;
1223 s = migrate_get_current();
1225 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1228 int migrate_use_xbzrle(void)
1230 MigrationState *s;
1232 s = migrate_get_current();
1234 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1237 int64_t migrate_xbzrle_cache_size(void)
1239 MigrationState *s;
1241 s = migrate_get_current();
1243 return s->xbzrle_cache_size;
1246 /* migration thread support */
1248 * Something bad happened to the RP stream, mark an error
1249 * The caller shall print or trace something to indicate why
1251 static void mark_source_rp_bad(MigrationState *s)
1253 s->rp_state.error = true;
1256 static struct rp_cmd_args {
1257 ssize_t len; /* -1 = variable */
1258 const char *name;
1259 } rp_cmd_args[] = {
1260 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1261 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1262 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
1263 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1264 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
1265 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1269 * Process a request for pages received on the return path,
1270 * We're allowed to send more than requested (e.g. to round to our page size)
1271 * and we don't need to send pages that have already been sent.
1273 static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1274 ram_addr_t start, size_t len)
1276 long our_host_ps = getpagesize();
1278 trace_migrate_handle_rp_req_pages(rbname, start, len);
1281 * Since we currently insist on matching page sizes, just sanity check
1282 * we're being asked for whole host pages.
1284 if (start & (our_host_ps-1) ||
1285 (len & (our_host_ps-1))) {
1286 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1287 " len: %zd", __func__, start, len);
1288 mark_source_rp_bad(ms);
1289 return;
1292 if (ram_save_queue_pages(ms, rbname, start, len)) {
1293 mark_source_rp_bad(ms);
1298 * Handles messages sent on the return path towards the source VM
1301 static void *source_return_path_thread(void *opaque)
1303 MigrationState *ms = opaque;
1304 QEMUFile *rp = ms->rp_state.from_dst_file;
1305 uint16_t header_len, header_type;
1306 uint8_t buf[512];
1307 uint32_t tmp32, sibling_error;
1308 ram_addr_t start = 0; /* =0 to silence warning */
1309 size_t len = 0, expected_len;
1310 int res;
1312 trace_source_return_path_thread_entry();
1313 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1314 migration_is_setup_or_active(ms->state)) {
1315 trace_source_return_path_thread_loop_top();
1316 header_type = qemu_get_be16(rp);
1317 header_len = qemu_get_be16(rp);
1319 if (header_type >= MIG_RP_MSG_MAX ||
1320 header_type == MIG_RP_MSG_INVALID) {
1321 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1322 header_type, header_len);
1323 mark_source_rp_bad(ms);
1324 goto out;
1327 if ((rp_cmd_args[header_type].len != -1 &&
1328 header_len != rp_cmd_args[header_type].len) ||
1329 header_len > sizeof(buf)) {
1330 error_report("RP: Received '%s' message (0x%04x) with"
1331 "incorrect length %d expecting %zu",
1332 rp_cmd_args[header_type].name, header_type, header_len,
1333 (size_t)rp_cmd_args[header_type].len);
1334 mark_source_rp_bad(ms);
1335 goto out;
1338 /* We know we've got a valid header by this point */
1339 res = qemu_get_buffer(rp, buf, header_len);
1340 if (res != header_len) {
1341 error_report("RP: Failed reading data for message 0x%04x"
1342 " read %d expected %d",
1343 header_type, res, header_len);
1344 mark_source_rp_bad(ms);
1345 goto out;
1348 /* OK, we have the message and the data */
1349 switch (header_type) {
1350 case MIG_RP_MSG_SHUT:
1351 sibling_error = be32_to_cpup((uint32_t *)buf);
1352 trace_source_return_path_thread_shut(sibling_error);
1353 if (sibling_error) {
1354 error_report("RP: Sibling indicated error %d", sibling_error);
1355 mark_source_rp_bad(ms);
1358 * We'll let the main thread deal with closing the RP
1359 * we could do a shutdown(2) on it, but we're the only user
1360 * anyway, so there's nothing gained.
1362 goto out;
1364 case MIG_RP_MSG_PONG:
1365 tmp32 = be32_to_cpup((uint32_t *)buf);
1366 trace_source_return_path_thread_pong(tmp32);
1367 break;
1369 case MIG_RP_MSG_REQ_PAGES:
1370 start = be64_to_cpup((uint64_t *)buf);
1371 len = be32_to_cpup((uint32_t *)(buf + 8));
1372 migrate_handle_rp_req_pages(ms, NULL, start, len);
1373 break;
1375 case MIG_RP_MSG_REQ_PAGES_ID:
1376 expected_len = 12 + 1; /* header + termination */
1378 if (header_len >= expected_len) {
1379 start = be64_to_cpup((uint64_t *)buf);
1380 len = be32_to_cpup((uint32_t *)(buf + 8));
1381 /* Now we expect an idstr */
1382 tmp32 = buf[12]; /* Length of the following idstr */
1383 buf[13 + tmp32] = '\0';
1384 expected_len += tmp32;
1386 if (header_len != expected_len) {
1387 error_report("RP: Req_Page_id with length %d expecting %zd",
1388 header_len, expected_len);
1389 mark_source_rp_bad(ms);
1390 goto out;
1392 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1393 break;
1395 default:
1396 break;
1399 if (qemu_file_get_error(rp)) {
1400 trace_source_return_path_thread_bad_end();
1401 mark_source_rp_bad(ms);
1404 trace_source_return_path_thread_end();
1405 out:
1406 ms->rp_state.from_dst_file = NULL;
1407 qemu_fclose(rp);
1408 return NULL;
1411 static int open_return_path_on_source(MigrationState *ms)
1414 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
1415 if (!ms->rp_state.from_dst_file) {
1416 return -1;
1419 trace_open_return_path_on_source();
1420 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1421 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1423 trace_open_return_path_on_source_continue();
1425 return 0;
1428 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1429 static int await_return_path_close_on_source(MigrationState *ms)
1432 * If this is a normal exit then the destination will send a SHUT and the
1433 * rp_thread will exit, however if there's an error we need to cause
1434 * it to exit.
1436 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
1438 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1439 * waiting for the destination.
1441 qemu_file_shutdown(ms->rp_state.from_dst_file);
1442 mark_source_rp_bad(ms);
1444 trace_await_return_path_close_on_source_joining();
1445 qemu_thread_join(&ms->rp_state.rp_thread);
1446 trace_await_return_path_close_on_source_close();
1447 return ms->rp_state.error;
1451 * Switch from normal iteration to postcopy
1452 * Returns non-0 on error
1454 static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1456 int ret;
1457 QIOChannelBuffer *bioc;
1458 QEMUFile *fb;
1459 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1460 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
1461 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1463 trace_postcopy_start();
1464 qemu_mutex_lock_iothread();
1465 trace_postcopy_start_set_run();
1467 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1468 *old_vm_running = runstate_is_running();
1469 global_state_store();
1470 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1471 if (ret < 0) {
1472 goto fail;
1475 ret = bdrv_inactivate_all();
1476 if (ret < 0) {
1477 goto fail;
1481 * Cause any non-postcopiable, but iterative devices to
1482 * send out their final data.
1484 qemu_savevm_state_complete_precopy(ms->to_dst_file, true);
1487 * in Finish migrate and with the io-lock held everything should
1488 * be quiet, but we've potentially still got dirty pages and we
1489 * need to tell the destination to throw any pages it's already received
1490 * that are dirty
1492 if (ram_postcopy_send_discard_bitmap(ms)) {
1493 error_report("postcopy send discard bitmap failed");
1494 goto fail;
1498 * send rest of state - note things that are doing postcopy
1499 * will notice we're in POSTCOPY_ACTIVE and not actually
1500 * wrap their state up here
1502 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
1503 /* Ping just for debugging, helps line traces up */
1504 qemu_savevm_send_ping(ms->to_dst_file, 2);
1507 * While loading the device state we may trigger page transfer
1508 * requests and the fd must be free to process those, and thus
1509 * the destination must read the whole device state off the fd before
1510 * it starts processing it. Unfortunately the ad-hoc migration format
1511 * doesn't allow the destination to know the size to read without fully
1512 * parsing it through each devices load-state code (especially the open
1513 * coded devices that use get/put).
1514 * So we wrap the device state up in a package with a length at the start;
1515 * to do this we use a qemu_buf to hold the whole of the device state.
1517 bioc = qio_channel_buffer_new(4096);
1518 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1519 object_unref(OBJECT(bioc));
1522 * Make sure the receiver can get incoming pages before we send the rest
1523 * of the state
1525 qemu_savevm_send_postcopy_listen(fb);
1527 qemu_savevm_state_complete_precopy(fb, false);
1528 qemu_savevm_send_ping(fb, 3);
1530 qemu_savevm_send_postcopy_run(fb);
1532 /* <><> end of stuff going into the package */
1534 /* Now send that blob */
1535 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
1536 goto fail_closefb;
1538 qemu_fclose(fb);
1540 /* Send a notify to give a chance for anything that needs to happen
1541 * at the transition to postcopy and after the device state; in particular
1542 * spice needs to trigger a transition now
1544 ms->postcopy_after_devices = true;
1545 notifier_list_notify(&migration_state_notifiers, ms);
1547 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1549 qemu_mutex_unlock_iothread();
1552 * Although this ping is just for debug, it could potentially be
1553 * used for getting a better measurement of downtime at the source.
1555 qemu_savevm_send_ping(ms->to_dst_file, 4);
1557 ret = qemu_file_get_error(ms->to_dst_file);
1558 if (ret) {
1559 error_report("postcopy_start: Migration stream errored");
1560 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1561 MIGRATION_STATUS_FAILED);
1564 return ret;
1566 fail_closefb:
1567 qemu_fclose(fb);
1568 fail:
1569 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1570 MIGRATION_STATUS_FAILED);
1571 qemu_mutex_unlock_iothread();
1572 return -1;
1576 * migration_completion: Used by migration_thread when there's not much left.
1577 * The caller 'breaks' the loop when this returns.
1579 * @s: Current migration state
1580 * @current_active_state: The migration state we expect to be in
1581 * @*old_vm_running: Pointer to old_vm_running flag
1582 * @*start_time: Pointer to time to update
1584 static void migration_completion(MigrationState *s, int current_active_state,
1585 bool *old_vm_running,
1586 int64_t *start_time)
1588 int ret;
1590 if (s->state == MIGRATION_STATUS_ACTIVE) {
1591 qemu_mutex_lock_iothread();
1592 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1593 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1594 *old_vm_running = runstate_is_running();
1595 ret = global_state_store();
1597 if (!ret) {
1598 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1599 if (ret >= 0) {
1600 ret = bdrv_inactivate_all();
1602 if (ret >= 0) {
1603 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
1604 qemu_savevm_state_complete_precopy(s->to_dst_file, false);
1607 qemu_mutex_unlock_iothread();
1609 if (ret < 0) {
1610 goto fail;
1612 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1613 trace_migration_completion_postcopy_end();
1615 qemu_savevm_state_complete_postcopy(s->to_dst_file);
1616 trace_migration_completion_postcopy_end_after_complete();
1620 * If rp was opened we must clean up the thread before
1621 * cleaning everything else up (since if there are no failures
1622 * it will wait for the destination to send it's status in
1623 * a SHUT command).
1624 * Postcopy opens rp if enabled (even if it's not avtivated)
1626 if (migrate_postcopy_ram()) {
1627 int rp_error;
1628 trace_migration_completion_postcopy_end_before_rp();
1629 rp_error = await_return_path_close_on_source(s);
1630 trace_migration_completion_postcopy_end_after_rp(rp_error);
1631 if (rp_error) {
1632 goto fail_invalidate;
1636 if (qemu_file_get_error(s->to_dst_file)) {
1637 trace_migration_completion_file_err();
1638 goto fail_invalidate;
1641 migrate_set_state(&s->state, current_active_state,
1642 MIGRATION_STATUS_COMPLETED);
1643 return;
1645 fail_invalidate:
1646 /* If not doing postcopy, vm_start() will be called: let's regain
1647 * control on images.
1649 if (s->state == MIGRATION_STATUS_ACTIVE) {
1650 Error *local_err = NULL;
1652 bdrv_invalidate_cache_all(&local_err);
1653 if (local_err) {
1654 error_report_err(local_err);
1658 fail:
1659 migrate_set_state(&s->state, current_active_state,
1660 MIGRATION_STATUS_FAILED);
1664 * Master migration thread on the source VM.
1665 * It drives the migration and pumps the data down the outgoing channel.
1667 static void *migration_thread(void *opaque)
1669 MigrationState *s = opaque;
1670 /* Used by the bandwidth calcs, updated later */
1671 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1672 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
1673 int64_t initial_bytes = 0;
1674 int64_t max_size = 0;
1675 int64_t start_time = initial_time;
1676 int64_t end_time;
1677 bool old_vm_running = false;
1678 bool entered_postcopy = false;
1679 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1680 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
1682 rcu_register_thread();
1684 qemu_savevm_state_header(s->to_dst_file);
1686 if (migrate_postcopy_ram()) {
1687 /* Now tell the dest that it should open its end so it can reply */
1688 qemu_savevm_send_open_return_path(s->to_dst_file);
1690 /* And do a ping that will make stuff easier to debug */
1691 qemu_savevm_send_ping(s->to_dst_file, 1);
1694 * Tell the destination that we *might* want to do postcopy later;
1695 * if the other end can't do postcopy it should fail now, nice and
1696 * early.
1698 qemu_savevm_send_postcopy_advise(s->to_dst_file);
1701 qemu_savevm_state_begin(s->to_dst_file, &s->params);
1703 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
1704 current_active_state = MIGRATION_STATUS_ACTIVE;
1705 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1706 MIGRATION_STATUS_ACTIVE);
1708 trace_migration_thread_setup_complete();
1710 while (s->state == MIGRATION_STATUS_ACTIVE ||
1711 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1712 int64_t current_time;
1713 uint64_t pending_size;
1715 if (!qemu_file_rate_limit(s->to_dst_file)) {
1716 uint64_t pend_post, pend_nonpost;
1718 qemu_savevm_state_pending(s->to_dst_file, max_size, &pend_nonpost,
1719 &pend_post);
1720 pending_size = pend_nonpost + pend_post;
1721 trace_migrate_pending(pending_size, max_size,
1722 pend_post, pend_nonpost);
1723 if (pending_size && pending_size >= max_size) {
1724 /* Still a significant amount to transfer */
1726 if (migrate_postcopy_ram() &&
1727 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
1728 pend_nonpost <= max_size &&
1729 atomic_read(&s->start_postcopy)) {
1731 if (!postcopy_start(s, &old_vm_running)) {
1732 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1733 entered_postcopy = true;
1736 continue;
1738 /* Just another iteration step */
1739 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
1740 } else {
1741 trace_migration_thread_low_pending(pending_size);
1742 migration_completion(s, current_active_state,
1743 &old_vm_running, &start_time);
1744 break;
1748 if (qemu_file_get_error(s->to_dst_file)) {
1749 migrate_set_state(&s->state, current_active_state,
1750 MIGRATION_STATUS_FAILED);
1751 trace_migration_thread_file_err();
1752 break;
1754 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1755 if (current_time >= initial_time + BUFFER_DELAY) {
1756 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
1757 initial_bytes;
1758 uint64_t time_spent = current_time - initial_time;
1759 double bandwidth = (double)transferred_bytes / time_spent;
1760 max_size = bandwidth * migrate_max_downtime() / 1000000;
1762 s->mbps = (((double) transferred_bytes * 8.0) /
1763 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
1765 trace_migrate_transferred(transferred_bytes, time_spent,
1766 bandwidth, max_size);
1767 /* if we haven't sent anything, we don't want to recalculate
1768 10000 is a small enough number for our purposes */
1769 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
1770 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
1773 qemu_file_reset_rate_limit(s->to_dst_file);
1774 initial_time = current_time;
1775 initial_bytes = qemu_ftell(s->to_dst_file);
1777 if (qemu_file_rate_limit(s->to_dst_file)) {
1778 /* usleep expects microseconds */
1779 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
1783 trace_migration_thread_after_loop();
1784 /* If we enabled cpu throttling for auto-converge, turn it off. */
1785 cpu_throttle_stop();
1786 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1788 qemu_mutex_lock_iothread();
1789 qemu_savevm_state_cleanup();
1790 if (s->state == MIGRATION_STATUS_COMPLETED) {
1791 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
1792 s->total_time = end_time - s->total_time;
1793 if (!entered_postcopy) {
1794 s->downtime = end_time - start_time;
1796 if (s->total_time) {
1797 s->mbps = (((double) transferred_bytes * 8.0) /
1798 ((double) s->total_time)) / 1000;
1800 runstate_set(RUN_STATE_POSTMIGRATE);
1801 } else {
1802 if (old_vm_running && !entered_postcopy) {
1803 vm_start();
1806 qemu_bh_schedule(s->cleanup_bh);
1807 qemu_mutex_unlock_iothread();
1809 rcu_unregister_thread();
1810 return NULL;
1813 void migrate_fd_connect(MigrationState *s)
1815 /* This is a best 1st approximation. ns to ms */
1816 s->expected_downtime = max_downtime/1000000;
1817 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
1819 qemu_file_set_blocking(s->to_dst_file, true);
1820 qemu_file_set_rate_limit(s->to_dst_file,
1821 s->bandwidth_limit / XFER_LIMIT_RATIO);
1823 /* Notify before starting migration thread */
1824 notifier_list_notify(&migration_state_notifiers, s);
1827 * Open the return path; currently for postcopy but other things might
1828 * also want it.
1830 if (migrate_postcopy_ram()) {
1831 if (open_return_path_on_source(s)) {
1832 error_report("Unable to open return-path for postcopy");
1833 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1834 MIGRATION_STATUS_FAILED);
1835 migrate_fd_cleanup(s);
1836 return;
1840 migrate_compress_threads_create();
1841 qemu_thread_create(&s->thread, "migration", migration_thread, s,
1842 QEMU_THREAD_JOINABLE);
1843 s->migration_thread_running = true;
1846 PostcopyState postcopy_state_get(void)
1848 return atomic_mb_read(&incoming_postcopy_state);
1851 /* Set the state and return the old state */
1852 PostcopyState postcopy_state_set(PostcopyState new_state)
1854 return atomic_xchg(&incoming_postcopy_state, new_state);