Skip to contentSkip to conversation

qapi: remove the 'middle' mode

PATCH Applied Marc-André Lureau 7 participants 33 messages Sep 12, 2016 – Sep 21, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

v6 cover qapi: remove the 'middle' mode

Hi,

Although some QMP commands are still not fully qapi'fied, it is possible to use more qapi common and generated code by dropping the 'middle' mode and use qmp_dispatch().

v6:
- get back to v4 approach, and unregister commands at run-time. (Markus Armbruster)
- comments and commit messages improvements. (Markus Armbruster)
- remove #ifdef/per-target qapi build. (Markus Armbruster)

v5:
- replace the dynamic unregister commands approach by a few preliminary patches to make the json schema #ifdef conditional and per-target
- fix "check invalid arguments" patch based on Markus version
- patch reordering

v4:
- export all marshaller functions (so we can keep calling them after middle mode is removed), remove 'export-marshal' patch
- get rid of unnecessary lambda in python code (leftover), remove second mcgen(), and outdated comment
- remove disabled commands at run-time to avoid any regression. It's now on my TODO list to fix qapi generator in 2.8 to have conditionals
- move qmp-commands.txt to doc/
- split the last patch, remove trailing ws
- add QEMU_VERSION_{MAJOR,MINOR,MICRO} patch, simplifying qmp_query_version() (could be applied outside this series)
- update commit title/messages/order

v3:
- add a reference to docs/qmp-spec.txt in qmp_capabilities doc
- remove 'props' from device_add doc, improve example
- replace a g_strcmp0 with more appropriate g_str_equal
- add 'export-marshal' command generator key patch
- call qmp_marshal_query_version() directly (also get rid of the need to do a make clean, since the qapi json is modified)
- add patch to check invalid arguments on no-args (the old dispatch code checks that), and a test
- patch reordering to fix intermediate builds
- commit messages improvements
- split some misc doc fixes in last patch
- add some r-b and rebase

v2:
- rebased on master
- add Since: 0.13 to qmp_capabilities and device_add documentation
- fix device_add doc
- add missing spaces after ',' in get_qmp_greeting()
- fix some grammar in monitor.c while touching it

Marc-André Lureau (17):
build-sys: define QEMU_VERSION_{MAJOR, MINOR, MICRO}
qapi-schema: use generated marshaller for 'qmp_capabilities'
qapi-schema: add 'device_add'
monitor: simplify invalid_qmp_mode()
monitor: register gen:false commands manually
qmp: Hack to keep commands configuration-specific
qapi: export the marshallers
monitor: use qmp_find_command() (using generated qapi code)
monitor: implement 'qmp_query_commands' without qmp_cmds
monitor: remove mhandler.cmd_new
qapi: remove the "middle" mode
qapi: check invalid arguments on no-args commands
tests: add a test to check invalid args
monitor: use qmp_dispatch()
build-sys: remove qmp-commands-old.h qmp-commands.hx: fix some styling
Replace qmp-commands.hx by docs/qmp-commands.txt

Markus Armbruster (1):
qapi: Support unregistering QMP commands

 monitor.c                                |  424 +++-------
 qapi/qmp-registry.c                      |    8 +
 qmp.c                                    |   16 +-
 tests/test-qga.c                         |   21 +
 tests/test-qmp-commands.c                |   15 +
 vl.c                                     |    1 +
 Makefile                                 |    8 +-
 Makefile.target                          |    7 +-
 scripts/create_config                    |    6 +
 scripts/qapi-commands.py                 |   81 +-
 .gitignore                               |    1 -
 MAINTAINERS                              |    1 -
 docs/qapi-code-gen.txt                   |    6 +-
 qmp-commands.hx => docs/qmp-commands.txt | 1297 +-----------------------------
 docs/writing-qmp-commands.txt            |   46 +-
 hmp-commands-info.hx                     |  118 +--
 hmp-commands.hx                          |  208 ++---
 include/qapi/qmp/dispatch.h              |    1 +
 qapi-schema.json                         |   61 ++
 trace-events                             |    1 -
 20 files changed, 458 insertions(+), 1869 deletions(-)
 rename qmp-commands.hx => docs/qmp-commands.txt (81%)
Signature · 1 line
2.10.0
#1 lore raw

PATCH v6 1/18 build-sys: define QEMU_VERSION_{MAJOR, MINOR, MICRO}

There are better chances to find what went wrong at build time than a later assert in qmp_query_version

---
 qmp.c                 | 16 +++-------------
 scripts/create_config |  6 ++++++
 2 files changed, 9 insertions(+), 13 deletions(-)
qmp.c +3 -13
index dea8f81..6733463 100644
@@ -51,21 +51,11 @@ NameInfo *qmp_query_name(Error **errp)
 VersionInfo *qmp_query_version(Error **errp)
 {
     VersionInfo *info = g_new0(VersionInfo, 1);
-    const char *version = QEMU_VERSION;
-    const char *tmp;
-    int err;
 
     info->qemu = g_new0(VersionTriple, 1);
-    err = qemu_strtoll(version, &tmp, 10, &info->qemu->major);
-    assert(err == 0);
-    tmp++;
-
-    err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->minor);
-    assert(err == 0);
-    tmp++;
-
-    err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->micro);
-    assert(err == 0);
+    info->qemu->major = QEMU_VERSION_MAJOR;
+    info->qemu->minor = QEMU_VERSION_MINOR;
+    info->qemu->micro = QEMU_VERSION_MICRO;
     info->package = g_strdup(QEMU_PKGVERSION);
 
     return info;
scripts/create_config +6 -0
index 1dd6a35..e6929dd 100755
@@ -7,7 +7,13 @@ while read line; do
 case $line in
  VERSION=*) # configuration
     version=${line#*=}
+    major=$(echo "$version" | cut -d. -f1)
+    minor=$(echo "$version" | cut -d. -f2)
+    micro=$(echo "$version" | cut -d. -f3)
     echo "#define QEMU_VERSION \"$version\""
+    echo "#define QEMU_VERSION_MAJOR $major"
+    echo "#define QEMU_VERSION_MINOR $minor"
+    echo "#define QEMU_VERSION_MICRO $micro"
     ;;
  qemu_*dir=*) # qemu-specific directory configuration
     name=${line%=*}
Signature · 1 line
2.10.0
Reviewed-by on v6 1/18
#2 lore raw

Re: [Qemu-devel] [PATCH v6 01/18] build-sys: define QEMU_VERSION_{MAJOR, MINOR, MICRO}

Quoted 10 lines · Marc-André Lureau

There are better chances to find what went wrong at build time than a later assert in qmp_query_version

---
 qmp.c                 | 16 +++-------------
 scripts/create_config |  6 ++++++
 2 files changed, 9 insertions(+), 13 deletions(-)

qmp.c NameInfo *qmp_query_name(Error **errp) · line 61

Quoted 9 lines
qmp.c +3 -3
-
-    err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->micro);
-    assert(err == 0);
+    info->qemu->major = QEMU_VERSION_MAJOR;
+    info->qemu->minor = QEMU_VERSION_MINOR;
+    info->qemu->micro = QEMU_VERSION_MICRO;
     info->package = g_strdup(QEMU_PKGVERSION);
 
     return info;

The old code silently ignores any garbage after the third integer (it populates &tmp, but never checks the value of tmp).

Quoted 5 lines
scripts/create_config +0 -0
index 1dd6a35..e6929dd 100755

scripts/create_config while read line; do · line 16

Quoted 10 lines
scripts/create_config +6 -0
@@ -7,7 +7,13 @@ while read line; do
 case $line in
  VERSION=*) # configuration
     version=${line#*=}
+    major=$(echo "$version" | cut -d. -f1)
+    minor=$(echo "$version" | cut -d. -f2)
+    micro=$(echo "$version" | cut -d. -f3)
     echo "#define QEMU_VERSION \"$version\""
+    echo "#define QEMU_VERSION_MAJOR $major"
+    echo "#define QEMU_VERSION_MINOR $minor"
+    echo "#define QEMU_VERSION_MICRO $micro"

The new code likewise ignores any fourth field.

Do we care either way? Unless someone else has a reason for why we should care, I'm fine with:

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH v6 01/18] build-sys: define QEMU_VERSION_{MAJOR, MINOR, MICRO}

Quoted 34 lines · Eric Blake
Quoted 33 lines · Marc-André Lureau

There are better chances to find what went wrong at build time than a later assert in qmp_query_version

---
 qmp.c                 | 16 +++-------------
 scripts/create_config |  6 ++++++
 2 files changed, 9 insertions(+), 13 deletions(-)
qmp.c +3 -9
index dea8f81..6733463 100644
@@ -51,21 +51,11 @@ NameInfo *qmp_query_name(Error **errp)
 VersionInfo *qmp_query_version(Error **errp)
 {
     VersionInfo *info = g_new0(VersionInfo, 1);
-    const char *version = QEMU_VERSION;
-    const char *tmp;
-    int err;
 
     info->qemu = g_new0(VersionTriple, 1);
-    err = qemu_strtoll(version, &tmp, 10, &info->qemu->major);
-    assert(err == 0);
-    tmp++;
-
-    err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->micro);
-    assert(err == 0);
+    info->qemu->major = QEMU_VERSION_MAJOR;
+    info->qemu->minor = QEMU_VERSION_MINOR;
+    info->qemu->micro = QEMU_VERSION_MICRO;
     info->package = g_strdup(QEMU_PKGVERSION);
 
Quoted 4 lines

qmp.c NameInfo *qmp_query_name(Error **errp) · line 61

Quoted 1 line
qmp.c +0 -0
     return info;

The old code silently ignores any garbage after the third integer (it populates &tmp, but never checks the value of tmp).

It also accepts any separator character between the integers, not just '.'.

Quoted 9 lines
Quoted 9 lines
scripts/create_config +1 -0
index 1dd6a35..e6929dd 100755
@@ -7,7 +7,13 @@ while read line; do
 case $line in
  VERSION=*) # configuration
     version=${line#*=}
+    major=$(echo "$version" | cut -d. -f1)
Quoted 8 lines

scripts/create_config while read line; do · line 16

Quoted 6 lines
scripts/create_config +5 -0
+    minor=$(echo "$version" | cut -d. -f2)
+    micro=$(echo "$version" | cut -d. -f3)
     echo "#define QEMU_VERSION \"$version\""
+    echo "#define QEMU_VERSION_MAJOR $major"
+    echo "#define QEMU_VERSION_MINOR $minor"
+    echo "#define QEMU_VERSION_MICRO $micro"

The new code likewise ignores any fourth field.

Nope:

    $ echo "2.7.50garbage" | cut -d. -f3
    50garbage

The compiler will choke on

    info->qemu->micro = QEMU_VERSION_MICRO;

because it's

    info->qemu->micro = 50garbage;

after preprocessing.

The commit message could mention that VERSION is now parsed more strictly (configure checks '.', the compiler checks integers), but I guess it's not worth the bother now.

Quoted 3 lines

Do we care either way? Unless someone else has a reason for why we should care, I'm fine with:

Does your R-by stand?

#4 lore raw

Re: [Qemu-devel] [PATCH v6 01/18] build-sys: define QEMU_VERSION_{MAJOR, MINOR, MICRO}

Quoted 7 lines · Markus Armbruster
Quoted 7 lines
Quoted 5 lines
     info->qemu = g_new0(VersionTriple, 1);
qmp.c +0 -4
-    err = qemu_strtoll(version, &tmp, 10, &info->qemu->major);
-    assert(err == 0);
-    tmp++;
-
Quoted 5 lines
Quoted 2 lines

The old code silently ignores any garbage after the third integer (it populates &tmp, but never checks the value of tmp).

It also accepts any separator character between the integers, not just '.'.

True, and didn't we have a release of qemu a few years ago (perhaps it was a downstream Red Hat release; I can't quickly find it now...) where we accidentally released the version string with a comma instead of a dot?

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
     version=${line#*=}
scripts/create_config +6 -0
+    major=$(echo "$version" | cut -d. -f1)
+    minor=$(echo "$version" | cut -d. -f2)
+    micro=$(echo "$version" | cut -d. -f3)
     echo "#define QEMU_VERSION \"$version\""
+    echo "#define QEMU_VERSION_MAJOR $major"
+    echo "#define QEMU_VERSION_MINOR $minor"
+    echo "#define QEMU_VERSION_MICRO $micro"
Quoted 6 lines
Quoted 1 line

The new code likewise ignores any fourth field.

Nope:

    $ echo "2.7.50garbage" | cut -d. -f3
    50garbage

That's not a fourth field. I was thinking '2.7.50.0' ignoring '.0'.

Quoted 7 lines

The compiler will choke on

    info->qemu->micro = QEMU_VERSION_MICRO;

because it's

Quoted 7 lines
    info->qemu->micro = 50garbage;

after preprocessing.

The commit message could mention that VERSION is now parsed more strictly (configure checks '.', the compiler checks integers), but I guess it's not worth the bother now.

Indeed, the fact that we now separate on exactly '.' instead of the end of an integer is different.

Quoted 6 lines
Quoted 4 lines

Do we care either way? Unless someone else has a reason for why we should care, I'm fine with:

Does your R-by stand?

Yes - while the parsing may have changed, it doesn't affect the common case of a correctly-formed VERSION file. I'm okay if you want to tweak the commit message to call out a summary of this conversation, but since the patch is already on qapi-next staging, I'm also okay if it doesn't change.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#5 lore raw

PATCH v6 2/18 qapi-schema: use generated marshaller for 'qmp_capabilities'

In reply to Marc-André Lureau

qapi'fy the 'qmp_capabilities' command, makes the command visible in query-qmp-schema.

---
 monitor.c        |  4 ++--
 qapi-schema.json | 21 +++++++++++++++++++++
 qmp-commands.hx  |  2 +-
 3 files changed, 24 insertions(+), 3 deletions(-)
monitor.c +2 -2
index 5c00373..991eaf7 100644
@@ -617,7 +617,7 @@ static void monitor_qapi_event_init(void)
     qmp_event_set_func_emit(monitor_qapi_event_queue);
 }
 
-static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
+void qmp_qmp_capabilities(Error **errp)
 {
     cur_mon->qmp.in_command_mode = true;
 }
@@ -3656,7 +3656,7 @@ static int monitor_can_read(void *opaque)
 static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
                              Error **errp)
 {
-    bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
+    bool is_cap = cmd->mhandler.cmd_new == qmp_marshal_qmp_capabilities;
 
     if (is_cap && mon->qmp.in_command_mode) {
         error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
qapi-schema.json +21 -0
index c4f3674..5e15d28 100644
@@ -20,6 +20,27 @@
 # QAPI introspection
 { 'include': 'qapi/introspect.json' }
 
+##
+# @qmp_capabilities:
+#
+# Enable QMP capabilities.
+#
+# Arguments: None.
+#
+# Example:
+#
+# -> { "execute": "qmp_capabilities" }
+# <- { "return": {} }
+#
+# Notes: This command is valid exactly when first connecting: it must be
+# issued before any other command will be accepted, and will fail once the
+# monitor is accepting other commands. (see qemu docs/qmp-spec.txt)
+#
+# Since: 0.13
+#
+##
+{ 'command': 'qmp_capabilities' }
+
 ##
 # @LostTickPolicy:
 #
qmp-commands.hx +1 -1
index e6c9193..8a13550 100644
@@ -2219,7 +2219,7 @@ EQMP
         .args_type  = "",
         .params     = "",
         .help       = "enable QMP capabilities",
-        .mhandler.cmd_new = qmp_capabilities,
+        .mhandler.cmd_new = qmp_marshal_qmp_capabilities,
     },
 
 SQMP
Signature · 1 line
2.10.0
#6 lore raw

PATCH v6 3/18 qapi-schema: add 'device_add'

In reply to Marc-André Lureau

Even though device_add is not fully qapi'fied, we may add it to the json schema with 'gen': false, so registration and documentation can be generated.

---
 qapi-schema.json | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
qapi-schema.json +40 -0
index 5e15d28..55f825d 100644
@@ -2200,6 +2200,46 @@
 ##
 { 'command': 'xen-set-global-dirty-log', 'data': { 'enable': 'bool' } }
 
+##
+# @device_add:
+#
+# @driver: the name of the new device's driver
+#
+# @bus: #optional the device's parent bus (device tree path)
+#
+# @id: the device's ID, must be unique
+#
+# Additional arguments depend on the type.
+#
+# Add a device.
+#
+# Notes:
+# 1. For detailed information about this command, please refer to the
+#    'docs/qdev-device-use.txt' file.
+#
+# 2. It's possible to list device properties by running QEMU with the
+#    "-device DEVICE,help" command-line argument, where DEVICE is the
+#    device's name
+#
+# Example:
+#
+# -> { "execute": "device_add",
+#      "arguments": { "driver": "e1000", "id": "net1",
+#                     "bus": "pci.0",
+#                     "mac": "52:54:00:12:34:56" } }
+# <- { "return": {} }
+#
+# TODO This command effectively bypasses QAPI completely due to its
+# "additional arguments" business.  It shouldn't have been added to
+# the schema in this form.  It should be qapified properly, or
+# replaced by a properly qapified command.
+#
+# Since: 0.13
+##
+{ 'command': 'device_add',
+  'data': {'driver': 'str', 'id': 'str'},
+  'gen': false } # so we can get the additional arguments
+
 ##
 # @device_del:
 #
Signature · 1 line
2.10.0
#7 lore raw

PATCH v6 4/18 monitor: simplify invalid_qmp_mode()

In reply to Marc-André Lureau

handle_qmp_command() will switch to use qmp_dispatch(). It won't have a pointer to the marshaller function anymore, but only the name of the command to invoke. Simplify invalid_qmp_mode() so it can just be called with the command name.

---
 monitor.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
monitor.c +5 -5
index 991eaf7..e156026 100644
@@ -3653,21 +3653,21 @@ static int monitor_can_read(void *opaque)
     return (mon->suspend_cnt == 0) ? 1 : 0;
 }
 
-static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
+static bool invalid_qmp_mode(const Monitor *mon, const char *cmd,
                              Error **errp)
 {
-    bool is_cap = cmd->mhandler.cmd_new == qmp_marshal_qmp_capabilities;
+    bool is_cap = g_str_equal(cmd, "qmp_capabilities");
 
     if (is_cap && mon->qmp.in_command_mode) {
         error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
                   "Capabilities negotiation is already complete, command "
-                  "'%s' ignored", cmd->name);
+                  "'%s' ignored", cmd);
         return true;
     }
     if (!is_cap && !mon->qmp.in_command_mode) {
         error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
                   "Expecting capabilities negotiation with "
-                  "'qmp_capabilities' before command '%s'", cmd->name);
+                  "'qmp_capabilities' before command '%s'", cmd);
         return true;
     }
     return false;
@@ -3958,7 +3958,7 @@ static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
                   "The command %s has not been found", cmd_name);
         goto err_out;
     }
-    if (invalid_qmp_mode(mon, cmd, &local_err)) {
+    if (invalid_qmp_mode(mon, cmd_name, &local_err)) {
         goto err_out;
     }
 
Signature · 1 line
2.10.0
#8 lore raw

PATCH v6 5/18 monitor: register gen:false commands manually

In reply to Marc-André Lureau

Since a few commands are using 'gen': false, they are not registered automatically by the generator. Register manually instead.

This is in preparation for removal of qapi 'middle' mode generation.

Note that qmp_init_marshal() function isn't run yet, so the commands aren't actually registered, until module_call_init(MODULE_INIT_QAPI) is added in a later patch.

---
 monitor.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
monitor.c +13 -0
index e156026..b7ae552 100644
@@ -79,6 +79,7 @@
 #include "sysemu/block-backend.h"
 #include "sysemu/qtest.h"
 #include "qemu/cutils.h"
+#include "qapi/qmp/dispatch.h"
 
 /* for hmp_info_irq/pic */
 #if defined(TARGET_SPARC)
@@ -1007,6 +1008,18 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
     *ret_data = qobject_from_json(qmp_schema_json);
 }
 
+static void qmp_init_marshal(void)
+{
+    qmp_register_command("query-qmp-schema", qmp_query_qmp_schema,
+                         QCO_NO_OPTIONS);
+    qmp_register_command("device_add", qmp_device_add,
+                         QCO_NO_OPTIONS);
+    qmp_register_command("netdev_add", qmp_netdev_add,
+                         QCO_NO_OPTIONS);
+}
+
+qapi_init(qmp_init_marshal);
+
 /* set the current CPU defined by the user */
 int monitor_set_cpu(int cpu_index)
 {
Signature · 1 line
2.10.0
#9 lore raw

PATCH v6 6/18 qapi: Support unregistering QMP commands

In reply to Marc-André Lureau

From: Markus Armbruster <[email protected]>

---
 qapi/qmp-registry.c         | 8 ++++++++
 include/qapi/qmp/dispatch.h | 1 +
 2 files changed, 9 insertions(+)
qapi/qmp-registry.c +8 -0
index 68b24c9..e805368 100644
@@ -30,6 +30,14 @@ void qmp_register_command(const char *name, QmpCommandFunc *fn,
     QTAILQ_INSERT_TAIL(&qmp_commands, cmd, node);
 }
 
+void qmp_unregister_command(const char *name)
+{
+    QmpCommand *cmd = qmp_find_command(name);
+
+    QTAILQ_REMOVE(&qmp_commands, cmd, node);
+    g_free(cmd);
+}
+
 QmpCommand *qmp_find_command(const char *name)
 {
     QmpCommand *cmd;
include/qapi/qmp/dispatch.h +1 -0
index 48c11b6..57651ea 100644
@@ -36,6 +36,7 @@ typedef struct QmpCommand
 
 void qmp_register_command(const char *name, QmpCommandFunc *fn,
                           QmpCommandOptions options);
+void qmp_unregister_command(const char *name);
 QmpCommand *qmp_find_command(const char *name);
 QObject *qmp_dispatch(QObject *request);
 void qmp_disable_command(const char *name);
Signature · 1 line
2.10.0
#10 lore raw

PATCH v6 7/18 qmp: Hack to keep commands configuration-specific

In reply to Marc-André Lureau

We currently define QMP commands in two places: the QAPI schema and qmp-commands.hx. The latter is preprocessed, the former is not. We use the preprocessor to suppress configuration-specific commands. For instance, query-spice is only available #ifdef CONFIG_SPICE.

QMP command dispatch and query-commands use the qmp-commands.hx definition, and thus obey the #ifdeffery there. Good, because it lets QMP clients probe for available features more easily.

query-qmp-schema uses the QAPI schema, and thus lists the configuration-specific commands even when they're unavailable. Not so good.

We're about to flip command dispatch and query-commands to the non-middle-mode command registry, which uses the QAPI schema, so we can ditch qmp-commands.hx. To avoid regressing query-commands, arrange for commands that are suppressed with the preprocessor now to be unregistered with that registry. This will keep them unavailable and out of query-commands when we flip command dispatch and query-commands to that registry, exactly as before.

This is a hack. The proper solution is to support configuration-specific commands in the QAPI schema. Mark it FIXME.

---
 monitor.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
monitor.c +35 -0
index b7ae552..18d37f7 100644
@@ -1008,6 +1008,38 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
     *ret_data = qobject_from_json(qmp_schema_json);
 }
 
+/*
+ * Note: right now, this function is never called.  It will be called
+ * shortly when we stop using QAPI 'middle mode'.  The rest of this
+ * comment is written as if that was the case already.
+ *
+ * We used to define commands in qmp-commands.hx in addition to the
+ * QAPI schema.  This permitted defining some of them only in certain
+ * configurations.  query-commands has always reflected that (good,
+ * because it lets QMP clients figure out what's actually available),
+ * while query-qmp-schema never did (not so good).  This function is a
+ * hack to keep the configuration-specific commands defined exactly as
+ * before, even though qmp-commands.hx is gone.
+ *
+ * FIXME Educate the QAPI schema on configuration-specific commands,
+ * and drop this hack.
+ */
+static void qmp_unregister_commands_hack(void)
+{
+#ifndef CONFIG_SPICE
+    qmp_unregister_command("query-spice");
+#endif
+#ifndef TARGET_I386
+    qmp_unregister_command("rtc-reset-reinjection");
+#endif
+#ifndef TARGET_S390X
+    qmp_unregister_command("dump-skeys");
+#endif
+#ifndef TARGET_ARM
+    qmp_unregister_command("query-gic-capabilities");
+#endif
+}
+
 static void qmp_init_marshal(void)
 {
     qmp_register_command("query-qmp-schema", qmp_query_qmp_schema,
@@ -1016,6 +1048,9 @@ static void qmp_init_marshal(void)
                          QCO_NO_OPTIONS);
     qmp_register_command("netdev_add", qmp_netdev_add,
                          QCO_NO_OPTIONS);
+
+    /* call it after the rest of qapi_init() */
+    register_module_init(qmp_unregister_commands_hack, MODULE_INIT_QAPI);
 }
 
 qapi_init(qmp_init_marshal);
Signature · 1 line
2.10.0
#11 lore raw

PATCH v6 8/18 qapi: export the marshallers

In reply to Marc-André Lureau

Make it possible to call marshallers manually, without going through qmp_dispatch(). (this is currently only possible in middle-mode, but it's also useful in general)

---
 scripts/qapi-commands.py | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
scripts/qapi-commands.py +2 -6
index a06a2c4..b150db9 100644
@@ -84,10 +84,7 @@ static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out,
 
 
 def gen_marshal_proto(name):
-    ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
-    if not middle_mode:
-        ret = 'static ' + ret
-    return ret
+    return 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
 
 
 def gen_marshal_decl(name):
@@ -222,8 +219,7 @@ class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
         if ret_type and ret_type not in self._visited_ret_types:
             self._visited_ret_types.add(ret_type)
             self.defn += gen_marshal_output(ret_type)
-        if middle_mode:
-            self.decl += gen_marshal_decl(name)
+        self.decl += gen_marshal_decl(name)
         self.defn += gen_marshal(name, arg_type, boxed, ret_type)
         if not middle_mode:
             self._regy += gen_register_command(name, success_response)
Signature · 1 line
2.10.0
#12 lore raw

PATCH v6 9/18 monitor: use qmp_find_command() (using generated qapi code)

In reply to Marc-André Lureau

Stop using the so-called 'middle' mode. Instead, use qmp_find_command() from generated qapi commands registry. Update and fix the documentation too.

---
 monitor.c                     |  14 ++--
 vl.c                          |   1 +
 Makefile                      |   2 +-
 docs/writing-qmp-commands.txt |   8 +--
 qmp-commands.hx               | 146 ------------------------------------------
 5 files changed, 11 insertions(+), 160 deletions(-)
monitor.c +7 -7
index 18d37f7..6e0ae81 100644
@@ -1009,10 +1009,6 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
 }
 
 /*
- * Note: right now, this function is never called.  It will be called
- * shortly when we stop using QAPI 'middle mode'.  The rest of this
- * comment is written as if that was the case already.
- *
  * We used to define commands in qmp-commands.hx in addition to the
  * QAPI schema.  This permitted defining some of them only in certain
  * configurations.  query-commands has always reflected that (good,
@@ -3976,6 +3972,7 @@ static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
     QObject *obj, *data;
     QDict *input, *args;
     const mon_cmd_t *cmd;
+    QmpCommand *qcmd;
     const char *cmd_name;
     Monitor *mon = cur_mon;
 
@@ -4001,7 +3998,8 @@ static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
     cmd_name = qdict_get_str(input, "execute");
     trace_handle_qmp_command(mon, cmd_name);
     cmd = qmp_find_cmd(cmd_name);
-    if (!cmd) {
+    qcmd = qmp_find_command(cmd_name);
+    if (!qcmd || !cmd) {
         error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
                   "The command %s has not been found", cmd_name);
         goto err_out;
@@ -4023,7 +4021,7 @@ static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
         goto err_out;
     }
 
-    cmd->mhandler.cmd_new(args, &data, &local_err);
+    qcmd->fn(args, &data, &local_err);
 
 err_out:
     monitor_protocol_emitter(mon, data, local_err);
@@ -4095,7 +4093,9 @@ static QObject *get_qmp_greeting(void)
     QObject *ver = NULL;
 
     qmp_marshal_query_version(NULL, &ver, NULL);
-    return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
+
+    return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
+                              ver);
 }
 
 static void monitor_qmp_event(void *opaque, int event)
vl.c +1 -0
index ee557a1..a256a47 100644
@@ -2986,6 +2986,7 @@ int main(int argc, char **argv, char **envp)
     qemu_init_exec_dir(argv[0]);
 
     module_call_init(MODULE_INIT_QOM);
+    module_call_init(MODULE_INIT_QAPI);
 
     qemu_add_opts(&qemu_drive_opts);
     qemu_add_drive_opts(&qemu_legacy_drive_opts);
Makefile +1 -1
index 50b4b3a..d8a2891 100644
@@ -312,7 +312,7 @@ $(qapi-modules) $(SRC_PATH)/scripts/qapi-event.py $(qapi-py)
 qmp-commands.h qmp-marshal.c :\
 $(qapi-modules) $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py \
-		$(gen-out-type) -o "." -m $<, \
+		$(gen-out-type) -o "." $<, \
 		"  GEN   $@")
 qmp-introspect.h qmp-introspect.c :\
 $(qapi-modules) $(SRC_PATH)/scripts/qapi-introspect.py $(qapi-py)
docs/writing-qmp-commands.txt +2 -6
index 59aa77a..0a66e0e 100644
@@ -127,7 +127,6 @@ following at the bottom:
     {
         .name       = "hello-world",
         .args_type  = "",
-        .mhandler.cmd_new = qmp_marshal_hello_world,
     },
 
 You're done. Now build qemu, run it as suggested in the "Testing" section,
@@ -179,7 +178,6 @@ The last step is to update the qmp-commands.hx file:
     {
         .name       = "hello-world",
         .args_type  = "message:s?",
-        .mhandler.cmd_new = qmp_marshal_hello_world,
     },
 
 Notice that the "args_type" member got our "message" argument. The character
@@ -454,12 +452,11 @@ There are a number of things to be noticed:
 6. You have to include the "qmp-commands.h" header file in qemu-timer.c,
    otherwise qemu won't build
 
-The last step is to add the correspoding entry in the qmp-commands.hx file:
+The last step is to add the corresponding entry in the qmp-commands.hx file:
 
     {
         .name       = "query-alarm-clock",
         .args_type  = "",
-        .mhandler.cmd_new = qmp_marshal_query_alarm_clock,
     },
 
 Time to test the new command. Build qemu, run it as described in the "Testing"
@@ -518,7 +515,7 @@ in the monitor.c file. The entry for the "info alarmclock" follows:
         .args_type  = "",
         .params     = "",
         .help       = "show information about the alarm clock",
-        .mhandler.info = hmp_info_alarm_clock,
+        .mhandler.cmd = hmp_info_alarm_clock,
     },
 
 To test this, run qemu and type "info alarmclock" in the user monitor.
@@ -605,7 +602,6 @@ To test this you have to add the corresponding qmp-commands.hx entry:
     {
         .name       = "query-alarm-methods",
         .args_type  = "",
-        .mhandler.cmd_new = qmp_marshal_query_alarm_methods,
     },
 
 Now Build qemu, run it as explained in the "Testing" section and try our new
qmp-commands.hx +0 -146

985 lines not on this page · raw message

Signature · 1 line
2.10.0
#13 lore raw

PATCH v6 10/18 monitor: implement 'qmp_query_commands' without qmp_cmds

In reply to Marc-André Lureau

One step towards getting rid of the static qmp_cmds table.

---
 monitor.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
monitor.c +18 -11
index 6e0ae81..f50aa6e 100644
@@ -957,21 +957,28 @@ static void hmp_info_help(Monitor *mon, const QDict *qdict)
     help_cmd(mon, "info");
 }
 
-CommandInfoList *qmp_query_commands(Error **errp)
+static void query_commands_cb(QmpCommand *cmd, void *opaque)
 {
-    CommandInfoList *info, *cmd_list = NULL;
-    const mon_cmd_t *cmd;
-
-    for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
-        info = g_malloc0(sizeof(*info));
-        info->value = g_malloc0(sizeof(*info->value));
-        info->value->name = g_strdup(cmd->name);
+    CommandInfoList *info, **list = opaque;
 
-        info->next = cmd_list;
-        cmd_list = info;
+    if (!cmd->enabled) {
+        return;
     }
 
-    return cmd_list;
+    info = g_malloc0(sizeof(*info));
+    info->value = g_malloc0(sizeof(*info->value));
+    info->value->name = g_strdup(cmd->name);
+    info->next = *list;
+    *list = info;
+}
+
+CommandInfoList *qmp_query_commands(Error **errp)
+{
+    CommandInfoList *list = NULL;
+
+    qmp_for_each_command(query_commands_cb, &list);
+
+    return list;
 }
 
 EventInfoList *qmp_query_events(Error **errp)
Signature · 1 line
2.10.0
#14 lore raw

PATCH v6 11/18 monitor: remove mhandler.cmd_new

In reply to Marc-André Lureau

This is no longer necessary now that we aren't using middle mode anymore.

---
 monitor.c                     |  13 +--
 docs/writing-qmp-commands.txt |   4 +-
 hmp-commands-info.hx          | 118 ++++++++++++------------
 hmp-commands.hx               | 208 +++++++++++++++++++++---------------------
 4 files changed, 170 insertions(+), 173 deletions(-)
monitor.c +5 -8
index f50aa6e..d5d6e95 100644
@@ -130,13 +130,10 @@ typedef struct mon_cmd_t {
     const char *args_type;
     const char *params;
     const char *help;
-    union {
-        void (*cmd)(Monitor *mon, const QDict *qdict);
-        void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
-    } mhandler;
-    /* @sub_table is a list of 2nd level of commands. If it do not exist,
-     * mhandler should be used. If it exist, sub_table[?].mhandler should be
-     * used, and mhandler of 1st level plays the role of help function.
+    void (*cmd)(Monitor *mon, const QDict *qdict);
+    /* @sub_table is a list of 2nd level of commands. If it does not exist,
+     * cmd should be used. If it exists, sub_table[?].cmd should be
+     * used, and cmd of 1st level plays the role of help function.
      */
     struct mon_cmd_t *sub_table;
     void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
@@ -3005,7 +3002,7 @@ static void handle_hmp_command(Monitor *mon, const char *cmdline)
         return;
     }
 
-    cmd->mhandler.cmd(mon, qdict);
+    cmd->cmd(mon, qdict);
     QDECREF(qdict);
 }
 
docs/writing-qmp-commands.txt +2 -2
index 0a66e0e..c425393 100644
@@ -335,7 +335,7 @@ we should add it to the hmp-commands.hx file:
         .args_type  = "message:s?",
         .params     = "hello-world [message]",
         .help       = "Print message to the standard output",
-        .mhandler.cmd = hmp_hello_world,
+        .cmd        = hmp_hello_world,
     },
 
 STEXI
@@ -515,7 +515,7 @@ in the monitor.c file. The entry for the "info alarmclock" follows:
         .args_type  = "",
         .params     = "",
         .help       = "show information about the alarm clock",
-        .mhandler.cmd = hmp_info_alarm_clock,
+        .cmd        = hmp_info_alarm_clock,
     },
 
 To test this, run qemu and type "info alarmclock" in the user monitor.
hmp-commands-info.hx +59 -59

452 lines not on this page · raw message

hmp-commands.hx +104 -104

826 lines not on this page · raw message

Signature · 1 line
2.10.0
#15 lore raw

PATCH v6 12/18 qapi: remove the "middle" mode

In reply to Marc-André Lureau

Now that the register function is always generated, we can remove the so-called "middle" mode from the generator script.

---
 scripts/qapi-commands.py | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
scripts/qapi-commands.py +3 -12
index b150db9..eac64ce 100644
@@ -206,8 +206,7 @@ class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
         self._visited_ret_types = set()
 
     def visit_end(self):
-        if not middle_mode:
-            self.defn += gen_registry(self._regy)
+        self.defn += gen_registry(self._regy)
         self._regy = None
         self._visited_ret_types = None
 
@@ -221,18 +220,10 @@ class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
             self.defn += gen_marshal_output(ret_type)
         self.decl += gen_marshal_decl(name)
         self.defn += gen_marshal(name, arg_type, boxed, ret_type)
-        if not middle_mode:
-            self._regy += gen_register_command(name, success_response)
+        self._regy += gen_register_command(name, success_response)
 
 
-middle_mode = False
-
-(input_file, output_dir, do_c, do_h, prefix, opts) = \
-    parse_command_line("m", ["middle"])
-
-for o, a in opts:
-    if o in ("-m", "--middle"):
-        middle_mode = True
+(input_file, output_dir, do_c, do_h, prefix, opts) = parse_command_line()
 
 c_comment = '''
 /*
Signature · 1 line
2.10.0
#16 lore raw

PATCH v6 13/18 qapi: check invalid arguments on no-args commands

In reply to Marc-André Lureau

The generated marshal functions do not visit arguments from commands that take no arguments. Thus they fail to catch invalid members. Visit the arguments, if provided, to throw an error in case of invalid members.

Currently, qmp_check_client_args() checks for invalid arguments and correctly catches this case. When switching to qmp_dispatch() we want to keep that behaviour. The commands using 'O' may have arbitrary arguments, and must have 'gen': false in the qapi schema to skip the generated checks.

Old/new diff:
void qmp_marshal_stop(QDict *args, QObject **ret, Error **errp)

 {
     Error *err = NULL;

+ Visitor *v = NULL;

- (void)args;
+ if (args) {

+        v = qmp_input_visitor_new(QOBJECT(args), true);
+        visit_start_struct(v, NULL, NULL, 0, &err);
+        if (err) {

+ goto out;
+ }
+
+ if (!err) {
+ visit_check_struct(v, &err);

+        }
+        visit_end_struct(v, NULL);
+        if (err) {

+ goto out;
+ }
+ }

     qmp_stop(&err);
+
+out:
     error_propagate(errp, err);

+ visit_free(v);
+ if (args) {
+ v = qapi_dealloc_visitor_new();
+ visit_start_struct(v, NULL, NULL, 0, NULL);
+
+ visit_end_struct(v, NULL);
+ visit_free(v);
+ }
}

The new code closely resembles code for a command with arguments. Differences:
- the visit of the argument and its cleanup struct don't visit any members (because there are none).
- the visit of the argument struct and its cleanup are conditional.

---
 tests/test-qmp-commands.c | 15 ++++++++++++
 scripts/qapi-commands.py  | 58 +++++++++++++++++++++++++++++++++++------------
 2 files changed, 58 insertions(+), 15 deletions(-)
tests/test-qmp-commands.c +15 -0
index 261fd9e..81cbe54 100644
@@ -106,6 +106,7 @@ static void test_dispatch_cmd(void)
 static void test_dispatch_cmd_failure(void)
 {
     QDict *req = qdict_new();
+    QDict *args = qdict_new();
     QObject *resp;
 
     qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
@@ -114,6 +115,20 @@ static void test_dispatch_cmd_failure(void)
     assert(resp != NULL);
     assert(qdict_haskey(qobject_to_qdict(resp), "error"));
 
+    qobject_decref(resp);
+    QDECREF(req);
+
+    /* check that with extra arguments it throws an error */
+    req = qdict_new();
+    qdict_put(args, "a", qint_from_int(66));
+    qdict_put(req, "arguments", args);
+
+    qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd")));
+
+    resp = qmp_dispatch(QOBJECT(req));
+    assert(resp != NULL);
+    assert(qdict_haskey(qobject_to_qdict(resp), "error"));
+
     qobject_decref(resp);
     QDECREF(req);
 }
scripts/qapi-commands.py +43 -15

101 lines not on this page · raw message

Signature · 1 line
2.10.0
#17 lore raw

PATCH v6 14/18 tests: add a test to check invalid args

In reply to Marc-André Lureau

Check that invalid args on commands without arguments returns an error.

---
 tests/test-qga.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
tests/test-qga.c +21 -0
index 21f44f8..40af649 100644
@@ -198,6 +198,26 @@ static void test_qga_ping(gconstpointer fix)
     QDECREF(ret);
 }
 
+static void test_qga_invalid_args(gconstpointer fix)
+{
+    const TestFixture *fixture = fix;
+    QDict *ret, *error;
+    const gchar *class, *desc;
+
+    ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', "
+                 "'arguments': {'foo': 42 }}");
+    g_assert_nonnull(ret);
+
+    error = qdict_get_qdict(ret, "error");
+    class = qdict_get_try_str(error, "class");
+    desc = qdict_get_try_str(error, "desc");
+
+    g_assert_cmpstr(class, ==, "GenericError");
+    g_assert_cmpstr(desc, ==, "QMP input object member 'foo' is unexpected");
+
+    QDECREF(ret);
+}
+
 static void test_qga_invalid_cmd(gconstpointer fix)
 {
     const TestFixture *fixture = fix;
@@ -911,6 +931,7 @@ int main(int argc, char **argv)
     g_test_add_data_func("/qga/file-write-read", &fix, test_qga_file_write_read);
     g_test_add_data_func("/qga/get-time", &fix, test_qga_get_time);
     g_test_add_data_func("/qga/invalid-cmd", &fix, test_qga_invalid_cmd);
+    g_test_add_data_func("/qga/invalid-args", &fix, test_qga_invalid_args);
     g_test_add_data_func("/qga/fsfreeze-status", &fix,
                          test_qga_fsfreeze_status);
 
Signature · 1 line
2.10.0
#18 lore raw

Re: [Qemu-devel] [PATCH v6 14/18] tests: add a test to check invalid args

Quoted 21 lines · Marc-André Lureau

Check that invalid args on commands without arguments returns an error.

---
 tests/test-qga.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
tests/test-qga.c +6 -0
index 21f44f8..40af649 100644
@@ -198,6 +198,26 @@ static void test_qga_ping(gconstpointer fix)
     QDECREF(ret);
 }
 
+static void test_qga_invalid_args(gconstpointer fix)
+{
+    const TestFixture *fixture = fix;
+    QDict *ret, *error;
+    const gchar *class, *desc;
+

tests/test-qga.c static void test_qga_ping(gconstpointer fix) · line 216

Quoted 10 lines
tests/test-qga.c +10 -0
+    ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', "
+                 "'arguments': {'foo': 42 }}");
+    g_assert_nonnull(ret);
+
+    error = qdict_get_qdict(ret, "error");
+    class = qdict_get_try_str(error, "class");
+    desc = qdict_get_try_str(error, "desc");
+
+    g_assert_cmpstr(class, ==, "GenericError");
+    g_assert_cmpstr(desc, ==, "QMP input object member 'foo' is unexpected");

We generally refrain from expecting an exact error message in tests, but this file does it already elsewhere, so no objection.

Quoted 14 lines
tests/test-qga.c +5 -0
+
+    QDECREF(ret);
+}
+
 static void test_qga_invalid_cmd(gconstpointer fix)
 {
     const TestFixture *fixture = fix;
@@ -911,6 +931,7 @@ int main(int argc, char **argv)
     g_test_add_data_func("/qga/file-write-read", &fix, test_qga_file_write_read);
     g_test_add_data_func("/qga/get-time", &fix, test_qga_get_time);
     g_test_add_data_func("/qga/invalid-cmd", &fix, test_qga_invalid_cmd);
+    g_test_add_data_func("/qga/invalid-args", &fix, test_qga_invalid_args);
     g_test_add_data_func("/qga/fsfreeze-status", &fix,
                          test_qga_fsfreeze_status);
#19 lore raw

PATCH v6 15/18 monitor: use qmp_dispatch()

In reply to Marc-André Lureau

Replace the old manual dispatch and validation code by the generic one provided by qapi common code.

Note that it is now possible to call the following commands that used to be disabled by compile-time conditionals:
- dump-skeys
- query-spice
- rtc-reset-reinjection
- query-gic-capabilities

Their fallback functions return an appropriate "feature disabled" error.

---
 monitor.c    | 326 +++++++----------------------------------------------------
 trace-events |   1 -
 2 files changed, 34 insertions(+), 293 deletions(-)
monitor.c +34 -292
index d5d6e95..8bb8bbf 100644
@@ -166,7 +166,6 @@ struct MonFdset {
 };
 
 typedef struct {
-    QObject *id;
     JSONMessageParser parser;
     /*
      * When a client connects, we're in capabilities negotiation mode.
@@ -229,8 +228,6 @@ static int mon_refcount;
 static mon_cmd_t mon_cmds[];
 static mon_cmd_t info_cmds[];
 
-static const mon_cmd_t qmp_cmds[];
-
 Monitor *cur_mon;
 
 static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
@@ -401,49 +398,6 @@ static void monitor_json_emitter(Monitor *mon, const QObject *data)
     QDECREF(json);
 }
 
-static QDict *build_qmp_error_dict(Error *err)
-{
-    QObject *obj;
-
-    obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
-                             QapiErrorClass_lookup[error_get_class(err)],
-                             error_get_pretty(err));
-
-    return qobject_to_qdict(obj);
-}
-
-static void monitor_protocol_emitter(Monitor *mon, QObject *data,
-                                     Error *err)
-{
-    QDict *qmp;
-
-    trace_monitor_protocol_emitter(mon);
-
-    if (!err) {
-        /* success response */
-        qmp = qdict_new();
-        if (data) {
-            qobject_incref(data);
-            qdict_put_obj(qmp, "return", data);
-        } else {
-            /* return an empty QDict by default */
-            qdict_put(qmp, "return", qdict_new());
-        }
-    } else {
-        /* error response */
-        qmp = build_qmp_error_dict(err);
-    }
-
-    if (mon->qmp.id) {
-        qdict_put_obj(qmp, "id", mon->qmp.id);
-        mon->qmp.id = NULL;
-    }
-
-    monitor_json_emitter(mon, QOBJECT(qmp));
-    QDECREF(qmp);
-}
-
-
 static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
     /* Limit guest-triggerable events to 1 per second */
     [QAPI_EVENT_RTC_CHANGE]        = { 1000 * SCALE_MS },
@@ -2211,11 +2165,6 @@ static mon_cmd_t mon_cmds[] = {
     { NULL, NULL, },
 };
 
-static const mon_cmd_t qmp_cmds[] = {
-#include "qmp-commands-old.h"
-    { /* NULL */ },
-};
-
 /*******************************************************************/
 
 static const char *pch;
@@ -2566,11 +2515,6 @@ static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
     return NULL;
 }
 
-static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
-{
-    return search_dispatch_table(qmp_cmds, cmdname);
-}
-
 /*
  * Parse command name from @cmdp according to command table @table.
  * If blank, return NULL.
@@ -3721,199 +3665,6 @@ static bool invalid_qmp_mode(const Monitor *mon, const char *cmd,
     return false;
 }
 
-/*
- * Argument validation rules:
- *
- * 1. The argument must exist in cmd_args qdict
- * 2. The argument type must be the expected one
- *
- * Special case: If the argument doesn't exist in cmd_args and
- *               the QMP_ACCEPT_UNKNOWNS flag is set, then the
- *               checking is skipped for it.
- */
-static void check_client_args_type(const QDict *client_args,
-                                   const QDict *cmd_args, int flags,
-                                   Error **errp)
-{
-    const QDictEntry *ent;
-
-    for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
-        QObject *obj;
-        QString *arg_type;
-        const QObject *client_arg = qdict_entry_value(ent);
-        const char *client_arg_name = qdict_entry_key(ent);
-
-        obj = qdict_get(cmd_args, client_arg_name);
-        if (!obj) {
-            if (flags & QMP_ACCEPT_UNKNOWNS) {
-                /* handler accepts unknowns */
-                continue;
-            }
-            /* client arg doesn't exist */
-            error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
-            return;
-        }
-
-        arg_type = qobject_to_qstring(obj);
-        assert(arg_type != NULL);
-
-        /* check if argument's type is correct */
-        switch (qstring_get_str(arg_type)[0]) {
-        case 'F':
-        case 'B':
-        case 's':
-            if (qobject_type(client_arg) != QTYPE_QSTRING) {
-                error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
-                           client_arg_name, "string");
-                return;
-            }
-        break;
-        case 'i':
-        case 'l':
-        case 'M':
-        case 'o':
-            if (qobject_type(client_arg) != QTYPE_QINT) {
-                error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
-                           client_arg_name, "int");
-                return;
-            }
-            break;
-        case 'T':
-            if (qobject_type(client_arg) != QTYPE_QINT &&
-                qobject_type(client_arg) != QTYPE_QFLOAT) {
-                error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
-                           client_arg_name, "number");
-                return;
-            }
-            break;
-        case 'b':
-        case '-':
-            if (qobject_type(client_arg) != QTYPE_QBOOL) {
-                error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
-                           client_arg_name, "bool");
-                return;
-            }
-            break;
-        case 'O':
-            assert(flags & QMP_ACCEPT_UNKNOWNS);
-            break;
-        case 'q':
-            /* Any QObject can be passed.  */
-            break;
-        case '/':
-        case '.':
-            /*
-             * These types are not supported by QMP and thus are not
-             * handled here. Fall through.
-             */
-        default:
-            abort();
-        }
-    }
-}
-
-/*
- * - Check if the client has passed all mandatory args
- * - Set special flags for argument validation
- */
-static void check_mandatory_args(const QDict *cmd_args,
-                                 const QDict *client_args, int *flags,
-                                 Error **errp)
-{
-    const QDictEntry *ent;
-
-    for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
-        const char *cmd_arg_name = qdict_entry_key(ent);
-        QString *type = qobject_to_qstring(qdict_entry_value(ent));
-        assert(type != NULL);
-
-        if (qstring_get_str(type)[0] == 'O') {
-            assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
-            *flags |= QMP_ACCEPT_UNKNOWNS;
-        } else if (qstring_get_str(type)[0] != '-' &&
-                   qstring_get_str(type)[1] != '?' &&
-                   !qdict_haskey(client_args, cmd_arg_name)) {
-            error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
-            return;
-        }
-    }
-}
-
-static QDict *qdict_from_args_type(const char *args_type)
-{
-    int i;
-    QDict *qdict;
-    QString *key, *type, *cur_qs;
-
-    assert(args_type != NULL);
-
-    qdict = qdict_new();
-
-    if (args_type == NULL || args_type[0] == '\0') {
-        /* no args, empty qdict */
-        goto out;
-    }
-
-    key = qstring_new();
-    type = qstring_new();
-
-    cur_qs = key;
-
-    for (i = 0;; i++) {
-        switch (args_type[i]) {
-            case ',':
-            case '\0':
-                qdict_put(qdict, qstring_get_str(key), type);
-                QDECREF(key);
-                if (args_type[i] == '\0') {
-                    goto out;
-                }
-                type = qstring_new(); /* qdict has ref */
-                cur_qs = key = qstring_new();
-                break;
-            case ':':
-                cur_qs = type;
-                break;
-            default:
-                qstring_append_chr(cur_qs, args_type[i]);
-                break;
-        }
-    }
-
-out:
-    return qdict;
-}
-
-/*
- * Client argument checking rules:
- *
- * 1. Client must provide all mandatory arguments
- * 2. Each argument provided by the client must be expected
- * 3. Each argument provided by the client must have the type expected
- *    by the command
- */
-static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
-                                  Error **errp)
-{
-    Error *err = NULL;
-    int flags;
-    QDict *cmd_args;
-
-    cmd_args = qdict_from_args_type(cmd->args_type);
-
-    flags = 0;
-    check_mandatory_args(cmd_args, client_args, &flags, &err);
-    if (err) {
-        goto out;
-    }
-
-    check_client_args_type(client_args, cmd_args, flags, &err);
-
-out:
-    error_propagate(errp, err);
-    QDECREF(cmd_args);
-}
-
 /*
  * Input object checking rules
  *
@@ -3972,67 +3723,58 @@ static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
 
 static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
 {
-    Error *local_err = NULL;
-    QObject *obj, *data;
-    QDict *input, *args;
-    const mon_cmd_t *cmd;
-    QmpCommand *qcmd;
+    QObject *req, *rsp = NULL, *id = NULL;
+    QDict *qdict = NULL;
     const char *cmd_name;
     Monitor *mon = cur_mon;
+    Error *err = NULL;
 
-    args = input = NULL;
-    data = NULL;
-
-    obj = json_parser_parse(tokens, NULL);
-    if (!obj) {
-        // FIXME: should be triggered in json_parser_parse()
-        error_setg(&local_err, QERR_JSON_PARSING);
+    req = json_parser_parse_err(tokens, NULL, &err);
+    if (err || !req || qobject_type(req) != QTYPE_QDICT) {
+        if (!err) {
+            error_setg(&err, QERR_JSON_PARSING);
+        }
         goto err_out;
     }
 
-    input = qmp_check_input_obj(obj, &local_err);
-    if (!input) {
-        qobject_decref(obj);
+    qdict = qmp_check_input_obj(req, &err);
+    if (!qdict) {
         goto err_out;
     }
 
-    mon->qmp.id = qdict_get(input, "id");
-    qobject_incref(mon->qmp.id);
+    id = qdict_get(qdict, "id");
+    qobject_incref(id);
+    qdict_del(qdict, "id");
 
-    cmd_name = qdict_get_str(input, "execute");
+    cmd_name = qdict_get_str(qdict, "execute");
     trace_handle_qmp_command(mon, cmd_name);
-    cmd = qmp_find_cmd(cmd_name);
-    qcmd = qmp_find_command(cmd_name);
-    if (!qcmd || !cmd) {
-        error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
-                  "The command %s has not been found", cmd_name);
-        goto err_out;
-    }
-    if (invalid_qmp_mode(mon, cmd_name, &local_err)) {
+
+    if (invalid_qmp_mode(mon, cmd_name, &err)) {
         goto err_out;
     }
 
-    obj = qdict_get(input, "arguments");
-    if (!obj) {
-        args = qdict_new();
-    } else {
-        args = qobject_to_qdict(obj);
-        QINCREF(args);
-    }
+    rsp = qmp_dispatch(req);
 
-    qmp_check_client_args(cmd, args, &local_err);
-    if (local_err) {
-        goto err_out;
+err_out:
+    if (err) {
+        qdict = qdict_new();
+        qdict_put_obj(qdict, "error", qmp_build_error_object(err));
+        error_free(err);
+        rsp = QOBJECT(qdict);
     }
 
-    qcmd->fn(args, &data, &local_err);
+    if (rsp) {
+        if (id) {
+            qdict_put_obj(qobject_to_qdict(rsp), "id", id);
+            id = NULL;
+        }
 
-err_out:
-    monitor_protocol_emitter(mon, data, local_err);
-    qobject_decref(data);
-    error_free(local_err);
-    QDECREF(input);
-    QDECREF(args);
+        monitor_json_emitter(mon, rsp);
+    }
+
+    qobject_decref(id);
+    qobject_decref(rsp);
+    qobject_decref(req);
 }
 
 static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
trace-events +0 -1
index 616cc52..8d59631 100644
@@ -98,7 +98,6 @@ qemu_co_mutex_unlock_return(void *mutex, void *self) "mutex %p self %p"
 
 # monitor.c
 handle_qmp_command(void *mon, const char *cmd_name) "mon %p cmd_name \"%s\""
-monitor_protocol_emitter(void *mon) "mon %p"
 monitor_protocol_event_handler(uint32_t event, void *qdict) "event=%d data=%p"
 monitor_protocol_event_emit(uint32_t event, void *data) "event=%d data=%p"
 monitor_protocol_event_queue(uint32_t event, void *qdict, uint64_t rate) "event=%d data=%p rate=%" PRId64
Signature · 1 line
2.10.0
#20 lore raw

Re: [Qemu-devel] [PATCH v6 15/18] monitor: use qmp_dispatch()

Quoted 10 lines · Marc-André Lureau

Replace the old manual dispatch and validation code by the generic one provided by qapi common code.

Note that it is now possible to call the following commands that used to be disabled by compile-time conditionals:
- dump-skeys
- query-spice
- rtc-reset-reinjection
- query-gic-capabilities

Quoted 3 lines

Their fallback functions return an appropriate "feature disabled" error.

This patch breaks iotest 085 because the "missing parameter" error is now different:

-{"error": {"class": "GenericError", "desc": "Parameter 'snapshot-file' is missing"}} +{"error": {"class": "GenericError", "desc": "Invalid parameter type for 'snapshot-file', expected: string"}}

I was thinking to update the expected output of the iotest, but I guess it's better to return a more meaningful error message?

Berto

#21 lore raw

Re: [Qemu-devel] [PATCH v6 15/18] monitor: use qmp_dispatch()

Hi Alberto

----- Original Message -----

Quoted 18 lines · Marc-André Lureau
Quoted 10 lines

Replace the old manual dispatch and validation code by the generic one provided by qapi common code.

Note that it is now possible to call the following commands that used to be disabled by compile-time conditionals:
- dump-skeys
- query-spice
- rtc-reset-reinjection
- query-gic-capabilities

Quoted 3 lines

Their fallback functions return an appropriate "feature disabled" error.

This patch breaks iotest 085 because the "missing parameter" error is now different:

Quoted 7 lines

-{"error": {"class": "GenericError", "desc": "Parameter 'snapshot-file' is missing"}}
+{"error": {"class": "GenericError", "desc": "Invalid parameter type for 'snapshot-file', expected: string"}}

I was thinking to update the expected output of the iotest, but I guess it's better to return a more meaningful error message?

The change is relatively easy (see attached patch), but there are other tests that expected the current error, see commit fe509ee237307843. I guess we should decided with one, and I think missing parameter is more appropriate.

#22 lore raw

Re: [Qemu-devel] [PATCH v6 15/18] monitor: use qmp_dispatch()

----- Original Message -----

Quoted 26 lines

Hi Alberto

----- Original Message -----

Quoted 23 lines · Marc-André Lureau
Quoted 10 lines

Replace the old manual dispatch and validation code by the generic one provided by qapi common code.

Note that it is now possible to call the following commands that used to be disabled by compile-time conditionals:
- dump-skeys
- query-spice
- rtc-reset-reinjection
- query-gic-capabilities

Quoted 3 lines

Their fallback functions return an appropriate "feature disabled" error.

This patch breaks iotest 085 because the "missing parameter" error is now different:

-{"error": {"class": "GenericError", "desc": "Parameter 'snapshot-file' is missing"}}
+{"error": {"class": "GenericError", "desc": "Invalid parameter type for 'snapshot-file', expected: string"}}

Quoted 7 lines
Quoted 2 lines

I was thinking to update the expected output of the iotest, but I guess it's better to return a more meaningful error message?

The change is relatively easy (see attached patch), but there are other tests that expected the current error, see commit fe509ee237307843. I guess we should decided with one, and I think missing parameter is more appropriate.

diff attached

qapi/qmp-input-visitor.c +56 -7
index 64dd392..22b132b 100644
@@ -169,7 +169,11 @@ static void qmp_input_start_struct(Visitor *v, const char *name, void **obj,
     if (obj) {
         *obj = NULL;
     }
-    if (!qobj || qobject_type(qobj) != QTYPE_QDICT) {
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
+    if (qobject_type(qobj) != QTYPE_QDICT) {
         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
                    "QDict");
         return;
@@ -194,7 +198,11 @@ static void qmp_input_start_list(Visitor *v, const char *name,
     QObject *qobj = qmp_input_get_object(qiv, name, true);
     const QListEntry *entry;
 
-    if (!qobj || qobject_type(qobj) != QTYPE_QLIST) {
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
+    if (qobject_type(qobj) != QTYPE_QLIST) {
         if (list) {
             *list = NULL;
         }
@@ -234,8 +242,10 @@ static void qmp_input_start_alternate(Visitor *v, const char *name,
     QmpInputVisitor *qiv = to_qiv(v);
     QObject *qobj = qmp_input_get_object(qiv, name, false);
 
-    if (!qobj) {
+    if (obj) {
         *obj = NULL;
+    }
+    if (!qobj) {
         error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
         return;
     }
@@ -250,8 +260,13 @@ static void qmp_input_type_int64(Visitor *v, const char *name, int64_t *obj,
                                  Error **errp)
 {
     QmpInputVisitor *qiv = to_qiv(v);
-    QInt *qint = qobject_to_qint(qmp_input_get_object(qiv, name, true));
+    QObject *qobj = qmp_input_get_object(qiv, name, true);
+    QInt *qint = qobject_to_qint(qobj);
 
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
     if (!qint) {
         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
                    "integer");
@@ -266,8 +281,13 @@ static void qmp_input_type_uint64(Visitor *v, const char *name, uint64_t *obj,
 {
     /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
     QmpInputVisitor *qiv = to_qiv(v);
-    QInt *qint = qobject_to_qint(qmp_input_get_object(qiv, name, true));
+    QObject *qobj = qmp_input_get_object(qiv, name, true);
+    QInt *qint = qobject_to_qint(qobj);
 
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
     if (!qint) {
         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
                    "integer");
@@ -281,8 +301,13 @@ static void qmp_input_type_bool(Visitor *v, const char *name, bool *obj,
                                 Error **errp)
 {
     QmpInputVisitor *qiv = to_qiv(v);
-    QBool *qbool = qobject_to_qbool(qmp_input_get_object(qiv, name, true));
+    QObject *qobj = qmp_input_get_object(qiv, name, true);
+    QBool *qbool = qobject_to_qbool(qobj);
 
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
     if (!qbool) {
         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
                    "boolean");
@@ -296,8 +321,16 @@ static void qmp_input_type_str(Visitor *v, const char *name, char **obj,
                                Error **errp)
 {
     QmpInputVisitor *qiv = to_qiv(v);
-    QString *qstr = qobject_to_qstring(qmp_input_get_object(qiv, name, true));
+    QObject *qobj = qmp_input_get_object(qiv, name, true);
+    QString *qstr = qobject_to_qstring(qobj);
 
+    if (obj) {
+        *obj = NULL;
+    }
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
     if (!qstr) {
         *obj = NULL;
         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
@@ -316,6 +349,10 @@ static void qmp_input_type_number(Visitor *v, const char *name, double *obj,
     QInt *qint;
     QFloat *qfloat;
 
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
     qint = qobject_to_qint(qobj);
     if (qint) {
         *obj = qint_get_int(qobject_to_qint(qobj));
@@ -338,6 +375,14 @@ static void qmp_input_type_any(Visitor *v, const char *name, QObject **obj,
     QmpInputVisitor *qiv = to_qiv(v);
     QObject *qobj = qmp_input_get_object(qiv, name, true);
 
+    if (obj) {
+        *obj = NULL;
+    }
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
+
     qobject_incref(qobj);
     *obj = qobj;
 }
@@ -347,6 +392,10 @@ static void qmp_input_type_null(Visitor *v, const char *name, Error **errp)
     QmpInputVisitor *qiv = to_qiv(v);
     QObject *qobj = qmp_input_get_object(qiv, name, true);
 
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
     if (qobject_type(qobj) != QTYPE_QNULL) {
         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
                    "null");
#23 lore raw

Re: [Qemu-devel] [PATCH v6 15/18] monitor: use qmp_dispatch()

Quoted 33 lines · Marc-André Lureau

----- Original Message -----

Quoted 32 lines

Hi Alberto

----- Original Message -----

Quoted 18 lines · Marc-André Lureau
Quoted 10 lines

Replace the old manual dispatch and validation code by the generic one provided by qapi common code.

Note that it is now possible to call the following commands that used to be disabled by compile-time conditionals:
- dump-skeys
- query-spice
- rtc-reset-reinjection
- query-gic-capabilities

Quoted 3 lines

Their fallback functions return an appropriate "feature disabled" error.

This patch breaks iotest 085 because the "missing parameter" error is now different:

Quoted 7 lines

-{"error": {"class": "GenericError", "desc": "Parameter 'snapshot-file' is missing"}}
+{"error": {"class": "GenericError", "desc": "Invalid parameter type for 'snapshot-file', expected: string"}}

I was thinking to update the expected output of the iotest, but I guess it's better to return a more meaningful error message?

The change is relatively easy (see attached patch), but there are other tests that expected the current error, see commit fe509ee237307843. I guess we should decided with one, and I think missing parameter is more appropriate.

Agree.

Quoted 134 lines

diff attached

qapi/qmp-input-visitor.c +52 -7
index 64dd392..22b132b 100644
@@ -169,7 +169,11 @@ static void qmp_input_start_struct(Visitor *v, const char *name, void **obj,
     if (obj) {
         *obj = NULL;
     }
-    if (!qobj || qobject_type(qobj) != QTYPE_QDICT) {
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
+    if (qobject_type(qobj) != QTYPE_QDICT) {
         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
                    "QDict");
         return;
@@ -194,7 +198,11 @@ static void qmp_input_start_list(Visitor *v, const char *name,
     QObject *qobj = qmp_input_get_object(qiv, name, true);
     const QListEntry *entry;
 
-    if (!qobj || qobject_type(qobj) != QTYPE_QLIST) {
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
+    if (qobject_type(qobj) != QTYPE_QLIST) {
         if (list) {
             *list = NULL;
         }
@@ -234,8 +242,10 @@ static void qmp_input_start_alternate(Visitor *v, const char *name,
     QmpInputVisitor *qiv = to_qiv(v);
     QObject *qobj = qmp_input_get_object(qiv, name, false);
 
-    if (!qobj) {
+    if (obj) {
         *obj = NULL;
+    }
+    if (!qobj) {
         error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
         return;
     }
@@ -250,8 +260,13 @@ static void qmp_input_type_int64(Visitor *v, const char *name, int64_t *obj,
                                  Error **errp)
 {
     QmpInputVisitor *qiv = to_qiv(v);
-    QInt *qint = qobject_to_qint(qmp_input_get_object(qiv, name, true));
+    QObject *qobj = qmp_input_get_object(qiv, name, true);
+    QInt *qint = qobject_to_qint(qobj);
 
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
     if (!qint) {
         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
                    "integer");
@@ -266,8 +281,13 @@ static void qmp_input_type_uint64(Visitor *v, const char *name, uint64_t *obj,
 {
     /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
     QmpInputVisitor *qiv = to_qiv(v);
-    QInt *qint = qobject_to_qint(qmp_input_get_object(qiv, name, true));
+    QObject *qobj = qmp_input_get_object(qiv, name, true);
+    QInt *qint = qobject_to_qint(qobj);
 
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
     if (!qint) {
         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
                    "integer");
@@ -281,8 +301,13 @@ static void qmp_input_type_bool(Visitor *v, const char *name, bool *obj,
                                 Error **errp)
 {
     QmpInputVisitor *qiv = to_qiv(v);
-    QBool *qbool = qobject_to_qbool(qmp_input_get_object(qiv, name, true));
+    QObject *qobj = qmp_input_get_object(qiv, name, true);
+    QBool *qbool = qobject_to_qbool(qobj);
 
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
     if (!qbool) {
         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
                    "boolean");
@@ -296,8 +321,16 @@ static void qmp_input_type_str(Visitor *v, const char *name, char **obj,
                                Error **errp)
 {
     QmpInputVisitor *qiv = to_qiv(v);
-    QString *qstr = qobject_to_qstring(qmp_input_get_object(qiv, name, true));
+    QObject *qobj = qmp_input_get_object(qiv, name, true);
+    QString *qstr = qobject_to_qstring(qobj);
 
+    if (obj) {
+        *obj = NULL;
+    }
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
     if (!qstr) {
         *obj = NULL;
         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
@@ -316,6 +349,10 @@ static void qmp_input_type_number(Visitor *v, const char *name, double *obj,
     QInt *qint;
     QFloat *qfloat;
 
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
     qint = qobject_to_qint(qobj);
     if (qint) {
         *obj = qint_get_int(qobject_to_qint(qobj));
@@ -338,6 +375,14 @@ static void qmp_input_type_any(Visitor *v, const char *name, QObject **obj,
     QmpInputVisitor *qiv = to_qiv(v);
     QObject *qobj = qmp_input_get_object(qiv, name, true);
 
+    if (obj) {
+        *obj = NULL;
+    }
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
+
     qobject_incref(qobj);
     *obj = qobj;
 }

qapi/qmp-input-visitor.c static void qmp_input_type_null(Visitor *v, const char *name, Error **errp) · line 401

Quoted 10 lines
qapi/qmp-input-visitor.c +4 -0
@@ -347,6 +392,10 @@ static void qmp_input_type_null(Visitor *v, const char *name, Error **errp)
     QmpInputVisitor *qiv = to_qiv(v);
     QObject *qobj = qmp_input_get_object(qiv, name, true);
 
+    if (!qobj) {
+        error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
+        return;
+    }
     if (qobject_type(qobj) != QTYPE_QNULL) {
         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
                    "null");

Changing qmp_input_get_object() to take an Error * parameter and set the "Parameter FOO is missing" error could require less code. Suggest to give it a try and see how you like it.

Need a proper patch to merge this regression fix.

#24 lore raw

PATCH v6 16/18 build-sys: remove qmp-commands-old.h

In reply to Marc-André Lureau
---
 Makefile.target | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
Makefile.target +2 -5
index 8c7a072..19cc49c 100644
@@ -156,7 +156,7 @@ else
 obj-y += hw/$(TARGET_BASE_ARCH)/
 endif
 
-GENERATED_HEADERS += hmp-commands.h hmp-commands-info.h qmp-commands-old.h
+GENERATED_HEADERS += hmp-commands.h hmp-commands-info.h
 
 endif # CONFIG_SOFTMMU
 
@@ -209,13 +209,10 @@ hmp-commands.h: $(SRC_PATH)/hmp-commands.hx $(SRC_PATH)/scripts/hxtool
 hmp-commands-info.h: $(SRC_PATH)/hmp-commands-info.hx $(SRC_PATH)/scripts/hxtool
 	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"  GEN   $(TARGET_DIR)$@")
 
-qmp-commands-old.h: $(SRC_PATH)/qmp-commands.hx $(SRC_PATH)/scripts/hxtool
-	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"  GEN   $(TARGET_DIR)$@")
-
 clean: clean-target
 	rm -f *.a *~ $(PROGS)
 	rm -f $(shell find . -name '*.[od]')
-	rm -f hmp-commands.h qmp-commands-old.h gdbstub-xml.c
+	rm -f hmp-commands.h gdbstub-xml.c
 ifdef CONFIG_TRACE_SYSTEMTAP
 	rm -f *.stp
 endif
Signature · 1 line
2.10.0
#25 lore raw

PATCH v6 17/18 qmp-commands.hx: fix some styling

In reply to Marc-André Lureau

Add some missing lines, remove superflous @ in command name, remove trailing spaces.

---
 qmp-commands.hx | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)
qmp-commands.hx +18 -16
index 734f3d7..b38772c 100644
@@ -91,7 +91,7 @@ eject
 
 Eject a removable medium.
 
-Arguments: 
+Arguments:
 
 - force: force ejection (json-bool, optional)
 - device: device name (json-string)
@@ -1395,7 +1395,6 @@ EQMP
     },
 
 SQMP
-
 block-dirty-bitmap-add
 ----------------------
 Since 2.4
@@ -1422,7 +1421,6 @@ EQMP
     },
 
 SQMP
-
 block-dirty-bitmap-remove
 -------------------------
 Since 2.4
@@ -1449,7 +1447,6 @@ EQMP
     },
 
 SQMP
-
 block-dirty-bitmap-clear
 ------------------------
 Since 2.4
@@ -2176,7 +2173,7 @@ human-monitor-command
 
 Execute a Human Monitor command.
 
-Arguments: 
+Arguments:
 
 - command-line: the command name and its arguments, just like the
                 Human Monitor's shell (json-string)
@@ -2988,7 +2985,7 @@ Example:
                   },
                   "function":0,
                   "regions":[
-   
+
                   ]
                },
                {
@@ -3005,7 +3002,7 @@ Example:
                   },
                   "function":0,
                   "regions":[
-   
+
                   ]
                },
                {
@@ -3467,8 +3464,8 @@ The main json-object contains the following:
 - "setup-time" amount of setup time in milliseconds _before_ the
                iterations begin but _after_ the QMP command is issued.
                This is designed to provide an accounting of any activities
-               (such as RDMA pinning) which may be expensive, but do not 
-               actually occur during the iterative migration rounds 
+               (such as RDMA pinning) which may be expensive, but do not
+               actually occur during the iterative migration rounds
                themselves. (json-int)
 - "downtime": only present when migration has finished correctly
               total amount in ms for downtime that happened (json-int)
@@ -4393,8 +4390,8 @@ EQMP
     },
 
 SQMP
-@query-named-block-nodes
-------------------------
+query-named-block-nodes
+-----------------------
 
 Return a list of BlockDeviceInfo for all the named block driver nodes
 
@@ -4543,7 +4540,7 @@ EQMP
     },
 
 SQMP
-@query-memory-devices
+query-memory-devices
 --------------------
 
 Return a list of memory devices.
@@ -4561,6 +4558,7 @@ Example:
                         "slot": 0},
                    "type": "dimm"
                  } ] }
+
 EQMP
 
     {
@@ -4569,8 +4567,8 @@ EQMP
     },
 
 SQMP
-@query-acpi-ospm-status
---------------------
+query-acpi-ospm-status
+----------------------
 
 Return list of ACPIOSTInfo for devices that support status reporting
 via ACPI _OST method.
@@ -4582,6 +4580,7 @@ Example:
                  { "slot": "2", "slot-type": "DIMM", "source": 0, "status": 0},
                  { "slot": "3", "slot-type": "DIMM", "source": 0, "status": 0}
    ]}
+
 EQMP
 
 #if defined TARGET_I386
@@ -4603,6 +4602,7 @@ Example:
 
 -> { "execute": "rtc-reset-reinjection" }
 <- { "return": {} }
+
 EQMP
 
     {
@@ -4634,6 +4634,7 @@ Example:
 
 -> { "execute": "trace-event-get-state", "arguments": { "name": "qemu_memalign" } }
 <- { "return": [ { "name": "qemu_memalign", "state": "disabled" } ] }
+
 EQMP
 
     {
@@ -4668,6 +4669,7 @@ Example:
 
 -> { "execute": "trace-event-set-state", "arguments": { "name": "qemu_memalign", "enable": "true" } }
 <- { "return": {} }
+
 EQMP
 
     {
@@ -4676,8 +4678,8 @@ EQMP
     },
 
 SQMP
-@input-send-event
------------------
+input-send-event
+----------------
 
 Send input event to guest.
 
Signature · 1 line
2.10.0
#26 lore raw

PATCH v6 18/18 Replace qmp-commands.hx by docs/qmp-commands.txt

In reply to Marc-André Lureau

The only remaining function of qmp-commands.hx is to let us generate qmp-commands.txt from it. Replace qmp-commands.hx by qmp-commands.txt.

We intend to move the documentation into the QAPI schema and generate qapi-commands.txt from it, but not right now.

---
 Makefile                                 |    6 +-
 .gitignore                               |    1 -
 MAINTAINERS                              |    1 -
 docs/qapi-code-gen.txt                   |    6 +-
 qmp-commands.hx => docs/qmp-commands.txt | 1127 ------------------------------
 docs/writing-qmp-commands.txt            |   38 -
 6 files changed, 4 insertions(+), 1175 deletions(-)
 rename qmp-commands.hx => docs/qmp-commands.txt (87%)
Makefile +1 -5
index d8a2891..bac69b2 100644
@@ -92,7 +92,6 @@ HELPERS-$(CONFIG_LINUX) = qemu-bridge-helper$(EXESUF)
 
 ifdef BUILD_DOCS
 DOCS=qemu-doc.html qemu-tech.html qemu.1 qemu-img.1 qemu-nbd.8 qemu-ga.8
-DOCS+=qmp-commands.txt
 ifdef CONFIG_VIRTFS
 DOCS+=fsdev/virtfs-proxy-helper.1
 endif
@@ -432,7 +431,7 @@ endif
 install-doc: $(DOCS)
 	$(INSTALL_DIR) "$(DESTDIR)$(qemu_docdir)"
 	$(INSTALL_DATA) qemu-doc.html  qemu-tech.html "$(DESTDIR)$(qemu_docdir)"
-	$(INSTALL_DATA) qmp-commands.txt "$(DESTDIR)$(qemu_docdir)"
+	$(INSTALL_DATA) docs/qmp-commands.txt "$(DESTDIR)$(qemu_docdir)"
 ifdef CONFIG_POSIX
 	$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man1"
 	$(INSTALL_DATA) qemu.1 "$(DESTDIR)$(mandir)/man1"
@@ -555,9 +554,6 @@ qemu-monitor.texi: $(SRC_PATH)/hmp-commands.hx $(SRC_PATH)/scripts/hxtool
 qemu-monitor-info.texi: $(SRC_PATH)/hmp-commands-info.hx $(SRC_PATH)/scripts/hxtool
 	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@,"  GEN   $@")
 
-qmp-commands.txt: $(SRC_PATH)/qmp-commands.hx $(SRC_PATH)/scripts/hxtool
-	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -q < $< > $@,"  GEN   $@")
-
 qemu-img-cmds.texi: $(SRC_PATH)/qemu-img-cmds.hx $(SRC_PATH)/scripts/hxtool
 	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@,"  GEN   $@")
 
.gitignore +0 -1
index 88ec249..0ab8263 100644
@@ -53,7 +53,6 @@
 /qemu-bridge-helper
 /qemu-monitor.texi
 /qemu-monitor-info.texi
-/qmp-commands.txt
 /vscclient
 /fsdev/virtfs-proxy-helper
 *.[1-9]
MAINTAINERS +0 -1
index b6fb84e..84bdc46 100644
@@ -1237,7 +1237,6 @@ M: Markus Armbruster <[email protected]>
 S: Supported
 F: qmp.c
 F: monitor.c
-F: qmp-commands.hx
 F: docs/*qmp-*
 F: scripts/qmp/
 T: git git://repo.or.cz/qemu/armbru.git qapi-next
docs/qapi-code-gen.txt +3 -3
index de298dc..5d4c2cd 100644
@@ -964,9 +964,9 @@ Example:
 
 Used to generate the marshaling/dispatch functions for the commands
 defined in the schema. The generated code implements
-qmp_marshal_COMMAND() (mentioned in qmp-commands.hx, and registered
-automatically), and declares qmp_COMMAND() that the user must
-implement.  The following files are generated:
+qmp_marshal_COMMAND() (registered automatically), and declares
+qmp_COMMAND() that the user must implement.  The following files are
+generated:
 
 $(prefix)qmp-marshal.c: command marshal/dispatch functions for each
                         QMP command defined in the schema. Functions
docs/qmp-commands.txt renamed from qmp-commands.hx +0 -1127

1,869 lines not on this page · raw message

docs/writing-qmp-commands.txt +0 -38
index c425393..cfa6fe7 100644
@@ -119,16 +119,6 @@ There are a few things to be noticed:
 5. Printing to the terminal is discouraged for QMP commands, we do it here
    because it's the easiest way to demonstrate a QMP command
 
-Now a little hack is needed. As we're still using the old QMP server we need
-to add the new command to its internal dispatch table. This step won't be
-required in the near future. Open the qmp-commands.hx file and add the
-following at the bottom:
-
-    {
-        .name       = "hello-world",
-        .args_type  = "",
-    },
-
 You're done. Now build qemu, run it as suggested in the "Testing" section,
 and then type the following QMP command:
 
@@ -173,20 +163,6 @@ There are two important details to be noticed:
 2. The C implementation signature must follow the schema's argument ordering,
    which is defined by the "data" member
 
-The last step is to update the qmp-commands.hx file:
-
-    {
-        .name       = "hello-world",
-        .args_type  = "message:s?",
-    },
-
-Notice that the "args_type" member got our "message" argument. The character
-"s" stands for "string" and "?" means it's optional. This too must be ordered
-according to the C implementation and schema file. You can look for more
-examples in the qmp-commands.hx file if you need to define more arguments.
-
-Again, this step won't be required in the future.
-
 Time to test our new version of the "hello-world" command. Build qemu, run it as
 described in the "Testing" section and then send two commands:
 
@@ -452,13 +428,6 @@ There are a number of things to be noticed:
 6. You have to include the "qmp-commands.h" header file in qemu-timer.c,
    otherwise qemu won't build
 
-The last step is to add the corresponding entry in the qmp-commands.hx file:
-
-    {
-        .name       = "query-alarm-clock",
-        .args_type  = "",
-    },
-
 Time to test the new command. Build qemu, run it as described in the "Testing"
 section and try this:
 
@@ -597,13 +566,6 @@ iteration of the loop. That's because the alarm timer method in use is the
 first element of the alarm_timers array. Also notice that QAPI lists are handled
 by hand and we return the head of the list.
 
-To test this you have to add the corresponding qmp-commands.hx entry:
-
-    {
-        .name       = "query-alarm-methods",
-        .args_type  = "",
-    },
-
 Now Build qemu, run it as explained in the "Testing" section and try our new
 command:
 
Signature · 1 line
2.10.0
#27 lore raw

Re: [Qemu-devel] [PATCH v6 18/18] Replace qmp-commands.hx by docs/qmp-commands.txt

Quoted 20 lines · Marc-André Lureau

The only remaining function of qmp-commands.hx is to let us generate qmp-commands.txt from it. Replace qmp-commands.hx by qmp-commands.txt.

We intend to move the documentation into the QAPI schema and generate qapi-commands.txt from it, but not right now.

---
 Makefile                                 |    6 +-
 .gitignore                               |    1 -
 MAINTAINERS                              |    1 -
 docs/qapi-code-gen.txt                   |    6 +-
 qmp-commands.hx => docs/qmp-commands.txt | 1127 ------------------------------
 docs/writing-qmp-commands.txt            |   38 -
 6 files changed, 4 insertions(+), 1175 deletions(-)
 rename qmp-commands.hx => docs/qmp-commands.txt (87%)

+++ b/MAINTAINERS

MAINTAINERS M: Markus Armbruster <[email protected]> · line 1242

Quoted 7 lines
MAINTAINERS +0 -1
@@ -1237,7 +1237,6 @@ M: Markus Armbruster <[email protected]>
 S: Supported
 F: qmp.c
 F: monitor.c
-F: qmp-commands.hx
 F: docs/*qmp-*
 F: scripts/qmp/
 T: git git://repo.or.cz/qemu/armbru.git qapi-next

Shouldn't we be adding F: qmp-commands.txt somewhere to the file in the interim, at least until the promised later patch lands that once again generates documentation (this time from .json files)?

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#28 lore raw

Re: [Qemu-devel] [PATCH v6 18/18] Replace qmp-commands.hx by docs/qmp-commands.txt

Quoted 19 lines · Eric Blake
Quoted 17 lines · Marc-André Lureau

The only remaining function of qmp-commands.hx is to let us generate qmp-commands.txt from it. Replace qmp-commands.hx by qmp-commands.txt.

We intend to move the documentation into the QAPI schema and generate qapi-commands.txt from it, but not right now.

---
 Makefile                                 |    6 +-
 .gitignore                               |    1 -
 MAINTAINERS                              |    1 -
 docs/qapi-code-gen.txt                   |    6 +-
 qmp-commands.hx => docs/qmp-commands.txt | 1127 ------------------------------
 docs/writing-qmp-commands.txt            |   38 -
 6 files changed, 4 insertions(+), 1175 deletions(-)
 rename qmp-commands.hx => docs/qmp-commands.txt (87%)
Quoted 13 lines

MAINTAINERS M: Markus Armbruster <[email protected]> · line 1242

Quoted 9 lines

+++ b/MAINTAINERS

MAINTAINERS +0 -1
@@ -1237,7 +1237,6 @@ M: Markus Armbruster <[email protected]>
 S: Supported
 F: qmp.c
 F: monitor.c
-F: qmp-commands.hx
 F: docs/*qmp-*
 F: scripts/qmp/
 T: git git://repo.or.cz/qemu/armbru.git qapi-next

Shouldn't we be adding F: qmp-commands.txt somewhere to the file in the interim, at least until the promised later patch lands that once again generates documentation (this time from .json files)?

It's covered by the existing F: docs/*qmp-* line.

#29 lore raw

Re: [Qemu-devel] [PATCH v6 18/18] Replace qmp-commands.hx by docs/qmp-commands.txt

In reply to Marc-André Lureau
Quoted 5 lines · Marc-André Lureau

The only remaining function of qmp-commands.hx is to let us generate qmp-commands.txt from it. Replace qmp-commands.hx by qmp-commands.txt.

We intend to move the documentation into the QAPI schema and generate qapi-commands.txt from it, but not right now.

This is a bit sad, though I see that there were not all that many actual docstrings to autogenerate from.

What's the plan for reintroducing autogeneration of the protocol docs?

thanks
-- PMM

#30 lore raw

Re: [Qemu-devel] [PATCH v6 18/18] Replace qmp-commands.hx by docs/qmp-commands.txt

Hi

Quoted 5 lines · Peter Maydell
Quoted 3 lines · Marc-André Lureau

The only remaining function of qmp-commands.hx is to let us generate qmp-commands.txt from it. Replace qmp-commands.hx by qmp-commands.txt.

Quoted 10 lines
Quoted 2 lines

We intend to move the documentation into the QAPI schema and generate qapi-commands.txt from it, but not right now.

This is a bit sad, though I see that there were not all that many actual docstrings to autogenerate from.

What's the plan for reintroducing autogeneration of the protocol docs?

I'll rebase my qapi-doc branch, do some cleanups, and submit (last update:
https://github.com/elmarco/qemu/commits/qapi-doc). Trouble is the series got pretty large after I splitted all the documentation move. It's over 150 patches iirc, but it helps a lot with reviewing. Should I send it by chunks, or should I send a single patch and points to the github branch for the details? other options?

Signature · 1 line
Marc-André Lureau
#31 lore raw

Re: [Qemu-devel] [PATCH v6 18/18] Replace qmp-commands.hx by docs/qmp-commands.txt

Quoted 15 lines · Marc-André Lureau

Hi

Quoted 10 lines · Peter Maydell
Quoted 5 lines · Marc-André Lureau

The only remaining function of qmp-commands.hx is to let us generate qmp-commands.txt from it. Replace qmp-commands.hx by qmp-commands.txt.

We intend to move the documentation into the QAPI schema and generate qapi-commands.txt from it, but not right now.

This is a bit sad, though I see that there were not all that many actual docstrings to autogenerate from.

Actually no sadder than before. This patch merely drops the parts of qmp-commands.hx that are no longer used, then renames the result to docs/qmp-commands.txt. No change to the duplication of docs with the QAPI schema. The actual de-duplication is in Marc-André's queue.

Quoted 10 lines
Quoted 4 lines

What's the plan for reintroducing autogeneration of the protocol docs?

I'll rebase my qapi-doc branch, do some cleanups, and submit (last update:
https://github.com/elmarco/qemu/commits/qapi-doc). Trouble is the series got pretty large after I splitted all the documentation move. It's over 150 patches iirc, but it helps a lot with reviewing. Should I send it by chunks, or should I send a single patch and points to the github branch for the details? other options?

Let's try chunks.

#32 lore raw
In reply to Marc-André Lureau
Quoted 5 lines · Marc-André Lureau

Hi,

Although some QMP commands are still not fully qapi'fied, it is possible to use more qapi common and generated code by dropping the 'middle' mode and use qmp_dispatch().

Applied to qapi-next, thanks!