]>
Commit | Line | Data |
---|---|---|
a672b469 AL |
1 | /* |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
76cc7b58 JQ |
5 | * Copyright (c) 2009-2015 Red Hat Inc |
6 | * | |
7 | * Authors: | |
8 | * Juan Quintela <[email protected]> | |
a672b469 AL |
9 | * |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
23 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
26 | * THE SOFTWARE. | |
27 | */ | |
a672b469 | 28 | |
d40cdb10 | 29 | #include "config-host.h" |
511d2b14 | 30 | #include "qemu-common.h" |
abfd9ce3 | 31 | #include "hw/boards.h" |
511d2b14 | 32 | #include "hw/hw.h" |
7685ee6a | 33 | #include "hw/qdev.h" |
1422e32d | 34 | #include "net/net.h" |
83c9089e | 35 | #include "monitor/monitor.h" |
9c17d615 | 36 | #include "sysemu/sysemu.h" |
1de7afc9 | 37 | #include "qemu/timer.h" |
511d2b14 | 38 | #include "audio/audio.h" |
caf71f86 | 39 | #include "migration/migration.h" |
cc7a8ea7 | 40 | #include "qapi/qmp/qerror.h" |
d49b6836 | 41 | #include "qemu/error-report.h" |
1de7afc9 PB |
42 | #include "qemu/sockets.h" |
43 | #include "qemu/queue.h" | |
9c17d615 | 44 | #include "sysemu/cpus.h" |
022c62cb | 45 | #include "exec/memory.h" |
a7ae8355 | 46 | #include "qmp-commands.h" |
517a13c9 | 47 | #include "trace.h" |
28085f7b | 48 | #include "qemu/iov.h" |
de08c606 | 49 | #include "block/snapshot.h" |
f364ec65 | 50 | #include "block/qapi.h" |
511d2b14 | 51 | |
a672b469 | 52 | |
18995b98 | 53 | #ifndef ETH_P_RARP |
f8778a77 | 54 | #define ETH_P_RARP 0x8035 |
18995b98 N |
55 | #endif |
56 | #define ARP_HTYPE_ETH 0x0001 | |
57 | #define ARP_PTYPE_IP 0x0800 | |
58 | #define ARP_OP_REQUEST_REV 0x3 | |
59 | ||
37fb569c DDAG |
60 | static bool skip_section_footers; |
61 | ||
18995b98 | 62 | static int announce_self_create(uint8_t *buf, |
5cecf414 | 63 | uint8_t *mac_addr) |
a672b469 | 64 | { |
18995b98 N |
65 | /* Ethernet header. */ |
66 | memset(buf, 0xff, 6); /* destination MAC addr */ | |
67 | memcpy(buf + 6, mac_addr, 6); /* source MAC addr */ | |
68 | *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */ | |
69 | ||
70 | /* RARP header. */ | |
71 | *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */ | |
72 | *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */ | |
73 | *(buf + 18) = 6; /* hardware addr length (ethernet) */ | |
74 | *(buf + 19) = 4; /* protocol addr length (IPv4) */ | |
75 | *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */ | |
76 | memcpy(buf + 22, mac_addr, 6); /* source hw addr */ | |
77 | memset(buf + 28, 0x00, 4); /* source protocol addr */ | |
78 | memcpy(buf + 32, mac_addr, 6); /* target hw addr */ | |
79 | memset(buf + 38, 0x00, 4); /* target protocol addr */ | |
80 | ||
81 | /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */ | |
82 | memset(buf + 42, 0x00, 18); | |
83 | ||
84 | return 60; /* len (FCS will be added by hardware) */ | |
a672b469 AL |
85 | } |
86 | ||
f401ca22 | 87 | static void qemu_announce_self_iter(NICState *nic, void *opaque) |
a672b469 | 88 | { |
18995b98 | 89 | uint8_t buf[60]; |
f401ca22 MM |
90 | int len; |
91 | ||
9013dca5 | 92 | trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic->conf->macaddr)); |
f401ca22 MM |
93 | len = announce_self_create(buf, nic->conf->macaddr.a); |
94 | ||
b356f76d | 95 | qemu_send_packet_raw(qemu_get_queue(nic), buf, len); |
f401ca22 MM |
96 | } |
97 | ||
98 | ||
99 | static void qemu_announce_self_once(void *opaque) | |
100 | { | |
ed8b330b GN |
101 | static int count = SELF_ANNOUNCE_ROUNDS; |
102 | QEMUTimer *timer = *(QEMUTimer **)opaque; | |
a672b469 | 103 | |
f401ca22 MM |
104 | qemu_foreach_nic(qemu_announce_self_iter, NULL); |
105 | ||
18995b98 N |
106 | if (--count) { |
107 | /* delay 50ms, 150ms, 250ms, ... */ | |
bc72ad67 | 108 | timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + |
508e1180 | 109 | self_announce_delay(count)); |
ed8b330b | 110 | } else { |
5cecf414 EH |
111 | timer_del(timer); |
112 | timer_free(timer); | |
ed8b330b GN |
113 | } |
114 | } | |
115 | ||
116 | void qemu_announce_self(void) | |
117 | { | |
5cecf414 EH |
118 | static QEMUTimer *timer; |
119 | timer = timer_new_ms(QEMU_CLOCK_REALTIME, qemu_announce_self_once, &timer); | |
120 | qemu_announce_self_once(&timer); | |
a672b469 AL |
121 | } |
122 | ||
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_put_buffer(void *opaque, const uint8_t *buf, |
142 | int64_t pos, size_t size) | |
a672b469 | 143 | { |
45566e9c | 144 | bdrv_save_vmstate(opaque, buf, pos, size); |
a672b469 AL |
145 | return size; |
146 | } | |
147 | ||
a202a4c0 DDAG |
148 | static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, |
149 | size_t size) | |
a672b469 | 150 | { |
45566e9c | 151 | return bdrv_load_vmstate(opaque, buf, pos, size); |
a672b469 AL |
152 | } |
153 | ||
154 | static int bdrv_fclose(void *opaque) | |
155 | { | |
ad492c92 | 156 | return bdrv_flush(opaque); |
a672b469 AL |
157 | } |
158 | ||
9229bf3c PB |
159 | static const QEMUFileOps bdrv_read_ops = { |
160 | .get_buffer = block_get_buffer, | |
161 | .close = bdrv_fclose | |
162 | }; | |
163 | ||
164 | static const QEMUFileOps bdrv_write_ops = { | |
05fcc848 KW |
165 | .put_buffer = block_put_buffer, |
166 | .writev_buffer = block_writev_buffer, | |
167 | .close = bdrv_fclose | |
9229bf3c PB |
168 | }; |
169 | ||
45566e9c | 170 | static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable) |
a672b469 | 171 | { |
38ff78d3 | 172 | if (is_writable) { |
9229bf3c | 173 | return qemu_fopen_ops(bs, &bdrv_write_ops); |
38ff78d3 | 174 | } |
9229bf3c | 175 | return qemu_fopen_ops(bs, &bdrv_read_ops); |
a672b469 AL |
176 | } |
177 | ||
2ff68d07 | 178 | |
bb1a6d8c EH |
179 | /* QEMUFile timer support. |
180 | * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c | |
181 | */ | |
2ff68d07 | 182 | |
40daca54 | 183 | void timer_put(QEMUFile *f, QEMUTimer *ts) |
2ff68d07 PB |
184 | { |
185 | uint64_t expire_time; | |
186 | ||
e93379b0 | 187 | expire_time = timer_expire_time_ns(ts); |
2ff68d07 PB |
188 | qemu_put_be64(f, expire_time); |
189 | } | |
190 | ||
40daca54 | 191 | void timer_get(QEMUFile *f, QEMUTimer *ts) |
2ff68d07 PB |
192 | { |
193 | uint64_t expire_time; | |
194 | ||
195 | expire_time = qemu_get_be64(f); | |
196 | if (expire_time != -1) { | |
bc72ad67 | 197 | timer_mod_ns(ts, expire_time); |
2ff68d07 | 198 | } else { |
bc72ad67 | 199 | timer_del(ts); |
2ff68d07 PB |
200 | } |
201 | } | |
202 | ||
203 | ||
bb1a6d8c EH |
204 | /* VMState timer support. |
205 | * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c | |
206 | */ | |
dde0463b JQ |
207 | |
208 | static int get_timer(QEMUFile *f, void *pv, size_t size) | |
209 | { | |
210 | QEMUTimer *v = pv; | |
40daca54 | 211 | timer_get(f, v); |
dde0463b JQ |
212 | return 0; |
213 | } | |
214 | ||
84e2e3eb | 215 | static void put_timer(QEMUFile *f, void *pv, size_t size) |
dde0463b | 216 | { |
84e2e3eb | 217 | QEMUTimer *v = pv; |
40daca54 | 218 | timer_put(f, v); |
dde0463b JQ |
219 | } |
220 | ||
221 | const VMStateInfo vmstate_info_timer = { | |
222 | .name = "timer", | |
223 | .get = get_timer, | |
224 | .put = put_timer, | |
225 | }; | |
226 | ||
08e99e29 | 227 | |
7685ee6a AW |
228 | typedef struct CompatEntry { |
229 | char idstr[256]; | |
230 | int instance_id; | |
231 | } CompatEntry; | |
232 | ||
a672b469 | 233 | typedef struct SaveStateEntry { |
72cf2d4f | 234 | QTAILQ_ENTRY(SaveStateEntry) entry; |
a672b469 AL |
235 | char idstr[256]; |
236 | int instance_id; | |
4d2ffa08 | 237 | int alias_id; |
a672b469 AL |
238 | int version_id; |
239 | int section_id; | |
22ea40f4 | 240 | 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 | bool skip_configuration; |
251 | uint32_t len; | |
252 | const char *name; | |
0163a2e0 JQ |
253 | } SaveState; |
254 | ||
255 | static SaveState savevm_state = { | |
256 | .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers), | |
257 | .global_section_id = 0, | |
61964c23 JQ |
258 | .skip_configuration = false, |
259 | }; | |
260 | ||
261 | void savevm_skip_configuration(void) | |
262 | { | |
263 | savevm_state.skip_configuration = true; | |
264 | } | |
265 | ||
266 | ||
267 | static void configuration_pre_save(void *opaque) | |
268 | { | |
269 | SaveState *state = opaque; | |
270 | const char *current_name = MACHINE_GET_CLASS(current_machine)->name; | |
271 | ||
272 | state->len = strlen(current_name); | |
273 | state->name = current_name; | |
274 | } | |
275 | ||
276 | static int configuration_post_load(void *opaque, int version_id) | |
277 | { | |
278 | SaveState *state = opaque; | |
279 | const char *current_name = MACHINE_GET_CLASS(current_machine)->name; | |
280 | ||
281 | if (strncmp(state->name, current_name, state->len) != 0) { | |
282 | error_report("Machine type received is '%s' and local is '%s'", | |
283 | state->name, current_name); | |
284 | return -EINVAL; | |
285 | } | |
286 | return 0; | |
287 | } | |
288 | ||
289 | static const VMStateDescription vmstate_configuration = { | |
290 | .name = "configuration", | |
291 | .version_id = 1, | |
292 | .post_load = configuration_post_load, | |
293 | .pre_save = configuration_pre_save, | |
294 | .fields = (VMStateField[]) { | |
295 | VMSTATE_UINT32(len, SaveState), | |
296 | VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, 0, len), | |
297 | VMSTATE_END_OF_LIST() | |
298 | }, | |
0163a2e0 | 299 | }; |
a672b469 | 300 | |
abfd9ce3 AS |
301 | static void dump_vmstate_vmsd(FILE *out_file, |
302 | const VMStateDescription *vmsd, int indent, | |
303 | bool is_subsection); | |
304 | ||
305 | static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field, | |
306 | int indent) | |
307 | { | |
308 | fprintf(out_file, "%*s{\n", indent, ""); | |
309 | indent += 2; | |
310 | fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name); | |
311 | fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "", | |
312 | field->version_id); | |
313 | fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "", | |
314 | field->field_exists ? "true" : "false"); | |
315 | fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size); | |
316 | if (field->vmsd != NULL) { | |
317 | fprintf(out_file, ",\n"); | |
318 | dump_vmstate_vmsd(out_file, field->vmsd, indent, false); | |
319 | } | |
320 | fprintf(out_file, "\n%*s}", indent - 2, ""); | |
321 | } | |
322 | ||
323 | static void dump_vmstate_vmss(FILE *out_file, | |
5cd8cada | 324 | const VMStateDescription **subsection, |
abfd9ce3 AS |
325 | int indent) |
326 | { | |
5cd8cada JQ |
327 | if (*subsection != NULL) { |
328 | dump_vmstate_vmsd(out_file, *subsection, indent, true); | |
abfd9ce3 AS |
329 | } |
330 | } | |
331 | ||
332 | static void dump_vmstate_vmsd(FILE *out_file, | |
333 | const VMStateDescription *vmsd, int indent, | |
334 | bool is_subsection) | |
335 | { | |
336 | if (is_subsection) { | |
337 | fprintf(out_file, "%*s{\n", indent, ""); | |
338 | } else { | |
339 | fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description"); | |
340 | } | |
341 | indent += 2; | |
342 | fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name); | |
343 | fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "", | |
344 | vmsd->version_id); | |
345 | fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "", | |
346 | vmsd->minimum_version_id); | |
347 | if (vmsd->fields != NULL) { | |
348 | const VMStateField *field = vmsd->fields; | |
349 | bool first; | |
350 | ||
351 | fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, ""); | |
352 | first = true; | |
353 | while (field->name != NULL) { | |
354 | if (field->flags & VMS_MUST_EXIST) { | |
355 | /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */ | |
356 | field++; | |
357 | continue; | |
358 | } | |
359 | if (!first) { | |
360 | fprintf(out_file, ",\n"); | |
361 | } | |
362 | dump_vmstate_vmsf(out_file, field, indent + 2); | |
363 | field++; | |
364 | first = false; | |
365 | } | |
366 | fprintf(out_file, "\n%*s]", indent, ""); | |
367 | } | |
368 | if (vmsd->subsections != NULL) { | |
5cd8cada | 369 | const VMStateDescription **subsection = vmsd->subsections; |
abfd9ce3 AS |
370 | bool first; |
371 | ||
372 | fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, ""); | |
373 | first = true; | |
5cd8cada | 374 | while (*subsection != NULL) { |
abfd9ce3 AS |
375 | if (!first) { |
376 | fprintf(out_file, ",\n"); | |
377 | } | |
378 | dump_vmstate_vmss(out_file, subsection, indent + 2); | |
379 | subsection++; | |
380 | first = false; | |
381 | } | |
382 | fprintf(out_file, "\n%*s]", indent, ""); | |
383 | } | |
384 | fprintf(out_file, "\n%*s}", indent - 2, ""); | |
385 | } | |
386 | ||
387 | static void dump_machine_type(FILE *out_file) | |
388 | { | |
389 | MachineClass *mc; | |
390 | ||
391 | mc = MACHINE_GET_CLASS(current_machine); | |
392 | ||
393 | fprintf(out_file, " \"vmschkmachine\": {\n"); | |
394 | fprintf(out_file, " \"Name\": \"%s\"\n", mc->name); | |
395 | fprintf(out_file, " },\n"); | |
396 | } | |
397 | ||
398 | void dump_vmstate_json_to_file(FILE *out_file) | |
399 | { | |
400 | GSList *list, *elt; | |
401 | bool first; | |
402 | ||
403 | fprintf(out_file, "{\n"); | |
404 | dump_machine_type(out_file); | |
405 | ||
406 | first = true; | |
407 | list = object_class_get_list(TYPE_DEVICE, true); | |
408 | for (elt = list; elt; elt = elt->next) { | |
409 | DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data, | |
410 | TYPE_DEVICE); | |
411 | const char *name; | |
412 | int indent = 2; | |
413 | ||
414 | if (!dc->vmsd) { | |
415 | continue; | |
416 | } | |
417 | ||
418 | if (!first) { | |
419 | fprintf(out_file, ",\n"); | |
420 | } | |
421 | name = object_class_get_name(OBJECT_CLASS(dc)); | |
422 | fprintf(out_file, "%*s\"%s\": {\n", indent, "", name); | |
423 | indent += 2; | |
424 | fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name); | |
425 | fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "", | |
426 | dc->vmsd->version_id); | |
427 | fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "", | |
428 | dc->vmsd->minimum_version_id); | |
429 | ||
430 | dump_vmstate_vmsd(out_file, dc->vmsd, indent, false); | |
431 | ||
432 | fprintf(out_file, "\n%*s}", indent - 2, ""); | |
433 | first = false; | |
434 | } | |
435 | fprintf(out_file, "\n}\n"); | |
436 | fclose(out_file); | |
437 | } | |
438 | ||
8718e999 JQ |
439 | static int calculate_new_instance_id(const char *idstr) |
440 | { | |
441 | SaveStateEntry *se; | |
442 | int instance_id = 0; | |
443 | ||
0163a2e0 | 444 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
8718e999 JQ |
445 | if (strcmp(idstr, se->idstr) == 0 |
446 | && instance_id <= se->instance_id) { | |
447 | instance_id = se->instance_id + 1; | |
448 | } | |
449 | } | |
450 | return instance_id; | |
451 | } | |
452 | ||
7685ee6a AW |
453 | static int calculate_compat_instance_id(const char *idstr) |
454 | { | |
455 | SaveStateEntry *se; | |
456 | int instance_id = 0; | |
457 | ||
0163a2e0 | 458 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
38ff78d3 | 459 | if (!se->compat) { |
7685ee6a | 460 | continue; |
38ff78d3 | 461 | } |
7685ee6a AW |
462 | |
463 | if (strcmp(idstr, se->compat->idstr) == 0 | |
464 | && instance_id <= se->compat->instance_id) { | |
465 | instance_id = se->compat->instance_id + 1; | |
466 | } | |
467 | } | |
468 | return instance_id; | |
469 | } | |
470 | ||
a672b469 AL |
471 | /* TODO: Individual devices generally have very little idea about the rest |
472 | of the system, so instance_id should be removed/replaced. | |
473 | Meanwhile pass -1 as instance_id if you do not already have a clearly | |
474 | distinguishing id for all instances of your device class. */ | |
0be71e32 AW |
475 | int register_savevm_live(DeviceState *dev, |
476 | const char *idstr, | |
a672b469 AL |
477 | int instance_id, |
478 | int version_id, | |
7908c78d | 479 | SaveVMHandlers *ops, |
a672b469 AL |
480 | void *opaque) |
481 | { | |
8718e999 | 482 | SaveStateEntry *se; |
a672b469 | 483 | |
97f3ad35 | 484 | se = g_new0(SaveStateEntry, 1); |
a672b469 | 485 | se->version_id = version_id; |
0163a2e0 | 486 | se->section_id = savevm_state.global_section_id++; |
7908c78d | 487 | se->ops = ops; |
a672b469 | 488 | se->opaque = opaque; |
9ed7d6ae | 489 | se->vmsd = NULL; |
a7ae8355 | 490 | /* if this is a live_savem then set is_ram */ |
16310a3c | 491 | if (ops->save_live_setup != NULL) { |
a7ae8355 SS |
492 | se->is_ram = 1; |
493 | } | |
a672b469 | 494 | |
09e5ab63 AL |
495 | if (dev) { |
496 | char *id = qdev_get_dev_path(dev); | |
7685ee6a AW |
497 | if (id) { |
498 | pstrcpy(se->idstr, sizeof(se->idstr), id); | |
499 | pstrcat(se->idstr, sizeof(se->idstr), "/"); | |
7267c094 | 500 | g_free(id); |
7685ee6a | 501 | |
97f3ad35 | 502 | se->compat = g_new0(CompatEntry, 1); |
7685ee6a AW |
503 | pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr); |
504 | se->compat->instance_id = instance_id == -1 ? | |
505 | calculate_compat_instance_id(idstr) : instance_id; | |
506 | instance_id = -1; | |
507 | } | |
508 | } | |
509 | pstrcat(se->idstr, sizeof(se->idstr), idstr); | |
510 | ||
8718e999 | 511 | if (instance_id == -1) { |
7685ee6a | 512 | se->instance_id = calculate_new_instance_id(se->idstr); |
8718e999 JQ |
513 | } else { |
514 | se->instance_id = instance_id; | |
a672b469 | 515 | } |
7685ee6a | 516 | assert(!se->compat || se->instance_id == 0); |
8718e999 | 517 | /* add at the end of list */ |
0163a2e0 | 518 | QTAILQ_INSERT_TAIL(&savevm_state.handlers, se, entry); |
a672b469 AL |
519 | return 0; |
520 | } | |
521 | ||
0be71e32 AW |
522 | int register_savevm(DeviceState *dev, |
523 | const char *idstr, | |
a672b469 AL |
524 | int instance_id, |
525 | int version_id, | |
526 | SaveStateHandler *save_state, | |
527 | LoadStateHandler *load_state, | |
528 | void *opaque) | |
529 | { | |
97f3ad35 | 530 | SaveVMHandlers *ops = g_new0(SaveVMHandlers, 1); |
7908c78d JQ |
531 | ops->save_state = save_state; |
532 | ops->load_state = load_state; | |
0be71e32 | 533 | return register_savevm_live(dev, idstr, instance_id, version_id, |
7908c78d | 534 | ops, opaque); |
a672b469 AL |
535 | } |
536 | ||
0be71e32 | 537 | void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque) |
41bd13af | 538 | { |
8718e999 | 539 | SaveStateEntry *se, *new_se; |
7685ee6a AW |
540 | char id[256] = ""; |
541 | ||
09e5ab63 AL |
542 | if (dev) { |
543 | char *path = qdev_get_dev_path(dev); | |
7685ee6a AW |
544 | if (path) { |
545 | pstrcpy(id, sizeof(id), path); | |
546 | pstrcat(id, sizeof(id), "/"); | |
7267c094 | 547 | g_free(path); |
7685ee6a AW |
548 | } |
549 | } | |
550 | pstrcat(id, sizeof(id), idstr); | |
41bd13af | 551 | |
0163a2e0 | 552 | QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) { |
7685ee6a | 553 | if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) { |
0163a2e0 | 554 | QTAILQ_REMOVE(&savevm_state.handlers, se, entry); |
ef1e1e07 | 555 | g_free(se->compat); |
22ea40f4 | 556 | g_free(se->ops); |
7267c094 | 557 | g_free(se); |
41bd13af | 558 | } |
41bd13af AL |
559 | } |
560 | } | |
561 | ||
0be71e32 | 562 | int vmstate_register_with_alias_id(DeviceState *dev, int instance_id, |
4d2ffa08 JK |
563 | const VMStateDescription *vmsd, |
564 | void *opaque, int alias_id, | |
565 | int required_for_version) | |
9ed7d6ae | 566 | { |
8718e999 | 567 | SaveStateEntry *se; |
9ed7d6ae | 568 | |
4d2ffa08 JK |
569 | /* If this triggers, alias support can be dropped for the vmsd. */ |
570 | assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id); | |
571 | ||
97f3ad35 | 572 | se = g_new0(SaveStateEntry, 1); |
9ed7d6ae | 573 | se->version_id = vmsd->version_id; |
0163a2e0 | 574 | se->section_id = savevm_state.global_section_id++; |
9ed7d6ae JQ |
575 | se->opaque = opaque; |
576 | se->vmsd = vmsd; | |
4d2ffa08 | 577 | se->alias_id = alias_id; |
9ed7d6ae | 578 | |
09e5ab63 AL |
579 | if (dev) { |
580 | char *id = qdev_get_dev_path(dev); | |
7685ee6a AW |
581 | if (id) { |
582 | pstrcpy(se->idstr, sizeof(se->idstr), id); | |
583 | pstrcat(se->idstr, sizeof(se->idstr), "/"); | |
7267c094 | 584 | g_free(id); |
7685ee6a | 585 | |
97f3ad35 | 586 | se->compat = g_new0(CompatEntry, 1); |
7685ee6a AW |
587 | pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name); |
588 | se->compat->instance_id = instance_id == -1 ? | |
589 | calculate_compat_instance_id(vmsd->name) : instance_id; | |
590 | instance_id = -1; | |
591 | } | |
592 | } | |
593 | pstrcat(se->idstr, sizeof(se->idstr), vmsd->name); | |
594 | ||
8718e999 | 595 | if (instance_id == -1) { |
7685ee6a | 596 | se->instance_id = calculate_new_instance_id(se->idstr); |
8718e999 JQ |
597 | } else { |
598 | se->instance_id = instance_id; | |
9ed7d6ae | 599 | } |
7685ee6a | 600 | assert(!se->compat || se->instance_id == 0); |
8718e999 | 601 | /* add at the end of list */ |
0163a2e0 | 602 | QTAILQ_INSERT_TAIL(&savevm_state.handlers, se, entry); |
9ed7d6ae JQ |
603 | return 0; |
604 | } | |
605 | ||
0be71e32 AW |
606 | void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd, |
607 | void *opaque) | |
9ed7d6ae | 608 | { |
1eb7538b JQ |
609 | SaveStateEntry *se, *new_se; |
610 | ||
0163a2e0 | 611 | QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) { |
1eb7538b | 612 | if (se->vmsd == vmsd && se->opaque == opaque) { |
0163a2e0 | 613 | QTAILQ_REMOVE(&savevm_state.handlers, se, entry); |
ef1e1e07 | 614 | g_free(se->compat); |
7267c094 | 615 | g_free(se); |
1eb7538b JQ |
616 | } |
617 | } | |
9ed7d6ae JQ |
618 | } |
619 | ||
4082be4d JQ |
620 | static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id) |
621 | { | |
9013dca5 | 622 | trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)"); |
9ed7d6ae | 623 | if (!se->vmsd) { /* Old style */ |
22ea40f4 | 624 | return se->ops->load_state(f, se->opaque, version_id); |
9ed7d6ae JQ |
625 | } |
626 | return vmstate_load_state(f, se->vmsd, se->opaque, version_id); | |
4082be4d JQ |
627 | } |
628 | ||
8118f095 AG |
629 | static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc) |
630 | { | |
631 | int64_t old_offset, size; | |
632 | ||
633 | old_offset = qemu_ftell_fast(f); | |
634 | se->ops->save_state(f, se->opaque); | |
635 | size = qemu_ftell_fast(f) - old_offset; | |
636 | ||
637 | if (vmdesc) { | |
638 | json_prop_int(vmdesc, "size", size); | |
639 | json_start_array(vmdesc, "fields"); | |
640 | json_start_object(vmdesc, NULL); | |
641 | json_prop_str(vmdesc, "name", "data"); | |
642 | json_prop_int(vmdesc, "size", size); | |
643 | json_prop_str(vmdesc, "type", "buffer"); | |
644 | json_end_object(vmdesc); | |
645 | json_end_array(vmdesc); | |
646 | } | |
647 | } | |
648 | ||
649 | static void vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc) | |
4082be4d | 650 | { |
9013dca5 | 651 | trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)"); |
8118f095 AG |
652 | if (!se->vmsd) { |
653 | vmstate_save_old_style(f, se, vmdesc); | |
dc912121 | 654 | return; |
9ed7d6ae | 655 | } |
8118f095 | 656 | vmstate_save_state(f, se->vmsd, se->opaque, vmdesc); |
4082be4d JQ |
657 | } |
658 | ||
37fb569c DDAG |
659 | void savevm_skip_section_footers(void) |
660 | { | |
661 | skip_section_footers = true; | |
662 | } | |
663 | ||
ce39bfc9 DDAG |
664 | /* |
665 | * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL) | |
666 | */ | |
667 | static void save_section_header(QEMUFile *f, SaveStateEntry *se, | |
668 | uint8_t section_type) | |
669 | { | |
670 | qemu_put_byte(f, section_type); | |
671 | qemu_put_be32(f, se->section_id); | |
672 | ||
673 | if (section_type == QEMU_VM_SECTION_FULL || | |
674 | section_type == QEMU_VM_SECTION_START) { | |
675 | /* ID string */ | |
676 | size_t len = strlen(se->idstr); | |
677 | qemu_put_byte(f, len); | |
678 | qemu_put_buffer(f, (uint8_t *)se->idstr, len); | |
679 | ||
680 | qemu_put_be32(f, se->instance_id); | |
681 | qemu_put_be32(f, se->version_id); | |
682 | } | |
683 | } | |
684 | ||
f68945d4 DDAG |
685 | /* |
686 | * Write a footer onto device sections that catches cases misformatted device | |
687 | * sections. | |
688 | */ | |
689 | static void save_section_footer(QEMUFile *f, SaveStateEntry *se) | |
690 | { | |
691 | if (!skip_section_footers) { | |
692 | qemu_put_byte(f, QEMU_VM_SECTION_FOOTER); | |
693 | qemu_put_be32(f, se->section_id); | |
694 | } | |
695 | } | |
696 | ||
e1c37d0e | 697 | bool qemu_savevm_state_blocked(Error **errp) |
dc912121 AW |
698 | { |
699 | SaveStateEntry *se; | |
700 | ||
0163a2e0 | 701 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
7d854c47 | 702 | if (se->vmsd && se->vmsd->unmigratable) { |
f231b88d CR |
703 | error_setg(errp, "State blocked by non-migratable device '%s'", |
704 | se->idstr); | |
dc912121 AW |
705 | return true; |
706 | } | |
707 | } | |
708 | return false; | |
709 | } | |
710 | ||
f796baa1 DDAG |
711 | void qemu_savevm_state_header(QEMUFile *f) |
712 | { | |
713 | trace_savevm_state_header(); | |
714 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); | |
715 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); | |
716 | } | |
717 | ||
47c8c17a PB |
718 | void qemu_savevm_state_begin(QEMUFile *f, |
719 | const MigrationParams *params) | |
a672b469 AL |
720 | { |
721 | SaveStateEntry *se; | |
39346385 | 722 | int ret; |
a672b469 | 723 | |
9013dca5 | 724 | trace_savevm_state_begin(); |
0163a2e0 | 725 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
22ea40f4 | 726 | if (!se->ops || !se->ops->set_params) { |
c163b5ca | 727 | continue; |
6607ae23 | 728 | } |
22ea40f4 | 729 | se->ops->set_params(params, se->opaque); |
c163b5ca | 730 | } |
38ff78d3 | 731 | |
61964c23 JQ |
732 | if (!savevm_state.skip_configuration) { |
733 | qemu_put_byte(f, QEMU_VM_CONFIGURATION); | |
734 | vmstate_save_state(f, &vmstate_configuration, &savevm_state, 0); | |
735 | } | |
736 | ||
0163a2e0 | 737 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
d1315aac | 738 | if (!se->ops || !se->ops->save_live_setup) { |
a672b469 | 739 | continue; |
22ea40f4 | 740 | } |
6bd68781 JQ |
741 | if (se->ops && se->ops->is_active) { |
742 | if (!se->ops->is_active(se->opaque)) { | |
743 | continue; | |
744 | } | |
745 | } | |
ce39bfc9 | 746 | save_section_header(f, se, QEMU_VM_SECTION_START); |
a672b469 | 747 | |
d1315aac | 748 | ret = se->ops->save_live_setup(f, se->opaque); |
f68945d4 | 749 | save_section_footer(f, se); |
2975725f | 750 | if (ret < 0) { |
47c8c17a PB |
751 | qemu_file_set_error(f, ret); |
752 | break; | |
2975725f | 753 | } |
a672b469 | 754 | } |
a672b469 AL |
755 | } |
756 | ||
39346385 | 757 | /* |
07f35073 | 758 | * this function has three return values: |
39346385 JQ |
759 | * negative: there was one error, and we have -errno. |
760 | * 0 : We haven't finished, caller have to go again | |
761 | * 1 : We have finished, we can go to complete phase | |
762 | */ | |
539de124 | 763 | int qemu_savevm_state_iterate(QEMUFile *f) |
a672b469 AL |
764 | { |
765 | SaveStateEntry *se; | |
766 | int ret = 1; | |
767 | ||
9013dca5 | 768 | trace_savevm_state_iterate(); |
0163a2e0 | 769 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
16310a3c | 770 | if (!se->ops || !se->ops->save_live_iterate) { |
a672b469 | 771 | continue; |
22ea40f4 | 772 | } |
6bd68781 JQ |
773 | if (se->ops && se->ops->is_active) { |
774 | if (!se->ops->is_active(se->opaque)) { | |
775 | continue; | |
776 | } | |
777 | } | |
aac844ed JQ |
778 | if (qemu_file_rate_limit(f)) { |
779 | return 0; | |
780 | } | |
464400f6 | 781 | trace_savevm_section_start(se->idstr, se->section_id); |
ce39bfc9 DDAG |
782 | |
783 | save_section_header(f, se, QEMU_VM_SECTION_PART); | |
a672b469 | 784 | |
16310a3c | 785 | ret = se->ops->save_live_iterate(f, se->opaque); |
a5df2a02 | 786 | trace_savevm_section_end(se->idstr, se->section_id, ret); |
f68945d4 | 787 | save_section_footer(f, se); |
517a13c9 | 788 | |
47c8c17a PB |
789 | if (ret < 0) { |
790 | qemu_file_set_error(f, ret); | |
791 | } | |
2975725f | 792 | if (ret <= 0) { |
90697be8 JK |
793 | /* Do not proceed to the next vmstate before this one reported |
794 | completion of the current stage. This serializes the migration | |
795 | and reduces the probability that a faster changing state is | |
796 | synchronized over and over again. */ | |
797 | break; | |
798 | } | |
a672b469 | 799 | } |
39346385 | 800 | return ret; |
a672b469 AL |
801 | } |
802 | ||
9850c604 AG |
803 | static bool should_send_vmdesc(void) |
804 | { | |
805 | MachineState *machine = MACHINE(qdev_get_machine()); | |
806 | return !machine->suppress_vmdesc; | |
807 | } | |
808 | ||
47c8c17a | 809 | void qemu_savevm_state_complete(QEMUFile *f) |
a672b469 | 810 | { |
8118f095 AG |
811 | QJSON *vmdesc; |
812 | int vmdesc_len; | |
a672b469 | 813 | SaveStateEntry *se; |
2975725f | 814 | int ret; |
a672b469 | 815 | |
9013dca5 AK |
816 | trace_savevm_state_complete(); |
817 | ||
ea375f9a JK |
818 | cpu_synchronize_all_states(); |
819 | ||
0163a2e0 | 820 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
16310a3c | 821 | if (!se->ops || !se->ops->save_live_complete) { |
a672b469 | 822 | continue; |
22ea40f4 | 823 | } |
6bd68781 JQ |
824 | if (se->ops && se->ops->is_active) { |
825 | if (!se->ops->is_active(se->opaque)) { | |
826 | continue; | |
827 | } | |
828 | } | |
464400f6 | 829 | trace_savevm_section_start(se->idstr, se->section_id); |
ce39bfc9 DDAG |
830 | |
831 | save_section_header(f, se, QEMU_VM_SECTION_END); | |
a672b469 | 832 | |
16310a3c | 833 | ret = se->ops->save_live_complete(f, se->opaque); |
a5df2a02 | 834 | trace_savevm_section_end(se->idstr, se->section_id, ret); |
f68945d4 | 835 | save_section_footer(f, se); |
2975725f | 836 | if (ret < 0) { |
47c8c17a PB |
837 | qemu_file_set_error(f, ret); |
838 | return; | |
2975725f | 839 | } |
a672b469 AL |
840 | } |
841 | ||
8118f095 AG |
842 | vmdesc = qjson_new(); |
843 | json_prop_int(vmdesc, "page_size", TARGET_PAGE_SIZE); | |
844 | json_start_array(vmdesc, "devices"); | |
0163a2e0 | 845 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
a672b469 | 846 | |
22ea40f4 | 847 | if ((!se->ops || !se->ops->save_state) && !se->vmsd) { |
5cecf414 | 848 | continue; |
22ea40f4 | 849 | } |
df896152 JQ |
850 | if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) { |
851 | trace_savevm_section_skip(se->idstr, se->section_id); | |
852 | continue; | |
853 | } | |
854 | ||
464400f6 | 855 | trace_savevm_section_start(se->idstr, se->section_id); |
8118f095 AG |
856 | |
857 | json_start_object(vmdesc, NULL); | |
858 | json_prop_str(vmdesc, "name", se->idstr); | |
859 | json_prop_int(vmdesc, "instance_id", se->instance_id); | |
860 | ||
ce39bfc9 | 861 | save_section_header(f, se, QEMU_VM_SECTION_FULL); |
a672b469 | 862 | |
8118f095 AG |
863 | vmstate_save(f, se, vmdesc); |
864 | ||
865 | json_end_object(vmdesc); | |
a5df2a02 | 866 | trace_savevm_section_end(se->idstr, se->section_id, 0); |
f68945d4 | 867 | save_section_footer(f, se); |
a672b469 AL |
868 | } |
869 | ||
870 | qemu_put_byte(f, QEMU_VM_EOF); | |
8118f095 AG |
871 | |
872 | json_end_array(vmdesc); | |
873 | qjson_finish(vmdesc); | |
874 | vmdesc_len = strlen(qjson_get_str(vmdesc)); | |
875 | ||
9850c604 AG |
876 | if (should_send_vmdesc()) { |
877 | qemu_put_byte(f, QEMU_VM_VMDESCRIPTION); | |
878 | qemu_put_be32(f, vmdesc_len); | |
879 | qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len); | |
880 | } | |
8118f095 AG |
881 | object_unref(OBJECT(vmdesc)); |
882 | ||
edaae611 | 883 | qemu_fflush(f); |
a672b469 AL |
884 | } |
885 | ||
e4ed1541 JQ |
886 | uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size) |
887 | { | |
888 | SaveStateEntry *se; | |
889 | uint64_t ret = 0; | |
890 | ||
0163a2e0 | 891 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
e4ed1541 JQ |
892 | if (!se->ops || !se->ops->save_live_pending) { |
893 | continue; | |
894 | } | |
895 | if (se->ops && se->ops->is_active) { | |
896 | if (!se->ops->is_active(se->opaque)) { | |
897 | continue; | |
898 | } | |
899 | } | |
900 | ret += se->ops->save_live_pending(f, se->opaque, max_size); | |
901 | } | |
902 | return ret; | |
903 | } | |
904 | ||
6522773f | 905 | void qemu_savevm_state_cancel(void) |
4ec7fcc7 JK |
906 | { |
907 | SaveStateEntry *se; | |
908 | ||
9013dca5 | 909 | trace_savevm_state_cancel(); |
0163a2e0 | 910 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
9b5bfab0 JQ |
911 | if (se->ops && se->ops->cancel) { |
912 | se->ops->cancel(se->opaque); | |
4ec7fcc7 JK |
913 | } |
914 | } | |
915 | } | |
916 | ||
5d80448c | 917 | static int qemu_savevm_state(QEMUFile *f, Error **errp) |
a672b469 | 918 | { |
a672b469 | 919 | int ret; |
6607ae23 IY |
920 | MigrationParams params = { |
921 | .blk = 0, | |
922 | .shared = 0 | |
923 | }; | |
a672b469 | 924 | |
5d80448c | 925 | if (qemu_savevm_state_blocked(errp)) { |
04943eba | 926 | return -EINVAL; |
dc912121 AW |
927 | } |
928 | ||
9b095037 | 929 | qemu_mutex_unlock_iothread(); |
f796baa1 | 930 | qemu_savevm_state_header(f); |
47c8c17a | 931 | qemu_savevm_state_begin(f, ¶ms); |
9b095037 PB |
932 | qemu_mutex_lock_iothread(); |
933 | ||
47c8c17a PB |
934 | while (qemu_file_get_error(f) == 0) { |
935 | if (qemu_savevm_state_iterate(f) > 0) { | |
936 | break; | |
937 | } | |
938 | } | |
a672b469 | 939 | |
47c8c17a | 940 | ret = qemu_file_get_error(f); |
39346385 | 941 | if (ret == 0) { |
47c8c17a | 942 | qemu_savevm_state_complete(f); |
624b9cc2 | 943 | ret = qemu_file_get_error(f); |
39346385 | 944 | } |
04943eba PB |
945 | if (ret != 0) { |
946 | qemu_savevm_state_cancel(); | |
5d80448c | 947 | error_setg_errno(errp, -ret, "Error while writing VM state"); |
04943eba | 948 | } |
a672b469 AL |
949 | return ret; |
950 | } | |
951 | ||
a7ae8355 SS |
952 | static int qemu_save_device_state(QEMUFile *f) |
953 | { | |
954 | SaveStateEntry *se; | |
955 | ||
956 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); | |
957 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); | |
958 | ||
959 | cpu_synchronize_all_states(); | |
960 | ||
0163a2e0 | 961 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
a7ae8355 SS |
962 | if (se->is_ram) { |
963 | continue; | |
964 | } | |
22ea40f4 | 965 | if ((!se->ops || !se->ops->save_state) && !se->vmsd) { |
a7ae8355 SS |
966 | continue; |
967 | } | |
df896152 JQ |
968 | if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) { |
969 | continue; | |
970 | } | |
a7ae8355 | 971 | |
ce39bfc9 | 972 | save_section_header(f, se, QEMU_VM_SECTION_FULL); |
a7ae8355 | 973 | |
8118f095 | 974 | vmstate_save(f, se, NULL); |
f68945d4 DDAG |
975 | |
976 | save_section_footer(f, se); | |
a7ae8355 SS |
977 | } |
978 | ||
979 | qemu_put_byte(f, QEMU_VM_EOF); | |
980 | ||
981 | return qemu_file_get_error(f); | |
982 | } | |
983 | ||
a672b469 AL |
984 | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
985 | { | |
986 | SaveStateEntry *se; | |
987 | ||
0163a2e0 | 988 | QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { |
a672b469 | 989 | if (!strcmp(se->idstr, idstr) && |
4d2ffa08 JK |
990 | (instance_id == se->instance_id || |
991 | instance_id == se->alias_id)) | |
a672b469 | 992 | return se; |
7685ee6a AW |
993 | /* Migrating from an older version? */ |
994 | if (strstr(se->idstr, idstr) && se->compat) { | |
995 | if (!strcmp(se->compat->idstr, idstr) && | |
996 | (instance_id == se->compat->instance_id || | |
997 | instance_id == se->alias_id)) | |
998 | return se; | |
999 | } | |
a672b469 AL |
1000 | } |
1001 | return NULL; | |
1002 | } | |
1003 | ||
1a8f46f8 | 1004 | struct LoadStateEntry { |
72cf2d4f | 1005 | QLIST_ENTRY(LoadStateEntry) entry; |
a672b469 AL |
1006 | SaveStateEntry *se; |
1007 | int section_id; | |
1008 | int version_id; | |
1a8f46f8 | 1009 | }; |
a672b469 | 1010 | |
59f39a47 DDAG |
1011 | /* |
1012 | * Read a footer off the wire and check that it matches the expected section | |
1013 | * | |
1014 | * Returns: true if the footer was good | |
1015 | * false if there is a problem (and calls error_report to say why) | |
1016 | */ | |
1017 | static bool check_section_footer(QEMUFile *f, LoadStateEntry *le) | |
1018 | { | |
1019 | uint8_t read_mark; | |
1020 | uint32_t read_section_id; | |
1021 | ||
1022 | if (skip_section_footers) { | |
1023 | /* No footer to check */ | |
1024 | return true; | |
1025 | } | |
1026 | ||
1027 | read_mark = qemu_get_byte(f); | |
1028 | ||
1029 | if (read_mark != QEMU_VM_SECTION_FOOTER) { | |
1030 | error_report("Missing section footer for %s", le->se->idstr); | |
1031 | return false; | |
1032 | } | |
1033 | ||
1034 | read_section_id = qemu_get_be32(f); | |
1035 | if (read_section_id != le->section_id) { | |
1036 | error_report("Mismatched section id in footer for %s -" | |
1037 | " read 0x%x expected 0x%x", | |
1038 | le->se->idstr, read_section_id, le->section_id); | |
1039 | return false; | |
1040 | } | |
1041 | ||
1042 | /* All good */ | |
1043 | return true; | |
1044 | } | |
1045 | ||
1a8f46f8 | 1046 | void loadvm_free_handlers(MigrationIncomingState *mis) |
a672b469 | 1047 | { |
f4dbb8dd | 1048 | LoadStateEntry *le, *new_le; |
1a8f46f8 DDAG |
1049 | |
1050 | QLIST_FOREACH_SAFE(le, &mis->loadvm_handlers, entry, new_le) { | |
1051 | QLIST_REMOVE(le, entry); | |
1052 | g_free(le); | |
1053 | } | |
1054 | } | |
1055 | ||
1056 | int qemu_loadvm_state(QEMUFile *f) | |
1057 | { | |
1058 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
0457d073 | 1059 | Error *local_err = NULL; |
a672b469 AL |
1060 | uint8_t section_type; |
1061 | unsigned int v; | |
1062 | int ret; | |
1925cebc | 1063 | int file_error_after_eof = -1; |
a672b469 | 1064 | |
0457d073 | 1065 | if (qemu_savevm_state_blocked(&local_err)) { |
19867549 | 1066 | error_report_err(local_err); |
dc912121 AW |
1067 | return -EINVAL; |
1068 | } | |
1069 | ||
a672b469 | 1070 | v = qemu_get_be32(f); |
38ff78d3 | 1071 | if (v != QEMU_VM_FILE_MAGIC) { |
0457d073 | 1072 | error_report("Not a migration stream"); |
a672b469 | 1073 | return -EINVAL; |
38ff78d3 | 1074 | } |
a672b469 AL |
1075 | |
1076 | v = qemu_get_be32(f); | |
bbfe1408 | 1077 | if (v == QEMU_VM_FILE_VERSION_COMPAT) { |
6a64b644 | 1078 | error_report("SaveVM v2 format is obsolete and don't work anymore"); |
bbfe1408 JQ |
1079 | return -ENOTSUP; |
1080 | } | |
38ff78d3 | 1081 | if (v != QEMU_VM_FILE_VERSION) { |
0457d073 | 1082 | error_report("Unsupported migration stream version"); |
a672b469 | 1083 | return -ENOTSUP; |
38ff78d3 | 1084 | } |
a672b469 | 1085 | |
61964c23 JQ |
1086 | if (!savevm_state.skip_configuration) { |
1087 | if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) { | |
1088 | error_report("Configuration section missing"); | |
1089 | return -EINVAL; | |
1090 | } | |
1091 | ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0); | |
1092 | ||
1093 | if (ret) { | |
1094 | return ret; | |
1095 | } | |
1096 | } | |
1097 | ||
a672b469 AL |
1098 | while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) { |
1099 | uint32_t instance_id, version_id, section_id; | |
a672b469 | 1100 | SaveStateEntry *se; |
1a8f46f8 | 1101 | LoadStateEntry *le; |
b3af1bc9 | 1102 | char idstr[256]; |
a672b469 | 1103 | |
a5df2a02 | 1104 | trace_qemu_loadvm_state_section(section_type); |
a672b469 AL |
1105 | switch (section_type) { |
1106 | case QEMU_VM_SECTION_START: | |
1107 | case QEMU_VM_SECTION_FULL: | |
1108 | /* Read section start */ | |
1109 | section_id = qemu_get_be32(f); | |
b3af1bc9 DDAG |
1110 | if (!qemu_get_counted_string(f, idstr)) { |
1111 | error_report("Unable to read ID string for section %u", | |
1112 | section_id); | |
1113 | return -EINVAL; | |
1114 | } | |
a672b469 AL |
1115 | instance_id = qemu_get_be32(f); |
1116 | version_id = qemu_get_be32(f); | |
1117 | ||
a5df2a02 DDAG |
1118 | trace_qemu_loadvm_state_section_startfull(section_id, idstr, |
1119 | instance_id, version_id); | |
a672b469 AL |
1120 | /* Find savevm section */ |
1121 | se = find_se(idstr, instance_id); | |
1122 | if (se == NULL) { | |
6a64b644 DDAG |
1123 | error_report("Unknown savevm section or instance '%s' %d", |
1124 | idstr, instance_id); | |
a672b469 AL |
1125 | ret = -EINVAL; |
1126 | goto out; | |
1127 | } | |
1128 | ||
1129 | /* Validate version */ | |
1130 | if (version_id > se->version_id) { | |
6a64b644 DDAG |
1131 | error_report("savevm: unsupported version %d for '%s' v%d", |
1132 | version_id, idstr, se->version_id); | |
a672b469 AL |
1133 | ret = -EINVAL; |
1134 | goto out; | |
1135 | } | |
1136 | ||
1137 | /* Add entry */ | |
7267c094 | 1138 | le = g_malloc0(sizeof(*le)); |
a672b469 AL |
1139 | |
1140 | le->se = se; | |
1141 | le->section_id = section_id; | |
1142 | le->version_id = version_id; | |
1a8f46f8 | 1143 | QLIST_INSERT_HEAD(&mis->loadvm_handlers, le, entry); |
a672b469 | 1144 | |
4082be4d | 1145 | ret = vmstate_load(f, le->se, le->version_id); |
b5a22e4a | 1146 | if (ret < 0) { |
6a64b644 DDAG |
1147 | error_report("error while loading state for instance 0x%x of" |
1148 | " device '%s'", instance_id, idstr); | |
b5a22e4a JQ |
1149 | goto out; |
1150 | } | |
59f39a47 | 1151 | if (!check_section_footer(f, le)) { |
f68945d4 DDAG |
1152 | ret = -EINVAL; |
1153 | goto out; | |
1154 | } | |
a672b469 AL |
1155 | break; |
1156 | case QEMU_VM_SECTION_PART: | |
1157 | case QEMU_VM_SECTION_END: | |
1158 | section_id = qemu_get_be32(f); | |
1159 | ||
a5df2a02 | 1160 | trace_qemu_loadvm_state_section_partend(section_id); |
1a8f46f8 | 1161 | QLIST_FOREACH(le, &mis->loadvm_handlers, entry) { |
f4dbb8dd JQ |
1162 | if (le->section_id == section_id) { |
1163 | break; | |
1164 | } | |
1165 | } | |
a672b469 | 1166 | if (le == NULL) { |
6a64b644 | 1167 | error_report("Unknown savevm section %d", section_id); |
a672b469 AL |
1168 | ret = -EINVAL; |
1169 | goto out; | |
1170 | } | |
1171 | ||
4082be4d | 1172 | ret = vmstate_load(f, le->se, le->version_id); |
b5a22e4a | 1173 | if (ret < 0) { |
6a64b644 DDAG |
1174 | error_report("error while loading state section id %d(%s)", |
1175 | section_id, le->se->idstr); | |
b5a22e4a JQ |
1176 | goto out; |
1177 | } | |
59f39a47 | 1178 | if (!check_section_footer(f, le)) { |
f68945d4 DDAG |
1179 | ret = -EINVAL; |
1180 | goto out; | |
1181 | } | |
a672b469 AL |
1182 | break; |
1183 | default: | |
6a64b644 | 1184 | error_report("Unknown savevm section type %d", section_type); |
a672b469 AL |
1185 | ret = -EINVAL; |
1186 | goto out; | |
1187 | } | |
1188 | } | |
1189 | ||
1925cebc AG |
1190 | file_error_after_eof = qemu_file_get_error(f); |
1191 | ||
1192 | /* | |
1193 | * Try to read in the VMDESC section as well, so that dumping tools that | |
1194 | * intercept our migration stream have the chance to see it. | |
1195 | */ | |
1aca9a5f DDAG |
1196 | |
1197 | /* We've got to be careful; if we don't read the data and just shut the fd | |
1198 | * then the sender can error if we close while it's still sending. | |
1199 | * We also mustn't read data that isn't there; some transports (RDMA) | |
1200 | * will stall waiting for that data when the source has already closed. | |
1201 | */ | |
1202 | if (should_send_vmdesc()) { | |
1203 | uint8_t *buf; | |
1204 | uint32_t size; | |
1205 | section_type = qemu_get_byte(f); | |
1206 | ||
1207 | if (section_type != QEMU_VM_VMDESCRIPTION) { | |
1208 | error_report("Expected vmdescription section, but got %d", | |
1209 | section_type); | |
1210 | /* | |
1211 | * It doesn't seem worth failing at this point since | |
1212 | * we apparently have an otherwise valid VM state | |
1213 | */ | |
1214 | } else { | |
1215 | buf = g_malloc(0x1000); | |
1216 | size = qemu_get_be32(f); | |
1217 | ||
1218 | while (size > 0) { | |
1219 | uint32_t read_chunk = MIN(size, 0x1000); | |
1220 | qemu_get_buffer(f, buf, read_chunk); | |
1221 | size -= read_chunk; | |
1222 | } | |
1223 | g_free(buf); | |
1925cebc | 1224 | } |
1925cebc AG |
1225 | } |
1226 | ||
ea375f9a JK |
1227 | cpu_synchronize_all_post_init(); |
1228 | ||
a672b469 AL |
1229 | ret = 0; |
1230 | ||
1231 | out: | |
42802d47 | 1232 | if (ret == 0) { |
1925cebc AG |
1233 | /* We may not have a VMDESC section, so ignore relative errors */ |
1234 | ret = file_error_after_eof; | |
624b9cc2 | 1235 | } |
a672b469 AL |
1236 | |
1237 | return ret; | |
1238 | } | |
1239 | ||
29d78271 SH |
1240 | static BlockDriverState *find_vmstate_bs(void) |
1241 | { | |
1242 | BlockDriverState *bs = NULL; | |
1243 | while ((bs = bdrv_next(bs))) { | |
1244 | if (bdrv_can_snapshot(bs)) { | |
1245 | return bs; | |
1246 | } | |
1247 | } | |
1248 | return NULL; | |
1249 | } | |
1250 | ||
cb499fb2 KW |
1251 | /* |
1252 | * Deletes snapshots of a given name in all opened images. | |
1253 | */ | |
1254 | static int del_existing_snapshots(Monitor *mon, const char *name) | |
1255 | { | |
1256 | BlockDriverState *bs; | |
cb499fb2 | 1257 | QEMUSnapshotInfo sn1, *snapshot = &sn1; |
a89d89d3 | 1258 | Error *err = NULL; |
cb499fb2 | 1259 | |
dbc13590 MA |
1260 | bs = NULL; |
1261 | while ((bs = bdrv_next(bs))) { | |
cb499fb2 | 1262 | if (bdrv_can_snapshot(bs) && |
38ff78d3 | 1263 | bdrv_snapshot_find(bs, snapshot, name) >= 0) { |
a89d89d3 | 1264 | bdrv_snapshot_delete_by_id_or_name(bs, name, &err); |
84d18f06 | 1265 | if (err) { |
cb499fb2 | 1266 | monitor_printf(mon, |
a89d89d3 WX |
1267 | "Error while deleting snapshot on device '%s':" |
1268 | " %s\n", | |
1269 | bdrv_get_device_name(bs), | |
1270 | error_get_pretty(err)); | |
1271 | error_free(err); | |
cb499fb2 KW |
1272 | return -1; |
1273 | } | |
1274 | } | |
1275 | } | |
1276 | ||
1277 | return 0; | |
1278 | } | |
1279 | ||
3e5a50d6 | 1280 | void hmp_savevm(Monitor *mon, const QDict *qdict) |
a672b469 AL |
1281 | { |
1282 | BlockDriverState *bs, *bs1; | |
1283 | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; | |
cb499fb2 | 1284 | int ret; |
a672b469 AL |
1285 | QEMUFile *f; |
1286 | int saved_vm_running; | |
c2c9a466 | 1287 | uint64_t vm_state_size; |
68b891ec | 1288 | qemu_timeval tv; |
7d631a11 | 1289 | struct tm tm; |
d54908a5 | 1290 | const char *name = qdict_get_try_str(qdict, "name"); |
5d80448c | 1291 | Error *local_err = NULL; |
a672b469 | 1292 | |
feeee5ac | 1293 | /* Verify if there is a device that doesn't support snapshots and is writable */ |
dbc13590 MA |
1294 | bs = NULL; |
1295 | while ((bs = bdrv_next(bs))) { | |
feeee5ac | 1296 | |
07b70bfb | 1297 | if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
feeee5ac MDCF |
1298 | continue; |
1299 | } | |
1300 | ||
1301 | if (!bdrv_can_snapshot(bs)) { | |
1302 | monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n", | |
1303 | bdrv_get_device_name(bs)); | |
1304 | return; | |
1305 | } | |
1306 | } | |
1307 | ||
29d78271 | 1308 | bs = find_vmstate_bs(); |
a672b469 | 1309 | if (!bs) { |
376253ec | 1310 | monitor_printf(mon, "No block device can accept snapshots\n"); |
a672b469 AL |
1311 | return; |
1312 | } | |
a672b469 | 1313 | |
1354869c | 1314 | saved_vm_running = runstate_is_running(); |
560d027b JQ |
1315 | |
1316 | ret = global_state_store(); | |
1317 | if (ret) { | |
1318 | monitor_printf(mon, "Error saving global state\n"); | |
1319 | return; | |
1320 | } | |
0461d5a6 | 1321 | vm_stop(RUN_STATE_SAVE_VM); |
a672b469 | 1322 | |
cb499fb2 | 1323 | memset(sn, 0, sizeof(*sn)); |
a672b469 AL |
1324 | |
1325 | /* fill auxiliary fields */ | |
68b891ec | 1326 | qemu_gettimeofday(&tv); |
a672b469 AL |
1327 | sn->date_sec = tv.tv_sec; |
1328 | sn->date_nsec = tv.tv_usec * 1000; | |
bc72ad67 | 1329 | sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
a672b469 | 1330 | |
7d631a11 MDCF |
1331 | if (name) { |
1332 | ret = bdrv_snapshot_find(bs, old_sn, name); | |
1333 | if (ret >= 0) { | |
1334 | pstrcpy(sn->name, sizeof(sn->name), old_sn->name); | |
1335 | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str); | |
1336 | } else { | |
1337 | pstrcpy(sn->name, sizeof(sn->name), name); | |
1338 | } | |
1339 | } else { | |
d7d9b528 BS |
1340 | /* cast below needed for OpenBSD where tv_sec is still 'long' */ |
1341 | localtime_r((const time_t *)&tv.tv_sec, &tm); | |
7d631a11 | 1342 | strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm); |
7d631a11 MDCF |
1343 | } |
1344 | ||
cb499fb2 | 1345 | /* Delete old snapshots of the same name */ |
f139a412 | 1346 | if (name && del_existing_snapshots(mon, name) < 0) { |
cb499fb2 KW |
1347 | goto the_end; |
1348 | } | |
1349 | ||
a672b469 | 1350 | /* save the VM state */ |
45566e9c | 1351 | f = qemu_fopen_bdrv(bs, 1); |
a672b469 | 1352 | if (!f) { |
376253ec | 1353 | monitor_printf(mon, "Could not open VM state file\n"); |
a672b469 AL |
1354 | goto the_end; |
1355 | } | |
5d80448c | 1356 | ret = qemu_savevm_state(f, &local_err); |
2d22b18f | 1357 | vm_state_size = qemu_ftell(f); |
a672b469 AL |
1358 | qemu_fclose(f); |
1359 | if (ret < 0) { | |
5d80448c KW |
1360 | monitor_printf(mon, "%s\n", error_get_pretty(local_err)); |
1361 | error_free(local_err); | |
a672b469 AL |
1362 | goto the_end; |
1363 | } | |
1364 | ||
1365 | /* create the snapshots */ | |
1366 | ||
dbc13590 MA |
1367 | bs1 = NULL; |
1368 | while ((bs1 = bdrv_next(bs1))) { | |
feeee5ac | 1369 | if (bdrv_can_snapshot(bs1)) { |
2d22b18f AL |
1370 | /* Write VM state size only to the image that contains the state */ |
1371 | sn->vm_state_size = (bs == bs1 ? vm_state_size : 0); | |
a672b469 AL |
1372 | ret = bdrv_snapshot_create(bs1, sn); |
1373 | if (ret < 0) { | |
376253ec AL |
1374 | monitor_printf(mon, "Error while creating snapshot on '%s'\n", |
1375 | bdrv_get_device_name(bs1)); | |
a672b469 AL |
1376 | } |
1377 | } | |
1378 | } | |
1379 | ||
1380 | the_end: | |
38ff78d3 | 1381 | if (saved_vm_running) { |
a672b469 | 1382 | vm_start(); |
38ff78d3 | 1383 | } |
a672b469 AL |
1384 | } |
1385 | ||
a7ae8355 SS |
1386 | void qmp_xen_save_devices_state(const char *filename, Error **errp) |
1387 | { | |
1388 | QEMUFile *f; | |
1389 | int saved_vm_running; | |
1390 | int ret; | |
1391 | ||
1392 | saved_vm_running = runstate_is_running(); | |
1393 | vm_stop(RUN_STATE_SAVE_VM); | |
c69adea4 | 1394 | global_state_store_running(); |
a7ae8355 SS |
1395 | |
1396 | f = qemu_fopen(filename, "wb"); | |
1397 | if (!f) { | |
1befce96 | 1398 | error_setg_file_open(errp, errno, filename); |
a7ae8355 SS |
1399 | goto the_end; |
1400 | } | |
1401 | ret = qemu_save_device_state(f); | |
1402 | qemu_fclose(f); | |
1403 | if (ret < 0) { | |
c6bd8c70 | 1404 | error_setg(errp, QERR_IO_ERROR); |
a7ae8355 SS |
1405 | } |
1406 | ||
1407 | the_end: | |
38ff78d3 | 1408 | if (saved_vm_running) { |
a7ae8355 | 1409 | vm_start(); |
38ff78d3 | 1410 | } |
a7ae8355 SS |
1411 | } |
1412 | ||
03cd4655 | 1413 | int load_vmstate(const char *name) |
a672b469 | 1414 | { |
f0aa7a8b | 1415 | BlockDriverState *bs, *bs_vm_state; |
2d22b18f | 1416 | QEMUSnapshotInfo sn; |
a672b469 | 1417 | QEMUFile *f; |
751c6a17 | 1418 | int ret; |
a672b469 | 1419 | |
29d78271 | 1420 | bs_vm_state = find_vmstate_bs(); |
f0aa7a8b MDCF |
1421 | if (!bs_vm_state) { |
1422 | error_report("No block device supports snapshots"); | |
1423 | return -ENOTSUP; | |
1424 | } | |
1425 | ||
1426 | /* Don't even try to load empty VM states */ | |
1427 | ret = bdrv_snapshot_find(bs_vm_state, &sn, name); | |
1428 | if (ret < 0) { | |
1429 | return ret; | |
1430 | } else if (sn.vm_state_size == 0) { | |
e11480db KW |
1431 | error_report("This is a disk-only snapshot. Revert to it offline " |
1432 | "using qemu-img."); | |
f0aa7a8b MDCF |
1433 | return -EINVAL; |
1434 | } | |
1435 | ||
1436 | /* Verify if there is any device that doesn't support snapshots and is | |
1437 | writable and check if the requested snapshot is available too. */ | |
dbc13590 MA |
1438 | bs = NULL; |
1439 | while ((bs = bdrv_next(bs))) { | |
feeee5ac | 1440 | |
07b70bfb | 1441 | if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { |
feeee5ac MDCF |
1442 | continue; |
1443 | } | |
1444 | ||
1445 | if (!bdrv_can_snapshot(bs)) { | |
1446 | error_report("Device '%s' is writable but does not support snapshots.", | |
1447 | bdrv_get_device_name(bs)); | |
1448 | return -ENOTSUP; | |
1449 | } | |
feeee5ac | 1450 | |
f0aa7a8b MDCF |
1451 | ret = bdrv_snapshot_find(bs, &sn, name); |
1452 | if (ret < 0) { | |
1453 | error_report("Device '%s' does not have the requested snapshot '%s'", | |
1454 | bdrv_get_device_name(bs), name); | |
1455 | return ret; | |
1456 | } | |
a672b469 AL |
1457 | } |
1458 | ||
1459 | /* Flush all IO requests so they don't interfere with the new state. */ | |
922453bc | 1460 | bdrv_drain_all(); |
a672b469 | 1461 | |
f0aa7a8b MDCF |
1462 | bs = NULL; |
1463 | while ((bs = bdrv_next(bs))) { | |
1464 | if (bdrv_can_snapshot(bs)) { | |
1465 | ret = bdrv_snapshot_goto(bs, name); | |
a672b469 | 1466 | if (ret < 0) { |
f0aa7a8b MDCF |
1467 | error_report("Error %d while activating snapshot '%s' on '%s'", |
1468 | ret, name, bdrv_get_device_name(bs)); | |
1469 | return ret; | |
a672b469 AL |
1470 | } |
1471 | } | |
1472 | } | |
1473 | ||
a672b469 | 1474 | /* restore the VM state */ |
f0aa7a8b | 1475 | f = qemu_fopen_bdrv(bs_vm_state, 0); |
a672b469 | 1476 | if (!f) { |
1ecda02b | 1477 | error_report("Could not open VM state file"); |
05f2401e | 1478 | return -EINVAL; |
a672b469 | 1479 | } |
f0aa7a8b | 1480 | |
5a8a49d7 | 1481 | qemu_system_reset(VMRESET_SILENT); |
bca7856a | 1482 | migration_incoming_state_new(f); |
a672b469 | 1483 | ret = qemu_loadvm_state(f); |
f0aa7a8b | 1484 | |
a672b469 | 1485 | qemu_fclose(f); |
bca7856a | 1486 | migration_incoming_state_destroy(); |
a672b469 | 1487 | if (ret < 0) { |
1ecda02b | 1488 | error_report("Error %d while loading VM state", ret); |
05f2401e | 1489 | return ret; |
a672b469 | 1490 | } |
f0aa7a8b | 1491 | |
05f2401e | 1492 | return 0; |
7b630349 JQ |
1493 | } |
1494 | ||
3e5a50d6 | 1495 | void hmp_delvm(Monitor *mon, const QDict *qdict) |
a672b469 | 1496 | { |
af957387 | 1497 | BlockDriverState *bs; |
ba2b2288 | 1498 | Error *err; |
d54908a5 | 1499 | const char *name = qdict_get_str(qdict, "name"); |
a672b469 | 1500 | |
af957387 | 1501 | if (!find_vmstate_bs()) { |
376253ec | 1502 | monitor_printf(mon, "No block device supports snapshots\n"); |
a672b469 AL |
1503 | return; |
1504 | } | |
1505 | ||
af957387 ZH |
1506 | bs = NULL; |
1507 | while ((bs = bdrv_next(bs))) { | |
1508 | if (bdrv_can_snapshot(bs)) { | |
ba2b2288 | 1509 | err = NULL; |
a89d89d3 | 1510 | bdrv_snapshot_delete_by_id_or_name(bs, name, &err); |
84d18f06 | 1511 | if (err) { |
a89d89d3 WX |
1512 | monitor_printf(mon, |
1513 | "Error while deleting snapshot on device '%s':" | |
1514 | " %s\n", | |
1515 | bdrv_get_device_name(bs), | |
1516 | error_get_pretty(err)); | |
1517 | error_free(err); | |
a672b469 AL |
1518 | } |
1519 | } | |
1520 | } | |
1521 | } | |
1522 | ||
1ce6be24 | 1523 | void hmp_info_snapshots(Monitor *mon, const QDict *qdict) |
a672b469 AL |
1524 | { |
1525 | BlockDriverState *bs, *bs1; | |
f9209915 MDCF |
1526 | QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s; |
1527 | int nb_sns, i, ret, available; | |
1528 | int total; | |
1529 | int *available_snapshots; | |
a672b469 | 1530 | |
29d78271 | 1531 | bs = find_vmstate_bs(); |
a672b469 | 1532 | if (!bs) { |
376253ec | 1533 | monitor_printf(mon, "No available block device supports snapshots\n"); |
a672b469 AL |
1534 | return; |
1535 | } | |
a672b469 AL |
1536 | |
1537 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); | |
1538 | if (nb_sns < 0) { | |
376253ec | 1539 | monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns); |
a672b469 AL |
1540 | return; |
1541 | } | |
f9209915 MDCF |
1542 | |
1543 | if (nb_sns == 0) { | |
1544 | monitor_printf(mon, "There is no snapshot available.\n"); | |
1545 | return; | |
1546 | } | |
1547 | ||
97f3ad35 | 1548 | available_snapshots = g_new0(int, nb_sns); |
f9209915 MDCF |
1549 | total = 0; |
1550 | for (i = 0; i < nb_sns; i++) { | |
a672b469 | 1551 | sn = &sn_tab[i]; |
f9209915 MDCF |
1552 | available = 1; |
1553 | bs1 = NULL; | |
1554 | ||
1555 | while ((bs1 = bdrv_next(bs1))) { | |
1556 | if (bdrv_can_snapshot(bs1) && bs1 != bs) { | |
1557 | ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str); | |
1558 | if (ret < 0) { | |
1559 | available = 0; | |
1560 | break; | |
1561 | } | |
1562 | } | |
1563 | } | |
1564 | ||
1565 | if (available) { | |
1566 | available_snapshots[total] = i; | |
1567 | total++; | |
1568 | } | |
a672b469 | 1569 | } |
f9209915 MDCF |
1570 | |
1571 | if (total > 0) { | |
5b917044 WX |
1572 | bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL); |
1573 | monitor_printf(mon, "\n"); | |
f9209915 MDCF |
1574 | for (i = 0; i < total; i++) { |
1575 | sn = &sn_tab[available_snapshots[i]]; | |
5b917044 WX |
1576 | bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn); |
1577 | monitor_printf(mon, "\n"); | |
f9209915 MDCF |
1578 | } |
1579 | } else { | |
1580 | monitor_printf(mon, "There is no suitable snapshot available\n"); | |
1581 | } | |
1582 | ||
7267c094 AL |
1583 | g_free(sn_tab); |
1584 | g_free(available_snapshots); | |
f9209915 | 1585 | |
a672b469 | 1586 | } |
c5705a77 AK |
1587 | |
1588 | void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev) | |
1589 | { | |
1ddde087 | 1590 | qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK, |
c5705a77 AK |
1591 | memory_region_name(mr), dev); |
1592 | } | |
1593 | ||
1594 | void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev) | |
1595 | { | |
b0e56e0b | 1596 | qemu_ram_unset_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK); |
c5705a77 AK |
1597 | } |
1598 | ||
1599 | void vmstate_register_ram_global(MemoryRegion *mr) | |
1600 | { | |
1601 | vmstate_register_ram(mr, NULL); | |
1602 | } |