libs/types: make the variable name easier to understand

1. modify default values for hypervisor
2. change the variable name
3. check the min memory limit

Signed-off-by: Zhongtao Hu <zhongtaohu.tim@linux.alibaba.com>
This commit is contained in:
Zhongtao Hu 2022-02-15 16:46:27 +08:00 committed by Fupan Li
parent b9b6d70aae
commit 48c201a1ac
8 changed files with 82 additions and 68 deletions

View File

@ -56,8 +56,8 @@ pub struct Agent {
}
impl ConfigOps for Agent {
fn adjust_configuration(conf: &mut TomlConfig) -> Result<()> {
AgentVendor::adjust_configuration(conf)?;
fn adjust_config(conf: &mut TomlConfig) -> Result<()> {
AgentVendor::adjust_config(conf)?;
Ok(())
}

View File

@ -26,33 +26,33 @@ pub const DEFAULT_BLOCK_NVDIMM_MEM_OFFSET: u64 = 0;
pub const DEFAULT_SHARED_FS_TYPE: &str = "virtio-fs";
pub const DEFAULT_VIRTIO_FS_CACHE_MODE: &str = "none";
pub const DEFAULT_VIRTIO_FS_DAX_SIZE_MB: u32 = 1024;
pub const DEFAULT_SHARED_9PFS_SIZE: u32 = 128 * 1024;
pub const MIN_SHARED_9PFS_SIZE: u32 = 4 * 1024;
pub const MAX_SHARED_9PFS_SIZE: u32 = 8 * 1024 * 1024;
pub const DEFAULT_SHARED_9PFS_SIZE_MB: u32 = 128 * 1024;
pub const MIN_SHARED_9PFS_SIZE_MB: u32 = 4 * 1024;
pub const MAX_SHARED_9PFS_SIZE_MB: u32 = 8 * 1024 * 1024;
pub const DEFAULT_GUEST_HOOK_PATH: &str = "/opt";
pub const DEFAULT_GUEST_HOOK_PATH: &str = "/opt/kata/hooks";
pub const DEFAULT_GUEST_VCPUS: u32 = 1;
// Default configuration for Dragonball
// Default configuration for dragonball
pub const DEFAULT_DRAGONBALL_GUEST_KERNEL_IMAGE: &str = "vmlinuz";
pub const DEFAULT_DRAGONBALL_GUEST_KERNEL_PARAMS: &str = "";
pub const DEFAULT_DRAGONBALL_ENTROPY_SOURCE: &str = "/dev/urandom";
pub const DEFAULT_DRAGONBALL_MEMORY_SIZE: u32 = 128;
pub const DEFAULT_DRAGONBALL_MEMORY_SIZE_MB: u32 = 128;
pub const DEFAULT_DRAGONBALL_MEMORY_SLOTS: u32 = 128;
pub const MAX_DRAGONBALL_VCPUS: u32 = 256;
pub const MIN_DRAGONBALL_MEMORY_SIZE: u32 = 64;
pub const MIN_DRAGONBALL_MEMORY_SIZE_MB: u32 = 64;
// Default configuration for qemu
pub const DEFAULT_QEMU_BINARY_PATH: &str = "qemu";
pub const DEFAULT_QEMU_BINARY_PATH: &str = "/usr/bin/qemu-system-x86_64";
pub const DEFAULT_QEMU_CONTROL_PATH: &str = "";
pub const DEFAULT_QEMU_MACHINE_TYPE: &str = "q35";
pub const DEFAULT_QEMU_ENTROPY_SOURCE: &str = "/dev/urandom";
pub const DEFAULT_QEMU_GUEST_KERNEL_IMAGE: &str = "vmlinuz";
pub const DEFAULT_QEMU_GUEST_KERNEL_PARAMS: &str = "";
pub const DEFAULT_QEMU_FIRMWARE_PATH: &str = "";
pub const DEFAULT_QEMU_MEMORY_SIZE: u32 = 128;
pub const DEFAULT_QEMU_MEMORY_SIZE_MB: u32 = 128;
pub const DEFAULT_QEMU_MEMORY_SLOTS: u32 = 128;
pub const DEFAULT_QEMU_PCI_BRIDGES: u32 = 2;
pub const MAX_QEMU_PCI_BRIDGES: u32 = 5;
pub const MAX_QEMU_VCPUS: u32 = 256;
pub const MIN_QEMU_MEMORY_SIZE: u32 = 64;
pub const MIN_QEMU_MEMORY_SIZE_MB: u32 = 64;

View File

@ -10,14 +10,14 @@ use std::u32;
use super::{default, register_hypervisor_plugin};
use crate::config::default::MAX_DRAGONBALL_VCPUS;
use crate::config::default::MIN_DRAGONBALL_MEMORY_SIZE;
use crate::config::default::MIN_DRAGONBALL_MEMORY_SIZE_MB;
use crate::config::hypervisor::{
VIRTIO_BLK, VIRTIO_BLK_MMIO, VIRTIO_FS, VIRTIO_FS_INLINE, VIRTIO_PMEM,
};
use crate::config::{ConfigPlugin, TomlConfig};
use crate::{eother, resolve_path, validate_path};
/// Hypervisor name for qemu, used to index `TomlConfig::hypervisor`.
/// Hypervisor name for dragonball, used to index `TomlConfig::hypervisor`.
pub const HYPERVISOR_NAME_DRAGONBALL: &str = "dragonball";
/// Configuration information for dragonball.
@ -42,16 +42,16 @@ impl ConfigPlugin for DragonballConfig {
MAX_DRAGONBALL_VCPUS
}
fn get_min_memory(&self) -> u32 {
MIN_DRAGONBALL_MEMORY_SIZE
MIN_DRAGONBALL_MEMORY_SIZE_MB
}
fn name(&self) -> &str {
HYPERVISOR_NAME_DRAGONBALL
}
/// Adjust the configuration information after loading from configuration file.
fn adjust_configuration(&self, conf: &mut TomlConfig) -> Result<()> {
fn adjust_config(&self, conf: &mut TomlConfig) -> Result<()> {
if let Some(db) = conf.hypervisor.get_mut(HYPERVISOR_NAME_DRAGONBALL) {
resolve_path!(db.jailer_path, "Dragonball jailer path {} is invalid: {}")?;
resolve_path!(db.jailer_path, "dragonball jailer path {} is invalid: {}")?;
if db.boot_info.kernel.is_empty() {
db.boot_info.kernel = default::DEFAULT_DRAGONBALL_GUEST_KERNEL_IMAGE.to_string();
@ -71,7 +71,7 @@ impl ConfigPlugin for DragonballConfig {
}
if db.memory_info.default_memory == 0 {
db.memory_info.default_memory = default::DEFAULT_DRAGONBALL_MEMORY_SIZE;
db.memory_info.default_memory = default::DEFAULT_DRAGONBALL_MEMORY_SIZE_MB;
}
if db.memory_info.memory_slots == 0 {
db.memory_info.memory_slots = default::DEFAULT_DRAGONBALL_MEMORY_SLOTS;
@ -97,9 +97,9 @@ impl ConfigPlugin for DragonballConfig {
if !db.valid_ctlpaths.is_empty() {
return Err(eother!("CtlPath for dragonball hypervisor should be empty"));
}
validate_path!(db.jailer_path, "Dragonball jailer path {} is invalid: {}")?;
validate_path!(db.jailer_path, "dragonball jailer path {} is invalid: {}")?;
if db.enable_iothreads {
return Err(eother!("Dragonball hypervisor doesn't support IO threads."));
return Err(eother!("dragonball hypervisor doesn't support IO threads."));
}
if !db.blockdev_info.disable_block_device_use
@ -137,49 +137,56 @@ impl ConfigPlugin for DragonballConfig {
|| db.cpu_info.default_maxvcpus > default::MAX_DRAGONBALL_VCPUS
{
return Err(eother!(
"Dragonball hypervisor can not support {} vCPUs",
"dragonball hypervisor can not support {} vCPUs",
db.cpu_info.default_maxvcpus
));
}
if db.device_info.enable_iommu || db.device_info.enable_iommu_platform {
return Err(eother!("Dragonball hypervisor does not support vIOMMU"));
return Err(eother!("dragonball hypervisor does not support vIOMMU"));
}
if db.device_info.hotplug_vfio_on_root_bus
|| db.device_info.default_bridges > 0
|| db.device_info.pcie_root_port > 0
{
return Err(eother!(
"Dragonball hypervisor does not support PCI hotplug options"
"dragonball hypervisor does not support PCI hotplug options"
));
}
if !db.machine_info.machine_type.is_empty() {
return Err(eother!(
"Dragonball hypervisor does not support machine_type"
"dragonball hypervisor does not support machine_type"
));
}
if !db.machine_info.pflashes.is_empty() {
return Err(eother!("Dragonball hypervisor does not support pflashes"));
return Err(eother!("dragonball hypervisor does not support pflashes"));
}
if db.memory_info.enable_guest_swap {
return Err(eother!(
"Dragonball hypervisor doesn't support enable_guest_swap"
"dragonball hypervisor doesn't support enable_guest_swap"
));
}
if db.security_info.rootless {
return Err(eother!(
"Dragonball hypervisor does not support rootless mode"
"dragonball hypervisor does not support rootless mode"
));
}
if let Some(v) = db.shared_fs.shared_fs.as_ref() {
if v != VIRTIO_FS && v != VIRTIO_FS_INLINE {
return Err(eother!("Dragonball hypervisor doesn't support {}", v));
return Err(eother!("dragonball hypervisor doesn't support {}", v));
}
}
if db.memory_info.default_memory < MIN_DRAGONBALL_MEMORY_SIZE_MB {
return Err(eother!(
"dragonball hypervisor has minimal memory limitation {}",
MIN_DRAGONBALL_MEMORY_SIZE_MB
));
}
}
Ok(())

View File

@ -134,7 +134,7 @@ pub struct BlockDeviceInfo {
impl BlockDeviceInfo {
/// Adjust the configuration information after loading from configuration file.
pub fn adjust_configuration(&mut self) -> Result<()> {
pub fn adjust_config(&mut self) -> Result<()> {
if self.disable_block_device_use {
self.block_device_driver = "".to_string();
self.enable_vhost_user_store = false;
@ -217,7 +217,7 @@ pub struct BootInfo {
impl BootInfo {
/// Adjust the configuration information after loading from configuration file.
pub fn adjust_configuration(&mut self) -> Result<()> {
pub fn adjust_config(&mut self) -> Result<()> {
resolve_path!(self.kernel, "guest kernel image file {} is invalid: {}")?;
resolve_path!(self.image, "guest boot image file {} is invalid: {}")?;
resolve_path!(self.initrd, "guest initrd image file {} is invalid: {}")?;
@ -286,7 +286,7 @@ pub struct CpuInfo {
impl CpuInfo {
/// Adjust the configuration information after loading from configuration file.
pub fn adjust_configuration(&mut self) -> Result<()> {
pub fn adjust_config(&mut self) -> Result<()> {
let features: Vec<&str> = self.cpu_features.split(',').map(|v| v.trim()).collect();
self.cpu_features = features.join(",");
Ok(())
@ -347,7 +347,7 @@ pub struct DebugInfo {
impl DebugInfo {
/// Adjust the configuration information after loading from configuration file.
pub fn adjust_configuration(&mut self) -> Result<()> {
pub fn adjust_config(&mut self) -> Result<()> {
Ok(())
}
@ -406,7 +406,7 @@ pub struct DeviceInfo {
impl DeviceInfo {
/// Adjust the configuration information after loading from configuration file.
pub fn adjust_configuration(&mut self) -> Result<()> {
pub fn adjust_config(&mut self) -> Result<()> {
if self.default_bridges > MAX_BRIDGE_SIZE {
self.default_bridges = MAX_BRIDGE_SIZE;
}
@ -463,7 +463,7 @@ pub struct MachineInfo {
impl MachineInfo {
/// Adjust the configuration information after loading from configuration file.
pub fn adjust_configuration(&mut self) -> Result<()> {
pub fn adjust_config(&mut self) -> Result<()> {
let accelerators: Vec<&str> = self
.machine_accelerators
.split(',')
@ -566,7 +566,7 @@ pub struct MemoryInfo {
impl MemoryInfo {
/// Adjust the configuration information after loading from configuration file.
pub fn adjust_configuration(&mut self) -> Result<()> {
pub fn adjust_config(&mut self) -> Result<()> {
resolve_path!(
self.file_mem_backend,
"Memory backend file {} is invalid: {}"
@ -624,7 +624,7 @@ pub struct NetworkInfo {
impl NetworkInfo {
/// Adjust the configuration information after loading from configuration file.
pub fn adjust_configuration(&mut self) -> Result<()> {
pub fn adjust_config(&mut self) -> Result<()> {
Ok(())
}
@ -688,7 +688,7 @@ pub struct SecurityInfo {
impl SecurityInfo {
/// Adjust the configuration information after loading from configuration file.
pub fn adjust_configuration(&mut self) -> Result<()> {
pub fn adjust_config(&mut self) -> Result<()> {
if self.guest_hook_path.is_empty() {
self.guest_hook_path = default::DEFAULT_GUEST_HOOK_PATH.to_string();
}
@ -770,7 +770,7 @@ pub struct SharedFsInfo {
impl SharedFsInfo {
/// Adjust the configuration information after loading from configuration file.
pub fn adjust_configuration(&mut self) -> Result<()> {
pub fn adjust_config(&mut self) -> Result<()> {
if self.shared_fs.as_deref() == Some("") {
self.shared_fs = Some(default::DEFAULT_SHARED_FS_TYPE.to_string());
}
@ -779,7 +779,7 @@ impl SharedFsInfo {
Some(VIRTIO_FS_INLINE) => self.adjust_virtio_fs(true)?,
Some(VIRTIO_9P) => {
if self.msize_9p == 0 {
self.msize_9p = default::DEFAULT_SHARED_9PFS_SIZE;
self.msize_9p = default::DEFAULT_SHARED_9PFS_SIZE_MB;
}
}
_ => {}
@ -795,12 +795,12 @@ impl SharedFsInfo {
Some(VIRTIO_FS) => self.validate_virtio_fs(false),
Some(VIRTIO_FS_INLINE) => self.validate_virtio_fs(true),
Some(VIRTIO_9P) => {
if self.msize_9p < default::MIN_SHARED_9PFS_SIZE
|| self.msize_9p > default::MAX_SHARED_9PFS_SIZE
if self.msize_9p < default::MIN_SHARED_9PFS_SIZE_MB
|| self.msize_9p > default::MAX_SHARED_9PFS_SIZE_MB
{
return Err(eother!(
"Invalid 9p configuration msize 0x{:x}, min value is 0x{:x}, max value is 0x{:x}",
self.msize_9p,default::MIN_SHARED_9PFS_SIZE, default::MAX_SHARED_9PFS_SIZE
self.msize_9p,default::MIN_SHARED_9PFS_SIZE_MB, default::MAX_SHARED_9PFS_SIZE_MB
));
}
Ok(())
@ -967,26 +967,26 @@ impl Hypervisor {
}
impl ConfigOps for Hypervisor {
fn adjust_configuration(conf: &mut TomlConfig) -> Result<()> {
HypervisorVendor::adjust_configuration(conf)?;
fn adjust_config(conf: &mut TomlConfig) -> Result<()> {
HypervisorVendor::adjust_config(conf)?;
let hypervisors: Vec<String> = conf.hypervisor.keys().cloned().collect();
for hypervisor in hypervisors.iter() {
if let Some(plugin) = get_hypervisor_plugin(hypervisor) {
plugin.adjust_configuration(conf)?;
plugin.adjust_config(conf)?;
// Safe to unwrap() because `hypervisor` is a valid key in the hash map.
let hv = conf.hypervisor.get_mut(hypervisor).ok_or_else(|| {
io::Error::new(io::ErrorKind::NotFound, "hypervisor not found".to_string())
})?;
hv.blockdev_info.adjust_configuration()?;
hv.boot_info.adjust_configuration()?;
hv.cpu_info.adjust_configuration()?;
hv.debug_info.adjust_configuration()?;
hv.device_info.adjust_configuration()?;
hv.machine_info.adjust_configuration()?;
hv.memory_info.adjust_configuration()?;
hv.network_info.adjust_configuration()?;
hv.security_info.adjust_configuration()?;
hv.shared_fs.adjust_configuration()?;
hv.blockdev_info.adjust_config()?;
hv.boot_info.adjust_config()?;
hv.cpu_info.adjust_config()?;
hv.debug_info.adjust_config()?;
hv.device_info.adjust_config()?;
hv.machine_info.adjust_config()?;
hv.memory_info.adjust_config()?;
hv.network_info.adjust_config()?;
hv.security_info.adjust_config()?;
hv.shared_fs.adjust_config()?;
} else {
return Err(eother!("Can not find plugin for hypervisor {}", hypervisor));
}

View File

@ -10,7 +10,7 @@ use std::sync::Arc;
use super::{default, register_hypervisor_plugin};
use crate::config::default::MAX_QEMU_VCPUS;
use crate::config::default::MIN_QEMU_MEMORY_SIZE;
use crate::config::default::MIN_QEMU_MEMORY_SIZE_MB;
use crate::config::hypervisor::VIRTIO_BLK_MMIO;
use crate::config::{ConfigPlugin, TomlConfig};
@ -42,14 +42,14 @@ impl ConfigPlugin for QemuConfig {
}
fn get_min_memory(&self) -> u32 {
MIN_QEMU_MEMORY_SIZE
MIN_QEMU_MEMORY_SIZE_MB
}
fn name(&self) -> &str {
HYPERVISOR_NAME_QEMU
}
/// Adjust the configuration information after loading from configuration file.
fn adjust_configuration(&self, conf: &mut TomlConfig) -> Result<()> {
fn adjust_config(&self, conf: &mut TomlConfig) -> Result<()> {
if let Some(qemu) = conf.hypervisor.get_mut(HYPERVISOR_NAME_QEMU) {
if qemu.path.is_empty() {
qemu.path = default::DEFAULT_QEMU_BINARY_PATH.to_string();
@ -83,7 +83,7 @@ impl ConfigPlugin for QemuConfig {
}
if qemu.memory_info.default_memory == 0 {
qemu.memory_info.default_memory = default::DEFAULT_QEMU_MEMORY_SIZE;
qemu.memory_info.default_memory = default::DEFAULT_QEMU_MEMORY_SIZE_MB;
}
if qemu.memory_info.memory_slots == 0 {
qemu.memory_info.memory_slots = default::DEFAULT_QEMU_MEMORY_SLOTS;
@ -136,6 +136,13 @@ impl ConfigPlugin for QemuConfig {
qemu.device_info.default_bridges
));
}
if qemu.memory_info.default_memory < MIN_QEMU_MEMORY_SIZE_MB {
return Err(eother!(
"Qemu hypervisor has minimal memory limitation {}",
MIN_QEMU_MEMORY_SIZE_MB
));
}
}
Ok(())

View File

@ -36,7 +36,7 @@ pub trait ConfigPlugin: Send + Sync {
fn name(&self) -> &str;
/// Adjust the configuration information after loading from configuration file.
fn adjust_configuration(&self, _conf: &mut TomlConfig) -> Result<()>;
fn adjust_config(&self, _conf: &mut TomlConfig) -> Result<()>;
/// Validate the configuration information.
fn validate(&self, _conf: &TomlConfig) -> Result<()>;
@ -51,7 +51,7 @@ pub trait ConfigPlugin: Send + Sync {
/// Trait to manipulate Kata configuration information.
pub trait ConfigOps {
/// Adjust the configuration information after loading from configuration file.
fn adjust_configuration(_conf: &mut TomlConfig) -> Result<()> {
fn adjust_config(_conf: &mut TomlConfig) -> Result<()> {
Ok(())
}
@ -64,7 +64,7 @@ pub trait ConfigOps {
/// Trait to manipulate global Kata configuration information.
pub trait ConfigObjectOps {
/// Adjust the configuration information after loading from configuration file.
fn adjust_configuration(&mut self) -> Result<()> {
fn adjust_config(&mut self) -> Result<()> {
Ok(())
}
@ -136,9 +136,9 @@ impl TomlConfig {
/// Load Kata configuration information from string.
pub fn load(content: &str) -> Result<TomlConfig> {
let mut config: TomlConfig = toml::from_str(content)?;
Hypervisor::adjust_configuration(&mut config)?;
Runtime::adjust_configuration(&mut config)?;
Agent::adjust_configuration(&mut config)?;
Hypervisor::adjust_config(&mut config)?;
Runtime::adjust_config(&mut config)?;
Agent::adjust_config(&mut config)?;
info!(sl!(), "get kata config: {:?}", config);
Ok(config)
}

View File

@ -113,8 +113,8 @@ pub struct Runtime {
}
impl ConfigOps for Runtime {
fn adjust_configuration(conf: &mut TomlConfig) -> Result<()> {
RuntimeVendor::adjust_configuration(conf)?;
fn adjust_config(conf: &mut TomlConfig) -> Result<()> {
RuntimeVendor::adjust_config(conf)?;
if conf.runtime.internetworking_model.is_empty() {
conf.runtime.internetworking_model = default::DEFAULT_INTERNETWORKING_MODEL.to_owned();
}

View File

@ -20,7 +20,7 @@ pub struct RuntimeVendor {
}
impl ConfigOps for RuntimeVendor {
fn adjust_configuration(conf: &mut TomlConfig) -> Result<()> {
fn adjust_config(conf: &mut TomlConfig) -> Result<()> {
if conf.runtime.vendor.log_level > Level::Debug as u32 {
conf.runtime.debug = true;
}