]> git.proxmox.com Git - mirror_qemu.git/blame - migration/savevm.c
multifd: The variable is only used inside the loop
[mirror_qemu.git] / migration / savevm.c
CommitLineData
a672b469
AL
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
76cc7b58
JQ
5 * Copyright (c) 2009-2015 Red Hat Inc
6 *
7 * Authors:
8 * Juan Quintela <quintela@redhat.com>
a672b469
AL
9 *
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 */
a672b469 28
1393a485 29#include "qemu/osdep.h"
abfd9ce3 30#include "hw/boards.h"
1422e32d 31#include "net/net.h"
6666c96a 32#include "migration.h"
5e22479a 33#include "migration/snapshot.h"
d6454270 34#include "migration/vmstate.h"
f8d806c9 35#include "migration/misc.h"
f2a8f0a6 36#include "migration/register.h"
84a899de 37#include "migration/global_state.h"
7b1e1a22 38#include "ram.h"
40014d81 39#include "qemu-file-channel.h"
08a0aee1 40#include "qemu-file.h"
20a519a0 41#include "savevm.h"
be07b0ac 42#include "postcopy-ram.h"
e688df6b 43#include "qapi/error.h"
112ed241 44#include "qapi/qapi-commands-migration.h"
3ddba9a9 45#include "qapi/qmp/json-writer.h"
f1a9fcdd
DB
46#include "qapi/clone-visitor.h"
47#include "qapi/qapi-builtin-visit.h"
cc7a8ea7 48#include "qapi/qmp/qerror.h"
d49b6836 49#include "qemu/error-report.h"
9c17d615 50#include "sysemu/cpus.h"
022c62cb 51#include "exec/memory.h"
51180423 52#include "exec/target_page.h"
517a13c9 53#include "trace.h"
28085f7b 54#include "qemu/iov.h"
db725815 55#include "qemu/main-loop.h"
de08c606 56#include "block/snapshot.h"
f348b6d1 57#include "qemu/cutils.h"
61b67d47 58#include "io/channel-buffer.h"
8925839f 59#include "io/channel-file.h"
377b21cc 60#include "sysemu/replay.h"
54d31236 61#include "sysemu/runstate.h"
46517dd4 62#include "sysemu/sysemu.h"
da278d58 63#include "sysemu/xen.h"
aad555c2 64#include "migration/colo.h"
6cafc8e4 65#include "qemu/bitmap.h"
7659505c 66#include "net/announce.h"
b5eea99e 67#include "qemu/yank.h"
39675fff 68#include "yank_functions.h"
18995b98 69
49324e93 70const unsigned int postcopy_ram_discard_version;
093e3c42 71
20a519a0
JQ
72/* Subcommands for QEMU_VM_COMMAND */
73enum qemu_vm_cmd {
74 MIG_CMD_INVALID = 0, /* Must be 0 */
75 MIG_CMD_OPEN_RETURN_PATH, /* Tell the dest to open the Return path */
76 MIG_CMD_PING, /* Request a PONG on the RP */
77
78 MIG_CMD_POSTCOPY_ADVISE, /* Prior to any page transfers, just
79 warn we might want to do PC */
80 MIG_CMD_POSTCOPY_LISTEN, /* Start listening for incoming
81 pages as it's running. */
82 MIG_CMD_POSTCOPY_RUN, /* Start execution */
83
84 MIG_CMD_POSTCOPY_RAM_DISCARD, /* A list of pages to discard that
85 were previously sent during
86 precopy but are dirty. */
87 MIG_CMD_PACKAGED, /* Send a wrapped stream within this stream */
aad555c2 88 MIG_CMD_ENABLE_COLO, /* Enable COLO */
858b6d62 89 MIG_CMD_POSTCOPY_RESUME, /* resume postcopy on dest */
f25d4225 90 MIG_CMD_RECV_BITMAP, /* Request for recved bitmap on dst */
20a519a0
JQ
91 MIG_CMD_MAX
92};
93
ee555cdf 94#define MAX_VM_CMD_PACKAGED_SIZE UINT32_MAX
c76ca188
DDAG
95static struct mig_cmd_args {
96 ssize_t len; /* -1 = variable */
97 const char *name;
98} mig_cmd_args[] = {
99 [MIG_CMD_INVALID] = { .len = -1, .name = "INVALID" },
2e37701e
DDAG
100 [MIG_CMD_OPEN_RETURN_PATH] = { .len = 0, .name = "OPEN_RETURN_PATH" },
101 [MIG_CMD_PING] = { .len = sizeof(uint32_t), .name = "PING" },
58110f0a 102 [MIG_CMD_POSTCOPY_ADVISE] = { .len = -1, .name = "POSTCOPY_ADVISE" },
093e3c42
DDAG
103 [MIG_CMD_POSTCOPY_LISTEN] = { .len = 0, .name = "POSTCOPY_LISTEN" },
104 [MIG_CMD_POSTCOPY_RUN] = { .len = 0, .name = "POSTCOPY_RUN" },
105 [MIG_CMD_POSTCOPY_RAM_DISCARD] = {
106 .len = -1, .name = "POSTCOPY_RAM_DISCARD" },
3f5875ec 107 [MIG_CMD_POSTCOPY_RESUME] = { .len = 0, .name = "POSTCOPY_RESUME" },
11cf1d98 108 [MIG_CMD_PACKAGED] = { .len = 4, .name = "PACKAGED" },
f25d4225 109 [MIG_CMD_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
c76ca188
DDAG
110 [MIG_CMD_MAX] = { .len = -1, .name = "MAX" },
111};
112
58110f0a
VSO
113/* Note for MIG_CMD_POSTCOPY_ADVISE:
114 * The format of arguments is depending on postcopy mode:
115 * - postcopy RAM only
116 * uint64_t host page size
117 * uint64_t taget page size
118 *
119 * - postcopy RAM and postcopy dirty bitmaps
120 * format is the same as for postcopy RAM only
121 *
122 * - postcopy dirty bitmaps only
123 * Nothing. Command length field is 0.
124 *
125 * Be careful: adding a new postcopy entity with some other parameters should
126 * not break format self-description ability. Good way is to introduce some
127 * generic extendable format with an exception for two old entities.
128 */
129
a672b469
AL
130/***********************************************************/
131/* savevm/loadvm support */
132
05fcc848 133static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
3d661c8a 134 int64_t pos, Error **errp)
05fcc848
KW
135{
136 int ret;
137 QEMUIOVector qiov;
138
139 qemu_iovec_init_external(&qiov, iov, iovcnt);
140 ret = bdrv_writev_vmstate(opaque, &qiov, pos);
141 if (ret < 0) {
142 return ret;
143 }
144
145 return qiov.size;
146}
147
a202a4c0 148static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
3d661c8a 149 size_t size, Error **errp)
a672b469 150{
45566e9c 151 return bdrv_load_vmstate(opaque, buf, pos, size);
a672b469
AL
152}
153
3d661c8a 154static int bdrv_fclose(void *opaque, Error **errp)
a672b469 155{
ad492c92 156 return bdrv_flush(opaque);
a672b469
AL
157}
158
9229bf3c
PB
159static const QEMUFileOps bdrv_read_ops = {
160 .get_buffer = block_get_buffer,
161 .close = bdrv_fclose
162};
163
164static const QEMUFileOps bdrv_write_ops = {
05fcc848
KW
165 .writev_buffer = block_writev_buffer,
166 .close = bdrv_fclose
9229bf3c
PB
167};
168
45566e9c 169static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
a672b469 170{
38ff78d3 171 if (is_writable) {
c6ad5be7 172 return qemu_fopen_ops(bs, &bdrv_write_ops, false);
38ff78d3 173 }
c6ad5be7 174 return qemu_fopen_ops(bs, &bdrv_read_ops, false);
a672b469
AL
175}
176
2ff68d07 177
bb1a6d8c
EH
178/* QEMUFile timer support.
179 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
180 */
2ff68d07 181
40daca54 182void timer_put(QEMUFile *f, QEMUTimer *ts)
2ff68d07
PB
183{
184 uint64_t expire_time;
185
e93379b0 186 expire_time = timer_expire_time_ns(ts);
2ff68d07
PB
187 qemu_put_be64(f, expire_time);
188}
189
40daca54 190void timer_get(QEMUFile *f, QEMUTimer *ts)
2ff68d07
PB
191{
192 uint64_t expire_time;
193
194 expire_time = qemu_get_be64(f);
195 if (expire_time != -1) {
bc72ad67 196 timer_mod_ns(ts, expire_time);
2ff68d07 197 } else {
bc72ad67 198 timer_del(ts);
2ff68d07
PB
199 }
200}
201
202
bb1a6d8c
EH
203/* VMState timer support.
204 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
205 */
dde0463b 206
03fee66f
MAL
207static int get_timer(QEMUFile *f, void *pv, size_t size,
208 const VMStateField *field)
dde0463b
JQ
209{
210 QEMUTimer *v = pv;
40daca54 211 timer_get(f, v);
dde0463b
JQ
212 return 0;
213}
214
03fee66f 215static int put_timer(QEMUFile *f, void *pv, size_t size,
3ddba9a9 216 const VMStateField *field, JSONWriter *vmdesc)
dde0463b 217{
84e2e3eb 218 QEMUTimer *v = pv;
40daca54 219 timer_put(f, v);
2c21ee76
JD
220
221 return 0;
dde0463b
JQ
222}
223
224const VMStateInfo vmstate_info_timer = {
225 .name = "timer",
226 .get = get_timer,
227 .put = put_timer,
228};
229
08e99e29 230
7685ee6a
AW
231typedef struct CompatEntry {
232 char idstr[256];
233 int instance_id;
234} CompatEntry;
235
a672b469 236typedef struct SaveStateEntry {
72cf2d4f 237 QTAILQ_ENTRY(SaveStateEntry) entry;
a672b469 238 char idstr[256];
93062e23 239 uint32_t instance_id;
4d2ffa08 240 int alias_id;
a672b469 241 int version_id;
0f42f657
JQ
242 /* version id read from the stream */
243 int load_version_id;
a672b469 244 int section_id;
0f42f657
JQ
245 /* section id read from the stream */
246 int load_section_id;
de22ded0 247 const SaveVMHandlers *ops;
9ed7d6ae 248 const VMStateDescription *vmsd;
a672b469 249 void *opaque;
7685ee6a 250 CompatEntry *compat;
a7ae8355 251 int is_ram;
a672b469
AL
252} SaveStateEntry;
253
0163a2e0
JQ
254typedef struct SaveState {
255 QTAILQ_HEAD(, SaveStateEntry) handlers;
174723ff 256 SaveStateEntry *handler_pri_head[MIG_PRI_MAX + 1];
0163a2e0 257 int global_section_id;
61964c23
JQ
258 uint32_t len;
259 const char *name;
59811a32 260 uint32_t target_page_bits;
6cafc8e4
YK
261 uint32_t caps_count;
262 MigrationCapability *capabilities;
b9d68df6 263 QemuUUID uuid;
0163a2e0
JQ
264} SaveState;
265
266static SaveState savevm_state = {
267 .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers),
174723ff 268 .handler_pri_head = { [MIG_PRI_DEFAULT ... MIG_PRI_MAX] = NULL },
0163a2e0 269 .global_section_id = 0,
61964c23
JQ
270};
271
6cafc8e4
YK
272static bool should_validate_capability(int capability)
273{
274 assert(capability >= 0 && capability < MIGRATION_CAPABILITY__MAX);
275 /* Validate only new capabilities to keep compatibility. */
276 switch (capability) {
277 case MIGRATION_CAPABILITY_X_IGNORE_SHARED:
278 return true;
279 default:
280 return false;
281 }
282}
283
284static uint32_t get_validatable_capabilities_count(void)
285{
286 MigrationState *s = migrate_get_current();
287 uint32_t result = 0;
288 int i;
289 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
290 if (should_validate_capability(i) && s->enabled_capabilities[i]) {
291 result++;
292 }
293 }
294 return result;
295}
296
44b1ff31 297static int configuration_pre_save(void *opaque)
61964c23
JQ
298{
299 SaveState *state = opaque;
300 const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
6cafc8e4
YK
301 MigrationState *s = migrate_get_current();
302 int i, j;
61964c23
JQ
303
304 state->len = strlen(current_name);
305 state->name = current_name;
46d702b1 306 state->target_page_bits = qemu_target_page_bits();
44b1ff31 307
6cafc8e4
YK
308 state->caps_count = get_validatable_capabilities_count();
309 state->capabilities = g_renew(MigrationCapability, state->capabilities,
310 state->caps_count);
311 for (i = j = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
312 if (should_validate_capability(i) && s->enabled_capabilities[i]) {
313 state->capabilities[j++] = i;
314 }
315 }
b9d68df6 316 state->uuid = qemu_uuid;
6cafc8e4 317
44b1ff31 318 return 0;
59811a32
PM
319}
320
39f633d4
JG
321static int configuration_post_save(void *opaque)
322{
323 SaveState *state = opaque;
324
325 g_free(state->capabilities);
326 state->capabilities = NULL;
327 state->caps_count = 0;
328 return 0;
329}
330
59811a32
PM
331static int configuration_pre_load(void *opaque)
332{
333 SaveState *state = opaque;
334
335 /* If there is no target-page-bits subsection it means the source
336 * predates the variable-target-page-bits support and is using the
337 * minimum possible value for this CPU.
338 */
46d702b1 339 state->target_page_bits = qemu_target_page_bits_min();
59811a32 340 return 0;
61964c23
JQ
341}
342
6cafc8e4
YK
343static bool configuration_validate_capabilities(SaveState *state)
344{
345 bool ret = true;
346 MigrationState *s = migrate_get_current();
347 unsigned long *source_caps_bm;
348 int i;
349
350 source_caps_bm = bitmap_new(MIGRATION_CAPABILITY__MAX);
351 for (i = 0; i < state->caps_count; i++) {
352 MigrationCapability capability = state->capabilities[i];
353 set_bit(capability, source_caps_bm);
354 }
355
356 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
357 bool source_state, target_state;
358 if (!should_validate_capability(i)) {
359 continue;
360 }
361 source_state = test_bit(i, source_caps_bm);
362 target_state = s->enabled_capabilities[i];
363 if (source_state != target_state) {
364 error_report("Capability %s is %s, but received capability is %s",
365 MigrationCapability_str(i),
366 target_state ? "on" : "off",
367 source_state ? "on" : "off");
368 ret = false;
369 /* Don't break here to report all failed capabilities */
370 }
371 }
372
373 g_free(source_caps_bm);
374 return ret;
375}
376
61964c23
JQ
377static int configuration_post_load(void *opaque, int version_id)
378{
379 SaveState *state = opaque;
380 const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
39f633d4 381 int ret = 0;
61964c23
JQ
382
383 if (strncmp(state->name, current_name, state->len) != 0) {
15d61692
GK
384 error_report("Machine type received is '%.*s' and local is '%s'",
385 (int) state->len, state->name, current_name);
39f633d4
JG
386 ret = -EINVAL;
387 goto out;
61964c23 388 }
59811a32 389
46d702b1 390 if (state->target_page_bits != qemu_target_page_bits()) {
59811a32 391 error_report("Received TARGET_PAGE_BITS is %d but local is %d",
46d702b1 392 state->target_page_bits, qemu_target_page_bits());
39f633d4
JG
393 ret = -EINVAL;
394 goto out;
59811a32
PM
395 }
396
6cafc8e4 397 if (!configuration_validate_capabilities(state)) {
39f633d4
JG
398 ret = -EINVAL;
399 goto out;
6cafc8e4
YK
400 }
401
39f633d4
JG
402out:
403 g_free((void *)state->name);
404 state->name = NULL;
405 state->len = 0;
406 g_free(state->capabilities);
407 state->capabilities = NULL;
408 state->caps_count = 0;
409
410 return ret;
61964c23
JQ
411}
412
6cafc8e4
YK
413static int get_capability(QEMUFile *f, void *pv, size_t size,
414 const VMStateField *field)
415{
416 MigrationCapability *capability = pv;
417 char capability_str[UINT8_MAX + 1];
418 uint8_t len;
419 int i;
420
421 len = qemu_get_byte(f);
422 qemu_get_buffer(f, (uint8_t *)capability_str, len);
423 capability_str[len] = '\0';
424 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
425 if (!strcmp(MigrationCapability_str(i), capability_str)) {
426 *capability = i;
427 return 0;
428 }
429 }
430 error_report("Received unknown capability %s", capability_str);
431 return -EINVAL;
432}
433
434static int put_capability(QEMUFile *f, void *pv, size_t size,
3ddba9a9 435 const VMStateField *field, JSONWriter *vmdesc)
6cafc8e4
YK
436{
437 MigrationCapability *capability = pv;
438 const char *capability_str = MigrationCapability_str(*capability);
439 size_t len = strlen(capability_str);
440 assert(len <= UINT8_MAX);
441
442 qemu_put_byte(f, len);
443 qemu_put_buffer(f, (uint8_t *)capability_str, len);
444 return 0;
445}
446
447static const VMStateInfo vmstate_info_capability = {
448 .name = "capability",
449 .get = get_capability,
450 .put = put_capability,
451};
452
59811a32
PM
453/* The target-page-bits subsection is present only if the
454 * target page size is not the same as the default (ie the
455 * minimum page size for a variable-page-size guest CPU).
456 * If it is present then it contains the actual target page
457 * bits for the machine, and migration will fail if the
458 * two ends don't agree about it.
459 */
460static bool vmstate_target_page_bits_needed(void *opaque)
461{
46d702b1
JQ
462 return qemu_target_page_bits()
463 > qemu_target_page_bits_min();
59811a32
PM
464}
465
466static const VMStateDescription vmstate_target_page_bits = {
467 .name = "configuration/target-page-bits",
468 .version_id = 1,
469 .minimum_version_id = 1,
470 .needed = vmstate_target_page_bits_needed,
471 .fields = (VMStateField[]) {
472 VMSTATE_UINT32(target_page_bits, SaveState),
473 VMSTATE_END_OF_LIST()
474 }
475};
476
6cafc8e4
YK
477static bool vmstate_capabilites_needed(void *opaque)
478{
479 return get_validatable_capabilities_count() > 0;
480}
481
482static const VMStateDescription vmstate_capabilites = {
483 .name = "configuration/capabilities",
484 .version_id = 1,
485 .minimum_version_id = 1,
486 .needed = vmstate_capabilites_needed,
487 .fields = (VMStateField[]) {
488 VMSTATE_UINT32_V(caps_count, SaveState, 1),
489 VMSTATE_VARRAY_UINT32_ALLOC(capabilities, SaveState, caps_count, 1,
490 vmstate_info_capability,
491 MigrationCapability),
492 VMSTATE_END_OF_LIST()
493 }
494};
495
b9d68df6
YK
496static bool vmstate_uuid_needed(void *opaque)
497{
498 return qemu_uuid_set && migrate_validate_uuid();
499}
500
501static int vmstate_uuid_post_load(void *opaque, int version_id)
502{
503 SaveState *state = opaque;
504 char uuid_src[UUID_FMT_LEN + 1];
505 char uuid_dst[UUID_FMT_LEN + 1];
506
507 if (!qemu_uuid_set) {
508 /*
509 * It's warning because user might not know UUID in some cases,
510 * e.g. load an old snapshot
511 */
512 qemu_uuid_unparse(&state->uuid, uuid_src);
513 warn_report("UUID is received %s, but local uuid isn't set",
514 uuid_src);
515 return 0;
516 }
517 if (!qemu_uuid_is_equal(&state->uuid, &qemu_uuid)) {
518 qemu_uuid_unparse(&state->uuid, uuid_src);
519 qemu_uuid_unparse(&qemu_uuid, uuid_dst);
520 error_report("UUID received is %s and local is %s", uuid_src, uuid_dst);
521 return -EINVAL;
522 }
523 return 0;
524}
525
526static const VMStateDescription vmstate_uuid = {
527 .name = "configuration/uuid",
528 .version_id = 1,
529 .minimum_version_id = 1,
530 .needed = vmstate_uuid_needed,
531 .post_load = vmstate_uuid_post_load,
532 .fields = (VMStateField[]) {
533 VMSTATE_UINT8_ARRAY_V(uuid.data, SaveState, sizeof(QemuUUID), 1),
534 VMSTATE_END_OF_LIST()
535 }
536};
537
61964c23
JQ
538static const VMStateDescription vmstate_configuration = {
539 .name = "configuration",
540 .version_id = 1,
59811a32 541 .pre_load = configuration_pre_load,
61964c23
JQ
542 .post_load = configuration_post_load,
543 .pre_save = configuration_pre_save,
39f633d4 544 .post_save = configuration_post_save,
61964c23
JQ
545 .fields = (VMStateField[]) {
546 VMSTATE_UINT32(len, SaveState),
59046ec2 547 VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, len),
61964c23
JQ
548 VMSTATE_END_OF_LIST()
549 },
395cb450 550 .subsections = (const VMStateDescription *[]) {
59811a32 551 &vmstate_target_page_bits,
6cafc8e4 552 &vmstate_capabilites,
b9d68df6 553 &vmstate_uuid,
59811a32
PM
554 NULL
555 }
0163a2e0 556};
a672b469 557
abfd9ce3
AS
558static void dump_vmstate_vmsd(FILE *out_file,
559 const VMStateDescription *vmsd, int indent,
560 bool is_subsection);
561
562static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field,
563 int indent)
564{
565 fprintf(out_file, "%*s{\n", indent, "");
566 indent += 2;
567 fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name);
568 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
569 field->version_id);
570 fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "",
571 field->field_exists ? "true" : "false");
572 fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size);
573 if (field->vmsd != NULL) {
574 fprintf(out_file, ",\n");
575 dump_vmstate_vmsd(out_file, field->vmsd, indent, false);
576 }
577 fprintf(out_file, "\n%*s}", indent - 2, "");
578}
579
580static void dump_vmstate_vmss(FILE *out_file,
5cd8cada 581 const VMStateDescription **subsection,
abfd9ce3
AS
582 int indent)
583{
5cd8cada
JQ
584 if (*subsection != NULL) {
585 dump_vmstate_vmsd(out_file, *subsection, indent, true);
abfd9ce3
AS
586 }
587}
588
589static void dump_vmstate_vmsd(FILE *out_file,
590 const VMStateDescription *vmsd, int indent,
591 bool is_subsection)
592{
593 if (is_subsection) {
594 fprintf(out_file, "%*s{\n", indent, "");
595 } else {
596 fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description");
597 }
598 indent += 2;
599 fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name);
600 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
601 vmsd->version_id);
602 fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "",
603 vmsd->minimum_version_id);
604 if (vmsd->fields != NULL) {
605 const VMStateField *field = vmsd->fields;
606 bool first;
607
608 fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, "");
609 first = true;
610 while (field->name != NULL) {
611 if (field->flags & VMS_MUST_EXIST) {
612 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
613 field++;
614 continue;
615 }
616 if (!first) {
617 fprintf(out_file, ",\n");
618 }
619 dump_vmstate_vmsf(out_file, field, indent + 2);
620 field++;
621 first = false;
622 }
623 fprintf(out_file, "\n%*s]", indent, "");
624 }
625 if (vmsd->subsections != NULL) {
5cd8cada 626 const VMStateDescription **subsection = vmsd->subsections;
abfd9ce3
AS
627 bool first;
628
629 fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, "");
630 first = true;
5cd8cada 631 while (*subsection != NULL) {
abfd9ce3
AS
632 if (!first) {
633 fprintf(out_file, ",\n");
634 }
635 dump_vmstate_vmss(out_file, subsection, indent + 2);
636 subsection++;
637 first = false;
638 }
639 fprintf(out_file, "\n%*s]", indent, "");
640 }
641 fprintf(out_file, "\n%*s}", indent - 2, "");
642}
643
644static void dump_machine_type(FILE *out_file)
645{
646 MachineClass *mc;
647
648 mc = MACHINE_GET_CLASS(current_machine);
649
650 fprintf(out_file, " \"vmschkmachine\": {\n");
651 fprintf(out_file, " \"Name\": \"%s\"\n", mc->name);
652 fprintf(out_file, " },\n");
653}
654
655void dump_vmstate_json_to_file(FILE *out_file)
656{
657 GSList *list, *elt;
658 bool first;
659
660 fprintf(out_file, "{\n");
661 dump_machine_type(out_file);
662
663 first = true;
664 list = object_class_get_list(TYPE_DEVICE, true);
665 for (elt = list; elt; elt = elt->next) {
666 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
667 TYPE_DEVICE);
668 const char *name;
669 int indent = 2;
670
671 if (!dc->vmsd) {
672 continue;
673 }
674
675 if (!first) {
676 fprintf(out_file, ",\n");
677 }
678 name = object_class_get_name(OBJECT_CLASS(dc));
679 fprintf(out_file, "%*s\"%s\": {\n", indent, "", name);
680 indent += 2;
681 fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name);
682 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
683 dc->vmsd->version_id);
684 fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "",
685 dc->vmsd->minimum_version_id);
686
687 dump_vmstate_vmsd(out_file, dc->vmsd, indent, false);
688
689 fprintf(out_file, "\n%*s}", indent - 2, "");
690 first = false;
691 }
692 fprintf(out_file, "\n}\n");
693 fclose(out_file);
26daeba4 694 g_slist_free(list);
abfd9ce3
AS
695}
696
93062e23 697static uint32_t calculate_new_instance_id(const char *idstr)
8718e999
JQ
698{
699 SaveStateEntry *se;
93062e23 700 uint32_t instance_id = 0;
8718e999 701
0163a2e0 702 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
8718e999
JQ
703 if (strcmp(idstr, se->idstr) == 0
704 && instance_id <= se->instance_id) {
705 instance_id = se->instance_id + 1;
706 }
707 }
93062e23
PX
708 /* Make sure we never loop over without being noticed */
709 assert(instance_id != VMSTATE_INSTANCE_ID_ANY);
8718e999
JQ
710 return instance_id;
711}
712
7685ee6a
AW
713static int calculate_compat_instance_id(const char *idstr)
714{
715 SaveStateEntry *se;
716 int instance_id = 0;
717
0163a2e0 718 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
38ff78d3 719 if (!se->compat) {
7685ee6a 720 continue;
38ff78d3 721 }
7685ee6a
AW
722
723 if (strcmp(idstr, se->compat->idstr) == 0
724 && instance_id <= se->compat->instance_id) {
725 instance_id = se->compat->instance_id + 1;
726 }
727 }
728 return instance_id;
729}
730
f37bc036
PX
731static inline MigrationPriority save_state_priority(SaveStateEntry *se)
732{
733 if (se->vmsd) {
734 return se->vmsd->priority;
735 }
736 return MIG_PRI_DEFAULT;
737}
738
739static void savevm_state_handler_insert(SaveStateEntry *nse)
740{
741 MigrationPriority priority = save_state_priority(nse);
742 SaveStateEntry *se;
174723ff 743 int i;
f37bc036
PX
744
745 assert(priority <= MIG_PRI_MAX);
746
174723ff
SC
747 for (i = priority - 1; i >= 0; i--) {
748 se = savevm_state.handler_pri_head[i];
749 if (se != NULL) {
750 assert(save_state_priority(se) < priority);
f37bc036
PX
751 break;
752 }
753 }
754
174723ff 755 if (i >= 0) {
f37bc036
PX
756 QTAILQ_INSERT_BEFORE(se, nse, entry);
757 } else {
758 QTAILQ_INSERT_TAIL(&savevm_state.handlers, nse, entry);
759 }
174723ff
SC
760
761 if (savevm_state.handler_pri_head[priority] == NULL) {
762 savevm_state.handler_pri_head[priority] = nse;
763 }
f37bc036
PX
764}
765
bd5de61e
SC
766static void savevm_state_handler_remove(SaveStateEntry *se)
767{
174723ff
SC
768 SaveStateEntry *next;
769 MigrationPriority priority = save_state_priority(se);
770
771 if (se == savevm_state.handler_pri_head[priority]) {
772 next = QTAILQ_NEXT(se, entry);
773 if (next != NULL && save_state_priority(next) == priority) {
774 savevm_state.handler_pri_head[priority] = next;
775 } else {
776 savevm_state.handler_pri_head[priority] = NULL;
777 }
778 }
bd5de61e
SC
779 QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
780}
781
a672b469
AL
782/* TODO: Individual devices generally have very little idea about the rest
783 of the system, so instance_id should be removed/replaced.
784 Meanwhile pass -1 as instance_id if you do not already have a clearly
785 distinguishing id for all instances of your device class. */
ce62df53 786int register_savevm_live(const char *idstr,
93062e23 787 uint32_t instance_id,
a672b469 788 int version_id,
de22ded0 789 const SaveVMHandlers *ops,
a672b469
AL
790 void *opaque)
791{
8718e999 792 SaveStateEntry *se;
a672b469 793
97f3ad35 794 se = g_new0(SaveStateEntry, 1);
a672b469 795 se->version_id = version_id;
0163a2e0 796 se->section_id = savevm_state.global_section_id++;
7908c78d 797 se->ops = ops;
a672b469 798 se->opaque = opaque;
9ed7d6ae 799 se->vmsd = NULL;
a7ae8355 800 /* if this is a live_savem then set is_ram */
9907e842 801 if (ops->save_setup != NULL) {
a7ae8355
SS
802 se->is_ram = 1;
803 }
a672b469 804
7685ee6a
AW
805 pstrcat(se->idstr, sizeof(se->idstr), idstr);
806
1df2c9a2 807 if (instance_id == VMSTATE_INSTANCE_ID_ANY) {
7685ee6a 808 se->instance_id = calculate_new_instance_id(se->idstr);
8718e999
JQ
809 } else {
810 se->instance_id = instance_id;
a672b469 811 }
7685ee6a 812 assert(!se->compat || se->instance_id == 0);
f37bc036 813 savevm_state_handler_insert(se);
a672b469
AL
814 return 0;
815}
816
3cad405b 817void unregister_savevm(VMStateIf *obj, const char *idstr, void *opaque)
41bd13af 818{
8718e999 819 SaveStateEntry *se, *new_se;
7685ee6a
AW
820 char id[256] = "";
821
3cad405b
MAL
822 if (obj) {
823 char *oid = vmstate_if_get_id(obj);
824 if (oid) {
825 pstrcpy(id, sizeof(id), oid);
7685ee6a 826 pstrcat(id, sizeof(id), "/");
3cad405b 827 g_free(oid);
7685ee6a
AW
828 }
829 }
830 pstrcat(id, sizeof(id), idstr);
41bd13af 831
0163a2e0 832 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
7685ee6a 833 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
bd5de61e 834 savevm_state_handler_remove(se);
ef1e1e07 835 g_free(se->compat);
7267c094 836 g_free(se);
41bd13af 837 }
41bd13af
AL
838 }
839}
840
93062e23 841int vmstate_register_with_alias_id(VMStateIf *obj, uint32_t instance_id,
4d2ffa08
JK
842 const VMStateDescription *vmsd,
843 void *opaque, int alias_id,
bc5c4f21
DDAG
844 int required_for_version,
845 Error **errp)
9ed7d6ae 846{
8718e999 847 SaveStateEntry *se;
9ed7d6ae 848
4d2ffa08
JK
849 /* If this triggers, alias support can be dropped for the vmsd. */
850 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
851
97f3ad35 852 se = g_new0(SaveStateEntry, 1);
9ed7d6ae 853 se->version_id = vmsd->version_id;
0163a2e0 854 se->section_id = savevm_state.global_section_id++;
9ed7d6ae
JQ
855 se->opaque = opaque;
856 se->vmsd = vmsd;
4d2ffa08 857 se->alias_id = alias_id;
9ed7d6ae 858
3cad405b
MAL
859 if (obj) {
860 char *id = vmstate_if_get_id(obj);
7685ee6a 861 if (id) {
581f08ba
DDAG
862 if (snprintf(se->idstr, sizeof(se->idstr), "%s/", id) >=
863 sizeof(se->idstr)) {
864 error_setg(errp, "Path too long for VMState (%s)", id);
865 g_free(id);
866 g_free(se);
867
868 return -1;
869 }
128e4e10 870 g_free(id);
7685ee6a 871
97f3ad35 872 se->compat = g_new0(CompatEntry, 1);
7685ee6a 873 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1df2c9a2 874 se->compat->instance_id = instance_id == VMSTATE_INSTANCE_ID_ANY ?
7685ee6a 875 calculate_compat_instance_id(vmsd->name) : instance_id;
1df2c9a2 876 instance_id = VMSTATE_INSTANCE_ID_ANY;
7685ee6a
AW
877 }
878 }
879 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
880
1df2c9a2 881 if (instance_id == VMSTATE_INSTANCE_ID_ANY) {
7685ee6a 882 se->instance_id = calculate_new_instance_id(se->idstr);
8718e999
JQ
883 } else {
884 se->instance_id = instance_id;
9ed7d6ae 885 }
7685ee6a 886 assert(!se->compat || se->instance_id == 0);
f37bc036 887 savevm_state_handler_insert(se);
9ed7d6ae
JQ
888 return 0;
889}
890
3cad405b 891void vmstate_unregister(VMStateIf *obj, const VMStateDescription *vmsd,
0be71e32 892 void *opaque)
9ed7d6ae 893{
1eb7538b
JQ
894 SaveStateEntry *se, *new_se;
895
0163a2e0 896 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
1eb7538b 897 if (se->vmsd == vmsd && se->opaque == opaque) {
bd5de61e 898 savevm_state_handler_remove(se);
ef1e1e07 899 g_free(se->compat);
7267c094 900 g_free(se);
1eb7538b
JQ
901 }
902 }
9ed7d6ae
JQ
903}
904
3a011c26 905static int vmstate_load(QEMUFile *f, SaveStateEntry *se)
4082be4d 906{
9013dca5 907 trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
9ed7d6ae 908 if (!se->vmsd) { /* Old style */
3a011c26 909 return se->ops->load_state(f, se->opaque, se->load_version_id);
9ed7d6ae 910 }
3a011c26 911 return vmstate_load_state(f, se->vmsd, se->opaque, se->load_version_id);
4082be4d
JQ
912}
913
3ddba9a9
MA
914static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se,
915 JSONWriter *vmdesc)
8118f095
AG
916{
917 int64_t old_offset, size;
918
919 old_offset = qemu_ftell_fast(f);
920 se->ops->save_state(f, se->opaque);
921 size = qemu_ftell_fast(f) - old_offset;
922
923 if (vmdesc) {
3ddba9a9
MA
924 json_writer_int64(vmdesc, "size", size);
925 json_writer_start_array(vmdesc, "fields");
926 json_writer_start_object(vmdesc, NULL);
927 json_writer_str(vmdesc, "name", "data");
928 json_writer_int64(vmdesc, "size", size);
929 json_writer_str(vmdesc, "type", "buffer");
930 json_writer_end_object(vmdesc);
931 json_writer_end_array(vmdesc);
8118f095
AG
932 }
933}
934
3ddba9a9
MA
935static int vmstate_save(QEMUFile *f, SaveStateEntry *se,
936 JSONWriter *vmdesc)
4082be4d 937{
9013dca5 938 trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
8118f095
AG
939 if (!se->vmsd) {
940 vmstate_save_old_style(f, se, vmdesc);
687433f6 941 return 0;
9ed7d6ae 942 }
687433f6 943 return vmstate_save_state(f, se->vmsd, se->opaque, vmdesc);
4082be4d
JQ
944}
945
ce39bfc9
DDAG
946/*
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
f68945d4
DDAG
967/*
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{
15c38503 973 if (migrate_get_current()->send_section_footer) {
f68945d4
DDAG
974 qemu_put_byte(f, QEMU_VM_SECTION_FOOTER);
975 qemu_put_be32(f, se->section_id);
976 }
977}
978
c76ca188
DDAG
979/**
980 * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the
981 * command and associated data.
982 *
983 * @f: File to send command on
984 * @command: Command type to send
985 * @len: Length of associated data
986 * @data: Data associated with command.
987 */
20a519a0
JQ
988static void qemu_savevm_command_send(QEMUFile *f,
989 enum qemu_vm_cmd command,
990 uint16_t len,
991 uint8_t *data)
c76ca188
DDAG
992{
993 trace_savevm_command_send(command, len);
994 qemu_put_byte(f, QEMU_VM_COMMAND);
995 qemu_put_be16(f, (uint16_t)command);
996 qemu_put_be16(f, len);
997 qemu_put_buffer(f, data, len);
998 qemu_fflush(f);
999}
1000
aad555c2
ZC
1001void qemu_savevm_send_colo_enable(QEMUFile *f)
1002{
1003 trace_savevm_send_colo_enable();
1004 qemu_savevm_command_send(f, MIG_CMD_ENABLE_COLO, 0, NULL);
1005}
1006
2e37701e
DDAG
1007void qemu_savevm_send_ping(QEMUFile *f, uint32_t value)
1008{
1009 uint32_t buf;
1010
1011 trace_savevm_send_ping(value);
1012 buf = cpu_to_be32(value);
1013 qemu_savevm_command_send(f, MIG_CMD_PING, sizeof(value), (uint8_t *)&buf);
1014}
1015
1016void qemu_savevm_send_open_return_path(QEMUFile *f)
1017{
1018 trace_savevm_send_open_return_path();
1019 qemu_savevm_command_send(f, MIG_CMD_OPEN_RETURN_PATH, 0, NULL);
1020}
1021
11cf1d98
DDAG
1022/* We have a buffer of data to send; we don't want that all to be loaded
1023 * by the command itself, so the command contains just the length of the
1024 * extra buffer that we then send straight after it.
1025 * TODO: Must be a better way to organise that
1026 *
1027 * Returns:
1028 * 0 on success
1029 * -ve on error
1030 */
61b67d47 1031int qemu_savevm_send_packaged(QEMUFile *f, const uint8_t *buf, size_t len)
11cf1d98 1032{
11cf1d98
DDAG
1033 uint32_t tmp;
1034
1035 if (len > MAX_VM_CMD_PACKAGED_SIZE) {
1036 error_report("%s: Unreasonably large packaged state: %zu",
1037 __func__, len);
1038 return -1;
1039 }
1040
1041 tmp = cpu_to_be32(len);
1042
1043 trace_qemu_savevm_send_packaged();
1044 qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp);
1045
61b67d47 1046 qemu_put_buffer(f, buf, len);
11cf1d98
DDAG
1047
1048 return 0;
1049}
1050
093e3c42
DDAG
1051/* Send prior to any postcopy transfer */
1052void qemu_savevm_send_postcopy_advise(QEMUFile *f)
1053{
58110f0a
VSO
1054 if (migrate_postcopy_ram()) {
1055 uint64_t tmp[2];
1056 tmp[0] = cpu_to_be64(ram_pagesize_summary());
1057 tmp[1] = cpu_to_be64(qemu_target_page_size());
1058
1059 trace_qemu_savevm_send_postcopy_advise();
1060 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE,
1061 16, (uint8_t *)tmp);
1062 } else {
1063 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 0, NULL);
1064 }
093e3c42
DDAG
1065}
1066
1067/* Sent prior to starting the destination running in postcopy, discard pages
1068 * that have already been sent but redirtied on the source.
1069 * CMD_POSTCOPY_RAM_DISCARD consist of:
1070 * byte version (0)
1071 * byte Length of name field (not including 0)
1072 * n x byte RAM block name
1073 * byte 0 terminator (just for safety)
1074 * n x Byte ranges within the named RAMBlock
1075 * be64 Start of the range
1076 * be64 Length
1077 *
1078 * name: RAMBlock name that these entries are part of
1079 * len: Number of page entries
1080 * start_list: 'len' addresses
1081 * length_list: 'len' addresses
1082 *
1083 */
1084void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name,
1085 uint16_t len,
1086 uint64_t *start_list,
1087 uint64_t *length_list)
1088{
1089 uint8_t *buf;
1090 uint16_t tmplen;
1091 uint16_t t;
1092 size_t name_len = strlen(name);
1093
1094 trace_qemu_savevm_send_postcopy_ram_discard(name, len);
1095 assert(name_len < 256);
1096 buf = g_malloc0(1 + 1 + name_len + 1 + (8 + 8) * len);
1097 buf[0] = postcopy_ram_discard_version;
1098 buf[1] = name_len;
1099 memcpy(buf + 2, name, name_len);
1100 tmplen = 2 + name_len;
1101 buf[tmplen++] = '\0';
1102
1103 for (t = 0; t < len; t++) {
4d885131 1104 stq_be_p(buf + tmplen, start_list[t]);
093e3c42 1105 tmplen += 8;
4d885131 1106 stq_be_p(buf + tmplen, length_list[t]);
093e3c42
DDAG
1107 tmplen += 8;
1108 }
1109 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RAM_DISCARD, tmplen, buf);
1110 g_free(buf);
1111}
1112
1113/* Get the destination into a state where it can receive postcopy data. */
1114void qemu_savevm_send_postcopy_listen(QEMUFile *f)
1115{
1116 trace_savevm_send_postcopy_listen();
1117 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_LISTEN, 0, NULL);
1118}
1119
1120/* Kick the destination into running */
1121void qemu_savevm_send_postcopy_run(QEMUFile *f)
1122{
1123 trace_savevm_send_postcopy_run();
1124 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RUN, 0, NULL);
1125}
1126
3f5875ec
PX
1127void qemu_savevm_send_postcopy_resume(QEMUFile *f)
1128{
1129 trace_savevm_send_postcopy_resume();
1130 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RESUME, 0, NULL);
1131}
1132
f25d4225
PX
1133void qemu_savevm_send_recv_bitmap(QEMUFile *f, char *block_name)
1134{
1135 size_t len;
1136 char buf[256];
1137
1138 trace_savevm_send_recv_bitmap(block_name);
1139
1140 buf[0] = len = strlen(block_name);
1141 memcpy(buf + 1, block_name, len);
1142
1143 qemu_savevm_command_send(f, MIG_CMD_RECV_BITMAP, len + 1, (uint8_t *)buf);
1144}
1145
e1c37d0e 1146bool qemu_savevm_state_blocked(Error **errp)
dc912121
AW
1147{
1148 SaveStateEntry *se;
1149
0163a2e0 1150 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
7d854c47 1151 if (se->vmsd && se->vmsd->unmigratable) {
f231b88d
CR
1152 error_setg(errp, "State blocked by non-migratable device '%s'",
1153 se->idstr);
dc912121
AW
1154 return true;
1155 }
1156 }
1157 return false;
1158}
1159
3af8554b
DDAG
1160void qemu_savevm_non_migratable_list(strList **reasons)
1161{
1162 SaveStateEntry *se;
1163
1164 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1165 if (se->vmsd && se->vmsd->unmigratable) {
1166 QAPI_LIST_PREPEND(*reasons,
1167 g_strdup_printf("non-migratable device: %s",
1168 se->idstr));
1169 }
1170 }
1171}
1172
f796baa1
DDAG
1173void qemu_savevm_state_header(QEMUFile *f)
1174{
1175 trace_savevm_state_header();
1176 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1177 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
172dfd4f 1178
4ffdb337 1179 if (migrate_get_current()->send_configuration) {
172dfd4f
DDAG
1180 qemu_put_byte(f, QEMU_VM_CONFIGURATION);
1181 vmstate_save_state(f, &vmstate_configuration, &savevm_state, 0);
1182 }
f796baa1
DDAG
1183}
1184
d05de9e3 1185bool qemu_savevm_state_guest_unplug_pending(void)
c7e0acd5
JF
1186{
1187 SaveStateEntry *se;
c7e0acd5
JF
1188
1189 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
284f42a5
JF
1190 if (se->vmsd && se->vmsd->dev_unplug_pending &&
1191 se->vmsd->dev_unplug_pending(se->opaque)) {
d05de9e3 1192 return true;
c7e0acd5
JF
1193 }
1194 }
1195
d05de9e3 1196 return false;
c7e0acd5
JF
1197}
1198
9907e842 1199void qemu_savevm_state_setup(QEMUFile *f)
a672b469
AL
1200{
1201 SaveStateEntry *se;
bd227060 1202 Error *local_err = NULL;
39346385 1203 int ret;
a672b469 1204
9907e842 1205 trace_savevm_state_setup();
0163a2e0 1206 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
9907e842 1207 if (!se->ops || !se->ops->save_setup) {
a672b469 1208 continue;
22ea40f4 1209 }
cea3b4c0 1210 if (se->ops->is_active) {
6bd68781
JQ
1211 if (!se->ops->is_active(se->opaque)) {
1212 continue;
1213 }
1214 }
ce39bfc9 1215 save_section_header(f, se, QEMU_VM_SECTION_START);
a672b469 1216
9907e842 1217 ret = se->ops->save_setup(f, se->opaque);
f68945d4 1218 save_section_footer(f, se);
2975725f 1219 if (ret < 0) {
47c8c17a
PB
1220 qemu_file_set_error(f, ret);
1221 break;
2975725f 1222 }
a672b469 1223 }
bd227060
WW
1224
1225 if (precopy_notify(PRECOPY_NOTIFY_SETUP, &local_err)) {
1226 error_report_err(local_err);
1227 }
a672b469
AL
1228}
1229
d1b8eadb
PX
1230int qemu_savevm_state_resume_prepare(MigrationState *s)
1231{
1232 SaveStateEntry *se;
1233 int ret;
1234
1235 trace_savevm_state_resume_prepare();
1236
1237 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1238 if (!se->ops || !se->ops->resume_prepare) {
1239 continue;
1240 }
cea3b4c0 1241 if (se->ops->is_active) {
d1b8eadb
PX
1242 if (!se->ops->is_active(se->opaque)) {
1243 continue;
1244 }
1245 }
1246 ret = se->ops->resume_prepare(s, se->opaque);
1247 if (ret < 0) {
1248 return ret;
1249 }
1250 }
1251
1252 return 0;
1253}
1254
39346385 1255/*
07f35073 1256 * this function has three return values:
39346385
JQ
1257 * negative: there was one error, and we have -errno.
1258 * 0 : We haven't finished, caller have to go again
1259 * 1 : We have finished, we can go to complete phase
1260 */
35ecd943 1261int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy)
a672b469
AL
1262{
1263 SaveStateEntry *se;
1264 int ret = 1;
1265
9013dca5 1266 trace_savevm_state_iterate();
0163a2e0 1267 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
16310a3c 1268 if (!se->ops || !se->ops->save_live_iterate) {
a672b469 1269 continue;
22ea40f4 1270 }
a94cd7b8
WY
1271 if (se->ops->is_active &&
1272 !se->ops->is_active(se->opaque)) {
1273 continue;
6bd68781 1274 }
a94cd7b8
WY
1275 if (se->ops->is_active_iterate &&
1276 !se->ops->is_active_iterate(se->opaque)) {
1277 continue;
c865d848 1278 }
35ecd943
DDAG
1279 /*
1280 * In the postcopy phase, any device that doesn't know how to
1281 * do postcopy should have saved it's state in the _complete
1282 * call that's already run, it might get confused if we call
1283 * iterate afterwards.
1284 */
c6467627
VSO
1285 if (postcopy &&
1286 !(se->ops->has_postcopy && se->ops->has_postcopy(se->opaque))) {
35ecd943
DDAG
1287 continue;
1288 }
aac844ed
JQ
1289 if (qemu_file_rate_limit(f)) {
1290 return 0;
1291 }
464400f6 1292 trace_savevm_section_start(se->idstr, se->section_id);
ce39bfc9
DDAG
1293
1294 save_section_header(f, se, QEMU_VM_SECTION_PART);
a672b469 1295
16310a3c 1296 ret = se->ops->save_live_iterate(f, se->opaque);
a5df2a02 1297 trace_savevm_section_end(se->idstr, se->section_id, ret);
f68945d4 1298 save_section_footer(f, se);
517a13c9 1299
47c8c17a 1300 if (ret < 0) {
4991f309
WY
1301 error_report("failed to save SaveStateEntry with id(name): %d(%s)",
1302 se->section_id, se->idstr);
47c8c17a
PB
1303 qemu_file_set_error(f, ret);
1304 }
2975725f 1305 if (ret <= 0) {
90697be8
JK
1306 /* Do not proceed to the next vmstate before this one reported
1307 completion of the current stage. This serializes the migration
1308 and reduces the probability that a faster changing state is
1309 synchronized over and over again. */
1310 break;
1311 }
a672b469 1312 }
39346385 1313 return ret;
a672b469
AL
1314}
1315
9850c604
AG
1316static bool should_send_vmdesc(void)
1317{
1318 MachineState *machine = MACHINE(qdev_get_machine());
5727309d 1319 bool in_postcopy = migration_in_postcopy();
8421b205 1320 return !machine->suppress_vmdesc && !in_postcopy;
9850c604
AG
1321}
1322
763c906b
DDAG
1323/*
1324 * Calls the save_live_complete_postcopy methods
1325 * causing the last few pages to be sent immediately and doing any associated
1326 * cleanup.
1327 * Note postcopy also calls qemu_savevm_state_complete_precopy to complete
1328 * all the other devices, but that happens at the point we switch to postcopy.
1329 */
1330void qemu_savevm_state_complete_postcopy(QEMUFile *f)
1331{
1332 SaveStateEntry *se;
1333 int ret;
1334
1335 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1336 if (!se->ops || !se->ops->save_live_complete_postcopy) {
1337 continue;
1338 }
cea3b4c0 1339 if (se->ops->is_active) {
763c906b
DDAG
1340 if (!se->ops->is_active(se->opaque)) {
1341 continue;
1342 }
1343 }
1344 trace_savevm_section_start(se->idstr, se->section_id);
1345 /* Section type */
1346 qemu_put_byte(f, QEMU_VM_SECTION_END);
1347 qemu_put_be32(f, se->section_id);
1348
1349 ret = se->ops->save_live_complete_postcopy(f, se->opaque);
1350 trace_savevm_section_end(se->idstr, se->section_id, ret);
1351 save_section_footer(f, se);
1352 if (ret < 0) {
1353 qemu_file_set_error(f, ret);
1354 return;
1355 }
1356 }
1357
1358 qemu_put_byte(f, QEMU_VM_EOF);
1359 qemu_fflush(f);
1360}
1361
622a80c9 1362static
e326767b 1363int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)
a672b469
AL
1364{
1365 SaveStateEntry *se;
2975725f 1366 int ret;
ea375f9a 1367
0163a2e0 1368 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
763c906b 1369 if (!se->ops ||
c6467627
VSO
1370 (in_postcopy && se->ops->has_postcopy &&
1371 se->ops->has_postcopy(se->opaque)) ||
763c906b 1372 !se->ops->save_live_complete_precopy) {
a672b469 1373 continue;
22ea40f4 1374 }
1c0d249d 1375
cea3b4c0 1376 if (se->ops->is_active) {
6bd68781
JQ
1377 if (!se->ops->is_active(se->opaque)) {
1378 continue;
1379 }
1380 }
464400f6 1381 trace_savevm_section_start(se->idstr, se->section_id);
ce39bfc9
DDAG
1382
1383 save_section_header(f, se, QEMU_VM_SECTION_END);
a672b469 1384
a3e06c3d 1385 ret = se->ops->save_live_complete_precopy(f, se->opaque);
a5df2a02 1386 trace_savevm_section_end(se->idstr, se->section_id, ret);
f68945d4 1387 save_section_footer(f, se);
2975725f 1388 if (ret < 0) {
47c8c17a 1389 qemu_file_set_error(f, ret);
a1fbe750 1390 return -1;
2975725f 1391 }
a672b469
AL
1392 }
1393
622a80c9
WY
1394 return 0;
1395}
1396
622a80c9
WY
1397int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f,
1398 bool in_postcopy,
1399 bool inactivate_disks)
1400{
3ddba9a9 1401 g_autoptr(JSONWriter) vmdesc = NULL;
622a80c9
WY
1402 int vmdesc_len;
1403 SaveStateEntry *se;
1404 int ret;
1c0d249d 1405
3ddba9a9
MA
1406 vmdesc = json_writer_new(false);
1407 json_writer_start_object(vmdesc, NULL);
1408 json_writer_int64(vmdesc, "page_size", qemu_target_page_size());
1409 json_writer_start_array(vmdesc, "devices");
0163a2e0 1410 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
a672b469 1411
22ea40f4 1412 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
5cecf414 1413 continue;
22ea40f4 1414 }
df896152
JQ
1415 if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1416 trace_savevm_section_skip(se->idstr, se->section_id);
1417 continue;
1418 }
1419
464400f6 1420 trace_savevm_section_start(se->idstr, se->section_id);
8118f095 1421
3ddba9a9
MA
1422 json_writer_start_object(vmdesc, NULL);
1423 json_writer_str(vmdesc, "name", se->idstr);
1424 json_writer_int64(vmdesc, "instance_id", se->instance_id);
8118f095 1425
ce39bfc9 1426 save_section_header(f, se, QEMU_VM_SECTION_FULL);
687433f6
DDAG
1427 ret = vmstate_save(f, se, vmdesc);
1428 if (ret) {
1429 qemu_file_set_error(f, ret);
1430 return ret;
1431 }
a5df2a02 1432 trace_savevm_section_end(se->idstr, se->section_id, 0);
f68945d4 1433 save_section_footer(f, se);
bdf46d64 1434
3ddba9a9 1435 json_writer_end_object(vmdesc);
a672b469
AL
1436 }
1437
a1fbe750
FZ
1438 if (inactivate_disks) {
1439 /* Inactivate before sending QEMU_VM_EOF so that the
1440 * bdrv_invalidate_cache_all() on the other end won't fail. */
1441 ret = bdrv_inactivate_all();
1442 if (ret) {
c232bf58
DDAG
1443 error_report("%s: bdrv_inactivate_all() failed (%d)",
1444 __func__, ret);
a1fbe750
FZ
1445 qemu_file_set_error(f, ret);
1446 return ret;
1447 }
1448 }
763c906b
DDAG
1449 if (!in_postcopy) {
1450 /* Postcopy stream will still be going */
1451 qemu_put_byte(f, QEMU_VM_EOF);
1452 }
8118f095 1453
3ddba9a9
MA
1454 json_writer_end_array(vmdesc);
1455 json_writer_end_object(vmdesc);
1456 vmdesc_len = strlen(json_writer_get(vmdesc));
8118f095 1457
9850c604
AG
1458 if (should_send_vmdesc()) {
1459 qemu_put_byte(f, QEMU_VM_VMDESCRIPTION);
1460 qemu_put_be32(f, vmdesc_len);
3ddba9a9 1461 qemu_put_buffer(f, (uint8_t *)json_writer_get(vmdesc), vmdesc_len);
9850c604 1462 }
8118f095 1463
622a80c9
WY
1464 return 0;
1465}
1466
1467int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only,
1468 bool inactivate_disks)
1469{
1470 int ret;
1471 Error *local_err = NULL;
1472 bool in_postcopy = migration_in_postcopy();
1473
1474 if (precopy_notify(PRECOPY_NOTIFY_COMPLETE, &local_err)) {
1475 error_report_err(local_err);
1476 }
1477
1478 trace_savevm_state_complete_precopy();
1479
1480 cpu_synchronize_all_states();
1481
e326767b
WY
1482 if (!in_postcopy || iterable_only) {
1483 ret = qemu_savevm_state_complete_precopy_iterable(f, in_postcopy);
1484 if (ret) {
1485 return ret;
1486 }
622a80c9
WY
1487 }
1488
1489 if (iterable_only) {
1490 goto flush;
1491 }
1492
1493 ret = qemu_savevm_state_complete_precopy_non_iterable(f, in_postcopy,
1494 inactivate_disks);
1495 if (ret) {
1496 return ret;
1497 }
1498
4e455d51 1499flush:
edaae611 1500 qemu_fflush(f);
a1fbe750 1501 return 0;
a672b469
AL
1502}
1503
c31b098f
DDAG
1504/* Give an estimate of the amount left to be transferred,
1505 * the result is split into the amount for units that can and
1506 * for units that can't do postcopy.
1507 */
faec066a 1508void qemu_savevm_state_pending(QEMUFile *f, uint64_t threshold_size,
47995026
VSO
1509 uint64_t *res_precopy_only,
1510 uint64_t *res_compatible,
1511 uint64_t *res_postcopy_only)
e4ed1541
JQ
1512{
1513 SaveStateEntry *se;
c31b098f 1514
47995026
VSO
1515 *res_precopy_only = 0;
1516 *res_compatible = 0;
1517 *res_postcopy_only = 0;
c31b098f 1518
e4ed1541 1519
0163a2e0 1520 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
e4ed1541
JQ
1521 if (!se->ops || !se->ops->save_live_pending) {
1522 continue;
1523 }
cea3b4c0 1524 if (se->ops->is_active) {
e4ed1541
JQ
1525 if (!se->ops->is_active(se->opaque)) {
1526 continue;
1527 }
1528 }
faec066a 1529 se->ops->save_live_pending(f, se->opaque, threshold_size,
47995026
VSO
1530 res_precopy_only, res_compatible,
1531 res_postcopy_only);
e4ed1541 1532 }
e4ed1541
JQ
1533}
1534
ea7415fa 1535void qemu_savevm_state_cleanup(void)
4ec7fcc7
JK
1536{
1537 SaveStateEntry *se;
bd227060
WW
1538 Error *local_err = NULL;
1539
1540 if (precopy_notify(PRECOPY_NOTIFY_CLEANUP, &local_err)) {
1541 error_report_err(local_err);
1542 }
4ec7fcc7 1543
ea7415fa 1544 trace_savevm_state_cleanup();
0163a2e0 1545 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
70f794fc
JQ
1546 if (se->ops && se->ops->save_cleanup) {
1547 se->ops->save_cleanup(se->opaque);
4ec7fcc7
JK
1548 }
1549 }
1550}
1551
5d80448c 1552static int qemu_savevm_state(QEMUFile *f, Error **errp)
a672b469 1553{
a672b469 1554 int ret;
3e0c8050 1555 MigrationState *ms = migrate_get_current();
6dcf6668 1556 MigrationStatus status;
3e0c8050 1557
392d87e2 1558 if (migration_is_running(ms->state)) {
3d63da16
JL
1559 error_setg(errp, QERR_MIGRATION_ACTIVE);
1560 return -EINVAL;
1561 }
a672b469 1562
ce7c817c
JQ
1563 if (migrate_use_block()) {
1564 error_setg(errp, "Block migration and snapshots are incompatible");
3d63da16 1565 return -EINVAL;
ce7c817c
JQ
1566 }
1567
3d63da16 1568 migrate_init(ms);
87f3bd87 1569 memset(&ram_counters, 0, sizeof(ram_counters));
02abee3d 1570 memset(&compression_counters, 0, sizeof(compression_counters));
3d63da16
JL
1571 ms->to_dst_file = f;
1572
9b095037 1573 qemu_mutex_unlock_iothread();
f796baa1 1574 qemu_savevm_state_header(f);
9907e842 1575 qemu_savevm_state_setup(f);
9b095037
PB
1576 qemu_mutex_lock_iothread();
1577
47c8c17a 1578 while (qemu_file_get_error(f) == 0) {
35ecd943 1579 if (qemu_savevm_state_iterate(f, false) > 0) {
47c8c17a
PB
1580 break;
1581 }
1582 }
a672b469 1583
47c8c17a 1584 ret = qemu_file_get_error(f);
39346385 1585 if (ret == 0) {
a1fbe750 1586 qemu_savevm_state_complete_precopy(f, false, false);
624b9cc2 1587 ret = qemu_file_get_error(f);
39346385 1588 }
15b3b8ea 1589 qemu_savevm_state_cleanup();
04943eba 1590 if (ret != 0) {
5d80448c 1591 error_setg_errno(errp, -ret, "Error while writing VM state");
04943eba 1592 }
6dcf6668 1593
6dcf6668
DL
1594 if (ret != 0) {
1595 status = MIGRATION_STATUS_FAILED;
1596 } else {
1597 status = MIGRATION_STATUS_COMPLETED;
1598 }
1599 migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP, status);
f9c8caa0
VSO
1600
1601 /* f is outer parameter, it should not stay in global migration state after
1602 * this function finished */
1603 ms->to_dst_file = NULL;
1604
a672b469
AL
1605 return ret;
1606}
1607
3f6df99d 1608void qemu_savevm_live_state(QEMUFile *f)
a7ae8355 1609{
3f6df99d
ZC
1610 /* save QEMU_VM_SECTION_END section */
1611 qemu_savevm_state_complete_precopy(f, true, false);
1612 qemu_put_byte(f, QEMU_VM_EOF);
1613}
a7ae8355 1614
3f6df99d
ZC
1615int qemu_save_device_state(QEMUFile *f)
1616{
1617 SaveStateEntry *se;
a7ae8355 1618
3f6df99d
ZC
1619 if (!migration_in_colo_state()) {
1620 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1621 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1622 }
a7ae8355
SS
1623 cpu_synchronize_all_states();
1624
0163a2e0 1625 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
687433f6
DDAG
1626 int ret;
1627
a7ae8355
SS
1628 if (se->is_ram) {
1629 continue;
1630 }
22ea40f4 1631 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
a7ae8355
SS
1632 continue;
1633 }
df896152
JQ
1634 if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1635 continue;
1636 }
a7ae8355 1637
ce39bfc9 1638 save_section_header(f, se, QEMU_VM_SECTION_FULL);
a7ae8355 1639
687433f6
DDAG
1640 ret = vmstate_save(f, se, NULL);
1641 if (ret) {
1642 return ret;
1643 }
f68945d4
DDAG
1644
1645 save_section_footer(f, se);
a7ae8355
SS
1646 }
1647
1648 qemu_put_byte(f, QEMU_VM_EOF);
1649
1650 return qemu_file_get_error(f);
1651}
1652
93062e23 1653static SaveStateEntry *find_se(const char *idstr, uint32_t instance_id)
a672b469
AL
1654{
1655 SaveStateEntry *se;
1656
0163a2e0 1657 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
a672b469 1658 if (!strcmp(se->idstr, idstr) &&
4d2ffa08
JK
1659 (instance_id == se->instance_id ||
1660 instance_id == se->alias_id))
a672b469 1661 return se;
7685ee6a
AW
1662 /* Migrating from an older version? */
1663 if (strstr(se->idstr, idstr) && se->compat) {
1664 if (!strcmp(se->compat->idstr, idstr) &&
1665 (instance_id == se->compat->instance_id ||
1666 instance_id == se->alias_id))
1667 return se;
1668 }
a672b469
AL
1669 }
1670 return NULL;
1671}
1672
7b89bf27
DDAG
1673enum LoadVMExitCodes {
1674 /* Allow a command to quit all layers of nested loadvm loops */
1675 LOADVM_QUIT = 1,
1676};
1677
093e3c42
DDAG
1678/* ------ incoming postcopy messages ------ */
1679/* 'advise' arrives before any transfers just to tell us that a postcopy
1680 * *might* happen - it might be skipped if precopy transferred everything
1681 * quickly.
1682 */
875fcd01
GK
1683static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis,
1684 uint16_t len)
093e3c42
DDAG
1685{
1686 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
e8ca1db2 1687 uint64_t remote_pagesize_summary, local_pagesize_summary, remote_tps;
144fa06b 1688 size_t page_size = qemu_target_page_size();
d3dff7a5 1689 Error *local_err = NULL;
093e3c42
DDAG
1690
1691 trace_loadvm_postcopy_handle_advise();
1692 if (ps != POSTCOPY_INCOMING_NONE) {
1693 error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps);
1694 return -1;
1695 }
1696
875fcd01
GK
1697 switch (len) {
1698 case 0:
1699 if (migrate_postcopy_ram()) {
1700 error_report("RAM postcopy is enabled but have 0 byte advise");
1701 return -EINVAL;
1702 }
58110f0a 1703 return 0;
875fcd01
GK
1704 case 8 + 8:
1705 if (!migrate_postcopy_ram()) {
1706 error_report("RAM postcopy is disabled but have 16 byte advise");
1707 return -EINVAL;
1708 }
1709 break;
1710 default:
1711 error_report("CMD_POSTCOPY_ADVISE invalid length (%d)", len);
1712 return -EINVAL;
58110f0a
VSO
1713 }
1714
d7651f15 1715 if (!postcopy_ram_supported_by_host(mis)) {
328d4d85 1716 postcopy_state_set(POSTCOPY_INCOMING_NONE);
eb59db53
DDAG
1717 return -1;
1718 }
1719
e8ca1db2
DDAG
1720 remote_pagesize_summary = qemu_get_be64(mis->from_src_file);
1721 local_pagesize_summary = ram_pagesize_summary();
1722
1723 if (remote_pagesize_summary != local_pagesize_summary) {
093e3c42 1724 /*
e8ca1db2
DDAG
1725 * This detects two potential causes of mismatch:
1726 * a) A mismatch in host page sizes
1727 * Some combinations of mismatch are probably possible but it gets
1728 * a bit more complicated. In particular we need to place whole
1729 * host pages on the dest at once, and we need to ensure that we
1730 * handle dirtying to make sure we never end up sending part of
1731 * a hostpage on it's own.
1732 * b) The use of different huge page sizes on source/destination
1733 * a more fine grain test is performed during RAM block migration
1734 * but this test here causes a nice early clear failure, and
1735 * also fails when passed to an older qemu that doesn't
1736 * do huge pages.
093e3c42 1737 */
e8ca1db2
DDAG
1738 error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64
1739 " d=%" PRIx64 ")",
1740 remote_pagesize_summary, local_pagesize_summary);
093e3c42
DDAG
1741 return -1;
1742 }
1743
1744 remote_tps = qemu_get_be64(mis->from_src_file);
144fa06b 1745 if (remote_tps != page_size) {
093e3c42
DDAG
1746 /*
1747 * Again, some differences could be dealt with, but for now keep it
1748 * simple.
1749 */
20afaed9 1750 error_report("Postcopy needs matching target page sizes (s=%d d=%zd)",
144fa06b 1751 (int)remote_tps, page_size);
093e3c42
DDAG
1752 return -1;
1753 }
1754
d3dff7a5
DDAG
1755 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_ADVISE, &local_err)) {
1756 error_report_err(local_err);
1757 return -1;
1758 }
1759
1caddf8a
DDAG
1760 if (ram_postcopy_incoming_init(mis)) {
1761 return -1;
1762 }
1763
093e3c42
DDAG
1764 return 0;
1765}
1766
1767/* After postcopy we will be told to throw some pages away since they're
1768 * dirty and will have to be demand fetched. Must happen before CPU is
1769 * started.
1770 * There can be 0..many of these messages, each encoding multiple pages.
1771 */
1772static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis,
1773 uint16_t len)
1774{
1775 int tmp;
1776 char ramid[256];
1777 PostcopyState ps = postcopy_state_get();
1778
1779 trace_loadvm_postcopy_ram_handle_discard();
1780
1781 switch (ps) {
1782 case POSTCOPY_INCOMING_ADVISE:
1783 /* 1st discard */
f9527107 1784 tmp = postcopy_ram_prepare_discard(mis);
093e3c42
DDAG
1785 if (tmp) {
1786 return tmp;
1787 }
1788 break;
1789
1790 case POSTCOPY_INCOMING_DISCARD:
1791 /* Expected state */
1792 break;
1793
1794 default:
1795 error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)",
1796 ps);
1797 return -1;
1798 }
1799 /* We're expecting a
1800 * Version (0)
1801 * a RAM ID string (length byte, name, 0 term)
1802 * then at least 1 16 byte chunk
1803 */
1804 if (len < (1 + 1 + 1 + 1 + 2 * 8)) {
1805 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1806 return -1;
1807 }
1808
1809 tmp = qemu_get_byte(mis->from_src_file);
1810 if (tmp != postcopy_ram_discard_version) {
1811 error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp);
1812 return -1;
1813 }
1814
1815 if (!qemu_get_counted_string(mis->from_src_file, ramid)) {
1816 error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID");
1817 return -1;
1818 }
1819 tmp = qemu_get_byte(mis->from_src_file);
1820 if (tmp != 0) {
1821 error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp);
1822 return -1;
1823 }
1824
1825 len -= 3 + strlen(ramid);
1826 if (len % 16) {
1827 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1828 return -1;
1829 }
1830 trace_loadvm_postcopy_ram_handle_discard_header(ramid, len);
1831 while (len) {
093e3c42
DDAG
1832 uint64_t start_addr, block_length;
1833 start_addr = qemu_get_be64(mis->from_src_file);
1834 block_length = qemu_get_be64(mis->from_src_file);
1835
1836 len -= 16;
aaa2064c 1837 int ret = ram_discard_range(ramid, start_addr, block_length);
093e3c42
DDAG
1838 if (ret) {
1839 return ret;
1840 }
093e3c42
DDAG
1841 }
1842 trace_loadvm_postcopy_ram_handle_discard_end();
1843
1844 return 0;
1845}
1846
c76201ab
DDAG
1847/*
1848 * Triggered by a postcopy_listen command; this thread takes over reading
1849 * the input stream, leaving the main thread free to carry on loading the rest
1850 * of the device state (from RAM).
1851 * (TODO:This could do with being in a postcopy file - but there again it's
1852 * just another input loop, not that postcopy specific)
1853 */
1854static void *postcopy_ram_listen_thread(void *opaque)
1855{
c76201ab 1856 MigrationIncomingState *mis = migration_incoming_get_current();
b411b844 1857 QEMUFile *f = mis->from_src_file;
c76201ab 1858 int load_res;
ee647225
VSO
1859 MigrationState *migr = migrate_get_current();
1860
1861 object_ref(OBJECT(migr));
c76201ab 1862
6ba996bb
DDAG
1863 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
1864 MIGRATION_STATUS_POSTCOPY_ACTIVE);
c76201ab
DDAG
1865 qemu_sem_post(&mis->listen_thread_sem);
1866 trace_postcopy_ram_listen_thread_start();
1867
74637e6f 1868 rcu_register_thread();
c76201ab
DDAG
1869 /*
1870 * Because we're a thread and not a coroutine we can't yield
1871 * in qemu_file, and thus we must be blocking now.
1872 */
1873 qemu_file_set_blocking(f, true);
1874 load_res = qemu_loadvm_state_main(f, mis);
b411b844
PX
1875
1876 /*
1877 * This is tricky, but, mis->from_src_file can change after it
1878 * returns, when postcopy recovery happened. In the future, we may
1879 * want a wrapper for the QEMUFile handle.
1880 */
1881 f = mis->from_src_file;
1882
c76201ab
DDAG
1883 /* And non-blocking again so we don't block in any cleanup */
1884 qemu_file_set_blocking(f, false);
1885
1886 trace_postcopy_ram_listen_thread_exit();
1887 if (load_res < 0) {
c76201ab 1888 qemu_file_set_error(f, load_res);
ee647225
VSO
1889 dirty_bitmap_mig_cancel_incoming();
1890 if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING &&
1891 !migrate_postcopy_ram() && migrate_dirty_bitmaps())
1892 {
1893 error_report("%s: loadvm failed during postcopy: %d. All states "
1894 "are migrated except dirty bitmaps. Some dirty "
1895 "bitmaps may be lost, and present migrated dirty "
1896 "bitmaps are correctly migrated and valid.",
1897 __func__, load_res);
1898 load_res = 0; /* prevent further exit() */
1899 } else {
1900 error_report("%s: loadvm failed: %d", __func__, load_res);
1901 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1902 MIGRATION_STATUS_FAILED);
1903 }
1904 }
1905 if (load_res >= 0) {
c76201ab
DDAG
1906 /*
1907 * This looks good, but it's possible that the device loading in the
1908 * main thread hasn't finished yet, and so we might not be in 'RUN'
1909 * state yet; wait for the end of the main thread.
1910 */
1911 qemu_event_wait(&mis->main_thread_load_event);
1912 }
1913 postcopy_ram_incoming_cleanup(mis);
c76201ab
DDAG
1914
1915 if (load_res < 0) {
1916 /*
1917 * If something went wrong then we have a bad state so exit;
1918 * depending how far we got it might be possible at this point
1919 * to leave the guest running and fire MCEs for pages that never
1920 * arrived as a desperate recovery step.
1921 */
74637e6f 1922 rcu_unregister_thread();
c76201ab
DDAG
1923 exit(EXIT_FAILURE);
1924 }
1925
6ba996bb
DDAG
1926 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1927 MIGRATION_STATUS_COMPLETED);
1928 /*
1929 * If everything has worked fine, then the main thread has waited
1930 * for us to start, and we're the last use of the mis.
1931 * (If something broke then qemu will have to exit anyway since it's
1932 * got a bad migration state).
1933 */
1934 migration_incoming_state_destroy();
acb5ea86 1935 qemu_loadvm_state_cleanup();
6ba996bb 1936
74637e6f 1937 rcu_unregister_thread();
9cf4bb87 1938 mis->have_listen_thread = false;
2d49bacd
WY
1939 postcopy_state_set(POSTCOPY_INCOMING_END);
1940
ee647225
VSO
1941 object_unref(OBJECT(migr));
1942
c76201ab
DDAG
1943 return NULL;
1944}
1945
093e3c42
DDAG
1946/* After this message we must be able to immediately receive postcopy data */
1947static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)
1948{
1949 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_LISTENING);
1950 trace_loadvm_postcopy_handle_listen();
6864a7b5
DDAG
1951 Error *local_err = NULL;
1952
093e3c42
DDAG
1953 if (ps != POSTCOPY_INCOMING_ADVISE && ps != POSTCOPY_INCOMING_DISCARD) {
1954 error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps);
1955 return -1;
1956 }
f9527107
DDAG
1957 if (ps == POSTCOPY_INCOMING_ADVISE) {
1958 /*
1959 * A rare case, we entered listen without having to do any discards,
1960 * so do the setup that's normally done at the time of the 1st discard.
1961 */
58110f0a
VSO
1962 if (migrate_postcopy_ram()) {
1963 postcopy_ram_prepare_discard(mis);
1964 }
f9527107 1965 }
093e3c42 1966
f0a227ad
DDAG
1967 /*
1968 * Sensitise RAM - can now generate requests for blocks that don't exist
1969 * However, at this point the CPU shouldn't be running, and the IO
1970 * shouldn't be doing anything yet so don't actually expect requests
1971 */
58110f0a 1972 if (migrate_postcopy_ram()) {
2a7eb148 1973 if (postcopy_ram_incoming_setup(mis)) {
91b02dc7 1974 postcopy_ram_incoming_cleanup(mis);
58110f0a
VSO
1975 return -1;
1976 }
f0a227ad
DDAG
1977 }
1978
6864a7b5
DDAG
1979 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN, &local_err)) {
1980 error_report_err(local_err);
1981 return -1;
1982 }
1983
c76201ab
DDAG
1984 mis->have_listen_thread = true;
1985 /* Start up the listening thread and wait for it to signal ready */
1986 qemu_sem_init(&mis->listen_thread_sem, 0);
1987 qemu_thread_create(&mis->listen_thread, "postcopy/listen",
b411b844 1988 postcopy_ram_listen_thread, NULL,
a587a3fe 1989 QEMU_THREAD_DETACHED);
c76201ab
DDAG
1990 qemu_sem_wait(&mis->listen_thread_sem);
1991 qemu_sem_destroy(&mis->listen_thread_sem);
1992
093e3c42
DDAG
1993 return 0;
1994}
1995
ea6a55bc 1996static void loadvm_postcopy_handle_run_bh(void *opaque)
093e3c42 1997{
27c6825b 1998 Error *local_err = NULL;
1ce54262 1999 MigrationIncomingState *mis = opaque;
093e3c42 2000
27c6825b
DDAG
2001 /* TODO we should move all of this lot into postcopy_ram.c or a shared code
2002 * in migration.c
2003 */
2004 cpu_synchronize_all_post_init();
2005
7659505c 2006 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
27c6825b 2007
ace21a58
KW
2008 /* Make sure all file formats flush their mutable metadata.
2009 * If we get an error here, just don't restart the VM yet. */
27c6825b 2010 bdrv_invalidate_cache_all(&local_err);
0042fd36 2011 if (local_err) {
ace21a58 2012 error_report_err(local_err);
0042fd36
KW
2013 local_err = NULL;
2014 autostart = false;
2015 }
2016
27c6825b 2017 trace_loadvm_postcopy_handle_run_cpu_sync();
27c6825b
DDAG
2018
2019 trace_loadvm_postcopy_handle_run_vmstart();
2020
b35ebdf0
VSO
2021 dirty_bitmap_mig_before_vm_start();
2022
093e3c42
DDAG
2023 if (autostart) {
2024 /* Hold onto your hats, starting the CPU */
2025 vm_start();
2026 } else {
2027 /* leave it paused and let management decide when to start the CPU */
2028 runstate_set(RUN_STATE_PAUSED);
2029 }
2030
1ce54262 2031 qemu_bh_delete(mis->bh);
ea6a55bc
DL
2032}
2033
2034/* After all discards we can start running and asking for pages */
2035static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
2036{
0197d890 2037 PostcopyState ps = postcopy_state_get();
ea6a55bc
DL
2038
2039 trace_loadvm_postcopy_handle_run();
2040 if (ps != POSTCOPY_INCOMING_LISTENING) {
2041 error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps);
2042 return -1;
2043 }
2044
0197d890 2045 postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
1ce54262
WY
2046 mis->bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, mis);
2047 qemu_bh_schedule(mis->bh);
ea6a55bc 2048
27c6825b
DDAG
2049 /* We need to finish reading the stream from the package
2050 * and also stop reading anything more from the stream that loaded the
2051 * package (since it's now being read by the listener thread).
2052 * LOADVM_QUIT will quit all the layers of nested loadvm loops.
2053 */
2054 return LOADVM_QUIT;
093e3c42
DDAG
2055}
2056
0c26781c
PX
2057/* We must be with page_request_mutex held */
2058static gboolean postcopy_sync_page_req(gpointer key, gpointer value,
2059 gpointer data)
2060{
2061 MigrationIncomingState *mis = data;
2062 void *host_addr = (void *) key;
2063 ram_addr_t rb_offset;
2064 RAMBlock *rb;
2065 int ret;
2066
2067 rb = qemu_ram_block_from_host(host_addr, true, &rb_offset);
2068 if (!rb) {
2069 /*
2070 * This should _never_ happen. However be nice for a migrating VM to
2071 * not crash/assert. Post an error (note: intended to not use *_once
2072 * because we do want to see all the illegal addresses; and this can
2073 * never be triggered by the guest so we're safe) and move on next.
2074 */
2075 error_report("%s: illegal host addr %p", __func__, host_addr);
2076 /* Try the next entry */
2077 return FALSE;
2078 }
2079
2080 ret = migrate_send_rp_message_req_pages(mis, rb, rb_offset);
2081 if (ret) {
2082 /* Please refer to above comment. */
2083 error_report("%s: send rp message failed for addr %p",
2084 __func__, host_addr);
2085 return FALSE;
2086 }
2087
2088 trace_postcopy_page_req_sync(host_addr);
2089
2090 return FALSE;
2091}
2092
2093static void migrate_send_rp_req_pages_pending(MigrationIncomingState *mis)
2094{
2095 WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
2096 g_tree_foreach(mis->page_requested, postcopy_sync_page_req, mis);
2097 }
2098}
2099
3f5875ec
PX
2100static int loadvm_postcopy_handle_resume(MigrationIncomingState *mis)
2101{
2102 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
2103 error_report("%s: illegal resume received", __func__);
2104 /* Don't fail the load, only for this. */
2105 return 0;
2106 }
2107
cc5ab872
PX
2108 /*
2109 * Reset the last_rb before we resend any page req to source again, since
2110 * the source should have it reset already.
2111 */
2112 mis->last_rb = NULL;
2113
3f5875ec
PX
2114 /*
2115 * This means source VM is ready to resume the postcopy migration.
3f5875ec
PX
2116 */
2117 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2118 MIGRATION_STATUS_POSTCOPY_ACTIVE);
3f5875ec
PX
2119
2120 trace_loadvm_postcopy_handle_resume();
2121
13955b89
PX
2122 /* Tell source that "we are ready" */
2123 migrate_send_rp_resume_ack(mis, MIGRATION_RESUME_ACK_VALUE);
3f5875ec 2124
0c26781c
PX
2125 /*
2126 * After a postcopy recovery, the source should have lost the postcopy
2127 * queue, or potentially the requested pages could have been lost during
2128 * the network down phase. Let's re-sync with the source VM by re-sending
2129 * all the pending pages that we eagerly need, so these threads won't get
2130 * blocked too long due to the recovery.
2131 *
2132 * Without this procedure, the faulted destination VM threads (waiting for
2133 * page requests right before the postcopy is interrupted) can keep hanging
2134 * until the pages are sent by the source during the background copying of
2135 * pages, or another thread faulted on the same address accidentally.
2136 */
2137 migrate_send_rp_req_pages_pending(mis);
2138
5e773431
PX
2139 /*
2140 * It's time to switch state and release the fault thread to continue
2141 * service page faults. Note that this should be explicitly after the
2142 * above call to migrate_send_rp_req_pages_pending(). In short:
2143 * migrate_send_rp_message_req_pages() is not thread safe, yet.
2144 */
2145 qemu_sem_post(&mis->postcopy_pause_sem_fault);
2146
3f5875ec
PX
2147 return 0;
2148}
2149
c76ca188 2150/**
11cf1d98
DDAG
2151 * Immediately following this command is a blob of data containing an embedded
2152 * chunk of migration stream; read it and load it.
2153 *
2154 * @mis: Incoming state
2155 * @length: Length of packaged data to read
c76ca188 2156 *
11cf1d98
DDAG
2157 * Returns: Negative values on error
2158 *
2159 */
2160static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
2161{
2162 int ret;
61b67d47
DB
2163 size_t length;
2164 QIOChannelBuffer *bioc;
11cf1d98
DDAG
2165
2166 length = qemu_get_be32(mis->from_src_file);
2167 trace_loadvm_handle_cmd_packaged(length);
2168
2169 if (length > MAX_VM_CMD_PACKAGED_SIZE) {
61b67d47 2170 error_report("Unreasonably large packaged state: %zu", length);
11cf1d98
DDAG
2171 return -1;
2172 }
61b67d47
DB
2173
2174 bioc = qio_channel_buffer_new(length);
6f01f136 2175 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-loadvm-buffer");
61b67d47
DB
2176 ret = qemu_get_buffer(mis->from_src_file,
2177 bioc->data,
2178 length);
11cf1d98 2179 if (ret != length) {
61b67d47
DB
2180 object_unref(OBJECT(bioc));
2181 error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu",
9af9e0fe 2182 ret, length);
11cf1d98
DDAG
2183 return (ret < 0) ? ret : -EAGAIN;
2184 }
61b67d47 2185 bioc->usage += length;
11cf1d98
DDAG
2186 trace_loadvm_handle_cmd_packaged_received(ret);
2187
61b67d47 2188 QEMUFile *packf = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
11cf1d98
DDAG
2189
2190 ret = qemu_loadvm_state_main(packf, mis);
2191 trace_loadvm_handle_cmd_packaged_main(ret);
2192 qemu_fclose(packf);
61b67d47 2193 object_unref(OBJECT(bioc));
11cf1d98
DDAG
2194
2195 return ret;
2196}
2197
f25d4225
PX
2198/*
2199 * Handle request that source requests for recved_bitmap on
2200 * destination. Payload format:
2201 *
2202 * len (1 byte) + ramblock_name (<255 bytes)
2203 */
2204static int loadvm_handle_recv_bitmap(MigrationIncomingState *mis,
2205 uint16_t len)
2206{
2207 QEMUFile *file = mis->from_src_file;
2208 RAMBlock *rb;
2209 char block_name[256];
2210 size_t cnt;
2211
2212 cnt = qemu_get_counted_string(file, block_name);
2213 if (!cnt) {
2214 error_report("%s: failed to read block name", __func__);
2215 return -EINVAL;
2216 }
2217
2218 /* Validate before using the data */
2219 if (qemu_file_get_error(file)) {
2220 return qemu_file_get_error(file);
2221 }
2222
2223 if (len != cnt + 1) {
2224 error_report("%s: invalid payload length (%d)", __func__, len);
2225 return -EINVAL;
2226 }
2227
2228 rb = qemu_ram_block_by_name(block_name);
2229 if (!rb) {
2230 error_report("%s: block '%s' not found", __func__, block_name);
2231 return -EINVAL;
2232 }
2233
a335debb 2234 migrate_send_rp_recv_bitmap(mis, block_name);
f25d4225
PX
2235
2236 trace_loadvm_handle_recv_bitmap(block_name);
2237
2238 return 0;
2239}
2240
aad555c2
ZC
2241static int loadvm_process_enable_colo(MigrationIncomingState *mis)
2242{
18b1d3c9
DH
2243 int ret = migration_incoming_enable_colo();
2244
2245 if (!ret) {
2246 ret = colo_init_ram_cache();
2247 if (ret) {
2248 migration_incoming_disable_colo();
2249 }
2250 }
2251 return ret;
aad555c2
ZC
2252}
2253
11cf1d98
DDAG
2254/*
2255 * Process an incoming 'QEMU_VM_COMMAND'
2256 * 0 just a normal return
2257 * LOADVM_QUIT All good, but exit the loop
2258 * <0 Error
c76ca188
DDAG
2259 */
2260static int loadvm_process_command(QEMUFile *f)
2261{
2e37701e 2262 MigrationIncomingState *mis = migration_incoming_get_current();
c76ca188
DDAG
2263 uint16_t cmd;
2264 uint16_t len;
2e37701e 2265 uint32_t tmp32;
c76ca188
DDAG
2266
2267 cmd = qemu_get_be16(f);
2268 len = qemu_get_be16(f);
2269
7a9ddfbf
PX
2270 /* Check validity before continue processing of cmds */
2271 if (qemu_file_get_error(f)) {
2272 return qemu_file_get_error(f);
2273 }
2274
c76ca188
DDAG
2275 trace_loadvm_process_command(cmd, len);
2276 if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) {
2277 error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len);
2278 return -EINVAL;
2279 }
2280
2281 if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) {
2282 error_report("%s received with bad length - expecting %zu, got %d",
2283 mig_cmd_args[cmd].name,
2284 (size_t)mig_cmd_args[cmd].len, len);
2285 return -ERANGE;
2286 }
2287
2288 switch (cmd) {
2e37701e
DDAG
2289 case MIG_CMD_OPEN_RETURN_PATH:
2290 if (mis->to_src_file) {
2291 error_report("CMD_OPEN_RETURN_PATH called when RP already open");
2292 /* Not really a problem, so don't give up */
2293 return 0;
2294 }
2295 mis->to_src_file = qemu_file_get_return_path(f);
2296 if (!mis->to_src_file) {
2297 error_report("CMD_OPEN_RETURN_PATH failed");
2298 return -1;
2299 }
2300 break;
2301
2302 case MIG_CMD_PING:
2303 tmp32 = qemu_get_be32(f);
2304 trace_loadvm_process_command_ping(tmp32);
2305 if (!mis->to_src_file) {
2306 error_report("CMD_PING (0x%x) received with no return path",
2307 tmp32);
2308 return -1;
2309 }
6decec93 2310 migrate_send_rp_pong(mis, tmp32);
2e37701e 2311 break;
093e3c42 2312
11cf1d98
DDAG
2313 case MIG_CMD_PACKAGED:
2314 return loadvm_handle_cmd_packaged(mis);
2315
093e3c42 2316 case MIG_CMD_POSTCOPY_ADVISE:
875fcd01 2317 return loadvm_postcopy_handle_advise(mis, len);
093e3c42
DDAG
2318
2319 case MIG_CMD_POSTCOPY_LISTEN:
2320 return loadvm_postcopy_handle_listen(mis);
2321
2322 case MIG_CMD_POSTCOPY_RUN:
2323 return loadvm_postcopy_handle_run(mis);
2324
2325 case MIG_CMD_POSTCOPY_RAM_DISCARD:
2326 return loadvm_postcopy_ram_handle_discard(mis, len);
f25d4225 2327
3f5875ec
PX
2328 case MIG_CMD_POSTCOPY_RESUME:
2329 return loadvm_postcopy_handle_resume(mis);
2330
f25d4225
PX
2331 case MIG_CMD_RECV_BITMAP:
2332 return loadvm_handle_recv_bitmap(mis, len);
aad555c2
ZC
2333
2334 case MIG_CMD_ENABLE_COLO:
2335 return loadvm_process_enable_colo(mis);
c76ca188
DDAG
2336 }
2337
2338 return 0;
2339}
2340
59f39a47
DDAG
2341/*
2342 * Read a footer off the wire and check that it matches the expected section
2343 *
2344 * Returns: true if the footer was good
2345 * false if there is a problem (and calls error_report to say why)
2346 */
0f42f657 2347static bool check_section_footer(QEMUFile *f, SaveStateEntry *se)
59f39a47 2348{
7a9ddfbf 2349 int ret;
59f39a47
DDAG
2350 uint8_t read_mark;
2351 uint32_t read_section_id;
2352
15c38503 2353 if (!migrate_get_current()->send_section_footer) {
59f39a47
DDAG
2354 /* No footer to check */
2355 return true;
2356 }
2357
2358 read_mark = qemu_get_byte(f);
2359
7a9ddfbf
PX
2360 ret = qemu_file_get_error(f);
2361 if (ret) {
2362 error_report("%s: Read section footer failed: %d",
2363 __func__, ret);
2364 return false;
2365 }
2366
59f39a47 2367 if (read_mark != QEMU_VM_SECTION_FOOTER) {
0f42f657 2368 error_report("Missing section footer for %s", se->idstr);
59f39a47
DDAG
2369 return false;
2370 }
2371
2372 read_section_id = qemu_get_be32(f);
0f42f657 2373 if (read_section_id != se->load_section_id) {
59f39a47
DDAG
2374 error_report("Mismatched section id in footer for %s -"
2375 " read 0x%x expected 0x%x",
0f42f657 2376 se->idstr, read_section_id, se->load_section_id);
59f39a47
DDAG
2377 return false;
2378 }
2379
2380 /* All good */
2381 return true;
2382}
2383
fb3520a8
HZ
2384static int
2385qemu_loadvm_section_start_full(QEMUFile *f, MigrationIncomingState *mis)
2386{
2387 uint32_t instance_id, version_id, section_id;
2388 SaveStateEntry *se;
fb3520a8
HZ
2389 char idstr[256];
2390 int ret;
2391
2392 /* Read section start */
2393 section_id = qemu_get_be32(f);
2394 if (!qemu_get_counted_string(f, idstr)) {
2395 error_report("Unable to read ID string for section %u",
2396 section_id);
2397 return -EINVAL;
2398 }
2399 instance_id = qemu_get_be32(f);
2400 version_id = qemu_get_be32(f);
2401
7a9ddfbf
PX
2402 ret = qemu_file_get_error(f);
2403 if (ret) {
2404 error_report("%s: Failed to read instance/version ID: %d",
2405 __func__, ret);
2406 return ret;
2407 }
2408
fb3520a8
HZ
2409 trace_qemu_loadvm_state_section_startfull(section_id, idstr,
2410 instance_id, version_id);
2411 /* Find savevm section */
2412 se = find_se(idstr, instance_id);
2413 if (se == NULL) {
93062e23 2414 error_report("Unknown savevm section or instance '%s' %"PRIu32". "
827beacb
JRZ
2415 "Make sure that your current VM setup matches your "
2416 "saved VM setup, including any hotplugged devices",
fb3520a8
HZ
2417 idstr, instance_id);
2418 return -EINVAL;
2419 }
2420
2421 /* Validate version */
2422 if (version_id > se->version_id) {
2423 error_report("savevm: unsupported version %d for '%s' v%d",
2424 version_id, idstr, se->version_id);
2425 return -EINVAL;
2426 }
0f42f657
JQ
2427 se->load_version_id = version_id;
2428 se->load_section_id = section_id;
fb3520a8 2429
88c16567
WC
2430 /* Validate if it is a device's state */
2431 if (xen_enabled() && se->is_ram) {
2432 error_report("loadvm: %s RAM loading not allowed on Xen", idstr);
2433 return -EINVAL;
2434 }
2435
3a011c26 2436 ret = vmstate_load(f, se);
fb3520a8 2437 if (ret < 0) {
93062e23 2438 error_report("error while loading state for instance 0x%"PRIx32" of"
fb3520a8
HZ
2439 " device '%s'", instance_id, idstr);
2440 return ret;
2441 }
0f42f657 2442 if (!check_section_footer(f, se)) {
fb3520a8
HZ
2443 return -EINVAL;
2444 }
2445
2446 return 0;
2447}
2448
2449static int
2450qemu_loadvm_section_part_end(QEMUFile *f, MigrationIncomingState *mis)
2451{
2452 uint32_t section_id;
0f42f657 2453 SaveStateEntry *se;
fb3520a8
HZ
2454 int ret;
2455
2456 section_id = qemu_get_be32(f);
2457
7a9ddfbf
PX
2458 ret = qemu_file_get_error(f);
2459 if (ret) {
2460 error_report("%s: Failed to read section ID: %d",
2461 __func__, ret);
2462 return ret;
2463 }
2464
fb3520a8 2465 trace_qemu_loadvm_state_section_partend(section_id);
0f42f657
JQ
2466 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2467 if (se->load_section_id == section_id) {
fb3520a8
HZ
2468 break;
2469 }
2470 }
0f42f657 2471 if (se == NULL) {
fb3520a8
HZ
2472 error_report("Unknown savevm section %d", section_id);
2473 return -EINVAL;
2474 }
2475
3a011c26 2476 ret = vmstate_load(f, se);
fb3520a8
HZ
2477 if (ret < 0) {
2478 error_report("error while loading state section id %d(%s)",
0f42f657 2479 section_id, se->idstr);
fb3520a8
HZ
2480 return ret;
2481 }
0f42f657 2482 if (!check_section_footer(f, se)) {
fb3520a8
HZ
2483 return -EINVAL;
2484 }
2485
2486 return 0;
2487}
2488
16015d32
WY
2489static int qemu_loadvm_state_header(QEMUFile *f)
2490{
2491 unsigned int v;
2492 int ret;
2493
2494 v = qemu_get_be32(f);
2495 if (v != QEMU_VM_FILE_MAGIC) {
2496 error_report("Not a migration stream");
2497 return -EINVAL;
2498 }
2499
2500 v = qemu_get_be32(f);
2501 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2502 error_report("SaveVM v2 format is obsolete and don't work anymore");
2503 return -ENOTSUP;
2504 }
2505 if (v != QEMU_VM_FILE_VERSION) {
2506 error_report("Unsupported migration stream version");
2507 return -ENOTSUP;
2508 }
2509
2510 if (migrate_get_current()->send_configuration) {
2511 if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) {
2512 error_report("Configuration section missing");
2513 qemu_loadvm_state_cleanup();
2514 return -EINVAL;
2515 }
2516 ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0);
2517
2518 if (ret) {
2519 qemu_loadvm_state_cleanup();
2520 return ret;
2521 }
2522 }
2523 return 0;
2524}
2525
acb5ea86
JQ
2526static int qemu_loadvm_state_setup(QEMUFile *f)
2527{
2528 SaveStateEntry *se;
2529 int ret;
2530
2531 trace_loadvm_state_setup();
2532 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2533 if (!se->ops || !se->ops->load_setup) {
2534 continue;
2535 }
cea3b4c0 2536 if (se->ops->is_active) {
acb5ea86
JQ
2537 if (!se->ops->is_active(se->opaque)) {
2538 continue;
2539 }
2540 }
2541
2542 ret = se->ops->load_setup(f, se->opaque);
2543 if (ret < 0) {
2544 qemu_file_set_error(f, ret);
2545 error_report("Load state of device %s failed", se->idstr);
2546 return ret;
2547 }
2548 }
2549 return 0;
2550}
2551
2552void qemu_loadvm_state_cleanup(void)
2553{
2554 SaveStateEntry *se;
2555
2556 trace_loadvm_state_cleanup();
2557 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2558 if (se->ops && se->ops->load_cleanup) {
2559 se->ops->load_cleanup(se->opaque);
2560 }
2561 }
2562}
2563
b411b844
PX
2564/* Return true if we should continue the migration, or false. */
2565static bool postcopy_pause_incoming(MigrationIncomingState *mis)
2566{
2567 trace_postcopy_pause_incoming();
2568
ee647225
VSO
2569 assert(migrate_postcopy_ram());
2570
02affd41
PX
2571 /* Clear the triggered bit to allow one recovery */
2572 mis->postcopy_recover_triggered = false;
2573
39675fff
PX
2574 /*
2575 * Unregister yank with either from/to src would work, since ioc behind it
2576 * is the same
2577 */
2578 migration_ioc_unregister_yank_from_file(mis->from_src_file);
2579
b411b844
PX
2580 assert(mis->from_src_file);
2581 qemu_file_shutdown(mis->from_src_file);
2582 qemu_fclose(mis->from_src_file);
2583 mis->from_src_file = NULL;
2584
2585 assert(mis->to_src_file);
2586 qemu_file_shutdown(mis->to_src_file);
2587 qemu_mutex_lock(&mis->rp_mutex);
2588 qemu_fclose(mis->to_src_file);
2589 mis->to_src_file = NULL;
2590 qemu_mutex_unlock(&mis->rp_mutex);
2591
eed1cc78
PX
2592 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2593 MIGRATION_STATUS_POSTCOPY_PAUSED);
2594
3a7804c3
PX
2595 /* Notify the fault thread for the invalidated file handle */
2596 postcopy_fault_thread_notify(mis);
2597
b411b844
PX
2598 error_report("Detected IO failure for postcopy. "
2599 "Migration paused.");
2600
2601 while (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2602 qemu_sem_wait(&mis->postcopy_pause_sem_dst);
2603 }
2604
2605 trace_postcopy_pause_incoming_continued();
2606
2607 return true;
2608}
2609
3f6df99d 2610int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis)
1a8f46f8 2611{
a672b469 2612 uint8_t section_type;
ccb783c3 2613 int ret = 0;
61964c23 2614
b411b844 2615retry:
7a9ddfbf
PX
2616 while (true) {
2617 section_type = qemu_get_byte(f);
2618
2619 if (qemu_file_get_error(f)) {
2620 ret = qemu_file_get_error(f);
2621 break;
2622 }
2623
a5df2a02 2624 trace_qemu_loadvm_state_section(section_type);
a672b469
AL
2625 switch (section_type) {
2626 case QEMU_VM_SECTION_START:
2627 case QEMU_VM_SECTION_FULL:
fb3520a8 2628 ret = qemu_loadvm_section_start_full(f, mis);
b5a22e4a 2629 if (ret < 0) {
ccb783c3 2630 goto out;
b5a22e4a 2631 }
a672b469
AL
2632 break;
2633 case QEMU_VM_SECTION_PART:
2634 case QEMU_VM_SECTION_END:
fb3520a8 2635 ret = qemu_loadvm_section_part_end(f, mis);
b5a22e4a 2636 if (ret < 0) {
ccb783c3 2637 goto out;
b5a22e4a 2638 }
a672b469 2639 break;
c76ca188
DDAG
2640 case QEMU_VM_COMMAND:
2641 ret = loadvm_process_command(f);
7b89bf27 2642 trace_qemu_loadvm_state_section_command(ret);
4695ce3f 2643 if ((ret < 0) || (ret == LOADVM_QUIT)) {
ccb783c3 2644 goto out;
c76ca188
DDAG
2645 }
2646 break;
7a9ddfbf
PX
2647 case QEMU_VM_EOF:
2648 /* This is the end of migration */
2649 goto out;
a672b469 2650 default:
6a64b644 2651 error_report("Unknown savevm section type %d", section_type);
ccb783c3
DDAG
2652 ret = -EINVAL;
2653 goto out;
a672b469
AL
2654 }
2655 }
2656
ccb783c3
DDAG
2657out:
2658 if (ret < 0) {
2659 qemu_file_set_error(f, ret);
b411b844 2660
ee647225
VSO
2661 /* Cancel bitmaps incoming regardless of recovery */
2662 dirty_bitmap_mig_cancel_incoming();
2663
b411b844 2664 /*
fd037a65
PX
2665 * If we are during an active postcopy, then we pause instead
2666 * of bail out to at least keep the VM's dirty data. Note
2667 * that POSTCOPY_INCOMING_LISTENING stage is still not enough,
2668 * during which we're still receiving device states and we
2669 * still haven't yet started the VM on destination.
ee647225
VSO
2670 *
2671 * Only RAM postcopy supports recovery. Still, if RAM postcopy is
2672 * enabled, canceled bitmaps postcopy will not affect RAM postcopy
2673 * recovering.
b411b844
PX
2674 */
2675 if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING &&
ee647225 2676 migrate_postcopy_ram() && postcopy_pause_incoming(mis)) {
b411b844
PX
2677 /* Reset f to point to the newly created channel */
2678 f = mis->from_src_file;
2679 goto retry;
2680 }
ccb783c3
DDAG
2681 }
2682 return ret;
7b89bf27
DDAG
2683}
2684
2685int qemu_loadvm_state(QEMUFile *f)
2686{
2687 MigrationIncomingState *mis = migration_incoming_get_current();
2688 Error *local_err = NULL;
7b89bf27
DDAG
2689 int ret;
2690
2691 if (qemu_savevm_state_blocked(&local_err)) {
2692 error_report_err(local_err);
2693 return -EINVAL;
2694 }
2695
16015d32
WY
2696 ret = qemu_loadvm_state_header(f);
2697 if (ret) {
2698 return ret;
7b89bf27
DDAG
2699 }
2700
9e14b849
WY
2701 if (qemu_loadvm_state_setup(f) != 0) {
2702 return -EINVAL;
2703 }
2704
75e972da
DG
2705 cpu_synchronize_all_pre_loadvm();
2706
7b89bf27
DDAG
2707 ret = qemu_loadvm_state_main(f, mis);
2708 qemu_event_set(&mis->main_thread_load_event);
2709
2710 trace_qemu_loadvm_state_post_main(ret);
2711
c76201ab
DDAG
2712 if (mis->have_listen_thread) {
2713 /* Listen thread still going, can't clean up yet */
2714 return ret;
2715 }
2716
7b89bf27
DDAG
2717 if (ret == 0) {
2718 ret = qemu_file_get_error(f);
2719 }
1925cebc
AG
2720
2721 /*
2722 * Try to read in the VMDESC section as well, so that dumping tools that
2723 * intercept our migration stream have the chance to see it.
2724 */
1aca9a5f
DDAG
2725
2726 /* We've got to be careful; if we don't read the data and just shut the fd
2727 * then the sender can error if we close while it's still sending.
2728 * We also mustn't read data that isn't there; some transports (RDMA)
2729 * will stall waiting for that data when the source has already closed.
2730 */
7b89bf27 2731 if (ret == 0 && should_send_vmdesc()) {
1aca9a5f
DDAG
2732 uint8_t *buf;
2733 uint32_t size;
7b89bf27 2734 uint8_t section_type = qemu_get_byte(f);
1aca9a5f
DDAG
2735
2736 if (section_type != QEMU_VM_VMDESCRIPTION) {
2737 error_report("Expected vmdescription section, but got %d",
2738 section_type);
2739 /*
2740 * It doesn't seem worth failing at this point since
2741 * we apparently have an otherwise valid VM state
2742 */
2743 } else {
2744 buf = g_malloc(0x1000);
2745 size = qemu_get_be32(f);
2746
2747 while (size > 0) {
2748 uint32_t read_chunk = MIN(size, 0x1000);
2749 qemu_get_buffer(f, buf, read_chunk);
2750 size -= read_chunk;
2751 }
2752 g_free(buf);
1925cebc 2753 }
1925cebc
AG
2754 }
2755
acb5ea86 2756 qemu_loadvm_state_cleanup();
ea375f9a
JK
2757 cpu_synchronize_all_post_init();
2758
a672b469
AL
2759 return ret;
2760}
2761
3f6df99d
ZC
2762int qemu_load_device_state(QEMUFile *f)
2763{
2764 MigrationIncomingState *mis = migration_incoming_get_current();
2765 int ret;
2766
2767 /* Load QEMU_VM_SECTION_FULL section */
2768 ret = qemu_loadvm_state_main(f, mis);
2769 if (ret < 0) {
2770 error_report("Failed to load device state: %d", ret);
2771 return ret;
2772 }
2773
2774 cpu_synchronize_all_post_init();
2775 return 0;
2776}
2777
f1a9fcdd
DB
2778bool save_snapshot(const char *name, bool overwrite, const char *vmstate,
2779 bool has_devices, strList *devices, Error **errp)
a672b469 2780{
e26f98e2 2781 BlockDriverState *bs;
80ef0586 2782 QEMUSnapshotInfo sn1, *sn = &sn1;
66270a47 2783 int ret = -1, ret2;
a672b469
AL
2784 QEMUFile *f;
2785 int saved_vm_running;
c2c9a466 2786 uint64_t vm_state_size;
85cd1cc6 2787 g_autoptr(GDateTime) now = g_date_time_new_now_local();
79b3c12a 2788 AioContext *aio_context;
a672b469 2789
5aaac467 2790 if (migration_is_blocked(errp)) {
7ea14df2 2791 return false;
5aaac467
PB
2792 }
2793
377b21cc 2794 if (!replay_can_snapshot()) {
4dd32b3d
MA
2795 error_setg(errp, "Record/replay does not allow making snapshot "
2796 "right now. Try once more later.");
7ea14df2 2797 return false;
377b21cc
PD
2798 }
2799
f1a9fcdd 2800 if (!bdrv_all_can_snapshot(has_devices, devices, errp)) {
7ea14df2 2801 return false;
feeee5ac
MDCF
2802 }
2803
0b461605 2804 /* Delete old snapshots of the same name */
ac8c19ba 2805 if (name) {
f781f841 2806 if (overwrite) {
f1a9fcdd
DB
2807 if (bdrv_all_delete_snapshot(name, has_devices,
2808 devices, errp) < 0) {
f781f841
DB
2809 return false;
2810 }
2811 } else {
f1a9fcdd 2812 ret2 = bdrv_all_has_snapshot(name, has_devices, devices, errp);
f781f841
DB
2813 if (ret2 < 0) {
2814 return false;
2815 }
2816 if (ret2 == 1) {
2817 error_setg(errp,
2818 "Snapshot '%s' already exists in one or more devices",
2819 name);
2820 return false;
2821 }
ac8c19ba 2822 }
0b461605
DL
2823 }
2824
f1a9fcdd 2825 bs = bdrv_all_find_vmstate_bs(vmstate, has_devices, devices, errp);
7cb14481 2826 if (bs == NULL) {
7ea14df2 2827 return false;
a672b469 2828 }
79b3c12a 2829 aio_context = bdrv_get_aio_context(bs);
a672b469 2830
1354869c 2831 saved_vm_running = runstate_is_running();
560d027b
JQ
2832
2833 ret = global_state_store();
2834 if (ret) {
927d6638 2835 error_setg(errp, "Error saving global state");
7ea14df2 2836 return false;
560d027b 2837 }
0461d5a6 2838 vm_stop(RUN_STATE_SAVE_VM);
a672b469 2839
8649f2f9
SH
2840 bdrv_drain_all_begin();
2841
79b3c12a
DL
2842 aio_context_acquire(aio_context);
2843
cb499fb2 2844 memset(sn, 0, sizeof(*sn));
a672b469
AL
2845
2846 /* fill auxiliary fields */
85cd1cc6
DB
2847 sn->date_sec = g_date_time_to_unix(now);
2848 sn->date_nsec = g_date_time_get_microsecond(now) * 1000;
bc72ad67 2849 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
b39847a5
PD
2850 if (replay_mode != REPLAY_MODE_NONE) {
2851 sn->icount = replay_get_current_icount();
2852 } else {
2853 sn->icount = -1ULL;
2854 }
a672b469 2855
7d631a11 2856 if (name) {
80ef0586 2857 pstrcpy(sn->name, sizeof(sn->name), name);
7d631a11 2858 } else {
85cd1cc6
DB
2859 g_autofree char *autoname = g_date_time_format(now, "vm-%Y%m%d%H%M%S");
2860 pstrcpy(sn->name, sizeof(sn->name), autoname);
7d631a11
MDCF
2861 }
2862
a672b469 2863 /* save the VM state */
45566e9c 2864 f = qemu_fopen_bdrv(bs, 1);
a672b469 2865 if (!f) {
927d6638 2866 error_setg(errp, "Could not open VM state file");
a672b469
AL
2867 goto the_end;
2868 }
927d6638 2869 ret = qemu_savevm_state(f, errp);
2d22b18f 2870 vm_state_size = qemu_ftell(f);
66270a47 2871 ret2 = qemu_fclose(f);
a672b469 2872 if (ret < 0) {
a672b469
AL
2873 goto the_end;
2874 }
66270a47
DL
2875 if (ret2 < 0) {
2876 ret = ret2;
2877 goto the_end;
2878 }
a672b469 2879
17e2a4a4
SH
2880 /* The bdrv_all_create_snapshot() call that follows acquires the AioContext
2881 * for itself. BDRV_POLL_WHILE() does not support nested locking because
2882 * it only releases the lock once. Therefore synchronous I/O will deadlock
2883 * unless we release the AioContext before bdrv_all_create_snapshot().
2884 */
2885 aio_context_release(aio_context);
2886 aio_context = NULL;
2887
f1a9fcdd
DB
2888 ret = bdrv_all_create_snapshot(sn, bs, vm_state_size,
2889 has_devices, devices, errp);
a9085f9b 2890 if (ret < 0) {
f1a9fcdd 2891 bdrv_all_delete_snapshot(sn->name, has_devices, devices, NULL);
ac8c19ba 2892 goto the_end;
a672b469
AL
2893 }
2894
ac8c19ba
PD
2895 ret = 0;
2896
a672b469 2897 the_end:
17e2a4a4
SH
2898 if (aio_context) {
2899 aio_context_release(aio_context);
2900 }
8649f2f9
SH
2901
2902 bdrv_drain_all_end();
2903
38ff78d3 2904 if (saved_vm_running) {
a672b469 2905 vm_start();
38ff78d3 2906 }
7ea14df2 2907 return ret == 0;
ac8c19ba
PD
2908}
2909
5d6c599f
AP
2910void qmp_xen_save_devices_state(const char *filename, bool has_live, bool live,
2911 Error **errp)
a7ae8355
SS
2912{
2913 QEMUFile *f;
8925839f 2914 QIOChannelFile *ioc;
a7ae8355
SS
2915 int saved_vm_running;
2916 int ret;
2917
5d6c599f
AP
2918 if (!has_live) {
2919 /* live default to true so old version of Xen tool stack can have a
3a4452d8 2920 * successful live migration */
5d6c599f
AP
2921 live = true;
2922 }
2923
a7ae8355
SS
2924 saved_vm_running = runstate_is_running();
2925 vm_stop(RUN_STATE_SAVE_VM);
c69adea4 2926 global_state_store_running();
a7ae8355 2927
b4deb9bf
DM
2928 ioc = qio_channel_file_new_path(filename, O_WRONLY | O_CREAT | O_TRUNC,
2929 0660, errp);
8925839f 2930 if (!ioc) {
a7ae8355
SS
2931 goto the_end;
2932 }
6f01f136 2933 qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state");
8925839f 2934 f = qemu_fopen_channel_output(QIO_CHANNEL(ioc));
032b79f7 2935 object_unref(OBJECT(ioc));
a7ae8355 2936 ret = qemu_save_device_state(f);
96994fd1 2937 if (ret < 0 || qemu_fclose(f) < 0) {
c6bd8c70 2938 error_setg(errp, QERR_IO_ERROR);
5d6c599f
AP
2939 } else {
2940 /* libxl calls the QMP command "stop" before calling
2941 * "xen-save-devices-state" and in case of migration failure, libxl
2942 * would call "cont".
2943 * So call bdrv_inactivate_all (release locks) here to let the other
3a4452d8 2944 * side of the migration take control of the images.
5d6c599f
AP
2945 */
2946 if (live && !saved_vm_running) {
2947 ret = bdrv_inactivate_all();
2948 if (ret) {
2949 error_setg(errp, "%s: bdrv_inactivate_all() failed (%d)",
2950 __func__, ret);
2951 }
2952 }
a7ae8355
SS
2953 }
2954
2955 the_end:
38ff78d3 2956 if (saved_vm_running) {
a7ae8355 2957 vm_start();
38ff78d3 2958 }
a7ae8355
SS
2959}
2960
88c16567
WC
2961void qmp_xen_load_devices_state(const char *filename, Error **errp)
2962{
2963 QEMUFile *f;
2964 QIOChannelFile *ioc;
2965 int ret;
2966
2967 /* Guest must be paused before loading the device state; the RAM state
2968 * will already have been loaded by xc
2969 */
2970 if (runstate_is_running()) {
2971 error_setg(errp, "Cannot update device state while vm is running");
2972 return;
2973 }
2974 vm_stop(RUN_STATE_RESTORE_VM);
2975
2976 ioc = qio_channel_file_new_path(filename, O_RDONLY | O_BINARY, 0, errp);
2977 if (!ioc) {
2978 return;
2979 }
6f01f136 2980 qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state");
88c16567 2981 f = qemu_fopen_channel_input(QIO_CHANNEL(ioc));
032b79f7 2982 object_unref(OBJECT(ioc));
88c16567 2983
88c16567
WC
2984 ret = qemu_loadvm_state(f);
2985 qemu_fclose(f);
2986 if (ret < 0) {
2987 error_setg(errp, QERR_IO_ERROR);
2988 }
2989 migration_incoming_state_destroy();
2990}
2991
f1a9fcdd
DB
2992bool load_snapshot(const char *name, const char *vmstate,
2993 bool has_devices, strList *devices, Error **errp)
a672b469 2994{
e26f98e2 2995 BlockDriverState *bs_vm_state;
2d22b18f 2996 QEMUSnapshotInfo sn;
a672b469 2997 QEMUFile *f;
751c6a17 2998 int ret;
79b3c12a 2999 AioContext *aio_context;
b4b076da 3000 MigrationIncomingState *mis = migration_incoming_get_current();
a672b469 3001
f1a9fcdd 3002 if (!bdrv_all_can_snapshot(has_devices, devices, errp)) {
f61fe11a 3003 return false;
849f96e2 3004 }
f1a9fcdd 3005 ret = bdrv_all_has_snapshot(name, has_devices, devices, errp);
723ccda1 3006 if (ret < 0) {
f61fe11a 3007 return false;
723ccda1 3008 }
3d3e9b1f
DB
3009 if (ret == 0) {
3010 error_setg(errp, "Snapshot '%s' does not exist in one or more devices",
3011 name);
3012 return false;
3013 }
849f96e2 3014
f1a9fcdd 3015 bs_vm_state = bdrv_all_find_vmstate_bs(vmstate, has_devices, devices, errp);
f0aa7a8b 3016 if (!bs_vm_state) {
f61fe11a 3017 return false;
f0aa7a8b 3018 }
79b3c12a 3019 aio_context = bdrv_get_aio_context(bs_vm_state);
f0aa7a8b
MDCF
3020
3021 /* Don't even try to load empty VM states */
79b3c12a 3022 aio_context_acquire(aio_context);
f0aa7a8b 3023 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
79b3c12a 3024 aio_context_release(aio_context);
f0aa7a8b 3025 if (ret < 0) {
f61fe11a 3026 return false;
f0aa7a8b 3027 } else if (sn.vm_state_size == 0) {
927d6638
JQ
3028 error_setg(errp, "This is a disk-only snapshot. Revert to it "
3029 " offline using qemu-img");
f61fe11a 3030 return false;
f0aa7a8b
MDCF
3031 }
3032
f9a9fb65
PD
3033 /*
3034 * Flush the record/replay queue. Now the VM state is going
3035 * to change. Therefore we don't need to preserve its consistency
3036 */
3037 replay_flush_events();
3038
a672b469 3039 /* Flush all IO requests so they don't interfere with the new state. */
8649f2f9 3040 bdrv_drain_all_begin();
a672b469 3041
f1a9fcdd 3042 ret = bdrv_all_goto_snapshot(name, has_devices, devices, errp);
4c1cdbaa 3043 if (ret < 0) {
8649f2f9 3044 goto err_drain;
a672b469
AL
3045 }
3046
a672b469 3047 /* restore the VM state */
f0aa7a8b 3048 f = qemu_fopen_bdrv(bs_vm_state, 0);
a672b469 3049 if (!f) {
927d6638 3050 error_setg(errp, "Could not open VM state file");
8649f2f9 3051 goto err_drain;
a672b469 3052 }
f0aa7a8b 3053
aedbe192 3054 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
b4b076da 3055 mis->from_src_file = f;
f0aa7a8b 3056
b5eea99e
LS
3057 if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
3058 ret = -EINVAL;
3059 goto err_drain;
3060 }
79b3c12a
DL
3061 aio_context_acquire(aio_context);
3062 ret = qemu_loadvm_state(f);
1575829d 3063 migration_incoming_state_destroy();
79b3c12a
DL
3064 aio_context_release(aio_context);
3065
8649f2f9
SH
3066 bdrv_drain_all_end();
3067
a672b469 3068 if (ret < 0) {
927d6638 3069 error_setg(errp, "Error %d while loading VM state", ret);
f61fe11a 3070 return false;
a672b469 3071 }
f0aa7a8b 3072
f61fe11a 3073 return true;
8649f2f9
SH
3074
3075err_drain:
3076 bdrv_drain_all_end();
f61fe11a 3077 return false;
7b630349
JQ
3078}
3079
bef7e9e2
DB
3080bool delete_snapshot(const char *name, bool has_devices,
3081 strList *devices, Error **errp)
3082{
3083 if (!bdrv_all_can_snapshot(has_devices, devices, errp)) {
3084 return false;
3085 }
3086
3087 if (bdrv_all_delete_snapshot(name, has_devices, devices, errp) < 0) {
3088 return false;
3089 }
3090
3091 return true;
3092}
3093
c5705a77
AK
3094void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
3095{
fa53a0e5 3096 qemu_ram_set_idstr(mr->ram_block,
c5705a77 3097 memory_region_name(mr), dev);
b895de50 3098 qemu_ram_set_migratable(mr->ram_block);
c5705a77
AK
3099}
3100
3101void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
3102{
fa53a0e5 3103 qemu_ram_unset_idstr(mr->ram_block);
b895de50 3104 qemu_ram_unset_migratable(mr->ram_block);
c5705a77
AK
3105}
3106
3107void vmstate_register_ram_global(MemoryRegion *mr)
3108{
3109 vmstate_register_ram(mr, NULL);
3110}
1bfe5f05
JQ
3111
3112bool vmstate_check_only_migratable(const VMStateDescription *vmsd)
3113{
3114 /* check needed if --only-migratable is specified */
811f8652 3115 if (!only_migratable) {
1bfe5f05
JQ
3116 return true;
3117 }
3118
3119 return !(vmsd && vmsd->unmigratable);
3120}
0f0d83a4
DB
3121
3122typedef struct SnapshotJob {
3123 Job common;
3124 char *tag;
3125 char *vmstate;
3126 strList *devices;
3127 Coroutine *co;
3128 Error **errp;
3129 bool ret;
3130} SnapshotJob;
3131
3132static void qmp_snapshot_job_free(SnapshotJob *s)
3133{
3134 g_free(s->tag);
3135 g_free(s->vmstate);
3136 qapi_free_strList(s->devices);
3137}
3138
3139
3140static void snapshot_load_job_bh(void *opaque)
3141{
3142 Job *job = opaque;
3143 SnapshotJob *s = container_of(job, SnapshotJob, common);
3144 int orig_vm_running;
3145
3146 job_progress_set_remaining(&s->common, 1);
3147
3148 orig_vm_running = runstate_is_running();
3149 vm_stop(RUN_STATE_RESTORE_VM);
3150
3151 s->ret = load_snapshot(s->tag, s->vmstate, true, s->devices, s->errp);
3152 if (s->ret && orig_vm_running) {
3153 vm_start();
3154 }
3155
3156 job_progress_update(&s->common, 1);
3157
3158 qmp_snapshot_job_free(s);
3159 aio_co_wake(s->co);
3160}
3161
3162static void snapshot_save_job_bh(void *opaque)
3163{
3164 Job *job = opaque;
3165 SnapshotJob *s = container_of(job, SnapshotJob, common);
3166
3167 job_progress_set_remaining(&s->common, 1);
3168 s->ret = save_snapshot(s->tag, false, s->vmstate,
3169 true, s->devices, s->errp);
3170 job_progress_update(&s->common, 1);
3171
3172 qmp_snapshot_job_free(s);
3173 aio_co_wake(s->co);
3174}
3175
3176static void snapshot_delete_job_bh(void *opaque)
3177{
3178 Job *job = opaque;
3179 SnapshotJob *s = container_of(job, SnapshotJob, common);
3180
3181 job_progress_set_remaining(&s->common, 1);
3182 s->ret = delete_snapshot(s->tag, true, s->devices, s->errp);
3183 job_progress_update(&s->common, 1);
3184
3185 qmp_snapshot_job_free(s);
3186 aio_co_wake(s->co);
3187}
3188
3189static int coroutine_fn snapshot_save_job_run(Job *job, Error **errp)
3190{
3191 SnapshotJob *s = container_of(job, SnapshotJob, common);
3192 s->errp = errp;
3193 s->co = qemu_coroutine_self();
3194 aio_bh_schedule_oneshot(qemu_get_aio_context(),
3195 snapshot_save_job_bh, job);
3196 qemu_coroutine_yield();
3197 return s->ret ? 0 : -1;
3198}
3199
3200static int coroutine_fn snapshot_load_job_run(Job *job, Error **errp)
3201{
3202 SnapshotJob *s = container_of(job, SnapshotJob, common);
3203 s->errp = errp;
3204 s->co = qemu_coroutine_self();
3205 aio_bh_schedule_oneshot(qemu_get_aio_context(),
3206 snapshot_load_job_bh, job);
3207 qemu_coroutine_yield();
3208 return s->ret ? 0 : -1;
3209}
3210
3211static int coroutine_fn snapshot_delete_job_run(Job *job, Error **errp)
3212{
3213 SnapshotJob *s = container_of(job, SnapshotJob, common);
3214 s->errp = errp;
3215 s->co = qemu_coroutine_self();
3216 aio_bh_schedule_oneshot(qemu_get_aio_context(),
3217 snapshot_delete_job_bh, job);
3218 qemu_coroutine_yield();
3219 return s->ret ? 0 : -1;
3220}
3221
3222
3223static const JobDriver snapshot_load_job_driver = {
3224 .instance_size = sizeof(SnapshotJob),
3225 .job_type = JOB_TYPE_SNAPSHOT_LOAD,
3226 .run = snapshot_load_job_run,
3227};
3228
3229static const JobDriver snapshot_save_job_driver = {
3230 .instance_size = sizeof(SnapshotJob),
3231 .job_type = JOB_TYPE_SNAPSHOT_SAVE,
3232 .run = snapshot_save_job_run,
3233};
3234
3235static const JobDriver snapshot_delete_job_driver = {
3236 .instance_size = sizeof(SnapshotJob),
3237 .job_type = JOB_TYPE_SNAPSHOT_DELETE,
3238 .run = snapshot_delete_job_run,
3239};
3240
3241
3242void qmp_snapshot_save(const char *job_id,
3243 const char *tag,
3244 const char *vmstate,
3245 strList *devices,
3246 Error **errp)
3247{
3248 SnapshotJob *s;
3249
3250 s = job_create(job_id, &snapshot_save_job_driver, NULL,
3251 qemu_get_aio_context(), JOB_MANUAL_DISMISS,
3252 NULL, NULL, errp);
3253 if (!s) {
3254 return;
3255 }
3256
3257 s->tag = g_strdup(tag);
3258 s->vmstate = g_strdup(vmstate);
3259 s->devices = QAPI_CLONE(strList, devices);
3260
3261 job_start(&s->common);
3262}
3263
3264void qmp_snapshot_load(const char *job_id,
3265 const char *tag,
3266 const char *vmstate,
3267 strList *devices,
3268 Error **errp)
3269{
3270 SnapshotJob *s;
3271
3272 s = job_create(job_id, &snapshot_load_job_driver, NULL,
3273 qemu_get_aio_context(), JOB_MANUAL_DISMISS,
3274 NULL, NULL, errp);
3275 if (!s) {
3276 return;
3277 }
3278
3279 s->tag = g_strdup(tag);
3280 s->vmstate = g_strdup(vmstate);
3281 s->devices = QAPI_CLONE(strList, devices);
3282
3283 job_start(&s->common);
3284}
3285
3286void qmp_snapshot_delete(const char *job_id,
3287 const char *tag,
3288 strList *devices,
3289 Error **errp)
3290{
3291 SnapshotJob *s;
3292
3293 s = job_create(job_id, &snapshot_delete_job_driver, NULL,
3294 qemu_get_aio_context(), JOB_MANUAL_DISMISS,
3295 NULL, NULL, errp);
3296 if (!s) {
3297 return;
3298 }
3299
3300 s->tag = g_strdup(tag);
3301 s->devices = QAPI_CLONE(strList, devices);
3302
3303 job_start(&s->common);
3304}