mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2025-01-05 04:15:20 +00:00
hypervisor: add create_passthrough_device call to Vm trait
That function is going to return a handle for passthrough related operations. Move create_kvm_device code there. Signed-off-by: Wei Liu <liuwe@microsoft.com>
This commit is contained in:
parent
7bfe87b7af
commit
ff8d7bfe83
@ -255,6 +255,17 @@ impl vm::Vm for KvmVm {
|
|||||||
fn check_extension(&self, c: Cap) -> bool {
|
fn check_extension(&self, c: Cap) -> bool {
|
||||||
self.fd.check_extension(c)
|
self.fd.check_extension(c)
|
||||||
}
|
}
|
||||||
|
/// Create a device that is used for passthrough
|
||||||
|
fn create_passthrough_device(&self) -> vm::Result<DeviceFd> {
|
||||||
|
let mut vfio_dev = kvm_create_device {
|
||||||
|
type_: kvm_device_type_KVM_DEV_TYPE_VFIO,
|
||||||
|
fd: 0,
|
||||||
|
flags: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
self.create_device(&mut vfio_dev)
|
||||||
|
.map_err(|e| vm::HypervisorVmError::CreatePassthroughDevice(e.into()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/// Wrapper over KVM system ioctls.
|
/// Wrapper over KVM system ioctls.
|
||||||
pub struct KvmHypervisor {
|
pub struct KvmHypervisor {
|
||||||
|
@ -110,6 +110,11 @@ pub enum HypervisorVmError {
|
|||||||
///
|
///
|
||||||
#[error("Failed to set clock: {0}")]
|
#[error("Failed to set clock: {0}")]
|
||||||
SetClock(#[source] anyhow::Error),
|
SetClock(#[source] anyhow::Error),
|
||||||
|
///
|
||||||
|
/// Create passthrough device
|
||||||
|
///
|
||||||
|
#[error("Failed to create passthrough device: {0}")]
|
||||||
|
CreatePassthroughDevice(#[source] anyhow::Error),
|
||||||
}
|
}
|
||||||
///
|
///
|
||||||
/// Result type for returning from a function
|
/// Result type for returning from a function
|
||||||
@ -171,4 +176,6 @@ pub trait Vm: Send + Sync {
|
|||||||
fn set_clock(&self, data: &ClockData) -> Result<()>;
|
fn set_clock(&self, data: &ClockData) -> Result<()>;
|
||||||
/// Checks if a particular `Cap` is available.
|
/// Checks if a particular `Cap` is available.
|
||||||
fn check_extension(&self, c: Cap) -> bool;
|
fn check_extension(&self, c: Cap) -> bool;
|
||||||
|
/// Create a device that is used for passthrough
|
||||||
|
fn create_passthrough_device(&self) -> Result<DeviceFd>;
|
||||||
}
|
}
|
||||||
|
@ -2350,18 +2350,6 @@ impl DeviceManager {
|
|||||||
Ok(devices)
|
Ok(devices)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "pci_support")]
|
|
||||||
fn create_kvm_device(vm: &Arc<dyn hypervisor::Vm>) -> DeviceManagerResult<DeviceFd> {
|
|
||||||
let mut vfio_dev = hypervisor::kvm::kvm_create_device {
|
|
||||||
type_: hypervisor::kvm::kvm_device_type_KVM_DEV_TYPE_VFIO,
|
|
||||||
fd: 0,
|
|
||||||
flags: 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
vm.create_device(&mut vfio_dev)
|
|
||||||
.map_err(|e| DeviceManagerError::CreateKvmDevice(e.into()))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "pci_support"))]
|
#[cfg(not(feature = "pci_support"))]
|
||||||
fn next_device_name(&mut self, prefix: &str) -> DeviceManagerResult<String> {
|
fn next_device_name(&mut self, prefix: &str) -> DeviceManagerResult<String> {
|
||||||
// Generate the temporary name.
|
// Generate the temporary name.
|
||||||
@ -2505,7 +2493,11 @@ impl DeviceManager {
|
|||||||
|
|
||||||
if let Some(device_list_cfg) = &mut devices {
|
if let Some(device_list_cfg) = &mut devices {
|
||||||
// Create the KVM VFIO device
|
// Create the KVM VFIO device
|
||||||
let device_fd = DeviceManager::create_kvm_device(&self.address_manager.vm)?;
|
let device_fd = self
|
||||||
|
.address_manager
|
||||||
|
.vm
|
||||||
|
.create_passthrough_device()
|
||||||
|
.map_err(|e| DeviceManagerError::CreateKvmDevice(e.into()))?;
|
||||||
let device_fd = Arc::new(device_fd);
|
let device_fd = Arc::new(device_fd);
|
||||||
self.kvm_device_fd = Some(Arc::clone(&device_fd));
|
self.kvm_device_fd = Some(Arc::clone(&device_fd));
|
||||||
|
|
||||||
@ -2932,7 +2924,11 @@ impl DeviceManager {
|
|||||||
// If the VFIO KVM device file descriptor has not been created yet,
|
// If the VFIO KVM device file descriptor has not been created yet,
|
||||||
// it is created here and stored in the DeviceManager structure for
|
// it is created here and stored in the DeviceManager structure for
|
||||||
// future needs.
|
// future needs.
|
||||||
let device_fd = DeviceManager::create_kvm_device(&self.address_manager.vm)?;
|
let device_fd = self
|
||||||
|
.address_manager
|
||||||
|
.vm
|
||||||
|
.create_passthrough_device()
|
||||||
|
.map_err(|e| DeviceManagerError::CreateKvmDevice(e.into()))?;
|
||||||
let device_fd = Arc::new(device_fd);
|
let device_fd = Arc::new(device_fd);
|
||||||
self.kvm_device_fd = Some(Arc::clone(&device_fd));
|
self.kvm_device_fd = Some(Arc::clone(&device_fd));
|
||||||
device_fd
|
device_fd
|
||||||
|
Loading…
Reference in New Issue
Block a user