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
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
36 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
45 #if defined(__NetBSD__)
46 #include <net/if_tap.h>
49 #include <linux/if_tun.h>
51 #include <arpa/inet.h>
54 #include <sys/select.h>
57 #if defined(__FreeBSD__) || defined(__DragonFly__)
62 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63 #include <freebsd/stdlib.h>
68 #include <linux/rtc.h>
76 #include <sys/timeb.h>
78 #define getopt_long_only getopt_long
79 #define memalign(align, size) malloc(size)
82 #include "qemu-common.h"
87 #include "qemu-timer.h"
88 #include "qemu-char.h"
90 #include "audio/audio.h"
91 #include "migration.h"
92 #include "qemu_socket.h"
93 #include "qemu-queue.h"
95 /* point to the block driver where the snapshots are managed */
96 static BlockDriverState
*bs_snapshots
;
98 #define SELF_ANNOUNCE_ROUNDS 5
101 #define ETH_P_RARP 0x0835
103 #define ARP_HTYPE_ETH 0x0001
104 #define ARP_PTYPE_IP 0x0800
105 #define ARP_OP_REQUEST_REV 0x3
107 static int announce_self_create(uint8_t *buf
,
110 /* Ethernet header. */
111 memset(buf
, 0xff, 6); /* destination MAC addr */
112 memcpy(buf
+ 6, mac_addr
, 6); /* source MAC addr */
113 *(uint16_t *)(buf
+ 12) = htons(ETH_P_RARP
); /* ethertype */
116 *(uint16_t *)(buf
+ 14) = htons(ARP_HTYPE_ETH
); /* hardware addr space */
117 *(uint16_t *)(buf
+ 16) = htons(ARP_PTYPE_IP
); /* protocol addr space */
118 *(buf
+ 18) = 6; /* hardware addr length (ethernet) */
119 *(buf
+ 19) = 4; /* protocol addr length (IPv4) */
120 *(uint16_t *)(buf
+ 20) = htons(ARP_OP_REQUEST_REV
); /* opcode */
121 memcpy(buf
+ 22, mac_addr
, 6); /* source hw addr */
122 memset(buf
+ 28, 0x00, 4); /* source protocol addr */
123 memcpy(buf
+ 32, mac_addr
, 6); /* target hw addr */
124 memset(buf
+ 38, 0x00, 4); /* target protocol addr */
126 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
127 memset(buf
+ 42, 0x00, 18);
129 return 60; /* len (FCS will be added by hardware) */
132 static void qemu_announce_self_once(void *opaque
)
138 static int count
= SELF_ANNOUNCE_ROUNDS
;
139 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
141 for (i
= 0; i
< MAX_NICS
; i
++) {
142 if (!nd_table
[i
].used
)
144 len
= announce_self_create(buf
, nd_table
[i
].macaddr
);
145 vlan
= nd_table
[i
].vlan
;
146 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
147 vc
->receive(vc
, buf
, len
);
151 /* delay 50ms, 150ms, 250ms, ... */
152 qemu_mod_timer(timer
, qemu_get_clock(rt_clock
) +
153 50 + (SELF_ANNOUNCE_ROUNDS
- count
- 1) * 100);
155 qemu_del_timer(timer
);
156 qemu_free_timer(timer
);
160 void qemu_announce_self(void)
162 static QEMUTimer
*timer
;
163 timer
= qemu_new_timer(rt_clock
, qemu_announce_self_once
, &timer
);
164 qemu_announce_self_once(&timer
);
167 /***********************************************************/
168 /* savevm/loadvm support */
170 #define IO_BUF_SIZE 32768
173 QEMUFilePutBufferFunc
*put_buffer
;
174 QEMUFileGetBufferFunc
*get_buffer
;
175 QEMUFileCloseFunc
*close
;
176 QEMUFileRateLimit
*rate_limit
;
177 QEMUFileSetRateLimit
*set_rate_limit
;
181 int64_t buf_offset
; /* start of buffer when writing, end of buffer
184 int buf_size
; /* 0 when writing */
185 uint8_t buf
[IO_BUF_SIZE
];
190 typedef struct QEMUFileStdio
196 typedef struct QEMUFileSocket
202 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
204 QEMUFileSocket
*s
= opaque
;
208 len
= recv(s
->fd
, (void *)buf
, size
, 0);
209 } while (len
== -1 && socket_error() == EINTR
);
212 len
= -socket_error();
217 static int socket_close(void *opaque
)
219 QEMUFileSocket
*s
= opaque
;
224 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
226 QEMUFileStdio
*s
= opaque
;
227 return fwrite(buf
, 1, size
, s
->stdio_file
);
230 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
232 QEMUFileStdio
*s
= opaque
;
233 FILE *fp
= s
->stdio_file
;
238 bytes
= fread(buf
, 1, size
, fp
);
239 } while ((bytes
== 0) && ferror(fp
) && (errno
== EINTR
));
243 static int stdio_pclose(void *opaque
)
245 QEMUFileStdio
*s
= opaque
;
246 pclose(s
->stdio_file
);
251 static int stdio_fclose(void *opaque
)
253 QEMUFileStdio
*s
= opaque
;
254 fclose(s
->stdio_file
);
259 QEMUFile
*qemu_popen(FILE *stdio_file
, const char *mode
)
263 if (stdio_file
== NULL
|| mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
264 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
268 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
270 s
->stdio_file
= stdio_file
;
273 s
->file
= qemu_fopen_ops(s
, NULL
, stdio_get_buffer
, stdio_pclose
, NULL
, NULL
);
275 s
->file
= qemu_fopen_ops(s
, stdio_put_buffer
, NULL
, stdio_pclose
, NULL
, NULL
);
280 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
284 popen_file
= popen(command
, mode
);
285 if(popen_file
== NULL
) {
289 return qemu_popen(popen_file
, mode
);
292 int qemu_stdio_fd(QEMUFile
*f
)
297 p
= (QEMUFileStdio
*)f
->opaque
;
298 fd
= fileno(p
->stdio_file
);
303 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
308 (mode
[0] != 'r' && mode
[0] != 'w') ||
309 mode
[1] != 'b' || mode
[2] != 0) {
310 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
314 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
315 s
->stdio_file
= fdopen(fd
, mode
);
320 s
->file
= qemu_fopen_ops(s
, NULL
, stdio_get_buffer
, stdio_fclose
, NULL
, NULL
);
322 s
->file
= qemu_fopen_ops(s
, stdio_put_buffer
, NULL
, stdio_fclose
, NULL
, NULL
);
331 QEMUFile
*qemu_fopen_socket(int fd
)
333 QEMUFileSocket
*s
= qemu_mallocz(sizeof(QEMUFileSocket
));
336 s
->file
= qemu_fopen_ops(s
, NULL
, socket_get_buffer
, socket_close
, NULL
, NULL
);
340 static int file_put_buffer(void *opaque
, const uint8_t *buf
,
341 int64_t pos
, int size
)
343 QEMUFileStdio
*s
= opaque
;
344 fseek(s
->stdio_file
, pos
, SEEK_SET
);
345 fwrite(buf
, 1, size
, s
->stdio_file
);
349 static int file_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
351 QEMUFileStdio
*s
= opaque
;
352 fseek(s
->stdio_file
, pos
, SEEK_SET
);
353 return fread(buf
, 1, size
, s
->stdio_file
);
356 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
361 (mode
[0] != 'r' && mode
[0] != 'w') ||
362 mode
[1] != 'b' || mode
[2] != 0) {
363 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
367 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
369 s
->stdio_file
= fopen(filename
, mode
);
374 s
->file
= qemu_fopen_ops(s
, file_put_buffer
, NULL
, stdio_fclose
, NULL
, NULL
);
376 s
->file
= qemu_fopen_ops(s
, NULL
, file_get_buffer
, stdio_fclose
, NULL
, NULL
);
384 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
385 int64_t pos
, int size
)
387 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
391 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
393 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
396 static int bdrv_fclose(void *opaque
)
401 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
404 return qemu_fopen_ops(bs
, block_put_buffer
, NULL
, bdrv_fclose
, NULL
, NULL
);
405 return qemu_fopen_ops(bs
, NULL
, block_get_buffer
, bdrv_fclose
, NULL
, NULL
);
408 QEMUFile
*qemu_fopen_ops(void *opaque
, QEMUFilePutBufferFunc
*put_buffer
,
409 QEMUFileGetBufferFunc
*get_buffer
,
410 QEMUFileCloseFunc
*close
,
411 QEMUFileRateLimit
*rate_limit
,
412 QEMUFileSetRateLimit
*set_rate_limit
)
416 f
= qemu_mallocz(sizeof(QEMUFile
));
419 f
->put_buffer
= put_buffer
;
420 f
->get_buffer
= get_buffer
;
422 f
->rate_limit
= rate_limit
;
423 f
->set_rate_limit
= set_rate_limit
;
429 int qemu_file_has_error(QEMUFile
*f
)
434 void qemu_file_set_error(QEMUFile
*f
)
439 void qemu_fflush(QEMUFile
*f
)
444 if (f
->is_write
&& f
->buf_index
> 0) {
447 len
= f
->put_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, f
->buf_index
);
449 f
->buf_offset
+= f
->buf_index
;
456 static void qemu_fill_buffer(QEMUFile
*f
)
466 len
= f
->get_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, IO_BUF_SIZE
);
470 f
->buf_offset
+= len
;
471 } else if (len
!= -EAGAIN
)
475 int qemu_fclose(QEMUFile
*f
)
480 ret
= f
->close(f
->opaque
);
485 void qemu_file_put_notify(QEMUFile
*f
)
487 f
->put_buffer(f
->opaque
, NULL
, 0, 0);
490 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
494 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
496 "Attempted to write to buffer while read buffer is not empty\n");
500 while (!f
->has_error
&& size
> 0) {
501 l
= IO_BUF_SIZE
- f
->buf_index
;
504 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
509 if (f
->buf_index
>= IO_BUF_SIZE
)
514 void qemu_put_byte(QEMUFile
*f
, int v
)
516 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
518 "Attempted to write to buffer while read buffer is not empty\n");
522 f
->buf
[f
->buf_index
++] = v
;
524 if (f
->buf_index
>= IO_BUF_SIZE
)
528 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
537 l
= f
->buf_size
- f
->buf_index
;
540 l
= f
->buf_size
- f
->buf_index
;
546 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
554 int qemu_get_byte(QEMUFile
*f
)
559 if (f
->buf_index
>= f
->buf_size
) {
561 if (f
->buf_index
>= f
->buf_size
)
564 return f
->buf
[f
->buf_index
++];
567 int64_t qemu_ftell(QEMUFile
*f
)
569 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
572 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
574 if (whence
== SEEK_SET
) {
576 } else if (whence
== SEEK_CUR
) {
577 pos
+= qemu_ftell(f
);
579 /* SEEK_END not supported */
593 int qemu_file_rate_limit(QEMUFile
*f
)
596 return f
->rate_limit(f
->opaque
);
601 size_t qemu_file_set_rate_limit(QEMUFile
*f
, size_t new_rate
)
603 /* any failed or completed migration keeps its state to allow probing of
604 * migration data, but has no associated file anymore */
605 if (f
&& f
->set_rate_limit
)
606 return f
->set_rate_limit(f
->opaque
, new_rate
);
611 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
613 qemu_put_byte(f
, v
>> 8);
617 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
619 qemu_put_byte(f
, v
>> 24);
620 qemu_put_byte(f
, v
>> 16);
621 qemu_put_byte(f
, v
>> 8);
625 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
627 qemu_put_be32(f
, v
>> 32);
631 unsigned int qemu_get_be16(QEMUFile
*f
)
634 v
= qemu_get_byte(f
) << 8;
635 v
|= qemu_get_byte(f
);
639 unsigned int qemu_get_be32(QEMUFile
*f
)
642 v
= qemu_get_byte(f
) << 24;
643 v
|= qemu_get_byte(f
) << 16;
644 v
|= qemu_get_byte(f
) << 8;
645 v
|= qemu_get_byte(f
);
649 uint64_t qemu_get_be64(QEMUFile
*f
)
652 v
= (uint64_t)qemu_get_be32(f
) << 32;
653 v
|= qemu_get_be32(f
);
659 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
666 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
672 const VMStateInfo vmstate_info_int8
= {
680 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
683 qemu_get_sbe16s(f
, v
);
687 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
690 qemu_put_sbe16s(f
, v
);
693 const VMStateInfo vmstate_info_int16
= {
701 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
704 qemu_get_sbe32s(f
, v
);
708 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
711 qemu_put_sbe32s(f
, v
);
714 const VMStateInfo vmstate_info_int32
= {
720 /* 32 bit int. See that the received value is the same than the one
723 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
727 qemu_get_sbe32s(f
, &v2
);
734 const VMStateInfo vmstate_info_int32_equal
= {
735 .name
= "int32 equal",
736 .get
= get_int32_equal
,
740 /* 32 bit int. See that the received value is the less or the same
741 than the one in the field */
743 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
747 qemu_get_sbe32s(f
, &new);
754 const VMStateInfo vmstate_info_int32_le
= {
755 .name
= "int32 equal",
762 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
765 qemu_get_sbe64s(f
, v
);
769 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
772 qemu_put_sbe64s(f
, v
);
775 const VMStateInfo vmstate_info_int64
= {
781 /* 8 bit unsigned int */
783 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
790 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
796 const VMStateInfo vmstate_info_uint8
= {
802 /* 16 bit unsigned int */
804 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
807 qemu_get_be16s(f
, v
);
811 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
814 qemu_put_be16s(f
, v
);
817 const VMStateInfo vmstate_info_uint16
= {
823 /* 32 bit unsigned int */
825 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
828 qemu_get_be32s(f
, v
);
832 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
835 qemu_put_be32s(f
, v
);
838 const VMStateInfo vmstate_info_uint32
= {
844 /* 64 bit unsigned int */
846 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
849 qemu_get_be64s(f
, v
);
853 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
856 qemu_put_be64s(f
, v
);
859 const VMStateInfo vmstate_info_uint64
= {
865 /* 8 bit int. See that the received value is the same than the one
868 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
879 const VMStateInfo vmstate_info_uint8_equal
= {
880 .name
= "uint8 equal",
881 .get
= get_uint8_equal
,
885 /* 16 bit unsigned int int. See that the received value is the same than the one
888 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
892 qemu_get_be16s(f
, &v2
);
899 const VMStateInfo vmstate_info_uint16_equal
= {
900 .name
= "uint16 equal",
901 .get
= get_uint16_equal
,
907 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
910 qemu_get_timer(f
, v
);
914 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
917 qemu_put_timer(f
, v
);
920 const VMStateInfo vmstate_info_timer
= {
926 /* uint8_t buffers */
928 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
931 qemu_get_buffer(f
, v
, size
);
935 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
938 qemu_put_buffer(f
, v
, size
);
941 const VMStateInfo vmstate_info_buffer
= {
947 /* unused buffers: space that was used for some fields that are
948 not usefull anymore */
950 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
952 qemu_fseek(f
, size
, SEEK_CUR
);
956 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
958 qemu_fseek(f
, size
, SEEK_CUR
);
961 const VMStateInfo vmstate_info_unused_buffer
= {
962 .name
= "unused_buffer",
963 .get
= get_unused_buffer
,
964 .put
= put_unused_buffer
,
967 typedef struct SaveStateEntry
{
968 QTAILQ_ENTRY(SaveStateEntry
) entry
;
973 SaveLiveStateHandler
*save_live_state
;
974 SaveStateHandler
*save_state
;
975 LoadStateHandler
*load_state
;
976 const VMStateDescription
*vmsd
;
980 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
981 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
982 static int global_section_id
;
984 static int calculate_new_instance_id(const char *idstr
)
989 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
990 if (strcmp(idstr
, se
->idstr
) == 0
991 && instance_id
<= se
->instance_id
) {
992 instance_id
= se
->instance_id
+ 1;
998 /* TODO: Individual devices generally have very little idea about the rest
999 of the system, so instance_id should be removed/replaced.
1000 Meanwhile pass -1 as instance_id if you do not already have a clearly
1001 distinguishing id for all instances of your device class. */
1002 int register_savevm_live(const char *idstr
,
1005 SaveLiveStateHandler
*save_live_state
,
1006 SaveStateHandler
*save_state
,
1007 LoadStateHandler
*load_state
,
1012 se
= qemu_malloc(sizeof(SaveStateEntry
));
1013 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1014 se
->version_id
= version_id
;
1015 se
->section_id
= global_section_id
++;
1016 se
->save_live_state
= save_live_state
;
1017 se
->save_state
= save_state
;
1018 se
->load_state
= load_state
;
1019 se
->opaque
= opaque
;
1022 if (instance_id
== -1) {
1023 se
->instance_id
= calculate_new_instance_id(idstr
);
1025 se
->instance_id
= instance_id
;
1027 /* add at the end of list */
1028 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1032 int register_savevm(const char *idstr
,
1035 SaveStateHandler
*save_state
,
1036 LoadStateHandler
*load_state
,
1039 return register_savevm_live(idstr
, instance_id
, version_id
,
1040 NULL
, save_state
, load_state
, opaque
);
1043 void unregister_savevm(const char *idstr
, void *opaque
)
1045 SaveStateEntry
*se
, *new_se
;
1047 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1048 if (strcmp(se
->idstr
, idstr
) == 0 && se
->opaque
== opaque
) {
1049 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1055 int vmstate_register(int instance_id
, const VMStateDescription
*vmsd
,
1060 se
= qemu_malloc(sizeof(SaveStateEntry
));
1061 pstrcpy(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1062 se
->version_id
= vmsd
->version_id
;
1063 se
->section_id
= global_section_id
++;
1064 se
->save_live_state
= NULL
;
1065 se
->save_state
= NULL
;
1066 se
->load_state
= NULL
;
1067 se
->opaque
= opaque
;
1070 if (instance_id
== -1) {
1071 se
->instance_id
= calculate_new_instance_id(vmsd
->name
);
1073 se
->instance_id
= instance_id
;
1075 /* add at the end of list */
1076 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1080 void vmstate_unregister(const VMStateDescription
*vmsd
, void *opaque
)
1082 SaveStateEntry
*se
, *new_se
;
1084 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1085 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1086 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1092 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1093 void *opaque
, int version_id
)
1095 VMStateField
*field
= vmsd
->fields
;
1097 if (version_id
> vmsd
->version_id
) {
1100 if (version_id
< vmsd
->minimum_version_id_old
) {
1103 if (version_id
< vmsd
->minimum_version_id
) {
1104 return vmsd
->load_state_old(f
, opaque
, version_id
);
1106 if (vmsd
->pre_load
) {
1107 int ret
= vmsd
->pre_load(opaque
);
1111 while(field
->name
) {
1112 if ((field
->field_exists
&&
1113 field
->field_exists(opaque
, version_id
)) ||
1114 (!field
->field_exists
&&
1115 field
->version_id
<= version_id
)) {
1116 void *base_addr
= opaque
+ field
->offset
;
1117 int ret
, i
, n_elems
= 1;
1119 if (field
->flags
& VMS_ARRAY
) {
1120 n_elems
= field
->num
;
1121 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1122 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1123 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1124 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1126 if (field
->flags
& VMS_POINTER
) {
1127 base_addr
= *(void **)base_addr
;
1129 for (i
= 0; i
< n_elems
; i
++) {
1130 void *addr
= base_addr
+ field
->size
* i
;
1132 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1133 addr
= *(void **)addr
;
1135 if (field
->flags
& VMS_STRUCT
) {
1136 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1138 ret
= field
->info
->get(f
, addr
, field
->size
);
1148 if (vmsd
->post_load
) {
1149 return vmsd
->post_load(opaque
, version_id
);
1154 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1157 VMStateField
*field
= vmsd
->fields
;
1159 if (vmsd
->pre_save
) {
1160 vmsd
->pre_save(opaque
);
1162 while(field
->name
) {
1163 if (!field
->field_exists
||
1164 field
->field_exists(opaque
, vmsd
->version_id
)) {
1165 void *base_addr
= opaque
+ field
->offset
;
1168 if (field
->flags
& VMS_ARRAY
) {
1169 n_elems
= field
->num
;
1170 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1171 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1172 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1173 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1175 if (field
->flags
& VMS_POINTER
) {
1176 base_addr
= *(void **)base_addr
;
1178 for (i
= 0; i
< n_elems
; i
++) {
1179 void *addr
= base_addr
+ field
->size
* i
;
1181 if (field
->flags
& VMS_STRUCT
) {
1182 vmstate_save_state(f
, field
->vmsd
, addr
);
1184 field
->info
->put(f
, addr
, field
->size
);
1190 if (vmsd
->post_save
) {
1191 vmsd
->post_save(opaque
);
1195 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1197 if (!se
->vmsd
) { /* Old style */
1198 return se
->load_state(f
, se
->opaque
, version_id
);
1200 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1203 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1205 if (!se
->vmsd
) { /* Old style */
1206 se
->save_state(f
, se
->opaque
);
1209 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1212 #define QEMU_VM_FILE_MAGIC 0x5145564d
1213 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1214 #define QEMU_VM_FILE_VERSION 0x00000003
1216 #define QEMU_VM_EOF 0x00
1217 #define QEMU_VM_SECTION_START 0x01
1218 #define QEMU_VM_SECTION_PART 0x02
1219 #define QEMU_VM_SECTION_END 0x03
1220 #define QEMU_VM_SECTION_FULL 0x04
1222 int qemu_savevm_state_begin(QEMUFile
*f
)
1226 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1227 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1229 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1232 if (se
->save_live_state
== NULL
)
1236 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1237 qemu_put_be32(f
, se
->section_id
);
1240 len
= strlen(se
->idstr
);
1241 qemu_put_byte(f
, len
);
1242 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1244 qemu_put_be32(f
, se
->instance_id
);
1245 qemu_put_be32(f
, se
->version_id
);
1247 se
->save_live_state(f
, QEMU_VM_SECTION_START
, se
->opaque
);
1250 if (qemu_file_has_error(f
))
1256 int qemu_savevm_state_iterate(QEMUFile
*f
)
1261 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1262 if (se
->save_live_state
== NULL
)
1266 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1267 qemu_put_be32(f
, se
->section_id
);
1269 ret
&= !!se
->save_live_state(f
, QEMU_VM_SECTION_PART
, se
->opaque
);
1275 if (qemu_file_has_error(f
))
1281 int qemu_savevm_state_complete(QEMUFile
*f
)
1285 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1286 if (se
->save_live_state
== NULL
)
1290 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1291 qemu_put_be32(f
, se
->section_id
);
1293 se
->save_live_state(f
, QEMU_VM_SECTION_END
, se
->opaque
);
1296 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1299 if (se
->save_state
== NULL
&& se
->vmsd
== NULL
)
1303 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1304 qemu_put_be32(f
, se
->section_id
);
1307 len
= strlen(se
->idstr
);
1308 qemu_put_byte(f
, len
);
1309 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1311 qemu_put_be32(f
, se
->instance_id
);
1312 qemu_put_be32(f
, se
->version_id
);
1314 vmstate_save(f
, se
);
1317 qemu_put_byte(f
, QEMU_VM_EOF
);
1319 if (qemu_file_has_error(f
))
1325 int qemu_savevm_state(QEMUFile
*f
)
1327 int saved_vm_running
;
1330 saved_vm_running
= vm_running
;
1335 ret
= qemu_savevm_state_begin(f
);
1340 ret
= qemu_savevm_state_iterate(f
);
1345 ret
= qemu_savevm_state_complete(f
);
1348 if (qemu_file_has_error(f
))
1351 if (!ret
&& saved_vm_running
)
1357 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1361 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1362 if (!strcmp(se
->idstr
, idstr
) &&
1363 instance_id
== se
->instance_id
)
1369 typedef struct LoadStateEntry
{
1370 QLIST_ENTRY(LoadStateEntry
) entry
;
1376 int qemu_loadvm_state(QEMUFile
*f
)
1378 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
1379 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
1380 LoadStateEntry
*le
, *new_le
;
1381 uint8_t section_type
;
1385 v
= qemu_get_be32(f
);
1386 if (v
!= QEMU_VM_FILE_MAGIC
)
1389 v
= qemu_get_be32(f
);
1390 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
1391 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
1394 if (v
!= QEMU_VM_FILE_VERSION
)
1397 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
1398 uint32_t instance_id
, version_id
, section_id
;
1403 switch (section_type
) {
1404 case QEMU_VM_SECTION_START
:
1405 case QEMU_VM_SECTION_FULL
:
1406 /* Read section start */
1407 section_id
= qemu_get_be32(f
);
1408 len
= qemu_get_byte(f
);
1409 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
1411 instance_id
= qemu_get_be32(f
);
1412 version_id
= qemu_get_be32(f
);
1414 /* Find savevm section */
1415 se
= find_se(idstr
, instance_id
);
1417 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
1422 /* Validate version */
1423 if (version_id
> se
->version_id
) {
1424 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
1425 version_id
, idstr
, se
->version_id
);
1431 le
= qemu_mallocz(sizeof(*le
));
1434 le
->section_id
= section_id
;
1435 le
->version_id
= version_id
;
1436 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
1438 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1440 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1441 instance_id
, idstr
);
1445 case QEMU_VM_SECTION_PART
:
1446 case QEMU_VM_SECTION_END
:
1447 section_id
= qemu_get_be32(f
);
1449 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
1450 if (le
->section_id
== section_id
) {
1455 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
1460 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1462 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
1468 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
1477 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
1478 QLIST_REMOVE(le
, entry
);
1482 if (qemu_file_has_error(f
))
1488 /* device can contain snapshots */
1489 static int bdrv_can_snapshot(BlockDriverState
*bs
)
1492 !bdrv_is_removable(bs
) &&
1493 !bdrv_is_read_only(bs
));
1496 /* device must be snapshots in order to have a reliable snapshot */
1497 static int bdrv_has_snapshot(BlockDriverState
*bs
)
1500 !bdrv_is_removable(bs
) &&
1501 !bdrv_is_read_only(bs
));
1504 static BlockDriverState
*get_bs_snapshots(void)
1506 BlockDriverState
*bs
;
1510 return bs_snapshots
;
1511 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1513 if (bdrv_can_snapshot(bs
))
1522 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
1525 QEMUSnapshotInfo
*sn_tab
, *sn
;
1529 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1532 for(i
= 0; i
< nb_sns
; i
++) {
1534 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
1544 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
1547 BlockDriverState
*bs
, *bs1
;
1548 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1549 int must_delete
, ret
;
1551 int saved_vm_running
;
1552 uint32_t vm_state_size
;
1558 const char *name
= qdict_get_try_str(qdict
, "name");
1560 bs
= get_bs_snapshots();
1562 monitor_printf(mon
, "No block device can accept snapshots\n");
1566 /* ??? Should this occur after vm_stop? */
1569 saved_vm_running
= vm_running
;
1574 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1579 memset(sn
, 0, sizeof(*sn
));
1581 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1582 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1585 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1588 /* fill auxiliary fields */
1591 sn
->date_sec
= tb
.time
;
1592 sn
->date_nsec
= tb
.millitm
* 1000000;
1594 gettimeofday(&tv
, NULL
);
1595 sn
->date_sec
= tv
.tv_sec
;
1596 sn
->date_nsec
= tv
.tv_usec
* 1000;
1598 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
1600 /* save the VM state */
1601 f
= qemu_fopen_bdrv(bs
, 1);
1603 monitor_printf(mon
, "Could not open VM state file\n");
1606 ret
= qemu_savevm_state(f
);
1607 vm_state_size
= qemu_ftell(f
);
1610 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
1614 /* create the snapshots */
1616 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1618 if (bdrv_has_snapshot(bs1
)) {
1620 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
1623 "Error while deleting snapshot on '%s'\n",
1624 bdrv_get_device_name(bs1
));
1627 /* Write VM state size only to the image that contains the state */
1628 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1629 ret
= bdrv_snapshot_create(bs1
, sn
);
1631 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
1632 bdrv_get_device_name(bs1
));
1638 if (saved_vm_running
)
1642 int load_vmstate(Monitor
*mon
, const char *name
)
1645 BlockDriverState
*bs
, *bs1
;
1646 QEMUSnapshotInfo sn
;
1650 bs
= get_bs_snapshots();
1652 monitor_printf(mon
, "No block device supports snapshots\n");
1656 /* Flush all IO requests so they don't interfere with the new state. */
1659 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1661 if (bdrv_has_snapshot(bs1
)) {
1662 ret
= bdrv_snapshot_goto(bs1
, name
);
1665 monitor_printf(mon
, "Warning: ");
1669 "Snapshots not supported on device '%s'\n",
1670 bdrv_get_device_name(bs1
));
1673 monitor_printf(mon
, "Could not find snapshot '%s' on "
1675 name
, bdrv_get_device_name(bs1
));
1678 monitor_printf(mon
, "Error %d while activating snapshot on"
1679 " '%s'\n", ret
, bdrv_get_device_name(bs1
));
1682 /* fatal on snapshot block device */
1689 /* Don't even try to load empty VM states */
1690 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1691 if ((ret
>= 0) && (sn
.vm_state_size
== 0))
1694 /* restore the VM state */
1695 f
= qemu_fopen_bdrv(bs
, 0);
1697 monitor_printf(mon
, "Could not open VM state file\n");
1700 ret
= qemu_loadvm_state(f
);
1703 monitor_printf(mon
, "Error %d while loading VM state\n", ret
);
1709 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
1712 BlockDriverState
*bs
, *bs1
;
1714 const char *name
= qdict_get_str(qdict
, "name");
1716 bs
= get_bs_snapshots();
1718 monitor_printf(mon
, "No block device supports snapshots\n");
1722 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1724 if (bdrv_has_snapshot(bs1
)) {
1725 ret
= bdrv_snapshot_delete(bs1
, name
);
1727 if (ret
== -ENOTSUP
)
1729 "Snapshots not supported on device '%s'\n",
1730 bdrv_get_device_name(bs1
));
1732 monitor_printf(mon
, "Error %d while deleting snapshot on "
1733 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
1739 void do_info_snapshots(Monitor
*mon
)
1742 BlockDriverState
*bs
, *bs1
;
1743 QEMUSnapshotInfo
*sn_tab
, *sn
;
1747 bs
= get_bs_snapshots();
1749 monitor_printf(mon
, "No available block device supports snapshots\n");
1752 monitor_printf(mon
, "Snapshot devices:");
1753 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1755 if (bdrv_has_snapshot(bs1
)) {
1757 monitor_printf(mon
, " %s", bdrv_get_device_name(bs1
));
1760 monitor_printf(mon
, "\n");
1762 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1764 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
1767 monitor_printf(mon
, "Snapshot list (from %s):\n",
1768 bdrv_get_device_name(bs
));
1769 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
1770 for(i
= 0; i
< nb_sns
; i
++) {
1772 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));