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