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"
32 #include "migration.h"
33 #include "migration/snapshot.h"
34 #include "migration-stats.h"
35 #include "migration/vmstate.h"
36 #include "migration/misc.h"
37 #include "migration/register.h"
38 #include "migration/global_state.h"
39 #include "migration/channel-block.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/clone-visitor.h"
47 #include "qapi/qapi-builtin-visit.h"
48 #include "qapi/qmp/qerror.h"
49 #include "qemu/error-report.h"
50 #include "sysemu/cpus.h"
51 #include "exec/memory.h"
52 #include "exec/target_page.h"
56 #include "qemu/main-loop.h"
57 #include "block/snapshot.h"
58 #include "qemu/cutils.h"
59 #include "io/channel-buffer.h"
60 #include "io/channel-file.h"
61 #include "sysemu/replay.h"
62 #include "sysemu/runstate.h"
63 #include "sysemu/sysemu.h"
64 #include "sysemu/xen.h"
65 #include "migration/colo.h"
66 #include "qemu/bitmap.h"
67 #include "net/announce.h"
68 #include "qemu/yank.h"
69 #include "yank_functions.h"
70 #include "sysemu/qtest.h"
73 const unsigned int postcopy_ram_discard_version
;
75 /* Subcommands for QEMU_VM_COMMAND */
77 MIG_CMD_INVALID
= 0, /* Must be 0 */
78 MIG_CMD_OPEN_RETURN_PATH
, /* Tell the dest to open the Return path */
79 MIG_CMD_PING
, /* Request a PONG on the RP */
81 MIG_CMD_POSTCOPY_ADVISE
, /* Prior to any page transfers, just
82 warn we might want to do PC */
83 MIG_CMD_POSTCOPY_LISTEN
, /* Start listening for incoming
84 pages as it's running. */
85 MIG_CMD_POSTCOPY_RUN
, /* Start execution */
87 MIG_CMD_POSTCOPY_RAM_DISCARD
, /* A list of pages to discard that
88 were previously sent during
89 precopy but are dirty. */
90 MIG_CMD_PACKAGED
, /* Send a wrapped stream within this stream */
91 MIG_CMD_ENABLE_COLO
, /* Enable COLO */
92 MIG_CMD_POSTCOPY_RESUME
, /* resume postcopy on dest */
93 MIG_CMD_RECV_BITMAP
, /* Request for recved bitmap on dst */
97 #define MAX_VM_CMD_PACKAGED_SIZE UINT32_MAX
98 static struct mig_cmd_args
{
99 ssize_t len
; /* -1 = variable */
102 [MIG_CMD_INVALID
] = { .len
= -1, .name
= "INVALID" },
103 [MIG_CMD_OPEN_RETURN_PATH
] = { .len
= 0, .name
= "OPEN_RETURN_PATH" },
104 [MIG_CMD_PING
] = { .len
= sizeof(uint32_t), .name
= "PING" },
105 [MIG_CMD_POSTCOPY_ADVISE
] = { .len
= -1, .name
= "POSTCOPY_ADVISE" },
106 [MIG_CMD_POSTCOPY_LISTEN
] = { .len
= 0, .name
= "POSTCOPY_LISTEN" },
107 [MIG_CMD_POSTCOPY_RUN
] = { .len
= 0, .name
= "POSTCOPY_RUN" },
108 [MIG_CMD_POSTCOPY_RAM_DISCARD
] = {
109 .len
= -1, .name
= "POSTCOPY_RAM_DISCARD" },
110 [MIG_CMD_POSTCOPY_RESUME
] = { .len
= 0, .name
= "POSTCOPY_RESUME" },
111 [MIG_CMD_PACKAGED
] = { .len
= 4, .name
= "PACKAGED" },
112 [MIG_CMD_RECV_BITMAP
] = { .len
= -1, .name
= "RECV_BITMAP" },
113 [MIG_CMD_MAX
] = { .len
= -1, .name
= "MAX" },
116 /* Note for MIG_CMD_POSTCOPY_ADVISE:
117 * The format of arguments is depending on postcopy mode:
118 * - postcopy RAM only
119 * uint64_t host page size
120 * uint64_t taget page size
122 * - postcopy RAM and postcopy dirty bitmaps
123 * format is the same as for postcopy RAM only
125 * - postcopy dirty bitmaps only
126 * Nothing. Command length field is 0.
128 * Be careful: adding a new postcopy entity with some other parameters should
129 * not break format self-description ability. Good way is to introduce some
130 * generic extendable format with an exception for two old entities.
133 /***********************************************************/
134 /* savevm/loadvm support */
136 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
139 return qemu_file_new_output(QIO_CHANNEL(qio_channel_block_new(bs
)));
141 return qemu_file_new_input(QIO_CHANNEL(qio_channel_block_new(bs
)));
146 /* QEMUFile timer support.
147 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
150 void timer_put(QEMUFile
*f
, QEMUTimer
*ts
)
152 uint64_t expire_time
;
154 expire_time
= timer_expire_time_ns(ts
);
155 qemu_put_be64(f
, expire_time
);
158 void timer_get(QEMUFile
*f
, QEMUTimer
*ts
)
160 uint64_t expire_time
;
162 expire_time
= qemu_get_be64(f
);
163 if (expire_time
!= -1) {
164 timer_mod_ns(ts
, expire_time
);
171 /* VMState timer support.
172 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
175 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
,
176 const VMStateField
*field
)
183 static int put_timer(QEMUFile
*f
, void *pv
, size_t size
,
184 const VMStateField
*field
, JSONWriter
*vmdesc
)
192 const VMStateInfo vmstate_info_timer
= {
199 typedef struct CompatEntry
{
204 typedef struct SaveStateEntry
{
205 QTAILQ_ENTRY(SaveStateEntry
) entry
;
207 uint32_t instance_id
;
210 /* version id read from the stream */
213 /* section id read from the stream */
215 const SaveVMHandlers
*ops
;
216 const VMStateDescription
*vmsd
;
222 typedef struct SaveState
{
223 QTAILQ_HEAD(, SaveStateEntry
) handlers
;
224 SaveStateEntry
*handler_pri_head
[MIG_PRI_MAX
+ 1];
225 int global_section_id
;
228 uint32_t target_page_bits
;
230 MigrationCapability
*capabilities
;
234 static SaveState savevm_state
= {
235 .handlers
= QTAILQ_HEAD_INITIALIZER(savevm_state
.handlers
),
236 .handler_pri_head
= { [MIG_PRI_DEFAULT
... MIG_PRI_MAX
] = NULL
},
237 .global_section_id
= 0,
240 static bool should_validate_capability(int capability
)
242 assert(capability
>= 0 && capability
< MIGRATION_CAPABILITY__MAX
);
243 /* Validate only new capabilities to keep compatibility. */
244 switch (capability
) {
245 case MIGRATION_CAPABILITY_X_IGNORE_SHARED
:
252 static uint32_t get_validatable_capabilities_count(void)
254 MigrationState
*s
= migrate_get_current();
257 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
258 if (should_validate_capability(i
) && s
->capabilities
[i
]) {
265 static int configuration_pre_save(void *opaque
)
267 SaveState
*state
= opaque
;
268 const char *current_name
= MACHINE_GET_CLASS(current_machine
)->name
;
269 MigrationState
*s
= migrate_get_current();
272 state
->len
= strlen(current_name
);
273 state
->name
= current_name
;
274 state
->target_page_bits
= qemu_target_page_bits();
276 state
->caps_count
= get_validatable_capabilities_count();
277 state
->capabilities
= g_renew(MigrationCapability
, state
->capabilities
,
279 for (i
= j
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
280 if (should_validate_capability(i
) && s
->capabilities
[i
]) {
281 state
->capabilities
[j
++] = i
;
284 state
->uuid
= qemu_uuid
;
289 static int configuration_post_save(void *opaque
)
291 SaveState
*state
= opaque
;
293 g_free(state
->capabilities
);
294 state
->capabilities
= NULL
;
295 state
->caps_count
= 0;
299 static int configuration_pre_load(void *opaque
)
301 SaveState
*state
= opaque
;
303 /* If there is no target-page-bits subsection it means the source
304 * predates the variable-target-page-bits support and is using the
305 * minimum possible value for this CPU.
307 state
->target_page_bits
= qemu_target_page_bits_min();
311 static bool configuration_validate_capabilities(SaveState
*state
)
314 MigrationState
*s
= migrate_get_current();
315 unsigned long *source_caps_bm
;
318 source_caps_bm
= bitmap_new(MIGRATION_CAPABILITY__MAX
);
319 for (i
= 0; i
< state
->caps_count
; i
++) {
320 MigrationCapability capability
= state
->capabilities
[i
];
321 set_bit(capability
, source_caps_bm
);
324 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
325 bool source_state
, target_state
;
326 if (!should_validate_capability(i
)) {
329 source_state
= test_bit(i
, source_caps_bm
);
330 target_state
= s
->capabilities
[i
];
331 if (source_state
!= target_state
) {
332 error_report("Capability %s is %s, but received capability is %s",
333 MigrationCapability_str(i
),
334 target_state
? "on" : "off",
335 source_state
? "on" : "off");
337 /* Don't break here to report all failed capabilities */
341 g_free(source_caps_bm
);
345 static int configuration_post_load(void *opaque
, int version_id
)
347 SaveState
*state
= opaque
;
348 const char *current_name
= MACHINE_GET_CLASS(current_machine
)->name
;
351 if (strncmp(state
->name
, current_name
, state
->len
) != 0) {
352 error_report("Machine type received is '%.*s' and local is '%s'",
353 (int) state
->len
, state
->name
, current_name
);
358 if (state
->target_page_bits
!= qemu_target_page_bits()) {
359 error_report("Received TARGET_PAGE_BITS is %d but local is %d",
360 state
->target_page_bits
, qemu_target_page_bits());
365 if (!configuration_validate_capabilities(state
)) {
371 g_free((void *)state
->name
);
374 g_free(state
->capabilities
);
375 state
->capabilities
= NULL
;
376 state
->caps_count
= 0;
381 static int get_capability(QEMUFile
*f
, void *pv
, size_t size
,
382 const VMStateField
*field
)
384 MigrationCapability
*capability
= pv
;
385 char capability_str
[UINT8_MAX
+ 1];
389 len
= qemu_get_byte(f
);
390 qemu_get_buffer(f
, (uint8_t *)capability_str
, len
);
391 capability_str
[len
] = '\0';
392 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
393 if (!strcmp(MigrationCapability_str(i
), capability_str
)) {
398 error_report("Received unknown capability %s", capability_str
);
402 static int put_capability(QEMUFile
*f
, void *pv
, size_t size
,
403 const VMStateField
*field
, JSONWriter
*vmdesc
)
405 MigrationCapability
*capability
= pv
;
406 const char *capability_str
= MigrationCapability_str(*capability
);
407 size_t len
= strlen(capability_str
);
408 assert(len
<= UINT8_MAX
);
410 qemu_put_byte(f
, len
);
411 qemu_put_buffer(f
, (uint8_t *)capability_str
, len
);
415 static const VMStateInfo vmstate_info_capability
= {
416 .name
= "capability",
417 .get
= get_capability
,
418 .put
= put_capability
,
421 /* The target-page-bits subsection is present only if the
422 * target page size is not the same as the default (ie the
423 * minimum page size for a variable-page-size guest CPU).
424 * If it is present then it contains the actual target page
425 * bits for the machine, and migration will fail if the
426 * two ends don't agree about it.
428 static bool vmstate_target_page_bits_needed(void *opaque
)
430 return qemu_target_page_bits()
431 > qemu_target_page_bits_min();
434 static const VMStateDescription vmstate_target_page_bits
= {
435 .name
= "configuration/target-page-bits",
437 .minimum_version_id
= 1,
438 .needed
= vmstate_target_page_bits_needed
,
439 .fields
= (VMStateField
[]) {
440 VMSTATE_UINT32(target_page_bits
, SaveState
),
441 VMSTATE_END_OF_LIST()
445 static bool vmstate_capabilites_needed(void *opaque
)
447 return get_validatable_capabilities_count() > 0;
450 static const VMStateDescription vmstate_capabilites
= {
451 .name
= "configuration/capabilities",
453 .minimum_version_id
= 1,
454 .needed
= vmstate_capabilites_needed
,
455 .fields
= (VMStateField
[]) {
456 VMSTATE_UINT32_V(caps_count
, SaveState
, 1),
457 VMSTATE_VARRAY_UINT32_ALLOC(capabilities
, SaveState
, caps_count
, 1,
458 vmstate_info_capability
,
459 MigrationCapability
),
460 VMSTATE_END_OF_LIST()
464 static bool vmstate_uuid_needed(void *opaque
)
466 return qemu_uuid_set
&& migrate_validate_uuid();
469 static int vmstate_uuid_post_load(void *opaque
, int version_id
)
471 SaveState
*state
= opaque
;
472 char uuid_src
[UUID_FMT_LEN
+ 1];
473 char uuid_dst
[UUID_FMT_LEN
+ 1];
475 if (!qemu_uuid_set
) {
477 * It's warning because user might not know UUID in some cases,
478 * e.g. load an old snapshot
480 qemu_uuid_unparse(&state
->uuid
, uuid_src
);
481 warn_report("UUID is received %s, but local uuid isn't set",
485 if (!qemu_uuid_is_equal(&state
->uuid
, &qemu_uuid
)) {
486 qemu_uuid_unparse(&state
->uuid
, uuid_src
);
487 qemu_uuid_unparse(&qemu_uuid
, uuid_dst
);
488 error_report("UUID received is %s and local is %s", uuid_src
, uuid_dst
);
494 static const VMStateDescription vmstate_uuid
= {
495 .name
= "configuration/uuid",
497 .minimum_version_id
= 1,
498 .needed
= vmstate_uuid_needed
,
499 .post_load
= vmstate_uuid_post_load
,
500 .fields
= (VMStateField
[]) {
501 VMSTATE_UINT8_ARRAY_V(uuid
.data
, SaveState
, sizeof(QemuUUID
), 1),
502 VMSTATE_END_OF_LIST()
506 static const VMStateDescription vmstate_configuration
= {
507 .name
= "configuration",
509 .pre_load
= configuration_pre_load
,
510 .post_load
= configuration_post_load
,
511 .pre_save
= configuration_pre_save
,
512 .post_save
= configuration_post_save
,
513 .fields
= (VMStateField
[]) {
514 VMSTATE_UINT32(len
, SaveState
),
515 VMSTATE_VBUFFER_ALLOC_UINT32(name
, SaveState
, 0, NULL
, len
),
516 VMSTATE_END_OF_LIST()
518 .subsections
= (const VMStateDescription
*[]) {
519 &vmstate_target_page_bits
,
520 &vmstate_capabilites
,
526 static void dump_vmstate_vmsd(FILE *out_file
,
527 const VMStateDescription
*vmsd
, int indent
,
530 static void dump_vmstate_vmsf(FILE *out_file
, const VMStateField
*field
,
533 fprintf(out_file
, "%*s{\n", indent
, "");
535 fprintf(out_file
, "%*s\"field\": \"%s\",\n", indent
, "", field
->name
);
536 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
538 fprintf(out_file
, "%*s\"field_exists\": %s,\n", indent
, "",
539 field
->field_exists
? "true" : "false");
540 if (field
->flags
& VMS_ARRAY
) {
541 fprintf(out_file
, "%*s\"num\": %d,\n", indent
, "", field
->num
);
543 fprintf(out_file
, "%*s\"size\": %zu", indent
, "", field
->size
);
544 if (field
->vmsd
!= NULL
) {
545 fprintf(out_file
, ",\n");
546 dump_vmstate_vmsd(out_file
, field
->vmsd
, indent
, false);
548 fprintf(out_file
, "\n%*s}", indent
- 2, "");
551 static void dump_vmstate_vmss(FILE *out_file
,
552 const VMStateDescription
**subsection
,
555 if (*subsection
!= NULL
) {
556 dump_vmstate_vmsd(out_file
, *subsection
, indent
, true);
560 static void dump_vmstate_vmsd(FILE *out_file
,
561 const VMStateDescription
*vmsd
, int indent
,
565 fprintf(out_file
, "%*s{\n", indent
, "");
567 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", "Description");
570 fprintf(out_file
, "%*s\"name\": \"%s\",\n", indent
, "", vmsd
->name
);
571 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
573 fprintf(out_file
, "%*s\"minimum_version_id\": %d", indent
, "",
574 vmsd
->minimum_version_id
);
575 if (vmsd
->fields
!= NULL
) {
576 const VMStateField
*field
= vmsd
->fields
;
579 fprintf(out_file
, ",\n%*s\"Fields\": [\n", indent
, "");
581 while (field
->name
!= NULL
) {
582 if (field
->flags
& VMS_MUST_EXIST
) {
583 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
588 fprintf(out_file
, ",\n");
590 dump_vmstate_vmsf(out_file
, field
, indent
+ 2);
594 assert(field
->flags
== VMS_END
);
595 fprintf(out_file
, "\n%*s]", indent
, "");
597 if (vmsd
->subsections
!= NULL
) {
598 const VMStateDescription
**subsection
= vmsd
->subsections
;
601 fprintf(out_file
, ",\n%*s\"Subsections\": [\n", indent
, "");
603 while (*subsection
!= NULL
) {
605 fprintf(out_file
, ",\n");
607 dump_vmstate_vmss(out_file
, subsection
, indent
+ 2);
611 fprintf(out_file
, "\n%*s]", indent
, "");
613 fprintf(out_file
, "\n%*s}", indent
- 2, "");
616 static void dump_machine_type(FILE *out_file
)
620 mc
= MACHINE_GET_CLASS(current_machine
);
622 fprintf(out_file
, " \"vmschkmachine\": {\n");
623 fprintf(out_file
, " \"Name\": \"%s\"\n", mc
->name
);
624 fprintf(out_file
, " },\n");
627 void dump_vmstate_json_to_file(FILE *out_file
)
632 fprintf(out_file
, "{\n");
633 dump_machine_type(out_file
);
636 list
= object_class_get_list(TYPE_DEVICE
, true);
637 for (elt
= list
; elt
; elt
= elt
->next
) {
638 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
648 fprintf(out_file
, ",\n");
650 name
= object_class_get_name(OBJECT_CLASS(dc
));
651 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", name
);
653 fprintf(out_file
, "%*s\"Name\": \"%s\",\n", indent
, "", name
);
654 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
655 dc
->vmsd
->version_id
);
656 fprintf(out_file
, "%*s\"minimum_version_id\": %d,\n", indent
, "",
657 dc
->vmsd
->minimum_version_id
);
659 dump_vmstate_vmsd(out_file
, dc
->vmsd
, indent
, false);
661 fprintf(out_file
, "\n%*s}", indent
- 2, "");
664 fprintf(out_file
, "\n}\n");
669 static uint32_t calculate_new_instance_id(const char *idstr
)
672 uint32_t instance_id
= 0;
674 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
675 if (strcmp(idstr
, se
->idstr
) == 0
676 && instance_id
<= se
->instance_id
) {
677 instance_id
= se
->instance_id
+ 1;
680 /* Make sure we never loop over without being noticed */
681 assert(instance_id
!= VMSTATE_INSTANCE_ID_ANY
);
685 static int calculate_compat_instance_id(const char *idstr
)
690 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
695 if (strcmp(idstr
, se
->compat
->idstr
) == 0
696 && instance_id
<= se
->compat
->instance_id
) {
697 instance_id
= se
->compat
->instance_id
+ 1;
703 static inline MigrationPriority
save_state_priority(SaveStateEntry
*se
)
706 return se
->vmsd
->priority
;
708 return MIG_PRI_DEFAULT
;
711 static void savevm_state_handler_insert(SaveStateEntry
*nse
)
713 MigrationPriority priority
= save_state_priority(nse
);
717 assert(priority
<= MIG_PRI_MAX
);
719 for (i
= priority
- 1; i
>= 0; i
--) {
720 se
= savevm_state
.handler_pri_head
[i
];
722 assert(save_state_priority(se
) < priority
);
728 QTAILQ_INSERT_BEFORE(se
, nse
, entry
);
730 QTAILQ_INSERT_TAIL(&savevm_state
.handlers
, nse
, entry
);
733 if (savevm_state
.handler_pri_head
[priority
] == NULL
) {
734 savevm_state
.handler_pri_head
[priority
] = nse
;
738 static void savevm_state_handler_remove(SaveStateEntry
*se
)
740 SaveStateEntry
*next
;
741 MigrationPriority priority
= save_state_priority(se
);
743 if (se
== savevm_state
.handler_pri_head
[priority
]) {
744 next
= QTAILQ_NEXT(se
, entry
);
745 if (next
!= NULL
&& save_state_priority(next
) == priority
) {
746 savevm_state
.handler_pri_head
[priority
] = next
;
748 savevm_state
.handler_pri_head
[priority
] = NULL
;
751 QTAILQ_REMOVE(&savevm_state
.handlers
, se
, entry
);
754 /* TODO: Individual devices generally have very little idea about the rest
755 of the system, so instance_id should be removed/replaced.
756 Meanwhile pass -1 as instance_id if you do not already have a clearly
757 distinguishing id for all instances of your device class. */
758 int register_savevm_live(const char *idstr
,
759 uint32_t instance_id
,
761 const SaveVMHandlers
*ops
,
766 se
= g_new0(SaveStateEntry
, 1);
767 se
->version_id
= version_id
;
768 se
->section_id
= savevm_state
.global_section_id
++;
772 /* if this is a live_savem then set is_ram */
773 if (ops
->save_setup
!= NULL
) {
777 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
779 if (instance_id
== VMSTATE_INSTANCE_ID_ANY
) {
780 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
782 se
->instance_id
= instance_id
;
784 assert(!se
->compat
|| se
->instance_id
== 0);
785 savevm_state_handler_insert(se
);
789 void unregister_savevm(VMStateIf
*obj
, const char *idstr
, void *opaque
)
791 SaveStateEntry
*se
, *new_se
;
795 char *oid
= vmstate_if_get_id(obj
);
797 pstrcpy(id
, sizeof(id
), oid
);
798 pstrcat(id
, sizeof(id
), "/");
802 pstrcat(id
, sizeof(id
), idstr
);
804 QTAILQ_FOREACH_SAFE(se
, &savevm_state
.handlers
, entry
, new_se
) {
805 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
806 savevm_state_handler_remove(se
);
814 * Perform some basic checks on vmsd's at registration
817 static void vmstate_check(const VMStateDescription
*vmsd
)
819 const VMStateField
*field
= vmsd
->fields
;
820 const VMStateDescription
**subsection
= vmsd
->subsections
;
823 while (field
->name
) {
824 if (field
->flags
& (VMS_STRUCT
| VMS_VSTRUCT
)) {
825 /* Recurse to sub structures */
826 vmstate_check(field
->vmsd
);
831 /* Check for the end of field list canary */
832 if (field
->flags
!= VMS_END
) {
833 error_report("VMSTATE not ending with VMS_END: %s", vmsd
->name
);
834 g_assert_not_reached();
838 while (subsection
&& *subsection
) {
840 * The name of a subsection should start with the name of the
843 assert(!strncmp(vmsd
->name
, (*subsection
)->name
, strlen(vmsd
->name
)));
844 vmstate_check(*subsection
);
849 int vmstate_register_with_alias_id(VMStateIf
*obj
, uint32_t instance_id
,
850 const VMStateDescription
*vmsd
,
851 void *opaque
, int alias_id
,
852 int required_for_version
,
857 /* If this triggers, alias support can be dropped for the vmsd. */
858 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
860 se
= g_new0(SaveStateEntry
, 1);
861 se
->version_id
= vmsd
->version_id
;
862 se
->section_id
= savevm_state
.global_section_id
++;
865 se
->alias_id
= alias_id
;
868 char *id
= vmstate_if_get_id(obj
);
870 if (snprintf(se
->idstr
, sizeof(se
->idstr
), "%s/", id
) >=
872 error_setg(errp
, "Path too long for VMState (%s)", id
);
880 se
->compat
= g_new0(CompatEntry
, 1);
881 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
882 se
->compat
->instance_id
= instance_id
== VMSTATE_INSTANCE_ID_ANY
?
883 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
884 instance_id
= VMSTATE_INSTANCE_ID_ANY
;
887 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
889 if (instance_id
== VMSTATE_INSTANCE_ID_ANY
) {
890 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
892 se
->instance_id
= instance_id
;
895 /* Perform a recursive sanity check during the test runs */
896 if (qtest_enabled()) {
899 assert(!se
->compat
|| se
->instance_id
== 0);
900 savevm_state_handler_insert(se
);
904 void vmstate_unregister(VMStateIf
*obj
, const VMStateDescription
*vmsd
,
907 SaveStateEntry
*se
, *new_se
;
909 QTAILQ_FOREACH_SAFE(se
, &savevm_state
.handlers
, entry
, new_se
) {
910 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
911 savevm_state_handler_remove(se
);
918 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
)
920 trace_vmstate_load(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
921 if (!se
->vmsd
) { /* Old style */
922 return se
->ops
->load_state(f
, se
->opaque
, se
->load_version_id
);
924 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, se
->load_version_id
);
927 static void vmstate_save_old_style(QEMUFile
*f
, SaveStateEntry
*se
,
930 uint64_t old_offset
= qemu_file_transferred_fast(f
);
931 se
->ops
->save_state(f
, se
->opaque
);
932 uint64_t size
= qemu_file_transferred_fast(f
) - old_offset
;
935 json_writer_int64(vmdesc
, "size", size
);
936 json_writer_start_array(vmdesc
, "fields");
937 json_writer_start_object(vmdesc
, NULL
);
938 json_writer_str(vmdesc
, "name", "data");
939 json_writer_int64(vmdesc
, "size", size
);
940 json_writer_str(vmdesc
, "type", "buffer");
941 json_writer_end_object(vmdesc
);
942 json_writer_end_array(vmdesc
);
947 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
949 static void save_section_header(QEMUFile
*f
, SaveStateEntry
*se
,
950 uint8_t section_type
)
952 qemu_put_byte(f
, section_type
);
953 qemu_put_be32(f
, se
->section_id
);
955 if (section_type
== QEMU_VM_SECTION_FULL
||
956 section_type
== QEMU_VM_SECTION_START
) {
958 size_t len
= strlen(se
->idstr
);
959 qemu_put_byte(f
, len
);
960 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
962 qemu_put_be32(f
, se
->instance_id
);
963 qemu_put_be32(f
, se
->version_id
);
968 * Write a footer onto device sections that catches cases misformatted device
971 static void save_section_footer(QEMUFile
*f
, SaveStateEntry
*se
)
973 if (migrate_get_current()->send_section_footer
) {
974 qemu_put_byte(f
, QEMU_VM_SECTION_FOOTER
);
975 qemu_put_be32(f
, se
->section_id
);
979 static int vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
, JSONWriter
*vmdesc
)
983 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
986 if (se
->vmsd
&& !vmstate_save_needed(se
->vmsd
, se
->opaque
)) {
987 trace_savevm_section_skip(se
->idstr
, se
->section_id
);
991 trace_savevm_section_start(se
->idstr
, se
->section_id
);
992 save_section_header(f
, se
, QEMU_VM_SECTION_FULL
);
994 json_writer_start_object(vmdesc
, NULL
);
995 json_writer_str(vmdesc
, "name", se
->idstr
);
996 json_writer_int64(vmdesc
, "instance_id", se
->instance_id
);
999 trace_vmstate_save(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
1001 vmstate_save_old_style(f
, se
, vmdesc
);
1003 ret
= vmstate_save_state(f
, se
->vmsd
, se
->opaque
, vmdesc
);
1009 trace_savevm_section_end(se
->idstr
, se
->section_id
, 0);
1010 save_section_footer(f
, se
);
1012 json_writer_end_object(vmdesc
);
1017 * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the
1018 * command and associated data.
1020 * @f: File to send command on
1021 * @command: Command type to send
1022 * @len: Length of associated data
1023 * @data: Data associated with command.
1025 static void qemu_savevm_command_send(QEMUFile
*f
,
1026 enum qemu_vm_cmd command
,
1030 trace_savevm_command_send(command
, len
);
1031 qemu_put_byte(f
, QEMU_VM_COMMAND
);
1032 qemu_put_be16(f
, (uint16_t)command
);
1033 qemu_put_be16(f
, len
);
1034 qemu_put_buffer(f
, data
, len
);
1038 void qemu_savevm_send_colo_enable(QEMUFile
*f
)
1040 trace_savevm_send_colo_enable();
1041 qemu_savevm_command_send(f
, MIG_CMD_ENABLE_COLO
, 0, NULL
);
1044 void qemu_savevm_send_ping(QEMUFile
*f
, uint32_t value
)
1048 trace_savevm_send_ping(value
);
1049 buf
= cpu_to_be32(value
);
1050 qemu_savevm_command_send(f
, MIG_CMD_PING
, sizeof(value
), (uint8_t *)&buf
);
1053 void qemu_savevm_send_open_return_path(QEMUFile
*f
)
1055 trace_savevm_send_open_return_path();
1056 qemu_savevm_command_send(f
, MIG_CMD_OPEN_RETURN_PATH
, 0, NULL
);
1059 /* We have a buffer of data to send; we don't want that all to be loaded
1060 * by the command itself, so the command contains just the length of the
1061 * extra buffer that we then send straight after it.
1062 * TODO: Must be a better way to organise that
1068 int qemu_savevm_send_packaged(QEMUFile
*f
, const uint8_t *buf
, size_t len
)
1072 if (len
> MAX_VM_CMD_PACKAGED_SIZE
) {
1073 error_report("%s: Unreasonably large packaged state: %zu",
1078 tmp
= cpu_to_be32(len
);
1080 trace_qemu_savevm_send_packaged();
1081 qemu_savevm_command_send(f
, MIG_CMD_PACKAGED
, 4, (uint8_t *)&tmp
);
1083 qemu_put_buffer(f
, buf
, len
);
1088 /* Send prior to any postcopy transfer */
1089 void qemu_savevm_send_postcopy_advise(QEMUFile
*f
)
1091 if (migrate_postcopy_ram()) {
1093 tmp
[0] = cpu_to_be64(ram_pagesize_summary());
1094 tmp
[1] = cpu_to_be64(qemu_target_page_size());
1096 trace_qemu_savevm_send_postcopy_advise();
1097 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_ADVISE
,
1098 16, (uint8_t *)tmp
);
1100 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_ADVISE
, 0, NULL
);
1104 /* Sent prior to starting the destination running in postcopy, discard pages
1105 * that have already been sent but redirtied on the source.
1106 * CMD_POSTCOPY_RAM_DISCARD consist of:
1108 * byte Length of name field (not including 0)
1109 * n x byte RAM block name
1110 * byte 0 terminator (just for safety)
1111 * n x Byte ranges within the named RAMBlock
1112 * be64 Start of the range
1115 * name: RAMBlock name that these entries are part of
1116 * len: Number of page entries
1117 * start_list: 'len' addresses
1118 * length_list: 'len' addresses
1121 void qemu_savevm_send_postcopy_ram_discard(QEMUFile
*f
, const char *name
,
1123 uint64_t *start_list
,
1124 uint64_t *length_list
)
1129 size_t name_len
= strlen(name
);
1131 trace_qemu_savevm_send_postcopy_ram_discard(name
, len
);
1132 assert(name_len
< 256);
1133 buf
= g_malloc0(1 + 1 + name_len
+ 1 + (8 + 8) * len
);
1134 buf
[0] = postcopy_ram_discard_version
;
1136 memcpy(buf
+ 2, name
, name_len
);
1137 tmplen
= 2 + name_len
;
1138 buf
[tmplen
++] = '\0';
1140 for (t
= 0; t
< len
; t
++) {
1141 stq_be_p(buf
+ tmplen
, start_list
[t
]);
1143 stq_be_p(buf
+ tmplen
, length_list
[t
]);
1146 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_RAM_DISCARD
, tmplen
, buf
);
1150 /* Get the destination into a state where it can receive postcopy data. */
1151 void qemu_savevm_send_postcopy_listen(QEMUFile
*f
)
1153 trace_savevm_send_postcopy_listen();
1154 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_LISTEN
, 0, NULL
);
1157 /* Kick the destination into running */
1158 void qemu_savevm_send_postcopy_run(QEMUFile
*f
)
1160 trace_savevm_send_postcopy_run();
1161 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_RUN
, 0, NULL
);
1164 void qemu_savevm_send_postcopy_resume(QEMUFile
*f
)
1166 trace_savevm_send_postcopy_resume();
1167 qemu_savevm_command_send(f
, MIG_CMD_POSTCOPY_RESUME
, 0, NULL
);
1170 void qemu_savevm_send_recv_bitmap(QEMUFile
*f
, char *block_name
)
1175 trace_savevm_send_recv_bitmap(block_name
);
1177 buf
[0] = len
= strlen(block_name
);
1178 memcpy(buf
+ 1, block_name
, len
);
1180 qemu_savevm_command_send(f
, MIG_CMD_RECV_BITMAP
, len
+ 1, (uint8_t *)buf
);
1183 bool qemu_savevm_state_blocked(Error
**errp
)
1187 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1188 if (se
->vmsd
&& se
->vmsd
->unmigratable
) {
1189 error_setg(errp
, "State blocked by non-migratable device '%s'",
1197 void qemu_savevm_non_migratable_list(strList
**reasons
)
1201 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1202 if (se
->vmsd
&& se
->vmsd
->unmigratable
) {
1203 QAPI_LIST_PREPEND(*reasons
,
1204 g_strdup_printf("non-migratable device: %s",
1210 void qemu_savevm_state_header(QEMUFile
*f
)
1212 trace_savevm_state_header();
1213 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1214 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1216 if (migrate_get_current()->send_configuration
) {
1217 qemu_put_byte(f
, QEMU_VM_CONFIGURATION
);
1218 vmstate_save_state(f
, &vmstate_configuration
, &savevm_state
, 0);
1222 bool qemu_savevm_state_guest_unplug_pending(void)
1226 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1227 if (se
->vmsd
&& se
->vmsd
->dev_unplug_pending
&&
1228 se
->vmsd
->dev_unplug_pending(se
->opaque
)) {
1236 void qemu_savevm_state_setup(QEMUFile
*f
)
1238 MigrationState
*ms
= migrate_get_current();
1240 Error
*local_err
= NULL
;
1243 ms
->vmdesc
= json_writer_new(false);
1244 json_writer_start_object(ms
->vmdesc
, NULL
);
1245 json_writer_int64(ms
->vmdesc
, "page_size", qemu_target_page_size());
1246 json_writer_start_array(ms
->vmdesc
, "devices");
1248 trace_savevm_state_setup();
1249 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1250 if (se
->vmsd
&& se
->vmsd
->early_setup
) {
1251 ret
= vmstate_save(f
, se
, ms
->vmdesc
);
1253 qemu_file_set_error(f
, ret
);
1259 if (!se
->ops
|| !se
->ops
->save_setup
) {
1262 if (se
->ops
->is_active
) {
1263 if (!se
->ops
->is_active(se
->opaque
)) {
1267 save_section_header(f
, se
, QEMU_VM_SECTION_START
);
1269 ret
= se
->ops
->save_setup(f
, se
->opaque
);
1270 save_section_footer(f
, se
);
1272 qemu_file_set_error(f
, ret
);
1277 if (precopy_notify(PRECOPY_NOTIFY_SETUP
, &local_err
)) {
1278 error_report_err(local_err
);
1282 int qemu_savevm_state_resume_prepare(MigrationState
*s
)
1287 trace_savevm_state_resume_prepare();
1289 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1290 if (!se
->ops
|| !se
->ops
->resume_prepare
) {
1293 if (se
->ops
->is_active
) {
1294 if (!se
->ops
->is_active(se
->opaque
)) {
1298 ret
= se
->ops
->resume_prepare(s
, se
->opaque
);
1308 * this function has three return values:
1309 * negative: there was one error, and we have -errno.
1310 * 0 : We haven't finished, caller have to go again
1311 * 1 : We have finished, we can go to complete phase
1313 int qemu_savevm_state_iterate(QEMUFile
*f
, bool postcopy
)
1318 trace_savevm_state_iterate();
1319 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1320 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
1323 if (se
->ops
->is_active
&&
1324 !se
->ops
->is_active(se
->opaque
)) {
1327 if (se
->ops
->is_active_iterate
&&
1328 !se
->ops
->is_active_iterate(se
->opaque
)) {
1332 * In the postcopy phase, any device that doesn't know how to
1333 * do postcopy should have saved it's state in the _complete
1334 * call that's already run, it might get confused if we call
1335 * iterate afterwards.
1338 !(se
->ops
->has_postcopy
&& se
->ops
->has_postcopy(se
->opaque
))) {
1341 if (qemu_file_rate_limit(f
)) {
1344 trace_savevm_section_start(se
->idstr
, se
->section_id
);
1346 save_section_header(f
, se
, QEMU_VM_SECTION_PART
);
1348 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
1349 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
1350 save_section_footer(f
, se
);
1353 error_report("failed to save SaveStateEntry with id(name): "
1355 se
->section_id
, se
->idstr
, ret
);
1356 qemu_file_set_error(f
, ret
);
1359 /* Do not proceed to the next vmstate before this one reported
1360 completion of the current stage. This serializes the migration
1361 and reduces the probability that a faster changing state is
1362 synchronized over and over again. */
1369 static bool should_send_vmdesc(void)
1371 MachineState
*machine
= MACHINE(qdev_get_machine());
1372 bool in_postcopy
= migration_in_postcopy();
1373 return !machine
->suppress_vmdesc
&& !in_postcopy
;
1377 * Calls the save_live_complete_postcopy methods
1378 * causing the last few pages to be sent immediately and doing any associated
1380 * Note postcopy also calls qemu_savevm_state_complete_precopy to complete
1381 * all the other devices, but that happens at the point we switch to postcopy.
1383 void qemu_savevm_state_complete_postcopy(QEMUFile
*f
)
1388 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1389 if (!se
->ops
|| !se
->ops
->save_live_complete_postcopy
) {
1392 if (se
->ops
->is_active
) {
1393 if (!se
->ops
->is_active(se
->opaque
)) {
1397 trace_savevm_section_start(se
->idstr
, se
->section_id
);
1399 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1400 qemu_put_be32(f
, se
->section_id
);
1402 ret
= se
->ops
->save_live_complete_postcopy(f
, se
->opaque
);
1403 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
1404 save_section_footer(f
, se
);
1406 qemu_file_set_error(f
, ret
);
1411 qemu_put_byte(f
, QEMU_VM_EOF
);
1416 int qemu_savevm_state_complete_precopy_iterable(QEMUFile
*f
, bool in_postcopy
)
1421 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1423 (in_postcopy
&& se
->ops
->has_postcopy
&&
1424 se
->ops
->has_postcopy(se
->opaque
)) ||
1425 !se
->ops
->save_live_complete_precopy
) {
1429 if (se
->ops
->is_active
) {
1430 if (!se
->ops
->is_active(se
->opaque
)) {
1434 trace_savevm_section_start(se
->idstr
, se
->section_id
);
1436 save_section_header(f
, se
, QEMU_VM_SECTION_END
);
1438 ret
= se
->ops
->save_live_complete_precopy(f
, se
->opaque
);
1439 trace_savevm_section_end(se
->idstr
, se
->section_id
, ret
);
1440 save_section_footer(f
, se
);
1442 qemu_file_set_error(f
, ret
);
1450 int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile
*f
,
1452 bool inactivate_disks
)
1454 MigrationState
*ms
= migrate_get_current();
1455 JSONWriter
*vmdesc
= ms
->vmdesc
;
1460 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1461 if (se
->vmsd
&& se
->vmsd
->early_setup
) {
1462 /* Already saved during qemu_savevm_state_setup(). */
1466 ret
= vmstate_save(f
, se
, vmdesc
);
1468 qemu_file_set_error(f
, ret
);
1473 if (inactivate_disks
) {
1474 /* Inactivate before sending QEMU_VM_EOF so that the
1475 * bdrv_activate_all() on the other end won't fail. */
1476 ret
= bdrv_inactivate_all();
1478 error_report("%s: bdrv_inactivate_all() failed (%d)",
1480 qemu_file_set_error(f
, ret
);
1485 /* Postcopy stream will still be going */
1486 qemu_put_byte(f
, QEMU_VM_EOF
);
1489 json_writer_end_array(vmdesc
);
1490 json_writer_end_object(vmdesc
);
1491 vmdesc_len
= strlen(json_writer_get(vmdesc
));
1493 if (should_send_vmdesc()) {
1494 qemu_put_byte(f
, QEMU_VM_VMDESCRIPTION
);
1495 qemu_put_be32(f
, vmdesc_len
);
1496 qemu_put_buffer(f
, (uint8_t *)json_writer_get(vmdesc
), vmdesc_len
);
1499 /* Free it now to detect any inconsistencies. */
1500 json_writer_free(vmdesc
);
1506 int qemu_savevm_state_complete_precopy(QEMUFile
*f
, bool iterable_only
,
1507 bool inactivate_disks
)
1510 Error
*local_err
= NULL
;
1511 bool in_postcopy
= migration_in_postcopy();
1513 if (precopy_notify(PRECOPY_NOTIFY_COMPLETE
, &local_err
)) {
1514 error_report_err(local_err
);
1517 trace_savevm_state_complete_precopy();
1519 cpu_synchronize_all_states();
1521 if (!in_postcopy
|| iterable_only
) {
1522 ret
= qemu_savevm_state_complete_precopy_iterable(f
, in_postcopy
);
1528 if (iterable_only
) {
1532 ret
= qemu_savevm_state_complete_precopy_non_iterable(f
, in_postcopy
,
1543 /* Give an estimate of the amount left to be transferred,
1544 * the result is split into the amount for units that can and
1545 * for units that can't do postcopy.
1547 void qemu_savevm_state_pending_estimate(uint64_t *must_precopy
,
1548 uint64_t *can_postcopy
)
1555 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1556 if (!se
->ops
|| !se
->ops
->state_pending_estimate
) {
1559 if (se
->ops
->is_active
) {
1560 if (!se
->ops
->is_active(se
->opaque
)) {
1564 se
->ops
->state_pending_estimate(se
->opaque
, must_precopy
, can_postcopy
);
1568 void qemu_savevm_state_pending_exact(uint64_t *must_precopy
,
1569 uint64_t *can_postcopy
)
1576 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1577 if (!se
->ops
|| !se
->ops
->state_pending_exact
) {
1580 if (se
->ops
->is_active
) {
1581 if (!se
->ops
->is_active(se
->opaque
)) {
1585 se
->ops
->state_pending_exact(se
->opaque
, must_precopy
, can_postcopy
);
1589 void qemu_savevm_state_cleanup(void)
1592 Error
*local_err
= NULL
;
1594 if (precopy_notify(PRECOPY_NOTIFY_CLEANUP
, &local_err
)) {
1595 error_report_err(local_err
);
1598 trace_savevm_state_cleanup();
1599 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1600 if (se
->ops
&& se
->ops
->save_cleanup
) {
1601 se
->ops
->save_cleanup(se
->opaque
);
1606 static int qemu_savevm_state(QEMUFile
*f
, Error
**errp
)
1609 MigrationState
*ms
= migrate_get_current();
1610 MigrationStatus status
;
1612 if (migration_is_running(ms
->state
)) {
1613 error_setg(errp
, QERR_MIGRATION_ACTIVE
);
1617 if (migrate_block()) {
1618 error_setg(errp
, "Block migration and snapshots are incompatible");
1623 memset(&mig_stats
, 0, sizeof(mig_stats
));
1624 memset(&compression_counters
, 0, sizeof(compression_counters
));
1625 ms
->to_dst_file
= f
;
1627 qemu_mutex_unlock_iothread();
1628 qemu_savevm_state_header(f
);
1629 qemu_savevm_state_setup(f
);
1630 qemu_mutex_lock_iothread();
1632 while (qemu_file_get_error(f
) == 0) {
1633 if (qemu_savevm_state_iterate(f
, false) > 0) {
1638 ret
= qemu_file_get_error(f
);
1640 qemu_savevm_state_complete_precopy(f
, false, false);
1641 ret
= qemu_file_get_error(f
);
1643 qemu_savevm_state_cleanup();
1645 error_setg_errno(errp
, -ret
, "Error while writing VM state");
1649 status
= MIGRATION_STATUS_FAILED
;
1651 status
= MIGRATION_STATUS_COMPLETED
;
1653 migrate_set_state(&ms
->state
, MIGRATION_STATUS_SETUP
, status
);
1655 /* f is outer parameter, it should not stay in global migration state after
1656 * this function finished */
1657 ms
->to_dst_file
= NULL
;
1662 void qemu_savevm_live_state(QEMUFile
*f
)
1664 /* save QEMU_VM_SECTION_END section */
1665 qemu_savevm_state_complete_precopy(f
, true, false);
1666 qemu_put_byte(f
, QEMU_VM_EOF
);
1669 int qemu_save_device_state(QEMUFile
*f
)
1673 if (!migration_in_colo_state()) {
1674 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1675 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1677 cpu_synchronize_all_states();
1679 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1685 ret
= vmstate_save(f
, se
, NULL
);
1691 qemu_put_byte(f
, QEMU_VM_EOF
);
1693 return qemu_file_get_error(f
);
1696 static SaveStateEntry
*find_se(const char *idstr
, uint32_t instance_id
)
1700 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
1701 if (!strcmp(se
->idstr
, idstr
) &&
1702 (instance_id
== se
->instance_id
||
1703 instance_id
== se
->alias_id
))
1705 /* Migrating from an older version? */
1706 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
1707 if (!strcmp(se
->compat
->idstr
, idstr
) &&
1708 (instance_id
== se
->compat
->instance_id
||
1709 instance_id
== se
->alias_id
))
1716 enum LoadVMExitCodes
{
1717 /* Allow a command to quit all layers of nested loadvm loops */
1721 /* ------ incoming postcopy messages ------ */
1722 /* 'advise' arrives before any transfers just to tell us that a postcopy
1723 * *might* happen - it might be skipped if precopy transferred everything
1726 static int loadvm_postcopy_handle_advise(MigrationIncomingState
*mis
,
1729 PostcopyState ps
= postcopy_state_set(POSTCOPY_INCOMING_ADVISE
);
1730 uint64_t remote_pagesize_summary
, local_pagesize_summary
, remote_tps
;
1731 size_t page_size
= qemu_target_page_size();
1732 Error
*local_err
= NULL
;
1734 trace_loadvm_postcopy_handle_advise();
1735 if (ps
!= POSTCOPY_INCOMING_NONE
) {
1736 error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps
);
1742 if (migrate_postcopy_ram()) {
1743 error_report("RAM postcopy is enabled but have 0 byte advise");
1748 if (!migrate_postcopy_ram()) {
1749 error_report("RAM postcopy is disabled but have 16 byte advise");
1754 error_report("CMD_POSTCOPY_ADVISE invalid length (%d)", len
);
1758 if (!postcopy_ram_supported_by_host(mis
, &local_err
)) {
1759 error_report_err(local_err
);
1760 postcopy_state_set(POSTCOPY_INCOMING_NONE
);
1764 remote_pagesize_summary
= qemu_get_be64(mis
->from_src_file
);
1765 local_pagesize_summary
= ram_pagesize_summary();
1767 if (remote_pagesize_summary
!= local_pagesize_summary
) {
1769 * This detects two potential causes of mismatch:
1770 * a) A mismatch in host page sizes
1771 * Some combinations of mismatch are probably possible but it gets
1772 * a bit more complicated. In particular we need to place whole
1773 * host pages on the dest at once, and we need to ensure that we
1774 * handle dirtying to make sure we never end up sending part of
1775 * a hostpage on it's own.
1776 * b) The use of different huge page sizes on source/destination
1777 * a more fine grain test is performed during RAM block migration
1778 * but this test here causes a nice early clear failure, and
1779 * also fails when passed to an older qemu that doesn't
1782 error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64
1784 remote_pagesize_summary
, local_pagesize_summary
);
1788 remote_tps
= qemu_get_be64(mis
->from_src_file
);
1789 if (remote_tps
!= page_size
) {
1791 * Again, some differences could be dealt with, but for now keep it
1794 error_report("Postcopy needs matching target page sizes (s=%d d=%zd)",
1795 (int)remote_tps
, page_size
);
1799 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_ADVISE
, &local_err
)) {
1800 error_report_err(local_err
);
1804 if (ram_postcopy_incoming_init(mis
)) {
1811 /* After postcopy we will be told to throw some pages away since they're
1812 * dirty and will have to be demand fetched. Must happen before CPU is
1814 * There can be 0..many of these messages, each encoding multiple pages.
1816 static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState
*mis
,
1821 PostcopyState ps
= postcopy_state_get();
1823 trace_loadvm_postcopy_ram_handle_discard();
1826 case POSTCOPY_INCOMING_ADVISE
:
1828 tmp
= postcopy_ram_prepare_discard(mis
);
1834 case POSTCOPY_INCOMING_DISCARD
:
1835 /* Expected state */
1839 error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)",
1843 /* We're expecting a
1845 * a RAM ID string (length byte, name, 0 term)
1846 * then at least 1 16 byte chunk
1848 if (len
< (1 + 1 + 1 + 1 + 2 * 8)) {
1849 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len
);
1853 tmp
= qemu_get_byte(mis
->from_src_file
);
1854 if (tmp
!= postcopy_ram_discard_version
) {
1855 error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp
);
1859 if (!qemu_get_counted_string(mis
->from_src_file
, ramid
)) {
1860 error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID");
1863 tmp
= qemu_get_byte(mis
->from_src_file
);
1865 error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp
);
1869 len
-= 3 + strlen(ramid
);
1871 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len
);
1874 trace_loadvm_postcopy_ram_handle_discard_header(ramid
, len
);
1876 uint64_t start_addr
, block_length
;
1877 start_addr
= qemu_get_be64(mis
->from_src_file
);
1878 block_length
= qemu_get_be64(mis
->from_src_file
);
1881 int ret
= ram_discard_range(ramid
, start_addr
, block_length
);
1886 trace_loadvm_postcopy_ram_handle_discard_end();
1892 * Triggered by a postcopy_listen command; this thread takes over reading
1893 * the input stream, leaving the main thread free to carry on loading the rest
1894 * of the device state (from RAM).
1895 * (TODO:This could do with being in a postcopy file - but there again it's
1896 * just another input loop, not that postcopy specific)
1898 static void *postcopy_ram_listen_thread(void *opaque
)
1900 MigrationIncomingState
*mis
= migration_incoming_get_current();
1901 QEMUFile
*f
= mis
->from_src_file
;
1903 MigrationState
*migr
= migrate_get_current();
1905 object_ref(OBJECT(migr
));
1907 migrate_set_state(&mis
->state
, MIGRATION_STATUS_ACTIVE
,
1908 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
1909 qemu_sem_post(&mis
->thread_sync_sem
);
1910 trace_postcopy_ram_listen_thread_start();
1912 rcu_register_thread();
1914 * Because we're a thread and not a coroutine we can't yield
1915 * in qemu_file, and thus we must be blocking now.
1917 qemu_file_set_blocking(f
, true);
1918 load_res
= qemu_loadvm_state_main(f
, mis
);
1921 * This is tricky, but, mis->from_src_file can change after it
1922 * returns, when postcopy recovery happened. In the future, we may
1923 * want a wrapper for the QEMUFile handle.
1925 f
= mis
->from_src_file
;
1927 /* And non-blocking again so we don't block in any cleanup */
1928 qemu_file_set_blocking(f
, false);
1930 trace_postcopy_ram_listen_thread_exit();
1932 qemu_file_set_error(f
, load_res
);
1933 dirty_bitmap_mig_cancel_incoming();
1934 if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING
&&
1935 !migrate_postcopy_ram() && migrate_dirty_bitmaps())
1937 error_report("%s: loadvm failed during postcopy: %d. All states "
1938 "are migrated except dirty bitmaps. Some dirty "
1939 "bitmaps may be lost, and present migrated dirty "
1940 "bitmaps are correctly migrated and valid.",
1941 __func__
, load_res
);
1942 load_res
= 0; /* prevent further exit() */
1944 error_report("%s: loadvm failed: %d", __func__
, load_res
);
1945 migrate_set_state(&mis
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
1946 MIGRATION_STATUS_FAILED
);
1949 if (load_res
>= 0) {
1951 * This looks good, but it's possible that the device loading in the
1952 * main thread hasn't finished yet, and so we might not be in 'RUN'
1953 * state yet; wait for the end of the main thread.
1955 qemu_event_wait(&mis
->main_thread_load_event
);
1957 postcopy_ram_incoming_cleanup(mis
);
1961 * If something went wrong then we have a bad state so exit;
1962 * depending how far we got it might be possible at this point
1963 * to leave the guest running and fire MCEs for pages that never
1964 * arrived as a desperate recovery step.
1966 rcu_unregister_thread();
1970 migrate_set_state(&mis
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
1971 MIGRATION_STATUS_COMPLETED
);
1973 * If everything has worked fine, then the main thread has waited
1974 * for us to start, and we're the last use of the mis.
1975 * (If something broke then qemu will have to exit anyway since it's
1976 * got a bad migration state).
1978 migration_incoming_state_destroy();
1979 qemu_loadvm_state_cleanup();
1981 rcu_unregister_thread();
1982 mis
->have_listen_thread
= false;
1983 postcopy_state_set(POSTCOPY_INCOMING_END
);
1985 object_unref(OBJECT(migr
));
1990 /* After this message we must be able to immediately receive postcopy data */
1991 static int loadvm_postcopy_handle_listen(MigrationIncomingState
*mis
)
1993 PostcopyState ps
= postcopy_state_set(POSTCOPY_INCOMING_LISTENING
);
1994 Error
*local_err
= NULL
;
1996 trace_loadvm_postcopy_handle_listen("enter");
1998 if (ps
!= POSTCOPY_INCOMING_ADVISE
&& ps
!= POSTCOPY_INCOMING_DISCARD
) {
1999 error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps
);
2002 if (ps
== POSTCOPY_INCOMING_ADVISE
) {
2004 * A rare case, we entered listen without having to do any discards,
2005 * so do the setup that's normally done at the time of the 1st discard.
2007 if (migrate_postcopy_ram()) {
2008 postcopy_ram_prepare_discard(mis
);
2012 trace_loadvm_postcopy_handle_listen("after discard");
2015 * Sensitise RAM - can now generate requests for blocks that don't exist
2016 * However, at this point the CPU shouldn't be running, and the IO
2017 * shouldn't be doing anything yet so don't actually expect requests
2019 if (migrate_postcopy_ram()) {
2020 if (postcopy_ram_incoming_setup(mis
)) {
2021 postcopy_ram_incoming_cleanup(mis
);
2026 trace_loadvm_postcopy_handle_listen("after uffd");
2028 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN
, &local_err
)) {
2029 error_report_err(local_err
);
2033 mis
->have_listen_thread
= true;
2034 postcopy_thread_create(mis
, &mis
->listen_thread
, "postcopy/listen",
2035 postcopy_ram_listen_thread
, QEMU_THREAD_DETACHED
);
2036 trace_loadvm_postcopy_handle_listen("return");
2041 static void loadvm_postcopy_handle_run_bh(void *opaque
)
2043 Error
*local_err
= NULL
;
2044 MigrationIncomingState
*mis
= opaque
;
2046 trace_loadvm_postcopy_handle_run_bh("enter");
2048 /* TODO we should move all of this lot into postcopy_ram.c or a shared code
2051 cpu_synchronize_all_post_init();
2053 trace_loadvm_postcopy_handle_run_bh("after cpu sync");
2055 qemu_announce_self(&mis
->announce_timer
, migrate_announce_params());
2057 trace_loadvm_postcopy_handle_run_bh("after announce");
2059 /* Make sure all file formats throw away their mutable metadata.
2060 * If we get an error here, just don't restart the VM yet. */
2061 bdrv_activate_all(&local_err
);
2063 error_report_err(local_err
);
2068 trace_loadvm_postcopy_handle_run_bh("after invalidate cache");
2070 dirty_bitmap_mig_before_vm_start();
2073 /* Hold onto your hats, starting the CPU */
2076 /* leave it paused and let management decide when to start the CPU */
2077 runstate_set(RUN_STATE_PAUSED
);
2080 qemu_bh_delete(mis
->bh
);
2082 trace_loadvm_postcopy_handle_run_bh("return");
2085 /* After all discards we can start running and asking for pages */
2086 static int loadvm_postcopy_handle_run(MigrationIncomingState
*mis
)
2088 PostcopyState ps
= postcopy_state_get();
2090 trace_loadvm_postcopy_handle_run();
2091 if (ps
!= POSTCOPY_INCOMING_LISTENING
) {
2092 error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps
);
2096 postcopy_state_set(POSTCOPY_INCOMING_RUNNING
);
2097 mis
->bh
= qemu_bh_new(loadvm_postcopy_handle_run_bh
, mis
);
2098 qemu_bh_schedule(mis
->bh
);
2100 /* We need to finish reading the stream from the package
2101 * and also stop reading anything more from the stream that loaded the
2102 * package (since it's now being read by the listener thread).
2103 * LOADVM_QUIT will quit all the layers of nested loadvm loops.
2108 /* We must be with page_request_mutex held */
2109 static gboolean
postcopy_sync_page_req(gpointer key
, gpointer value
,
2112 MigrationIncomingState
*mis
= data
;
2113 void *host_addr
= (void *) key
;
2114 ram_addr_t rb_offset
;
2118 rb
= qemu_ram_block_from_host(host_addr
, true, &rb_offset
);
2121 * This should _never_ happen. However be nice for a migrating VM to
2122 * not crash/assert. Post an error (note: intended to not use *_once
2123 * because we do want to see all the illegal addresses; and this can
2124 * never be triggered by the guest so we're safe) and move on next.
2126 error_report("%s: illegal host addr %p", __func__
, host_addr
);
2127 /* Try the next entry */
2131 ret
= migrate_send_rp_message_req_pages(mis
, rb
, rb_offset
);
2133 /* Please refer to above comment. */
2134 error_report("%s: send rp message failed for addr %p",
2135 __func__
, host_addr
);
2139 trace_postcopy_page_req_sync(host_addr
);
2144 static void migrate_send_rp_req_pages_pending(MigrationIncomingState
*mis
)
2146 WITH_QEMU_LOCK_GUARD(&mis
->page_request_mutex
) {
2147 g_tree_foreach(mis
->page_requested
, postcopy_sync_page_req
, mis
);
2151 static int loadvm_postcopy_handle_resume(MigrationIncomingState
*mis
)
2153 if (mis
->state
!= MIGRATION_STATUS_POSTCOPY_RECOVER
) {
2154 error_report("%s: illegal resume received", __func__
);
2155 /* Don't fail the load, only for this. */
2160 * Reset the last_rb before we resend any page req to source again, since
2161 * the source should have it reset already.
2163 mis
->last_rb
= NULL
;
2166 * This means source VM is ready to resume the postcopy migration.
2168 migrate_set_state(&mis
->state
, MIGRATION_STATUS_POSTCOPY_RECOVER
,
2169 MIGRATION_STATUS_POSTCOPY_ACTIVE
);
2171 trace_loadvm_postcopy_handle_resume();
2173 /* Tell source that "we are ready" */
2174 migrate_send_rp_resume_ack(mis
, MIGRATION_RESUME_ACK_VALUE
);
2177 * After a postcopy recovery, the source should have lost the postcopy
2178 * queue, or potentially the requested pages could have been lost during
2179 * the network down phase. Let's re-sync with the source VM by re-sending
2180 * all the pending pages that we eagerly need, so these threads won't get
2181 * blocked too long due to the recovery.
2183 * Without this procedure, the faulted destination VM threads (waiting for
2184 * page requests right before the postcopy is interrupted) can keep hanging
2185 * until the pages are sent by the source during the background copying of
2186 * pages, or another thread faulted on the same address accidentally.
2188 migrate_send_rp_req_pages_pending(mis
);
2191 * It's time to switch state and release the fault thread to continue
2192 * service page faults. Note that this should be explicitly after the
2193 * above call to migrate_send_rp_req_pages_pending(). In short:
2194 * migrate_send_rp_message_req_pages() is not thread safe, yet.
2196 qemu_sem_post(&mis
->postcopy_pause_sem_fault
);
2198 if (migrate_postcopy_preempt()) {
2200 * The preempt channel will be created in async manner, now let's
2201 * wait for it and make sure it's created.
2203 qemu_sem_wait(&mis
->postcopy_qemufile_dst_done
);
2204 assert(mis
->postcopy_qemufile_dst
);
2205 /* Kick the fast ram load thread too */
2206 qemu_sem_post(&mis
->postcopy_pause_sem_fast_load
);
2213 * Immediately following this command is a blob of data containing an embedded
2214 * chunk of migration stream; read it and load it.
2216 * @mis: Incoming state
2217 * @length: Length of packaged data to read
2219 * Returns: Negative values on error
2222 static int loadvm_handle_cmd_packaged(MigrationIncomingState
*mis
)
2226 QIOChannelBuffer
*bioc
;
2228 length
= qemu_get_be32(mis
->from_src_file
);
2229 trace_loadvm_handle_cmd_packaged(length
);
2231 if (length
> MAX_VM_CMD_PACKAGED_SIZE
) {
2232 error_report("Unreasonably large packaged state: %zu", length
);
2236 bioc
= qio_channel_buffer_new(length
);
2237 qio_channel_set_name(QIO_CHANNEL(bioc
), "migration-loadvm-buffer");
2238 ret
= qemu_get_buffer(mis
->from_src_file
,
2241 if (ret
!= length
) {
2242 object_unref(OBJECT(bioc
));
2243 error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu",
2245 return (ret
< 0) ? ret
: -EAGAIN
;
2247 bioc
->usage
+= length
;
2248 trace_loadvm_handle_cmd_packaged_received(ret
);
2250 QEMUFile
*packf
= qemu_file_new_input(QIO_CHANNEL(bioc
));
2252 ret
= qemu_loadvm_state_main(packf
, mis
);
2253 trace_loadvm_handle_cmd_packaged_main(ret
);
2255 object_unref(OBJECT(bioc
));
2261 * Handle request that source requests for recved_bitmap on
2262 * destination. Payload format:
2264 * len (1 byte) + ramblock_name (<255 bytes)
2266 static int loadvm_handle_recv_bitmap(MigrationIncomingState
*mis
,
2269 QEMUFile
*file
= mis
->from_src_file
;
2271 char block_name
[256];
2274 cnt
= qemu_get_counted_string(file
, block_name
);
2276 error_report("%s: failed to read block name", __func__
);
2280 /* Validate before using the data */
2281 if (qemu_file_get_error(file
)) {
2282 return qemu_file_get_error(file
);
2285 if (len
!= cnt
+ 1) {
2286 error_report("%s: invalid payload length (%d)", __func__
, len
);
2290 rb
= qemu_ram_block_by_name(block_name
);
2292 error_report("%s: block '%s' not found", __func__
, block_name
);
2296 migrate_send_rp_recv_bitmap(mis
, block_name
);
2298 trace_loadvm_handle_recv_bitmap(block_name
);
2303 static int loadvm_process_enable_colo(MigrationIncomingState
*mis
)
2305 int ret
= migration_incoming_enable_colo();
2308 ret
= colo_init_ram_cache();
2310 migration_incoming_disable_colo();
2317 * Process an incoming 'QEMU_VM_COMMAND'
2318 * 0 just a normal return
2319 * LOADVM_QUIT All good, but exit the loop
2322 static int loadvm_process_command(QEMUFile
*f
)
2324 MigrationIncomingState
*mis
= migration_incoming_get_current();
2329 cmd
= qemu_get_be16(f
);
2330 len
= qemu_get_be16(f
);
2332 /* Check validity before continue processing of cmds */
2333 if (qemu_file_get_error(f
)) {
2334 return qemu_file_get_error(f
);
2337 if (cmd
>= MIG_CMD_MAX
|| cmd
== MIG_CMD_INVALID
) {
2338 error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd
, len
);
2342 trace_loadvm_process_command(mig_cmd_args
[cmd
].name
, len
);
2344 if (mig_cmd_args
[cmd
].len
!= -1 && mig_cmd_args
[cmd
].len
!= len
) {
2345 error_report("%s received with bad length - expecting %zu, got %d",
2346 mig_cmd_args
[cmd
].name
,
2347 (size_t)mig_cmd_args
[cmd
].len
, len
);
2352 case MIG_CMD_OPEN_RETURN_PATH
:
2353 if (mis
->to_src_file
) {
2354 error_report("CMD_OPEN_RETURN_PATH called when RP already open");
2355 /* Not really a problem, so don't give up */
2358 mis
->to_src_file
= qemu_file_get_return_path(f
);
2359 if (!mis
->to_src_file
) {
2360 error_report("CMD_OPEN_RETURN_PATH failed");
2366 tmp32
= qemu_get_be32(f
);
2367 trace_loadvm_process_command_ping(tmp32
);
2368 if (!mis
->to_src_file
) {
2369 error_report("CMD_PING (0x%x) received with no return path",
2373 migrate_send_rp_pong(mis
, tmp32
);
2376 case MIG_CMD_PACKAGED
:
2377 return loadvm_handle_cmd_packaged(mis
);
2379 case MIG_CMD_POSTCOPY_ADVISE
:
2380 return loadvm_postcopy_handle_advise(mis
, len
);
2382 case MIG_CMD_POSTCOPY_LISTEN
:
2383 return loadvm_postcopy_handle_listen(mis
);
2385 case MIG_CMD_POSTCOPY_RUN
:
2386 return loadvm_postcopy_handle_run(mis
);
2388 case MIG_CMD_POSTCOPY_RAM_DISCARD
:
2389 return loadvm_postcopy_ram_handle_discard(mis
, len
);
2391 case MIG_CMD_POSTCOPY_RESUME
:
2392 return loadvm_postcopy_handle_resume(mis
);
2394 case MIG_CMD_RECV_BITMAP
:
2395 return loadvm_handle_recv_bitmap(mis
, len
);
2397 case MIG_CMD_ENABLE_COLO
:
2398 return loadvm_process_enable_colo(mis
);
2405 * Read a footer off the wire and check that it matches the expected section
2407 * Returns: true if the footer was good
2408 * false if there is a problem (and calls error_report to say why)
2410 static bool check_section_footer(QEMUFile
*f
, SaveStateEntry
*se
)
2414 uint32_t read_section_id
;
2416 if (!migrate_get_current()->send_section_footer
) {
2417 /* No footer to check */
2421 read_mark
= qemu_get_byte(f
);
2423 ret
= qemu_file_get_error(f
);
2425 error_report("%s: Read section footer failed: %d",
2430 if (read_mark
!= QEMU_VM_SECTION_FOOTER
) {
2431 error_report("Missing section footer for %s", se
->idstr
);
2435 read_section_id
= qemu_get_be32(f
);
2436 if (read_section_id
!= se
->load_section_id
) {
2437 error_report("Mismatched section id in footer for %s -"
2438 " read 0x%x expected 0x%x",
2439 se
->idstr
, read_section_id
, se
->load_section_id
);
2448 qemu_loadvm_section_start_full(QEMUFile
*f
, MigrationIncomingState
*mis
)
2450 uint32_t instance_id
, version_id
, section_id
;
2455 /* Read section start */
2456 section_id
= qemu_get_be32(f
);
2457 if (!qemu_get_counted_string(f
, idstr
)) {
2458 error_report("Unable to read ID string for section %u",
2462 instance_id
= qemu_get_be32(f
);
2463 version_id
= qemu_get_be32(f
);
2465 ret
= qemu_file_get_error(f
);
2467 error_report("%s: Failed to read instance/version ID: %d",
2472 trace_qemu_loadvm_state_section_startfull(section_id
, idstr
,
2473 instance_id
, version_id
);
2474 /* Find savevm section */
2475 se
= find_se(idstr
, instance_id
);
2477 error_report("Unknown savevm section or instance '%s' %"PRIu32
". "
2478 "Make sure that your current VM setup matches your "
2479 "saved VM setup, including any hotplugged devices",
2480 idstr
, instance_id
);
2484 /* Validate version */
2485 if (version_id
> se
->version_id
) {
2486 error_report("savevm: unsupported version %d for '%s' v%d",
2487 version_id
, idstr
, se
->version_id
);
2490 se
->load_version_id
= version_id
;
2491 se
->load_section_id
= section_id
;
2493 /* Validate if it is a device's state */
2494 if (xen_enabled() && se
->is_ram
) {
2495 error_report("loadvm: %s RAM loading not allowed on Xen", idstr
);
2499 ret
= vmstate_load(f
, se
);
2501 error_report("error while loading state for instance 0x%"PRIx32
" of"
2502 " device '%s'", instance_id
, idstr
);
2505 if (!check_section_footer(f
, se
)) {
2513 qemu_loadvm_section_part_end(QEMUFile
*f
, MigrationIncomingState
*mis
)
2515 uint32_t section_id
;
2519 section_id
= qemu_get_be32(f
);
2521 ret
= qemu_file_get_error(f
);
2523 error_report("%s: Failed to read section ID: %d",
2528 trace_qemu_loadvm_state_section_partend(section_id
);
2529 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
2530 if (se
->load_section_id
== section_id
) {
2535 error_report("Unknown savevm section %d", section_id
);
2539 ret
= vmstate_load(f
, se
);
2541 error_report("error while loading state section id %d(%s)",
2542 section_id
, se
->idstr
);
2545 if (!check_section_footer(f
, se
)) {
2552 static int qemu_loadvm_state_header(QEMUFile
*f
)
2557 v
= qemu_get_be32(f
);
2558 if (v
!= QEMU_VM_FILE_MAGIC
) {
2559 error_report("Not a migration stream");
2563 v
= qemu_get_be32(f
);
2564 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
2565 error_report("SaveVM v2 format is obsolete and don't work anymore");
2568 if (v
!= QEMU_VM_FILE_VERSION
) {
2569 error_report("Unsupported migration stream version");
2573 if (migrate_get_current()->send_configuration
) {
2574 if (qemu_get_byte(f
) != QEMU_VM_CONFIGURATION
) {
2575 error_report("Configuration section missing");
2576 qemu_loadvm_state_cleanup();
2579 ret
= vmstate_load_state(f
, &vmstate_configuration
, &savevm_state
, 0);
2582 qemu_loadvm_state_cleanup();
2589 static int qemu_loadvm_state_setup(QEMUFile
*f
)
2594 trace_loadvm_state_setup();
2595 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
2596 if (!se
->ops
|| !se
->ops
->load_setup
) {
2599 if (se
->ops
->is_active
) {
2600 if (!se
->ops
->is_active(se
->opaque
)) {
2605 ret
= se
->ops
->load_setup(f
, se
->opaque
);
2607 qemu_file_set_error(f
, ret
);
2608 error_report("Load state of device %s failed", se
->idstr
);
2615 void qemu_loadvm_state_cleanup(void)
2619 trace_loadvm_state_cleanup();
2620 QTAILQ_FOREACH(se
, &savevm_state
.handlers
, entry
) {
2621 if (se
->ops
&& se
->ops
->load_cleanup
) {
2622 se
->ops
->load_cleanup(se
->opaque
);
2627 /* Return true if we should continue the migration, or false. */
2628 static bool postcopy_pause_incoming(MigrationIncomingState
*mis
)
2632 trace_postcopy_pause_incoming();
2634 assert(migrate_postcopy_ram());
2637 * Unregister yank with either from/to src would work, since ioc behind it
2640 migration_ioc_unregister_yank_from_file(mis
->from_src_file
);
2642 assert(mis
->from_src_file
);
2643 qemu_file_shutdown(mis
->from_src_file
);
2644 qemu_fclose(mis
->from_src_file
);
2645 mis
->from_src_file
= NULL
;
2647 assert(mis
->to_src_file
);
2648 qemu_file_shutdown(mis
->to_src_file
);
2649 qemu_mutex_lock(&mis
->rp_mutex
);
2650 qemu_fclose(mis
->to_src_file
);
2651 mis
->to_src_file
= NULL
;
2652 qemu_mutex_unlock(&mis
->rp_mutex
);
2655 * NOTE: this must happen before reset the PostcopyTmpPages below,
2656 * otherwise it's racy to reset those fields when the fast load thread
2657 * can be accessing it in parallel.
2659 if (mis
->postcopy_qemufile_dst
) {
2660 qemu_file_shutdown(mis
->postcopy_qemufile_dst
);
2661 /* Take the mutex to make sure the fast ram load thread halted */
2662 qemu_mutex_lock(&mis
->postcopy_prio_thread_mutex
);
2663 migration_ioc_unregister_yank_from_file(mis
->postcopy_qemufile_dst
);
2664 qemu_fclose(mis
->postcopy_qemufile_dst
);
2665 mis
->postcopy_qemufile_dst
= NULL
;
2666 qemu_mutex_unlock(&mis
->postcopy_prio_thread_mutex
);
2669 migrate_set_state(&mis
->state
, MIGRATION_STATUS_POSTCOPY_ACTIVE
,
2670 MIGRATION_STATUS_POSTCOPY_PAUSED
);
2672 /* Notify the fault thread for the invalidated file handle */
2673 postcopy_fault_thread_notify(mis
);
2676 * If network is interrupted, any temp page we received will be useless
2677 * because we didn't mark them as "received" in receivedmap. After a
2678 * proper recovery later (which will sync src dirty bitmap with receivedmap
2679 * on dest) these cached small pages will be resent again.
2681 for (i
= 0; i
< mis
->postcopy_channels
; i
++) {
2682 postcopy_temp_page_reset(&mis
->postcopy_tmp_pages
[i
]);
2685 error_report("Detected IO failure for postcopy. "
2686 "Migration paused.");
2688 while (mis
->state
== MIGRATION_STATUS_POSTCOPY_PAUSED
) {
2689 qemu_sem_wait(&mis
->postcopy_pause_sem_dst
);
2692 trace_postcopy_pause_incoming_continued();
2697 int qemu_loadvm_state_main(QEMUFile
*f
, MigrationIncomingState
*mis
)
2699 uint8_t section_type
;
2704 section_type
= qemu_get_byte(f
);
2706 ret
= qemu_file_get_error_obj_any(f
, mis
->postcopy_qemufile_dst
, NULL
);
2711 trace_qemu_loadvm_state_section(section_type
);
2712 switch (section_type
) {
2713 case QEMU_VM_SECTION_START
:
2714 case QEMU_VM_SECTION_FULL
:
2715 ret
= qemu_loadvm_section_start_full(f
, mis
);
2720 case QEMU_VM_SECTION_PART
:
2721 case QEMU_VM_SECTION_END
:
2722 ret
= qemu_loadvm_section_part_end(f
, mis
);
2727 case QEMU_VM_COMMAND
:
2728 ret
= loadvm_process_command(f
);
2729 trace_qemu_loadvm_state_section_command(ret
);
2730 if ((ret
< 0) || (ret
== LOADVM_QUIT
)) {
2735 /* This is the end of migration */
2738 error_report("Unknown savevm section type %d", section_type
);
2746 qemu_file_set_error(f
, ret
);
2748 /* Cancel bitmaps incoming regardless of recovery */
2749 dirty_bitmap_mig_cancel_incoming();
2752 * If we are during an active postcopy, then we pause instead
2753 * of bail out to at least keep the VM's dirty data. Note
2754 * that POSTCOPY_INCOMING_LISTENING stage is still not enough,
2755 * during which we're still receiving device states and we
2756 * still haven't yet started the VM on destination.
2758 * Only RAM postcopy supports recovery. Still, if RAM postcopy is
2759 * enabled, canceled bitmaps postcopy will not affect RAM postcopy
2762 if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING
&&
2763 migrate_postcopy_ram() && postcopy_pause_incoming(mis
)) {
2764 /* Reset f to point to the newly created channel */
2765 f
= mis
->from_src_file
;
2772 int qemu_loadvm_state(QEMUFile
*f
)
2774 MigrationIncomingState
*mis
= migration_incoming_get_current();
2775 Error
*local_err
= NULL
;
2778 if (qemu_savevm_state_blocked(&local_err
)) {
2779 error_report_err(local_err
);
2783 ret
= qemu_loadvm_state_header(f
);
2788 if (qemu_loadvm_state_setup(f
) != 0) {
2792 cpu_synchronize_all_pre_loadvm();
2794 ret
= qemu_loadvm_state_main(f
, mis
);
2795 qemu_event_set(&mis
->main_thread_load_event
);
2797 trace_qemu_loadvm_state_post_main(ret
);
2799 if (mis
->have_listen_thread
) {
2800 /* Listen thread still going, can't clean up yet */
2805 ret
= qemu_file_get_error(f
);
2809 * Try to read in the VMDESC section as well, so that dumping tools that
2810 * intercept our migration stream have the chance to see it.
2813 /* We've got to be careful; if we don't read the data and just shut the fd
2814 * then the sender can error if we close while it's still sending.
2815 * We also mustn't read data that isn't there; some transports (RDMA)
2816 * will stall waiting for that data when the source has already closed.
2818 if (ret
== 0 && should_send_vmdesc()) {
2821 uint8_t section_type
= qemu_get_byte(f
);
2823 if (section_type
!= QEMU_VM_VMDESCRIPTION
) {
2824 error_report("Expected vmdescription section, but got %d",
2827 * It doesn't seem worth failing at this point since
2828 * we apparently have an otherwise valid VM state
2831 buf
= g_malloc(0x1000);
2832 size
= qemu_get_be32(f
);
2835 uint32_t read_chunk
= MIN(size
, 0x1000);
2836 qemu_get_buffer(f
, buf
, read_chunk
);
2843 qemu_loadvm_state_cleanup();
2844 cpu_synchronize_all_post_init();
2849 int qemu_load_device_state(QEMUFile
*f
)
2851 MigrationIncomingState
*mis
= migration_incoming_get_current();
2854 /* Load QEMU_VM_SECTION_FULL section */
2855 ret
= qemu_loadvm_state_main(f
, mis
);
2857 error_report("Failed to load device state: %d", ret
);
2861 cpu_synchronize_all_post_init();
2865 bool save_snapshot(const char *name
, bool overwrite
, const char *vmstate
,
2866 bool has_devices
, strList
*devices
, Error
**errp
)
2868 BlockDriverState
*bs
;
2869 QEMUSnapshotInfo sn1
, *sn
= &sn1
;
2872 int saved_vm_running
;
2873 uint64_t vm_state_size
;
2874 g_autoptr(GDateTime
) now
= g_date_time_new_now_local();
2875 AioContext
*aio_context
;
2877 GLOBAL_STATE_CODE();
2879 if (migration_is_blocked(errp
)) {
2883 if (!replay_can_snapshot()) {
2884 error_setg(errp
, "Record/replay does not allow making snapshot "
2885 "right now. Try once more later.");
2889 if (!bdrv_all_can_snapshot(has_devices
, devices
, errp
)) {
2893 /* Delete old snapshots of the same name */
2896 if (bdrv_all_delete_snapshot(name
, has_devices
,
2897 devices
, errp
) < 0) {
2901 ret2
= bdrv_all_has_snapshot(name
, has_devices
, devices
, errp
);
2907 "Snapshot '%s' already exists in one or more devices",
2914 bs
= bdrv_all_find_vmstate_bs(vmstate
, has_devices
, devices
, errp
);
2918 aio_context
= bdrv_get_aio_context(bs
);
2920 saved_vm_running
= runstate_is_running();
2922 ret
= global_state_store();
2924 error_setg(errp
, "Error saving global state");
2927 vm_stop(RUN_STATE_SAVE_VM
);
2929 bdrv_drain_all_begin();
2931 aio_context_acquire(aio_context
);
2933 memset(sn
, 0, sizeof(*sn
));
2935 /* fill auxiliary fields */
2936 sn
->date_sec
= g_date_time_to_unix(now
);
2937 sn
->date_nsec
= g_date_time_get_microsecond(now
) * 1000;
2938 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2939 if (replay_mode
!= REPLAY_MODE_NONE
) {
2940 sn
->icount
= replay_get_current_icount();
2946 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
2948 g_autofree
char *autoname
= g_date_time_format(now
, "vm-%Y%m%d%H%M%S");
2949 pstrcpy(sn
->name
, sizeof(sn
->name
), autoname
);
2952 /* save the VM state */
2953 f
= qemu_fopen_bdrv(bs
, 1);
2955 error_setg(errp
, "Could not open VM state file");
2958 ret
= qemu_savevm_state(f
, errp
);
2959 vm_state_size
= qemu_file_transferred(f
);
2960 ret2
= qemu_fclose(f
);
2969 /* The bdrv_all_create_snapshot() call that follows acquires the AioContext
2970 * for itself. BDRV_POLL_WHILE() does not support nested locking because
2971 * it only releases the lock once. Therefore synchronous I/O will deadlock
2972 * unless we release the AioContext before bdrv_all_create_snapshot().
2974 aio_context_release(aio_context
);
2977 ret
= bdrv_all_create_snapshot(sn
, bs
, vm_state_size
,
2978 has_devices
, devices
, errp
);
2980 bdrv_all_delete_snapshot(sn
->name
, has_devices
, devices
, NULL
);
2988 aio_context_release(aio_context
);
2991 bdrv_drain_all_end();
2993 if (saved_vm_running
) {
2999 void qmp_xen_save_devices_state(const char *filename
, bool has_live
, bool live
,
3003 QIOChannelFile
*ioc
;
3004 int saved_vm_running
;
3008 /* live default to true so old version of Xen tool stack can have a
3009 * successful live migration */
3013 saved_vm_running
= runstate_is_running();
3014 vm_stop(RUN_STATE_SAVE_VM
);
3015 global_state_store_running();
3017 ioc
= qio_channel_file_new_path(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
,
3022 qio_channel_set_name(QIO_CHANNEL(ioc
), "migration-xen-save-state");
3023 f
= qemu_file_new_output(QIO_CHANNEL(ioc
));
3024 object_unref(OBJECT(ioc
));
3025 ret
= qemu_save_device_state(f
);
3026 if (ret
< 0 || qemu_fclose(f
) < 0) {
3027 error_setg(errp
, QERR_IO_ERROR
);
3029 /* libxl calls the QMP command "stop" before calling
3030 * "xen-save-devices-state" and in case of migration failure, libxl
3031 * would call "cont".
3032 * So call bdrv_inactivate_all (release locks) here to let the other
3033 * side of the migration take control of the images.
3035 if (live
&& !saved_vm_running
) {
3036 ret
= bdrv_inactivate_all();
3038 error_setg(errp
, "%s: bdrv_inactivate_all() failed (%d)",
3045 if (saved_vm_running
) {
3050 void qmp_xen_load_devices_state(const char *filename
, Error
**errp
)
3053 QIOChannelFile
*ioc
;
3056 /* Guest must be paused before loading the device state; the RAM state
3057 * will already have been loaded by xc
3059 if (runstate_is_running()) {
3060 error_setg(errp
, "Cannot update device state while vm is running");
3063 vm_stop(RUN_STATE_RESTORE_VM
);
3065 ioc
= qio_channel_file_new_path(filename
, O_RDONLY
| O_BINARY
, 0, errp
);
3069 qio_channel_set_name(QIO_CHANNEL(ioc
), "migration-xen-load-state");
3070 f
= qemu_file_new_input(QIO_CHANNEL(ioc
));
3071 object_unref(OBJECT(ioc
));
3073 ret
= qemu_loadvm_state(f
);
3076 error_setg(errp
, QERR_IO_ERROR
);
3078 migration_incoming_state_destroy();
3081 bool load_snapshot(const char *name
, const char *vmstate
,
3082 bool has_devices
, strList
*devices
, Error
**errp
)
3084 BlockDriverState
*bs_vm_state
;
3085 QEMUSnapshotInfo sn
;
3088 AioContext
*aio_context
;
3089 MigrationIncomingState
*mis
= migration_incoming_get_current();
3091 if (!bdrv_all_can_snapshot(has_devices
, devices
, errp
)) {
3094 ret
= bdrv_all_has_snapshot(name
, has_devices
, devices
, errp
);
3099 error_setg(errp
, "Snapshot '%s' does not exist in one or more devices",
3104 bs_vm_state
= bdrv_all_find_vmstate_bs(vmstate
, has_devices
, devices
, errp
);
3108 aio_context
= bdrv_get_aio_context(bs_vm_state
);
3110 /* Don't even try to load empty VM states */
3111 aio_context_acquire(aio_context
);
3112 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
3113 aio_context_release(aio_context
);
3116 } else if (sn
.vm_state_size
== 0) {
3117 error_setg(errp
, "This is a disk-only snapshot. Revert to it "
3118 " offline using qemu-img");
3123 * Flush the record/replay queue. Now the VM state is going
3124 * to change. Therefore we don't need to preserve its consistency
3126 replay_flush_events();
3128 /* Flush all IO requests so they don't interfere with the new state. */
3129 bdrv_drain_all_begin();
3131 ret
= bdrv_all_goto_snapshot(name
, has_devices
, devices
, errp
);
3136 /* restore the VM state */
3137 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
3139 error_setg(errp
, "Could not open VM state file");
3143 qemu_system_reset(SHUTDOWN_CAUSE_SNAPSHOT_LOAD
);
3144 mis
->from_src_file
= f
;
3146 if (!yank_register_instance(MIGRATION_YANK_INSTANCE
, errp
)) {
3150 aio_context_acquire(aio_context
);
3151 ret
= qemu_loadvm_state(f
);
3152 migration_incoming_state_destroy();
3153 aio_context_release(aio_context
);
3155 bdrv_drain_all_end();
3158 error_setg(errp
, "Error %d while loading VM state", ret
);
3165 bdrv_drain_all_end();
3169 bool delete_snapshot(const char *name
, bool has_devices
,
3170 strList
*devices
, Error
**errp
)
3172 if (!bdrv_all_can_snapshot(has_devices
, devices
, errp
)) {
3176 if (bdrv_all_delete_snapshot(name
, has_devices
, devices
, errp
) < 0) {
3183 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
3185 qemu_ram_set_idstr(mr
->ram_block
,
3186 memory_region_name(mr
), dev
);
3187 qemu_ram_set_migratable(mr
->ram_block
);
3190 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
3192 qemu_ram_unset_idstr(mr
->ram_block
);
3193 qemu_ram_unset_migratable(mr
->ram_block
);
3196 void vmstate_register_ram_global(MemoryRegion
*mr
)
3198 vmstate_register_ram(mr
, NULL
);
3201 bool vmstate_check_only_migratable(const VMStateDescription
*vmsd
)
3203 /* check needed if --only-migratable is specified */
3204 if (!only_migratable
) {
3208 return !(vmsd
&& vmsd
->unmigratable
);
3211 typedef struct SnapshotJob
{
3221 static void qmp_snapshot_job_free(SnapshotJob
*s
)
3225 qapi_free_strList(s
->devices
);
3229 static void snapshot_load_job_bh(void *opaque
)
3232 SnapshotJob
*s
= container_of(job
, SnapshotJob
, common
);
3233 int orig_vm_running
;
3235 job_progress_set_remaining(&s
->common
, 1);
3237 orig_vm_running
= runstate_is_running();
3238 vm_stop(RUN_STATE_RESTORE_VM
);
3240 s
->ret
= load_snapshot(s
->tag
, s
->vmstate
, true, s
->devices
, s
->errp
);
3241 if (s
->ret
&& orig_vm_running
) {
3245 job_progress_update(&s
->common
, 1);
3247 qmp_snapshot_job_free(s
);
3251 static void snapshot_save_job_bh(void *opaque
)
3254 SnapshotJob
*s
= container_of(job
, SnapshotJob
, common
);
3256 job_progress_set_remaining(&s
->common
, 1);
3257 s
->ret
= save_snapshot(s
->tag
, false, s
->vmstate
,
3258 true, s
->devices
, s
->errp
);
3259 job_progress_update(&s
->common
, 1);
3261 qmp_snapshot_job_free(s
);
3265 static void snapshot_delete_job_bh(void *opaque
)
3268 SnapshotJob
*s
= container_of(job
, SnapshotJob
, common
);
3270 job_progress_set_remaining(&s
->common
, 1);
3271 s
->ret
= delete_snapshot(s
->tag
, true, s
->devices
, s
->errp
);
3272 job_progress_update(&s
->common
, 1);
3274 qmp_snapshot_job_free(s
);
3278 static int coroutine_fn
snapshot_save_job_run(Job
*job
, Error
**errp
)
3280 SnapshotJob
*s
= container_of(job
, SnapshotJob
, common
);
3282 s
->co
= qemu_coroutine_self();
3283 aio_bh_schedule_oneshot(qemu_get_aio_context(),
3284 snapshot_save_job_bh
, job
);
3285 qemu_coroutine_yield();
3286 return s
->ret
? 0 : -1;
3289 static int coroutine_fn
snapshot_load_job_run(Job
*job
, Error
**errp
)
3291 SnapshotJob
*s
= container_of(job
, SnapshotJob
, common
);
3293 s
->co
= qemu_coroutine_self();
3294 aio_bh_schedule_oneshot(qemu_get_aio_context(),
3295 snapshot_load_job_bh
, job
);
3296 qemu_coroutine_yield();
3297 return s
->ret
? 0 : -1;
3300 static int coroutine_fn
snapshot_delete_job_run(Job
*job
, Error
**errp
)
3302 SnapshotJob
*s
= container_of(job
, SnapshotJob
, common
);
3304 s
->co
= qemu_coroutine_self();
3305 aio_bh_schedule_oneshot(qemu_get_aio_context(),
3306 snapshot_delete_job_bh
, job
);
3307 qemu_coroutine_yield();
3308 return s
->ret
? 0 : -1;
3312 static const JobDriver snapshot_load_job_driver
= {
3313 .instance_size
= sizeof(SnapshotJob
),
3314 .job_type
= JOB_TYPE_SNAPSHOT_LOAD
,
3315 .run
= snapshot_load_job_run
,
3318 static const JobDriver snapshot_save_job_driver
= {
3319 .instance_size
= sizeof(SnapshotJob
),
3320 .job_type
= JOB_TYPE_SNAPSHOT_SAVE
,
3321 .run
= snapshot_save_job_run
,
3324 static const JobDriver snapshot_delete_job_driver
= {
3325 .instance_size
= sizeof(SnapshotJob
),
3326 .job_type
= JOB_TYPE_SNAPSHOT_DELETE
,
3327 .run
= snapshot_delete_job_run
,
3331 void qmp_snapshot_save(const char *job_id
,
3333 const char *vmstate
,
3339 s
= job_create(job_id
, &snapshot_save_job_driver
, NULL
,
3340 qemu_get_aio_context(), JOB_MANUAL_DISMISS
,
3346 s
->tag
= g_strdup(tag
);
3347 s
->vmstate
= g_strdup(vmstate
);
3348 s
->devices
= QAPI_CLONE(strList
, devices
);
3350 job_start(&s
->common
);
3353 void qmp_snapshot_load(const char *job_id
,
3355 const char *vmstate
,
3361 s
= job_create(job_id
, &snapshot_load_job_driver
, NULL
,
3362 qemu_get_aio_context(), JOB_MANUAL_DISMISS
,
3368 s
->tag
= g_strdup(tag
);
3369 s
->vmstate
= g_strdup(vmstate
);
3370 s
->devices
= QAPI_CLONE(strList
, devices
);
3372 job_start(&s
->common
);
3375 void qmp_snapshot_delete(const char *job_id
,
3382 s
= job_create(job_id
, &snapshot_delete_job_driver
, NULL
,
3383 qemu_get_aio_context(), JOB_MANUAL_DISMISS
,
3389 s
->tag
= g_strdup(tag
);
3390 s
->devices
= QAPI_CLONE(strList
, devices
);
3392 job_start(&s
->common
);