savevm.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  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:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  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.
  23. */
  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"
  35. #include <unistd.h>
  36. #include <fcntl.h>
  37. #include <signal.h>
  38. #include <time.h>
  39. #include <errno.h>
  40. #include <sys/time.h>
  41. #include <zlib.h>
  42. #ifndef _WIN32
  43. #include <sys/times.h>
  44. #include <sys/wait.h>
  45. #include <termios.h>
  46. #include <sys/mman.h>
  47. #include <sys/ioctl.h>
  48. #include <sys/resource.h>
  49. #include <sys/socket.h>
  50. #include <netinet/in.h>
  51. #include <net/if.h>
  52. #if defined(__NetBSD__)
  53. #include <net/if_tap.h>
  54. #endif
  55. #ifdef __linux__
  56. #include <linux/if_tun.h>
  57. #endif
  58. #include <arpa/inet.h>
  59. #include <dirent.h>
  60. #include <netdb.h>
  61. #include <sys/select.h>
  62. #ifdef _BSD
  63. #include <sys/stat.h>
  64. #ifdef __FreeBSD__
  65. #include <libutil.h>
  66. #else
  67. #include <util.h>
  68. #endif
  69. #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
  70. #include <freebsd/stdlib.h>
  71. #else
  72. #ifdef __linux__
  73. #include <pty.h>
  74. #include <malloc.h>
  75. #include <linux/rtc.h>
  76. #endif
  77. #endif
  78. #endif
  79. #ifdef _WIN32
  80. #include <malloc.h>
  81. #include <sys/timeb.h>
  82. #include <mmsystem.h>
  83. #define getopt_long_only getopt_long
  84. #define memalign(align, size) malloc(size)
  85. #endif
  86. /* point to the block driver where the snapshots are managed */
  87. static BlockDriverState *bs_snapshots;
  88. #define SELF_ANNOUNCE_ROUNDS 5
  89. #define ETH_P_EXPERIMENTAL 0x01F1 /* just a number */
  90. //#define ETH_P_EXPERIMENTAL 0x0012 /* make it the size of the packet */
  91. #define EXPERIMENTAL_MAGIC 0xf1f23f4f
  92. static int announce_self_create(uint8_t *buf,
  93. uint8_t *mac_addr)
  94. {
  95. uint32_t magic = EXPERIMENTAL_MAGIC;
  96. uint16_t proto = htons(ETH_P_EXPERIMENTAL);
  97. /* FIXME: should we send a different packet (arp/rarp/ping)? */
  98. memset(buf, 0, 64);
  99. memset(buf, 0xff, 6); /* h_dst */
  100. memcpy(buf + 6, mac_addr, 6); /* h_src */
  101. memcpy(buf + 12, &proto, 2); /* h_proto */
  102. memcpy(buf + 14, &magic, 4); /* magic */
  103. return 64; /* len */
  104. }
  105. static void qemu_announce_self_once(void *opaque)
  106. {
  107. int i, len;
  108. VLANState *vlan;
  109. VLANClientState *vc;
  110. uint8_t buf[256];
  111. static int count = SELF_ANNOUNCE_ROUNDS;
  112. QEMUTimer *timer = *(QEMUTimer **)opaque;
  113. for (i = 0; i < MAX_NICS; i++) {
  114. if (!nd_table[i].used)
  115. continue;
  116. len = announce_self_create(buf, nd_table[i].macaddr);
  117. vlan = nd_table[i].vlan;
  118. for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
  119. vc->fd_read(vc->opaque, buf, len);
  120. }
  121. }
  122. if (count--) {
  123. qemu_mod_timer(timer, qemu_get_clock(rt_clock) + 100);
  124. } else {
  125. qemu_del_timer(timer);
  126. qemu_free_timer(timer);
  127. }
  128. }
  129. void qemu_announce_self(void)
  130. {
  131. static QEMUTimer *timer;
  132. timer = qemu_new_timer(rt_clock, qemu_announce_self_once, &timer);
  133. qemu_announce_self_once(&timer);
  134. }
  135. /***********************************************************/
  136. /* savevm/loadvm support */
  137. #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;
  145. int64_t buf_offset; /* start of buffer when writing, end of buffer
  146. when reading */
  147. int buf_index;
  148. int buf_size; /* 0 when writing */
  149. uint8_t buf[IO_BUF_SIZE];
  150. int has_error;
  151. };
  152. typedef struct QEMUFilePopen
  153. {
  154. FILE *popen_file;
  155. QEMUFile *file;
  156. } QEMUFilePopen;
  157. typedef struct QEMUFileSocket
  158. {
  159. int fd;
  160. QEMUFile *file;
  161. } QEMUFileSocket;
  162. static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
  163. {
  164. QEMUFileSocket *s = opaque;
  165. ssize_t len;
  166. do {
  167. len = recv(s->fd, buf, size, 0);
  168. } while (len == -1 && socket_error() == EINTR);
  169. if (len == -1)
  170. len = -socket_error();
  171. return len;
  172. }
  173. static int socket_close(void *opaque)
  174. {
  175. QEMUFileSocket *s = opaque;
  176. qemu_free(s);
  177. return 0;
  178. }
  179. static int popen_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
  180. {
  181. QEMUFilePopen *s = opaque;
  182. return fwrite(buf, 1, size, s->popen_file);
  183. }
  184. static int popen_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
  185. {
  186. QEMUFilePopen *s = opaque;
  187. FILE *fp = s->popen_file;
  188. int bytes;
  189. do {
  190. clearerr(fp);
  191. bytes = fread(buf, 1, size, fp);
  192. } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
  193. return bytes;
  194. }
  195. static int popen_close(void *opaque)
  196. {
  197. QEMUFilePopen *s = opaque;
  198. pclose(s->popen_file);
  199. qemu_free(s);
  200. return 0;
  201. }
  202. QEMUFile *qemu_popen(FILE *popen_file, const char *mode)
  203. {
  204. QEMUFilePopen *s;
  205. if (popen_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
  206. fprintf(stderr, "qemu_popen: Argument validity check failed\n");
  207. return NULL;
  208. }
  209. s = qemu_mallocz(sizeof(QEMUFilePopen));
  210. s->popen_file = popen_file;
  211. if(mode[0] == 'r') {
  212. s->file = qemu_fopen_ops(s, NULL, popen_get_buffer, popen_close, NULL);
  213. } else {
  214. s->file = qemu_fopen_ops(s, popen_put_buffer, NULL, popen_close, NULL);
  215. }
  216. return s->file;
  217. }
  218. QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
  219. {
  220. FILE *popen_file;
  221. popen_file = popen(command, mode);
  222. if(popen_file == NULL) {
  223. return NULL;
  224. }
  225. return qemu_popen(popen_file, mode);
  226. }
  227. int qemu_popen_fd(QEMUFile *f)
  228. {
  229. QEMUFilePopen *p;
  230. int fd;
  231. p = (QEMUFilePopen *)f->opaque;
  232. fd = fileno(p->popen_file);
  233. return fd;
  234. }
  235. QEMUFile *qemu_fopen_socket(int fd)
  236. {
  237. QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
  238. s->fd = fd;
  239. s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, NULL);
  240. return s->file;
  241. }
  242. typedef struct QEMUFileStdio
  243. {
  244. FILE *outfile;
  245. } QEMUFileStdio;
  246. static int file_put_buffer(void *opaque, const uint8_t *buf,
  247. int64_t pos, int size)
  248. {
  249. QEMUFileStdio *s = opaque;
  250. fseek(s->outfile, pos, SEEK_SET);
  251. fwrite(buf, 1, size, s->outfile);
  252. return size;
  253. }
  254. static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
  255. {
  256. QEMUFileStdio *s = opaque;
  257. fseek(s->outfile, pos, SEEK_SET);
  258. return fread(buf, 1, size, s->outfile);
  259. }
  260. static int file_close(void *opaque)
  261. {
  262. QEMUFileStdio *s = opaque;
  263. fclose(s->outfile);
  264. qemu_free(s);
  265. return 0;
  266. }
  267. QEMUFile *qemu_fopen(const char *filename, const char *mode)
  268. {
  269. QEMUFileStdio *s;
  270. s = qemu_mallocz(sizeof(QEMUFileStdio));
  271. s->outfile = fopen(filename, mode);
  272. if (!s->outfile)
  273. goto fail;
  274. if (!strcmp(mode, "wb"))
  275. return qemu_fopen_ops(s, file_put_buffer, NULL, file_close, NULL);
  276. else if (!strcmp(mode, "rb"))
  277. return qemu_fopen_ops(s, NULL, file_get_buffer, file_close, NULL);
  278. fail:
  279. if (s->outfile)
  280. fclose(s->outfile);
  281. qemu_free(s);
  282. return NULL;
  283. }
  284. typedef struct QEMUFileBdrv
  285. {
  286. BlockDriverState *bs;
  287. int64_t base_offset;
  288. } QEMUFileBdrv;
  289. static int block_put_buffer(void *opaque, const uint8_t *buf,
  290. int64_t pos, int size)
  291. {
  292. QEMUFileBdrv *s = opaque;
  293. bdrv_put_buffer(s->bs, buf, s->base_offset + pos, size);
  294. return size;
  295. }
  296. static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
  297. {
  298. QEMUFileBdrv *s = opaque;
  299. return bdrv_get_buffer(s->bs, buf, s->base_offset + pos, size);
  300. }
  301. static int bdrv_fclose(void *opaque)
  302. {
  303. QEMUFileBdrv *s = opaque;
  304. qemu_free(s);
  305. return 0;
  306. }
  307. static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
  308. {
  309. QEMUFileBdrv *s;
  310. s = qemu_mallocz(sizeof(QEMUFileBdrv));
  311. s->bs = bs;
  312. s->base_offset = offset;
  313. if (is_writable)
  314. return qemu_fopen_ops(s, block_put_buffer, NULL, bdrv_fclose, NULL);
  315. return qemu_fopen_ops(s, NULL, block_get_buffer, bdrv_fclose, NULL);
  316. }
  317. QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
  318. QEMUFileGetBufferFunc *get_buffer,
  319. QEMUFileCloseFunc *close,
  320. QEMUFileRateLimit *rate_limit)
  321. {
  322. QEMUFile *f;
  323. f = qemu_mallocz(sizeof(QEMUFile));
  324. f->opaque = opaque;
  325. f->put_buffer = put_buffer;
  326. f->get_buffer = get_buffer;
  327. f->close = close;
  328. f->rate_limit = rate_limit;
  329. f->is_write = 0;
  330. return f;
  331. }
  332. int qemu_file_has_error(QEMUFile *f)
  333. {
  334. return f->has_error;
  335. }
  336. void qemu_file_set_error(QEMUFile *f)
  337. {
  338. f->has_error = 1;
  339. }
  340. void qemu_fflush(QEMUFile *f)
  341. {
  342. if (!f->put_buffer)
  343. return;
  344. if (f->is_write && f->buf_index > 0) {
  345. int len;
  346. len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
  347. if (len > 0)
  348. f->buf_offset += f->buf_index;
  349. else
  350. f->has_error = 1;
  351. f->buf_index = 0;
  352. }
  353. }
  354. static void qemu_fill_buffer(QEMUFile *f)
  355. {
  356. int len;
  357. if (!f->get_buffer)
  358. return;
  359. if (f->is_write)
  360. abort();
  361. len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
  362. if (len > 0) {
  363. f->buf_index = 0;
  364. f->buf_size = len;
  365. f->buf_offset += len;
  366. } else if (len != -EAGAIN)
  367. f->has_error = 1;
  368. }
  369. int qemu_fclose(QEMUFile *f)
  370. {
  371. int ret = 0;
  372. qemu_fflush(f);
  373. if (f->close)
  374. ret = f->close(f->opaque);
  375. qemu_free(f);
  376. return ret;
  377. }
  378. void qemu_file_put_notify(QEMUFile *f)
  379. {
  380. f->put_buffer(f->opaque, NULL, 0, 0);
  381. }
  382. void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
  383. {
  384. int l;
  385. if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
  386. fprintf(stderr,
  387. "Attempted to write to buffer while read buffer is not empty\n");
  388. abort();
  389. }
  390. while (!f->has_error && size > 0) {
  391. l = IO_BUF_SIZE - f->buf_index;
  392. if (l > size)
  393. l = size;
  394. memcpy(f->buf + f->buf_index, buf, l);
  395. f->is_write = 1;
  396. f->buf_index += l;
  397. buf += l;
  398. size -= l;
  399. if (f->buf_index >= IO_BUF_SIZE)
  400. qemu_fflush(f);
  401. }
  402. }
  403. void qemu_put_byte(QEMUFile *f, int v)
  404. {
  405. if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
  406. fprintf(stderr,
  407. "Attempted to write to buffer while read buffer is not empty\n");
  408. abort();
  409. }
  410. f->buf[f->buf_index++] = v;
  411. f->is_write = 1;
  412. if (f->buf_index >= IO_BUF_SIZE)
  413. qemu_fflush(f);
  414. }
  415. int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
  416. {
  417. int size, l;
  418. if (f->is_write)
  419. abort();
  420. size = size1;
  421. while (size > 0) {
  422. l = f->buf_size - f->buf_index;
  423. if (l == 0) {
  424. qemu_fill_buffer(f);
  425. l = f->buf_size - f->buf_index;
  426. if (l == 0)
  427. break;
  428. }
  429. if (l > size)
  430. l = size;
  431. memcpy(buf, f->buf + f->buf_index, l);
  432. f->buf_index += l;
  433. buf += l;
  434. size -= l;
  435. }
  436. return size1 - size;
  437. }
  438. int qemu_get_byte(QEMUFile *f)
  439. {
  440. if (f->is_write)
  441. abort();
  442. if (f->buf_index >= f->buf_size) {
  443. qemu_fill_buffer(f);
  444. if (f->buf_index >= f->buf_size)
  445. return 0;
  446. }
  447. return f->buf[f->buf_index++];
  448. }
  449. int64_t qemu_ftell(QEMUFile *f)
  450. {
  451. return f->buf_offset - f->buf_size + f->buf_index;
  452. }
  453. int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
  454. {
  455. if (whence == SEEK_SET) {
  456. /* nothing to do */
  457. } else if (whence == SEEK_CUR) {
  458. pos += qemu_ftell(f);
  459. } else {
  460. /* SEEK_END not supported */
  461. return -1;
  462. }
  463. if (f->put_buffer) {
  464. qemu_fflush(f);
  465. f->buf_offset = pos;
  466. } else {
  467. f->buf_offset = pos;
  468. f->buf_index = 0;
  469. f->buf_size = 0;
  470. }
  471. return pos;
  472. }
  473. int qemu_file_rate_limit(QEMUFile *f)
  474. {
  475. if (f->rate_limit)
  476. return f->rate_limit(f->opaque);
  477. return 0;
  478. }
  479. void qemu_put_be16(QEMUFile *f, unsigned int v)
  480. {
  481. qemu_put_byte(f, v >> 8);
  482. qemu_put_byte(f, v);
  483. }
  484. void qemu_put_be32(QEMUFile *f, unsigned int v)
  485. {
  486. qemu_put_byte(f, v >> 24);
  487. qemu_put_byte(f, v >> 16);
  488. qemu_put_byte(f, v >> 8);
  489. qemu_put_byte(f, v);
  490. }
  491. void qemu_put_be64(QEMUFile *f, uint64_t v)
  492. {
  493. qemu_put_be32(f, v >> 32);
  494. qemu_put_be32(f, v);
  495. }
  496. unsigned int qemu_get_be16(QEMUFile *f)
  497. {
  498. unsigned int v;
  499. v = qemu_get_byte(f) << 8;
  500. v |= qemu_get_byte(f);
  501. return v;
  502. }
  503. unsigned int qemu_get_be32(QEMUFile *f)
  504. {
  505. unsigned int v;
  506. v = qemu_get_byte(f) << 24;
  507. v |= qemu_get_byte(f) << 16;
  508. v |= qemu_get_byte(f) << 8;
  509. v |= qemu_get_byte(f);
  510. return v;
  511. }
  512. uint64_t qemu_get_be64(QEMUFile *f)
  513. {
  514. uint64_t v;
  515. v = (uint64_t)qemu_get_be32(f) << 32;
  516. v |= qemu_get_be32(f);
  517. return v;
  518. }
  519. typedef struct SaveStateEntry {
  520. char idstr[256];
  521. int instance_id;
  522. int version_id;
  523. int section_id;
  524. SaveLiveStateHandler *save_live_state;
  525. SaveStateHandler *save_state;
  526. LoadStateHandler *load_state;
  527. void *opaque;
  528. struct SaveStateEntry *next;
  529. } SaveStateEntry;
  530. static SaveStateEntry *first_se;
  531. /* TODO: Individual devices generally have very little idea about the rest
  532. of the system, so instance_id should be removed/replaced.
  533. Meanwhile pass -1 as instance_id if you do not already have a clearly
  534. distinguishing id for all instances of your device class. */
  535. int register_savevm_live(const char *idstr,
  536. int instance_id,
  537. int version_id,
  538. SaveLiveStateHandler *save_live_state,
  539. SaveStateHandler *save_state,
  540. LoadStateHandler *load_state,
  541. void *opaque)
  542. {
  543. SaveStateEntry *se, **pse;
  544. static int global_section_id;
  545. se = qemu_malloc(sizeof(SaveStateEntry));
  546. pstrcpy(se->idstr, sizeof(se->idstr), idstr);
  547. se->instance_id = (instance_id == -1) ? 0 : instance_id;
  548. se->version_id = version_id;
  549. se->section_id = global_section_id++;
  550. se->save_live_state = save_live_state;
  551. se->save_state = save_state;
  552. se->load_state = load_state;
  553. se->opaque = opaque;
  554. se->next = NULL;
  555. /* add at the end of list */
  556. pse = &first_se;
  557. while (*pse != NULL) {
  558. if (instance_id == -1
  559. && strcmp(se->idstr, (*pse)->idstr) == 0
  560. && se->instance_id <= (*pse)->instance_id)
  561. se->instance_id = (*pse)->instance_id + 1;
  562. pse = &(*pse)->next;
  563. }
  564. *pse = se;
  565. return 0;
  566. }
  567. int register_savevm(const char *idstr,
  568. int instance_id,
  569. int version_id,
  570. SaveStateHandler *save_state,
  571. LoadStateHandler *load_state,
  572. void *opaque)
  573. {
  574. return register_savevm_live(idstr, instance_id, version_id,
  575. NULL, save_state, load_state, opaque);
  576. }
  577. void unregister_savevm(const char *idstr, void *opaque)
  578. {
  579. SaveStateEntry **pse;
  580. pse = &first_se;
  581. while (*pse != NULL) {
  582. if (strcmp((*pse)->idstr, idstr) == 0 && (*pse)->opaque == opaque) {
  583. SaveStateEntry *next = (*pse)->next;
  584. qemu_free(*pse);
  585. *pse = next;
  586. continue;
  587. }
  588. pse = &(*pse)->next;
  589. }
  590. }
  591. #define QEMU_VM_FILE_MAGIC 0x5145564d
  592. #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
  593. #define QEMU_VM_FILE_VERSION 0x00000003
  594. #define QEMU_VM_EOF 0x00
  595. #define QEMU_VM_SECTION_START 0x01
  596. #define QEMU_VM_SECTION_PART 0x02
  597. #define QEMU_VM_SECTION_END 0x03
  598. #define QEMU_VM_SECTION_FULL 0x04
  599. int qemu_savevm_state_begin(QEMUFile *f)
  600. {
  601. SaveStateEntry *se;
  602. qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
  603. qemu_put_be32(f, QEMU_VM_FILE_VERSION);
  604. for (se = first_se; se != NULL; se = se->next) {
  605. int len;
  606. if (se->save_live_state == NULL)
  607. continue;
  608. /* Section type */
  609. qemu_put_byte(f, QEMU_VM_SECTION_START);
  610. qemu_put_be32(f, se->section_id);
  611. /* ID string */
  612. len = strlen(se->idstr);
  613. qemu_put_byte(f, len);
  614. qemu_put_buffer(f, (uint8_t *)se->idstr, len);
  615. qemu_put_be32(f, se->instance_id);
  616. qemu_put_be32(f, se->version_id);
  617. se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
  618. }
  619. if (qemu_file_has_error(f))
  620. return -EIO;
  621. return 0;
  622. }
  623. int qemu_savevm_state_iterate(QEMUFile *f)
  624. {
  625. SaveStateEntry *se;
  626. int ret = 1;
  627. for (se = first_se; se != NULL; se = se->next) {
  628. if (se->save_live_state == NULL)
  629. continue;
  630. /* Section type */
  631. qemu_put_byte(f, QEMU_VM_SECTION_PART);
  632. qemu_put_be32(f, se->section_id);
  633. ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
  634. }
  635. if (ret)
  636. return 1;
  637. if (qemu_file_has_error(f))
  638. return -EIO;
  639. return 0;
  640. }
  641. int qemu_savevm_state_complete(QEMUFile *f)
  642. {
  643. SaveStateEntry *se;
  644. for (se = first_se; se != NULL; se = se->next) {
  645. if (se->save_live_state == NULL)
  646. continue;
  647. /* Section type */
  648. qemu_put_byte(f, QEMU_VM_SECTION_END);
  649. qemu_put_be32(f, se->section_id);
  650. se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
  651. }
  652. for(se = first_se; se != NULL; se = se->next) {
  653. int len;
  654. if (se->save_state == NULL)
  655. continue;
  656. /* Section type */
  657. qemu_put_byte(f, QEMU_VM_SECTION_FULL);
  658. qemu_put_be32(f, se->section_id);
  659. /* ID string */
  660. len = strlen(se->idstr);
  661. qemu_put_byte(f, len);
  662. qemu_put_buffer(f, (uint8_t *)se->idstr, len);
  663. qemu_put_be32(f, se->instance_id);
  664. qemu_put_be32(f, se->version_id);
  665. se->save_state(f, se->opaque);
  666. }
  667. qemu_put_byte(f, QEMU_VM_EOF);
  668. if (qemu_file_has_error(f))
  669. return -EIO;
  670. return 0;
  671. }
  672. int qemu_savevm_state(QEMUFile *f)
  673. {
  674. int saved_vm_running;
  675. int ret;
  676. saved_vm_running = vm_running;
  677. vm_stop(0);
  678. bdrv_flush_all();
  679. ret = qemu_savevm_state_begin(f);
  680. if (ret < 0)
  681. goto out;
  682. do {
  683. ret = qemu_savevm_state_iterate(f);
  684. if (ret < 0)
  685. goto out;
  686. } while (ret == 0);
  687. ret = qemu_savevm_state_complete(f);
  688. out:
  689. if (qemu_file_has_error(f))
  690. ret = -EIO;
  691. if (!ret && saved_vm_running)
  692. vm_start();
  693. return ret;
  694. }
  695. static SaveStateEntry *find_se(const char *idstr, int instance_id)
  696. {
  697. SaveStateEntry *se;
  698. for(se = first_se; se != NULL; se = se->next) {
  699. if (!strcmp(se->idstr, idstr) &&
  700. instance_id == se->instance_id)
  701. return se;
  702. }
  703. return NULL;
  704. }
  705. typedef struct LoadStateEntry {
  706. SaveStateEntry *se;
  707. int section_id;
  708. int version_id;
  709. struct LoadStateEntry *next;
  710. } LoadStateEntry;
  711. static int qemu_loadvm_state_v2(QEMUFile *f)
  712. {
  713. SaveStateEntry *se;
  714. int len, ret, instance_id, record_len, version_id;
  715. int64_t total_len, end_pos, cur_pos;
  716. char idstr[256];
  717. total_len = qemu_get_be64(f);
  718. end_pos = total_len + qemu_ftell(f);
  719. for(;;) {
  720. if (qemu_ftell(f) >= end_pos)
  721. break;
  722. len = qemu_get_byte(f);
  723. qemu_get_buffer(f, (uint8_t *)idstr, len);
  724. idstr[len] = '\0';
  725. instance_id = qemu_get_be32(f);
  726. version_id = qemu_get_be32(f);
  727. record_len = qemu_get_be32(f);
  728. cur_pos = qemu_ftell(f);
  729. se = find_se(idstr, instance_id);
  730. if (!se) {
  731. fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
  732. instance_id, idstr);
  733. } else {
  734. ret = se->load_state(f, se->opaque, version_id);
  735. if (ret < 0) {
  736. fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
  737. instance_id, idstr);
  738. return ret;
  739. }
  740. }
  741. /* always seek to exact end of record */
  742. qemu_fseek(f, cur_pos + record_len, SEEK_SET);
  743. }
  744. if (qemu_file_has_error(f))
  745. return -EIO;
  746. return 0;
  747. }
  748. int qemu_loadvm_state(QEMUFile *f)
  749. {
  750. LoadStateEntry *first_le = NULL;
  751. uint8_t section_type;
  752. unsigned int v;
  753. int ret;
  754. v = qemu_get_be32(f);
  755. if (v != QEMU_VM_FILE_MAGIC)
  756. return -EINVAL;
  757. v = qemu_get_be32(f);
  758. if (v == QEMU_VM_FILE_VERSION_COMPAT)
  759. return qemu_loadvm_state_v2(f);
  760. if (v != QEMU_VM_FILE_VERSION)
  761. return -ENOTSUP;
  762. while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
  763. uint32_t instance_id, version_id, section_id;
  764. LoadStateEntry *le;
  765. SaveStateEntry *se;
  766. char idstr[257];
  767. int len;
  768. switch (section_type) {
  769. case QEMU_VM_SECTION_START:
  770. case QEMU_VM_SECTION_FULL:
  771. /* Read section start */
  772. section_id = qemu_get_be32(f);
  773. len = qemu_get_byte(f);
  774. qemu_get_buffer(f, (uint8_t *)idstr, len);
  775. idstr[len] = 0;
  776. instance_id = qemu_get_be32(f);
  777. version_id = qemu_get_be32(f);
  778. /* Find savevm section */
  779. se = find_se(idstr, instance_id);
  780. if (se == NULL) {
  781. fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
  782. ret = -EINVAL;
  783. goto out;
  784. }
  785. /* Validate version */
  786. if (version_id > se->version_id) {
  787. fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
  788. version_id, idstr, se->version_id);
  789. ret = -EINVAL;
  790. goto out;
  791. }
  792. /* Add entry */
  793. le = qemu_mallocz(sizeof(*le));
  794. le->se = se;
  795. le->section_id = section_id;
  796. le->version_id = version_id;
  797. le->next = first_le;
  798. first_le = le;
  799. le->se->load_state(f, le->se->opaque, le->version_id);
  800. break;
  801. case QEMU_VM_SECTION_PART:
  802. case QEMU_VM_SECTION_END:
  803. section_id = qemu_get_be32(f);
  804. for (le = first_le; le && le->section_id != section_id; le = le->next);
  805. if (le == NULL) {
  806. fprintf(stderr, "Unknown savevm section %d\n", section_id);
  807. ret = -EINVAL;
  808. goto out;
  809. }
  810. le->se->load_state(f, le->se->opaque, le->version_id);
  811. break;
  812. default:
  813. fprintf(stderr, "Unknown savevm section type %d\n", section_type);
  814. ret = -EINVAL;
  815. goto out;
  816. }
  817. }
  818. ret = 0;
  819. out:
  820. while (first_le) {
  821. LoadStateEntry *le = first_le;
  822. first_le = first_le->next;
  823. qemu_free(le);
  824. }
  825. if (qemu_file_has_error(f))
  826. ret = -EIO;
  827. return ret;
  828. }
  829. /* device can contain snapshots */
  830. static int bdrv_can_snapshot(BlockDriverState *bs)
  831. {
  832. return (bs &&
  833. !bdrv_is_removable(bs) &&
  834. !bdrv_is_read_only(bs));
  835. }
  836. /* device must be snapshots in order to have a reliable snapshot */
  837. static int bdrv_has_snapshot(BlockDriverState *bs)
  838. {
  839. return (bs &&
  840. !bdrv_is_removable(bs) &&
  841. !bdrv_is_read_only(bs));
  842. }
  843. static BlockDriverState *get_bs_snapshots(void)
  844. {
  845. BlockDriverState *bs;
  846. int i;
  847. if (bs_snapshots)
  848. return bs_snapshots;
  849. for(i = 0; i <= nb_drives; i++) {
  850. bs = drives_table[i].bdrv;
  851. if (bdrv_can_snapshot(bs))
  852. goto ok;
  853. }
  854. return NULL;
  855. ok:
  856. bs_snapshots = bs;
  857. return bs;
  858. }
  859. static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
  860. const char *name)
  861. {
  862. QEMUSnapshotInfo *sn_tab, *sn;
  863. int nb_sns, i, ret;
  864. ret = -ENOENT;
  865. nb_sns = bdrv_snapshot_list(bs, &sn_tab);
  866. if (nb_sns < 0)
  867. return ret;
  868. for(i = 0; i < nb_sns; i++) {
  869. sn = &sn_tab[i];
  870. if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
  871. *sn_info = *sn;
  872. ret = 0;
  873. break;
  874. }
  875. }
  876. qemu_free(sn_tab);
  877. return ret;
  878. }
  879. void do_savevm(const char *name)
  880. {
  881. BlockDriverState *bs, *bs1;
  882. QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
  883. int must_delete, ret, i;
  884. BlockDriverInfo bdi1, *bdi = &bdi1;
  885. QEMUFile *f;
  886. int saved_vm_running;
  887. uint32_t vm_state_size;
  888. #ifdef _WIN32
  889. struct _timeb tb;
  890. #else
  891. struct timeval tv;
  892. #endif
  893. bs = get_bs_snapshots();
  894. if (!bs) {
  895. term_printf("No block device can accept snapshots\n");
  896. return;
  897. }
  898. /* ??? Should this occur after vm_stop? */
  899. qemu_aio_flush();
  900. saved_vm_running = vm_running;
  901. vm_stop(0);
  902. must_delete = 0;
  903. if (name) {
  904. ret = bdrv_snapshot_find(bs, old_sn, name);
  905. if (ret >= 0) {
  906. must_delete = 1;
  907. }
  908. }
  909. memset(sn, 0, sizeof(*sn));
  910. if (must_delete) {
  911. pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
  912. pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
  913. } else {
  914. if (name)
  915. pstrcpy(sn->name, sizeof(sn->name), name);
  916. }
  917. /* fill auxiliary fields */
  918. #ifdef _WIN32
  919. _ftime(&tb);
  920. sn->date_sec = tb.time;
  921. sn->date_nsec = tb.millitm * 1000000;
  922. #else
  923. gettimeofday(&tv, NULL);
  924. sn->date_sec = tv.tv_sec;
  925. sn->date_nsec = tv.tv_usec * 1000;
  926. #endif
  927. sn->vm_clock_nsec = qemu_get_clock(vm_clock);
  928. if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
  929. term_printf("Device %s does not support VM state snapshots\n",
  930. bdrv_get_device_name(bs));
  931. goto the_end;
  932. }
  933. /* save the VM state */
  934. f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
  935. if (!f) {
  936. term_printf("Could not open VM state file\n");
  937. goto the_end;
  938. }
  939. ret = qemu_savevm_state(f);
  940. vm_state_size = qemu_ftell(f);
  941. qemu_fclose(f);
  942. if (ret < 0) {
  943. term_printf("Error %d while writing VM\n", ret);
  944. goto the_end;
  945. }
  946. /* create the snapshots */
  947. for(i = 0; i < nb_drives; i++) {
  948. bs1 = drives_table[i].bdrv;
  949. if (bdrv_has_snapshot(bs1)) {
  950. if (must_delete) {
  951. ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
  952. if (ret < 0) {
  953. term_printf("Error while deleting snapshot on '%s'\n",
  954. bdrv_get_device_name(bs1));
  955. }
  956. }
  957. /* Write VM state size only to the image that contains the state */
  958. sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
  959. ret = bdrv_snapshot_create(bs1, sn);
  960. if (ret < 0) {
  961. term_printf("Error while creating snapshot on '%s'\n",
  962. bdrv_get_device_name(bs1));
  963. }
  964. }
  965. }
  966. the_end:
  967. if (saved_vm_running)
  968. vm_start();
  969. }
  970. void do_loadvm(const char *name)
  971. {
  972. BlockDriverState *bs, *bs1;
  973. BlockDriverInfo bdi1, *bdi = &bdi1;
  974. QEMUSnapshotInfo sn;
  975. QEMUFile *f;
  976. int i, ret;
  977. int saved_vm_running;
  978. bs = get_bs_snapshots();
  979. if (!bs) {
  980. term_printf("No block device supports snapshots\n");
  981. return;
  982. }
  983. /* Flush all IO requests so they don't interfere with the new state. */
  984. qemu_aio_flush();
  985. saved_vm_running = vm_running;
  986. vm_stop(0);
  987. for(i = 0; i <= nb_drives; i++) {
  988. bs1 = drives_table[i].bdrv;
  989. if (bdrv_has_snapshot(bs1)) {
  990. ret = bdrv_snapshot_goto(bs1, name);
  991. if (ret < 0) {
  992. if (bs != bs1)
  993. term_printf("Warning: ");
  994. switch(ret) {
  995. case -ENOTSUP:
  996. term_printf("Snapshots not supported on device '%s'\n",
  997. bdrv_get_device_name(bs1));
  998. break;
  999. case -ENOENT:
  1000. term_printf("Could not find snapshot '%s' on device '%s'\n",
  1001. name, bdrv_get_device_name(bs1));
  1002. break;
  1003. default:
  1004. term_printf("Error %d while activating snapshot on '%s'\n",
  1005. ret, bdrv_get_device_name(bs1));
  1006. break;
  1007. }
  1008. /* fatal on snapshot block device */
  1009. if (bs == bs1)
  1010. goto the_end;
  1011. }
  1012. }
  1013. }
  1014. if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
  1015. term_printf("Device %s does not support VM state snapshots\n",
  1016. bdrv_get_device_name(bs));
  1017. return;
  1018. }
  1019. /* Don't even try to load empty VM states */
  1020. ret = bdrv_snapshot_find(bs, &sn, name);
  1021. if ((ret >= 0) && (sn.vm_state_size == 0))
  1022. goto the_end;
  1023. /* restore the VM state */
  1024. f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
  1025. if (!f) {
  1026. term_printf("Could not open VM state file\n");
  1027. goto the_end;
  1028. }
  1029. ret = qemu_loadvm_state(f);
  1030. qemu_fclose(f);
  1031. if (ret < 0) {
  1032. term_printf("Error %d while loading VM state\n", ret);
  1033. }
  1034. the_end:
  1035. if (saved_vm_running)
  1036. vm_start();
  1037. }
  1038. void do_delvm(const char *name)
  1039. {
  1040. BlockDriverState *bs, *bs1;
  1041. int i, ret;
  1042. bs = get_bs_snapshots();
  1043. if (!bs) {
  1044. term_printf("No block device supports snapshots\n");
  1045. return;
  1046. }
  1047. for(i = 0; i <= nb_drives; i++) {
  1048. bs1 = drives_table[i].bdrv;
  1049. if (bdrv_has_snapshot(bs1)) {
  1050. ret = bdrv_snapshot_delete(bs1, name);
  1051. if (ret < 0) {
  1052. if (ret == -ENOTSUP)
  1053. term_printf("Snapshots not supported on device '%s'\n",
  1054. bdrv_get_device_name(bs1));
  1055. else
  1056. term_printf("Error %d while deleting snapshot on '%s'\n",
  1057. ret, bdrv_get_device_name(bs1));
  1058. }
  1059. }
  1060. }
  1061. }
  1062. void do_info_snapshots(void)
  1063. {
  1064. BlockDriverState *bs, *bs1;
  1065. QEMUSnapshotInfo *sn_tab, *sn;
  1066. int nb_sns, i;
  1067. char buf[256];
  1068. bs = get_bs_snapshots();
  1069. if (!bs) {
  1070. term_printf("No available block device supports snapshots\n");
  1071. return;
  1072. }
  1073. term_printf("Snapshot devices:");
  1074. for(i = 0; i <= nb_drives; i++) {
  1075. bs1 = drives_table[i].bdrv;
  1076. if (bdrv_has_snapshot(bs1)) {
  1077. if (bs == bs1)
  1078. term_printf(" %s", bdrv_get_device_name(bs1));
  1079. }
  1080. }
  1081. term_printf("\n");
  1082. nb_sns = bdrv_snapshot_list(bs, &sn_tab);
  1083. if (nb_sns < 0) {
  1084. term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
  1085. return;
  1086. }
  1087. term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
  1088. term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
  1089. for(i = 0; i < nb_sns; i++) {
  1090. sn = &sn_tab[i];
  1091. term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
  1092. }
  1093. qemu_free(sn_tab);
  1094. }