qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/5] chardev: implement backend chardev multiplexing
@ 2024-10-09 17:45 Roman Penyaev
  2024-10-09 17:45 ` [PATCH v2 1/5] chardev/char: introduce `mux-be-id=ID` option and _MUX_BE type Roman Penyaev
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: Roman Penyaev @ 2024-10-09 17:45 UTC (permalink / raw)
  Cc: Roman Penyaev, Marc-André Lureau, qemu-devel

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(-)

Signed-off-by: Roman Penyaev <r.peniaev@gmail.com>
Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>
Cc: qemu-devel@nongnu.org

-- 
2.43.0



^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 1/5] chardev/char: introduce `mux-be-id=ID` option and _MUX_BE type
  2024-10-09 17:45 [PATCH v2 0/5] chardev: implement backend chardev multiplexing Roman Penyaev
@ 2024-10-09 17:45 ` Roman Penyaev
  2024-10-09 17:45 ` [PATCH v2 2/5] chardev/char: rename `mux_cnt` to `fe_cnt` for the `MuxChardev` Roman Penyaev
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Roman Penyaev @ 2024-10-09 17:45 UTC (permalink / raw)
  Cc: Roman Penyaev, Marc-André Lureau, qemu-devel

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.

Signed-off-by: Roman Penyaev <r.peniaev@gmail.com>
Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>
Cc: qemu-devel@nongnu.org
---
 chardev/char.c         | 3 +++
 include/chardev/char.h | 1 +
 qapi/char.json         | 9 ++++++++-
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/chardev/char.c b/chardev/char.c
index ba847b6e9eff..4b3e45b2a128 100644
--- a/chardev/char.c
+++ b/chardev/char.c
@@ -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,
diff --git a/include/chardev/char.h b/include/chardev/char.h
index 01df55f9e8c8..f68f949f6ebc 100644
--- a/include/chardev/char.h
+++ b/include/chardev/char.h
@@ -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"
diff --git a/qapi/char.json b/qapi/char.json
index ef58445ceec9..30748784513b 100644
--- a/qapi/char.json
+++ b/qapi/char.json
@@ -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' },
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 2/5] chardev/char: rename `mux_cnt` to `fe_cnt` for the `MuxChardev`
  2024-10-09 17:45 [PATCH v2 0/5] chardev: implement backend chardev multiplexing Roman Penyaev
  2024-10-09 17:45 ` [PATCH v2 1/5] chardev/char: introduce `mux-be-id=ID` option and _MUX_BE type Roman Penyaev
@ 2024-10-09 17:45 ` Roman Penyaev
  2024-10-09 17:45 ` [PATCH v2 3/5] chardev/char-mux: implement backend chardev multiplexing Roman Penyaev
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Roman Penyaev @ 2024-10-09 17:45 UTC (permalink / raw)
  Cc: Roman Penyaev, Marc-André Lureau, qemu-devel

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.

Signed-off-by: Roman Penyaev <r.peniaev@gmail.com>
Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>
Cc: qemu-devel@nongnu.org
---
 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(-)

diff --git a/chardev/char-fe.c b/chardev/char-fe.c
index b214ba3802b1..e6f44801162a 100644
--- a/chardev/char-fe.c
+++ b/chardev/char-fe.c
@@ -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;
diff --git a/chardev/char-mux.c b/chardev/char-mux.c
index ee2d47b20d9b..eb72b3cdb80b 100644
--- a/chardev/char-mux.c
+++ b/chardev/char-mux.c
@@ -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);
diff --git a/chardev/char.c b/chardev/char.c
index 4b3e45b2a128..3f0fcc8b16f6 100644
--- a/chardev/char.c
+++ b/chardev/char.c
@@ -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;
     }
diff --git a/chardev/chardev-internal.h b/chardev/chardev-internal.h
index 4e03af31476c..e8c3c29d1a59 100644
--- a/chardev/chardev-internal.h
+++ b/chardev/chardev-internal.h
@@ -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
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 3/5] chardev/char-mux: implement backend chardev multiplexing
  2024-10-09 17:45 [PATCH v2 0/5] chardev: implement backend chardev multiplexing Roman Penyaev
  2024-10-09 17:45 ` [PATCH v2 1/5] chardev/char: introduce `mux-be-id=ID` option and _MUX_BE type Roman Penyaev
  2024-10-09 17:45 ` [PATCH v2 2/5] chardev/char: rename `mux_cnt` to `fe_cnt` for the `MuxChardev` Roman Penyaev
@ 2024-10-09 17:45 ` Roman Penyaev
  2024-10-09 17:45 ` [PATCH v2 4/5] tests/unit/test-char: add unit test for the `mux-be` multiplexer Roman Penyaev
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Roman Penyaev @ 2024-10-09 17:45 UTC (permalink / raw)
  Cc: Roman Penyaev, Marc-André Lureau, qemu-devel

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.

Signed-off-by: Roman Penyaev <r.peniaev@gmail.com>
Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>
Cc: qemu-devel@nongnu.org
---
 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(-)

diff --git a/chardev/char-fe.c b/chardev/char-fe.c
index e6f44801162a..d1f67338084d 100644
--- a/chardev/char-fe.c
+++ b/chardev/char-fe.c
@@ -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) {
diff --git a/chardev/char-mux.c b/chardev/char-mux.c
index eb72b3cdb80b..c79cee008c17 100644
--- a/chardev/char-mux.c
+++ b/chardev/char-mux.c
@@ -26,6 +26,7 @@
 #include "qapi/error.h"
 #include "qemu/module.h"
 #include "qemu/option.h"
+#include "qemu/cutils.h"
 #include "chardev/char.h"
 #include "sysemu/block-backend.h"
 #include "qapi/qapi-commands-control.h"
@@ -40,13 +41,71 @@
  */
 static bool muxes_opened = true;
 
+/*
+ * Write to all backends. Different backend devices accept data with
+ * various rate, so it is quite possible that one device returns less,
+ * then others. In this case we return minimum to the caller,
+ * expecting caller will repeat operation soon. When repeat happens
+ * send to the devices which consume data faster must be avoided
+ * for obvious reasons not to send data, which was already sent.
+ */
+static int mux_chr_fe_write_to_all(MuxChardev *mux, const uint8_t *buf, int len,
+                                   int (*write_func)(CharBackend *,
+                                                     const uint8_t *buf,
+                                                     int len))
+{
+    int r, i, ret = len;
+    unsigned int written;
+
+    for (i = 0; i < mux->be_cnt; i++) {
+        written = mux->be_written[i] - mux->be_min_written;
+        if (written) {
+            /* Written in the previous call so take into account */
+            ret = MIN(written, ret);
+            continue;
+        }
+        r = write_func(&mux->chrs[i], buf, len);
+        if (!mux->is_mux_be || (r < 0 && errno == EAGAIN)) {
+            /*
+             * Fail immediately if this is a frontend multiplexer or
+             * write would block. Expect to be called soon on watch
+             * wake up.
+             */
+            return r;
+        } else if (r < 0) {
+            /*
+             * Ignore all other errors and pretend the entire buffer is
+             * written to avoid this chardev being watched. This device
+             * becomes disabled until the following write succeeds, but
+             * writing continues to others.
+             */
+            r = len;
+        }
+        mux->be_written[i] += r;
+        ret = MIN(r, ret);
+    }
+    mux->be_min_written += ret;
+
+    return ret;
+}
+
+static int mux_chr_fe_write(MuxChardev *mux, const uint8_t *buf, int len)
+{
+    return mux_chr_fe_write_to_all(mux, buf, len, qemu_chr_fe_write);
+}
+
+static int mux_chr_fe_write_all(MuxChardev *mux, const uint8_t *buf, int len)
+{
+    return mux_chr_fe_write_to_all(mux, buf, len, qemu_chr_fe_write_all);
+}
+
 /* Called with chr_write_lock held.  */
 static int mux_chr_write(Chardev *chr, const uint8_t *buf, int len)
 {
     MuxChardev *d = MUX_CHARDEV(chr);
     int ret;
     if (!d->timestamps) {
-        ret = qemu_chr_fe_write(&d->chr, buf, len);
+        ret = mux_chr_fe_write(d, buf, len);
     } else {
         int i;
 
@@ -71,11 +130,11 @@ static int mux_chr_write(Chardev *chr, const uint8_t *buf, int len)
                          (int)(ti % 1000));
                 /* XXX this blocks entire thread. Rewrite to use
                  * qemu_chr_fe_write and background I/O callbacks */
-                qemu_chr_fe_write_all(&d->chr,
+                mux_chr_fe_write_all(d,
                                       (uint8_t *)buf1, strlen(buf1));
                 d->linestart = 0;
             }
-            ret += qemu_chr_fe_write(&d->chr, buf + i, 1);
+            ret += mux_chr_fe_write(d, buf + i, 1);
             if (buf[i] == '\n') {
                 d->linestart = 1;
             }
@@ -262,14 +321,35 @@ static void mux_chr_event(void *opaque, QEMUChrEvent event)
 static GSource *mux_chr_add_watch(Chardev *s, GIOCondition cond)
 {
     MuxChardev *d = MUX_CHARDEV(s);
-    Chardev *chr = qemu_chr_fe_get_driver(&d->chr);
-    ChardevClass *cc = CHARDEV_GET_CLASS(chr);
+    Chardev *chr;
+    ChardevClass *cc;
+    unsigned int written;
+    int i;
+
+    for (i = 0; i < d->be_cnt; i++) {
+        written = d->be_written[i] - d->be_min_written;
+        if (written) {
+            /* We skip the device with already written buffer */
+            continue;
+        }
+
+        /*
+         * The first device that has no data written to it must be
+         * the device that recently returned EAGAIN and should be
+         * watched.
+         */
+
+        chr = qemu_chr_fe_get_driver(&d->chrs[i]);
+        cc = CHARDEV_GET_CLASS(chr);
+
+        if (!cc->chr_add_watch) {
+            return NULL;
+        }
 
-    if (!cc->chr_add_watch) {
-        return NULL;
+        return cc->chr_add_watch(chr, cond);
     }
 
-    return cc->chr_add_watch(chr, cond);
+    return NULL;
 }
 
 static void char_mux_finalize(Object *obj)
@@ -283,21 +363,26 @@ static void char_mux_finalize(Object *obj)
             be->chr = NULL;
         }
     }
-    qemu_chr_fe_deinit(&d->chr, false);
+    for (i = 0; i < d->be_cnt; i++) {
+        qemu_chr_fe_deinit(&d->chrs[i], false);
+    }
 }
 
 static void mux_chr_update_read_handlers(Chardev *chr)
 {
     MuxChardev *d = MUX_CHARDEV(chr);
+    int i;
 
-    /* Fix up the real driver with mux routines */
-    qemu_chr_fe_set_handlers_full(&d->chr,
-                                  mux_chr_can_read,
-                                  mux_chr_read,
-                                  mux_chr_event,
-                                  NULL,
-                                  chr,
-                                  chr->gcontext, true, false);
+    for (i = 0; i < d->be_cnt; i++) {
+        /* Fix up the real driver with mux routines */
+        qemu_chr_fe_set_handlers_full(&d->chrs[i],
+                                      mux_chr_can_read,
+                                      mux_chr_read,
+                                      mux_chr_event,
+                                      NULL,
+                                      chr,
+                                      chr->gcontext, true, false);
+    }
 }
 
 void mux_set_focus(Chardev *chr, int focus)
@@ -316,6 +401,33 @@ void mux_set_focus(Chardev *chr, int focus)
     mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
 }
 
+bool mux_chr_attach_chardev(MuxChardev *d, Chardev *chr, Error **errp)
+{
+    bool r;
+
+    if (d->fe_cnt > 1) {
+        error_setg(errp,
+                   "multiplexed chardev '%s' is already used "
+                   "for frontend multiplexing",
+                   chr->label);
+        return false;
+    }
+    if (d->be_cnt >= MAX_MUX) {
+        error_setg(errp, "too many uses of multiplexed chardev '%s'"
+                   " (maximum is " stringify(MAX_MUX) ")",
+                   d->parent.label);
+        return false;
+    }
+    r = qemu_chr_fe_init(&d->chrs[d->be_cnt], chr, errp);
+    if (r) {
+        /* Catch up with what was already written */
+        d->be_written[d->be_cnt] = d->be_min_written;
+        d->be_cnt += 1;
+    }
+
+    return r;
+}
+
 static void qemu_chr_open_mux(Chardev *chr,
                               ChardevBackend *backend,
                               bool *be_opened,
@@ -336,7 +448,7 @@ static void qemu_chr_open_mux(Chardev *chr,
      * set of muxes
      */
     *be_opened = muxes_opened;
-    qemu_chr_fe_init(&d->chr, drv, errp);
+    mux_chr_attach_chardev(d, drv, errp);
 }
 
 static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
@@ -355,6 +467,31 @@ static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
     mux->chardev = g_strdup(chardev);
 }
 
+static void qemu_chr_open_mux_be(Chardev *chr,
+                                 ChardevBackend *backend,
+                                 bool *be_opened,
+                                 Error **errp)
+{
+    MuxChardev *d = MUX_CHARDEV(chr);
+
+    /*
+     * only default to opened state if we've realized the initial
+     * set of muxes
+     */
+    *be_opened = muxes_opened;
+    d->is_mux_be = true;
+}
+
+static void qemu_chr_parse_mux_be(QemuOpts *opts, ChardevBackend *backend,
+                                  Error **errp)
+{
+    ChardevMux *mux;
+
+    backend->type = CHARDEV_BACKEND_KIND_MUX_BE;
+    mux = backend->u.mux.data = g_new0(ChardevMux, 1);
+    qemu_chr_parse_common(opts, qapi_ChardevMux_base(mux));
+}
+
 /**
  * Called after processing of default and command-line-specified
  * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
@@ -415,7 +552,19 @@ static void char_mux_class_init(ObjectClass *oc, void *data)
     cc->chr_update_read_handler = mux_chr_update_read_handlers;
 }
 
-static const TypeInfo char_mux_type_info = {
+static void char_mux_be_class_init(ObjectClass *oc, void *data)
+{
+    ChardevClass *cc = CHARDEV_CLASS(oc);
+
+    char_mux_class_init(oc, data);
+
+    /* Callbacks related to the mux-be device */
+    cc->parse = qemu_chr_parse_mux_be;
+    cc->open = qemu_chr_open_mux_be;
+}
+
+/* Multiplexes 1 backend to N frontends */
+static const TypeInfo char_mux_fe_type_info = {
     .name = TYPE_CHARDEV_MUX,
     .parent = TYPE_CHARDEV,
     .class_init = char_mux_class_init,
@@ -423,9 +572,20 @@ static const TypeInfo char_mux_type_info = {
     .instance_finalize = char_mux_finalize,
 };
 
+/* Multiplexes N backends to 1 frontend */
+static const TypeInfo char_mux_be_type_info = {
+    .name = TYPE_CHARDEV_MUX_BE,
+    .parent = TYPE_CHARDEV,
+    .class_init = char_mux_be_class_init,
+    .instance_size = sizeof(MuxChardev),
+    .instance_finalize = char_mux_finalize,
+};
+
 static void register_types(void)
 {
-    type_register_static(&char_mux_type_info);
+    /* Register two multiplexers */
+    type_register_static(&char_mux_fe_type_info);
+    type_register_static(&char_mux_be_type_info);
 }
 
 type_init(register_types);
diff --git a/chardev/char.c b/chardev/char.c
index 3f0fcc8b16f6..20f4e2bdd3e6 100644
--- a/chardev/char.c
+++ b/chardev/char.c
@@ -636,7 +636,8 @@ static Chardev *__qemu_chr_new_from_opts(QemuOpts *opts, GMainContext *context,
     ChardevBackend *backend = NULL;
     const char *name = qemu_opt_get(opts, "backend");
     const char *id = qemu_opts_id(opts);
-    char *bid = NULL;
+    const char *mux_be_id = NULL;
+    char *mux_fe_id = NULL;
 
     if (name && is_help_option(name)) {
         GString *str = g_string_new("");
@@ -664,10 +665,16 @@ static Chardev *__qemu_chr_new_from_opts(QemuOpts *opts, GMainContext *context,
     }
 
     if (qemu_opt_get_bool(opts, "mux", 0)) {
-        bid = g_strdup_printf("%s-base", id);
+        mux_fe_id = g_strdup_printf("%s-base", id);
+    }
+    mux_be_id = qemu_opt_get(opts, "mux-be-id");
+    if (mux_be_id && mux_fe_id) {
+        error_setg(errp, "chardev: mux and mux-be can't be used for the same "
+                   "device");
+        goto out;
     }
 
-    chr = qemu_chardev_new(bid ? bid : id,
+    chr = qemu_chardev_new(mux_fe_id ? mux_fe_id : id,
                            object_class_get_name(OBJECT_CLASS(cc)),
                            backend, context, errp);
     if (chr == NULL) {
@@ -675,25 +682,47 @@ static Chardev *__qemu_chr_new_from_opts(QemuOpts *opts, GMainContext *context,
     }
 
     base = chr;
-    if (bid) {
+    if (mux_fe_id) {
         Chardev *mux;
         qapi_free_ChardevBackend(backend);
         backend = g_new0(ChardevBackend, 1);
         backend->type = CHARDEV_BACKEND_KIND_MUX;
         backend->u.mux.data = g_new0(ChardevMux, 1);
-        backend->u.mux.data->chardev = g_strdup(bid);
+        backend->u.mux.data->chardev = g_strdup(mux_fe_id);
         mux = qemu_chardev_new(id, TYPE_CHARDEV_MUX, backend, context, errp);
         if (mux == NULL) {
-            object_unparent(OBJECT(chr));
-            chr = NULL;
-            goto out;
+            goto unparent_chr;
         }
         chr = mux;
+    } else if (mux_be_id) {
+        MuxChardev *d;
+        Chardev *s;
+
+        s = qemu_chr_find(mux_be_id);
+        if (!s) {
+            error_setg(errp, "chardev: mux-be device can't be found by id '%s'",
+                       mux_be_id);
+            goto unparent_chr;
+        }
+        if (!CHARDEV_IS_MUX(s)) {
+            error_setg(errp, "chardev: device '%s' is not a multiplexer device",
+                       mux_be_id);
+            goto unparent_chr;
+        }
+        d = MUX_CHARDEV(s);
+        if (!d->is_mux_be) {
+            error_setg(errp, "chardev: device '%s' is not a multiplexer device"
+                       " of 'mux-de' type", mux_be_id);
+            goto unparent_chr;
+        }
+        if (!mux_chr_attach_chardev(d, chr, errp)) {
+            goto unparent_chr;
+        }
     }
 
 out:
     qapi_free_ChardevBackend(backend);
-    g_free(bid);
+    g_free(mux_fe_id);
 
     if (replay && base) {
         /* RR should be set on the base device, not the mux */
@@ -701,6 +730,11 @@ out:
     }
 
     return chr;
+
+unparent_chr:
+    object_unparent(OBJECT(chr));
+    chr = NULL;
+    goto out;
 }
 
 Chardev *qemu_chr_new_from_opts(QemuOpts *opts, GMainContext *context,
diff --git a/chardev/chardev-internal.h b/chardev/chardev-internal.h
index e8c3c29d1a59..07a917737fc3 100644
--- a/chardev/chardev-internal.h
+++ b/chardev/chardev-internal.h
@@ -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);
 
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 4/5] tests/unit/test-char: add unit test for the `mux-be` multiplexer
  2024-10-09 17:45 [PATCH v2 0/5] chardev: implement backend chardev multiplexing Roman Penyaev
                   ` (2 preceding siblings ...)
  2024-10-09 17:45 ` [PATCH v2 3/5] chardev/char-mux: implement backend chardev multiplexing Roman Penyaev
@ 2024-10-09 17:45 ` Roman Penyaev
  2024-10-09 17:45 ` [PATCH v2 5/5] qemu-options.hx: describe multiplexing of several backend devices Roman Penyaev
  2024-10-09 18:23 ` [PATCH v2 0/5] chardev: implement backend chardev multiplexing Marc-André Lureau
  5 siblings, 0 replies; 8+ messages in thread
From: Roman Penyaev @ 2024-10-09 17:45 UTC (permalink / raw)
  Cc: Roman Penyaev, Marc-André Lureau, qemu-devel

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

Signed-off-by: Roman Penyaev <r.peniaev@gmail.com>
Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>
Cc: qemu-devel@nongnu.org
---
 tests/unit/test-char.c | 217 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 215 insertions(+), 2 deletions(-)

diff --git a/tests/unit/test-char.c b/tests/unit/test-char.c
index f273ce522612..554129c3b1a9 100644
--- a/tests/unit/test-char.c
+++ b/tests/unit/test-char.c
@@ -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);
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 5/5] qemu-options.hx: describe multiplexing of several backend devices
  2024-10-09 17:45 [PATCH v2 0/5] chardev: implement backend chardev multiplexing Roman Penyaev
                   ` (3 preceding siblings ...)
  2024-10-09 17:45 ` [PATCH v2 4/5] tests/unit/test-char: add unit test for the `mux-be` multiplexer Roman Penyaev
@ 2024-10-09 17:45 ` Roman Penyaev
  2024-10-09 18:23 ` [PATCH v2 0/5] chardev: implement backend chardev multiplexing Marc-André Lureau
  5 siblings, 0 replies; 8+ messages in thread
From: Roman Penyaev @ 2024-10-09 17:45 UTC (permalink / raw)
  Cc: Roman Penyaev, Marc-André Lureau, qemu-devel

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

Signed-off-by: Roman Penyaev <r.peniaev@gmail.com>
Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>
Cc: qemu-devel@nongnu.org
---
 qemu-options.hx | 46 ++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 42 insertions(+), 4 deletions(-)

diff --git a/qemu-options.hx b/qemu-options.hx
index d94e2cbbaeb1..21f112c2e445 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -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.
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 0/5] chardev: implement backend chardev multiplexing
  2024-10-09 17:45 [PATCH v2 0/5] chardev: implement backend chardev multiplexing Roman Penyaev
                   ` (4 preceding siblings ...)
  2024-10-09 17:45 ` [PATCH v2 5/5] qemu-options.hx: describe multiplexing of several backend devices Roman Penyaev
@ 2024-10-09 18:23 ` Marc-André Lureau
  2024-10-10  8:34   ` Roman Penyaev
  5 siblings, 1 reply; 8+ messages in thread
From: Marc-André Lureau @ 2024-10-09 18:23 UTC (permalink / raw)
  To: Roman Penyaev; +Cc: qemu-devel

[-- Attachment #1: Type: text/plain, Size: 3919 bytes --]

Hi Roman

On Wed, Oct 9, 2024 at 9:47 PM Roman Penyaev <r.peniaev@gmail.com> wrote:

> 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
>

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!


>  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(-)
>
> Signed-off-by: Roman Penyaev <r.peniaev@gmail.com>
> Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>
> Cc: qemu-devel@nongnu.org
>
> --
> 2.43.0
>
>
>

-- 
Marc-André Lureau

[-- Attachment #2: Type: text/html, Size: 5317 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 0/5] chardev: implement backend chardev multiplexing
  2024-10-09 18:23 ` [PATCH v2 0/5] chardev: implement backend chardev multiplexing Marc-André Lureau
@ 2024-10-10  8:34   ` Roman Penyaev
  0 siblings, 0 replies; 8+ messages in thread
From: Roman Penyaev @ 2024-10-10  8:34 UTC (permalink / raw)
  To: Marc-André Lureau; +Cc: qemu-devel

Hi Marc-André,

On Wed, Oct 9, 2024 at 8:23 PM Marc-André Lureau
<marcandre.lureau@gmail.com> wrote:

[cut]

>
>
> Please rebase, it fails to apply cleanly on master.

Yep.

>
> 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


>
> Thanks for the tests, that helps a lot!
>
>>
>>  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(-)
>>
>> Signed-off-by: Roman Penyaev <r.peniaev@gmail.com>
>> Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>
>> Cc: qemu-devel@nongnu.org
>>
>> --
>> 2.43.0
>>
>>
>
>
> --
> Marc-André Lureau


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-10-10  8:37 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-09 17:45 [PATCH v2 0/5] chardev: implement backend chardev multiplexing Roman Penyaev
2024-10-09 17:45 ` [PATCH v2 1/5] chardev/char: introduce `mux-be-id=ID` option and _MUX_BE type Roman Penyaev
2024-10-09 17:45 ` [PATCH v2 2/5] chardev/char: rename `mux_cnt` to `fe_cnt` for the `MuxChardev` Roman Penyaev
2024-10-09 17:45 ` [PATCH v2 3/5] chardev/char-mux: implement backend chardev multiplexing Roman Penyaev
2024-10-09 17:45 ` [PATCH v2 4/5] tests/unit/test-char: add unit test for the `mux-be` multiplexer Roman Penyaev
2024-10-09 17:45 ` [PATCH v2 5/5] qemu-options.hx: describe multiplexing of several backend devices Roman Penyaev
2024-10-09 18:23 ` [PATCH v2 0/5] chardev: implement backend chardev multiplexing Marc-André Lureau
2024-10-10  8:34   ` Roman Penyaev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).