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/vmstate.h"
36 #include "migration/misc.h"
37 #include "migration/register.h"
38 #include "migration/global_state.h"
40 #include "qemu-file-channel.h"
41 #include "qemu-file.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"
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/sysemu.h"
62 #include "migration/colo.h"
63 #include "qemu/bitmap.h"
64 #include "net/announce.h"
66 const unsigned int postcopy_ram_discard_version
= 0;
68 /* Subcommands for QEMU_VM_COMMAND */
70 MIG_CMD_INVALID
= 0, /* Must be 0 */
71 MIG_CMD_OPEN_RETURN_PATH
, /* Tell the dest to open the Return path */
72 MIG_CMD_PING
, /* Request a PONG on the RP */
74 MIG_CMD_POSTCOPY_ADVISE
, /* Prior to any page transfers, just
75 warn we might want to do PC */
76 MIG_CMD_POSTCOPY_LISTEN
, /* Start listening for incoming
77 pages as it's running. */
78 MIG_CMD_POSTCOPY_RUN
, /* Start execution */
80 MIG_CMD_POSTCOPY_RAM_DISCARD
, /* A list of pages to discard that
81 were previously sent during
82 precopy but are dirty. */
83 MIG_CMD_PACKAGED
, /* Send a wrapped stream within this stream */
84 MIG_CMD_ENABLE_COLO
, /* Enable COLO */
85 MIG_CMD_POSTCOPY_RESUME
, /* resume postcopy on dest */
86 MIG_CMD_RECV_BITMAP
, /* Request for recved bitmap on dst */
90 #define MAX_VM_CMD_PACKAGED_SIZE UINT32_MAX
91 static struct mig_cmd_args
{
92 ssize_t len
; /* -1 = variable */
95 [MIG_CMD_INVALID
] = { .len
= -1, .name
= "INVALID" },
96 [MIG_CMD_OPEN_RETURN_PATH
] = { .len
= 0, .name
= "OPEN_RETURN_PATH" },
97 [MIG_CMD_PING
] = { .len
= sizeof(uint32_t), .name
= "PING" },
98 [MIG_CMD_POSTCOPY_ADVISE
] = { .len
= -1, .name
= "POSTCOPY_ADVISE" },
99 [MIG_CMD_POSTCOPY_LISTEN
] = { .len
= 0, .name
= "POSTCOPY_LISTEN" },
100 [MIG_CMD_POSTCOPY_RUN
] = { .len
= 0, .name
= "POSTCOPY_RUN" },
101 [MIG_CMD_POSTCOPY_RAM_DISCARD
] = {
102 .len
= -1, .name
= "POSTCOPY_RAM_DISCARD" },
103 [MIG_CMD_POSTCOPY_RESUME
] = { .len
= 0, .name
= "POSTCOPY_RESUME" },
104 [MIG_CMD_PACKAGED
] = { .len
= 4, .name
= "PACKAGED" },
105 [MIG_CMD_RECV_BITMAP
] = { .len
= -1, .name
= "RECV_BITMAP" },
106 [MIG_CMD_MAX
] = { .len
= -1, .name
= "MAX" },
109 /* Note for MIG_CMD_POSTCOPY_ADVISE:
110 * The format of arguments is depending on postcopy mode:
111 * - postcopy RAM only
112 * uint64_t host page size
113 * uint64_t taget page size
115 * - postcopy RAM and postcopy dirty bitmaps
116 * format is the same as for postcopy RAM only
118 * - postcopy dirty bitmaps only
119 * Nothing. Command length field is 0.
121 * Be careful: adding a new postcopy entity with some other parameters should
122 * not break format self-description ability. Good way is to introduce some
123 * generic extendable format with an exception for two old entities.
126 /***********************************************************/
127 /* savevm/loadvm support */
129 static ssize_t
block_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
135 qemu_iovec_init_external(&qiov
, iov
, iovcnt
);
136 ret
= bdrv_writev_vmstate(opaque
, &qiov
, pos
);
144 static ssize_t
block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
,
147 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
150 static int bdrv_fclose(void *opaque
)
152 return bdrv_flush(opaque
);
155 static const QEMUFileOps bdrv_read_ops
= {
156 .get_buffer
= block_get_buffer
,
160 static const QEMUFileOps bdrv_write_ops
= {
161 .writev_buffer
= block_writev_buffer
,
165 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
168 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
170 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
174 /* QEMUFile timer support.
175 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
178 void timer_put(QEMUFile
*f
, QEMUTimer
*ts
)
180 uint64_t expire_time
;
182 expire_time
= timer_expire_time_ns(ts
);
183 qemu_put_be64(f
, expire_time
);
186 void timer_get(QEMUFile
*f
, QEMUTimer
*ts
)
188 uint64_t expire_time
;
190 expire_time
= qemu_get_be64(f
);
191 if (expire_time
!= -1) {
192 timer_mod_ns(ts
, expire_time
);
199 /* VMState timer support.
200 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
203 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
,
204 const VMStateField
*field
)
211 static int put_timer(QEMUFile
*f
, void *pv
, size_t size
,
212 const VMStateField
*field
, QJSON
*vmdesc
)
220 const VMStateInfo vmstate_info_timer
= {
227 typedef struct CompatEntry
{
232 typedef struct SaveStateEntry
{
233 QTAILQ_ENTRY(SaveStateEntry
) entry
;
238 /* version id read from the stream */
241 /* section id read from the stream */
243 const SaveVMHandlers
*ops
;
244 const VMStateDescription
*vmsd
;
250 typedef struct SaveState
{
251 QTAILQ_HEAD(, SaveStateEntry
) handlers
;
252 int global_section_id
;
255 uint32_t target_page_bits
;
257 MigrationCapability
*capabilities
;
260 static SaveState savevm_state
= {
261 .handlers
= QTAILQ_HEAD_INITIALIZER(savevm_state
.handlers
),
262 .global_section_id
= 0,
265 static bool should_validate_capability(int capability
)
267 assert(capability
>= 0 && capability
< MIGRATION_CAPABILITY__MAX
);
268 /* Validate only new capabilities to keep compatibility. */
269 switch (capability
) {
270 case MIGRATION_CAPABILITY_X_IGNORE_SHARED
:
277 static uint32_t get_validatable_capabilities_count(void)
279 MigrationState
*s
= migrate_get_current();
282 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
283 if (should_validate_capability(i
) && s
->enabled_capabilities
[i
]) {
290 static int configuration_pre_save(void *opaque
)
292 SaveState
*state
= opaque
;
293 const char *current_name
= MACHINE_GET_CLASS(current_machine
)->name
;
294 MigrationState
*s
= migrate_get_current();
297 state
->len
= strlen(current_name
);
298 state
->name
= current_name
;
299 state
->target_page_bits
= qemu_target_page_bits();
301 state
->caps_count
= get_validatable_capabilities_count();
302 state
->capabilities
= g_renew(MigrationCapability
, state
->capabilities
,
304 for (i
= j
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
305 if (should_validate_capability(i
) && s
->enabled_capabilities
[i
]) {
306 state
->capabilities
[j
++] = i
;
313 static int configuration_pre_load(void *opaque
)
315 SaveState
*state
= opaque
;
317 /* If there is no target-page-bits subsection it means the source
318 * predates the variable-target-page-bits support and is using the
319 * minimum possible value for this CPU.
321 state
->target_page_bits
= qemu_target_page_bits_min();
325 static bool configuration_validate_capabilities(SaveState
*state
)
328 MigrationState
*s
= migrate_get_current();
329 unsigned long *source_caps_bm
;
332 source_caps_bm
= bitmap_new(MIGRATION_CAPABILITY__MAX
);
333 for (i
= 0; i
< state
->caps_count
; i
++) {
334 MigrationCapability capability
= state
->capabilities
[i
];
335 set_bit(capability
, source_caps_bm
);
338 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
339 bool source_state
, target_state
;
340 if (!should_validate_capability(i
)) {
343 source_state
= test_bit(i
, source_caps_bm
);
344 target_state
= s
->enabled_capabilities
[i
];
345 if (source_state
!= target_state
) {
346 error_report("Capability %s is %s, but received capability is %s",
347 MigrationCapability_str(i
),
348 target_state
? "on" : "off",
349 source_state
? "on" : "off");
351 /* Don't break here to report all failed capabilities */
355 g_free(source_caps_bm
);
359 static int configuration_post_load(void *opaque
, int version_id
)
361 SaveState
*state
= opaque
;
362 const char *current_name
= MACHINE_GET_CLASS(current_machine
)->name
;
364 if (strncmp(state
->name
, current_name
, state
->len
) != 0) {
365 error_report("Machine type received is '%.*s' and local is '%s'",
366 (int) state
->len
, state
->name
, current_name
);
370 if (state
->target_page_bits
!= qemu_target_page_bits()) {
371 error_report("Received TARGET_PAGE_BITS is %d but local is %d",
372 state
->target_page_bits
, qemu_target_page_bits());
376 if (!configuration_validate_capabilities(state
)) {
383 static int get_capability(QEMUFile
*f
, void *pv
, size_t size
,
384 const VMStateField
*field
)
386 MigrationCapability
*capability
= pv
;
387 char capability_str
[UINT8_MAX
+ 1];
391 len
= qemu_get_byte(f
);
392 qemu_get_buffer(f
, (uint8_t *)capability_str
, len
);
393 capability_str
[len
] = '\0';
394 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
395 if (!strcmp(MigrationCapability_str(i
), capability_str
)) {
400 error_report("Received unknown capability %s", capability_str
);
404 static int put_capability(QEMUFile
*f
, void *pv
, size_t size
,
405 const VMStateField
*field
, QJSON
*vmdesc
)
407 MigrationCapability
*capability
= pv
;
408 const char *capability_str
= MigrationCapability_str(*capability
);
409 size_t len
= strlen(capability_str
);
410 assert(len
<= UINT8_MAX
);
412 qemu_put_byte(f
, len
);
413 qemu_put_buffer(f
, (uint8_t *)capability_str
, len
);
417 static const VMStateInfo vmstate_info_capability
= {
418 .name
= "capability",
419 .get
= get_capability
,
420 .put
= put_capability
,
423 /* The target-page-bits subsection is present only if the
424 * target page size is not the same as the default (ie the
425 * minimum page size for a variable-page-size guest CPU).
426 * If it is present then it contains the actual target page
427 * bits for the machine, and migration will fail if the
428 * two ends don't agree about it.
430 static bool vmstate_target_page_bits_needed(void *opaque
)
432 return qemu_target_page_bits()
433 > qemu_target_page_bits_min();
436 static const VMStateDescription vmstate_target_page_bits
= {
437 .name
= "configuration/target-page-bits",
439 .minimum_version_id
= 1,
440 .needed
= vmstate_target_page_bits_needed
,
441 .fields
= (VMStateField
[]) {
442 VMSTATE_UINT32(target_page_bits
, SaveState
),
443 VMSTATE_END_OF_LIST()
447 static bool vmstate_capabilites_needed(void *opaque
)
449 return get_validatable_capabilities_count() > 0;
452 static const VMStateDescription vmstate_capabilites
= {
453 .name
= "configuration/capabilities",
455 .minimum_version_id
= 1,
456 .needed
= vmstate_capabilites_needed
,
457 .fields
= (VMStateField
[]) {
458 VMSTATE_UINT32_V(caps_count
, SaveState
, 1),
459 VMSTATE_VARRAY_UINT32_ALLOC(capabilities
, SaveState
, caps_count
, 1,
460 vmstate_info_capability
,
461 MigrationCapability
),
462 VMSTATE_END_OF_LIST()
466 static const VMStateDescription vmstate_configuration
= {
467 .name
= "configuration",
469 .pre_load
= configuration_pre_load
,
470 .post_load
= configuration_post_load
,
471 .pre_save
= configuration_pre_save
,
472 .fields
= (VMStateField
[]) {
473 VMSTATE_UINT32(len
, SaveState
),
474 VMSTATE_VBUFFER_ALLOC_UINT32(name
, SaveState
, 0, NULL
, len
),
475 VMSTATE_END_OF_LIST()
477 .subsections
= (const VMStateDescription
*[]) {
478 &vmstate_target_page_bits
,
479 &vmstate_capabilites
,
484 static void dump_vmstate_vmsd(FILE *out_file
,
485 const VMStateDescription
*vmsd
, int indent
,
488 static void dump_vmstate_vmsf(FILE *out_file
, const VMStateField
*field
,
491 fprintf(out_file
, "%*s{\n", indent
, "");
493 fprintf(out_file
, "%*s\"field\": \"%s\",\n", indent
, "", field
->name
);
494 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
496 fprintf(out_file
, "%*s\"field_exists\": %s,\n", indent
, "",
497 field
->field_exists
? "true" : "false");
498 fprintf(out_file
, "%*s\"size\": %zu", indent
, "", field
->size
);
499 if (field
->vmsd
!= NULL
) {
500 fprintf(out_file
, ",\n");
501 dump_vmstate_vmsd(out_file
, field
->vmsd
, indent
, false);
503 fprintf(out_file
, "\n%*s}", indent
- 2, "");
506 static void dump_vmstate_vmss(FILE *out_file
,
507 const VMStateDescription
**subsection
,
510 if (*subsection
!= NULL
) {
511 dump_vmstate_vmsd(out_file
, *subsection
, indent
, true);
515 static void dump_vmstate_vmsd(FILE *out_file
,
516 const VMStateDescription
*vmsd
, int indent
,
520 fprintf(out_file
, "%*s{\n", indent
, "");
522 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", "Description");
525 fprintf(out_file
, "%*s\"name\": \"%s\",\n", indent
, "", vmsd
->name
);
526 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
528 fprintf(out_file
, "%*s\"minimum_version_id\": %d", indent
, "",
529 vmsd
->minimum_version_id
);
530 if (vmsd
->fields
!= NULL
) {
531 const VMStateField
*field
= vmsd
->fields
;
534 fprintf(out_file
, ",\n%*s\"Fields\": [\n", indent
, "");
536 while (field
->name
!= NULL
) {
537 if (field
->flags
& VMS_MUST_EXIST
) {
538 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
543 fprintf(out_file
, ",\n");
545 dump_vmstate_vmsf(out_file
, field
, indent
+ 2);
549 fprintf(out_file
, "\n%*s]", indent
, "");
551 if (vmsd
->subsections
!= NULL
) {
552 const VMStateDescription
**subsection
= vmsd
->subsections
;
555 fprintf(out_file
, ",\n%*s\"Subsections\": [\n", indent
, "");
557 while (*subsection
!= NULL
) {
559 fprintf(out_file
, ",\n");
561 dump_vmstate_vmss(out_file
, subsection
, indent
+ 2);
565 fprintf(out_file
, "\n%*s]", indent
, "");
567 fprintf(out_file
, "\n%*s}", indent
- 2, "");
570 static void dump_machine_type(FILE *out_file
)
574 mc
= MACHINE_GET_CLASS(current_machine
);
576 fprintf(out_file
, " \"vmschkmachine\": {\n");
577 fprintf(out_file
, " \"Name\": \"%s\"\n", mc
->name
);
578 fprintf(out_file
, " },\n");
581 void dump_vmstate_json_to_file(FILE *out_file
)
586 fprintf(out_file
, "{\n");
587 dump_machine_type(out_file
);
590 list
= object_class_get_list(TYPE_DEVICE
, true);
591 for (elt
= list
; elt
; elt
= elt
->next
) {
592 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
602 fprintf(out_file
, ",\n");
604 name
= object_class_get_name(OBJECT_CLASS(dc
));
605 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", name
);
607 fprintf(out_file
, "%*s\"Name\": \"%s\",\n", indent
, "", name
);
608 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
609 dc
->vmsd
->version_id
);
610 fprintf(out_file
, "%*s\"minimum_version_id\": %d,\n", indent
, "",
611 dc
->vmsd
->minimum_version_id
);
613 dump_vmstate_vmsd(out_file
, dc
->vmsd
, indent
, false);
615 fprintf(out_file
, "\n%*s}", indent
- 2, "");
618 fprintf(out_file
, "\n}\n");
622 static int calculate_new_instance_id(const char *idstr
)
627 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
628 if (strcmp(idstr
, se
->idstr
) == 0
629 && instance_id
<= se
->instance_id
) {
630 instance_id
= se
->instance_id
+ 1;
636 static int calculate_compat_instance_id(const char *idstr
)
641 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
646 if (strcmp(idstr
, se
->compat
->idstr
) == 0
647 && instance_id
<= se
->compat
->instance_id
) {
648 instance_id
= se
->compat
->instance_id
+ 1;
654 static inline MigrationPriority
save_state_priority(SaveStateEntry
*se
)
657 return se
->vmsd
->priority
;
659 return MIG_PRI_DEFAULT
;
662 static void savevm_state_handler_insert(SaveStateEntry
*nse
)
664 MigrationPriority priority
= save_state_priority(nse
);
667 assert(priority
<= MIG_PRI_MAX
);
669 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
670 if (save_state_priority(se
) < priority
) {
676 QTAILQ_INSERT_BEFORE(se
, nse
, entry
);
678 QTAILQ_INSERT_TAIL(&savevm_state
.handlers
, nse
, entry
);
682 /* TODO: Individual devices generally have very little idea about the rest
683 of the system, so instance_id should be removed/replaced.
684 Meanwhile pass -1 as instance_id if you do not already have a clearly
685 distinguishing id for all instances of your device class. */
686 int register_savevm_live(DeviceState
*dev
,
690 const SaveVMHandlers
*ops
,
695 se
= g_new0(SaveStateEntry
, 1);
696 se
->version_id
= version_id
;
697 se
->section_id
= savevm_state
.global_section_id
++;
701 /* if this is a live_savem then set is_ram */
702 if (ops
->save_setup
!= NULL
) {
707 char *id
= qdev_get_dev_path(dev
);
709 if (snprintf(se
->idstr
, sizeof(se
->idstr
), "%s/", id
) >=
711 error_report("Path too long for VMState (%s)", id
);
719 se
->compat
= g_new0(CompatEntry
, 1);
720 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
721 se
->compat
->instance_id
= instance_id
== -1 ?
722 calculate_compat_instance_id(idstr
) : instance_id
;
726 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
728 if (instance_id
== -1) {
729 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
731 se
->instance_id
= instance_id
;
733 assert(!se
->compat
|| se
->instance_id
== 0);
734 savevm_state_handler_insert(se
);
738 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
740 SaveStateEntry
*se
, *new_se
;
744 char *path
= qdev_get_dev_path(dev
);
746 pstrcpy(id
, sizeof(id
), path
);
747 pstrcat(id
, sizeof(id
), "/");
751 pstrcat(id
, sizeof(id
), idstr
);
753 QTAILQ_FOREACH_SAFE(se
, &savevm_state
.handlers
, entry
, new_se
) {
754 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
755 QTAILQ_REMOVE(&savevm_state
.handlers
, se
, entry
);
762 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
763 const VMStateDescription
*vmsd
,
764 void *opaque
, int alias_id
,
765 int required_for_version
,
770 /* If this triggers, alias support can be dropped for the vmsd. */
771 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
773 se
= g_new0(SaveStateEntry
, 1);
774 se
->version_id
= vmsd
->version_id
;
775 se
->section_id
= savevm_state
.global_section_id
++;
778 se
->alias_id
= alias_id
;
781 char *id
= qdev_get_dev_path(dev
);
783 if (snprintf(se
->idstr
, sizeof(se
->idstr
), "%s/", id
) >=
785 error_setg(errp
, "Path too long for VMState (%s)", id
);
793 se
->compat
= g_new0(CompatEntry
, 1);
794 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
795 se
->compat
->instance_id
= instance_id
== -1 ?
796 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
800 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
802 if (instance_id
== -1) {
803 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
805 se
->instance_id
= instance_id
;
807 assert(!se
->compat
|| se
->instance_id
== 0);
808 savevm_state_handler_insert(se
);
812 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
815 SaveStateEntry
*se
, *new_se
;
817 QTAILQ_FOREACH_SAFE(se
, &savevm_state
.handlers
, entry
, new_se
) {
818 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
819 QTAILQ_REMOVE(&savevm_state
.handlers
, se
, entry
);
826 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
)
828 trace_vmstate_load(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
829 if (!se
->vmsd
) { /* Old style */
830 return se
->ops
->load_state(f
, se
->opaque
, se
->load_version_id
);
832 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, se
->load_version_id
);
835 static void vmstate_save_old_style(QEMUFile
*f
, SaveStateEntry
*se
, QJSON
*vmdesc
)
837 int64_t old_offset
, size
;
839 old_offset
= qemu_ftell_fast(f
);
840 se
->ops
->save_state(f
, se
->opaque
);
841 size
= qemu_ftell_fast(f
) - old_offset
;
844 json_prop_int(vmdesc
, "size", size
);
845 json_start_array(vmdesc
, "fields");
846 json_start_object(vmdesc
, NULL
);
847 json_prop_str(vmdesc
, "name", "data");
848 json_prop_int(vmdesc
, "size", size
);
849 json_prop_str(vmdesc
, "type", "buffer");
850 json_end_object(vmdesc
);
851 json_end_array(vmdesc
);
855 static int vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
, QJSON
*vmdesc
)
857 trace_vmstate_save(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
859 vmstate_save_old_style(f
, se
, vmdesc
);
862 return vmstate_save_state(f
, se
->vmsd
, se
->opaque
, vmdesc
);
866 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
868 static void save_section_header(QEMUFile
*f
, SaveStateEntry
*se
,
869 uint8_t section_type
)
871 qemu_put_byte(f
, section_type
);
872 qemu_put_be32(f
, se
->section_id
);
874 if (section_type
== QEMU_VM_SECTION_FULL
||
875 section_type
== QEMU_VM_SECTION_START
) {
877 size_t len
= strlen(se
->idstr
);
878 qemu_put_byte(f
, len
);
879 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
881 qemu_put_be32(f
, se
->instance_id
);
882 qemu_put_be32(f
, se
->version_id
);
887 * Write a footer onto device sections that catches cases misformatted device
890 static void save_section_footer(QEMUFile
*f
, SaveStateEntry
*se
)
892 if (migrate_get_current()->send_section_footer
) {
893 qemu_put_byte(f
, QEMU_VM_SECTION_FOOTER
);
894 qemu_put_be32(f
, se
->section_id
);
899 * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the
900 * command and associated data.
902 * @f: File to send command on
903 * @command: Command type to send
904 * @len: Length of associated data
905 * @data: Data associated with command.
907 static void qemu_savevm_command_send(QEMUFile
*f
,
908 enum qemu_vm_cmd command
,
912 trace_savevm_command_send(command
, len
);
913 qemu_put_byte(f
, QEMU_VM_COMMAND
);
914 qemu_put_be16(f
, (uint16_t)command
);
915 qemu_put_be16(f
, len
);
916 qemu_put_buffer(f
, data
, len
);
920 void qemu_savevm_send_colo_enable(QEMUFile
*f
)
922 trace_savevm_send_colo_enable();
923 qemu_savevm_command_send(f
, MIG_CMD_ENABLE_COLO
, 0, NULL
);
926 void qemu_savevm_send_ping(QEMUFile
*f
, uint32_t value
)
930 trace_savevm_send_ping(value
);
931 buf
= cpu_to_be32(value
);
932 qemu_savevm_command_send(f
, MIG_CMD_PING
, sizeof(value
), (uint8_t *)&buf
);
935 void qemu_savevm_send_open_return_path(QEMUFile
*f
)
937 trace_savevm_send_open_return_path();
938 qemu_savevm_command_send(f
, MIG_CMD_OPEN_RETURN_PATH
, 0, NULL
);
941 /* We have a buffer of data to send; we don't want that all to be loaded
942 * by the command itself, so the command contains just the length of the
943 * extra buffer that we then send straight after it.
944 * TODO: Must be a better way to organise that
950 int qemu_savevm_send_packaged(QEMUFile
*f
, const uint8_t *buf
, size_t len
)
954 if (len
> MAX_VM_CMD_PACKAGED_SIZE
) {
955 error_report("%s: Unreasonably large packaged state: %zu",
960 tmp
= cpu_to_be32(len
);
962 trace_qemu_savevm_send_packaged();
963 qemu_savevm_command_send(f
, MIG_CMD_PACKAGED
, 4, (uint8_t *)&tmp
);
965 qemu_put_buffer(f
, buf
, len
);
970 /* Send prior to any postcopy transfer */
971 void qemu_savevm_send_postcopy_advise(QEMUFile
*f
)
973 if (migrate_postcopy_ram()) {
975 tmp
[0] = cpu_to_be64(ram_pagesize_summary());
976 tmp
[1] = cpu_to_be64(qemu_target_page_size());
978 trace_qemu_savevm_send_postcopy_advise();
979 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_ADVISE
,
982 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_ADVISE
, 0, NULL
);
986 /* Sent prior to starting the destination running in postcopy, discard pages
987 * that have already been sent but redirtied on the source.
988 * CMD_POSTCOPY_RAM_DISCARD consist of:
990 * byte Length of name field (not including 0)
991 * n x byte RAM block name
992 * byte 0 terminator (just for safety)
993 * n x Byte ranges within the named RAMBlock
994 * be64 Start of the range
997 * name: RAMBlock name that these entries are part of
998 * len: Number of page entries
999 * start_list: 'len' addresses
1000 * length_list: 'len' addresses
1003 void qemu_savevm_send_postcopy_ram_discard(QEMUFile
*f
, const char *name
,
1005 uint64_t *start_list
,
1006 uint64_t *length_list
)
1011 size_t name_len
= strlen(name
);
1013 trace_qemu_savevm_send_postcopy_ram_discard(name
, len
);
1014 assert(name_len
< 256);
1015 buf
= g_malloc0(1 + 1 + name_len
+ 1 + (8 + 8) * len
);
1016 buf
[0] = postcopy_ram_discard_version
;
1018 memcpy(buf
+ 2, name
, name_len
);
1019 tmplen
= 2 + name_len
;
1020 buf
[tmplen
++] = '\0';
1022 for (t
= 0; t
< len
; t
++) {
1023 stq_be_p(buf
+ tmplen
, start_list
[t
]);
1025 stq_be_p(buf
+ tmplen
, length_list
[t
]);
1028 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_RAM_DISCARD
, tmplen
, buf
);
1032 /* Get the destination into a state where it can receive postcopy data. */
1033 void qemu_savevm_send_postcopy_listen(QEMUFile
*f
)
1035 trace_savevm_send_postcopy_listen();
1036 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_LISTEN
, 0, NULL
);
1039 /* Kick the destination into running */
1040 void qemu_savevm_send_postcopy_run(QEMUFile
*f
)
1042 trace_savevm_send_postcopy_run();
1043 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_RUN
, 0, NULL
);
1046 void qemu_savevm_send_postcopy_resume(QEMUFile
*f
)
1048 trace_savevm_send_postcopy_resume();
1049 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_RESUME
, 0, NULL
);
1052 void qemu_savevm_send_recv_bitmap(QEMUFile
*f
, char *block_name
)
1057 trace_savevm_send_recv_bitmap(block_name
);
1059 buf
[0] = len
= strlen(block_name
);
1060 memcpy(buf
+ 1, block_name
, len
);
1062 qemu_savevm_command_send(f
, MIG_CMD_RECV_BITMAP
, len
+ 1, (uint8_t *)buf
);
1065 bool qemu_savevm_state_blocked(Error
**errp
)
1069 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1070 if (se
->vmsd
&& se
->vmsd
->unmigratable
) {
1071 error_setg(errp
, "State blocked by non-migratable device '%s'",
1079 void qemu_savevm_state_header(QEMUFile
*f
)
1081 trace_savevm_state_header();
1082 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1083 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1085 if (migrate_get_current()->send_configuration
) {
1086 qemu_put_byte(f
, QEMU_VM_CONFIGURATION
);
1087 vmstate_save_state(f
, &vmstate_configuration
, &savevm_state
, 0);
1091 void qemu_savevm_state_setup(QEMUFile
*f
)
1094 Error
*local_err
= NULL
;
1097 trace_savevm_state_setup();
1098 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1099 if (!se
->ops
|| !se
->ops
->save_setup
) {
1102 if (se
->ops
&& se
->ops
->is_active
) {
1103 if (!se
->ops
->is_active(se
->opaque
)) {
1107 save_section_header(f
, se
, QEMU_VM_SECTION_START
);
1109 ret
= se
->ops
->save_setup(f
, se
->opaque
);
1110 save_section_footer(f
, se
);
1112 qemu_file_set_error(f
, ret
);
1117 if (precopy_notify(PRECOPY_NOTIFY_SETUP
, &local_err
)) {
1118 error_report_err(local_err
);
1122 int qemu_savevm_state_resume_prepare(MigrationState
*s
)
1127 trace_savevm_state_resume_prepare();
1129 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1130 if (!se
->ops
|| !se
->ops
->resume_prepare
) {
1133 if (se
->ops
&& se
->ops
->is_active
) {
1134 if (!se
->ops
->is_active(se
->opaque
)) {
1138 ret
= se
->ops
->resume_prepare(s
, se
->opaque
);
1148 * this function has three return values:
1149 * negative: there was one error, and we have -errno.
1150 * 0 : We haven't finished, caller have to go again
1151 * 1 : We have finished, we can go to complete phase
1153 int qemu_savevm_state_iterate(QEMUFile
*f
, bool postcopy
)
1158 trace_savevm_state_iterate();
1159 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1160 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
1163 if (se
->ops
->is_active
&&
1164 !se
->ops
->is_active(se
->opaque
)) {
1167 if (se
->ops
->is_active_iterate
&&
1168 !se
->ops
->is_active_iterate(se
->opaque
)) {
1172 * In the postcopy phase, any device that doesn't know how to
1173 * do postcopy should have saved it's state in the _complete
1174 * call that's already run, it might get confused if we call
1175 * iterate afterwards.
1178 !(se
->ops
->has_postcopy
&& se
->ops
->has_postcopy(se
->opaque
))) {
1181 if (qemu_file_rate_limit(f
)) {
1184 trace_savevm_section_start(se
->idstr
, se
->section_id
);
1186 save_section_header(f
, se
, QEMU_VM_SECTION_PART
);
1188 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
1189 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
1190 save_section_footer(f
, se
);
1193 qemu_file_set_error(f
, ret
);
1196 /* Do not proceed to the next vmstate before this one reported
1197 completion of the current stage. This serializes the migration
1198 and reduces the probability that a faster changing state is
1199 synchronized over and over again. */
1206 static bool should_send_vmdesc(void)
1208 MachineState
*machine
= MACHINE(qdev_get_machine());
1209 bool in_postcopy
= migration_in_postcopy();
1210 return !machine
->suppress_vmdesc
&& !in_postcopy
;
1214 * Calls the save_live_complete_postcopy methods
1215 * causing the last few pages to be sent immediately and doing any associated
1217 * Note postcopy also calls qemu_savevm_state_complete_precopy to complete
1218 * all the other devices, but that happens at the point we switch to postcopy.
1220 void qemu_savevm_state_complete_postcopy(QEMUFile
*f
)
1225 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1226 if (!se
->ops
|| !se
->ops
->save_live_complete_postcopy
) {
1229 if (se
->ops
&& se
->ops
->is_active
) {
1230 if (!se
->ops
->is_active(se
->opaque
)) {
1234 trace_savevm_section_start(se
->idstr
, se
->section_id
);
1236 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1237 qemu_put_be32(f
, se
->section_id
);
1239 ret
= se
->ops
->save_live_complete_postcopy(f
, se
->opaque
);
1240 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
1241 save_section_footer(f
, se
);
1243 qemu_file_set_error(f
, ret
);
1248 qemu_put_byte(f
, QEMU_VM_EOF
);
1252 int qemu_savevm_state_complete_precopy(QEMUFile
*f
, bool iterable_only
,
1253 bool inactivate_disks
)
1259 bool in_postcopy
= migration_in_postcopy();
1260 Error
*local_err
= NULL
;
1262 if (precopy_notify(PRECOPY_NOTIFY_COMPLETE
, &local_err
)) {
1263 error_report_err(local_err
);
1266 trace_savevm_state_complete_precopy();
1268 cpu_synchronize_all_states();
1270 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1272 (in_postcopy
&& se
->ops
->has_postcopy
&&
1273 se
->ops
->has_postcopy(se
->opaque
)) ||
1274 (in_postcopy
&& !iterable_only
) ||
1275 !se
->ops
->save_live_complete_precopy
) {
1279 if (se
->ops
&& se
->ops
->is_active
) {
1280 if (!se
->ops
->is_active(se
->opaque
)) {
1284 trace_savevm_section_start(se
->idstr
, se
->section_id
);
1286 save_section_header(f
, se
, QEMU_VM_SECTION_END
);
1288 ret
= se
->ops
->save_live_complete_precopy(f
, se
->opaque
);
1289 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
1290 save_section_footer(f
, se
);
1292 qemu_file_set_error(f
, ret
);
1297 if (iterable_only
) {
1301 vmdesc
= qjson_new();
1302 json_prop_int(vmdesc
, "page_size", qemu_target_page_size());
1303 json_start_array(vmdesc
, "devices");
1304 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1306 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1309 if (se
->vmsd
&& !vmstate_save_needed(se
->vmsd
, se
->opaque
)) {
1310 trace_savevm_section_skip(se
->idstr
, se
->section_id
);
1314 trace_savevm_section_start(se
->idstr
, se
->section_id
);
1316 json_start_object(vmdesc
, NULL
);
1317 json_prop_str(vmdesc
, "name", se
->idstr
);
1318 json_prop_int(vmdesc
, "instance_id", se
->instance_id
);
1320 save_section_header(f
, se
, QEMU_VM_SECTION_FULL
);
1321 ret
= vmstate_save(f
, se
, vmdesc
);
1323 qemu_file_set_error(f
, ret
);
1326 trace_savevm_section_end(se
->idstr
, se
->section_id
, 0);
1327 save_section_footer(f
, se
);
1329 json_end_object(vmdesc
);
1332 if (inactivate_disks
) {
1333 /* Inactivate before sending QEMU_VM_EOF so that the
1334 * bdrv_invalidate_cache_all() on the other end won't fail. */
1335 ret
= bdrv_inactivate_all();
1337 error_report("%s: bdrv_inactivate_all() failed (%d)",
1339 qemu_file_set_error(f
, ret
);
1344 /* Postcopy stream will still be going */
1345 qemu_put_byte(f
, QEMU_VM_EOF
);
1348 json_end_array(vmdesc
);
1349 qjson_finish(vmdesc
);
1350 vmdesc_len
= strlen(qjson_get_str(vmdesc
));
1352 if (should_send_vmdesc()) {
1353 qemu_put_byte(f
, QEMU_VM_VMDESCRIPTION
);
1354 qemu_put_be32(f
, vmdesc_len
);
1355 qemu_put_buffer(f
, (uint8_t *)qjson_get_str(vmdesc
), vmdesc_len
);
1357 qjson_destroy(vmdesc
);
1363 /* Give an estimate of the amount left to be transferred,
1364 * the result is split into the amount for units that can and
1365 * for units that can't do postcopy.
1367 void qemu_savevm_state_pending(QEMUFile
*f
, uint64_t threshold_size
,
1368 uint64_t *res_precopy_only
,
1369 uint64_t *res_compatible
,
1370 uint64_t *res_postcopy_only
)
1374 *res_precopy_only
= 0;
1375 *res_compatible
= 0;
1376 *res_postcopy_only
= 0;
1379 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1380 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
1383 if (se
->ops
&& se
->ops
->is_active
) {
1384 if (!se
->ops
->is_active(se
->opaque
)) {
1388 se
->ops
->save_live_pending(f
, se
->opaque
, threshold_size
,
1389 res_precopy_only
, res_compatible
,
1394 void qemu_savevm_state_cleanup(void)
1397 Error
*local_err
= NULL
;
1399 if (precopy_notify(PRECOPY_NOTIFY_CLEANUP
, &local_err
)) {
1400 error_report_err(local_err
);
1403 trace_savevm_state_cleanup();
1404 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1405 if (se
->ops
&& se
->ops
->save_cleanup
) {
1406 se
->ops
->save_cleanup(se
->opaque
);
1411 static int qemu_savevm_state(QEMUFile
*f
, Error
**errp
)
1414 MigrationState
*ms
= migrate_get_current();
1415 MigrationStatus status
;
1417 if (migration_is_setup_or_active(ms
->state
) ||
1418 ms
->state
== MIGRATION_STATUS_CANCELLING
||
1419 ms
->state
== MIGRATION_STATUS_COLO
) {
1420 error_setg(errp
, QERR_MIGRATION_ACTIVE
);
1424 if (migrate_use_block()) {
1425 error_setg(errp
, "Block migration and snapshots are incompatible");
1430 ms
->to_dst_file
= f
;
1432 qemu_mutex_unlock_iothread();
1433 qemu_savevm_state_header(f
);
1434 qemu_savevm_state_setup(f
);
1435 qemu_mutex_lock_iothread();
1437 while (qemu_file_get_error(f
) == 0) {
1438 if (qemu_savevm_state_iterate(f
, false) > 0) {
1443 ret
= qemu_file_get_error(f
);
1445 qemu_savevm_state_complete_precopy(f
, false, false);
1446 ret
= qemu_file_get_error(f
);
1448 qemu_savevm_state_cleanup();
1450 error_setg_errno(errp
, -ret
, "Error while writing VM state");
1454 status
= MIGRATION_STATUS_FAILED
;
1456 status
= MIGRATION_STATUS_COMPLETED
;
1458 migrate_set_state(&ms
->state
, MIGRATION_STATUS_SETUP
, status
);
1460 /* f is outer parameter, it should not stay in global migration state after
1461 * this function finished */
1462 ms
->to_dst_file
= NULL
;
1467 void qemu_savevm_live_state(QEMUFile
*f
)
1469 /* save QEMU_VM_SECTION_END section */
1470 qemu_savevm_state_complete_precopy(f
, true, false);
1471 qemu_put_byte(f
, QEMU_VM_EOF
);
1474 int qemu_save_device_state(QEMUFile
*f
)
1478 if (!migration_in_colo_state()) {
1479 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1480 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1482 cpu_synchronize_all_states();
1484 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1490 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1493 if (se
->vmsd
&& !vmstate_save_needed(se
->vmsd
, se
->opaque
)) {
1497 save_section_header(f
, se
, QEMU_VM_SECTION_FULL
);
1499 ret
= vmstate_save(f
, se
, NULL
);
1504 save_section_footer(f
, se
);
1507 qemu_put_byte(f
, QEMU_VM_EOF
);
1509 return qemu_file_get_error(f
);
1512 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1516 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1517 if (!strcmp(se
->idstr
, idstr
) &&
1518 (instance_id
== se
->instance_id
||
1519 instance_id
== se
->alias_id
))
1521 /* Migrating from an older version? */
1522 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
1523 if (!strcmp(se
->compat
->idstr
, idstr
) &&
1524 (instance_id
== se
->compat
->instance_id
||
1525 instance_id
== se
->alias_id
))
1532 enum LoadVMExitCodes
{
1533 /* Allow a command to quit all layers of nested loadvm loops */
1537 /* ------ incoming postcopy messages ------ */
1538 /* 'advise' arrives before any transfers just to tell us that a postcopy
1539 * *might* happen - it might be skipped if precopy transferred everything
1542 static int loadvm_postcopy_handle_advise(MigrationIncomingState
*mis
,
1545 PostcopyState ps
= postcopy_state_set(POSTCOPY_INCOMING_ADVISE
);
1546 uint64_t remote_pagesize_summary
, local_pagesize_summary
, remote_tps
;
1547 Error
*local_err
= NULL
;
1549 trace_loadvm_postcopy_handle_advise();
1550 if (ps
!= POSTCOPY_INCOMING_NONE
) {
1551 error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps
);
1557 if (migrate_postcopy_ram()) {
1558 error_report("RAM postcopy is enabled but have 0 byte advise");
1563 if (!migrate_postcopy_ram()) {
1564 error_report("RAM postcopy is disabled but have 16 byte advise");
1569 error_report("CMD_POSTCOPY_ADVISE invalid length (%d)", len
);
1573 if (!postcopy_ram_supported_by_host(mis
)) {
1574 postcopy_state_set(POSTCOPY_INCOMING_NONE
);
1578 remote_pagesize_summary
= qemu_get_be64(mis
->from_src_file
);
1579 local_pagesize_summary
= ram_pagesize_summary();
1581 if (remote_pagesize_summary
!= local_pagesize_summary
) {
1583 * This detects two potential causes of mismatch:
1584 * a) A mismatch in host page sizes
1585 * Some combinations of mismatch are probably possible but it gets
1586 * a bit more complicated. In particular we need to place whole
1587 * host pages on the dest at once, and we need to ensure that we
1588 * handle dirtying to make sure we never end up sending part of
1589 * a hostpage on it's own.
1590 * b) The use of different huge page sizes on source/destination
1591 * a more fine grain test is performed during RAM block migration
1592 * but this test here causes a nice early clear failure, and
1593 * also fails when passed to an older qemu that doesn't
1596 error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64
1598 remote_pagesize_summary
, local_pagesize_summary
);
1602 remote_tps
= qemu_get_be64(mis
->from_src_file
);
1603 if (remote_tps
!= qemu_target_page_size()) {
1605 * Again, some differences could be dealt with, but for now keep it
1608 error_report("Postcopy needs matching target page sizes (s=%d d=%zd)",
1609 (int)remote_tps
, qemu_target_page_size());
1613 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_ADVISE
, &local_err
)) {
1614 error_report_err(local_err
);
1618 if (ram_postcopy_incoming_init(mis
)) {
1622 postcopy_state_set(POSTCOPY_INCOMING_ADVISE
);
1627 /* After postcopy we will be told to throw some pages away since they're
1628 * dirty and will have to be demand fetched. Must happen before CPU is
1630 * There can be 0..many of these messages, each encoding multiple pages.
1632 static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState
*mis
,
1637 PostcopyState ps
= postcopy_state_get();
1639 trace_loadvm_postcopy_ram_handle_discard();
1642 case POSTCOPY_INCOMING_ADVISE
:
1644 tmp
= postcopy_ram_prepare_discard(mis
);
1650 case POSTCOPY_INCOMING_DISCARD
:
1651 /* Expected state */
1655 error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)",
1659 /* We're expecting a
1661 * a RAM ID string (length byte, name, 0 term)
1662 * then at least 1 16 byte chunk
1664 if (len
< (1 + 1 + 1 + 1 + 2 * 8)) {
1665 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len
);
1669 tmp
= qemu_get_byte(mis
->from_src_file
);
1670 if (tmp
!= postcopy_ram_discard_version
) {
1671 error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp
);
1675 if (!qemu_get_counted_string(mis
->from_src_file
, ramid
)) {
1676 error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID");
1679 tmp
= qemu_get_byte(mis
->from_src_file
);
1681 error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp
);
1685 len
-= 3 + strlen(ramid
);
1687 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len
);
1690 trace_loadvm_postcopy_ram_handle_discard_header(ramid
, len
);
1692 uint64_t start_addr
, block_length
;
1693 start_addr
= qemu_get_be64(mis
->from_src_file
);
1694 block_length
= qemu_get_be64(mis
->from_src_file
);
1697 int ret
= ram_discard_range(ramid
, start_addr
, block_length
);
1702 trace_loadvm_postcopy_ram_handle_discard_end();
1708 * Triggered by a postcopy_listen command; this thread takes over reading
1709 * the input stream, leaving the main thread free to carry on loading the rest
1710 * of the device state (from RAM).
1711 * (TODO:This could do with being in a postcopy file - but there again it's
1712 * just another input loop, not that postcopy specific)
1714 static void *postcopy_ram_listen_thread(void *opaque
)
1716 MigrationIncomingState
*mis
= migration_incoming_get_current();
1717 QEMUFile
*f
= mis
->from_src_file
;
1720 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
1721 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
1722 qemu_sem_post(&mis
->listen_thread_sem
);
1723 trace_postcopy_ram_listen_thread_start();
1725 rcu_register_thread();
1727 * Because we're a thread and not a coroutine we can't yield
1728 * in qemu_file, and thus we must be blocking now.
1730 qemu_file_set_blocking(f
, true);
1731 load_res
= qemu_loadvm_state_main(f
, mis
);
1734 * This is tricky, but, mis->from_src_file can change after it
1735 * returns, when postcopy recovery happened. In the future, we may
1736 * want a wrapper for the QEMUFile handle.
1738 f
= mis
->from_src_file
;
1740 /* And non-blocking again so we don't block in any cleanup */
1741 qemu_file_set_blocking(f
, false);
1743 trace_postcopy_ram_listen_thread_exit();
1745 error_report("%s: loadvm failed: %d", __func__
, load_res
);
1746 qemu_file_set_error(f
, load_res
);
1747 migrate_set_state(&mis
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
1748 MIGRATION_STATUS_FAILED
);
1751 * This looks good, but it's possible that the device loading in the
1752 * main thread hasn't finished yet, and so we might not be in 'RUN'
1753 * state yet; wait for the end of the main thread.
1755 qemu_event_wait(&mis
->main_thread_load_event
);
1757 postcopy_ram_incoming_cleanup(mis
);
1761 * If something went wrong then we have a bad state so exit;
1762 * depending how far we got it might be possible at this point
1763 * to leave the guest running and fire MCEs for pages that never
1764 * arrived as a desperate recovery step.
1766 rcu_unregister_thread();
1770 migrate_set_state(&mis
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
1771 MIGRATION_STATUS_COMPLETED
);
1773 * If everything has worked fine, then the main thread has waited
1774 * for us to start, and we're the last use of the mis.
1775 * (If something broke then qemu will have to exit anyway since it's
1776 * got a bad migration state).
1778 migration_incoming_state_destroy();
1779 qemu_loadvm_state_cleanup();
1781 rcu_unregister_thread();
1782 mis
->have_listen_thread
= false;
1786 /* After this message we must be able to immediately receive postcopy data */
1787 static int loadvm_postcopy_handle_listen(MigrationIncomingState
*mis
)
1789 PostcopyState ps
= postcopy_state_set(POSTCOPY_INCOMING_LISTENING
);
1790 trace_loadvm_postcopy_handle_listen();
1791 Error
*local_err
= NULL
;
1793 if (ps
!= POSTCOPY_INCOMING_ADVISE
&& ps
!= POSTCOPY_INCOMING_DISCARD
) {
1794 error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps
);
1797 if (ps
== POSTCOPY_INCOMING_ADVISE
) {
1799 * A rare case, we entered listen without having to do any discards,
1800 * so do the setup that's normally done at the time of the 1st discard.
1802 if (migrate_postcopy_ram()) {
1803 postcopy_ram_prepare_discard(mis
);
1808 * Sensitise RAM - can now generate requests for blocks that don't exist
1809 * However, at this point the CPU shouldn't be running, and the IO
1810 * shouldn't be doing anything yet so don't actually expect requests
1812 if (migrate_postcopy_ram()) {
1813 if (postcopy_ram_enable_notify(mis
)) {
1814 postcopy_ram_incoming_cleanup(mis
);
1819 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN
, &local_err
)) {
1820 error_report_err(local_err
);
1824 if (mis
->have_listen_thread
) {
1825 error_report("CMD_POSTCOPY_RAM_LISTEN already has a listen thread");
1829 mis
->have_listen_thread
= true;
1830 /* Start up the listening thread and wait for it to signal ready */
1831 qemu_sem_init(&mis
->listen_thread_sem
, 0);
1832 qemu_thread_create(&mis
->listen_thread
, "postcopy/listen",
1833 postcopy_ram_listen_thread
, NULL
,
1834 QEMU_THREAD_DETACHED
);
1835 qemu_sem_wait(&mis
->listen_thread_sem
);
1836 qemu_sem_destroy(&mis
->listen_thread_sem
);
1846 static void loadvm_postcopy_handle_run_bh(void *opaque
)
1848 Error
*local_err
= NULL
;
1849 HandleRunBhData
*data
= opaque
;
1850 MigrationIncomingState
*mis
= migration_incoming_get_current();
1852 /* TODO we should move all of this lot into postcopy_ram.c or a shared code
1855 cpu_synchronize_all_post_init();
1857 qemu_announce_self(&mis
->announce_timer
, migrate_announce_params());
1859 /* Make sure all file formats flush their mutable metadata.
1860 * If we get an error here, just don't restart the VM yet. */
1861 bdrv_invalidate_cache_all(&local_err
);
1863 error_report_err(local_err
);
1868 trace_loadvm_postcopy_handle_run_cpu_sync();
1870 trace_loadvm_postcopy_handle_run_vmstart();
1872 dirty_bitmap_mig_before_vm_start();
1875 /* Hold onto your hats, starting the CPU */
1878 /* leave it paused and let management decide when to start the CPU */
1879 runstate_set(RUN_STATE_PAUSED
);
1882 qemu_bh_delete(data
->bh
);
1886 /* After all discards we can start running and asking for pages */
1887 static int loadvm_postcopy_handle_run(MigrationIncomingState
*mis
)
1889 PostcopyState ps
= postcopy_state_set(POSTCOPY_INCOMING_RUNNING
);
1890 HandleRunBhData
*data
;
1892 trace_loadvm_postcopy_handle_run();
1893 if (ps
!= POSTCOPY_INCOMING_LISTENING
) {
1894 error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps
);
1898 data
= g_new(HandleRunBhData
, 1);
1899 data
->bh
= qemu_bh_new(loadvm_postcopy_handle_run_bh
, data
);
1900 qemu_bh_schedule(data
->bh
);
1902 /* We need to finish reading the stream from the package
1903 * and also stop reading anything more from the stream that loaded the
1904 * package (since it's now being read by the listener thread).
1905 * LOADVM_QUIT will quit all the layers of nested loadvm loops.
1910 static int loadvm_postcopy_handle_resume(MigrationIncomingState
*mis
)
1912 if (mis
->state
!= MIGRATION_STATUS_POSTCOPY_RECOVER
) {
1913 error_report("%s: illegal resume received", __func__
);
1914 /* Don't fail the load, only for this. */
1919 * This means source VM is ready to resume the postcopy migration.
1920 * It's time to switch state and release the fault thread to
1921 * continue service page faults.
1923 migrate_set_state(&mis
->state
, MIGRATION_STATUS_POSTCOPY_RECOVER
,
1924 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
1925 qemu_sem_post(&mis
->postcopy_pause_sem_fault
);
1927 trace_loadvm_postcopy_handle_resume();
1929 /* Tell source that "we are ready" */
1930 migrate_send_rp_resume_ack(mis
, MIGRATION_RESUME_ACK_VALUE
);
1936 * Immediately following this command is a blob of data containing an embedded
1937 * chunk of migration stream; read it and load it.
1939 * @mis: Incoming state
1940 * @length: Length of packaged data to read
1942 * Returns: Negative values on error
1945 static int loadvm_handle_cmd_packaged(MigrationIncomingState
*mis
)
1949 QIOChannelBuffer
*bioc
;
1951 length
= qemu_get_be32(mis
->from_src_file
);
1952 trace_loadvm_handle_cmd_packaged(length
);
1954 if (length
> MAX_VM_CMD_PACKAGED_SIZE
) {
1955 error_report("Unreasonably large packaged state: %zu", length
);
1959 bioc
= qio_channel_buffer_new(length
);
1960 qio_channel_set_name(QIO_CHANNEL(bioc
), "migration-loadvm-buffer");
1961 ret
= qemu_get_buffer(mis
->from_src_file
,
1964 if (ret
!= length
) {
1965 object_unref(OBJECT(bioc
));
1966 error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu",
1968 return (ret
< 0) ? ret
: -EAGAIN
;
1970 bioc
->usage
+= length
;
1971 trace_loadvm_handle_cmd_packaged_received(ret
);
1973 QEMUFile
*packf
= qemu_fopen_channel_input(QIO_CHANNEL(bioc
));
1975 ret
= qemu_loadvm_state_main(packf
, mis
);
1976 trace_loadvm_handle_cmd_packaged_main(ret
);
1978 object_unref(OBJECT(bioc
));
1984 * Handle request that source requests for recved_bitmap on
1985 * destination. Payload format:
1987 * len (1 byte) + ramblock_name (<255 bytes)
1989 static int loadvm_handle_recv_bitmap(MigrationIncomingState
*mis
,
1992 QEMUFile
*file
= mis
->from_src_file
;
1994 char block_name
[256];
1997 cnt
= qemu_get_counted_string(file
, block_name
);
1999 error_report("%s: failed to read block name", __func__
);
2003 /* Validate before using the data */
2004 if (qemu_file_get_error(file
)) {
2005 return qemu_file_get_error(file
);
2008 if (len
!= cnt
+ 1) {
2009 error_report("%s: invalid payload length (%d)", __func__
, len
);
2013 rb
= qemu_ram_block_by_name(block_name
);
2015 error_report("%s: block '%s' not found", __func__
, block_name
);
2019 migrate_send_rp_recv_bitmap(mis
, block_name
);
2021 trace_loadvm_handle_recv_bitmap(block_name
);
2026 static int loadvm_process_enable_colo(MigrationIncomingState
*mis
)
2028 migration_incoming_enable_colo();
2029 return colo_init_ram_cache();
2033 * Process an incoming 'QEMU_VM_COMMAND'
2034 * 0 just a normal return
2035 * LOADVM_QUIT All good, but exit the loop
2038 static int loadvm_process_command(QEMUFile
*f
)
2040 MigrationIncomingState
*mis
= migration_incoming_get_current();
2045 cmd
= qemu_get_be16(f
);
2046 len
= qemu_get_be16(f
);
2048 /* Check validity before continue processing of cmds */
2049 if (qemu_file_get_error(f
)) {
2050 return qemu_file_get_error(f
);
2053 trace_loadvm_process_command(cmd
, len
);
2054 if (cmd
>= MIG_CMD_MAX
|| cmd
== MIG_CMD_INVALID
) {
2055 error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd
, len
);
2059 if (mig_cmd_args
[cmd
].len
!= -1 && mig_cmd_args
[cmd
].len
!= len
) {
2060 error_report("%s received with bad length - expecting %zu, got %d",
2061 mig_cmd_args
[cmd
].name
,
2062 (size_t)mig_cmd_args
[cmd
].len
, len
);
2067 case MIG_CMD_OPEN_RETURN_PATH
:
2068 if (mis
->to_src_file
) {
2069 error_report("CMD_OPEN_RETURN_PATH called when RP already open");
2070 /* Not really a problem, so don't give up */
2073 mis
->to_src_file
= qemu_file_get_return_path(f
);
2074 if (!mis
->to_src_file
) {
2075 error_report("CMD_OPEN_RETURN_PATH failed");
2081 tmp32
= qemu_get_be32(f
);
2082 trace_loadvm_process_command_ping(tmp32
);
2083 if (!mis
->to_src_file
) {
2084 error_report("CMD_PING (0x%x) received with no return path",
2088 migrate_send_rp_pong(mis
, tmp32
);
2091 case MIG_CMD_PACKAGED
:
2092 return loadvm_handle_cmd_packaged(mis
);
2094 case MIG_CMD_POSTCOPY_ADVISE
:
2095 return loadvm_postcopy_handle_advise(mis
, len
);
2097 case MIG_CMD_POSTCOPY_LISTEN
:
2098 return loadvm_postcopy_handle_listen(mis
);
2100 case MIG_CMD_POSTCOPY_RUN
:
2101 return loadvm_postcopy_handle_run(mis
);
2103 case MIG_CMD_POSTCOPY_RAM_DISCARD
:
2104 return loadvm_postcopy_ram_handle_discard(mis
, len
);
2106 case MIG_CMD_POSTCOPY_RESUME
:
2107 return loadvm_postcopy_handle_resume(mis
);
2109 case MIG_CMD_RECV_BITMAP
:
2110 return loadvm_handle_recv_bitmap(mis
, len
);
2112 case MIG_CMD_ENABLE_COLO
:
2113 return loadvm_process_enable_colo(mis
);
2120 * Read a footer off the wire and check that it matches the expected section
2122 * Returns: true if the footer was good
2123 * false if there is a problem (and calls error_report to say why)
2125 static bool check_section_footer(QEMUFile
*f
, SaveStateEntry
*se
)
2129 uint32_t read_section_id
;
2131 if (!migrate_get_current()->send_section_footer
) {
2132 /* No footer to check */
2136 read_mark
= qemu_get_byte(f
);
2138 ret
= qemu_file_get_error(f
);
2140 error_report("%s: Read section footer failed: %d",
2145 if (read_mark
!= QEMU_VM_SECTION_FOOTER
) {
2146 error_report("Missing section footer for %s", se
->idstr
);
2150 read_section_id
= qemu_get_be32(f
);
2151 if (read_section_id
!= se
->load_section_id
) {
2152 error_report("Mismatched section id in footer for %s -"
2153 " read 0x%x expected 0x%x",
2154 se
->idstr
, read_section_id
, se
->load_section_id
);
2163 qemu_loadvm_section_start_full(QEMUFile
*f
, MigrationIncomingState
*mis
)
2165 uint32_t instance_id
, version_id
, section_id
;
2170 /* Read section start */
2171 section_id
= qemu_get_be32(f
);
2172 if (!qemu_get_counted_string(f
, idstr
)) {
2173 error_report("Unable to read ID string for section %u",
2177 instance_id
= qemu_get_be32(f
);
2178 version_id
= qemu_get_be32(f
);
2180 ret
= qemu_file_get_error(f
);
2182 error_report("%s: Failed to read instance/version ID: %d",
2187 trace_qemu_loadvm_state_section_startfull(section_id
, idstr
,
2188 instance_id
, version_id
);
2189 /* Find savevm section */
2190 se
= find_se(idstr
, instance_id
);
2192 error_report("Unknown savevm section or instance '%s' %d. "
2193 "Make sure that your current VM setup matches your "
2194 "saved VM setup, including any hotplugged devices",
2195 idstr
, instance_id
);
2199 /* Validate version */
2200 if (version_id
> se
->version_id
) {
2201 error_report("savevm: unsupported version %d for '%s' v%d",
2202 version_id
, idstr
, se
->version_id
);
2205 se
->load_version_id
= version_id
;
2206 se
->load_section_id
= section_id
;
2208 /* Validate if it is a device's state */
2209 if (xen_enabled() && se
->is_ram
) {
2210 error_report("loadvm: %s RAM loading not allowed on Xen", idstr
);
2214 ret
= vmstate_load(f
, se
);
2216 error_report("error while loading state for instance 0x%x of"
2217 " device '%s'", instance_id
, idstr
);
2220 if (!check_section_footer(f
, se
)) {
2228 qemu_loadvm_section_part_end(QEMUFile
*f
, MigrationIncomingState
*mis
)
2230 uint32_t section_id
;
2234 section_id
= qemu_get_be32(f
);
2236 ret
= qemu_file_get_error(f
);
2238 error_report("%s: Failed to read section ID: %d",
2243 trace_qemu_loadvm_state_section_partend(section_id
);
2244 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
2245 if (se
->load_section_id
== section_id
) {
2250 error_report("Unknown savevm section %d", section_id
);
2254 ret
= vmstate_load(f
, se
);
2256 error_report("error while loading state section id %d(%s)",
2257 section_id
, se
->idstr
);
2260 if (!check_section_footer(f
, se
)) {
2267 static int qemu_loadvm_state_header(QEMUFile
*f
)
2272 v
= qemu_get_be32(f
);
2273 if (v
!= QEMU_VM_FILE_MAGIC
) {
2274 error_report("Not a migration stream");
2278 v
= qemu_get_be32(f
);
2279 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
2280 error_report("SaveVM v2 format is obsolete and don't work anymore");
2283 if (v
!= QEMU_VM_FILE_VERSION
) {
2284 error_report("Unsupported migration stream version");
2288 if (migrate_get_current()->send_configuration
) {
2289 if (qemu_get_byte(f
) != QEMU_VM_CONFIGURATION
) {
2290 error_report("Configuration section missing");
2291 qemu_loadvm_state_cleanup();
2294 ret
= vmstate_load_state(f
, &vmstate_configuration
, &savevm_state
, 0);
2297 qemu_loadvm_state_cleanup();
2304 static int qemu_loadvm_state_setup(QEMUFile
*f
)
2309 trace_loadvm_state_setup();
2310 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
2311 if (!se
->ops
|| !se
->ops
->load_setup
) {
2314 if (se
->ops
&& se
->ops
->is_active
) {
2315 if (!se
->ops
->is_active(se
->opaque
)) {
2320 ret
= se
->ops
->load_setup(f
, se
->opaque
);
2322 qemu_file_set_error(f
, ret
);
2323 error_report("Load state of device %s failed", se
->idstr
);
2330 void qemu_loadvm_state_cleanup(void)
2334 trace_loadvm_state_cleanup();
2335 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
2336 if (se
->ops
&& se
->ops
->load_cleanup
) {
2337 se
->ops
->load_cleanup(se
->opaque
);
2342 /* Return true if we should continue the migration, or false. */
2343 static bool postcopy_pause_incoming(MigrationIncomingState
*mis
)
2345 trace_postcopy_pause_incoming();
2347 /* Clear the triggered bit to allow one recovery */
2348 mis
->postcopy_recover_triggered
= false;
2350 assert(mis
->from_src_file
);
2351 qemu_file_shutdown(mis
->from_src_file
);
2352 qemu_fclose(mis
->from_src_file
);
2353 mis
->from_src_file
= NULL
;
2355 assert(mis
->to_src_file
);
2356 qemu_file_shutdown(mis
->to_src_file
);
2357 qemu_mutex_lock(&mis
->rp_mutex
);
2358 qemu_fclose(mis
->to_src_file
);
2359 mis
->to_src_file
= NULL
;
2360 qemu_mutex_unlock(&mis
->rp_mutex
);
2362 migrate_set_state(&mis
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
2363 MIGRATION_STATUS_POSTCOPY_PAUSED
);
2365 /* Notify the fault thread for the invalidated file handle */
2366 postcopy_fault_thread_notify(mis
);
2368 error_report("Detected IO failure for postcopy. "
2369 "Migration paused.");
2371 while (mis
->state
== MIGRATION_STATUS_POSTCOPY_PAUSED
) {
2372 qemu_sem_wait(&mis
->postcopy_pause_sem_dst
);
2375 trace_postcopy_pause_incoming_continued();
2380 int qemu_loadvm_state_main(QEMUFile
*f
, MigrationIncomingState
*mis
)
2382 uint8_t section_type
;
2387 section_type
= qemu_get_byte(f
);
2389 if (qemu_file_get_error(f
)) {
2390 ret
= qemu_file_get_error(f
);
2394 trace_qemu_loadvm_state_section(section_type
);
2395 switch (section_type
) {
2396 case QEMU_VM_SECTION_START
:
2397 case QEMU_VM_SECTION_FULL
:
2398 ret
= qemu_loadvm_section_start_full(f
, mis
);
2403 case QEMU_VM_SECTION_PART
:
2404 case QEMU_VM_SECTION_END
:
2405 ret
= qemu_loadvm_section_part_end(f
, mis
);
2410 case QEMU_VM_COMMAND
:
2411 ret
= loadvm_process_command(f
);
2412 trace_qemu_loadvm_state_section_command(ret
);
2413 if ((ret
< 0) || (ret
& LOADVM_QUIT
)) {
2418 /* This is the end of migration */
2421 error_report("Unknown savevm section type %d", section_type
);
2429 qemu_file_set_error(f
, ret
);
2432 * If we are during an active postcopy, then we pause instead
2433 * of bail out to at least keep the VM's dirty data. Note
2434 * that POSTCOPY_INCOMING_LISTENING stage is still not enough,
2435 * during which we're still receiving device states and we
2436 * still haven't yet started the VM on destination.
2438 if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING
&&
2439 postcopy_pause_incoming(mis
)) {
2440 /* Reset f to point to the newly created channel */
2441 f
= mis
->from_src_file
;
2448 int qemu_loadvm_state(QEMUFile
*f
)
2450 MigrationIncomingState
*mis
= migration_incoming_get_current();
2451 Error
*local_err
= NULL
;
2454 if (qemu_savevm_state_blocked(&local_err
)) {
2455 error_report_err(local_err
);
2459 ret
= qemu_loadvm_state_header(f
);
2464 if (qemu_loadvm_state_setup(f
) != 0) {
2468 cpu_synchronize_all_pre_loadvm();
2470 ret
= qemu_loadvm_state_main(f
, mis
);
2471 qemu_event_set(&mis
->main_thread_load_event
);
2473 trace_qemu_loadvm_state_post_main(ret
);
2475 if (mis
->have_listen_thread
) {
2476 /* Listen thread still going, can't clean up yet */
2481 ret
= qemu_file_get_error(f
);
2485 * Try to read in the VMDESC section as well, so that dumping tools that
2486 * intercept our migration stream have the chance to see it.
2489 /* We've got to be careful; if we don't read the data and just shut the fd
2490 * then the sender can error if we close while it's still sending.
2491 * We also mustn't read data that isn't there; some transports (RDMA)
2492 * will stall waiting for that data when the source has already closed.
2494 if (ret
== 0 && should_send_vmdesc()) {
2497 uint8_t section_type
= qemu_get_byte(f
);
2499 if (section_type
!= QEMU_VM_VMDESCRIPTION
) {
2500 error_report("Expected vmdescription section, but got %d",
2503 * It doesn't seem worth failing at this point since
2504 * we apparently have an otherwise valid VM state
2507 buf
= g_malloc(0x1000);
2508 size
= qemu_get_be32(f
);
2511 uint32_t read_chunk
= MIN(size
, 0x1000);
2512 qemu_get_buffer(f
, buf
, read_chunk
);
2519 qemu_loadvm_state_cleanup();
2520 cpu_synchronize_all_post_init();
2525 int qemu_load_device_state(QEMUFile
*f
)
2527 MigrationIncomingState
*mis
= migration_incoming_get_current();
2530 /* Load QEMU_VM_SECTION_FULL section */
2531 ret
= qemu_loadvm_state_main(f
, mis
);
2533 error_report("Failed to load device state: %d", ret
);
2537 cpu_synchronize_all_post_init();
2541 int save_snapshot(const char *name
, Error
**errp
)
2543 BlockDriverState
*bs
, *bs1
;
2544 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
2547 int saved_vm_running
;
2548 uint64_t vm_state_size
;
2551 AioContext
*aio_context
;
2553 if (migration_is_blocked(errp
)) {
2557 if (!replay_can_snapshot()) {
2558 error_setg(errp
, "Record/replay does not allow making snapshot "
2559 "right now. Try once more later.");
2563 if (!bdrv_all_can_snapshot(&bs
)) {
2564 error_setg(errp
, "Device '%s' is writable but does not support "
2565 "snapshots", bdrv_get_device_name(bs
));
2569 /* Delete old snapshots of the same name */
2571 ret
= bdrv_all_delete_snapshot(name
, &bs1
, errp
);
2573 error_prepend(errp
, "Error while deleting snapshot on device "
2574 "'%s': ", bdrv_get_device_name(bs1
));
2579 bs
= bdrv_all_find_vmstate_bs();
2581 error_setg(errp
, "No block device can accept snapshots");
2584 aio_context
= bdrv_get_aio_context(bs
);
2586 saved_vm_running
= runstate_is_running();
2588 ret
= global_state_store();
2590 error_setg(errp
, "Error saving global state");
2593 vm_stop(RUN_STATE_SAVE_VM
);
2595 bdrv_drain_all_begin();
2597 aio_context_acquire(aio_context
);
2599 memset(sn
, 0, sizeof(*sn
));
2601 /* fill auxiliary fields */
2602 qemu_gettimeofday(&tv
);
2603 sn
->date_sec
= tv
.tv_sec
;
2604 sn
->date_nsec
= tv
.tv_usec
* 1000;
2605 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2608 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
2610 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
2611 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
2613 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
2616 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2617 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
2618 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
2621 /* save the VM state */
2622 f
= qemu_fopen_bdrv(bs
, 1);
2624 error_setg(errp
, "Could not open VM state file");
2627 ret
= qemu_savevm_state(f
, errp
);
2628 vm_state_size
= qemu_ftell(f
);
2634 /* The bdrv_all_create_snapshot() call that follows acquires the AioContext
2635 * for itself. BDRV_POLL_WHILE() does not support nested locking because
2636 * it only releases the lock once. Therefore synchronous I/O will deadlock
2637 * unless we release the AioContext before bdrv_all_create_snapshot().
2639 aio_context_release(aio_context
);
2642 ret
= bdrv_all_create_snapshot(sn
, bs
, vm_state_size
, &bs
);
2644 error_setg(errp
, "Error while creating snapshot on '%s'",
2645 bdrv_get_device_name(bs
));
2653 aio_context_release(aio_context
);
2656 bdrv_drain_all_end();
2658 if (saved_vm_running
) {
2664 void qmp_xen_save_devices_state(const char *filename
, bool has_live
, bool live
,
2668 QIOChannelFile
*ioc
;
2669 int saved_vm_running
;
2673 /* live default to true so old version of Xen tool stack can have a
2674 * successfull live migration */
2678 saved_vm_running
= runstate_is_running();
2679 vm_stop(RUN_STATE_SAVE_VM
);
2680 global_state_store_running();
2682 ioc
= qio_channel_file_new_path(filename
, O_WRONLY
| O_CREAT
, 0660, errp
);
2686 qio_channel_set_name(QIO_CHANNEL(ioc
), "migration-xen-save-state");
2687 f
= qemu_fopen_channel_output(QIO_CHANNEL(ioc
));
2688 object_unref(OBJECT(ioc
));
2689 ret
= qemu_save_device_state(f
);
2690 if (ret
< 0 || qemu_fclose(f
) < 0) {
2691 error_setg(errp
, QERR_IO_ERROR
);
2693 /* libxl calls the QMP command "stop" before calling
2694 * "xen-save-devices-state" and in case of migration failure, libxl
2695 * would call "cont".
2696 * So call bdrv_inactivate_all (release locks) here to let the other
2697 * side of the migration take controle of the images.
2699 if (live
&& !saved_vm_running
) {
2700 ret
= bdrv_inactivate_all();
2702 error_setg(errp
, "%s: bdrv_inactivate_all() failed (%d)",
2709 if (saved_vm_running
) {
2714 void qmp_xen_load_devices_state(const char *filename
, Error
**errp
)
2717 QIOChannelFile
*ioc
;
2720 /* Guest must be paused before loading the device state; the RAM state
2721 * will already have been loaded by xc
2723 if (runstate_is_running()) {
2724 error_setg(errp
, "Cannot update device state while vm is running");
2727 vm_stop(RUN_STATE_RESTORE_VM
);
2729 ioc
= qio_channel_file_new_path(filename
, O_RDONLY
| O_BINARY
, 0, errp
);
2733 qio_channel_set_name(QIO_CHANNEL(ioc
), "migration-xen-load-state");
2734 f
= qemu_fopen_channel_input(QIO_CHANNEL(ioc
));
2735 object_unref(OBJECT(ioc
));
2737 ret
= qemu_loadvm_state(f
);
2740 error_setg(errp
, QERR_IO_ERROR
);
2742 migration_incoming_state_destroy();
2745 int load_snapshot(const char *name
, Error
**errp
)
2747 BlockDriverState
*bs
, *bs_vm_state
;
2748 QEMUSnapshotInfo sn
;
2751 AioContext
*aio_context
;
2752 MigrationIncomingState
*mis
= migration_incoming_get_current();
2754 if (!replay_can_snapshot()) {
2755 error_setg(errp
, "Record/replay does not allow loading snapshot "
2756 "right now. Try once more later.");
2760 if (!bdrv_all_can_snapshot(&bs
)) {
2762 "Device '%s' is writable but does not support snapshots",
2763 bdrv_get_device_name(bs
));
2766 ret
= bdrv_all_find_snapshot(name
, &bs
);
2769 "Device '%s' does not have the requested snapshot '%s'",
2770 bdrv_get_device_name(bs
), name
);
2774 bs_vm_state
= bdrv_all_find_vmstate_bs();
2776 error_setg(errp
, "No block device supports snapshots");
2779 aio_context
= bdrv_get_aio_context(bs_vm_state
);
2781 /* Don't even try to load empty VM states */
2782 aio_context_acquire(aio_context
);
2783 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
2784 aio_context_release(aio_context
);
2787 } else if (sn
.vm_state_size
== 0) {
2788 error_setg(errp
, "This is a disk-only snapshot. Revert to it "
2789 " offline using qemu-img");
2793 /* Flush all IO requests so they don't interfere with the new state. */
2794 bdrv_drain_all_begin();
2796 ret
= bdrv_all_goto_snapshot(name
, &bs
, errp
);
2798 error_prepend(errp
, "Could not load snapshot '%s' on '%s': ",
2799 name
, bdrv_get_device_name(bs
));
2803 /* restore the VM state */
2804 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
2806 error_setg(errp
, "Could not open VM state file");
2811 qemu_system_reset(SHUTDOWN_CAUSE_NONE
);
2812 mis
->from_src_file
= f
;
2814 aio_context_acquire(aio_context
);
2815 ret
= qemu_loadvm_state(f
);
2816 migration_incoming_state_destroy();
2817 aio_context_release(aio_context
);
2819 bdrv_drain_all_end();
2822 error_setg(errp
, "Error %d while loading VM state", ret
);
2829 bdrv_drain_all_end();
2833 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2835 qemu_ram_set_idstr(mr
->ram_block
,
2836 memory_region_name(mr
), dev
);
2837 qemu_ram_set_migratable(mr
->ram_block
);
2840 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2842 qemu_ram_unset_idstr(mr
->ram_block
);
2843 qemu_ram_unset_migratable(mr
->ram_block
);
2846 void vmstate_register_ram_global(MemoryRegion
*mr
)
2848 vmstate_register_ram(mr
, NULL
);
2851 bool vmstate_check_only_migratable(const VMStateDescription
*vmsd
)
2853 /* check needed if --only-migratable is specified */
2854 if (!only_migratable
) {
2858 return !(vmsd
&& vmsd
->unmigratable
);