Skip to content

Commit de20b39

Browse files
author
Dorinda Bassey
committed
Add VhostUserRng device implementation
Implements a vhost-user RNG device. The VMM now switches between the standard RNG device and vhost-user RNG depending on whether a socket path is configured via krun_add_vhost_user_device() This allows us to use the RNG device from the rust-vmm vhost-device running in a separate process for better isolation and flexibility. Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
1 parent 021f6cf commit de20b39

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

src/vmm/src/builder.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,28 @@ pub fn build_microvm(
976976
#[cfg(not(feature = "tee"))]
977977
attach_balloon_device(&mut vmm, event_manager, intc.clone())?;
978978
#[cfg(not(feature = "tee"))]
979-
attach_rng_device(&mut vmm, event_manager, intc.clone())?;
979+
{
980+
#[cfg(feature = "vhost-user")]
981+
{
982+
const VIRTIO_ID_RNG: u32 = 4;
983+
984+
let has_vhost_user_rng = vm_resources
985+
.vhost_user_devices
986+
.iter()
987+
.any(|dev| dev.device_type == VIRTIO_ID_RNG);
988+
989+
if has_vhost_user_rng {
990+
for device_config in &vm_resources.vhost_user_devices {
991+
attach_vhost_user_device(&mut vmm, intc.clone(), device_config)?;
992+
}
993+
} else {
994+
attach_rng_device(&mut vmm, event_manager, intc.clone())?;
995+
}
996+
}
997+
998+
#[cfg(not(feature = "vhost-user"))]
999+
attach_rng_device(&mut vmm, event_manager, intc.clone())?;
1000+
}
9801001
let mut console_id = 0;
9811002
if !vm_resources.disable_implicit_console {
9821003
attach_console_devices(
@@ -2388,6 +2409,36 @@ fn attach_rng_device(
23882409
Ok(())
23892410
}
23902411

2412+
#[cfg(not(feature = "tee"))]
2413+
#[cfg(feature = "vhost-user")]
2414+
fn attach_vhost_user_device(
2415+
vmm: &mut Vmm,
2416+
intc: IrqChip,
2417+
device_config: &VhostUserDeviceConfig,
2418+
) -> std::result::Result<(), StartMicrovmError> {
2419+
use self::StartMicrovmError::*;
2420+
2421+
let device_name = device_config
2422+
.name
2423+
.clone()
2424+
.unwrap_or_else(|| format!("vhost-user-{}", device_config.device_type));
2425+
2426+
let device = Arc::new(Mutex::new(
2427+
devices::virtio::VhostUserDevice::new(
2428+
&device_config.socket_path,
2429+
device_config.device_type,
2430+
device_name.clone(),
2431+
device_config.num_queues,
2432+
&device_config.queue_sizes,
2433+
)
2434+
.map_err(|e| RegisterRngDevice(device_manager::mmio::Error::VhostUserDevice(e)))?,
2435+
));
2436+
2437+
attach_mmio_device(vmm, device_name, intc.clone(), device).map_err(RegisterRngDevice)?;
2438+
2439+
Ok(())
2440+
}
2441+
23912442
#[cfg(feature = "gpu")]
23922443
#[allow(clippy::too_many_arguments)]
23932444
fn attach_gpu_device(

src/vmm/src/device_manager/kvm/mmio.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ pub enum Error {
4141
DeviceNotFound,
4242
/// Failed to update the mmio device.
4343
UpdateFailed,
44+
/// Failed to create vhost-user device.
45+
#[cfg(feature = "vhost-user")]
46+
VhostUserDevice(io::Error),
4447
}
4548

4649
impl fmt::Display for Error {
@@ -59,6 +62,8 @@ impl fmt::Display for Error {
5962
Error::RegisterIrqFd(ref e) => write!(f, "failed to register irqfd: {e}"),
6063
Error::DeviceNotFound => write!(f, "the device couldn't be found"),
6164
Error::UpdateFailed => write!(f, "failed to update the mmio device"),
65+
#[cfg(feature = "vhost-user")]
66+
Error::VhostUserDevice(ref e) => write!(f, "failed to create vhost-user device: {e}"),
6267
}
6368
}
6469
}

0 commit comments

Comments
 (0)