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