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