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