mbox series

[v2,0/5] chardev: implement backend chardev multiplexing

Message ID 20241009174517.286935-1-r.peniaev@gmail.com (mailing list archive)
Headers show
Series chardev: implement backend chardev multiplexing | expand

Message

Roman Penyaev Oct. 9, 2024, 5:45 p.m. UTC
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

Comments

Marc-André Lureau Oct. 9, 2024, 6:23 p.m. UTC | #1
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
>
>
>
Roman Penyaev Oct. 10, 2024, 8:34 a.m. UTC | #2
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