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/error-report.h"
41 #include "qemu/sockets.h"
42 #include "qemu/queue.h"
43 #include "sysemu/cpus.h"
44 #include "exec/memory.h"
45 #include "qmp-commands.h"
48 #include "block/snapshot.h"
49 #include "block/qapi.h"
53 #define ETH_P_RARP 0x8035
55 #define ARP_HTYPE_ETH 0x0001
56 #define ARP_PTYPE_IP 0x0800
57 #define ARP_OP_REQUEST_REV 0x3
59 static bool skip_section_footers
;
61 static int announce_self_create(uint8_t *buf
,
64 /* Ethernet header. */
65 memset(buf
, 0xff, 6); /* destination MAC addr */
66 memcpy(buf
+ 6, mac_addr
, 6); /* source MAC addr */
67 *(uint16_t *)(buf
+ 12) = htons(ETH_P_RARP
); /* ethertype */
70 *(uint16_t *)(buf
+ 14) = htons(ARP_HTYPE_ETH
); /* hardware addr space */
71 *(uint16_t *)(buf
+ 16) = htons(ARP_PTYPE_IP
); /* protocol addr space */
72 *(buf
+ 18) = 6; /* hardware addr length (ethernet) */
73 *(buf
+ 19) = 4; /* protocol addr length (IPv4) */
74 *(uint16_t *)(buf
+ 20) = htons(ARP_OP_REQUEST_REV
); /* opcode */
75 memcpy(buf
+ 22, mac_addr
, 6); /* source hw addr */
76 memset(buf
+ 28, 0x00, 4); /* source protocol addr */
77 memcpy(buf
+ 32, mac_addr
, 6); /* target hw addr */
78 memset(buf
+ 38, 0x00, 4); /* target protocol addr */
80 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
81 memset(buf
+ 42, 0x00, 18);
83 return 60; /* len (FCS will be added by hardware) */
86 static void qemu_announce_self_iter(NICState
*nic
, void *opaque
)
91 trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic
->conf
->macaddr
));
92 len
= announce_self_create(buf
, nic
->conf
->macaddr
.a
);
94 qemu_send_packet_raw(qemu_get_queue(nic
), buf
, len
);
98 static void qemu_announce_self_once(void *opaque
)
100 static int count
= SELF_ANNOUNCE_ROUNDS
;
101 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
103 qemu_foreach_nic(qemu_announce_self_iter
, NULL
);
106 /* delay 50ms, 150ms, 250ms, ... */
107 timer_mod(timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) +
108 self_announce_delay(count
));
115 void qemu_announce_self(void)
117 static QEMUTimer
*timer
;
118 timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, qemu_announce_self_once
, &timer
);
119 qemu_announce_self_once(&timer
);
122 /***********************************************************/
123 /* savevm/loadvm support */
125 static ssize_t
block_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
131 qemu_iovec_init_external(&qiov
, iov
, iovcnt
);
132 ret
= bdrv_writev_vmstate(opaque
, &qiov
, pos
);
140 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
141 int64_t pos
, int size
)
143 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
147 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
149 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
152 static int bdrv_fclose(void *opaque
)
154 return bdrv_flush(opaque
);
157 static const QEMUFileOps bdrv_read_ops
= {
158 .get_buffer
= block_get_buffer
,
162 static const QEMUFileOps bdrv_write_ops
= {
163 .put_buffer
= block_put_buffer
,
164 .writev_buffer
= block_writev_buffer
,
168 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
171 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
173 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
177 /* QEMUFile timer support.
178 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
181 void timer_put(QEMUFile
*f
, QEMUTimer
*ts
)
183 uint64_t expire_time
;
185 expire_time
= timer_expire_time_ns(ts
);
186 qemu_put_be64(f
, expire_time
);
189 void timer_get(QEMUFile
*f
, QEMUTimer
*ts
)
191 uint64_t expire_time
;
193 expire_time
= qemu_get_be64(f
);
194 if (expire_time
!= -1) {
195 timer_mod_ns(ts
, expire_time
);
202 /* VMState timer support.
203 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
206 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
213 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
219 const VMStateInfo vmstate_info_timer
= {
226 typedef struct CompatEntry
{
231 typedef struct SaveStateEntry
{
232 QTAILQ_ENTRY(SaveStateEntry
) entry
;
239 const VMStateDescription
*vmsd
;
245 typedef struct SaveState
{
246 QTAILQ_HEAD(, SaveStateEntry
) handlers
;
247 int global_section_id
;
250 static SaveState savevm_state
= {
251 .handlers
= QTAILQ_HEAD_INITIALIZER(savevm_state
.handlers
),
252 .global_section_id
= 0,
255 static void dump_vmstate_vmsd(FILE *out_file
,
256 const VMStateDescription
*vmsd
, int indent
,
259 static void dump_vmstate_vmsf(FILE *out_file
, const VMStateField
*field
,
262 fprintf(out_file
, "%*s{\n", indent
, "");
264 fprintf(out_file
, "%*s\"field\": \"%s\",\n", indent
, "", field
->name
);
265 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
267 fprintf(out_file
, "%*s\"field_exists\": %s,\n", indent
, "",
268 field
->field_exists
? "true" : "false");
269 fprintf(out_file
, "%*s\"size\": %zu", indent
, "", field
->size
);
270 if (field
->vmsd
!= NULL
) {
271 fprintf(out_file
, ",\n");
272 dump_vmstate_vmsd(out_file
, field
->vmsd
, indent
, false);
274 fprintf(out_file
, "\n%*s}", indent
- 2, "");
277 static void dump_vmstate_vmss(FILE *out_file
,
278 const VMStateDescription
**subsection
,
281 if (*subsection
!= NULL
) {
282 dump_vmstate_vmsd(out_file
, *subsection
, indent
, true);
286 static void dump_vmstate_vmsd(FILE *out_file
,
287 const VMStateDescription
*vmsd
, int indent
,
291 fprintf(out_file
, "%*s{\n", indent
, "");
293 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", "Description");
296 fprintf(out_file
, "%*s\"name\": \"%s\",\n", indent
, "", vmsd
->name
);
297 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
299 fprintf(out_file
, "%*s\"minimum_version_id\": %d", indent
, "",
300 vmsd
->minimum_version_id
);
301 if (vmsd
->fields
!= NULL
) {
302 const VMStateField
*field
= vmsd
->fields
;
305 fprintf(out_file
, ",\n%*s\"Fields\": [\n", indent
, "");
307 while (field
->name
!= NULL
) {
308 if (field
->flags
& VMS_MUST_EXIST
) {
309 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
314 fprintf(out_file
, ",\n");
316 dump_vmstate_vmsf(out_file
, field
, indent
+ 2);
320 fprintf(out_file
, "\n%*s]", indent
, "");
322 if (vmsd
->subsections
!= NULL
) {
323 const VMStateDescription
**subsection
= vmsd
->subsections
;
326 fprintf(out_file
, ",\n%*s\"Subsections\": [\n", indent
, "");
328 while (*subsection
!= NULL
) {
330 fprintf(out_file
, ",\n");
332 dump_vmstate_vmss(out_file
, subsection
, indent
+ 2);
336 fprintf(out_file
, "\n%*s]", indent
, "");
338 fprintf(out_file
, "\n%*s}", indent
- 2, "");
341 static void dump_machine_type(FILE *out_file
)
345 mc
= MACHINE_GET_CLASS(current_machine
);
347 fprintf(out_file
, " \"vmschkmachine\": {\n");
348 fprintf(out_file
, " \"Name\": \"%s\"\n", mc
->name
);
349 fprintf(out_file
, " },\n");
352 void dump_vmstate_json_to_file(FILE *out_file
)
357 fprintf(out_file
, "{\n");
358 dump_machine_type(out_file
);
361 list
= object_class_get_list(TYPE_DEVICE
, true);
362 for (elt
= list
; elt
; elt
= elt
->next
) {
363 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
373 fprintf(out_file
, ",\n");
375 name
= object_class_get_name(OBJECT_CLASS(dc
));
376 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", name
);
378 fprintf(out_file
, "%*s\"Name\": \"%s\",\n", indent
, "", name
);
379 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
380 dc
->vmsd
->version_id
);
381 fprintf(out_file
, "%*s\"minimum_version_id\": %d,\n", indent
, "",
382 dc
->vmsd
->minimum_version_id
);
384 dump_vmstate_vmsd(out_file
, dc
->vmsd
, indent
, false);
386 fprintf(out_file
, "\n%*s}", indent
- 2, "");
389 fprintf(out_file
, "\n}\n");
393 static int calculate_new_instance_id(const char *idstr
)
398 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
399 if (strcmp(idstr
, se
->idstr
) == 0
400 && instance_id
<= se
->instance_id
) {
401 instance_id
= se
->instance_id
+ 1;
407 static int calculate_compat_instance_id(const char *idstr
)
412 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
417 if (strcmp(idstr
, se
->compat
->idstr
) == 0
418 && instance_id
<= se
->compat
->instance_id
) {
419 instance_id
= se
->compat
->instance_id
+ 1;
425 /* TODO: Individual devices generally have very little idea about the rest
426 of the system, so instance_id should be removed/replaced.
427 Meanwhile pass -1 as instance_id if you do not already have a clearly
428 distinguishing id for all instances of your device class. */
429 int register_savevm_live(DeviceState
*dev
,
438 se
= g_malloc0(sizeof(SaveStateEntry
));
439 se
->version_id
= version_id
;
440 se
->section_id
= savevm_state
.global_section_id
++;
444 /* if this is a live_savem then set is_ram */
445 if (ops
->save_live_setup
!= NULL
) {
450 char *id
= qdev_get_dev_path(dev
);
452 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
453 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
456 se
->compat
= g_malloc0(sizeof(CompatEntry
));
457 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
458 se
->compat
->instance_id
= instance_id
== -1 ?
459 calculate_compat_instance_id(idstr
) : instance_id
;
463 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
465 if (instance_id
== -1) {
466 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
468 se
->instance_id
= instance_id
;
470 assert(!se
->compat
|| se
->instance_id
== 0);
471 /* add at the end of list */
472 QTAILQ_INSERT_TAIL(&savevm_state
.handlers
, se
, entry
);
476 int register_savevm(DeviceState
*dev
,
480 SaveStateHandler
*save_state
,
481 LoadStateHandler
*load_state
,
484 SaveVMHandlers
*ops
= g_malloc0(sizeof(SaveVMHandlers
));
485 ops
->save_state
= save_state
;
486 ops
->load_state
= load_state
;
487 return register_savevm_live(dev
, idstr
, instance_id
, version_id
,
491 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
493 SaveStateEntry
*se
, *new_se
;
497 char *path
= qdev_get_dev_path(dev
);
499 pstrcpy(id
, sizeof(id
), path
);
500 pstrcat(id
, sizeof(id
), "/");
504 pstrcat(id
, sizeof(id
), idstr
);
506 QTAILQ_FOREACH_SAFE(se
, &savevm_state
.handlers
, entry
, new_se
) {
507 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
508 QTAILQ_REMOVE(&savevm_state
.handlers
, se
, entry
);
518 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
519 const VMStateDescription
*vmsd
,
520 void *opaque
, int alias_id
,
521 int required_for_version
)
525 /* If this triggers, alias support can be dropped for the vmsd. */
526 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
528 se
= g_malloc0(sizeof(SaveStateEntry
));
529 se
->version_id
= vmsd
->version_id
;
530 se
->section_id
= savevm_state
.global_section_id
++;
533 se
->alias_id
= alias_id
;
536 char *id
= qdev_get_dev_path(dev
);
538 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
539 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
542 se
->compat
= g_malloc0(sizeof(CompatEntry
));
543 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
544 se
->compat
->instance_id
= instance_id
== -1 ?
545 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
549 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
551 if (instance_id
== -1) {
552 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
554 se
->instance_id
= instance_id
;
556 assert(!se
->compat
|| se
->instance_id
== 0);
557 /* add at the end of list */
558 QTAILQ_INSERT_TAIL(&savevm_state
.handlers
, se
, entry
);
562 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
565 SaveStateEntry
*se
, *new_se
;
567 QTAILQ_FOREACH_SAFE(se
, &savevm_state
.handlers
, entry
, new_se
) {
568 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
569 QTAILQ_REMOVE(&savevm_state
.handlers
, se
, entry
);
578 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
580 trace_vmstate_load(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
581 if (!se
->vmsd
) { /* Old style */
582 return se
->ops
->load_state(f
, se
->opaque
, version_id
);
584 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
587 static void vmstate_save_old_style(QEMUFile
*f
, SaveStateEntry
*se
, QJSON
*vmdesc
)
589 int64_t old_offset
, size
;
591 old_offset
= qemu_ftell_fast(f
);
592 se
->ops
->save_state(f
, se
->opaque
);
593 size
= qemu_ftell_fast(f
) - old_offset
;
596 json_prop_int(vmdesc
, "size", size
);
597 json_start_array(vmdesc
, "fields");
598 json_start_object(vmdesc
, NULL
);
599 json_prop_str(vmdesc
, "name", "data");
600 json_prop_int(vmdesc
, "size", size
);
601 json_prop_str(vmdesc
, "type", "buffer");
602 json_end_object(vmdesc
);
603 json_end_array(vmdesc
);
607 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
, QJSON
*vmdesc
)
609 trace_vmstate_save(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
611 vmstate_save_old_style(f
, se
, vmdesc
);
614 vmstate_save_state(f
, se
->vmsd
, se
->opaque
, vmdesc
);
617 void savevm_skip_section_footers(void)
619 skip_section_footers
= true;
623 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
625 static void save_section_header(QEMUFile
*f
, SaveStateEntry
*se
,
626 uint8_t section_type
)
628 qemu_put_byte(f
, section_type
);
629 qemu_put_be32(f
, se
->section_id
);
631 if (section_type
== QEMU_VM_SECTION_FULL
||
632 section_type
== QEMU_VM_SECTION_START
) {
634 size_t len
= strlen(se
->idstr
);
635 qemu_put_byte(f
, len
);
636 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
638 qemu_put_be32(f
, se
->instance_id
);
639 qemu_put_be32(f
, se
->version_id
);
644 * Write a footer onto device sections that catches cases misformatted device
647 static void save_section_footer(QEMUFile
*f
, SaveStateEntry
*se
)
649 if (!skip_section_footers
) {
650 qemu_put_byte(f
, QEMU_VM_SECTION_FOOTER
);
651 qemu_put_be32(f
, se
->section_id
);
656 * Read a footer off the wire and check that it matches the expected section
658 * Returns: true if the footer was good
659 * false if there is a problem (and calls error_report to say why)
661 static bool check_section_footer(QEMUFile
*f
, SaveStateEntry
*se
)
664 uint32_t read_section_id
;
666 if (skip_section_footers
) {
667 /* No footer to check */
671 read_mark
= qemu_get_byte(f
);
673 if (read_mark
!= QEMU_VM_SECTION_FOOTER
) {
674 error_report("Missing section footer for %s", se
->idstr
);
678 read_section_id
= qemu_get_be32(f
);
679 if (read_section_id
!= se
->section_id
) {
680 error_report("Mismatched section id in footer for %s -"
681 " read 0x%x expected 0x%x",
682 se
->idstr
, read_section_id
, se
->section_id
);
690 bool qemu_savevm_state_blocked(Error
**errp
)
694 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
695 if (se
->vmsd
&& se
->vmsd
->unmigratable
) {
696 error_setg(errp
, "State blocked by non-migratable device '%s'",
704 void qemu_savevm_state_header(QEMUFile
*f
)
706 trace_savevm_state_header();
707 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
708 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
711 void qemu_savevm_state_begin(QEMUFile
*f
,
712 const MigrationParams
*params
)
717 trace_savevm_state_begin();
718 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
719 if (!se
->ops
|| !se
->ops
->set_params
) {
722 se
->ops
->set_params(params
, se
->opaque
);
725 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
726 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
729 if (se
->ops
&& se
->ops
->is_active
) {
730 if (!se
->ops
->is_active(se
->opaque
)) {
734 save_section_header(f
, se
, QEMU_VM_SECTION_START
);
736 ret
= se
->ops
->save_live_setup(f
, se
->opaque
);
737 save_section_footer(f
, se
);
739 qemu_file_set_error(f
, ret
);
746 * this function has three return values:
747 * negative: there was one error, and we have -errno.
748 * 0 : We haven't finished, caller have to go again
749 * 1 : We have finished, we can go to complete phase
751 int qemu_savevm_state_iterate(QEMUFile
*f
)
756 trace_savevm_state_iterate();
757 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
758 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
761 if (se
->ops
&& se
->ops
->is_active
) {
762 if (!se
->ops
->is_active(se
->opaque
)) {
766 if (qemu_file_rate_limit(f
)) {
769 trace_savevm_section_start(se
->idstr
, se
->section_id
);
771 save_section_header(f
, se
, QEMU_VM_SECTION_PART
);
773 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
774 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
775 save_section_footer(f
, se
);
778 qemu_file_set_error(f
, ret
);
781 /* Do not proceed to the next vmstate before this one reported
782 completion of the current stage. This serializes the migration
783 and reduces the probability that a faster changing state is
784 synchronized over and over again. */
791 static bool should_send_vmdesc(void)
793 MachineState
*machine
= MACHINE(qdev_get_machine());
794 return !machine
->suppress_vmdesc
;
797 void qemu_savevm_state_complete(QEMUFile
*f
)
804 trace_savevm_state_complete();
806 cpu_synchronize_all_states();
808 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
809 if (!se
->ops
|| !se
->ops
->save_live_complete
) {
812 if (se
->ops
&& se
->ops
->is_active
) {
813 if (!se
->ops
->is_active(se
->opaque
)) {
817 trace_savevm_section_start(se
->idstr
, se
->section_id
);
819 save_section_header(f
, se
, QEMU_VM_SECTION_END
);
821 ret
= se
->ops
->save_live_complete(f
, se
->opaque
);
822 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
823 save_section_footer(f
, se
);
825 qemu_file_set_error(f
, ret
);
830 vmdesc
= qjson_new();
831 json_prop_int(vmdesc
, "page_size", TARGET_PAGE_SIZE
);
832 json_start_array(vmdesc
, "devices");
833 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
835 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
838 trace_savevm_section_start(se
->idstr
, se
->section_id
);
840 json_start_object(vmdesc
, NULL
);
841 json_prop_str(vmdesc
, "name", se
->idstr
);
842 json_prop_int(vmdesc
, "instance_id", se
->instance_id
);
844 save_section_header(f
, se
, QEMU_VM_SECTION_FULL
);
846 vmstate_save(f
, se
, vmdesc
);
848 json_end_object(vmdesc
);
849 trace_savevm_section_end(se
->idstr
, se
->section_id
, 0);
850 save_section_footer(f
, se
);
853 qemu_put_byte(f
, QEMU_VM_EOF
);
855 json_end_array(vmdesc
);
856 qjson_finish(vmdesc
);
857 vmdesc_len
= strlen(qjson_get_str(vmdesc
));
859 if (should_send_vmdesc()) {
860 qemu_put_byte(f
, QEMU_VM_VMDESCRIPTION
);
861 qemu_put_be32(f
, vmdesc_len
);
862 qemu_put_buffer(f
, (uint8_t *)qjson_get_str(vmdesc
), vmdesc_len
);
864 object_unref(OBJECT(vmdesc
));
869 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
874 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
875 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
878 if (se
->ops
&& se
->ops
->is_active
) {
879 if (!se
->ops
->is_active(se
->opaque
)) {
883 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
888 void qemu_savevm_state_cancel(void)
892 trace_savevm_state_cancel();
893 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
894 if (se
->ops
&& se
->ops
->cancel
) {
895 se
->ops
->cancel(se
->opaque
);
900 static int qemu_savevm_state(QEMUFile
*f
, Error
**errp
)
903 MigrationParams params
= {
908 if (qemu_savevm_state_blocked(errp
)) {
912 qemu_mutex_unlock_iothread();
913 qemu_savevm_state_header(f
);
914 qemu_savevm_state_begin(f
, ¶ms
);
915 qemu_mutex_lock_iothread();
917 while (qemu_file_get_error(f
) == 0) {
918 if (qemu_savevm_state_iterate(f
) > 0) {
923 ret
= qemu_file_get_error(f
);
925 qemu_savevm_state_complete(f
);
926 ret
= qemu_file_get_error(f
);
929 qemu_savevm_state_cancel();
930 error_setg_errno(errp
, -ret
, "Error while writing VM state");
935 static int qemu_save_device_state(QEMUFile
*f
)
939 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
940 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
942 cpu_synchronize_all_states();
944 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
948 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
952 save_section_header(f
, se
, QEMU_VM_SECTION_FULL
);
954 vmstate_save(f
, se
, NULL
);
956 save_section_footer(f
, se
);
959 qemu_put_byte(f
, QEMU_VM_EOF
);
961 return qemu_file_get_error(f
);
964 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
968 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
969 if (!strcmp(se
->idstr
, idstr
) &&
970 (instance_id
== se
->instance_id
||
971 instance_id
== se
->alias_id
))
973 /* Migrating from an older version? */
974 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
975 if (!strcmp(se
->compat
->idstr
, idstr
) &&
976 (instance_id
== se
->compat
->instance_id
||
977 instance_id
== se
->alias_id
))
984 struct LoadStateEntry
{
985 QLIST_ENTRY(LoadStateEntry
) entry
;
991 void loadvm_free_handlers(MigrationIncomingState
*mis
)
993 LoadStateEntry
*le
, *new_le
;
995 QLIST_FOREACH_SAFE(le
, &mis
->loadvm_handlers
, entry
, new_le
) {
996 QLIST_REMOVE(le
, entry
);
1001 int qemu_loadvm_state(QEMUFile
*f
)
1003 MigrationIncomingState
*mis
= migration_incoming_get_current();
1004 Error
*local_err
= NULL
;
1005 uint8_t section_type
;
1008 int file_error_after_eof
= -1;
1010 if (qemu_savevm_state_blocked(&local_err
)) {
1011 error_report_err(local_err
);
1015 v
= qemu_get_be32(f
);
1016 if (v
!= QEMU_VM_FILE_MAGIC
) {
1017 error_report("Not a migration stream");
1021 v
= qemu_get_be32(f
);
1022 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
1023 error_report("SaveVM v2 format is obsolete and don't work anymore");
1026 if (v
!= QEMU_VM_FILE_VERSION
) {
1027 error_report("Unsupported migration stream version");
1031 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
1032 uint32_t instance_id
, version_id
, section_id
;
1037 trace_qemu_loadvm_state_section(section_type
);
1038 switch (section_type
) {
1039 case QEMU_VM_SECTION_START
:
1040 case QEMU_VM_SECTION_FULL
:
1041 /* Read section start */
1042 section_id
= qemu_get_be32(f
);
1043 if (!qemu_get_counted_string(f
, idstr
)) {
1044 error_report("Unable to read ID string for section %u",
1048 instance_id
= qemu_get_be32(f
);
1049 version_id
= qemu_get_be32(f
);
1051 trace_qemu_loadvm_state_section_startfull(section_id
, idstr
,
1052 instance_id
, version_id
);
1053 /* Find savevm section */
1054 se
= find_se(idstr
, instance_id
);
1056 error_report("Unknown savevm section or instance '%s' %d",
1057 idstr
, instance_id
);
1062 /* Validate version */
1063 if (version_id
> se
->version_id
) {
1064 error_report("savevm: unsupported version %d for '%s' v%d",
1065 version_id
, idstr
, se
->version_id
);
1071 le
= g_malloc0(sizeof(*le
));
1074 le
->section_id
= section_id
;
1075 le
->version_id
= version_id
;
1076 QLIST_INSERT_HEAD(&mis
->loadvm_handlers
, le
, entry
);
1078 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1080 error_report("error while loading state for instance 0x%x of"
1081 " device '%s'", instance_id
, idstr
);
1084 if (!check_section_footer(f
, le
->se
)) {
1089 case QEMU_VM_SECTION_PART
:
1090 case QEMU_VM_SECTION_END
:
1091 section_id
= qemu_get_be32(f
);
1093 trace_qemu_loadvm_state_section_partend(section_id
);
1094 QLIST_FOREACH(le
, &mis
->loadvm_handlers
, entry
) {
1095 if (le
->section_id
== section_id
) {
1100 error_report("Unknown savevm section %d", section_id
);
1105 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1107 error_report("error while loading state section id %d(%s)",
1108 section_id
, le
->se
->idstr
);
1111 if (!check_section_footer(f
, le
->se
)) {
1117 error_report("Unknown savevm section type %d", section_type
);
1123 file_error_after_eof
= qemu_file_get_error(f
);
1126 * Try to read in the VMDESC section as well, so that dumping tools that
1127 * intercept our migration stream have the chance to see it.
1129 if (qemu_get_byte(f
) == QEMU_VM_VMDESCRIPTION
) {
1130 uint32_t size
= qemu_get_be32(f
);
1131 uint8_t *buf
= g_malloc(0x1000);
1134 uint32_t read_chunk
= MIN(size
, 0x1000);
1135 qemu_get_buffer(f
, buf
, read_chunk
);
1141 cpu_synchronize_all_post_init();
1147 /* We may not have a VMDESC section, so ignore relative errors */
1148 ret
= file_error_after_eof
;
1154 static BlockDriverState
*find_vmstate_bs(void)
1156 BlockDriverState
*bs
= NULL
;
1157 while ((bs
= bdrv_next(bs
))) {
1158 if (bdrv_can_snapshot(bs
)) {
1166 * Deletes snapshots of a given name in all opened images.
1168 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
1170 BlockDriverState
*bs
;
1171 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
1175 while ((bs
= bdrv_next(bs
))) {
1176 if (bdrv_can_snapshot(bs
) &&
1177 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0) {
1178 bdrv_snapshot_delete_by_id_or_name(bs
, name
, &err
);
1181 "Error while deleting snapshot on device '%s':"
1183 bdrv_get_device_name(bs
),
1184 error_get_pretty(err
));
1194 void hmp_savevm(Monitor
*mon
, const QDict
*qdict
)
1196 BlockDriverState
*bs
, *bs1
;
1197 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1200 int saved_vm_running
;
1201 uint64_t vm_state_size
;
1204 const char *name
= qdict_get_try_str(qdict
, "name");
1205 Error
*local_err
= NULL
;
1207 /* Verify if there is a device that doesn't support snapshots and is writable */
1209 while ((bs
= bdrv_next(bs
))) {
1211 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
1215 if (!bdrv_can_snapshot(bs
)) {
1216 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
1217 bdrv_get_device_name(bs
));
1222 bs
= find_vmstate_bs();
1224 monitor_printf(mon
, "No block device can accept snapshots\n");
1228 saved_vm_running
= runstate_is_running();
1229 vm_stop(RUN_STATE_SAVE_VM
);
1231 memset(sn
, 0, sizeof(*sn
));
1233 /* fill auxiliary fields */
1234 qemu_gettimeofday(&tv
);
1235 sn
->date_sec
= tv
.tv_sec
;
1236 sn
->date_nsec
= tv
.tv_usec
* 1000;
1237 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1240 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1242 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1243 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1245 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1248 /* cast below needed for OpenBSD where tv_sec is still 'long' */
1249 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
1250 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
1253 /* Delete old snapshots of the same name */
1254 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
1258 /* save the VM state */
1259 f
= qemu_fopen_bdrv(bs
, 1);
1261 monitor_printf(mon
, "Could not open VM state file\n");
1264 ret
= qemu_savevm_state(f
, &local_err
);
1265 vm_state_size
= qemu_ftell(f
);
1268 monitor_printf(mon
, "%s\n", error_get_pretty(local_err
));
1269 error_free(local_err
);
1273 /* create the snapshots */
1276 while ((bs1
= bdrv_next(bs1
))) {
1277 if (bdrv_can_snapshot(bs1
)) {
1278 /* Write VM state size only to the image that contains the state */
1279 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1280 ret
= bdrv_snapshot_create(bs1
, sn
);
1282 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
1283 bdrv_get_device_name(bs1
));
1289 if (saved_vm_running
) {
1294 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
1297 int saved_vm_running
;
1300 saved_vm_running
= runstate_is_running();
1301 vm_stop(RUN_STATE_SAVE_VM
);
1303 f
= qemu_fopen(filename
, "wb");
1305 error_setg_file_open(errp
, errno
, filename
);
1308 ret
= qemu_save_device_state(f
);
1311 error_setg(errp
, QERR_IO_ERROR
);
1315 if (saved_vm_running
) {
1320 int load_vmstate(const char *name
)
1322 BlockDriverState
*bs
, *bs_vm_state
;
1323 QEMUSnapshotInfo sn
;
1327 bs_vm_state
= find_vmstate_bs();
1329 error_report("No block device supports snapshots");
1333 /* Don't even try to load empty VM states */
1334 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
1337 } else if (sn
.vm_state_size
== 0) {
1338 error_report("This is a disk-only snapshot. Revert to it offline "
1343 /* Verify if there is any device that doesn't support snapshots and is
1344 writable and check if the requested snapshot is available too. */
1346 while ((bs
= bdrv_next(bs
))) {
1348 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
1352 if (!bdrv_can_snapshot(bs
)) {
1353 error_report("Device '%s' is writable but does not support snapshots.",
1354 bdrv_get_device_name(bs
));
1358 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1360 error_report("Device '%s' does not have the requested snapshot '%s'",
1361 bdrv_get_device_name(bs
), name
);
1366 /* Flush all IO requests so they don't interfere with the new state. */
1370 while ((bs
= bdrv_next(bs
))) {
1371 if (bdrv_can_snapshot(bs
)) {
1372 ret
= bdrv_snapshot_goto(bs
, name
);
1374 error_report("Error %d while activating snapshot '%s' on '%s'",
1375 ret
, name
, bdrv_get_device_name(bs
));
1381 /* restore the VM state */
1382 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
1384 error_report("Could not open VM state file");
1388 qemu_system_reset(VMRESET_SILENT
);
1389 migration_incoming_state_new(f
);
1390 ret
= qemu_loadvm_state(f
);
1393 migration_incoming_state_destroy();
1395 error_report("Error %d while loading VM state", ret
);
1402 void hmp_delvm(Monitor
*mon
, const QDict
*qdict
)
1404 BlockDriverState
*bs
;
1406 const char *name
= qdict_get_str(qdict
, "name");
1408 if (!find_vmstate_bs()) {
1409 monitor_printf(mon
, "No block device supports snapshots\n");
1414 while ((bs
= bdrv_next(bs
))) {
1415 if (bdrv_can_snapshot(bs
)) {
1417 bdrv_snapshot_delete_by_id_or_name(bs
, name
, &err
);
1420 "Error while deleting snapshot on device '%s':"
1422 bdrv_get_device_name(bs
),
1423 error_get_pretty(err
));
1430 void hmp_info_snapshots(Monitor
*mon
, const QDict
*qdict
)
1432 BlockDriverState
*bs
, *bs1
;
1433 QEMUSnapshotInfo
*sn_tab
, *sn
, s
, *sn_info
= &s
;
1434 int nb_sns
, i
, ret
, available
;
1436 int *available_snapshots
;
1438 bs
= find_vmstate_bs();
1440 monitor_printf(mon
, "No available block device supports snapshots\n");
1444 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1446 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
1451 monitor_printf(mon
, "There is no snapshot available.\n");
1455 available_snapshots
= g_malloc0(sizeof(int) * nb_sns
);
1457 for (i
= 0; i
< nb_sns
; i
++) {
1462 while ((bs1
= bdrv_next(bs1
))) {
1463 if (bdrv_can_snapshot(bs1
) && bs1
!= bs
) {
1464 ret
= bdrv_snapshot_find(bs1
, sn_info
, sn
->id_str
);
1473 available_snapshots
[total
] = i
;
1479 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, NULL
);
1480 monitor_printf(mon
, "\n");
1481 for (i
= 0; i
< total
; i
++) {
1482 sn
= &sn_tab
[available_snapshots
[i
]];
1483 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, sn
);
1484 monitor_printf(mon
, "\n");
1487 monitor_printf(mon
, "There is no suitable snapshot available\n");
1491 g_free(available_snapshots
);
1495 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
1497 qemu_ram_set_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
,
1498 memory_region_name(mr
), dev
);
1501 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
1503 qemu_ram_unset_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
);
1506 void vmstate_register_ram_global(MemoryRegion
*mr
)
1508 vmstate_register_ram(mr
, NULL
);