Lines Matching +full:post +full:- +full:processing

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"
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;
584 WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
586 if (!received && !g_tree_lookup(mis->page_requested, aligned)) {
592 g_tree_insert(mis->page_requested, aligned, (gpointer)1);
593 qatomic_inc(&mis->page_requested_count);
594 trace_postcopy_page_req_add(aligned, mis->page_requested_count);
626 return -ENOTSUP;
630 error_report("ENABLE_COLO command come in migration stream, but x-colo "
632 return -EINVAL;
637 return -EBUSY;
647 QAPI_LIST_PREPEND(mis->socket_address_list,
666 InetSocketAddress *isock = &addr->u.rdma;
667 strList **tail = &addr->u.exec.args;
670 addr->transport = MIGRATION_ADDRESS_TYPE_EXEC;
676 QAPI_LIST_APPEND(tail, g_strdup("-c"));
684 addr->transport = MIGRATION_ADDRESS_TYPE_RDMA;
689 addr->transport = MIGRATION_ADDRESS_TYPE_SOCKET;
694 addr->u.socket.type = saddr->type;
695 addr->u.socket.u = saddr->u;
699 addr->transport = MIGRATION_ADDRESS_TYPE_FILE;
700 addr->u.file.filename = g_strdup(uri + strlen("file:"));
701 if (file_parse_offset(addr->u.file.filename, &addr->u.file.offset,
710 val->channel_type = MIGRATION_CHANNEL_TYPE_MAIN;
711 val->addr = g_steal_pointer(&addr);
719 MigrationStatus current = mis->state;
735 migrate_set_state(&mis->state, current, MIGRATION_STATUS_SETUP);
757 if (channels->next) {
762 addr = channels->value->addr;
770 addr = channel->addr;
782 if (addr->transport == MIGRATION_ADDRESS_TYPE_SOCKET) {
783 SocketAddress *saddr = &addr->u.socket;
784 if (saddr->type == SOCKET_ADDRESS_TYPE_INET ||
785 saddr->type == SOCKET_ADDRESS_TYPE_UNIX ||
786 saddr->type == SOCKET_ADDRESS_TYPE_VSOCK) {
788 } else if (saddr->type == SOCKET_ADDRESS_TYPE_FD) {
789 fd_start_incoming_migration(saddr->u.fd.str, errp);
792 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_RDMA) {
793 rdma_start_incoming_migration(&addr->u.rdma, errp);
795 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_EXEC) {
796 exec_start_incoming_migration(addr->u.exec.args, errp);
797 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_FILE) {
798 file_start_incoming_migration(&addr->u.file, errp);
811 trace_vmstate_downtime_checkpoint("dst-precopy-bh-enter");
817 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
819 trace_vmstate_downtime_checkpoint("dst-precopy-bh-announced");
832 * We used to have cap 'late-block-activate' but now we do this
851 trace_vmstate_downtime_checkpoint("dst-precopy-bh-vm-started");
857 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
871 assert(mis->from_src_file);
873 mis->largest_page_size = qemu_ram_pagesize_largest();
875 migrate_set_state(&mis->state, MIGRATION_STATUS_SETUP,
878 mis->loadvm_co = qemu_coroutine_self();
879 ret = qemu_loadvm_state(mis->from_src_file);
880 mis->loadvm_co = NULL;
882 trace_vmstate_downtime_checkpoint("dst-precopy-loadvm-completed");
906 error_setg(&local_err, "load of migration failed: %s", strerror(-ret));
919 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
926 if (mis->exit_on_error) {
927 WITH_QEMU_LOCK_GUARD(&s->error_mutex) {
928 error_report_err(s->error);
929 s->error = NULL;
947 assert(!mis->from_src_file);
948 mis->from_src_file = f;
963 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
967 assert(mis->from_src_file);
969 qemu_file_set_blocking(mis->from_src_file, true);
971 /* Re-configure the return path */
972 mis->to_src_file = qemu_file_get_return_path(mis->from_src_file);
974 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
984 qemu_sem_post(&mis->postcopy_pause_sem_dst);
1003 if (!mis->from_src_file) {
1049 } else if (!mis->from_src_file &&
1050 mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
1057 } else if (mis->from_src_file && migrate_multifd()) {
1059 * Non-peekable channels like tls/file are processed as
1063 } else if (!mis->from_src_file) {
1066 error_setg(errp, "non-peekable channel used without multifd");
1089 assert(!mis->postcopy_qemufile_dst);
1117 if (migrate_postcopy_preempt() && !mis->postcopy_qemufile_dst) {
1131 * to indicate that we've finished with the RP. Non-0 value indicates
1171 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
1188 qemu_mutex_lock(&mis->rp_mutex);
1189 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
1190 qemu_mutex_unlock(&mis->rp_mutex);
1211 switch (s->state) {
1233 return (s->state == MIGRATION_STATUS_ACTIVE ||
1234 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
1239 return (s->state == MIGRATION_STATUS_COMPLETED) || migration_in_postcopy();
1244 info->has_status = true;
1245 info->has_setup_time = true;
1246 info->setup_time = s->setup_time;
1248 if (s->state == MIGRATION_STATUS_COMPLETED) {
1249 info->has_total_time = true;
1250 info->total_time = s->total_time;
1252 info->has_total_time = true;
1253 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
1254 s->start_time;
1258 info->has_downtime = true;
1259 info->downtime = s->downtime;
1261 info->has_expected_downtime = true;
1262 info->expected_downtime = s->expected_downtime;
1270 info->ram = g_malloc0(sizeof(*info->ram));
1271 info->ram->transferred = migration_transferred_bytes();
1272 info->ram->total = ram_bytes_total();
1273 info->ram->duplicate = stat64_get(&mig_stats.zero_pages);
1274 info->ram->normal = stat64_get(&mig_stats.normal_pages);
1275 info->ram->normal_bytes = info->ram->normal * page_size;
1276 info->ram->mbps = s->mbps;
1277 info->ram->dirty_sync_count =
1279 info->ram->dirty_sync_missed_zero_copy =
1281 info->ram->postcopy_requests =
1283 info->ram->page_size = page_size;
1284 info->ram->multifd_bytes = stat64_get(&mig_stats.multifd_bytes);
1285 info->ram->pages_per_second = s->pages_per_second;
1286 info->ram->precopy_bytes = stat64_get(&mig_stats.precopy_bytes);
1287 info->ram->downtime_bytes = stat64_get(&mig_stats.downtime_bytes);
1288 info->ram->postcopy_bytes = stat64_get(&mig_stats.postcopy_bytes);
1291 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
1292 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
1293 info->xbzrle_cache->bytes = xbzrle_counters.bytes;
1294 info->xbzrle_cache->pages = xbzrle_counters.pages;
1295 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
1296 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
1297 info->xbzrle_cache->encoding_rate = xbzrle_counters.encoding_rate;
1298 info->xbzrle_cache->overflow = xbzrle_counters.overflow;
1302 info->has_cpu_throttle_percentage = true;
1303 info->cpu_throttle_percentage = cpu_throttle_get_percentage();
1306 if (s->state != MIGRATION_STATUS_COMPLETED) {
1307 info->ram->remaining = ram_bytes_remaining();
1308 info->ram->dirty_pages_rate =
1313 info->has_dirty_limit_throttle_time_per_round = true;
1314 info->dirty_limit_throttle_time_per_round =
1317 info->has_dirty_limit_ring_full_time = true;
1318 info->dirty_limit_ring_full_time = dirtylimit_ring_full_time();
1325 int state = qatomic_read(&s->state);
1328 info->blocked_reasons = NULL;
1332 * a) devices marked in VMState as non-migratable, and
1336 qemu_savevm_non_migratable_list(&info->blocked_reasons);
1339 QAPI_LIST_PREPEND(info->blocked_reasons,
1340 g_strdup(error_get_pretty(cur_blocker->data)));
1343 info->has_blocked_reasons = info->blocked_reasons != NULL;
1351 info->has_status = true;
1352 info->has_total_time = false;
1368 info->has_status = true;
1377 info->has_status = true;
1380 info->has_status = true;
1383 info->has_status = true;
1386 info->status = state;
1388 QEMU_LOCK_GUARD(&s->error_mutex);
1389 if (s->error) {
1390 info->error_desc = g_strdup(error_get_pretty(s->error));
1398 if (mis->socket_address_list) {
1399 info->has_socket_address = true;
1400 info->socket_address =
1401 QAPI_CLONE(SocketAddressList, mis->socket_address_list);
1404 switch (mis->state) {
1414 info->has_status = true;
1417 info->has_status = true;
1423 info->status = mis->state;
1425 if (!info->error_desc) {
1427 QEMU_LOCK_GUARD(&s->error_mutex);
1429 if (s->error) {
1430 info->error_desc = g_strdup(error_get_pretty(s->error));
1455 if (s->state == MIGRATION_STATUS_NONE) {
1464 qatomic_set(&s->start_postcopy, true);
1481 g_clear_pointer(&s->vmdesc, json_writer_free);
1493 g_free(s->hostname);
1494 s->hostname = NULL;
1502 if (s->migration_thread_running) {
1504 qemu_thread_join(&s->thread);
1505 s->migration_thread_running = false;
1509 WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) {
1514 tmp = s->to_dst_file;
1515 s->to_dst_file = NULL;
1531 if (s->state == MIGRATION_STATUS_CANCELLING) {
1532 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
1536 if (s->error) {
1538 error_report_err(error_copy(s->error));
1553 QEMU_LOCK_GUARD(&s->error_mutex);
1557 if (!s->error) {
1558 s->error = error_copy(error);
1565 QEMU_LOCK_GUARD(&s->error_mutex);
1566 return qatomic_read(&s->error);
1571 QEMU_LOCK_GUARD(&s->error_mutex);
1572 if (s->error) {
1573 error_free(s->error);
1574 s->error = NULL;
1580 MigrationStatus current = s->state;
1583 assert(s->to_dst_file == NULL);
1603 migrate_set_state(&s->state, current, next);
1611 bool setup = (s->state == MIGRATION_STATUS_SETUP);
1616 qmp_cancel_vcpu_dirty_limit(false, -1, NULL);
1619 WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) {
1620 if (s->rp_state.from_dst_file) {
1622 qemu_file_shutdown(s->rp_state.from_dst_file);
1627 old_state = s->state;
1633 qemu_event_set(&s->pause_event);
1635 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
1636 } while (s->state != MIGRATION_STATUS_CANCELLING);
1643 if (s->state == MIGRATION_STATUS_CANCELLING) {
1644 WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) {
1645 if (s->to_dst_file) {
1646 qemu_file_shutdown(s->to_dst_file);
1655 if (setup && !s->to_dst_file) {
1656 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
1666 notify->notify = (NotifierWithReturnFunc)func;
1678 if (notify->notify) {
1680 notify->notify = NULL;
1687 MigMode mode = s->parameters.mode;
1700 return (s->state == MIGRATION_STATUS_CANCELLED ||
1701 s->state == MIGRATION_STATUS_FAILED);
1708 switch (s->state) {
1753 return qemu_thread_is_self(&s->thread);
1758 MigMode mode = s->parameters.mode;
1777 s->to_dst_file = NULL;
1778 s->state = MIGRATION_STATUS_NONE;
1779 s->rp_state.from_dst_file = NULL;
1780 s->mbps = 0.0;
1781 s->pages_per_second = 0.0;
1782 s->downtime = 0;
1783 s->expected_downtime = 0;
1784 s->setup_time = 0;
1785 s->start_postcopy = false;
1786 s->migration_thread_running = false;
1787 error_free(s->error);
1788 s->error = NULL;
1791 s->vmdesc = json_writer_new(false);
1794 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
1796 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1797 s->total_time = 0;
1798 s->vm_old_state = -1;
1799 s->iteration_initial_bytes = 0;
1800 s->threshold_size = 0;
1801 s->switchover_acked = false;
1802 s->rdma_migration = false;
1834 "(--only-migratable) for: ");
1845 while (mode != -1 && mode != MIG_MODE_ALL) {
1851 modes = BIT(MIG_MODE__MAX) - 1;
1874 return migrate_add_blocker_modes(reasonp, errp, MIG_MODE_NORMAL, -1);
1887 return -EACCES;
1889 return -EBUSY;
1896 int modes = BIT(MIG_MODE__MAX) - 1;
1899 return -EBUSY;
1930 error_setg(errp, "'-incoming' was not specified on the command line");
1938 mis->exit_on_error =
1974 if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
1985 * only re-setup the migration stream and poke existing migration
1997 if (migration_postcopy_is_alive(ms->state)) {
2006 qemu_mutex_lock(&ms->qemu_file_lock);
2007 if (ms->to_dst_file) {
2008 ret = qemu_file_shutdown(ms->to_dst_file);
2010 qemu_mutex_unlock(&ms->qemu_file_lock);
2024 if (migration_postcopy_is_alive(mis->state)) {
2025 ret = qemu_file_shutdown(mis->from_src_file);
2032 error_setg(errp, "migrate-pause is currently only supported "
2033 "during postcopy-active or postcopy-recover state");
2045 error_propagate(errp, error_copy(blockers->data));
2056 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
2063 * Postcopy recovery won't work well with release-ram
2064 * capability since release-ram will drop the page buffer as
2070 * Luckily release-ram was designed to only be used when src
2076 "when release-ram capability is set");
2080 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
2115 error_setg(errp, "Cannot use TLS with mapped-ram");
2120 error_setg(errp, "Cannot use compression with mapped-ram");
2155 s->hup_source = qio_channel_create_watch(ioc, G_IO_HUP);
2156 g_source_set_callback(s->hup_source, cb, opaque, NULL);
2157 g_source_attach(s->hup_source, NULL);
2162 if (s->hup_source) {
2163 g_source_destroy(s->hup_source);
2164 g_source_unref(s->hup_source);
2165 s->hup_source = NULL;
2204 for ( ; channels; channels = channels->next) {
2205 MigrationChannelType type = channels->value->channel_type;
2212 channelv[type] = channels->value;
2215 addr = channelv[MIGRATION_CHANNEL_TYPE_MAIN]->addr;
2227 addr = channel->addr;
2235 if (s->parameters.mode == MIG_MODE_CPR_TRANSFER && !cpr_channel) {
2251 * For cpr-transfer, the target may not be listening yet on the migration
2253 * us it is listening by closing the cpr-state socket. Wait for that HUP
2260 if (s->parameters.mode == MIG_MODE_CPR_TRANSFER) {
2287 if (addr->transport == MIGRATION_ADDRESS_TYPE_SOCKET) {
2288 SocketAddress *saddr = &addr->u.socket;
2289 if (saddr->type == SOCKET_ADDRESS_TYPE_INET ||
2290 saddr->type == SOCKET_ADDRESS_TYPE_UNIX ||
2291 saddr->type == SOCKET_ADDRESS_TYPE_VSOCK) {
2293 } else if (saddr->type == SOCKET_ADDRESS_TYPE_FD) {
2294 fd_start_outgoing_migration(s, saddr->u.fd.str, &local_err);
2297 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_RDMA) {
2298 rdma_start_outgoing_migration(s, &addr->u.rdma, &local_err);
2300 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_EXEC) {
2301 exec_start_outgoing_migration(s, addr->u.exec.args, &local_err);
2302 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_FILE) {
2303 file_start_outgoing_migration(s, &addr->u.file, &local_err);
2307 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
2340 if (s->state != state) {
2342 MigrationStatus_str(s->state));
2345 qemu_event_set(&s->pause_event);
2352 return -1;
2355 qemu_sem_wait(&s->rp_state.rp_sem);
2359 return -1;
2367 qemu_sem_post(&s->rp_state.rp_sem);
2371 ssize_t len; /* -1 = variable */
2374 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
2378 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
2379 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
2382 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
2438 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2448 * Release ms->rp_state.from_dst_file (and postcopy_qemufile_src if
2455 WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) {
2460 file = ms->rp_state.from_dst_file;
2461 ms->rp_state.from_dst_file = NULL;
2469 if (ms->postcopy_qemufile_src) {
2470 migration_ioc_unregister_yank_from_file(ms->postcopy_qemufile_src);
2471 qemu_file_shutdown(ms->postcopy_qemufile_src);
2472 qemu_fclose(ms->postcopy_qemufile_src);
2473 ms->postcopy_qemufile_src = NULL;
2486 QEMUFile *rp = ms->rp_state.from_dst_file;
2516 if ((rp_cmd_args[header_type].len != -1 &&
2553 qemu_sem_post(&ms->rp_state.rp_pong_acks);
2608 ms->switchover_acked = true;
2624 if (ms->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
2646 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
2647 if (!ms->rp_state.from_dst_file) {
2648 return -1;
2653 qemu_thread_create(&ms->rp_state.rp_thread, MIGRATION_THREAD_SRC_RETURN,
2655 ms->rp_state.rp_thread_created = true;
2665 if (!ms->rp_state.rp_thread_created) {
2677 WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) {
2678 if (migrate_has_error(ms) && ms->rp_state.from_dst_file) {
2679 qemu_file_shutdown(ms->rp_state.from_dst_file);
2683 qemu_thread_join(&ms->rp_state.rp_thread);
2684 ms->rp_state.rp_thread_created = false;
2696 qemu_sem_wait(&ms->rp_state.rp_pong_acks);
2701 * Returns non-0 on error
2720 if (ms->state != MIGRATION_STATUS_CANCELLING) {
2721 migrate_set_state(&ms->state, ms->state,
2726 return -1;
2730 if (!qemu_savevm_state_postcopy_prepare(ms->to_dst_file, errp)) {
2731 return -1;
2740 error_setg_errno(errp, -ret, "%s: Failed to stop the VM", __func__);
2749 * Cause any non-postcopiable, but iterative devices to
2752 ret = qemu_savevm_state_complete_precopy_iterable(ms->to_dst_file, true);
2754 error_setg(errp, "Postcopy save non-postcopiable iterables failed");
2759 * in Finish migrate and with the io-lock held everything should
2770 qemu_savevm_send_ping(ms->to_dst_file, 2);
2777 * it starts processing it. Unfortunately the ad-hoc migration format
2779 * parsing it through each devices load-state code (especially the open
2785 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
2797 error_setg(errp, "Postcopy save non-iterable device states failed");
2813 ret = qemu_file_get_error(ms->to_dst_file);
2820 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
2839 qemu_savevm_send_ping(ms->to_dst_file, 4);
2846 ret = qemu_file_get_error(ms->to_dst_file);
2848 error_setg_errno(errp, -ret, "postcopy_start: Migration stream error");
2859 /* Now, switchover looks all fine, switching to postcopy-active */
2860 migrate_set_state(&ms->state, MIGRATION_STATUS_DEVICE,
2870 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2875 return -1;
2883 * Prepares for the switchover, depending on "pause-before-switchover"
2887 * [postcopy-]active -> pre-switchover -> device
2890 * [postcopy-]active -> device
2897 if (s->state == MIGRATION_STATUS_CANCELLING) {
2910 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_DEVICE);
2919 qemu_event_reset(&s->pause_event);
2922 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_PRE_SWITCHOVER);
2925 qemu_event_wait(&s->pause_event);
2931 * pre-switchover.
2933 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
2936 return s->state == MIGRATION_STATUS_DEVICE;
2964 qemu_savevm_maybe_send_switchover_start(s->to_dst_file);
2983 ret = -EFAULT;
2987 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false);
2998 qemu_savevm_state_complete_postcopy(s->to_dst_file);
3005 if (migrate_postcopy_preempt() && s->preempt_pre_7_2) {
3023 if (s->state == MIGRATION_STATUS_ACTIVE) {
3025 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3028 ret = -1;
3039 if (qemu_file_get_error(s->to_dst_file)) {
3044 if (migrate_colo() && s->state == MIGRATION_STATUS_ACTIVE) {
3046 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
3055 if (qemu_file_get_error_obj(s->to_dst_file, &local_err)) {
3059 error_setg_errno(&local_err, -ret, "Error in migration completion");
3064 if (s->state != MIGRATION_STATUS_CANCELLING) {
3065 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
3077 int current_active_state = s->state;
3079 if (s->state == MIGRATION_STATUS_ACTIVE) {
3082 * The next step is to flush the non-RAM content (device state)
3086 qemu_put_buffer(s->to_dst_file, s->bioc->data, s->bioc->usage);
3087 qemu_fflush(s->to_dst_file);
3088 } else if (s->state == MIGRATION_STATUS_CANCELLING) {
3092 if (qemu_file_get_error(s->to_dst_file)) {
3101 migrate_set_state(&s->state, current_active_state,
3116 qemu_savevm_send_postcopy_resume(s->to_dst_file);
3118 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3120 return -1;
3124 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
3128 return -1;
3148 * If preempt is enabled, re-establish the preempt channel. Note that
3163 * switch to postcopy-active afterwards)
3181 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
3189 * re-created when we resume.
3202 assert(s->to_dst_file);
3203 migration_ioc_unregister_yank_from_file(s->to_dst_file);
3204 qemu_mutex_lock(&s->qemu_file_lock);
3205 file = s->to_dst_file;
3206 s->to_dst_file = NULL;
3207 qemu_mutex_unlock(&s->qemu_file_lock);
3212 migrate_set_state(&s->state, s->state,
3223 qemu_sem_wait(&s->postcopy_pause_sem);
3224 } while (postcopy_is_paused(s->state));
3226 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
3253 WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) {
3254 if (s->to_dst_file) {
3255 qemu_file_set_error_obj(s->to_dst_file, ret, err);
3265 int state = s->state;
3278 ret = qemu_file_get_error_obj_any(s->to_dst_file,
3279 s->postcopy_qemufile_src,
3304 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
3319 * Take the BQL here so that query-migrate on the QMP thread sees:
3320 * - atomic update of s->total_time and s->mbps;
3321 * - correct ordering of s->mbps update vs. s->state;
3325 s->total_time = end_time - s->start_time;
3326 transfer_time = s->total_time - s->setup_time;
3328 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
3331 migrate_set_state(&s->state, s->state,
3342 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
3343 s->iteration_initial_bytes = migration_transferred_bytes();
3344 s->iteration_initial_pages = ram_get_total_transferred_pages();
3357 if (current_time < s->iteration_start_time + BUFFER_DELAY) {
3363 transferred = current_bytes - s->iteration_initial_bytes;
3364 time_spent = current_time - s->iteration_start_time;
3378 s->threshold_size = expected_bw_per_ms * migrate_downtime_limit();
3380 s->mbps = (((double) transferred * 8.0) /
3383 transferred_pages = ram_get_total_transferred_pages() -
3384 s->iteration_initial_pages;
3385 s->pages_per_second = (double) transferred_pages /
3394 s->expected_downtime =
3405 s->threshold_size);
3419 return s->switchover_acked;
3437 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
3444 if (pending_size < s->threshold_size) {
3450 if ((!pending_size || pending_size < s->threshold_size) && can_switchover) {
3457 if (!in_postcopy && must_precopy <= s->threshold_size && can_switchover &&
3458 qatomic_read(&s->start_postcopy)) {
3467 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
3476 * If we enabled cpu throttling for auto-converge, turn it off.
3484 switch (s->state) {
3491 s->vm_old_state = RUN_STATE_RUNNING;
3497 * Re-activate the block drives if they're inactivated. Note, COLO
3498 * shouldn't use block_active at all, so it should be no-op there.
3501 if (runstate_is_live(s->vm_old_state)) {
3507 runstate_set(s->vm_old_state);
3514 error_report("%s: Unknown ending state %d", __func__, s->state);
3525 * Stop tracking RAM writes - un-protect memory, un-register UFFD
3532 switch (s->state) {
3542 error_report("%s: Unknown ending state %d", __func__, s->state);
3558 res = qemu_savevm_state_iterate(s->to_dst_file, false);
3569 qemu_sem_post(&migrate_get_current()->rate_limit_sem);
3574 qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
3585 if (migration_rate_exceeded(s->to_dst_file)) {
3587 if (qemu_file_get_error(s->to_dst_file)) {
3592 * something urgent to post the semaphore.
3594 int ms = s->iteration_start_time + BUFFER_DELAY - now;
3596 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
3604 qemu_sem_post(&s->rate_limit_sem);
3621 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_WAIT_UNPLUG);
3623 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
3625 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3627 if (s->state != MIGRATION_STATUS_WAIT_UNPLUG) {
3634 while (timeout-- && qemu_savevm_state_guest_unplug_pending()) {
3635 qemu_sem_timedwait(&s->wait_unplug_sem, 250);
3644 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG, new_state);
3646 migrate_set_state(&s->state, old_state, new_state);
3676 qemu_savevm_state_header(s->to_dst_file);
3683 if (s->rp_state.rp_thread_created) {
3685 qemu_savevm_send_open_return_path(s->to_dst_file);
3688 qemu_savevm_send_ping(s->to_dst_file, 1);
3697 qemu_savevm_send_postcopy_advise(s->to_dst_file);
3702 qemu_savevm_send_colo_enable(s->to_dst_file);
3711 ret = qemu_savevm_state_setup(s->to_dst_file, &local_err);
3718 * Handle SETUP failures after waiting for virtio-net-failover
3724 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
3729 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
3734 if (urgent || !migration_rate_exceeded(s->to_dst_file)) {
3776 vm_resume(s->vm_old_state);
3814 * stash the non-RAM part of the vmstate to the temporary buffer,
3816 * with vCPUs running and, finally, write stashed non-RAM part of
3819 s->bioc = qio_channel_buffer_new(512 * 1024);
3820 qio_channel_set_name(QIO_CHANNEL(s->bioc), "vmstate-buffer");
3821 fb = qemu_file_new_output(QIO_CHANNEL(s->bioc));
3822 object_unref(OBJECT(s->bioc));
3827 * Prepare for tracking memory writes with UFFD-WP - populate
3835 qemu_savevm_state_header(s->to_dst_file);
3836 ret = qemu_savevm_state_setup(s->to_dst_file, &local_err);
3843 * Handle SETUP failures after waiting for virtio-net-failover
3849 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
3854 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
3868 * Since we are going to get non-iterable state data directly
3869 * from s->bioc->data, explicit flush is needed here.
3880 * Start VM from BH handler to avoid write-fault lock here.
3881 * UFFD-WP protection for the whole RAM is already enabled so
3883 * writes to virtio VQs memory which is in write-protected region.
3913 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
3932 bool resume = (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP);
3942 s->expected_downtime = migrate_downtime_limit();
3953 error_report_err(error_copy(s->error));
3974 qemu_file_set_blocking(s->to_dst_file, true);
3978 * precopy, only if user specified "return-path" capability would
3983 error_setg(&local_err, "Unable to open return-path for postcopy");
3993 if (migrate_postcopy_preempt() && s->preempt_pre_7_2) {
3999 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP,
4001 qemu_sem_post(&s->postcopy_pause_sem);
4008 error_setg(&local_err, "migration_stop_vm failed, error %d", -ret);
4022 qemu_thread_create(&s->thread, MIGRATION_THREAD_SNAPSHOT,
4025 qemu_thread_create(&s->thread, MIGRATION_THREAD_SRC_MAIN,
4028 s->migration_thread_running = true;
4033 if (s->state != MIGRATION_STATUS_CANCELLING) {
4034 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
4044 dc->user_creatable = false;
4053 qemu_mutex_destroy(&ms->error_mutex);
4054 qemu_mutex_destroy(&ms->qemu_file_lock);
4055 qemu_sem_destroy(&ms->wait_unplug_sem);
4056 qemu_sem_destroy(&ms->rate_limit_sem);
4057 qemu_event_destroy(&ms->pause_event);
4058 qemu_sem_destroy(&ms->postcopy_pause_sem);
4059 qemu_sem_destroy(&ms->rp_state.rp_sem);
4060 qemu_sem_destroy(&ms->rp_state.rp_pong_acks);
4061 qemu_sem_destroy(&ms->postcopy_qemufile_src_sem);
4062 error_free(ms->error);
4069 ms->state = MIGRATION_STATUS_NONE;
4070 ms->mbps = -1;
4071 ms->pages_per_second = -1;
4072 qemu_event_init(&ms->pause_event, false);
4073 qemu_mutex_init(&ms->error_mutex);
4075 migrate_params_init(&ms->parameters);
4077 qemu_sem_init(&ms->postcopy_pause_sem, 0);
4078 qemu_sem_init(&ms->rp_state.rp_sem, 0);
4079 qemu_sem_init(&ms->rp_state.rp_pong_acks, 0);
4080 qemu_sem_init(&ms->rate_limit_sem, 0);
4081 qemu_sem_init(&ms->wait_unplug_sem, 0);
4082 qemu_sem_init(&ms->postcopy_qemufile_src_sem, 0);
4083 qemu_mutex_init(&ms->qemu_file_lock);
4095 if (!migrate_params_check(&ms->parameters, errp)) {
4099 return migrate_caps_check(old_caps, ms->capabilities, errp);
4110 * TYPE_DEVICE's "-global" properties.