]> Git Repo - qemu.git/blame - migration/savevm.c
Return path: Control commands
[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
d40cdb10 29#include "config-host.h"
511d2b14 30#include "qemu-common.h"
abfd9ce3 31#include "hw/boards.h"
511d2b14 32#include "hw/hw.h"
7685ee6a 33#include "hw/qdev.h"
1422e32d 34#include "net/net.h"
83c9089e 35#include "monitor/monitor.h"
9c17d615 36#include "sysemu/sysemu.h"
1de7afc9 37#include "qemu/timer.h"
511d2b14 38#include "audio/audio.h"
caf71f86 39#include "migration/migration.h"
cc7a8ea7 40#include "qapi/qmp/qerror.h"
d49b6836 41#include "qemu/error-report.h"
1de7afc9
PB
42#include "qemu/sockets.h"
43#include "qemu/queue.h"
9c17d615 44#include "sysemu/cpus.h"
022c62cb 45#include "exec/memory.h"
a7ae8355 46#include "qmp-commands.h"
517a13c9 47#include "trace.h"
28085f7b 48#include "qemu/iov.h"
de08c606 49#include "block/snapshot.h"
f364ec65 50#include "block/qapi.h"
511d2b14 51
a672b469 52
18995b98 53#ifndef ETH_P_RARP
f8778a77 54#define ETH_P_RARP 0x8035
18995b98
N
55#endif
56#define ARP_HTYPE_ETH 0x0001
57#define ARP_PTYPE_IP 0x0800
58#define ARP_OP_REQUEST_REV 0x3
59
37fb569c
DDAG
60static bool skip_section_footers;
61
c76ca188
DDAG
62static struct mig_cmd_args {
63 ssize_t len; /* -1 = variable */
64 const char *name;
65} mig_cmd_args[] = {
66 [MIG_CMD_INVALID] = { .len = -1, .name = "INVALID" },
2e37701e
DDAG
67 [MIG_CMD_OPEN_RETURN_PATH] = { .len = 0, .name = "OPEN_RETURN_PATH" },
68 [MIG_CMD_PING] = { .len = sizeof(uint32_t), .name = "PING" },
c76ca188
DDAG
69 [MIG_CMD_MAX] = { .len = -1, .name = "MAX" },
70};
71
18995b98 72static int announce_self_create(uint8_t *buf,
5cecf414 73 uint8_t *mac_addr)
a672b469 74{
18995b98
N
75 /* Ethernet header. */
76 memset(buf, 0xff, 6); /* destination MAC addr */
77 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
78 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
79
80 /* RARP header. */
81 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
82 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
83 *(buf + 18) = 6; /* hardware addr length (ethernet) */
84 *(buf + 19) = 4; /* protocol addr length (IPv4) */
85 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
86 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
87 memset(buf + 28, 0x00, 4); /* source protocol addr */
88 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
89 memset(buf + 38, 0x00, 4); /* target protocol addr */
90
91 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
92 memset(buf + 42, 0x00, 18);
93
94 return 60; /* len (FCS will be added by hardware) */
a672b469
AL
95}
96
f401ca22 97static void qemu_announce_self_iter(NICState *nic, void *opaque)
a672b469 98{
18995b98 99 uint8_t buf[60];
f401ca22
MM
100 int len;
101
9013dca5 102 trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic->conf->macaddr));
f401ca22
MM
103 len = announce_self_create(buf, nic->conf->macaddr.a);
104
b356f76d 105 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
f401ca22
MM
106}
107
108
109static void qemu_announce_self_once(void *opaque)
110{
ed8b330b
GN
111 static int count = SELF_ANNOUNCE_ROUNDS;
112 QEMUTimer *timer = *(QEMUTimer **)opaque;
a672b469 113
f401ca22
MM
114 qemu_foreach_nic(qemu_announce_self_iter, NULL);
115
18995b98
N
116 if (--count) {
117 /* delay 50ms, 150ms, 250ms, ... */
bc72ad67 118 timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) +
508e1180 119 self_announce_delay(count));
ed8b330b 120 } else {
5cecf414
EH
121 timer_del(timer);
122 timer_free(timer);
ed8b330b
GN
123 }
124}
125
126void qemu_announce_self(void)
127{
5cecf414
EH
128 static QEMUTimer *timer;
129 timer = timer_new_ms(QEMU_CLOCK_REALTIME, qemu_announce_self_once, &timer);
130 qemu_announce_self_once(&timer);
a672b469
AL
131}
132
133/***********************************************************/
134/* savevm/loadvm support */
135
05fcc848
KW
136static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
137 int64_t pos)
138{
139 int ret;
140 QEMUIOVector qiov;
141
142 qemu_iovec_init_external(&qiov, iov, iovcnt);
143 ret = bdrv_writev_vmstate(opaque, &qiov, pos);
144 if (ret < 0) {
145 return ret;
146 }
147
148 return qiov.size;
149}
150
a202a4c0
DDAG
151static ssize_t block_put_buffer(void *opaque, const uint8_t *buf,
152 int64_t pos, size_t size)
a672b469 153{
45566e9c 154 bdrv_save_vmstate(opaque, buf, pos, size);
a672b469
AL
155 return size;
156}
157
a202a4c0
DDAG
158static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
159 size_t size)
a672b469 160{
45566e9c 161 return bdrv_load_vmstate(opaque, buf, pos, size);
a672b469
AL
162}
163
164static int bdrv_fclose(void *opaque)
165{
ad492c92 166 return bdrv_flush(opaque);
a672b469
AL
167}
168
9229bf3c
PB
169static const QEMUFileOps bdrv_read_ops = {
170 .get_buffer = block_get_buffer,
171 .close = bdrv_fclose
172};
173
174static const QEMUFileOps bdrv_write_ops = {
05fcc848
KW
175 .put_buffer = block_put_buffer,
176 .writev_buffer = block_writev_buffer,
177 .close = bdrv_fclose
9229bf3c
PB
178};
179
45566e9c 180static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
a672b469 181{
38ff78d3 182 if (is_writable) {
9229bf3c 183 return qemu_fopen_ops(bs, &bdrv_write_ops);
38ff78d3 184 }
9229bf3c 185 return qemu_fopen_ops(bs, &bdrv_read_ops);
a672b469
AL
186}
187
2ff68d07 188
bb1a6d8c
EH
189/* QEMUFile timer support.
190 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
191 */
2ff68d07 192
40daca54 193void timer_put(QEMUFile *f, QEMUTimer *ts)
2ff68d07
PB
194{
195 uint64_t expire_time;
196
e93379b0 197 expire_time = timer_expire_time_ns(ts);
2ff68d07
PB
198 qemu_put_be64(f, expire_time);
199}
200
40daca54 201void timer_get(QEMUFile *f, QEMUTimer *ts)
2ff68d07
PB
202{
203 uint64_t expire_time;
204
205 expire_time = qemu_get_be64(f);
206 if (expire_time != -1) {
bc72ad67 207 timer_mod_ns(ts, expire_time);
2ff68d07 208 } else {
bc72ad67 209 timer_del(ts);
2ff68d07
PB
210 }
211}
212
213
bb1a6d8c
EH
214/* VMState timer support.
215 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
216 */
dde0463b
JQ
217
218static int get_timer(QEMUFile *f, void *pv, size_t size)
219{
220 QEMUTimer *v = pv;
40daca54 221 timer_get(f, v);
dde0463b
JQ
222 return 0;
223}
224
84e2e3eb 225static void put_timer(QEMUFile *f, void *pv, size_t size)
dde0463b 226{
84e2e3eb 227 QEMUTimer *v = pv;
40daca54 228 timer_put(f, v);
dde0463b
JQ
229}
230
231const VMStateInfo vmstate_info_timer = {
232 .name = "timer",
233 .get = get_timer,
234 .put = put_timer,
235};
236
08e99e29 237
7685ee6a
AW
238typedef struct CompatEntry {
239 char idstr[256];
240 int instance_id;
241} CompatEntry;
242
a672b469 243typedef struct SaveStateEntry {
72cf2d4f 244 QTAILQ_ENTRY(SaveStateEntry) entry;
a672b469
AL
245 char idstr[256];
246 int instance_id;
4d2ffa08 247 int alias_id;
a672b469
AL
248 int version_id;
249 int section_id;
22ea40f4 250 SaveVMHandlers *ops;
9ed7d6ae 251 const VMStateDescription *vmsd;
a672b469 252 void *opaque;
7685ee6a 253 CompatEntry *compat;
a7ae8355 254 int is_ram;
a672b469
AL
255} SaveStateEntry;
256
0163a2e0
JQ
257typedef struct SaveState {
258 QTAILQ_HEAD(, SaveStateEntry) handlers;
259 int global_section_id;
61964c23
JQ
260 bool skip_configuration;
261 uint32_t len;
262 const char *name;
0163a2e0
JQ
263} SaveState;
264
265static SaveState savevm_state = {
266 .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers),
267 .global_section_id = 0,
61964c23
JQ
268 .skip_configuration = false,
269};
270
271void savevm_skip_configuration(void)
272{
273 savevm_state.skip_configuration = true;
274}
275
276
277static void configuration_pre_save(void *opaque)
278{
279 SaveState *state = opaque;
280 const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
281
282 state->len = strlen(current_name);
283 state->name = current_name;
284}
285
286static int configuration_post_load(void *opaque, int version_id)
287{
288 SaveState *state = opaque;
289 const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
290
291 if (strncmp(state->name, current_name, state->len) != 0) {
292 error_report("Machine type received is '%s' and local is '%s'",
293 state->name, current_name);
294 return -EINVAL;
295 }
296 return 0;
297}
298
299static const VMStateDescription vmstate_configuration = {
300 .name = "configuration",
301 .version_id = 1,
302 .post_load = configuration_post_load,
303 .pre_save = configuration_pre_save,
304 .fields = (VMStateField[]) {
305 VMSTATE_UINT32(len, SaveState),
306 VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, 0, len),
307 VMSTATE_END_OF_LIST()
308 },
0163a2e0 309};
a672b469 310
abfd9ce3
AS
311static void dump_vmstate_vmsd(FILE *out_file,
312 const VMStateDescription *vmsd, int indent,
313 bool is_subsection);
314
315static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field,
316 int indent)
317{
318 fprintf(out_file, "%*s{\n", indent, "");
319 indent += 2;
320 fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name);
321 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
322 field->version_id);
323 fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "",
324 field->field_exists ? "true" : "false");
325 fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size);
326 if (field->vmsd != NULL) {
327 fprintf(out_file, ",\n");
328 dump_vmstate_vmsd(out_file, field->vmsd, indent, false);
329 }
330 fprintf(out_file, "\n%*s}", indent - 2, "");
331}
332
333static void dump_vmstate_vmss(FILE *out_file,
5cd8cada 334 const VMStateDescription **subsection,
abfd9ce3
AS
335 int indent)
336{
5cd8cada
JQ
337 if (*subsection != NULL) {
338 dump_vmstate_vmsd(out_file, *subsection, indent, true);
abfd9ce3
AS
339 }
340}
341
342static void dump_vmstate_vmsd(FILE *out_file,
343 const VMStateDescription *vmsd, int indent,
344 bool is_subsection)
345{
346 if (is_subsection) {
347 fprintf(out_file, "%*s{\n", indent, "");
348 } else {
349 fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description");
350 }
351 indent += 2;
352 fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name);
353 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
354 vmsd->version_id);
355 fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "",
356 vmsd->minimum_version_id);
357 if (vmsd->fields != NULL) {
358 const VMStateField *field = vmsd->fields;
359 bool first;
360
361 fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, "");
362 first = true;
363 while (field->name != NULL) {
364 if (field->flags & VMS_MUST_EXIST) {
365 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
366 field++;
367 continue;
368 }
369 if (!first) {
370 fprintf(out_file, ",\n");
371 }
372 dump_vmstate_vmsf(out_file, field, indent + 2);
373 field++;
374 first = false;
375 }
376 fprintf(out_file, "\n%*s]", indent, "");
377 }
378 if (vmsd->subsections != NULL) {
5cd8cada 379 const VMStateDescription **subsection = vmsd->subsections;
abfd9ce3
AS
380 bool first;
381
382 fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, "");
383 first = true;
5cd8cada 384 while (*subsection != NULL) {
abfd9ce3
AS
385 if (!first) {
386 fprintf(out_file, ",\n");
387 }
388 dump_vmstate_vmss(out_file, subsection, indent + 2);
389 subsection++;
390 first = false;
391 }
392 fprintf(out_file, "\n%*s]", indent, "");
393 }
394 fprintf(out_file, "\n%*s}", indent - 2, "");
395}
396
397static void dump_machine_type(FILE *out_file)
398{
399 MachineClass *mc;
400
401 mc = MACHINE_GET_CLASS(current_machine);
402
403 fprintf(out_file, " \"vmschkmachine\": {\n");
404 fprintf(out_file, " \"Name\": \"%s\"\n", mc->name);
405 fprintf(out_file, " },\n");
406}
407
408void dump_vmstate_json_to_file(FILE *out_file)
409{
410 GSList *list, *elt;
411 bool first;
412
413 fprintf(out_file, "{\n");
414 dump_machine_type(out_file);
415
416 first = true;
417 list = object_class_get_list(TYPE_DEVICE, true);
418 for (elt = list; elt; elt = elt->next) {
419 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
420 TYPE_DEVICE);
421 const char *name;
422 int indent = 2;
423
424 if (!dc->vmsd) {
425 continue;
426 }
427
428 if (!first) {
429 fprintf(out_file, ",\n");
430 }
431 name = object_class_get_name(OBJECT_CLASS(dc));
432 fprintf(out_file, "%*s\"%s\": {\n", indent, "", name);
433 indent += 2;
434 fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name);
435 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
436 dc->vmsd->version_id);
437 fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "",
438 dc->vmsd->minimum_version_id);
439
440 dump_vmstate_vmsd(out_file, dc->vmsd, indent, false);
441
442 fprintf(out_file, "\n%*s}", indent - 2, "");
443 first = false;
444 }
445 fprintf(out_file, "\n}\n");
446 fclose(out_file);
447}
448
8718e999
JQ
449static int calculate_new_instance_id(const char *idstr)
450{
451 SaveStateEntry *se;
452 int instance_id = 0;
453
0163a2e0 454 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
8718e999
JQ
455 if (strcmp(idstr, se->idstr) == 0
456 && instance_id <= se->instance_id) {
457 instance_id = se->instance_id + 1;
458 }
459 }
460 return instance_id;
461}
462
7685ee6a
AW
463static int calculate_compat_instance_id(const char *idstr)
464{
465 SaveStateEntry *se;
466 int instance_id = 0;
467
0163a2e0 468 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
38ff78d3 469 if (!se->compat) {
7685ee6a 470 continue;
38ff78d3 471 }
7685ee6a
AW
472
473 if (strcmp(idstr, se->compat->idstr) == 0
474 && instance_id <= se->compat->instance_id) {
475 instance_id = se->compat->instance_id + 1;
476 }
477 }
478 return instance_id;
479}
480
a672b469
AL
481/* TODO: Individual devices generally have very little idea about the rest
482 of the system, so instance_id should be removed/replaced.
483 Meanwhile pass -1 as instance_id if you do not already have a clearly
484 distinguishing id for all instances of your device class. */
0be71e32
AW
485int register_savevm_live(DeviceState *dev,
486 const char *idstr,
a672b469
AL
487 int instance_id,
488 int version_id,
7908c78d 489 SaveVMHandlers *ops,
a672b469
AL
490 void *opaque)
491{
8718e999 492 SaveStateEntry *se;
a672b469 493
97f3ad35 494 se = g_new0(SaveStateEntry, 1);
a672b469 495 se->version_id = version_id;
0163a2e0 496 se->section_id = savevm_state.global_section_id++;
7908c78d 497 se->ops = ops;
a672b469 498 se->opaque = opaque;
9ed7d6ae 499 se->vmsd = NULL;
a7ae8355 500 /* if this is a live_savem then set is_ram */
16310a3c 501 if (ops->save_live_setup != NULL) {
a7ae8355
SS
502 se->is_ram = 1;
503 }
a672b469 504
09e5ab63
AL
505 if (dev) {
506 char *id = qdev_get_dev_path(dev);
7685ee6a
AW
507 if (id) {
508 pstrcpy(se->idstr, sizeof(se->idstr), id);
509 pstrcat(se->idstr, sizeof(se->idstr), "/");
7267c094 510 g_free(id);
7685ee6a 511
97f3ad35 512 se->compat = g_new0(CompatEntry, 1);
7685ee6a
AW
513 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
514 se->compat->instance_id = instance_id == -1 ?
515 calculate_compat_instance_id(idstr) : instance_id;
516 instance_id = -1;
517 }
518 }
519 pstrcat(se->idstr, sizeof(se->idstr), idstr);
520
8718e999 521 if (instance_id == -1) {
7685ee6a 522 se->instance_id = calculate_new_instance_id(se->idstr);
8718e999
JQ
523 } else {
524 se->instance_id = instance_id;
a672b469 525 }
7685ee6a 526 assert(!se->compat || se->instance_id == 0);
8718e999 527 /* add at the end of list */
0163a2e0 528 QTAILQ_INSERT_TAIL(&savevm_state.handlers, se, entry);
a672b469
AL
529 return 0;
530}
531
0be71e32
AW
532int register_savevm(DeviceState *dev,
533 const char *idstr,
a672b469
AL
534 int instance_id,
535 int version_id,
536 SaveStateHandler *save_state,
537 LoadStateHandler *load_state,
538 void *opaque)
539{
97f3ad35 540 SaveVMHandlers *ops = g_new0(SaveVMHandlers, 1);
7908c78d
JQ
541 ops->save_state = save_state;
542 ops->load_state = load_state;
0be71e32 543 return register_savevm_live(dev, idstr, instance_id, version_id,
7908c78d 544 ops, opaque);
a672b469
AL
545}
546
0be71e32 547void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
41bd13af 548{
8718e999 549 SaveStateEntry *se, *new_se;
7685ee6a
AW
550 char id[256] = "";
551
09e5ab63
AL
552 if (dev) {
553 char *path = qdev_get_dev_path(dev);
7685ee6a
AW
554 if (path) {
555 pstrcpy(id, sizeof(id), path);
556 pstrcat(id, sizeof(id), "/");
7267c094 557 g_free(path);
7685ee6a
AW
558 }
559 }
560 pstrcat(id, sizeof(id), idstr);
41bd13af 561
0163a2e0 562 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
7685ee6a 563 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
0163a2e0 564 QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
ef1e1e07 565 g_free(se->compat);
22ea40f4 566 g_free(se->ops);
7267c094 567 g_free(se);
41bd13af 568 }
41bd13af
AL
569 }
570}
571
0be71e32 572int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
4d2ffa08
JK
573 const VMStateDescription *vmsd,
574 void *opaque, int alias_id,
575 int required_for_version)
9ed7d6ae 576{
8718e999 577 SaveStateEntry *se;
9ed7d6ae 578
4d2ffa08
JK
579 /* If this triggers, alias support can be dropped for the vmsd. */
580 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
581
97f3ad35 582 se = g_new0(SaveStateEntry, 1);
9ed7d6ae 583 se->version_id = vmsd->version_id;
0163a2e0 584 se->section_id = savevm_state.global_section_id++;
9ed7d6ae
JQ
585 se->opaque = opaque;
586 se->vmsd = vmsd;
4d2ffa08 587 se->alias_id = alias_id;
9ed7d6ae 588
09e5ab63
AL
589 if (dev) {
590 char *id = qdev_get_dev_path(dev);
7685ee6a
AW
591 if (id) {
592 pstrcpy(se->idstr, sizeof(se->idstr), id);
593 pstrcat(se->idstr, sizeof(se->idstr), "/");
7267c094 594 g_free(id);
7685ee6a 595
97f3ad35 596 se->compat = g_new0(CompatEntry, 1);
7685ee6a
AW
597 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
598 se->compat->instance_id = instance_id == -1 ?
599 calculate_compat_instance_id(vmsd->name) : instance_id;
600 instance_id = -1;
601 }
602 }
603 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
604
8718e999 605 if (instance_id == -1) {
7685ee6a 606 se->instance_id = calculate_new_instance_id(se->idstr);
8718e999
JQ
607 } else {
608 se->instance_id = instance_id;
9ed7d6ae 609 }
7685ee6a 610 assert(!se->compat || se->instance_id == 0);
8718e999 611 /* add at the end of list */
0163a2e0 612 QTAILQ_INSERT_TAIL(&savevm_state.handlers, se, entry);
9ed7d6ae
JQ
613 return 0;
614}
615
0be71e32
AW
616void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
617 void *opaque)
9ed7d6ae 618{
1eb7538b
JQ
619 SaveStateEntry *se, *new_se;
620
0163a2e0 621 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
1eb7538b 622 if (se->vmsd == vmsd && se->opaque == opaque) {
0163a2e0 623 QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
ef1e1e07 624 g_free(se->compat);
7267c094 625 g_free(se);
1eb7538b
JQ
626 }
627 }
9ed7d6ae
JQ
628}
629
4082be4d
JQ
630static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
631{
9013dca5 632 trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
9ed7d6ae 633 if (!se->vmsd) { /* Old style */
22ea40f4 634 return se->ops->load_state(f, se->opaque, version_id);
9ed7d6ae
JQ
635 }
636 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
4082be4d
JQ
637}
638
8118f095
AG
639static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
640{
641 int64_t old_offset, size;
642
643 old_offset = qemu_ftell_fast(f);
644 se->ops->save_state(f, se->opaque);
645 size = qemu_ftell_fast(f) - old_offset;
646
647 if (vmdesc) {
648 json_prop_int(vmdesc, "size", size);
649 json_start_array(vmdesc, "fields");
650 json_start_object(vmdesc, NULL);
651 json_prop_str(vmdesc, "name", "data");
652 json_prop_int(vmdesc, "size", size);
653 json_prop_str(vmdesc, "type", "buffer");
654 json_end_object(vmdesc);
655 json_end_array(vmdesc);
656 }
657}
658
659static void vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
4082be4d 660{
9013dca5 661 trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
8118f095
AG
662 if (!se->vmsd) {
663 vmstate_save_old_style(f, se, vmdesc);
dc912121 664 return;
9ed7d6ae 665 }
8118f095 666 vmstate_save_state(f, se->vmsd, se->opaque, vmdesc);
4082be4d
JQ
667}
668
37fb569c
DDAG
669void savevm_skip_section_footers(void)
670{
671 skip_section_footers = true;
672}
673
ce39bfc9
DDAG
674/*
675 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
676 */
677static void save_section_header(QEMUFile *f, SaveStateEntry *se,
678 uint8_t section_type)
679{
680 qemu_put_byte(f, section_type);
681 qemu_put_be32(f, se->section_id);
682
683 if (section_type == QEMU_VM_SECTION_FULL ||
684 section_type == QEMU_VM_SECTION_START) {
685 /* ID string */
686 size_t len = strlen(se->idstr);
687 qemu_put_byte(f, len);
688 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
689
690 qemu_put_be32(f, se->instance_id);
691 qemu_put_be32(f, se->version_id);
692 }
693}
694
f68945d4
DDAG
695/*
696 * Write a footer onto device sections that catches cases misformatted device
697 * sections.
698 */
699static void save_section_footer(QEMUFile *f, SaveStateEntry *se)
700{
701 if (!skip_section_footers) {
702 qemu_put_byte(f, QEMU_VM_SECTION_FOOTER);
703 qemu_put_be32(f, se->section_id);
704 }
705}
706
c76ca188
DDAG
707/**
708 * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the
709 * command and associated data.
710 *
711 * @f: File to send command on
712 * @command: Command type to send
713 * @len: Length of associated data
714 * @data: Data associated with command.
715 */
716void qemu_savevm_command_send(QEMUFile *f,
717 enum qemu_vm_cmd command,
718 uint16_t len,
719 uint8_t *data)
720{
721 trace_savevm_command_send(command, len);
722 qemu_put_byte(f, QEMU_VM_COMMAND);
723 qemu_put_be16(f, (uint16_t)command);
724 qemu_put_be16(f, len);
725 qemu_put_buffer(f, data, len);
726 qemu_fflush(f);
727}
728
2e37701e
DDAG
729void qemu_savevm_send_ping(QEMUFile *f, uint32_t value)
730{
731 uint32_t buf;
732
733 trace_savevm_send_ping(value);
734 buf = cpu_to_be32(value);
735 qemu_savevm_command_send(f, MIG_CMD_PING, sizeof(value), (uint8_t *)&buf);
736}
737
738void qemu_savevm_send_open_return_path(QEMUFile *f)
739{
740 trace_savevm_send_open_return_path();
741 qemu_savevm_command_send(f, MIG_CMD_OPEN_RETURN_PATH, 0, NULL);
742}
743
e1c37d0e 744bool qemu_savevm_state_blocked(Error **errp)
dc912121
AW
745{
746 SaveStateEntry *se;
747
0163a2e0 748 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
7d854c47 749 if (se->vmsd && se->vmsd->unmigratable) {
f231b88d
CR
750 error_setg(errp, "State blocked by non-migratable device '%s'",
751 se->idstr);
dc912121
AW
752 return true;
753 }
754 }
755 return false;
756}
757
f796baa1
DDAG
758void qemu_savevm_state_header(QEMUFile *f)
759{
760 trace_savevm_state_header();
761 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
762 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
172dfd4f
DDAG
763
764 if (!savevm_state.skip_configuration) {
765 qemu_put_byte(f, QEMU_VM_CONFIGURATION);
766 vmstate_save_state(f, &vmstate_configuration, &savevm_state, 0);
767 }
768
f796baa1
DDAG
769}
770
47c8c17a
PB
771void qemu_savevm_state_begin(QEMUFile *f,
772 const MigrationParams *params)
a672b469
AL
773{
774 SaveStateEntry *se;
39346385 775 int ret;
a672b469 776
9013dca5 777 trace_savevm_state_begin();
0163a2e0 778 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
22ea40f4 779 if (!se->ops || !se->ops->set_params) {
c163b5ca 780 continue;
6607ae23 781 }
22ea40f4 782 se->ops->set_params(params, se->opaque);
c163b5ca 783 }
38ff78d3 784
0163a2e0 785 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
d1315aac 786 if (!se->ops || !se->ops->save_live_setup) {
a672b469 787 continue;
22ea40f4 788 }
6bd68781
JQ
789 if (se->ops && se->ops->is_active) {
790 if (!se->ops->is_active(se->opaque)) {
791 continue;
792 }
793 }
ce39bfc9 794 save_section_header(f, se, QEMU_VM_SECTION_START);
a672b469 795
d1315aac 796 ret = se->ops->save_live_setup(f, se->opaque);
f68945d4 797 save_section_footer(f, se);
2975725f 798 if (ret < 0) {
47c8c17a
PB
799 qemu_file_set_error(f, ret);
800 break;
2975725f 801 }
a672b469 802 }
a672b469
AL
803}
804
39346385 805/*
07f35073 806 * this function has three return values:
39346385
JQ
807 * negative: there was one error, and we have -errno.
808 * 0 : We haven't finished, caller have to go again
809 * 1 : We have finished, we can go to complete phase
810 */
539de124 811int qemu_savevm_state_iterate(QEMUFile *f)
a672b469
AL
812{
813 SaveStateEntry *se;
814 int ret = 1;
815
9013dca5 816 trace_savevm_state_iterate();
0163a2e0 817 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
16310a3c 818 if (!se->ops || !se->ops->save_live_iterate) {
a672b469 819 continue;
22ea40f4 820 }
6bd68781
JQ
821 if (se->ops && se->ops->is_active) {
822 if (!se->ops->is_active(se->opaque)) {
823 continue;
824 }
825 }
aac844ed
JQ
826 if (qemu_file_rate_limit(f)) {
827 return 0;
828 }
464400f6 829 trace_savevm_section_start(se->idstr, se->section_id);
ce39bfc9
DDAG
830
831 save_section_header(f, se, QEMU_VM_SECTION_PART);
a672b469 832
16310a3c 833 ret = se->ops->save_live_iterate(f, se->opaque);
a5df2a02 834 trace_savevm_section_end(se->idstr, se->section_id, ret);
f68945d4 835 save_section_footer(f, se);
517a13c9 836
47c8c17a
PB
837 if (ret < 0) {
838 qemu_file_set_error(f, ret);
839 }
2975725f 840 if (ret <= 0) {
90697be8
JK
841 /* Do not proceed to the next vmstate before this one reported
842 completion of the current stage. This serializes the migration
843 and reduces the probability that a faster changing state is
844 synchronized over and over again. */
845 break;
846 }
a672b469 847 }
39346385 848 return ret;
a672b469
AL
849}
850
9850c604
AG
851static bool should_send_vmdesc(void)
852{
853 MachineState *machine = MACHINE(qdev_get_machine());
854 return !machine->suppress_vmdesc;
855}
856
a3e06c3d 857void qemu_savevm_state_complete_precopy(QEMUFile *f)
a672b469 858{
8118f095
AG
859 QJSON *vmdesc;
860 int vmdesc_len;
a672b469 861 SaveStateEntry *se;
2975725f 862 int ret;
a672b469 863
a3e06c3d 864 trace_savevm_state_complete_precopy();
9013dca5 865
ea375f9a
JK
866 cpu_synchronize_all_states();
867
0163a2e0 868 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
a3e06c3d 869 if (!se->ops || !se->ops->save_live_complete_precopy) {
a672b469 870 continue;
22ea40f4 871 }
6bd68781
JQ
872 if (se->ops && se->ops->is_active) {
873 if (!se->ops->is_active(se->opaque)) {
874 continue;
875 }
876 }
464400f6 877 trace_savevm_section_start(se->idstr, se->section_id);
ce39bfc9
DDAG
878
879 save_section_header(f, se, QEMU_VM_SECTION_END);
a672b469 880
a3e06c3d 881 ret = se->ops->save_live_complete_precopy(f, se->opaque);
a5df2a02 882 trace_savevm_section_end(se->idstr, se->section_id, ret);
f68945d4 883 save_section_footer(f, se);
2975725f 884 if (ret < 0) {
47c8c17a
PB
885 qemu_file_set_error(f, ret);
886 return;
2975725f 887 }
a672b469
AL
888 }
889
8118f095
AG
890 vmdesc = qjson_new();
891 json_prop_int(vmdesc, "page_size", TARGET_PAGE_SIZE);
892 json_start_array(vmdesc, "devices");
0163a2e0 893 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
a672b469 894
22ea40f4 895 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
5cecf414 896 continue;
22ea40f4 897 }
df896152
JQ
898 if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
899 trace_savevm_section_skip(se->idstr, se->section_id);
900 continue;
901 }
902
464400f6 903 trace_savevm_section_start(se->idstr, se->section_id);
8118f095
AG
904
905 json_start_object(vmdesc, NULL);
906 json_prop_str(vmdesc, "name", se->idstr);
907 json_prop_int(vmdesc, "instance_id", se->instance_id);
908
ce39bfc9 909 save_section_header(f, se, QEMU_VM_SECTION_FULL);
a672b469 910
8118f095
AG
911 vmstate_save(f, se, vmdesc);
912
913 json_end_object(vmdesc);
a5df2a02 914 trace_savevm_section_end(se->idstr, se->section_id, 0);
f68945d4 915 save_section_footer(f, se);
a672b469
AL
916 }
917
918 qemu_put_byte(f, QEMU_VM_EOF);
8118f095
AG
919
920 json_end_array(vmdesc);
921 qjson_finish(vmdesc);
922 vmdesc_len = strlen(qjson_get_str(vmdesc));
923
9850c604
AG
924 if (should_send_vmdesc()) {
925 qemu_put_byte(f, QEMU_VM_VMDESCRIPTION);
926 qemu_put_be32(f, vmdesc_len);
927 qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len);
928 }
8118f095
AG
929 object_unref(OBJECT(vmdesc));
930
edaae611 931 qemu_fflush(f);
a672b469
AL
932}
933
e4ed1541
JQ
934uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
935{
936 SaveStateEntry *se;
937 uint64_t ret = 0;
938
0163a2e0 939 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
e4ed1541
JQ
940 if (!se->ops || !se->ops->save_live_pending) {
941 continue;
942 }
943 if (se->ops && se->ops->is_active) {
944 if (!se->ops->is_active(se->opaque)) {
945 continue;
946 }
947 }
948 ret += se->ops->save_live_pending(f, se->opaque, max_size);
949 }
950 return ret;
951}
952
ea7415fa 953void qemu_savevm_state_cleanup(void)
4ec7fcc7
JK
954{
955 SaveStateEntry *se;
956
ea7415fa 957 trace_savevm_state_cleanup();
0163a2e0 958 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
d1a8548c
LL
959 if (se->ops && se->ops->cleanup) {
960 se->ops->cleanup(se->opaque);
4ec7fcc7
JK
961 }
962 }
963}
964
5d80448c 965static int qemu_savevm_state(QEMUFile *f, Error **errp)
a672b469 966{
a672b469 967 int ret;
6607ae23
IY
968 MigrationParams params = {
969 .blk = 0,
970 .shared = 0
971 };
aefeb18b
DDAG
972 MigrationState *ms = migrate_init(&params);
973 ms->file = f;
a672b469 974
5d80448c 975 if (qemu_savevm_state_blocked(errp)) {
04943eba 976 return -EINVAL;
dc912121
AW
977 }
978
9b095037 979 qemu_mutex_unlock_iothread();
f796baa1 980 qemu_savevm_state_header(f);
47c8c17a 981 qemu_savevm_state_begin(f, &params);
9b095037
PB
982 qemu_mutex_lock_iothread();
983
47c8c17a
PB
984 while (qemu_file_get_error(f) == 0) {
985 if (qemu_savevm_state_iterate(f) > 0) {
986 break;
987 }
988 }
a672b469 989
47c8c17a 990 ret = qemu_file_get_error(f);
39346385 991 if (ret == 0) {
a3e06c3d 992 qemu_savevm_state_complete_precopy(f);
624b9cc2 993 ret = qemu_file_get_error(f);
39346385 994 }
04943eba 995 if (ret != 0) {
ea7415fa 996 qemu_savevm_state_cleanup();
5d80448c 997 error_setg_errno(errp, -ret, "Error while writing VM state");
04943eba 998 }
a672b469
AL
999 return ret;
1000}
1001
a7ae8355
SS
1002static int qemu_save_device_state(QEMUFile *f)
1003{
1004 SaveStateEntry *se;
1005
1006 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1007 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1008
1009 cpu_synchronize_all_states();
1010
0163a2e0 1011 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
a7ae8355
SS
1012 if (se->is_ram) {
1013 continue;
1014 }
22ea40f4 1015 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
a7ae8355
SS
1016 continue;
1017 }
df896152
JQ
1018 if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1019 continue;
1020 }
a7ae8355 1021
ce39bfc9 1022 save_section_header(f, se, QEMU_VM_SECTION_FULL);
a7ae8355 1023
8118f095 1024 vmstate_save(f, se, NULL);
f68945d4
DDAG
1025
1026 save_section_footer(f, se);
a7ae8355
SS
1027 }
1028
1029 qemu_put_byte(f, QEMU_VM_EOF);
1030
1031 return qemu_file_get_error(f);
1032}
1033
a672b469
AL
1034static SaveStateEntry *find_se(const char *idstr, int instance_id)
1035{
1036 SaveStateEntry *se;
1037
0163a2e0 1038 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
a672b469 1039 if (!strcmp(se->idstr, idstr) &&
4d2ffa08
JK
1040 (instance_id == se->instance_id ||
1041 instance_id == se->alias_id))
a672b469 1042 return se;
7685ee6a
AW
1043 /* Migrating from an older version? */
1044 if (strstr(se->idstr, idstr) && se->compat) {
1045 if (!strcmp(se->compat->idstr, idstr) &&
1046 (instance_id == se->compat->instance_id ||
1047 instance_id == se->alias_id))
1048 return se;
1049 }
a672b469
AL
1050 }
1051 return NULL;
1052}
1053
c76ca188
DDAG
1054/**
1055 * loadvm_process_command: Process an incoming 'QEMU_VM_COMMAND'
1056 *
1057 * Returns: 0 on success, negative on error (in which case it will issue an
1058 * error message).
1059 * @f: The stream to read the command data from.
1060 */
1061static int loadvm_process_command(QEMUFile *f)
1062{
2e37701e 1063 MigrationIncomingState *mis = migration_incoming_get_current();
c76ca188
DDAG
1064 uint16_t cmd;
1065 uint16_t len;
2e37701e 1066 uint32_t tmp32;
c76ca188
DDAG
1067
1068 cmd = qemu_get_be16(f);
1069 len = qemu_get_be16(f);
1070
1071 trace_loadvm_process_command(cmd, len);
1072 if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) {
1073 error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len);
1074 return -EINVAL;
1075 }
1076
1077 if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) {
1078 error_report("%s received with bad length - expecting %zu, got %d",
1079 mig_cmd_args[cmd].name,
1080 (size_t)mig_cmd_args[cmd].len, len);
1081 return -ERANGE;
1082 }
1083
1084 switch (cmd) {
2e37701e
DDAG
1085 case MIG_CMD_OPEN_RETURN_PATH:
1086 if (mis->to_src_file) {
1087 error_report("CMD_OPEN_RETURN_PATH called when RP already open");
1088 /* Not really a problem, so don't give up */
1089 return 0;
1090 }
1091 mis->to_src_file = qemu_file_get_return_path(f);
1092 if (!mis->to_src_file) {
1093 error_report("CMD_OPEN_RETURN_PATH failed");
1094 return -1;
1095 }
1096 break;
1097
1098 case MIG_CMD_PING:
1099 tmp32 = qemu_get_be32(f);
1100 trace_loadvm_process_command_ping(tmp32);
1101 if (!mis->to_src_file) {
1102 error_report("CMD_PING (0x%x) received with no return path",
1103 tmp32);
1104 return -1;
1105 }
1106 /* migrate_send_rp_pong(mis, tmp32); TODO: gets added later */
1107 break;
c76ca188
DDAG
1108 }
1109
1110 return 0;
1111}
1112
1a8f46f8 1113struct LoadStateEntry {
72cf2d4f 1114 QLIST_ENTRY(LoadStateEntry) entry;
a672b469
AL
1115 SaveStateEntry *se;
1116 int section_id;
1117 int version_id;
1a8f46f8 1118};
a672b469 1119
59f39a47
DDAG
1120/*
1121 * Read a footer off the wire and check that it matches the expected section
1122 *
1123 * Returns: true if the footer was good
1124 * false if there is a problem (and calls error_report to say why)
1125 */
1126static bool check_section_footer(QEMUFile *f, LoadStateEntry *le)
1127{
1128 uint8_t read_mark;
1129 uint32_t read_section_id;
1130
1131 if (skip_section_footers) {
1132 /* No footer to check */
1133 return true;
1134 }
1135
1136 read_mark = qemu_get_byte(f);
1137
1138 if (read_mark != QEMU_VM_SECTION_FOOTER) {
1139 error_report("Missing section footer for %s", le->se->idstr);
1140 return false;
1141 }
1142
1143 read_section_id = qemu_get_be32(f);
1144 if (read_section_id != le->section_id) {
1145 error_report("Mismatched section id in footer for %s -"
1146 " read 0x%x expected 0x%x",
1147 le->se->idstr, read_section_id, le->section_id);
1148 return false;
1149 }
1150
1151 /* All good */
1152 return true;
1153}
1154
1a8f46f8 1155void loadvm_free_handlers(MigrationIncomingState *mis)
a672b469 1156{
f4dbb8dd 1157 LoadStateEntry *le, *new_le;
1a8f46f8
DDAG
1158
1159 QLIST_FOREACH_SAFE(le, &mis->loadvm_handlers, entry, new_le) {
1160 QLIST_REMOVE(le, entry);
1161 g_free(le);
1162 }
1163}
1164
1165int qemu_loadvm_state(QEMUFile *f)
1166{
1167 MigrationIncomingState *mis = migration_incoming_get_current();
0457d073 1168 Error *local_err = NULL;
a672b469
AL
1169 uint8_t section_type;
1170 unsigned int v;
1171 int ret;
1925cebc 1172 int file_error_after_eof = -1;
a672b469 1173
0457d073 1174 if (qemu_savevm_state_blocked(&local_err)) {
19867549 1175 error_report_err(local_err);
dc912121
AW
1176 return -EINVAL;
1177 }
1178
a672b469 1179 v = qemu_get_be32(f);
38ff78d3 1180 if (v != QEMU_VM_FILE_MAGIC) {
0457d073 1181 error_report("Not a migration stream");
a672b469 1182 return -EINVAL;
38ff78d3 1183 }
a672b469
AL
1184
1185 v = qemu_get_be32(f);
bbfe1408 1186 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
6a64b644 1187 error_report("SaveVM v2 format is obsolete and don't work anymore");
bbfe1408
JQ
1188 return -ENOTSUP;
1189 }
38ff78d3 1190 if (v != QEMU_VM_FILE_VERSION) {
0457d073 1191 error_report("Unsupported migration stream version");
a672b469 1192 return -ENOTSUP;
38ff78d3 1193 }
a672b469 1194
61964c23
JQ
1195 if (!savevm_state.skip_configuration) {
1196 if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) {
1197 error_report("Configuration section missing");
1198 return -EINVAL;
1199 }
1200 ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0);
1201
1202 if (ret) {
1203 return ret;
1204 }
1205 }
1206
a672b469
AL
1207 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1208 uint32_t instance_id, version_id, section_id;
a672b469 1209 SaveStateEntry *se;
1a8f46f8 1210 LoadStateEntry *le;
b3af1bc9 1211 char idstr[256];
a672b469 1212
a5df2a02 1213 trace_qemu_loadvm_state_section(section_type);
a672b469
AL
1214 switch (section_type) {
1215 case QEMU_VM_SECTION_START:
1216 case QEMU_VM_SECTION_FULL:
1217 /* Read section start */
1218 section_id = qemu_get_be32(f);
b3af1bc9
DDAG
1219 if (!qemu_get_counted_string(f, idstr)) {
1220 error_report("Unable to read ID string for section %u",
1221 section_id);
1222 return -EINVAL;
1223 }
a672b469
AL
1224 instance_id = qemu_get_be32(f);
1225 version_id = qemu_get_be32(f);
1226
a5df2a02
DDAG
1227 trace_qemu_loadvm_state_section_startfull(section_id, idstr,
1228 instance_id, version_id);
a672b469
AL
1229 /* Find savevm section */
1230 se = find_se(idstr, instance_id);
1231 if (se == NULL) {
6a64b644
DDAG
1232 error_report("Unknown savevm section or instance '%s' %d",
1233 idstr, instance_id);
a672b469
AL
1234 ret = -EINVAL;
1235 goto out;
1236 }
1237
1238 /* Validate version */
1239 if (version_id > se->version_id) {
6a64b644
DDAG
1240 error_report("savevm: unsupported version %d for '%s' v%d",
1241 version_id, idstr, se->version_id);
a672b469
AL
1242 ret = -EINVAL;
1243 goto out;
1244 }
1245
1246 /* Add entry */
7267c094 1247 le = g_malloc0(sizeof(*le));
a672b469
AL
1248
1249 le->se = se;
1250 le->section_id = section_id;
1251 le->version_id = version_id;
1a8f46f8 1252 QLIST_INSERT_HEAD(&mis->loadvm_handlers, le, entry);
a672b469 1253
4082be4d 1254 ret = vmstate_load(f, le->se, le->version_id);
b5a22e4a 1255 if (ret < 0) {
6a64b644
DDAG
1256 error_report("error while loading state for instance 0x%x of"
1257 " device '%s'", instance_id, idstr);
b5a22e4a
JQ
1258 goto out;
1259 }
59f39a47 1260 if (!check_section_footer(f, le)) {
f68945d4
DDAG
1261 ret = -EINVAL;
1262 goto out;
1263 }
a672b469
AL
1264 break;
1265 case QEMU_VM_SECTION_PART:
1266 case QEMU_VM_SECTION_END:
1267 section_id = qemu_get_be32(f);
1268
a5df2a02 1269 trace_qemu_loadvm_state_section_partend(section_id);
1a8f46f8 1270 QLIST_FOREACH(le, &mis->loadvm_handlers, entry) {
f4dbb8dd
JQ
1271 if (le->section_id == section_id) {
1272 break;
1273 }
1274 }
a672b469 1275 if (le == NULL) {
6a64b644 1276 error_report("Unknown savevm section %d", section_id);
a672b469
AL
1277 ret = -EINVAL;
1278 goto out;
1279 }
1280
4082be4d 1281 ret = vmstate_load(f, le->se, le->version_id);
b5a22e4a 1282 if (ret < 0) {
6a64b644
DDAG
1283 error_report("error while loading state section id %d(%s)",
1284 section_id, le->se->idstr);
b5a22e4a
JQ
1285 goto out;
1286 }
59f39a47 1287 if (!check_section_footer(f, le)) {
f68945d4
DDAG
1288 ret = -EINVAL;
1289 goto out;
1290 }
a672b469 1291 break;
c76ca188
DDAG
1292 case QEMU_VM_COMMAND:
1293 ret = loadvm_process_command(f);
1294 if (ret < 0) {
1295 goto out;
1296 }
1297 break;
a672b469 1298 default:
6a64b644 1299 error_report("Unknown savevm section type %d", section_type);
a672b469
AL
1300 ret = -EINVAL;
1301 goto out;
1302 }
1303 }
1304
1925cebc
AG
1305 file_error_after_eof = qemu_file_get_error(f);
1306
1307 /*
1308 * Try to read in the VMDESC section as well, so that dumping tools that
1309 * intercept our migration stream have the chance to see it.
1310 */
1aca9a5f
DDAG
1311
1312 /* We've got to be careful; if we don't read the data and just shut the fd
1313 * then the sender can error if we close while it's still sending.
1314 * We also mustn't read data that isn't there; some transports (RDMA)
1315 * will stall waiting for that data when the source has already closed.
1316 */
1317 if (should_send_vmdesc()) {
1318 uint8_t *buf;
1319 uint32_t size;
1320 section_type = qemu_get_byte(f);
1321
1322 if (section_type != QEMU_VM_VMDESCRIPTION) {
1323 error_report("Expected vmdescription section, but got %d",
1324 section_type);
1325 /*
1326 * It doesn't seem worth failing at this point since
1327 * we apparently have an otherwise valid VM state
1328 */
1329 } else {
1330 buf = g_malloc(0x1000);
1331 size = qemu_get_be32(f);
1332
1333 while (size > 0) {
1334 uint32_t read_chunk = MIN(size, 0x1000);
1335 qemu_get_buffer(f, buf, read_chunk);
1336 size -= read_chunk;
1337 }
1338 g_free(buf);
1925cebc 1339 }
1925cebc
AG
1340 }
1341
ea375f9a
JK
1342 cpu_synchronize_all_post_init();
1343
a672b469
AL
1344 ret = 0;
1345
1346out:
42802d47 1347 if (ret == 0) {
1925cebc
AG
1348 /* We may not have a VMDESC section, so ignore relative errors */
1349 ret = file_error_after_eof;
624b9cc2 1350 }
a672b469
AL
1351
1352 return ret;
1353}
1354
29d78271
SH
1355static BlockDriverState *find_vmstate_bs(void)
1356{
1357 BlockDriverState *bs = NULL;
1358 while ((bs = bdrv_next(bs))) {
1359 if (bdrv_can_snapshot(bs)) {
1360 return bs;
1361 }
1362 }
1363 return NULL;
1364}
1365
cb499fb2
KW
1366/*
1367 * Deletes snapshots of a given name in all opened images.
1368 */
1369static int del_existing_snapshots(Monitor *mon, const char *name)
1370{
1371 BlockDriverState *bs;
cb499fb2 1372 QEMUSnapshotInfo sn1, *snapshot = &sn1;
a89d89d3 1373 Error *err = NULL;
cb499fb2 1374
dbc13590
MA
1375 bs = NULL;
1376 while ((bs = bdrv_next(bs))) {
cb499fb2 1377 if (bdrv_can_snapshot(bs) &&
38ff78d3 1378 bdrv_snapshot_find(bs, snapshot, name) >= 0) {
a89d89d3 1379 bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
84d18f06 1380 if (err) {
cb499fb2 1381 monitor_printf(mon,
a89d89d3
WX
1382 "Error while deleting snapshot on device '%s':"
1383 " %s\n",
1384 bdrv_get_device_name(bs),
1385 error_get_pretty(err));
1386 error_free(err);
cb499fb2
KW
1387 return -1;
1388 }
1389 }
1390 }
1391
1392 return 0;
1393}
1394
3e5a50d6 1395void hmp_savevm(Monitor *mon, const QDict *qdict)
a672b469
AL
1396{
1397 BlockDriverState *bs, *bs1;
1398 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
cb499fb2 1399 int ret;
a672b469
AL
1400 QEMUFile *f;
1401 int saved_vm_running;
c2c9a466 1402 uint64_t vm_state_size;
68b891ec 1403 qemu_timeval tv;
7d631a11 1404 struct tm tm;
d54908a5 1405 const char *name = qdict_get_try_str(qdict, "name");
5d80448c 1406 Error *local_err = NULL;
a672b469 1407
feeee5ac 1408 /* Verify if there is a device that doesn't support snapshots and is writable */
dbc13590
MA
1409 bs = NULL;
1410 while ((bs = bdrv_next(bs))) {
feeee5ac 1411
07b70bfb 1412 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
feeee5ac
MDCF
1413 continue;
1414 }
1415
1416 if (!bdrv_can_snapshot(bs)) {
1417 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
1418 bdrv_get_device_name(bs));
1419 return;
1420 }
1421 }
1422
29d78271 1423 bs = find_vmstate_bs();
a672b469 1424 if (!bs) {
376253ec 1425 monitor_printf(mon, "No block device can accept snapshots\n");
a672b469
AL
1426 return;
1427 }
a672b469 1428
1354869c 1429 saved_vm_running = runstate_is_running();
560d027b
JQ
1430
1431 ret = global_state_store();
1432 if (ret) {
1433 monitor_printf(mon, "Error saving global state\n");
1434 return;
1435 }
0461d5a6 1436 vm_stop(RUN_STATE_SAVE_VM);
a672b469 1437
cb499fb2 1438 memset(sn, 0, sizeof(*sn));
a672b469
AL
1439
1440 /* fill auxiliary fields */
68b891ec 1441 qemu_gettimeofday(&tv);
a672b469
AL
1442 sn->date_sec = tv.tv_sec;
1443 sn->date_nsec = tv.tv_usec * 1000;
bc72ad67 1444 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
a672b469 1445
7d631a11
MDCF
1446 if (name) {
1447 ret = bdrv_snapshot_find(bs, old_sn, name);
1448 if (ret >= 0) {
1449 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1450 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1451 } else {
1452 pstrcpy(sn->name, sizeof(sn->name), name);
1453 }
1454 } else {
d7d9b528
BS
1455 /* cast below needed for OpenBSD where tv_sec is still 'long' */
1456 localtime_r((const time_t *)&tv.tv_sec, &tm);
7d631a11 1457 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
7d631a11
MDCF
1458 }
1459
cb499fb2 1460 /* Delete old snapshots of the same name */
f139a412 1461 if (name && del_existing_snapshots(mon, name) < 0) {
cb499fb2
KW
1462 goto the_end;
1463 }
1464
a672b469 1465 /* save the VM state */
45566e9c 1466 f = qemu_fopen_bdrv(bs, 1);
a672b469 1467 if (!f) {
376253ec 1468 monitor_printf(mon, "Could not open VM state file\n");
a672b469
AL
1469 goto the_end;
1470 }
5d80448c 1471 ret = qemu_savevm_state(f, &local_err);
2d22b18f 1472 vm_state_size = qemu_ftell(f);
a672b469
AL
1473 qemu_fclose(f);
1474 if (ret < 0) {
5d80448c
KW
1475 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1476 error_free(local_err);
a672b469
AL
1477 goto the_end;
1478 }
1479
1480 /* create the snapshots */
1481
dbc13590
MA
1482 bs1 = NULL;
1483 while ((bs1 = bdrv_next(bs1))) {
feeee5ac 1484 if (bdrv_can_snapshot(bs1)) {
2d22b18f
AL
1485 /* Write VM state size only to the image that contains the state */
1486 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
a672b469
AL
1487 ret = bdrv_snapshot_create(bs1, sn);
1488 if (ret < 0) {
376253ec
AL
1489 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1490 bdrv_get_device_name(bs1));
a672b469
AL
1491 }
1492 }
1493 }
1494
1495 the_end:
38ff78d3 1496 if (saved_vm_running) {
a672b469 1497 vm_start();
38ff78d3 1498 }
a672b469
AL
1499}
1500
a7ae8355
SS
1501void qmp_xen_save_devices_state(const char *filename, Error **errp)
1502{
1503 QEMUFile *f;
1504 int saved_vm_running;
1505 int ret;
1506
1507 saved_vm_running = runstate_is_running();
1508 vm_stop(RUN_STATE_SAVE_VM);
c69adea4 1509 global_state_store_running();
a7ae8355
SS
1510
1511 f = qemu_fopen(filename, "wb");
1512 if (!f) {
1befce96 1513 error_setg_file_open(errp, errno, filename);
a7ae8355
SS
1514 goto the_end;
1515 }
1516 ret = qemu_save_device_state(f);
1517 qemu_fclose(f);
1518 if (ret < 0) {
c6bd8c70 1519 error_setg(errp, QERR_IO_ERROR);
a7ae8355
SS
1520 }
1521
1522 the_end:
38ff78d3 1523 if (saved_vm_running) {
a7ae8355 1524 vm_start();
38ff78d3 1525 }
a7ae8355
SS
1526}
1527
03cd4655 1528int load_vmstate(const char *name)
a672b469 1529{
f0aa7a8b 1530 BlockDriverState *bs, *bs_vm_state;
2d22b18f 1531 QEMUSnapshotInfo sn;
a672b469 1532 QEMUFile *f;
751c6a17 1533 int ret;
a672b469 1534
29d78271 1535 bs_vm_state = find_vmstate_bs();
f0aa7a8b
MDCF
1536 if (!bs_vm_state) {
1537 error_report("No block device supports snapshots");
1538 return -ENOTSUP;
1539 }
1540
1541 /* Don't even try to load empty VM states */
1542 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
1543 if (ret < 0) {
1544 return ret;
1545 } else if (sn.vm_state_size == 0) {
e11480db
KW
1546 error_report("This is a disk-only snapshot. Revert to it offline "
1547 "using qemu-img.");
f0aa7a8b
MDCF
1548 return -EINVAL;
1549 }
1550
1551 /* Verify if there is any device that doesn't support snapshots and is
1552 writable and check if the requested snapshot is available too. */
dbc13590
MA
1553 bs = NULL;
1554 while ((bs = bdrv_next(bs))) {
feeee5ac 1555
07b70bfb 1556 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
feeee5ac
MDCF
1557 continue;
1558 }
1559
1560 if (!bdrv_can_snapshot(bs)) {
1561 error_report("Device '%s' is writable but does not support snapshots.",
1562 bdrv_get_device_name(bs));
1563 return -ENOTSUP;
1564 }
feeee5ac 1565
f0aa7a8b
MDCF
1566 ret = bdrv_snapshot_find(bs, &sn, name);
1567 if (ret < 0) {
1568 error_report("Device '%s' does not have the requested snapshot '%s'",
1569 bdrv_get_device_name(bs), name);
1570 return ret;
1571 }
a672b469
AL
1572 }
1573
1574 /* Flush all IO requests so they don't interfere with the new state. */
922453bc 1575 bdrv_drain_all();
a672b469 1576
f0aa7a8b
MDCF
1577 bs = NULL;
1578 while ((bs = bdrv_next(bs))) {
1579 if (bdrv_can_snapshot(bs)) {
1580 ret = bdrv_snapshot_goto(bs, name);
a672b469 1581 if (ret < 0) {
f0aa7a8b
MDCF
1582 error_report("Error %d while activating snapshot '%s' on '%s'",
1583 ret, name, bdrv_get_device_name(bs));
1584 return ret;
a672b469
AL
1585 }
1586 }
1587 }
1588
a672b469 1589 /* restore the VM state */
f0aa7a8b 1590 f = qemu_fopen_bdrv(bs_vm_state, 0);
a672b469 1591 if (!f) {
1ecda02b 1592 error_report("Could not open VM state file");
05f2401e 1593 return -EINVAL;
a672b469 1594 }
f0aa7a8b 1595
5a8a49d7 1596 qemu_system_reset(VMRESET_SILENT);
bca7856a 1597 migration_incoming_state_new(f);
a672b469 1598 ret = qemu_loadvm_state(f);
f0aa7a8b 1599
a672b469 1600 qemu_fclose(f);
bca7856a 1601 migration_incoming_state_destroy();
a672b469 1602 if (ret < 0) {
1ecda02b 1603 error_report("Error %d while loading VM state", ret);
05f2401e 1604 return ret;
a672b469 1605 }
f0aa7a8b 1606
05f2401e 1607 return 0;
7b630349
JQ
1608}
1609
3e5a50d6 1610void hmp_delvm(Monitor *mon, const QDict *qdict)
a672b469 1611{
af957387 1612 BlockDriverState *bs;
ba2b2288 1613 Error *err;
d54908a5 1614 const char *name = qdict_get_str(qdict, "name");
a672b469 1615
af957387 1616 if (!find_vmstate_bs()) {
376253ec 1617 monitor_printf(mon, "No block device supports snapshots\n");
a672b469
AL
1618 return;
1619 }
1620
af957387
ZH
1621 bs = NULL;
1622 while ((bs = bdrv_next(bs))) {
1623 if (bdrv_can_snapshot(bs)) {
ba2b2288 1624 err = NULL;
a89d89d3 1625 bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
84d18f06 1626 if (err) {
a89d89d3
WX
1627 monitor_printf(mon,
1628 "Error while deleting snapshot on device '%s':"
1629 " %s\n",
1630 bdrv_get_device_name(bs),
1631 error_get_pretty(err));
1632 error_free(err);
a672b469
AL
1633 }
1634 }
1635 }
1636}
1637
1ce6be24 1638void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
a672b469
AL
1639{
1640 BlockDriverState *bs, *bs1;
f9209915
MDCF
1641 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
1642 int nb_sns, i, ret, available;
1643 int total;
1644 int *available_snapshots;
a672b469 1645
29d78271 1646 bs = find_vmstate_bs();
a672b469 1647 if (!bs) {
376253ec 1648 monitor_printf(mon, "No available block device supports snapshots\n");
a672b469
AL
1649 return;
1650 }
a672b469
AL
1651
1652 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1653 if (nb_sns < 0) {
376253ec 1654 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
a672b469
AL
1655 return;
1656 }
f9209915
MDCF
1657
1658 if (nb_sns == 0) {
1659 monitor_printf(mon, "There is no snapshot available.\n");
1660 return;
1661 }
1662
97f3ad35 1663 available_snapshots = g_new0(int, nb_sns);
f9209915
MDCF
1664 total = 0;
1665 for (i = 0; i < nb_sns; i++) {
a672b469 1666 sn = &sn_tab[i];
f9209915
MDCF
1667 available = 1;
1668 bs1 = NULL;
1669
1670 while ((bs1 = bdrv_next(bs1))) {
1671 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
1672 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
1673 if (ret < 0) {
1674 available = 0;
1675 break;
1676 }
1677 }
1678 }
1679
1680 if (available) {
1681 available_snapshots[total] = i;
1682 total++;
1683 }
a672b469 1684 }
f9209915
MDCF
1685
1686 if (total > 0) {
5b917044
WX
1687 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1688 monitor_printf(mon, "\n");
f9209915
MDCF
1689 for (i = 0; i < total; i++) {
1690 sn = &sn_tab[available_snapshots[i]];
5b917044
WX
1691 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
1692 monitor_printf(mon, "\n");
f9209915
MDCF
1693 }
1694 } else {
1695 monitor_printf(mon, "There is no suitable snapshot available\n");
1696 }
1697
7267c094
AL
1698 g_free(sn_tab);
1699 g_free(available_snapshots);
f9209915 1700
a672b469 1701}
c5705a77
AK
1702
1703void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
1704{
1ddde087 1705 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
c5705a77
AK
1706 memory_region_name(mr), dev);
1707}
1708
1709void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
1710{
b0e56e0b 1711 qemu_ram_unset_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK);
c5705a77
AK
1712}
1713
1714void vmstate_register_ram_global(MemoryRegion *mr)
1715{
1716 vmstate_register_ram(mr, NULL);
1717}
This page took 0.952095 seconds and 4 git commands to generate.