Skip to contentSkip to conversation

vl: add -object support back into -readconfig

PATCH Superseded Paolo Bonzini 2 participants 6 messages May 21, 2021

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover vl: add -object support back into -readconfig

LXD developers have reported that [object] stanzas have stopped working in configuration files.

The problem is that QEMU 6.0 switched the creation of objects from qemu_opts_foreach to a bespoke QTAILQ, in preparation for supporting JSON syntax in -object. Entries from the configuration file however do not go through object_option_parse, and are thus lost. Of the many fixes that are possible, I chose one that is slightly more invasive but more consistent with the plans for keyval-ification of options such as -M and -accel.

-set was also broken by the same change. For simplicity I chose not to add it back yet, however this series will report the breakage instead of failing silently.

The first two patches of this series are thus a reduced version of
https://patchew.org/QEMU/[email protected]/
([PATCH 00/14] vl: compound properties for machines and accelerators), with the -set infrastructure removed. The third is very simple and uses the newly-provided hooks to parse objects from configuration files.

Paolo

Based-on: <[email protected]>

Paolo Bonzini (3):
qemu-config: parse configuration files to a QDict
vl: plumb keyval-based options into -readconfig
vl: plug -object back into -readconfig

 include/block/qdict.h      |   2 -
 include/qapi/qmp/qdict.h   |   3 ++
 include/qemu/config-file.h |   7 ++-
 softmmu/vl.c               | 105 +++++++++++++++++++++++++++----------
 util/qemu-config.c         |  98 +++++++++++++++++++++++-----------
 5 files changed, 153 insertions(+), 62 deletions(-)
Signature · 1 line
2.27.0
#1 lore raw

PATCH v3 1/3 qemu-config: parse configuration files to a QDict

Change the parser to put the values into a QDict and pass them to a callback. qemu_config_parse's QemuOpts creation is itself turned into a callback function.

This is useful for -readconfig to support keyval-based options; getting a QDict from the parser removes a roundtrip from QDict to QemuOpts and then back to QDict.

Unfortunately there is a disadvantage in that semantic errors will point to the last line of the group, because the entries of the QDict do not have a location attached.

---
v1->v2: check for unrecognized entries in the QDict, move a loc_pop for
better error messages

v2->v3: fix error propagation

 include/qemu/config-file.h |  7 ++-
 softmmu/vl.c               |  4 +-
 util/qemu-config.c         | 98 ++++++++++++++++++++++++++------------
 3 files changed, 76 insertions(+), 33 deletions(-)
include/qemu/config-file.h +6 -1
index 0500b3668d..f605423321 100644
@@ -1,6 +1,8 @@
 #ifndef QEMU_CONFIG_FILE_H
 #define QEMU_CONFIG_FILE_H
 
+typedef void QEMUConfigCB(const char *group, QDict *qdict, void *opaque, Error **errp);
+
 void qemu_load_module_for_opts(const char *group);
 QemuOptsList *qemu_find_opts(const char *group);
 QemuOptsList *qemu_find_opts_err(const char *group, Error **errp);
@@ -14,7 +16,10 @@ void qemu_config_write(FILE *fp);
 int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname,
                       Error **errp);
 
-int qemu_read_config_file(const char *filename, Error **errp);
+/* A default callback for qemu_read_config_file().  */
+void qemu_config_do_parse(const char *group, QDict *qdict, void *opaque, Error **errp);
+
+int qemu_read_config_file(const char *filename, QEMUConfigCB *f, Error **errp);
 
 /* Parse QDict options as a replacement for a config file (allowing multiple
    enumerated (0..(n-1)) configuration "sections") */
softmmu/vl.c +2 -2
index 6054f6f0b9..47dfdd704f 100644
@@ -2133,7 +2133,7 @@ static void qemu_read_default_config_file(Error **errp)
     int ret;
     g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
 
-    ret = qemu_read_config_file(file, errp);
+    ret = qemu_read_config_file(file, qemu_config_do_parse, errp);
     if (ret < 0) {
         if (ret == -ENOENT) {
             error_free(*errp);
@@ -3399,7 +3399,7 @@ void qemu_init(int argc, char **argv, char **envp)
                 qemu_plugin_opt_parse(optarg, &plugin_list);
                 break;
             case QEMU_OPTION_readconfig:
-                qemu_read_config_file(optarg, &error_fatal);
+                qemu_read_config_file(optarg, qemu_config_do_parse, &error_fatal);
                 break;
             case QEMU_OPTION_spice:
                 olist = qemu_find_opts_err("spice", NULL);
util/qemu-config.c +68 -30

150 lines not on this page · raw message

Signature · 1 line
2.27.0
#2 lore raw

Re: [PATCH v3 1/3] qemu-config: parse configuration files to a QDict

Am 21.05.2021 um 12:21 hat Paolo Bonzini geschrieben:

Quoted 31 lines

Change the parser to put the values into a QDict and pass them to a callback. qemu_config_parse's QemuOpts creation is itself turned into a callback function.

This is useful for -readconfig to support keyval-based options; getting a QDict from the parser removes a roundtrip from QDict to QemuOpts and then back to QDict.

Unfortunately there is a disadvantage in that semantic errors will point to the last line of the group, because the entries of the QDict do not have a location attached.

---
v1->v2: check for unrecognized entries in the QDict, move a loc_pop for
better error messages

v2->v3: fix error propagation

util/qemu-config.c +9 -0
+void qemu_config_do_parse(const char *group, QDict *qdict, void *opaque, Error **errp)
+{
+    QemuOptsList **lists = opaque;
+    const char *id = qdict_get_try_str(qdict, "id");
+    QemuOptsList *list;
+    QemuOpts *opts;
+    const QDictEntry *unrecognized;
+
+    list = find_list(lists, group, errp);

util/qemu-config.c int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname, Error * · line 447

Quoted 10 lines
util/qemu-config.c +10 -0
+    if (!list) {
+        return;
+    }
+
+    opts = qemu_opts_create(list, id, 1, errp);
+    if (!opts) {
+        return;
+    }
+    qemu_opts_absorb_qdict(opts, qdict, errp);
+    if (errp) {

This doesn't do what you wanted.

*errp would be be better, but you also need ERRP_GUARD() then.

The other option would be using the return value of qemu_opts_absorb_qdict().

util/qemu-config.c int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname, Error * · line 455

Quoted 8 lines
util/qemu-config.c +8 -0
+        return;
+    }
+    unrecognized = qdict_first(qdict);
+    if (unrecognized) {
+        error_setg(errp, QERR_INVALID_PARAMETER, unrecognized->key);
+        qemu_opts_del(opts);
+    }
+}

Kevin

#3 lore raw

Re: [PATCH v3 1/3] qemu-config: parse configuration files to a QDict

Quoted 9 lines · Kevin Wolf

util/qemu-config.c int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname, Error * · line 447

Quoted 2 lines
util/qemu-config.c +2 -0
+    qemu_opts_absorb_qdict(opts, qdict, errp);
+    if (errp) {

This doesn't do what you wanted.

*errp would be be better, but you also need ERRP_GUARD() then.

The other option would be using the return value of qemu_opts_absorb_qdict().

Uff, you're absolutely right. I'm a trainwreck today...

Paolo

#4 lore raw

PATCH v3 2/3 vl: plumb keyval-based options into -readconfig

In reply to Paolo Bonzini

Let -readconfig support parsing command line options into QDict or QemuOpts. This will be used to add back support for objects in -readconfig.

---
v1->v2: fix overlong line

 include/block/qdict.h    |  2 -
 include/qapi/qmp/qdict.h |  3 ++
 softmmu/vl.c             | 83 ++++++++++++++++++++++++++++------------
 3 files changed, 62 insertions(+), 26 deletions(-)
include/block/qdict.h +0 -2
index d8cb502d7d..ced2acfb92 100644
@@ -20,8 +20,6 @@ void qdict_join(QDict *dest, QDict *src, bool overwrite);
 void qdict_extract_subqdict(QDict *src, QDict **dst, const char *start);
 void qdict_array_split(QDict *src, QList **dst);
 int qdict_array_entries(QDict *src, const char *subqdict);
-QObject *qdict_crumple(const QDict *src, Error **errp);
-void qdict_flatten(QDict *qdict);
 
 typedef struct QDictRenames {
     const char *from;
include/qapi/qmp/qdict.h +3 -0
index 9934539c1b..d5b5430e21 100644
@@ -64,4 +64,7 @@ const char *qdict_get_try_str(const QDict *qdict, const char *key);
 
 QDict *qdict_clone_shallow(const QDict *src);
 
+QObject *qdict_crumple(const QDict *src, Error **errp);
+void qdict_flatten(QDict *qdict);
+
 #endif /* QDICT_H */
softmmu/vl.c +59 -24

129 lines not on this page · raw message

Signature · 1 line
2.27.0
#5 lore raw

PATCH v3 3/3 vl: plug -object back into -readconfig

In reply to Paolo Bonzini

Commit bc2f4fcb1d ("qom: move user_creatable_add_opts logic to vl.c and QAPIfy it", 2021-03-19) switched the creation of objects from qemu_opts_foreach to a bespoke QTAILQ in preparation for supporting JSON syntax in -object.

Unfortunately in doing so it lost support for [object] stanzas in configuration files and also for "-set object.ID.KEY=VAL". The latter is hard to re-establish and probably best solved by deprecating -set. This patch uses the infrastructure introduced by the previous two patches in order to parse QOM objects correctly from configuration files.

---
 softmmu/vl.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
softmmu/vl.c +18 -6
index 5e8240b9d8..326c1e9080 100644
@@ -1722,9 +1722,15 @@ static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
     }
 }
 
+static void object_option_add_visitor(Visitor *v)
+{
+    ObjectOption *opt = g_new0(ObjectOption, 1);
+    visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
+    QTAILQ_INSERT_TAIL(&object_opts, opt, next);
+}
+
 static void object_option_parse(const char *optarg)
 {
-    ObjectOption *opt;
     QemuOpts *opts;
     const char *type;
     Visitor *v;
@@ -1752,11 +1758,8 @@ static void object_option_parse(const char *optarg)
         v = opts_visitor_new(opts);
     }
 
-    opt = g_new0(ObjectOption, 1);
-    visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
+    object_option_add_visitor(v);
     visit_free(v);
-
-    QTAILQ_INSERT_TAIL(&object_opts, opt, next);
 }
 
 /*
@@ -2134,13 +2137,22 @@ static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
  */
 static bool is_qemuopts_group(const char *group)
 {
+    if (g_str_equal(group, "object")) {
+        return false;
+    }
     return true;
 }
 
 static void qemu_record_config_group(const char *group, QDict *dict,
                                      bool from_json, Error **errp)
 {
-    abort();
+    if (g_str_equal(group, "object")) {
+        Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
+        object_option_add_visitor(v);
+        visit_free(v);
+    } else {
+        abort();
+    }
 }
 
 /*
Signature · 1 line
2.27.0