Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/vmm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ gdb = ["arrayvec", "gdbstub", "gdbstub_arch"]

acpi_tables = { path = "../acpi-tables" }
aes-gcm = { version = "0.10.1", default-features = false, features = ["aes"] }
anyhow = "1.0.100"
arrayvec = { version = "0.7.6", optional = true }
aws-lc-rs = { version = "1.14.0", features = ["bindgen"] }
base64 = "0.22.1"
Expand Down
32 changes: 0 additions & 32 deletions src/vmm/src/device_manager/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use crate::arch::{RTC_MEM_START, SERIAL_MEM_START};
#[cfg(target_arch = "aarch64")]
use crate::devices::legacy::{RTCDevice, SerialDevice};
use crate::devices::pseudo::BootTimer;
use crate::devices::virtio::device::VirtioDevice;
use crate::devices::virtio::transport::mmio::MmioTransport;
use crate::vstate::bus::{Bus, BusError};
#[cfg(target_arch = "x86_64")]
Expand All @@ -41,12 +40,6 @@ pub enum MmioError {
BusInsert(#[from] BusError),
/// Failed to allocate requested resourc: {0}
Cmdline(#[from] linux_loader::cmdline::Error),
/// Failed to find the device on the bus.
DeviceNotFound,
/// Invalid device type found on the MMIO bus.
InvalidDeviceType,
/// {0}
InternalDeviceError(String),
/// Could not create IRQ for MMIO device: {0}
CreateIrq(#[from] std::io::Error),
/// Invalid MMIO IRQ configuration.
Expand Down Expand Up @@ -407,31 +400,6 @@ impl MMIODeviceManager {
Ok(())
}

/// Run fn `f()` for the virtio device matching `virtio_type` and `id`.
pub fn with_virtio_device_with_id<T, F>(
&self,
virtio_type: u32,
id: &str,
f: F,
) -> Result<(), MmioError>
where
T: VirtioDevice + 'static + Debug,
F: FnOnce(&mut T) -> Result<(), String>,
{
if let Some(device) = self.get_virtio_device(virtio_type, id) {
let virtio_device = device.inner.lock().expect("Poisoned lock").device();
let mut dev = virtio_device.lock().expect("Poisoned lock");
f(dev
.as_mut_any()
.downcast_mut::<T>()
.ok_or(MmioError::InvalidDeviceType)?)
.map_err(MmioError::InternalDeviceError)?;
} else {
return Err(MmioError::DeviceNotFound);
}
Ok(())
}

#[cfg(target_arch = "aarch64")]
pub fn virtio_device_info(&self) -> Vec<&MMIODeviceInfo> {
let mut device_info = Vec::new();
Expand Down
29 changes: 5 additions & 24 deletions src/vmm/src/device_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,8 @@ pub enum AttachDeviceError {
#[derive(Debug, thiserror::Error, displaydoc::Display)]
/// Error while searching for a VirtIO device
pub enum FindDeviceError {
/// Device type is invalid
InvalidDeviceType,
/// Device not found
DeviceNotFound,
/// Internal Device error: {0}
InternalDeviceError(anyhow::Error),
}

#[derive(Debug)]
Expand Down Expand Up @@ -372,36 +368,21 @@ impl DeviceManager {
}

/// Run fn `f()` for the virtio device matching `virtio_type` and `id`.
pub fn try_with_virtio_device_with_id<T, F, R, E>(
&self,
id: &str,
f: F,
) -> Result<R, FindDeviceError>
pub fn with_virtio_device<T, F, R>(&self, id: &str, f: F) -> Result<R, FindDeviceError>
where
T: VirtioDevice + 'static + Debug,
E: std::error::Error + 'static + Send + Sync,
F: FnOnce(&mut T) -> Result<R, E>,
F: FnOnce(&mut T) -> R,
{
if let Some(device) = self.get_virtio_device(T::const_device_type(), id) {
let mut dev = device.lock().expect("Poisoned lock");
f(dev
Ok(f(dev
.as_mut_any()
.downcast_mut::<T>()
.ok_or(FindDeviceError::InvalidDeviceType)?)
.map_err(|e| FindDeviceError::InternalDeviceError(e.into()))
.expect("Invalid device for a given device type")))
} else {
Err(FindDeviceError::DeviceNotFound)
}
}

/// Run fn `f()` for the virtio device matching `virtio_type` and `id`.
pub fn with_virtio_device_with_id<T, F, R>(&self, id: &str, f: F) -> Result<R, FindDeviceError>
where
T: VirtioDevice + 'static + Debug,
F: FnOnce(&mut T) -> R,
{
self.try_with_virtio_device_with_id(id, |dev: &mut T| Ok::<R, FindDeviceError>(f(dev)))
}
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -471,7 +452,7 @@ impl<'a> Persist<'a> for DeviceManager {
fn restore(
constructor_args: Self::ConstructorArgs,
state: &Self::State,
) -> Result<Self, Self::Error> {
) -> std::result::Result<Self, Self::Error> {
// Setup legacy devices in case of x86
#[cfg(target_arch = "x86_64")]
let legacy_devices = Self::create_legacy_devices(
Expand Down
46 changes: 30 additions & 16 deletions src/vmm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ use vstate::kvm::Kvm;
use vstate::vcpu::{self, StartThreadedError, VcpuSendEventError};

use crate::cpu_config::templates::CpuConfiguration;
use crate::devices::virtio::balloon::{BALLOON_DEV_ID, Balloon, BalloonConfig, BalloonStats};
use crate::devices::virtio::balloon::{
BALLOON_DEV_ID, Balloon, BalloonConfig, BalloonError, BalloonStats,
};
use crate::devices::virtio::block::BlockError;
use crate::devices::virtio::block::device::Block;
use crate::devices::virtio::net::Net;
use crate::logger::{METRICS, MetricsError, error, info, warn};
Expand Down Expand Up @@ -248,6 +251,10 @@ pub enum VmmError {
VMGenID(#[from] VmGenIdError),
/// Failed perform action on device: {0}
FindDeviceError(#[from] device_manager::FindDeviceError),
/// Block: {0}
Block(#[from] BlockError),
/// Balloon: {0}
Balloon(#[from] BalloonError),
}

/// Shorthand type for KVM dirty page bitmap.
Expand Down Expand Up @@ -519,10 +526,11 @@ impl Vmm {
path_on_host: String,
) -> Result<(), VmmError> {
self.device_manager
.try_with_virtio_device_with_id(drive_id, |block: &mut Block| {
.with_virtio_device(drive_id, |block: &mut Block| {
block.update_disk_image(path_on_host)
})
.map_err(VmmError::FindDeviceError)
.map_err(VmmError::FindDeviceError)??;
Ok(())
}

/// Updates the rate limiter parameters for block device with `drive_id` id.
Expand All @@ -533,17 +541,19 @@ impl Vmm {
rl_ops: BucketUpdate,
) -> Result<(), VmmError> {
self.device_manager
.try_with_virtio_device_with_id(drive_id, |block: &mut Block| {
.with_virtio_device(drive_id, |block: &mut Block| {
block.update_rate_limiter(rl_bytes, rl_ops)
})
.map_err(VmmError::FindDeviceError)
.map_err(VmmError::FindDeviceError)??;
Ok(())
}

/// Updates the rate limiter parameters for block device with `drive_id` id.
pub fn update_vhost_user_block_config(&mut self, drive_id: &str) -> Result<(), VmmError> {
self.device_manager
.try_with_virtio_device_with_id(drive_id, |block: &mut Block| block.update_config())
.map_err(VmmError::FindDeviceError)
.with_virtio_device(drive_id, |block: &mut Block| block.update_config())
.map_err(VmmError::FindDeviceError)??;
Ok(())
}

/// Updates the rate limiter parameters for net device with `net_id` id.
Expand All @@ -556,7 +566,7 @@ impl Vmm {
tx_ops: BucketUpdate,
) -> Result<(), VmmError> {
self.device_manager
.with_virtio_device_with_id(net_id, |net: &mut Net| {
.with_virtio_device(net_id, |net: &mut Net| {
net.patch_rate_limiters(rx_bytes, rx_ops, tx_bytes, tx_ops)
})
.map_err(VmmError::FindDeviceError)
Expand All @@ -565,24 +575,27 @@ impl Vmm {
/// Returns a reference to the balloon device if present.
pub fn balloon_config(&self) -> Result<BalloonConfig, VmmError> {
self.device_manager
.with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| dev.config())
.with_virtio_device(BALLOON_DEV_ID, |dev: &mut Balloon| dev.config())
.map_err(VmmError::FindDeviceError)
}

/// Returns the latest balloon statistics if they are enabled.
pub fn latest_balloon_stats(&self) -> Result<BalloonStats, VmmError> {
self.device_manager
.try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| dev.latest_stats())
.map_err(VmmError::FindDeviceError)
let stats = self
.device_manager
.with_virtio_device(BALLOON_DEV_ID, |dev: &mut Balloon| dev.latest_stats())
.map_err(VmmError::FindDeviceError)??;
Ok(stats)
}

/// Updates configuration for the balloon device target size.
pub fn update_balloon_config(&mut self, amount_mib: u32) -> Result<(), VmmError> {
self.device_manager
.try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| {
.with_virtio_device(BALLOON_DEV_ID, |dev: &mut Balloon| {
dev.update_size(amount_mib)
})
.map_err(VmmError::FindDeviceError)
.map_err(VmmError::FindDeviceError)??;
Ok(())
}

/// Updates configuration for the balloon device as described in `balloon_stats_update`.
Expand All @@ -591,10 +604,11 @@ impl Vmm {
stats_polling_interval_s: u16,
) -> Result<(), VmmError> {
self.device_manager
.try_with_virtio_device_with_id(BALLOON_DEV_ID, |dev: &mut Balloon| {
.with_virtio_device(BALLOON_DEV_ID, |dev: &mut Balloon| {
dev.update_stats_polling_interval(stats_polling_interval_s)
})
.map_err(VmmError::FindDeviceError)
.map_err(VmmError::FindDeviceError)??;
Ok(())
}

/// Signals Vmm to stop and exit.
Expand Down
Loading