4 * Copyright (c) 2003-2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include "monitor-internal.h"
27 #include "qapi/error.h"
28 #include "qapi/qapi-emit-events.h"
29 #include "qapi/qmp/qdict.h"
30 #include "qapi/qmp/qstring.h"
31 #include "qemu/error-report.h"
32 #include "qemu/option.h"
33 #include "sysemu/qtest.h"
37 * To prevent flooding clients, events can be throttled. The
38 * throttling is calculated globally, rather than per-Monitor
41 typedef struct MonitorQAPIEventState
{
42 QAPIEvent event
; /* Throttling state for this event type and... */
43 QDict
*data
; /* ... data, see qapi_event_throttle_equal() */
44 QEMUTimer
*timer
; /* Timer for handling delayed events */
45 QDict
*qdict
; /* Delayed event (if any) */
46 } MonitorQAPIEventState
;
49 int64_t rate
; /* Minimum time (in ns) between two events */
50 } MonitorQAPIEventConf
;
52 /* Shared monitor I/O thread */
53 IOThread
*mon_iothread
;
55 /* Bottom half to dispatch the requests received from I/O thread */
56 QEMUBH
*qmp_dispatcher_bh
;
58 /* Protects mon_list, monitor_qapi_event_state, monitor_destroyed. */
59 QemuMutex monitor_lock
;
60 static GHashTable
*monitor_qapi_event_state
;
64 static bool monitor_destroyed
;
66 __thread Monitor
*cur_mon
;
69 * Is the current monitor, if any, a QMP monitor?
71 bool monitor_cur_is_qmp(void)
73 return cur_mon
&& monitor_is_qmp(cur_mon
);
77 * Is @mon is using readline?
78 * Note: not all HMP monitors use readline, e.g., gdbserver has a
79 * non-interactive HMP monitor, so readline is not used there.
81 static inline bool monitor_uses_readline(const MonitorHMP
*mon
)
83 return mon
->use_readline
;
86 static inline bool monitor_is_hmp_non_interactive(const Monitor
*mon
)
88 if (monitor_is_qmp(mon
)) {
92 return !monitor_uses_readline(container_of(mon
, MonitorHMP
, common
));
95 static void monitor_flush_locked(Monitor
*mon
);
97 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
100 Monitor
*mon
= opaque
;
102 qemu_mutex_lock(&mon
->mon_lock
);
104 monitor_flush_locked(mon
);
105 qemu_mutex_unlock(&mon
->mon_lock
);
109 /* Caller must hold mon->mon_lock */
110 static void monitor_flush_locked(Monitor
*mon
)
116 if (mon
->skip_flush
) {
120 buf
= qstring_get_str(mon
->outbuf
);
121 len
= qstring_get_length(mon
->outbuf
);
123 if (len
&& !mon
->mux_out
) {
124 rc
= qemu_chr_fe_write(&mon
->chr
, (const uint8_t *) buf
, len
);
125 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
126 /* all flushed or error */
127 qobject_unref(mon
->outbuf
);
128 mon
->outbuf
= qstring_new();
133 QString
*tmp
= qstring_from_str(buf
+ rc
);
134 qobject_unref(mon
->outbuf
);
137 if (mon
->out_watch
== 0) {
139 qemu_chr_fe_add_watch(&mon
->chr
, G_IO_OUT
| G_IO_HUP
,
140 monitor_unblocked
, mon
);
145 void monitor_flush(Monitor
*mon
)
147 qemu_mutex_lock(&mon
->mon_lock
);
148 monitor_flush_locked(mon
);
149 qemu_mutex_unlock(&mon
->mon_lock
);
152 /* flush at every end of line */
153 int monitor_puts(Monitor
*mon
, const char *str
)
158 qemu_mutex_lock(&mon
->mon_lock
);
159 for (i
= 0; str
[i
]; i
++) {
162 qstring_append_chr(mon
->outbuf
, '\r');
164 qstring_append_chr(mon
->outbuf
, c
);
166 monitor_flush_locked(mon
);
169 qemu_mutex_unlock(&mon
->mon_lock
);
174 int monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
183 if (monitor_is_qmp(mon
)) {
187 buf
= g_strdup_vprintf(fmt
, ap
);
188 n
= monitor_puts(mon
, buf
);
193 int monitor_printf(Monitor
*mon
, const char *fmt
, ...)
199 ret
= monitor_vprintf(mon
, fmt
, ap
);
205 * Print to current monitor if we have one, else to stderr.
207 int error_vprintf(const char *fmt
, va_list ap
)
209 if (cur_mon
&& !monitor_cur_is_qmp()) {
210 return monitor_vprintf(cur_mon
, fmt
, ap
);
212 return vfprintf(stderr
, fmt
, ap
);
215 int error_vprintf_unless_qmp(const char *fmt
, va_list ap
)
218 return vfprintf(stderr
, fmt
, ap
);
220 if (!monitor_cur_is_qmp()) {
221 return monitor_vprintf(cur_mon
, fmt
, ap
);
227 static MonitorQAPIEventConf monitor_qapi_event_conf
[QAPI_EVENT__MAX
] = {
228 /* Limit guest-triggerable events to 1 per second */
229 [QAPI_EVENT_RTC_CHANGE
] = { 1000 * SCALE_MS
},
230 [QAPI_EVENT_WATCHDOG
] = { 1000 * SCALE_MS
},
231 [QAPI_EVENT_BALLOON_CHANGE
] = { 1000 * SCALE_MS
},
232 [QAPI_EVENT_QUORUM_REPORT_BAD
] = { 1000 * SCALE_MS
},
233 [QAPI_EVENT_QUORUM_FAILURE
] = { 1000 * SCALE_MS
},
234 [QAPI_EVENT_VSERPORT_CHANGE
] = { 1000 * SCALE_MS
},
238 * Return the clock to use for recording an event's time.
239 * It's QEMU_CLOCK_REALTIME, except for qtests it's
240 * QEMU_CLOCK_VIRTUAL, to support testing rate limits.
241 * Beware: result is invalid before configure_accelerator().
243 static inline QEMUClockType
monitor_get_event_clock(void)
245 return qtest_enabled() ? QEMU_CLOCK_VIRTUAL
: QEMU_CLOCK_REALTIME
;
249 * Broadcast an event to all monitors.
250 * @qdict is the event object. Its member "event" must match @event.
251 * Caller must hold monitor_lock.
253 static void monitor_qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
258 trace_monitor_protocol_event_emit(event
, qdict
);
259 QTAILQ_FOREACH(mon
, &mon_list
, entry
) {
260 if (!monitor_is_qmp(mon
)) {
264 qmp_mon
= container_of(mon
, MonitorQMP
, common
);
265 if (qmp_mon
->commands
!= &qmp_cap_negotiation_commands
) {
266 qmp_send_response(qmp_mon
, qdict
);
271 static void monitor_qapi_event_handler(void *opaque
);
274 * Queue a new event for emission to Monitor instances,
275 * applying any rate limiting if required.
278 monitor_qapi_event_queue_no_reenter(QAPIEvent event
, QDict
*qdict
)
280 MonitorQAPIEventConf
*evconf
;
281 MonitorQAPIEventState
*evstate
;
283 assert(event
< QAPI_EVENT__MAX
);
284 evconf
= &monitor_qapi_event_conf
[event
];
285 trace_monitor_protocol_event_queue(event
, qdict
, evconf
->rate
);
287 qemu_mutex_lock(&monitor_lock
);
290 /* Unthrottled event */
291 monitor_qapi_event_emit(event
, qdict
);
293 QDict
*data
= qobject_to(QDict
, qdict_get(qdict
, "data"));
294 MonitorQAPIEventState key
= { .event
= event
, .data
= data
};
296 evstate
= g_hash_table_lookup(monitor_qapi_event_state
, &key
);
297 assert(!evstate
|| timer_pending(evstate
->timer
));
301 * Timer is pending for (at least) evconf->rate ns after
302 * last send. Store event for sending when timer fires,
303 * replacing a prior stored event if any.
305 qobject_unref(evstate
->qdict
);
306 evstate
->qdict
= qobject_ref(qdict
);
309 * Last send was (at least) evconf->rate ns ago.
310 * Send immediately, and arm the timer to call
311 * monitor_qapi_event_handler() in evconf->rate ns. Any
312 * events arriving before then will be delayed until then.
314 int64_t now
= qemu_clock_get_ns(monitor_get_event_clock());
316 monitor_qapi_event_emit(event
, qdict
);
318 evstate
= g_new(MonitorQAPIEventState
, 1);
319 evstate
->event
= event
;
320 evstate
->data
= qobject_ref(data
);
321 evstate
->qdict
= NULL
;
322 evstate
->timer
= timer_new_ns(monitor_get_event_clock(),
323 monitor_qapi_event_handler
,
325 g_hash_table_add(monitor_qapi_event_state
, evstate
);
326 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
330 qemu_mutex_unlock(&monitor_lock
);
333 void qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
336 * monitor_qapi_event_queue_no_reenter() is not reentrant: it
337 * would deadlock on monitor_lock. Work around by queueing
338 * events in thread-local storage.
339 * TODO: remove this, make it re-enter safe.
341 typedef struct MonitorQapiEvent
{
344 QSIMPLEQ_ENTRY(MonitorQapiEvent
) entry
;
346 static __thread
QSIMPLEQ_HEAD(, MonitorQapiEvent
) event_queue
;
347 static __thread
bool reentered
;
348 MonitorQapiEvent
*ev
;
351 QSIMPLEQ_INIT(&event_queue
);
354 ev
= g_new(MonitorQapiEvent
, 1);
355 ev
->qdict
= qobject_ref(qdict
);
357 QSIMPLEQ_INSERT_TAIL(&event_queue
, ev
, entry
);
364 while ((ev
= QSIMPLEQ_FIRST(&event_queue
)) != NULL
) {
365 QSIMPLEQ_REMOVE_HEAD(&event_queue
, entry
);
366 monitor_qapi_event_queue_no_reenter(ev
->event
, ev
->qdict
);
367 qobject_unref(ev
->qdict
);
375 * This function runs evconf->rate ns after sending a throttled
377 * If another event has since been stored, send it.
379 static void monitor_qapi_event_handler(void *opaque
)
381 MonitorQAPIEventState
*evstate
= opaque
;
382 MonitorQAPIEventConf
*evconf
= &monitor_qapi_event_conf
[evstate
->event
];
384 trace_monitor_protocol_event_handler(evstate
->event
, evstate
->qdict
);
385 qemu_mutex_lock(&monitor_lock
);
387 if (evstate
->qdict
) {
388 int64_t now
= qemu_clock_get_ns(monitor_get_event_clock());
390 monitor_qapi_event_emit(evstate
->event
, evstate
->qdict
);
391 qobject_unref(evstate
->qdict
);
392 evstate
->qdict
= NULL
;
393 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
395 g_hash_table_remove(monitor_qapi_event_state
, evstate
);
396 qobject_unref(evstate
->data
);
397 timer_free(evstate
->timer
);
401 qemu_mutex_unlock(&monitor_lock
);
404 static unsigned int qapi_event_throttle_hash(const void *key
)
406 const MonitorQAPIEventState
*evstate
= key
;
407 unsigned int hash
= evstate
->event
* 255;
409 if (evstate
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
410 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "id"));
413 if (evstate
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
414 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "node-name"));
420 static gboolean
qapi_event_throttle_equal(const void *a
, const void *b
)
422 const MonitorQAPIEventState
*eva
= a
;
423 const MonitorQAPIEventState
*evb
= b
;
425 if (eva
->event
!= evb
->event
) {
429 if (eva
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
430 return !strcmp(qdict_get_str(eva
->data
, "id"),
431 qdict_get_str(evb
->data
, "id"));
434 if (eva
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
435 return !strcmp(qdict_get_str(eva
->data
, "node-name"),
436 qdict_get_str(evb
->data
, "node-name"));
442 int monitor_suspend(Monitor
*mon
)
444 if (monitor_is_hmp_non_interactive(mon
)) {
448 atomic_inc(&mon
->suspend_cnt
);
450 if (mon
->use_io_thread
) {
452 * Kick I/O thread to make sure this takes effect. It'll be
453 * evaluated again in prepare() of the watch object.
455 aio_notify(iothread_get_aio_context(mon_iothread
));
458 trace_monitor_suspend(mon
, 1);
462 static void monitor_accept_input(void *opaque
)
464 Monitor
*mon
= opaque
;
466 qemu_chr_fe_accept_input(&mon
->chr
);
469 void monitor_resume(Monitor
*mon
)
471 if (monitor_is_hmp_non_interactive(mon
)) {
475 if (atomic_dec_fetch(&mon
->suspend_cnt
) == 0) {
478 if (mon
->use_io_thread
) {
479 ctx
= iothread_get_aio_context(mon_iothread
);
481 ctx
= qemu_get_aio_context();
484 if (!monitor_is_qmp(mon
)) {
485 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
487 readline_show_prompt(hmp_mon
->rs
);
490 aio_bh_schedule_oneshot(ctx
, monitor_accept_input
, mon
);
493 trace_monitor_suspend(mon
, -1);
496 int monitor_can_read(void *opaque
)
498 Monitor
*mon
= opaque
;
500 return !atomic_mb_read(&mon
->suspend_cnt
);
503 void monitor_list_append(Monitor
*mon
)
505 qemu_mutex_lock(&monitor_lock
);
507 * This prevents inserting new monitors during monitor_cleanup().
508 * A cleaner solution would involve the main thread telling other
509 * threads to terminate, waiting for their termination.
511 if (!monitor_destroyed
) {
512 QTAILQ_INSERT_HEAD(&mon_list
, mon
, entry
);
515 qemu_mutex_unlock(&monitor_lock
);
518 monitor_data_destroy(mon
);
523 static void monitor_iothread_init(void)
525 mon_iothread
= iothread_create("mon_iothread", &error_abort
);
528 void monitor_data_init(Monitor
*mon
, bool is_qmp
, bool skip_flush
,
531 if (use_io_thread
&& !mon_iothread
) {
532 monitor_iothread_init();
534 qemu_mutex_init(&mon
->mon_lock
);
535 mon
->is_qmp
= is_qmp
;
536 mon
->outbuf
= qstring_new();
537 mon
->skip_flush
= skip_flush
;
538 mon
->use_io_thread
= use_io_thread
;
541 void monitor_data_destroy(Monitor
*mon
)
543 g_free(mon
->mon_cpu_path
);
544 qemu_chr_fe_deinit(&mon
->chr
, false);
545 if (monitor_is_qmp(mon
)) {
546 monitor_data_destroy_qmp(container_of(mon
, MonitorQMP
, common
));
548 readline_free(container_of(mon
, MonitorHMP
, common
)->rs
);
550 qobject_unref(mon
->outbuf
);
551 qemu_mutex_destroy(&mon
->mon_lock
);
554 void monitor_cleanup(void)
557 * We need to explicitly stop the I/O thread (but not destroy it),
558 * clean up the monitor resources, then destroy the I/O thread since
559 * we need to unregister from chardev below in
560 * monitor_data_destroy(), and chardev is not thread-safe yet
563 iothread_stop(mon_iothread
);
566 /* Flush output buffers and destroy monitors */
567 qemu_mutex_lock(&monitor_lock
);
568 monitor_destroyed
= true;
569 while (!QTAILQ_EMPTY(&mon_list
)) {
570 Monitor
*mon
= QTAILQ_FIRST(&mon_list
);
571 QTAILQ_REMOVE(&mon_list
, mon
, entry
);
572 /* Permit QAPI event emission from character frontend release */
573 qemu_mutex_unlock(&monitor_lock
);
575 monitor_data_destroy(mon
);
576 qemu_mutex_lock(&monitor_lock
);
579 qemu_mutex_unlock(&monitor_lock
);
581 /* QEMUBHs needs to be deleted before destroying the I/O thread */
582 qemu_bh_delete(qmp_dispatcher_bh
);
583 qmp_dispatcher_bh
= NULL
;
585 iothread_destroy(mon_iothread
);
590 static void monitor_qapi_event_init(void)
592 monitor_qapi_event_state
= g_hash_table_new(qapi_event_throttle_hash
,
593 qapi_event_throttle_equal
);
596 void monitor_init_globals_core(void)
598 monitor_qapi_event_init();
599 qemu_mutex_init(&monitor_lock
);
602 * The dispatcher BH must run in the main loop thread, since we
603 * have commands assuming that context. It would be nice to get
604 * rid of those assumptions.
606 qmp_dispatcher_bh
= aio_bh_new(iohandler_get_aio_context(),
607 monitor_qmp_bh_dispatcher
,
611 QemuOptsList qemu_mon_opts
= {
613 .implied_opt_name
= "chardev",
614 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
618 .type
= QEMU_OPT_STRING
,
621 .type
= QEMU_OPT_STRING
,
624 .type
= QEMU_OPT_BOOL
,
626 { /* end of list */ }