[PATCH 3/5] qapi: add monitor-add, monitor-remove, query-monitors commands

Christian Brauner posted 5 patches 1 week, 1 day ago
Maintainers: "Dr. David Alan Gilbert" <dave@treblig.org>, Markus Armbruster <armbru@redhat.com>, Eric Blake <eblake@redhat.com>, Thomas Huth <th.huth+qemu@posteo.eu>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, "Daniel P. Berrangé" <berrange@redhat.com>, Fabiano Rosas <farosas@suse.de>, Laurent Vivier <lvivier@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>
There is a newer version of this series
[PATCH 3/5] qapi: add monitor-add, monitor-remove, query-monitors commands
Posted by Christian Brauner 1 week, 1 day ago
Add QMP commands for dynamic monitor lifecycle management:

- monitor-add: Create a QMP monitor on an existing chardev at runtime.
  The chardev must exist and not be in use. The new monitor starts in
  capability negotiation mode.

- monitor-remove: Remove a dynamically-added monitor. CLI-created
  monitors cannot be removed. If the dispatcher is currently servicing
  the target monitor (self-removal), destruction is deferred until the
  in-flight command completes. The underlying chardev is not destroyed.

- query-monitors: Introspect all active monitors with their id, mode,
  chardev name, and whether they were dynamically added.

The motivating use case is systemd-vmspawn: when an external client
requests raw QMP access, vmspawn can create an independent QMP session
on demand rather than pre-allocating spare monitors at launch or
building an id-rewriting proxy.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 monitor/qmp-cmds-control.c |  94 ++++++++++++++++++++++++++++++++++++++++
 qapi/control.json          | 106 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 200 insertions(+)

diff --git a/monitor/qmp-cmds-control.c b/monitor/qmp-cmds-control.c
index 150ca9f5cb..0998017620 100644
--- a/monitor/qmp-cmds-control.c
+++ b/monitor/qmp-cmds-control.c
@@ -219,3 +219,97 @@ SchemaInfoList *qmp_query_qmp_schema(Error **errp)
     }
     return schema;
 }
+
+void qmp_monitor_add(const char *id, const char *chardev,
+                     bool has_pretty, bool pretty, Error **errp)
+{
+    Chardev *chr;
+
+    /* Reject duplicate monitor id */
+    if (monitor_find_by_id(id)) {
+        error_setg(errp, "monitor '%s' already exists", id);
+        return;
+    }
+
+    chr = qemu_chr_find(chardev);
+    if (!chr) {
+        error_setg(errp, "chardev '%s' not found", chardev);
+        return;
+    }
+
+    monitor_init_qmp(chr, has_pretty && pretty, id, true, errp);
+}
+
+void qmp_monitor_remove(const char *id, Error **errp)
+{
+    Monitor *mon;
+    MonitorQMP *qmp_mon;
+
+    mon = monitor_find_by_id(id);
+    if (!mon) {
+        error_setg(errp, "monitor '%s' not found", id);
+        return;
+    }
+
+    if (!mon->dynamic) {
+        error_setg(errp, "monitor '%s' was not dynamically added", id);
+        return;
+    }
+
+    qmp_mon = container_of(mon, MonitorQMP, common);
+
+    /*
+     * Step 1: Disconnect chardev handlers so no new data arrives
+     * and no new requests are enqueued.
+     */
+    qemu_chr_fe_set_handlers(&mon->chr, NULL, NULL, NULL, NULL,
+                             NULL, NULL, true);
+
+    /* Step 2: Drain pending requests from the queue */
+    monitor_qmp_cleanup_queue_and_resume(qmp_mon);
+
+    /*
+     * Step 3: Mark dead and remove from mon_list.
+     * After removal, the dispatcher will never pop new requests from
+     * this monitor, and event broadcast will skip it.
+     */
+    qemu_mutex_lock(&monitor_lock);
+    mon->dead = true;
+    QTAILQ_REMOVE(&mon_list, mon, entry);
+    qemu_mutex_unlock(&monitor_lock);
+
+    /*
+     * Step 4: Check if the dispatcher is currently mid-dispatch on
+     * this monitor (i.e. monitor-remove was sent from the monitor
+     * being removed).  If so, defer destruction -- the dispatcher
+     * will call monitor_qmp_destroy() after completing the request.
+     */
+    if (monitor_qmp_dispatcher_is_servicing(qmp_mon)) {
+        return;
+    }
+
+    /* Step 5: Safe to destroy immediately */
+    monitor_qmp_destroy(qmp_mon);
+}
+
+MonitorInfoList *qmp_query_monitors(Error **errp)
+{
+    MonitorInfoList *list = NULL;
+    Monitor *mon;
+
+    qemu_mutex_lock(&monitor_lock);
+    QTAILQ_FOREACH(mon, &mon_list, entry) {
+        MonitorInfo *info = g_new0(MonitorInfo, 1);
+        Chardev *chr = qemu_chr_fe_get_driver(&mon->chr);
+
+        info->id = g_strdup(mon->id); /* NULL if unnamed */
+        info->mode = mon->is_qmp ? MONITOR_MODE_CONTROL
+                                 : MONITOR_MODE_READLINE;
+        info->chardev = g_strdup(chr ? chr->label : "unknown");
+        info->dynamic = mon->dynamic;
+        QAPI_LIST_PREPEND(list, info);
+    }
+    qemu_mutex_unlock(&monitor_lock);
+
+    return list;
+}
diff --git a/qapi/control.json b/qapi/control.json
index 9a5302193d..b9f495c08c 100644
--- a/qapi/control.json
+++ b/qapi/control.json
@@ -211,3 +211,109 @@
       '*pretty': 'bool',
       'chardev': 'str'
   } }
+
+##
+# @monitor-add:
+#
+# Add a QMP monitor on an existing character device backend.
+#
+# The chardev must already exist (created via chardev-add or CLI) and
+# must not be in use by another frontend.  The monitor begins in
+# capability negotiation mode -- the first client to connect receives
+# the QMP greeting.
+#
+# @id: Monitor identifier, must be unique among monitors
+#
+# @chardev: Name of the character device backend to attach to
+#
+# @pretty: Enable pretty-printing of QMP responses (default: false)
+#
+# Errors:
+#     - GenericError if @id is already in use
+#     - GenericError if @chardev does not exist
+#     - GenericError if @chardev is already in use by another frontend
+#
+# Since: 11.0
+#
+# .. qmp-example::
+#
+#     -> { "execute": "monitor-add",
+#          "arguments": { "id": "extra-qmp",
+#                         "chardev": "qmp-extra" } }
+#     <- { "return": {} }
+##
+{ 'command': 'monitor-add',
+  'data': { 'id': 'str',
+            'chardev': 'str',
+            '*pretty': 'bool' } }
+
+##
+# @monitor-remove:
+#
+# Remove a dynamically added QMP monitor.
+#
+# The monitor must have been created via monitor-add.  Monitors
+# created via CLI options (-mon, -qmp) cannot be removed.  The
+# underlying chardev is NOT removed -- use chardev-remove separately
+# if desired.
+#
+# If a client is currently connected, the connection is dropped.
+#
+# @id: Monitor identifier as passed to monitor-add
+#
+# Errors:
+#     - GenericError if @id does not exist
+#     - GenericError if the monitor was not dynamically added
+#
+# Since: 11.0
+#
+# .. qmp-example::
+#
+#     -> { "execute": "monitor-remove",
+#          "arguments": { "id": "extra-qmp" } }
+#     <- { "return": {} }
+##
+{ 'command': 'monitor-remove',
+  'data': { 'id': 'str' } }
+
+##
+# @MonitorInfo:
+#
+# Information about a QMP/HMP monitor.
+#
+# @id: Monitor identifier (absent for CLI-created monitors without
+#     an explicit id)
+#
+# @mode: Monitor mode (readline or control)
+#
+# @chardev: Name of the attached character device
+#
+# @dynamic: true if created via monitor-add (removable), false if
+#     created via CLI
+#
+# Since: 11.0
+##
+{ 'struct': 'MonitorInfo',
+  'data': { '*id': 'str',
+            'mode': 'MonitorMode',
+            'chardev': 'str',
+            'dynamic': 'bool' } }
+
+##
+# @query-monitors:
+#
+# Return information about all active monitors.
+#
+# Returns: a list of @MonitorInfo for each active monitor
+#
+# Since: 11.0
+#
+# .. qmp-example::
+#
+#     -> { "execute": "query-monitors" }
+#     <- { "return": [ { "id": "mon0", "mode": "control",
+#                         "chardev": "compat_monitor0",
+#                         "dynamic": false } ] }
+##
+{ 'command': 'query-monitors',
+  'returns': ['MonitorInfo'] }

-- 
2.47.3