Skip to content

libkrun: Add vhost-user sound device support#605

Open
dorindabassey wants to merge 8 commits intocontainers:mainfrom
dorindabassey:vhost-user-snd
Open

libkrun: Add vhost-user sound device support#605
dorindabassey wants to merge 8 commits intocontainers:mainfrom
dorindabassey:vhost-user-snd

Conversation

@dorindabassey
Copy link
Copy Markdown
Collaborator

@dorindabassey dorindabassey commented Mar 24, 2026

Add support for vhost-user sound devices following
QEMU's vhost-user-snd implementation. This allows
libkrun VMs to use external vhost-user sound backends
(e.g., vhost-device-sound) for audio playback and capture.

Note: The ALSA default device in the guest may not work without
additional configuration. Use explicit device specification
(e.g., 'aplay -D hw:0,0 ') or create /etc/asound.conf
in the guest to set the default device.

Depends on #527 and containers/libkrunfw#114

Copy link
Copy Markdown
Collaborator

@mtjhrc mtjhrc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please split the commit and improve the commit messages a bit, it's quite hard to follow. It goes unnecessarily into internals of the virtio-sound spec ("The virtio-snd configuration space is read-only and contains device capabilities (jacks, streams, chmaps, controls).", but doesn't mention actual implementation details (I am wondering why do we need config_cache? - or at least mention existence of it).

Maybe split it into like 3 commits:

  1. add config support for vhost user devices (this is mostly general, we need that for other vhost-user devices)
  2. add the vhost-sound #define constants (and possibly other sound specific things)
  3. add support in the chroot_vm example (you can mention how to get aplay to work in that commit message or in a comment somewhere that is great!)

@dorindabassey
Copy link
Copy Markdown
Collaborator Author

Please split the commit and improve the commit messages a bit, it's quite hard to follow.

Fixed, Thanks!

@dorindabassey dorindabassey force-pushed the vhost-user-snd branch 2 times, most recently from ab3caa7 to 567dcc0 Compare April 2, 2026 13:58
Comment on lines +390 to +399
let offset_usize = offset as usize;
let end = offset_usize + data.len();
let mut config_buf = vec![0u8; end];

match frontend.get_config(
0,
end as u32,
VhostUserConfigFlags::empty(),
&mut config_buf,
) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intermediary buffer doesn't seem useful - is there a reason why we can't just do this?

Suggested change
let offset_usize = offset as usize;
let end = offset_usize + data.len();
let mut config_buf = vec![0u8; end];
match frontend.get_config(
0,
end as u32,
VhostUserConfigFlags::empty(),
&mut config_buf,
) {
match frontend.get_config(
offset as u32,
data.len() as u32,
VhostUserConfigFlags::empty(),
data,
) {

Sidenote: The protocol for VHOST_USER_GET_CONFIG is weird - I can't fathom why are we even sending the buffer (the last parameter of frontend.get_config) attached to the msg... But that concern is beyond libkrun project, nothing we can do about it directly here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right! there's no need for the intermediary buffer.

Dorinda Bassey and others added 4 commits April 8, 2026 15:22
Implement vhost-user support for connecting
to external virtio device backends running
in separate processes.
Add vhost-user feature flag, vhost dependency,
and krun_add_vhost_user_device() generalized
API for adding vhost-user devices.

Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
Add memfd-backed memory region creation to enable memory
sharing with vhost-user backends via FD passing. When
vhost-user is enabled, all guest RAM regions are created
with memfd backing instead of anonymous mmap.

This lays the groundwork for vhost-user device support
while maintaining backward compatibility such that the
VM boots normally with standard memory when vhost-user
is not configured.

Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
Implement generic vhost-user device wrapper
with connection, feature negotiation, and
Guest physical address(GPA) to Virtual
address(VA) translation. Supports protocol
feature negotiation (CONFIG, MQ).
Backend interrupts (vring_call_event) are
monitored by the EventManager and forwarded
to the guest without spawning additional threads.

Co-authored-by: Matej Hrica <mhrica@redhat.com>
Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
Add support for attaching vhost-user devices to the VM.
Devices are registered with the EventManager as subscribers
to integrate with the VMM's event loop for interrupt handling.

The VMM now automatically suppresses the implicit RNG
device when a vhost-user RNG is configured via
krun_add_vhost_user_device(), allowing seamless
switching between the standard virtio-rng and external
vhost-user-rng backend for better isolation and flexibility.

Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
Dorinda Bassey added 4 commits April 10, 2026 16:31
Adds --vhost-user-rng command line option to
specify a vhost-user RNG backend socket path.
When provided, the VM uses the external
vhost-user RNG device instead of the built-in
virtio-rng implementation.

Example usage:  ./examples/chroot_vm \
--vhost-user-rng=/tmp/vhost-rng.sock0 \
/ /bin/sh -c "head -c 32 /dev/hwrng | xxd"

Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
Implement read_config() for vhost-user devices using the
VHOST_USER_GET_CONFIG protocol message. This enables vhost-user
devices to expose their configuration space to the guest.

This provides a general mechanism for any vhost-user device that
needs to expose configuration to the guest (e.g., virtio-snd).

Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
Add constants and device-specific configuration for virtio-snd
(vhost-user sound device).

Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
Add --vhost-user-snd option to chroot_vm example,
allowing VMs to use external vhost-user sound backends
for audio playback and capture.

Usage:
  ./chroot_vm --vhost-user-snd=/path/to/sound.sock ...

Note: In the guest, the ALSA default device may not work without
additional configuration. Use explicit device specification:
  aplay -D hw:0,0 /path/to/audio.wav

Or create /etc/asound.conf in the guest:
  defaults.pcm.card 0
  defaults.pcm.device 0
  pcm.!default {
      type hw
      card 0
      device 0
  }

Tested with vhost-device-sound backend using PipeWire.

Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants