Skip to content

Commit ca3349b

Browse files
author
Dorinda Bassey
committed
Add vhost-user infrastructure and API
Add vhost-user feature flag, vhost dependency, and krun_set_vhost_user_rng() API to configure vhost-user RNG socket path. Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
1 parent b8fda57 commit ca3349b

8 files changed

Lines changed: 76 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ endif
7575
ifeq ($(INPUT),1)
7676
FEATURE_FLAGS += --features input
7777
endif
78+
ifeq ($(VHOST_USER),1)
79+
FEATURE_FLAGS += --features vhost-user
80+
endif
7881
ifeq ($(AWS_NITRO),1)
7982
VARIANT = -awsnitro
8083
FEATURE_FLAGS := --features aws-nitro,net

include/libkrun.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,28 @@ int krun_add_input_device_fd(uint32_t ctx_id, int input_fd);
717717
*/
718718
int32_t krun_set_snd_device(uint32_t ctx_id, bool enable);
719719

720+
/**
721+
* Configure vhost-user RNG device.
722+
*
723+
* This function sets the socket path for the vhost-user RNG backend.
724+
* When configured, libkrun will connect to a vhost-user RNG backend
725+
* (such as vhost-device-rng) instead of using the in-process RNG device.
726+
*
727+
* The vhost-user backend must be running and listening on the specified
728+
* socket before starting the VM.
729+
*
730+
* Arguments:
731+
* "ctx_id" - the configuration context ID.
732+
* "socket_path" - path to the vhost-user Unix domain socket (e.g., "/tmp/vhost-rng.sock").
733+
*
734+
* Returns:
735+
* Zero on success or a negative error number on failure.
736+
* -EINVAL - Invalid socket_path
737+
* -ENOENT - Context doesn't exist
738+
* -ENOTSUP - vhost-user support not compiled in
739+
*/
740+
int32_t krun_set_vhost_user_rng(uint32_t ctx_id, const char *socket_path);
741+
720742
/**
721743
* Configures a map of rlimits to be set in the guest before starting the isolated binary.
722744
*

src/devices/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ input = ["zerocopy", "krun_input"]
1717
virgl_resource_map2 = []
1818
aws-nitro = []
1919
test_utils = []
20+
vhost-user = ["vhost"]
2021

2122
[dependencies]
2223
bitflags = "1.2.0"
@@ -28,6 +29,7 @@ nix = { version = "0.30.1", features = ["ioctl", "net", "poll", "socket", "fs"]
2829
pw = { package = "pipewire", version = "0.8.0", optional = true }
2930
rand = "0.9.2"
3031
thiserror = { version = "2.0", optional = true }
32+
vhost = { version = "0.14", optional = true, features = ["vhost-user-frontend"] }
3133
virtio-bindings = "0.2.0"
3234
vm-memory = { version = ">=0.13", features = ["backend-mmap"] }
3335
zerocopy = { version = "0.8.26", optional = true, features = ["derive"] }

src/libkrun/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ snd = []
1717
input = ["krun_input", "vmm/input", "devices/input"]
1818
virgl_resource_map2 = []
1919
aws-nitro = [ "dep:aws-nitro", "dep:nitro-enclaves" ]
20+
vhost-user = ["vmm/vhost-user", "devices/vhost-user"]
2021

2122
[dependencies]
2223
crossbeam-channel = ">=0.5.15"

src/libkrun/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,32 @@ pub unsafe extern "C" fn krun_set_snd_device(ctx_id: u32, enable: bool) -> i32 {
17731773
KRUN_SUCCESS
17741774
}
17751775

1776+
#[allow(clippy::missing_safety_doc)]
1777+
#[no_mangle]
1778+
#[cfg(feature = "vhost-user")]
1779+
pub unsafe extern "C" fn krun_set_vhost_user_rng(ctx_id: u32, socket_path: *const c_char) -> i32 {
1780+
let socket_path_str = match CStr::from_ptr(socket_path).to_str() {
1781+
Ok(s) => s,
1782+
Err(_) => return -libc::EINVAL,
1783+
};
1784+
1785+
match CTX_MAP.lock().unwrap().entry(ctx_id) {
1786+
Entry::Occupied(mut ctx_cfg) => {
1787+
let cfg = ctx_cfg.get_mut();
1788+
cfg.vmr.vhost_user_rng_socket = Some(socket_path_str.to_string());
1789+
KRUN_SUCCESS
1790+
}
1791+
Entry::Vacant(_) => -libc::ENOENT,
1792+
}
1793+
}
1794+
1795+
#[allow(clippy::missing_safety_doc)]
1796+
#[no_mangle]
1797+
#[cfg(not(feature = "vhost-user"))]
1798+
pub unsafe extern "C" fn krun_set_vhost_user_rng(_ctx_id: u32, _socket_path: *const c_char) -> i32 {
1799+
-libc::ENOTSUP
1800+
}
1801+
17761802
#[allow(unused_assignments)]
17771803
#[no_mangle]
17781804
pub extern "C" fn krun_get_shutdown_eventfd(ctx_id: u32) -> i32 {

src/vmm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ gpu = ["krun_display"]
1515
snd = []
1616
input = ["krun_input"]
1717
aws-nitro = []
18+
vhost-user = ["devices/vhost-user"]
1819

1920
[dependencies]
2021
crossbeam-channel = ">=0.5.15"

src/vmm/src/resources.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ pub struct VmResources {
173173
#[cfg(feature = "snd")]
174174
/// Enable the virtio-snd device.
175175
pub snd_device: bool,
176+
#[cfg(feature = "vhost-user")]
177+
/// Vhost-user RNG device socket path
178+
pub vhost_user_rng_socket: Option<String>,
176179
/// File to send console output.
177180
pub console_output: Option<PathBuf>,
178181
/// SMBIOS OEM Strings
@@ -423,6 +426,8 @@ mod tests {
423426
input_backends: Vec::new(),
424427
#[cfg(feature = "snd")]
425428
snd_device: false,
429+
#[cfg(feature = "vhost-user")]
430+
vhost_user_rng_socket: None,
426431
console_output: None,
427432
smbios_oem_strings: None,
428433
nested_enabled: false,

0 commit comments

Comments
 (0)