From nobody Tue Nov 4 18:28:05 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1530633393680166.35408756866707; Tue, 3 Jul 2018 08:56:33 -0700 (PDT) Received: from localhost ([::1]:41271 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1faNfI-0001IH-V2 for importer@patchew.org; Tue, 03 Jul 2018 11:56:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34532) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1faNN2-0002QQ-TA for qemu-devel@nongnu.org; Tue, 03 Jul 2018 11:37:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1faNMy-0005N0-Gv for qemu-devel@nongnu.org; Tue, 03 Jul 2018 11:37:36 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:50136 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1faNMy-0005L4-5D for qemu-devel@nongnu.org; Tue, 03 Jul 2018 11:37:32 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AD3EE8163C5F for ; Tue, 3 Jul 2018 15:37:31 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-126.ams2.redhat.com [10.36.116.126]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6BED32156880 for ; Tue, 3 Jul 2018 15:37:31 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 684EF1132CAA; Tue, 3 Jul 2018 17:37:28 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Tue, 3 Jul 2018 17:37:13 +0200 Message-Id: <20180703153728.2175-18-armbru@redhat.com> In-Reply-To: <20180703153728.2175-1-armbru@redhat.com> References: <20180703153728.2175-1-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 03 Jul 2018 15:37:31 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 03 Jul 2018 15:37:31 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'armbru@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PULL 17/32] qmp: Don't let malformed in-band commands jump the queue X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" handle_qmp_command() reports certain errors right away. This is wrong when OOB is enabled, because the errors can "jump the queue" then, as the previous commit demonstrates. To fix, we need to delay errors until dispatch. Do that for semantic errors, mostly by reverting ill-advised parts of commit cf869d53172 "qmp: support out-of-band (oob) execution". Bonus: doesn't run qmp_dispatch_check_obj() twice, once in handle_qmp_command(), and again in do_qmp_dispatch(). That's also due to commit cf869d53172. The next commit will fix queue jumping for syntax errors. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-Id: <20180703085358.13941-18-armbru@redhat.com> --- include/qapi/qmp/dispatch.h | 2 - monitor.c | 79 +++++++++---------------------------- qapi/qmp-dispatch.c | 12 +++++- tests/qmp-test.c | 4 +- 4 files changed, 30 insertions(+), 67 deletions(-) diff --git a/include/qapi/qmp/dispatch.h b/include/qapi/qmp/dispatch.h index 303a15ba84..514bfc45b0 100644 --- a/include/qapi/qmp/dispatch.h +++ b/include/qapi/qmp/dispatch.h @@ -48,8 +48,6 @@ bool qmp_command_is_enabled(const QmpCommand *cmd); const char *qmp_command_name(const QmpCommand *cmd); bool qmp_has_success_response(const QmpCommand *cmd); QObject *qmp_build_error_object(Error *err); -QDict *qmp_dispatch_check_obj(const QObject *request, bool allow_oob, - Error **errp); QObject *qmp_dispatch(QmpCommandList *cmds, QObject *request, bool allow_oob); bool qmp_is_oob(QDict *dict); diff --git a/monitor.c b/monitor.c index c49214cd8f..be2a856d1c 100644 --- a/monitor.c +++ b/monitor.c @@ -1290,48 +1290,6 @@ static void qmp_caps_apply(Monitor *mon, QMPCapabili= tyList *list) } } =20 -/* - * Return true if check successful, or false otherwise. When false is - * returned, detailed error will be in errp if provided. - */ -static bool qmp_cmd_oob_check(Monitor *mon, QDict *req, Error **errp) -{ - const char *command; - QmpCommand *cmd; - - command =3D qdict_get_try_str(req, "execute"); - if (!command) { - command =3D qdict_get_try_str(req, "exec-oob"); - } - if (!command) { - error_setg(errp, "Command field 'execute' missing"); - return false; - } - - cmd =3D qmp_find_command(mon->qmp.commands, command); - if (!cmd) { - if (mon->qmp.commands =3D=3D &qmp_cap_negotiation_commands) { - error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND, - "Expecting capabilities negotiation " - "with 'qmp_capabilities'"); - } else { - error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND, - "The command %s has not been found", command); - } - return false; - } - - if (qmp_is_oob(req)) { - if (!(cmd->options & QCO_ALLOW_OOB)) { - error_setg(errp, "The command %s does not support OOB", - command); - return false; - } - } - - return true; -} - void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable, Error **errp) { @@ -4171,6 +4129,7 @@ static void monitor_qmp_dispatch(Monitor *mon, QObjec= t *req, QObject *id) { Monitor *old_mon; QObject *rsp; + QDict *error; =20 old_mon =3D cur_mon; cur_mon =3D mon; @@ -4179,6 +4138,19 @@ static void monitor_qmp_dispatch(Monitor *mon, QObje= ct *req, QObject *id) =20 cur_mon =3D old_mon; =20 + if (mon->qmp.commands =3D=3D &qmp_cap_negotiation_commands) { + error =3D qdict_get_qdict(qobject_to(QDict, rsp), "error"); + if (error + && !g_strcmp0(qdict_get_try_str(error, "class"), + QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND))) { + /* Provide a more useful error message */ + qdict_del(error, "desc"); + qdict_put_str(error, "desc", "Expecting capabilities negotiati= on" + " with 'qmp_capabilities'"); + } + } + + /* Respond if necessary */ monitor_qmp_respond(mon, rsp, NULL, qobject_ref(id)); } =20 @@ -4256,7 +4228,9 @@ static void handle_qmp_command(JSONMessageParser *par= ser, GQueue *tokens) error_setg(&err, QERR_JSON_PARSING); } if (err) { - goto err; + assert(!req); + monitor_qmp_respond(mon, NULL, err, NULL); + return; } =20 qdict =3D qobject_to(QDict, req); @@ -4271,18 +4245,7 @@ static void handle_qmp_command(JSONMessageParser *pa= rser, GQueue *tokens) qobject_unref(req_json); } =20 - /* Check against the request in general layout */ - qdict =3D qmp_dispatch_check_obj(req, qmp_oob_enabled(mon), &err); - if (!qdict) { - goto err; - } - - /* Check against OOB specific */ - if (!qmp_cmd_oob_check(mon, qdict, &err)) { - goto err; - } - - if (qmp_is_oob(qdict)) { + if (qdict && qmp_is_oob(qdict)) { /* Out-of-band (OOB) requests are executed directly in parser. */ trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(id) ?: ""); @@ -4336,12 +4299,6 @@ static void handle_qmp_command(JSONMessageParser *pa= rser, GQueue *tokens) =20 /* Kick the dispatcher routine */ qemu_bh_schedule(mon_global.qmp_dispatcher_bh); - return; - -err: - /* FIXME overtakes queued in-band commands, wrong when !qmp_is_oob() */ - monitor_qmp_respond(mon, NULL, err, NULL); - qobject_unref(req); } =20 static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size) diff --git a/qapi/qmp-dispatch.c b/qapi/qmp-dispatch.c index 12be120fe7..6d78f3e9f6 100644 --- a/qapi/qmp-dispatch.c +++ b/qapi/qmp-dispatch.c @@ -20,8 +20,8 @@ #include "qapi/qmp/qbool.h" #include "sysemu/sysemu.h" =20 -QDict *qmp_dispatch_check_obj(const QObject *request, bool allow_oob, - Error **errp) +static QDict *qmp_dispatch_check_obj(const QObject *request, bool allow_oo= b, + Error **errp) { const char *exec_key =3D NULL; const QDictEntry *ent; @@ -78,6 +78,7 @@ static QObject *do_qmp_dispatch(QmpCommandList *cmds, QOb= ject *request, bool allow_oob, Error **errp) { Error *local_err =3D NULL; + bool oob; const char *command; QDict *args, *dict; QmpCommand *cmd; @@ -89,9 +90,11 @@ static QObject *do_qmp_dispatch(QmpCommandList *cmds, QO= bject *request, } =20 command =3D qdict_get_try_str(dict, "execute"); + oob =3D false; if (!command) { assert(allow_oob); command =3D qdict_get_str(dict, "exec-oob"); + oob =3D true; } cmd =3D qmp_find_command(cmds, command); if (cmd =3D=3D NULL) { @@ -104,6 +107,11 @@ static QObject *do_qmp_dispatch(QmpCommandList *cmds, = QObject *request, command); return NULL; } + if (oob && !(cmd->options & QCO_ALLOW_OOB)) { + error_setg(errp, "The command %s does not support OOB", + command); + return false; + } =20 if (runstate_check(RUN_STATE_PRECONFIG) && !(cmd->options & QCO_ALLOW_PRECONFIG)) { diff --git a/tests/qmp-test.c b/tests/qmp-test.c index 6581d1d755..57c1296573 100644 --- a/tests/qmp-test.c +++ b/tests/qmp-test.c @@ -243,12 +243,12 @@ static void test_qmp_oob(void) recv_cmd_id(qts, "ib-blocks-1"); recv_cmd_id(qts, "ib-quick-1"); =20 - /* FIXME certain in-band errors overtake slow in-band command */ + /* Even malformed in-band command fails in-band */ send_cmd_that_blocks(qts, "blocks-2"); qtest_async_qmp(qts, "{ 'id': 'err-2' }"); - recv_cmd_id(qts, NULL); unblock_blocked_cmd(); recv_cmd_id(qts, "blocks-2"); + recv_cmd_id(qts, "err-2"); cleanup_blocking_cmd(); =20 qtest_quit(qts); --=20 2.17.1