multifd: Make no compression operations into its own structure
[qemu/kevin.git] / migration / migration.c
blobeb7b0a2df011ca9af87d7a51859b4be6edcfb0f7
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 MultiFDCompression migrate_multifd_compression(void)
2250 MigrationState *s;
2252 s = migrate_get_current();
2254 return s->parameters.multifd_compression;
2257 int migrate_use_xbzrle(void)
2259 MigrationState *s;
2261 s = migrate_get_current();
2263 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
2266 int64_t migrate_xbzrle_cache_size(void)
2268 MigrationState *s;
2270 s = migrate_get_current();
2272 return s->parameters.xbzrle_cache_size;
2275 static int64_t migrate_max_postcopy_bandwidth(void)
2277 MigrationState *s;
2279 s = migrate_get_current();
2281 return s->parameters.max_postcopy_bandwidth;
2284 bool migrate_use_block(void)
2286 MigrationState *s;
2288 s = migrate_get_current();
2290 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
2293 bool migrate_use_return_path(void)
2295 MigrationState *s;
2297 s = migrate_get_current();
2299 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
2302 bool migrate_use_block_incremental(void)
2304 MigrationState *s;
2306 s = migrate_get_current();
2308 return s->parameters.block_incremental;
2311 /* migration thread support */
2313 * Something bad happened to the RP stream, mark an error
2314 * The caller shall print or trace something to indicate why
2316 static void mark_source_rp_bad(MigrationState *s)
2318 s->rp_state.error = true;
2321 static struct rp_cmd_args {
2322 ssize_t len; /* -1 = variable */
2323 const char *name;
2324 } rp_cmd_args[] = {
2325 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
2326 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
2327 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
2328 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
2329 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
2330 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
2331 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" },
2332 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
2336 * Process a request for pages received on the return path,
2337 * We're allowed to send more than requested (e.g. to round to our page size)
2338 * and we don't need to send pages that have already been sent.
2340 static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
2341 ram_addr_t start, size_t len)
2343 long our_host_ps = qemu_real_host_page_size;
2345 trace_migrate_handle_rp_req_pages(rbname, start, len);
2348 * Since we currently insist on matching page sizes, just sanity check
2349 * we're being asked for whole host pages.
2351 if (start & (our_host_ps-1) ||
2352 (len & (our_host_ps-1))) {
2353 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
2354 " len: %zd", __func__, start, len);
2355 mark_source_rp_bad(ms);
2356 return;
2359 if (ram_save_queue_pages(rbname, start, len)) {
2360 mark_source_rp_bad(ms);
2364 /* Return true to retry, false to quit */
2365 static bool postcopy_pause_return_path_thread(MigrationState *s)
2367 trace_postcopy_pause_return_path();
2369 qemu_sem_wait(&s->postcopy_pause_rp_sem);
2371 trace_postcopy_pause_return_path_continued();
2373 return true;
2376 static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
2378 RAMBlock *block = qemu_ram_block_by_name(block_name);
2380 if (!block) {
2381 error_report("%s: invalid block name '%s'", __func__, block_name);
2382 return -EINVAL;
2385 /* Fetch the received bitmap and refresh the dirty bitmap */
2386 return ram_dirty_bitmap_reload(s, block);
2389 static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
2391 trace_source_return_path_thread_resume_ack(value);
2393 if (value != MIGRATION_RESUME_ACK_VALUE) {
2394 error_report("%s: illegal resume_ack value %"PRIu32,
2395 __func__, value);
2396 return -1;
2399 /* Now both sides are active. */
2400 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2401 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2403 /* Notify send thread that time to continue send pages */
2404 qemu_sem_post(&s->rp_state.rp_sem);
2406 return 0;
2410 * Handles messages sent on the return path towards the source VM
2413 static void *source_return_path_thread(void *opaque)
2415 MigrationState *ms = opaque;
2416 QEMUFile *rp = ms->rp_state.from_dst_file;
2417 uint16_t header_len, header_type;
2418 uint8_t buf[512];
2419 uint32_t tmp32, sibling_error;
2420 ram_addr_t start = 0; /* =0 to silence warning */
2421 size_t len = 0, expected_len;
2422 int res;
2424 trace_source_return_path_thread_entry();
2425 rcu_register_thread();
2427 retry:
2428 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
2429 migration_is_setup_or_active(ms->state)) {
2430 trace_source_return_path_thread_loop_top();
2431 header_type = qemu_get_be16(rp);
2432 header_len = qemu_get_be16(rp);
2434 if (qemu_file_get_error(rp)) {
2435 mark_source_rp_bad(ms);
2436 goto out;
2439 if (header_type >= MIG_RP_MSG_MAX ||
2440 header_type == MIG_RP_MSG_INVALID) {
2441 error_report("RP: Received invalid message 0x%04x length 0x%04x",
2442 header_type, header_len);
2443 mark_source_rp_bad(ms);
2444 goto out;
2447 if ((rp_cmd_args[header_type].len != -1 &&
2448 header_len != rp_cmd_args[header_type].len) ||
2449 header_len > sizeof(buf)) {
2450 error_report("RP: Received '%s' message (0x%04x) with"
2451 "incorrect length %d expecting %zu",
2452 rp_cmd_args[header_type].name, header_type, header_len,
2453 (size_t)rp_cmd_args[header_type].len);
2454 mark_source_rp_bad(ms);
2455 goto out;
2458 /* We know we've got a valid header by this point */
2459 res = qemu_get_buffer(rp, buf, header_len);
2460 if (res != header_len) {
2461 error_report("RP: Failed reading data for message 0x%04x"
2462 " read %d expected %d",
2463 header_type, res, header_len);
2464 mark_source_rp_bad(ms);
2465 goto out;
2468 /* OK, we have the message and the data */
2469 switch (header_type) {
2470 case MIG_RP_MSG_SHUT:
2471 sibling_error = ldl_be_p(buf);
2472 trace_source_return_path_thread_shut(sibling_error);
2473 if (sibling_error) {
2474 error_report("RP: Sibling indicated error %d", sibling_error);
2475 mark_source_rp_bad(ms);
2478 * We'll let the main thread deal with closing the RP
2479 * we could do a shutdown(2) on it, but we're the only user
2480 * anyway, so there's nothing gained.
2482 goto out;
2484 case MIG_RP_MSG_PONG:
2485 tmp32 = ldl_be_p(buf);
2486 trace_source_return_path_thread_pong(tmp32);
2487 break;
2489 case MIG_RP_MSG_REQ_PAGES:
2490 start = ldq_be_p(buf);
2491 len = ldl_be_p(buf + 8);
2492 migrate_handle_rp_req_pages(ms, NULL, start, len);
2493 break;
2495 case MIG_RP_MSG_REQ_PAGES_ID:
2496 expected_len = 12 + 1; /* header + termination */
2498 if (header_len >= expected_len) {
2499 start = ldq_be_p(buf);
2500 len = ldl_be_p(buf + 8);
2501 /* Now we expect an idstr */
2502 tmp32 = buf[12]; /* Length of the following idstr */
2503 buf[13 + tmp32] = '\0';
2504 expected_len += tmp32;
2506 if (header_len != expected_len) {
2507 error_report("RP: Req_Page_id with length %d expecting %zd",
2508 header_len, expected_len);
2509 mark_source_rp_bad(ms);
2510 goto out;
2512 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2513 break;
2515 case MIG_RP_MSG_RECV_BITMAP:
2516 if (header_len < 1) {
2517 error_report("%s: missing block name", __func__);
2518 mark_source_rp_bad(ms);
2519 goto out;
2521 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2522 buf[buf[0] + 1] = '\0';
2523 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2524 mark_source_rp_bad(ms);
2525 goto out;
2527 break;
2529 case MIG_RP_MSG_RESUME_ACK:
2530 tmp32 = ldl_be_p(buf);
2531 if (migrate_handle_rp_resume_ack(ms, tmp32)) {
2532 mark_source_rp_bad(ms);
2533 goto out;
2535 break;
2537 default:
2538 break;
2542 out:
2543 res = qemu_file_get_error(rp);
2544 if (res) {
2545 if (res == -EIO && migration_in_postcopy()) {
2547 * Maybe there is something we can do: it looks like a
2548 * network down issue, and we pause for a recovery.
2550 if (postcopy_pause_return_path_thread(ms)) {
2551 /* Reload rp, reset the rest */
2552 if (rp != ms->rp_state.from_dst_file) {
2553 qemu_fclose(rp);
2554 rp = ms->rp_state.from_dst_file;
2556 ms->rp_state.error = false;
2557 goto retry;
2561 trace_source_return_path_thread_bad_end();
2562 mark_source_rp_bad(ms);
2565 trace_source_return_path_thread_end();
2566 ms->rp_state.from_dst_file = NULL;
2567 qemu_fclose(rp);
2568 rcu_unregister_thread();
2569 return NULL;
2572 static int open_return_path_on_source(MigrationState *ms,
2573 bool create_thread)
2576 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
2577 if (!ms->rp_state.from_dst_file) {
2578 return -1;
2581 trace_open_return_path_on_source();
2583 if (!create_thread) {
2584 /* We're done */
2585 return 0;
2588 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2589 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2591 trace_open_return_path_on_source_continue();
2593 return 0;
2596 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2597 static int await_return_path_close_on_source(MigrationState *ms)
2600 * If this is a normal exit then the destination will send a SHUT and the
2601 * rp_thread will exit, however if there's an error we need to cause
2602 * it to exit.
2604 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
2606 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2607 * waiting for the destination.
2609 qemu_file_shutdown(ms->rp_state.from_dst_file);
2610 mark_source_rp_bad(ms);
2612 trace_await_return_path_close_on_source_joining();
2613 qemu_thread_join(&ms->rp_state.rp_thread);
2614 trace_await_return_path_close_on_source_close();
2615 return ms->rp_state.error;
2619 * Switch from normal iteration to postcopy
2620 * Returns non-0 on error
2622 static int postcopy_start(MigrationState *ms)
2624 int ret;
2625 QIOChannelBuffer *bioc;
2626 QEMUFile *fb;
2627 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2628 int64_t bandwidth = migrate_max_postcopy_bandwidth();
2629 bool restart_block = false;
2630 int cur_state = MIGRATION_STATUS_ACTIVE;
2631 if (!migrate_pause_before_switchover()) {
2632 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2633 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2636 trace_postcopy_start();
2637 qemu_mutex_lock_iothread();
2638 trace_postcopy_start_set_run();
2640 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
2641 global_state_store();
2642 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
2643 if (ret < 0) {
2644 goto fail;
2647 ret = migration_maybe_pause(ms, &cur_state,
2648 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2649 if (ret < 0) {
2650 goto fail;
2653 ret = bdrv_inactivate_all();
2654 if (ret < 0) {
2655 goto fail;
2657 restart_block = true;
2660 * Cause any non-postcopiable, but iterative devices to
2661 * send out their final data.
2663 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
2666 * in Finish migrate and with the io-lock held everything should
2667 * be quiet, but we've potentially still got dirty pages and we
2668 * need to tell the destination to throw any pages it's already received
2669 * that are dirty
2671 if (migrate_postcopy_ram()) {
2672 if (ram_postcopy_send_discard_bitmap(ms)) {
2673 error_report("postcopy send discard bitmap failed");
2674 goto fail;
2679 * send rest of state - note things that are doing postcopy
2680 * will notice we're in POSTCOPY_ACTIVE and not actually
2681 * wrap their state up here
2683 /* 0 max-postcopy-bandwidth means unlimited */
2684 if (!bandwidth) {
2685 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
2686 } else {
2687 qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO);
2689 if (migrate_postcopy_ram()) {
2690 /* Ping just for debugging, helps line traces up */
2691 qemu_savevm_send_ping(ms->to_dst_file, 2);
2695 * While loading the device state we may trigger page transfer
2696 * requests and the fd must be free to process those, and thus
2697 * the destination must read the whole device state off the fd before
2698 * it starts processing it. Unfortunately the ad-hoc migration format
2699 * doesn't allow the destination to know the size to read without fully
2700 * parsing it through each devices load-state code (especially the open
2701 * coded devices that use get/put).
2702 * So we wrap the device state up in a package with a length at the start;
2703 * to do this we use a qemu_buf to hold the whole of the device state.
2705 bioc = qio_channel_buffer_new(4096);
2706 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
2707 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2708 object_unref(OBJECT(bioc));
2711 * Make sure the receiver can get incoming pages before we send the rest
2712 * of the state
2714 qemu_savevm_send_postcopy_listen(fb);
2716 qemu_savevm_state_complete_precopy(fb, false, false);
2717 if (migrate_postcopy_ram()) {
2718 qemu_savevm_send_ping(fb, 3);
2721 qemu_savevm_send_postcopy_run(fb);
2723 /* <><> end of stuff going into the package */
2725 /* Last point of recovery; as soon as we send the package the destination
2726 * can open devices and potentially start running.
2727 * Lets just check again we've not got any errors.
2729 ret = qemu_file_get_error(ms->to_dst_file);
2730 if (ret) {
2731 error_report("postcopy_start: Migration stream errored (pre package)");
2732 goto fail_closefb;
2735 restart_block = false;
2737 /* Now send that blob */
2738 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
2739 goto fail_closefb;
2741 qemu_fclose(fb);
2743 /* Send a notify to give a chance for anything that needs to happen
2744 * at the transition to postcopy and after the device state; in particular
2745 * spice needs to trigger a transition now
2747 ms->postcopy_after_devices = true;
2748 notifier_list_notify(&migration_state_notifiers, ms);
2750 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2752 qemu_mutex_unlock_iothread();
2754 if (migrate_postcopy_ram()) {
2756 * Although this ping is just for debug, it could potentially be
2757 * used for getting a better measurement of downtime at the source.
2759 qemu_savevm_send_ping(ms->to_dst_file, 4);
2762 if (migrate_release_ram()) {
2763 ram_postcopy_migrated_memory_release(ms);
2766 ret = qemu_file_get_error(ms->to_dst_file);
2767 if (ret) {
2768 error_report("postcopy_start: Migration stream errored");
2769 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2770 MIGRATION_STATUS_FAILED);
2773 return ret;
2775 fail_closefb:
2776 qemu_fclose(fb);
2777 fail:
2778 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2779 MIGRATION_STATUS_FAILED);
2780 if (restart_block) {
2781 /* A failure happened early enough that we know the destination hasn't
2782 * accessed block devices, so we're safe to recover.
2784 Error *local_err = NULL;
2786 bdrv_invalidate_cache_all(&local_err);
2787 if (local_err) {
2788 error_report_err(local_err);
2791 qemu_mutex_unlock_iothread();
2792 return -1;
2796 * migration_maybe_pause: Pause if required to by
2797 * migrate_pause_before_switchover called with the iothread locked
2798 * Returns: 0 on success
2800 static int migration_maybe_pause(MigrationState *s,
2801 int *current_active_state,
2802 int new_state)
2804 if (!migrate_pause_before_switchover()) {
2805 return 0;
2808 /* Since leaving this state is not atomic with posting the semaphore
2809 * it's possible that someone could have issued multiple migrate_continue
2810 * and the semaphore is incorrectly positive at this point;
2811 * the docs say it's undefined to reinit a semaphore that's already
2812 * init'd, so use timedwait to eat up any existing posts.
2814 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2815 /* This block intentionally left blank */
2819 * If the migration is cancelled when it is in the completion phase,
2820 * the migration state is set to MIGRATION_STATUS_CANCELLING.
2821 * So we don't need to wait a semaphore, otherwise we would always
2822 * wait for the 'pause_sem' semaphore.
2824 if (s->state != MIGRATION_STATUS_CANCELLING) {
2825 qemu_mutex_unlock_iothread();
2826 migrate_set_state(&s->state, *current_active_state,
2827 MIGRATION_STATUS_PRE_SWITCHOVER);
2828 qemu_sem_wait(&s->pause_sem);
2829 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
2830 new_state);
2831 *current_active_state = new_state;
2832 qemu_mutex_lock_iothread();
2835 return s->state == new_state ? 0 : -EINVAL;
2839 * migration_completion: Used by migration_thread when there's not much left.
2840 * The caller 'breaks' the loop when this returns.
2842 * @s: Current migration state
2844 static void migration_completion(MigrationState *s)
2846 int ret;
2847 int current_active_state = s->state;
2849 if (s->state == MIGRATION_STATUS_ACTIVE) {
2850 qemu_mutex_lock_iothread();
2851 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2852 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
2853 s->vm_was_running = runstate_is_running();
2854 ret = global_state_store();
2856 if (!ret) {
2857 bool inactivate = !migrate_colo_enabled();
2858 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
2859 if (ret >= 0) {
2860 ret = migration_maybe_pause(s, &current_active_state,
2861 MIGRATION_STATUS_DEVICE);
2863 if (ret >= 0) {
2864 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
2865 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2866 inactivate);
2868 if (inactivate && ret >= 0) {
2869 s->block_inactive = true;
2872 qemu_mutex_unlock_iothread();
2874 if (ret < 0) {
2875 goto fail;
2877 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2878 trace_migration_completion_postcopy_end();
2880 qemu_savevm_state_complete_postcopy(s->to_dst_file);
2881 trace_migration_completion_postcopy_end_after_complete();
2885 * If rp was opened we must clean up the thread before
2886 * cleaning everything else up (since if there are no failures
2887 * it will wait for the destination to send it's status in
2888 * a SHUT command).
2890 if (s->rp_state.from_dst_file) {
2891 int rp_error;
2892 trace_migration_return_path_end_before();
2893 rp_error = await_return_path_close_on_source(s);
2894 trace_migration_return_path_end_after(rp_error);
2895 if (rp_error) {
2896 goto fail_invalidate;
2900 if (qemu_file_get_error(s->to_dst_file)) {
2901 trace_migration_completion_file_err();
2902 goto fail_invalidate;
2905 if (!migrate_colo_enabled()) {
2906 migrate_set_state(&s->state, current_active_state,
2907 MIGRATION_STATUS_COMPLETED);
2910 return;
2912 fail_invalidate:
2913 /* If not doing postcopy, vm_start() will be called: let's regain
2914 * control on images.
2916 if (s->state == MIGRATION_STATUS_ACTIVE ||
2917 s->state == MIGRATION_STATUS_DEVICE) {
2918 Error *local_err = NULL;
2920 qemu_mutex_lock_iothread();
2921 bdrv_invalidate_cache_all(&local_err);
2922 if (local_err) {
2923 error_report_err(local_err);
2924 } else {
2925 s->block_inactive = false;
2927 qemu_mutex_unlock_iothread();
2930 fail:
2931 migrate_set_state(&s->state, current_active_state,
2932 MIGRATION_STATUS_FAILED);
2935 bool migrate_colo_enabled(void)
2937 MigrationState *s = migrate_get_current();
2938 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
2941 typedef enum MigThrError {
2942 /* No error detected */
2943 MIG_THR_ERR_NONE = 0,
2944 /* Detected error, but resumed successfully */
2945 MIG_THR_ERR_RECOVERED = 1,
2946 /* Detected fatal error, need to exit */
2947 MIG_THR_ERR_FATAL = 2,
2948 } MigThrError;
2950 static int postcopy_resume_handshake(MigrationState *s)
2952 qemu_savevm_send_postcopy_resume(s->to_dst_file);
2954 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
2955 qemu_sem_wait(&s->rp_state.rp_sem);
2958 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2959 return 0;
2962 return -1;
2965 /* Return zero if success, or <0 for error */
2966 static int postcopy_do_resume(MigrationState *s)
2968 int ret;
2971 * Call all the resume_prepare() hooks, so that modules can be
2972 * ready for the migration resume.
2974 ret = qemu_savevm_state_resume_prepare(s);
2975 if (ret) {
2976 error_report("%s: resume_prepare() failure detected: %d",
2977 __func__, ret);
2978 return ret;
2982 * Last handshake with destination on the resume (destination will
2983 * switch to postcopy-active afterwards)
2985 ret = postcopy_resume_handshake(s);
2986 if (ret) {
2987 error_report("%s: handshake failed: %d", __func__, ret);
2988 return ret;
2991 return 0;
2995 * We don't return until we are in a safe state to continue current
2996 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
2997 * MIG_THR_ERR_FATAL if unrecovery failure happened.
2999 static MigThrError postcopy_pause(MigrationState *s)
3001 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
3003 while (true) {
3004 QEMUFile *file;
3006 migrate_set_state(&s->state, s->state,
3007 MIGRATION_STATUS_POSTCOPY_PAUSED);
3009 /* Current channel is possibly broken. Release it. */
3010 assert(s->to_dst_file);
3011 qemu_mutex_lock(&s->qemu_file_lock);
3012 file = s->to_dst_file;
3013 s->to_dst_file = NULL;
3014 qemu_mutex_unlock(&s->qemu_file_lock);
3016 qemu_file_shutdown(file);
3017 qemu_fclose(file);
3019 error_report("Detected IO failure for postcopy. "
3020 "Migration paused.");
3023 * We wait until things fixed up. Then someone will setup the
3024 * status back for us.
3026 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
3027 qemu_sem_wait(&s->postcopy_pause_sem);
3030 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3031 /* Woken up by a recover procedure. Give it a shot */
3034 * Firstly, let's wake up the return path now, with a new
3035 * return path channel.
3037 qemu_sem_post(&s->postcopy_pause_rp_sem);
3039 /* Do the resume logic */
3040 if (postcopy_do_resume(s) == 0) {
3041 /* Let's continue! */
3042 trace_postcopy_pause_continued();
3043 return MIG_THR_ERR_RECOVERED;
3044 } else {
3046 * Something wrong happened during the recovery, let's
3047 * pause again. Pause is always better than throwing
3048 * data away.
3050 continue;
3052 } else {
3053 /* This is not right... Time to quit. */
3054 return MIG_THR_ERR_FATAL;
3059 static MigThrError migration_detect_error(MigrationState *s)
3061 int ret;
3062 int state = s->state;
3063 Error *local_error = NULL;
3065 if (state == MIGRATION_STATUS_CANCELLING ||
3066 state == MIGRATION_STATUS_CANCELLED) {
3067 /* End the migration, but don't set the state to failed */
3068 return MIG_THR_ERR_FATAL;
3071 /* Try to detect any file errors */
3072 ret = qemu_file_get_error_obj(s->to_dst_file, &local_error);
3073 if (!ret) {
3074 /* Everything is fine */
3075 assert(!local_error);
3076 return MIG_THR_ERR_NONE;
3079 if (local_error) {
3080 migrate_set_error(s, local_error);
3081 error_free(local_error);
3084 if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
3086 * For postcopy, we allow the network to be down for a
3087 * while. After that, it can be continued by a
3088 * recovery phase.
3090 return postcopy_pause(s);
3091 } else {
3093 * For precopy (or postcopy with error outside IO), we fail
3094 * with no time.
3096 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
3097 trace_migration_thread_file_err();
3099 /* Time to stop the migration, now. */
3100 return MIG_THR_ERR_FATAL;
3104 /* How many bytes have we transferred since the beginning of the migration */
3105 static uint64_t migration_total_bytes(MigrationState *s)
3107 return qemu_ftell(s->to_dst_file) + ram_counters.multifd_bytes;
3110 static void migration_calculate_complete(MigrationState *s)
3112 uint64_t bytes = migration_total_bytes(s);
3113 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3114 int64_t transfer_time;
3116 s->total_time = end_time - s->start_time;
3117 if (!s->downtime) {
3119 * It's still not set, so we are precopy migration. For
3120 * postcopy, downtime is calculated during postcopy_start().
3122 s->downtime = end_time - s->downtime_start;
3125 transfer_time = s->total_time - s->setup_time;
3126 if (transfer_time) {
3127 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
3131 static void update_iteration_initial_status(MigrationState *s)
3134 * Update these three fields at the same time to avoid mismatch info lead
3135 * wrong speed calculation.
3137 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3138 s->iteration_initial_bytes = migration_total_bytes(s);
3139 s->iteration_initial_pages = ram_get_total_transferred_pages();
3142 static void migration_update_counters(MigrationState *s,
3143 int64_t current_time)
3145 uint64_t transferred, transferred_pages, time_spent;
3146 uint64_t current_bytes; /* bytes transferred since the beginning */
3147 double bandwidth;
3149 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
3150 return;
3153 current_bytes = migration_total_bytes(s);
3154 transferred = current_bytes - s->iteration_initial_bytes;
3155 time_spent = current_time - s->iteration_start_time;
3156 bandwidth = (double)transferred / time_spent;
3157 s->threshold_size = bandwidth * s->parameters.downtime_limit;
3159 s->mbps = (((double) transferred * 8.0) /
3160 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
3162 transferred_pages = ram_get_total_transferred_pages() -
3163 s->iteration_initial_pages;
3164 s->pages_per_second = (double) transferred_pages /
3165 (((double) time_spent / 1000.0));
3168 * if we haven't sent anything, we don't want to
3169 * recalculate. 10000 is a small enough number for our purposes
3171 if (ram_counters.dirty_pages_rate && transferred > 10000) {
3172 s->expected_downtime = ram_counters.remaining / bandwidth;
3175 qemu_file_reset_rate_limit(s->to_dst_file);
3177 update_iteration_initial_status(s);
3179 trace_migrate_transferred(transferred, time_spent,
3180 bandwidth, s->threshold_size);
3183 /* Migration thread iteration status */
3184 typedef enum {
3185 MIG_ITERATE_RESUME, /* Resume current iteration */
3186 MIG_ITERATE_SKIP, /* Skip current iteration */
3187 MIG_ITERATE_BREAK, /* Break the loop */
3188 } MigIterateState;
3191 * Return true if continue to the next iteration directly, false
3192 * otherwise.
3194 static MigIterateState migration_iteration_run(MigrationState *s)
3196 uint64_t pending_size, pend_pre, pend_compat, pend_post;
3197 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
3199 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
3200 &pend_compat, &pend_post);
3201 pending_size = pend_pre + pend_compat + pend_post;
3203 trace_migrate_pending(pending_size, s->threshold_size,
3204 pend_pre, pend_compat, pend_post);
3206 if (pending_size && pending_size >= s->threshold_size) {
3207 /* Still a significant amount to transfer */
3208 if (!in_postcopy && pend_pre <= s->threshold_size &&
3209 atomic_read(&s->start_postcopy)) {
3210 if (postcopy_start(s)) {
3211 error_report("%s: postcopy failed to start", __func__);
3213 return MIG_ITERATE_SKIP;
3215 /* Just another iteration step */
3216 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
3217 } else {
3218 trace_migration_thread_low_pending(pending_size);
3219 migration_completion(s);
3220 return MIG_ITERATE_BREAK;
3223 return MIG_ITERATE_RESUME;
3226 static void migration_iteration_finish(MigrationState *s)
3228 /* If we enabled cpu throttling for auto-converge, turn it off. */
3229 cpu_throttle_stop();
3231 qemu_mutex_lock_iothread();
3232 switch (s->state) {
3233 case MIGRATION_STATUS_COMPLETED:
3234 migration_calculate_complete(s);
3235 runstate_set(RUN_STATE_POSTMIGRATE);
3236 break;
3238 case MIGRATION_STATUS_ACTIVE:
3240 * We should really assert here, but since it's during
3241 * migration, let's try to reduce the usage of assertions.
3243 if (!migrate_colo_enabled()) {
3244 error_report("%s: critical error: calling COLO code without "
3245 "COLO enabled", __func__);
3247 migrate_start_colo_process(s);
3249 * Fixme: we will run VM in COLO no matter its old running state.
3250 * After exited COLO, we will keep running.
3252 s->vm_was_running = true;
3253 /* Fallthrough */
3254 case MIGRATION_STATUS_FAILED:
3255 case MIGRATION_STATUS_CANCELLED:
3256 case MIGRATION_STATUS_CANCELLING:
3257 if (s->vm_was_running) {
3258 vm_start();
3259 } else {
3260 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
3261 runstate_set(RUN_STATE_POSTMIGRATE);
3264 break;
3266 default:
3267 /* Should not reach here, but if so, forgive the VM. */
3268 error_report("%s: Unknown ending state %d", __func__, s->state);
3269 break;
3271 migrate_fd_cleanup_schedule(s);
3272 qemu_mutex_unlock_iothread();
3275 void migration_make_urgent_request(void)
3277 qemu_sem_post(&migrate_get_current()->rate_limit_sem);
3280 void migration_consume_urgent_request(void)
3282 qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
3285 /* Returns true if the rate limiting was broken by an urgent request */
3286 bool migration_rate_limit(void)
3288 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3289 MigrationState *s = migrate_get_current();
3291 bool urgent = false;
3292 migration_update_counters(s, now);
3293 if (qemu_file_rate_limit(s->to_dst_file)) {
3295 * Wait for a delay to do rate limiting OR
3296 * something urgent to post the semaphore.
3298 int ms = s->iteration_start_time + BUFFER_DELAY - now;
3299 trace_migration_rate_limit_pre(ms);
3300 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
3302 * We were woken by one or more urgent things but
3303 * the timedwait will have consumed one of them.
3304 * The service routine for the urgent wake will dec
3305 * the semaphore itself for each item it consumes,
3306 * so add this one we just eat back.
3308 qemu_sem_post(&s->rate_limit_sem);
3309 urgent = true;
3311 trace_migration_rate_limit_post(urgent);
3313 return urgent;
3317 * Master migration thread on the source VM.
3318 * It drives the migration and pumps the data down the outgoing channel.
3320 static void *migration_thread(void *opaque)
3322 MigrationState *s = opaque;
3323 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
3324 MigThrError thr_error;
3325 bool urgent = false;
3327 rcu_register_thread();
3329 object_ref(OBJECT(s));
3330 update_iteration_initial_status(s);
3332 qemu_savevm_state_header(s->to_dst_file);
3335 * If we opened the return path, we need to make sure dst has it
3336 * opened as well.
3338 if (s->rp_state.from_dst_file) {
3339 /* Now tell the dest that it should open its end so it can reply */
3340 qemu_savevm_send_open_return_path(s->to_dst_file);
3342 /* And do a ping that will make stuff easier to debug */
3343 qemu_savevm_send_ping(s->to_dst_file, 1);
3346 if (migrate_postcopy()) {
3348 * Tell the destination that we *might* want to do postcopy later;
3349 * if the other end can't do postcopy it should fail now, nice and
3350 * early.
3352 qemu_savevm_send_postcopy_advise(s->to_dst_file);
3355 if (migrate_colo_enabled()) {
3356 /* Notify migration destination that we enable COLO */
3357 qemu_savevm_send_colo_enable(s->to_dst_file);
3360 qemu_savevm_state_setup(s->to_dst_file);
3362 if (qemu_savevm_state_guest_unplug_pending()) {
3363 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3364 MIGRATION_STATUS_WAIT_UNPLUG);
3366 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
3367 qemu_savevm_state_guest_unplug_pending()) {
3368 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3371 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG,
3372 MIGRATION_STATUS_ACTIVE);
3375 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
3376 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3377 MIGRATION_STATUS_ACTIVE);
3379 trace_migration_thread_setup_complete();
3381 while (migration_is_active(s)) {
3382 if (urgent || !qemu_file_rate_limit(s->to_dst_file)) {
3383 MigIterateState iter_state = migration_iteration_run(s);
3384 if (iter_state == MIG_ITERATE_SKIP) {
3385 continue;
3386 } else if (iter_state == MIG_ITERATE_BREAK) {
3387 break;
3392 * Try to detect any kind of failures, and see whether we
3393 * should stop the migration now.
3395 thr_error = migration_detect_error(s);
3396 if (thr_error == MIG_THR_ERR_FATAL) {
3397 /* Stop migration */
3398 break;
3399 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
3401 * Just recovered from a e.g. network failure, reset all
3402 * the local variables. This is important to avoid
3403 * breaking transferred_bytes and bandwidth calculation
3405 update_iteration_initial_status(s);
3408 urgent = migration_rate_limit();
3411 trace_migration_thread_after_loop();
3412 migration_iteration_finish(s);
3413 object_unref(OBJECT(s));
3414 rcu_unregister_thread();
3415 return NULL;
3418 void migrate_fd_connect(MigrationState *s, Error *error_in)
3420 Error *local_err = NULL;
3421 int64_t rate_limit;
3422 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
3424 s->expected_downtime = s->parameters.downtime_limit;
3425 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s);
3426 if (error_in) {
3427 migrate_fd_error(s, error_in);
3428 migrate_fd_cleanup(s);
3429 return;
3432 if (resume) {
3433 /* This is a resumed migration */
3434 rate_limit = s->parameters.max_postcopy_bandwidth /
3435 XFER_LIMIT_RATIO;
3436 } else {
3437 /* This is a fresh new migration */
3438 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
3440 /* Notify before starting migration thread */
3441 notifier_list_notify(&migration_state_notifiers, s);
3444 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
3445 qemu_file_set_blocking(s->to_dst_file, true);
3448 * Open the return path. For postcopy, it is used exclusively. For
3449 * precopy, only if user specified "return-path" capability would
3450 * QEMU uses the return path.
3452 if (migrate_postcopy_ram() || migrate_use_return_path()) {
3453 if (open_return_path_on_source(s, !resume)) {
3454 error_report("Unable to open return-path for postcopy");
3455 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
3456 migrate_fd_cleanup(s);
3457 return;
3461 if (resume) {
3462 /* Wakeup the main migration thread to do the recovery */
3463 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
3464 MIGRATION_STATUS_POSTCOPY_RECOVER);
3465 qemu_sem_post(&s->postcopy_pause_sem);
3466 return;
3469 if (multifd_save_setup(&local_err) != 0) {
3470 error_report_err(local_err);
3471 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3472 MIGRATION_STATUS_FAILED);
3473 migrate_fd_cleanup(s);
3474 return;
3476 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
3477 QEMU_THREAD_JOINABLE);
3478 s->migration_thread_running = true;
3481 void migration_global_dump(Monitor *mon)
3483 MigrationState *ms = migrate_get_current();
3485 monitor_printf(mon, "globals:\n");
3486 monitor_printf(mon, "store-global-state: %s\n",
3487 ms->store_global_state ? "on" : "off");
3488 monitor_printf(mon, "only-migratable: %s\n",
3489 only_migratable ? "on" : "off");
3490 monitor_printf(mon, "send-configuration: %s\n",
3491 ms->send_configuration ? "on" : "off");
3492 monitor_printf(mon, "send-section-footer: %s\n",
3493 ms->send_section_footer ? "on" : "off");
3494 monitor_printf(mon, "decompress-error-check: %s\n",
3495 ms->decompress_error_check ? "on" : "off");
3496 monitor_printf(mon, "clear-bitmap-shift: %u\n",
3497 ms->clear_bitmap_shift);
3500 #define DEFINE_PROP_MIG_CAP(name, x) \
3501 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
3503 static Property migration_properties[] = {
3504 DEFINE_PROP_BOOL("store-global-state", MigrationState,
3505 store_global_state, true),
3506 DEFINE_PROP_BOOL("send-configuration", MigrationState,
3507 send_configuration, true),
3508 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
3509 send_section_footer, true),
3510 DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
3511 decompress_error_check, true),
3512 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
3513 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
3515 /* Migration parameters */
3516 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
3517 parameters.compress_level,
3518 DEFAULT_MIGRATE_COMPRESS_LEVEL),
3519 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
3520 parameters.compress_threads,
3521 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
3522 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
3523 parameters.compress_wait_thread, true),
3524 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
3525 parameters.decompress_threads,
3526 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
3527 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
3528 parameters.cpu_throttle_initial,
3529 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
3530 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
3531 parameters.cpu_throttle_increment,
3532 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
3533 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
3534 parameters.max_bandwidth, MAX_THROTTLE),
3535 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
3536 parameters.downtime_limit,
3537 DEFAULT_MIGRATE_SET_DOWNTIME),
3538 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
3539 parameters.x_checkpoint_delay,
3540 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
3541 DEFINE_PROP_UINT8("multifd-channels", MigrationState,
3542 parameters.multifd_channels,
3543 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
3544 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
3545 parameters.multifd_compression,
3546 DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
3547 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
3548 parameters.xbzrle_cache_size,
3549 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
3550 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
3551 parameters.max_postcopy_bandwidth,
3552 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
3553 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
3554 parameters.max_cpu_throttle,
3555 DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
3556 DEFINE_PROP_SIZE("announce-initial", MigrationState,
3557 parameters.announce_initial,
3558 DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
3559 DEFINE_PROP_SIZE("announce-max", MigrationState,
3560 parameters.announce_max,
3561 DEFAULT_MIGRATE_ANNOUNCE_MAX),
3562 DEFINE_PROP_SIZE("announce-rounds", MigrationState,
3563 parameters.announce_rounds,
3564 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
3565 DEFINE_PROP_SIZE("announce-step", MigrationState,
3566 parameters.announce_step,
3567 DEFAULT_MIGRATE_ANNOUNCE_STEP),
3569 /* Migration capabilities */
3570 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
3571 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
3572 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
3573 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
3574 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
3575 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
3576 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
3577 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
3578 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
3579 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
3580 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
3581 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
3583 DEFINE_PROP_END_OF_LIST(),
3586 static void migration_class_init(ObjectClass *klass, void *data)
3588 DeviceClass *dc = DEVICE_CLASS(klass);
3590 dc->user_creatable = false;
3591 device_class_set_props(dc, migration_properties);
3594 static void migration_instance_finalize(Object *obj)
3596 MigrationState *ms = MIGRATION_OBJ(obj);
3597 MigrationParameters *params = &ms->parameters;
3599 qemu_mutex_destroy(&ms->error_mutex);
3600 qemu_mutex_destroy(&ms->qemu_file_lock);
3601 g_free(params->tls_hostname);
3602 g_free(params->tls_creds);
3603 qemu_sem_destroy(&ms->wait_unplug_sem);
3604 qemu_sem_destroy(&ms->rate_limit_sem);
3605 qemu_sem_destroy(&ms->pause_sem);
3606 qemu_sem_destroy(&ms->postcopy_pause_sem);
3607 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
3608 qemu_sem_destroy(&ms->rp_state.rp_sem);
3609 error_free(ms->error);
3612 static void migration_instance_init(Object *obj)
3614 MigrationState *ms = MIGRATION_OBJ(obj);
3615 MigrationParameters *params = &ms->parameters;
3617 ms->state = MIGRATION_STATUS_NONE;
3618 ms->mbps = -1;
3619 ms->pages_per_second = -1;
3620 qemu_sem_init(&ms->pause_sem, 0);
3621 qemu_mutex_init(&ms->error_mutex);
3623 params->tls_hostname = g_strdup("");
3624 params->tls_creds = g_strdup("");
3626 /* Set has_* up only for parameter checks */
3627 params->has_compress_level = true;
3628 params->has_compress_threads = true;
3629 params->has_decompress_threads = true;
3630 params->has_cpu_throttle_initial = true;
3631 params->has_cpu_throttle_increment = true;
3632 params->has_max_bandwidth = true;
3633 params->has_downtime_limit = true;
3634 params->has_x_checkpoint_delay = true;
3635 params->has_block_incremental = true;
3636 params->has_multifd_channels = true;
3637 params->has_multifd_compression = true;
3638 params->has_xbzrle_cache_size = true;
3639 params->has_max_postcopy_bandwidth = true;
3640 params->has_max_cpu_throttle = true;
3641 params->has_announce_initial = true;
3642 params->has_announce_max = true;
3643 params->has_announce_rounds = true;
3644 params->has_announce_step = true;
3646 qemu_sem_init(&ms->postcopy_pause_sem, 0);
3647 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
3648 qemu_sem_init(&ms->rp_state.rp_sem, 0);
3649 qemu_sem_init(&ms->rate_limit_sem, 0);
3650 qemu_sem_init(&ms->wait_unplug_sem, 0);
3651 qemu_mutex_init(&ms->qemu_file_lock);
3655 * Return true if check pass, false otherwise. Error will be put
3656 * inside errp if provided.
3658 static bool migration_object_check(MigrationState *ms, Error **errp)
3660 MigrationCapabilityStatusList *head = NULL;
3661 /* Assuming all off */
3662 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
3663 int i;
3665 if (!migrate_params_check(&ms->parameters, errp)) {
3666 return false;
3669 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
3670 if (ms->enabled_capabilities[i]) {
3671 head = migrate_cap_add(head, i, true);
3675 ret = migrate_caps_check(cap_list, head, errp);
3677 /* It works with head == NULL */
3678 qapi_free_MigrationCapabilityStatusList(head);
3680 return ret;
3683 static const TypeInfo migration_type = {
3684 .name = TYPE_MIGRATION,
3686 * NOTE: TYPE_MIGRATION is not really a device, as the object is
3687 * not created using qdev_create(), it is not attached to the qdev
3688 * device tree, and it is never realized.
3690 * TODO: Make this TYPE_OBJECT once QOM provides something like
3691 * TYPE_DEVICE's "-global" properties.
3693 .parent = TYPE_DEVICE,
3694 .class_init = migration_class_init,
3695 .class_size = sizeof(MigrationClass),
3696 .instance_size = sizeof(MigrationState),
3697 .instance_init = migration_instance_init,
3698 .instance_finalize = migration_instance_finalize,
3701 static void register_migration_types(void)
3703 type_register_static(&migration_type);
3706 type_init(register_migration_types);