Add vhost-user device support (RNG, sound, vsock, CAN, console, input, rtc)#642
Add vhost-user device support (RNG, sound, vsock, CAN, console, input, rtc)#642slp merged 14 commits intocontainers:mainfrom
Conversation
bilelmoussaoui
left a comment
There was a problem hiding this comment.
various comments
54cf7eb to
6db7dd8
Compare
|
Thanks for the reviews @bilelmoussaoui! |
038d252 to
945517b
Compare
| */ | ||
| int32_t krun_add_vhost_user_device(uint32_t ctx_id, | ||
| uint32_t device_type, | ||
| const char *socket_path, |
There was a problem hiding this comment.
Does this need to be a path at the API level? What if we already have a file descriptor?
There was a problem hiding this comment.
Fair, we should probably support that, but this is not really an issue blocking the current PR really, this is getting merged into main which is now libkrun 2.x branch. Every public API function is about to get replaced. #634
e2388ed to
44c9c19
Compare
mtjhrc
left a comment
There was a problem hiding this comment.
LGTM, thanks!
Code LGTM. I've only tested Sound and Vsock specifically now (and NET before which is not in the list of "officially" supported devices yet).
| event_manager | ||
| .unregister(self.activate_evt.as_raw_fd()) | ||
| .unwrap_or_else(|e| { | ||
| error!( | ||
| "{}: failed to unregister activate event: {e:?}", | ||
| self.device_name | ||
| ); | ||
| }); |
There was a problem hiding this comment.
This is not really correct, you will miss registering the file descriptors on second activate() call after reset. But It still wouldn't work even if you removed the highlighted portion since the VHOST_USER_PROTOCOL_F_RESET_DEVICE is not implemented - the implemented fn reset() only works as a shutdown but not as an actual reset.
Reset is a general problem in other libkrun devices too and is half-borked everywhere, there are also some unfinished things in our mmio transport layer implementation, so we should probably address that more generally first before implementing the vhost reset here. 1 2 3
No change needed for now, I just wanted to point out the limitation which is not obvious.
Footnotes
-
https://github.com/containers/libkrun/pull/631#issuecomment-4237384774 ↩
-
https://github.com/mtjhrc/libkrun/blob/e12b9b3780ffa8df9f3e1797b217d13453479167/src/devices/src/virtio/mmio.rs#L295-L296 ↩
-
https://github.com/mtjhrc/libkrun/blob/e12b9b3780ffa8df9f3e1797b217d13453479167/src/devices/src/virtio/mmio.rs#L352-L353 ↩
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. Bridge vmm-sys-util version mismatch: vhost 0.15 requires vmm-sys-util 0.15 (for vm-memory 0.17 compatibility), while libkrun core uses 0.14 and TDX uses 0.12. Convert EventFd types via AsRawFd/FromRawFd when calling vhost functions to avoid type conflicts. This allows all 3 versions coexist in the dependency tree. 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>
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>
Add public API constants for vhost-user vsock devices and example usage in chroot_vm. The underlying support already exists via the generic VhostUserDevice wrapper. Example integration: - Added --vhost-user-vsock option to chroot_vm - Calls krun_disable_implicit_vsock() to avoid conflict with built-in device - Skips TSI port mapping when vhost-user-vsock is active Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
Add public API constants for vhost-user CAN devices and example usage in chroot_vm. The underlying support already exists via the generic VhostUserDevice wrapper. Example integration: - Added --vhost-user-can option to chroot_vm - Uses 3 queues (TX, RX, control) with 64-entry queue sizes - Follows same pattern as other vhost-user device integrations Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
Add public API constants for vhost-user console devices and example
usage in chroot_vm. The underlying support already exists via the
generic VhostUserDevice wrapper.
Console devices require 4 queues for multiport support (receiveq,
transmitq, control receiveq, control transmitq).
Example integration:
- Added --vhost-user-console option to chroot_vm
- Available as /dev/hvc1 in the guest (implicit console uses hvc0)
- Can be used with vhost-device-console for remote console access
Example workflow:
Terminal 1: Start vhost-device-console backend
vhost-device-console --socket-path=/tmp/console.sock \
--socket-count=1 --tcp-port=12346 --backend=network
Terminal 2: Connect to console (before starting VM)
nc localhost 12346
Terminal 3: Start VM with vhost-user console
chroot_vm --vhost-user-console=/tmp/console.sock0 / /bin/sh
In guest: Test with echo "hello" > /dev/hvc1
Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
Implement write_config in VhostUserDevice to support virtio devices that use dynamic configuration space, such as virtio-input. Some virtio devices like virtio-input use a request-response pattern for device discovery. Without write_config support, the backend never receives the virtio_input config values, causing device initialization to fail. This implementation uses VHOST_USER_SET_CONFIG to forward writes to the backend. Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
Add public API constants for vhost-user input devices and example
usage in chroot_vm. The underlying support already exists via the
generic VhostUserDevice wrapper.
Console devices require 2 queues (event, status).
Example integration:
- Added --vhost-user-console option to chroot_vm
Example usage:
On host, start vhost-device-input backend
vhost-device-input --socket-path /tmp/input.sock \
--event-list /dev/input/event3
In libkrun
./chroot_vm --vhost-user-input=/tmp/input.sock0 / /bin/sh
The guest will see the input device as /dev/input/event0 and
can read input events from the host device.
Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
Add public API constants for vhost-user rtc devices and example usage in chroot_vm. The underlying support already exists via the generic VhostUserDevice wrapper. RTC devices require 2 queues (requestq, alarmq). Example integration: - Added --vhost-user-rtc option to chroot_vm Example usage: On host, start vhost-device-rtc backend vhost-device-rtc --socket-path /tmp/rtc.sock Run libkrun with RTC device ./chroot_vm --vhost-user-rtc=/tmp/rtc.sock / /bin/sh The RTC device provides: - Accurate time synchronization without NTP - Multiple clock types (UTC, TAI, monotonic) - PTP clocks for precision time protocol - Alarm support for wake-from-suspend Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
slp
left a comment
There was a problem hiding this comment.
vhost-user support let's gooooo!
thanks @dorindabassey
|
Do you imagine extending this to vhost-user-fs devices? |
Implement vhost-user protocol support in libkrun, enabling out-of-process virtio device backends using rust-vmm/vhost-device.
Key Changes
VhostUserDevicewrapper, file-backed memory (memfd), config space supportkrun_set_vhost_user_device()with public constants for each device typechroot_vmfor all supported devicesAll devices tested and working with corresponding vhost-device backends.