2
0

savevm.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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 "config-host.h"
  25. #include "qemu-common.h"
  26. #include "hw/hw.h"
  27. #include "hw/qdev.h"
  28. #include "net/net.h"
  29. #include "monitor/monitor.h"
  30. #include "sysemu/sysemu.h"
  31. #include "qemu/timer.h"
  32. #include "audio/audio.h"
  33. #include "migration/migration.h"
  34. #include "qemu/sockets.h"
  35. #include "qemu/queue.h"
  36. #include "sysemu/cpus.h"
  37. #include "exec/memory.h"
  38. #include "qmp-commands.h"
  39. #include "trace.h"
  40. #include "qemu/iov.h"
  41. #include "block/snapshot.h"
  42. #include "block/qapi.h"
  43. #define SELF_ANNOUNCE_ROUNDS 5
  44. #ifndef ETH_P_RARP
  45. #define ETH_P_RARP 0x8035
  46. #endif
  47. #define ARP_HTYPE_ETH 0x0001
  48. #define ARP_PTYPE_IP 0x0800
  49. #define ARP_OP_REQUEST_REV 0x3
  50. static int announce_self_create(uint8_t *buf,
  51. uint8_t *mac_addr)
  52. {
  53. /* Ethernet header. */
  54. memset(buf, 0xff, 6); /* destination MAC addr */
  55. memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
  56. *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
  57. /* RARP header. */
  58. *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
  59. *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
  60. *(buf + 18) = 6; /* hardware addr length (ethernet) */
  61. *(buf + 19) = 4; /* protocol addr length (IPv4) */
  62. *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
  63. memcpy(buf + 22, mac_addr, 6); /* source hw addr */
  64. memset(buf + 28, 0x00, 4); /* source protocol addr */
  65. memcpy(buf + 32, mac_addr, 6); /* target hw addr */
  66. memset(buf + 38, 0x00, 4); /* target protocol addr */
  67. /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
  68. memset(buf + 42, 0x00, 18);
  69. return 60; /* len (FCS will be added by hardware) */
  70. }
  71. static void qemu_announce_self_iter(NICState *nic, void *opaque)
  72. {
  73. uint8_t buf[60];
  74. int len;
  75. trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic->conf->macaddr));
  76. len = announce_self_create(buf, nic->conf->macaddr.a);
  77. qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
  78. }
  79. static void qemu_announce_self_once(void *opaque)
  80. {
  81. static int count = SELF_ANNOUNCE_ROUNDS;
  82. QEMUTimer *timer = *(QEMUTimer **)opaque;
  83. qemu_foreach_nic(qemu_announce_self_iter, NULL);
  84. if (--count) {
  85. /* delay 50ms, 150ms, 250ms, ... */
  86. timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) +
  87. 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
  88. } else {
  89. timer_del(timer);
  90. timer_free(timer);
  91. }
  92. }
  93. void qemu_announce_self(void)
  94. {
  95. static QEMUTimer *timer;
  96. timer = timer_new_ms(QEMU_CLOCK_REALTIME, qemu_announce_self_once, &timer);
  97. qemu_announce_self_once(&timer);
  98. }
  99. /***********************************************************/
  100. /* savevm/loadvm support */
  101. static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
  102. int64_t pos)
  103. {
  104. int ret;
  105. QEMUIOVector qiov;
  106. qemu_iovec_init_external(&qiov, iov, iovcnt);
  107. ret = bdrv_writev_vmstate(opaque, &qiov, pos);
  108. if (ret < 0) {
  109. return ret;
  110. }
  111. return qiov.size;
  112. }
  113. static int block_put_buffer(void *opaque, const uint8_t *buf,
  114. int64_t pos, int size)
  115. {
  116. bdrv_save_vmstate(opaque, buf, pos, size);
  117. return size;
  118. }
  119. static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
  120. {
  121. return bdrv_load_vmstate(opaque, buf, pos, size);
  122. }
  123. static int bdrv_fclose(void *opaque)
  124. {
  125. return bdrv_flush(opaque);
  126. }
  127. static const QEMUFileOps bdrv_read_ops = {
  128. .get_buffer = block_get_buffer,
  129. .close = bdrv_fclose
  130. };
  131. static const QEMUFileOps bdrv_write_ops = {
  132. .put_buffer = block_put_buffer,
  133. .writev_buffer = block_writev_buffer,
  134. .close = bdrv_fclose
  135. };
  136. static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
  137. {
  138. if (is_writable) {
  139. return qemu_fopen_ops(bs, &bdrv_write_ops);
  140. }
  141. return qemu_fopen_ops(bs, &bdrv_read_ops);
  142. }
  143. /* QEMUFile timer support.
  144. * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
  145. */
  146. void timer_put(QEMUFile *f, QEMUTimer *ts)
  147. {
  148. uint64_t expire_time;
  149. expire_time = timer_expire_time_ns(ts);
  150. qemu_put_be64(f, expire_time);
  151. }
  152. void timer_get(QEMUFile *f, QEMUTimer *ts)
  153. {
  154. uint64_t expire_time;
  155. expire_time = qemu_get_be64(f);
  156. if (expire_time != -1) {
  157. timer_mod_ns(ts, expire_time);
  158. } else {
  159. timer_del(ts);
  160. }
  161. }
  162. /* VMState timer support.
  163. * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
  164. */
  165. static int get_timer(QEMUFile *f, void *pv, size_t size)
  166. {
  167. QEMUTimer *v = pv;
  168. timer_get(f, v);
  169. return 0;
  170. }
  171. static void put_timer(QEMUFile *f, void *pv, size_t size)
  172. {
  173. QEMUTimer *v = pv;
  174. timer_put(f, v);
  175. }
  176. const VMStateInfo vmstate_info_timer = {
  177. .name = "timer",
  178. .get = get_timer,
  179. .put = put_timer,
  180. };
  181. typedef struct CompatEntry {
  182. char idstr[256];
  183. int instance_id;
  184. } CompatEntry;
  185. typedef struct SaveStateEntry {
  186. QTAILQ_ENTRY(SaveStateEntry) entry;
  187. char idstr[256];
  188. int instance_id;
  189. int alias_id;
  190. int version_id;
  191. int section_id;
  192. SaveVMHandlers *ops;
  193. const VMStateDescription *vmsd;
  194. void *opaque;
  195. CompatEntry *compat;
  196. int no_migrate;
  197. int is_ram;
  198. } SaveStateEntry;
  199. static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
  200. QTAILQ_HEAD_INITIALIZER(savevm_handlers);
  201. static int global_section_id;
  202. static int calculate_new_instance_id(const char *idstr)
  203. {
  204. SaveStateEntry *se;
  205. int instance_id = 0;
  206. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  207. if (strcmp(idstr, se->idstr) == 0
  208. && instance_id <= se->instance_id) {
  209. instance_id = se->instance_id + 1;
  210. }
  211. }
  212. return instance_id;
  213. }
  214. static int calculate_compat_instance_id(const char *idstr)
  215. {
  216. SaveStateEntry *se;
  217. int instance_id = 0;
  218. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  219. if (!se->compat) {
  220. continue;
  221. }
  222. if (strcmp(idstr, se->compat->idstr) == 0
  223. && instance_id <= se->compat->instance_id) {
  224. instance_id = se->compat->instance_id + 1;
  225. }
  226. }
  227. return instance_id;
  228. }
  229. /* TODO: Individual devices generally have very little idea about the rest
  230. of the system, so instance_id should be removed/replaced.
  231. Meanwhile pass -1 as instance_id if you do not already have a clearly
  232. distinguishing id for all instances of your device class. */
  233. int register_savevm_live(DeviceState *dev,
  234. const char *idstr,
  235. int instance_id,
  236. int version_id,
  237. SaveVMHandlers *ops,
  238. void *opaque)
  239. {
  240. SaveStateEntry *se;
  241. se = g_malloc0(sizeof(SaveStateEntry));
  242. se->version_id = version_id;
  243. se->section_id = global_section_id++;
  244. se->ops = ops;
  245. se->opaque = opaque;
  246. se->vmsd = NULL;
  247. se->no_migrate = 0;
  248. /* if this is a live_savem then set is_ram */
  249. if (ops->save_live_setup != NULL) {
  250. se->is_ram = 1;
  251. }
  252. if (dev) {
  253. char *id = qdev_get_dev_path(dev);
  254. if (id) {
  255. pstrcpy(se->idstr, sizeof(se->idstr), id);
  256. pstrcat(se->idstr, sizeof(se->idstr), "/");
  257. g_free(id);
  258. se->compat = g_malloc0(sizeof(CompatEntry));
  259. pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
  260. se->compat->instance_id = instance_id == -1 ?
  261. calculate_compat_instance_id(idstr) : instance_id;
  262. instance_id = -1;
  263. }
  264. }
  265. pstrcat(se->idstr, sizeof(se->idstr), idstr);
  266. if (instance_id == -1) {
  267. se->instance_id = calculate_new_instance_id(se->idstr);
  268. } else {
  269. se->instance_id = instance_id;
  270. }
  271. assert(!se->compat || se->instance_id == 0);
  272. /* add at the end of list */
  273. QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
  274. return 0;
  275. }
  276. int register_savevm(DeviceState *dev,
  277. const char *idstr,
  278. int instance_id,
  279. int version_id,
  280. SaveStateHandler *save_state,
  281. LoadStateHandler *load_state,
  282. void *opaque)
  283. {
  284. SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
  285. ops->save_state = save_state;
  286. ops->load_state = load_state;
  287. return register_savevm_live(dev, idstr, instance_id, version_id,
  288. ops, opaque);
  289. }
  290. void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
  291. {
  292. SaveStateEntry *se, *new_se;
  293. char id[256] = "";
  294. if (dev) {
  295. char *path = qdev_get_dev_path(dev);
  296. if (path) {
  297. pstrcpy(id, sizeof(id), path);
  298. pstrcat(id, sizeof(id), "/");
  299. g_free(path);
  300. }
  301. }
  302. pstrcat(id, sizeof(id), idstr);
  303. QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
  304. if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
  305. QTAILQ_REMOVE(&savevm_handlers, se, entry);
  306. if (se->compat) {
  307. g_free(se->compat);
  308. }
  309. g_free(se->ops);
  310. g_free(se);
  311. }
  312. }
  313. }
  314. int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
  315. const VMStateDescription *vmsd,
  316. void *opaque, int alias_id,
  317. int required_for_version)
  318. {
  319. SaveStateEntry *se;
  320. /* If this triggers, alias support can be dropped for the vmsd. */
  321. assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
  322. se = g_malloc0(sizeof(SaveStateEntry));
  323. se->version_id = vmsd->version_id;
  324. se->section_id = global_section_id++;
  325. se->opaque = opaque;
  326. se->vmsd = vmsd;
  327. se->alias_id = alias_id;
  328. se->no_migrate = vmsd->unmigratable;
  329. if (dev) {
  330. char *id = qdev_get_dev_path(dev);
  331. if (id) {
  332. pstrcpy(se->idstr, sizeof(se->idstr), id);
  333. pstrcat(se->idstr, sizeof(se->idstr), "/");
  334. g_free(id);
  335. se->compat = g_malloc0(sizeof(CompatEntry));
  336. pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
  337. se->compat->instance_id = instance_id == -1 ?
  338. calculate_compat_instance_id(vmsd->name) : instance_id;
  339. instance_id = -1;
  340. }
  341. }
  342. pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
  343. if (instance_id == -1) {
  344. se->instance_id = calculate_new_instance_id(se->idstr);
  345. } else {
  346. se->instance_id = instance_id;
  347. }
  348. assert(!se->compat || se->instance_id == 0);
  349. /* add at the end of list */
  350. QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
  351. return 0;
  352. }
  353. void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
  354. void *opaque)
  355. {
  356. SaveStateEntry *se, *new_se;
  357. QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
  358. if (se->vmsd == vmsd && se->opaque == opaque) {
  359. QTAILQ_REMOVE(&savevm_handlers, se, entry);
  360. if (se->compat) {
  361. g_free(se->compat);
  362. }
  363. g_free(se);
  364. }
  365. }
  366. }
  367. static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
  368. {
  369. trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
  370. if (!se->vmsd) { /* Old style */
  371. return se->ops->load_state(f, se->opaque, version_id);
  372. }
  373. return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
  374. }
  375. static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
  376. {
  377. trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
  378. if (!se->vmsd) { /* Old style */
  379. se->ops->save_state(f, se->opaque);
  380. return;
  381. }
  382. vmstate_save_state(f, se->vmsd, se->opaque);
  383. }
  384. bool qemu_savevm_state_blocked(Error **errp)
  385. {
  386. SaveStateEntry *se;
  387. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  388. if (se->no_migrate) {
  389. error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
  390. return true;
  391. }
  392. }
  393. return false;
  394. }
  395. void qemu_savevm_state_begin(QEMUFile *f,
  396. const MigrationParams *params)
  397. {
  398. SaveStateEntry *se;
  399. int ret;
  400. trace_savevm_state_begin();
  401. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  402. if (!se->ops || !se->ops->set_params) {
  403. continue;
  404. }
  405. se->ops->set_params(params, se->opaque);
  406. }
  407. qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
  408. qemu_put_be32(f, QEMU_VM_FILE_VERSION);
  409. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  410. int len;
  411. if (!se->ops || !se->ops->save_live_setup) {
  412. continue;
  413. }
  414. if (se->ops && se->ops->is_active) {
  415. if (!se->ops->is_active(se->opaque)) {
  416. continue;
  417. }
  418. }
  419. /* Section type */
  420. qemu_put_byte(f, QEMU_VM_SECTION_START);
  421. qemu_put_be32(f, se->section_id);
  422. /* ID string */
  423. len = strlen(se->idstr);
  424. qemu_put_byte(f, len);
  425. qemu_put_buffer(f, (uint8_t *)se->idstr, len);
  426. qemu_put_be32(f, se->instance_id);
  427. qemu_put_be32(f, se->version_id);
  428. ret = se->ops->save_live_setup(f, se->opaque);
  429. if (ret < 0) {
  430. qemu_file_set_error(f, ret);
  431. break;
  432. }
  433. }
  434. }
  435. /*
  436. * this function has three return values:
  437. * negative: there was one error, and we have -errno.
  438. * 0 : We haven't finished, caller have to go again
  439. * 1 : We have finished, we can go to complete phase
  440. */
  441. int qemu_savevm_state_iterate(QEMUFile *f)
  442. {
  443. SaveStateEntry *se;
  444. int ret = 1;
  445. trace_savevm_state_iterate();
  446. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  447. if (!se->ops || !se->ops->save_live_iterate) {
  448. continue;
  449. }
  450. if (se->ops && se->ops->is_active) {
  451. if (!se->ops->is_active(se->opaque)) {
  452. continue;
  453. }
  454. }
  455. if (qemu_file_rate_limit(f)) {
  456. return 0;
  457. }
  458. trace_savevm_section_start(se->idstr, se->section_id);
  459. /* Section type */
  460. qemu_put_byte(f, QEMU_VM_SECTION_PART);
  461. qemu_put_be32(f, se->section_id);
  462. ret = se->ops->save_live_iterate(f, se->opaque);
  463. trace_savevm_section_end(se->idstr, se->section_id);
  464. if (ret < 0) {
  465. qemu_file_set_error(f, ret);
  466. }
  467. if (ret <= 0) {
  468. /* Do not proceed to the next vmstate before this one reported
  469. completion of the current stage. This serializes the migration
  470. and reduces the probability that a faster changing state is
  471. synchronized over and over again. */
  472. break;
  473. }
  474. }
  475. return ret;
  476. }
  477. void qemu_savevm_state_complete(QEMUFile *f)
  478. {
  479. SaveStateEntry *se;
  480. int ret;
  481. trace_savevm_state_complete();
  482. cpu_synchronize_all_states();
  483. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  484. if (!se->ops || !se->ops->save_live_complete) {
  485. continue;
  486. }
  487. if (se->ops && se->ops->is_active) {
  488. if (!se->ops->is_active(se->opaque)) {
  489. continue;
  490. }
  491. }
  492. trace_savevm_section_start(se->idstr, se->section_id);
  493. /* Section type */
  494. qemu_put_byte(f, QEMU_VM_SECTION_END);
  495. qemu_put_be32(f, se->section_id);
  496. ret = se->ops->save_live_complete(f, se->opaque);
  497. trace_savevm_section_end(se->idstr, se->section_id);
  498. if (ret < 0) {
  499. qemu_file_set_error(f, ret);
  500. return;
  501. }
  502. }
  503. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  504. int len;
  505. if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
  506. continue;
  507. }
  508. trace_savevm_section_start(se->idstr, se->section_id);
  509. /* Section type */
  510. qemu_put_byte(f, QEMU_VM_SECTION_FULL);
  511. qemu_put_be32(f, se->section_id);
  512. /* ID string */
  513. len = strlen(se->idstr);
  514. qemu_put_byte(f, len);
  515. qemu_put_buffer(f, (uint8_t *)se->idstr, len);
  516. qemu_put_be32(f, se->instance_id);
  517. qemu_put_be32(f, se->version_id);
  518. vmstate_save(f, se);
  519. trace_savevm_section_end(se->idstr, se->section_id);
  520. }
  521. qemu_put_byte(f, QEMU_VM_EOF);
  522. qemu_fflush(f);
  523. }
  524. uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
  525. {
  526. SaveStateEntry *se;
  527. uint64_t ret = 0;
  528. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  529. if (!se->ops || !se->ops->save_live_pending) {
  530. continue;
  531. }
  532. if (se->ops && se->ops->is_active) {
  533. if (!se->ops->is_active(se->opaque)) {
  534. continue;
  535. }
  536. }
  537. ret += se->ops->save_live_pending(f, se->opaque, max_size);
  538. }
  539. return ret;
  540. }
  541. void qemu_savevm_state_cancel(void)
  542. {
  543. SaveStateEntry *se;
  544. trace_savevm_state_cancel();
  545. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  546. if (se->ops && se->ops->cancel) {
  547. se->ops->cancel(se->opaque);
  548. }
  549. }
  550. }
  551. static int qemu_savevm_state(QEMUFile *f)
  552. {
  553. int ret;
  554. MigrationParams params = {
  555. .blk = 0,
  556. .shared = 0
  557. };
  558. if (qemu_savevm_state_blocked(NULL)) {
  559. return -EINVAL;
  560. }
  561. qemu_mutex_unlock_iothread();
  562. qemu_savevm_state_begin(f, &params);
  563. qemu_mutex_lock_iothread();
  564. while (qemu_file_get_error(f) == 0) {
  565. if (qemu_savevm_state_iterate(f) > 0) {
  566. break;
  567. }
  568. }
  569. ret = qemu_file_get_error(f);
  570. if (ret == 0) {
  571. qemu_savevm_state_complete(f);
  572. ret = qemu_file_get_error(f);
  573. }
  574. if (ret != 0) {
  575. qemu_savevm_state_cancel();
  576. }
  577. return ret;
  578. }
  579. static int qemu_save_device_state(QEMUFile *f)
  580. {
  581. SaveStateEntry *se;
  582. qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
  583. qemu_put_be32(f, QEMU_VM_FILE_VERSION);
  584. cpu_synchronize_all_states();
  585. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  586. int len;
  587. if (se->is_ram) {
  588. continue;
  589. }
  590. if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
  591. continue;
  592. }
  593. /* Section type */
  594. qemu_put_byte(f, QEMU_VM_SECTION_FULL);
  595. qemu_put_be32(f, se->section_id);
  596. /* ID string */
  597. len = strlen(se->idstr);
  598. qemu_put_byte(f, len);
  599. qemu_put_buffer(f, (uint8_t *)se->idstr, len);
  600. qemu_put_be32(f, se->instance_id);
  601. qemu_put_be32(f, se->version_id);
  602. vmstate_save(f, se);
  603. }
  604. qemu_put_byte(f, QEMU_VM_EOF);
  605. return qemu_file_get_error(f);
  606. }
  607. static SaveStateEntry *find_se(const char *idstr, int instance_id)
  608. {
  609. SaveStateEntry *se;
  610. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  611. if (!strcmp(se->idstr, idstr) &&
  612. (instance_id == se->instance_id ||
  613. instance_id == se->alias_id))
  614. return se;
  615. /* Migrating from an older version? */
  616. if (strstr(se->idstr, idstr) && se->compat) {
  617. if (!strcmp(se->compat->idstr, idstr) &&
  618. (instance_id == se->compat->instance_id ||
  619. instance_id == se->alias_id))
  620. return se;
  621. }
  622. }
  623. return NULL;
  624. }
  625. typedef struct LoadStateEntry {
  626. QLIST_ENTRY(LoadStateEntry) entry;
  627. SaveStateEntry *se;
  628. int section_id;
  629. int version_id;
  630. } LoadStateEntry;
  631. int qemu_loadvm_state(QEMUFile *f)
  632. {
  633. QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
  634. QLIST_HEAD_INITIALIZER(loadvm_handlers);
  635. LoadStateEntry *le, *new_le;
  636. uint8_t section_type;
  637. unsigned int v;
  638. int ret;
  639. if (qemu_savevm_state_blocked(NULL)) {
  640. return -EINVAL;
  641. }
  642. v = qemu_get_be32(f);
  643. if (v != QEMU_VM_FILE_MAGIC) {
  644. return -EINVAL;
  645. }
  646. v = qemu_get_be32(f);
  647. if (v == QEMU_VM_FILE_VERSION_COMPAT) {
  648. fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
  649. return -ENOTSUP;
  650. }
  651. if (v != QEMU_VM_FILE_VERSION) {
  652. return -ENOTSUP;
  653. }
  654. while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
  655. uint32_t instance_id, version_id, section_id;
  656. SaveStateEntry *se;
  657. char idstr[257];
  658. int len;
  659. switch (section_type) {
  660. case QEMU_VM_SECTION_START:
  661. case QEMU_VM_SECTION_FULL:
  662. /* Read section start */
  663. section_id = qemu_get_be32(f);
  664. len = qemu_get_byte(f);
  665. qemu_get_buffer(f, (uint8_t *)idstr, len);
  666. idstr[len] = 0;
  667. instance_id = qemu_get_be32(f);
  668. version_id = qemu_get_be32(f);
  669. /* Find savevm section */
  670. se = find_se(idstr, instance_id);
  671. if (se == NULL) {
  672. fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
  673. ret = -EINVAL;
  674. goto out;
  675. }
  676. /* Validate version */
  677. if (version_id > se->version_id) {
  678. fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
  679. version_id, idstr, se->version_id);
  680. ret = -EINVAL;
  681. goto out;
  682. }
  683. /* Add entry */
  684. le = g_malloc0(sizeof(*le));
  685. le->se = se;
  686. le->section_id = section_id;
  687. le->version_id = version_id;
  688. QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
  689. ret = vmstate_load(f, le->se, le->version_id);
  690. if (ret < 0) {
  691. fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
  692. instance_id, idstr);
  693. goto out;
  694. }
  695. break;
  696. case QEMU_VM_SECTION_PART:
  697. case QEMU_VM_SECTION_END:
  698. section_id = qemu_get_be32(f);
  699. QLIST_FOREACH(le, &loadvm_handlers, entry) {
  700. if (le->section_id == section_id) {
  701. break;
  702. }
  703. }
  704. if (le == NULL) {
  705. fprintf(stderr, "Unknown savevm section %d\n", section_id);
  706. ret = -EINVAL;
  707. goto out;
  708. }
  709. ret = vmstate_load(f, le->se, le->version_id);
  710. if (ret < 0) {
  711. fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
  712. section_id);
  713. goto out;
  714. }
  715. break;
  716. default:
  717. fprintf(stderr, "Unknown savevm section type %d\n", section_type);
  718. ret = -EINVAL;
  719. goto out;
  720. }
  721. }
  722. cpu_synchronize_all_post_init();
  723. ret = 0;
  724. out:
  725. QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
  726. QLIST_REMOVE(le, entry);
  727. g_free(le);
  728. }
  729. if (ret == 0) {
  730. ret = qemu_file_get_error(f);
  731. }
  732. return ret;
  733. }
  734. static BlockDriverState *find_vmstate_bs(void)
  735. {
  736. BlockDriverState *bs = NULL;
  737. while ((bs = bdrv_next(bs))) {
  738. if (bdrv_can_snapshot(bs)) {
  739. return bs;
  740. }
  741. }
  742. return NULL;
  743. }
  744. /*
  745. * Deletes snapshots of a given name in all opened images.
  746. */
  747. static int del_existing_snapshots(Monitor *mon, const char *name)
  748. {
  749. BlockDriverState *bs;
  750. QEMUSnapshotInfo sn1, *snapshot = &sn1;
  751. Error *err = NULL;
  752. bs = NULL;
  753. while ((bs = bdrv_next(bs))) {
  754. if (bdrv_can_snapshot(bs) &&
  755. bdrv_snapshot_find(bs, snapshot, name) >= 0) {
  756. bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
  757. if (err) {
  758. monitor_printf(mon,
  759. "Error while deleting snapshot on device '%s':"
  760. " %s\n",
  761. bdrv_get_device_name(bs),
  762. error_get_pretty(err));
  763. error_free(err);
  764. return -1;
  765. }
  766. }
  767. }
  768. return 0;
  769. }
  770. void do_savevm(Monitor *mon, const QDict *qdict)
  771. {
  772. BlockDriverState *bs, *bs1;
  773. QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
  774. int ret;
  775. QEMUFile *f;
  776. int saved_vm_running;
  777. uint64_t vm_state_size;
  778. qemu_timeval tv;
  779. struct tm tm;
  780. const char *name = qdict_get_try_str(qdict, "name");
  781. /* Verify if there is a device that doesn't support snapshots and is writable */
  782. bs = NULL;
  783. while ((bs = bdrv_next(bs))) {
  784. if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
  785. continue;
  786. }
  787. if (!bdrv_can_snapshot(bs)) {
  788. monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
  789. bdrv_get_device_name(bs));
  790. return;
  791. }
  792. }
  793. bs = find_vmstate_bs();
  794. if (!bs) {
  795. monitor_printf(mon, "No block device can accept snapshots\n");
  796. return;
  797. }
  798. saved_vm_running = runstate_is_running();
  799. vm_stop(RUN_STATE_SAVE_VM);
  800. memset(sn, 0, sizeof(*sn));
  801. /* fill auxiliary fields */
  802. qemu_gettimeofday(&tv);
  803. sn->date_sec = tv.tv_sec;
  804. sn->date_nsec = tv.tv_usec * 1000;
  805. sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  806. if (name) {
  807. ret = bdrv_snapshot_find(bs, old_sn, name);
  808. if (ret >= 0) {
  809. pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
  810. pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
  811. } else {
  812. pstrcpy(sn->name, sizeof(sn->name), name);
  813. }
  814. } else {
  815. /* cast below needed for OpenBSD where tv_sec is still 'long' */
  816. localtime_r((const time_t *)&tv.tv_sec, &tm);
  817. strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
  818. }
  819. /* Delete old snapshots of the same name */
  820. if (name && del_existing_snapshots(mon, name) < 0) {
  821. goto the_end;
  822. }
  823. /* save the VM state */
  824. f = qemu_fopen_bdrv(bs, 1);
  825. if (!f) {
  826. monitor_printf(mon, "Could not open VM state file\n");
  827. goto the_end;
  828. }
  829. ret = qemu_savevm_state(f);
  830. vm_state_size = qemu_ftell(f);
  831. qemu_fclose(f);
  832. if (ret < 0) {
  833. monitor_printf(mon, "Error %d while writing VM\n", ret);
  834. goto the_end;
  835. }
  836. /* create the snapshots */
  837. bs1 = NULL;
  838. while ((bs1 = bdrv_next(bs1))) {
  839. if (bdrv_can_snapshot(bs1)) {
  840. /* Write VM state size only to the image that contains the state */
  841. sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
  842. ret = bdrv_snapshot_create(bs1, sn);
  843. if (ret < 0) {
  844. monitor_printf(mon, "Error while creating snapshot on '%s'\n",
  845. bdrv_get_device_name(bs1));
  846. }
  847. }
  848. }
  849. the_end:
  850. if (saved_vm_running) {
  851. vm_start();
  852. }
  853. }
  854. void qmp_xen_save_devices_state(const char *filename, Error **errp)
  855. {
  856. QEMUFile *f;
  857. int saved_vm_running;
  858. int ret;
  859. saved_vm_running = runstate_is_running();
  860. vm_stop(RUN_STATE_SAVE_VM);
  861. f = qemu_fopen(filename, "wb");
  862. if (!f) {
  863. error_setg_file_open(errp, errno, filename);
  864. goto the_end;
  865. }
  866. ret = qemu_save_device_state(f);
  867. qemu_fclose(f);
  868. if (ret < 0) {
  869. error_set(errp, QERR_IO_ERROR);
  870. }
  871. the_end:
  872. if (saved_vm_running) {
  873. vm_start();
  874. }
  875. }
  876. int load_vmstate(const char *name)
  877. {
  878. BlockDriverState *bs, *bs_vm_state;
  879. QEMUSnapshotInfo sn;
  880. QEMUFile *f;
  881. int ret;
  882. bs_vm_state = find_vmstate_bs();
  883. if (!bs_vm_state) {
  884. error_report("No block device supports snapshots");
  885. return -ENOTSUP;
  886. }
  887. /* Don't even try to load empty VM states */
  888. ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
  889. if (ret < 0) {
  890. return ret;
  891. } else if (sn.vm_state_size == 0) {
  892. error_report("This is a disk-only snapshot. Revert to it offline "
  893. "using qemu-img.");
  894. return -EINVAL;
  895. }
  896. /* Verify if there is any device that doesn't support snapshots and is
  897. writable and check if the requested snapshot is available too. */
  898. bs = NULL;
  899. while ((bs = bdrv_next(bs))) {
  900. if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
  901. continue;
  902. }
  903. if (!bdrv_can_snapshot(bs)) {
  904. error_report("Device '%s' is writable but does not support snapshots.",
  905. bdrv_get_device_name(bs));
  906. return -ENOTSUP;
  907. }
  908. ret = bdrv_snapshot_find(bs, &sn, name);
  909. if (ret < 0) {
  910. error_report("Device '%s' does not have the requested snapshot '%s'",
  911. bdrv_get_device_name(bs), name);
  912. return ret;
  913. }
  914. }
  915. /* Flush all IO requests so they don't interfere with the new state. */
  916. bdrv_drain_all();
  917. bs = NULL;
  918. while ((bs = bdrv_next(bs))) {
  919. if (bdrv_can_snapshot(bs)) {
  920. ret = bdrv_snapshot_goto(bs, name);
  921. if (ret < 0) {
  922. error_report("Error %d while activating snapshot '%s' on '%s'",
  923. ret, name, bdrv_get_device_name(bs));
  924. return ret;
  925. }
  926. }
  927. }
  928. /* restore the VM state */
  929. f = qemu_fopen_bdrv(bs_vm_state, 0);
  930. if (!f) {
  931. error_report("Could not open VM state file");
  932. return -EINVAL;
  933. }
  934. qemu_system_reset(VMRESET_SILENT);
  935. ret = qemu_loadvm_state(f);
  936. qemu_fclose(f);
  937. if (ret < 0) {
  938. error_report("Error %d while loading VM state", ret);
  939. return ret;
  940. }
  941. return 0;
  942. }
  943. void do_delvm(Monitor *mon, const QDict *qdict)
  944. {
  945. BlockDriverState *bs, *bs1;
  946. Error *err = NULL;
  947. const char *name = qdict_get_str(qdict, "name");
  948. bs = find_vmstate_bs();
  949. if (!bs) {
  950. monitor_printf(mon, "No block device supports snapshots\n");
  951. return;
  952. }
  953. bs1 = NULL;
  954. while ((bs1 = bdrv_next(bs1))) {
  955. if (bdrv_can_snapshot(bs1)) {
  956. bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
  957. if (err) {
  958. monitor_printf(mon,
  959. "Error while deleting snapshot on device '%s':"
  960. " %s\n",
  961. bdrv_get_device_name(bs),
  962. error_get_pretty(err));
  963. error_free(err);
  964. }
  965. }
  966. }
  967. }
  968. void do_info_snapshots(Monitor *mon, const QDict *qdict)
  969. {
  970. BlockDriverState *bs, *bs1;
  971. QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
  972. int nb_sns, i, ret, available;
  973. int total;
  974. int *available_snapshots;
  975. bs = find_vmstate_bs();
  976. if (!bs) {
  977. monitor_printf(mon, "No available block device supports snapshots\n");
  978. return;
  979. }
  980. nb_sns = bdrv_snapshot_list(bs, &sn_tab);
  981. if (nb_sns < 0) {
  982. monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
  983. return;
  984. }
  985. if (nb_sns == 0) {
  986. monitor_printf(mon, "There is no snapshot available.\n");
  987. return;
  988. }
  989. available_snapshots = g_malloc0(sizeof(int) * nb_sns);
  990. total = 0;
  991. for (i = 0; i < nb_sns; i++) {
  992. sn = &sn_tab[i];
  993. available = 1;
  994. bs1 = NULL;
  995. while ((bs1 = bdrv_next(bs1))) {
  996. if (bdrv_can_snapshot(bs1) && bs1 != bs) {
  997. ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
  998. if (ret < 0) {
  999. available = 0;
  1000. break;
  1001. }
  1002. }
  1003. }
  1004. if (available) {
  1005. available_snapshots[total] = i;
  1006. total++;
  1007. }
  1008. }
  1009. if (total > 0) {
  1010. bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
  1011. monitor_printf(mon, "\n");
  1012. for (i = 0; i < total; i++) {
  1013. sn = &sn_tab[available_snapshots[i]];
  1014. bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
  1015. monitor_printf(mon, "\n");
  1016. }
  1017. } else {
  1018. monitor_printf(mon, "There is no suitable snapshot available\n");
  1019. }
  1020. g_free(sn_tab);
  1021. g_free(available_snapshots);
  1022. }
  1023. void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
  1024. {
  1025. qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
  1026. memory_region_name(mr), dev);
  1027. }
  1028. void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
  1029. {
  1030. /* Nothing do to while the implementation is in RAMBlock */
  1031. }
  1032. void vmstate_register_ram_global(MemoryRegion *mr)
  1033. {
  1034. vmstate_register_ram(mr, NULL);
  1035. }