xref: /openbmc/qemu/qapi/qapi-visit-core.c (revision c8688760)
1 /*
2  * Core Definitions for QAPI Visitor Classes
3  *
4  * Copyright (C) 2012-2016 Red Hat, Inc.
5  * Copyright IBM, Corp. 2011
6  *
7  * Authors:
8  *  Anthony Liguori   <aliguori@us.ibm.com>
9  *
10  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
11  * See the COPYING.LIB file in the top-level directory.
12  *
13  */
14 
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qerror.h"
18 #include "qapi/visitor.h"
19 #include "qapi/visitor-impl.h"
20 #include "trace.h"
21 
22 /* Zero-initialization must result in default policy */
23 QEMU_BUILD_BUG_ON(COMPAT_POLICY_INPUT_ACCEPT || COMPAT_POLICY_OUTPUT_ACCEPT);
24 
25 
26 void visit_complete(Visitor *v, void *opaque)
27 {
28     assert(v->type != VISITOR_OUTPUT || v->complete);
29     trace_visit_complete(v, opaque);
30     if (v->complete) {
31         v->complete(v, opaque);
32     }
33 }
34 
35 void visit_free(Visitor *v)
36 {
37     trace_visit_free(v);
38     if (v) {
39         v->free(v);
40     }
41 }
42 
43 bool visit_start_struct(Visitor *v, const char *name, void **obj,
44                         size_t size, Error **errp)
45 {
46     bool ok;
47 
48     trace_visit_start_struct(v, name, obj, size);
49     if (obj) {
50         assert(size);
51         assert(!(v->type & VISITOR_OUTPUT) || *obj);
52     }
53     ok = v->start_struct(v, name, obj, size, errp);
54     if (obj && (v->type & VISITOR_INPUT)) {
55         assert(ok != !*obj);
56     }
57     return ok;
58 }
59 
60 bool visit_check_struct(Visitor *v, Error **errp)
61 {
62     trace_visit_check_struct(v);
63     return v->check_struct ? v->check_struct(v, errp) : true;
64 }
65 
66 void visit_end_struct(Visitor *v, void **obj)
67 {
68     trace_visit_end_struct(v, obj);
69     v->end_struct(v, obj);
70 }
71 
72 bool visit_start_list(Visitor *v, const char *name, GenericList **list,
73                       size_t size, Error **errp)
74 {
75     bool ok;
76 
77     assert(!list || size >= sizeof(GenericList));
78     trace_visit_start_list(v, name, list, size);
79     ok = v->start_list(v, name, list, size, errp);
80     if (list && (v->type & VISITOR_INPUT)) {
81         assert(ok || !*list);
82     }
83     return ok;
84 }
85 
86 GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size)
87 {
88     assert(tail && size >= sizeof(GenericList));
89     trace_visit_next_list(v, tail, size);
90     return v->next_list(v, tail, size);
91 }
92 
93 bool visit_check_list(Visitor *v, Error **errp)
94 {
95     trace_visit_check_list(v);
96     return v->check_list ? v->check_list(v, errp) : true;
97 }
98 
99 void visit_end_list(Visitor *v, void **obj)
100 {
101     trace_visit_end_list(v, obj);
102     v->end_list(v, obj);
103 }
104 
105 bool visit_start_alternate(Visitor *v, const char *name,
106                            GenericAlternate **obj, size_t size,
107                            Error **errp)
108 {
109     bool ok;
110 
111     assert(obj && size >= sizeof(GenericAlternate));
112     assert(!(v->type & VISITOR_OUTPUT) || *obj);
113     trace_visit_start_alternate(v, name, obj, size);
114     if (!v->start_alternate) {
115         assert(!(v->type & VISITOR_INPUT));
116         return true;
117     }
118     ok = v->start_alternate(v, name, obj, size, errp);
119     if (v->type & VISITOR_INPUT) {
120         assert(ok != !*obj);
121     }
122     return ok;
123 }
124 
125 void visit_end_alternate(Visitor *v, void **obj)
126 {
127     trace_visit_end_alternate(v, obj);
128     if (v->end_alternate) {
129         v->end_alternate(v, obj);
130     }
131 }
132 
133 bool visit_optional(Visitor *v, const char *name, bool *present)
134 {
135     trace_visit_optional(v, name, present);
136     if (v->optional) {
137         v->optional(v, name, present);
138     }
139     return *present;
140 }
141 
142 bool visit_policy_reject(Visitor *v, const char *name,
143                          unsigned special_features, Error **errp)
144 {
145     trace_visit_policy_reject(v, name);
146     if (v->policy_reject) {
147         return v->policy_reject(v, name, special_features, errp);
148     }
149     return false;
150 }
151 
152 bool visit_policy_skip(Visitor *v, const char *name,
153                        unsigned special_features)
154 {
155     trace_visit_policy_skip(v, name);
156     if (v->policy_skip) {
157         return v->policy_skip(v, name, special_features);
158     }
159     return false;
160 }
161 
162 void visit_set_policy(Visitor *v, CompatPolicy *policy)
163 {
164     v->compat_policy = *policy;
165 }
166 
167 bool visit_is_input(Visitor *v)
168 {
169     return v->type == VISITOR_INPUT;
170 }
171 
172 bool visit_is_dealloc(Visitor *v)
173 {
174     return v->type == VISITOR_DEALLOC;
175 }
176 
177 bool visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
178 {
179     assert(obj);
180     trace_visit_type_int(v, name, obj);
181     return v->type_int64(v, name, obj, errp);
182 }
183 
184 static bool visit_type_uintN(Visitor *v, uint64_t *obj, const char *name,
185                              uint64_t max, const char *type, Error **errp)
186 {
187     uint64_t value = *obj;
188 
189     assert(v->type == VISITOR_INPUT || value <= max);
190 
191     if (!v->type_uint64(v, name, &value, errp)) {
192         return false;
193     }
194     if (value > max) {
195         assert(v->type == VISITOR_INPUT);
196         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
197                    name ? name : "null", type);
198         return false;
199     }
200     *obj = value;
201     return true;
202 }
203 
204 bool visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
205                       Error **errp)
206 {
207     uint64_t value;
208     bool ok;
209 
210     trace_visit_type_uint8(v, name, obj);
211     value = *obj;
212     ok = visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp);
213     *obj = value;
214     return ok;
215 }
216 
217 bool visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
218                        Error **errp)
219 {
220     uint64_t value;
221     bool ok;
222 
223     trace_visit_type_uint16(v, name, obj);
224     value = *obj;
225     ok = visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp);
226     *obj = value;
227     return ok;
228 }
229 
230 bool visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
231                        Error **errp)
232 {
233     uint64_t value;
234     bool ok;
235 
236     trace_visit_type_uint32(v, name, obj);
237     value = *obj;
238     ok = visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp);
239     *obj = value;
240     return ok;
241 }
242 
243 bool visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
244                        Error **errp)
245 {
246     assert(obj);
247     trace_visit_type_uint64(v, name, obj);
248     return v->type_uint64(v, name, obj, errp);
249 }
250 
251 static bool visit_type_intN(Visitor *v, int64_t *obj, const char *name,
252                             int64_t min, int64_t max, const char *type,
253                             Error **errp)
254 {
255     int64_t value = *obj;
256 
257     assert(v->type == VISITOR_INPUT || (value >= min && value <= max));
258 
259     if (!v->type_int64(v, name, &value, errp)) {
260         return false;
261     }
262     if (value < min || value > max) {
263         assert(v->type == VISITOR_INPUT);
264         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
265                    name ? name : "null", type);
266         return false;
267     }
268     *obj = value;
269     return true;
270 }
271 
272 bool visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp)
273 {
274     int64_t value;
275     bool ok;
276 
277     trace_visit_type_int8(v, name, obj);
278     value = *obj;
279     ok = visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp);
280     *obj = value;
281     return ok;
282 }
283 
284 bool visit_type_int16(Visitor *v, const char *name, int16_t *obj,
285                       Error **errp)
286 {
287     int64_t value;
288     bool ok;
289 
290     trace_visit_type_int16(v, name, obj);
291     value = *obj;
292     ok = visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t",
293                          errp);
294     *obj = value;
295     return ok;
296 }
297 
298 bool visit_type_int32(Visitor *v, const char *name, int32_t *obj,
299                       Error **errp)
300 {
301     int64_t value;
302     bool ok;
303 
304     trace_visit_type_int32(v, name, obj);
305     value = *obj;
306     ok = visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t",
307                         errp);
308     *obj = value;
309     return ok;
310 }
311 
312 bool visit_type_int64(Visitor *v, const char *name, int64_t *obj,
313                       Error **errp)
314 {
315     assert(obj);
316     trace_visit_type_int64(v, name, obj);
317     return v->type_int64(v, name, obj, errp);
318 }
319 
320 bool visit_type_size(Visitor *v, const char *name, uint64_t *obj,
321                      Error **errp)
322 {
323     assert(obj);
324     trace_visit_type_size(v, name, obj);
325     if (v->type_size) {
326         return v->type_size(v, name, obj, errp);
327     }
328     return v->type_uint64(v, name, obj, errp);
329 }
330 
331 bool visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
332 {
333     assert(obj);
334     trace_visit_type_bool(v, name, obj);
335     return v->type_bool(v, name, obj, errp);
336 }
337 
338 bool visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)
339 {
340     bool ok;
341 
342     assert(obj);
343     /* TODO: Fix callers to not pass NULL when they mean "", so that we
344      * can enable:
345     assert(!(v->type & VISITOR_OUTPUT) || *obj);
346      */
347     trace_visit_type_str(v, name, obj);
348     ok = v->type_str(v, name, obj, errp);
349     if (v->type & VISITOR_INPUT) {
350         assert(ok != !*obj);
351     }
352     return ok;
353 }
354 
355 bool visit_type_number(Visitor *v, const char *name, double *obj,
356                        Error **errp)
357 {
358     assert(obj);
359     trace_visit_type_number(v, name, obj);
360     return v->type_number(v, name, obj, errp);
361 }
362 
363 bool visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)
364 {
365     bool ok;
366 
367     assert(obj);
368     assert(v->type != VISITOR_OUTPUT || *obj);
369     trace_visit_type_any(v, name, obj);
370     ok = v->type_any(v, name, obj, errp);
371     if (v->type == VISITOR_INPUT) {
372         assert(ok != !*obj);
373     }
374     return ok;
375 }
376 
377 bool visit_type_null(Visitor *v, const char *name, QNull **obj,
378                      Error **errp)
379 {
380     trace_visit_type_null(v, name, obj);
381     return v->type_null(v, name, obj, errp);
382 }
383 
384 static bool output_type_enum(Visitor *v, const char *name, int *obj,
385                              const QEnumLookup *lookup, Error **errp)
386 {
387     int value = *obj;
388     char *enum_str;
389 
390     enum_str = (char *)qapi_enum_lookup(lookup, value);
391     return visit_type_str(v, name, &enum_str, errp);
392 }
393 
394 static bool input_type_enum(Visitor *v, const char *name, int *obj,
395                             const QEnumLookup *lookup, Error **errp)
396 {
397     int64_t value;
398     g_autofree char *enum_str = NULL;
399 
400     if (!visit_type_str(v, name, &enum_str, errp)) {
401         return false;
402     }
403 
404     value = qapi_enum_parse(lookup, enum_str, -1, NULL);
405     if (value < 0) {
406         error_setg(errp, "Parameter '%s' does not accept value '%s'",
407                    name ? name : "null", enum_str);
408         return false;
409     }
410 
411     if (lookup->special_features
412         && (lookup->special_features[value] & QAPI_DEPRECATED)) {
413         switch (v->compat_policy.deprecated_input) {
414         case COMPAT_POLICY_INPUT_ACCEPT:
415             break;
416         case COMPAT_POLICY_INPUT_REJECT:
417             error_setg(errp, "Deprecated value '%s' disabled by policy",
418                        enum_str);
419             return false;
420         case COMPAT_POLICY_INPUT_CRASH:
421         default:
422             abort();
423         }
424     }
425 
426     *obj = value;
427     return true;
428 }
429 
430 bool visit_type_enum(Visitor *v, const char *name, int *obj,
431                      const QEnumLookup *lookup, Error **errp)
432 {
433     assert(obj && lookup);
434     trace_visit_type_enum(v, name, obj);
435     switch (v->type) {
436     case VISITOR_INPUT:
437         return input_type_enum(v, name, obj, lookup, errp);
438     case VISITOR_OUTPUT:
439         return output_type_enum(v, name, obj, lookup, errp);
440     case VISITOR_CLONE:
441         /* nothing further to do, scalar value was already copied by
442          * g_memdup() during visit_start_*() */
443         return true;
444     case VISITOR_DEALLOC:
445         /* nothing to deallocate for a scalar */
446         return true;
447     default:
448         abort();
449     }
450 }
451