hw/arm/allwinner-h3: add System Control module
[qemu.git] / migration / migration.c
blob0b2045ccbd2a738332448280032593375477721e
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
92 /* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
93 #define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
94 /* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
95 #define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
97 /* Background transfer rate for postcopy, 0 means unlimited, note
98 * that page requests can still exceed this limit.
100 #define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
103 * Parameters for self_announce_delay giving a stream of RARP/ARP
104 * packets after migration.
106 #define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50
107 #define DEFAULT_MIGRATE_ANNOUNCE_MAX 550
108 #define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5
109 #define DEFAULT_MIGRATE_ANNOUNCE_STEP 100
111 static NotifierList migration_state_notifiers =
112 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
114 static bool deferred_incoming;
116 /* Messages sent on the return path from destination to source */
117 enum mig_rp_message_type {
118 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
119 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
120 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
122 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
123 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
124 MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */
125 MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */
127 MIG_RP_MSG_MAX
130 /* When we add fault tolerance, we could have several
131 migrations at once. For now we don't need to add
132 dynamic creation of migration */
134 static MigrationState *current_migration;
135 static MigrationIncomingState *current_incoming;
137 static bool migration_object_check(MigrationState *ms, Error **errp);
138 static int migration_maybe_pause(MigrationState *s,
139 int *current_active_state,
140 int new_state);
141 static void migrate_fd_cancel(MigrationState *s);
143 void migration_object_init(void)
145 MachineState *ms = MACHINE(qdev_get_machine());
146 Error *err = NULL;
148 /* This can only be called once. */
149 assert(!current_migration);
150 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
153 * Init the migrate incoming object as well no matter whether
154 * we'll use it or not.
156 assert(!current_incoming);
157 current_incoming = g_new0(MigrationIncomingState, 1);
158 current_incoming->state = MIGRATION_STATUS_NONE;
159 current_incoming->postcopy_remote_fds =
160 g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD));
161 qemu_mutex_init(&current_incoming->rp_mutex);
162 qemu_event_init(&current_incoming->main_thread_load_event, false);
163 qemu_sem_init(&current_incoming->postcopy_pause_sem_dst, 0);
164 qemu_sem_init(&current_incoming->postcopy_pause_sem_fault, 0);
166 init_dirty_bitmap_incoming_migration();
168 if (!migration_object_check(current_migration, &err)) {
169 error_report_err(err);
170 exit(1);
174 * We cannot really do this in migration_instance_init() since at
175 * that time global properties are not yet applied, then this
176 * value will be definitely replaced by something else.
178 if (ms->enforce_config_section) {
179 current_migration->send_configuration = true;
183 void migration_shutdown(void)
186 * Cancel the current migration - that will (eventually)
187 * stop the migration using this structure
189 migrate_fd_cancel(current_migration);
190 object_unref(OBJECT(current_migration));
193 /* For outgoing */
194 MigrationState *migrate_get_current(void)
196 /* This can only be called after the object created. */
197 assert(current_migration);
198 return current_migration;
201 MigrationIncomingState *migration_incoming_get_current(void)
203 assert(current_incoming);
204 return current_incoming;
207 void migration_incoming_state_destroy(void)
209 struct MigrationIncomingState *mis = migration_incoming_get_current();
211 if (mis->to_src_file) {
212 /* Tell source that we are done */
213 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
214 qemu_fclose(mis->to_src_file);
215 mis->to_src_file = NULL;
218 if (mis->from_src_file) {
219 qemu_fclose(mis->from_src_file);
220 mis->from_src_file = NULL;
222 if (mis->postcopy_remote_fds) {
223 g_array_free(mis->postcopy_remote_fds, TRUE);
224 mis->postcopy_remote_fds = NULL;
227 qemu_event_reset(&mis->main_thread_load_event);
229 if (mis->socket_address_list) {
230 qapi_free_SocketAddressList(mis->socket_address_list);
231 mis->socket_address_list = NULL;
235 static void migrate_generate_event(int new_state)
237 if (migrate_use_events()) {
238 qapi_event_send_migration(new_state);
242 static bool migrate_late_block_activate(void)
244 MigrationState *s;
246 s = migrate_get_current();
248 return s->enabled_capabilities[
249 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
253 * Called on -incoming with a defer: uri.
254 * The migration can be started later after any parameters have been
255 * changed.
257 static void deferred_incoming_migration(Error **errp)
259 if (deferred_incoming) {
260 error_setg(errp, "Incoming migration already deferred");
262 deferred_incoming = true;
266 * Send a message on the return channel back to the source
267 * of the migration.
269 static int migrate_send_rp_message(MigrationIncomingState *mis,
270 enum mig_rp_message_type message_type,
271 uint16_t len, void *data)
273 int ret = 0;
275 trace_migrate_send_rp_message((int)message_type, len);
276 qemu_mutex_lock(&mis->rp_mutex);
279 * It's possible that the file handle got lost due to network
280 * failures.
282 if (!mis->to_src_file) {
283 ret = -EIO;
284 goto error;
287 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
288 qemu_put_be16(mis->to_src_file, len);
289 qemu_put_buffer(mis->to_src_file, data, len);
290 qemu_fflush(mis->to_src_file);
292 /* It's possible that qemu file got error during sending */
293 ret = qemu_file_get_error(mis->to_src_file);
295 error:
296 qemu_mutex_unlock(&mis->rp_mutex);
297 return ret;
300 /* Request a range of pages from the source VM at the given
301 * start address.
302 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
303 * as the last request (a name must have been given previously)
304 * Start: Address offset within the RB
305 * Len: Length in bytes required - must be a multiple of pagesize
307 int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
308 ram_addr_t start, size_t len)
310 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
311 size_t msglen = 12; /* start + len */
312 enum mig_rp_message_type msg_type;
314 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
315 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
317 if (rbname) {
318 int rbname_len = strlen(rbname);
319 assert(rbname_len < 256);
321 bufc[msglen++] = rbname_len;
322 memcpy(bufc + msglen, rbname, rbname_len);
323 msglen += rbname_len;
324 msg_type = MIG_RP_MSG_REQ_PAGES_ID;
325 } else {
326 msg_type = MIG_RP_MSG_REQ_PAGES;
329 return migrate_send_rp_message(mis, msg_type, msglen, bufc);
332 static bool migration_colo_enabled;
333 bool migration_incoming_colo_enabled(void)
335 return migration_colo_enabled;
338 void migration_incoming_disable_colo(void)
340 migration_colo_enabled = false;
343 void migration_incoming_enable_colo(void)
345 migration_colo_enabled = true;
348 void migrate_add_address(SocketAddress *address)
350 MigrationIncomingState *mis = migration_incoming_get_current();
351 SocketAddressList *addrs;
353 addrs = g_new0(SocketAddressList, 1);
354 addrs->next = mis->socket_address_list;
355 mis->socket_address_list = addrs;
356 addrs->value = QAPI_CLONE(SocketAddress, address);
359 void qemu_start_incoming_migration(const char *uri, Error **errp)
361 const char *p;
363 qapi_event_send_migration(MIGRATION_STATUS_SETUP);
364 if (!strcmp(uri, "defer")) {
365 deferred_incoming_migration(errp);
366 } else if (strstart(uri, "tcp:", &p)) {
367 tcp_start_incoming_migration(p, errp);
368 #ifdef CONFIG_RDMA
369 } else if (strstart(uri, "rdma:", &p)) {
370 rdma_start_incoming_migration(p, errp);
371 #endif
372 } else if (strstart(uri, "exec:", &p)) {
373 exec_start_incoming_migration(p, errp);
374 } else if (strstart(uri, "unix:", &p)) {
375 unix_start_incoming_migration(p, errp);
376 } else if (strstart(uri, "fd:", &p)) {
377 fd_start_incoming_migration(p, errp);
378 } else {
379 error_setg(errp, "unknown migration protocol: %s", uri);
383 static void process_incoming_migration_bh(void *opaque)
385 Error *local_err = NULL;
386 MigrationIncomingState *mis = opaque;
388 /* If capability late_block_activate is set:
389 * Only fire up the block code now if we're going to restart the
390 * VM, else 'cont' will do it.
391 * This causes file locking to happen; so we don't want it to happen
392 * unless we really are starting the VM.
394 if (!migrate_late_block_activate() ||
395 (autostart && (!global_state_received() ||
396 global_state_get_runstate() == RUN_STATE_RUNNING))) {
397 /* Make sure all file formats flush their mutable metadata.
398 * If we get an error here, just don't restart the VM yet. */
399 bdrv_invalidate_cache_all(&local_err);
400 if (local_err) {
401 error_report_err(local_err);
402 local_err = NULL;
403 autostart = false;
408 * This must happen after all error conditions are dealt with and
409 * we're sure the VM is going to be running on this host.
411 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
413 if (multifd_load_cleanup(&local_err) != 0) {
414 error_report_err(local_err);
415 autostart = false;
417 /* If global state section was not received or we are in running
418 state, we need to obey autostart. Any other state is set with
419 runstate_set. */
421 dirty_bitmap_mig_before_vm_start();
423 if (!global_state_received() ||
424 global_state_get_runstate() == RUN_STATE_RUNNING) {
425 if (autostart) {
426 vm_start();
427 } else {
428 runstate_set(RUN_STATE_PAUSED);
430 } else if (migration_incoming_colo_enabled()) {
431 migration_incoming_disable_colo();
432 vm_start();
433 } else {
434 runstate_set(global_state_get_runstate());
437 * This must happen after any state changes since as soon as an external
438 * observer sees this event they might start to prod at the VM assuming
439 * it's ready to use.
441 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
442 MIGRATION_STATUS_COMPLETED);
443 qemu_bh_delete(mis->bh);
444 migration_incoming_state_destroy();
447 static void process_incoming_migration_co(void *opaque)
449 MigrationIncomingState *mis = migration_incoming_get_current();
450 PostcopyState ps;
451 int ret;
452 Error *local_err = NULL;
454 assert(mis->from_src_file);
455 mis->migration_incoming_co = qemu_coroutine_self();
456 mis->largest_page_size = qemu_ram_pagesize_largest();
457 postcopy_state_set(POSTCOPY_INCOMING_NONE);
458 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
459 MIGRATION_STATUS_ACTIVE);
460 ret = qemu_loadvm_state(mis->from_src_file);
462 ps = postcopy_state_get();
463 trace_process_incoming_migration_co_end(ret, ps);
464 if (ps != POSTCOPY_INCOMING_NONE) {
465 if (ps == POSTCOPY_INCOMING_ADVISE) {
467 * Where a migration had postcopy enabled (and thus went to advise)
468 * but managed to complete within the precopy period, we can use
469 * the normal exit.
471 postcopy_ram_incoming_cleanup(mis);
472 } else if (ret >= 0) {
474 * Postcopy was started, cleanup should happen at the end of the
475 * postcopy thread.
477 trace_process_incoming_migration_co_postcopy_end_main();
478 return;
480 /* Else if something went wrong then just fall out of the normal exit */
483 /* we get COLO info, and know if we are in COLO mode */
484 if (!ret && migration_incoming_colo_enabled()) {
485 /* Make sure all file formats flush their mutable metadata */
486 bdrv_invalidate_cache_all(&local_err);
487 if (local_err) {
488 error_report_err(local_err);
489 goto fail;
492 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
493 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
494 mis->have_colo_incoming_thread = true;
495 qemu_coroutine_yield();
497 /* Wait checkpoint incoming thread exit before free resource */
498 qemu_thread_join(&mis->colo_incoming_thread);
499 /* We hold the global iothread lock, so it is safe here */
500 colo_release_ram_cache();
503 if (ret < 0) {
504 error_report("load of migration failed: %s", strerror(-ret));
505 goto fail;
507 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
508 qemu_bh_schedule(mis->bh);
509 mis->migration_incoming_co = NULL;
510 return;
511 fail:
512 local_err = NULL;
513 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
514 MIGRATION_STATUS_FAILED);
515 qemu_fclose(mis->from_src_file);
516 if (multifd_load_cleanup(&local_err) != 0) {
517 error_report_err(local_err);
519 exit(EXIT_FAILURE);
523 * @migration_incoming_setup: Setup incoming migration
525 * Returns 0 for no error or 1 for error
527 * @f: file for main migration channel
528 * @errp: where to put errors
530 static int migration_incoming_setup(QEMUFile *f, Error **errp)
532 MigrationIncomingState *mis = migration_incoming_get_current();
533 Error *local_err = NULL;
535 if (multifd_load_setup(&local_err) != 0) {
536 /* We haven't been able to create multifd threads
537 nothing better to do */
538 error_report_err(local_err);
539 exit(EXIT_FAILURE);
542 if (!mis->from_src_file) {
543 mis->from_src_file = f;
545 qemu_file_set_blocking(f, false);
546 return 0;
549 void migration_incoming_process(void)
551 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
552 qemu_coroutine_enter(co);
555 /* Returns true if recovered from a paused migration, otherwise false */
556 static bool postcopy_try_recover(QEMUFile *f)
558 MigrationIncomingState *mis = migration_incoming_get_current();
560 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
561 /* Resumed from a paused postcopy migration */
563 mis->from_src_file = f;
564 /* Postcopy has standalone thread to do vm load */
565 qemu_file_set_blocking(f, true);
567 /* Re-configure the return path */
568 mis->to_src_file = qemu_file_get_return_path(f);
570 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
571 MIGRATION_STATUS_POSTCOPY_RECOVER);
574 * Here, we only wake up the main loading thread (while the
575 * fault thread will still be waiting), so that we can receive
576 * commands from source now, and answer it if needed. The
577 * fault thread will be woken up afterwards until we are sure
578 * that source is ready to reply to page requests.
580 qemu_sem_post(&mis->postcopy_pause_sem_dst);
581 return true;
584 return false;
587 void migration_fd_process_incoming(QEMUFile *f, Error **errp)
589 Error *local_err = NULL;
591 if (postcopy_try_recover(f)) {
592 return;
595 if (migration_incoming_setup(f, &local_err)) {
596 if (local_err) {
597 error_propagate(errp, local_err);
599 return;
601 migration_incoming_process();
604 void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp)
606 MigrationIncomingState *mis = migration_incoming_get_current();
607 Error *local_err = NULL;
608 bool start_migration;
610 if (!mis->from_src_file) {
611 /* The first connection (multifd may have multiple) */
612 QEMUFile *f = qemu_fopen_channel_input(ioc);
614 /* If it's a recovery, we're done */
615 if (postcopy_try_recover(f)) {
616 return;
619 if (migration_incoming_setup(f, &local_err)) {
620 if (local_err) {
621 error_propagate(errp, local_err);
623 return;
627 * Common migration only needs one channel, so we can start
628 * right now. Multifd needs more than one channel, we wait.
630 start_migration = !migrate_use_multifd();
631 } else {
632 /* Multiple connections */
633 assert(migrate_use_multifd());
634 start_migration = multifd_recv_new_channel(ioc, &local_err);
635 if (local_err) {
636 error_propagate(errp, local_err);
637 return;
641 if (start_migration) {
642 migration_incoming_process();
647 * @migration_has_all_channels: We have received all channels that we need
649 * Returns true when we have got connections to all the channels that
650 * we need for migration.
652 bool migration_has_all_channels(void)
654 MigrationIncomingState *mis = migration_incoming_get_current();
655 bool all_channels;
657 all_channels = multifd_recv_all_channels_created();
659 return all_channels && mis->from_src_file != NULL;
663 * Send a 'SHUT' message on the return channel with the given value
664 * to indicate that we've finished with the RP. Non-0 value indicates
665 * error.
667 void migrate_send_rp_shut(MigrationIncomingState *mis,
668 uint32_t value)
670 uint32_t buf;
672 buf = cpu_to_be32(value);
673 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
677 * Send a 'PONG' message on the return channel with the given value
678 * (normally in response to a 'PING')
680 void migrate_send_rp_pong(MigrationIncomingState *mis,
681 uint32_t value)
683 uint32_t buf;
685 buf = cpu_to_be32(value);
686 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
689 void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
690 char *block_name)
692 char buf[512];
693 int len;
694 int64_t res;
697 * First, we send the header part. It contains only the len of
698 * idstr, and the idstr itself.
700 len = strlen(block_name);
701 buf[0] = len;
702 memcpy(buf + 1, block_name, len);
704 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
705 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
706 __func__);
707 return;
710 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
713 * Next, we dump the received bitmap to the stream.
715 * TODO: currently we are safe since we are the only one that is
716 * using the to_src_file handle (fault thread is still paused),
717 * and it's ok even not taking the mutex. However the best way is
718 * to take the lock before sending the message header, and release
719 * the lock after sending the bitmap.
721 qemu_mutex_lock(&mis->rp_mutex);
722 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
723 qemu_mutex_unlock(&mis->rp_mutex);
725 trace_migrate_send_rp_recv_bitmap(block_name, res);
728 void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
730 uint32_t buf;
732 buf = cpu_to_be32(value);
733 migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
736 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
738 MigrationCapabilityStatusList *head = NULL;
739 MigrationCapabilityStatusList *caps;
740 MigrationState *s = migrate_get_current();
741 int i;
743 caps = NULL; /* silence compiler warning */
744 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
745 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
746 if (i == MIGRATION_CAPABILITY_BLOCK) {
747 continue;
749 #endif
750 if (head == NULL) {
751 head = g_malloc0(sizeof(*caps));
752 caps = head;
753 } else {
754 caps->next = g_malloc0(sizeof(*caps));
755 caps = caps->next;
757 caps->value =
758 g_malloc(sizeof(*caps->value));
759 caps->value->capability = i;
760 caps->value->state = s->enabled_capabilities[i];
763 return head;
766 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
768 MigrationParameters *params;
769 MigrationState *s = migrate_get_current();
771 /* TODO use QAPI_CLONE() instead of duplicating it inline */
772 params = g_malloc0(sizeof(*params));
773 params->has_compress_level = true;
774 params->compress_level = s->parameters.compress_level;
775 params->has_compress_threads = true;
776 params->compress_threads = s->parameters.compress_threads;
777 params->has_compress_wait_thread = true;
778 params->compress_wait_thread = s->parameters.compress_wait_thread;
779 params->has_decompress_threads = true;
780 params->decompress_threads = s->parameters.decompress_threads;
781 params->has_cpu_throttle_initial = true;
782 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
783 params->has_cpu_throttle_increment = true;
784 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
785 params->has_tls_creds = true;
786 params->tls_creds = g_strdup(s->parameters.tls_creds);
787 params->has_tls_hostname = true;
788 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
789 params->has_tls_authz = true;
790 params->tls_authz = g_strdup(s->parameters.tls_authz);
791 params->has_max_bandwidth = true;
792 params->max_bandwidth = s->parameters.max_bandwidth;
793 params->has_downtime_limit = true;
794 params->downtime_limit = s->parameters.downtime_limit;
795 params->has_x_checkpoint_delay = true;
796 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
797 params->has_block_incremental = true;
798 params->block_incremental = s->parameters.block_incremental;
799 params->has_multifd_channels = true;
800 params->multifd_channels = s->parameters.multifd_channels;
801 params->has_multifd_compression = true;
802 params->multifd_compression = s->parameters.multifd_compression;
803 params->has_multifd_zlib_level = true;
804 params->multifd_zlib_level = s->parameters.multifd_zlib_level;
805 params->has_multifd_zstd_level = true;
806 params->multifd_zstd_level = s->parameters.multifd_zstd_level;
807 params->has_xbzrle_cache_size = true;
808 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
809 params->has_max_postcopy_bandwidth = true;
810 params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
811 params->has_max_cpu_throttle = true;
812 params->max_cpu_throttle = s->parameters.max_cpu_throttle;
813 params->has_announce_initial = true;
814 params->announce_initial = s->parameters.announce_initial;
815 params->has_announce_max = true;
816 params->announce_max = s->parameters.announce_max;
817 params->has_announce_rounds = true;
818 params->announce_rounds = s->parameters.announce_rounds;
819 params->has_announce_step = true;
820 params->announce_step = s->parameters.announce_step;
822 return params;
825 AnnounceParameters *migrate_announce_params(void)
827 static AnnounceParameters ap;
829 MigrationState *s = migrate_get_current();
831 ap.initial = s->parameters.announce_initial;
832 ap.max = s->parameters.announce_max;
833 ap.rounds = s->parameters.announce_rounds;
834 ap.step = s->parameters.announce_step;
836 return &ap;
840 * Return true if we're already in the middle of a migration
841 * (i.e. any of the active or setup states)
843 bool migration_is_setup_or_active(int state)
845 switch (state) {
846 case MIGRATION_STATUS_ACTIVE:
847 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
848 case MIGRATION_STATUS_POSTCOPY_PAUSED:
849 case MIGRATION_STATUS_POSTCOPY_RECOVER:
850 case MIGRATION_STATUS_SETUP:
851 case MIGRATION_STATUS_PRE_SWITCHOVER:
852 case MIGRATION_STATUS_DEVICE:
853 case MIGRATION_STATUS_WAIT_UNPLUG:
854 return true;
856 default:
857 return false;
862 bool migration_is_running(int state)
864 switch (state) {
865 case MIGRATION_STATUS_ACTIVE:
866 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
867 case MIGRATION_STATUS_POSTCOPY_PAUSED:
868 case MIGRATION_STATUS_POSTCOPY_RECOVER:
869 case MIGRATION_STATUS_SETUP:
870 case MIGRATION_STATUS_PRE_SWITCHOVER:
871 case MIGRATION_STATUS_DEVICE:
872 case MIGRATION_STATUS_WAIT_UNPLUG:
873 case MIGRATION_STATUS_CANCELLING:
874 return true;
876 default:
877 return false;
882 static void populate_time_info(MigrationInfo *info, MigrationState *s)
884 info->has_status = true;
885 info->has_setup_time = true;
886 info->setup_time = s->setup_time;
887 if (s->state == MIGRATION_STATUS_COMPLETED) {
888 info->has_total_time = true;
889 info->total_time = s->total_time;
890 info->has_downtime = true;
891 info->downtime = s->downtime;
892 } else {
893 info->has_total_time = true;
894 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
895 s->start_time;
896 info->has_expected_downtime = true;
897 info->expected_downtime = s->expected_downtime;
901 static void populate_ram_info(MigrationInfo *info, MigrationState *s)
903 info->has_ram = true;
904 info->ram = g_malloc0(sizeof(*info->ram));
905 info->ram->transferred = ram_counters.transferred;
906 info->ram->total = ram_bytes_total();
907 info->ram->duplicate = ram_counters.duplicate;
908 /* legacy value. It is not used anymore */
909 info->ram->skipped = 0;
910 info->ram->normal = ram_counters.normal;
911 info->ram->normal_bytes = ram_counters.normal *
912 qemu_target_page_size();
913 info->ram->mbps = s->mbps;
914 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
915 info->ram->postcopy_requests = ram_counters.postcopy_requests;
916 info->ram->page_size = qemu_target_page_size();
917 info->ram->multifd_bytes = ram_counters.multifd_bytes;
918 info->ram->pages_per_second = s->pages_per_second;
920 if (migrate_use_xbzrle()) {
921 info->has_xbzrle_cache = true;
922 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
923 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
924 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
925 info->xbzrle_cache->pages = xbzrle_counters.pages;
926 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
927 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
928 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
931 if (migrate_use_compression()) {
932 info->has_compression = true;
933 info->compression = g_malloc0(sizeof(*info->compression));
934 info->compression->pages = compression_counters.pages;
935 info->compression->busy = compression_counters.busy;
936 info->compression->busy_rate = compression_counters.busy_rate;
937 info->compression->compressed_size =
938 compression_counters.compressed_size;
939 info->compression->compression_rate =
940 compression_counters.compression_rate;
943 if (cpu_throttle_active()) {
944 info->has_cpu_throttle_percentage = true;
945 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
948 if (s->state != MIGRATION_STATUS_COMPLETED) {
949 info->ram->remaining = ram_bytes_remaining();
950 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
954 static void populate_disk_info(MigrationInfo *info)
956 if (blk_mig_active()) {
957 info->has_disk = true;
958 info->disk = g_malloc0(sizeof(*info->disk));
959 info->disk->transferred = blk_mig_bytes_transferred();
960 info->disk->remaining = blk_mig_bytes_remaining();
961 info->disk->total = blk_mig_bytes_total();
965 static void fill_source_migration_info(MigrationInfo *info)
967 MigrationState *s = migrate_get_current();
969 switch (s->state) {
970 case MIGRATION_STATUS_NONE:
971 /* no migration has happened ever */
972 /* do not overwrite destination migration status */
973 return;
974 break;
975 case MIGRATION_STATUS_SETUP:
976 info->has_status = true;
977 info->has_total_time = false;
978 break;
979 case MIGRATION_STATUS_ACTIVE:
980 case MIGRATION_STATUS_CANCELLING:
981 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
982 case MIGRATION_STATUS_PRE_SWITCHOVER:
983 case MIGRATION_STATUS_DEVICE:
984 case MIGRATION_STATUS_POSTCOPY_PAUSED:
985 case MIGRATION_STATUS_POSTCOPY_RECOVER:
986 /* TODO add some postcopy stats */
987 populate_time_info(info, s);
988 populate_ram_info(info, s);
989 populate_disk_info(info);
990 break;
991 case MIGRATION_STATUS_COLO:
992 info->has_status = true;
993 /* TODO: display COLO specific information (checkpoint info etc.) */
994 break;
995 case MIGRATION_STATUS_COMPLETED:
996 populate_time_info(info, s);
997 populate_ram_info(info, s);
998 break;
999 case MIGRATION_STATUS_FAILED:
1000 info->has_status = true;
1001 if (s->error) {
1002 info->has_error_desc = true;
1003 info->error_desc = g_strdup(error_get_pretty(s->error));
1005 break;
1006 case MIGRATION_STATUS_CANCELLED:
1007 info->has_status = true;
1008 break;
1009 case MIGRATION_STATUS_WAIT_UNPLUG:
1010 info->has_status = true;
1011 break;
1013 info->status = s->state;
1017 * @migration_caps_check - check capability validity
1019 * @cap_list: old capability list, array of bool
1020 * @params: new capabilities to be applied soon
1021 * @errp: set *errp if the check failed, with reason
1023 * Returns true if check passed, otherwise false.
1025 static bool migrate_caps_check(bool *cap_list,
1026 MigrationCapabilityStatusList *params,
1027 Error **errp)
1029 MigrationCapabilityStatusList *cap;
1030 bool old_postcopy_cap;
1031 MigrationIncomingState *mis = migration_incoming_get_current();
1033 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
1035 for (cap = params; cap; cap = cap->next) {
1036 cap_list[cap->value->capability] = cap->value->state;
1039 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
1040 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
1041 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
1042 "block migration");
1043 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
1044 return false;
1046 #endif
1048 #ifndef CONFIG_REPLICATION
1049 if (cap_list[MIGRATION_CAPABILITY_X_COLO]) {
1050 error_setg(errp, "QEMU compiled without replication module"
1051 " can't enable COLO");
1052 error_append_hint(errp, "Please enable replication before COLO.\n");
1053 return false;
1055 #endif
1057 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
1058 /* This check is reasonably expensive, so only when it's being
1059 * set the first time, also it's only the destination that needs
1060 * special support.
1062 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
1063 !postcopy_ram_supported_by_host(mis)) {
1064 /* postcopy_ram_supported_by_host will have emitted a more
1065 * detailed message
1067 error_setg(errp, "Postcopy is not supported");
1068 return false;
1071 if (cap_list[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
1072 error_setg(errp, "Postcopy is not compatible with ignore-shared");
1073 return false;
1077 return true;
1080 static void fill_destination_migration_info(MigrationInfo *info)
1082 MigrationIncomingState *mis = migration_incoming_get_current();
1084 if (mis->socket_address_list) {
1085 info->has_socket_address = true;
1086 info->socket_address =
1087 QAPI_CLONE(SocketAddressList, mis->socket_address_list);
1090 switch (mis->state) {
1091 case MIGRATION_STATUS_NONE:
1092 return;
1093 break;
1094 case MIGRATION_STATUS_SETUP:
1095 case MIGRATION_STATUS_CANCELLING:
1096 case MIGRATION_STATUS_CANCELLED:
1097 case MIGRATION_STATUS_ACTIVE:
1098 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1099 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1100 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1101 case MIGRATION_STATUS_FAILED:
1102 case MIGRATION_STATUS_COLO:
1103 info->has_status = true;
1104 break;
1105 case MIGRATION_STATUS_COMPLETED:
1106 info->has_status = true;
1107 fill_destination_postcopy_migration_info(info);
1108 break;
1110 info->status = mis->state;
1113 MigrationInfo *qmp_query_migrate(Error **errp)
1115 MigrationInfo *info = g_malloc0(sizeof(*info));
1117 fill_destination_migration_info(info);
1118 fill_source_migration_info(info);
1120 return info;
1123 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
1124 Error **errp)
1126 MigrationState *s = migrate_get_current();
1127 MigrationCapabilityStatusList *cap;
1128 bool cap_list[MIGRATION_CAPABILITY__MAX];
1130 if (migration_is_running(s->state)) {
1131 error_setg(errp, QERR_MIGRATION_ACTIVE);
1132 return;
1135 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
1136 if (!migrate_caps_check(cap_list, params, errp)) {
1137 return;
1140 for (cap = params; cap; cap = cap->next) {
1141 s->enabled_capabilities[cap->value->capability] = cap->value->state;
1146 * Check whether the parameters are valid. Error will be put into errp
1147 * (if provided). Return true if valid, otherwise false.
1149 static bool migrate_params_check(MigrationParameters *params, Error **errp)
1151 if (params->has_compress_level &&
1152 (params->compress_level > 9)) {
1153 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
1154 "is invalid, it should be in the range of 0 to 9");
1155 return false;
1158 if (params->has_compress_threads && (params->compress_threads < 1)) {
1159 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1160 "compress_threads",
1161 "is invalid, it should be in the range of 1 to 255");
1162 return false;
1165 if (params->has_decompress_threads && (params->decompress_threads < 1)) {
1166 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1167 "decompress_threads",
1168 "is invalid, it should be in the range of 1 to 255");
1169 return false;
1172 if (params->has_cpu_throttle_initial &&
1173 (params->cpu_throttle_initial < 1 ||
1174 params->cpu_throttle_initial > 99)) {
1175 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1176 "cpu_throttle_initial",
1177 "an integer in the range of 1 to 99");
1178 return false;
1181 if (params->has_cpu_throttle_increment &&
1182 (params->cpu_throttle_increment < 1 ||
1183 params->cpu_throttle_increment > 99)) {
1184 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1185 "cpu_throttle_increment",
1186 "an integer in the range of 1 to 99");
1187 return false;
1190 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
1191 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
1192 " range of 0 to %zu bytes/second", SIZE_MAX);
1193 return false;
1196 if (params->has_downtime_limit &&
1197 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
1198 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1199 "the range of 0 to %d milliseconds",
1200 MAX_MIGRATE_DOWNTIME);
1201 return false;
1204 /* x_checkpoint_delay is now always positive */
1206 if (params->has_multifd_channels && (params->multifd_channels < 1)) {
1207 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1208 "multifd_channels",
1209 "is invalid, it should be in the range of 1 to 255");
1210 return false;
1213 if (params->has_multifd_zlib_level &&
1214 (params->multifd_zlib_level > 9)) {
1215 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
1216 "is invalid, it should be in the range of 0 to 9");
1217 return false;
1220 if (params->has_multifd_zstd_level &&
1221 (params->multifd_zstd_level > 20)) {
1222 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
1223 "is invalid, it should be in the range of 0 to 20");
1224 return false;
1227 if (params->has_xbzrle_cache_size &&
1228 (params->xbzrle_cache_size < qemu_target_page_size() ||
1229 !is_power_of_2(params->xbzrle_cache_size))) {
1230 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1231 "xbzrle_cache_size",
1232 "is invalid, it should be bigger than target page size"
1233 " and a power of two");
1234 return false;
1237 if (params->has_max_cpu_throttle &&
1238 (params->max_cpu_throttle < params->cpu_throttle_initial ||
1239 params->max_cpu_throttle > 99)) {
1240 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1241 "max_cpu_throttle",
1242 "an integer in the range of cpu_throttle_initial to 99");
1243 return false;
1246 if (params->has_announce_initial &&
1247 params->announce_initial > 100000) {
1248 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1249 "announce_initial",
1250 "is invalid, it must be less than 100000 ms");
1251 return false;
1253 if (params->has_announce_max &&
1254 params->announce_max > 100000) {
1255 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1256 "announce_max",
1257 "is invalid, it must be less than 100000 ms");
1258 return false;
1260 if (params->has_announce_rounds &&
1261 params->announce_rounds > 1000) {
1262 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1263 "announce_rounds",
1264 "is invalid, it must be in the range of 0 to 1000");
1265 return false;
1267 if (params->has_announce_step &&
1268 (params->announce_step < 1 ||
1269 params->announce_step > 10000)) {
1270 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1271 "announce_step",
1272 "is invalid, it must be in the range of 1 to 10000 ms");
1273 return false;
1275 return true;
1278 static void migrate_params_test_apply(MigrateSetParameters *params,
1279 MigrationParameters *dest)
1281 *dest = migrate_get_current()->parameters;
1283 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1285 if (params->has_compress_level) {
1286 dest->compress_level = params->compress_level;
1289 if (params->has_compress_threads) {
1290 dest->compress_threads = params->compress_threads;
1293 if (params->has_compress_wait_thread) {
1294 dest->compress_wait_thread = params->compress_wait_thread;
1297 if (params->has_decompress_threads) {
1298 dest->decompress_threads = params->decompress_threads;
1301 if (params->has_cpu_throttle_initial) {
1302 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1305 if (params->has_cpu_throttle_increment) {
1306 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1309 if (params->has_tls_creds) {
1310 assert(params->tls_creds->type == QTYPE_QSTRING);
1311 dest->tls_creds = g_strdup(params->tls_creds->u.s);
1314 if (params->has_tls_hostname) {
1315 assert(params->tls_hostname->type == QTYPE_QSTRING);
1316 dest->tls_hostname = g_strdup(params->tls_hostname->u.s);
1319 if (params->has_max_bandwidth) {
1320 dest->max_bandwidth = params->max_bandwidth;
1323 if (params->has_downtime_limit) {
1324 dest->downtime_limit = params->downtime_limit;
1327 if (params->has_x_checkpoint_delay) {
1328 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1331 if (params->has_block_incremental) {
1332 dest->block_incremental = params->block_incremental;
1334 if (params->has_multifd_channels) {
1335 dest->multifd_channels = params->multifd_channels;
1337 if (params->has_multifd_compression) {
1338 dest->multifd_compression = params->multifd_compression;
1340 if (params->has_xbzrle_cache_size) {
1341 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1343 if (params->has_max_postcopy_bandwidth) {
1344 dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1346 if (params->has_max_cpu_throttle) {
1347 dest->max_cpu_throttle = params->max_cpu_throttle;
1349 if (params->has_announce_initial) {
1350 dest->announce_initial = params->announce_initial;
1352 if (params->has_announce_max) {
1353 dest->announce_max = params->announce_max;
1355 if (params->has_announce_rounds) {
1356 dest->announce_rounds = params->announce_rounds;
1358 if (params->has_announce_step) {
1359 dest->announce_step = params->announce_step;
1363 static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
1365 MigrationState *s = migrate_get_current();
1367 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1369 if (params->has_compress_level) {
1370 s->parameters.compress_level = params->compress_level;
1373 if (params->has_compress_threads) {
1374 s->parameters.compress_threads = params->compress_threads;
1377 if (params->has_compress_wait_thread) {
1378 s->parameters.compress_wait_thread = params->compress_wait_thread;
1381 if (params->has_decompress_threads) {
1382 s->parameters.decompress_threads = params->decompress_threads;
1385 if (params->has_cpu_throttle_initial) {
1386 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
1389 if (params->has_cpu_throttle_increment) {
1390 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
1393 if (params->has_tls_creds) {
1394 g_free(s->parameters.tls_creds);
1395 assert(params->tls_creds->type == QTYPE_QSTRING);
1396 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
1399 if (params->has_tls_hostname) {
1400 g_free(s->parameters.tls_hostname);
1401 assert(params->tls_hostname->type == QTYPE_QSTRING);
1402 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
1405 if (params->has_tls_authz) {
1406 g_free(s->parameters.tls_authz);
1407 assert(params->tls_authz->type == QTYPE_QSTRING);
1408 s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
1411 if (params->has_max_bandwidth) {
1412 s->parameters.max_bandwidth = params->max_bandwidth;
1413 if (s->to_dst_file && !migration_in_postcopy()) {
1414 qemu_file_set_rate_limit(s->to_dst_file,
1415 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
1419 if (params->has_downtime_limit) {
1420 s->parameters.downtime_limit = params->downtime_limit;
1423 if (params->has_x_checkpoint_delay) {
1424 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
1425 if (migration_in_colo_state()) {
1426 colo_checkpoint_notify(s);
1430 if (params->has_block_incremental) {
1431 s->parameters.block_incremental = params->block_incremental;
1433 if (params->has_multifd_channels) {
1434 s->parameters.multifd_channels = params->multifd_channels;
1436 if (params->has_multifd_compression) {
1437 s->parameters.multifd_compression = params->multifd_compression;
1439 if (params->has_xbzrle_cache_size) {
1440 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1441 xbzrle_cache_resize(params->xbzrle_cache_size, errp);
1443 if (params->has_max_postcopy_bandwidth) {
1444 s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1445 if (s->to_dst_file && migration_in_postcopy()) {
1446 qemu_file_set_rate_limit(s->to_dst_file,
1447 s->parameters.max_postcopy_bandwidth / XFER_LIMIT_RATIO);
1450 if (params->has_max_cpu_throttle) {
1451 s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1453 if (params->has_announce_initial) {
1454 s->parameters.announce_initial = params->announce_initial;
1456 if (params->has_announce_max) {
1457 s->parameters.announce_max = params->announce_max;
1459 if (params->has_announce_rounds) {
1460 s->parameters.announce_rounds = params->announce_rounds;
1462 if (params->has_announce_step) {
1463 s->parameters.announce_step = params->announce_step;
1467 void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
1469 MigrationParameters tmp;
1471 /* TODO Rewrite "" to null instead */
1472 if (params->has_tls_creds
1473 && params->tls_creds->type == QTYPE_QNULL) {
1474 qobject_unref(params->tls_creds->u.n);
1475 params->tls_creds->type = QTYPE_QSTRING;
1476 params->tls_creds->u.s = strdup("");
1478 /* TODO Rewrite "" to null instead */
1479 if (params->has_tls_hostname
1480 && params->tls_hostname->type == QTYPE_QNULL) {
1481 qobject_unref(params->tls_hostname->u.n);
1482 params->tls_hostname->type = QTYPE_QSTRING;
1483 params->tls_hostname->u.s = strdup("");
1486 migrate_params_test_apply(params, &tmp);
1488 if (!migrate_params_check(&tmp, errp)) {
1489 /* Invalid parameter */
1490 return;
1493 migrate_params_apply(params, errp);
1497 void qmp_migrate_start_postcopy(Error **errp)
1499 MigrationState *s = migrate_get_current();
1501 if (!migrate_postcopy()) {
1502 error_setg(errp, "Enable postcopy with migrate_set_capability before"
1503 " the start of migration");
1504 return;
1507 if (s->state == MIGRATION_STATUS_NONE) {
1508 error_setg(errp, "Postcopy must be started after migration has been"
1509 " started");
1510 return;
1513 * we don't error if migration has finished since that would be racy
1514 * with issuing this command.
1516 atomic_set(&s->start_postcopy, true);
1519 /* shared migration helpers */
1521 void migrate_set_state(int *state, int old_state, int new_state)
1523 assert(new_state < MIGRATION_STATUS__MAX);
1524 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
1525 trace_migrate_set_state(MigrationStatus_str(new_state));
1526 migrate_generate_event(new_state);
1530 static MigrationCapabilityStatusList *migrate_cap_add(
1531 MigrationCapabilityStatusList *list,
1532 MigrationCapability index,
1533 bool state)
1535 MigrationCapabilityStatusList *cap;
1537 cap = g_new0(MigrationCapabilityStatusList, 1);
1538 cap->value = g_new0(MigrationCapabilityStatus, 1);
1539 cap->value->capability = index;
1540 cap->value->state = state;
1541 cap->next = list;
1543 return cap;
1546 void migrate_set_block_enabled(bool value, Error **errp)
1548 MigrationCapabilityStatusList *cap;
1550 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
1551 qmp_migrate_set_capabilities(cap, errp);
1552 qapi_free_MigrationCapabilityStatusList(cap);
1555 static void migrate_set_block_incremental(MigrationState *s, bool value)
1557 s->parameters.block_incremental = value;
1560 static void block_cleanup_parameters(MigrationState *s)
1562 if (s->must_remove_block_options) {
1563 /* setting to false can never fail */
1564 migrate_set_block_enabled(false, &error_abort);
1565 migrate_set_block_incremental(s, false);
1566 s->must_remove_block_options = false;
1570 static void migrate_fd_cleanup(MigrationState *s)
1572 qemu_bh_delete(s->cleanup_bh);
1573 s->cleanup_bh = NULL;
1575 qemu_savevm_state_cleanup();
1577 if (s->to_dst_file) {
1578 QEMUFile *tmp;
1580 trace_migrate_fd_cleanup();
1581 qemu_mutex_unlock_iothread();
1582 if (s->migration_thread_running) {
1583 qemu_thread_join(&s->thread);
1584 s->migration_thread_running = false;
1586 qemu_mutex_lock_iothread();
1588 multifd_save_cleanup();
1589 qemu_mutex_lock(&s->qemu_file_lock);
1590 tmp = s->to_dst_file;
1591 s->to_dst_file = NULL;
1592 qemu_mutex_unlock(&s->qemu_file_lock);
1594 * Close the file handle without the lock to make sure the
1595 * critical section won't block for long.
1597 qemu_fclose(tmp);
1600 assert(!migration_is_active(s));
1602 if (s->state == MIGRATION_STATUS_CANCELLING) {
1603 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
1604 MIGRATION_STATUS_CANCELLED);
1607 if (s->error) {
1608 /* It is used on info migrate. We can't free it */
1609 error_report_err(error_copy(s->error));
1611 notifier_list_notify(&migration_state_notifiers, s);
1612 block_cleanup_parameters(s);
1615 static void migrate_fd_cleanup_schedule(MigrationState *s)
1618 * Ref the state for bh, because it may be called when
1619 * there're already no other refs
1621 object_ref(OBJECT(s));
1622 qemu_bh_schedule(s->cleanup_bh);
1625 static void migrate_fd_cleanup_bh(void *opaque)
1627 MigrationState *s = opaque;
1628 migrate_fd_cleanup(s);
1629 object_unref(OBJECT(s));
1632 void migrate_set_error(MigrationState *s, const Error *error)
1634 qemu_mutex_lock(&s->error_mutex);
1635 if (!s->error) {
1636 s->error = error_copy(error);
1638 qemu_mutex_unlock(&s->error_mutex);
1641 void migrate_fd_error(MigrationState *s, const Error *error)
1643 trace_migrate_fd_error(error_get_pretty(error));
1644 assert(s->to_dst_file == NULL);
1645 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1646 MIGRATION_STATUS_FAILED);
1647 migrate_set_error(s, error);
1650 static void migrate_fd_cancel(MigrationState *s)
1652 int old_state ;
1653 QEMUFile *f = migrate_get_current()->to_dst_file;
1654 trace_migrate_fd_cancel();
1656 if (s->rp_state.from_dst_file) {
1657 /* shutdown the rp socket, so causing the rp thread to shutdown */
1658 qemu_file_shutdown(s->rp_state.from_dst_file);
1661 do {
1662 old_state = s->state;
1663 if (!migration_is_running(old_state)) {
1664 break;
1666 /* If the migration is paused, kick it out of the pause */
1667 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
1668 qemu_sem_post(&s->pause_sem);
1670 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
1671 } while (s->state != MIGRATION_STATUS_CANCELLING);
1674 * If we're unlucky the migration code might be stuck somewhere in a
1675 * send/write while the network has failed and is waiting to timeout;
1676 * if we've got shutdown(2) available then we can force it to quit.
1677 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1678 * called in a bh, so there is no race against this cancel.
1680 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
1681 qemu_file_shutdown(f);
1683 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1684 Error *local_err = NULL;
1686 bdrv_invalidate_cache_all(&local_err);
1687 if (local_err) {
1688 error_report_err(local_err);
1689 } else {
1690 s->block_inactive = false;
1695 void add_migration_state_change_notifier(Notifier *notify)
1697 notifier_list_add(&migration_state_notifiers, notify);
1700 void remove_migration_state_change_notifier(Notifier *notify)
1702 notifier_remove(notify);
1705 bool migration_in_setup(MigrationState *s)
1707 return s->state == MIGRATION_STATUS_SETUP;
1710 bool migration_has_finished(MigrationState *s)
1712 return s->state == MIGRATION_STATUS_COMPLETED;
1715 bool migration_has_failed(MigrationState *s)
1717 return (s->state == MIGRATION_STATUS_CANCELLED ||
1718 s->state == MIGRATION_STATUS_FAILED);
1721 bool migration_in_postcopy(void)
1723 MigrationState *s = migrate_get_current();
1725 switch (s->state) {
1726 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1727 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1728 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1729 return true;
1730 default:
1731 return false;
1735 bool migration_in_postcopy_after_devices(MigrationState *s)
1737 return migration_in_postcopy() && s->postcopy_after_devices;
1740 bool migration_is_idle(void)
1742 MigrationState *s = current_migration;
1744 if (!s) {
1745 return true;
1748 switch (s->state) {
1749 case MIGRATION_STATUS_NONE:
1750 case MIGRATION_STATUS_CANCELLED:
1751 case MIGRATION_STATUS_COMPLETED:
1752 case MIGRATION_STATUS_FAILED:
1753 return true;
1754 case MIGRATION_STATUS_SETUP:
1755 case MIGRATION_STATUS_CANCELLING:
1756 case MIGRATION_STATUS_ACTIVE:
1757 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1758 case MIGRATION_STATUS_COLO:
1759 case MIGRATION_STATUS_PRE_SWITCHOVER:
1760 case MIGRATION_STATUS_DEVICE:
1761 case MIGRATION_STATUS_WAIT_UNPLUG:
1762 return false;
1763 case MIGRATION_STATUS__MAX:
1764 g_assert_not_reached();
1767 return false;
1770 bool migration_is_active(MigrationState *s)
1772 return (s->state == MIGRATION_STATUS_ACTIVE ||
1773 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1776 void migrate_init(MigrationState *s)
1779 * Reinitialise all migration state, except
1780 * parameters/capabilities that the user set, and
1781 * locks.
1783 s->cleanup_bh = 0;
1784 s->to_dst_file = NULL;
1785 s->state = MIGRATION_STATUS_NONE;
1786 s->rp_state.from_dst_file = NULL;
1787 s->rp_state.error = false;
1788 s->mbps = 0.0;
1789 s->pages_per_second = 0.0;
1790 s->downtime = 0;
1791 s->expected_downtime = 0;
1792 s->setup_time = 0;
1793 s->start_postcopy = false;
1794 s->postcopy_after_devices = false;
1795 s->migration_thread_running = false;
1796 error_free(s->error);
1797 s->error = NULL;
1799 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
1801 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1802 s->total_time = 0;
1803 s->vm_was_running = false;
1804 s->iteration_initial_bytes = 0;
1805 s->threshold_size = 0;
1808 static GSList *migration_blockers;
1810 int migrate_add_blocker(Error *reason, Error **errp)
1812 if (only_migratable) {
1813 error_propagate_prepend(errp, error_copy(reason),
1814 "disallowing migration blocker "
1815 "(--only-migratable) for: ");
1816 return -EACCES;
1819 if (migration_is_idle()) {
1820 migration_blockers = g_slist_prepend(migration_blockers, reason);
1821 return 0;
1824 error_propagate_prepend(errp, error_copy(reason),
1825 "disallowing migration blocker "
1826 "(migration in progress) for: ");
1827 return -EBUSY;
1830 void migrate_del_blocker(Error *reason)
1832 migration_blockers = g_slist_remove(migration_blockers, reason);
1835 void qmp_migrate_incoming(const char *uri, Error **errp)
1837 Error *local_err = NULL;
1838 static bool once = true;
1840 if (!deferred_incoming) {
1841 error_setg(errp, "For use with '-incoming defer'");
1842 return;
1844 if (!once) {
1845 error_setg(errp, "The incoming migration has already been started");
1846 return;
1849 qemu_start_incoming_migration(uri, &local_err);
1851 if (local_err) {
1852 error_propagate(errp, local_err);
1853 return;
1856 once = false;
1859 void qmp_migrate_recover(const char *uri, Error **errp)
1861 MigrationIncomingState *mis = migration_incoming_get_current();
1863 if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1864 error_setg(errp, "Migrate recover can only be run "
1865 "when postcopy is paused.");
1866 return;
1869 if (atomic_cmpxchg(&mis->postcopy_recover_triggered,
1870 false, true) == true) {
1871 error_setg(errp, "Migrate recovery is triggered already");
1872 return;
1876 * Note that this call will never start a real migration; it will
1877 * only re-setup the migration stream and poke existing migration
1878 * to continue using that newly established channel.
1880 qemu_start_incoming_migration(uri, errp);
1883 void qmp_migrate_pause(Error **errp)
1885 MigrationState *ms = migrate_get_current();
1886 MigrationIncomingState *mis = migration_incoming_get_current();
1887 int ret;
1889 if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1890 /* Source side, during postcopy */
1891 qemu_mutex_lock(&ms->qemu_file_lock);
1892 ret = qemu_file_shutdown(ms->to_dst_file);
1893 qemu_mutex_unlock(&ms->qemu_file_lock);
1894 if (ret) {
1895 error_setg(errp, "Failed to pause source migration");
1897 return;
1900 if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1901 ret = qemu_file_shutdown(mis->from_src_file);
1902 if (ret) {
1903 error_setg(errp, "Failed to pause destination migration");
1905 return;
1908 error_setg(errp, "migrate-pause is currently only supported "
1909 "during postcopy-active state");
1912 bool migration_is_blocked(Error **errp)
1914 if (qemu_savevm_state_blocked(errp)) {
1915 return true;
1918 if (migration_blockers) {
1919 error_propagate(errp, error_copy(migration_blockers->data));
1920 return true;
1923 return false;
1926 /* Returns true if continue to migrate, or false if error detected */
1927 static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
1928 bool resume, Error **errp)
1930 Error *local_err = NULL;
1932 if (resume) {
1933 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1934 error_setg(errp, "Cannot resume if there is no "
1935 "paused migration");
1936 return false;
1940 * Postcopy recovery won't work well with release-ram
1941 * capability since release-ram will drop the page buffer as
1942 * long as the page is put into the send buffer. So if there
1943 * is a network failure happened, any page buffers that have
1944 * not yet reached the destination VM but have already been
1945 * sent from the source VM will be lost forever. Let's refuse
1946 * the client from resuming such a postcopy migration.
1947 * Luckily release-ram was designed to only be used when src
1948 * and destination VMs are on the same host, so it should be
1949 * fine.
1951 if (migrate_release_ram()) {
1952 error_setg(errp, "Postcopy recovery cannot work "
1953 "when release-ram capability is set");
1954 return false;
1957 /* This is a resume, skip init status */
1958 return true;
1961 if (migration_is_running(s->state)) {
1962 error_setg(errp, QERR_MIGRATION_ACTIVE);
1963 return false;
1966 if (runstate_check(RUN_STATE_INMIGRATE)) {
1967 error_setg(errp, "Guest is waiting for an incoming migration");
1968 return false;
1971 if (migration_is_blocked(errp)) {
1972 return false;
1975 if (blk || blk_inc) {
1976 if (migrate_use_block() || migrate_use_block_incremental()) {
1977 error_setg(errp, "Command options are incompatible with "
1978 "current migration capabilities");
1979 return false;
1981 migrate_set_block_enabled(true, &local_err);
1982 if (local_err) {
1983 error_propagate(errp, local_err);
1984 return false;
1986 s->must_remove_block_options = true;
1989 if (blk_inc) {
1990 migrate_set_block_incremental(s, true);
1993 migrate_init(s);
1995 * set ram_counters memory to zero for a
1996 * new migration
1998 memset(&ram_counters, 0, sizeof(ram_counters));
2000 return true;
2003 void qmp_migrate(const char *uri, bool has_blk, bool blk,
2004 bool has_inc, bool inc, bool has_detach, bool detach,
2005 bool has_resume, bool resume, Error **errp)
2007 Error *local_err = NULL;
2008 MigrationState *s = migrate_get_current();
2009 const char *p;
2011 if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
2012 has_resume && resume, errp)) {
2013 /* Error detected, put into errp */
2014 return;
2017 if (strstart(uri, "tcp:", &p)) {
2018 tcp_start_outgoing_migration(s, p, &local_err);
2019 #ifdef CONFIG_RDMA
2020 } else if (strstart(uri, "rdma:", &p)) {
2021 rdma_start_outgoing_migration(s, p, &local_err);
2022 #endif
2023 } else if (strstart(uri, "exec:", &p)) {
2024 exec_start_outgoing_migration(s, p, &local_err);
2025 } else if (strstart(uri, "unix:", &p)) {
2026 unix_start_outgoing_migration(s, p, &local_err);
2027 } else if (strstart(uri, "fd:", &p)) {
2028 fd_start_outgoing_migration(s, p, &local_err);
2029 } else {
2030 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
2031 "a valid migration protocol");
2032 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2033 MIGRATION_STATUS_FAILED);
2034 block_cleanup_parameters(s);
2035 return;
2038 if (local_err) {
2039 migrate_fd_error(s, local_err);
2040 error_propagate(errp, local_err);
2041 return;
2045 void qmp_migrate_cancel(Error **errp)
2047 migrate_fd_cancel(migrate_get_current());
2050 void qmp_migrate_continue(MigrationStatus state, Error **errp)
2052 MigrationState *s = migrate_get_current();
2053 if (s->state != state) {
2054 error_setg(errp, "Migration not in expected state: %s",
2055 MigrationStatus_str(s->state));
2056 return;
2058 qemu_sem_post(&s->pause_sem);
2061 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
2063 MigrateSetParameters p = {
2064 .has_xbzrle_cache_size = true,
2065 .xbzrle_cache_size = value,
2068 qmp_migrate_set_parameters(&p, errp);
2071 int64_t qmp_query_migrate_cache_size(Error **errp)
2073 return migrate_xbzrle_cache_size();
2076 void qmp_migrate_set_speed(int64_t value, Error **errp)
2078 MigrateSetParameters p = {
2079 .has_max_bandwidth = true,
2080 .max_bandwidth = value,
2083 qmp_migrate_set_parameters(&p, errp);
2086 void qmp_migrate_set_downtime(double value, Error **errp)
2088 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
2089 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
2090 "the range of 0 to %d seconds",
2091 MAX_MIGRATE_DOWNTIME_SECONDS);
2092 return;
2095 value *= 1000; /* Convert to milliseconds */
2097 MigrateSetParameters p = {
2098 .has_downtime_limit = true,
2099 .downtime_limit = (int64_t)value,
2102 qmp_migrate_set_parameters(&p, errp);
2105 bool migrate_release_ram(void)
2107 MigrationState *s;
2109 s = migrate_get_current();
2111 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
2114 bool migrate_postcopy_ram(void)
2116 MigrationState *s;
2118 s = migrate_get_current();
2120 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
2123 bool migrate_postcopy(void)
2125 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
2128 bool migrate_auto_converge(void)
2130 MigrationState *s;
2132 s = migrate_get_current();
2134 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
2137 bool migrate_zero_blocks(void)
2139 MigrationState *s;
2141 s = migrate_get_current();
2143 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
2146 bool migrate_postcopy_blocktime(void)
2148 MigrationState *s;
2150 s = migrate_get_current();
2152 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
2155 bool migrate_use_compression(void)
2157 MigrationState *s;
2159 s = migrate_get_current();
2161 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
2164 int migrate_compress_level(void)
2166 MigrationState *s;
2168 s = migrate_get_current();
2170 return s->parameters.compress_level;
2173 int migrate_compress_threads(void)
2175 MigrationState *s;
2177 s = migrate_get_current();
2179 return s->parameters.compress_threads;
2182 int migrate_compress_wait_thread(void)
2184 MigrationState *s;
2186 s = migrate_get_current();
2188 return s->parameters.compress_wait_thread;
2191 int migrate_decompress_threads(void)
2193 MigrationState *s;
2195 s = migrate_get_current();
2197 return s->parameters.decompress_threads;
2200 bool migrate_dirty_bitmaps(void)
2202 MigrationState *s;
2204 s = migrate_get_current();
2206 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
2209 bool migrate_ignore_shared(void)
2211 MigrationState *s;
2213 s = migrate_get_current();
2215 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
2218 bool migrate_validate_uuid(void)
2220 MigrationState *s;
2222 s = migrate_get_current();
2224 return s->enabled_capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
2227 bool migrate_use_events(void)
2229 MigrationState *s;
2231 s = migrate_get_current();
2233 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
2236 bool migrate_use_multifd(void)
2238 MigrationState *s;
2240 s = migrate_get_current();
2242 return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD];
2245 bool migrate_pause_before_switchover(void)
2247 MigrationState *s;
2249 s = migrate_get_current();
2251 return s->enabled_capabilities[
2252 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
2255 int migrate_multifd_channels(void)
2257 MigrationState *s;
2259 s = migrate_get_current();
2261 return s->parameters.multifd_channels;
2264 MultiFDCompression migrate_multifd_compression(void)
2266 MigrationState *s;
2268 s = migrate_get_current();
2270 return s->parameters.multifd_compression;
2273 int migrate_multifd_zlib_level(void)
2275 MigrationState *s;
2277 s = migrate_get_current();
2279 return s->parameters.multifd_zlib_level;
2282 int migrate_multifd_zstd_level(void)
2284 MigrationState *s;
2286 s = migrate_get_current();
2288 return s->parameters.multifd_zstd_level;
2291 int migrate_use_xbzrle(void)
2293 MigrationState *s;
2295 s = migrate_get_current();
2297 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
2300 int64_t migrate_xbzrle_cache_size(void)
2302 MigrationState *s;
2304 s = migrate_get_current();
2306 return s->parameters.xbzrle_cache_size;
2309 static int64_t migrate_max_postcopy_bandwidth(void)
2311 MigrationState *s;
2313 s = migrate_get_current();
2315 return s->parameters.max_postcopy_bandwidth;
2318 bool migrate_use_block(void)
2320 MigrationState *s;
2322 s = migrate_get_current();
2324 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
2327 bool migrate_use_return_path(void)
2329 MigrationState *s;
2331 s = migrate_get_current();
2333 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
2336 bool migrate_use_block_incremental(void)
2338 MigrationState *s;
2340 s = migrate_get_current();
2342 return s->parameters.block_incremental;
2345 /* migration thread support */
2347 * Something bad happened to the RP stream, mark an error
2348 * The caller shall print or trace something to indicate why
2350 static void mark_source_rp_bad(MigrationState *s)
2352 s->rp_state.error = true;
2355 static struct rp_cmd_args {
2356 ssize_t len; /* -1 = variable */
2357 const char *name;
2358 } rp_cmd_args[] = {
2359 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
2360 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
2361 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
2362 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
2363 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
2364 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
2365 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" },
2366 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
2370 * Process a request for pages received on the return path,
2371 * We're allowed to send more than requested (e.g. to round to our page size)
2372 * and we don't need to send pages that have already been sent.
2374 static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
2375 ram_addr_t start, size_t len)
2377 long our_host_ps = qemu_real_host_page_size;
2379 trace_migrate_handle_rp_req_pages(rbname, start, len);
2382 * Since we currently insist on matching page sizes, just sanity check
2383 * we're being asked for whole host pages.
2385 if (start & (our_host_ps-1) ||
2386 (len & (our_host_ps-1))) {
2387 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
2388 " len: %zd", __func__, start, len);
2389 mark_source_rp_bad(ms);
2390 return;
2393 if (ram_save_queue_pages(rbname, start, len)) {
2394 mark_source_rp_bad(ms);
2398 /* Return true to retry, false to quit */
2399 static bool postcopy_pause_return_path_thread(MigrationState *s)
2401 trace_postcopy_pause_return_path();
2403 qemu_sem_wait(&s->postcopy_pause_rp_sem);
2405 trace_postcopy_pause_return_path_continued();
2407 return true;
2410 static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
2412 RAMBlock *block = qemu_ram_block_by_name(block_name);
2414 if (!block) {
2415 error_report("%s: invalid block name '%s'", __func__, block_name);
2416 return -EINVAL;
2419 /* Fetch the received bitmap and refresh the dirty bitmap */
2420 return ram_dirty_bitmap_reload(s, block);
2423 static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
2425 trace_source_return_path_thread_resume_ack(value);
2427 if (value != MIGRATION_RESUME_ACK_VALUE) {
2428 error_report("%s: illegal resume_ack value %"PRIu32,
2429 __func__, value);
2430 return -1;
2433 /* Now both sides are active. */
2434 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2435 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2437 /* Notify send thread that time to continue send pages */
2438 qemu_sem_post(&s->rp_state.rp_sem);
2440 return 0;
2444 * Handles messages sent on the return path towards the source VM
2447 static void *source_return_path_thread(void *opaque)
2449 MigrationState *ms = opaque;
2450 QEMUFile *rp = ms->rp_state.from_dst_file;
2451 uint16_t header_len, header_type;
2452 uint8_t buf[512];
2453 uint32_t tmp32, sibling_error;
2454 ram_addr_t start = 0; /* =0 to silence warning */
2455 size_t len = 0, expected_len;
2456 int res;
2458 trace_source_return_path_thread_entry();
2459 rcu_register_thread();
2461 retry:
2462 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
2463 migration_is_setup_or_active(ms->state)) {
2464 trace_source_return_path_thread_loop_top();
2465 header_type = qemu_get_be16(rp);
2466 header_len = qemu_get_be16(rp);
2468 if (qemu_file_get_error(rp)) {
2469 mark_source_rp_bad(ms);
2470 goto out;
2473 if (header_type >= MIG_RP_MSG_MAX ||
2474 header_type == MIG_RP_MSG_INVALID) {
2475 error_report("RP: Received invalid message 0x%04x length 0x%04x",
2476 header_type, header_len);
2477 mark_source_rp_bad(ms);
2478 goto out;
2481 if ((rp_cmd_args[header_type].len != -1 &&
2482 header_len != rp_cmd_args[header_type].len) ||
2483 header_len > sizeof(buf)) {
2484 error_report("RP: Received '%s' message (0x%04x) with"
2485 "incorrect length %d expecting %zu",
2486 rp_cmd_args[header_type].name, header_type, header_len,
2487 (size_t)rp_cmd_args[header_type].len);
2488 mark_source_rp_bad(ms);
2489 goto out;
2492 /* We know we've got a valid header by this point */
2493 res = qemu_get_buffer(rp, buf, header_len);
2494 if (res != header_len) {
2495 error_report("RP: Failed reading data for message 0x%04x"
2496 " read %d expected %d",
2497 header_type, res, header_len);
2498 mark_source_rp_bad(ms);
2499 goto out;
2502 /* OK, we have the message and the data */
2503 switch (header_type) {
2504 case MIG_RP_MSG_SHUT:
2505 sibling_error = ldl_be_p(buf);
2506 trace_source_return_path_thread_shut(sibling_error);
2507 if (sibling_error) {
2508 error_report("RP: Sibling indicated error %d", sibling_error);
2509 mark_source_rp_bad(ms);
2512 * We'll let the main thread deal with closing the RP
2513 * we could do a shutdown(2) on it, but we're the only user
2514 * anyway, so there's nothing gained.
2516 goto out;
2518 case MIG_RP_MSG_PONG:
2519 tmp32 = ldl_be_p(buf);
2520 trace_source_return_path_thread_pong(tmp32);
2521 break;
2523 case MIG_RP_MSG_REQ_PAGES:
2524 start = ldq_be_p(buf);
2525 len = ldl_be_p(buf + 8);
2526 migrate_handle_rp_req_pages(ms, NULL, start, len);
2527 break;
2529 case MIG_RP_MSG_REQ_PAGES_ID:
2530 expected_len = 12 + 1; /* header + termination */
2532 if (header_len >= expected_len) {
2533 start = ldq_be_p(buf);
2534 len = ldl_be_p(buf + 8);
2535 /* Now we expect an idstr */
2536 tmp32 = buf[12]; /* Length of the following idstr */
2537 buf[13 + tmp32] = '\0';
2538 expected_len += tmp32;
2540 if (header_len != expected_len) {
2541 error_report("RP: Req_Page_id with length %d expecting %zd",
2542 header_len, expected_len);
2543 mark_source_rp_bad(ms);
2544 goto out;
2546 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
2547 break;
2549 case MIG_RP_MSG_RECV_BITMAP:
2550 if (header_len < 1) {
2551 error_report("%s: missing block name", __func__);
2552 mark_source_rp_bad(ms);
2553 goto out;
2555 /* Format: len (1B) + idstr (<255B). This ends the idstr. */
2556 buf[buf[0] + 1] = '\0';
2557 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
2558 mark_source_rp_bad(ms);
2559 goto out;
2561 break;
2563 case MIG_RP_MSG_RESUME_ACK:
2564 tmp32 = ldl_be_p(buf);
2565 if (migrate_handle_rp_resume_ack(ms, tmp32)) {
2566 mark_source_rp_bad(ms);
2567 goto out;
2569 break;
2571 default:
2572 break;
2576 out:
2577 res = qemu_file_get_error(rp);
2578 if (res) {
2579 if (res == -EIO && migration_in_postcopy()) {
2581 * Maybe there is something we can do: it looks like a
2582 * network down issue, and we pause for a recovery.
2584 if (postcopy_pause_return_path_thread(ms)) {
2585 /* Reload rp, reset the rest */
2586 if (rp != ms->rp_state.from_dst_file) {
2587 qemu_fclose(rp);
2588 rp = ms->rp_state.from_dst_file;
2590 ms->rp_state.error = false;
2591 goto retry;
2595 trace_source_return_path_thread_bad_end();
2596 mark_source_rp_bad(ms);
2599 trace_source_return_path_thread_end();
2600 ms->rp_state.from_dst_file = NULL;
2601 qemu_fclose(rp);
2602 rcu_unregister_thread();
2603 return NULL;
2606 static int open_return_path_on_source(MigrationState *ms,
2607 bool create_thread)
2610 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
2611 if (!ms->rp_state.from_dst_file) {
2612 return -1;
2615 trace_open_return_path_on_source();
2617 if (!create_thread) {
2618 /* We're done */
2619 return 0;
2622 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
2623 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
2625 trace_open_return_path_on_source_continue();
2627 return 0;
2630 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
2631 static int await_return_path_close_on_source(MigrationState *ms)
2634 * If this is a normal exit then the destination will send a SHUT and the
2635 * rp_thread will exit, however if there's an error we need to cause
2636 * it to exit.
2638 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
2640 * shutdown(2), if we have it, will cause it to unblock if it's stuck
2641 * waiting for the destination.
2643 qemu_file_shutdown(ms->rp_state.from_dst_file);
2644 mark_source_rp_bad(ms);
2646 trace_await_return_path_close_on_source_joining();
2647 qemu_thread_join(&ms->rp_state.rp_thread);
2648 trace_await_return_path_close_on_source_close();
2649 return ms->rp_state.error;
2653 * Switch from normal iteration to postcopy
2654 * Returns non-0 on error
2656 static int postcopy_start(MigrationState *ms)
2658 int ret;
2659 QIOChannelBuffer *bioc;
2660 QEMUFile *fb;
2661 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2662 int64_t bandwidth = migrate_max_postcopy_bandwidth();
2663 bool restart_block = false;
2664 int cur_state = MIGRATION_STATUS_ACTIVE;
2665 if (!migrate_pause_before_switchover()) {
2666 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
2667 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2670 trace_postcopy_start();
2671 qemu_mutex_lock_iothread();
2672 trace_postcopy_start_set_run();
2674 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
2675 global_state_store();
2676 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
2677 if (ret < 0) {
2678 goto fail;
2681 ret = migration_maybe_pause(ms, &cur_state,
2682 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2683 if (ret < 0) {
2684 goto fail;
2687 ret = bdrv_inactivate_all();
2688 if (ret < 0) {
2689 goto fail;
2691 restart_block = true;
2694 * Cause any non-postcopiable, but iterative devices to
2695 * send out their final data.
2697 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
2700 * in Finish migrate and with the io-lock held everything should
2701 * be quiet, but we've potentially still got dirty pages and we
2702 * need to tell the destination to throw any pages it's already received
2703 * that are dirty
2705 if (migrate_postcopy_ram()) {
2706 if (ram_postcopy_send_discard_bitmap(ms)) {
2707 error_report("postcopy send discard bitmap failed");
2708 goto fail;
2713 * send rest of state - note things that are doing postcopy
2714 * will notice we're in POSTCOPY_ACTIVE and not actually
2715 * wrap their state up here
2717 /* 0 max-postcopy-bandwidth means unlimited */
2718 if (!bandwidth) {
2719 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
2720 } else {
2721 qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO);
2723 if (migrate_postcopy_ram()) {
2724 /* Ping just for debugging, helps line traces up */
2725 qemu_savevm_send_ping(ms->to_dst_file, 2);
2729 * While loading the device state we may trigger page transfer
2730 * requests and the fd must be free to process those, and thus
2731 * the destination must read the whole device state off the fd before
2732 * it starts processing it. Unfortunately the ad-hoc migration format
2733 * doesn't allow the destination to know the size to read without fully
2734 * parsing it through each devices load-state code (especially the open
2735 * coded devices that use get/put).
2736 * So we wrap the device state up in a package with a length at the start;
2737 * to do this we use a qemu_buf to hold the whole of the device state.
2739 bioc = qio_channel_buffer_new(4096);
2740 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
2741 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
2742 object_unref(OBJECT(bioc));
2745 * Make sure the receiver can get incoming pages before we send the rest
2746 * of the state
2748 qemu_savevm_send_postcopy_listen(fb);
2750 qemu_savevm_state_complete_precopy(fb, false, false);
2751 if (migrate_postcopy_ram()) {
2752 qemu_savevm_send_ping(fb, 3);
2755 qemu_savevm_send_postcopy_run(fb);
2757 /* <><> end of stuff going into the package */
2759 /* Last point of recovery; as soon as we send the package the destination
2760 * can open devices and potentially start running.
2761 * Lets just check again we've not got any errors.
2763 ret = qemu_file_get_error(ms->to_dst_file);
2764 if (ret) {
2765 error_report("postcopy_start: Migration stream errored (pre package)");
2766 goto fail_closefb;
2769 restart_block = false;
2771 /* Now send that blob */
2772 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
2773 goto fail_closefb;
2775 qemu_fclose(fb);
2777 /* Send a notify to give a chance for anything that needs to happen
2778 * at the transition to postcopy and after the device state; in particular
2779 * spice needs to trigger a transition now
2781 ms->postcopy_after_devices = true;
2782 notifier_list_notify(&migration_state_notifiers, ms);
2784 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
2786 qemu_mutex_unlock_iothread();
2788 if (migrate_postcopy_ram()) {
2790 * Although this ping is just for debug, it could potentially be
2791 * used for getting a better measurement of downtime at the source.
2793 qemu_savevm_send_ping(ms->to_dst_file, 4);
2796 if (migrate_release_ram()) {
2797 ram_postcopy_migrated_memory_release(ms);
2800 ret = qemu_file_get_error(ms->to_dst_file);
2801 if (ret) {
2802 error_report("postcopy_start: Migration stream errored");
2803 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2804 MIGRATION_STATUS_FAILED);
2807 return ret;
2809 fail_closefb:
2810 qemu_fclose(fb);
2811 fail:
2812 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2813 MIGRATION_STATUS_FAILED);
2814 if (restart_block) {
2815 /* A failure happened early enough that we know the destination hasn't
2816 * accessed block devices, so we're safe to recover.
2818 Error *local_err = NULL;
2820 bdrv_invalidate_cache_all(&local_err);
2821 if (local_err) {
2822 error_report_err(local_err);
2825 qemu_mutex_unlock_iothread();
2826 return -1;
2830 * migration_maybe_pause: Pause if required to by
2831 * migrate_pause_before_switchover called with the iothread locked
2832 * Returns: 0 on success
2834 static int migration_maybe_pause(MigrationState *s,
2835 int *current_active_state,
2836 int new_state)
2838 if (!migrate_pause_before_switchover()) {
2839 return 0;
2842 /* Since leaving this state is not atomic with posting the semaphore
2843 * it's possible that someone could have issued multiple migrate_continue
2844 * and the semaphore is incorrectly positive at this point;
2845 * the docs say it's undefined to reinit a semaphore that's already
2846 * init'd, so use timedwait to eat up any existing posts.
2848 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
2849 /* This block intentionally left blank */
2853 * If the migration is cancelled when it is in the completion phase,
2854 * the migration state is set to MIGRATION_STATUS_CANCELLING.
2855 * So we don't need to wait a semaphore, otherwise we would always
2856 * wait for the 'pause_sem' semaphore.
2858 if (s->state != MIGRATION_STATUS_CANCELLING) {
2859 qemu_mutex_unlock_iothread();
2860 migrate_set_state(&s->state, *current_active_state,
2861 MIGRATION_STATUS_PRE_SWITCHOVER);
2862 qemu_sem_wait(&s->pause_sem);
2863 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
2864 new_state);
2865 *current_active_state = new_state;
2866 qemu_mutex_lock_iothread();
2869 return s->state == new_state ? 0 : -EINVAL;
2873 * migration_completion: Used by migration_thread when there's not much left.
2874 * The caller 'breaks' the loop when this returns.
2876 * @s: Current migration state
2878 static void migration_completion(MigrationState *s)
2880 int ret;
2881 int current_active_state = s->state;
2883 if (s->state == MIGRATION_STATUS_ACTIVE) {
2884 qemu_mutex_lock_iothread();
2885 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2886 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
2887 s->vm_was_running = runstate_is_running();
2888 ret = global_state_store();
2890 if (!ret) {
2891 bool inactivate = !migrate_colo_enabled();
2892 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
2893 if (ret >= 0) {
2894 ret = migration_maybe_pause(s, &current_active_state,
2895 MIGRATION_STATUS_DEVICE);
2897 if (ret >= 0) {
2898 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
2899 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
2900 inactivate);
2902 if (inactivate && ret >= 0) {
2903 s->block_inactive = true;
2906 qemu_mutex_unlock_iothread();
2908 if (ret < 0) {
2909 goto fail;
2911 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2912 trace_migration_completion_postcopy_end();
2914 qemu_savevm_state_complete_postcopy(s->to_dst_file);
2915 trace_migration_completion_postcopy_end_after_complete();
2919 * If rp was opened we must clean up the thread before
2920 * cleaning everything else up (since if there are no failures
2921 * it will wait for the destination to send it's status in
2922 * a SHUT command).
2924 if (s->rp_state.from_dst_file) {
2925 int rp_error;
2926 trace_migration_return_path_end_before();
2927 rp_error = await_return_path_close_on_source(s);
2928 trace_migration_return_path_end_after(rp_error);
2929 if (rp_error) {
2930 goto fail_invalidate;
2934 if (qemu_file_get_error(s->to_dst_file)) {
2935 trace_migration_completion_file_err();
2936 goto fail_invalidate;
2939 if (!migrate_colo_enabled()) {
2940 migrate_set_state(&s->state, current_active_state,
2941 MIGRATION_STATUS_COMPLETED);
2944 return;
2946 fail_invalidate:
2947 /* If not doing postcopy, vm_start() will be called: let's regain
2948 * control on images.
2950 if (s->state == MIGRATION_STATUS_ACTIVE ||
2951 s->state == MIGRATION_STATUS_DEVICE) {
2952 Error *local_err = NULL;
2954 qemu_mutex_lock_iothread();
2955 bdrv_invalidate_cache_all(&local_err);
2956 if (local_err) {
2957 error_report_err(local_err);
2958 } else {
2959 s->block_inactive = false;
2961 qemu_mutex_unlock_iothread();
2964 fail:
2965 migrate_set_state(&s->state, current_active_state,
2966 MIGRATION_STATUS_FAILED);
2969 bool migrate_colo_enabled(void)
2971 MigrationState *s = migrate_get_current();
2972 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
2975 typedef enum MigThrError {
2976 /* No error detected */
2977 MIG_THR_ERR_NONE = 0,
2978 /* Detected error, but resumed successfully */
2979 MIG_THR_ERR_RECOVERED = 1,
2980 /* Detected fatal error, need to exit */
2981 MIG_THR_ERR_FATAL = 2,
2982 } MigThrError;
2984 static int postcopy_resume_handshake(MigrationState *s)
2986 qemu_savevm_send_postcopy_resume(s->to_dst_file);
2988 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
2989 qemu_sem_wait(&s->rp_state.rp_sem);
2992 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
2993 return 0;
2996 return -1;
2999 /* Return zero if success, or <0 for error */
3000 static int postcopy_do_resume(MigrationState *s)
3002 int ret;
3005 * Call all the resume_prepare() hooks, so that modules can be
3006 * ready for the migration resume.
3008 ret = qemu_savevm_state_resume_prepare(s);
3009 if (ret) {
3010 error_report("%s: resume_prepare() failure detected: %d",
3011 __func__, ret);
3012 return ret;
3016 * Last handshake with destination on the resume (destination will
3017 * switch to postcopy-active afterwards)
3019 ret = postcopy_resume_handshake(s);
3020 if (ret) {
3021 error_report("%s: handshake failed: %d", __func__, ret);
3022 return ret;
3025 return 0;
3029 * We don't return until we are in a safe state to continue current
3030 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
3031 * MIG_THR_ERR_FATAL if unrecovery failure happened.
3033 static MigThrError postcopy_pause(MigrationState *s)
3035 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
3037 while (true) {
3038 QEMUFile *file;
3040 migrate_set_state(&s->state, s->state,
3041 MIGRATION_STATUS_POSTCOPY_PAUSED);
3043 /* Current channel is possibly broken. Release it. */
3044 assert(s->to_dst_file);
3045 qemu_mutex_lock(&s->qemu_file_lock);
3046 file = s->to_dst_file;
3047 s->to_dst_file = NULL;
3048 qemu_mutex_unlock(&s->qemu_file_lock);
3050 qemu_file_shutdown(file);
3051 qemu_fclose(file);
3053 error_report("Detected IO failure for postcopy. "
3054 "Migration paused.");
3057 * We wait until things fixed up. Then someone will setup the
3058 * status back for us.
3060 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
3061 qemu_sem_wait(&s->postcopy_pause_sem);
3064 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3065 /* Woken up by a recover procedure. Give it a shot */
3068 * Firstly, let's wake up the return path now, with a new
3069 * return path channel.
3071 qemu_sem_post(&s->postcopy_pause_rp_sem);
3073 /* Do the resume logic */
3074 if (postcopy_do_resume(s) == 0) {
3075 /* Let's continue! */
3076 trace_postcopy_pause_continued();
3077 return MIG_THR_ERR_RECOVERED;
3078 } else {
3080 * Something wrong happened during the recovery, let's
3081 * pause again. Pause is always better than throwing
3082 * data away.
3084 continue;
3086 } else {
3087 /* This is not right... Time to quit. */
3088 return MIG_THR_ERR_FATAL;
3093 static MigThrError migration_detect_error(MigrationState *s)
3095 int ret;
3096 int state = s->state;
3097 Error *local_error = NULL;
3099 if (state == MIGRATION_STATUS_CANCELLING ||
3100 state == MIGRATION_STATUS_CANCELLED) {
3101 /* End the migration, but don't set the state to failed */
3102 return MIG_THR_ERR_FATAL;
3105 /* Try to detect any file errors */
3106 ret = qemu_file_get_error_obj(s->to_dst_file, &local_error);
3107 if (!ret) {
3108 /* Everything is fine */
3109 assert(!local_error);
3110 return MIG_THR_ERR_NONE;
3113 if (local_error) {
3114 migrate_set_error(s, local_error);
3115 error_free(local_error);
3118 if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
3120 * For postcopy, we allow the network to be down for a
3121 * while. After that, it can be continued by a
3122 * recovery phase.
3124 return postcopy_pause(s);
3125 } else {
3127 * For precopy (or postcopy with error outside IO), we fail
3128 * with no time.
3130 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
3131 trace_migration_thread_file_err();
3133 /* Time to stop the migration, now. */
3134 return MIG_THR_ERR_FATAL;
3138 /* How many bytes have we transferred since the beginning of the migration */
3139 static uint64_t migration_total_bytes(MigrationState *s)
3141 return qemu_ftell(s->to_dst_file) + ram_counters.multifd_bytes;
3144 static void migration_calculate_complete(MigrationState *s)
3146 uint64_t bytes = migration_total_bytes(s);
3147 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3148 int64_t transfer_time;
3150 s->total_time = end_time - s->start_time;
3151 if (!s->downtime) {
3153 * It's still not set, so we are precopy migration. For
3154 * postcopy, downtime is calculated during postcopy_start().
3156 s->downtime = end_time - s->downtime_start;
3159 transfer_time = s->total_time - s->setup_time;
3160 if (transfer_time) {
3161 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
3165 static void update_iteration_initial_status(MigrationState *s)
3168 * Update these three fields at the same time to avoid mismatch info lead
3169 * wrong speed calculation.
3171 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3172 s->iteration_initial_bytes = migration_total_bytes(s);
3173 s->iteration_initial_pages = ram_get_total_transferred_pages();
3176 static void migration_update_counters(MigrationState *s,
3177 int64_t current_time)
3179 uint64_t transferred, transferred_pages, time_spent;
3180 uint64_t current_bytes; /* bytes transferred since the beginning */
3181 double bandwidth;
3183 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
3184 return;
3187 current_bytes = migration_total_bytes(s);
3188 transferred = current_bytes - s->iteration_initial_bytes;
3189 time_spent = current_time - s->iteration_start_time;
3190 bandwidth = (double)transferred / time_spent;
3191 s->threshold_size = bandwidth * s->parameters.downtime_limit;
3193 s->mbps = (((double) transferred * 8.0) /
3194 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
3196 transferred_pages = ram_get_total_transferred_pages() -
3197 s->iteration_initial_pages;
3198 s->pages_per_second = (double) transferred_pages /
3199 (((double) time_spent / 1000.0));
3202 * if we haven't sent anything, we don't want to
3203 * recalculate. 10000 is a small enough number for our purposes
3205 if (ram_counters.dirty_pages_rate && transferred > 10000) {
3206 s->expected_downtime = ram_counters.remaining / bandwidth;
3209 qemu_file_reset_rate_limit(s->to_dst_file);
3211 update_iteration_initial_status(s);
3213 trace_migrate_transferred(transferred, time_spent,
3214 bandwidth, s->threshold_size);
3217 /* Migration thread iteration status */
3218 typedef enum {
3219 MIG_ITERATE_RESUME, /* Resume current iteration */
3220 MIG_ITERATE_SKIP, /* Skip current iteration */
3221 MIG_ITERATE_BREAK, /* Break the loop */
3222 } MigIterateState;
3225 * Return true if continue to the next iteration directly, false
3226 * otherwise.
3228 static MigIterateState migration_iteration_run(MigrationState *s)
3230 uint64_t pending_size, pend_pre, pend_compat, pend_post;
3231 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
3233 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
3234 &pend_compat, &pend_post);
3235 pending_size = pend_pre + pend_compat + pend_post;
3237 trace_migrate_pending(pending_size, s->threshold_size,
3238 pend_pre, pend_compat, pend_post);
3240 if (pending_size && pending_size >= s->threshold_size) {
3241 /* Still a significant amount to transfer */
3242 if (!in_postcopy && pend_pre <= s->threshold_size &&
3243 atomic_read(&s->start_postcopy)) {
3244 if (postcopy_start(s)) {
3245 error_report("%s: postcopy failed to start", __func__);
3247 return MIG_ITERATE_SKIP;
3249 /* Just another iteration step */
3250 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
3251 } else {
3252 trace_migration_thread_low_pending(pending_size);
3253 migration_completion(s);
3254 return MIG_ITERATE_BREAK;
3257 return MIG_ITERATE_RESUME;
3260 static void migration_iteration_finish(MigrationState *s)
3262 /* If we enabled cpu throttling for auto-converge, turn it off. */
3263 cpu_throttle_stop();
3265 qemu_mutex_lock_iothread();
3266 switch (s->state) {
3267 case MIGRATION_STATUS_COMPLETED:
3268 migration_calculate_complete(s);
3269 runstate_set(RUN_STATE_POSTMIGRATE);
3270 break;
3272 case MIGRATION_STATUS_ACTIVE:
3274 * We should really assert here, but since it's during
3275 * migration, let's try to reduce the usage of assertions.
3277 if (!migrate_colo_enabled()) {
3278 error_report("%s: critical error: calling COLO code without "
3279 "COLO enabled", __func__);
3281 migrate_start_colo_process(s);
3283 * Fixme: we will run VM in COLO no matter its old running state.
3284 * After exited COLO, we will keep running.
3286 s->vm_was_running = true;
3287 /* Fallthrough */
3288 case MIGRATION_STATUS_FAILED:
3289 case MIGRATION_STATUS_CANCELLED:
3290 case MIGRATION_STATUS_CANCELLING:
3291 if (s->vm_was_running) {
3292 vm_start();
3293 } else {
3294 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
3295 runstate_set(RUN_STATE_POSTMIGRATE);
3298 break;
3300 default:
3301 /* Should not reach here, but if so, forgive the VM. */
3302 error_report("%s: Unknown ending state %d", __func__, s->state);
3303 break;
3305 migrate_fd_cleanup_schedule(s);
3306 qemu_mutex_unlock_iothread();
3309 void migration_make_urgent_request(void)
3311 qemu_sem_post(&migrate_get_current()->rate_limit_sem);
3314 void migration_consume_urgent_request(void)
3316 qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
3319 /* Returns true if the rate limiting was broken by an urgent request */
3320 bool migration_rate_limit(void)
3322 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3323 MigrationState *s = migrate_get_current();
3325 bool urgent = false;
3326 migration_update_counters(s, now);
3327 if (qemu_file_rate_limit(s->to_dst_file)) {
3329 * Wait for a delay to do rate limiting OR
3330 * something urgent to post the semaphore.
3332 int ms = s->iteration_start_time + BUFFER_DELAY - now;
3333 trace_migration_rate_limit_pre(ms);
3334 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
3336 * We were woken by one or more urgent things but
3337 * the timedwait will have consumed one of them.
3338 * The service routine for the urgent wake will dec
3339 * the semaphore itself for each item it consumes,
3340 * so add this one we just eat back.
3342 qemu_sem_post(&s->rate_limit_sem);
3343 urgent = true;
3345 trace_migration_rate_limit_post(urgent);
3347 return urgent;
3351 * Master migration thread on the source VM.
3352 * It drives the migration and pumps the data down the outgoing channel.
3354 static void *migration_thread(void *opaque)
3356 MigrationState *s = opaque;
3357 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
3358 MigThrError thr_error;
3359 bool urgent = false;
3361 rcu_register_thread();
3363 object_ref(OBJECT(s));
3364 update_iteration_initial_status(s);
3366 qemu_savevm_state_header(s->to_dst_file);
3369 * If we opened the return path, we need to make sure dst has it
3370 * opened as well.
3372 if (s->rp_state.from_dst_file) {
3373 /* Now tell the dest that it should open its end so it can reply */
3374 qemu_savevm_send_open_return_path(s->to_dst_file);
3376 /* And do a ping that will make stuff easier to debug */
3377 qemu_savevm_send_ping(s->to_dst_file, 1);
3380 if (migrate_postcopy()) {
3382 * Tell the destination that we *might* want to do postcopy later;
3383 * if the other end can't do postcopy it should fail now, nice and
3384 * early.
3386 qemu_savevm_send_postcopy_advise(s->to_dst_file);
3389 if (migrate_colo_enabled()) {
3390 /* Notify migration destination that we enable COLO */
3391 qemu_savevm_send_colo_enable(s->to_dst_file);
3394 qemu_savevm_state_setup(s->to_dst_file);
3396 if (qemu_savevm_state_guest_unplug_pending()) {
3397 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3398 MIGRATION_STATUS_WAIT_UNPLUG);
3400 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
3401 qemu_savevm_state_guest_unplug_pending()) {
3402 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3405 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG,
3406 MIGRATION_STATUS_ACTIVE);
3409 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
3410 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3411 MIGRATION_STATUS_ACTIVE);
3413 trace_migration_thread_setup_complete();
3415 while (migration_is_active(s)) {
3416 if (urgent || !qemu_file_rate_limit(s->to_dst_file)) {
3417 MigIterateState iter_state = migration_iteration_run(s);
3418 if (iter_state == MIG_ITERATE_SKIP) {
3419 continue;
3420 } else if (iter_state == MIG_ITERATE_BREAK) {
3421 break;
3426 * Try to detect any kind of failures, and see whether we
3427 * should stop the migration now.
3429 thr_error = migration_detect_error(s);
3430 if (thr_error == MIG_THR_ERR_FATAL) {
3431 /* Stop migration */
3432 break;
3433 } else if (thr_error == MIG_THR_ERR_RECOVERED) {
3435 * Just recovered from a e.g. network failure, reset all
3436 * the local variables. This is important to avoid
3437 * breaking transferred_bytes and bandwidth calculation
3439 update_iteration_initial_status(s);
3442 urgent = migration_rate_limit();
3445 trace_migration_thread_after_loop();
3446 migration_iteration_finish(s);
3447 object_unref(OBJECT(s));
3448 rcu_unregister_thread();
3449 return NULL;
3452 void migrate_fd_connect(MigrationState *s, Error *error_in)
3454 Error *local_err = NULL;
3455 int64_t rate_limit;
3456 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
3458 s->expected_downtime = s->parameters.downtime_limit;
3459 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s);
3460 if (error_in) {
3461 migrate_fd_error(s, error_in);
3462 migrate_fd_cleanup(s);
3463 return;
3466 if (resume) {
3467 /* This is a resumed migration */
3468 rate_limit = s->parameters.max_postcopy_bandwidth /
3469 XFER_LIMIT_RATIO;
3470 } else {
3471 /* This is a fresh new migration */
3472 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
3474 /* Notify before starting migration thread */
3475 notifier_list_notify(&migration_state_notifiers, s);
3478 qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
3479 qemu_file_set_blocking(s->to_dst_file, true);
3482 * Open the return path. For postcopy, it is used exclusively. For
3483 * precopy, only if user specified "return-path" capability would
3484 * QEMU uses the return path.
3486 if (migrate_postcopy_ram() || migrate_use_return_path()) {
3487 if (open_return_path_on_source(s, !resume)) {
3488 error_report("Unable to open return-path for postcopy");
3489 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
3490 migrate_fd_cleanup(s);
3491 return;
3495 if (resume) {
3496 /* Wakeup the main migration thread to do the recovery */
3497 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
3498 MIGRATION_STATUS_POSTCOPY_RECOVER);
3499 qemu_sem_post(&s->postcopy_pause_sem);
3500 return;
3503 if (multifd_save_setup(&local_err) != 0) {
3504 error_report_err(local_err);
3505 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
3506 MIGRATION_STATUS_FAILED);
3507 migrate_fd_cleanup(s);
3508 return;
3510 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
3511 QEMU_THREAD_JOINABLE);
3512 s->migration_thread_running = true;
3515 void migration_global_dump(Monitor *mon)
3517 MigrationState *ms = migrate_get_current();
3519 monitor_printf(mon, "globals:\n");
3520 monitor_printf(mon, "store-global-state: %s\n",
3521 ms->store_global_state ? "on" : "off");
3522 monitor_printf(mon, "only-migratable: %s\n",
3523 only_migratable ? "on" : "off");
3524 monitor_printf(mon, "send-configuration: %s\n",
3525 ms->send_configuration ? "on" : "off");
3526 monitor_printf(mon, "send-section-footer: %s\n",
3527 ms->send_section_footer ? "on" : "off");
3528 monitor_printf(mon, "decompress-error-check: %s\n",
3529 ms->decompress_error_check ? "on" : "off");
3530 monitor_printf(mon, "clear-bitmap-shift: %u\n",
3531 ms->clear_bitmap_shift);
3534 #define DEFINE_PROP_MIG_CAP(name, x) \
3535 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
3537 static Property migration_properties[] = {
3538 DEFINE_PROP_BOOL("store-global-state", MigrationState,
3539 store_global_state, true),
3540 DEFINE_PROP_BOOL("send-configuration", MigrationState,
3541 send_configuration, true),
3542 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
3543 send_section_footer, true),
3544 DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
3545 decompress_error_check, true),
3546 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
3547 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
3549 /* Migration parameters */
3550 DEFINE_PROP_UINT8("x-compress-level", MigrationState,
3551 parameters.compress_level,
3552 DEFAULT_MIGRATE_COMPRESS_LEVEL),
3553 DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
3554 parameters.compress_threads,
3555 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
3556 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
3557 parameters.compress_wait_thread, true),
3558 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
3559 parameters.decompress_threads,
3560 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
3561 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
3562 parameters.cpu_throttle_initial,
3563 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
3564 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
3565 parameters.cpu_throttle_increment,
3566 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
3567 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
3568 parameters.max_bandwidth, MAX_THROTTLE),
3569 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
3570 parameters.downtime_limit,
3571 DEFAULT_MIGRATE_SET_DOWNTIME),
3572 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
3573 parameters.x_checkpoint_delay,
3574 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
3575 DEFINE_PROP_UINT8("multifd-channels", MigrationState,
3576 parameters.multifd_channels,
3577 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
3578 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
3579 parameters.multifd_compression,
3580 DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
3581 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
3582 parameters.multifd_zlib_level,
3583 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
3584 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
3585 parameters.multifd_zstd_level,
3586 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
3587 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
3588 parameters.xbzrle_cache_size,
3589 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
3590 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
3591 parameters.max_postcopy_bandwidth,
3592 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
3593 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
3594 parameters.max_cpu_throttle,
3595 DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
3596 DEFINE_PROP_SIZE("announce-initial", MigrationState,
3597 parameters.announce_initial,
3598 DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
3599 DEFINE_PROP_SIZE("announce-max", MigrationState,
3600 parameters.announce_max,
3601 DEFAULT_MIGRATE_ANNOUNCE_MAX),
3602 DEFINE_PROP_SIZE("announce-rounds", MigrationState,
3603 parameters.announce_rounds,
3604 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
3605 DEFINE_PROP_SIZE("announce-step", MigrationState,
3606 parameters.announce_step,
3607 DEFAULT_MIGRATE_ANNOUNCE_STEP),
3609 /* Migration capabilities */
3610 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
3611 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
3612 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
3613 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
3614 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
3615 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
3616 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
3617 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
3618 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
3619 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
3620 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
3621 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
3623 DEFINE_PROP_END_OF_LIST(),
3626 static void migration_class_init(ObjectClass *klass, void *data)
3628 DeviceClass *dc = DEVICE_CLASS(klass);
3630 dc->user_creatable = false;
3631 device_class_set_props(dc, migration_properties);
3634 static void migration_instance_finalize(Object *obj)
3636 MigrationState *ms = MIGRATION_OBJ(obj);
3637 MigrationParameters *params = &ms->parameters;
3639 qemu_mutex_destroy(&ms->error_mutex);
3640 qemu_mutex_destroy(&ms->qemu_file_lock);
3641 g_free(params->tls_hostname);
3642 g_free(params->tls_creds);
3643 qemu_sem_destroy(&ms->wait_unplug_sem);
3644 qemu_sem_destroy(&ms->rate_limit_sem);
3645 qemu_sem_destroy(&ms->pause_sem);
3646 qemu_sem_destroy(&ms->postcopy_pause_sem);
3647 qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
3648 qemu_sem_destroy(&ms->rp_state.rp_sem);
3649 error_free(ms->error);
3652 static void migration_instance_init(Object *obj)
3654 MigrationState *ms = MIGRATION_OBJ(obj);
3655 MigrationParameters *params = &ms->parameters;
3657 ms->state = MIGRATION_STATUS_NONE;
3658 ms->mbps = -1;
3659 ms->pages_per_second = -1;
3660 qemu_sem_init(&ms->pause_sem, 0);
3661 qemu_mutex_init(&ms->error_mutex);
3663 params->tls_hostname = g_strdup("");
3664 params->tls_creds = g_strdup("");
3666 /* Set has_* up only for parameter checks */
3667 params->has_compress_level = true;
3668 params->has_compress_threads = true;
3669 params->has_decompress_threads = true;
3670 params->has_cpu_throttle_initial = true;
3671 params->has_cpu_throttle_increment = true;
3672 params->has_max_bandwidth = true;
3673 params->has_downtime_limit = true;
3674 params->has_x_checkpoint_delay = true;
3675 params->has_block_incremental = true;
3676 params->has_multifd_channels = true;
3677 params->has_multifd_compression = true;
3678 params->has_multifd_zlib_level = true;
3679 params->has_multifd_zstd_level = true;
3680 params->has_xbzrle_cache_size = true;
3681 params->has_max_postcopy_bandwidth = true;
3682 params->has_max_cpu_throttle = true;
3683 params->has_announce_initial = true;
3684 params->has_announce_max = true;
3685 params->has_announce_rounds = true;
3686 params->has_announce_step = true;
3688 qemu_sem_init(&ms->postcopy_pause_sem, 0);
3689 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
3690 qemu_sem_init(&ms->rp_state.rp_sem, 0);
3691 qemu_sem_init(&ms->rate_limit_sem, 0);
3692 qemu_sem_init(&ms->wait_unplug_sem, 0);
3693 qemu_mutex_init(&ms->qemu_file_lock);
3697 * Return true if check pass, false otherwise. Error will be put
3698 * inside errp if provided.
3700 static bool migration_object_check(MigrationState *ms, Error **errp)
3702 MigrationCapabilityStatusList *head = NULL;
3703 /* Assuming all off */
3704 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
3705 int i;
3707 if (!migrate_params_check(&ms->parameters, errp)) {
3708 return false;
3711 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
3712 if (ms->enabled_capabilities[i]) {
3713 head = migrate_cap_add(head, i, true);
3717 ret = migrate_caps_check(cap_list, head, errp);
3719 /* It works with head == NULL */
3720 qapi_free_MigrationCapabilityStatusList(head);
3722 return ret;
3725 static const TypeInfo migration_type = {
3726 .name = TYPE_MIGRATION,
3728 * NOTE: TYPE_MIGRATION is not really a device, as the object is
3729 * not created using qdev_create(), it is not attached to the qdev
3730 * device tree, and it is never realized.
3732 * TODO: Make this TYPE_OBJECT once QOM provides something like
3733 * TYPE_DEVICE's "-global" properties.
3735 .parent = TYPE_DEVICE,
3736 .class_init = migration_class_init,
3737 .class_size = sizeof(MigrationClass),
3738 .instance_size = sizeof(MigrationState),
3739 .instance_init = migration_instance_init,
3740 .instance_finalize = migration_instance_finalize,
3743 static void register_migration_types(void)
3745 type_register_static(&migration_type);
3748 type_init(register_migration_types);