libkrun: vhost-user-vsock support#613
Conversation
9212731 to
6ebaf32
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces support for vhost-user devices in libkrun, enabling the use of external backends for RNG, sound, and vsock devices. Key changes include the implementation of a generic VhostUserDevice wrapper, updates to the VM builder to support file-backed memory via memfd_create (necessary for vhost-user shared memory), and the addition of the krun_add_vhost_user_device C API. Feedback identifies a high-severity issue where the interrupt_monitor thread is not terminated during a device reset, potentially leading to resource leaks. Additionally, it is suggested to refactor repetitive error handling in the example code into a helper function to improve maintainability.
| // Configure vhost-user RNG if requested | ||
| if (cmdline.vhost_user_rng_socket != NULL) { | ||
| // Test sentinel-terminated array: auto-detect queue count, use custom size | ||
| uint16_t custom_sizes[] = {512, 0}; // 0 = sentinel terminator | ||
|
|
||
| if (err = krun_add_vhost_user_device(ctx_id, KRUN_VIRTIO_DEVICE_RNG, | ||
| cmdline.vhost_user_rng_socket, NULL, 0, custom_sizes)) { | ||
| errno = -err; | ||
| perror("Error adding vhost-user RNG device"); | ||
| return -1; | ||
| } | ||
| printf("Using vhost-user RNG backend at %s (custom queue size: 512)\n", cmdline.vhost_user_rng_socket); | ||
| } | ||
|
|
||
| // Configure vhost-user sound if requested | ||
| if (cmdline.vhost_user_snd_socket != NULL) { | ||
| if (err = krun_add_vhost_user_device(ctx_id, KRUN_VIRTIO_DEVICE_SND, | ||
| cmdline.vhost_user_snd_socket, NULL, | ||
| KRUN_VHOST_USER_SND_NUM_QUEUES, | ||
| KRUN_VHOST_USER_SND_QUEUE_SIZES)) { | ||
| errno = -err; | ||
| perror("Error adding vhost-user sound device"); | ||
| return -1; | ||
| } | ||
| printf("Using vhost-user sound backend at %s\n", cmdline.vhost_user_snd_socket); | ||
| } | ||
|
|
||
| // Configure vhost-user vsock if requested | ||
| if (cmdline.vhost_user_vsock_socket != NULL) { | ||
| // Disable the implicit vsock device to avoid conflict | ||
| if (err = krun_disable_implicit_vsock(ctx_id)) { | ||
| errno = -err; | ||
| perror("Error disabling implicit vsock"); | ||
| return -1; | ||
| } | ||
|
|
||
| if (err = krun_add_vhost_user_device(ctx_id, KRUN_VIRTIO_DEVICE_VSOCK, | ||
| cmdline.vhost_user_vsock_socket, NULL, | ||
| KRUN_VHOST_USER_VSOCK_NUM_QUEUES, | ||
| KRUN_VHOST_USER_VSOCK_QUEUE_SIZES)) { | ||
| errno = -err; | ||
| perror("Error adding vhost-user vsock device"); | ||
| return -1; | ||
| } | ||
| printf("Using vhost-user vsock backend at %s\n", cmdline.vhost_user_vsock_socket); | ||
| } |
There was a problem hiding this comment.
The error handling logic for krun_add_vhost_user_device and krun_disable_implicit_vsock is duplicated across the setup for RNG, sound, and vsock devices. To improve maintainability and reduce code duplication, consider extracting this logic into a helper function. This aligns with the repository's priority on code readability.
Example helper:
static bool check_krun_error(int err, const char *msg)
{
if (err) {
errno = -err;
perror(msg);
return false;
}
return true;
}References
- Prioritize code readability and focused commits over micro-optimizations.
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>
6ebaf32 to
4056c01
Compare
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>
4056c01 to
96a5695
Compare
libkrun: Add API constants and example for vhost-user vsock
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:
built-in device