From: MkfsSion <mkfssion@mkfssion.com>
To: armbru@redhat.com
Cc: Paolo Bonzini <pbonzini@redhat.com>,
MkfsSion <mkfssion@mkfssion.com>,
"open list:All patches CC here" <qemu-devel@nongnu.org>
Subject: [PATCH v2] vl: Add support to set properties when using JSON syntax for -device via -set option
Date: Wed, 29 Dec 2021 14:44:21 +0800 [thread overview]
Message-ID: <20211224072511.63894-1-mkfssion@mkfssion.com> (raw)
Message-ID: <20211229064421.5LPUBTk_b7lwFSu6jdh7beB7kZHoVtGGztQSJR1SClI@z> (raw)
When using JSON syntax for -device, -set option can not find device
specified in JSON by id field. The following commandline is an example:
$ qemu-system-x86_64 -device '{"id":"foo"}' -set device.foo.bar=1
qemu-system-x86_64: -set device.foo.bar=1: there is no device "foo" defined
The patch fixes the above issue by trying to convert value provided by -set
option to the type that the setting property actually takes.
Signed-off-by: YuanYang Meng <mkfssion@mkfssion.com>
---
v2:
1.Set device option when group is 'device' only
2.Store value in type that properties actually take
softmmu/vl.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 62 insertions(+)
diff --git a/softmmu/vl.c b/softmmu/vl.c
index 620a1f1367..c213e9e022 100644
--- a/softmmu/vl.c
+++ b/softmmu/vl.c
@@ -30,7 +30,9 @@
#include "hw/qdev-properties.h"
#include "qapi/compat-policy.h"
#include "qapi/error.h"
+#include "qapi/qmp/qbool.h"
#include "qapi/qmp/qdict.h"
+#include "qapi/qmp/qnum.h"
#include "qapi/qmp/qstring.h"
#include "qapi/qmp/qjson.h"
#include "qemu-version.h"
@@ -2274,6 +2276,61 @@ static void qemu_read_default_config_file(Error **errp)
}
}
+static bool qemu_set_device_option_property(const char *id, const char *key,
+ const char *value, Error **errp) {
+ DeviceOption *opt;
+ QTAILQ_FOREACH(opt, &device_opts, next) {
+ const char *device_id = qdict_get_try_str(opt->opts, "id");
+ if (device_id && (strcmp(device_id, id) == 0)) {
+ QObject *obj = NULL;
+ if ((strcmp(key, "id") == 0) ||
+ (strcmp(key, "bus") == 0) ||
+ (strcmp(key, "driver") == 0)) {
+ obj = QOBJECT(qstring_from_str(value));
+ } else {
+ const char *driver = qdict_get_try_str(opt->opts, "driver");
+ if (driver) {
+ ObjectClass *klass = object_class_by_name(driver);
+ ObjectProperty *prop = object_class_property_find(klass, key);
+ if (prop) {
+ if (strcmp(prop->type, "str") == 0) {
+ obj = QOBJECT(qstring_from_str(value));
+ } else if (strcmp(prop->type, "bool") == 0) {
+ bool boolean;
+ if (qapi_bool_parse(key, value, &boolean, errp)) {
+ obj = QOBJECT(qbool_from_bool(boolean));
+ }
+ } else if (strncmp(prop->type, "uint", 4) == 0) {
+ uint64_t num;
+ if (parse_option_size(key, value, &num, errp)) {
+ obj = QOBJECT(qnum_from_uint(num));
+ }
+ } else {
+ error_setg(errp,
+ "Setting property %s on device %s with "
+ "type %s is unsupported via -set option",
+ key, id, prop->type);
+ }
+ } else {
+ error_setg(errp, "Unable to find property %s on device %s",
+ key, id);
+ }
+ } else {
+ error_setg(errp, "Unable to get driver for device %s", id);
+ }
+ }
+ if (obj) {
+ qdict_del(opt->opts, key);
+ qdict_put_obj(opt->opts, key, obj);
+ return true;
+ } else {
+ return false;
+ }
+ }
+ }
+ return false;
+}
+
static void qemu_set_option(const char *str, Error **errp)
{
char group[64], id[64], arg[64];
@@ -2294,6 +2351,11 @@ static void qemu_set_option(const char *str, Error **errp)
if (list) {
opts = qemu_opts_find(list, id);
if (!opts) {
+ if (strcmp(group, "device") == 0) {
+ if (qemu_set_device_option_property(id, arg,
+ str + offset + 1, errp))
+ return;
+ }
error_setg(errp, "there is no %s \"%s\" defined", group, id);
return;
}
--
2.34.1
next reply other threads:[~2021-12-29 6:53 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-24 7:25 MkfsSion [this message]
2021-12-29 6:44 ` [PATCH v2] vl: Add support to set properties when using JSON syntax for -device via -set option MkfsSion
2022-01-03 12:35 ` MkfsSion
2022-01-12 7:49 ` MkfsSion
2022-01-19 14:08 ` Markus Armbruster
2022-01-21 12:59 ` MkfsSion
2022-01-21 14:38 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211224072511.63894-1-mkfssion@mkfssion.com \
--to=mkfssion@mkfssion.com \
--cc=armbru@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).