4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "config-host.h"
26 #include "qemu-common.h"
27 #include "hw/boards.h"
31 #include "monitor/monitor.h"
32 #include "sysemu/sysemu.h"
33 #include "qemu/timer.h"
34 #include "audio/audio.h"
35 #include "migration/migration.h"
36 #include "qemu/sockets.h"
37 #include "qemu/queue.h"
38 #include "sysemu/cpus.h"
39 #include "exec/memory.h"
40 #include "qmp-commands.h"
43 #include "block/snapshot.h"
44 #include "block/qapi.h"
48 #define ETH_P_RARP 0x8035
50 #define ARP_HTYPE_ETH 0x0001
51 #define ARP_PTYPE_IP 0x0800
52 #define ARP_OP_REQUEST_REV 0x3
54 static int announce_self_create(uint8_t *buf
,
57 /* Ethernet header. */
58 memset(buf
, 0xff, 6); /* destination MAC addr */
59 memcpy(buf
+ 6, mac_addr
, 6); /* source MAC addr */
60 *(uint16_t *)(buf
+ 12) = htons(ETH_P_RARP
); /* ethertype */
63 *(uint16_t *)(buf
+ 14) = htons(ARP_HTYPE_ETH
); /* hardware addr space */
64 *(uint16_t *)(buf
+ 16) = htons(ARP_PTYPE_IP
); /* protocol addr space */
65 *(buf
+ 18) = 6; /* hardware addr length (ethernet) */
66 *(buf
+ 19) = 4; /* protocol addr length (IPv4) */
67 *(uint16_t *)(buf
+ 20) = htons(ARP_OP_REQUEST_REV
); /* opcode */
68 memcpy(buf
+ 22, mac_addr
, 6); /* source hw addr */
69 memset(buf
+ 28, 0x00, 4); /* source protocol addr */
70 memcpy(buf
+ 32, mac_addr
, 6); /* target hw addr */
71 memset(buf
+ 38, 0x00, 4); /* target protocol addr */
73 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
74 memset(buf
+ 42, 0x00, 18);
76 return 60; /* len (FCS will be added by hardware) */
79 static void qemu_announce_self_iter(NICState
*nic
, void *opaque
)
84 trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic
->conf
->macaddr
));
85 len
= announce_self_create(buf
, nic
->conf
->macaddr
.a
);
87 qemu_send_packet_raw(qemu_get_queue(nic
), buf
, len
);
91 static void qemu_announce_self_once(void *opaque
)
93 static int count
= SELF_ANNOUNCE_ROUNDS
;
94 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
96 qemu_foreach_nic(qemu_announce_self_iter
, NULL
);
99 /* delay 50ms, 150ms, 250ms, ... */
100 timer_mod(timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) +
101 self_announce_delay(count
));
108 void qemu_announce_self(void)
110 static QEMUTimer
*timer
;
111 timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, qemu_announce_self_once
, &timer
);
112 qemu_announce_self_once(&timer
);
115 /***********************************************************/
116 /* savevm/loadvm support */
118 static ssize_t
block_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
124 qemu_iovec_init_external(&qiov
, iov
, iovcnt
);
125 ret
= bdrv_writev_vmstate(opaque
, &qiov
, pos
);
133 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
134 int64_t pos
, int size
)
136 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
140 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
142 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
145 static int bdrv_fclose(void *opaque
)
147 return bdrv_flush(opaque
);
150 static const QEMUFileOps bdrv_read_ops
= {
151 .get_buffer
= block_get_buffer
,
155 static const QEMUFileOps bdrv_write_ops
= {
156 .put_buffer
= block_put_buffer
,
157 .writev_buffer
= block_writev_buffer
,
161 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
164 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
166 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
170 /* QEMUFile timer support.
171 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
174 void timer_put(QEMUFile
*f
, QEMUTimer
*ts
)
176 uint64_t expire_time
;
178 expire_time
= timer_expire_time_ns(ts
);
179 qemu_put_be64(f
, expire_time
);
182 void timer_get(QEMUFile
*f
, QEMUTimer
*ts
)
184 uint64_t expire_time
;
186 expire_time
= qemu_get_be64(f
);
187 if (expire_time
!= -1) {
188 timer_mod_ns(ts
, expire_time
);
195 /* VMState timer support.
196 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
199 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
206 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
212 const VMStateInfo vmstate_info_timer
= {
219 typedef struct CompatEntry
{
224 typedef struct SaveStateEntry
{
225 QTAILQ_ENTRY(SaveStateEntry
) entry
;
232 const VMStateDescription
*vmsd
;
239 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
240 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
241 static int global_section_id
;
243 static void dump_vmstate_vmsd(FILE *out_file
,
244 const VMStateDescription
*vmsd
, int indent
,
247 static void dump_vmstate_vmsf(FILE *out_file
, const VMStateField
*field
,
250 fprintf(out_file
, "%*s{\n", indent
, "");
252 fprintf(out_file
, "%*s\"field\": \"%s\",\n", indent
, "", field
->name
);
253 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
255 fprintf(out_file
, "%*s\"field_exists\": %s,\n", indent
, "",
256 field
->field_exists
? "true" : "false");
257 fprintf(out_file
, "%*s\"size\": %zu", indent
, "", field
->size
);
258 if (field
->vmsd
!= NULL
) {
259 fprintf(out_file
, ",\n");
260 dump_vmstate_vmsd(out_file
, field
->vmsd
, indent
, false);
262 fprintf(out_file
, "\n%*s}", indent
- 2, "");
265 static void dump_vmstate_vmss(FILE *out_file
,
266 const VMStateSubsection
*subsection
,
269 if (subsection
->vmsd
!= NULL
) {
270 dump_vmstate_vmsd(out_file
, subsection
->vmsd
, indent
, true);
274 static void dump_vmstate_vmsd(FILE *out_file
,
275 const VMStateDescription
*vmsd
, int indent
,
279 fprintf(out_file
, "%*s{\n", indent
, "");
281 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", "Description");
284 fprintf(out_file
, "%*s\"name\": \"%s\",\n", indent
, "", vmsd
->name
);
285 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
287 fprintf(out_file
, "%*s\"minimum_version_id\": %d", indent
, "",
288 vmsd
->minimum_version_id
);
289 if (vmsd
->fields
!= NULL
) {
290 const VMStateField
*field
= vmsd
->fields
;
293 fprintf(out_file
, ",\n%*s\"Fields\": [\n", indent
, "");
295 while (field
->name
!= NULL
) {
296 if (field
->flags
& VMS_MUST_EXIST
) {
297 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
302 fprintf(out_file
, ",\n");
304 dump_vmstate_vmsf(out_file
, field
, indent
+ 2);
308 fprintf(out_file
, "\n%*s]", indent
, "");
310 if (vmsd
->subsections
!= NULL
) {
311 const VMStateSubsection
*subsection
= vmsd
->subsections
;
314 fprintf(out_file
, ",\n%*s\"Subsections\": [\n", indent
, "");
316 while (subsection
->vmsd
!= NULL
) {
318 fprintf(out_file
, ",\n");
320 dump_vmstate_vmss(out_file
, subsection
, indent
+ 2);
324 fprintf(out_file
, "\n%*s]", indent
, "");
326 fprintf(out_file
, "\n%*s}", indent
- 2, "");
329 static void dump_machine_type(FILE *out_file
)
333 mc
= MACHINE_GET_CLASS(current_machine
);
335 fprintf(out_file
, " \"vmschkmachine\": {\n");
336 fprintf(out_file
, " \"Name\": \"%s\"\n", mc
->name
);
337 fprintf(out_file
, " },\n");
340 void dump_vmstate_json_to_file(FILE *out_file
)
345 fprintf(out_file
, "{\n");
346 dump_machine_type(out_file
);
349 list
= object_class_get_list(TYPE_DEVICE
, true);
350 for (elt
= list
; elt
; elt
= elt
->next
) {
351 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
361 fprintf(out_file
, ",\n");
363 name
= object_class_get_name(OBJECT_CLASS(dc
));
364 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", name
);
366 fprintf(out_file
, "%*s\"Name\": \"%s\",\n", indent
, "", name
);
367 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
368 dc
->vmsd
->version_id
);
369 fprintf(out_file
, "%*s\"minimum_version_id\": %d,\n", indent
, "",
370 dc
->vmsd
->minimum_version_id
);
372 dump_vmstate_vmsd(out_file
, dc
->vmsd
, indent
, false);
374 fprintf(out_file
, "\n%*s}", indent
- 2, "");
377 fprintf(out_file
, "\n}\n");
381 static int calculate_new_instance_id(const char *idstr
)
386 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
387 if (strcmp(idstr
, se
->idstr
) == 0
388 && instance_id
<= se
->instance_id
) {
389 instance_id
= se
->instance_id
+ 1;
395 static int calculate_compat_instance_id(const char *idstr
)
400 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
405 if (strcmp(idstr
, se
->compat
->idstr
) == 0
406 && instance_id
<= se
->compat
->instance_id
) {
407 instance_id
= se
->compat
->instance_id
+ 1;
413 /* TODO: Individual devices generally have very little idea about the rest
414 of the system, so instance_id should be removed/replaced.
415 Meanwhile pass -1 as instance_id if you do not already have a clearly
416 distinguishing id for all instances of your device class. */
417 int register_savevm_live(DeviceState
*dev
,
426 se
= g_malloc0(sizeof(SaveStateEntry
));
427 se
->version_id
= version_id
;
428 se
->section_id
= global_section_id
++;
432 /* if this is a live_savem then set is_ram */
433 if (ops
->save_live_setup
!= NULL
) {
438 char *id
= qdev_get_dev_path(dev
);
440 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
441 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
444 se
->compat
= g_malloc0(sizeof(CompatEntry
));
445 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
446 se
->compat
->instance_id
= instance_id
== -1 ?
447 calculate_compat_instance_id(idstr
) : instance_id
;
451 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
453 if (instance_id
== -1) {
454 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
456 se
->instance_id
= instance_id
;
458 assert(!se
->compat
|| se
->instance_id
== 0);
459 /* add at the end of list */
460 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
464 int register_savevm(DeviceState
*dev
,
468 SaveStateHandler
*save_state
,
469 LoadStateHandler
*load_state
,
472 SaveVMHandlers
*ops
= g_malloc0(sizeof(SaveVMHandlers
));
473 ops
->save_state
= save_state
;
474 ops
->load_state
= load_state
;
475 return register_savevm_live(dev
, idstr
, instance_id
, version_id
,
479 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
481 SaveStateEntry
*se
, *new_se
;
485 char *path
= qdev_get_dev_path(dev
);
487 pstrcpy(id
, sizeof(id
), path
);
488 pstrcat(id
, sizeof(id
), "/");
492 pstrcat(id
, sizeof(id
), idstr
);
494 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
495 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
496 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
506 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
507 const VMStateDescription
*vmsd
,
508 void *opaque
, int alias_id
,
509 int required_for_version
)
513 /* If this triggers, alias support can be dropped for the vmsd. */
514 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
516 se
= g_malloc0(sizeof(SaveStateEntry
));
517 se
->version_id
= vmsd
->version_id
;
518 se
->section_id
= global_section_id
++;
521 se
->alias_id
= alias_id
;
524 char *id
= qdev_get_dev_path(dev
);
526 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
527 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
530 se
->compat
= g_malloc0(sizeof(CompatEntry
));
531 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
532 se
->compat
->instance_id
= instance_id
== -1 ?
533 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
537 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
539 if (instance_id
== -1) {
540 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
542 se
->instance_id
= instance_id
;
544 assert(!se
->compat
|| se
->instance_id
== 0);
545 /* add at the end of list */
546 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
550 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
553 SaveStateEntry
*se
, *new_se
;
555 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
556 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
557 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
566 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
568 trace_vmstate_load(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
569 if (!se
->vmsd
) { /* Old style */
570 return se
->ops
->load_state(f
, se
->opaque
, version_id
);
572 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
575 static void vmstate_save_old_style(QEMUFile
*f
, SaveStateEntry
*se
, QJSON
*vmdesc
)
577 int64_t old_offset
, size
;
579 old_offset
= qemu_ftell_fast(f
);
580 se
->ops
->save_state(f
, se
->opaque
);
581 size
= qemu_ftell_fast(f
) - old_offset
;
584 json_prop_int(vmdesc
, "size", size
);
585 json_start_array(vmdesc
, "fields");
586 json_start_object(vmdesc
, NULL
);
587 json_prop_str(vmdesc
, "name", "data");
588 json_prop_int(vmdesc
, "size", size
);
589 json_prop_str(vmdesc
, "type", "buffer");
590 json_end_object(vmdesc
);
591 json_end_array(vmdesc
);
595 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
, QJSON
*vmdesc
)
597 trace_vmstate_save(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
599 vmstate_save_old_style(f
, se
, vmdesc
);
602 vmstate_save_state(f
, se
->vmsd
, se
->opaque
, vmdesc
);
605 bool qemu_savevm_state_blocked(Error
**errp
)
609 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
610 if (se
->vmsd
&& se
->vmsd
->unmigratable
) {
611 error_setg(errp
, "State blocked by non-migratable device '%s'",
619 void qemu_savevm_state_begin(QEMUFile
*f
,
620 const MigrationParams
*params
)
625 trace_savevm_state_begin();
626 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
627 if (!se
->ops
|| !se
->ops
->set_params
) {
630 se
->ops
->set_params(params
, se
->opaque
);
633 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
634 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
636 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
639 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
642 if (se
->ops
&& se
->ops
->is_active
) {
643 if (!se
->ops
->is_active(se
->opaque
)) {
648 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
649 qemu_put_be32(f
, se
->section_id
);
652 len
= strlen(se
->idstr
);
653 qemu_put_byte(f
, len
);
654 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
656 qemu_put_be32(f
, se
->instance_id
);
657 qemu_put_be32(f
, se
->version_id
);
659 ret
= se
->ops
->save_live_setup(f
, se
->opaque
);
661 qemu_file_set_error(f
, ret
);
668 * this function has three return values:
669 * negative: there was one error, and we have -errno.
670 * 0 : We haven't finished, caller have to go again
671 * 1 : We have finished, we can go to complete phase
673 int qemu_savevm_state_iterate(QEMUFile
*f
)
678 trace_savevm_state_iterate();
679 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
680 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
683 if (se
->ops
&& se
->ops
->is_active
) {
684 if (!se
->ops
->is_active(se
->opaque
)) {
688 if (qemu_file_rate_limit(f
)) {
691 trace_savevm_section_start(se
->idstr
, se
->section_id
);
693 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
694 qemu_put_be32(f
, se
->section_id
);
696 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
697 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
700 qemu_file_set_error(f
, ret
);
703 /* Do not proceed to the next vmstate before this one reported
704 completion of the current stage. This serializes the migration
705 and reduces the probability that a faster changing state is
706 synchronized over and over again. */
713 void qemu_savevm_state_complete(QEMUFile
*f
)
720 trace_savevm_state_complete();
722 cpu_synchronize_all_states();
724 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
725 if (!se
->ops
|| !se
->ops
->save_live_complete
) {
728 if (se
->ops
&& se
->ops
->is_active
) {
729 if (!se
->ops
->is_active(se
->opaque
)) {
733 trace_savevm_section_start(se
->idstr
, se
->section_id
);
735 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
736 qemu_put_be32(f
, se
->section_id
);
738 ret
= se
->ops
->save_live_complete(f
, se
->opaque
);
739 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
741 qemu_file_set_error(f
, ret
);
746 vmdesc
= qjson_new();
747 json_prop_int(vmdesc
, "page_size", TARGET_PAGE_SIZE
);
748 json_start_array(vmdesc
, "devices");
749 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
752 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
755 trace_savevm_section_start(se
->idstr
, se
->section_id
);
757 json_start_object(vmdesc
, NULL
);
758 json_prop_str(vmdesc
, "name", se
->idstr
);
759 json_prop_int(vmdesc
, "instance_id", se
->instance_id
);
762 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
763 qemu_put_be32(f
, se
->section_id
);
766 len
= strlen(se
->idstr
);
767 qemu_put_byte(f
, len
);
768 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
770 qemu_put_be32(f
, se
->instance_id
);
771 qemu_put_be32(f
, se
->version_id
);
773 vmstate_save(f
, se
, vmdesc
);
775 json_end_object(vmdesc
);
776 trace_savevm_section_end(se
->idstr
, se
->section_id
, 0);
779 qemu_put_byte(f
, QEMU_VM_EOF
);
781 json_end_array(vmdesc
);
782 qjson_finish(vmdesc
);
783 vmdesc_len
= strlen(qjson_get_str(vmdesc
));
785 qemu_put_byte(f
, QEMU_VM_VMDESCRIPTION
);
786 qemu_put_be32(f
, vmdesc_len
);
787 qemu_put_buffer(f
, (uint8_t *)qjson_get_str(vmdesc
), vmdesc_len
);
788 object_unref(OBJECT(vmdesc
));
793 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
798 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
799 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
802 if (se
->ops
&& se
->ops
->is_active
) {
803 if (!se
->ops
->is_active(se
->opaque
)) {
807 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
812 void qemu_savevm_state_cancel(void)
816 trace_savevm_state_cancel();
817 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
818 if (se
->ops
&& se
->ops
->cancel
) {
819 se
->ops
->cancel(se
->opaque
);
824 static int qemu_savevm_state(QEMUFile
*f
, Error
**errp
)
827 MigrationParams params
= {
832 if (qemu_savevm_state_blocked(errp
)) {
836 qemu_mutex_unlock_iothread();
837 qemu_savevm_state_begin(f
, ¶ms
);
838 qemu_mutex_lock_iothread();
840 while (qemu_file_get_error(f
) == 0) {
841 if (qemu_savevm_state_iterate(f
) > 0) {
846 ret
= qemu_file_get_error(f
);
848 qemu_savevm_state_complete(f
);
849 ret
= qemu_file_get_error(f
);
852 qemu_savevm_state_cancel();
853 error_setg_errno(errp
, -ret
, "Error while writing VM state");
858 static int qemu_save_device_state(QEMUFile
*f
)
862 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
863 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
865 cpu_synchronize_all_states();
867 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
873 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
878 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
879 qemu_put_be32(f
, se
->section_id
);
882 len
= strlen(se
->idstr
);
883 qemu_put_byte(f
, len
);
884 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
886 qemu_put_be32(f
, se
->instance_id
);
887 qemu_put_be32(f
, se
->version_id
);
889 vmstate_save(f
, se
, NULL
);
892 qemu_put_byte(f
, QEMU_VM_EOF
);
894 return qemu_file_get_error(f
);
897 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
901 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
902 if (!strcmp(se
->idstr
, idstr
) &&
903 (instance_id
== se
->instance_id
||
904 instance_id
== se
->alias_id
))
906 /* Migrating from an older version? */
907 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
908 if (!strcmp(se
->compat
->idstr
, idstr
) &&
909 (instance_id
== se
->compat
->instance_id
||
910 instance_id
== se
->alias_id
))
917 typedef struct LoadStateEntry
{
918 QLIST_ENTRY(LoadStateEntry
) entry
;
924 int qemu_loadvm_state(QEMUFile
*f
)
926 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
927 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
928 LoadStateEntry
*le
, *new_le
;
929 Error
*local_err
= NULL
;
930 uint8_t section_type
;
934 if (qemu_savevm_state_blocked(&local_err
)) {
935 error_report("%s", error_get_pretty(local_err
));
936 error_free(local_err
);
940 v
= qemu_get_be32(f
);
941 if (v
!= QEMU_VM_FILE_MAGIC
) {
942 error_report("Not a migration stream");
946 v
= qemu_get_be32(f
);
947 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
948 error_report("SaveVM v2 format is obsolete and don't work anymore");
951 if (v
!= QEMU_VM_FILE_VERSION
) {
952 error_report("Unsupported migration stream version");
956 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
957 uint32_t instance_id
, version_id
, section_id
;
962 trace_qemu_loadvm_state_section(section_type
);
963 switch (section_type
) {
964 case QEMU_VM_SECTION_START
:
965 case QEMU_VM_SECTION_FULL
:
966 /* Read section start */
967 section_id
= qemu_get_be32(f
);
968 len
= qemu_get_byte(f
);
969 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
971 instance_id
= qemu_get_be32(f
);
972 version_id
= qemu_get_be32(f
);
974 trace_qemu_loadvm_state_section_startfull(section_id
, idstr
,
975 instance_id
, version_id
);
976 /* Find savevm section */
977 se
= find_se(idstr
, instance_id
);
979 error_report("Unknown savevm section or instance '%s' %d",
985 /* Validate version */
986 if (version_id
> se
->version_id
) {
987 error_report("savevm: unsupported version %d for '%s' v%d",
988 version_id
, idstr
, se
->version_id
);
994 le
= g_malloc0(sizeof(*le
));
997 le
->section_id
= section_id
;
998 le
->version_id
= version_id
;
999 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
1001 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1003 error_report("error while loading state for instance 0x%x of"
1004 " device '%s'", instance_id
, idstr
);
1008 case QEMU_VM_SECTION_PART
:
1009 case QEMU_VM_SECTION_END
:
1010 section_id
= qemu_get_be32(f
);
1012 trace_qemu_loadvm_state_section_partend(section_id
);
1013 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
1014 if (le
->section_id
== section_id
) {
1019 error_report("Unknown savevm section %d", section_id
);
1024 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1026 error_report("error while loading state section id %d(%s)",
1027 section_id
, le
->se
->idstr
);
1032 error_report("Unknown savevm section type %d", section_type
);
1038 cpu_synchronize_all_post_init();
1043 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
1044 QLIST_REMOVE(le
, entry
);
1049 ret
= qemu_file_get_error(f
);
1055 static BlockDriverState
*find_vmstate_bs(void)
1057 BlockDriverState
*bs
= NULL
;
1058 while ((bs
= bdrv_next(bs
))) {
1059 if (bdrv_can_snapshot(bs
)) {
1067 * Deletes snapshots of a given name in all opened images.
1069 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
1071 BlockDriverState
*bs
;
1072 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
1076 while ((bs
= bdrv_next(bs
))) {
1077 if (bdrv_can_snapshot(bs
) &&
1078 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0) {
1079 bdrv_snapshot_delete_by_id_or_name(bs
, name
, &err
);
1082 "Error while deleting snapshot on device '%s':"
1084 bdrv_get_device_name(bs
),
1085 error_get_pretty(err
));
1095 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
1097 BlockDriverState
*bs
, *bs1
;
1098 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1101 int saved_vm_running
;
1102 uint64_t vm_state_size
;
1105 const char *name
= qdict_get_try_str(qdict
, "name");
1106 Error
*local_err
= NULL
;
1108 /* Verify if there is a device that doesn't support snapshots and is writable */
1110 while ((bs
= bdrv_next(bs
))) {
1112 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
1116 if (!bdrv_can_snapshot(bs
)) {
1117 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
1118 bdrv_get_device_name(bs
));
1123 bs
= find_vmstate_bs();
1125 monitor_printf(mon
, "No block device can accept snapshots\n");
1129 saved_vm_running
= runstate_is_running();
1130 vm_stop(RUN_STATE_SAVE_VM
);
1132 memset(sn
, 0, sizeof(*sn
));
1134 /* fill auxiliary fields */
1135 qemu_gettimeofday(&tv
);
1136 sn
->date_sec
= tv
.tv_sec
;
1137 sn
->date_nsec
= tv
.tv_usec
* 1000;
1138 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1141 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1143 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1144 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1146 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1149 /* cast below needed for OpenBSD where tv_sec is still 'long' */
1150 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
1151 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
1154 /* Delete old snapshots of the same name */
1155 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
1159 /* save the VM state */
1160 f
= qemu_fopen_bdrv(bs
, 1);
1162 monitor_printf(mon
, "Could not open VM state file\n");
1165 ret
= qemu_savevm_state(f
, &local_err
);
1166 vm_state_size
= qemu_ftell(f
);
1169 monitor_printf(mon
, "%s\n", error_get_pretty(local_err
));
1170 error_free(local_err
);
1174 /* create the snapshots */
1177 while ((bs1
= bdrv_next(bs1
))) {
1178 if (bdrv_can_snapshot(bs1
)) {
1179 /* Write VM state size only to the image that contains the state */
1180 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1181 ret
= bdrv_snapshot_create(bs1
, sn
);
1183 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
1184 bdrv_get_device_name(bs1
));
1190 if (saved_vm_running
) {
1195 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
1198 int saved_vm_running
;
1201 saved_vm_running
= runstate_is_running();
1202 vm_stop(RUN_STATE_SAVE_VM
);
1204 f
= qemu_fopen(filename
, "wb");
1206 error_setg_file_open(errp
, errno
, filename
);
1209 ret
= qemu_save_device_state(f
);
1212 error_set(errp
, QERR_IO_ERROR
);
1216 if (saved_vm_running
) {
1221 int load_vmstate(const char *name
)
1223 BlockDriverState
*bs
, *bs_vm_state
;
1224 QEMUSnapshotInfo sn
;
1228 bs_vm_state
= find_vmstate_bs();
1230 error_report("No block device supports snapshots");
1234 /* Don't even try to load empty VM states */
1235 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
1238 } else if (sn
.vm_state_size
== 0) {
1239 error_report("This is a disk-only snapshot. Revert to it offline "
1244 /* Verify if there is any device that doesn't support snapshots and is
1245 writable and check if the requested snapshot is available too. */
1247 while ((bs
= bdrv_next(bs
))) {
1249 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
1253 if (!bdrv_can_snapshot(bs
)) {
1254 error_report("Device '%s' is writable but does not support snapshots.",
1255 bdrv_get_device_name(bs
));
1259 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1261 error_report("Device '%s' does not have the requested snapshot '%s'",
1262 bdrv_get_device_name(bs
), name
);
1267 /* Flush all IO requests so they don't interfere with the new state. */
1271 while ((bs
= bdrv_next(bs
))) {
1272 if (bdrv_can_snapshot(bs
)) {
1273 ret
= bdrv_snapshot_goto(bs
, name
);
1275 error_report("Error %d while activating snapshot '%s' on '%s'",
1276 ret
, name
, bdrv_get_device_name(bs
));
1282 /* restore the VM state */
1283 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
1285 error_report("Could not open VM state file");
1289 qemu_system_reset(VMRESET_SILENT
);
1290 ret
= qemu_loadvm_state(f
);
1294 error_report("Error %d while loading VM state", ret
);
1301 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
1303 BlockDriverState
*bs
;
1305 const char *name
= qdict_get_str(qdict
, "name");
1307 if (!find_vmstate_bs()) {
1308 monitor_printf(mon
, "No block device supports snapshots\n");
1313 while ((bs
= bdrv_next(bs
))) {
1314 if (bdrv_can_snapshot(bs
)) {
1316 bdrv_snapshot_delete_by_id_or_name(bs
, name
, &err
);
1319 "Error while deleting snapshot on device '%s':"
1321 bdrv_get_device_name(bs
),
1322 error_get_pretty(err
));
1329 void do_info_snapshots(Monitor
*mon
, const QDict
*qdict
)
1331 BlockDriverState
*bs
, *bs1
;
1332 QEMUSnapshotInfo
*sn_tab
, *sn
, s
, *sn_info
= &s
;
1333 int nb_sns
, i
, ret
, available
;
1335 int *available_snapshots
;
1337 bs
= find_vmstate_bs();
1339 monitor_printf(mon
, "No available block device supports snapshots\n");
1343 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1345 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
1350 monitor_printf(mon
, "There is no snapshot available.\n");
1354 available_snapshots
= g_malloc0(sizeof(int) * nb_sns
);
1356 for (i
= 0; i
< nb_sns
; i
++) {
1361 while ((bs1
= bdrv_next(bs1
))) {
1362 if (bdrv_can_snapshot(bs1
) && bs1
!= bs
) {
1363 ret
= bdrv_snapshot_find(bs1
, sn_info
, sn
->id_str
);
1372 available_snapshots
[total
] = i
;
1378 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, NULL
);
1379 monitor_printf(mon
, "\n");
1380 for (i
= 0; i
< total
; i
++) {
1381 sn
= &sn_tab
[available_snapshots
[i
]];
1382 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, sn
);
1383 monitor_printf(mon
, "\n");
1386 monitor_printf(mon
, "There is no suitable snapshot available\n");
1390 g_free(available_snapshots
);
1394 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
1396 qemu_ram_set_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
,
1397 memory_region_name(mr
), dev
);
1400 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
1402 qemu_ram_unset_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
);
1405 void vmstate_register_ram_global(MemoryRegion
*mr
)
1407 vmstate_register_ram(mr
, NULL
);