17e3c0deaSKevin Wolf /*
27e3c0deaSKevin Wolf * QEMU monitor
37e3c0deaSKevin Wolf *
47e3c0deaSKevin Wolf * Copyright (c) 2003-2004 Fabrice Bellard
57e3c0deaSKevin Wolf *
67e3c0deaSKevin Wolf * Permission is hereby granted, free of charge, to any person obtaining a copy
77e3c0deaSKevin Wolf * of this software and associated documentation files (the "Software"), to deal
87e3c0deaSKevin Wolf * in the Software without restriction, including without limitation the rights
97e3c0deaSKevin Wolf * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
107e3c0deaSKevin Wolf * copies of the Software, and to permit persons to whom the Software is
117e3c0deaSKevin Wolf * furnished to do so, subject to the following conditions:
127e3c0deaSKevin Wolf *
137e3c0deaSKevin Wolf * The above copyright notice and this permission notice shall be included in
147e3c0deaSKevin Wolf * all copies or substantial portions of the Software.
157e3c0deaSKevin Wolf *
167e3c0deaSKevin Wolf * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
177e3c0deaSKevin Wolf * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
187e3c0deaSKevin Wolf * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
197e3c0deaSKevin Wolf * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
207e3c0deaSKevin Wolf * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
217e3c0deaSKevin Wolf * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
227e3c0deaSKevin Wolf * THE SOFTWARE.
237e3c0deaSKevin Wolf */
247e3c0deaSKevin Wolf
257e3c0deaSKevin Wolf #include "qemu/osdep.h"
267e3c0deaSKevin Wolf
277e3c0deaSKevin Wolf #include "chardev/char-io.h"
287e3c0deaSKevin Wolf #include "monitor-internal.h"
297e3c0deaSKevin Wolf #include "qapi/error.h"
30fa4dcf57SKevin Wolf #include "qapi/qapi-commands-control.h"
31407bc4bfSDaniel P. Berrangé #include "qobject/qdict.h"
32407bc4bfSDaniel P. Berrangé #include "qobject/qjson.h"
33407bc4bfSDaniel P. Berrangé #include "qobject/qlist.h"
347e3c0deaSKevin Wolf #include "trace.h"
357e3c0deaSKevin Wolf
369f2d5854SPaolo Bonzini /*
379f2d5854SPaolo Bonzini * qmp_dispatcher_co_busy is used for synchronisation between the
389f2d5854SPaolo Bonzini * monitor thread and the main thread to ensure that the dispatcher
399f2d5854SPaolo Bonzini * coroutine never gets scheduled a second time when it's already
409f2d5854SPaolo Bonzini * scheduled (scheduling the same coroutine twice is forbidden).
419f2d5854SPaolo Bonzini *
42eea7cd3fSPaolo Bonzini * It is true if the coroutine will process at least one more request
43eea7cd3fSPaolo Bonzini * before going to sleep. Either it has been kicked already, or it
44eea7cd3fSPaolo Bonzini * is active and processing requests. Additional requests may therefore
45eea7cd3fSPaolo Bonzini * be pushed onto mon->qmp_requests, and @qmp_dispatcher_co_shutdown may
46eea7cd3fSPaolo Bonzini * be set without further ado. @qmp_dispatcher_co must not be woken up
47eea7cd3fSPaolo Bonzini * in this case.
489f2d5854SPaolo Bonzini *
49eea7cd3fSPaolo Bonzini * If false, you have to wake up @qmp_dispatcher_co after pushing new
50eea7cd3fSPaolo Bonzini * requests. You also have to set @qmp_dispatcher_co_busy to true
51eea7cd3fSPaolo Bonzini * before waking up the coroutine.
529f2d5854SPaolo Bonzini *
539f2d5854SPaolo Bonzini * The coroutine will automatically change this variable back to false
549f2d5854SPaolo Bonzini * before it yields. Nobody else may set the variable to false.
559f2d5854SPaolo Bonzini *
569f2d5854SPaolo Bonzini * Access must be atomic for thread safety.
579f2d5854SPaolo Bonzini */
589f2d5854SPaolo Bonzini static bool qmp_dispatcher_co_busy = true;
599f2d5854SPaolo Bonzini
607e3c0deaSKevin Wolf struct QMPRequest {
617e3c0deaSKevin Wolf /* Owner of the request */
627e3c0deaSKevin Wolf MonitorQMP *mon;
637e3c0deaSKevin Wolf /*
647e3c0deaSKevin Wolf * Request object to be handled or Error to be reported
657e3c0deaSKevin Wolf * (exactly one of them is non-null)
667e3c0deaSKevin Wolf */
677e3c0deaSKevin Wolf QObject *req;
687e3c0deaSKevin Wolf Error *err;
697e3c0deaSKevin Wolf };
707e3c0deaSKevin Wolf typedef struct QMPRequest QMPRequest;
717e3c0deaSKevin Wolf
727e3c0deaSKevin Wolf QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
737e3c0deaSKevin Wolf
qmp_oob_enabled(MonitorQMP * mon)747e3c0deaSKevin Wolf static bool qmp_oob_enabled(MonitorQMP *mon)
757e3c0deaSKevin Wolf {
767e3c0deaSKevin Wolf return mon->capab[QMP_CAPABILITY_OOB];
777e3c0deaSKevin Wolf }
787e3c0deaSKevin Wolf
monitor_qmp_caps_reset(MonitorQMP * mon)797e3c0deaSKevin Wolf static void monitor_qmp_caps_reset(MonitorQMP *mon)
807e3c0deaSKevin Wolf {
817e3c0deaSKevin Wolf memset(mon->capab_offered, 0, sizeof(mon->capab_offered));
827e3c0deaSKevin Wolf memset(mon->capab, 0, sizeof(mon->capab));
837e3c0deaSKevin Wolf mon->capab_offered[QMP_CAPABILITY_OOB] = mon->common.use_io_thread;
847e3c0deaSKevin Wolf }
857e3c0deaSKevin Wolf
qmp_request_free(QMPRequest * req)867e3c0deaSKevin Wolf static void qmp_request_free(QMPRequest *req)
877e3c0deaSKevin Wolf {
887e3c0deaSKevin Wolf qobject_unref(req->req);
897e3c0deaSKevin Wolf error_free(req->err);
907e3c0deaSKevin Wolf g_free(req);
917e3c0deaSKevin Wolf }
927e3c0deaSKevin Wolf
937e3c0deaSKevin Wolf /* Caller must hold mon->qmp.qmp_queue_lock */
monitor_qmp_cleanup_req_queue_locked(MonitorQMP * mon)947e3c0deaSKevin Wolf static void monitor_qmp_cleanup_req_queue_locked(MonitorQMP *mon)
957e3c0deaSKevin Wolf {
967e3c0deaSKevin Wolf while (!g_queue_is_empty(mon->qmp_requests)) {
977e3c0deaSKevin Wolf qmp_request_free(g_queue_pop_head(mon->qmp_requests));
987e3c0deaSKevin Wolf }
997e3c0deaSKevin Wolf }
1007e3c0deaSKevin Wolf
monitor_qmp_cleanup_queue_and_resume(MonitorQMP * mon)1012895aaa1SWolfgang Bumiller static void monitor_qmp_cleanup_queue_and_resume(MonitorQMP *mon)
1027e3c0deaSKevin Wolf {
103a8e2ab5dSMahmoud Mandour QEMU_LOCK_GUARD(&mon->qmp_queue_lock);
1042895aaa1SWolfgang Bumiller
1052895aaa1SWolfgang Bumiller /*
106395a9508SMarkus Armbruster * Same condition as in monitor_qmp_dispatcher_co(), but before
1072895aaa1SWolfgang Bumiller * removing an element from the queue (hence no `- 1`).
1082895aaa1SWolfgang Bumiller * Also, the queue should not be empty either, otherwise the
1092895aaa1SWolfgang Bumiller * monitor hasn't been suspended yet (or was already resumed).
1102895aaa1SWolfgang Bumiller */
1112895aaa1SWolfgang Bumiller bool need_resume = (!qmp_oob_enabled(mon) ||
1122895aaa1SWolfgang Bumiller mon->qmp_requests->length == QMP_REQ_QUEUE_LEN_MAX)
1132895aaa1SWolfgang Bumiller && !g_queue_is_empty(mon->qmp_requests);
1142895aaa1SWolfgang Bumiller
1157e3c0deaSKevin Wolf monitor_qmp_cleanup_req_queue_locked(mon);
1162895aaa1SWolfgang Bumiller
1172895aaa1SWolfgang Bumiller if (need_resume) {
1182895aaa1SWolfgang Bumiller /*
1192895aaa1SWolfgang Bumiller * handle_qmp_command() suspended the monitor because the
1202895aaa1SWolfgang Bumiller * request queue filled up, to be resumed when the queue has
1212895aaa1SWolfgang Bumiller * space again. We just emptied it; resume the monitor.
1222895aaa1SWolfgang Bumiller *
1232895aaa1SWolfgang Bumiller * Without this, the monitor would remain suspended forever
1242895aaa1SWolfgang Bumiller * when we get here while the monitor is suspended. An
1252895aaa1SWolfgang Bumiller * unfortunately timed CHR_EVENT_CLOSED can do the trick.
1262895aaa1SWolfgang Bumiller */
1272895aaa1SWolfgang Bumiller monitor_resume(&mon->common);
1282895aaa1SWolfgang Bumiller }
1292895aaa1SWolfgang Bumiller
1307e3c0deaSKevin Wolf }
1317e3c0deaSKevin Wolf
qmp_send_response(MonitorQMP * mon,const QDict * rsp)1327e3c0deaSKevin Wolf void qmp_send_response(MonitorQMP *mon, const QDict *rsp)
1337e3c0deaSKevin Wolf {
1347e3c0deaSKevin Wolf const QObject *data = QOBJECT(rsp);
135eab3a467SMarkus Armbruster GString *json;
1367e3c0deaSKevin Wolf
1376589f459SMarkus Armbruster json = qobject_to_json_pretty(data, mon->pretty);
1387e3c0deaSKevin Wolf assert(json != NULL);
139f680405fSMarkus Armbruster trace_monitor_qmp_respond(mon, json->str);
1407e3c0deaSKevin Wolf
141eab3a467SMarkus Armbruster g_string_append_c(json, '\n');
142eab3a467SMarkus Armbruster monitor_puts(&mon->common, json->str);
1437e3c0deaSKevin Wolf
144eab3a467SMarkus Armbruster g_string_free(json, true);
1457e3c0deaSKevin Wolf }
1467e3c0deaSKevin Wolf
1477e3c0deaSKevin Wolf /*
148eb707eacSMarkus Armbruster * Emit QMP response @rsp to @mon.
1497e3c0deaSKevin Wolf * Null @rsp can only happen for commands with QCO_NO_SUCCESS_RESP.
1507e3c0deaSKevin Wolf * Nothing is emitted then.
1517e3c0deaSKevin Wolf */
monitor_qmp_respond(MonitorQMP * mon,QDict * rsp)1527e3c0deaSKevin Wolf static void monitor_qmp_respond(MonitorQMP *mon, QDict *rsp)
1537e3c0deaSKevin Wolf {
1547e3c0deaSKevin Wolf if (rsp) {
1557e3c0deaSKevin Wolf qmp_send_response(mon, rsp);
1567e3c0deaSKevin Wolf }
1577e3c0deaSKevin Wolf }
1587e3c0deaSKevin Wolf
1599ce44e2cSKevin Wolf /*
1609ce44e2cSKevin Wolf * Runs outside of coroutine context for OOB commands, but in
1619ce44e2cSKevin Wolf * coroutine context for everything else.
1629ce44e2cSKevin Wolf */
monitor_qmp_dispatch(MonitorQMP * mon,QObject * req)1637e3c0deaSKevin Wolf static void monitor_qmp_dispatch(MonitorQMP *mon, QObject *req)
1647e3c0deaSKevin Wolf {
1657e3c0deaSKevin Wolf QDict *rsp;
1667e3c0deaSKevin Wolf QDict *error;
1677e3c0deaSKevin Wolf
16841725fa7SKevin Wolf rsp = qmp_dispatch(mon->commands, req, qmp_oob_enabled(mon),
16941725fa7SKevin Wolf &mon->common);
1707e3c0deaSKevin Wolf
1717e3c0deaSKevin Wolf if (mon->commands == &qmp_cap_negotiation_commands) {
1727e3c0deaSKevin Wolf error = qdict_get_qdict(rsp, "error");
1737e3c0deaSKevin Wolf if (error
1747e3c0deaSKevin Wolf && !g_strcmp0(qdict_get_try_str(error, "class"),
1757e3c0deaSKevin Wolf QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND))) {
1767e3c0deaSKevin Wolf /* Provide a more useful error message */
1777e3c0deaSKevin Wolf qdict_del(error, "desc");
1787e3c0deaSKevin Wolf qdict_put_str(error, "desc", "Expecting capabilities negotiation"
1797e3c0deaSKevin Wolf " with 'qmp_capabilities'");
1807e3c0deaSKevin Wolf }
1817e3c0deaSKevin Wolf }
1827e3c0deaSKevin Wolf
1837e3c0deaSKevin Wolf monitor_qmp_respond(mon, rsp);
1847e3c0deaSKevin Wolf qobject_unref(rsp);
1857e3c0deaSKevin Wolf }
1867e3c0deaSKevin Wolf
1877e3c0deaSKevin Wolf /*
1887e3c0deaSKevin Wolf * Pop a QMP request from a monitor request queue.
1897e3c0deaSKevin Wolf * Return the request, or NULL all request queues are empty.
1907e3c0deaSKevin Wolf * We are using round-robin fashion to pop the request, to avoid
1917e3c0deaSKevin Wolf * processing commands only on a very busy monitor. To achieve that,
1927e3c0deaSKevin Wolf * when we process one request on a specific monitor, we put that
1937e3c0deaSKevin Wolf * monitor to the end of mon_list queue.
1947e3c0deaSKevin Wolf *
1957e3c0deaSKevin Wolf * Note: if the function returned with non-NULL, then the caller will
1967e3c0deaSKevin Wolf * be with qmp_mon->qmp_queue_lock held, and the caller is responsible
1977e3c0deaSKevin Wolf * to release it.
1987e3c0deaSKevin Wolf */
monitor_qmp_requests_pop_any_with_lock(void)1997e3c0deaSKevin Wolf static QMPRequest *monitor_qmp_requests_pop_any_with_lock(void)
2007e3c0deaSKevin Wolf {
2017e3c0deaSKevin Wolf QMPRequest *req_obj = NULL;
2027e3c0deaSKevin Wolf Monitor *mon;
2037e3c0deaSKevin Wolf MonitorQMP *qmp_mon;
2047e3c0deaSKevin Wolf
2057e3c0deaSKevin Wolf QTAILQ_FOREACH(mon, &mon_list, entry) {
2067e3c0deaSKevin Wolf if (!monitor_is_qmp(mon)) {
2077e3c0deaSKevin Wolf continue;
2087e3c0deaSKevin Wolf }
2097e3c0deaSKevin Wolf
2107e3c0deaSKevin Wolf qmp_mon = container_of(mon, MonitorQMP, common);
2117e3c0deaSKevin Wolf qemu_mutex_lock(&qmp_mon->qmp_queue_lock);
2127e3c0deaSKevin Wolf req_obj = g_queue_pop_head(qmp_mon->qmp_requests);
2137e3c0deaSKevin Wolf if (req_obj) {
2147e3c0deaSKevin Wolf /* With the lock of corresponding queue held */
2157e3c0deaSKevin Wolf break;
2167e3c0deaSKevin Wolf }
2177e3c0deaSKevin Wolf qemu_mutex_unlock(&qmp_mon->qmp_queue_lock);
2187e3c0deaSKevin Wolf }
2197e3c0deaSKevin Wolf
2207e3c0deaSKevin Wolf if (req_obj) {
2217e3c0deaSKevin Wolf /*
2227e3c0deaSKevin Wolf * We found one request on the monitor. Degrade this monitor's
2237e3c0deaSKevin Wolf * priority to lowest by re-inserting it to end of queue.
2247e3c0deaSKevin Wolf */
2257e3c0deaSKevin Wolf QTAILQ_REMOVE(&mon_list, mon, entry);
2267e3c0deaSKevin Wolf QTAILQ_INSERT_TAIL(&mon_list, mon, entry);
2277e3c0deaSKevin Wolf }
2287e3c0deaSKevin Wolf
2297e3c0deaSKevin Wolf return req_obj;
2307e3c0deaSKevin Wolf }
2317e3c0deaSKevin Wolf
monitor_qmp_dispatcher_pop_any(void)23260f4f62eSPaolo Bonzini static QMPRequest *monitor_qmp_dispatcher_pop_any(void)
2337e3c0deaSKevin Wolf {
2349ce44e2cSKevin Wolf while (true) {
2350ff25537SPaolo Bonzini /*
236eea7cd3fSPaolo Bonzini * To avoid double scheduling, busy is true on entry to
237eea7cd3fSPaolo Bonzini * monitor_qmp_dispatcher_co(), and must be set again before
238eea7cd3fSPaolo Bonzini * aio_co_wake()-ing it.
2390ff25537SPaolo Bonzini */
240eea7cd3fSPaolo Bonzini assert(qatomic_read(&qmp_dispatcher_co_busy) == true);
2419ce44e2cSKevin Wolf
2429ce44e2cSKevin Wolf /*
2439ce44e2cSKevin Wolf * Mark the dispatcher as not busy already here so that we
2449ce44e2cSKevin Wolf * don't miss any new requests coming in the middle of our
2459ce44e2cSKevin Wolf * processing.
246eea7cd3fSPaolo Bonzini *
247eea7cd3fSPaolo Bonzini * Clear qmp_dispatcher_co_busy before reading request.
2489ce44e2cSKevin Wolf */
24906831001SPaolo Bonzini qatomic_set_mb(&qmp_dispatcher_co_busy, false);
2509ce44e2cSKevin Wolf
2510ff25537SPaolo Bonzini WITH_QEMU_LOCK_GUARD(&monitor_lock) {
25260f4f62eSPaolo Bonzini QMPRequest *req_obj;
25360f4f62eSPaolo Bonzini
254b248e616SKevin Wolf /* On shutdown, don't take any more requests from the queue */
255b248e616SKevin Wolf if (qmp_dispatcher_co_shutdown) {
2560ff25537SPaolo Bonzini return NULL;
257b248e616SKevin Wolf }
258b248e616SKevin Wolf
2590ff25537SPaolo Bonzini req_obj = monitor_qmp_requests_pop_any_with_lock();
26060f4f62eSPaolo Bonzini if (req_obj) {
26160f4f62eSPaolo Bonzini return req_obj;
26260f4f62eSPaolo Bonzini }
2630ff25537SPaolo Bonzini }
2640ff25537SPaolo Bonzini
2659ce44e2cSKevin Wolf /*
2669ce44e2cSKevin Wolf * No more requests to process. Wait to be reentered from
2679ce44e2cSKevin Wolf * handle_qmp_command() when it pushes more requests, or
2689ce44e2cSKevin Wolf * from monitor_cleanup() when it requests shutdown.
2699ce44e2cSKevin Wolf */
2709ce44e2cSKevin Wolf qemu_coroutine_yield();
27160f4f62eSPaolo Bonzini }
2729ce44e2cSKevin Wolf }
2739ce44e2cSKevin Wolf
monitor_qmp_dispatcher_co(void * data)27460f4f62eSPaolo Bonzini void coroutine_fn monitor_qmp_dispatcher_co(void *data)
27560f4f62eSPaolo Bonzini {
27660f4f62eSPaolo Bonzini QMPRequest *req_obj;
27760f4f62eSPaolo Bonzini QDict *rsp;
27860f4f62eSPaolo Bonzini bool oob_enabled;
27960f4f62eSPaolo Bonzini MonitorQMP *mon;
28060f4f62eSPaolo Bonzini
28160f4f62eSPaolo Bonzini while ((req_obj = monitor_qmp_dispatcher_pop_any()) != NULL) {
282f680405fSMarkus Armbruster trace_monitor_qmp_in_band_dequeue(req_obj,
283f680405fSMarkus Armbruster req_obj->mon->qmp_requests->length);
284f680405fSMarkus Armbruster
28588daf099SMarkus Armbruster /*
28688daf099SMarkus Armbruster * @req_obj has a request, we hold req_obj->mon->qmp_queue_lock
28788daf099SMarkus Armbruster */
28888daf099SMarkus Armbruster
2897e3c0deaSKevin Wolf mon = req_obj->mon;
29088daf099SMarkus Armbruster
29188daf099SMarkus Armbruster /*
29288daf099SMarkus Armbruster * We need to resume the monitor if handle_qmp_command()
29388daf099SMarkus Armbruster * suspended it. Two cases:
29488daf099SMarkus Armbruster * 1. OOB enabled: mon->qmp_requests has no more space
29588daf099SMarkus Armbruster * Resume right away, so that OOB commands can get executed while
29688daf099SMarkus Armbruster * this request is being processed.
29788daf099SMarkus Armbruster * 2. OOB disabled: always
29888daf099SMarkus Armbruster * Resume only after we're done processing the request,
29988daf099SMarkus Armbruster * We need to save qmp_oob_enabled() for later, because
30088daf099SMarkus Armbruster * qmp_qmp_capabilities() can change it.
30188daf099SMarkus Armbruster */
30288daf099SMarkus Armbruster oob_enabled = qmp_oob_enabled(mon);
30388daf099SMarkus Armbruster if (oob_enabled
30488daf099SMarkus Armbruster && mon->qmp_requests->length == QMP_REQ_QUEUE_LEN_MAX - 1) {
30588daf099SMarkus Armbruster monitor_resume(&mon->common);
30688daf099SMarkus Armbruster }
30788daf099SMarkus Armbruster
308a67b996eSStefan Reiter /*
309a67b996eSStefan Reiter * Drop the queue mutex now, before yielding, otherwise we might
310a67b996eSStefan Reiter * deadlock if the main thread tries to lock it.
311a67b996eSStefan Reiter */
3127e3c0deaSKevin Wolf qemu_mutex_unlock(&mon->qmp_queue_lock);
31388daf099SMarkus Armbruster
314a67b996eSStefan Reiter if (qatomic_xchg(&qmp_dispatcher_co_busy, true) == true) {
315a67b996eSStefan Reiter /*
316a67b996eSStefan Reiter * Someone rescheduled us (probably because a new requests
317a67b996eSStefan Reiter * came in), but we didn't actually yield. Do that now,
318a67b996eSStefan Reiter * only to be immediately reentered and removed from the
319a67b996eSStefan Reiter * list of scheduled coroutines.
320a67b996eSStefan Reiter */
321a67b996eSStefan Reiter qemu_coroutine_yield();
322a67b996eSStefan Reiter }
323a67b996eSStefan Reiter
32488daf099SMarkus Armbruster /* Process request */
3257e3c0deaSKevin Wolf if (req_obj->req) {
326d403d92dSMarkus Armbruster if (trace_event_get_state(TRACE_MONITOR_QMP_CMD_IN_BAND)) {
3277e3c0deaSKevin Wolf QDict *qdict = qobject_to(QDict, req_obj->req);
3287e3c0deaSKevin Wolf QObject *id = qdict ? qdict_get(qdict, "id") : NULL;
329d403d92dSMarkus Armbruster GString *id_json;
330d403d92dSMarkus Armbruster
331d403d92dSMarkus Armbruster id_json = id ? qobject_to_json(id) : g_string_new(NULL);
332d403d92dSMarkus Armbruster trace_monitor_qmp_cmd_in_band(id_json->str);
333d403d92dSMarkus Armbruster g_string_free(id_json, true);
334d403d92dSMarkus Armbruster }
3357e3c0deaSKevin Wolf monitor_qmp_dispatch(mon, req_obj->req);
3367e3c0deaSKevin Wolf } else {
3377e3c0deaSKevin Wolf assert(req_obj->err);
338f680405fSMarkus Armbruster trace_monitor_qmp_err_in_band(error_get_pretty(req_obj->err));
3397e3c0deaSKevin Wolf rsp = qmp_error_response(req_obj->err);
3407e3c0deaSKevin Wolf req_obj->err = NULL;
3417e3c0deaSKevin Wolf monitor_qmp_respond(mon, rsp);
3427e3c0deaSKevin Wolf qobject_unref(rsp);
3437e3c0deaSKevin Wolf }
3447e3c0deaSKevin Wolf
34588daf099SMarkus Armbruster if (!oob_enabled) {
3467e3c0deaSKevin Wolf monitor_resume(&mon->common);
3477e3c0deaSKevin Wolf }
34888daf099SMarkus Armbruster
3497e3c0deaSKevin Wolf qmp_request_free(req_obj);
3509ce44e2cSKevin Wolf }
3510ff25537SPaolo Bonzini qatomic_set(&qmp_dispatcher_co, NULL);
3527e3c0deaSKevin Wolf }
3537e3c0deaSKevin Wolf
qmp_dispatcher_co_wake(void)3549f2d5854SPaolo Bonzini void qmp_dispatcher_co_wake(void)
3559f2d5854SPaolo Bonzini {
356eea7cd3fSPaolo Bonzini /* Write request before reading qmp_dispatcher_co_busy. */
357eea7cd3fSPaolo Bonzini smp_mb__before_rmw();
358eea7cd3fSPaolo Bonzini
359*03f50d7eSAndrey Drobyshev if (!qatomic_xchg(&qmp_dispatcher_co_busy, true) &&
360*03f50d7eSAndrey Drobyshev qatomic_read(&qmp_dispatcher_co)) {
3619f2d5854SPaolo Bonzini aio_co_wake(qmp_dispatcher_co);
3629f2d5854SPaolo Bonzini }
3639f2d5854SPaolo Bonzini }
3649f2d5854SPaolo Bonzini
handle_qmp_command(void * opaque,QObject * req,Error * err)3657e3c0deaSKevin Wolf static void handle_qmp_command(void *opaque, QObject *req, Error *err)
3667e3c0deaSKevin Wolf {
3677e3c0deaSKevin Wolf MonitorQMP *mon = opaque;
368d403d92dSMarkus Armbruster QDict *qdict = qobject_to(QDict, req);
3697e3c0deaSKevin Wolf QMPRequest *req_obj;
3707e3c0deaSKevin Wolf
3717e3c0deaSKevin Wolf assert(!req != !err);
3727e3c0deaSKevin Wolf
3737e3c0deaSKevin Wolf if (req && trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) {
374eab3a467SMarkus Armbruster GString *req_json = qobject_to_json(req);
375eab3a467SMarkus Armbruster trace_handle_qmp_command(mon, req_json->str);
376eab3a467SMarkus Armbruster g_string_free(req_json, true);
3777e3c0deaSKevin Wolf }
3787e3c0deaSKevin Wolf
3797e3c0deaSKevin Wolf if (qdict && qmp_is_oob(qdict)) {
3807e3c0deaSKevin Wolf /* OOB commands are executed immediately */
381d403d92dSMarkus Armbruster if (trace_event_get_state(TRACE_MONITOR_QMP_CMD_OUT_OF_BAND)) {
382d403d92dSMarkus Armbruster QObject *id = qdict_get(qdict, "id");
383d403d92dSMarkus Armbruster GString *id_json;
384d403d92dSMarkus Armbruster
385d403d92dSMarkus Armbruster id_json = id ? qobject_to_json(id) : g_string_new(NULL);
386d403d92dSMarkus Armbruster trace_monitor_qmp_cmd_out_of_band(id_json->str);
387d403d92dSMarkus Armbruster g_string_free(id_json, true);
388d403d92dSMarkus Armbruster }
3897e3c0deaSKevin Wolf monitor_qmp_dispatch(mon, req);
3907e3c0deaSKevin Wolf qobject_unref(req);
3917e3c0deaSKevin Wolf return;
3927e3c0deaSKevin Wolf }
3937e3c0deaSKevin Wolf
3947e3c0deaSKevin Wolf req_obj = g_new0(QMPRequest, 1);
3957e3c0deaSKevin Wolf req_obj->mon = mon;
3967e3c0deaSKevin Wolf req_obj->req = req;
3977e3c0deaSKevin Wolf req_obj->err = err;
3987e3c0deaSKevin Wolf
3997e3c0deaSKevin Wolf /* Protect qmp_requests and fetching its length. */
400a8e2ab5dSMahmoud Mandour WITH_QEMU_LOCK_GUARD(&mon->qmp_queue_lock) {
4017e3c0deaSKevin Wolf
4027e3c0deaSKevin Wolf /*
4037e3c0deaSKevin Wolf * Suspend the monitor when we can't queue more requests after
404395a9508SMarkus Armbruster * this one. Dequeuing in monitor_qmp_dispatcher_co() or
4052895aaa1SWolfgang Bumiller * monitor_qmp_cleanup_queue_and_resume() will resume it.
4062895aaa1SWolfgang Bumiller * Note that when OOB is disabled, we queue at most one command,
4072895aaa1SWolfgang Bumiller * for backward compatibility.
4087e3c0deaSKevin Wolf */
4097e3c0deaSKevin Wolf if (!qmp_oob_enabled(mon) ||
4107e3c0deaSKevin Wolf mon->qmp_requests->length == QMP_REQ_QUEUE_LEN_MAX - 1) {
4117e3c0deaSKevin Wolf monitor_suspend(&mon->common);
4127e3c0deaSKevin Wolf }
4137e3c0deaSKevin Wolf
4147e3c0deaSKevin Wolf /*
4157e3c0deaSKevin Wolf * Put the request to the end of queue so that requests will be
4167e3c0deaSKevin Wolf * handled in time order. Ownership for req_obj, req,
4177e3c0deaSKevin Wolf * etc. will be delivered to the handler side.
4187e3c0deaSKevin Wolf */
419f680405fSMarkus Armbruster trace_monitor_qmp_in_band_enqueue(req_obj, mon,
420f680405fSMarkus Armbruster mon->qmp_requests->length);
4217e3c0deaSKevin Wolf assert(mon->qmp_requests->length < QMP_REQ_QUEUE_LEN_MAX);
4227e3c0deaSKevin Wolf g_queue_push_tail(mon->qmp_requests, req_obj);
423a8e2ab5dSMahmoud Mandour }
4247e3c0deaSKevin Wolf
4257e3c0deaSKevin Wolf /* Kick the dispatcher routine */
4269f2d5854SPaolo Bonzini qmp_dispatcher_co_wake();
4277e3c0deaSKevin Wolf }
4287e3c0deaSKevin Wolf
monitor_qmp_read(void * opaque,const uint8_t * buf,int size)4297e3c0deaSKevin Wolf static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
4307e3c0deaSKevin Wolf {
4317e3c0deaSKevin Wolf MonitorQMP *mon = opaque;
4327e3c0deaSKevin Wolf
4337e3c0deaSKevin Wolf json_message_parser_feed(&mon->parser, (const char *) buf, size);
4347e3c0deaSKevin Wolf }
4357e3c0deaSKevin Wolf
qmp_greeting(MonitorQMP * mon)4367e3c0deaSKevin Wolf static QDict *qmp_greeting(MonitorQMP *mon)
4377e3c0deaSKevin Wolf {
4387e3c0deaSKevin Wolf QList *cap_list = qlist_new();
4397e3c0deaSKevin Wolf QObject *ver = NULL;
4402061487bSMarkus Armbruster QDict *args;
4417e3c0deaSKevin Wolf QMPCapability cap;
4427e3c0deaSKevin Wolf
4432061487bSMarkus Armbruster args = qdict_new();
4442061487bSMarkus Armbruster qmp_marshal_query_version(args, &ver, NULL);
4452061487bSMarkus Armbruster qobject_unref(args);
4467e3c0deaSKevin Wolf
4477e3c0deaSKevin Wolf for (cap = 0; cap < QMP_CAPABILITY__MAX; cap++) {
4487e3c0deaSKevin Wolf if (mon->capab_offered[cap]) {
4497e3c0deaSKevin Wolf qlist_append_str(cap_list, QMPCapability_str(cap));
4507e3c0deaSKevin Wolf }
4517e3c0deaSKevin Wolf }
4527e3c0deaSKevin Wolf
4537e3c0deaSKevin Wolf return qdict_from_jsonf_nofail(
4547e3c0deaSKevin Wolf "{'QMP': {'version': %p, 'capabilities': %p}}",
4557e3c0deaSKevin Wolf ver, cap_list);
4567e3c0deaSKevin Wolf }
4577e3c0deaSKevin Wolf
monitor_qmp_event(void * opaque,QEMUChrEvent event)458083b266fSPhilippe Mathieu-Daudé static void monitor_qmp_event(void *opaque, QEMUChrEvent event)
4597e3c0deaSKevin Wolf {
4607e3c0deaSKevin Wolf QDict *data;
4617e3c0deaSKevin Wolf MonitorQMP *mon = opaque;
4627e3c0deaSKevin Wolf
4637e3c0deaSKevin Wolf switch (event) {
4647e3c0deaSKevin Wolf case CHR_EVENT_OPENED:
4657e3c0deaSKevin Wolf mon->commands = &qmp_cap_negotiation_commands;
4667e3c0deaSKevin Wolf monitor_qmp_caps_reset(mon);
4677e3c0deaSKevin Wolf data = qmp_greeting(mon);
4687e3c0deaSKevin Wolf qmp_send_response(mon, data);
4697e3c0deaSKevin Wolf qobject_unref(data);
4707e3c0deaSKevin Wolf break;
4717e3c0deaSKevin Wolf case CHR_EVENT_CLOSED:
4727e3c0deaSKevin Wolf /*
4737e3c0deaSKevin Wolf * Note: this is only useful when the output of the chardev
4747e3c0deaSKevin Wolf * backend is still open. For example, when the backend is
4757e3c0deaSKevin Wolf * stdio, it's possible that stdout is still open when stdin
4767e3c0deaSKevin Wolf * is closed.
4777e3c0deaSKevin Wolf */
4782895aaa1SWolfgang Bumiller monitor_qmp_cleanup_queue_and_resume(mon);
4797e3c0deaSKevin Wolf json_message_parser_destroy(&mon->parser);
4807e3c0deaSKevin Wolf json_message_parser_init(&mon->parser, handle_qmp_command,
4817e3c0deaSKevin Wolf mon, NULL);
4827e3c0deaSKevin Wolf monitor_fdsets_cleanup();
4837e3c0deaSKevin Wolf break;
484ed7c5bb7SPhilippe Mathieu-Daudé case CHR_EVENT_BREAK:
485ed7c5bb7SPhilippe Mathieu-Daudé case CHR_EVENT_MUX_IN:
486ed7c5bb7SPhilippe Mathieu-Daudé case CHR_EVENT_MUX_OUT:
487ed7c5bb7SPhilippe Mathieu-Daudé /* Ignore */
488ed7c5bb7SPhilippe Mathieu-Daudé break;
4897e3c0deaSKevin Wolf }
4907e3c0deaSKevin Wolf }
4917e3c0deaSKevin Wolf
monitor_data_destroy_qmp(MonitorQMP * mon)4927e3c0deaSKevin Wolf void monitor_data_destroy_qmp(MonitorQMP *mon)
4937e3c0deaSKevin Wolf {
4947e3c0deaSKevin Wolf json_message_parser_destroy(&mon->parser);
4957e3c0deaSKevin Wolf qemu_mutex_destroy(&mon->qmp_queue_lock);
4967e3c0deaSKevin Wolf monitor_qmp_cleanup_req_queue_locked(mon);
4977e3c0deaSKevin Wolf g_queue_free(mon->qmp_requests);
4987e3c0deaSKevin Wolf }
4997e3c0deaSKevin Wolf
monitor_qmp_setup_handlers_bh(void * opaque)5007e3c0deaSKevin Wolf static void monitor_qmp_setup_handlers_bh(void *opaque)
5017e3c0deaSKevin Wolf {
5027e3c0deaSKevin Wolf MonitorQMP *mon = opaque;
5037e3c0deaSKevin Wolf GMainContext *context;
5047e3c0deaSKevin Wolf
5057e3c0deaSKevin Wolf assert(mon->common.use_io_thread);
5067e3c0deaSKevin Wolf context = iothread_get_g_main_context(mon_iothread);
5077e3c0deaSKevin Wolf assert(context);
5087e3c0deaSKevin Wolf qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read,
5097e3c0deaSKevin Wolf monitor_qmp_read, monitor_qmp_event,
5107e3c0deaSKevin Wolf NULL, &mon->common, context, true);
5117e3c0deaSKevin Wolf monitor_list_append(&mon->common);
5127e3c0deaSKevin Wolf }
5137e3c0deaSKevin Wolf
monitor_init_qmp(Chardev * chr,bool pretty,Error ** errp)514f27a9bb3SKevin Wolf void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
5157e3c0deaSKevin Wolf {
5167e3c0deaSKevin Wolf MonitorQMP *mon = g_new0(MonitorQMP, 1);
5177e3c0deaSKevin Wolf
518f27a9bb3SKevin Wolf if (!qemu_chr_fe_init(&mon->common.chr, chr, errp)) {
519f27a9bb3SKevin Wolf g_free(mon);
520f27a9bb3SKevin Wolf return;
521f27a9bb3SKevin Wolf }
522f27a9bb3SKevin Wolf qemu_chr_fe_set_echo(&mon->common.chr, true);
523f27a9bb3SKevin Wolf
5247e3c0deaSKevin Wolf /* Note: we run QMP monitor in I/O thread when @chr supports that */
52592082416SKevin Wolf monitor_data_init(&mon->common, true, false,
5267e3c0deaSKevin Wolf qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT));
5277e3c0deaSKevin Wolf
528fbfc29e3SKevin Wolf mon->pretty = pretty;
52992082416SKevin Wolf
5307e3c0deaSKevin Wolf qemu_mutex_init(&mon->qmp_queue_lock);
5317e3c0deaSKevin Wolf mon->qmp_requests = g_queue_new();
5327e3c0deaSKevin Wolf
5337e3c0deaSKevin Wolf json_message_parser_init(&mon->parser, handle_qmp_command, mon, NULL);
5347e3c0deaSKevin Wolf if (mon->common.use_io_thread) {
5357e3c0deaSKevin Wolf /*
5367e3c0deaSKevin Wolf * Make sure the old iowatch is gone. It's possible when
5377e3c0deaSKevin Wolf * e.g. the chardev is in client mode, with wait=on.
5387e3c0deaSKevin Wolf */
5397e3c0deaSKevin Wolf remove_fd_in_watch(chr);
5407e3c0deaSKevin Wolf /*
5417e3c0deaSKevin Wolf * We can't call qemu_chr_fe_set_handlers() directly here
5427e3c0deaSKevin Wolf * since chardev might be running in the monitor I/O
5437e3c0deaSKevin Wolf * thread. Schedule a bottom half.
5447e3c0deaSKevin Wolf */
5457e3c0deaSKevin Wolf aio_bh_schedule_oneshot(iothread_get_aio_context(mon_iothread),
5467e3c0deaSKevin Wolf monitor_qmp_setup_handlers_bh, mon);
5477e3c0deaSKevin Wolf /* The bottom half will add @mon to @mon_list */
5487e3c0deaSKevin Wolf } else {
5497e3c0deaSKevin Wolf qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read,
5507e3c0deaSKevin Wolf monitor_qmp_read, monitor_qmp_event,
5517e3c0deaSKevin Wolf NULL, &mon->common, NULL, true);
5527e3c0deaSKevin Wolf monitor_list_append(&mon->common);
5537e3c0deaSKevin Wolf }
5547e3c0deaSKevin Wolf }
555