mirror of
https://github.com/qemu/qemu.git
synced 2026-07-08 17:46:10 +00:00
The default monitor is usually a long lived object that will exist for
the entire lifetime of the VM. A monitor can only service a single
client at a time though, and so it might be desirable to hotplug
additional monitors at runtime for specific tasks. If doing that,
however, there is a need to remove the monitor when it is no longer
needed.
A use case for hotplugging a monitor can involve a user wishing to
spawn an ad hoc script that uses a temporary monitor. The script can
ask the management application to hotplug a monitor and pass back a
pre-opened FD using SCM_RIGHTS. In this case the lifetime of the
script is not tied to the management application and thus it is
desirable to have automatic cleanup when the script exits.
Allowing a client to run "object-del" against its own monitor adds
complex edge cases, as it would be desirable to send the QMP response
despite the monitor sending it being deleted. Doing "object-del" alone
will also result in orphaning a character device backend instance, as
there is no opportunity to run the companion "chardev-del" command.
A simpler way to ensure cleanup is to add the concept of auto-deleting
monitor objects. Specifically when the "CHR_EVENT_CLOSED" event is
emitted, the equivalent of "object-del" + "chardev-del" can be run
internally. Since the transient client has already droppped its
monitor connection, there is no synchronization to be concerned about
with sending QMP replies. There is still some internal synchronization
needed, however, between the character device event callback and the
bottom-half that runs the delete. There is a chance that an incoming
client connection may arise before the bottom-half runs, which has
to be checked. Once the monitor object is deleted, the event callback
is unregistered from the character device, eliminating any further
races before the character device is fully deleted.
This is implemented via a new "close-action=none|delete" property on
the 'monitor-qmp' object. This concept could be extended with further
actions in future, for example:
* close-action=shutdown - graceful guest shutdown
* close-action=terminate - immediate guest poweroff
* close-action=stop - pause guest CPUs while the monitor is not
connected to any client
This is left as an exercise for future interested contributors.
Tested-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Acked-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Message-ID: <20260706135824.2623960-33-berrange@redhat.com>
[Commit message typos fixed]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
234 lines
7.9 KiB
C
234 lines
7.9 KiB
C
/*
|
|
* QEMU monitor
|
|
*
|
|
* Copyright (c) 2003-2004 Fabrice Bellard
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
#ifndef MONITOR_INTERNAL_H
|
|
#define MONITOR_INTERNAL_H
|
|
|
|
#include "chardev/char-fe.h"
|
|
#include "monitor/monitor.h"
|
|
#include "qapi/qapi-types-control.h"
|
|
#include "qapi/qapi-types-qom.h"
|
|
#include "qapi/qmp-registry.h"
|
|
#include "qobject/json-parser.h"
|
|
#include "qemu/readline.h"
|
|
#include "system/iothread.h"
|
|
|
|
/*
|
|
* Supported types:
|
|
*
|
|
* 'F' filename
|
|
* 'B' block device name
|
|
* 's' string (accept optional quote)
|
|
* 'S' it just appends the rest of the string (accept optional quote)
|
|
* 'O' option string of the form NAME=VALUE,...
|
|
* parsed according to QemuOptsList given by its name
|
|
* Example: 'device:O' uses qemu_device_opts.
|
|
* Restriction: only lists with empty desc are supported
|
|
* TODO lift the restriction
|
|
* 'i' 32 bit integer
|
|
* 'l' target long (32 or 64 bit)
|
|
* 'M' Non-negative target long (32 or 64 bit), in user mode the
|
|
* value is multiplied by 2^20 (think Mebibyte)
|
|
* 'o' octets (aka bytes)
|
|
* user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
|
|
* K, k suffix, which multiplies the value by 2^60 for suffixes E
|
|
* and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
|
|
* 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
|
|
* 'T' double
|
|
* user mode accepts an optional ms, us, ns suffix,
|
|
* which divides the value by 1e3, 1e6, 1e9, respectively
|
|
* '/' optional gdb-like print format (like "/10x")
|
|
*
|
|
* '?' optional type (for all types, except '/')
|
|
* '.' other form of optional type (for 'i' and 'l')
|
|
* 'b' boolean
|
|
* user mode accepts "on" or "off"
|
|
* '-' optional parameter (eg. '-f'); if followed by a 's', it
|
|
* specifies an optional string param (e.g. '-fs' allows '-f foo')
|
|
*
|
|
*/
|
|
|
|
typedef struct HMPCommand {
|
|
const char *name;
|
|
const char *args_type;
|
|
const char *params;
|
|
const char *help;
|
|
const char *flags; /* p=preconfig */
|
|
void (*cmd)(Monitor *mon, const QDict *qdict);
|
|
/*
|
|
* If implementing a command that takes no arguments and simply
|
|
* prints formatted data, then leave @cmd NULL, and then set
|
|
* @cmd_info_hrt to the corresponding QMP handler that returns
|
|
* the formatted text.
|
|
*/
|
|
HumanReadableText *(*cmd_info_hrt)(Error **errp);
|
|
/*
|
|
* @sub_table is a list of 2nd level of commands. If it does not exist,
|
|
* cmd should be used. If it exists, sub_table[?].cmd should be
|
|
* used, and cmd of 1st level plays the role of help function.
|
|
*/
|
|
struct HMPCommand *sub_table;
|
|
void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
|
|
|
|
/* Keep non-pointer data at the end to minimize holes. */
|
|
|
|
/**
|
|
* @arch_bitmask: bitmask of QEMU_ARCH_* constants
|
|
* Allow to restrict the command for a particular set of
|
|
* target architectures.
|
|
*/
|
|
uint32_t arch_bitmask;
|
|
bool coroutine;
|
|
} HMPCommand;
|
|
|
|
|
|
struct MonitorClass {
|
|
ObjectClass parent_class;
|
|
|
|
/*
|
|
* If non-NULL, the monitor is able to print messages
|
|
* for attention of the client user
|
|
*/
|
|
int (*vprintf)(Monitor *mon, const char *fmt, va_list ap)
|
|
G_GNUC_PRINTF(2, 0);
|
|
/*
|
|
* If non-NULL, the monitor is able to send event
|
|
* notifications back to the client
|
|
*/
|
|
void (*emit_event)(Monitor *mon, QAPIEvent event, QDict *qdict);
|
|
/*
|
|
* If non-NULL, perform any actions needed to prepare
|
|
* the monitor to accept further client input
|
|
*/
|
|
void (*accept_input)(Monitor *mon);
|
|
|
|
/*
|
|
* If non-NULL and returns true, then an I/O thread
|
|
* is required for processing the monitor
|
|
*/
|
|
bool (*requires_iothread)(const Monitor *mon);
|
|
};
|
|
|
|
struct Monitor {
|
|
Object parent;
|
|
char *chardev_id;
|
|
CharFrontend chr;
|
|
int suspend_cnt; /* Needs to be accessed atomically */
|
|
QEMUBH *accept_input_bh; /* persistent BH for monitor_accept_input */
|
|
char *mon_cpu_path;
|
|
QTAILQ_ENTRY(Monitor) entry;
|
|
|
|
/*
|
|
* The per-monitor lock. We can't access guest memory when holding
|
|
* the lock.
|
|
*/
|
|
QemuMutex mon_lock;
|
|
|
|
/*
|
|
* Members that are protected by the per-monitor lock
|
|
*/
|
|
QLIST_HEAD(, mon_fd_t) fds;
|
|
GString *outbuf;
|
|
guint out_watch;
|
|
int mux_out;
|
|
int reset_seen;
|
|
};
|
|
|
|
struct MonitorHMPClass {
|
|
MonitorClass parent_class;
|
|
};
|
|
|
|
struct MonitorHMP {
|
|
Monitor parent_obj;
|
|
bool use_readline;
|
|
/*
|
|
* State used only in the thread "owning" the monitor.
|
|
* This is currently always the main thread, since
|
|
* HMP does not allow use of the I/O thread at this time.
|
|
* These members can be safely accessed without locks.
|
|
*/
|
|
ReadLineState *rs;
|
|
};
|
|
|
|
struct MonitorQMPClass {
|
|
MonitorClass parent_class;
|
|
};
|
|
|
|
struct MonitorQMP {
|
|
Monitor parent_obj;
|
|
JSONMessageParser parser;
|
|
bool pretty;
|
|
MonitorQMPCloseAction close_action;
|
|
bool setup_pending; /* iothread BH has not yet set up chardev handlers */
|
|
bool delete_pending; /* close_action has started 'delete' process */
|
|
/*
|
|
* When a client connects, we're in capabilities negotiation mode.
|
|
* @commands is &qmp_cap_negotiation_commands then. When command
|
|
* qmp_capabilities succeeds, we go into command mode, and
|
|
* @command becomes &qmp_commands.
|
|
*/
|
|
const QmpCommandList *commands;
|
|
bool capab_offered[QMP_CAPABILITY__MAX]; /* capabilities offered */
|
|
bool capab[QMP_CAPABILITY__MAX]; /* offered and accepted */
|
|
/*
|
|
* Protects qmp request/response queue.
|
|
* Take monitor_lock first when you need both.
|
|
*/
|
|
QemuMutex qmp_queue_lock;
|
|
/* Input queue that holds all the parsed QMP requests */
|
|
GQueue *qmp_requests;
|
|
};
|
|
|
|
typedef QTAILQ_HEAD(MonitorList, Monitor) MonitorList;
|
|
extern IOThread *mon_iothread;
|
|
extern Coroutine *qmp_dispatcher_co;
|
|
extern bool qmp_dispatcher_co_shutdown;
|
|
extern QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
|
|
extern QemuMutex monitor_lock;
|
|
extern MonitorList mon_list;
|
|
|
|
bool monitor_requires_iothread(const Monitor *mon);
|
|
int monitor_can_read(void *opaque);
|
|
void monitor_cancel_out_watch(Monitor *mon);
|
|
void monitor_list_append(Monitor *mon);
|
|
void monitor_fdsets_cleanup(void);
|
|
int monitor_set_cpu(Monitor *mon, int cpu_index);
|
|
|
|
void qmp_send_response(MonitorQMP *mon, const QDict *rsp);
|
|
void monitor_data_destroy_qmp(MonitorQMP *mon);
|
|
void coroutine_fn monitor_qmp_dispatcher_co(void *data);
|
|
void qmp_dispatcher_co_wake(void);
|
|
|
|
void handle_hmp_command(MonitorHMP *mon, const char *cmdline);
|
|
int hmp_compare_cmd(const char *name, const char *list);
|
|
|
|
/*
|
|
* hmp_cmds_for_target: Return array of HMPCommand entries
|
|
*
|
|
* If @info_command is true, return the particular 'info foo' commands array.
|
|
*/
|
|
HMPCommand *hmp_cmds_for_target(bool info_command);
|
|
|
|
#endif
|