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