From 9fc3379e8d17a84546d4bec35678b93904d8fcfd Mon Sep 17 00:00:00 2001 From: Wei Liu Date: Wed, 20 Jul 2022 10:41:13 +0000 Subject: [PATCH] hypervisor: add a function to return hypervisor type Signed-off-by: Wei Liu --- hypervisor/src/hypervisor.rs | 5 +++++ hypervisor/src/kvm/mod.rs | 7 +++++++ hypervisor/src/lib.rs | 6 ++++++ hypervisor/src/mshv/mod.rs | 7 +++++++ 4 files changed, 25 insertions(+) diff --git a/hypervisor/src/hypervisor.rs b/hypervisor/src/hypervisor.rs index dc628c932..b51e0e076 100644 --- a/hypervisor/src/hypervisor.rs +++ b/hypervisor/src/hypervisor.rs @@ -12,6 +12,7 @@ use crate::arch::x86::CpuIdEntry; #[cfg(feature = "tdx")] use crate::kvm::TdxCapabilities; use crate::vm::Vm; +use crate::HypervisorType; use std::sync::Arc; use thiserror::Error; @@ -82,6 +83,10 @@ pub type Result = std::result::Result; /// This crate provides a hypervisor-agnostic interfaces /// pub trait Hypervisor: Send + Sync { + /// + /// Returns the type of the hypervisor + /// + fn hypervisor_type(&self) -> HypervisorType; /// /// Create a Vm using the underlying hypervisor /// Return a hypervisor-agnostic Vm trait object diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs index bf80ae318..4fc04a6ab 100644 --- a/hypervisor/src/kvm/mod.rs +++ b/hypervisor/src/kvm/mod.rs @@ -21,6 +21,7 @@ use crate::cpu; use crate::hypervisor; use crate::vec_with_array_field; use crate::vm::{self, InterruptSourceConfig, VmOps}; +use crate::HypervisorType; #[cfg(target_arch = "aarch64")] use crate::{arm64_core_reg_id, offset__of}; use kvm_ioctls::{NoDatamatch, VcpuFd, VmFd}; @@ -924,6 +925,12 @@ impl KvmHypervisor { /// let vm = hypervisor.create_vm().expect("new VM fd creation failed"); /// 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 /// Example /// # extern crate hypervisor; diff --git a/hypervisor/src/lib.rs b/hypervisor/src/lib.rs index 0e2a198ae..6b798d462 100644 --- a/hypervisor/src/lib.rs +++ b/hypervisor/src/lib.rs @@ -61,6 +61,12 @@ pub use vm::{ Vm, VmOps, }; +#[derive(Debug, Copy, Clone)] +pub enum HypervisorType { + Kvm, + Mshv, +} + pub fn new() -> std::result::Result, HypervisorError> { #[cfg(feature = "kvm")] let hv = kvm::KvmHypervisor::new()?; diff --git a/hypervisor/src/mshv/mod.rs b/hypervisor/src/mshv/mod.rs index dc823063c..f99c5c50e 100644 --- a/hypervisor/src/mshv/mod.rs +++ b/hypervisor/src/mshv/mod.rs @@ -12,6 +12,7 @@ use crate::cpu::Vcpu; use crate::hypervisor; use crate::vec_with_array_field; use crate::vm::{self, InterruptSourceConfig, VmOps}; +use crate::HypervisorType; pub use mshv_bindings::*; use mshv_ioctls::{set_registers_64, Mshv, NoDatamatch, VcpuFd, VmFd}; use std::any::Any; @@ -191,6 +192,12 @@ impl MshvHypervisor { /// let vm = hypervisor.create_vm().expect("new VM fd creation failed"); /// 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 /// Example /// # extern crate hypervisor;