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