4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009-2015 Red Hat Inc
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
29 #include "qemu/osdep.h"
30 #include "hw/boards.h"
31 #include "hw/xen/xen.h"
33 #include "migration.h"
34 #include "migration/snapshot.h"
35 #include "migration/misc.h"
36 #include "migration/register.h"
37 #include "migration/global_state.h"
39 #include "qemu-file-channel.h"
40 #include "qemu-file.h"
42 #include "postcopy-ram.h"
43 #include "qapi/error.h"
44 #include "qapi/qapi-commands-migration.h"
45 #include "qapi/qapi-commands-misc.h"
46 #include "qapi/qmp/qerror.h"
47 #include "qemu/error-report.h"
48 #include "sysemu/cpus.h"
49 #include "exec/memory.h"
50 #include "exec/target_page.h"
53 #include "block/snapshot.h"
54 #include "qemu/cutils.h"
55 #include "io/channel-buffer.h"
56 #include "io/channel-file.h"
57 #include "sysemu/replay.h"
59 #include "migration/colo.h"
60 #include "qemu/bitmap.h"
61 #include "net/announce.h"
63 const unsigned int postcopy_ram_discard_version
= 0;
65 /* Subcommands for QEMU_VM_COMMAND */
67 MIG_CMD_INVALID
= 0, /* Must be 0 */
68 MIG_CMD_OPEN_RETURN_PATH
, /* Tell the dest to open the Return path */
69 MIG_CMD_PING
, /* Request a PONG on the RP */
71 MIG_CMD_POSTCOPY_ADVISE
, /* Prior to any page transfers, just
72 warn we might want to do PC */
73 MIG_CMD_POSTCOPY_LISTEN
, /* Start listening for incoming
74 pages as it's running. */
75 MIG_CMD_POSTCOPY_RUN
, /* Start execution */
77 MIG_CMD_POSTCOPY_RAM_DISCARD
, /* A list of pages to discard that
78 were previously sent during
79 precopy but are dirty. */
80 MIG_CMD_PACKAGED
, /* Send a wrapped stream within this stream */
81 MIG_CMD_ENABLE_COLO
, /* Enable COLO */
82 MIG_CMD_POSTCOPY_RESUME
, /* resume postcopy on dest */
83 MIG_CMD_RECV_BITMAP
, /* Request for recved bitmap on dst */
87 #define MAX_VM_CMD_PACKAGED_SIZE UINT32_MAX
88 static struct mig_cmd_args
{
89 ssize_t len
; /* -1 = variable */
92 [MIG_CMD_INVALID
] = { .len
= -1, .name
= "INVALID" },
93 [MIG_CMD_OPEN_RETURN_PATH
] = { .len
= 0, .name
= "OPEN_RETURN_PATH" },
94 [MIG_CMD_PING
] = { .len
= sizeof(uint32_t), .name
= "PING" },
95 [MIG_CMD_POSTCOPY_ADVISE
] = { .len
= -1, .name
= "POSTCOPY_ADVISE" },
96 [MIG_CMD_POSTCOPY_LISTEN
] = { .len
= 0, .name
= "POSTCOPY_LISTEN" },
97 [MIG_CMD_POSTCOPY_RUN
] = { .len
= 0, .name
= "POSTCOPY_RUN" },
98 [MIG_CMD_POSTCOPY_RAM_DISCARD
] = {
99 .len
= -1, .name
= "POSTCOPY_RAM_DISCARD" },
100 [MIG_CMD_POSTCOPY_RESUME
] = { .len
= 0, .name
= "POSTCOPY_RESUME" },
101 [MIG_CMD_PACKAGED
] = { .len
= 4, .name
= "PACKAGED" },
102 [MIG_CMD_RECV_BITMAP
] = { .len
= -1, .name
= "RECV_BITMAP" },
103 [MIG_CMD_MAX
] = { .len
= -1, .name
= "MAX" },
106 /* Note for MIG_CMD_POSTCOPY_ADVISE:
107 * The format of arguments is depending on postcopy mode:
108 * - postcopy RAM only
109 * uint64_t host page size
110 * uint64_t taget page size
112 * - postcopy RAM and postcopy dirty bitmaps
113 * format is the same as for postcopy RAM only
115 * - postcopy dirty bitmaps only
116 * Nothing. Command length field is 0.
118 * Be careful: adding a new postcopy entity with some other parameters should
119 * not break format self-description ability. Good way is to introduce some
120 * generic extendable format with an exception for two old entities.
123 /***********************************************************/
124 /* savevm/loadvm support */
126 static ssize_t
block_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
127 int64_t pos
, Error
**errp
)
132 qemu_iovec_init_external(&qiov
, iov
, iovcnt
);
133 ret
= bdrv_writev_vmstate(opaque
, &qiov
, pos
);
141 static ssize_t
block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
,
142 size_t size
, Error
**errp
)
144 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
147 static int bdrv_fclose(void *opaque
, Error
**errp
)
149 return bdrv_flush(opaque
);
152 static const QEMUFileOps bdrv_read_ops
= {
153 .get_buffer
= block_get_buffer
,
157 static const QEMUFileOps bdrv_write_ops
= {
158 .writev_buffer
= block_writev_buffer
,
162 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
165 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
167 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
171 /* QEMUFile timer support.
172 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
175 void timer_put(QEMUFile
*f
, QEMUTimer
*ts
)
177 uint64_t expire_time
;
179 expire_time
= timer_expire_time_ns(ts
);
180 qemu_put_be64(f
, expire_time
);
183 void timer_get(QEMUFile
*f
, QEMUTimer
*ts
)
185 uint64_t expire_time
;
187 expire_time
= qemu_get_be64(f
);
188 if (expire_time
!= -1) {
189 timer_mod_ns(ts
, expire_time
);
196 /* VMState timer support.
197 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
200 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
,
201 const VMStateField
*field
)
208 static int put_timer(QEMUFile
*f
, void *pv
, size_t size
,
209 const VMStateField
*field
, QJSON
*vmdesc
)
217 const VMStateInfo vmstate_info_timer
= {
224 typedef struct CompatEntry
{
229 typedef struct SaveStateEntry
{
230 QTAILQ_ENTRY(SaveStateEntry
) entry
;
235 /* version id read from the stream */
238 /* section id read from the stream */
240 const SaveVMHandlers
*ops
;
241 const VMStateDescription
*vmsd
;
247 typedef struct SaveState
{
248 QTAILQ_HEAD(, SaveStateEntry
) handlers
;
249 int global_section_id
;
252 uint32_t target_page_bits
;
254 MigrationCapability
*capabilities
;
257 static SaveState savevm_state
= {
258 .handlers
= QTAILQ_HEAD_INITIALIZER(savevm_state
.handlers
),
259 .global_section_id
= 0,
262 static bool should_validate_capability(int capability
)
264 assert(capability
>= 0 && capability
< MIGRATION_CAPABILITY__MAX
);
265 /* Validate only new capabilities to keep compatibility. */
266 switch (capability
) {
267 case MIGRATION_CAPABILITY_X_IGNORE_SHARED
:
274 static uint32_t get_validatable_capabilities_count(void)
276 MigrationState
*s
= migrate_get_current();
279 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
280 if (should_validate_capability(i
) && s
->enabled_capabilities
[i
]) {
287 static int configuration_pre_save(void *opaque
)
289 SaveState
*state
= opaque
;
290 const char *current_name
= MACHINE_GET_CLASS(current_machine
)->name
;
291 MigrationState
*s
= migrate_get_current();
294 state
->len
= strlen(current_name
);
295 state
->name
= current_name
;
296 state
->target_page_bits
= qemu_target_page_bits();
298 state
->caps_count
= get_validatable_capabilities_count();
299 state
->capabilities
= g_renew(MigrationCapability
, state
->capabilities
,
301 for (i
= j
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
302 if (should_validate_capability(i
) && s
->enabled_capabilities
[i
]) {
303 state
->capabilities
[j
++] = i
;
310 static int configuration_pre_load(void *opaque
)
312 SaveState
*state
= opaque
;
314 /* If there is no target-page-bits subsection it means the source
315 * predates the variable-target-page-bits support and is using the
316 * minimum possible value for this CPU.
318 state
->target_page_bits
= qemu_target_page_bits_min();
322 static bool configuration_validate_capabilities(SaveState
*state
)
325 MigrationState
*s
= migrate_get_current();
326 unsigned long *source_caps_bm
;
329 source_caps_bm
= bitmap_new(MIGRATION_CAPABILITY__MAX
);
330 for (i
= 0; i
< state
->caps_count
; i
++) {
331 MigrationCapability capability
= state
->capabilities
[i
];
332 set_bit(capability
, source_caps_bm
);
335 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
336 bool source_state
, target_state
;
337 if (!should_validate_capability(i
)) {
340 source_state
= test_bit(i
, source_caps_bm
);
341 target_state
= s
->enabled_capabilities
[i
];
342 if (source_state
!= target_state
) {
343 error_report("Capability %s is %s, but received capability is %s",
344 MigrationCapability_str(i
),
345 target_state
? "on" : "off",
346 source_state
? "on" : "off");
348 /* Don't break here to report all failed capabilities */
352 g_free(source_caps_bm
);
356 static int configuration_post_load(void *opaque
, int version_id
)
358 SaveState
*state
= opaque
;
359 const char *current_name
= MACHINE_GET_CLASS(current_machine
)->name
;
361 if (strncmp(state
->name
, current_name
, state
->len
) != 0) {
362 error_report("Machine type received is '%.*s' and local is '%s'",
363 (int) state
->len
, state
->name
, current_name
);
367 if (state
->target_page_bits
!= qemu_target_page_bits()) {
368 error_report("Received TARGET_PAGE_BITS is %d but local is %d",
369 state
->target_page_bits
, qemu_target_page_bits());
373 if (!configuration_validate_capabilities(state
)) {
380 static int get_capability(QEMUFile
*f
, void *pv
, size_t size
,
381 const VMStateField
*field
)
383 MigrationCapability
*capability
= pv
;
384 char capability_str
[UINT8_MAX
+ 1];
388 len
= qemu_get_byte(f
);
389 qemu_get_buffer(f
, (uint8_t *)capability_str
, len
);
390 capability_str
[len
] = '\0';
391 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
392 if (!strcmp(MigrationCapability_str(i
), capability_str
)) {
397 error_report("Received unknown capability %s", capability_str
);
401 static int put_capability(QEMUFile
*f
, void *pv
, size_t size
,
402 const VMStateField
*field
, QJSON
*vmdesc
)
404 MigrationCapability
*capability
= pv
;
405 const char *capability_str
= MigrationCapability_str(*capability
);
406 size_t len
= strlen(capability_str
);
407 assert(len
<= UINT8_MAX
);
409 qemu_put_byte(f
, len
);
410 qemu_put_buffer(f
, (uint8_t *)capability_str
, len
);
414 static const VMStateInfo vmstate_info_capability
= {
415 .name
= "capability",
416 .get
= get_capability
,
417 .put
= put_capability
,
420 /* The target-page-bits subsection is present only if the
421 * target page size is not the same as the default (ie the
422 * minimum page size for a variable-page-size guest CPU).
423 * If it is present then it contains the actual target page
424 * bits for the machine, and migration will fail if the
425 * two ends don't agree about it.
427 static bool vmstate_target_page_bits_needed(void *opaque
)
429 return qemu_target_page_bits()
430 > qemu_target_page_bits_min();
433 static const VMStateDescription vmstate_target_page_bits
= {
434 .name
= "configuration/target-page-bits",
436 .minimum_version_id
= 1,
437 .needed
= vmstate_target_page_bits_needed
,
438 .fields
= (VMStateField
[]) {
439 VMSTATE_UINT32(target_page_bits
, SaveState
),
440 VMSTATE_END_OF_LIST()
444 static bool vmstate_capabilites_needed(void *opaque
)
446 return get_validatable_capabilities_count() > 0;
449 static const VMStateDescription vmstate_capabilites
= {
450 .name
= "configuration/capabilities",
452 .minimum_version_id
= 1,
453 .needed
= vmstate_capabilites_needed
,
454 .fields
= (VMStateField
[]) {
455 VMSTATE_UINT32_V(caps_count
, SaveState
, 1),
456 VMSTATE_VARRAY_UINT32_ALLOC(capabilities
, SaveState
, caps_count
, 1,
457 vmstate_info_capability
,
458 MigrationCapability
),
459 VMSTATE_END_OF_LIST()
463 static const VMStateDescription vmstate_configuration
= {
464 .name
= "configuration",
466 .pre_load
= configuration_pre_load
,
467 .post_load
= configuration_post_load
,
468 .pre_save
= configuration_pre_save
,
469 .fields
= (VMStateField
[]) {
470 VMSTATE_UINT32(len
, SaveState
),
471 VMSTATE_VBUFFER_ALLOC_UINT32(name
, SaveState
, 0, NULL
, len
),
472 VMSTATE_END_OF_LIST()
474 .subsections
= (const VMStateDescription
*[]) {
475 &vmstate_target_page_bits
,
476 &vmstate_capabilites
,
481 static void dump_vmstate_vmsd(FILE *out_file
,
482 const VMStateDescription
*vmsd
, int indent
,
485 static void dump_vmstate_vmsf(FILE *out_file
, const VMStateField
*field
,
488 fprintf(out_file
, "%*s{\n", indent
, "");
490 fprintf(out_file
, "%*s\"field\": \"%s\",\n", indent
, "", field
->name
);
491 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
493 fprintf(out_file
, "%*s\"field_exists\": %s,\n", indent
, "",
494 field
->field_exists
? "true" : "false");
495 fprintf(out_file
, "%*s\"size\": %zu", indent
, "", field
->size
);
496 if (field
->vmsd
!= NULL
) {
497 fprintf(out_file
, ",\n");
498 dump_vmstate_vmsd(out_file
, field
->vmsd
, indent
, false);
500 fprintf(out_file
, "\n%*s}", indent
- 2, "");
503 static void dump_vmstate_vmss(FILE *out_file
,
504 const VMStateDescription
**subsection
,
507 if (*subsection
!= NULL
) {
508 dump_vmstate_vmsd(out_file
, *subsection
, indent
, true);
512 static void dump_vmstate_vmsd(FILE *out_file
,
513 const VMStateDescription
*vmsd
, int indent
,
517 fprintf(out_file
, "%*s{\n", indent
, "");
519 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", "Description");
522 fprintf(out_file
, "%*s\"name\": \"%s\",\n", indent
, "", vmsd
->name
);
523 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
525 fprintf(out_file
, "%*s\"minimum_version_id\": %d", indent
, "",
526 vmsd
->minimum_version_id
);
527 if (vmsd
->fields
!= NULL
) {
528 const VMStateField
*field
= vmsd
->fields
;
531 fprintf(out_file
, ",\n%*s\"Fields\": [\n", indent
, "");
533 while (field
->name
!= NULL
) {
534 if (field
->flags
& VMS_MUST_EXIST
) {
535 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
540 fprintf(out_file
, ",\n");
542 dump_vmstate_vmsf(out_file
, field
, indent
+ 2);
546 fprintf(out_file
, "\n%*s]", indent
, "");
548 if (vmsd
->subsections
!= NULL
) {
549 const VMStateDescription
**subsection
= vmsd
->subsections
;
552 fprintf(out_file
, ",\n%*s\"Subsections\": [\n", indent
, "");
554 while (*subsection
!= NULL
) {
556 fprintf(out_file
, ",\n");
558 dump_vmstate_vmss(out_file
, subsection
, indent
+ 2);
562 fprintf(out_file
, "\n%*s]", indent
, "");
564 fprintf(out_file
, "\n%*s}", indent
- 2, "");
567 static void dump_machine_type(FILE *out_file
)
571 mc
= MACHINE_GET_CLASS(current_machine
);
573 fprintf(out_file
, " \"vmschkmachine\": {\n");
574 fprintf(out_file
, " \"Name\": \"%s\"\n", mc
->name
);
575 fprintf(out_file
, " },\n");
578 void dump_vmstate_json_to_file(FILE *out_file
)
583 fprintf(out_file
, "{\n");
584 dump_machine_type(out_file
);
587 list
= object_class_get_list(TYPE_DEVICE
, true);
588 for (elt
= list
; elt
; elt
= elt
->next
) {
589 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
599 fprintf(out_file
, ",\n");
601 name
= object_class_get_name(OBJECT_CLASS(dc
));
602 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", name
);
604 fprintf(out_file
, "%*s\"Name\": \"%s\",\n", indent
, "", name
);
605 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
606 dc
->vmsd
->version_id
);
607 fprintf(out_file
, "%*s\"minimum_version_id\": %d,\n", indent
, "",
608 dc
->vmsd
->minimum_version_id
);
610 dump_vmstate_vmsd(out_file
, dc
->vmsd
, indent
, false);
612 fprintf(out_file
, "\n%*s}", indent
- 2, "");
615 fprintf(out_file
, "\n}\n");
619 static int calculate_new_instance_id(const char *idstr
)
624 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
625 if (strcmp(idstr
, se
->idstr
) == 0
626 && instance_id
<= se
->instance_id
) {
627 instance_id
= se
->instance_id
+ 1;
633 static int calculate_compat_instance_id(const char *idstr
)
638 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
643 if (strcmp(idstr
, se
->compat
->idstr
) == 0
644 && instance_id
<= se
->compat
->instance_id
) {
645 instance_id
= se
->compat
->instance_id
+ 1;
651 static inline MigrationPriority
save_state_priority(SaveStateEntry
*se
)
654 return se
->vmsd
->priority
;
656 return MIG_PRI_DEFAULT
;
659 static void savevm_state_handler_insert(SaveStateEntry
*nse
)
661 MigrationPriority priority
= save_state_priority(nse
);
664 assert(priority
<= MIG_PRI_MAX
);
666 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
667 if (save_state_priority(se
) < priority
) {
673 QTAILQ_INSERT_BEFORE(se
, nse
, entry
);
675 QTAILQ_INSERT_TAIL(&savevm_state
.handlers
, nse
, entry
);
679 /* TODO: Individual devices generally have very little idea about the rest
680 of the system, so instance_id should be removed/replaced.
681 Meanwhile pass -1 as instance_id if you do not already have a clearly
682 distinguishing id for all instances of your device class. */
683 int register_savevm_live(DeviceState
*dev
,
687 const SaveVMHandlers
*ops
,
692 se
= g_new0(SaveStateEntry
, 1);
693 se
->version_id
= version_id
;
694 se
->section_id
= savevm_state
.global_section_id
++;
698 /* if this is a live_savem then set is_ram */
699 if (ops
->save_setup
!= NULL
) {
704 char *id
= qdev_get_dev_path(dev
);
706 if (snprintf(se
->idstr
, sizeof(se
->idstr
), "%s/", id
) >=
708 error_report("Path too long for VMState (%s)", id
);
716 se
->compat
= g_new0(CompatEntry
, 1);
717 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
718 se
->compat
->instance_id
= instance_id
== -1 ?
719 calculate_compat_instance_id(idstr
) : instance_id
;
723 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
725 if (instance_id
== -1) {
726 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
728 se
->instance_id
= instance_id
;
730 assert(!se
->compat
|| se
->instance_id
== 0);
731 savevm_state_handler_insert(se
);
735 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
737 SaveStateEntry
*se
, *new_se
;
741 char *path
= qdev_get_dev_path(dev
);
743 pstrcpy(id
, sizeof(id
), path
);
744 pstrcat(id
, sizeof(id
), "/");
748 pstrcat(id
, sizeof(id
), idstr
);
750 QTAILQ_FOREACH_SAFE(se
, &savevm_state
.handlers
, entry
, new_se
) {
751 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
752 QTAILQ_REMOVE(&savevm_state
.handlers
, se
, entry
);
759 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
760 const VMStateDescription
*vmsd
,
761 void *opaque
, int alias_id
,
762 int required_for_version
,
767 /* If this triggers, alias support can be dropped for the vmsd. */
768 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
770 se
= g_new0(SaveStateEntry
, 1);
771 se
->version_id
= vmsd
->version_id
;
772 se
->section_id
= savevm_state
.global_section_id
++;
775 se
->alias_id
= alias_id
;
778 char *id
= qdev_get_dev_path(dev
);
780 if (snprintf(se
->idstr
, sizeof(se
->idstr
), "%s/", id
) >=
782 error_setg(errp
, "Path too long for VMState (%s)", id
);
790 se
->compat
= g_new0(CompatEntry
, 1);
791 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
792 se
->compat
->instance_id
= instance_id
== -1 ?
793 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
797 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
799 if (instance_id
== -1) {
800 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
802 se
->instance_id
= instance_id
;
804 assert(!se
->compat
|| se
->instance_id
== 0);
805 savevm_state_handler_insert(se
);
809 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
812 SaveStateEntry
*se
, *new_se
;
814 QTAILQ_FOREACH_SAFE(se
, &savevm_state
.handlers
, entry
, new_se
) {
815 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
816 QTAILQ_REMOVE(&savevm_state
.handlers
, se
, entry
);
823 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
)
825 trace_vmstate_load(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
826 if (!se
->vmsd
) { /* Old style */
827 return se
->ops
->load_state(f
, se
->opaque
, se
->load_version_id
);
829 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, se
->load_version_id
);
832 static void vmstate_save_old_style(QEMUFile
*f
, SaveStateEntry
*se
, QJSON
*vmdesc
)
834 int64_t old_offset
, size
;
836 old_offset
= qemu_ftell_fast(f
);
837 se
->ops
->save_state(f
, se
->opaque
);
838 size
= qemu_ftell_fast(f
) - old_offset
;
841 json_prop_int(vmdesc
, "size", size
);
842 json_start_array(vmdesc
, "fields");
843 json_start_object(vmdesc
, NULL
);
844 json_prop_str(vmdesc
, "name", "data");
845 json_prop_int(vmdesc
, "size", size
);
846 json_prop_str(vmdesc
, "type", "buffer");
847 json_end_object(vmdesc
);
848 json_end_array(vmdesc
);
852 static int vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
, QJSON
*vmdesc
)
854 trace_vmstate_save(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
856 vmstate_save_old_style(f
, se
, vmdesc
);
859 return vmstate_save_state(f
, se
->vmsd
, se
->opaque
, vmdesc
);
863 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
865 static void save_section_header(QEMUFile
*f
, SaveStateEntry
*se
,
866 uint8_t section_type
)
868 qemu_put_byte(f
, section_type
);
869 qemu_put_be32(f
, se
->section_id
);
871 if (section_type
== QEMU_VM_SECTION_FULL
||
872 section_type
== QEMU_VM_SECTION_START
) {
874 size_t len
= strlen(se
->idstr
);
875 qemu_put_byte(f
, len
);
876 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
878 qemu_put_be32(f
, se
->instance_id
);
879 qemu_put_be32(f
, se
->version_id
);
884 * Write a footer onto device sections that catches cases misformatted device
887 static void save_section_footer(QEMUFile
*f
, SaveStateEntry
*se
)
889 if (migrate_get_current()->send_section_footer
) {
890 qemu_put_byte(f
, QEMU_VM_SECTION_FOOTER
);
891 qemu_put_be32(f
, se
->section_id
);
896 * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the
897 * command and associated data.
899 * @f: File to send command on
900 * @command: Command type to send
901 * @len: Length of associated data
902 * @data: Data associated with command.
904 static void qemu_savevm_command_send(QEMUFile
*f
,
905 enum qemu_vm_cmd command
,
909 trace_savevm_command_send(command
, len
);
910 qemu_put_byte(f
, QEMU_VM_COMMAND
);
911 qemu_put_be16(f
, (uint16_t)command
);
912 qemu_put_be16(f
, len
);
913 qemu_put_buffer(f
, data
, len
);
917 void qemu_savevm_send_colo_enable(QEMUFile
*f
)
919 trace_savevm_send_colo_enable();
920 qemu_savevm_command_send(f
, MIG_CMD_ENABLE_COLO
, 0, NULL
);
923 void qemu_savevm_send_ping(QEMUFile
*f
, uint32_t value
)
927 trace_savevm_send_ping(value
);
928 buf
= cpu_to_be32(value
);
929 qemu_savevm_command_send(f
, MIG_CMD_PING
, sizeof(value
), (uint8_t *)&buf
);
932 void qemu_savevm_send_open_return_path(QEMUFile
*f
)
934 trace_savevm_send_open_return_path();
935 qemu_savevm_command_send(f
, MIG_CMD_OPEN_RETURN_PATH
, 0, NULL
);
938 /* We have a buffer of data to send; we don't want that all to be loaded
939 * by the command itself, so the command contains just the length of the
940 * extra buffer that we then send straight after it.
941 * TODO: Must be a better way to organise that
947 int qemu_savevm_send_packaged(QEMUFile
*f
, const uint8_t *buf
, size_t len
)
951 if (len
> MAX_VM_CMD_PACKAGED_SIZE
) {
952 error_report("%s: Unreasonably large packaged state: %zu",
957 tmp
= cpu_to_be32(len
);
959 trace_qemu_savevm_send_packaged();
960 qemu_savevm_command_send(f
, MIG_CMD_PACKAGED
, 4, (uint8_t *)&tmp
);
962 qemu_put_buffer(f
, buf
, len
);
967 /* Send prior to any postcopy transfer */
968 void qemu_savevm_send_postcopy_advise(QEMUFile
*f
)
970 if (migrate_postcopy_ram()) {
972 tmp
[0] = cpu_to_be64(ram_pagesize_summary());
973 tmp
[1] = cpu_to_be64(qemu_target_page_size());
975 trace_qemu_savevm_send_postcopy_advise();
976 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_ADVISE
,
979 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_ADVISE
, 0, NULL
);
983 /* Sent prior to starting the destination running in postcopy, discard pages
984 * that have already been sent but redirtied on the source.
985 * CMD_POSTCOPY_RAM_DISCARD consist of:
987 * byte Length of name field (not including 0)
988 * n x byte RAM block name
989 * byte 0 terminator (just for safety)
990 * n x Byte ranges within the named RAMBlock
991 * be64 Start of the range
994 * name: RAMBlock name that these entries are part of
995 * len: Number of page entries
996 * start_list: 'len' addresses
997 * length_list: 'len' addresses
1000 void qemu_savevm_send_postcopy_ram_discard(QEMUFile
*f
, const char *name
,
1002 uint64_t *start_list
,
1003 uint64_t *length_list
)
1008 size_t name_len
= strlen(name
);
1010 trace_qemu_savevm_send_postcopy_ram_discard(name
, len
);
1011 assert(name_len
< 256);
1012 buf
= g_malloc0(1 + 1 + name_len
+ 1 + (8 + 8) * len
);
1013 buf
[0] = postcopy_ram_discard_version
;
1015 memcpy(buf
+ 2, name
, name_len
);
1016 tmplen
= 2 + name_len
;
1017 buf
[tmplen
++] = '\0';
1019 for (t
= 0; t
< len
; t
++) {
1020 stq_be_p(buf
+ tmplen
, start_list
[t
]);
1022 stq_be_p(buf
+ tmplen
, length_list
[t
]);
1025 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_RAM_DISCARD
, tmplen
, buf
);
1029 /* Get the destination into a state where it can receive postcopy data. */
1030 void qemu_savevm_send_postcopy_listen(QEMUFile
*f
)
1032 trace_savevm_send_postcopy_listen();
1033 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_LISTEN
, 0, NULL
);
1036 /* Kick the destination into running */
1037 void qemu_savevm_send_postcopy_run(QEMUFile
*f
)
1039 trace_savevm_send_postcopy_run();
1040 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_RUN
, 0, NULL
);
1043 void qemu_savevm_send_postcopy_resume(QEMUFile
*f
)
1045 trace_savevm_send_postcopy_resume();
1046 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_RESUME
, 0, NULL
);
1049 void qemu_savevm_send_recv_bitmap(QEMUFile
*f
, char *block_name
)
1054 trace_savevm_send_recv_bitmap(block_name
);
1056 buf
[0] = len
= strlen(block_name
);
1057 memcpy(buf
+ 1, block_name
, len
);
1059 qemu_savevm_command_send(f
, MIG_CMD_RECV_BITMAP
, len
+ 1, (uint8_t *)buf
);
1062 bool qemu_savevm_state_blocked(Error
**errp
)
1066 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1067 if (se
->vmsd
&& se
->vmsd
->unmigratable
) {
1068 error_setg(errp
, "State blocked by non-migratable device '%s'",
1076 void qemu_savevm_state_header(QEMUFile
*f
)
1078 trace_savevm_state_header();
1079 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1080 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1082 if (migrate_get_current()->send_configuration
) {
1083 qemu_put_byte(f
, QEMU_VM_CONFIGURATION
);
1084 vmstate_save_state(f
, &vmstate_configuration
, &savevm_state
, 0);
1088 void qemu_savevm_state_setup(QEMUFile
*f
)
1091 Error
*local_err
= NULL
;
1094 trace_savevm_state_setup();
1095 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1096 if (!se
->ops
|| !se
->ops
->save_setup
) {
1099 if (se
->ops
&& se
->ops
->is_active
) {
1100 if (!se
->ops
->is_active(se
->opaque
)) {
1104 save_section_header(f
, se
, QEMU_VM_SECTION_START
);
1106 ret
= se
->ops
->save_setup(f
, se
->opaque
);
1107 save_section_footer(f
, se
);
1109 qemu_file_set_error(f
, ret
);
1114 if (precopy_notify(PRECOPY_NOTIFY_SETUP
, &local_err
)) {
1115 error_report_err(local_err
);
1119 int qemu_savevm_state_resume_prepare(MigrationState
*s
)
1124 trace_savevm_state_resume_prepare();
1126 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1127 if (!se
->ops
|| !se
->ops
->resume_prepare
) {
1130 if (se
->ops
&& se
->ops
->is_active
) {
1131 if (!se
->ops
->is_active(se
->opaque
)) {
1135 ret
= se
->ops
->resume_prepare(s
, se
->opaque
);
1145 * this function has three return values:
1146 * negative: there was one error, and we have -errno.
1147 * 0 : We haven't finished, caller have to go again
1148 * 1 : We have finished, we can go to complete phase
1150 int qemu_savevm_state_iterate(QEMUFile
*f
, bool postcopy
)
1155 trace_savevm_state_iterate();
1156 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1157 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
1160 if (se
->ops
->is_active
&&
1161 !se
->ops
->is_active(se
->opaque
)) {
1164 if (se
->ops
->is_active_iterate
&&
1165 !se
->ops
->is_active_iterate(se
->opaque
)) {
1169 * In the postcopy phase, any device that doesn't know how to
1170 * do postcopy should have saved it's state in the _complete
1171 * call that's already run, it might get confused if we call
1172 * iterate afterwards.
1175 !(se
->ops
->has_postcopy
&& se
->ops
->has_postcopy(se
->opaque
))) {
1178 if (qemu_file_rate_limit(f
)) {
1181 trace_savevm_section_start(se
->idstr
, se
->section_id
);
1183 save_section_header(f
, se
, QEMU_VM_SECTION_PART
);
1185 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
1186 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
1187 save_section_footer(f
, se
);
1190 qemu_file_set_error(f
, ret
);
1193 /* Do not proceed to the next vmstate before this one reported
1194 completion of the current stage. This serializes the migration
1195 and reduces the probability that a faster changing state is
1196 synchronized over and over again. */
1203 static bool should_send_vmdesc(void)
1205 MachineState
*machine
= MACHINE(qdev_get_machine());
1206 bool in_postcopy
= migration_in_postcopy();
1207 return !machine
->suppress_vmdesc
&& !in_postcopy
;
1211 * Calls the save_live_complete_postcopy methods
1212 * causing the last few pages to be sent immediately and doing any associated
1214 * Note postcopy also calls qemu_savevm_state_complete_precopy to complete
1215 * all the other devices, but that happens at the point we switch to postcopy.
1217 void qemu_savevm_state_complete_postcopy(QEMUFile
*f
)
1222 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1223 if (!se
->ops
|| !se
->ops
->save_live_complete_postcopy
) {
1226 if (se
->ops
&& se
->ops
->is_active
) {
1227 if (!se
->ops
->is_active(se
->opaque
)) {
1231 trace_savevm_section_start(se
->idstr
, se
->section_id
);
1233 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1234 qemu_put_be32(f
, se
->section_id
);
1236 ret
= se
->ops
->save_live_complete_postcopy(f
, se
->opaque
);
1237 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
1238 save_section_footer(f
, se
);
1240 qemu_file_set_error(f
, ret
);
1245 qemu_put_byte(f
, QEMU_VM_EOF
);
1250 int qemu_savevm_state_complete_precopy_iterable(QEMUFile
*f
, bool in_postcopy
,
1256 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1258 (in_postcopy
&& se
->ops
->has_postcopy
&&
1259 se
->ops
->has_postcopy(se
->opaque
)) ||
1260 (in_postcopy
&& !iterable_only
) ||
1261 !se
->ops
->save_live_complete_precopy
) {
1265 if (se
->ops
&& se
->ops
->is_active
) {
1266 if (!se
->ops
->is_active(se
->opaque
)) {
1270 trace_savevm_section_start(se
->idstr
, se
->section_id
);
1272 save_section_header(f
, se
, QEMU_VM_SECTION_END
);
1274 ret
= se
->ops
->save_live_complete_precopy(f
, se
->opaque
);
1275 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
1276 save_section_footer(f
, se
);
1278 qemu_file_set_error(f
, ret
);
1287 int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile
*f
,
1289 bool inactivate_disks
)
1296 vmdesc
= qjson_new();
1297 json_prop_int(vmdesc
, "page_size", qemu_target_page_size());
1298 json_start_array(vmdesc
, "devices");
1299 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1301 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1304 if (se
->vmsd
&& !vmstate_save_needed(se
->vmsd
, se
->opaque
)) {
1305 trace_savevm_section_skip(se
->idstr
, se
->section_id
);
1309 trace_savevm_section_start(se
->idstr
, se
->section_id
);
1311 json_start_object(vmdesc
, NULL
);
1312 json_prop_str(vmdesc
, "name", se
->idstr
);
1313 json_prop_int(vmdesc
, "instance_id", se
->instance_id
);
1315 save_section_header(f
, se
, QEMU_VM_SECTION_FULL
);
1316 ret
= vmstate_save(f
, se
, vmdesc
);
1318 qemu_file_set_error(f
, ret
);
1321 trace_savevm_section_end(se
->idstr
, se
->section_id
, 0);
1322 save_section_footer(f
, se
);
1324 json_end_object(vmdesc
);
1327 if (inactivate_disks
) {
1328 /* Inactivate before sending QEMU_VM_EOF so that the
1329 * bdrv_invalidate_cache_all() on the other end won't fail. */
1330 ret
= bdrv_inactivate_all();
1332 error_report("%s: bdrv_inactivate_all() failed (%d)",
1334 qemu_file_set_error(f
, ret
);
1339 /* Postcopy stream will still be going */
1340 qemu_put_byte(f
, QEMU_VM_EOF
);
1343 json_end_array(vmdesc
);
1344 qjson_finish(vmdesc
);
1345 vmdesc_len
= strlen(qjson_get_str(vmdesc
));
1347 if (should_send_vmdesc()) {
1348 qemu_put_byte(f
, QEMU_VM_VMDESCRIPTION
);
1349 qemu_put_be32(f
, vmdesc_len
);
1350 qemu_put_buffer(f
, (uint8_t *)qjson_get_str(vmdesc
), vmdesc_len
);
1352 qjson_destroy(vmdesc
);
1357 int qemu_savevm_state_complete_precopy(QEMUFile
*f
, bool iterable_only
,
1358 bool inactivate_disks
)
1361 Error
*local_err
= NULL
;
1362 bool in_postcopy
= migration_in_postcopy();
1364 if (precopy_notify(PRECOPY_NOTIFY_COMPLETE
, &local_err
)) {
1365 error_report_err(local_err
);
1368 trace_savevm_state_complete_precopy();
1370 cpu_synchronize_all_states();
1372 ret
= qemu_savevm_state_complete_precopy_iterable(f
, in_postcopy
,
1378 if (iterable_only
) {
1382 ret
= qemu_savevm_state_complete_precopy_non_iterable(f
, in_postcopy
,
1393 /* Give an estimate of the amount left to be transferred,
1394 * the result is split into the amount for units that can and
1395 * for units that can't do postcopy.
1397 void qemu_savevm_state_pending(QEMUFile
*f
, uint64_t threshold_size
,
1398 uint64_t *res_precopy_only
,
1399 uint64_t *res_compatible
,
1400 uint64_t *res_postcopy_only
)
1404 *res_precopy_only
= 0;
1405 *res_compatible
= 0;
1406 *res_postcopy_only
= 0;
1409 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1410 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
1413 if (se
->ops
&& se
->ops
->is_active
) {
1414 if (!se
->ops
->is_active(se
->opaque
)) {
1418 se
->ops
->save_live_pending(f
, se
->opaque
, threshold_size
,
1419 res_precopy_only
, res_compatible
,
1424 void qemu_savevm_state_cleanup(void)
1427 Error
*local_err
= NULL
;
1429 if (precopy_notify(PRECOPY_NOTIFY_CLEANUP
, &local_err
)) {
1430 error_report_err(local_err
);
1433 trace_savevm_state_cleanup();
1434 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1435 if (se
->ops
&& se
->ops
->save_cleanup
) {
1436 se
->ops
->save_cleanup(se
->opaque
);
1441 static int qemu_savevm_state(QEMUFile
*f
, Error
**errp
)
1444 MigrationState
*ms
= migrate_get_current();
1445 MigrationStatus status
;
1447 if (migration_is_setup_or_active(ms
->state
) ||
1448 ms
->state
== MIGRATION_STATUS_CANCELLING
||
1449 ms
->state
== MIGRATION_STATUS_COLO
) {
1450 error_setg(errp
, QERR_MIGRATION_ACTIVE
);
1454 if (migrate_use_block()) {
1455 error_setg(errp
, "Block migration and snapshots are incompatible");
1460 ms
->to_dst_file
= f
;
1462 qemu_mutex_unlock_iothread();
1463 qemu_savevm_state_header(f
);
1464 qemu_savevm_state_setup(f
);
1465 qemu_mutex_lock_iothread();
1467 while (qemu_file_get_error(f
) == 0) {
1468 if (qemu_savevm_state_iterate(f
, false) > 0) {
1473 ret
= qemu_file_get_error(f
);
1475 qemu_savevm_state_complete_precopy(f
, false, false);
1476 ret
= qemu_file_get_error(f
);
1478 qemu_savevm_state_cleanup();
1480 error_setg_errno(errp
, -ret
, "Error while writing VM state");
1484 status
= MIGRATION_STATUS_FAILED
;
1486 status
= MIGRATION_STATUS_COMPLETED
;
1488 migrate_set_state(&ms
->state
, MIGRATION_STATUS_SETUP
, status
);
1490 /* f is outer parameter, it should not stay in global migration state after
1491 * this function finished */
1492 ms
->to_dst_file
= NULL
;
1497 void qemu_savevm_live_state(QEMUFile
*f
)
1499 /* save QEMU_VM_SECTION_END section */
1500 qemu_savevm_state_complete_precopy(f
, true, false);
1501 qemu_put_byte(f
, QEMU_VM_EOF
);
1504 int qemu_save_device_state(QEMUFile
*f
)
1508 if (!migration_in_colo_state()) {
1509 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1510 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1512 cpu_synchronize_all_states();
1514 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1520 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1523 if (se
->vmsd
&& !vmstate_save_needed(se
->vmsd
, se
->opaque
)) {
1527 save_section_header(f
, se
, QEMU_VM_SECTION_FULL
);
1529 ret
= vmstate_save(f
, se
, NULL
);
1534 save_section_footer(f
, se
);
1537 qemu_put_byte(f
, QEMU_VM_EOF
);
1539 return qemu_file_get_error(f
);
1542 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1546 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1547 if (!strcmp(se
->idstr
, idstr
) &&
1548 (instance_id
== se
->instance_id
||
1549 instance_id
== se
->alias_id
))
1551 /* Migrating from an older version? */
1552 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
1553 if (!strcmp(se
->compat
->idstr
, idstr
) &&
1554 (instance_id
== se
->compat
->instance_id
||
1555 instance_id
== se
->alias_id
))
1562 enum LoadVMExitCodes
{
1563 /* Allow a command to quit all layers of nested loadvm loops */
1567 /* ------ incoming postcopy messages ------ */
1568 /* 'advise' arrives before any transfers just to tell us that a postcopy
1569 * *might* happen - it might be skipped if precopy transferred everything
1572 static int loadvm_postcopy_handle_advise(MigrationIncomingState
*mis
,
1575 PostcopyState ps
= postcopy_state_set(POSTCOPY_INCOMING_ADVISE
);
1576 uint64_t remote_pagesize_summary
, local_pagesize_summary
, remote_tps
;
1577 Error
*local_err
= NULL
;
1579 trace_loadvm_postcopy_handle_advise();
1580 if (ps
!= POSTCOPY_INCOMING_NONE
) {
1581 error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps
);
1587 if (migrate_postcopy_ram()) {
1588 error_report("RAM postcopy is enabled but have 0 byte advise");
1593 if (!migrate_postcopy_ram()) {
1594 error_report("RAM postcopy is disabled but have 16 byte advise");
1599 error_report("CMD_POSTCOPY_ADVISE invalid length (%d)", len
);
1603 if (!postcopy_ram_supported_by_host(mis
)) {
1604 postcopy_state_set(POSTCOPY_INCOMING_NONE
);
1608 remote_pagesize_summary
= qemu_get_be64(mis
->from_src_file
);
1609 local_pagesize_summary
= ram_pagesize_summary();
1611 if (remote_pagesize_summary
!= local_pagesize_summary
) {
1613 * This detects two potential causes of mismatch:
1614 * a) A mismatch in host page sizes
1615 * Some combinations of mismatch are probably possible but it gets
1616 * a bit more complicated. In particular we need to place whole
1617 * host pages on the dest at once, and we need to ensure that we
1618 * handle dirtying to make sure we never end up sending part of
1619 * a hostpage on it's own.
1620 * b) The use of different huge page sizes on source/destination
1621 * a more fine grain test is performed during RAM block migration
1622 * but this test here causes a nice early clear failure, and
1623 * also fails when passed to an older qemu that doesn't
1626 error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64
1628 remote_pagesize_summary
, local_pagesize_summary
);
1632 remote_tps
= qemu_get_be64(mis
->from_src_file
);
1633 if (remote_tps
!= qemu_target_page_size()) {
1635 * Again, some differences could be dealt with, but for now keep it
1638 error_report("Postcopy needs matching target page sizes (s=%d d=%zd)",
1639 (int)remote_tps
, qemu_target_page_size());
1643 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_ADVISE
, &local_err
)) {
1644 error_report_err(local_err
);
1648 if (ram_postcopy_incoming_init(mis
)) {
1652 postcopy_state_set(POSTCOPY_INCOMING_ADVISE
);
1657 /* After postcopy we will be told to throw some pages away since they're
1658 * dirty and will have to be demand fetched. Must happen before CPU is
1660 * There can be 0..many of these messages, each encoding multiple pages.
1662 static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState
*mis
,
1667 PostcopyState ps
= postcopy_state_get();
1669 trace_loadvm_postcopy_ram_handle_discard();
1672 case POSTCOPY_INCOMING_ADVISE
:
1674 tmp
= postcopy_ram_prepare_discard(mis
);
1680 case POSTCOPY_INCOMING_DISCARD
:
1681 /* Expected state */
1685 error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)",
1689 /* We're expecting a
1691 * a RAM ID string (length byte, name, 0 term)
1692 * then at least 1 16 byte chunk
1694 if (len
< (1 + 1 + 1 + 1 + 2 * 8)) {
1695 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len
);
1699 tmp
= qemu_get_byte(mis
->from_src_file
);
1700 if (tmp
!= postcopy_ram_discard_version
) {
1701 error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp
);
1705 if (!qemu_get_counted_string(mis
->from_src_file
, ramid
)) {
1706 error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID");
1709 tmp
= qemu_get_byte(mis
->from_src_file
);
1711 error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp
);
1715 len
-= 3 + strlen(ramid
);
1717 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len
);
1720 trace_loadvm_postcopy_ram_handle_discard_header(ramid
, len
);
1722 uint64_t start_addr
, block_length
;
1723 start_addr
= qemu_get_be64(mis
->from_src_file
);
1724 block_length
= qemu_get_be64(mis
->from_src_file
);
1727 int ret
= ram_discard_range(ramid
, start_addr
, block_length
);
1732 trace_loadvm_postcopy_ram_handle_discard_end();
1738 * Triggered by a postcopy_listen command; this thread takes over reading
1739 * the input stream, leaving the main thread free to carry on loading the rest
1740 * of the device state (from RAM).
1741 * (TODO:This could do with being in a postcopy file - but there again it's
1742 * just another input loop, not that postcopy specific)
1744 static void *postcopy_ram_listen_thread(void *opaque
)
1746 MigrationIncomingState
*mis
= migration_incoming_get_current();
1747 QEMUFile
*f
= mis
->from_src_file
;
1750 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
1751 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
1752 qemu_sem_post(&mis
->listen_thread_sem
);
1753 trace_postcopy_ram_listen_thread_start();
1755 rcu_register_thread();
1757 * Because we're a thread and not a coroutine we can't yield
1758 * in qemu_file, and thus we must be blocking now.
1760 qemu_file_set_blocking(f
, true);
1761 load_res
= qemu_loadvm_state_main(f
, mis
);
1764 * This is tricky, but, mis->from_src_file can change after it
1765 * returns, when postcopy recovery happened. In the future, we may
1766 * want a wrapper for the QEMUFile handle.
1768 f
= mis
->from_src_file
;
1770 /* And non-blocking again so we don't block in any cleanup */
1771 qemu_file_set_blocking(f
, false);
1773 trace_postcopy_ram_listen_thread_exit();
1775 error_report("%s: loadvm failed: %d", __func__
, load_res
);
1776 qemu_file_set_error(f
, load_res
);
1777 migrate_set_state(&mis
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
1778 MIGRATION_STATUS_FAILED
);
1781 * This looks good, but it's possible that the device loading in the
1782 * main thread hasn't finished yet, and so we might not be in 'RUN'
1783 * state yet; wait for the end of the main thread.
1785 qemu_event_wait(&mis
->main_thread_load_event
);
1787 postcopy_ram_incoming_cleanup(mis
);
1791 * If something went wrong then we have a bad state so exit;
1792 * depending how far we got it might be possible at this point
1793 * to leave the guest running and fire MCEs for pages that never
1794 * arrived as a desperate recovery step.
1796 rcu_unregister_thread();
1800 migrate_set_state(&mis
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
1801 MIGRATION_STATUS_COMPLETED
);
1803 * If everything has worked fine, then the main thread has waited
1804 * for us to start, and we're the last use of the mis.
1805 * (If something broke then qemu will have to exit anyway since it's
1806 * got a bad migration state).
1808 migration_incoming_state_destroy();
1809 qemu_loadvm_state_cleanup();
1811 rcu_unregister_thread();
1812 mis
->have_listen_thread
= false;
1816 /* After this message we must be able to immediately receive postcopy data */
1817 static int loadvm_postcopy_handle_listen(MigrationIncomingState
*mis
)
1819 PostcopyState ps
= postcopy_state_set(POSTCOPY_INCOMING_LISTENING
);
1820 trace_loadvm_postcopy_handle_listen();
1821 Error
*local_err
= NULL
;
1823 if (ps
!= POSTCOPY_INCOMING_ADVISE
&& ps
!= POSTCOPY_INCOMING_DISCARD
) {
1824 error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps
);
1827 if (ps
== POSTCOPY_INCOMING_ADVISE
) {
1829 * A rare case, we entered listen without having to do any discards,
1830 * so do the setup that's normally done at the time of the 1st discard.
1832 if (migrate_postcopy_ram()) {
1833 postcopy_ram_prepare_discard(mis
);
1838 * Sensitise RAM - can now generate requests for blocks that don't exist
1839 * However, at this point the CPU shouldn't be running, and the IO
1840 * shouldn't be doing anything yet so don't actually expect requests
1842 if (migrate_postcopy_ram()) {
1843 if (postcopy_ram_enable_notify(mis
)) {
1844 postcopy_ram_incoming_cleanup(mis
);
1849 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN
, &local_err
)) {
1850 error_report_err(local_err
);
1854 if (mis
->have_listen_thread
) {
1855 error_report("CMD_POSTCOPY_RAM_LISTEN already has a listen thread");
1859 mis
->have_listen_thread
= true;
1860 /* Start up the listening thread and wait for it to signal ready */
1861 qemu_sem_init(&mis
->listen_thread_sem
, 0);
1862 qemu_thread_create(&mis
->listen_thread
, "postcopy/listen",
1863 postcopy_ram_listen_thread
, NULL
,
1864 QEMU_THREAD_DETACHED
);
1865 qemu_sem_wait(&mis
->listen_thread_sem
);
1866 qemu_sem_destroy(&mis
->listen_thread_sem
);
1876 static void loadvm_postcopy_handle_run_bh(void *opaque
)
1878 Error
*local_err
= NULL
;
1879 HandleRunBhData
*data
= opaque
;
1880 MigrationIncomingState
*mis
= migration_incoming_get_current();
1882 /* TODO we should move all of this lot into postcopy_ram.c or a shared code
1885 cpu_synchronize_all_post_init();
1887 qemu_announce_self(&mis
->announce_timer
, migrate_announce_params());
1889 /* Make sure all file formats flush their mutable metadata.
1890 * If we get an error here, just don't restart the VM yet. */
1891 bdrv_invalidate_cache_all(&local_err
);
1893 error_report_err(local_err
);
1898 trace_loadvm_postcopy_handle_run_cpu_sync();
1900 trace_loadvm_postcopy_handle_run_vmstart();
1902 dirty_bitmap_mig_before_vm_start();
1905 /* Hold onto your hats, starting the CPU */
1908 /* leave it paused and let management decide when to start the CPU */
1909 runstate_set(RUN_STATE_PAUSED
);
1912 qemu_bh_delete(data
->bh
);
1916 /* After all discards we can start running and asking for pages */
1917 static int loadvm_postcopy_handle_run(MigrationIncomingState
*mis
)
1919 PostcopyState ps
= postcopy_state_set(POSTCOPY_INCOMING_RUNNING
);
1920 HandleRunBhData
*data
;
1922 trace_loadvm_postcopy_handle_run();
1923 if (ps
!= POSTCOPY_INCOMING_LISTENING
) {
1924 error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps
);
1928 data
= g_new(HandleRunBhData
, 1);
1929 data
->bh
= qemu_bh_new(loadvm_postcopy_handle_run_bh
, data
);
1930 qemu_bh_schedule(data
->bh
);
1932 /* We need to finish reading the stream from the package
1933 * and also stop reading anything more from the stream that loaded the
1934 * package (since it's now being read by the listener thread).
1935 * LOADVM_QUIT will quit all the layers of nested loadvm loops.
1940 static int loadvm_postcopy_handle_resume(MigrationIncomingState
*mis
)
1942 if (mis
->state
!= MIGRATION_STATUS_POSTCOPY_RECOVER
) {
1943 error_report("%s: illegal resume received", __func__
);
1944 /* Don't fail the load, only for this. */
1949 * This means source VM is ready to resume the postcopy migration.
1950 * It's time to switch state and release the fault thread to
1951 * continue service page faults.
1953 migrate_set_state(&mis
->state
, MIGRATION_STATUS_POSTCOPY_RECOVER
,
1954 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
1955 qemu_sem_post(&mis
->postcopy_pause_sem_fault
);
1957 trace_loadvm_postcopy_handle_resume();
1959 /* Tell source that "we are ready" */
1960 migrate_send_rp_resume_ack(mis
, MIGRATION_RESUME_ACK_VALUE
);
1966 * Immediately following this command is a blob of data containing an embedded
1967 * chunk of migration stream; read it and load it.
1969 * @mis: Incoming state
1970 * @length: Length of packaged data to read
1972 * Returns: Negative values on error
1975 static int loadvm_handle_cmd_packaged(MigrationIncomingState
*mis
)
1979 QIOChannelBuffer
*bioc
;
1981 length
= qemu_get_be32(mis
->from_src_file
);
1982 trace_loadvm_handle_cmd_packaged(length
);
1984 if (length
> MAX_VM_CMD_PACKAGED_SIZE
) {
1985 error_report("Unreasonably large packaged state: %zu", length
);
1989 bioc
= qio_channel_buffer_new(length
);
1990 qio_channel_set_name(QIO_CHANNEL(bioc
), "migration-loadvm-buffer");
1991 ret
= qemu_get_buffer(mis
->from_src_file
,
1994 if (ret
!= length
) {
1995 object_unref(OBJECT(bioc
));
1996 error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu",
1998 return (ret
< 0) ? ret
: -EAGAIN
;
2000 bioc
->usage
+= length
;
2001 trace_loadvm_handle_cmd_packaged_received(ret
);
2003 QEMUFile
*packf
= qemu_fopen_channel_input(QIO_CHANNEL(bioc
));
2005 ret
= qemu_loadvm_state_main(packf
, mis
);
2006 trace_loadvm_handle_cmd_packaged_main(ret
);
2008 object_unref(OBJECT(bioc
));
2014 * Handle request that source requests for recved_bitmap on
2015 * destination. Payload format:
2017 * len (1 byte) + ramblock_name (<255 bytes)
2019 static int loadvm_handle_recv_bitmap(MigrationIncomingState
*mis
,
2022 QEMUFile
*file
= mis
->from_src_file
;
2024 char block_name
[256];
2027 cnt
= qemu_get_counted_string(file
, block_name
);
2029 error_report("%s: failed to read block name", __func__
);
2033 /* Validate before using the data */
2034 if (qemu_file_get_error(file
)) {
2035 return qemu_file_get_error(file
);
2038 if (len
!= cnt
+ 1) {
2039 error_report("%s: invalid payload length (%d)", __func__
, len
);
2043 rb
= qemu_ram_block_by_name(block_name
);
2045 error_report("%s: block '%s' not found", __func__
, block_name
);
2049 migrate_send_rp_recv_bitmap(mis
, block_name
);
2051 trace_loadvm_handle_recv_bitmap(block_name
);
2056 static int loadvm_process_enable_colo(MigrationIncomingState
*mis
)
2058 migration_incoming_enable_colo();
2059 return colo_init_ram_cache();
2063 * Process an incoming 'QEMU_VM_COMMAND'
2064 * 0 just a normal return
2065 * LOADVM_QUIT All good, but exit the loop
2068 static int loadvm_process_command(QEMUFile
*f
)
2070 MigrationIncomingState
*mis
= migration_incoming_get_current();
2075 cmd
= qemu_get_be16(f
);
2076 len
= qemu_get_be16(f
);
2078 /* Check validity before continue processing of cmds */
2079 if (qemu_file_get_error(f
)) {
2080 return qemu_file_get_error(f
);
2083 trace_loadvm_process_command(cmd
, len
);
2084 if (cmd
>= MIG_CMD_MAX
|| cmd
== MIG_CMD_INVALID
) {
2085 error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd
, len
);
2089 if (mig_cmd_args
[cmd
].len
!= -1 && mig_cmd_args
[cmd
].len
!= len
) {
2090 error_report("%s received with bad length - expecting %zu, got %d",
2091 mig_cmd_args
[cmd
].name
,
2092 (size_t)mig_cmd_args
[cmd
].len
, len
);
2097 case MIG_CMD_OPEN_RETURN_PATH
:
2098 if (mis
->to_src_file
) {
2099 error_report("CMD_OPEN_RETURN_PATH called when RP already open");
2100 /* Not really a problem, so don't give up */
2103 mis
->to_src_file
= qemu_file_get_return_path(f
);
2104 if (!mis
->to_src_file
) {
2105 error_report("CMD_OPEN_RETURN_PATH failed");
2111 tmp32
= qemu_get_be32(f
);
2112 trace_loadvm_process_command_ping(tmp32
);
2113 if (!mis
->to_src_file
) {
2114 error_report("CMD_PING (0x%x) received with no return path",
2118 migrate_send_rp_pong(mis
, tmp32
);
2121 case MIG_CMD_PACKAGED
:
2122 return loadvm_handle_cmd_packaged(mis
);
2124 case MIG_CMD_POSTCOPY_ADVISE
:
2125 return loadvm_postcopy_handle_advise(mis
, len
);
2127 case MIG_CMD_POSTCOPY_LISTEN
:
2128 return loadvm_postcopy_handle_listen(mis
);
2130 case MIG_CMD_POSTCOPY_RUN
:
2131 return loadvm_postcopy_handle_run(mis
);
2133 case MIG_CMD_POSTCOPY_RAM_DISCARD
:
2134 return loadvm_postcopy_ram_handle_discard(mis
, len
);
2136 case MIG_CMD_POSTCOPY_RESUME
:
2137 return loadvm_postcopy_handle_resume(mis
);
2139 case MIG_CMD_RECV_BITMAP
:
2140 return loadvm_handle_recv_bitmap(mis
, len
);
2142 case MIG_CMD_ENABLE_COLO
:
2143 return loadvm_process_enable_colo(mis
);
2150 * Read a footer off the wire and check that it matches the expected section
2152 * Returns: true if the footer was good
2153 * false if there is a problem (and calls error_report to say why)
2155 static bool check_section_footer(QEMUFile
*f
, SaveStateEntry
*se
)
2159 uint32_t read_section_id
;
2161 if (!migrate_get_current()->send_section_footer
) {
2162 /* No footer to check */
2166 read_mark
= qemu_get_byte(f
);
2168 ret
= qemu_file_get_error(f
);
2170 error_report("%s: Read section footer failed: %d",
2175 if (read_mark
!= QEMU_VM_SECTION_FOOTER
) {
2176 error_report("Missing section footer for %s", se
->idstr
);
2180 read_section_id
= qemu_get_be32(f
);
2181 if (read_section_id
!= se
->load_section_id
) {
2182 error_report("Mismatched section id in footer for %s -"
2183 " read 0x%x expected 0x%x",
2184 se
->idstr
, read_section_id
, se
->load_section_id
);
2193 qemu_loadvm_section_start_full(QEMUFile
*f
, MigrationIncomingState
*mis
)
2195 uint32_t instance_id
, version_id
, section_id
;
2200 /* Read section start */
2201 section_id
= qemu_get_be32(f
);
2202 if (!qemu_get_counted_string(f
, idstr
)) {
2203 error_report("Unable to read ID string for section %u",
2207 instance_id
= qemu_get_be32(f
);
2208 version_id
= qemu_get_be32(f
);
2210 ret
= qemu_file_get_error(f
);
2212 error_report("%s: Failed to read instance/version ID: %d",
2217 trace_qemu_loadvm_state_section_startfull(section_id
, idstr
,
2218 instance_id
, version_id
);
2219 /* Find savevm section */
2220 se
= find_se(idstr
, instance_id
);
2222 error_report("Unknown savevm section or instance '%s' %d. "
2223 "Make sure that your current VM setup matches your "
2224 "saved VM setup, including any hotplugged devices",
2225 idstr
, instance_id
);
2229 /* Validate version */
2230 if (version_id
> se
->version_id
) {
2231 error_report("savevm: unsupported version %d for '%s' v%d",
2232 version_id
, idstr
, se
->version_id
);
2235 se
->load_version_id
= version_id
;
2236 se
->load_section_id
= section_id
;
2238 /* Validate if it is a device's state */
2239 if (xen_enabled() && se
->is_ram
) {
2240 error_report("loadvm: %s RAM loading not allowed on Xen", idstr
);
2244 ret
= vmstate_load(f
, se
);
2246 error_report("error while loading state for instance 0x%x of"
2247 " device '%s'", instance_id
, idstr
);
2250 if (!check_section_footer(f
, se
)) {
2258 qemu_loadvm_section_part_end(QEMUFile
*f
, MigrationIncomingState
*mis
)
2260 uint32_t section_id
;
2264 section_id
= qemu_get_be32(f
);
2266 ret
= qemu_file_get_error(f
);
2268 error_report("%s: Failed to read section ID: %d",
2273 trace_qemu_loadvm_state_section_partend(section_id
);
2274 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
2275 if (se
->load_section_id
== section_id
) {
2280 error_report("Unknown savevm section %d", section_id
);
2284 ret
= vmstate_load(f
, se
);
2286 error_report("error while loading state section id %d(%s)",
2287 section_id
, se
->idstr
);
2290 if (!check_section_footer(f
, se
)) {
2297 static int qemu_loadvm_state_header(QEMUFile
*f
)
2302 v
= qemu_get_be32(f
);
2303 if (v
!= QEMU_VM_FILE_MAGIC
) {
2304 error_report("Not a migration stream");
2308 v
= qemu_get_be32(f
);
2309 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
2310 error_report("SaveVM v2 format is obsolete and don't work anymore");
2313 if (v
!= QEMU_VM_FILE_VERSION
) {
2314 error_report("Unsupported migration stream version");
2318 if (migrate_get_current()->send_configuration
) {
2319 if (qemu_get_byte(f
) != QEMU_VM_CONFIGURATION
) {
2320 error_report("Configuration section missing");
2321 qemu_loadvm_state_cleanup();
2324 ret
= vmstate_load_state(f
, &vmstate_configuration
, &savevm_state
, 0);
2327 qemu_loadvm_state_cleanup();
2334 static int qemu_loadvm_state_setup(QEMUFile
*f
)
2339 trace_loadvm_state_setup();
2340 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
2341 if (!se
->ops
|| !se
->ops
->load_setup
) {
2344 if (se
->ops
&& se
->ops
->is_active
) {
2345 if (!se
->ops
->is_active(se
->opaque
)) {
2350 ret
= se
->ops
->load_setup(f
, se
->opaque
);
2352 qemu_file_set_error(f
, ret
);
2353 error_report("Load state of device %s failed", se
->idstr
);
2360 void qemu_loadvm_state_cleanup(void)
2364 trace_loadvm_state_cleanup();
2365 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
2366 if (se
->ops
&& se
->ops
->load_cleanup
) {
2367 se
->ops
->load_cleanup(se
->opaque
);
2372 /* Return true if we should continue the migration, or false. */
2373 static bool postcopy_pause_incoming(MigrationIncomingState
*mis
)
2375 trace_postcopy_pause_incoming();
2377 /* Clear the triggered bit to allow one recovery */
2378 mis
->postcopy_recover_triggered
= false;
2380 assert(mis
->from_src_file
);
2381 qemu_file_shutdown(mis
->from_src_file
);
2382 qemu_fclose(mis
->from_src_file
);
2383 mis
->from_src_file
= NULL
;
2385 assert(mis
->to_src_file
);
2386 qemu_file_shutdown(mis
->to_src_file
);
2387 qemu_mutex_lock(&mis
->rp_mutex
);
2388 qemu_fclose(mis
->to_src_file
);
2389 mis
->to_src_file
= NULL
;
2390 qemu_mutex_unlock(&mis
->rp_mutex
);
2392 migrate_set_state(&mis
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
2393 MIGRATION_STATUS_POSTCOPY_PAUSED
);
2395 /* Notify the fault thread for the invalidated file handle */
2396 postcopy_fault_thread_notify(mis
);
2398 error_report("Detected IO failure for postcopy. "
2399 "Migration paused.");
2401 while (mis
->state
== MIGRATION_STATUS_POSTCOPY_PAUSED
) {
2402 qemu_sem_wait(&mis
->postcopy_pause_sem_dst
);
2405 trace_postcopy_pause_incoming_continued();
2410 int qemu_loadvm_state_main(QEMUFile
*f
, MigrationIncomingState
*mis
)
2412 uint8_t section_type
;
2417 section_type
= qemu_get_byte(f
);
2419 if (qemu_file_get_error(f
)) {
2420 ret
= qemu_file_get_error(f
);
2424 trace_qemu_loadvm_state_section(section_type
);
2425 switch (section_type
) {
2426 case QEMU_VM_SECTION_START
:
2427 case QEMU_VM_SECTION_FULL
:
2428 ret
= qemu_loadvm_section_start_full(f
, mis
);
2433 case QEMU_VM_SECTION_PART
:
2434 case QEMU_VM_SECTION_END
:
2435 ret
= qemu_loadvm_section_part_end(f
, mis
);
2440 case QEMU_VM_COMMAND
:
2441 ret
= loadvm_process_command(f
);
2442 trace_qemu_loadvm_state_section_command(ret
);
2443 if ((ret
< 0) || (ret
& LOADVM_QUIT
)) {
2448 /* This is the end of migration */
2451 error_report("Unknown savevm section type %d", section_type
);
2459 qemu_file_set_error(f
, ret
);
2462 * If we are during an active postcopy, then we pause instead
2463 * of bail out to at least keep the VM's dirty data. Note
2464 * that POSTCOPY_INCOMING_LISTENING stage is still not enough,
2465 * during which we're still receiving device states and we
2466 * still haven't yet started the VM on destination.
2468 if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING
&&
2469 postcopy_pause_incoming(mis
)) {
2470 /* Reset f to point to the newly created channel */
2471 f
= mis
->from_src_file
;
2478 int qemu_loadvm_state(QEMUFile
*f
)
2480 MigrationIncomingState
*mis
= migration_incoming_get_current();
2481 Error
*local_err
= NULL
;
2484 if (qemu_savevm_state_blocked(&local_err
)) {
2485 error_report_err(local_err
);
2489 ret
= qemu_loadvm_state_header(f
);
2494 if (qemu_loadvm_state_setup(f
) != 0) {
2498 cpu_synchronize_all_pre_loadvm();
2500 ret
= qemu_loadvm_state_main(f
, mis
);
2501 qemu_event_set(&mis
->main_thread_load_event
);
2503 trace_qemu_loadvm_state_post_main(ret
);
2505 if (mis
->have_listen_thread
) {
2506 /* Listen thread still going, can't clean up yet */
2511 ret
= qemu_file_get_error(f
);
2515 * Try to read in the VMDESC section as well, so that dumping tools that
2516 * intercept our migration stream have the chance to see it.
2519 /* We've got to be careful; if we don't read the data and just shut the fd
2520 * then the sender can error if we close while it's still sending.
2521 * We also mustn't read data that isn't there; some transports (RDMA)
2522 * will stall waiting for that data when the source has already closed.
2524 if (ret
== 0 && should_send_vmdesc()) {
2527 uint8_t section_type
= qemu_get_byte(f
);
2529 if (section_type
!= QEMU_VM_VMDESCRIPTION
) {
2530 error_report("Expected vmdescription section, but got %d",
2533 * It doesn't seem worth failing at this point since
2534 * we apparently have an otherwise valid VM state
2537 buf
= g_malloc(0x1000);
2538 size
= qemu_get_be32(f
);
2541 uint32_t read_chunk
= MIN(size
, 0x1000);
2542 qemu_get_buffer(f
, buf
, read_chunk
);
2549 qemu_loadvm_state_cleanup();
2550 cpu_synchronize_all_post_init();
2555 int qemu_load_device_state(QEMUFile
*f
)
2557 MigrationIncomingState
*mis
= migration_incoming_get_current();
2560 /* Load QEMU_VM_SECTION_FULL section */
2561 ret
= qemu_loadvm_state_main(f
, mis
);
2563 error_report("Failed to load device state: %d", ret
);
2567 cpu_synchronize_all_post_init();
2571 int save_snapshot(const char *name
, Error
**errp
)
2573 BlockDriverState
*bs
, *bs1
;
2574 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
2577 int saved_vm_running
;
2578 uint64_t vm_state_size
;
2581 AioContext
*aio_context
;
2583 if (migration_is_blocked(errp
)) {
2587 if (!replay_can_snapshot()) {
2588 error_setg(errp
, "Record/replay does not allow making snapshot "
2589 "right now. Try once more later.");
2593 if (!bdrv_all_can_snapshot(&bs
)) {
2594 error_setg(errp
, "Device '%s' is writable but does not support "
2595 "snapshots", bdrv_get_device_name(bs
));
2599 /* Delete old snapshots of the same name */
2601 ret
= bdrv_all_delete_snapshot(name
, &bs1
, errp
);
2603 error_prepend(errp
, "Error while deleting snapshot on device "
2604 "'%s': ", bdrv_get_device_name(bs1
));
2609 bs
= bdrv_all_find_vmstate_bs();
2611 error_setg(errp
, "No block device can accept snapshots");
2614 aio_context
= bdrv_get_aio_context(bs
);
2616 saved_vm_running
= runstate_is_running();
2618 ret
= global_state_store();
2620 error_setg(errp
, "Error saving global state");
2623 vm_stop(RUN_STATE_SAVE_VM
);
2625 bdrv_drain_all_begin();
2627 aio_context_acquire(aio_context
);
2629 memset(sn
, 0, sizeof(*sn
));
2631 /* fill auxiliary fields */
2632 qemu_gettimeofday(&tv
);
2633 sn
->date_sec
= tv
.tv_sec
;
2634 sn
->date_nsec
= tv
.tv_usec
* 1000;
2635 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2638 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
2640 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
2641 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
2643 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
2646 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2647 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
2648 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
2651 /* save the VM state */
2652 f
= qemu_fopen_bdrv(bs
, 1);
2654 error_setg(errp
, "Could not open VM state file");
2657 ret
= qemu_savevm_state(f
, errp
);
2658 vm_state_size
= qemu_ftell(f
);
2664 /* The bdrv_all_create_snapshot() call that follows acquires the AioContext
2665 * for itself. BDRV_POLL_WHILE() does not support nested locking because
2666 * it only releases the lock once. Therefore synchronous I/O will deadlock
2667 * unless we release the AioContext before bdrv_all_create_snapshot().
2669 aio_context_release(aio_context
);
2672 ret
= bdrv_all_create_snapshot(sn
, bs
, vm_state_size
, &bs
);
2674 error_setg(errp
, "Error while creating snapshot on '%s'",
2675 bdrv_get_device_name(bs
));
2683 aio_context_release(aio_context
);
2686 bdrv_drain_all_end();
2688 if (saved_vm_running
) {
2694 void qmp_xen_save_devices_state(const char *filename
, bool has_live
, bool live
,
2698 QIOChannelFile
*ioc
;
2699 int saved_vm_running
;
2703 /* live default to true so old version of Xen tool stack can have a
2704 * successfull live migration */
2708 saved_vm_running
= runstate_is_running();
2709 vm_stop(RUN_STATE_SAVE_VM
);
2710 global_state_store_running();
2712 ioc
= qio_channel_file_new_path(filename
, O_WRONLY
| O_CREAT
, 0660, errp
);
2716 qio_channel_set_name(QIO_CHANNEL(ioc
), "migration-xen-save-state");
2717 f
= qemu_fopen_channel_output(QIO_CHANNEL(ioc
));
2718 object_unref(OBJECT(ioc
));
2719 ret
= qemu_save_device_state(f
);
2720 if (ret
< 0 || qemu_fclose(f
) < 0) {
2721 error_setg(errp
, QERR_IO_ERROR
);
2723 /* libxl calls the QMP command "stop" before calling
2724 * "xen-save-devices-state" and in case of migration failure, libxl
2725 * would call "cont".
2726 * So call bdrv_inactivate_all (release locks) here to let the other
2727 * side of the migration take controle of the images.
2729 if (live
&& !saved_vm_running
) {
2730 ret
= bdrv_inactivate_all();
2732 error_setg(errp
, "%s: bdrv_inactivate_all() failed (%d)",
2739 if (saved_vm_running
) {
2744 void qmp_xen_load_devices_state(const char *filename
, Error
**errp
)
2747 QIOChannelFile
*ioc
;
2750 /* Guest must be paused before loading the device state; the RAM state
2751 * will already have been loaded by xc
2753 if (runstate_is_running()) {
2754 error_setg(errp
, "Cannot update device state while vm is running");
2757 vm_stop(RUN_STATE_RESTORE_VM
);
2759 ioc
= qio_channel_file_new_path(filename
, O_RDONLY
| O_BINARY
, 0, errp
);
2763 qio_channel_set_name(QIO_CHANNEL(ioc
), "migration-xen-load-state");
2764 f
= qemu_fopen_channel_input(QIO_CHANNEL(ioc
));
2765 object_unref(OBJECT(ioc
));
2767 ret
= qemu_loadvm_state(f
);
2770 error_setg(errp
, QERR_IO_ERROR
);
2772 migration_incoming_state_destroy();
2775 int load_snapshot(const char *name
, Error
**errp
)
2777 BlockDriverState
*bs
, *bs_vm_state
;
2778 QEMUSnapshotInfo sn
;
2781 AioContext
*aio_context
;
2782 MigrationIncomingState
*mis
= migration_incoming_get_current();
2784 if (!replay_can_snapshot()) {
2785 error_setg(errp
, "Record/replay does not allow loading snapshot "
2786 "right now. Try once more later.");
2790 if (!bdrv_all_can_snapshot(&bs
)) {
2792 "Device '%s' is writable but does not support snapshots",
2793 bdrv_get_device_name(bs
));
2796 ret
= bdrv_all_find_snapshot(name
, &bs
);
2799 "Device '%s' does not have the requested snapshot '%s'",
2800 bdrv_get_device_name(bs
), name
);
2804 bs_vm_state
= bdrv_all_find_vmstate_bs();
2806 error_setg(errp
, "No block device supports snapshots");
2809 aio_context
= bdrv_get_aio_context(bs_vm_state
);
2811 /* Don't even try to load empty VM states */
2812 aio_context_acquire(aio_context
);
2813 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
2814 aio_context_release(aio_context
);
2817 } else if (sn
.vm_state_size
== 0) {
2818 error_setg(errp
, "This is a disk-only snapshot. Revert to it "
2819 " offline using qemu-img");
2823 /* Flush all IO requests so they don't interfere with the new state. */
2824 bdrv_drain_all_begin();
2826 ret
= bdrv_all_goto_snapshot(name
, &bs
, errp
);
2828 error_prepend(errp
, "Could not load snapshot '%s' on '%s': ",
2829 name
, bdrv_get_device_name(bs
));
2833 /* restore the VM state */
2834 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
2836 error_setg(errp
, "Could not open VM state file");
2841 qemu_system_reset(SHUTDOWN_CAUSE_NONE
);
2842 mis
->from_src_file
= f
;
2844 aio_context_acquire(aio_context
);
2845 ret
= qemu_loadvm_state(f
);
2846 migration_incoming_state_destroy();
2847 aio_context_release(aio_context
);
2849 bdrv_drain_all_end();
2852 error_setg(errp
, "Error %d while loading VM state", ret
);
2859 bdrv_drain_all_end();
2863 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2865 qemu_ram_set_idstr(mr
->ram_block
,
2866 memory_region_name(mr
), dev
);
2867 qemu_ram_set_migratable(mr
->ram_block
);
2870 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2872 qemu_ram_unset_idstr(mr
->ram_block
);
2873 qemu_ram_unset_migratable(mr
->ram_block
);
2876 void vmstate_register_ram_global(MemoryRegion
*mr
)
2878 vmstate_register_ram(mr
, NULL
);
2881 bool vmstate_check_only_migratable(const VMStateDescription
*vmsd
)
2883 /* check needed if --only-migratable is specified */
2884 if (!only_migratable
) {
2888 return !(vmsd
&& vmsd
->unmigratable
);