qemu-option, keyval, vl: switch -object/-M/-accel to keyval parsing
cover qemu-option, keyval, vl: switch -object/-M/-accel to keyval parsing
This series switches -object, -M and -accel from QemuOpts to keyval. Monitor commands device_add and netdev_add are also switched to keyval, though -device and -netdev for now are not.
Along the way, the syntax of keyval and QemuOpts becomes more consistent and support for keyval-based options is added to -readconfig. -writeconfig instead is removed (see patch 13 for rationale).
The reason to do this is:
- to make qemu-io, qemu-nbd, qemu-img and QEMU's parsing of -object consistent with qemu-storage-daemon's
- to allow using compound properties in -object, -M and -accel
Patch 1-5: make QemuOpts parsing a bit more restrictive, warning for short-form boolean options and removing weird ways to request help such as "help=foo" or "no?".
Patch 6-12: let keyval accept escaped commas in implied options, switch comma-separated syntax for HMP from QemuOpts to keyval, add help support to object_add
Patch 13-18: plumbing for reading keyval-based options in vl.c, including -set and -readconfig.
Patch 19-23: switch -object to keyval everywhere
Patch 24-28: switch -M and -accel to keyval
Paolo Bonzini (28):
qemu-option: simplify search for end of key
qemu-option: pass QemuOptsList to opts_accepts_any
qemu-option: clean up id vs. list->merge_lists
qemu-option: move help handling to get_opt_name_value
qemu-option: warn for short-form boolean options
keyval: accept escaped commas in implied option
keyval: simplify keyval_parse_one
tests: convert check-qom-proplist to keyval
keyval: introduce keyval_parse_into
hmp: replace "O" parser with keyval
qom: use qemu_printf to print help for user-creatable objects
hmp: special case help options for object_add
remove -writeconfig
qemu-config: add error propagation to qemu_config_parse
qemu-option: support accept-any QemuOptsList in qemu_opts_absorb_qdict
qemu-config: parse configuration files to a QDict
vl: plumb keyval-based options into -set and -readconfig
qom: do not modify QDict argument in user_creatable_add_dict
qemu-io: use keyval for -object parsing
qemu-nbd: use keyval for -object parsing
qemu-img: use keyval for -object parsing
qemu: use keyval for -object parsing
storage-daemon: do not register the "object" group with QemuOpts
qom: export more functions for use with non-UserCreatable objects
vl: rename local variable in configure_accelerators
vl: switch -M parsing to keyval
qemu-option: remove now-dead code
vl: switch -accel parsing to keyval
accel/accel.c | 6 + block/blkdebug.c | 3 +- docs/system/deprecated.rst | 6 + hmp-commands.hx | 6 +- include/block/qdict.h | 2 - include/qapi/qmp/qdict.h | 3 + include/qemu/config-file.h | 9 +- include/qemu/help_option.h | 11 - include/qemu/option.h | 6 +- include/qom/object.h | 21 + include/qom/object_interfaces.h | 68 +-- include/sysemu/accel.h | 1 + monitor/hmp-cmds.c | 22 +- monitor/hmp.c | 20 +- qemu-img.c | 258 ++-------- qemu-io.c | 42 +- qemu-nbd.c | 42 +- qemu-options.hx | 13 +- qom/object_interfaces.c | 152 ++---- softmmu/vl.c | 687 ++++++++++++++------------- storage-daemon/qemu-storage-daemon.c | 10 - tests/check-qom-proplist.c | 58 ++- tests/test-keyval.c | 53 ++- tests/test-qemu-opts.c | 37 +- util/keyval.c | 230 +++++---- util/qemu-config.c | 141 +++--- util/qemu-option.c | 184 ++++--- 27 files changed, 946 insertions(+), 1145 deletions(-)
Signature · 1 line
2.26.2
PATCH 1/28 qemu-option: simplify search for end of key
Use strcspn to find an equal or comma value, and pass the result directly to get_opt_name to avoid another strchr.
--- util/qemu-option.c | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-)
util/qemu-option.c +13 -22
@@ -38,27 +38,19 @@#include "qemu/help_option.h" /* -* Extracts the name of an option from the parameter string (p points at the +* Extracts the name of an option from the parameter string (@p points at the * first byte of the option name) * -* The option name is delimited by delim (usually , or =) or the string end -* and is copied into option. The caller is responsible for free'ing option -* when no longer required. +* The option name is @len characters long and is copied into @option. The +* caller is responsible for free'ing @option when no longer required. * * The return value is the position of the delimiter/zero byte after the option -* name in p. +* name in @p. */ -static const char *get_opt_name (const char *p,char **option,char delim) +static const char *get_opt_name (const char *p,char **option,size_t len) { -char *offset =strchr (p, delim); - -if (offset) { - *option =g_strndup (p, offset - p); -return offset; - }else { - *option =g_strdup (p); -return p +strlen (p); - } + *option =g_strndup (p, len); +return p + len; }/*
@@ -766,12 +758,11 @@ static const char *get_opt_name_value(const char *params,const char *firstname, char **name, char **value) { - const char *p, *pe, *pc; - - pe =strchr (params,'=' ); - pc =strchr (params,',' ); + const char *p; +size_t len; -if (!pe || (pc && pc < pe)) { + len =strcspn (params,"=," ); +if (params[len] !='=' ) {/* found "foo,more" */ if (firstname) {/* implicitly named first option */
@@ -779,7 +770,7 @@ static const char *get_opt_name_value(const char *params, p =get_opt_value (params, value); }else {/* option without value, must be a flag */ - p =get_opt_name (params, name,',' ); + p =get_opt_name (params, name, len);if (strncmp (*name,"no" ,2 ) ==0 ) {memmove (*name, *name +2 ,strlen (*name +2 ) +1 ); *value =g_strdup ("off" );
@@ -789,7 +780,7 @@ static const char *get_opt_name_value(const char *params, } }else {/* found "foo=bar,more" */ - p =get_opt_name (params, name,'=' ); + p =get_opt_name (params, name, len);assert (*p =='=' ); p++; p =get_opt_value (p, value);
Signature · 1 line
2.26.2
PATCH 2/28 qemu-option: pass QemuOptsList to opts_accepts_any
In reply to Paolo BonziniA QemuOptsList can be of one of two kinds: either it is pre-validated, or it accepts any key and validation happens somewhere else (typically in a Visitor or against a list of QOM properties). opts_accepts_any returns true if a QemuOpts instance was created from a QemuOptsList of the latter kind, but there is no function to do the check on a QemuOptsList.
We will need it in the next patch; since almost all callers of opts_accepts_any use opts->list anyway, simply repurpose it instead of adding a new function.
--- util/qemu-option.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-)
util/qemu-option.c +13 -10
@@ -460,16 +460,16 @@ static bool qemu_opt_parse(QemuOpt *opt, Error **errp) } } -static bool opts_accepts_any (const QemuOpts *opts) +static bool opts_accepts_any (const QemuOptsList *list) { -return opts->list->desc[0 ].name ==NULL ; +return list->desc[0 ].name ==NULL ; }int qemu_opt_unset (QemuOpts *opts,const char *name) {QemuOpt *opt =qemu_opt_find (opts, name); -assert (opts_accepts_any (opts)); +assert (opts_accepts_any (opts->list));if (opt ==NULL ) {return -1 ;
@@ -500,9 +500,10 @@ static bool opt_validate(QemuOpt *opt, bool *help_wanted, Error **errp) {const QemuOptDesc *desc; +const QemuOptsList *list = opt->opts->list; - desc =find_desc_by_name (opt->opts->list->desc, opt->name); -if (!desc && !opts_accepts_any (opt->opts)) { + desc =find_desc_by_name (list->desc, opt->name); +if (!desc && !opts_accepts_any (list)) {error_setg (errp,QERR_INVALID_PARAMETER , opt->name);if (help_wanted &&is_help_option (opt->name)) { *help_wanted = true;
@@ -535,9 +536,10 @@ bool qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val, {QemuOpt *opt;const QemuOptDesc *desc; +const QemuOptsList *list = opts->list; - desc =find_desc_by_name (opts->list->desc, name); -if (!desc && !opts_accepts_any (opts)) { + desc =find_desc_by_name (list->desc, name); +if (!desc && !opts_accepts_any (list)) {error_setg (errp,QERR_INVALID_PARAMETER , name);return false; }
@@ -557,9 +559,10 @@ bool qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val, {QemuOpt *opt;const QemuOptDesc *desc; +const QemuOptsList *list = opts->list; - desc =find_desc_by_name (opts->list->desc, name); -if (!desc && !opts_accepts_any (opts)) { + desc =find_desc_by_name (list->desc, name); +if (!desc && !opts_accepts_any (list)) {error_setg (errp,QERR_INVALID_PARAMETER , name);return false; }
@@ -1107,7 +1110,7 @@ bool qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp) {QemuOpt *opt; -assert (opts_accepts_any (opts)); +assert (opts_accepts_any (opts->list)); QTAILQ_FOREACH(opt, &opts->head, next) { opt->desc =find_desc_by_name (desc, opt->name);
Signature · 1 line
2.26.2
PATCH 3/28 qemu-option: clean up id vs. list->merge_lists
In reply to Paolo BonziniLooking at all merge-lists QemuOptsList, here is how they access their QemuOpts:
reopen_opts in qemu-io-cmds.c ("qemu-img reopen -o")
qemu_opts_find(&reopen_opts, NULL)
empty_opts in qemu-io.c ("qemu-io open -o")
qemu_opts_find(&empty_opts, NULL)
qemu_rtc_opts ("-rtc")
qemu_find_opts_singleton("rtc")
qemu_machine_opts ("-M")
qemu_find_opts_singleton("machine")
qemu_boot_opts ("-boot")
QTAILQ_FIRST(&qemu_find_opts("bootopts")->head)qemu_name_opts ("-name")
qemu_opts_foreach->parse_name parse_name does not use id
qemu_mem_opts ("-m")
qemu_find_opts_singleton("memory")
qemu_icount_opts ("-icount")
qemu_opts_foreach->do_configuree_icount do_configure_icount->icount_configure icount_configure does not use id
qemu_smp_opts ("-smp")
qemu_opts_find(qemu_find_opts("smp-opts"), NULL)
qemu_spice_opts ("-spice")
QTAILQ_FIRST(&qemu_spice_opts.head)
i.e. they don't need an id. Sometimes its presence is ignored (e.g. when using qemu_opts_foreach), sometimes all the options with the id are skipped, sometimes only the first option on the command line is considered. With this patch we just forbid id on merge-lists QemuOptsLists; if the command line still works, it has the same semantics as before.
qemu_opts_create's fail_if_exists parameter is now unnecessary:
- it is unused if id is NULL
- opts_parse only passes false if reached from qemu_opts_set_defaults, in which case this patch enforces that id must be NULL
- other callers that can pass a non-NULL id always set it to true
Assert that it is true in the only case where "fail_if_exists" matters, i.e. "id && !lists->merge_lists". This means that if an id is present, duplicates are always forbidden, which was already the status quo.
Discounting the case that aborts as it's not user-controlled (it's "just" a matter of inspecting qemu_opts_create callers), the paths through qemu_opts_create can be summarized as:
- merge_lists = true: singleton opts with NULL id; non-NULL id fails
- merge_lists = false: always return new opts; non-NULL id fails if dup
--- util/qemu-option.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)
util/qemu-option.c +14 -13
@@ -619,7 +619,17 @@ QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, {QemuOpts *opts =NULL ; -if (id) { +if (list->merge_lists) { +if (id) { +error_setg (errp,QERR_INVALID_PARAMETER ,"id" ); +return NULL ; + } + opts =qemu_opts_find (list,NULL ); +if (opts) { +return opts; + } + }else if (id) { +assert (fail_if_exists);if (!id_wellformed (id)) {error_setg (errp,QERR_INVALID_PARAMETER_VALUE ,"id" ,"an identifier" );
@@ -629,17 +639,8 @@ QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, } opts =qemu_opts_find (list, id);if (opts !=NULL ) { -if (fail_if_exists && !list->merge_lists) { -error_setg (errp,"Duplicate ID '%s' for %s" , id, list->name); -return NULL ; - }else { -return opts; - } - } - }else if (list->merge_lists) { - opts =qemu_opts_find (list,NULL ); -if (opts) { -return opts; +error_setg (errp,"Duplicate ID '%s' for %s" , id, list->name); +return NULL ; } } opts =g_malloc0 (sizeof (*opts));
@@ -893,7 +894,7 @@ static QemuOpts *opts_parse(QemuOptsList *list, const char *params,* (if unlikely) future misuse: */ assert (!defaults || list->merge_lists); - opts =qemu_opts_create (list, id, !defaults, errp); + opts =qemu_opts_create (list, id, !list->merge_lists, errp);g_free (id);if (opts ==NULL ) {return NULL ;
Signature · 1 line
2.26.2
PATCH 4/28 qemu-option: move help handling to get_opt_name_value
In reply to Paolo BonziniRight now, help options are parsed normally and then checked specially in opt_validate, but only if coming from qemu_opts_parse_noisily. has_help_option does the check on its own.
opt_validate() has two callers: qemu_opt_set(), which passes null and is therefore unaffected, and opts_do_parse(), which is affected.
opts_do_parse() is called by qemu_opts_do_parse(), which passes null and is therefore unaffected, and opts_parse().
opts_parse() is called by qemu_opts_parse() and qemu_opts_set_defaults(), which pass null and are therefore unaffected, and qemu_opts_parse_noisily().
Move the check from opt_validate to the parsing workhorse of QemuOpts, get_opt_name_value. This will come in handy in the next patch, which will raise a warning for "-object memory-backend-ram,share" ("flag" option with no =on/=off part) but not for "-object memory-backend-ram,help".
As a result:
- opts_parse and opts_do_parse do not return an error anymore when help is requested; qemu_opts_parse_noisily does not have to work around that anymore.
- various crazy ways to request help are not recognized anymore:
- "help=..."
- "nohelp" (sugar for "help=off")
- "?=..."
- "no?" (sugar for "?=off")
--- util/qemu-option.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-)
util/qemu-option.c +23 -15
@@ -496,8 +496,7 @@ static QemuOpt *opt_create(QemuOpts *opts, const char *name, char *value,return opt; } -static bool opt_validate (QemuOpt *opt,bool *help_wanted, -Error **errp) +static bool opt_validate (QemuOpt *opt,Error **errp) {const QemuOptDesc *desc;const QemuOptsList *list = opt->opts->list;
@@ -505,9 +504,6 @@ static bool opt_validate(QemuOpt *opt, bool *help_wanted, desc =find_desc_by_name (list->desc, opt->name);if (!desc && !opts_accepts_any (list)) {error_setg (errp,QERR_INVALID_PARAMETER , opt->name); -if (help_wanted &&is_help_option (opt->name)) { - *help_wanted = true; - }return false; }
@@ -524,7 +520,7 @@ bool qemu_opt_set(QemuOpts *opts, const char *name, const char *value, {QemuOpt *opt =opt_create (opts, name,g_strdup (value), false); -if (!opt_validate (opt,NULL , errp)) { +if (!opt_validate (opt, errp)) {qemu_opt_del (opt);return false; }
@@ -760,10 +756,12 @@ void qemu_opts_print(QemuOpts *opts, const char *separator)static const char *get_opt_name_value (const char *params,const char *firstname, +bool *help_wanted,char **name,char **value) {const char *p;size_t len; +bool is_help = false; len =strcspn (params,"=," );if (params[len] !='=' ) {
@@ -780,6 +778,7 @@ static const char *get_opt_name_value(const char *params, *value =g_strdup ("off" ); }else { *value =g_strdup ("on" ); + is_help =is_help_option (*name); } } }else {
@@ -791,6 +790,9 @@ static const char *get_opt_name_value(const char *params, }assert (!*p || *p ==',' ); +if (help_wanted && is_help) { + *help_wanted = true; + }if (*p ==',' ) { p++; }
@@ -806,7 +808,12 @@ static bool opts_do_parse(QemuOpts *opts, const char *params,QemuOpt *opt;for (p = params; *p;) { - p =get_opt_name_value (p, firstname, &option, &value); + p =get_opt_name_value (p, firstname, help_wanted, &option, &value); +if (help_wanted && *help_wanted) { +g_free (option); +g_free (value); +return false; + } firstname =NULL ;if (!strcmp (option,"id" )) {
@@ -817,7 +824,7 @@ static bool opts_do_parse(QemuOpts *opts, const char *params, opt =opt_create (opts, option, value, prepend);g_free (option); -if (!opt_validate (opt, help_wanted, errp)) { +if (!opt_validate (opt, errp)) {qemu_opt_del (opt);return false; }
@@ -832,7 +839,7 @@ static char *opts_parse_id(const char *params)char *name, *value;for (p = params; *p;) { - p =get_opt_name_value (p,NULL , &name, &value); + p =get_opt_name_value (p,NULL ,NULL , &name, &value);if (!strcmp (name,"id" )) {g_free (name);return value;
@@ -848,11 +855,10 @@ bool has_help_option(const char *params) {const char *p;char *name, *value; -bool ret; +bool ret = false;for (p = params; *p;) { - p =get_opt_name_value (p,NULL , &name, &value); - ret =is_help_option (name); + p =get_opt_name_value (p,NULL , &ret, &name, &value);g_free (name);g_free (value);if (ret) {
@@ -937,11 +943,13 @@ QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params,QemuOpts *opts;bool help_wanted = false; - opts =opts_parse (list, params, permit_abbrev, false, &help_wanted, &err); -if (err) { + opts =opts_parse (list, params, permit_abbrev, false, +opts_accepts_any (list) ?NULL : &help_wanted, + &err); +if (!opts) { +assert (!!err + !!help_wanted ==1 );if (help_wanted) {qemu_opts_print_help (list, true); -error_free (err); }else {error_report_err (err); }
Signature · 1 line
2.26.2
PATCH 5/28 qemu-option: warn for short-form boolean options
In reply to Paolo BonziniOptions such as "server" or "nowait", that are commonly found in -chardev, are sugar for "server=on" and "wait=off". This is quite surprising and also does not have any notion of typing attached. It is even possible to do "-device e1000,noid" and get a device with "id=off".
Deprecate it and print a warning when it is encountered. In general, this short form for boolean options only seems to be in wide use for -chardev and -spice.
--- docs/system/deprecated.rst | 6 ++++++ tests/test-qemu-opts.c | 2 +- util/qemu-option.c | 29 ++++++++++++++++++----------- 3 files changed, 25 insertions(+), 12 deletions(-)
docs/system/deprecated.rst +6 -0
@@ -146,6 +146,12 @@ Drives with interface types other than ``if=none`` are for onboard devices. It is possible to use drives the board doesn't pick up with -device. This usage is now deprecated. Use ``if=none`` instead. +Short-form boolean options (since 5.2) +'''''''''''''''''''''''''''''''''''''' + +Boolean options such as ``share=on``/``share=off`` can be written +in short form as ``share`` and ``noshare``. This is deprecated +and will cause a warning. QEMU Machine Protocol (QMP) commands ------------------------------------
tests/test-qemu-opts.c +1 -1
@@ -515,7 +515,7 @@ static void test_opts_parse(void)error_free_or_abort (&err);g_assert (!opts); -/* Implied value */ +/* Implied value (qemu_opts_parse warns but accepts it) */ opts =qemu_opts_parse (&opts_list_03,"an,noaus,noaus=" , false, &error_abort); g_assert_cmpuint(opts_count(opts), ==, 3);
util/qemu-option.c +18 -11
@@ -756,10 +756,12 @@ void qemu_opts_print(QemuOpts *opts, const char *separator)static const char *get_opt_name_value (const char *params,const char *firstname, +bool warn_on_flag,bool *help_wanted,char **name,char **value) {const char *p; +const char *prefix ="" ;size_t len;bool is_help = false;
@@ -776,10 +778,15 @@ static const char *get_opt_name_value(const char *params,if (strncmp (*name,"no" ,2 ) ==0 ) {memmove (*name, *name +2 ,strlen (*name +2 ) +1 ); *value =g_strdup ("off" ); + prefix ="no" ; }else { *value =g_strdup ("on" ); is_help =is_help_option (*name); } +if (!is_help && warn_on_flag) { +warn_report ("short-form boolean option '%s%s' deprecated" , prefix, *name); +error_printf ("Please use %s=%s instead\n" , *name, *value); + } } }else {/* found "foo=bar,more" */
@@ -801,14 +808,14 @@ static const char *get_opt_name_value(const char *params,static bool opts_do_parse (QemuOpts *opts,const char *params,const char *firstname,bool prepend, -bool *help_wanted,Error **errp) +bool warn_on_flag,bool *help_wanted,Error **errp) {char *option, *value;const char *p;QemuOpt *opt;for (p = params; *p;) { - p =get_opt_name_value (p, firstname, help_wanted, &option, &value); + p =get_opt_name_value (p, firstname, warn_on_flag, help_wanted, &option, &value);if (help_wanted && *help_wanted) {g_free (option);g_free (value);
@@ -839,7 +846,7 @@ static char *opts_parse_id(const char *params)char *name, *value;for (p = params; *p;) { - p =get_opt_name_value (p,NULL ,NULL , &name, &value); + p =get_opt_name_value (p,NULL , false,NULL , &name, &value);if (!strcmp (name,"id" )) {g_free (name);return value;
@@ -858,7 +865,7 @@ bool has_help_option(const char *params)bool ret = false;for (p = params; *p;) { - p =get_opt_name_value (p,NULL , &ret, &name, &value); + p =get_opt_name_value (p,NULL , false, &ret, &name, &value);g_free (name);g_free (value);if (ret) {
@@ -878,12 +885,12 @@ bool has_help_option(const char *params)bool qemu_opts_do_parse (QemuOpts *opts,const char *params,const char *firstname,Error **errp) { -return opts_do_parse (opts, params, firstname, false,NULL , errp); +return opts_do_parse (opts, params, firstname, false, false,NULL , errp); }static QemuOpts *opts_parse (QemuOptsList *list,const char *params,bool permit_abbrev,bool defaults, -bool *help_wanted,Error **errp) +bool warn_on_flag,bool *help_wanted,Error **errp) {const char *firstname;char *id =opts_parse_id (params);
@@ -906,8 +913,8 @@ static QemuOpts *opts_parse(QemuOptsList *list, const char *params,return NULL ; } -if (!opts_do_parse (opts, params, firstname, defaults, help_wanted, - errp)) { +if (!opts_do_parse (opts, params, firstname, defaults, + warn_on_flag, help_wanted, errp)) {qemu_opts_del (opts);return NULL ; }
@@ -925,7 +932,7 @@ static QemuOpts *opts_parse(QemuOptsList *list, const char *params,QemuOpts *qemu_opts_parse (QemuOptsList *list,const char *params,bool permit_abbrev,Error **errp) { -return opts_parse (list, params, permit_abbrev, false,NULL , errp); +return opts_parse (list, params, permit_abbrev, false, false,NULL , errp); }/**
@@ -943,7 +950,7 @@ QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params,QemuOpts *opts;bool help_wanted = false; - opts =opts_parse (list, params, permit_abbrev, false, + opts =opts_parse (list, params, permit_abbrev, false, true,opts_accepts_any (list) ?NULL : &help_wanted, &err);if (!opts) {
@@ -962,7 +969,7 @@ void qemu_opts_set_defaults(QemuOptsList *list, const char *params, {QemuOpts *opts; - opts =opts_parse (list, params, permit_abbrev, true,NULL ,NULL ); + opts =opts_parse (list, params, permit_abbrev, true, false,NULL ,NULL );assert (opts); }
Signature · 1 line
2.26.2
PATCH 6/28 keyval: accept escaped commas in implied option
In reply to Paolo BonziniThis is used with the weirdly-named device "SUNFD,fdtwo":
$ qemu-system-sparc -device SUNW,,fdtwo,help
SUNW,fdtwo options:
drive=<str> - Node name or ID of a block device to use as a backend
fallback=<FdcDriveType> - FDC drive type, 144/288/120/none/auto (default: "144")
...Therefore, accepting it is a preparatory step towards keyval-ifying -device and the device_add monitor command. In general, however, this unexpected wart of the keyval syntax leads to suboptimal errors compared to QemuOpts:
$ ./qemu-system-x86_64 -object foo,,bar,id=obj qemu-system-x86_64: -object foo,,bar,id=obj: invalid object type: foo,bar $ storage-daemon/qemu-storage-daemon --object foo,,bar,id=obj qemu-storage-daemon: Invalid parameter ''
To implement this, the flow of the parser is changed to first unescape everything up to the next comma or equal sign. This is done in a new function keyval_fetch_string for both the key and value part. Keys therefore are now parsed in unescaped form, but this makes no difference in practice because a comma is an invalid character for a QAPI name. Thus keys with a comma in them are rejected anyway, as demonstrated by the new testcase.
As a side effect of the new code, parse errors are slightly improved as
well: "Invalid parameter ''" becomes "Expected parameter before '='" when keyval is fed a string starting with an equal sign.
The slightly baroque interface of keyval_fetch_string lets me keep the key parsing loop mostly untouched. It is simplified in the next patch, however.
--- include/qemu/help_option.h | 11 --- tests/test-keyval.c | 21 +++--- util/keyval.c | 142 ++++++++++++++++++++----------------- 3 files changed, 91 insertions(+), 83 deletions(-)
include/qemu/help_option.h +0 -11
@@ -19,15 +19,4 @@ static inline bool is_help_option(const char *s) return !strcmp(s, "?") || !strcmp(s, "help"); } -static inline int starts_with_help_option (const char *s) -{ -if (*s =='?' ) { -return 1 ; - } -if (g_str_has_prefix (s,"help" )) { -return 4 ; - } -return 0 ; -} -#endif
tests/test-keyval.c +13 -8
@@ -89,6 +89,11 @@ static void test_keyval_parse(void)error_free_or_abort (&err);g_assert (!qdict); +/* Keys must be QAPI identifiers */ + qdict =keyval_parse ("weird,,=key" ,NULL ,NULL , &err); +error_free_or_abort (&err); +g_assert (!qdict); +/* Multiple keys, last one wins */ qdict =keyval_parse ("a=1,b=2,,x,a=3" ,NULL ,NULL , &error_abort); g_assert_cmpuint(qdict_size(qdict), ==, 2);
@@ -178,15 +183,15 @@ static void test_keyval_parse(void)error_free_or_abort (&err);g_assert (!qdict); -/* Likewise (qemu_opts_parse(): implied key with comma value) */ - qdict =keyval_parse (",,,a=1" ,"implied" ,NULL , &err); -error_free_or_abort (&err); -g_assert (!qdict); +/* Implied key's value can have a comma */ + qdict =keyval_parse (",,,a=1" ,"implied" ,NULL , &error_abort); + g_assert_cmpstr(qdict_get_try_str(qdict, "implied"), ==, ","); + g_assert_cmpstr(qdict_get_try_str(qdict, "a"), ==, "1"); +qobject_unref (qdict); -/* Implied key's value can't have comma (qemu_opts_parse(): it can) */ - qdict =keyval_parse ("val,,ue" ,"implied" ,NULL , &err); -error_free_or_abort (&err); -g_assert (!qdict); + qdict =keyval_parse ("val,,ue" ,"implied" ,NULL , &error_abort); + g_assert_cmpstr(qdict_get_try_str(qdict, "implied"), ==, "val,ue"); +qobject_unref (qdict);/* Empty key is not an implied key */ qdict =keyval_parse ("=val" ,"implied" ,NULL , &err);
util/keyval.c +78 -64
224 lines not on this page · raw message
Signature · 1 line
2.26.2
PATCH 7/28 keyval: simplify keyval_parse_one
In reply to Paolo BonziniNow that the key is NULL terminated, we can remove some of the contortions that were done to operate on possibly '='-terminated strings in keyval_parse_one.
--- util/keyval.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-)
util/keyval.c +10 -17
@@ -170,11 +170,10 @@ static QObject *keyval_parse_put(QDict *cur,* * On return: * - either NUL or the separator (comma or equal sign) is returned. -* - the length of the string is stored in @len. * - @start is advanced to either the NUL or the first character past the * separator. */ -static char keyval_fetch_string (char **start,size_t *len,bool key) +static char keyval_fetch_string (char **start,bool key) {char sep;char *p, *unescaped;
@@ -197,7 +196,6 @@ static char keyval_fetch_string(char **start, size_t *len, bool key) } *unescaped =0 ; - *len = unescaped - *start; *start = p;return sep; }
@@ -219,7 +217,7 @@ static char *keyval_parse_one(QDict *qdict, char *params, const char *implied_key, bool *help, Error **errp) { - const char *key, *key_end, *s, *end; + const char *key, *s, *end;const char *val =NULL ;char sep;size_t len;
@@ -229,8 +227,8 @@ static char *keyval_parse_one(QDict *qdict, char *params,QObject *next; key = params; - sep =keyval_fetch_string (¶ms, &len, true); -if (!len) { + sep =keyval_fetch_string (¶ms, true); +if (!*key) {if (sep) {error_setg (errp,"Expected parameter before '%c%s'" , sep, params); }else {
@@ -247,13 +245,11 @@ static char *keyval_parse_one(QDict *qdict, char *params,/* Desugar implied key */ val = key; key = implied_key; - len =strlen (implied_key); }else {error_setg (errp,"No implicit parameter name for value '%s'" , key);return NULL ; } } - key_end = key + len;/* * Loop over key fragments: @s points to current fragment, it
@@ -269,24 +265,21 @@ static char *keyval_parse_one(QDict *qdict, char *params, ret =parse_qapi_name (s, false); len = ret <0 ?0 : ret; } -assert (s + len <= key_end); -if (!len || (s + len < key_end && s[len] !='.' )) { +if (!len || (s[len] !='\0' && s[len] !='.' )) {assert (key != implied_key); -error_setg (errp,"Invalid parameter '%.*s'" , - (int )(key_end - key), key); +error_setg (errp,"Invalid parameter '%s'" , key);return NULL ; }if (len >=sizeof (key_in_cur)) {assert (key != implied_key);error_setg (errp,"Parameter%s '%.*s' is too long" , - s != key || s + len != key_end ?" fragment" :"" , + s != key || s[len] =='.' ?" fragment" :"" , (int )len, s);return NULL ; }if (s != key) { - next =keyval_parse_put (cur, key_in_cur,NULL , - key, s -1 , errp); + next =keyval_parse_put (cur, key_in_cur,NULL , key, s -1 , errp);if (!next) {return NULL ; }
@@ -301,9 +294,9 @@ static char *keyval_parse_one(QDict *qdict, char *params,if (key != implied_key) { val = params; -keyval_fetch_string (¶ms, &len, false); +keyval_fetch_string (¶ms, false); } -if (!keyval_parse_put (cur, key_in_cur, val, key, key_end, errp)) { +if (!keyval_parse_put (cur, key_in_cur, val, key, s -1 , errp)) {return NULL ; }return params;
Signature · 1 line
2.26.2
PATCH 8/28 tests: convert check-qom-proplist to keyval
In reply to Paolo BonziniThe command-line creation test is using QemuOpts. Switch it to keyval, since all the -object command line options will follow qemu-storage-daemon and do the same.
--- tests/check-qom-proplist.c | 58 +++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 20 deletions(-)
tests/check-qom-proplist.c +38 -20
@@ -21,6 +21,8 @@#include "qemu/osdep.h" #include "qapi/error.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qmp/qobject.h" #include "qom/object.h" #include "qemu/module.h" #include "qemu/option.h"
@@ -400,42 +402,58 @@ static void test_dummy_createlist(void)static void test_dummy_createcmdl (void ) { -QemuOpts *opts; +QDict *qdict;DummyObject *dobj;Error *err =NULL ; +bool help;const char *params =TYPE_DUMMY \",id=dev0," \"bv=yes,sv=Hiss hiss hiss,av=platypus" ; -qemu_add_opts (&qemu_object_opts); - opts =qemu_opts_parse (&qemu_object_opts, params, true, &err); + qdict =keyval_parse (params,"qom-type" , &help, &err);g_assert (err ==NULL ); -g_assert (opts); +g_assert (qdict); +g_assert (!help); - dobj =DUMMY_OBJECT (user_creatable_add_opts (opts, &err)); +g_assert (user_creatable_add_dict (qdict, true, &err));g_assert (err ==NULL ); +qobject_unref (qdict); + + dobj =DUMMY_OBJECT (object_resolve_path_component (object_get_objects_root (), +"dev0" ));g_assert (dobj); g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");g_assert (dobj->bv == true);g_assert (dobj->av ==DUMMY_PLATYPUS ); + qdict =keyval_parse (params,"qom-type" , &help, &err); +g_assert (!user_creatable_add_dict (qdict, true, &err)); +g_assert (err); +g_assert (object_resolve_path_component (object_get_objects_root (),"dev0" ) + ==OBJECT (dobj)); +qobject_unref (qdict); +error_free (err); + err =NULL ; + + qdict =keyval_parse (params,"qom-type" , &help, &err);user_creatable_del ("dev0" , &error_abort); +g_assert (object_resolve_path_component (object_get_objects_root (),"dev0" ) + ==NULL ); -object_unref (OBJECT (dobj)); - -/* -* cmdline-parsing via qemu_opts_parse() results in a QemuOpts entry -* corresponding to the Object's ID to be added to the QemuOptsList -* for objects. To avoid having this entry conflict with future -* Objects using the same ID (which can happen in cases where -* qemu_opts_parse() is used to parse the object params, such as -* with hmp_object_add() at the time of this comment), we need to -* check for this in user_creatable_del() and remove the QemuOpts if -* it is present. -* -* The below check ensures this works as expected. -*/ -g_assert_null (qemu_opts_find (&qemu_object_opts,"dev0" )); +g_assert (user_creatable_add_dict (qdict, true, &err)); +g_assert (err ==NULL ); +qobject_unref (qdict); + + dobj =DUMMY_OBJECT (object_resolve_path_component (object_get_objects_root (), +"dev0" )); +g_assert (dobj); + g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss"); +g_assert (dobj->bv == true); +g_assert (dobj->av ==DUMMY_PLATYPUS ); +g_assert (object_resolve_path_component (object_get_objects_root (),"dev0" ) + ==OBJECT (dobj)); + +object_unparent (OBJECT (dobj)); }static void test_dummy_badenum (void )
Signature · 1 line
2.26.2
PATCH 9/28 keyval: introduce keyval_parse_into
In reply to Paolo BonziniAllow parsing multiple keyval sequences into the same dictionary.
--- include/qemu/option.h | 2 ++ util/keyval.c | 39 ++++++++++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-)
include/qemu/option.h +2 -0
@@ -147,6 +147,8 @@ void qemu_opts_print_help(QemuOptsList *list, bool print_caption);void qemu_opts_free (QemuOptsList *list);QemuOptsList *qemu_opts_append (QemuOptsList *dst,QemuOptsList *list); +QDict *keyval_parse_into (QDict *qdict,const char *params,const char *implied_key, +bool *p_help,Error **errp);QDict *keyval_parse (const char *params,const char *implied_key,bool *help,Error **errp);
util/keyval.c +32 -7
@@ -436,13 +436,12 @@ static QObject *keyval_listify(QDict *cur, GSList *key_of_cur, Error **errp)* If @p_help is not NULL, store whether help is requested there. * If @p_help is NULL and help is requested, fail. * -* On success, return a dictionary of the parsed keys and values. +* On success, return @dict, now filled with the parsed keys and values. * On failure, store an error through @errp and return NULL. */ -QDict *keyval_parse (const char *params,const char *implied_key, -bool *p_help,Error **errp) +QDict *keyval_parse_into (QDict *qdict,const char *params,const char *implied_key, +bool *p_help,Error **errp) { -QDict *qdict =qdict_new ();QObject *listified; g_autofree char *dup;char *s;
@@ -452,7 +451,6 @@ QDict *keyval_parse(const char *params, const char *implied_key,while (*s) { s =keyval_parse_one (qdict, s, implied_key, &help, errp);if (!s) { -qobject_unref (qdict);return NULL ; } implied_key =NULL ;
@@ -462,15 +460,42 @@ QDict *keyval_parse(const char *params, const char *implied_key, *p_help = help; }else if (help) {error_setg (errp,"Help is not available for this option" ); -qobject_unref (qdict);return NULL ; } listified =keyval_listify (qdict,NULL , errp);if (!listified) { -qobject_unref (qdict);return NULL ; }assert (listified ==QOBJECT (qdict));return qdict; } + +/* +* Parse @params in QEMU's traditional KEY=VALUE,... syntax. +* +* If @implied_key, the first KEY= can be omitted. @implied_key is +* implied then, and VALUE can't be empty or contain ',' or '='. +* +* A parameter "help" or "?" without a value isn't added to the +* resulting dictionary, but instead is interpreted as help request. +* All other options are parsed and returned normally so that context +* specific help can be printed. +* +* If @p_help is not NULL, store whether help is requested there. +* If @p_help is NULL and help is requested, fail. +* +* On success, return a dictionary of the parsed keys and values. +* On failure, store an error through @errp and return NULL. +*/ +QDict *keyval_parse (const char *params,const char *implied_key, +bool *p_help,Error **errp) +{ +QDict *qdict =qdict_new (); +QDict *ret =keyval_parse_into (qdict, params, implied_key, p_help, errp); + +if (!ret) { +qobject_unref (qdict); + } +return ret; +}
Signature · 1 line
2.26.2
PATCH 10/28 hmp: replace "O" parser with keyval
In reply to Paolo BonziniHMP is using QemuOpts to parse free-form commands device_add, netdev_add and object_add. However, none of these need QemuOpts for validation (these three QemuOptsLists are all of the catch-all kind), and keyval is already able to parse into QDict. So use keyval directly, avoiding the detour from
string to QemuOpts to QDict.
The args_type now stores the implied key. This arguably makes more sense than storing the QemuOptsList name; at least, it _is_ a key that might end up in the arguments QDict.
--- hmp-commands.hx | 6 +++--- monitor/hmp.c | 20 +++++++++----------- 2 files changed, 12 insertions(+), 14 deletions(-)
hmp-commands.hx +3 -3
@@ -676,7 +676,7 @@ ERST { .name = "device_add", - .args_type = "device:O", + .args_type = "driver:O", .params = "driver[,prop=value][,...]", .help = "add device, like -device on the command line", .cmd = hmp_device_add,
@@ -1322,7 +1322,7 @@ ERST { .name = "netdev_add", - .args_type = "netdev:O", + .args_type = "type:O", .params = "[user|tap|socket|vde|bridge|hubport|netmap|vhost-user],id=str[,prop=value][,...]", .help = "add host network device", .cmd = hmp_netdev_add,
@@ -1352,7 +1352,7 @@ ERST { .name = "object_add", - .args_type = "object:O", + .args_type = "qom-type:O", .params = "[qom-type=]type,id=str[,prop=value][,...]", .help = "create QOM object", .cmd = hmp_object_add,
monitor/hmp.c +9 -11
@@ -744,13 +744,9 @@ static QDict *monitor_parse_arguments(Monitor *mon,break ;case 'O' : { -QemuOptsList *opts_list; -QemuOpts *opts; +Error *errp; +bool help; - opts_list =qemu_find_opts (key); -if (!opts_list || opts_list->desc->name) { - goto bad_type; - }while (qemu_isspace (*p)) { p++; }
@@ -760,12 +756,14 @@ static QDict *monitor_parse_arguments(Monitor *mon,if (get_str (buf,sizeof (buf), &p) <0 ) { goto fail; } - opts =qemu_opts_parse_noisily (opts_list, buf, true); -if (!opts) { - goto fail; +keyval_parse_into (qdict, buf, key, &help, &errp); +if (help) { +if (qdict_haskey (qdict, key)) { +qdict_put_bool (qdict,"help" , true); + }else { +qdict_put_str (qdict, key,"help" ); + } } -qemu_opts_to_qdict (opts, qdict); -qemu_opts_del (opts); }break ;case '/' :
Signature · 1 line
2.26.2
PATCH 11/28 qom: use qemu_printf to print help for user-creatable objects
In reply to Paolo BonziniThis is needed when we add help support for object_add.
--- qom/object_interfaces.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
qom/object_interfaces.c +7 -6
@@ -13,6 +13,7 @@#include "qemu/option.h" #include "qapi/opts-visitor.h" #include "qemu/config-file.h" +#include "qemu/qemu-print.h" bool user_creatable_complete (UserCreatable *uc,Error **errp) {
@@ -214,15 +215,15 @@ char *object_property_help(const char *name, const char *type,return g_string_free (str, false); } -static void user_creatable_print_types (void ) +void user_creatable_print_types (void ) {GSList *l, *list; -printf ("List of user creatable objects:\n" ); +qemu_printf ("List of user creatable objects:\n" ); list =object_class_get_list_sorted (TYPE_USER_CREATABLE , false);for (l = list; l !=NULL ; l = l->next) {ObjectClass *oc =OBJECT_CLASS (l->data); -printf (" %s\n" ,object_class_get_name (oc)); +qemu_printf (" %s\n" ,object_class_get_name (oc)); }g_slist_free (list); }
@@ -253,12 +254,12 @@ static bool user_creatable_print_type_properites(const char *type) }g_ptr_array_sort (array, (GCompareFunc )qemu_pstrcmp0);if (array->len >0 ) { -printf ("%s options:\n" , type); +qemu_printf ("%s options:\n" , type); }else { -printf ("There are no options for %s.\n" , type); +qemu_printf ("There are no options for %s.\n" , type); }for (i =0 ; i < array->len; i++) { -printf ("%s\n" , (char *)array->pdata[i]); +qemu_printf ("%s\n" , (char *)array->pdata[i]); }g_ptr_array_set_free_func (array, g_free);g_ptr_array_free (array, true);
Signature · 1 line
2.26.2
PATCH 12/28 hmp: special case help options for object_add
In reply to Paolo BonziniFix "object_add help" and "object_add TYPE,help".
--- include/qom/object_interfaces.h | 9 ++++++++- monitor/hmp-cmds.c | 22 ++++++++-------------- qom/object_interfaces.c | 2 +- 3 files changed, 17 insertions(+), 16 deletions(-)
include/qom/object_interfaces.h +8 -1
@@ -149,6 +149,13 @@ typedef bool (*user_creatable_add_opts_predicate)(const char *type);int user_creatable_add_opts_foreach (void *opaque,QemuOpts *opts,Error **errp); +/** +* user_creatable_print_types: +* +* Prints a list of user-creatable objects to stdout or the monitor. +*/ +void user_creatable_print_types (void ); +/** * user_creatable_print_help: * @type: the QOM type to be added
@@ -174,7 +181,7 @@ bool user_creatable_print_help(const char *type, QemuOpts *opts);* no help was requested. It should only be called if we know that help is * requested and it will always print some help. */ -void user_creatable_print_help_from_qdict (QDict *args); +void user_creatable_print_help_from_qdict (const QDict *args);/** * user_creatable_del:
monitor/hmp-cmds.c +8 -14
@@ -1664,23 +1664,17 @@ void hmp_netdev_del(Monitor *mon, const QDict *qdict)void hmp_object_add (Monitor *mon,const QDict *qdict) {Error *err =NULL ; -QemuOpts *opts; -Object *obj =NULL ; - opts =qemu_opts_from_qdict (qemu_find_opts ("object" ), qdict, &err); -if (err) { - goto end; +if (is_help_option (qdict_get_str (qdict,"qom-type" ))) { +user_creatable_print_types (); +return ; } - - obj =user_creatable_add_opts (opts, &err); -qemu_opts_del (opts); - -end: -hmp_handle_error (mon, err); - -if (obj) { -object_unref (obj); +if (qdict_haskey (qdict,"help" )) { +user_creatable_print_help_from_qdict (qdict); +return ; } +user_creatable_add_dict ((QDict *)qdict, true, &err); +hmp_handle_error (mon, err); }void hmp_getfd (Monitor *mon,const QDict *qdict)
qom/object_interfaces.c +1 -1
@@ -280,7 +280,7 @@ bool user_creatable_print_help(const char *type, QemuOpts *opts)return false; } -void user_creatable_print_help_from_qdict (QDict *args) +void user_creatable_print_help_from_qdict (const QDict *args) {const char *type =qdict_get_try_str (args,"qom-type" );
Signature · 1 line
2.26.2
PATCH 13/28 remove -writeconfig
In reply to Paolo BonziniLike -set and -readconfig, it would not really be too hard to extend -writeconfig to parsing mechanisms other than QemuOpts. However, the uses of -writeconfig are substantially more limited, as it is generally easier to write the configuration by hand in the first place. In addition, -writeconfig does not even try to detect cases where it prints incorrect syntax (for example if values have a quote in them, since qemu_config_parse does not support any kind of escaping. Just remove it.
--- include/qemu/config-file.h | 1 - qemu-options.hx | 13 ++---------- softmmu/vl.c | 19 ----------------- util/qemu-config.c | 42 -------------------------------------- 4 files changed, 2 insertions(+), 73 deletions(-)
include/qemu/config-file.h +0 -1
@@ -10,7 +10,6 @@ void qemu_add_opts(QemuOptsList *list);void qemu_add_drive_opts (QemuOptsList *list);int qemu_global_option (const char *str); -void qemu_config_write (FILE *fp);int qemu_config_parse (FILE *fp,QemuOptsList **lists,const char *fname);int qemu_read_config_file (const char *filename);
qemu-options.hx +2 -11
@@ -4278,23 +4278,14 @@ SRST ERST DEF("readconfig", HAS_ARG, QEMU_OPTION_readconfig, - "-readconfig <file>\n", QEMU_ARCH_ALL) + "-readconfig <file>\n", + " read config file\n", QEMU_ARCH_ALL) SRST ``-readconfig file`` Read device configuration from file. This approach is useful when you want to spawn QEMU process with many command line options but you don't want to exceed the command line character limit. ERST -DEF("writeconfig", HAS_ARG, QEMU_OPTION_writeconfig, - "-writeconfig <file>\n" - " read/write config file\n", QEMU_ARCH_ALL) -SRST -``-writeconfig file`` - Write device configuration to file. The file can be either filename - to save command line and device configuration into file or dash - ``-``) character to print the output to stdout. This can be later - used as input file for ``-readconfig`` option. -ERST DEF("no-user-config", 0, QEMU_OPTION_nouserconfig, "-no-user-config\n"
softmmu/vl.c +0 -19
@@ -3309,25 +3309,6 @@ void qemu_init(int argc, char **argv, char **envp) } display_remote++;break ; -case QEMU_OPTION_writeconfig: - { -FILE *fp; -if (strcmp (optarg,"-" ) ==0 ) { - fp = stdout; - }else { - fp =fopen (optarg,"w" ); -if (fp ==NULL ) { -error_report ("open %s: %s" , optarg, -strerror (errno)); -exit (1 ); - } - } -qemu_config_write (fp); -if (fp != stdout) { -fclose (fp); - } -break ; - }case QEMU_OPTION_qtest: qtest_chrdev = optarg;break ;
util/qemu-config.c +0 -42
48 lines not on this page · raw message
Signature · 1 line
2.26.2
PATCH 14/28 qemu-config: add error propagation to qemu_config_parse
In reply to Paolo BonziniThis enables some simplification of vl.c via error_fatal.
--- block/blkdebug.c | 3 +-- include/qemu/config-file.h | 4 ++-- softmmu/vl.c | 30 ++++++++++++------------------ util/qemu-config.c | 20 ++++++++++---------- 4 files changed, 25 insertions(+), 32 deletions(-)
block/blkdebug.c +1 -2
@@ -279,9 +279,8 @@ static int read_config(BDRVBlkdebugState *s, const char *filename,return -errno; } - ret =qemu_config_parse (f, config_groups, filename); + ret =qemu_config_parse (f, config_groups, filename, errp);if (ret <0 ) { -error_setg (errp,"Could not parse blkdebug config file" ); goto fail; } }
include/qemu/config-file.h +2 -2
@@ -10,9 +10,9 @@ void qemu_add_opts(QemuOptsList *list);void qemu_add_drive_opts (QemuOptsList *list);int qemu_global_option (const char *str); -int qemu_config_parse (FILE *fp,QemuOptsList **lists,const char *fname); +int qemu_config_parse (FILE *fp,QemuOptsList **lists,const char *fname,Error **errp); -int qemu_read_config_file (const char *filename); +int qemu_read_config_file (const char *filename,Error **errp);/* Parse QDict options as a replacement for a config file (allowing multiple enumerated (0..(n-1)) configuration "sections") */
softmmu/vl.c +12 -18
@@ -2045,17 +2045,20 @@ static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)return 0 ; } -static int qemu_read_default_config_file (void ) +static void qemu_read_default_config_file (Error **errp) {int ret; +Error *local_err =NULL ; g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf"); - ret =qemu_read_config_file (file); -if (ret <0 && ret != -ENOENT ) { -return ret; + ret =qemu_read_config_file (file, &local_err); +if (ret <0 ) { +if (ret == -ENOENT ) { +error_free (local_err); + }else { +error_propagate (errp, local_err); + } } - -return 0 ; }static int qemu_set_option (const char *str)
@@ -2586,9 +2589,7 @@ void qemu_init(int argc, char **argv, char **envp) }if (userconfig) { -if (qemu_read_default_config_file () <0 ) { -exit (1 ); - } +qemu_read_default_config_file (&error_fatal); }/* second pass of option parsing */
@@ -3284,15 +3285,8 @@ void qemu_init(int argc, char **argv, char **envp)qemu_plugin_opt_parse (optarg, &plugin_list);break ;case QEMU_OPTION_readconfig: - { -int ret =qemu_read_config_file (optarg); -if (ret <0 ) { -error_report ("read config %s: %s" , optarg, -strerror (-ret)); -exit (1 ); - } -break ; - } +qemu_read_config_file (optarg, &error_fatal); +break ;case QEMU_OPTION_spice: olist =qemu_find_opts_err ("spice" ,NULL );if (!olist) {
util/qemu-config.c +10 -10
@@ -314,7 +314,7 @@ void qemu_add_opts(QemuOptsList *list) }/* Returns number of config groups on success, -errno on error */ -int qemu_config_parse (FILE *fp,QemuOptsList **lists,const char *fname) +int qemu_config_parse (FILE *fp,QemuOptsList **lists,const char *fname,Error **errp) {char line[1024 ], group[64 ], id[64 ], arg[64 ], value[1024 ];Location loc;
@@ -339,7 +339,7 @@ int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)/* group with id */ list =find_list (lists, group, &local_err);if (local_err) { -error_report_err (local_err); +error_propagate (errp, local_err); goto out; } opts =qemu_opts_create (list, id,1 ,NULL );
@@ -350,7 +350,7 @@ int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)/* group without id */ list =find_list (lists, group, &local_err);if (local_err) { -error_report_err (local_err); +error_propagate (errp, local_err); goto out; } opts =qemu_opts_create (list,NULL ,0 , &error_abort);
@@ -362,20 +362,19 @@ int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname) sscanf(line, " %63s = \"\"", arg) == 1) {/* arg = value */ if (opts ==NULL ) { -error_report ("no group defined" ); +error_setg (errp,"no group defined" ); goto out; } -if (!qemu_opt_set (opts, arg, value, &local_err)) { -error_report_err (local_err); +if (!qemu_opt_set (opts, arg, value, errp)) { goto out; }continue ; } -error_report ("parse error" ); +error_setg (errp,"parse error" ); goto out; }if (ferror (fp)) { -error_report ("error reading file" ); +error_setg (errp,"error reading file" ); goto out; } res = count;
@@ -384,16 +383,17 @@ out:return res; } -int qemu_read_config_file (const char *filename) +int qemu_read_config_file (const char *filename,Error **errp) {FILE *f =fopen (filename,"r" );int ret;if (f ==NULL ) { +error_setg_errno (errp, errno,"Cannot read config file %s" , filename);return -errno; } - ret =qemu_config_parse (f, vm_config_groups, filename); + ret =qemu_config_parse (f, vm_config_groups, filename, errp);fclose (f);return ret; }
Signature · 1 line
2.26.2
PATCH 15/28 qemu-option: support accept-any QemuOptsList in qemu_opts_absorb_qdict
In reply to Paolo Bonzini--- util/qemu-option.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
util/qemu-option.c +2 -1
@@ -1052,7 +1052,8 @@ bool qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)while (entry !=NULL ) { next =qdict_next (qdict, entry); -if (find_desc_by_name (opts->list->desc, entry->key)) { +if (opts_accepts_any (opts->list) || +find_desc_by_name (opts->list->desc, entry->key)) {if (!qemu_opts_from_qdict_entry (opts, entry, errp)) {return false; }
Signature · 1 line
2.26.2
PATCH 16/28 qemu-config: parse configuration files to a QDict
In reply to Paolo BonziniChange 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.
--- include/qemu/config-file.h | 6 ++- softmmu/vl.c | 4 +- util/qemu-config.c | 91 +++++++++++++++++++++++++------------- 3 files changed, 68 insertions(+), 33 deletions(-)
include/qemu/config-file.h +5 -1
@@ -1,6 +1,7 @@#ifndef QEMU_CONFIG_FILE_H #define QEMU_CONFIG_FILE_H +typedef void QEMUConfigCB (const char *group,QDict *qdict,void *opaque,Error **errp);QemuOptsList *qemu_find_opts (const char *group);QemuOptsList *qemu_find_opts_err (const char *group,Error **errp);
@@ -12,7 +13,10 @@ int qemu_global_option(const char *str);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
@@ -2051,7 +2051,7 @@ static void qemu_read_default_config_file(Error **errp)Error *local_err =NULL ; g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf"); - ret =qemu_read_config_file (file, &local_err); + ret =qemu_read_config_file (file, qemu_config_do_parse, &local_err);if (ret <0 ) {if (ret == -ENOENT ) {error_free (local_err);
@@ -3285,7 +3285,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 +61 -30
136 lines not on this page · raw message
Signature · 1 line
2.26.2
PATCH 17/28 vl: plumb keyval-based options into -set and -readconfig
In reply to Paolo BonziniAdd generic machinery to support parsing command line options with keyval in -set and -readconfig, choosing between QDict and QemuOpts as the underlying data structure.
The keyval_merge function is slightly heavyweight as a way to do qemu_set_option for QDict-based options, but it will be put to further use later to merge entire -readconfig sections together with their command-line equivalents.
--- include/block/qdict.h | 2 - include/qapi/qmp/qdict.h | 3 + include/qemu/option.h | 1 + softmmu/vl.c | 131 ++++++++++++++++++++++++++++++++------- tests/test-keyval.c | 32 ++++++++++ util/keyval.c | 36 +++++++++++ 6 files changed, 182 insertions(+), 23 deletions(-)
include/block/qdict.h +0 -2
@@ -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
@@ -66,4 +66,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 */
include/qemu/option.h +1 -0
@@ -151,5 +151,6 @@ QDict *keyval_parse_into(QDict *qdict, const char *params, const char *implied_k bool *p_help, Error **errp);QDict *keyval_parse (const char *params,const char *implied_key,bool *help,Error **errp); +void keyval_merge (QDict *old,const QDict *new,Error **errp);#endif
softmmu/vl.c +110 -21
181 lines not on this page · raw message
tests/test-keyval.c +32 -0
@@ -737,6 +737,36 @@ static void test_keyval_visit_any(void)visit_free (v); } +static void test_keyval_merge_success (void ) +{ +QDict *old =keyval_parse ("opt1=abc,opt2.sub1=def,opt2.sub2=ghi,opt3=xyz" , +NULL ,NULL , &error_abort); +QDict *new =keyval_parse ("opt1=ABC,opt2.sub2=GHI,opt2.sub3=JKL" , +NULL ,NULL , &error_abort); +QDict *combined =keyval_parse ("opt1=ABC,opt2.sub1=def,opt2.sub2=GHI,opt2.sub3=JKL,opt3=xyz" , +NULL ,NULL , &error_abort); +Error *err =NULL ; + +keyval_merge (old, new, &err); +g_assert (!err); +g_assert (qdict_is_equal (QOBJECT (combined),QOBJECT (old))); +} + +static void test_keyval_merge_conflict (void ) +{ +QDict *old =keyval_parse ("opt2.sub1=def,opt2.sub2=ghi" , +NULL ,NULL , &error_abort); +QDict *new =keyval_parse ("opt2=ABC" , +NULL ,NULL , &error_abort); +Error *err =NULL ; + +keyval_merge (new, old, &err); +error_free_or_abort (&err); + +keyval_merge (old, new, &err); +error_free_or_abort (&err); +} +int main (int argc,char *argv[]) {g_test_init (&argc, &argv,NULL );
@@ -750,6 +780,8 @@ int main(int argc, char *argv[])g_test_add_func ("/keyval/visit/optional" , test_keyval_visit_optional);g_test_add_func ("/keyval/visit/alternate" , test_keyval_visit_alternate);g_test_add_func ("/keyval/visit/any" , test_keyval_visit_any); +g_test_add_func ("/keyval/merge/success" , test_keyval_merge_success); +g_test_add_func ("/keyval/merge/conflict" , test_keyval_merge_conflict);g_test_run ();return 0 ; }
util/keyval.c +36 -0
@@ -315,6 +315,42 @@ static char *reassemble_key(GSList *key)return g_string_free (s, FALSE); } +/* Merge two dictionaries. */ +static void keyval_do_merge (QDict *old,const QDict *new,GString *str,Error **errp) +{ +size_t save_len = str->len; +const QDictEntry *ent; +QObject *old_value; + +for (ent =qdict_first (new); ent; ent =qdict_next (new, ent)) { + old_value =qdict_get (old, ent->key); +if (old_value &&qobject_type (old_value) !=qobject_type (ent->value)) { +error_setg (errp,"Parameter '%s%s' used inconsistently" , str->str, ent->key); +return ; + } +if (!old_value ||qobject_type (ent->value) !=QTYPE_QDICT ) { +qobject_ref (ent->value); +qdict_put_obj (old, ent->key, ent->value); +continue ; + } + +/* Merge sub-dictionaries. */ +g_string_append (str, ent->key); +g_string_append_c (str,'.' ); +keyval_do_merge (qobject_to (QDict, old_value), +qobject_to (QDict, ent->value), + str, errp); +g_string_truncate (str, save_len); + } +} + +void keyval_merge (QDict *old,const QDict *new,Error **errp) +{ +GString *str =g_string_new ("" ); +keyval_do_merge (old, new, str, errp); +g_string_free (str, TRUE); +} +/* * Listify @cur recursively. * Replace QDicts whose keys are all valid list indexes by QLists.
Signature · 1 line
2.26.2
PATCH 18/28 qom: do not modify QDict argument in user_creatable_add_dict
In reply to Paolo Bonzini-object will process its QDicts in two steps, first for the "early" objects and then for the "late" objects. If qom-type is removed by the "early" pass, the late pass fails. So just create a shallow copy of the QDict in user_creatable_add_dict.
--- include/qom/object_interfaces.h | 2 +- qom/object_interfaces.c | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-)
include/qom/object_interfaces.h +1 -1
@@ -102,7 +102,7 @@ Object *user_creatable_add_type(const char *type, const char *id,* * Returns: %true on success, %false on failure. */ -bool user_creatable_add_dict (QDict *qdict,bool keyval,Error **errp); +bool user_creatable_add_dict (const QDict *qdict,bool keyval,Error **errp);/** * user_creatable_add_opts:
qom/object_interfaces.c +7 -4
@@ -106,24 +106,25 @@ out:return obj; } -bool user_creatable_add_dict (QDict *qdict,bool keyval,Error **errp) +bool user_creatable_add_dict (const QDict *dict,bool keyval,Error **errp) {Visitor *v; -Object *obj; +Object *obj =NULL ; +QDict *qdict =qdict_clone_shallow (dict); g_autofree char *type = NULL; g_autofree char *id = NULL; type =g_strdup (qdict_get_try_str (qdict,"qom-type" ));if (!type) {error_setg (errp,QERR_MISSING_PARAMETER ,"qom-type" ); -return false; + goto out; }qdict_del (qdict,"qom-type" ); id =g_strdup (qdict_get_try_str (qdict,"id" ));if (!id) {error_setg (errp,QERR_MISSING_PARAMETER ,"id" ); -return false; + goto out; }qdict_del (qdict,"id" );
@@ -135,6 +136,8 @@ bool user_creatable_add_dict(QDict *qdict, bool keyval, Error **errp) obj =user_creatable_add_type (type, id, qdict, v, errp);visit_free (v);object_unref (obj); +out: +qobject_unref (qdict);return !!obj; }
Signature · 1 line
2.26.2
PATCH 19/28 qemu-io: use keyval for -object parsing
In reply to Paolo BonziniEnable creation of object with non-scalar properties.
--- qemu-io.c | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-)
qemu-io.c +13 -29
@@ -477,23 +477,6 @@ enum { OPTION_IMAGE_OPTS = 257, }; -static QemuOptsList qemu_object_opts = { - .name ="object" , - .implied_opt_name ="qom-type" , - .head =QTAILQ_HEAD_INITIALIZER (qemu_object_opts.head), - .desc = { - { } - }, -}; - -static bool qemu_io_object_print_help (const char *type,QemuOpts *opts) -{ -if (user_creatable_print_help (type, opts)) { -exit (0 ); - } -return true; -} -static QemuOptsList file_opts = { .name ="file" , .implied_opt_name ="file" ,
@@ -550,7 +533,6 @@ int main(int argc, char **argv)qcrypto_init (&error_fatal);module_call_init (MODULE_INIT_QOM ); -qemu_add_opts (&qemu_object_opts);qemu_add_opts (&qemu_trace_opts);bdrv_init ();
@@ -612,14 +594,20 @@ int main(int argc, char **argv)case 'U' : force_share = true;break ; -case OPTION_OBJECT : { -QemuOpts *qopts; - qopts =qemu_opts_parse_noisily (&qemu_object_opts, - optarg, true); -if (!qopts) { -exit (1 ); +case OPTION_OBJECT : + { +QDict *args; +bool help; + + args =keyval_parse (optarg,"qom-type" , &help, &error_fatal); +if (help) { +user_creatable_print_help_from_qdict (args); +exit (EXIT_SUCCESS ); + } +user_creatable_add_dict (args, true, &error_fatal); +qobject_unref (args); +break ; } - }break ;case OPTION_IMAGE_OPTS : imageOpts = true;break ;
@@ -644,10 +632,6 @@ int main(int argc, char **argv)exit (1 ); } -qemu_opts_foreach (&qemu_object_opts, - user_creatable_add_opts_foreach, - qemu_io_object_print_help, &error_fatal); -if (!trace_init_backends ()) {exit (1 ); }
Signature · 1 line
2.26.2
PATCH 20/28 qemu-nbd: use keyval for -object parsing
In reply to Paolo BonziniEnable creation of object with non-scalar properties.
--- qemu-nbd.c | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-)
qemu-nbd.c +13 -29
@@ -407,23 +407,6 @@ static QemuOptsList file_opts = { }, }; -static QemuOptsList qemu_object_opts = { - .name ="object" , - .implied_opt_name ="qom-type" , - .head =QTAILQ_HEAD_INITIALIZER (qemu_object_opts.head), - .desc = { - { } - }, -}; - -static bool qemu_nbd_object_print_help (const char *type,QemuOpts *opts) -{ -if (user_creatable_print_help (type, opts)) { -exit (0 ); - } -return true; -} -static QCryptoTLSCreds *nbd_get_tls_creds (const char *id,bool list,Error **errp)
@@ -599,7 +582,6 @@ int main(int argc, char **argv)qcrypto_init (&error_fatal);module_call_init (MODULE_INIT_QOM ); -qemu_add_opts (&qemu_object_opts);qemu_add_opts (&qemu_trace_opts);qemu_init_exec_dir (argv[0 ]);
@@ -752,14 +734,20 @@ int main(int argc, char **argv)case '?' :error_report ("Try `%s --help' for more information." , argv[0 ]);exit (EXIT_FAILURE ); -case QEMU_NBD_OPT_OBJECT : { -QemuOpts *opts; - opts =qemu_opts_parse_noisily (&qemu_object_opts, - optarg, true); -if (!opts) { -exit (EXIT_FAILURE ); +case QEMU_NBD_OPT_OBJECT : + { +QDict *args; +bool help; + + args =keyval_parse (optarg,"qom-type" , &help, &error_fatal); +if (help) { +user_creatable_print_help_from_qdict (args); +exit (EXIT_SUCCESS ); + } +user_creatable_add_dict (args, true, &error_fatal); +qobject_unref (args); +break ; } - }break ;case QEMU_NBD_OPT_TLSCREDS : tlscredsid = optarg;break ;
@@ -807,10 +795,6 @@ int main(int argc, char **argv) export_name ="" ; } -qemu_opts_foreach (&qemu_object_opts, - user_creatable_add_opts_foreach, - qemu_nbd_object_print_help, &error_fatal); -if (!trace_init_backends ()) {exit (1 ); }
Signature · 1 line
2.26.2
PATCH 21/28 qemu-img: use keyval for -object parsing
In reply to Paolo BonziniEnable creation of object with non-scalar properties.
--- qemu-img.c | 258 +++++++++++------------------------------------------ 1 file changed, 52 insertions(+), 206 deletions(-)
qemu-img.c +52 -206
@@ -227,21 +227,27 @@ static void QEMU_NORETURN help(void)exit (EXIT_SUCCESS ); } -static QemuOptsList qemu_object_opts = { - .name ="object" , - .implied_opt_name ="qom-type" , - .head =QTAILQ_HEAD_INITIALIZER (qemu_object_opts.head), - .desc = { - { } - }, -}; - -static bool qemu_img_object_print_help (const char *type,QemuOpts *opts) +static void qemu_img_object_parse (const char *optarg,int exit_code) { -if (user_creatable_print_help (type, opts)) { -exit (0 ); +QDict *args; +bool help; +Error *local_error =NULL ; + + args =keyval_parse (optarg,"qom-type" , &help, &local_error); +if (local_error) { +error_report_err (local_error); +exit (exit_code); } -return true; +if (help) { +user_creatable_print_help_from_qdict (args); +exit (EXIT_SUCCESS ); + } +user_creatable_add_dict (args, true, &local_error); +if (local_error) { +error_report_err (local_error); +exit (exit_code); + } +qobject_unref (args); }/*
@@ -567,14 +573,9 @@ static int img_create(int argc, char **argv)case 'u' : flags |=BDRV_O_NO_BACKING ;break ; -case OPTION_OBJECT : { -QemuOpts *opts; - opts =qemu_opts_parse_noisily (&qemu_object_opts, - optarg, true); -if (!opts) { - goto fail; - } - }break ; +case OPTION_OBJECT : +qemu_img_object_parse (optarg,1 ); +break ; } }
@@ -590,12 +591,6 @@ static int img_create(int argc, char **argv) } optind++; -if (qemu_opts_foreach (&qemu_object_opts, - user_creatable_add_opts_foreach, - qemu_img_object_print_help, &error_fatal)) { - goto fail; - } -/* Get image size, if specified */ if (optind < argc) {int64_t sval;
@@ -805,14 +800,9 @@ static int img_check(int argc, char **argv)case 'U' : force_share = true;break ; -case OPTION_OBJECT : { -QemuOpts *opts; - opts =qemu_opts_parse_noisily (&qemu_object_opts, - optarg, true); -if (!opts) { -return 1 ; - } - }break ; +case OPTION_OBJECT : +qemu_img_object_parse (optarg,1 ); +break ;case OPTION_IMAGE_OPTS : image_opts = true;break ;
@@ -832,12 +822,6 @@ static int img_check(int argc, char **argv)return 1 ; } -if (qemu_opts_foreach (&qemu_object_opts, - user_creatable_add_opts_foreach, - qemu_img_object_print_help, &error_fatal)) { -return 1 ; - } - ret =bdrv_parse_cache_mode (cache, &flags, &writethrough);if (ret <0 ) {error_report ("Invalid source cache option: %s" , cache);
@@ -1035,14 +1019,9 @@ static int img_commit(int argc, char **argv)return 1 ; }break ; -case OPTION_OBJECT : { -QemuOpts *opts; - opts =qemu_opts_parse_noisily (&qemu_object_opts, - optarg, true); -if (!opts) { -return 1 ; - } - }break ; +case OPTION_OBJECT : +qemu_img_object_parse (optarg,1 ); +break ;case OPTION_IMAGE_OPTS : image_opts = true;break ;
@@ -1059,12 +1038,6 @@ static int img_commit(int argc, char **argv) } filename = argv[optind++]; -if (qemu_opts_foreach (&qemu_object_opts, - user_creatable_add_opts_foreach, - qemu_img_object_print_help, &error_fatal)) { -return 1 ; - } - flags =BDRV_O_RDWR |BDRV_O_UNMAP ; ret =bdrv_parse_cache_mode (cache, &flags, &writethrough);if (ret <0 ) {
@@ -1424,15 +1397,9 @@ static int img_compare(int argc, char **argv)case 'U' : force_share = true;break ; -case OPTION_OBJECT : { -QemuOpts *opts; - opts =qemu_opts_parse_noisily (&qemu_object_opts, - optarg, true); -if (!opts) { - ret =2 ; - goto out4; - } - }break ; +case OPTION_OBJECT : +qemu_img_object_parse (optarg,2 ); +break ;case OPTION_IMAGE_OPTS : image_opts = true;break ;
@@ -1451,13 +1418,6 @@ static int img_compare(int argc, char **argv) filename1 = argv[optind++]; filename2 = argv[optind++]; -if (qemu_opts_foreach (&qemu_object_opts, - user_creatable_add_opts_foreach, - qemu_img_object_print_help, &error_fatal)) { - ret =2 ; - goto out4; - } -/* Initialize before goto out */ qemu_progress_init (progress,2.0 );
@@ -1642,7 +1602,6 @@ out2:blk_unref (blk1); out3:qemu_progress_end (); -out4:return ret; }
@@ -2344,15 +2303,9 @@ static int img_convert(int argc, char **argv) goto fail_getopt; }break ; -case OPTION_OBJECT : { -QemuOpts *object_opts; - object_opts =qemu_opts_parse_noisily (&qemu_object_opts, - optarg, true); -if (!object_opts) { - goto fail_getopt; - } +case OPTION_OBJECT : +qemu_img_object_parse (optarg,1 );break ; - }case OPTION_IMAGE_OPTS : image_opts = true;break ;
@@ -2380,12 +2333,6 @@ static int img_convert(int argc, char **argv) out_fmt ="raw" ; } -if (qemu_opts_foreach (&qemu_object_opts, - user_creatable_add_opts_foreach, - qemu_img_object_print_help, &error_fatal)) { - goto fail_getopt; - } -if (s.compressed && s.copy_range) {error_report ("Cannot enable copy offloading when -c is used" ); goto fail_getopt;
@@ -2977,14 +2924,9 @@ static int img_info(int argc, char **argv)case OPTION_BACKING_CHAIN : chain = true;break ; -case OPTION_OBJECT : { -QemuOpts *opts; - opts =qemu_opts_parse_noisily (&qemu_object_opts, - optarg, true); -if (!opts) { -return 1 ; - } - }break ; +case OPTION_OBJECT : +qemu_img_object_parse (optarg,1 ); +break ;case OPTION_IMAGE_OPTS : image_opts = true;break ;
@@ -3004,12 +2946,6 @@ static int img_info(int argc, char **argv)return 1 ; } -if (qemu_opts_foreach (&qemu_object_opts, - user_creatable_add_opts_foreach, - qemu_img_object_print_help, &error_fatal)) { -return 1 ; - } - list =collect_image_info_list (image_opts, filename, fmt, chain, force_share);if (!list) {
@@ -3219,14 +3155,9 @@ static int img_map(int argc, char **argv)return 1 ; }break ; -case OPTION_OBJECT : { -QemuOpts *opts; - opts =qemu_opts_parse_noisily (&qemu_object_opts, - optarg, true); -if (!opts) { -return 1 ; - } - }break ; +case OPTION_OBJECT : +qemu_img_object_parse (optarg,1 ); +break ;case OPTION_IMAGE_OPTS : image_opts = true;break ;
@@ -3246,12 +3177,6 @@ static int img_map(int argc, char **argv)return 1 ; } -if (qemu_opts_foreach (&qemu_object_opts, - user_creatable_add_opts_foreach, - qemu_img_object_print_help, &error_fatal)) { -return 1 ; - } - blk =img_open (image_opts, filename, fmt,0 , false, false, force_share);if (!blk) {return 1 ;
@@ -3390,14 +3315,9 @@ static int img_snapshot(int argc, char **argv)case 'U' : force_share = true;break ; -case OPTION_OBJECT : { -QemuOpts *opts; - opts =qemu_opts_parse_noisily (&qemu_object_opts, - optarg, true); -if (!opts) { -return 1 ; - } - }break ; +case OPTION_OBJECT : +qemu_img_object_parse (optarg,1 ); +break ;case OPTION_IMAGE_OPTS : image_opts = true;break ;
@@ -3409,12 +3329,6 @@ static int img_snapshot(int argc, char **argv) } filename = argv[optind++]; -if (qemu_opts_foreach (&qemu_object_opts, - user_creatable_add_opts_foreach, - qemu_img_object_print_help, &error_fatal)) { -return 1 ; - } -/* Open the image */ blk =img_open (image_opts, filename,NULL , bdrv_oflags, false, quiet, force_share);
@@ -3548,14 +3462,9 @@ static int img_rebase(int argc, char **argv)case 'q' : quiet = true;break ; -case OPTION_OBJECT : { -QemuOpts *opts; - opts =qemu_opts_parse_noisily (&qemu_object_opts, - optarg, true); -if (!opts) { -return 1 ; - } - }break ; +case OPTION_OBJECT : +qemu_img_object_parse (optarg,1 ); +break ;case OPTION_IMAGE_OPTS : image_opts = true;break ;
@@ -3577,12 +3486,6 @@ static int img_rebase(int argc, char **argv) } filename = argv[optind++]; -if (qemu_opts_foreach (&qemu_object_opts, - user_creatable_add_opts_foreach, - qemu_img_object_print_help, &error_fatal)) { -return 1 ; - } -qemu_progress_init (progress,2.0 );qemu_progress_print (0 ,100 );
@@ -3973,14 +3876,9 @@ static int img_resize(int argc, char **argv)case 'q' : quiet = true;break ; -case OPTION_OBJECT : { -QemuOpts *opts; - opts =qemu_opts_parse_noisily (&qemu_object_opts, - optarg, true); -if (!opts) { -return 1 ; - } - }break ; +case OPTION_OBJECT : +qemu_img_object_parse (optarg,1 ); +break ;case OPTION_IMAGE_OPTS : image_opts = true;break ;
@@ -4002,12 +3900,6 @@ static int img_resize(int argc, char **argv) } filename = argv[optind++]; -if (qemu_opts_foreach (&qemu_object_opts, - user_creatable_add_opts_foreach, - qemu_img_object_print_help, &error_fatal)) { -return 1 ; - } -/* Choose grow, shrink, or absolute resize mode */ switch (size[0 ]) {case '+' :
@@ -4187,12 +4079,7 @@ static int img_amend(int argc, char **argv) quiet = true;break ;case OPTION_OBJECT : - opts =qemu_opts_parse_noisily (&qemu_object_opts, - optarg, true); -if (!opts) { - ret =-1 ; - goto out_no_progress; - } +qemu_img_object_parse (optarg,1 );break ;case OPTION_IMAGE_OPTS : image_opts = true;
@@ -4207,13 +4094,6 @@ static int img_amend(int argc, char **argv)error_exit ("Must specify options (-o)" ); } -if (qemu_opts_foreach (&qemu_object_opts, - user_creatable_add_opts_foreach, - qemu_img_object_print_help, &error_fatal)) { - ret =-1 ; - goto out_no_progress; - } -if (quiet) { progress = false; }
@@ -4672,7 +4552,6 @@ static int img_bitmap(int argc, char **argv) {Error *err =NULL ;int c, ret =1 ; -QemuOpts *opts =NULL ;const char *fmt =NULL , *src_fmt =NULL , *src_filename =NULL ;const char *filename, *bitmap;BlockBackend *blk =NULL , *src =NULL ;
@@ -4766,10 +4645,7 @@ static int img_bitmap(int argc, char **argv) merge = true;break ;case OPTION_OBJECT : - opts =qemu_opts_parse_noisily (&qemu_object_opts, optarg, true); -if (!opts) { - goto out; - } +qemu_img_object_parse (optarg,1 );break ;case OPTION_IMAGE_OPTS : image_opts = true;
@@ -4777,12 +4653,6 @@ static int img_bitmap(int argc, char **argv) } } -if (qemu_opts_foreach (&qemu_object_opts, - user_creatable_add_opts_foreach, - qemu_img_object_print_help, &error_fatal)) { - goto out; - } -if (QSIMPLEQ_EMPTY (&actions)) {error_report ("Need at least one of --add, --remove, --clear, " "--enable, --disable, or --merge" );
@@ -4878,7 +4748,6 @@ static int img_bitmap(int argc, char **argv) out:blk_unref (src);blk_unref (blk); -qemu_opts_del (opts);return ret; }
@@ -5040,10 +4909,7 @@ static int img_dd(int argc, char **argv) force_share = true;break ;case OPTION_OBJECT : -if (!qemu_opts_parse_noisily (&qemu_object_opts, optarg, true)) { - ret =-1 ; - goto out; - } +qemu_img_object_parse (optarg,1 );break ;case OPTION_IMAGE_OPTS : image_opts = true;
@@ -5090,13 +4956,6 @@ static int img_dd(int argc, char **argv) goto out; } -if (qemu_opts_foreach (&qemu_object_opts, - user_creatable_add_opts_foreach, - qemu_img_object_print_help, &error_fatal)) { - ret =-1 ; - goto out; - } - blk1 =img_open (image_opts, in.filename, fmt,0 , false, false, force_share);
@@ -5272,7 +5131,6 @@ static int img_measure(int argc, char **argv)char *snapshot_name =NULL ;bool force_share = false;QemuOpts *opts =NULL ; -QemuOpts *object_opts =NULL ;QemuOpts *sn_opts =NULL ;QemuOptsList *create_opts =NULL ;bool image_opts = false;
@@ -5317,11 +5175,7 @@ static int img_measure(int argc, char **argv) force_share = true;break ;case OPTION_OBJECT : - object_opts =qemu_opts_parse_noisily (&qemu_object_opts, - optarg, true); -if (!object_opts) { - goto out; - } +qemu_img_object_parse (optarg,1 );break ;case OPTION_IMAGE_OPTS : image_opts = true;
@@ -5351,12 +5205,6 @@ static int img_measure(int argc, char **argv) } } -if (qemu_opts_foreach (&qemu_object_opts, - user_creatable_add_opts_foreach, - qemu_img_object_print_help, &error_fatal)) { - goto out; - } -if (argc - optind >1 ) {error_report ("At most one filename argument is allowed." ); goto out;
@@ -5444,7 +5292,6 @@ static int img_measure(int argc, char **argv) out:qapi_free_BlockMeasureInfo (info); -qemu_opts_del (object_opts);qemu_opts_del (opts);qemu_opts_del (sn_opts);qemu_opts_free (create_opts);
@@ -5496,7 +5343,6 @@ int main(int argc, char **argv)error_exit ("Not enough arguments" ); } -qemu_add_opts (&qemu_object_opts);qemu_add_opts (&qemu_source_opts);qemu_add_opts (&qemu_trace_opts);
Signature · 1 line
2.26.2
PATCH 22/28 qemu: use keyval for -object parsing
In reply to Paolo Bonzini--- include/qom/object_interfaces.h | 65 +--------------------------- qom/object_interfaces.c | 75 --------------------------------- softmmu/vl.c | 75 +++++++++++++++++++++------------ 3 files changed, 48 insertions(+), 167 deletions(-)
include/qom/object_interfaces.h +1 -64
@@ -104,51 +104,6 @@ Object *user_creatable_add_type(const char *type, const char *id,*/ bool user_creatable_add_dict (const QDict *qdict,bool keyval,Error **errp); -/** -* user_creatable_add_opts: -* @opts: the object definition -* @errp: if an error occurs, a pointer to an area to store the error -* -* Create an instance of the user creatable object whose type -* is defined in @opts by the 'qom-type' option, placing it -* in the object composition tree with name provided by the -* 'id' field. The remaining options in @opts are used to -* initialize the object properties. -* -* Returns: the newly created object or NULL on error -*/ -Object *user_creatable_add_opts (QemuOpts *opts,Error **errp); - - -/** -* user_creatable_add_opts_predicate: -* @type: the QOM type to be added -* -* A callback function to determine whether an object -* of type @type should be created. Instances of this -* callback should be passed to user_creatable_add_opts_foreach -*/ -typedef bool (*user_creatable_add_opts_predicate )(const char *type); - -/** -* user_creatable_add_opts_foreach: -* @opaque: a user_creatable_add_opts_predicate callback or NULL -* @opts: options to create -* @errp: unused -* -* An iterator callback to be used in conjunction with -* the qemu_opts_foreach() method for creating a list of -* objects from a set of QemuOpts -* -* The @opaque parameter can be passed a user_creatable_add_opts_predicate -* callback to filter which types of object are created during iteration. -* When it fails, report the error. -* -* Returns: 0 on success, -1 when an error was reported. -*/ -int user_creatable_add_opts_foreach (void *opaque, -QemuOpts *opts,Error **errp); -/** * user_creatable_print_types: *
@@ -156,30 +111,12 @@ int user_creatable_add_opts_foreach(void *opaque,*/ void user_creatable_print_types (void ); -/** -* user_creatable_print_help: -* @type: the QOM type to be added -* @opts: options to create -* -* Prints help if requested in @type or @opts. Note that if @type is neither -* "help"/"?" nor a valid user creatable type, no help will be printed -* regardless of @opts. -* -* Returns: true if a help option was found and help was printed, false -* otherwise. -*/ -bool user_creatable_print_help (const char *type,QemuOpts *opts); -/** * user_creatable_print_help_from_qdict: * @args: options to create * * Prints help considering the other options given in @args (if "qom-type" is -* given and valid, print properties for the type, otherwise print valid types) -* -* In contrast to user_creatable_print_help(), this function can't return that -* no help was requested. It should only be called if we know that help is -* requested and it will always print some help. +* given and valid, print properties for the type, otherwise print valid types). */ void user_creatable_print_help_from_qdict (const QDict *args);
qom/object_interfaces.c +0 -75
93 lines not on this page · raw message
softmmu/vl.c +47 -28
150 lines not on this page · raw message
Signature · 1 line
2.26.2
PATCH 23/28 storage-daemon: do not register the "object" group with QemuOpts
In reply to Paolo BonziniSince qsd uses keyval instead of -object, do not bother registering qemu_object_opts. It used to be necessary for object-del; that requirement is not there anymore since emulators have switched to keyval.
--- storage-daemon/qemu-storage-daemon.c | 10 ---------- 1 file changed, 10 deletions(-)
storage-daemon/qemu-storage-daemon.c +0 -10
@@ -130,15 +130,6 @@ enum {extern QemuOptsList qemu_chardev_opts; -static QemuOptsList qemu_object_opts = { - .name ="object" , - .implied_opt_name ="qom-type" , - .head =QTAILQ_HEAD_INITIALIZER (qemu_object_opts.head), - .desc = { - { } - }, -}; -static void init_qmp_commands (void ) {qmp_init_marshal (&qmp_commands);
@@ -295,7 +286,6 @@ int main(int argc, char *argv[])module_call_init (MODULE_INIT_QOM );module_call_init (MODULE_INIT_TRACE ); -qemu_add_opts (&qemu_object_opts);qemu_add_opts (&qemu_trace_opts);qcrypto_init (&error_fatal);bdrv_init ();
Signature · 1 line
2.26.2
PATCH 24/28 qom: export more functions for use with non-UserCreatable objects
In reply to Paolo BonziniMachines and accelerators are not user-creatable but they share similar parsing machinery. Export functions that will be used with -machine and -accel in softmmu/vl.c.
--- include/qom/object.h | 21 +++++++++++++++++ qom/object_interfaces.c | 51 +++++++++++++++++++++++++++++------------ 2 files changed, 57 insertions(+), 15 deletions(-)
include/qom/object.h +21 -0
@@ -860,6 +860,27 @@ static void do_qemu_init_ ## type_array(void) \ } \ type_init(do_qemu_init_ ## type_array) +/** +* type_print_class_properties: +* @type: a QOM class name +* +* Print the object's class properties to stdout or the monitor. +* Return whether an object was found. +*/ +bool type_print_class_properties (const char *type); + +/** +* object_set_properties_from_keyval: +* @obj: a QOM object +* @qdict: a dictionary whose leaf values are strings +* @errp: pointer to error object +* +* For each key in the dictionary, parse the value string and set the +* corresponding property in @obj. +*/ +void object_set_properties_from_keyval (Object *obj,const QDict *qdict, +Error **errp); +/** * object_class_dynamic_cast_assert: * @klass: The #ObjectClass to attempt to cast.
qom/object_interfaces.c +36 -15
81 lines not on this page · raw message
Signature · 1 line
2.26.2
PATCH 25/28 vl: rename local variable in configure_accelerators
In reply to Paolo BonziniSilly patch extracted from the next one, which is already big enough.
Because there are already local variables named "accel", we will name the global vl.c variable for "-M accel" accelerators instead. Rename it already in configure_accelerators to be ready.
--- softmmu/vl.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
softmmu/vl.c +9 -9
@@ -2251,17 +2251,17 @@ static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)static void configure_accelerators (const char *progname) { -const char *accel; +const char *accelerators;bool init_failed = false;qemu_opts_foreach (qemu_find_opts ("icount" ), do_configure_icount,NULL , &error_fatal); - accel =qemu_opt_get (qemu_get_machine_opts (),"accel" ); + accelerators =qemu_opt_get (qemu_get_machine_opts (),"accel" );if (QTAILQ_EMPTY (&qemu_accel_opts.head)) {char **accel_list, **tmp; -if (accel ==NULL ) { +if (accelerators ==NULL ) {/* Select the default accelerator */ bool have_tcg =accel_find ("tcg" );bool have_kvm =accel_find ("kvm" );
@@ -2269,21 +2269,21 @@ static void configure_accelerators(const char *progname)if (have_tcg && have_kvm) {if (g_str_has_suffix (progname,"kvm" )) {/* If the program name ends with "kvm", we prefer KVM */ - accel ="kvm:tcg" ; + accelerators ="kvm:tcg" ; }else { - accel ="tcg:kvm" ; + accelerators ="tcg:kvm" ; } }else if (have_kvm) { - accel ="kvm" ; + accelerators ="kvm" ; }else if (have_tcg) { - accel ="tcg" ; + accelerators ="tcg" ; }else {error_report ("No accelerator selected and" " no default accelerator available" );exit (1 ); } } - accel_list =g_strsplit (accel,":" ,0 ); + accel_list =g_strsplit (accelerators,":" ,0 );for (tmp = accel_list; *tmp; tmp++) {/*
@@ -2299,7 +2299,7 @@ static void configure_accelerators(const char *progname) }g_strfreev (accel_list); }else { -if (accel !=NULL ) { +if (accelerators !=NULL ) {error_report ("The -accel and \"-machine accel=\" options are incompatible" );exit (1 ); }
Signature · 1 line
2.26.2
PATCH 26/28 vl: switch -M parsing to keyval
In reply to Paolo BonziniSwitch from QemuOpts to keyval. This enables non-scalar machine properties.
--- softmmu/vl.c | 296 +++++++++++++++++++++++---------------------------- 1 file changed, 132 insertions(+), 164 deletions(-)
softmmu/vl.c +132 -164
@@ -134,6 +134,8 @@ static const char *cpu_option;static const char *mem_path;static const char *incoming;static const char *loadvm; +static const char *accelerators; +static QDict *machine_opts_dict;static GSList *object_opts_list =NULL ;static ram_addr_t maxram_size;static uint64_t ram_slots;
@@ -225,21 +227,6 @@ static QemuOptsList qemu_option_rom_opts = { }, }; -static QemuOptsList qemu_machine_opts = { - .name ="machine" , - .implied_opt_name ="type" , - .merge_lists = true, - .head =QTAILQ_HEAD_INITIALIZER (qemu_machine_opts.head), - .desc = { -/* -* no elements => accept any -* sanity checking will happen later -* when setting machine properties -*/ - { } - }, -}; -static QemuOptsList qemu_accel_opts = { .name ="accel" , .implied_opt_name ="accel" ,
@@ -469,16 +456,6 @@ static QemuOptsList qemu_fw_cfg_opts = { }, }; -/** -* Get machine options -* -* Returns: machine options (never null). -*/ -static QemuOpts *qemu_get_machine_opts (void ) -{ -return qemu_find_opts_singleton ("machine" ); -} -const char *qemu_get_vm_name (void ) {return qemu_name;
@@ -786,33 +763,6 @@ static MachineClass *find_default_machine(GSList *machines)return default_machineclass; } -static int machine_help_func (QemuOpts *opts,MachineState *machine) -{ -ObjectProperty *prop; -ObjectPropertyIterator iter; - -if (!qemu_opt_has_help_opt (opts)) { -return 0 ; - } - -object_property_iter_init (&iter,OBJECT (machine)); -while ((prop =object_property_iter_next (&iter))) { -if (!prop->set) { -continue ; - } - -printf ("%s.%s=%s" ,MACHINE_GET_CLASS (machine)->name, - prop->name, prop->type); -if (prop->description) { -printf (" (%s)\n" , prop->description); - }else { -printf ("\n" ); - } - } - -return 1 ; -} -static void version (void ) { printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
@@ -1503,33 +1453,28 @@ static gint machine_class_cmp(gconstpointer a, gconstpointer b) object_class_get_name(OBJECT_CLASS(mc1))); } -static MachineClass *machine_parse (const char *name,GSList *machines) +static void machine_help_func (const QDict *qdict) { -MachineClass *mc; -GSList *el; +GSList *machines, *el; +const char *type =qdict_get_try_str (qdict,"type" ); -if (is_help_option (name)) { -printf ("Supported machines are:\n" ); - machines =g_slist_sort (machines, machine_class_cmp); -for (el = machines; el; el = el->next) { -MachineClass *mc = el->data; -if (mc->alias) { -printf ("%-20s %s (alias of %s)\n" , mc->alias, mc->desc, mc->name); - } -printf ("%-20s %s%s%s\n" , mc->name, mc->desc, - mc->is_default ?" (default)" :"" , - mc->deprecation_reason ?" (deprecated)" :"" ); - } -exit (0 ); +if (type) { +type_print_class_properties (type); +return ; } - mc =find_machine (name, machines); -if (!mc) { -error_report ("unsupported machine type" ); -error_printf ("Use -machine help to list supported machines\n" ); -exit (1 ); +printf ("Supported machines are:\n" ); + machines =object_class_get_list (TYPE_MACHINE , false); + machines =g_slist_sort (machines, machine_class_cmp); +for (el = machines; el; el = el->next) { +MachineClass *mc = el->data; +if (mc->alias) { +printf ("%-20s %s (alias of %s)\n" , mc->alias, mc->desc, mc->name); + } +printf ("%-20s %s%s%s\n" , mc->name, mc->desc, + mc->is_default ?" (default)" :"" , + mc->deprecation_reason ?" (deprecated)" :"" ); } -return mc; }static const char *pid_file;
@@ -1582,32 +1527,31 @@ static const QEMUOption *lookup_opt(int argc, char **argv,return popt; } -static MachineClass *select_machine (void ) +static MachineClass *select_machine (QDict *qdict,Error **errp) { +const char *optarg =qdict_get_try_str (qdict,"type" );GSList *machines =object_class_get_list (TYPE_MACHINE , false); -MachineClass *machine_class =find_default_machine (machines); -const char *optarg; -QemuOpts *opts; -Location loc; - -loc_push_none (&loc); - - opts =qemu_get_machine_opts (); -qemu_opts_loc_restore (opts); +MachineClass *machine_class; +Error *local_err =NULL ; - optarg =qemu_opt_get (opts,"type" );if (optarg) { - machine_class =machine_parse (optarg, machines); - } - -if (!machine_class) { -error_report ("No machine specified, and there is no default" ); -error_printf ("Use -machine help to list supported machines\n" ); -exit (1 ); + machine_class =find_machine (optarg, machines); +qdict_del (qdict,"type" ); +if (!machine_class) { +error_setg (&local_err,"unsupported machine type" ); + } + }else { + machine_class =find_default_machine (machines); +if (!machine_class) { +error_setg (&local_err,"No machine specified, and there is no default" ); + } } -loc_pop (&loc);g_slist_free (machines); +if (local_err) { +error_append_hint (&local_err,"Use -machine help to list supported machines\n" ); +error_propagate (errp, local_err); + }return machine_class; }
@@ -1626,38 +1570,66 @@ static int object_parse_property_opt(Object *obj,return 0 ; } -static int machine_set_property (void *opaque, -const char *name,const char *value, -Error **errp) +/* *Non*recursively replace underscores with dashes in QDict keys. */ +static void keyval_dashify (QDict *qdict,Error **errp) { - g_autofree char *qom_name = g_strdup(name); +const QDictEntry *ent, *next;char *p; -for (p = qom_name; *p; p++) { -if (*p =='_' ) { - *p ='-' ; +for (ent =qdict_first (qdict); ent; ent = next) { + g_autofree char *new_key = NULL; + + next =qdict_next (qdict, ent); +if (!strchr (ent->key,'_' )) { +continue ; } + new_key =g_strdup (ent->key); +for (p = new_key; *p; p++) { +if (*p =='_' ) { + *p ='-' ; + } + } +if (qdict_haskey (qdict, new_key)) { +error_setg (errp,"Conflict between '%s' and '%s'" , ent->key, new_key); +return ; + } +qobject_ref (ent->value); +qdict_put_obj (qdict, new_key, ent->value); +qdict_del (qdict, ent->key); } +} + +static void qemu_apply_legacy_machine_options (QDict *qdict) +{ +const char *value; + +keyval_dashify (qdict, &error_fatal);/* Legacy options do not correspond to MachineState properties. */ -if (g_str_equal (qom_name,"accel" )) { -return 0 ; - } -if (g_str_equal (qom_name,"igd-passthru" )) { -object_register_sugar_prop (ACCEL_CLASS_NAME ("xen" ), qom_name, value); -return 0 ; + value =qdict_get_try_str (qdict,"accel" ); +if (value) { + accelerators =g_strdup (value); +qdict_del (qdict,"accel" ); } -if (g_str_equal (qom_name,"kvm-shadow-mem" )) { -object_register_sugar_prop (ACCEL_CLASS_NAME ("kvm" ), qom_name, value); -return 0 ; + + value =qdict_get_try_str (qdict,"igd-passthru" ); +if (value) { +object_register_sugar_prop (ACCEL_CLASS_NAME ("xen" ),"igd-passthru" , value); +qdict_del (qdict,"igd-passthru" ); } -if (g_str_equal (qom_name,"kernel-irqchip" )) { -object_register_sugar_prop (ACCEL_CLASS_NAME ("kvm" ), qom_name, value); -object_register_sugar_prop (ACCEL_CLASS_NAME ("whpx" ), qom_name, value); -return 0 ; + + value =qdict_get_try_str (qdict,"kvm-shadow-mem" ); +if (value) { +object_register_sugar_prop (ACCEL_CLASS_NAME ("kvm" ),"kvm-shadow-mem" , value); +qdict_del (qdict,"kvm-shadow-mem" ); } -return object_parse_property_opt (opaque, name, value,"type" , errp); + value =qdict_get_try_str (qdict,"kernel-irqchip" ); +if (value) { +object_register_sugar_prop (ACCEL_CLASS_NAME ("kvm" ),"kernel-irqchip" , value); +object_register_sugar_prop (ACCEL_CLASS_NAME ("whpx" ),"kernel-irqchip" , value); +qdict_del (qdict,"kernel-irqchip" ); + } }/*
@@ -1716,16 +1688,14 @@ static bool object_create_early(const char *type)return true; } -static void qemu_apply_machine_options (void ) +static void qemu_apply_machine_options (QDict *qdict) {MachineClass *machine_class =MACHINE_GET_CLASS (current_machine); -QemuOpts *machine_opts =qemu_get_machine_opts ();const char *boot_order =NULL ;const char *boot_once =NULL ;QemuOpts *opts; -qemu_opt_foreach (machine_opts, machine_set_property, current_machine, - &error_fatal); +object_set_properties_from_keyval (OBJECT (current_machine), qdict, &error_fatal); current_machine->ram_size = ram_size; current_machine->maxram_size = maxram_size; current_machine->ram_slots = ram_slots;
@@ -1754,10 +1724,8 @@ static void qemu_apply_machine_options(void) current_machine->boot_once = boot_once;if (semihosting_enabled () && !semihosting_get_argc ()) { -const char *kernel_filename =qemu_opt_get (machine_opts,"kernel" ); -const char *kernel_cmdline =qemu_opt_get (machine_opts,"append" ) ?:"" ;/* fall back to the -kernel/-append */ -semihosting_arg_fallback (kernel_filename, kernel_cmdline); +semihosting_arg_fallback (current_machine->kernel_filename, current_machine->kernel_cmdline); } }
@@ -1822,8 +1790,7 @@ static void qemu_create_early_backends(void)/* * Note: we need to create audio and block backends before -* machine_set_property(), so machine properties can refer to -* them. +* setting machine properties, so they can be referred to. */ configure_blockdev (&bdo_queue, machine_class, snapshot);audio_init_audiodevs ();
@@ -1993,16 +1960,14 @@ static void set_memory_options(MachineClass *mc)loc_pop (&loc); } -static void qemu_create_machine (MachineClass *machine_class) +static void qemu_create_machine (QDict *qdict) { +MachineClass *machine_class =select_machine (qdict, &error_fatal);object_set_machine_compat_props (machine_class->compat_props);set_memory_options (machine_class); current_machine =MACHINE (object_new_with_class (OBJECT_CLASS (machine_class))); -if (machine_help_func (qemu_get_machine_opts (), current_machine)) { -exit (0 ); - }object_property_add_child (object_get_root (),"machine" ,OBJECT (current_machine)); object_property_add_child(container_get(OBJECT(current_machine),
@@ -2033,8 +1998,11 @@ static void qemu_create_machine(MachineClass *machine_class)* specified either by the configuration file or by the command line. */ if (machine_class->default_machine_opts) { -qemu_opts_set_defaults (qemu_find_opts ("machine" ), - machine_class->default_machine_opts,0 ); +QDict *default_opts = +keyval_parse (machine_class->default_machine_opts,NULL ,NULL , + &error_abort); +object_set_properties_from_keyval (OBJECT (current_machine), default_opts, &error_abort); +qobject_unref (default_opts); } }
@@ -2056,7 +2024,8 @@ 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" )) { +if (g_str_equal (group,"object" ) || +g_str_equal (group,"machine" )) {return false; }return true;
@@ -2098,6 +2067,8 @@ static void qemu_record_config_group(const char *group, QDict *dict, Error **err p_head =qemu_config_list (group);if (p_head) { *p_head =g_slist_prepend (*p_head, dict); + }else if (g_str_equal (group,"machine" )) { +keyval_merge (machine_opts_dict, dict, errp); }else {abort (); }
@@ -2251,13 +2222,11 @@ static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)static void configure_accelerators (const char *progname) { -const char *accelerators;bool init_failed = false;qemu_opts_foreach (qemu_find_opts ("icount" ), do_configure_icount,NULL , &error_fatal); - accelerators =qemu_opt_get (qemu_get_machine_opts (),"accel" );if (QTAILQ_EMPTY (&qemu_accel_opts.head)) {char **accel_list, **tmp;
@@ -2345,12 +2314,11 @@ static void create_default_memdev(MachineState *ms, const char *path) &error_fatal); } -static void qemu_validate_options (void ) +static void qemu_validate_options (const QDict *machine_opts) { -QemuOpts *machine_opts =qemu_get_machine_opts (); -const char *kernel_filename =qemu_opt_get (machine_opts,"kernel" ); -const char *initrd_filename =qemu_opt_get (machine_opts,"initrd" ); -const char *kernel_cmdline =qemu_opt_get (machine_opts,"append" ); +const char *kernel_filename =qdict_get_try_str (machine_opts,"kernel" ); +const char *initrd_filename =qdict_get_try_str (machine_opts,"initrd" ); +const char *kernel_cmdline =qdict_get_try_str (machine_opts,"append" );if (kernel_filename ==NULL ) {if (kernel_cmdline !=NULL ) {
@@ -2647,7 +2615,6 @@ void qemu_init(int argc, char **argv, char **envp)qemu_add_opts (&qemu_trace_opts);qemu_plugin_add_opts ();qemu_add_opts (&qemu_option_rom_opts); -qemu_add_opts (&qemu_machine_opts);qemu_add_opts (&qemu_accel_opts);qemu_add_opts (&qemu_mem_opts);qemu_add_opts (&qemu_smp_opts);
@@ -2687,6 +2654,7 @@ void qemu_init(int argc, char **argv, char **envp) } } + machine_opts_dict =qdict_new ();if (userconfig) {qemu_read_default_config_file (&error_fatal); }
@@ -2776,8 +2744,7 @@ void qemu_init(int argc, char **argv, char **envp)parse_display (optarg);break ;case QEMU_OPTION_nographic: - olist =qemu_find_opts ("machine" ); -qemu_opts_parse_noisily (olist,"graphics=off" , false); +qdict_put_str (machine_opts_dict,"graphics" ,"off" ); nographic = true; dpy.type =DISPLAY_TYPE_NONE ;break ;
@@ -2801,16 +2768,16 @@ void qemu_init(int argc, char **argv, char **envp) }break ;case QEMU_OPTION_kernel: -qemu_opts_set (qemu_find_opts ("machine" ),"kernel" , optarg, &error_abort); +qdict_put_str (machine_opts_dict,"kernel" , optarg);break ;case QEMU_OPTION_initrd: -qemu_opts_set (qemu_find_opts ("machine" ),"initrd" , optarg, &error_abort); +qdict_put_str (machine_opts_dict,"initrd" , optarg);break ;case QEMU_OPTION_append: -qemu_opts_set (qemu_find_opts ("machine" ),"append" , optarg, &error_abort); +qdict_put_str (machine_opts_dict,"append" , optarg);break ;case QEMU_OPTION_dtb: -qemu_opts_set (qemu_find_opts ("machine" ),"dtb" , optarg, &error_abort); +qdict_put_str (machine_opts_dict,"dtb" , optarg);break ;case QEMU_OPTION_cdrom:drive_add (IF_DEFAULT ,2 , optarg,CDROM_OPTS );
@@ -2920,7 +2887,7 @@ void qemu_init(int argc, char **argv, char **envp) }break ;case QEMU_OPTION_bios: -qemu_opts_set (qemu_find_opts ("machine" ),"firmware" , optarg, &error_abort); +qdict_put_str (machine_opts_dict,"firmware" , optarg);break ;case QEMU_OPTION_singlestep: singlestep =1 ;
@@ -3183,17 +3150,20 @@ void qemu_init(int argc, char **argv, char **envp) preconfig_requested = true;break ;case QEMU_OPTION_enable_kvm: - olist =qemu_find_opts ("machine" ); -qemu_opts_parse_noisily (olist,"accel=kvm" , false); +qdict_put_str (machine_opts_dict,"accel" ,"kvm" );break ;case QEMU_OPTION_M :case QEMU_OPTION_machine: - olist =qemu_find_opts ("machine" ); - opts =qemu_opts_parse_noisily (olist, optarg, true); -if (!opts) { -exit (1 ); + { +bool help; + +keyval_parse_into (machine_opts_dict, optarg,"type" , &help, &error_fatal); +if (help) { +machine_help_func (machine_opts_dict); +exit (EXIT_SUCCESS ); + } +break ; } -break ;case QEMU_OPTION_accel: accel_opts =qemu_opts_parse_noisily (qemu_find_opts ("accel" ), optarg, true);
@@ -3220,14 +3190,12 @@ void qemu_init(int argc, char **argv, char **envp) }break ;case QEMU_OPTION_usb: - olist =qemu_find_opts ("machine" ); -qemu_opts_parse_noisily (olist,"usb=on" , false); +qdict_put_str (machine_opts_dict,"usb" ,"on" );break ;case QEMU_OPTION_usbdevice:error_report ("'-usbdevice' is deprecated, please use " "'-device usb-...' instead" ); - olist =qemu_find_opts ("machine" ); -qemu_opts_parse_noisily (olist,"usb=on" , false); +qdict_put_str (machine_opts_dict,"usb" ,"on" );add_device_config (DEV_USB , optarg);break ;case QEMU_OPTION_device:
@@ -3246,12 +3214,10 @@ void qemu_init(int argc, char **argv, char **envp)vnc_parse (optarg, &error_fatal);break ;case QEMU_OPTION_no_acpi: - olist =qemu_find_opts ("machine" ); -qemu_opts_parse_noisily (olist,"acpi=off" , false); +qdict_put_str (machine_opts_dict,"acpi" ,"off" );break ;case QEMU_OPTION_no_hpet: - olist =qemu_find_opts ("machine" ); -qemu_opts_parse_noisily (olist,"hpet=off" , false); +qdict_put_str (machine_opts_dict,"hpet" ,"off" );break ;case QEMU_OPTION_no_reboot: no_reboot =1 ;
@@ -3515,7 +3481,7 @@ void qemu_init(int argc, char **argv, char **envp)*/ loc_set_none (); -qemu_validate_options (); +qemu_validate_options (machine_opts_dict);qemu_process_sugar_options ();/*
@@ -3535,7 +3501,7 @@ void qemu_init(int argc, char **argv, char **envp)configure_rtc (qemu_find_opts_singleton ("rtc" )); -qemu_create_machine (select_machine ()); +qemu_create_machine (machine_opts_dict);suspend_mux_open ();
@@ -3543,12 +3509,14 @@ void qemu_init(int argc, char **argv, char **envp)qemu_create_default_devices ();qemu_create_early_backends (); -qemu_apply_machine_options (); +qemu_apply_legacy_machine_options (machine_opts_dict); +qemu_apply_machine_options (machine_opts_dict); +qobject_unref (machine_opts_dict);phase_advance (PHASE_MACHINE_CREATED );/* * Note: uses machine properties such as kernel-irqchip, must run -* after machine_set_property(). +* after qemu_apply_machine_options. */ configure_accelerators (argv[0 ]);phase_advance (PHASE_ACCEL_CREATED );
Signature · 1 line
2.26.2
PATCH 27/28 qemu-option: remove now-dead code
In reply to Paolo Bonzini-M was the sole user of qemu_opts_set and qemu_opts_set_defaults, remove them and the arguments that they used.
--- include/qemu/option.h | 3 --- tests/test-qemu-opts.c | 35 ----------------------------- util/qemu-option.c | 51 +++++++++--------------------------------- 3 files changed, 10 insertions(+), 79 deletions(-)
include/qemu/option.h +0 -3
@@ -119,7 +119,6 @@ QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists, Error **errp);void qemu_opts_reset (QemuOptsList *list);void qemu_opts_loc_restore (QemuOpts *opts); -bool qemu_opts_set (QemuOptsList *list,const char *name,const char *value,Error **errp);const char *qemu_opts_id (QemuOpts *opts);void qemu_opts_set_id (QemuOpts *opts,char *id);void qemu_opts_del (QemuOpts *opts);
@@ -130,8 +129,6 @@ QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params, bool permit_abbrev);QemuOpts *qemu_opts_parse (QemuOptsList *list,const char *params,bool permit_abbrev,Error **errp); -void qemu_opts_set_defaults (QemuOptsList *list,const char *params, -int permit_abbrev);QemuOpts *qemu_opts_from_qdict (QemuOptsList *list,const QDict *qdict,Error **errp); QDict *qemu_opts_to_qdict_filtered(QemuOpts *opts, QDict *qdict,
tests/test-qemu-opts.c +0 -35
@@ -410,40 +410,6 @@ static void test_qemu_opts_reset(void)g_assert (opts ==NULL ); } -static void test_qemu_opts_set (void ) -{ -QemuOptsList *list; -QemuOpts *opts; -const char *opt; - - list =qemu_find_opts ("opts_list_04" ); -g_assert (list !=NULL ); -g_assert (QTAILQ_EMPTY (&list->head)); - g_assert_cmpstr(list->name, ==, "opts_list_04"); - -/* should not find anything at this point */ - opts =qemu_opts_find (list,NULL ); -g_assert (opts ==NULL ); - -/* implicitly create opts and set str3 value */ -qemu_opts_set (list,"str3" ,"value" , &error_abort); -g_assert (!QTAILQ_EMPTY (&list->head)); - -/* get the just created opts */ - opts =qemu_opts_find (list,NULL ); -g_assert (opts !=NULL ); - -/* check the str3 value */ - opt =qemu_opt_get (opts,"str3" ); - g_assert_cmpstr(opt, ==, "value"); - -qemu_opts_del (opts); - -/* should not find anything at this point */ - opts =qemu_opts_find (list,NULL ); -g_assert (opts ==NULL ); -} -static int opts_count_iter (void *opaque,const char *name,const char *value,Error **errp) {
@@ -1030,7 +996,6 @@ int main(int argc, char *argv[])g_test_add_func ("/qemu-opts/opt_get_size" , test_qemu_opt_get_size);g_test_add_func ("/qemu-opts/opt_unset" , test_qemu_opt_unset);g_test_add_func ("/qemu-opts/opts_reset" , test_qemu_opts_reset); -g_test_add_func ("/qemu-opts/opts_set" , test_qemu_opts_set);g_test_add_func ("/qemu-opts/opts_parse/general" , test_opts_parse);g_test_add_func ("/qemu-opts/opts_parse/bool" , test_opts_parse_bool);g_test_add_func ("/qemu-opts/opts_parse/number" , test_opts_parse_number);
util/qemu-option.c +10 -41
126 lines not on this page · raw message
Signature · 1 line
2.26.2
PATCH 28/28 vl: switch -accel parsing to keyval
In reply to Paolo BonziniSwitch from QemuOpts to keyval. This enables compound options for accelerators.
--- accel/accel.c | 6 ++ include/sysemu/accel.h | 1 + softmmu/vl.c | 134 ++++++++++++++++++----------------------- 3 files changed, 67 insertions(+), 74 deletions(-)
accel/accel.c +6 -0
@@ -46,6 +46,12 @@ AccelClass *accel_find(const char *opt_name)return ac; } +bool accel_print_class_properties (const char *opt_name) +{ + g_autofree char *class_name = g_strdup_printf(ACCEL_CLASS_NAME("%s"), opt_name); +return type_print_class_properties (class_name); +} +int accel_init_machine (AccelState *accel,MachineState *ms) {AccelClass *acc =ACCEL_GET_CLASS (accel);
include/sysemu/accel.h +1 -0
@@ -67,6 +67,7 @@ typedef struct AccelClass { OBJECT_GET_CLASS(AccelClass, (obj), TYPE_ACCEL)AccelClass *accel_find (const char *opt_name); +bool accel_print_class_properties (const char *opt_name);int accel_init_machine (AccelState *accel,MachineState *ms);/* Called just before os_setup_post (ie just before drop OS privs) */
softmmu/vl.c +60 -74
238 lines not on this page · raw message
Signature · 1 line
2.26.2
Quoted 80 lines · Paolo Bonzini
This series switches -object, -M and -accel from QemuOpts to keyval. Monitor commands
device_addandnetdev_addare also switched to keyval, though -device and -netdev for now are not.Along the way, the syntax of keyval and QemuOpts becomes more consistent and support for keyval-based options is added to -readconfig. -writeconfig instead is removed (see patch 13 for rationale).
The reason to do this is:
- to make qemu-io, qemu-nbd, qemu-img and QEMU's parsing of -object consistent with qemu-storage-daemon's
- to allow using compound properties in -object, -M and -accel
Patch 1-5: make QemuOpts parsing a bit more restrictive, warning for short-form boolean options and removing weird ways to request help such as "help=foo" or "no?".
Patch 6-12: let keyval accept escaped commas in implied options, switch comma-separated syntax for HMP from QemuOpts to keyval, add help support to
object_addPatch 13-18: plumbing for reading keyval-based options in
vl.c, including -set and -readconfig.Patch 19-23: switch -object to keyval everywhere
Patch 24-28: switch -M and -accel to keyval
Paolo Bonzini (28):
qemu-option: simplify search for end of key
qemu-option: pass QemuOptsList toopts_accepts_any
qemu-option: clean up id vs.list->merge_lists
qemu-option: move help handling toget_opt_name_value
qemu-option: warn for short-form boolean options
keyval: accept escaped commas in implied option
keyval: simplifykeyval_parse_one
tests: convert check-qom-proplist to keyval
keyval: introducekeyval_parse_into
hmp: replace "O" parser with keyval
qom: useqemu_printfto print help for user-creatable objects
hmp: special case help options forobject_add
remove -writeconfig
qemu-config: add error propagation toqemu_config_parse
qemu-option: support accept-any QemuOptsList inqemu_opts_absorb_qdict
qemu-config: parse configuration files to a QDict
vl: plumb keyval-based options into -set and -readconfig
qom: do not modify QDict argument inuser_creatable_add_dict
qemu-io: use keyval for -object parsing
qemu-nbd: use keyval for -object parsing
qemu-img: use keyval for -object parsing
qemu: use keyval for -object parsing
storage-daemon: do not register the "object" group with QemuOpts
qom: export more functions for use with non-UserCreatable objects
vl: rename local variable inconfigure_accelerators
vl: switch -M parsing to keyval
qemu-option: remove now-dead code
vl: switch -accel parsing to keyvalaccel/accel.c | 6 + block/blkdebug.c | 3 +- docs/system/deprecated.rst | 6 + hmp-commands.hx | 6 +- include/block/qdict.h | 2 - include/qapi/qmp/qdict.h | 3 + include/qemu/config-file.h | 9 +- include/qemu/help_option.h | 11 - include/qemu/option.h | 6 +- include/qom/object.h | 21 + include/qom/object_interfaces.h | 68 +-- include/sysemu/accel.h | 1 + monitor/hmp-cmds.c | 22 +- monitor/hmp.c | 20 +- qemu-img.c | 258 ++-------- qemu-io.c | 42 +- qemu-nbd.c | 42 +- qemu-options.hx | 13 +- qom/object_interfaces.c | 152 ++---- softmmu/vl.c | 687 ++++++++++++++-------------
Quoted 9 lines
storage-daemon/qemu-storage-daemon.c | 10 - tests/check-qom-proplist.c | 58 ++- tests/test-keyval.c | 53 ++- tests/test-qemu-opts.c | 37 +- util/keyval.c | 230 +++++---- util/qemu-config.c | 141 +++--- util/qemu-option.c | 184 ++++--- 27 files changed, 946 insertions(+), 1145 deletions(-)
Ping? It's been over a month (even if with the Christmas vacation).
Patches 1-2 were already reviewed so I have included them already. I would like at least the next 14 patches to go in as soon as possible. (The rest can be routed through maintainer trees or I can post them together with the softmmu/vl.c cleanups).
Paolo
Am 17.01.2021 um 17:48 hat Paolo Bonzini geschrieben:
Quoted 9 lines · Paolo Bonzini
Quoted 7 lines
This series switches -object, -M and -accel from QemuOpts to keyval. Monitor commands
device_addandnetdev_addare also switched to keyval, though -device and -netdev for now are not.Along the way, the syntax of keyval and QemuOpts becomes more consistent and support for keyval-based options is added to -readconfig. -writeconfig instead is removed (see patch 13 for rationale).
Quoted 6 lines
Ping? It's been over a month (even if with the Christmas vacation).
Patches 1-2 were already reviewed so I have included them already. I would like at least the next 14 patches to go in as soon as possible. (The rest can be routed through maintainer trees or I can post them together with the
softmmu/vl.ccleanups).
On which commit is this based? It doesn't seem to apply to master (maybe no surprise), but also not to 5.2.0-rc4, which was the state of master when this was posted.
Kevin
Quoted 12 lines · Kevin Wolf
Am 17.01.2021 um 17:48 hat Paolo Bonzini geschrieben:
Quoted 11 lines · Paolo Bonzini
Quoted 7 lines
This series switches -object, -M and -accel from QemuOpts to keyval. Monitor commands
device_addandnetdev_addare also switched to keyval, though -device and -netdev for now are not.Along the way, the syntax of keyval and QemuOpts becomes more consistent and support for keyval-based options is added to -readconfig. -writeconfig instead is removed (see patch 13 for rationale).
Ping? It's been over a month (even if with the Christmas vacation).
Quoted 8 lines
Quoted 4 lines
Patches 1-2 were already reviewed so I have included them already. I would like at least the next 14 patches to go in as soon as possible. (The rest can be routed through maintainer trees or I can post them together with the
softmmu/vl.ccleanups).On which commit is this based? It doesn't seem to apply to master (maybe no surprise), but also not to 5.2.0-rc4, which was the state of master when this was posted.
I'll retest and repost.
Paolo