4 * Copyright IBM, Corp. 2008
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu/osdep.h"
17 #include "qemu/cutils.h"
18 #include "qemu/error-report.h"
19 #include "migration/blocker.h"
25 #include "migration/global_state.h"
26 #include "migration/misc.h"
27 #include "migration.h"
29 #include "qemu-file-channel.h"
30 #include "qemu-file.h"
31 #include "migration/vmstate.h"
32 #include "block/block.h"
33 #include "qapi/error.h"
34 #include "qapi/qmp/qerror.h"
35 #include "qapi/qmp/qnull.h"
38 #include "postcopy-ram.h"
39 #include "qemu/thread.h"
40 #include "qmp-commands.h"
42 #include "qapi-event.h"
43 #include "exec/target_page.h"
44 #include "io/channel-buffer.h"
45 #include "migration/colo.h"
46 #include "hw/boards.h"
47 #include "monitor/monitor.h"
49 #define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
51 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
53 #define BUFFER_DELAY 100
54 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
56 /* Time in milliseconds we are allowed to stop the source,
57 * for sending the last part */
58 #define DEFAULT_MIGRATE_SET_DOWNTIME 300
60 /* Maximum migrate downtime set to 2000 seconds */
61 #define MAX_MIGRATE_DOWNTIME_SECONDS 2000
62 #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
64 /* Default compression thread count */
65 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
66 /* Default decompression thread count, usually decompression is at
67 * least 4 times as fast as compression.*/
68 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
69 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
70 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
71 /* Define default autoconverge cpu throttle migration parameters */
72 #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
73 #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
75 /* Migration XBZRLE default cache size */
76 #define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
78 /* The delay time (in ms) between two COLO checkpoints
79 * Note: Please change this default value to 10000 when we support hybrid mode.
81 #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
82 #define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
83 #define DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT 16
85 static NotifierList migration_state_notifiers
=
86 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers
);
88 static bool deferred_incoming
;
90 /* Messages sent on the return path from destination to source */
91 enum mig_rp_message_type
{
92 MIG_RP_MSG_INVALID
= 0, /* Must be 0 */
93 MIG_RP_MSG_SHUT
, /* sibling will not send any more RP messages */
94 MIG_RP_MSG_PONG
, /* Response to a PING; data (seq: be32 ) */
96 MIG_RP_MSG_REQ_PAGES_ID
, /* data (start: be64, len: be32, id: string) */
97 MIG_RP_MSG_REQ_PAGES
, /* data (start: be64, len: be32) */
102 /* When we add fault tolerance, we could have several
103 migrations at once. For now we don't need to add
104 dynamic creation of migration */
106 static MigrationState
*current_migration
;
108 static bool migration_object_check(MigrationState
*ms
, Error
**errp
);
109 static int migration_maybe_pause(MigrationState
*s
,
110 int *current_active_state
,
113 void migration_object_init(void)
115 MachineState
*ms
= MACHINE(qdev_get_machine());
118 /* This can only be called once. */
119 assert(!current_migration
);
120 current_migration
= MIGRATION_OBJ(object_new(TYPE_MIGRATION
));
122 if (!migration_object_check(current_migration
, &err
)) {
123 error_report_err(err
);
128 * We cannot really do this in migration_instance_init() since at
129 * that time global properties are not yet applied, then this
130 * value will be definitely replaced by something else.
132 if (ms
->enforce_config_section
) {
133 current_migration
->send_configuration
= true;
137 void migration_object_finalize(void)
139 object_unref(OBJECT(current_migration
));
143 MigrationState
*migrate_get_current(void)
145 /* This can only be called after the object created. */
146 assert(current_migration
);
147 return current_migration
;
150 MigrationIncomingState
*migration_incoming_get_current(void)
153 static MigrationIncomingState mis_current
;
156 mis_current
.state
= MIGRATION_STATUS_NONE
;
157 memset(&mis_current
, 0, sizeof(MigrationIncomingState
));
158 qemu_mutex_init(&mis_current
.rp_mutex
);
159 qemu_event_init(&mis_current
.main_thread_load_event
, false);
165 void migration_incoming_state_destroy(void)
167 struct MigrationIncomingState
*mis
= migration_incoming_get_current();
169 if (mis
->to_src_file
) {
170 /* Tell source that we are done */
171 migrate_send_rp_shut(mis
, qemu_file_get_error(mis
->from_src_file
) != 0);
172 qemu_fclose(mis
->to_src_file
);
173 mis
->to_src_file
= NULL
;
176 if (mis
->from_src_file
) {
177 qemu_fclose(mis
->from_src_file
);
178 mis
->from_src_file
= NULL
;
181 qemu_event_reset(&mis
->main_thread_load_event
);
184 static void migrate_generate_event(int new_state
)
186 if (migrate_use_events()) {
187 qapi_event_send_migration(new_state
, &error_abort
);
192 * Called on -incoming with a defer: uri.
193 * The migration can be started later after any parameters have been
196 static void deferred_incoming_migration(Error
**errp
)
198 if (deferred_incoming
) {
199 error_setg(errp
, "Incoming migration already deferred");
201 deferred_incoming
= true;
205 * Send a message on the return channel back to the source
208 static void migrate_send_rp_message(MigrationIncomingState
*mis
,
209 enum mig_rp_message_type message_type
,
210 uint16_t len
, void *data
)
212 trace_migrate_send_rp_message((int)message_type
, len
);
213 qemu_mutex_lock(&mis
->rp_mutex
);
214 qemu_put_be16(mis
->to_src_file
, (unsigned int)message_type
);
215 qemu_put_be16(mis
->to_src_file
, len
);
216 qemu_put_buffer(mis
->to_src_file
, data
, len
);
217 qemu_fflush(mis
->to_src_file
);
218 qemu_mutex_unlock(&mis
->rp_mutex
);
221 /* Request a range of pages from the source VM at the given
223 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
224 * as the last request (a name must have been given previously)
225 * Start: Address offset within the RB
226 * Len: Length in bytes required - must be a multiple of pagesize
228 void migrate_send_rp_req_pages(MigrationIncomingState
*mis
, const char *rbname
,
229 ram_addr_t start
, size_t len
)
231 uint8_t bufc
[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
232 size_t msglen
= 12; /* start + len */
234 *(uint64_t *)bufc
= cpu_to_be64((uint64_t)start
);
235 *(uint32_t *)(bufc
+ 8) = cpu_to_be32((uint32_t)len
);
238 int rbname_len
= strlen(rbname
);
239 assert(rbname_len
< 256);
241 bufc
[msglen
++] = rbname_len
;
242 memcpy(bufc
+ msglen
, rbname
, rbname_len
);
243 msglen
+= rbname_len
;
244 migrate_send_rp_message(mis
, MIG_RP_MSG_REQ_PAGES_ID
, msglen
, bufc
);
246 migrate_send_rp_message(mis
, MIG_RP_MSG_REQ_PAGES
, msglen
, bufc
);
250 void qemu_start_incoming_migration(const char *uri
, Error
**errp
)
254 qapi_event_send_migration(MIGRATION_STATUS_SETUP
, &error_abort
);
255 if (!strcmp(uri
, "defer")) {
256 deferred_incoming_migration(errp
);
257 } else if (strstart(uri
, "tcp:", &p
)) {
258 tcp_start_incoming_migration(p
, errp
);
260 } else if (strstart(uri
, "rdma:", &p
)) {
261 rdma_start_incoming_migration(p
, errp
);
263 } else if (strstart(uri
, "exec:", &p
)) {
264 exec_start_incoming_migration(p
, errp
);
265 } else if (strstart(uri
, "unix:", &p
)) {
266 unix_start_incoming_migration(p
, errp
);
267 } else if (strstart(uri
, "fd:", &p
)) {
268 fd_start_incoming_migration(p
, errp
);
270 error_setg(errp
, "unknown migration protocol: %s", uri
);
274 static void process_incoming_migration_bh(void *opaque
)
276 Error
*local_err
= NULL
;
277 MigrationIncomingState
*mis
= opaque
;
279 /* Make sure all file formats flush their mutable metadata.
280 * If we get an error here, just don't restart the VM yet. */
281 bdrv_invalidate_cache_all(&local_err
);
283 error_report_err(local_err
);
289 * This must happen after all error conditions are dealt with and
290 * we're sure the VM is going to be running on this host.
292 qemu_announce_self();
294 if (multifd_load_cleanup(&local_err
) != 0) {
295 error_report_err(local_err
);
298 /* If global state section was not received or we are in running
299 state, we need to obey autostart. Any other state is set with
302 if (!global_state_received() ||
303 global_state_get_runstate() == RUN_STATE_RUNNING
) {
307 runstate_set(RUN_STATE_PAUSED
);
310 runstate_set(global_state_get_runstate());
313 * This must happen after any state changes since as soon as an external
314 * observer sees this event they might start to prod at the VM assuming
317 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
318 MIGRATION_STATUS_COMPLETED
);
319 qemu_bh_delete(mis
->bh
);
320 migration_incoming_state_destroy();
323 static void process_incoming_migration_co(void *opaque
)
325 MigrationIncomingState
*mis
= migration_incoming_get_current();
329 assert(mis
->from_src_file
);
330 mis
->largest_page_size
= qemu_ram_pagesize_largest();
331 postcopy_state_set(POSTCOPY_INCOMING_NONE
);
332 migrate_set_state(&mis
->state
, MIGRATION_STATUS_NONE
,
333 MIGRATION_STATUS_ACTIVE
);
334 ret
= qemu_loadvm_state(mis
->from_src_file
);
336 ps
= postcopy_state_get();
337 trace_process_incoming_migration_co_end(ret
, ps
);
338 if (ps
!= POSTCOPY_INCOMING_NONE
) {
339 if (ps
== POSTCOPY_INCOMING_ADVISE
) {
341 * Where a migration had postcopy enabled (and thus went to advise)
342 * but managed to complete within the precopy period, we can use
345 postcopy_ram_incoming_cleanup(mis
);
346 } else if (ret
>= 0) {
348 * Postcopy was started, cleanup should happen at the end of the
351 trace_process_incoming_migration_co_postcopy_end_main();
354 /* Else if something went wrong then just fall out of the normal exit */
357 /* we get COLO info, and know if we are in COLO mode */
358 if (!ret
&& migration_incoming_enable_colo()) {
359 mis
->migration_incoming_co
= qemu_coroutine_self();
360 qemu_thread_create(&mis
->colo_incoming_thread
, "COLO incoming",
361 colo_process_incoming_thread
, mis
, QEMU_THREAD_JOINABLE
);
362 mis
->have_colo_incoming_thread
= true;
363 qemu_coroutine_yield();
365 /* Wait checkpoint incoming thread exit before free resource */
366 qemu_thread_join(&mis
->colo_incoming_thread
);
370 Error
*local_err
= NULL
;
372 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
373 MIGRATION_STATUS_FAILED
);
374 error_report("load of migration failed: %s", strerror(-ret
));
375 qemu_fclose(mis
->from_src_file
);
376 if (multifd_load_cleanup(&local_err
) != 0) {
377 error_report_err(local_err
);
381 mis
->bh
= qemu_bh_new(process_incoming_migration_bh
, mis
);
382 qemu_bh_schedule(mis
->bh
);
385 static void migration_incoming_setup(QEMUFile
*f
)
387 MigrationIncomingState
*mis
= migration_incoming_get_current();
389 if (multifd_load_setup() != 0) {
390 /* We haven't been able to create multifd threads
391 nothing better to do */
395 if (!mis
->from_src_file
) {
396 mis
->from_src_file
= f
;
398 qemu_file_set_blocking(f
, false);
401 static void migration_incoming_process(void)
403 Coroutine
*co
= qemu_coroutine_create(process_incoming_migration_co
, NULL
);
404 qemu_coroutine_enter(co
);
407 void migration_fd_process_incoming(QEMUFile
*f
)
409 migration_incoming_setup(f
);
410 migration_incoming_process();
413 void migration_ioc_process_incoming(QIOChannel
*ioc
)
415 MigrationIncomingState
*mis
= migration_incoming_get_current();
417 if (!mis
->from_src_file
) {
418 QEMUFile
*f
= qemu_fopen_channel_input(ioc
);
419 migration_fd_process_incoming(f
);
421 /* We still only have a single channel. Nothing to do here yet */
425 * @migration_has_all_channels: We have received all channels that we need
427 * Returns true when we have got connections to all the channels that
428 * we need for migration.
430 bool migration_has_all_channels(void)
436 * Send a 'SHUT' message on the return channel with the given value
437 * to indicate that we've finished with the RP. Non-0 value indicates
440 void migrate_send_rp_shut(MigrationIncomingState
*mis
,
445 buf
= cpu_to_be32(value
);
446 migrate_send_rp_message(mis
, MIG_RP_MSG_SHUT
, sizeof(buf
), &buf
);
450 * Send a 'PONG' message on the return channel with the given value
451 * (normally in response to a 'PING')
453 void migrate_send_rp_pong(MigrationIncomingState
*mis
,
458 buf
= cpu_to_be32(value
);
459 migrate_send_rp_message(mis
, MIG_RP_MSG_PONG
, sizeof(buf
), &buf
);
462 MigrationCapabilityStatusList
*qmp_query_migrate_capabilities(Error
**errp
)
464 MigrationCapabilityStatusList
*head
= NULL
;
465 MigrationCapabilityStatusList
*caps
;
466 MigrationState
*s
= migrate_get_current();
469 caps
= NULL
; /* silence compiler warning */
470 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
471 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
472 if (i
== MIGRATION_CAPABILITY_BLOCK
) {
477 head
= g_malloc0(sizeof(*caps
));
480 caps
->next
= g_malloc0(sizeof(*caps
));
484 g_malloc(sizeof(*caps
->value
));
485 caps
->value
->capability
= i
;
486 caps
->value
->state
= s
->enabled_capabilities
[i
];
492 MigrationParameters
*qmp_query_migrate_parameters(Error
**errp
)
494 MigrationParameters
*params
;
495 MigrationState
*s
= migrate_get_current();
497 /* TODO use QAPI_CLONE() instead of duplicating it inline */
498 params
= g_malloc0(sizeof(*params
));
499 params
->has_compress_level
= true;
500 params
->compress_level
= s
->parameters
.compress_level
;
501 params
->has_compress_threads
= true;
502 params
->compress_threads
= s
->parameters
.compress_threads
;
503 params
->has_decompress_threads
= true;
504 params
->decompress_threads
= s
->parameters
.decompress_threads
;
505 params
->has_cpu_throttle_initial
= true;
506 params
->cpu_throttle_initial
= s
->parameters
.cpu_throttle_initial
;
507 params
->has_cpu_throttle_increment
= true;
508 params
->cpu_throttle_increment
= s
->parameters
.cpu_throttle_increment
;
509 params
->has_tls_creds
= true;
510 params
->tls_creds
= g_strdup(s
->parameters
.tls_creds
);
511 params
->has_tls_hostname
= true;
512 params
->tls_hostname
= g_strdup(s
->parameters
.tls_hostname
);
513 params
->has_max_bandwidth
= true;
514 params
->max_bandwidth
= s
->parameters
.max_bandwidth
;
515 params
->has_downtime_limit
= true;
516 params
->downtime_limit
= s
->parameters
.downtime_limit
;
517 params
->has_x_checkpoint_delay
= true;
518 params
->x_checkpoint_delay
= s
->parameters
.x_checkpoint_delay
;
519 params
->has_block_incremental
= true;
520 params
->block_incremental
= s
->parameters
.block_incremental
;
521 params
->has_x_multifd_channels
= true;
522 params
->x_multifd_channels
= s
->parameters
.x_multifd_channels
;
523 params
->has_x_multifd_page_count
= true;
524 params
->x_multifd_page_count
= s
->parameters
.x_multifd_page_count
;
525 params
->has_xbzrle_cache_size
= true;
526 params
->xbzrle_cache_size
= s
->parameters
.xbzrle_cache_size
;
532 * Return true if we're already in the middle of a migration
533 * (i.e. any of the active or setup states)
535 static bool migration_is_setup_or_active(int state
)
538 case MIGRATION_STATUS_ACTIVE
:
539 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
540 case MIGRATION_STATUS_SETUP
:
541 case MIGRATION_STATUS_PRE_SWITCHOVER
:
542 case MIGRATION_STATUS_DEVICE
:
551 static void populate_ram_info(MigrationInfo
*info
, MigrationState
*s
)
553 info
->has_ram
= true;
554 info
->ram
= g_malloc0(sizeof(*info
->ram
));
555 info
->ram
->transferred
= ram_counters
.transferred
;
556 info
->ram
->total
= ram_bytes_total();
557 info
->ram
->duplicate
= ram_counters
.duplicate
;
558 /* legacy value. It is not used anymore */
559 info
->ram
->skipped
= 0;
560 info
->ram
->normal
= ram_counters
.normal
;
561 info
->ram
->normal_bytes
= ram_counters
.normal
*
562 qemu_target_page_size();
563 info
->ram
->mbps
= s
->mbps
;
564 info
->ram
->dirty_sync_count
= ram_counters
.dirty_sync_count
;
565 info
->ram
->postcopy_requests
= ram_counters
.postcopy_requests
;
566 info
->ram
->page_size
= qemu_target_page_size();
568 if (migrate_use_xbzrle()) {
569 info
->has_xbzrle_cache
= true;
570 info
->xbzrle_cache
= g_malloc0(sizeof(*info
->xbzrle_cache
));
571 info
->xbzrle_cache
->cache_size
= migrate_xbzrle_cache_size();
572 info
->xbzrle_cache
->bytes
= xbzrle_counters
.bytes
;
573 info
->xbzrle_cache
->pages
= xbzrle_counters
.pages
;
574 info
->xbzrle_cache
->cache_miss
= xbzrle_counters
.cache_miss
;
575 info
->xbzrle_cache
->cache_miss_rate
= xbzrle_counters
.cache_miss_rate
;
576 info
->xbzrle_cache
->overflow
= xbzrle_counters
.overflow
;
579 if (cpu_throttle_active()) {
580 info
->has_cpu_throttle_percentage
= true;
581 info
->cpu_throttle_percentage
= cpu_throttle_get_percentage();
584 if (s
->state
!= MIGRATION_STATUS_COMPLETED
) {
585 info
->ram
->remaining
= ram_bytes_remaining();
586 info
->ram
->dirty_pages_rate
= ram_counters
.dirty_pages_rate
;
590 static void populate_disk_info(MigrationInfo
*info
)
592 if (blk_mig_active()) {
593 info
->has_disk
= true;
594 info
->disk
= g_malloc0(sizeof(*info
->disk
));
595 info
->disk
->transferred
= blk_mig_bytes_transferred();
596 info
->disk
->remaining
= blk_mig_bytes_remaining();
597 info
->disk
->total
= blk_mig_bytes_total();
601 MigrationInfo
*qmp_query_migrate(Error
**errp
)
603 MigrationInfo
*info
= g_malloc0(sizeof(*info
));
604 MigrationState
*s
= migrate_get_current();
607 case MIGRATION_STATUS_NONE
:
608 /* no migration has happened ever */
610 case MIGRATION_STATUS_SETUP
:
611 info
->has_status
= true;
612 info
->has_total_time
= false;
614 case MIGRATION_STATUS_ACTIVE
:
615 case MIGRATION_STATUS_CANCELLING
:
616 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
617 case MIGRATION_STATUS_PRE_SWITCHOVER
:
618 case MIGRATION_STATUS_DEVICE
:
619 /* TODO add some postcopy stats */
620 info
->has_status
= true;
621 info
->has_total_time
= true;
622 info
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
624 info
->has_expected_downtime
= true;
625 info
->expected_downtime
= s
->expected_downtime
;
626 info
->has_setup_time
= true;
627 info
->setup_time
= s
->setup_time
;
629 populate_ram_info(info
, s
);
630 populate_disk_info(info
);
632 case MIGRATION_STATUS_COLO
:
633 info
->has_status
= true;
634 /* TODO: display COLO specific information (checkpoint info etc.) */
636 case MIGRATION_STATUS_COMPLETED
:
637 info
->has_status
= true;
638 info
->has_total_time
= true;
639 info
->total_time
= s
->total_time
;
640 info
->has_downtime
= true;
641 info
->downtime
= s
->downtime
;
642 info
->has_setup_time
= true;
643 info
->setup_time
= s
->setup_time
;
645 populate_ram_info(info
, s
);
647 case MIGRATION_STATUS_FAILED
:
648 info
->has_status
= true;
650 info
->has_error_desc
= true;
651 info
->error_desc
= g_strdup(error_get_pretty(s
->error
));
654 case MIGRATION_STATUS_CANCELLED
:
655 info
->has_status
= true;
658 info
->status
= s
->state
;
664 * @migration_caps_check - check capability validity
666 * @cap_list: old capability list, array of bool
667 * @params: new capabilities to be applied soon
668 * @errp: set *errp if the check failed, with reason
670 * Returns true if check passed, otherwise false.
672 static bool migrate_caps_check(bool *cap_list
,
673 MigrationCapabilityStatusList
*params
,
676 MigrationCapabilityStatusList
*cap
;
677 bool old_postcopy_cap
;
678 MigrationIncomingState
*mis
= migration_incoming_get_current();
680 old_postcopy_cap
= cap_list
[MIGRATION_CAPABILITY_POSTCOPY_RAM
];
682 for (cap
= params
; cap
; cap
= cap
->next
) {
683 cap_list
[cap
->value
->capability
] = cap
->value
->state
;
686 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
687 if (cap_list
[MIGRATION_CAPABILITY_BLOCK
]) {
688 error_setg(errp
, "QEMU compiled without old-style (blk/-b, inc/-i) "
690 error_append_hint(errp
, "Use drive_mirror+NBD instead.\n");
695 if (cap_list
[MIGRATION_CAPABILITY_POSTCOPY_RAM
]) {
696 if (cap_list
[MIGRATION_CAPABILITY_COMPRESS
]) {
697 /* The decompression threads asynchronously write into RAM
698 * rather than use the atomic copies needed to avoid
699 * userfaulting. It should be possible to fix the decompression
700 * threads for compatibility in future.
702 error_setg(errp
, "Postcopy is not currently compatible "
707 /* This check is reasonably expensive, so only when it's being
708 * set the first time, also it's only the destination that needs
711 if (!old_postcopy_cap
&& runstate_check(RUN_STATE_INMIGRATE
) &&
712 !postcopy_ram_supported_by_host(mis
)) {
713 /* postcopy_ram_supported_by_host will have emitted a more
716 error_setg(errp
, "Postcopy is not supported");
724 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList
*params
,
727 MigrationState
*s
= migrate_get_current();
728 MigrationCapabilityStatusList
*cap
;
730 if (migration_is_setup_or_active(s
->state
)) {
731 error_setg(errp
, QERR_MIGRATION_ACTIVE
);
735 if (!migrate_caps_check(s
->enabled_capabilities
, params
, errp
)) {
739 for (cap
= params
; cap
; cap
= cap
->next
) {
740 s
->enabled_capabilities
[cap
->value
->capability
] = cap
->value
->state
;
745 * Check whether the parameters are valid. Error will be put into errp
746 * (if provided). Return true if valid, otherwise false.
748 static bool migrate_params_check(MigrationParameters
*params
, Error
**errp
)
750 if (params
->has_compress_level
&&
751 (params
->compress_level
> 9)) {
752 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "compress_level",
753 "is invalid, it should be in the range of 0 to 9");
757 if (params
->has_compress_threads
&& (params
->compress_threads
< 1)) {
758 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
760 "is invalid, it should be in the range of 1 to 255");
764 if (params
->has_decompress_threads
&& (params
->decompress_threads
< 1)) {
765 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
766 "decompress_threads",
767 "is invalid, it should be in the range of 1 to 255");
771 if (params
->has_cpu_throttle_initial
&&
772 (params
->cpu_throttle_initial
< 1 ||
773 params
->cpu_throttle_initial
> 99)) {
774 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
775 "cpu_throttle_initial",
776 "an integer in the range of 1 to 99");
780 if (params
->has_cpu_throttle_increment
&&
781 (params
->cpu_throttle_increment
< 1 ||
782 params
->cpu_throttle_increment
> 99)) {
783 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
784 "cpu_throttle_increment",
785 "an integer in the range of 1 to 99");
789 if (params
->has_max_bandwidth
&& (params
->max_bandwidth
> SIZE_MAX
)) {
790 error_setg(errp
, "Parameter 'max_bandwidth' expects an integer in the"
791 " range of 0 to %zu bytes/second", SIZE_MAX
);
795 if (params
->has_downtime_limit
&&
796 (params
->downtime_limit
> MAX_MIGRATE_DOWNTIME
)) {
797 error_setg(errp
, "Parameter 'downtime_limit' expects an integer in "
798 "the range of 0 to %d milliseconds",
799 MAX_MIGRATE_DOWNTIME
);
803 /* x_checkpoint_delay is now always positive */
805 if (params
->has_x_multifd_channels
&& (params
->x_multifd_channels
< 1)) {
806 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
808 "is invalid, it should be in the range of 1 to 255");
811 if (params
->has_x_multifd_page_count
&&
812 (params
->x_multifd_page_count
< 1 ||
813 params
->x_multifd_page_count
> 10000)) {
814 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
815 "multifd_page_count",
816 "is invalid, it should be in the range of 1 to 10000");
820 if (params
->has_xbzrle_cache_size
&&
821 (params
->xbzrle_cache_size
< qemu_target_page_size() ||
822 !is_power_of_2(params
->xbzrle_cache_size
))) {
823 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
825 "is invalid, it should be bigger than target page size"
826 " and a power of two");
833 static void migrate_params_test_apply(MigrateSetParameters
*params
,
834 MigrationParameters
*dest
)
836 *dest
= migrate_get_current()->parameters
;
838 /* TODO use QAPI_CLONE() instead of duplicating it inline */
840 if (params
->has_compress_level
) {
841 dest
->compress_level
= params
->compress_level
;
844 if (params
->has_compress_threads
) {
845 dest
->compress_threads
= params
->compress_threads
;
848 if (params
->has_decompress_threads
) {
849 dest
->decompress_threads
= params
->decompress_threads
;
852 if (params
->has_cpu_throttle_initial
) {
853 dest
->cpu_throttle_initial
= params
->cpu_throttle_initial
;
856 if (params
->has_cpu_throttle_increment
) {
857 dest
->cpu_throttle_increment
= params
->cpu_throttle_increment
;
860 if (params
->has_tls_creds
) {
861 assert(params
->tls_creds
->type
== QTYPE_QSTRING
);
862 dest
->tls_creds
= g_strdup(params
->tls_creds
->u
.s
);
865 if (params
->has_tls_hostname
) {
866 assert(params
->tls_hostname
->type
== QTYPE_QSTRING
);
867 dest
->tls_hostname
= g_strdup(params
->tls_hostname
->u
.s
);
870 if (params
->has_max_bandwidth
) {
871 dest
->max_bandwidth
= params
->max_bandwidth
;
874 if (params
->has_downtime_limit
) {
875 dest
->downtime_limit
= params
->downtime_limit
;
878 if (params
->has_x_checkpoint_delay
) {
879 dest
->x_checkpoint_delay
= params
->x_checkpoint_delay
;
882 if (params
->has_block_incremental
) {
883 dest
->block_incremental
= params
->block_incremental
;
885 if (params
->has_x_multifd_channels
) {
886 dest
->x_multifd_channels
= params
->x_multifd_channels
;
888 if (params
->has_x_multifd_page_count
) {
889 dest
->x_multifd_page_count
= params
->x_multifd_page_count
;
891 if (params
->has_xbzrle_cache_size
) {
892 dest
->xbzrle_cache_size
= params
->xbzrle_cache_size
;
896 static void migrate_params_apply(MigrateSetParameters
*params
, Error
**errp
)
898 MigrationState
*s
= migrate_get_current();
900 /* TODO use QAPI_CLONE() instead of duplicating it inline */
902 if (params
->has_compress_level
) {
903 s
->parameters
.compress_level
= params
->compress_level
;
906 if (params
->has_compress_threads
) {
907 s
->parameters
.compress_threads
= params
->compress_threads
;
910 if (params
->has_decompress_threads
) {
911 s
->parameters
.decompress_threads
= params
->decompress_threads
;
914 if (params
->has_cpu_throttle_initial
) {
915 s
->parameters
.cpu_throttle_initial
= params
->cpu_throttle_initial
;
918 if (params
->has_cpu_throttle_increment
) {
919 s
->parameters
.cpu_throttle_increment
= params
->cpu_throttle_increment
;
922 if (params
->has_tls_creds
) {
923 g_free(s
->parameters
.tls_creds
);
924 assert(params
->tls_creds
->type
== QTYPE_QSTRING
);
925 s
->parameters
.tls_creds
= g_strdup(params
->tls_creds
->u
.s
);
928 if (params
->has_tls_hostname
) {
929 g_free(s
->parameters
.tls_hostname
);
930 assert(params
->tls_hostname
->type
== QTYPE_QSTRING
);
931 s
->parameters
.tls_hostname
= g_strdup(params
->tls_hostname
->u
.s
);
934 if (params
->has_max_bandwidth
) {
935 s
->parameters
.max_bandwidth
= params
->max_bandwidth
;
936 if (s
->to_dst_file
) {
937 qemu_file_set_rate_limit(s
->to_dst_file
,
938 s
->parameters
.max_bandwidth
/ XFER_LIMIT_RATIO
);
942 if (params
->has_downtime_limit
) {
943 s
->parameters
.downtime_limit
= params
->downtime_limit
;
946 if (params
->has_x_checkpoint_delay
) {
947 s
->parameters
.x_checkpoint_delay
= params
->x_checkpoint_delay
;
948 if (migration_in_colo_state()) {
949 colo_checkpoint_notify(s
);
953 if (params
->has_block_incremental
) {
954 s
->parameters
.block_incremental
= params
->block_incremental
;
956 if (params
->has_x_multifd_channels
) {
957 s
->parameters
.x_multifd_channels
= params
->x_multifd_channels
;
959 if (params
->has_x_multifd_page_count
) {
960 s
->parameters
.x_multifd_page_count
= params
->x_multifd_page_count
;
962 if (params
->has_xbzrle_cache_size
) {
963 s
->parameters
.xbzrle_cache_size
= params
->xbzrle_cache_size
;
964 xbzrle_cache_resize(params
->xbzrle_cache_size
, errp
);
968 void qmp_migrate_set_parameters(MigrateSetParameters
*params
, Error
**errp
)
970 MigrationParameters tmp
;
972 /* TODO Rewrite "" to null instead */
973 if (params
->has_tls_creds
974 && params
->tls_creds
->type
== QTYPE_QNULL
) {
975 QDECREF(params
->tls_creds
->u
.n
);
976 params
->tls_creds
->type
= QTYPE_QSTRING
;
977 params
->tls_creds
->u
.s
= strdup("");
979 /* TODO Rewrite "" to null instead */
980 if (params
->has_tls_hostname
981 && params
->tls_hostname
->type
== QTYPE_QNULL
) {
982 QDECREF(params
->tls_hostname
->u
.n
);
983 params
->tls_hostname
->type
= QTYPE_QSTRING
;
984 params
->tls_hostname
->u
.s
= strdup("");
987 migrate_params_test_apply(params
, &tmp
);
989 if (!migrate_params_check(&tmp
, errp
)) {
990 /* Invalid parameter */
994 migrate_params_apply(params
, errp
);
998 void qmp_migrate_start_postcopy(Error
**errp
)
1000 MigrationState
*s
= migrate_get_current();
1002 if (!migrate_postcopy_ram()) {
1003 error_setg(errp
, "Enable postcopy with migrate_set_capability before"
1004 " the start of migration");
1008 if (s
->state
== MIGRATION_STATUS_NONE
) {
1009 error_setg(errp
, "Postcopy must be started after migration has been"
1014 * we don't error if migration has finished since that would be racy
1015 * with issuing this command.
1017 atomic_set(&s
->start_postcopy
, true);
1020 /* shared migration helpers */
1022 void migrate_set_state(int *state
, int old_state
, int new_state
)
1024 assert(new_state
< MIGRATION_STATUS__MAX
);
1025 if (atomic_cmpxchg(state
, old_state
, new_state
) == old_state
) {
1026 trace_migrate_set_state(MigrationStatus_str(new_state
));
1027 migrate_generate_event(new_state
);
1031 static MigrationCapabilityStatusList
*migrate_cap_add(
1032 MigrationCapabilityStatusList
*list
,
1033 MigrationCapability index
,
1036 MigrationCapabilityStatusList
*cap
;
1038 cap
= g_new0(MigrationCapabilityStatusList
, 1);
1039 cap
->value
= g_new0(MigrationCapabilityStatus
, 1);
1040 cap
->value
->capability
= index
;
1041 cap
->value
->state
= state
;
1047 void migrate_set_block_enabled(bool value
, Error
**errp
)
1049 MigrationCapabilityStatusList
*cap
;
1051 cap
= migrate_cap_add(NULL
, MIGRATION_CAPABILITY_BLOCK
, value
);
1052 qmp_migrate_set_capabilities(cap
, errp
);
1053 qapi_free_MigrationCapabilityStatusList(cap
);
1056 static void migrate_set_block_incremental(MigrationState
*s
, bool value
)
1058 s
->parameters
.block_incremental
= value
;
1061 static void block_cleanup_parameters(MigrationState
*s
)
1063 if (s
->must_remove_block_options
) {
1064 /* setting to false can never fail */
1065 migrate_set_block_enabled(false, &error_abort
);
1066 migrate_set_block_incremental(s
, false);
1067 s
->must_remove_block_options
= false;
1071 static void migrate_fd_cleanup(void *opaque
)
1073 MigrationState
*s
= opaque
;
1075 qemu_bh_delete(s
->cleanup_bh
);
1076 s
->cleanup_bh
= NULL
;
1078 qemu_savevm_state_cleanup();
1080 if (s
->to_dst_file
) {
1081 Error
*local_err
= NULL
;
1083 trace_migrate_fd_cleanup();
1084 qemu_mutex_unlock_iothread();
1085 if (s
->migration_thread_running
) {
1086 qemu_thread_join(&s
->thread
);
1087 s
->migration_thread_running
= false;
1089 qemu_mutex_lock_iothread();
1091 if (multifd_save_cleanup(&local_err
) != 0) {
1092 error_report_err(local_err
);
1094 qemu_fclose(s
->to_dst_file
);
1095 s
->to_dst_file
= NULL
;
1098 assert((s
->state
!= MIGRATION_STATUS_ACTIVE
) &&
1099 (s
->state
!= MIGRATION_STATUS_POSTCOPY_ACTIVE
));
1101 if (s
->state
== MIGRATION_STATUS_CANCELLING
) {
1102 migrate_set_state(&s
->state
, MIGRATION_STATUS_CANCELLING
,
1103 MIGRATION_STATUS_CANCELLED
);
1107 /* It is used on info migrate. We can't free it */
1108 error_report_err(error_copy(s
->error
));
1110 notifier_list_notify(&migration_state_notifiers
, s
);
1111 block_cleanup_parameters(s
);
1114 void migrate_set_error(MigrationState
*s
, const Error
*error
)
1116 qemu_mutex_lock(&s
->error_mutex
);
1118 s
->error
= error_copy(error
);
1120 qemu_mutex_unlock(&s
->error_mutex
);
1123 void migrate_fd_error(MigrationState
*s
, const Error
*error
)
1125 trace_migrate_fd_error(error_get_pretty(error
));
1126 assert(s
->to_dst_file
== NULL
);
1127 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
1128 MIGRATION_STATUS_FAILED
);
1129 migrate_set_error(s
, error
);
1132 static void migrate_fd_cancel(MigrationState
*s
)
1135 QEMUFile
*f
= migrate_get_current()->to_dst_file
;
1136 trace_migrate_fd_cancel();
1138 if (s
->rp_state
.from_dst_file
) {
1139 /* shutdown the rp socket, so causing the rp thread to shutdown */
1140 qemu_file_shutdown(s
->rp_state
.from_dst_file
);
1144 old_state
= s
->state
;
1145 if (!migration_is_setup_or_active(old_state
)) {
1148 /* If the migration is paused, kick it out of the pause */
1149 if (old_state
== MIGRATION_STATUS_PRE_SWITCHOVER
) {
1150 qemu_sem_post(&s
->pause_sem
);
1152 migrate_set_state(&s
->state
, old_state
, MIGRATION_STATUS_CANCELLING
);
1153 } while (s
->state
!= MIGRATION_STATUS_CANCELLING
);
1156 * If we're unlucky the migration code might be stuck somewhere in a
1157 * send/write while the network has failed and is waiting to timeout;
1158 * if we've got shutdown(2) available then we can force it to quit.
1159 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1160 * called in a bh, so there is no race against this cancel.
1162 if (s
->state
== MIGRATION_STATUS_CANCELLING
&& f
) {
1163 qemu_file_shutdown(f
);
1165 if (s
->state
== MIGRATION_STATUS_CANCELLING
&& s
->block_inactive
) {
1166 Error
*local_err
= NULL
;
1168 bdrv_invalidate_cache_all(&local_err
);
1170 error_report_err(local_err
);
1172 s
->block_inactive
= false;
1177 void add_migration_state_change_notifier(Notifier
*notify
)
1179 notifier_list_add(&migration_state_notifiers
, notify
);
1182 void remove_migration_state_change_notifier(Notifier
*notify
)
1184 notifier_remove(notify
);
1187 bool migration_in_setup(MigrationState
*s
)
1189 return s
->state
== MIGRATION_STATUS_SETUP
;
1192 bool migration_has_finished(MigrationState
*s
)
1194 return s
->state
== MIGRATION_STATUS_COMPLETED
;
1197 bool migration_has_failed(MigrationState
*s
)
1199 return (s
->state
== MIGRATION_STATUS_CANCELLED
||
1200 s
->state
== MIGRATION_STATUS_FAILED
);
1203 bool migration_in_postcopy(void)
1205 MigrationState
*s
= migrate_get_current();
1207 return (s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
);
1210 bool migration_in_postcopy_after_devices(MigrationState
*s
)
1212 return migration_in_postcopy() && s
->postcopy_after_devices
;
1215 bool migration_is_idle(void)
1217 MigrationState
*s
= migrate_get_current();
1220 case MIGRATION_STATUS_NONE
:
1221 case MIGRATION_STATUS_CANCELLED
:
1222 case MIGRATION_STATUS_COMPLETED
:
1223 case MIGRATION_STATUS_FAILED
:
1225 case MIGRATION_STATUS_SETUP
:
1226 case MIGRATION_STATUS_CANCELLING
:
1227 case MIGRATION_STATUS_ACTIVE
:
1228 case MIGRATION_STATUS_POSTCOPY_ACTIVE
:
1229 case MIGRATION_STATUS_COLO
:
1230 case MIGRATION_STATUS_PRE_SWITCHOVER
:
1231 case MIGRATION_STATUS_DEVICE
:
1233 case MIGRATION_STATUS__MAX
:
1234 g_assert_not_reached();
1240 MigrationState
*migrate_init(void)
1242 MigrationState
*s
= migrate_get_current();
1245 * Reinitialise all migration state, except
1246 * parameters/capabilities that the user set, and
1252 s
->to_dst_file
= NULL
;
1253 s
->state
= MIGRATION_STATUS_NONE
;
1254 s
->rp_state
.from_dst_file
= NULL
;
1255 s
->rp_state
.error
= false;
1258 s
->expected_downtime
= 0;
1260 s
->start_postcopy
= false;
1261 s
->postcopy_after_devices
= false;
1262 s
->migration_thread_running
= false;
1263 error_free(s
->error
);
1266 migrate_set_state(&s
->state
, MIGRATION_STATUS_NONE
, MIGRATION_STATUS_SETUP
);
1268 s
->start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1270 s
->vm_was_running
= false;
1271 s
->iteration_initial_bytes
= 0;
1272 s
->threshold_size
= 0;
1276 static GSList
*migration_blockers
;
1278 int migrate_add_blocker(Error
*reason
, Error
**errp
)
1280 if (migrate_get_current()->only_migratable
) {
1281 error_propagate(errp
, error_copy(reason
));
1282 error_prepend(errp
, "disallowing migration blocker "
1283 "(--only_migratable) for: ");
1287 if (migration_is_idle()) {
1288 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
1292 error_propagate(errp
, error_copy(reason
));
1293 error_prepend(errp
, "disallowing migration blocker (migration in "
1298 void migrate_del_blocker(Error
*reason
)
1300 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
1303 void qmp_migrate_incoming(const char *uri
, Error
**errp
)
1305 Error
*local_err
= NULL
;
1306 static bool once
= true;
1308 if (!deferred_incoming
) {
1309 error_setg(errp
, "For use with '-incoming defer'");
1313 error_setg(errp
, "The incoming migration has already been started");
1316 qemu_start_incoming_migration(uri
, &local_err
);
1319 error_propagate(errp
, local_err
);
1326 bool migration_is_blocked(Error
**errp
)
1328 if (qemu_savevm_state_blocked(errp
)) {
1332 if (migration_blockers
) {
1333 error_propagate(errp
, error_copy(migration_blockers
->data
));
1340 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
1341 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
1344 Error
*local_err
= NULL
;
1345 MigrationState
*s
= migrate_get_current();
1348 if (migration_is_setup_or_active(s
->state
) ||
1349 s
->state
== MIGRATION_STATUS_CANCELLING
||
1350 s
->state
== MIGRATION_STATUS_COLO
) {
1351 error_setg(errp
, QERR_MIGRATION_ACTIVE
);
1354 if (runstate_check(RUN_STATE_INMIGRATE
)) {
1355 error_setg(errp
, "Guest is waiting for an incoming migration");
1359 if (migration_is_blocked(errp
)) {
1363 if ((has_blk
&& blk
) || (has_inc
&& inc
)) {
1364 if (migrate_use_block() || migrate_use_block_incremental()) {
1365 error_setg(errp
, "Command options are incompatible with "
1366 "current migration capabilities");
1369 migrate_set_block_enabled(true, &local_err
);
1371 error_propagate(errp
, local_err
);
1374 s
->must_remove_block_options
= true;
1377 if (has_inc
&& inc
) {
1378 migrate_set_block_incremental(s
, true);
1383 if (strstart(uri
, "tcp:", &p
)) {
1384 tcp_start_outgoing_migration(s
, p
, &local_err
);
1386 } else if (strstart(uri
, "rdma:", &p
)) {
1387 rdma_start_outgoing_migration(s
, p
, &local_err
);
1389 } else if (strstart(uri
, "exec:", &p
)) {
1390 exec_start_outgoing_migration(s
, p
, &local_err
);
1391 } else if (strstart(uri
, "unix:", &p
)) {
1392 unix_start_outgoing_migration(s
, p
, &local_err
);
1393 } else if (strstart(uri
, "fd:", &p
)) {
1394 fd_start_outgoing_migration(s
, p
, &local_err
);
1396 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri",
1397 "a valid migration protocol");
1398 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
1399 MIGRATION_STATUS_FAILED
);
1404 migrate_fd_error(s
, local_err
);
1405 error_propagate(errp
, local_err
);
1410 void qmp_migrate_cancel(Error
**errp
)
1412 migrate_fd_cancel(migrate_get_current());
1415 void qmp_migrate_continue(MigrationStatus state
, Error
**errp
)
1417 MigrationState
*s
= migrate_get_current();
1418 if (s
->state
!= state
) {
1419 error_setg(errp
, "Migration not in expected state: %s",
1420 MigrationStatus_str(s
->state
));
1423 qemu_sem_post(&s
->pause_sem
);
1426 void qmp_migrate_set_cache_size(int64_t value
, Error
**errp
)
1428 MigrateSetParameters p
= {
1429 .has_xbzrle_cache_size
= true,
1430 .xbzrle_cache_size
= value
,
1433 qmp_migrate_set_parameters(&p
, errp
);
1436 int64_t qmp_query_migrate_cache_size(Error
**errp
)
1438 return migrate_xbzrle_cache_size();
1441 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
1443 MigrateSetParameters p
= {
1444 .has_max_bandwidth
= true,
1445 .max_bandwidth
= value
,
1448 qmp_migrate_set_parameters(&p
, errp
);
1451 void qmp_migrate_set_downtime(double value
, Error
**errp
)
1453 if (value
< 0 || value
> MAX_MIGRATE_DOWNTIME_SECONDS
) {
1454 error_setg(errp
, "Parameter 'downtime_limit' expects an integer in "
1455 "the range of 0 to %d seconds",
1456 MAX_MIGRATE_DOWNTIME_SECONDS
);
1460 value
*= 1000; /* Convert to milliseconds */
1461 value
= MAX(0, MIN(INT64_MAX
, value
));
1463 MigrateSetParameters p
= {
1464 .has_downtime_limit
= true,
1465 .downtime_limit
= value
,
1468 qmp_migrate_set_parameters(&p
, errp
);
1471 bool migrate_release_ram(void)
1475 s
= migrate_get_current();
1477 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_RELEASE_RAM
];
1480 bool migrate_postcopy_ram(void)
1484 s
= migrate_get_current();
1486 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_POSTCOPY_RAM
];
1489 bool migrate_postcopy(void)
1491 return migrate_postcopy_ram();
1494 bool migrate_auto_converge(void)
1498 s
= migrate_get_current();
1500 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_AUTO_CONVERGE
];
1503 bool migrate_zero_blocks(void)
1507 s
= migrate_get_current();
1509 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_ZERO_BLOCKS
];
1512 bool migrate_use_compression(void)
1516 s
= migrate_get_current();
1518 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_COMPRESS
];
1521 int migrate_compress_level(void)
1525 s
= migrate_get_current();
1527 return s
->parameters
.compress_level
;
1530 int migrate_compress_threads(void)
1534 s
= migrate_get_current();
1536 return s
->parameters
.compress_threads
;
1539 int migrate_decompress_threads(void)
1543 s
= migrate_get_current();
1545 return s
->parameters
.decompress_threads
;
1548 bool migrate_use_events(void)
1552 s
= migrate_get_current();
1554 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_EVENTS
];
1557 bool migrate_use_multifd(void)
1561 s
= migrate_get_current();
1563 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_X_MULTIFD
];
1566 bool migrate_pause_before_switchover(void)
1570 s
= migrate_get_current();
1572 return s
->enabled_capabilities
[
1573 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER
];
1576 int migrate_multifd_channels(void)
1580 s
= migrate_get_current();
1582 return s
->parameters
.x_multifd_channels
;
1585 int migrate_multifd_page_count(void)
1589 s
= migrate_get_current();
1591 return s
->parameters
.x_multifd_page_count
;
1594 int migrate_use_xbzrle(void)
1598 s
= migrate_get_current();
1600 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_XBZRLE
];
1603 int64_t migrate_xbzrle_cache_size(void)
1607 s
= migrate_get_current();
1609 return s
->parameters
.xbzrle_cache_size
;
1612 bool migrate_use_block(void)
1616 s
= migrate_get_current();
1618 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_BLOCK
];
1621 bool migrate_use_return_path(void)
1625 s
= migrate_get_current();
1627 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_RETURN_PATH
];
1630 bool migrate_use_block_incremental(void)
1634 s
= migrate_get_current();
1636 return s
->parameters
.block_incremental
;
1639 /* migration thread support */
1641 * Something bad happened to the RP stream, mark an error
1642 * The caller shall print or trace something to indicate why
1644 static void mark_source_rp_bad(MigrationState
*s
)
1646 s
->rp_state
.error
= true;
1649 static struct rp_cmd_args
{
1650 ssize_t len
; /* -1 = variable */
1653 [MIG_RP_MSG_INVALID
] = { .len
= -1, .name
= "INVALID" },
1654 [MIG_RP_MSG_SHUT
] = { .len
= 4, .name
= "SHUT" },
1655 [MIG_RP_MSG_PONG
] = { .len
= 4, .name
= "PONG" },
1656 [MIG_RP_MSG_REQ_PAGES
] = { .len
= 12, .name
= "REQ_PAGES" },
1657 [MIG_RP_MSG_REQ_PAGES_ID
] = { .len
= -1, .name
= "REQ_PAGES_ID" },
1658 [MIG_RP_MSG_MAX
] = { .len
= -1, .name
= "MAX" },
1662 * Process a request for pages received on the return path,
1663 * We're allowed to send more than requested (e.g. to round to our page size)
1664 * and we don't need to send pages that have already been sent.
1666 static void migrate_handle_rp_req_pages(MigrationState
*ms
, const char* rbname
,
1667 ram_addr_t start
, size_t len
)
1669 long our_host_ps
= getpagesize();
1671 trace_migrate_handle_rp_req_pages(rbname
, start
, len
);
1674 * Since we currently insist on matching page sizes, just sanity check
1675 * we're being asked for whole host pages.
1677 if (start
& (our_host_ps
-1) ||
1678 (len
& (our_host_ps
-1))) {
1679 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1680 " len: %zd", __func__
, start
, len
);
1681 mark_source_rp_bad(ms
);
1685 if (ram_save_queue_pages(rbname
, start
, len
)) {
1686 mark_source_rp_bad(ms
);
1691 * Handles messages sent on the return path towards the source VM
1694 static void *source_return_path_thread(void *opaque
)
1696 MigrationState
*ms
= opaque
;
1697 QEMUFile
*rp
= ms
->rp_state
.from_dst_file
;
1698 uint16_t header_len
, header_type
;
1700 uint32_t tmp32
, sibling_error
;
1701 ram_addr_t start
= 0; /* =0 to silence warning */
1702 size_t len
= 0, expected_len
;
1705 trace_source_return_path_thread_entry();
1706 while (!ms
->rp_state
.error
&& !qemu_file_get_error(rp
) &&
1707 migration_is_setup_or_active(ms
->state
)) {
1708 trace_source_return_path_thread_loop_top();
1709 header_type
= qemu_get_be16(rp
);
1710 header_len
= qemu_get_be16(rp
);
1712 if (header_type
>= MIG_RP_MSG_MAX
||
1713 header_type
== MIG_RP_MSG_INVALID
) {
1714 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1715 header_type
, header_len
);
1716 mark_source_rp_bad(ms
);
1720 if ((rp_cmd_args
[header_type
].len
!= -1 &&
1721 header_len
!= rp_cmd_args
[header_type
].len
) ||
1722 header_len
> sizeof(buf
)) {
1723 error_report("RP: Received '%s' message (0x%04x) with"
1724 "incorrect length %d expecting %zu",
1725 rp_cmd_args
[header_type
].name
, header_type
, header_len
,
1726 (size_t)rp_cmd_args
[header_type
].len
);
1727 mark_source_rp_bad(ms
);
1731 /* We know we've got a valid header by this point */
1732 res
= qemu_get_buffer(rp
, buf
, header_len
);
1733 if (res
!= header_len
) {
1734 error_report("RP: Failed reading data for message 0x%04x"
1735 " read %d expected %d",
1736 header_type
, res
, header_len
);
1737 mark_source_rp_bad(ms
);
1741 /* OK, we have the message and the data */
1742 switch (header_type
) {
1743 case MIG_RP_MSG_SHUT
:
1744 sibling_error
= ldl_be_p(buf
);
1745 trace_source_return_path_thread_shut(sibling_error
);
1746 if (sibling_error
) {
1747 error_report("RP: Sibling indicated error %d", sibling_error
);
1748 mark_source_rp_bad(ms
);
1751 * We'll let the main thread deal with closing the RP
1752 * we could do a shutdown(2) on it, but we're the only user
1753 * anyway, so there's nothing gained.
1757 case MIG_RP_MSG_PONG
:
1758 tmp32
= ldl_be_p(buf
);
1759 trace_source_return_path_thread_pong(tmp32
);
1762 case MIG_RP_MSG_REQ_PAGES
:
1763 start
= ldq_be_p(buf
);
1764 len
= ldl_be_p(buf
+ 8);
1765 migrate_handle_rp_req_pages(ms
, NULL
, start
, len
);
1768 case MIG_RP_MSG_REQ_PAGES_ID
:
1769 expected_len
= 12 + 1; /* header + termination */
1771 if (header_len
>= expected_len
) {
1772 start
= ldq_be_p(buf
);
1773 len
= ldl_be_p(buf
+ 8);
1774 /* Now we expect an idstr */
1775 tmp32
= buf
[12]; /* Length of the following idstr */
1776 buf
[13 + tmp32
] = '\0';
1777 expected_len
+= tmp32
;
1779 if (header_len
!= expected_len
) {
1780 error_report("RP: Req_Page_id with length %d expecting %zd",
1781 header_len
, expected_len
);
1782 mark_source_rp_bad(ms
);
1785 migrate_handle_rp_req_pages(ms
, (char *)&buf
[13], start
, len
);
1792 if (qemu_file_get_error(rp
)) {
1793 trace_source_return_path_thread_bad_end();
1794 mark_source_rp_bad(ms
);
1797 trace_source_return_path_thread_end();
1799 ms
->rp_state
.from_dst_file
= NULL
;
1804 static int open_return_path_on_source(MigrationState
*ms
)
1807 ms
->rp_state
.from_dst_file
= qemu_file_get_return_path(ms
->to_dst_file
);
1808 if (!ms
->rp_state
.from_dst_file
) {
1812 trace_open_return_path_on_source();
1813 qemu_thread_create(&ms
->rp_state
.rp_thread
, "return path",
1814 source_return_path_thread
, ms
, QEMU_THREAD_JOINABLE
);
1816 trace_open_return_path_on_source_continue();
1821 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1822 static int await_return_path_close_on_source(MigrationState
*ms
)
1825 * If this is a normal exit then the destination will send a SHUT and the
1826 * rp_thread will exit, however if there's an error we need to cause
1829 if (qemu_file_get_error(ms
->to_dst_file
) && ms
->rp_state
.from_dst_file
) {
1831 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1832 * waiting for the destination.
1834 qemu_file_shutdown(ms
->rp_state
.from_dst_file
);
1835 mark_source_rp_bad(ms
);
1837 trace_await_return_path_close_on_source_joining();
1838 qemu_thread_join(&ms
->rp_state
.rp_thread
);
1839 trace_await_return_path_close_on_source_close();
1840 return ms
->rp_state
.error
;
1844 * Switch from normal iteration to postcopy
1845 * Returns non-0 on error
1847 static int postcopy_start(MigrationState
*ms
)
1850 QIOChannelBuffer
*bioc
;
1852 int64_t time_at_stop
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1853 bool restart_block
= false;
1854 int cur_state
= MIGRATION_STATUS_ACTIVE
;
1855 if (!migrate_pause_before_switchover()) {
1856 migrate_set_state(&ms
->state
, MIGRATION_STATUS_ACTIVE
,
1857 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
1860 trace_postcopy_start();
1861 qemu_mutex_lock_iothread();
1862 trace_postcopy_start_set_run();
1864 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
1865 global_state_store();
1866 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
1871 ret
= migration_maybe_pause(ms
, &cur_state
,
1872 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
1877 ret
= bdrv_inactivate_all();
1881 restart_block
= true;
1884 * Cause any non-postcopiable, but iterative devices to
1885 * send out their final data.
1887 qemu_savevm_state_complete_precopy(ms
->to_dst_file
, true, false);
1890 * in Finish migrate and with the io-lock held everything should
1891 * be quiet, but we've potentially still got dirty pages and we
1892 * need to tell the destination to throw any pages it's already received
1895 if (migrate_postcopy_ram()) {
1896 if (ram_postcopy_send_discard_bitmap(ms
)) {
1897 error_report("postcopy send discard bitmap failed");
1903 * send rest of state - note things that are doing postcopy
1904 * will notice we're in POSTCOPY_ACTIVE and not actually
1905 * wrap their state up here
1907 qemu_file_set_rate_limit(ms
->to_dst_file
, INT64_MAX
);
1908 if (migrate_postcopy_ram()) {
1909 /* Ping just for debugging, helps line traces up */
1910 qemu_savevm_send_ping(ms
->to_dst_file
, 2);
1914 * While loading the device state we may trigger page transfer
1915 * requests and the fd must be free to process those, and thus
1916 * the destination must read the whole device state off the fd before
1917 * it starts processing it. Unfortunately the ad-hoc migration format
1918 * doesn't allow the destination to know the size to read without fully
1919 * parsing it through each devices load-state code (especially the open
1920 * coded devices that use get/put).
1921 * So we wrap the device state up in a package with a length at the start;
1922 * to do this we use a qemu_buf to hold the whole of the device state.
1924 bioc
= qio_channel_buffer_new(4096);
1925 qio_channel_set_name(QIO_CHANNEL(bioc
), "migration-postcopy-buffer");
1926 fb
= qemu_fopen_channel_output(QIO_CHANNEL(bioc
));
1927 object_unref(OBJECT(bioc
));
1930 * Make sure the receiver can get incoming pages before we send the rest
1933 qemu_savevm_send_postcopy_listen(fb
);
1935 qemu_savevm_state_complete_precopy(fb
, false, false);
1936 if (migrate_postcopy_ram()) {
1937 qemu_savevm_send_ping(fb
, 3);
1940 qemu_savevm_send_postcopy_run(fb
);
1942 /* <><> end of stuff going into the package */
1944 /* Last point of recovery; as soon as we send the package the destination
1945 * can open devices and potentially start running.
1946 * Lets just check again we've not got any errors.
1948 ret
= qemu_file_get_error(ms
->to_dst_file
);
1950 error_report("postcopy_start: Migration stream errored (pre package)");
1954 restart_block
= false;
1956 /* Now send that blob */
1957 if (qemu_savevm_send_packaged(ms
->to_dst_file
, bioc
->data
, bioc
->usage
)) {
1962 /* Send a notify to give a chance for anything that needs to happen
1963 * at the transition to postcopy and after the device state; in particular
1964 * spice needs to trigger a transition now
1966 ms
->postcopy_after_devices
= true;
1967 notifier_list_notify(&migration_state_notifiers
, ms
);
1969 ms
->downtime
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) - time_at_stop
;
1971 qemu_mutex_unlock_iothread();
1973 if (migrate_postcopy_ram()) {
1975 * Although this ping is just for debug, it could potentially be
1976 * used for getting a better measurement of downtime at the source.
1978 qemu_savevm_send_ping(ms
->to_dst_file
, 4);
1981 if (migrate_release_ram()) {
1982 ram_postcopy_migrated_memory_release(ms
);
1985 ret
= qemu_file_get_error(ms
->to_dst_file
);
1987 error_report("postcopy_start: Migration stream errored");
1988 migrate_set_state(&ms
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
1989 MIGRATION_STATUS_FAILED
);
1997 migrate_set_state(&ms
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
1998 MIGRATION_STATUS_FAILED
);
1999 if (restart_block
) {
2000 /* A failure happened early enough that we know the destination hasn't
2001 * accessed block devices, so we're safe to recover.
2003 Error
*local_err
= NULL
;
2005 bdrv_invalidate_cache_all(&local_err
);
2007 error_report_err(local_err
);
2010 qemu_mutex_unlock_iothread();
2015 * migration_maybe_pause: Pause if required to by
2016 * migrate_pause_before_switchover called with the iothread locked
2017 * Returns: 0 on success
2019 static int migration_maybe_pause(MigrationState
*s
,
2020 int *current_active_state
,
2023 if (!migrate_pause_before_switchover()) {
2027 /* Since leaving this state is not atomic with posting the semaphore
2028 * it's possible that someone could have issued multiple migrate_continue
2029 * and the semaphore is incorrectly positive at this point;
2030 * the docs say it's undefined to reinit a semaphore that's already
2031 * init'd, so use timedwait to eat up any existing posts.
2033 while (qemu_sem_timedwait(&s
->pause_sem
, 1) == 0) {
2034 /* This block intentionally left blank */
2037 qemu_mutex_unlock_iothread();
2038 migrate_set_state(&s
->state
, *current_active_state
,
2039 MIGRATION_STATUS_PRE_SWITCHOVER
);
2040 qemu_sem_wait(&s
->pause_sem
);
2041 migrate_set_state(&s
->state
, MIGRATION_STATUS_PRE_SWITCHOVER
,
2043 *current_active_state
= new_state
;
2044 qemu_mutex_lock_iothread();
2046 return s
->state
== new_state
? 0 : -EINVAL
;
2050 * migration_completion: Used by migration_thread when there's not much left.
2051 * The caller 'breaks' the loop when this returns.
2053 * @s: Current migration state
2055 static void migration_completion(MigrationState
*s
)
2058 int current_active_state
= s
->state
;
2060 if (s
->state
== MIGRATION_STATUS_ACTIVE
) {
2061 qemu_mutex_lock_iothread();
2062 s
->downtime_start
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
2063 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
2064 s
->vm_was_running
= runstate_is_running();
2065 ret
= global_state_store();
2068 bool inactivate
= !migrate_colo_enabled();
2069 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
2071 ret
= migration_maybe_pause(s
, ¤t_active_state
,
2072 MIGRATION_STATUS_DEVICE
);
2075 qemu_file_set_rate_limit(s
->to_dst_file
, INT64_MAX
);
2076 ret
= qemu_savevm_state_complete_precopy(s
->to_dst_file
, false,
2079 if (inactivate
&& ret
>= 0) {
2080 s
->block_inactive
= true;
2083 qemu_mutex_unlock_iothread();
2088 } else if (s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
2089 trace_migration_completion_postcopy_end();
2091 qemu_savevm_state_complete_postcopy(s
->to_dst_file
);
2092 trace_migration_completion_postcopy_end_after_complete();
2096 * If rp was opened we must clean up the thread before
2097 * cleaning everything else up (since if there are no failures
2098 * it will wait for the destination to send it's status in
2101 if (s
->rp_state
.from_dst_file
) {
2103 trace_migration_return_path_end_before();
2104 rp_error
= await_return_path_close_on_source(s
);
2105 trace_migration_return_path_end_after(rp_error
);
2107 goto fail_invalidate
;
2111 if (qemu_file_get_error(s
->to_dst_file
)) {
2112 trace_migration_completion_file_err();
2113 goto fail_invalidate
;
2116 if (!migrate_colo_enabled()) {
2117 migrate_set_state(&s
->state
, current_active_state
,
2118 MIGRATION_STATUS_COMPLETED
);
2124 /* If not doing postcopy, vm_start() will be called: let's regain
2125 * control on images.
2127 if (s
->state
== MIGRATION_STATUS_ACTIVE
||
2128 s
->state
== MIGRATION_STATUS_DEVICE
) {
2129 Error
*local_err
= NULL
;
2131 qemu_mutex_lock_iothread();
2132 bdrv_invalidate_cache_all(&local_err
);
2134 error_report_err(local_err
);
2136 s
->block_inactive
= false;
2138 qemu_mutex_unlock_iothread();
2142 migrate_set_state(&s
->state
, current_active_state
,
2143 MIGRATION_STATUS_FAILED
);
2146 bool migrate_colo_enabled(void)
2148 MigrationState
*s
= migrate_get_current();
2149 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_X_COLO
];
2152 static void migration_calculate_complete(MigrationState
*s
)
2154 uint64_t bytes
= qemu_ftell(s
->to_dst_file
);
2155 int64_t end_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
2157 s
->total_time
= end_time
- s
->start_time
;
2160 * It's still not set, so we are precopy migration. For
2161 * postcopy, downtime is calculated during postcopy_start().
2163 s
->downtime
= end_time
- s
->downtime_start
;
2166 if (s
->total_time
) {
2167 s
->mbps
= ((double) bytes
* 8.0) / s
->total_time
/ 1000;
2171 static void migration_update_counters(MigrationState
*s
,
2172 int64_t current_time
)
2174 uint64_t transferred
, time_spent
;
2177 if (current_time
< s
->iteration_start_time
+ BUFFER_DELAY
) {
2181 transferred
= qemu_ftell(s
->to_dst_file
) - s
->iteration_initial_bytes
;
2182 time_spent
= current_time
- s
->iteration_start_time
;
2183 bandwidth
= (double)transferred
/ time_spent
;
2184 s
->threshold_size
= bandwidth
* s
->parameters
.downtime_limit
;
2186 s
->mbps
= (((double) transferred
* 8.0) /
2187 ((double) time_spent
/ 1000.0)) / 1000.0 / 1000.0;
2190 * if we haven't sent anything, we don't want to
2191 * recalculate. 10000 is a small enough number for our purposes
2193 if (ram_counters
.dirty_pages_rate
&& transferred
> 10000) {
2194 s
->expected_downtime
= ram_counters
.dirty_pages_rate
*
2195 qemu_target_page_size() / bandwidth
;
2198 qemu_file_reset_rate_limit(s
->to_dst_file
);
2200 s
->iteration_start_time
= current_time
;
2201 s
->iteration_initial_bytes
= qemu_ftell(s
->to_dst_file
);
2203 trace_migrate_transferred(transferred
, time_spent
,
2204 bandwidth
, s
->threshold_size
);
2207 /* Migration thread iteration status */
2209 MIG_ITERATE_RESUME
, /* Resume current iteration */
2210 MIG_ITERATE_SKIP
, /* Skip current iteration */
2211 MIG_ITERATE_BREAK
, /* Break the loop */
2215 * Return true if continue to the next iteration directly, false
2218 static MigIterateState
migration_iteration_run(MigrationState
*s
)
2220 uint64_t pending_size
, pend_post
, pend_nonpost
;
2221 bool in_postcopy
= s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
;
2223 qemu_savevm_state_pending(s
->to_dst_file
, s
->threshold_size
,
2224 &pend_nonpost
, &pend_post
);
2225 pending_size
= pend_nonpost
+ pend_post
;
2227 trace_migrate_pending(pending_size
, s
->threshold_size
,
2228 pend_post
, pend_nonpost
);
2230 if (pending_size
&& pending_size
>= s
->threshold_size
) {
2231 /* Still a significant amount to transfer */
2232 if (migrate_postcopy() && !in_postcopy
&&
2233 pend_nonpost
<= s
->threshold_size
&&
2234 atomic_read(&s
->start_postcopy
)) {
2235 if (postcopy_start(s
)) {
2236 error_report("%s: postcopy failed to start", __func__
);
2238 return MIG_ITERATE_SKIP
;
2240 /* Just another iteration step */
2241 qemu_savevm_state_iterate(s
->to_dst_file
,
2242 s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
);
2244 trace_migration_thread_low_pending(pending_size
);
2245 migration_completion(s
);
2246 return MIG_ITERATE_BREAK
;
2249 return MIG_ITERATE_RESUME
;
2252 static void migration_iteration_finish(MigrationState
*s
)
2254 /* If we enabled cpu throttling for auto-converge, turn it off. */
2255 cpu_throttle_stop();
2257 qemu_mutex_lock_iothread();
2259 case MIGRATION_STATUS_COMPLETED
:
2260 migration_calculate_complete(s
);
2261 runstate_set(RUN_STATE_POSTMIGRATE
);
2264 case MIGRATION_STATUS_ACTIVE
:
2266 * We should really assert here, but since it's during
2267 * migration, let's try to reduce the usage of assertions.
2269 if (!migrate_colo_enabled()) {
2270 error_report("%s: critical error: calling COLO code without "
2271 "COLO enabled", __func__
);
2273 migrate_start_colo_process(s
);
2275 * Fixme: we will run VM in COLO no matter its old running state.
2276 * After exited COLO, we will keep running.
2278 s
->vm_was_running
= true;
2280 case MIGRATION_STATUS_FAILED
:
2281 case MIGRATION_STATUS_CANCELLED
:
2282 if (s
->vm_was_running
) {
2285 if (runstate_check(RUN_STATE_FINISH_MIGRATE
)) {
2286 runstate_set(RUN_STATE_POSTMIGRATE
);
2292 /* Should not reach here, but if so, forgive the VM. */
2293 error_report("%s: Unknown ending state %d", __func__
, s
->state
);
2296 qemu_bh_schedule(s
->cleanup_bh
);
2297 qemu_mutex_unlock_iothread();
2301 * Master migration thread on the source VM.
2302 * It drives the migration and pumps the data down the outgoing channel.
2304 static void *migration_thread(void *opaque
)
2306 MigrationState
*s
= opaque
;
2307 int64_t setup_start
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
2309 rcu_register_thread();
2311 s
->iteration_start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
2313 qemu_savevm_state_header(s
->to_dst_file
);
2316 * If we opened the return path, we need to make sure dst has it
2319 if (s
->rp_state
.from_dst_file
) {
2320 /* Now tell the dest that it should open its end so it can reply */
2321 qemu_savevm_send_open_return_path(s
->to_dst_file
);
2323 /* And do a ping that will make stuff easier to debug */
2324 qemu_savevm_send_ping(s
->to_dst_file
, 1);
2327 if (migrate_postcopy()) {
2329 * Tell the destination that we *might* want to do postcopy later;
2330 * if the other end can't do postcopy it should fail now, nice and
2333 qemu_savevm_send_postcopy_advise(s
->to_dst_file
);
2336 qemu_savevm_state_setup(s
->to_dst_file
);
2338 s
->setup_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
) - setup_start
;
2339 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
2340 MIGRATION_STATUS_ACTIVE
);
2342 trace_migration_thread_setup_complete();
2344 while (s
->state
== MIGRATION_STATUS_ACTIVE
||
2345 s
->state
== MIGRATION_STATUS_POSTCOPY_ACTIVE
) {
2346 int64_t current_time
;
2348 if (!qemu_file_rate_limit(s
->to_dst_file
)) {
2349 MigIterateState iter_state
= migration_iteration_run(s
);
2350 if (iter_state
== MIG_ITERATE_SKIP
) {
2352 } else if (iter_state
== MIG_ITERATE_BREAK
) {
2357 if (qemu_file_get_error(s
->to_dst_file
)) {
2358 if (migration_is_setup_or_active(s
->state
)) {
2359 migrate_set_state(&s
->state
, s
->state
,
2360 MIGRATION_STATUS_FAILED
);
2362 trace_migration_thread_file_err();
2366 current_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
2368 migration_update_counters(s
, current_time
);
2370 if (qemu_file_rate_limit(s
->to_dst_file
)) {
2371 /* usleep expects microseconds */
2372 g_usleep((s
->iteration_start_time
+ BUFFER_DELAY
-
2373 current_time
) * 1000);
2377 trace_migration_thread_after_loop();
2378 migration_iteration_finish(s
);
2379 rcu_unregister_thread();
2383 void migrate_fd_connect(MigrationState
*s
, Error
*error_in
)
2385 s
->expected_downtime
= s
->parameters
.downtime_limit
;
2386 s
->cleanup_bh
= qemu_bh_new(migrate_fd_cleanup
, s
);
2388 migrate_fd_error(s
, error_in
);
2389 migrate_fd_cleanup(s
);
2393 qemu_file_set_blocking(s
->to_dst_file
, true);
2394 qemu_file_set_rate_limit(s
->to_dst_file
,
2395 s
->parameters
.max_bandwidth
/ XFER_LIMIT_RATIO
);
2397 /* Notify before starting migration thread */
2398 notifier_list_notify(&migration_state_notifiers
, s
);
2401 * Open the return path. For postcopy, it is used exclusively. For
2402 * precopy, only if user specified "return-path" capability would
2403 * QEMU uses the return path.
2405 if (migrate_postcopy_ram() || migrate_use_return_path()) {
2406 if (open_return_path_on_source(s
)) {
2407 error_report("Unable to open return-path for postcopy");
2408 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
2409 MIGRATION_STATUS_FAILED
);
2410 migrate_fd_cleanup(s
);
2415 if (multifd_save_setup() != 0) {
2416 migrate_set_state(&s
->state
, MIGRATION_STATUS_SETUP
,
2417 MIGRATION_STATUS_FAILED
);
2418 migrate_fd_cleanup(s
);
2421 qemu_thread_create(&s
->thread
, "live_migration", migration_thread
, s
,
2422 QEMU_THREAD_JOINABLE
);
2423 s
->migration_thread_running
= true;
2426 void migration_global_dump(Monitor
*mon
)
2428 MigrationState
*ms
= migrate_get_current();
2430 monitor_printf(mon
, "globals:\n");
2431 monitor_printf(mon
, "store-global-state: %s\n",
2432 ms
->store_global_state
? "on" : "off");
2433 monitor_printf(mon
, "only-migratable: %s\n",
2434 ms
->only_migratable
? "on" : "off");
2435 monitor_printf(mon
, "send-configuration: %s\n",
2436 ms
->send_configuration
? "on" : "off");
2437 monitor_printf(mon
, "send-section-footer: %s\n",
2438 ms
->send_section_footer
? "on" : "off");
2441 #define DEFINE_PROP_MIG_CAP(name, x) \
2442 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
2444 static Property migration_properties
[] = {
2445 DEFINE_PROP_BOOL("store-global-state", MigrationState
,
2446 store_global_state
, true),
2447 DEFINE_PROP_BOOL("only-migratable", MigrationState
, only_migratable
, false),
2448 DEFINE_PROP_BOOL("send-configuration", MigrationState
,
2449 send_configuration
, true),
2450 DEFINE_PROP_BOOL("send-section-footer", MigrationState
,
2451 send_section_footer
, true),
2453 /* Migration parameters */
2454 DEFINE_PROP_UINT8("x-compress-level", MigrationState
,
2455 parameters
.compress_level
,
2456 DEFAULT_MIGRATE_COMPRESS_LEVEL
),
2457 DEFINE_PROP_UINT8("x-compress-threads", MigrationState
,
2458 parameters
.compress_threads
,
2459 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT
),
2460 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState
,
2461 parameters
.decompress_threads
,
2462 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT
),
2463 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState
,
2464 parameters
.cpu_throttle_initial
,
2465 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL
),
2466 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState
,
2467 parameters
.cpu_throttle_increment
,
2468 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT
),
2469 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState
,
2470 parameters
.max_bandwidth
, MAX_THROTTLE
),
2471 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState
,
2472 parameters
.downtime_limit
,
2473 DEFAULT_MIGRATE_SET_DOWNTIME
),
2474 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState
,
2475 parameters
.x_checkpoint_delay
,
2476 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY
),
2477 DEFINE_PROP_UINT8("x-multifd-channels", MigrationState
,
2478 parameters
.x_multifd_channels
,
2479 DEFAULT_MIGRATE_MULTIFD_CHANNELS
),
2480 DEFINE_PROP_UINT32("x-multifd-page-count", MigrationState
,
2481 parameters
.x_multifd_page_count
,
2482 DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT
),
2483 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState
,
2484 parameters
.xbzrle_cache_size
,
2485 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE
),
2487 /* Migration capabilities */
2488 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE
),
2489 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL
),
2490 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE
),
2491 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS
),
2492 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS
),
2493 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS
),
2494 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM
),
2495 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO
),
2496 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM
),
2497 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK
),
2498 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH
),
2499 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_X_MULTIFD
),
2501 DEFINE_PROP_END_OF_LIST(),
2504 static void migration_class_init(ObjectClass
*klass
, void *data
)
2506 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2508 dc
->user_creatable
= false;
2509 dc
->props
= migration_properties
;
2512 static void migration_instance_finalize(Object
*obj
)
2514 MigrationState
*ms
= MIGRATION_OBJ(obj
);
2515 MigrationParameters
*params
= &ms
->parameters
;
2517 qemu_mutex_destroy(&ms
->error_mutex
);
2518 g_free(params
->tls_hostname
);
2519 g_free(params
->tls_creds
);
2520 qemu_sem_destroy(&ms
->pause_sem
);
2523 static void migration_instance_init(Object
*obj
)
2525 MigrationState
*ms
= MIGRATION_OBJ(obj
);
2526 MigrationParameters
*params
= &ms
->parameters
;
2528 ms
->state
= MIGRATION_STATUS_NONE
;
2530 qemu_sem_init(&ms
->pause_sem
, 0);
2531 qemu_mutex_init(&ms
->error_mutex
);
2533 params
->tls_hostname
= g_strdup("");
2534 params
->tls_creds
= g_strdup("");
2536 /* Set has_* up only for parameter checks */
2537 params
->has_compress_level
= true;
2538 params
->has_compress_threads
= true;
2539 params
->has_decompress_threads
= true;
2540 params
->has_cpu_throttle_initial
= true;
2541 params
->has_cpu_throttle_increment
= true;
2542 params
->has_max_bandwidth
= true;
2543 params
->has_downtime_limit
= true;
2544 params
->has_x_checkpoint_delay
= true;
2545 params
->has_block_incremental
= true;
2546 params
->has_x_multifd_channels
= true;
2547 params
->has_x_multifd_page_count
= true;
2548 params
->has_xbzrle_cache_size
= true;
2552 * Return true if check pass, false otherwise. Error will be put
2553 * inside errp if provided.
2555 static bool migration_object_check(MigrationState
*ms
, Error
**errp
)
2557 MigrationCapabilityStatusList
*head
= NULL
;
2558 /* Assuming all off */
2559 bool cap_list
[MIGRATION_CAPABILITY__MAX
] = { 0 }, ret
;
2562 if (!migrate_params_check(&ms
->parameters
, errp
)) {
2566 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
2567 if (ms
->enabled_capabilities
[i
]) {
2568 head
= migrate_cap_add(head
, i
, true);
2572 ret
= migrate_caps_check(cap_list
, head
, errp
);
2574 /* It works with head == NULL */
2575 qapi_free_MigrationCapabilityStatusList(head
);
2580 static const TypeInfo migration_type
= {
2581 .name
= TYPE_MIGRATION
,
2583 * NOTE: TYPE_MIGRATION is not really a device, as the object is
2584 * not created using qdev_create(), it is not attached to the qdev
2585 * device tree, and it is never realized.
2587 * TODO: Make this TYPE_OBJECT once QOM provides something like
2588 * TYPE_DEVICE's "-global" properties.
2590 .parent
= TYPE_DEVICE
,
2591 .class_init
= migration_class_init
,
2592 .class_size
= sizeof(MigrationClass
),
2593 .instance_size
= sizeof(MigrationState
),
2594 .instance_init
= migration_instance_init
,
2595 .instance_finalize
= migration_instance_finalize
,
2598 static void register_migration_types(void)
2600 type_register_static(&migration_type
);
2603 type_init(register_migration_types
);