]> git.proxmox.com Git - mirror_qemu.git/blame - qapi/qmp-dispatch.c
Merge tag 'pull-aspeed-20240201' of https://github.com/legoater/qemu into staging
[mirror_qemu.git] / qapi / qmp-dispatch.c
CommitLineData
ab02ab2a
MR
1/*
2 * Core Definitions for QAPI/QMP Dispatch
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 *
12 */
13
cbf21151 14#include "qemu/osdep.h"
9ce44e2c
KW
15
16#include "block/aio.h"
6dd75472 17#include "qapi/compat-policy.h"
da34e65c 18#include "qapi/error.h"
7b1b5d19 19#include "qapi/qmp/dispatch.h"
452fcdbc 20#include "qapi/qmp/qdict.h"
c7eb39cb 21#include "qapi/qmp/qjson.h"
db291641 22#include "qapi/qobject-input-visitor.h"
91fa93e5 23#include "qapi/qobject-output-visitor.h"
cf869d53 24#include "qapi/qmp/qbool.h"
9ce44e2c
KW
25#include "qemu/coroutine.h"
26#include "qemu/main-loop.h"
ab02ab2a 27
db291641
MA
28Visitor *qobject_input_visitor_new_qmp(QObject *obj)
29{
30 Visitor *v = qobject_input_visitor_new(obj);
31
ed29bb28 32 visit_set_policy(v, &compat_policy);
db291641
MA
33 return v;
34}
35
91fa93e5
MA
36Visitor *qobject_output_visitor_new_qmp(QObject **result)
37{
38 Visitor *v = qobject_output_visitor_new(result);
39
ed29bb28 40 visit_set_policy(v, &compat_policy);
91fa93e5
MA
41 return v;
42}
43
a62c6174 44static QDict *qmp_dispatch_check_obj(QDict *dict, bool allow_oob,
69240fe6 45 Error **errp)
ab02ab2a 46{
00ecec15 47 const char *exec_key = NULL;
ab02ab2a
MR
48 const QDictEntry *ent;
49 const char *arg_name;
50 const QObject *arg_obj;
ab02ab2a 51
ab02ab2a
MR
52 for (ent = qdict_first(dict); ent;
53 ent = qdict_next(dict, ent)) {
54 arg_name = qdict_entry_key(ent);
55 arg_obj = qdict_entry_value(ent);
56
00ecec15
MA
57 if (!strcmp(arg_name, "execute")
58 || (!strcmp(arg_name, "exec-oob") && allow_oob)) {
ab02ab2a 59 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
00ecec15
MA
60 error_setg(errp, "QMP input member '%s' must be a string",
61 arg_name);
ab02ab2a
MR
62 return NULL;
63 }
00ecec15
MA
64 if (exec_key) {
65 error_setg(errp, "QMP input member '%s' clashes with '%s'",
66 arg_name, exec_key);
74d8c9d9
MA
67 return NULL;
68 }
00ecec15
MA
69 exec_key = arg_name;
70 } else if (!strcmp(arg_name, "arguments")) {
cf869d53
PX
71 if (qobject_type(arg_obj) != QTYPE_QDICT) {
72 error_setg(errp,
00ecec15 73 "QMP input member 'arguments' must be an object");
cf869d53
PX
74 return NULL;
75 }
4eaca8de
MAL
76 } else if (!strcmp(arg_name, "id")) {
77 continue;
74d8c9d9 78 } else {
10e37839 79 error_setg(errp, "QMP input member '%s' is unexpected",
99fb0c53 80 arg_name);
ab02ab2a
MR
81 return NULL;
82 }
83 }
84
00ecec15 85 if (!exec_key) {
10e37839 86 error_setg(errp, "QMP input lacks member 'execute'");
ab02ab2a
MR
87 return NULL;
88 }
89
90 return dict;
91}
92
cf4a0643
MA
93QDict *qmp_error_response(Error *err)
94{
95 QDict *rsp;
96
97 rsp = qdict_from_jsonf_nofail("{ 'error': { 'class': %s, 'desc': %s } }",
98 QapiErrorClass_str(error_get_class(err)),
99 error_get_pretty(err));
100 error_free(err);
101 return rsp;
102}
103
104/*
105 * Does @qdict look like a command to be run out-of-band?
106 */
107bool qmp_is_oob(const QDict *dict)
ab02ab2a 108{
cf4a0643
MA
109 return qdict_haskey(dict, "exec-oob")
110 && !qdict_haskey(dict, "execute");
111}
112
9ce44e2c
KW
113typedef struct QmpDispatchBH {
114 const QmpCommand *cmd;
115 Monitor *cur_mon;
116 QDict *args;
117 QObject **ret;
118 Error **errp;
119 Coroutine *co;
120} QmpDispatchBH;
121
122static void do_qmp_dispatch_bh(void *opaque)
123{
124 QmpDispatchBH *data = opaque;
125
126 assert(monitor_cur() == NULL);
127 monitor_set_cur(qemu_coroutine_self(), data->cur_mon);
128 data->cmd->fn(data->args, data->ret, data->errp);
129 monitor_set_cur(qemu_coroutine_self(), NULL);
130 aio_co_wake(data->co);
131}
132
133/*
134 * Runs outside of coroutine context for OOB commands, but in coroutine
135 * context for everything else.
136 */
a50c99bc
PB
137QDict *coroutine_mixed_fn qmp_dispatch(const QmpCommandList *cmds, QObject *request,
138 bool allow_oob, Monitor *cur_mon)
cf4a0643
MA
139{
140 Error *err = NULL;
69240fe6 141 bool oob;
ab02ab2a 142 const char *command;
cf4a0643 143 QDict *args;
f0ccc00b 144 const QmpCommand *cmd;
a62c6174
MA
145 QDict *dict;
146 QObject *id;
ab02ab2a 147 QObject *ret = NULL;
d3226035 148 QDict *rsp = NULL;
ab02ab2a 149
a62c6174 150 dict = qobject_to(QDict, request);
4af8be1f 151 if (!dict) {
a62c6174
MA
152 id = NULL;
153 error_setg(&err, "QMP input must be a JSON object");
154 goto out;
155 }
156
157 id = qdict_get(dict, "id");
158
159 if (!qmp_dispatch_check_obj(dict, allow_oob, &err)) {
cf4a0643 160 goto out;
ab02ab2a
MR
161 }
162
00ecec15 163 command = qdict_get_try_str(dict, "execute");
69240fe6 164 oob = false;
00ecec15
MA
165 if (!command) {
166 assert(allow_oob);
167 command = qdict_get_str(dict, "exec-oob");
69240fe6 168 oob = true;
00ecec15 169 }
1527badb 170 cmd = qmp_find_command(cmds, command);
ab02ab2a 171 if (cmd == NULL) {
cf4a0643 172 error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND,
a6c90cbc 173 "The command %s has not been found", command);
cf4a0643 174 goto out;
ab02ab2a 175 }
7ce5fc63
MA
176 if (!compat_policy_input_ok(cmd->special_features, &compat_policy,
177 ERROR_CLASS_COMMAND_NOT_FOUND,
178 "command", command, &err)) {
179 goto out;
d2032598 180 }
abd6cf6d 181 if (!cmd->enabled) {
cf4a0643 182 error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND,
c98939da
MAL
183 "Command %s has been disabled%s%s",
184 command,
185 cmd->disable_reason ? ": " : "",
186 cmd->disable_reason ?: "");
cf4a0643 187 goto out;
abd6cf6d 188 }
69240fe6 189 if (oob && !(cmd->options & QCO_ALLOW_OOB)) {
cf4a0643 190 error_setg(&err, "The command %s does not support OOB",
69240fe6 191 command);
cf4a0643 192 goto out;
69240fe6 193 }
ab02ab2a 194
164dafd1 195 if (!qmp_command_available(cmd, &err)) {
cf4a0643 196 goto out;
047f7038
IM
197 }
198
ab02ab2a
MR
199 if (!qdict_haskey(dict, "arguments")) {
200 args = qdict_new();
201 } else {
202 args = qdict_get_qdict(dict, "arguments");
cb3e7f08 203 qobject_ref(args);
ab02ab2a 204 }
41725fa7 205
9ce44e2c 206 assert(!(oob && qemu_in_coroutine()));
41725fa7 207 assert(monitor_cur() == NULL);
9ce44e2c 208 if (!!(cmd->options & QCO_COROUTINE) == qemu_in_coroutine()) {
effd60c8
SH
209 if (qemu_in_coroutine()) {
210 /*
211 * Move the coroutine from iohandler_ctx to qemu_aio_context for
212 * executing the command handler so that it can make progress if it
213 * involves an AIO_WAIT_WHILE().
214 */
215 aio_co_schedule(qemu_get_aio_context(), qemu_coroutine_self());
216 qemu_coroutine_yield();
217 }
218
9ce44e2c
KW
219 monitor_set_cur(qemu_coroutine_self(), cur_mon);
220 cmd->fn(args, &ret, &err);
221 monitor_set_cur(qemu_coroutine_self(), NULL);
effd60c8
SH
222
223 if (qemu_in_coroutine()) {
224 /*
225 * Yield and reschedule so the main loop stays responsive.
226 *
227 * Move back to iohandler_ctx so that nested event loops for
228 * qemu_aio_context don't start new monitor commands.
229 */
230 aio_co_schedule(iohandler_get_aio_context(),
231 qemu_coroutine_self());
232 qemu_coroutine_yield();
233 }
9ce44e2c
KW
234 } else {
235 /*
236 * Actual context doesn't match the one the command needs.
237 *
238 * Case 1: we are in coroutine context, but command does not
239 * have QCO_COROUTINE. We need to drop out of coroutine
240 * context for executing it.
241 *
242 * Case 2: we are outside coroutine context, but command has
243 * QCO_COROUTINE. Can't actually happen, because we get here
244 * outside coroutine context only when executing a command
245 * out of band, and OOB commands never have QCO_COROUTINE.
246 */
247 assert(!oob && qemu_in_coroutine() && !(cmd->options & QCO_COROUTINE));
248
249 QmpDispatchBH data = {
250 .cur_mon = cur_mon,
251 .cmd = cmd,
252 .args = args,
253 .ret = &ret,
254 .errp = &err,
255 .co = qemu_coroutine_self(),
256 };
effd60c8 257 aio_bh_schedule_oneshot(iohandler_get_aio_context(), do_qmp_dispatch_bh,
9ce44e2c
KW
258 &data);
259 qemu_coroutine_yield();
260 }
d3226035 261 qobject_unref(args);
cf4a0643 262 if (err) {
b3fbb328
MAL
263 /* or assert(!ret) after reviewing all handlers: */
264 qobject_unref(ret);
d3226035
MA
265 goto out;
266 }
267
268 if (cmd->options & QCO_NO_SUCCESS_RESP) {
42a502a7 269 g_assert(!ret);
d3226035 270 return NULL;
42a502a7 271 } else if (!ret) {
4a883738
MA
272 /*
273 * When the command's schema has no 'returns', cmd->fn()
274 * leaves @ret null. The QMP spec calls for an empty object
275 * then; supply it.
276 */
42a502a7 277 ret = QOBJECT(qdict_new());
ab02ab2a
MR
278 }
279
d3226035
MA
280 rsp = qdict_new();
281 qdict_put_obj(rsp, "return", ret);
ab02ab2a 282
cf4a0643 283out:
ab02ab2a 284 if (err) {
d3226035 285 assert(!rsp);
cee32796 286 rsp = qmp_error_response(err);
ab02ab2a
MR
287 }
288
d3226035
MA
289 assert(rsp);
290
291 if (id) {
4eaca8de
MAL
292 qdict_put_obj(rsp, "id", qobject_ref(id));
293 }
294
d43b1694 295 return rsp;
ab02ab2a 296}