migration: new message MIG_RP_MSG_RECV_BITMAP
[qemu/ar7.git] / migration / migration.c
blob7c5e20b3f6049f25f0c0ac8aa0e0506d96c4cd2a
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 */
100 MIG_RP_MSG_MAX
103 /* When we add fault tolerance, we could have several
104 migrations at once. For now we don't need to add
105 dynamic creation of migration */
107 static MigrationState *current_migration;
109 static bool migration_object_check(MigrationState *ms, Error **errp);
110 static int migration_maybe_pause(MigrationState *s,
111 int *current_active_state,
112 int new_state);
114 void migration_object_init(void)
116 MachineState *ms = MACHINE(qdev_get_machine());
117 Error *err = NULL;
119 /* This can only be called once. */
120 assert(!current_migration);
121 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
123 if (!migration_object_check(current_migration, &err)) {
124 error_report_err(err);
125 exit(1);
129 * We cannot really do this in migration_instance_init() since at
130 * that time global properties are not yet applied, then this
131 * value will be definitely replaced by something else.
133 if (ms->enforce_config_section) {
134 current_migration->send_configuration = true;
138 void migration_object_finalize(void)
140 object_unref(OBJECT(current_migration));
143 /* For outgoing */
144 MigrationState *migrate_get_current(void)
146 /* This can only be called after the object created. */
147 assert(current_migration);
148 return current_migration;
151 MigrationIncomingState *migration_incoming_get_current(void)
153 static bool once;
154 static MigrationIncomingState mis_current;
156 if (!once) {
157 mis_current.state = MIGRATION_STATUS_NONE;
158 memset(&mis_current, 0, sizeof(MigrationIncomingState));
159 mis_current.postcopy_remote_fds = g_array_new(FALSE, TRUE,
160 sizeof(struct PostCopyFD));
161 qemu_mutex_init(&mis_current.rp_mutex);
162 qemu_event_init(&mis_current.main_thread_load_event, false);
163 qemu_sem_init(&mis_current.postcopy_pause_sem_dst, 0);
164 qemu_sem_init(&mis_current.postcopy_pause_sem_fault, 0);
166 init_dirty_bitmap_incoming_migration();
168 once = true;
170 return &mis_current;
173 void migration_incoming_state_destroy(void)
175 struct MigrationIncomingState *mis = migration_incoming_get_current();
177 if (mis->to_src_file) {
178 /* Tell source that we are done */
179 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
180 qemu_fclose(mis->to_src_file);
181 mis->to_src_file = NULL;
184 if (mis->from_src_file) {
185 qemu_fclose(mis->from_src_file);
186 mis->from_src_file = NULL;
188 if (mis->postcopy_remote_fds) {
189 g_array_free(mis->postcopy_remote_fds, TRUE);
190 mis->postcopy_remote_fds = NULL;
193 qemu_event_reset(&mis->main_thread_load_event);
196 static void migrate_generate_event(int new_state)
198 if (migrate_use_events()) {
199 qapi_event_send_migration(new_state, &error_abort);
204 * Called on -incoming with a defer: uri.
205 * The migration can be started later after any parameters have been
206 * changed.
208 static void deferred_incoming_migration(Error **errp)
210 if (deferred_incoming) {
211 error_setg(errp, "Incoming migration already deferred");
213 deferred_incoming = true;
217 * Send a message on the return channel back to the source
218 * of the migration.
220 static int migrate_send_rp_message(MigrationIncomingState *mis,
221 enum mig_rp_message_type message_type,
222 uint16_t len, void *data)
224 int ret = 0;
226 trace_migrate_send_rp_message((int)message_type, len);
227 qemu_mutex_lock(&mis->rp_mutex);
230 * It's possible that the file handle got lost due to network
231 * failures.
233 if (!mis->to_src_file) {
234 ret = -EIO;
235 goto error;
238 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
239 qemu_put_be16(mis->to_src_file, len);
240 qemu_put_buffer(mis->to_src_file, data, len);
241 qemu_fflush(mis->to_src_file);
243 /* It's possible that qemu file got error during sending */
244 ret = qemu_file_get_error(mis->to_src_file);
246 error:
247 qemu_mutex_unlock(&mis->rp_mutex);
248 return ret;
251 /* Request a range of pages from the source VM at the given
252 * start address.
253 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
254 * as the last request (a name must have been given previously)
255 * Start: Address offset within the RB
256 * Len: Length in bytes required - must be a multiple of pagesize
258 int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
259 ram_addr_t start, size_t len)
261 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
262 size_t msglen = 12; /* start + len */
263 enum mig_rp_message_type msg_type;
265 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
266 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
268 if (rbname) {
269 int rbname_len = strlen(rbname);
270 assert(rbname_len < 256);
272 bufc[msglen++] = rbname_len;
273 memcpy(bufc + msglen, rbname, rbname_len);
274 msglen += rbname_len;
275 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
276 } else {
277 msg_type = MIG_RP_MSG_REQ_PAGES;
280 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
283 void qemu_start_incoming_migration(const char *uri, Error **errp)
285 const char *p;
287 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
288 if (!strcmp(uri, "defer")) {
289 deferred_incoming_migration(errp);
290 } else if (strstart(uri, "tcp:", &p)) {
291 tcp_start_incoming_migration(p, errp);
292 #ifdef CONFIG_RDMA
293 } else if (strstart(uri, "rdma:", &p)) {
294 rdma_start_incoming_migration(p, errp);
295 #endif
296 } else if (strstart(uri, "exec:", &p)) {
297 exec_start_incoming_migration(p, errp);
298 } else if (strstart(uri, "unix:", &p)) {
299 unix_start_incoming_migration(p, errp);
300 } else if (strstart(uri, "fd:", &p)) {
301 fd_start_incoming_migration(p, errp);
302 } else {
303 error_setg(errp, "unknown migration protocol: %s", uri);
307 static void process_incoming_migration_bh(void *opaque)
309 Error *local_err = NULL;
310 MigrationIncomingState *mis = opaque;
312 /* Make sure all file formats flush their mutable metadata.
313 * If we get an error here, just don't restart the VM yet. */
314 bdrv_invalidate_cache_all(&local_err);
315 if (local_err) {
316 error_report_err(local_err);
317 local_err = NULL;
318 autostart = false;
322 * This must happen after all error conditions are dealt with and
323 * we're sure the VM is going to be running on this host.
325 qemu_announce_self();
327 if (multifd_load_cleanup(&local_err) != 0) {
328 error_report_err(local_err);
329 autostart = false;
331 /* If global state section was not received or we are in running
332 state, we need to obey autostart. Any other state is set with
333 runstate_set. */
335 dirty_bitmap_mig_before_vm_start();
337 if (!global_state_received() ||
338 global_state_get_runstate() == RUN_STATE_RUNNING) {
339 if (autostart) {
340 vm_start();
341 } else {
342 runstate_set(RUN_STATE_PAUSED);
344 } else {
345 runstate_set(global_state_get_runstate());
348 * This must happen after any state changes since as soon as an external
349 * observer sees this event they might start to prod at the VM assuming
350 * it's ready to use.
352 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
353 MIGRATION_STATUS_COMPLETED);
354 qemu_bh_delete(mis->bh);
355 migration_incoming_state_destroy();
358 static void process_incoming_migration_co(void *opaque)
360 MigrationIncomingState *mis = migration_incoming_get_current();
361 PostcopyState ps;
362 int ret;
364 assert(mis->from_src_file);
365 mis->largest_page_size = qemu_ram_pagesize_largest();
366 postcopy_state_set(POSTCOPY_INCOMING_NONE);
367 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
368 MIGRATION_STATUS_ACTIVE);
369 ret = qemu_loadvm_state(mis->from_src_file);
371 ps = postcopy_state_get();
372 trace_process_incoming_migration_co_end(ret, ps);
373 if (ps != POSTCOPY_INCOMING_NONE) {
374 if (ps == POSTCOPY_INCOMING_ADVISE) {
376 * Where a migration had postcopy enabled (and thus went to advise)
377 * but managed to complete within the precopy period, we can use
378 * the normal exit.
380 postcopy_ram_incoming_cleanup(mis);
381 } else if (ret >= 0) {
383 * Postcopy was started, cleanup should happen at the end of the
384 * postcopy thread.
386 trace_process_incoming_migration_co_postcopy_end_main();
387 return;
389 /* Else if something went wrong then just fall out of the normal exit */
392 /* we get COLO info, and know if we are in COLO mode */
393 if (!ret && migration_incoming_enable_colo()) {
394 mis->migration_incoming_co = qemu_coroutine_self();
395 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
396 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
397 mis->have_colo_incoming_thread = true;
398 qemu_coroutine_yield();
400 /* Wait checkpoint incoming thread exit before free resource */
401 qemu_thread_join(&mis->colo_incoming_thread);
404 if (ret < 0) {
405 Error *local_err = NULL;
407 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
408 MIGRATION_STATUS_FAILED);
409 error_report("load of migration failed: %s", strerror(-ret));
410 qemu_fclose(mis->from_src_file);
411 if (multifd_load_cleanup(&local_err) != 0) {
412 error_report_err(local_err);
414 exit(EXIT_FAILURE);
416 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
417 qemu_bh_schedule(mis->bh);
420 static void migration_incoming_setup(QEMUFile *f)
422 MigrationIncomingState *mis = migration_incoming_get_current();
424 if (multifd_load_setup() != 0) {
425 /* We haven't been able to create multifd threads
426 nothing better to do */
427 exit(EXIT_FAILURE);
430 if (!mis->from_src_file) {
431 mis->from_src_file = f;
433 qemu_file_set_blocking(f, false);
436 void migration_incoming_process(void)
438 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
439 qemu_coroutine_enter(co);
442 void migration_fd_process_incoming(QEMUFile *f)
444 MigrationIncomingState *mis = migration_incoming_get_current();
446 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
447 /* Resumed from a paused postcopy migration */
449 mis->from_src_file = f;
450 /* Postcopy has standalone thread to do vm load */
451 qemu_file_set_blocking(f, true);
453 /* Re-configure the return path */
454 mis->to_src_file = qemu_file_get_return_path(f);
456 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
457 MIGRATION_STATUS_POSTCOPY_RECOVER);
460 * Here, we only wake up the main loading thread (while the
461 * fault thread will still be waiting), so that we can receive
462 * commands from source now, and answer it if needed. The
463 * fault thread will be woken up afterwards until we are sure
464 * that source is ready to reply to page requests.
466 qemu_sem_post(&mis->postcopy_pause_sem_dst);
467 } else {
468 /* New incoming migration */
469 migration_incoming_setup(f);
470 migration_incoming_process();
474 void migration_ioc_process_incoming(QIOChannel *ioc)
476 MigrationIncomingState *mis = migration_incoming_get_current();
478 if (!mis->from_src_file) {
479 QEMUFile *f = qemu_fopen_channel_input(ioc);
480 migration_incoming_setup(f);
481 return;
483 multifd_recv_new_channel(ioc);
487 * @migration_has_all_channels: We have received all channels that we need
489 * Returns true when we have got connections to all the channels that
490 * we need for migration.
492 bool migration_has_all_channels(void)
494 bool all_channels;
496 all_channels = multifd_recv_all_channels_created();
498 return all_channels;
502 * Send a 'SHUT' message on the return channel with the given value
503 * to indicate that we've finished with the RP. Non-0 value indicates
504 * error.
506 void migrate_send_rp_shut(MigrationIncomingState *mis,
507 uint32_t value)
509 uint32_t buf;
511 buf = cpu_to_be32(value);
512 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
516 * Send a 'PONG' message on the return channel with the given value
517 * (normally in response to a 'PING')
519 void migrate_send_rp_pong(MigrationIncomingState *mis,
520 uint32_t value)
522 uint32_t buf;
524 buf = cpu_to_be32(value);
525 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
528 void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
529 char *block_name)
531 char buf[512];
532 int len;
533 int64_t res;
536 * First, we send the header part. It contains only the len of
537 * idstr, and the idstr itself.
539 len = strlen(block_name);
540 buf[0] = len;
541 memcpy(buf + 1, block_name, len);
543 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
544 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
545 __func__);
546 return;
549 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
552 * Next, we dump the received bitmap to the stream.
554 * TODO: currently we are safe since we are the only one that is
555 * using the to_src_file handle (fault thread is still paused),
556 * and it's ok even not taking the mutex. However the best way is
557 * to take the lock before sending the message header, and release
558 * the lock after sending the bitmap.
560 qemu_mutex_lock(&mis->rp_mutex);
561 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
562 qemu_mutex_unlock(&mis->rp_mutex);
564 trace_migrate_send_rp_recv_bitmap(block_name, res);
567 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
569 MigrationCapabilityStatusList *head = NULL;
570 MigrationCapabilityStatusList *caps;
571 MigrationState *s = migrate_get_current();
572 int i;
574 caps = NULL; /* silence compiler warning */
575 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
576 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
577 if (i == MIGRATION_CAPABILITY_BLOCK) {
578 continue;
580 #endif
581 if (head == NULL) {
582 head = g_malloc0(sizeof(*caps));
583 caps = head;
584 } else {
585 caps->next = g_malloc0(sizeof(*caps));
586 caps = caps->next;
588 caps->value =
589 g_malloc(sizeof(*caps->value));
590 caps->value->capability = i;
591 caps->value->state = s->enabled_capabilities[i];
594 return head;
597 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
599 MigrationParameters *params;
600 MigrationState *s = migrate_get_current();
602 /* TODO use QAPI_CLONE() instead of duplicating it inline */
603 params = g_malloc0(sizeof(*params));
604 params->has_compress_level = true;
605 params->compress_level = s->parameters.compress_level;
606 params->has_compress_threads = true;
607 params->compress_threads = s->parameters.compress_threads;
608 params->has_decompress_threads = true;
609 params->decompress_threads = s->parameters.decompress_threads;
610 params->has_cpu_throttle_initial = true;
611 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
612 params->has_cpu_throttle_increment = true;
613 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
614 params->has_tls_creds = true;
615 params->tls_creds = g_strdup(s->parameters.tls_creds);
616 params->has_tls_hostname = true;
617 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
618 params->has_max_bandwidth = true;
619 params->max_bandwidth = s->parameters.max_bandwidth;
620 params->has_downtime_limit = true;
621 params->downtime_limit = s->parameters.downtime_limit;
622 params->has_x_checkpoint_delay = true;
623 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
624 params->has_block_incremental = true;
625 params->block_incremental = s->parameters.block_incremental;
626 params->has_x_multifd_channels = true;
627 params->x_multifd_channels = s->parameters.x_multifd_channels;
628 params->has_x_multifd_page_count = true;
629 params->x_multifd_page_count = s->parameters.x_multifd_page_count;
630 params->has_xbzrle_cache_size = true;
631 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
633 return params;
637 * Return true if we're already in the middle of a migration
638 * (i.e. any of the active or setup states)
640 static bool migration_is_setup_or_active(int state)
642 switch (state) {
643 case MIGRATION_STATUS_ACTIVE:
644 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
645 case MIGRATION_STATUS_POSTCOPY_PAUSED:
646 case MIGRATION_STATUS_POSTCOPY_RECOVER:
647 case MIGRATION_STATUS_SETUP:
648 case MIGRATION_STATUS_PRE_SWITCHOVER:
649 case MIGRATION_STATUS_DEVICE:
650 return true;
652 default:
653 return false;
658 static void populate_ram_info(MigrationInfo *info, MigrationState *s)
660 info->has_ram = true;
661 info->ram = g_malloc0(sizeof(*info->ram));
662 info->ram->transferred = ram_counters.transferred;
663 info->ram->total = ram_bytes_total();
664 info->ram->duplicate = ram_counters.duplicate;
665 /* legacy value. It is not used anymore */
666 info->ram->skipped = 0;
667 info->ram->normal = ram_counters.normal;
668 info->ram->normal_bytes = ram_counters.normal *
669 qemu_target_page_size();
670 info->ram->mbps = s->mbps;
671 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
672 info->ram->postcopy_requests = ram_counters.postcopy_requests;
673 info->ram->page_size = qemu_target_page_size();
675 if (migrate_use_xbzrle()) {
676 info->has_xbzrle_cache = true;
677 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
678 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
679 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
680 info->xbzrle_cache->pages = xbzrle_counters.pages;
681 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
682 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
683 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
686 if (cpu_throttle_active()) {
687 info->has_cpu_throttle_percentage = true;
688 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
691 if (s->state != MIGRATION_STATUS_COMPLETED) {
692 info->ram->remaining = ram_bytes_remaining();
693 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
697 static void populate_disk_info(MigrationInfo *info)
699 if (blk_mig_active()) {
700 info->has_disk = true;
701 info->disk = g_malloc0(sizeof(*info->disk));
702 info->disk->transferred = blk_mig_bytes_transferred();
703 info->disk->remaining = blk_mig_bytes_remaining();
704 info->disk->total = blk_mig_bytes_total();
708 static void fill_source_migration_info(MigrationInfo *info)
710 MigrationState *s = migrate_get_current();
712 switch (s->state) {
713 case MIGRATION_STATUS_NONE:
714 /* no migration has happened ever */
715 /* do not overwrite destination migration status */
716 return;
717 break;
718 case MIGRATION_STATUS_SETUP:
719 info->has_status = true;
720 info->has_total_time = false;
721 break;
722 case MIGRATION_STATUS_ACTIVE:
723 case MIGRATION_STATUS_CANCELLING:
724 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
725 case MIGRATION_STATUS_PRE_SWITCHOVER:
726 case MIGRATION_STATUS_DEVICE:
727 case MIGRATION_STATUS_POSTCOPY_PAUSED:
728 case MIGRATION_STATUS_POSTCOPY_RECOVER:
729 /* TODO add some postcopy stats */
730 info->has_status = true;
731 info->has_total_time = true;
732 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
733 - s->start_time;
734 info->has_expected_downtime = true;
735 info->expected_downtime = s->expected_downtime;
736 info->has_setup_time = true;
737 info->setup_time = s->setup_time;
739 populate_ram_info(info, s);
740 populate_disk_info(info);
741 break;
742 case MIGRATION_STATUS_COLO:
743 info->has_status = true;
744 /* TODO: display COLO specific information (checkpoint info etc.) */
745 break;
746 case MIGRATION_STATUS_COMPLETED:
747 info->has_status = true;
748 info->has_total_time = true;
749 info->total_time = s->total_time;
750 info->has_downtime = true;
751 info->downtime = s->downtime;
752 info->has_setup_time = true;
753 info->setup_time = s->setup_time;
755 populate_ram_info(info, s);
756 break;
757 case MIGRATION_STATUS_FAILED:
758 info->has_status = true;
759 if (s->error) {
760 info->has_error_desc = true;
761 info->error_desc = g_strdup(error_get_pretty(s->error));
763 break;
764 case MIGRATION_STATUS_CANCELLED:
765 info->has_status = true;
766 break;
768 info->status = s->state;
772 * @migration_caps_check - check capability validity
774 * @cap_list: old capability list, array of bool
775 * @params: new capabilities to be applied soon
776 * @errp: set *errp if the check failed, with reason
778 * Returns true if check passed, otherwise false.
780 static bool migrate_caps_check(bool *cap_list,
781 MigrationCapabilityStatusList *params,
782 Error **errp)
784 MigrationCapabilityStatusList *cap;
785 bool old_postcopy_cap;
786 MigrationIncomingState *mis = migration_incoming_get_current();
788 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
790 for (cap = params; cap; cap = cap->next) {
791 cap_list[cap->value->capability] = cap->value->state;
794 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
795 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
796 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
797 "block migration");
798 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
799 return false;
801 #endif
803 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
804 if (cap_list[MIGRATION_CAPABILITY_COMPRESS]) {
805 /* The decompression threads asynchronously write into RAM
806 * rather than use the atomic copies needed to avoid
807 * userfaulting. It should be possible to fix the decompression
808 * threads for compatibility in future.
810 error_setg(errp, "Postcopy is not currently compatible "
811 "with compression");
812 return false;
815 /* This check is reasonably expensive, so only when it's being
816 * set the first time, also it's only the destination that needs
817 * special support.
819 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
820 !postcopy_ram_supported_by_host(mis)) {
821 /* postcopy_ram_supported_by_host will have emitted a more
822 * detailed message
824 error_setg(errp, "Postcopy is not supported");
825 return false;
829 return true;
832 static void fill_destination_migration_info(MigrationInfo *info)
834 MigrationIncomingState *mis = migration_incoming_get_current();
836 switch (mis->state) {
837 case MIGRATION_STATUS_NONE:
838 return;
839 break;
840 case MIGRATION_STATUS_SETUP:
841 case MIGRATION_STATUS_CANCELLING:
842 case MIGRATION_STATUS_CANCELLED:
843 case MIGRATION_STATUS_ACTIVE:
844 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
845 case MIGRATION_STATUS_FAILED:
846 case MIGRATION_STATUS_COLO:
847 info->has_status = true;
848 break;
849 case MIGRATION_STATUS_COMPLETED:
850 info->has_status = true;
851 fill_destination_postcopy_migration_info(info);
852 break;
854 info->status = mis->state;
857 MigrationInfo *qmp_query_migrate(Error **errp)
859 MigrationInfo *info = g_malloc0(sizeof(*info));
861 fill_destination_migration_info(info);
862 fill_source_migration_info(info);
864 return info;
867 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
868 Error **errp)
870 MigrationState *s = migrate_get_current();
871 MigrationCapabilityStatusList *cap;
872 bool cap_list[MIGRATION_CAPABILITY__MAX];
874 if (migration_is_setup_or_active(s->state)) {
875 error_setg(errp, QERR_MIGRATION_ACTIVE);
876 return;
879 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
880 if (!migrate_caps_check(cap_list, params, errp)) {
881 return;
884 for (cap = params; cap; cap = cap->next) {
885 s->enabled_capabilities[cap->value->capability] = cap->value->state;
890 * Check whether the parameters are valid. Error will be put into errp
891 * (if provided). Return true if valid, otherwise false.
893 static bool migrate_params_check(MigrationParameters *params, Error **errp)
895 if (params->has_compress_level &&
896 (params->compress_level > 9)) {
897 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
898 "is invalid, it should be in the range of 0 to 9");
899 return false;
902 if (params->has_compress_threads && (params->compress_threads < 1)) {
903 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
904 "compress_threads",
905 "is invalid, it should be in the range of 1 to 255");
906 return false;
909 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
910 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
911 "decompress_threads",
912 "is invalid, it should be in the range of 1 to 255");
913 return false;
916 if (params->has_cpu_throttle_initial &&
917 (params->cpu_throttle_initial < 1 ||
918 params->cpu_throttle_initial > 99)) {
919 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
920 "cpu_throttle_initial",
921 "an integer in the range of 1 to 99");
922 return false;
925 if (params->has_cpu_throttle_increment &&
926 (params->cpu_throttle_increment < 1 ||
927 params->cpu_throttle_increment > 99)) {
928 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
929 "cpu_throttle_increment",
930 "an integer in the range of 1 to 99");
931 return false;
934 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
935 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
936 " range of 0 to %zu bytes/second", SIZE_MAX);
937 return false;
940 if (params->has_downtime_limit &&
941 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
942 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
943 "the range of 0 to %d milliseconds",
944 MAX_MIGRATE_DOWNTIME);
945 return false;
948 /* x_checkpoint_delay is now always positive */
950 if (params->has_x_multifd_channels && (params->x_multifd_channels < 1)) {
951 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
952 "multifd_channels",
953 "is invalid, it should be in the range of 1 to 255");
954 return false;
956 if (params->has_x_multifd_page_count &&
957 (params->x_multifd_page_count < 1 ||
958 params->x_multifd_page_count > 10000)) {
959 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
960 "multifd_page_count",
961 "is invalid, it should be in the range of 1 to 10000");
962 return false;
965 if (params->has_xbzrle_cache_size &&
966 (params->xbzrle_cache_size < qemu_target_page_size() ||
967 !is_power_of_2(params->xbzrle_cache_size))) {
968 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
969 "xbzrle_cache_size",
970 "is invalid, it should be bigger than target page size"
971 " and a power of two");
972 return false;
975 return true;
978 static void migrate_params_test_apply(MigrateSetParameters *params,
979 MigrationParameters *dest)
981 *dest = migrate_get_current()->parameters;
983 /* TODO use QAPI_CLONE() instead of duplicating it inline */
985 if (params->has_compress_level) {
986 dest->compress_level = params->compress_level;
989 if (params->has_compress_threads) {
990 dest->compress_threads = params->compress_threads;
993 if (params->has_decompress_threads) {
994 dest->decompress_threads = params->decompress_threads;
997 if (params->has_cpu_throttle_initial) {
998 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1001 if (params->has_cpu_throttle_increment) {
1002 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1005 if (params->has_tls_creds) {
1006 assert(params->tls_creds->type == QTYPE_QSTRING);
1007 dest->tls_creds = g_strdup(params->tls_creds->u.s);
1010 if (params->has_tls_hostname) {
1011 assert(params->tls_hostname->type == QTYPE_QSTRING);
1012 dest->tls_hostname = g_strdup(params->tls_hostname->u.s);
1015 if (params->has_max_bandwidth) {
1016 dest->max_bandwidth = params->max_bandwidth;
1019 if (params->has_downtime_limit) {
1020 dest->downtime_limit = params->downtime_limit;
1023 if (params->has_x_checkpoint_delay) {
1024 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1027 if (params->has_block_incremental) {
1028 dest->block_incremental = params->block_incremental;
1030 if (params->has_x_multifd_channels) {
1031 dest->x_multifd_channels = params->x_multifd_channels;
1033 if (params->has_x_multifd_page_count) {
1034 dest->x_multifd_page_count = params->x_multifd_page_count;
1036 if (params->has_xbzrle_cache_size) {
1037 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1041 static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
1043 MigrationState *s = migrate_get_current();
1045 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1047 if (params->has_compress_level) {
1048 s->parameters.compress_level = params->compress_level;
1051 if (params->has_compress_threads) {
1052 s->parameters.compress_threads = params->compress_threads;
1055 if (params->has_decompress_threads) {
1056 s->parameters.decompress_threads = params->decompress_threads;
1059 if (params->has_cpu_throttle_initial) {
1060 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
1063 if (params->has_cpu_throttle_increment) {
1064 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
1067 if (params->has_tls_creds) {
1068 g_free(s->parameters.tls_creds);
1069 assert(params->tls_creds->type == QTYPE_QSTRING);
1070 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
1073 if (params->has_tls_hostname) {
1074 g_free(s->parameters.tls_hostname);
1075 assert(params->tls_hostname->type == QTYPE_QSTRING);
1076 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
1079 if (params->has_max_bandwidth) {
1080 s->parameters.max_bandwidth = params->max_bandwidth;
1081 if (s->to_dst_file) {
1082 qemu_file_set_rate_limit(s->to_dst_file,
1083 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1087 if (params->has_downtime_limit) {
1088 s->parameters.downtime_limit = params->downtime_limit;
1091 if (params->has_x_checkpoint_delay) {
1092 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
1093 if (migration_in_colo_state()) {
1094 colo_checkpoint_notify(s);
1098 if (params->has_block_incremental) {
1099 s->parameters.block_incremental = params->block_incremental;
1101 if (params->has_x_multifd_channels) {
1102 s->parameters.x_multifd_channels = params->x_multifd_channels;
1104 if (params->has_x_multifd_page_count) {
1105 s->parameters.x_multifd_page_count = params->x_multifd_page_count;
1107 if (params->has_xbzrle_cache_size) {
1108 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1109 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1113 void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
1115 MigrationParameters tmp;
1117 /* TODO Rewrite "" to null instead */
1118 if (params->has_tls_creds
1119 && params->tls_creds->type == QTYPE_QNULL) {
1120 qobject_unref(params->tls_creds->u.n);
1121 params->tls_creds->type = QTYPE_QSTRING;
1122 params->tls_creds->u.s = strdup("");
1124 /* TODO Rewrite "" to null instead */
1125 if (params->has_tls_hostname
1126 && params->tls_hostname->type == QTYPE_QNULL) {
1127 qobject_unref(params->tls_hostname->u.n);
1128 params->tls_hostname->type = QTYPE_QSTRING;
1129 params->tls_hostname->u.s = strdup("");
1132 migrate_params_test_apply(params, &tmp);
1134 if (!migrate_params_check(&tmp, errp)) {
1135 /* Invalid parameter */
1136 return;
1139 migrate_params_apply(params, errp);
1143 void qmp_migrate_start_postcopy(Error **errp)
1145 MigrationState *s = migrate_get_current();
1147 if (!migrate_postcopy()) {
1148 error_setg(errp, "Enable postcopy with migrate_set_capability before"
1149 " the start of migration");
1150 return;
1153 if (s->state == MIGRATION_STATUS_NONE) {
1154 error_setg(errp, "Postcopy must be started after migration has been"
1155 " started");
1156 return;
1159 * we don't error if migration has finished since that would be racy
1160 * with issuing this command.
1162 atomic_set(&s->start_postcopy, true);
1165 /* shared migration helpers */
1167 void migrate_set_state(int *state, int old_state, int new_state)
1169 assert(new_state < MIGRATION_STATUS__MAX);
1170 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
1171 trace_migrate_set_state(MigrationStatus_str(new_state));
1172 migrate_generate_event(new_state);
1176 static MigrationCapabilityStatusList *migrate_cap_add(
1177 MigrationCapabilityStatusList *list,
1178 MigrationCapability index,
1179 bool state)
1181 MigrationCapabilityStatusList *cap;
1183 cap = g_new0(MigrationCapabilityStatusList, 1);
1184 cap->value = g_new0(MigrationCapabilityStatus, 1);
1185 cap->value->capability = index;
1186 cap->value->state = state;
1187 cap->next = list;
1189 return cap;
1192 void migrate_set_block_enabled(bool value, Error **errp)
1194 MigrationCapabilityStatusList *cap;
1196 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
1197 qmp_migrate_set_capabilities(cap, errp);
1198 qapi_free_MigrationCapabilityStatusList(cap);
1201 static void migrate_set_block_incremental(MigrationState *s, bool value)
1203 s->parameters.block_incremental = value;
1206 static void block_cleanup_parameters(MigrationState *s)
1208 if (s->must_remove_block_options) {
1209 /* setting to false can never fail */
1210 migrate_set_block_enabled(false, &error_abort);
1211 migrate_set_block_incremental(s, false);
1212 s->must_remove_block_options = false;
1216 static void migrate_fd_cleanup(void *opaque)
1218 MigrationState *s = opaque;
1220 qemu_bh_delete(s->cleanup_bh);
1221 s->cleanup_bh = NULL;
1223 qemu_savevm_state_cleanup();
1225 if (s->to_dst_file) {
1226 Error *local_err = NULL;
1228 trace_migrate_fd_cleanup();
1229 qemu_mutex_unlock_iothread();
1230 if (s->migration_thread_running) {
1231 qemu_thread_join(&s->thread);
1232 s->migration_thread_running = false;
1234 qemu_mutex_lock_iothread();
1236 if (multifd_save_cleanup(&local_err) != 0) {
1237 error_report_err(local_err);
1239 qemu_fclose(s->to_dst_file);
1240 s->to_dst_file = NULL;
1243 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
1244 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
1246 if (s->state == MIGRATION_STATUS_CANCELLING) {
1247 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
1248 MIGRATION_STATUS_CANCELLED);
1251 if (s->error) {
1252 /* It is used on info migrate. We can't free it */
1253 error_report_err(error_copy(s->error));
1255 notifier_list_notify(&migration_state_notifiers, s);
1256 block_cleanup_parameters(s);
1259 void migrate_set_error(MigrationState *s, const Error *error)
1261 qemu_mutex_lock(&s->error_mutex);
1262 if (!s->error) {
1263 s->error = error_copy(error);
1265 qemu_mutex_unlock(&s->error_mutex);
1268 void migrate_fd_error(MigrationState *s, const Error *error)
1270 trace_migrate_fd_error(error_get_pretty(error));
1271 assert(s->to_dst_file == NULL);
1272 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1273 MIGRATION_STATUS_FAILED);
1274 migrate_set_error(s, error);
1277 static void migrate_fd_cancel(MigrationState *s)
1279 int old_state ;
1280 QEMUFile *f = migrate_get_current()->to_dst_file;
1281 trace_migrate_fd_cancel();
1283 if (s->rp_state.from_dst_file) {
1284 /* shutdown the rp socket, so causing the rp thread to shutdown */
1285 qemu_file_shutdown(s->rp_state.from_dst_file);
1288 do {
1289 old_state = s->state;
1290 if (!migration_is_setup_or_active(old_state)) {
1291 break;
1293 /* If the migration is paused, kick it out of the pause */
1294 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1295 qemu_sem_post(&s->pause_sem);
1297 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
1298 } while (s->state != MIGRATION_STATUS_CANCELLING);
1301 * If we're unlucky the migration code might be stuck somewhere in a
1302 * send/write while the network has failed and is waiting to timeout;
1303 * if we've got shutdown(2) available then we can force it to quit.
1304 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1305 * called in a bh, so there is no race against this cancel.
1307 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
1308 qemu_file_shutdown(f);
1310 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1311 Error *local_err = NULL;
1313 bdrv_invalidate_cache_all(&local_err);
1314 if (local_err) {
1315 error_report_err(local_err);
1316 } else {
1317 s->block_inactive = false;
1322 void add_migration_state_change_notifier(Notifier *notify)
1324 notifier_list_add(&migration_state_notifiers, notify);
1327 void remove_migration_state_change_notifier(Notifier *notify)
1329 notifier_remove(notify);
1332 bool migration_in_setup(MigrationState *s)
1334 return s->state == MIGRATION_STATUS_SETUP;
1337 bool migration_has_finished(MigrationState *s)
1339 return s->state == MIGRATION_STATUS_COMPLETED;
1342 bool migration_has_failed(MigrationState *s)
1344 return (s->state == MIGRATION_STATUS_CANCELLED ||
1345 s->state == MIGRATION_STATUS_FAILED);
1348 bool migration_in_postcopy(void)
1350 MigrationState *s = migrate_get_current();
1352 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1355 bool migration_in_postcopy_after_devices(MigrationState *s)
1357 return migration_in_postcopy() && s->postcopy_after_devices;
1360 bool migration_is_idle(void)
1362 MigrationState *s = migrate_get_current();
1364 switch (s->state) {
1365 case MIGRATION_STATUS_NONE:
1366 case MIGRATION_STATUS_CANCELLED:
1367 case MIGRATION_STATUS_COMPLETED:
1368 case MIGRATION_STATUS_FAILED:
1369 return true;
1370 case MIGRATION_STATUS_SETUP:
1371 case MIGRATION_STATUS_CANCELLING:
1372 case MIGRATION_STATUS_ACTIVE:
1373 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1374 case MIGRATION_STATUS_COLO:
1375 case MIGRATION_STATUS_PRE_SWITCHOVER:
1376 case MIGRATION_STATUS_DEVICE:
1377 return false;
1378 case MIGRATION_STATUS__MAX:
1379 g_assert_not_reached();
1382 return false;
1385 void migrate_init(MigrationState *s)
1388 * Reinitialise all migration state, except
1389 * parameters/capabilities that the user set, and
1390 * locks.
1392 s->bytes_xfer = 0;
1393 s->xfer_limit = 0;
1394 s->cleanup_bh = 0;
1395 s->to_dst_file = NULL;
1396 s->state = MIGRATION_STATUS_NONE;
1397 s->rp_state.from_dst_file = NULL;
1398 s->rp_state.error = false;
1399 s->mbps = 0.0;
1400 s->downtime = 0;
1401 s->expected_downtime = 0;
1402 s->setup_time = 0;
1403 s->start_postcopy = false;
1404 s->postcopy_after_devices = false;
1405 s->migration_thread_running = false;
1406 error_free(s->error);
1407 s->error = NULL;
1409 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
1411 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1412 s->total_time = 0;
1413 s->vm_was_running = false;
1414 s->iteration_initial_bytes = 0;
1415 s->threshold_size = 0;
1418 static GSList *migration_blockers;
1420 int migrate_add_blocker(Error *reason, Error **errp)
1422 if (migrate_get_current()->only_migratable) {
1423 error_propagate(errp, error_copy(reason));
1424 error_prepend(errp, "disallowing migration blocker "
1425 "(--only_migratable) for: ");
1426 return -EACCES;
1429 if (migration_is_idle()) {
1430 migration_blockers = g_slist_prepend(migration_blockers, reason);
1431 return 0;
1434 error_propagate(errp, error_copy(reason));
1435 error_prepend(errp, "disallowing migration blocker (migration in "
1436 "progress) for: ");
1437 return -EBUSY;
1440 void migrate_del_blocker(Error *reason)
1442 migration_blockers = g_slist_remove(migration_blockers, reason);
1445 void qmp_migrate_incoming(const char *uri, Error **errp)
1447 Error *local_err = NULL;
1448 static bool once = true;
1450 if (!deferred_incoming) {
1451 error_setg(errp, "For use with '-incoming defer'");
1452 return;
1454 if (!once) {
1455 error_setg(errp, "The incoming migration has already been started");
1458 qemu_start_incoming_migration(uri, &local_err);
1460 if (local_err) {
1461 error_propagate(errp, local_err);
1462 return;
1465 once = false;
1468 bool migration_is_blocked(Error **errp)
1470 if (qemu_savevm_state_blocked(errp)) {
1471 return true;
1474 if (migration_blockers) {
1475 error_propagate(errp, error_copy(migration_blockers->data));
1476 return true;
1479 return false;
1482 /* Returns true if continue to migrate, or false if error detected */
1483 static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
1484 bool resume, Error **errp)
1486 Error *local_err = NULL;
1488 if (resume) {
1489 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1490 error_setg(errp, "Cannot resume if there is no "
1491 "paused migration");
1492 return false;
1494 /* This is a resume, skip init status */
1495 return true;
1498 if (migration_is_setup_or_active(s->state) ||
1499 s->state == MIGRATION_STATUS_CANCELLING ||
1500 s->state == MIGRATION_STATUS_COLO) {
1501 error_setg(errp, QERR_MIGRATION_ACTIVE);
1502 return false;
1505 if (runstate_check(RUN_STATE_INMIGRATE)) {
1506 error_setg(errp, "Guest is waiting for an incoming migration");
1507 return false;
1510 if (migration_is_blocked(errp)) {
1511 return false;
1514 if (blk || blk_inc) {
1515 if (migrate_use_block() || migrate_use_block_incremental()) {
1516 error_setg(errp, "Command options are incompatible with "
1517 "current migration capabilities");
1518 return false;
1520 migrate_set_block_enabled(true, &local_err);
1521 if (local_err) {
1522 error_propagate(errp, local_err);
1523 return false;
1525 s->must_remove_block_options = true;
1528 if (blk_inc) {
1529 migrate_set_block_incremental(s, true);
1532 migrate_init(s);
1534 return true;
1537 void qmp_migrate(const char *uri, bool has_blk, bool blk,
1538 bool has_inc, bool inc, bool has_detach, bool detach,
1539 bool has_resume, bool resume, Error **errp)
1541 Error *local_err = NULL;
1542 MigrationState *s = migrate_get_current();
1543 const char *p;
1545 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
1546 has_resume && resume, errp)) {
1547 /* Error detected, put into errp */
1548 return;
1551 if (strstart(uri, "tcp:", &p)) {
1552 tcp_start_outgoing_migration(s, p, &local_err);
1553 #ifdef CONFIG_RDMA
1554 } else if (strstart(uri, "rdma:", &p)) {
1555 rdma_start_outgoing_migration(s, p, &local_err);
1556 #endif
1557 } else if (strstart(uri, "exec:", &p)) {
1558 exec_start_outgoing_migration(s, p, &local_err);
1559 } else if (strstart(uri, "unix:", &p)) {
1560 unix_start_outgoing_migration(s, p, &local_err);
1561 } else if (strstart(uri, "fd:", &p)) {
1562 fd_start_outgoing_migration(s, p, &local_err);
1563 } else {
1564 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1565 "a valid migration protocol");
1566 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1567 MIGRATION_STATUS_FAILED);
1568 block_cleanup_parameters(s);
1569 return;
1572 if (local_err) {
1573 migrate_fd_error(s, local_err);
1574 error_propagate(errp, local_err);
1575 return;
1579 void qmp_migrate_cancel(Error **errp)
1581 migrate_fd_cancel(migrate_get_current());
1584 void qmp_migrate_continue(MigrationStatus state, Error **errp)
1586 MigrationState *s = migrate_get_current();
1587 if (s->state != state) {
1588 error_setg(errp, "Migration not in expected state: %s",
1589 MigrationStatus_str(s->state));
1590 return;
1592 qemu_sem_post(&s->pause_sem);
1595 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1597 MigrateSetParameters p = {
1598 .has_xbzrle_cache_size = true,
1599 .xbzrle_cache_size = value,
1602 qmp_migrate_set_parameters(&p, errp);
1605 int64_t qmp_query_migrate_cache_size(Error **errp)
1607 return migrate_xbzrle_cache_size();
1610 void qmp_migrate_set_speed(int64_t value, Error **errp)
1612 MigrateSetParameters p = {
1613 .has_max_bandwidth = true,
1614 .max_bandwidth = value,
1617 qmp_migrate_set_parameters(&p, errp);
1620 void qmp_migrate_set_downtime(double value, Error **errp)
1622 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1623 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1624 "the range of 0 to %d seconds",
1625 MAX_MIGRATE_DOWNTIME_SECONDS);
1626 return;
1629 value *= 1000; /* Convert to milliseconds */
1630 value = MAX(0, MIN(INT64_MAX, value));
1632 MigrateSetParameters p = {
1633 .has_downtime_limit = true,
1634 .downtime_limit = value,
1637 qmp_migrate_set_parameters(&p, errp);
1640 bool migrate_release_ram(void)
1642 MigrationState *s;
1644 s = migrate_get_current();
1646 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1649 bool migrate_postcopy_ram(void)
1651 MigrationState *s;
1653 s = migrate_get_current();
1655 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
1658 bool migrate_postcopy(void)
1660 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
1663 bool migrate_auto_converge(void)
1665 MigrationState *s;
1667 s = migrate_get_current();
1669 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1672 bool migrate_zero_blocks(void)
1674 MigrationState *s;
1676 s = migrate_get_current();
1678 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1681 bool migrate_postcopy_blocktime(void)
1683 MigrationState *s;
1685 s = migrate_get_current();
1687 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
1690 bool migrate_use_compression(void)
1692 MigrationState *s;
1694 s = migrate_get_current();
1696 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
1699 int migrate_compress_level(void)
1701 MigrationState *s;
1703 s = migrate_get_current();
1705 return s->parameters.compress_level;
1708 int migrate_compress_threads(void)
1710 MigrationState *s;
1712 s = migrate_get_current();
1714 return s->parameters.compress_threads;
1717 int migrate_decompress_threads(void)
1719 MigrationState *s;
1721 s = migrate_get_current();
1723 return s->parameters.decompress_threads;
1726 bool migrate_dirty_bitmaps(void)
1728 MigrationState *s;
1730 s = migrate_get_current();
1732 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
1735 bool migrate_use_events(void)
1737 MigrationState *s;
1739 s = migrate_get_current();
1741 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1744 bool migrate_use_multifd(void)
1746 MigrationState *s;
1748 s = migrate_get_current();
1750 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_MULTIFD];
1753 bool migrate_pause_before_switchover(void)
1755 MigrationState *s;
1757 s = migrate_get_current();
1759 return s->enabled_capabilities[
1760 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
1763 int migrate_multifd_channels(void)
1765 MigrationState *s;
1767 s = migrate_get_current();
1769 return s->parameters.x_multifd_channels;
1772 int migrate_multifd_page_count(void)
1774 MigrationState *s;
1776 s = migrate_get_current();
1778 return s->parameters.x_multifd_page_count;
1781 int migrate_use_xbzrle(void)
1783 MigrationState *s;
1785 s = migrate_get_current();
1787 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1790 int64_t migrate_xbzrle_cache_size(void)
1792 MigrationState *s;
1794 s = migrate_get_current();
1796 return s->parameters.xbzrle_cache_size;
1799 bool migrate_use_block(void)
1801 MigrationState *s;
1803 s = migrate_get_current();
1805 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1808 bool migrate_use_return_path(void)
1810 MigrationState *s;
1812 s = migrate_get_current();
1814 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
1817 bool migrate_use_block_incremental(void)
1819 MigrationState *s;
1821 s = migrate_get_current();
1823 return s->parameters.block_incremental;
1826 /* migration thread support */
1828 * Something bad happened to the RP stream, mark an error
1829 * The caller shall print or trace something to indicate why
1831 static void mark_source_rp_bad(MigrationState *s)
1833 s->rp_state.error = true;
1836 static struct rp_cmd_args {
1837 ssize_t len; /* -1 = variable */
1838 const char *name;
1839 } rp_cmd_args[] = {
1840 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1841 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1842 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
1843 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1844 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
1845 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
1846 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1850 * Process a request for pages received on the return path,
1851 * We're allowed to send more than requested (e.g. to round to our page size)
1852 * and we don't need to send pages that have already been sent.
1854 static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1855 ram_addr_t start, size_t len)
1857 long our_host_ps = getpagesize();
1859 trace_migrate_handle_rp_req_pages(rbname, start, len);
1862 * Since we currently insist on matching page sizes, just sanity check
1863 * we're being asked for whole host pages.
1865 if (start & (our_host_ps-1) ||
1866 (len & (our_host_ps-1))) {
1867 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1868 " len: %zd", __func__, start, len);
1869 mark_source_rp_bad(ms);
1870 return;
1873 if (ram_save_queue_pages(rbname, start, len)) {
1874 mark_source_rp_bad(ms);
1878 /* Return true to retry, false to quit */
1879 static bool postcopy_pause_return_path_thread(MigrationState *s)
1881 trace_postcopy_pause_return_path();
1883 qemu_sem_wait(&s->postcopy_pause_rp_sem);
1885 trace_postcopy_pause_return_path_continued();
1887 return true;
1890 static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
1892 RAMBlock *block = qemu_ram_block_by_name(block_name);
1894 if (!block) {
1895 error_report("%s: invalid block name '%s'", __func__, block_name);
1896 return -EINVAL;
1899 /* Fetch the received bitmap and refresh the dirty bitmap */
1900 return ram_dirty_bitmap_reload(s, block);
1904 * Handles messages sent on the return path towards the source VM
1907 static void *source_return_path_thread(void *opaque)
1909 MigrationState *ms = opaque;
1910 QEMUFile *rp = ms->rp_state.from_dst_file;
1911 uint16_t header_len, header_type;
1912 uint8_t buf[512];
1913 uint32_t tmp32, sibling_error;
1914 ram_addr_t start = 0; /* =0 to silence warning */
1915 size_t len = 0, expected_len;
1916 int res;
1918 trace_source_return_path_thread_entry();
1920 retry:
1921 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1922 migration_is_setup_or_active(ms->state)) {
1923 trace_source_return_path_thread_loop_top();
1924 header_type = qemu_get_be16(rp);
1925 header_len = qemu_get_be16(rp);
1927 if (qemu_file_get_error(rp)) {
1928 mark_source_rp_bad(ms);
1929 goto out;
1932 if (header_type >= MIG_RP_MSG_MAX ||
1933 header_type == MIG_RP_MSG_INVALID) {
1934 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1935 header_type, header_len);
1936 mark_source_rp_bad(ms);
1937 goto out;
1940 if ((rp_cmd_args[header_type].len != -1 &&
1941 header_len != rp_cmd_args[header_type].len) ||
1942 header_len > sizeof(buf)) {
1943 error_report("RP: Received '%s' message (0x%04x) with"
1944 "incorrect length %d expecting %zu",
1945 rp_cmd_args[header_type].name, header_type, header_len,
1946 (size_t)rp_cmd_args[header_type].len);
1947 mark_source_rp_bad(ms);
1948 goto out;
1951 /* We know we've got a valid header by this point */
1952 res = qemu_get_buffer(rp, buf, header_len);
1953 if (res != header_len) {
1954 error_report("RP: Failed reading data for message 0x%04x"
1955 " read %d expected %d",
1956 header_type, res, header_len);
1957 mark_source_rp_bad(ms);
1958 goto out;
1961 /* OK, we have the message and the data */
1962 switch (header_type) {
1963 case MIG_RP_MSG_SHUT:
1964 sibling_error = ldl_be_p(buf);
1965 trace_source_return_path_thread_shut(sibling_error);
1966 if (sibling_error) {
1967 error_report("RP: Sibling indicated error %d", sibling_error);
1968 mark_source_rp_bad(ms);
1971 * We'll let the main thread deal with closing the RP
1972 * we could do a shutdown(2) on it, but we're the only user
1973 * anyway, so there's nothing gained.
1975 goto out;
1977 case MIG_RP_MSG_PONG:
1978 tmp32 = ldl_be_p(buf);
1979 trace_source_return_path_thread_pong(tmp32);
1980 break;
1982 case MIG_RP_MSG_REQ_PAGES:
1983 start = ldq_be_p(buf);
1984 len = ldl_be_p(buf + 8);
1985 migrate_handle_rp_req_pages(ms, NULL, start, len);
1986 break;
1988 case MIG_RP_MSG_REQ_PAGES_ID:
1989 expected_len = 12 + 1; /* header + termination */
1991 if (header_len >= expected_len) {
1992 start = ldq_be_p(buf);
1993 len = ldl_be_p(buf + 8);
1994 /* Now we expect an idstr */
1995 tmp32 = buf[12]; /* Length of the following idstr */
1996 buf[13 + tmp32] = '\0';
1997 expected_len += tmp32;
1999 if (header_len != expected_len) {
2000 error_report("RP: Req_Page_id with length %d expecting %zd",
2001 header_len, expected_len);
2002 mark_source_rp_bad(ms);
2003 goto out;
2005 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2006 break;
2008 case MIG_RP_MSG_RECV_BITMAP:
2009 if (header_len < 1) {
2010 error_report("%s: missing block name", __func__);
2011 mark_source_rp_bad(ms);
2012 goto out;
2014 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2015 buf[buf[0] + 1] = '\0';
2016 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2017 mark_source_rp_bad(ms);
2018 goto out;
2020 break;
2022 default:
2023 break;
2027 out:
2028 res = qemu_file_get_error(rp);
2029 if (res) {
2030 if (res == -EIO) {
2032 * Maybe there is something we can do: it looks like a
2033 * network down issue, and we pause for a recovery.
2035 if (postcopy_pause_return_path_thread(ms)) {
2036 /* Reload rp, reset the rest */
2037 rp = ms->rp_state.from_dst_file;
2038 ms->rp_state.error = false;
2039 goto retry;
2043 trace_source_return_path_thread_bad_end();
2044 mark_source_rp_bad(ms);
2047 trace_source_return_path_thread_end();
2048 ms->rp_state.from_dst_file = NULL;
2049 qemu_fclose(rp);
2050 return NULL;
2053 static int open_return_path_on_source(MigrationState *ms,
2054 bool create_thread)
2057 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
2058 if (!ms->rp_state.from_dst_file) {
2059 return -1;
2062 trace_open_return_path_on_source();
2064 if (!create_thread) {
2065 /* We're done */
2066 return 0;
2069 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2070 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2072 trace_open_return_path_on_source_continue();
2074 return 0;
2077 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2078 static int await_return_path_close_on_source(MigrationState *ms)
2081 * If this is a normal exit then the destination will send a SHUT and the
2082 * rp_thread will exit, however if there's an error we need to cause
2083 * it to exit.
2085 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
2087 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2088 * waiting for the destination.
2090 qemu_file_shutdown(ms->rp_state.from_dst_file);
2091 mark_source_rp_bad(ms);
2093 trace_await_return_path_close_on_source_joining();
2094 qemu_thread_join(&ms->rp_state.rp_thread);
2095 trace_await_return_path_close_on_source_close();
2096 return ms->rp_state.error;
2100 * Switch from normal iteration to postcopy
2101 * Returns non-0 on error
2103 static int postcopy_start(MigrationState *ms)
2105 int ret;
2106 QIOChannelBuffer *bioc;
2107 QEMUFile *fb;
2108 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2109 bool restart_block = false;
2110 int cur_state = MIGRATION_STATUS_ACTIVE;
2111 if (!migrate_pause_before_switchover()) {
2112 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2113 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2116 trace_postcopy_start();
2117 qemu_mutex_lock_iothread();
2118 trace_postcopy_start_set_run();
2120 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
2121 global_state_store();
2122 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
2123 if (ret < 0) {
2124 goto fail;
2127 ret = migration_maybe_pause(ms, &cur_state,
2128 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2129 if (ret < 0) {
2130 goto fail;
2133 ret = bdrv_inactivate_all();
2134 if (ret < 0) {
2135 goto fail;
2137 restart_block = true;
2140 * Cause any non-postcopiable, but iterative devices to
2141 * send out their final data.
2143 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
2146 * in Finish migrate and with the io-lock held everything should
2147 * be quiet, but we've potentially still got dirty pages and we
2148 * need to tell the destination to throw any pages it's already received
2149 * that are dirty
2151 if (migrate_postcopy_ram()) {
2152 if (ram_postcopy_send_discard_bitmap(ms)) {
2153 error_report("postcopy send discard bitmap failed");
2154 goto fail;
2159 * send rest of state - note things that are doing postcopy
2160 * will notice we're in POSTCOPY_ACTIVE and not actually
2161 * wrap their state up here
2163 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
2164 if (migrate_postcopy_ram()) {
2165 /* Ping just for debugging, helps line traces up */
2166 qemu_savevm_send_ping(ms->to_dst_file, 2);
2170 * While loading the device state we may trigger page transfer
2171 * requests and the fd must be free to process those, and thus
2172 * the destination must read the whole device state off the fd before
2173 * it starts processing it. Unfortunately the ad-hoc migration format
2174 * doesn't allow the destination to know the size to read without fully
2175 * parsing it through each devices load-state code (especially the open
2176 * coded devices that use get/put).
2177 * So we wrap the device state up in a package with a length at the start;
2178 * to do this we use a qemu_buf to hold the whole of the device state.
2180 bioc = qio_channel_buffer_new(4096);
2181 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
2182 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2183 object_unref(OBJECT(bioc));
2186 * Make sure the receiver can get incoming pages before we send the rest
2187 * of the state
2189 qemu_savevm_send_postcopy_listen(fb);
2191 qemu_savevm_state_complete_precopy(fb, false, false);
2192 if (migrate_postcopy_ram()) {
2193 qemu_savevm_send_ping(fb, 3);
2196 qemu_savevm_send_postcopy_run(fb);
2198 /* <><> end of stuff going into the package */
2200 /* Last point of recovery; as soon as we send the package the destination
2201 * can open devices and potentially start running.
2202 * Lets just check again we've not got any errors.
2204 ret = qemu_file_get_error(ms->to_dst_file);
2205 if (ret) {
2206 error_report("postcopy_start: Migration stream errored (pre package)");
2207 goto fail_closefb;
2210 restart_block = false;
2212 /* Now send that blob */
2213 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
2214 goto fail_closefb;
2216 qemu_fclose(fb);
2218 /* Send a notify to give a chance for anything that needs to happen
2219 * at the transition to postcopy and after the device state; in particular
2220 * spice needs to trigger a transition now
2222 ms->postcopy_after_devices = true;
2223 notifier_list_notify(&migration_state_notifiers, ms);
2225 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2227 qemu_mutex_unlock_iothread();
2229 if (migrate_postcopy_ram()) {
2231 * Although this ping is just for debug, it could potentially be
2232 * used for getting a better measurement of downtime at the source.
2234 qemu_savevm_send_ping(ms->to_dst_file, 4);
2237 if (migrate_release_ram()) {
2238 ram_postcopy_migrated_memory_release(ms);
2241 ret = qemu_file_get_error(ms->to_dst_file);
2242 if (ret) {
2243 error_report("postcopy_start: Migration stream errored");
2244 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2245 MIGRATION_STATUS_FAILED);
2248 return ret;
2250 fail_closefb:
2251 qemu_fclose(fb);
2252 fail:
2253 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2254 MIGRATION_STATUS_FAILED);
2255 if (restart_block) {
2256 /* A failure happened early enough that we know the destination hasn't
2257 * accessed block devices, so we're safe to recover.
2259 Error *local_err = NULL;
2261 bdrv_invalidate_cache_all(&local_err);
2262 if (local_err) {
2263 error_report_err(local_err);
2266 qemu_mutex_unlock_iothread();
2267 return -1;
2271 * migration_maybe_pause: Pause if required to by
2272 * migrate_pause_before_switchover called with the iothread locked
2273 * Returns: 0 on success
2275 static int migration_maybe_pause(MigrationState *s,
2276 int *current_active_state,
2277 int new_state)
2279 if (!migrate_pause_before_switchover()) {
2280 return 0;
2283 /* Since leaving this state is not atomic with posting the semaphore
2284 * it's possible that someone could have issued multiple migrate_continue
2285 * and the semaphore is incorrectly positive at this point;
2286 * the docs say it's undefined to reinit a semaphore that's already
2287 * init'd, so use timedwait to eat up any existing posts.
2289 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2290 /* This block intentionally left blank */
2293 qemu_mutex_unlock_iothread();
2294 migrate_set_state(&s->state, *current_active_state,
2295 MIGRATION_STATUS_PRE_SWITCHOVER);
2296 qemu_sem_wait(&s->pause_sem);
2297 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
2298 new_state);
2299 *current_active_state = new_state;
2300 qemu_mutex_lock_iothread();
2302 return s->state == new_state ? 0 : -EINVAL;
2306 * migration_completion: Used by migration_thread when there's not much left.
2307 * The caller 'breaks' the loop when this returns.
2309 * @s: Current migration state
2311 static void migration_completion(MigrationState *s)
2313 int ret;
2314 int current_active_state = s->state;
2316 if (s->state == MIGRATION_STATUS_ACTIVE) {
2317 qemu_mutex_lock_iothread();
2318 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2319 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
2320 s->vm_was_running = runstate_is_running();
2321 ret = global_state_store();
2323 if (!ret) {
2324 bool inactivate = !migrate_colo_enabled();
2325 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
2326 if (ret >= 0) {
2327 ret = migration_maybe_pause(s, &current_active_state,
2328 MIGRATION_STATUS_DEVICE);
2330 if (ret >= 0) {
2331 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
2332 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2333 inactivate);
2335 if (inactivate && ret >= 0) {
2336 s->block_inactive = true;
2339 qemu_mutex_unlock_iothread();
2341 if (ret < 0) {
2342 goto fail;
2344 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2345 trace_migration_completion_postcopy_end();
2347 qemu_savevm_state_complete_postcopy(s->to_dst_file);
2348 trace_migration_completion_postcopy_end_after_complete();
2352 * If rp was opened we must clean up the thread before
2353 * cleaning everything else up (since if there are no failures
2354 * it will wait for the destination to send it's status in
2355 * a SHUT command).
2357 if (s->rp_state.from_dst_file) {
2358 int rp_error;
2359 trace_migration_return_path_end_before();
2360 rp_error = await_return_path_close_on_source(s);
2361 trace_migration_return_path_end_after(rp_error);
2362 if (rp_error) {
2363 goto fail_invalidate;
2367 if (qemu_file_get_error(s->to_dst_file)) {
2368 trace_migration_completion_file_err();
2369 goto fail_invalidate;
2372 if (!migrate_colo_enabled()) {
2373 migrate_set_state(&s->state, current_active_state,
2374 MIGRATION_STATUS_COMPLETED);
2377 return;
2379 fail_invalidate:
2380 /* If not doing postcopy, vm_start() will be called: let's regain
2381 * control on images.
2383 if (s->state == MIGRATION_STATUS_ACTIVE ||
2384 s->state == MIGRATION_STATUS_DEVICE) {
2385 Error *local_err = NULL;
2387 qemu_mutex_lock_iothread();
2388 bdrv_invalidate_cache_all(&local_err);
2389 if (local_err) {
2390 error_report_err(local_err);
2391 } else {
2392 s->block_inactive = false;
2394 qemu_mutex_unlock_iothread();
2397 fail:
2398 migrate_set_state(&s->state, current_active_state,
2399 MIGRATION_STATUS_FAILED);
2402 bool migrate_colo_enabled(void)
2404 MigrationState *s = migrate_get_current();
2405 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
2408 typedef enum MigThrError {
2409 /* No error detected */
2410 MIG_THR_ERR_NONE = 0,
2411 /* Detected error, but resumed successfully */
2412 MIG_THR_ERR_RECOVERED = 1,
2413 /* Detected fatal error, need to exit */
2414 MIG_THR_ERR_FATAL = 2,
2415 } MigThrError;
2417 /* Return zero if success, or <0 for error */
2418 static int postcopy_do_resume(MigrationState *s)
2420 /* TODO: do the resume logic */
2421 return 0;
2425 * We don't return until we are in a safe state to continue current
2426 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
2427 * MIG_THR_ERR_FATAL if unrecovery failure happened.
2429 static MigThrError postcopy_pause(MigrationState *s)
2431 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2433 while (true) {
2434 migrate_set_state(&s->state, s->state,
2435 MIGRATION_STATUS_POSTCOPY_PAUSED);
2437 /* Current channel is possibly broken. Release it. */
2438 assert(s->to_dst_file);
2439 qemu_file_shutdown(s->to_dst_file);
2440 qemu_fclose(s->to_dst_file);
2441 s->to_dst_file = NULL;
2443 error_report("Detected IO failure for postcopy. "
2444 "Migration paused.");
2447 * We wait until things fixed up. Then someone will setup the
2448 * status back for us.
2450 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2451 qemu_sem_wait(&s->postcopy_pause_sem);
2454 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
2455 /* Woken up by a recover procedure. Give it a shot */
2458 * Firstly, let's wake up the return path now, with a new
2459 * return path channel.
2461 qemu_sem_post(&s->postcopy_pause_rp_sem);
2463 /* Do the resume logic */
2464 if (postcopy_do_resume(s) == 0) {
2465 /* Let's continue! */
2466 trace_postcopy_pause_continued();
2467 return MIG_THR_ERR_RECOVERED;
2468 } else {
2470 * Something wrong happened during the recovery, let's
2471 * pause again. Pause is always better than throwing
2472 * data away.
2474 continue;
2476 } else {
2477 /* This is not right... Time to quit. */
2478 return MIG_THR_ERR_FATAL;
2483 static MigThrError migration_detect_error(MigrationState *s)
2485 int ret;
2487 /* Try to detect any file errors */
2488 ret = qemu_file_get_error(s->to_dst_file);
2490 if (!ret) {
2491 /* Everything is fine */
2492 return MIG_THR_ERR_NONE;
2495 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
2497 * For postcopy, we allow the network to be down for a
2498 * while. After that, it can be continued by a
2499 * recovery phase.
2501 return postcopy_pause(s);
2502 } else {
2504 * For precopy (or postcopy with error outside IO), we fail
2505 * with no time.
2507 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
2508 trace_migration_thread_file_err();
2510 /* Time to stop the migration, now. */
2511 return MIG_THR_ERR_FATAL;
2515 static void migration_calculate_complete(MigrationState *s)
2517 uint64_t bytes = qemu_ftell(s->to_dst_file);
2518 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2520 s->total_time = end_time - s->start_time;
2521 if (!s->downtime) {
2523 * It's still not set, so we are precopy migration. For
2524 * postcopy, downtime is calculated during postcopy_start().
2526 s->downtime = end_time - s->downtime_start;
2529 if (s->total_time) {
2530 s->mbps = ((double) bytes * 8.0) / s->total_time / 1000;
2534 static void migration_update_counters(MigrationState *s,
2535 int64_t current_time)
2537 uint64_t transferred, time_spent;
2538 double bandwidth;
2540 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
2541 return;
2544 transferred = qemu_ftell(s->to_dst_file) - s->iteration_initial_bytes;
2545 time_spent = current_time - s->iteration_start_time;
2546 bandwidth = (double)transferred / time_spent;
2547 s->threshold_size = bandwidth * s->parameters.downtime_limit;
2549 s->mbps = (((double) transferred * 8.0) /
2550 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
2553 * if we haven't sent anything, we don't want to
2554 * recalculate. 10000 is a small enough number for our purposes
2556 if (ram_counters.dirty_pages_rate && transferred > 10000) {
2557 s->expected_downtime = ram_counters.dirty_pages_rate *
2558 qemu_target_page_size() / bandwidth;
2561 qemu_file_reset_rate_limit(s->to_dst_file);
2563 s->iteration_start_time = current_time;
2564 s->iteration_initial_bytes = qemu_ftell(s->to_dst_file);
2566 trace_migrate_transferred(transferred, time_spent,
2567 bandwidth, s->threshold_size);
2570 /* Migration thread iteration status */
2571 typedef enum {
2572 MIG_ITERATE_RESUME, /* Resume current iteration */
2573 MIG_ITERATE_SKIP, /* Skip current iteration */
2574 MIG_ITERATE_BREAK, /* Break the loop */
2575 } MigIterateState;
2578 * Return true if continue to the next iteration directly, false
2579 * otherwise.
2581 static MigIterateState migration_iteration_run(MigrationState *s)
2583 uint64_t pending_size, pend_pre, pend_compat, pend_post;
2584 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
2586 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
2587 &pend_compat, &pend_post);
2588 pending_size = pend_pre + pend_compat + pend_post;
2590 trace_migrate_pending(pending_size, s->threshold_size,
2591 pend_pre, pend_compat, pend_post);
2593 if (pending_size && pending_size >= s->threshold_size) {
2594 /* Still a significant amount to transfer */
2595 if (migrate_postcopy() && !in_postcopy &&
2596 pend_pre <= s->threshold_size &&
2597 atomic_read(&s->start_postcopy)) {
2598 if (postcopy_start(s)) {
2599 error_report("%s: postcopy failed to start", __func__);
2601 return MIG_ITERATE_SKIP;
2603 /* Just another iteration step */
2604 qemu_savevm_state_iterate(s->to_dst_file,
2605 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2606 } else {
2607 trace_migration_thread_low_pending(pending_size);
2608 migration_completion(s);
2609 return MIG_ITERATE_BREAK;
2612 return MIG_ITERATE_RESUME;
2615 static void migration_iteration_finish(MigrationState *s)
2617 /* If we enabled cpu throttling for auto-converge, turn it off. */
2618 cpu_throttle_stop();
2620 qemu_mutex_lock_iothread();
2621 switch (s->state) {
2622 case MIGRATION_STATUS_COMPLETED:
2623 migration_calculate_complete(s);
2624 runstate_set(RUN_STATE_POSTMIGRATE);
2625 break;
2627 case MIGRATION_STATUS_ACTIVE:
2629 * We should really assert here, but since it's during
2630 * migration, let's try to reduce the usage of assertions.
2632 if (!migrate_colo_enabled()) {
2633 error_report("%s: critical error: calling COLO code without "
2634 "COLO enabled", __func__);
2636 migrate_start_colo_process(s);
2638 * Fixme: we will run VM in COLO no matter its old running state.
2639 * After exited COLO, we will keep running.
2641 s->vm_was_running = true;
2642 /* Fallthrough */
2643 case MIGRATION_STATUS_FAILED:
2644 case MIGRATION_STATUS_CANCELLED:
2645 if (s->vm_was_running) {
2646 vm_start();
2647 } else {
2648 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2649 runstate_set(RUN_STATE_POSTMIGRATE);
2652 break;
2654 default:
2655 /* Should not reach here, but if so, forgive the VM. */
2656 error_report("%s: Unknown ending state %d", __func__, s->state);
2657 break;
2659 qemu_bh_schedule(s->cleanup_bh);
2660 qemu_mutex_unlock_iothread();
2664 * Master migration thread on the source VM.
2665 * It drives the migration and pumps the data down the outgoing channel.
2667 static void *migration_thread(void *opaque)
2669 MigrationState *s = opaque;
2670 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
2671 MigThrError thr_error;
2673 rcu_register_thread();
2675 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2677 qemu_savevm_state_header(s->to_dst_file);
2680 * If we opened the return path, we need to make sure dst has it
2681 * opened as well.
2683 if (s->rp_state.from_dst_file) {
2684 /* Now tell the dest that it should open its end so it can reply */
2685 qemu_savevm_send_open_return_path(s->to_dst_file);
2687 /* And do a ping that will make stuff easier to debug */
2688 qemu_savevm_send_ping(s->to_dst_file, 1);
2691 if (migrate_postcopy()) {
2693 * Tell the destination that we *might* want to do postcopy later;
2694 * if the other end can't do postcopy it should fail now, nice and
2695 * early.
2697 qemu_savevm_send_postcopy_advise(s->to_dst_file);
2700 qemu_savevm_state_setup(s->to_dst_file);
2702 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
2703 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2704 MIGRATION_STATUS_ACTIVE);
2706 trace_migration_thread_setup_complete();
2708 while (s->state == MIGRATION_STATUS_ACTIVE ||
2709 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2710 int64_t current_time;
2712 if (!qemu_file_rate_limit(s->to_dst_file)) {
2713 MigIterateState iter_state = migration_iteration_run(s);
2714 if (iter_state == MIG_ITERATE_SKIP) {
2715 continue;
2716 } else if (iter_state == MIG_ITERATE_BREAK) {
2717 break;
2722 * Try to detect any kind of failures, and see whether we
2723 * should stop the migration now.
2725 thr_error = migration_detect_error(s);
2726 if (thr_error == MIG_THR_ERR_FATAL) {
2727 /* Stop migration */
2728 break;
2729 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
2731 * Just recovered from a e.g. network failure, reset all
2732 * the local variables. This is important to avoid
2733 * breaking transferred_bytes and bandwidth calculation
2735 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2736 s->iteration_initial_bytes = 0;
2739 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2741 migration_update_counters(s, current_time);
2743 if (qemu_file_rate_limit(s->to_dst_file)) {
2744 /* usleep expects microseconds */
2745 g_usleep((s->iteration_start_time + BUFFER_DELAY -
2746 current_time) * 1000);
2750 trace_migration_thread_after_loop();
2751 migration_iteration_finish(s);
2752 rcu_unregister_thread();
2753 return NULL;
2756 void migrate_fd_connect(MigrationState *s, Error *error_in)
2758 int64_t rate_limit;
2759 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
2761 s->expected_downtime = s->parameters.downtime_limit;
2762 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
2763 if (error_in) {
2764 migrate_fd_error(s, error_in);
2765 migrate_fd_cleanup(s);
2766 return;
2769 if (resume) {
2770 /* This is a resumed migration */
2771 rate_limit = INT64_MAX;
2772 } else {
2773 /* This is a fresh new migration */
2774 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
2775 s->expected_downtime = s->parameters.downtime_limit;
2776 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
2778 /* Notify before starting migration thread */
2779 notifier_list_notify(&migration_state_notifiers, s);
2782 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
2783 qemu_file_set_blocking(s->to_dst_file, true);
2786 * Open the return path. For postcopy, it is used exclusively. For
2787 * precopy, only if user specified "return-path" capability would
2788 * QEMU uses the return path.
2790 if (migrate_postcopy_ram() || migrate_use_return_path()) {
2791 if (open_return_path_on_source(s, !resume)) {
2792 error_report("Unable to open return-path for postcopy");
2793 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
2794 migrate_fd_cleanup(s);
2795 return;
2799 if (resume) {
2800 /* Wakeup the main migration thread to do the recovery */
2801 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
2802 MIGRATION_STATUS_POSTCOPY_RECOVER);
2803 qemu_sem_post(&s->postcopy_pause_sem);
2804 return;
2807 if (multifd_save_setup() != 0) {
2808 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2809 MIGRATION_STATUS_FAILED);
2810 migrate_fd_cleanup(s);
2811 return;
2813 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
2814 QEMU_THREAD_JOINABLE);
2815 s->migration_thread_running = true;
2818 void migration_global_dump(Monitor *mon)
2820 MigrationState *ms = migrate_get_current();
2822 monitor_printf(mon, "globals:\n");
2823 monitor_printf(mon, "store-global-state: %s\n",
2824 ms->store_global_state ? "on" : "off");
2825 monitor_printf(mon, "only-migratable: %s\n",
2826 ms->only_migratable ? "on" : "off");
2827 monitor_printf(mon, "send-configuration: %s\n",
2828 ms->send_configuration ? "on" : "off");
2829 monitor_printf(mon, "send-section-footer: %s\n",
2830 ms->send_section_footer ? "on" : "off");
2833 #define DEFINE_PROP_MIG_CAP(name, x) \
2834 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
2836 static Property migration_properties[] = {
2837 DEFINE_PROP_BOOL("store-global-state", MigrationState,
2838 store_global_state, true),
2839 DEFINE_PROP_BOOL("only-migratable", MigrationState, only_migratable, false),
2840 DEFINE_PROP_BOOL("send-configuration", MigrationState,
2841 send_configuration, true),
2842 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
2843 send_section_footer, true),
2845 /* Migration parameters */
2846 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
2847 parameters.compress_level,
2848 DEFAULT_MIGRATE_COMPRESS_LEVEL),
2849 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
2850 parameters.compress_threads,
2851 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
2852 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
2853 parameters.decompress_threads,
2854 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
2855 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
2856 parameters.cpu_throttle_initial,
2857 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
2858 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
2859 parameters.cpu_throttle_increment,
2860 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
2861 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
2862 parameters.max_bandwidth, MAX_THROTTLE),
2863 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
2864 parameters.downtime_limit,
2865 DEFAULT_MIGRATE_SET_DOWNTIME),
2866 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
2867 parameters.x_checkpoint_delay,
2868 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
2869 DEFINE_PROP_UINT8("x-multifd-channels", MigrationState,
2870 parameters.x_multifd_channels,
2871 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
2872 DEFINE_PROP_UINT32("x-multifd-page-count", MigrationState,
2873 parameters.x_multifd_page_count,
2874 DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT),
2875 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
2876 parameters.xbzrle_cache_size,
2877 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
2879 /* Migration capabilities */
2880 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
2881 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
2882 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
2883 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
2884 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
2885 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
2886 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
2887 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
2888 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
2889 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
2890 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
2891 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_X_MULTIFD),
2893 DEFINE_PROP_END_OF_LIST(),
2896 static void migration_class_init(ObjectClass *klass, void *data)
2898 DeviceClass *dc = DEVICE_CLASS(klass);
2900 dc->user_creatable = false;
2901 dc->props = migration_properties;
2904 static void migration_instance_finalize(Object *obj)
2906 MigrationState *ms = MIGRATION_OBJ(obj);
2907 MigrationParameters *params = &ms->parameters;
2909 qemu_mutex_destroy(&ms->error_mutex);
2910 g_free(params->tls_hostname);
2911 g_free(params->tls_creds);
2912 qemu_sem_destroy(&ms->pause_sem);
2913 qemu_sem_destroy(&ms->postcopy_pause_sem);
2914 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
2915 error_free(ms->error);
2918 static void migration_instance_init(Object *obj)
2920 MigrationState *ms = MIGRATION_OBJ(obj);
2921 MigrationParameters *params = &ms->parameters;
2923 ms->state = MIGRATION_STATUS_NONE;
2924 ms->mbps = -1;
2925 qemu_sem_init(&ms->pause_sem, 0);
2926 qemu_mutex_init(&ms->error_mutex);
2928 params->tls_hostname = g_strdup("");
2929 params->tls_creds = g_strdup("");
2931 /* Set has_* up only for parameter checks */
2932 params->has_compress_level = true;
2933 params->has_compress_threads = true;
2934 params->has_decompress_threads = true;
2935 params->has_cpu_throttle_initial = true;
2936 params->has_cpu_throttle_increment = true;
2937 params->has_max_bandwidth = true;
2938 params->has_downtime_limit = true;
2939 params->has_x_checkpoint_delay = true;
2940 params->has_block_incremental = true;
2941 params->has_x_multifd_channels = true;
2942 params->has_x_multifd_page_count = true;
2943 params->has_xbzrle_cache_size = true;
2945 qemu_sem_init(&ms->postcopy_pause_sem, 0);
2946 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
2950 * Return true if check pass, false otherwise. Error will be put
2951 * inside errp if provided.
2953 static bool migration_object_check(MigrationState *ms, Error **errp)
2955 MigrationCapabilityStatusList *head = NULL;
2956 /* Assuming all off */
2957 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
2958 int i;
2960 if (!migrate_params_check(&ms->parameters, errp)) {
2961 return false;
2964 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
2965 if (ms->enabled_capabilities[i]) {
2966 head = migrate_cap_add(head, i, true);
2970 ret = migrate_caps_check(cap_list, head, errp);
2972 /* It works with head == NULL */
2973 qapi_free_MigrationCapabilityStatusList(head);
2975 return ret;
2978 static const TypeInfo migration_type = {
2979 .name = TYPE_MIGRATION,
2981 * NOTE: TYPE_MIGRATION is not really a device, as the object is
2982 * not created using qdev_create(), it is not attached to the qdev
2983 * device tree, and it is never realized.
2985 * TODO: Make this TYPE_OBJECT once QOM provides something like
2986 * TYPE_DEVICE's "-global" properties.
2988 .parent = TYPE_DEVICE,
2989 .class_init = migration_class_init,
2990 .class_size = sizeof(MigrationClass),
2991 .instance_size = sizeof(MigrationState),
2992 .instance_init = migration_instance_init,
2993 .instance_finalize = migration_instance_finalize,
2996 static void register_migration_types(void)
2998 type_register_static(&migration_type);
3001 type_init(register_migration_types);