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