Skip to content

Commit

Permalink
libs/types: make the variable name easier to understand
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Tim-0731-Hzt authored and Fupan Li committed Feb 23, 2022
1 parent 7626f4b commit 1893986
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 68 deletions.
4 changes: 2 additions & 2 deletions src/libs/kata-types/src/config/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
20 changes: 10 additions & 10 deletions src/libs/kata-types/src/config/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
39 changes: 23 additions & 16 deletions src/libs/kata-types/src/config/hypervisor/dragonball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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(())
Expand Down
54 changes: 27 additions & 27 deletions src/libs/kata-types/src/config/hypervisor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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: {}")?;
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -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(())
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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(',')
Expand Down Expand Up @@ -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: {}"
Expand Down Expand Up @@ -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(())
}

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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());
}
Expand All @@ -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;
}
}
_ => {}
Expand All @@ -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(())
Expand Down Expand Up @@ -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));
}
Expand Down
15 changes: 11 additions & 4 deletions src/libs/kata-types/src/config/hypervisor/qemu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
Expand Down
Loading

0 comments on commit 1893986

Please sign in to comment.