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