migration: new message MIG_RP_MSG_RESUME_ACK
[qemu/ar7.git] / migration / migration.c
blob4f2c6d22d14b97e7d8e707823ab10632b543b311
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 "migration/blocker.h"
20 #include "exec.h"
21 #include "fd.h"
22 #include "socket.h"
23 #include "rdma.h"
24 #include "ram.h"
25 #include "migration/global_state.h"
26 #include "migration/misc.h"
27 #include "migration.h"
28 #include "savevm.h"
29 #include "qemu-file-channel.h"
30 #include "qemu-file.h"
31 #include "migration/vmstate.h"
32 #include "block/block.h"
33 #include "qapi/error.h"
34 #include "qapi/qapi-commands-migration.h"
35 #include "qapi/qapi-events-migration.h"
36 #include "qapi/qmp/qerror.h"
37 #include "qapi/qmp/qnull.h"
38 #include "qemu/rcu.h"
39 #include "block.h"
40 #include "postcopy-ram.h"
41 #include "qemu/thread.h"
42 #include "trace.h"
43 #include "exec/target_page.h"
44 #include "io/channel-buffer.h"
45 #include "migration/colo.h"
46 #include "hw/boards.h"
47 #include "monitor/monitor.h"
49 #define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
51 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
52 * data. */
53 #define BUFFER_DELAY 100
54 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
56 /* Time in milliseconds we are allowed to stop the source,
57 * for sending the last part */
58 #define DEFAULT_MIGRATE_SET_DOWNTIME 300
60 /* Maximum migrate downtime set to 2000 seconds */
61 #define MAX_MIGRATE_DOWNTIME_SECONDS 2000
62 #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
64 /* Default compression thread count */
65 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
66 /* Default decompression thread count, usually decompression is at
67 * least 4 times as fast as compression.*/
68 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
69 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
70 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
71 /* Define default autoconverge cpu throttle migration parameters */
72 #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
73 #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
75 /* Migration XBZRLE default cache size */
76 #define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
78 /* The delay time (in ms) between two COLO checkpoints
79 * Note: Please change this default value to 10000 when we support hybrid mode.
81 #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
82 #define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
83 #define DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT 16
85 static NotifierList migration_state_notifiers =
86 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
88 static bool deferred_incoming;
90 /* Messages sent on the return path from destination to source */
91 enum mig_rp_message_type {
92 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
93 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
94 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
96 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
97 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
98 MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */
99 MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */
101 MIG_RP_MSG_MAX
104 /* When we add fault tolerance, we could have several
105 migrations at once. For now we don't need to add
106 dynamic creation of migration */
108 static MigrationState *current_migration;
110 static bool migration_object_check(MigrationState *ms, Error **errp);
111 static int migration_maybe_pause(MigrationState *s,
112 int *current_active_state,
113 int new_state);
115 void migration_object_init(void)
117 MachineState *ms = MACHINE(qdev_get_machine());
118 Error *err = NULL;
120 /* This can only be called once. */
121 assert(!current_migration);
122 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
124 if (!migration_object_check(current_migration, &err)) {
125 error_report_err(err);
126 exit(1);
130 * We cannot really do this in migration_instance_init() since at
131 * that time global properties are not yet applied, then this
132 * value will be definitely replaced by something else.
134 if (ms->enforce_config_section) {
135 current_migration->send_configuration = true;
139 void migration_object_finalize(void)
141 object_unref(OBJECT(current_migration));
144 /* For outgoing */
145 MigrationState *migrate_get_current(void)
147 /* This can only be called after the object created. */
148 assert(current_migration);
149 return current_migration;
152 MigrationIncomingState *migration_incoming_get_current(void)
154 static bool once;
155 static MigrationIncomingState mis_current;
157 if (!once) {
158 mis_current.state = MIGRATION_STATUS_NONE;
159 memset(&mis_current, 0, sizeof(MigrationIncomingState));
160 mis_current.postcopy_remote_fds = g_array_new(FALSE, TRUE,
161 sizeof(struct PostCopyFD));
162 qemu_mutex_init(&mis_current.rp_mutex);
163 qemu_event_init(&mis_current.main_thread_load_event, false);
164 qemu_sem_init(&mis_current.postcopy_pause_sem_dst, 0);
165 qemu_sem_init(&mis_current.postcopy_pause_sem_fault, 0);
167 init_dirty_bitmap_incoming_migration();
169 once = true;
171 return &mis_current;
174 void migration_incoming_state_destroy(void)
176 struct MigrationIncomingState *mis = migration_incoming_get_current();
178 if (mis->to_src_file) {
179 /* Tell source that we are done */
180 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
181 qemu_fclose(mis->to_src_file);
182 mis->to_src_file = NULL;
185 if (mis->from_src_file) {
186 qemu_fclose(mis->from_src_file);
187 mis->from_src_file = NULL;
189 if (mis->postcopy_remote_fds) {
190 g_array_free(mis->postcopy_remote_fds, TRUE);
191 mis->postcopy_remote_fds = NULL;
194 qemu_event_reset(&mis->main_thread_load_event);
197 static void migrate_generate_event(int new_state)
199 if (migrate_use_events()) {
200 qapi_event_send_migration(new_state, &error_abort);
205 * Called on -incoming with a defer: uri.
206 * The migration can be started later after any parameters have been
207 * changed.
209 static void deferred_incoming_migration(Error **errp)
211 if (deferred_incoming) {
212 error_setg(errp, "Incoming migration already deferred");
214 deferred_incoming = true;
218 * Send a message on the return channel back to the source
219 * of the migration.
221 static int migrate_send_rp_message(MigrationIncomingState *mis,
222 enum mig_rp_message_type message_type,
223 uint16_t len, void *data)
225 int ret = 0;
227 trace_migrate_send_rp_message((int)message_type, len);
228 qemu_mutex_lock(&mis->rp_mutex);
231 * It's possible that the file handle got lost due to network
232 * failures.
234 if (!mis->to_src_file) {
235 ret = -EIO;
236 goto error;
239 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
240 qemu_put_be16(mis->to_src_file, len);
241 qemu_put_buffer(mis->to_src_file, data, len);
242 qemu_fflush(mis->to_src_file);
244 /* It's possible that qemu file got error during sending */
245 ret = qemu_file_get_error(mis->to_src_file);
247 error:
248 qemu_mutex_unlock(&mis->rp_mutex);
249 return ret;
252 /* Request a range of pages from the source VM at the given
253 * start address.
254 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
255 * as the last request (a name must have been given previously)
256 * Start: Address offset within the RB
257 * Len: Length in bytes required - must be a multiple of pagesize
259 int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
260 ram_addr_t start, size_t len)
262 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
263 size_t msglen = 12; /* start + len */
264 enum mig_rp_message_type msg_type;
266 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
267 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
269 if (rbname) {
270 int rbname_len = strlen(rbname);
271 assert(rbname_len < 256);
273 bufc[msglen++] = rbname_len;
274 memcpy(bufc + msglen, rbname, rbname_len);
275 msglen += rbname_len;
276 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
277 } else {
278 msg_type = MIG_RP_MSG_REQ_PAGES;
281 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
284 void qemu_start_incoming_migration(const char *uri, Error **errp)
286 const char *p;
288 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
289 if (!strcmp(uri, "defer")) {
290 deferred_incoming_migration(errp);
291 } else if (strstart(uri, "tcp:", &p)) {
292 tcp_start_incoming_migration(p, errp);
293 #ifdef CONFIG_RDMA
294 } else if (strstart(uri, "rdma:", &p)) {
295 rdma_start_incoming_migration(p, errp);
296 #endif
297 } else if (strstart(uri, "exec:", &p)) {
298 exec_start_incoming_migration(p, errp);
299 } else if (strstart(uri, "unix:", &p)) {
300 unix_start_incoming_migration(p, errp);
301 } else if (strstart(uri, "fd:", &p)) {
302 fd_start_incoming_migration(p, errp);
303 } else {
304 error_setg(errp, "unknown migration protocol: %s", uri);
308 static void process_incoming_migration_bh(void *opaque)
310 Error *local_err = NULL;
311 MigrationIncomingState *mis = opaque;
313 /* Make sure all file formats flush their mutable metadata.
314 * If we get an error here, just don't restart the VM yet. */
315 bdrv_invalidate_cache_all(&local_err);
316 if (local_err) {
317 error_report_err(local_err);
318 local_err = NULL;
319 autostart = false;
323 * This must happen after all error conditions are dealt with and
324 * we're sure the VM is going to be running on this host.
326 qemu_announce_self();
328 if (multifd_load_cleanup(&local_err) != 0) {
329 error_report_err(local_err);
330 autostart = false;
332 /* If global state section was not received or we are in running
333 state, we need to obey autostart. Any other state is set with
334 runstate_set. */
336 dirty_bitmap_mig_before_vm_start();
338 if (!global_state_received() ||
339 global_state_get_runstate() == RUN_STATE_RUNNING) {
340 if (autostart) {
341 vm_start();
342 } else {
343 runstate_set(RUN_STATE_PAUSED);
345 } else {
346 runstate_set(global_state_get_runstate());
349 * This must happen after any state changes since as soon as an external
350 * observer sees this event they might start to prod at the VM assuming
351 * it's ready to use.
353 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
354 MIGRATION_STATUS_COMPLETED);
355 qemu_bh_delete(mis->bh);
356 migration_incoming_state_destroy();
359 static void process_incoming_migration_co(void *opaque)
361 MigrationIncomingState *mis = migration_incoming_get_current();
362 PostcopyState ps;
363 int ret;
365 assert(mis->from_src_file);
366 mis->largest_page_size = qemu_ram_pagesize_largest();
367 postcopy_state_set(POSTCOPY_INCOMING_NONE);
368 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
369 MIGRATION_STATUS_ACTIVE);
370 ret = qemu_loadvm_state(mis->from_src_file);
372 ps = postcopy_state_get();
373 trace_process_incoming_migration_co_end(ret, ps);
374 if (ps != POSTCOPY_INCOMING_NONE) {
375 if (ps == POSTCOPY_INCOMING_ADVISE) {
377 * Where a migration had postcopy enabled (and thus went to advise)
378 * but managed to complete within the precopy period, we can use
379 * the normal exit.
381 postcopy_ram_incoming_cleanup(mis);
382 } else if (ret >= 0) {
384 * Postcopy was started, cleanup should happen at the end of the
385 * postcopy thread.
387 trace_process_incoming_migration_co_postcopy_end_main();
388 return;
390 /* Else if something went wrong then just fall out of the normal exit */
393 /* we get COLO info, and know if we are in COLO mode */
394 if (!ret && migration_incoming_enable_colo()) {
395 mis->migration_incoming_co = qemu_coroutine_self();
396 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
397 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
398 mis->have_colo_incoming_thread = true;
399 qemu_coroutine_yield();
401 /* Wait checkpoint incoming thread exit before free resource */
402 qemu_thread_join(&mis->colo_incoming_thread);
405 if (ret < 0) {
406 Error *local_err = NULL;
408 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
409 MIGRATION_STATUS_FAILED);
410 error_report("load of migration failed: %s", strerror(-ret));
411 qemu_fclose(mis->from_src_file);
412 if (multifd_load_cleanup(&local_err) != 0) {
413 error_report_err(local_err);
415 exit(EXIT_FAILURE);
417 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
418 qemu_bh_schedule(mis->bh);
421 static void migration_incoming_setup(QEMUFile *f)
423 MigrationIncomingState *mis = migration_incoming_get_current();
425 if (multifd_load_setup() != 0) {
426 /* We haven't been able to create multifd threads
427 nothing better to do */
428 exit(EXIT_FAILURE);
431 if (!mis->from_src_file) {
432 mis->from_src_file = f;
434 qemu_file_set_blocking(f, false);
437 void migration_incoming_process(void)
439 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
440 qemu_coroutine_enter(co);
443 void migration_fd_process_incoming(QEMUFile *f)
445 MigrationIncomingState *mis = migration_incoming_get_current();
447 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
448 /* Resumed from a paused postcopy migration */
450 mis->from_src_file = f;
451 /* Postcopy has standalone thread to do vm load */
452 qemu_file_set_blocking(f, true);
454 /* Re-configure the return path */
455 mis->to_src_file = qemu_file_get_return_path(f);
457 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
458 MIGRATION_STATUS_POSTCOPY_RECOVER);
461 * Here, we only wake up the main loading thread (while the
462 * fault thread will still be waiting), so that we can receive
463 * commands from source now, and answer it if needed. The
464 * fault thread will be woken up afterwards until we are sure
465 * that source is ready to reply to page requests.
467 qemu_sem_post(&mis->postcopy_pause_sem_dst);
468 } else {
469 /* New incoming migration */
470 migration_incoming_setup(f);
471 migration_incoming_process();
475 void migration_ioc_process_incoming(QIOChannel *ioc)
477 MigrationIncomingState *mis = migration_incoming_get_current();
479 if (!mis->from_src_file) {
480 QEMUFile *f = qemu_fopen_channel_input(ioc);
481 migration_incoming_setup(f);
482 return;
484 multifd_recv_new_channel(ioc);
488 * @migration_has_all_channels: We have received all channels that we need
490 * Returns true when we have got connections to all the channels that
491 * we need for migration.
493 bool migration_has_all_channels(void)
495 bool all_channels;
497 all_channels = multifd_recv_all_channels_created();
499 return all_channels;
503 * Send a 'SHUT' message on the return channel with the given value
504 * to indicate that we've finished with the RP. Non-0 value indicates
505 * error.
507 void migrate_send_rp_shut(MigrationIncomingState *mis,
508 uint32_t value)
510 uint32_t buf;
512 buf = cpu_to_be32(value);
513 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
517 * Send a 'PONG' message on the return channel with the given value
518 * (normally in response to a 'PING')
520 void migrate_send_rp_pong(MigrationIncomingState *mis,
521 uint32_t value)
523 uint32_t buf;
525 buf = cpu_to_be32(value);
526 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
529 void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
530 char *block_name)
532 char buf[512];
533 int len;
534 int64_t res;
537 * First, we send the header part. It contains only the len of
538 * idstr, and the idstr itself.
540 len = strlen(block_name);
541 buf[0] = len;
542 memcpy(buf + 1, block_name, len);
544 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
545 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
546 __func__);
547 return;
550 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
553 * Next, we dump the received bitmap to the stream.
555 * TODO: currently we are safe since we are the only one that is
556 * using the to_src_file handle (fault thread is still paused),
557 * and it's ok even not taking the mutex. However the best way is
558 * to take the lock before sending the message header, and release
559 * the lock after sending the bitmap.
561 qemu_mutex_lock(&mis->rp_mutex);
562 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
563 qemu_mutex_unlock(&mis->rp_mutex);
565 trace_migrate_send_rp_recv_bitmap(block_name, res);
568 void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
570 uint32_t buf;
572 buf = cpu_to_be32(value);
573 migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
576 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
578 MigrationCapabilityStatusList *head = NULL;
579 MigrationCapabilityStatusList *caps;
580 MigrationState *s = migrate_get_current();
581 int i;
583 caps = NULL; /* silence compiler warning */
584 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
585 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
586 if (i == MIGRATION_CAPABILITY_BLOCK) {
587 continue;
589 #endif
590 if (head == NULL) {
591 head = g_malloc0(sizeof(*caps));
592 caps = head;
593 } else {
594 caps->next = g_malloc0(sizeof(*caps));
595 caps = caps->next;
597 caps->value =
598 g_malloc(sizeof(*caps->value));
599 caps->value->capability = i;
600 caps->value->state = s->enabled_capabilities[i];
603 return head;
606 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
608 MigrationParameters *params;
609 MigrationState *s = migrate_get_current();
611 /* TODO use QAPI_CLONE() instead of duplicating it inline */
612 params = g_malloc0(sizeof(*params));
613 params->has_compress_level = true;
614 params->compress_level = s->parameters.compress_level;
615 params->has_compress_threads = true;
616 params->compress_threads = s->parameters.compress_threads;
617 params->has_decompress_threads = true;
618 params->decompress_threads = s->parameters.decompress_threads;
619 params->has_cpu_throttle_initial = true;
620 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
621 params->has_cpu_throttle_increment = true;
622 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
623 params->has_tls_creds = true;
624 params->tls_creds = g_strdup(s->parameters.tls_creds);
625 params->has_tls_hostname = true;
626 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
627 params->has_max_bandwidth = true;
628 params->max_bandwidth = s->parameters.max_bandwidth;
629 params->has_downtime_limit = true;
630 params->downtime_limit = s->parameters.downtime_limit;
631 params->has_x_checkpoint_delay = true;
632 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
633 params->has_block_incremental = true;
634 params->block_incremental = s->parameters.block_incremental;
635 params->has_x_multifd_channels = true;
636 params->x_multifd_channels = s->parameters.x_multifd_channels;
637 params->has_x_multifd_page_count = true;
638 params->x_multifd_page_count = s->parameters.x_multifd_page_count;
639 params->has_xbzrle_cache_size = true;
640 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
642 return params;
646 * Return true if we're already in the middle of a migration
647 * (i.e. any of the active or setup states)
649 static bool migration_is_setup_or_active(int state)
651 switch (state) {
652 case MIGRATION_STATUS_ACTIVE:
653 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
654 case MIGRATION_STATUS_POSTCOPY_PAUSED:
655 case MIGRATION_STATUS_POSTCOPY_RECOVER:
656 case MIGRATION_STATUS_SETUP:
657 case MIGRATION_STATUS_PRE_SWITCHOVER:
658 case MIGRATION_STATUS_DEVICE:
659 return true;
661 default:
662 return false;
667 static void populate_ram_info(MigrationInfo *info, MigrationState *s)
669 info->has_ram = true;
670 info->ram = g_malloc0(sizeof(*info->ram));
671 info->ram->transferred = ram_counters.transferred;
672 info->ram->total = ram_bytes_total();
673 info->ram->duplicate = ram_counters.duplicate;
674 /* legacy value. It is not used anymore */
675 info->ram->skipped = 0;
676 info->ram->normal = ram_counters.normal;
677 info->ram->normal_bytes = ram_counters.normal *
678 qemu_target_page_size();
679 info->ram->mbps = s->mbps;
680 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
681 info->ram->postcopy_requests = ram_counters.postcopy_requests;
682 info->ram->page_size = qemu_target_page_size();
684 if (migrate_use_xbzrle()) {
685 info->has_xbzrle_cache = true;
686 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
687 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
688 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
689 info->xbzrle_cache->pages = xbzrle_counters.pages;
690 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
691 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
692 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
695 if (cpu_throttle_active()) {
696 info->has_cpu_throttle_percentage = true;
697 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
700 if (s->state != MIGRATION_STATUS_COMPLETED) {
701 info->ram->remaining = ram_bytes_remaining();
702 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
706 static void populate_disk_info(MigrationInfo *info)
708 if (blk_mig_active()) {
709 info->has_disk = true;
710 info->disk = g_malloc0(sizeof(*info->disk));
711 info->disk->transferred = blk_mig_bytes_transferred();
712 info->disk->remaining = blk_mig_bytes_remaining();
713 info->disk->total = blk_mig_bytes_total();
717 static void fill_source_migration_info(MigrationInfo *info)
719 MigrationState *s = migrate_get_current();
721 switch (s->state) {
722 case MIGRATION_STATUS_NONE:
723 /* no migration has happened ever */
724 /* do not overwrite destination migration status */
725 return;
726 break;
727 case MIGRATION_STATUS_SETUP:
728 info->has_status = true;
729 info->has_total_time = false;
730 break;
731 case MIGRATION_STATUS_ACTIVE:
732 case MIGRATION_STATUS_CANCELLING:
733 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
734 case MIGRATION_STATUS_PRE_SWITCHOVER:
735 case MIGRATION_STATUS_DEVICE:
736 case MIGRATION_STATUS_POSTCOPY_PAUSED:
737 case MIGRATION_STATUS_POSTCOPY_RECOVER:
738 /* TODO add some postcopy stats */
739 info->has_status = true;
740 info->has_total_time = true;
741 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
742 - s->start_time;
743 info->has_expected_downtime = true;
744 info->expected_downtime = s->expected_downtime;
745 info->has_setup_time = true;
746 info->setup_time = s->setup_time;
748 populate_ram_info(info, s);
749 populate_disk_info(info);
750 break;
751 case MIGRATION_STATUS_COLO:
752 info->has_status = true;
753 /* TODO: display COLO specific information (checkpoint info etc.) */
754 break;
755 case MIGRATION_STATUS_COMPLETED:
756 info->has_status = true;
757 info->has_total_time = true;
758 info->total_time = s->total_time;
759 info->has_downtime = true;
760 info->downtime = s->downtime;
761 info->has_setup_time = true;
762 info->setup_time = s->setup_time;
764 populate_ram_info(info, s);
765 break;
766 case MIGRATION_STATUS_FAILED:
767 info->has_status = true;
768 if (s->error) {
769 info->has_error_desc = true;
770 info->error_desc = g_strdup(error_get_pretty(s->error));
772 break;
773 case MIGRATION_STATUS_CANCELLED:
774 info->has_status = true;
775 break;
777 info->status = s->state;
781 * @migration_caps_check - check capability validity
783 * @cap_list: old capability list, array of bool
784 * @params: new capabilities to be applied soon
785 * @errp: set *errp if the check failed, with reason
787 * Returns true if check passed, otherwise false.
789 static bool migrate_caps_check(bool *cap_list,
790 MigrationCapabilityStatusList *params,
791 Error **errp)
793 MigrationCapabilityStatusList *cap;
794 bool old_postcopy_cap;
795 MigrationIncomingState *mis = migration_incoming_get_current();
797 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
799 for (cap = params; cap; cap = cap->next) {
800 cap_list[cap->value->capability] = cap->value->state;
803 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
804 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
805 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
806 "block migration");
807 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
808 return false;
810 #endif
812 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
813 if (cap_list[MIGRATION_CAPABILITY_COMPRESS]) {
814 /* The decompression threads asynchronously write into RAM
815 * rather than use the atomic copies needed to avoid
816 * userfaulting. It should be possible to fix the decompression
817 * threads for compatibility in future.
819 error_setg(errp, "Postcopy is not currently compatible "
820 "with compression");
821 return false;
824 /* This check is reasonably expensive, so only when it's being
825 * set the first time, also it's only the destination that needs
826 * special support.
828 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
829 !postcopy_ram_supported_by_host(mis)) {
830 /* postcopy_ram_supported_by_host will have emitted a more
831 * detailed message
833 error_setg(errp, "Postcopy is not supported");
834 return false;
838 return true;
841 static void fill_destination_migration_info(MigrationInfo *info)
843 MigrationIncomingState *mis = migration_incoming_get_current();
845 switch (mis->state) {
846 case MIGRATION_STATUS_NONE:
847 return;
848 break;
849 case MIGRATION_STATUS_SETUP:
850 case MIGRATION_STATUS_CANCELLING:
851 case MIGRATION_STATUS_CANCELLED:
852 case MIGRATION_STATUS_ACTIVE:
853 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
854 case MIGRATION_STATUS_FAILED:
855 case MIGRATION_STATUS_COLO:
856 info->has_status = true;
857 break;
858 case MIGRATION_STATUS_COMPLETED:
859 info->has_status = true;
860 fill_destination_postcopy_migration_info(info);
861 break;
863 info->status = mis->state;
866 MigrationInfo *qmp_query_migrate(Error **errp)
868 MigrationInfo *info = g_malloc0(sizeof(*info));
870 fill_destination_migration_info(info);
871 fill_source_migration_info(info);
873 return info;
876 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
877 Error **errp)
879 MigrationState *s = migrate_get_current();
880 MigrationCapabilityStatusList *cap;
881 bool cap_list[MIGRATION_CAPABILITY__MAX];
883 if (migration_is_setup_or_active(s->state)) {
884 error_setg(errp, QERR_MIGRATION_ACTIVE);
885 return;
888 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
889 if (!migrate_caps_check(cap_list, params, errp)) {
890 return;
893 for (cap = params; cap; cap = cap->next) {
894 s->enabled_capabilities[cap->value->capability] = cap->value->state;
899 * Check whether the parameters are valid. Error will be put into errp
900 * (if provided). Return true if valid, otherwise false.
902 static bool migrate_params_check(MigrationParameters *params, Error **errp)
904 if (params->has_compress_level &&
905 (params->compress_level > 9)) {
906 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
907 "is invalid, it should be in the range of 0 to 9");
908 return false;
911 if (params->has_compress_threads && (params->compress_threads < 1)) {
912 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
913 "compress_threads",
914 "is invalid, it should be in the range of 1 to 255");
915 return false;
918 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
919 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
920 "decompress_threads",
921 "is invalid, it should be in the range of 1 to 255");
922 return false;
925 if (params->has_cpu_throttle_initial &&
926 (params->cpu_throttle_initial < 1 ||
927 params->cpu_throttle_initial > 99)) {
928 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
929 "cpu_throttle_initial",
930 "an integer in the range of 1 to 99");
931 return false;
934 if (params->has_cpu_throttle_increment &&
935 (params->cpu_throttle_increment < 1 ||
936 params->cpu_throttle_increment > 99)) {
937 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
938 "cpu_throttle_increment",
939 "an integer in the range of 1 to 99");
940 return false;
943 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
944 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
945 " range of 0 to %zu bytes/second", SIZE_MAX);
946 return false;
949 if (params->has_downtime_limit &&
950 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
951 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
952 "the range of 0 to %d milliseconds",
953 MAX_MIGRATE_DOWNTIME);
954 return false;
957 /* x_checkpoint_delay is now always positive */
959 if (params->has_x_multifd_channels && (params->x_multifd_channels < 1)) {
960 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
961 "multifd_channels",
962 "is invalid, it should be in the range of 1 to 255");
963 return false;
965 if (params->has_x_multifd_page_count &&
966 (params->x_multifd_page_count < 1 ||
967 params->x_multifd_page_count > 10000)) {
968 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
969 "multifd_page_count",
970 "is invalid, it should be in the range of 1 to 10000");
971 return false;
974 if (params->has_xbzrle_cache_size &&
975 (params->xbzrle_cache_size < qemu_target_page_size() ||
976 !is_power_of_2(params->xbzrle_cache_size))) {
977 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
978 "xbzrle_cache_size",
979 "is invalid, it should be bigger than target page size"
980 " and a power of two");
981 return false;
984 return true;
987 static void migrate_params_test_apply(MigrateSetParameters *params,
988 MigrationParameters *dest)
990 *dest = migrate_get_current()->parameters;
992 /* TODO use QAPI_CLONE() instead of duplicating it inline */
994 if (params->has_compress_level) {
995 dest->compress_level = params->compress_level;
998 if (params->has_compress_threads) {
999 dest->compress_threads = params->compress_threads;
1002 if (params->has_decompress_threads) {
1003 dest->decompress_threads = params->decompress_threads;
1006 if (params->has_cpu_throttle_initial) {
1007 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1010 if (params->has_cpu_throttle_increment) {
1011 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1014 if (params->has_tls_creds) {
1015 assert(params->tls_creds->type == QTYPE_QSTRING);
1016 dest->tls_creds = g_strdup(params->tls_creds->u.s);
1019 if (params->has_tls_hostname) {
1020 assert(params->tls_hostname->type == QTYPE_QSTRING);
1021 dest->tls_hostname = g_strdup(params->tls_hostname->u.s);
1024 if (params->has_max_bandwidth) {
1025 dest->max_bandwidth = params->max_bandwidth;
1028 if (params->has_downtime_limit) {
1029 dest->downtime_limit = params->downtime_limit;
1032 if (params->has_x_checkpoint_delay) {
1033 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1036 if (params->has_block_incremental) {
1037 dest->block_incremental = params->block_incremental;
1039 if (params->has_x_multifd_channels) {
1040 dest->x_multifd_channels = params->x_multifd_channels;
1042 if (params->has_x_multifd_page_count) {
1043 dest->x_multifd_page_count = params->x_multifd_page_count;
1045 if (params->has_xbzrle_cache_size) {
1046 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1050 static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
1052 MigrationState *s = migrate_get_current();
1054 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1056 if (params->has_compress_level) {
1057 s->parameters.compress_level = params->compress_level;
1060 if (params->has_compress_threads) {
1061 s->parameters.compress_threads = params->compress_threads;
1064 if (params->has_decompress_threads) {
1065 s->parameters.decompress_threads = params->decompress_threads;
1068 if (params->has_cpu_throttle_initial) {
1069 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
1072 if (params->has_cpu_throttle_increment) {
1073 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
1076 if (params->has_tls_creds) {
1077 g_free(s->parameters.tls_creds);
1078 assert(params->tls_creds->type == QTYPE_QSTRING);
1079 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
1082 if (params->has_tls_hostname) {
1083 g_free(s->parameters.tls_hostname);
1084 assert(params->tls_hostname->type == QTYPE_QSTRING);
1085 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
1088 if (params->has_max_bandwidth) {
1089 s->parameters.max_bandwidth = params->max_bandwidth;
1090 if (s->to_dst_file) {
1091 qemu_file_set_rate_limit(s->to_dst_file,
1092 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1096 if (params->has_downtime_limit) {
1097 s->parameters.downtime_limit = params->downtime_limit;
1100 if (params->has_x_checkpoint_delay) {
1101 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
1102 if (migration_in_colo_state()) {
1103 colo_checkpoint_notify(s);
1107 if (params->has_block_incremental) {
1108 s->parameters.block_incremental = params->block_incremental;
1110 if (params->has_x_multifd_channels) {
1111 s->parameters.x_multifd_channels = params->x_multifd_channels;
1113 if (params->has_x_multifd_page_count) {
1114 s->parameters.x_multifd_page_count = params->x_multifd_page_count;
1116 if (params->has_xbzrle_cache_size) {
1117 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1118 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1122 void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
1124 MigrationParameters tmp;
1126 /* TODO Rewrite "" to null instead */
1127 if (params->has_tls_creds
1128 && params->tls_creds->type == QTYPE_QNULL) {
1129 qobject_unref(params->tls_creds->u.n);
1130 params->tls_creds->type = QTYPE_QSTRING;
1131 params->tls_creds->u.s = strdup("");
1133 /* TODO Rewrite "" to null instead */
1134 if (params->has_tls_hostname
1135 && params->tls_hostname->type == QTYPE_QNULL) {
1136 qobject_unref(params->tls_hostname->u.n);
1137 params->tls_hostname->type = QTYPE_QSTRING;
1138 params->tls_hostname->u.s = strdup("");
1141 migrate_params_test_apply(params, &tmp);
1143 if (!migrate_params_check(&tmp, errp)) {
1144 /* Invalid parameter */
1145 return;
1148 migrate_params_apply(params, errp);
1152 void qmp_migrate_start_postcopy(Error **errp)
1154 MigrationState *s = migrate_get_current();
1156 if (!migrate_postcopy()) {
1157 error_setg(errp, "Enable postcopy with migrate_set_capability before"
1158 " the start of migration");
1159 return;
1162 if (s->state == MIGRATION_STATUS_NONE) {
1163 error_setg(errp, "Postcopy must be started after migration has been"
1164 " started");
1165 return;
1168 * we don't error if migration has finished since that would be racy
1169 * with issuing this command.
1171 atomic_set(&s->start_postcopy, true);
1174 /* shared migration helpers */
1176 void migrate_set_state(int *state, int old_state, int new_state)
1178 assert(new_state < MIGRATION_STATUS__MAX);
1179 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
1180 trace_migrate_set_state(MigrationStatus_str(new_state));
1181 migrate_generate_event(new_state);
1185 static MigrationCapabilityStatusList *migrate_cap_add(
1186 MigrationCapabilityStatusList *list,
1187 MigrationCapability index,
1188 bool state)
1190 MigrationCapabilityStatusList *cap;
1192 cap = g_new0(MigrationCapabilityStatusList, 1);
1193 cap->value = g_new0(MigrationCapabilityStatus, 1);
1194 cap->value->capability = index;
1195 cap->value->state = state;
1196 cap->next = list;
1198 return cap;
1201 void migrate_set_block_enabled(bool value, Error **errp)
1203 MigrationCapabilityStatusList *cap;
1205 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
1206 qmp_migrate_set_capabilities(cap, errp);
1207 qapi_free_MigrationCapabilityStatusList(cap);
1210 static void migrate_set_block_incremental(MigrationState *s, bool value)
1212 s->parameters.block_incremental = value;
1215 static void block_cleanup_parameters(MigrationState *s)
1217 if (s->must_remove_block_options) {
1218 /* setting to false can never fail */
1219 migrate_set_block_enabled(false, &error_abort);
1220 migrate_set_block_incremental(s, false);
1221 s->must_remove_block_options = false;
1225 static void migrate_fd_cleanup(void *opaque)
1227 MigrationState *s = opaque;
1229 qemu_bh_delete(s->cleanup_bh);
1230 s->cleanup_bh = NULL;
1232 qemu_savevm_state_cleanup();
1234 if (s->to_dst_file) {
1235 Error *local_err = NULL;
1237 trace_migrate_fd_cleanup();
1238 qemu_mutex_unlock_iothread();
1239 if (s->migration_thread_running) {
1240 qemu_thread_join(&s->thread);
1241 s->migration_thread_running = false;
1243 qemu_mutex_lock_iothread();
1245 if (multifd_save_cleanup(&local_err) != 0) {
1246 error_report_err(local_err);
1248 qemu_fclose(s->to_dst_file);
1249 s->to_dst_file = NULL;
1252 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
1253 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
1255 if (s->state == MIGRATION_STATUS_CANCELLING) {
1256 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
1257 MIGRATION_STATUS_CANCELLED);
1260 if (s->error) {
1261 /* It is used on info migrate. We can't free it */
1262 error_report_err(error_copy(s->error));
1264 notifier_list_notify(&migration_state_notifiers, s);
1265 block_cleanup_parameters(s);
1268 void migrate_set_error(MigrationState *s, const Error *error)
1270 qemu_mutex_lock(&s->error_mutex);
1271 if (!s->error) {
1272 s->error = error_copy(error);
1274 qemu_mutex_unlock(&s->error_mutex);
1277 void migrate_fd_error(MigrationState *s, const Error *error)
1279 trace_migrate_fd_error(error_get_pretty(error));
1280 assert(s->to_dst_file == NULL);
1281 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1282 MIGRATION_STATUS_FAILED);
1283 migrate_set_error(s, error);
1286 static void migrate_fd_cancel(MigrationState *s)
1288 int old_state ;
1289 QEMUFile *f = migrate_get_current()->to_dst_file;
1290 trace_migrate_fd_cancel();
1292 if (s->rp_state.from_dst_file) {
1293 /* shutdown the rp socket, so causing the rp thread to shutdown */
1294 qemu_file_shutdown(s->rp_state.from_dst_file);
1297 do {
1298 old_state = s->state;
1299 if (!migration_is_setup_or_active(old_state)) {
1300 break;
1302 /* If the migration is paused, kick it out of the pause */
1303 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1304 qemu_sem_post(&s->pause_sem);
1306 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
1307 } while (s->state != MIGRATION_STATUS_CANCELLING);
1310 * If we're unlucky the migration code might be stuck somewhere in a
1311 * send/write while the network has failed and is waiting to timeout;
1312 * if we've got shutdown(2) available then we can force it to quit.
1313 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1314 * called in a bh, so there is no race against this cancel.
1316 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
1317 qemu_file_shutdown(f);
1319 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1320 Error *local_err = NULL;
1322 bdrv_invalidate_cache_all(&local_err);
1323 if (local_err) {
1324 error_report_err(local_err);
1325 } else {
1326 s->block_inactive = false;
1331 void add_migration_state_change_notifier(Notifier *notify)
1333 notifier_list_add(&migration_state_notifiers, notify);
1336 void remove_migration_state_change_notifier(Notifier *notify)
1338 notifier_remove(notify);
1341 bool migration_in_setup(MigrationState *s)
1343 return s->state == MIGRATION_STATUS_SETUP;
1346 bool migration_has_finished(MigrationState *s)
1348 return s->state == MIGRATION_STATUS_COMPLETED;
1351 bool migration_has_failed(MigrationState *s)
1353 return (s->state == MIGRATION_STATUS_CANCELLED ||
1354 s->state == MIGRATION_STATUS_FAILED);
1357 bool migration_in_postcopy(void)
1359 MigrationState *s = migrate_get_current();
1361 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1364 bool migration_in_postcopy_after_devices(MigrationState *s)
1366 return migration_in_postcopy() && s->postcopy_after_devices;
1369 bool migration_is_idle(void)
1371 MigrationState *s = migrate_get_current();
1373 switch (s->state) {
1374 case MIGRATION_STATUS_NONE:
1375 case MIGRATION_STATUS_CANCELLED:
1376 case MIGRATION_STATUS_COMPLETED:
1377 case MIGRATION_STATUS_FAILED:
1378 return true;
1379 case MIGRATION_STATUS_SETUP:
1380 case MIGRATION_STATUS_CANCELLING:
1381 case MIGRATION_STATUS_ACTIVE:
1382 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1383 case MIGRATION_STATUS_COLO:
1384 case MIGRATION_STATUS_PRE_SWITCHOVER:
1385 case MIGRATION_STATUS_DEVICE:
1386 return false;
1387 case MIGRATION_STATUS__MAX:
1388 g_assert_not_reached();
1391 return false;
1394 void migrate_init(MigrationState *s)
1397 * Reinitialise all migration state, except
1398 * parameters/capabilities that the user set, and
1399 * locks.
1401 s->bytes_xfer = 0;
1402 s->xfer_limit = 0;
1403 s->cleanup_bh = 0;
1404 s->to_dst_file = NULL;
1405 s->state = MIGRATION_STATUS_NONE;
1406 s->rp_state.from_dst_file = NULL;
1407 s->rp_state.error = false;
1408 s->mbps = 0.0;
1409 s->downtime = 0;
1410 s->expected_downtime = 0;
1411 s->setup_time = 0;
1412 s->start_postcopy = false;
1413 s->postcopy_after_devices = false;
1414 s->migration_thread_running = false;
1415 error_free(s->error);
1416 s->error = NULL;
1418 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
1420 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1421 s->total_time = 0;
1422 s->vm_was_running = false;
1423 s->iteration_initial_bytes = 0;
1424 s->threshold_size = 0;
1427 static GSList *migration_blockers;
1429 int migrate_add_blocker(Error *reason, Error **errp)
1431 if (migrate_get_current()->only_migratable) {
1432 error_propagate(errp, error_copy(reason));
1433 error_prepend(errp, "disallowing migration blocker "
1434 "(--only_migratable) for: ");
1435 return -EACCES;
1438 if (migration_is_idle()) {
1439 migration_blockers = g_slist_prepend(migration_blockers, reason);
1440 return 0;
1443 error_propagate(errp, error_copy(reason));
1444 error_prepend(errp, "disallowing migration blocker (migration in "
1445 "progress) for: ");
1446 return -EBUSY;
1449 void migrate_del_blocker(Error *reason)
1451 migration_blockers = g_slist_remove(migration_blockers, reason);
1454 void qmp_migrate_incoming(const char *uri, Error **errp)
1456 Error *local_err = NULL;
1457 static bool once = true;
1459 if (!deferred_incoming) {
1460 error_setg(errp, "For use with '-incoming defer'");
1461 return;
1463 if (!once) {
1464 error_setg(errp, "The incoming migration has already been started");
1467 qemu_start_incoming_migration(uri, &local_err);
1469 if (local_err) {
1470 error_propagate(errp, local_err);
1471 return;
1474 once = false;
1477 bool migration_is_blocked(Error **errp)
1479 if (qemu_savevm_state_blocked(errp)) {
1480 return true;
1483 if (migration_blockers) {
1484 error_propagate(errp, error_copy(migration_blockers->data));
1485 return true;
1488 return false;
1491 /* Returns true if continue to migrate, or false if error detected */
1492 static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
1493 bool resume, Error **errp)
1495 Error *local_err = NULL;
1497 if (resume) {
1498 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1499 error_setg(errp, "Cannot resume if there is no "
1500 "paused migration");
1501 return false;
1503 /* This is a resume, skip init status */
1504 return true;
1507 if (migration_is_setup_or_active(s->state) ||
1508 s->state == MIGRATION_STATUS_CANCELLING ||
1509 s->state == MIGRATION_STATUS_COLO) {
1510 error_setg(errp, QERR_MIGRATION_ACTIVE);
1511 return false;
1514 if (runstate_check(RUN_STATE_INMIGRATE)) {
1515 error_setg(errp, "Guest is waiting for an incoming migration");
1516 return false;
1519 if (migration_is_blocked(errp)) {
1520 return false;
1523 if (blk || blk_inc) {
1524 if (migrate_use_block() || migrate_use_block_incremental()) {
1525 error_setg(errp, "Command options are incompatible with "
1526 "current migration capabilities");
1527 return false;
1529 migrate_set_block_enabled(true, &local_err);
1530 if (local_err) {
1531 error_propagate(errp, local_err);
1532 return false;
1534 s->must_remove_block_options = true;
1537 if (blk_inc) {
1538 migrate_set_block_incremental(s, true);
1541 migrate_init(s);
1543 return true;
1546 void qmp_migrate(const char *uri, bool has_blk, bool blk,
1547 bool has_inc, bool inc, bool has_detach, bool detach,
1548 bool has_resume, bool resume, Error **errp)
1550 Error *local_err = NULL;
1551 MigrationState *s = migrate_get_current();
1552 const char *p;
1554 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
1555 has_resume && resume, errp)) {
1556 /* Error detected, put into errp */
1557 return;
1560 if (strstart(uri, "tcp:", &p)) {
1561 tcp_start_outgoing_migration(s, p, &local_err);
1562 #ifdef CONFIG_RDMA
1563 } else if (strstart(uri, "rdma:", &p)) {
1564 rdma_start_outgoing_migration(s, p, &local_err);
1565 #endif
1566 } else if (strstart(uri, "exec:", &p)) {
1567 exec_start_outgoing_migration(s, p, &local_err);
1568 } else if (strstart(uri, "unix:", &p)) {
1569 unix_start_outgoing_migration(s, p, &local_err);
1570 } else if (strstart(uri, "fd:", &p)) {
1571 fd_start_outgoing_migration(s, p, &local_err);
1572 } else {
1573 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1574 "a valid migration protocol");
1575 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1576 MIGRATION_STATUS_FAILED);
1577 block_cleanup_parameters(s);
1578 return;
1581 if (local_err) {
1582 migrate_fd_error(s, local_err);
1583 error_propagate(errp, local_err);
1584 return;
1588 void qmp_migrate_cancel(Error **errp)
1590 migrate_fd_cancel(migrate_get_current());
1593 void qmp_migrate_continue(MigrationStatus state, Error **errp)
1595 MigrationState *s = migrate_get_current();
1596 if (s->state != state) {
1597 error_setg(errp, "Migration not in expected state: %s",
1598 MigrationStatus_str(s->state));
1599 return;
1601 qemu_sem_post(&s->pause_sem);
1604 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1606 MigrateSetParameters p = {
1607 .has_xbzrle_cache_size = true,
1608 .xbzrle_cache_size = value,
1611 qmp_migrate_set_parameters(&p, errp);
1614 int64_t qmp_query_migrate_cache_size(Error **errp)
1616 return migrate_xbzrle_cache_size();
1619 void qmp_migrate_set_speed(int64_t value, Error **errp)
1621 MigrateSetParameters p = {
1622 .has_max_bandwidth = true,
1623 .max_bandwidth = value,
1626 qmp_migrate_set_parameters(&p, errp);
1629 void qmp_migrate_set_downtime(double value, Error **errp)
1631 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1632 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1633 "the range of 0 to %d seconds",
1634 MAX_MIGRATE_DOWNTIME_SECONDS);
1635 return;
1638 value *= 1000; /* Convert to milliseconds */
1639 value = MAX(0, MIN(INT64_MAX, value));
1641 MigrateSetParameters p = {
1642 .has_downtime_limit = true,
1643 .downtime_limit = value,
1646 qmp_migrate_set_parameters(&p, errp);
1649 bool migrate_release_ram(void)
1651 MigrationState *s;
1653 s = migrate_get_current();
1655 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1658 bool migrate_postcopy_ram(void)
1660 MigrationState *s;
1662 s = migrate_get_current();
1664 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
1667 bool migrate_postcopy(void)
1669 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
1672 bool migrate_auto_converge(void)
1674 MigrationState *s;
1676 s = migrate_get_current();
1678 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1681 bool migrate_zero_blocks(void)
1683 MigrationState *s;
1685 s = migrate_get_current();
1687 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1690 bool migrate_postcopy_blocktime(void)
1692 MigrationState *s;
1694 s = migrate_get_current();
1696 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
1699 bool migrate_use_compression(void)
1701 MigrationState *s;
1703 s = migrate_get_current();
1705 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
1708 int migrate_compress_level(void)
1710 MigrationState *s;
1712 s = migrate_get_current();
1714 return s->parameters.compress_level;
1717 int migrate_compress_threads(void)
1719 MigrationState *s;
1721 s = migrate_get_current();
1723 return s->parameters.compress_threads;
1726 int migrate_decompress_threads(void)
1728 MigrationState *s;
1730 s = migrate_get_current();
1732 return s->parameters.decompress_threads;
1735 bool migrate_dirty_bitmaps(void)
1737 MigrationState *s;
1739 s = migrate_get_current();
1741 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
1744 bool migrate_use_events(void)
1746 MigrationState *s;
1748 s = migrate_get_current();
1750 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1753 bool migrate_use_multifd(void)
1755 MigrationState *s;
1757 s = migrate_get_current();
1759 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_MULTIFD];
1762 bool migrate_pause_before_switchover(void)
1764 MigrationState *s;
1766 s = migrate_get_current();
1768 return s->enabled_capabilities[
1769 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
1772 int migrate_multifd_channels(void)
1774 MigrationState *s;
1776 s = migrate_get_current();
1778 return s->parameters.x_multifd_channels;
1781 int migrate_multifd_page_count(void)
1783 MigrationState *s;
1785 s = migrate_get_current();
1787 return s->parameters.x_multifd_page_count;
1790 int migrate_use_xbzrle(void)
1792 MigrationState *s;
1794 s = migrate_get_current();
1796 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1799 int64_t migrate_xbzrle_cache_size(void)
1801 MigrationState *s;
1803 s = migrate_get_current();
1805 return s->parameters.xbzrle_cache_size;
1808 bool migrate_use_block(void)
1810 MigrationState *s;
1812 s = migrate_get_current();
1814 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1817 bool migrate_use_return_path(void)
1819 MigrationState *s;
1821 s = migrate_get_current();
1823 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
1826 bool migrate_use_block_incremental(void)
1828 MigrationState *s;
1830 s = migrate_get_current();
1832 return s->parameters.block_incremental;
1835 /* migration thread support */
1837 * Something bad happened to the RP stream, mark an error
1838 * The caller shall print or trace something to indicate why
1840 static void mark_source_rp_bad(MigrationState *s)
1842 s->rp_state.error = true;
1845 static struct rp_cmd_args {
1846 ssize_t len; /* -1 = variable */
1847 const char *name;
1848 } rp_cmd_args[] = {
1849 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1850 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1851 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
1852 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1853 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
1854 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
1855 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" },
1856 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1860 * Process a request for pages received on the return path,
1861 * We're allowed to send more than requested (e.g. to round to our page size)
1862 * and we don't need to send pages that have already been sent.
1864 static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1865 ram_addr_t start, size_t len)
1867 long our_host_ps = getpagesize();
1869 trace_migrate_handle_rp_req_pages(rbname, start, len);
1872 * Since we currently insist on matching page sizes, just sanity check
1873 * we're being asked for whole host pages.
1875 if (start & (our_host_ps-1) ||
1876 (len & (our_host_ps-1))) {
1877 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1878 " len: %zd", __func__, start, len);
1879 mark_source_rp_bad(ms);
1880 return;
1883 if (ram_save_queue_pages(rbname, start, len)) {
1884 mark_source_rp_bad(ms);
1888 /* Return true to retry, false to quit */
1889 static bool postcopy_pause_return_path_thread(MigrationState *s)
1891 trace_postcopy_pause_return_path();
1893 qemu_sem_wait(&s->postcopy_pause_rp_sem);
1895 trace_postcopy_pause_return_path_continued();
1897 return true;
1900 static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
1902 RAMBlock *block = qemu_ram_block_by_name(block_name);
1904 if (!block) {
1905 error_report("%s: invalid block name '%s'", __func__, block_name);
1906 return -EINVAL;
1909 /* Fetch the received bitmap and refresh the dirty bitmap */
1910 return ram_dirty_bitmap_reload(s, block);
1913 static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
1915 trace_source_return_path_thread_resume_ack(value);
1917 if (value != MIGRATION_RESUME_ACK_VALUE) {
1918 error_report("%s: illegal resume_ack value %"PRIu32,
1919 __func__, value);
1920 return -1;
1923 /* Now both sides are active. */
1924 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
1925 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1927 /* TODO: notify send thread that time to continue send pages */
1929 return 0;
1933 * Handles messages sent on the return path towards the source VM
1936 static void *source_return_path_thread(void *opaque)
1938 MigrationState *ms = opaque;
1939 QEMUFile *rp = ms->rp_state.from_dst_file;
1940 uint16_t header_len, header_type;
1941 uint8_t buf[512];
1942 uint32_t tmp32, sibling_error;
1943 ram_addr_t start = 0; /* =0 to silence warning */
1944 size_t len = 0, expected_len;
1945 int res;
1947 trace_source_return_path_thread_entry();
1949 retry:
1950 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1951 migration_is_setup_or_active(ms->state)) {
1952 trace_source_return_path_thread_loop_top();
1953 header_type = qemu_get_be16(rp);
1954 header_len = qemu_get_be16(rp);
1956 if (qemu_file_get_error(rp)) {
1957 mark_source_rp_bad(ms);
1958 goto out;
1961 if (header_type >= MIG_RP_MSG_MAX ||
1962 header_type == MIG_RP_MSG_INVALID) {
1963 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1964 header_type, header_len);
1965 mark_source_rp_bad(ms);
1966 goto out;
1969 if ((rp_cmd_args[header_type].len != -1 &&
1970 header_len != rp_cmd_args[header_type].len) ||
1971 header_len > sizeof(buf)) {
1972 error_report("RP: Received '%s' message (0x%04x) with"
1973 "incorrect length %d expecting %zu",
1974 rp_cmd_args[header_type].name, header_type, header_len,
1975 (size_t)rp_cmd_args[header_type].len);
1976 mark_source_rp_bad(ms);
1977 goto out;
1980 /* We know we've got a valid header by this point */
1981 res = qemu_get_buffer(rp, buf, header_len);
1982 if (res != header_len) {
1983 error_report("RP: Failed reading data for message 0x%04x"
1984 " read %d expected %d",
1985 header_type, res, header_len);
1986 mark_source_rp_bad(ms);
1987 goto out;
1990 /* OK, we have the message and the data */
1991 switch (header_type) {
1992 case MIG_RP_MSG_SHUT:
1993 sibling_error = ldl_be_p(buf);
1994 trace_source_return_path_thread_shut(sibling_error);
1995 if (sibling_error) {
1996 error_report("RP: Sibling indicated error %d", sibling_error);
1997 mark_source_rp_bad(ms);
2000 * We'll let the main thread deal with closing the RP
2001 * we could do a shutdown(2) on it, but we're the only user
2002 * anyway, so there's nothing gained.
2004 goto out;
2006 case MIG_RP_MSG_PONG:
2007 tmp32 = ldl_be_p(buf);
2008 trace_source_return_path_thread_pong(tmp32);
2009 break;
2011 case MIG_RP_MSG_REQ_PAGES:
2012 start = ldq_be_p(buf);
2013 len = ldl_be_p(buf + 8);
2014 migrate_handle_rp_req_pages(ms, NULL, start, len);
2015 break;
2017 case MIG_RP_MSG_REQ_PAGES_ID:
2018 expected_len = 12 + 1; /* header + termination */
2020 if (header_len >= expected_len) {
2021 start = ldq_be_p(buf);
2022 len = ldl_be_p(buf + 8);
2023 /* Now we expect an idstr */
2024 tmp32 = buf[12]; /* Length of the following idstr */
2025 buf[13 + tmp32] = '\0';
2026 expected_len += tmp32;
2028 if (header_len != expected_len) {
2029 error_report("RP: Req_Page_id with length %d expecting %zd",
2030 header_len, expected_len);
2031 mark_source_rp_bad(ms);
2032 goto out;
2034 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2035 break;
2037 case MIG_RP_MSG_RECV_BITMAP:
2038 if (header_len < 1) {
2039 error_report("%s: missing block name", __func__);
2040 mark_source_rp_bad(ms);
2041 goto out;
2043 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2044 buf[buf[0] + 1] = '\0';
2045 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2046 mark_source_rp_bad(ms);
2047 goto out;
2049 break;
2051 case MIG_RP_MSG_RESUME_ACK:
2052 tmp32 = ldl_be_p(buf);
2053 if (migrate_handle_rp_resume_ack(ms, tmp32)) {
2054 mark_source_rp_bad(ms);
2055 goto out;
2057 break;
2059 default:
2060 break;
2064 out:
2065 res = qemu_file_get_error(rp);
2066 if (res) {
2067 if (res == -EIO) {
2069 * Maybe there is something we can do: it looks like a
2070 * network down issue, and we pause for a recovery.
2072 if (postcopy_pause_return_path_thread(ms)) {
2073 /* Reload rp, reset the rest */
2074 rp = ms->rp_state.from_dst_file;
2075 ms->rp_state.error = false;
2076 goto retry;
2080 trace_source_return_path_thread_bad_end();
2081 mark_source_rp_bad(ms);
2084 trace_source_return_path_thread_end();
2085 ms->rp_state.from_dst_file = NULL;
2086 qemu_fclose(rp);
2087 return NULL;
2090 static int open_return_path_on_source(MigrationState *ms,
2091 bool create_thread)
2094 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
2095 if (!ms->rp_state.from_dst_file) {
2096 return -1;
2099 trace_open_return_path_on_source();
2101 if (!create_thread) {
2102 /* We're done */
2103 return 0;
2106 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2107 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2109 trace_open_return_path_on_source_continue();
2111 return 0;
2114 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2115 static int await_return_path_close_on_source(MigrationState *ms)
2118 * If this is a normal exit then the destination will send a SHUT and the
2119 * rp_thread will exit, however if there's an error we need to cause
2120 * it to exit.
2122 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
2124 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2125 * waiting for the destination.
2127 qemu_file_shutdown(ms->rp_state.from_dst_file);
2128 mark_source_rp_bad(ms);
2130 trace_await_return_path_close_on_source_joining();
2131 qemu_thread_join(&ms->rp_state.rp_thread);
2132 trace_await_return_path_close_on_source_close();
2133 return ms->rp_state.error;
2137 * Switch from normal iteration to postcopy
2138 * Returns non-0 on error
2140 static int postcopy_start(MigrationState *ms)
2142 int ret;
2143 QIOChannelBuffer *bioc;
2144 QEMUFile *fb;
2145 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2146 bool restart_block = false;
2147 int cur_state = MIGRATION_STATUS_ACTIVE;
2148 if (!migrate_pause_before_switchover()) {
2149 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2150 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2153 trace_postcopy_start();
2154 qemu_mutex_lock_iothread();
2155 trace_postcopy_start_set_run();
2157 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
2158 global_state_store();
2159 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
2160 if (ret < 0) {
2161 goto fail;
2164 ret = migration_maybe_pause(ms, &cur_state,
2165 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2166 if (ret < 0) {
2167 goto fail;
2170 ret = bdrv_inactivate_all();
2171 if (ret < 0) {
2172 goto fail;
2174 restart_block = true;
2177 * Cause any non-postcopiable, but iterative devices to
2178 * send out their final data.
2180 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
2183 * in Finish migrate and with the io-lock held everything should
2184 * be quiet, but we've potentially still got dirty pages and we
2185 * need to tell the destination to throw any pages it's already received
2186 * that are dirty
2188 if (migrate_postcopy_ram()) {
2189 if (ram_postcopy_send_discard_bitmap(ms)) {
2190 error_report("postcopy send discard bitmap failed");
2191 goto fail;
2196 * send rest of state - note things that are doing postcopy
2197 * will notice we're in POSTCOPY_ACTIVE and not actually
2198 * wrap their state up here
2200 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
2201 if (migrate_postcopy_ram()) {
2202 /* Ping just for debugging, helps line traces up */
2203 qemu_savevm_send_ping(ms->to_dst_file, 2);
2207 * While loading the device state we may trigger page transfer
2208 * requests and the fd must be free to process those, and thus
2209 * the destination must read the whole device state off the fd before
2210 * it starts processing it. Unfortunately the ad-hoc migration format
2211 * doesn't allow the destination to know the size to read without fully
2212 * parsing it through each devices load-state code (especially the open
2213 * coded devices that use get/put).
2214 * So we wrap the device state up in a package with a length at the start;
2215 * to do this we use a qemu_buf to hold the whole of the device state.
2217 bioc = qio_channel_buffer_new(4096);
2218 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
2219 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2220 object_unref(OBJECT(bioc));
2223 * Make sure the receiver can get incoming pages before we send the rest
2224 * of the state
2226 qemu_savevm_send_postcopy_listen(fb);
2228 qemu_savevm_state_complete_precopy(fb, false, false);
2229 if (migrate_postcopy_ram()) {
2230 qemu_savevm_send_ping(fb, 3);
2233 qemu_savevm_send_postcopy_run(fb);
2235 /* <><> end of stuff going into the package */
2237 /* Last point of recovery; as soon as we send the package the destination
2238 * can open devices and potentially start running.
2239 * Lets just check again we've not got any errors.
2241 ret = qemu_file_get_error(ms->to_dst_file);
2242 if (ret) {
2243 error_report("postcopy_start: Migration stream errored (pre package)");
2244 goto fail_closefb;
2247 restart_block = false;
2249 /* Now send that blob */
2250 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
2251 goto fail_closefb;
2253 qemu_fclose(fb);
2255 /* Send a notify to give a chance for anything that needs to happen
2256 * at the transition to postcopy and after the device state; in particular
2257 * spice needs to trigger a transition now
2259 ms->postcopy_after_devices = true;
2260 notifier_list_notify(&migration_state_notifiers, ms);
2262 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2264 qemu_mutex_unlock_iothread();
2266 if (migrate_postcopy_ram()) {
2268 * Although this ping is just for debug, it could potentially be
2269 * used for getting a better measurement of downtime at the source.
2271 qemu_savevm_send_ping(ms->to_dst_file, 4);
2274 if (migrate_release_ram()) {
2275 ram_postcopy_migrated_memory_release(ms);
2278 ret = qemu_file_get_error(ms->to_dst_file);
2279 if (ret) {
2280 error_report("postcopy_start: Migration stream errored");
2281 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2282 MIGRATION_STATUS_FAILED);
2285 return ret;
2287 fail_closefb:
2288 qemu_fclose(fb);
2289 fail:
2290 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2291 MIGRATION_STATUS_FAILED);
2292 if (restart_block) {
2293 /* A failure happened early enough that we know the destination hasn't
2294 * accessed block devices, so we're safe to recover.
2296 Error *local_err = NULL;
2298 bdrv_invalidate_cache_all(&local_err);
2299 if (local_err) {
2300 error_report_err(local_err);
2303 qemu_mutex_unlock_iothread();
2304 return -1;
2308 * migration_maybe_pause: Pause if required to by
2309 * migrate_pause_before_switchover called with the iothread locked
2310 * Returns: 0 on success
2312 static int migration_maybe_pause(MigrationState *s,
2313 int *current_active_state,
2314 int new_state)
2316 if (!migrate_pause_before_switchover()) {
2317 return 0;
2320 /* Since leaving this state is not atomic with posting the semaphore
2321 * it's possible that someone could have issued multiple migrate_continue
2322 * and the semaphore is incorrectly positive at this point;
2323 * the docs say it's undefined to reinit a semaphore that's already
2324 * init'd, so use timedwait to eat up any existing posts.
2326 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2327 /* This block intentionally left blank */
2330 qemu_mutex_unlock_iothread();
2331 migrate_set_state(&s->state, *current_active_state,
2332 MIGRATION_STATUS_PRE_SWITCHOVER);
2333 qemu_sem_wait(&s->pause_sem);
2334 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
2335 new_state);
2336 *current_active_state = new_state;
2337 qemu_mutex_lock_iothread();
2339 return s->state == new_state ? 0 : -EINVAL;
2343 * migration_completion: Used by migration_thread when there's not much left.
2344 * The caller 'breaks' the loop when this returns.
2346 * @s: Current migration state
2348 static void migration_completion(MigrationState *s)
2350 int ret;
2351 int current_active_state = s->state;
2353 if (s->state == MIGRATION_STATUS_ACTIVE) {
2354 qemu_mutex_lock_iothread();
2355 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2356 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
2357 s->vm_was_running = runstate_is_running();
2358 ret = global_state_store();
2360 if (!ret) {
2361 bool inactivate = !migrate_colo_enabled();
2362 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
2363 if (ret >= 0) {
2364 ret = migration_maybe_pause(s, &current_active_state,
2365 MIGRATION_STATUS_DEVICE);
2367 if (ret >= 0) {
2368 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
2369 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2370 inactivate);
2372 if (inactivate && ret >= 0) {
2373 s->block_inactive = true;
2376 qemu_mutex_unlock_iothread();
2378 if (ret < 0) {
2379 goto fail;
2381 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2382 trace_migration_completion_postcopy_end();
2384 qemu_savevm_state_complete_postcopy(s->to_dst_file);
2385 trace_migration_completion_postcopy_end_after_complete();
2389 * If rp was opened we must clean up the thread before
2390 * cleaning everything else up (since if there are no failures
2391 * it will wait for the destination to send it's status in
2392 * a SHUT command).
2394 if (s->rp_state.from_dst_file) {
2395 int rp_error;
2396 trace_migration_return_path_end_before();
2397 rp_error = await_return_path_close_on_source(s);
2398 trace_migration_return_path_end_after(rp_error);
2399 if (rp_error) {
2400 goto fail_invalidate;
2404 if (qemu_file_get_error(s->to_dst_file)) {
2405 trace_migration_completion_file_err();
2406 goto fail_invalidate;
2409 if (!migrate_colo_enabled()) {
2410 migrate_set_state(&s->state, current_active_state,
2411 MIGRATION_STATUS_COMPLETED);
2414 return;
2416 fail_invalidate:
2417 /* If not doing postcopy, vm_start() will be called: let's regain
2418 * control on images.
2420 if (s->state == MIGRATION_STATUS_ACTIVE ||
2421 s->state == MIGRATION_STATUS_DEVICE) {
2422 Error *local_err = NULL;
2424 qemu_mutex_lock_iothread();
2425 bdrv_invalidate_cache_all(&local_err);
2426 if (local_err) {
2427 error_report_err(local_err);
2428 } else {
2429 s->block_inactive = false;
2431 qemu_mutex_unlock_iothread();
2434 fail:
2435 migrate_set_state(&s->state, current_active_state,
2436 MIGRATION_STATUS_FAILED);
2439 bool migrate_colo_enabled(void)
2441 MigrationState *s = migrate_get_current();
2442 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
2445 typedef enum MigThrError {
2446 /* No error detected */
2447 MIG_THR_ERR_NONE = 0,
2448 /* Detected error, but resumed successfully */
2449 MIG_THR_ERR_RECOVERED = 1,
2450 /* Detected fatal error, need to exit */
2451 MIG_THR_ERR_FATAL = 2,
2452 } MigThrError;
2454 /* Return zero if success, or <0 for error */
2455 static int postcopy_do_resume(MigrationState *s)
2457 /* TODO: do the resume logic */
2458 return 0;
2462 * We don't return until we are in a safe state to continue current
2463 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
2464 * MIG_THR_ERR_FATAL if unrecovery failure happened.
2466 static MigThrError postcopy_pause(MigrationState *s)
2468 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2470 while (true) {
2471 migrate_set_state(&s->state, s->state,
2472 MIGRATION_STATUS_POSTCOPY_PAUSED);
2474 /* Current channel is possibly broken. Release it. */
2475 assert(s->to_dst_file);
2476 qemu_file_shutdown(s->to_dst_file);
2477 qemu_fclose(s->to_dst_file);
2478 s->to_dst_file = NULL;
2480 error_report("Detected IO failure for postcopy. "
2481 "Migration paused.");
2484 * We wait until things fixed up. Then someone will setup the
2485 * status back for us.
2487 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2488 qemu_sem_wait(&s->postcopy_pause_sem);
2491 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
2492 /* Woken up by a recover procedure. Give it a shot */
2495 * Firstly, let's wake up the return path now, with a new
2496 * return path channel.
2498 qemu_sem_post(&s->postcopy_pause_rp_sem);
2500 /* Do the resume logic */
2501 if (postcopy_do_resume(s) == 0) {
2502 /* Let's continue! */
2503 trace_postcopy_pause_continued();
2504 return MIG_THR_ERR_RECOVERED;
2505 } else {
2507 * Something wrong happened during the recovery, let's
2508 * pause again. Pause is always better than throwing
2509 * data away.
2511 continue;
2513 } else {
2514 /* This is not right... Time to quit. */
2515 return MIG_THR_ERR_FATAL;
2520 static MigThrError migration_detect_error(MigrationState *s)
2522 int ret;
2524 /* Try to detect any file errors */
2525 ret = qemu_file_get_error(s->to_dst_file);
2527 if (!ret) {
2528 /* Everything is fine */
2529 return MIG_THR_ERR_NONE;
2532 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
2534 * For postcopy, we allow the network to be down for a
2535 * while. After that, it can be continued by a
2536 * recovery phase.
2538 return postcopy_pause(s);
2539 } else {
2541 * For precopy (or postcopy with error outside IO), we fail
2542 * with no time.
2544 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
2545 trace_migration_thread_file_err();
2547 /* Time to stop the migration, now. */
2548 return MIG_THR_ERR_FATAL;
2552 static void migration_calculate_complete(MigrationState *s)
2554 uint64_t bytes = qemu_ftell(s->to_dst_file);
2555 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2557 s->total_time = end_time - s->start_time;
2558 if (!s->downtime) {
2560 * It's still not set, so we are precopy migration. For
2561 * postcopy, downtime is calculated during postcopy_start().
2563 s->downtime = end_time - s->downtime_start;
2566 if (s->total_time) {
2567 s->mbps = ((double) bytes * 8.0) / s->total_time / 1000;
2571 static void migration_update_counters(MigrationState *s,
2572 int64_t current_time)
2574 uint64_t transferred, time_spent;
2575 double bandwidth;
2577 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
2578 return;
2581 transferred = qemu_ftell(s->to_dst_file) - s->iteration_initial_bytes;
2582 time_spent = current_time - s->iteration_start_time;
2583 bandwidth = (double)transferred / time_spent;
2584 s->threshold_size = bandwidth * s->parameters.downtime_limit;
2586 s->mbps = (((double) transferred * 8.0) /
2587 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
2590 * if we haven't sent anything, we don't want to
2591 * recalculate. 10000 is a small enough number for our purposes
2593 if (ram_counters.dirty_pages_rate && transferred > 10000) {
2594 s->expected_downtime = ram_counters.dirty_pages_rate *
2595 qemu_target_page_size() / bandwidth;
2598 qemu_file_reset_rate_limit(s->to_dst_file);
2600 s->iteration_start_time = current_time;
2601 s->iteration_initial_bytes = qemu_ftell(s->to_dst_file);
2603 trace_migrate_transferred(transferred, time_spent,
2604 bandwidth, s->threshold_size);
2607 /* Migration thread iteration status */
2608 typedef enum {
2609 MIG_ITERATE_RESUME, /* Resume current iteration */
2610 MIG_ITERATE_SKIP, /* Skip current iteration */
2611 MIG_ITERATE_BREAK, /* Break the loop */
2612 } MigIterateState;
2615 * Return true if continue to the next iteration directly, false
2616 * otherwise.
2618 static MigIterateState migration_iteration_run(MigrationState *s)
2620 uint64_t pending_size, pend_pre, pend_compat, pend_post;
2621 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
2623 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
2624 &pend_compat, &pend_post);
2625 pending_size = pend_pre + pend_compat + pend_post;
2627 trace_migrate_pending(pending_size, s->threshold_size,
2628 pend_pre, pend_compat, pend_post);
2630 if (pending_size && pending_size >= s->threshold_size) {
2631 /* Still a significant amount to transfer */
2632 if (migrate_postcopy() && !in_postcopy &&
2633 pend_pre <= s->threshold_size &&
2634 atomic_read(&s->start_postcopy)) {
2635 if (postcopy_start(s)) {
2636 error_report("%s: postcopy failed to start", __func__);
2638 return MIG_ITERATE_SKIP;
2640 /* Just another iteration step */
2641 qemu_savevm_state_iterate(s->to_dst_file,
2642 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2643 } else {
2644 trace_migration_thread_low_pending(pending_size);
2645 migration_completion(s);
2646 return MIG_ITERATE_BREAK;
2649 return MIG_ITERATE_RESUME;
2652 static void migration_iteration_finish(MigrationState *s)
2654 /* If we enabled cpu throttling for auto-converge, turn it off. */
2655 cpu_throttle_stop();
2657 qemu_mutex_lock_iothread();
2658 switch (s->state) {
2659 case MIGRATION_STATUS_COMPLETED:
2660 migration_calculate_complete(s);
2661 runstate_set(RUN_STATE_POSTMIGRATE);
2662 break;
2664 case MIGRATION_STATUS_ACTIVE:
2666 * We should really assert here, but since it's during
2667 * migration, let's try to reduce the usage of assertions.
2669 if (!migrate_colo_enabled()) {
2670 error_report("%s: critical error: calling COLO code without "
2671 "COLO enabled", __func__);
2673 migrate_start_colo_process(s);
2675 * Fixme: we will run VM in COLO no matter its old running state.
2676 * After exited COLO, we will keep running.
2678 s->vm_was_running = true;
2679 /* Fallthrough */
2680 case MIGRATION_STATUS_FAILED:
2681 case MIGRATION_STATUS_CANCELLED:
2682 if (s->vm_was_running) {
2683 vm_start();
2684 } else {
2685 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2686 runstate_set(RUN_STATE_POSTMIGRATE);
2689 break;
2691 default:
2692 /* Should not reach here, but if so, forgive the VM. */
2693 error_report("%s: Unknown ending state %d", __func__, s->state);
2694 break;
2696 qemu_bh_schedule(s->cleanup_bh);
2697 qemu_mutex_unlock_iothread();
2701 * Master migration thread on the source VM.
2702 * It drives the migration and pumps the data down the outgoing channel.
2704 static void *migration_thread(void *opaque)
2706 MigrationState *s = opaque;
2707 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
2708 MigThrError thr_error;
2710 rcu_register_thread();
2712 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2714 qemu_savevm_state_header(s->to_dst_file);
2717 * If we opened the return path, we need to make sure dst has it
2718 * opened as well.
2720 if (s->rp_state.from_dst_file) {
2721 /* Now tell the dest that it should open its end so it can reply */
2722 qemu_savevm_send_open_return_path(s->to_dst_file);
2724 /* And do a ping that will make stuff easier to debug */
2725 qemu_savevm_send_ping(s->to_dst_file, 1);
2728 if (migrate_postcopy()) {
2730 * Tell the destination that we *might* want to do postcopy later;
2731 * if the other end can't do postcopy it should fail now, nice and
2732 * early.
2734 qemu_savevm_send_postcopy_advise(s->to_dst_file);
2737 qemu_savevm_state_setup(s->to_dst_file);
2739 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
2740 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2741 MIGRATION_STATUS_ACTIVE);
2743 trace_migration_thread_setup_complete();
2745 while (s->state == MIGRATION_STATUS_ACTIVE ||
2746 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2747 int64_t current_time;
2749 if (!qemu_file_rate_limit(s->to_dst_file)) {
2750 MigIterateState iter_state = migration_iteration_run(s);
2751 if (iter_state == MIG_ITERATE_SKIP) {
2752 continue;
2753 } else if (iter_state == MIG_ITERATE_BREAK) {
2754 break;
2759 * Try to detect any kind of failures, and see whether we
2760 * should stop the migration now.
2762 thr_error = migration_detect_error(s);
2763 if (thr_error == MIG_THR_ERR_FATAL) {
2764 /* Stop migration */
2765 break;
2766 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
2768 * Just recovered from a e.g. network failure, reset all
2769 * the local variables. This is important to avoid
2770 * breaking transferred_bytes and bandwidth calculation
2772 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2773 s->iteration_initial_bytes = 0;
2776 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2778 migration_update_counters(s, current_time);
2780 if (qemu_file_rate_limit(s->to_dst_file)) {
2781 /* usleep expects microseconds */
2782 g_usleep((s->iteration_start_time + BUFFER_DELAY -
2783 current_time) * 1000);
2787 trace_migration_thread_after_loop();
2788 migration_iteration_finish(s);
2789 rcu_unregister_thread();
2790 return NULL;
2793 void migrate_fd_connect(MigrationState *s, Error *error_in)
2795 int64_t rate_limit;
2796 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
2798 s->expected_downtime = s->parameters.downtime_limit;
2799 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
2800 if (error_in) {
2801 migrate_fd_error(s, error_in);
2802 migrate_fd_cleanup(s);
2803 return;
2806 if (resume) {
2807 /* This is a resumed migration */
2808 rate_limit = INT64_MAX;
2809 } else {
2810 /* This is a fresh new migration */
2811 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
2812 s->expected_downtime = s->parameters.downtime_limit;
2813 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
2815 /* Notify before starting migration thread */
2816 notifier_list_notify(&migration_state_notifiers, s);
2819 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
2820 qemu_file_set_blocking(s->to_dst_file, true);
2823 * Open the return path. For postcopy, it is used exclusively. For
2824 * precopy, only if user specified "return-path" capability would
2825 * QEMU uses the return path.
2827 if (migrate_postcopy_ram() || migrate_use_return_path()) {
2828 if (open_return_path_on_source(s, !resume)) {
2829 error_report("Unable to open return-path for postcopy");
2830 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
2831 migrate_fd_cleanup(s);
2832 return;
2836 if (resume) {
2837 /* Wakeup the main migration thread to do the recovery */
2838 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
2839 MIGRATION_STATUS_POSTCOPY_RECOVER);
2840 qemu_sem_post(&s->postcopy_pause_sem);
2841 return;
2844 if (multifd_save_setup() != 0) {
2845 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2846 MIGRATION_STATUS_FAILED);
2847 migrate_fd_cleanup(s);
2848 return;
2850 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
2851 QEMU_THREAD_JOINABLE);
2852 s->migration_thread_running = true;
2855 void migration_global_dump(Monitor *mon)
2857 MigrationState *ms = migrate_get_current();
2859 monitor_printf(mon, "globals:\n");
2860 monitor_printf(mon, "store-global-state: %s\n",
2861 ms->store_global_state ? "on" : "off");
2862 monitor_printf(mon, "only-migratable: %s\n",
2863 ms->only_migratable ? "on" : "off");
2864 monitor_printf(mon, "send-configuration: %s\n",
2865 ms->send_configuration ? "on" : "off");
2866 monitor_printf(mon, "send-section-footer: %s\n",
2867 ms->send_section_footer ? "on" : "off");
2870 #define DEFINE_PROP_MIG_CAP(name, x) \
2871 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
2873 static Property migration_properties[] = {
2874 DEFINE_PROP_BOOL("store-global-state", MigrationState,
2875 store_global_state, true),
2876 DEFINE_PROP_BOOL("only-migratable", MigrationState, only_migratable, false),
2877 DEFINE_PROP_BOOL("send-configuration", MigrationState,
2878 send_configuration, true),
2879 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
2880 send_section_footer, true),
2882 /* Migration parameters */
2883 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
2884 parameters.compress_level,
2885 DEFAULT_MIGRATE_COMPRESS_LEVEL),
2886 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
2887 parameters.compress_threads,
2888 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
2889 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
2890 parameters.decompress_threads,
2891 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
2892 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
2893 parameters.cpu_throttle_initial,
2894 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
2895 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
2896 parameters.cpu_throttle_increment,
2897 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
2898 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
2899 parameters.max_bandwidth, MAX_THROTTLE),
2900 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
2901 parameters.downtime_limit,
2902 DEFAULT_MIGRATE_SET_DOWNTIME),
2903 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
2904 parameters.x_checkpoint_delay,
2905 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
2906 DEFINE_PROP_UINT8("x-multifd-channels", MigrationState,
2907 parameters.x_multifd_channels,
2908 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
2909 DEFINE_PROP_UINT32("x-multifd-page-count", MigrationState,
2910 parameters.x_multifd_page_count,
2911 DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT),
2912 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
2913 parameters.xbzrle_cache_size,
2914 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
2916 /* Migration capabilities */
2917 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
2918 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
2919 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
2920 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
2921 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
2922 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
2923 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
2924 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
2925 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
2926 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
2927 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
2928 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_X_MULTIFD),
2930 DEFINE_PROP_END_OF_LIST(),
2933 static void migration_class_init(ObjectClass *klass, void *data)
2935 DeviceClass *dc = DEVICE_CLASS(klass);
2937 dc->user_creatable = false;
2938 dc->props = migration_properties;
2941 static void migration_instance_finalize(Object *obj)
2943 MigrationState *ms = MIGRATION_OBJ(obj);
2944 MigrationParameters *params = &ms->parameters;
2946 qemu_mutex_destroy(&ms->error_mutex);
2947 g_free(params->tls_hostname);
2948 g_free(params->tls_creds);
2949 qemu_sem_destroy(&ms->pause_sem);
2950 qemu_sem_destroy(&ms->postcopy_pause_sem);
2951 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
2952 error_free(ms->error);
2955 static void migration_instance_init(Object *obj)
2957 MigrationState *ms = MIGRATION_OBJ(obj);
2958 MigrationParameters *params = &ms->parameters;
2960 ms->state = MIGRATION_STATUS_NONE;
2961 ms->mbps = -1;
2962 qemu_sem_init(&ms->pause_sem, 0);
2963 qemu_mutex_init(&ms->error_mutex);
2965 params->tls_hostname = g_strdup("");
2966 params->tls_creds = g_strdup("");
2968 /* Set has_* up only for parameter checks */
2969 params->has_compress_level = true;
2970 params->has_compress_threads = true;
2971 params->has_decompress_threads = true;
2972 params->has_cpu_throttle_initial = true;
2973 params->has_cpu_throttle_increment = true;
2974 params->has_max_bandwidth = true;
2975 params->has_downtime_limit = true;
2976 params->has_x_checkpoint_delay = true;
2977 params->has_block_incremental = true;
2978 params->has_x_multifd_channels = true;
2979 params->has_x_multifd_page_count = true;
2980 params->has_xbzrle_cache_size = true;
2982 qemu_sem_init(&ms->postcopy_pause_sem, 0);
2983 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
2987 * Return true if check pass, false otherwise. Error will be put
2988 * inside errp if provided.
2990 static bool migration_object_check(MigrationState *ms, Error **errp)
2992 MigrationCapabilityStatusList *head = NULL;
2993 /* Assuming all off */
2994 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
2995 int i;
2997 if (!migrate_params_check(&ms->parameters, errp)) {
2998 return false;
3001 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
3002 if (ms->enabled_capabilities[i]) {
3003 head = migrate_cap_add(head, i, true);
3007 ret = migrate_caps_check(cap_list, head, errp);
3009 /* It works with head == NULL */
3010 qapi_free_MigrationCapabilityStatusList(head);
3012 return ret;
3015 static const TypeInfo migration_type = {
3016 .name = TYPE_MIGRATION,
3018 * NOTE: TYPE_MIGRATION is not really a device, as the object is
3019 * not created using qdev_create(), it is not attached to the qdev
3020 * device tree, and it is never realized.
3022 * TODO: Make this TYPE_OBJECT once QOM provides something like
3023 * TYPE_DEVICE's "-global" properties.
3025 .parent = TYPE_DEVICE,
3026 .class_init = migration_class_init,
3027 .class_size = sizeof(MigrationClass),
3028 .instance_size = sizeof(MigrationState),
3029 .instance_init = migration_instance_init,
3030 .instance_finalize = migration_instance_finalize,
3033 static void register_migration_types(void)
3035 type_register_static(&migration_type);
3038 type_init(register_migration_types);