libkrun: Add vhost-user sound device support#605
libkrun: Add vhost-user sound device support#605dorindabassey wants to merge 8 commits intocontainers:mainfrom
Conversation
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) and forwards backend interrupts to guest. 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. 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>
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>
There was a problem hiding this comment.
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:
- add config support for vhost user devices (this is mostly general, we need that for other vhost-user devices)
- add the vhost-sound #define constants (and possibly other sound specific things)
- add support in the chroot_vm example (you can mention how to get
aplayto work in that commit message or in a comment somewhere that is great!)
| const VIRTIO_ID_SOUND: u32 = 25; | ||
| let config_size = match device_type { | ||
| VIRTIO_ID_SOUND => 16, // sizeof(struct virtio_snd_config) | ||
| _ => 256, | ||
| }; |
There was a problem hiding this comment.
This is quite unfortunate, if we need to know the size of the config, I think it should be configured the same way as number and sizes of virtqueues are, preferably specified in the C API, with a constant for the known value.
But if we keep adding more parameters to krun_add_vhost_device(), maybe it makes sense for that function to accept a whole struct krun_vhost_config. (and provide entire configured values of the krun_vhost_config struct for each device type).
There was a problem hiding this comment.
humm, I don't think this is something that should be user-configurable, because what's the point if config_size is Fixed by the virtio spec, it's different from num_queues and queue_sizes. I'd leave the current implementation as is correct. We could add a documentation constant to be clear.
There was a problem hiding this comment.
because what's the point if config_size is Fixed by the virtio spec
Main problem is that it can change with new features introduced by future protocol versions, so assuming the size based on device type is very brittle. One example is VIRTIO_SND_F_CTLS which adds another field to the struct for virtio-sound (introduced in virtio 1.3 spec).
Correct me if I am wrong, but the only reason you need to know the size of the config space is the cache? In that case maybe we could avoid knowing the size of the config space and implement the caching like this:
guest requests 0..4 of config -> CACHE MISS: we read into 0..4 cache -> return from cache
guest requests 7..9 of config -> CACHE MISS: we read into 4..9 into cache -> return from cache
guest requests 4..8 of config -> CACHE HIT -> return from cache
basically you just have a cache: Vec<u8> and extend it whenever guest tries to read past the end of the cache.
btw do other vmms (like QEMU) also cache the config space?
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. The implementation uses on-demand caching: when the guest first reads from config space, the frontend fetches the configuration from the backend via VHOST_USER_GET_CONFIG and caches it locally. Subsequent reads are served from the cache, avoiding repeated backend calls. 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). The 16-byte config space is required for proper negotiation with vhost-device-sound backend. 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>
c3668f2 to
23a83df
Compare
Fixed, Thanks! |
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