]> git.proxmox.com Git - mirror_qemu.git/blame - migration/vmstate.c
migration: Make VMStateDescription.subsections const
[mirror_qemu.git] / migration / vmstate.c
CommitLineData
576d1abc
JQ
1/*
2 * VMState interpreter
3 *
4 * Copyright (c) 2009-2017 Red Hat Inc
5 *
6 * Authors:
7 * Juan Quintela <quintela@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 */
12
1393a485 13#include "qemu/osdep.h"
6666c96a 14#include "migration.h"
b6fcfa59 15#include "migration/vmstate.h"
53d37d36 16#include "savevm.h"
848a0503 17#include "qapi/error.h"
3ddba9a9 18#include "qapi/qmp/json-writer.h"
08a0aee1 19#include "qemu-file.h"
b6fcfa59 20#include "qemu/bitops.h"
6a64b644 21#include "qemu/error-report.h"
9013dca5 22#include "trace.h"
b6fcfa59 23
f3cadd39 24static int vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
3ddba9a9 25 void *opaque, JSONWriter *vmdesc);
b6fcfa59
EH
26static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
27 void *opaque);
28
579cedf4
PX
29/* Whether this field should exist for either save or load the VM? */
30static bool
31vmstate_field_exists(const VMStateDescription *vmsd, const VMStateField *field,
32 void *opaque, int version_id)
33{
34 bool result;
35
36 if (field->field_exists) {
37 /* If there's the function checker, that's the solo truth */
38 result = field->field_exists(opaque, version_id);
39 trace_vmstate_field_exists(vmsd->name, field->name, field->version_id,
40 version_id, result);
41 } else {
42 /*
43 * Otherwise, we only save/load if field version is same or older.
44 * For example, when loading from an old binary with old version,
45 * we ignore new fields with newer version_ids.
46 */
47 result = field->version_id <= version_id;
48 }
49
50 return result;
51}
52
03fee66f 53static int vmstate_n_elems(void *opaque, const VMStateField *field)
35fc1f71
MT
54{
55 int n_elems = 1;
56
57 if (field->flags & VMS_ARRAY) {
58 n_elems = field->num;
59 } else if (field->flags & VMS_VARRAY_INT32) {
395cb450 60 n_elems = *(int32_t *)(opaque + field->num_offset);
35fc1f71 61 } else if (field->flags & VMS_VARRAY_UINT32) {
395cb450 62 n_elems = *(uint32_t *)(opaque + field->num_offset);
35fc1f71 63 } else if (field->flags & VMS_VARRAY_UINT16) {
395cb450 64 n_elems = *(uint16_t *)(opaque + field->num_offset);
35fc1f71 65 } else if (field->flags & VMS_VARRAY_UINT8) {
395cb450 66 n_elems = *(uint8_t *)(opaque + field->num_offset);
35fc1f71
MT
67 }
68
b47d3af7
JQ
69 if (field->flags & VMS_MULTIPLY_ELEMENTS) {
70 n_elems *= field->num;
71 }
72
023ad1a6 73 trace_vmstate_n_elems(field->name, n_elems);
35fc1f71
MT
74 return n_elems;
75}
76
03fee66f 77static int vmstate_size(void *opaque, const VMStateField *field)
35fc1f71
MT
78{
79 int size = field->size;
80
81 if (field->flags & VMS_VBUFFER) {
395cb450 82 size = *(int32_t *)(opaque + field->size_offset);
35fc1f71
MT
83 if (field->flags & VMS_MULTIPLY) {
84 size *= field->size;
85 }
86 }
87
88 return size;
89}
90
03fee66f
MAL
91static void vmstate_handle_alloc(void *ptr, const VMStateField *field,
92 void *opaque)
35fc1f71 93{
cbfda0e6
HP
94 if (field->flags & VMS_POINTER && field->flags & VMS_ALLOC) {
95 gsize size = vmstate_size(opaque, field);
96 size *= vmstate_n_elems(opaque, field);
97 if (size) {
98 *(void **)ptr = g_malloc(size);
f32935ea 99 }
35fc1f71 100 }
35fc1f71
MT
101}
102
b6fcfa59
EH
103int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
104 void *opaque, int version_id)
105{
03fee66f 106 const VMStateField *field = vmsd->fields;
a5df2a02 107 int ret = 0;
b6fcfa59 108
a5df2a02 109 trace_vmstate_load_state(vmsd->name, version_id);
b6fcfa59 110 if (version_id > vmsd->version_id) {
cde7ee59
JD
111 error_report("%s: incoming version_id %d is too new "
112 "for local version_id %d",
113 vmsd->name, version_id, vmsd->version_id);
a5df2a02 114 trace_vmstate_load_state_end(vmsd->name, "too new", -EINVAL);
b6fcfa59
EH
115 return -EINVAL;
116 }
b6fcfa59 117 if (version_id < vmsd->minimum_version_id) {
cde7ee59
JD
118 error_report("%s: incoming version_id %d is too old "
119 "for local minimum version_id %d",
120 vmsd->name, version_id, vmsd->minimum_version_id);
a5df2a02 121 trace_vmstate_load_state_end(vmsd->name, "too old", -EINVAL);
767adce2 122 return -EINVAL;
b6fcfa59
EH
123 }
124 if (vmsd->pre_load) {
7f3de3f0 125 ret = vmsd->pre_load(opaque);
b6fcfa59
EH
126 if (ret) {
127 return ret;
128 }
129 }
130 while (field->name) {
a5df2a02 131 trace_vmstate_load_state_field(vmsd->name, field->name);
579cedf4 132 if (vmstate_field_exists(vmsd, field, opaque, version_id)) {
cbfda0e6 133 void *first_elem = opaque + field->offset;
35fc1f71
MT
134 int i, n_elems = vmstate_n_elems(opaque, field);
135 int size = vmstate_size(opaque, field);
136
cbfda0e6
HP
137 vmstate_handle_alloc(first_elem, field, opaque);
138 if (field->flags & VMS_POINTER) {
139 first_elem = *(void **)first_elem;
e1e686c1 140 assert(first_elem || !n_elems || !size);
cbfda0e6 141 }
b6fcfa59 142 for (i = 0; i < n_elems; i++) {
e84641f7 143 void *curr_elem = first_elem + size * i;
b6fcfa59
EH
144
145 if (field->flags & VMS_ARRAY_OF_POINTER) {
e84641f7 146 curr_elem = *(void **)curr_elem;
b6fcfa59 147 }
e1e686c1 148 if (!curr_elem && size) {
07d4e691
HP
149 /* if null pointer check placeholder and do not follow */
150 assert(field->flags & VMS_ARRAY_OF_POINTER);
151 ret = vmstate_info_nullptr.get(f, curr_elem, size, NULL);
152 } else if (field->flags & VMS_STRUCT) {
e84641f7 153 ret = vmstate_load_state(f, field->vmsd, curr_elem,
b6fcfa59 154 field->vmsd->version_id);
2dc6660b
CM
155 } else if (field->flags & VMS_VSTRUCT) {
156 ret = vmstate_load_state(f, field->vmsd, curr_elem,
157 field->struct_version_id);
b6fcfa59 158 } else {
e84641f7 159 ret = field->info->get(f, curr_elem, size, field);
b6fcfa59 160 }
13cde508
JQ
161 if (ret >= 0) {
162 ret = qemu_file_get_error(f);
163 }
b6fcfa59 164 if (ret < 0) {
13cde508 165 qemu_file_set_error(f, ret);
a1771070
DDAG
166 error_report("Failed to load %s:%s", vmsd->name,
167 field->name);
9013dca5 168 trace_vmstate_load_field_error(field->name, ret);
b6fcfa59
EH
169 return ret;
170 }
171 }
5bf81c8d 172 } else if (field->flags & VMS_MUST_EXIST) {
6a64b644
DDAG
173 error_report("Input validation failed: %s/%s",
174 vmsd->name, field->name);
5bf81c8d 175 return -1;
b6fcfa59
EH
176 }
177 field++;
178 }
89c56848 179 assert(field->flags == VMS_END);
b6fcfa59
EH
180 ret = vmstate_subsection_load(f, vmsd, opaque);
181 if (ret != 0) {
cd4c0da6 182 qemu_file_set_error(f, ret);
b6fcfa59
EH
183 return ret;
184 }
185 if (vmsd->post_load) {
a5df2a02 186 ret = vmsd->post_load(opaque, version_id);
b6fcfa59 187 }
a5df2a02
DDAG
188 trace_vmstate_load_state_end(vmsd->name, "end", ret);
189 return ret;
b6fcfa59
EH
190}
191
03fee66f
MAL
192static int vmfield_name_num(const VMStateField *start,
193 const VMStateField *search)
8118f095 194{
03fee66f 195 const VMStateField *field;
8118f095
AG
196 int found = 0;
197
198 for (field = start; field->name; field++) {
199 if (!strcmp(field->name, search->name)) {
200 if (field == search) {
201 return found;
202 }
203 found++;
204 }
205 }
206
207 return -1;
208}
209
03fee66f
MAL
210static bool vmfield_name_is_unique(const VMStateField *start,
211 const VMStateField *search)
8118f095 212{
03fee66f 213 const VMStateField *field;
8118f095
AG
214 int found = 0;
215
216 for (field = start; field->name; field++) {
217 if (!strcmp(field->name, search->name)) {
218 found++;
219 /* name found more than once, so it's not unique */
220 if (found > 1) {
221 return false;
222 }
223 }
224 }
225
226 return true;
227}
228
03fee66f 229static const char *vmfield_get_type_name(const VMStateField *field)
8118f095
AG
230{
231 const char *type = "unknown";
232
233 if (field->flags & VMS_STRUCT) {
234 type = "struct";
2dc6660b
CM
235 } else if (field->flags & VMS_VSTRUCT) {
236 type = "vstruct";
8118f095
AG
237 } else if (field->info->name) {
238 type = field->info->name;
239 }
240
241 return type;
242}
243
03fee66f 244static bool vmsd_can_compress(const VMStateField *field)
8118f095
AG
245{
246 if (field->field_exists) {
247 /* Dynamically existing fields mess up compression */
248 return false;
249 }
250
251 if (field->flags & VMS_STRUCT) {
03fee66f 252 const VMStateField *sfield = field->vmsd->fields;
8118f095
AG
253 while (sfield->name) {
254 if (!vmsd_can_compress(sfield)) {
255 /* Child elements can't compress, so can't we */
256 return false;
257 }
258 sfield++;
259 }
260
261 if (field->vmsd->subsections) {
262 /* Subsections may come and go, better don't compress */
263 return false;
264 }
265 }
266
267 return true;
268}
269
3ddba9a9
MA
270static void vmsd_desc_field_start(const VMStateDescription *vmsd,
271 JSONWriter *vmdesc,
03fee66f 272 const VMStateField *field, int i, int max)
8118f095
AG
273{
274 char *name, *old_name;
275 bool is_array = max > 1;
276 bool can_compress = vmsd_can_compress(field);
277
278 if (!vmdesc) {
279 return;
280 }
281
282 name = g_strdup(field->name);
283
284 /* Field name is not unique, need to make it unique */
285 if (!vmfield_name_is_unique(vmsd->fields, field)) {
286 int num = vmfield_name_num(vmsd->fields, field);
287 old_name = name;
288 name = g_strdup_printf("%s[%d]", name, num);
289 g_free(old_name);
290 }
291
3ddba9a9
MA
292 json_writer_start_object(vmdesc, NULL);
293 json_writer_str(vmdesc, "name", name);
8118f095
AG
294 if (is_array) {
295 if (can_compress) {
3ddba9a9 296 json_writer_int64(vmdesc, "array_len", max);
8118f095 297 } else {
3ddba9a9 298 json_writer_int64(vmdesc, "index", i);
8118f095
AG
299 }
300 }
3ddba9a9 301 json_writer_str(vmdesc, "type", vmfield_get_type_name(field));
8118f095
AG
302
303 if (field->flags & VMS_STRUCT) {
3ddba9a9 304 json_writer_start_object(vmdesc, "struct");
8118f095
AG
305 }
306
307 g_free(name);
308}
309
3ddba9a9
MA
310static void vmsd_desc_field_end(const VMStateDescription *vmsd,
311 JSONWriter *vmdesc,
03fee66f 312 const VMStateField *field, size_t size, int i)
8118f095
AG
313{
314 if (!vmdesc) {
315 return;
316 }
317
318 if (field->flags & VMS_STRUCT) {
319 /* We printed a struct in between, close its child object */
3ddba9a9 320 json_writer_end_object(vmdesc);
8118f095
AG
321 }
322
3ddba9a9
MA
323 json_writer_int64(vmdesc, "size", size);
324 json_writer_end_object(vmdesc);
8118f095
AG
325}
326
df896152 327
4d8bdc2a 328bool vmstate_section_needed(const VMStateDescription *vmsd, void *opaque)
df896152
JQ
329{
330 if (vmsd->needed && !vmsd->needed(opaque)) {
331 /* optional section not needed */
332 return false;
333 }
334 return true;
335}
336
337
551dbd08 338int vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
3ddba9a9 339 void *opaque, JSONWriter *vmdesc_id)
2dc6660b 340{
969298f9
TG
341 return vmstate_save_state_v(f, vmsd, opaque, vmdesc_id, vmsd->version_id, NULL);
342}
343
344int vmstate_save_state_with_err(QEMUFile *f, const VMStateDescription *vmsd,
345 void *opaque, JSONWriter *vmdesc_id, Error **errp)
346{
347 return vmstate_save_state_v(f, vmsd, opaque, vmdesc_id, vmsd->version_id, errp);
2dc6660b
CM
348}
349
350int vmstate_save_state_v(QEMUFile *f, const VMStateDescription *vmsd,
969298f9 351 void *opaque, JSONWriter *vmdesc, int version_id, Error **errp)
b6fcfa59 352{
551dbd08 353 int ret = 0;
03fee66f 354 const VMStateField *field = vmsd->fields;
b6fcfa59 355
46a02a74
DDAG
356 trace_vmstate_save_state_top(vmsd->name);
357
b6fcfa59 358 if (vmsd->pre_save) {
551dbd08
DDAG
359 ret = vmsd->pre_save(opaque);
360 trace_vmstate_save_state_pre_save_res(vmsd->name, ret);
361 if (ret) {
848a0503 362 error_setg(errp, "pre-save failed: %s", vmsd->name);
551dbd08
DDAG
363 return ret;
364 }
b6fcfa59 365 }
8118f095
AG
366
367 if (vmdesc) {
3ddba9a9
MA
368 json_writer_str(vmdesc, "vmsd_name", vmsd->name);
369 json_writer_int64(vmdesc, "version", version_id);
370 json_writer_start_array(vmdesc, "fields");
8118f095
AG
371 }
372
b6fcfa59 373 while (field->name) {
579cedf4 374 if (vmstate_field_exists(vmsd, field, opaque, version_id)) {
cbfda0e6 375 void *first_elem = opaque + field->offset;
35fc1f71
MT
376 int i, n_elems = vmstate_n_elems(opaque, field);
377 int size = vmstate_size(opaque, field);
61abf1eb 378 uint64_t old_offset, written_bytes;
3ddba9a9 379 JSONWriter *vmdesc_loop = vmdesc;
35fc1f71 380
46a02a74 381 trace_vmstate_save_state_loop(vmsd->name, field->name, n_elems);
cbfda0e6
HP
382 if (field->flags & VMS_POINTER) {
383 first_elem = *(void **)first_elem;
e1e686c1 384 assert(first_elem || !n_elems || !size);
cbfda0e6 385 }
b6fcfa59 386 for (i = 0; i < n_elems; i++) {
e84641f7 387 void *curr_elem = first_elem + size * i;
b6fcfa59 388
8118f095 389 vmsd_desc_field_start(vmsd, vmdesc_loop, field, i, n_elems);
e9c0eed7 390 old_offset = qemu_file_transferred(f);
b6fcfa59 391 if (field->flags & VMS_ARRAY_OF_POINTER) {
e84641f7
HP
392 assert(curr_elem);
393 curr_elem = *(void **)curr_elem;
b6fcfa59 394 }
e1e686c1 395 if (!curr_elem && size) {
07d4e691
HP
396 /* if null pointer write placeholder and do not follow */
397 assert(field->flags & VMS_ARRAY_OF_POINTER);
88b0faf1
DDAG
398 ret = vmstate_info_nullptr.put(f, curr_elem, size, NULL,
399 NULL);
07d4e691 400 } else if (field->flags & VMS_STRUCT) {
88b0faf1
DDAG
401 ret = vmstate_save_state(f, field->vmsd, curr_elem,
402 vmdesc_loop);
2dc6660b
CM
403 } else if (field->flags & VMS_VSTRUCT) {
404 ret = vmstate_save_state_v(f, field->vmsd, curr_elem,
405 vmdesc_loop,
969298f9 406 field->struct_version_id, errp);
b6fcfa59 407 } else {
88b0faf1
DDAG
408 ret = field->info->put(f, curr_elem, size, field,
409 vmdesc_loop);
410 }
411 if (ret) {
848a0503
TG
412 error_setg(errp, "Save of field %s/%s failed",
413 vmsd->name, field->name);
8c07559f
AL
414 if (vmsd->post_save) {
415 vmsd->post_save(opaque);
416 }
88b0faf1 417 return ret;
b6fcfa59 418 }
8118f095 419
e9c0eed7 420 written_bytes = qemu_file_transferred(f) - old_offset;
8118f095
AG
421 vmsd_desc_field_end(vmsd, vmdesc_loop, field, written_bytes, i);
422
423 /* Compressed arrays only care about the first element */
424 if (vmdesc_loop && vmsd_can_compress(field)) {
425 vmdesc_loop = NULL;
426 }
b6fcfa59 427 }
5bf81c8d
MT
428 } else {
429 if (field->flags & VMS_MUST_EXIST) {
6a64b644 430 error_report("Output state validation failed: %s/%s",
5bf81c8d
MT
431 vmsd->name, field->name);
432 assert(!(field->flags & VMS_MUST_EXIST));
433 }
b6fcfa59
EH
434 }
435 field++;
436 }
89c56848 437 assert(field->flags == VMS_END);
8118f095
AG
438
439 if (vmdesc) {
3ddba9a9 440 json_writer_end_array(vmdesc);
8118f095
AG
441 }
442
8c07559f
AL
443 ret = vmstate_subsection_save(f, vmsd, opaque, vmdesc);
444
445 if (vmsd->post_save) {
446 int ps_ret = vmsd->post_save(opaque);
447 if (!ret) {
448 ret = ps_ret;
449 }
450 }
451 return ret;
b6fcfa59
EH
452}
453
454static const VMStateDescription *
20270019
RH
455vmstate_get_subsection(const VMStateDescription * const *sub,
456 const char *idstr)
b6fcfa59 457{
20270019
RH
458 if (sub) {
459 for (const VMStateDescription *s = *sub; s ; s = *++sub) {
460 if (strcmp(idstr, s->name) == 0) {
461 return s;
462 }
b6fcfa59 463 }
b6fcfa59
EH
464 }
465 return NULL;
466}
467
468static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
469 void *opaque)
470{
a5df2a02
DDAG
471 trace_vmstate_subsection_load(vmsd->name);
472
b6fcfa59 473 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
7c1e52ba 474 char idstr[256], *idstr_ret;
b6fcfa59
EH
475 int ret;
476 uint8_t version_id, len, size;
477 const VMStateDescription *sub_vmsd;
478
479 len = qemu_peek_byte(f, 1);
480 if (len < strlen(vmsd->name) + 1) {
481 /* subsection name has be be "section_name/a" */
023ad1a6 482 trace_vmstate_subsection_load_bad(vmsd->name, "(short)", "");
b6fcfa59
EH
483 return 0;
484 }
7c1e52ba 485 size = qemu_peek_buffer(f, (uint8_t **)&idstr_ret, len, 2);
b6fcfa59 486 if (size != len) {
023ad1a6 487 trace_vmstate_subsection_load_bad(vmsd->name, "(peek fail)", "");
b6fcfa59
EH
488 return 0;
489 }
7c1e52ba 490 memcpy(idstr, idstr_ret, size);
b6fcfa59
EH
491 idstr[size] = 0;
492
493 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
023ad1a6
DDAG
494 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(prefix)");
495 /* it doesn't have a valid subsection name */
b6fcfa59
EH
496 return 0;
497 }
498 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
499 if (sub_vmsd == NULL) {
023ad1a6 500 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(lookup)");
b6fcfa59
EH
501 return -ENOENT;
502 }
503 qemu_file_skip(f, 1); /* subsection */
504 qemu_file_skip(f, 1); /* len */
505 qemu_file_skip(f, len); /* idstr */
506 version_id = qemu_get_be32(f);
507
508 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
509 if (ret) {
023ad1a6 510 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(child)");
b6fcfa59
EH
511 return ret;
512 }
513 }
a5df2a02
DDAG
514
515 trace_vmstate_subsection_load_good(vmsd->name);
b6fcfa59
EH
516 return 0;
517}
518
f3cadd39 519static int vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
3ddba9a9 520 void *opaque, JSONWriter *vmdesc)
b6fcfa59 521{
20270019 522 const VMStateDescription * const *sub = vmsd->subsections;
f2dd7edd 523 bool vmdesc_has_subsections = false;
f3cadd39 524 int ret = 0;
b6fcfa59 525
46a02a74 526 trace_vmstate_subsection_save_top(vmsd->name);
6f4923fc 527 while (sub && *sub) {
4d8bdc2a 528 if (vmstate_section_needed(*sub, opaque)) {
46a02a74 529 const VMStateDescription *vmsdsub = *sub;
b6fcfa59
EH
530 uint8_t len;
531
46a02a74 532 trace_vmstate_subsection_save_loop(vmsd->name, vmsdsub->name);
8118f095
AG
533 if (vmdesc) {
534 /* Only create subsection array when we have any */
f2dd7edd 535 if (!vmdesc_has_subsections) {
3ddba9a9 536 json_writer_start_array(vmdesc, "subsections");
f2dd7edd 537 vmdesc_has_subsections = true;
8118f095
AG
538 }
539
3ddba9a9 540 json_writer_start_object(vmdesc, NULL);
8118f095
AG
541 }
542
b6fcfa59 543 qemu_put_byte(f, QEMU_VM_SUBSECTION);
46a02a74 544 len = strlen(vmsdsub->name);
b6fcfa59 545 qemu_put_byte(f, len);
46a02a74
DDAG
546 qemu_put_buffer(f, (uint8_t *)vmsdsub->name, len);
547 qemu_put_be32(f, vmsdsub->version_id);
f3cadd39
DDAG
548 ret = vmstate_save_state(f, vmsdsub, opaque, vmdesc);
549 if (ret) {
550 return ret;
551 }
8118f095
AG
552
553 if (vmdesc) {
3ddba9a9 554 json_writer_end_object(vmdesc);
8118f095 555 }
b6fcfa59
EH
556 }
557 sub++;
558 }
8118f095 559
f2dd7edd 560 if (vmdesc_has_subsections) {
3ddba9a9 561 json_writer_end_array(vmdesc);
8118f095 562 }
f3cadd39
DDAG
563
564 return ret;
b6fcfa59 565}