From patchwork Fri Nov 8 15:01:03 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= X-Patchwork-Id: 1191953 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="MXIiUya/"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 478k9w3TT5z9sP3 for ; Sat, 9 Nov 2019 02:06:12 +1100 (AEDT) Received: from localhost ([::1]:55884 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iT5px-0005va-67 for incoming@patchwork.ozlabs.org; Fri, 08 Nov 2019 10:06:09 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46171) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iT5mj-0003Cd-DL for qemu-devel@nongnu.org; Fri, 08 Nov 2019 10:02:50 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iT5mi-0003o2-1I for qemu-devel@nongnu.org; Fri, 08 Nov 2019 10:02:49 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:49936 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iT5mg-0003nG-2v for qemu-devel@nongnu.org; Fri, 08 Nov 2019 10:02:46 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573225364; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=e1yDkbX9kSrQE2esR1pQgQNkZhwENHxRrNgX62Wk9qo=; b=MXIiUya/MPD2Ez2ZMO3WRgSUVGJrGMj9oeAV2dc1fDGh2U6QlROkmBsw3yScmtxyc0HrDx jEy6MP1v17zsv4YMPll0Z9RWLcAW8Ddt5FyoXL3NqKkIX6ae2lCeyoCb9fhlvMmJxDyP/A EHaR0pusZ/Z35NiNuk/9v4WQnnU206I= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-283-LzGJj93WOm6s5V5vro0rcg-1; Fri, 08 Nov 2019 10:02:43 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CF4778017DD for ; Fri, 8 Nov 2019 15:02:42 +0000 (UTC) Received: from localhost (ovpn-112-25.ams2.redhat.com [10.36.112.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id 202A65C1BB; Fri, 8 Nov 2019 15:02:34 +0000 (UTC) From: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= To: qemu-devel@nongnu.org Subject: [PATCH v6 05/25] QmpSession: add json parser and use it in qga Date: Fri, 8 Nov 2019 19:01:03 +0400 Message-Id: <20191108150123.12213-6-marcandre.lureau@redhat.com> In-Reply-To: <20191108150123.12213-1-marcandre.lureau@redhat.com> References: <20191108150123.12213-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-MC-Unique: LzGJj93WOm6s5V5vro0rcg-1 X-Mimecast-Spam-Score: 0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , armbru@redhat.com, kraxel@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Move JSON parser to QmpSession, and implement a simple handler to check the parsed tokens and call qmp_dispatch(). This is enough for a simple QMP client, like QGA. The QEMU monitor has more complicated handling of dispatching which will be addressed in a following patch to benefit from more common code. Signed-off-by: Marc-André Lureau --- include/qapi/qmp/dispatch.h | 7 +++++++ qapi/qmp-dispatch.c | 19 +++++++++++++++++++ qga/main.c | 31 +------------------------------ 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/include/qapi/qmp/dispatch.h b/include/qapi/qmp/dispatch.h index d1ce631a93..c84edff7d2 100644 --- a/include/qapi/qmp/dispatch.h +++ b/include/qapi/qmp/dispatch.h @@ -15,6 +15,7 @@ #define QAPI_QMP_DISPATCH_H #include "qemu/queue.h" +#include "qapi/qmp/json-parser.h" typedef void (QmpCommandFunc)(QDict *, QObject **, Error **); @@ -42,6 +43,7 @@ typedef void (QmpDispatchReturn) (QmpSession *session, QDict *rsp); struct QmpSession { const QmpCommandList *cmds; + JSONMessageParser parser; QmpDispatchReturn *return_cb; }; @@ -52,6 +54,11 @@ const QmpCommand *qmp_find_command(const QmpCommandList *cmds, void qmp_session_init(QmpSession *session, const QmpCommandList *cmds, QmpDispatchReturn *return_cb); +static inline void +qmp_session_feed(QmpSession *session, const char *buf, size_t count) +{ + json_message_parser_feed(&session->parser, buf, count); +} void qmp_session_destroy(QmpSession *session); void qmp_disable_command(QmpCommandList *cmds, const char *name); void qmp_enable_command(QmpCommandList *cmds, const char *name); diff --git a/qapi/qmp-dispatch.c b/qapi/qmp-dispatch.c index 1314f4929f..f8c491924d 100644 --- a/qapi/qmp-dispatch.c +++ b/qapi/qmp-dispatch.c @@ -164,6 +164,23 @@ bool qmp_is_oob(const QDict *dict) && !qdict_haskey(dict, "execute"); } +static void qmp_json_emit(void *opaque, QObject *obj, Error *err) +{ + QmpSession *session = opaque; + + assert(!obj != !err); + + if (err) { + QDict *rsp = qmp_error_response(err); + session->return_cb(session, rsp); + qobject_unref(rsp); + } else { + qmp_dispatch(session, obj, false); + } + + qobject_unref(obj); +} + void qmp_session_init(QmpSession *session, const QmpCommandList *cmds, QmpDispatchReturn *return_cb) @@ -171,6 +188,7 @@ void qmp_session_init(QmpSession *session, assert(return_cb); assert(!session->return_cb); + json_message_parser_init(&session->parser, qmp_json_emit, session, NULL); session->cmds = cmds; session->return_cb = return_cb; } @@ -183,6 +201,7 @@ void qmp_session_destroy(QmpSession *session) session->cmds = NULL; session->return_cb = NULL; + json_message_parser_destroy(&session->parser); } void qmp_dispatch(QmpSession *session, QObject *request, bool allow_oob) diff --git a/qga/main.c b/qga/main.c index c291d06491..057368eb16 100644 --- a/qga/main.c +++ b/qga/main.c @@ -19,7 +19,6 @@ #include #endif #include "qemu-common.h" -#include "qapi/qmp/json-parser.h" #include "qapi/qmp/qdict.h" #include "qapi/qmp/qjson.h" #include "qapi/qmp/qstring.h" @@ -75,7 +74,6 @@ typedef struct GAConfig GAConfig; struct GAState { QmpSession session; - JSONMessageParser parser; GMainLoop *main_loop; GAChannel *channel; bool virtio; /* fastpath to check for virtio to deal with poll() quirks */ @@ -567,31 +565,6 @@ static void dispatch_return_cb(QmpSession *session, QDict *rsp) } } -/* handle requests/control events coming in over the channel */ -static void process_event(void *opaque, QObject *obj, Error *err) -{ - GAState *s = opaque; - int ret; - - g_debug("process_event: called"); - assert(!obj != !err); - - if (err) { - QDict *rsp = qmp_error_response(err); - - ret = send_response(s, rsp); - if (ret < 0) { - g_warning("error sending error response: %s", strerror(-ret)); - } - qobject_unref(rsp); - } else { - g_debug("processing command"); - qmp_dispatch(&s->session, obj, false); - } - - qobject_unref(obj); -} - /* false return signals GAChannel to close the current client connection */ static gboolean channel_event_cb(GIOCondition condition, gpointer data) { @@ -607,7 +580,7 @@ static gboolean channel_event_cb(GIOCondition condition, gpointer data) case G_IO_STATUS_NORMAL: buf[count] = 0; g_debug("read data, count: %d, data: %s", (int)count, buf); - json_message_parser_feed(&s->parser, (char *)buf, (int)count); + qmp_session_feed(&s->session, (char *)buf, (int)count); break; case G_IO_STATUS_EOF: g_debug("received EOF"); @@ -1346,7 +1319,6 @@ static GAState *initialize_agent(GAConfig *config, int socket_activation) s->command_state = ga_command_state_new(); ga_command_state_init(s, s->command_state); ga_command_state_init_all(s->command_state); - json_message_parser_init(&s->parser, process_event, s, NULL); qmp_session_init(&s->session, &ga_commands, dispatch_return_cb); #ifndef _WIN32 @@ -1382,7 +1354,6 @@ static void cleanup_agent(GAState *s) qmp_session_destroy(&s->session); ga_command_state_cleanup_all(s->command_state); ga_command_state_free(s->command_state); - json_message_parser_destroy(&s->parser); } g_free(s->pstate_filepath); g_free(s->state_filepath_isfrozen);