diff --git a/hypervisor/src/device.rs b/hypervisor/src/device.rs new file mode 100644 index 000000000..cc62d5fb4 --- /dev/null +++ b/hypervisor/src/device.rs @@ -0,0 +1,39 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause +// +// Copyright © 2020, Microsoft Corporation +// +// Copyright 2018-2019 CrowdStrike, Inc. +// +// Copyright 2020, ARM Limited +// + +use crate::DeviceAttr; +use thiserror::Error; + +#[derive(Error, Debug)] +/// +/// Enum for device error +pub enum HypervisorDeviceError { + /// + /// Set device attribute error + /// + #[error("Failed to set device attribute: {0}")] + SetDeviceAttribute(#[source] anyhow::Error), +} + +/// +/// Result type for returning from a function +/// +pub type Result = std::result::Result; + +/// +/// Trait to represent a device +/// +/// This crate provides a hypervisor-agnostic interfaces for device +/// +pub trait Device: Send + Sync { + /// Set device attribute. + fn set_device_attr(&self, attr: &DeviceAttr) -> Result<()>; +} diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs index 01ed27866..5e932b196 100644 --- a/hypervisor/src/kvm/mod.rs +++ b/hypervisor/src/kvm/mod.rs @@ -18,6 +18,7 @@ use vmm_sys_util::eventfd::EventFd; #[cfg(target_arch = "aarch64")] pub use crate::aarch64::{check_required_kvm_extensions, VcpuInit, VcpuKvmState as CpuState}; use crate::cpu; +use crate::device; use crate::hypervisor; use crate::vm; // x86_64 dependencies @@ -58,6 +59,7 @@ pub use kvm_ioctls::{Cap, Kvm}; /// pub use { kvm_bindings::kvm_clock_data as ClockData, kvm_bindings::kvm_create_device as CreateDevice, + kvm_bindings::kvm_device_attr as DeviceAttr, kvm_bindings::kvm_irq_routing_entry as IrqRoutingEntry, kvm_bindings::kvm_mp_state as MpState, kvm_bindings::kvm_userspace_memory_region as MemoryRegion, kvm_bindings::kvm_vcpu_events as VcpuEvents, kvm_ioctls::DeviceFd, kvm_ioctls::IoEventAddress, @@ -248,10 +250,13 @@ impl vm::Vm for KvmVm { /// Creates an emulated device in the kernel. /// /// See the documentation for `KVM_CREATE_DEVICE`. - fn create_device(&self, device: &mut CreateDevice) -> vm::Result { - self.fd + fn create_device(&self, device: &mut CreateDevice) -> vm::Result> { + let fd = self + .fd .create_device(device) - .map_err(|e| vm::HypervisorVmError::CreateDevice(e.into())) + .map_err(|e| vm::HypervisorVmError::CreateDevice(e.into()))?; + let device = KvmDevice { fd }; + Ok(Arc::new(device)) } /// /// Returns the preferred CPU target type which can be emulated by KVM on underlying host. @@ -305,7 +310,8 @@ impl vm::Vm for KvmVm { flags: 0, }; - self.create_device(&mut vfio_dev) + self.fd + .create_device(&mut vfio_dev) .map_err(|e| vm::HypervisorVmError::CreatePassthroughDevice(e.into())) } } @@ -847,3 +853,19 @@ impl cpu::Vcpu for KvmVcpu { Ok(()) } } + +/// Device struct for KVM +pub struct KvmDevice { + fd: DeviceFd, +} + +impl device::Device for KvmDevice { + /// + /// Set device attribute + /// + fn set_device_attr(&self, attr: &DeviceAttr) -> device::Result<()> { + self.fd + .set_device_attr(attr) + .map_err(|e| device::HypervisorDeviceError::SetDeviceAttribute(e.into())) + } +} diff --git a/hypervisor/src/lib.rs b/hypervisor/src/lib.rs index 45c48f864..b9065f0d6 100644 --- a/hypervisor/src/lib.rs +++ b/hypervisor/src/lib.rs @@ -40,8 +40,12 @@ pub mod arch; /// CPU related module mod cpu; +/// Device related module +mod device; + pub use crate::hypervisor::{Hypervisor, HypervisorError}; pub use cpu::{HypervisorCpuError, Vcpu, VmExit}; +pub use device::{Device, HypervisorDeviceError}; pub use kvm::*; pub use vm::{DataMatch, HypervisorVmError, Vm}; diff --git a/hypervisor/src/vm.rs b/hypervisor/src/vm.rs index 4622a2f5a..a48b2776c 100644 --- a/hypervisor/src/vm.rs +++ b/hypervisor/src/vm.rs @@ -11,6 +11,7 @@ #[cfg(target_arch = "aarch64")] use crate::aarch64::VcpuInit; use crate::cpu::Vcpu; +use crate::device::Device; #[cfg(target_arch = "x86_64")] use crate::ClockData; use crate::{CreateDevice, DeviceFd, IoEventAddress, IrqRoutingEntry, MemoryRegion}; @@ -161,7 +162,7 @@ pub trait Vm: Send + Sync { /// Creates/modifies a guest physical memory slot. fn set_user_memory_region(&self, user_memory_region: MemoryRegion) -> Result<()>; /// Creates an emulated device in the kernel. - fn create_device(&self, device: &mut CreateDevice) -> Result; + fn create_device(&self, device: &mut CreateDevice) -> Result>; /// Returns the preferred CPU target type which can be emulated by KVM on underlying host. #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] fn get_preferred_target(&self, kvi: &mut VcpuInit) -> Result<()>;