Skip to content

Add vhost-user support with RNG device implementation#527

Open
dorindabassey wants to merge 5 commits intocontainers:mainfrom
dorindabassey:vhost-user-support
Open

Add vhost-user support with RNG device implementation#527
dorindabassey wants to merge 5 commits intocontainers:mainfrom
dorindabassey:vhost-user-support

Conversation

@dorindabassey
Copy link
Copy Markdown
Collaborator

This PR adds vhost-user frontend support to libkrun, enabling virtio devices to run in separate processes using rust-vmm's vhost-device backends for improved isolation and flexibility. The RNG frontend is implemented as the initial use case, and is designed to easily support additional devices (sound, GPU, can, etc).

@dorindabassey dorindabassey marked this pull request as ready for review February 5, 2026 15:59
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.

I had a look at the tests, and honestly I don't see much value in them, we don't have any arbitrary coverage percent metric for merging stuff, so I would just remove most of them.

The rest of the code looks good, but I've only had a quick look so far, when this was still a draft, I'll try running this and have another look later.

Having vhost-user support in libkrun seems pretty cool, thanks!

@tylerfanelli
Copy link
Copy Markdown
Member

tylerfanelli commented Feb 6, 2026

Not really a review, just a comment:

@dorindabassey This is some great work and a huge addition to the project.

@slp passt uses vhost-user to provide near-native network performance in user mode for QEMU. Perhaps we should consider doing the same here, especially as the main network driver for the v2 API.

@sbrivio-rh
Copy link
Copy Markdown

@slp passt uses vhost-user to provide near-native network performance in user mode for QEMU. Perhaps we should consider doing the same here, especially as the main network driver for the v2 API.

Right, that would be great to have! It's mostly about throughput and latency, but also, perhaps, one day, to implement live migration of TCP connections, as it's only available via vhost-user interface.

I'm not sure how much effort networking support would take on top of this pull request, and whether it's beyond its scope or not, but let me share a couple of pointers just in case. The part of API that's implemented by passt is this:

https://passt.top/passt/tree/vhost_user.c?id=af7b81b5408da8c56bb22dd11679f2b4024a45c8#n1128

And there's a bit of documentation about how it can be used with QEMU here:

https://www.qemu.org/docs/master/system/devices/net.html#using-passt-as-the-user-mode-network-stack

Let me know if you have any question! I'll also tag @vivier, the author of the vhost-user implementation in passt, for good measure.

@tylerfanelli
Copy link
Copy Markdown
Member

I'm not sure how much effort networking support would take on top of this pull request, and whether it's beyond its scope or not

It's fine for this PR to stand as-is. This can be used as a base for another series adding the network implementation.

@mtjhrc
Copy link
Copy Markdown
Collaborator

mtjhrc commented Feb 9, 2026

passt uses vhost-user to provide near-native network performance in user mode for QEMU. Perhaps we should consider doing the same here, especially as the main network driver for the v2 API.

I'm not sure how much effort networking support would take on top of this pull request, and whether it's beyond its scope or not

Adding support for virtio-net over vhost doesn't require any changes to any of our networking code. Our virtio-net is not attached by default.

All the user would have to do to enable it is:

  1. Disable the implicit vsock device to get rid of TSI (krun_disable_implicit_vsock). Optionally add a vsock device back (krun_add_vsock) to use vsock for other purpose
  2. Use something like krun_add_vhost_user_device to attach the net device

@dorindabassey dorindabassey force-pushed the vhost-user-support branch 3 times, most recently from 9cfb1ea to fe4e9ba Compare February 10, 2026 11:28
@dorindabassey dorindabassey force-pushed the vhost-user-support branch 2 times, most recently from b3bfb2e to 3feb780 Compare February 12, 2026 11:59
@dorindabassey dorindabassey force-pushed the vhost-user-support branch 2 times, most recently from e5c413f to 5b0b670 Compare February 16, 2026 16:03
@dorindabassey dorindabassey marked this pull request as draft February 17, 2026 09:54
@dorindabassey dorindabassey marked this pull request as ready for review February 18, 2026 11:45
@dorindabassey
Copy link
Copy Markdown
Collaborator Author

Fixed! Thank you for the reviews @mtjhrc!

@dorindabassey dorindabassey force-pushed the vhost-user-support branch 3 times, most recently from 58f0fbf to 4ce3cb5 Compare March 10, 2026 10:52
@dorindabassey dorindabassey requested a review from mtjhrc March 10, 2026 11:31
@dorindabassey
Copy link
Copy Markdown
Collaborator Author

Hi @mtjhrc and @tylerfanelli PTAL

mtjhrc
mtjhrc previously approved these changes Mar 10, 2026
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.

LGTM now, thanks!

tylerfanelli
tylerfanelli previously approved these changes Mar 12, 2026
Copy link
Copy Markdown
Member

@tylerfanelli tylerfanelli left a comment

Choose a reason for hiding this comment

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

LGTM. A few comments that are non-blockers. Great work!

@dorindabassey dorindabassey dismissed stale reviews from tylerfanelli and mtjhrc via e14fd13 March 16, 2026 11:34
@dorindabassey dorindabassey force-pushed the vhost-user-support branch 2 times, most recently from e14fd13 to 8bcb47b Compare March 16, 2026 11:49
tylerfanelli
tylerfanelli previously approved these changes Mar 18, 2026
Copy link
Copy Markdown
Member

@tylerfanelli tylerfanelli left a comment

Choose a reason for hiding this comment

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

LGTM!

@dorindabassey
Copy link
Copy Markdown
Collaborator Author

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for vhost-user devices in libkrun, enabling the integration of external backends for virtio devices such as RNG, sound, and CAN. The implementation includes a new generic VhostUserDevice wrapper, an expanded C API, and modifications to the VMM builder to ensure guest memory is file-backed via memfd when vhost-user is active. Review feedback suggests using non-blocking eventfds for interrupt monitoring to prevent potential deadlocks and recommends using more descriptive names for memory file descriptors to improve debuggability.

Comment on lines +1384 to +1387
let fd = libc::memfd_create(
b"kernel\0".as_ptr() as *const libc::c_char,
libc::MFD_CLOEXEC,
);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The memfd_create call should ideally specify a more descriptive name than just 'kernel' to aid in debugging when inspecting /proc/self/fd or using tools like lsof.

Comment on lines +1635 to +1638
let fd = libc::memfd_create(
b"guest_mem\0".as_ptr() as *const libc::c_char,
libc::MFD_CLOEXEC,
);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Similar to the kernel memfd, the guest memory memfd should have a more descriptive name, perhaps including the context ID or region index, to facilitate debugging.

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>
Dorinda Bassey and others added 3 commits April 10, 2026 10:27
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>
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>
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.

4 participants