]>
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" |
88c16567 | 31 | #include "hw/xen/xen.h" |
1422e32d | 32 | #include "net/net.h" |
6666c96a | 33 | #include "migration.h" |
5e22479a | 34 | #include "migration/snapshot.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" |
7b1e1a22 | 39 | #include "ram.h" |
40014d81 | 40 | #include "qemu-file-channel.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 MA |
45 | #include "qapi/qapi-commands-migration.h" |
46 | #include "qapi/qapi-commands-misc.h" | |
cc7a8ea7 | 47 | #include "qapi/qmp/qerror.h" |
d49b6836 | 48 | #include "qemu/error-report.h" |
9c17d615 | 49 | #include "sysemu/cpus.h" |
022c62cb | 50 | #include "exec/memory.h" |
51180423 | 51 | #include "exec/target_page.h" |
517a13c9 | 52 | #include "trace.h" |
28085f7b | 53 | #include "qemu/iov.h" |
db725815 | 54 | #include "qemu/main-loop.h" |
de08c606 | 55 | #include "block/snapshot.h" |
f348b6d1 | 56 | #include "qemu/cutils.h" |
61b67d47 | 57 | #include "io/channel-buffer.h" |
8925839f | 58 | #include "io/channel-file.h" |
377b21cc | 59 | #include "sysemu/replay.h" |
54d31236 | 60 | #include "sysemu/runstate.h" |
46517dd4 | 61 | #include "sysemu/sysemu.h" |
83ee768d | 62 | #include "qjson.h" |
aad555c2 | 63 | #include "migration/colo.h" |
6cafc8e4 | 64 | #include "qemu/bitmap.h" |
7659505c | 65 | #include "net/announce.h" |
18995b98 | 66 | |
093e3c42 DDAG |
67 | const unsigned int postcopy_ram_discard_version = 0; |
68 | ||
20a519a0 JQ |
69 | /* Subcommands for QEMU_VM_COMMAND */ |
70 | enum qemu_vm_cmd { | |
71 | MIG_CMD_INVALID = 0, /* Must be 0 */ | |
72 | MIG_CMD_OPEN_RETURN_PATH, /* Tell the dest to open the Return path */ | |
73 | MIG_CMD_PING, /* Request a PONG on the RP */ | |
74 | ||
75 | MIG_CMD_POSTCOPY_ADVISE, /* Prior to any page transfers, just | |
76 | warn we might want to do PC */ | |
77 | MIG_CMD_POSTCOPY_LISTEN, /* Start listening for incoming | |
78 | pages as it's running. */ | |
79 | MIG_CMD_POSTCOPY_RUN, /* Start execution */ | |
80 | ||
81 | MIG_CMD_POSTCOPY_RAM_DISCARD, /* A list of pages to discard that | |
82 | were previously sent during | |
83 | precopy but are dirty. */ | |
84 | MIG_CMD_PACKAGED, /* Send a wrapped stream within this stream */ | |
aad555c2 | 85 | MIG_CMD_ENABLE_COLO, /* Enable COLO */ |
858b6d62 | 86 | MIG_CMD_POSTCOPY_RESUME, /* resume postcopy on dest */ |
f25d4225 | 87 | MIG_CMD_RECV_BITMAP, /* Request for recved bitmap on dst */ |
20a519a0 JQ |
88 | MIG_CMD_MAX |
89 | }; | |
90 | ||
ee555cdf | 91 | #define MAX_VM_CMD_PACKAGED_SIZE UINT32_MAX |
c76ca188 DDAG |
92 | static struct mig_cmd_args { |
93 | ssize_t len; /* -1 = variable */ | |
94 | const char *name; | |
95 | } mig_cmd_args[] = { | |
96 | [MIG_CMD_INVALID] = { .len = -1, .name = "INVALID" }, | |
2e37701e DDAG |
97 | [MIG_CMD_OPEN_RETURN_PATH] = { .len = 0, .name = "OPEN_RETURN_PATH" }, |
98 | [MIG_CMD_PING] = { .len = sizeof(uint32_t), .name = "PING" }, | |
58110f0a | 99 | [MIG_CMD_POSTCOPY_ADVISE] = { .len = -1, .name = "POSTCOPY_ADVISE" }, |
093e3c42 DDAG |
100 | [MIG_CMD_POSTCOPY_LISTEN] = { .len = 0, .name = "POSTCOPY_LISTEN" }, |
101 | [MIG_CMD_POSTCOPY_RUN] = { .len = 0, .name = "POSTCOPY_RUN" }, | |
102 | [MIG_CMD_POSTCOPY_RAM_DISCARD] = { | |
103 | .len = -1, .name = "POSTCOPY_RAM_DISCARD" }, | |
3f5875ec | 104 | [MIG_CMD_POSTCOPY_RESUME] = { .len = 0, .name = "POSTCOPY_RESUME" }, |
11cf1d98 | 105 | [MIG_CMD_PACKAGED] = { .len = 4, .name = "PACKAGED" }, |
f25d4225 | 106 | [MIG_CMD_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" }, |
c76ca188 DDAG |
107 | [MIG_CMD_MAX] = { .len = -1, .name = "MAX" }, |
108 | }; | |
109 | ||
58110f0a VSO |
110 | /* Note for MIG_CMD_POSTCOPY_ADVISE: |
111 | * The format of arguments is depending on postcopy mode: | |
112 | * - postcopy RAM only | |
113 | * uint64_t host page size | |
114 | * uint64_t taget page size | |
115 | * | |
116 | * - postcopy RAM and postcopy dirty bitmaps | |
117 | * format is the same as for postcopy RAM only | |
118 | * | |
119 | * - postcopy dirty bitmaps only | |
120 | * Nothing. Command length field is 0. | |
121 | * | |
122 | * Be careful: adding a new postcopy entity with some other parameters should | |
123 | * not break format self-description ability. Good way is to introduce some | |
124 | * generic extendable format with an exception for two old entities. | |
125 | */ | |
126 | ||
a672b469 AL |
127 | /***********************************************************/ |
128 | /* savevm/loadvm support */ | |
129 | ||
05fcc848 | 130 | static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt, |
3d661c8a | 131 | int64_t pos, Error **errp) |
05fcc848 KW |
132 | { |
133 | int ret; | |
134 | QEMUIOVector qiov; | |
135 | ||
136 | qemu_iovec_init_external(&qiov, iov, iovcnt); | |
137 | ret = bdrv_writev_vmstate(opaque, &qiov, pos); | |
138 | if (ret < 0) { | |
139 | return ret; | |
140 | } | |
141 | ||
142 | return qiov.size; | |
143 | } | |
144 | ||
a202a4c0 | 145 | static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, |
3d661c8a | 146 | size_t size, Error **errp) |
a672b469 | 147 | { |
45566e9c | 148 | return bdrv_load_vmstate(opaque, buf, pos, size); |
a672b469 AL |
149 | } |
150 | ||
3d661c8a | 151 | static int bdrv_fclose(void *opaque, Error **errp) |
a672b469 | 152 | { |
ad492c92 | 153 | return bdrv_flush(opaque); |
a672b469 AL |
154 | } |
155 | ||
9229bf3c PB |
156 | static const QEMUFileOps bdrv_read_ops = { |
157 | .get_buffer = block_get_buffer, | |
158 | .close = bdrv_fclose | |
159 | }; | |
160 | ||
161 | static const QEMUFileOps bdrv_write_ops = { | |
05fcc848 KW |
162 | .writev_buffer = block_writev_buffer, |
163 | .close = bdrv_fclose | |
9229bf3c PB |
164 | }; |
165 | ||
45566e9c | 166 | static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable) |
a672b469 | 167 | { |
38ff78d3 | 168 | if (is_writable) { |
9229bf3c | 169 | return qemu_fopen_ops(bs, &bdrv_write_ops); |
38ff78d3 | 170 | } |
9229bf3c | 171 | return qemu_fopen_ops(bs, &bdrv_read_ops); |
a672b469 AL |
172 | } |
173 | ||
2ff68d07 | 174 | |
bb1a6d8c EH |
175 | /* QEMUFile timer support. |
176 | * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c | |
177 | */ | |
2ff68d07 | 178 | |
40daca54 | 179 | void timer_put(QEMUFile *f, QEMUTimer *ts) |
2ff68d07 PB |
180 | { |
181 | uint64_t expire_time; | |
182 | ||
e93379b0 | 183 | expire_time = timer_expire_time_ns(ts); |
2ff68d07 PB |
184 | qemu_put_be64(f, expire_time); |
185 | } | |
186 | ||
40daca54 | 187 | void timer_get(QEMUFile *f, QEMUTimer *ts) |
2ff68d07 PB |
188 | { |
189 | uint64_t expire_time; | |
190 | ||
191 | expire_time = qemu_get_be64(f); | |
192 | if (expire_time != -1) { | |
bc72ad67 | 193 | timer_mod_ns(ts, expire_time); |
2ff68d07 | 194 | } else { |
bc72ad67 | 195 | timer_del(ts); |
2ff68d07 PB |
196 | } |
197 | } | |
198 | ||
199 | ||
bb1a6d8c EH |
200 | /* VMState timer support. |
201 | * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c | |
202 | */ | |
dde0463b | 203 | |
03fee66f MAL |
204 | static int get_timer(QEMUFile *f, void *pv, size_t size, |
205 | const VMStateField *field) | |
dde0463b JQ |
206 | { |
207 | QEMUTimer *v = pv; | |
40daca54 | 208 | timer_get(f, v); |
dde0463b JQ |
209 | return 0; |
210 | } | |
211 | ||
03fee66f MAL |
212 | static int put_timer(QEMUFile *f, void *pv, size_t size, |
213 | const VMStateField *field, QJSON *vmdesc) | |
dde0463b | 214 | { |
84e2e3eb | 215 | QEMUTimer *v = pv; |
40daca54 | 216 | timer_put(f, v); |
2c21ee76 JD |
217 | |
218 | return 0; | |
dde0463b JQ |
219 | } |
220 | ||
221 | const VMStateInfo vmstate_info_timer = { | |
222 | .name = "timer", | |
223 | .get = get_timer, | |
224 | .put = put_timer, | |
225 | }; | |
226 | ||
08e99e29 | 227 | |
7685ee6a AW |
228 | typedef struct CompatEntry { |
229 | char idstr[256]; | |
230 | int instance_id; | |
231 | } CompatEntry; | |
232 | ||
a672b469 | 233 | typedef struct SaveStateEntry { |
72cf2d4f | 234 | QTAILQ_ENTRY(SaveStateEntry) entry; |
a672b469 AL |
235 | char idstr[256]; |
236 | int instance_id; | |
4d2ffa08 | 237 | int alias_id; |
a672b469 | 238 | int version_id; |
0f42f657 JQ |
239 | /* version id read from the stream */ |
240 | int load_version_id; | |
a672b469 | 241 | int section_id; |
0f42f657 JQ |
242 | /* section id read from the stream */ |
243 | int load_section_id; | |
de22ded0 | 244 | const SaveVMHandlers *ops; |
9ed7d6ae | 245 | const VMStateDescription *vmsd; |
a672b469 | 246 | void *opaque; |
7685ee6a | 247 | CompatEntry *compat; |
a7ae8355 | 248 | int is_ram; |
a672b469 AL |
249 | } SaveStateEntry; |
250 | ||
0163a2e0 JQ |
251 | typedef struct SaveState { |
252 | QTAILQ_HEAD(, SaveStateEntry) handlers; | |
253 | int global_section_id; | |
61964c23 JQ |
254 | uint32_t len; |
255 | const char *name; | |
59811a32 | 256 | uint32_t target_page_bits; |
6cafc8e4 YK |
257 | uint32_t caps_count; |
258 | MigrationCapability *capabilities; | |
b9d68df6 | 259 | QemuUUID uuid; |
0163a2e0 JQ |
260 | } SaveState; |
261 | ||
262 | static SaveState savevm_state = { | |
263 | .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers), | |
264 | .global_section_id = 0, | |
61964c23 JQ |
265 | }; |
266 | ||
6cafc8e4 YK |
267 | static bool should_validate_capability(int capability) |
268 | { | |
269 | assert(capability >= 0 && capability < MIGRATION_CAPABILITY__MAX); | |
270 | /* Validate only new capabilities to keep compatibility. */ | |
271 | switch (capability) { | |
272 | case MIGRATION_CAPABILITY_X_IGNORE_SHARED: | |
273 | return true; | |
274 | default: | |
275 | return false; | |
276 | } | |
277 | } | |
278 | ||
279 | static uint32_t get_validatable_capabilities_count(void) | |
280 | { | |
281 | MigrationState *s = migrate_get_current(); | |
282 | uint32_t result = 0; | |
283 | int i; | |
284 | for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { | |
285 | if (should_validate_capability(i) && s->enabled_capabilities[i]) { | |
286 | result++; | |
287 | } | |
288 | } | |
289 | return result; | |
290 | } | |
291 | ||
44b1ff31 | 292 | static int configuration_pre_save(void *opaque) |
61964c23 JQ |
293 | { |
294 | SaveState *state = opaque; | |
295 | const char *current_name = MACHINE_GET_CLASS(current_machine)->name; | |
6cafc8e4 YK |
296 | MigrationState *s = migrate_get_current(); |
297 | int i, j; | |
61964c23 JQ |
298 | |
299 | state->len = strlen(current_name); | |
300 | state->name = current_name; | |
46d702b1 | 301 | state->target_page_bits = qemu_target_page_bits(); |
44b1ff31 | 302 | |
6cafc8e4 YK |
303 | state->caps_count = get_validatable_capabilities_count(); |
304 | state->capabilities = g_renew(MigrationCapability, state->capabilities, | |
305 | state->caps_count); | |
306 | for (i = j = 0; i < MIGRATION_CAPABILITY__MAX; i++) { | |
307 | if (should_validate_capability(i) && s->enabled_capabilities[i]) { | |
308 | state->capabilities[j++] = i; | |
309 | } | |
310 | } | |
b9d68df6 | 311 | state->uuid = qemu_uuid; |
6cafc8e4 | 312 | |
44b1ff31 | 313 | return 0; |
59811a32 PM |
314 | } |
315 | ||
316 | static int configuration_pre_load(void *opaque) | |
317 | { | |
318 | SaveState *state = opaque; | |
319 | ||
320 | /* If there is no target-page-bits subsection it means the source | |
321 | * predates the variable-target-page-bits support and is using the | |
322 | * minimum possible value for this CPU. | |
323 | */ | |
46d702b1 | 324 | state->target_page_bits = qemu_target_page_bits_min(); |
59811a32 | 325 | return 0; |
61964c23 JQ |
326 | } |
327 | ||
6cafc8e4 YK |
328 | static bool configuration_validate_capabilities(SaveState *state) |
329 | { | |
330 | bool ret = true; | |
331 | MigrationState *s = migrate_get_current(); | |
332 | unsigned long *source_caps_bm; | |
333 | int i; | |
334 | ||
335 | source_caps_bm = bitmap_new(MIGRATION_CAPABILITY__MAX); | |
336 | for (i = 0; i < state->caps_count; i++) { | |
337 | MigrationCapability capability = state->capabilities[i]; | |
338 | set_bit(capability, source_caps_bm); | |
339 | } | |
340 | ||
341 | for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { | |
342 | bool source_state, target_state; | |
343 | if (!should_validate_capability(i)) { | |
344 | continue; | |
345 | } | |
346 | source_state = test_bit(i, source_caps_bm); | |
347 | target_state = s->enabled_capabilities[i]; | |
348 | if (source_state != target_state) { | |
349 | error_report("Capability %s is %s, but received capability is %s", | |
350 | MigrationCapability_str(i), | |
351 | target_state ? "on" : "off", | |
352 | source_state ? "on" : "off"); | |
353 | ret = false; | |
354 | /* Don't break here to report all failed capabilities */ | |
355 | } | |
356 | } | |
357 | ||
358 | g_free(source_caps_bm); | |
359 | return ret; | |
360 | } | |
361 | ||
61964c23 JQ |
362 | static int configuration_post_load(void *opaque, int version_id) |
363 | { | |
364 | SaveState *state = opaque; | |
365 | const char *current_name = MACHINE_GET_CLASS(current_machine)->name; | |
366 | ||
367 | if (strncmp(state->name, current_name, state->len) != 0) { | |
15d61692 GK |
368 | error_report("Machine type received is '%.*s' and local is '%s'", |
369 | (int) state->len, state->name, current_name); | |
61964c23 JQ |
370 | return -EINVAL; |
371 | } | |
59811a32 | 372 | |
46d702b1 | 373 | if (state->target_page_bits != qemu_target_page_bits()) { |
59811a32 | 374 | error_report("Received TARGET_PAGE_BITS is %d but local is %d", |
46d702b1 | 375 | state->target_page_bits, qemu_target_page_bits()); |
59811a32 PM |
376 | return -EINVAL; |
377 | } | |
378 | ||
6cafc8e4 YK |
379 | if (!configuration_validate_capabilities(state)) { |
380 | return -EINVAL; | |
381 | } | |
382 | ||
61964c23 JQ |
383 | return 0; |
384 | } | |
385 | ||
6cafc8e4 YK |
386 | static int get_capability(QEMUFile *f, void *pv, size_t size, |
387 | const VMStateField *field) | |
388 | { | |
389 | MigrationCapability *capability = pv; | |
390 | char capability_str[UINT8_MAX + 1]; | |
391 | uint8_t len; | |
392 | int i; | |
393 | ||
394 | len = qemu_get_byte(f); | |
395 | qemu_get_buffer(f, (uint8_t *)capability_str, len); | |
396 | capability_str[len] = '\0'; | |
397 | for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { | |
398 | if (!strcmp(MigrationCapability_str(i), capability_str)) { | |
399 | *capability = i; | |
400 | return 0; | |
401 | } | |
402 | } | |
403 | error_report("Received unknown capability %s", capability_str); | |
404 | return -EINVAL; | |
405 | } | |
406 | ||
407 | static int put_capability(QEMUFile *f, void *pv, size_t size, | |
408 | const VMStateField *field, QJSON *vmdesc) | |
409 | { | |
410 | MigrationCapability *capability = pv; | |
411 | const char *capability_str = MigrationCapability_str(*capability); | |
412 | size_t len = strlen(capability_str); | |
413 | assert(len <= UINT8_MAX); | |
414 | ||
415 | qemu_put_byte(f, len); | |
416 | qemu_put_buffer(f, (uint8_t *)capability_str, len); | |
417 | return 0; | |
418 | } | |
419 | ||
420 | static const VMStateInfo vmstate_info_capability = { | |
421 | .name = "capability", | |
422 | .get = get_capability, | |
423 | .put = put_capability, | |
424 | }; | |
425 | ||
59811a32 PM |
426 | /* The target-page-bits subsection is present only if the |
427 | * target page size is not the same as the default (ie the | |
428 | * minimum page size for a variable-page-size guest CPU). | |
429 | * If it is present then it contains the actual target page | |
430 | * bits for the machine, and migration will fail if the | |
431 | * two ends don't agree about it. | |
432 | */ | |
433 | static bool vmstate_target_page_bits_needed(void *opaque) | |
434 | { | |
46d702b1 JQ |
435 | return qemu_target_page_bits() |
436 | > qemu_target_page_bits_min(); | |
59811a32 PM |
437 | } |
438 | ||
439 | static const VMStateDescription vmstate_target_page_bits = { | |
440 | .name = "configuration/target-page-bits", | |
441 | .version_id = 1, | |
442 | .minimum_version_id = 1, | |
443 | .needed = vmstate_target_page_bits_needed, | |
444 | .fields = (VMStateField[]) { | |
445 | VMSTATE_UINT32(target_page_bits, SaveState), | |
446 | VMSTATE_END_OF_LIST() | |
447 | } | |
448 | }; | |
449 | ||
6cafc8e4 YK |
450 | static bool vmstate_capabilites_needed(void *opaque) |
451 | { | |
452 | return get_validatable_capabilities_count() > 0; | |
453 | } | |
454 | ||
455 | static const VMStateDescription vmstate_capabilites = { | |
456 | .name = "configuration/capabilities", | |
457 | .version_id = 1, | |
458 | .minimum_version_id = 1, | |
459 | .needed = vmstate_capabilites_needed, | |
460 | .fields = (VMStateField[]) { | |
461 | VMSTATE_UINT32_V(caps_count, SaveState, 1), | |
462 | VMSTATE_VARRAY_UINT32_ALLOC(capabilities, SaveState, caps_count, 1, | |
463 | vmstate_info_capability, | |
464 | MigrationCapability), | |
465 | VMSTATE_END_OF_LIST() | |
466 | } | |
467 | }; | |
468 | ||
b9d68df6 YK |
469 | static bool vmstate_uuid_needed(void *opaque) |
470 | { | |
471 | return qemu_uuid_set && migrate_validate_uuid(); | |
472 | } | |
473 | ||
474 | static int vmstate_uuid_post_load(void *opaque, int version_id) | |
475 | { | |
476 | SaveState *state = opaque; | |
477 | char uuid_src[UUID_FMT_LEN + 1]; | |
478 | char uuid_dst[UUID_FMT_LEN + 1]; | |
479 | ||
480 | if (!qemu_uuid_set) { | |
481 | /* | |
482 | * It's warning because user might not know UUID in some cases, | |
483 | * e.g. load an old snapshot | |
484 | */ | |
485 | qemu_uuid_unparse(&state->uuid, uuid_src); | |
486 | warn_report("UUID is received %s, but local uuid isn't set", | |
487 | uuid_src); | |
488 | return 0; | |
489 | } | |
490 | if (!qemu_uuid_is_equal(&state->uuid, &qemu_uuid)) { | |
491 | qemu_uuid_unparse(&state->uuid, uuid_src); | |
492 | qemu_uuid_unparse(&qemu_uuid, uuid_dst); | |
493 | error_report("UUID received is %s and local is %s", uuid_src, uuid_dst); | |
494 | return -EINVAL; | |
495 | } | |
496 | return 0; | |
497 | } | |
498 | ||
499 | static const VMStateDescription vmstate_uuid = { | |
500 | .name = "configuration/uuid", | |
501 | .version_id = 1, | |
502 | .minimum_version_id = 1, | |
503 | .needed = vmstate_uuid_needed, | |
504 | .post_load = vmstate_uuid_post_load, | |
505 | .fields = (VMStateField[]) { | |
506 | VMSTATE_UINT8_ARRAY_V(uuid.data, SaveState, sizeof(QemuUUID), 1), | |
507 | VMSTATE_END_OF_LIST() | |
508 | } | |
509 | }; | |
510 | ||
61964c23 JQ |
511 | static const VMStateDescription vmstate_configuration = { |
512 | .name = "configuration", | |
513 | .version_id = 1, | |
59811a32 | 514 | .pre_load = configuration_pre_load, |
61964c23 JQ |
515 | .post_load = configuration_post_load, |
516 | .pre_save = configuration_pre_save, | |
517 | .fields = (VMStateField[]) { | |
518 | VMSTATE_UINT32(len, SaveState), | |
59046ec2 | 519 | VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, len), |
61964c23 JQ |
520 | VMSTATE_END_OF_LIST() |
521 | }, | |
59811a32 PM |
522 | .subsections = (const VMStateDescription*[]) { |
523 | &vmstate_target_page_bits, | |
6cafc8e4 | 524 | &vmstate_capabilites, |
b9d68df6 | 525 | &vmstate_uuid, |
59811a32 PM |
526 | NULL |
527 | } | |
0163a2e0 | 528 | }; |
a672b469 | 529 | |
abfd9ce3 AS |
530 | static void dump_vmstate_vmsd(FILE *out_file, |
531 | const VMStateDescription *vmsd, int indent, | |
532 | bool is_subsection); | |
533 | ||
534 | static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field, | |
535 | int indent) | |
536 | { | |
537 | fprintf(out_file, "%*s{\n", indent, ""); | |
538 | indent += 2; | |
539 | fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name); | |
540 | fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "", | |
541 | field->version_id); | |
542 | fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "", | |
543 | field->field_exists ? "true" : "false"); | |
544 | fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size); | |
545 | if (field->vmsd != NULL) { | |
546 | fprintf(out_file, ",\n"); | |
547 | dump_vmstate_vmsd(out_file, field->vmsd, indent, false); | |
548 | } | |
549 | fprintf(out_file, "\n%*s}", indent - 2, ""); | |
550 | } | |
551 | ||
552 | static void dump_vmstate_vmss(FILE *out_file, | |
5cd8cada | 553 | const VMStateDescription **subsection, |
abfd9ce3 AS |
554 | int indent) |
555 | { | |
5cd8cada JQ |
556 | if (*subsection != NULL) { |
557 | dump_vmstate_vmsd(out_file, *subsection, indent, true); | |
abfd9ce3 AS |
558 | } |
559 | } | |
560 | ||
561 | static void dump_vmstate_vmsd(FILE *out_file, | |
562 | const VMStateDescription *vmsd, int indent, | |
563 | bool is_subsection) | |
564 | { | |
565 | if (is_subsection) { | |
566 | fprintf(out_file, "%*s{\n", indent, ""); | |
567 | } else { | |
568 | fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description"); | |
569 | } | |
570 | indent += 2; | |
571 | fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name); | |
572 | fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "", | |
573 | vmsd->version_id); | |
574 | fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "", | |
575 | vmsd->minimum_version_id); | |
576 | if (vmsd->fields != NULL) { | |
577 | const VMStateField *field = vmsd->fields; | |
578 | bool first; | |
579 | ||
580 | fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, ""); | |
581 | first = true; | |
582 | while (field->name != NULL) { | |
583 | if (field->flags & VMS_MUST_EXIST) { | |
584 | /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */ | |
585 | field++; | |
586 | continue; | |
587 | } | |
588 | if (!first) { | |
589 | fprintf(out_file, ",\n"); | |
590 | } | |
591 | dump_vmstate_vmsf(out_file, field, indent + 2); | |
592 | field++; | |
593 | first = false; | |
594 | } | |
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); | |
666 | } | |
667 | ||
8718e999 JQ |
668 | static int calculate_new_instance_id(const char *idstr) |
669 | { | |
670 | SaveStateEntry *se; | |
671 | int instance_id = 0; | |
672 | ||
0163a2e0 | 673 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
8718e999 JQ |
674 | if (strcmp(idstr, se->idstr) == 0 |
675 | && instance_id <= se->instance_id) { | |
676 | instance_id = se->instance_id + 1; | |
677 | } | |
678 | } | |
679 | return instance_id; | |
680 | } | |
681 | ||
7685ee6a AW |
682 | static int calculate_compat_instance_id(const char *idstr) |
683 | { | |
684 | SaveStateEntry *se; | |
685 | int instance_id = 0; | |
686 | ||
0163a2e0 | 687 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
38ff78d3 | 688 | if (!se->compat) { |
7685ee6a | 689 | continue; |
38ff78d3 | 690 | } |
7685ee6a AW |
691 | |
692 | if (strcmp(idstr, se->compat->idstr) == 0 | |
693 | && instance_id <= se->compat->instance_id) { | |
694 | instance_id = se->compat->instance_id + 1; | |
695 | } | |
696 | } | |
697 | return instance_id; | |
698 | } | |
699 | ||
f37bc036 PX |
700 | static inline MigrationPriority save_state_priority(SaveStateEntry *se) |
701 | { | |
702 | if (se->vmsd) { | |
703 | return se->vmsd->priority; | |
704 | } | |
705 | return MIG_PRI_DEFAULT; | |
706 | } | |
707 | ||
708 | static void savevm_state_handler_insert(SaveStateEntry *nse) | |
709 | { | |
710 | MigrationPriority priority = save_state_priority(nse); | |
711 | SaveStateEntry *se; | |
712 | ||
713 | assert(priority <= MIG_PRI_MAX); | |
714 | ||
715 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { | |
716 | if (save_state_priority(se) < priority) { | |
717 | break; | |
718 | } | |
719 | } | |
720 | ||
721 | if (se) { | |
722 | QTAILQ_INSERT_BEFORE(se, nse, entry); | |
723 | } else { | |
724 | QTAILQ_INSERT_TAIL(&savevm_state.handlers, nse, entry); | |
725 | } | |
726 | } | |
727 | ||
a672b469 AL |
728 | /* TODO: Individual devices generally have very little idea about the rest |
729 | of the system, so instance_id should be removed/replaced. | |
730 | Meanwhile pass -1 as instance_id if you do not already have a clearly | |
731 | distinguishing id for all instances of your device class. */ | |
ce62df53 | 732 | int register_savevm_live(const char *idstr, |
a672b469 AL |
733 | int instance_id, |
734 | int version_id, | |
de22ded0 | 735 | const SaveVMHandlers *ops, |
a672b469 AL |
736 | void *opaque) |
737 | { | |
8718e999 | 738 | SaveStateEntry *se; |
a672b469 | 739 | |
97f3ad35 | 740 | se = g_new0(SaveStateEntry, 1); |
a672b469 | 741 | se->version_id = version_id; |
0163a2e0 | 742 | se->section_id = savevm_state.global_section_id++; |
7908c78d | 743 | se->ops = ops; |
a672b469 | 744 | se->opaque = opaque; |
9ed7d6ae | 745 | se->vmsd = NULL; |
a7ae8355 | 746 | /* if this is a live_savem then set is_ram */ |
9907e842 | 747 | if (ops->save_setup != NULL) { |
a7ae8355 SS |
748 | se->is_ram = 1; |
749 | } | |
a672b469 | 750 | |
7685ee6a AW |
751 | pstrcat(se->idstr, sizeof(se->idstr), idstr); |
752 | ||
8718e999 | 753 | if (instance_id == -1) { |
7685ee6a | 754 | se->instance_id = calculate_new_instance_id(se->idstr); |
8718e999 JQ |
755 | } else { |
756 | se->instance_id = instance_id; | |
a672b469 | 757 | } |
7685ee6a | 758 | assert(!se->compat || se->instance_id == 0); |
f37bc036 | 759 | savevm_state_handler_insert(se); |
a672b469 AL |
760 | return 0; |
761 | } | |
762 | ||
0be71e32 | 763 | void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque) |
41bd13af | 764 | { |
8718e999 | 765 | SaveStateEntry *se, *new_se; |
7685ee6a AW |
766 | char id[256] = ""; |
767 | ||
09e5ab63 AL |
768 | if (dev) { |
769 | char *path = qdev_get_dev_path(dev); | |
7685ee6a AW |
770 | if (path) { |
771 | pstrcpy(id, sizeof(id), path); | |
772 | pstrcat(id, sizeof(id), "/"); | |
7267c094 | 773 | g_free(path); |
7685ee6a AW |
774 | } |
775 | } | |
776 | pstrcat(id, sizeof(id), idstr); | |
41bd13af | 777 | |
0163a2e0 | 778 | QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) { |
7685ee6a | 779 | if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) { |
0163a2e0 | 780 | QTAILQ_REMOVE(&savevm_state.handlers, se, entry); |
ef1e1e07 | 781 | g_free(se->compat); |
7267c094 | 782 | g_free(se); |
41bd13af | 783 | } |
41bd13af AL |
784 | } |
785 | } | |
786 | ||
0be71e32 | 787 | int vmstate_register_with_alias_id(DeviceState *dev, int instance_id, |
4d2ffa08 JK |
788 | const VMStateDescription *vmsd, |
789 | void *opaque, int alias_id, | |
bc5c4f21 DDAG |
790 | int required_for_version, |
791 | Error **errp) | |
9ed7d6ae | 792 | { |
8718e999 | 793 | SaveStateEntry *se; |
9ed7d6ae | 794 | |
4d2ffa08 JK |
795 | /* If this triggers, alias support can be dropped for the vmsd. */ |
796 | assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id); | |
797 | ||
97f3ad35 | 798 | se = g_new0(SaveStateEntry, 1); |
9ed7d6ae | 799 | se->version_id = vmsd->version_id; |
0163a2e0 | 800 | se->section_id = savevm_state.global_section_id++; |
9ed7d6ae JQ |
801 | se->opaque = opaque; |
802 | se->vmsd = vmsd; | |
4d2ffa08 | 803 | se->alias_id = alias_id; |
9ed7d6ae | 804 | |
09e5ab63 AL |
805 | if (dev) { |
806 | char *id = qdev_get_dev_path(dev); | |
7685ee6a | 807 | if (id) { |
581f08ba DDAG |
808 | if (snprintf(se->idstr, sizeof(se->idstr), "%s/", id) >= |
809 | sizeof(se->idstr)) { | |
810 | error_setg(errp, "Path too long for VMState (%s)", id); | |
811 | g_free(id); | |
812 | g_free(se); | |
813 | ||
814 | return -1; | |
815 | } | |
128e4e10 | 816 | g_free(id); |
7685ee6a | 817 | |
97f3ad35 | 818 | se->compat = g_new0(CompatEntry, 1); |
7685ee6a AW |
819 | pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name); |
820 | se->compat->instance_id = instance_id == -1 ? | |
821 | calculate_compat_instance_id(vmsd->name) : instance_id; | |
822 | instance_id = -1; | |
823 | } | |
824 | } | |
825 | pstrcat(se->idstr, sizeof(se->idstr), vmsd->name); | |
826 | ||
8718e999 | 827 | if (instance_id == -1) { |
7685ee6a | 828 | se->instance_id = calculate_new_instance_id(se->idstr); |
8718e999 JQ |
829 | } else { |
830 | se->instance_id = instance_id; | |
9ed7d6ae | 831 | } |
7685ee6a | 832 | assert(!se->compat || se->instance_id == 0); |
f37bc036 | 833 | savevm_state_handler_insert(se); |
9ed7d6ae JQ |
834 | return 0; |
835 | } | |
836 | ||
0be71e32 AW |
837 | void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd, |
838 | void *opaque) | |
9ed7d6ae | 839 | { |
1eb7538b JQ |
840 | SaveStateEntry *se, *new_se; |
841 | ||
0163a2e0 | 842 | QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) { |
1eb7538b | 843 | if (se->vmsd == vmsd && se->opaque == opaque) { |
0163a2e0 | 844 | QTAILQ_REMOVE(&savevm_state.handlers, se, entry); |
ef1e1e07 | 845 | g_free(se->compat); |
7267c094 | 846 | g_free(se); |
1eb7538b JQ |
847 | } |
848 | } | |
9ed7d6ae JQ |
849 | } |
850 | ||
3a011c26 | 851 | static int vmstate_load(QEMUFile *f, SaveStateEntry *se) |
4082be4d | 852 | { |
9013dca5 | 853 | trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)"); |
9ed7d6ae | 854 | if (!se->vmsd) { /* Old style */ |
3a011c26 | 855 | return se->ops->load_state(f, se->opaque, se->load_version_id); |
9ed7d6ae | 856 | } |
3a011c26 | 857 | return vmstate_load_state(f, se->vmsd, se->opaque, se->load_version_id); |
4082be4d JQ |
858 | } |
859 | ||
8118f095 AG |
860 | static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc) |
861 | { | |
862 | int64_t old_offset, size; | |
863 | ||
864 | old_offset = qemu_ftell_fast(f); | |
865 | se->ops->save_state(f, se->opaque); | |
866 | size = qemu_ftell_fast(f) - old_offset; | |
867 | ||
868 | if (vmdesc) { | |
869 | json_prop_int(vmdesc, "size", size); | |
870 | json_start_array(vmdesc, "fields"); | |
871 | json_start_object(vmdesc, NULL); | |
872 | json_prop_str(vmdesc, "name", "data"); | |
873 | json_prop_int(vmdesc, "size", size); | |
874 | json_prop_str(vmdesc, "type", "buffer"); | |
875 | json_end_object(vmdesc); | |
876 | json_end_array(vmdesc); | |
877 | } | |
878 | } | |
879 | ||
687433f6 | 880 | static int vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc) |
4082be4d | 881 | { |
9013dca5 | 882 | trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)"); |
8118f095 AG |
883 | if (!se->vmsd) { |
884 | vmstate_save_old_style(f, se, vmdesc); | |
687433f6 | 885 | return 0; |
9ed7d6ae | 886 | } |
687433f6 | 887 | return vmstate_save_state(f, se->vmsd, se->opaque, vmdesc); |
4082be4d JQ |
888 | } |
889 | ||
ce39bfc9 DDAG |
890 | /* |
891 | * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL) | |
892 | */ | |
893 | static void save_section_header(QEMUFile *f, SaveStateEntry *se, | |
894 | uint8_t section_type) | |
895 | { | |
896 | qemu_put_byte(f, section_type); | |
897 | qemu_put_be32(f, se->section_id); | |
898 | ||
899 | if (section_type == QEMU_VM_SECTION_FULL || | |
900 | section_type == QEMU_VM_SECTION_START) { | |
901 | /* ID string */ | |
902 | size_t len = strlen(se->idstr); | |
903 | qemu_put_byte(f, len); | |
904 | qemu_put_buffer(f, (uint8_t *)se->idstr, len); | |
905 | ||
906 | qemu_put_be32(f, se->instance_id); | |
907 | qemu_put_be32(f, se->version_id); | |
908 | } | |
909 | } | |
910 | ||
f68945d4 DDAG |
911 | /* |
912 | * Write a footer onto device sections that catches cases misformatted device | |
913 | * sections. | |
914 | */ | |
915 | static void save_section_footer(QEMUFile *f, SaveStateEntry *se) | |
916 | { | |
15c38503 | 917 | if (migrate_get_current()->send_section_footer) { |
f68945d4 DDAG |
918 | qemu_put_byte(f, QEMU_VM_SECTION_FOOTER); |
919 | qemu_put_be32(f, se->section_id); | |
920 | } | |
921 | } | |
922 | ||
c76ca188 DDAG |
923 | /** |
924 | * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the | |
925 | * command and associated data. | |
926 | * | |
927 | * @f: File to send command on | |
928 | * @command: Command type to send | |
929 | * @len: Length of associated data | |
930 | * @data: Data associated with command. | |
931 | */ | |
20a519a0 JQ |
932 | static void qemu_savevm_command_send(QEMUFile *f, |
933 | enum qemu_vm_cmd command, | |
934 | uint16_t len, | |
935 | uint8_t *data) | |
c76ca188 DDAG |
936 | { |
937 | trace_savevm_command_send(command, len); | |
938 | qemu_put_byte(f, QEMU_VM_COMMAND); | |
939 | qemu_put_be16(f, (uint16_t)command); | |
940 | qemu_put_be16(f, len); | |
941 | qemu_put_buffer(f, data, len); | |
942 | qemu_fflush(f); | |
943 | } | |
944 | ||
aad555c2 ZC |
945 | void qemu_savevm_send_colo_enable(QEMUFile *f) |
946 | { | |
947 | trace_savevm_send_colo_enable(); | |
948 | qemu_savevm_command_send(f, MIG_CMD_ENABLE_COLO, 0, NULL); | |
949 | } | |
950 | ||
2e37701e DDAG |
951 | void qemu_savevm_send_ping(QEMUFile *f, uint32_t value) |
952 | { | |
953 | uint32_t buf; | |
954 | ||
955 | trace_savevm_send_ping(value); | |
956 | buf = cpu_to_be32(value); | |
957 | qemu_savevm_command_send(f, MIG_CMD_PING, sizeof(value), (uint8_t *)&buf); | |
958 | } | |
959 | ||
960 | void qemu_savevm_send_open_return_path(QEMUFile *f) | |
961 | { | |
962 | trace_savevm_send_open_return_path(); | |
963 | qemu_savevm_command_send(f, MIG_CMD_OPEN_RETURN_PATH, 0, NULL); | |
964 | } | |
965 | ||
11cf1d98 DDAG |
966 | /* We have a buffer of data to send; we don't want that all to be loaded |
967 | * by the command itself, so the command contains just the length of the | |
968 | * extra buffer that we then send straight after it. | |
969 | * TODO: Must be a better way to organise that | |
970 | * | |
971 | * Returns: | |
972 | * 0 on success | |
973 | * -ve on error | |
974 | */ | |
61b67d47 | 975 | int qemu_savevm_send_packaged(QEMUFile *f, const uint8_t *buf, size_t len) |
11cf1d98 | 976 | { |
11cf1d98 DDAG |
977 | uint32_t tmp; |
978 | ||
979 | if (len > MAX_VM_CMD_PACKAGED_SIZE) { | |
980 | error_report("%s: Unreasonably large packaged state: %zu", | |
981 | __func__, len); | |
982 | return -1; | |
983 | } | |
984 | ||
985 | tmp = cpu_to_be32(len); | |
986 | ||
987 | trace_qemu_savevm_send_packaged(); | |
988 | qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp); | |
989 | ||
61b67d47 | 990 | qemu_put_buffer(f, buf, len); |
11cf1d98 DDAG |
991 | |
992 | return 0; | |
993 | } | |
994 | ||
093e3c42 DDAG |
995 | /* Send prior to any postcopy transfer */ |
996 | void qemu_savevm_send_postcopy_advise(QEMUFile *f) | |
997 | { | |
58110f0a VSO |
998 | if (migrate_postcopy_ram()) { |
999 | uint64_t tmp[2]; | |
1000 | tmp[0] = cpu_to_be64(ram_pagesize_summary()); | |
1001 | tmp[1] = cpu_to_be64(qemu_target_page_size()); | |
1002 | ||
1003 | trace_qemu_savevm_send_postcopy_advise(); | |
1004 | qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, | |
1005 | 16, (uint8_t *)tmp); | |
1006 | } else { | |
1007 | qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 0, NULL); | |
1008 | } | |
093e3c42 DDAG |
1009 | } |
1010 | ||
1011 | /* Sent prior to starting the destination running in postcopy, discard pages | |
1012 | * that have already been sent but redirtied on the source. | |
1013 | * CMD_POSTCOPY_RAM_DISCARD consist of: | |
1014 | * byte version (0) | |
1015 | * byte Length of name field (not including 0) | |
1016 | * n x byte RAM block name | |
1017 | * byte 0 terminator (just for safety) | |
1018 | * n x Byte ranges within the named RAMBlock | |
1019 | * be64 Start of the range | |
1020 | * be64 Length | |
1021 | * | |
1022 | * name: RAMBlock name that these entries are part of | |
1023 | * len: Number of page entries | |
1024 | * start_list: 'len' addresses | |
1025 | * length_list: 'len' addresses | |
1026 | * | |
1027 | */ | |
1028 | void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name, | |
1029 | uint16_t len, | |
1030 | uint64_t *start_list, | |
1031 | uint64_t *length_list) | |
1032 | { | |
1033 | uint8_t *buf; | |
1034 | uint16_t tmplen; | |
1035 | uint16_t t; | |
1036 | size_t name_len = strlen(name); | |
1037 | ||
1038 | trace_qemu_savevm_send_postcopy_ram_discard(name, len); | |
1039 | assert(name_len < 256); | |
1040 | buf = g_malloc0(1 + 1 + name_len + 1 + (8 + 8) * len); | |
1041 | buf[0] = postcopy_ram_discard_version; | |
1042 | buf[1] = name_len; | |
1043 | memcpy(buf + 2, name, name_len); | |
1044 | tmplen = 2 + name_len; | |
1045 | buf[tmplen++] = '\0'; | |
1046 | ||
1047 | for (t = 0; t < len; t++) { | |
4d885131 | 1048 | stq_be_p(buf + tmplen, start_list[t]); |
093e3c42 | 1049 | tmplen += 8; |
4d885131 | 1050 | stq_be_p(buf + tmplen, length_list[t]); |
093e3c42 DDAG |
1051 | tmplen += 8; |
1052 | } | |
1053 | qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RAM_DISCARD, tmplen, buf); | |
1054 | g_free(buf); | |
1055 | } | |
1056 | ||
1057 | /* Get the destination into a state where it can receive postcopy data. */ | |
1058 | void qemu_savevm_send_postcopy_listen(QEMUFile *f) | |
1059 | { | |
1060 | trace_savevm_send_postcopy_listen(); | |
1061 | qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_LISTEN, 0, NULL); | |
1062 | } | |
1063 | ||
1064 | /* Kick the destination into running */ | |
1065 | void qemu_savevm_send_postcopy_run(QEMUFile *f) | |
1066 | { | |
1067 | trace_savevm_send_postcopy_run(); | |
1068 | qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RUN, 0, NULL); | |
1069 | } | |
1070 | ||
3f5875ec PX |
1071 | void qemu_savevm_send_postcopy_resume(QEMUFile *f) |
1072 | { | |
1073 | trace_savevm_send_postcopy_resume(); | |
1074 | qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RESUME, 0, NULL); | |
1075 | } | |
1076 | ||
f25d4225 PX |
1077 | void qemu_savevm_send_recv_bitmap(QEMUFile *f, char *block_name) |
1078 | { | |
1079 | size_t len; | |
1080 | char buf[256]; | |
1081 | ||
1082 | trace_savevm_send_recv_bitmap(block_name); | |
1083 | ||
1084 | buf[0] = len = strlen(block_name); | |
1085 | memcpy(buf + 1, block_name, len); | |
1086 | ||
1087 | qemu_savevm_command_send(f, MIG_CMD_RECV_BITMAP, len + 1, (uint8_t *)buf); | |
1088 | } | |
1089 | ||
e1c37d0e | 1090 | bool qemu_savevm_state_blocked(Error **errp) |
dc912121 AW |
1091 | { |
1092 | SaveStateEntry *se; | |
1093 | ||
0163a2e0 | 1094 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
7d854c47 | 1095 | if (se->vmsd && se->vmsd->unmigratable) { |
f231b88d CR |
1096 | error_setg(errp, "State blocked by non-migratable device '%s'", |
1097 | se->idstr); | |
dc912121 AW |
1098 | return true; |
1099 | } | |
1100 | } | |
1101 | return false; | |
1102 | } | |
1103 | ||
f796baa1 DDAG |
1104 | void qemu_savevm_state_header(QEMUFile *f) |
1105 | { | |
1106 | trace_savevm_state_header(); | |
1107 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); | |
1108 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); | |
172dfd4f | 1109 | |
4ffdb337 | 1110 | if (migrate_get_current()->send_configuration) { |
172dfd4f DDAG |
1111 | qemu_put_byte(f, QEMU_VM_CONFIGURATION); |
1112 | vmstate_save_state(f, &vmstate_configuration, &savevm_state, 0); | |
1113 | } | |
f796baa1 DDAG |
1114 | } |
1115 | ||
9907e842 | 1116 | void qemu_savevm_state_setup(QEMUFile *f) |
a672b469 AL |
1117 | { |
1118 | SaveStateEntry *se; | |
bd227060 | 1119 | Error *local_err = NULL; |
39346385 | 1120 | int ret; |
a672b469 | 1121 | |
9907e842 | 1122 | trace_savevm_state_setup(); |
0163a2e0 | 1123 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
9907e842 | 1124 | if (!se->ops || !se->ops->save_setup) { |
a672b469 | 1125 | continue; |
22ea40f4 | 1126 | } |
cea3b4c0 | 1127 | if (se->ops->is_active) { |
6bd68781 JQ |
1128 | if (!se->ops->is_active(se->opaque)) { |
1129 | continue; | |
1130 | } | |
1131 | } | |
ce39bfc9 | 1132 | save_section_header(f, se, QEMU_VM_SECTION_START); |
a672b469 | 1133 | |
9907e842 | 1134 | ret = se->ops->save_setup(f, se->opaque); |
f68945d4 | 1135 | save_section_footer(f, se); |
2975725f | 1136 | if (ret < 0) { |
47c8c17a PB |
1137 | qemu_file_set_error(f, ret); |
1138 | break; | |
2975725f | 1139 | } |
a672b469 | 1140 | } |
bd227060 WW |
1141 | |
1142 | if (precopy_notify(PRECOPY_NOTIFY_SETUP, &local_err)) { | |
1143 | error_report_err(local_err); | |
1144 | } | |
a672b469 AL |
1145 | } |
1146 | ||
d1b8eadb PX |
1147 | int qemu_savevm_state_resume_prepare(MigrationState *s) |
1148 | { | |
1149 | SaveStateEntry *se; | |
1150 | int ret; | |
1151 | ||
1152 | trace_savevm_state_resume_prepare(); | |
1153 | ||
1154 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { | |
1155 | if (!se->ops || !se->ops->resume_prepare) { | |
1156 | continue; | |
1157 | } | |
cea3b4c0 | 1158 | if (se->ops->is_active) { |
d1b8eadb PX |
1159 | if (!se->ops->is_active(se->opaque)) { |
1160 | continue; | |
1161 | } | |
1162 | } | |
1163 | ret = se->ops->resume_prepare(s, se->opaque); | |
1164 | if (ret < 0) { | |
1165 | return ret; | |
1166 | } | |
1167 | } | |
1168 | ||
1169 | return 0; | |
1170 | } | |
1171 | ||
39346385 | 1172 | /* |
07f35073 | 1173 | * this function has three return values: |
39346385 JQ |
1174 | * negative: there was one error, and we have -errno. |
1175 | * 0 : We haven't finished, caller have to go again | |
1176 | * 1 : We have finished, we can go to complete phase | |
1177 | */ | |
35ecd943 | 1178 | int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy) |
a672b469 AL |
1179 | { |
1180 | SaveStateEntry *se; | |
1181 | int ret = 1; | |
1182 | ||
9013dca5 | 1183 | trace_savevm_state_iterate(); |
0163a2e0 | 1184 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
16310a3c | 1185 | if (!se->ops || !se->ops->save_live_iterate) { |
a672b469 | 1186 | continue; |
22ea40f4 | 1187 | } |
a94cd7b8 WY |
1188 | if (se->ops->is_active && |
1189 | !se->ops->is_active(se->opaque)) { | |
1190 | continue; | |
6bd68781 | 1191 | } |
a94cd7b8 WY |
1192 | if (se->ops->is_active_iterate && |
1193 | !se->ops->is_active_iterate(se->opaque)) { | |
1194 | continue; | |
c865d848 | 1195 | } |
35ecd943 DDAG |
1196 | /* |
1197 | * In the postcopy phase, any device that doesn't know how to | |
1198 | * do postcopy should have saved it's state in the _complete | |
1199 | * call that's already run, it might get confused if we call | |
1200 | * iterate afterwards. | |
1201 | */ | |
c6467627 VSO |
1202 | if (postcopy && |
1203 | !(se->ops->has_postcopy && se->ops->has_postcopy(se->opaque))) { | |
35ecd943 DDAG |
1204 | continue; |
1205 | } | |
aac844ed JQ |
1206 | if (qemu_file_rate_limit(f)) { |
1207 | return 0; | |
1208 | } | |
464400f6 | 1209 | trace_savevm_section_start(se->idstr, se->section_id); |
ce39bfc9 DDAG |
1210 | |
1211 | save_section_header(f, se, QEMU_VM_SECTION_PART); | |
a672b469 | 1212 | |
16310a3c | 1213 | ret = se->ops->save_live_iterate(f, se->opaque); |
a5df2a02 | 1214 | trace_savevm_section_end(se->idstr, se->section_id, ret); |
f68945d4 | 1215 | save_section_footer(f, se); |
517a13c9 | 1216 | |
47c8c17a PB |
1217 | if (ret < 0) { |
1218 | qemu_file_set_error(f, ret); | |
1219 | } | |
2975725f | 1220 | if (ret <= 0) { |
90697be8 JK |
1221 | /* Do not proceed to the next vmstate before this one reported |
1222 | completion of the current stage. This serializes the migration | |
1223 | and reduces the probability that a faster changing state is | |
1224 | synchronized over and over again. */ | |
1225 | break; | |
1226 | } | |
a672b469 | 1227 | } |
39346385 | 1228 | return ret; |
a672b469 AL |
1229 | } |
1230 | ||
9850c604 AG |
1231 | static bool should_send_vmdesc(void) |
1232 | { | |
1233 | MachineState *machine = MACHINE(qdev_get_machine()); | |
5727309d | 1234 | bool in_postcopy = migration_in_postcopy(); |
8421b205 | 1235 | return !machine->suppress_vmdesc && !in_postcopy; |
9850c604 AG |
1236 | } |
1237 | ||
763c906b DDAG |
1238 | /* |
1239 | * Calls the save_live_complete_postcopy methods | |
1240 | * causing the last few pages to be sent immediately and doing any associated | |
1241 | * cleanup. | |
1242 | * Note postcopy also calls qemu_savevm_state_complete_precopy to complete | |
1243 | * all the other devices, but that happens at the point we switch to postcopy. | |
1244 | */ | |
1245 | void qemu_savevm_state_complete_postcopy(QEMUFile *f) | |
1246 | { | |
1247 | SaveStateEntry *se; | |
1248 | int ret; | |
1249 | ||
1250 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { | |
1251 | if (!se->ops || !se->ops->save_live_complete_postcopy) { | |
1252 | continue; | |
1253 | } | |
cea3b4c0 | 1254 | if (se->ops->is_active) { |
763c906b DDAG |
1255 | if (!se->ops->is_active(se->opaque)) { |
1256 | continue; | |
1257 | } | |
1258 | } | |
1259 | trace_savevm_section_start(se->idstr, se->section_id); | |
1260 | /* Section type */ | |
1261 | qemu_put_byte(f, QEMU_VM_SECTION_END); | |
1262 | qemu_put_be32(f, se->section_id); | |
1263 | ||
1264 | ret = se->ops->save_live_complete_postcopy(f, se->opaque); | |
1265 | trace_savevm_section_end(se->idstr, se->section_id, ret); | |
1266 | save_section_footer(f, se); | |
1267 | if (ret < 0) { | |
1268 | qemu_file_set_error(f, ret); | |
1269 | return; | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | qemu_put_byte(f, QEMU_VM_EOF); | |
1274 | qemu_fflush(f); | |
1275 | } | |
1276 | ||
622a80c9 | 1277 | static |
e326767b | 1278 | int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy) |
a672b469 AL |
1279 | { |
1280 | SaveStateEntry *se; | |
2975725f | 1281 | int ret; |
ea375f9a | 1282 | |
0163a2e0 | 1283 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
763c906b | 1284 | if (!se->ops || |
c6467627 VSO |
1285 | (in_postcopy && se->ops->has_postcopy && |
1286 | se->ops->has_postcopy(se->opaque)) || | |
763c906b | 1287 | !se->ops->save_live_complete_precopy) { |
a672b469 | 1288 | continue; |
22ea40f4 | 1289 | } |
1c0d249d | 1290 | |
cea3b4c0 | 1291 | if (se->ops->is_active) { |
6bd68781 JQ |
1292 | if (!se->ops->is_active(se->opaque)) { |
1293 | continue; | |
1294 | } | |
1295 | } | |
464400f6 | 1296 | trace_savevm_section_start(se->idstr, se->section_id); |
ce39bfc9 DDAG |
1297 | |
1298 | save_section_header(f, se, QEMU_VM_SECTION_END); | |
a672b469 | 1299 | |
a3e06c3d | 1300 | ret = se->ops->save_live_complete_precopy(f, se->opaque); |
a5df2a02 | 1301 | trace_savevm_section_end(se->idstr, se->section_id, ret); |
f68945d4 | 1302 | save_section_footer(f, se); |
2975725f | 1303 | if (ret < 0) { |
47c8c17a | 1304 | qemu_file_set_error(f, ret); |
a1fbe750 | 1305 | return -1; |
2975725f | 1306 | } |
a672b469 AL |
1307 | } |
1308 | ||
622a80c9 WY |
1309 | return 0; |
1310 | } | |
1311 | ||
1312 | static | |
1313 | int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f, | |
1314 | bool in_postcopy, | |
1315 | bool inactivate_disks) | |
1316 | { | |
1317 | QJSON *vmdesc; | |
1318 | int vmdesc_len; | |
1319 | SaveStateEntry *se; | |
1320 | int ret; | |
1c0d249d | 1321 | |
8118f095 | 1322 | vmdesc = qjson_new(); |
46d702b1 | 1323 | json_prop_int(vmdesc, "page_size", qemu_target_page_size()); |
8118f095 | 1324 | json_start_array(vmdesc, "devices"); |
0163a2e0 | 1325 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
a672b469 | 1326 | |
22ea40f4 | 1327 | if ((!se->ops || !se->ops->save_state) && !se->vmsd) { |
5cecf414 | 1328 | continue; |
22ea40f4 | 1329 | } |
df896152 JQ |
1330 | if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) { |
1331 | trace_savevm_section_skip(se->idstr, se->section_id); | |
1332 | continue; | |
1333 | } | |
1334 | ||
464400f6 | 1335 | trace_savevm_section_start(se->idstr, se->section_id); |
8118f095 AG |
1336 | |
1337 | json_start_object(vmdesc, NULL); | |
1338 | json_prop_str(vmdesc, "name", se->idstr); | |
1339 | json_prop_int(vmdesc, "instance_id", se->instance_id); | |
1340 | ||
ce39bfc9 | 1341 | save_section_header(f, se, QEMU_VM_SECTION_FULL); |
687433f6 DDAG |
1342 | ret = vmstate_save(f, se, vmdesc); |
1343 | if (ret) { | |
1344 | qemu_file_set_error(f, ret); | |
1345 | return ret; | |
1346 | } | |
a5df2a02 | 1347 | trace_savevm_section_end(se->idstr, se->section_id, 0); |
f68945d4 | 1348 | save_section_footer(f, se); |
bdf46d64 WY |
1349 | |
1350 | json_end_object(vmdesc); | |
a672b469 AL |
1351 | } |
1352 | ||
a1fbe750 FZ |
1353 | if (inactivate_disks) { |
1354 | /* Inactivate before sending QEMU_VM_EOF so that the | |
1355 | * bdrv_invalidate_cache_all() on the other end won't fail. */ | |
1356 | ret = bdrv_inactivate_all(); | |
1357 | if (ret) { | |
c232bf58 DDAG |
1358 | error_report("%s: bdrv_inactivate_all() failed (%d)", |
1359 | __func__, ret); | |
a1fbe750 FZ |
1360 | qemu_file_set_error(f, ret); |
1361 | return ret; | |
1362 | } | |
1363 | } | |
763c906b DDAG |
1364 | if (!in_postcopy) { |
1365 | /* Postcopy stream will still be going */ | |
1366 | qemu_put_byte(f, QEMU_VM_EOF); | |
1367 | } | |
8118f095 AG |
1368 | |
1369 | json_end_array(vmdesc); | |
1370 | qjson_finish(vmdesc); | |
1371 | vmdesc_len = strlen(qjson_get_str(vmdesc)); | |
1372 | ||
9850c604 AG |
1373 | if (should_send_vmdesc()) { |
1374 | qemu_put_byte(f, QEMU_VM_VMDESCRIPTION); | |
1375 | qemu_put_be32(f, vmdesc_len); | |
1376 | qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len); | |
1377 | } | |
b72fe9e6 | 1378 | qjson_destroy(vmdesc); |
8118f095 | 1379 | |
622a80c9 WY |
1380 | return 0; |
1381 | } | |
1382 | ||
1383 | int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only, | |
1384 | bool inactivate_disks) | |
1385 | { | |
1386 | int ret; | |
1387 | Error *local_err = NULL; | |
1388 | bool in_postcopy = migration_in_postcopy(); | |
1389 | ||
1390 | if (precopy_notify(PRECOPY_NOTIFY_COMPLETE, &local_err)) { | |
1391 | error_report_err(local_err); | |
1392 | } | |
1393 | ||
1394 | trace_savevm_state_complete_precopy(); | |
1395 | ||
1396 | cpu_synchronize_all_states(); | |
1397 | ||
e326767b WY |
1398 | if (!in_postcopy || iterable_only) { |
1399 | ret = qemu_savevm_state_complete_precopy_iterable(f, in_postcopy); | |
1400 | if (ret) { | |
1401 | return ret; | |
1402 | } | |
622a80c9 WY |
1403 | } |
1404 | ||
1405 | if (iterable_only) { | |
1406 | goto flush; | |
1407 | } | |
1408 | ||
1409 | ret = qemu_savevm_state_complete_precopy_non_iterable(f, in_postcopy, | |
1410 | inactivate_disks); | |
1411 | if (ret) { | |
1412 | return ret; | |
1413 | } | |
1414 | ||
4e455d51 | 1415 | flush: |
edaae611 | 1416 | qemu_fflush(f); |
a1fbe750 | 1417 | return 0; |
a672b469 AL |
1418 | } |
1419 | ||
c31b098f DDAG |
1420 | /* Give an estimate of the amount left to be transferred, |
1421 | * the result is split into the amount for units that can and | |
1422 | * for units that can't do postcopy. | |
1423 | */ | |
faec066a | 1424 | void qemu_savevm_state_pending(QEMUFile *f, uint64_t threshold_size, |
47995026 VSO |
1425 | uint64_t *res_precopy_only, |
1426 | uint64_t *res_compatible, | |
1427 | uint64_t *res_postcopy_only) | |
e4ed1541 JQ |
1428 | { |
1429 | SaveStateEntry *se; | |
c31b098f | 1430 | |
47995026 VSO |
1431 | *res_precopy_only = 0; |
1432 | *res_compatible = 0; | |
1433 | *res_postcopy_only = 0; | |
c31b098f | 1434 | |
e4ed1541 | 1435 | |
0163a2e0 | 1436 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
e4ed1541 JQ |
1437 | if (!se->ops || !se->ops->save_live_pending) { |
1438 | continue; | |
1439 | } | |
cea3b4c0 | 1440 | if (se->ops->is_active) { |
e4ed1541 JQ |
1441 | if (!se->ops->is_active(se->opaque)) { |
1442 | continue; | |
1443 | } | |
1444 | } | |
faec066a | 1445 | se->ops->save_live_pending(f, se->opaque, threshold_size, |
47995026 VSO |
1446 | res_precopy_only, res_compatible, |
1447 | res_postcopy_only); | |
e4ed1541 | 1448 | } |
e4ed1541 JQ |
1449 | } |
1450 | ||
ea7415fa | 1451 | void qemu_savevm_state_cleanup(void) |
4ec7fcc7 JK |
1452 | { |
1453 | SaveStateEntry *se; | |
bd227060 WW |
1454 | Error *local_err = NULL; |
1455 | ||
1456 | if (precopy_notify(PRECOPY_NOTIFY_CLEANUP, &local_err)) { | |
1457 | error_report_err(local_err); | |
1458 | } | |
4ec7fcc7 | 1459 | |
ea7415fa | 1460 | trace_savevm_state_cleanup(); |
0163a2e0 | 1461 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
70f794fc JQ |
1462 | if (se->ops && se->ops->save_cleanup) { |
1463 | se->ops->save_cleanup(se->opaque); | |
4ec7fcc7 JK |
1464 | } |
1465 | } | |
1466 | } | |
1467 | ||
5d80448c | 1468 | static int qemu_savevm_state(QEMUFile *f, Error **errp) |
a672b469 | 1469 | { |
a672b469 | 1470 | int ret; |
3e0c8050 | 1471 | MigrationState *ms = migrate_get_current(); |
6dcf6668 | 1472 | MigrationStatus status; |
3e0c8050 | 1473 | |
3d63da16 JL |
1474 | if (migration_is_setup_or_active(ms->state) || |
1475 | ms->state == MIGRATION_STATUS_CANCELLING || | |
1476 | ms->state == MIGRATION_STATUS_COLO) { | |
1477 | error_setg(errp, QERR_MIGRATION_ACTIVE); | |
1478 | return -EINVAL; | |
1479 | } | |
a672b469 | 1480 | |
ce7c817c JQ |
1481 | if (migrate_use_block()) { |
1482 | error_setg(errp, "Block migration and snapshots are incompatible"); | |
3d63da16 | 1483 | return -EINVAL; |
ce7c817c JQ |
1484 | } |
1485 | ||
3d63da16 | 1486 | migrate_init(ms); |
87f3bd87 | 1487 | memset(&ram_counters, 0, sizeof(ram_counters)); |
3d63da16 JL |
1488 | ms->to_dst_file = f; |
1489 | ||
9b095037 | 1490 | qemu_mutex_unlock_iothread(); |
f796baa1 | 1491 | qemu_savevm_state_header(f); |
9907e842 | 1492 | qemu_savevm_state_setup(f); |
9b095037 PB |
1493 | qemu_mutex_lock_iothread(); |
1494 | ||
47c8c17a | 1495 | while (qemu_file_get_error(f) == 0) { |
35ecd943 | 1496 | if (qemu_savevm_state_iterate(f, false) > 0) { |
47c8c17a PB |
1497 | break; |
1498 | } | |
1499 | } | |
a672b469 | 1500 | |
47c8c17a | 1501 | ret = qemu_file_get_error(f); |
39346385 | 1502 | if (ret == 0) { |
a1fbe750 | 1503 | qemu_savevm_state_complete_precopy(f, false, false); |
624b9cc2 | 1504 | ret = qemu_file_get_error(f); |
39346385 | 1505 | } |
15b3b8ea | 1506 | qemu_savevm_state_cleanup(); |
04943eba | 1507 | if (ret != 0) { |
5d80448c | 1508 | error_setg_errno(errp, -ret, "Error while writing VM state"); |
04943eba | 1509 | } |
6dcf6668 | 1510 | |
6dcf6668 DL |
1511 | if (ret != 0) { |
1512 | status = MIGRATION_STATUS_FAILED; | |
1513 | } else { | |
1514 | status = MIGRATION_STATUS_COMPLETED; | |
1515 | } | |
1516 | migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP, status); | |
f9c8caa0 VSO |
1517 | |
1518 | /* f is outer parameter, it should not stay in global migration state after | |
1519 | * this function finished */ | |
1520 | ms->to_dst_file = NULL; | |
1521 | ||
a672b469 AL |
1522 | return ret; |
1523 | } | |
1524 | ||
3f6df99d | 1525 | void qemu_savevm_live_state(QEMUFile *f) |
a7ae8355 | 1526 | { |
3f6df99d ZC |
1527 | /* save QEMU_VM_SECTION_END section */ |
1528 | qemu_savevm_state_complete_precopy(f, true, false); | |
1529 | qemu_put_byte(f, QEMU_VM_EOF); | |
1530 | } | |
a7ae8355 | 1531 | |
3f6df99d ZC |
1532 | int qemu_save_device_state(QEMUFile *f) |
1533 | { | |
1534 | SaveStateEntry *se; | |
a7ae8355 | 1535 | |
3f6df99d ZC |
1536 | if (!migration_in_colo_state()) { |
1537 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); | |
1538 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); | |
1539 | } | |
a7ae8355 SS |
1540 | cpu_synchronize_all_states(); |
1541 | ||
0163a2e0 | 1542 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
687433f6 DDAG |
1543 | int ret; |
1544 | ||
a7ae8355 SS |
1545 | if (se->is_ram) { |
1546 | continue; | |
1547 | } | |
22ea40f4 | 1548 | if ((!se->ops || !se->ops->save_state) && !se->vmsd) { |
a7ae8355 SS |
1549 | continue; |
1550 | } | |
df896152 JQ |
1551 | if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) { |
1552 | continue; | |
1553 | } | |
a7ae8355 | 1554 | |
ce39bfc9 | 1555 | save_section_header(f, se, QEMU_VM_SECTION_FULL); |
a7ae8355 | 1556 | |
687433f6 DDAG |
1557 | ret = vmstate_save(f, se, NULL); |
1558 | if (ret) { | |
1559 | return ret; | |
1560 | } | |
f68945d4 DDAG |
1561 | |
1562 | save_section_footer(f, se); | |
a7ae8355 SS |
1563 | } |
1564 | ||
1565 | qemu_put_byte(f, QEMU_VM_EOF); | |
1566 | ||
1567 | return qemu_file_get_error(f); | |
1568 | } | |
1569 | ||
a672b469 AL |
1570 | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
1571 | { | |
1572 | SaveStateEntry *se; | |
1573 | ||
0163a2e0 | 1574 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
a672b469 | 1575 | if (!strcmp(se->idstr, idstr) && |
4d2ffa08 JK |
1576 | (instance_id == se->instance_id || |
1577 | instance_id == se->alias_id)) | |
a672b469 | 1578 | return se; |
7685ee6a AW |
1579 | /* Migrating from an older version? */ |
1580 | if (strstr(se->idstr, idstr) && se->compat) { | |
1581 | if (!strcmp(se->compat->idstr, idstr) && | |
1582 | (instance_id == se->compat->instance_id || | |
1583 | instance_id == se->alias_id)) | |
1584 | return se; | |
1585 | } | |
a672b469 AL |
1586 | } |
1587 | return NULL; | |
1588 | } | |
1589 | ||
7b89bf27 DDAG |
1590 | enum LoadVMExitCodes { |
1591 | /* Allow a command to quit all layers of nested loadvm loops */ | |
1592 | LOADVM_QUIT = 1, | |
1593 | }; | |
1594 | ||
093e3c42 DDAG |
1595 | /* ------ incoming postcopy messages ------ */ |
1596 | /* 'advise' arrives before any transfers just to tell us that a postcopy | |
1597 | * *might* happen - it might be skipped if precopy transferred everything | |
1598 | * quickly. | |
1599 | */ | |
875fcd01 GK |
1600 | static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis, |
1601 | uint16_t len) | |
093e3c42 DDAG |
1602 | { |
1603 | PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_ADVISE); | |
e8ca1db2 | 1604 | uint64_t remote_pagesize_summary, local_pagesize_summary, remote_tps; |
d3dff7a5 | 1605 | Error *local_err = NULL; |
093e3c42 DDAG |
1606 | |
1607 | trace_loadvm_postcopy_handle_advise(); | |
1608 | if (ps != POSTCOPY_INCOMING_NONE) { | |
1609 | error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps); | |
1610 | return -1; | |
1611 | } | |
1612 | ||
875fcd01 GK |
1613 | switch (len) { |
1614 | case 0: | |
1615 | if (migrate_postcopy_ram()) { | |
1616 | error_report("RAM postcopy is enabled but have 0 byte advise"); | |
1617 | return -EINVAL; | |
1618 | } | |
58110f0a | 1619 | return 0; |
875fcd01 GK |
1620 | case 8 + 8: |
1621 | if (!migrate_postcopy_ram()) { | |
1622 | error_report("RAM postcopy is disabled but have 16 byte advise"); | |
1623 | return -EINVAL; | |
1624 | } | |
1625 | break; | |
1626 | default: | |
1627 | error_report("CMD_POSTCOPY_ADVISE invalid length (%d)", len); | |
1628 | return -EINVAL; | |
58110f0a VSO |
1629 | } |
1630 | ||
d7651f15 | 1631 | if (!postcopy_ram_supported_by_host(mis)) { |
328d4d85 | 1632 | postcopy_state_set(POSTCOPY_INCOMING_NONE); |
eb59db53 DDAG |
1633 | return -1; |
1634 | } | |
1635 | ||
e8ca1db2 DDAG |
1636 | remote_pagesize_summary = qemu_get_be64(mis->from_src_file); |
1637 | local_pagesize_summary = ram_pagesize_summary(); | |
1638 | ||
1639 | if (remote_pagesize_summary != local_pagesize_summary) { | |
093e3c42 | 1640 | /* |
e8ca1db2 DDAG |
1641 | * This detects two potential causes of mismatch: |
1642 | * a) A mismatch in host page sizes | |
1643 | * Some combinations of mismatch are probably possible but it gets | |
1644 | * a bit more complicated. In particular we need to place whole | |
1645 | * host pages on the dest at once, and we need to ensure that we | |
1646 | * handle dirtying to make sure we never end up sending part of | |
1647 | * a hostpage on it's own. | |
1648 | * b) The use of different huge page sizes on source/destination | |
1649 | * a more fine grain test is performed during RAM block migration | |
1650 | * but this test here causes a nice early clear failure, and | |
1651 | * also fails when passed to an older qemu that doesn't | |
1652 | * do huge pages. | |
093e3c42 | 1653 | */ |
e8ca1db2 DDAG |
1654 | error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64 |
1655 | " d=%" PRIx64 ")", | |
1656 | remote_pagesize_summary, local_pagesize_summary); | |
093e3c42 DDAG |
1657 | return -1; |
1658 | } | |
1659 | ||
1660 | remote_tps = qemu_get_be64(mis->from_src_file); | |
20afaed9 | 1661 | if (remote_tps != qemu_target_page_size()) { |
093e3c42 DDAG |
1662 | /* |
1663 | * Again, some differences could be dealt with, but for now keep it | |
1664 | * simple. | |
1665 | */ | |
20afaed9 JQ |
1666 | error_report("Postcopy needs matching target page sizes (s=%d d=%zd)", |
1667 | (int)remote_tps, qemu_target_page_size()); | |
093e3c42 DDAG |
1668 | return -1; |
1669 | } | |
1670 | ||
d3dff7a5 DDAG |
1671 | if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_ADVISE, &local_err)) { |
1672 | error_report_err(local_err); | |
1673 | return -1; | |
1674 | } | |
1675 | ||
1caddf8a DDAG |
1676 | if (ram_postcopy_incoming_init(mis)) { |
1677 | return -1; | |
1678 | } | |
1679 | ||
093e3c42 DDAG |
1680 | return 0; |
1681 | } | |
1682 | ||
1683 | /* After postcopy we will be told to throw some pages away since they're | |
1684 | * dirty and will have to be demand fetched. Must happen before CPU is | |
1685 | * started. | |
1686 | * There can be 0..many of these messages, each encoding multiple pages. | |
1687 | */ | |
1688 | static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis, | |
1689 | uint16_t len) | |
1690 | { | |
1691 | int tmp; | |
1692 | char ramid[256]; | |
1693 | PostcopyState ps = postcopy_state_get(); | |
1694 | ||
1695 | trace_loadvm_postcopy_ram_handle_discard(); | |
1696 | ||
1697 | switch (ps) { | |
1698 | case POSTCOPY_INCOMING_ADVISE: | |
1699 | /* 1st discard */ | |
f9527107 | 1700 | tmp = postcopy_ram_prepare_discard(mis); |
093e3c42 DDAG |
1701 | if (tmp) { |
1702 | return tmp; | |
1703 | } | |
1704 | break; | |
1705 | ||
1706 | case POSTCOPY_INCOMING_DISCARD: | |
1707 | /* Expected state */ | |
1708 | break; | |
1709 | ||
1710 | default: | |
1711 | error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)", | |
1712 | ps); | |
1713 | return -1; | |
1714 | } | |
1715 | /* We're expecting a | |
1716 | * Version (0) | |
1717 | * a RAM ID string (length byte, name, 0 term) | |
1718 | * then at least 1 16 byte chunk | |
1719 | */ | |
1720 | if (len < (1 + 1 + 1 + 1 + 2 * 8)) { | |
1721 | error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len); | |
1722 | return -1; | |
1723 | } | |
1724 | ||
1725 | tmp = qemu_get_byte(mis->from_src_file); | |
1726 | if (tmp != postcopy_ram_discard_version) { | |
1727 | error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp); | |
1728 | return -1; | |
1729 | } | |
1730 | ||
1731 | if (!qemu_get_counted_string(mis->from_src_file, ramid)) { | |
1732 | error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID"); | |
1733 | return -1; | |
1734 | } | |
1735 | tmp = qemu_get_byte(mis->from_src_file); | |
1736 | if (tmp != 0) { | |
1737 | error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp); | |
1738 | return -1; | |
1739 | } | |
1740 | ||
1741 | len -= 3 + strlen(ramid); | |
1742 | if (len % 16) { | |
1743 | error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len); | |
1744 | return -1; | |
1745 | } | |
1746 | trace_loadvm_postcopy_ram_handle_discard_header(ramid, len); | |
1747 | while (len) { | |
093e3c42 DDAG |
1748 | uint64_t start_addr, block_length; |
1749 | start_addr = qemu_get_be64(mis->from_src_file); | |
1750 | block_length = qemu_get_be64(mis->from_src_file); | |
1751 | ||
1752 | len -= 16; | |
aaa2064c | 1753 | int ret = ram_discard_range(ramid, start_addr, block_length); |
093e3c42 DDAG |
1754 | if (ret) { |
1755 | return ret; | |
1756 | } | |
093e3c42 DDAG |
1757 | } |
1758 | trace_loadvm_postcopy_ram_handle_discard_end(); | |
1759 | ||
1760 | return 0; | |
1761 | } | |
1762 | ||
c76201ab DDAG |
1763 | /* |
1764 | * Triggered by a postcopy_listen command; this thread takes over reading | |
1765 | * the input stream, leaving the main thread free to carry on loading the rest | |
1766 | * of the device state (from RAM). | |
1767 | * (TODO:This could do with being in a postcopy file - but there again it's | |
1768 | * just another input loop, not that postcopy specific) | |
1769 | */ | |
1770 | static void *postcopy_ram_listen_thread(void *opaque) | |
1771 | { | |
c76201ab | 1772 | MigrationIncomingState *mis = migration_incoming_get_current(); |
b411b844 | 1773 | QEMUFile *f = mis->from_src_file; |
c76201ab DDAG |
1774 | int load_res; |
1775 | ||
6ba996bb DDAG |
1776 | migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, |
1777 | MIGRATION_STATUS_POSTCOPY_ACTIVE); | |
c76201ab DDAG |
1778 | qemu_sem_post(&mis->listen_thread_sem); |
1779 | trace_postcopy_ram_listen_thread_start(); | |
1780 | ||
74637e6f | 1781 | rcu_register_thread(); |
c76201ab DDAG |
1782 | /* |
1783 | * Because we're a thread and not a coroutine we can't yield | |
1784 | * in qemu_file, and thus we must be blocking now. | |
1785 | */ | |
1786 | qemu_file_set_blocking(f, true); | |
1787 | load_res = qemu_loadvm_state_main(f, mis); | |
b411b844 PX |
1788 | |
1789 | /* | |
1790 | * This is tricky, but, mis->from_src_file can change after it | |
1791 | * returns, when postcopy recovery happened. In the future, we may | |
1792 | * want a wrapper for the QEMUFile handle. | |
1793 | */ | |
1794 | f = mis->from_src_file; | |
1795 | ||
c76201ab DDAG |
1796 | /* And non-blocking again so we don't block in any cleanup */ |
1797 | qemu_file_set_blocking(f, false); | |
1798 | ||
1799 | trace_postcopy_ram_listen_thread_exit(); | |
1800 | if (load_res < 0) { | |
1801 | error_report("%s: loadvm failed: %d", __func__, load_res); | |
1802 | qemu_file_set_error(f, load_res); | |
6ba996bb DDAG |
1803 | migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, |
1804 | MIGRATION_STATUS_FAILED); | |
c76201ab DDAG |
1805 | } else { |
1806 | /* | |
1807 | * This looks good, but it's possible that the device loading in the | |
1808 | * main thread hasn't finished yet, and so we might not be in 'RUN' | |
1809 | * state yet; wait for the end of the main thread. | |
1810 | */ | |
1811 | qemu_event_wait(&mis->main_thread_load_event); | |
1812 | } | |
1813 | postcopy_ram_incoming_cleanup(mis); | |
c76201ab DDAG |
1814 | |
1815 | if (load_res < 0) { | |
1816 | /* | |
1817 | * If something went wrong then we have a bad state so exit; | |
1818 | * depending how far we got it might be possible at this point | |
1819 | * to leave the guest running and fire MCEs for pages that never | |
1820 | * arrived as a desperate recovery step. | |
1821 | */ | |
74637e6f | 1822 | rcu_unregister_thread(); |
c76201ab DDAG |
1823 | exit(EXIT_FAILURE); |
1824 | } | |
1825 | ||
6ba996bb DDAG |
1826 | migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, |
1827 | MIGRATION_STATUS_COMPLETED); | |
1828 | /* | |
1829 | * If everything has worked fine, then the main thread has waited | |
1830 | * for us to start, and we're the last use of the mis. | |
1831 | * (If something broke then qemu will have to exit anyway since it's | |
1832 | * got a bad migration state). | |
1833 | */ | |
1834 | migration_incoming_state_destroy(); | |
acb5ea86 | 1835 | qemu_loadvm_state_cleanup(); |
6ba996bb | 1836 | |
74637e6f | 1837 | rcu_unregister_thread(); |
9cf4bb87 | 1838 | mis->have_listen_thread = false; |
c76201ab DDAG |
1839 | return NULL; |
1840 | } | |
1841 | ||
093e3c42 DDAG |
1842 | /* After this message we must be able to immediately receive postcopy data */ |
1843 | static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis) | |
1844 | { | |
1845 | PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_LISTENING); | |
1846 | trace_loadvm_postcopy_handle_listen(); | |
6864a7b5 DDAG |
1847 | Error *local_err = NULL; |
1848 | ||
093e3c42 DDAG |
1849 | if (ps != POSTCOPY_INCOMING_ADVISE && ps != POSTCOPY_INCOMING_DISCARD) { |
1850 | error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps); | |
1851 | return -1; | |
1852 | } | |
f9527107 DDAG |
1853 | if (ps == POSTCOPY_INCOMING_ADVISE) { |
1854 | /* | |
1855 | * A rare case, we entered listen without having to do any discards, | |
1856 | * so do the setup that's normally done at the time of the 1st discard. | |
1857 | */ | |
58110f0a VSO |
1858 | if (migrate_postcopy_ram()) { |
1859 | postcopy_ram_prepare_discard(mis); | |
1860 | } | |
f9527107 | 1861 | } |
093e3c42 | 1862 | |
f0a227ad DDAG |
1863 | /* |
1864 | * Sensitise RAM - can now generate requests for blocks that don't exist | |
1865 | * However, at this point the CPU shouldn't be running, and the IO | |
1866 | * shouldn't be doing anything yet so don't actually expect requests | |
1867 | */ | |
58110f0a VSO |
1868 | if (migrate_postcopy_ram()) { |
1869 | if (postcopy_ram_enable_notify(mis)) { | |
91b02dc7 | 1870 | postcopy_ram_incoming_cleanup(mis); |
58110f0a VSO |
1871 | return -1; |
1872 | } | |
f0a227ad DDAG |
1873 | } |
1874 | ||
6864a7b5 DDAG |
1875 | if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN, &local_err)) { |
1876 | error_report_err(local_err); | |
1877 | return -1; | |
1878 | } | |
1879 | ||
c76201ab DDAG |
1880 | if (mis->have_listen_thread) { |
1881 | error_report("CMD_POSTCOPY_RAM_LISTEN already has a listen thread"); | |
1882 | return -1; | |
1883 | } | |
1884 | ||
1885 | mis->have_listen_thread = true; | |
1886 | /* Start up the listening thread and wait for it to signal ready */ | |
1887 | qemu_sem_init(&mis->listen_thread_sem, 0); | |
1888 | qemu_thread_create(&mis->listen_thread, "postcopy/listen", | |
b411b844 | 1889 | postcopy_ram_listen_thread, NULL, |
a587a3fe | 1890 | QEMU_THREAD_DETACHED); |
c76201ab DDAG |
1891 | qemu_sem_wait(&mis->listen_thread_sem); |
1892 | qemu_sem_destroy(&mis->listen_thread_sem); | |
1893 | ||
093e3c42 DDAG |
1894 | return 0; |
1895 | } | |
1896 | ||
ea6a55bc | 1897 | static void loadvm_postcopy_handle_run_bh(void *opaque) |
093e3c42 | 1898 | { |
27c6825b | 1899 | Error *local_err = NULL; |
1ce54262 | 1900 | MigrationIncomingState *mis = opaque; |
093e3c42 | 1901 | |
27c6825b DDAG |
1902 | /* TODO we should move all of this lot into postcopy_ram.c or a shared code |
1903 | * in migration.c | |
1904 | */ | |
1905 | cpu_synchronize_all_post_init(); | |
1906 | ||
7659505c | 1907 | qemu_announce_self(&mis->announce_timer, migrate_announce_params()); |
27c6825b | 1908 | |
ace21a58 KW |
1909 | /* Make sure all file formats flush their mutable metadata. |
1910 | * If we get an error here, just don't restart the VM yet. */ | |
27c6825b | 1911 | bdrv_invalidate_cache_all(&local_err); |
0042fd36 | 1912 | if (local_err) { |
ace21a58 | 1913 | error_report_err(local_err); |
0042fd36 KW |
1914 | local_err = NULL; |
1915 | autostart = false; | |
1916 | } | |
1917 | ||
27c6825b | 1918 | trace_loadvm_postcopy_handle_run_cpu_sync(); |
27c6825b DDAG |
1919 | |
1920 | trace_loadvm_postcopy_handle_run_vmstart(); | |
1921 | ||
b35ebdf0 VSO |
1922 | dirty_bitmap_mig_before_vm_start(); |
1923 | ||
093e3c42 DDAG |
1924 | if (autostart) { |
1925 | /* Hold onto your hats, starting the CPU */ | |
1926 | vm_start(); | |
1927 | } else { | |
1928 | /* leave it paused and let management decide when to start the CPU */ | |
1929 | runstate_set(RUN_STATE_PAUSED); | |
1930 | } | |
1931 | ||
1ce54262 | 1932 | qemu_bh_delete(mis->bh); |
ea6a55bc DL |
1933 | } |
1934 | ||
1935 | /* After all discards we can start running and asking for pages */ | |
1936 | static int loadvm_postcopy_handle_run(MigrationIncomingState *mis) | |
1937 | { | |
1938 | PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_RUNNING); | |
1939 | ||
1940 | trace_loadvm_postcopy_handle_run(); | |
1941 | if (ps != POSTCOPY_INCOMING_LISTENING) { | |
1942 | error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps); | |
1943 | return -1; | |
1944 | } | |
1945 | ||
1ce54262 WY |
1946 | mis->bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, mis); |
1947 | qemu_bh_schedule(mis->bh); | |
ea6a55bc | 1948 | |
27c6825b DDAG |
1949 | /* We need to finish reading the stream from the package |
1950 | * and also stop reading anything more from the stream that loaded the | |
1951 | * package (since it's now being read by the listener thread). | |
1952 | * LOADVM_QUIT will quit all the layers of nested loadvm loops. | |
1953 | */ | |
1954 | return LOADVM_QUIT; | |
093e3c42 DDAG |
1955 | } |
1956 | ||
3f5875ec PX |
1957 | static int loadvm_postcopy_handle_resume(MigrationIncomingState *mis) |
1958 | { | |
1959 | if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) { | |
1960 | error_report("%s: illegal resume received", __func__); | |
1961 | /* Don't fail the load, only for this. */ | |
1962 | return 0; | |
1963 | } | |
1964 | ||
1965 | /* | |
1966 | * This means source VM is ready to resume the postcopy migration. | |
1967 | * It's time to switch state and release the fault thread to | |
1968 | * continue service page faults. | |
1969 | */ | |
1970 | migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_RECOVER, | |
1971 | MIGRATION_STATUS_POSTCOPY_ACTIVE); | |
1972 | qemu_sem_post(&mis->postcopy_pause_sem_fault); | |
1973 | ||
1974 | trace_loadvm_postcopy_handle_resume(); | |
1975 | ||
13955b89 PX |
1976 | /* Tell source that "we are ready" */ |
1977 | migrate_send_rp_resume_ack(mis, MIGRATION_RESUME_ACK_VALUE); | |
3f5875ec PX |
1978 | |
1979 | return 0; | |
1980 | } | |
1981 | ||
c76ca188 | 1982 | /** |
11cf1d98 DDAG |
1983 | * Immediately following this command is a blob of data containing an embedded |
1984 | * chunk of migration stream; read it and load it. | |
1985 | * | |
1986 | * @mis: Incoming state | |
1987 | * @length: Length of packaged data to read | |
c76ca188 | 1988 | * |
11cf1d98 DDAG |
1989 | * Returns: Negative values on error |
1990 | * | |
1991 | */ | |
1992 | static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis) | |
1993 | { | |
1994 | int ret; | |
61b67d47 DB |
1995 | size_t length; |
1996 | QIOChannelBuffer *bioc; | |
11cf1d98 DDAG |
1997 | |
1998 | length = qemu_get_be32(mis->from_src_file); | |
1999 | trace_loadvm_handle_cmd_packaged(length); | |
2000 | ||
2001 | if (length > MAX_VM_CMD_PACKAGED_SIZE) { | |
61b67d47 | 2002 | error_report("Unreasonably large packaged state: %zu", length); |
11cf1d98 DDAG |
2003 | return -1; |
2004 | } | |
61b67d47 DB |
2005 | |
2006 | bioc = qio_channel_buffer_new(length); | |
6f01f136 | 2007 | qio_channel_set_name(QIO_CHANNEL(bioc), "migration-loadvm-buffer"); |
61b67d47 DB |
2008 | ret = qemu_get_buffer(mis->from_src_file, |
2009 | bioc->data, | |
2010 | length); | |
11cf1d98 | 2011 | if (ret != length) { |
61b67d47 DB |
2012 | object_unref(OBJECT(bioc)); |
2013 | error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu", | |
9af9e0fe | 2014 | ret, length); |
11cf1d98 DDAG |
2015 | return (ret < 0) ? ret : -EAGAIN; |
2016 | } | |
61b67d47 | 2017 | bioc->usage += length; |
11cf1d98 DDAG |
2018 | trace_loadvm_handle_cmd_packaged_received(ret); |
2019 | ||
61b67d47 | 2020 | QEMUFile *packf = qemu_fopen_channel_input(QIO_CHANNEL(bioc)); |
11cf1d98 DDAG |
2021 | |
2022 | ret = qemu_loadvm_state_main(packf, mis); | |
2023 | trace_loadvm_handle_cmd_packaged_main(ret); | |
2024 | qemu_fclose(packf); | |
61b67d47 | 2025 | object_unref(OBJECT(bioc)); |
11cf1d98 DDAG |
2026 | |
2027 | return ret; | |
2028 | } | |
2029 | ||
f25d4225 PX |
2030 | /* |
2031 | * Handle request that source requests for recved_bitmap on | |
2032 | * destination. Payload format: | |
2033 | * | |
2034 | * len (1 byte) + ramblock_name (<255 bytes) | |
2035 | */ | |
2036 | static int loadvm_handle_recv_bitmap(MigrationIncomingState *mis, | |
2037 | uint16_t len) | |
2038 | { | |
2039 | QEMUFile *file = mis->from_src_file; | |
2040 | RAMBlock *rb; | |
2041 | char block_name[256]; | |
2042 | size_t cnt; | |
2043 | ||
2044 | cnt = qemu_get_counted_string(file, block_name); | |
2045 | if (!cnt) { | |
2046 | error_report("%s: failed to read block name", __func__); | |
2047 | return -EINVAL; | |
2048 | } | |
2049 | ||
2050 | /* Validate before using the data */ | |
2051 | if (qemu_file_get_error(file)) { | |
2052 | return qemu_file_get_error(file); | |
2053 | } | |
2054 | ||
2055 | if (len != cnt + 1) { | |
2056 | error_report("%s: invalid payload length (%d)", __func__, len); | |
2057 | return -EINVAL; | |
2058 | } | |
2059 | ||
2060 | rb = qemu_ram_block_by_name(block_name); | |
2061 | if (!rb) { | |
2062 | error_report("%s: block '%s' not found", __func__, block_name); | |
2063 | return -EINVAL; | |
2064 | } | |
2065 | ||
a335debb | 2066 | migrate_send_rp_recv_bitmap(mis, block_name); |
f25d4225 PX |
2067 | |
2068 | trace_loadvm_handle_recv_bitmap(block_name); | |
2069 | ||
2070 | return 0; | |
2071 | } | |
2072 | ||
aad555c2 ZC |
2073 | static int loadvm_process_enable_colo(MigrationIncomingState *mis) |
2074 | { | |
2075 | migration_incoming_enable_colo(); | |
13af18f2 | 2076 | return colo_init_ram_cache(); |
aad555c2 ZC |
2077 | } |
2078 | ||
11cf1d98 DDAG |
2079 | /* |
2080 | * Process an incoming 'QEMU_VM_COMMAND' | |
2081 | * 0 just a normal return | |
2082 | * LOADVM_QUIT All good, but exit the loop | |
2083 | * <0 Error | |
c76ca188 DDAG |
2084 | */ |
2085 | static int loadvm_process_command(QEMUFile *f) | |
2086 | { | |
2e37701e | 2087 | MigrationIncomingState *mis = migration_incoming_get_current(); |
c76ca188 DDAG |
2088 | uint16_t cmd; |
2089 | uint16_t len; | |
2e37701e | 2090 | uint32_t tmp32; |
c76ca188 DDAG |
2091 | |
2092 | cmd = qemu_get_be16(f); | |
2093 | len = qemu_get_be16(f); | |
2094 | ||
7a9ddfbf PX |
2095 | /* Check validity before continue processing of cmds */ |
2096 | if (qemu_file_get_error(f)) { | |
2097 | return qemu_file_get_error(f); | |
2098 | } | |
2099 | ||
c76ca188 DDAG |
2100 | trace_loadvm_process_command(cmd, len); |
2101 | if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) { | |
2102 | error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len); | |
2103 | return -EINVAL; | |
2104 | } | |
2105 | ||
2106 | if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) { | |
2107 | error_report("%s received with bad length - expecting %zu, got %d", | |
2108 | mig_cmd_args[cmd].name, | |
2109 | (size_t)mig_cmd_args[cmd].len, len); | |
2110 | return -ERANGE; | |
2111 | } | |
2112 | ||
2113 | switch (cmd) { | |
2e37701e DDAG |
2114 | case MIG_CMD_OPEN_RETURN_PATH: |
2115 | if (mis->to_src_file) { | |
2116 | error_report("CMD_OPEN_RETURN_PATH called when RP already open"); | |
2117 | /* Not really a problem, so don't give up */ | |
2118 | return 0; | |
2119 | } | |
2120 | mis->to_src_file = qemu_file_get_return_path(f); | |
2121 | if (!mis->to_src_file) { | |
2122 | error_report("CMD_OPEN_RETURN_PATH failed"); | |
2123 | return -1; | |
2124 | } | |
2125 | break; | |
2126 | ||
2127 | case MIG_CMD_PING: | |
2128 | tmp32 = qemu_get_be32(f); | |
2129 | trace_loadvm_process_command_ping(tmp32); | |
2130 | if (!mis->to_src_file) { | |
2131 | error_report("CMD_PING (0x%x) received with no return path", | |
2132 | tmp32); | |
2133 | return -1; | |
2134 | } | |
6decec93 | 2135 | migrate_send_rp_pong(mis, tmp32); |
2e37701e | 2136 | break; |
093e3c42 | 2137 | |
11cf1d98 DDAG |
2138 | case MIG_CMD_PACKAGED: |
2139 | return loadvm_handle_cmd_packaged(mis); | |
2140 | ||
093e3c42 | 2141 | case MIG_CMD_POSTCOPY_ADVISE: |
875fcd01 | 2142 | return loadvm_postcopy_handle_advise(mis, len); |
093e3c42 DDAG |
2143 | |
2144 | case MIG_CMD_POSTCOPY_LISTEN: | |
2145 | return loadvm_postcopy_handle_listen(mis); | |
2146 | ||
2147 | case MIG_CMD_POSTCOPY_RUN: | |
2148 | return loadvm_postcopy_handle_run(mis); | |
2149 | ||
2150 | case MIG_CMD_POSTCOPY_RAM_DISCARD: | |
2151 | return loadvm_postcopy_ram_handle_discard(mis, len); | |
f25d4225 | 2152 | |
3f5875ec PX |
2153 | case MIG_CMD_POSTCOPY_RESUME: |
2154 | return loadvm_postcopy_handle_resume(mis); | |
2155 | ||
f25d4225 PX |
2156 | case MIG_CMD_RECV_BITMAP: |
2157 | return loadvm_handle_recv_bitmap(mis, len); | |
aad555c2 ZC |
2158 | |
2159 | case MIG_CMD_ENABLE_COLO: | |
2160 | return loadvm_process_enable_colo(mis); | |
c76ca188 DDAG |
2161 | } |
2162 | ||
2163 | return 0; | |
2164 | } | |
2165 | ||
59f39a47 DDAG |
2166 | /* |
2167 | * Read a footer off the wire and check that it matches the expected section | |
2168 | * | |
2169 | * Returns: true if the footer was good | |
2170 | * false if there is a problem (and calls error_report to say why) | |
2171 | */ | |
0f42f657 | 2172 | static bool check_section_footer(QEMUFile *f, SaveStateEntry *se) |
59f39a47 | 2173 | { |
7a9ddfbf | 2174 | int ret; |
59f39a47 DDAG |
2175 | uint8_t read_mark; |
2176 | uint32_t read_section_id; | |
2177 | ||
15c38503 | 2178 | if (!migrate_get_current()->send_section_footer) { |
59f39a47 DDAG |
2179 | /* No footer to check */ |
2180 | return true; | |
2181 | } | |
2182 | ||
2183 | read_mark = qemu_get_byte(f); | |
2184 | ||
7a9ddfbf PX |
2185 | ret = qemu_file_get_error(f); |
2186 | if (ret) { | |
2187 | error_report("%s: Read section footer failed: %d", | |
2188 | __func__, ret); | |
2189 | return false; | |
2190 | } | |
2191 | ||
59f39a47 | 2192 | if (read_mark != QEMU_VM_SECTION_FOOTER) { |
0f42f657 | 2193 | error_report("Missing section footer for %s", se->idstr); |
59f39a47 DDAG |
2194 | return false; |
2195 | } | |
2196 | ||
2197 | read_section_id = qemu_get_be32(f); | |
0f42f657 | 2198 | if (read_section_id != se->load_section_id) { |
59f39a47 DDAG |
2199 | error_report("Mismatched section id in footer for %s -" |
2200 | " read 0x%x expected 0x%x", | |
0f42f657 | 2201 | se->idstr, read_section_id, se->load_section_id); |
59f39a47 DDAG |
2202 | return false; |
2203 | } | |
2204 | ||
2205 | /* All good */ | |
2206 | return true; | |
2207 | } | |
2208 | ||
fb3520a8 HZ |
2209 | static int |
2210 | qemu_loadvm_section_start_full(QEMUFile *f, MigrationIncomingState *mis) | |
2211 | { | |
2212 | uint32_t instance_id, version_id, section_id; | |
2213 | SaveStateEntry *se; | |
fb3520a8 HZ |
2214 | char idstr[256]; |
2215 | int ret; | |
2216 | ||
2217 | /* Read section start */ | |
2218 | section_id = qemu_get_be32(f); | |
2219 | if (!qemu_get_counted_string(f, idstr)) { | |
2220 | error_report("Unable to read ID string for section %u", | |
2221 | section_id); | |
2222 | return -EINVAL; | |
2223 | } | |
2224 | instance_id = qemu_get_be32(f); | |
2225 | version_id = qemu_get_be32(f); | |
2226 | ||
7a9ddfbf PX |
2227 | ret = qemu_file_get_error(f); |
2228 | if (ret) { | |
2229 | error_report("%s: Failed to read instance/version ID: %d", | |
2230 | __func__, ret); | |
2231 | return ret; | |
2232 | } | |
2233 | ||
fb3520a8 HZ |
2234 | trace_qemu_loadvm_state_section_startfull(section_id, idstr, |
2235 | instance_id, version_id); | |
2236 | /* Find savevm section */ | |
2237 | se = find_se(idstr, instance_id); | |
2238 | if (se == NULL) { | |
827beacb JRZ |
2239 | error_report("Unknown savevm section or instance '%s' %d. " |
2240 | "Make sure that your current VM setup matches your " | |
2241 | "saved VM setup, including any hotplugged devices", | |
fb3520a8 HZ |
2242 | idstr, instance_id); |
2243 | return -EINVAL; | |
2244 | } | |
2245 | ||
2246 | /* Validate version */ | |
2247 | if (version_id > se->version_id) { | |
2248 | error_report("savevm: unsupported version %d for '%s' v%d", | |
2249 | version_id, idstr, se->version_id); | |
2250 | return -EINVAL; | |
2251 | } | |
0f42f657 JQ |
2252 | se->load_version_id = version_id; |
2253 | se->load_section_id = section_id; | |
fb3520a8 | 2254 | |
88c16567 WC |
2255 | /* Validate if it is a device's state */ |
2256 | if (xen_enabled() && se->is_ram) { | |
2257 | error_report("loadvm: %s RAM loading not allowed on Xen", idstr); | |
2258 | return -EINVAL; | |
2259 | } | |
2260 | ||
3a011c26 | 2261 | ret = vmstate_load(f, se); |
fb3520a8 HZ |
2262 | if (ret < 0) { |
2263 | error_report("error while loading state for instance 0x%x of" | |
2264 | " device '%s'", instance_id, idstr); | |
2265 | return ret; | |
2266 | } | |
0f42f657 | 2267 | if (!check_section_footer(f, se)) { |
fb3520a8 HZ |
2268 | return -EINVAL; |
2269 | } | |
2270 | ||
2271 | return 0; | |
2272 | } | |
2273 | ||
2274 | static int | |
2275 | qemu_loadvm_section_part_end(QEMUFile *f, MigrationIncomingState *mis) | |
2276 | { | |
2277 | uint32_t section_id; | |
0f42f657 | 2278 | SaveStateEntry *se; |
fb3520a8 HZ |
2279 | int ret; |
2280 | ||
2281 | section_id = qemu_get_be32(f); | |
2282 | ||
7a9ddfbf PX |
2283 | ret = qemu_file_get_error(f); |
2284 | if (ret) { | |
2285 | error_report("%s: Failed to read section ID: %d", | |
2286 | __func__, ret); | |
2287 | return ret; | |
2288 | } | |
2289 | ||
fb3520a8 | 2290 | trace_qemu_loadvm_state_section_partend(section_id); |
0f42f657 JQ |
2291 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
2292 | if (se->load_section_id == section_id) { | |
fb3520a8 HZ |
2293 | break; |
2294 | } | |
2295 | } | |
0f42f657 | 2296 | if (se == NULL) { |
fb3520a8 HZ |
2297 | error_report("Unknown savevm section %d", section_id); |
2298 | return -EINVAL; | |
2299 | } | |
2300 | ||
3a011c26 | 2301 | ret = vmstate_load(f, se); |
fb3520a8 HZ |
2302 | if (ret < 0) { |
2303 | error_report("error while loading state section id %d(%s)", | |
0f42f657 | 2304 | section_id, se->idstr); |
fb3520a8 HZ |
2305 | return ret; |
2306 | } | |
0f42f657 | 2307 | if (!check_section_footer(f, se)) { |
fb3520a8 HZ |
2308 | return -EINVAL; |
2309 | } | |
2310 | ||
2311 | return 0; | |
2312 | } | |
2313 | ||
16015d32 WY |
2314 | static int qemu_loadvm_state_header(QEMUFile *f) |
2315 | { | |
2316 | unsigned int v; | |
2317 | int ret; | |
2318 | ||
2319 | v = qemu_get_be32(f); | |
2320 | if (v != QEMU_VM_FILE_MAGIC) { | |
2321 | error_report("Not a migration stream"); | |
2322 | return -EINVAL; | |
2323 | } | |
2324 | ||
2325 | v = qemu_get_be32(f); | |
2326 | if (v == QEMU_VM_FILE_VERSION_COMPAT) { | |
2327 | error_report("SaveVM v2 format is obsolete and don't work anymore"); | |
2328 | return -ENOTSUP; | |
2329 | } | |
2330 | if (v != QEMU_VM_FILE_VERSION) { | |
2331 | error_report("Unsupported migration stream version"); | |
2332 | return -ENOTSUP; | |
2333 | } | |
2334 | ||
2335 | if (migrate_get_current()->send_configuration) { | |
2336 | if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) { | |
2337 | error_report("Configuration section missing"); | |
2338 | qemu_loadvm_state_cleanup(); | |
2339 | return -EINVAL; | |
2340 | } | |
2341 | ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0); | |
2342 | ||
2343 | if (ret) { | |
2344 | qemu_loadvm_state_cleanup(); | |
2345 | return ret; | |
2346 | } | |
2347 | } | |
2348 | return 0; | |
2349 | } | |
2350 | ||
acb5ea86 JQ |
2351 | static int qemu_loadvm_state_setup(QEMUFile *f) |
2352 | { | |
2353 | SaveStateEntry *se; | |
2354 | int ret; | |
2355 | ||
2356 | trace_loadvm_state_setup(); | |
2357 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { | |
2358 | if (!se->ops || !se->ops->load_setup) { | |
2359 | continue; | |
2360 | } | |
cea3b4c0 | 2361 | if (se->ops->is_active) { |
acb5ea86 JQ |
2362 | if (!se->ops->is_active(se->opaque)) { |
2363 | continue; | |
2364 | } | |
2365 | } | |
2366 | ||
2367 | ret = se->ops->load_setup(f, se->opaque); | |
2368 | if (ret < 0) { | |
2369 | qemu_file_set_error(f, ret); | |
2370 | error_report("Load state of device %s failed", se->idstr); | |
2371 | return ret; | |
2372 | } | |
2373 | } | |
2374 | return 0; | |
2375 | } | |
2376 | ||
2377 | void qemu_loadvm_state_cleanup(void) | |
2378 | { | |
2379 | SaveStateEntry *se; | |
2380 | ||
2381 | trace_loadvm_state_cleanup(); | |
2382 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { | |
2383 | if (se->ops && se->ops->load_cleanup) { | |
2384 | se->ops->load_cleanup(se->opaque); | |
2385 | } | |
2386 | } | |
2387 | } | |
2388 | ||
b411b844 PX |
2389 | /* Return true if we should continue the migration, or false. */ |
2390 | static bool postcopy_pause_incoming(MigrationIncomingState *mis) | |
2391 | { | |
2392 | trace_postcopy_pause_incoming(); | |
2393 | ||
02affd41 PX |
2394 | /* Clear the triggered bit to allow one recovery */ |
2395 | mis->postcopy_recover_triggered = false; | |
2396 | ||
b411b844 PX |
2397 | assert(mis->from_src_file); |
2398 | qemu_file_shutdown(mis->from_src_file); | |
2399 | qemu_fclose(mis->from_src_file); | |
2400 | mis->from_src_file = NULL; | |
2401 | ||
2402 | assert(mis->to_src_file); | |
2403 | qemu_file_shutdown(mis->to_src_file); | |
2404 | qemu_mutex_lock(&mis->rp_mutex); | |
2405 | qemu_fclose(mis->to_src_file); | |
2406 | mis->to_src_file = NULL; | |
2407 | qemu_mutex_unlock(&mis->rp_mutex); | |
2408 | ||
eed1cc78 PX |
2409 | migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, |
2410 | MIGRATION_STATUS_POSTCOPY_PAUSED); | |
2411 | ||
3a7804c3 PX |
2412 | /* Notify the fault thread for the invalidated file handle */ |
2413 | postcopy_fault_thread_notify(mis); | |
2414 | ||
b411b844 PX |
2415 | error_report("Detected IO failure for postcopy. " |
2416 | "Migration paused."); | |
2417 | ||
2418 | while (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) { | |
2419 | qemu_sem_wait(&mis->postcopy_pause_sem_dst); | |
2420 | } | |
2421 | ||
2422 | trace_postcopy_pause_incoming_continued(); | |
2423 | ||
2424 | return true; | |
2425 | } | |
2426 | ||
3f6df99d | 2427 | int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis) |
1a8f46f8 | 2428 | { |
a672b469 | 2429 | uint8_t section_type; |
ccb783c3 | 2430 | int ret = 0; |
61964c23 | 2431 | |
b411b844 | 2432 | retry: |
7a9ddfbf PX |
2433 | while (true) { |
2434 | section_type = qemu_get_byte(f); | |
2435 | ||
2436 | if (qemu_file_get_error(f)) { | |
2437 | ret = qemu_file_get_error(f); | |
2438 | break; | |
2439 | } | |
2440 | ||
a5df2a02 | 2441 | trace_qemu_loadvm_state_section(section_type); |
a672b469 AL |
2442 | switch (section_type) { |
2443 | case QEMU_VM_SECTION_START: | |
2444 | case QEMU_VM_SECTION_FULL: | |
fb3520a8 | 2445 | ret = qemu_loadvm_section_start_full(f, mis); |
b5a22e4a | 2446 | if (ret < 0) { |
ccb783c3 | 2447 | goto out; |
b5a22e4a | 2448 | } |
a672b469 AL |
2449 | break; |
2450 | case QEMU_VM_SECTION_PART: | |
2451 | case QEMU_VM_SECTION_END: | |
fb3520a8 | 2452 | ret = qemu_loadvm_section_part_end(f, mis); |
b5a22e4a | 2453 | if (ret < 0) { |
ccb783c3 | 2454 | goto out; |
b5a22e4a | 2455 | } |
a672b469 | 2456 | break; |
c76ca188 DDAG |
2457 | case QEMU_VM_COMMAND: |
2458 | ret = loadvm_process_command(f); | |
7b89bf27 | 2459 | trace_qemu_loadvm_state_section_command(ret); |
4695ce3f | 2460 | if ((ret < 0) || (ret == LOADVM_QUIT)) { |
ccb783c3 | 2461 | goto out; |
c76ca188 DDAG |
2462 | } |
2463 | break; | |
7a9ddfbf PX |
2464 | case QEMU_VM_EOF: |
2465 | /* This is the end of migration */ | |
2466 | goto out; | |
a672b469 | 2467 | default: |
6a64b644 | 2468 | error_report("Unknown savevm section type %d", section_type); |
ccb783c3 DDAG |
2469 | ret = -EINVAL; |
2470 | goto out; | |
a672b469 AL |
2471 | } |
2472 | } | |
2473 | ||
ccb783c3 DDAG |
2474 | out: |
2475 | if (ret < 0) { | |
2476 | qemu_file_set_error(f, ret); | |
b411b844 PX |
2477 | |
2478 | /* | |
fd037a65 PX |
2479 | * If we are during an active postcopy, then we pause instead |
2480 | * of bail out to at least keep the VM's dirty data. Note | |
2481 | * that POSTCOPY_INCOMING_LISTENING stage is still not enough, | |
2482 | * during which we're still receiving device states and we | |
2483 | * still haven't yet started the VM on destination. | |
b411b844 PX |
2484 | */ |
2485 | if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING && | |
fd037a65 | 2486 | postcopy_pause_incoming(mis)) { |
b411b844 PX |
2487 | /* Reset f to point to the newly created channel */ |
2488 | f = mis->from_src_file; | |
2489 | goto retry; | |
2490 | } | |
ccb783c3 DDAG |
2491 | } |
2492 | return ret; | |
7b89bf27 DDAG |
2493 | } |
2494 | ||
2495 | int qemu_loadvm_state(QEMUFile *f) | |
2496 | { | |
2497 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
2498 | Error *local_err = NULL; | |
7b89bf27 DDAG |
2499 | int ret; |
2500 | ||
2501 | if (qemu_savevm_state_blocked(&local_err)) { | |
2502 | error_report_err(local_err); | |
2503 | return -EINVAL; | |
2504 | } | |
2505 | ||
16015d32 WY |
2506 | ret = qemu_loadvm_state_header(f); |
2507 | if (ret) { | |
2508 | return ret; | |
7b89bf27 DDAG |
2509 | } |
2510 | ||
9e14b849 WY |
2511 | if (qemu_loadvm_state_setup(f) != 0) { |
2512 | return -EINVAL; | |
2513 | } | |
2514 | ||
75e972da DG |
2515 | cpu_synchronize_all_pre_loadvm(); |
2516 | ||
7b89bf27 DDAG |
2517 | ret = qemu_loadvm_state_main(f, mis); |
2518 | qemu_event_set(&mis->main_thread_load_event); | |
2519 | ||
2520 | trace_qemu_loadvm_state_post_main(ret); | |
2521 | ||
c76201ab DDAG |
2522 | if (mis->have_listen_thread) { |
2523 | /* Listen thread still going, can't clean up yet */ | |
2524 | return ret; | |
2525 | } | |
2526 | ||
7b89bf27 DDAG |
2527 | if (ret == 0) { |
2528 | ret = qemu_file_get_error(f); | |
2529 | } | |
1925cebc AG |
2530 | |
2531 | /* | |
2532 | * Try to read in the VMDESC section as well, so that dumping tools that | |
2533 | * intercept our migration stream have the chance to see it. | |
2534 | */ | |
1aca9a5f DDAG |
2535 | |
2536 | /* We've got to be careful; if we don't read the data and just shut the fd | |
2537 | * then the sender can error if we close while it's still sending. | |
2538 | * We also mustn't read data that isn't there; some transports (RDMA) | |
2539 | * will stall waiting for that data when the source has already closed. | |
2540 | */ | |
7b89bf27 | 2541 | if (ret == 0 && should_send_vmdesc()) { |
1aca9a5f DDAG |
2542 | uint8_t *buf; |
2543 | uint32_t size; | |
7b89bf27 | 2544 | uint8_t section_type = qemu_get_byte(f); |
1aca9a5f DDAG |
2545 | |
2546 | if (section_type != QEMU_VM_VMDESCRIPTION) { | |
2547 | error_report("Expected vmdescription section, but got %d", | |
2548 | section_type); | |
2549 | /* | |
2550 | * It doesn't seem worth failing at this point since | |
2551 | * we apparently have an otherwise valid VM state | |
2552 | */ | |
2553 | } else { | |
2554 | buf = g_malloc(0x1000); | |
2555 | size = qemu_get_be32(f); | |
2556 | ||
2557 | while (size > 0) { | |
2558 | uint32_t read_chunk = MIN(size, 0x1000); | |
2559 | qemu_get_buffer(f, buf, read_chunk); | |
2560 | size -= read_chunk; | |
2561 | } | |
2562 | g_free(buf); | |
1925cebc | 2563 | } |
1925cebc AG |
2564 | } |
2565 | ||
acb5ea86 | 2566 | qemu_loadvm_state_cleanup(); |
ea375f9a JK |
2567 | cpu_synchronize_all_post_init(); |
2568 | ||
a672b469 AL |
2569 | return ret; |
2570 | } | |
2571 | ||
3f6df99d ZC |
2572 | int qemu_load_device_state(QEMUFile *f) |
2573 | { | |
2574 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
2575 | int ret; | |
2576 | ||
2577 | /* Load QEMU_VM_SECTION_FULL section */ | |
2578 | ret = qemu_loadvm_state_main(f, mis); | |
2579 | if (ret < 0) { | |
2580 | error_report("Failed to load device state: %d", ret); | |
2581 | return ret; | |
2582 | } | |
2583 | ||
2584 | cpu_synchronize_all_post_init(); | |
2585 | return 0; | |
2586 | } | |
2587 | ||
5e22479a | 2588 | int save_snapshot(const char *name, Error **errp) |
a672b469 AL |
2589 | { |
2590 | BlockDriverState *bs, *bs1; | |
2591 | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; | |
ac8c19ba | 2592 | int ret = -1; |
a672b469 AL |
2593 | QEMUFile *f; |
2594 | int saved_vm_running; | |
c2c9a466 | 2595 | uint64_t vm_state_size; |
68b891ec | 2596 | qemu_timeval tv; |
7d631a11 | 2597 | struct tm tm; |
79b3c12a | 2598 | AioContext *aio_context; |
a672b469 | 2599 | |
5aaac467 | 2600 | if (migration_is_blocked(errp)) { |
aded9dfa | 2601 | return ret; |
5aaac467 PB |
2602 | } |
2603 | ||
377b21cc | 2604 | if (!replay_can_snapshot()) { |
4dd32b3d MA |
2605 | error_setg(errp, "Record/replay does not allow making snapshot " |
2606 | "right now. Try once more later."); | |
377b21cc PD |
2607 | return ret; |
2608 | } | |
2609 | ||
e9ff957a | 2610 | if (!bdrv_all_can_snapshot(&bs)) { |
927d6638 JQ |
2611 | error_setg(errp, "Device '%s' is writable but does not support " |
2612 | "snapshots", bdrv_get_device_name(bs)); | |
ac8c19ba | 2613 | return ret; |
feeee5ac MDCF |
2614 | } |
2615 | ||
0b461605 | 2616 | /* Delete old snapshots of the same name */ |
ac8c19ba | 2617 | if (name) { |
927d6638 | 2618 | ret = bdrv_all_delete_snapshot(name, &bs1, errp); |
ac8c19ba | 2619 | if (ret < 0) { |
927d6638 JQ |
2620 | error_prepend(errp, "Error while deleting snapshot on device " |
2621 | "'%s': ", bdrv_get_device_name(bs1)); | |
ac8c19ba PD |
2622 | return ret; |
2623 | } | |
0b461605 DL |
2624 | } |
2625 | ||
7cb14481 DL |
2626 | bs = bdrv_all_find_vmstate_bs(); |
2627 | if (bs == NULL) { | |
927d6638 | 2628 | error_setg(errp, "No block device can accept snapshots"); |
ac8c19ba | 2629 | return ret; |
a672b469 | 2630 | } |
79b3c12a | 2631 | aio_context = bdrv_get_aio_context(bs); |
a672b469 | 2632 | |
1354869c | 2633 | saved_vm_running = runstate_is_running(); |
560d027b JQ |
2634 | |
2635 | ret = global_state_store(); | |
2636 | if (ret) { | |
927d6638 | 2637 | error_setg(errp, "Error saving global state"); |
ac8c19ba | 2638 | return ret; |
560d027b | 2639 | } |
0461d5a6 | 2640 | vm_stop(RUN_STATE_SAVE_VM); |
a672b469 | 2641 | |
8649f2f9 SH |
2642 | bdrv_drain_all_begin(); |
2643 | ||
79b3c12a DL |
2644 | aio_context_acquire(aio_context); |
2645 | ||
cb499fb2 | 2646 | memset(sn, 0, sizeof(*sn)); |
a672b469 AL |
2647 | |
2648 | /* fill auxiliary fields */ | |
68b891ec | 2649 | qemu_gettimeofday(&tv); |
a672b469 AL |
2650 | sn->date_sec = tv.tv_sec; |
2651 | sn->date_nsec = tv.tv_usec * 1000; | |
bc72ad67 | 2652 | sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
a672b469 | 2653 | |
7d631a11 MDCF |
2654 | if (name) { |
2655 | ret = bdrv_snapshot_find(bs, old_sn, name); | |
2656 | if (ret >= 0) { | |
2657 | pstrcpy(sn->name, sizeof(sn->name), old_sn->name); | |
2658 | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str); | |
2659 | } else { | |
2660 | pstrcpy(sn->name, sizeof(sn->name), name); | |
2661 | } | |
2662 | } else { | |
d7d9b528 BS |
2663 | /* cast below needed for OpenBSD where tv_sec is still 'long' */ |
2664 | localtime_r((const time_t *)&tv.tv_sec, &tm); | |
7d631a11 | 2665 | strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm); |
7d631a11 MDCF |
2666 | } |
2667 | ||
a672b469 | 2668 | /* save the VM state */ |
45566e9c | 2669 | f = qemu_fopen_bdrv(bs, 1); |
a672b469 | 2670 | if (!f) { |
927d6638 | 2671 | error_setg(errp, "Could not open VM state file"); |
a672b469 AL |
2672 | goto the_end; |
2673 | } | |
927d6638 | 2674 | ret = qemu_savevm_state(f, errp); |
2d22b18f | 2675 | vm_state_size = qemu_ftell(f); |
a672b469 AL |
2676 | qemu_fclose(f); |
2677 | if (ret < 0) { | |
a672b469 AL |
2678 | goto the_end; |
2679 | } | |
2680 | ||
17e2a4a4 SH |
2681 | /* The bdrv_all_create_snapshot() call that follows acquires the AioContext |
2682 | * for itself. BDRV_POLL_WHILE() does not support nested locking because | |
2683 | * it only releases the lock once. Therefore synchronous I/O will deadlock | |
2684 | * unless we release the AioContext before bdrv_all_create_snapshot(). | |
2685 | */ | |
2686 | aio_context_release(aio_context); | |
2687 | aio_context = NULL; | |
2688 | ||
a9085f9b DL |
2689 | ret = bdrv_all_create_snapshot(sn, bs, vm_state_size, &bs); |
2690 | if (ret < 0) { | |
927d6638 JQ |
2691 | error_setg(errp, "Error while creating snapshot on '%s'", |
2692 | bdrv_get_device_name(bs)); | |
ac8c19ba | 2693 | goto the_end; |
a672b469 AL |
2694 | } |
2695 | ||
ac8c19ba PD |
2696 | ret = 0; |
2697 | ||
a672b469 | 2698 | the_end: |
17e2a4a4 SH |
2699 | if (aio_context) { |
2700 | aio_context_release(aio_context); | |
2701 | } | |
8649f2f9 SH |
2702 | |
2703 | bdrv_drain_all_end(); | |
2704 | ||
38ff78d3 | 2705 | if (saved_vm_running) { |
a672b469 | 2706 | vm_start(); |
38ff78d3 | 2707 | } |
ac8c19ba PD |
2708 | return ret; |
2709 | } | |
2710 | ||
5d6c599f AP |
2711 | void qmp_xen_save_devices_state(const char *filename, bool has_live, bool live, |
2712 | Error **errp) | |
a7ae8355 SS |
2713 | { |
2714 | QEMUFile *f; | |
8925839f | 2715 | QIOChannelFile *ioc; |
a7ae8355 SS |
2716 | int saved_vm_running; |
2717 | int ret; | |
2718 | ||
5d6c599f AP |
2719 | if (!has_live) { |
2720 | /* live default to true so old version of Xen tool stack can have a | |
2721 | * successfull live migration */ | |
2722 | live = true; | |
2723 | } | |
2724 | ||
a7ae8355 SS |
2725 | saved_vm_running = runstate_is_running(); |
2726 | vm_stop(RUN_STATE_SAVE_VM); | |
c69adea4 | 2727 | global_state_store_running(); |
a7ae8355 | 2728 | |
8925839f DB |
2729 | ioc = qio_channel_file_new_path(filename, O_WRONLY | O_CREAT, 0660, errp); |
2730 | if (!ioc) { | |
a7ae8355 SS |
2731 | goto the_end; |
2732 | } | |
6f01f136 | 2733 | qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state"); |
8925839f | 2734 | f = qemu_fopen_channel_output(QIO_CHANNEL(ioc)); |
032b79f7 | 2735 | object_unref(OBJECT(ioc)); |
a7ae8355 | 2736 | ret = qemu_save_device_state(f); |
96994fd1 | 2737 | if (ret < 0 || qemu_fclose(f) < 0) { |
c6bd8c70 | 2738 | error_setg(errp, QERR_IO_ERROR); |
5d6c599f AP |
2739 | } else { |
2740 | /* libxl calls the QMP command "stop" before calling | |
2741 | * "xen-save-devices-state" and in case of migration failure, libxl | |
2742 | * would call "cont". | |
2743 | * So call bdrv_inactivate_all (release locks) here to let the other | |
2744 | * side of the migration take controle of the images. | |
2745 | */ | |
2746 | if (live && !saved_vm_running) { | |
2747 | ret = bdrv_inactivate_all(); | |
2748 | if (ret) { | |
2749 | error_setg(errp, "%s: bdrv_inactivate_all() failed (%d)", | |
2750 | __func__, ret); | |
2751 | } | |
2752 | } | |
a7ae8355 SS |
2753 | } |
2754 | ||
2755 | the_end: | |
38ff78d3 | 2756 | if (saved_vm_running) { |
a7ae8355 | 2757 | vm_start(); |
38ff78d3 | 2758 | } |
a7ae8355 SS |
2759 | } |
2760 | ||
88c16567 WC |
2761 | void qmp_xen_load_devices_state(const char *filename, Error **errp) |
2762 | { | |
2763 | QEMUFile *f; | |
2764 | QIOChannelFile *ioc; | |
2765 | int ret; | |
2766 | ||
2767 | /* Guest must be paused before loading the device state; the RAM state | |
2768 | * will already have been loaded by xc | |
2769 | */ | |
2770 | if (runstate_is_running()) { | |
2771 | error_setg(errp, "Cannot update device state while vm is running"); | |
2772 | return; | |
2773 | } | |
2774 | vm_stop(RUN_STATE_RESTORE_VM); | |
2775 | ||
2776 | ioc = qio_channel_file_new_path(filename, O_RDONLY | O_BINARY, 0, errp); | |
2777 | if (!ioc) { | |
2778 | return; | |
2779 | } | |
6f01f136 | 2780 | qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state"); |
88c16567 | 2781 | f = qemu_fopen_channel_input(QIO_CHANNEL(ioc)); |
032b79f7 | 2782 | object_unref(OBJECT(ioc)); |
88c16567 | 2783 | |
88c16567 WC |
2784 | ret = qemu_loadvm_state(f); |
2785 | qemu_fclose(f); | |
2786 | if (ret < 0) { | |
2787 | error_setg(errp, QERR_IO_ERROR); | |
2788 | } | |
2789 | migration_incoming_state_destroy(); | |
2790 | } | |
2791 | ||
5e22479a | 2792 | int load_snapshot(const char *name, Error **errp) |
a672b469 | 2793 | { |
f0aa7a8b | 2794 | BlockDriverState *bs, *bs_vm_state; |
2d22b18f | 2795 | QEMUSnapshotInfo sn; |
a672b469 | 2796 | QEMUFile *f; |
751c6a17 | 2797 | int ret; |
79b3c12a | 2798 | AioContext *aio_context; |
b4b076da | 2799 | MigrationIncomingState *mis = migration_incoming_get_current(); |
a672b469 | 2800 | |
377b21cc | 2801 | if (!replay_can_snapshot()) { |
4dd32b3d MA |
2802 | error_setg(errp, "Record/replay does not allow loading snapshot " |
2803 | "right now. Try once more later."); | |
377b21cc PD |
2804 | return -EINVAL; |
2805 | } | |
2806 | ||
849f96e2 | 2807 | if (!bdrv_all_can_snapshot(&bs)) { |
927d6638 JQ |
2808 | error_setg(errp, |
2809 | "Device '%s' is writable but does not support snapshots", | |
2810 | bdrv_get_device_name(bs)); | |
849f96e2 DL |
2811 | return -ENOTSUP; |
2812 | } | |
723ccda1 DL |
2813 | ret = bdrv_all_find_snapshot(name, &bs); |
2814 | if (ret < 0) { | |
927d6638 JQ |
2815 | error_setg(errp, |
2816 | "Device '%s' does not have the requested snapshot '%s'", | |
2817 | bdrv_get_device_name(bs), name); | |
723ccda1 DL |
2818 | return ret; |
2819 | } | |
849f96e2 | 2820 | |
7cb14481 | 2821 | bs_vm_state = bdrv_all_find_vmstate_bs(); |
f0aa7a8b | 2822 | if (!bs_vm_state) { |
927d6638 | 2823 | error_setg(errp, "No block device supports snapshots"); |
f0aa7a8b MDCF |
2824 | return -ENOTSUP; |
2825 | } | |
79b3c12a | 2826 | aio_context = bdrv_get_aio_context(bs_vm_state); |
f0aa7a8b MDCF |
2827 | |
2828 | /* Don't even try to load empty VM states */ | |
79b3c12a | 2829 | aio_context_acquire(aio_context); |
f0aa7a8b | 2830 | ret = bdrv_snapshot_find(bs_vm_state, &sn, name); |
79b3c12a | 2831 | aio_context_release(aio_context); |
f0aa7a8b MDCF |
2832 | if (ret < 0) { |
2833 | return ret; | |
2834 | } else if (sn.vm_state_size == 0) { | |
927d6638 JQ |
2835 | error_setg(errp, "This is a disk-only snapshot. Revert to it " |
2836 | " offline using qemu-img"); | |
f0aa7a8b MDCF |
2837 | return -EINVAL; |
2838 | } | |
2839 | ||
a672b469 | 2840 | /* Flush all IO requests so they don't interfere with the new state. */ |
8649f2f9 | 2841 | bdrv_drain_all_begin(); |
a672b469 | 2842 | |
2b624fe0 | 2843 | ret = bdrv_all_goto_snapshot(name, &bs, errp); |
4c1cdbaa | 2844 | if (ret < 0) { |
2b624fe0 KW |
2845 | error_prepend(errp, "Could not load snapshot '%s' on '%s': ", |
2846 | name, bdrv_get_device_name(bs)); | |
8649f2f9 | 2847 | goto err_drain; |
a672b469 AL |
2848 | } |
2849 | ||
a672b469 | 2850 | /* restore the VM state */ |
f0aa7a8b | 2851 | f = qemu_fopen_bdrv(bs_vm_state, 0); |
a672b469 | 2852 | if (!f) { |
927d6638 | 2853 | error_setg(errp, "Could not open VM state file"); |
8649f2f9 SH |
2854 | ret = -EINVAL; |
2855 | goto err_drain; | |
a672b469 | 2856 | } |
f0aa7a8b | 2857 | |
aedbe192 | 2858 | qemu_system_reset(SHUTDOWN_CAUSE_NONE); |
b4b076da | 2859 | mis->from_src_file = f; |
f0aa7a8b | 2860 | |
79b3c12a DL |
2861 | aio_context_acquire(aio_context); |
2862 | ret = qemu_loadvm_state(f); | |
1575829d | 2863 | migration_incoming_state_destroy(); |
79b3c12a DL |
2864 | aio_context_release(aio_context); |
2865 | ||
8649f2f9 SH |
2866 | bdrv_drain_all_end(); |
2867 | ||
a672b469 | 2868 | if (ret < 0) { |
927d6638 | 2869 | error_setg(errp, "Error %d while loading VM state", ret); |
05f2401e | 2870 | return ret; |
a672b469 | 2871 | } |
f0aa7a8b | 2872 | |
05f2401e | 2873 | return 0; |
8649f2f9 SH |
2874 | |
2875 | err_drain: | |
2876 | bdrv_drain_all_end(); | |
2877 | return ret; | |
7b630349 JQ |
2878 | } |
2879 | ||
c5705a77 AK |
2880 | void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev) |
2881 | { | |
fa53a0e5 | 2882 | qemu_ram_set_idstr(mr->ram_block, |
c5705a77 | 2883 | memory_region_name(mr), dev); |
b895de50 | 2884 | qemu_ram_set_migratable(mr->ram_block); |
c5705a77 AK |
2885 | } |
2886 | ||
2887 | void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev) | |
2888 | { | |
fa53a0e5 | 2889 | qemu_ram_unset_idstr(mr->ram_block); |
b895de50 | 2890 | qemu_ram_unset_migratable(mr->ram_block); |
c5705a77 AK |
2891 | } |
2892 | ||
2893 | void vmstate_register_ram_global(MemoryRegion *mr) | |
2894 | { | |
2895 | vmstate_register_ram(mr, NULL); | |
2896 | } | |
1bfe5f05 JQ |
2897 | |
2898 | bool vmstate_check_only_migratable(const VMStateDescription *vmsd) | |
2899 | { | |
2900 | /* check needed if --only-migratable is specified */ | |
811f8652 | 2901 | if (!only_migratable) { |
1bfe5f05 JQ |
2902 | return true; |
2903 | } | |
2904 | ||
2905 | return !(vmsd && vmsd->unmigratable); | |
2906 | } |