| 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 "monitor-internal.h" |
| 27 | #include "monitor-hmp-internal.h" |
| 28 | #include "qapi/error.h" |
| 29 | #include "qapi/opts-visitor.h" |
| 30 | #include "qapi/qapi-emit-events.h" |
| 31 | #include "qapi/qapi-visit-control.h" |
| 32 | #include "qobject/qdict.h" |
| 33 | #include "qom/object_interfaces.h" |
| 34 | #include "qemu/aio-wait.h" |
| 35 | #include "qemu/error-report.h" |
| 36 | #include "qemu/lockable.h" |
| 37 | #include "qemu/option.h" |
| 38 | #include "system/qtest.h" |
| 39 | #include "trace.h" |
| 40 | |
| 41 | /* |
| 42 | * To prevent flooding clients, events can be throttled. The |
| 43 | * throttling is calculated globally, rather than per-Monitor |
| 44 | * instance. |
| 45 | */ |
| 46 | typedef struct MonitorQAPIEventState { |
| 47 | QAPIEvent event; /* Throttling state for this event type and... */ |
| 48 | QDict *data; /* ... data, see qapi_event_throttle_equal() */ |
| 49 | QEMUTimer *timer; /* Timer for handling delayed events */ |
| 50 | QDict *qdict; /* Delayed event (if any) */ |
| 51 | } MonitorQAPIEventState; |
| 52 | |
| 53 | typedef struct { |
| 54 | int64_t rate; /* Minimum time (in ns) between two events */ |
| 55 | } MonitorQAPIEventConf; |
| 56 | |
| 57 | /* Shared monitor I/O thread */ |
| 58 | IOThread *mon_iothread; |
| 59 | |
| 60 | /* Coroutine to dispatch the requests received from I/O thread */ |
| 61 | Coroutine *qmp_dispatcher_co; |
| 62 | |
| 63 | /* |
| 64 | * Set to true when the dispatcher coroutine should terminate. Protected |
| 65 | * by monitor_lock. |
| 66 | */ |
| 67 | bool qmp_dispatcher_co_shutdown; |
| 68 | |
| 69 | /* |
| 70 | * Protects mon_list, monitor_qapi_event_state, coroutine_mon, |
| 71 | * monitor_destroyed. |
| 72 | */ |
| 73 | QemuMutex monitor_lock; |
| 74 | static GHashTable *monitor_qapi_event_state; |
| 75 | static GHashTable *coroutine_mon; /* Maps Coroutine* to Monitor* */ |
| 76 | |
| 77 | MonitorList mon_list; |
| 78 | static bool monitor_destroyed; |
| 79 | |
| 80 | int monitor_device_index; |
| 81 | |
| 82 | OBJECT_DEFINE_TYPE_EXTENDED(Monitor, monitor, MONITOR, OBJECT, true, |
| 83 | { TYPE_USER_CREATABLE }, {}); |
| 84 | |
| 85 | static void monitor_finalize(Object *obj) |
| 86 | { |
| 87 | Monitor *mon = MONITOR(obj); |
| 88 | |
| 89 | if (mon->accept_input_bh) { |
| 90 | qemu_bh_delete(mon->accept_input_bh); |
| 91 | } |
| 92 | g_free(mon->chardev_id); |
| 93 | qemu_chr_fe_deinit(&mon->chr, false); |
| 94 | g_string_free(mon->outbuf, true); |
| 95 | qemu_mutex_destroy(&mon->mon_lock); |
| 96 | } |
| 97 | |
| 98 | static char *monitor_get_chardev_id(Object *obj, Error **errp) |
| 99 | { |
| 100 | Monitor *mon = MONITOR(obj); |
| 101 | |
| 102 | return g_strdup(mon->chardev_id); |
| 103 | } |
| 104 | |
| 105 | static void monitor_set_chardev_id(Object *obj, const char *str, Error **errp) |
| 106 | { |
| 107 | Monitor *mon = MONITOR(obj); |
| 108 | |
| 109 | g_free(mon->chardev_id); |
| 110 | mon->chardev_id = g_strdup(str); |
| 111 | } |
| 112 | |
| 113 | static void monitor_complete(UserCreatable *uc, Error **errp); |
| 114 | |
| 115 | static void monitor_class_init(ObjectClass *cls, const void *data) |
| 116 | { |
| 117 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(cls); |
| 118 | |
| 119 | object_class_property_add_str(cls, "chardev", |
| 120 | monitor_get_chardev_id, |
| 121 | monitor_set_chardev_id); |
| 122 | |
| 123 | ucc->complete = monitor_complete; |
| 124 | } |
| 125 | |
| 126 | static void monitor_init(Object *obj) |
| 127 | { |
| 128 | Monitor *mon = MONITOR(obj); |
| 129 | |
| 130 | qemu_mutex_init(&mon->mon_lock); |
| 131 | mon->outbuf = g_string_new(NULL); |
| 132 | } |
| 133 | |
| 134 | Monitor *monitor_cur(void) |
| 135 | { |
| 136 | Monitor *mon; |
| 137 | |
| 138 | qemu_mutex_lock(&monitor_lock); |
| 139 | mon = g_hash_table_lookup(coroutine_mon, qemu_coroutine_self()); |
| 140 | qemu_mutex_unlock(&monitor_lock); |
| 141 | |
| 142 | return mon; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Sets a new current monitor and returns the old one. |
| 147 | * |
| 148 | * If a non-NULL monitor is set for a coroutine, another call |
| 149 | * resetting it to NULL is required before the coroutine terminates, |
| 150 | * otherwise a stale entry would remain in the hash table. |
| 151 | */ |
| 152 | Monitor *monitor_set_cur(Coroutine *co, Monitor *mon) |
| 153 | { |
| 154 | Monitor *old_monitor = monitor_cur(); |
| 155 | |
| 156 | qemu_mutex_lock(&monitor_lock); |
| 157 | if (mon) { |
| 158 | g_hash_table_replace(coroutine_mon, co, mon); |
| 159 | } else { |
| 160 | g_hash_table_remove(coroutine_mon, co); |
| 161 | } |
| 162 | qemu_mutex_unlock(&monitor_lock); |
| 163 | |
| 164 | return old_monitor; |
| 165 | } |
| 166 | |
| 167 | bool monitor_requires_iothread(const Monitor *mon) |
| 168 | { |
| 169 | MonitorClass *cls = MONITOR_GET_CLASS(mon); |
| 170 | return cls->requires_iothread && cls->requires_iothread(mon); |
| 171 | } |
| 172 | |
| 173 | static gboolean monitor_unblocked(void *do_not_use, GIOCondition cond, |
| 174 | void *opaque) |
| 175 | { |
| 176 | Monitor *mon = opaque; |
| 177 | |
| 178 | QEMU_LOCK_GUARD(&mon->mon_lock); |
| 179 | mon->out_watch = 0; |
| 180 | monitor_flush_locked(mon); |
| 181 | return G_SOURCE_REMOVE; |
| 182 | } |
| 183 | |
| 184 | /* Cancel a pending out_watch GSource. Caller must hold mon_lock. */ |
| 185 | void monitor_cancel_out_watch(Monitor *mon) |
| 186 | { |
| 187 | if (mon->out_watch) { |
| 188 | GMainContext *ctx = NULL; |
| 189 | GSource *src; |
| 190 | |
| 191 | if (monitor_requires_iothread(mon)) { |
| 192 | ctx = iothread_get_g_main_context(mon_iothread); |
| 193 | } |
| 194 | src = g_main_context_find_source_by_id(ctx, mon->out_watch); |
| 195 | if (!src && ctx) { |
| 196 | /* Handler disconnect may have reset gcontext to NULL. */ |
| 197 | src = g_main_context_find_source_by_id(NULL, mon->out_watch); |
| 198 | } |
| 199 | if (src) { |
| 200 | g_source_destroy(src); |
| 201 | } |
| 202 | mon->out_watch = 0; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /* Caller must hold mon->mon_lock */ |
| 207 | void monitor_flush_locked(Monitor *mon) |
| 208 | { |
| 209 | int rc; |
| 210 | size_t len; |
| 211 | const char *buf; |
| 212 | |
| 213 | /* |
| 214 | * When used by QMP human-monitor-command, no chardev |
| 215 | * will be connected, as we want to just collect the |
| 216 | * output in the buffer |
| 217 | */ |
| 218 | if (!mon->chr.fe_is_open) { |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | buf = mon->outbuf->str; |
| 223 | len = mon->outbuf->len; |
| 224 | |
| 225 | if (len && !mon->mux_out) { |
| 226 | rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len); |
| 227 | if ((rc < 0 && errno != EAGAIN) || (rc == len)) { |
| 228 | /* all flushed or error */ |
| 229 | g_string_truncate(mon->outbuf, 0); |
| 230 | return; |
| 231 | } |
| 232 | if (rc > 0) { |
| 233 | /* partial write */ |
| 234 | g_string_erase(mon->outbuf, 0, rc); |
| 235 | } |
| 236 | if (mon->out_watch == 0) { |
| 237 | mon->out_watch = |
| 238 | qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP, |
| 239 | monitor_unblocked, mon); |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | void monitor_flush(Monitor *mon) |
| 245 | { |
| 246 | QEMU_LOCK_GUARD(&mon->mon_lock); |
| 247 | monitor_flush_locked(mon); |
| 248 | } |
| 249 | |
| 250 | /* flush at every end of line */ |
| 251 | int monitor_puts_locked(Monitor *mon, const char *str) |
| 252 | { |
| 253 | int i; |
| 254 | char c; |
| 255 | |
| 256 | for (i = 0; str[i]; i++) { |
| 257 | c = str[i]; |
| 258 | if (c == '\n') { |
| 259 | g_string_append_c(mon->outbuf, '\r'); |
| 260 | } |
| 261 | g_string_append_c(mon->outbuf, c); |
| 262 | if (c == '\n') { |
| 263 | monitor_flush_locked(mon); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return i; |
| 268 | } |
| 269 | |
| 270 | int monitor_puts(Monitor *mon, const char *str) |
| 271 | { |
| 272 | QEMU_LOCK_GUARD(&mon->mon_lock); |
| 273 | return monitor_puts_locked(mon, str); |
| 274 | } |
| 275 | |
| 276 | static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = { |
| 277 | /* Limit guest-triggerable events to 1 per second */ |
| 278 | [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS }, |
| 279 | [QAPI_EVENT_BLOCK_IO_ERROR] = { 1000 * SCALE_MS }, |
| 280 | [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS }, |
| 281 | [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS }, |
| 282 | [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS }, |
| 283 | [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS }, |
| 284 | [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS }, |
| 285 | [QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE] = { 1000 * SCALE_MS }, |
| 286 | [QAPI_EVENT_HV_BALLOON_STATUS_REPORT] = { 1000 * SCALE_MS }, |
| 287 | }; |
| 288 | |
| 289 | /* |
| 290 | * Return the clock to use for recording an event's time. |
| 291 | * It's QEMU_CLOCK_REALTIME, except for qtests it's |
| 292 | * QEMU_CLOCK_VIRTUAL, to support testing rate limits. |
| 293 | * Beware: result is invalid before configure_accelerator(). |
| 294 | */ |
| 295 | static inline QEMUClockType monitor_get_event_clock(void) |
| 296 | { |
| 297 | return qtest_enabled() ? QEMU_CLOCK_VIRTUAL : QEMU_CLOCK_REALTIME; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Broadcast an event to all monitors. |
| 302 | * @qdict is the event object. Its member "event" must match @event. |
| 303 | * Caller must hold monitor_lock. |
| 304 | */ |
| 305 | static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict) |
| 306 | { |
| 307 | Monitor *mon; |
| 308 | |
| 309 | trace_monitor_protocol_event_emit(event, qdict); |
| 310 | QTAILQ_FOREACH(mon, &mon_list, entry) { |
| 311 | MonitorClass *cls = MONITOR_GET_CLASS(mon); |
| 312 | if (cls->emit_event) { |
| 313 | cls->emit_event(mon, event, qdict); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | static void monitor_qapi_event_handler(void *opaque); |
| 319 | |
| 320 | /* |
| 321 | * Queue a new event for emission to Monitor instances, |
| 322 | * applying any rate limiting if required. |
| 323 | */ |
| 324 | static void |
| 325 | monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict) |
| 326 | { |
| 327 | MonitorQAPIEventConf *evconf; |
| 328 | MonitorQAPIEventState *evstate; |
| 329 | bool throttled; |
| 330 | |
| 331 | assert(event < QAPI_EVENT__MAX); |
| 332 | evconf = &monitor_qapi_event_conf[event]; |
| 333 | trace_monitor_protocol_event_queue(event, qdict, evconf->rate); |
| 334 | throttled = evconf->rate; |
| 335 | |
| 336 | /* |
| 337 | * Rate limit BLOCK_IO_ERROR only for action != "stop". |
| 338 | * |
| 339 | * If the VM is stopped after an I/O error, this is important information |
| 340 | * for the management tool to keep track of the state of QEMU and we can't |
| 341 | * merge any events. At the same time, stopping the VM means that the guest |
| 342 | * can't send additional requests and the number of events is already |
| 343 | * limited, so we can do without rate limiting. |
| 344 | */ |
| 345 | if (event == QAPI_EVENT_BLOCK_IO_ERROR) { |
| 346 | QDict *data = qobject_to(QDict, qdict_get(qdict, "data")); |
| 347 | const char *action = qdict_get_str(data, "action"); |
| 348 | if (!strcmp(action, "stop")) { |
| 349 | throttled = false; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | QEMU_LOCK_GUARD(&monitor_lock); |
| 354 | |
| 355 | if (!throttled) { |
| 356 | /* Unthrottled event */ |
| 357 | monitor_qapi_event_emit(event, qdict); |
| 358 | } else { |
| 359 | QDict *data = qobject_to(QDict, qdict_get(qdict, "data")); |
| 360 | MonitorQAPIEventState key = { .event = event, .data = data }; |
| 361 | |
| 362 | evstate = g_hash_table_lookup(monitor_qapi_event_state, &key); |
| 363 | assert(!evstate || timer_pending(evstate->timer)); |
| 364 | |
| 365 | if (evstate) { |
| 366 | /* |
| 367 | * Timer is pending for (at least) evconf->rate ns after |
| 368 | * last send. Store event for sending when timer fires, |
| 369 | * replacing a prior stored event if any. |
| 370 | */ |
| 371 | qobject_unref(evstate->qdict); |
| 372 | evstate->qdict = qobject_ref(qdict); |
| 373 | } else { |
| 374 | /* |
| 375 | * Last send was (at least) evconf->rate ns ago. |
| 376 | * Send immediately, and arm the timer to call |
| 377 | * monitor_qapi_event_handler() in evconf->rate ns. Any |
| 378 | * events arriving before then will be delayed until then. |
| 379 | */ |
| 380 | int64_t now = qemu_clock_get_ns(monitor_get_event_clock()); |
| 381 | |
| 382 | monitor_qapi_event_emit(event, qdict); |
| 383 | |
| 384 | evstate = g_new(MonitorQAPIEventState, 1); |
| 385 | evstate->event = event; |
| 386 | evstate->data = qobject_ref(data); |
| 387 | evstate->qdict = NULL; |
| 388 | evstate->timer = timer_new_ns(monitor_get_event_clock(), |
| 389 | monitor_qapi_event_handler, |
| 390 | evstate); |
| 391 | g_hash_table_add(monitor_qapi_event_state, evstate); |
| 392 | timer_mod_ns(evstate->timer, now + evconf->rate); |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | void qapi_event_emit(QAPIEvent event, QDict *qdict) |
| 398 | { |
| 399 | /* |
| 400 | * monitor_qapi_event_queue_no_reenter() is not reentrant: it |
| 401 | * would deadlock on monitor_lock. Work around by queueing |
| 402 | * events in thread-local storage. |
| 403 | * TODO: remove this, make it re-enter safe. |
| 404 | */ |
| 405 | typedef struct MonitorQapiEvent { |
| 406 | QAPIEvent event; |
| 407 | QDict *qdict; |
| 408 | QSIMPLEQ_ENTRY(MonitorQapiEvent) entry; |
| 409 | } MonitorQapiEvent; |
| 410 | static __thread QSIMPLEQ_HEAD(, MonitorQapiEvent) event_queue; |
| 411 | static __thread bool reentered; |
| 412 | MonitorQapiEvent *ev; |
| 413 | |
| 414 | if (!reentered) { |
| 415 | QSIMPLEQ_INIT(&event_queue); |
| 416 | } |
| 417 | |
| 418 | ev = g_new(MonitorQapiEvent, 1); |
| 419 | ev->qdict = qobject_ref(qdict); |
| 420 | ev->event = event; |
| 421 | QSIMPLEQ_INSERT_TAIL(&event_queue, ev, entry); |
| 422 | if (reentered) { |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | reentered = true; |
| 427 | |
| 428 | while ((ev = QSIMPLEQ_FIRST(&event_queue)) != NULL) { |
| 429 | QSIMPLEQ_REMOVE_HEAD(&event_queue, entry); |
| 430 | monitor_qapi_event_queue_no_reenter(ev->event, ev->qdict); |
| 431 | qobject_unref(ev->qdict); |
| 432 | g_free(ev); |
| 433 | } |
| 434 | |
| 435 | reentered = false; |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * This function runs evconf->rate ns after sending a throttled |
| 440 | * event. |
| 441 | * If another event has since been stored, send it. |
| 442 | */ |
| 443 | static void monitor_qapi_event_handler(void *opaque) |
| 444 | { |
| 445 | MonitorQAPIEventState *evstate = opaque; |
| 446 | MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event]; |
| 447 | |
| 448 | trace_monitor_protocol_event_handler(evstate->event, evstate->qdict); |
| 449 | QEMU_LOCK_GUARD(&monitor_lock); |
| 450 | |
| 451 | if (evstate->qdict) { |
| 452 | int64_t now = qemu_clock_get_ns(monitor_get_event_clock()); |
| 453 | |
| 454 | monitor_qapi_event_emit(evstate->event, evstate->qdict); |
| 455 | qobject_unref(evstate->qdict); |
| 456 | evstate->qdict = NULL; |
| 457 | timer_mod_ns(evstate->timer, now + evconf->rate); |
| 458 | } else { |
| 459 | g_hash_table_remove(monitor_qapi_event_state, evstate); |
| 460 | qobject_unref(evstate->data); |
| 461 | timer_free(evstate->timer); |
| 462 | g_free(evstate); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | static unsigned int qapi_event_throttle_hash(const void *key) |
| 467 | { |
| 468 | const MonitorQAPIEventState *evstate = key; |
| 469 | unsigned int hash = evstate->event * 255; |
| 470 | |
| 471 | if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) { |
| 472 | hash += g_str_hash(qdict_get_str(evstate->data, "id")); |
| 473 | } |
| 474 | |
| 475 | if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) { |
| 476 | hash += g_str_hash(qdict_get_str(evstate->data, "node-name")); |
| 477 | } |
| 478 | |
| 479 | if (evstate->event == QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE || |
| 480 | evstate->event == QAPI_EVENT_BLOCK_IO_ERROR) { |
| 481 | hash += g_str_hash(qdict_get_str(evstate->data, "qom-path")); |
| 482 | } |
| 483 | |
| 484 | return hash; |
| 485 | } |
| 486 | |
| 487 | static gboolean qapi_event_throttle_equal(const void *a, const void *b) |
| 488 | { |
| 489 | const MonitorQAPIEventState *eva = a; |
| 490 | const MonitorQAPIEventState *evb = b; |
| 491 | |
| 492 | if (eva->event != evb->event) { |
| 493 | return FALSE; |
| 494 | } |
| 495 | |
| 496 | if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) { |
| 497 | return !strcmp(qdict_get_str(eva->data, "id"), |
| 498 | qdict_get_str(evb->data, "id")); |
| 499 | } |
| 500 | |
| 501 | if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) { |
| 502 | return !strcmp(qdict_get_str(eva->data, "node-name"), |
| 503 | qdict_get_str(evb->data, "node-name")); |
| 504 | } |
| 505 | |
| 506 | if (eva->event == QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE || |
| 507 | eva->event == QAPI_EVENT_BLOCK_IO_ERROR) { |
| 508 | return !strcmp(qdict_get_str(eva->data, "qom-path"), |
| 509 | qdict_get_str(evb->data, "qom-path")); |
| 510 | } |
| 511 | |
| 512 | return TRUE; |
| 513 | } |
| 514 | |
| 515 | void monitor_suspend(Monitor *mon) |
| 516 | { |
| 517 | qatomic_inc(&mon->suspend_cnt); |
| 518 | |
| 519 | if (monitor_requires_iothread(mon)) { |
| 520 | /* |
| 521 | * Kick I/O thread to make sure this takes effect. It'll be |
| 522 | * evaluated again in prepare() of the watch object. |
| 523 | */ |
| 524 | aio_notify(iothread_get_aio_context(mon_iothread)); |
| 525 | } |
| 526 | |
| 527 | trace_monitor_suspend(mon, 1); |
| 528 | } |
| 529 | |
| 530 | static void monitor_accept_input(void *opaque) |
| 531 | { |
| 532 | Monitor *mon = opaque; |
| 533 | MonitorClass *cls = MONITOR_GET_CLASS(mon); |
| 534 | |
| 535 | cls->accept_input(mon); |
| 536 | } |
| 537 | |
| 538 | void monitor_resume(Monitor *mon) |
| 539 | { |
| 540 | if (qatomic_dec_fetch(&mon->suspend_cnt) == 0) { |
| 541 | qemu_bh_schedule(mon->accept_input_bh); |
| 542 | } |
| 543 | |
| 544 | trace_monitor_suspend(mon, -1); |
| 545 | } |
| 546 | |
| 547 | int monitor_can_read(void *opaque) |
| 548 | { |
| 549 | Monitor *mon = opaque; |
| 550 | |
| 551 | return !qatomic_read(&mon->suspend_cnt); |
| 552 | } |
| 553 | |
| 554 | void monitor_list_append(Monitor *mon) |
| 555 | { |
| 556 | qemu_mutex_lock(&monitor_lock); |
| 557 | /* |
| 558 | * This prevents inserting new monitors during monitor_cleanup(). |
| 559 | * A cleaner solution would involve the main thread telling other |
| 560 | * threads to terminate, waiting for their termination. |
| 561 | */ |
| 562 | if (!monitor_destroyed) { |
| 563 | QTAILQ_INSERT_HEAD(&mon_list, mon, entry); |
| 564 | mon = NULL; |
| 565 | } |
| 566 | qemu_mutex_unlock(&monitor_lock); |
| 567 | |
| 568 | if (mon) { |
| 569 | object_unparent(OBJECT(mon)); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | void monitor_cleanup(void) |
| 574 | { |
| 575 | /* |
| 576 | * The dispatcher needs to stop before destroying the monitor and |
| 577 | * the I/O thread. |
| 578 | * |
| 579 | * We need to poll both qemu_aio_context and iohandler_ctx to make |
| 580 | * sure that the dispatcher coroutine keeps making progress and |
| 581 | * eventually terminates. qemu_aio_context is automatically |
| 582 | * polled by calling AIO_WAIT_WHILE_UNLOCKED on it, but we must poll |
| 583 | * iohandler_ctx manually. |
| 584 | * |
| 585 | * Letting the iothread continue while shutting down the dispatcher |
| 586 | * means that new requests may still be coming in. This is okay, |
| 587 | * we'll just leave them in the queue without sending a response |
| 588 | * and object finalization will free them. |
| 589 | */ |
| 590 | WITH_QEMU_LOCK_GUARD(&monitor_lock) { |
| 591 | qmp_dispatcher_co_shutdown = true; |
| 592 | } |
| 593 | qmp_dispatcher_co_wake(); |
| 594 | |
| 595 | AIO_WAIT_WHILE_UNLOCKED(NULL, |
| 596 | (aio_poll(iohandler_get_aio_context(), false), |
| 597 | qatomic_read(&qmp_dispatcher_co))); |
| 598 | |
| 599 | /* |
| 600 | * We need to explicitly stop the I/O thread (but not destroy it), |
| 601 | * clean up the monitor resources, then destroy the I/O thread since |
| 602 | * we need to unregister from chardev below in object |
| 603 | * finalization, and chardev is not thread-safe yet |
| 604 | */ |
| 605 | if (mon_iothread) { |
| 606 | iothread_stop(mon_iothread); |
| 607 | } |
| 608 | |
| 609 | /* Flush output buffers and destroy monitors */ |
| 610 | qemu_mutex_lock(&monitor_lock); |
| 611 | monitor_destroyed = true; |
| 612 | while (!QTAILQ_EMPTY(&mon_list)) { |
| 613 | Monitor *mon = QTAILQ_FIRST(&mon_list); |
| 614 | QTAILQ_REMOVE(&mon_list, mon, entry); |
| 615 | /* Permit QAPI event emission from character frontend release */ |
| 616 | qemu_mutex_unlock(&monitor_lock); |
| 617 | monitor_flush(mon); |
| 618 | qemu_mutex_lock(&monitor_lock); |
| 619 | object_unparent(OBJECT(mon)); |
| 620 | } |
| 621 | qemu_mutex_unlock(&monitor_lock); |
| 622 | |
| 623 | if (mon_iothread) { |
| 624 | iothread_destroy(mon_iothread); |
| 625 | mon_iothread = NULL; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * Initialize static vars that have no deps on external |
| 631 | * module initialization, and are required for external |
| 632 | * functions to call things like monitor_cur() |
| 633 | */ |
| 634 | static void __attribute__((__constructor__(QEMU_CONSTRUCTOR_EARLY))) |
| 635 | monitor_init_static(void) |
| 636 | { |
| 637 | qemu_mutex_init(&monitor_lock); |
| 638 | coroutine_mon = g_hash_table_new(NULL, NULL); |
| 639 | monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash, |
| 640 | qapi_event_throttle_equal); |
| 641 | } |
| 642 | |
| 643 | void monitor_init_globals(void) |
| 644 | { |
| 645 | /* |
| 646 | * The dispatcher BH must run in the main loop thread, since we |
| 647 | * have commands assuming that context. It would be nice to get |
| 648 | * rid of those assumptions. |
| 649 | */ |
| 650 | qmp_dispatcher_co = qemu_coroutine_create(monitor_qmp_dispatcher_co, NULL); |
| 651 | aio_co_schedule(iohandler_get_aio_context(), qmp_dispatcher_co); |
| 652 | } |
| 653 | |
| 654 | char *monitor_compat_id(void) |
| 655 | { |
| 656 | static int monitor_device_index; |
| 657 | |
| 658 | return g_strdup_printf("compat_monitor%d", monitor_device_index++); |
| 659 | } |
| 660 | |
| 661 | static void monitor_complete(UserCreatable *uc, Error **errp) |
| 662 | { |
| 663 | Monitor *mon = MONITOR(uc); |
| 664 | AioContext *ctx; |
| 665 | |
| 666 | if (mon->chardev_id) { |
| 667 | Chardev *chr = qemu_chr_find(mon->chardev_id); |
| 668 | if (chr == NULL) { |
| 669 | error_setg(errp, "chardev \"%s\" not found", mon->chardev_id); |
| 670 | return; |
| 671 | } |
| 672 | |
| 673 | if (!qemu_chr_fe_init(&mon->chr, chr, errp)) { |
| 674 | return; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | if (monitor_requires_iothread(mon)) { |
| 679 | if (!mon_iothread) { |
| 680 | mon_iothread = iothread_create("mon_iothread", &error_abort); |
| 681 | } |
| 682 | |
| 683 | ctx = iothread_get_aio_context(mon_iothread); |
| 684 | } else { |
| 685 | ctx = qemu_get_aio_context(); |
| 686 | } |
| 687 | mon->accept_input_bh = aio_bh_new(ctx, monitor_accept_input, mon); |
| 688 | } |
| 689 | |
| 690 | int monitor_new(MonitorOptions *opts, bool allow_hmp, Error **errp) |
| 691 | { |
| 692 | ERRP_GUARD(); |
| 693 | |
| 694 | if (!opts->has_mode) { |
| 695 | #ifdef CONFIG_HMP |
| 696 | opts->mode = allow_hmp ? MONITOR_MODE_READLINE : MONITOR_MODE_CONTROL; |
| 697 | #else |
| 698 | if (allow_hmp) { |
| 699 | error_setg(errp, "HMP support is not built in this QEMU"); |
| 700 | return -1; |
| 701 | } |
| 702 | opts->mode = MONITOR_MODE_CONTROL; |
| 703 | #endif |
| 704 | } |
| 705 | |
| 706 | switch (opts->mode) { |
| 707 | case MONITOR_MODE_CONTROL: |
| 708 | monitor_new_qmp(opts->id, opts->chardev, opts->pretty, errp); |
| 709 | break; |
| 710 | #ifdef CONFIG_HMP |
| 711 | case MONITOR_MODE_READLINE: |
| 712 | if (!allow_hmp) { |
| 713 | error_setg(errp, "Only QMP is supported"); |
| 714 | return -1; |
| 715 | } |
| 716 | if (opts->pretty) { |
| 717 | error_setg(errp, "'pretty' is not compatible with HMP monitors"); |
| 718 | return -1; |
| 719 | } |
| 720 | monitor_new_hmp(opts->id, opts->chardev, true, errp); |
| 721 | break; |
| 722 | #endif /* CONFIG_HMP */ |
| 723 | default: |
| 724 | g_assert_not_reached(); |
| 725 | } |
| 726 | |
| 727 | return *errp ? -1 : 0; |
| 728 | } |
| 729 | |
| 730 | int monitor_new_opts(QemuOpts *opts, Error **errp) |
| 731 | { |
| 732 | Visitor *v; |
| 733 | MonitorOptions *options; |
| 734 | int ret; |
| 735 | |
| 736 | #ifndef CONFIG_HMP |
| 737 | const char *mode = qemu_opt_get(opts, "mode"); |
| 738 | /* readline is HMP.. */ |
| 739 | if (mode && g_str_equal(mode, "readline")) { |
| 740 | error_setg(errp, "HMP monitor is not available," |
| 741 | " use '-qmp' instead of '-monitor'"); |
| 742 | return -1; |
| 743 | } |
| 744 | #endif |
| 745 | |
| 746 | v = opts_visitor_new(opts); |
| 747 | visit_type_MonitorOptions(v, NULL, &options, errp); |
| 748 | visit_free(v); |
| 749 | if (!options) { |
| 750 | return -1; |
| 751 | } |
| 752 | |
| 753 | ret = monitor_new(options, true, errp); |
| 754 | qapi_free_MonitorOptions(options); |
| 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | QemuOptsList qemu_mon_opts = { |
| 759 | .name = "mon", |
| 760 | .implied_opt_name = "chardev", |
| 761 | .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head), |
| 762 | .desc = { |
| 763 | { |
| 764 | .name = "mode", |
| 765 | .type = QEMU_OPT_STRING, |
| 766 | },{ |
| 767 | .name = "chardev", |
| 768 | .type = QEMU_OPT_STRING, |
| 769 | },{ |
| 770 | .name = "pretty", |
| 771 | .type = QEMU_OPT_BOOL, |
| 772 | }, |
| 773 | { /* end of list */ } |
| 774 | }, |
| 775 | }; |