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 "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"
49 #include "block/snapshot.h"
50 #include "block/qapi.h"
54 #define ETH_P_RARP 0x8035
56 #define ARP_HTYPE_ETH 0x0001
57 #define ARP_PTYPE_IP 0x0800
58 #define ARP_OP_REQUEST_REV 0x3
60 static bool skip_section_footers
;
62 static int announce_self_create(uint8_t *buf
,
65 /* Ethernet header. */
66 memset(buf
, 0xff, 6); /* destination MAC addr */
67 memcpy(buf
+ 6, mac_addr
, 6); /* source MAC addr */
68 *(uint16_t *)(buf
+ 12) = htons(ETH_P_RARP
); /* ethertype */
71 *(uint16_t *)(buf
+ 14) = htons(ARP_HTYPE_ETH
); /* hardware addr space */
72 *(uint16_t *)(buf
+ 16) = htons(ARP_PTYPE_IP
); /* protocol addr space */
73 *(buf
+ 18) = 6; /* hardware addr length (ethernet) */
74 *(buf
+ 19) = 4; /* protocol addr length (IPv4) */
75 *(uint16_t *)(buf
+ 20) = htons(ARP_OP_REQUEST_REV
); /* opcode */
76 memcpy(buf
+ 22, mac_addr
, 6); /* source hw addr */
77 memset(buf
+ 28, 0x00, 4); /* source protocol addr */
78 memcpy(buf
+ 32, mac_addr
, 6); /* target hw addr */
79 memset(buf
+ 38, 0x00, 4); /* target protocol addr */
81 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
82 memset(buf
+ 42, 0x00, 18);
84 return 60; /* len (FCS will be added by hardware) */
87 static void qemu_announce_self_iter(NICState
*nic
, void *opaque
)
92 trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic
->conf
->macaddr
));
93 len
= announce_self_create(buf
, nic
->conf
->macaddr
.a
);
95 qemu_send_packet_raw(qemu_get_queue(nic
), buf
, len
);
99 static void qemu_announce_self_once(void *opaque
)
101 static int count
= SELF_ANNOUNCE_ROUNDS
;
102 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
104 qemu_foreach_nic(qemu_announce_self_iter
, NULL
);
107 /* delay 50ms, 150ms, 250ms, ... */
108 timer_mod(timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) +
109 self_announce_delay(count
));
116 void qemu_announce_self(void)
118 static QEMUTimer
*timer
;
119 timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, qemu_announce_self_once
, &timer
);
120 qemu_announce_self_once(&timer
);
123 /***********************************************************/
124 /* savevm/loadvm support */
126 static ssize_t
block_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
132 qemu_iovec_init_external(&qiov
, iov
, iovcnt
);
133 ret
= bdrv_writev_vmstate(opaque
, &qiov
, pos
);
141 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
142 int64_t pos
, int size
)
144 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
148 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
150 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
153 static int bdrv_fclose(void *opaque
)
155 return bdrv_flush(opaque
);
158 static const QEMUFileOps bdrv_read_ops
= {
159 .get_buffer
= block_get_buffer
,
163 static const QEMUFileOps bdrv_write_ops
= {
164 .put_buffer
= block_put_buffer
,
165 .writev_buffer
= block_writev_buffer
,
169 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
172 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
174 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
178 /* QEMUFile timer support.
179 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
182 void timer_put(QEMUFile
*f
, QEMUTimer
*ts
)
184 uint64_t expire_time
;
186 expire_time
= timer_expire_time_ns(ts
);
187 qemu_put_be64(f
, expire_time
);
190 void timer_get(QEMUFile
*f
, QEMUTimer
*ts
)
192 uint64_t expire_time
;
194 expire_time
= qemu_get_be64(f
);
195 if (expire_time
!= -1) {
196 timer_mod_ns(ts
, expire_time
);
203 /* VMState timer support.
204 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
207 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
214 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
220 const VMStateInfo vmstate_info_timer
= {
227 typedef struct CompatEntry
{
232 typedef struct SaveStateEntry
{
233 QTAILQ_ENTRY(SaveStateEntry
) entry
;
240 const VMStateDescription
*vmsd
;
246 typedef struct SaveState
{
247 QTAILQ_HEAD(, SaveStateEntry
) handlers
;
248 int global_section_id
;
251 static SaveState savevm_state
= {
252 .handlers
= QTAILQ_HEAD_INITIALIZER(savevm_state
.handlers
),
253 .global_section_id
= 0,
256 static void dump_vmstate_vmsd(FILE *out_file
,
257 const VMStateDescription
*vmsd
, int indent
,
260 static void dump_vmstate_vmsf(FILE *out_file
, const VMStateField
*field
,
263 fprintf(out_file
, "%*s{\n", indent
, "");
265 fprintf(out_file
, "%*s\"field\": \"%s\",\n", indent
, "", field
->name
);
266 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
268 fprintf(out_file
, "%*s\"field_exists\": %s,\n", indent
, "",
269 field
->field_exists
? "true" : "false");
270 fprintf(out_file
, "%*s\"size\": %zu", indent
, "", field
->size
);
271 if (field
->vmsd
!= NULL
) {
272 fprintf(out_file
, ",\n");
273 dump_vmstate_vmsd(out_file
, field
->vmsd
, indent
, false);
275 fprintf(out_file
, "\n%*s}", indent
- 2, "");
278 static void dump_vmstate_vmss(FILE *out_file
,
279 const VMStateDescription
**subsection
,
282 if (*subsection
!= NULL
) {
283 dump_vmstate_vmsd(out_file
, *subsection
, indent
, true);
287 static void dump_vmstate_vmsd(FILE *out_file
,
288 const VMStateDescription
*vmsd
, int indent
,
292 fprintf(out_file
, "%*s{\n", indent
, "");
294 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", "Description");
297 fprintf(out_file
, "%*s\"name\": \"%s\",\n", indent
, "", vmsd
->name
);
298 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
300 fprintf(out_file
, "%*s\"minimum_version_id\": %d", indent
, "",
301 vmsd
->minimum_version_id
);
302 if (vmsd
->fields
!= NULL
) {
303 const VMStateField
*field
= vmsd
->fields
;
306 fprintf(out_file
, ",\n%*s\"Fields\": [\n", indent
, "");
308 while (field
->name
!= NULL
) {
309 if (field
->flags
& VMS_MUST_EXIST
) {
310 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
315 fprintf(out_file
, ",\n");
317 dump_vmstate_vmsf(out_file
, field
, indent
+ 2);
321 fprintf(out_file
, "\n%*s]", indent
, "");
323 if (vmsd
->subsections
!= NULL
) {
324 const VMStateDescription
**subsection
= vmsd
->subsections
;
327 fprintf(out_file
, ",\n%*s\"Subsections\": [\n", indent
, "");
329 while (*subsection
!= NULL
) {
331 fprintf(out_file
, ",\n");
333 dump_vmstate_vmss(out_file
, subsection
, indent
+ 2);
337 fprintf(out_file
, "\n%*s]", indent
, "");
339 fprintf(out_file
, "\n%*s}", indent
- 2, "");
342 static void dump_machine_type(FILE *out_file
)
346 mc
= MACHINE_GET_CLASS(current_machine
);
348 fprintf(out_file
, " \"vmschkmachine\": {\n");
349 fprintf(out_file
, " \"Name\": \"%s\"\n", mc
->name
);
350 fprintf(out_file
, " },\n");
353 void dump_vmstate_json_to_file(FILE *out_file
)
358 fprintf(out_file
, "{\n");
359 dump_machine_type(out_file
);
362 list
= object_class_get_list(TYPE_DEVICE
, true);
363 for (elt
= list
; elt
; elt
= elt
->next
) {
364 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
374 fprintf(out_file
, ",\n");
376 name
= object_class_get_name(OBJECT_CLASS(dc
));
377 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", name
);
379 fprintf(out_file
, "%*s\"Name\": \"%s\",\n", indent
, "", name
);
380 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
381 dc
->vmsd
->version_id
);
382 fprintf(out_file
, "%*s\"minimum_version_id\": %d,\n", indent
, "",
383 dc
->vmsd
->minimum_version_id
);
385 dump_vmstate_vmsd(out_file
, dc
->vmsd
, indent
, false);
387 fprintf(out_file
, "\n%*s}", indent
- 2, "");
390 fprintf(out_file
, "\n}\n");
394 static int calculate_new_instance_id(const char *idstr
)
399 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
400 if (strcmp(idstr
, se
->idstr
) == 0
401 && instance_id
<= se
->instance_id
) {
402 instance_id
= se
->instance_id
+ 1;
408 static int calculate_compat_instance_id(const char *idstr
)
413 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
418 if (strcmp(idstr
, se
->compat
->idstr
) == 0
419 && instance_id
<= se
->compat
->instance_id
) {
420 instance_id
= se
->compat
->instance_id
+ 1;
426 /* TODO: Individual devices generally have very little idea about the rest
427 of the system, so instance_id should be removed/replaced.
428 Meanwhile pass -1 as instance_id if you do not already have a clearly
429 distinguishing id for all instances of your device class. */
430 int register_savevm_live(DeviceState
*dev
,
439 se
= g_malloc0(sizeof(SaveStateEntry
));
440 se
->version_id
= version_id
;
441 se
->section_id
= savevm_state
.global_section_id
++;
445 /* if this is a live_savem then set is_ram */
446 if (ops
->save_live_setup
!= NULL
) {
451 char *id
= qdev_get_dev_path(dev
);
453 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
454 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
457 se
->compat
= g_malloc0(sizeof(CompatEntry
));
458 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
459 se
->compat
->instance_id
= instance_id
== -1 ?
460 calculate_compat_instance_id(idstr
) : instance_id
;
464 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
466 if (instance_id
== -1) {
467 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
469 se
->instance_id
= instance_id
;
471 assert(!se
->compat
|| se
->instance_id
== 0);
472 /* add at the end of list */
473 QTAILQ_INSERT_TAIL(&savevm_state
.handlers
, se
, entry
);
477 int register_savevm(DeviceState
*dev
,
481 SaveStateHandler
*save_state
,
482 LoadStateHandler
*load_state
,
485 SaveVMHandlers
*ops
= g_malloc0(sizeof(SaveVMHandlers
));
486 ops
->save_state
= save_state
;
487 ops
->load_state
= load_state
;
488 return register_savevm_live(dev
, idstr
, instance_id
, version_id
,
492 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
494 SaveStateEntry
*se
, *new_se
;
498 char *path
= qdev_get_dev_path(dev
);
500 pstrcpy(id
, sizeof(id
), path
);
501 pstrcat(id
, sizeof(id
), "/");
505 pstrcat(id
, sizeof(id
), idstr
);
507 QTAILQ_FOREACH_SAFE(se
, &savevm_state
.handlers
, entry
, new_se
) {
508 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
509 QTAILQ_REMOVE(&savevm_state
.handlers
, se
, entry
);
519 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
520 const VMStateDescription
*vmsd
,
521 void *opaque
, int alias_id
,
522 int required_for_version
)
526 /* If this triggers, alias support can be dropped for the vmsd. */
527 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
529 se
= g_malloc0(sizeof(SaveStateEntry
));
530 se
->version_id
= vmsd
->version_id
;
531 se
->section_id
= savevm_state
.global_section_id
++;
534 se
->alias_id
= alias_id
;
537 char *id
= qdev_get_dev_path(dev
);
539 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
540 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
543 se
->compat
= g_malloc0(sizeof(CompatEntry
));
544 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
545 se
->compat
->instance_id
= instance_id
== -1 ?
546 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
550 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
552 if (instance_id
== -1) {
553 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
555 se
->instance_id
= instance_id
;
557 assert(!se
->compat
|| se
->instance_id
== 0);
558 /* add at the end of list */
559 QTAILQ_INSERT_TAIL(&savevm_state
.handlers
, se
, entry
);
563 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
566 SaveStateEntry
*se
, *new_se
;
568 QTAILQ_FOREACH_SAFE(se
, &savevm_state
.handlers
, entry
, new_se
) {
569 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
570 QTAILQ_REMOVE(&savevm_state
.handlers
, se
, entry
);
579 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
581 trace_vmstate_load(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
582 if (!se
->vmsd
) { /* Old style */
583 return se
->ops
->load_state(f
, se
->opaque
, version_id
);
585 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
588 static void vmstate_save_old_style(QEMUFile
*f
, SaveStateEntry
*se
, QJSON
*vmdesc
)
590 int64_t old_offset
, size
;
592 old_offset
= qemu_ftell_fast(f
);
593 se
->ops
->save_state(f
, se
->opaque
);
594 size
= qemu_ftell_fast(f
) - old_offset
;
597 json_prop_int(vmdesc
, "size", size
);
598 json_start_array(vmdesc
, "fields");
599 json_start_object(vmdesc
, NULL
);
600 json_prop_str(vmdesc
, "name", "data");
601 json_prop_int(vmdesc
, "size", size
);
602 json_prop_str(vmdesc
, "type", "buffer");
603 json_end_object(vmdesc
);
604 json_end_array(vmdesc
);
608 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
, QJSON
*vmdesc
)
610 trace_vmstate_save(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
612 vmstate_save_old_style(f
, se
, vmdesc
);
615 vmstate_save_state(f
, se
->vmsd
, se
->opaque
, vmdesc
);
618 void savevm_skip_section_footers(void)
620 skip_section_footers
= true;
624 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
626 static void save_section_header(QEMUFile
*f
, SaveStateEntry
*se
,
627 uint8_t section_type
)
629 qemu_put_byte(f
, section_type
);
630 qemu_put_be32(f
, se
->section_id
);
632 if (section_type
== QEMU_VM_SECTION_FULL
||
633 section_type
== QEMU_VM_SECTION_START
) {
635 size_t len
= strlen(se
->idstr
);
636 qemu_put_byte(f
, len
);
637 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
639 qemu_put_be32(f
, se
->instance_id
);
640 qemu_put_be32(f
, se
->version_id
);
645 * Write a footer onto device sections that catches cases misformatted device
648 static void save_section_footer(QEMUFile
*f
, SaveStateEntry
*se
)
650 if (!skip_section_footers
) {
651 qemu_put_byte(f
, QEMU_VM_SECTION_FOOTER
);
652 qemu_put_be32(f
, se
->section_id
);
657 * Read a footer off the wire and check that it matches the expected section
659 * Returns: true if the footer was good
660 * false if there is a problem (and calls error_report to say why)
662 static bool check_section_footer(QEMUFile
*f
, SaveStateEntry
*se
)
665 uint32_t read_section_id
;
667 if (skip_section_footers
) {
668 /* No footer to check */
672 read_mark
= qemu_get_byte(f
);
674 if (read_mark
!= QEMU_VM_SECTION_FOOTER
) {
675 error_report("Missing section footer for %s", se
->idstr
);
679 read_section_id
= qemu_get_be32(f
);
680 if (read_section_id
!= se
->section_id
) {
681 error_report("Mismatched section id in footer for %s -"
682 " read 0x%x expected 0x%x",
683 se
->idstr
, read_section_id
, se
->section_id
);
691 bool qemu_savevm_state_blocked(Error
**errp
)
695 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
696 if (se
->vmsd
&& se
->vmsd
->unmigratable
) {
697 error_setg(errp
, "State blocked by non-migratable device '%s'",
705 void qemu_savevm_state_header(QEMUFile
*f
)
707 trace_savevm_state_header();
708 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
709 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
712 void qemu_savevm_state_begin(QEMUFile
*f
,
713 const MigrationParams
*params
)
718 trace_savevm_state_begin();
719 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
720 if (!se
->ops
|| !se
->ops
->set_params
) {
723 se
->ops
->set_params(params
, se
->opaque
);
726 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
727 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
730 if (se
->ops
&& se
->ops
->is_active
) {
731 if (!se
->ops
->is_active(se
->opaque
)) {
735 save_section_header(f
, se
, QEMU_VM_SECTION_START
);
737 ret
= se
->ops
->save_live_setup(f
, se
->opaque
);
738 save_section_footer(f
, se
);
740 qemu_file_set_error(f
, ret
);
747 * this function has three return values:
748 * negative: there was one error, and we have -errno.
749 * 0 : We haven't finished, caller have to go again
750 * 1 : We have finished, we can go to complete phase
752 int qemu_savevm_state_iterate(QEMUFile
*f
)
757 trace_savevm_state_iterate();
758 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
759 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
762 if (se
->ops
&& se
->ops
->is_active
) {
763 if (!se
->ops
->is_active(se
->opaque
)) {
767 if (qemu_file_rate_limit(f
)) {
770 trace_savevm_section_start(se
->idstr
, se
->section_id
);
772 save_section_header(f
, se
, QEMU_VM_SECTION_PART
);
774 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
775 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
776 save_section_footer(f
, se
);
779 qemu_file_set_error(f
, ret
);
782 /* Do not proceed to the next vmstate before this one reported
783 completion of the current stage. This serializes the migration
784 and reduces the probability that a faster changing state is
785 synchronized over and over again. */
792 static bool should_send_vmdesc(void)
794 MachineState
*machine
= MACHINE(qdev_get_machine());
795 return !machine
->suppress_vmdesc
;
798 void qemu_savevm_state_complete(QEMUFile
*f
)
805 trace_savevm_state_complete();
807 cpu_synchronize_all_states();
809 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
810 if (!se
->ops
|| !se
->ops
->save_live_complete
) {
813 if (se
->ops
&& se
->ops
->is_active
) {
814 if (!se
->ops
->is_active(se
->opaque
)) {
818 trace_savevm_section_start(se
->idstr
, se
->section_id
);
820 save_section_header(f
, se
, QEMU_VM_SECTION_END
);
822 ret
= se
->ops
->save_live_complete(f
, se
->opaque
);
823 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
824 save_section_footer(f
, se
);
826 qemu_file_set_error(f
, ret
);
831 vmdesc
= qjson_new();
832 json_prop_int(vmdesc
, "page_size", TARGET_PAGE_SIZE
);
833 json_start_array(vmdesc
, "devices");
834 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
836 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
839 trace_savevm_section_start(se
->idstr
, se
->section_id
);
841 json_start_object(vmdesc
, NULL
);
842 json_prop_str(vmdesc
, "name", se
->idstr
);
843 json_prop_int(vmdesc
, "instance_id", se
->instance_id
);
845 save_section_header(f
, se
, QEMU_VM_SECTION_FULL
);
847 vmstate_save(f
, se
, vmdesc
);
849 json_end_object(vmdesc
);
850 trace_savevm_section_end(se
->idstr
, se
->section_id
, 0);
851 save_section_footer(f
, se
);
854 qemu_put_byte(f
, QEMU_VM_EOF
);
856 json_end_array(vmdesc
);
857 qjson_finish(vmdesc
);
858 vmdesc_len
= strlen(qjson_get_str(vmdesc
));
860 if (should_send_vmdesc()) {
861 qemu_put_byte(f
, QEMU_VM_VMDESCRIPTION
);
862 qemu_put_be32(f
, vmdesc_len
);
863 qemu_put_buffer(f
, (uint8_t *)qjson_get_str(vmdesc
), vmdesc_len
);
865 object_unref(OBJECT(vmdesc
));
870 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
875 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
876 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
879 if (se
->ops
&& se
->ops
->is_active
) {
880 if (!se
->ops
->is_active(se
->opaque
)) {
884 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
889 void qemu_savevm_state_cancel(void)
893 trace_savevm_state_cancel();
894 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
895 if (se
->ops
&& se
->ops
->cancel
) {
896 se
->ops
->cancel(se
->opaque
);
901 static int qemu_savevm_state(QEMUFile
*f
, Error
**errp
)
904 MigrationParams params
= {
909 if (qemu_savevm_state_blocked(errp
)) {
913 qemu_mutex_unlock_iothread();
914 qemu_savevm_state_header(f
);
915 qemu_savevm_state_begin(f
, ¶ms
);
916 qemu_mutex_lock_iothread();
918 while (qemu_file_get_error(f
) == 0) {
919 if (qemu_savevm_state_iterate(f
) > 0) {
924 ret
= qemu_file_get_error(f
);
926 qemu_savevm_state_complete(f
);
927 ret
= qemu_file_get_error(f
);
930 qemu_savevm_state_cancel();
931 error_setg_errno(errp
, -ret
, "Error while writing VM state");
936 static int qemu_save_device_state(QEMUFile
*f
)
940 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
941 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
943 cpu_synchronize_all_states();
945 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
949 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
953 save_section_header(f
, se
, QEMU_VM_SECTION_FULL
);
955 vmstate_save(f
, se
, NULL
);
957 save_section_footer(f
, se
);
960 qemu_put_byte(f
, QEMU_VM_EOF
);
962 return qemu_file_get_error(f
);
965 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
969 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
970 if (!strcmp(se
->idstr
, idstr
) &&
971 (instance_id
== se
->instance_id
||
972 instance_id
== se
->alias_id
))
974 /* Migrating from an older version? */
975 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
976 if (!strcmp(se
->compat
->idstr
, idstr
) &&
977 (instance_id
== se
->compat
->instance_id
||
978 instance_id
== se
->alias_id
))
985 struct LoadStateEntry
{
986 QLIST_ENTRY(LoadStateEntry
) entry
;
992 void loadvm_free_handlers(MigrationIncomingState
*mis
)
994 LoadStateEntry
*le
, *new_le
;
996 QLIST_FOREACH_SAFE(le
, &mis
->loadvm_handlers
, entry
, new_le
) {
997 QLIST_REMOVE(le
, entry
);
1002 int qemu_loadvm_state(QEMUFile
*f
)
1004 MigrationIncomingState
*mis
= migration_incoming_get_current();
1005 Error
*local_err
= NULL
;
1006 uint8_t section_type
;
1009 int file_error_after_eof
= -1;
1011 if (qemu_savevm_state_blocked(&local_err
)) {
1012 error_report_err(local_err
);
1016 v
= qemu_get_be32(f
);
1017 if (v
!= QEMU_VM_FILE_MAGIC
) {
1018 error_report("Not a migration stream");
1022 v
= qemu_get_be32(f
);
1023 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
1024 error_report("SaveVM v2 format is obsolete and don't work anymore");
1027 if (v
!= QEMU_VM_FILE_VERSION
) {
1028 error_report("Unsupported migration stream version");
1032 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
1033 uint32_t instance_id
, version_id
, section_id
;
1038 trace_qemu_loadvm_state_section(section_type
);
1039 switch (section_type
) {
1040 case QEMU_VM_SECTION_START
:
1041 case QEMU_VM_SECTION_FULL
:
1042 /* Read section start */
1043 section_id
= qemu_get_be32(f
);
1044 if (!qemu_get_counted_string(f
, idstr
)) {
1045 error_report("Unable to read ID string for section %u",
1049 instance_id
= qemu_get_be32(f
);
1050 version_id
= qemu_get_be32(f
);
1052 trace_qemu_loadvm_state_section_startfull(section_id
, idstr
,
1053 instance_id
, version_id
);
1054 /* Find savevm section */
1055 se
= find_se(idstr
, instance_id
);
1057 error_report("Unknown savevm section or instance '%s' %d",
1058 idstr
, instance_id
);
1063 /* Validate version */
1064 if (version_id
> se
->version_id
) {
1065 error_report("savevm: unsupported version %d for '%s' v%d",
1066 version_id
, idstr
, se
->version_id
);
1072 le
= g_malloc0(sizeof(*le
));
1075 le
->section_id
= section_id
;
1076 le
->version_id
= version_id
;
1077 QLIST_INSERT_HEAD(&mis
->loadvm_handlers
, le
, entry
);
1079 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1081 error_report("error while loading state for instance 0x%x of"
1082 " device '%s'", instance_id
, idstr
);
1085 if (!check_section_footer(f
, le
->se
)) {
1090 case QEMU_VM_SECTION_PART
:
1091 case QEMU_VM_SECTION_END
:
1092 section_id
= qemu_get_be32(f
);
1094 trace_qemu_loadvm_state_section_partend(section_id
);
1095 QLIST_FOREACH(le
, &mis
->loadvm_handlers
, entry
) {
1096 if (le
->section_id
== section_id
) {
1101 error_report("Unknown savevm section %d", section_id
);
1106 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1108 error_report("error while loading state section id %d(%s)",
1109 section_id
, le
->se
->idstr
);
1112 if (!check_section_footer(f
, le
->se
)) {
1118 error_report("Unknown savevm section type %d", section_type
);
1124 file_error_after_eof
= qemu_file_get_error(f
);
1127 * Try to read in the VMDESC section as well, so that dumping tools that
1128 * intercept our migration stream have the chance to see it.
1130 if (qemu_get_byte(f
) == QEMU_VM_VMDESCRIPTION
) {
1131 uint32_t size
= qemu_get_be32(f
);
1132 uint8_t *buf
= g_malloc(0x1000);
1135 uint32_t read_chunk
= MIN(size
, 0x1000);
1136 qemu_get_buffer(f
, buf
, read_chunk
);
1142 cpu_synchronize_all_post_init();
1148 /* We may not have a VMDESC section, so ignore relative errors */
1149 ret
= file_error_after_eof
;
1155 static BlockDriverState
*find_vmstate_bs(void)
1157 BlockDriverState
*bs
= NULL
;
1158 while ((bs
= bdrv_next(bs
))) {
1159 if (bdrv_can_snapshot(bs
)) {
1167 * Deletes snapshots of a given name in all opened images.
1169 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
1171 BlockDriverState
*bs
;
1172 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
1176 while ((bs
= bdrv_next(bs
))) {
1177 if (bdrv_can_snapshot(bs
) &&
1178 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0) {
1179 bdrv_snapshot_delete_by_id_or_name(bs
, name
, &err
);
1182 "Error while deleting snapshot on device '%s':"
1184 bdrv_get_device_name(bs
),
1185 error_get_pretty(err
));
1195 void hmp_savevm(Monitor
*mon
, const QDict
*qdict
)
1197 BlockDriverState
*bs
, *bs1
;
1198 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1201 int saved_vm_running
;
1202 uint64_t vm_state_size
;
1205 const char *name
= qdict_get_try_str(qdict
, "name");
1206 Error
*local_err
= NULL
;
1208 /* Verify if there is a device that doesn't support snapshots and is writable */
1210 while ((bs
= bdrv_next(bs
))) {
1212 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
1216 if (!bdrv_can_snapshot(bs
)) {
1217 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
1218 bdrv_get_device_name(bs
));
1223 bs
= find_vmstate_bs();
1225 monitor_printf(mon
, "No block device can accept snapshots\n");
1229 saved_vm_running
= runstate_is_running();
1230 vm_stop(RUN_STATE_SAVE_VM
);
1232 memset(sn
, 0, sizeof(*sn
));
1234 /* fill auxiliary fields */
1235 qemu_gettimeofday(&tv
);
1236 sn
->date_sec
= tv
.tv_sec
;
1237 sn
->date_nsec
= tv
.tv_usec
* 1000;
1238 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1241 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1243 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1244 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1246 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1249 /* cast below needed for OpenBSD where tv_sec is still 'long' */
1250 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
1251 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
1254 /* Delete old snapshots of the same name */
1255 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
1259 /* save the VM state */
1260 f
= qemu_fopen_bdrv(bs
, 1);
1262 monitor_printf(mon
, "Could not open VM state file\n");
1265 ret
= qemu_savevm_state(f
, &local_err
);
1266 vm_state_size
= qemu_ftell(f
);
1269 monitor_printf(mon
, "%s\n", error_get_pretty(local_err
));
1270 error_free(local_err
);
1274 /* create the snapshots */
1277 while ((bs1
= bdrv_next(bs1
))) {
1278 if (bdrv_can_snapshot(bs1
)) {
1279 /* Write VM state size only to the image that contains the state */
1280 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1281 ret
= bdrv_snapshot_create(bs1
, sn
);
1283 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
1284 bdrv_get_device_name(bs1
));
1290 if (saved_vm_running
) {
1295 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
1298 int saved_vm_running
;
1301 saved_vm_running
= runstate_is_running();
1302 vm_stop(RUN_STATE_SAVE_VM
);
1304 f
= qemu_fopen(filename
, "wb");
1306 error_setg_file_open(errp
, errno
, filename
);
1309 ret
= qemu_save_device_state(f
);
1312 error_setg(errp
, QERR_IO_ERROR
);
1316 if (saved_vm_running
) {
1321 int load_vmstate(const char *name
)
1323 BlockDriverState
*bs
, *bs_vm_state
;
1324 QEMUSnapshotInfo sn
;
1328 bs_vm_state
= find_vmstate_bs();
1330 error_report("No block device supports snapshots");
1334 /* Don't even try to load empty VM states */
1335 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
1338 } else if (sn
.vm_state_size
== 0) {
1339 error_report("This is a disk-only snapshot. Revert to it offline "
1344 /* Verify if there is any device that doesn't support snapshots and is
1345 writable and check if the requested snapshot is available too. */
1347 while ((bs
= bdrv_next(bs
))) {
1349 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
1353 if (!bdrv_can_snapshot(bs
)) {
1354 error_report("Device '%s' is writable but does not support snapshots.",
1355 bdrv_get_device_name(bs
));
1359 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1361 error_report("Device '%s' does not have the requested snapshot '%s'",
1362 bdrv_get_device_name(bs
), name
);
1367 /* Flush all IO requests so they don't interfere with the new state. */
1371 while ((bs
= bdrv_next(bs
))) {
1372 if (bdrv_can_snapshot(bs
)) {
1373 ret
= bdrv_snapshot_goto(bs
, name
);
1375 error_report("Error %d while activating snapshot '%s' on '%s'",
1376 ret
, name
, bdrv_get_device_name(bs
));
1382 /* restore the VM state */
1383 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
1385 error_report("Could not open VM state file");
1389 qemu_system_reset(VMRESET_SILENT
);
1390 migration_incoming_state_new(f
);
1391 ret
= qemu_loadvm_state(f
);
1394 migration_incoming_state_destroy();
1396 error_report("Error %d while loading VM state", ret
);
1403 void hmp_delvm(Monitor
*mon
, const QDict
*qdict
)
1405 BlockDriverState
*bs
;
1407 const char *name
= qdict_get_str(qdict
, "name");
1409 if (!find_vmstate_bs()) {
1410 monitor_printf(mon
, "No block device supports snapshots\n");
1415 while ((bs
= bdrv_next(bs
))) {
1416 if (bdrv_can_snapshot(bs
)) {
1418 bdrv_snapshot_delete_by_id_or_name(bs
, name
, &err
);
1421 "Error while deleting snapshot on device '%s':"
1423 bdrv_get_device_name(bs
),
1424 error_get_pretty(err
));
1431 void hmp_info_snapshots(Monitor
*mon
, const QDict
*qdict
)
1433 BlockDriverState
*bs
, *bs1
;
1434 QEMUSnapshotInfo
*sn_tab
, *sn
, s
, *sn_info
= &s
;
1435 int nb_sns
, i
, ret
, available
;
1437 int *available_snapshots
;
1439 bs
= find_vmstate_bs();
1441 monitor_printf(mon
, "No available block device supports snapshots\n");
1445 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1447 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
1452 monitor_printf(mon
, "There is no snapshot available.\n");
1456 available_snapshots
= g_malloc0(sizeof(int) * nb_sns
);
1458 for (i
= 0; i
< nb_sns
; i
++) {
1463 while ((bs1
= bdrv_next(bs1
))) {
1464 if (bdrv_can_snapshot(bs1
) && bs1
!= bs
) {
1465 ret
= bdrv_snapshot_find(bs1
, sn_info
, sn
->id_str
);
1474 available_snapshots
[total
] = i
;
1480 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, NULL
);
1481 monitor_printf(mon
, "\n");
1482 for (i
= 0; i
< total
; i
++) {
1483 sn
= &sn_tab
[available_snapshots
[i
]];
1484 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, sn
);
1485 monitor_printf(mon
, "\n");
1488 monitor_printf(mon
, "There is no suitable snapshot available\n");
1492 g_free(available_snapshots
);
1496 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
1498 qemu_ram_set_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
,
1499 memory_region_name(mr
), dev
);
1502 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
1504 qemu_ram_unset_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
);
1507 void vmstate_register_ram_global(MemoryRegion
*mr
)
1509 vmstate_register_ram(mr
, NULL
);