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