]> git.ipfire.org Git - thirdparty/qemu.git/blame - qapi/opts-visitor.c
Merge remote-tracking branch 'remotes/stefanha/tags/block-pull-request' into staging
[thirdparty/qemu.git] / qapi / opts-visitor.c
CommitLineData
eb7ee2cb
LE
1/*
2 * Options Visitor
3 *
08f9541d 4 * Copyright Red Hat, Inc. 2012-2016
eb7ee2cb
LE
5 *
6 * Author: Laszlo Ersek <lersek@redhat.com>
7 *
8 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
9 * See the COPYING.LIB file in the top-level directory.
10 *
11 */
12
cbf21151 13#include "qemu/osdep.h"
da34e65c 14#include "qapi/error.h"
f348b6d1 15#include "qemu/cutils.h"
7b1b5d19
PB
16#include "qapi/qmp/qerror.h"
17#include "qapi/opts-visitor.h"
1de7afc9
PB
18#include "qemu/queue.h"
19#include "qemu/option_int.h"
7b1b5d19 20#include "qapi/visitor-impl.h"
eb7ee2cb
LE
21
22
d9570434
LE
23enum ListMode
24{
25 LM_NONE, /* not traversing a list of repeated options */
d8754f40 26
863f195f
AS
27 LM_IN_PROGRESS, /*
28 * opts_next_list() ready to be called.
d8754f40
LE
29 *
30 * Generating the next list link will consume the most
31 * recently parsed QemuOpt instance of the repeated
32 * option.
33 *
34 * Parsing a value into the list link will examine the
35 * next QemuOpt instance of the repeated option, and
36 * possibly enter LM_SIGNED_INTERVAL or
37 * LM_UNSIGNED_INTERVAL.
38 */
39
863f195f
AS
40 LM_SIGNED_INTERVAL, /*
41 * opts_next_list() has been called.
d8754f40
LE
42 *
43 * Generating the next list link will consume the most
44 * recently stored element from the signed interval,
45 * parsed from the most recent QemuOpt instance of the
46 * repeated option. This may consume QemuOpt itself
47 * and return to LM_IN_PROGRESS.
48 *
49 * Parsing a value into the list link will store the
50 * next element of the signed interval.
51 */
52
863f195f
AS
53 LM_UNSIGNED_INTERVAL, /* Same as above, only for an unsigned interval. */
54
55 LM_TRAVERSED /*
56 * opts_next_list() has been called.
57 *
58 * No more QemuOpt instance in the list.
59 * The traversal has been completed.
60 */
d9570434
LE
61};
62
63typedef enum ListMode ListMode;
64
eb7ee2cb
LE
65struct OptsVisitor
66{
67 Visitor visitor;
68
69 /* Ownership remains with opts_visitor_new()'s caller. */
70 const QemuOpts *opts_root;
71
72 unsigned depth;
73
74 /* Non-null iff depth is positive. Each key is a QemuOpt name. Each value
75 * is a non-empty GQueue, enumerating all QemuOpt occurrences with that
76 * name. */
77 GHashTable *unprocessed_opts;
78
79 /* The list currently being traversed with opts_start_list() /
80 * opts_next_list(). The list must have a struct element type in the
81 * schema, with a single mandatory scalar member. */
d9570434 82 ListMode list_mode;
eb7ee2cb 83 GQueue *repeated_opts;
eb7ee2cb 84
d8754f40
LE
85 /* When parsing a list of repeating options as integers, values of the form
86 * "a-b", representing a closed interval, are allowed. Elements in the
87 * range are generated individually.
88 */
89 union {
90 int64_t s;
91 uint64_t u;
92 } range_next, range_limit;
93
eb7ee2cb
LE
94 /* If "opts_root->id" is set, reinstantiate it as a fake QemuOpt for
95 * uniformity. Only its "name" and "str" fields are set. "fake_id_opt" does
96 * not survive or escape the OptsVisitor object.
97 */
98 QemuOpt *fake_id_opt;
99};
100
101
d7bea75d
EB
102static OptsVisitor *to_ov(Visitor *v)
103{
104 return container_of(v, OptsVisitor, visitor);
105}
106
107
eb7ee2cb
LE
108static void
109destroy_list(gpointer list)
110{
111 g_queue_free(list);
112}
113
114
115static void
116opts_visitor_insert(GHashTable *unprocessed_opts, const QemuOpt *opt)
117{
118 GQueue *list;
119
120 list = g_hash_table_lookup(unprocessed_opts, opt->name);
121 if (list == NULL) {
122 list = g_queue_new();
123
124 /* GHashTable will never try to free the keys -- we supply NULL as
125 * "key_destroy_func" in opts_start_struct(). Thus cast away key
126 * const-ness in order to suppress gcc's warning.
127 */
128 g_hash_table_insert(unprocessed_opts, (gpointer)opt->name, list);
129 }
130
131 /* Similarly, destroy_list() doesn't call g_queue_free_full(). */
132 g_queue_push_tail(list, (gpointer)opt);
133}
134
135
136static void
337283df 137opts_start_struct(Visitor *v, const char *name, void **obj,
0b2a0d6b 138 size_t size, Error **errp)
eb7ee2cb 139{
d7bea75d 140 OptsVisitor *ov = to_ov(v);
eb7ee2cb
LE
141 const QemuOpt *opt;
142
b7745397 143 if (obj) {
e58d695e 144 *obj = g_malloc0(size);
b7745397 145 }
eb7ee2cb
LE
146 if (ov->depth++ > 0) {
147 return;
148 }
149
150 ov->unprocessed_opts = g_hash_table_new_full(&g_str_hash, &g_str_equal,
151 NULL, &destroy_list);
152 QTAILQ_FOREACH(opt, &ov->opts_root->head, next) {
153 /* ensured by qemu-option.c::opts_do_parse() */
154 assert(strcmp(opt->name, "id") != 0);
155
156 opts_visitor_insert(ov->unprocessed_opts, opt);
157 }
158
159 if (ov->opts_root->id != NULL) {
160 ov->fake_id_opt = g_malloc0(sizeof *ov->fake_id_opt);
161
dc8622f2
CL
162 ov->fake_id_opt->name = g_strdup("id");
163 ov->fake_id_opt->str = g_strdup(ov->opts_root->id);
eb7ee2cb
LE
164 opts_visitor_insert(ov->unprocessed_opts, ov->fake_id_opt);
165 }
166}
167
168
eb7ee2cb 169static void
15c2f669 170opts_check_struct(Visitor *v, Error **errp)
eb7ee2cb 171{
d7bea75d 172 OptsVisitor *ov = to_ov(v);
f96493b1 173 GHashTableIter iter;
eb7ee2cb
LE
174 GQueue *any;
175
21f88d02 176 if (ov->depth > 1) {
eb7ee2cb
LE
177 return;
178 }
179
180 /* we should have processed all (distinct) QemuOpt instances */
f96493b1
EB
181 g_hash_table_iter_init(&iter, ov->unprocessed_opts);
182 if (g_hash_table_iter_next(&iter, NULL, (void **)&any)) {
eb7ee2cb
LE
183 const QemuOpt *first;
184
185 first = g_queue_peek_head(any);
c6bd8c70 186 error_setg(errp, QERR_INVALID_PARAMETER, first->name);
eb7ee2cb 187 }
15c2f669
EB
188}
189
190
191static void
1158bb2a 192opts_end_struct(Visitor *v, void **obj)
15c2f669
EB
193{
194 OptsVisitor *ov = to_ov(v);
195
196 if (--ov->depth > 0) {
197 return;
198 }
199
eb7ee2cb
LE
200 g_hash_table_destroy(ov->unprocessed_opts);
201 ov->unprocessed_opts = NULL;
dc8622f2
CL
202 if (ov->fake_id_opt) {
203 g_free(ov->fake_id_opt->name);
204 g_free(ov->fake_id_opt->str);
205 g_free(ov->fake_id_opt);
206 }
eb7ee2cb
LE
207 ov->fake_id_opt = NULL;
208}
209
210
211static GQueue *
212lookup_distinct(const OptsVisitor *ov, const char *name, Error **errp)
213{
214 GQueue *list;
215
216 list = g_hash_table_lookup(ov->unprocessed_opts, name);
217 if (!list) {
c6bd8c70 218 error_setg(errp, QERR_MISSING_PARAMETER, name);
eb7ee2cb
LE
219 }
220 return list;
221}
222
223
224static void
d9f62dde
EB
225opts_start_list(Visitor *v, const char *name, GenericList **list, size_t size,
226 Error **errp)
eb7ee2cb 227{
d7bea75d 228 OptsVisitor *ov = to_ov(v);
eb7ee2cb
LE
229
230 /* we can't traverse a list in a list */
d9570434 231 assert(ov->list_mode == LM_NONE);
d9f62dde
EB
232 /* we don't support visits without a list */
233 assert(list);
eb7ee2cb 234 ov->repeated_opts = lookup_distinct(ov, name, errp);
d9f62dde
EB
235 if (ov->repeated_opts) {
236 ov->list_mode = LM_IN_PROGRESS;
237 *list = g_malloc0(size);
238 } else {
239 *list = NULL;
d9570434 240 }
eb7ee2cb
LE
241}
242
243
244static GenericList *
d9f62dde 245opts_next_list(Visitor *v, GenericList *tail, size_t size)
eb7ee2cb 246{
d7bea75d 247 OptsVisitor *ov = to_ov(v);
eb7ee2cb 248
d9570434 249 switch (ov->list_mode) {
863f195f
AS
250 case LM_TRAVERSED:
251 return NULL;
d8754f40
LE
252 case LM_SIGNED_INTERVAL:
253 case LM_UNSIGNED_INTERVAL:
d8754f40
LE
254 if (ov->list_mode == LM_SIGNED_INTERVAL) {
255 if (ov->range_next.s < ov->range_limit.s) {
256 ++ov->range_next.s;
257 break;
258 }
259 } else if (ov->range_next.u < ov->range_limit.u) {
260 ++ov->range_next.u;
261 break;
262 }
263 ov->list_mode = LM_IN_PROGRESS;
264 /* range has been completed, fall through in order to pop option */
265
d9570434 266 case LM_IN_PROGRESS: {
eb7ee2cb
LE
267 const QemuOpt *opt;
268
269 opt = g_queue_pop_head(ov->repeated_opts);
270 if (g_queue_is_empty(ov->repeated_opts)) {
271 g_hash_table_remove(ov->unprocessed_opts, opt->name);
863f195f
AS
272 ov->repeated_opts = NULL;
273 ov->list_mode = LM_TRAVERSED;
eb7ee2cb
LE
274 return NULL;
275 }
d9570434
LE
276 break;
277 }
278
279 default:
280 abort();
eb7ee2cb
LE
281 }
282
d9f62dde
EB
283 tail->next = g_malloc0(size);
284 return tail->next;
eb7ee2cb
LE
285}
286
287
a4a1c70d
MA
288static void
289opts_check_list(Visitor *v, Error **errp)
290{
291 /*
21f88d02
EB
292 * Unvisited list elements will be reported later when checking
293 * whether unvisited struct members remain.
a4a1c70d
MA
294 */
295}
296
297
eb7ee2cb 298static void
1158bb2a 299opts_end_list(Visitor *v, void **obj)
eb7ee2cb 300{
d7bea75d 301 OptsVisitor *ov = to_ov(v);
eb7ee2cb 302
d9f62dde 303 assert(ov->list_mode == LM_IN_PROGRESS ||
d8754f40 304 ov->list_mode == LM_SIGNED_INTERVAL ||
863f195f
AS
305 ov->list_mode == LM_UNSIGNED_INTERVAL ||
306 ov->list_mode == LM_TRAVERSED);
eb7ee2cb 307 ov->repeated_opts = NULL;
d9570434 308 ov->list_mode = LM_NONE;
eb7ee2cb
LE
309}
310
311
312static const QemuOpt *
313lookup_scalar(const OptsVisitor *ov, const char *name, Error **errp)
314{
d9570434 315 if (ov->list_mode == LM_NONE) {
eb7ee2cb
LE
316 GQueue *list;
317
318 /* the last occurrence of any QemuOpt takes effect when queried by name
319 */
320 list = lookup_distinct(ov, name, errp);
321 return list ? g_queue_peek_tail(list) : NULL;
322 }
863f195f
AS
323 if (ov->list_mode == LM_TRAVERSED) {
324 error_setg(errp, "Fewer list elements than expected");
325 return NULL;
326 }
d9570434 327 assert(ov->list_mode == LM_IN_PROGRESS);
eb7ee2cb
LE
328 return g_queue_peek_head(ov->repeated_opts);
329}
330
331
332static void
333processed(OptsVisitor *ov, const char *name)
334{
d9570434 335 if (ov->list_mode == LM_NONE) {
eb7ee2cb 336 g_hash_table_remove(ov->unprocessed_opts, name);
d9570434 337 return;
eb7ee2cb 338 }
d9570434
LE
339 assert(ov->list_mode == LM_IN_PROGRESS);
340 /* do nothing */
eb7ee2cb
LE
341}
342
343
344static void
0b2a0d6b 345opts_type_str(Visitor *v, const char *name, char **obj, Error **errp)
eb7ee2cb 346{
d7bea75d 347 OptsVisitor *ov = to_ov(v);
eb7ee2cb
LE
348 const QemuOpt *opt;
349
350 opt = lookup_scalar(ov, name, errp);
351 if (!opt) {
e58d695e 352 *obj = NULL;
eb7ee2cb
LE
353 return;
354 }
355 *obj = g_strdup(opt->str ? opt->str : "");
983f52d4
EB
356 /* Note that we consume a string even if this is called as part of
357 * an enum visit that later fails because the string is not a
358 * valid enum value; this is harmless because tracking what gets
359 * consumed only matters to visit_end_struct() as the final error
360 * check if there were no other failures during the visit. */
eb7ee2cb
LE
361 processed(ov, name);
362}
363
364
365/* mimics qemu-option.c::parse_option_bool() */
366static void
0b2a0d6b 367opts_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
eb7ee2cb 368{
d7bea75d 369 OptsVisitor *ov = to_ov(v);
eb7ee2cb
LE
370 const QemuOpt *opt;
371
372 opt = lookup_scalar(ov, name, errp);
373 if (!opt) {
374 return;
375 }
376
377 if (opt->str) {
378 if (strcmp(opt->str, "on") == 0 ||
379 strcmp(opt->str, "yes") == 0 ||
380 strcmp(opt->str, "y") == 0) {
381 *obj = true;
382 } else if (strcmp(opt->str, "off") == 0 ||
383 strcmp(opt->str, "no") == 0 ||
384 strcmp(opt->str, "n") == 0) {
385 *obj = false;
386 } else {
c6bd8c70
MA
387 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
388 "on|yes|y|off|no|n");
eb7ee2cb
LE
389 return;
390 }
391 } else {
392 *obj = true;
393 }
394
395 processed(ov, name);
396}
397
398
399static void
0b2a0d6b 400opts_type_int64(Visitor *v, const char *name, int64_t *obj, Error **errp)
eb7ee2cb 401{
d7bea75d 402 OptsVisitor *ov = to_ov(v);
eb7ee2cb
LE
403 const QemuOpt *opt;
404 const char *str;
405 long long val;
406 char *endptr;
407
d8754f40
LE
408 if (ov->list_mode == LM_SIGNED_INTERVAL) {
409 *obj = ov->range_next.s;
410 return;
411 }
412
eb7ee2cb
LE
413 opt = lookup_scalar(ov, name, errp);
414 if (!opt) {
415 return;
416 }
417 str = opt->str ? opt->str : "";
418
1e1c555a
LE
419 /* we've gotten past lookup_scalar() */
420 assert(ov->list_mode == LM_NONE || ov->list_mode == LM_IN_PROGRESS);
421
eb7ee2cb
LE
422 errno = 0;
423 val = strtoll(str, &endptr, 0);
1e1c555a
LE
424 if (errno == 0 && endptr > str && INT64_MIN <= val && val <= INT64_MAX) {
425 if (*endptr == '\0') {
426 *obj = val;
427 processed(ov, name);
428 return;
429 }
430 if (*endptr == '-' && ov->list_mode == LM_IN_PROGRESS) {
431 long long val2;
432
433 str = endptr + 1;
434 val2 = strtoll(str, &endptr, 0);
435 if (errno == 0 && endptr > str && *endptr == '\0' &&
15a849be
LE
436 INT64_MIN <= val2 && val2 <= INT64_MAX && val <= val2 &&
437 (val > INT64_MAX - OPTS_VISITOR_RANGE_MAX ||
438 val2 < val + OPTS_VISITOR_RANGE_MAX)) {
1e1c555a
LE
439 ov->range_next.s = val;
440 ov->range_limit.s = val2;
441 ov->list_mode = LM_SIGNED_INTERVAL;
442
443 /* as if entering on the top */
444 *obj = ov->range_next.s;
445 return;
446 }
447 }
eb7ee2cb 448 }
c6bd8c70
MA
449 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
450 (ov->list_mode == LM_NONE) ? "an int64 value" :
451 "an int64 value or range");
eb7ee2cb
LE
452}
453
454
455static void
0b2a0d6b 456opts_type_uint64(Visitor *v, const char *name, uint64_t *obj, Error **errp)
eb7ee2cb 457{
d7bea75d 458 OptsVisitor *ov = to_ov(v);
eb7ee2cb
LE
459 const QemuOpt *opt;
460 const char *str;
62d090e2 461 unsigned long long val;
581a8a80 462 char *endptr;
eb7ee2cb 463
d8754f40
LE
464 if (ov->list_mode == LM_UNSIGNED_INTERVAL) {
465 *obj = ov->range_next.u;
466 return;
467 }
468
eb7ee2cb
LE
469 opt = lookup_scalar(ov, name, errp);
470 if (!opt) {
471 return;
472 }
eb7ee2cb 473 str = opt->str;
eb7ee2cb 474
581a8a80
LE
475 /* we've gotten past lookup_scalar() */
476 assert(ov->list_mode == LM_NONE || ov->list_mode == LM_IN_PROGRESS);
477
478 if (parse_uint(str, &val, &endptr, 0) == 0 && val <= UINT64_MAX) {
479 if (*endptr == '\0') {
480 *obj = val;
481 processed(ov, name);
482 return;
483 }
484 if (*endptr == '-' && ov->list_mode == LM_IN_PROGRESS) {
485 unsigned long long val2;
486
487 str = endptr + 1;
488 if (parse_uint_full(str, &val2, 0) == 0 &&
15a849be
LE
489 val2 <= UINT64_MAX && val <= val2 &&
490 val2 - val < OPTS_VISITOR_RANGE_MAX) {
581a8a80
LE
491 ov->range_next.u = val;
492 ov->range_limit.u = val2;
493 ov->list_mode = LM_UNSIGNED_INTERVAL;
494
495 /* as if entering on the top */
496 *obj = ov->range_next.u;
497 return;
498 }
499 }
eb7ee2cb 500 }
c6bd8c70
MA
501 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
502 (ov->list_mode == LM_NONE) ? "a uint64 value" :
503 "a uint64 value or range");
eb7ee2cb
LE
504}
505
506
507static void
0b2a0d6b 508opts_type_size(Visitor *v, const char *name, uint64_t *obj, Error **errp)
eb7ee2cb 509{
d7bea75d 510 OptsVisitor *ov = to_ov(v);
eb7ee2cb 511 const QemuOpt *opt;
f17fd4fd 512 int err;
eb7ee2cb
LE
513
514 opt = lookup_scalar(ov, name, errp);
515 if (!opt) {
516 return;
517 }
518
f46bfdbf 519 err = qemu_strtosz(opt->str ? opt->str : "", NULL, obj);
f17fd4fd 520 if (err < 0) {
c6bd8c70 521 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
f46bfdbf 522 "a size value");
eb7ee2cb
LE
523 return;
524 }
cb45de67 525
cb45de67 526 processed(ov, name);
eb7ee2cb
LE
527}
528
529
530static void
0b2a0d6b 531opts_optional(Visitor *v, const char *name, bool *present)
eb7ee2cb 532{
d7bea75d 533 OptsVisitor *ov = to_ov(v);
eb7ee2cb
LE
534
535 /* we only support a single mandatory scalar field in a list node */
d9570434 536 assert(ov->list_mode == LM_NONE);
eb7ee2cb
LE
537 *present = (lookup_distinct(ov, name, NULL) != NULL);
538}
539
540
2c0ef9f4
EB
541static void
542opts_free(Visitor *v)
543{
544 OptsVisitor *ov = to_ov(v);
545
09204eac
EB
546 if (ov->unprocessed_opts != NULL) {
547 g_hash_table_destroy(ov->unprocessed_opts);
548 }
549 g_free(ov->fake_id_opt);
550 g_free(ov);
2c0ef9f4
EB
551}
552
553
09204eac 554Visitor *
eb7ee2cb
LE
555opts_visitor_new(const QemuOpts *opts)
556{
557 OptsVisitor *ov;
558
f332e830 559 assert(opts);
eb7ee2cb
LE
560 ov = g_malloc0(sizeof *ov);
561
983f52d4
EB
562 ov->visitor.type = VISITOR_INPUT;
563
eb7ee2cb 564 ov->visitor.start_struct = &opts_start_struct;
15c2f669 565 ov->visitor.check_struct = &opts_check_struct;
eb7ee2cb
LE
566 ov->visitor.end_struct = &opts_end_struct;
567
568 ov->visitor.start_list = &opts_start_list;
569 ov->visitor.next_list = &opts_next_list;
a4a1c70d 570 ov->visitor.check_list = &opts_check_list;
eb7ee2cb
LE
571 ov->visitor.end_list = &opts_end_list;
572
4c40314a 573 ov->visitor.type_int64 = &opts_type_int64;
eb7ee2cb
LE
574 ov->visitor.type_uint64 = &opts_type_uint64;
575 ov->visitor.type_size = &opts_type_size;
576 ov->visitor.type_bool = &opts_type_bool;
577 ov->visitor.type_str = &opts_type_str;
578
579 /* type_number() is not filled in, but this is not the first visitor to
580 * skip some mandatory methods... */
581
e2cd0f4f 582 ov->visitor.optional = &opts_optional;
2c0ef9f4 583 ov->visitor.free = opts_free;
eb7ee2cb
LE
584
585 ov->opts_root = opts;
586
eb7ee2cb
LE
587 return &ov->visitor;
588}