Add wrappers and handlers for sending/receiving the postcopy-ram migration messages.
[qemu/ar7.git] / migration / savevm.c
blob1ce022a9be0c78ad67e2a944ee21da770f90af86
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009-2015 Red Hat Inc
7 * Authors:
8 * Juan Quintela <quintela@redhat.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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.
29 #include "config-host.h"
30 #include "qemu-common.h"
31 #include "hw/boards.h"
32 #include "hw/hw.h"
33 #include "hw/qdev.h"
34 #include "net/net.h"
35 #include "monitor/monitor.h"
36 #include "sysemu/sysemu.h"
37 #include "qemu/timer.h"
38 #include "audio/audio.h"
39 #include "migration/migration.h"
40 #include "qapi/qmp/qerror.h"
41 #include "qemu/error-report.h"
42 #include "qemu/sockets.h"
43 #include "qemu/queue.h"
44 #include "sysemu/cpus.h"
45 #include "exec/memory.h"
46 #include "qmp-commands.h"
47 #include "trace.h"
48 #include "qemu/bitops.h"
49 #include "qemu/iov.h"
50 #include "block/snapshot.h"
51 #include "block/qapi.h"
54 #ifndef ETH_P_RARP
55 #define ETH_P_RARP 0x8035
56 #endif
57 #define ARP_HTYPE_ETH 0x0001
58 #define ARP_PTYPE_IP 0x0800
59 #define ARP_OP_REQUEST_REV 0x3
61 const unsigned int postcopy_ram_discard_version = 0;
63 static bool skip_section_footers;
65 static struct mig_cmd_args {
66 ssize_t len; /* -1 = variable */
67 const char *name;
68 } mig_cmd_args[] = {
69 [MIG_CMD_INVALID] = { .len = -1, .name = "INVALID" },
70 [MIG_CMD_OPEN_RETURN_PATH] = { .len = 0, .name = "OPEN_RETURN_PATH" },
71 [MIG_CMD_PING] = { .len = sizeof(uint32_t), .name = "PING" },
72 [MIG_CMD_POSTCOPY_ADVISE] = { .len = 16, .name = "POSTCOPY_ADVISE" },
73 [MIG_CMD_POSTCOPY_LISTEN] = { .len = 0, .name = "POSTCOPY_LISTEN" },
74 [MIG_CMD_POSTCOPY_RUN] = { .len = 0, .name = "POSTCOPY_RUN" },
75 [MIG_CMD_POSTCOPY_RAM_DISCARD] = {
76 .len = -1, .name = "POSTCOPY_RAM_DISCARD" },
77 [MIG_CMD_MAX] = { .len = -1, .name = "MAX" },
80 static int announce_self_create(uint8_t *buf,
81 uint8_t *mac_addr)
83 /* Ethernet header. */
84 memset(buf, 0xff, 6); /* destination MAC addr */
85 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
86 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
88 /* RARP header. */
89 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
90 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
91 *(buf + 18) = 6; /* hardware addr length (ethernet) */
92 *(buf + 19) = 4; /* protocol addr length (IPv4) */
93 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
94 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
95 memset(buf + 28, 0x00, 4); /* source protocol addr */
96 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
97 memset(buf + 38, 0x00, 4); /* target protocol addr */
99 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
100 memset(buf + 42, 0x00, 18);
102 return 60; /* len (FCS will be added by hardware) */
105 static void qemu_announce_self_iter(NICState *nic, void *opaque)
107 uint8_t buf[60];
108 int len;
110 trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic->conf->macaddr));
111 len = announce_self_create(buf, nic->conf->macaddr.a);
113 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
117 static void qemu_announce_self_once(void *opaque)
119 static int count = SELF_ANNOUNCE_ROUNDS;
120 QEMUTimer *timer = *(QEMUTimer **)opaque;
122 qemu_foreach_nic(qemu_announce_self_iter, NULL);
124 if (--count) {
125 /* delay 50ms, 150ms, 250ms, ... */
126 timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) +
127 self_announce_delay(count));
128 } else {
129 timer_del(timer);
130 timer_free(timer);
134 void qemu_announce_self(void)
136 static QEMUTimer *timer;
137 timer = timer_new_ms(QEMU_CLOCK_REALTIME, qemu_announce_self_once, &timer);
138 qemu_announce_self_once(&timer);
141 /***********************************************************/
142 /* savevm/loadvm support */
144 static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
145 int64_t pos)
147 int ret;
148 QEMUIOVector qiov;
150 qemu_iovec_init_external(&qiov, iov, iovcnt);
151 ret = bdrv_writev_vmstate(opaque, &qiov, pos);
152 if (ret < 0) {
153 return ret;
156 return qiov.size;
159 static ssize_t block_put_buffer(void *opaque, const uint8_t *buf,
160 int64_t pos, size_t size)
162 bdrv_save_vmstate(opaque, buf, pos, size);
163 return size;
166 static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
167 size_t size)
169 return bdrv_load_vmstate(opaque, buf, pos, size);
172 static int bdrv_fclose(void *opaque)
174 return bdrv_flush(opaque);
177 static const QEMUFileOps bdrv_read_ops = {
178 .get_buffer = block_get_buffer,
179 .close = bdrv_fclose
182 static const QEMUFileOps bdrv_write_ops = {
183 .put_buffer = block_put_buffer,
184 .writev_buffer = block_writev_buffer,
185 .close = bdrv_fclose
188 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
190 if (is_writable) {
191 return qemu_fopen_ops(bs, &bdrv_write_ops);
193 return qemu_fopen_ops(bs, &bdrv_read_ops);
197 /* QEMUFile timer support.
198 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
201 void timer_put(QEMUFile *f, QEMUTimer *ts)
203 uint64_t expire_time;
205 expire_time = timer_expire_time_ns(ts);
206 qemu_put_be64(f, expire_time);
209 void timer_get(QEMUFile *f, QEMUTimer *ts)
211 uint64_t expire_time;
213 expire_time = qemu_get_be64(f);
214 if (expire_time != -1) {
215 timer_mod_ns(ts, expire_time);
216 } else {
217 timer_del(ts);
222 /* VMState timer support.
223 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
226 static int get_timer(QEMUFile *f, void *pv, size_t size)
228 QEMUTimer *v = pv;
229 timer_get(f, v);
230 return 0;
233 static void put_timer(QEMUFile *f, void *pv, size_t size)
235 QEMUTimer *v = pv;
236 timer_put(f, v);
239 const VMStateInfo vmstate_info_timer = {
240 .name = "timer",
241 .get = get_timer,
242 .put = put_timer,
246 typedef struct CompatEntry {
247 char idstr[256];
248 int instance_id;
249 } CompatEntry;
251 typedef struct SaveStateEntry {
252 QTAILQ_ENTRY(SaveStateEntry) entry;
253 char idstr[256];
254 int instance_id;
255 int alias_id;
256 int version_id;
257 int section_id;
258 SaveVMHandlers *ops;
259 const VMStateDescription *vmsd;
260 void *opaque;
261 CompatEntry *compat;
262 int is_ram;
263 } SaveStateEntry;
265 typedef struct SaveState {
266 QTAILQ_HEAD(, SaveStateEntry) handlers;
267 int global_section_id;
268 bool skip_configuration;
269 uint32_t len;
270 const char *name;
271 } SaveState;
273 static SaveState savevm_state = {
274 .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers),
275 .global_section_id = 0,
276 .skip_configuration = false,
279 void savevm_skip_configuration(void)
281 savevm_state.skip_configuration = true;
285 static void configuration_pre_save(void *opaque)
287 SaveState *state = opaque;
288 const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
290 state->len = strlen(current_name);
291 state->name = current_name;
294 static int configuration_post_load(void *opaque, int version_id)
296 SaveState *state = opaque;
297 const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
299 if (strncmp(state->name, current_name, state->len) != 0) {
300 error_report("Machine type received is '%s' and local is '%s'",
301 state->name, current_name);
302 return -EINVAL;
304 return 0;
307 static const VMStateDescription vmstate_configuration = {
308 .name = "configuration",
309 .version_id = 1,
310 .post_load = configuration_post_load,
311 .pre_save = configuration_pre_save,
312 .fields = (VMStateField[]) {
313 VMSTATE_UINT32(len, SaveState),
314 VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, 0, len),
315 VMSTATE_END_OF_LIST()
319 static void dump_vmstate_vmsd(FILE *out_file,
320 const VMStateDescription *vmsd, int indent,
321 bool is_subsection);
323 static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field,
324 int indent)
326 fprintf(out_file, "%*s{\n", indent, "");
327 indent += 2;
328 fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name);
329 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
330 field->version_id);
331 fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "",
332 field->field_exists ? "true" : "false");
333 fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size);
334 if (field->vmsd != NULL) {
335 fprintf(out_file, ",\n");
336 dump_vmstate_vmsd(out_file, field->vmsd, indent, false);
338 fprintf(out_file, "\n%*s}", indent - 2, "");
341 static void dump_vmstate_vmss(FILE *out_file,
342 const VMStateDescription **subsection,
343 int indent)
345 if (*subsection != NULL) {
346 dump_vmstate_vmsd(out_file, *subsection, indent, true);
350 static void dump_vmstate_vmsd(FILE *out_file,
351 const VMStateDescription *vmsd, int indent,
352 bool is_subsection)
354 if (is_subsection) {
355 fprintf(out_file, "%*s{\n", indent, "");
356 } else {
357 fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description");
359 indent += 2;
360 fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name);
361 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
362 vmsd->version_id);
363 fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "",
364 vmsd->minimum_version_id);
365 if (vmsd->fields != NULL) {
366 const VMStateField *field = vmsd->fields;
367 bool first;
369 fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, "");
370 first = true;
371 while (field->name != NULL) {
372 if (field->flags & VMS_MUST_EXIST) {
373 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
374 field++;
375 continue;
377 if (!first) {
378 fprintf(out_file, ",\n");
380 dump_vmstate_vmsf(out_file, field, indent + 2);
381 field++;
382 first = false;
384 fprintf(out_file, "\n%*s]", indent, "");
386 if (vmsd->subsections != NULL) {
387 const VMStateDescription **subsection = vmsd->subsections;
388 bool first;
390 fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, "");
391 first = true;
392 while (*subsection != NULL) {
393 if (!first) {
394 fprintf(out_file, ",\n");
396 dump_vmstate_vmss(out_file, subsection, indent + 2);
397 subsection++;
398 first = false;
400 fprintf(out_file, "\n%*s]", indent, "");
402 fprintf(out_file, "\n%*s}", indent - 2, "");
405 static void dump_machine_type(FILE *out_file)
407 MachineClass *mc;
409 mc = MACHINE_GET_CLASS(current_machine);
411 fprintf(out_file, " \"vmschkmachine\": {\n");
412 fprintf(out_file, " \"Name\": \"%s\"\n", mc->name);
413 fprintf(out_file, " },\n");
416 void dump_vmstate_json_to_file(FILE *out_file)
418 GSList *list, *elt;
419 bool first;
421 fprintf(out_file, "{\n");
422 dump_machine_type(out_file);
424 first = true;
425 list = object_class_get_list(TYPE_DEVICE, true);
426 for (elt = list; elt; elt = elt->next) {
427 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
428 TYPE_DEVICE);
429 const char *name;
430 int indent = 2;
432 if (!dc->vmsd) {
433 continue;
436 if (!first) {
437 fprintf(out_file, ",\n");
439 name = object_class_get_name(OBJECT_CLASS(dc));
440 fprintf(out_file, "%*s\"%s\": {\n", indent, "", name);
441 indent += 2;
442 fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name);
443 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
444 dc->vmsd->version_id);
445 fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "",
446 dc->vmsd->minimum_version_id);
448 dump_vmstate_vmsd(out_file, dc->vmsd, indent, false);
450 fprintf(out_file, "\n%*s}", indent - 2, "");
451 first = false;
453 fprintf(out_file, "\n}\n");
454 fclose(out_file);
457 static int calculate_new_instance_id(const char *idstr)
459 SaveStateEntry *se;
460 int instance_id = 0;
462 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
463 if (strcmp(idstr, se->idstr) == 0
464 && instance_id <= se->instance_id) {
465 instance_id = se->instance_id + 1;
468 return instance_id;
471 static int calculate_compat_instance_id(const char *idstr)
473 SaveStateEntry *se;
474 int instance_id = 0;
476 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
477 if (!se->compat) {
478 continue;
481 if (strcmp(idstr, se->compat->idstr) == 0
482 && instance_id <= se->compat->instance_id) {
483 instance_id = se->compat->instance_id + 1;
486 return instance_id;
489 /* TODO: Individual devices generally have very little idea about the rest
490 of the system, so instance_id should be removed/replaced.
491 Meanwhile pass -1 as instance_id if you do not already have a clearly
492 distinguishing id for all instances of your device class. */
493 int register_savevm_live(DeviceState *dev,
494 const char *idstr,
495 int instance_id,
496 int version_id,
497 SaveVMHandlers *ops,
498 void *opaque)
500 SaveStateEntry *se;
502 se = g_new0(SaveStateEntry, 1);
503 se->version_id = version_id;
504 se->section_id = savevm_state.global_section_id++;
505 se->ops = ops;
506 se->opaque = opaque;
507 se->vmsd = NULL;
508 /* if this is a live_savem then set is_ram */
509 if (ops->save_live_setup != NULL) {
510 se->is_ram = 1;
513 if (dev) {
514 char *id = qdev_get_dev_path(dev);
515 if (id) {
516 pstrcpy(se->idstr, sizeof(se->idstr), id);
517 pstrcat(se->idstr, sizeof(se->idstr), "/");
518 g_free(id);
520 se->compat = g_new0(CompatEntry, 1);
521 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
522 se->compat->instance_id = instance_id == -1 ?
523 calculate_compat_instance_id(idstr) : instance_id;
524 instance_id = -1;
527 pstrcat(se->idstr, sizeof(se->idstr), idstr);
529 if (instance_id == -1) {
530 se->instance_id = calculate_new_instance_id(se->idstr);
531 } else {
532 se->instance_id = instance_id;
534 assert(!se->compat || se->instance_id == 0);
535 /* add at the end of list */
536 QTAILQ_INSERT_TAIL(&savevm_state.handlers, se, entry);
537 return 0;
540 int register_savevm(DeviceState *dev,
541 const char *idstr,
542 int instance_id,
543 int version_id,
544 SaveStateHandler *save_state,
545 LoadStateHandler *load_state,
546 void *opaque)
548 SaveVMHandlers *ops = g_new0(SaveVMHandlers, 1);
549 ops->save_state = save_state;
550 ops->load_state = load_state;
551 return register_savevm_live(dev, idstr, instance_id, version_id,
552 ops, opaque);
555 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
557 SaveStateEntry *se, *new_se;
558 char id[256] = "";
560 if (dev) {
561 char *path = qdev_get_dev_path(dev);
562 if (path) {
563 pstrcpy(id, sizeof(id), path);
564 pstrcat(id, sizeof(id), "/");
565 g_free(path);
568 pstrcat(id, sizeof(id), idstr);
570 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
571 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
572 QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
573 g_free(se->compat);
574 g_free(se->ops);
575 g_free(se);
580 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
581 const VMStateDescription *vmsd,
582 void *opaque, int alias_id,
583 int required_for_version)
585 SaveStateEntry *se;
587 /* If this triggers, alias support can be dropped for the vmsd. */
588 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
590 se = g_new0(SaveStateEntry, 1);
591 se->version_id = vmsd->version_id;
592 se->section_id = savevm_state.global_section_id++;
593 se->opaque = opaque;
594 se->vmsd = vmsd;
595 se->alias_id = alias_id;
597 if (dev) {
598 char *id = qdev_get_dev_path(dev);
599 if (id) {
600 pstrcpy(se->idstr, sizeof(se->idstr), id);
601 pstrcat(se->idstr, sizeof(se->idstr), "/");
602 g_free(id);
604 se->compat = g_new0(CompatEntry, 1);
605 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
606 se->compat->instance_id = instance_id == -1 ?
607 calculate_compat_instance_id(vmsd->name) : instance_id;
608 instance_id = -1;
611 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
613 if (instance_id == -1) {
614 se->instance_id = calculate_new_instance_id(se->idstr);
615 } else {
616 se->instance_id = instance_id;
618 assert(!se->compat || se->instance_id == 0);
619 /* add at the end of list */
620 QTAILQ_INSERT_TAIL(&savevm_state.handlers, se, entry);
621 return 0;
624 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
625 void *opaque)
627 SaveStateEntry *se, *new_se;
629 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
630 if (se->vmsd == vmsd && se->opaque == opaque) {
631 QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
632 g_free(se->compat);
633 g_free(se);
638 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
640 trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
641 if (!se->vmsd) { /* Old style */
642 return se->ops->load_state(f, se->opaque, version_id);
644 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
647 static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
649 int64_t old_offset, size;
651 old_offset = qemu_ftell_fast(f);
652 se->ops->save_state(f, se->opaque);
653 size = qemu_ftell_fast(f) - old_offset;
655 if (vmdesc) {
656 json_prop_int(vmdesc, "size", size);
657 json_start_array(vmdesc, "fields");
658 json_start_object(vmdesc, NULL);
659 json_prop_str(vmdesc, "name", "data");
660 json_prop_int(vmdesc, "size", size);
661 json_prop_str(vmdesc, "type", "buffer");
662 json_end_object(vmdesc);
663 json_end_array(vmdesc);
667 static void vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
669 trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
670 if (!se->vmsd) {
671 vmstate_save_old_style(f, se, vmdesc);
672 return;
674 vmstate_save_state(f, se->vmsd, se->opaque, vmdesc);
677 void savevm_skip_section_footers(void)
679 skip_section_footers = true;
683 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
685 static void save_section_header(QEMUFile *f, SaveStateEntry *se,
686 uint8_t section_type)
688 qemu_put_byte(f, section_type);
689 qemu_put_be32(f, se->section_id);
691 if (section_type == QEMU_VM_SECTION_FULL ||
692 section_type == QEMU_VM_SECTION_START) {
693 /* ID string */
694 size_t len = strlen(se->idstr);
695 qemu_put_byte(f, len);
696 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
698 qemu_put_be32(f, se->instance_id);
699 qemu_put_be32(f, se->version_id);
704 * Write a footer onto device sections that catches cases misformatted device
705 * sections.
707 static void save_section_footer(QEMUFile *f, SaveStateEntry *se)
709 if (!skip_section_footers) {
710 qemu_put_byte(f, QEMU_VM_SECTION_FOOTER);
711 qemu_put_be32(f, se->section_id);
716 * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the
717 * command and associated data.
719 * @f: File to send command on
720 * @command: Command type to send
721 * @len: Length of associated data
722 * @data: Data associated with command.
724 void qemu_savevm_command_send(QEMUFile *f,
725 enum qemu_vm_cmd command,
726 uint16_t len,
727 uint8_t *data)
729 trace_savevm_command_send(command, len);
730 qemu_put_byte(f, QEMU_VM_COMMAND);
731 qemu_put_be16(f, (uint16_t)command);
732 qemu_put_be16(f, len);
733 qemu_put_buffer(f, data, len);
734 qemu_fflush(f);
737 void qemu_savevm_send_ping(QEMUFile *f, uint32_t value)
739 uint32_t buf;
741 trace_savevm_send_ping(value);
742 buf = cpu_to_be32(value);
743 qemu_savevm_command_send(f, MIG_CMD_PING, sizeof(value), (uint8_t *)&buf);
746 void qemu_savevm_send_open_return_path(QEMUFile *f)
748 trace_savevm_send_open_return_path();
749 qemu_savevm_command_send(f, MIG_CMD_OPEN_RETURN_PATH, 0, NULL);
752 /* Send prior to any postcopy transfer */
753 void qemu_savevm_send_postcopy_advise(QEMUFile *f)
755 uint64_t tmp[2];
756 tmp[0] = cpu_to_be64(getpagesize());
757 tmp[1] = cpu_to_be64(1ul << qemu_target_page_bits());
759 trace_qemu_savevm_send_postcopy_advise();
760 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 16, (uint8_t *)tmp);
763 /* Sent prior to starting the destination running in postcopy, discard pages
764 * that have already been sent but redirtied on the source.
765 * CMD_POSTCOPY_RAM_DISCARD consist of:
766 * byte version (0)
767 * byte Length of name field (not including 0)
768 * n x byte RAM block name
769 * byte 0 terminator (just for safety)
770 * n x Byte ranges within the named RAMBlock
771 * be64 Start of the range
772 * be64 Length
774 * name: RAMBlock name that these entries are part of
775 * len: Number of page entries
776 * start_list: 'len' addresses
777 * length_list: 'len' addresses
780 void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name,
781 uint16_t len,
782 uint64_t *start_list,
783 uint64_t *length_list)
785 uint8_t *buf;
786 uint16_t tmplen;
787 uint16_t t;
788 size_t name_len = strlen(name);
790 trace_qemu_savevm_send_postcopy_ram_discard(name, len);
791 assert(name_len < 256);
792 buf = g_malloc0(1 + 1 + name_len + 1 + (8 + 8) * len);
793 buf[0] = postcopy_ram_discard_version;
794 buf[1] = name_len;
795 memcpy(buf + 2, name, name_len);
796 tmplen = 2 + name_len;
797 buf[tmplen++] = '\0';
799 for (t = 0; t < len; t++) {
800 cpu_to_be64w((uint64_t *)(buf + tmplen), start_list[t]);
801 tmplen += 8;
802 cpu_to_be64w((uint64_t *)(buf + tmplen), length_list[t]);
803 tmplen += 8;
805 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RAM_DISCARD, tmplen, buf);
806 g_free(buf);
809 /* Get the destination into a state where it can receive postcopy data. */
810 void qemu_savevm_send_postcopy_listen(QEMUFile *f)
812 trace_savevm_send_postcopy_listen();
813 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_LISTEN, 0, NULL);
816 /* Kick the destination into running */
817 void qemu_savevm_send_postcopy_run(QEMUFile *f)
819 trace_savevm_send_postcopy_run();
820 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RUN, 0, NULL);
823 bool qemu_savevm_state_blocked(Error **errp)
825 SaveStateEntry *se;
827 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
828 if (se->vmsd && se->vmsd->unmigratable) {
829 error_setg(errp, "State blocked by non-migratable device '%s'",
830 se->idstr);
831 return true;
834 return false;
837 void qemu_savevm_state_header(QEMUFile *f)
839 trace_savevm_state_header();
840 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
841 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
843 if (!savevm_state.skip_configuration) {
844 qemu_put_byte(f, QEMU_VM_CONFIGURATION);
845 vmstate_save_state(f, &vmstate_configuration, &savevm_state, 0);
850 void qemu_savevm_state_begin(QEMUFile *f,
851 const MigrationParams *params)
853 SaveStateEntry *se;
854 int ret;
856 trace_savevm_state_begin();
857 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
858 if (!se->ops || !se->ops->set_params) {
859 continue;
861 se->ops->set_params(params, se->opaque);
864 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
865 if (!se->ops || !se->ops->save_live_setup) {
866 continue;
868 if (se->ops && se->ops->is_active) {
869 if (!se->ops->is_active(se->opaque)) {
870 continue;
873 save_section_header(f, se, QEMU_VM_SECTION_START);
875 ret = se->ops->save_live_setup(f, se->opaque);
876 save_section_footer(f, se);
877 if (ret < 0) {
878 qemu_file_set_error(f, ret);
879 break;
885 * this function has three return values:
886 * negative: there was one error, and we have -errno.
887 * 0 : We haven't finished, caller have to go again
888 * 1 : We have finished, we can go to complete phase
890 int qemu_savevm_state_iterate(QEMUFile *f)
892 SaveStateEntry *se;
893 int ret = 1;
895 trace_savevm_state_iterate();
896 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
897 if (!se->ops || !se->ops->save_live_iterate) {
898 continue;
900 if (se->ops && se->ops->is_active) {
901 if (!se->ops->is_active(se->opaque)) {
902 continue;
905 if (qemu_file_rate_limit(f)) {
906 return 0;
908 trace_savevm_section_start(se->idstr, se->section_id);
910 save_section_header(f, se, QEMU_VM_SECTION_PART);
912 ret = se->ops->save_live_iterate(f, se->opaque);
913 trace_savevm_section_end(se->idstr, se->section_id, ret);
914 save_section_footer(f, se);
916 if (ret < 0) {
917 qemu_file_set_error(f, ret);
919 if (ret <= 0) {
920 /* Do not proceed to the next vmstate before this one reported
921 completion of the current stage. This serializes the migration
922 and reduces the probability that a faster changing state is
923 synchronized over and over again. */
924 break;
927 return ret;
930 static bool should_send_vmdesc(void)
932 MachineState *machine = MACHINE(qdev_get_machine());
933 return !machine->suppress_vmdesc;
936 void qemu_savevm_state_complete_precopy(QEMUFile *f)
938 QJSON *vmdesc;
939 int vmdesc_len;
940 SaveStateEntry *se;
941 int ret;
943 trace_savevm_state_complete_precopy();
945 cpu_synchronize_all_states();
947 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
948 if (!se->ops || !se->ops->save_live_complete_precopy) {
949 continue;
951 if (se->ops && se->ops->is_active) {
952 if (!se->ops->is_active(se->opaque)) {
953 continue;
956 trace_savevm_section_start(se->idstr, se->section_id);
958 save_section_header(f, se, QEMU_VM_SECTION_END);
960 ret = se->ops->save_live_complete_precopy(f, se->opaque);
961 trace_savevm_section_end(se->idstr, se->section_id, ret);
962 save_section_footer(f, se);
963 if (ret < 0) {
964 qemu_file_set_error(f, ret);
965 return;
969 vmdesc = qjson_new();
970 json_prop_int(vmdesc, "page_size", TARGET_PAGE_SIZE);
971 json_start_array(vmdesc, "devices");
972 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
974 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
975 continue;
977 if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
978 trace_savevm_section_skip(se->idstr, se->section_id);
979 continue;
982 trace_savevm_section_start(se->idstr, se->section_id);
984 json_start_object(vmdesc, NULL);
985 json_prop_str(vmdesc, "name", se->idstr);
986 json_prop_int(vmdesc, "instance_id", se->instance_id);
988 save_section_header(f, se, QEMU_VM_SECTION_FULL);
990 vmstate_save(f, se, vmdesc);
992 json_end_object(vmdesc);
993 trace_savevm_section_end(se->idstr, se->section_id, 0);
994 save_section_footer(f, se);
997 qemu_put_byte(f, QEMU_VM_EOF);
999 json_end_array(vmdesc);
1000 qjson_finish(vmdesc);
1001 vmdesc_len = strlen(qjson_get_str(vmdesc));
1003 if (should_send_vmdesc()) {
1004 qemu_put_byte(f, QEMU_VM_VMDESCRIPTION);
1005 qemu_put_be32(f, vmdesc_len);
1006 qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len);
1008 object_unref(OBJECT(vmdesc));
1010 qemu_fflush(f);
1013 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1015 SaveStateEntry *se;
1016 uint64_t ret = 0;
1018 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1019 if (!se->ops || !se->ops->save_live_pending) {
1020 continue;
1022 if (se->ops && se->ops->is_active) {
1023 if (!se->ops->is_active(se->opaque)) {
1024 continue;
1027 ret += se->ops->save_live_pending(f, se->opaque, max_size);
1029 return ret;
1032 void qemu_savevm_state_cleanup(void)
1034 SaveStateEntry *se;
1036 trace_savevm_state_cleanup();
1037 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1038 if (se->ops && se->ops->cleanup) {
1039 se->ops->cleanup(se->opaque);
1044 static int qemu_savevm_state(QEMUFile *f, Error **errp)
1046 int ret;
1047 MigrationParams params = {
1048 .blk = 0,
1049 .shared = 0
1051 MigrationState *ms = migrate_init(&params);
1052 ms->file = f;
1054 if (qemu_savevm_state_blocked(errp)) {
1055 return -EINVAL;
1058 qemu_mutex_unlock_iothread();
1059 qemu_savevm_state_header(f);
1060 qemu_savevm_state_begin(f, &params);
1061 qemu_mutex_lock_iothread();
1063 while (qemu_file_get_error(f) == 0) {
1064 if (qemu_savevm_state_iterate(f) > 0) {
1065 break;
1069 ret = qemu_file_get_error(f);
1070 if (ret == 0) {
1071 qemu_savevm_state_complete_precopy(f);
1072 ret = qemu_file_get_error(f);
1074 if (ret != 0) {
1075 qemu_savevm_state_cleanup();
1076 error_setg_errno(errp, -ret, "Error while writing VM state");
1078 return ret;
1081 static int qemu_save_device_state(QEMUFile *f)
1083 SaveStateEntry *se;
1085 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1086 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1088 cpu_synchronize_all_states();
1090 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1091 if (se->is_ram) {
1092 continue;
1094 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1095 continue;
1097 if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1098 continue;
1101 save_section_header(f, se, QEMU_VM_SECTION_FULL);
1103 vmstate_save(f, se, NULL);
1105 save_section_footer(f, se);
1108 qemu_put_byte(f, QEMU_VM_EOF);
1110 return qemu_file_get_error(f);
1113 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1115 SaveStateEntry *se;
1117 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1118 if (!strcmp(se->idstr, idstr) &&
1119 (instance_id == se->instance_id ||
1120 instance_id == se->alias_id))
1121 return se;
1122 /* Migrating from an older version? */
1123 if (strstr(se->idstr, idstr) && se->compat) {
1124 if (!strcmp(se->compat->idstr, idstr) &&
1125 (instance_id == se->compat->instance_id ||
1126 instance_id == se->alias_id))
1127 return se;
1130 return NULL;
1133 enum LoadVMExitCodes {
1134 /* Allow a command to quit all layers of nested loadvm loops */
1135 LOADVM_QUIT = 1,
1138 static int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis);
1140 /* ------ incoming postcopy messages ------ */
1141 /* 'advise' arrives before any transfers just to tell us that a postcopy
1142 * *might* happen - it might be skipped if precopy transferred everything
1143 * quickly.
1145 static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis)
1147 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
1148 uint64_t remote_hps, remote_tps;
1150 trace_loadvm_postcopy_handle_advise();
1151 if (ps != POSTCOPY_INCOMING_NONE) {
1152 error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps);
1153 return -1;
1156 remote_hps = qemu_get_be64(mis->from_src_file);
1157 if (remote_hps != getpagesize()) {
1159 * Some combinations of mismatch are probably possible but it gets
1160 * a bit more complicated. In particular we need to place whole
1161 * host pages on the dest at once, and we need to ensure that we
1162 * handle dirtying to make sure we never end up sending part of
1163 * a hostpage on it's own.
1165 error_report("Postcopy needs matching host page sizes (s=%d d=%d)",
1166 (int)remote_hps, getpagesize());
1167 return -1;
1170 remote_tps = qemu_get_be64(mis->from_src_file);
1171 if (remote_tps != (1ul << qemu_target_page_bits())) {
1173 * Again, some differences could be dealt with, but for now keep it
1174 * simple.
1176 error_report("Postcopy needs matching target page sizes (s=%d d=%d)",
1177 (int)remote_tps, 1 << qemu_target_page_bits());
1178 return -1;
1181 return 0;
1184 /* After postcopy we will be told to throw some pages away since they're
1185 * dirty and will have to be demand fetched. Must happen before CPU is
1186 * started.
1187 * There can be 0..many of these messages, each encoding multiple pages.
1189 static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis,
1190 uint16_t len)
1192 int tmp;
1193 char ramid[256];
1194 PostcopyState ps = postcopy_state_get();
1196 trace_loadvm_postcopy_ram_handle_discard();
1198 switch (ps) {
1199 case POSTCOPY_INCOMING_ADVISE:
1200 /* 1st discard */
1201 tmp = 0; /* TODO: later patch postcopy_ram_prepare_discard(mis); */
1202 if (tmp) {
1203 return tmp;
1205 break;
1207 case POSTCOPY_INCOMING_DISCARD:
1208 /* Expected state */
1209 break;
1211 default:
1212 error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)",
1213 ps);
1214 return -1;
1216 /* We're expecting a
1217 * Version (0)
1218 * a RAM ID string (length byte, name, 0 term)
1219 * then at least 1 16 byte chunk
1221 if (len < (1 + 1 + 1 + 1 + 2 * 8)) {
1222 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1223 return -1;
1226 tmp = qemu_get_byte(mis->from_src_file);
1227 if (tmp != postcopy_ram_discard_version) {
1228 error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp);
1229 return -1;
1232 if (!qemu_get_counted_string(mis->from_src_file, ramid)) {
1233 error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID");
1234 return -1;
1236 tmp = qemu_get_byte(mis->from_src_file);
1237 if (tmp != 0) {
1238 error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp);
1239 return -1;
1242 len -= 3 + strlen(ramid);
1243 if (len % 16) {
1244 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1245 return -1;
1247 trace_loadvm_postcopy_ram_handle_discard_header(ramid, len);
1248 while (len) {
1249 /* TODO - ram_discard_range gets added in a later patch
1250 uint64_t start_addr, block_length;
1251 start_addr = qemu_get_be64(mis->from_src_file);
1252 block_length = qemu_get_be64(mis->from_src_file);
1254 len -= 16;
1255 int ret = ram_discard_range(mis, ramid, start_addr,
1256 block_length);
1257 if (ret) {
1258 return ret;
1262 trace_loadvm_postcopy_ram_handle_discard_end();
1264 return 0;
1267 /* After this message we must be able to immediately receive postcopy data */
1268 static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)
1270 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_LISTENING);
1271 trace_loadvm_postcopy_handle_listen();
1272 if (ps != POSTCOPY_INCOMING_ADVISE && ps != POSTCOPY_INCOMING_DISCARD) {
1273 error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps);
1274 return -1;
1277 /* TODO start up the postcopy listening thread */
1278 return 0;
1281 /* After all discards we can start running and asking for pages */
1282 static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
1284 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
1285 trace_loadvm_postcopy_handle_run();
1286 if (ps != POSTCOPY_INCOMING_LISTENING) {
1287 error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps);
1288 return -1;
1291 if (autostart) {
1292 /* Hold onto your hats, starting the CPU */
1293 vm_start();
1294 } else {
1295 /* leave it paused and let management decide when to start the CPU */
1296 runstate_set(RUN_STATE_PAUSED);
1299 return 0;
1303 * loadvm_process_command: Process an incoming 'QEMU_VM_COMMAND'
1305 * Returns: 0 on just a normal return
1306 * LOADVM_QUIT All good, but exit the loop
1307 * <0 error (in which case it will issue an error message).
1308 * @f: The stream to read the command data from.
1310 static int loadvm_process_command(QEMUFile *f)
1312 MigrationIncomingState *mis = migration_incoming_get_current();
1313 uint16_t cmd;
1314 uint16_t len;
1315 uint32_t tmp32;
1317 cmd = qemu_get_be16(f);
1318 len = qemu_get_be16(f);
1320 trace_loadvm_process_command(cmd, len);
1321 if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) {
1322 error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len);
1323 return -EINVAL;
1326 if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) {
1327 error_report("%s received with bad length - expecting %zu, got %d",
1328 mig_cmd_args[cmd].name,
1329 (size_t)mig_cmd_args[cmd].len, len);
1330 return -ERANGE;
1333 switch (cmd) {
1334 case MIG_CMD_OPEN_RETURN_PATH:
1335 if (mis->to_src_file) {
1336 error_report("CMD_OPEN_RETURN_PATH called when RP already open");
1337 /* Not really a problem, so don't give up */
1338 return 0;
1340 mis->to_src_file = qemu_file_get_return_path(f);
1341 if (!mis->to_src_file) {
1342 error_report("CMD_OPEN_RETURN_PATH failed");
1343 return -1;
1345 break;
1347 case MIG_CMD_PING:
1348 tmp32 = qemu_get_be32(f);
1349 trace_loadvm_process_command_ping(tmp32);
1350 if (!mis->to_src_file) {
1351 error_report("CMD_PING (0x%x) received with no return path",
1352 tmp32);
1353 return -1;
1355 migrate_send_rp_pong(mis, tmp32);
1356 break;
1358 case MIG_CMD_POSTCOPY_ADVISE:
1359 return loadvm_postcopy_handle_advise(mis);
1361 case MIG_CMD_POSTCOPY_LISTEN:
1362 return loadvm_postcopy_handle_listen(mis);
1364 case MIG_CMD_POSTCOPY_RUN:
1365 return loadvm_postcopy_handle_run(mis);
1367 case MIG_CMD_POSTCOPY_RAM_DISCARD:
1368 return loadvm_postcopy_ram_handle_discard(mis, len);
1371 return 0;
1374 struct LoadStateEntry {
1375 QLIST_ENTRY(LoadStateEntry) entry;
1376 SaveStateEntry *se;
1377 int section_id;
1378 int version_id;
1382 * Read a footer off the wire and check that it matches the expected section
1384 * Returns: true if the footer was good
1385 * false if there is a problem (and calls error_report to say why)
1387 static bool check_section_footer(QEMUFile *f, LoadStateEntry *le)
1389 uint8_t read_mark;
1390 uint32_t read_section_id;
1392 if (skip_section_footers) {
1393 /* No footer to check */
1394 return true;
1397 read_mark = qemu_get_byte(f);
1399 if (read_mark != QEMU_VM_SECTION_FOOTER) {
1400 error_report("Missing section footer for %s", le->se->idstr);
1401 return false;
1404 read_section_id = qemu_get_be32(f);
1405 if (read_section_id != le->section_id) {
1406 error_report("Mismatched section id in footer for %s -"
1407 " read 0x%x expected 0x%x",
1408 le->se->idstr, read_section_id, le->section_id);
1409 return false;
1412 /* All good */
1413 return true;
1416 void loadvm_free_handlers(MigrationIncomingState *mis)
1418 LoadStateEntry *le, *new_le;
1420 QLIST_FOREACH_SAFE(le, &mis->loadvm_handlers, entry, new_le) {
1421 QLIST_REMOVE(le, entry);
1422 g_free(le);
1426 static int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis)
1428 uint8_t section_type;
1429 int ret;
1431 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1432 uint32_t instance_id, version_id, section_id;
1433 SaveStateEntry *se;
1434 LoadStateEntry *le;
1435 char idstr[256];
1437 trace_qemu_loadvm_state_section(section_type);
1438 switch (section_type) {
1439 case QEMU_VM_SECTION_START:
1440 case QEMU_VM_SECTION_FULL:
1441 /* Read section start */
1442 section_id = qemu_get_be32(f);
1443 if (!qemu_get_counted_string(f, idstr)) {
1444 error_report("Unable to read ID string for section %u",
1445 section_id);
1446 return -EINVAL;
1448 instance_id = qemu_get_be32(f);
1449 version_id = qemu_get_be32(f);
1451 trace_qemu_loadvm_state_section_startfull(section_id, idstr,
1452 instance_id, version_id);
1453 /* Find savevm section */
1454 se = find_se(idstr, instance_id);
1455 if (se == NULL) {
1456 error_report("Unknown savevm section or instance '%s' %d",
1457 idstr, instance_id);
1458 return -EINVAL;
1461 /* Validate version */
1462 if (version_id > se->version_id) {
1463 error_report("savevm: unsupported version %d for '%s' v%d",
1464 version_id, idstr, se->version_id);
1465 return -EINVAL;
1468 /* Add entry */
1469 le = g_malloc0(sizeof(*le));
1471 le->se = se;
1472 le->section_id = section_id;
1473 le->version_id = version_id;
1474 QLIST_INSERT_HEAD(&mis->loadvm_handlers, le, entry);
1476 ret = vmstate_load(f, le->se, le->version_id);
1477 if (ret < 0) {
1478 error_report("error while loading state for instance 0x%x of"
1479 " device '%s'", instance_id, idstr);
1480 return ret;
1482 if (!check_section_footer(f, le)) {
1483 return -EINVAL;
1485 break;
1486 case QEMU_VM_SECTION_PART:
1487 case QEMU_VM_SECTION_END:
1488 section_id = qemu_get_be32(f);
1490 trace_qemu_loadvm_state_section_partend(section_id);
1491 QLIST_FOREACH(le, &mis->loadvm_handlers, entry) {
1492 if (le->section_id == section_id) {
1493 break;
1496 if (le == NULL) {
1497 error_report("Unknown savevm section %d", section_id);
1498 return -EINVAL;
1501 ret = vmstate_load(f, le->se, le->version_id);
1502 if (ret < 0) {
1503 error_report("error while loading state section id %d(%s)",
1504 section_id, le->se->idstr);
1505 return ret;
1507 if (!check_section_footer(f, le)) {
1508 return -EINVAL;
1510 break;
1511 case QEMU_VM_COMMAND:
1512 ret = loadvm_process_command(f);
1513 trace_qemu_loadvm_state_section_command(ret);
1514 if ((ret < 0) || (ret & LOADVM_QUIT)) {
1515 return ret;
1517 break;
1518 default:
1519 error_report("Unknown savevm section type %d", section_type);
1520 return -EINVAL;
1524 return 0;
1527 int qemu_loadvm_state(QEMUFile *f)
1529 MigrationIncomingState *mis = migration_incoming_get_current();
1530 Error *local_err = NULL;
1531 unsigned int v;
1532 int ret;
1534 if (qemu_savevm_state_blocked(&local_err)) {
1535 error_report_err(local_err);
1536 return -EINVAL;
1539 v = qemu_get_be32(f);
1540 if (v != QEMU_VM_FILE_MAGIC) {
1541 error_report("Not a migration stream");
1542 return -EINVAL;
1545 v = qemu_get_be32(f);
1546 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1547 error_report("SaveVM v2 format is obsolete and don't work anymore");
1548 return -ENOTSUP;
1550 if (v != QEMU_VM_FILE_VERSION) {
1551 error_report("Unsupported migration stream version");
1552 return -ENOTSUP;
1555 if (!savevm_state.skip_configuration) {
1556 if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) {
1557 error_report("Configuration section missing");
1558 return -EINVAL;
1560 ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0);
1562 if (ret) {
1563 return ret;
1567 ret = qemu_loadvm_state_main(f, mis);
1568 qemu_event_set(&mis->main_thread_load_event);
1570 trace_qemu_loadvm_state_post_main(ret);
1572 if (ret == 0) {
1573 ret = qemu_file_get_error(f);
1577 * Try to read in the VMDESC section as well, so that dumping tools that
1578 * intercept our migration stream have the chance to see it.
1581 /* We've got to be careful; if we don't read the data and just shut the fd
1582 * then the sender can error if we close while it's still sending.
1583 * We also mustn't read data that isn't there; some transports (RDMA)
1584 * will stall waiting for that data when the source has already closed.
1586 if (ret == 0 && should_send_vmdesc()) {
1587 uint8_t *buf;
1588 uint32_t size;
1589 uint8_t section_type = qemu_get_byte(f);
1591 if (section_type != QEMU_VM_VMDESCRIPTION) {
1592 error_report("Expected vmdescription section, but got %d",
1593 section_type);
1595 * It doesn't seem worth failing at this point since
1596 * we apparently have an otherwise valid VM state
1598 } else {
1599 buf = g_malloc(0x1000);
1600 size = qemu_get_be32(f);
1602 while (size > 0) {
1603 uint32_t read_chunk = MIN(size, 0x1000);
1604 qemu_get_buffer(f, buf, read_chunk);
1605 size -= read_chunk;
1607 g_free(buf);
1611 cpu_synchronize_all_post_init();
1613 return ret;
1616 static BlockDriverState *find_vmstate_bs(void)
1618 BlockDriverState *bs = NULL;
1619 while ((bs = bdrv_next(bs))) {
1620 if (bdrv_can_snapshot(bs)) {
1621 return bs;
1624 return NULL;
1628 * Deletes snapshots of a given name in all opened images.
1630 static int del_existing_snapshots(Monitor *mon, const char *name)
1632 BlockDriverState *bs;
1633 QEMUSnapshotInfo sn1, *snapshot = &sn1;
1634 Error *err = NULL;
1636 bs = NULL;
1637 while ((bs = bdrv_next(bs))) {
1638 if (bdrv_can_snapshot(bs) &&
1639 bdrv_snapshot_find(bs, snapshot, name) >= 0) {
1640 bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
1641 if (err) {
1642 monitor_printf(mon,
1643 "Error while deleting snapshot on device '%s':"
1644 " %s\n",
1645 bdrv_get_device_name(bs),
1646 error_get_pretty(err));
1647 error_free(err);
1648 return -1;
1653 return 0;
1656 void hmp_savevm(Monitor *mon, const QDict *qdict)
1658 BlockDriverState *bs, *bs1;
1659 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1660 int ret;
1661 QEMUFile *f;
1662 int saved_vm_running;
1663 uint64_t vm_state_size;
1664 qemu_timeval tv;
1665 struct tm tm;
1666 const char *name = qdict_get_try_str(qdict, "name");
1667 Error *local_err = NULL;
1669 /* Verify if there is a device that doesn't support snapshots and is writable */
1670 bs = NULL;
1671 while ((bs = bdrv_next(bs))) {
1673 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
1674 continue;
1677 if (!bdrv_can_snapshot(bs)) {
1678 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
1679 bdrv_get_device_name(bs));
1680 return;
1684 bs = find_vmstate_bs();
1685 if (!bs) {
1686 monitor_printf(mon, "No block device can accept snapshots\n");
1687 return;
1690 saved_vm_running = runstate_is_running();
1692 ret = global_state_store();
1693 if (ret) {
1694 monitor_printf(mon, "Error saving global state\n");
1695 return;
1697 vm_stop(RUN_STATE_SAVE_VM);
1699 memset(sn, 0, sizeof(*sn));
1701 /* fill auxiliary fields */
1702 qemu_gettimeofday(&tv);
1703 sn->date_sec = tv.tv_sec;
1704 sn->date_nsec = tv.tv_usec * 1000;
1705 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1707 if (name) {
1708 ret = bdrv_snapshot_find(bs, old_sn, name);
1709 if (ret >= 0) {
1710 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1711 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1712 } else {
1713 pstrcpy(sn->name, sizeof(sn->name), name);
1715 } else {
1716 /* cast below needed for OpenBSD where tv_sec is still 'long' */
1717 localtime_r((const time_t *)&tv.tv_sec, &tm);
1718 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
1721 /* Delete old snapshots of the same name */
1722 if (name && del_existing_snapshots(mon, name) < 0) {
1723 goto the_end;
1726 /* save the VM state */
1727 f = qemu_fopen_bdrv(bs, 1);
1728 if (!f) {
1729 monitor_printf(mon, "Could not open VM state file\n");
1730 goto the_end;
1732 ret = qemu_savevm_state(f, &local_err);
1733 vm_state_size = qemu_ftell(f);
1734 qemu_fclose(f);
1735 if (ret < 0) {
1736 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1737 error_free(local_err);
1738 goto the_end;
1741 /* create the snapshots */
1743 bs1 = NULL;
1744 while ((bs1 = bdrv_next(bs1))) {
1745 if (bdrv_can_snapshot(bs1)) {
1746 /* Write VM state size only to the image that contains the state */
1747 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1748 ret = bdrv_snapshot_create(bs1, sn);
1749 if (ret < 0) {
1750 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1751 bdrv_get_device_name(bs1));
1756 the_end:
1757 if (saved_vm_running) {
1758 vm_start();
1762 void qmp_xen_save_devices_state(const char *filename, Error **errp)
1764 QEMUFile *f;
1765 int saved_vm_running;
1766 int ret;
1768 saved_vm_running = runstate_is_running();
1769 vm_stop(RUN_STATE_SAVE_VM);
1770 global_state_store_running();
1772 f = qemu_fopen(filename, "wb");
1773 if (!f) {
1774 error_setg_file_open(errp, errno, filename);
1775 goto the_end;
1777 ret = qemu_save_device_state(f);
1778 qemu_fclose(f);
1779 if (ret < 0) {
1780 error_setg(errp, QERR_IO_ERROR);
1783 the_end:
1784 if (saved_vm_running) {
1785 vm_start();
1789 int load_vmstate(const char *name)
1791 BlockDriverState *bs, *bs_vm_state;
1792 QEMUSnapshotInfo sn;
1793 QEMUFile *f;
1794 int ret;
1796 bs_vm_state = find_vmstate_bs();
1797 if (!bs_vm_state) {
1798 error_report("No block device supports snapshots");
1799 return -ENOTSUP;
1802 /* Don't even try to load empty VM states */
1803 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
1804 if (ret < 0) {
1805 return ret;
1806 } else if (sn.vm_state_size == 0) {
1807 error_report("This is a disk-only snapshot. Revert to it offline "
1808 "using qemu-img.");
1809 return -EINVAL;
1812 /* Verify if there is any device that doesn't support snapshots and is
1813 writable and check if the requested snapshot is available too. */
1814 bs = NULL;
1815 while ((bs = bdrv_next(bs))) {
1817 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
1818 continue;
1821 if (!bdrv_can_snapshot(bs)) {
1822 error_report("Device '%s' is writable but does not support snapshots.",
1823 bdrv_get_device_name(bs));
1824 return -ENOTSUP;
1827 ret = bdrv_snapshot_find(bs, &sn, name);
1828 if (ret < 0) {
1829 error_report("Device '%s' does not have the requested snapshot '%s'",
1830 bdrv_get_device_name(bs), name);
1831 return ret;
1835 /* Flush all IO requests so they don't interfere with the new state. */
1836 bdrv_drain_all();
1838 bs = NULL;
1839 while ((bs = bdrv_next(bs))) {
1840 if (bdrv_can_snapshot(bs)) {
1841 ret = bdrv_snapshot_goto(bs, name);
1842 if (ret < 0) {
1843 error_report("Error %d while activating snapshot '%s' on '%s'",
1844 ret, name, bdrv_get_device_name(bs));
1845 return ret;
1850 /* restore the VM state */
1851 f = qemu_fopen_bdrv(bs_vm_state, 0);
1852 if (!f) {
1853 error_report("Could not open VM state file");
1854 return -EINVAL;
1857 qemu_system_reset(VMRESET_SILENT);
1858 migration_incoming_state_new(f);
1859 ret = qemu_loadvm_state(f);
1861 qemu_fclose(f);
1862 migration_incoming_state_destroy();
1863 if (ret < 0) {
1864 error_report("Error %d while loading VM state", ret);
1865 return ret;
1868 return 0;
1871 void hmp_delvm(Monitor *mon, const QDict *qdict)
1873 BlockDriverState *bs;
1874 Error *err;
1875 const char *name = qdict_get_str(qdict, "name");
1877 if (!find_vmstate_bs()) {
1878 monitor_printf(mon, "No block device supports snapshots\n");
1879 return;
1882 bs = NULL;
1883 while ((bs = bdrv_next(bs))) {
1884 if (bdrv_can_snapshot(bs)) {
1885 err = NULL;
1886 bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
1887 if (err) {
1888 monitor_printf(mon,
1889 "Error while deleting snapshot on device '%s':"
1890 " %s\n",
1891 bdrv_get_device_name(bs),
1892 error_get_pretty(err));
1893 error_free(err);
1899 void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
1901 BlockDriverState *bs, *bs1;
1902 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
1903 int nb_sns, i, ret, available;
1904 int total;
1905 int *available_snapshots;
1907 bs = find_vmstate_bs();
1908 if (!bs) {
1909 monitor_printf(mon, "No available block device supports snapshots\n");
1910 return;
1913 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1914 if (nb_sns < 0) {
1915 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1916 return;
1919 if (nb_sns == 0) {
1920 monitor_printf(mon, "There is no snapshot available.\n");
1921 return;
1924 available_snapshots = g_new0(int, nb_sns);
1925 total = 0;
1926 for (i = 0; i < nb_sns; i++) {
1927 sn = &sn_tab[i];
1928 available = 1;
1929 bs1 = NULL;
1931 while ((bs1 = bdrv_next(bs1))) {
1932 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
1933 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
1934 if (ret < 0) {
1935 available = 0;
1936 break;
1941 if (available) {
1942 available_snapshots[total] = i;
1943 total++;
1947 if (total > 0) {
1948 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1949 monitor_printf(mon, "\n");
1950 for (i = 0; i < total; i++) {
1951 sn = &sn_tab[available_snapshots[i]];
1952 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
1953 monitor_printf(mon, "\n");
1955 } else {
1956 monitor_printf(mon, "There is no suitable snapshot available\n");
1959 g_free(sn_tab);
1960 g_free(available_snapshots);
1964 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
1966 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
1967 memory_region_name(mr), dev);
1970 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
1972 qemu_ram_unset_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK);
1975 void vmstate_register_ram_global(MemoryRegion *mr)
1977 vmstate_register_ram(mr, NULL);