[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 10/14] hmp: Add support for coroutine command handlers
From: |
Markus Armbruster |
Subject: |
[PULL 10/14] hmp: Add support for coroutine command handlers |
Date: |
Fri, 9 Oct 2020 08:34:28 +0200 |
From: Kevin Wolf <kwolf@redhat.com>
Often, QMP command handlers are not only called to handle QMP commands,
but also from a corresponding HMP command handler. In order to give them
a consistent environment, optionally run HMP command handlers in a
coroutine, too.
The implementation is a lot simpler than in QMP because for HMP, we
still block the VM while the coroutine is running.
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Message-Id: <20201005155855.256490-11-kwolf@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
docs/devel/qapi-code-gen.txt | 4 ++--
monitor/monitor-internal.h | 1 +
monitor/hmp.c | 37 +++++++++++++++++++++++++++++++-----
3 files changed, 35 insertions(+), 7 deletions(-)
diff --git a/docs/devel/qapi-code-gen.txt b/docs/devel/qapi-code-gen.txt
index 4a41e36a75..c6438c6aa9 100644
--- a/docs/devel/qapi-code-gen.txt
+++ b/docs/devel/qapi-code-gen.txt
@@ -617,8 +617,8 @@ pitfalls are:
Since the command handler may assume coroutine context, any callers
other than the QMP dispatcher must also call it in coroutine context.
-In particular, HMP commands calling such a QMP command handler must
-enter coroutine context before calling the handler.
+In particular, HMP commands calling such a QMP command handler must be
+marked .coroutine = true in hmp-commands.hx.
It is an error to specify both 'coroutine': true and 'allow-oob': true
for a command. We don't currently have a use case for both together and
diff --git a/monitor/monitor-internal.h b/monitor/monitor-internal.h
index b55d6df07f..ad2e64be13 100644
--- a/monitor/monitor-internal.h
+++ b/monitor/monitor-internal.h
@@ -74,6 +74,7 @@ typedef struct HMPCommand {
const char *help;
const char *flags; /* p=preconfig */
void (*cmd)(Monitor *mon, const QDict *qdict);
+ bool coroutine;
/*
* @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
diff --git a/monitor/hmp.c b/monitor/hmp.c
index abaf939b2d..c5cd9d372b 100644
--- a/monitor/hmp.c
+++ b/monitor/hmp.c
@@ -1056,12 +1056,26 @@ fail:
return NULL;
}
+typedef struct HandleHmpCommandCo {
+ Monitor *mon;
+ const HMPCommand *cmd;
+ QDict *qdict;
+ bool done;
+} HandleHmpCommandCo;
+
+static void handle_hmp_command_co(void *opaque)
+{
+ HandleHmpCommandCo *data = opaque;
+ data->cmd->cmd(data->mon, data->qdict);
+ monitor_set_cur(qemu_coroutine_self(), NULL);
+ data->done = true;
+}
+
void handle_hmp_command(MonitorHMP *mon, const char *cmdline)
{
QDict *qdict;
const HMPCommand *cmd;
const char *cmd_start = cmdline;
- Monitor *old_mon;
trace_handle_hmp_command(mon, cmdline);
@@ -1080,10 +1094,23 @@ void handle_hmp_command(MonitorHMP *mon, const char
*cmdline)
return;
}
- /* old_mon is non-NULL when called from qmp_human_monitor_command() */
- old_mon = monitor_set_cur(qemu_coroutine_self(), &mon->common);
- cmd->cmd(&mon->common, qdict);
- monitor_set_cur(qemu_coroutine_self(), old_mon);
+ if (!cmd->coroutine) {
+ /* old_mon is non-NULL when called from qmp_human_monitor_command() */
+ Monitor *old_mon = monitor_set_cur(qemu_coroutine_self(),
&mon->common);
+ cmd->cmd(&mon->common, qdict);
+ monitor_set_cur(qemu_coroutine_self(), old_mon);
+ } else {
+ HandleHmpCommandCo data = {
+ .mon = &mon->common,
+ .cmd = cmd,
+ .qdict = qdict,
+ .done = false,
+ };
+ Coroutine *co = qemu_coroutine_create(handle_hmp_command_co, &data);
+ monitor_set_cur(co, &mon->common);
+ aio_co_enter(qemu_get_aio_context(), co);
+ AIO_WAIT_WHILE(qemu_get_aio_context(), !data.done);
+ }
qobject_unref(qdict);
}
--
2.26.2
- Re: [PULL 09/14] qmp: Move dispatcher to a coroutine, (continued)
Re: [PULL 09/14] qmp: Move dispatcher to a coroutine, Volker Rümelin, 2020/10/17
[PULL 07/14] monitor: Make current monitor a per-coroutine property, Markus Armbruster, 2020/10/09
[PULL 03/14] monitor: Use getter/setter functions for cur_mon, Markus Armbruster, 2020/10/09
[PULL 08/14] qapi: Add a 'coroutine' flag for commands, Markus Armbruster, 2020/10/09
[PULL 11/14] util/async: Add aio_co_reschedule_self(), Markus Armbruster, 2020/10/09
[PULL 10/14] hmp: Add support for coroutine command handlers,
Markus Armbruster <=
[PULL 13/14] block: Add bdrv_lock()/unlock(), Markus Armbruster, 2020/10/09
Re: [PULL 00/14] Monitor patches for 2020-10-09, Peter Maydell, 2020/10/09