Skip to contentSkip to conversation

chardev: implement backend chardev multiplexing

PATCH Superseded Roman Penyaev 2 participants 8 messages Oct 9, 2024 – Oct 10, 2024

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v2 cover chardev: implement backend chardev multiplexing

Mux is a character backend (host side) device, which multiplexes multiple frontends with one backend device. The following is a few lines from the QEMU manpage [1]:

A multiplexer is a "1:N" device, and here the "1" end is your specified chardev backend, and the "N" end is the various parts of QEMU that can talk to a chardev.

But sadly multiple backends are not supported.

This work implements multiplexing capability of several backend devices, which opens up an opportunity to use a single frontend device on the guest, which can be manipulated from several backend devices.

The motivation is the EVE project [2], where it would be very convenient to have a virtio console frontend device on the guest that can be controlled from multiple backend devices. The following is an example of the QEMU command line:

   -chardev mux-be,id=mux0 \
   -chardev socket,path=/tmp/sock,server=on,wait=off,id=sock0,mux-be-id=mux0 \
   -chardev vc,id=vc0,mux-be-id=mux0 \
   -device virtconsole,chardev=mux0 \
   -vnc 0.0.0.0:0

Which creates 2 backend devices: text virtual console (vc0) and a socket (sock0) connected to the single virtio hvc console with the backend multiplexer (mux0) help. vc0 renders text to an image, which can be shared over the VNC protocol. sock0 is a socket backend which provides biderectional communication to the virtio hvc console.

New type of multiplexer mux-be actually is an alias for the same MuxChardev struct, which uses same functions as for the original mux type, but supports multiplexing N backends with 1 frontend.

Once QEMU starts VNC client and any TTY emulator can be used to control a single hvc console, for example these two different consoles should have similar input and output due the buffer multiplexing:

# VNC client
vncviewer :0

# TTY emulator
socat unix-connect:/tmp/sock pty,link=/tmp/pty
tio /tmp/pty

Difference to the previous version:

* Separate type for the backend multiplexer mux-be
* Handle EAGAIN on write to the backend device
* Support of watch of previously failed backend device
* Proper json support of the mux-be-id option
* Unit test for the mux-be multiplexer

[1] https://www.qemu.org/docs/master/system/qemu-manpage.html#hxtool-6
[2] https://github.com/lf-edge/eve

Roman Penyaev (5):
chardev/char: introduce mux-be-id=ID option and _MUX_BE type chardev/char: rename mux_cnt to fe_cnt for the MuxChardev chardev/char-mux: implement backend chardev multiplexing tests/unit/test-char: add unit test for the mux-be multiplexer qemu-options.hx: describe multiplexing of several backend devices

 chardev/char-fe.c          |  14 ++-
 chardev/char-mux.c         | 212 +++++++++++++++++++++++++++++++-----
 chardev/char.c             |  57 ++++++++--
 chardev/chardev-internal.h |  33 +++++-
 include/chardev/char.h     |   1 +
 qapi/char.json             |   9 +-
 qemu-options.hx            |  46 +++++++-
 tests/unit/test-char.c     | 217 ++++++++++++++++++++++++++++++++++++-
 8 files changed, 538 insertions(+), 51 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/5 chardev/char: introduce `mux-be-id=ID` option and _MUX_BE type

Patch introduces mux-be-id=ID option for all chardev devices and TYPE_CHARDEV_MUX_BE type for distinction different types of multiplexers: frontend (current "mux" type) and backend (new "mux-be" type). Actual attach of the chardev to the multiplexer and mux-be implementation will follow.

---
 chardev/char.c         | 3 +++
 include/chardev/char.h | 1 +
 qapi/char.json         | 9 ++++++++-
 3 files changed, 12 insertions(+), 1 deletion(-)
chardev/char.c +3 -0
index ba847b6e9eff..4b3e45b2a128 100644
@@ -918,6 +918,9 @@ QemuOptsList qemu_chardev_opts = {
         },{
             .name = "mux",
             .type = QEMU_OPT_BOOL,
+        },{
+            .name = "mux-be-id",
+            .type = QEMU_OPT_STRING,
         },{
             .name = "signal",
             .type = QEMU_OPT_BOOL,
include/chardev/char.h +1 -0
index 01df55f9e8c8..f68f949f6ebc 100644
@@ -232,6 +232,7 @@ OBJECT_DECLARE_TYPE(Chardev, ChardevClass, CHARDEV)
 
 #define TYPE_CHARDEV_NULL "chardev-null"
 #define TYPE_CHARDEV_MUX "chardev-mux"
+#define TYPE_CHARDEV_MUX_BE "chardev-mux-be"
 #define TYPE_CHARDEV_RINGBUF "chardev-ringbuf"
 #define TYPE_CHARDEV_PTY "chardev-pty"
 #define TYPE_CHARDEV_CONSOLE "chardev-console"
qapi/char.json +8 -1
index ef58445ceec9..30748784513b 100644
@@ -199,11 +199,15 @@
 # @logappend: true to append instead of truncate (default to false to
 #     truncate)
 #
+# @mux-be-id: id of the mux-be device for backend multiplexing
+#     (since: 9.2)
+#
 # Since: 2.6
 ##
 { 'struct': 'ChardevCommon',
   'data': { '*logfile': 'str',
-            '*logappend': 'bool' } }
+            '*logappend': 'bool',
+            '*mux-be-id': 'str' } }
 
 ##
 # @ChardevFile:
@@ -440,6 +444,8 @@
 #
 # @mux: Since 1.5
 #
+# @mux-be: Since 9.2
+#
 # @msmouse: Since 1.5
 #
 # @wctablet: Since 2.9
@@ -482,6 +488,7 @@
             'pty',
             'null',
             'mux',
+            'mux-be',
             'msmouse',
             'wctablet',
             { 'name': 'braille', 'if': 'CONFIG_BRLAPI' },
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/5 chardev/char: rename `mux_cnt` to `fe_cnt` for the `MuxChardev`

In reply to Roman Penyaev

In the following patches MuxChardev struct will suport backend multiplexing, the mux_cnt struct member has very common name and does not reflect the actual meaning: number of frontends attached to a mux. This patch renames the mux_cnt to fe_cnt. No other functional changes made.

---
 chardev/char-fe.c          |  7 +++----
 chardev/char-mux.c         | 12 ++++++------
 chardev/char.c             |  2 +-
 chardev/chardev-internal.h |  2 +-
 4 files changed, 11 insertions(+), 12 deletions(-)
chardev/char-fe.c +3 -4
index b214ba3802b1..e6f44801162a 100644
@@ -197,16 +197,15 @@ bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp)
         if (CHARDEV_IS_MUX(s)) {
             MuxChardev *d = MUX_CHARDEV(s);
 
-            if (d->mux_cnt >= MAX_MUX) {
+            if (d->fe_cnt >= MAX_MUX) {
                 error_setg(errp,
                            "too many uses of multiplexed chardev '%s'"
                            " (maximum is " stringify(MAX_MUX) ")",
                            s->label);
                 return false;
             }
-
-            d->backends[d->mux_cnt] = b;
-            tag = d->mux_cnt++;
+            d->backends[d->fe_cnt] = b;
+            tag = d->fe_cnt++;
         } else if (s->be) {
             error_setg(errp, "chardev '%s' is already in use", s->label);
             return false;
chardev/char-mux.c +6 -6
index ee2d47b20d9b..eb72b3cdb80b 100644
@@ -168,9 +168,9 @@ static int mux_proc_byte(Chardev *chr, MuxChardev *d, int ch)
             qemu_chr_be_event(chr, CHR_EVENT_BREAK);
             break;
         case 'c':
-            assert(d->mux_cnt > 0); /* handler registered with first fe */
+            assert(d->fe_cnt > 0); /* handler registered with first fe */
             /* Switch to the next registered device */
-            mux_set_focus(chr, (d->focus + 1) % d->mux_cnt);
+            mux_set_focus(chr, (d->focus + 1) % d->fe_cnt);
             break;
         case 't':
             d->timestamps = !d->timestamps;
@@ -248,8 +248,8 @@ void mux_chr_send_all_event(Chardev *chr, QEMUChrEvent event)
         return;
     }
 
-    /* Send the event to all registered listeners */
-    for (i = 0; i < d->mux_cnt; i++) {
+    /* Send the event to all registered frontend listeners */
+    for (i = 0; i < d->fe_cnt; i++) {
         mux_chr_send_event(d, i, event);
     }
 }
@@ -277,7 +277,7 @@ static void char_mux_finalize(Object *obj)
     MuxChardev *d = MUX_CHARDEV(obj);
     int i;
 
-    for (i = 0; i < d->mux_cnt; i++) {
+    for (i = 0; i < d->fe_cnt; i++) {
         CharBackend *be = d->backends[i];
         if (be) {
             be->chr = NULL;
@@ -305,7 +305,7 @@ void mux_set_focus(Chardev *chr, int focus)
     MuxChardev *d = MUX_CHARDEV(chr);
 
     assert(focus >= 0);
-    assert(focus < d->mux_cnt);
+    assert(focus < d->fe_cnt);
 
     if (d->focus != -1) {
         mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
chardev/char.c +1 -1
index 4b3e45b2a128..3f0fcc8b16f6 100644
@@ -333,7 +333,7 @@ static bool qemu_chr_is_busy(Chardev *s)
 {
     if (CHARDEV_IS_MUX(s)) {
         MuxChardev *d = MUX_CHARDEV(s);
-        return d->mux_cnt >= 0;
+        return d->fe_cnt >= 0;
     } else {
         return s->be != NULL;
     }
chardev/chardev-internal.h +1 -1
index 4e03af31476c..e8c3c29d1a59 100644
@@ -38,7 +38,7 @@ struct MuxChardev {
     CharBackend *backends[MAX_MUX];
     CharBackend chr;
     int focus;
-    int mux_cnt;
+    int fe_cnt;
     int term_got_escape;
     int max_size;
     /* Intermediate input buffer catches escape sequences even if the
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 3/5 chardev/char-mux: implement backend chardev multiplexing

In reply to Roman Penyaev

This patch implements multiplexing capability of several backend devices, which opens up an opportunity to use a single frontend device on the guest, which can be manipulated from several backend devices.

The idea of the change is trivial: keep list of backend devices (up to 4), init them on demand and forward data buffer back and forth.

The following is QEMU command line example:

   -chardev mux-be,id=mux0 \
   -chardev socket,path=/tmp/sock,server=on,wait=off,id=sock0,mux-be-id=mux0 \
   -chardev vc,id=vc0,mux-be-id=mux0 \
   -device virtconsole,chardev=mux0 \
   -vnc 0.0.0.0:0

Which creates 2 backend devices: text virtual console (vc0) and a socket (sock0) connected to the single virtio hvc console with the backend multiplexer (mux0) help. vc0 renders text to an image, which can be shared over the VNC protocol. sock0 is a socket backend which provides biderectional communication to the virtio hvc console.

New type of multiplexer mux-be actually is an alias for the same MuxChardev struct, which uses same functions as for the original mux type, but supports multiplexing N backends with 1 frontend.

---
 chardev/char-fe.c          |   7 ++
 chardev/char-mux.c         | 200 +++++++++++++++++++++++++++++++++----
 chardev/char.c             |  52 ++++++++--
 chardev/chardev-internal.h |  31 +++++-
 4 files changed, 258 insertions(+), 32 deletions(-)
chardev/char-fe.c +7 -0
index e6f44801162a..d1f67338084d 100644
@@ -204,6 +204,13 @@ bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp)
                            s->label);
                 return false;
             }
+            if (d->fe_cnt > 0 && d->be_cnt > 1) {
+                error_setg(errp,
+                           "multiplexed chardev '%s' is already used "
+                           "for backend multiplexing",
+                           s->label);
+                return false;
+            }
             d->backends[d->fe_cnt] = b;
             tag = d->fe_cnt++;
         } else if (s->be) {
chardev/char-mux.c +180 -20

280 lines not on this page · raw message

chardev/char.c +43 -9

91 lines not on this page · raw message

chardev/chardev-internal.h +28 -3
index e8c3c29d1a59..07a917737fc3 100644
@@ -35,10 +35,14 @@
 
 struct MuxChardev {
     Chardev parent;
+    /* Linked frontends ("mux" type) */
     CharBackend *backends[MAX_MUX];
-    CharBackend chr;
+    /* Linked backends ("mux-be" type) */
+    CharBackend chrs[MAX_MUX];
+    bool is_mux_be;
     int focus;
     int fe_cnt;
+    int be_cnt;
     int term_got_escape;
     int max_size;
     /* Intermediate input buffer catches escape sequences even if the
@@ -52,16 +56,37 @@ struct MuxChardev {
     /* Protected by the Chardev chr_write_lock.  */
     int linestart;
     int64_t timestamps_start;
+
+    /*
+     * Counters of written bytes from a single frontend device
+     * to multiple backend devices ("mux-be" type).
+     */
+    unsigned int be_written[MAX_MUX];
+    unsigned int be_min_written;
 };
 typedef struct MuxChardev MuxChardev;
 
-DECLARE_INSTANCE_CHECKER(MuxChardev, MUX_CHARDEV,
+/* Two different names correspond to similar struct */
+DECLARE_INSTANCE_CHECKER(MuxChardev, __MUX_CHARDEV,
                          TYPE_CHARDEV_MUX)
-#define CHARDEV_IS_MUX(chr)                             \
+DECLARE_INSTANCE_CHECKER(MuxChardev, __MUX_BE_CHARDEV,
+                         TYPE_CHARDEV_MUX_BE)
+
+#define __CHARDEV_IS_MUX(chr) \
     object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_MUX)
+#define __CHARDEV_IS_MUX_BE(chr) \
+    object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_MUX_BE)
+
+#define CHARDEV_IS_MUX(chr) \
+    (__CHARDEV_IS_MUX(chr) || __CHARDEV_IS_MUX_BE(chr))
+
+/* Either "mux", either "mux-be" */
+#define MUX_CHARDEV(chr) \
+    (__CHARDEV_IS_MUX(chr) ? __MUX_CHARDEV(chr) : __MUX_BE_CHARDEV(chr))
 
 void mux_set_focus(Chardev *chr, int focus);
 void mux_chr_send_all_event(Chardev *chr, QEMUChrEvent event);
+bool mux_chr_attach_chardev(MuxChardev *d, Chardev *chr, Error **errp);
 
 Object *get_chardevs_root(void);
 
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 4/5 tests/unit/test-char: add unit test for the `mux-be` multiplexer

In reply to Roman Penyaev

The test is trivial: several backends, 1 mux-be, 1 frontend do the buffer write and read. Pipe is used for EAGAIN verification.

---
 tests/unit/test-char.c | 217 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 215 insertions(+), 2 deletions(-)
tests/unit/test-char.c +215 -2
index f273ce522612..554129c3b1a9 100644
@@ -177,7 +177,7 @@ static void char_ringbuf_test(void)
     qemu_opts_del(opts);
 }
 
-static void char_mux_test(void)
+static void char_mux_fe_test(void)
 {
     QemuOpts *opts;
     Chardev *chr, *base;
@@ -337,6 +337,218 @@ static void char_mux_test(void)
     qemu_chr_fe_deinit(&chr_be2, true);
 }
 
+static void char_mux_be_test(void)
+{
+    QemuOpts *opts;
+    Chardev *mux_be, *chr1, *chr2, *base;
+    char *data;
+    FeHandler h = { 0, false, 0, false, };
+    CharBackend chr_be;
+    int ret;
+
+#define RB_SIZE 128
+
+    /* Create mux-be */
+    opts = qemu_opts_create(qemu_find_opts("chardev"), "mux0",
+                            1, &error_abort);
+    qemu_opt_set(opts, "backend", "mux-be", &error_abort);
+    mux_be = qemu_chr_new_from_opts(opts, NULL, &error_abort);
+    g_assert_nonnull(mux_be);
+    qemu_opts_del(opts);
+
+    /* Create first chardev */
+    opts = qemu_opts_create(qemu_find_opts("chardev"), "chr1",
+                            1, &error_abort);
+    qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
+    qemu_opt_set(opts, "size", stringify(RB_SIZE), &error_abort);
+    qemu_opt_set(opts, "mux-be-id", "mux0", &error_abort);
+    chr1 = qemu_chr_new_from_opts(opts, NULL, &error_abort);
+    g_assert_nonnull(chr1);
+    qemu_opts_del(opts);
+
+    /* Create second chardev */
+    opts = qemu_opts_create(qemu_find_opts("chardev"), "chr2",
+                            1, &error_abort);
+    qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
+    qemu_opt_set(opts, "size", stringify(RB_SIZE), &error_abort);
+    qemu_opt_set(opts, "mux-be-id", "mux0", &error_abort);
+    chr2 = qemu_chr_new_from_opts(opts, NULL, &error_abort);
+    g_assert_nonnull(chr2);
+    qemu_opts_del(opts);
+
+    /* Attach mux-be to a frontend */
+    qemu_chr_fe_init(&chr_be, mux_be, &error_abort);
+    qemu_chr_fe_set_handlers(&chr_be,
+                             fe_can_read,
+                             fe_read,
+                             fe_event,
+                             NULL,
+                             &h,
+                             NULL, true);
+
+    /* Write to backend, chr1 */
+    base = qemu_chr_find("chr1");
+    g_assert_cmpint(qemu_chr_be_can_write(base), !=, 0);
+
+    qemu_chr_be_write(base, (void *)"hello", 6);
+    g_assert_cmpint(h.read_count, ==, 6);
+    g_assert_cmpstr(h.read_buf, ==, "hello");
+    h.read_count = 0;
+
+    /* Write to backend, chr2 */
+    base = qemu_chr_find("chr2");
+    g_assert_cmpint(qemu_chr_be_can_write(base), !=, 0);
+
+    qemu_chr_be_write(base, (void *)"olleh", 6);
+    g_assert_cmpint(h.read_count, ==, 6);
+    g_assert_cmpstr(h.read_buf, ==, "olleh");
+    h.read_count = 0;
+
+    /* Write to frontend, chr_be */
+    ret = qemu_chr_fe_write(&chr_be, (void *)"heyhey", 6);
+    g_assert_cmpint(ret, ==, 6);
+
+    data = qmp_ringbuf_read("chr1", RB_SIZE, false, 0, &error_abort);
+    g_assert_cmpint(strlen(data), ==, 6);
+    g_assert_cmpstr(data, ==, "heyhey");
+    g_free(data);
+
+    data = qmp_ringbuf_read("chr2", RB_SIZE, false, 0, &error_abort);
+    g_assert_cmpint(strlen(data), ==, 6);
+    g_assert_cmpstr(data, ==, "heyhey");
+    g_free(data);
+
+
+#ifndef _WIN32
+    /*
+     * Create third chardev to simulate EAGAIN and watcher.
+     * Mainly copied from char_pipe_test().
+     */
+    {
+        gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
+        gchar *in, *out, *pipe = g_build_filename(tmp_path, "pipe", NULL);
+        Chardev *chr3;
+        int fd, len;
+        char buf[128];
+
+        in = g_strdup_printf("%s.in", pipe);
+        if (mkfifo(in, 0600) < 0) {
+            abort();
+        }
+        out = g_strdup_printf("%s.out", pipe);
+        if (mkfifo(out, 0600) < 0) {
+            abort();
+        }
+
+        opts = qemu_opts_create(qemu_find_opts("chardev"), "chr3",
+                                1, &error_abort);
+        qemu_opt_set(opts, "backend", "pipe", &error_abort);
+        qemu_opt_set(opts, "path", pipe, &error_abort);
+        qemu_opt_set(opts, "mux-be-id", "mux0", &error_abort);
+        chr3 = qemu_chr_new_from_opts(opts, NULL, &error_abort);
+        g_assert_nonnull(chr3);
+
+        /* Write to frontend, chr_be */
+        ret = qemu_chr_fe_write(&chr_be, (void *)"thisis", 6);
+        g_assert_cmpint(ret, ==, 6);
+
+        data = qmp_ringbuf_read("chr1", RB_SIZE, false, 0, &error_abort);
+        g_assert_cmpint(strlen(data), ==, 6);
+        g_assert_cmpstr(data, ==, "thisis");
+        g_free(data);
+
+        data = qmp_ringbuf_read("chr2", RB_SIZE, false, 0, &error_abort);
+        g_assert_cmpint(strlen(data), ==, 6);
+        g_assert_cmpstr(data, ==, "thisis");
+        g_free(data);
+
+        fd = open(out, O_RDWR);
+        ret = read(fd, buf, sizeof(buf));
+        g_assert_cmpint(ret, ==, 6);
+        buf[ret] = 0;
+        g_assert_cmpstr(buf, ==, "thisis");
+        close(fd);
+
+        /* Add watch. 0 indicates no watches if nothing to wait for */
+        ret = qemu_chr_fe_add_watch(&chr_be, G_IO_OUT | G_IO_HUP,
+                                    NULL, NULL);
+        g_assert_cmpint(ret, ==, 0);
+
+        /*
+         * Write to frontend, chr_be, until EAGAIN. Make sure length is
+         * power of two to fit nicely the whole pipe buffer.
+         */
+        len = 0;
+        while ((ret = qemu_chr_fe_write(&chr_be, (void *)"thisisit", 8))
+               != -1) {
+            len += ret;
+        }
+        g_assert_cmpint(errno, ==, EAGAIN);
+
+        /* Further all writes should cause EAGAIN */
+        ret = qemu_chr_fe_write(&chr_be, (void *)"b", 1);
+        g_assert_cmpint(ret, ==, -1);
+        g_assert_cmpint(errno, ==, EAGAIN);
+
+        /*
+         * Add watch. Non 0 indicates we have a blocked chardev, which
+         * can wakes us up when write is possible.
+         */
+        ret = qemu_chr_fe_add_watch(&chr_be, G_IO_OUT | G_IO_HUP,
+                                    NULL, NULL);
+        g_assert_cmpint(ret, !=, 0);
+
+        /* Drain pipe and ring buffers */
+        fd = open(out, O_RDWR);
+        while ((ret = read(fd, buf, MIN(sizeof(buf), len))) != -1 && len > 0) {
+            len -= ret;
+        }
+        close(fd);
+
+        data = qmp_ringbuf_read("chr1", RB_SIZE, false, 0, &error_abort);
+        g_assert_cmpint(strlen(data), ==, 128);
+        g_free(data);
+
+        data = qmp_ringbuf_read("chr2", RB_SIZE, false, 0, &error_abort);
+        g_assert_cmpint(strlen(data), ==, 128);
+        g_free(data);
+
+        /*
+         * Now we are good to go, first repeat "lost" sequence, which
+         * was already consumed and drained by the ring buffers, but
+         * pipe have not recieved that yet.
+         */
+        ret = qemu_chr_fe_write(&chr_be, (void *)"thisisit", 8);
+        g_assert_cmpint(ret, ==, 8);
+
+        ret = qemu_chr_fe_write(&chr_be, (void *)"streamisrestored", 16);
+        g_assert_cmpint(ret, ==, 16);
+
+        data = qmp_ringbuf_read("chr1", RB_SIZE, false, 0, &error_abort);
+        g_assert_cmpint(strlen(data), ==, 16);
+        /* Only last 16 bytes, see big comment above */
+        g_assert_cmpstr(data, ==, "streamisrestored");
+        g_free(data);
+
+        data = qmp_ringbuf_read("chr2", RB_SIZE, false, 0, &error_abort);
+        g_assert_cmpint(strlen(data), ==, 16);
+        /* Only last 16 bytes, see big comment above */
+        g_assert_cmpstr(data, ==, "streamisrestored");
+        g_free(data);
+
+        fd = open(out, O_RDWR);
+        ret = read(fd, buf, sizeof(buf));
+        g_assert_cmpint(ret, ==, 24);
+        buf[ret] = 0;
+        /* Both 8 and 16 bytes */
+        g_assert_cmpstr(buf, ==, "thisisitstreamisrestored");
+        close(fd);
+    }
+#endif
+
+    /* Finalize */
+    qemu_chr_fe_deinit(&chr_be, false);
+}
 
 static void websock_server_read(void *opaque, const uint8_t *buf, int size)
 {
@@ -1484,7 +1696,8 @@ int main(int argc, char **argv)
     g_test_add_func("/char/null", char_null_test);
     g_test_add_func("/char/invalid", char_invalid_test);
     g_test_add_func("/char/ringbuf", char_ringbuf_test);
-    g_test_add_func("/char/mux", char_mux_test);
+    g_test_add_func("/char/mux", char_mux_fe_test);
+    g_test_add_func("/char/mux-be", char_mux_be_test);
 #ifdef _WIN32
     g_test_add_func("/char/console/subprocess", char_console_test_subprocess);
     g_test_add_func("/char/console", char_console_test);
Signature · 1 line
2.43.0
#5 lore raw

PATCH v2 5/5 qemu-options.hx: describe multiplexing of several backend devices

In reply to Roman Penyaev

This adds a few lines describing mux-be multiplexer configuration for multiplexing several backend devices with a single frontend device.

---
 qemu-options.hx | 46 ++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 42 insertions(+), 4 deletions(-)
qemu-options.hx +42 -4
index d94e2cbbaeb1..21f112c2e445 100644
@@ -3782,7 +3782,7 @@ SRST
 The general form of a character device option is:
 
 ``-chardev backend,id=id[,mux=on|off][,options]``
-    Backend is one of: ``null``, ``socket``, ``udp``, ``msmouse``,
+    Backend is one of: ``null``, ``socket``, ``udp``, ``msmouse``, ``mux-be``,
     ``vc``, ``ringbuf``, ``file``, ``pipe``, ``console``, ``serial``,
     ``pty``, ``stdio``, ``braille``, ``parallel``,
     ``spicevmc``, ``spiceport``. The specific backend will determine the
@@ -3839,9 +3839,10 @@ The general form of a character device option is:
     the QEMU monitor, and ``-nographic`` also multiplexes the console
     and the monitor to stdio.
 
-    There is currently no support for multiplexing in the other
-    direction (where a single QEMU front end takes input and output from
-    multiple chardevs).
+    If you need to multiplex in the opposite direction (where one QEMU
+    interface receives input and output from multiple chardev devices),
+    please refer to the paragraph below regarding chardev ``mux-be``
+    configuration.
 
     Every backend supports the ``logfile`` option, which supplies the
     path to a file to record all data transmitted via the backend. The
@@ -3941,6 +3942,43 @@ The available backends are:
     Forward QEMU's emulated msmouse events to the guest. ``msmouse``
     does not take any options.
 
+``-chardev mux-be,id=id``
+    Explicitly create chardev backend multiplexer with possibility to
+    multiplex in the opposite direction, where one QEMU interface
+    (frontend device) receives input and output from multiple chardev
+    backend devices.
+
+    For example the following is a use case of 2 backend devices: text
+    virtual console ``vc0`` and a socket ``sock0`` connected
+    to a single virtio hvc console frontend device with multiplexer
+    ``mux0`` help. Virtual console renders text to an image, which
+    can be shared over the VNC protocol, in turn socket backend provides
+    biderectional communication to the virtio hvc console over socket.
+    The example configuration can be the following:
+
+    ::
+
+       -chardev mux-be,id=mux0 \
+       -chardev socket,path=/tmp/sock,server=on,wait=off,id=sock0,mux-be-id=mux0 \
+       -chardev vc,id=vc0,mux-be-id=mux0 \
+       -device virtconsole,chardev=mux0 \
+       -vnc 0.0.0.0:0
+
+    Once QEMU starts VNC client and any TTY emulator can be used to
+    control a single hvc console:
+
+    ::
+
+       # VNC client
+       vncviewer :0
+
+       # TTY emulator
+       socat unix-connect:/tmp/sock pty,link=/tmp/pty & \
+       tio /tmp/pty
+
+    Multiplexing of several backend devices with serveral frontend devices
+    is not supported.
+
 ``-chardev vc,id=id[[,width=width][,height=height]][[,cols=cols][,rows=rows]]``
     Connect to a QEMU text console. ``vc`` may optionally be given a
     specific size.
Signature · 1 line
2.43.0
#6 lore raw
In reply to Roman Penyaev

Hi Roman

Quoted 61 lines · Roman Penyaev

Mux is a character backend (host side) device, which multiplexes multiple frontends with one backend device. The following is a few lines from the QEMU manpage [1]:

A multiplexer is a "1:N" device, and here the "1" end is your specified chardev backend, and the "N" end is the various parts of QEMU that can talk to a chardev.

But sadly multiple backends are not supported.

This work implements multiplexing capability of several backend devices, which opens up an opportunity to use a single frontend device on the guest, which can be manipulated from several backend devices.

The motivation is the EVE project [2], where it would be very convenient to have a virtio console frontend device on the guest that can be controlled from multiple backend devices. The following is an example of the QEMU command line:

   -chardev mux-be,id=mux0 \
   -chardev
socket,path=/tmp/sock,server=on,wait=off,id=sock0,mux-be-id=mux0 \
   -chardev vc,id=vc0,mux-be-id=mux0 \
   -device virtconsole,chardev=mux0 \
   -vnc 0.0.0.0:0

Which creates 2 backend devices: text virtual console (vc0) and a socket (sock0) connected to the single virtio hvc console with the backend multiplexer (mux0) help. vc0 renders text to an image, which can be shared over the VNC protocol. sock0 is a socket backend which provides biderectional communication to the virtio hvc console.

New type of multiplexer mux-be actually is an alias for the same MuxChardev struct, which uses same functions as for the original mux type, but supports multiplexing N backends with 1 frontend.

Once QEMU starts VNC client and any TTY emulator can be used to control a single hvc console, for example these two different consoles should have similar input and output due the buffer multiplexing:

# VNC client
vncviewer :0

# TTY emulator
socat unix-connect:/tmp/sock pty,link=/tmp/pty
tio /tmp/pty

Difference to the previous version:

* Separate type for the backend multiplexer mux-be
* Handle EAGAIN on write to the backend device
* Support of watch of previously failed backend device
* Proper json support of the mux-be-id option
* Unit test for the mux-be multiplexer

[1] https://www.qemu.org/docs/master/system/qemu-manpage.html#hxtool-6
[2] https://github.com/lf-edge/eve

Quoted 7 lines

Roman Penyaev (5):
chardev/char: introduce mux-be-id=ID option and _MUX_BE type chardev/char: rename mux_cnt to fe_cnt for the MuxChardev chardev/char-mux: implement backend chardev multiplexing tests/unit/test-char: add unit test for the mux-be multiplexer qemu-options.hx: describe multiplexing of several backend devices

Please rebase, it fails to apply cleanly on master.

Can you try to split MuxChardev in a base common class? You could have MuxBase or simply Mux abstract, with MuxFe (for 'mux') & MuxBe (for 'mux-be'). This should clarify the code a bit and avoid sharing the same struct with unused fields.

Thanks for the tests, that helps a lot!

Quoted 19 lines
 chardev/char-fe.c          |  14 ++-
 chardev/char-mux.c         | 212 +++++++++++++++++++++++++++++++-----
 chardev/char.c             |  57 ++++++++--
 chardev/chardev-internal.h |  33 +++++-
 include/chardev/char.h     |   1 +
 qapi/char.json             |   9 +-
 qemu-options.hx            |  46 +++++++-
 tests/unit/test-char.c     | 217 ++++++++++++++++++++++++++++++++++++-
 8 files changed, 538 insertions(+), 51 deletions(-)

--
2.43.0

Signature · 1 line
Marc-André Lureau
#7 lore raw

Hi Marc-André,

On Wed, Oct 9, 2024 at 8:23 PM Marc-André Lureau
<[email protected]> wrote:

[cut]

Quoted 3 lines

Please rebase, it fails to apply cleanly on master.

Yep.

Quoted 2 lines

Can you try to split MuxChardev in a base common class? You could have MuxBase or simply Mux abstract, with MuxFe (for 'mux') & MuxBe (for 'mux-be'). This should clarify the code a bit and avoid sharing the same struct with unused fields.

I can give it a try. Let's see how it goes.

--
Roman

Quoted 26 lines

Thanks for the tests, that helps a lot!

Quoted 11 lines
 chardev/char-fe.c          |  14 ++-
 chardev/char-mux.c         | 212 +++++++++++++++++++++++++++++++-----
 chardev/char.c             |  57 ++++++++--
 chardev/chardev-internal.h |  33 +++++-
 include/chardev/char.h     |   1 +
 qapi/char.json             |   9 +-
 qemu-options.hx            |  46 +++++++-
 tests/unit/test-char.c     | 217 ++++++++++++++++++++++++++++++++++++-
 8 files changed, 538 insertions(+), 51 deletions(-)
Quoted 8 lines

--
2.43.0

--
Marc-André Lureau