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