blob: bb3e99194c608d4a08fe46182cfa67d94635d3c9 [file] [log] [blame]
aliguoria672b462008-11-11 21:33:36 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
Juan Quintela76cc7b52015-05-08 13:20:21 +02005 * Copyright (c) 2009-2015 Red Hat Inc
6 *
7 * Authors:
8 * Juan Quintela <quintela@redhat.com>
aliguoria672b462008-11-11 21:33:36 +00009 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
27 */
aliguoria672b462008-11-11 21:33:36 +000028
Peter Maydell1393a482016-01-26 18:16:54 +000029#include "qemu/osdep.h"
Amit Shahabfd9ce2014-06-20 18:56:08 +053030#include "hw/boards.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020031#include "net/net.h"
Juan Quintela6666c962017-04-24 20:07:27 +020032#include "migration.h"
Juan Quintela5e224792017-04-20 14:25:55 +020033#include "migration/snapshot.h"
Juan Quintela947701c2023-04-26 19:04:06 +020034#include "migration-stats.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020035#include "migration/vmstate.h"
Juan Quintelaf8d806c2017-04-21 17:39:30 +020036#include "migration/misc.h"
Juan Quintelaf2a8f0a2017-04-24 13:42:55 +020037#include "migration/register.h"
Juan Quintela84a899d2017-04-24 18:53:30 +020038#include "migration/global_state.h"
Daniel P. Berrangé67bdabe2022-06-20 12:01:54 +010039#include "migration/channel-block.h"
Juan Quintela7b1e1a22017-04-17 20:26:27 +020040#include "ram.h"
Juan Quintela08a0aee2017-04-20 18:52:18 +020041#include "qemu-file.h"
Juan Quintela20a519a2017-04-20 14:48:46 +020042#include "savevm.h"
Juan Quintelabe07b0a2017-04-20 13:12:24 +020043#include "postcopy-ram.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010044#include "qapi/error.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060045#include "qapi/qapi-commands-migration.h"
Daniel P. Berrangéf1a9fcd2021-02-04 12:48:30 +000046#include "qapi/clone-visitor.h"
47#include "qapi/qapi-builtin-visit.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010048#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010049#include "qemu/error-report.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010050#include "sysemu/cpus.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010051#include "exec/memory.h"
Juan Quintela51180422017-04-24 20:50:19 +020052#include "exec/target_page.h"
Juan Quintela517a13c2012-05-21 23:46:44 +020053#include "trace.h"
Orit Wasserman28085f72013-03-22 16:47:58 +020054#include "qemu/iov.h"
Markus Armbrustere2c1c342022-12-21 14:35:49 +010055#include "qemu/job.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020056#include "qemu/main-loop.h"
Wenchao Xiade08c602013-05-25 11:09:43 +080057#include "block/snapshot.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020058#include "qemu/cutils.h"
Daniel P. Berrange61b67d42016-04-27 11:05:01 +010059#include "io/channel-buffer.h"
Daniel P. Berrange89258392016-04-27 11:05:08 +010060#include "io/channel-file.h"
Pavel Dovgalyuk377b21c2018-02-27 12:52:14 +030061#include "sysemu/replay.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020062#include "sysemu/runstate.h"
Markus Armbruster46517dd2019-08-12 07:23:57 +020063#include "sysemu/sysemu.h"
Philippe Mathieu-Daudéda278d52020-05-08 12:02:22 +020064#include "sysemu/xen.h"
Zhang Chenaad555c2018-09-03 12:38:47 +080065#include "migration/colo.h"
Yury Kotov6cafc8e2019-02-15 20:45:48 +030066#include "qemu/bitmap.h"
Dr. David Alan Gilbert76595052019-02-27 13:24:08 +000067#include "net/announce.h"
Lukas Straubb5eea992020-12-28 16:08:52 +010068#include "qemu/yank.h"
Peter Xu39675ff2021-07-22 13:58:41 -040069#include "yank_functions.h"
Dr. David Alan Gilbertbb25a722022-01-13 19:44:52 +000070#include "sysemu/qtest.h"
Juan Quintela1f0776f2023-03-01 21:18:45 +010071#include "options.h"
Nolan18995b92009-10-15 16:53:55 -070072
Bihong Yu49324e92020-10-20 11:10:46 +080073const unsigned int postcopy_ram_discard_version;
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +000074
Juan Quintela20a519a2017-04-20 14:48:46 +020075/* Subcommands for QEMU_VM_COMMAND */
76enum qemu_vm_cmd {
77 MIG_CMD_INVALID = 0, /* Must be 0 */
78 MIG_CMD_OPEN_RETURN_PATH, /* Tell the dest to open the Return path */
79 MIG_CMD_PING, /* Request a PONG on the RP */
80
81 MIG_CMD_POSTCOPY_ADVISE, /* Prior to any page transfers, just
82 warn we might want to do PC */
83 MIG_CMD_POSTCOPY_LISTEN, /* Start listening for incoming
84 pages as it's running. */
85 MIG_CMD_POSTCOPY_RUN, /* Start execution */
86
87 MIG_CMD_POSTCOPY_RAM_DISCARD, /* A list of pages to discard that
88 were previously sent during
89 precopy but are dirty. */
90 MIG_CMD_PACKAGED, /* Send a wrapped stream within this stream */
Zhang Chenaad555c2018-09-03 12:38:47 +080091 MIG_CMD_ENABLE_COLO, /* Enable COLO */
Peter Xu858b6d62018-07-10 17:44:24 +080092 MIG_CMD_POSTCOPY_RESUME, /* resume postcopy on dest */
Peter Xuf25d4222018-05-02 18:47:27 +080093 MIG_CMD_RECV_BITMAP, /* Request for recved bitmap on dst */
Juan Quintela20a519a2017-04-20 14:48:46 +020094 MIG_CMD_MAX
95};
96
Daniel Henrique Barbozaee555cd2018-01-26 13:59:40 -020097#define MAX_VM_CMD_PACKAGED_SIZE UINT32_MAX
Dr. David Alan Gilbertc76ca182015-11-05 18:10:45 +000098static struct mig_cmd_args {
99 ssize_t len; /* -1 = variable */
100 const char *name;
101} mig_cmd_args[] = {
102 [MIG_CMD_INVALID] = { .len = -1, .name = "INVALID" },
Dr. David Alan Gilbert2e377012015-11-05 18:10:46 +0000103 [MIG_CMD_OPEN_RETURN_PATH] = { .len = 0, .name = "OPEN_RETURN_PATH" },
104 [MIG_CMD_PING] = { .len = sizeof(uint32_t), .name = "PING" },
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +0300105 [MIG_CMD_POSTCOPY_ADVISE] = { .len = -1, .name = "POSTCOPY_ADVISE" },
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +0000106 [MIG_CMD_POSTCOPY_LISTEN] = { .len = 0, .name = "POSTCOPY_LISTEN" },
107 [MIG_CMD_POSTCOPY_RUN] = { .len = 0, .name = "POSTCOPY_RUN" },
108 [MIG_CMD_POSTCOPY_RAM_DISCARD] = {
109 .len = -1, .name = "POSTCOPY_RAM_DISCARD" },
Peter Xu3f5875e2018-05-02 18:47:29 +0800110 [MIG_CMD_POSTCOPY_RESUME] = { .len = 0, .name = "POSTCOPY_RESUME" },
Dr. David Alan Gilbert11cf1d92015-11-05 18:10:53 +0000111 [MIG_CMD_PACKAGED] = { .len = 4, .name = "PACKAGED" },
Peter Xuf25d4222018-05-02 18:47:27 +0800112 [MIG_CMD_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
Dr. David Alan Gilbertc76ca182015-11-05 18:10:45 +0000113 [MIG_CMD_MAX] = { .len = -1, .name = "MAX" },
114};
115
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +0300116/* Note for MIG_CMD_POSTCOPY_ADVISE:
117 * The format of arguments is depending on postcopy mode:
118 * - postcopy RAM only
119 * uint64_t host page size
Michael Tokarevd8b71d92023-07-14 14:32:41 +0300120 * uint64_t target page size
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +0300121 *
122 * - postcopy RAM and postcopy dirty bitmaps
123 * format is the same as for postcopy RAM only
124 *
125 * - postcopy dirty bitmaps only
126 * Nothing. Command length field is 0.
127 *
128 * Be careful: adding a new postcopy entity with some other parameters should
129 * not break format self-description ability. Good way is to introduce some
130 * generic extendable format with an exception for two old entities.
131 */
132
aliguoria672b462008-11-11 21:33:36 +0000133/***********************************************************/
134/* savevm/loadvm support */
135
Christoph Hellwig45566e92009-07-10 23:11:57 +0200136static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000137{
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200138 if (is_writable) {
Daniel P. Berrangé77ef2dc2022-06-20 12:02:05 +0100139 return qemu_file_new_output(QIO_CHANNEL(qio_channel_block_new(bs)));
Daniel P. Berrangé67bdabe2022-06-20 12:01:54 +0100140 } else {
Daniel P. Berrangé77ef2dc2022-06-20 12:02:05 +0100141 return qemu_file_new_input(QIO_CHANNEL(qio_channel_block_new(bs)));
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200142 }
aliguoria672b462008-11-11 21:33:36 +0000143}
144
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200145
Eduardo Habkostbb1a6d82013-11-29 12:26:02 -0200146/* QEMUFile timer support.
147 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
148 */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200149
Alex Bligh40daca52013-08-21 16:03:02 +0100150void timer_put(QEMUFile *f, QEMUTimer *ts)
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200151{
152 uint64_t expire_time;
153
Alex Blighe93379b2013-08-21 16:02:39 +0100154 expire_time = timer_expire_time_ns(ts);
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200155 qemu_put_be64(f, expire_time);
156}
157
Alex Bligh40daca52013-08-21 16:03:02 +0100158void timer_get(QEMUFile *f, QEMUTimer *ts)
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200159{
160 uint64_t expire_time;
161
162 expire_time = qemu_get_be64(f);
163 if (expire_time != -1) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100164 timer_mod_ns(ts, expire_time);
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200165 } else {
Alex Blighbc72ad62013-08-21 16:03:08 +0100166 timer_del(ts);
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200167 }
168}
169
170
Eduardo Habkostbb1a6d82013-11-29 12:26:02 -0200171/* VMState timer support.
172 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
173 */
Juan Quinteladde04632009-08-20 19:42:26 +0200174
Marc-André Lureau03fee662018-11-14 17:29:30 +0400175static int get_timer(QEMUFile *f, void *pv, size_t size,
176 const VMStateField *field)
Juan Quinteladde04632009-08-20 19:42:26 +0200177{
178 QEMUTimer *v = pv;
Alex Bligh40daca52013-08-21 16:03:02 +0100179 timer_get(f, v);
Juan Quinteladde04632009-08-20 19:42:26 +0200180 return 0;
181}
182
Marc-André Lureau03fee662018-11-14 17:29:30 +0400183static int put_timer(QEMUFile *f, void *pv, size_t size,
Markus Armbruster3ddba9a2020-12-11 18:11:48 +0100184 const VMStateField *field, JSONWriter *vmdesc)
Juan Quinteladde04632009-08-20 19:42:26 +0200185{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200186 QEMUTimer *v = pv;
Alex Bligh40daca52013-08-21 16:03:02 +0100187 timer_put(f, v);
Jianjun Duan2c21ee72017-01-19 11:00:50 -0800188
189 return 0;
Juan Quinteladde04632009-08-20 19:42:26 +0200190}
191
192const VMStateInfo vmstate_info_timer = {
193 .name = "timer",
194 .get = get_timer,
195 .put = put_timer,
196};
197
Peter Maydell08e99e22012-10-30 07:45:12 +0000198
Alex Williamson7685ee62010-06-25 11:09:14 -0600199typedef struct CompatEntry {
200 char idstr[256];
201 int instance_id;
202} CompatEntry;
203
aliguoria672b462008-11-11 21:33:36 +0000204typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000205 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +0000206 char idstr[256];
Peter Xu93062e22019-10-16 10:29:31 +0800207 uint32_t instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200208 int alias_id;
aliguoria672b462008-11-11 21:33:36 +0000209 int version_id;
Juan Quintela0f42f652017-05-24 09:09:58 +0200210 /* version id read from the stream */
211 int load_version_id;
aliguoria672b462008-11-11 21:33:36 +0000212 int section_id;
Juan Quintela0f42f652017-05-24 09:09:58 +0200213 /* section id read from the stream */
214 int load_section_id;
Marc-André Lureaude22ded2018-11-14 17:31:39 +0400215 const SaveVMHandlers *ops;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200216 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +0000217 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -0600218 CompatEntry *compat;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +0000219 int is_ram;
aliguoria672b462008-11-11 21:33:36 +0000220} SaveStateEntry;
221
Juan Quintela0163a2e2015-05-13 13:37:04 +0200222typedef struct SaveState {
223 QTAILQ_HEAD(, SaveStateEntry) handlers;
Scott Cheloha174723f2019-10-17 15:59:53 -0500224 SaveStateEntry *handler_pri_head[MIG_PRI_MAX + 1];
Juan Quintela0163a2e2015-05-13 13:37:04 +0200225 int global_section_id;
Juan Quintela61964c22015-05-13 18:17:43 +0200226 uint32_t len;
227 const char *name;
Peter Maydell59811a32016-10-24 16:26:50 +0100228 uint32_t target_page_bits;
Yury Kotov6cafc8e2019-02-15 20:45:48 +0300229 uint32_t caps_count;
230 MigrationCapability *capabilities;
Yury Kotovb9d68df2019-09-03 19:22:44 +0300231 QemuUUID uuid;
Juan Quintela0163a2e2015-05-13 13:37:04 +0200232} SaveState;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200233
Juan Quintela0163a2e2015-05-13 13:37:04 +0200234static SaveState savevm_state = {
235 .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers),
Scott Cheloha174723f2019-10-17 15:59:53 -0500236 .handler_pri_head = { [MIG_PRI_DEFAULT ... MIG_PRI_MAX] = NULL },
Juan Quintela0163a2e2015-05-13 13:37:04 +0200237 .global_section_id = 0,
Juan Quintela61964c22015-05-13 18:17:43 +0200238};
239
Yury Kotov6cafc8e2019-02-15 20:45:48 +0300240static bool should_validate_capability(int capability)
241{
242 assert(capability >= 0 && capability < MIGRATION_CAPABILITY__MAX);
243 /* Validate only new capabilities to keep compatibility. */
244 switch (capability) {
245 case MIGRATION_CAPABILITY_X_IGNORE_SHARED:
246 return true;
247 default:
248 return false;
249 }
250}
251
252static uint32_t get_validatable_capabilities_count(void)
253{
254 MigrationState *s = migrate_get_current();
255 uint32_t result = 0;
256 int i;
257 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Juan Quintela0cec2052023-03-01 18:26:59 +0100258 if (should_validate_capability(i) && s->capabilities[i]) {
Yury Kotov6cafc8e2019-02-15 20:45:48 +0300259 result++;
260 }
261 }
262 return result;
263}
264
Dr. David Alan Gilbert44b1ff32017-09-25 12:29:12 +0100265static int configuration_pre_save(void *opaque)
Juan Quintela61964c22015-05-13 18:17:43 +0200266{
267 SaveState *state = opaque;
268 const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
Yury Kotov6cafc8e2019-02-15 20:45:48 +0300269 MigrationState *s = migrate_get_current();
270 int i, j;
Juan Quintela61964c22015-05-13 18:17:43 +0200271
272 state->len = strlen(current_name);
273 state->name = current_name;
Juan Quintela46d702b2017-04-24 21:03:48 +0200274 state->target_page_bits = qemu_target_page_bits();
Dr. David Alan Gilbert44b1ff32017-09-25 12:29:12 +0100275
Yury Kotov6cafc8e2019-02-15 20:45:48 +0300276 state->caps_count = get_validatable_capabilities_count();
277 state->capabilities = g_renew(MigrationCapability, state->capabilities,
278 state->caps_count);
279 for (i = j = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Juan Quintela0cec2052023-03-01 18:26:59 +0100280 if (should_validate_capability(i) && s->capabilities[i]) {
Yury Kotov6cafc8e2019-02-15 20:45:48 +0300281 state->capabilities[j++] = i;
282 }
283 }
Yury Kotovb9d68df2019-09-03 19:22:44 +0300284 state->uuid = qemu_uuid;
Yury Kotov6cafc8e2019-02-15 20:45:48 +0300285
Dr. David Alan Gilbert44b1ff32017-09-25 12:29:12 +0100286 return 0;
Peter Maydell59811a32016-10-24 16:26:50 +0100287}
288
Jinhao Gao39f633d2020-12-31 14:10:19 +0800289static int configuration_post_save(void *opaque)
290{
291 SaveState *state = opaque;
292
293 g_free(state->capabilities);
294 state->capabilities = NULL;
295 state->caps_count = 0;
296 return 0;
297}
298
Peter Maydell59811a32016-10-24 16:26:50 +0100299static int configuration_pre_load(void *opaque)
300{
301 SaveState *state = opaque;
302
303 /* If there is no target-page-bits subsection it means the source
304 * predates the variable-target-page-bits support and is using the
305 * minimum possible value for this CPU.
306 */
Juan Quintela46d702b2017-04-24 21:03:48 +0200307 state->target_page_bits = qemu_target_page_bits_min();
Peter Maydell59811a32016-10-24 16:26:50 +0100308 return 0;
Juan Quintela61964c22015-05-13 18:17:43 +0200309}
310
Yury Kotov6cafc8e2019-02-15 20:45:48 +0300311static bool configuration_validate_capabilities(SaveState *state)
312{
313 bool ret = true;
314 MigrationState *s = migrate_get_current();
315 unsigned long *source_caps_bm;
316 int i;
317
318 source_caps_bm = bitmap_new(MIGRATION_CAPABILITY__MAX);
319 for (i = 0; i < state->caps_count; i++) {
320 MigrationCapability capability = state->capabilities[i];
321 set_bit(capability, source_caps_bm);
322 }
323
324 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
325 bool source_state, target_state;
326 if (!should_validate_capability(i)) {
327 continue;
328 }
329 source_state = test_bit(i, source_caps_bm);
Juan Quintela0cec2052023-03-01 18:26:59 +0100330 target_state = s->capabilities[i];
Yury Kotov6cafc8e2019-02-15 20:45:48 +0300331 if (source_state != target_state) {
332 error_report("Capability %s is %s, but received capability is %s",
333 MigrationCapability_str(i),
334 target_state ? "on" : "off",
335 source_state ? "on" : "off");
336 ret = false;
337 /* Don't break here to report all failed capabilities */
338 }
339 }
340
341 g_free(source_caps_bm);
342 return ret;
343}
344
Juan Quintela61964c22015-05-13 18:17:43 +0200345static int configuration_post_load(void *opaque, int version_id)
346{
347 SaveState *state = opaque;
348 const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
Jinhao Gao39f633d2020-12-31 14:10:19 +0800349 int ret = 0;
Juan Quintela61964c22015-05-13 18:17:43 +0200350
351 if (strncmp(state->name, current_name, state->len) != 0) {
Greg Kurz15d61692016-02-05 09:33:26 +0100352 error_report("Machine type received is '%.*s' and local is '%s'",
353 (int) state->len, state->name, current_name);
Jinhao Gao39f633d2020-12-31 14:10:19 +0800354 ret = -EINVAL;
355 goto out;
Juan Quintela61964c22015-05-13 18:17:43 +0200356 }
Peter Maydell59811a32016-10-24 16:26:50 +0100357
Juan Quintela46d702b2017-04-24 21:03:48 +0200358 if (state->target_page_bits != qemu_target_page_bits()) {
Peter Maydell59811a32016-10-24 16:26:50 +0100359 error_report("Received TARGET_PAGE_BITS is %d but local is %d",
Juan Quintela46d702b2017-04-24 21:03:48 +0200360 state->target_page_bits, qemu_target_page_bits());
Jinhao Gao39f633d2020-12-31 14:10:19 +0800361 ret = -EINVAL;
362 goto out;
Peter Maydell59811a32016-10-24 16:26:50 +0100363 }
364
Yury Kotov6cafc8e2019-02-15 20:45:48 +0300365 if (!configuration_validate_capabilities(state)) {
Jinhao Gao39f633d2020-12-31 14:10:19 +0800366 ret = -EINVAL;
367 goto out;
Yury Kotov6cafc8e2019-02-15 20:45:48 +0300368 }
369
Jinhao Gao39f633d2020-12-31 14:10:19 +0800370out:
371 g_free((void *)state->name);
372 state->name = NULL;
373 state->len = 0;
374 g_free(state->capabilities);
375 state->capabilities = NULL;
376 state->caps_count = 0;
377
378 return ret;
Juan Quintela61964c22015-05-13 18:17:43 +0200379}
380
Yury Kotov6cafc8e2019-02-15 20:45:48 +0300381static int get_capability(QEMUFile *f, void *pv, size_t size,
382 const VMStateField *field)
383{
384 MigrationCapability *capability = pv;
385 char capability_str[UINT8_MAX + 1];
386 uint8_t len;
387 int i;
388
389 len = qemu_get_byte(f);
390 qemu_get_buffer(f, (uint8_t *)capability_str, len);
391 capability_str[len] = '\0';
392 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
393 if (!strcmp(MigrationCapability_str(i), capability_str)) {
394 *capability = i;
395 return 0;
396 }
397 }
398 error_report("Received unknown capability %s", capability_str);
399 return -EINVAL;
400}
401
402static int put_capability(QEMUFile *f, void *pv, size_t size,
Markus Armbruster3ddba9a2020-12-11 18:11:48 +0100403 const VMStateField *field, JSONWriter *vmdesc)
Yury Kotov6cafc8e2019-02-15 20:45:48 +0300404{
405 MigrationCapability *capability = pv;
406 const char *capability_str = MigrationCapability_str(*capability);
407 size_t len = strlen(capability_str);
408 assert(len <= UINT8_MAX);
409
410 qemu_put_byte(f, len);
411 qemu_put_buffer(f, (uint8_t *)capability_str, len);
412 return 0;
413}
414
415static const VMStateInfo vmstate_info_capability = {
416 .name = "capability",
417 .get = get_capability,
418 .put = put_capability,
419};
420
Peter Maydell59811a32016-10-24 16:26:50 +0100421/* The target-page-bits subsection is present only if the
422 * target page size is not the same as the default (ie the
423 * minimum page size for a variable-page-size guest CPU).
424 * If it is present then it contains the actual target page
425 * bits for the machine, and migration will fail if the
426 * two ends don't agree about it.
427 */
428static bool vmstate_target_page_bits_needed(void *opaque)
429{
Juan Quintela46d702b2017-04-24 21:03:48 +0200430 return qemu_target_page_bits()
431 > qemu_target_page_bits_min();
Peter Maydell59811a32016-10-24 16:26:50 +0100432}
433
434static const VMStateDescription vmstate_target_page_bits = {
435 .name = "configuration/target-page-bits",
436 .version_id = 1,
437 .minimum_version_id = 1,
438 .needed = vmstate_target_page_bits_needed,
439 .fields = (VMStateField[]) {
440 VMSTATE_UINT32(target_page_bits, SaveState),
441 VMSTATE_END_OF_LIST()
442 }
443};
444
Yury Kotov6cafc8e2019-02-15 20:45:48 +0300445static bool vmstate_capabilites_needed(void *opaque)
446{
447 return get_validatable_capabilities_count() > 0;
448}
449
450static const VMStateDescription vmstate_capabilites = {
451 .name = "configuration/capabilities",
452 .version_id = 1,
453 .minimum_version_id = 1,
454 .needed = vmstate_capabilites_needed,
455 .fields = (VMStateField[]) {
456 VMSTATE_UINT32_V(caps_count, SaveState, 1),
457 VMSTATE_VARRAY_UINT32_ALLOC(capabilities, SaveState, caps_count, 1,
458 vmstate_info_capability,
459 MigrationCapability),
460 VMSTATE_END_OF_LIST()
461 }
462};
463
Yury Kotovb9d68df2019-09-03 19:22:44 +0300464static bool vmstate_uuid_needed(void *opaque)
465{
466 return qemu_uuid_set && migrate_validate_uuid();
467}
468
469static int vmstate_uuid_post_load(void *opaque, int version_id)
470{
471 SaveState *state = opaque;
472 char uuid_src[UUID_FMT_LEN + 1];
473 char uuid_dst[UUID_FMT_LEN + 1];
474
475 if (!qemu_uuid_set) {
476 /*
477 * It's warning because user might not know UUID in some cases,
478 * e.g. load an old snapshot
479 */
480 qemu_uuid_unparse(&state->uuid, uuid_src);
481 warn_report("UUID is received %s, but local uuid isn't set",
482 uuid_src);
483 return 0;
484 }
485 if (!qemu_uuid_is_equal(&state->uuid, &qemu_uuid)) {
486 qemu_uuid_unparse(&state->uuid, uuid_src);
487 qemu_uuid_unparse(&qemu_uuid, uuid_dst);
488 error_report("UUID received is %s and local is %s", uuid_src, uuid_dst);
489 return -EINVAL;
490 }
491 return 0;
492}
493
494static const VMStateDescription vmstate_uuid = {
495 .name = "configuration/uuid",
496 .version_id = 1,
497 .minimum_version_id = 1,
498 .needed = vmstate_uuid_needed,
499 .post_load = vmstate_uuid_post_load,
500 .fields = (VMStateField[]) {
501 VMSTATE_UINT8_ARRAY_V(uuid.data, SaveState, sizeof(QemuUUID), 1),
502 VMSTATE_END_OF_LIST()
503 }
504};
505
Juan Quintela61964c22015-05-13 18:17:43 +0200506static const VMStateDescription vmstate_configuration = {
507 .name = "configuration",
508 .version_id = 1,
Peter Maydell59811a32016-10-24 16:26:50 +0100509 .pre_load = configuration_pre_load,
Juan Quintela61964c22015-05-13 18:17:43 +0200510 .post_load = configuration_post_load,
511 .pre_save = configuration_pre_save,
Jinhao Gao39f633d2020-12-31 14:10:19 +0800512 .post_save = configuration_post_save,
Juan Quintela61964c22015-05-13 18:17:43 +0200513 .fields = (VMStateField[]) {
514 VMSTATE_UINT32(len, SaveState),
Halil Pasic59046ec2017-02-03 18:52:17 +0100515 VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, len),
Juan Quintela61964c22015-05-13 18:17:43 +0200516 VMSTATE_END_OF_LIST()
517 },
Bihong Yu395cb452020-10-20 11:10:43 +0800518 .subsections = (const VMStateDescription *[]) {
Peter Maydell59811a32016-10-24 16:26:50 +0100519 &vmstate_target_page_bits,
Yury Kotov6cafc8e2019-02-15 20:45:48 +0300520 &vmstate_capabilites,
Yury Kotovb9d68df2019-09-03 19:22:44 +0300521 &vmstate_uuid,
Peter Maydell59811a32016-10-24 16:26:50 +0100522 NULL
523 }
Juan Quintela0163a2e2015-05-13 13:37:04 +0200524};
aliguoria672b462008-11-11 21:33:36 +0000525
Amit Shahabfd9ce2014-06-20 18:56:08 +0530526static void dump_vmstate_vmsd(FILE *out_file,
527 const VMStateDescription *vmsd, int indent,
528 bool is_subsection);
529
530static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field,
531 int indent)
532{
533 fprintf(out_file, "%*s{\n", indent, "");
534 indent += 2;
535 fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name);
536 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
537 field->version_id);
538 fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "",
539 field->field_exists ? "true" : "false");
Peter Xu12c81e52023-04-25 14:05:43 -0400540 if (field->flags & VMS_ARRAY) {
541 fprintf(out_file, "%*s\"num\": %d,\n", indent, "", field->num);
542 }
Amit Shahabfd9ce2014-06-20 18:56:08 +0530543 fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size);
544 if (field->vmsd != NULL) {
545 fprintf(out_file, ",\n");
546 dump_vmstate_vmsd(out_file, field->vmsd, indent, false);
547 }
548 fprintf(out_file, "\n%*s}", indent - 2, "");
549}
550
551static void dump_vmstate_vmss(FILE *out_file,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200552 const VMStateDescription **subsection,
Amit Shahabfd9ce2014-06-20 18:56:08 +0530553 int indent)
554{
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200555 if (*subsection != NULL) {
556 dump_vmstate_vmsd(out_file, *subsection, indent, true);
Amit Shahabfd9ce2014-06-20 18:56:08 +0530557 }
558}
559
560static void dump_vmstate_vmsd(FILE *out_file,
561 const VMStateDescription *vmsd, int indent,
562 bool is_subsection)
563{
564 if (is_subsection) {
565 fprintf(out_file, "%*s{\n", indent, "");
566 } else {
567 fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description");
568 }
569 indent += 2;
570 fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name);
571 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
572 vmsd->version_id);
573 fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "",
574 vmsd->minimum_version_id);
575 if (vmsd->fields != NULL) {
576 const VMStateField *field = vmsd->fields;
577 bool first;
578
579 fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, "");
580 first = true;
581 while (field->name != NULL) {
582 if (field->flags & VMS_MUST_EXIST) {
583 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
584 field++;
585 continue;
586 }
587 if (!first) {
588 fprintf(out_file, ",\n");
589 }
590 dump_vmstate_vmsf(out_file, field, indent + 2);
591 field++;
592 first = false;
593 }
Dr. David Alan Gilbert89c56842022-01-13 19:44:51 +0000594 assert(field->flags == VMS_END);
Amit Shahabfd9ce2014-06-20 18:56:08 +0530595 fprintf(out_file, "\n%*s]", indent, "");
596 }
597 if (vmsd->subsections != NULL) {
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200598 const VMStateDescription **subsection = vmsd->subsections;
Amit Shahabfd9ce2014-06-20 18:56:08 +0530599 bool first;
600
601 fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, "");
602 first = true;
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200603 while (*subsection != NULL) {
Amit Shahabfd9ce2014-06-20 18:56:08 +0530604 if (!first) {
605 fprintf(out_file, ",\n");
606 }
607 dump_vmstate_vmss(out_file, subsection, indent + 2);
608 subsection++;
609 first = false;
610 }
611 fprintf(out_file, "\n%*s]", indent, "");
612 }
613 fprintf(out_file, "\n%*s}", indent - 2, "");
614}
615
616static void dump_machine_type(FILE *out_file)
617{
618 MachineClass *mc;
619
620 mc = MACHINE_GET_CLASS(current_machine);
621
622 fprintf(out_file, " \"vmschkmachine\": {\n");
623 fprintf(out_file, " \"Name\": \"%s\"\n", mc->name);
624 fprintf(out_file, " },\n");
625}
626
627void dump_vmstate_json_to_file(FILE *out_file)
628{
629 GSList *list, *elt;
630 bool first;
631
632 fprintf(out_file, "{\n");
633 dump_machine_type(out_file);
634
635 first = true;
636 list = object_class_get_list(TYPE_DEVICE, true);
637 for (elt = list; elt; elt = elt->next) {
638 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
639 TYPE_DEVICE);
640 const char *name;
641 int indent = 2;
642
643 if (!dc->vmsd) {
644 continue;
645 }
646
647 if (!first) {
648 fprintf(out_file, ",\n");
649 }
650 name = object_class_get_name(OBJECT_CLASS(dc));
651 fprintf(out_file, "%*s\"%s\": {\n", indent, "", name);
652 indent += 2;
653 fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name);
654 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
655 dc->vmsd->version_id);
656 fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "",
657 dc->vmsd->minimum_version_id);
658
659 dump_vmstate_vmsd(out_file, dc->vmsd, indent, false);
660
661 fprintf(out_file, "\n%*s}", indent - 2, "");
662 first = false;
663 }
664 fprintf(out_file, "\n}\n");
665 fclose(out_file);
Pan Nengyuan26daeba2020-02-19 17:47:05 +0800666 g_slist_free(list);
Amit Shahabfd9ce2014-06-20 18:56:08 +0530667}
668
Peter Xu93062e22019-10-16 10:29:31 +0800669static uint32_t calculate_new_instance_id(const char *idstr)
Juan Quintela8718e992009-09-01 02:12:31 +0200670{
671 SaveStateEntry *se;
Peter Xu93062e22019-10-16 10:29:31 +0800672 uint32_t instance_id = 0;
Juan Quintela8718e992009-09-01 02:12:31 +0200673
Juan Quintela0163a2e2015-05-13 13:37:04 +0200674 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +0200675 if (strcmp(idstr, se->idstr) == 0
676 && instance_id <= se->instance_id) {
677 instance_id = se->instance_id + 1;
678 }
679 }
Peter Xu93062e22019-10-16 10:29:31 +0800680 /* Make sure we never loop over without being noticed */
681 assert(instance_id != VMSTATE_INSTANCE_ID_ANY);
Juan Quintela8718e992009-09-01 02:12:31 +0200682 return instance_id;
683}
684
Alex Williamson7685ee62010-06-25 11:09:14 -0600685static int calculate_compat_instance_id(const char *idstr)
686{
687 SaveStateEntry *se;
688 int instance_id = 0;
689
Juan Quintela0163a2e2015-05-13 13:37:04 +0200690 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200691 if (!se->compat) {
Alex Williamson7685ee62010-06-25 11:09:14 -0600692 continue;
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200693 }
Alex Williamson7685ee62010-06-25 11:09:14 -0600694
695 if (strcmp(idstr, se->compat->idstr) == 0
696 && instance_id <= se->compat->instance_id) {
697 instance_id = se->compat->instance_id + 1;
698 }
699 }
700 return instance_id;
701}
702
Peter Xuf37bc032017-01-06 12:06:12 +0800703static inline MigrationPriority save_state_priority(SaveStateEntry *se)
704{
705 if (se->vmsd) {
706 return se->vmsd->priority;
707 }
708 return MIG_PRI_DEFAULT;
709}
710
711static void savevm_state_handler_insert(SaveStateEntry *nse)
712{
713 MigrationPriority priority = save_state_priority(nse);
714 SaveStateEntry *se;
Scott Cheloha174723f2019-10-17 15:59:53 -0500715 int i;
Peter Xuf37bc032017-01-06 12:06:12 +0800716
717 assert(priority <= MIG_PRI_MAX);
718
Scott Cheloha174723f2019-10-17 15:59:53 -0500719 for (i = priority - 1; i >= 0; i--) {
720 se = savevm_state.handler_pri_head[i];
721 if (se != NULL) {
722 assert(save_state_priority(se) < priority);
Peter Xuf37bc032017-01-06 12:06:12 +0800723 break;
724 }
725 }
726
Scott Cheloha174723f2019-10-17 15:59:53 -0500727 if (i >= 0) {
Peter Xuf37bc032017-01-06 12:06:12 +0800728 QTAILQ_INSERT_BEFORE(se, nse, entry);
729 } else {
730 QTAILQ_INSERT_TAIL(&savevm_state.handlers, nse, entry);
731 }
Scott Cheloha174723f2019-10-17 15:59:53 -0500732
733 if (savevm_state.handler_pri_head[priority] == NULL) {
734 savevm_state.handler_pri_head[priority] = nse;
735 }
Peter Xuf37bc032017-01-06 12:06:12 +0800736}
737
Scott Chelohabd5de612019-10-17 15:59:52 -0500738static void savevm_state_handler_remove(SaveStateEntry *se)
739{
Scott Cheloha174723f2019-10-17 15:59:53 -0500740 SaveStateEntry *next;
741 MigrationPriority priority = save_state_priority(se);
742
743 if (se == savevm_state.handler_pri_head[priority]) {
744 next = QTAILQ_NEXT(se, entry);
745 if (next != NULL && save_state_priority(next) == priority) {
746 savevm_state.handler_pri_head[priority] = next;
747 } else {
748 savevm_state.handler_pri_head[priority] = NULL;
749 }
750 }
Scott Chelohabd5de612019-10-17 15:59:52 -0500751 QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
752}
753
aliguoria672b462008-11-11 21:33:36 +0000754/* TODO: Individual devices generally have very little idea about the rest
755 of the system, so instance_id should be removed/replaced.
756 Meanwhile pass -1 as instance_id if you do not already have a clearly
757 distinguishing id for all instances of your device class. */
Dr. David Alan Gilbertce62df52019-08-22 12:54:33 +0100758int register_savevm_live(const char *idstr,
Peter Xu93062e22019-10-16 10:29:31 +0800759 uint32_t instance_id,
aliguoria672b462008-11-11 21:33:36 +0000760 int version_id,
Marc-André Lureaude22ded2018-11-14 17:31:39 +0400761 const SaveVMHandlers *ops,
aliguoria672b462008-11-11 21:33:36 +0000762 void *opaque)
763{
Juan Quintela8718e992009-09-01 02:12:31 +0200764 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +0000765
Markus Armbruster97f3ad32015-09-14 13:51:31 +0200766 se = g_new0(SaveStateEntry, 1);
aliguoria672b462008-11-11 21:33:36 +0000767 se->version_id = version_id;
Juan Quintela0163a2e2015-05-13 13:37:04 +0200768 se->section_id = savevm_state.global_section_id++;
Juan Quintela7908c782012-06-26 18:46:10 +0200769 se->ops = ops;
aliguoria672b462008-11-11 21:33:36 +0000770 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200771 se->vmsd = NULL;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +0000772 /* if this is a live_savem then set is_ram */
Juan Quintela9907e842017-06-28 11:52:24 +0200773 if (ops->save_setup != NULL) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +0000774 se->is_ram = 1;
775 }
aliguoria672b462008-11-11 21:33:36 +0000776
Alex Williamson7685ee62010-06-25 11:09:14 -0600777 pstrcat(se->idstr, sizeof(se->idstr), idstr);
778
Peter Xu1df2c9a2019-10-16 10:29:30 +0800779 if (instance_id == VMSTATE_INSTANCE_ID_ANY) {
Alex Williamson7685ee62010-06-25 11:09:14 -0600780 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +0200781 } else {
782 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +0000783 }
Alex Williamson7685ee62010-06-25 11:09:14 -0600784 assert(!se->compat || se->instance_id == 0);
Peter Xuf37bc032017-01-06 12:06:12 +0800785 savevm_state_handler_insert(se);
aliguoria672b462008-11-11 21:33:36 +0000786 return 0;
787}
788
Marc-André Lureau3cad4052019-08-28 16:02:32 +0400789void unregister_savevm(VMStateIf *obj, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +0000790{
Juan Quintela8718e992009-09-01 02:12:31 +0200791 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -0600792 char id[256] = "";
793
Marc-André Lureau3cad4052019-08-28 16:02:32 +0400794 if (obj) {
795 char *oid = vmstate_if_get_id(obj);
796 if (oid) {
797 pstrcpy(id, sizeof(id), oid);
Alex Williamson7685ee62010-06-25 11:09:14 -0600798 pstrcat(id, sizeof(id), "/");
Marc-André Lureau3cad4052019-08-28 16:02:32 +0400799 g_free(oid);
Alex Williamson7685ee62010-06-25 11:09:14 -0600800 }
801 }
802 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +0000803
Juan Quintela0163a2e2015-05-13 13:37:04 +0200804 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -0600805 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Scott Chelohabd5de612019-10-17 15:59:52 -0500806 savevm_state_handler_remove(se);
Daniel P. Berrangeef1e1e02015-08-26 12:17:18 +0100807 g_free(se->compat);
Anthony Liguori7267c092011-08-20 22:09:37 -0500808 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +0000809 }
aliguori41bd13a2009-04-17 17:10:59 +0000810 }
811}
812
Dr. David Alan Gilbertbb25a722022-01-13 19:44:52 +0000813/*
814 * Perform some basic checks on vmsd's at registration
815 * time.
816 */
817static void vmstate_check(const VMStateDescription *vmsd)
818{
819 const VMStateField *field = vmsd->fields;
820 const VMStateDescription **subsection = vmsd->subsections;
821
822 if (field) {
823 while (field->name) {
824 if (field->flags & (VMS_STRUCT | VMS_VSTRUCT)) {
825 /* Recurse to sub structures */
826 vmstate_check(field->vmsd);
827 }
828 /* Carry on */
829 field++;
830 }
831 /* Check for the end of field list canary */
832 if (field->flags != VMS_END) {
833 error_report("VMSTATE not ending with VMS_END: %s", vmsd->name);
834 g_assert_not_reached();
835 }
836 }
837
838 while (subsection && *subsection) {
839 /*
840 * The name of a subsection should start with the name of the
841 * current object.
842 */
843 assert(!strncmp(vmsd->name, (*subsection)->name, strlen(vmsd->name)));
844 vmstate_check(*subsection);
845 subsection++;
846 }
847}
848
Peter Xu93062e22019-10-16 10:29:31 +0800849int vmstate_register_with_alias_id(VMStateIf *obj, uint32_t instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200850 const VMStateDescription *vmsd,
851 void *opaque, int alias_id,
Dr. David Alan Gilbertbc5c4f22017-02-02 12:59:54 +0000852 int required_for_version,
853 Error **errp)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200854{
Juan Quintela8718e992009-09-01 02:12:31 +0200855 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200856
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200857 /* If this triggers, alias support can be dropped for the vmsd. */
858 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
859
Markus Armbruster97f3ad32015-09-14 13:51:31 +0200860 se = g_new0(SaveStateEntry, 1);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200861 se->version_id = vmsd->version_id;
Juan Quintela0163a2e2015-05-13 13:37:04 +0200862 se->section_id = savevm_state.global_section_id++;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200863 se->opaque = opaque;
864 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +0200865 se->alias_id = alias_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200866
Marc-André Lureau3cad4052019-08-28 16:02:32 +0400867 if (obj) {
868 char *id = vmstate_if_get_id(obj);
Alex Williamson7685ee62010-06-25 11:09:14 -0600869 if (id) {
Dr. David Alan Gilbert581f08b2017-02-02 12:59:55 +0000870 if (snprintf(se->idstr, sizeof(se->idstr), "%s/", id) >=
871 sizeof(se->idstr)) {
872 error_setg(errp, "Path too long for VMState (%s)", id);
873 g_free(id);
874 g_free(se);
875
876 return -1;
877 }
Marc-André Lureau128e4e12017-02-21 18:14:51 +0400878 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -0600879
Markus Armbruster97f3ad32015-09-14 13:51:31 +0200880 se->compat = g_new0(CompatEntry, 1);
Alex Williamson7685ee62010-06-25 11:09:14 -0600881 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
Peter Xu1df2c9a2019-10-16 10:29:30 +0800882 se->compat->instance_id = instance_id == VMSTATE_INSTANCE_ID_ANY ?
Alex Williamson7685ee62010-06-25 11:09:14 -0600883 calculate_compat_instance_id(vmsd->name) : instance_id;
Peter Xu1df2c9a2019-10-16 10:29:30 +0800884 instance_id = VMSTATE_INSTANCE_ID_ANY;
Alex Williamson7685ee62010-06-25 11:09:14 -0600885 }
886 }
887 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
888
Peter Xu1df2c9a2019-10-16 10:29:30 +0800889 if (instance_id == VMSTATE_INSTANCE_ID_ANY) {
Alex Williamson7685ee62010-06-25 11:09:14 -0600890 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +0200891 } else {
892 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200893 }
Dr. David Alan Gilbertbb25a722022-01-13 19:44:52 +0000894
895 /* Perform a recursive sanity check during the test runs */
896 if (qtest_enabled()) {
897 vmstate_check(vmsd);
898 }
Alex Williamson7685ee62010-06-25 11:09:14 -0600899 assert(!se->compat || se->instance_id == 0);
Peter Xuf37bc032017-01-06 12:06:12 +0800900 savevm_state_handler_insert(se);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200901 return 0;
902}
903
Marc-André Lureau3cad4052019-08-28 16:02:32 +0400904void vmstate_unregister(VMStateIf *obj, const VMStateDescription *vmsd,
Alex Williamson0be71e32010-06-25 11:09:07 -0600905 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200906{
Juan Quintela1eb75382009-09-10 03:04:29 +0200907 SaveStateEntry *se, *new_se;
908
Juan Quintela0163a2e2015-05-13 13:37:04 +0200909 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +0200910 if (se->vmsd == vmsd && se->opaque == opaque) {
Scott Chelohabd5de612019-10-17 15:59:52 -0500911 savevm_state_handler_remove(se);
Daniel P. Berrangeef1e1e02015-08-26 12:17:18 +0100912 g_free(se->compat);
Anthony Liguori7267c092011-08-20 22:09:37 -0500913 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +0200914 }
915 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200916}
917
Juan Quintela3a011c22017-05-24 09:28:47 +0200918static int vmstate_load(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +0200919{
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100920 trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200921 if (!se->vmsd) { /* Old style */
Juan Quintela3a011c22017-05-24 09:28:47 +0200922 return se->ops->load_state(f, se->opaque, se->load_version_id);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200923 }
Juan Quintela3a011c22017-05-24 09:28:47 +0200924 return vmstate_load_state(f, se->vmsd, se->opaque, se->load_version_id);
Juan Quintela4082be42009-08-20 19:42:24 +0200925}
926
Markus Armbruster3ddba9a2020-12-11 18:11:48 +0100927static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se,
928 JSONWriter *vmdesc)
Alexander Graf8118f092015-01-22 15:01:39 +0100929{
Juan Quintelafc95c632023-05-30 20:39:22 +0200930 uint64_t old_offset = qemu_file_transferred_noflush(f);
Alexander Graf8118f092015-01-22 15:01:39 +0100931 se->ops->save_state(f, se->opaque);
Juan Quintelafc95c632023-05-30 20:39:22 +0200932 uint64_t size = qemu_file_transferred_noflush(f) - old_offset;
Alexander Graf8118f092015-01-22 15:01:39 +0100933
934 if (vmdesc) {
Markus Armbruster3ddba9a2020-12-11 18:11:48 +0100935 json_writer_int64(vmdesc, "size", size);
936 json_writer_start_array(vmdesc, "fields");
937 json_writer_start_object(vmdesc, NULL);
938 json_writer_str(vmdesc, "name", "data");
939 json_writer_int64(vmdesc, "size", size);
940 json_writer_str(vmdesc, "type", "buffer");
941 json_writer_end_object(vmdesc);
942 json_writer_end_array(vmdesc);
Alexander Graf8118f092015-01-22 15:01:39 +0100943 }
944}
945
Dr. David Alan Gilbertce39bfc2015-05-19 12:29:50 +0100946/*
947 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
948 */
949static void save_section_header(QEMUFile *f, SaveStateEntry *se,
950 uint8_t section_type)
951{
952 qemu_put_byte(f, section_type);
953 qemu_put_be32(f, se->section_id);
954
955 if (section_type == QEMU_VM_SECTION_FULL ||
956 section_type == QEMU_VM_SECTION_START) {
957 /* ID string */
958 size_t len = strlen(se->idstr);
959 qemu_put_byte(f, len);
960 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
961
962 qemu_put_be32(f, se->instance_id);
963 qemu_put_be32(f, se->version_id);
964 }
965}
966
Dr. David Alan Gilbertf68945d2015-05-19 12:29:52 +0100967/*
968 * Write a footer onto device sections that catches cases misformatted device
969 * sections.
970 */
971static void save_section_footer(QEMUFile *f, SaveStateEntry *se)
972{
Peter Xu15c38502017-06-27 12:10:17 +0800973 if (migrate_get_current()->send_section_footer) {
Dr. David Alan Gilbertf68945d2015-05-19 12:29:52 +0100974 qemu_put_byte(f, QEMU_VM_SECTION_FOOTER);
975 qemu_put_be32(f, se->section_id);
976 }
977}
978
David Hildenbrand5e104f22023-01-17 12:22:42 +0100979static int vmstate_save(QEMUFile *f, SaveStateEntry *se, JSONWriter *vmdesc)
980{
981 int ret;
982
983 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
984 return 0;
985 }
986 if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
987 trace_savevm_section_skip(se->idstr, se->section_id);
988 return 0;
989 }
990
991 trace_savevm_section_start(se->idstr, se->section_id);
992 save_section_header(f, se, QEMU_VM_SECTION_FULL);
993 if (vmdesc) {
994 json_writer_start_object(vmdesc, NULL);
995 json_writer_str(vmdesc, "name", se->idstr);
996 json_writer_int64(vmdesc, "instance_id", se->instance_id);
997 }
998
999 trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
1000 if (!se->vmsd) {
1001 vmstate_save_old_style(f, se, vmdesc);
1002 } else {
1003 ret = vmstate_save_state(f, se->vmsd, se->opaque, vmdesc);
1004 if (ret) {
1005 return ret;
1006 }
1007 }
1008
1009 trace_savevm_section_end(se->idstr, se->section_id, 0);
1010 save_section_footer(f, se);
1011 if (vmdesc) {
1012 json_writer_end_object(vmdesc);
1013 }
1014 return 0;
1015}
Dr. David Alan Gilbertc76ca182015-11-05 18:10:45 +00001016/**
1017 * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the
1018 * command and associated data.
1019 *
1020 * @f: File to send command on
1021 * @command: Command type to send
1022 * @len: Length of associated data
1023 * @data: Data associated with command.
1024 */
Juan Quintela20a519a2017-04-20 14:48:46 +02001025static void qemu_savevm_command_send(QEMUFile *f,
1026 enum qemu_vm_cmd command,
1027 uint16_t len,
1028 uint8_t *data)
Dr. David Alan Gilbertc76ca182015-11-05 18:10:45 +00001029{
1030 trace_savevm_command_send(command, len);
1031 qemu_put_byte(f, QEMU_VM_COMMAND);
1032 qemu_put_be16(f, (uint16_t)command);
1033 qemu_put_be16(f, len);
1034 qemu_put_buffer(f, data, len);
1035 qemu_fflush(f);
1036}
1037
Zhang Chenaad555c2018-09-03 12:38:47 +08001038void qemu_savevm_send_colo_enable(QEMUFile *f)
1039{
1040 trace_savevm_send_colo_enable();
1041 qemu_savevm_command_send(f, MIG_CMD_ENABLE_COLO, 0, NULL);
1042}
1043
Dr. David Alan Gilbert2e377012015-11-05 18:10:46 +00001044void qemu_savevm_send_ping(QEMUFile *f, uint32_t value)
1045{
1046 uint32_t buf;
1047
1048 trace_savevm_send_ping(value);
1049 buf = cpu_to_be32(value);
1050 qemu_savevm_command_send(f, MIG_CMD_PING, sizeof(value), (uint8_t *)&buf);
1051}
1052
1053void qemu_savevm_send_open_return_path(QEMUFile *f)
1054{
1055 trace_savevm_send_open_return_path();
1056 qemu_savevm_command_send(f, MIG_CMD_OPEN_RETURN_PATH, 0, NULL);
1057}
1058
Dr. David Alan Gilbert11cf1d92015-11-05 18:10:53 +00001059/* We have a buffer of data to send; we don't want that all to be loaded
1060 * by the command itself, so the command contains just the length of the
1061 * extra buffer that we then send straight after it.
1062 * TODO: Must be a better way to organise that
1063 *
1064 * Returns:
1065 * 0 on success
1066 * -ve on error
1067 */
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001068int qemu_savevm_send_packaged(QEMUFile *f, const uint8_t *buf, size_t len)
Dr. David Alan Gilbert11cf1d92015-11-05 18:10:53 +00001069{
Dr. David Alan Gilbert11cf1d92015-11-05 18:10:53 +00001070 uint32_t tmp;
1071
1072 if (len > MAX_VM_CMD_PACKAGED_SIZE) {
1073 error_report("%s: Unreasonably large packaged state: %zu",
1074 __func__, len);
1075 return -1;
1076 }
1077
1078 tmp = cpu_to_be32(len);
1079
1080 trace_qemu_savevm_send_packaged();
1081 qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp);
1082
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01001083 qemu_put_buffer(f, buf, len);
Dr. David Alan Gilbert11cf1d92015-11-05 18:10:53 +00001084
1085 return 0;
1086}
1087
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001088/* Send prior to any postcopy transfer */
1089void qemu_savevm_send_postcopy_advise(QEMUFile *f)
1090{
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001091 if (migrate_postcopy_ram()) {
1092 uint64_t tmp[2];
1093 tmp[0] = cpu_to_be64(ram_pagesize_summary());
1094 tmp[1] = cpu_to_be64(qemu_target_page_size());
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001095
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001096 trace_qemu_savevm_send_postcopy_advise();
1097 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE,
1098 16, (uint8_t *)tmp);
1099 } else {
1100 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 0, NULL);
1101 }
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001102}
1103
1104/* Sent prior to starting the destination running in postcopy, discard pages
1105 * that have already been sent but redirtied on the source.
1106 * CMD_POSTCOPY_RAM_DISCARD consist of:
1107 * byte version (0)
1108 * byte Length of name field (not including 0)
1109 * n x byte RAM block name
1110 * byte 0 terminator (just for safety)
1111 * n x Byte ranges within the named RAMBlock
1112 * be64 Start of the range
1113 * be64 Length
1114 *
1115 * name: RAMBlock name that these entries are part of
1116 * len: Number of page entries
1117 * start_list: 'len' addresses
1118 * length_list: 'len' addresses
1119 *
1120 */
1121void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name,
1122 uint16_t len,
1123 uint64_t *start_list,
1124 uint64_t *length_list)
1125{
1126 uint8_t *buf;
1127 uint16_t tmplen;
1128 uint16_t t;
1129 size_t name_len = strlen(name);
1130
1131 trace_qemu_savevm_send_postcopy_ram_discard(name, len);
1132 assert(name_len < 256);
1133 buf = g_malloc0(1 + 1 + name_len + 1 + (8 + 8) * len);
1134 buf[0] = postcopy_ram_discard_version;
1135 buf[1] = name_len;
1136 memcpy(buf + 2, name, name_len);
1137 tmplen = 2 + name_len;
1138 buf[tmplen++] = '\0';
1139
1140 for (t = 0; t < len; t++) {
Peter Maydell4d885132016-06-10 17:09:22 +01001141 stq_be_p(buf + tmplen, start_list[t]);
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001142 tmplen += 8;
Peter Maydell4d885132016-06-10 17:09:22 +01001143 stq_be_p(buf + tmplen, length_list[t]);
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001144 tmplen += 8;
1145 }
1146 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RAM_DISCARD, tmplen, buf);
1147 g_free(buf);
1148}
1149
1150/* Get the destination into a state where it can receive postcopy data. */
1151void qemu_savevm_send_postcopy_listen(QEMUFile *f)
1152{
1153 trace_savevm_send_postcopy_listen();
1154 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_LISTEN, 0, NULL);
1155}
1156
1157/* Kick the destination into running */
1158void qemu_savevm_send_postcopy_run(QEMUFile *f)
1159{
1160 trace_savevm_send_postcopy_run();
1161 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RUN, 0, NULL);
1162}
1163
Peter Xu3f5875e2018-05-02 18:47:29 +08001164void qemu_savevm_send_postcopy_resume(QEMUFile *f)
1165{
1166 trace_savevm_send_postcopy_resume();
1167 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RESUME, 0, NULL);
1168}
1169
Peter Xuf25d4222018-05-02 18:47:27 +08001170void qemu_savevm_send_recv_bitmap(QEMUFile *f, char *block_name)
1171{
1172 size_t len;
1173 char buf[256];
1174
1175 trace_savevm_send_recv_bitmap(block_name);
1176
1177 buf[0] = len = strlen(block_name);
1178 memcpy(buf + 1, block_name, len);
1179
1180 qemu_savevm_command_send(f, MIG_CMD_RECV_BITMAP, len + 1, (uint8_t *)buf);
1181}
1182
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001183bool qemu_savevm_state_blocked(Error **errp)
Alex Williamsondc912122011-01-11 14:39:43 -07001184{
1185 SaveStateEntry *se;
1186
Juan Quintela0163a2e2015-05-13 13:37:04 +02001187 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
Marcelo Tosatti7d854c42014-05-14 16:30:08 -03001188 if (se->vmsd && se->vmsd->unmigratable) {
Cole Robinsonf231b882014-03-21 19:42:26 -04001189 error_setg(errp, "State blocked by non-migratable device '%s'",
1190 se->idstr);
Alex Williamsondc912122011-01-11 14:39:43 -07001191 return true;
1192 }
1193 }
1194 return false;
1195}
1196
Dr. David Alan Gilbert3af85542021-02-02 13:55:21 +00001197void qemu_savevm_non_migratable_list(strList **reasons)
1198{
1199 SaveStateEntry *se;
1200
1201 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1202 if (se->vmsd && se->vmsd->unmigratable) {
1203 QAPI_LIST_PREPEND(*reasons,
1204 g_strdup_printf("non-migratable device: %s",
1205 se->idstr));
1206 }
1207 }
1208}
1209
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +01001210void qemu_savevm_state_header(QEMUFile *f)
1211{
1212 trace_savevm_state_header();
1213 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1214 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
Dr. David Alan Gilbert172dfd42015-11-05 18:10:30 +00001215
Peter Xu4ffdb332017-06-27 12:10:18 +08001216 if (migrate_get_current()->send_configuration) {
Dr. David Alan Gilbert172dfd42015-11-05 18:10:30 +00001217 qemu_put_byte(f, QEMU_VM_CONFIGURATION);
1218 vmstate_save_state(f, &vmstate_configuration, &savevm_state, 0);
1219 }
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +01001220}
1221
Keqian Zhud05de9e2020-02-04 13:08:41 +08001222bool qemu_savevm_state_guest_unplug_pending(void)
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001223{
1224 SaveStateEntry *se;
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001225
1226 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
Jens Freimann284f42a2019-11-20 16:49:48 +01001227 if (se->vmsd && se->vmsd->dev_unplug_pending &&
1228 se->vmsd->dev_unplug_pending(se->opaque)) {
Keqian Zhud05de9e2020-02-04 13:08:41 +08001229 return true;
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001230 }
1231 }
1232
Keqian Zhud05de9e2020-02-04 13:08:41 +08001233 return false;
Jens Freimannc7e0acd2019-10-29 12:49:02 +01001234}
1235
Avihai Horon08fc4cb2023-09-06 18:08:51 +03001236int qemu_savevm_state_prepare(Error **errp)
1237{
1238 SaveStateEntry *se;
1239 int ret;
1240
1241 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1242 if (!se->ops || !se->ops->save_prepare) {
1243 continue;
1244 }
1245 if (se->ops->is_active) {
1246 if (!se->ops->is_active(se->opaque)) {
1247 continue;
1248 }
1249 }
1250
1251 ret = se->ops->save_prepare(se->opaque, errp);
1252 if (ret < 0) {
1253 return ret;
1254 }
1255 }
1256
1257 return 0;
1258}
1259
Juan Quintela9907e842017-06-28 11:52:24 +02001260void qemu_savevm_state_setup(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001261{
David Hildenbrande3bf5e62023-01-17 12:22:43 +01001262 MigrationState *ms = migrate_get_current();
aliguoria672b462008-11-11 21:33:36 +00001263 SaveStateEntry *se;
Wei Wangbd227062018-12-11 16:24:51 +08001264 Error *local_err = NULL;
Juan Quintela39346382011-09-22 11:02:14 +02001265 int ret;
aliguoria672b462008-11-11 21:33:36 +00001266
David Hildenbrande3bf5e62023-01-17 12:22:43 +01001267 ms->vmdesc = json_writer_new(false);
1268 json_writer_start_object(ms->vmdesc, NULL);
1269 json_writer_int64(ms->vmdesc, "page_size", qemu_target_page_size());
1270 json_writer_start_array(ms->vmdesc, "devices");
1271
Juan Quintela9907e842017-06-28 11:52:24 +02001272 trace_savevm_state_setup();
Juan Quintela0163a2e2015-05-13 13:37:04 +02001273 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
David Hildenbrand62f42622023-01-17 12:22:44 +01001274 if (se->vmsd && se->vmsd->early_setup) {
1275 ret = vmstate_save(f, se, ms->vmdesc);
1276 if (ret) {
1277 qemu_file_set_error(f, ret);
1278 break;
1279 }
1280 continue;
1281 }
1282
Juan Quintela9907e842017-06-28 11:52:24 +02001283 if (!se->ops || !se->ops->save_setup) {
aliguoria672b462008-11-11 21:33:36 +00001284 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001285 }
Wei Yangcea3b4c2019-08-19 11:28:04 +08001286 if (se->ops->is_active) {
Juan Quintela6bd68782012-06-27 10:59:15 +02001287 if (!se->ops->is_active(se->opaque)) {
1288 continue;
1289 }
1290 }
Dr. David Alan Gilbertce39bfc2015-05-19 12:29:50 +01001291 save_section_header(f, se, QEMU_VM_SECTION_START);
aliguoria672b462008-11-11 21:33:36 +00001292
Juan Quintela9907e842017-06-28 11:52:24 +02001293 ret = se->ops->save_setup(f, se->opaque);
Dr. David Alan Gilbertf68945d2015-05-19 12:29:52 +01001294 save_section_footer(f, se);
Juan Quintela29757252011-10-19 15:22:18 +02001295 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001296 qemu_file_set_error(f, ret);
1297 break;
Juan Quintela29757252011-10-19 15:22:18 +02001298 }
aliguoria672b462008-11-11 21:33:36 +00001299 }
Wei Wangbd227062018-12-11 16:24:51 +08001300
1301 if (precopy_notify(PRECOPY_NOTIFY_SETUP, &local_err)) {
1302 error_report_err(local_err);
1303 }
aliguoria672b462008-11-11 21:33:36 +00001304}
1305
Peter Xud1b8ead2018-05-02 18:47:31 +08001306int qemu_savevm_state_resume_prepare(MigrationState *s)
1307{
1308 SaveStateEntry *se;
1309 int ret;
1310
1311 trace_savevm_state_resume_prepare();
1312
1313 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1314 if (!se->ops || !se->ops->resume_prepare) {
1315 continue;
1316 }
Wei Yangcea3b4c2019-08-19 11:28:04 +08001317 if (se->ops->is_active) {
Peter Xud1b8ead2018-05-02 18:47:31 +08001318 if (!se->ops->is_active(se->opaque)) {
1319 continue;
1320 }
1321 }
1322 ret = se->ops->resume_prepare(s, se->opaque);
1323 if (ret < 0) {
1324 return ret;
1325 }
1326 }
1327
1328 return 0;
1329}
1330
Juan Quintela39346382011-09-22 11:02:14 +02001331/*
Dong Xu Wang07f35072011-11-22 18:06:26 +08001332 * this function has three return values:
Juan Quintela39346382011-09-22 11:02:14 +02001333 * negative: there was one error, and we have -errno.
1334 * 0 : We haven't finished, caller have to go again
1335 * 1 : We have finished, we can go to complete phase
1336 */
Dr. David Alan Gilbert35ecd942015-11-05 18:11:14 +00001337int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy)
aliguoria672b462008-11-11 21:33:36 +00001338{
1339 SaveStateEntry *se;
1340 int ret = 1;
1341
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +11001342 trace_savevm_state_iterate();
Juan Quintela0163a2e2015-05-13 13:37:04 +02001343 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001344 if (!se->ops || !se->ops->save_live_iterate) {
aliguoria672b462008-11-11 21:33:36 +00001345 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001346 }
Wei Yanga94cd7b2019-03-27 09:31:30 +08001347 if (se->ops->is_active &&
1348 !se->ops->is_active(se->opaque)) {
1349 continue;
Juan Quintela6bd68782012-06-27 10:59:15 +02001350 }
Wei Yanga94cd7b2019-03-27 09:31:30 +08001351 if (se->ops->is_active_iterate &&
1352 !se->ops->is_active_iterate(se->opaque)) {
1353 continue;
Vladimir Sementsov-Ogievskiyc865d842018-03-13 15:34:01 -04001354 }
Dr. David Alan Gilbert35ecd942015-11-05 18:11:14 +00001355 /*
1356 * In the postcopy phase, any device that doesn't know how to
1357 * do postcopy should have saved it's state in the _complete
1358 * call that's already run, it might get confused if we call
1359 * iterate afterwards.
1360 */
Vladimir Sementsov-Ogievskiyc6467622017-07-10 19:30:14 +03001361 if (postcopy &&
1362 !(se->ops->has_postcopy && se->ops->has_postcopy(se->opaque))) {
Dr. David Alan Gilbert35ecd942015-11-05 18:11:14 +00001363 continue;
1364 }
Juan Quintelae1fde0e2023-05-15 21:56:58 +02001365 if (migration_rate_exceeded(f)) {
Juan Quintelaaac844e2012-05-22 00:38:26 +02001366 return 0;
1367 }
Alexey Kardashevskiy464400f2014-03-07 01:33:37 +05301368 trace_savevm_section_start(se->idstr, se->section_id);
Dr. David Alan Gilbertce39bfc2015-05-19 12:29:50 +01001369
1370 save_section_header(f, se, QEMU_VM_SECTION_PART);
aliguoria672b462008-11-11 21:33:36 +00001371
Juan Quintela16310a32012-06-28 15:31:37 +02001372 ret = se->ops->save_live_iterate(f, se->opaque);
Dr. David Alan Gilberta5df2a02015-01-21 10:14:48 +00001373 trace_savevm_section_end(se->idstr, se->section_id, ret);
Dr. David Alan Gilbertf68945d2015-05-19 12:29:52 +01001374 save_section_footer(f, se);
Juan Quintela517a13c2012-05-21 23:46:44 +02001375
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001376 if (ret < 0) {
David Edmondson92002652021-12-15 14:14:37 +00001377 error_report("failed to save SaveStateEntry with id(name): "
1378 "%d(%s): %d",
1379 se->section_id, se->idstr, ret);
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001380 qemu_file_set_error(f, ret);
1381 }
Juan Quintela29757252011-10-19 15:22:18 +02001382 if (ret <= 0) {
Jan Kiszka90697be2009-12-01 15:19:55 +01001383 /* Do not proceed to the next vmstate before this one reported
1384 completion of the current stage. This serializes the migration
1385 and reduces the probability that a faster changing state is
1386 synchronized over and over again. */
1387 break;
1388 }
aliguoria672b462008-11-11 21:33:36 +00001389 }
Juan Quintela39346382011-09-22 11:02:14 +02001390 return ret;
aliguoria672b462008-11-11 21:33:36 +00001391}
1392
Alexander Graf9850c602015-02-23 13:56:42 +01001393static bool should_send_vmdesc(void)
1394{
1395 MachineState *machine = MACHINE(qdev_get_machine());
Juan Quintela57273092017-03-20 22:25:28 +01001396 bool in_postcopy = migration_in_postcopy();
Dr. David Alan Gilbert8421b202015-11-05 18:10:59 +00001397 return !machine->suppress_vmdesc && !in_postcopy;
Alexander Graf9850c602015-02-23 13:56:42 +01001398}
1399
Dr. David Alan Gilbert763c9062015-11-05 18:11:00 +00001400/*
1401 * Calls the save_live_complete_postcopy methods
1402 * causing the last few pages to be sent immediately and doing any associated
1403 * cleanup.
1404 * Note postcopy also calls qemu_savevm_state_complete_precopy to complete
1405 * all the other devices, but that happens at the point we switch to postcopy.
1406 */
1407void qemu_savevm_state_complete_postcopy(QEMUFile *f)
1408{
1409 SaveStateEntry *se;
1410 int ret;
1411
1412 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1413 if (!se->ops || !se->ops->save_live_complete_postcopy) {
1414 continue;
1415 }
Wei Yangcea3b4c2019-08-19 11:28:04 +08001416 if (se->ops->is_active) {
Dr. David Alan Gilbert763c9062015-11-05 18:11:00 +00001417 if (!se->ops->is_active(se->opaque)) {
1418 continue;
1419 }
1420 }
1421 trace_savevm_section_start(se->idstr, se->section_id);
1422 /* Section type */
1423 qemu_put_byte(f, QEMU_VM_SECTION_END);
1424 qemu_put_be32(f, se->section_id);
1425
1426 ret = se->ops->save_live_complete_postcopy(f, se->opaque);
1427 trace_savevm_section_end(se->idstr, se->section_id, ret);
1428 save_section_footer(f, se);
1429 if (ret < 0) {
1430 qemu_file_set_error(f, ret);
1431 return;
1432 }
1433 }
1434
1435 qemu_put_byte(f, QEMU_VM_EOF);
1436 qemu_fflush(f);
1437}
1438
Wei Yang622a80c2019-07-09 22:09:23 +08001439static
Wei Yange3267672019-07-09 22:09:24 +08001440int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)
aliguoria672b462008-11-11 21:33:36 +00001441{
1442 SaveStateEntry *se;
Juan Quintela29757252011-10-19 15:22:18 +02001443 int ret;
Jan Kiszkaea375f92010-03-01 19:10:30 +01001444
Juan Quintela0163a2e2015-05-13 13:37:04 +02001445 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
Dr. David Alan Gilbert763c9062015-11-05 18:11:00 +00001446 if (!se->ops ||
Vladimir Sementsov-Ogievskiyc6467622017-07-10 19:30:14 +03001447 (in_postcopy && se->ops->has_postcopy &&
1448 se->ops->has_postcopy(se->opaque)) ||
Dr. David Alan Gilbert763c9062015-11-05 18:11:00 +00001449 !se->ops->save_live_complete_precopy) {
aliguoria672b462008-11-11 21:33:36 +00001450 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001451 }
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001452
Wei Yangcea3b4c2019-08-19 11:28:04 +08001453 if (se->ops->is_active) {
Juan Quintela6bd68782012-06-27 10:59:15 +02001454 if (!se->ops->is_active(se->opaque)) {
1455 continue;
1456 }
1457 }
Alexey Kardashevskiy464400f2014-03-07 01:33:37 +05301458 trace_savevm_section_start(se->idstr, se->section_id);
Dr. David Alan Gilbertce39bfc2015-05-19 12:29:50 +01001459
1460 save_section_header(f, se, QEMU_VM_SECTION_END);
aliguoria672b462008-11-11 21:33:36 +00001461
Dr. David Alan Gilberta3e06c32015-11-05 18:10:41 +00001462 ret = se->ops->save_live_complete_precopy(f, se->opaque);
Dr. David Alan Gilberta5df2a02015-01-21 10:14:48 +00001463 trace_savevm_section_end(se->idstr, se->section_id, ret);
Dr. David Alan Gilbertf68945d2015-05-19 12:29:52 +01001464 save_section_footer(f, se);
Juan Quintela29757252011-10-19 15:22:18 +02001465 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001466 qemu_file_set_error(f, ret);
Fam Zhenga1fbe752017-06-17 00:06:58 +08001467 return -1;
Juan Quintela29757252011-10-19 15:22:18 +02001468 }
aliguoria672b462008-11-11 21:33:36 +00001469 }
1470
Wei Yang622a80c2019-07-09 22:09:23 +08001471 return 0;
1472}
1473
Wei Yang622a80c2019-07-09 22:09:23 +08001474int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f,
1475 bool in_postcopy,
1476 bool inactivate_disks)
1477{
David Hildenbrande3bf5e62023-01-17 12:22:43 +01001478 MigrationState *ms = migrate_get_current();
1479 JSONWriter *vmdesc = ms->vmdesc;
Wei Yang622a80c2019-07-09 22:09:23 +08001480 int vmdesc_len;
1481 SaveStateEntry *se;
1482 int ret;
Dr. David Alan Gilbert1c0d2492015-11-11 14:02:27 +00001483
Juan Quintela0163a2e2015-05-13 13:37:04 +02001484 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
David Hildenbrand62f42622023-01-17 12:22:44 +01001485 if (se->vmsd && se->vmsd->early_setup) {
1486 /* Already saved during qemu_savevm_state_setup(). */
1487 continue;
1488 }
1489
Dr. David Alan Gilbert687433f2017-09-25 12:29:16 +01001490 ret = vmstate_save(f, se, vmdesc);
1491 if (ret) {
1492 qemu_file_set_error(f, ret);
1493 return ret;
1494 }
aliguoria672b462008-11-11 21:33:36 +00001495 }
1496
Fam Zhenga1fbe752017-06-17 00:06:58 +08001497 if (inactivate_disks) {
1498 /* Inactivate before sending QEMU_VM_EOF so that the
Emanuele Giuseppe Esposito3b717192022-02-09 05:54:51 -05001499 * bdrv_activate_all() on the other end won't fail. */
Fam Zhenga1fbe752017-06-17 00:06:58 +08001500 ret = bdrv_inactivate_all();
1501 if (ret) {
Dr. David Alan Gilbertc232bf52017-08-22 18:02:12 +01001502 error_report("%s: bdrv_inactivate_all() failed (%d)",
1503 __func__, ret);
Fam Zhenga1fbe752017-06-17 00:06:58 +08001504 qemu_file_set_error(f, ret);
1505 return ret;
1506 }
1507 }
Dr. David Alan Gilbert763c9062015-11-05 18:11:00 +00001508 if (!in_postcopy) {
1509 /* Postcopy stream will still be going */
1510 qemu_put_byte(f, QEMU_VM_EOF);
1511 }
Alexander Graf8118f092015-01-22 15:01:39 +01001512
Markus Armbruster3ddba9a2020-12-11 18:11:48 +01001513 json_writer_end_array(vmdesc);
1514 json_writer_end_object(vmdesc);
1515 vmdesc_len = strlen(json_writer_get(vmdesc));
Alexander Graf8118f092015-01-22 15:01:39 +01001516
Alexander Graf9850c602015-02-23 13:56:42 +01001517 if (should_send_vmdesc()) {
1518 qemu_put_byte(f, QEMU_VM_VMDESCRIPTION);
1519 qemu_put_be32(f, vmdesc_len);
Markus Armbruster3ddba9a2020-12-11 18:11:48 +01001520 qemu_put_buffer(f, (uint8_t *)json_writer_get(vmdesc), vmdesc_len);
Alexander Graf9850c602015-02-23 13:56:42 +01001521 }
Alexander Graf8118f092015-01-22 15:01:39 +01001522
David Hildenbrande3bf5e62023-01-17 12:22:43 +01001523 /* Free it now to detect any inconsistencies. */
1524 json_writer_free(vmdesc);
1525 ms->vmdesc = NULL;
1526
Wei Yang622a80c2019-07-09 22:09:23 +08001527 return 0;
1528}
1529
1530int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only,
1531 bool inactivate_disks)
1532{
1533 int ret;
1534 Error *local_err = NULL;
1535 bool in_postcopy = migration_in_postcopy();
1536
1537 if (precopy_notify(PRECOPY_NOTIFY_COMPLETE, &local_err)) {
1538 error_report_err(local_err);
1539 }
1540
1541 trace_savevm_state_complete_precopy();
1542
1543 cpu_synchronize_all_states();
1544
Wei Yange3267672019-07-09 22:09:24 +08001545 if (!in_postcopy || iterable_only) {
1546 ret = qemu_savevm_state_complete_precopy_iterable(f, in_postcopy);
1547 if (ret) {
1548 return ret;
1549 }
Wei Yang622a80c2019-07-09 22:09:23 +08001550 }
1551
1552 if (iterable_only) {
1553 goto flush;
1554 }
1555
1556 ret = qemu_savevm_state_complete_precopy_non_iterable(f, in_postcopy,
1557 inactivate_disks);
1558 if (ret) {
1559 return ret;
1560 }
1561
Wei Yang4e455d52019-07-09 22:09:22 +08001562flush:
Paolo Bonziniedaae612013-02-22 17:36:29 +01001563 qemu_fflush(f);
Fam Zhenga1fbe752017-06-17 00:06:58 +08001564 return 0;
aliguoria672b462008-11-11 21:33:36 +00001565}
1566
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001567/* Give an estimate of the amount left to be transferred,
1568 * the result is split into the amount for units that can and
1569 * for units that can't do postcopy.
1570 */
Juan Quintela24beea42023-02-08 14:48:02 +01001571void qemu_savevm_state_pending_estimate(uint64_t *must_precopy,
1572 uint64_t *can_postcopy)
Juan Quintelae4ed1542012-09-21 11:18:18 +02001573{
1574 SaveStateEntry *se;
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001575
Juan Quintela24beea42023-02-08 14:48:02 +01001576 *must_precopy = 0;
1577 *can_postcopy = 0;
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00001578
Juan Quintela0163a2e2015-05-13 13:37:04 +02001579 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
Juan Quintelae2647052023-02-09 14:29:45 +01001580 if (!se->ops || !se->ops->state_pending_estimate) {
Juan Quintelae4ed1542012-09-21 11:18:18 +02001581 continue;
1582 }
Wei Yangcea3b4c2019-08-19 11:28:04 +08001583 if (se->ops->is_active) {
Juan Quintelae4ed1542012-09-21 11:18:18 +02001584 if (!se->ops->is_active(se->opaque)) {
1585 continue;
1586 }
1587 }
Juan Quintela24beea42023-02-08 14:48:02 +01001588 se->ops->state_pending_estimate(se->opaque, must_precopy, can_postcopy);
Juan Quintelac8df4a72022-10-03 02:00:03 +02001589 }
1590}
1591
Juan Quintela24beea42023-02-08 14:48:02 +01001592void qemu_savevm_state_pending_exact(uint64_t *must_precopy,
1593 uint64_t *can_postcopy)
Juan Quintelac8df4a72022-10-03 02:00:03 +02001594{
1595 SaveStateEntry *se;
1596
Juan Quintela24beea42023-02-08 14:48:02 +01001597 *must_precopy = 0;
1598 *can_postcopy = 0;
Juan Quintelac8df4a72022-10-03 02:00:03 +02001599
1600 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
Juan Quintelae2647052023-02-09 14:29:45 +01001601 if (!se->ops || !se->ops->state_pending_exact) {
Juan Quintelac8df4a72022-10-03 02:00:03 +02001602 continue;
1603 }
1604 if (se->ops->is_active) {
1605 if (!se->ops->is_active(se->opaque)) {
1606 continue;
1607 }
1608 }
Juan Quintela24beea42023-02-08 14:48:02 +01001609 se->ops->state_pending_exact(se->opaque, must_precopy, can_postcopy);
Juan Quintelae4ed1542012-09-21 11:18:18 +02001610 }
Juan Quintelae4ed1542012-09-21 11:18:18 +02001611}
1612
Liang Liea7415f2015-11-02 15:37:01 +08001613void qemu_savevm_state_cleanup(void)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001614{
1615 SaveStateEntry *se;
Wei Wangbd227062018-12-11 16:24:51 +08001616 Error *local_err = NULL;
1617
1618 if (precopy_notify(PRECOPY_NOTIFY_CLEANUP, &local_err)) {
1619 error_report_err(local_err);
1620 }
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001621
Liang Liea7415f2015-11-02 15:37:01 +08001622 trace_savevm_state_cleanup();
Juan Quintela0163a2e2015-05-13 13:37:04 +02001623 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
Juan Quintela70f794f2017-06-28 11:52:25 +02001624 if (se->ops && se->ops->save_cleanup) {
1625 se->ops->save_cleanup(se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001626 }
1627 }
1628}
1629
Kevin Wolf5d804482015-02-10 14:25:02 +01001630static int qemu_savevm_state(QEMUFile *f, Error **errp)
aliguoria672b462008-11-11 21:33:36 +00001631{
aliguoria672b462008-11-11 21:33:36 +00001632 int ret;
Peter Xu3e0c8052018-02-08 18:31:15 +08001633 MigrationState *ms = migrate_get_current();
Denis V. Lunev6dcf6662016-06-15 18:06:43 +03001634 MigrationStatus status;
Peter Xu3e0c8052018-02-08 18:31:15 +08001635
Juan Quintela392d87e2020-01-21 15:39:23 +01001636 if (migration_is_running(ms->state)) {
Jia Lina3d63da12018-10-26 16:36:20 +08001637 error_setg(errp, QERR_MIGRATION_ACTIVE);
1638 return -EINVAL;
1639 }
aliguoria672b462008-11-11 21:33:36 +00001640
Juan Quintela9d4b1e52023-03-01 22:23:57 +01001641 if (migrate_block()) {
Juan Quintelace7c8172017-04-05 20:45:22 +02001642 error_setg(errp, "Block migration and snapshots are incompatible");
Jia Lina3d63da12018-10-26 16:36:20 +08001643 return -EINVAL;
Juan Quintelace7c8172017-04-05 20:45:22 +02001644 }
1645
Avihai Horon08fc4cb2023-09-06 18:08:51 +03001646 ret = migrate_init(ms, errp);
1647 if (ret) {
1648 return ret;
1649 }
Jia Lina3d63da12018-10-26 16:36:20 +08001650 ms->to_dst_file = f;
1651
Paolo Bonzini9b095032013-02-22 17:36:28 +01001652 qemu_mutex_unlock_iothread();
Dr. David Alan Gilbertf796baa2015-05-21 13:24:12 +01001653 qemu_savevm_state_header(f);
Juan Quintela9907e842017-06-28 11:52:24 +02001654 qemu_savevm_state_setup(f);
Paolo Bonzini9b095032013-02-22 17:36:28 +01001655 qemu_mutex_lock_iothread();
1656
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001657 while (qemu_file_get_error(f) == 0) {
Dr. David Alan Gilbert35ecd942015-11-05 18:11:14 +00001658 if (qemu_savevm_state_iterate(f, false) > 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001659 break;
1660 }
1661 }
aliguoria672b462008-11-11 21:33:36 +00001662
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001663 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001664 if (ret == 0) {
Fam Zhenga1fbe752017-06-17 00:06:58 +08001665 qemu_savevm_state_complete_precopy(f, false, false);
Juan Quintela624b9cc2011-10-05 01:02:52 +02001666 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001667 }
Denis V. Lunev15b3b8e2015-11-09 10:24:04 +03001668 qemu_savevm_state_cleanup();
Paolo Bonzini04943eb2013-02-22 17:36:10 +01001669 if (ret != 0) {
Kevin Wolf5d804482015-02-10 14:25:02 +01001670 error_setg_errno(errp, -ret, "Error while writing VM state");
Paolo Bonzini04943eb2013-02-22 17:36:10 +01001671 }
Denis V. Lunev6dcf6662016-06-15 18:06:43 +03001672
Denis V. Lunev6dcf6662016-06-15 18:06:43 +03001673 if (ret != 0) {
1674 status = MIGRATION_STATUS_FAILED;
1675 } else {
1676 status = MIGRATION_STATUS_COMPLETED;
1677 }
1678 migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP, status);
Vladimir Sementsov-Ogievskiyf9c8caa2017-02-25 22:31:55 +03001679
1680 /* f is outer parameter, it should not stay in global migration state after
1681 * this function finished */
1682 ms->to_dst_file = NULL;
1683
aliguoria672b462008-11-11 21:33:36 +00001684 return ret;
1685}
1686
Zhang Chen3f6df992018-09-03 12:38:54 +08001687void qemu_savevm_live_state(QEMUFile *f)
1688{
1689 /* save QEMU_VM_SECTION_END section */
1690 qemu_savevm_state_complete_precopy(f, true, false);
1691 qemu_put_byte(f, QEMU_VM_EOF);
1692}
1693
1694int qemu_save_device_state(QEMUFile *f)
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001695{
1696 SaveStateEntry *se;
1697
Zhang Chen3f6df992018-09-03 12:38:54 +08001698 if (!migration_in_colo_state()) {
1699 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1700 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1701 }
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001702 cpu_synchronize_all_states();
1703
Juan Quintela0163a2e2015-05-13 13:37:04 +02001704 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
Dr. David Alan Gilbert687433f2017-09-25 12:29:16 +01001705 int ret;
1706
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001707 if (se->is_ram) {
1708 continue;
1709 }
Dr. David Alan Gilbert687433f2017-09-25 12:29:16 +01001710 ret = vmstate_save(f, se, NULL);
1711 if (ret) {
1712 return ret;
1713 }
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001714 }
1715
1716 qemu_put_byte(f, QEMU_VM_EOF);
1717
1718 return qemu_file_get_error(f);
1719}
1720
Peter Xu93062e22019-10-16 10:29:31 +08001721static SaveStateEntry *find_se(const char *idstr, uint32_t instance_id)
aliguoria672b462008-11-11 21:33:36 +00001722{
1723 SaveStateEntry *se;
1724
Juan Quintela0163a2e2015-05-13 13:37:04 +02001725 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001726 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001727 (instance_id == se->instance_id ||
1728 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +00001729 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001730 /* Migrating from an older version? */
1731 if (strstr(se->idstr, idstr) && se->compat) {
1732 if (!strcmp(se->compat->idstr, idstr) &&
1733 (instance_id == se->compat->instance_id ||
1734 instance_id == se->alias_id))
1735 return se;
1736 }
aliguoria672b462008-11-11 21:33:36 +00001737 }
1738 return NULL;
1739}
1740
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00001741enum LoadVMExitCodes {
1742 /* Allow a command to quit all layers of nested loadvm loops */
1743 LOADVM_QUIT = 1,
1744};
1745
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001746/* ------ incoming postcopy messages ------ */
1747/* 'advise' arrives before any transfers just to tell us that a postcopy
1748 * *might* happen - it might be skipped if precopy transferred everything
1749 * quickly.
1750 */
Greg Kurz875fcd02018-02-06 12:23:30 +01001751static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis,
1752 uint16_t len)
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001753{
1754 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
Dr. David Alan Gilberte8ca1db2017-02-24 18:28:29 +00001755 uint64_t remote_pagesize_summary, local_pagesize_summary, remote_tps;
Juan Quintela144fa062021-11-22 16:21:16 +01001756 size_t page_size = qemu_target_page_size();
Dr. David Alan Gilbertd3dff7a2018-03-12 17:21:01 +00001757 Error *local_err = NULL;
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001758
1759 trace_loadvm_postcopy_handle_advise();
1760 if (ps != POSTCOPY_INCOMING_NONE) {
1761 error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps);
1762 return -1;
1763 }
1764
Greg Kurz875fcd02018-02-06 12:23:30 +01001765 switch (len) {
1766 case 0:
1767 if (migrate_postcopy_ram()) {
1768 error_report("RAM postcopy is enabled but have 0 byte advise");
1769 return -EINVAL;
1770 }
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001771 return 0;
Greg Kurz875fcd02018-02-06 12:23:30 +01001772 case 8 + 8:
1773 if (!migrate_postcopy_ram()) {
1774 error_report("RAM postcopy is disabled but have 16 byte advise");
1775 return -EINVAL;
1776 }
1777 break;
1778 default:
1779 error_report("CMD_POSTCOPY_ADVISE invalid length (%d)", len);
1780 return -EINVAL;
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03001781 }
1782
Peter Xu74c38cf2023-04-25 21:15:14 -04001783 if (!postcopy_ram_supported_by_host(mis, &local_err)) {
1784 error_report_err(local_err);
Dr. David Alan Gilbert328d4d82017-02-02 15:59:08 +00001785 postcopy_state_set(POSTCOPY_INCOMING_NONE);
Dr. David Alan Gilberteb59db52015-11-05 18:10:55 +00001786 return -1;
1787 }
1788
Dr. David Alan Gilberte8ca1db2017-02-24 18:28:29 +00001789 remote_pagesize_summary = qemu_get_be64(mis->from_src_file);
1790 local_pagesize_summary = ram_pagesize_summary();
1791
1792 if (remote_pagesize_summary != local_pagesize_summary) {
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001793 /*
Dr. David Alan Gilberte8ca1db2017-02-24 18:28:29 +00001794 * This detects two potential causes of mismatch:
1795 * a) A mismatch in host page sizes
1796 * Some combinations of mismatch are probably possible but it gets
1797 * a bit more complicated. In particular we need to place whole
1798 * host pages on the dest at once, and we need to ensure that we
1799 * handle dirtying to make sure we never end up sending part of
1800 * a hostpage on it's own.
1801 * b) The use of different huge page sizes on source/destination
1802 * a more fine grain test is performed during RAM block migration
1803 * but this test here causes a nice early clear failure, and
1804 * also fails when passed to an older qemu that doesn't
1805 * do huge pages.
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001806 */
Dr. David Alan Gilberte8ca1db2017-02-24 18:28:29 +00001807 error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64
1808 " d=%" PRIx64 ")",
1809 remote_pagesize_summary, local_pagesize_summary);
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001810 return -1;
1811 }
1812
1813 remote_tps = qemu_get_be64(mis->from_src_file);
Juan Quintela144fa062021-11-22 16:21:16 +01001814 if (remote_tps != page_size) {
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001815 /*
1816 * Again, some differences could be dealt with, but for now keep it
1817 * simple.
1818 */
Juan Quintela20afaed2017-03-21 09:09:14 +01001819 error_report("Postcopy needs matching target page sizes (s=%d d=%zd)",
Juan Quintela144fa062021-11-22 16:21:16 +01001820 (int)remote_tps, page_size);
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001821 return -1;
1822 }
1823
Dr. David Alan Gilbertd3dff7a2018-03-12 17:21:01 +00001824 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_ADVISE, &local_err)) {
1825 error_report_err(local_err);
1826 return -1;
1827 }
1828
Dr. David Alan Gilbert1caddf82015-11-05 18:11:03 +00001829 if (ram_postcopy_incoming_init(mis)) {
1830 return -1;
1831 }
1832
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001833 return 0;
1834}
1835
1836/* After postcopy we will be told to throw some pages away since they're
1837 * dirty and will have to be demand fetched. Must happen before CPU is
1838 * started.
1839 * There can be 0..many of these messages, each encoding multiple pages.
1840 */
1841static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis,
1842 uint16_t len)
1843{
1844 int tmp;
1845 char ramid[256];
1846 PostcopyState ps = postcopy_state_get();
1847
1848 trace_loadvm_postcopy_ram_handle_discard();
1849
1850 switch (ps) {
1851 case POSTCOPY_INCOMING_ADVISE:
1852 /* 1st discard */
Dr. David Alan Gilbertf9527102015-11-05 18:11:20 +00001853 tmp = postcopy_ram_prepare_discard(mis);
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001854 if (tmp) {
1855 return tmp;
1856 }
1857 break;
1858
1859 case POSTCOPY_INCOMING_DISCARD:
1860 /* Expected state */
1861 break;
1862
1863 default:
1864 error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)",
1865 ps);
1866 return -1;
1867 }
1868 /* We're expecting a
1869 * Version (0)
1870 * a RAM ID string (length byte, name, 0 term)
1871 * then at least 1 16 byte chunk
1872 */
1873 if (len < (1 + 1 + 1 + 1 + 2 * 8)) {
1874 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1875 return -1;
1876 }
1877
1878 tmp = qemu_get_byte(mis->from_src_file);
1879 if (tmp != postcopy_ram_discard_version) {
1880 error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp);
1881 return -1;
1882 }
1883
1884 if (!qemu_get_counted_string(mis->from_src_file, ramid)) {
1885 error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID");
1886 return -1;
1887 }
1888 tmp = qemu_get_byte(mis->from_src_file);
1889 if (tmp != 0) {
1890 error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp);
1891 return -1;
1892 }
1893
1894 len -= 3 + strlen(ramid);
1895 if (len % 16) {
1896 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1897 return -1;
1898 }
1899 trace_loadvm_postcopy_ram_handle_discard_header(ramid, len);
1900 while (len) {
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001901 uint64_t start_addr, block_length;
1902 start_addr = qemu_get_be64(mis->from_src_file);
1903 block_length = qemu_get_be64(mis->from_src_file);
1904
1905 len -= 16;
Juan Quintelaaaa20642017-03-21 11:35:24 +01001906 int ret = ram_discard_range(ramid, start_addr, block_length);
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001907 if (ret) {
1908 return ret;
1909 }
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00001910 }
1911 trace_loadvm_postcopy_ram_handle_discard_end();
1912
1913 return 0;
1914}
1915
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001916/*
1917 * Triggered by a postcopy_listen command; this thread takes over reading
1918 * the input stream, leaving the main thread free to carry on loading the rest
1919 * of the device state (from RAM).
1920 * (TODO:This could do with being in a postcopy file - but there again it's
1921 * just another input loop, not that postcopy specific)
1922 */
1923static void *postcopy_ram_listen_thread(void *opaque)
1924{
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001925 MigrationIncomingState *mis = migration_incoming_get_current();
Peter Xub411b842018-05-02 18:47:20 +08001926 QEMUFile *f = mis->from_src_file;
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001927 int load_res;
Vladimir Sementsov-Ogievskiyee647222020-07-27 22:42:32 +03001928 MigrationState *migr = migrate_get_current();
1929
1930 object_ref(OBJECT(migr));
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001931
Dr. David Alan Gilbert6ba996b2015-12-16 11:47:35 +00001932 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
1933 MIGRATION_STATUS_POSTCOPY_ACTIVE);
Peter Xu095c12a2022-03-01 16:39:06 +08001934 qemu_sem_post(&mis->thread_sync_sem);
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001935 trace_postcopy_ram_listen_thread_start();
1936
Lidong Chen74637e62018-08-06 21:29:29 +08001937 rcu_register_thread();
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001938 /*
1939 * Because we're a thread and not a coroutine we can't yield
1940 * in qemu_file, and thus we must be blocking now.
1941 */
1942 qemu_file_set_blocking(f, true);
1943 load_res = qemu_loadvm_state_main(f, mis);
Peter Xub411b842018-05-02 18:47:20 +08001944
1945 /*
1946 * This is tricky, but, mis->from_src_file can change after it
1947 * returns, when postcopy recovery happened. In the future, we may
1948 * want a wrapper for the QEMUFile handle.
1949 */
1950 f = mis->from_src_file;
1951
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001952 /* And non-blocking again so we don't block in any cleanup */
1953 qemu_file_set_blocking(f, false);
1954
1955 trace_postcopy_ram_listen_thread_exit();
1956 if (load_res < 0) {
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001957 qemu_file_set_error(f, load_res);
Vladimir Sementsov-Ogievskiyee647222020-07-27 22:42:32 +03001958 dirty_bitmap_mig_cancel_incoming();
1959 if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING &&
1960 !migrate_postcopy_ram() && migrate_dirty_bitmaps())
1961 {
1962 error_report("%s: loadvm failed during postcopy: %d. All states "
1963 "are migrated except dirty bitmaps. Some dirty "
1964 "bitmaps may be lost, and present migrated dirty "
1965 "bitmaps are correctly migrated and valid.",
1966 __func__, load_res);
1967 load_res = 0; /* prevent further exit() */
1968 } else {
1969 error_report("%s: loadvm failed: %d", __func__, load_res);
1970 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1971 MIGRATION_STATUS_FAILED);
1972 }
1973 }
1974 if (load_res >= 0) {
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001975 /*
1976 * This looks good, but it's possible that the device loading in the
1977 * main thread hasn't finished yet, and so we might not be in 'RUN'
1978 * state yet; wait for the end of the main thread.
1979 */
1980 qemu_event_wait(&mis->main_thread_load_event);
1981 }
1982 postcopy_ram_incoming_cleanup(mis);
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001983
1984 if (load_res < 0) {
1985 /*
1986 * If something went wrong then we have a bad state so exit;
1987 * depending how far we got it might be possible at this point
1988 * to leave the guest running and fire MCEs for pages that never
1989 * arrived as a desperate recovery step.
1990 */
Lidong Chen74637e62018-08-06 21:29:29 +08001991 rcu_unregister_thread();
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00001992 exit(EXIT_FAILURE);
1993 }
1994
Dr. David Alan Gilbert6ba996b2015-12-16 11:47:35 +00001995 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1996 MIGRATION_STATUS_COMPLETED);
1997 /*
1998 * If everything has worked fine, then the main thread has waited
1999 * for us to start, and we're the last use of the mis.
2000 * (If something broke then qemu will have to exit anyway since it's
2001 * got a bad migration state).
2002 */
2003 migration_incoming_state_destroy();
Juan Quintelaacb5ea82017-06-28 11:52:26 +02002004 qemu_loadvm_state_cleanup();
Dr. David Alan Gilbert6ba996b2015-12-16 11:47:35 +00002005
Lidong Chen74637e62018-08-06 21:29:29 +08002006 rcu_unregister_thread();
Dr. David Alan Gilbert9cf4bb82018-09-14 18:04:29 +01002007 mis->have_listen_thread = false;
Wei Yang2d49bac2019-10-06 08:02:48 +08002008 postcopy_state_set(POSTCOPY_INCOMING_END);
2009
Vladimir Sementsov-Ogievskiyee647222020-07-27 22:42:32 +03002010 object_unref(OBJECT(migr));
2011
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00002012 return NULL;
2013}
2014
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002015/* After this message we must be able to immediately receive postcopy data */
2016static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)
2017{
2018 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_LISTENING);
Dr. David Alan Gilbert6864a7b2018-03-12 17:21:06 +00002019 Error *local_err = NULL;
2020
Peter Xuc84f9762022-03-01 16:39:02 +08002021 trace_loadvm_postcopy_handle_listen("enter");
2022
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002023 if (ps != POSTCOPY_INCOMING_ADVISE && ps != POSTCOPY_INCOMING_DISCARD) {
2024 error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps);
2025 return -1;
2026 }
Dr. David Alan Gilbertf9527102015-11-05 18:11:20 +00002027 if (ps == POSTCOPY_INCOMING_ADVISE) {
2028 /*
2029 * A rare case, we entered listen without having to do any discards,
2030 * so do the setup that's normally done at the time of the 1st discard.
2031 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002032 if (migrate_postcopy_ram()) {
2033 postcopy_ram_prepare_discard(mis);
2034 }
Dr. David Alan Gilbertf9527102015-11-05 18:11:20 +00002035 }
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002036
Peter Xuc84f9762022-03-01 16:39:02 +08002037 trace_loadvm_postcopy_handle_listen("after discard");
2038
Dr. David Alan Gilbertf0a227a2015-11-05 18:11:04 +00002039 /*
2040 * Sensitise RAM - can now generate requests for blocks that don't exist
2041 * However, at this point the CPU shouldn't be running, and the IO
2042 * shouldn't be doing anything yet so don't actually expect requests
2043 */
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002044 if (migrate_postcopy_ram()) {
Wei Yang2a7eb142019-10-10 09:13:15 +08002045 if (postcopy_ram_incoming_setup(mis)) {
Fei Li91b02dc2019-01-13 22:08:48 +08002046 postcopy_ram_incoming_cleanup(mis);
Vladimir Sementsov-Ogievskiy58110f02017-07-10 19:30:16 +03002047 return -1;
2048 }
Dr. David Alan Gilbertf0a227a2015-11-05 18:11:04 +00002049 }
2050
Peter Xuc84f9762022-03-01 16:39:02 +08002051 trace_loadvm_postcopy_handle_listen("after uffd");
2052
Dr. David Alan Gilbert6864a7b2018-03-12 17:21:06 +00002053 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN, &local_err)) {
2054 error_report_err(local_err);
2055 return -1;
2056 }
2057
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00002058 mis->have_listen_thread = true;
Peter Xu095c12a2022-03-01 16:39:06 +08002059 postcopy_thread_create(mis, &mis->listen_thread, "postcopy/listen",
2060 postcopy_ram_listen_thread, QEMU_THREAD_DETACHED);
Peter Xuc84f9762022-03-01 16:39:02 +08002061 trace_loadvm_postcopy_handle_listen("return");
2062
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002063 return 0;
2064}
2065
Denis V. Lunevea6a55b2016-02-24 11:53:39 +03002066static void loadvm_postcopy_handle_run_bh(void *opaque)
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002067{
Dr. David Alan Gilbert27c68252015-11-05 18:11:19 +00002068 Error *local_err = NULL;
Wei Yang1ce54262019-08-05 13:31:46 +08002069 MigrationIncomingState *mis = opaque;
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002070
Peter Xub9a040b2022-03-01 16:39:03 +08002071 trace_loadvm_postcopy_handle_run_bh("enter");
2072
Dr. David Alan Gilbert27c68252015-11-05 18:11:19 +00002073 /* TODO we should move all of this lot into postcopy_ram.c or a shared code
2074 * in migration.c
2075 */
2076 cpu_synchronize_all_post_init();
2077
Peter Xub9a040b2022-03-01 16:39:03 +08002078 trace_loadvm_postcopy_handle_run_bh("after cpu sync");
2079
Dr. David Alan Gilbert76595052019-02-27 13:24:08 +00002080 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
Dr. David Alan Gilbert27c68252015-11-05 18:11:19 +00002081
Peter Xub9a040b2022-03-01 16:39:03 +08002082 trace_loadvm_postcopy_handle_run_bh("after announce");
2083
Emanuele Giuseppe Esposito3b717192022-02-09 05:54:51 -05002084 /* Make sure all file formats throw away their mutable metadata.
Kevin Wolface21a52017-05-04 18:52:36 +02002085 * If we get an error here, just don't restart the VM yet. */
Emanuele Giuseppe Esposito3b717192022-02-09 05:54:51 -05002086 bdrv_activate_all(&local_err);
Dr. David Alan Gilbert27c68252015-11-05 18:11:19 +00002087 if (local_err) {
2088 error_report_err(local_err);
Kevin Wolf0042fd32017-04-13 18:38:28 +02002089 local_err = NULL;
2090 autostart = false;
2091 }
2092
Peter Xub9a040b2022-03-01 16:39:03 +08002093 trace_loadvm_postcopy_handle_run_bh("after invalidate cache");
Dr. David Alan Gilbert27c68252015-11-05 18:11:19 +00002094
Vladimir Sementsov-Ogievskiyb35ebdf2018-03-13 15:34:01 -04002095 dirty_bitmap_mig_before_vm_start();
2096
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002097 if (autostart) {
2098 /* Hold onto your hats, starting the CPU */
2099 vm_start();
2100 } else {
2101 /* leave it paused and let management decide when to start the CPU */
2102 runstate_set(RUN_STATE_PAUSED);
2103 }
2104
Wei Yang1ce54262019-08-05 13:31:46 +08002105 qemu_bh_delete(mis->bh);
Peter Xub9a040b2022-03-01 16:39:03 +08002106
2107 trace_loadvm_postcopy_handle_run_bh("return");
Denis V. Lunevea6a55b2016-02-24 11:53:39 +03002108}
2109
2110/* After all discards we can start running and asking for pages */
2111static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
2112{
Wei Yang0197d892019-10-10 09:13:16 +08002113 PostcopyState ps = postcopy_state_get();
Denis V. Lunevea6a55b2016-02-24 11:53:39 +03002114
2115 trace_loadvm_postcopy_handle_run();
2116 if (ps != POSTCOPY_INCOMING_LISTENING) {
2117 error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps);
2118 return -1;
2119 }
2120
Wei Yang0197d892019-10-10 09:13:16 +08002121 postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
Wei Yang1ce54262019-08-05 13:31:46 +08002122 mis->bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, mis);
2123 qemu_bh_schedule(mis->bh);
Denis V. Lunevea6a55b2016-02-24 11:53:39 +03002124
Dr. David Alan Gilbert27c68252015-11-05 18:11:19 +00002125 /* We need to finish reading the stream from the package
2126 * and also stop reading anything more from the stream that loaded the
2127 * package (since it's now being read by the listener thread).
2128 * LOADVM_QUIT will quit all the layers of nested loadvm loops.
2129 */
2130 return LOADVM_QUIT;
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002131}
2132
Peter Xu0c267812020-10-21 17:27:19 -04002133/* We must be with page_request_mutex held */
2134static gboolean postcopy_sync_page_req(gpointer key, gpointer value,
2135 gpointer data)
2136{
2137 MigrationIncomingState *mis = data;
2138 void *host_addr = (void *) key;
2139 ram_addr_t rb_offset;
2140 RAMBlock *rb;
2141 int ret;
2142
2143 rb = qemu_ram_block_from_host(host_addr, true, &rb_offset);
2144 if (!rb) {
2145 /*
2146 * This should _never_ happen. However be nice for a migrating VM to
2147 * not crash/assert. Post an error (note: intended to not use *_once
2148 * because we do want to see all the illegal addresses; and this can
2149 * never be triggered by the guest so we're safe) and move on next.
2150 */
2151 error_report("%s: illegal host addr %p", __func__, host_addr);
2152 /* Try the next entry */
2153 return FALSE;
2154 }
2155
2156 ret = migrate_send_rp_message_req_pages(mis, rb, rb_offset);
2157 if (ret) {
2158 /* Please refer to above comment. */
2159 error_report("%s: send rp message failed for addr %p",
2160 __func__, host_addr);
2161 return FALSE;
2162 }
2163
2164 trace_postcopy_page_req_sync(host_addr);
2165
2166 return FALSE;
2167}
2168
2169static void migrate_send_rp_req_pages_pending(MigrationIncomingState *mis)
2170{
2171 WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
2172 g_tree_foreach(mis->page_requested, postcopy_sync_page_req, mis);
2173 }
2174}
2175
Peter Xu3f5875e2018-05-02 18:47:29 +08002176static int loadvm_postcopy_handle_resume(MigrationIncomingState *mis)
2177{
2178 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
2179 error_report("%s: illegal resume received", __func__);
2180 /* Don't fail the load, only for this. */
2181 return 0;
2182 }
2183
2184 /*
Peter Xucc5ab872020-11-02 10:30:09 -05002185 * Reset the last_rb before we resend any page req to source again, since
2186 * the source should have it reset already.
2187 */
2188 mis->last_rb = NULL;
2189
2190 /*
Peter Xu3f5875e2018-05-02 18:47:29 +08002191 * This means source VM is ready to resume the postcopy migration.
Peter Xu3f5875e2018-05-02 18:47:29 +08002192 */
2193 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2194 MIGRATION_STATUS_POSTCOPY_ACTIVE);
Peter Xu3f5875e2018-05-02 18:47:29 +08002195
2196 trace_loadvm_postcopy_handle_resume();
2197
Peter Xu13955b82018-05-02 18:47:30 +08002198 /* Tell source that "we are ready" */
2199 migrate_send_rp_resume_ack(mis, MIGRATION_RESUME_ACK_VALUE);
Peter Xu3f5875e2018-05-02 18:47:29 +08002200
Peter Xu0c267812020-10-21 17:27:19 -04002201 /*
2202 * After a postcopy recovery, the source should have lost the postcopy
2203 * queue, or potentially the requested pages could have been lost during
2204 * the network down phase. Let's re-sync with the source VM by re-sending
2205 * all the pending pages that we eagerly need, so these threads won't get
2206 * blocked too long due to the recovery.
2207 *
2208 * Without this procedure, the faulted destination VM threads (waiting for
2209 * page requests right before the postcopy is interrupted) can keep hanging
2210 * until the pages are sent by the source during the background copying of
2211 * pages, or another thread faulted on the same address accidentally.
2212 */
2213 migrate_send_rp_req_pages_pending(mis);
2214
Peter Xu5e773432020-11-02 10:30:10 -05002215 /*
2216 * It's time to switch state and release the fault thread to continue
2217 * service page faults. Note that this should be explicitly after the
2218 * above call to migrate_send_rp_req_pages_pending(). In short:
2219 * migrate_send_rp_message_req_pages() is not thread safe, yet.
2220 */
2221 qemu_sem_post(&mis->postcopy_pause_sem_fault);
2222
Peter Xu60bb3c52022-07-07 14:55:06 -04002223 if (migrate_postcopy_preempt()) {
Peter Xu5655aab2023-02-08 15:28:13 -05002224 /*
2225 * The preempt channel will be created in async manner, now let's
2226 * wait for it and make sure it's created.
2227 */
2228 qemu_sem_wait(&mis->postcopy_qemufile_dst_done);
Peter Xu60bb3c52022-07-07 14:55:06 -04002229 assert(mis->postcopy_qemufile_dst);
2230 /* Kick the fast ram load thread too */
2231 qemu_sem_post(&mis->postcopy_pause_sem_fast_load);
2232 }
2233
Peter Xu3f5875e2018-05-02 18:47:29 +08002234 return 0;
2235}
2236
Dr. David Alan Gilbertc76ca182015-11-05 18:10:45 +00002237/**
Dr. David Alan Gilbert11cf1d92015-11-05 18:10:53 +00002238 * Immediately following this command is a blob of data containing an embedded
2239 * chunk of migration stream; read it and load it.
Dr. David Alan Gilbertc76ca182015-11-05 18:10:45 +00002240 *
Dr. David Alan Gilbert11cf1d92015-11-05 18:10:53 +00002241 * @mis: Incoming state
2242 * @length: Length of packaged data to read
2243 *
2244 * Returns: Negative values on error
2245 *
2246 */
2247static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
2248{
2249 int ret;
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002250 size_t length;
2251 QIOChannelBuffer *bioc;
Dr. David Alan Gilbert11cf1d92015-11-05 18:10:53 +00002252
2253 length = qemu_get_be32(mis->from_src_file);
2254 trace_loadvm_handle_cmd_packaged(length);
2255
2256 if (length > MAX_VM_CMD_PACKAGED_SIZE) {
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002257 error_report("Unreasonably large packaged state: %zu", length);
Dr. David Alan Gilbert11cf1d92015-11-05 18:10:53 +00002258 return -1;
2259 }
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002260
2261 bioc = qio_channel_buffer_new(length);
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01002262 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-loadvm-buffer");
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002263 ret = qemu_get_buffer(mis->from_src_file,
2264 bioc->data,
2265 length);
Dr. David Alan Gilbert11cf1d92015-11-05 18:10:53 +00002266 if (ret != length) {
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002267 object_unref(OBJECT(bioc));
2268 error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu",
Markus Armbruster9af9e0f2015-12-18 16:35:19 +01002269 ret, length);
Dr. David Alan Gilbert11cf1d92015-11-05 18:10:53 +00002270 return (ret < 0) ? ret : -EAGAIN;
2271 }
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002272 bioc->usage += length;
Dr. David Alan Gilbert11cf1d92015-11-05 18:10:53 +00002273 trace_loadvm_handle_cmd_packaged_received(ret);
2274
Daniel P. Berrangé77ef2dc2022-06-20 12:02:05 +01002275 QEMUFile *packf = qemu_file_new_input(QIO_CHANNEL(bioc));
Dr. David Alan Gilbert11cf1d92015-11-05 18:10:53 +00002276
2277 ret = qemu_loadvm_state_main(packf, mis);
2278 trace_loadvm_handle_cmd_packaged_main(ret);
2279 qemu_fclose(packf);
Daniel P. Berrange61b67d42016-04-27 11:05:01 +01002280 object_unref(OBJECT(bioc));
Dr. David Alan Gilbert11cf1d92015-11-05 18:10:53 +00002281
2282 return ret;
2283}
2284
2285/*
Peter Xuf25d4222018-05-02 18:47:27 +08002286 * Handle request that source requests for recved_bitmap on
2287 * destination. Payload format:
2288 *
2289 * len (1 byte) + ramblock_name (<255 bytes)
2290 */
2291static int loadvm_handle_recv_bitmap(MigrationIncomingState *mis,
2292 uint16_t len)
2293{
2294 QEMUFile *file = mis->from_src_file;
2295 RAMBlock *rb;
2296 char block_name[256];
2297 size_t cnt;
2298
2299 cnt = qemu_get_counted_string(file, block_name);
2300 if (!cnt) {
2301 error_report("%s: failed to read block name", __func__);
2302 return -EINVAL;
2303 }
2304
2305 /* Validate before using the data */
2306 if (qemu_file_get_error(file)) {
2307 return qemu_file_get_error(file);
2308 }
2309
2310 if (len != cnt + 1) {
2311 error_report("%s: invalid payload length (%d)", __func__, len);
2312 return -EINVAL;
2313 }
2314
2315 rb = qemu_ram_block_by_name(block_name);
2316 if (!rb) {
2317 error_report("%s: block '%s' not found", __func__, block_name);
2318 return -EINVAL;
2319 }
2320
Peter Xua335deb2018-05-02 18:47:28 +08002321 migrate_send_rp_recv_bitmap(mis, block_name);
Peter Xuf25d4222018-05-02 18:47:27 +08002322
2323 trace_loadvm_handle_recv_bitmap(block_name);
2324
2325 return 0;
2326}
2327
Zhang Chenaad555c2018-09-03 12:38:47 +08002328static int loadvm_process_enable_colo(MigrationIncomingState *mis)
2329{
David Hildenbrand18b1d3c2020-06-26 09:22:36 +02002330 int ret = migration_incoming_enable_colo();
2331
2332 if (!ret) {
2333 ret = colo_init_ram_cache();
2334 if (ret) {
2335 migration_incoming_disable_colo();
2336 }
2337 }
2338 return ret;
Zhang Chenaad555c2018-09-03 12:38:47 +08002339}
2340
Peter Xuf25d4222018-05-02 18:47:27 +08002341/*
Dr. David Alan Gilbert11cf1d92015-11-05 18:10:53 +00002342 * Process an incoming 'QEMU_VM_COMMAND'
2343 * 0 just a normal return
2344 * LOADVM_QUIT All good, but exit the loop
2345 * <0 Error
Dr. David Alan Gilbertc76ca182015-11-05 18:10:45 +00002346 */
2347static int loadvm_process_command(QEMUFile *f)
2348{
Dr. David Alan Gilbert2e377012015-11-05 18:10:46 +00002349 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilbertc76ca182015-11-05 18:10:45 +00002350 uint16_t cmd;
2351 uint16_t len;
Dr. David Alan Gilbert2e377012015-11-05 18:10:46 +00002352 uint32_t tmp32;
Dr. David Alan Gilbertc76ca182015-11-05 18:10:45 +00002353
2354 cmd = qemu_get_be16(f);
2355 len = qemu_get_be16(f);
2356
Peter Xu7a9ddfb2018-02-08 18:31:05 +08002357 /* Check validity before continue processing of cmds */
2358 if (qemu_file_get_error(f)) {
2359 return qemu_file_get_error(f);
2360 }
2361
Dr. David Alan Gilbertc76ca182015-11-05 18:10:45 +00002362 if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) {
2363 error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len);
2364 return -EINVAL;
2365 }
2366
Peter Xua7060ba2022-03-01 16:39:01 +08002367 trace_loadvm_process_command(mig_cmd_args[cmd].name, len);
2368
Dr. David Alan Gilbertc76ca182015-11-05 18:10:45 +00002369 if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) {
2370 error_report("%s received with bad length - expecting %zu, got %d",
2371 mig_cmd_args[cmd].name,
2372 (size_t)mig_cmd_args[cmd].len, len);
2373 return -ERANGE;
2374 }
2375
2376 switch (cmd) {
Dr. David Alan Gilbert2e377012015-11-05 18:10:46 +00002377 case MIG_CMD_OPEN_RETURN_PATH:
2378 if (mis->to_src_file) {
2379 error_report("CMD_OPEN_RETURN_PATH called when RP already open");
2380 /* Not really a problem, so don't give up */
2381 return 0;
2382 }
2383 mis->to_src_file = qemu_file_get_return_path(f);
2384 if (!mis->to_src_file) {
2385 error_report("CMD_OPEN_RETURN_PATH failed");
2386 return -1;
2387 }
Avihai Horon1b4adb12023-06-21 14:11:55 +03002388
2389 /*
2390 * Switchover ack is enabled but no device uses it, so send an ACK to
2391 * source that it's OK to switchover. Do it here, after return path has
2392 * been created.
2393 */
2394 if (migrate_switchover_ack() && !mis->switchover_ack_pending_num) {
2395 int ret = migrate_send_rp_switchover_ack(mis);
2396 if (ret) {
2397 error_report(
2398 "Could not send switchover ack RP MSG, err %d (%s)", ret,
2399 strerror(-ret));
2400 return ret;
2401 }
2402 }
Dr. David Alan Gilbert2e377012015-11-05 18:10:46 +00002403 break;
2404
2405 case MIG_CMD_PING:
2406 tmp32 = qemu_get_be32(f);
2407 trace_loadvm_process_command_ping(tmp32);
2408 if (!mis->to_src_file) {
2409 error_report("CMD_PING (0x%x) received with no return path",
2410 tmp32);
2411 return -1;
2412 }
Dr. David Alan Gilbert6decec92015-11-05 18:10:47 +00002413 migrate_send_rp_pong(mis, tmp32);
Dr. David Alan Gilbert2e377012015-11-05 18:10:46 +00002414 break;
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002415
Dr. David Alan Gilbert11cf1d92015-11-05 18:10:53 +00002416 case MIG_CMD_PACKAGED:
2417 return loadvm_handle_cmd_packaged(mis);
2418
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002419 case MIG_CMD_POSTCOPY_ADVISE:
Greg Kurz875fcd02018-02-06 12:23:30 +01002420 return loadvm_postcopy_handle_advise(mis, len);
Dr. David Alan Gilbert093e3c42015-11-05 18:10:52 +00002421
2422 case MIG_CMD_POSTCOPY_LISTEN:
2423 return loadvm_postcopy_handle_listen(mis);
2424
2425 case MIG_CMD_POSTCOPY_RUN:
2426 return loadvm_postcopy_handle_run(mis);
2427
2428 case MIG_CMD_POSTCOPY_RAM_DISCARD:
2429 return loadvm_postcopy_ram_handle_discard(mis, len);
Peter Xuf25d4222018-05-02 18:47:27 +08002430
Peter Xu3f5875e2018-05-02 18:47:29 +08002431 case MIG_CMD_POSTCOPY_RESUME:
2432 return loadvm_postcopy_handle_resume(mis);
2433
Peter Xuf25d4222018-05-02 18:47:27 +08002434 case MIG_CMD_RECV_BITMAP:
2435 return loadvm_handle_recv_bitmap(mis, len);
Zhang Chenaad555c2018-09-03 12:38:47 +08002436
2437 case MIG_CMD_ENABLE_COLO:
2438 return loadvm_process_enable_colo(mis);
Dr. David Alan Gilbertc76ca182015-11-05 18:10:45 +00002439 }
2440
2441 return 0;
2442}
2443
Dr. David Alan Gilbert59f39a42015-07-02 09:22:03 +01002444/*
2445 * Read a footer off the wire and check that it matches the expected section
2446 *
2447 * Returns: true if the footer was good
2448 * false if there is a problem (and calls error_report to say why)
2449 */
Juan Quintela0f42f652017-05-24 09:09:58 +02002450static bool check_section_footer(QEMUFile *f, SaveStateEntry *se)
Dr. David Alan Gilbert59f39a42015-07-02 09:22:03 +01002451{
Peter Xu7a9ddfb2018-02-08 18:31:05 +08002452 int ret;
Dr. David Alan Gilbert59f39a42015-07-02 09:22:03 +01002453 uint8_t read_mark;
2454 uint32_t read_section_id;
2455
Peter Xu15c38502017-06-27 12:10:17 +08002456 if (!migrate_get_current()->send_section_footer) {
Dr. David Alan Gilbert59f39a42015-07-02 09:22:03 +01002457 /* No footer to check */
2458 return true;
2459 }
2460
2461 read_mark = qemu_get_byte(f);
2462
Peter Xu7a9ddfb2018-02-08 18:31:05 +08002463 ret = qemu_file_get_error(f);
2464 if (ret) {
2465 error_report("%s: Read section footer failed: %d",
2466 __func__, ret);
2467 return false;
2468 }
2469
Dr. David Alan Gilbert59f39a42015-07-02 09:22:03 +01002470 if (read_mark != QEMU_VM_SECTION_FOOTER) {
Juan Quintela0f42f652017-05-24 09:09:58 +02002471 error_report("Missing section footer for %s", se->idstr);
Dr. David Alan Gilbert59f39a42015-07-02 09:22:03 +01002472 return false;
2473 }
2474
2475 read_section_id = qemu_get_be32(f);
Juan Quintela0f42f652017-05-24 09:09:58 +02002476 if (read_section_id != se->load_section_id) {
Dr. David Alan Gilbert59f39a42015-07-02 09:22:03 +01002477 error_report("Mismatched section id in footer for %s -"
2478 " read 0x%x expected 0x%x",
Juan Quintela0f42f652017-05-24 09:09:58 +02002479 se->idstr, read_section_id, se->load_section_id);
Dr. David Alan Gilbert59f39a42015-07-02 09:22:03 +01002480 return false;
2481 }
2482
2483 /* All good */
2484 return true;
2485}
2486
zhanghailiangfb3520a2016-01-15 11:37:43 +08002487static int
2488qemu_loadvm_section_start_full(QEMUFile *f, MigrationIncomingState *mis)
2489{
2490 uint32_t instance_id, version_id, section_id;
2491 SaveStateEntry *se;
zhanghailiangfb3520a2016-01-15 11:37:43 +08002492 char idstr[256];
2493 int ret;
2494
2495 /* Read section start */
2496 section_id = qemu_get_be32(f);
2497 if (!qemu_get_counted_string(f, idstr)) {
2498 error_report("Unable to read ID string for section %u",
2499 section_id);
2500 return -EINVAL;
2501 }
2502 instance_id = qemu_get_be32(f);
2503 version_id = qemu_get_be32(f);
2504
Peter Xu7a9ddfb2018-02-08 18:31:05 +08002505 ret = qemu_file_get_error(f);
2506 if (ret) {
2507 error_report("%s: Failed to read instance/version ID: %d",
2508 __func__, ret);
2509 return ret;
2510 }
2511
zhanghailiangfb3520a2016-01-15 11:37:43 +08002512 trace_qemu_loadvm_state_section_startfull(section_id, idstr,
2513 instance_id, version_id);
2514 /* Find savevm section */
2515 se = find_se(idstr, instance_id);
2516 if (se == NULL) {
Peter Xu93062e22019-10-16 10:29:31 +08002517 error_report("Unknown savevm section or instance '%s' %"PRIu32". "
Jose Ricardo Ziviani827beac2018-09-03 13:26:13 -03002518 "Make sure that your current VM setup matches your "
2519 "saved VM setup, including any hotplugged devices",
zhanghailiangfb3520a2016-01-15 11:37:43 +08002520 idstr, instance_id);
2521 return -EINVAL;
2522 }
2523
2524 /* Validate version */
2525 if (version_id > se->version_id) {
2526 error_report("savevm: unsupported version %d for '%s' v%d",
2527 version_id, idstr, se->version_id);
2528 return -EINVAL;
2529 }
Juan Quintela0f42f652017-05-24 09:09:58 +02002530 se->load_version_id = version_id;
2531 se->load_section_id = section_id;
zhanghailiangfb3520a2016-01-15 11:37:43 +08002532
Wen Congyang88c16562016-06-03 17:58:34 +08002533 /* Validate if it is a device's state */
2534 if (xen_enabled() && se->is_ram) {
2535 error_report("loadvm: %s RAM loading not allowed on Xen", idstr);
2536 return -EINVAL;
2537 }
2538
Juan Quintela3a011c22017-05-24 09:28:47 +02002539 ret = vmstate_load(f, se);
zhanghailiangfb3520a2016-01-15 11:37:43 +08002540 if (ret < 0) {
Peter Xu93062e22019-10-16 10:29:31 +08002541 error_report("error while loading state for instance 0x%"PRIx32" of"
zhanghailiangfb3520a2016-01-15 11:37:43 +08002542 " device '%s'", instance_id, idstr);
2543 return ret;
2544 }
Juan Quintela0f42f652017-05-24 09:09:58 +02002545 if (!check_section_footer(f, se)) {
zhanghailiangfb3520a2016-01-15 11:37:43 +08002546 return -EINVAL;
2547 }
2548
2549 return 0;
2550}
2551
2552static int
2553qemu_loadvm_section_part_end(QEMUFile *f, MigrationIncomingState *mis)
2554{
2555 uint32_t section_id;
Juan Quintela0f42f652017-05-24 09:09:58 +02002556 SaveStateEntry *se;
zhanghailiangfb3520a2016-01-15 11:37:43 +08002557 int ret;
2558
2559 section_id = qemu_get_be32(f);
2560
Peter Xu7a9ddfb2018-02-08 18:31:05 +08002561 ret = qemu_file_get_error(f);
2562 if (ret) {
2563 error_report("%s: Failed to read section ID: %d",
2564 __func__, ret);
2565 return ret;
2566 }
2567
zhanghailiangfb3520a2016-01-15 11:37:43 +08002568 trace_qemu_loadvm_state_section_partend(section_id);
Juan Quintela0f42f652017-05-24 09:09:58 +02002569 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2570 if (se->load_section_id == section_id) {
zhanghailiangfb3520a2016-01-15 11:37:43 +08002571 break;
2572 }
2573 }
Juan Quintela0f42f652017-05-24 09:09:58 +02002574 if (se == NULL) {
zhanghailiangfb3520a2016-01-15 11:37:43 +08002575 error_report("Unknown savevm section %d", section_id);
2576 return -EINVAL;
2577 }
2578
Juan Quintela3a011c22017-05-24 09:28:47 +02002579 ret = vmstate_load(f, se);
zhanghailiangfb3520a2016-01-15 11:37:43 +08002580 if (ret < 0) {
2581 error_report("error while loading state section id %d(%s)",
Juan Quintela0f42f652017-05-24 09:09:58 +02002582 section_id, se->idstr);
zhanghailiangfb3520a2016-01-15 11:37:43 +08002583 return ret;
2584 }
Juan Quintela0f42f652017-05-24 09:09:58 +02002585 if (!check_section_footer(f, se)) {
zhanghailiangfb3520a2016-01-15 11:37:43 +08002586 return -EINVAL;
2587 }
2588
2589 return 0;
2590}
2591
Wei Yang16015d32019-04-24 08:47:00 +08002592static int qemu_loadvm_state_header(QEMUFile *f)
2593{
2594 unsigned int v;
2595 int ret;
2596
2597 v = qemu_get_be32(f);
2598 if (v != QEMU_VM_FILE_MAGIC) {
2599 error_report("Not a migration stream");
2600 return -EINVAL;
2601 }
2602
2603 v = qemu_get_be32(f);
2604 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2605 error_report("SaveVM v2 format is obsolete and don't work anymore");
2606 return -ENOTSUP;
2607 }
2608 if (v != QEMU_VM_FILE_VERSION) {
2609 error_report("Unsupported migration stream version");
2610 return -ENOTSUP;
2611 }
2612
2613 if (migrate_get_current()->send_configuration) {
2614 if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) {
2615 error_report("Configuration section missing");
2616 qemu_loadvm_state_cleanup();
2617 return -EINVAL;
2618 }
2619 ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0);
2620
2621 if (ret) {
2622 qemu_loadvm_state_cleanup();
2623 return ret;
2624 }
2625 }
2626 return 0;
2627}
2628
Avihai Horon1b4adb12023-06-21 14:11:55 +03002629static void qemu_loadvm_state_switchover_ack_needed(MigrationIncomingState *mis)
2630{
2631 SaveStateEntry *se;
2632
2633 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2634 if (!se->ops || !se->ops->switchover_ack_needed) {
2635 continue;
2636 }
2637
2638 if (se->ops->switchover_ack_needed(se->opaque)) {
2639 mis->switchover_ack_pending_num++;
2640 }
2641 }
2642
2643 trace_loadvm_state_switchover_ack_needed(mis->switchover_ack_pending_num);
2644}
2645
Juan Quintelaacb5ea82017-06-28 11:52:26 +02002646static int qemu_loadvm_state_setup(QEMUFile *f)
2647{
2648 SaveStateEntry *se;
2649 int ret;
2650
2651 trace_loadvm_state_setup();
2652 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2653 if (!se->ops || !se->ops->load_setup) {
2654 continue;
2655 }
Wei Yangcea3b4c2019-08-19 11:28:04 +08002656 if (se->ops->is_active) {
Juan Quintelaacb5ea82017-06-28 11:52:26 +02002657 if (!se->ops->is_active(se->opaque)) {
2658 continue;
2659 }
2660 }
2661
2662 ret = se->ops->load_setup(f, se->opaque);
2663 if (ret < 0) {
2664 qemu_file_set_error(f, ret);
2665 error_report("Load state of device %s failed", se->idstr);
2666 return ret;
2667 }
2668 }
2669 return 0;
2670}
2671
2672void qemu_loadvm_state_cleanup(void)
2673{
2674 SaveStateEntry *se;
2675
2676 trace_loadvm_state_cleanup();
2677 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2678 if (se->ops && se->ops->load_cleanup) {
2679 se->ops->load_cleanup(se->opaque);
2680 }
2681 }
2682}
2683
Peter Xub411b842018-05-02 18:47:20 +08002684/* Return true if we should continue the migration, or false. */
2685static bool postcopy_pause_incoming(MigrationIncomingState *mis)
2686{
Peter Xu77dadc32022-03-01 16:39:04 +08002687 int i;
2688
Peter Xub411b842018-05-02 18:47:20 +08002689 trace_postcopy_pause_incoming();
2690
Vladimir Sementsov-Ogievskiyee647222020-07-27 22:42:32 +03002691 assert(migrate_postcopy_ram());
2692
Peter Xu39675ff2021-07-22 13:58:41 -04002693 /*
2694 * Unregister yank with either from/to src would work, since ioc behind it
2695 * is the same
2696 */
2697 migration_ioc_unregister_yank_from_file(mis->from_src_file);
2698
Peter Xub411b842018-05-02 18:47:20 +08002699 assert(mis->from_src_file);
2700 qemu_file_shutdown(mis->from_src_file);
2701 qemu_fclose(mis->from_src_file);
2702 mis->from_src_file = NULL;
2703
2704 assert(mis->to_src_file);
2705 qemu_file_shutdown(mis->to_src_file);
2706 qemu_mutex_lock(&mis->rp_mutex);
2707 qemu_fclose(mis->to_src_file);
2708 mis->to_src_file = NULL;
2709 qemu_mutex_unlock(&mis->rp_mutex);
2710
Peter Xu60bb3c52022-07-07 14:55:06 -04002711 /*
2712 * NOTE: this must happen before reset the PostcopyTmpPages below,
2713 * otherwise it's racy to reset those fields when the fast load thread
2714 * can be accessing it in parallel.
2715 */
2716 if (mis->postcopy_qemufile_dst) {
2717 qemu_file_shutdown(mis->postcopy_qemufile_dst);
2718 /* Take the mutex to make sure the fast ram load thread halted */
2719 qemu_mutex_lock(&mis->postcopy_prio_thread_mutex);
2720 migration_ioc_unregister_yank_from_file(mis->postcopy_qemufile_dst);
2721 qemu_fclose(mis->postcopy_qemufile_dst);
2722 mis->postcopy_qemufile_dst = NULL;
2723 qemu_mutex_unlock(&mis->postcopy_prio_thread_mutex);
2724 }
2725
Peter Xueed1cc72018-06-27 21:22:43 +08002726 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2727 MIGRATION_STATUS_POSTCOPY_PAUSED);
2728
Peter Xu3a7804c2018-05-02 18:47:22 +08002729 /* Notify the fault thread for the invalidated file handle */
2730 postcopy_fault_thread_notify(mis);
2731
Peter Xu36f62f12022-07-07 14:55:02 -04002732 /*
2733 * If network is interrupted, any temp page we received will be useless
2734 * because we didn't mark them as "received" in receivedmap. After a
2735 * proper recovery later (which will sync src dirty bitmap with receivedmap
2736 * on dest) these cached small pages will be resent again.
2737 */
2738 for (i = 0; i < mis->postcopy_channels; i++) {
2739 postcopy_temp_page_reset(&mis->postcopy_tmp_pages[i]);
2740 }
2741
Peter Xub411b842018-05-02 18:47:20 +08002742 error_report("Detected IO failure for postcopy. "
2743 "Migration paused.");
2744
2745 while (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2746 qemu_sem_wait(&mis->postcopy_pause_sem_dst);
2747 }
2748
2749 trace_postcopy_pause_incoming_continued();
2750
2751 return true;
2752}
2753
Zhang Chen3f6df992018-09-03 12:38:54 +08002754int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis)
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002755{
2756 uint8_t section_type;
Dr. David Alan Gilbertccb783c2016-09-23 20:14:03 +01002757 int ret = 0;
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002758
Peter Xub411b842018-05-02 18:47:20 +08002759retry:
Peter Xu7a9ddfb2018-02-08 18:31:05 +08002760 while (true) {
2761 section_type = qemu_get_byte(f);
2762
Peter Xu60bb3c52022-07-07 14:55:06 -04002763 ret = qemu_file_get_error_obj_any(f, mis->postcopy_qemufile_dst, NULL);
2764 if (ret) {
Peter Xu7a9ddfb2018-02-08 18:31:05 +08002765 break;
2766 }
2767
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002768 trace_qemu_loadvm_state_section(section_type);
2769 switch (section_type) {
2770 case QEMU_VM_SECTION_START:
2771 case QEMU_VM_SECTION_FULL:
zhanghailiangfb3520a2016-01-15 11:37:43 +08002772 ret = qemu_loadvm_section_start_full(f, mis);
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002773 if (ret < 0) {
Dr. David Alan Gilbertccb783c2016-09-23 20:14:03 +01002774 goto out;
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002775 }
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002776 break;
2777 case QEMU_VM_SECTION_PART:
2778 case QEMU_VM_SECTION_END:
zhanghailiangfb3520a2016-01-15 11:37:43 +08002779 ret = qemu_loadvm_section_part_end(f, mis);
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002780 if (ret < 0) {
Dr. David Alan Gilbertccb783c2016-09-23 20:14:03 +01002781 goto out;
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002782 }
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002783 break;
2784 case QEMU_VM_COMMAND:
2785 ret = loadvm_process_command(f);
2786 trace_qemu_loadvm_state_section_command(ret);
Wei Yang4695ce32019-07-18 14:42:57 +08002787 if ((ret < 0) || (ret == LOADVM_QUIT)) {
Dr. David Alan Gilbertccb783c2016-09-23 20:14:03 +01002788 goto out;
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002789 }
2790 break;
Peter Xu7a9ddfb2018-02-08 18:31:05 +08002791 case QEMU_VM_EOF:
2792 /* This is the end of migration */
2793 goto out;
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002794 default:
2795 error_report("Unknown savevm section type %d", section_type);
Dr. David Alan Gilbertccb783c2016-09-23 20:14:03 +01002796 ret = -EINVAL;
2797 goto out;
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002798 }
2799 }
2800
Dr. David Alan Gilbertccb783c2016-09-23 20:14:03 +01002801out:
2802 if (ret < 0) {
2803 qemu_file_set_error(f, ret);
Peter Xub411b842018-05-02 18:47:20 +08002804
Vladimir Sementsov-Ogievskiyee647222020-07-27 22:42:32 +03002805 /* Cancel bitmaps incoming regardless of recovery */
2806 dirty_bitmap_mig_cancel_incoming();
2807
Peter Xub411b842018-05-02 18:47:20 +08002808 /*
Peter Xufd037a62018-07-10 17:18:54 +08002809 * If we are during an active postcopy, then we pause instead
2810 * of bail out to at least keep the VM's dirty data. Note
2811 * that POSTCOPY_INCOMING_LISTENING stage is still not enough,
2812 * during which we're still receiving device states and we
2813 * still haven't yet started the VM on destination.
Vladimir Sementsov-Ogievskiyee647222020-07-27 22:42:32 +03002814 *
2815 * Only RAM postcopy supports recovery. Still, if RAM postcopy is
2816 * enabled, canceled bitmaps postcopy will not affect RAM postcopy
2817 * recovering.
Peter Xub411b842018-05-02 18:47:20 +08002818 */
2819 if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING &&
Vladimir Sementsov-Ogievskiyee647222020-07-27 22:42:32 +03002820 migrate_postcopy_ram() && postcopy_pause_incoming(mis)) {
Peter Xub411b842018-05-02 18:47:20 +08002821 /* Reset f to point to the newly created channel */
2822 f = mis->from_src_file;
2823 goto retry;
2824 }
Dr. David Alan Gilbertccb783c2016-09-23 20:14:03 +01002825 }
2826 return ret;
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002827}
2828
aliguoria672b462008-11-11 21:33:36 +00002829int qemu_loadvm_state(QEMUFile *f)
2830{
Dr. David Alan Gilbert1a8f46f2015-05-21 13:24:16 +01002831 MigrationIncomingState *mis = migration_incoming_get_current();
Dr. David Alan Gilbert0457d072015-01-21 10:14:49 +00002832 Error *local_err = NULL;
aliguoria672b462008-11-11 21:33:36 +00002833 int ret;
2834
Dr. David Alan Gilbert0457d072015-01-21 10:14:49 +00002835 if (qemu_savevm_state_blocked(&local_err)) {
zhanghailiang19867542015-03-04 18:25:54 +08002836 error_report_err(local_err);
Alex Williamsondc912122011-01-11 14:39:43 -07002837 return -EINVAL;
2838 }
2839
Wei Yang16015d32019-04-24 08:47:00 +08002840 ret = qemu_loadvm_state_header(f);
2841 if (ret) {
2842 return ret;
Juan Quintela61964c22015-05-13 18:17:43 +02002843 }
2844
Wei Yang9e14b842019-04-24 08:46:59 +08002845 if (qemu_loadvm_state_setup(f) != 0) {
2846 return -EINVAL;
2847 }
2848
Avihai Horon1b4adb12023-06-21 14:11:55 +03002849 if (migrate_switchover_ack()) {
2850 qemu_loadvm_state_switchover_ack_needed(mis);
2851 }
2852
David Gibson75e972d2017-05-26 14:46:28 +10002853 cpu_synchronize_all_pre_loadvm();
2854
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002855 ret = qemu_loadvm_state_main(f, mis);
2856 qemu_event_set(&mis->main_thread_load_event);
aliguoria672b462008-11-11 21:33:36 +00002857
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002858 trace_qemu_loadvm_state_post_main(ret);
aliguoria672b462008-11-11 21:33:36 +00002859
Dr. David Alan Gilbertc76201a2015-11-05 18:11:18 +00002860 if (mis->have_listen_thread) {
2861 /* Listen thread still going, can't clean up yet */
2862 return ret;
2863 }
2864
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002865 if (ret == 0) {
2866 ret = qemu_file_get_error(f);
aliguoria672b462008-11-11 21:33:36 +00002867 }
2868
Alexander Graf1925ceb2015-02-23 13:56:41 +01002869 /*
2870 * Try to read in the VMDESC section as well, so that dumping tools that
2871 * intercept our migration stream have the chance to see it.
2872 */
Alexander Graf1925ceb2015-02-23 13:56:41 +01002873
Dr. David Alan Gilbert1aca9a52015-06-23 17:34:35 +01002874 /* We've got to be careful; if we don't read the data and just shut the fd
2875 * then the sender can error if we close while it's still sending.
2876 * We also mustn't read data that isn't there; some transports (RDMA)
2877 * will stall waiting for that data when the source has already closed.
2878 */
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002879 if (ret == 0 && should_send_vmdesc()) {
Dr. David Alan Gilbert1aca9a52015-06-23 17:34:35 +01002880 uint8_t *buf;
2881 uint32_t size;
Dr. David Alan Gilbert7b89bf22015-11-05 18:10:50 +00002882 uint8_t section_type = qemu_get_byte(f);
Dr. David Alan Gilbert1aca9a52015-06-23 17:34:35 +01002883
2884 if (section_type != QEMU_VM_VMDESCRIPTION) {
2885 error_report("Expected vmdescription section, but got %d",
2886 section_type);
2887 /*
2888 * It doesn't seem worth failing at this point since
2889 * we apparently have an otherwise valid VM state
2890 */
2891 } else {
2892 buf = g_malloc(0x1000);
2893 size = qemu_get_be32(f);
2894
2895 while (size > 0) {
2896 uint32_t read_chunk = MIN(size, 0x1000);
2897 qemu_get_buffer(f, buf, read_chunk);
2898 size -= read_chunk;
2899 }
2900 g_free(buf);
Alexander Graf1925ceb2015-02-23 13:56:41 +01002901 }
Alexander Graf1925ceb2015-02-23 13:56:41 +01002902 }
2903
Juan Quintelaacb5ea82017-06-28 11:52:26 +02002904 qemu_loadvm_state_cleanup();
Jan Kiszkaea375f92010-03-01 19:10:30 +01002905 cpu_synchronize_all_post_init();
2906
aliguoria672b462008-11-11 21:33:36 +00002907 return ret;
2908}
2909
Zhang Chen3f6df992018-09-03 12:38:54 +08002910int qemu_load_device_state(QEMUFile *f)
2911{
2912 MigrationIncomingState *mis = migration_incoming_get_current();
2913 int ret;
2914
2915 /* Load QEMU_VM_SECTION_FULL section */
2916 ret = qemu_loadvm_state_main(f, mis);
2917 if (ret < 0) {
2918 error_report("Failed to load device state: %d", ret);
2919 return ret;
2920 }
2921
2922 cpu_synchronize_all_post_init();
2923 return 0;
2924}
2925
Avihai Horon1b4adb12023-06-21 14:11:55 +03002926int qemu_loadvm_approve_switchover(void)
2927{
2928 MigrationIncomingState *mis = migration_incoming_get_current();
2929
2930 if (!mis->switchover_ack_pending_num) {
2931 return -EINVAL;
2932 }
2933
2934 mis->switchover_ack_pending_num--;
2935 trace_loadvm_approve_switchover(mis->switchover_ack_pending_num);
2936
2937 if (mis->switchover_ack_pending_num) {
2938 return 0;
2939 }
2940
2941 return migrate_send_rp_switchover_ack(mis);
2942}
2943
Daniel P. Berrangéf1a9fcd2021-02-04 12:48:30 +00002944bool save_snapshot(const char *name, bool overwrite, const char *vmstate,
2945 bool has_devices, strList *devices, Error **errp)
aliguoria672b462008-11-11 21:33:36 +00002946{
Daniel P. Berrangée26f98e2021-02-04 12:48:23 +00002947 BlockDriverState *bs;
Tuguoyi80ef0582020-12-08 14:53:35 +08002948 QEMUSnapshotInfo sn1, *sn = &sn1;
Denis V. Lunev66270a42020-07-03 19:11:24 +03002949 int ret = -1, ret2;
aliguoria672b462008-11-11 21:33:36 +00002950 QEMUFile *f;
2951 int saved_vm_running;
Kevin Wolfc2c9a462011-11-16 11:35:54 +01002952 uint64_t vm_state_size;
Daniel P. Berrangé85cd1cc2021-04-30 12:59:06 +01002953 g_autoptr(GDateTime) now = g_date_time_new_now_local();
Denis V. Lunev79b3c122015-11-19 09:42:11 +03002954 AioContext *aio_context;
aliguoria672b462008-11-11 21:33:36 +00002955
Emanuele Giuseppe Esposito6b573ef2022-03-03 10:16:07 -05002956 GLOBAL_STATE_CODE();
2957
Paolo Bonzini5aaac462018-11-14 10:48:00 +01002958 if (migration_is_blocked(errp)) {
Philippe Mathieu-Daudé7ea14df2021-02-04 12:48:24 +00002959 return false;
Paolo Bonzini5aaac462018-11-14 10:48:00 +01002960 }
2961
Pavel Dovgalyuk377b21c2018-02-27 12:52:14 +03002962 if (!replay_can_snapshot()) {
Markus Armbruster4dd32b32018-10-17 10:26:36 +02002963 error_setg(errp, "Record/replay does not allow making snapshot "
2964 "right now. Try once more later.");
Philippe Mathieu-Daudé7ea14df2021-02-04 12:48:24 +00002965 return false;
Pavel Dovgalyuk377b21c2018-02-27 12:52:14 +03002966 }
2967
Daniel P. Berrangéf1a9fcd2021-02-04 12:48:30 +00002968 if (!bdrv_all_can_snapshot(has_devices, devices, errp)) {
Philippe Mathieu-Daudé7ea14df2021-02-04 12:48:24 +00002969 return false;
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002970 }
2971
Denis V. Lunev0b461602015-11-19 09:42:09 +03002972 /* Delete old snapshots of the same name */
Pavel Dovgalyukac8c19b2017-01-24 10:17:41 +03002973 if (name) {
Daniel P. Berrangéf781f842021-02-04 12:48:29 +00002974 if (overwrite) {
Daniel P. Berrangéf1a9fcd2021-02-04 12:48:30 +00002975 if (bdrv_all_delete_snapshot(name, has_devices,
2976 devices, errp) < 0) {
Daniel P. Berrangéf781f842021-02-04 12:48:29 +00002977 return false;
2978 }
2979 } else {
Daniel P. Berrangéf1a9fcd2021-02-04 12:48:30 +00002980 ret2 = bdrv_all_has_snapshot(name, has_devices, devices, errp);
Daniel P. Berrangéf781f842021-02-04 12:48:29 +00002981 if (ret2 < 0) {
2982 return false;
2983 }
2984 if (ret2 == 1) {
2985 error_setg(errp,
2986 "Snapshot '%s' already exists in one or more devices",
2987 name);
2988 return false;
2989 }
Pavel Dovgalyukac8c19b2017-01-24 10:17:41 +03002990 }
Denis V. Lunev0b461602015-11-19 09:42:09 +03002991 }
2992
Daniel P. Berrangéf1a9fcd2021-02-04 12:48:30 +00002993 bs = bdrv_all_find_vmstate_bs(vmstate, has_devices, devices, errp);
Denis V. Lunev7cb14482015-11-19 09:42:10 +03002994 if (bs == NULL) {
Philippe Mathieu-Daudé7ea14df2021-02-04 12:48:24 +00002995 return false;
aliguoria672b462008-11-11 21:33:36 +00002996 }
Denis V. Lunev79b3c122015-11-19 09:42:11 +03002997 aio_context = bdrv_get_aio_context(bs);
aliguoria672b462008-11-11 21:33:36 +00002998
Luiz Capitulino13548692011-07-29 15:36:43 -03002999 saved_vm_running = runstate_is_running();
Juan Quintela560d0272015-07-15 09:53:46 +02003000
Vladimir Sementsov-Ogievskiyc33f1822023-05-17 15:37:49 +03003001 global_state_store();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003002 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00003003
Stefan Hajnoczi8649f2f2017-05-22 14:57:04 +01003004 bdrv_drain_all_begin();
3005
Denis V. Lunev79b3c122015-11-19 09:42:11 +03003006 aio_context_acquire(aio_context);
3007
Kevin Wolfcb499fb2009-11-03 17:34:37 +01003008 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00003009
3010 /* fill auxiliary fields */
Daniel P. Berrangé85cd1cc2021-04-30 12:59:06 +01003011 sn->date_sec = g_date_time_to_unix(now);
3012 sn->date_nsec = g_date_time_get_microsecond(now) * 1000;
Alex Blighbc72ad62013-08-21 16:03:08 +01003013 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Pavel Dovgalyukb39847a2020-10-03 20:13:08 +03003014 if (replay_mode != REPLAY_MODE_NONE) {
3015 sn->icount = replay_get_current_icount();
3016 } else {
3017 sn->icount = -1ULL;
3018 }
aliguoria672b462008-11-11 21:33:36 +00003019
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03003020 if (name) {
Tuguoyi80ef0582020-12-08 14:53:35 +08003021 pstrcpy(sn->name, sizeof(sn->name), name);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03003022 } else {
Daniel P. Berrangé85cd1cc2021-04-30 12:59:06 +01003023 g_autofree char *autoname = g_date_time_format(now, "vm-%Y%m%d%H%M%S");
3024 pstrcpy(sn->name, sizeof(sn->name), autoname);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03003025 }
3026
aliguoria672b462008-11-11 21:33:36 +00003027 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02003028 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00003029 if (!f) {
Juan Quintela927d6632017-04-18 18:12:35 +02003030 error_setg(errp, "Could not open VM state file");
aliguoria672b462008-11-11 21:33:36 +00003031 goto the_end;
3032 }
Juan Quintela927d6632017-04-18 18:12:35 +02003033 ret = qemu_savevm_state(f, errp);
Juan Quintelacf786542023-05-30 20:39:23 +02003034 vm_state_size = qemu_file_transferred_noflush(f);
Denis V. Lunev66270a42020-07-03 19:11:24 +03003035 ret2 = qemu_fclose(f);
aliguoria672b462008-11-11 21:33:36 +00003036 if (ret < 0) {
aliguoria672b462008-11-11 21:33:36 +00003037 goto the_end;
3038 }
Denis V. Lunev66270a42020-07-03 19:11:24 +03003039 if (ret2 < 0) {
3040 ret = ret2;
3041 goto the_end;
3042 }
aliguoria672b462008-11-11 21:33:36 +00003043
Stefan Hajnoczi17e2a4a2017-05-22 14:57:03 +01003044 /* The bdrv_all_create_snapshot() call that follows acquires the AioContext
3045 * for itself. BDRV_POLL_WHILE() does not support nested locking because
3046 * it only releases the lock once. Therefore synchronous I/O will deadlock
3047 * unless we release the AioContext before bdrv_all_create_snapshot().
3048 */
3049 aio_context_release(aio_context);
3050 aio_context = NULL;
3051
Daniel P. Berrangéf1a9fcd2021-02-04 12:48:30 +00003052 ret = bdrv_all_create_snapshot(sn, bs, vm_state_size,
3053 has_devices, devices, errp);
Denis V. Luneva9085f92015-11-19 09:42:08 +03003054 if (ret < 0) {
Daniel P. Berrangéf1a9fcd2021-02-04 12:48:30 +00003055 bdrv_all_delete_snapshot(sn->name, has_devices, devices, NULL);
Pavel Dovgalyukac8c19b2017-01-24 10:17:41 +03003056 goto the_end;
aliguoria672b462008-11-11 21:33:36 +00003057 }
3058
Pavel Dovgalyukac8c19b2017-01-24 10:17:41 +03003059 ret = 0;
3060
aliguoria672b462008-11-11 21:33:36 +00003061 the_end:
Stefan Hajnoczi17e2a4a2017-05-22 14:57:03 +01003062 if (aio_context) {
3063 aio_context_release(aio_context);
3064 }
Stefan Hajnoczi8649f2f2017-05-22 14:57:04 +01003065
3066 bdrv_drain_all_end();
3067
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02003068 if (saved_vm_running) {
aliguoria672b462008-11-11 21:33:36 +00003069 vm_start();
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02003070 }
Philippe Mathieu-Daudé7ea14df2021-02-04 12:48:24 +00003071 return ret == 0;
Pavel Dovgalyukac8c19b2017-01-24 10:17:41 +03003072}
3073
Anthony PERARD5d6c5992017-11-16 15:14:19 +00003074void qmp_xen_save_devices_state(const char *filename, bool has_live, bool live,
3075 Error **errp)
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00003076{
3077 QEMUFile *f;
Daniel P. Berrange89258392016-04-27 11:05:08 +01003078 QIOChannelFile *ioc;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00003079 int saved_vm_running;
3080 int ret;
3081
Anthony PERARD5d6c5992017-11-16 15:14:19 +00003082 if (!has_live) {
3083 /* live default to true so old version of Xen tool stack can have a
zhaolichang3a4452d2020-09-17 15:50:21 +08003084 * successful live migration */
Anthony PERARD5d6c5992017-11-16 15:14:19 +00003085 live = true;
3086 }
3087
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00003088 saved_vm_running = runstate_is_running();
3089 vm_stop(RUN_STATE_SAVE_VM);
Anthony PERARDc69adea2015-08-03 15:29:19 +01003090 global_state_store_running();
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00003091
Dov Murikb4deb9bf82020-09-21 09:48:30 +00003092 ioc = qio_channel_file_new_path(filename, O_WRONLY | O_CREAT | O_TRUNC,
3093 0660, errp);
Daniel P. Berrange89258392016-04-27 11:05:08 +01003094 if (!ioc) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00003095 goto the_end;
3096 }
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01003097 qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state");
Daniel P. Berrangé77ef2dc2022-06-20 12:02:05 +01003098 f = qemu_file_new_output(QIO_CHANNEL(ioc));
Ross Lagerwall032b79f2017-11-01 14:25:23 +00003099 object_unref(OBJECT(ioc));
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00003100 ret = qemu_save_device_state(f);
Ross Lagerwall96994fd2018-02-06 16:30:39 +00003101 if (ret < 0 || qemu_fclose(f) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003102 error_setg(errp, QERR_IO_ERROR);
Anthony PERARD5d6c5992017-11-16 15:14:19 +00003103 } else {
3104 /* libxl calls the QMP command "stop" before calling
3105 * "xen-save-devices-state" and in case of migration failure, libxl
3106 * would call "cont".
3107 * So call bdrv_inactivate_all (release locks) here to let the other
zhaolichang3a4452d2020-09-17 15:50:21 +08003108 * side of the migration take control of the images.
Anthony PERARD5d6c5992017-11-16 15:14:19 +00003109 */
3110 if (live && !saved_vm_running) {
3111 ret = bdrv_inactivate_all();
3112 if (ret) {
3113 error_setg(errp, "%s: bdrv_inactivate_all() failed (%d)",
3114 __func__, ret);
3115 }
3116 }
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00003117 }
3118
3119 the_end:
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02003120 if (saved_vm_running) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00003121 vm_start();
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02003122 }
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00003123}
3124
Wen Congyang88c16562016-06-03 17:58:34 +08003125void qmp_xen_load_devices_state(const char *filename, Error **errp)
3126{
3127 QEMUFile *f;
3128 QIOChannelFile *ioc;
3129 int ret;
3130
3131 /* Guest must be paused before loading the device state; the RAM state
3132 * will already have been loaded by xc
3133 */
3134 if (runstate_is_running()) {
3135 error_setg(errp, "Cannot update device state while vm is running");
3136 return;
3137 }
3138 vm_stop(RUN_STATE_RESTORE_VM);
3139
3140 ioc = qio_channel_file_new_path(filename, O_RDONLY | O_BINARY, 0, errp);
3141 if (!ioc) {
3142 return;
3143 }
Daniel P. Berrange6f01f132016-09-30 11:57:14 +01003144 qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state");
Daniel P. Berrangé77ef2dc2022-06-20 12:02:05 +01003145 f = qemu_file_new_input(QIO_CHANNEL(ioc));
Ross Lagerwall032b79f2017-11-01 14:25:23 +00003146 object_unref(OBJECT(ioc));
Wen Congyang88c16562016-06-03 17:58:34 +08003147
Wen Congyang88c16562016-06-03 17:58:34 +08003148 ret = qemu_loadvm_state(f);
3149 qemu_fclose(f);
3150 if (ret < 0) {
3151 error_setg(errp, QERR_IO_ERROR);
3152 }
3153 migration_incoming_state_destroy();
3154}
3155
Daniel P. Berrangéf1a9fcd2021-02-04 12:48:30 +00003156bool load_snapshot(const char *name, const char *vmstate,
3157 bool has_devices, strList *devices, Error **errp)
aliguoria672b462008-11-11 21:33:36 +00003158{
Daniel P. Berrangée26f98e2021-02-04 12:48:23 +00003159 BlockDriverState *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00003160 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00003161 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02003162 int ret;
Denis V. Lunev79b3c122015-11-19 09:42:11 +03003163 AioContext *aio_context;
Juan Quintelab4b076d2017-01-23 22:32:06 +01003164 MigrationIncomingState *mis = migration_incoming_get_current();
aliguoria672b462008-11-11 21:33:36 +00003165
Daniel P. Berrangéf1a9fcd2021-02-04 12:48:30 +00003166 if (!bdrv_all_can_snapshot(has_devices, devices, errp)) {
Daniel P. Berrangéf61fe112021-02-04 12:48:25 +00003167 return false;
Denis V. Lunev849f96e2015-11-19 09:42:05 +03003168 }
Daniel P. Berrangéf1a9fcd2021-02-04 12:48:30 +00003169 ret = bdrv_all_has_snapshot(name, has_devices, devices, errp);
Denis V. Lunev723ccda2015-11-19 09:42:06 +03003170 if (ret < 0) {
Daniel P. Berrangéf61fe112021-02-04 12:48:25 +00003171 return false;
Denis V. Lunev723ccda2015-11-19 09:42:06 +03003172 }
Daniel P. Berrangé3d3e9b12021-02-04 12:48:28 +00003173 if (ret == 0) {
3174 error_setg(errp, "Snapshot '%s' does not exist in one or more devices",
3175 name);
3176 return false;
3177 }
Denis V. Lunev849f96e2015-11-19 09:42:05 +03003178
Daniel P. Berrangéf1a9fcd2021-02-04 12:48:30 +00003179 bs_vm_state = bdrv_all_find_vmstate_bs(vmstate, has_devices, devices, errp);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03003180 if (!bs_vm_state) {
Daniel P. Berrangéf61fe112021-02-04 12:48:25 +00003181 return false;
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03003182 }
Denis V. Lunev79b3c122015-11-19 09:42:11 +03003183 aio_context = bdrv_get_aio_context(bs_vm_state);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03003184
3185 /* Don't even try to load empty VM states */
Denis V. Lunev79b3c122015-11-19 09:42:11 +03003186 aio_context_acquire(aio_context);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03003187 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
Denis V. Lunev79b3c122015-11-19 09:42:11 +03003188 aio_context_release(aio_context);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03003189 if (ret < 0) {
Daniel P. Berrangéf61fe112021-02-04 12:48:25 +00003190 return false;
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03003191 } else if (sn.vm_state_size == 0) {
Juan Quintela927d6632017-04-18 18:12:35 +02003192 error_setg(errp, "This is a disk-only snapshot. Revert to it "
3193 " offline using qemu-img");
Daniel P. Berrangéf61fe112021-02-04 12:48:25 +00003194 return false;
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03003195 }
3196
Pavel Dovgalyukf9a9fb62020-10-03 20:13:37 +03003197 /*
3198 * Flush the record/replay queue. Now the VM state is going
3199 * to change. Therefore we don't need to preserve its consistency
3200 */
3201 replay_flush_events();
3202
aliguoria672b462008-11-11 21:33:36 +00003203 /* Flush all IO requests so they don't interfere with the new state. */
Stefan Hajnoczi8649f2f2017-05-22 14:57:04 +01003204 bdrv_drain_all_begin();
aliguoria672b462008-11-11 21:33:36 +00003205
Daniel P. Berrangéf1a9fcd2021-02-04 12:48:30 +00003206 ret = bdrv_all_goto_snapshot(name, has_devices, devices, errp);
Denis V. Lunev4c1cdba2015-11-19 09:42:04 +03003207 if (ret < 0) {
Stefan Hajnoczi8649f2f2017-05-22 14:57:04 +01003208 goto err_drain;
aliguoria672b462008-11-11 21:33:36 +00003209 }
3210
aliguoria672b462008-11-11 21:33:36 +00003211 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03003212 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00003213 if (!f) {
Juan Quintela927d6632017-04-18 18:12:35 +02003214 error_setg(errp, "Could not open VM state file");
Stefan Hajnoczi8649f2f2017-05-22 14:57:04 +01003215 goto err_drain;
aliguoria672b462008-11-11 21:33:36 +00003216 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03003217
Jason A. Donenfeld7966d702022-10-25 02:43:17 +02003218 qemu_system_reset(SHUTDOWN_CAUSE_SNAPSHOT_LOAD);
Juan Quintelab4b076d2017-01-23 22:32:06 +01003219 mis->from_src_file = f;
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03003220
Lukas Straubb5eea992020-12-28 16:08:52 +01003221 if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
3222 ret = -EINVAL;
3223 goto err_drain;
3224 }
Denis V. Lunev79b3c122015-11-19 09:42:11 +03003225 aio_context_acquire(aio_context);
3226 ret = qemu_loadvm_state(f);
Stefan Hajnoczi15758292017-06-15 17:38:10 +01003227 migration_incoming_state_destroy();
Denis V. Lunev79b3c122015-11-19 09:42:11 +03003228 aio_context_release(aio_context);
3229
Stefan Hajnoczi8649f2f2017-05-22 14:57:04 +01003230 bdrv_drain_all_end();
3231
aliguoria672b462008-11-11 21:33:36 +00003232 if (ret < 0) {
Juan Quintela927d6632017-04-18 18:12:35 +02003233 error_setg(errp, "Error %d while loading VM state", ret);
Daniel P. Berrangéf61fe112021-02-04 12:48:25 +00003234 return false;
aliguoria672b462008-11-11 21:33:36 +00003235 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03003236
Daniel P. Berrangéf61fe112021-02-04 12:48:25 +00003237 return true;
Stefan Hajnoczi8649f2f2017-05-22 14:57:04 +01003238
3239err_drain:
3240 bdrv_drain_all_end();
Daniel P. Berrangéf61fe112021-02-04 12:48:25 +00003241 return false;
Juan Quintela7b630342009-08-20 19:42:20 +02003242}
3243
Daniel P. Berrangébef7e9e2021-02-04 12:48:31 +00003244bool delete_snapshot(const char *name, bool has_devices,
3245 strList *devices, Error **errp)
3246{
3247 if (!bdrv_all_can_snapshot(has_devices, devices, errp)) {
3248 return false;
3249 }
3250
3251 if (bdrv_all_delete_snapshot(name, has_devices, devices, errp) < 0) {
3252 return false;
3253 }
3254
3255 return true;
3256}
3257
Avi Kivityc5705a72011-12-20 15:59:12 +02003258void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
3259{
Gongleifa53a0e2016-05-10 10:04:59 +08003260 qemu_ram_set_idstr(mr->ram_block,
Avi Kivityc5705a72011-12-20 15:59:12 +02003261 memory_region_name(mr), dev);
Cédric Le Goaterb895de52018-05-14 08:57:00 +02003262 qemu_ram_set_migratable(mr->ram_block);
Avi Kivityc5705a72011-12-20 15:59:12 +02003263}
3264
3265void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
3266{
Gongleifa53a0e2016-05-10 10:04:59 +08003267 qemu_ram_unset_idstr(mr->ram_block);
Cédric Le Goaterb895de52018-05-14 08:57:00 +02003268 qemu_ram_unset_migratable(mr->ram_block);
Avi Kivityc5705a72011-12-20 15:59:12 +02003269}
3270
3271void vmstate_register_ram_global(MemoryRegion *mr)
3272{
3273 vmstate_register_ram(mr, NULL);
3274}
Juan Quintela1bfe5f02017-04-17 14:57:54 +02003275
3276bool vmstate_check_only_migratable(const VMStateDescription *vmsd)
3277{
3278 /* check needed if --only-migratable is specified */
Markus Armbruster811f8652019-04-01 11:08:24 +02003279 if (!only_migratable) {
Juan Quintela1bfe5f02017-04-17 14:57:54 +02003280 return true;
3281 }
3282
3283 return !(vmsd && vmsd->unmigratable);
3284}
Daniel P. Berrangé0f0d83a2021-02-04 12:48:34 +00003285
3286typedef struct SnapshotJob {
3287 Job common;
3288 char *tag;
3289 char *vmstate;
3290 strList *devices;
3291 Coroutine *co;
3292 Error **errp;
3293 bool ret;
3294} SnapshotJob;
3295
3296static void qmp_snapshot_job_free(SnapshotJob *s)
3297{
3298 g_free(s->tag);
3299 g_free(s->vmstate);
3300 qapi_free_strList(s->devices);
3301}
3302
3303
3304static void snapshot_load_job_bh(void *opaque)
3305{
3306 Job *job = opaque;
3307 SnapshotJob *s = container_of(job, SnapshotJob, common);
3308 int orig_vm_running;
3309
3310 job_progress_set_remaining(&s->common, 1);
3311
3312 orig_vm_running = runstate_is_running();
3313 vm_stop(RUN_STATE_RESTORE_VM);
3314
3315 s->ret = load_snapshot(s->tag, s->vmstate, true, s->devices, s->errp);
3316 if (s->ret && orig_vm_running) {
3317 vm_start();
3318 }
3319
3320 job_progress_update(&s->common, 1);
3321
3322 qmp_snapshot_job_free(s);
3323 aio_co_wake(s->co);
3324}
3325
3326static void snapshot_save_job_bh(void *opaque)
3327{
3328 Job *job = opaque;
3329 SnapshotJob *s = container_of(job, SnapshotJob, common);
3330
3331 job_progress_set_remaining(&s->common, 1);
3332 s->ret = save_snapshot(s->tag, false, s->vmstate,
3333 true, s->devices, s->errp);
3334 job_progress_update(&s->common, 1);
3335
3336 qmp_snapshot_job_free(s);
3337 aio_co_wake(s->co);
3338}
3339
3340static void snapshot_delete_job_bh(void *opaque)
3341{
3342 Job *job = opaque;
3343 SnapshotJob *s = container_of(job, SnapshotJob, common);
3344
3345 job_progress_set_remaining(&s->common, 1);
3346 s->ret = delete_snapshot(s->tag, true, s->devices, s->errp);
3347 job_progress_update(&s->common, 1);
3348
3349 qmp_snapshot_job_free(s);
3350 aio_co_wake(s->co);
3351}
3352
3353static int coroutine_fn snapshot_save_job_run(Job *job, Error **errp)
3354{
3355 SnapshotJob *s = container_of(job, SnapshotJob, common);
3356 s->errp = errp;
3357 s->co = qemu_coroutine_self();
3358 aio_bh_schedule_oneshot(qemu_get_aio_context(),
3359 snapshot_save_job_bh, job);
3360 qemu_coroutine_yield();
3361 return s->ret ? 0 : -1;
3362}
3363
3364static int coroutine_fn snapshot_load_job_run(Job *job, Error **errp)
3365{
3366 SnapshotJob *s = container_of(job, SnapshotJob, common);
3367 s->errp = errp;
3368 s->co = qemu_coroutine_self();
3369 aio_bh_schedule_oneshot(qemu_get_aio_context(),
3370 snapshot_load_job_bh, job);
3371 qemu_coroutine_yield();
3372 return s->ret ? 0 : -1;
3373}
3374
3375static int coroutine_fn snapshot_delete_job_run(Job *job, Error **errp)
3376{
3377 SnapshotJob *s = container_of(job, SnapshotJob, common);
3378 s->errp = errp;
3379 s->co = qemu_coroutine_self();
3380 aio_bh_schedule_oneshot(qemu_get_aio_context(),
3381 snapshot_delete_job_bh, job);
3382 qemu_coroutine_yield();
3383 return s->ret ? 0 : -1;
3384}
3385
3386
3387static const JobDriver snapshot_load_job_driver = {
3388 .instance_size = sizeof(SnapshotJob),
3389 .job_type = JOB_TYPE_SNAPSHOT_LOAD,
3390 .run = snapshot_load_job_run,
3391};
3392
3393static const JobDriver snapshot_save_job_driver = {
3394 .instance_size = sizeof(SnapshotJob),
3395 .job_type = JOB_TYPE_SNAPSHOT_SAVE,
3396 .run = snapshot_save_job_run,
3397};
3398
3399static const JobDriver snapshot_delete_job_driver = {
3400 .instance_size = sizeof(SnapshotJob),
3401 .job_type = JOB_TYPE_SNAPSHOT_DELETE,
3402 .run = snapshot_delete_job_run,
3403};
3404
3405
3406void qmp_snapshot_save(const char *job_id,
3407 const char *tag,
3408 const char *vmstate,
3409 strList *devices,
3410 Error **errp)
3411{
3412 SnapshotJob *s;
3413
3414 s = job_create(job_id, &snapshot_save_job_driver, NULL,
3415 qemu_get_aio_context(), JOB_MANUAL_DISMISS,
3416 NULL, NULL, errp);
3417 if (!s) {
3418 return;
3419 }
3420
3421 s->tag = g_strdup(tag);
3422 s->vmstate = g_strdup(vmstate);
3423 s->devices = QAPI_CLONE(strList, devices);
3424
3425 job_start(&s->common);
3426}
3427
3428void qmp_snapshot_load(const char *job_id,
3429 const char *tag,
3430 const char *vmstate,
3431 strList *devices,
3432 Error **errp)
3433{
3434 SnapshotJob *s;
3435
3436 s = job_create(job_id, &snapshot_load_job_driver, NULL,
3437 qemu_get_aio_context(), JOB_MANUAL_DISMISS,
3438 NULL, NULL, errp);
3439 if (!s) {
3440 return;
3441 }
3442
3443 s->tag = g_strdup(tag);
3444 s->vmstate = g_strdup(vmstate);
3445 s->devices = QAPI_CLONE(strList, devices);
3446
3447 job_start(&s->common);
3448}
3449
3450void qmp_snapshot_delete(const char *job_id,
3451 const char *tag,
3452 strList *devices,
3453 Error **errp)
3454{
3455 SnapshotJob *s;
3456
3457 s = job_create(job_id, &snapshot_delete_job_driver, NULL,
3458 qemu_get_aio_context(), JOB_MANUAL_DISMISS,
3459 NULL, NULL, errp);
3460 if (!s) {
3461 return;
3462 }
3463
3464 s->tag = g_strdup(tag);
3465 s->devices = QAPI_CLONE(strList, devices);
3466
3467 job_start(&s->common);
3468}