4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009-2015 Red Hat Inc
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
29 #include "config-host.h"
30 #include "qemu-common.h"
31 #include "hw/boards.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 "qemu/sockets.h"
41 #include "qemu/queue.h"
42 #include "sysemu/cpus.h"
43 #include "exec/memory.h"
44 #include "qmp-commands.h"
47 #include "block/snapshot.h"
48 #include "block/qapi.h"
52 #define ETH_P_RARP 0x8035
54 #define ARP_HTYPE_ETH 0x0001
55 #define ARP_PTYPE_IP 0x0800
56 #define ARP_OP_REQUEST_REV 0x3
58 static bool skip_section_footers
;
60 static int announce_self_create(uint8_t *buf
,
63 /* Ethernet header. */
64 memset(buf
, 0xff, 6); /* destination MAC addr */
65 memcpy(buf
+ 6, mac_addr
, 6); /* source MAC addr */
66 *(uint16_t *)(buf
+ 12) = htons(ETH_P_RARP
); /* ethertype */
69 *(uint16_t *)(buf
+ 14) = htons(ARP_HTYPE_ETH
); /* hardware addr space */
70 *(uint16_t *)(buf
+ 16) = htons(ARP_PTYPE_IP
); /* protocol addr space */
71 *(buf
+ 18) = 6; /* hardware addr length (ethernet) */
72 *(buf
+ 19) = 4; /* protocol addr length (IPv4) */
73 *(uint16_t *)(buf
+ 20) = htons(ARP_OP_REQUEST_REV
); /* opcode */
74 memcpy(buf
+ 22, mac_addr
, 6); /* source hw addr */
75 memset(buf
+ 28, 0x00, 4); /* source protocol addr */
76 memcpy(buf
+ 32, mac_addr
, 6); /* target hw addr */
77 memset(buf
+ 38, 0x00, 4); /* target protocol addr */
79 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
80 memset(buf
+ 42, 0x00, 18);
82 return 60; /* len (FCS will be added by hardware) */
85 static void qemu_announce_self_iter(NICState
*nic
, void *opaque
)
90 trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic
->conf
->macaddr
));
91 len
= announce_self_create(buf
, nic
->conf
->macaddr
.a
);
93 qemu_send_packet_raw(qemu_get_queue(nic
), buf
, len
);
97 static void qemu_announce_self_once(void *opaque
)
99 static int count
= SELF_ANNOUNCE_ROUNDS
;
100 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
102 qemu_foreach_nic(qemu_announce_self_iter
, NULL
);
105 /* delay 50ms, 150ms, 250ms, ... */
106 timer_mod(timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) +
107 self_announce_delay(count
));
114 void qemu_announce_self(void)
116 static QEMUTimer
*timer
;
117 timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, qemu_announce_self_once
, &timer
);
118 qemu_announce_self_once(&timer
);
121 /***********************************************************/
122 /* savevm/loadvm support */
124 static ssize_t
block_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
130 qemu_iovec_init_external(&qiov
, iov
, iovcnt
);
131 ret
= bdrv_writev_vmstate(opaque
, &qiov
, pos
);
139 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
140 int64_t pos
, int size
)
142 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
146 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
148 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
151 static int bdrv_fclose(void *opaque
)
153 return bdrv_flush(opaque
);
156 static const QEMUFileOps bdrv_read_ops
= {
157 .get_buffer
= block_get_buffer
,
161 static const QEMUFileOps bdrv_write_ops
= {
162 .put_buffer
= block_put_buffer
,
163 .writev_buffer
= block_writev_buffer
,
167 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
170 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
172 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
176 /* QEMUFile timer support.
177 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
180 void timer_put(QEMUFile
*f
, QEMUTimer
*ts
)
182 uint64_t expire_time
;
184 expire_time
= timer_expire_time_ns(ts
);
185 qemu_put_be64(f
, expire_time
);
188 void timer_get(QEMUFile
*f
, QEMUTimer
*ts
)
190 uint64_t expire_time
;
192 expire_time
= qemu_get_be64(f
);
193 if (expire_time
!= -1) {
194 timer_mod_ns(ts
, expire_time
);
201 /* VMState timer support.
202 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
205 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
212 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
218 const VMStateInfo vmstate_info_timer
= {
225 typedef struct CompatEntry
{
230 typedef struct SaveStateEntry
{
231 QTAILQ_ENTRY(SaveStateEntry
) entry
;
238 const VMStateDescription
*vmsd
;
244 typedef struct SaveState
{
245 QTAILQ_HEAD(, SaveStateEntry
) handlers
;
246 int global_section_id
;
249 static SaveState savevm_state
= {
250 .handlers
= QTAILQ_HEAD_INITIALIZER(savevm_state
.handlers
),
251 .global_section_id
= 0,
254 static void dump_vmstate_vmsd(FILE *out_file
,
255 const VMStateDescription
*vmsd
, int indent
,
258 static void dump_vmstate_vmsf(FILE *out_file
, const VMStateField
*field
,
261 fprintf(out_file
, "%*s{\n", indent
, "");
263 fprintf(out_file
, "%*s\"field\": \"%s\",\n", indent
, "", field
->name
);
264 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
266 fprintf(out_file
, "%*s\"field_exists\": %s,\n", indent
, "",
267 field
->field_exists
? "true" : "false");
268 fprintf(out_file
, "%*s\"size\": %zu", indent
, "", field
->size
);
269 if (field
->vmsd
!= NULL
) {
270 fprintf(out_file
, ",\n");
271 dump_vmstate_vmsd(out_file
, field
->vmsd
, indent
, false);
273 fprintf(out_file
, "\n%*s}", indent
- 2, "");
276 static void dump_vmstate_vmss(FILE *out_file
,
277 const VMStateDescription
**subsection
,
280 if (*subsection
!= NULL
) {
281 dump_vmstate_vmsd(out_file
, *subsection
, indent
, true);
285 static void dump_vmstate_vmsd(FILE *out_file
,
286 const VMStateDescription
*vmsd
, int indent
,
290 fprintf(out_file
, "%*s{\n", indent
, "");
292 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", "Description");
295 fprintf(out_file
, "%*s\"name\": \"%s\",\n", indent
, "", vmsd
->name
);
296 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
298 fprintf(out_file
, "%*s\"minimum_version_id\": %d", indent
, "",
299 vmsd
->minimum_version_id
);
300 if (vmsd
->fields
!= NULL
) {
301 const VMStateField
*field
= vmsd
->fields
;
304 fprintf(out_file
, ",\n%*s\"Fields\": [\n", indent
, "");
306 while (field
->name
!= NULL
) {
307 if (field
->flags
& VMS_MUST_EXIST
) {
308 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
313 fprintf(out_file
, ",\n");
315 dump_vmstate_vmsf(out_file
, field
, indent
+ 2);
319 fprintf(out_file
, "\n%*s]", indent
, "");
321 if (vmsd
->subsections
!= NULL
) {
322 const VMStateDescription
**subsection
= vmsd
->subsections
;
325 fprintf(out_file
, ",\n%*s\"Subsections\": [\n", indent
, "");
327 while (*subsection
!= NULL
) {
329 fprintf(out_file
, ",\n");
331 dump_vmstate_vmss(out_file
, subsection
, indent
+ 2);
335 fprintf(out_file
, "\n%*s]", indent
, "");
337 fprintf(out_file
, "\n%*s}", indent
- 2, "");
340 static void dump_machine_type(FILE *out_file
)
344 mc
= MACHINE_GET_CLASS(current_machine
);
346 fprintf(out_file
, " \"vmschkmachine\": {\n");
347 fprintf(out_file
, " \"Name\": \"%s\"\n", mc
->name
);
348 fprintf(out_file
, " },\n");
351 void dump_vmstate_json_to_file(FILE *out_file
)
356 fprintf(out_file
, "{\n");
357 dump_machine_type(out_file
);
360 list
= object_class_get_list(TYPE_DEVICE
, true);
361 for (elt
= list
; elt
; elt
= elt
->next
) {
362 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
372 fprintf(out_file
, ",\n");
374 name
= object_class_get_name(OBJECT_CLASS(dc
));
375 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", name
);
377 fprintf(out_file
, "%*s\"Name\": \"%s\",\n", indent
, "", name
);
378 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
379 dc
->vmsd
->version_id
);
380 fprintf(out_file
, "%*s\"minimum_version_id\": %d,\n", indent
, "",
381 dc
->vmsd
->minimum_version_id
);
383 dump_vmstate_vmsd(out_file
, dc
->vmsd
, indent
, false);
385 fprintf(out_file
, "\n%*s}", indent
- 2, "");
388 fprintf(out_file
, "\n}\n");
392 static int calculate_new_instance_id(const char *idstr
)
397 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
398 if (strcmp(idstr
, se
->idstr
) == 0
399 && instance_id
<= se
->instance_id
) {
400 instance_id
= se
->instance_id
+ 1;
406 static int calculate_compat_instance_id(const char *idstr
)
411 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
416 if (strcmp(idstr
, se
->compat
->idstr
) == 0
417 && instance_id
<= se
->compat
->instance_id
) {
418 instance_id
= se
->compat
->instance_id
+ 1;
424 /* TODO: Individual devices generally have very little idea about the rest
425 of the system, so instance_id should be removed/replaced.
426 Meanwhile pass -1 as instance_id if you do not already have a clearly
427 distinguishing id for all instances of your device class. */
428 int register_savevm_live(DeviceState
*dev
,
437 se
= g_malloc0(sizeof(SaveStateEntry
));
438 se
->version_id
= version_id
;
439 se
->section_id
= savevm_state
.global_section_id
++;
443 /* if this is a live_savem then set is_ram */
444 if (ops
->save_live_setup
!= NULL
) {
449 char *id
= qdev_get_dev_path(dev
);
451 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
452 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
455 se
->compat
= g_malloc0(sizeof(CompatEntry
));
456 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
457 se
->compat
->instance_id
= instance_id
== -1 ?
458 calculate_compat_instance_id(idstr
) : instance_id
;
462 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
464 if (instance_id
== -1) {
465 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
467 se
->instance_id
= instance_id
;
469 assert(!se
->compat
|| se
->instance_id
== 0);
470 /* add at the end of list */
471 QTAILQ_INSERT_TAIL(&savevm_state
.handlers
, se
, entry
);
475 int register_savevm(DeviceState
*dev
,
479 SaveStateHandler
*save_state
,
480 LoadStateHandler
*load_state
,
483 SaveVMHandlers
*ops
= g_malloc0(sizeof(SaveVMHandlers
));
484 ops
->save_state
= save_state
;
485 ops
->load_state
= load_state
;
486 return register_savevm_live(dev
, idstr
, instance_id
, version_id
,
490 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
492 SaveStateEntry
*se
, *new_se
;
496 char *path
= qdev_get_dev_path(dev
);
498 pstrcpy(id
, sizeof(id
), path
);
499 pstrcat(id
, sizeof(id
), "/");
503 pstrcat(id
, sizeof(id
), idstr
);
505 QTAILQ_FOREACH_SAFE(se
, &savevm_state
.handlers
, entry
, new_se
) {
506 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
507 QTAILQ_REMOVE(&savevm_state
.handlers
, se
, entry
);
517 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
518 const VMStateDescription
*vmsd
,
519 void *opaque
, int alias_id
,
520 int required_for_version
)
524 /* If this triggers, alias support can be dropped for the vmsd. */
525 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
527 se
= g_malloc0(sizeof(SaveStateEntry
));
528 se
->version_id
= vmsd
->version_id
;
529 se
->section_id
= savevm_state
.global_section_id
++;
532 se
->alias_id
= alias_id
;
535 char *id
= qdev_get_dev_path(dev
);
537 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
538 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
541 se
->compat
= g_malloc0(sizeof(CompatEntry
));
542 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
543 se
->compat
->instance_id
= instance_id
== -1 ?
544 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
548 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
550 if (instance_id
== -1) {
551 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
553 se
->instance_id
= instance_id
;
555 assert(!se
->compat
|| se
->instance_id
== 0);
556 /* add at the end of list */
557 QTAILQ_INSERT_TAIL(&savevm_state
.handlers
, se
, entry
);
561 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
564 SaveStateEntry
*se
, *new_se
;
566 QTAILQ_FOREACH_SAFE(se
, &savevm_state
.handlers
, entry
, new_se
) {
567 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
568 QTAILQ_REMOVE(&savevm_state
.handlers
, se
, entry
);
577 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
579 trace_vmstate_load(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
580 if (!se
->vmsd
) { /* Old style */
581 return se
->ops
->load_state(f
, se
->opaque
, version_id
);
583 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
586 static void vmstate_save_old_style(QEMUFile
*f
, SaveStateEntry
*se
, QJSON
*vmdesc
)
588 int64_t old_offset
, size
;
590 old_offset
= qemu_ftell_fast(f
);
591 se
->ops
->save_state(f
, se
->opaque
);
592 size
= qemu_ftell_fast(f
) - old_offset
;
595 json_prop_int(vmdesc
, "size", size
);
596 json_start_array(vmdesc
, "fields");
597 json_start_object(vmdesc
, NULL
);
598 json_prop_str(vmdesc
, "name", "data");
599 json_prop_int(vmdesc
, "size", size
);
600 json_prop_str(vmdesc
, "type", "buffer");
601 json_end_object(vmdesc
);
602 json_end_array(vmdesc
);
606 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
, QJSON
*vmdesc
)
608 trace_vmstate_save(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
610 vmstate_save_old_style(f
, se
, vmdesc
);
613 vmstate_save_state(f
, se
->vmsd
, se
->opaque
, vmdesc
);
616 void savevm_skip_section_footers(void)
618 skip_section_footers
= true;
622 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
624 static void save_section_header(QEMUFile
*f
, SaveStateEntry
*se
,
625 uint8_t section_type
)
627 qemu_put_byte(f
, section_type
);
628 qemu_put_be32(f
, se
->section_id
);
630 if (section_type
== QEMU_VM_SECTION_FULL
||
631 section_type
== QEMU_VM_SECTION_START
) {
633 size_t len
= strlen(se
->idstr
);
634 qemu_put_byte(f
, len
);
635 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
637 qemu_put_be32(f
, se
->instance_id
);
638 qemu_put_be32(f
, se
->version_id
);
643 * Write a footer onto device sections that catches cases misformatted device
646 static void save_section_footer(QEMUFile
*f
, SaveStateEntry
*se
)
648 if (!skip_section_footers
) {
649 qemu_put_byte(f
, QEMU_VM_SECTION_FOOTER
);
650 qemu_put_be32(f
, se
->section_id
);
655 * Read a footer off the wire and check that it matches the expected section
657 * Returns: true if the footer was good
658 * false if there is a problem (and calls error_report to say why)
660 static bool check_section_footer(QEMUFile
*f
, SaveStateEntry
*se
)
663 uint32_t read_section_id
;
665 if (skip_section_footers
) {
666 /* No footer to check */
670 read_mark
= qemu_get_byte(f
);
672 if (read_mark
!= QEMU_VM_SECTION_FOOTER
) {
673 error_report("Missing section footer for %s", se
->idstr
);
677 read_section_id
= qemu_get_be32(f
);
678 if (read_section_id
!= se
->section_id
) {
679 error_report("Mismatched section id in footer for %s -"
680 " read 0x%x expected 0x%x",
681 se
->idstr
, read_section_id
, se
->section_id
);
689 bool qemu_savevm_state_blocked(Error
**errp
)
693 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
694 if (se
->vmsd
&& se
->vmsd
->unmigratable
) {
695 error_setg(errp
, "State blocked by non-migratable device '%s'",
703 void qemu_savevm_state_header(QEMUFile
*f
)
705 trace_savevm_state_header();
706 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
707 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
710 void qemu_savevm_state_begin(QEMUFile
*f
,
711 const MigrationParams
*params
)
716 trace_savevm_state_begin();
717 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
718 if (!se
->ops
|| !se
->ops
->set_params
) {
721 se
->ops
->set_params(params
, se
->opaque
);
724 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
725 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
728 if (se
->ops
&& se
->ops
->is_active
) {
729 if (!se
->ops
->is_active(se
->opaque
)) {
733 save_section_header(f
, se
, QEMU_VM_SECTION_START
);
735 ret
= se
->ops
->save_live_setup(f
, se
->opaque
);
736 save_section_footer(f
, se
);
738 qemu_file_set_error(f
, ret
);
745 * this function has three return values:
746 * negative: there was one error, and we have -errno.
747 * 0 : We haven't finished, caller have to go again
748 * 1 : We have finished, we can go to complete phase
750 int qemu_savevm_state_iterate(QEMUFile
*f
)
755 trace_savevm_state_iterate();
756 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
757 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
760 if (se
->ops
&& se
->ops
->is_active
) {
761 if (!se
->ops
->is_active(se
->opaque
)) {
765 if (qemu_file_rate_limit(f
)) {
768 trace_savevm_section_start(se
->idstr
, se
->section_id
);
770 save_section_header(f
, se
, QEMU_VM_SECTION_PART
);
772 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
773 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
774 save_section_footer(f
, se
);
777 qemu_file_set_error(f
, ret
);
780 /* Do not proceed to the next vmstate before this one reported
781 completion of the current stage. This serializes the migration
782 and reduces the probability that a faster changing state is
783 synchronized over and over again. */
790 static bool should_send_vmdesc(void)
792 MachineState
*machine
= MACHINE(qdev_get_machine());
793 return !machine
->suppress_vmdesc
;
796 void qemu_savevm_state_complete(QEMUFile
*f
)
803 trace_savevm_state_complete();
805 cpu_synchronize_all_states();
807 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
808 if (!se
->ops
|| !se
->ops
->save_live_complete
) {
811 if (se
->ops
&& se
->ops
->is_active
) {
812 if (!se
->ops
->is_active(se
->opaque
)) {
816 trace_savevm_section_start(se
->idstr
, se
->section_id
);
818 save_section_header(f
, se
, QEMU_VM_SECTION_END
);
820 ret
= se
->ops
->save_live_complete(f
, se
->opaque
);
821 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
822 save_section_footer(f
, se
);
824 qemu_file_set_error(f
, ret
);
829 vmdesc
= qjson_new();
830 json_prop_int(vmdesc
, "page_size", TARGET_PAGE_SIZE
);
831 json_start_array(vmdesc
, "devices");
832 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
834 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
837 trace_savevm_section_start(se
->idstr
, se
->section_id
);
839 json_start_object(vmdesc
, NULL
);
840 json_prop_str(vmdesc
, "name", se
->idstr
);
841 json_prop_int(vmdesc
, "instance_id", se
->instance_id
);
843 save_section_header(f
, se
, QEMU_VM_SECTION_FULL
);
845 vmstate_save(f
, se
, vmdesc
);
847 json_end_object(vmdesc
);
848 trace_savevm_section_end(se
->idstr
, se
->section_id
, 0);
849 save_section_footer(f
, se
);
852 qemu_put_byte(f
, QEMU_VM_EOF
);
854 json_end_array(vmdesc
);
855 qjson_finish(vmdesc
);
856 vmdesc_len
= strlen(qjson_get_str(vmdesc
));
858 if (should_send_vmdesc()) {
859 qemu_put_byte(f
, QEMU_VM_VMDESCRIPTION
);
860 qemu_put_be32(f
, vmdesc_len
);
861 qemu_put_buffer(f
, (uint8_t *)qjson_get_str(vmdesc
), vmdesc_len
);
863 object_unref(OBJECT(vmdesc
));
868 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
873 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
874 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
877 if (se
->ops
&& se
->ops
->is_active
) {
878 if (!se
->ops
->is_active(se
->opaque
)) {
882 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
887 void qemu_savevm_state_cancel(void)
891 trace_savevm_state_cancel();
892 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
893 if (se
->ops
&& se
->ops
->cancel
) {
894 se
->ops
->cancel(se
->opaque
);
899 static int qemu_savevm_state(QEMUFile
*f
, Error
**errp
)
902 MigrationParams params
= {
907 if (qemu_savevm_state_blocked(errp
)) {
911 qemu_mutex_unlock_iothread();
912 qemu_savevm_state_header(f
);
913 qemu_savevm_state_begin(f
, ¶ms
);
914 qemu_mutex_lock_iothread();
916 while (qemu_file_get_error(f
) == 0) {
917 if (qemu_savevm_state_iterate(f
) > 0) {
922 ret
= qemu_file_get_error(f
);
924 qemu_savevm_state_complete(f
);
925 ret
= qemu_file_get_error(f
);
928 qemu_savevm_state_cancel();
929 error_setg_errno(errp
, -ret
, "Error while writing VM state");
934 static int qemu_save_device_state(QEMUFile
*f
)
938 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
939 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
941 cpu_synchronize_all_states();
943 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
947 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
951 save_section_header(f
, se
, QEMU_VM_SECTION_FULL
);
953 vmstate_save(f
, se
, NULL
);
955 save_section_footer(f
, se
);
958 qemu_put_byte(f
, QEMU_VM_EOF
);
960 return qemu_file_get_error(f
);
963 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
967 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
968 if (!strcmp(se
->idstr
, idstr
) &&
969 (instance_id
== se
->instance_id
||
970 instance_id
== se
->alias_id
))
972 /* Migrating from an older version? */
973 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
974 if (!strcmp(se
->compat
->idstr
, idstr
) &&
975 (instance_id
== se
->compat
->instance_id
||
976 instance_id
== se
->alias_id
))
983 struct LoadStateEntry
{
984 QLIST_ENTRY(LoadStateEntry
) entry
;
990 void loadvm_free_handlers(MigrationIncomingState
*mis
)
992 LoadStateEntry
*le
, *new_le
;
994 QLIST_FOREACH_SAFE(le
, &mis
->loadvm_handlers
, entry
, new_le
) {
995 QLIST_REMOVE(le
, entry
);
1000 int qemu_loadvm_state(QEMUFile
*f
)
1002 MigrationIncomingState
*mis
= migration_incoming_get_current();
1003 Error
*local_err
= NULL
;
1004 uint8_t section_type
;
1007 int file_error_after_eof
= -1;
1009 if (qemu_savevm_state_blocked(&local_err
)) {
1010 error_report_err(local_err
);
1014 v
= qemu_get_be32(f
);
1015 if (v
!= QEMU_VM_FILE_MAGIC
) {
1016 error_report("Not a migration stream");
1020 v
= qemu_get_be32(f
);
1021 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
1022 error_report("SaveVM v2 format is obsolete and don't work anymore");
1025 if (v
!= QEMU_VM_FILE_VERSION
) {
1026 error_report("Unsupported migration stream version");
1030 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
1031 uint32_t instance_id
, version_id
, section_id
;
1036 trace_qemu_loadvm_state_section(section_type
);
1037 switch (section_type
) {
1038 case QEMU_VM_SECTION_START
:
1039 case QEMU_VM_SECTION_FULL
:
1040 /* Read section start */
1041 section_id
= qemu_get_be32(f
);
1042 if (!qemu_get_counted_string(f
, idstr
)) {
1043 error_report("Unable to read ID string for section %u",
1047 instance_id
= qemu_get_be32(f
);
1048 version_id
= qemu_get_be32(f
);
1050 trace_qemu_loadvm_state_section_startfull(section_id
, idstr
,
1051 instance_id
, version_id
);
1052 /* Find savevm section */
1053 se
= find_se(idstr
, instance_id
);
1055 error_report("Unknown savevm section or instance '%s' %d",
1056 idstr
, instance_id
);
1061 /* Validate version */
1062 if (version_id
> se
->version_id
) {
1063 error_report("savevm: unsupported version %d for '%s' v%d",
1064 version_id
, idstr
, se
->version_id
);
1070 le
= g_malloc0(sizeof(*le
));
1073 le
->section_id
= section_id
;
1074 le
->version_id
= version_id
;
1075 QLIST_INSERT_HEAD(&mis
->loadvm_handlers
, le
, entry
);
1077 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1079 error_report("error while loading state for instance 0x%x of"
1080 " device '%s'", instance_id
, idstr
);
1083 if (!check_section_footer(f
, le
->se
)) {
1088 case QEMU_VM_SECTION_PART
:
1089 case QEMU_VM_SECTION_END
:
1090 section_id
= qemu_get_be32(f
);
1092 trace_qemu_loadvm_state_section_partend(section_id
);
1093 QLIST_FOREACH(le
, &mis
->loadvm_handlers
, entry
) {
1094 if (le
->section_id
== section_id
) {
1099 error_report("Unknown savevm section %d", section_id
);
1104 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1106 error_report("error while loading state section id %d(%s)",
1107 section_id
, le
->se
->idstr
);
1110 if (!check_section_footer(f
, le
->se
)) {
1116 error_report("Unknown savevm section type %d", section_type
);
1122 file_error_after_eof
= qemu_file_get_error(f
);
1125 * Try to read in the VMDESC section as well, so that dumping tools that
1126 * intercept our migration stream have the chance to see it.
1128 if (qemu_get_byte(f
) == QEMU_VM_VMDESCRIPTION
) {
1129 uint32_t size
= qemu_get_be32(f
);
1130 uint8_t *buf
= g_malloc(0x1000);
1133 uint32_t read_chunk
= MIN(size
, 0x1000);
1134 qemu_get_buffer(f
, buf
, read_chunk
);
1140 cpu_synchronize_all_post_init();
1146 /* We may not have a VMDESC section, so ignore relative errors */
1147 ret
= file_error_after_eof
;
1153 static BlockDriverState
*find_vmstate_bs(void)
1155 BlockDriverState
*bs
= NULL
;
1156 while ((bs
= bdrv_next(bs
))) {
1157 if (bdrv_can_snapshot(bs
)) {
1165 * Deletes snapshots of a given name in all opened images.
1167 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
1169 BlockDriverState
*bs
;
1170 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
1174 while ((bs
= bdrv_next(bs
))) {
1175 if (bdrv_can_snapshot(bs
) &&
1176 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0) {
1177 bdrv_snapshot_delete_by_id_or_name(bs
, name
, &err
);
1180 "Error while deleting snapshot on device '%s':"
1182 bdrv_get_device_name(bs
),
1183 error_get_pretty(err
));
1193 void hmp_savevm(Monitor
*mon
, const QDict
*qdict
)
1195 BlockDriverState
*bs
, *bs1
;
1196 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1199 int saved_vm_running
;
1200 uint64_t vm_state_size
;
1203 const char *name
= qdict_get_try_str(qdict
, "name");
1204 Error
*local_err
= NULL
;
1206 /* Verify if there is a device that doesn't support snapshots and is writable */
1208 while ((bs
= bdrv_next(bs
))) {
1210 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
1214 if (!bdrv_can_snapshot(bs
)) {
1215 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
1216 bdrv_get_device_name(bs
));
1221 bs
= find_vmstate_bs();
1223 monitor_printf(mon
, "No block device can accept snapshots\n");
1227 saved_vm_running
= runstate_is_running();
1228 vm_stop(RUN_STATE_SAVE_VM
);
1230 memset(sn
, 0, sizeof(*sn
));
1232 /* fill auxiliary fields */
1233 qemu_gettimeofday(&tv
);
1234 sn
->date_sec
= tv
.tv_sec
;
1235 sn
->date_nsec
= tv
.tv_usec
* 1000;
1236 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1239 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1241 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1242 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1244 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1247 /* cast below needed for OpenBSD where tv_sec is still 'long' */
1248 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
1249 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
1252 /* Delete old snapshots of the same name */
1253 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
1257 /* save the VM state */
1258 f
= qemu_fopen_bdrv(bs
, 1);
1260 monitor_printf(mon
, "Could not open VM state file\n");
1263 ret
= qemu_savevm_state(f
, &local_err
);
1264 vm_state_size
= qemu_ftell(f
);
1267 monitor_printf(mon
, "%s\n", error_get_pretty(local_err
));
1268 error_free(local_err
);
1272 /* create the snapshots */
1275 while ((bs1
= bdrv_next(bs1
))) {
1276 if (bdrv_can_snapshot(bs1
)) {
1277 /* Write VM state size only to the image that contains the state */
1278 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1279 ret
= bdrv_snapshot_create(bs1
, sn
);
1281 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
1282 bdrv_get_device_name(bs1
));
1288 if (saved_vm_running
) {
1293 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
1296 int saved_vm_running
;
1299 saved_vm_running
= runstate_is_running();
1300 vm_stop(RUN_STATE_SAVE_VM
);
1302 f
= qemu_fopen(filename
, "wb");
1304 error_setg_file_open(errp
, errno
, filename
);
1307 ret
= qemu_save_device_state(f
);
1310 error_set(errp
, QERR_IO_ERROR
);
1314 if (saved_vm_running
) {
1319 int load_vmstate(const char *name
)
1321 BlockDriverState
*bs
, *bs_vm_state
;
1322 QEMUSnapshotInfo sn
;
1326 bs_vm_state
= find_vmstate_bs();
1328 error_report("No block device supports snapshots");
1332 /* Don't even try to load empty VM states */
1333 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
1336 } else if (sn
.vm_state_size
== 0) {
1337 error_report("This is a disk-only snapshot. Revert to it offline "
1342 /* Verify if there is any device that doesn't support snapshots and is
1343 writable and check if the requested snapshot is available too. */
1345 while ((bs
= bdrv_next(bs
))) {
1347 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
1351 if (!bdrv_can_snapshot(bs
)) {
1352 error_report("Device '%s' is writable but does not support snapshots.",
1353 bdrv_get_device_name(bs
));
1357 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1359 error_report("Device '%s' does not have the requested snapshot '%s'",
1360 bdrv_get_device_name(bs
), name
);
1365 /* Flush all IO requests so they don't interfere with the new state. */
1369 while ((bs
= bdrv_next(bs
))) {
1370 if (bdrv_can_snapshot(bs
)) {
1371 ret
= bdrv_snapshot_goto(bs
, name
);
1373 error_report("Error %d while activating snapshot '%s' on '%s'",
1374 ret
, name
, bdrv_get_device_name(bs
));
1380 /* restore the VM state */
1381 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
1383 error_report("Could not open VM state file");
1387 qemu_system_reset(VMRESET_SILENT
);
1388 migration_incoming_state_new(f
);
1389 ret
= qemu_loadvm_state(f
);
1392 migration_incoming_state_destroy();
1394 error_report("Error %d while loading VM state", ret
);
1401 void hmp_delvm(Monitor
*mon
, const QDict
*qdict
)
1403 BlockDriverState
*bs
;
1405 const char *name
= qdict_get_str(qdict
, "name");
1407 if (!find_vmstate_bs()) {
1408 monitor_printf(mon
, "No block device supports snapshots\n");
1413 while ((bs
= bdrv_next(bs
))) {
1414 if (bdrv_can_snapshot(bs
)) {
1416 bdrv_snapshot_delete_by_id_or_name(bs
, name
, &err
);
1419 "Error while deleting snapshot on device '%s':"
1421 bdrv_get_device_name(bs
),
1422 error_get_pretty(err
));
1429 void hmp_info_snapshots(Monitor
*mon
, const QDict
*qdict
)
1431 BlockDriverState
*bs
, *bs1
;
1432 QEMUSnapshotInfo
*sn_tab
, *sn
, s
, *sn_info
= &s
;
1433 int nb_sns
, i
, ret
, available
;
1435 int *available_snapshots
;
1437 bs
= find_vmstate_bs();
1439 monitor_printf(mon
, "No available block device supports snapshots\n");
1443 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1445 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
1450 monitor_printf(mon
, "There is no snapshot available.\n");
1454 available_snapshots
= g_malloc0(sizeof(int) * nb_sns
);
1456 for (i
= 0; i
< nb_sns
; i
++) {
1461 while ((bs1
= bdrv_next(bs1
))) {
1462 if (bdrv_can_snapshot(bs1
) && bs1
!= bs
) {
1463 ret
= bdrv_snapshot_find(bs1
, sn_info
, sn
->id_str
);
1472 available_snapshots
[total
] = i
;
1478 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, NULL
);
1479 monitor_printf(mon
, "\n");
1480 for (i
= 0; i
< total
; i
++) {
1481 sn
= &sn_tab
[available_snapshots
[i
]];
1482 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, sn
);
1483 monitor_printf(mon
, "\n");
1486 monitor_printf(mon
, "There is no suitable snapshot available\n");
1490 g_free(available_snapshots
);
1494 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
1496 qemu_ram_set_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
,
1497 memory_region_name(mr
), dev
);
1500 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
1502 qemu_ram_unset_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
);
1505 void vmstate_register_ram_global(MemoryRegion
*mr
)
1507 vmstate_register_ram(mr
, NULL
);