From 422bf89d4dadce7b5ebecb464da8085ea7ccf2a9 Mon Sep 17 00:00:00 2001 From: Wei Liu Date: Wed, 20 Jul 2022 15:11:27 +0000 Subject: [PATCH] hypervisor: drop create_device from Vm trait This then avoids the need for creating a generic DeviceFd type in the hypervisor crate. Signed-off-by: Wei Liu --- hypervisor/src/kvm/mod.rs | 25 ++++++++++++++----------- hypervisor/src/mshv/mod.rs | 26 ++++++++++++++------------ hypervisor/src/vm.rs | 3 --- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs index a386ed010..fbc14d598 100644 --- a/hypervisor/src/kvm/mod.rs +++ b/hypervisor/src/kvm/mod.rs @@ -314,6 +314,20 @@ pub struct KvmVm { dirty_log_slots: Arc>>, } +impl KvmVm { + /// + /// Creates an emulated device in the kernel. + /// + /// See the documentation for `KVM_CREATE_DEVICE`. + fn create_device(&self, device: &mut CreateDevice) -> vm::Result> { + let device_fd = self + .fd + .create_device(device) + .map_err(|e| vm::HypervisorVmError::CreateDevice(e.into()))?; + Ok(Arc::new(device_fd)) + } +} + /// /// Implementation of Vm trait for KVM /// Example: @@ -616,17 +630,6 @@ 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> { - let device_fd = self - .fd - .create_device(device) - .map_err(|e| vm::HypervisorVmError::CreateDevice(e.into()))?; - Ok(Arc::new(device_fd)) - } - /// /// Returns the preferred CPU target type which can be emulated by KVM on underlying host. /// #[cfg(target_arch = "aarch64")] diff --git a/hypervisor/src/mshv/mod.rs b/hypervisor/src/mshv/mod.rs index dc65340c8..170ce4045 100644 --- a/hypervisor/src/mshv/mod.rs +++ b/hypervisor/src/mshv/mod.rs @@ -913,6 +913,20 @@ pub struct MshvVm { dirty_log_slots: Arc>>, } +impl MshvVm { + /// + /// Creates an in-kernel device. + /// + /// See the documentation for `MSHV_CREATE_DEVICE`. + fn create_device(&self, device: &mut CreateDevice) -> vm::Result> { + let device_fd = self + .fd + .create_device(device) + .map_err(|e| vm::HypervisorVmError::CreateDevice(e.into()))?; + Ok(Arc::new(device_fd)) + } +} + /// /// Implementation of Vm trait for Mshv /// Example: @@ -1096,18 +1110,6 @@ impl vm::Vm for MshvVm { .into() } - /// - /// Creates an in-kernel device. - /// - /// See the documentation for `MSHV_CREATE_DEVICE`. - fn create_device(&self, device: &mut CreateDevice) -> vm::Result> { - let device_fd = self - .fd - .create_device(device) - .map_err(|e| vm::HypervisorVmError::CreateDevice(e.into()))?; - Ok(Arc::new(device_fd)) - } - fn create_passthrough_device(&self) -> vm::Result> { let mut vfio_dev = mshv_create_device { type_: mshv_device_type_MSHV_DEV_TYPE_VFIO, diff --git a/hypervisor/src/vm.rs b/hypervisor/src/vm.rs index 8e364d638..1873d2bae 100644 --- a/hypervisor/src/vm.rs +++ b/hypervisor/src/vm.rs @@ -18,7 +18,6 @@ use crate::cpu::Vcpu; use crate::device::Device; #[cfg(target_arch = "x86_64")] use crate::ClockData; -use crate::CreateDevice; use crate::UserMemoryRegion; use crate::{IoEventAddress, IrqRoutingEntry}; #[cfg(feature = "kvm")] @@ -314,8 +313,6 @@ pub trait Vm: Send + Sync + Any { fn create_user_memory_region(&self, user_memory_region: UserMemoryRegion) -> Result<()>; /// Removes a guest physical memory slot. fn remove_user_memory_region(&self, user_memory_region: UserMemoryRegion) -> Result<()>; - /// Creates an emulated device in the kernel. - fn create_device(&self, device: &mut CreateDevice) -> Result>; /// Returns the preferred CPU target type which can be emulated by KVM on underlying host. #[cfg(target_arch = "aarch64")] fn get_preferred_target(&self, kvi: &mut VcpuInit) -> Result<()>;