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"
48 #include "qemu/bitops.h"
50 #include "block/snapshot.h"
51 #include "block/qapi.h"
55 #define ETH_P_RARP 0x8035
57 #define ARP_HTYPE_ETH 0x0001
58 #define ARP_PTYPE_IP 0x0800
59 #define ARP_OP_REQUEST_REV 0x3
61 const unsigned int postcopy_ram_discard_version
= 0;
63 static bool skip_section_footers
;
65 static struct mig_cmd_args
{
66 ssize_t len
; /* -1 = variable */
69 [MIG_CMD_INVALID
] = { .len
= -1, .name
= "INVALID" },
70 [MIG_CMD_OPEN_RETURN_PATH
] = { .len
= 0, .name
= "OPEN_RETURN_PATH" },
71 [MIG_CMD_PING
] = { .len
= sizeof(uint32_t), .name
= "PING" },
72 [MIG_CMD_POSTCOPY_ADVISE
] = { .len
= 16, .name
= "POSTCOPY_ADVISE" },
73 [MIG_CMD_POSTCOPY_LISTEN
] = { .len
= 0, .name
= "POSTCOPY_LISTEN" },
74 [MIG_CMD_POSTCOPY_RUN
] = { .len
= 0, .name
= "POSTCOPY_RUN" },
75 [MIG_CMD_POSTCOPY_RAM_DISCARD
] = {
76 .len
= -1, .name
= "POSTCOPY_RAM_DISCARD" },
77 [MIG_CMD_MAX
] = { .len
= -1, .name
= "MAX" },
80 static int announce_self_create(uint8_t *buf
,
83 /* Ethernet header. */
84 memset(buf
, 0xff, 6); /* destination MAC addr */
85 memcpy(buf
+ 6, mac_addr
, 6); /* source MAC addr */
86 *(uint16_t *)(buf
+ 12) = htons(ETH_P_RARP
); /* ethertype */
89 *(uint16_t *)(buf
+ 14) = htons(ARP_HTYPE_ETH
); /* hardware addr space */
90 *(uint16_t *)(buf
+ 16) = htons(ARP_PTYPE_IP
); /* protocol addr space */
91 *(buf
+ 18) = 6; /* hardware addr length (ethernet) */
92 *(buf
+ 19) = 4; /* protocol addr length (IPv4) */
93 *(uint16_t *)(buf
+ 20) = htons(ARP_OP_REQUEST_REV
); /* opcode */
94 memcpy(buf
+ 22, mac_addr
, 6); /* source hw addr */
95 memset(buf
+ 28, 0x00, 4); /* source protocol addr */
96 memcpy(buf
+ 32, mac_addr
, 6); /* target hw addr */
97 memset(buf
+ 38, 0x00, 4); /* target protocol addr */
99 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
100 memset(buf
+ 42, 0x00, 18);
102 return 60; /* len (FCS will be added by hardware) */
105 static void qemu_announce_self_iter(NICState
*nic
, void *opaque
)
110 trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic
->conf
->macaddr
));
111 len
= announce_self_create(buf
, nic
->conf
->macaddr
.a
);
113 qemu_send_packet_raw(qemu_get_queue(nic
), buf
, len
);
117 static void qemu_announce_self_once(void *opaque
)
119 static int count
= SELF_ANNOUNCE_ROUNDS
;
120 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
122 qemu_foreach_nic(qemu_announce_self_iter
, NULL
);
125 /* delay 50ms, 150ms, 250ms, ... */
126 timer_mod(timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) +
127 self_announce_delay(count
));
134 void qemu_announce_self(void)
136 static QEMUTimer
*timer
;
137 timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, qemu_announce_self_once
, &timer
);
138 qemu_announce_self_once(&timer
);
141 /***********************************************************/
142 /* savevm/loadvm support */
144 static ssize_t
block_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
150 qemu_iovec_init_external(&qiov
, iov
, iovcnt
);
151 ret
= bdrv_writev_vmstate(opaque
, &qiov
, pos
);
159 static ssize_t
block_put_buffer(void *opaque
, const uint8_t *buf
,
160 int64_t pos
, size_t size
)
162 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
166 static ssize_t
block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
,
169 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
172 static int bdrv_fclose(void *opaque
)
174 return bdrv_flush(opaque
);
177 static const QEMUFileOps bdrv_read_ops
= {
178 .get_buffer
= block_get_buffer
,
182 static const QEMUFileOps bdrv_write_ops
= {
183 .put_buffer
= block_put_buffer
,
184 .writev_buffer
= block_writev_buffer
,
188 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
191 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
193 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
197 /* QEMUFile timer support.
198 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
201 void timer_put(QEMUFile
*f
, QEMUTimer
*ts
)
203 uint64_t expire_time
;
205 expire_time
= timer_expire_time_ns(ts
);
206 qemu_put_be64(f
, expire_time
);
209 void timer_get(QEMUFile
*f
, QEMUTimer
*ts
)
211 uint64_t expire_time
;
213 expire_time
= qemu_get_be64(f
);
214 if (expire_time
!= -1) {
215 timer_mod_ns(ts
, expire_time
);
222 /* VMState timer support.
223 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
226 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
233 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
239 const VMStateInfo vmstate_info_timer
= {
246 typedef struct CompatEntry
{
251 typedef struct SaveStateEntry
{
252 QTAILQ_ENTRY(SaveStateEntry
) entry
;
259 const VMStateDescription
*vmsd
;
265 typedef struct SaveState
{
266 QTAILQ_HEAD(, SaveStateEntry
) handlers
;
267 int global_section_id
;
268 bool skip_configuration
;
273 static SaveState savevm_state
= {
274 .handlers
= QTAILQ_HEAD_INITIALIZER(savevm_state
.handlers
),
275 .global_section_id
= 0,
276 .skip_configuration
= false,
279 void savevm_skip_configuration(void)
281 savevm_state
.skip_configuration
= true;
285 static void configuration_pre_save(void *opaque
)
287 SaveState
*state
= opaque
;
288 const char *current_name
= MACHINE_GET_CLASS(current_machine
)->name
;
290 state
->len
= strlen(current_name
);
291 state
->name
= current_name
;
294 static int configuration_post_load(void *opaque
, int version_id
)
296 SaveState
*state
= opaque
;
297 const char *current_name
= MACHINE_GET_CLASS(current_machine
)->name
;
299 if (strncmp(state
->name
, current_name
, state
->len
) != 0) {
300 error_report("Machine type received is '%s' and local is '%s'",
301 state
->name
, current_name
);
307 static const VMStateDescription vmstate_configuration
= {
308 .name
= "configuration",
310 .post_load
= configuration_post_load
,
311 .pre_save
= configuration_pre_save
,
312 .fields
= (VMStateField
[]) {
313 VMSTATE_UINT32(len
, SaveState
),
314 VMSTATE_VBUFFER_ALLOC_UINT32(name
, SaveState
, 0, NULL
, 0, len
),
315 VMSTATE_END_OF_LIST()
319 static void dump_vmstate_vmsd(FILE *out_file
,
320 const VMStateDescription
*vmsd
, int indent
,
323 static void dump_vmstate_vmsf(FILE *out_file
, const VMStateField
*field
,
326 fprintf(out_file
, "%*s{\n", indent
, "");
328 fprintf(out_file
, "%*s\"field\": \"%s\",\n", indent
, "", field
->name
);
329 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
331 fprintf(out_file
, "%*s\"field_exists\": %s,\n", indent
, "",
332 field
->field_exists
? "true" : "false");
333 fprintf(out_file
, "%*s\"size\": %zu", indent
, "", field
->size
);
334 if (field
->vmsd
!= NULL
) {
335 fprintf(out_file
, ",\n");
336 dump_vmstate_vmsd(out_file
, field
->vmsd
, indent
, false);
338 fprintf(out_file
, "\n%*s}", indent
- 2, "");
341 static void dump_vmstate_vmss(FILE *out_file
,
342 const VMStateDescription
**subsection
,
345 if (*subsection
!= NULL
) {
346 dump_vmstate_vmsd(out_file
, *subsection
, indent
, true);
350 static void dump_vmstate_vmsd(FILE *out_file
,
351 const VMStateDescription
*vmsd
, int indent
,
355 fprintf(out_file
, "%*s{\n", indent
, "");
357 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", "Description");
360 fprintf(out_file
, "%*s\"name\": \"%s\",\n", indent
, "", vmsd
->name
);
361 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
363 fprintf(out_file
, "%*s\"minimum_version_id\": %d", indent
, "",
364 vmsd
->minimum_version_id
);
365 if (vmsd
->fields
!= NULL
) {
366 const VMStateField
*field
= vmsd
->fields
;
369 fprintf(out_file
, ",\n%*s\"Fields\": [\n", indent
, "");
371 while (field
->name
!= NULL
) {
372 if (field
->flags
& VMS_MUST_EXIST
) {
373 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
378 fprintf(out_file
, ",\n");
380 dump_vmstate_vmsf(out_file
, field
, indent
+ 2);
384 fprintf(out_file
, "\n%*s]", indent
, "");
386 if (vmsd
->subsections
!= NULL
) {
387 const VMStateDescription
**subsection
= vmsd
->subsections
;
390 fprintf(out_file
, ",\n%*s\"Subsections\": [\n", indent
, "");
392 while (*subsection
!= NULL
) {
394 fprintf(out_file
, ",\n");
396 dump_vmstate_vmss(out_file
, subsection
, indent
+ 2);
400 fprintf(out_file
, "\n%*s]", indent
, "");
402 fprintf(out_file
, "\n%*s}", indent
- 2, "");
405 static void dump_machine_type(FILE *out_file
)
409 mc
= MACHINE_GET_CLASS(current_machine
);
411 fprintf(out_file
, " \"vmschkmachine\": {\n");
412 fprintf(out_file
, " \"Name\": \"%s\"\n", mc
->name
);
413 fprintf(out_file
, " },\n");
416 void dump_vmstate_json_to_file(FILE *out_file
)
421 fprintf(out_file
, "{\n");
422 dump_machine_type(out_file
);
425 list
= object_class_get_list(TYPE_DEVICE
, true);
426 for (elt
= list
; elt
; elt
= elt
->next
) {
427 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
437 fprintf(out_file
, ",\n");
439 name
= object_class_get_name(OBJECT_CLASS(dc
));
440 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", name
);
442 fprintf(out_file
, "%*s\"Name\": \"%s\",\n", indent
, "", name
);
443 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
444 dc
->vmsd
->version_id
);
445 fprintf(out_file
, "%*s\"minimum_version_id\": %d,\n", indent
, "",
446 dc
->vmsd
->minimum_version_id
);
448 dump_vmstate_vmsd(out_file
, dc
->vmsd
, indent
, false);
450 fprintf(out_file
, "\n%*s}", indent
- 2, "");
453 fprintf(out_file
, "\n}\n");
457 static int calculate_new_instance_id(const char *idstr
)
462 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
463 if (strcmp(idstr
, se
->idstr
) == 0
464 && instance_id
<= se
->instance_id
) {
465 instance_id
= se
->instance_id
+ 1;
471 static int calculate_compat_instance_id(const char *idstr
)
476 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
481 if (strcmp(idstr
, se
->compat
->idstr
) == 0
482 && instance_id
<= se
->compat
->instance_id
) {
483 instance_id
= se
->compat
->instance_id
+ 1;
489 /* TODO: Individual devices generally have very little idea about the rest
490 of the system, so instance_id should be removed/replaced.
491 Meanwhile pass -1 as instance_id if you do not already have a clearly
492 distinguishing id for all instances of your device class. */
493 int register_savevm_live(DeviceState
*dev
,
502 se
= g_new0(SaveStateEntry
, 1);
503 se
->version_id
= version_id
;
504 se
->section_id
= savevm_state
.global_section_id
++;
508 /* if this is a live_savem then set is_ram */
509 if (ops
->save_live_setup
!= NULL
) {
514 char *id
= qdev_get_dev_path(dev
);
516 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
517 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
520 se
->compat
= g_new0(CompatEntry
, 1);
521 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
522 se
->compat
->instance_id
= instance_id
== -1 ?
523 calculate_compat_instance_id(idstr
) : instance_id
;
527 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
529 if (instance_id
== -1) {
530 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
532 se
->instance_id
= instance_id
;
534 assert(!se
->compat
|| se
->instance_id
== 0);
535 /* add at the end of list */
536 QTAILQ_INSERT_TAIL(&savevm_state
.handlers
, se
, entry
);
540 int register_savevm(DeviceState
*dev
,
544 SaveStateHandler
*save_state
,
545 LoadStateHandler
*load_state
,
548 SaveVMHandlers
*ops
= g_new0(SaveVMHandlers
, 1);
549 ops
->save_state
= save_state
;
550 ops
->load_state
= load_state
;
551 return register_savevm_live(dev
, idstr
, instance_id
, version_id
,
555 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
557 SaveStateEntry
*se
, *new_se
;
561 char *path
= qdev_get_dev_path(dev
);
563 pstrcpy(id
, sizeof(id
), path
);
564 pstrcat(id
, sizeof(id
), "/");
568 pstrcat(id
, sizeof(id
), idstr
);
570 QTAILQ_FOREACH_SAFE(se
, &savevm_state
.handlers
, entry
, new_se
) {
571 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
572 QTAILQ_REMOVE(&savevm_state
.handlers
, se
, entry
);
580 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
581 const VMStateDescription
*vmsd
,
582 void *opaque
, int alias_id
,
583 int required_for_version
)
587 /* If this triggers, alias support can be dropped for the vmsd. */
588 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
590 se
= g_new0(SaveStateEntry
, 1);
591 se
->version_id
= vmsd
->version_id
;
592 se
->section_id
= savevm_state
.global_section_id
++;
595 se
->alias_id
= alias_id
;
598 char *id
= qdev_get_dev_path(dev
);
600 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
601 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
604 se
->compat
= g_new0(CompatEntry
, 1);
605 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
606 se
->compat
->instance_id
= instance_id
== -1 ?
607 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
611 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
613 if (instance_id
== -1) {
614 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
616 se
->instance_id
= instance_id
;
618 assert(!se
->compat
|| se
->instance_id
== 0);
619 /* add at the end of list */
620 QTAILQ_INSERT_TAIL(&savevm_state
.handlers
, se
, entry
);
624 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
627 SaveStateEntry
*se
, *new_se
;
629 QTAILQ_FOREACH_SAFE(se
, &savevm_state
.handlers
, entry
, new_se
) {
630 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
631 QTAILQ_REMOVE(&savevm_state
.handlers
, se
, entry
);
638 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
640 trace_vmstate_load(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
641 if (!se
->vmsd
) { /* Old style */
642 return se
->ops
->load_state(f
, se
->opaque
, version_id
);
644 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
647 static void vmstate_save_old_style(QEMUFile
*f
, SaveStateEntry
*se
, QJSON
*vmdesc
)
649 int64_t old_offset
, size
;
651 old_offset
= qemu_ftell_fast(f
);
652 se
->ops
->save_state(f
, se
->opaque
);
653 size
= qemu_ftell_fast(f
) - old_offset
;
656 json_prop_int(vmdesc
, "size", size
);
657 json_start_array(vmdesc
, "fields");
658 json_start_object(vmdesc
, NULL
);
659 json_prop_str(vmdesc
, "name", "data");
660 json_prop_int(vmdesc
, "size", size
);
661 json_prop_str(vmdesc
, "type", "buffer");
662 json_end_object(vmdesc
);
663 json_end_array(vmdesc
);
667 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
, QJSON
*vmdesc
)
669 trace_vmstate_save(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
671 vmstate_save_old_style(f
, se
, vmdesc
);
674 vmstate_save_state(f
, se
->vmsd
, se
->opaque
, vmdesc
);
677 void savevm_skip_section_footers(void)
679 skip_section_footers
= true;
683 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
685 static void save_section_header(QEMUFile
*f
, SaveStateEntry
*se
,
686 uint8_t section_type
)
688 qemu_put_byte(f
, section_type
);
689 qemu_put_be32(f
, se
->section_id
);
691 if (section_type
== QEMU_VM_SECTION_FULL
||
692 section_type
== QEMU_VM_SECTION_START
) {
694 size_t len
= strlen(se
->idstr
);
695 qemu_put_byte(f
, len
);
696 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
698 qemu_put_be32(f
, se
->instance_id
);
699 qemu_put_be32(f
, se
->version_id
);
704 * Write a footer onto device sections that catches cases misformatted device
707 static void save_section_footer(QEMUFile
*f
, SaveStateEntry
*se
)
709 if (!skip_section_footers
) {
710 qemu_put_byte(f
, QEMU_VM_SECTION_FOOTER
);
711 qemu_put_be32(f
, se
->section_id
);
716 * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the
717 * command and associated data.
719 * @f: File to send command on
720 * @command: Command type to send
721 * @len: Length of associated data
722 * @data: Data associated with command.
724 void qemu_savevm_command_send(QEMUFile
*f
,
725 enum qemu_vm_cmd command
,
729 trace_savevm_command_send(command
, len
);
730 qemu_put_byte(f
, QEMU_VM_COMMAND
);
731 qemu_put_be16(f
, (uint16_t)command
);
732 qemu_put_be16(f
, len
);
733 qemu_put_buffer(f
, data
, len
);
737 void qemu_savevm_send_ping(QEMUFile
*f
, uint32_t value
)
741 trace_savevm_send_ping(value
);
742 buf
= cpu_to_be32(value
);
743 qemu_savevm_command_send(f
, MIG_CMD_PING
, sizeof(value
), (uint8_t *)&buf
);
746 void qemu_savevm_send_open_return_path(QEMUFile
*f
)
748 trace_savevm_send_open_return_path();
749 qemu_savevm_command_send(f
, MIG_CMD_OPEN_RETURN_PATH
, 0, NULL
);
752 /* Send prior to any postcopy transfer */
753 void qemu_savevm_send_postcopy_advise(QEMUFile
*f
)
756 tmp
[0] = cpu_to_be64(getpagesize());
757 tmp
[1] = cpu_to_be64(1ul << qemu_target_page_bits());
759 trace_qemu_savevm_send_postcopy_advise();
760 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_ADVISE
, 16, (uint8_t *)tmp
);
763 /* Sent prior to starting the destination running in postcopy, discard pages
764 * that have already been sent but redirtied on the source.
765 * CMD_POSTCOPY_RAM_DISCARD consist of:
767 * byte Length of name field (not including 0)
768 * n x byte RAM block name
769 * byte 0 terminator (just for safety)
770 * n x Byte ranges within the named RAMBlock
771 * be64 Start of the range
774 * name: RAMBlock name that these entries are part of
775 * len: Number of page entries
776 * start_list: 'len' addresses
777 * length_list: 'len' addresses
780 void qemu_savevm_send_postcopy_ram_discard(QEMUFile
*f
, const char *name
,
782 uint64_t *start_list
,
783 uint64_t *length_list
)
788 size_t name_len
= strlen(name
);
790 trace_qemu_savevm_send_postcopy_ram_discard(name
, len
);
791 assert(name_len
< 256);
792 buf
= g_malloc0(1 + 1 + name_len
+ 1 + (8 + 8) * len
);
793 buf
[0] = postcopy_ram_discard_version
;
795 memcpy(buf
+ 2, name
, name_len
);
796 tmplen
= 2 + name_len
;
797 buf
[tmplen
++] = '\0';
799 for (t
= 0; t
< len
; t
++) {
800 cpu_to_be64w((uint64_t *)(buf
+ tmplen
), start_list
[t
]);
802 cpu_to_be64w((uint64_t *)(buf
+ tmplen
), length_list
[t
]);
805 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_RAM_DISCARD
, tmplen
, buf
);
809 /* Get the destination into a state where it can receive postcopy data. */
810 void qemu_savevm_send_postcopy_listen(QEMUFile
*f
)
812 trace_savevm_send_postcopy_listen();
813 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_LISTEN
, 0, NULL
);
816 /* Kick the destination into running */
817 void qemu_savevm_send_postcopy_run(QEMUFile
*f
)
819 trace_savevm_send_postcopy_run();
820 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_RUN
, 0, NULL
);
823 bool qemu_savevm_state_blocked(Error
**errp
)
827 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
828 if (se
->vmsd
&& se
->vmsd
->unmigratable
) {
829 error_setg(errp
, "State blocked by non-migratable device '%s'",
837 void qemu_savevm_state_header(QEMUFile
*f
)
839 trace_savevm_state_header();
840 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
841 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
843 if (!savevm_state
.skip_configuration
) {
844 qemu_put_byte(f
, QEMU_VM_CONFIGURATION
);
845 vmstate_save_state(f
, &vmstate_configuration
, &savevm_state
, 0);
850 void qemu_savevm_state_begin(QEMUFile
*f
,
851 const MigrationParams
*params
)
856 trace_savevm_state_begin();
857 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
858 if (!se
->ops
|| !se
->ops
->set_params
) {
861 se
->ops
->set_params(params
, se
->opaque
);
864 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
865 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
868 if (se
->ops
&& se
->ops
->is_active
) {
869 if (!se
->ops
->is_active(se
->opaque
)) {
873 save_section_header(f
, se
, QEMU_VM_SECTION_START
);
875 ret
= se
->ops
->save_live_setup(f
, se
->opaque
);
876 save_section_footer(f
, se
);
878 qemu_file_set_error(f
, ret
);
885 * this function has three return values:
886 * negative: there was one error, and we have -errno.
887 * 0 : We haven't finished, caller have to go again
888 * 1 : We have finished, we can go to complete phase
890 int qemu_savevm_state_iterate(QEMUFile
*f
)
895 trace_savevm_state_iterate();
896 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
897 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
900 if (se
->ops
&& se
->ops
->is_active
) {
901 if (!se
->ops
->is_active(se
->opaque
)) {
905 if (qemu_file_rate_limit(f
)) {
908 trace_savevm_section_start(se
->idstr
, se
->section_id
);
910 save_section_header(f
, se
, QEMU_VM_SECTION_PART
);
912 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
913 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
914 save_section_footer(f
, se
);
917 qemu_file_set_error(f
, ret
);
920 /* Do not proceed to the next vmstate before this one reported
921 completion of the current stage. This serializes the migration
922 and reduces the probability that a faster changing state is
923 synchronized over and over again. */
930 static bool should_send_vmdesc(void)
932 MachineState
*machine
= MACHINE(qdev_get_machine());
933 return !machine
->suppress_vmdesc
;
936 void qemu_savevm_state_complete_precopy(QEMUFile
*f
)
943 trace_savevm_state_complete_precopy();
945 cpu_synchronize_all_states();
947 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
948 if (!se
->ops
|| !se
->ops
->save_live_complete_precopy
) {
951 if (se
->ops
&& se
->ops
->is_active
) {
952 if (!se
->ops
->is_active(se
->opaque
)) {
956 trace_savevm_section_start(se
->idstr
, se
->section_id
);
958 save_section_header(f
, se
, QEMU_VM_SECTION_END
);
960 ret
= se
->ops
->save_live_complete_precopy(f
, se
->opaque
);
961 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
962 save_section_footer(f
, se
);
964 qemu_file_set_error(f
, ret
);
969 vmdesc
= qjson_new();
970 json_prop_int(vmdesc
, "page_size", TARGET_PAGE_SIZE
);
971 json_start_array(vmdesc
, "devices");
972 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
974 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
977 if (se
->vmsd
&& !vmstate_save_needed(se
->vmsd
, se
->opaque
)) {
978 trace_savevm_section_skip(se
->idstr
, se
->section_id
);
982 trace_savevm_section_start(se
->idstr
, se
->section_id
);
984 json_start_object(vmdesc
, NULL
);
985 json_prop_str(vmdesc
, "name", se
->idstr
);
986 json_prop_int(vmdesc
, "instance_id", se
->instance_id
);
988 save_section_header(f
, se
, QEMU_VM_SECTION_FULL
);
990 vmstate_save(f
, se
, vmdesc
);
992 json_end_object(vmdesc
);
993 trace_savevm_section_end(se
->idstr
, se
->section_id
, 0);
994 save_section_footer(f
, se
);
997 qemu_put_byte(f
, QEMU_VM_EOF
);
999 json_end_array(vmdesc
);
1000 qjson_finish(vmdesc
);
1001 vmdesc_len
= strlen(qjson_get_str(vmdesc
));
1003 if (should_send_vmdesc()) {
1004 qemu_put_byte(f
, QEMU_VM_VMDESCRIPTION
);
1005 qemu_put_be32(f
, vmdesc_len
);
1006 qemu_put_buffer(f
, (uint8_t *)qjson_get_str(vmdesc
), vmdesc_len
);
1008 object_unref(OBJECT(vmdesc
));
1013 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
1018 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1019 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
1022 if (se
->ops
&& se
->ops
->is_active
) {
1023 if (!se
->ops
->is_active(se
->opaque
)) {
1027 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
1032 void qemu_savevm_state_cleanup(void)
1036 trace_savevm_state_cleanup();
1037 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1038 if (se
->ops
&& se
->ops
->cleanup
) {
1039 se
->ops
->cleanup(se
->opaque
);
1044 static int qemu_savevm_state(QEMUFile
*f
, Error
**errp
)
1047 MigrationParams params
= {
1051 MigrationState
*ms
= migrate_init(¶ms
);
1054 if (qemu_savevm_state_blocked(errp
)) {
1058 qemu_mutex_unlock_iothread();
1059 qemu_savevm_state_header(f
);
1060 qemu_savevm_state_begin(f
, ¶ms
);
1061 qemu_mutex_lock_iothread();
1063 while (qemu_file_get_error(f
) == 0) {
1064 if (qemu_savevm_state_iterate(f
) > 0) {
1069 ret
= qemu_file_get_error(f
);
1071 qemu_savevm_state_complete_precopy(f
);
1072 ret
= qemu_file_get_error(f
);
1075 qemu_savevm_state_cleanup();
1076 error_setg_errno(errp
, -ret
, "Error while writing VM state");
1081 static int qemu_save_device_state(QEMUFile
*f
)
1085 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1086 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1088 cpu_synchronize_all_states();
1090 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1094 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1097 if (se
->vmsd
&& !vmstate_save_needed(se
->vmsd
, se
->opaque
)) {
1101 save_section_header(f
, se
, QEMU_VM_SECTION_FULL
);
1103 vmstate_save(f
, se
, NULL
);
1105 save_section_footer(f
, se
);
1108 qemu_put_byte(f
, QEMU_VM_EOF
);
1110 return qemu_file_get_error(f
);
1113 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1117 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1118 if (!strcmp(se
->idstr
, idstr
) &&
1119 (instance_id
== se
->instance_id
||
1120 instance_id
== se
->alias_id
))
1122 /* Migrating from an older version? */
1123 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
1124 if (!strcmp(se
->compat
->idstr
, idstr
) &&
1125 (instance_id
== se
->compat
->instance_id
||
1126 instance_id
== se
->alias_id
))
1133 enum LoadVMExitCodes
{
1134 /* Allow a command to quit all layers of nested loadvm loops */
1138 static int qemu_loadvm_state_main(QEMUFile
*f
, MigrationIncomingState
*mis
);
1140 /* ------ incoming postcopy messages ------ */
1141 /* 'advise' arrives before any transfers just to tell us that a postcopy
1142 * *might* happen - it might be skipped if precopy transferred everything
1145 static int loadvm_postcopy_handle_advise(MigrationIncomingState
*mis
)
1147 PostcopyState ps
= postcopy_state_set(POSTCOPY_INCOMING_ADVISE
);
1148 uint64_t remote_hps
, remote_tps
;
1150 trace_loadvm_postcopy_handle_advise();
1151 if (ps
!= POSTCOPY_INCOMING_NONE
) {
1152 error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps
);
1156 remote_hps
= qemu_get_be64(mis
->from_src_file
);
1157 if (remote_hps
!= getpagesize()) {
1159 * Some combinations of mismatch are probably possible but it gets
1160 * a bit more complicated. In particular we need to place whole
1161 * host pages on the dest at once, and we need to ensure that we
1162 * handle dirtying to make sure we never end up sending part of
1163 * a hostpage on it's own.
1165 error_report("Postcopy needs matching host page sizes (s=%d d=%d)",
1166 (int)remote_hps
, getpagesize());
1170 remote_tps
= qemu_get_be64(mis
->from_src_file
);
1171 if (remote_tps
!= (1ul << qemu_target_page_bits())) {
1173 * Again, some differences could be dealt with, but for now keep it
1176 error_report("Postcopy needs matching target page sizes (s=%d d=%d)",
1177 (int)remote_tps
, 1 << qemu_target_page_bits());
1184 /* After postcopy we will be told to throw some pages away since they're
1185 * dirty and will have to be demand fetched. Must happen before CPU is
1187 * There can be 0..many of these messages, each encoding multiple pages.
1189 static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState
*mis
,
1194 PostcopyState ps
= postcopy_state_get();
1196 trace_loadvm_postcopy_ram_handle_discard();
1199 case POSTCOPY_INCOMING_ADVISE
:
1201 tmp
= 0; /* TODO: later patch postcopy_ram_prepare_discard(mis); */
1207 case POSTCOPY_INCOMING_DISCARD
:
1208 /* Expected state */
1212 error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)",
1216 /* We're expecting a
1218 * a RAM ID string (length byte, name, 0 term)
1219 * then at least 1 16 byte chunk
1221 if (len
< (1 + 1 + 1 + 1 + 2 * 8)) {
1222 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len
);
1226 tmp
= qemu_get_byte(mis
->from_src_file
);
1227 if (tmp
!= postcopy_ram_discard_version
) {
1228 error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp
);
1232 if (!qemu_get_counted_string(mis
->from_src_file
, ramid
)) {
1233 error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID");
1236 tmp
= qemu_get_byte(mis
->from_src_file
);
1238 error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp
);
1242 len
-= 3 + strlen(ramid
);
1244 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len
);
1247 trace_loadvm_postcopy_ram_handle_discard_header(ramid
, len
);
1249 /* TODO - ram_discard_range gets added in a later patch
1250 uint64_t start_addr, block_length;
1251 start_addr = qemu_get_be64(mis->from_src_file);
1252 block_length = qemu_get_be64(mis->from_src_file);
1255 int ret = ram_discard_range(mis, ramid, start_addr,
1262 trace_loadvm_postcopy_ram_handle_discard_end();
1267 /* After this message we must be able to immediately receive postcopy data */
1268 static int loadvm_postcopy_handle_listen(MigrationIncomingState
*mis
)
1270 PostcopyState ps
= postcopy_state_set(POSTCOPY_INCOMING_LISTENING
);
1271 trace_loadvm_postcopy_handle_listen();
1272 if (ps
!= POSTCOPY_INCOMING_ADVISE
&& ps
!= POSTCOPY_INCOMING_DISCARD
) {
1273 error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps
);
1277 /* TODO start up the postcopy listening thread */
1281 /* After all discards we can start running and asking for pages */
1282 static int loadvm_postcopy_handle_run(MigrationIncomingState
*mis
)
1284 PostcopyState ps
= postcopy_state_set(POSTCOPY_INCOMING_RUNNING
);
1285 trace_loadvm_postcopy_handle_run();
1286 if (ps
!= POSTCOPY_INCOMING_LISTENING
) {
1287 error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps
);
1292 /* Hold onto your hats, starting the CPU */
1295 /* leave it paused and let management decide when to start the CPU */
1296 runstate_set(RUN_STATE_PAUSED
);
1303 * loadvm_process_command: Process an incoming 'QEMU_VM_COMMAND'
1305 * Returns: 0 on just a normal return
1306 * LOADVM_QUIT All good, but exit the loop
1307 * <0 error (in which case it will issue an error message).
1308 * @f: The stream to read the command data from.
1310 static int loadvm_process_command(QEMUFile
*f
)
1312 MigrationIncomingState
*mis
= migration_incoming_get_current();
1317 cmd
= qemu_get_be16(f
);
1318 len
= qemu_get_be16(f
);
1320 trace_loadvm_process_command(cmd
, len
);
1321 if (cmd
>= MIG_CMD_MAX
|| cmd
== MIG_CMD_INVALID
) {
1322 error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd
, len
);
1326 if (mig_cmd_args
[cmd
].len
!= -1 && mig_cmd_args
[cmd
].len
!= len
) {
1327 error_report("%s received with bad length - expecting %zu, got %d",
1328 mig_cmd_args
[cmd
].name
,
1329 (size_t)mig_cmd_args
[cmd
].len
, len
);
1334 case MIG_CMD_OPEN_RETURN_PATH
:
1335 if (mis
->to_src_file
) {
1336 error_report("CMD_OPEN_RETURN_PATH called when RP already open");
1337 /* Not really a problem, so don't give up */
1340 mis
->to_src_file
= qemu_file_get_return_path(f
);
1341 if (!mis
->to_src_file
) {
1342 error_report("CMD_OPEN_RETURN_PATH failed");
1348 tmp32
= qemu_get_be32(f
);
1349 trace_loadvm_process_command_ping(tmp32
);
1350 if (!mis
->to_src_file
) {
1351 error_report("CMD_PING (0x%x) received with no return path",
1355 migrate_send_rp_pong(mis
, tmp32
);
1358 case MIG_CMD_POSTCOPY_ADVISE
:
1359 return loadvm_postcopy_handle_advise(mis
);
1361 case MIG_CMD_POSTCOPY_LISTEN
:
1362 return loadvm_postcopy_handle_listen(mis
);
1364 case MIG_CMD_POSTCOPY_RUN
:
1365 return loadvm_postcopy_handle_run(mis
);
1367 case MIG_CMD_POSTCOPY_RAM_DISCARD
:
1368 return loadvm_postcopy_ram_handle_discard(mis
, len
);
1374 struct LoadStateEntry
{
1375 QLIST_ENTRY(LoadStateEntry
) entry
;
1382 * Read a footer off the wire and check that it matches the expected section
1384 * Returns: true if the footer was good
1385 * false if there is a problem (and calls error_report to say why)
1387 static bool check_section_footer(QEMUFile
*f
, LoadStateEntry
*le
)
1390 uint32_t read_section_id
;
1392 if (skip_section_footers
) {
1393 /* No footer to check */
1397 read_mark
= qemu_get_byte(f
);
1399 if (read_mark
!= QEMU_VM_SECTION_FOOTER
) {
1400 error_report("Missing section footer for %s", le
->se
->idstr
);
1404 read_section_id
= qemu_get_be32(f
);
1405 if (read_section_id
!= le
->section_id
) {
1406 error_report("Mismatched section id in footer for %s -"
1407 " read 0x%x expected 0x%x",
1408 le
->se
->idstr
, read_section_id
, le
->section_id
);
1416 void loadvm_free_handlers(MigrationIncomingState
*mis
)
1418 LoadStateEntry
*le
, *new_le
;
1420 QLIST_FOREACH_SAFE(le
, &mis
->loadvm_handlers
, entry
, new_le
) {
1421 QLIST_REMOVE(le
, entry
);
1426 static int qemu_loadvm_state_main(QEMUFile
*f
, MigrationIncomingState
*mis
)
1428 uint8_t section_type
;
1431 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
1432 uint32_t instance_id
, version_id
, section_id
;
1437 trace_qemu_loadvm_state_section(section_type
);
1438 switch (section_type
) {
1439 case QEMU_VM_SECTION_START
:
1440 case QEMU_VM_SECTION_FULL
:
1441 /* Read section start */
1442 section_id
= qemu_get_be32(f
);
1443 if (!qemu_get_counted_string(f
, idstr
)) {
1444 error_report("Unable to read ID string for section %u",
1448 instance_id
= qemu_get_be32(f
);
1449 version_id
= qemu_get_be32(f
);
1451 trace_qemu_loadvm_state_section_startfull(section_id
, idstr
,
1452 instance_id
, version_id
);
1453 /* Find savevm section */
1454 se
= find_se(idstr
, instance_id
);
1456 error_report("Unknown savevm section or instance '%s' %d",
1457 idstr
, instance_id
);
1461 /* Validate version */
1462 if (version_id
> se
->version_id
) {
1463 error_report("savevm: unsupported version %d for '%s' v%d",
1464 version_id
, idstr
, se
->version_id
);
1469 le
= g_malloc0(sizeof(*le
));
1472 le
->section_id
= section_id
;
1473 le
->version_id
= version_id
;
1474 QLIST_INSERT_HEAD(&mis
->loadvm_handlers
, le
, entry
);
1476 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1478 error_report("error while loading state for instance 0x%x of"
1479 " device '%s'", instance_id
, idstr
);
1482 if (!check_section_footer(f
, le
)) {
1486 case QEMU_VM_SECTION_PART
:
1487 case QEMU_VM_SECTION_END
:
1488 section_id
= qemu_get_be32(f
);
1490 trace_qemu_loadvm_state_section_partend(section_id
);
1491 QLIST_FOREACH(le
, &mis
->loadvm_handlers
, entry
) {
1492 if (le
->section_id
== section_id
) {
1497 error_report("Unknown savevm section %d", section_id
);
1501 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1503 error_report("error while loading state section id %d(%s)",
1504 section_id
, le
->se
->idstr
);
1507 if (!check_section_footer(f
, le
)) {
1511 case QEMU_VM_COMMAND
:
1512 ret
= loadvm_process_command(f
);
1513 trace_qemu_loadvm_state_section_command(ret
);
1514 if ((ret
< 0) || (ret
& LOADVM_QUIT
)) {
1519 error_report("Unknown savevm section type %d", section_type
);
1527 int qemu_loadvm_state(QEMUFile
*f
)
1529 MigrationIncomingState
*mis
= migration_incoming_get_current();
1530 Error
*local_err
= NULL
;
1534 if (qemu_savevm_state_blocked(&local_err
)) {
1535 error_report_err(local_err
);
1539 v
= qemu_get_be32(f
);
1540 if (v
!= QEMU_VM_FILE_MAGIC
) {
1541 error_report("Not a migration stream");
1545 v
= qemu_get_be32(f
);
1546 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
1547 error_report("SaveVM v2 format is obsolete and don't work anymore");
1550 if (v
!= QEMU_VM_FILE_VERSION
) {
1551 error_report("Unsupported migration stream version");
1555 if (!savevm_state
.skip_configuration
) {
1556 if (qemu_get_byte(f
) != QEMU_VM_CONFIGURATION
) {
1557 error_report("Configuration section missing");
1560 ret
= vmstate_load_state(f
, &vmstate_configuration
, &savevm_state
, 0);
1567 ret
= qemu_loadvm_state_main(f
, mis
);
1568 qemu_event_set(&mis
->main_thread_load_event
);
1570 trace_qemu_loadvm_state_post_main(ret
);
1573 ret
= qemu_file_get_error(f
);
1577 * Try to read in the VMDESC section as well, so that dumping tools that
1578 * intercept our migration stream have the chance to see it.
1581 /* We've got to be careful; if we don't read the data and just shut the fd
1582 * then the sender can error if we close while it's still sending.
1583 * We also mustn't read data that isn't there; some transports (RDMA)
1584 * will stall waiting for that data when the source has already closed.
1586 if (ret
== 0 && should_send_vmdesc()) {
1589 uint8_t section_type
= qemu_get_byte(f
);
1591 if (section_type
!= QEMU_VM_VMDESCRIPTION
) {
1592 error_report("Expected vmdescription section, but got %d",
1595 * It doesn't seem worth failing at this point since
1596 * we apparently have an otherwise valid VM state
1599 buf
= g_malloc(0x1000);
1600 size
= qemu_get_be32(f
);
1603 uint32_t read_chunk
= MIN(size
, 0x1000);
1604 qemu_get_buffer(f
, buf
, read_chunk
);
1611 cpu_synchronize_all_post_init();
1616 static BlockDriverState
*find_vmstate_bs(void)
1618 BlockDriverState
*bs
= NULL
;
1619 while ((bs
= bdrv_next(bs
))) {
1620 if (bdrv_can_snapshot(bs
)) {
1628 * Deletes snapshots of a given name in all opened images.
1630 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
1632 BlockDriverState
*bs
;
1633 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
1637 while ((bs
= bdrv_next(bs
))) {
1638 if (bdrv_can_snapshot(bs
) &&
1639 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0) {
1640 bdrv_snapshot_delete_by_id_or_name(bs
, name
, &err
);
1643 "Error while deleting snapshot on device '%s':"
1645 bdrv_get_device_name(bs
),
1646 error_get_pretty(err
));
1656 void hmp_savevm(Monitor
*mon
, const QDict
*qdict
)
1658 BlockDriverState
*bs
, *bs1
;
1659 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1662 int saved_vm_running
;
1663 uint64_t vm_state_size
;
1666 const char *name
= qdict_get_try_str(qdict
, "name");
1667 Error
*local_err
= NULL
;
1669 /* Verify if there is a device that doesn't support snapshots and is writable */
1671 while ((bs
= bdrv_next(bs
))) {
1673 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
1677 if (!bdrv_can_snapshot(bs
)) {
1678 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
1679 bdrv_get_device_name(bs
));
1684 bs
= find_vmstate_bs();
1686 monitor_printf(mon
, "No block device can accept snapshots\n");
1690 saved_vm_running
= runstate_is_running();
1692 ret
= global_state_store();
1694 monitor_printf(mon
, "Error saving global state\n");
1697 vm_stop(RUN_STATE_SAVE_VM
);
1699 memset(sn
, 0, sizeof(*sn
));
1701 /* fill auxiliary fields */
1702 qemu_gettimeofday(&tv
);
1703 sn
->date_sec
= tv
.tv_sec
;
1704 sn
->date_nsec
= tv
.tv_usec
* 1000;
1705 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1708 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1710 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1711 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1713 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1716 /* cast below needed for OpenBSD where tv_sec is still 'long' */
1717 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
1718 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
1721 /* Delete old snapshots of the same name */
1722 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
1726 /* save the VM state */
1727 f
= qemu_fopen_bdrv(bs
, 1);
1729 monitor_printf(mon
, "Could not open VM state file\n");
1732 ret
= qemu_savevm_state(f
, &local_err
);
1733 vm_state_size
= qemu_ftell(f
);
1736 monitor_printf(mon
, "%s\n", error_get_pretty(local_err
));
1737 error_free(local_err
);
1741 /* create the snapshots */
1744 while ((bs1
= bdrv_next(bs1
))) {
1745 if (bdrv_can_snapshot(bs1
)) {
1746 /* Write VM state size only to the image that contains the state */
1747 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1748 ret
= bdrv_snapshot_create(bs1
, sn
);
1750 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
1751 bdrv_get_device_name(bs1
));
1757 if (saved_vm_running
) {
1762 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
1765 int saved_vm_running
;
1768 saved_vm_running
= runstate_is_running();
1769 vm_stop(RUN_STATE_SAVE_VM
);
1770 global_state_store_running();
1772 f
= qemu_fopen(filename
, "wb");
1774 error_setg_file_open(errp
, errno
, filename
);
1777 ret
= qemu_save_device_state(f
);
1780 error_setg(errp
, QERR_IO_ERROR
);
1784 if (saved_vm_running
) {
1789 int load_vmstate(const char *name
)
1791 BlockDriverState
*bs
, *bs_vm_state
;
1792 QEMUSnapshotInfo sn
;
1796 bs_vm_state
= find_vmstate_bs();
1798 error_report("No block device supports snapshots");
1802 /* Don't even try to load empty VM states */
1803 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
1806 } else if (sn
.vm_state_size
== 0) {
1807 error_report("This is a disk-only snapshot. Revert to it offline "
1812 /* Verify if there is any device that doesn't support snapshots and is
1813 writable and check if the requested snapshot is available too. */
1815 while ((bs
= bdrv_next(bs
))) {
1817 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
1821 if (!bdrv_can_snapshot(bs
)) {
1822 error_report("Device '%s' is writable but does not support snapshots.",
1823 bdrv_get_device_name(bs
));
1827 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1829 error_report("Device '%s' does not have the requested snapshot '%s'",
1830 bdrv_get_device_name(bs
), name
);
1835 /* Flush all IO requests so they don't interfere with the new state. */
1839 while ((bs
= bdrv_next(bs
))) {
1840 if (bdrv_can_snapshot(bs
)) {
1841 ret
= bdrv_snapshot_goto(bs
, name
);
1843 error_report("Error %d while activating snapshot '%s' on '%s'",
1844 ret
, name
, bdrv_get_device_name(bs
));
1850 /* restore the VM state */
1851 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
1853 error_report("Could not open VM state file");
1857 qemu_system_reset(VMRESET_SILENT
);
1858 migration_incoming_state_new(f
);
1859 ret
= qemu_loadvm_state(f
);
1862 migration_incoming_state_destroy();
1864 error_report("Error %d while loading VM state", ret
);
1871 void hmp_delvm(Monitor
*mon
, const QDict
*qdict
)
1873 BlockDriverState
*bs
;
1875 const char *name
= qdict_get_str(qdict
, "name");
1877 if (!find_vmstate_bs()) {
1878 monitor_printf(mon
, "No block device supports snapshots\n");
1883 while ((bs
= bdrv_next(bs
))) {
1884 if (bdrv_can_snapshot(bs
)) {
1886 bdrv_snapshot_delete_by_id_or_name(bs
, name
, &err
);
1889 "Error while deleting snapshot on device '%s':"
1891 bdrv_get_device_name(bs
),
1892 error_get_pretty(err
));
1899 void hmp_info_snapshots(Monitor
*mon
, const QDict
*qdict
)
1901 BlockDriverState
*bs
, *bs1
;
1902 QEMUSnapshotInfo
*sn_tab
, *sn
, s
, *sn_info
= &s
;
1903 int nb_sns
, i
, ret
, available
;
1905 int *available_snapshots
;
1907 bs
= find_vmstate_bs();
1909 monitor_printf(mon
, "No available block device supports snapshots\n");
1913 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1915 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
1920 monitor_printf(mon
, "There is no snapshot available.\n");
1924 available_snapshots
= g_new0(int, nb_sns
);
1926 for (i
= 0; i
< nb_sns
; i
++) {
1931 while ((bs1
= bdrv_next(bs1
))) {
1932 if (bdrv_can_snapshot(bs1
) && bs1
!= bs
) {
1933 ret
= bdrv_snapshot_find(bs1
, sn_info
, sn
->id_str
);
1942 available_snapshots
[total
] = i
;
1948 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, NULL
);
1949 monitor_printf(mon
, "\n");
1950 for (i
= 0; i
< total
; i
++) {
1951 sn
= &sn_tab
[available_snapshots
[i
]];
1952 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, sn
);
1953 monitor_printf(mon
, "\n");
1956 monitor_printf(mon
, "There is no suitable snapshot available\n");
1960 g_free(available_snapshots
);
1964 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
1966 qemu_ram_set_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
,
1967 memory_region_name(mr
), dev
);
1970 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
1972 qemu_ram_unset_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
);
1975 void vmstate_register_ram_global(MemoryRegion
*mr
)
1977 vmstate_register_ram(mr
, NULL
);