*-user: plugin syscalls
[qemu/armbru.git] / migration / savevm.c
blob8d95e261f60acda0a173b9c46d20f490cee0d7c4
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009-2015 Red Hat Inc
7 * Authors:
8 * Juan Quintela <quintela@redhat.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
29 #include "qemu/osdep.h"
30 #include "hw/boards.h"
31 #include "hw/xen/xen.h"
32 #include "net/net.h"
33 #include "migration.h"
34 #include "migration/snapshot.h"
35 #include "migration/vmstate.h"
36 #include "migration/misc.h"
37 #include "migration/register.h"
38 #include "migration/global_state.h"
39 #include "ram.h"
40 #include "qemu-file-channel.h"
41 #include "qemu-file.h"
42 #include "savevm.h"
43 #include "postcopy-ram.h"
44 #include "qapi/error.h"
45 #include "qapi/qapi-commands-migration.h"
46 #include "qapi/qapi-commands-misc.h"
47 #include "qapi/qmp/qerror.h"
48 #include "qemu/error-report.h"
49 #include "sysemu/cpus.h"
50 #include "exec/memory.h"
51 #include "exec/target_page.h"
52 #include "trace.h"
53 #include "qemu/iov.h"
54 #include "qemu/main-loop.h"
55 #include "block/snapshot.h"
56 #include "qemu/cutils.h"
57 #include "io/channel-buffer.h"
58 #include "io/channel-file.h"
59 #include "sysemu/replay.h"
60 #include "sysemu/runstate.h"
61 #include "sysemu/sysemu.h"
62 #include "qjson.h"
63 #include "migration/colo.h"
64 #include "qemu/bitmap.h"
65 #include "net/announce.h"
67 const unsigned int postcopy_ram_discard_version = 0;
69 /* Subcommands for QEMU_VM_COMMAND */
70 enum qemu_vm_cmd {
71 MIG_CMD_INVALID = 0, /* Must be 0 */
72 MIG_CMD_OPEN_RETURN_PATH, /* Tell the dest to open the Return path */
73 MIG_CMD_PING, /* Request a PONG on the RP */
75 MIG_CMD_POSTCOPY_ADVISE, /* Prior to any page transfers, just
76 warn we might want to do PC */
77 MIG_CMD_POSTCOPY_LISTEN, /* Start listening for incoming
78 pages as it's running. */
79 MIG_CMD_POSTCOPY_RUN, /* Start execution */
81 MIG_CMD_POSTCOPY_RAM_DISCARD, /* A list of pages to discard that
82 were previously sent during
83 precopy but are dirty. */
84 MIG_CMD_PACKAGED, /* Send a wrapped stream within this stream */
85 MIG_CMD_ENABLE_COLO, /* Enable COLO */
86 MIG_CMD_POSTCOPY_RESUME, /* resume postcopy on dest */
87 MIG_CMD_RECV_BITMAP, /* Request for recved bitmap on dst */
88 MIG_CMD_MAX
91 #define MAX_VM_CMD_PACKAGED_SIZE UINT32_MAX
92 static struct mig_cmd_args {
93 ssize_t len; /* -1 = variable */
94 const char *name;
95 } mig_cmd_args[] = {
96 [MIG_CMD_INVALID] = { .len = -1, .name = "INVALID" },
97 [MIG_CMD_OPEN_RETURN_PATH] = { .len = 0, .name = "OPEN_RETURN_PATH" },
98 [MIG_CMD_PING] = { .len = sizeof(uint32_t), .name = "PING" },
99 [MIG_CMD_POSTCOPY_ADVISE] = { .len = -1, .name = "POSTCOPY_ADVISE" },
100 [MIG_CMD_POSTCOPY_LISTEN] = { .len = 0, .name = "POSTCOPY_LISTEN" },
101 [MIG_CMD_POSTCOPY_RUN] = { .len = 0, .name = "POSTCOPY_RUN" },
102 [MIG_CMD_POSTCOPY_RAM_DISCARD] = {
103 .len = -1, .name = "POSTCOPY_RAM_DISCARD" },
104 [MIG_CMD_POSTCOPY_RESUME] = { .len = 0, .name = "POSTCOPY_RESUME" },
105 [MIG_CMD_PACKAGED] = { .len = 4, .name = "PACKAGED" },
106 [MIG_CMD_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
107 [MIG_CMD_MAX] = { .len = -1, .name = "MAX" },
110 /* Note for MIG_CMD_POSTCOPY_ADVISE:
111 * The format of arguments is depending on postcopy mode:
112 * - postcopy RAM only
113 * uint64_t host page size
114 * uint64_t taget page size
116 * - postcopy RAM and postcopy dirty bitmaps
117 * format is the same as for postcopy RAM only
119 * - postcopy dirty bitmaps only
120 * Nothing. Command length field is 0.
122 * Be careful: adding a new postcopy entity with some other parameters should
123 * not break format self-description ability. Good way is to introduce some
124 * generic extendable format with an exception for two old entities.
127 /***********************************************************/
128 /* savevm/loadvm support */
130 static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
131 int64_t pos, Error **errp)
133 int ret;
134 QEMUIOVector qiov;
136 qemu_iovec_init_external(&qiov, iov, iovcnt);
137 ret = bdrv_writev_vmstate(opaque, &qiov, pos);
138 if (ret < 0) {
139 return ret;
142 return qiov.size;
145 static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
146 size_t size, Error **errp)
148 return bdrv_load_vmstate(opaque, buf, pos, size);
151 static int bdrv_fclose(void *opaque, Error **errp)
153 return bdrv_flush(opaque);
156 static const QEMUFileOps bdrv_read_ops = {
157 .get_buffer = block_get_buffer,
158 .close = bdrv_fclose
161 static const QEMUFileOps bdrv_write_ops = {
162 .writev_buffer = block_writev_buffer,
163 .close = bdrv_fclose
166 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
168 if (is_writable) {
169 return qemu_fopen_ops(bs, &bdrv_write_ops);
171 return qemu_fopen_ops(bs, &bdrv_read_ops);
175 /* QEMUFile timer support.
176 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
179 void timer_put(QEMUFile *f, QEMUTimer *ts)
181 uint64_t expire_time;
183 expire_time = timer_expire_time_ns(ts);
184 qemu_put_be64(f, expire_time);
187 void timer_get(QEMUFile *f, QEMUTimer *ts)
189 uint64_t expire_time;
191 expire_time = qemu_get_be64(f);
192 if (expire_time != -1) {
193 timer_mod_ns(ts, expire_time);
194 } else {
195 timer_del(ts);
200 /* VMState timer support.
201 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
204 static int get_timer(QEMUFile *f, void *pv, size_t size,
205 const VMStateField *field)
207 QEMUTimer *v = pv;
208 timer_get(f, v);
209 return 0;
212 static int put_timer(QEMUFile *f, void *pv, size_t size,
213 const VMStateField *field, QJSON *vmdesc)
215 QEMUTimer *v = pv;
216 timer_put(f, v);
218 return 0;
221 const VMStateInfo vmstate_info_timer = {
222 .name = "timer",
223 .get = get_timer,
224 .put = put_timer,
228 typedef struct CompatEntry {
229 char idstr[256];
230 int instance_id;
231 } CompatEntry;
233 typedef struct SaveStateEntry {
234 QTAILQ_ENTRY(SaveStateEntry) entry;
235 char idstr[256];
236 int instance_id;
237 int alias_id;
238 int version_id;
239 /* version id read from the stream */
240 int load_version_id;
241 int section_id;
242 /* section id read from the stream */
243 int load_section_id;
244 const SaveVMHandlers *ops;
245 const VMStateDescription *vmsd;
246 void *opaque;
247 CompatEntry *compat;
248 int is_ram;
249 } SaveStateEntry;
251 typedef struct SaveState {
252 QTAILQ_HEAD(, SaveStateEntry) handlers;
253 int global_section_id;
254 uint32_t len;
255 const char *name;
256 uint32_t target_page_bits;
257 uint32_t caps_count;
258 MigrationCapability *capabilities;
259 QemuUUID uuid;
260 } SaveState;
262 static SaveState savevm_state = {
263 .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers),
264 .global_section_id = 0,
267 static bool should_validate_capability(int capability)
269 assert(capability >= 0 && capability < MIGRATION_CAPABILITY__MAX);
270 /* Validate only new capabilities to keep compatibility. */
271 switch (capability) {
272 case MIGRATION_CAPABILITY_X_IGNORE_SHARED:
273 return true;
274 default:
275 return false;
279 static uint32_t get_validatable_capabilities_count(void)
281 MigrationState *s = migrate_get_current();
282 uint32_t result = 0;
283 int i;
284 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
285 if (should_validate_capability(i) && s->enabled_capabilities[i]) {
286 result++;
289 return result;
292 static int configuration_pre_save(void *opaque)
294 SaveState *state = opaque;
295 const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
296 MigrationState *s = migrate_get_current();
297 int i, j;
299 state->len = strlen(current_name);
300 state->name = current_name;
301 state->target_page_bits = qemu_target_page_bits();
303 state->caps_count = get_validatable_capabilities_count();
304 state->capabilities = g_renew(MigrationCapability, state->capabilities,
305 state->caps_count);
306 for (i = j = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
307 if (should_validate_capability(i) && s->enabled_capabilities[i]) {
308 state->capabilities[j++] = i;
311 state->uuid = qemu_uuid;
313 return 0;
316 static int configuration_pre_load(void *opaque)
318 SaveState *state = opaque;
320 /* If there is no target-page-bits subsection it means the source
321 * predates the variable-target-page-bits support and is using the
322 * minimum possible value for this CPU.
324 state->target_page_bits = qemu_target_page_bits_min();
325 return 0;
328 static bool configuration_validate_capabilities(SaveState *state)
330 bool ret = true;
331 MigrationState *s = migrate_get_current();
332 unsigned long *source_caps_bm;
333 int i;
335 source_caps_bm = bitmap_new(MIGRATION_CAPABILITY__MAX);
336 for (i = 0; i < state->caps_count; i++) {
337 MigrationCapability capability = state->capabilities[i];
338 set_bit(capability, source_caps_bm);
341 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
342 bool source_state, target_state;
343 if (!should_validate_capability(i)) {
344 continue;
346 source_state = test_bit(i, source_caps_bm);
347 target_state = s->enabled_capabilities[i];
348 if (source_state != target_state) {
349 error_report("Capability %s is %s, but received capability is %s",
350 MigrationCapability_str(i),
351 target_state ? "on" : "off",
352 source_state ? "on" : "off");
353 ret = false;
354 /* Don't break here to report all failed capabilities */
358 g_free(source_caps_bm);
359 return ret;
362 static int configuration_post_load(void *opaque, int version_id)
364 SaveState *state = opaque;
365 const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
367 if (strncmp(state->name, current_name, state->len) != 0) {
368 error_report("Machine type received is '%.*s' and local is '%s'",
369 (int) state->len, state->name, current_name);
370 return -EINVAL;
373 if (state->target_page_bits != qemu_target_page_bits()) {
374 error_report("Received TARGET_PAGE_BITS is %d but local is %d",
375 state->target_page_bits, qemu_target_page_bits());
376 return -EINVAL;
379 if (!configuration_validate_capabilities(state)) {
380 return -EINVAL;
383 return 0;
386 static int get_capability(QEMUFile *f, void *pv, size_t size,
387 const VMStateField *field)
389 MigrationCapability *capability = pv;
390 char capability_str[UINT8_MAX + 1];
391 uint8_t len;
392 int i;
394 len = qemu_get_byte(f);
395 qemu_get_buffer(f, (uint8_t *)capability_str, len);
396 capability_str[len] = '\0';
397 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
398 if (!strcmp(MigrationCapability_str(i), capability_str)) {
399 *capability = i;
400 return 0;
403 error_report("Received unknown capability %s", capability_str);
404 return -EINVAL;
407 static int put_capability(QEMUFile *f, void *pv, size_t size,
408 const VMStateField *field, QJSON *vmdesc)
410 MigrationCapability *capability = pv;
411 const char *capability_str = MigrationCapability_str(*capability);
412 size_t len = strlen(capability_str);
413 assert(len <= UINT8_MAX);
415 qemu_put_byte(f, len);
416 qemu_put_buffer(f, (uint8_t *)capability_str, len);
417 return 0;
420 static const VMStateInfo vmstate_info_capability = {
421 .name = "capability",
422 .get = get_capability,
423 .put = put_capability,
426 /* The target-page-bits subsection is present only if the
427 * target page size is not the same as the default (ie the
428 * minimum page size for a variable-page-size guest CPU).
429 * If it is present then it contains the actual target page
430 * bits for the machine, and migration will fail if the
431 * two ends don't agree about it.
433 static bool vmstate_target_page_bits_needed(void *opaque)
435 return qemu_target_page_bits()
436 > qemu_target_page_bits_min();
439 static const VMStateDescription vmstate_target_page_bits = {
440 .name = "configuration/target-page-bits",
441 .version_id = 1,
442 .minimum_version_id = 1,
443 .needed = vmstate_target_page_bits_needed,
444 .fields = (VMStateField[]) {
445 VMSTATE_UINT32(target_page_bits, SaveState),
446 VMSTATE_END_OF_LIST()
450 static bool vmstate_capabilites_needed(void *opaque)
452 return get_validatable_capabilities_count() > 0;
455 static const VMStateDescription vmstate_capabilites = {
456 .name = "configuration/capabilities",
457 .version_id = 1,
458 .minimum_version_id = 1,
459 .needed = vmstate_capabilites_needed,
460 .fields = (VMStateField[]) {
461 VMSTATE_UINT32_V(caps_count, SaveState, 1),
462 VMSTATE_VARRAY_UINT32_ALLOC(capabilities, SaveState, caps_count, 1,
463 vmstate_info_capability,
464 MigrationCapability),
465 VMSTATE_END_OF_LIST()
469 static bool vmstate_uuid_needed(void *opaque)
471 return qemu_uuid_set && migrate_validate_uuid();
474 static int vmstate_uuid_post_load(void *opaque, int version_id)
476 SaveState *state = opaque;
477 char uuid_src[UUID_FMT_LEN + 1];
478 char uuid_dst[UUID_FMT_LEN + 1];
480 if (!qemu_uuid_set) {
482 * It's warning because user might not know UUID in some cases,
483 * e.g. load an old snapshot
485 qemu_uuid_unparse(&state->uuid, uuid_src);
486 warn_report("UUID is received %s, but local uuid isn't set",
487 uuid_src);
488 return 0;
490 if (!qemu_uuid_is_equal(&state->uuid, &qemu_uuid)) {
491 qemu_uuid_unparse(&state->uuid, uuid_src);
492 qemu_uuid_unparse(&qemu_uuid, uuid_dst);
493 error_report("UUID received is %s and local is %s", uuid_src, uuid_dst);
494 return -EINVAL;
496 return 0;
499 static const VMStateDescription vmstate_uuid = {
500 .name = "configuration/uuid",
501 .version_id = 1,
502 .minimum_version_id = 1,
503 .needed = vmstate_uuid_needed,
504 .post_load = vmstate_uuid_post_load,
505 .fields = (VMStateField[]) {
506 VMSTATE_UINT8_ARRAY_V(uuid.data, SaveState, sizeof(QemuUUID), 1),
507 VMSTATE_END_OF_LIST()
511 static const VMStateDescription vmstate_configuration = {
512 .name = "configuration",
513 .version_id = 1,
514 .pre_load = configuration_pre_load,
515 .post_load = configuration_post_load,
516 .pre_save = configuration_pre_save,
517 .fields = (VMStateField[]) {
518 VMSTATE_UINT32(len, SaveState),
519 VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, len),
520 VMSTATE_END_OF_LIST()
522 .subsections = (const VMStateDescription*[]) {
523 &vmstate_target_page_bits,
524 &vmstate_capabilites,
525 &vmstate_uuid,
526 NULL
530 static void dump_vmstate_vmsd(FILE *out_file,
531 const VMStateDescription *vmsd, int indent,
532 bool is_subsection);
534 static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field,
535 int indent)
537 fprintf(out_file, "%*s{\n", indent, "");
538 indent += 2;
539 fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name);
540 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
541 field->version_id);
542 fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "",
543 field->field_exists ? "true" : "false");
544 fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size);
545 if (field->vmsd != NULL) {
546 fprintf(out_file, ",\n");
547 dump_vmstate_vmsd(out_file, field->vmsd, indent, false);
549 fprintf(out_file, "\n%*s}", indent - 2, "");
552 static void dump_vmstate_vmss(FILE *out_file,
553 const VMStateDescription **subsection,
554 int indent)
556 if (*subsection != NULL) {
557 dump_vmstate_vmsd(out_file, *subsection, indent, true);
561 static void dump_vmstate_vmsd(FILE *out_file,
562 const VMStateDescription *vmsd, int indent,
563 bool is_subsection)
565 if (is_subsection) {
566 fprintf(out_file, "%*s{\n", indent, "");
567 } else {
568 fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description");
570 indent += 2;
571 fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name);
572 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
573 vmsd->version_id);
574 fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "",
575 vmsd->minimum_version_id);
576 if (vmsd->fields != NULL) {
577 const VMStateField *field = vmsd->fields;
578 bool first;
580 fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, "");
581 first = true;
582 while (field->name != NULL) {
583 if (field->flags & VMS_MUST_EXIST) {
584 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
585 field++;
586 continue;
588 if (!first) {
589 fprintf(out_file, ",\n");
591 dump_vmstate_vmsf(out_file, field, indent + 2);
592 field++;
593 first = false;
595 fprintf(out_file, "\n%*s]", indent, "");
597 if (vmsd->subsections != NULL) {
598 const VMStateDescription **subsection = vmsd->subsections;
599 bool first;
601 fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, "");
602 first = true;
603 while (*subsection != NULL) {
604 if (!first) {
605 fprintf(out_file, ",\n");
607 dump_vmstate_vmss(out_file, subsection, indent + 2);
608 subsection++;
609 first = false;
611 fprintf(out_file, "\n%*s]", indent, "");
613 fprintf(out_file, "\n%*s}", indent - 2, "");
616 static void dump_machine_type(FILE *out_file)
618 MachineClass *mc;
620 mc = MACHINE_GET_CLASS(current_machine);
622 fprintf(out_file, " \"vmschkmachine\": {\n");
623 fprintf(out_file, " \"Name\": \"%s\"\n", mc->name);
624 fprintf(out_file, " },\n");
627 void dump_vmstate_json_to_file(FILE *out_file)
629 GSList *list, *elt;
630 bool first;
632 fprintf(out_file, "{\n");
633 dump_machine_type(out_file);
635 first = true;
636 list = object_class_get_list(TYPE_DEVICE, true);
637 for (elt = list; elt; elt = elt->next) {
638 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
639 TYPE_DEVICE);
640 const char *name;
641 int indent = 2;
643 if (!dc->vmsd) {
644 continue;
647 if (!first) {
648 fprintf(out_file, ",\n");
650 name = object_class_get_name(OBJECT_CLASS(dc));
651 fprintf(out_file, "%*s\"%s\": {\n", indent, "", name);
652 indent += 2;
653 fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name);
654 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
655 dc->vmsd->version_id);
656 fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "",
657 dc->vmsd->minimum_version_id);
659 dump_vmstate_vmsd(out_file, dc->vmsd, indent, false);
661 fprintf(out_file, "\n%*s}", indent - 2, "");
662 first = false;
664 fprintf(out_file, "\n}\n");
665 fclose(out_file);
668 static int calculate_new_instance_id(const char *idstr)
670 SaveStateEntry *se;
671 int instance_id = 0;
673 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
674 if (strcmp(idstr, se->idstr) == 0
675 && instance_id <= se->instance_id) {
676 instance_id = se->instance_id + 1;
679 return instance_id;
682 static int calculate_compat_instance_id(const char *idstr)
684 SaveStateEntry *se;
685 int instance_id = 0;
687 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
688 if (!se->compat) {
689 continue;
692 if (strcmp(idstr, se->compat->idstr) == 0
693 && instance_id <= se->compat->instance_id) {
694 instance_id = se->compat->instance_id + 1;
697 return instance_id;
700 static inline MigrationPriority save_state_priority(SaveStateEntry *se)
702 if (se->vmsd) {
703 return se->vmsd->priority;
705 return MIG_PRI_DEFAULT;
708 static void savevm_state_handler_insert(SaveStateEntry *nse)
710 MigrationPriority priority = save_state_priority(nse);
711 SaveStateEntry *se;
713 assert(priority <= MIG_PRI_MAX);
715 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
716 if (save_state_priority(se) < priority) {
717 break;
721 if (se) {
722 QTAILQ_INSERT_BEFORE(se, nse, entry);
723 } else {
724 QTAILQ_INSERT_TAIL(&savevm_state.handlers, nse, entry);
728 /* TODO: Individual devices generally have very little idea about the rest
729 of the system, so instance_id should be removed/replaced.
730 Meanwhile pass -1 as instance_id if you do not already have a clearly
731 distinguishing id for all instances of your device class. */
732 int register_savevm_live(const char *idstr,
733 int instance_id,
734 int version_id,
735 const SaveVMHandlers *ops,
736 void *opaque)
738 SaveStateEntry *se;
740 se = g_new0(SaveStateEntry, 1);
741 se->version_id = version_id;
742 se->section_id = savevm_state.global_section_id++;
743 se->ops = ops;
744 se->opaque = opaque;
745 se->vmsd = NULL;
746 /* if this is a live_savem then set is_ram */
747 if (ops->save_setup != NULL) {
748 se->is_ram = 1;
751 pstrcat(se->idstr, sizeof(se->idstr), idstr);
753 if (instance_id == -1) {
754 se->instance_id = calculate_new_instance_id(se->idstr);
755 } else {
756 se->instance_id = instance_id;
758 assert(!se->compat || se->instance_id == 0);
759 savevm_state_handler_insert(se);
760 return 0;
763 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
765 SaveStateEntry *se, *new_se;
766 char id[256] = "";
768 if (dev) {
769 char *path = qdev_get_dev_path(dev);
770 if (path) {
771 pstrcpy(id, sizeof(id), path);
772 pstrcat(id, sizeof(id), "/");
773 g_free(path);
776 pstrcat(id, sizeof(id), idstr);
778 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
779 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
780 QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
781 g_free(se->compat);
782 g_free(se);
787 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
788 const VMStateDescription *vmsd,
789 void *opaque, int alias_id,
790 int required_for_version,
791 Error **errp)
793 SaveStateEntry *se;
795 /* If this triggers, alias support can be dropped for the vmsd. */
796 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
798 se = g_new0(SaveStateEntry, 1);
799 se->version_id = vmsd->version_id;
800 se->section_id = savevm_state.global_section_id++;
801 se->opaque = opaque;
802 se->vmsd = vmsd;
803 se->alias_id = alias_id;
805 if (dev) {
806 char *id = qdev_get_dev_path(dev);
807 if (id) {
808 if (snprintf(se->idstr, sizeof(se->idstr), "%s/", id) >=
809 sizeof(se->idstr)) {
810 error_setg(errp, "Path too long for VMState (%s)", id);
811 g_free(id);
812 g_free(se);
814 return -1;
816 g_free(id);
818 se->compat = g_new0(CompatEntry, 1);
819 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
820 se->compat->instance_id = instance_id == -1 ?
821 calculate_compat_instance_id(vmsd->name) : instance_id;
822 instance_id = -1;
825 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
827 if (instance_id == -1) {
828 se->instance_id = calculate_new_instance_id(se->idstr);
829 } else {
830 se->instance_id = instance_id;
832 assert(!se->compat || se->instance_id == 0);
833 savevm_state_handler_insert(se);
834 return 0;
837 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
838 void *opaque)
840 SaveStateEntry *se, *new_se;
842 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
843 if (se->vmsd == vmsd && se->opaque == opaque) {
844 QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
845 g_free(se->compat);
846 g_free(se);
851 static int vmstate_load(QEMUFile *f, SaveStateEntry *se)
853 trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
854 if (!se->vmsd) { /* Old style */
855 return se->ops->load_state(f, se->opaque, se->load_version_id);
857 return vmstate_load_state(f, se->vmsd, se->opaque, se->load_version_id);
860 static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
862 int64_t old_offset, size;
864 old_offset = qemu_ftell_fast(f);
865 se->ops->save_state(f, se->opaque);
866 size = qemu_ftell_fast(f) - old_offset;
868 if (vmdesc) {
869 json_prop_int(vmdesc, "size", size);
870 json_start_array(vmdesc, "fields");
871 json_start_object(vmdesc, NULL);
872 json_prop_str(vmdesc, "name", "data");
873 json_prop_int(vmdesc, "size", size);
874 json_prop_str(vmdesc, "type", "buffer");
875 json_end_object(vmdesc);
876 json_end_array(vmdesc);
880 static int vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
882 trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
883 if (!se->vmsd) {
884 vmstate_save_old_style(f, se, vmdesc);
885 return 0;
887 return vmstate_save_state(f, se->vmsd, se->opaque, vmdesc);
891 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
893 static void save_section_header(QEMUFile *f, SaveStateEntry *se,
894 uint8_t section_type)
896 qemu_put_byte(f, section_type);
897 qemu_put_be32(f, se->section_id);
899 if (section_type == QEMU_VM_SECTION_FULL ||
900 section_type == QEMU_VM_SECTION_START) {
901 /* ID string */
902 size_t len = strlen(se->idstr);
903 qemu_put_byte(f, len);
904 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
906 qemu_put_be32(f, se->instance_id);
907 qemu_put_be32(f, se->version_id);
912 * Write a footer onto device sections that catches cases misformatted device
913 * sections.
915 static void save_section_footer(QEMUFile *f, SaveStateEntry *se)
917 if (migrate_get_current()->send_section_footer) {
918 qemu_put_byte(f, QEMU_VM_SECTION_FOOTER);
919 qemu_put_be32(f, se->section_id);
924 * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the
925 * command and associated data.
927 * @f: File to send command on
928 * @command: Command type to send
929 * @len: Length of associated data
930 * @data: Data associated with command.
932 static void qemu_savevm_command_send(QEMUFile *f,
933 enum qemu_vm_cmd command,
934 uint16_t len,
935 uint8_t *data)
937 trace_savevm_command_send(command, len);
938 qemu_put_byte(f, QEMU_VM_COMMAND);
939 qemu_put_be16(f, (uint16_t)command);
940 qemu_put_be16(f, len);
941 qemu_put_buffer(f, data, len);
942 qemu_fflush(f);
945 void qemu_savevm_send_colo_enable(QEMUFile *f)
947 trace_savevm_send_colo_enable();
948 qemu_savevm_command_send(f, MIG_CMD_ENABLE_COLO, 0, NULL);
951 void qemu_savevm_send_ping(QEMUFile *f, uint32_t value)
953 uint32_t buf;
955 trace_savevm_send_ping(value);
956 buf = cpu_to_be32(value);
957 qemu_savevm_command_send(f, MIG_CMD_PING, sizeof(value), (uint8_t *)&buf);
960 void qemu_savevm_send_open_return_path(QEMUFile *f)
962 trace_savevm_send_open_return_path();
963 qemu_savevm_command_send(f, MIG_CMD_OPEN_RETURN_PATH, 0, NULL);
966 /* We have a buffer of data to send; we don't want that all to be loaded
967 * by the command itself, so the command contains just the length of the
968 * extra buffer that we then send straight after it.
969 * TODO: Must be a better way to organise that
971 * Returns:
972 * 0 on success
973 * -ve on error
975 int qemu_savevm_send_packaged(QEMUFile *f, const uint8_t *buf, size_t len)
977 uint32_t tmp;
979 if (len > MAX_VM_CMD_PACKAGED_SIZE) {
980 error_report("%s: Unreasonably large packaged state: %zu",
981 __func__, len);
982 return -1;
985 tmp = cpu_to_be32(len);
987 trace_qemu_savevm_send_packaged();
988 qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp);
990 qemu_put_buffer(f, buf, len);
992 return 0;
995 /* Send prior to any postcopy transfer */
996 void qemu_savevm_send_postcopy_advise(QEMUFile *f)
998 if (migrate_postcopy_ram()) {
999 uint64_t tmp[2];
1000 tmp[0] = cpu_to_be64(ram_pagesize_summary());
1001 tmp[1] = cpu_to_be64(qemu_target_page_size());
1003 trace_qemu_savevm_send_postcopy_advise();
1004 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE,
1005 16, (uint8_t *)tmp);
1006 } else {
1007 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 0, NULL);
1011 /* Sent prior to starting the destination running in postcopy, discard pages
1012 * that have already been sent but redirtied on the source.
1013 * CMD_POSTCOPY_RAM_DISCARD consist of:
1014 * byte version (0)
1015 * byte Length of name field (not including 0)
1016 * n x byte RAM block name
1017 * byte 0 terminator (just for safety)
1018 * n x Byte ranges within the named RAMBlock
1019 * be64 Start of the range
1020 * be64 Length
1022 * name: RAMBlock name that these entries are part of
1023 * len: Number of page entries
1024 * start_list: 'len' addresses
1025 * length_list: 'len' addresses
1028 void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name,
1029 uint16_t len,
1030 uint64_t *start_list,
1031 uint64_t *length_list)
1033 uint8_t *buf;
1034 uint16_t tmplen;
1035 uint16_t t;
1036 size_t name_len = strlen(name);
1038 trace_qemu_savevm_send_postcopy_ram_discard(name, len);
1039 assert(name_len < 256);
1040 buf = g_malloc0(1 + 1 + name_len + 1 + (8 + 8) * len);
1041 buf[0] = postcopy_ram_discard_version;
1042 buf[1] = name_len;
1043 memcpy(buf + 2, name, name_len);
1044 tmplen = 2 + name_len;
1045 buf[tmplen++] = '\0';
1047 for (t = 0; t < len; t++) {
1048 stq_be_p(buf + tmplen, start_list[t]);
1049 tmplen += 8;
1050 stq_be_p(buf + tmplen, length_list[t]);
1051 tmplen += 8;
1053 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RAM_DISCARD, tmplen, buf);
1054 g_free(buf);
1057 /* Get the destination into a state where it can receive postcopy data. */
1058 void qemu_savevm_send_postcopy_listen(QEMUFile *f)
1060 trace_savevm_send_postcopy_listen();
1061 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_LISTEN, 0, NULL);
1064 /* Kick the destination into running */
1065 void qemu_savevm_send_postcopy_run(QEMUFile *f)
1067 trace_savevm_send_postcopy_run();
1068 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RUN, 0, NULL);
1071 void qemu_savevm_send_postcopy_resume(QEMUFile *f)
1073 trace_savevm_send_postcopy_resume();
1074 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RESUME, 0, NULL);
1077 void qemu_savevm_send_recv_bitmap(QEMUFile *f, char *block_name)
1079 size_t len;
1080 char buf[256];
1082 trace_savevm_send_recv_bitmap(block_name);
1084 buf[0] = len = strlen(block_name);
1085 memcpy(buf + 1, block_name, len);
1087 qemu_savevm_command_send(f, MIG_CMD_RECV_BITMAP, len + 1, (uint8_t *)buf);
1090 bool qemu_savevm_state_blocked(Error **errp)
1092 SaveStateEntry *se;
1094 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1095 if (se->vmsd && se->vmsd->unmigratable) {
1096 error_setg(errp, "State blocked by non-migratable device '%s'",
1097 se->idstr);
1098 return true;
1101 return false;
1104 void qemu_savevm_state_header(QEMUFile *f)
1106 trace_savevm_state_header();
1107 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1108 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1110 if (migrate_get_current()->send_configuration) {
1111 qemu_put_byte(f, QEMU_VM_CONFIGURATION);
1112 vmstate_save_state(f, &vmstate_configuration, &savevm_state, 0);
1116 void qemu_savevm_state_setup(QEMUFile *f)
1118 SaveStateEntry *se;
1119 Error *local_err = NULL;
1120 int ret;
1122 trace_savevm_state_setup();
1123 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1124 if (!se->ops || !se->ops->save_setup) {
1125 continue;
1127 if (se->ops->is_active) {
1128 if (!se->ops->is_active(se->opaque)) {
1129 continue;
1132 save_section_header(f, se, QEMU_VM_SECTION_START);
1134 ret = se->ops->save_setup(f, se->opaque);
1135 save_section_footer(f, se);
1136 if (ret < 0) {
1137 qemu_file_set_error(f, ret);
1138 break;
1142 if (precopy_notify(PRECOPY_NOTIFY_SETUP, &local_err)) {
1143 error_report_err(local_err);
1147 int qemu_savevm_state_resume_prepare(MigrationState *s)
1149 SaveStateEntry *se;
1150 int ret;
1152 trace_savevm_state_resume_prepare();
1154 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1155 if (!se->ops || !se->ops->resume_prepare) {
1156 continue;
1158 if (se->ops->is_active) {
1159 if (!se->ops->is_active(se->opaque)) {
1160 continue;
1163 ret = se->ops->resume_prepare(s, se->opaque);
1164 if (ret < 0) {
1165 return ret;
1169 return 0;
1173 * this function has three return values:
1174 * negative: there was one error, and we have -errno.
1175 * 0 : We haven't finished, caller have to go again
1176 * 1 : We have finished, we can go to complete phase
1178 int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy)
1180 SaveStateEntry *se;
1181 int ret = 1;
1183 trace_savevm_state_iterate();
1184 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1185 if (!se->ops || !se->ops->save_live_iterate) {
1186 continue;
1188 if (se->ops->is_active &&
1189 !se->ops->is_active(se->opaque)) {
1190 continue;
1192 if (se->ops->is_active_iterate &&
1193 !se->ops->is_active_iterate(se->opaque)) {
1194 continue;
1197 * In the postcopy phase, any device that doesn't know how to
1198 * do postcopy should have saved it's state in the _complete
1199 * call that's already run, it might get confused if we call
1200 * iterate afterwards.
1202 if (postcopy &&
1203 !(se->ops->has_postcopy && se->ops->has_postcopy(se->opaque))) {
1204 continue;
1206 if (qemu_file_rate_limit(f)) {
1207 return 0;
1209 trace_savevm_section_start(se->idstr, se->section_id);
1211 save_section_header(f, se, QEMU_VM_SECTION_PART);
1213 ret = se->ops->save_live_iterate(f, se->opaque);
1214 trace_savevm_section_end(se->idstr, se->section_id, ret);
1215 save_section_footer(f, se);
1217 if (ret < 0) {
1218 error_report("failed to save SaveStateEntry with id(name): %d(%s)",
1219 se->section_id, se->idstr);
1220 qemu_file_set_error(f, ret);
1222 if (ret <= 0) {
1223 /* Do not proceed to the next vmstate before this one reported
1224 completion of the current stage. This serializes the migration
1225 and reduces the probability that a faster changing state is
1226 synchronized over and over again. */
1227 break;
1230 return ret;
1233 static bool should_send_vmdesc(void)
1235 MachineState *machine = MACHINE(qdev_get_machine());
1236 bool in_postcopy = migration_in_postcopy();
1237 return !machine->suppress_vmdesc && !in_postcopy;
1241 * Calls the save_live_complete_postcopy methods
1242 * causing the last few pages to be sent immediately and doing any associated
1243 * cleanup.
1244 * Note postcopy also calls qemu_savevm_state_complete_precopy to complete
1245 * all the other devices, but that happens at the point we switch to postcopy.
1247 void qemu_savevm_state_complete_postcopy(QEMUFile *f)
1249 SaveStateEntry *se;
1250 int ret;
1252 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1253 if (!se->ops || !se->ops->save_live_complete_postcopy) {
1254 continue;
1256 if (se->ops->is_active) {
1257 if (!se->ops->is_active(se->opaque)) {
1258 continue;
1261 trace_savevm_section_start(se->idstr, se->section_id);
1262 /* Section type */
1263 qemu_put_byte(f, QEMU_VM_SECTION_END);
1264 qemu_put_be32(f, se->section_id);
1266 ret = se->ops->save_live_complete_postcopy(f, se->opaque);
1267 trace_savevm_section_end(se->idstr, se->section_id, ret);
1268 save_section_footer(f, se);
1269 if (ret < 0) {
1270 qemu_file_set_error(f, ret);
1271 return;
1275 qemu_put_byte(f, QEMU_VM_EOF);
1276 qemu_fflush(f);
1279 static
1280 int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)
1282 SaveStateEntry *se;
1283 int ret;
1285 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1286 if (!se->ops ||
1287 (in_postcopy && se->ops->has_postcopy &&
1288 se->ops->has_postcopy(se->opaque)) ||
1289 !se->ops->save_live_complete_precopy) {
1290 continue;
1293 if (se->ops->is_active) {
1294 if (!se->ops->is_active(se->opaque)) {
1295 continue;
1298 trace_savevm_section_start(se->idstr, se->section_id);
1300 save_section_header(f, se, QEMU_VM_SECTION_END);
1302 ret = se->ops->save_live_complete_precopy(f, se->opaque);
1303 trace_savevm_section_end(se->idstr, se->section_id, ret);
1304 save_section_footer(f, se);
1305 if (ret < 0) {
1306 qemu_file_set_error(f, ret);
1307 return -1;
1311 return 0;
1314 static
1315 int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f,
1316 bool in_postcopy,
1317 bool inactivate_disks)
1319 g_autoptr(QJSON) vmdesc = NULL;
1320 int vmdesc_len;
1321 SaveStateEntry *se;
1322 int ret;
1324 vmdesc = qjson_new();
1325 json_prop_int(vmdesc, "page_size", qemu_target_page_size());
1326 json_start_array(vmdesc, "devices");
1327 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1329 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1330 continue;
1332 if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1333 trace_savevm_section_skip(se->idstr, se->section_id);
1334 continue;
1337 trace_savevm_section_start(se->idstr, se->section_id);
1339 json_start_object(vmdesc, NULL);
1340 json_prop_str(vmdesc, "name", se->idstr);
1341 json_prop_int(vmdesc, "instance_id", se->instance_id);
1343 save_section_header(f, se, QEMU_VM_SECTION_FULL);
1344 ret = vmstate_save(f, se, vmdesc);
1345 if (ret) {
1346 qemu_file_set_error(f, ret);
1347 return ret;
1349 trace_savevm_section_end(se->idstr, se->section_id, 0);
1350 save_section_footer(f, se);
1352 json_end_object(vmdesc);
1355 if (inactivate_disks) {
1356 /* Inactivate before sending QEMU_VM_EOF so that the
1357 * bdrv_invalidate_cache_all() on the other end won't fail. */
1358 ret = bdrv_inactivate_all();
1359 if (ret) {
1360 error_report("%s: bdrv_inactivate_all() failed (%d)",
1361 __func__, ret);
1362 qemu_file_set_error(f, ret);
1363 return ret;
1366 if (!in_postcopy) {
1367 /* Postcopy stream will still be going */
1368 qemu_put_byte(f, QEMU_VM_EOF);
1371 json_end_array(vmdesc);
1372 qjson_finish(vmdesc);
1373 vmdesc_len = strlen(qjson_get_str(vmdesc));
1375 if (should_send_vmdesc()) {
1376 qemu_put_byte(f, QEMU_VM_VMDESCRIPTION);
1377 qemu_put_be32(f, vmdesc_len);
1378 qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len);
1381 return 0;
1384 int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only,
1385 bool inactivate_disks)
1387 int ret;
1388 Error *local_err = NULL;
1389 bool in_postcopy = migration_in_postcopy();
1391 if (precopy_notify(PRECOPY_NOTIFY_COMPLETE, &local_err)) {
1392 error_report_err(local_err);
1395 trace_savevm_state_complete_precopy();
1397 cpu_synchronize_all_states();
1399 if (!in_postcopy || iterable_only) {
1400 ret = qemu_savevm_state_complete_precopy_iterable(f, in_postcopy);
1401 if (ret) {
1402 return ret;
1406 if (iterable_only) {
1407 goto flush;
1410 ret = qemu_savevm_state_complete_precopy_non_iterable(f, in_postcopy,
1411 inactivate_disks);
1412 if (ret) {
1413 return ret;
1416 flush:
1417 qemu_fflush(f);
1418 return 0;
1421 /* Give an estimate of the amount left to be transferred,
1422 * the result is split into the amount for units that can and
1423 * for units that can't do postcopy.
1425 void qemu_savevm_state_pending(QEMUFile *f, uint64_t threshold_size,
1426 uint64_t *res_precopy_only,
1427 uint64_t *res_compatible,
1428 uint64_t *res_postcopy_only)
1430 SaveStateEntry *se;
1432 *res_precopy_only = 0;
1433 *res_compatible = 0;
1434 *res_postcopy_only = 0;
1437 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1438 if (!se->ops || !se->ops->save_live_pending) {
1439 continue;
1441 if (se->ops->is_active) {
1442 if (!se->ops->is_active(se->opaque)) {
1443 continue;
1446 se->ops->save_live_pending(f, se->opaque, threshold_size,
1447 res_precopy_only, res_compatible,
1448 res_postcopy_only);
1452 void qemu_savevm_state_cleanup(void)
1454 SaveStateEntry *se;
1455 Error *local_err = NULL;
1457 if (precopy_notify(PRECOPY_NOTIFY_CLEANUP, &local_err)) {
1458 error_report_err(local_err);
1461 trace_savevm_state_cleanup();
1462 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1463 if (se->ops && se->ops->save_cleanup) {
1464 se->ops->save_cleanup(se->opaque);
1469 static int qemu_savevm_state(QEMUFile *f, Error **errp)
1471 int ret;
1472 MigrationState *ms = migrate_get_current();
1473 MigrationStatus status;
1475 if (migration_is_setup_or_active(ms->state) ||
1476 ms->state == MIGRATION_STATUS_CANCELLING ||
1477 ms->state == MIGRATION_STATUS_COLO) {
1478 error_setg(errp, QERR_MIGRATION_ACTIVE);
1479 return -EINVAL;
1482 if (migrate_use_block()) {
1483 error_setg(errp, "Block migration and snapshots are incompatible");
1484 return -EINVAL;
1487 migrate_init(ms);
1488 memset(&ram_counters, 0, sizeof(ram_counters));
1489 ms->to_dst_file = f;
1491 qemu_mutex_unlock_iothread();
1492 qemu_savevm_state_header(f);
1493 qemu_savevm_state_setup(f);
1494 qemu_mutex_lock_iothread();
1496 while (qemu_file_get_error(f) == 0) {
1497 if (qemu_savevm_state_iterate(f, false) > 0) {
1498 break;
1502 ret = qemu_file_get_error(f);
1503 if (ret == 0) {
1504 qemu_savevm_state_complete_precopy(f, false, false);
1505 ret = qemu_file_get_error(f);
1507 qemu_savevm_state_cleanup();
1508 if (ret != 0) {
1509 error_setg_errno(errp, -ret, "Error while writing VM state");
1512 if (ret != 0) {
1513 status = MIGRATION_STATUS_FAILED;
1514 } else {
1515 status = MIGRATION_STATUS_COMPLETED;
1517 migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP, status);
1519 /* f is outer parameter, it should not stay in global migration state after
1520 * this function finished */
1521 ms->to_dst_file = NULL;
1523 return ret;
1526 void qemu_savevm_live_state(QEMUFile *f)
1528 /* save QEMU_VM_SECTION_END section */
1529 qemu_savevm_state_complete_precopy(f, true, false);
1530 qemu_put_byte(f, QEMU_VM_EOF);
1533 int qemu_save_device_state(QEMUFile *f)
1535 SaveStateEntry *se;
1537 if (!migration_in_colo_state()) {
1538 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1539 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1541 cpu_synchronize_all_states();
1543 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1544 int ret;
1546 if (se->is_ram) {
1547 continue;
1549 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1550 continue;
1552 if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1553 continue;
1556 save_section_header(f, se, QEMU_VM_SECTION_FULL);
1558 ret = vmstate_save(f, se, NULL);
1559 if (ret) {
1560 return ret;
1563 save_section_footer(f, se);
1566 qemu_put_byte(f, QEMU_VM_EOF);
1568 return qemu_file_get_error(f);
1571 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1573 SaveStateEntry *se;
1575 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1576 if (!strcmp(se->idstr, idstr) &&
1577 (instance_id == se->instance_id ||
1578 instance_id == se->alias_id))
1579 return se;
1580 /* Migrating from an older version? */
1581 if (strstr(se->idstr, idstr) && se->compat) {
1582 if (!strcmp(se->compat->idstr, idstr) &&
1583 (instance_id == se->compat->instance_id ||
1584 instance_id == se->alias_id))
1585 return se;
1588 return NULL;
1591 enum LoadVMExitCodes {
1592 /* Allow a command to quit all layers of nested loadvm loops */
1593 LOADVM_QUIT = 1,
1596 /* ------ incoming postcopy messages ------ */
1597 /* 'advise' arrives before any transfers just to tell us that a postcopy
1598 * *might* happen - it might be skipped if precopy transferred everything
1599 * quickly.
1601 static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis,
1602 uint16_t len)
1604 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
1605 uint64_t remote_pagesize_summary, local_pagesize_summary, remote_tps;
1606 Error *local_err = NULL;
1608 trace_loadvm_postcopy_handle_advise();
1609 if (ps != POSTCOPY_INCOMING_NONE) {
1610 error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps);
1611 return -1;
1614 switch (len) {
1615 case 0:
1616 if (migrate_postcopy_ram()) {
1617 error_report("RAM postcopy is enabled but have 0 byte advise");
1618 return -EINVAL;
1620 return 0;
1621 case 8 + 8:
1622 if (!migrate_postcopy_ram()) {
1623 error_report("RAM postcopy is disabled but have 16 byte advise");
1624 return -EINVAL;
1626 break;
1627 default:
1628 error_report("CMD_POSTCOPY_ADVISE invalid length (%d)", len);
1629 return -EINVAL;
1632 if (!postcopy_ram_supported_by_host(mis)) {
1633 postcopy_state_set(POSTCOPY_INCOMING_NONE);
1634 return -1;
1637 remote_pagesize_summary = qemu_get_be64(mis->from_src_file);
1638 local_pagesize_summary = ram_pagesize_summary();
1640 if (remote_pagesize_summary != local_pagesize_summary) {
1642 * This detects two potential causes of mismatch:
1643 * a) A mismatch in host page sizes
1644 * Some combinations of mismatch are probably possible but it gets
1645 * a bit more complicated. In particular we need to place whole
1646 * host pages on the dest at once, and we need to ensure that we
1647 * handle dirtying to make sure we never end up sending part of
1648 * a hostpage on it's own.
1649 * b) The use of different huge page sizes on source/destination
1650 * a more fine grain test is performed during RAM block migration
1651 * but this test here causes a nice early clear failure, and
1652 * also fails when passed to an older qemu that doesn't
1653 * do huge pages.
1655 error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64
1656 " d=%" PRIx64 ")",
1657 remote_pagesize_summary, local_pagesize_summary);
1658 return -1;
1661 remote_tps = qemu_get_be64(mis->from_src_file);
1662 if (remote_tps != qemu_target_page_size()) {
1664 * Again, some differences could be dealt with, but for now keep it
1665 * simple.
1667 error_report("Postcopy needs matching target page sizes (s=%d d=%zd)",
1668 (int)remote_tps, qemu_target_page_size());
1669 return -1;
1672 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_ADVISE, &local_err)) {
1673 error_report_err(local_err);
1674 return -1;
1677 if (ram_postcopy_incoming_init(mis)) {
1678 return -1;
1681 return 0;
1684 /* After postcopy we will be told to throw some pages away since they're
1685 * dirty and will have to be demand fetched. Must happen before CPU is
1686 * started.
1687 * There can be 0..many of these messages, each encoding multiple pages.
1689 static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis,
1690 uint16_t len)
1692 int tmp;
1693 char ramid[256];
1694 PostcopyState ps = postcopy_state_get();
1696 trace_loadvm_postcopy_ram_handle_discard();
1698 switch (ps) {
1699 case POSTCOPY_INCOMING_ADVISE:
1700 /* 1st discard */
1701 tmp = postcopy_ram_prepare_discard(mis);
1702 if (tmp) {
1703 return tmp;
1705 break;
1707 case POSTCOPY_INCOMING_DISCARD:
1708 /* Expected state */
1709 break;
1711 default:
1712 error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)",
1713 ps);
1714 return -1;
1716 /* We're expecting a
1717 * Version (0)
1718 * a RAM ID string (length byte, name, 0 term)
1719 * then at least 1 16 byte chunk
1721 if (len < (1 + 1 + 1 + 1 + 2 * 8)) {
1722 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1723 return -1;
1726 tmp = qemu_get_byte(mis->from_src_file);
1727 if (tmp != postcopy_ram_discard_version) {
1728 error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp);
1729 return -1;
1732 if (!qemu_get_counted_string(mis->from_src_file, ramid)) {
1733 error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID");
1734 return -1;
1736 tmp = qemu_get_byte(mis->from_src_file);
1737 if (tmp != 0) {
1738 error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp);
1739 return -1;
1742 len -= 3 + strlen(ramid);
1743 if (len % 16) {
1744 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1745 return -1;
1747 trace_loadvm_postcopy_ram_handle_discard_header(ramid, len);
1748 while (len) {
1749 uint64_t start_addr, block_length;
1750 start_addr = qemu_get_be64(mis->from_src_file);
1751 block_length = qemu_get_be64(mis->from_src_file);
1753 len -= 16;
1754 int ret = ram_discard_range(ramid, start_addr, block_length);
1755 if (ret) {
1756 return ret;
1759 trace_loadvm_postcopy_ram_handle_discard_end();
1761 return 0;
1765 * Triggered by a postcopy_listen command; this thread takes over reading
1766 * the input stream, leaving the main thread free to carry on loading the rest
1767 * of the device state (from RAM).
1768 * (TODO:This could do with being in a postcopy file - but there again it's
1769 * just another input loop, not that postcopy specific)
1771 static void *postcopy_ram_listen_thread(void *opaque)
1773 MigrationIncomingState *mis = migration_incoming_get_current();
1774 QEMUFile *f = mis->from_src_file;
1775 int load_res;
1777 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
1778 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1779 qemu_sem_post(&mis->listen_thread_sem);
1780 trace_postcopy_ram_listen_thread_start();
1782 rcu_register_thread();
1784 * Because we're a thread and not a coroutine we can't yield
1785 * in qemu_file, and thus we must be blocking now.
1787 qemu_file_set_blocking(f, true);
1788 load_res = qemu_loadvm_state_main(f, mis);
1791 * This is tricky, but, mis->from_src_file can change after it
1792 * returns, when postcopy recovery happened. In the future, we may
1793 * want a wrapper for the QEMUFile handle.
1795 f = mis->from_src_file;
1797 /* And non-blocking again so we don't block in any cleanup */
1798 qemu_file_set_blocking(f, false);
1800 trace_postcopy_ram_listen_thread_exit();
1801 if (load_res < 0) {
1802 error_report("%s: loadvm failed: %d", __func__, load_res);
1803 qemu_file_set_error(f, load_res);
1804 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1805 MIGRATION_STATUS_FAILED);
1806 } else {
1808 * This looks good, but it's possible that the device loading in the
1809 * main thread hasn't finished yet, and so we might not be in 'RUN'
1810 * state yet; wait for the end of the main thread.
1812 qemu_event_wait(&mis->main_thread_load_event);
1814 postcopy_ram_incoming_cleanup(mis);
1816 if (load_res < 0) {
1818 * If something went wrong then we have a bad state so exit;
1819 * depending how far we got it might be possible at this point
1820 * to leave the guest running and fire MCEs for pages that never
1821 * arrived as a desperate recovery step.
1823 rcu_unregister_thread();
1824 exit(EXIT_FAILURE);
1827 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1828 MIGRATION_STATUS_COMPLETED);
1830 * If everything has worked fine, then the main thread has waited
1831 * for us to start, and we're the last use of the mis.
1832 * (If something broke then qemu will have to exit anyway since it's
1833 * got a bad migration state).
1835 migration_incoming_state_destroy();
1836 qemu_loadvm_state_cleanup();
1838 rcu_unregister_thread();
1839 mis->have_listen_thread = false;
1840 postcopy_state_set(POSTCOPY_INCOMING_END);
1842 return NULL;
1845 /* After this message we must be able to immediately receive postcopy data */
1846 static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)
1848 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_LISTENING);
1849 trace_loadvm_postcopy_handle_listen();
1850 Error *local_err = NULL;
1852 if (ps != POSTCOPY_INCOMING_ADVISE && ps != POSTCOPY_INCOMING_DISCARD) {
1853 error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps);
1854 return -1;
1856 if (ps == POSTCOPY_INCOMING_ADVISE) {
1858 * A rare case, we entered listen without having to do any discards,
1859 * so do the setup that's normally done at the time of the 1st discard.
1861 if (migrate_postcopy_ram()) {
1862 postcopy_ram_prepare_discard(mis);
1867 * Sensitise RAM - can now generate requests for blocks that don't exist
1868 * However, at this point the CPU shouldn't be running, and the IO
1869 * shouldn't be doing anything yet so don't actually expect requests
1871 if (migrate_postcopy_ram()) {
1872 if (postcopy_ram_incoming_setup(mis)) {
1873 postcopy_ram_incoming_cleanup(mis);
1874 return -1;
1878 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN, &local_err)) {
1879 error_report_err(local_err);
1880 return -1;
1883 mis->have_listen_thread = true;
1884 /* Start up the listening thread and wait for it to signal ready */
1885 qemu_sem_init(&mis->listen_thread_sem, 0);
1886 qemu_thread_create(&mis->listen_thread, "postcopy/listen",
1887 postcopy_ram_listen_thread, NULL,
1888 QEMU_THREAD_DETACHED);
1889 qemu_sem_wait(&mis->listen_thread_sem);
1890 qemu_sem_destroy(&mis->listen_thread_sem);
1892 return 0;
1895 static void loadvm_postcopy_handle_run_bh(void *opaque)
1897 Error *local_err = NULL;
1898 MigrationIncomingState *mis = opaque;
1900 /* TODO we should move all of this lot into postcopy_ram.c or a shared code
1901 * in migration.c
1903 cpu_synchronize_all_post_init();
1905 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
1907 /* Make sure all file formats flush their mutable metadata.
1908 * If we get an error here, just don't restart the VM yet. */
1909 bdrv_invalidate_cache_all(&local_err);
1910 if (local_err) {
1911 error_report_err(local_err);
1912 local_err = NULL;
1913 autostart = false;
1916 trace_loadvm_postcopy_handle_run_cpu_sync();
1918 trace_loadvm_postcopy_handle_run_vmstart();
1920 dirty_bitmap_mig_before_vm_start();
1922 if (autostart) {
1923 /* Hold onto your hats, starting the CPU */
1924 vm_start();
1925 } else {
1926 /* leave it paused and let management decide when to start the CPU */
1927 runstate_set(RUN_STATE_PAUSED);
1930 qemu_bh_delete(mis->bh);
1933 /* After all discards we can start running and asking for pages */
1934 static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
1936 PostcopyState ps = postcopy_state_get();
1938 trace_loadvm_postcopy_handle_run();
1939 if (ps != POSTCOPY_INCOMING_LISTENING) {
1940 error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps);
1941 return -1;
1944 postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
1945 mis->bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, mis);
1946 qemu_bh_schedule(mis->bh);
1948 /* We need to finish reading the stream from the package
1949 * and also stop reading anything more from the stream that loaded the
1950 * package (since it's now being read by the listener thread).
1951 * LOADVM_QUIT will quit all the layers of nested loadvm loops.
1953 return LOADVM_QUIT;
1956 static int loadvm_postcopy_handle_resume(MigrationIncomingState *mis)
1958 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
1959 error_report("%s: illegal resume received", __func__);
1960 /* Don't fail the load, only for this. */
1961 return 0;
1965 * This means source VM is ready to resume the postcopy migration.
1966 * It's time to switch state and release the fault thread to
1967 * continue service page faults.
1969 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
1970 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1971 qemu_sem_post(&mis->postcopy_pause_sem_fault);
1973 trace_loadvm_postcopy_handle_resume();
1975 /* Tell source that "we are ready" */
1976 migrate_send_rp_resume_ack(mis, MIGRATION_RESUME_ACK_VALUE);
1978 return 0;
1982 * Immediately following this command is a blob of data containing an embedded
1983 * chunk of migration stream; read it and load it.
1985 * @mis: Incoming state
1986 * @length: Length of packaged data to read
1988 * Returns: Negative values on error
1991 static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
1993 int ret;
1994 size_t length;
1995 QIOChannelBuffer *bioc;
1997 length = qemu_get_be32(mis->from_src_file);
1998 trace_loadvm_handle_cmd_packaged(length);
2000 if (length > MAX_VM_CMD_PACKAGED_SIZE) {
2001 error_report("Unreasonably large packaged state: %zu", length);
2002 return -1;
2005 bioc = qio_channel_buffer_new(length);
2006 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-loadvm-buffer");
2007 ret = qemu_get_buffer(mis->from_src_file,
2008 bioc->data,
2009 length);
2010 if (ret != length) {
2011 object_unref(OBJECT(bioc));
2012 error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu",
2013 ret, length);
2014 return (ret < 0) ? ret : -EAGAIN;
2016 bioc->usage += length;
2017 trace_loadvm_handle_cmd_packaged_received(ret);
2019 QEMUFile *packf = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
2021 ret = qemu_loadvm_state_main(packf, mis);
2022 trace_loadvm_handle_cmd_packaged_main(ret);
2023 qemu_fclose(packf);
2024 object_unref(OBJECT(bioc));
2026 return ret;
2030 * Handle request that source requests for recved_bitmap on
2031 * destination. Payload format:
2033 * len (1 byte) + ramblock_name (<255 bytes)
2035 static int loadvm_handle_recv_bitmap(MigrationIncomingState *mis,
2036 uint16_t len)
2038 QEMUFile *file = mis->from_src_file;
2039 RAMBlock *rb;
2040 char block_name[256];
2041 size_t cnt;
2043 cnt = qemu_get_counted_string(file, block_name);
2044 if (!cnt) {
2045 error_report("%s: failed to read block name", __func__);
2046 return -EINVAL;
2049 /* Validate before using the data */
2050 if (qemu_file_get_error(file)) {
2051 return qemu_file_get_error(file);
2054 if (len != cnt + 1) {
2055 error_report("%s: invalid payload length (%d)", __func__, len);
2056 return -EINVAL;
2059 rb = qemu_ram_block_by_name(block_name);
2060 if (!rb) {
2061 error_report("%s: block '%s' not found", __func__, block_name);
2062 return -EINVAL;
2065 migrate_send_rp_recv_bitmap(mis, block_name);
2067 trace_loadvm_handle_recv_bitmap(block_name);
2069 return 0;
2072 static int loadvm_process_enable_colo(MigrationIncomingState *mis)
2074 migration_incoming_enable_colo();
2075 return colo_init_ram_cache();
2079 * Process an incoming 'QEMU_VM_COMMAND'
2080 * 0 just a normal return
2081 * LOADVM_QUIT All good, but exit the loop
2082 * <0 Error
2084 static int loadvm_process_command(QEMUFile *f)
2086 MigrationIncomingState *mis = migration_incoming_get_current();
2087 uint16_t cmd;
2088 uint16_t len;
2089 uint32_t tmp32;
2091 cmd = qemu_get_be16(f);
2092 len = qemu_get_be16(f);
2094 /* Check validity before continue processing of cmds */
2095 if (qemu_file_get_error(f)) {
2096 return qemu_file_get_error(f);
2099 trace_loadvm_process_command(cmd, len);
2100 if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) {
2101 error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len);
2102 return -EINVAL;
2105 if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) {
2106 error_report("%s received with bad length - expecting %zu, got %d",
2107 mig_cmd_args[cmd].name,
2108 (size_t)mig_cmd_args[cmd].len, len);
2109 return -ERANGE;
2112 switch (cmd) {
2113 case MIG_CMD_OPEN_RETURN_PATH:
2114 if (mis->to_src_file) {
2115 error_report("CMD_OPEN_RETURN_PATH called when RP already open");
2116 /* Not really a problem, so don't give up */
2117 return 0;
2119 mis->to_src_file = qemu_file_get_return_path(f);
2120 if (!mis->to_src_file) {
2121 error_report("CMD_OPEN_RETURN_PATH failed");
2122 return -1;
2124 break;
2126 case MIG_CMD_PING:
2127 tmp32 = qemu_get_be32(f);
2128 trace_loadvm_process_command_ping(tmp32);
2129 if (!mis->to_src_file) {
2130 error_report("CMD_PING (0x%x) received with no return path",
2131 tmp32);
2132 return -1;
2134 migrate_send_rp_pong(mis, tmp32);
2135 break;
2137 case MIG_CMD_PACKAGED:
2138 return loadvm_handle_cmd_packaged(mis);
2140 case MIG_CMD_POSTCOPY_ADVISE:
2141 return loadvm_postcopy_handle_advise(mis, len);
2143 case MIG_CMD_POSTCOPY_LISTEN:
2144 return loadvm_postcopy_handle_listen(mis);
2146 case MIG_CMD_POSTCOPY_RUN:
2147 return loadvm_postcopy_handle_run(mis);
2149 case MIG_CMD_POSTCOPY_RAM_DISCARD:
2150 return loadvm_postcopy_ram_handle_discard(mis, len);
2152 case MIG_CMD_POSTCOPY_RESUME:
2153 return loadvm_postcopy_handle_resume(mis);
2155 case MIG_CMD_RECV_BITMAP:
2156 return loadvm_handle_recv_bitmap(mis, len);
2158 case MIG_CMD_ENABLE_COLO:
2159 return loadvm_process_enable_colo(mis);
2162 return 0;
2166 * Read a footer off the wire and check that it matches the expected section
2168 * Returns: true if the footer was good
2169 * false if there is a problem (and calls error_report to say why)
2171 static bool check_section_footer(QEMUFile *f, SaveStateEntry *se)
2173 int ret;
2174 uint8_t read_mark;
2175 uint32_t read_section_id;
2177 if (!migrate_get_current()->send_section_footer) {
2178 /* No footer to check */
2179 return true;
2182 read_mark = qemu_get_byte(f);
2184 ret = qemu_file_get_error(f);
2185 if (ret) {
2186 error_report("%s: Read section footer failed: %d",
2187 __func__, ret);
2188 return false;
2191 if (read_mark != QEMU_VM_SECTION_FOOTER) {
2192 error_report("Missing section footer for %s", se->idstr);
2193 return false;
2196 read_section_id = qemu_get_be32(f);
2197 if (read_section_id != se->load_section_id) {
2198 error_report("Mismatched section id in footer for %s -"
2199 " read 0x%x expected 0x%x",
2200 se->idstr, read_section_id, se->load_section_id);
2201 return false;
2204 /* All good */
2205 return true;
2208 static int
2209 qemu_loadvm_section_start_full(QEMUFile *f, MigrationIncomingState *mis)
2211 uint32_t instance_id, version_id, section_id;
2212 SaveStateEntry *se;
2213 char idstr[256];
2214 int ret;
2216 /* Read section start */
2217 section_id = qemu_get_be32(f);
2218 if (!qemu_get_counted_string(f, idstr)) {
2219 error_report("Unable to read ID string for section %u",
2220 section_id);
2221 return -EINVAL;
2223 instance_id = qemu_get_be32(f);
2224 version_id = qemu_get_be32(f);
2226 ret = qemu_file_get_error(f);
2227 if (ret) {
2228 error_report("%s: Failed to read instance/version ID: %d",
2229 __func__, ret);
2230 return ret;
2233 trace_qemu_loadvm_state_section_startfull(section_id, idstr,
2234 instance_id, version_id);
2235 /* Find savevm section */
2236 se = find_se(idstr, instance_id);
2237 if (se == NULL) {
2238 error_report("Unknown savevm section or instance '%s' %d. "
2239 "Make sure that your current VM setup matches your "
2240 "saved VM setup, including any hotplugged devices",
2241 idstr, instance_id);
2242 return -EINVAL;
2245 /* Validate version */
2246 if (version_id > se->version_id) {
2247 error_report("savevm: unsupported version %d for '%s' v%d",
2248 version_id, idstr, se->version_id);
2249 return -EINVAL;
2251 se->load_version_id = version_id;
2252 se->load_section_id = section_id;
2254 /* Validate if it is a device's state */
2255 if (xen_enabled() && se->is_ram) {
2256 error_report("loadvm: %s RAM loading not allowed on Xen", idstr);
2257 return -EINVAL;
2260 ret = vmstate_load(f, se);
2261 if (ret < 0) {
2262 error_report("error while loading state for instance 0x%x of"
2263 " device '%s'", instance_id, idstr);
2264 return ret;
2266 if (!check_section_footer(f, se)) {
2267 return -EINVAL;
2270 return 0;
2273 static int
2274 qemu_loadvm_section_part_end(QEMUFile *f, MigrationIncomingState *mis)
2276 uint32_t section_id;
2277 SaveStateEntry *se;
2278 int ret;
2280 section_id = qemu_get_be32(f);
2282 ret = qemu_file_get_error(f);
2283 if (ret) {
2284 error_report("%s: Failed to read section ID: %d",
2285 __func__, ret);
2286 return ret;
2289 trace_qemu_loadvm_state_section_partend(section_id);
2290 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2291 if (se->load_section_id == section_id) {
2292 break;
2295 if (se == NULL) {
2296 error_report("Unknown savevm section %d", section_id);
2297 return -EINVAL;
2300 ret = vmstate_load(f, se);
2301 if (ret < 0) {
2302 error_report("error while loading state section id %d(%s)",
2303 section_id, se->idstr);
2304 return ret;
2306 if (!check_section_footer(f, se)) {
2307 return -EINVAL;
2310 return 0;
2313 static int qemu_loadvm_state_header(QEMUFile *f)
2315 unsigned int v;
2316 int ret;
2318 v = qemu_get_be32(f);
2319 if (v != QEMU_VM_FILE_MAGIC) {
2320 error_report("Not a migration stream");
2321 return -EINVAL;
2324 v = qemu_get_be32(f);
2325 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2326 error_report("SaveVM v2 format is obsolete and don't work anymore");
2327 return -ENOTSUP;
2329 if (v != QEMU_VM_FILE_VERSION) {
2330 error_report("Unsupported migration stream version");
2331 return -ENOTSUP;
2334 if (migrate_get_current()->send_configuration) {
2335 if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) {
2336 error_report("Configuration section missing");
2337 qemu_loadvm_state_cleanup();
2338 return -EINVAL;
2340 ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0);
2342 if (ret) {
2343 qemu_loadvm_state_cleanup();
2344 return ret;
2347 return 0;
2350 static int qemu_loadvm_state_setup(QEMUFile *f)
2352 SaveStateEntry *se;
2353 int ret;
2355 trace_loadvm_state_setup();
2356 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2357 if (!se->ops || !se->ops->load_setup) {
2358 continue;
2360 if (se->ops->is_active) {
2361 if (!se->ops->is_active(se->opaque)) {
2362 continue;
2366 ret = se->ops->load_setup(f, se->opaque);
2367 if (ret < 0) {
2368 qemu_file_set_error(f, ret);
2369 error_report("Load state of device %s failed", se->idstr);
2370 return ret;
2373 return 0;
2376 void qemu_loadvm_state_cleanup(void)
2378 SaveStateEntry *se;
2380 trace_loadvm_state_cleanup();
2381 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2382 if (se->ops && se->ops->load_cleanup) {
2383 se->ops->load_cleanup(se->opaque);
2388 /* Return true if we should continue the migration, or false. */
2389 static bool postcopy_pause_incoming(MigrationIncomingState *mis)
2391 trace_postcopy_pause_incoming();
2393 /* Clear the triggered bit to allow one recovery */
2394 mis->postcopy_recover_triggered = false;
2396 assert(mis->from_src_file);
2397 qemu_file_shutdown(mis->from_src_file);
2398 qemu_fclose(mis->from_src_file);
2399 mis->from_src_file = NULL;
2401 assert(mis->to_src_file);
2402 qemu_file_shutdown(mis->to_src_file);
2403 qemu_mutex_lock(&mis->rp_mutex);
2404 qemu_fclose(mis->to_src_file);
2405 mis->to_src_file = NULL;
2406 qemu_mutex_unlock(&mis->rp_mutex);
2408 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2409 MIGRATION_STATUS_POSTCOPY_PAUSED);
2411 /* Notify the fault thread for the invalidated file handle */
2412 postcopy_fault_thread_notify(mis);
2414 error_report("Detected IO failure for postcopy. "
2415 "Migration paused.");
2417 while (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2418 qemu_sem_wait(&mis->postcopy_pause_sem_dst);
2421 trace_postcopy_pause_incoming_continued();
2423 return true;
2426 int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis)
2428 uint8_t section_type;
2429 int ret = 0;
2431 retry:
2432 while (true) {
2433 section_type = qemu_get_byte(f);
2435 if (qemu_file_get_error(f)) {
2436 ret = qemu_file_get_error(f);
2437 break;
2440 trace_qemu_loadvm_state_section(section_type);
2441 switch (section_type) {
2442 case QEMU_VM_SECTION_START:
2443 case QEMU_VM_SECTION_FULL:
2444 ret = qemu_loadvm_section_start_full(f, mis);
2445 if (ret < 0) {
2446 goto out;
2448 break;
2449 case QEMU_VM_SECTION_PART:
2450 case QEMU_VM_SECTION_END:
2451 ret = qemu_loadvm_section_part_end(f, mis);
2452 if (ret < 0) {
2453 goto out;
2455 break;
2456 case QEMU_VM_COMMAND:
2457 ret = loadvm_process_command(f);
2458 trace_qemu_loadvm_state_section_command(ret);
2459 if ((ret < 0) || (ret == LOADVM_QUIT)) {
2460 goto out;
2462 break;
2463 case QEMU_VM_EOF:
2464 /* This is the end of migration */
2465 goto out;
2466 default:
2467 error_report("Unknown savevm section type %d", section_type);
2468 ret = -EINVAL;
2469 goto out;
2473 out:
2474 if (ret < 0) {
2475 qemu_file_set_error(f, ret);
2478 * If we are during an active postcopy, then we pause instead
2479 * of bail out to at least keep the VM's dirty data. Note
2480 * that POSTCOPY_INCOMING_LISTENING stage is still not enough,
2481 * during which we're still receiving device states and we
2482 * still haven't yet started the VM on destination.
2484 if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING &&
2485 postcopy_pause_incoming(mis)) {
2486 /* Reset f to point to the newly created channel */
2487 f = mis->from_src_file;
2488 goto retry;
2491 return ret;
2494 int qemu_loadvm_state(QEMUFile *f)
2496 MigrationIncomingState *mis = migration_incoming_get_current();
2497 Error *local_err = NULL;
2498 int ret;
2500 if (qemu_savevm_state_blocked(&local_err)) {
2501 error_report_err(local_err);
2502 return -EINVAL;
2505 ret = qemu_loadvm_state_header(f);
2506 if (ret) {
2507 return ret;
2510 if (qemu_loadvm_state_setup(f) != 0) {
2511 return -EINVAL;
2514 cpu_synchronize_all_pre_loadvm();
2516 ret = qemu_loadvm_state_main(f, mis);
2517 qemu_event_set(&mis->main_thread_load_event);
2519 trace_qemu_loadvm_state_post_main(ret);
2521 if (mis->have_listen_thread) {
2522 /* Listen thread still going, can't clean up yet */
2523 return ret;
2526 if (ret == 0) {
2527 ret = qemu_file_get_error(f);
2531 * Try to read in the VMDESC section as well, so that dumping tools that
2532 * intercept our migration stream have the chance to see it.
2535 /* We've got to be careful; if we don't read the data and just shut the fd
2536 * then the sender can error if we close while it's still sending.
2537 * We also mustn't read data that isn't there; some transports (RDMA)
2538 * will stall waiting for that data when the source has already closed.
2540 if (ret == 0 && should_send_vmdesc()) {
2541 uint8_t *buf;
2542 uint32_t size;
2543 uint8_t section_type = qemu_get_byte(f);
2545 if (section_type != QEMU_VM_VMDESCRIPTION) {
2546 error_report("Expected vmdescription section, but got %d",
2547 section_type);
2549 * It doesn't seem worth failing at this point since
2550 * we apparently have an otherwise valid VM state
2552 } else {
2553 buf = g_malloc(0x1000);
2554 size = qemu_get_be32(f);
2556 while (size > 0) {
2557 uint32_t read_chunk = MIN(size, 0x1000);
2558 qemu_get_buffer(f, buf, read_chunk);
2559 size -= read_chunk;
2561 g_free(buf);
2565 qemu_loadvm_state_cleanup();
2566 cpu_synchronize_all_post_init();
2568 return ret;
2571 int qemu_load_device_state(QEMUFile *f)
2573 MigrationIncomingState *mis = migration_incoming_get_current();
2574 int ret;
2576 /* Load QEMU_VM_SECTION_FULL section */
2577 ret = qemu_loadvm_state_main(f, mis);
2578 if (ret < 0) {
2579 error_report("Failed to load device state: %d", ret);
2580 return ret;
2583 cpu_synchronize_all_post_init();
2584 return 0;
2587 int save_snapshot(const char *name, Error **errp)
2589 BlockDriverState *bs, *bs1;
2590 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2591 int ret = -1;
2592 QEMUFile *f;
2593 int saved_vm_running;
2594 uint64_t vm_state_size;
2595 qemu_timeval tv;
2596 struct tm tm;
2597 AioContext *aio_context;
2599 if (migration_is_blocked(errp)) {
2600 return ret;
2603 if (!replay_can_snapshot()) {
2604 error_setg(errp, "Record/replay does not allow making snapshot "
2605 "right now. Try once more later.");
2606 return ret;
2609 if (!bdrv_all_can_snapshot(&bs)) {
2610 error_setg(errp, "Device '%s' is writable but does not support "
2611 "snapshots", bdrv_get_device_name(bs));
2612 return ret;
2615 /* Delete old snapshots of the same name */
2616 if (name) {
2617 ret = bdrv_all_delete_snapshot(name, &bs1, errp);
2618 if (ret < 0) {
2619 error_prepend(errp, "Error while deleting snapshot on device "
2620 "'%s': ", bdrv_get_device_name(bs1));
2621 return ret;
2625 bs = bdrv_all_find_vmstate_bs();
2626 if (bs == NULL) {
2627 error_setg(errp, "No block device can accept snapshots");
2628 return ret;
2630 aio_context = bdrv_get_aio_context(bs);
2632 saved_vm_running = runstate_is_running();
2634 ret = global_state_store();
2635 if (ret) {
2636 error_setg(errp, "Error saving global state");
2637 return ret;
2639 vm_stop(RUN_STATE_SAVE_VM);
2641 bdrv_drain_all_begin();
2643 aio_context_acquire(aio_context);
2645 memset(sn, 0, sizeof(*sn));
2647 /* fill auxiliary fields */
2648 qemu_gettimeofday(&tv);
2649 sn->date_sec = tv.tv_sec;
2650 sn->date_nsec = tv.tv_usec * 1000;
2651 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
2653 if (name) {
2654 ret = bdrv_snapshot_find(bs, old_sn, name);
2655 if (ret >= 0) {
2656 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2657 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2658 } else {
2659 pstrcpy(sn->name, sizeof(sn->name), name);
2661 } else {
2662 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2663 localtime_r((const time_t *)&tv.tv_sec, &tm);
2664 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2667 /* save the VM state */
2668 f = qemu_fopen_bdrv(bs, 1);
2669 if (!f) {
2670 error_setg(errp, "Could not open VM state file");
2671 goto the_end;
2673 ret = qemu_savevm_state(f, errp);
2674 vm_state_size = qemu_ftell(f);
2675 qemu_fclose(f);
2676 if (ret < 0) {
2677 goto the_end;
2680 /* The bdrv_all_create_snapshot() call that follows acquires the AioContext
2681 * for itself. BDRV_POLL_WHILE() does not support nested locking because
2682 * it only releases the lock once. Therefore synchronous I/O will deadlock
2683 * unless we release the AioContext before bdrv_all_create_snapshot().
2685 aio_context_release(aio_context);
2686 aio_context = NULL;
2688 ret = bdrv_all_create_snapshot(sn, bs, vm_state_size, &bs);
2689 if (ret < 0) {
2690 error_setg(errp, "Error while creating snapshot on '%s'",
2691 bdrv_get_device_name(bs));
2692 goto the_end;
2695 ret = 0;
2697 the_end:
2698 if (aio_context) {
2699 aio_context_release(aio_context);
2702 bdrv_drain_all_end();
2704 if (saved_vm_running) {
2705 vm_start();
2707 return ret;
2710 void qmp_xen_save_devices_state(const char *filename, bool has_live, bool live,
2711 Error **errp)
2713 QEMUFile *f;
2714 QIOChannelFile *ioc;
2715 int saved_vm_running;
2716 int ret;
2718 if (!has_live) {
2719 /* live default to true so old version of Xen tool stack can have a
2720 * successfull live migration */
2721 live = true;
2724 saved_vm_running = runstate_is_running();
2725 vm_stop(RUN_STATE_SAVE_VM);
2726 global_state_store_running();
2728 ioc = qio_channel_file_new_path(filename, O_WRONLY | O_CREAT, 0660, errp);
2729 if (!ioc) {
2730 goto the_end;
2732 qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state");
2733 f = qemu_fopen_channel_output(QIO_CHANNEL(ioc));
2734 object_unref(OBJECT(ioc));
2735 ret = qemu_save_device_state(f);
2736 if (ret < 0 || qemu_fclose(f) < 0) {
2737 error_setg(errp, QERR_IO_ERROR);
2738 } else {
2739 /* libxl calls the QMP command "stop" before calling
2740 * "xen-save-devices-state" and in case of migration failure, libxl
2741 * would call "cont".
2742 * So call bdrv_inactivate_all (release locks) here to let the other
2743 * side of the migration take controle of the images.
2745 if (live && !saved_vm_running) {
2746 ret = bdrv_inactivate_all();
2747 if (ret) {
2748 error_setg(errp, "%s: bdrv_inactivate_all() failed (%d)",
2749 __func__, ret);
2754 the_end:
2755 if (saved_vm_running) {
2756 vm_start();
2760 void qmp_xen_load_devices_state(const char *filename, Error **errp)
2762 QEMUFile *f;
2763 QIOChannelFile *ioc;
2764 int ret;
2766 /* Guest must be paused before loading the device state; the RAM state
2767 * will already have been loaded by xc
2769 if (runstate_is_running()) {
2770 error_setg(errp, "Cannot update device state while vm is running");
2771 return;
2773 vm_stop(RUN_STATE_RESTORE_VM);
2775 ioc = qio_channel_file_new_path(filename, O_RDONLY | O_BINARY, 0, errp);
2776 if (!ioc) {
2777 return;
2779 qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state");
2780 f = qemu_fopen_channel_input(QIO_CHANNEL(ioc));
2781 object_unref(OBJECT(ioc));
2783 ret = qemu_loadvm_state(f);
2784 qemu_fclose(f);
2785 if (ret < 0) {
2786 error_setg(errp, QERR_IO_ERROR);
2788 migration_incoming_state_destroy();
2791 int load_snapshot(const char *name, Error **errp)
2793 BlockDriverState *bs, *bs_vm_state;
2794 QEMUSnapshotInfo sn;
2795 QEMUFile *f;
2796 int ret;
2797 AioContext *aio_context;
2798 MigrationIncomingState *mis = migration_incoming_get_current();
2800 if (!replay_can_snapshot()) {
2801 error_setg(errp, "Record/replay does not allow loading snapshot "
2802 "right now. Try once more later.");
2803 return -EINVAL;
2806 if (!bdrv_all_can_snapshot(&bs)) {
2807 error_setg(errp,
2808 "Device '%s' is writable but does not support snapshots",
2809 bdrv_get_device_name(bs));
2810 return -ENOTSUP;
2812 ret = bdrv_all_find_snapshot(name, &bs);
2813 if (ret < 0) {
2814 error_setg(errp,
2815 "Device '%s' does not have the requested snapshot '%s'",
2816 bdrv_get_device_name(bs), name);
2817 return ret;
2820 bs_vm_state = bdrv_all_find_vmstate_bs();
2821 if (!bs_vm_state) {
2822 error_setg(errp, "No block device supports snapshots");
2823 return -ENOTSUP;
2825 aio_context = bdrv_get_aio_context(bs_vm_state);
2827 /* Don't even try to load empty VM states */
2828 aio_context_acquire(aio_context);
2829 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2830 aio_context_release(aio_context);
2831 if (ret < 0) {
2832 return ret;
2833 } else if (sn.vm_state_size == 0) {
2834 error_setg(errp, "This is a disk-only snapshot. Revert to it "
2835 " offline using qemu-img");
2836 return -EINVAL;
2839 /* Flush all IO requests so they don't interfere with the new state. */
2840 bdrv_drain_all_begin();
2842 ret = bdrv_all_goto_snapshot(name, &bs, errp);
2843 if (ret < 0) {
2844 error_prepend(errp, "Could not load snapshot '%s' on '%s': ",
2845 name, bdrv_get_device_name(bs));
2846 goto err_drain;
2849 /* restore the VM state */
2850 f = qemu_fopen_bdrv(bs_vm_state, 0);
2851 if (!f) {
2852 error_setg(errp, "Could not open VM state file");
2853 ret = -EINVAL;
2854 goto err_drain;
2857 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
2858 mis->from_src_file = f;
2860 aio_context_acquire(aio_context);
2861 ret = qemu_loadvm_state(f);
2862 migration_incoming_state_destroy();
2863 aio_context_release(aio_context);
2865 bdrv_drain_all_end();
2867 if (ret < 0) {
2868 error_setg(errp, "Error %d while loading VM state", ret);
2869 return ret;
2872 return 0;
2874 err_drain:
2875 bdrv_drain_all_end();
2876 return ret;
2879 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2881 qemu_ram_set_idstr(mr->ram_block,
2882 memory_region_name(mr), dev);
2883 qemu_ram_set_migratable(mr->ram_block);
2886 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2888 qemu_ram_unset_idstr(mr->ram_block);
2889 qemu_ram_unset_migratable(mr->ram_block);
2892 void vmstate_register_ram_global(MemoryRegion *mr)
2894 vmstate_register_ram(mr, NULL);
2897 bool vmstate_check_only_migratable(const VMStateDescription *vmsd)
2899 /* check needed if --only-migratable is specified */
2900 if (!only_migratable) {
2901 return true;
2904 return !(vmsd && vmsd->unmigratable);