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