]>
Commit | Line | Data |
---|---|---|
a672b469 AL |
1 | /* |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
76cc7b58 JQ |
5 | * Copyright (c) 2009-2015 Red Hat Inc |
6 | * | |
7 | * Authors: | |
8 | * Juan Quintela <quintela@redhat.com> | |
a672b469 AL |
9 | * |
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: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
23 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
26 | * THE SOFTWARE. | |
27 | */ | |
a672b469 | 28 | |
1393a485 | 29 | #include "qemu/osdep.h" |
abfd9ce3 | 30 | #include "hw/boards.h" |
1422e32d | 31 | #include "net/net.h" |
6666c96a | 32 | #include "migration.h" |
5e22479a | 33 | #include "migration/snapshot.h" |
947701cc | 34 | #include "migration-stats.h" |
d6454270 | 35 | #include "migration/vmstate.h" |
f8d806c9 | 36 | #include "migration/misc.h" |
f2a8f0a6 | 37 | #include "migration/register.h" |
84a899de | 38 | #include "migration/global_state.h" |
67bdabe2 | 39 | #include "migration/channel-block.h" |
7b1e1a22 | 40 | #include "ram.h" |
08a0aee1 | 41 | #include "qemu-file.h" |
20a519a0 | 42 | #include "savevm.h" |
be07b0ac | 43 | #include "postcopy-ram.h" |
e688df6b | 44 | #include "qapi/error.h" |
112ed241 | 45 | #include "qapi/qapi-commands-migration.h" |
f1a9fcdd DB |
46 | #include "qapi/clone-visitor.h" |
47 | #include "qapi/qapi-builtin-visit.h" | |
cc7a8ea7 | 48 | #include "qapi/qmp/qerror.h" |
d49b6836 | 49 | #include "qemu/error-report.h" |
9c17d615 | 50 | #include "sysemu/cpus.h" |
022c62cb | 51 | #include "exec/memory.h" |
51180423 | 52 | #include "exec/target_page.h" |
517a13c9 | 53 | #include "trace.h" |
28085f7b | 54 | #include "qemu/iov.h" |
e2c1c34f | 55 | #include "qemu/job.h" |
db725815 | 56 | #include "qemu/main-loop.h" |
de08c606 | 57 | #include "block/snapshot.h" |
f348b6d1 | 58 | #include "qemu/cutils.h" |
61b67d47 | 59 | #include "io/channel-buffer.h" |
8925839f | 60 | #include "io/channel-file.h" |
377b21cc | 61 | #include "sysemu/replay.h" |
54d31236 | 62 | #include "sysemu/runstate.h" |
46517dd4 | 63 | #include "sysemu/sysemu.h" |
da278d58 | 64 | #include "sysemu/xen.h" |
aad555c2 | 65 | #include "migration/colo.h" |
6cafc8e4 | 66 | #include "qemu/bitmap.h" |
7659505c | 67 | #include "net/announce.h" |
b5eea99e | 68 | #include "qemu/yank.h" |
39675fff | 69 | #include "yank_functions.h" |
bb25a728 | 70 | #include "sysemu/qtest.h" |
1f0776f1 | 71 | #include "options.h" |
18995b98 | 72 | |
49324e93 | 73 | const unsigned int postcopy_ram_discard_version; |
093e3c42 | 74 | |
20a519a0 JQ |
75 | /* Subcommands for QEMU_VM_COMMAND */ |
76 | enum qemu_vm_cmd { | |
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 */ | |
80 | ||
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 */ | |
86 | ||
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 */ | |
aad555c2 | 91 | MIG_CMD_ENABLE_COLO, /* Enable COLO */ |
858b6d62 | 92 | MIG_CMD_POSTCOPY_RESUME, /* resume postcopy on dest */ |
f25d4225 | 93 | MIG_CMD_RECV_BITMAP, /* Request for recved bitmap on dst */ |
20a519a0 JQ |
94 | MIG_CMD_MAX |
95 | }; | |
96 | ||
ee555cdf | 97 | #define MAX_VM_CMD_PACKAGED_SIZE UINT32_MAX |
c76ca188 DDAG |
98 | static struct mig_cmd_args { |
99 | ssize_t len; /* -1 = variable */ | |
100 | const char *name; | |
101 | } mig_cmd_args[] = { | |
102 | [MIG_CMD_INVALID] = { .len = -1, .name = "INVALID" }, | |
2e37701e DDAG |
103 | [MIG_CMD_OPEN_RETURN_PATH] = { .len = 0, .name = "OPEN_RETURN_PATH" }, |
104 | [MIG_CMD_PING] = { .len = sizeof(uint32_t), .name = "PING" }, | |
58110f0a | 105 | [MIG_CMD_POSTCOPY_ADVISE] = { .len = -1, .name = "POSTCOPY_ADVISE" }, |
093e3c42 DDAG |
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" }, | |
3f5875ec | 110 | [MIG_CMD_POSTCOPY_RESUME] = { .len = 0, .name = "POSTCOPY_RESUME" }, |
11cf1d98 | 111 | [MIG_CMD_PACKAGED] = { .len = 4, .name = "PACKAGED" }, |
f25d4225 | 112 | [MIG_CMD_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" }, |
c76ca188 DDAG |
113 | [MIG_CMD_MAX] = { .len = -1, .name = "MAX" }, |
114 | }; | |
115 | ||
58110f0a VSO |
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 | |
d8b71d96 | 120 | * uint64_t target page size |
58110f0a VSO |
121 | * |
122 | * - postcopy RAM and postcopy dirty bitmaps | |
123 | * format is the same as for postcopy RAM only | |
124 | * | |
125 | * - postcopy dirty bitmaps only | |
126 | * Nothing. Command length field is 0. | |
127 | * | |
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. | |
131 | */ | |
132 | ||
a672b469 AL |
133 | /***********************************************************/ |
134 | /* savevm/loadvm support */ | |
135 | ||
45566e9c | 136 | static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable) |
a672b469 | 137 | { |
38ff78d3 | 138 | if (is_writable) { |
77ef2dc1 | 139 | return qemu_file_new_output(QIO_CHANNEL(qio_channel_block_new(bs))); |
67bdabe2 | 140 | } else { |
77ef2dc1 | 141 | return qemu_file_new_input(QIO_CHANNEL(qio_channel_block_new(bs))); |
38ff78d3 | 142 | } |
a672b469 AL |
143 | } |
144 | ||
2ff68d07 | 145 | |
bb1a6d8c EH |
146 | /* QEMUFile timer support. |
147 | * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c | |
148 | */ | |
2ff68d07 | 149 | |
40daca54 | 150 | void timer_put(QEMUFile *f, QEMUTimer *ts) |
2ff68d07 PB |
151 | { |
152 | uint64_t expire_time; | |
153 | ||
e93379b0 | 154 | expire_time = timer_expire_time_ns(ts); |
2ff68d07 PB |
155 | qemu_put_be64(f, expire_time); |
156 | } | |
157 | ||
40daca54 | 158 | void timer_get(QEMUFile *f, QEMUTimer *ts) |
2ff68d07 PB |
159 | { |
160 | uint64_t expire_time; | |
161 | ||
162 | expire_time = qemu_get_be64(f); | |
163 | if (expire_time != -1) { | |
bc72ad67 | 164 | timer_mod_ns(ts, expire_time); |
2ff68d07 | 165 | } else { |
bc72ad67 | 166 | timer_del(ts); |
2ff68d07 PB |
167 | } |
168 | } | |
169 | ||
170 | ||
bb1a6d8c EH |
171 | /* VMState timer support. |
172 | * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c | |
173 | */ | |
dde0463b | 174 | |
03fee66f MAL |
175 | static int get_timer(QEMUFile *f, void *pv, size_t size, |
176 | const VMStateField *field) | |
dde0463b JQ |
177 | { |
178 | QEMUTimer *v = pv; | |
40daca54 | 179 | timer_get(f, v); |
dde0463b JQ |
180 | return 0; |
181 | } | |
182 | ||
03fee66f | 183 | static int put_timer(QEMUFile *f, void *pv, size_t size, |
3ddba9a9 | 184 | const VMStateField *field, JSONWriter *vmdesc) |
dde0463b | 185 | { |
84e2e3eb | 186 | QEMUTimer *v = pv; |
40daca54 | 187 | timer_put(f, v); |
2c21ee76 JD |
188 | |
189 | return 0; | |
dde0463b JQ |
190 | } |
191 | ||
192 | const VMStateInfo vmstate_info_timer = { | |
193 | .name = "timer", | |
194 | .get = get_timer, | |
195 | .put = put_timer, | |
196 | }; | |
197 | ||
08e99e29 | 198 | |
7685ee6a AW |
199 | typedef struct CompatEntry { |
200 | char idstr[256]; | |
201 | int instance_id; | |
202 | } CompatEntry; | |
203 | ||
a672b469 | 204 | typedef struct SaveStateEntry { |
72cf2d4f | 205 | QTAILQ_ENTRY(SaveStateEntry) entry; |
a672b469 | 206 | char idstr[256]; |
93062e23 | 207 | uint32_t instance_id; |
4d2ffa08 | 208 | int alias_id; |
a672b469 | 209 | int version_id; |
0f42f657 JQ |
210 | /* version id read from the stream */ |
211 | int load_version_id; | |
a672b469 | 212 | int section_id; |
0f42f657 JQ |
213 | /* section id read from the stream */ |
214 | int load_section_id; | |
de22ded0 | 215 | const SaveVMHandlers *ops; |
9ed7d6ae | 216 | const VMStateDescription *vmsd; |
a672b469 | 217 | void *opaque; |
7685ee6a | 218 | CompatEntry *compat; |
a7ae8355 | 219 | int is_ram; |
a672b469 AL |
220 | } SaveStateEntry; |
221 | ||
0163a2e0 JQ |
222 | typedef struct SaveState { |
223 | QTAILQ_HEAD(, SaveStateEntry) handlers; | |
174723ff | 224 | SaveStateEntry *handler_pri_head[MIG_PRI_MAX + 1]; |
0163a2e0 | 225 | int global_section_id; |
61964c23 JQ |
226 | uint32_t len; |
227 | const char *name; | |
59811a32 | 228 | uint32_t target_page_bits; |
6cafc8e4 YK |
229 | uint32_t caps_count; |
230 | MigrationCapability *capabilities; | |
b9d68df6 | 231 | QemuUUID uuid; |
0163a2e0 JQ |
232 | } SaveState; |
233 | ||
234 | static SaveState savevm_state = { | |
235 | .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers), | |
174723ff | 236 | .handler_pri_head = { [MIG_PRI_DEFAULT ... MIG_PRI_MAX] = NULL }, |
0163a2e0 | 237 | .global_section_id = 0, |
61964c23 JQ |
238 | }; |
239 | ||
6cafc8e4 YK |
240 | static bool should_validate_capability(int capability) |
241 | { | |
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: | |
246 | return true; | |
247 | default: | |
248 | return false; | |
249 | } | |
250 | } | |
251 | ||
252 | static uint32_t get_validatable_capabilities_count(void) | |
253 | { | |
254 | MigrationState *s = migrate_get_current(); | |
255 | uint32_t result = 0; | |
256 | int i; | |
257 | for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { | |
0cec2056 | 258 | if (should_validate_capability(i) && s->capabilities[i]) { |
6cafc8e4 YK |
259 | result++; |
260 | } | |
261 | } | |
262 | return result; | |
263 | } | |
264 | ||
44b1ff31 | 265 | static int configuration_pre_save(void *opaque) |
61964c23 JQ |
266 | { |
267 | SaveState *state = opaque; | |
268 | const char *current_name = MACHINE_GET_CLASS(current_machine)->name; | |
6cafc8e4 YK |
269 | MigrationState *s = migrate_get_current(); |
270 | int i, j; | |
61964c23 JQ |
271 | |
272 | state->len = strlen(current_name); | |
273 | state->name = current_name; | |
46d702b1 | 274 | state->target_page_bits = qemu_target_page_bits(); |
44b1ff31 | 275 | |
6cafc8e4 YK |
276 | state->caps_count = get_validatable_capabilities_count(); |
277 | state->capabilities = g_renew(MigrationCapability, state->capabilities, | |
278 | state->caps_count); | |
279 | for (i = j = 0; i < MIGRATION_CAPABILITY__MAX; i++) { | |
0cec2056 | 280 | if (should_validate_capability(i) && s->capabilities[i]) { |
6cafc8e4 YK |
281 | state->capabilities[j++] = i; |
282 | } | |
283 | } | |
b9d68df6 | 284 | state->uuid = qemu_uuid; |
6cafc8e4 | 285 | |
44b1ff31 | 286 | return 0; |
59811a32 PM |
287 | } |
288 | ||
39f633d4 JG |
289 | static int configuration_post_save(void *opaque) |
290 | { | |
291 | SaveState *state = opaque; | |
292 | ||
293 | g_free(state->capabilities); | |
294 | state->capabilities = NULL; | |
295 | state->caps_count = 0; | |
296 | return 0; | |
297 | } | |
298 | ||
59811a32 PM |
299 | static int configuration_pre_load(void *opaque) |
300 | { | |
301 | SaveState *state = opaque; | |
302 | ||
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. | |
306 | */ | |
46d702b1 | 307 | state->target_page_bits = qemu_target_page_bits_min(); |
59811a32 | 308 | return 0; |
61964c23 JQ |
309 | } |
310 | ||
6cafc8e4 YK |
311 | static bool configuration_validate_capabilities(SaveState *state) |
312 | { | |
313 | bool ret = true; | |
314 | MigrationState *s = migrate_get_current(); | |
315 | unsigned long *source_caps_bm; | |
316 | int i; | |
317 | ||
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); | |
322 | } | |
323 | ||
324 | for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { | |
325 | bool source_state, target_state; | |
326 | if (!should_validate_capability(i)) { | |
327 | continue; | |
328 | } | |
329 | source_state = test_bit(i, source_caps_bm); | |
0cec2056 | 330 | target_state = s->capabilities[i]; |
6cafc8e4 YK |
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"); | |
336 | ret = false; | |
337 | /* Don't break here to report all failed capabilities */ | |
338 | } | |
339 | } | |
340 | ||
341 | g_free(source_caps_bm); | |
342 | return ret; | |
343 | } | |
344 | ||
61964c23 JQ |
345 | static int configuration_post_load(void *opaque, int version_id) |
346 | { | |
347 | SaveState *state = opaque; | |
348 | const char *current_name = MACHINE_GET_CLASS(current_machine)->name; | |
39f633d4 | 349 | int ret = 0; |
61964c23 JQ |
350 | |
351 | if (strncmp(state->name, current_name, state->len) != 0) { | |
15d61692 GK |
352 | error_report("Machine type received is '%.*s' and local is '%s'", |
353 | (int) state->len, state->name, current_name); | |
39f633d4 JG |
354 | ret = -EINVAL; |
355 | goto out; | |
61964c23 | 356 | } |
59811a32 | 357 | |
46d702b1 | 358 | if (state->target_page_bits != qemu_target_page_bits()) { |
59811a32 | 359 | error_report("Received TARGET_PAGE_BITS is %d but local is %d", |
46d702b1 | 360 | state->target_page_bits, qemu_target_page_bits()); |
39f633d4 JG |
361 | ret = -EINVAL; |
362 | goto out; | |
59811a32 PM |
363 | } |
364 | ||
6cafc8e4 | 365 | if (!configuration_validate_capabilities(state)) { |
39f633d4 JG |
366 | ret = -EINVAL; |
367 | goto out; | |
6cafc8e4 YK |
368 | } |
369 | ||
39f633d4 JG |
370 | out: |
371 | g_free((void *)state->name); | |
372 | state->name = NULL; | |
373 | state->len = 0; | |
374 | g_free(state->capabilities); | |
375 | state->capabilities = NULL; | |
376 | state->caps_count = 0; | |
377 | ||
378 | return ret; | |
61964c23 JQ |
379 | } |
380 | ||
6cafc8e4 YK |
381 | static int get_capability(QEMUFile *f, void *pv, size_t size, |
382 | const VMStateField *field) | |
383 | { | |
384 | MigrationCapability *capability = pv; | |
385 | char capability_str[UINT8_MAX + 1]; | |
386 | uint8_t len; | |
387 | int i; | |
388 | ||
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)) { | |
394 | *capability = i; | |
395 | return 0; | |
396 | } | |
397 | } | |
398 | error_report("Received unknown capability %s", capability_str); | |
399 | return -EINVAL; | |
400 | } | |
401 | ||
402 | static int put_capability(QEMUFile *f, void *pv, size_t size, | |
3ddba9a9 | 403 | const VMStateField *field, JSONWriter *vmdesc) |
6cafc8e4 YK |
404 | { |
405 | MigrationCapability *capability = pv; | |
406 | const char *capability_str = MigrationCapability_str(*capability); | |
407 | size_t len = strlen(capability_str); | |
408 | assert(len <= UINT8_MAX); | |
409 | ||
410 | qemu_put_byte(f, len); | |
411 | qemu_put_buffer(f, (uint8_t *)capability_str, len); | |
412 | return 0; | |
413 | } | |
414 | ||
415 | static const VMStateInfo vmstate_info_capability = { | |
416 | .name = "capability", | |
417 | .get = get_capability, | |
418 | .put = put_capability, | |
419 | }; | |
420 | ||
59811a32 PM |
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. | |
427 | */ | |
428 | static bool vmstate_target_page_bits_needed(void *opaque) | |
429 | { | |
46d702b1 JQ |
430 | return qemu_target_page_bits() |
431 | > qemu_target_page_bits_min(); | |
59811a32 PM |
432 | } |
433 | ||
434 | static const VMStateDescription vmstate_target_page_bits = { | |
435 | .name = "configuration/target-page-bits", | |
436 | .version_id = 1, | |
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() | |
442 | } | |
443 | }; | |
444 | ||
6cafc8e4 YK |
445 | static bool vmstate_capabilites_needed(void *opaque) |
446 | { | |
447 | return get_validatable_capabilities_count() > 0; | |
448 | } | |
449 | ||
450 | static const VMStateDescription vmstate_capabilites = { | |
451 | .name = "configuration/capabilities", | |
452 | .version_id = 1, | |
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() | |
461 | } | |
462 | }; | |
463 | ||
b9d68df6 YK |
464 | static bool vmstate_uuid_needed(void *opaque) |
465 | { | |
466 | return qemu_uuid_set && migrate_validate_uuid(); | |
467 | } | |
468 | ||
469 | static int vmstate_uuid_post_load(void *opaque, int version_id) | |
470 | { | |
471 | SaveState *state = opaque; | |
472 | char uuid_src[UUID_FMT_LEN + 1]; | |
473 | char uuid_dst[UUID_FMT_LEN + 1]; | |
474 | ||
475 | if (!qemu_uuid_set) { | |
476 | /* | |
477 | * It's warning because user might not know UUID in some cases, | |
478 | * e.g. load an old snapshot | |
479 | */ | |
480 | qemu_uuid_unparse(&state->uuid, uuid_src); | |
481 | warn_report("UUID is received %s, but local uuid isn't set", | |
482 | uuid_src); | |
483 | return 0; | |
484 | } | |
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); | |
489 | return -EINVAL; | |
490 | } | |
491 | return 0; | |
492 | } | |
493 | ||
494 | static const VMStateDescription vmstate_uuid = { | |
495 | .name = "configuration/uuid", | |
496 | .version_id = 1, | |
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() | |
503 | } | |
504 | }; | |
505 | ||
61964c23 JQ |
506 | static const VMStateDescription vmstate_configuration = { |
507 | .name = "configuration", | |
508 | .version_id = 1, | |
59811a32 | 509 | .pre_load = configuration_pre_load, |
61964c23 JQ |
510 | .post_load = configuration_post_load, |
511 | .pre_save = configuration_pre_save, | |
39f633d4 | 512 | .post_save = configuration_post_save, |
61964c23 JQ |
513 | .fields = (VMStateField[]) { |
514 | VMSTATE_UINT32(len, SaveState), | |
59046ec2 | 515 | VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, len), |
61964c23 JQ |
516 | VMSTATE_END_OF_LIST() |
517 | }, | |
395cb450 | 518 | .subsections = (const VMStateDescription *[]) { |
59811a32 | 519 | &vmstate_target_page_bits, |
6cafc8e4 | 520 | &vmstate_capabilites, |
b9d68df6 | 521 | &vmstate_uuid, |
59811a32 PM |
522 | NULL |
523 | } | |
0163a2e0 | 524 | }; |
a672b469 | 525 | |
abfd9ce3 AS |
526 | static void dump_vmstate_vmsd(FILE *out_file, |
527 | const VMStateDescription *vmsd, int indent, | |
528 | bool is_subsection); | |
529 | ||
530 | static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field, | |
531 | int indent) | |
532 | { | |
533 | fprintf(out_file, "%*s{\n", indent, ""); | |
534 | indent += 2; | |
535 | fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name); | |
536 | fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "", | |
537 | field->version_id); | |
538 | fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "", | |
539 | field->field_exists ? "true" : "false"); | |
12c81e5a PX |
540 | if (field->flags & VMS_ARRAY) { |
541 | fprintf(out_file, "%*s\"num\": %d,\n", indent, "", field->num); | |
542 | } | |
abfd9ce3 AS |
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); | |
547 | } | |
548 | fprintf(out_file, "\n%*s}", indent - 2, ""); | |
549 | } | |
550 | ||
551 | static void dump_vmstate_vmss(FILE *out_file, | |
5cd8cada | 552 | const VMStateDescription **subsection, |
abfd9ce3 AS |
553 | int indent) |
554 | { | |
5cd8cada JQ |
555 | if (*subsection != NULL) { |
556 | dump_vmstate_vmsd(out_file, *subsection, indent, true); | |
abfd9ce3 AS |
557 | } |
558 | } | |
559 | ||
560 | static void dump_vmstate_vmsd(FILE *out_file, | |
561 | const VMStateDescription *vmsd, int indent, | |
562 | bool is_subsection) | |
563 | { | |
564 | if (is_subsection) { | |
565 | fprintf(out_file, "%*s{\n", indent, ""); | |
566 | } else { | |
567 | fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description"); | |
568 | } | |
569 | indent += 2; | |
570 | fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name); | |
571 | fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "", | |
572 | vmsd->version_id); | |
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; | |
577 | bool first; | |
578 | ||
579 | fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, ""); | |
580 | first = true; | |
581 | while (field->name != NULL) { | |
582 | if (field->flags & VMS_MUST_EXIST) { | |
583 | /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */ | |
584 | field++; | |
585 | continue; | |
586 | } | |
587 | if (!first) { | |
588 | fprintf(out_file, ",\n"); | |
589 | } | |
590 | dump_vmstate_vmsf(out_file, field, indent + 2); | |
591 | field++; | |
592 | first = false; | |
593 | } | |
89c56848 | 594 | assert(field->flags == VMS_END); |
abfd9ce3 AS |
595 | fprintf(out_file, "\n%*s]", indent, ""); |
596 | } | |
597 | if (vmsd->subsections != NULL) { | |
5cd8cada | 598 | const VMStateDescription **subsection = vmsd->subsections; |
abfd9ce3 AS |
599 | bool first; |
600 | ||
601 | fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, ""); | |
602 | first = true; | |
5cd8cada | 603 | while (*subsection != NULL) { |
abfd9ce3 AS |
604 | if (!first) { |
605 | fprintf(out_file, ",\n"); | |
606 | } | |
607 | dump_vmstate_vmss(out_file, subsection, indent + 2); | |
608 | subsection++; | |
609 | first = false; | |
610 | } | |
611 | fprintf(out_file, "\n%*s]", indent, ""); | |
612 | } | |
613 | fprintf(out_file, "\n%*s}", indent - 2, ""); | |
614 | } | |
615 | ||
616 | static void dump_machine_type(FILE *out_file) | |
617 | { | |
618 | MachineClass *mc; | |
619 | ||
620 | mc = MACHINE_GET_CLASS(current_machine); | |
621 | ||
622 | fprintf(out_file, " \"vmschkmachine\": {\n"); | |
623 | fprintf(out_file, " \"Name\": \"%s\"\n", mc->name); | |
624 | fprintf(out_file, " },\n"); | |
625 | } | |
626 | ||
627 | void dump_vmstate_json_to_file(FILE *out_file) | |
628 | { | |
629 | GSList *list, *elt; | |
630 | bool first; | |
631 | ||
632 | fprintf(out_file, "{\n"); | |
633 | dump_machine_type(out_file); | |
634 | ||
635 | first = true; | |
636 | list = object_class_get_list(TYPE_DEVICE, true); | |
637 | for (elt = list; elt; elt = elt->next) { | |
638 | DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data, | |
639 | TYPE_DEVICE); | |
640 | const char *name; | |
641 | int indent = 2; | |
642 | ||
643 | if (!dc->vmsd) { | |
644 | continue; | |
645 | } | |
646 | ||
647 | if (!first) { | |
648 | fprintf(out_file, ",\n"); | |
649 | } | |
650 | name = object_class_get_name(OBJECT_CLASS(dc)); | |
651 | fprintf(out_file, "%*s\"%s\": {\n", indent, "", name); | |
652 | indent += 2; | |
653 | fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name); | |
654 | fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "", | |
655 | dc->vmsd->version_id); | |
656 | fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "", | |
657 | dc->vmsd->minimum_version_id); | |
658 | ||
659 | dump_vmstate_vmsd(out_file, dc->vmsd, indent, false); | |
660 | ||
661 | fprintf(out_file, "\n%*s}", indent - 2, ""); | |
662 | first = false; | |
663 | } | |
664 | fprintf(out_file, "\n}\n"); | |
665 | fclose(out_file); | |
26daeba4 | 666 | g_slist_free(list); |
abfd9ce3 AS |
667 | } |
668 | ||
93062e23 | 669 | static uint32_t calculate_new_instance_id(const char *idstr) |
8718e999 JQ |
670 | { |
671 | SaveStateEntry *se; | |
93062e23 | 672 | uint32_t instance_id = 0; |
8718e999 | 673 | |
0163a2e0 | 674 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
8718e999 JQ |
675 | if (strcmp(idstr, se->idstr) == 0 |
676 | && instance_id <= se->instance_id) { | |
677 | instance_id = se->instance_id + 1; | |
678 | } | |
679 | } | |
93062e23 PX |
680 | /* Make sure we never loop over without being noticed */ |
681 | assert(instance_id != VMSTATE_INSTANCE_ID_ANY); | |
8718e999 JQ |
682 | return instance_id; |
683 | } | |
684 | ||
7685ee6a AW |
685 | static int calculate_compat_instance_id(const char *idstr) |
686 | { | |
687 | SaveStateEntry *se; | |
688 | int instance_id = 0; | |
689 | ||
0163a2e0 | 690 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
38ff78d3 | 691 | if (!se->compat) { |
7685ee6a | 692 | continue; |
38ff78d3 | 693 | } |
7685ee6a AW |
694 | |
695 | if (strcmp(idstr, se->compat->idstr) == 0 | |
696 | && instance_id <= se->compat->instance_id) { | |
697 | instance_id = se->compat->instance_id + 1; | |
698 | } | |
699 | } | |
700 | return instance_id; | |
701 | } | |
702 | ||
f37bc036 PX |
703 | static inline MigrationPriority save_state_priority(SaveStateEntry *se) |
704 | { | |
705 | if (se->vmsd) { | |
706 | return se->vmsd->priority; | |
707 | } | |
708 | return MIG_PRI_DEFAULT; | |
709 | } | |
710 | ||
711 | static void savevm_state_handler_insert(SaveStateEntry *nse) | |
712 | { | |
713 | MigrationPriority priority = save_state_priority(nse); | |
714 | SaveStateEntry *se; | |
174723ff | 715 | int i; |
f37bc036 PX |
716 | |
717 | assert(priority <= MIG_PRI_MAX); | |
718 | ||
174723ff SC |
719 | for (i = priority - 1; i >= 0; i--) { |
720 | se = savevm_state.handler_pri_head[i]; | |
721 | if (se != NULL) { | |
722 | assert(save_state_priority(se) < priority); | |
f37bc036 PX |
723 | break; |
724 | } | |
725 | } | |
726 | ||
174723ff | 727 | if (i >= 0) { |
f37bc036 PX |
728 | QTAILQ_INSERT_BEFORE(se, nse, entry); |
729 | } else { | |
730 | QTAILQ_INSERT_TAIL(&savevm_state.handlers, nse, entry); | |
731 | } | |
174723ff SC |
732 | |
733 | if (savevm_state.handler_pri_head[priority] == NULL) { | |
734 | savevm_state.handler_pri_head[priority] = nse; | |
735 | } | |
f37bc036 PX |
736 | } |
737 | ||
bd5de61e SC |
738 | static void savevm_state_handler_remove(SaveStateEntry *se) |
739 | { | |
174723ff SC |
740 | SaveStateEntry *next; |
741 | MigrationPriority priority = save_state_priority(se); | |
742 | ||
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; | |
747 | } else { | |
748 | savevm_state.handler_pri_head[priority] = NULL; | |
749 | } | |
750 | } | |
bd5de61e SC |
751 | QTAILQ_REMOVE(&savevm_state.handlers, se, entry); |
752 | } | |
753 | ||
a672b469 AL |
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. */ | |
ce62df53 | 758 | int register_savevm_live(const char *idstr, |
93062e23 | 759 | uint32_t instance_id, |
a672b469 | 760 | int version_id, |
de22ded0 | 761 | const SaveVMHandlers *ops, |
a672b469 AL |
762 | void *opaque) |
763 | { | |
8718e999 | 764 | SaveStateEntry *se; |
a672b469 | 765 | |
97f3ad35 | 766 | se = g_new0(SaveStateEntry, 1); |
a672b469 | 767 | se->version_id = version_id; |
0163a2e0 | 768 | se->section_id = savevm_state.global_section_id++; |
7908c78d | 769 | se->ops = ops; |
a672b469 | 770 | se->opaque = opaque; |
9ed7d6ae | 771 | se->vmsd = NULL; |
a7ae8355 | 772 | /* if this is a live_savem then set is_ram */ |
9907e842 | 773 | if (ops->save_setup != NULL) { |
a7ae8355 SS |
774 | se->is_ram = 1; |
775 | } | |
a672b469 | 776 | |
7685ee6a AW |
777 | pstrcat(se->idstr, sizeof(se->idstr), idstr); |
778 | ||
1df2c9a2 | 779 | if (instance_id == VMSTATE_INSTANCE_ID_ANY) { |
7685ee6a | 780 | se->instance_id = calculate_new_instance_id(se->idstr); |
8718e999 JQ |
781 | } else { |
782 | se->instance_id = instance_id; | |
a672b469 | 783 | } |
7685ee6a | 784 | assert(!se->compat || se->instance_id == 0); |
f37bc036 | 785 | savevm_state_handler_insert(se); |
a672b469 AL |
786 | return 0; |
787 | } | |
788 | ||
3cad405b | 789 | void unregister_savevm(VMStateIf *obj, const char *idstr, void *opaque) |
41bd13af | 790 | { |
8718e999 | 791 | SaveStateEntry *se, *new_se; |
7685ee6a AW |
792 | char id[256] = ""; |
793 | ||
3cad405b MAL |
794 | if (obj) { |
795 | char *oid = vmstate_if_get_id(obj); | |
796 | if (oid) { | |
797 | pstrcpy(id, sizeof(id), oid); | |
7685ee6a | 798 | pstrcat(id, sizeof(id), "/"); |
3cad405b | 799 | g_free(oid); |
7685ee6a AW |
800 | } |
801 | } | |
802 | pstrcat(id, sizeof(id), idstr); | |
41bd13af | 803 | |
0163a2e0 | 804 | QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) { |
7685ee6a | 805 | if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) { |
bd5de61e | 806 | savevm_state_handler_remove(se); |
ef1e1e07 | 807 | g_free(se->compat); |
7267c094 | 808 | g_free(se); |
41bd13af | 809 | } |
41bd13af AL |
810 | } |
811 | } | |
812 | ||
bb25a728 DDAG |
813 | /* |
814 | * Perform some basic checks on vmsd's at registration | |
815 | * time. | |
816 | */ | |
817 | static void vmstate_check(const VMStateDescription *vmsd) | |
818 | { | |
819 | const VMStateField *field = vmsd->fields; | |
820 | const VMStateDescription **subsection = vmsd->subsections; | |
821 | ||
822 | if (field) { | |
823 | while (field->name) { | |
824 | if (field->flags & (VMS_STRUCT | VMS_VSTRUCT)) { | |
825 | /* Recurse to sub structures */ | |
826 | vmstate_check(field->vmsd); | |
827 | } | |
828 | /* Carry on */ | |
829 | field++; | |
830 | } | |
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(); | |
835 | } | |
836 | } | |
837 | ||
838 | while (subsection && *subsection) { | |
839 | /* | |
840 | * The name of a subsection should start with the name of the | |
841 | * current object. | |
842 | */ | |
843 | assert(!strncmp(vmsd->name, (*subsection)->name, strlen(vmsd->name))); | |
844 | vmstate_check(*subsection); | |
845 | subsection++; | |
846 | } | |
847 | } | |
848 | ||
93062e23 | 849 | int vmstate_register_with_alias_id(VMStateIf *obj, uint32_t instance_id, |
4d2ffa08 JK |
850 | const VMStateDescription *vmsd, |
851 | void *opaque, int alias_id, | |
bc5c4f21 DDAG |
852 | int required_for_version, |
853 | Error **errp) | |
9ed7d6ae | 854 | { |
8718e999 | 855 | SaveStateEntry *se; |
9ed7d6ae | 856 | |
4d2ffa08 JK |
857 | /* If this triggers, alias support can be dropped for the vmsd. */ |
858 | assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id); | |
859 | ||
97f3ad35 | 860 | se = g_new0(SaveStateEntry, 1); |
9ed7d6ae | 861 | se->version_id = vmsd->version_id; |
0163a2e0 | 862 | se->section_id = savevm_state.global_section_id++; |
9ed7d6ae JQ |
863 | se->opaque = opaque; |
864 | se->vmsd = vmsd; | |
4d2ffa08 | 865 | se->alias_id = alias_id; |
9ed7d6ae | 866 | |
3cad405b MAL |
867 | if (obj) { |
868 | char *id = vmstate_if_get_id(obj); | |
7685ee6a | 869 | if (id) { |
581f08ba DDAG |
870 | if (snprintf(se->idstr, sizeof(se->idstr), "%s/", id) >= |
871 | sizeof(se->idstr)) { | |
872 | error_setg(errp, "Path too long for VMState (%s)", id); | |
873 | g_free(id); | |
874 | g_free(se); | |
875 | ||
876 | return -1; | |
877 | } | |
128e4e10 | 878 | g_free(id); |
7685ee6a | 879 | |
97f3ad35 | 880 | se->compat = g_new0(CompatEntry, 1); |
7685ee6a | 881 | pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name); |
1df2c9a2 | 882 | se->compat->instance_id = instance_id == VMSTATE_INSTANCE_ID_ANY ? |
7685ee6a | 883 | calculate_compat_instance_id(vmsd->name) : instance_id; |
1df2c9a2 | 884 | instance_id = VMSTATE_INSTANCE_ID_ANY; |
7685ee6a AW |
885 | } |
886 | } | |
887 | pstrcat(se->idstr, sizeof(se->idstr), vmsd->name); | |
888 | ||
1df2c9a2 | 889 | if (instance_id == VMSTATE_INSTANCE_ID_ANY) { |
7685ee6a | 890 | se->instance_id = calculate_new_instance_id(se->idstr); |
8718e999 JQ |
891 | } else { |
892 | se->instance_id = instance_id; | |
9ed7d6ae | 893 | } |
bb25a728 DDAG |
894 | |
895 | /* Perform a recursive sanity check during the test runs */ | |
896 | if (qtest_enabled()) { | |
897 | vmstate_check(vmsd); | |
898 | } | |
7685ee6a | 899 | assert(!se->compat || se->instance_id == 0); |
f37bc036 | 900 | savevm_state_handler_insert(se); |
9ed7d6ae JQ |
901 | return 0; |
902 | } | |
903 | ||
3cad405b | 904 | void vmstate_unregister(VMStateIf *obj, const VMStateDescription *vmsd, |
0be71e32 | 905 | void *opaque) |
9ed7d6ae | 906 | { |
1eb7538b JQ |
907 | SaveStateEntry *se, *new_se; |
908 | ||
0163a2e0 | 909 | QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) { |
1eb7538b | 910 | if (se->vmsd == vmsd && se->opaque == opaque) { |
bd5de61e | 911 | savevm_state_handler_remove(se); |
ef1e1e07 | 912 | g_free(se->compat); |
7267c094 | 913 | g_free(se); |
1eb7538b JQ |
914 | } |
915 | } | |
9ed7d6ae JQ |
916 | } |
917 | ||
3a011c26 | 918 | static int vmstate_load(QEMUFile *f, SaveStateEntry *se) |
4082be4d | 919 | { |
9013dca5 | 920 | trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)"); |
9ed7d6ae | 921 | if (!se->vmsd) { /* Old style */ |
3a011c26 | 922 | return se->ops->load_state(f, se->opaque, se->load_version_id); |
9ed7d6ae | 923 | } |
3a011c26 | 924 | return vmstate_load_state(f, se->vmsd, se->opaque, se->load_version_id); |
4082be4d JQ |
925 | } |
926 | ||
3ddba9a9 MA |
927 | static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, |
928 | JSONWriter *vmdesc) | |
8118f095 | 929 | { |
fc95c63b | 930 | uint64_t old_offset = qemu_file_transferred_noflush(f); |
8118f095 | 931 | se->ops->save_state(f, se->opaque); |
fc95c63b | 932 | uint64_t size = qemu_file_transferred_noflush(f) - old_offset; |
8118f095 AG |
933 | |
934 | if (vmdesc) { | |
3ddba9a9 MA |
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); | |
8118f095 AG |
943 | } |
944 | } | |
945 | ||
ce39bfc9 DDAG |
946 | /* |
947 | * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL) | |
948 | */ | |
949 | static void save_section_header(QEMUFile *f, SaveStateEntry *se, | |
950 | uint8_t section_type) | |
951 | { | |
952 | qemu_put_byte(f, section_type); | |
953 | qemu_put_be32(f, se->section_id); | |
954 | ||
955 | if (section_type == QEMU_VM_SECTION_FULL || | |
956 | section_type == QEMU_VM_SECTION_START) { | |
957 | /* ID string */ | |
958 | size_t len = strlen(se->idstr); | |
959 | qemu_put_byte(f, len); | |
960 | qemu_put_buffer(f, (uint8_t *)se->idstr, len); | |
961 | ||
962 | qemu_put_be32(f, se->instance_id); | |
963 | qemu_put_be32(f, se->version_id); | |
964 | } | |
965 | } | |
966 | ||
f68945d4 DDAG |
967 | /* |
968 | * Write a footer onto device sections that catches cases misformatted device | |
969 | * sections. | |
970 | */ | |
971 | static void save_section_footer(QEMUFile *f, SaveStateEntry *se) | |
972 | { | |
15c38503 | 973 | if (migrate_get_current()->send_section_footer) { |
f68945d4 DDAG |
974 | qemu_put_byte(f, QEMU_VM_SECTION_FOOTER); |
975 | qemu_put_be32(f, se->section_id); | |
976 | } | |
977 | } | |
978 | ||
5e104f24 DH |
979 | static int vmstate_save(QEMUFile *f, SaveStateEntry *se, JSONWriter *vmdesc) |
980 | { | |
981 | int ret; | |
848a0503 TG |
982 | Error *local_err = NULL; |
983 | MigrationState *s = migrate_get_current(); | |
5e104f24 DH |
984 | |
985 | if ((!se->ops || !se->ops->save_state) && !se->vmsd) { | |
986 | return 0; | |
987 | } | |
4d8bdc2a | 988 | if (se->vmsd && !vmstate_section_needed(se->vmsd, se->opaque)) { |
5e104f24 DH |
989 | trace_savevm_section_skip(se->idstr, se->section_id); |
990 | return 0; | |
991 | } | |
992 | ||
993 | trace_savevm_section_start(se->idstr, se->section_id); | |
994 | save_section_header(f, se, QEMU_VM_SECTION_FULL); | |
995 | if (vmdesc) { | |
996 | json_writer_start_object(vmdesc, NULL); | |
997 | json_writer_str(vmdesc, "name", se->idstr); | |
998 | json_writer_int64(vmdesc, "instance_id", se->instance_id); | |
999 | } | |
1000 | ||
1001 | trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)"); | |
1002 | if (!se->vmsd) { | |
1003 | vmstate_save_old_style(f, se, vmdesc); | |
1004 | } else { | |
969298f9 | 1005 | ret = vmstate_save_state_with_err(f, se->vmsd, se->opaque, vmdesc, &local_err); |
5e104f24 | 1006 | if (ret) { |
848a0503 TG |
1007 | migrate_set_error(s, local_err); |
1008 | error_report_err(local_err); | |
5e104f24 DH |
1009 | return ret; |
1010 | } | |
1011 | } | |
1012 | ||
1013 | trace_savevm_section_end(se->idstr, se->section_id, 0); | |
1014 | save_section_footer(f, se); | |
1015 | if (vmdesc) { | |
1016 | json_writer_end_object(vmdesc); | |
1017 | } | |
1018 | return 0; | |
1019 | } | |
c76ca188 DDAG |
1020 | /** |
1021 | * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the | |
1022 | * command and associated data. | |
1023 | * | |
1024 | * @f: File to send command on | |
1025 | * @command: Command type to send | |
1026 | * @len: Length of associated data | |
1027 | * @data: Data associated with command. | |
1028 | */ | |
20a519a0 JQ |
1029 | static void qemu_savevm_command_send(QEMUFile *f, |
1030 | enum qemu_vm_cmd command, | |
1031 | uint16_t len, | |
1032 | uint8_t *data) | |
c76ca188 DDAG |
1033 | { |
1034 | trace_savevm_command_send(command, len); | |
1035 | qemu_put_byte(f, QEMU_VM_COMMAND); | |
1036 | qemu_put_be16(f, (uint16_t)command); | |
1037 | qemu_put_be16(f, len); | |
1038 | qemu_put_buffer(f, data, len); | |
1039 | qemu_fflush(f); | |
1040 | } | |
1041 | ||
aad555c2 ZC |
1042 | void qemu_savevm_send_colo_enable(QEMUFile *f) |
1043 | { | |
1044 | trace_savevm_send_colo_enable(); | |
1045 | qemu_savevm_command_send(f, MIG_CMD_ENABLE_COLO, 0, NULL); | |
1046 | } | |
1047 | ||
2e37701e DDAG |
1048 | void qemu_savevm_send_ping(QEMUFile *f, uint32_t value) |
1049 | { | |
1050 | uint32_t buf; | |
1051 | ||
1052 | trace_savevm_send_ping(value); | |
1053 | buf = cpu_to_be32(value); | |
1054 | qemu_savevm_command_send(f, MIG_CMD_PING, sizeof(value), (uint8_t *)&buf); | |
1055 | } | |
1056 | ||
1057 | void qemu_savevm_send_open_return_path(QEMUFile *f) | |
1058 | { | |
1059 | trace_savevm_send_open_return_path(); | |
1060 | qemu_savevm_command_send(f, MIG_CMD_OPEN_RETURN_PATH, 0, NULL); | |
1061 | } | |
1062 | ||
11cf1d98 DDAG |
1063 | /* We have a buffer of data to send; we don't want that all to be loaded |
1064 | * by the command itself, so the command contains just the length of the | |
1065 | * extra buffer that we then send straight after it. | |
1066 | * TODO: Must be a better way to organise that | |
1067 | * | |
1068 | * Returns: | |
1069 | * 0 on success | |
1070 | * -ve on error | |
1071 | */ | |
61b67d47 | 1072 | int qemu_savevm_send_packaged(QEMUFile *f, const uint8_t *buf, size_t len) |
11cf1d98 | 1073 | { |
11cf1d98 | 1074 | uint32_t tmp; |
848a0503 TG |
1075 | MigrationState *ms = migrate_get_current(); |
1076 | Error *local_err = NULL; | |
11cf1d98 DDAG |
1077 | |
1078 | if (len > MAX_VM_CMD_PACKAGED_SIZE) { | |
848a0503 | 1079 | error_setg(&local_err, "%s: Unreasonably large packaged state: %zu", |
11cf1d98 | 1080 | __func__, len); |
848a0503 TG |
1081 | migrate_set_error(ms, local_err); |
1082 | error_report_err(local_err); | |
11cf1d98 DDAG |
1083 | return -1; |
1084 | } | |
1085 | ||
1086 | tmp = cpu_to_be32(len); | |
1087 | ||
1088 | trace_qemu_savevm_send_packaged(); | |
1089 | qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp); | |
1090 | ||
61b67d47 | 1091 | qemu_put_buffer(f, buf, len); |
11cf1d98 DDAG |
1092 | |
1093 | return 0; | |
1094 | } | |
1095 | ||
093e3c42 DDAG |
1096 | /* Send prior to any postcopy transfer */ |
1097 | void qemu_savevm_send_postcopy_advise(QEMUFile *f) | |
1098 | { | |
58110f0a VSO |
1099 | if (migrate_postcopy_ram()) { |
1100 | uint64_t tmp[2]; | |
1101 | tmp[0] = cpu_to_be64(ram_pagesize_summary()); | |
1102 | tmp[1] = cpu_to_be64(qemu_target_page_size()); | |
1103 | ||
1104 | trace_qemu_savevm_send_postcopy_advise(); | |
1105 | qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, | |
1106 | 16, (uint8_t *)tmp); | |
1107 | } else { | |
1108 | qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 0, NULL); | |
1109 | } | |
093e3c42 DDAG |
1110 | } |
1111 | ||
1112 | /* Sent prior to starting the destination running in postcopy, discard pages | |
1113 | * that have already been sent but redirtied on the source. | |
1114 | * CMD_POSTCOPY_RAM_DISCARD consist of: | |
1115 | * byte version (0) | |
1116 | * byte Length of name field (not including 0) | |
1117 | * n x byte RAM block name | |
1118 | * byte 0 terminator (just for safety) | |
1119 | * n x Byte ranges within the named RAMBlock | |
1120 | * be64 Start of the range | |
1121 | * be64 Length | |
1122 | * | |
1123 | * name: RAMBlock name that these entries are part of | |
1124 | * len: Number of page entries | |
1125 | * start_list: 'len' addresses | |
1126 | * length_list: 'len' addresses | |
1127 | * | |
1128 | */ | |
1129 | void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name, | |
1130 | uint16_t len, | |
1131 | uint64_t *start_list, | |
1132 | uint64_t *length_list) | |
1133 | { | |
1134 | uint8_t *buf; | |
1135 | uint16_t tmplen; | |
1136 | uint16_t t; | |
1137 | size_t name_len = strlen(name); | |
1138 | ||
1139 | trace_qemu_savevm_send_postcopy_ram_discard(name, len); | |
1140 | assert(name_len < 256); | |
1141 | buf = g_malloc0(1 + 1 + name_len + 1 + (8 + 8) * len); | |
1142 | buf[0] = postcopy_ram_discard_version; | |
1143 | buf[1] = name_len; | |
1144 | memcpy(buf + 2, name, name_len); | |
1145 | tmplen = 2 + name_len; | |
1146 | buf[tmplen++] = '\0'; | |
1147 | ||
1148 | for (t = 0; t < len; t++) { | |
4d885131 | 1149 | stq_be_p(buf + tmplen, start_list[t]); |
093e3c42 | 1150 | tmplen += 8; |
4d885131 | 1151 | stq_be_p(buf + tmplen, length_list[t]); |
093e3c42 DDAG |
1152 | tmplen += 8; |
1153 | } | |
1154 | qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RAM_DISCARD, tmplen, buf); | |
1155 | g_free(buf); | |
1156 | } | |
1157 | ||
1158 | /* Get the destination into a state where it can receive postcopy data. */ | |
1159 | void qemu_savevm_send_postcopy_listen(QEMUFile *f) | |
1160 | { | |
1161 | trace_savevm_send_postcopy_listen(); | |
1162 | qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_LISTEN, 0, NULL); | |
1163 | } | |
1164 | ||
1165 | /* Kick the destination into running */ | |
1166 | void qemu_savevm_send_postcopy_run(QEMUFile *f) | |
1167 | { | |
1168 | trace_savevm_send_postcopy_run(); | |
1169 | qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RUN, 0, NULL); | |
1170 | } | |
1171 | ||
3f5875ec PX |
1172 | void qemu_savevm_send_postcopy_resume(QEMUFile *f) |
1173 | { | |
1174 | trace_savevm_send_postcopy_resume(); | |
1175 | qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RESUME, 0, NULL); | |
1176 | } | |
1177 | ||
f25d4225 PX |
1178 | void qemu_savevm_send_recv_bitmap(QEMUFile *f, char *block_name) |
1179 | { | |
1180 | size_t len; | |
1181 | char buf[256]; | |
1182 | ||
1183 | trace_savevm_send_recv_bitmap(block_name); | |
1184 | ||
1185 | buf[0] = len = strlen(block_name); | |
1186 | memcpy(buf + 1, block_name, len); | |
1187 | ||
1188 | qemu_savevm_command_send(f, MIG_CMD_RECV_BITMAP, len + 1, (uint8_t *)buf); | |
1189 | } | |
1190 | ||
e1c37d0e | 1191 | bool qemu_savevm_state_blocked(Error **errp) |
dc912121 AW |
1192 | { |
1193 | SaveStateEntry *se; | |
1194 | ||
0163a2e0 | 1195 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
7d854c47 | 1196 | if (se->vmsd && se->vmsd->unmigratable) { |
f231b88d CR |
1197 | error_setg(errp, "State blocked by non-migratable device '%s'", |
1198 | se->idstr); | |
dc912121 AW |
1199 | return true; |
1200 | } | |
1201 | } | |
1202 | return false; | |
1203 | } | |
1204 | ||
3af8554b DDAG |
1205 | void qemu_savevm_non_migratable_list(strList **reasons) |
1206 | { | |
1207 | SaveStateEntry *se; | |
1208 | ||
1209 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { | |
1210 | if (se->vmsd && se->vmsd->unmigratable) { | |
1211 | QAPI_LIST_PREPEND(*reasons, | |
1212 | g_strdup_printf("non-migratable device: %s", | |
1213 | se->idstr)); | |
1214 | } | |
1215 | } | |
1216 | } | |
1217 | ||
f796baa1 DDAG |
1218 | void qemu_savevm_state_header(QEMUFile *f) |
1219 | { | |
2aae1eb8 NB |
1220 | MigrationState *s = migrate_get_current(); |
1221 | ||
1222 | s->vmdesc = json_writer_new(false); | |
1223 | ||
f796baa1 DDAG |
1224 | trace_savevm_state_header(); |
1225 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); | |
1226 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); | |
172dfd4f | 1227 | |
2aae1eb8 | 1228 | if (s->send_configuration) { |
172dfd4f | 1229 | qemu_put_byte(f, QEMU_VM_CONFIGURATION); |
2aae1eb8 NB |
1230 | |
1231 | /* | |
1232 | * This starts the main json object and is paired with the | |
1233 | * json_writer_end_object in | |
1234 | * qemu_savevm_state_complete_precopy_non_iterable | |
1235 | */ | |
1236 | json_writer_start_object(s->vmdesc, NULL); | |
1237 | ||
1238 | json_writer_start_object(s->vmdesc, "configuration"); | |
1239 | vmstate_save_state(f, &vmstate_configuration, &savevm_state, s->vmdesc); | |
1240 | json_writer_end_object(s->vmdesc); | |
172dfd4f | 1241 | } |
f796baa1 DDAG |
1242 | } |
1243 | ||
d05de9e3 | 1244 | bool qemu_savevm_state_guest_unplug_pending(void) |
c7e0acd5 JF |
1245 | { |
1246 | SaveStateEntry *se; | |
c7e0acd5 JF |
1247 | |
1248 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { | |
284f42a5 JF |
1249 | if (se->vmsd && se->vmsd->dev_unplug_pending && |
1250 | se->vmsd->dev_unplug_pending(se->opaque)) { | |
d05de9e3 | 1251 | return true; |
c7e0acd5 JF |
1252 | } |
1253 | } | |
1254 | ||
d05de9e3 | 1255 | return false; |
c7e0acd5 JF |
1256 | } |
1257 | ||
08fc4cb5 AH |
1258 | int qemu_savevm_state_prepare(Error **errp) |
1259 | { | |
1260 | SaveStateEntry *se; | |
1261 | int ret; | |
1262 | ||
1263 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { | |
1264 | if (!se->ops || !se->ops->save_prepare) { | |
1265 | continue; | |
1266 | } | |
1267 | if (se->ops->is_active) { | |
1268 | if (!se->ops->is_active(se->opaque)) { | |
1269 | continue; | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | ret = se->ops->save_prepare(se->opaque, errp); | |
1274 | if (ret < 0) { | |
1275 | return ret; | |
1276 | } | |
1277 | } | |
1278 | ||
1279 | return 0; | |
1280 | } | |
1281 | ||
9907e842 | 1282 | void qemu_savevm_state_setup(QEMUFile *f) |
a672b469 | 1283 | { |
e3bf5e68 | 1284 | MigrationState *ms = migrate_get_current(); |
a672b469 | 1285 | SaveStateEntry *se; |
bd227060 | 1286 | Error *local_err = NULL; |
39346385 | 1287 | int ret; |
a672b469 | 1288 | |
e3bf5e68 DH |
1289 | json_writer_int64(ms->vmdesc, "page_size", qemu_target_page_size()); |
1290 | json_writer_start_array(ms->vmdesc, "devices"); | |
1291 | ||
9907e842 | 1292 | trace_savevm_state_setup(); |
0163a2e0 | 1293 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
62f42625 DH |
1294 | if (se->vmsd && se->vmsd->early_setup) { |
1295 | ret = vmstate_save(f, se, ms->vmdesc); | |
1296 | if (ret) { | |
1297 | qemu_file_set_error(f, ret); | |
1298 | break; | |
1299 | } | |
1300 | continue; | |
1301 | } | |
1302 | ||
9907e842 | 1303 | if (!se->ops || !se->ops->save_setup) { |
a672b469 | 1304 | continue; |
22ea40f4 | 1305 | } |
cea3b4c0 | 1306 | if (se->ops->is_active) { |
6bd68781 JQ |
1307 | if (!se->ops->is_active(se->opaque)) { |
1308 | continue; | |
1309 | } | |
1310 | } | |
ce39bfc9 | 1311 | save_section_header(f, se, QEMU_VM_SECTION_START); |
a672b469 | 1312 | |
9907e842 | 1313 | ret = se->ops->save_setup(f, se->opaque); |
f68945d4 | 1314 | save_section_footer(f, se); |
2975725f | 1315 | if (ret < 0) { |
47c8c17a PB |
1316 | qemu_file_set_error(f, ret); |
1317 | break; | |
2975725f | 1318 | } |
a672b469 | 1319 | } |
bd227060 WW |
1320 | |
1321 | if (precopy_notify(PRECOPY_NOTIFY_SETUP, &local_err)) { | |
1322 | error_report_err(local_err); | |
1323 | } | |
a672b469 AL |
1324 | } |
1325 | ||
d1b8eadb PX |
1326 | int qemu_savevm_state_resume_prepare(MigrationState *s) |
1327 | { | |
1328 | SaveStateEntry *se; | |
1329 | int ret; | |
1330 | ||
1331 | trace_savevm_state_resume_prepare(); | |
1332 | ||
1333 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { | |
1334 | if (!se->ops || !se->ops->resume_prepare) { | |
1335 | continue; | |
1336 | } | |
cea3b4c0 | 1337 | if (se->ops->is_active) { |
d1b8eadb PX |
1338 | if (!se->ops->is_active(se->opaque)) { |
1339 | continue; | |
1340 | } | |
1341 | } | |
1342 | ret = se->ops->resume_prepare(s, se->opaque); | |
1343 | if (ret < 0) { | |
1344 | return ret; | |
1345 | } | |
1346 | } | |
1347 | ||
1348 | return 0; | |
1349 | } | |
1350 | ||
39346385 | 1351 | /* |
07f35073 | 1352 | * this function has three return values: |
39346385 JQ |
1353 | * negative: there was one error, and we have -errno. |
1354 | * 0 : We haven't finished, caller have to go again | |
1355 | * 1 : We have finished, we can go to complete phase | |
1356 | */ | |
35ecd943 | 1357 | int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy) |
a672b469 AL |
1358 | { |
1359 | SaveStateEntry *se; | |
1360 | int ret = 1; | |
1361 | ||
9013dca5 | 1362 | trace_savevm_state_iterate(); |
0163a2e0 | 1363 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
16310a3c | 1364 | if (!se->ops || !se->ops->save_live_iterate) { |
a672b469 | 1365 | continue; |
22ea40f4 | 1366 | } |
a94cd7b8 WY |
1367 | if (se->ops->is_active && |
1368 | !se->ops->is_active(se->opaque)) { | |
1369 | continue; | |
6bd68781 | 1370 | } |
a94cd7b8 WY |
1371 | if (se->ops->is_active_iterate && |
1372 | !se->ops->is_active_iterate(se->opaque)) { | |
1373 | continue; | |
c865d848 | 1374 | } |
35ecd943 DDAG |
1375 | /* |
1376 | * In the postcopy phase, any device that doesn't know how to | |
1377 | * do postcopy should have saved it's state in the _complete | |
1378 | * call that's already run, it might get confused if we call | |
1379 | * iterate afterwards. | |
1380 | */ | |
c6467627 VSO |
1381 | if (postcopy && |
1382 | !(se->ops->has_postcopy && se->ops->has_postcopy(se->opaque))) { | |
35ecd943 DDAG |
1383 | continue; |
1384 | } | |
e1fde0e0 | 1385 | if (migration_rate_exceeded(f)) { |
aac844ed JQ |
1386 | return 0; |
1387 | } | |
464400f6 | 1388 | trace_savevm_section_start(se->idstr, se->section_id); |
ce39bfc9 DDAG |
1389 | |
1390 | save_section_header(f, se, QEMU_VM_SECTION_PART); | |
a672b469 | 1391 | |
16310a3c | 1392 | ret = se->ops->save_live_iterate(f, se->opaque); |
a5df2a02 | 1393 | trace_savevm_section_end(se->idstr, se->section_id, ret); |
f68945d4 | 1394 | save_section_footer(f, se); |
517a13c9 | 1395 | |
47c8c17a | 1396 | if (ret < 0) { |
92002658 DE |
1397 | error_report("failed to save SaveStateEntry with id(name): " |
1398 | "%d(%s): %d", | |
1399 | se->section_id, se->idstr, ret); | |
47c8c17a PB |
1400 | qemu_file_set_error(f, ret); |
1401 | } | |
2975725f | 1402 | if (ret <= 0) { |
90697be8 JK |
1403 | /* Do not proceed to the next vmstate before this one reported |
1404 | completion of the current stage. This serializes the migration | |
1405 | and reduces the probability that a faster changing state is | |
1406 | synchronized over and over again. */ | |
1407 | break; | |
1408 | } | |
a672b469 | 1409 | } |
39346385 | 1410 | return ret; |
a672b469 AL |
1411 | } |
1412 | ||
9850c604 AG |
1413 | static bool should_send_vmdesc(void) |
1414 | { | |
1415 | MachineState *machine = MACHINE(qdev_get_machine()); | |
5727309d | 1416 | bool in_postcopy = migration_in_postcopy(); |
8421b205 | 1417 | return !machine->suppress_vmdesc && !in_postcopy; |
9850c604 AG |
1418 | } |
1419 | ||
763c906b DDAG |
1420 | /* |
1421 | * Calls the save_live_complete_postcopy methods | |
1422 | * causing the last few pages to be sent immediately and doing any associated | |
1423 | * cleanup. | |
1424 | * Note postcopy also calls qemu_savevm_state_complete_precopy to complete | |
1425 | * all the other devices, but that happens at the point we switch to postcopy. | |
1426 | */ | |
1427 | void qemu_savevm_state_complete_postcopy(QEMUFile *f) | |
1428 | { | |
1429 | SaveStateEntry *se; | |
1430 | int ret; | |
1431 | ||
1432 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { | |
1433 | if (!se->ops || !se->ops->save_live_complete_postcopy) { | |
1434 | continue; | |
1435 | } | |
cea3b4c0 | 1436 | if (se->ops->is_active) { |
763c906b DDAG |
1437 | if (!se->ops->is_active(se->opaque)) { |
1438 | continue; | |
1439 | } | |
1440 | } | |
1441 | trace_savevm_section_start(se->idstr, se->section_id); | |
1442 | /* Section type */ | |
1443 | qemu_put_byte(f, QEMU_VM_SECTION_END); | |
1444 | qemu_put_be32(f, se->section_id); | |
1445 | ||
1446 | ret = se->ops->save_live_complete_postcopy(f, se->opaque); | |
1447 | trace_savevm_section_end(se->idstr, se->section_id, ret); | |
1448 | save_section_footer(f, se); | |
1449 | if (ret < 0) { | |
1450 | qemu_file_set_error(f, ret); | |
1451 | return; | |
1452 | } | |
1453 | } | |
1454 | ||
1455 | qemu_put_byte(f, QEMU_VM_EOF); | |
1456 | qemu_fflush(f); | |
1457 | } | |
1458 | ||
622a80c9 | 1459 | static |
e326767b | 1460 | int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy) |
a672b469 AL |
1461 | { |
1462 | SaveStateEntry *se; | |
2975725f | 1463 | int ret; |
ea375f9a | 1464 | |
0163a2e0 | 1465 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
763c906b | 1466 | if (!se->ops || |
c6467627 VSO |
1467 | (in_postcopy && se->ops->has_postcopy && |
1468 | se->ops->has_postcopy(se->opaque)) || | |
763c906b | 1469 | !se->ops->save_live_complete_precopy) { |
a672b469 | 1470 | continue; |
22ea40f4 | 1471 | } |
1c0d249d | 1472 | |
cea3b4c0 | 1473 | if (se->ops->is_active) { |
6bd68781 JQ |
1474 | if (!se->ops->is_active(se->opaque)) { |
1475 | continue; | |
1476 | } | |
1477 | } | |
464400f6 | 1478 | trace_savevm_section_start(se->idstr, se->section_id); |
ce39bfc9 DDAG |
1479 | |
1480 | save_section_header(f, se, QEMU_VM_SECTION_END); | |
a672b469 | 1481 | |
a3e06c3d | 1482 | ret = se->ops->save_live_complete_precopy(f, se->opaque); |
a5df2a02 | 1483 | trace_savevm_section_end(se->idstr, se->section_id, ret); |
f68945d4 | 1484 | save_section_footer(f, se); |
2975725f | 1485 | if (ret < 0) { |
47c8c17a | 1486 | qemu_file_set_error(f, ret); |
a1fbe750 | 1487 | return -1; |
2975725f | 1488 | } |
a672b469 AL |
1489 | } |
1490 | ||
622a80c9 WY |
1491 | return 0; |
1492 | } | |
1493 | ||
622a80c9 WY |
1494 | int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f, |
1495 | bool in_postcopy, | |
1496 | bool inactivate_disks) | |
1497 | { | |
e3bf5e68 DH |
1498 | MigrationState *ms = migrate_get_current(); |
1499 | JSONWriter *vmdesc = ms->vmdesc; | |
622a80c9 WY |
1500 | int vmdesc_len; |
1501 | SaveStateEntry *se; | |
1502 | int ret; | |
1c0d249d | 1503 | |
0163a2e0 | 1504 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
62f42625 DH |
1505 | if (se->vmsd && se->vmsd->early_setup) { |
1506 | /* Already saved during qemu_savevm_state_setup(). */ | |
1507 | continue; | |
1508 | } | |
1509 | ||
687433f6 DDAG |
1510 | ret = vmstate_save(f, se, vmdesc); |
1511 | if (ret) { | |
1512 | qemu_file_set_error(f, ret); | |
1513 | return ret; | |
1514 | } | |
a672b469 AL |
1515 | } |
1516 | ||
a1fbe750 FZ |
1517 | if (inactivate_disks) { |
1518 | /* Inactivate before sending QEMU_VM_EOF so that the | |
3b717194 | 1519 | * bdrv_activate_all() on the other end won't fail. */ |
a1fbe750 FZ |
1520 | ret = bdrv_inactivate_all(); |
1521 | if (ret) { | |
848a0503 TG |
1522 | Error *local_err = NULL; |
1523 | error_setg(&local_err, "%s: bdrv_inactivate_all() failed (%d)", | |
1524 | __func__, ret); | |
1525 | migrate_set_error(ms, local_err); | |
1526 | error_report_err(local_err); | |
a1fbe750 FZ |
1527 | qemu_file_set_error(f, ret); |
1528 | return ret; | |
1529 | } | |
1530 | } | |
763c906b DDAG |
1531 | if (!in_postcopy) { |
1532 | /* Postcopy stream will still be going */ | |
1533 | qemu_put_byte(f, QEMU_VM_EOF); | |
1534 | } | |
8118f095 | 1535 | |
3ddba9a9 MA |
1536 | json_writer_end_array(vmdesc); |
1537 | json_writer_end_object(vmdesc); | |
1538 | vmdesc_len = strlen(json_writer_get(vmdesc)); | |
8118f095 | 1539 | |
9850c604 AG |
1540 | if (should_send_vmdesc()) { |
1541 | qemu_put_byte(f, QEMU_VM_VMDESCRIPTION); | |
1542 | qemu_put_be32(f, vmdesc_len); | |
3ddba9a9 | 1543 | qemu_put_buffer(f, (uint8_t *)json_writer_get(vmdesc), vmdesc_len); |
9850c604 | 1544 | } |
8118f095 | 1545 | |
e3bf5e68 DH |
1546 | /* Free it now to detect any inconsistencies. */ |
1547 | json_writer_free(vmdesc); | |
1548 | ms->vmdesc = NULL; | |
1549 | ||
622a80c9 WY |
1550 | return 0; |
1551 | } | |
1552 | ||
1553 | int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only, | |
1554 | bool inactivate_disks) | |
1555 | { | |
1556 | int ret; | |
1557 | Error *local_err = NULL; | |
1558 | bool in_postcopy = migration_in_postcopy(); | |
1559 | ||
1560 | if (precopy_notify(PRECOPY_NOTIFY_COMPLETE, &local_err)) { | |
1561 | error_report_err(local_err); | |
1562 | } | |
1563 | ||
1564 | trace_savevm_state_complete_precopy(); | |
1565 | ||
1566 | cpu_synchronize_all_states(); | |
1567 | ||
e326767b WY |
1568 | if (!in_postcopy || iterable_only) { |
1569 | ret = qemu_savevm_state_complete_precopy_iterable(f, in_postcopy); | |
1570 | if (ret) { | |
1571 | return ret; | |
1572 | } | |
622a80c9 WY |
1573 | } |
1574 | ||
1575 | if (iterable_only) { | |
1576 | goto flush; | |
1577 | } | |
1578 | ||
1579 | ret = qemu_savevm_state_complete_precopy_non_iterable(f, in_postcopy, | |
1580 | inactivate_disks); | |
1581 | if (ret) { | |
1582 | return ret; | |
1583 | } | |
1584 | ||
4e455d51 | 1585 | flush: |
edaae611 | 1586 | qemu_fflush(f); |
a1fbe750 | 1587 | return 0; |
a672b469 AL |
1588 | } |
1589 | ||
c31b098f DDAG |
1590 | /* Give an estimate of the amount left to be transferred, |
1591 | * the result is split into the amount for units that can and | |
1592 | * for units that can't do postcopy. | |
1593 | */ | |
24beea4e JQ |
1594 | void qemu_savevm_state_pending_estimate(uint64_t *must_precopy, |
1595 | uint64_t *can_postcopy) | |
e4ed1541 JQ |
1596 | { |
1597 | SaveStateEntry *se; | |
c31b098f | 1598 | |
24beea4e JQ |
1599 | *must_precopy = 0; |
1600 | *can_postcopy = 0; | |
c31b098f | 1601 | |
0163a2e0 | 1602 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
e2647050 | 1603 | if (!se->ops || !se->ops->state_pending_estimate) { |
e4ed1541 JQ |
1604 | continue; |
1605 | } | |
cea3b4c0 | 1606 | if (se->ops->is_active) { |
e4ed1541 JQ |
1607 | if (!se->ops->is_active(se->opaque)) { |
1608 | continue; | |
1609 | } | |
1610 | } | |
24beea4e | 1611 | se->ops->state_pending_estimate(se->opaque, must_precopy, can_postcopy); |
c8df4a7a JQ |
1612 | } |
1613 | } | |
1614 | ||
24beea4e JQ |
1615 | void qemu_savevm_state_pending_exact(uint64_t *must_precopy, |
1616 | uint64_t *can_postcopy) | |
c8df4a7a JQ |
1617 | { |
1618 | SaveStateEntry *se; | |
1619 | ||
24beea4e JQ |
1620 | *must_precopy = 0; |
1621 | *can_postcopy = 0; | |
c8df4a7a JQ |
1622 | |
1623 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { | |
e2647050 | 1624 | if (!se->ops || !se->ops->state_pending_exact) { |
c8df4a7a JQ |
1625 | continue; |
1626 | } | |
1627 | if (se->ops->is_active) { | |
1628 | if (!se->ops->is_active(se->opaque)) { | |
1629 | continue; | |
1630 | } | |
1631 | } | |
24beea4e | 1632 | se->ops->state_pending_exact(se->opaque, must_precopy, can_postcopy); |
e4ed1541 | 1633 | } |
e4ed1541 JQ |
1634 | } |
1635 | ||
ea7415fa | 1636 | void qemu_savevm_state_cleanup(void) |
4ec7fcc7 JK |
1637 | { |
1638 | SaveStateEntry *se; | |
bd227060 WW |
1639 | Error *local_err = NULL; |
1640 | ||
1641 | if (precopy_notify(PRECOPY_NOTIFY_CLEANUP, &local_err)) { | |
1642 | error_report_err(local_err); | |
1643 | } | |
4ec7fcc7 | 1644 | |
ea7415fa | 1645 | trace_savevm_state_cleanup(); |
0163a2e0 | 1646 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
70f794fc JQ |
1647 | if (se->ops && se->ops->save_cleanup) { |
1648 | se->ops->save_cleanup(se->opaque); | |
4ec7fcc7 JK |
1649 | } |
1650 | } | |
1651 | } | |
1652 | ||
5d80448c | 1653 | static int qemu_savevm_state(QEMUFile *f, Error **errp) |
a672b469 | 1654 | { |
a672b469 | 1655 | int ret; |
3e0c8050 | 1656 | MigrationState *ms = migrate_get_current(); |
6dcf6668 | 1657 | MigrationStatus status; |
3e0c8050 | 1658 | |
392d87e2 | 1659 | if (migration_is_running(ms->state)) { |
3d63da16 JL |
1660 | error_setg(errp, QERR_MIGRATION_ACTIVE); |
1661 | return -EINVAL; | |
1662 | } | |
a672b469 | 1663 | |
9d4b1e5f | 1664 | if (migrate_block()) { |
ce7c817c | 1665 | error_setg(errp, "Block migration and snapshots are incompatible"); |
3d63da16 | 1666 | return -EINVAL; |
ce7c817c JQ |
1667 | } |
1668 | ||
08fc4cb5 AH |
1669 | ret = migrate_init(ms, errp); |
1670 | if (ret) { | |
1671 | return ret; | |
1672 | } | |
3d63da16 JL |
1673 | ms->to_dst_file = f; |
1674 | ||
f796baa1 | 1675 | qemu_savevm_state_header(f); |
9907e842 | 1676 | qemu_savevm_state_setup(f); |
9b095037 | 1677 | |
47c8c17a | 1678 | while (qemu_file_get_error(f) == 0) { |
35ecd943 | 1679 | if (qemu_savevm_state_iterate(f, false) > 0) { |
47c8c17a PB |
1680 | break; |
1681 | } | |
1682 | } | |
a672b469 | 1683 | |
47c8c17a | 1684 | ret = qemu_file_get_error(f); |
39346385 | 1685 | if (ret == 0) { |
a1fbe750 | 1686 | qemu_savevm_state_complete_precopy(f, false, false); |
624b9cc2 | 1687 | ret = qemu_file_get_error(f); |
39346385 | 1688 | } |
15b3b8ea | 1689 | qemu_savevm_state_cleanup(); |
04943eba | 1690 | if (ret != 0) { |
5d80448c | 1691 | error_setg_errno(errp, -ret, "Error while writing VM state"); |
04943eba | 1692 | } |
6dcf6668 | 1693 | |
6dcf6668 DL |
1694 | if (ret != 0) { |
1695 | status = MIGRATION_STATUS_FAILED; | |
1696 | } else { | |
1697 | status = MIGRATION_STATUS_COMPLETED; | |
1698 | } | |
1699 | migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP, status); | |
f9c8caa0 VSO |
1700 | |
1701 | /* f is outer parameter, it should not stay in global migration state after | |
1702 | * this function finished */ | |
1703 | ms->to_dst_file = NULL; | |
1704 | ||
a672b469 AL |
1705 | return ret; |
1706 | } | |
1707 | ||
3f6df99d | 1708 | void qemu_savevm_live_state(QEMUFile *f) |
a7ae8355 | 1709 | { |
3f6df99d ZC |
1710 | /* save QEMU_VM_SECTION_END section */ |
1711 | qemu_savevm_state_complete_precopy(f, true, false); | |
1712 | qemu_put_byte(f, QEMU_VM_EOF); | |
1713 | } | |
a7ae8355 | 1714 | |
3f6df99d ZC |
1715 | int qemu_save_device_state(QEMUFile *f) |
1716 | { | |
1717 | SaveStateEntry *se; | |
a7ae8355 | 1718 | |
3f6df99d ZC |
1719 | if (!migration_in_colo_state()) { |
1720 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); | |
1721 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); | |
1722 | } | |
a7ae8355 SS |
1723 | cpu_synchronize_all_states(); |
1724 | ||
0163a2e0 | 1725 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
687433f6 DDAG |
1726 | int ret; |
1727 | ||
a7ae8355 SS |
1728 | if (se->is_ram) { |
1729 | continue; | |
1730 | } | |
687433f6 DDAG |
1731 | ret = vmstate_save(f, se, NULL); |
1732 | if (ret) { | |
1733 | return ret; | |
1734 | } | |
a7ae8355 SS |
1735 | } |
1736 | ||
1737 | qemu_put_byte(f, QEMU_VM_EOF); | |
1738 | ||
1739 | return qemu_file_get_error(f); | |
1740 | } | |
1741 | ||
93062e23 | 1742 | static SaveStateEntry *find_se(const char *idstr, uint32_t instance_id) |
a672b469 AL |
1743 | { |
1744 | SaveStateEntry *se; | |
1745 | ||
0163a2e0 | 1746 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
a672b469 | 1747 | if (!strcmp(se->idstr, idstr) && |
4d2ffa08 JK |
1748 | (instance_id == se->instance_id || |
1749 | instance_id == se->alias_id)) | |
a672b469 | 1750 | return se; |
7685ee6a AW |
1751 | /* Migrating from an older version? */ |
1752 | if (strstr(se->idstr, idstr) && se->compat) { | |
1753 | if (!strcmp(se->compat->idstr, idstr) && | |
1754 | (instance_id == se->compat->instance_id || | |
1755 | instance_id == se->alias_id)) | |
1756 | return se; | |
1757 | } | |
a672b469 AL |
1758 | } |
1759 | return NULL; | |
1760 | } | |
1761 | ||
7b89bf27 DDAG |
1762 | enum LoadVMExitCodes { |
1763 | /* Allow a command to quit all layers of nested loadvm loops */ | |
1764 | LOADVM_QUIT = 1, | |
1765 | }; | |
1766 | ||
093e3c42 DDAG |
1767 | /* ------ incoming postcopy messages ------ */ |
1768 | /* 'advise' arrives before any transfers just to tell us that a postcopy | |
1769 | * *might* happen - it might be skipped if precopy transferred everything | |
1770 | * quickly. | |
1771 | */ | |
875fcd01 GK |
1772 | static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis, |
1773 | uint16_t len) | |
093e3c42 DDAG |
1774 | { |
1775 | PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_ADVISE); | |
e8ca1db2 | 1776 | uint64_t remote_pagesize_summary, local_pagesize_summary, remote_tps; |
144fa06b | 1777 | size_t page_size = qemu_target_page_size(); |
d3dff7a5 | 1778 | Error *local_err = NULL; |
093e3c42 DDAG |
1779 | |
1780 | trace_loadvm_postcopy_handle_advise(); | |
1781 | if (ps != POSTCOPY_INCOMING_NONE) { | |
1782 | error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps); | |
1783 | return -1; | |
1784 | } | |
1785 | ||
875fcd01 GK |
1786 | switch (len) { |
1787 | case 0: | |
1788 | if (migrate_postcopy_ram()) { | |
1789 | error_report("RAM postcopy is enabled but have 0 byte advise"); | |
1790 | return -EINVAL; | |
1791 | } | |
58110f0a | 1792 | return 0; |
875fcd01 GK |
1793 | case 8 + 8: |
1794 | if (!migrate_postcopy_ram()) { | |
1795 | error_report("RAM postcopy is disabled but have 16 byte advise"); | |
1796 | return -EINVAL; | |
1797 | } | |
1798 | break; | |
1799 | default: | |
1800 | error_report("CMD_POSTCOPY_ADVISE invalid length (%d)", len); | |
1801 | return -EINVAL; | |
58110f0a VSO |
1802 | } |
1803 | ||
74c38cf7 PX |
1804 | if (!postcopy_ram_supported_by_host(mis, &local_err)) { |
1805 | error_report_err(local_err); | |
328d4d85 | 1806 | postcopy_state_set(POSTCOPY_INCOMING_NONE); |
eb59db53 DDAG |
1807 | return -1; |
1808 | } | |
1809 | ||
e8ca1db2 DDAG |
1810 | remote_pagesize_summary = qemu_get_be64(mis->from_src_file); |
1811 | local_pagesize_summary = ram_pagesize_summary(); | |
1812 | ||
1813 | if (remote_pagesize_summary != local_pagesize_summary) { | |
093e3c42 | 1814 | /* |
e8ca1db2 DDAG |
1815 | * This detects two potential causes of mismatch: |
1816 | * a) A mismatch in host page sizes | |
1817 | * Some combinations of mismatch are probably possible but it gets | |
1818 | * a bit more complicated. In particular we need to place whole | |
1819 | * host pages on the dest at once, and we need to ensure that we | |
1820 | * handle dirtying to make sure we never end up sending part of | |
1821 | * a hostpage on it's own. | |
1822 | * b) The use of different huge page sizes on source/destination | |
1823 | * a more fine grain test is performed during RAM block migration | |
1824 | * but this test here causes a nice early clear failure, and | |
1825 | * also fails when passed to an older qemu that doesn't | |
1826 | * do huge pages. | |
093e3c42 | 1827 | */ |
e8ca1db2 DDAG |
1828 | error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64 |
1829 | " d=%" PRIx64 ")", | |
1830 | remote_pagesize_summary, local_pagesize_summary); | |
093e3c42 DDAG |
1831 | return -1; |
1832 | } | |
1833 | ||
1834 | remote_tps = qemu_get_be64(mis->from_src_file); | |
144fa06b | 1835 | if (remote_tps != page_size) { |
093e3c42 DDAG |
1836 | /* |
1837 | * Again, some differences could be dealt with, but for now keep it | |
1838 | * simple. | |
1839 | */ | |
20afaed9 | 1840 | error_report("Postcopy needs matching target page sizes (s=%d d=%zd)", |
144fa06b | 1841 | (int)remote_tps, page_size); |
093e3c42 DDAG |
1842 | return -1; |
1843 | } | |
1844 | ||
d3dff7a5 DDAG |
1845 | if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_ADVISE, &local_err)) { |
1846 | error_report_err(local_err); | |
1847 | return -1; | |
1848 | } | |
1849 | ||
1caddf8a DDAG |
1850 | if (ram_postcopy_incoming_init(mis)) { |
1851 | return -1; | |
1852 | } | |
1853 | ||
093e3c42 DDAG |
1854 | return 0; |
1855 | } | |
1856 | ||
1857 | /* After postcopy we will be told to throw some pages away since they're | |
1858 | * dirty and will have to be demand fetched. Must happen before CPU is | |
1859 | * started. | |
1860 | * There can be 0..many of these messages, each encoding multiple pages. | |
1861 | */ | |
1862 | static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis, | |
1863 | uint16_t len) | |
1864 | { | |
1865 | int tmp; | |
1866 | char ramid[256]; | |
1867 | PostcopyState ps = postcopy_state_get(); | |
1868 | ||
1869 | trace_loadvm_postcopy_ram_handle_discard(); | |
1870 | ||
1871 | switch (ps) { | |
1872 | case POSTCOPY_INCOMING_ADVISE: | |
1873 | /* 1st discard */ | |
f9527107 | 1874 | tmp = postcopy_ram_prepare_discard(mis); |
093e3c42 DDAG |
1875 | if (tmp) { |
1876 | return tmp; | |
1877 | } | |
1878 | break; | |
1879 | ||
1880 | case POSTCOPY_INCOMING_DISCARD: | |
1881 | /* Expected state */ | |
1882 | break; | |
1883 | ||
1884 | default: | |
1885 | error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)", | |
1886 | ps); | |
1887 | return -1; | |
1888 | } | |
1889 | /* We're expecting a | |
1890 | * Version (0) | |
1891 | * a RAM ID string (length byte, name, 0 term) | |
1892 | * then at least 1 16 byte chunk | |
1893 | */ | |
1894 | if (len < (1 + 1 + 1 + 1 + 2 * 8)) { | |
1895 | error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len); | |
1896 | return -1; | |
1897 | } | |
1898 | ||
1899 | tmp = qemu_get_byte(mis->from_src_file); | |
1900 | if (tmp != postcopy_ram_discard_version) { | |
1901 | error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp); | |
1902 | return -1; | |
1903 | } | |
1904 | ||
1905 | if (!qemu_get_counted_string(mis->from_src_file, ramid)) { | |
1906 | error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID"); | |
1907 | return -1; | |
1908 | } | |
1909 | tmp = qemu_get_byte(mis->from_src_file); | |
1910 | if (tmp != 0) { | |
1911 | error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp); | |
1912 | return -1; | |
1913 | } | |
1914 | ||
1915 | len -= 3 + strlen(ramid); | |
1916 | if (len % 16) { | |
1917 | error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len); | |
1918 | return -1; | |
1919 | } | |
1920 | trace_loadvm_postcopy_ram_handle_discard_header(ramid, len); | |
1921 | while (len) { | |
093e3c42 DDAG |
1922 | uint64_t start_addr, block_length; |
1923 | start_addr = qemu_get_be64(mis->from_src_file); | |
1924 | block_length = qemu_get_be64(mis->from_src_file); | |
1925 | ||
1926 | len -= 16; | |
aaa2064c | 1927 | int ret = ram_discard_range(ramid, start_addr, block_length); |
093e3c42 DDAG |
1928 | if (ret) { |
1929 | return ret; | |
1930 | } | |
093e3c42 DDAG |
1931 | } |
1932 | trace_loadvm_postcopy_ram_handle_discard_end(); | |
1933 | ||
1934 | return 0; | |
1935 | } | |
1936 | ||
c76201ab DDAG |
1937 | /* |
1938 | * Triggered by a postcopy_listen command; this thread takes over reading | |
1939 | * the input stream, leaving the main thread free to carry on loading the rest | |
1940 | * of the device state (from RAM). | |
1941 | * (TODO:This could do with being in a postcopy file - but there again it's | |
1942 | * just another input loop, not that postcopy specific) | |
1943 | */ | |
1944 | static void *postcopy_ram_listen_thread(void *opaque) | |
1945 | { | |
c76201ab | 1946 | MigrationIncomingState *mis = migration_incoming_get_current(); |
b411b844 | 1947 | QEMUFile *f = mis->from_src_file; |
c76201ab | 1948 | int load_res; |
ee647225 VSO |
1949 | MigrationState *migr = migrate_get_current(); |
1950 | ||
1951 | object_ref(OBJECT(migr)); | |
c76201ab | 1952 | |
6ba996bb DDAG |
1953 | migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, |
1954 | MIGRATION_STATUS_POSTCOPY_ACTIVE); | |
095c12a4 | 1955 | qemu_sem_post(&mis->thread_sync_sem); |
c76201ab DDAG |
1956 | trace_postcopy_ram_listen_thread_start(); |
1957 | ||
74637e6f | 1958 | rcu_register_thread(); |
c76201ab DDAG |
1959 | /* |
1960 | * Because we're a thread and not a coroutine we can't yield | |
1961 | * in qemu_file, and thus we must be blocking now. | |
1962 | */ | |
1963 | qemu_file_set_blocking(f, true); | |
1964 | load_res = qemu_loadvm_state_main(f, mis); | |
b411b844 PX |
1965 | |
1966 | /* | |
1967 | * This is tricky, but, mis->from_src_file can change after it | |
1968 | * returns, when postcopy recovery happened. In the future, we may | |
1969 | * want a wrapper for the QEMUFile handle. | |
1970 | */ | |
1971 | f = mis->from_src_file; | |
1972 | ||
c76201ab DDAG |
1973 | /* And non-blocking again so we don't block in any cleanup */ |
1974 | qemu_file_set_blocking(f, false); | |
1975 | ||
1976 | trace_postcopy_ram_listen_thread_exit(); | |
1977 | if (load_res < 0) { | |
c76201ab | 1978 | qemu_file_set_error(f, load_res); |
ee647225 VSO |
1979 | dirty_bitmap_mig_cancel_incoming(); |
1980 | if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING && | |
1981 | !migrate_postcopy_ram() && migrate_dirty_bitmaps()) | |
1982 | { | |
1983 | error_report("%s: loadvm failed during postcopy: %d. All states " | |
1984 | "are migrated except dirty bitmaps. Some dirty " | |
1985 | "bitmaps may be lost, and present migrated dirty " | |
1986 | "bitmaps are correctly migrated and valid.", | |
1987 | __func__, load_res); | |
1988 | load_res = 0; /* prevent further exit() */ | |
1989 | } else { | |
1990 | error_report("%s: loadvm failed: %d", __func__, load_res); | |
1991 | migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, | |
1992 | MIGRATION_STATUS_FAILED); | |
1993 | } | |
1994 | } | |
1995 | if (load_res >= 0) { | |
c76201ab DDAG |
1996 | /* |
1997 | * This looks good, but it's possible that the device loading in the | |
1998 | * main thread hasn't finished yet, and so we might not be in 'RUN' | |
1999 | * state yet; wait for the end of the main thread. | |
2000 | */ | |
2001 | qemu_event_wait(&mis->main_thread_load_event); | |
2002 | } | |
2003 | postcopy_ram_incoming_cleanup(mis); | |
c76201ab DDAG |
2004 | |
2005 | if (load_res < 0) { | |
2006 | /* | |
2007 | * If something went wrong then we have a bad state so exit; | |
2008 | * depending how far we got it might be possible at this point | |
2009 | * to leave the guest running and fire MCEs for pages that never | |
2010 | * arrived as a desperate recovery step. | |
2011 | */ | |
74637e6f | 2012 | rcu_unregister_thread(); |
c76201ab DDAG |
2013 | exit(EXIT_FAILURE); |
2014 | } | |
2015 | ||
6ba996bb DDAG |
2016 | migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, |
2017 | MIGRATION_STATUS_COMPLETED); | |
2018 | /* | |
2019 | * If everything has worked fine, then the main thread has waited | |
2020 | * for us to start, and we're the last use of the mis. | |
2021 | * (If something broke then qemu will have to exit anyway since it's | |
2022 | * got a bad migration state). | |
2023 | */ | |
2024 | migration_incoming_state_destroy(); | |
acb5ea86 | 2025 | qemu_loadvm_state_cleanup(); |
6ba996bb | 2026 | |
74637e6f | 2027 | rcu_unregister_thread(); |
9cf4bb87 | 2028 | mis->have_listen_thread = false; |
2d49bacd WY |
2029 | postcopy_state_set(POSTCOPY_INCOMING_END); |
2030 | ||
ee647225 VSO |
2031 | object_unref(OBJECT(migr)); |
2032 | ||
c76201ab DDAG |
2033 | return NULL; |
2034 | } | |
2035 | ||
093e3c42 DDAG |
2036 | /* After this message we must be able to immediately receive postcopy data */ |
2037 | static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis) | |
2038 | { | |
2039 | PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_LISTENING); | |
6864a7b5 DDAG |
2040 | Error *local_err = NULL; |
2041 | ||
c84f976e PX |
2042 | trace_loadvm_postcopy_handle_listen("enter"); |
2043 | ||
093e3c42 DDAG |
2044 | if (ps != POSTCOPY_INCOMING_ADVISE && ps != POSTCOPY_INCOMING_DISCARD) { |
2045 | error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps); | |
2046 | return -1; | |
2047 | } | |
f9527107 DDAG |
2048 | if (ps == POSTCOPY_INCOMING_ADVISE) { |
2049 | /* | |
2050 | * A rare case, we entered listen without having to do any discards, | |
2051 | * so do the setup that's normally done at the time of the 1st discard. | |
2052 | */ | |
58110f0a VSO |
2053 | if (migrate_postcopy_ram()) { |
2054 | postcopy_ram_prepare_discard(mis); | |
2055 | } | |
f9527107 | 2056 | } |
093e3c42 | 2057 | |
c84f976e PX |
2058 | trace_loadvm_postcopy_handle_listen("after discard"); |
2059 | ||
f0a227ad DDAG |
2060 | /* |
2061 | * Sensitise RAM - can now generate requests for blocks that don't exist | |
2062 | * However, at this point the CPU shouldn't be running, and the IO | |
2063 | * shouldn't be doing anything yet so don't actually expect requests | |
2064 | */ | |
58110f0a | 2065 | if (migrate_postcopy_ram()) { |
2a7eb148 | 2066 | if (postcopy_ram_incoming_setup(mis)) { |
91b02dc7 | 2067 | postcopy_ram_incoming_cleanup(mis); |
58110f0a VSO |
2068 | return -1; |
2069 | } | |
f0a227ad DDAG |
2070 | } |
2071 | ||
c84f976e PX |
2072 | trace_loadvm_postcopy_handle_listen("after uffd"); |
2073 | ||
6864a7b5 DDAG |
2074 | if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN, &local_err)) { |
2075 | error_report_err(local_err); | |
2076 | return -1; | |
2077 | } | |
2078 | ||
c76201ab | 2079 | mis->have_listen_thread = true; |
095c12a4 PX |
2080 | postcopy_thread_create(mis, &mis->listen_thread, "postcopy/listen", |
2081 | postcopy_ram_listen_thread, QEMU_THREAD_DETACHED); | |
c84f976e PX |
2082 | trace_loadvm_postcopy_handle_listen("return"); |
2083 | ||
093e3c42 DDAG |
2084 | return 0; |
2085 | } | |
2086 | ||
ea6a55bc | 2087 | static void loadvm_postcopy_handle_run_bh(void *opaque) |
093e3c42 | 2088 | { |
27c6825b | 2089 | Error *local_err = NULL; |
1ce54262 | 2090 | MigrationIncomingState *mis = opaque; |
093e3c42 | 2091 | |
b9a040b9 PX |
2092 | trace_loadvm_postcopy_handle_run_bh("enter"); |
2093 | ||
27c6825b DDAG |
2094 | /* TODO we should move all of this lot into postcopy_ram.c or a shared code |
2095 | * in migration.c | |
2096 | */ | |
2097 | cpu_synchronize_all_post_init(); | |
2098 | ||
b9a040b9 PX |
2099 | trace_loadvm_postcopy_handle_run_bh("after cpu sync"); |
2100 | ||
7659505c | 2101 | qemu_announce_self(&mis->announce_timer, migrate_announce_params()); |
27c6825b | 2102 | |
b9a040b9 PX |
2103 | trace_loadvm_postcopy_handle_run_bh("after announce"); |
2104 | ||
3b717194 | 2105 | /* Make sure all file formats throw away their mutable metadata. |
ace21a58 | 2106 | * If we get an error here, just don't restart the VM yet. */ |
3b717194 | 2107 | bdrv_activate_all(&local_err); |
0042fd36 | 2108 | if (local_err) { |
ace21a58 | 2109 | error_report_err(local_err); |
0042fd36 KW |
2110 | local_err = NULL; |
2111 | autostart = false; | |
2112 | } | |
2113 | ||
b9a040b9 | 2114 | trace_loadvm_postcopy_handle_run_bh("after invalidate cache"); |
27c6825b | 2115 | |
b35ebdf0 VSO |
2116 | dirty_bitmap_mig_before_vm_start(); |
2117 | ||
093e3c42 DDAG |
2118 | if (autostart) { |
2119 | /* Hold onto your hats, starting the CPU */ | |
2120 | vm_start(); | |
2121 | } else { | |
2122 | /* leave it paused and let management decide when to start the CPU */ | |
2123 | runstate_set(RUN_STATE_PAUSED); | |
2124 | } | |
2125 | ||
1ce54262 | 2126 | qemu_bh_delete(mis->bh); |
b9a040b9 PX |
2127 | |
2128 | trace_loadvm_postcopy_handle_run_bh("return"); | |
ea6a55bc DL |
2129 | } |
2130 | ||
2131 | /* After all discards we can start running and asking for pages */ | |
2132 | static int loadvm_postcopy_handle_run(MigrationIncomingState *mis) | |
2133 | { | |
0197d890 | 2134 | PostcopyState ps = postcopy_state_get(); |
ea6a55bc DL |
2135 | |
2136 | trace_loadvm_postcopy_handle_run(); | |
2137 | if (ps != POSTCOPY_INCOMING_LISTENING) { | |
2138 | error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps); | |
2139 | return -1; | |
2140 | } | |
2141 | ||
0197d890 | 2142 | postcopy_state_set(POSTCOPY_INCOMING_RUNNING); |
1ce54262 WY |
2143 | mis->bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, mis); |
2144 | qemu_bh_schedule(mis->bh); | |
ea6a55bc | 2145 | |
27c6825b DDAG |
2146 | /* We need to finish reading the stream from the package |
2147 | * and also stop reading anything more from the stream that loaded the | |
2148 | * package (since it's now being read by the listener thread). | |
2149 | * LOADVM_QUIT will quit all the layers of nested loadvm loops. | |
2150 | */ | |
2151 | return LOADVM_QUIT; | |
093e3c42 DDAG |
2152 | } |
2153 | ||
0c26781c PX |
2154 | /* We must be with page_request_mutex held */ |
2155 | static gboolean postcopy_sync_page_req(gpointer key, gpointer value, | |
2156 | gpointer data) | |
2157 | { | |
2158 | MigrationIncomingState *mis = data; | |
2159 | void *host_addr = (void *) key; | |
2160 | ram_addr_t rb_offset; | |
2161 | RAMBlock *rb; | |
2162 | int ret; | |
2163 | ||
2164 | rb = qemu_ram_block_from_host(host_addr, true, &rb_offset); | |
2165 | if (!rb) { | |
2166 | /* | |
2167 | * This should _never_ happen. However be nice for a migrating VM to | |
2168 | * not crash/assert. Post an error (note: intended to not use *_once | |
2169 | * because we do want to see all the illegal addresses; and this can | |
2170 | * never be triggered by the guest so we're safe) and move on next. | |
2171 | */ | |
2172 | error_report("%s: illegal host addr %p", __func__, host_addr); | |
2173 | /* Try the next entry */ | |
2174 | return FALSE; | |
2175 | } | |
2176 | ||
2177 | ret = migrate_send_rp_message_req_pages(mis, rb, rb_offset); | |
2178 | if (ret) { | |
2179 | /* Please refer to above comment. */ | |
2180 | error_report("%s: send rp message failed for addr %p", | |
2181 | __func__, host_addr); | |
2182 | return FALSE; | |
2183 | } | |
2184 | ||
2185 | trace_postcopy_page_req_sync(host_addr); | |
2186 | ||
2187 | return FALSE; | |
2188 | } | |
2189 | ||
2190 | static void migrate_send_rp_req_pages_pending(MigrationIncomingState *mis) | |
2191 | { | |
2192 | WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) { | |
2193 | g_tree_foreach(mis->page_requested, postcopy_sync_page_req, mis); | |
2194 | } | |
2195 | } | |
2196 | ||
3f5875ec PX |
2197 | static int loadvm_postcopy_handle_resume(MigrationIncomingState *mis) |
2198 | { | |
2199 | if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) { | |
2200 | error_report("%s: illegal resume received", __func__); | |
2201 | /* Don't fail the load, only for this. */ | |
2202 | return 0; | |
2203 | } | |
2204 | ||
cc5ab872 PX |
2205 | /* |
2206 | * Reset the last_rb before we resend any page req to source again, since | |
2207 | * the source should have it reset already. | |
2208 | */ | |
2209 | mis->last_rb = NULL; | |
2210 | ||
3f5875ec PX |
2211 | /* |
2212 | * This means source VM is ready to resume the postcopy migration. | |
3f5875ec PX |
2213 | */ |
2214 | migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_RECOVER, | |
2215 | MIGRATION_STATUS_POSTCOPY_ACTIVE); | |
3f5875ec PX |
2216 | |
2217 | trace_loadvm_postcopy_handle_resume(); | |
2218 | ||
13955b89 PX |
2219 | /* Tell source that "we are ready" */ |
2220 | migrate_send_rp_resume_ack(mis, MIGRATION_RESUME_ACK_VALUE); | |
3f5875ec | 2221 | |
0c26781c PX |
2222 | /* |
2223 | * After a postcopy recovery, the source should have lost the postcopy | |
2224 | * queue, or potentially the requested pages could have been lost during | |
2225 | * the network down phase. Let's re-sync with the source VM by re-sending | |
2226 | * all the pending pages that we eagerly need, so these threads won't get | |
2227 | * blocked too long due to the recovery. | |
2228 | * | |
2229 | * Without this procedure, the faulted destination VM threads (waiting for | |
2230 | * page requests right before the postcopy is interrupted) can keep hanging | |
2231 | * until the pages are sent by the source during the background copying of | |
2232 | * pages, or another thread faulted on the same address accidentally. | |
2233 | */ | |
2234 | migrate_send_rp_req_pages_pending(mis); | |
2235 | ||
5e773431 PX |
2236 | /* |
2237 | * It's time to switch state and release the fault thread to continue | |
2238 | * service page faults. Note that this should be explicitly after the | |
2239 | * above call to migrate_send_rp_req_pages_pending(). In short: | |
2240 | * migrate_send_rp_message_req_pages() is not thread safe, yet. | |
2241 | */ | |
2242 | qemu_sem_post(&mis->postcopy_pause_sem_fault); | |
2243 | ||
60bb3c58 | 2244 | if (migrate_postcopy_preempt()) { |
5655aab0 PX |
2245 | /* |
2246 | * The preempt channel will be created in async manner, now let's | |
2247 | * wait for it and make sure it's created. | |
2248 | */ | |
2249 | qemu_sem_wait(&mis->postcopy_qemufile_dst_done); | |
60bb3c58 PX |
2250 | assert(mis->postcopy_qemufile_dst); |
2251 | /* Kick the fast ram load thread too */ | |
2252 | qemu_sem_post(&mis->postcopy_pause_sem_fast_load); | |
2253 | } | |
2254 | ||
3f5875ec PX |
2255 | return 0; |
2256 | } | |
2257 | ||
c76ca188 | 2258 | /** |
11cf1d98 DDAG |
2259 | * Immediately following this command is a blob of data containing an embedded |
2260 | * chunk of migration stream; read it and load it. | |
2261 | * | |
2262 | * @mis: Incoming state | |
2263 | * @length: Length of packaged data to read | |
c76ca188 | 2264 | * |
11cf1d98 DDAG |
2265 | * Returns: Negative values on error |
2266 | * | |
2267 | */ | |
2268 | static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis) | |
2269 | { | |
2270 | int ret; | |
61b67d47 DB |
2271 | size_t length; |
2272 | QIOChannelBuffer *bioc; | |
11cf1d98 DDAG |
2273 | |
2274 | length = qemu_get_be32(mis->from_src_file); | |
2275 | trace_loadvm_handle_cmd_packaged(length); | |
2276 | ||
2277 | if (length > MAX_VM_CMD_PACKAGED_SIZE) { | |
61b67d47 | 2278 | error_report("Unreasonably large packaged state: %zu", length); |
11cf1d98 DDAG |
2279 | return -1; |
2280 | } | |
61b67d47 DB |
2281 | |
2282 | bioc = qio_channel_buffer_new(length); | |
6f01f136 | 2283 | qio_channel_set_name(QIO_CHANNEL(bioc), "migration-loadvm-buffer"); |
61b67d47 DB |
2284 | ret = qemu_get_buffer(mis->from_src_file, |
2285 | bioc->data, | |
2286 | length); | |
11cf1d98 | 2287 | if (ret != length) { |
61b67d47 DB |
2288 | object_unref(OBJECT(bioc)); |
2289 | error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu", | |
9af9e0fe | 2290 | ret, length); |
11cf1d98 DDAG |
2291 | return (ret < 0) ? ret : -EAGAIN; |
2292 | } | |
61b67d47 | 2293 | bioc->usage += length; |
11cf1d98 DDAG |
2294 | trace_loadvm_handle_cmd_packaged_received(ret); |
2295 | ||
77ef2dc1 | 2296 | QEMUFile *packf = qemu_file_new_input(QIO_CHANNEL(bioc)); |
11cf1d98 DDAG |
2297 | |
2298 | ret = qemu_loadvm_state_main(packf, mis); | |
2299 | trace_loadvm_handle_cmd_packaged_main(ret); | |
2300 | qemu_fclose(packf); | |
61b67d47 | 2301 | object_unref(OBJECT(bioc)); |
11cf1d98 DDAG |
2302 | |
2303 | return ret; | |
2304 | } | |
2305 | ||
f25d4225 PX |
2306 | /* |
2307 | * Handle request that source requests for recved_bitmap on | |
2308 | * destination. Payload format: | |
2309 | * | |
2310 | * len (1 byte) + ramblock_name (<255 bytes) | |
2311 | */ | |
2312 | static int loadvm_handle_recv_bitmap(MigrationIncomingState *mis, | |
2313 | uint16_t len) | |
2314 | { | |
2315 | QEMUFile *file = mis->from_src_file; | |
2316 | RAMBlock *rb; | |
2317 | char block_name[256]; | |
2318 | size_t cnt; | |
2319 | ||
2320 | cnt = qemu_get_counted_string(file, block_name); | |
2321 | if (!cnt) { | |
2322 | error_report("%s: failed to read block name", __func__); | |
2323 | return -EINVAL; | |
2324 | } | |
2325 | ||
2326 | /* Validate before using the data */ | |
2327 | if (qemu_file_get_error(file)) { | |
2328 | return qemu_file_get_error(file); | |
2329 | } | |
2330 | ||
2331 | if (len != cnt + 1) { | |
2332 | error_report("%s: invalid payload length (%d)", __func__, len); | |
2333 | return -EINVAL; | |
2334 | } | |
2335 | ||
2336 | rb = qemu_ram_block_by_name(block_name); | |
2337 | if (!rb) { | |
2338 | error_report("%s: block '%s' not found", __func__, block_name); | |
2339 | return -EINVAL; | |
2340 | } | |
2341 | ||
a335debb | 2342 | migrate_send_rp_recv_bitmap(mis, block_name); |
f25d4225 PX |
2343 | |
2344 | trace_loadvm_handle_recv_bitmap(block_name); | |
2345 | ||
2346 | return 0; | |
2347 | } | |
2348 | ||
aad555c2 ZC |
2349 | static int loadvm_process_enable_colo(MigrationIncomingState *mis) |
2350 | { | |
18b1d3c9 DH |
2351 | int ret = migration_incoming_enable_colo(); |
2352 | ||
2353 | if (!ret) { | |
2354 | ret = colo_init_ram_cache(); | |
2355 | if (ret) { | |
2356 | migration_incoming_disable_colo(); | |
2357 | } | |
2358 | } | |
2359 | return ret; | |
aad555c2 ZC |
2360 | } |
2361 | ||
11cf1d98 DDAG |
2362 | /* |
2363 | * Process an incoming 'QEMU_VM_COMMAND' | |
2364 | * 0 just a normal return | |
2365 | * LOADVM_QUIT All good, but exit the loop | |
2366 | * <0 Error | |
c76ca188 DDAG |
2367 | */ |
2368 | static int loadvm_process_command(QEMUFile *f) | |
2369 | { | |
2e37701e | 2370 | MigrationIncomingState *mis = migration_incoming_get_current(); |
c76ca188 DDAG |
2371 | uint16_t cmd; |
2372 | uint16_t len; | |
2e37701e | 2373 | uint32_t tmp32; |
c76ca188 DDAG |
2374 | |
2375 | cmd = qemu_get_be16(f); | |
2376 | len = qemu_get_be16(f); | |
2377 | ||
7a9ddfbf PX |
2378 | /* Check validity before continue processing of cmds */ |
2379 | if (qemu_file_get_error(f)) { | |
2380 | return qemu_file_get_error(f); | |
2381 | } | |
2382 | ||
c76ca188 DDAG |
2383 | if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) { |
2384 | error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len); | |
2385 | return -EINVAL; | |
2386 | } | |
2387 | ||
a7060ba3 PX |
2388 | trace_loadvm_process_command(mig_cmd_args[cmd].name, len); |
2389 | ||
c76ca188 DDAG |
2390 | if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) { |
2391 | error_report("%s received with bad length - expecting %zu, got %d", | |
2392 | mig_cmd_args[cmd].name, | |
2393 | (size_t)mig_cmd_args[cmd].len, len); | |
2394 | return -ERANGE; | |
2395 | } | |
2396 | ||
2397 | switch (cmd) { | |
2e37701e DDAG |
2398 | case MIG_CMD_OPEN_RETURN_PATH: |
2399 | if (mis->to_src_file) { | |
2400 | error_report("CMD_OPEN_RETURN_PATH called when RP already open"); | |
2401 | /* Not really a problem, so don't give up */ | |
2402 | return 0; | |
2403 | } | |
2404 | mis->to_src_file = qemu_file_get_return_path(f); | |
2405 | if (!mis->to_src_file) { | |
2406 | error_report("CMD_OPEN_RETURN_PATH failed"); | |
2407 | return -1; | |
2408 | } | |
1b4adb10 AH |
2409 | |
2410 | /* | |
2411 | * Switchover ack is enabled but no device uses it, so send an ACK to | |
2412 | * source that it's OK to switchover. Do it here, after return path has | |
2413 | * been created. | |
2414 | */ | |
2415 | if (migrate_switchover_ack() && !mis->switchover_ack_pending_num) { | |
2416 | int ret = migrate_send_rp_switchover_ack(mis); | |
2417 | if (ret) { | |
2418 | error_report( | |
2419 | "Could not send switchover ack RP MSG, err %d (%s)", ret, | |
2420 | strerror(-ret)); | |
2421 | return ret; | |
2422 | } | |
2423 | } | |
2e37701e DDAG |
2424 | break; |
2425 | ||
2426 | case MIG_CMD_PING: | |
2427 | tmp32 = qemu_get_be32(f); | |
2428 | trace_loadvm_process_command_ping(tmp32); | |
2429 | if (!mis->to_src_file) { | |
2430 | error_report("CMD_PING (0x%x) received with no return path", | |
2431 | tmp32); | |
2432 | return -1; | |
2433 | } | |
6decec93 | 2434 | migrate_send_rp_pong(mis, tmp32); |
2e37701e | 2435 | break; |
093e3c42 | 2436 | |
11cf1d98 DDAG |
2437 | case MIG_CMD_PACKAGED: |
2438 | return loadvm_handle_cmd_packaged(mis); | |
2439 | ||
093e3c42 | 2440 | case MIG_CMD_POSTCOPY_ADVISE: |
875fcd01 | 2441 | return loadvm_postcopy_handle_advise(mis, len); |
093e3c42 DDAG |
2442 | |
2443 | case MIG_CMD_POSTCOPY_LISTEN: | |
2444 | return loadvm_postcopy_handle_listen(mis); | |
2445 | ||
2446 | case MIG_CMD_POSTCOPY_RUN: | |
2447 | return loadvm_postcopy_handle_run(mis); | |
2448 | ||
2449 | case MIG_CMD_POSTCOPY_RAM_DISCARD: | |
2450 | return loadvm_postcopy_ram_handle_discard(mis, len); | |
f25d4225 | 2451 | |
3f5875ec PX |
2452 | case MIG_CMD_POSTCOPY_RESUME: |
2453 | return loadvm_postcopy_handle_resume(mis); | |
2454 | ||
f25d4225 PX |
2455 | case MIG_CMD_RECV_BITMAP: |
2456 | return loadvm_handle_recv_bitmap(mis, len); | |
aad555c2 ZC |
2457 | |
2458 | case MIG_CMD_ENABLE_COLO: | |
2459 | return loadvm_process_enable_colo(mis); | |
c76ca188 DDAG |
2460 | } |
2461 | ||
2462 | return 0; | |
2463 | } | |
2464 | ||
59f39a47 DDAG |
2465 | /* |
2466 | * Read a footer off the wire and check that it matches the expected section | |
2467 | * | |
2468 | * Returns: true if the footer was good | |
2469 | * false if there is a problem (and calls error_report to say why) | |
2470 | */ | |
0f42f657 | 2471 | static bool check_section_footer(QEMUFile *f, SaveStateEntry *se) |
59f39a47 | 2472 | { |
7a9ddfbf | 2473 | int ret; |
59f39a47 DDAG |
2474 | uint8_t read_mark; |
2475 | uint32_t read_section_id; | |
2476 | ||
15c38503 | 2477 | if (!migrate_get_current()->send_section_footer) { |
59f39a47 DDAG |
2478 | /* No footer to check */ |
2479 | return true; | |
2480 | } | |
2481 | ||
2482 | read_mark = qemu_get_byte(f); | |
2483 | ||
7a9ddfbf PX |
2484 | ret = qemu_file_get_error(f); |
2485 | if (ret) { | |
2486 | error_report("%s: Read section footer failed: %d", | |
2487 | __func__, ret); | |
2488 | return false; | |
2489 | } | |
2490 | ||
59f39a47 | 2491 | if (read_mark != QEMU_VM_SECTION_FOOTER) { |
0f42f657 | 2492 | error_report("Missing section footer for %s", se->idstr); |
59f39a47 DDAG |
2493 | return false; |
2494 | } | |
2495 | ||
2496 | read_section_id = qemu_get_be32(f); | |
0f42f657 | 2497 | if (read_section_id != se->load_section_id) { |
59f39a47 DDAG |
2498 | error_report("Mismatched section id in footer for %s -" |
2499 | " read 0x%x expected 0x%x", | |
0f42f657 | 2500 | se->idstr, read_section_id, se->load_section_id); |
59f39a47 DDAG |
2501 | return false; |
2502 | } | |
2503 | ||
2504 | /* All good */ | |
2505 | return true; | |
2506 | } | |
2507 | ||
fb3520a8 HZ |
2508 | static int |
2509 | qemu_loadvm_section_start_full(QEMUFile *f, MigrationIncomingState *mis) | |
2510 | { | |
2511 | uint32_t instance_id, version_id, section_id; | |
2512 | SaveStateEntry *se; | |
fb3520a8 HZ |
2513 | char idstr[256]; |
2514 | int ret; | |
2515 | ||
2516 | /* Read section start */ | |
2517 | section_id = qemu_get_be32(f); | |
2518 | if (!qemu_get_counted_string(f, idstr)) { | |
2519 | error_report("Unable to read ID string for section %u", | |
2520 | section_id); | |
2521 | return -EINVAL; | |
2522 | } | |
2523 | instance_id = qemu_get_be32(f); | |
2524 | version_id = qemu_get_be32(f); | |
2525 | ||
7a9ddfbf PX |
2526 | ret = qemu_file_get_error(f); |
2527 | if (ret) { | |
2528 | error_report("%s: Failed to read instance/version ID: %d", | |
2529 | __func__, ret); | |
2530 | return ret; | |
2531 | } | |
2532 | ||
fb3520a8 HZ |
2533 | trace_qemu_loadvm_state_section_startfull(section_id, idstr, |
2534 | instance_id, version_id); | |
2535 | /* Find savevm section */ | |
2536 | se = find_se(idstr, instance_id); | |
2537 | if (se == NULL) { | |
93062e23 | 2538 | error_report("Unknown savevm section or instance '%s' %"PRIu32". " |
827beacb JRZ |
2539 | "Make sure that your current VM setup matches your " |
2540 | "saved VM setup, including any hotplugged devices", | |
fb3520a8 HZ |
2541 | idstr, instance_id); |
2542 | return -EINVAL; | |
2543 | } | |
2544 | ||
2545 | /* Validate version */ | |
2546 | if (version_id > se->version_id) { | |
2547 | error_report("savevm: unsupported version %d for '%s' v%d", | |
2548 | version_id, idstr, se->version_id); | |
2549 | return -EINVAL; | |
2550 | } | |
0f42f657 JQ |
2551 | se->load_version_id = version_id; |
2552 | se->load_section_id = section_id; | |
fb3520a8 | 2553 | |
88c16567 WC |
2554 | /* Validate if it is a device's state */ |
2555 | if (xen_enabled() && se->is_ram) { | |
2556 | error_report("loadvm: %s RAM loading not allowed on Xen", idstr); | |
2557 | return -EINVAL; | |
2558 | } | |
2559 | ||
3a011c26 | 2560 | ret = vmstate_load(f, se); |
fb3520a8 | 2561 | if (ret < 0) { |
93062e23 | 2562 | error_report("error while loading state for instance 0x%"PRIx32" of" |
fb3520a8 HZ |
2563 | " device '%s'", instance_id, idstr); |
2564 | return ret; | |
2565 | } | |
0f42f657 | 2566 | if (!check_section_footer(f, se)) { |
fb3520a8 HZ |
2567 | return -EINVAL; |
2568 | } | |
2569 | ||
2570 | return 0; | |
2571 | } | |
2572 | ||
2573 | static int | |
2574 | qemu_loadvm_section_part_end(QEMUFile *f, MigrationIncomingState *mis) | |
2575 | { | |
2576 | uint32_t section_id; | |
0f42f657 | 2577 | SaveStateEntry *se; |
fb3520a8 HZ |
2578 | int ret; |
2579 | ||
2580 | section_id = qemu_get_be32(f); | |
2581 | ||
7a9ddfbf PX |
2582 | ret = qemu_file_get_error(f); |
2583 | if (ret) { | |
2584 | error_report("%s: Failed to read section ID: %d", | |
2585 | __func__, ret); | |
2586 | return ret; | |
2587 | } | |
2588 | ||
fb3520a8 | 2589 | trace_qemu_loadvm_state_section_partend(section_id); |
0f42f657 JQ |
2590 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
2591 | if (se->load_section_id == section_id) { | |
fb3520a8 HZ |
2592 | break; |
2593 | } | |
2594 | } | |
0f42f657 | 2595 | if (se == NULL) { |
fb3520a8 HZ |
2596 | error_report("Unknown savevm section %d", section_id); |
2597 | return -EINVAL; | |
2598 | } | |
2599 | ||
3a011c26 | 2600 | ret = vmstate_load(f, se); |
fb3520a8 HZ |
2601 | if (ret < 0) { |
2602 | error_report("error while loading state section id %d(%s)", | |
0f42f657 | 2603 | section_id, se->idstr); |
fb3520a8 HZ |
2604 | return ret; |
2605 | } | |
0f42f657 | 2606 | if (!check_section_footer(f, se)) { |
fb3520a8 HZ |
2607 | return -EINVAL; |
2608 | } | |
2609 | ||
2610 | return 0; | |
2611 | } | |
2612 | ||
16015d32 WY |
2613 | static int qemu_loadvm_state_header(QEMUFile *f) |
2614 | { | |
2615 | unsigned int v; | |
2616 | int ret; | |
2617 | ||
2618 | v = qemu_get_be32(f); | |
2619 | if (v != QEMU_VM_FILE_MAGIC) { | |
2620 | error_report("Not a migration stream"); | |
2621 | return -EINVAL; | |
2622 | } | |
2623 | ||
2624 | v = qemu_get_be32(f); | |
2625 | if (v == QEMU_VM_FILE_VERSION_COMPAT) { | |
2626 | error_report("SaveVM v2 format is obsolete and don't work anymore"); | |
2627 | return -ENOTSUP; | |
2628 | } | |
2629 | if (v != QEMU_VM_FILE_VERSION) { | |
2630 | error_report("Unsupported migration stream version"); | |
2631 | return -ENOTSUP; | |
2632 | } | |
2633 | ||
2634 | if (migrate_get_current()->send_configuration) { | |
2635 | if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) { | |
2636 | error_report("Configuration section missing"); | |
2637 | qemu_loadvm_state_cleanup(); | |
2638 | return -EINVAL; | |
2639 | } | |
2640 | ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0); | |
2641 | ||
2642 | if (ret) { | |
2643 | qemu_loadvm_state_cleanup(); | |
2644 | return ret; | |
2645 | } | |
2646 | } | |
2647 | return 0; | |
2648 | } | |
2649 | ||
1b4adb10 AH |
2650 | static void qemu_loadvm_state_switchover_ack_needed(MigrationIncomingState *mis) |
2651 | { | |
2652 | SaveStateEntry *se; | |
2653 | ||
2654 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { | |
2655 | if (!se->ops || !se->ops->switchover_ack_needed) { | |
2656 | continue; | |
2657 | } | |
2658 | ||
2659 | if (se->ops->switchover_ack_needed(se->opaque)) { | |
2660 | mis->switchover_ack_pending_num++; | |
2661 | } | |
2662 | } | |
2663 | ||
2664 | trace_loadvm_state_switchover_ack_needed(mis->switchover_ack_pending_num); | |
2665 | } | |
2666 | ||
acb5ea86 JQ |
2667 | static int qemu_loadvm_state_setup(QEMUFile *f) |
2668 | { | |
2669 | SaveStateEntry *se; | |
2670 | int ret; | |
2671 | ||
2672 | trace_loadvm_state_setup(); | |
2673 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { | |
2674 | if (!se->ops || !se->ops->load_setup) { | |
2675 | continue; | |
2676 | } | |
cea3b4c0 | 2677 | if (se->ops->is_active) { |
acb5ea86 JQ |
2678 | if (!se->ops->is_active(se->opaque)) { |
2679 | continue; | |
2680 | } | |
2681 | } | |
2682 | ||
2683 | ret = se->ops->load_setup(f, se->opaque); | |
2684 | if (ret < 0) { | |
2685 | qemu_file_set_error(f, ret); | |
2686 | error_report("Load state of device %s failed", se->idstr); | |
2687 | return ret; | |
2688 | } | |
2689 | } | |
2690 | return 0; | |
2691 | } | |
2692 | ||
2693 | void qemu_loadvm_state_cleanup(void) | |
2694 | { | |
2695 | SaveStateEntry *se; | |
2696 | ||
2697 | trace_loadvm_state_cleanup(); | |
2698 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { | |
2699 | if (se->ops && se->ops->load_cleanup) { | |
2700 | se->ops->load_cleanup(se->opaque); | |
2701 | } | |
2702 | } | |
2703 | } | |
2704 | ||
b411b844 PX |
2705 | /* Return true if we should continue the migration, or false. */ |
2706 | static bool postcopy_pause_incoming(MigrationIncomingState *mis) | |
2707 | { | |
77dadc3f PX |
2708 | int i; |
2709 | ||
b411b844 PX |
2710 | trace_postcopy_pause_incoming(); |
2711 | ||
ee647225 VSO |
2712 | assert(migrate_postcopy_ram()); |
2713 | ||
39675fff PX |
2714 | /* |
2715 | * Unregister yank with either from/to src would work, since ioc behind it | |
2716 | * is the same | |
2717 | */ | |
2718 | migration_ioc_unregister_yank_from_file(mis->from_src_file); | |
2719 | ||
b411b844 PX |
2720 | assert(mis->from_src_file); |
2721 | qemu_file_shutdown(mis->from_src_file); | |
2722 | qemu_fclose(mis->from_src_file); | |
2723 | mis->from_src_file = NULL; | |
2724 | ||
2725 | assert(mis->to_src_file); | |
2726 | qemu_file_shutdown(mis->to_src_file); | |
2727 | qemu_mutex_lock(&mis->rp_mutex); | |
2728 | qemu_fclose(mis->to_src_file); | |
2729 | mis->to_src_file = NULL; | |
2730 | qemu_mutex_unlock(&mis->rp_mutex); | |
2731 | ||
60bb3c58 PX |
2732 | /* |
2733 | * NOTE: this must happen before reset the PostcopyTmpPages below, | |
2734 | * otherwise it's racy to reset those fields when the fast load thread | |
2735 | * can be accessing it in parallel. | |
2736 | */ | |
2737 | if (mis->postcopy_qemufile_dst) { | |
2738 | qemu_file_shutdown(mis->postcopy_qemufile_dst); | |
2739 | /* Take the mutex to make sure the fast ram load thread halted */ | |
2740 | qemu_mutex_lock(&mis->postcopy_prio_thread_mutex); | |
2741 | migration_ioc_unregister_yank_from_file(mis->postcopy_qemufile_dst); | |
2742 | qemu_fclose(mis->postcopy_qemufile_dst); | |
2743 | mis->postcopy_qemufile_dst = NULL; | |
2744 | qemu_mutex_unlock(&mis->postcopy_prio_thread_mutex); | |
2745 | } | |
2746 | ||
0e99bb8f PX |
2747 | /* Current state can be either ACTIVE or RECOVER */ |
2748 | migrate_set_state(&mis->state, mis->state, | |
eed1cc78 PX |
2749 | MIGRATION_STATUS_POSTCOPY_PAUSED); |
2750 | ||
3a7804c3 PX |
2751 | /* Notify the fault thread for the invalidated file handle */ |
2752 | postcopy_fault_thread_notify(mis); | |
2753 | ||
36f62f11 PX |
2754 | /* |
2755 | * If network is interrupted, any temp page we received will be useless | |
2756 | * because we didn't mark them as "received" in receivedmap. After a | |
2757 | * proper recovery later (which will sync src dirty bitmap with receivedmap | |
2758 | * on dest) these cached small pages will be resent again. | |
2759 | */ | |
2760 | for (i = 0; i < mis->postcopy_channels; i++) { | |
2761 | postcopy_temp_page_reset(&mis->postcopy_tmp_pages[i]); | |
2762 | } | |
2763 | ||
b411b844 PX |
2764 | error_report("Detected IO failure for postcopy. " |
2765 | "Migration paused."); | |
2766 | ||
2767 | while (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) { | |
2768 | qemu_sem_wait(&mis->postcopy_pause_sem_dst); | |
2769 | } | |
2770 | ||
2771 | trace_postcopy_pause_incoming_continued(); | |
2772 | ||
2773 | return true; | |
2774 | } | |
2775 | ||
3f6df99d | 2776 | int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis) |
1a8f46f8 | 2777 | { |
a672b469 | 2778 | uint8_t section_type; |
ccb783c3 | 2779 | int ret = 0; |
61964c23 | 2780 | |
b411b844 | 2781 | retry: |
7a9ddfbf PX |
2782 | while (true) { |
2783 | section_type = qemu_get_byte(f); | |
2784 | ||
60bb3c58 PX |
2785 | ret = qemu_file_get_error_obj_any(f, mis->postcopy_qemufile_dst, NULL); |
2786 | if (ret) { | |
7a9ddfbf PX |
2787 | break; |
2788 | } | |
2789 | ||
a5df2a02 | 2790 | trace_qemu_loadvm_state_section(section_type); |
a672b469 AL |
2791 | switch (section_type) { |
2792 | case QEMU_VM_SECTION_START: | |
2793 | case QEMU_VM_SECTION_FULL: | |
fb3520a8 | 2794 | ret = qemu_loadvm_section_start_full(f, mis); |
b5a22e4a | 2795 | if (ret < 0) { |
ccb783c3 | 2796 | goto out; |
b5a22e4a | 2797 | } |
a672b469 AL |
2798 | break; |
2799 | case QEMU_VM_SECTION_PART: | |
2800 | case QEMU_VM_SECTION_END: | |
fb3520a8 | 2801 | ret = qemu_loadvm_section_part_end(f, mis); |
b5a22e4a | 2802 | if (ret < 0) { |
ccb783c3 | 2803 | goto out; |
b5a22e4a | 2804 | } |
a672b469 | 2805 | break; |
c76ca188 DDAG |
2806 | case QEMU_VM_COMMAND: |
2807 | ret = loadvm_process_command(f); | |
7b89bf27 | 2808 | trace_qemu_loadvm_state_section_command(ret); |
4695ce3f | 2809 | if ((ret < 0) || (ret == LOADVM_QUIT)) { |
ccb783c3 | 2810 | goto out; |
c76ca188 DDAG |
2811 | } |
2812 | break; | |
7a9ddfbf PX |
2813 | case QEMU_VM_EOF: |
2814 | /* This is the end of migration */ | |
2815 | goto out; | |
a672b469 | 2816 | default: |
6a64b644 | 2817 | error_report("Unknown savevm section type %d", section_type); |
ccb783c3 DDAG |
2818 | ret = -EINVAL; |
2819 | goto out; | |
a672b469 AL |
2820 | } |
2821 | } | |
2822 | ||
ccb783c3 DDAG |
2823 | out: |
2824 | if (ret < 0) { | |
2825 | qemu_file_set_error(f, ret); | |
b411b844 | 2826 | |
ee647225 VSO |
2827 | /* Cancel bitmaps incoming regardless of recovery */ |
2828 | dirty_bitmap_mig_cancel_incoming(); | |
2829 | ||
b411b844 | 2830 | /* |
fd037a65 PX |
2831 | * If we are during an active postcopy, then we pause instead |
2832 | * of bail out to at least keep the VM's dirty data. Note | |
2833 | * that POSTCOPY_INCOMING_LISTENING stage is still not enough, | |
2834 | * during which we're still receiving device states and we | |
2835 | * still haven't yet started the VM on destination. | |
ee647225 VSO |
2836 | * |
2837 | * Only RAM postcopy supports recovery. Still, if RAM postcopy is | |
2838 | * enabled, canceled bitmaps postcopy will not affect RAM postcopy | |
2839 | * recovering. | |
b411b844 PX |
2840 | */ |
2841 | if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING && | |
ee647225 | 2842 | migrate_postcopy_ram() && postcopy_pause_incoming(mis)) { |
b411b844 PX |
2843 | /* Reset f to point to the newly created channel */ |
2844 | f = mis->from_src_file; | |
2845 | goto retry; | |
2846 | } | |
ccb783c3 DDAG |
2847 | } |
2848 | return ret; | |
7b89bf27 DDAG |
2849 | } |
2850 | ||
2851 | int qemu_loadvm_state(QEMUFile *f) | |
2852 | { | |
2853 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
2854 | Error *local_err = NULL; | |
7b89bf27 DDAG |
2855 | int ret; |
2856 | ||
2857 | if (qemu_savevm_state_blocked(&local_err)) { | |
2858 | error_report_err(local_err); | |
2859 | return -EINVAL; | |
2860 | } | |
2861 | ||
16015d32 WY |
2862 | ret = qemu_loadvm_state_header(f); |
2863 | if (ret) { | |
2864 | return ret; | |
7b89bf27 DDAG |
2865 | } |
2866 | ||
9e14b849 WY |
2867 | if (qemu_loadvm_state_setup(f) != 0) { |
2868 | return -EINVAL; | |
2869 | } | |
2870 | ||
1b4adb10 AH |
2871 | if (migrate_switchover_ack()) { |
2872 | qemu_loadvm_state_switchover_ack_needed(mis); | |
2873 | } | |
2874 | ||
75e972da DG |
2875 | cpu_synchronize_all_pre_loadvm(); |
2876 | ||
7b89bf27 DDAG |
2877 | ret = qemu_loadvm_state_main(f, mis); |
2878 | qemu_event_set(&mis->main_thread_load_event); | |
2879 | ||
2880 | trace_qemu_loadvm_state_post_main(ret); | |
2881 | ||
c76201ab DDAG |
2882 | if (mis->have_listen_thread) { |
2883 | /* Listen thread still going, can't clean up yet */ | |
2884 | return ret; | |
2885 | } | |
2886 | ||
7b89bf27 DDAG |
2887 | if (ret == 0) { |
2888 | ret = qemu_file_get_error(f); | |
2889 | } | |
1925cebc AG |
2890 | |
2891 | /* | |
2892 | * Try to read in the VMDESC section as well, so that dumping tools that | |
2893 | * intercept our migration stream have the chance to see it. | |
2894 | */ | |
1aca9a5f DDAG |
2895 | |
2896 | /* We've got to be careful; if we don't read the data and just shut the fd | |
2897 | * then the sender can error if we close while it's still sending. | |
2898 | * We also mustn't read data that isn't there; some transports (RDMA) | |
2899 | * will stall waiting for that data when the source has already closed. | |
2900 | */ | |
7b89bf27 | 2901 | if (ret == 0 && should_send_vmdesc()) { |
1aca9a5f DDAG |
2902 | uint8_t *buf; |
2903 | uint32_t size; | |
7b89bf27 | 2904 | uint8_t section_type = qemu_get_byte(f); |
1aca9a5f DDAG |
2905 | |
2906 | if (section_type != QEMU_VM_VMDESCRIPTION) { | |
2907 | error_report("Expected vmdescription section, but got %d", | |
2908 | section_type); | |
2909 | /* | |
2910 | * It doesn't seem worth failing at this point since | |
2911 | * we apparently have an otherwise valid VM state | |
2912 | */ | |
2913 | } else { | |
2914 | buf = g_malloc(0x1000); | |
2915 | size = qemu_get_be32(f); | |
2916 | ||
2917 | while (size > 0) { | |
2918 | uint32_t read_chunk = MIN(size, 0x1000); | |
2919 | qemu_get_buffer(f, buf, read_chunk); | |
2920 | size -= read_chunk; | |
2921 | } | |
2922 | g_free(buf); | |
1925cebc | 2923 | } |
1925cebc AG |
2924 | } |
2925 | ||
acb5ea86 | 2926 | qemu_loadvm_state_cleanup(); |
ea375f9a JK |
2927 | cpu_synchronize_all_post_init(); |
2928 | ||
a672b469 AL |
2929 | return ret; |
2930 | } | |
2931 | ||
3f6df99d ZC |
2932 | int qemu_load_device_state(QEMUFile *f) |
2933 | { | |
2934 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
2935 | int ret; | |
2936 | ||
2937 | /* Load QEMU_VM_SECTION_FULL section */ | |
2938 | ret = qemu_loadvm_state_main(f, mis); | |
2939 | if (ret < 0) { | |
2940 | error_report("Failed to load device state: %d", ret); | |
2941 | return ret; | |
2942 | } | |
2943 | ||
2944 | cpu_synchronize_all_post_init(); | |
2945 | return 0; | |
2946 | } | |
2947 | ||
1b4adb10 AH |
2948 | int qemu_loadvm_approve_switchover(void) |
2949 | { | |
2950 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
2951 | ||
2952 | if (!mis->switchover_ack_pending_num) { | |
2953 | return -EINVAL; | |
2954 | } | |
2955 | ||
2956 | mis->switchover_ack_pending_num--; | |
2957 | trace_loadvm_approve_switchover(mis->switchover_ack_pending_num); | |
2958 | ||
2959 | if (mis->switchover_ack_pending_num) { | |
2960 | return 0; | |
2961 | } | |
2962 | ||
2963 | return migrate_send_rp_switchover_ack(mis); | |
2964 | } | |
2965 | ||
f1a9fcdd DB |
2966 | bool save_snapshot(const char *name, bool overwrite, const char *vmstate, |
2967 | bool has_devices, strList *devices, Error **errp) | |
a672b469 | 2968 | { |
e26f98e2 | 2969 | BlockDriverState *bs; |
80ef0586 | 2970 | QEMUSnapshotInfo sn1, *sn = &sn1; |
66270a47 | 2971 | int ret = -1, ret2; |
a672b469 AL |
2972 | QEMUFile *f; |
2973 | int saved_vm_running; | |
c2c9a466 | 2974 | uint64_t vm_state_size; |
85cd1cc6 | 2975 | g_autoptr(GDateTime) now = g_date_time_new_now_local(); |
79b3c12a | 2976 | AioContext *aio_context; |
a672b469 | 2977 | |
6b573efe EGE |
2978 | GLOBAL_STATE_CODE(); |
2979 | ||
5aaac467 | 2980 | if (migration_is_blocked(errp)) { |
7ea14df2 | 2981 | return false; |
5aaac467 PB |
2982 | } |
2983 | ||
377b21cc | 2984 | if (!replay_can_snapshot()) { |
4dd32b3d MA |
2985 | error_setg(errp, "Record/replay does not allow making snapshot " |
2986 | "right now. Try once more later."); | |
7ea14df2 | 2987 | return false; |
377b21cc PD |
2988 | } |
2989 | ||
f1a9fcdd | 2990 | if (!bdrv_all_can_snapshot(has_devices, devices, errp)) { |
7ea14df2 | 2991 | return false; |
feeee5ac MDCF |
2992 | } |
2993 | ||
0b461605 | 2994 | /* Delete old snapshots of the same name */ |
ac8c19ba | 2995 | if (name) { |
f781f841 | 2996 | if (overwrite) { |
f1a9fcdd DB |
2997 | if (bdrv_all_delete_snapshot(name, has_devices, |
2998 | devices, errp) < 0) { | |
f781f841 DB |
2999 | return false; |
3000 | } | |
3001 | } else { | |
f1a9fcdd | 3002 | ret2 = bdrv_all_has_snapshot(name, has_devices, devices, errp); |
f781f841 DB |
3003 | if (ret2 < 0) { |
3004 | return false; | |
3005 | } | |
3006 | if (ret2 == 1) { | |
3007 | error_setg(errp, | |
3008 | "Snapshot '%s' already exists in one or more devices", | |
3009 | name); | |
3010 | return false; | |
3011 | } | |
ac8c19ba | 3012 | } |
0b461605 DL |
3013 | } |
3014 | ||
f1a9fcdd | 3015 | bs = bdrv_all_find_vmstate_bs(vmstate, has_devices, devices, errp); |
7cb14481 | 3016 | if (bs == NULL) { |
7ea14df2 | 3017 | return false; |
a672b469 | 3018 | } |
79b3c12a | 3019 | aio_context = bdrv_get_aio_context(bs); |
a672b469 | 3020 | |
1354869c | 3021 | saved_vm_running = runstate_is_running(); |
560d027b | 3022 | |
c33f1829 | 3023 | global_state_store(); |
0461d5a6 | 3024 | vm_stop(RUN_STATE_SAVE_VM); |
a672b469 | 3025 | |
8649f2f9 SH |
3026 | bdrv_drain_all_begin(); |
3027 | ||
79b3c12a DL |
3028 | aio_context_acquire(aio_context); |
3029 | ||
cb499fb2 | 3030 | memset(sn, 0, sizeof(*sn)); |
a672b469 AL |
3031 | |
3032 | /* fill auxiliary fields */ | |
85cd1cc6 DB |
3033 | sn->date_sec = g_date_time_to_unix(now); |
3034 | sn->date_nsec = g_date_time_get_microsecond(now) * 1000; | |
bc72ad67 | 3035 | sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
b39847a5 PD |
3036 | if (replay_mode != REPLAY_MODE_NONE) { |
3037 | sn->icount = replay_get_current_icount(); | |
3038 | } else { | |
3039 | sn->icount = -1ULL; | |
3040 | } | |
a672b469 | 3041 | |
7d631a11 | 3042 | if (name) { |
80ef0586 | 3043 | pstrcpy(sn->name, sizeof(sn->name), name); |
7d631a11 | 3044 | } else { |
85cd1cc6 DB |
3045 | g_autofree char *autoname = g_date_time_format(now, "vm-%Y%m%d%H%M%S"); |
3046 | pstrcpy(sn->name, sizeof(sn->name), autoname); | |
7d631a11 MDCF |
3047 | } |
3048 | ||
a672b469 | 3049 | /* save the VM state */ |
45566e9c | 3050 | f = qemu_fopen_bdrv(bs, 1); |
a672b469 | 3051 | if (!f) { |
927d6638 | 3052 | error_setg(errp, "Could not open VM state file"); |
a672b469 AL |
3053 | goto the_end; |
3054 | } | |
927d6638 | 3055 | ret = qemu_savevm_state(f, errp); |
cf786549 | 3056 | vm_state_size = qemu_file_transferred_noflush(f); |
66270a47 | 3057 | ret2 = qemu_fclose(f); |
a672b469 | 3058 | if (ret < 0) { |
a672b469 AL |
3059 | goto the_end; |
3060 | } | |
66270a47 DL |
3061 | if (ret2 < 0) { |
3062 | ret = ret2; | |
3063 | goto the_end; | |
3064 | } | |
a672b469 | 3065 | |
17e2a4a4 SH |
3066 | /* The bdrv_all_create_snapshot() call that follows acquires the AioContext |
3067 | * for itself. BDRV_POLL_WHILE() does not support nested locking because | |
3068 | * it only releases the lock once. Therefore synchronous I/O will deadlock | |
3069 | * unless we release the AioContext before bdrv_all_create_snapshot(). | |
3070 | */ | |
3071 | aio_context_release(aio_context); | |
3072 | aio_context = NULL; | |
3073 | ||
f1a9fcdd DB |
3074 | ret = bdrv_all_create_snapshot(sn, bs, vm_state_size, |
3075 | has_devices, devices, errp); | |
a9085f9b | 3076 | if (ret < 0) { |
f1a9fcdd | 3077 | bdrv_all_delete_snapshot(sn->name, has_devices, devices, NULL); |
ac8c19ba | 3078 | goto the_end; |
a672b469 AL |
3079 | } |
3080 | ||
ac8c19ba PD |
3081 | ret = 0; |
3082 | ||
a672b469 | 3083 | the_end: |
17e2a4a4 SH |
3084 | if (aio_context) { |
3085 | aio_context_release(aio_context); | |
3086 | } | |
8649f2f9 SH |
3087 | |
3088 | bdrv_drain_all_end(); | |
3089 | ||
38ff78d3 | 3090 | if (saved_vm_running) { |
a672b469 | 3091 | vm_start(); |
38ff78d3 | 3092 | } |
7ea14df2 | 3093 | return ret == 0; |
ac8c19ba PD |
3094 | } |
3095 | ||
5d6c599f AP |
3096 | void qmp_xen_save_devices_state(const char *filename, bool has_live, bool live, |
3097 | Error **errp) | |
a7ae8355 SS |
3098 | { |
3099 | QEMUFile *f; | |
8925839f | 3100 | QIOChannelFile *ioc; |
a7ae8355 SS |
3101 | int saved_vm_running; |
3102 | int ret; | |
3103 | ||
5d6c599f AP |
3104 | if (!has_live) { |
3105 | /* live default to true so old version of Xen tool stack can have a | |
3a4452d8 | 3106 | * successful live migration */ |
5d6c599f AP |
3107 | live = true; |
3108 | } | |
3109 | ||
a7ae8355 SS |
3110 | saved_vm_running = runstate_is_running(); |
3111 | vm_stop(RUN_STATE_SAVE_VM); | |
c69adea4 | 3112 | global_state_store_running(); |
a7ae8355 | 3113 | |
b4deb9bf DM |
3114 | ioc = qio_channel_file_new_path(filename, O_WRONLY | O_CREAT | O_TRUNC, |
3115 | 0660, errp); | |
8925839f | 3116 | if (!ioc) { |
a7ae8355 SS |
3117 | goto the_end; |
3118 | } | |
6f01f136 | 3119 | qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state"); |
77ef2dc1 | 3120 | f = qemu_file_new_output(QIO_CHANNEL(ioc)); |
032b79f7 | 3121 | object_unref(OBJECT(ioc)); |
a7ae8355 | 3122 | ret = qemu_save_device_state(f); |
96994fd1 | 3123 | if (ret < 0 || qemu_fclose(f) < 0) { |
c6bd8c70 | 3124 | error_setg(errp, QERR_IO_ERROR); |
5d6c599f AP |
3125 | } else { |
3126 | /* libxl calls the QMP command "stop" before calling | |
3127 | * "xen-save-devices-state" and in case of migration failure, libxl | |
3128 | * would call "cont". | |
3129 | * So call bdrv_inactivate_all (release locks) here to let the other | |
3a4452d8 | 3130 | * side of the migration take control of the images. |
5d6c599f AP |
3131 | */ |
3132 | if (live && !saved_vm_running) { | |
3133 | ret = bdrv_inactivate_all(); | |
3134 | if (ret) { | |
3135 | error_setg(errp, "%s: bdrv_inactivate_all() failed (%d)", | |
3136 | __func__, ret); | |
3137 | } | |
3138 | } | |
a7ae8355 SS |
3139 | } |
3140 | ||
3141 | the_end: | |
38ff78d3 | 3142 | if (saved_vm_running) { |
a7ae8355 | 3143 | vm_start(); |
38ff78d3 | 3144 | } |
a7ae8355 SS |
3145 | } |
3146 | ||
88c16567 WC |
3147 | void qmp_xen_load_devices_state(const char *filename, Error **errp) |
3148 | { | |
3149 | QEMUFile *f; | |
3150 | QIOChannelFile *ioc; | |
3151 | int ret; | |
3152 | ||
3153 | /* Guest must be paused before loading the device state; the RAM state | |
3154 | * will already have been loaded by xc | |
3155 | */ | |
3156 | if (runstate_is_running()) { | |
3157 | error_setg(errp, "Cannot update device state while vm is running"); | |
3158 | return; | |
3159 | } | |
3160 | vm_stop(RUN_STATE_RESTORE_VM); | |
3161 | ||
3162 | ioc = qio_channel_file_new_path(filename, O_RDONLY | O_BINARY, 0, errp); | |
3163 | if (!ioc) { | |
3164 | return; | |
3165 | } | |
6f01f136 | 3166 | qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state"); |
77ef2dc1 | 3167 | f = qemu_file_new_input(QIO_CHANNEL(ioc)); |
032b79f7 | 3168 | object_unref(OBJECT(ioc)); |
88c16567 | 3169 | |
88c16567 WC |
3170 | ret = qemu_loadvm_state(f); |
3171 | qemu_fclose(f); | |
3172 | if (ret < 0) { | |
3173 | error_setg(errp, QERR_IO_ERROR); | |
3174 | } | |
3175 | migration_incoming_state_destroy(); | |
3176 | } | |
3177 | ||
f1a9fcdd DB |
3178 | bool load_snapshot(const char *name, const char *vmstate, |
3179 | bool has_devices, strList *devices, Error **errp) | |
a672b469 | 3180 | { |
e26f98e2 | 3181 | BlockDriverState *bs_vm_state; |
2d22b18f | 3182 | QEMUSnapshotInfo sn; |
a672b469 | 3183 | QEMUFile *f; |
751c6a17 | 3184 | int ret; |
79b3c12a | 3185 | AioContext *aio_context; |
b4b076da | 3186 | MigrationIncomingState *mis = migration_incoming_get_current(); |
a672b469 | 3187 | |
f1a9fcdd | 3188 | if (!bdrv_all_can_snapshot(has_devices, devices, errp)) { |
f61fe11a | 3189 | return false; |
849f96e2 | 3190 | } |
f1a9fcdd | 3191 | ret = bdrv_all_has_snapshot(name, has_devices, devices, errp); |
723ccda1 | 3192 | if (ret < 0) { |
f61fe11a | 3193 | return false; |
723ccda1 | 3194 | } |
3d3e9b1f DB |
3195 | if (ret == 0) { |
3196 | error_setg(errp, "Snapshot '%s' does not exist in one or more devices", | |
3197 | name); | |
3198 | return false; | |
3199 | } | |
849f96e2 | 3200 | |
f1a9fcdd | 3201 | bs_vm_state = bdrv_all_find_vmstate_bs(vmstate, has_devices, devices, errp); |
f0aa7a8b | 3202 | if (!bs_vm_state) { |
f61fe11a | 3203 | return false; |
f0aa7a8b | 3204 | } |
79b3c12a | 3205 | aio_context = bdrv_get_aio_context(bs_vm_state); |
f0aa7a8b MDCF |
3206 | |
3207 | /* Don't even try to load empty VM states */ | |
79b3c12a | 3208 | aio_context_acquire(aio_context); |
f0aa7a8b | 3209 | ret = bdrv_snapshot_find(bs_vm_state, &sn, name); |
79b3c12a | 3210 | aio_context_release(aio_context); |
f0aa7a8b | 3211 | if (ret < 0) { |
f61fe11a | 3212 | return false; |
f0aa7a8b | 3213 | } else if (sn.vm_state_size == 0) { |
927d6638 JQ |
3214 | error_setg(errp, "This is a disk-only snapshot. Revert to it " |
3215 | " offline using qemu-img"); | |
f61fe11a | 3216 | return false; |
f0aa7a8b MDCF |
3217 | } |
3218 | ||
f9a9fb65 PD |
3219 | /* |
3220 | * Flush the record/replay queue. Now the VM state is going | |
3221 | * to change. Therefore we don't need to preserve its consistency | |
3222 | */ | |
3223 | replay_flush_events(); | |
3224 | ||
a672b469 | 3225 | /* Flush all IO requests so they don't interfere with the new state. */ |
8649f2f9 | 3226 | bdrv_drain_all_begin(); |
a672b469 | 3227 | |
f1a9fcdd | 3228 | ret = bdrv_all_goto_snapshot(name, has_devices, devices, errp); |
4c1cdbaa | 3229 | if (ret < 0) { |
8649f2f9 | 3230 | goto err_drain; |
a672b469 AL |
3231 | } |
3232 | ||
a672b469 | 3233 | /* restore the VM state */ |
f0aa7a8b | 3234 | f = qemu_fopen_bdrv(bs_vm_state, 0); |
a672b469 | 3235 | if (!f) { |
927d6638 | 3236 | error_setg(errp, "Could not open VM state file"); |
8649f2f9 | 3237 | goto err_drain; |
a672b469 | 3238 | } |
f0aa7a8b | 3239 | |
7966d70f | 3240 | qemu_system_reset(SHUTDOWN_CAUSE_SNAPSHOT_LOAD); |
b4b076da | 3241 | mis->from_src_file = f; |
f0aa7a8b | 3242 | |
b5eea99e LS |
3243 | if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) { |
3244 | ret = -EINVAL; | |
3245 | goto err_drain; | |
3246 | } | |
79b3c12a DL |
3247 | aio_context_acquire(aio_context); |
3248 | ret = qemu_loadvm_state(f); | |
1575829d | 3249 | migration_incoming_state_destroy(); |
79b3c12a DL |
3250 | aio_context_release(aio_context); |
3251 | ||
8649f2f9 SH |
3252 | bdrv_drain_all_end(); |
3253 | ||
a672b469 | 3254 | if (ret < 0) { |
927d6638 | 3255 | error_setg(errp, "Error %d while loading VM state", ret); |
f61fe11a | 3256 | return false; |
a672b469 | 3257 | } |
f0aa7a8b | 3258 | |
f61fe11a | 3259 | return true; |
8649f2f9 SH |
3260 | |
3261 | err_drain: | |
3262 | bdrv_drain_all_end(); | |
f61fe11a | 3263 | return false; |
7b630349 JQ |
3264 | } |
3265 | ||
bef7e9e2 DB |
3266 | bool delete_snapshot(const char *name, bool has_devices, |
3267 | strList *devices, Error **errp) | |
3268 | { | |
3269 | if (!bdrv_all_can_snapshot(has_devices, devices, errp)) { | |
3270 | return false; | |
3271 | } | |
3272 | ||
3273 | if (bdrv_all_delete_snapshot(name, has_devices, devices, errp) < 0) { | |
3274 | return false; | |
3275 | } | |
3276 | ||
3277 | return true; | |
3278 | } | |
3279 | ||
c5705a77 AK |
3280 | void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev) |
3281 | { | |
fa53a0e5 | 3282 | qemu_ram_set_idstr(mr->ram_block, |
c5705a77 | 3283 | memory_region_name(mr), dev); |
b895de50 | 3284 | qemu_ram_set_migratable(mr->ram_block); |
c5705a77 AK |
3285 | } |
3286 | ||
3287 | void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev) | |
3288 | { | |
fa53a0e5 | 3289 | qemu_ram_unset_idstr(mr->ram_block); |
b895de50 | 3290 | qemu_ram_unset_migratable(mr->ram_block); |
c5705a77 AK |
3291 | } |
3292 | ||
3293 | void vmstate_register_ram_global(MemoryRegion *mr) | |
3294 | { | |
3295 | vmstate_register_ram(mr, NULL); | |
3296 | } | |
1bfe5f05 JQ |
3297 | |
3298 | bool vmstate_check_only_migratable(const VMStateDescription *vmsd) | |
3299 | { | |
3300 | /* check needed if --only-migratable is specified */ | |
811f8652 | 3301 | if (!only_migratable) { |
1bfe5f05 JQ |
3302 | return true; |
3303 | } | |
3304 | ||
3305 | return !(vmsd && vmsd->unmigratable); | |
3306 | } | |
0f0d83a4 DB |
3307 | |
3308 | typedef struct SnapshotJob { | |
3309 | Job common; | |
3310 | char *tag; | |
3311 | char *vmstate; | |
3312 | strList *devices; | |
3313 | Coroutine *co; | |
3314 | Error **errp; | |
3315 | bool ret; | |
3316 | } SnapshotJob; | |
3317 | ||
3318 | static void qmp_snapshot_job_free(SnapshotJob *s) | |
3319 | { | |
3320 | g_free(s->tag); | |
3321 | g_free(s->vmstate); | |
3322 | qapi_free_strList(s->devices); | |
3323 | } | |
3324 | ||
3325 | ||
3326 | static void snapshot_load_job_bh(void *opaque) | |
3327 | { | |
3328 | Job *job = opaque; | |
3329 | SnapshotJob *s = container_of(job, SnapshotJob, common); | |
3330 | int orig_vm_running; | |
3331 | ||
3332 | job_progress_set_remaining(&s->common, 1); | |
3333 | ||
3334 | orig_vm_running = runstate_is_running(); | |
3335 | vm_stop(RUN_STATE_RESTORE_VM); | |
3336 | ||
3337 | s->ret = load_snapshot(s->tag, s->vmstate, true, s->devices, s->errp); | |
3338 | if (s->ret && orig_vm_running) { | |
3339 | vm_start(); | |
3340 | } | |
3341 | ||
3342 | job_progress_update(&s->common, 1); | |
3343 | ||
3344 | qmp_snapshot_job_free(s); | |
3345 | aio_co_wake(s->co); | |
3346 | } | |
3347 | ||
3348 | static void snapshot_save_job_bh(void *opaque) | |
3349 | { | |
3350 | Job *job = opaque; | |
3351 | SnapshotJob *s = container_of(job, SnapshotJob, common); | |
3352 | ||
3353 | job_progress_set_remaining(&s->common, 1); | |
3354 | s->ret = save_snapshot(s->tag, false, s->vmstate, | |
3355 | true, s->devices, s->errp); | |
3356 | job_progress_update(&s->common, 1); | |
3357 | ||
3358 | qmp_snapshot_job_free(s); | |
3359 | aio_co_wake(s->co); | |
3360 | } | |
3361 | ||
3362 | static void snapshot_delete_job_bh(void *opaque) | |
3363 | { | |
3364 | Job *job = opaque; | |
3365 | SnapshotJob *s = container_of(job, SnapshotJob, common); | |
3366 | ||
3367 | job_progress_set_remaining(&s->common, 1); | |
3368 | s->ret = delete_snapshot(s->tag, true, s->devices, s->errp); | |
3369 | job_progress_update(&s->common, 1); | |
3370 | ||
3371 | qmp_snapshot_job_free(s); | |
3372 | aio_co_wake(s->co); | |
3373 | } | |
3374 | ||
3375 | static int coroutine_fn snapshot_save_job_run(Job *job, Error **errp) | |
3376 | { | |
3377 | SnapshotJob *s = container_of(job, SnapshotJob, common); | |
3378 | s->errp = errp; | |
3379 | s->co = qemu_coroutine_self(); | |
3380 | aio_bh_schedule_oneshot(qemu_get_aio_context(), | |
3381 | snapshot_save_job_bh, job); | |
3382 | qemu_coroutine_yield(); | |
3383 | return s->ret ? 0 : -1; | |
3384 | } | |
3385 | ||
3386 | static int coroutine_fn snapshot_load_job_run(Job *job, Error **errp) | |
3387 | { | |
3388 | SnapshotJob *s = container_of(job, SnapshotJob, common); | |
3389 | s->errp = errp; | |
3390 | s->co = qemu_coroutine_self(); | |
3391 | aio_bh_schedule_oneshot(qemu_get_aio_context(), | |
3392 | snapshot_load_job_bh, job); | |
3393 | qemu_coroutine_yield(); | |
3394 | return s->ret ? 0 : -1; | |
3395 | } | |
3396 | ||
3397 | static int coroutine_fn snapshot_delete_job_run(Job *job, Error **errp) | |
3398 | { | |
3399 | SnapshotJob *s = container_of(job, SnapshotJob, common); | |
3400 | s->errp = errp; | |
3401 | s->co = qemu_coroutine_self(); | |
3402 | aio_bh_schedule_oneshot(qemu_get_aio_context(), | |
3403 | snapshot_delete_job_bh, job); | |
3404 | qemu_coroutine_yield(); | |
3405 | return s->ret ? 0 : -1; | |
3406 | } | |
3407 | ||
3408 | ||
3409 | static const JobDriver snapshot_load_job_driver = { | |
3410 | .instance_size = sizeof(SnapshotJob), | |
3411 | .job_type = JOB_TYPE_SNAPSHOT_LOAD, | |
3412 | .run = snapshot_load_job_run, | |
3413 | }; | |
3414 | ||
3415 | static const JobDriver snapshot_save_job_driver = { | |
3416 | .instance_size = sizeof(SnapshotJob), | |
3417 | .job_type = JOB_TYPE_SNAPSHOT_SAVE, | |
3418 | .run = snapshot_save_job_run, | |
3419 | }; | |
3420 | ||
3421 | static const JobDriver snapshot_delete_job_driver = { | |
3422 | .instance_size = sizeof(SnapshotJob), | |
3423 | .job_type = JOB_TYPE_SNAPSHOT_DELETE, | |
3424 | .run = snapshot_delete_job_run, | |
3425 | }; | |
3426 | ||
3427 | ||
3428 | void qmp_snapshot_save(const char *job_id, | |
3429 | const char *tag, | |
3430 | const char *vmstate, | |
3431 | strList *devices, | |
3432 | Error **errp) | |
3433 | { | |
3434 | SnapshotJob *s; | |
3435 | ||
3436 | s = job_create(job_id, &snapshot_save_job_driver, NULL, | |
3437 | qemu_get_aio_context(), JOB_MANUAL_DISMISS, | |
3438 | NULL, NULL, errp); | |
3439 | if (!s) { | |
3440 | return; | |
3441 | } | |
3442 | ||
3443 | s->tag = g_strdup(tag); | |
3444 | s->vmstate = g_strdup(vmstate); | |
3445 | s->devices = QAPI_CLONE(strList, devices); | |
3446 | ||
3447 | job_start(&s->common); | |
3448 | } | |
3449 | ||
3450 | void qmp_snapshot_load(const char *job_id, | |
3451 | const char *tag, | |
3452 | const char *vmstate, | |
3453 | strList *devices, | |
3454 | Error **errp) | |
3455 | { | |
3456 | SnapshotJob *s; | |
3457 | ||
3458 | s = job_create(job_id, &snapshot_load_job_driver, NULL, | |
3459 | qemu_get_aio_context(), JOB_MANUAL_DISMISS, | |
3460 | NULL, NULL, errp); | |
3461 | if (!s) { | |
3462 | return; | |
3463 | } | |
3464 | ||
3465 | s->tag = g_strdup(tag); | |
3466 | s->vmstate = g_strdup(vmstate); | |
3467 | s->devices = QAPI_CLONE(strList, devices); | |
3468 | ||
3469 | job_start(&s->common); | |
3470 | } | |
3471 | ||
3472 | void qmp_snapshot_delete(const char *job_id, | |
3473 | const char *tag, | |
3474 | strList *devices, | |
3475 | Error **errp) | |
3476 | { | |
3477 | SnapshotJob *s; | |
3478 | ||
3479 | s = job_create(job_id, &snapshot_delete_job_driver, NULL, | |
3480 | qemu_get_aio_context(), JOB_MANUAL_DISMISS, | |
3481 | NULL, NULL, errp); | |
3482 | if (!s) { | |
3483 | return; | |
3484 | } | |
3485 | ||
3486 | s->tag = g_strdup(tag); | |
3487 | s->devices = QAPI_CLONE(strList, devices); | |
3488 | ||
3489 | job_start(&s->common); | |
3490 | } |