From 683210d6e9379ed6d135f6a6afcedcb3aba32847 Mon Sep 17 00:00:00 2001 From: Muminul Islam Date: Wed, 3 Jun 2020 12:59:49 -0700 Subject: [PATCH] hypervisor: Add Hypervisor trait As the only hypervisor that Cloud-Hypervisor supports is KVM, the Hypervisor trait accomodates for the upcoming KVM implementation. This trait will be instanciated at build time through hypervisor specific features, i.e. it's not aiming at run-time selection of hypervisors for Cloud-Hypervisor. Signed-off-by: Muminul Islam Signed-off-by: Samuel Ortiz --- hypervisor/src/hypervisor.rs | 106 +++++++++++++++++++++++++++++++++++ hypervisor/src/lib.rs | 4 ++ 2 files changed, 110 insertions(+) create mode 100644 hypervisor/src/hypervisor.rs diff --git a/hypervisor/src/hypervisor.rs b/hypervisor/src/hypervisor.rs new file mode 100644 index 000000000..539da321d --- /dev/null +++ b/hypervisor/src/hypervisor.rs @@ -0,0 +1,106 @@ +// Copyright © 2019 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright © 2020, Microsoft Corporation +// +// Copyright 2018-2019 CrowdStrike, Inc. +// +// +use crate::vm::Vm; +#[cfg(target_arch = "x86_64")] +use crate::x86_64::CpuId; + +use kvm_ioctls::Cap; +use std::sync::Arc; + +use thiserror::Error; + +#[derive(Error, Debug)] +/// +/// +pub enum HypervisorError { + /// + /// Vm creation failure + /// + #[error("Failed to create Vm: {0}")] + VmCreate(#[source] anyhow::Error), + /// + /// Vm setup failure + /// + #[error("Failed to setup Vm: {0}")] + VmSetup(#[source] anyhow::Error), + /// + /// API version error + /// + #[error("Failed to get API Version: {0}")] + GetApiVersion(#[source] anyhow::Error), + /// + /// Vcpu mmap error + /// + #[error("Failed to get Vcpu Mmap: {0}")] + GetVcpuMmap(#[source] anyhow::Error), + /// + /// Max Vcpu error + /// + #[error("Failed to get number of max vcpus: {0}")] + GetMaxVcpu(#[source] anyhow::Error), + /// + /// Recommended Vcpu error + /// + #[error("Failed to get number of max vcpus: {0}")] + GetNrVcpus(#[source] anyhow::Error), + /// + /// CpuId error + /// + #[error("Failed to get number of max vcpus: {0}")] + GetCpuId(#[source] anyhow::Error), +} + +/// +/// Result type for returning from a function +/// +pub type Result = std::result::Result; + +/// +/// Trait to represent a Hypervisor +/// +/// This crate provides a hypervisor-agnostic interfaces +/// +pub trait Hypervisor: Send + Sync { + /// + /// Create a Vm using the underlying hypervisor + /// Return a hypervisor-agnostic Vm trait object + /// + fn create_vm(&self) -> Result>; + /// + /// Get the API version of the hypervisor + /// + fn get_api_version(&self) -> i32; + /// + /// Returns the size of the memory mapping required to use the vcpu's structures + /// + fn get_vcpu_mmap_size(&self) -> Result; + /// + /// Gets the recommended maximum number of VCPUs per VM. + /// + fn get_max_vcpus(&self) -> Result; + /// + /// Gets the recommended number of VCPUs per VM. + /// + fn get_nr_vcpus(&self) -> Result; + #[cfg(target_arch = "x86_64")] + /// + /// Checks if a particular `Cap` is available. + /// + fn check_capability(&self, c: Cap) -> bool; + #[cfg(target_arch = "x86_64")] + /// + /// Get the supported CpuID + /// + fn get_cpuid(&self) -> Result; + /// + /// Check particular extensions if any + /// + fn check_required_extensions(&self) -> Result<()>; +} diff --git a/hypervisor/src/lib.rs b/hypervisor/src/lib.rs index 78ba0e178..fcf7ec0a1 100644 --- a/hypervisor/src/lib.rs +++ b/hypervisor/src/lib.rs @@ -21,11 +21,15 @@ /// KVM implementation module pub mod kvm; +/// Hypevisor related module +pub mod hypervisor; + /// Vm related module pub mod vm; /// CPU related module mod cpu; +pub use crate::hypervisor::{Hypervisor, HypervisorError}; pub use cpu::{HypervisorCpuError, Vcpu}; pub use kvm::*;