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