hypervisor: add a function to return hypervisor type

Signed-off-by: Wei Liu <liuwe@microsoft.com>
This commit is contained in:
Wei Liu 2022-07-20 10:41:13 +00:00 committed by Liu Wei
parent d56263706d
commit 9fc3379e8d
4 changed files with 25 additions and 0 deletions

View File

@ -12,6 +12,7 @@ use crate::arch::x86::CpuIdEntry;
#[cfg(feature = "tdx")] #[cfg(feature = "tdx")]
use crate::kvm::TdxCapabilities; use crate::kvm::TdxCapabilities;
use crate::vm::Vm; use crate::vm::Vm;
use crate::HypervisorType;
use std::sync::Arc; use std::sync::Arc;
use thiserror::Error; use thiserror::Error;
@ -82,6 +83,10 @@ pub type Result<T> = std::result::Result<T, HypervisorError>;
/// This crate provides a hypervisor-agnostic interfaces /// This crate provides a hypervisor-agnostic interfaces
/// ///
pub trait Hypervisor: Send + Sync { pub trait Hypervisor: Send + Sync {
///
/// Returns the type of the hypervisor
///
fn hypervisor_type(&self) -> HypervisorType;
/// ///
/// Create a Vm using the underlying hypervisor /// Create a Vm using the underlying hypervisor
/// Return a hypervisor-agnostic Vm trait object /// Return a hypervisor-agnostic Vm trait object

View File

@ -21,6 +21,7 @@ use crate::cpu;
use crate::hypervisor; use crate::hypervisor;
use crate::vec_with_array_field; use crate::vec_with_array_field;
use crate::vm::{self, InterruptSourceConfig, VmOps}; use crate::vm::{self, InterruptSourceConfig, VmOps};
use crate::HypervisorType;
#[cfg(target_arch = "aarch64")] #[cfg(target_arch = "aarch64")]
use crate::{arm64_core_reg_id, offset__of}; use crate::{arm64_core_reg_id, offset__of};
use kvm_ioctls::{NoDatamatch, VcpuFd, VmFd}; use kvm_ioctls::{NoDatamatch, VcpuFd, VmFd};
@ -924,6 +925,12 @@ impl KvmHypervisor {
/// let vm = hypervisor.create_vm().expect("new VM fd creation failed"); /// let vm = hypervisor.create_vm().expect("new VM fd creation failed");
/// ///
impl hypervisor::Hypervisor for KvmHypervisor { impl hypervisor::Hypervisor for KvmHypervisor {
///
/// Returns the type of the hypervisor
///
fn hypervisor_type(&self) -> HypervisorType {
HypervisorType::Kvm
}
/// Create a KVM vm object of a specific VM type and return the object as Vm trait object /// Create a KVM vm object of a specific VM type and return the object as Vm trait object
/// Example /// Example
/// # extern crate hypervisor; /// # extern crate hypervisor;

View File

@ -61,6 +61,12 @@ pub use vm::{
Vm, VmOps, Vm, VmOps,
}; };
#[derive(Debug, Copy, Clone)]
pub enum HypervisorType {
Kvm,
Mshv,
}
pub fn new() -> std::result::Result<Arc<dyn Hypervisor>, HypervisorError> { pub fn new() -> std::result::Result<Arc<dyn Hypervisor>, HypervisorError> {
#[cfg(feature = "kvm")] #[cfg(feature = "kvm")]
let hv = kvm::KvmHypervisor::new()?; let hv = kvm::KvmHypervisor::new()?;

View File

@ -12,6 +12,7 @@ use crate::cpu::Vcpu;
use crate::hypervisor; use crate::hypervisor;
use crate::vec_with_array_field; use crate::vec_with_array_field;
use crate::vm::{self, InterruptSourceConfig, VmOps}; use crate::vm::{self, InterruptSourceConfig, VmOps};
use crate::HypervisorType;
pub use mshv_bindings::*; pub use mshv_bindings::*;
use mshv_ioctls::{set_registers_64, Mshv, NoDatamatch, VcpuFd, VmFd}; use mshv_ioctls::{set_registers_64, Mshv, NoDatamatch, VcpuFd, VmFd};
use std::any::Any; use std::any::Any;
@ -191,6 +192,12 @@ impl MshvHypervisor {
/// let vm = hypervisor.create_vm().expect("new VM fd creation failed"); /// let vm = hypervisor.create_vm().expect("new VM fd creation failed");
/// ///
impl hypervisor::Hypervisor for MshvHypervisor { impl hypervisor::Hypervisor for MshvHypervisor {
///
/// Returns the type of the hypervisor
///
fn hypervisor_type(&self) -> HypervisorType {
HypervisorType::Mshv
}
/// Create a mshv vm object and return the object as Vm trait object /// Create a mshv vm object and return the object as Vm trait object
/// Example /// Example
/// # extern crate hypervisor; /// # extern crate hypervisor;