multifd: Add multifd-compression parameter
[qemu/kevin.git] / migration / migration.c
blobbc744d1734dd779df47719147e70d1ceb95b5803
1 /*
2 * QEMU live migration
4 * Copyright IBM, Corp. 2008
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu/osdep.h"
17 #include "qemu/cutils.h"
18 #include "qemu/error-report.h"
19 #include "qemu/main-loop.h"
20 #include "migration/blocker.h"
21 #include "exec.h"
22 #include "fd.h"
23 #include "socket.h"
24 #include "sysemu/runstate.h"
25 #include "sysemu/sysemu.h"
26 #include "rdma.h"
27 #include "ram.h"
28 #include "migration/global_state.h"
29 #include "migration/misc.h"
30 #include "migration.h"
31 #include "savevm.h"
32 #include "qemu-file-channel.h"
33 #include "qemu-file.h"
34 #include "migration/vmstate.h"
35 #include "block/block.h"
36 #include "qapi/error.h"
37 #include "qapi/clone-visitor.h"
38 #include "qapi/qapi-visit-sockets.h"
39 #include "qapi/qapi-commands-migration.h"
40 #include "qapi/qapi-events-migration.h"
41 #include "qapi/qmp/qerror.h"
42 #include "qapi/qmp/qnull.h"
43 #include "qemu/rcu.h"
44 #include "block.h"
45 #include "postcopy-ram.h"
46 #include "qemu/thread.h"
47 #include "trace.h"
48 #include "exec/target_page.h"
49 #include "io/channel-buffer.h"
50 #include "migration/colo.h"
51 #include "hw/boards.h"
52 #include "hw/qdev-properties.h"
53 #include "monitor/monitor.h"
54 #include "net/announce.h"
55 #include "qemu/queue.h"
56 #include "multifd.h"
58 #define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
60 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
61 * data. */
62 #define BUFFER_DELAY 100
63 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
65 /* Time in milliseconds we are allowed to stop the source,
66 * for sending the last part */
67 #define DEFAULT_MIGRATE_SET_DOWNTIME 300
69 /* Maximum migrate downtime set to 2000 seconds */
70 #define MAX_MIGRATE_DOWNTIME_SECONDS 2000
71 #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
73 /* Default compression thread count */
74 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
75 /* Default decompression thread count, usually decompression is at
76 * least 4 times as fast as compression.*/
77 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
78 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
79 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
80 /* Define default autoconverge cpu throttle migration parameters */
81 #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
82 #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
83 #define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
85 /* Migration XBZRLE default cache size */
86 #define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
88 /* The delay time (in ms) between two COLO checkpoints */
89 #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
90 #define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
91 #define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
93 /* Background transfer rate for postcopy, 0 means unlimited, note
94 * that page requests can still exceed this limit.
96 #define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
99 * Parameters for self_announce_delay giving a stream of RARP/ARP
100 * packets after migration.
102 #define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50
103 #define DEFAULT_MIGRATE_ANNOUNCE_MAX 550
104 #define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5
105 #define DEFAULT_MIGRATE_ANNOUNCE_STEP 100
107 static NotifierList migration_state_notifiers =
108 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
110 static bool deferred_incoming;
112 /* Messages sent on the return path from destination to source */
113 enum mig_rp_message_type {
114 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
115 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
116 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
118 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
119 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
120 MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */
121 MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */
123 MIG_RP_MSG_MAX
126 /* When we add fault tolerance, we could have several
127 migrations at once. For now we don't need to add
128 dynamic creation of migration */
130 static MigrationState *current_migration;
131 static MigrationIncomingState *current_incoming;
133 static bool migration_object_check(MigrationState *ms, Error **errp);
134 static int migration_maybe_pause(MigrationState *s,
135 int *current_active_state,
136 int new_state);
137 static void migrate_fd_cancel(MigrationState *s);
139 void migration_object_init(void)
141 MachineState *ms = MACHINE(qdev_get_machine());
142 Error *err = NULL;
144 /* This can only be called once. */
145 assert(!current_migration);
146 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
149 * Init the migrate incoming object as well no matter whether
150 * we'll use it or not.
152 assert(!current_incoming);
153 current_incoming = g_new0(MigrationIncomingState, 1);
154 current_incoming->state = MIGRATION_STATUS_NONE;
155 current_incoming->postcopy_remote_fds =
156 g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD));
157 qemu_mutex_init(&current_incoming->rp_mutex);
158 qemu_event_init(&current_incoming->main_thread_load_event, false);
159 qemu_sem_init(&current_incoming->postcopy_pause_sem_dst, 0);
160 qemu_sem_init(&current_incoming->postcopy_pause_sem_fault, 0);
162 init_dirty_bitmap_incoming_migration();
164 if (!migration_object_check(current_migration, &err)) {
165 error_report_err(err);
166 exit(1);
170 * We cannot really do this in migration_instance_init() since at
171 * that time global properties are not yet applied, then this
172 * value will be definitely replaced by something else.
174 if (ms->enforce_config_section) {
175 current_migration->send_configuration = true;
179 void migration_shutdown(void)
182 * Cancel the current migration - that will (eventually)
183 * stop the migration using this structure
185 migrate_fd_cancel(current_migration);
186 object_unref(OBJECT(current_migration));
189 /* For outgoing */
190 MigrationState *migrate_get_current(void)
192 /* This can only be called after the object created. */
193 assert(current_migration);
194 return current_migration;
197 MigrationIncomingState *migration_incoming_get_current(void)
199 assert(current_incoming);
200 return current_incoming;
203 void migration_incoming_state_destroy(void)
205 struct MigrationIncomingState *mis = migration_incoming_get_current();
207 if (mis->to_src_file) {
208 /* Tell source that we are done */
209 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
210 qemu_fclose(mis->to_src_file);
211 mis->to_src_file = NULL;
214 if (mis->from_src_file) {
215 qemu_fclose(mis->from_src_file);
216 mis->from_src_file = NULL;
218 if (mis->postcopy_remote_fds) {
219 g_array_free(mis->postcopy_remote_fds, TRUE);
220 mis->postcopy_remote_fds = NULL;
223 qemu_event_reset(&mis->main_thread_load_event);
225 if (mis->socket_address_list) {
226 qapi_free_SocketAddressList(mis->socket_address_list);
227 mis->socket_address_list = NULL;
231 static void migrate_generate_event(int new_state)
233 if (migrate_use_events()) {
234 qapi_event_send_migration(new_state);
238 static bool migrate_late_block_activate(void)
240 MigrationState *s;
242 s = migrate_get_current();
244 return s->enabled_capabilities[
245 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
249 * Called on -incoming with a defer: uri.
250 * The migration can be started later after any parameters have been
251 * changed.
253 static void deferred_incoming_migration(Error **errp)
255 if (deferred_incoming) {
256 error_setg(errp, "Incoming migration already deferred");
258 deferred_incoming = true;
262 * Send a message on the return channel back to the source
263 * of the migration.
265 static int migrate_send_rp_message(MigrationIncomingState *mis,
266 enum mig_rp_message_type message_type,
267 uint16_t len, void *data)
269 int ret = 0;
271 trace_migrate_send_rp_message((int)message_type, len);
272 qemu_mutex_lock(&mis->rp_mutex);
275 * It's possible that the file handle got lost due to network
276 * failures.
278 if (!mis->to_src_file) {
279 ret = -EIO;
280 goto error;
283 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
284 qemu_put_be16(mis->to_src_file, len);
285 qemu_put_buffer(mis->to_src_file, data, len);
286 qemu_fflush(mis->to_src_file);
288 /* It's possible that qemu file got error during sending */
289 ret = qemu_file_get_error(mis->to_src_file);
291 error:
292 qemu_mutex_unlock(&mis->rp_mutex);
293 return ret;
296 /* Request a range of pages from the source VM at the given
297 * start address.
298 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
299 * as the last request (a name must have been given previously)
300 * Start: Address offset within the RB
301 * Len: Length in bytes required - must be a multiple of pagesize
303 int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
304 ram_addr_t start, size_t len)
306 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
307 size_t msglen = 12; /* start + len */
308 enum mig_rp_message_type msg_type;
310 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
311 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
313 if (rbname) {
314 int rbname_len = strlen(rbname);
315 assert(rbname_len < 256);
317 bufc[msglen++] = rbname_len;
318 memcpy(bufc + msglen, rbname, rbname_len);
319 msglen += rbname_len;
320 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
321 } else {
322 msg_type = MIG_RP_MSG_REQ_PAGES;
325 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
328 static bool migration_colo_enabled;
329 bool migration_incoming_colo_enabled(void)
331 return migration_colo_enabled;
334 void migration_incoming_disable_colo(void)
336 migration_colo_enabled = false;
339 void migration_incoming_enable_colo(void)
341 migration_colo_enabled = true;
344 void migrate_add_address(SocketAddress *address)
346 MigrationIncomingState *mis = migration_incoming_get_current();
347 SocketAddressList *addrs;
349 addrs = g_new0(SocketAddressList, 1);
350 addrs->next = mis->socket_address_list;
351 mis->socket_address_list = addrs;
352 addrs->value = QAPI_CLONE(SocketAddress, address);
355 void qemu_start_incoming_migration(const char *uri, Error **errp)
357 const char *p;
359 qapi_event_send_migration(MIGRATION_STATUS_SETUP);
360 if (!strcmp(uri, "defer")) {
361 deferred_incoming_migration(errp);
362 } else if (strstart(uri, "tcp:", &p)) {
363 tcp_start_incoming_migration(p, errp);
364 #ifdef CONFIG_RDMA
365 } else if (strstart(uri, "rdma:", &p)) {
366 rdma_start_incoming_migration(p, errp);
367 #endif
368 } else if (strstart(uri, "exec:", &p)) {
369 exec_start_incoming_migration(p, errp);
370 } else if (strstart(uri, "unix:", &p)) {
371 unix_start_incoming_migration(p, errp);
372 } else if (strstart(uri, "fd:", &p)) {
373 fd_start_incoming_migration(p, errp);
374 } else {
375 error_setg(errp, "unknown migration protocol: %s", uri);
379 static void process_incoming_migration_bh(void *opaque)
381 Error *local_err = NULL;
382 MigrationIncomingState *mis = opaque;
384 /* If capability late_block_activate is set:
385 * Only fire up the block code now if we're going to restart the
386 * VM, else 'cont' will do it.
387 * This causes file locking to happen; so we don't want it to happen
388 * unless we really are starting the VM.
390 if (!migrate_late_block_activate() ||
391 (autostart && (!global_state_received() ||
392 global_state_get_runstate() == RUN_STATE_RUNNING))) {
393 /* Make sure all file formats flush their mutable metadata.
394 * If we get an error here, just don't restart the VM yet. */
395 bdrv_invalidate_cache_all(&local_err);
396 if (local_err) {
397 error_report_err(local_err);
398 local_err = NULL;
399 autostart = false;
404 * This must happen after all error conditions are dealt with and
405 * we're sure the VM is going to be running on this host.
407 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
409 if (multifd_load_cleanup(&local_err) != 0) {
410 error_report_err(local_err);
411 autostart = false;
413 /* If global state section was not received or we are in running
414 state, we need to obey autostart. Any other state is set with
415 runstate_set. */
417 dirty_bitmap_mig_before_vm_start();
419 if (!global_state_received() ||
420 global_state_get_runstate() == RUN_STATE_RUNNING) {
421 if (autostart) {
422 vm_start();
423 } else {
424 runstate_set(RUN_STATE_PAUSED);
426 } else if (migration_incoming_colo_enabled()) {
427 migration_incoming_disable_colo();
428 vm_start();
429 } else {
430 runstate_set(global_state_get_runstate());
433 * This must happen after any state changes since as soon as an external
434 * observer sees this event they might start to prod at the VM assuming
435 * it's ready to use.
437 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
438 MIGRATION_STATUS_COMPLETED);
439 qemu_bh_delete(mis->bh);
440 migration_incoming_state_destroy();
443 static void process_incoming_migration_co(void *opaque)
445 MigrationIncomingState *mis = migration_incoming_get_current();
446 PostcopyState ps;
447 int ret;
448 Error *local_err = NULL;
450 assert(mis->from_src_file);
451 mis->migration_incoming_co = qemu_coroutine_self();
452 mis->largest_page_size = qemu_ram_pagesize_largest();
453 postcopy_state_set(POSTCOPY_INCOMING_NONE);
454 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
455 MIGRATION_STATUS_ACTIVE);
456 ret = qemu_loadvm_state(mis->from_src_file);
458 ps = postcopy_state_get();
459 trace_process_incoming_migration_co_end(ret, ps);
460 if (ps != POSTCOPY_INCOMING_NONE) {
461 if (ps == POSTCOPY_INCOMING_ADVISE) {
463 * Where a migration had postcopy enabled (and thus went to advise)
464 * but managed to complete within the precopy period, we can use
465 * the normal exit.
467 postcopy_ram_incoming_cleanup(mis);
468 } else if (ret >= 0) {
470 * Postcopy was started, cleanup should happen at the end of the
471 * postcopy thread.
473 trace_process_incoming_migration_co_postcopy_end_main();
474 return;
476 /* Else if something went wrong then just fall out of the normal exit */
479 /* we get COLO info, and know if we are in COLO mode */
480 if (!ret && migration_incoming_colo_enabled()) {
481 /* Make sure all file formats flush their mutable metadata */
482 bdrv_invalidate_cache_all(&local_err);
483 if (local_err) {
484 error_report_err(local_err);
485 goto fail;
488 if (colo_init_ram_cache() < 0) {
489 error_report("Init ram cache failed");
490 goto fail;
493 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
494 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
495 mis->have_colo_incoming_thread = true;
496 qemu_coroutine_yield();
498 /* Wait checkpoint incoming thread exit before free resource */
499 qemu_thread_join(&mis->colo_incoming_thread);
500 /* We hold the global iothread lock, so it is safe here */
501 colo_release_ram_cache();
504 if (ret < 0) {
505 error_report("load of migration failed: %s", strerror(-ret));
506 goto fail;
508 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
509 qemu_bh_schedule(mis->bh);
510 mis->migration_incoming_co = NULL;
511 return;
512 fail:
513 local_err = NULL;
514 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
515 MIGRATION_STATUS_FAILED);
516 qemu_fclose(mis->from_src_file);
517 if (multifd_load_cleanup(&local_err) != 0) {
518 error_report_err(local_err);
520 exit(EXIT_FAILURE);
524 * @migration_incoming_setup: Setup incoming migration
526 * Returns 0 for no error or 1 for error
528 * @f: file for main migration channel
529 * @errp: where to put errors
531 static int migration_incoming_setup(QEMUFile *f, Error **errp)
533 MigrationIncomingState *mis = migration_incoming_get_current();
534 Error *local_err = NULL;
536 if (multifd_load_setup(&local_err) != 0) {
537 /* We haven't been able to create multifd threads
538 nothing better to do */
539 error_report_err(local_err);
540 exit(EXIT_FAILURE);
543 if (!mis->from_src_file) {
544 mis->from_src_file = f;
546 qemu_file_set_blocking(f, false);
547 return 0;
550 void migration_incoming_process(void)
552 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
553 qemu_coroutine_enter(co);
556 /* Returns true if recovered from a paused migration, otherwise false */
557 static bool postcopy_try_recover(QEMUFile *f)
559 MigrationIncomingState *mis = migration_incoming_get_current();
561 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
562 /* Resumed from a paused postcopy migration */
564 mis->from_src_file = f;
565 /* Postcopy has standalone thread to do vm load */
566 qemu_file_set_blocking(f, true);
568 /* Re-configure the return path */
569 mis->to_src_file = qemu_file_get_return_path(f);
571 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
572 MIGRATION_STATUS_POSTCOPY_RECOVER);
575 * Here, we only wake up the main loading thread (while the
576 * fault thread will still be waiting), so that we can receive
577 * commands from source now, and answer it if needed. The
578 * fault thread will be woken up afterwards until we are sure
579 * that source is ready to reply to page requests.
581 qemu_sem_post(&mis->postcopy_pause_sem_dst);
582 return true;
585 return false;
588 void migration_fd_process_incoming(QEMUFile *f, Error **errp)
590 Error *local_err = NULL;
592 if (postcopy_try_recover(f)) {
593 return;
596 if (migration_incoming_setup(f, &local_err)) {
597 if (local_err) {
598 error_propagate(errp, local_err);
600 return;
602 migration_incoming_process();
605 void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp)
607 MigrationIncomingState *mis = migration_incoming_get_current();
608 Error *local_err = NULL;
609 bool start_migration;
611 if (!mis->from_src_file) {
612 /* The first connection (multifd may have multiple) */
613 QEMUFile *f = qemu_fopen_channel_input(ioc);
615 /* If it's a recovery, we're done */
616 if (postcopy_try_recover(f)) {
617 return;
620 if (migration_incoming_setup(f, &local_err)) {
621 if (local_err) {
622 error_propagate(errp, local_err);
624 return;
628 * Common migration only needs one channel, so we can start
629 * right now. Multifd needs more than one channel, we wait.
631 start_migration = !migrate_use_multifd();
632 } else {
633 /* Multiple connections */
634 assert(migrate_use_multifd());
635 start_migration = multifd_recv_new_channel(ioc, &local_err);
636 if (local_err) {
637 error_propagate(errp, local_err);
638 return;
642 if (start_migration) {
643 migration_incoming_process();
648 * @migration_has_all_channels: We have received all channels that we need
650 * Returns true when we have got connections to all the channels that
651 * we need for migration.
653 bool migration_has_all_channels(void)
655 MigrationIncomingState *mis = migration_incoming_get_current();
656 bool all_channels;
658 all_channels = multifd_recv_all_channels_created();
660 return all_channels && mis->from_src_file != NULL;
664 * Send a 'SHUT' message on the return channel with the given value
665 * to indicate that we've finished with the RP. Non-0 value indicates
666 * error.
668 void migrate_send_rp_shut(MigrationIncomingState *mis,
669 uint32_t value)
671 uint32_t buf;
673 buf = cpu_to_be32(value);
674 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
678 * Send a 'PONG' message on the return channel with the given value
679 * (normally in response to a 'PING')
681 void migrate_send_rp_pong(MigrationIncomingState *mis,
682 uint32_t value)
684 uint32_t buf;
686 buf = cpu_to_be32(value);
687 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
690 void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
691 char *block_name)
693 char buf[512];
694 int len;
695 int64_t res;
698 * First, we send the header part. It contains only the len of
699 * idstr, and the idstr itself.
701 len = strlen(block_name);
702 buf[0] = len;
703 memcpy(buf + 1, block_name, len);
705 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
706 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
707 __func__);
708 return;
711 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
714 * Next, we dump the received bitmap to the stream.
716 * TODO: currently we are safe since we are the only one that is
717 * using the to_src_file handle (fault thread is still paused),
718 * and it's ok even not taking the mutex. However the best way is
719 * to take the lock before sending the message header, and release
720 * the lock after sending the bitmap.
722 qemu_mutex_lock(&mis->rp_mutex);
723 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
724 qemu_mutex_unlock(&mis->rp_mutex);
726 trace_migrate_send_rp_recv_bitmap(block_name, res);
729 void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
731 uint32_t buf;
733 buf = cpu_to_be32(value);
734 migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
737 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
739 MigrationCapabilityStatusList *head = NULL;
740 MigrationCapabilityStatusList *caps;
741 MigrationState *s = migrate_get_current();
742 int i;
744 caps = NULL; /* silence compiler warning */
745 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
746 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
747 if (i == MIGRATION_CAPABILITY_BLOCK) {
748 continue;
750 #endif
751 if (head == NULL) {
752 head = g_malloc0(sizeof(*caps));
753 caps = head;
754 } else {
755 caps->next = g_malloc0(sizeof(*caps));
756 caps = caps->next;
758 caps->value =
759 g_malloc(sizeof(*caps->value));
760 caps->value->capability = i;
761 caps->value->state = s->enabled_capabilities[i];
764 return head;
767 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
769 MigrationParameters *params;
770 MigrationState *s = migrate_get_current();
772 /* TODO use QAPI_CLONE() instead of duplicating it inline */
773 params = g_malloc0(sizeof(*params));
774 params->has_compress_level = true;
775 params->compress_level = s->parameters.compress_level;
776 params->has_compress_threads = true;
777 params->compress_threads = s->parameters.compress_threads;
778 params->has_compress_wait_thread = true;
779 params->compress_wait_thread = s->parameters.compress_wait_thread;
780 params->has_decompress_threads = true;
781 params->decompress_threads = s->parameters.decompress_threads;
782 params->has_cpu_throttle_initial = true;
783 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
784 params->has_cpu_throttle_increment = true;
785 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
786 params->has_tls_creds = true;
787 params->tls_creds = g_strdup(s->parameters.tls_creds);
788 params->has_tls_hostname = true;
789 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
790 params->has_tls_authz = true;
791 params->tls_authz = g_strdup(s->parameters.tls_authz);
792 params->has_max_bandwidth = true;
793 params->max_bandwidth = s->parameters.max_bandwidth;
794 params->has_downtime_limit = true;
795 params->downtime_limit = s->parameters.downtime_limit;
796 params->has_x_checkpoint_delay = true;
797 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
798 params->has_block_incremental = true;
799 params->block_incremental = s->parameters.block_incremental;
800 params->has_multifd_channels = true;
801 params->multifd_channels = s->parameters.multifd_channels;
802 params->has_multifd_compression = true;
803 params->multifd_compression = s->parameters.multifd_compression;
804 params->has_xbzrle_cache_size = true;
805 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
806 params->has_max_postcopy_bandwidth = true;
807 params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
808 params->has_max_cpu_throttle = true;
809 params->max_cpu_throttle = s->parameters.max_cpu_throttle;
810 params->has_announce_initial = true;
811 params->announce_initial = s->parameters.announce_initial;
812 params->has_announce_max = true;
813 params->announce_max = s->parameters.announce_max;
814 params->has_announce_rounds = true;
815 params->announce_rounds = s->parameters.announce_rounds;
816 params->has_announce_step = true;
817 params->announce_step = s->parameters.announce_step;
819 return params;
822 AnnounceParameters *migrate_announce_params(void)
824 static AnnounceParameters ap;
826 MigrationState *s = migrate_get_current();
828 ap.initial = s->parameters.announce_initial;
829 ap.max = s->parameters.announce_max;
830 ap.rounds = s->parameters.announce_rounds;
831 ap.step = s->parameters.announce_step;
833 return &ap;
837 * Return true if we're already in the middle of a migration
838 * (i.e. any of the active or setup states)
840 bool migration_is_setup_or_active(int state)
842 switch (state) {
843 case MIGRATION_STATUS_ACTIVE:
844 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
845 case MIGRATION_STATUS_POSTCOPY_PAUSED:
846 case MIGRATION_STATUS_POSTCOPY_RECOVER:
847 case MIGRATION_STATUS_SETUP:
848 case MIGRATION_STATUS_PRE_SWITCHOVER:
849 case MIGRATION_STATUS_DEVICE:
850 case MIGRATION_STATUS_WAIT_UNPLUG:
851 return true;
853 default:
854 return false;
859 bool migration_is_running(int state)
861 switch (state) {
862 case MIGRATION_STATUS_ACTIVE:
863 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
864 case MIGRATION_STATUS_POSTCOPY_PAUSED:
865 case MIGRATION_STATUS_POSTCOPY_RECOVER:
866 case MIGRATION_STATUS_SETUP:
867 case MIGRATION_STATUS_PRE_SWITCHOVER:
868 case MIGRATION_STATUS_DEVICE:
869 case MIGRATION_STATUS_WAIT_UNPLUG:
870 case MIGRATION_STATUS_CANCELLING:
871 case MIGRATION_STATUS_COLO:
872 return true;
874 default:
875 return false;
880 static void populate_time_info(MigrationInfo *info, MigrationState *s)
882 info->has_status = true;
883 info->has_setup_time = true;
884 info->setup_time = s->setup_time;
885 if (s->state == MIGRATION_STATUS_COMPLETED) {
886 info->has_total_time = true;
887 info->total_time = s->total_time;
888 info->has_downtime = true;
889 info->downtime = s->downtime;
890 } else {
891 info->has_total_time = true;
892 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
893 s->start_time;
894 info->has_expected_downtime = true;
895 info->expected_downtime = s->expected_downtime;
899 static void populate_ram_info(MigrationInfo *info, MigrationState *s)
901 info->has_ram = true;
902 info->ram = g_malloc0(sizeof(*info->ram));
903 info->ram->transferred = ram_counters.transferred;
904 info->ram->total = ram_bytes_total();
905 info->ram->duplicate = ram_counters.duplicate;
906 /* legacy value. It is not used anymore */
907 info->ram->skipped = 0;
908 info->ram->normal = ram_counters.normal;
909 info->ram->normal_bytes = ram_counters.normal *
910 qemu_target_page_size();
911 info->ram->mbps = s->mbps;
912 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
913 info->ram->postcopy_requests = ram_counters.postcopy_requests;
914 info->ram->page_size = qemu_target_page_size();
915 info->ram->multifd_bytes = ram_counters.multifd_bytes;
916 info->ram->pages_per_second = s->pages_per_second;
918 if (migrate_use_xbzrle()) {
919 info->has_xbzrle_cache = true;
920 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
921 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
922 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
923 info->xbzrle_cache->pages = xbzrle_counters.pages;
924 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
925 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
926 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
929 if (migrate_use_compression()) {
930 info->has_compression = true;
931 info->compression = g_malloc0(sizeof(*info->compression));
932 info->compression->pages = compression_counters.pages;
933 info->compression->busy = compression_counters.busy;
934 info->compression->busy_rate = compression_counters.busy_rate;
935 info->compression->compressed_size =
936 compression_counters.compressed_size;
937 info->compression->compression_rate =
938 compression_counters.compression_rate;
941 if (cpu_throttle_active()) {
942 info->has_cpu_throttle_percentage = true;
943 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
946 if (s->state != MIGRATION_STATUS_COMPLETED) {
947 info->ram->remaining = ram_bytes_remaining();
948 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
952 static void populate_disk_info(MigrationInfo *info)
954 if (blk_mig_active()) {
955 info->has_disk = true;
956 info->disk = g_malloc0(sizeof(*info->disk));
957 info->disk->transferred = blk_mig_bytes_transferred();
958 info->disk->remaining = blk_mig_bytes_remaining();
959 info->disk->total = blk_mig_bytes_total();
963 static void fill_source_migration_info(MigrationInfo *info)
965 MigrationState *s = migrate_get_current();
967 switch (s->state) {
968 case MIGRATION_STATUS_NONE:
969 /* no migration has happened ever */
970 /* do not overwrite destination migration status */
971 return;
972 break;
973 case MIGRATION_STATUS_SETUP:
974 info->has_status = true;
975 info->has_total_time = false;
976 break;
977 case MIGRATION_STATUS_ACTIVE:
978 case MIGRATION_STATUS_CANCELLING:
979 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
980 case MIGRATION_STATUS_PRE_SWITCHOVER:
981 case MIGRATION_STATUS_DEVICE:
982 case MIGRATION_STATUS_POSTCOPY_PAUSED:
983 case MIGRATION_STATUS_POSTCOPY_RECOVER:
984 /* TODO add some postcopy stats */
985 populate_time_info(info, s);
986 populate_ram_info(info, s);
987 populate_disk_info(info);
988 break;
989 case MIGRATION_STATUS_COLO:
990 info->has_status = true;
991 /* TODO: display COLO specific information (checkpoint info etc.) */
992 break;
993 case MIGRATION_STATUS_COMPLETED:
994 populate_time_info(info, s);
995 populate_ram_info(info, s);
996 break;
997 case MIGRATION_STATUS_FAILED:
998 info->has_status = true;
999 if (s->error) {
1000 info->has_error_desc = true;
1001 info->error_desc = g_strdup(error_get_pretty(s->error));
1003 break;
1004 case MIGRATION_STATUS_CANCELLED:
1005 info->has_status = true;
1006 break;
1007 case MIGRATION_STATUS_WAIT_UNPLUG:
1008 info->has_status = true;
1009 break;
1011 info->status = s->state;
1015 * @migration_caps_check - check capability validity
1017 * @cap_list: old capability list, array of bool
1018 * @params: new capabilities to be applied soon
1019 * @errp: set *errp if the check failed, with reason
1021 * Returns true if check passed, otherwise false.
1023 static bool migrate_caps_check(bool *cap_list,
1024 MigrationCapabilityStatusList *params,
1025 Error **errp)
1027 MigrationCapabilityStatusList *cap;
1028 bool old_postcopy_cap;
1029 MigrationIncomingState *mis = migration_incoming_get_current();
1031 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
1033 for (cap = params; cap; cap = cap->next) {
1034 cap_list[cap->value->capability] = cap->value->state;
1037 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
1038 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
1039 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
1040 "block migration");
1041 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
1042 return false;
1044 #endif
1046 #ifndef CONFIG_REPLICATION
1047 if (cap_list[MIGRATION_CAPABILITY_X_COLO]) {
1048 error_setg(errp, "QEMU compiled without replication module"
1049 " can't enable COLO");
1050 error_append_hint(errp, "Please enable replication before COLO.\n");
1051 return false;
1053 #endif
1055 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
1056 /* This check is reasonably expensive, so only when it's being
1057 * set the first time, also it's only the destination that needs
1058 * special support.
1060 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
1061 !postcopy_ram_supported_by_host(mis)) {
1062 /* postcopy_ram_supported_by_host will have emitted a more
1063 * detailed message
1065 error_setg(errp, "Postcopy is not supported");
1066 return false;
1069 if (cap_list[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
1070 error_setg(errp, "Postcopy is not compatible with ignore-shared");
1071 return false;
1075 return true;
1078 static void fill_destination_migration_info(MigrationInfo *info)
1080 MigrationIncomingState *mis = migration_incoming_get_current();
1082 if (mis->socket_address_list) {
1083 info->has_socket_address = true;
1084 info->socket_address =
1085 QAPI_CLONE(SocketAddressList, mis->socket_address_list);
1088 switch (mis->state) {
1089 case MIGRATION_STATUS_NONE:
1090 return;
1091 break;
1092 case MIGRATION_STATUS_SETUP:
1093 case MIGRATION_STATUS_CANCELLING:
1094 case MIGRATION_STATUS_CANCELLED:
1095 case MIGRATION_STATUS_ACTIVE:
1096 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1097 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1098 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1099 case MIGRATION_STATUS_FAILED:
1100 case MIGRATION_STATUS_COLO:
1101 info->has_status = true;
1102 break;
1103 case MIGRATION_STATUS_COMPLETED:
1104 info->has_status = true;
1105 fill_destination_postcopy_migration_info(info);
1106 break;
1108 info->status = mis->state;
1111 MigrationInfo *qmp_query_migrate(Error **errp)
1113 MigrationInfo *info = g_malloc0(sizeof(*info));
1115 fill_destination_migration_info(info);
1116 fill_source_migration_info(info);
1118 return info;
1121 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
1122 Error **errp)
1124 MigrationState *s = migrate_get_current();
1125 MigrationCapabilityStatusList *cap;
1126 bool cap_list[MIGRATION_CAPABILITY__MAX];
1128 if (migration_is_running(s->state)) {
1129 error_setg(errp, QERR_MIGRATION_ACTIVE);
1130 return;
1133 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
1134 if (!migrate_caps_check(cap_list, params, errp)) {
1135 return;
1138 for (cap = params; cap; cap = cap->next) {
1139 s->enabled_capabilities[cap->value->capability] = cap->value->state;
1144 * Check whether the parameters are valid. Error will be put into errp
1145 * (if provided). Return true if valid, otherwise false.
1147 static bool migrate_params_check(MigrationParameters *params, Error **errp)
1149 if (params->has_compress_level &&
1150 (params->compress_level > 9)) {
1151 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
1152 "is invalid, it should be in the range of 0 to 9");
1153 return false;
1156 if (params->has_compress_threads && (params->compress_threads < 1)) {
1157 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1158 "compress_threads",
1159 "is invalid, it should be in the range of 1 to 255");
1160 return false;
1163 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
1164 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1165 "decompress_threads",
1166 "is invalid, it should be in the range of 1 to 255");
1167 return false;
1170 if (params->has_cpu_throttle_initial &&
1171 (params->cpu_throttle_initial < 1 ||
1172 params->cpu_throttle_initial > 99)) {
1173 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1174 "cpu_throttle_initial",
1175 "an integer in the range of 1 to 99");
1176 return false;
1179 if (params->has_cpu_throttle_increment &&
1180 (params->cpu_throttle_increment < 1 ||
1181 params->cpu_throttle_increment > 99)) {
1182 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1183 "cpu_throttle_increment",
1184 "an integer in the range of 1 to 99");
1185 return false;
1188 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
1189 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
1190 " range of 0 to %zu bytes/second", SIZE_MAX);
1191 return false;
1194 if (params->has_downtime_limit &&
1195 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
1196 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1197 "the range of 0 to %d milliseconds",
1198 MAX_MIGRATE_DOWNTIME);
1199 return false;
1202 /* x_checkpoint_delay is now always positive */
1204 if (params->has_multifd_channels && (params->multifd_channels < 1)) {
1205 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1206 "multifd_channels",
1207 "is invalid, it should be in the range of 1 to 255");
1208 return false;
1211 if (params->has_xbzrle_cache_size &&
1212 (params->xbzrle_cache_size < qemu_target_page_size() ||
1213 !is_power_of_2(params->xbzrle_cache_size))) {
1214 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1215 "xbzrle_cache_size",
1216 "is invalid, it should be bigger than target page size"
1217 " and a power of two");
1218 return false;
1221 if (params->has_max_cpu_throttle &&
1222 (params->max_cpu_throttle < params->cpu_throttle_initial ||
1223 params->max_cpu_throttle > 99)) {
1224 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1225 "max_cpu_throttle",
1226 "an integer in the range of cpu_throttle_initial to 99");
1227 return false;
1230 if (params->has_announce_initial &&
1231 params->announce_initial > 100000) {
1232 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1233 "announce_initial",
1234 "is invalid, it must be less than 100000 ms");
1235 return false;
1237 if (params->has_announce_max &&
1238 params->announce_max > 100000) {
1239 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1240 "announce_max",
1241 "is invalid, it must be less than 100000 ms");
1242 return false;
1244 if (params->has_announce_rounds &&
1245 params->announce_rounds > 1000) {
1246 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1247 "announce_rounds",
1248 "is invalid, it must be in the range of 0 to 1000");
1249 return false;
1251 if (params->has_announce_step &&
1252 (params->announce_step < 1 ||
1253 params->announce_step > 10000)) {
1254 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1255 "announce_step",
1256 "is invalid, it must be in the range of 1 to 10000 ms");
1257 return false;
1259 return true;
1262 static void migrate_params_test_apply(MigrateSetParameters *params,
1263 MigrationParameters *dest)
1265 *dest = migrate_get_current()->parameters;
1267 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1269 if (params->has_compress_level) {
1270 dest->compress_level = params->compress_level;
1273 if (params->has_compress_threads) {
1274 dest->compress_threads = params->compress_threads;
1277 if (params->has_compress_wait_thread) {
1278 dest->compress_wait_thread = params->compress_wait_thread;
1281 if (params->has_decompress_threads) {
1282 dest->decompress_threads = params->decompress_threads;
1285 if (params->has_cpu_throttle_initial) {
1286 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1289 if (params->has_cpu_throttle_increment) {
1290 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1293 if (params->has_tls_creds) {
1294 assert(params->tls_creds->type == QTYPE_QSTRING);
1295 dest->tls_creds = g_strdup(params->tls_creds->u.s);
1298 if (params->has_tls_hostname) {
1299 assert(params->tls_hostname->type == QTYPE_QSTRING);
1300 dest->tls_hostname = g_strdup(params->tls_hostname->u.s);
1303 if (params->has_max_bandwidth) {
1304 dest->max_bandwidth = params->max_bandwidth;
1307 if (params->has_downtime_limit) {
1308 dest->downtime_limit = params->downtime_limit;
1311 if (params->has_x_checkpoint_delay) {
1312 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1315 if (params->has_block_incremental) {
1316 dest->block_incremental = params->block_incremental;
1318 if (params->has_multifd_channels) {
1319 dest->multifd_channels = params->multifd_channels;
1321 if (params->has_multifd_compression) {
1322 dest->multifd_compression = params->multifd_compression;
1324 if (params->has_xbzrle_cache_size) {
1325 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1327 if (params->has_max_postcopy_bandwidth) {
1328 dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1330 if (params->has_max_cpu_throttle) {
1331 dest->max_cpu_throttle = params->max_cpu_throttle;
1333 if (params->has_announce_initial) {
1334 dest->announce_initial = params->announce_initial;
1336 if (params->has_announce_max) {
1337 dest->announce_max = params->announce_max;
1339 if (params->has_announce_rounds) {
1340 dest->announce_rounds = params->announce_rounds;
1342 if (params->has_announce_step) {
1343 dest->announce_step = params->announce_step;
1347 static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
1349 MigrationState *s = migrate_get_current();
1351 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1353 if (params->has_compress_level) {
1354 s->parameters.compress_level = params->compress_level;
1357 if (params->has_compress_threads) {
1358 s->parameters.compress_threads = params->compress_threads;
1361 if (params->has_compress_wait_thread) {
1362 s->parameters.compress_wait_thread = params->compress_wait_thread;
1365 if (params->has_decompress_threads) {
1366 s->parameters.decompress_threads = params->decompress_threads;
1369 if (params->has_cpu_throttle_initial) {
1370 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
1373 if (params->has_cpu_throttle_increment) {
1374 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
1377 if (params->has_tls_creds) {
1378 g_free(s->parameters.tls_creds);
1379 assert(params->tls_creds->type == QTYPE_QSTRING);
1380 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
1383 if (params->has_tls_hostname) {
1384 g_free(s->parameters.tls_hostname);
1385 assert(params->tls_hostname->type == QTYPE_QSTRING);
1386 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
1389 if (params->has_tls_authz) {
1390 g_free(s->parameters.tls_authz);
1391 assert(params->tls_authz->type == QTYPE_QSTRING);
1392 s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1395 if (params->has_max_bandwidth) {
1396 s->parameters.max_bandwidth = params->max_bandwidth;
1397 if (s->to_dst_file && !migration_in_postcopy()) {
1398 qemu_file_set_rate_limit(s->to_dst_file,
1399 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1403 if (params->has_downtime_limit) {
1404 s->parameters.downtime_limit = params->downtime_limit;
1407 if (params->has_x_checkpoint_delay) {
1408 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
1409 if (migration_in_colo_state()) {
1410 colo_checkpoint_notify(s);
1414 if (params->has_block_incremental) {
1415 s->parameters.block_incremental = params->block_incremental;
1417 if (params->has_multifd_channels) {
1418 s->parameters.multifd_channels = params->multifd_channels;
1420 if (params->has_multifd_compression) {
1421 s->parameters.multifd_compression = params->multifd_compression;
1423 if (params->has_xbzrle_cache_size) {
1424 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1425 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1427 if (params->has_max_postcopy_bandwidth) {
1428 s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1429 if (s->to_dst_file && migration_in_postcopy()) {
1430 qemu_file_set_rate_limit(s->to_dst_file,
1431 s->parameters.max_postcopy_bandwidth / XFER_LIMIT_RATIO);
1434 if (params->has_max_cpu_throttle) {
1435 s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1437 if (params->has_announce_initial) {
1438 s->parameters.announce_initial = params->announce_initial;
1440 if (params->has_announce_max) {
1441 s->parameters.announce_max = params->announce_max;
1443 if (params->has_announce_rounds) {
1444 s->parameters.announce_rounds = params->announce_rounds;
1446 if (params->has_announce_step) {
1447 s->parameters.announce_step = params->announce_step;
1451 void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
1453 MigrationParameters tmp;
1455 /* TODO Rewrite "" to null instead */
1456 if (params->has_tls_creds
1457 && params->tls_creds->type == QTYPE_QNULL) {
1458 qobject_unref(params->tls_creds->u.n);
1459 params->tls_creds->type = QTYPE_QSTRING;
1460 params->tls_creds->u.s = strdup("");
1462 /* TODO Rewrite "" to null instead */
1463 if (params->has_tls_hostname
1464 && params->tls_hostname->type == QTYPE_QNULL) {
1465 qobject_unref(params->tls_hostname->u.n);
1466 params->tls_hostname->type = QTYPE_QSTRING;
1467 params->tls_hostname->u.s = strdup("");
1470 migrate_params_test_apply(params, &tmp);
1472 if (!migrate_params_check(&tmp, errp)) {
1473 /* Invalid parameter */
1474 return;
1477 migrate_params_apply(params, errp);
1481 void qmp_migrate_start_postcopy(Error **errp)
1483 MigrationState *s = migrate_get_current();
1485 if (!migrate_postcopy()) {
1486 error_setg(errp, "Enable postcopy with migrate_set_capability before"
1487 " the start of migration");
1488 return;
1491 if (s->state == MIGRATION_STATUS_NONE) {
1492 error_setg(errp, "Postcopy must be started after migration has been"
1493 " started");
1494 return;
1497 * we don't error if migration has finished since that would be racy
1498 * with issuing this command.
1500 atomic_set(&s->start_postcopy, true);
1503 /* shared migration helpers */
1505 void migrate_set_state(int *state, int old_state, int new_state)
1507 assert(new_state < MIGRATION_STATUS__MAX);
1508 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
1509 trace_migrate_set_state(MigrationStatus_str(new_state));
1510 migrate_generate_event(new_state);
1514 static MigrationCapabilityStatusList *migrate_cap_add(
1515 MigrationCapabilityStatusList *list,
1516 MigrationCapability index,
1517 bool state)
1519 MigrationCapabilityStatusList *cap;
1521 cap = g_new0(MigrationCapabilityStatusList, 1);
1522 cap->value = g_new0(MigrationCapabilityStatus, 1);
1523 cap->value->capability = index;
1524 cap->value->state = state;
1525 cap->next = list;
1527 return cap;
1530 void migrate_set_block_enabled(bool value, Error **errp)
1532 MigrationCapabilityStatusList *cap;
1534 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
1535 qmp_migrate_set_capabilities(cap, errp);
1536 qapi_free_MigrationCapabilityStatusList(cap);
1539 static void migrate_set_block_incremental(MigrationState *s, bool value)
1541 s->parameters.block_incremental = value;
1544 static void block_cleanup_parameters(MigrationState *s)
1546 if (s->must_remove_block_options) {
1547 /* setting to false can never fail */
1548 migrate_set_block_enabled(false, &error_abort);
1549 migrate_set_block_incremental(s, false);
1550 s->must_remove_block_options = false;
1554 static void migrate_fd_cleanup(MigrationState *s)
1556 qemu_bh_delete(s->cleanup_bh);
1557 s->cleanup_bh = NULL;
1559 qemu_savevm_state_cleanup();
1561 if (s->to_dst_file) {
1562 QEMUFile *tmp;
1564 trace_migrate_fd_cleanup();
1565 qemu_mutex_unlock_iothread();
1566 if (s->migration_thread_running) {
1567 qemu_thread_join(&s->thread);
1568 s->migration_thread_running = false;
1570 qemu_mutex_lock_iothread();
1572 multifd_save_cleanup();
1573 qemu_mutex_lock(&s->qemu_file_lock);
1574 tmp = s->to_dst_file;
1575 s->to_dst_file = NULL;
1576 qemu_mutex_unlock(&s->qemu_file_lock);
1578 * Close the file handle without the lock to make sure the
1579 * critical section won't block for long.
1581 qemu_fclose(tmp);
1584 assert(!migration_is_active(s));
1586 if (s->state == MIGRATION_STATUS_CANCELLING) {
1587 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
1588 MIGRATION_STATUS_CANCELLED);
1591 if (s->error) {
1592 /* It is used on info migrate. We can't free it */
1593 error_report_err(error_copy(s->error));
1595 notifier_list_notify(&migration_state_notifiers, s);
1596 block_cleanup_parameters(s);
1599 static void migrate_fd_cleanup_schedule(MigrationState *s)
1602 * Ref the state for bh, because it may be called when
1603 * there're already no other refs
1605 object_ref(OBJECT(s));
1606 qemu_bh_schedule(s->cleanup_bh);
1609 static void migrate_fd_cleanup_bh(void *opaque)
1611 MigrationState *s = opaque;
1612 migrate_fd_cleanup(s);
1613 object_unref(OBJECT(s));
1616 void migrate_set_error(MigrationState *s, const Error *error)
1618 qemu_mutex_lock(&s->error_mutex);
1619 if (!s->error) {
1620 s->error = error_copy(error);
1622 qemu_mutex_unlock(&s->error_mutex);
1625 void migrate_fd_error(MigrationState *s, const Error *error)
1627 trace_migrate_fd_error(error_get_pretty(error));
1628 assert(s->to_dst_file == NULL);
1629 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1630 MIGRATION_STATUS_FAILED);
1631 migrate_set_error(s, error);
1634 static void migrate_fd_cancel(MigrationState *s)
1636 int old_state ;
1637 QEMUFile *f = migrate_get_current()->to_dst_file;
1638 trace_migrate_fd_cancel();
1640 if (s->rp_state.from_dst_file) {
1641 /* shutdown the rp socket, so causing the rp thread to shutdown */
1642 qemu_file_shutdown(s->rp_state.from_dst_file);
1645 do {
1646 old_state = s->state;
1647 if (!migration_is_running(old_state)) {
1648 break;
1650 /* If the migration is paused, kick it out of the pause */
1651 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1652 qemu_sem_post(&s->pause_sem);
1654 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
1655 } while (s->state != MIGRATION_STATUS_CANCELLING);
1658 * If we're unlucky the migration code might be stuck somewhere in a
1659 * send/write while the network has failed and is waiting to timeout;
1660 * if we've got shutdown(2) available then we can force it to quit.
1661 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1662 * called in a bh, so there is no race against this cancel.
1664 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
1665 qemu_file_shutdown(f);
1667 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1668 Error *local_err = NULL;
1670 bdrv_invalidate_cache_all(&local_err);
1671 if (local_err) {
1672 error_report_err(local_err);
1673 } else {
1674 s->block_inactive = false;
1679 void add_migration_state_change_notifier(Notifier *notify)
1681 notifier_list_add(&migration_state_notifiers, notify);
1684 void remove_migration_state_change_notifier(Notifier *notify)
1686 notifier_remove(notify);
1689 bool migration_in_setup(MigrationState *s)
1691 return s->state == MIGRATION_STATUS_SETUP;
1694 bool migration_has_finished(MigrationState *s)
1696 return s->state == MIGRATION_STATUS_COMPLETED;
1699 bool migration_has_failed(MigrationState *s)
1701 return (s->state == MIGRATION_STATUS_CANCELLED ||
1702 s->state == MIGRATION_STATUS_FAILED);
1705 bool migration_in_postcopy(void)
1707 MigrationState *s = migrate_get_current();
1709 switch (s->state) {
1710 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1711 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1712 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1713 return true;
1714 default:
1715 return false;
1719 bool migration_in_postcopy_after_devices(MigrationState *s)
1721 return migration_in_postcopy() && s->postcopy_after_devices;
1724 bool migration_is_idle(void)
1726 MigrationState *s = current_migration;
1728 if (!s) {
1729 return true;
1732 switch (s->state) {
1733 case MIGRATION_STATUS_NONE:
1734 case MIGRATION_STATUS_CANCELLED:
1735 case MIGRATION_STATUS_COMPLETED:
1736 case MIGRATION_STATUS_FAILED:
1737 return true;
1738 case MIGRATION_STATUS_SETUP:
1739 case MIGRATION_STATUS_CANCELLING:
1740 case MIGRATION_STATUS_ACTIVE:
1741 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1742 case MIGRATION_STATUS_COLO:
1743 case MIGRATION_STATUS_PRE_SWITCHOVER:
1744 case MIGRATION_STATUS_DEVICE:
1745 case MIGRATION_STATUS_WAIT_UNPLUG:
1746 return false;
1747 case MIGRATION_STATUS__MAX:
1748 g_assert_not_reached();
1751 return false;
1754 bool migration_is_active(MigrationState *s)
1756 return (s->state == MIGRATION_STATUS_ACTIVE ||
1757 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1760 void migrate_init(MigrationState *s)
1763 * Reinitialise all migration state, except
1764 * parameters/capabilities that the user set, and
1765 * locks.
1767 s->cleanup_bh = 0;
1768 s->to_dst_file = NULL;
1769 s->state = MIGRATION_STATUS_NONE;
1770 s->rp_state.from_dst_file = NULL;
1771 s->rp_state.error = false;
1772 s->mbps = 0.0;
1773 s->pages_per_second = 0.0;
1774 s->downtime = 0;
1775 s->expected_downtime = 0;
1776 s->setup_time = 0;
1777 s->start_postcopy = false;
1778 s->postcopy_after_devices = false;
1779 s->migration_thread_running = false;
1780 error_free(s->error);
1781 s->error = NULL;
1783 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
1785 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1786 s->total_time = 0;
1787 s->vm_was_running = false;
1788 s->iteration_initial_bytes = 0;
1789 s->threshold_size = 0;
1792 static GSList *migration_blockers;
1794 int migrate_add_blocker(Error *reason, Error **errp)
1796 if (only_migratable) {
1797 error_propagate_prepend(errp, error_copy(reason),
1798 "disallowing migration blocker "
1799 "(--only-migratable) for: ");
1800 return -EACCES;
1803 if (migration_is_idle()) {
1804 migration_blockers = g_slist_prepend(migration_blockers, reason);
1805 return 0;
1808 error_propagate_prepend(errp, error_copy(reason),
1809 "disallowing migration blocker "
1810 "(migration in progress) for: ");
1811 return -EBUSY;
1814 void migrate_del_blocker(Error *reason)
1816 migration_blockers = g_slist_remove(migration_blockers, reason);
1819 void qmp_migrate_incoming(const char *uri, Error **errp)
1821 Error *local_err = NULL;
1822 static bool once = true;
1824 if (!deferred_incoming) {
1825 error_setg(errp, "For use with '-incoming defer'");
1826 return;
1828 if (!once) {
1829 error_setg(errp, "The incoming migration has already been started");
1830 return;
1833 qemu_start_incoming_migration(uri, &local_err);
1835 if (local_err) {
1836 error_propagate(errp, local_err);
1837 return;
1840 once = false;
1843 void qmp_migrate_recover(const char *uri, Error **errp)
1845 MigrationIncomingState *mis = migration_incoming_get_current();
1847 if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1848 error_setg(errp, "Migrate recover can only be run "
1849 "when postcopy is paused.");
1850 return;
1853 if (atomic_cmpxchg(&mis->postcopy_recover_triggered,
1854 false, true) == true) {
1855 error_setg(errp, "Migrate recovery is triggered already");
1856 return;
1860 * Note that this call will never start a real migration; it will
1861 * only re-setup the migration stream and poke existing migration
1862 * to continue using that newly established channel.
1864 qemu_start_incoming_migration(uri, errp);
1867 void qmp_migrate_pause(Error **errp)
1869 MigrationState *ms = migrate_get_current();
1870 MigrationIncomingState *mis = migration_incoming_get_current();
1871 int ret;
1873 if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1874 /* Source side, during postcopy */
1875 qemu_mutex_lock(&ms->qemu_file_lock);
1876 ret = qemu_file_shutdown(ms->to_dst_file);
1877 qemu_mutex_unlock(&ms->qemu_file_lock);
1878 if (ret) {
1879 error_setg(errp, "Failed to pause source migration");
1881 return;
1884 if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1885 ret = qemu_file_shutdown(mis->from_src_file);
1886 if (ret) {
1887 error_setg(errp, "Failed to pause destination migration");
1889 return;
1892 error_setg(errp, "migrate-pause is currently only supported "
1893 "during postcopy-active state");
1896 bool migration_is_blocked(Error **errp)
1898 if (qemu_savevm_state_blocked(errp)) {
1899 return true;
1902 if (migration_blockers) {
1903 error_propagate(errp, error_copy(migration_blockers->data));
1904 return true;
1907 return false;
1910 /* Returns true if continue to migrate, or false if error detected */
1911 static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
1912 bool resume, Error **errp)
1914 Error *local_err = NULL;
1916 if (resume) {
1917 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1918 error_setg(errp, "Cannot resume if there is no "
1919 "paused migration");
1920 return false;
1924 * Postcopy recovery won't work well with release-ram
1925 * capability since release-ram will drop the page buffer as
1926 * long as the page is put into the send buffer. So if there
1927 * is a network failure happened, any page buffers that have
1928 * not yet reached the destination VM but have already been
1929 * sent from the source VM will be lost forever. Let's refuse
1930 * the client from resuming such a postcopy migration.
1931 * Luckily release-ram was designed to only be used when src
1932 * and destination VMs are on the same host, so it should be
1933 * fine.
1935 if (migrate_release_ram()) {
1936 error_setg(errp, "Postcopy recovery cannot work "
1937 "when release-ram capability is set");
1938 return false;
1941 /* This is a resume, skip init status */
1942 return true;
1945 if (migration_is_running(s->state)) {
1946 error_setg(errp, QERR_MIGRATION_ACTIVE);
1947 return false;
1950 if (runstate_check(RUN_STATE_INMIGRATE)) {
1951 error_setg(errp, "Guest is waiting for an incoming migration");
1952 return false;
1955 if (migration_is_blocked(errp)) {
1956 return false;
1959 if (blk || blk_inc) {
1960 if (migrate_use_block() || migrate_use_block_incremental()) {
1961 error_setg(errp, "Command options are incompatible with "
1962 "current migration capabilities");
1963 return false;
1965 migrate_set_block_enabled(true, &local_err);
1966 if (local_err) {
1967 error_propagate(errp, local_err);
1968 return false;
1970 s->must_remove_block_options = true;
1973 if (blk_inc) {
1974 migrate_set_block_incremental(s, true);
1977 migrate_init(s);
1979 * set ram_counters memory to zero for a
1980 * new migration
1982 memset(&ram_counters, 0, sizeof(ram_counters));
1984 return true;
1987 void qmp_migrate(const char *uri, bool has_blk, bool blk,
1988 bool has_inc, bool inc, bool has_detach, bool detach,
1989 bool has_resume, bool resume, Error **errp)
1991 Error *local_err = NULL;
1992 MigrationState *s = migrate_get_current();
1993 const char *p;
1995 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
1996 has_resume && resume, errp)) {
1997 /* Error detected, put into errp */
1998 return;
2001 if (strstart(uri, "tcp:", &p)) {
2002 tcp_start_outgoing_migration(s, p, &local_err);
2003 #ifdef CONFIG_RDMA
2004 } else if (strstart(uri, "rdma:", &p)) {
2005 rdma_start_outgoing_migration(s, p, &local_err);
2006 #endif
2007 } else if (strstart(uri, "exec:", &p)) {
2008 exec_start_outgoing_migration(s, p, &local_err);
2009 } else if (strstart(uri, "unix:", &p)) {
2010 unix_start_outgoing_migration(s, p, &local_err);
2011 } else if (strstart(uri, "fd:", &p)) {
2012 fd_start_outgoing_migration(s, p, &local_err);
2013 } else {
2014 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
2015 "a valid migration protocol");
2016 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2017 MIGRATION_STATUS_FAILED);
2018 block_cleanup_parameters(s);
2019 return;
2022 if (local_err) {
2023 migrate_fd_error(s, local_err);
2024 error_propagate(errp, local_err);
2025 return;
2029 void qmp_migrate_cancel(Error **errp)
2031 migrate_fd_cancel(migrate_get_current());
2034 void qmp_migrate_continue(MigrationStatus state, Error **errp)
2036 MigrationState *s = migrate_get_current();
2037 if (s->state != state) {
2038 error_setg(errp, "Migration not in expected state: %s",
2039 MigrationStatus_str(s->state));
2040 return;
2042 qemu_sem_post(&s->pause_sem);
2045 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
2047 MigrateSetParameters p = {
2048 .has_xbzrle_cache_size = true,
2049 .xbzrle_cache_size = value,
2052 qmp_migrate_set_parameters(&p, errp);
2055 int64_t qmp_query_migrate_cache_size(Error **errp)
2057 return migrate_xbzrle_cache_size();
2060 void qmp_migrate_set_speed(int64_t value, Error **errp)
2062 MigrateSetParameters p = {
2063 .has_max_bandwidth = true,
2064 .max_bandwidth = value,
2067 qmp_migrate_set_parameters(&p, errp);
2070 void qmp_migrate_set_downtime(double value, Error **errp)
2072 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
2073 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
2074 "the range of 0 to %d seconds",
2075 MAX_MIGRATE_DOWNTIME_SECONDS);
2076 return;
2079 value *= 1000; /* Convert to milliseconds */
2081 MigrateSetParameters p = {
2082 .has_downtime_limit = true,
2083 .downtime_limit = (int64_t)value,
2086 qmp_migrate_set_parameters(&p, errp);
2089 bool migrate_release_ram(void)
2091 MigrationState *s;
2093 s = migrate_get_current();
2095 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
2098 bool migrate_postcopy_ram(void)
2100 MigrationState *s;
2102 s = migrate_get_current();
2104 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
2107 bool migrate_postcopy(void)
2109 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
2112 bool migrate_auto_converge(void)
2114 MigrationState *s;
2116 s = migrate_get_current();
2118 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
2121 bool migrate_zero_blocks(void)
2123 MigrationState *s;
2125 s = migrate_get_current();
2127 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
2130 bool migrate_postcopy_blocktime(void)
2132 MigrationState *s;
2134 s = migrate_get_current();
2136 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
2139 bool migrate_use_compression(void)
2141 MigrationState *s;
2143 s = migrate_get_current();
2145 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
2148 int migrate_compress_level(void)
2150 MigrationState *s;
2152 s = migrate_get_current();
2154 return s->parameters.compress_level;
2157 int migrate_compress_threads(void)
2159 MigrationState *s;
2161 s = migrate_get_current();
2163 return s->parameters.compress_threads;
2166 int migrate_compress_wait_thread(void)
2168 MigrationState *s;
2170 s = migrate_get_current();
2172 return s->parameters.compress_wait_thread;
2175 int migrate_decompress_threads(void)
2177 MigrationState *s;
2179 s = migrate_get_current();
2181 return s->parameters.decompress_threads;
2184 bool migrate_dirty_bitmaps(void)
2186 MigrationState *s;
2188 s = migrate_get_current();
2190 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
2193 bool migrate_ignore_shared(void)
2195 MigrationState *s;
2197 s = migrate_get_current();
2199 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
2202 bool migrate_validate_uuid(void)
2204 MigrationState *s;
2206 s = migrate_get_current();
2208 return s->enabled_capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
2211 bool migrate_use_events(void)
2213 MigrationState *s;
2215 s = migrate_get_current();
2217 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
2220 bool migrate_use_multifd(void)
2222 MigrationState *s;
2224 s = migrate_get_current();
2226 return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD];
2229 bool migrate_pause_before_switchover(void)
2231 MigrationState *s;
2233 s = migrate_get_current();
2235 return s->enabled_capabilities[
2236 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
2239 int migrate_multifd_channels(void)
2241 MigrationState *s;
2243 s = migrate_get_current();
2245 return s->parameters.multifd_channels;
2248 int migrate_use_xbzrle(void)
2250 MigrationState *s;
2252 s = migrate_get_current();
2254 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
2257 int64_t migrate_xbzrle_cache_size(void)
2259 MigrationState *s;
2261 s = migrate_get_current();
2263 return s->parameters.xbzrle_cache_size;
2266 static int64_t migrate_max_postcopy_bandwidth(void)
2268 MigrationState *s;
2270 s = migrate_get_current();
2272 return s->parameters.max_postcopy_bandwidth;
2275 bool migrate_use_block(void)
2277 MigrationState *s;
2279 s = migrate_get_current();
2281 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
2284 bool migrate_use_return_path(void)
2286 MigrationState *s;
2288 s = migrate_get_current();
2290 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
2293 bool migrate_use_block_incremental(void)
2295 MigrationState *s;
2297 s = migrate_get_current();
2299 return s->parameters.block_incremental;
2302 /* migration thread support */
2304 * Something bad happened to the RP stream, mark an error
2305 * The caller shall print or trace something to indicate why
2307 static void mark_source_rp_bad(MigrationState *s)
2309 s->rp_state.error = true;
2312 static struct rp_cmd_args {
2313 ssize_t len; /* -1 = variable */
2314 const char *name;
2315 } rp_cmd_args[] = {
2316 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
2317 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
2318 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
2319 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
2320 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
2321 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
2322 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" },
2323 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
2327 * Process a request for pages received on the return path,
2328 * We're allowed to send more than requested (e.g. to round to our page size)
2329 * and we don't need to send pages that have already been sent.
2331 static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
2332 ram_addr_t start, size_t len)
2334 long our_host_ps = qemu_real_host_page_size;
2336 trace_migrate_handle_rp_req_pages(rbname, start, len);
2339 * Since we currently insist on matching page sizes, just sanity check
2340 * we're being asked for whole host pages.
2342 if (start & (our_host_ps-1) ||
2343 (len & (our_host_ps-1))) {
2344 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
2345 " len: %zd", __func__, start, len);
2346 mark_source_rp_bad(ms);
2347 return;
2350 if (ram_save_queue_pages(rbname, start, len)) {
2351 mark_source_rp_bad(ms);
2355 /* Return true to retry, false to quit */
2356 static bool postcopy_pause_return_path_thread(MigrationState *s)
2358 trace_postcopy_pause_return_path();
2360 qemu_sem_wait(&s->postcopy_pause_rp_sem);
2362 trace_postcopy_pause_return_path_continued();
2364 return true;
2367 static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
2369 RAMBlock *block = qemu_ram_block_by_name(block_name);
2371 if (!block) {
2372 error_report("%s: invalid block name '%s'", __func__, block_name);
2373 return -EINVAL;
2376 /* Fetch the received bitmap and refresh the dirty bitmap */
2377 return ram_dirty_bitmap_reload(s, block);
2380 static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
2382 trace_source_return_path_thread_resume_ack(value);
2384 if (value != MIGRATION_RESUME_ACK_VALUE) {
2385 error_report("%s: illegal resume_ack value %"PRIu32,
2386 __func__, value);
2387 return -1;
2390 /* Now both sides are active. */
2391 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2392 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2394 /* Notify send thread that time to continue send pages */
2395 qemu_sem_post(&s->rp_state.rp_sem);
2397 return 0;
2401 * Handles messages sent on the return path towards the source VM
2404 static void *source_return_path_thread(void *opaque)
2406 MigrationState *ms = opaque;
2407 QEMUFile *rp = ms->rp_state.from_dst_file;
2408 uint16_t header_len, header_type;
2409 uint8_t buf[512];
2410 uint32_t tmp32, sibling_error;
2411 ram_addr_t start = 0; /* =0 to silence warning */
2412 size_t len = 0, expected_len;
2413 int res;
2415 trace_source_return_path_thread_entry();
2416 rcu_register_thread();
2418 retry:
2419 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
2420 migration_is_setup_or_active(ms->state)) {
2421 trace_source_return_path_thread_loop_top();
2422 header_type = qemu_get_be16(rp);
2423 header_len = qemu_get_be16(rp);
2425 if (qemu_file_get_error(rp)) {
2426 mark_source_rp_bad(ms);
2427 goto out;
2430 if (header_type >= MIG_RP_MSG_MAX ||
2431 header_type == MIG_RP_MSG_INVALID) {
2432 error_report("RP: Received invalid message 0x%04x length 0x%04x",
2433 header_type, header_len);
2434 mark_source_rp_bad(ms);
2435 goto out;
2438 if ((rp_cmd_args[header_type].len != -1 &&
2439 header_len != rp_cmd_args[header_type].len) ||
2440 header_len > sizeof(buf)) {
2441 error_report("RP: Received '%s' message (0x%04x) with"
2442 "incorrect length %d expecting %zu",
2443 rp_cmd_args[header_type].name, header_type, header_len,
2444 (size_t)rp_cmd_args[header_type].len);
2445 mark_source_rp_bad(ms);
2446 goto out;
2449 /* We know we've got a valid header by this point */
2450 res = qemu_get_buffer(rp, buf, header_len);
2451 if (res != header_len) {
2452 error_report("RP: Failed reading data for message 0x%04x"
2453 " read %d expected %d",
2454 header_type, res, header_len);
2455 mark_source_rp_bad(ms);
2456 goto out;
2459 /* OK, we have the message and the data */
2460 switch (header_type) {
2461 case MIG_RP_MSG_SHUT:
2462 sibling_error = ldl_be_p(buf);
2463 trace_source_return_path_thread_shut(sibling_error);
2464 if (sibling_error) {
2465 error_report("RP: Sibling indicated error %d", sibling_error);
2466 mark_source_rp_bad(ms);
2469 * We'll let the main thread deal with closing the RP
2470 * we could do a shutdown(2) on it, but we're the only user
2471 * anyway, so there's nothing gained.
2473 goto out;
2475 case MIG_RP_MSG_PONG:
2476 tmp32 = ldl_be_p(buf);
2477 trace_source_return_path_thread_pong(tmp32);
2478 break;
2480 case MIG_RP_MSG_REQ_PAGES:
2481 start = ldq_be_p(buf);
2482 len = ldl_be_p(buf + 8);
2483 migrate_handle_rp_req_pages(ms, NULL, start, len);
2484 break;
2486 case MIG_RP_MSG_REQ_PAGES_ID:
2487 expected_len = 12 + 1; /* header + termination */
2489 if (header_len >= expected_len) {
2490 start = ldq_be_p(buf);
2491 len = ldl_be_p(buf + 8);
2492 /* Now we expect an idstr */
2493 tmp32 = buf[12]; /* Length of the following idstr */
2494 buf[13 + tmp32] = '\0';
2495 expected_len += tmp32;
2497 if (header_len != expected_len) {
2498 error_report("RP: Req_Page_id with length %d expecting %zd",
2499 header_len, expected_len);
2500 mark_source_rp_bad(ms);
2501 goto out;
2503 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2504 break;
2506 case MIG_RP_MSG_RECV_BITMAP:
2507 if (header_len < 1) {
2508 error_report("%s: missing block name", __func__);
2509 mark_source_rp_bad(ms);
2510 goto out;
2512 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2513 buf[buf[0] + 1] = '\0';
2514 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2515 mark_source_rp_bad(ms);
2516 goto out;
2518 break;
2520 case MIG_RP_MSG_RESUME_ACK:
2521 tmp32 = ldl_be_p(buf);
2522 if (migrate_handle_rp_resume_ack(ms, tmp32)) {
2523 mark_source_rp_bad(ms);
2524 goto out;
2526 break;
2528 default:
2529 break;
2533 out:
2534 res = qemu_file_get_error(rp);
2535 if (res) {
2536 if (res == -EIO && migration_in_postcopy()) {
2538 * Maybe there is something we can do: it looks like a
2539 * network down issue, and we pause for a recovery.
2541 if (postcopy_pause_return_path_thread(ms)) {
2542 /* Reload rp, reset the rest */
2543 if (rp != ms->rp_state.from_dst_file) {
2544 qemu_fclose(rp);
2545 rp = ms->rp_state.from_dst_file;
2547 ms->rp_state.error = false;
2548 goto retry;
2552 trace_source_return_path_thread_bad_end();
2553 mark_source_rp_bad(ms);
2556 trace_source_return_path_thread_end();
2557 ms->rp_state.from_dst_file = NULL;
2558 qemu_fclose(rp);
2559 rcu_unregister_thread();
2560 return NULL;
2563 static int open_return_path_on_source(MigrationState *ms,
2564 bool create_thread)
2567 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
2568 if (!ms->rp_state.from_dst_file) {
2569 return -1;
2572 trace_open_return_path_on_source();
2574 if (!create_thread) {
2575 /* We're done */
2576 return 0;
2579 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2580 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2582 trace_open_return_path_on_source_continue();
2584 return 0;
2587 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2588 static int await_return_path_close_on_source(MigrationState *ms)
2591 * If this is a normal exit then the destination will send a SHUT and the
2592 * rp_thread will exit, however if there's an error we need to cause
2593 * it to exit.
2595 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
2597 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2598 * waiting for the destination.
2600 qemu_file_shutdown(ms->rp_state.from_dst_file);
2601 mark_source_rp_bad(ms);
2603 trace_await_return_path_close_on_source_joining();
2604 qemu_thread_join(&ms->rp_state.rp_thread);
2605 trace_await_return_path_close_on_source_close();
2606 return ms->rp_state.error;
2610 * Switch from normal iteration to postcopy
2611 * Returns non-0 on error
2613 static int postcopy_start(MigrationState *ms)
2615 int ret;
2616 QIOChannelBuffer *bioc;
2617 QEMUFile *fb;
2618 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2619 int64_t bandwidth = migrate_max_postcopy_bandwidth();
2620 bool restart_block = false;
2621 int cur_state = MIGRATION_STATUS_ACTIVE;
2622 if (!migrate_pause_before_switchover()) {
2623 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2624 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2627 trace_postcopy_start();
2628 qemu_mutex_lock_iothread();
2629 trace_postcopy_start_set_run();
2631 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
2632 global_state_store();
2633 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
2634 if (ret < 0) {
2635 goto fail;
2638 ret = migration_maybe_pause(ms, &cur_state,
2639 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2640 if (ret < 0) {
2641 goto fail;
2644 ret = bdrv_inactivate_all();
2645 if (ret < 0) {
2646 goto fail;
2648 restart_block = true;
2651 * Cause any non-postcopiable, but iterative devices to
2652 * send out their final data.
2654 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
2657 * in Finish migrate and with the io-lock held everything should
2658 * be quiet, but we've potentially still got dirty pages and we
2659 * need to tell the destination to throw any pages it's already received
2660 * that are dirty
2662 if (migrate_postcopy_ram()) {
2663 if (ram_postcopy_send_discard_bitmap(ms)) {
2664 error_report("postcopy send discard bitmap failed");
2665 goto fail;
2670 * send rest of state - note things that are doing postcopy
2671 * will notice we're in POSTCOPY_ACTIVE and not actually
2672 * wrap their state up here
2674 /* 0 max-postcopy-bandwidth means unlimited */
2675 if (!bandwidth) {
2676 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
2677 } else {
2678 qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO);
2680 if (migrate_postcopy_ram()) {
2681 /* Ping just for debugging, helps line traces up */
2682 qemu_savevm_send_ping(ms->to_dst_file, 2);
2686 * While loading the device state we may trigger page transfer
2687 * requests and the fd must be free to process those, and thus
2688 * the destination must read the whole device state off the fd before
2689 * it starts processing it. Unfortunately the ad-hoc migration format
2690 * doesn't allow the destination to know the size to read without fully
2691 * parsing it through each devices load-state code (especially the open
2692 * coded devices that use get/put).
2693 * So we wrap the device state up in a package with a length at the start;
2694 * to do this we use a qemu_buf to hold the whole of the device state.
2696 bioc = qio_channel_buffer_new(4096);
2697 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
2698 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2699 object_unref(OBJECT(bioc));
2702 * Make sure the receiver can get incoming pages before we send the rest
2703 * of the state
2705 qemu_savevm_send_postcopy_listen(fb);
2707 qemu_savevm_state_complete_precopy(fb, false, false);
2708 if (migrate_postcopy_ram()) {
2709 qemu_savevm_send_ping(fb, 3);
2712 qemu_savevm_send_postcopy_run(fb);
2714 /* <><> end of stuff going into the package */
2716 /* Last point of recovery; as soon as we send the package the destination
2717 * can open devices and potentially start running.
2718 * Lets just check again we've not got any errors.
2720 ret = qemu_file_get_error(ms->to_dst_file);
2721 if (ret) {
2722 error_report("postcopy_start: Migration stream errored (pre package)");
2723 goto fail_closefb;
2726 restart_block = false;
2728 /* Now send that blob */
2729 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
2730 goto fail_closefb;
2732 qemu_fclose(fb);
2734 /* Send a notify to give a chance for anything that needs to happen
2735 * at the transition to postcopy and after the device state; in particular
2736 * spice needs to trigger a transition now
2738 ms->postcopy_after_devices = true;
2739 notifier_list_notify(&migration_state_notifiers, ms);
2741 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2743 qemu_mutex_unlock_iothread();
2745 if (migrate_postcopy_ram()) {
2747 * Although this ping is just for debug, it could potentially be
2748 * used for getting a better measurement of downtime at the source.
2750 qemu_savevm_send_ping(ms->to_dst_file, 4);
2753 if (migrate_release_ram()) {
2754 ram_postcopy_migrated_memory_release(ms);
2757 ret = qemu_file_get_error(ms->to_dst_file);
2758 if (ret) {
2759 error_report("postcopy_start: Migration stream errored");
2760 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2761 MIGRATION_STATUS_FAILED);
2764 return ret;
2766 fail_closefb:
2767 qemu_fclose(fb);
2768 fail:
2769 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2770 MIGRATION_STATUS_FAILED);
2771 if (restart_block) {
2772 /* A failure happened early enough that we know the destination hasn't
2773 * accessed block devices, so we're safe to recover.
2775 Error *local_err = NULL;
2777 bdrv_invalidate_cache_all(&local_err);
2778 if (local_err) {
2779 error_report_err(local_err);
2782 qemu_mutex_unlock_iothread();
2783 return -1;
2787 * migration_maybe_pause: Pause if required to by
2788 * migrate_pause_before_switchover called with the iothread locked
2789 * Returns: 0 on success
2791 static int migration_maybe_pause(MigrationState *s,
2792 int *current_active_state,
2793 int new_state)
2795 if (!migrate_pause_before_switchover()) {
2796 return 0;
2799 /* Since leaving this state is not atomic with posting the semaphore
2800 * it's possible that someone could have issued multiple migrate_continue
2801 * and the semaphore is incorrectly positive at this point;
2802 * the docs say it's undefined to reinit a semaphore that's already
2803 * init'd, so use timedwait to eat up any existing posts.
2805 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2806 /* This block intentionally left blank */
2810 * If the migration is cancelled when it is in the completion phase,
2811 * the migration state is set to MIGRATION_STATUS_CANCELLING.
2812 * So we don't need to wait a semaphore, otherwise we would always
2813 * wait for the 'pause_sem' semaphore.
2815 if (s->state != MIGRATION_STATUS_CANCELLING) {
2816 qemu_mutex_unlock_iothread();
2817 migrate_set_state(&s->state, *current_active_state,
2818 MIGRATION_STATUS_PRE_SWITCHOVER);
2819 qemu_sem_wait(&s->pause_sem);
2820 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
2821 new_state);
2822 *current_active_state = new_state;
2823 qemu_mutex_lock_iothread();
2826 return s->state == new_state ? 0 : -EINVAL;
2830 * migration_completion: Used by migration_thread when there's not much left.
2831 * The caller 'breaks' the loop when this returns.
2833 * @s: Current migration state
2835 static void migration_completion(MigrationState *s)
2837 int ret;
2838 int current_active_state = s->state;
2840 if (s->state == MIGRATION_STATUS_ACTIVE) {
2841 qemu_mutex_lock_iothread();
2842 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2843 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
2844 s->vm_was_running = runstate_is_running();
2845 ret = global_state_store();
2847 if (!ret) {
2848 bool inactivate = !migrate_colo_enabled();
2849 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
2850 if (ret >= 0) {
2851 ret = migration_maybe_pause(s, &current_active_state,
2852 MIGRATION_STATUS_DEVICE);
2854 if (ret >= 0) {
2855 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
2856 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2857 inactivate);
2859 if (inactivate && ret >= 0) {
2860 s->block_inactive = true;
2863 qemu_mutex_unlock_iothread();
2865 if (ret < 0) {
2866 goto fail;
2868 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2869 trace_migration_completion_postcopy_end();
2871 qemu_savevm_state_complete_postcopy(s->to_dst_file);
2872 trace_migration_completion_postcopy_end_after_complete();
2876 * If rp was opened we must clean up the thread before
2877 * cleaning everything else up (since if there are no failures
2878 * it will wait for the destination to send it's status in
2879 * a SHUT command).
2881 if (s->rp_state.from_dst_file) {
2882 int rp_error;
2883 trace_migration_return_path_end_before();
2884 rp_error = await_return_path_close_on_source(s);
2885 trace_migration_return_path_end_after(rp_error);
2886 if (rp_error) {
2887 goto fail_invalidate;
2891 if (qemu_file_get_error(s->to_dst_file)) {
2892 trace_migration_completion_file_err();
2893 goto fail_invalidate;
2896 if (!migrate_colo_enabled()) {
2897 migrate_set_state(&s->state, current_active_state,
2898 MIGRATION_STATUS_COMPLETED);
2901 return;
2903 fail_invalidate:
2904 /* If not doing postcopy, vm_start() will be called: let's regain
2905 * control on images.
2907 if (s->state == MIGRATION_STATUS_ACTIVE ||
2908 s->state == MIGRATION_STATUS_DEVICE) {
2909 Error *local_err = NULL;
2911 qemu_mutex_lock_iothread();
2912 bdrv_invalidate_cache_all(&local_err);
2913 if (local_err) {
2914 error_report_err(local_err);
2915 } else {
2916 s->block_inactive = false;
2918 qemu_mutex_unlock_iothread();
2921 fail:
2922 migrate_set_state(&s->state, current_active_state,
2923 MIGRATION_STATUS_FAILED);
2926 bool migrate_colo_enabled(void)
2928 MigrationState *s = migrate_get_current();
2929 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
2932 typedef enum MigThrError {
2933 /* No error detected */
2934 MIG_THR_ERR_NONE = 0,
2935 /* Detected error, but resumed successfully */
2936 MIG_THR_ERR_RECOVERED = 1,
2937 /* Detected fatal error, need to exit */
2938 MIG_THR_ERR_FATAL = 2,
2939 } MigThrError;
2941 static int postcopy_resume_handshake(MigrationState *s)
2943 qemu_savevm_send_postcopy_resume(s->to_dst_file);
2945 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
2946 qemu_sem_wait(&s->rp_state.rp_sem);
2949 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2950 return 0;
2953 return -1;
2956 /* Return zero if success, or <0 for error */
2957 static int postcopy_do_resume(MigrationState *s)
2959 int ret;
2962 * Call all the resume_prepare() hooks, so that modules can be
2963 * ready for the migration resume.
2965 ret = qemu_savevm_state_resume_prepare(s);
2966 if (ret) {
2967 error_report("%s: resume_prepare() failure detected: %d",
2968 __func__, ret);
2969 return ret;
2973 * Last handshake with destination on the resume (destination will
2974 * switch to postcopy-active afterwards)
2976 ret = postcopy_resume_handshake(s);
2977 if (ret) {
2978 error_report("%s: handshake failed: %d", __func__, ret);
2979 return ret;
2982 return 0;
2986 * We don't return until we are in a safe state to continue current
2987 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
2988 * MIG_THR_ERR_FATAL if unrecovery failure happened.
2990 static MigThrError postcopy_pause(MigrationState *s)
2992 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
2994 while (true) {
2995 QEMUFile *file;
2997 migrate_set_state(&s->state, s->state,
2998 MIGRATION_STATUS_POSTCOPY_PAUSED);
3000 /* Current channel is possibly broken. Release it. */
3001 assert(s->to_dst_file);
3002 qemu_mutex_lock(&s->qemu_file_lock);
3003 file = s->to_dst_file;
3004 s->to_dst_file = NULL;
3005 qemu_mutex_unlock(&s->qemu_file_lock);
3007 qemu_file_shutdown(file);
3008 qemu_fclose(file);
3010 error_report("Detected IO failure for postcopy. "
3011 "Migration paused.");
3014 * We wait until things fixed up. Then someone will setup the
3015 * status back for us.
3017 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
3018 qemu_sem_wait(&s->postcopy_pause_sem);
3021 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3022 /* Woken up by a recover procedure. Give it a shot */
3025 * Firstly, let's wake up the return path now, with a new
3026 * return path channel.
3028 qemu_sem_post(&s->postcopy_pause_rp_sem);
3030 /* Do the resume logic */
3031 if (postcopy_do_resume(s) == 0) {
3032 /* Let's continue! */
3033 trace_postcopy_pause_continued();
3034 return MIG_THR_ERR_RECOVERED;
3035 } else {
3037 * Something wrong happened during the recovery, let's
3038 * pause again. Pause is always better than throwing
3039 * data away.
3041 continue;
3043 } else {
3044 /* This is not right... Time to quit. */
3045 return MIG_THR_ERR_FATAL;
3050 static MigThrError migration_detect_error(MigrationState *s)
3052 int ret;
3053 int state = s->state;
3054 Error *local_error = NULL;
3056 if (state == MIGRATION_STATUS_CANCELLING ||
3057 state == MIGRATION_STATUS_CANCELLED) {
3058 /* End the migration, but don't set the state to failed */
3059 return MIG_THR_ERR_FATAL;
3062 /* Try to detect any file errors */
3063 ret = qemu_file_get_error_obj(s->to_dst_file, &local_error);
3064 if (!ret) {
3065 /* Everything is fine */
3066 assert(!local_error);
3067 return MIG_THR_ERR_NONE;
3070 if (local_error) {
3071 migrate_set_error(s, local_error);
3072 error_free(local_error);
3075 if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
3077 * For postcopy, we allow the network to be down for a
3078 * while. After that, it can be continued by a
3079 * recovery phase.
3081 return postcopy_pause(s);
3082 } else {
3084 * For precopy (or postcopy with error outside IO), we fail
3085 * with no time.
3087 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
3088 trace_migration_thread_file_err();
3090 /* Time to stop the migration, now. */
3091 return MIG_THR_ERR_FATAL;
3095 /* How many bytes have we transferred since the beginning of the migration */
3096 static uint64_t migration_total_bytes(MigrationState *s)
3098 return qemu_ftell(s->to_dst_file) + ram_counters.multifd_bytes;
3101 static void migration_calculate_complete(MigrationState *s)
3103 uint64_t bytes = migration_total_bytes(s);
3104 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3105 int64_t transfer_time;
3107 s->total_time = end_time - s->start_time;
3108 if (!s->downtime) {
3110 * It's still not set, so we are precopy migration. For
3111 * postcopy, downtime is calculated during postcopy_start().
3113 s->downtime = end_time - s->downtime_start;
3116 transfer_time = s->total_time - s->setup_time;
3117 if (transfer_time) {
3118 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
3122 static void update_iteration_initial_status(MigrationState *s)
3125 * Update these three fields at the same time to avoid mismatch info lead
3126 * wrong speed calculation.
3128 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3129 s->iteration_initial_bytes = migration_total_bytes(s);
3130 s->iteration_initial_pages = ram_get_total_transferred_pages();
3133 static void migration_update_counters(MigrationState *s,
3134 int64_t current_time)
3136 uint64_t transferred, transferred_pages, time_spent;
3137 uint64_t current_bytes; /* bytes transferred since the beginning */
3138 double bandwidth;
3140 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
3141 return;
3144 current_bytes = migration_total_bytes(s);
3145 transferred = current_bytes - s->iteration_initial_bytes;
3146 time_spent = current_time - s->iteration_start_time;
3147 bandwidth = (double)transferred / time_spent;
3148 s->threshold_size = bandwidth * s->parameters.downtime_limit;
3150 s->mbps = (((double) transferred * 8.0) /
3151 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
3153 transferred_pages = ram_get_total_transferred_pages() -
3154 s->iteration_initial_pages;
3155 s->pages_per_second = (double) transferred_pages /
3156 (((double) time_spent / 1000.0));
3159 * if we haven't sent anything, we don't want to
3160 * recalculate. 10000 is a small enough number for our purposes
3162 if (ram_counters.dirty_pages_rate && transferred > 10000) {
3163 s->expected_downtime = ram_counters.remaining / bandwidth;
3166 qemu_file_reset_rate_limit(s->to_dst_file);
3168 update_iteration_initial_status(s);
3170 trace_migrate_transferred(transferred, time_spent,
3171 bandwidth, s->threshold_size);
3174 /* Migration thread iteration status */
3175 typedef enum {
3176 MIG_ITERATE_RESUME, /* Resume current iteration */
3177 MIG_ITERATE_SKIP, /* Skip current iteration */
3178 MIG_ITERATE_BREAK, /* Break the loop */
3179 } MigIterateState;
3182 * Return true if continue to the next iteration directly, false
3183 * otherwise.
3185 static MigIterateState migration_iteration_run(MigrationState *s)
3187 uint64_t pending_size, pend_pre, pend_compat, pend_post;
3188 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
3190 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
3191 &pend_compat, &pend_post);
3192 pending_size = pend_pre + pend_compat + pend_post;
3194 trace_migrate_pending(pending_size, s->threshold_size,
3195 pend_pre, pend_compat, pend_post);
3197 if (pending_size && pending_size >= s->threshold_size) {
3198 /* Still a significant amount to transfer */
3199 if (!in_postcopy && pend_pre <= s->threshold_size &&
3200 atomic_read(&s->start_postcopy)) {
3201 if (postcopy_start(s)) {
3202 error_report("%s: postcopy failed to start", __func__);
3204 return MIG_ITERATE_SKIP;
3206 /* Just another iteration step */
3207 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
3208 } else {
3209 trace_migration_thread_low_pending(pending_size);
3210 migration_completion(s);
3211 return MIG_ITERATE_BREAK;
3214 return MIG_ITERATE_RESUME;
3217 static void migration_iteration_finish(MigrationState *s)
3219 /* If we enabled cpu throttling for auto-converge, turn it off. */
3220 cpu_throttle_stop();
3222 qemu_mutex_lock_iothread();
3223 switch (s->state) {
3224 case MIGRATION_STATUS_COMPLETED:
3225 migration_calculate_complete(s);
3226 runstate_set(RUN_STATE_POSTMIGRATE);
3227 break;
3229 case MIGRATION_STATUS_ACTIVE:
3231 * We should really assert here, but since it's during
3232 * migration, let's try to reduce the usage of assertions.
3234 if (!migrate_colo_enabled()) {
3235 error_report("%s: critical error: calling COLO code without "
3236 "COLO enabled", __func__);
3238 migrate_start_colo_process(s);
3240 * Fixme: we will run VM in COLO no matter its old running state.
3241 * After exited COLO, we will keep running.
3243 s->vm_was_running = true;
3244 /* Fallthrough */
3245 case MIGRATION_STATUS_FAILED:
3246 case MIGRATION_STATUS_CANCELLED:
3247 case MIGRATION_STATUS_CANCELLING:
3248 if (s->vm_was_running) {
3249 vm_start();
3250 } else {
3251 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
3252 runstate_set(RUN_STATE_POSTMIGRATE);
3255 break;
3257 default:
3258 /* Should not reach here, but if so, forgive the VM. */
3259 error_report("%s: Unknown ending state %d", __func__, s->state);
3260 break;
3262 migrate_fd_cleanup_schedule(s);
3263 qemu_mutex_unlock_iothread();
3266 void migration_make_urgent_request(void)
3268 qemu_sem_post(&migrate_get_current()->rate_limit_sem);
3271 void migration_consume_urgent_request(void)
3273 qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
3276 /* Returns true if the rate limiting was broken by an urgent request */
3277 bool migration_rate_limit(void)
3279 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3280 MigrationState *s = migrate_get_current();
3282 bool urgent = false;
3283 migration_update_counters(s, now);
3284 if (qemu_file_rate_limit(s->to_dst_file)) {
3286 * Wait for a delay to do rate limiting OR
3287 * something urgent to post the semaphore.
3289 int ms = s->iteration_start_time + BUFFER_DELAY - now;
3290 trace_migration_rate_limit_pre(ms);
3291 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
3293 * We were woken by one or more urgent things but
3294 * the timedwait will have consumed one of them.
3295 * The service routine for the urgent wake will dec
3296 * the semaphore itself for each item it consumes,
3297 * so add this one we just eat back.
3299 qemu_sem_post(&s->rate_limit_sem);
3300 urgent = true;
3302 trace_migration_rate_limit_post(urgent);
3304 return urgent;
3308 * Master migration thread on the source VM.
3309 * It drives the migration and pumps the data down the outgoing channel.
3311 static void *migration_thread(void *opaque)
3313 MigrationState *s = opaque;
3314 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
3315 MigThrError thr_error;
3316 bool urgent = false;
3318 rcu_register_thread();
3320 object_ref(OBJECT(s));
3321 update_iteration_initial_status(s);
3323 qemu_savevm_state_header(s->to_dst_file);
3326 * If we opened the return path, we need to make sure dst has it
3327 * opened as well.
3329 if (s->rp_state.from_dst_file) {
3330 /* Now tell the dest that it should open its end so it can reply */
3331 qemu_savevm_send_open_return_path(s->to_dst_file);
3333 /* And do a ping that will make stuff easier to debug */
3334 qemu_savevm_send_ping(s->to_dst_file, 1);
3337 if (migrate_postcopy()) {
3339 * Tell the destination that we *might* want to do postcopy later;
3340 * if the other end can't do postcopy it should fail now, nice and
3341 * early.
3343 qemu_savevm_send_postcopy_advise(s->to_dst_file);
3346 if (migrate_colo_enabled()) {
3347 /* Notify migration destination that we enable COLO */
3348 qemu_savevm_send_colo_enable(s->to_dst_file);
3351 qemu_savevm_state_setup(s->to_dst_file);
3353 if (qemu_savevm_state_guest_unplug_pending()) {
3354 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3355 MIGRATION_STATUS_WAIT_UNPLUG);
3357 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
3358 qemu_savevm_state_guest_unplug_pending()) {
3359 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3362 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG,
3363 MIGRATION_STATUS_ACTIVE);
3366 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
3367 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3368 MIGRATION_STATUS_ACTIVE);
3370 trace_migration_thread_setup_complete();
3372 while (migration_is_active(s)) {
3373 if (urgent || !qemu_file_rate_limit(s->to_dst_file)) {
3374 MigIterateState iter_state = migration_iteration_run(s);
3375 if (iter_state == MIG_ITERATE_SKIP) {
3376 continue;
3377 } else if (iter_state == MIG_ITERATE_BREAK) {
3378 break;
3383 * Try to detect any kind of failures, and see whether we
3384 * should stop the migration now.
3386 thr_error = migration_detect_error(s);
3387 if (thr_error == MIG_THR_ERR_FATAL) {
3388 /* Stop migration */
3389 break;
3390 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
3392 * Just recovered from a e.g. network failure, reset all
3393 * the local variables. This is important to avoid
3394 * breaking transferred_bytes and bandwidth calculation
3396 update_iteration_initial_status(s);
3399 urgent = migration_rate_limit();
3402 trace_migration_thread_after_loop();
3403 migration_iteration_finish(s);
3404 object_unref(OBJECT(s));
3405 rcu_unregister_thread();
3406 return NULL;
3409 void migrate_fd_connect(MigrationState *s, Error *error_in)
3411 Error *local_err = NULL;
3412 int64_t rate_limit;
3413 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
3415 s->expected_downtime = s->parameters.downtime_limit;
3416 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s);
3417 if (error_in) {
3418 migrate_fd_error(s, error_in);
3419 migrate_fd_cleanup(s);
3420 return;
3423 if (resume) {
3424 /* This is a resumed migration */
3425 rate_limit = s->parameters.max_postcopy_bandwidth /
3426 XFER_LIMIT_RATIO;
3427 } else {
3428 /* This is a fresh new migration */
3429 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
3431 /* Notify before starting migration thread */
3432 notifier_list_notify(&migration_state_notifiers, s);
3435 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
3436 qemu_file_set_blocking(s->to_dst_file, true);
3439 * Open the return path. For postcopy, it is used exclusively. For
3440 * precopy, only if user specified "return-path" capability would
3441 * QEMU uses the return path.
3443 if (migrate_postcopy_ram() || migrate_use_return_path()) {
3444 if (open_return_path_on_source(s, !resume)) {
3445 error_report("Unable to open return-path for postcopy");
3446 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
3447 migrate_fd_cleanup(s);
3448 return;
3452 if (resume) {
3453 /* Wakeup the main migration thread to do the recovery */
3454 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
3455 MIGRATION_STATUS_POSTCOPY_RECOVER);
3456 qemu_sem_post(&s->postcopy_pause_sem);
3457 return;
3460 if (multifd_save_setup(&local_err) != 0) {
3461 error_report_err(local_err);
3462 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3463 MIGRATION_STATUS_FAILED);
3464 migrate_fd_cleanup(s);
3465 return;
3467 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
3468 QEMU_THREAD_JOINABLE);
3469 s->migration_thread_running = true;
3472 void migration_global_dump(Monitor *mon)
3474 MigrationState *ms = migrate_get_current();
3476 monitor_printf(mon, "globals:\n");
3477 monitor_printf(mon, "store-global-state: %s\n",
3478 ms->store_global_state ? "on" : "off");
3479 monitor_printf(mon, "only-migratable: %s\n",
3480 only_migratable ? "on" : "off");
3481 monitor_printf(mon, "send-configuration: %s\n",
3482 ms->send_configuration ? "on" : "off");
3483 monitor_printf(mon, "send-section-footer: %s\n",
3484 ms->send_section_footer ? "on" : "off");
3485 monitor_printf(mon, "decompress-error-check: %s\n",
3486 ms->decompress_error_check ? "on" : "off");
3487 monitor_printf(mon, "clear-bitmap-shift: %u\n",
3488 ms->clear_bitmap_shift);
3491 #define DEFINE_PROP_MIG_CAP(name, x) \
3492 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
3494 static Property migration_properties[] = {
3495 DEFINE_PROP_BOOL("store-global-state", MigrationState,
3496 store_global_state, true),
3497 DEFINE_PROP_BOOL("send-configuration", MigrationState,
3498 send_configuration, true),
3499 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
3500 send_section_footer, true),
3501 DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
3502 decompress_error_check, true),
3503 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
3504 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
3506 /* Migration parameters */
3507 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
3508 parameters.compress_level,
3509 DEFAULT_MIGRATE_COMPRESS_LEVEL),
3510 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
3511 parameters.compress_threads,
3512 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
3513 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
3514 parameters.compress_wait_thread, true),
3515 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
3516 parameters.decompress_threads,
3517 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
3518 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
3519 parameters.cpu_throttle_initial,
3520 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
3521 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
3522 parameters.cpu_throttle_increment,
3523 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
3524 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
3525 parameters.max_bandwidth, MAX_THROTTLE),
3526 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
3527 parameters.downtime_limit,
3528 DEFAULT_MIGRATE_SET_DOWNTIME),
3529 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
3530 parameters.x_checkpoint_delay,
3531 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
3532 DEFINE_PROP_UINT8("multifd-channels", MigrationState,
3533 parameters.multifd_channels,
3534 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
3535 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
3536 parameters.multifd_compression,
3537 DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
3538 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
3539 parameters.xbzrle_cache_size,
3540 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
3541 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
3542 parameters.max_postcopy_bandwidth,
3543 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
3544 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
3545 parameters.max_cpu_throttle,
3546 DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
3547 DEFINE_PROP_SIZE("announce-initial", MigrationState,
3548 parameters.announce_initial,
3549 DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
3550 DEFINE_PROP_SIZE("announce-max", MigrationState,
3551 parameters.announce_max,
3552 DEFAULT_MIGRATE_ANNOUNCE_MAX),
3553 DEFINE_PROP_SIZE("announce-rounds", MigrationState,
3554 parameters.announce_rounds,
3555 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
3556 DEFINE_PROP_SIZE("announce-step", MigrationState,
3557 parameters.announce_step,
3558 DEFAULT_MIGRATE_ANNOUNCE_STEP),
3560 /* Migration capabilities */
3561 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
3562 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
3563 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
3564 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
3565 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
3566 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
3567 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
3568 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
3569 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
3570 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
3571 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
3572 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
3574 DEFINE_PROP_END_OF_LIST(),
3577 static void migration_class_init(ObjectClass *klass, void *data)
3579 DeviceClass *dc = DEVICE_CLASS(klass);
3581 dc->user_creatable = false;
3582 device_class_set_props(dc, migration_properties);
3585 static void migration_instance_finalize(Object *obj)
3587 MigrationState *ms = MIGRATION_OBJ(obj);
3588 MigrationParameters *params = &ms->parameters;
3590 qemu_mutex_destroy(&ms->error_mutex);
3591 qemu_mutex_destroy(&ms->qemu_file_lock);
3592 g_free(params->tls_hostname);
3593 g_free(params->tls_creds);
3594 qemu_sem_destroy(&ms->wait_unplug_sem);
3595 qemu_sem_destroy(&ms->rate_limit_sem);
3596 qemu_sem_destroy(&ms->pause_sem);
3597 qemu_sem_destroy(&ms->postcopy_pause_sem);
3598 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
3599 qemu_sem_destroy(&ms->rp_state.rp_sem);
3600 error_free(ms->error);
3603 static void migration_instance_init(Object *obj)
3605 MigrationState *ms = MIGRATION_OBJ(obj);
3606 MigrationParameters *params = &ms->parameters;
3608 ms->state = MIGRATION_STATUS_NONE;
3609 ms->mbps = -1;
3610 ms->pages_per_second = -1;
3611 qemu_sem_init(&ms->pause_sem, 0);
3612 qemu_mutex_init(&ms->error_mutex);
3614 params->tls_hostname = g_strdup("");
3615 params->tls_creds = g_strdup("");
3617 /* Set has_* up only for parameter checks */
3618 params->has_compress_level = true;
3619 params->has_compress_threads = true;
3620 params->has_decompress_threads = true;
3621 params->has_cpu_throttle_initial = true;
3622 params->has_cpu_throttle_increment = true;
3623 params->has_max_bandwidth = true;
3624 params->has_downtime_limit = true;
3625 params->has_x_checkpoint_delay = true;
3626 params->has_block_incremental = true;
3627 params->has_multifd_channels = true;
3628 params->has_multifd_compression = true;
3629 params->has_xbzrle_cache_size = true;
3630 params->has_max_postcopy_bandwidth = true;
3631 params->has_max_cpu_throttle = true;
3632 params->has_announce_initial = true;
3633 params->has_announce_max = true;
3634 params->has_announce_rounds = true;
3635 params->has_announce_step = true;
3637 qemu_sem_init(&ms->postcopy_pause_sem, 0);
3638 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
3639 qemu_sem_init(&ms->rp_state.rp_sem, 0);
3640 qemu_sem_init(&ms->rate_limit_sem, 0);
3641 qemu_sem_init(&ms->wait_unplug_sem, 0);
3642 qemu_mutex_init(&ms->qemu_file_lock);
3646 * Return true if check pass, false otherwise. Error will be put
3647 * inside errp if provided.
3649 static bool migration_object_check(MigrationState *ms, Error **errp)
3651 MigrationCapabilityStatusList *head = NULL;
3652 /* Assuming all off */
3653 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
3654 int i;
3656 if (!migrate_params_check(&ms->parameters, errp)) {
3657 return false;
3660 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
3661 if (ms->enabled_capabilities[i]) {
3662 head = migrate_cap_add(head, i, true);
3666 ret = migrate_caps_check(cap_list, head, errp);
3668 /* It works with head == NULL */
3669 qapi_free_MigrationCapabilityStatusList(head);
3671 return ret;
3674 static const TypeInfo migration_type = {
3675 .name = TYPE_MIGRATION,
3677 * NOTE: TYPE_MIGRATION is not really a device, as the object is
3678 * not created using qdev_create(), it is not attached to the qdev
3679 * device tree, and it is never realized.
3681 * TODO: Make this TYPE_OBJECT once QOM provides something like
3682 * TYPE_DEVICE's "-global" properties.
3684 .parent = TYPE_DEVICE,
3685 .class_init = migration_class_init,
3686 .class_size = sizeof(MigrationClass),
3687 .instance_size = sizeof(MigrationState),
3688 .instance_init = migration_instance_init,
3689 .instance_finalize = migration_instance_finalize,
3692 static void register_migration_types(void)
3694 type_register_static(&migration_type);
3697 type_init(register_migration_types);