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