| 1 | /* |
| 2 | * QEMU monitor |
| 3 | * |
| 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "qemu/osdep.h" |
| 26 | #include "qemu/aio-wait.h" |
| 27 | #include "qemu/lockable.h" |
| 28 | #include "chardev/char-io.h" |
| 29 | #include "monitor-internal.h" |
| 30 | #include "qapi/error.h" |
| 31 | #include "qapi/qapi-commands-control.h" |
| 32 | #include "qapi/qapi-commands-char.h" |
| 33 | #include "qobject/qdict.h" |
| 34 | #include "qobject/qjson.h" |
| 35 | #include "qobject/qlist.h" |
| 36 | #include "qom/object_interfaces.h" |
| 37 | #include "trace.h" |
| 38 | |
| 39 | /* |
| 40 | * qmp_dispatcher_co_busy is used for synchronisation between the |
| 41 | * monitor thread and the main thread to ensure that the dispatcher |
| 42 | * coroutine never gets scheduled a second time when it's already |
| 43 | * scheduled (scheduling the same coroutine twice is forbidden). |
| 44 | * |
| 45 | * It is true if the coroutine will process at least one more request |
| 46 | * before going to sleep. Either it has been kicked already, or it |
| 47 | * is active and processing requests. Additional requests may therefore |
| 48 | * be pushed onto mon->qmp_requests, and @qmp_dispatcher_co_shutdown may |
| 49 | * be set without further ado. @qmp_dispatcher_co must not be woken up |
| 50 | * in this case. |
| 51 | * |
| 52 | * If false, you have to wake up @qmp_dispatcher_co after pushing new |
| 53 | * requests. You also have to set @qmp_dispatcher_co_busy to true |
| 54 | * before waking up the coroutine. |
| 55 | * |
| 56 | * The coroutine will automatically change this variable back to false |
| 57 | * before it yields. Nobody else may set the variable to false. |
| 58 | * |
| 59 | * Access must be atomic for thread safety. |
| 60 | */ |
| 61 | static bool qmp_dispatcher_co_busy = true; |
| 62 | |
| 63 | struct QMPRequest { |
| 64 | /* Owner of the request */ |
| 65 | MonitorQMP *mon; |
| 66 | /* |
| 67 | * Request object to be handled or Error to be reported |
| 68 | * (exactly one of them is non-null) |
| 69 | */ |
| 70 | QObject *req; |
| 71 | Error *err; |
| 72 | }; |
| 73 | typedef struct QMPRequest QMPRequest; |
| 74 | |
| 75 | QmpCommandList qmp_commands, qmp_cap_negotiation_commands; |
| 76 | |
| 77 | /* Monitor being serviced by the dispatcher. Protected by BQL. */ |
| 78 | static MonitorQMP *qmp_dispatcher_current_mon; |
| 79 | |
| 80 | OBJECT_DEFINE_TYPE(MonitorQMP, monitor_qmp, MONITOR_QMP, MONITOR); |
| 81 | |
| 82 | static void monitor_qmp_cleanup_req_queue_locked(MonitorQMP *mon); |
| 83 | |
| 84 | static void monitor_qmp_finalize(Object *obj) |
| 85 | { |
| 86 | MonitorQMP *mon = MONITOR_QMP(obj); |
| 87 | |
| 88 | json_message_parser_destroy(&mon->parser); |
| 89 | qemu_mutex_destroy(&mon->qmp_queue_lock); |
| 90 | monitor_qmp_cleanup_req_queue_locked(mon); |
| 91 | g_queue_free(mon->qmp_requests); |
| 92 | } |
| 93 | |
| 94 | static bool monitor_qmp_get_pretty(Object *obj, Error **errp) |
| 95 | { |
| 96 | MonitorQMP *mon = MONITOR_QMP(obj); |
| 97 | |
| 98 | return mon->pretty; |
| 99 | } |
| 100 | |
| 101 | static void monitor_qmp_set_pretty(Object *obj, bool val, Error **errp) |
| 102 | { |
| 103 | MonitorQMP *mon = MONITOR_QMP(obj); |
| 104 | |
| 105 | mon->pretty = val; |
| 106 | } |
| 107 | |
| 108 | static int monitor_qmp_get_close_action(Object *obj, Error **errp) |
| 109 | { |
| 110 | MonitorQMP *mon = MONITOR_QMP(obj); |
| 111 | |
| 112 | return mon->close_action; |
| 113 | } |
| 114 | |
| 115 | static void monitor_qmp_set_close_action(Object *obj, int val, Error **errp) |
| 116 | { |
| 117 | MonitorQMP *mon = MONITOR_QMP(obj); |
| 118 | |
| 119 | mon->close_action = val; |
| 120 | } |
| 121 | |
| 122 | static void monitor_qmp_emit_event(Monitor *mon, QAPIEvent event, QDict *qdict); |
| 123 | static bool monitor_qmp_requires_iothread(const Monitor *mon); |
| 124 | static void monitor_qmp_complete(UserCreatable *uc, Error **errp); |
| 125 | static bool monitor_qmp_prepare_delete(UserCreatable *uc, Error **errp); |
| 126 | static void monitor_qmp_accept_input(Monitor *mon); |
| 127 | |
| 128 | static void monitor_qmp_class_init(ObjectClass *cls, const void *data) |
| 129 | { |
| 130 | MonitorClass *moncls = MONITOR_CLASS(cls); |
| 131 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(cls); |
| 132 | |
| 133 | object_class_property_add_bool(cls, "pretty", |
| 134 | monitor_qmp_get_pretty, |
| 135 | monitor_qmp_set_pretty); |
| 136 | object_class_property_add_enum(cls, "close-action", |
| 137 | "MonitorQMPCloseAction", |
| 138 | &MonitorQMPCloseAction_lookup, |
| 139 | monitor_qmp_get_close_action, |
| 140 | monitor_qmp_set_close_action); |
| 141 | |
| 142 | moncls->emit_event = monitor_qmp_emit_event; |
| 143 | moncls->requires_iothread = monitor_qmp_requires_iothread; |
| 144 | moncls->accept_input = monitor_qmp_accept_input; |
| 145 | |
| 146 | ucc->complete = monitor_qmp_complete; |
| 147 | ucc->prepare_delete = monitor_qmp_prepare_delete; |
| 148 | } |
| 149 | |
| 150 | static void handle_qmp_command(void *opaque, QObject *req, Error *err); |
| 151 | static void monitor_qmp_init(Object *obj) |
| 152 | { |
| 153 | MonitorQMP *mon = MONITOR_QMP(obj); |
| 154 | |
| 155 | qemu_mutex_init(&mon->qmp_queue_lock); |
| 156 | mon->qmp_requests = g_queue_new(); |
| 157 | |
| 158 | json_message_parser_init(&mon->parser, handle_qmp_command, mon, NULL); |
| 159 | } |
| 160 | |
| 161 | static void monitor_qmp_emit_event(Monitor *mon, QAPIEvent event, QDict *qdict) |
| 162 | { |
| 163 | MonitorQMP *qmp = MONITOR_QMP(mon); |
| 164 | |
| 165 | WITH_QEMU_LOCK_GUARD(&mon->mon_lock) { |
| 166 | if (qmp->commands == &qmp_cap_negotiation_commands) { |
| 167 | return; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | qmp_send_response(qmp, qdict); |
| 172 | } |
| 173 | |
| 174 | static bool monitor_qmp_requires_iothread(const Monitor *mon) |
| 175 | { |
| 176 | return qemu_chr_has_feature(mon->chr.chr, |
| 177 | QEMU_CHAR_FEATURE_GCONTEXT); |
| 178 | } |
| 179 | |
| 180 | static bool qmp_oob_enabled(MonitorQMP *mon) |
| 181 | { |
| 182 | return mon->capab[QMP_CAPABILITY_OOB]; |
| 183 | } |
| 184 | |
| 185 | static void monitor_qmp_caps_reset(MonitorQMP *mon) |
| 186 | { |
| 187 | memset(mon->capab_offered, 0, sizeof(mon->capab_offered)); |
| 188 | memset(mon->capab, 0, sizeof(mon->capab)); |
| 189 | mon->capab_offered[QMP_CAPABILITY_OOB] = |
| 190 | monitor_requires_iothread(MONITOR(mon)); |
| 191 | } |
| 192 | |
| 193 | static void qmp_request_free(QMPRequest *req) |
| 194 | { |
| 195 | qobject_unref(req->req); |
| 196 | error_free(req->err); |
| 197 | g_free(req); |
| 198 | } |
| 199 | |
| 200 | /* Caller must hold mon->qmp.qmp_queue_lock */ |
| 201 | static void monitor_qmp_cleanup_req_queue_locked(MonitorQMP *mon) |
| 202 | { |
| 203 | while (!g_queue_is_empty(mon->qmp_requests)) { |
| 204 | qmp_request_free(g_queue_pop_head(mon->qmp_requests)); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | static void monitor_qmp_drain_queue(MonitorQMP *mon) |
| 209 | { |
| 210 | QEMU_LOCK_GUARD(&mon->qmp_queue_lock); |
| 211 | monitor_qmp_cleanup_req_queue_locked(mon); |
| 212 | } |
| 213 | |
| 214 | static void monitor_qmp_cleanup_queue_and_resume(MonitorQMP *mon) |
| 215 | { |
| 216 | QEMU_LOCK_GUARD(&mon->qmp_queue_lock); |
| 217 | |
| 218 | /* |
| 219 | * Same condition as in monitor_qmp_dispatcher_co(), but before |
| 220 | * removing an element from the queue (hence no `- 1`). |
| 221 | * Also, the queue should not be empty either, otherwise the |
| 222 | * monitor hasn't been suspended yet (or was already resumed). |
| 223 | */ |
| 224 | bool need_resume = (!qmp_oob_enabled(mon) || |
| 225 | mon->qmp_requests->length == QMP_REQ_QUEUE_LEN_MAX) |
| 226 | && !g_queue_is_empty(mon->qmp_requests); |
| 227 | |
| 228 | monitor_qmp_cleanup_req_queue_locked(mon); |
| 229 | |
| 230 | if (need_resume) { |
| 231 | /* |
| 232 | * handle_qmp_command() suspended the monitor because the |
| 233 | * request queue filled up, to be resumed when the queue has |
| 234 | * space again. We just emptied it; resume the monitor. |
| 235 | * |
| 236 | * Without this, the monitor would remain suspended forever |
| 237 | * when we get here while the monitor is suspended. An |
| 238 | * unfortunately timed CHR_EVENT_CLOSED can do the trick. |
| 239 | */ |
| 240 | monitor_resume(&mon->parent_obj); |
| 241 | } |
| 242 | |
| 243 | } |
| 244 | |
| 245 | void qmp_send_response(MonitorQMP *mon, const QDict *rsp) |
| 246 | { |
| 247 | const QObject *data = QOBJECT(rsp); |
| 248 | GString *json; |
| 249 | |
| 250 | json = qobject_to_json_pretty(data, mon->pretty); |
| 251 | assert(json != NULL); |
| 252 | trace_monitor_qmp_respond(mon, json->str); |
| 253 | |
| 254 | g_string_append_c(json, '\n'); |
| 255 | monitor_puts(&mon->parent_obj, json->str); |
| 256 | |
| 257 | g_string_free(json, true); |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Emit QMP response @rsp to @mon. |
| 262 | * Null @rsp can only happen for commands with QCO_NO_SUCCESS_RESP. |
| 263 | * Nothing is emitted then. |
| 264 | */ |
| 265 | static void monitor_qmp_respond(MonitorQMP *mon, QDict *rsp) |
| 266 | { |
| 267 | if (rsp) { |
| 268 | qmp_send_response(mon, rsp); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Runs outside of coroutine context for OOB commands, but in |
| 274 | * coroutine context for everything else. |
| 275 | */ |
| 276 | static void monitor_qmp_dispatch(MonitorQMP *mon, QObject *req) |
| 277 | { |
| 278 | QDict *rsp; |
| 279 | QDict *error; |
| 280 | |
| 281 | rsp = qmp_dispatch(mon->commands, req, qmp_oob_enabled(mon), |
| 282 | &mon->parent_obj); |
| 283 | |
| 284 | if (mon->commands == &qmp_cap_negotiation_commands) { |
| 285 | error = qdict_get_qdict(rsp, "error"); |
| 286 | if (error |
| 287 | && !g_strcmp0(qdict_get_try_str(error, "class"), |
| 288 | QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND))) { |
| 289 | /* Provide a more useful error message */ |
| 290 | qdict_del(error, "desc"); |
| 291 | qdict_put_str(error, "desc", "Expecting capabilities negotiation" |
| 292 | " with 'qmp_capabilities'"); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | monitor_qmp_respond(mon, rsp); |
| 297 | qobject_unref(rsp); |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Pop a QMP request from a monitor request queue. |
| 302 | * Return the request, or NULL all request queues are empty. |
| 303 | * We are using round-robin fashion to pop the request, to avoid |
| 304 | * processing commands only on a very busy monitor. To achieve that, |
| 305 | * when we process one request on a specific monitor, we put that |
| 306 | * monitor to the end of mon_list queue. |
| 307 | * |
| 308 | * Note: if the function returned with non-NULL, then the caller will |
| 309 | * be with qmp_mon->qmp_queue_lock held, and the caller is responsible |
| 310 | * to release it. |
| 311 | */ |
| 312 | static QMPRequest *monitor_qmp_requests_pop_any_with_lock(void) |
| 313 | { |
| 314 | QMPRequest *req_obj = NULL; |
| 315 | Monitor *mon; |
| 316 | MonitorQMP *qmp_mon; |
| 317 | |
| 318 | QTAILQ_FOREACH(mon, &mon_list, entry) { |
| 319 | qmp_mon = MONITOR_QMP( |
| 320 | object_dynamic_cast(OBJECT(mon), TYPE_MONITOR_QMP)); |
| 321 | if (!qmp_mon) { |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | qemu_mutex_lock(&qmp_mon->qmp_queue_lock); |
| 326 | req_obj = g_queue_pop_head(qmp_mon->qmp_requests); |
| 327 | if (req_obj) { |
| 328 | /* With the lock of corresponding queue held */ |
| 329 | break; |
| 330 | } |
| 331 | qemu_mutex_unlock(&qmp_mon->qmp_queue_lock); |
| 332 | } |
| 333 | |
| 334 | if (req_obj) { |
| 335 | /* |
| 336 | * We found one request on the monitor. Degrade this monitor's |
| 337 | * priority to lowest by re-inserting it to end of queue. |
| 338 | */ |
| 339 | QTAILQ_REMOVE(&mon_list, mon, entry); |
| 340 | QTAILQ_INSERT_TAIL(&mon_list, mon, entry); |
| 341 | } |
| 342 | |
| 343 | return req_obj; |
| 344 | } |
| 345 | |
| 346 | static QMPRequest * coroutine_fn |
| 347 | monitor_qmp_dispatcher_pop_any(void) |
| 348 | { |
| 349 | while (true) { |
| 350 | /* |
| 351 | * To avoid double scheduling, busy is true on entry to |
| 352 | * monitor_qmp_dispatcher_co(), and must be set again before |
| 353 | * aio_co_wake()-ing it. |
| 354 | */ |
| 355 | assert(qatomic_read(&qmp_dispatcher_co_busy) == true); |
| 356 | |
| 357 | /* |
| 358 | * Mark the dispatcher as not busy already here so that we |
| 359 | * don't miss any new requests coming in the middle of our |
| 360 | * processing. |
| 361 | * |
| 362 | * Clear qmp_dispatcher_co_busy before reading request. |
| 363 | */ |
| 364 | qatomic_set_mb(&qmp_dispatcher_co_busy, false); |
| 365 | |
| 366 | WITH_QEMU_LOCK_GUARD(&monitor_lock) { |
| 367 | QMPRequest *req_obj; |
| 368 | |
| 369 | /* On shutdown, don't take any more requests from the queue */ |
| 370 | if (qmp_dispatcher_co_shutdown) { |
| 371 | return NULL; |
| 372 | } |
| 373 | |
| 374 | req_obj = monitor_qmp_requests_pop_any_with_lock(); |
| 375 | if (req_obj) { |
| 376 | return req_obj; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * No more requests to process. Wait to be reentered from |
| 382 | * handle_qmp_command() when it pushes more requests, or |
| 383 | * from monitor_cleanup() when it requests shutdown. |
| 384 | */ |
| 385 | qemu_coroutine_yield(); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | void coroutine_fn monitor_qmp_dispatcher_co(void *data) |
| 390 | { |
| 391 | QMPRequest *req_obj; |
| 392 | QDict *rsp; |
| 393 | bool oob_enabled; |
| 394 | MonitorQMP *mon; |
| 395 | |
| 396 | while ((req_obj = monitor_qmp_dispatcher_pop_any()) != NULL) { |
| 397 | trace_monitor_qmp_in_band_dequeue(req_obj, |
| 398 | req_obj->mon->qmp_requests->length); |
| 399 | |
| 400 | /* |
| 401 | * @req_obj has a request, we hold req_obj->mon->qmp_queue_lock |
| 402 | */ |
| 403 | |
| 404 | mon = req_obj->mon; |
| 405 | qmp_dispatcher_current_mon = mon; |
| 406 | |
| 407 | /* |
| 408 | * We need to resume the monitor if handle_qmp_command() |
| 409 | * suspended it. Two cases: |
| 410 | * 1. OOB enabled: mon->qmp_requests has no more space |
| 411 | * Resume right away, so that OOB commands can get executed while |
| 412 | * this request is being processed. |
| 413 | * 2. OOB disabled: always |
| 414 | * Resume only after we're done processing the request, |
| 415 | * We need to save qmp_oob_enabled() for later, because |
| 416 | * qmp_qmp_capabilities() can change it. |
| 417 | */ |
| 418 | oob_enabled = qmp_oob_enabled(mon); |
| 419 | if (oob_enabled |
| 420 | && mon->qmp_requests->length == QMP_REQ_QUEUE_LEN_MAX - 1) { |
| 421 | monitor_resume(&mon->parent_obj); |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * Drop the queue mutex now, before yielding, otherwise we might |
| 426 | * deadlock if the main thread tries to lock it. |
| 427 | */ |
| 428 | qemu_mutex_unlock(&mon->qmp_queue_lock); |
| 429 | |
| 430 | if (qatomic_xchg(&qmp_dispatcher_co_busy, true) == true) { |
| 431 | /* |
| 432 | * Someone rescheduled us (probably because a new requests |
| 433 | * came in), but we didn't actually yield. Do that now, |
| 434 | * only to be immediately reentered and removed from the |
| 435 | * list of scheduled coroutines. |
| 436 | */ |
| 437 | qemu_coroutine_yield(); |
| 438 | } |
| 439 | |
| 440 | /* Process request */ |
| 441 | if (req_obj->req) { |
| 442 | if (trace_event_get_state(TRACE_MONITOR_QMP_CMD_IN_BAND)) { |
| 443 | QDict *qdict = qobject_to(QDict, req_obj->req); |
| 444 | QObject *id = qdict ? qdict_get(qdict, "id") : NULL; |
| 445 | GString *id_json; |
| 446 | |
| 447 | id_json = id ? qobject_to_json(id) : g_string_new(NULL); |
| 448 | trace_monitor_qmp_cmd_in_band(id_json->str); |
| 449 | g_string_free(id_json, true); |
| 450 | } |
| 451 | monitor_qmp_dispatch(mon, req_obj->req); |
| 452 | } else { |
| 453 | assert(req_obj->err); |
| 454 | trace_monitor_qmp_err_in_band(error_get_pretty(req_obj->err)); |
| 455 | rsp = qmp_error_response(req_obj->err); |
| 456 | req_obj->err = NULL; |
| 457 | monitor_qmp_respond(mon, rsp); |
| 458 | qobject_unref(rsp); |
| 459 | } |
| 460 | |
| 461 | if (!oob_enabled) { |
| 462 | monitor_resume(&mon->parent_obj); |
| 463 | } |
| 464 | |
| 465 | qmp_request_free(req_obj); |
| 466 | qmp_dispatcher_current_mon = NULL; |
| 467 | } |
| 468 | qatomic_set(&qmp_dispatcher_co, NULL); |
| 469 | } |
| 470 | |
| 471 | void qmp_dispatcher_co_wake(void) |
| 472 | { |
| 473 | /* Write request before reading qmp_dispatcher_co_busy. */ |
| 474 | smp_mb__before_rmw(); |
| 475 | |
| 476 | if (!qatomic_xchg(&qmp_dispatcher_co_busy, true) && |
| 477 | qatomic_read(&qmp_dispatcher_co)) { |
| 478 | aio_co_wake(qmp_dispatcher_co); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | static void handle_qmp_command(void *opaque, QObject *req, Error *err) |
| 483 | { |
| 484 | MonitorQMP *mon = opaque; |
| 485 | QDict *qdict = qobject_to(QDict, req); |
| 486 | QMPRequest *req_obj; |
| 487 | |
| 488 | assert(!req != !err); |
| 489 | |
| 490 | if (req && trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) { |
| 491 | GString *req_json = qobject_to_json(req); |
| 492 | trace_handle_qmp_command(mon, req_json->str); |
| 493 | g_string_free(req_json, true); |
| 494 | } |
| 495 | |
| 496 | if (qdict && qmp_is_oob(qdict)) { |
| 497 | /* OOB commands are executed immediately */ |
| 498 | if (trace_event_get_state(TRACE_MONITOR_QMP_CMD_OUT_OF_BAND)) { |
| 499 | QObject *id = qdict_get(qdict, "id"); |
| 500 | GString *id_json; |
| 501 | |
| 502 | id_json = id ? qobject_to_json(id) : g_string_new(NULL); |
| 503 | trace_monitor_qmp_cmd_out_of_band(id_json->str); |
| 504 | g_string_free(id_json, true); |
| 505 | } |
| 506 | monitor_qmp_dispatch(mon, req); |
| 507 | qobject_unref(req); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | req_obj = g_new0(QMPRequest, 1); |
| 512 | req_obj->mon = mon; |
| 513 | req_obj->req = req; |
| 514 | req_obj->err = err; |
| 515 | |
| 516 | /* Protect qmp_requests and fetching its length. */ |
| 517 | WITH_QEMU_LOCK_GUARD(&mon->qmp_queue_lock) { |
| 518 | |
| 519 | /* |
| 520 | * Suspend the monitor when we can't queue more requests after |
| 521 | * this one. Dequeuing in monitor_qmp_dispatcher_co() or |
| 522 | * monitor_qmp_cleanup_queue_and_resume() will resume it. |
| 523 | * Note that when OOB is disabled, we queue at most one command, |
| 524 | * for backward compatibility. |
| 525 | */ |
| 526 | if (!qmp_oob_enabled(mon) || |
| 527 | mon->qmp_requests->length == QMP_REQ_QUEUE_LEN_MAX - 1) { |
| 528 | monitor_suspend(&mon->parent_obj); |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Put the request to the end of queue so that requests will be |
| 533 | * handled in time order. Ownership for req_obj, req, |
| 534 | * etc. will be delivered to the handler side. |
| 535 | */ |
| 536 | trace_monitor_qmp_in_band_enqueue(req_obj, mon, |
| 537 | mon->qmp_requests->length); |
| 538 | assert(mon->qmp_requests->length < QMP_REQ_QUEUE_LEN_MAX); |
| 539 | g_queue_push_tail(mon->qmp_requests, req_obj); |
| 540 | } |
| 541 | |
| 542 | /* Kick the dispatcher routine */ |
| 543 | qmp_dispatcher_co_wake(); |
| 544 | } |
| 545 | |
| 546 | static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size) |
| 547 | { |
| 548 | MonitorQMP *mon = opaque; |
| 549 | |
| 550 | json_message_parser_feed(&mon->parser, (const char *) buf, size); |
| 551 | } |
| 552 | |
| 553 | static QDict *qmp_greeting(MonitorQMP *mon) |
| 554 | { |
| 555 | QList *cap_list = qlist_new(); |
| 556 | QObject *ver = NULL; |
| 557 | QDict *args; |
| 558 | QMPCapability cap; |
| 559 | |
| 560 | args = qdict_new(); |
| 561 | qmp_marshal_query_version(args, &ver, NULL); |
| 562 | qobject_unref(args); |
| 563 | |
| 564 | for (cap = 0; cap < QMP_CAPABILITY__MAX; cap++) { |
| 565 | if (mon->capab_offered[cap]) { |
| 566 | qlist_append_str(cap_list, QMPCapability_str(cap)); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | return qdict_from_jsonf_nofail( |
| 571 | "{'QMP': {'version': %p, 'capabilities': %p}}", |
| 572 | ver, cap_list); |
| 573 | } |
| 574 | |
| 575 | static void monitor_qmp_self_delete_bh(void *opaque) |
| 576 | { |
| 577 | MonitorQMP *mon = opaque; |
| 578 | const char *mon_id = object_get_canonical_path_component( |
| 579 | OBJECT(mon)); |
| 580 | g_autofree char *chardev_id = g_strdup(mon->parent_obj.chardev_id); |
| 581 | Error *local_error = NULL; |
| 582 | |
| 583 | if (!mon_id) { |
| 584 | /* |
| 585 | * Another monitor raced & ran 'object-del' on 'mon' |
| 586 | * before this BH got scheduled, so we have a ref on |
| 587 | * mon from monitor_qmp_event but it is already |
| 588 | * unparented. |
| 589 | */ |
| 590 | object_unref(mon); |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | user_creatable_del(mon_id, &local_error); |
| 595 | /* Pairs with ref from monitor_qmp_event */ |
| 596 | object_unref(mon); |
| 597 | if (local_error != NULL) { |
| 598 | error_report_err(local_error); |
| 599 | } else { |
| 600 | qmp_chardev_remove(chardev_id, NULL); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | static void monitor_qmp_event(void *opaque, QEMUChrEvent event) |
| 605 | { |
| 606 | QDict *data; |
| 607 | MonitorQMP *mon = opaque; |
| 608 | |
| 609 | /* |
| 610 | * Protect against race if a client drops & quickly |
| 611 | * reconnects - we'll have the delete BH scheduled |
| 612 | * so must not honour a new open request |
| 613 | */ |
| 614 | if (mon->delete_pending) { |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | switch (event) { |
| 619 | case CHR_EVENT_OPENED: |
| 620 | WITH_QEMU_LOCK_GUARD(&mon->parent_obj.mon_lock) { |
| 621 | mon->commands = &qmp_cap_negotiation_commands; |
| 622 | monitor_qmp_caps_reset(mon); |
| 623 | } |
| 624 | data = qmp_greeting(mon); |
| 625 | qmp_send_response(mon, data); |
| 626 | qobject_unref(data); |
| 627 | break; |
| 628 | case CHR_EVENT_CLOSED: |
| 629 | /* |
| 630 | * Note: this is only useful when the output of the chardev |
| 631 | * backend is still open. For example, when the backend is |
| 632 | * stdio, it's possible that stdout is still open when stdin |
| 633 | * is closed. |
| 634 | */ |
| 635 | monitor_qmp_cleanup_queue_and_resume(mon); |
| 636 | json_message_parser_destroy(&mon->parser); |
| 637 | json_message_parser_init(&mon->parser, handle_qmp_command, |
| 638 | mon, NULL); |
| 639 | monitor_fdsets_cleanup(); |
| 640 | switch (mon->close_action) { |
| 641 | case MONITOR_QMP_CLOSE_ACTION_NONE: |
| 642 | break; |
| 643 | case MONITOR_QMP_CLOSE_ACTION_DELETE: |
| 644 | mon->delete_pending = true; |
| 645 | /* |
| 646 | * Do NOT run in the AIO context associated with the |
| 647 | * monitor. We need to run in the default AIO context |
| 648 | * which is the same context in which 'qmp_object_del' |
| 649 | * will execute |
| 650 | * |
| 651 | * Hold an extra ref in case a separate monitor races |
| 652 | * with the BH by processing an explicit 'object-del'. |
| 653 | * Will be released by monitor_qmp_self_delete_bh |
| 654 | */ |
| 655 | object_ref(mon); |
| 656 | aio_bh_schedule_oneshot(qemu_get_aio_context(), |
| 657 | monitor_qmp_self_delete_bh, mon); |
| 658 | break; |
| 659 | default: |
| 660 | g_assert_not_reached(); |
| 661 | } |
| 662 | break; |
| 663 | case CHR_EVENT_BREAK: |
| 664 | case CHR_EVENT_MUX_IN: |
| 665 | case CHR_EVENT_MUX_OUT: |
| 666 | /* Ignore */ |
| 667 | break; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | static bool monitor_qmp_dispatcher_is_servicing(MonitorQMP *mon) |
| 672 | { |
| 673 | return qmp_dispatcher_current_mon == mon; |
| 674 | } |
| 675 | |
| 676 | static void monitor_qmp_setup_handlers_bh(void *opaque) |
| 677 | { |
| 678 | MonitorQMP *mon = opaque; |
| 679 | GMainContext *context; |
| 680 | |
| 681 | assert(monitor_requires_iothread(MONITOR(mon))); |
| 682 | context = iothread_get_g_main_context(mon_iothread); |
| 683 | assert(context); |
| 684 | qemu_chr_fe_set_handlers(&mon->parent_obj.chr, monitor_can_read, |
| 685 | monitor_qmp_read, monitor_qmp_event, |
| 686 | NULL, &mon->parent_obj, context, true); |
| 687 | monitor_list_append(&mon->parent_obj); |
| 688 | qatomic_set(&mon->setup_pending, false); |
| 689 | } |
| 690 | |
| 691 | void monitor_new_qmp(const char *id, const char *chardev_id, |
| 692 | bool pretty, Error **errp) |
| 693 | { |
| 694 | g_autofree char *autoid = id ? NULL : monitor_compat_id(); |
| 695 | object_new_with_props(TYPE_MONITOR_QMP, |
| 696 | object_get_objects_root(), |
| 697 | id ? id : autoid, |
| 698 | errp, |
| 699 | "chardev", chardev_id, |
| 700 | "pretty", pretty ? "yes" : "no", |
| 701 | NULL); |
| 702 | } |
| 703 | |
| 704 | static void monitor_qmp_complete(UserCreatable *uc, Error **errp) |
| 705 | { |
| 706 | MonitorQMP *mon = MONITOR_QMP(uc); |
| 707 | UserCreatableClass *ucc_parent = |
| 708 | USER_CREATABLE_CLASS( |
| 709 | object_class_get_parent( |
| 710 | OBJECT_CLASS(MONITOR_QMP_GET_CLASS(mon)))); |
| 711 | ERRP_GUARD(); |
| 712 | |
| 713 | ucc_parent->complete(uc, errp); |
| 714 | if (*errp) { |
| 715 | return; |
| 716 | } |
| 717 | |
| 718 | qemu_chr_fe_set_echo(&mon->parent_obj.chr, true); |
| 719 | |
| 720 | if (monitor_requires_iothread(MONITOR(mon))) { |
| 721 | /* |
| 722 | * Make sure the old iowatch is gone. It's possible when |
| 723 | * e.g. the chardev is in client mode, with wait=on. |
| 724 | */ |
| 725 | remove_fd_in_watch(mon->parent_obj.chr.chr); |
| 726 | /* |
| 727 | * Clean up listener IO sources early to prevent racy fd |
| 728 | * handling between the main thread and the I/O thread. |
| 729 | */ |
| 730 | remove_listener_fd_in_watch(mon->parent_obj.chr.chr); |
| 731 | /* |
| 732 | * We can't call qemu_chr_fe_set_handlers() directly here |
| 733 | * since chardev might be running in the monitor I/O |
| 734 | * thread. Schedule a bottom half. |
| 735 | */ |
| 736 | mon->setup_pending = true; |
| 737 | aio_bh_schedule_oneshot(iothread_get_aio_context(mon_iothread), |
| 738 | monitor_qmp_setup_handlers_bh, mon); |
| 739 | /* The bottom half will add @mon to @mon_list */ |
| 740 | } else { |
| 741 | qemu_chr_fe_set_handlers(&mon->parent_obj.chr, monitor_can_read, |
| 742 | monitor_qmp_read, monitor_qmp_event, |
| 743 | NULL, &mon->parent_obj, NULL, true); |
| 744 | monitor_list_append(&mon->parent_obj); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | static void monitor_qmp_iothread_quiesce(void *opaque) |
| 749 | { |
| 750 | /* No-op: synchronization point only */ |
| 751 | } |
| 752 | |
| 753 | static bool monitor_qmp_prepare_delete(UserCreatable *uc, Error **errp) |
| 754 | { |
| 755 | Monitor *mon = MONITOR(uc); |
| 756 | MonitorQMP *qmp = MONITOR_QMP(uc); |
| 757 | |
| 758 | if (monitor_qmp_dispatcher_is_servicing(qmp)) { |
| 759 | error_setg(errp, "Cannot delete the current QMP monitor"); |
| 760 | return false; |
| 761 | } |
| 762 | |
| 763 | if (qatomic_read(&qmp->setup_pending)) { |
| 764 | error_setg(errp, "monitor is still initializing"); |
| 765 | return false; |
| 766 | } |
| 767 | |
| 768 | /* Remove from mon_list before chardev disconnect. */ |
| 769 | WITH_QEMU_LOCK_GUARD(&monitor_lock) { |
| 770 | QTAILQ_REMOVE(&mon_list, mon, entry); |
| 771 | } |
| 772 | |
| 773 | /* Cancel out_watch while gcontext still points to the right ctx. */ |
| 774 | WITH_QEMU_LOCK_GUARD(&mon->mon_lock) { |
| 775 | monitor_cancel_out_watch(mon); |
| 776 | } |
| 777 | |
| 778 | qemu_chr_fe_set_handlers(&mon->chr, NULL, NULL, NULL, NULL, |
| 779 | NULL, NULL, true); |
| 780 | |
| 781 | /* Drain requests from any in-flight monitor_qmp_read(). */ |
| 782 | monitor_qmp_drain_queue(qmp); |
| 783 | |
| 784 | WITH_QEMU_LOCK_GUARD(&mon->mon_lock) { |
| 785 | /* Disable flushes before cancel -- gcontext is already wrong. */ |
| 786 | qemu_chr_fe_set_open(&mon->chr, false); |
| 787 | monitor_cancel_out_watch(mon); |
| 788 | } |
| 789 | |
| 790 | /* Synchronize with in-flight iothread callbacks. */ |
| 791 | if (monitor_requires_iothread(mon)) { |
| 792 | aio_wait_bh_oneshot(iothread_get_aio_context(mon_iothread), |
| 793 | monitor_qmp_iothread_quiesce, NULL); |
| 794 | } |
| 795 | |
| 796 | /* Catch requests from a racing monitor_qmp_read(). */ |
| 797 | monitor_qmp_drain_queue(qmp); |
| 798 | monitor_fdsets_cleanup(); |
| 799 | |
| 800 | return true; |
| 801 | } |
| 802 | |
| 803 | static void monitor_qmp_accept_input(Monitor *mon) |
| 804 | { |
| 805 | WITH_QEMU_LOCK_GUARD(&mon->mon_lock) { |
| 806 | qemu_chr_fe_accept_input(&mon->chr); |
| 807 | } |
| 808 | } |