xref: /openbmc/qemu/util/qemu-config.c (revision 4c4465ff)
1 #include "qemu/osdep.h"
2 #include "block/qdict.h" /* for qdict_extract_subqdict() */
3 #include "qapi/error.h"
4 #include "qapi/qapi-commands-misc.h"
5 #include "qapi/qmp/qdict.h"
6 #include "qapi/qmp/qlist.h"
7 #include "qemu/error-report.h"
8 #include "qemu/option.h"
9 #include "qemu/config-file.h"
10 
11 static QemuOptsList *vm_config_groups[48];
12 static QemuOptsList *drive_config_groups[5];
13 
14 static QemuOptsList *find_list(QemuOptsList **lists, const char *group,
15                                Error **errp)
16 {
17     int i;
18 
19     for (i = 0; lists[i] != NULL; i++) {
20         if (strcmp(lists[i]->name, group) == 0)
21             break;
22     }
23     if (lists[i] == NULL) {
24         error_setg(errp, "There is no option group '%s'", group);
25     }
26     return lists[i];
27 }
28 
29 QemuOptsList *qemu_find_opts(const char *group)
30 {
31     QemuOptsList *ret;
32     Error *local_err = NULL;
33 
34     ret = find_list(vm_config_groups, group, &local_err);
35     if (local_err) {
36         error_report_err(local_err);
37     }
38 
39     return ret;
40 }
41 
42 QemuOpts *qemu_find_opts_singleton(const char *group)
43 {
44     QemuOptsList *list;
45     QemuOpts *opts;
46 
47     list = qemu_find_opts(group);
48     assert(list);
49     opts = qemu_opts_find(list, NULL);
50     if (!opts) {
51         opts = qemu_opts_create(list, NULL, 0, &error_abort);
52     }
53     return opts;
54 }
55 
56 static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc)
57 {
58     CommandLineParameterInfoList *param_list = NULL;
59     CommandLineParameterInfo *info;
60     int i;
61 
62     for (i = 0; desc[i].name != NULL; i++) {
63         info = g_malloc0(sizeof(*info));
64         info->name = g_strdup(desc[i].name);
65 
66         switch (desc[i].type) {
67         case QEMU_OPT_STRING:
68             info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
69             break;
70         case QEMU_OPT_BOOL:
71             info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN;
72             break;
73         case QEMU_OPT_NUMBER:
74             info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER;
75             break;
76         case QEMU_OPT_SIZE:
77             info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE;
78             break;
79         }
80 
81         if (desc[i].help) {
82             info->has_help = true;
83             info->help = g_strdup(desc[i].help);
84         }
85         if (desc[i].def_value_str) {
86             info->has_q_default = true;
87             info->q_default = g_strdup(desc[i].def_value_str);
88         }
89 
90         QAPI_LIST_PREPEND(param_list, info);
91     }
92 
93     return param_list;
94 }
95 
96 /* remove repeated entry from the info list */
97 static void cleanup_infolist(CommandLineParameterInfoList *head)
98 {
99     CommandLineParameterInfoList *pre_entry, *cur, *del_entry;
100 
101     cur = head;
102     while (cur->next) {
103         pre_entry = head;
104         while (pre_entry != cur->next) {
105             if (!strcmp(pre_entry->value->name, cur->next->value->name)) {
106                 del_entry = cur->next;
107                 cur->next = cur->next->next;
108                 del_entry->next = NULL;
109                 qapi_free_CommandLineParameterInfoList(del_entry);
110                 break;
111             }
112             pre_entry = pre_entry->next;
113         }
114         cur = cur->next;
115     }
116 }
117 
118 /* merge the description items of two parameter infolists */
119 static void connect_infolist(CommandLineParameterInfoList *head,
120                              CommandLineParameterInfoList *new)
121 {
122     CommandLineParameterInfoList *cur;
123 
124     cur = head;
125     while (cur->next) {
126         cur = cur->next;
127     }
128     cur->next = new;
129 }
130 
131 /* access all the local QemuOptsLists for drive option */
132 static CommandLineParameterInfoList *get_drive_infolist(void)
133 {
134     CommandLineParameterInfoList *head = NULL, *cur;
135     int i;
136 
137     for (i = 0; drive_config_groups[i] != NULL; i++) {
138         if (!head) {
139             head = query_option_descs(drive_config_groups[i]->desc);
140         } else {
141             cur = query_option_descs(drive_config_groups[i]->desc);
142             connect_infolist(head, cur);
143         }
144     }
145     cleanup_infolist(head);
146 
147     return head;
148 }
149 
150 /* restore machine options that are now machine's properties */
151 static QemuOptsList machine_opts = {
152     .merge_lists = true,
153     .head = QTAILQ_HEAD_INITIALIZER(machine_opts.head),
154     .desc = {
155         {
156             .name = "type",
157             .type = QEMU_OPT_STRING,
158             .help = "emulated machine"
159         },{
160             .name = "accel",
161             .type = QEMU_OPT_STRING,
162             .help = "accelerator list",
163         },{
164             .name = "kernel_irqchip",
165             .type = QEMU_OPT_BOOL,
166             .help = "use KVM in-kernel irqchip",
167         },{
168             .name = "kvm_shadow_mem",
169             .type = QEMU_OPT_SIZE,
170             .help = "KVM shadow MMU size",
171         },{
172             .name = "kernel",
173             .type = QEMU_OPT_STRING,
174             .help = "Linux kernel image file",
175         },{
176             .name = "initrd",
177             .type = QEMU_OPT_STRING,
178             .help = "Linux initial ramdisk file",
179         },{
180             .name = "append",
181             .type = QEMU_OPT_STRING,
182             .help = "Linux kernel command line",
183         },{
184             .name = "dtb",
185             .type = QEMU_OPT_STRING,
186             .help = "Linux kernel device tree file",
187         },{
188             .name = "dumpdtb",
189             .type = QEMU_OPT_STRING,
190             .help = "Dump current dtb to a file and quit",
191         },{
192             .name = "phandle_start",
193             .type = QEMU_OPT_NUMBER,
194             .help = "The first phandle ID we may generate dynamically",
195         },{
196             .name = "dt_compatible",
197             .type = QEMU_OPT_STRING,
198             .help = "Overrides the \"compatible\" property of the dt root node",
199         },{
200             .name = "dump-guest-core",
201             .type = QEMU_OPT_BOOL,
202             .help = "Include guest memory in  a core dump",
203         },{
204             .name = "mem-merge",
205             .type = QEMU_OPT_BOOL,
206             .help = "enable/disable memory merge support",
207         },{
208             .name = "usb",
209             .type = QEMU_OPT_BOOL,
210             .help = "Set on/off to enable/disable usb",
211         },{
212             .name = "firmware",
213             .type = QEMU_OPT_STRING,
214             .help = "firmware image",
215         },{
216             .name = "iommu",
217             .type = QEMU_OPT_BOOL,
218             .help = "Set on/off to enable/disable Intel IOMMU (VT-d)",
219         },{
220             .name = "suppress-vmdesc",
221             .type = QEMU_OPT_BOOL,
222             .help = "Set on to disable self-describing migration",
223         },{
224             .name = "aes-key-wrap",
225             .type = QEMU_OPT_BOOL,
226             .help = "enable/disable AES key wrapping using the CPACF wrapping key",
227         },{
228             .name = "dea-key-wrap",
229             .type = QEMU_OPT_BOOL,
230             .help = "enable/disable DEA key wrapping using the CPACF wrapping key",
231         },{
232             .name = "loadparm",
233             .type = QEMU_OPT_STRING,
234             .help = "Up to 8 chars in set of [A-Za-z0-9. ](lower case chars"
235                     " converted to upper case) to pass to machine"
236                     " loader, boot manager, and guest kernel",
237         },
238         { /* End of list */ }
239     }
240 };
241 
242 CommandLineOptionInfoList *qmp_query_command_line_options(bool has_option,
243                                                           const char *option,
244                                                           Error **errp)
245 {
246     CommandLineOptionInfoList *conf_list = NULL;
247     CommandLineOptionInfo *info;
248     int i;
249 
250     for (i = 0; vm_config_groups[i] != NULL; i++) {
251         if (!has_option || !strcmp(option, vm_config_groups[i]->name)) {
252             info = g_malloc0(sizeof(*info));
253             info->option = g_strdup(vm_config_groups[i]->name);
254             if (!strcmp("drive", vm_config_groups[i]->name)) {
255                 info->parameters = get_drive_infolist();
256             } else if (!strcmp("machine", vm_config_groups[i]->name)) {
257                 info->parameters = query_option_descs(machine_opts.desc);
258             } else {
259                 info->parameters =
260                     query_option_descs(vm_config_groups[i]->desc);
261             }
262             QAPI_LIST_PREPEND(conf_list, info);
263         }
264     }
265 
266     if (conf_list == NULL) {
267         error_setg(errp, "invalid option name: %s", option);
268     }
269 
270     return conf_list;
271 }
272 
273 QemuOptsList *qemu_find_opts_err(const char *group, Error **errp)
274 {
275     return find_list(vm_config_groups, group, errp);
276 }
277 
278 void qemu_add_drive_opts(QemuOptsList *list)
279 {
280     int entries, i;
281 
282     entries = ARRAY_SIZE(drive_config_groups);
283     entries--; /* keep list NULL terminated */
284     for (i = 0; i < entries; i++) {
285         if (drive_config_groups[i] == NULL) {
286             drive_config_groups[i] = list;
287             return;
288         }
289     }
290     fprintf(stderr, "ran out of space in drive_config_groups");
291     abort();
292 }
293 
294 void qemu_add_opts(QemuOptsList *list)
295 {
296     int entries, i;
297 
298     entries = ARRAY_SIZE(vm_config_groups);
299     entries--; /* keep list NULL terminated */
300     for (i = 0; i < entries; i++) {
301         if (vm_config_groups[i] == NULL) {
302             vm_config_groups[i] = list;
303             return;
304         }
305     }
306     fprintf(stderr, "ran out of space in vm_config_groups");
307     abort();
308 }
309 
310 struct ConfigWriteData {
311     QemuOptsList *list;
312     FILE *fp;
313 };
314 
315 static int config_write_opt(void *opaque, const char *name, const char *value,
316                             Error **errp)
317 {
318     struct ConfigWriteData *data = opaque;
319 
320     fprintf(data->fp, "  %s = \"%s\"\n", name, value);
321     return 0;
322 }
323 
324 static int config_write_opts(void *opaque, QemuOpts *opts, Error **errp)
325 {
326     struct ConfigWriteData *data = opaque;
327     const char *id = qemu_opts_id(opts);
328 
329     if (id) {
330         fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
331     } else {
332         fprintf(data->fp, "[%s]\n", data->list->name);
333     }
334     qemu_opt_foreach(opts, config_write_opt, data, NULL);
335     fprintf(data->fp, "\n");
336     return 0;
337 }
338 
339 void qemu_config_write(FILE *fp)
340 {
341     struct ConfigWriteData data = { .fp = fp };
342     QemuOptsList **lists = vm_config_groups;
343     int i;
344 
345     fprintf(fp, "# qemu config file\n\n");
346     for (i = 0; lists[i] != NULL; i++) {
347         data.list = lists[i];
348         qemu_opts_foreach(data.list, config_write_opts, &data, NULL);
349     }
350 }
351 
352 /* Returns number of config groups on success, -errno on error */
353 int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
354 {
355     char line[1024], group[64], id[64], arg[64], value[1024];
356     Location loc;
357     QemuOptsList *list = NULL;
358     Error *local_err = NULL;
359     QemuOpts *opts = NULL;
360     int res = -EINVAL, lno = 0;
361     int count = 0;
362 
363     loc_push_none(&loc);
364     while (fgets(line, sizeof(line), fp) != NULL) {
365         loc_set_file(fname, ++lno);
366         if (line[0] == '\n') {
367             /* skip empty lines */
368             continue;
369         }
370         if (line[0] == '#') {
371             /* comment */
372             continue;
373         }
374         if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
375             /* group with id */
376             list = find_list(lists, group, &local_err);
377             if (local_err) {
378                 error_report_err(local_err);
379                 goto out;
380             }
381             opts = qemu_opts_create(list, id, 1, NULL);
382             count++;
383             continue;
384         }
385         if (sscanf(line, "[%63[^]]]", group) == 1) {
386             /* group without id */
387             list = find_list(lists, group, &local_err);
388             if (local_err) {
389                 error_report_err(local_err);
390                 goto out;
391             }
392             opts = qemu_opts_create(list, NULL, 0, &error_abort);
393             count++;
394             continue;
395         }
396         value[0] = '\0';
397         if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2 ||
398             sscanf(line, " %63s = \"\"", arg) == 1) {
399             /* arg = value */
400             if (opts == NULL) {
401                 error_report("no group defined");
402                 goto out;
403             }
404             if (!qemu_opt_set(opts, arg, value, &local_err)) {
405                 error_report_err(local_err);
406                 goto out;
407             }
408             continue;
409         }
410         error_report("parse error");
411         goto out;
412     }
413     if (ferror(fp)) {
414         error_report("error reading file");
415         goto out;
416     }
417     res = count;
418 out:
419     loc_pop(&loc);
420     return res;
421 }
422 
423 int qemu_read_config_file(const char *filename)
424 {
425     FILE *f = fopen(filename, "r");
426     int ret;
427 
428     if (f == NULL) {
429         return -errno;
430     }
431 
432     ret = qemu_config_parse(f, vm_config_groups, filename);
433     fclose(f);
434     return ret;
435 }
436 
437 static void config_parse_qdict_section(QDict *options, QemuOptsList *opts,
438                                        Error **errp)
439 {
440     QemuOpts *subopts;
441     QDict *subqdict;
442     QList *list = NULL;
443     size_t orig_size, enum_size;
444     char *prefix;
445 
446     prefix = g_strdup_printf("%s.", opts->name);
447     qdict_extract_subqdict(options, &subqdict, prefix);
448     g_free(prefix);
449     orig_size = qdict_size(subqdict);
450     if (!orig_size) {
451         goto out;
452     }
453 
454     subopts = qemu_opts_create(opts, NULL, 0, errp);
455     if (!subopts) {
456         goto out;
457     }
458 
459     if (!qemu_opts_absorb_qdict(subopts, subqdict, errp)) {
460         goto out;
461     }
462 
463     enum_size = qdict_size(subqdict);
464     if (enum_size < orig_size && enum_size) {
465         error_setg(errp, "Unknown option '%s' for [%s]",
466                    qdict_first(subqdict)->key, opts->name);
467         goto out;
468     }
469 
470     if (enum_size) {
471         /* Multiple, enumerated sections */
472         QListEntry *list_entry;
473         unsigned i = 0;
474 
475         /* Not required anymore */
476         qemu_opts_del(subopts);
477 
478         qdict_array_split(subqdict, &list);
479         if (qdict_size(subqdict)) {
480             error_setg(errp, "Unused option '%s' for [%s]",
481                        qdict_first(subqdict)->key, opts->name);
482             goto out;
483         }
484 
485         QLIST_FOREACH_ENTRY(list, list_entry) {
486             QDict *section = qobject_to(QDict, qlist_entry_obj(list_entry));
487             char *opt_name;
488 
489             if (!section) {
490                 error_setg(errp, "[%s] section (index %u) does not consist of "
491                            "keys", opts->name, i);
492                 goto out;
493             }
494 
495             opt_name = g_strdup_printf("%s.%u", opts->name, i++);
496             subopts = qemu_opts_create(opts, opt_name, 1, errp);
497             g_free(opt_name);
498             if (!subopts) {
499                 goto out;
500             }
501 
502             if (!qemu_opts_absorb_qdict(subopts, section, errp)) {
503                 qemu_opts_del(subopts);
504                 goto out;
505             }
506 
507             if (qdict_size(section)) {
508                 error_setg(errp, "[%s] section doesn't support the option '%s'",
509                            opts->name, qdict_first(section)->key);
510                 qemu_opts_del(subopts);
511                 goto out;
512             }
513         }
514     }
515 
516 out:
517     qobject_unref(subqdict);
518     qobject_unref(list);
519 }
520 
521 void qemu_config_parse_qdict(QDict *options, QemuOptsList **lists,
522                              Error **errp)
523 {
524     int i;
525     Error *local_err = NULL;
526 
527     for (i = 0; lists[i]; i++) {
528         config_parse_qdict_section(options, lists[i], &local_err);
529         if (local_err) {
530             error_propagate(errp, local_err);
531             return;
532         }
533     }
534 }
535