net: Fix -net socket,listen (Jan Kiszka)
[qemu-kvm/fedora.git] / savevm.c
blobcb6cd2e4361ef98754f29925ba4a4bf33d734834
1 /*
2 * QEMU System Emulator
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
22 * THE SOFTWARE.
24 #include "qemu-common.h"
25 #include "hw/hw.h"
26 #include "net.h"
27 #include "console.h"
28 #include "sysemu.h"
29 #include "qemu-timer.h"
30 #include "qemu-char.h"
31 #include "block.h"
32 #include "audio/audio.h"
33 #include "migration.h"
34 #include "qemu_socket.h"
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <signal.h>
39 #include <time.h>
40 #include <errno.h>
41 #include <sys/time.h>
42 #include <zlib.h>
44 #ifndef _WIN32
45 #include <sys/times.h>
46 #include <sys/wait.h>
47 #include <termios.h>
48 #include <sys/mman.h>
49 #include <sys/ioctl.h>
50 #include <sys/resource.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <net/if.h>
54 #if defined(__NetBSD__)
55 #include <net/if_tap.h>
56 #endif
57 #ifdef __linux__
58 #include <linux/if_tun.h>
59 #endif
60 #include <arpa/inet.h>
61 #include <dirent.h>
62 #include <netdb.h>
63 #include <sys/select.h>
64 #ifdef _BSD
65 #include <sys/stat.h>
66 #ifdef __FreeBSD__
67 #include <libutil.h>
68 #else
69 #include <util.h>
70 #endif
71 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
72 #include <freebsd/stdlib.h>
73 #else
74 #ifdef __linux__
75 #include <pty.h>
76 #include <malloc.h>
77 #include <linux/rtc.h>
78 #endif
79 #endif
80 #endif
82 #ifdef _WIN32
83 #include <malloc.h>
84 #include <sys/timeb.h>
85 #include <mmsystem.h>
86 #define getopt_long_only getopt_long
87 #define memalign(align, size) malloc(size)
88 #endif
90 /* point to the block driver where the snapshots are managed */
91 static BlockDriverState *bs_snapshots;
93 #define SELF_ANNOUNCE_ROUNDS 5
94 #define ETH_P_EXPERIMENTAL 0x01F1 /* just a number */
95 //#define ETH_P_EXPERIMENTAL 0x0012 /* make it the size of the packet */
96 #define EXPERIMENTAL_MAGIC 0xf1f23f4f
98 static int announce_self_create(uint8_t *buf,
99 uint8_t *mac_addr)
101 uint32_t magic = EXPERIMENTAL_MAGIC;
102 uint16_t proto = htons(ETH_P_EXPERIMENTAL);
104 /* FIXME: should we send a different packet (arp/rarp/ping)? */
106 memset(buf, 0xff, 6); /* h_dst */
107 memcpy(buf + 6, mac_addr, 6); /* h_src */
108 memcpy(buf + 12, &proto, 2); /* h_proto */
109 memcpy(buf + 14, &magic, 4); /* magic */
111 return 18; /* len */
114 void qemu_announce_self(void)
116 int i, j, len;
117 VLANState *vlan;
118 VLANClientState *vc;
119 uint8_t buf[256];
121 for (i = 0; i < MAX_NICS; i++) {
122 if (!nd_table[i].used)
123 continue;
124 len = announce_self_create(buf, nd_table[i].macaddr);
125 vlan = nd_table[i].vlan;
126 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
127 for (j=0; j < SELF_ANNOUNCE_ROUNDS; j++)
128 vc->fd_read(vc->opaque, buf, len);
133 /***********************************************************/
134 /* savevm/loadvm support */
136 #define IO_BUF_SIZE 32768
138 struct QEMUFile {
139 QEMUFilePutBufferFunc *put_buffer;
140 QEMUFileGetBufferFunc *get_buffer;
141 QEMUFileCloseFunc *close;
142 QEMUFileRateLimit *rate_limit;
143 void *opaque;
144 int is_write;
146 int64_t buf_offset; /* start of buffer when writing, end of buffer
147 when reading */
148 int buf_index;
149 int buf_size; /* 0 when writing */
150 uint8_t buf[IO_BUF_SIZE];
152 int has_error;
155 typedef struct QEMUFilePopen
157 FILE *popen_file;
158 QEMUFile *file;
159 } QEMUFilePopen;
161 typedef struct QEMUFileSocket
163 int fd;
164 QEMUFile *file;
165 } QEMUFileSocket;
167 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
169 QEMUFileSocket *s = opaque;
170 ssize_t len;
172 do {
173 len = recv(s->fd, buf, size, 0);
174 } while (len == -1 && socket_error() == EINTR);
176 if (len == -1)
177 len = -socket_error();
179 return len;
182 static int socket_close(void *opaque)
184 QEMUFileSocket *s = opaque;
185 qemu_free(s);
186 return 0;
189 static int popen_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
191 QEMUFilePopen *s = opaque;
192 return fwrite(buf, 1, size, s->popen_file);
195 static int popen_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
197 QEMUFilePopen *s = opaque;
198 return fread(buf, 1, size, s->popen_file);
201 static int popen_close(void *opaque)
203 QEMUFilePopen *s = opaque;
204 pclose(s->popen_file);
205 qemu_free(s);
206 return 0;
209 QEMUFile *qemu_popen(FILE *popen_file, const char *mode)
211 QEMUFilePopen *s;
213 if (popen_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
214 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
215 return NULL;
218 s = qemu_mallocz(sizeof(QEMUFilePopen));
220 s->popen_file = popen_file;
222 if(mode[0] == 'r') {
223 s->file = qemu_fopen_ops(s, NULL, popen_get_buffer, popen_close, NULL);
224 } else {
225 s->file = qemu_fopen_ops(s, popen_put_buffer, NULL, popen_close, NULL);
227 return s->file;
230 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
232 FILE *popen_file;
234 popen_file = popen(command, mode);
235 if(popen_file == NULL) {
236 return NULL;
239 return qemu_popen(popen_file, mode);
242 int qemu_popen_fd(QEMUFile *f)
244 QEMUFilePopen *p;
245 int fd;
247 p = (QEMUFilePopen *)f->opaque;
248 fd = fileno(p->popen_file);
250 return fd;
253 QEMUFile *qemu_fopen_socket(int fd)
255 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
257 s->fd = fd;
258 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, NULL);
259 return s->file;
262 typedef struct QEMUFileStdio
264 FILE *outfile;
265 } QEMUFileStdio;
267 static int file_put_buffer(void *opaque, const uint8_t *buf,
268 int64_t pos, int size)
270 QEMUFileStdio *s = opaque;
271 fseek(s->outfile, pos, SEEK_SET);
272 fwrite(buf, 1, size, s->outfile);
273 return size;
276 static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
278 QEMUFileStdio *s = opaque;
279 fseek(s->outfile, pos, SEEK_SET);
280 return fread(buf, 1, size, s->outfile);
283 static int file_close(void *opaque)
285 QEMUFileStdio *s = opaque;
286 fclose(s->outfile);
287 qemu_free(s);
288 return 0;
291 QEMUFile *qemu_fopen(const char *filename, const char *mode)
293 QEMUFileStdio *s;
295 s = qemu_mallocz(sizeof(QEMUFileStdio));
297 s->outfile = fopen(filename, mode);
298 if (!s->outfile)
299 goto fail;
301 if (!strcmp(mode, "wb"))
302 return qemu_fopen_ops(s, file_put_buffer, NULL, file_close, NULL);
303 else if (!strcmp(mode, "rb"))
304 return qemu_fopen_ops(s, NULL, file_get_buffer, file_close, NULL);
306 fail:
307 if (s->outfile)
308 fclose(s->outfile);
309 qemu_free(s);
310 return NULL;
313 typedef struct QEMUFileBdrv
315 BlockDriverState *bs;
316 int64_t base_offset;
317 } QEMUFileBdrv;
319 static int block_put_buffer(void *opaque, const uint8_t *buf,
320 int64_t pos, int size)
322 QEMUFileBdrv *s = opaque;
323 bdrv_put_buffer(s->bs, buf, s->base_offset + pos, size);
324 return size;
327 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
329 QEMUFileBdrv *s = opaque;
330 return bdrv_get_buffer(s->bs, buf, s->base_offset + pos, size);
333 static int bdrv_fclose(void *opaque)
335 QEMUFileBdrv *s = opaque;
336 qemu_free(s);
337 return 0;
340 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
342 QEMUFileBdrv *s;
344 s = qemu_mallocz(sizeof(QEMUFileBdrv));
346 s->bs = bs;
347 s->base_offset = offset;
349 if (is_writable)
350 return qemu_fopen_ops(s, block_put_buffer, NULL, bdrv_fclose, NULL);
352 return qemu_fopen_ops(s, NULL, block_get_buffer, bdrv_fclose, NULL);
355 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
356 QEMUFileGetBufferFunc *get_buffer,
357 QEMUFileCloseFunc *close,
358 QEMUFileRateLimit *rate_limit)
360 QEMUFile *f;
362 f = qemu_mallocz(sizeof(QEMUFile));
364 f->opaque = opaque;
365 f->put_buffer = put_buffer;
366 f->get_buffer = get_buffer;
367 f->close = close;
368 f->rate_limit = rate_limit;
369 f->is_write = 0;
371 return f;
374 int qemu_file_has_error(QEMUFile *f)
376 return f->has_error;
379 void qemu_file_set_error(QEMUFile *f)
381 f->has_error = 1;
384 void qemu_fflush(QEMUFile *f)
386 if (!f->put_buffer)
387 return;
389 if (f->is_write && f->buf_index > 0) {
390 int len;
392 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
393 if (len > 0)
394 f->buf_offset += f->buf_index;
395 else
396 f->has_error = 1;
397 f->buf_index = 0;
401 static void qemu_fill_buffer(QEMUFile *f)
403 int len;
405 if (!f->get_buffer)
406 return;
408 if (f->is_write)
409 abort();
411 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
412 if (len > 0) {
413 f->buf_index = 0;
414 f->buf_size = len;
415 f->buf_offset += len;
416 } else if (len != -EAGAIN)
417 f->has_error = 1;
420 int qemu_fclose(QEMUFile *f)
422 int ret = 0;
423 qemu_fflush(f);
424 if (f->close)
425 ret = f->close(f->opaque);
426 qemu_free(f);
427 return ret;
430 void qemu_file_put_notify(QEMUFile *f)
432 f->put_buffer(f->opaque, NULL, 0, 0);
435 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
437 int l;
439 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
440 fprintf(stderr,
441 "Attempted to write to buffer while read buffer is not empty\n");
442 abort();
445 while (!f->has_error && size > 0) {
446 l = IO_BUF_SIZE - f->buf_index;
447 if (l > size)
448 l = size;
449 memcpy(f->buf + f->buf_index, buf, l);
450 f->is_write = 1;
451 f->buf_index += l;
452 buf += l;
453 size -= l;
454 if (f->buf_index >= IO_BUF_SIZE)
455 qemu_fflush(f);
459 void qemu_put_byte(QEMUFile *f, int v)
461 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
462 fprintf(stderr,
463 "Attempted to write to buffer while read buffer is not empty\n");
464 abort();
467 f->buf[f->buf_index++] = v;
468 f->is_write = 1;
469 if (f->buf_index >= IO_BUF_SIZE)
470 qemu_fflush(f);
473 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
475 int size, l;
477 if (f->is_write)
478 abort();
480 size = size1;
481 while (size > 0) {
482 l = f->buf_size - f->buf_index;
483 if (l == 0) {
484 qemu_fill_buffer(f);
485 l = f->buf_size - f->buf_index;
486 if (l == 0)
487 break;
489 if (l > size)
490 l = size;
491 memcpy(buf, f->buf + f->buf_index, l);
492 f->buf_index += l;
493 buf += l;
494 size -= l;
496 return size1 - size;
499 int qemu_get_byte(QEMUFile *f)
501 if (f->is_write)
502 abort();
504 if (f->buf_index >= f->buf_size) {
505 qemu_fill_buffer(f);
506 if (f->buf_index >= f->buf_size)
507 return 0;
509 return f->buf[f->buf_index++];
512 int64_t qemu_ftell(QEMUFile *f)
514 return f->buf_offset - f->buf_size + f->buf_index;
517 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
519 if (whence == SEEK_SET) {
520 /* nothing to do */
521 } else if (whence == SEEK_CUR) {
522 pos += qemu_ftell(f);
523 } else {
524 /* SEEK_END not supported */
525 return -1;
527 if (f->put_buffer) {
528 qemu_fflush(f);
529 f->buf_offset = pos;
530 } else {
531 f->buf_offset = pos;
532 f->buf_index = 0;
533 f->buf_size = 0;
535 return pos;
538 int qemu_file_rate_limit(QEMUFile *f)
540 if (f->rate_limit)
541 return f->rate_limit(f->opaque);
543 return 0;
546 void qemu_put_be16(QEMUFile *f, unsigned int v)
548 qemu_put_byte(f, v >> 8);
549 qemu_put_byte(f, v);
552 void qemu_put_be32(QEMUFile *f, unsigned int v)
554 qemu_put_byte(f, v >> 24);
555 qemu_put_byte(f, v >> 16);
556 qemu_put_byte(f, v >> 8);
557 qemu_put_byte(f, v);
560 void qemu_put_be64(QEMUFile *f, uint64_t v)
562 qemu_put_be32(f, v >> 32);
563 qemu_put_be32(f, v);
566 unsigned int qemu_get_be16(QEMUFile *f)
568 unsigned int v;
569 v = qemu_get_byte(f) << 8;
570 v |= qemu_get_byte(f);
571 return v;
574 unsigned int qemu_get_be32(QEMUFile *f)
576 unsigned int v;
577 v = qemu_get_byte(f) << 24;
578 v |= qemu_get_byte(f) << 16;
579 v |= qemu_get_byte(f) << 8;
580 v |= qemu_get_byte(f);
581 return v;
584 uint64_t qemu_get_be64(QEMUFile *f)
586 uint64_t v;
587 v = (uint64_t)qemu_get_be32(f) << 32;
588 v |= qemu_get_be32(f);
589 return v;
592 typedef struct SaveStateEntry {
593 char idstr[256];
594 int instance_id;
595 int version_id;
596 int section_id;
597 SaveLiveStateHandler *save_live_state;
598 SaveStateHandler *save_state;
599 LoadStateHandler *load_state;
600 void *opaque;
601 struct SaveStateEntry *next;
602 } SaveStateEntry;
604 static SaveStateEntry *first_se;
606 /* TODO: Individual devices generally have very little idea about the rest
607 of the system, so instance_id should be removed/replaced.
608 Meanwhile pass -1 as instance_id if you do not already have a clearly
609 distinguishing id for all instances of your device class. */
610 int register_savevm_live(const char *idstr,
611 int instance_id,
612 int version_id,
613 SaveLiveStateHandler *save_live_state,
614 SaveStateHandler *save_state,
615 LoadStateHandler *load_state,
616 void *opaque)
618 SaveStateEntry *se, **pse;
619 static int global_section_id;
621 se = qemu_malloc(sizeof(SaveStateEntry));
622 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
623 se->instance_id = (instance_id == -1) ? 0 : instance_id;
624 se->version_id = version_id;
625 se->section_id = global_section_id++;
626 se->save_live_state = save_live_state;
627 se->save_state = save_state;
628 se->load_state = load_state;
629 se->opaque = opaque;
630 se->next = NULL;
632 /* add at the end of list */
633 pse = &first_se;
634 while (*pse != NULL) {
635 if (instance_id == -1
636 && strcmp(se->idstr, (*pse)->idstr) == 0
637 && se->instance_id <= (*pse)->instance_id)
638 se->instance_id = (*pse)->instance_id + 1;
639 pse = &(*pse)->next;
641 *pse = se;
642 return 0;
645 int register_savevm(const char *idstr,
646 int instance_id,
647 int version_id,
648 SaveStateHandler *save_state,
649 LoadStateHandler *load_state,
650 void *opaque)
652 return register_savevm_live(idstr, instance_id, version_id,
653 NULL, save_state, load_state, opaque);
656 void unregister_savevm(const char *idstr, void *opaque)
658 SaveStateEntry **pse;
660 pse = &first_se;
661 while (*pse != NULL) {
662 if (strcmp((*pse)->idstr, idstr) == 0 && (*pse)->opaque == opaque) {
663 SaveStateEntry *next = (*pse)->next;
664 qemu_free(*pse);
665 *pse = next;
666 continue;
668 pse = &(*pse)->next;
672 #define QEMU_VM_FILE_MAGIC 0x5145564d
673 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
674 #define QEMU_VM_FILE_VERSION 0x00000003
676 #define QEMU_VM_EOF 0x00
677 #define QEMU_VM_SECTION_START 0x01
678 #define QEMU_VM_SECTION_PART 0x02
679 #define QEMU_VM_SECTION_END 0x03
680 #define QEMU_VM_SECTION_FULL 0x04
682 int qemu_savevm_state_begin(QEMUFile *f)
684 SaveStateEntry *se;
686 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
687 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
689 for (se = first_se; se != NULL; se = se->next) {
690 int len;
692 if (se->save_live_state == NULL)
693 continue;
695 /* Section type */
696 qemu_put_byte(f, QEMU_VM_SECTION_START);
697 qemu_put_be32(f, se->section_id);
699 /* ID string */
700 len = strlen(se->idstr);
701 qemu_put_byte(f, len);
702 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
704 qemu_put_be32(f, se->instance_id);
705 qemu_put_be32(f, se->version_id);
707 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
710 if (qemu_file_has_error(f))
711 return -EIO;
713 return 0;
716 int qemu_savevm_state_iterate(QEMUFile *f)
718 SaveStateEntry *se;
719 int ret = 1;
721 for (se = first_se; se != NULL; se = se->next) {
722 if (se->save_live_state == NULL)
723 continue;
725 /* Section type */
726 qemu_put_byte(f, QEMU_VM_SECTION_PART);
727 qemu_put_be32(f, se->section_id);
729 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
732 if (ret)
733 return 1;
735 if (qemu_file_has_error(f))
736 return -EIO;
738 return 0;
741 int qemu_savevm_state_complete(QEMUFile *f)
743 SaveStateEntry *se;
745 for (se = first_se; se != NULL; se = se->next) {
746 if (se->save_live_state == NULL)
747 continue;
749 /* Section type */
750 qemu_put_byte(f, QEMU_VM_SECTION_END);
751 qemu_put_be32(f, se->section_id);
753 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
756 for(se = first_se; se != NULL; se = se->next) {
757 int len;
759 if (se->save_state == NULL)
760 continue;
762 /* Section type */
763 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
764 qemu_put_be32(f, se->section_id);
766 /* ID string */
767 len = strlen(se->idstr);
768 qemu_put_byte(f, len);
769 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
771 qemu_put_be32(f, se->instance_id);
772 qemu_put_be32(f, se->version_id);
774 se->save_state(f, se->opaque);
777 qemu_put_byte(f, QEMU_VM_EOF);
779 if (qemu_file_has_error(f))
780 return -EIO;
782 return 0;
785 int qemu_savevm_state(QEMUFile *f)
787 int saved_vm_running;
788 int ret;
790 saved_vm_running = vm_running;
791 vm_stop(0);
793 bdrv_flush_all();
795 ret = qemu_savevm_state_begin(f);
796 if (ret < 0)
797 goto out;
799 do {
800 ret = qemu_savevm_state_iterate(f);
801 if (ret < 0)
802 goto out;
803 } while (ret == 0);
805 ret = qemu_savevm_state_complete(f);
807 out:
808 if (qemu_file_has_error(f))
809 ret = -EIO;
811 if (!ret && saved_vm_running)
812 vm_start();
814 return ret;
817 static SaveStateEntry *find_se(const char *idstr, int instance_id)
819 SaveStateEntry *se;
821 for(se = first_se; se != NULL; se = se->next) {
822 if (!strcmp(se->idstr, idstr) &&
823 instance_id == se->instance_id)
824 return se;
826 return NULL;
829 typedef struct LoadStateEntry {
830 SaveStateEntry *se;
831 int section_id;
832 int version_id;
833 struct LoadStateEntry *next;
834 } LoadStateEntry;
836 static int qemu_loadvm_state_v2(QEMUFile *f)
838 SaveStateEntry *se;
839 int len, ret, instance_id, record_len, version_id;
840 int64_t total_len, end_pos, cur_pos;
841 char idstr[256];
843 total_len = qemu_get_be64(f);
844 end_pos = total_len + qemu_ftell(f);
845 for(;;) {
846 if (qemu_ftell(f) >= end_pos)
847 break;
848 len = qemu_get_byte(f);
849 qemu_get_buffer(f, (uint8_t *)idstr, len);
850 idstr[len] = '\0';
851 instance_id = qemu_get_be32(f);
852 version_id = qemu_get_be32(f);
853 record_len = qemu_get_be32(f);
854 cur_pos = qemu_ftell(f);
855 se = find_se(idstr, instance_id);
856 if (!se) {
857 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
858 instance_id, idstr);
859 } else {
860 ret = se->load_state(f, se->opaque, version_id);
861 if (ret < 0) {
862 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
863 instance_id, idstr);
864 return ret;
867 /* always seek to exact end of record */
868 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
871 if (qemu_file_has_error(f))
872 return -EIO;
874 return 0;
877 int qemu_loadvm_state(QEMUFile *f)
879 LoadStateEntry *first_le = NULL;
880 uint8_t section_type;
881 unsigned int v;
882 int ret;
884 v = qemu_get_be32(f);
885 if (v != QEMU_VM_FILE_MAGIC)
886 return -EINVAL;
888 v = qemu_get_be32(f);
889 if (v == QEMU_VM_FILE_VERSION_COMPAT)
890 return qemu_loadvm_state_v2(f);
891 if (v != QEMU_VM_FILE_VERSION)
892 return -ENOTSUP;
894 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
895 uint32_t instance_id, version_id, section_id;
896 LoadStateEntry *le;
897 SaveStateEntry *se;
898 char idstr[257];
899 int len;
901 switch (section_type) {
902 case QEMU_VM_SECTION_START:
903 case QEMU_VM_SECTION_FULL:
904 /* Read section start */
905 section_id = qemu_get_be32(f);
906 len = qemu_get_byte(f);
907 qemu_get_buffer(f, (uint8_t *)idstr, len);
908 idstr[len] = 0;
909 instance_id = qemu_get_be32(f);
910 version_id = qemu_get_be32(f);
912 /* Find savevm section */
913 se = find_se(idstr, instance_id);
914 if (se == NULL) {
915 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
916 ret = -EINVAL;
917 goto out;
920 /* Validate version */
921 if (version_id > se->version_id) {
922 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
923 version_id, idstr, se->version_id);
924 ret = -EINVAL;
925 goto out;
928 /* Add entry */
929 le = qemu_mallocz(sizeof(*le));
931 le->se = se;
932 le->section_id = section_id;
933 le->version_id = version_id;
934 le->next = first_le;
935 first_le = le;
937 le->se->load_state(f, le->se->opaque, le->version_id);
938 break;
939 case QEMU_VM_SECTION_PART:
940 case QEMU_VM_SECTION_END:
941 section_id = qemu_get_be32(f);
943 for (le = first_le; le && le->section_id != section_id; le = le->next);
944 if (le == NULL) {
945 fprintf(stderr, "Unknown savevm section %d\n", section_id);
946 ret = -EINVAL;
947 goto out;
950 le->se->load_state(f, le->se->opaque, le->version_id);
951 break;
952 default:
953 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
954 ret = -EINVAL;
955 goto out;
959 ret = 0;
961 out:
962 while (first_le) {
963 LoadStateEntry *le = first_le;
964 first_le = first_le->next;
965 qemu_free(le);
968 if (qemu_file_has_error(f))
969 ret = -EIO;
971 return ret;
974 /* device can contain snapshots */
975 static int bdrv_can_snapshot(BlockDriverState *bs)
977 return (bs &&
978 !bdrv_is_removable(bs) &&
979 !bdrv_is_read_only(bs));
982 /* device must be snapshots in order to have a reliable snapshot */
983 static int bdrv_has_snapshot(BlockDriverState *bs)
985 return (bs &&
986 !bdrv_is_removable(bs) &&
987 !bdrv_is_read_only(bs));
990 static BlockDriverState *get_bs_snapshots(void)
992 BlockDriverState *bs;
993 int i;
995 if (bs_snapshots)
996 return bs_snapshots;
997 for(i = 0; i <= nb_drives; i++) {
998 bs = drives_table[i].bdrv;
999 if (bdrv_can_snapshot(bs))
1000 goto ok;
1002 return NULL;
1004 bs_snapshots = bs;
1005 return bs;
1008 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1009 const char *name)
1011 QEMUSnapshotInfo *sn_tab, *sn;
1012 int nb_sns, i, ret;
1014 ret = -ENOENT;
1015 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1016 if (nb_sns < 0)
1017 return ret;
1018 for(i = 0; i < nb_sns; i++) {
1019 sn = &sn_tab[i];
1020 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1021 *sn_info = *sn;
1022 ret = 0;
1023 break;
1026 qemu_free(sn_tab);
1027 return ret;
1030 void do_savevm(const char *name)
1032 BlockDriverState *bs, *bs1;
1033 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1034 int must_delete, ret, i;
1035 BlockDriverInfo bdi1, *bdi = &bdi1;
1036 QEMUFile *f;
1037 int saved_vm_running;
1038 uint32_t vm_state_size;
1039 #ifdef _WIN32
1040 struct _timeb tb;
1041 #else
1042 struct timeval tv;
1043 #endif
1045 bs = get_bs_snapshots();
1046 if (!bs) {
1047 term_printf("No block device can accept snapshots\n");
1048 return;
1051 /* ??? Should this occur after vm_stop? */
1052 qemu_aio_flush();
1054 saved_vm_running = vm_running;
1055 vm_stop(0);
1057 must_delete = 0;
1058 if (name) {
1059 ret = bdrv_snapshot_find(bs, old_sn, name);
1060 if (ret >= 0) {
1061 must_delete = 1;
1064 memset(sn, 0, sizeof(*sn));
1065 if (must_delete) {
1066 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1067 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1068 } else {
1069 if (name)
1070 pstrcpy(sn->name, sizeof(sn->name), name);
1073 /* fill auxiliary fields */
1074 #ifdef _WIN32
1075 _ftime(&tb);
1076 sn->date_sec = tb.time;
1077 sn->date_nsec = tb.millitm * 1000000;
1078 #else
1079 gettimeofday(&tv, NULL);
1080 sn->date_sec = tv.tv_sec;
1081 sn->date_nsec = tv.tv_usec * 1000;
1082 #endif
1083 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1085 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
1086 term_printf("Device %s does not support VM state snapshots\n",
1087 bdrv_get_device_name(bs));
1088 goto the_end;
1091 /* save the VM state */
1092 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
1093 if (!f) {
1094 term_printf("Could not open VM state file\n");
1095 goto the_end;
1097 ret = qemu_savevm_state(f);
1098 vm_state_size = qemu_ftell(f);
1099 qemu_fclose(f);
1100 if (ret < 0) {
1101 term_printf("Error %d while writing VM\n", ret);
1102 goto the_end;
1105 /* create the snapshots */
1107 for(i = 0; i < nb_drives; i++) {
1108 bs1 = drives_table[i].bdrv;
1109 if (bdrv_has_snapshot(bs1)) {
1110 if (must_delete) {
1111 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
1112 if (ret < 0) {
1113 term_printf("Error while deleting snapshot on '%s'\n",
1114 bdrv_get_device_name(bs1));
1117 /* Write VM state size only to the image that contains the state */
1118 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1119 ret = bdrv_snapshot_create(bs1, sn);
1120 if (ret < 0) {
1121 term_printf("Error while creating snapshot on '%s'\n",
1122 bdrv_get_device_name(bs1));
1127 the_end:
1128 if (saved_vm_running)
1129 vm_start();
1132 void do_loadvm(const char *name)
1134 BlockDriverState *bs, *bs1;
1135 BlockDriverInfo bdi1, *bdi = &bdi1;
1136 QEMUSnapshotInfo sn;
1137 QEMUFile *f;
1138 int i, ret;
1139 int saved_vm_running;
1141 bs = get_bs_snapshots();
1142 if (!bs) {
1143 term_printf("No block device supports snapshots\n");
1144 return;
1147 /* Flush all IO requests so they don't interfere with the new state. */
1148 qemu_aio_flush();
1150 saved_vm_running = vm_running;
1151 vm_stop(0);
1153 for(i = 0; i <= nb_drives; i++) {
1154 bs1 = drives_table[i].bdrv;
1155 if (bdrv_has_snapshot(bs1)) {
1156 ret = bdrv_snapshot_goto(bs1, name);
1157 if (ret < 0) {
1158 if (bs != bs1)
1159 term_printf("Warning: ");
1160 switch(ret) {
1161 case -ENOTSUP:
1162 term_printf("Snapshots not supported on device '%s'\n",
1163 bdrv_get_device_name(bs1));
1164 break;
1165 case -ENOENT:
1166 term_printf("Could not find snapshot '%s' on device '%s'\n",
1167 name, bdrv_get_device_name(bs1));
1168 break;
1169 default:
1170 term_printf("Error %d while activating snapshot on '%s'\n",
1171 ret, bdrv_get_device_name(bs1));
1172 break;
1174 /* fatal on snapshot block device */
1175 if (bs == bs1)
1176 goto the_end;
1181 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
1182 term_printf("Device %s does not support VM state snapshots\n",
1183 bdrv_get_device_name(bs));
1184 return;
1187 /* Don't even try to load empty VM states */
1188 ret = bdrv_snapshot_find(bs, &sn, name);
1189 if ((ret >= 0) && (sn.vm_state_size == 0))
1190 goto the_end;
1192 /* restore the VM state */
1193 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
1194 if (!f) {
1195 term_printf("Could not open VM state file\n");
1196 goto the_end;
1198 ret = qemu_loadvm_state(f);
1199 qemu_fclose(f);
1200 if (ret < 0) {
1201 term_printf("Error %d while loading VM state\n", ret);
1203 the_end:
1204 if (saved_vm_running)
1205 vm_start();
1208 void do_delvm(const char *name)
1210 BlockDriverState *bs, *bs1;
1211 int i, ret;
1213 bs = get_bs_snapshots();
1214 if (!bs) {
1215 term_printf("No block device supports snapshots\n");
1216 return;
1219 for(i = 0; i <= nb_drives; i++) {
1220 bs1 = drives_table[i].bdrv;
1221 if (bdrv_has_snapshot(bs1)) {
1222 ret = bdrv_snapshot_delete(bs1, name);
1223 if (ret < 0) {
1224 if (ret == -ENOTSUP)
1225 term_printf("Snapshots not supported on device '%s'\n",
1226 bdrv_get_device_name(bs1));
1227 else
1228 term_printf("Error %d while deleting snapshot on '%s'\n",
1229 ret, bdrv_get_device_name(bs1));
1235 void do_info_snapshots(void)
1237 BlockDriverState *bs, *bs1;
1238 QEMUSnapshotInfo *sn_tab, *sn;
1239 int nb_sns, i;
1240 char buf[256];
1242 bs = get_bs_snapshots();
1243 if (!bs) {
1244 term_printf("No available block device supports snapshots\n");
1245 return;
1247 term_printf("Snapshot devices:");
1248 for(i = 0; i <= nb_drives; i++) {
1249 bs1 = drives_table[i].bdrv;
1250 if (bdrv_has_snapshot(bs1)) {
1251 if (bs == bs1)
1252 term_printf(" %s", bdrv_get_device_name(bs1));
1255 term_printf("\n");
1257 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1258 if (nb_sns < 0) {
1259 term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
1260 return;
1262 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
1263 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1264 for(i = 0; i < nb_sns; i++) {
1265 sn = &sn_tab[i];
1266 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1268 qemu_free(sn_tab);