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