mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-06 06:31:31 +00:00
feat(xen): update xenclient and xenplatform to the latest structure (#433)
This commit is contained in:
@ -1,9 +1,10 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{
|
||||
boot::{BootSetup, BootSetupPlatform},
|
||||
elfloader::ElfImageLoader,
|
||||
boot::BootDomain, elfloader::ElfImageLoader, error::Error, ImageLoader, RuntimePlatform,
|
||||
RuntimePlatformType,
|
||||
};
|
||||
use log::warn;
|
||||
use uuid::Uuid;
|
||||
use xencall::XenCall;
|
||||
|
||||
@ -11,42 +12,92 @@ use crate::error::Result;
|
||||
|
||||
pub const XEN_EXTRA_MEMORY_KB: u64 = 2048;
|
||||
|
||||
pub struct BaseDomainManager<P: BootSetupPlatform> {
|
||||
pub struct PlatformDomainManager {
|
||||
call: XenCall,
|
||||
pub platform: Arc<P>,
|
||||
}
|
||||
|
||||
impl<P: BootSetupPlatform> BaseDomainManager<P> {
|
||||
pub async fn new(call: XenCall, platform: P) -> Result<BaseDomainManager<P>> {
|
||||
Ok(BaseDomainManager {
|
||||
call,
|
||||
platform: Arc::new(platform),
|
||||
})
|
||||
impl PlatformDomainManager {
|
||||
pub async fn new(call: XenCall) -> Result<PlatformDomainManager> {
|
||||
Ok(PlatformDomainManager { call })
|
||||
}
|
||||
|
||||
pub async fn create(&self, config: BaseDomainConfig) -> Result<CreatedDomain> {
|
||||
let mut domain = self.platform.create_domain(config.enable_iommu);
|
||||
fn max_memory_kb(resources: &PlatformResourcesConfig) -> u64 {
|
||||
(resources.max_memory_mb * 1024) + XEN_EXTRA_MEMORY_KB
|
||||
}
|
||||
|
||||
async fn create_base_domain(
|
||||
&self,
|
||||
config: &PlatformDomainConfig,
|
||||
platform: &RuntimePlatform,
|
||||
) -> Result<u32> {
|
||||
let mut domain = platform.create_domain(config.options.iommu);
|
||||
domain.handle = config.uuid.into_bytes();
|
||||
domain.max_vcpus = config.max_vcpus;
|
||||
domain.max_vcpus = config.resources.max_vcpus;
|
||||
let domid = self.call.create_domain(domain).await?;
|
||||
self.call.set_max_vcpus(domid, config.max_vcpus).await?;
|
||||
Ok(domid)
|
||||
}
|
||||
|
||||
async fn configure_domain_resources(
|
||||
&self,
|
||||
domid: u32,
|
||||
config: &PlatformDomainConfig,
|
||||
) -> Result<()> {
|
||||
self.call
|
||||
.set_max_mem(domid, (config.max_mem_mb * 1024) + XEN_EXTRA_MEMORY_KB)
|
||||
.set_max_vcpus(domid, config.resources.max_vcpus)
|
||||
.await?;
|
||||
let loader = ElfImageLoader::load_file_kernel(&config.kernel)?;
|
||||
let platform = (*self.platform).clone();
|
||||
let mut boot = BootSetup::new(self.call.clone(), domid, platform, loader, None);
|
||||
let mut domain = boot
|
||||
.initialize(
|
||||
&config.initrd,
|
||||
config.target_mem_mb,
|
||||
config.max_mem_mb,
|
||||
config.max_vcpus,
|
||||
&config.cmdline,
|
||||
self.call
|
||||
.set_max_mem(
|
||||
domid,
|
||||
PlatformDomainManager::max_memory_kb(&config.resources),
|
||||
)
|
||||
.await?;
|
||||
boot.boot(&mut domain).await?;
|
||||
Ok(CreatedDomain {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn create_internal(
|
||||
&self,
|
||||
domid: u32,
|
||||
config: &PlatformDomainConfig,
|
||||
mut platform: RuntimePlatform,
|
||||
) -> Result<BootDomain> {
|
||||
self.configure_domain_resources(domid, config).await?;
|
||||
let kernel = config.kernel.clone();
|
||||
let loader = tokio::task::spawn_blocking(move || match kernel.format {
|
||||
KernelFormat::ElfCompressed => ElfImageLoader::load(kernel.data),
|
||||
KernelFormat::ElfUncompressed => Ok(ElfImageLoader::new(kernel.data)),
|
||||
})
|
||||
.await
|
||||
.map_err(Error::AsyncJoinError)??;
|
||||
let loader = ImageLoader::Elf(loader);
|
||||
let mut domain = platform
|
||||
.initialize(
|
||||
domid,
|
||||
self.call.clone(),
|
||||
&loader,
|
||||
&config.kernel,
|
||||
&config.resources,
|
||||
)
|
||||
.await?;
|
||||
platform.boot(domid, self.call.clone(), &mut domain).await?;
|
||||
Ok(domain)
|
||||
}
|
||||
|
||||
pub async fn create(&self, config: PlatformDomainConfig) -> Result<PlatformDomainInfo> {
|
||||
let platform = config.platform.create();
|
||||
let domid = self.create_base_domain(&config, &platform).await?;
|
||||
let domain = match self.create_internal(domid, &config, platform).await {
|
||||
Ok(domain) => domain,
|
||||
Err(error) => {
|
||||
if let Err(destroy_fail) = self.call.destroy_domain(domid).await {
|
||||
warn!(
|
||||
"failed to destroy failed domain {}: {}",
|
||||
domid, destroy_fail
|
||||
);
|
||||
}
|
||||
return Err(error);
|
||||
}
|
||||
};
|
||||
Ok(PlatformDomainInfo {
|
||||
domid,
|
||||
store_evtchn: domain.store_evtchn,
|
||||
store_mfn: domain.store_mfn,
|
||||
@ -62,21 +113,43 @@ impl<P: BootSetupPlatform> BaseDomainManager<P> {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct BaseDomainConfig {
|
||||
pub struct PlatformDomainConfig {
|
||||
pub uuid: Uuid,
|
||||
pub owner_domid: u32,
|
||||
pub max_vcpus: u32,
|
||||
pub target_vcpus: u32,
|
||||
pub max_mem_mb: u64,
|
||||
pub target_mem_mb: u64,
|
||||
pub kernel: Vec<u8>,
|
||||
pub initrd: Vec<u8>,
|
||||
pub cmdline: String,
|
||||
pub enable_iommu: bool,
|
||||
pub platform: RuntimePlatformType,
|
||||
pub resources: PlatformResourcesConfig,
|
||||
pub kernel: PlatformKernelConfig,
|
||||
pub options: PlatformOptions,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CreatedDomain {
|
||||
pub struct PlatformKernelConfig {
|
||||
pub data: Arc<Vec<u8>>,
|
||||
pub format: KernelFormat,
|
||||
pub initrd: Option<Arc<Vec<u8>>>,
|
||||
pub cmdline: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PlatformResourcesConfig {
|
||||
pub max_vcpus: u32,
|
||||
pub assigned_vcpus: u32,
|
||||
pub max_memory_mb: u64,
|
||||
pub assigned_memory_mb: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PlatformOptions {
|
||||
pub iommu: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum KernelFormat {
|
||||
ElfUncompressed,
|
||||
ElfCompressed,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PlatformDomainInfo {
|
||||
pub domid: u32,
|
||||
pub store_evtchn: u32,
|
||||
pub store_mfn: u64,
|
||||
|
Reference in New Issue
Block a user