All of lore.kernel.org
 help / color / mirror / Atom feed
From: Li Zhang <zhlcindy@gmail.com>
To: armbru@redhat.com, marcandre.lureau@gmail.com,
	pankaj.gupta.linux@gmail.com, qemu-devel@nongnu.org
Cc: Li Zhang <li.zhang@ionos.com>
Subject: [PATCH v3 1/2] qmp: Support chardev-change for QMP device
Date: Tue,  6 Jul 2021 21:14:51 +0200	[thread overview]
Message-ID: <20210706191452.127893-1-li.zhang@ionos.com> (raw)

For some scenarios, we'd like to hot-add a monitor device.  But QEMU
doesn't support that, yet.  It does support hot-swapping character
backends with QMP command chardev-change.  This lets us pre-add a
monitor with a null character backend, then chardev-change to a
socket backend.  Except the chardev-change fails with "Chardev user
does not support chardev hotswap" because monitors don't provide the
required callback.  Implement it for QMP monitors.

Signed-off-by: Li Zhang <li.zhang@ionos.com>
---
v3 -> v2: 
  * rework the patch according.
  * refactor the source code of chardev.

 monitor/monitor-internal.h |  1 +
 monitor/monitor.c          |  4 +-
 monitor/qmp.c              | 83 +++++++++++++++++++++++++++-----------
 3 files changed, 62 insertions(+), 26 deletions(-)

diff --git a/monitor/monitor-internal.h b/monitor/monitor-internal.h
index 9c3a09cb01..162f73119b 100644
--- a/monitor/monitor-internal.h
+++ b/monitor/monitor-internal.h
@@ -182,5 +182,6 @@ int get_monitor_def(Monitor *mon, int64_t *pval, const char *name);
 void help_cmd(Monitor *mon, const char *name);
 void handle_hmp_command(MonitorHMP *mon, const char *cmdline);
 int hmp_compare_cmd(const char *name, const char *list);
+void monitor_flush_locked(Monitor *mon);
 
 #endif
diff --git a/monitor/monitor.c b/monitor/monitor.c
index b90c0f4051..1b05ef3bdb 100644
--- a/monitor/monitor.c
+++ b/monitor/monitor.c
@@ -154,8 +154,6 @@ static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
     return !monitor_uses_readline(container_of(mon, MonitorHMP, common));
 }
 
-static void monitor_flush_locked(Monitor *mon);
-
 static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
                                   void *opaque)
 {
@@ -169,7 +167,7 @@ static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
 }
 
 /* Caller must hold mon->mon_lock */
-static void monitor_flush_locked(Monitor *mon)
+void monitor_flush_locked(Monitor *mon)
 {
     int rc;
     size_t len;
diff --git a/monitor/qmp.c b/monitor/qmp.c
index 092c527b6f..92c704373f 100644
--- a/monitor/qmp.c
+++ b/monitor/qmp.c
@@ -46,6 +46,8 @@ struct QMPRequest {
 typedef struct QMPRequest QMPRequest;
 
 QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
+static void monitor_qmp_setup_handlers_bh(void *opaque);
+static void monitor_backend_init(MonitorQMP *mon, Chardev *chr);
 
 static bool qmp_oob_enabled(MonitorQMP *mon)
 {
@@ -481,6 +483,35 @@ void monitor_data_destroy_qmp(MonitorQMP *mon)
     g_queue_free(mon->qmp_requests);
 }
 
+static bool mointor_in_list(Monitor *mon)
+{
+    Monitor *mon_tmp;
+    QTAILQ_FOREACH(mon_tmp, &mon_list, entry) {
+        if (mon_tmp == mon) {
+            return true;
+        }
+    }
+    return false;
+}
+
+static int monitor_qmp_change(void *opaque)
+{
+    MonitorQMP *mon = opaque;
+
+    monitor_data_init(&mon->common, true, false,
+            qemu_chr_has_feature(mon->common.chr.chr,
+                                 QEMU_CHAR_FEATURE_GCONTEXT));
+    monitor_backend_init(mon, mon->common.chr.chr);
+    qemu_mutex_lock(&mon->common.mon_lock);
+    if (mon->common.out_watch) {
+        mon->common.out_watch = 0;
+        monitor_flush_locked(&mon->common);
+    }
+    qemu_mutex_unlock(&mon->common.mon_lock);
+
+    return 0;
+}
+
 static void monitor_qmp_setup_handlers_bh(void *opaque)
 {
     MonitorQMP *mon = opaque;
@@ -491,30 +522,14 @@ static void monitor_qmp_setup_handlers_bh(void *opaque)
     assert(context);
     qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read,
                              monitor_qmp_read, monitor_qmp_event,
-                             NULL, &mon->common, context, true);
-    monitor_list_append(&mon->common);
+                             monitor_qmp_change, &mon->common, context, true);
+
+    if (!mointor_in_list(&mon->common))
+        monitor_list_append(&mon->common);
 }
 
-void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
+static void monitor_backend_init(MonitorQMP *mon, Chardev *chr)
 {
-    MonitorQMP *mon = g_new0(MonitorQMP, 1);
-
-    if (!qemu_chr_fe_init(&mon->common.chr, chr, errp)) {
-        g_free(mon);
-        return;
-    }
-    qemu_chr_fe_set_echo(&mon->common.chr, true);
-
-    /* Note: we run QMP monitor in I/O thread when @chr supports that */
-    monitor_data_init(&mon->common, true, false,
-                      qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT));
-
-    mon->pretty = pretty;
-
-    qemu_mutex_init(&mon->qmp_queue_lock);
-    mon->qmp_requests = g_queue_new();
-
-    json_message_parser_init(&mon->parser, handle_qmp_command, mon, NULL);
     if (mon->common.use_io_thread) {
         /*
          * Make sure the old iowatch is gone.  It's possible when
@@ -532,7 +547,29 @@ void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
     } else {
         qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read,
                                  monitor_qmp_read, monitor_qmp_event,
-                                 NULL, &mon->common, NULL, true);
-        monitor_list_append(&mon->common);
+                                 monitor_qmp_change, &mon->common, NULL, true);
+        if (!mointor_in_list(&mon->common)) {
+            monitor_list_append(&mon->common);
+        }
+    }
+}
+
+void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
+{
+    MonitorQMP *mon = g_new0(MonitorQMP, 1);
+
+    if (!qemu_chr_fe_init(&mon->common.chr, chr, errp)) {
+        g_free(mon);
+        return;
     }
+    qemu_chr_fe_set_echo(&mon->common.chr, true);
+     /* Note: we run QMP monitor in I/O thread when @chr supports that */
+    monitor_data_init(&mon->common, true, false,
+                      qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT));
+
+    mon->pretty = pretty;
+    qemu_mutex_init(&mon->qmp_queue_lock);
+    mon->qmp_requests = g_queue_new();
+    json_message_parser_init(&mon->parser, handle_qmp_command, mon, NULL);
+    monitor_backend_init(mon, chr);
 }
-- 
2.25.1



             reply	other threads:[~2021-07-06 19:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-06 19:14 Li Zhang [this message]
2021-07-06 19:14 ` [PATCH v3 2/2] chardev: refactor qmp_chardev_add and qmp_chardev_change Li Zhang
2021-07-06 19:28 ` [PATCH v3 1/2] qmp: Support chardev-change for QMP device Li Zhang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210706191452.127893-1-li.zhang@ionos.com \
    --to=zhlcindy@gmail.com \
    --cc=armbru@redhat.com \
    --cc=li.zhang@ionos.com \
    --cc=marcandre.lureau@gmail.com \
    --cc=pankaj.gupta.linux@gmail.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.