migration: Unshare MigrationParameters struct for now
[qemu/ar7.git] / migration / migration.c
blob205b801b70d233952a7ef2bd78af1acc1e0c98f9
1 /*
2 * QEMU live migration
4 * Copyright IBM, Corp. 2008
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu/osdep.h"
17 #include "qemu/cutils.h"
18 #include "qemu/error-report.h"
19 #include "migration/blocker.h"
20 #include "exec.h"
21 #include "fd.h"
22 #include "socket.h"
23 #include "rdma.h"
24 #include "ram.h"
25 #include "migration/global_state.h"
26 #include "migration/misc.h"
27 #include "migration.h"
28 #include "savevm.h"
29 #include "qemu-file-channel.h"
30 #include "qemu-file.h"
31 #include "migration/vmstate.h"
32 #include "block/block.h"
33 #include "qapi/qmp/qerror.h"
34 #include "qapi/util.h"
35 #include "qemu/rcu.h"
36 #include "block.h"
37 #include "postcopy-ram.h"
38 #include "qemu/thread.h"
39 #include "qmp-commands.h"
40 #include "trace.h"
41 #include "qapi-event.h"
42 #include "exec/target_page.h"
43 #include "io/channel-buffer.h"
44 #include "migration/colo.h"
45 #include "hw/boards.h"
46 #include "monitor/monitor.h"
48 #define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
50 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
51 * data. */
52 #define BUFFER_DELAY 100
53 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
55 /* Time in milliseconds we are allowed to stop the source,
56 * for sending the last part */
57 #define DEFAULT_MIGRATE_SET_DOWNTIME 300
59 /* Maximum migrate downtime set to 2000 seconds */
60 #define MAX_MIGRATE_DOWNTIME_SECONDS 2000
61 #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
63 /* Default compression thread count */
64 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
65 /* Default decompression thread count, usually decompression is at
66 * least 4 times as fast as compression.*/
67 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
68 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
69 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
70 /* Define default autoconverge cpu throttle migration parameters */
71 #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
72 #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
74 /* Migration XBZRLE default cache size */
75 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
77 /* The delay time (in ms) between two COLO checkpoints
78 * Note: Please change this default value to 10000 when we support hybrid mode.
80 #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
82 static NotifierList migration_state_notifiers =
83 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
85 static bool deferred_incoming;
87 /* Messages sent on the return path from destination to source */
88 enum mig_rp_message_type {
89 MIG_RP_MSG_INVALID = 0, /* Must be 0 */
90 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
91 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
93 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
94 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
96 MIG_RP_MSG_MAX
99 /* When we add fault tolerance, we could have several
100 migrations at once. For now we don't need to add
101 dynamic creation of migration */
103 static MigrationState *current_migration;
105 static bool migration_object_check(MigrationState *ms, Error **errp);
107 void migration_object_init(void)
109 MachineState *ms = MACHINE(qdev_get_machine());
110 Error *err = NULL;
112 /* This can only be called once. */
113 assert(!current_migration);
114 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
116 if (!migration_object_check(current_migration, &err)) {
117 error_report_err(err);
118 exit(1);
122 * We cannot really do this in migration_instance_init() since at
123 * that time global properties are not yet applied, then this
124 * value will be definitely replaced by something else.
126 if (ms->enforce_config_section) {
127 current_migration->send_configuration = true;
131 /* For outgoing */
132 MigrationState *migrate_get_current(void)
134 /* This can only be called after the object created. */
135 assert(current_migration);
136 return current_migration;
139 MigrationIncomingState *migration_incoming_get_current(void)
141 static bool once;
142 static MigrationIncomingState mis_current;
144 if (!once) {
145 mis_current.state = MIGRATION_STATUS_NONE;
146 memset(&mis_current, 0, sizeof(MigrationIncomingState));
147 qemu_mutex_init(&mis_current.rp_mutex);
148 qemu_event_init(&mis_current.main_thread_load_event, false);
149 once = true;
151 return &mis_current;
154 void migration_incoming_state_destroy(void)
156 struct MigrationIncomingState *mis = migration_incoming_get_current();
158 if (mis->to_src_file) {
159 /* Tell source that we are done */
160 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
161 qemu_fclose(mis->to_src_file);
162 mis->to_src_file = NULL;
165 if (mis->from_src_file) {
166 qemu_fclose(mis->from_src_file);
167 mis->from_src_file = NULL;
170 qemu_event_destroy(&mis->main_thread_load_event);
173 static void migrate_generate_event(int new_state)
175 if (migrate_use_events()) {
176 qapi_event_send_migration(new_state, &error_abort);
181 * Called on -incoming with a defer: uri.
182 * The migration can be started later after any parameters have been
183 * changed.
185 static void deferred_incoming_migration(Error **errp)
187 if (deferred_incoming) {
188 error_setg(errp, "Incoming migration already deferred");
190 deferred_incoming = true;
194 * Send a message on the return channel back to the source
195 * of the migration.
197 static void migrate_send_rp_message(MigrationIncomingState *mis,
198 enum mig_rp_message_type message_type,
199 uint16_t len, void *data)
201 trace_migrate_send_rp_message((int)message_type, len);
202 qemu_mutex_lock(&mis->rp_mutex);
203 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
204 qemu_put_be16(mis->to_src_file, len);
205 qemu_put_buffer(mis->to_src_file, data, len);
206 qemu_fflush(mis->to_src_file);
207 qemu_mutex_unlock(&mis->rp_mutex);
210 /* Request a range of pages from the source VM at the given
211 * start address.
212 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
213 * as the last request (a name must have been given previously)
214 * Start: Address offset within the RB
215 * Len: Length in bytes required - must be a multiple of pagesize
217 void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
218 ram_addr_t start, size_t len)
220 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
221 size_t msglen = 12; /* start + len */
223 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
224 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
226 if (rbname) {
227 int rbname_len = strlen(rbname);
228 assert(rbname_len < 256);
230 bufc[msglen++] = rbname_len;
231 memcpy(bufc + msglen, rbname, rbname_len);
232 msglen += rbname_len;
233 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc);
234 } else {
235 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc);
239 void qemu_start_incoming_migration(const char *uri, Error **errp)
241 const char *p;
243 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort);
244 if (!strcmp(uri, "defer")) {
245 deferred_incoming_migration(errp);
246 } else if (strstart(uri, "tcp:", &p)) {
247 tcp_start_incoming_migration(p, errp);
248 #ifdef CONFIG_RDMA
249 } else if (strstart(uri, "rdma:", &p)) {
250 rdma_start_incoming_migration(p, errp);
251 #endif
252 } else if (strstart(uri, "exec:", &p)) {
253 exec_start_incoming_migration(p, errp);
254 } else if (strstart(uri, "unix:", &p)) {
255 unix_start_incoming_migration(p, errp);
256 } else if (strstart(uri, "fd:", &p)) {
257 fd_start_incoming_migration(p, errp);
258 } else {
259 error_setg(errp, "unknown migration protocol: %s", uri);
263 static void process_incoming_migration_bh(void *opaque)
265 Error *local_err = NULL;
266 MigrationIncomingState *mis = opaque;
268 /* Make sure all file formats flush their mutable metadata.
269 * If we get an error here, just don't restart the VM yet. */
270 bdrv_invalidate_cache_all(&local_err);
271 if (local_err) {
272 error_report_err(local_err);
273 local_err = NULL;
274 autostart = false;
278 * This must happen after all error conditions are dealt with and
279 * we're sure the VM is going to be running on this host.
281 qemu_announce_self();
283 /* If global state section was not received or we are in running
284 state, we need to obey autostart. Any other state is set with
285 runstate_set. */
287 if (!global_state_received() ||
288 global_state_get_runstate() == RUN_STATE_RUNNING) {
289 if (autostart) {
290 vm_start();
291 } else {
292 runstate_set(RUN_STATE_PAUSED);
294 } else {
295 runstate_set(global_state_get_runstate());
298 * This must happen after any state changes since as soon as an external
299 * observer sees this event they might start to prod at the VM assuming
300 * it's ready to use.
302 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
303 MIGRATION_STATUS_COMPLETED);
304 qemu_bh_delete(mis->bh);
305 migration_incoming_state_destroy();
308 static void process_incoming_migration_co(void *opaque)
310 QEMUFile *f = opaque;
311 MigrationIncomingState *mis = migration_incoming_get_current();
312 PostcopyState ps;
313 int ret;
315 mis->from_src_file = f;
316 mis->largest_page_size = qemu_ram_pagesize_largest();
317 postcopy_state_set(POSTCOPY_INCOMING_NONE);
318 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
319 MIGRATION_STATUS_ACTIVE);
320 ret = qemu_loadvm_state(f);
322 ps = postcopy_state_get();
323 trace_process_incoming_migration_co_end(ret, ps);
324 if (ps != POSTCOPY_INCOMING_NONE) {
325 if (ps == POSTCOPY_INCOMING_ADVISE) {
327 * Where a migration had postcopy enabled (and thus went to advise)
328 * but managed to complete within the precopy period, we can use
329 * the normal exit.
331 postcopy_ram_incoming_cleanup(mis);
332 } else if (ret >= 0) {
334 * Postcopy was started, cleanup should happen at the end of the
335 * postcopy thread.
337 trace_process_incoming_migration_co_postcopy_end_main();
338 return;
340 /* Else if something went wrong then just fall out of the normal exit */
343 /* we get COLO info, and know if we are in COLO mode */
344 if (!ret && migration_incoming_enable_colo()) {
345 mis->migration_incoming_co = qemu_coroutine_self();
346 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
347 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
348 mis->have_colo_incoming_thread = true;
349 qemu_coroutine_yield();
351 /* Wait checkpoint incoming thread exit before free resource */
352 qemu_thread_join(&mis->colo_incoming_thread);
355 if (ret < 0) {
356 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
357 MIGRATION_STATUS_FAILED);
358 error_report("load of migration failed: %s", strerror(-ret));
359 qemu_fclose(mis->from_src_file);
360 exit(EXIT_FAILURE);
362 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
363 qemu_bh_schedule(mis->bh);
366 void migration_fd_process_incoming(QEMUFile *f)
368 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, f);
370 qemu_file_set_blocking(f, false);
371 qemu_coroutine_enter(co);
375 * Send a 'SHUT' message on the return channel with the given value
376 * to indicate that we've finished with the RP. Non-0 value indicates
377 * error.
379 void migrate_send_rp_shut(MigrationIncomingState *mis,
380 uint32_t value)
382 uint32_t buf;
384 buf = cpu_to_be32(value);
385 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
389 * Send a 'PONG' message on the return channel with the given value
390 * (normally in response to a 'PING')
392 void migrate_send_rp_pong(MigrationIncomingState *mis,
393 uint32_t value)
395 uint32_t buf;
397 buf = cpu_to_be32(value);
398 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
401 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
403 MigrationCapabilityStatusList *head = NULL;
404 MigrationCapabilityStatusList *caps;
405 MigrationState *s = migrate_get_current();
406 int i;
408 caps = NULL; /* silence compiler warning */
409 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
410 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
411 if (i == MIGRATION_CAPABILITY_BLOCK) {
412 continue;
414 #endif
415 if (head == NULL) {
416 head = g_malloc0(sizeof(*caps));
417 caps = head;
418 } else {
419 caps->next = g_malloc0(sizeof(*caps));
420 caps = caps->next;
422 caps->value =
423 g_malloc(sizeof(*caps->value));
424 caps->value->capability = i;
425 caps->value->state = s->enabled_capabilities[i];
428 return head;
431 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
433 MigrationParameters *params;
434 MigrationState *s = migrate_get_current();
436 /* TODO use QAPI_CLONE() instead of duplicating it inline */
437 params = g_malloc0(sizeof(*params));
438 params->has_compress_level = true;
439 params->compress_level = s->parameters.compress_level;
440 params->has_compress_threads = true;
441 params->compress_threads = s->parameters.compress_threads;
442 params->has_decompress_threads = true;
443 params->decompress_threads = s->parameters.decompress_threads;
444 params->has_cpu_throttle_initial = true;
445 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
446 params->has_cpu_throttle_increment = true;
447 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
448 params->has_tls_creds = true;
449 params->tls_creds = g_strdup(s->parameters.tls_creds);
450 params->has_tls_hostname = true;
451 params->tls_hostname = g_strdup(s->parameters.tls_hostname);
452 params->has_max_bandwidth = true;
453 params->max_bandwidth = s->parameters.max_bandwidth;
454 params->has_downtime_limit = true;
455 params->downtime_limit = s->parameters.downtime_limit;
456 params->has_x_checkpoint_delay = true;
457 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
458 params->has_block_incremental = true;
459 params->block_incremental = s->parameters.block_incremental;
461 return params;
465 * Return true if we're already in the middle of a migration
466 * (i.e. any of the active or setup states)
468 static bool migration_is_setup_or_active(int state)
470 switch (state) {
471 case MIGRATION_STATUS_ACTIVE:
472 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
473 case MIGRATION_STATUS_SETUP:
474 return true;
476 default:
477 return false;
482 static void populate_ram_info(MigrationInfo *info, MigrationState *s)
484 info->has_ram = true;
485 info->ram = g_malloc0(sizeof(*info->ram));
486 info->ram->transferred = ram_counters.transferred;
487 info->ram->total = ram_bytes_total();
488 info->ram->duplicate = ram_counters.duplicate;
489 /* legacy value. It is not used anymore */
490 info->ram->skipped = 0;
491 info->ram->normal = ram_counters.normal;
492 info->ram->normal_bytes = ram_counters.normal *
493 qemu_target_page_size();
494 info->ram->mbps = s->mbps;
495 info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
496 info->ram->postcopy_requests = ram_counters.postcopy_requests;
497 info->ram->page_size = qemu_target_page_size();
499 if (migrate_use_xbzrle()) {
500 info->has_xbzrle_cache = true;
501 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
502 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
503 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
504 info->xbzrle_cache->pages = xbzrle_counters.pages;
505 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
506 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
507 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
510 if (cpu_throttle_active()) {
511 info->has_cpu_throttle_percentage = true;
512 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
515 if (s->state != MIGRATION_STATUS_COMPLETED) {
516 info->ram->remaining = ram_bytes_remaining();
517 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
521 static void populate_disk_info(MigrationInfo *info)
523 if (blk_mig_active()) {
524 info->has_disk = true;
525 info->disk = g_malloc0(sizeof(*info->disk));
526 info->disk->transferred = blk_mig_bytes_transferred();
527 info->disk->remaining = blk_mig_bytes_remaining();
528 info->disk->total = blk_mig_bytes_total();
532 MigrationInfo *qmp_query_migrate(Error **errp)
534 MigrationInfo *info = g_malloc0(sizeof(*info));
535 MigrationState *s = migrate_get_current();
537 switch (s->state) {
538 case MIGRATION_STATUS_NONE:
539 /* no migration has happened ever */
540 break;
541 case MIGRATION_STATUS_SETUP:
542 info->has_status = true;
543 info->has_total_time = false;
544 break;
545 case MIGRATION_STATUS_ACTIVE:
546 case MIGRATION_STATUS_CANCELLING:
547 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
548 /* TODO add some postcopy stats */
549 info->has_status = true;
550 info->has_total_time = true;
551 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
552 - s->total_time;
553 info->has_expected_downtime = true;
554 info->expected_downtime = s->expected_downtime;
555 info->has_setup_time = true;
556 info->setup_time = s->setup_time;
558 populate_ram_info(info, s);
559 populate_disk_info(info);
560 break;
561 case MIGRATION_STATUS_COLO:
562 info->has_status = true;
563 /* TODO: display COLO specific information (checkpoint info etc.) */
564 break;
565 case MIGRATION_STATUS_COMPLETED:
566 info->has_status = true;
567 info->has_total_time = true;
568 info->total_time = s->total_time;
569 info->has_downtime = true;
570 info->downtime = s->downtime;
571 info->has_setup_time = true;
572 info->setup_time = s->setup_time;
574 populate_ram_info(info, s);
575 break;
576 case MIGRATION_STATUS_FAILED:
577 info->has_status = true;
578 if (s->error) {
579 info->has_error_desc = true;
580 info->error_desc = g_strdup(error_get_pretty(s->error));
582 break;
583 case MIGRATION_STATUS_CANCELLED:
584 info->has_status = true;
585 break;
587 info->status = s->state;
589 return info;
593 * @migration_caps_check - check capability validity
595 * @cap_list: old capability list, array of bool
596 * @params: new capabilities to be applied soon
597 * @errp: set *errp if the check failed, with reason
599 * Returns true if check passed, otherwise false.
601 static bool migrate_caps_check(bool *cap_list,
602 MigrationCapabilityStatusList *params,
603 Error **errp)
605 MigrationCapabilityStatusList *cap;
606 bool old_postcopy_cap;
608 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
610 for (cap = params; cap; cap = cap->next) {
611 cap_list[cap->value->capability] = cap->value->state;
614 #ifndef CONFIG_LIVE_BLOCK_MIGRATION
615 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
616 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
617 "block migration");
618 error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
619 return false;
621 #endif
623 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
624 if (cap_list[MIGRATION_CAPABILITY_COMPRESS]) {
625 /* The decompression threads asynchronously write into RAM
626 * rather than use the atomic copies needed to avoid
627 * userfaulting. It should be possible to fix the decompression
628 * threads for compatibility in future.
630 error_setg(errp, "Postcopy is not currently compatible "
631 "with compression");
632 return false;
635 /* This check is reasonably expensive, so only when it's being
636 * set the first time, also it's only the destination that needs
637 * special support.
639 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
640 !postcopy_ram_supported_by_host()) {
641 /* postcopy_ram_supported_by_host will have emitted a more
642 * detailed message
644 error_setg(errp, "Postcopy is not supported");
645 return false;
649 return true;
652 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
653 Error **errp)
655 MigrationState *s = migrate_get_current();
656 MigrationCapabilityStatusList *cap;
658 if (migration_is_setup_or_active(s->state)) {
659 error_setg(errp, QERR_MIGRATION_ACTIVE);
660 return;
663 if (!migrate_caps_check(s->enabled_capabilities, params, errp)) {
664 return;
667 for (cap = params; cap; cap = cap->next) {
668 s->enabled_capabilities[cap->value->capability] = cap->value->state;
673 * Check whether the parameters are valid. Error will be put into errp
674 * (if provided). Return true if valid, otherwise false.
676 static bool migrate_params_check(MigrationParameters *params, Error **errp)
678 if (params->has_compress_level &&
679 (params->compress_level < 0 || params->compress_level > 9)) {
680 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
681 "is invalid, it should be in the range of 0 to 9");
682 return false;
685 if (params->has_compress_threads &&
686 (params->compress_threads < 1 || params->compress_threads > 255)) {
687 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
688 "compress_threads",
689 "is invalid, it should be in the range of 1 to 255");
690 return false;
693 if (params->has_decompress_threads &&
694 (params->decompress_threads < 1 || params->decompress_threads > 255)) {
695 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
696 "decompress_threads",
697 "is invalid, it should be in the range of 1 to 255");
698 return false;
701 if (params->has_cpu_throttle_initial &&
702 (params->cpu_throttle_initial < 1 ||
703 params->cpu_throttle_initial > 99)) {
704 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
705 "cpu_throttle_initial",
706 "an integer in the range of 1 to 99");
707 return false;
710 if (params->has_cpu_throttle_increment &&
711 (params->cpu_throttle_increment < 1 ||
712 params->cpu_throttle_increment > 99)) {
713 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
714 "cpu_throttle_increment",
715 "an integer in the range of 1 to 99");
716 return false;
719 if (params->has_max_bandwidth &&
720 (params->max_bandwidth < 0 || params->max_bandwidth > SIZE_MAX)) {
721 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the"
722 " range of 0 to %zu bytes/second", SIZE_MAX);
723 return false;
726 if (params->has_downtime_limit &&
727 (params->downtime_limit < 0 ||
728 params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
729 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
730 "the range of 0 to %d milliseconds",
731 MAX_MIGRATE_DOWNTIME);
732 return false;
735 if (params->has_x_checkpoint_delay && (params->x_checkpoint_delay < 0)) {
736 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
737 "x_checkpoint_delay",
738 "is invalid, it should be positive");
739 return false;
742 return true;
745 static void migrate_params_test_apply(MigrateSetParameters *params,
746 MigrationParameters *dest)
748 *dest = migrate_get_current()->parameters;
750 /* TODO use QAPI_CLONE() instead of duplicating it inline */
752 if (params->has_compress_level) {
753 dest->compress_level = params->compress_level;
756 if (params->has_compress_threads) {
757 dest->compress_threads = params->compress_threads;
760 if (params->has_decompress_threads) {
761 dest->decompress_threads = params->decompress_threads;
764 if (params->has_cpu_throttle_initial) {
765 dest->cpu_throttle_initial = params->cpu_throttle_initial;
768 if (params->has_cpu_throttle_increment) {
769 dest->cpu_throttle_increment = params->cpu_throttle_increment;
772 if (params->has_tls_creds) {
773 dest->tls_creds = g_strdup(params->tls_creds);
776 if (params->has_tls_hostname) {
777 dest->tls_hostname = g_strdup(params->tls_hostname);
780 if (params->has_max_bandwidth) {
781 dest->max_bandwidth = params->max_bandwidth;
784 if (params->has_downtime_limit) {
785 dest->downtime_limit = params->downtime_limit;
788 if (params->has_x_checkpoint_delay) {
789 dest->x_checkpoint_delay = params->x_checkpoint_delay;
792 if (params->has_block_incremental) {
793 dest->block_incremental = params->block_incremental;
797 static void migrate_params_apply(MigrateSetParameters *params)
799 MigrationState *s = migrate_get_current();
801 /* TODO use QAPI_CLONE() instead of duplicating it inline */
803 if (params->has_compress_level) {
804 s->parameters.compress_level = params->compress_level;
807 if (params->has_compress_threads) {
808 s->parameters.compress_threads = params->compress_threads;
811 if (params->has_decompress_threads) {
812 s->parameters.decompress_threads = params->decompress_threads;
815 if (params->has_cpu_throttle_initial) {
816 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
819 if (params->has_cpu_throttle_increment) {
820 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
823 if (params->has_tls_creds) {
824 g_free(s->parameters.tls_creds);
825 s->parameters.tls_creds = g_strdup(params->tls_creds);
828 if (params->has_tls_hostname) {
829 g_free(s->parameters.tls_hostname);
830 s->parameters.tls_hostname = g_strdup(params->tls_hostname);
833 if (params->has_max_bandwidth) {
834 s->parameters.max_bandwidth = params->max_bandwidth;
835 if (s->to_dst_file) {
836 qemu_file_set_rate_limit(s->to_dst_file,
837 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
841 if (params->has_downtime_limit) {
842 s->parameters.downtime_limit = params->downtime_limit;
845 if (params->has_x_checkpoint_delay) {
846 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
847 if (migration_in_colo_state()) {
848 colo_checkpoint_notify(s);
852 if (params->has_block_incremental) {
853 s->parameters.block_incremental = params->block_incremental;
857 void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
859 MigrationParameters tmp;
861 migrate_params_test_apply(params, &tmp);
863 if (!migrate_params_check(&tmp, errp)) {
864 /* Invalid parameter */
865 return;
868 migrate_params_apply(params);
872 void qmp_migrate_start_postcopy(Error **errp)
874 MigrationState *s = migrate_get_current();
876 if (!migrate_postcopy_ram()) {
877 error_setg(errp, "Enable postcopy with migrate_set_capability before"
878 " the start of migration");
879 return;
882 if (s->state == MIGRATION_STATUS_NONE) {
883 error_setg(errp, "Postcopy must be started after migration has been"
884 " started");
885 return;
888 * we don't error if migration has finished since that would be racy
889 * with issuing this command.
891 atomic_set(&s->start_postcopy, true);
894 /* shared migration helpers */
896 void migrate_set_state(int *state, int old_state, int new_state)
898 if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
899 trace_migrate_set_state(new_state);
900 migrate_generate_event(new_state);
904 static MigrationCapabilityStatusList *migrate_cap_add(
905 MigrationCapabilityStatusList *list,
906 MigrationCapability index,
907 bool state)
909 MigrationCapabilityStatusList *cap;
911 cap = g_new0(MigrationCapabilityStatusList, 1);
912 cap->value = g_new0(MigrationCapabilityStatus, 1);
913 cap->value->capability = index;
914 cap->value->state = state;
915 cap->next = list;
917 return cap;
920 void migrate_set_block_enabled(bool value, Error **errp)
922 MigrationCapabilityStatusList *cap;
924 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
925 qmp_migrate_set_capabilities(cap, errp);
926 qapi_free_MigrationCapabilityStatusList(cap);
929 static void migrate_set_block_incremental(MigrationState *s, bool value)
931 s->parameters.block_incremental = value;
934 static void block_cleanup_parameters(MigrationState *s)
936 if (s->must_remove_block_options) {
937 /* setting to false can never fail */
938 migrate_set_block_enabled(false, &error_abort);
939 migrate_set_block_incremental(s, false);
940 s->must_remove_block_options = false;
944 static void migrate_fd_cleanup(void *opaque)
946 MigrationState *s = opaque;
948 qemu_bh_delete(s->cleanup_bh);
949 s->cleanup_bh = NULL;
951 if (s->to_dst_file) {
952 trace_migrate_fd_cleanup();
953 qemu_mutex_unlock_iothread();
954 if (s->migration_thread_running) {
955 qemu_thread_join(&s->thread);
956 s->migration_thread_running = false;
958 qemu_mutex_lock_iothread();
960 qemu_fclose(s->to_dst_file);
961 s->to_dst_file = NULL;
964 assert((s->state != MIGRATION_STATUS_ACTIVE) &&
965 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE));
967 if (s->state == MIGRATION_STATUS_CANCELLING) {
968 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
969 MIGRATION_STATUS_CANCELLED);
972 notifier_list_notify(&migration_state_notifiers, s);
973 block_cleanup_parameters(s);
976 void migrate_fd_error(MigrationState *s, const Error *error)
978 trace_migrate_fd_error(error_get_pretty(error));
979 assert(s->to_dst_file == NULL);
980 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
981 MIGRATION_STATUS_FAILED);
982 if (!s->error) {
983 s->error = error_copy(error);
985 notifier_list_notify(&migration_state_notifiers, s);
986 block_cleanup_parameters(s);
989 static void migrate_fd_cancel(MigrationState *s)
991 int old_state ;
992 QEMUFile *f = migrate_get_current()->to_dst_file;
993 trace_migrate_fd_cancel();
995 if (s->rp_state.from_dst_file) {
996 /* shutdown the rp socket, so causing the rp thread to shutdown */
997 qemu_file_shutdown(s->rp_state.from_dst_file);
1000 do {
1001 old_state = s->state;
1002 if (!migration_is_setup_or_active(old_state)) {
1003 break;
1005 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
1006 } while (s->state != MIGRATION_STATUS_CANCELLING);
1009 * If we're unlucky the migration code might be stuck somewhere in a
1010 * send/write while the network has failed and is waiting to timeout;
1011 * if we've got shutdown(2) available then we can force it to quit.
1012 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
1013 * called in a bh, so there is no race against this cancel.
1015 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
1016 qemu_file_shutdown(f);
1018 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
1019 Error *local_err = NULL;
1021 bdrv_invalidate_cache_all(&local_err);
1022 if (local_err) {
1023 error_report_err(local_err);
1024 } else {
1025 s->block_inactive = false;
1028 block_cleanup_parameters(s);
1031 void add_migration_state_change_notifier(Notifier *notify)
1033 notifier_list_add(&migration_state_notifiers, notify);
1036 void remove_migration_state_change_notifier(Notifier *notify)
1038 notifier_remove(notify);
1041 bool migration_in_setup(MigrationState *s)
1043 return s->state == MIGRATION_STATUS_SETUP;
1046 bool migration_has_finished(MigrationState *s)
1048 return s->state == MIGRATION_STATUS_COMPLETED;
1051 bool migration_has_failed(MigrationState *s)
1053 return (s->state == MIGRATION_STATUS_CANCELLED ||
1054 s->state == MIGRATION_STATUS_FAILED);
1057 bool migration_in_postcopy(void)
1059 MigrationState *s = migrate_get_current();
1061 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1064 bool migration_in_postcopy_after_devices(MigrationState *s)
1066 return migration_in_postcopy() && s->postcopy_after_devices;
1069 bool migration_is_idle(void)
1071 MigrationState *s = migrate_get_current();
1073 switch (s->state) {
1074 case MIGRATION_STATUS_NONE:
1075 case MIGRATION_STATUS_CANCELLED:
1076 case MIGRATION_STATUS_COMPLETED:
1077 case MIGRATION_STATUS_FAILED:
1078 return true;
1079 case MIGRATION_STATUS_SETUP:
1080 case MIGRATION_STATUS_CANCELLING:
1081 case MIGRATION_STATUS_ACTIVE:
1082 case MIGRATION_STATUS_POSTCOPY_ACTIVE:
1083 case MIGRATION_STATUS_COLO:
1084 return false;
1085 case MIGRATION_STATUS__MAX:
1086 g_assert_not_reached();
1089 return false;
1092 MigrationState *migrate_init(void)
1094 MigrationState *s = migrate_get_current();
1097 * Reinitialise all migration state, except
1098 * parameters/capabilities that the user set, and
1099 * locks.
1101 s->bytes_xfer = 0;
1102 s->xfer_limit = 0;
1103 s->cleanup_bh = 0;
1104 s->to_dst_file = NULL;
1105 s->state = MIGRATION_STATUS_NONE;
1106 s->rp_state.from_dst_file = NULL;
1107 s->rp_state.error = false;
1108 s->mbps = 0.0;
1109 s->downtime = 0;
1110 s->expected_downtime = 0;
1111 s->setup_time = 0;
1112 s->start_postcopy = false;
1113 s->postcopy_after_devices = false;
1114 s->migration_thread_running = false;
1115 error_free(s->error);
1116 s->error = NULL;
1118 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
1120 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1121 return s;
1124 static GSList *migration_blockers;
1126 int migrate_add_blocker(Error *reason, Error **errp)
1128 if (migrate_get_current()->only_migratable) {
1129 error_propagate(errp, error_copy(reason));
1130 error_prepend(errp, "disallowing migration blocker "
1131 "(--only_migratable) for: ");
1132 return -EACCES;
1135 if (migration_is_idle()) {
1136 migration_blockers = g_slist_prepend(migration_blockers, reason);
1137 return 0;
1140 error_propagate(errp, error_copy(reason));
1141 error_prepend(errp, "disallowing migration blocker (migration in "
1142 "progress) for: ");
1143 return -EBUSY;
1146 void migrate_del_blocker(Error *reason)
1148 migration_blockers = g_slist_remove(migration_blockers, reason);
1151 void qmp_migrate_incoming(const char *uri, Error **errp)
1153 Error *local_err = NULL;
1154 static bool once = true;
1156 if (!deferred_incoming) {
1157 error_setg(errp, "For use with '-incoming defer'");
1158 return;
1160 if (!once) {
1161 error_setg(errp, "The incoming migration has already been started");
1164 qemu_start_incoming_migration(uri, &local_err);
1166 if (local_err) {
1167 error_propagate(errp, local_err);
1168 return;
1171 once = false;
1174 bool migration_is_blocked(Error **errp)
1176 if (qemu_savevm_state_blocked(errp)) {
1177 return true;
1180 if (migration_blockers) {
1181 error_propagate(errp, error_copy(migration_blockers->data));
1182 return true;
1185 return false;
1188 void qmp_migrate(const char *uri, bool has_blk, bool blk,
1189 bool has_inc, bool inc, bool has_detach, bool detach,
1190 Error **errp)
1192 Error *local_err = NULL;
1193 MigrationState *s = migrate_get_current();
1194 const char *p;
1196 if (migration_is_setup_or_active(s->state) ||
1197 s->state == MIGRATION_STATUS_CANCELLING ||
1198 s->state == MIGRATION_STATUS_COLO) {
1199 error_setg(errp, QERR_MIGRATION_ACTIVE);
1200 return;
1202 if (runstate_check(RUN_STATE_INMIGRATE)) {
1203 error_setg(errp, "Guest is waiting for an incoming migration");
1204 return;
1207 if (migration_is_blocked(errp)) {
1208 return;
1211 if ((has_blk && blk) || (has_inc && inc)) {
1212 if (migrate_use_block() || migrate_use_block_incremental()) {
1213 error_setg(errp, "Command options are incompatible with "
1214 "current migration capabilities");
1215 return;
1217 migrate_set_block_enabled(true, &local_err);
1218 if (local_err) {
1219 error_propagate(errp, local_err);
1220 return;
1222 s->must_remove_block_options = true;
1225 if (has_inc && inc) {
1226 migrate_set_block_incremental(s, true);
1229 s = migrate_init();
1231 if (strstart(uri, "tcp:", &p)) {
1232 tcp_start_outgoing_migration(s, p, &local_err);
1233 #ifdef CONFIG_RDMA
1234 } else if (strstart(uri, "rdma:", &p)) {
1235 rdma_start_outgoing_migration(s, p, &local_err);
1236 #endif
1237 } else if (strstart(uri, "exec:", &p)) {
1238 exec_start_outgoing_migration(s, p, &local_err);
1239 } else if (strstart(uri, "unix:", &p)) {
1240 unix_start_outgoing_migration(s, p, &local_err);
1241 } else if (strstart(uri, "fd:", &p)) {
1242 fd_start_outgoing_migration(s, p, &local_err);
1243 } else {
1244 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
1245 "a valid migration protocol");
1246 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1247 MIGRATION_STATUS_FAILED);
1248 return;
1251 if (local_err) {
1252 migrate_fd_error(s, local_err);
1253 error_propagate(errp, local_err);
1254 return;
1258 void qmp_migrate_cancel(Error **errp)
1260 migrate_fd_cancel(migrate_get_current());
1263 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
1265 MigrationState *s = migrate_get_current();
1266 int64_t new_size;
1268 /* Check for truncation */
1269 if (value != (size_t)value) {
1270 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1271 "exceeding address space");
1272 return;
1275 /* Cache should not be larger than guest ram size */
1276 if (value > ram_bytes_total()) {
1277 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1278 "exceeds guest ram size ");
1279 return;
1282 new_size = xbzrle_cache_resize(value);
1283 if (new_size < 0) {
1284 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
1285 "is smaller than page size");
1286 return;
1289 s->xbzrle_cache_size = new_size;
1292 int64_t qmp_query_migrate_cache_size(Error **errp)
1294 return migrate_xbzrle_cache_size();
1297 void qmp_migrate_set_speed(int64_t value, Error **errp)
1299 MigrateSetParameters p = {
1300 .has_max_bandwidth = true,
1301 .max_bandwidth = value,
1304 qmp_migrate_set_parameters(&p, errp);
1307 void qmp_migrate_set_downtime(double value, Error **errp)
1309 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
1310 error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
1311 "the range of 0 to %d seconds",
1312 MAX_MIGRATE_DOWNTIME_SECONDS);
1313 return;
1316 value *= 1000; /* Convert to milliseconds */
1317 value = MAX(0, MIN(INT64_MAX, value));
1319 MigrateSetParameters p = {
1320 .has_downtime_limit = true,
1321 .downtime_limit = value,
1324 qmp_migrate_set_parameters(&p, errp);
1327 bool migrate_release_ram(void)
1329 MigrationState *s;
1331 s = migrate_get_current();
1333 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
1336 bool migrate_postcopy_ram(void)
1338 MigrationState *s;
1340 s = migrate_get_current();
1342 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
1345 bool migrate_auto_converge(void)
1347 MigrationState *s;
1349 s = migrate_get_current();
1351 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
1354 bool migrate_zero_blocks(void)
1356 MigrationState *s;
1358 s = migrate_get_current();
1360 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
1363 bool migrate_use_compression(void)
1365 MigrationState *s;
1367 s = migrate_get_current();
1369 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
1372 int migrate_compress_level(void)
1374 MigrationState *s;
1376 s = migrate_get_current();
1378 return s->parameters.compress_level;
1381 int migrate_compress_threads(void)
1383 MigrationState *s;
1385 s = migrate_get_current();
1387 return s->parameters.compress_threads;
1390 int migrate_decompress_threads(void)
1392 MigrationState *s;
1394 s = migrate_get_current();
1396 return s->parameters.decompress_threads;
1399 bool migrate_use_events(void)
1401 MigrationState *s;
1403 s = migrate_get_current();
1405 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
1408 int migrate_use_xbzrle(void)
1410 MigrationState *s;
1412 s = migrate_get_current();
1414 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
1417 int64_t migrate_xbzrle_cache_size(void)
1419 MigrationState *s;
1421 s = migrate_get_current();
1423 return s->xbzrle_cache_size;
1426 bool migrate_use_block(void)
1428 MigrationState *s;
1430 s = migrate_get_current();
1432 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
1435 bool migrate_use_return_path(void)
1437 MigrationState *s;
1439 s = migrate_get_current();
1441 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
1444 bool migrate_use_block_incremental(void)
1446 MigrationState *s;
1448 s = migrate_get_current();
1450 return s->parameters.block_incremental;
1453 /* migration thread support */
1455 * Something bad happened to the RP stream, mark an error
1456 * The caller shall print or trace something to indicate why
1458 static void mark_source_rp_bad(MigrationState *s)
1460 s->rp_state.error = true;
1463 static struct rp_cmd_args {
1464 ssize_t len; /* -1 = variable */
1465 const char *name;
1466 } rp_cmd_args[] = {
1467 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
1468 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
1469 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
1470 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
1471 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
1472 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
1476 * Process a request for pages received on the return path,
1477 * We're allowed to send more than requested (e.g. to round to our page size)
1478 * and we don't need to send pages that have already been sent.
1480 static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
1481 ram_addr_t start, size_t len)
1483 long our_host_ps = getpagesize();
1485 trace_migrate_handle_rp_req_pages(rbname, start, len);
1488 * Since we currently insist on matching page sizes, just sanity check
1489 * we're being asked for whole host pages.
1491 if (start & (our_host_ps-1) ||
1492 (len & (our_host_ps-1))) {
1493 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
1494 " len: %zd", __func__, start, len);
1495 mark_source_rp_bad(ms);
1496 return;
1499 if (ram_save_queue_pages(rbname, start, len)) {
1500 mark_source_rp_bad(ms);
1505 * Handles messages sent on the return path towards the source VM
1508 static void *source_return_path_thread(void *opaque)
1510 MigrationState *ms = opaque;
1511 QEMUFile *rp = ms->rp_state.from_dst_file;
1512 uint16_t header_len, header_type;
1513 uint8_t buf[512];
1514 uint32_t tmp32, sibling_error;
1515 ram_addr_t start = 0; /* =0 to silence warning */
1516 size_t len = 0, expected_len;
1517 int res;
1519 trace_source_return_path_thread_entry();
1520 while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
1521 migration_is_setup_or_active(ms->state)) {
1522 trace_source_return_path_thread_loop_top();
1523 header_type = qemu_get_be16(rp);
1524 header_len = qemu_get_be16(rp);
1526 if (header_type >= MIG_RP_MSG_MAX ||
1527 header_type == MIG_RP_MSG_INVALID) {
1528 error_report("RP: Received invalid message 0x%04x length 0x%04x",
1529 header_type, header_len);
1530 mark_source_rp_bad(ms);
1531 goto out;
1534 if ((rp_cmd_args[header_type].len != -1 &&
1535 header_len != rp_cmd_args[header_type].len) ||
1536 header_len > sizeof(buf)) {
1537 error_report("RP: Received '%s' message (0x%04x) with"
1538 "incorrect length %d expecting %zu",
1539 rp_cmd_args[header_type].name, header_type, header_len,
1540 (size_t)rp_cmd_args[header_type].len);
1541 mark_source_rp_bad(ms);
1542 goto out;
1545 /* We know we've got a valid header by this point */
1546 res = qemu_get_buffer(rp, buf, header_len);
1547 if (res != header_len) {
1548 error_report("RP: Failed reading data for message 0x%04x"
1549 " read %d expected %d",
1550 header_type, res, header_len);
1551 mark_source_rp_bad(ms);
1552 goto out;
1555 /* OK, we have the message and the data */
1556 switch (header_type) {
1557 case MIG_RP_MSG_SHUT:
1558 sibling_error = ldl_be_p(buf);
1559 trace_source_return_path_thread_shut(sibling_error);
1560 if (sibling_error) {
1561 error_report("RP: Sibling indicated error %d", sibling_error);
1562 mark_source_rp_bad(ms);
1565 * We'll let the main thread deal with closing the RP
1566 * we could do a shutdown(2) on it, but we're the only user
1567 * anyway, so there's nothing gained.
1569 goto out;
1571 case MIG_RP_MSG_PONG:
1572 tmp32 = ldl_be_p(buf);
1573 trace_source_return_path_thread_pong(tmp32);
1574 break;
1576 case MIG_RP_MSG_REQ_PAGES:
1577 start = ldq_be_p(buf);
1578 len = ldl_be_p(buf + 8);
1579 migrate_handle_rp_req_pages(ms, NULL, start, len);
1580 break;
1582 case MIG_RP_MSG_REQ_PAGES_ID:
1583 expected_len = 12 + 1; /* header + termination */
1585 if (header_len >= expected_len) {
1586 start = ldq_be_p(buf);
1587 len = ldl_be_p(buf + 8);
1588 /* Now we expect an idstr */
1589 tmp32 = buf[12]; /* Length of the following idstr */
1590 buf[13 + tmp32] = '\0';
1591 expected_len += tmp32;
1593 if (header_len != expected_len) {
1594 error_report("RP: Req_Page_id with length %d expecting %zd",
1595 header_len, expected_len);
1596 mark_source_rp_bad(ms);
1597 goto out;
1599 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
1600 break;
1602 default:
1603 break;
1606 if (qemu_file_get_error(rp)) {
1607 trace_source_return_path_thread_bad_end();
1608 mark_source_rp_bad(ms);
1611 trace_source_return_path_thread_end();
1612 out:
1613 ms->rp_state.from_dst_file = NULL;
1614 qemu_fclose(rp);
1615 return NULL;
1618 static int open_return_path_on_source(MigrationState *ms)
1621 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
1622 if (!ms->rp_state.from_dst_file) {
1623 return -1;
1626 trace_open_return_path_on_source();
1627 qemu_thread_create(&ms->rp_state.rp_thread, "return path",
1628 source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
1630 trace_open_return_path_on_source_continue();
1632 return 0;
1635 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
1636 static int await_return_path_close_on_source(MigrationState *ms)
1639 * If this is a normal exit then the destination will send a SHUT and the
1640 * rp_thread will exit, however if there's an error we need to cause
1641 * it to exit.
1643 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
1645 * shutdown(2), if we have it, will cause it to unblock if it's stuck
1646 * waiting for the destination.
1648 qemu_file_shutdown(ms->rp_state.from_dst_file);
1649 mark_source_rp_bad(ms);
1651 trace_await_return_path_close_on_source_joining();
1652 qemu_thread_join(&ms->rp_state.rp_thread);
1653 trace_await_return_path_close_on_source_close();
1654 return ms->rp_state.error;
1658 * Switch from normal iteration to postcopy
1659 * Returns non-0 on error
1661 static int postcopy_start(MigrationState *ms, bool *old_vm_running)
1663 int ret;
1664 QIOChannelBuffer *bioc;
1665 QEMUFile *fb;
1666 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1667 bool restart_block = false;
1668 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
1669 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1671 trace_postcopy_start();
1672 qemu_mutex_lock_iothread();
1673 trace_postcopy_start_set_run();
1675 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1676 *old_vm_running = runstate_is_running();
1677 global_state_store();
1678 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1679 if (ret < 0) {
1680 goto fail;
1683 ret = bdrv_inactivate_all();
1684 if (ret < 0) {
1685 goto fail;
1687 restart_block = true;
1690 * Cause any non-postcopiable, but iterative devices to
1691 * send out their final data.
1693 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
1696 * in Finish migrate and with the io-lock held everything should
1697 * be quiet, but we've potentially still got dirty pages and we
1698 * need to tell the destination to throw any pages it's already received
1699 * that are dirty
1701 if (ram_postcopy_send_discard_bitmap(ms)) {
1702 error_report("postcopy send discard bitmap failed");
1703 goto fail;
1707 * send rest of state - note things that are doing postcopy
1708 * will notice we're in POSTCOPY_ACTIVE and not actually
1709 * wrap their state up here
1711 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
1712 /* Ping just for debugging, helps line traces up */
1713 qemu_savevm_send_ping(ms->to_dst_file, 2);
1716 * While loading the device state we may trigger page transfer
1717 * requests and the fd must be free to process those, and thus
1718 * the destination must read the whole device state off the fd before
1719 * it starts processing it. Unfortunately the ad-hoc migration format
1720 * doesn't allow the destination to know the size to read without fully
1721 * parsing it through each devices load-state code (especially the open
1722 * coded devices that use get/put).
1723 * So we wrap the device state up in a package with a length at the start;
1724 * to do this we use a qemu_buf to hold the whole of the device state.
1726 bioc = qio_channel_buffer_new(4096);
1727 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
1728 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
1729 object_unref(OBJECT(bioc));
1732 * Make sure the receiver can get incoming pages before we send the rest
1733 * of the state
1735 qemu_savevm_send_postcopy_listen(fb);
1737 qemu_savevm_state_complete_precopy(fb, false, false);
1738 qemu_savevm_send_ping(fb, 3);
1740 qemu_savevm_send_postcopy_run(fb);
1742 /* <><> end of stuff going into the package */
1744 /* Last point of recovery; as soon as we send the package the destination
1745 * can open devices and potentially start running.
1746 * Lets just check again we've not got any errors.
1748 ret = qemu_file_get_error(ms->to_dst_file);
1749 if (ret) {
1750 error_report("postcopy_start: Migration stream errored (pre package)");
1751 goto fail_closefb;
1754 restart_block = false;
1756 /* Now send that blob */
1757 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
1758 goto fail_closefb;
1760 qemu_fclose(fb);
1762 /* Send a notify to give a chance for anything that needs to happen
1763 * at the transition to postcopy and after the device state; in particular
1764 * spice needs to trigger a transition now
1766 ms->postcopy_after_devices = true;
1767 notifier_list_notify(&migration_state_notifiers, ms);
1769 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
1771 qemu_mutex_unlock_iothread();
1774 * Although this ping is just for debug, it could potentially be
1775 * used for getting a better measurement of downtime at the source.
1777 qemu_savevm_send_ping(ms->to_dst_file, 4);
1779 if (migrate_release_ram()) {
1780 ram_postcopy_migrated_memory_release(ms);
1783 ret = qemu_file_get_error(ms->to_dst_file);
1784 if (ret) {
1785 error_report("postcopy_start: Migration stream errored");
1786 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1787 MIGRATION_STATUS_FAILED);
1790 return ret;
1792 fail_closefb:
1793 qemu_fclose(fb);
1794 fail:
1795 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1796 MIGRATION_STATUS_FAILED);
1797 if (restart_block) {
1798 /* A failure happened early enough that we know the destination hasn't
1799 * accessed block devices, so we're safe to recover.
1801 Error *local_err = NULL;
1803 bdrv_invalidate_cache_all(&local_err);
1804 if (local_err) {
1805 error_report_err(local_err);
1808 qemu_mutex_unlock_iothread();
1809 return -1;
1813 * migration_completion: Used by migration_thread when there's not much left.
1814 * The caller 'breaks' the loop when this returns.
1816 * @s: Current migration state
1817 * @current_active_state: The migration state we expect to be in
1818 * @*old_vm_running: Pointer to old_vm_running flag
1819 * @*start_time: Pointer to time to update
1821 static void migration_completion(MigrationState *s, int current_active_state,
1822 bool *old_vm_running,
1823 int64_t *start_time)
1825 int ret;
1827 if (s->state == MIGRATION_STATUS_ACTIVE) {
1828 qemu_mutex_lock_iothread();
1829 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1830 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
1831 *old_vm_running = runstate_is_running();
1832 ret = global_state_store();
1834 if (!ret) {
1835 bool inactivate = !migrate_colo_enabled();
1836 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
1837 if (ret >= 0) {
1838 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
1839 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
1840 inactivate);
1842 if (inactivate && ret >= 0) {
1843 s->block_inactive = true;
1846 qemu_mutex_unlock_iothread();
1848 if (ret < 0) {
1849 goto fail;
1851 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1852 trace_migration_completion_postcopy_end();
1854 qemu_savevm_state_complete_postcopy(s->to_dst_file);
1855 trace_migration_completion_postcopy_end_after_complete();
1859 * If rp was opened we must clean up the thread before
1860 * cleaning everything else up (since if there are no failures
1861 * it will wait for the destination to send it's status in
1862 * a SHUT command).
1864 if (s->rp_state.from_dst_file) {
1865 int rp_error;
1866 trace_migration_return_path_end_before();
1867 rp_error = await_return_path_close_on_source(s);
1868 trace_migration_return_path_end_after(rp_error);
1869 if (rp_error) {
1870 goto fail_invalidate;
1874 if (qemu_file_get_error(s->to_dst_file)) {
1875 trace_migration_completion_file_err();
1876 goto fail_invalidate;
1879 if (!migrate_colo_enabled()) {
1880 migrate_set_state(&s->state, current_active_state,
1881 MIGRATION_STATUS_COMPLETED);
1884 return;
1886 fail_invalidate:
1887 /* If not doing postcopy, vm_start() will be called: let's regain
1888 * control on images.
1890 if (s->state == MIGRATION_STATUS_ACTIVE) {
1891 Error *local_err = NULL;
1893 qemu_mutex_lock_iothread();
1894 bdrv_invalidate_cache_all(&local_err);
1895 if (local_err) {
1896 error_report_err(local_err);
1897 } else {
1898 s->block_inactive = false;
1900 qemu_mutex_unlock_iothread();
1903 fail:
1904 migrate_set_state(&s->state, current_active_state,
1905 MIGRATION_STATUS_FAILED);
1908 bool migrate_colo_enabled(void)
1910 MigrationState *s = migrate_get_current();
1911 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
1915 * Master migration thread on the source VM.
1916 * It drives the migration and pumps the data down the outgoing channel.
1918 static void *migration_thread(void *opaque)
1920 MigrationState *s = opaque;
1921 /* Used by the bandwidth calcs, updated later */
1922 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1923 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
1924 int64_t initial_bytes = 0;
1926 * The final stage happens when the remaining data is smaller than
1927 * this threshold; it's calculated from the requested downtime and
1928 * measured bandwidth
1930 int64_t threshold_size = 0;
1931 int64_t start_time = initial_time;
1932 int64_t end_time;
1933 bool old_vm_running = false;
1934 bool entered_postcopy = false;
1935 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */
1936 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE;
1937 bool enable_colo = migrate_colo_enabled();
1939 rcu_register_thread();
1941 qemu_savevm_state_header(s->to_dst_file);
1944 * If we opened the return path, we need to make sure dst has it
1945 * opened as well.
1947 if (s->rp_state.from_dst_file) {
1948 /* Now tell the dest that it should open its end so it can reply */
1949 qemu_savevm_send_open_return_path(s->to_dst_file);
1951 /* And do a ping that will make stuff easier to debug */
1952 qemu_savevm_send_ping(s->to_dst_file, 1);
1955 if (migrate_postcopy_ram()) {
1957 * Tell the destination that we *might* want to do postcopy later;
1958 * if the other end can't do postcopy it should fail now, nice and
1959 * early.
1961 qemu_savevm_send_postcopy_advise(s->to_dst_file);
1964 qemu_savevm_state_setup(s->to_dst_file);
1966 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
1967 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
1968 MIGRATION_STATUS_ACTIVE);
1970 trace_migration_thread_setup_complete();
1972 while (s->state == MIGRATION_STATUS_ACTIVE ||
1973 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
1974 int64_t current_time;
1975 uint64_t pending_size;
1977 if (!qemu_file_rate_limit(s->to_dst_file)) {
1978 uint64_t pend_post, pend_nonpost;
1980 qemu_savevm_state_pending(s->to_dst_file, threshold_size,
1981 &pend_nonpost, &pend_post);
1982 pending_size = pend_nonpost + pend_post;
1983 trace_migrate_pending(pending_size, threshold_size,
1984 pend_post, pend_nonpost);
1985 if (pending_size && pending_size >= threshold_size) {
1986 /* Still a significant amount to transfer */
1988 if (migrate_postcopy_ram() &&
1989 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
1990 pend_nonpost <= threshold_size &&
1991 atomic_read(&s->start_postcopy)) {
1993 if (!postcopy_start(s, &old_vm_running)) {
1994 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE;
1995 entered_postcopy = true;
1998 continue;
2000 /* Just another iteration step */
2001 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy);
2002 } else {
2003 trace_migration_thread_low_pending(pending_size);
2004 migration_completion(s, current_active_state,
2005 &old_vm_running, &start_time);
2006 break;
2010 if (qemu_file_get_error(s->to_dst_file)) {
2011 migrate_set_state(&s->state, current_active_state,
2012 MIGRATION_STATUS_FAILED);
2013 trace_migration_thread_file_err();
2014 break;
2016 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2017 if (current_time >= initial_time + BUFFER_DELAY) {
2018 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) -
2019 initial_bytes;
2020 uint64_t time_spent = current_time - initial_time;
2021 double bandwidth = (double)transferred_bytes / time_spent;
2022 threshold_size = bandwidth * s->parameters.downtime_limit;
2024 s->mbps = (((double) transferred_bytes * 8.0) /
2025 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
2027 trace_migrate_transferred(transferred_bytes, time_spent,
2028 bandwidth, threshold_size);
2029 /* if we haven't sent anything, we don't want to recalculate
2030 10000 is a small enough number for our purposes */
2031 if (ram_counters.dirty_pages_rate && transferred_bytes > 10000) {
2032 s->expected_downtime = ram_counters.dirty_pages_rate *
2033 qemu_target_page_size() / bandwidth;
2036 qemu_file_reset_rate_limit(s->to_dst_file);
2037 initial_time = current_time;
2038 initial_bytes = qemu_ftell(s->to_dst_file);
2040 if (qemu_file_rate_limit(s->to_dst_file)) {
2041 /* usleep expects microseconds */
2042 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
2046 trace_migration_thread_after_loop();
2047 /* If we enabled cpu throttling for auto-converge, turn it off. */
2048 cpu_throttle_stop();
2049 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
2051 qemu_mutex_lock_iothread();
2053 * The resource has been allocated by migration will be reused in COLO
2054 * process, so don't release them.
2056 if (!enable_colo) {
2057 qemu_savevm_state_cleanup();
2059 if (s->state == MIGRATION_STATUS_COMPLETED) {
2060 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file);
2061 s->total_time = end_time - s->total_time;
2062 if (!entered_postcopy) {
2063 s->downtime = end_time - start_time;
2065 if (s->total_time) {
2066 s->mbps = (((double) transferred_bytes * 8.0) /
2067 ((double) s->total_time)) / 1000;
2069 runstate_set(RUN_STATE_POSTMIGRATE);
2070 } else {
2071 if (s->state == MIGRATION_STATUS_ACTIVE && enable_colo) {
2072 migrate_start_colo_process(s);
2073 qemu_savevm_state_cleanup();
2075 * Fixme: we will run VM in COLO no matter its old running state.
2076 * After exited COLO, we will keep running.
2078 old_vm_running = true;
2080 if (old_vm_running && !entered_postcopy) {
2081 vm_start();
2082 } else {
2083 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
2084 runstate_set(RUN_STATE_POSTMIGRATE);
2088 qemu_bh_schedule(s->cleanup_bh);
2089 qemu_mutex_unlock_iothread();
2091 rcu_unregister_thread();
2092 return NULL;
2095 void migrate_fd_connect(MigrationState *s)
2097 s->expected_downtime = s->parameters.downtime_limit;
2098 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
2100 qemu_file_set_blocking(s->to_dst_file, true);
2101 qemu_file_set_rate_limit(s->to_dst_file,
2102 s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
2104 /* Notify before starting migration thread */
2105 notifier_list_notify(&migration_state_notifiers, s);
2108 * Open the return path. For postcopy, it is used exclusively. For
2109 * precopy, only if user specified "return-path" capability would
2110 * QEMU uses the return path.
2112 if (migrate_postcopy_ram() || migrate_use_return_path()) {
2113 if (open_return_path_on_source(s)) {
2114 error_report("Unable to open return-path for postcopy");
2115 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2116 MIGRATION_STATUS_FAILED);
2117 migrate_fd_cleanup(s);
2118 return;
2122 qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
2123 QEMU_THREAD_JOINABLE);
2124 s->migration_thread_running = true;
2127 void migration_global_dump(Monitor *mon)
2129 MigrationState *ms = migrate_get_current();
2131 monitor_printf(mon, "globals: store-global-state=%d, only_migratable=%d, "
2132 "send-configuration=%d, send-section-footer=%d\n",
2133 ms->store_global_state, ms->only_migratable,
2134 ms->send_configuration, ms->send_section_footer);
2137 #define DEFINE_PROP_MIG_CAP(name, x) \
2138 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
2140 static Property migration_properties[] = {
2141 DEFINE_PROP_BOOL("store-global-state", MigrationState,
2142 store_global_state, true),
2143 DEFINE_PROP_BOOL("only-migratable", MigrationState, only_migratable, false),
2144 DEFINE_PROP_BOOL("send-configuration", MigrationState,
2145 send_configuration, true),
2146 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
2147 send_section_footer, true),
2149 /* Migration parameters */
2150 DEFINE_PROP_INT64("x-compress-level", MigrationState,
2151 parameters.compress_level,
2152 DEFAULT_MIGRATE_COMPRESS_LEVEL),
2153 DEFINE_PROP_INT64("x-compress-threads", MigrationState,
2154 parameters.compress_threads,
2155 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
2156 DEFINE_PROP_INT64("x-decompress-threads", MigrationState,
2157 parameters.decompress_threads,
2158 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
2159 DEFINE_PROP_INT64("x-cpu-throttle-initial", MigrationState,
2160 parameters.cpu_throttle_initial,
2161 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
2162 DEFINE_PROP_INT64("x-cpu-throttle-increment", MigrationState,
2163 parameters.cpu_throttle_increment,
2164 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
2165 DEFINE_PROP_INT64("x-max-bandwidth", MigrationState,
2166 parameters.max_bandwidth, MAX_THROTTLE),
2167 DEFINE_PROP_INT64("x-downtime-limit", MigrationState,
2168 parameters.downtime_limit,
2169 DEFAULT_MIGRATE_SET_DOWNTIME),
2170 DEFINE_PROP_INT64("x-checkpoint-delay", MigrationState,
2171 parameters.x_checkpoint_delay,
2172 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
2174 /* Migration capabilities */
2175 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
2176 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
2177 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
2178 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
2179 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
2180 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
2181 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
2182 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
2183 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
2184 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
2185 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
2187 DEFINE_PROP_END_OF_LIST(),
2190 static void migration_class_init(ObjectClass *klass, void *data)
2192 DeviceClass *dc = DEVICE_CLASS(klass);
2194 dc->user_creatable = false;
2195 dc->props = migration_properties;
2198 static void migration_instance_init(Object *obj)
2200 MigrationState *ms = MIGRATION_OBJ(obj);
2201 MigrationParameters *params = &ms->parameters;
2203 ms->state = MIGRATION_STATUS_NONE;
2204 ms->xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE;
2205 ms->mbps = -1;
2207 params->tls_hostname = g_strdup("");
2208 params->tls_creds = g_strdup("");
2210 /* Set has_* up only for parameter checks */
2211 params->has_compress_level = true;
2212 params->has_compress_threads = true;
2213 params->has_decompress_threads = true;
2214 params->has_cpu_throttle_initial = true;
2215 params->has_cpu_throttle_increment = true;
2216 params->has_max_bandwidth = true;
2217 params->has_downtime_limit = true;
2218 params->has_x_checkpoint_delay = true;
2219 params->has_block_incremental = true;
2223 * Return true if check pass, false otherwise. Error will be put
2224 * inside errp if provided.
2226 static bool migration_object_check(MigrationState *ms, Error **errp)
2228 MigrationCapabilityStatusList *head = NULL;
2229 /* Assuming all off */
2230 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
2231 int i;
2233 if (!migrate_params_check(&ms->parameters, errp)) {
2234 return false;
2237 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
2238 if (ms->enabled_capabilities[i]) {
2239 head = migrate_cap_add(head, i, true);
2243 ret = migrate_caps_check(cap_list, head, errp);
2245 /* It works with head == NULL */
2246 qapi_free_MigrationCapabilityStatusList(head);
2248 return ret;
2251 static const TypeInfo migration_type = {
2252 .name = TYPE_MIGRATION,
2254 * NOTE: TYPE_MIGRATION is not really a device, as the object is
2255 * not created using qdev_create(), it is not attached to the qdev
2256 * device tree, and it is never realized.
2258 * TODO: Make this TYPE_OBJECT once QOM provides something like
2259 * TYPE_DEVICE's "-global" properties.
2261 .parent = TYPE_DEVICE,
2262 .class_init = migration_class_init,
2263 .class_size = sizeof(MigrationClass),
2264 .instance_size = sizeof(MigrationState),
2265 .instance_init = migration_instance_init,
2268 static void register_migration_types(void)
2270 type_register_static(&migration_type);
2273 type_init(register_migration_types);