hypervisor: drop a level of indirection for KVM's DeviceFd

Signed-off-by: Wei Liu <liuwe@microsoft.com>
This commit is contained in:
Wei Liu 2022-07-09 15:57:43 +00:00 committed by Rob Bradford
parent ddad5f3510
commit 3710932248

View File

@ -32,7 +32,10 @@ use std::collections::HashMap;
use std::convert::TryInto; use std::convert::TryInto;
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
use std::fs::File; use std::fs::File;
use std::os::unix::io::{AsRawFd, RawFd}; #[cfg(target_arch = "x86_64")]
use std::os::unix::io::AsRawFd;
#[cfg(feature = "tdx")]
use std::os::unix::io::RawFd;
use std::result; use std::result;
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
@ -530,12 +533,11 @@ impl vm::Vm for KvmVm {
/// ///
/// See the documentation for `KVM_CREATE_DEVICE`. /// See the documentation for `KVM_CREATE_DEVICE`.
fn create_device(&self, device: &mut CreateDevice) -> vm::Result<Arc<dyn device::Device>> { fn create_device(&self, device: &mut CreateDevice) -> vm::Result<Arc<dyn device::Device>> {
let fd = self let device_fd = self
.fd .fd
.create_device(device) .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_fd))
Ok(Arc::new(device))
} }
/// ///
/// Returns the preferred CPU target type which can be emulated by KVM on underlying host. /// Returns the preferred CPU target type which can be emulated by KVM on underlying host.
@ -1984,25 +1986,21 @@ impl cpu::Vcpu for KvmVcpu {
} }
/// Device struct for KVM /// Device struct for KVM
pub struct KvmDevice { pub type KvmDevice = DeviceFd;
fd: DeviceFd,
}
impl device::Device for KvmDevice { impl device::Device for KvmDevice {
/// ///
/// Set device attribute /// Set device attribute
/// ///
fn set_device_attr(&self, attr: &DeviceAttr) -> device::Result<()> { fn set_device_attr(&self, attr: &DeviceAttr) -> device::Result<()> {
self.fd self.set_device_attr(attr)
.set_device_attr(attr)
.map_err(|e| device::HypervisorDeviceError::SetDeviceAttribute(e.into())) .map_err(|e| device::HypervisorDeviceError::SetDeviceAttribute(e.into()))
} }
/// ///
/// Get device attribute /// Get device attribute
/// ///
fn get_device_attr(&self, attr: &mut DeviceAttr) -> device::Result<()> { fn get_device_attr(&self, attr: &mut DeviceAttr) -> device::Result<()> {
self.fd self.get_device_attr(attr)
.get_device_attr(attr)
.map_err(|e| device::HypervisorDeviceError::GetDeviceAttribute(e.into())) .map_err(|e| device::HypervisorDeviceError::GetDeviceAttribute(e.into()))
} }
/// ///
@ -2012,9 +2010,3 @@ impl device::Device for KvmDevice {
self self
} }
} }
impl AsRawFd for KvmDevice {
fn as_raw_fd(&self) -> RawFd {
self.fd.as_raw_fd()
}
}