Lines Matching +full:- +full:- +full:disable +full:- +full:rdma

10  * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
19 #include "qemu/error-report.h"
20 #include "qemu/main-loop.h"
28 #include "system/cpu-throttle.h"
29 #include "rdma.h"
35 #include "migration-stats.h"
37 #include "qemu-file.h"
42 #include "qapi/clone-visitor.h"
43 #include "qapi/qapi-visit-migration.h"
44 #include "qapi/qapi-visit-sockets.h"
45 #include "qapi/qapi-commands-migration.h"
46 #include "qapi/qapi-events-migration.h"
50 #include "postcopy-ram.h"
54 #include "io/channel-buffer.h"
55 #include "io/channel-tls.h"
118 trace_vmstate_downtime_checkpoint("src-downtime-start");
119 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
129 * an use-after-free itself..
151 if (!s->downtime) {
152 s->downtime = now - s->downtime_start;
153 trace_vmstate_downtime_checkpoint("src-downtime-end");
190 if (addr->transport == MIGRATION_ADDRESS_TYPE_SOCKET) {
191 SocketAddress *saddr = &addr->u.socket;
193 return (saddr->type == SOCKET_ADDRESS_TYPE_INET ||
194 saddr->type == SOCKET_ADDRESS_TYPE_UNIX ||
195 saddr->type == SOCKET_ADDRESS_TYPE_VSOCK);
196 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_FILE) {
211 * When doing direct-io, multifd requires two different,
212 * non-duplicated file descriptors so we can use one of them for
220 if (addr->transport == MIGRATION_ADDRESS_TYPE_FILE) {
230 return addr->transport == MIGRATION_ADDRESS_TYPE_FILE;
245 error_setg(errp, "Migration requires multi-channel URIs (e.g. tcp)");
257 addr->transport == MIGRATION_ADDRESS_TYPE_FILE) {
269 if (addr->transport == MIGRATION_ADDRESS_TYPE_RDMA) {
270 return migrate_rdma_caps_check(migrate_get_current()->capabilities,
287 return (a > b) - (a < b);
296 s->vm_old_state = runstate_get();
301 trace_vmstate_downtime_checkpoint("src-vm-stopped");
319 current_incoming->state = MIGRATION_STATUS_NONE;
320 current_incoming->postcopy_remote_fds =
322 qemu_mutex_init(&current_incoming->rp_mutex);
323 qemu_mutex_init(&current_incoming->postcopy_prio_thread_mutex);
324 qemu_event_init(&current_incoming->main_thread_load_event, false);
325 qemu_sem_init(&current_incoming->postcopy_pause_sem_dst, 0);
326 qemu_sem_init(&current_incoming->postcopy_pause_sem_fault, 0);
327 qemu_sem_init(&current_incoming->postcopy_pause_sem_fast_load, 0);
328 qemu_sem_init(&current_incoming->postcopy_qemufile_dst_done, 0);
330 qemu_mutex_init(&current_incoming->page_request_mutex);
331 qemu_cond_init(&current_incoming->page_request_cond);
332 current_incoming->page_requested = g_tree_new(page_request_addr_cmp);
334 current_incoming->exit_on_error = INMIGRATE_DEFAULT_EXIT_ON_ERROR;
357 qemu_bh_delete(migbh->bh);
358 migbh->bh = NULL;
361 migbh->cb(migbh->opaque);
374 migbh->bh = bh;
375 migbh->cb = cb;
376 migbh->opaque = opaque;
395 * Cancel the current migration - that will (eventually)
409 * are non-critical data, and their loss never considered as
431 if (mis->socket_address_list) {
432 qapi_free_SocketAddressList(mis->socket_address_list);
433 mis->socket_address_list = NULL;
436 if (mis->transport_cleanup) {
437 mis->transport_cleanup(mis->transport_data);
438 mis->transport_data = mis->transport_cleanup = NULL;
451 * The VFIO load_cleanup() implementation is BQL-sensitive. It requires
466 if (mis->to_src_file) {
468 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
469 qemu_fclose(mis->to_src_file);
470 mis->to_src_file = NULL;
473 if (mis->from_src_file) {
474 migration_ioc_unregister_yank_from_file(mis->from_src_file);
475 qemu_fclose(mis->from_src_file);
476 mis->from_src_file = NULL;
478 if (mis->postcopy_remote_fds) {
479 g_array_free(mis->postcopy_remote_fds, TRUE);
480 mis->postcopy_remote_fds = NULL;
484 qemu_event_reset(&mis->main_thread_load_event);
486 if (mis->page_requested) {
487 g_tree_destroy(mis->page_requested);
488 mis->page_requested = NULL;
491 if (mis->postcopy_qemufile_dst) {
492 migration_ioc_unregister_yank_from_file(mis->postcopy_qemufile_dst);
493 qemu_fclose(mis->postcopy_qemufile_dst);
494 mis->postcopy_qemufile_dst = NULL;
519 QEMU_LOCK_GUARD(&mis->rp_mutex);
525 if (!mis->to_src_file) {
526 ret = -EIO;
530 qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
531 qemu_put_be16(mis->to_src_file, len);
532 qemu_put_buffer(mis->to_src_file, data, len);
533 return qemu_fflush(mis->to_src_file);
539 * Len: Length in bytes required - must be a multiple of pagesize
559 if (rb != mis->last_rb) {
560 mis->last_rb = rb;
585 WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
588 if (!g_tree_lookup(mis->page_requested, aligned)) {
595 g_tree_insert(mis->page_requested, aligned, (gpointer)1);
596 qatomic_inc(&mis->page_requested_count);
597 trace_postcopy_page_req_add(aligned, mis->page_requested_count);
631 return -ENOTSUP;
635 error_report("ENABLE_COLO command come in migration stream, but x-colo "
637 return -EINVAL;
641 error_report("COLO: cannot disable RAM discard");
642 return -EBUSY;
652 QAPI_LIST_PREPEND(mis->socket_address_list,
671 InetSocketAddress *isock = &addr->u.rdma;
672 strList **tail = &addr->u.exec.args;
675 addr->transport = MIGRATION_ADDRESS_TYPE_EXEC;
681 QAPI_LIST_APPEND(tail, g_strdup("-c"));
684 } else if (strstart(uri, "rdma:", NULL)) {
685 if (inet_parse(isock, uri + strlen("rdma:"), errp)) {
689 addr->transport = MIGRATION_ADDRESS_TYPE_RDMA;
694 addr->transport = MIGRATION_ADDRESS_TYPE_SOCKET;
699 addr->u.socket.type = saddr->type;
700 addr->u.socket.u = saddr->u;
704 addr->transport = MIGRATION_ADDRESS_TYPE_FILE;
705 addr->u.file.filename = g_strdup(uri + strlen("file:"));
706 if (file_parse_offset(addr->u.file.filename, &addr->u.file.offset,
715 val->channel_type = MIGRATION_CHANNEL_TYPE_MAIN;
716 val->addr = g_steal_pointer(&addr);
724 MigrationStatus current = mis->state;
740 migrate_set_state(&mis->state, current, MIGRATION_STATUS_SETUP);
762 if (channels->next) {
767 addr = channels->value->addr;
775 addr = channel->addr;
787 if (addr->transport == MIGRATION_ADDRESS_TYPE_SOCKET) {
788 SocketAddress *saddr = &addr->u.socket;
789 if (saddr->type == SOCKET_ADDRESS_TYPE_INET ||
790 saddr->type == SOCKET_ADDRESS_TYPE_UNIX ||
791 saddr->type == SOCKET_ADDRESS_TYPE_VSOCK) {
793 } else if (saddr->type == SOCKET_ADDRESS_TYPE_FD) {
794 fd_start_incoming_migration(saddr->u.fd.str, errp);
797 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_RDMA) {
798 rdma_start_incoming_migration(&addr->u.rdma, errp);
800 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_EXEC) {
801 exec_start_incoming_migration(addr->u.exec.args, errp);
802 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_FILE) {
803 file_start_incoming_migration(&addr->u.file, errp);
816 trace_vmstate_downtime_checkpoint("dst-precopy-bh-enter");
822 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
824 trace_vmstate_downtime_checkpoint("dst-precopy-bh-announced");
837 * We used to have cap 'late-block-activate' but now we do this
856 trace_vmstate_downtime_checkpoint("dst-precopy-bh-vm-started");
862 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
876 assert(mis->from_src_file);
878 mis->largest_page_size = qemu_ram_pagesize_largest();
880 migrate_set_state(&mis->state, MIGRATION_STATUS_SETUP,
883 mis->loadvm_co = qemu_coroutine_self();
884 ret = qemu_loadvm_state(mis->from_src_file);
885 mis->loadvm_co = NULL;
887 trace_vmstate_downtime_checkpoint("dst-precopy-loadvm-completed");
911 error_setg(&local_err, "load of migration failed: %s", strerror(-ret));
924 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
931 if (mis->exit_on_error) {
932 WITH_QEMU_LOCK_GUARD(&s->error_mutex) {
933 error_report_err(s->error);
934 s->error = NULL;
952 assert(!mis->from_src_file);
953 mis->from_src_file = f;
968 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
972 assert(mis->from_src_file);
974 qemu_file_set_blocking(mis->from_src_file, true);
976 /* Re-configure the return path */
977 mis->to_src_file = qemu_file_get_return_path(mis->from_src_file);
979 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
989 qemu_sem_post(&mis->postcopy_pause_sem_dst);
1008 if (!mis->from_src_file) {
1054 } else if (!mis->from_src_file &&
1055 mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
1062 } else if (mis->from_src_file && migrate_multifd()) {
1064 * Non-peekable channels like tls/file are processed as
1068 } else if (!mis->from_src_file) {
1071 error_setg(errp, "non-peekable channel used without multifd");
1094 assert(!mis->postcopy_qemufile_dst);
1122 if (migrate_postcopy_preempt() && !mis->postcopy_qemufile_dst) {
1136 * to indicate that we've finished with the RP. Non-0 value indicates
1176 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
1193 qemu_mutex_lock(&mis->rp_mutex);
1194 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
1195 qemu_mutex_unlock(&mis->rp_mutex);
1216 switch (s->state) {
1238 return (s->state == MIGRATION_STATUS_ACTIVE ||
1239 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1244 return (s->state == MIGRATION_STATUS_COMPLETED) || migration_in_postcopy();
1249 info->has_status = true;
1250 info->has_setup_time = true;
1251 info->setup_time = s->setup_time;
1253 if (s->state == MIGRATION_STATUS_COMPLETED) {
1254 info->has_total_time = true;
1255 info->total_time = s->total_time;
1257 info->has_total_time = true;
1258 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
1259 s->start_time;
1263 info->has_downtime = true;
1264 info->downtime = s->downtime;
1266 info->has_expected_downtime = true;
1267 info->expected_downtime = s->expected_downtime;
1275 info->ram = g_malloc0(sizeof(*info->ram));
1276 info->ram->transferred = migration_transferred_bytes();
1277 info->ram->total = ram_bytes_total();
1278 info->ram->duplicate = stat64_get(&mig_stats.zero_pages);
1279 info->ram->normal = stat64_get(&mig_stats.normal_pages);
1280 info->ram->normal_bytes = info->ram->normal * page_size;
1281 info->ram->mbps = s->mbps;
1282 info->ram->dirty_sync_count =
1284 info->ram->dirty_sync_missed_zero_copy =
1286 info->ram->postcopy_requests =
1288 info->ram->page_size = page_size;
1289 info->ram->multifd_bytes = stat64_get(&mig_stats.multifd_bytes);
1290 info->ram->pages_per_second = s->pages_per_second;
1291 info->ram->precopy_bytes = stat64_get(&mig_stats.precopy_bytes);
1292 info->ram->downtime_bytes = stat64_get(&mig_stats.downtime_bytes);
1293 info->ram->postcopy_bytes = stat64_get(&mig_stats.postcopy_bytes);
1296 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
1297 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
1298 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
1299 info->xbzrle_cache->pages = xbzrle_counters.pages;
1300 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
1301 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
1302 info->xbzrle_cache->encoding_rate = xbzrle_counters.encoding_rate;
1303 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
1307 info->has_cpu_throttle_percentage = true;
1308 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
1311 if (s->state != MIGRATION_STATUS_COMPLETED) {
1312 info->ram->remaining = ram_bytes_remaining();
1313 info->ram->dirty_pages_rate =
1318 info->has_dirty_limit_throttle_time_per_round = true;
1319 info->dirty_limit_throttle_time_per_round =
1322 info->has_dirty_limit_ring_full_time = true;
1323 info->dirty_limit_ring_full_time = dirtylimit_ring_full_time();
1330 int state = qatomic_read(&s->state);
1333 info->blocked_reasons = NULL;
1337 * a) devices marked in VMState as non-migratable, and
1341 qemu_savevm_non_migratable_list(&info->blocked_reasons);
1344 QAPI_LIST_PREPEND(info->blocked_reasons,
1345 g_strdup(error_get_pretty(cur_blocker->data)));
1348 info->has_blocked_reasons = info->blocked_reasons != NULL;
1356 info->has_status = true;
1357 info->has_total_time = false;
1373 info->has_status = true;
1382 info->has_status = true;
1385 info->has_status = true;
1388 info->has_status = true;
1391 info->status = state;
1393 QEMU_LOCK_GUARD(&s->error_mutex);
1394 if (s->error) {
1395 info->error_desc = g_strdup(error_get_pretty(s->error));
1403 if (mis->socket_address_list) {
1404 info->has_socket_address = true;
1405 info->socket_address =
1406 QAPI_CLONE(SocketAddressList, mis->socket_address_list);
1409 switch (mis->state) {
1419 info->has_status = true;
1422 info->has_status = true;
1428 info->status = mis->state;
1430 if (!info->error_desc) {
1432 QEMU_LOCK_GUARD(&s->error_mutex);
1434 if (s->error) {
1435 info->error_desc = g_strdup(error_get_pretty(s->error));
1460 if (s->state == MIGRATION_STATUS_NONE) {
1469 qatomic_set(&s->start_postcopy, true);
1486 g_clear_pointer(&s->vmdesc, json_writer_free);
1498 g_free(s->hostname);
1499 s->hostname = NULL;
1507 if (s->migration_thread_running) {
1509 qemu_thread_join(&s->thread);
1510 s->migration_thread_running = false;
1514 WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) {
1519 tmp = s->to_dst_file;
1520 s->to_dst_file = NULL;
1536 if (s->state == MIGRATION_STATUS_CANCELLING) {
1537 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
1541 if (s->error) {
1543 error_report_err(error_copy(s->error));
1558 QEMU_LOCK_GUARD(&s->error_mutex);
1562 if (!s->error) {
1563 s->error = error_copy(error);
1570 QEMU_LOCK_GUARD(&s->error_mutex);
1571 return qatomic_read(&s->error);
1576 QEMU_LOCK_GUARD(&s->error_mutex);
1577 if (s->error) {
1578 error_free(s->error);
1579 s->error = NULL;
1585 MigrationStatus current = s->state;
1588 assert(s->to_dst_file == NULL);
1608 migrate_set_state(&s->state, current, next);
1616 bool setup = (s->state == MIGRATION_STATUS_SETUP);
1621 qmp_cancel_vcpu_dirty_limit(false, -1, NULL);
1624 WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) {
1625 if (s->rp_state.from_dst_file) {
1627 qemu_file_shutdown(s->rp_state.from_dst_file);
1632 old_state = s->state;
1638 qemu_event_set(&s->pause_event);
1640 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
1641 } while (s->state != MIGRATION_STATUS_CANCELLING);
1648 if (s->state == MIGRATION_STATUS_CANCELLING) {
1649 WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) {
1650 if (s->to_dst_file) {
1651 qemu_file_shutdown(s->to_dst_file);
1660 if (setup && !s->to_dst_file) {
1661 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
1671 notify->notify = (NotifierWithReturnFunc)func;
1683 if (notify->notify) {
1685 notify->notify = NULL;
1692 MigMode mode = s->parameters.mode;
1705 return (s->state == MIGRATION_STATUS_CANCELLED ||
1706 s->state == MIGRATION_STATUS_FAILED);
1713 switch (s->state) {
1758 return qemu_thread_is_self(&s->thread);
1763 MigMode mode = s->parameters.mode;
1782 s->to_dst_file = NULL;
1783 s->state = MIGRATION_STATUS_NONE;
1784 s->rp_state.from_dst_file = NULL;
1785 s->mbps = 0.0;
1786 s->pages_per_second = 0.0;
1787 s->downtime = 0;
1788 s->expected_downtime = 0;
1789 s->setup_time = 0;
1790 s->start_postcopy = false;
1791 s->migration_thread_running = false;
1792 error_free(s->error);
1793 s->error = NULL;
1796 s->vmdesc = json_writer_new(false);
1799 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
1801 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1802 s->total_time = 0;
1803 s->vm_old_state = -1;
1804 s->iteration_initial_bytes = 0;
1805 s->threshold_size = 0;
1806 s->switchover_acked = false;
1807 s->rdma_migration = false;
1839 "(--only-migratable) for: ");
1850 while (mode != -1 && mode != MIG_MODE_ALL) {
1856 modes = BIT(MIG_MODE__MAX) - 1;
1879 return migrate_add_blocker_modes(reasonp, errp, MIG_MODE_NORMAL, -1);
1892 return -EACCES;
1894 return -EBUSY;
1901 int modes = BIT(MIG_MODE__MAX) - 1;
1904 return -EBUSY;
1935 error_setg(errp, "'-incoming' was not specified on the command line");
1943 mis->exit_on_error =
1979 if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1990 * only re-setup the migration stream and poke existing migration
2002 if (migration_postcopy_is_alive(ms->state)) {
2011 qemu_mutex_lock(&ms->qemu_file_lock);
2012 if (ms->to_dst_file) {
2013 ret = qemu_file_shutdown(ms->to_dst_file);
2015 qemu_mutex_unlock(&ms->qemu_file_lock);
2029 if (migration_postcopy_is_alive(mis->state)) {
2030 ret = qemu_file_shutdown(mis->from_src_file);
2037 error_setg(errp, "migrate-pause is currently only supported "
2038 "during postcopy-active or postcopy-recover state");
2050 error_propagate(errp, error_copy(blockers->data));
2061 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
2068 * Postcopy recovery won't work well with release-ram
2069 * capability since release-ram will drop the page buffer as
2075 * Luckily release-ram was designed to only be used when src
2081 "when release-ram capability is set");
2085 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
2120 error_setg(errp, "Cannot use TLS with mapped-ram");
2125 error_setg(errp, "Cannot use compression with mapped-ram");
2160 s->hup_source = qio_channel_create_watch(ioc, G_IO_HUP);
2161 g_source_set_callback(s->hup_source, cb, opaque, NULL);
2162 g_source_attach(s->hup_source, NULL);
2167 if (s->hup_source) {
2168 g_source_destroy(s->hup_source);
2169 g_source_unref(s->hup_source);
2170 s->hup_source = NULL;
2209 for ( ; channels; channels = channels->next) {
2210 MigrationChannelType type = channels->value->channel_type;
2217 channelv[type] = channels->value;
2220 addr = channelv[MIGRATION_CHANNEL_TYPE_MAIN]->addr;
2232 addr = channel->addr;
2240 if (s->parameters.mode == MIG_MODE_CPR_TRANSFER && !cpr_channel) {
2256 * For cpr-transfer, the target may not be listening yet on the migration
2258 * us it is listening by closing the cpr-state socket. Wait for that HUP
2265 if (s->parameters.mode == MIG_MODE_CPR_TRANSFER) {
2292 if (addr->transport == MIGRATION_ADDRESS_TYPE_SOCKET) {
2293 SocketAddress *saddr = &addr->u.socket;
2294 if (saddr->type == SOCKET_ADDRESS_TYPE_INET ||
2295 saddr->type == SOCKET_ADDRESS_TYPE_UNIX ||
2296 saddr->type == SOCKET_ADDRESS_TYPE_VSOCK) {
2298 } else if (saddr->type == SOCKET_ADDRESS_TYPE_FD) {
2299 fd_start_outgoing_migration(s, saddr->u.fd.str, &local_err);
2302 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_RDMA) {
2303 rdma_start_outgoing_migration(s, &addr->u.rdma, &local_err);
2305 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_EXEC) {
2306 exec_start_outgoing_migration(s, addr->u.exec.args, &local_err);
2307 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_FILE) {
2308 file_start_outgoing_migration(s, &addr->u.file, &local_err);
2312 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2345 if (s->state != state) {
2347 MigrationStatus_str(s->state));
2350 qemu_event_set(&s->pause_event);
2357 return -1;
2360 qemu_sem_wait(&s->rp_state.rp_sem);
2364 return -1;
2372 qemu_sem_post(&s->rp_state.rp_sem);
2376 ssize_t len; /* -1 = variable */
2379 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
2383 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
2384 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
2387 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
2443 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2453 * Release ms->rp_state.from_dst_file (and postcopy_qemufile_src if
2460 WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) {
2465 file = ms->rp_state.from_dst_file;
2466 ms->rp_state.from_dst_file = NULL;
2474 if (ms->postcopy_qemufile_src) {
2475 migration_ioc_unregister_yank_from_file(ms->postcopy_qemufile_src);
2476 qemu_file_shutdown(ms->postcopy_qemufile_src);
2477 qemu_fclose(ms->postcopy_qemufile_src);
2478 ms->postcopy_qemufile_src = NULL;
2491 QEMUFile *rp = ms->rp_state.from_dst_file;
2521 if ((rp_cmd_args[header_type].len != -1 &&
2558 qemu_sem_post(&ms->rp_state.rp_pong_acks);
2613 ms->switchover_acked = true;
2629 if (ms->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
2651 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
2652 if (!ms->rp_state.from_dst_file) {
2653 return -1;
2658 qemu_thread_create(&ms->rp_state.rp_thread, MIGRATION_THREAD_SRC_RETURN,
2660 ms->rp_state.rp_thread_created = true;
2670 if (!ms->rp_state.rp_thread_created) {
2682 WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) {
2683 if (migrate_has_error(ms) && ms->rp_state.from_dst_file) {
2684 qemu_file_shutdown(ms->rp_state.from_dst_file);
2688 qemu_thread_join(&ms->rp_state.rp_thread);
2689 ms->rp_state.rp_thread_created = false;
2701 qemu_sem_wait(&ms->rp_state.rp_pong_acks);
2706 * Returns non-0 on error
2725 if (ms->state != MIGRATION_STATUS_CANCELLING) {
2726 migrate_set_state(&ms->state, ms->state,
2731 return -1;
2735 if (!qemu_savevm_state_postcopy_prepare(ms->to_dst_file, errp)) {
2736 return -1;
2745 error_setg_errno(errp, -ret, "%s: Failed to stop the VM", __func__);
2754 * Cause any non-postcopiable, but iterative devices to
2757 ret = qemu_savevm_state_complete_precopy_iterable(ms->to_dst_file, true);
2759 error_setg(errp, "Postcopy save non-postcopiable iterables failed");
2764 * in Finish migrate and with the io-lock held everything should
2775 qemu_savevm_send_ping(ms->to_dst_file, 2);
2782 * it starts processing it. Unfortunately the ad-hoc migration format
2784 * parsing it through each devices load-state code (especially the open
2790 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
2802 error_setg(errp, "Postcopy save non-iterable device states failed");
2818 ret = qemu_file_get_error(ms->to_dst_file);
2825 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
2844 qemu_savevm_send_ping(ms->to_dst_file, 4);
2851 ret = qemu_file_get_error(ms->to_dst_file);
2853 error_setg_errno(errp, -ret, "postcopy_start: Migration stream error");
2864 /* Now, switchover looks all fine, switching to postcopy-active */
2865 migrate_set_state(&ms->state, MIGRATION_STATUS_DEVICE,
2875 if (ms->state != MIGRATION_STATUS_CANCELLING) {
2876 migrate_set_state(&ms->state, ms->state, MIGRATION_STATUS_FAILED);
2881 return -1;
2889 * Prepares for the switchover, depending on "pause-before-switchover"
2893 * [postcopy-]active -> pre-switchover -> device
2896 * [postcopy-]active -> device
2903 if (s->state == MIGRATION_STATUS_CANCELLING) {
2916 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_DEVICE);
2925 qemu_event_reset(&s->pause_event);
2928 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_PRE_SWITCHOVER);
2931 qemu_event_wait(&s->pause_event);
2937 * pre-switchover.
2939 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
2942 return s->state == MIGRATION_STATUS_DEVICE;
2970 qemu_savevm_maybe_send_switchover_start(s->to_dst_file);
2989 ret = -EFAULT;
2993 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false);
3004 qemu_savevm_state_complete_postcopy(s->to_dst_file);
3011 if (migrate_postcopy_preempt() && s->preempt_pre_7_2) {
3029 if (s->state == MIGRATION_STATUS_ACTIVE) {
3031 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3034 ret = -1;
3045 if (qemu_file_get_error(s->to_dst_file)) {
3050 if (migrate_colo() && s->state == MIGRATION_STATUS_ACTIVE) {
3052 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
3061 if (qemu_file_get_error_obj(s->to_dst_file, &local_err)) {
3065 error_setg_errno(&local_err, -ret, "Error in migration completion");
3070 if (s->state != MIGRATION_STATUS_CANCELLING) {
3071 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
3083 int current_active_state = s->state;
3085 if (s->state == MIGRATION_STATUS_ACTIVE) {
3088 * The next step is to flush the non-RAM content (device state)
3092 qemu_put_buffer(s->to_dst_file, s->bioc->data, s->bioc->usage);
3093 qemu_fflush(s->to_dst_file);
3094 } else if (s->state == MIGRATION_STATUS_CANCELLING) {
3098 if (qemu_file_get_error(s->to_dst_file)) {
3107 migrate_set_state(&s->state, current_active_state,
3122 qemu_savevm_send_postcopy_resume(s->to_dst_file);
3124 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3126 return -1;
3130 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3134 return -1;
3154 * If preempt is enabled, re-establish the preempt channel. Note that
3169 * switch to postcopy-active afterwards)
3187 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
3195 * re-created when we resume.
3208 assert(s->to_dst_file);
3209 migration_ioc_unregister_yank_from_file(s->to_dst_file);
3210 qemu_mutex_lock(&s->qemu_file_lock);
3211 file = s->to_dst_file;
3212 s->to_dst_file = NULL;
3213 qemu_mutex_unlock(&s->qemu_file_lock);
3218 migrate_set_state(&s->state, s->state,
3229 qemu_sem_wait(&s->postcopy_pause_sem);
3230 } while (postcopy_is_paused(s->state));
3232 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3259 WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) {
3260 if (s->to_dst_file) {
3261 qemu_file_set_error_obj(s->to_dst_file, ret, err);
3271 int state = s->state;
3284 ret = qemu_file_get_error_obj_any(s->to_dst_file,
3285 s->postcopy_qemufile_src,
3310 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
3325 * Take the BQL here so that query-migrate on the QMP thread sees:
3326 * - atomic update of s->total_time and s->mbps;
3327 * - correct ordering of s->mbps update vs. s->state;
3331 s->total_time = end_time - s->start_time;
3332 transfer_time = s->total_time - s->setup_time;
3334 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
3337 migrate_set_state(&s->state, s->state,
3348 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3349 s->iteration_initial_bytes = migration_transferred_bytes();
3350 s->iteration_initial_pages = ram_get_total_transferred_pages();
3363 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
3369 transferred = current_bytes - s->iteration_initial_bytes;
3370 time_spent = current_time - s->iteration_start_time;
3384 s->threshold_size = expected_bw_per_ms * migrate_downtime_limit();
3386 s->mbps = (((double) transferred * 8.0) /
3389 transferred_pages = ram_get_total_transferred_pages() -
3390 s->iteration_initial_pages;
3391 s->pages_per_second = (double) transferred_pages /
3400 s->expected_downtime =
3411 s->threshold_size);
3425 return s->switchover_acked;
3443 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
3447 /* Fast path - get the estimated amount of pending data */
3466 if (pending_size < s->threshold_size) {
3474 if (must_precopy <= s->threshold_size &&
3475 can_switchover && qatomic_read(&s->start_postcopy)) {
3490 complete_ready = can_switchover && (pending_size <= s->threshold_size);
3500 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
3509 * If we enabled cpu throttling for auto-converge, turn it off.
3517 switch (s->state) {
3524 s->vm_old_state = RUN_STATE_RUNNING;
3530 * Re-activate the block drives if they're inactivated. Note, COLO
3531 * shouldn't use block_active at all, so it should be no-op there.
3534 if (runstate_is_live(s->vm_old_state)) {
3540 runstate_set(s->vm_old_state);
3547 error_report("%s: Unknown ending state %d", __func__, s->state);
3558 * Stop tracking RAM writes - un-protect memory, un-register UFFD
3565 switch (s->state) {
3575 error_report("%s: Unknown ending state %d", __func__, s->state);
3591 res = qemu_savevm_state_iterate(s->to_dst_file, false);
3602 qemu_sem_post(&migrate_get_current()->rate_limit_sem);
3607 qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
3618 if (migration_rate_exceeded(s->to_dst_file)) {
3620 if (qemu_file_get_error(s->to_dst_file)) {
3627 int ms = s->iteration_start_time + BUFFER_DELAY - now;
3629 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
3637 qemu_sem_post(&s->rate_limit_sem);
3654 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_WAIT_UNPLUG);
3656 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
3658 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3660 if (s->state != MIGRATION_STATUS_WAIT_UNPLUG) {
3667 while (timeout-- && qemu_savevm_state_guest_unplug_pending()) {
3668 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3677 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG, new_state);
3679 migrate_set_state(&s->state, old_state, new_state);
3709 qemu_savevm_state_header(s->to_dst_file);
3716 if (s->rp_state.rp_thread_created) {
3718 qemu_savevm_send_open_return_path(s->to_dst_file);
3721 qemu_savevm_send_ping(s->to_dst_file, 1);
3730 qemu_savevm_send_postcopy_advise(s->to_dst_file);
3735 qemu_savevm_send_colo_enable(s->to_dst_file);
3744 ret = qemu_savevm_state_setup(s->to_dst_file, &local_err);
3751 * Handle SETUP failures after waiting for virtio-net-failover
3757 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
3762 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
3767 if (urgent || !migration_rate_exceeded(s->to_dst_file)) {
3809 vm_resume(s->vm_old_state);
3847 * stash the non-RAM part of the vmstate to the temporary buffer,
3849 * with vCPUs running and, finally, write stashed non-RAM part of
3852 s->bioc = qio_channel_buffer_new(512 * 1024);
3853 qio_channel_set_name(QIO_CHANNEL(s->bioc), "vmstate-buffer");
3854 fb = qemu_file_new_output(QIO_CHANNEL(s->bioc));
3855 object_unref(OBJECT(s->bioc));
3860 * Prepare for tracking memory writes with UFFD-WP - populate
3868 qemu_savevm_state_header(s->to_dst_file);
3869 ret = qemu_savevm_state_setup(s->to_dst_file, &local_err);
3876 * Handle SETUP failures after waiting for virtio-net-failover
3882 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
3887 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
3901 * Since we are going to get non-iterable state data directly
3902 * from s->bioc->data, explicit flush is needed here.
3913 * Start VM from BH handler to avoid write-fault lock here.
3914 * UFFD-WP protection for the whole RAM is already enabled so
3916 * writes to virtio VQs memory which is in write-protected region.
3945 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
3964 bool resume = (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP);
3974 s->expected_downtime = migrate_downtime_limit();
3985 error_report_err(error_copy(s->error));
4006 qemu_file_set_blocking(s->to_dst_file, true);
4010 * precopy, only if user specified "return-path" capability would
4015 error_setg(&local_err, "Unable to open return-path for postcopy");
4025 if (migrate_postcopy_preempt() && s->preempt_pre_7_2) {
4031 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP,
4033 qemu_sem_post(&s->postcopy_pause_sem);
4040 error_setg(&local_err, "migration_stop_vm failed, error %d", -ret);
4054 qemu_thread_create(&s->thread, MIGRATION_THREAD_SNAPSHOT,
4057 qemu_thread_create(&s->thread, MIGRATION_THREAD_SRC_MAIN,
4060 s->migration_thread_running = true;
4065 if (s->state != MIGRATION_STATUS_CANCELLING) {
4066 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
4076 dc->user_creatable = false;
4085 qemu_mutex_destroy(&ms->error_mutex);
4086 qemu_mutex_destroy(&ms->qemu_file_lock);
4087 qemu_sem_destroy(&ms->wait_unplug_sem);
4088 qemu_sem_destroy(&ms->rate_limit_sem);
4089 qemu_event_destroy(&ms->pause_event);
4090 qemu_sem_destroy(&ms->postcopy_pause_sem);
4091 qemu_sem_destroy(&ms->rp_state.rp_sem);
4092 qemu_sem_destroy(&ms->rp_state.rp_pong_acks);
4093 qemu_sem_destroy(&ms->postcopy_qemufile_src_sem);
4094 error_free(ms->error);
4101 ms->state = MIGRATION_STATUS_NONE;
4102 ms->mbps = -1;
4103 ms->pages_per_second = -1;
4104 qemu_event_init(&ms->pause_event, false);
4105 qemu_mutex_init(&ms->error_mutex);
4107 migrate_params_init(&ms->parameters);
4109 qemu_sem_init(&ms->postcopy_pause_sem, 0);
4110 qemu_sem_init(&ms->rp_state.rp_sem, 0);
4111 qemu_sem_init(&ms->rp_state.rp_pong_acks, 0);
4112 qemu_sem_init(&ms->rate_limit_sem, 0);
4113 qemu_sem_init(&ms->wait_unplug_sem, 0);
4114 qemu_sem_init(&ms->postcopy_qemufile_src_sem, 0);
4115 qemu_mutex_init(&ms->qemu_file_lock);
4127 if (!migrate_params_check(&ms->parameters, errp)) {
4131 return migrate_caps_check(old_caps, ms->capabilities, errp);
4142 * TYPE_DEVICE's "-global" properties.