Skip to contentSkip to conversation

chardev: implement backend chardev multiplexing

PATCH Superseded Roman Penyaev 4 participants 9 messages Sep 13, 2024 – Sep 18, 2024

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

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 main motivation of this work is to use a virtio console frontend device on the guest, which can be manipulated from several backend devices. The following is QEMU command line example:

  -chardev socket,path=/tmp/sock,server=on,wait=off,id=sock0 \
  -chardev vc,id=vc0 \
  -chardev mux,id=mux0,chardev=vc0,,sock0 \
  -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 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.

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

[1] https://www.qemu.org/docs/master/system/qemu-manpage.html#hxtool-6

Roman Penyaev (2):
chardev: implement backend chardev multiplexing qemu-options.hx: describe multiplexing of several backend devices

 chardev/char-fe.c          |  14 +++--
 chardev/char-mux.c         | 120 +++++++++++++++++++++++++++++--------
 chardev/char.c             |   2 +-
 chardev/chardev-internal.h |   7 ++-
 qemu-options.hx            |  44 ++++++++++++--
 5 files changed, 151 insertions(+), 36 deletions(-)
Cc "Marc-André Lureau" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 chardev: implement backend chardev multiplexing

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 socket,path=/tmp/sock,server=on,wait=off,id=sock0 \
  -chardev vc,id=vc0 \
  -chardev mux,id=mux0,chardev=vc0,,sock0 \
  -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 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.

Cc "Marc-André Lureau" <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 chardev/char-fe.c          |  14 +++--
 chardev/char-mux.c         | 120 +++++++++++++++++++++++++++++--------
 chardev/char.c             |   2 +-
 chardev/chardev-internal.h |   7 ++-
 4 files changed, 111 insertions(+), 32 deletions(-)
chardev/char-fe.c +10 -4
index b214ba3802b1..d1f67338084d 100644
@@ -197,16 +197,22 @@ 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++;
+            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) {
             error_setg(errp, "chardev '%s' is already in use", s->label);
             return false;
chardev/char-mux.c +95 -25

203 lines not on this page · raw message

chardev/char.c +1 -1
index ba847b6e9eff..2643c79e5749 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 +5 -2
index 4e03af31476c..72c2e4da7552 100644
@@ -35,10 +35,13 @@
 
 struct MuxChardev {
     Chardev parent;
+    /* Linked frontends */
     CharBackend *backends[MAX_MUX];
-    CharBackend chr;
+    /* Linked backends */
+    CharBackend chrs[MAX_MUX];
     int focus;
-    int mux_cnt;
+    int fe_cnt;
+    int be_cnt;
     int term_got_escape;
     int max_size;
     /* Intermediate input buffer catches escape sequences even if the
Signature · 1 line
2.34.1
#2 lore raw

Hi Roman

Quoted 18 lines · 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 socket,path=/tmp/sock,server=on,wait=off,id=sock0 \
  -chardev vc,id=vc0 \
  -chardev mux,id=mux0,chardev=vc0,,sock0 \
  -device virtconsole,chardev=mux0 \
  -vnc 0.0.0.0:0
Quoted 6 lines

Which creates 2 backend devices: text virtual console (vc0) and a socket (sock0) connected to the single virtio hvc console with the 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.

I think I would rather implement a new mux for this purpose, like "mux-be" perhaps?

"mux" has been a bit hidden (behind mux=on) for various reasons, and is probably not at production quality level.

I am not sure how CLI should handle option arrays these days (especially with -chardev CLI not being json-friendly).

Quoted 57 lines
Cc "Marc-André Lureau" <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 chardev/char-fe.c          |  14 +++--
 chardev/char-mux.c         | 120 +++++++++++++++++++++++++++++--------
 chardev/char.c             |   2 +-
 chardev/chardev-internal.h |   7 ++-
 4 files changed, 111 insertions(+), 32 deletions(-)
chardev/char-fe.c +10 -4
index b214ba3802b1..d1f67338084d 100644
@@ -197,16 +197,22 @@ 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++;
+            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) {
             error_setg(errp, "chardev '%s' is already in use", s->label);
             return false;
chardev/char-mux.c +1 -0
index ee2d47b20d9b..82f728b5caf8 100644
@@ -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,39 @@
  */

chardev/char-mux.c line 51

Quoted 10 lines
chardev/char-mux.c +8 -0
 static bool muxes_opened = true;
 
+/* Write to all backends  */
+static int mux_chr_fe_write(MuxChardev *mux, const uint8_t *buf, int len)
+{
+        int r, ret = -1, i;
+
+        for (i = 0; i < mux->be_cnt; i++) {
+                r = qemu_chr_fe_write(&mux->chrs[i], buf, len);
+                ret = ret < 0 ? r : MAX(r, ret);

I think it should be conservative and fail early if one of the backend fails. Also if different frontends can write different amounts, there needs to be some buffering... or it should always use write_all() which has also a shortcoming since it blocks the thread.

Quoted 67 lines
chardev/char-mux.c +25 -7
+        }
+
+        return ret;
+}
+
+/* Write to all backends  */
+static int mux_chr_fe_write_all(MuxChardev *mux, const uint8_t *buf, int len)
+{
+        int r, ret = -1, i;
+
+        for (i = 0; i < mux->be_cnt; i++) {
+                r = qemu_chr_fe_write_all(&mux->chrs[i], buf, len);
+                ret = ret < 0 ? r : MAX(r, ret);
+        }
+
+        return ret;
+}
+
 /* 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 +98,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;
             }
@@ -168,9 +195,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 +275,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);
     }
 }

chardev/char-mux.c static void mux_chr_event(void *opaque, QEMUChrEvent event) · line 296

Quoted 10 lines
chardev/char-mux.c +5 -2
@@ -262,8 +289,16 @@ 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;
+
+    if (d->be_cnt > 1) {
+            /* TODO: multiple backends have to be combined on a single watch */

I think this must be done, otherwise there is a severe limitation.

Quoted 149 lines
chardev/char-mux.c +56 -16
+            return NULL;
+    }
+
+    chr = qemu_chr_fe_get_driver(&d->chrs[0]);
+    cc = CHARDEV_GET_CLASS(chr);
 
     if (!cc->chr_add_watch) {
         return NULL;
@@ -277,27 +312,32 @@ 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;
         }
     }
-    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)
@@ -305,7 +345,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);
@@ -324,19 +364,49 @@ static void qemu_chr_open_mux(Chardev *chr,
     ChardevMux *mux = backend->u.mux.data;
     Chardev *drv;
     MuxChardev *d = MUX_CHARDEV(chr);
-
-    drv = qemu_chr_find(mux->chardev);
-    if (drv == NULL) {
-        error_setg(errp, "mux: base chardev %s not found", mux->chardev);
+    const char *offset, *chardevs;
+    int length, i;
+
+    if (d->fe_cnt > 1) {
+        error_setg(errp,
+                   "multiplexed chardev '%s' is already used "
+                   "for frontend multiplexing",
+                   chr->label);
         return;
     }
 
+    chardevs = mux->chardev;
+    for (i = 0; i < MAX_MUX; i++) {
+        char *chardev;
+
+        offset = qemu_strchrnul(chardevs, ',');
+        length = offset - chardevs;
+        if (!length) {
+            break;
+        }
+        chardev = strndupa(chardevs, length);
+        chardevs += length + 1;
+        drv = qemu_chr_find(chardev);
+        if (drv == NULL) {
+            error_setg(errp, "mux: base chardev %s not found", chardev);
+            goto deinit_on_error;
+        }
+        qemu_chr_fe_init(&d->chrs[i], drv, errp);
+        d->be_cnt += 1;
+    }
     d->focus = -1;
     /* only default to opened state if we've realized the initial
      * set of muxes
      */
     *be_opened = muxes_opened;
-    qemu_chr_fe_init(&d->chr, drv, errp);
+
+    return;
+
+deinit_on_error:
+    for (i = 0; i < d->be_cnt; i++) {
+        qemu_chr_fe_deinit(&d->chrs[i], false);
+    }
+    d->be_cnt = 0;
 }
 
 static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
chardev/char.c +1 -1
index ba847b6e9eff..2643c79e5749 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 +5 -3
index 4e03af31476c..72c2e4da7552 100644
@@ -35,10 +35,13 @@
 
 struct MuxChardev {
     Chardev parent;
+    /* Linked frontends */
     CharBackend *backends[MAX_MUX];
-    CharBackend chr;
+    /* Linked backends */
+    CharBackend chrs[MAX_MUX];
     int focus;
-    int mux_cnt;
+    int fe_cnt;
+    int be_cnt;
     int term_got_escape;
     int max_size;
     /* Intermediate input buffer catches escape sequences even if the
--
Quoted 2 lines

2.34.1

It would also require some tests and QAPI support.

#3 lore raw
Quoted 29 lines · Marc-André Lureau

Hi Roman

Quoted 18 lines · 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 socket,path=/tmp/sock,server=on,wait=off,id=sock0 \
  -chardev vc,id=vc0 \
  -chardev mux,id=mux0,chardev=vc0,,sock0 \
  -device virtconsole,chardev=mux0 \
  -vnc 0.0.0.0:0
Quoted 6 lines

Which creates 2 backend devices: text virtual console (vc0) and a socket (sock0) connected to the single virtio hvc console with the 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.

Quoted 5 lines

I think I would rather implement a new mux for this purpose, like "mux-be" perhaps?

"mux" has been a bit hidden (behind mux=on) for various reasons, and is probably not at production quality level.

You get a mux by default (for serial vs HMP monitor), so I think it's pretty heavily used and tested in that sense...

-- PMM

#4 lore raw
Quoted 32 lines · Peter Maydell
Quoted 29 lines · Marc-André Lureau

Hi Roman

Quoted 24 lines · 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 socket,path=/tmp/sock,server=on,wait=off,id=sock0 \
  -chardev vc,id=vc0 \
  -chardev mux,id=mux0,chardev=vc0,,sock0 \
  -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 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.

Quoted 8 lines
Quoted 5 lines

I think I would rather implement a new mux for this purpose, like "mux-be" perhaps?

"mux" has been a bit hidden (behind mux=on) for various reasons, and is probably not at production quality level.

You get a mux by default (for serial vs HMP monitor), so I think it's pretty heavily used and tested in that sense...

I should have said "by default for any -nographic invocation"; that's still a pretty common usage pattern, though.

-- PMM

#5 lore raw
In reply to Marc-André Lureau

Hi Marc-André,

Quoted 29 lines · Marc-André Lureau

Hi Roman

Quoted 18 lines · 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 socket,path=/tmp/sock,server=on,wait=off,id=sock0 \
  -chardev vc,id=vc0 \
  -chardev mux,id=mux0,chardev=vc0,,sock0 \
  -device virtconsole,chardev=mux0 \
  -vnc 0.0.0.0:0
Quoted 6 lines

Which creates 2 backend devices: text virtual console (vc0) and a socket (sock0) connected to the single virtio hvc console with the 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.

Quoted 2 lines

I think I would rather implement a new mux for this purpose, like "mux-be" perhaps?

Do you mean a completely different implementation or just an alias for the cli needs? Because code of the backend multiplexing nicely fits current mux and I tried to avoid code duplication.

Quoted 3 lines

"mux" has been a bit hidden (behind mux=on) for various reasons, and is probably not at production quality level.

Well, indeed creating "chardev mux" is not described in the doc, but you can do this anyway :) Here I just followed the same pattern as we do for other devices: "chardev NAME". Having a "mux-be" (or any other) alias won't expose the default "mux", so configurations can be separated. Is that what you meant?

Also, mux is used implicitly for various of configurations, this is described in the manual:

Note that some other command line options may implicitly create multiplexed character backends; for instance -serial mon:stdio creates a multiplexed stdio backend connected to the serial port and the QEMU monitor, and -nographic also multiplexes the console and the
monitor to stdio.

So it seems to me mux is used (tested) quite extensively.

Quoted 3 lines

I am not sure how CLI should handle option arrays these days (especially with -chardev CLI not being json-friendly).

By CLI do you mean the default QEMU command line interface? The command line with arrays (double commas ",,") I specified above works fine. Can you suggest any other tool (or json formatting) you have in mind so I can verify?

Quoted 60 lines
Quoted 60 lines
Cc "Marc-André Lureau" <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 chardev/char-fe.c          |  14 +++--
 chardev/char-mux.c         | 120 +++++++++++++++++++++++++++++--------
 chardev/char.c             |   2 +-
 chardev/chardev-internal.h |   7 ++-
 4 files changed, 111 insertions(+), 32 deletions(-)
chardev/char-fe.c +10 -4
index b214ba3802b1..d1f67338084d 100644
@@ -197,16 +197,22 @@ 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++;
+            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) {
             error_setg(errp, "chardev '%s' is already in use", s->label);
             return false;
chardev/char-mux.c +1 -0
index ee2d47b20d9b..82f728b5caf8 100644
@@ -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,39 @@
  */
 static bool muxes_opened = true;
 
Quoted 13 lines

chardev/char-mux.c line 51

Quoted 8 lines
chardev/char-mux.c +8 -0
+/* Write to all backends  */
+static int mux_chr_fe_write(MuxChardev *mux, const uint8_t *buf, int len)
+{
+        int r, ret = -1, i;
+
+        for (i = 0; i < mux->be_cnt; i++) {
+                r = qemu_chr_fe_write(&mux->chrs[i], buf, len);
+                ret = ret < 0 ? r : MAX(r, ret);

I think it should be conservative and fail early if one of the backend fails. Also if different frontends can write different amounts, there needs to be some buffering... or it should always use write_all() which has also a shortcoming since it blocks the thread.

Yes, early fail and buffers make sense.

Quoted 72 lines
Quoted 72 lines
chardev/char-mux.c +25 -8
+        }
+
+        return ret;
+}
+
+/* Write to all backends  */
+static int mux_chr_fe_write_all(MuxChardev *mux, const uint8_t *buf, int len)
+{
+        int r, ret = -1, i;
+
+        for (i = 0; i < mux->be_cnt; i++) {
+                r = qemu_chr_fe_write_all(&mux->chrs[i], buf, len);
+                ret = ret < 0 ? r : MAX(r, ret);
+        }
+
+        return ret;
+}
+
 /* 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 +98,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;
             }
@@ -168,9 +195,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 +275,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);
     }
 }
@@ -262,8 +289,16 @@ 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);
Quoted 8 lines

chardev/char-mux.c static void mux_chr_event(void *opaque, QEMUChrEvent event) · line 296

Quoted 6 lines
chardev/char-mux.c +5 -1
-    ChardevClass *cc = CHARDEV_GET_CLASS(chr);
+    Chardev *chr;
+    ChardevClass *cc;
+
+    if (d->be_cnt > 1) {
+            /* TODO: multiple backends have to be combined on a single watch */

I think this must be done, otherwise there is a severe limitation.

Ok.

Quoted 147 lines
Quoted 147 lines
chardev/char-mux.c +56 -16
+            return NULL;
+    }
+
+    chr = qemu_chr_fe_get_driver(&d->chrs[0]);
+    cc = CHARDEV_GET_CLASS(chr);
 
     if (!cc->chr_add_watch) {
         return NULL;
@@ -277,27 +312,32 @@ 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;
         }
     }
-    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)
@@ -305,7 +345,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);
@@ -324,19 +364,49 @@ static void qemu_chr_open_mux(Chardev *chr,
     ChardevMux *mux = backend->u.mux.data;
     Chardev *drv;
     MuxChardev *d = MUX_CHARDEV(chr);
-
-    drv = qemu_chr_find(mux->chardev);
-    if (drv == NULL) {
-        error_setg(errp, "mux: base chardev %s not found", mux->chardev);
+    const char *offset, *chardevs;
+    int length, i;
+
+    if (d->fe_cnt > 1) {
+        error_setg(errp,
+                   "multiplexed chardev '%s' is already used "
+                   "for frontend multiplexing",
+                   chr->label);
         return;
     }
 
+    chardevs = mux->chardev;
+    for (i = 0; i < MAX_MUX; i++) {
+        char *chardev;
+
+        offset = qemu_strchrnul(chardevs, ',');
+        length = offset - chardevs;
+        if (!length) {
+            break;
+        }
+        chardev = strndupa(chardevs, length);
+        chardevs += length + 1;
+        drv = qemu_chr_find(chardev);
+        if (drv == NULL) {
+            error_setg(errp, "mux: base chardev %s not found", chardev);
+            goto deinit_on_error;
+        }
+        qemu_chr_fe_init(&d->chrs[i], drv, errp);
+        d->be_cnt += 1;
+    }
     d->focus = -1;
     /* only default to opened state if we've realized the initial
      * set of muxes
      */
     *be_opened = muxes_opened;
-    qemu_chr_fe_init(&d->chr, drv, errp);
+
+    return;
+
+deinit_on_error:
+    for (i = 0; i < d->be_cnt; i++) {
+        qemu_chr_fe_deinit(&d->chrs[i], false);
+    }
+    d->be_cnt = 0;
 }
 
 static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
chardev/char.c +1 -1
index ba847b6e9eff..2643c79e5749 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 +5 -2
index 4e03af31476c..72c2e4da7552 100644
@@ -35,10 +35,13 @@
 
 struct MuxChardev {
     Chardev parent;
+    /* Linked frontends */
     CharBackend *backends[MAX_MUX];
-    CharBackend chr;
+    /* Linked backends */
+    CharBackend chrs[MAX_MUX];
     int focus;
-    int mux_cnt;
+    int fe_cnt;
+    int be_cnt;
     int term_got_escape;
Quoted 8 lines
Quoted 5 lines
chardev/chardev-internal.h +0 -1
     int max_size;
     /* Intermediate input buffer catches escape sequences even if the
--

2.34.1

It would also require some tests and QAPI support.

I will take a look at tests and will try to come up with some extension for backend multiplexing.

Since the mux object API was not changed and the current change heavily relies on what is already in the code, do you think there should be any QAPI related change? In my understanding this should work out of the box (not tested though).

--
Roman

#6 lore raw

Hi

Quoted 34 lines · Roman Penyaev

Hi Marc-André,

Quoted 4 lines · Marc-André Lureau

Hi Roman

On Fri, Sep 13, 2024 at 8:37 PM Roman Penyaev <[email protected]>

Quoted 25 lines · wrote:
Quoted 24 lines

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 socket,path=/tmp/sock,server=on,wait=off,id=sock0 \
  -chardev vc,id=vc0 \
  -chardev mux,id=mux0,chardev=vc0,,sock0 \
  -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 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.

Quoted 7 lines
Quoted 2 lines

I think I would rather implement a new mux for this purpose, like "mux-be" perhaps?

Do you mean a completely different implementation or just an alias for the cli needs? Because code of the backend multiplexing nicely fits current mux and I tried to avoid code duplication.

It's not the same behaviour though, and discoverability/compatibility would be easier to handle. There is also various code in mux that isn't needed.

You can still factorize common code, or have a common base class. Eventually, the two chardev can be from the same file. I don't know if that makes sense.

Quoted 10 lines
Quoted 3 lines

"mux" has been a bit hidden (behind mux=on) for various reasons, and is probably not at production quality level.

Well, indeed creating "chardev mux" is not described in the doc, but you can do this anyway :) Here I just followed the same pattern as we do for other devices: "chardev NAME". Having a "mux-be" (or any other) alias won't expose the default "mux", so configurations can be separated. Is that what you meant?

yes, this would help in general misconfiguration (although -chardev option handling has a lot to be desired)

Quoted 4 lines

Also, mux is used implicitly for various of configurations, this is described in the manual:

Quoted 8 lines

Note that some other command line options may implicitly create multiplexed character backends; for instance -serial mon:stdio creates a multiplexed stdio backend connected to the serial port and the QEMU monitor, and -nographic also multiplexes the console and the
monitor to stdio.

So it seems to me mux is used (tested) quite extensively.

kinda, it's used by end-users who run qemu from the CLI. Probably less in production systems.

Quoted 7 lines
Quoted 4 lines

I am not sure how CLI should handle option arrays these days (especially with -chardev CLI not being json-friendly).

By CLI do you mean the default QEMU command line interface?

yes

Quoted 4 lines

The command line with arrays (double commas ",,") I specified above works fine. Can you suggest any other tool (or json formatting) you have in mind so I can verify?

Indeed, this double commas stuff is weird, I don't know that "trick". Is it used elsewhere and documented? I don't see a test in test-qemu-opts.c either.

There is another ongoing discussion about json support for -chardev:
"-chardev with a JSON argument".

Quoted 341 lines
Quoted 13 lines
Quoted 13 lines
Cc "Marc-André Lureau" <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 chardev/char-fe.c          |  14 +++--
 chardev/char-mux.c         | 120 +++++++++++++++++++++++++++++--------
 chardev/char.c             |   2 +-
 chardev/chardev-internal.h |   7 ++-
 4 files changed, 111 insertions(+), 32 deletions(-)
Quoted 5 lines

chardev/char-fe.c bool qemu_chr_fe_init(CharBackend *b, Chardev

Quoted 5 lines
chardev/char-fe.c +0 -0
index b214ba3802b1..d1f67338084d 100644
@@ -197,16 +197,22 @@ bool qemu_chr_fe_init(CharBackend *b, Chardev

*s, Error **errp)

Quoted 17 lines
Quoted 17 lines
chardev/char-fe.c +4 -4
         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++;
+            if (d->fe_cnt > 0 && d->be_cnt > 1) {
+                error_setg(errp,
+                           "multiplexed chardev '%s' is already used "
Quoted 8 lines

chardev/char-fe.c bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp)

Quoted 8 lines
chardev/char-fe.c +6 -0
+                           "for backend multiplexing",
+                           s->label);
+                return false;
+            }
+            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);

Quoted 17 lines
Quoted 17 lines
chardev/char-fe.c +0 -0
             return false;
chardev/char-mux.c +1 -0
index ee2d47b20d9b..82f728b5caf8 100644
@@ -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,39 @@
  */
 static bool muxes_opened = true;
 
Quoted 2 lines

chardev/char-mux.c line 45

Quoted 2 lines
chardev/char-mux.c +2 -0
+/* Write to all backends  */
+static int mux_chr_fe_write(MuxChardev *mux, const uint8_t *buf, int

len)

Quoted 11 lines

chardev/char-mux.c line 51

Quoted 6 lines
chardev/char-mux.c +6 -0
+{
+        int r, ret = -1, i;
+
+        for (i = 0; i < mux->be_cnt; i++) {
+                r = qemu_chr_fe_write(&mux->chrs[i], buf, len);
+                ret = ret < 0 ? r : MAX(r, ret);

I think it should be conservative and fail early if one of the backend fails. Also if different frontends can write different amounts, there needs to be some buffering... or it should always use write_all() which has also a shortcoming since it blocks the thread.

Yes, early fail and buffers make sense.

Quoted 8 lines

chardev/char-mux.c

Quoted 8 lines
chardev/char-mux.c +7 -0
+        }
+
+        return ret;
+}
+
+/* Write to all backends  */
+static int mux_chr_fe_write_all(MuxChardev *mux, const uint8_t *buf,

int len)

Quoted 22 lines
Quoted 22 lines
chardev/char-mux.c +12 -1
+{
+        int r, ret = -1, i;
+
+        for (i = 0; i < mux->be_cnt; i++) {
+                r = qemu_chr_fe_write_all(&mux->chrs[i], buf, len);
+                ret = ret < 0 ? r : MAX(r, ret);
+        }
+
+        return ret;
+}
+
 /* 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;
 
Quoted 1 line

chardev/char-mux.c static int mux_chr_write(Chardev *chr, const

Quoted 1 line
chardev/char-mux.c +0 -0
@@ -71,11 +98,11 @@ static int mux_chr_write(Chardev *chr, const

uint8_t *buf, int len)

Quoted 6 lines
Quoted 6 lines
chardev/char-mux.c +1 -1
                          (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));
Quoted 8 lines

chardev/char-mux.c static int mux_proc_byte(Chardev *chr, MuxChardev

Quoted 8 lines
chardev/char-mux.c +1 -1
                 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;
             }
@@ -168,9 +195,9 @@ static int mux_proc_byte(Chardev *chr, MuxChardev

*d, int ch)

Quoted 4 lines

chardev/char-mux.c static int mux_proc_byte(Chardev *chr, MuxChardev *d, int ch)

Quoted 4 lines
chardev/char-mux.c +0 -1
             qemu_chr_be_event(chr, CHR_EVENT_BREAK);
             break;
         case 'c':
-            assert(d->mux_cnt > 0); /* handler registered with first

fe */

Quoted 1 line

chardev/char-mux.c static int mux_proc_byte(Chardev *chr, MuxChardev · line 198

Quoted 1 line
chardev/char-mux.c +1 -0
+            assert(d->fe_cnt > 0); /* handler registered with first

fe */

Quoted 7 lines

chardev/char-mux.c void mux_chr_send_all_event(Chardev *chr,

Quoted 7 lines
chardev/char-mux.c +1 -1
             /* 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 +275,8 @@ void mux_chr_send_all_event(Chardev *chr,

QEMUChrEvent event)

Quoted 11 lines

chardev/char-mux.c static void mux_chr_event(void *opaque,

Quoted 11 lines
chardev/char-mux.c +2 -2
         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);
     }
 }
@@ -262,8 +289,16 @@ static void mux_chr_event(void *opaque,

QEMUChrEvent event)

Quoted 10 lines

chardev/char-mux.c static void mux_chr_event(void *opaque, QEMUChrEvent event)

Quoted 10 lines
chardev/char-mux.c +5 -2
 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;
+
+    if (d->be_cnt > 1) {
+            /* TODO: multiple backends have to be combined on a

single watch */

Quoted 2 lines

I think this must be done, otherwise there is a severe limitation.

Ok.

Quoted 59 lines
Quoted 59 lines
chardev/char-mux.c +21 -11
+            return NULL;
+    }
+
+    chr = qemu_chr_fe_get_driver(&d->chrs[0]);
+    cc = CHARDEV_GET_CLASS(chr);
 
     if (!cc->chr_add_watch) {
         return NULL;
@@ -277,27 +312,32 @@ 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;
         }
     }
-    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)
@@ -305,7 +345,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);
 
Quoted 10 lines

chardev/char-mux.c static void qemu_chr_open_mux(Chardev *chr, · line 330

Quoted 10 lines
chardev/char-mux.c +0 -4
     if (d->focus != -1) {
         mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
@@ -324,19 +364,49 @@ static void qemu_chr_open_mux(Chardev *chr,
     ChardevMux *mux = backend->u.mux.data;
     Chardev *drv;
     MuxChardev *d = MUX_CHARDEV(chr);
-
-    drv = qemu_chr_find(mux->chardev);
-    if (drv == NULL) {
-        error_setg(errp, "mux: base chardev %s not found",

mux->chardev);

Quoted 11 lines
Quoted 11 lines
chardev/char-mux.c +8 -0
+    const char *offset, *chardevs;
+    int length, i;
+
+    if (d->fe_cnt > 1) {
+        error_setg(errp,
+                   "multiplexed chardev '%s' is already used "
+                   "for frontend multiplexing",
+                   chr->label);
         return;
     }
 
Quoted 14 lines
Quoted 4 lines
chardev/char-mux.c +4 -0
+    chardevs = mux->chardev;
+    for (i = 0; i < MAX_MUX; i++) {
+        char *chardev;
+

chardev/char-mux.c static void qemu_chr_open_mux(Chardev *chr,

Quoted 10 lines
chardev/char-mux.c +10 -0
+        offset = qemu_strchrnul(chardevs, ',');
+        length = offset - chardevs;
+        if (!length) {
+            break;
+        }
+        chardev = strndupa(chardevs, length);
+        chardevs += length + 1;
+        drv = qemu_chr_find(chardev);
+        if (drv == NULL) {
+            error_setg(errp, "mux: base chardev %s not found",

chardev);

Quoted 21 lines
Quoted 21 lines
chardev/char-mux.c +13 -1
+            goto deinit_on_error;
+        }
+        qemu_chr_fe_init(&d->chrs[i], drv, errp);
+        d->be_cnt += 1;
+    }
     d->focus = -1;
     /* only default to opened state if we've realized the initial
      * set of muxes
      */
     *be_opened = muxes_opened;
-    qemu_chr_fe_init(&d->chr, drv, errp);
+
+    return;
+
+deinit_on_error:
+    for (i = 0; i < d->be_cnt; i++) {
+        qemu_chr_fe_deinit(&d->chrs[i], false);
+    }
+    d->be_cnt = 0;
 }
 
Quoted 1 line

chardev/char-mux.c static void qemu_chr_open_mux(Chardev *chr, · line 412

Quoted 1 line
chardev/char-mux.c +0 -0
 static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend

*backend,

Quoted 31 lines
Quoted 31 lines
chardev/char.c +1 -1
index ba847b6e9eff..2643c79e5749 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 +5 -2
index 4e03af31476c..72c2e4da7552 100644
@@ -35,10 +35,13 @@
 
 struct MuxChardev {
     Chardev parent;
+    /* Linked frontends */
     CharBackend *backends[MAX_MUX];
-    CharBackend chr;
+    /* Linked backends */
+    CharBackend chrs[MAX_MUX];
     int focus;
-    int mux_cnt;
+    int fe_cnt;
+    int be_cnt;
     int term_got_escape;
Quoted 8 lines
Quoted 5 lines
chardev/chardev-internal.h +0 -1
     int max_size;
     /* Intermediate input buffer catches escape sequences even if the
--

2.34.1

It would also require some tests and QAPI support.

I will take a look at tests and will try to come up with some extension for backend multiplexing.

Since the mux object API was not changed and the current change heavily relies on what is already in the code, do you think there should be any QAPI related change? In my understanding this should work out of the box (not tested though).

--
Roman

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

Hi,

Thanks for the review, I will try to come up with the next version of this series.

On Wed, Sep 18, 2024 at 12:52 PM Marc-André Lureau <[email protected]> wrote:
[cut]

Quoted 3 lines

Indeed, this double commas stuff is weird, I don't know that "trick". Is it used elsewhere and documented? I don't see a test in test-qemu-opts.c either.

In few places, for example here:
https://www.qemu.org/docs/master/system/qemu-manpage.html#options
(look for "double")

The second quote acts as an escape symbol to avoid splitting of the command line.
Then in the modified mux I used a single comma as a separator for an array of backends.

Quoted 2 lines

There is another ongoing discussion about json support for -chardev: "-chardev with a JSON argument".

Thanks, found.

--
Roman

#8 lore raw

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

In reply to Roman Penyaev

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

Cc "Marc-André Lureau" <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 qemu-options.hx | 44 ++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 40 insertions(+), 4 deletions(-)
qemu-options.hx +40 -4
index d94e2cbbaeb1..de1d540b85cd 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``,
     ``vc``, ``ringbuf``, ``file``, ``pipe``, ``console``, ``serial``,
     ``pty``, ``stdio``, ``braille``, ``parallel``,
     ``spicevmc``, ``spiceport``. The specific backend will determine the
@@ -3839,9 +3839,9 @@ 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 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 +3941,42 @@ The available backends are:
     Forward QEMU's emulated msmouse events to the guest. ``msmouse``
     does not take any options.
 
+``-chardev mux,id=id,chardev=chardev-id[,,chardev-idN]``
+    Explicitly create chardev 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 socket,path=/tmp/sock,server=on,wait=off,id=sock0 \
+        -chardev vc,id=vc0 \
+        -chardev mux,id=mux0,chardev=vc0,,sock0 \
+        -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.34.1