2019-05-23 19:48:05 +00:00
|
|
|
// Copyright © 2019 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2019-08-28 10:01:01 +00:00
|
|
|
extern crate vm_virtio;
|
|
|
|
|
2019-12-12 17:12:42 +00:00
|
|
|
use clap::ArgMatches;
|
2019-05-23 19:48:05 +00:00
|
|
|
use net_util::MacAddr;
|
2020-03-27 11:15:55 +00:00
|
|
|
use std::collections::HashMap;
|
2019-05-23 19:48:05 +00:00
|
|
|
use std::convert::From;
|
2020-01-24 07:30:50 +00:00
|
|
|
use std::io;
|
2019-05-23 19:48:05 +00:00
|
|
|
use std::net::AddrParseError;
|
|
|
|
use std::net::Ipv4Addr;
|
2019-09-23 17:46:35 +00:00
|
|
|
use std::path::PathBuf;
|
2019-05-23 19:48:05 +00:00
|
|
|
use std::result;
|
2020-03-27 11:15:55 +00:00
|
|
|
use std::str::FromStr;
|
2019-05-23 19:48:05 +00:00
|
|
|
|
2019-09-27 09:40:50 +00:00
|
|
|
pub const DEFAULT_VCPUS: u8 = 1;
|
|
|
|
pub const DEFAULT_MEMORY_MB: u64 = 512;
|
2019-05-23 19:48:05 +00:00
|
|
|
pub const DEFAULT_RNG_SOURCE: &str = "/dev/urandom";
|
2019-12-10 16:09:28 +00:00
|
|
|
pub const DEFAULT_NUM_QUEUES_VUNET: usize = 2;
|
|
|
|
pub const DEFAULT_QUEUE_SIZE_VUNET: u16 = 256;
|
2019-12-10 16:15:43 +00:00
|
|
|
pub const DEFAULT_NUM_QUEUES_VUBLK: usize = 1;
|
|
|
|
pub const DEFAULT_QUEUE_SIZE_VUBLK: u16 = 128;
|
2019-05-23 19:48:05 +00:00
|
|
|
|
|
|
|
/// Errors associated with VM configuration parameters.
|
|
|
|
#[derive(Debug)]
|
2020-01-24 07:30:50 +00:00
|
|
|
pub enum Error {
|
2019-11-25 14:18:03 +00:00
|
|
|
/// Max is less than boot
|
|
|
|
ParseCpusMaxLowerThanBoot,
|
2020-03-04 02:16:07 +00:00
|
|
|
/// Failed parsing memory hotplug_method parameter.
|
2020-03-27 12:34:05 +00:00
|
|
|
ParseMemoryHotplugMethodParam(ParseHotplugMethodError),
|
2019-05-24 19:21:23 +00:00
|
|
|
/// Failed parsing memory file parameter.
|
|
|
|
ParseMemoryFileParam,
|
2019-05-23 19:48:05 +00:00
|
|
|
/// Failed parsing kernel parameters.
|
|
|
|
ParseKernelParams,
|
|
|
|
/// Failed parsing kernel command line parameters.
|
|
|
|
ParseCmdlineParams,
|
|
|
|
/// Failed parsing disks parameters.
|
|
|
|
ParseDisksParams,
|
2020-01-24 15:31:13 +00:00
|
|
|
/// Failed parsing disk queue number parameter.
|
|
|
|
ParseDiskNumQueuesParam(std::num::ParseIntError),
|
2020-02-20 15:49:16 +00:00
|
|
|
/// Failed parsing disk poll_queue parameter.
|
|
|
|
ParseDiskPollQueueParam(std::str::ParseBoolError),
|
2020-01-24 15:31:13 +00:00
|
|
|
/// Failed parsing disk queue size parameter.
|
|
|
|
ParseDiskQueueSizeParam(std::num::ParseIntError),
|
2020-01-28 11:43:15 +00:00
|
|
|
/// Failed to parse vhost parameters
|
|
|
|
ParseDiskVhostParam(std::str::ParseBoolError),
|
|
|
|
/// Failed parsing disk wce parameter.
|
|
|
|
ParseDiskWceParam(std::str::ParseBoolError),
|
2020-03-13 10:28:39 +00:00
|
|
|
/// Both socket and path specified
|
|
|
|
ParseDiskSocketAndPath,
|
2019-05-23 19:48:05 +00:00
|
|
|
/// Failed parsing random number generator parameters.
|
|
|
|
ParseRngParams,
|
|
|
|
/// Failed parsing network ip parameter.
|
|
|
|
ParseNetIpParam(AddrParseError),
|
|
|
|
/// Failed parsing network mask parameter.
|
|
|
|
ParseNetMaskParam(AddrParseError),
|
|
|
|
/// Failed parsing network mac parameter.
|
2020-01-24 07:30:50 +00:00
|
|
|
ParseNetMacParam(io::Error),
|
2019-11-12 09:14:54 +00:00
|
|
|
/// Failed parsing network queue number parameter.
|
|
|
|
ParseNetNumQueuesParam(std::num::ParseIntError),
|
|
|
|
/// Failed parsing network queue size parameter.
|
|
|
|
ParseNetQueueSizeParam(std::num::ParseIntError),
|
2020-01-27 15:14:07 +00:00
|
|
|
/// Failed to parse vhost parameters
|
|
|
|
ParseNetVhostParam(std::str::ParseBoolError),
|
|
|
|
/// Need a vhost socket
|
|
|
|
ParseNetVhostSocketRequired,
|
2019-05-22 20:06:49 +00:00
|
|
|
/// Failed parsing fs tag parameter.
|
|
|
|
ParseFsTagParam,
|
|
|
|
/// Failed parsing fs socket path parameter.
|
|
|
|
ParseFsSockParam,
|
|
|
|
/// Failed parsing fs number of queues parameter.
|
|
|
|
ParseFsNumQueuesParam(std::num::ParseIntError),
|
|
|
|
/// Failed parsing fs queue size parameter.
|
|
|
|
ParseFsQueueSizeParam(std::num::ParseIntError),
|
2019-08-05 19:45:58 +00:00
|
|
|
/// Failed parsing fs dax parameter.
|
|
|
|
ParseFsDax,
|
|
|
|
/// Cannot have dax=off along with cache_size parameter.
|
|
|
|
InvalidCacheSizeWithDaxOff,
|
2019-06-19 20:48:37 +00:00
|
|
|
/// Failed parsing persitent memory file parameter.
|
|
|
|
ParsePmemFileParam,
|
2019-07-09 03:13:02 +00:00
|
|
|
/// Failed parsing size parameter.
|
|
|
|
ParseSizeParam(std::num::ParseIntError),
|
2019-07-22 19:29:02 +00:00
|
|
|
/// Failed parsing console parameter.
|
|
|
|
ParseConsoleParam,
|
|
|
|
/// Both console and serial are tty.
|
|
|
|
ParseTTYParam,
|
2019-08-28 10:01:01 +00:00
|
|
|
/// Failed parsing vhost-user-net mac parameter.
|
2020-01-24 07:30:50 +00:00
|
|
|
ParseVuNetMacParam(io::Error),
|
2019-09-10 11:28:30 +00:00
|
|
|
/// Failed parsing vhost-user sock parameter.
|
|
|
|
ParseVuSockParam,
|
|
|
|
/// Failed parsing vhost-user queue number parameter.
|
|
|
|
ParseVuNumQueuesParam(std::num::ParseIntError),
|
|
|
|
/// Failed parsing vhost-user queue size parameter.
|
|
|
|
ParseVuQueueSizeParam(std::num::ParseIntError),
|
2019-08-28 10:01:01 +00:00
|
|
|
/// Failed parsing vhost-user-net server parameter.
|
|
|
|
ParseVuNetServerParam(std::num::ParseIntError),
|
2019-09-11 03:16:41 +00:00
|
|
|
/// Failed parsing vhost-user-blk wce parameter.
|
|
|
|
ParseVuBlkWceParam(std::str::ParseBoolError),
|
2019-09-04 17:39:17 +00:00
|
|
|
/// Failed parsing vsock context ID parameter.
|
|
|
|
ParseVsockCidParam(std::num::ParseIntError),
|
|
|
|
/// Failed parsing vsock socket path parameter.
|
|
|
|
ParseVsockSockParam,
|
2019-09-19 07:52:55 +00:00
|
|
|
/// Missing kernel configuration
|
|
|
|
ValidateMissingKernelConfig,
|
2019-11-19 23:40:57 +00:00
|
|
|
/// Failed parsing generic on|off parameter.
|
|
|
|
ParseOnOff,
|
2020-03-27 11:51:09 +00:00
|
|
|
/// Error parsing CPU options
|
|
|
|
ParseCpus(OptionParserError),
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
2020-01-24 07:30:50 +00:00
|
|
|
pub type Result<T> = result::Result<T, Error>;
|
2019-05-23 19:48:05 +00:00
|
|
|
|
2020-03-27 11:15:55 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct OptionParser {
|
|
|
|
options: HashMap<String, OptionParserValue>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct OptionParserValue {
|
|
|
|
value: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum OptionParserError {
|
|
|
|
UnknownOption(String),
|
|
|
|
InvalidSyntax(String),
|
|
|
|
Conversion(String, String),
|
|
|
|
}
|
|
|
|
|
|
|
|
type OptionParserResult<T> = std::result::Result<T, OptionParserError>;
|
|
|
|
|
|
|
|
impl OptionParser {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
options: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse(&mut self, input: &str) -> OptionParserResult<()> {
|
|
|
|
if input.trim().is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let options_list: Vec<&str> = input.trim().split(',').collect();
|
|
|
|
|
|
|
|
for option in options_list.iter() {
|
|
|
|
let parts: Vec<&str> = option.split('=').collect();
|
|
|
|
|
|
|
|
if parts.len() != 2 {
|
|
|
|
return Err(OptionParserError::InvalidSyntax((*option).to_owned()));
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.options.get_mut(parts[0]) {
|
|
|
|
None => return Err(OptionParserError::UnknownOption(parts[0].to_owned())),
|
|
|
|
Some(value) => {
|
|
|
|
value.value = Some(parts[1].trim().to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add(&mut self, option: &str) -> &mut Self {
|
|
|
|
self.options
|
|
|
|
.insert(option.to_owned(), OptionParserValue { value: None });
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, option: &str) -> Option<String> {
|
|
|
|
self.options.get(option).and_then(|v| v.value.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_set(&self, option: &str) -> bool {
|
|
|
|
self.options
|
|
|
|
.get(option)
|
|
|
|
.and_then(|v| v.value.as_ref())
|
|
|
|
.is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn convert<T: FromStr>(&self, option: &str) -> OptionParserResult<Option<T>> {
|
|
|
|
match self.options.get(option).and_then(|v| v.value.as_ref()) {
|
|
|
|
None => Ok(None),
|
|
|
|
Some(v) => Ok(Some(v.parse().map_err(|_| {
|
|
|
|
OptionParserError::Conversion(option.to_owned(), v.to_owned())
|
|
|
|
})?)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 19:48:05 +00:00
|
|
|
pub struct VmParams<'a> {
|
|
|
|
pub cpus: &'a str,
|
|
|
|
pub memory: &'a str,
|
2019-09-19 07:52:55 +00:00
|
|
|
pub kernel: Option<&'a str>,
|
2020-03-15 17:51:17 +00:00
|
|
|
pub initramfs: Option<&'a str>,
|
2019-05-23 19:48:05 +00:00
|
|
|
pub cmdline: Option<&'a str>,
|
2019-07-08 22:48:39 +00:00
|
|
|
pub disks: Option<Vec<&'a str>>,
|
2019-07-08 22:31:13 +00:00
|
|
|
pub net: Option<Vec<&'a str>>,
|
2019-05-22 20:06:49 +00:00
|
|
|
pub rng: &'a str,
|
2019-06-27 16:14:11 +00:00
|
|
|
pub fs: Option<Vec<&'a str>>,
|
2019-06-28 08:45:58 +00:00
|
|
|
pub pmem: Option<Vec<&'a str>>,
|
2019-07-10 14:41:46 +00:00
|
|
|
pub serial: &'a str,
|
2019-07-22 19:29:02 +00:00
|
|
|
pub console: &'a str,
|
2019-07-15 09:42:40 +00:00
|
|
|
pub devices: Option<Vec<&'a str>>,
|
2019-09-04 17:39:17 +00:00
|
|
|
pub vsock: Option<Vec<&'a str>>,
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 17:12:42 +00:00
|
|
|
impl<'a> VmParams<'a> {
|
|
|
|
pub fn from_arg_matches(args: &'a ArgMatches) -> Self {
|
|
|
|
// These .unwrap()s cannot fail as there is a default value defined
|
|
|
|
let cpus = args.value_of("cpus").unwrap();
|
|
|
|
let memory = args.value_of("memory").unwrap();
|
|
|
|
let rng = args.value_of("rng").unwrap();
|
|
|
|
let serial = args.value_of("serial").unwrap();
|
|
|
|
|
|
|
|
let kernel = args.value_of("kernel");
|
2020-03-15 17:51:17 +00:00
|
|
|
let initramfs = args.value_of("initramfs");
|
2019-12-12 17:12:42 +00:00
|
|
|
let cmdline = args.value_of("cmdline");
|
|
|
|
|
|
|
|
let disks: Option<Vec<&str>> = args.values_of("disk").map(|x| x.collect());
|
|
|
|
let net: Option<Vec<&str>> = args.values_of("net").map(|x| x.collect());
|
|
|
|
let console = args.value_of("console").unwrap();
|
|
|
|
let fs: Option<Vec<&str>> = args.values_of("fs").map(|x| x.collect());
|
|
|
|
let pmem: Option<Vec<&str>> = args.values_of("pmem").map(|x| x.collect());
|
|
|
|
let devices: Option<Vec<&str>> = args.values_of("device").map(|x| x.collect());
|
|
|
|
let vsock: Option<Vec<&str>> = args.values_of("vsock").map(|x| x.collect());
|
|
|
|
|
|
|
|
VmParams {
|
|
|
|
cpus,
|
|
|
|
memory,
|
|
|
|
kernel,
|
2020-03-15 17:51:17 +00:00
|
|
|
initramfs,
|
2019-12-12 17:12:42 +00:00
|
|
|
cmdline,
|
|
|
|
disks,
|
|
|
|
net,
|
|
|
|
rng,
|
|
|
|
fs,
|
|
|
|
pmem,
|
|
|
|
serial,
|
|
|
|
console,
|
|
|
|
devices,
|
|
|
|
vsock,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 12:33:32 +00:00
|
|
|
struct Toggle(bool);
|
|
|
|
|
|
|
|
enum ToggleParseError {
|
|
|
|
InvalidValue(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Toggle {
|
|
|
|
type Err = ToggleParseError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
|
|
|
|
Ok(Toggle(parse_on_off(s).map_err(|_| {
|
|
|
|
ToggleParseError::InvalidValue(s.to_owned())
|
|
|
|
})?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 12:34:05 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
|
|
|
pub enum HotplugMethod {
|
|
|
|
Acpi,
|
|
|
|
VirtioMem,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for HotplugMethod {
|
|
|
|
fn default() -> Self {
|
|
|
|
HotplugMethod::Acpi
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ParseHotplugMethodError {
|
|
|
|
InvalidValue(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for HotplugMethod {
|
|
|
|
type Err = ParseHotplugMethodError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
|
|
|
|
match s.to_lowercase().as_str() {
|
|
|
|
"acpi" => Ok(HotplugMethod::Acpi),
|
|
|
|
"virtio-mem" => Ok(HotplugMethod::VirtioMem),
|
|
|
|
_ => Err(ParseHotplugMethodError::InvalidValue(s.to_owned())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 03:13:02 +00:00
|
|
|
fn parse_size(size: &str) -> Result<u64> {
|
|
|
|
let s = size.trim();
|
|
|
|
|
|
|
|
let shift = if s.ends_with('K') {
|
|
|
|
10
|
|
|
|
} else if s.ends_with('M') {
|
|
|
|
20
|
|
|
|
} else if s.ends_with('G') {
|
|
|
|
30
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
let s = s.trim_end_matches(|c| c == 'K' || c == 'M' || c == 'G');
|
|
|
|
let res = s.parse::<u64>().map_err(Error::ParseSizeParam)?;
|
|
|
|
Ok(res << shift)
|
|
|
|
}
|
|
|
|
|
2019-11-19 23:40:57 +00:00
|
|
|
fn parse_on_off(param: &str) -> Result<bool> {
|
|
|
|
if !param.is_empty() {
|
|
|
|
let res = match param {
|
2019-10-02 21:01:36 +00:00
|
|
|
"on" => true,
|
|
|
|
"off" => false,
|
2019-11-19 23:40:57 +00:00
|
|
|
_ => return Err(Error::ParseOnOff),
|
2019-10-02 21:01:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
} else {
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 10:56:37 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
2019-10-16 05:38:42 +00:00
|
|
|
pub struct CpusConfig {
|
2019-11-25 14:18:03 +00:00
|
|
|
pub boot_vcpus: u8,
|
|
|
|
pub max_vcpus: u8,
|
2019-10-16 05:38:42 +00:00
|
|
|
}
|
2019-05-23 19:48:05 +00:00
|
|
|
|
|
|
|
impl CpusConfig {
|
|
|
|
pub fn parse(cpus: &str) -> Result<Self> {
|
2020-03-27 11:51:09 +00:00
|
|
|
let mut parser = OptionParser::new();
|
|
|
|
parser.add("boot").add("max");
|
|
|
|
parser.parse(cpus).map_err(Error::ParseCpus)?;
|
|
|
|
|
|
|
|
let boot_vcpus: u8 = parser
|
|
|
|
.convert("boot")
|
|
|
|
.map_err(Error::ParseCpus)?
|
|
|
|
.unwrap_or(DEFAULT_VCPUS);
|
|
|
|
let max_vcpus: u8 = parser
|
|
|
|
.convert("max")
|
|
|
|
.map_err(Error::ParseCpus)?
|
|
|
|
.unwrap_or(boot_vcpus);
|
2020-02-21 16:08:25 +00:00
|
|
|
|
|
|
|
if max_vcpus < boot_vcpus {
|
|
|
|
return Err(Error::ParseCpusMaxLowerThanBoot);
|
2019-11-25 14:18:03 +00:00
|
|
|
}
|
2020-02-21 16:08:25 +00:00
|
|
|
|
|
|
|
Ok(CpusConfig {
|
|
|
|
boot_vcpus,
|
|
|
|
max_vcpus,
|
|
|
|
})
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 09:40:50 +00:00
|
|
|
impl Default for CpusConfig {
|
|
|
|
fn default() -> Self {
|
2019-10-16 05:38:42 +00:00
|
|
|
CpusConfig {
|
2019-11-25 14:18:03 +00:00
|
|
|
boot_vcpus: DEFAULT_VCPUS,
|
|
|
|
max_vcpus: DEFAULT_VCPUS,
|
2019-10-16 05:38:42 +00:00
|
|
|
}
|
2019-09-27 09:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 10:56:37 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
2019-09-23 16:51:36 +00:00
|
|
|
pub struct MemoryConfig {
|
2019-05-24 19:21:23 +00:00
|
|
|
pub size: u64,
|
2019-12-10 13:43:26 +00:00
|
|
|
#[serde(default)]
|
2019-09-23 16:51:36 +00:00
|
|
|
pub file: Option<PathBuf>,
|
2019-11-19 23:40:57 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub mergeable: bool,
|
2020-01-07 11:25:55 +00:00
|
|
|
#[serde(default)]
|
2020-03-04 02:16:07 +00:00
|
|
|
pub hotplug_method: HotplugMethod,
|
|
|
|
#[serde(default)]
|
2020-01-07 11:25:55 +00:00
|
|
|
pub hotplug_size: Option<u64>,
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 16:51:36 +00:00
|
|
|
impl MemoryConfig {
|
|
|
|
pub fn parse(memory: &str) -> Result<Self> {
|
2019-05-24 19:21:23 +00:00
|
|
|
// Split the parameters based on the comma delimiter
|
|
|
|
let params_list: Vec<&str> = memory.split(',').collect();
|
|
|
|
|
2020-03-27 12:54:48 +00:00
|
|
|
let mut size_str: &str = "512M";
|
2019-05-24 19:21:23 +00:00
|
|
|
let mut file_str: &str = "";
|
2019-11-19 23:40:57 +00:00
|
|
|
let mut mergeable_str: &str = "";
|
2019-05-24 19:21:23 +00:00
|
|
|
let mut backed = false;
|
2020-03-04 02:16:07 +00:00
|
|
|
let mut hotplug_method_str: &str = "acpi";
|
2020-01-07 11:25:55 +00:00
|
|
|
let mut hotplug_str: &str = "";
|
2019-05-24 19:21:23 +00:00
|
|
|
|
|
|
|
for param in params_list.iter() {
|
|
|
|
if param.starts_with("size=") {
|
|
|
|
size_str = ¶m[5..];
|
|
|
|
} else if param.starts_with("file=") {
|
|
|
|
backed = true;
|
|
|
|
file_str = ¶m[5..];
|
2019-11-19 23:40:57 +00:00
|
|
|
} else if param.starts_with("mergeable=") {
|
|
|
|
mergeable_str = ¶m[10..];
|
2020-03-04 02:16:07 +00:00
|
|
|
} else if param.starts_with("hotplug_method=") {
|
|
|
|
hotplug_method_str = ¶m[15..];
|
2020-01-07 11:25:55 +00:00
|
|
|
} else if param.starts_with("hotplug_size=") {
|
|
|
|
hotplug_str = ¶m[13..]
|
2019-05-24 19:21:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let file = if backed {
|
|
|
|
if file_str.is_empty() {
|
|
|
|
return Err(Error::ParseMemoryFileParam);
|
|
|
|
}
|
|
|
|
|
2019-09-23 16:51:36 +00:00
|
|
|
Some(PathBuf::from(file_str))
|
2019-05-24 19:21:23 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-03-27 12:34:05 +00:00
|
|
|
let hotplug_method = hotplug_method_str[..]
|
|
|
|
.parse()
|
|
|
|
.map_err(Error::ParseMemoryHotplugMethodParam)?;
|
2020-03-04 02:16:07 +00:00
|
|
|
|
2019-05-24 19:21:23 +00:00
|
|
|
Ok(MemoryConfig {
|
2019-07-09 03:13:02 +00:00
|
|
|
size: parse_size(size_str)?,
|
2019-05-24 19:21:23 +00:00
|
|
|
file,
|
2019-11-19 23:40:57 +00:00
|
|
|
mergeable: parse_on_off(mergeable_str)?,
|
2020-03-04 02:16:07 +00:00
|
|
|
hotplug_method,
|
2020-01-07 11:25:55 +00:00
|
|
|
hotplug_size: if hotplug_str == "" {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(parse_size(hotplug_str)?)
|
|
|
|
},
|
2019-05-24 19:21:23 +00:00
|
|
|
})
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 09:40:50 +00:00
|
|
|
impl Default for MemoryConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
MemoryConfig {
|
|
|
|
size: DEFAULT_MEMORY_MB << 20,
|
|
|
|
file: None,
|
2019-11-19 23:40:57 +00:00
|
|
|
mergeable: false,
|
2020-03-04 02:16:07 +00:00
|
|
|
hotplug_method: HotplugMethod::Acpi,
|
2020-01-07 11:25:55 +00:00
|
|
|
hotplug_size: None,
|
2019-09-27 09:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 10:56:37 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
2019-09-23 16:54:00 +00:00
|
|
|
pub struct KernelConfig {
|
|
|
|
pub path: PathBuf,
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 17:51:17 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
|
|
|
pub struct InitramfsConfig {
|
|
|
|
pub path: PathBuf,
|
|
|
|
}
|
|
|
|
|
2019-12-13 10:56:37 +00:00
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
|
2019-05-23 19:48:05 +00:00
|
|
|
pub struct CmdlineConfig {
|
2019-09-27 08:39:56 +00:00
|
|
|
pub args: String,
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CmdlineConfig {
|
|
|
|
pub fn parse(cmdline: Option<&str>) -> Result<Self> {
|
2019-09-27 08:39:56 +00:00
|
|
|
let args = cmdline
|
2019-05-23 19:48:05 +00:00
|
|
|
.map(std::string::ToString::to_string)
|
|
|
|
.unwrap_or_else(String::new);
|
|
|
|
|
2019-09-27 16:06:53 +00:00
|
|
|
Ok(CmdlineConfig { args })
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 10:56:37 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
2019-09-23 17:12:19 +00:00
|
|
|
pub struct DiskConfig {
|
2020-03-13 10:25:17 +00:00
|
|
|
pub path: Option<PathBuf>,
|
2019-10-02 21:01:36 +00:00
|
|
|
#[serde(default)]
|
2020-01-21 11:22:09 +00:00
|
|
|
pub readonly: bool,
|
|
|
|
#[serde(default)]
|
2020-01-21 11:36:27 +00:00
|
|
|
pub direct: bool,
|
|
|
|
#[serde(default)]
|
2019-10-02 21:01:36 +00:00
|
|
|
pub iommu: bool,
|
2020-01-24 15:31:13 +00:00
|
|
|
#[serde(default = "default_diskconfig_num_queues")]
|
|
|
|
pub num_queues: usize,
|
|
|
|
#[serde(default = "default_diskconfig_queue_size")]
|
|
|
|
pub queue_size: u16,
|
2020-01-28 11:43:15 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub vhost_user: bool,
|
|
|
|
pub vhost_socket: Option<String>,
|
|
|
|
#[serde(default = "default_diskconfig_wce")]
|
|
|
|
pub wce: bool,
|
2020-02-20 15:49:16 +00:00
|
|
|
#[serde(default = "default_diskconfig_poll_queue")]
|
|
|
|
pub poll_queue: bool,
|
2020-01-24 15:31:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn default_diskconfig_num_queues() -> usize {
|
|
|
|
DEFAULT_NUM_QUEUES_VUBLK
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_diskconfig_queue_size() -> u16 {
|
|
|
|
DEFAULT_QUEUE_SIZE_VUBLK
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 11:43:15 +00:00
|
|
|
fn default_diskconfig_wce() -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2020-02-20 15:49:16 +00:00
|
|
|
fn default_diskconfig_poll_queue() -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:12:19 +00:00
|
|
|
impl DiskConfig {
|
2020-03-12 16:31:26 +00:00
|
|
|
pub const SYNTAX: &'static str = "Disk parameters \
|
|
|
|
\"path=<disk_image_path>,readonly=on|off,iommu=on|off,num_queues=<number_of_queues>,\
|
|
|
|
queue_size=<size_of_each_queue>,vhost_user=<vhost_user_enable>,\
|
|
|
|
socket=<vhost_user_socket_path>,wce=<true|false, default true>\"";
|
|
|
|
|
2019-09-23 17:12:19 +00:00
|
|
|
pub fn parse(disk: &str) -> Result<Self> {
|
2019-10-02 21:01:36 +00:00
|
|
|
// Split the parameters based on the comma delimiter
|
|
|
|
let params_list: Vec<&str> = disk.split(',').collect();
|
|
|
|
|
|
|
|
let mut path_str: &str = "";
|
2020-01-21 11:22:09 +00:00
|
|
|
let mut readonly_str: &str = "";
|
2020-01-21 11:36:27 +00:00
|
|
|
let mut direct_str: &str = "";
|
2019-10-02 21:01:36 +00:00
|
|
|
let mut iommu_str: &str = "";
|
2020-01-24 15:31:13 +00:00
|
|
|
let mut num_queues_str: &str = "";
|
|
|
|
let mut queue_size_str: &str = "";
|
2020-01-28 11:43:15 +00:00
|
|
|
let mut vhost_socket_str: &str = "";
|
|
|
|
let mut vhost_user_str: &str = "";
|
|
|
|
let mut wce_str: &str = "";
|
2020-02-20 15:49:16 +00:00
|
|
|
let mut poll_queue_str: &str = "";
|
2019-10-02 21:01:36 +00:00
|
|
|
|
|
|
|
for param in params_list.iter() {
|
|
|
|
if param.starts_with("path=") {
|
|
|
|
path_str = ¶m[5..];
|
2020-01-21 11:22:09 +00:00
|
|
|
} else if param.starts_with("readonly=") {
|
|
|
|
readonly_str = ¶m[9..];
|
2020-01-21 11:36:27 +00:00
|
|
|
} else if param.starts_with("direct=") {
|
|
|
|
direct_str = ¶m[7..];
|
2019-10-02 21:01:36 +00:00
|
|
|
} else if param.starts_with("iommu=") {
|
|
|
|
iommu_str = ¶m[6..];
|
2020-01-24 15:31:13 +00:00
|
|
|
} else if param.starts_with("num_queues=") {
|
|
|
|
num_queues_str = ¶m[11..];
|
|
|
|
} else if param.starts_with("queue_size=") {
|
|
|
|
queue_size_str = ¶m[11..];
|
2020-01-28 11:43:15 +00:00
|
|
|
} else if param.starts_with("vhost_user=") {
|
|
|
|
vhost_user_str = ¶m[11..];
|
|
|
|
} else if param.starts_with("socket=") {
|
|
|
|
vhost_socket_str = ¶m[7..];
|
|
|
|
} else if param.starts_with("wce=") {
|
|
|
|
wce_str = ¶m[4..];
|
2020-02-20 15:49:16 +00:00
|
|
|
} else if param.starts_with("poll_queue=") {
|
|
|
|
poll_queue_str = ¶m[11..];
|
2019-10-02 21:01:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:31:13 +00:00
|
|
|
let mut num_queues: usize = default_diskconfig_num_queues();
|
|
|
|
let mut queue_size: u16 = default_diskconfig_queue_size();
|
2020-01-28 11:43:15 +00:00
|
|
|
let mut vhost_user = false;
|
|
|
|
let mut vhost_socket = None;
|
|
|
|
let mut wce: bool = default_diskconfig_wce();
|
2020-02-20 15:49:16 +00:00
|
|
|
let mut poll_queue: bool = default_diskconfig_poll_queue();
|
2020-03-13 10:25:17 +00:00
|
|
|
let mut path = None;
|
2020-01-24 15:31:13 +00:00
|
|
|
|
|
|
|
if !num_queues_str.is_empty() {
|
|
|
|
num_queues = num_queues_str
|
|
|
|
.parse()
|
|
|
|
.map_err(Error::ParseDiskNumQueuesParam)?;
|
|
|
|
}
|
|
|
|
if !queue_size_str.is_empty() {
|
|
|
|
queue_size = queue_size_str
|
|
|
|
.parse()
|
|
|
|
.map_err(Error::ParseDiskQueueSizeParam)?;
|
|
|
|
}
|
2020-01-28 11:43:15 +00:00
|
|
|
if !vhost_user_str.is_empty() {
|
|
|
|
vhost_user = vhost_user_str.parse().map_err(Error::ParseDiskVhostParam)?;
|
|
|
|
}
|
|
|
|
if !vhost_socket_str.is_empty() {
|
|
|
|
vhost_socket = Some(vhost_socket_str.to_owned());
|
|
|
|
}
|
|
|
|
if !wce_str.is_empty() {
|
2020-01-28 12:04:11 +00:00
|
|
|
if !vhost_user {
|
|
|
|
warn!("wce parameter currently only has effect when used vhost_user=true");
|
|
|
|
}
|
2020-01-28 11:43:15 +00:00
|
|
|
wce = wce_str.parse().map_err(Error::ParseDiskWceParam)?;
|
|
|
|
}
|
2020-02-20 15:49:16 +00:00
|
|
|
if !poll_queue_str.is_empty() {
|
|
|
|
if !vhost_user {
|
|
|
|
warn!("poll_queue parameter currently only has effect when used vhost_user=true");
|
|
|
|
}
|
|
|
|
poll_queue = poll_queue_str
|
|
|
|
.parse()
|
|
|
|
.map_err(Error::ParseDiskPollQueueParam)?;
|
|
|
|
}
|
2020-03-13 10:25:17 +00:00
|
|
|
if !path_str.is_empty() {
|
|
|
|
path = Some(PathBuf::from(path_str))
|
|
|
|
}
|
2020-01-28 11:43:15 +00:00
|
|
|
|
2020-03-13 10:28:39 +00:00
|
|
|
if vhost_socket.as_ref().and(path.as_ref()).is_some() {
|
|
|
|
return Err(Error::ParseDiskSocketAndPath);
|
|
|
|
}
|
|
|
|
|
2019-05-23 19:48:05 +00:00
|
|
|
Ok(DiskConfig {
|
2020-03-13 10:25:17 +00:00
|
|
|
path,
|
2020-01-21 11:22:09 +00:00
|
|
|
readonly: parse_on_off(readonly_str)?,
|
2020-01-21 11:36:27 +00:00
|
|
|
direct: parse_on_off(direct_str)?,
|
2019-11-19 23:40:57 +00:00
|
|
|
iommu: parse_on_off(iommu_str)?,
|
2020-01-24 15:31:13 +00:00
|
|
|
num_queues,
|
|
|
|
queue_size,
|
2020-01-28 11:43:15 +00:00
|
|
|
vhost_socket,
|
|
|
|
vhost_user,
|
|
|
|
wce,
|
2020-02-20 15:49:16 +00:00
|
|
|
poll_queue,
|
2019-05-23 19:48:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 10:56:37 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
2019-09-23 17:14:35 +00:00
|
|
|
pub struct NetConfig {
|
2019-12-09 17:45:00 +00:00
|
|
|
#[serde(default = "default_netconfig_tap")]
|
2019-09-23 17:14:35 +00:00
|
|
|
pub tap: Option<String>,
|
2019-12-09 17:45:00 +00:00
|
|
|
#[serde(default = "default_netconfig_ip")]
|
2019-05-23 19:48:05 +00:00
|
|
|
pub ip: Ipv4Addr,
|
2019-12-09 17:45:00 +00:00
|
|
|
#[serde(default = "default_netconfig_mask")]
|
2019-05-23 19:48:05 +00:00
|
|
|
pub mask: Ipv4Addr,
|
2019-12-09 17:45:00 +00:00
|
|
|
#[serde(default = "default_netconfig_mac")]
|
2019-05-23 19:48:05 +00:00
|
|
|
pub mac: MacAddr,
|
2019-10-02 21:13:44 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub iommu: bool,
|
2019-11-12 09:14:54 +00:00
|
|
|
#[serde(default = "default_netconfig_num_queues")]
|
|
|
|
pub num_queues: usize,
|
|
|
|
#[serde(default = "default_netconfig_queue_size")]
|
|
|
|
pub queue_size: u16,
|
2020-01-27 15:14:07 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub vhost_user: bool,
|
|
|
|
pub vhost_socket: Option<String>,
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 17:45:00 +00:00
|
|
|
fn default_netconfig_tap() -> Option<String> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_netconfig_ip() -> Ipv4Addr {
|
|
|
|
Ipv4Addr::new(192, 168, 249, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_netconfig_mask() -> Ipv4Addr {
|
|
|
|
Ipv4Addr::new(255, 255, 255, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_netconfig_mac() -> MacAddr {
|
|
|
|
MacAddr::local_random()
|
|
|
|
}
|
|
|
|
|
2019-11-12 09:14:54 +00:00
|
|
|
fn default_netconfig_num_queues() -> usize {
|
|
|
|
DEFAULT_NUM_QUEUES_VUNET
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_netconfig_queue_size() -> u16 {
|
|
|
|
DEFAULT_QUEUE_SIZE_VUNET
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:14:35 +00:00
|
|
|
impl NetConfig {
|
2020-03-12 16:31:26 +00:00
|
|
|
pub const SYNTAX: &'static str = "Network parameters \
|
|
|
|
\"tap=<if_name>,ip=<ip_addr>,mask=<net_mask>,mac=<mac_addr>,iommu=on|off,\
|
|
|
|
num_queues=<number_of_queues>,queue_size=<size_of_each_queue>,\
|
|
|
|
vhost_user=<vhost_user_enable>,socket=<vhost_user_socket_path>\"";
|
|
|
|
|
2019-09-23 17:14:35 +00:00
|
|
|
pub fn parse(net: &str) -> Result<Self> {
|
2019-05-23 19:48:05 +00:00
|
|
|
// Split the parameters based on the comma delimiter
|
2019-07-08 22:31:13 +00:00
|
|
|
let params_list: Vec<&str> = net.split(',').collect();
|
2019-05-23 19:48:05 +00:00
|
|
|
|
|
|
|
let mut tap_str: &str = "";
|
|
|
|
let mut ip_str: &str = "";
|
|
|
|
let mut mask_str: &str = "";
|
|
|
|
let mut mac_str: &str = "";
|
2019-10-02 21:13:44 +00:00
|
|
|
let mut iommu_str: &str = "";
|
2019-11-12 09:14:54 +00:00
|
|
|
let mut num_queues_str: &str = "";
|
|
|
|
let mut queue_size_str: &str = "";
|
2020-01-27 15:14:07 +00:00
|
|
|
let mut vhost_socket_str: &str = "";
|
|
|
|
let mut vhost_user_str: &str = "";
|
2019-05-23 19:48:05 +00:00
|
|
|
|
|
|
|
for param in params_list.iter() {
|
|
|
|
if param.starts_with("tap=") {
|
|
|
|
tap_str = ¶m[4..];
|
|
|
|
} else if param.starts_with("ip=") {
|
|
|
|
ip_str = ¶m[3..];
|
|
|
|
} else if param.starts_with("mask=") {
|
|
|
|
mask_str = ¶m[5..];
|
|
|
|
} else if param.starts_with("mac=") {
|
|
|
|
mac_str = ¶m[4..];
|
2019-10-02 21:13:44 +00:00
|
|
|
} else if param.starts_with("iommu=") {
|
|
|
|
iommu_str = ¶m[6..];
|
2019-11-12 09:14:54 +00:00
|
|
|
} else if param.starts_with("num_queues=") {
|
|
|
|
num_queues_str = ¶m[11..];
|
|
|
|
} else if param.starts_with("queue_size=") {
|
|
|
|
queue_size_str = ¶m[11..];
|
2020-01-27 15:14:07 +00:00
|
|
|
} else if param.starts_with("vhost_user=") {
|
|
|
|
vhost_user_str = ¶m[11..];
|
|
|
|
} else if param.starts_with("socket=") {
|
|
|
|
vhost_socket_str = ¶m[7..];
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 17:45:00 +00:00
|
|
|
let mut tap: Option<String> = default_netconfig_tap();
|
|
|
|
let mut ip: Ipv4Addr = default_netconfig_ip();
|
|
|
|
let mut mask: Ipv4Addr = default_netconfig_mask();
|
|
|
|
let mut mac: MacAddr = default_netconfig_mac();
|
2019-11-19 23:40:57 +00:00
|
|
|
let iommu = parse_on_off(iommu_str)?;
|
2019-11-12 09:14:54 +00:00
|
|
|
let mut num_queues: usize = default_netconfig_num_queues();
|
|
|
|
let mut queue_size: u16 = default_netconfig_queue_size();
|
2020-01-27 15:14:07 +00:00
|
|
|
let mut vhost_user = false;
|
|
|
|
let mut vhost_socket = None;
|
2019-05-23 19:48:05 +00:00
|
|
|
|
|
|
|
if !tap_str.is_empty() {
|
2019-09-23 17:14:35 +00:00
|
|
|
tap = Some(tap_str.to_string());
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
if !ip_str.is_empty() {
|
|
|
|
ip = ip_str.parse().map_err(Error::ParseNetIpParam)?;
|
|
|
|
}
|
|
|
|
if !mask_str.is_empty() {
|
|
|
|
mask = mask_str.parse().map_err(Error::ParseNetMaskParam)?;
|
|
|
|
}
|
|
|
|
if !mac_str.is_empty() {
|
|
|
|
mac = MacAddr::parse_str(mac_str).map_err(Error::ParseNetMacParam)?;
|
|
|
|
}
|
2019-11-12 09:14:54 +00:00
|
|
|
if !num_queues_str.is_empty() {
|
|
|
|
num_queues = num_queues_str
|
|
|
|
.parse()
|
|
|
|
.map_err(Error::ParseNetNumQueuesParam)?;
|
|
|
|
}
|
|
|
|
if !queue_size_str.is_empty() {
|
|
|
|
queue_size = queue_size_str
|
|
|
|
.parse()
|
|
|
|
.map_err(Error::ParseNetQueueSizeParam)?;
|
|
|
|
}
|
2020-01-27 15:14:07 +00:00
|
|
|
if !vhost_user_str.is_empty() {
|
|
|
|
vhost_user = vhost_user_str.parse().map_err(Error::ParseNetVhostParam)?;
|
|
|
|
}
|
|
|
|
if !vhost_socket_str.is_empty() {
|
|
|
|
vhost_socket = Some(vhost_socket_str.to_owned());
|
|
|
|
}
|
|
|
|
|
2019-10-02 21:13:44 +00:00
|
|
|
Ok(NetConfig {
|
|
|
|
tap,
|
|
|
|
ip,
|
|
|
|
mask,
|
|
|
|
mac,
|
|
|
|
iommu,
|
2019-11-12 09:14:54 +00:00
|
|
|
num_queues,
|
|
|
|
queue_size,
|
2020-01-27 15:14:07 +00:00
|
|
|
vhost_user,
|
|
|
|
vhost_socket,
|
2019-10-02 21:13:44 +00:00
|
|
|
})
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 10:56:37 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
2019-09-23 17:15:50 +00:00
|
|
|
pub struct RngConfig {
|
|
|
|
pub src: PathBuf,
|
2019-10-04 18:18:49 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub iommu: bool,
|
2019-05-22 20:06:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 17:15:50 +00:00
|
|
|
impl RngConfig {
|
|
|
|
pub fn parse(rng: &str) -> Result<Self> {
|
2019-10-04 18:18:49 +00:00
|
|
|
// Split the parameters based on the comma delimiter
|
|
|
|
let params_list: Vec<&str> = rng.split(',').collect();
|
|
|
|
|
|
|
|
let mut src_str: &str = "";
|
|
|
|
let mut iommu_str: &str = "";
|
|
|
|
|
|
|
|
for param in params_list.iter() {
|
|
|
|
if param.starts_with("src=") {
|
|
|
|
src_str = ¶m[4..];
|
|
|
|
} else if param.starts_with("iommu=") {
|
|
|
|
iommu_str = ¶m[6..];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 20:06:49 +00:00
|
|
|
Ok(RngConfig {
|
2019-10-04 18:18:49 +00:00
|
|
|
src: PathBuf::from(src_str),
|
2019-11-19 23:40:57 +00:00
|
|
|
iommu: parse_on_off(iommu_str)?,
|
2019-05-22 20:06:49 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 09:40:50 +00:00
|
|
|
impl Default for RngConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
RngConfig {
|
|
|
|
src: PathBuf::from(DEFAULT_RNG_SOURCE),
|
2019-10-04 18:18:49 +00:00
|
|
|
iommu: false,
|
2019-09-27 09:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 10:56:37 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
2019-09-23 17:23:17 +00:00
|
|
|
pub struct FsConfig {
|
|
|
|
pub tag: String,
|
|
|
|
pub sock: PathBuf,
|
2019-12-09 17:22:51 +00:00
|
|
|
#[serde(default = "default_fsconfig_num_queues")]
|
2019-05-22 20:06:49 +00:00
|
|
|
pub num_queues: usize,
|
2019-12-09 17:22:51 +00:00
|
|
|
#[serde(default = "default_fsconfig_queue_size")]
|
2019-05-22 20:06:49 +00:00
|
|
|
pub queue_size: u16,
|
vmm: api: Adjust FsConfig for OpenAPI
The FsConfig structure has been recently adjusted so that the default
value matches between OpenAPI and CLI. Unfortunately, with the current
description, there is no way from the OpenAPI to describe a cache_size
value "None", so that DAX does not get enabled. Usually, using a Rust
"Option" works because the default value is None. But in this case, the
default value is Some(8G), which means we cannot describe a None.
This commit tackles the problem, introducing an explicit parameter
"dax", and leaving "cache_size" as a simple u64 integer.
This way, the default value is dax=true and cache_size=8G, but it lets
the opportunity to disable DAX entirely with dax=false, which will
simply ignore the cache_size value.
Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
2019-12-11 09:44:26 +00:00
|
|
|
#[serde(default = "default_fsconfig_dax")]
|
|
|
|
pub dax: bool,
|
2019-12-09 17:22:51 +00:00
|
|
|
#[serde(default = "default_fsconfig_cache_size")]
|
vmm: api: Adjust FsConfig for OpenAPI
The FsConfig structure has been recently adjusted so that the default
value matches between OpenAPI and CLI. Unfortunately, with the current
description, there is no way from the OpenAPI to describe a cache_size
value "None", so that DAX does not get enabled. Usually, using a Rust
"Option" works because the default value is None. But in this case, the
default value is Some(8G), which means we cannot describe a None.
This commit tackles the problem, introducing an explicit parameter
"dax", and leaving "cache_size" as a simple u64 integer.
This way, the default value is dax=true and cache_size=8G, but it lets
the opportunity to disable DAX entirely with dax=false, which will
simply ignore the cache_size value.
Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
2019-12-11 09:44:26 +00:00
|
|
|
pub cache_size: u64,
|
2019-05-22 20:06:49 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 17:22:51 +00:00
|
|
|
fn default_fsconfig_num_queues() -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_fsconfig_queue_size() -> u16 {
|
|
|
|
1024
|
|
|
|
}
|
|
|
|
|
vmm: api: Adjust FsConfig for OpenAPI
The FsConfig structure has been recently adjusted so that the default
value matches between OpenAPI and CLI. Unfortunately, with the current
description, there is no way from the OpenAPI to describe a cache_size
value "None", so that DAX does not get enabled. Usually, using a Rust
"Option" works because the default value is None. But in this case, the
default value is Some(8G), which means we cannot describe a None.
This commit tackles the problem, introducing an explicit parameter
"dax", and leaving "cache_size" as a simple u64 integer.
This way, the default value is dax=true and cache_size=8G, but it lets
the opportunity to disable DAX entirely with dax=false, which will
simply ignore the cache_size value.
Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
2019-12-11 09:44:26 +00:00
|
|
|
fn default_fsconfig_dax() -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_fsconfig_cache_size() -> u64 {
|
|
|
|
0x0002_0000_0000
|
2019-12-09 17:22:51 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 17:23:17 +00:00
|
|
|
impl FsConfig {
|
|
|
|
pub fn parse(fs: &str) -> Result<Self> {
|
2019-05-22 20:06:49 +00:00
|
|
|
// Split the parameters based on the comma delimiter
|
2019-06-27 16:14:11 +00:00
|
|
|
let params_list: Vec<&str> = fs.split(',').collect();
|
2019-05-22 20:06:49 +00:00
|
|
|
|
|
|
|
let mut tag: &str = "";
|
|
|
|
let mut sock: &str = "";
|
|
|
|
let mut num_queues_str: &str = "";
|
|
|
|
let mut queue_size_str: &str = "";
|
2019-08-05 19:45:58 +00:00
|
|
|
let mut dax_str: &str = "";
|
|
|
|
let mut cache_size_str: &str = "";
|
2019-05-22 20:06:49 +00:00
|
|
|
|
|
|
|
for param in params_list.iter() {
|
|
|
|
if param.starts_with("tag=") {
|
|
|
|
tag = ¶m[4..];
|
|
|
|
} else if param.starts_with("sock=") {
|
|
|
|
sock = ¶m[5..];
|
|
|
|
} else if param.starts_with("num_queues=") {
|
|
|
|
num_queues_str = ¶m[11..];
|
|
|
|
} else if param.starts_with("queue_size=") {
|
|
|
|
queue_size_str = ¶m[11..];
|
2019-08-05 19:45:58 +00:00
|
|
|
} else if param.starts_with("dax=") {
|
|
|
|
dax_str = ¶m[4..];
|
|
|
|
} else if param.starts_with("cache_size=") {
|
|
|
|
cache_size_str = ¶m[11..];
|
2019-05-22 20:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 17:22:51 +00:00
|
|
|
let mut num_queues: usize = default_fsconfig_num_queues();
|
|
|
|
let mut queue_size: u16 = default_fsconfig_queue_size();
|
vmm: api: Adjust FsConfig for OpenAPI
The FsConfig structure has been recently adjusted so that the default
value matches between OpenAPI and CLI. Unfortunately, with the current
description, there is no way from the OpenAPI to describe a cache_size
value "None", so that DAX does not get enabled. Usually, using a Rust
"Option" works because the default value is None. But in this case, the
default value is Some(8G), which means we cannot describe a None.
This commit tackles the problem, introducing an explicit parameter
"dax", and leaving "cache_size" as a simple u64 integer.
This way, the default value is dax=true and cache_size=8G, but it lets
the opportunity to disable DAX entirely with dax=false, which will
simply ignore the cache_size value.
Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
2019-12-11 09:44:26 +00:00
|
|
|
let mut dax: bool = default_fsconfig_dax();
|
2019-08-05 19:45:58 +00:00
|
|
|
// Default cache size set to 8Gib.
|
vmm: api: Adjust FsConfig for OpenAPI
The FsConfig structure has been recently adjusted so that the default
value matches between OpenAPI and CLI. Unfortunately, with the current
description, there is no way from the OpenAPI to describe a cache_size
value "None", so that DAX does not get enabled. Usually, using a Rust
"Option" works because the default value is None. But in this case, the
default value is Some(8G), which means we cannot describe a None.
This commit tackles the problem, introducing an explicit parameter
"dax", and leaving "cache_size" as a simple u64 integer.
This way, the default value is dax=true and cache_size=8G, but it lets
the opportunity to disable DAX entirely with dax=false, which will
simply ignore the cache_size value.
Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
2019-12-11 09:44:26 +00:00
|
|
|
let mut cache_size: u64 = default_fsconfig_cache_size();
|
2019-05-22 20:06:49 +00:00
|
|
|
|
|
|
|
if tag.is_empty() {
|
|
|
|
return Err(Error::ParseFsTagParam);
|
|
|
|
}
|
|
|
|
if sock.is_empty() {
|
|
|
|
return Err(Error::ParseFsSockParam);
|
|
|
|
}
|
|
|
|
if !num_queues_str.is_empty() {
|
|
|
|
num_queues = num_queues_str
|
|
|
|
.parse()
|
|
|
|
.map_err(Error::ParseFsNumQueuesParam)?;
|
|
|
|
}
|
|
|
|
if !queue_size_str.is_empty() {
|
|
|
|
queue_size = queue_size_str
|
|
|
|
.parse()
|
|
|
|
.map_err(Error::ParseFsQueueSizeParam)?;
|
|
|
|
}
|
2019-08-05 19:45:58 +00:00
|
|
|
if !dax_str.is_empty() {
|
|
|
|
match dax_str {
|
|
|
|
"on" => dax = true,
|
|
|
|
"off" => dax = false,
|
|
|
|
_ => return Err(Error::ParseFsDax),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take appropriate decision about cache_size based on DAX being
|
|
|
|
// enabled or disabled.
|
|
|
|
if !dax {
|
|
|
|
if !cache_size_str.is_empty() {
|
|
|
|
return Err(Error::InvalidCacheSizeWithDaxOff);
|
|
|
|
}
|
vmm: api: Adjust FsConfig for OpenAPI
The FsConfig structure has been recently adjusted so that the default
value matches between OpenAPI and CLI. Unfortunately, with the current
description, there is no way from the OpenAPI to describe a cache_size
value "None", so that DAX does not get enabled. Usually, using a Rust
"Option" works because the default value is None. But in this case, the
default value is Some(8G), which means we cannot describe a None.
This commit tackles the problem, introducing an explicit parameter
"dax", and leaving "cache_size" as a simple u64 integer.
This way, the default value is dax=true and cache_size=8G, but it lets
the opportunity to disable DAX entirely with dax=false, which will
simply ignore the cache_size value.
Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
2019-12-11 09:44:26 +00:00
|
|
|
cache_size = 0;
|
2019-08-05 19:45:58 +00:00
|
|
|
} else if !cache_size_str.is_empty() {
|
vmm: api: Adjust FsConfig for OpenAPI
The FsConfig structure has been recently adjusted so that the default
value matches between OpenAPI and CLI. Unfortunately, with the current
description, there is no way from the OpenAPI to describe a cache_size
value "None", so that DAX does not get enabled. Usually, using a Rust
"Option" works because the default value is None. But in this case, the
default value is Some(8G), which means we cannot describe a None.
This commit tackles the problem, introducing an explicit parameter
"dax", and leaving "cache_size" as a simple u64 integer.
This way, the default value is dax=true and cache_size=8G, but it lets
the opportunity to disable DAX entirely with dax=false, which will
simply ignore the cache_size value.
Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
2019-12-11 09:44:26 +00:00
|
|
|
cache_size = parse_size(cache_size_str)?;
|
2019-08-05 19:45:58 +00:00
|
|
|
}
|
2019-05-22 20:06:49 +00:00
|
|
|
|
2019-06-27 16:14:11 +00:00
|
|
|
Ok(FsConfig {
|
2019-09-23 17:23:17 +00:00
|
|
|
tag: tag.to_string(),
|
|
|
|
sock: PathBuf::from(sock),
|
2019-05-22 20:06:49 +00:00
|
|
|
num_queues,
|
|
|
|
queue_size,
|
vmm: api: Adjust FsConfig for OpenAPI
The FsConfig structure has been recently adjusted so that the default
value matches between OpenAPI and CLI. Unfortunately, with the current
description, there is no way from the OpenAPI to describe a cache_size
value "None", so that DAX does not get enabled. Usually, using a Rust
"Option" works because the default value is None. But in this case, the
default value is Some(8G), which means we cannot describe a None.
This commit tackles the problem, introducing an explicit parameter
"dax", and leaving "cache_size" as a simple u64 integer.
This way, the default value is dax=true and cache_size=8G, but it lets
the opportunity to disable DAX entirely with dax=false, which will
simply ignore the cache_size value.
Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
2019-12-11 09:44:26 +00:00
|
|
|
dax,
|
2019-08-05 19:45:58 +00:00
|
|
|
cache_size,
|
2019-06-27 16:14:11 +00:00
|
|
|
})
|
2019-05-22 20:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 10:56:37 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
2019-09-23 17:27:56 +00:00
|
|
|
pub struct PmemConfig {
|
|
|
|
pub file: PathBuf,
|
2019-06-19 20:48:37 +00:00
|
|
|
pub size: u64,
|
2019-10-04 18:27:22 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub iommu: bool,
|
2019-11-19 23:47:31 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub mergeable: bool,
|
2020-03-19 10:13:53 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub discard_writes: bool,
|
2019-06-19 20:48:37 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 17:27:56 +00:00
|
|
|
impl PmemConfig {
|
2020-03-12 16:31:26 +00:00
|
|
|
pub const SYNTAX: &'static str = "Persistent memory parameters \
|
|
|
|
\"file=<backing_file_path>,size=<persistent_memory_size>,iommu=on|off,\
|
|
|
|
mergeable=on|off,discard_writes=on|off,\"";
|
2019-09-23 17:27:56 +00:00
|
|
|
pub fn parse(pmem: &str) -> Result<Self> {
|
2019-06-19 20:48:37 +00:00
|
|
|
// Split the parameters based on the comma delimiter
|
2019-06-28 08:45:58 +00:00
|
|
|
let params_list: Vec<&str> = pmem.split(',').collect();
|
2019-06-19 20:48:37 +00:00
|
|
|
|
|
|
|
let mut file_str: &str = "";
|
|
|
|
let mut size_str: &str = "";
|
2019-10-04 18:27:22 +00:00
|
|
|
let mut iommu_str: &str = "";
|
2019-11-19 23:47:31 +00:00
|
|
|
let mut mergeable_str: &str = "";
|
2020-03-19 10:13:53 +00:00
|
|
|
let mut discard_writes_str: &str = "";
|
2019-06-19 20:48:37 +00:00
|
|
|
|
|
|
|
for param in params_list.iter() {
|
|
|
|
if param.starts_with("file=") {
|
|
|
|
file_str = ¶m[5..];
|
|
|
|
} else if param.starts_with("size=") {
|
|
|
|
size_str = ¶m[5..];
|
2019-10-04 18:27:22 +00:00
|
|
|
} else if param.starts_with("iommu=") {
|
|
|
|
iommu_str = ¶m[6..];
|
2019-11-19 23:47:31 +00:00
|
|
|
} else if param.starts_with("mergeable=") {
|
|
|
|
mergeable_str = ¶m[10..];
|
2020-03-19 10:13:53 +00:00
|
|
|
} else if param.starts_with("discard_writes=") {
|
|
|
|
discard_writes_str = ¶m[15..];
|
2019-06-19 20:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if file_str.is_empty() {
|
|
|
|
return Err(Error::ParsePmemFileParam);
|
|
|
|
}
|
|
|
|
|
2019-06-28 08:45:58 +00:00
|
|
|
Ok(PmemConfig {
|
2019-09-23 17:27:56 +00:00
|
|
|
file: PathBuf::from(file_str),
|
2019-07-09 03:13:02 +00:00
|
|
|
size: parse_size(size_str)?,
|
2019-11-19 23:40:57 +00:00
|
|
|
iommu: parse_on_off(iommu_str)?,
|
2019-11-19 23:47:31 +00:00
|
|
|
mergeable: parse_on_off(mergeable_str)?,
|
2020-03-19 10:13:53 +00:00
|
|
|
discard_writes: parse_on_off(discard_writes_str)?,
|
2019-06-28 08:45:58 +00:00
|
|
|
})
|
2019-06-19 20:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 10:56:37 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
2019-07-22 19:29:02 +00:00
|
|
|
pub enum ConsoleOutputMode {
|
2019-07-10 14:41:46 +00:00
|
|
|
Off,
|
|
|
|
Tty,
|
|
|
|
File,
|
2019-08-09 07:56:10 +00:00
|
|
|
Null,
|
2019-07-10 14:41:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 10:48:03 +00:00
|
|
|
impl ConsoleOutputMode {
|
|
|
|
pub fn input_enabled(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
ConsoleOutputMode::Tty => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 10:56:37 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
2019-09-23 17:30:22 +00:00
|
|
|
pub struct ConsoleConfig {
|
2019-12-10 14:40:47 +00:00
|
|
|
#[serde(default = "default_consoleconfig_file")]
|
2019-09-23 17:30:22 +00:00
|
|
|
pub file: Option<PathBuf>,
|
2019-07-22 19:29:02 +00:00
|
|
|
pub mode: ConsoleOutputMode,
|
2019-10-04 19:01:32 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub iommu: bool,
|
2019-07-10 14:41:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 14:40:47 +00:00
|
|
|
fn default_consoleconfig_file() -> Option<PathBuf> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:30:22 +00:00
|
|
|
impl ConsoleConfig {
|
2019-10-04 19:01:32 +00:00
|
|
|
pub fn parse(console: &str) -> Result<Self> {
|
|
|
|
// Split the parameters based on the comma delimiter
|
|
|
|
let params_list: Vec<&str> = console.split(',').collect();
|
|
|
|
|
|
|
|
let mut valid = false;
|
2019-12-10 14:40:47 +00:00
|
|
|
let mut file: Option<PathBuf> = default_consoleconfig_file();
|
2019-10-04 19:01:32 +00:00
|
|
|
let mut mode: ConsoleOutputMode = ConsoleOutputMode::Off;
|
|
|
|
let mut iommu_str: &str = "";
|
|
|
|
|
|
|
|
for param in params_list.iter() {
|
|
|
|
if param.starts_with("iommu=") {
|
|
|
|
iommu_str = ¶m[6..];
|
|
|
|
} else {
|
|
|
|
if *param == "off" {
|
|
|
|
mode = ConsoleOutputMode::Off;
|
|
|
|
file = None;
|
|
|
|
} else if *param == "tty" {
|
|
|
|
mode = ConsoleOutputMode::Tty;
|
|
|
|
file = None;
|
|
|
|
} else if param.starts_with("file=") {
|
|
|
|
mode = ConsoleOutputMode::File;
|
|
|
|
file = Some(PathBuf::from(¶m[5..]));
|
|
|
|
} else if param.starts_with("null") {
|
|
|
|
mode = ConsoleOutputMode::Null;
|
|
|
|
file = None;
|
|
|
|
} else {
|
|
|
|
return Err(Error::ParseConsoleParam);
|
|
|
|
}
|
|
|
|
valid = true;
|
|
|
|
}
|
2019-07-10 14:41:46 +00:00
|
|
|
}
|
2019-10-04 19:01:32 +00:00
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return Err(Error::ParseConsoleParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
mode,
|
|
|
|
file,
|
2019-11-19 23:40:57 +00:00
|
|
|
iommu: parse_on_off(iommu_str)?,
|
2019-10-04 19:01:32 +00:00
|
|
|
})
|
2019-07-10 14:41:46 +00:00
|
|
|
}
|
2019-09-27 09:40:50 +00:00
|
|
|
|
|
|
|
pub fn default_serial() -> Self {
|
|
|
|
ConsoleConfig {
|
|
|
|
file: None,
|
|
|
|
mode: ConsoleOutputMode::Null,
|
2019-10-04 19:01:32 +00:00
|
|
|
iommu: false,
|
2019-09-27 09:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn default_console() -> Self {
|
|
|
|
ConsoleConfig {
|
|
|
|
file: None,
|
|
|
|
mode: ConsoleOutputMode::Tty,
|
2019-10-04 19:01:32 +00:00
|
|
|
iommu: false,
|
2019-09-27 09:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-10 14:41:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 10:56:37 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
2019-09-23 17:31:41 +00:00
|
|
|
pub struct DeviceConfig {
|
|
|
|
pub path: PathBuf,
|
2019-10-07 16:03:58 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub iommu: bool,
|
2020-03-09 13:08:59 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub id: Option<String>,
|
2019-07-15 09:42:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 17:31:41 +00:00
|
|
|
impl DeviceConfig {
|
2020-03-12 16:20:31 +00:00
|
|
|
pub const SYNTAX: &'static str =
|
|
|
|
"Direct device assignment parameters \"path=<device_path>,iommu=on|off,id=<device_id>\"";
|
2019-09-23 17:31:41 +00:00
|
|
|
pub fn parse(device: &str) -> Result<Self> {
|
2019-10-07 16:03:58 +00:00
|
|
|
// Split the parameters based on the comma delimiter
|
|
|
|
let params_list: Vec<&str> = device.split(',').collect();
|
|
|
|
|
|
|
|
let mut path_str: &str = "";
|
|
|
|
let mut iommu_str: &str = "";
|
2020-03-11 10:23:42 +00:00
|
|
|
let mut id_str: &str = "";
|
2019-10-07 16:03:58 +00:00
|
|
|
|
|
|
|
for param in params_list.iter() {
|
|
|
|
if param.starts_with("path=") {
|
|
|
|
path_str = ¶m[5..];
|
|
|
|
} else if param.starts_with("iommu=") {
|
|
|
|
iommu_str = ¶m[6..];
|
2020-03-11 10:23:42 +00:00
|
|
|
} else if param.starts_with("id=") {
|
|
|
|
id_str = ¶m[3..];
|
2019-10-07 16:03:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 10:23:42 +00:00
|
|
|
let id = if !id_str.is_empty() {
|
|
|
|
Some(String::from(id_str))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2019-07-15 09:42:40 +00:00
|
|
|
Ok(DeviceConfig {
|
2019-10-07 16:03:58 +00:00
|
|
|
path: PathBuf::from(path_str),
|
2019-11-19 23:40:57 +00:00
|
|
|
iommu: parse_on_off(iommu_str)?,
|
2020-03-11 10:23:42 +00:00
|
|
|
id,
|
2019-07-15 09:42:40 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 10:56:37 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
2019-09-23 17:46:35 +00:00
|
|
|
pub struct VsockConfig {
|
2019-09-04 17:39:17 +00:00
|
|
|
pub cid: u64,
|
2019-09-23 17:46:35 +00:00
|
|
|
pub sock: PathBuf,
|
2019-10-04 19:06:34 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub iommu: bool,
|
2019-09-04 17:39:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 17:46:35 +00:00
|
|
|
impl VsockConfig {
|
|
|
|
pub fn parse(vsock: &str) -> Result<Self> {
|
2019-09-04 17:39:17 +00:00
|
|
|
// Split the parameters based on the comma delimiter
|
|
|
|
let params_list: Vec<&str> = vsock.split(',').collect();
|
|
|
|
|
|
|
|
let mut cid_str: &str = "";
|
|
|
|
let mut sock_str: &str = "";
|
2019-10-04 19:06:34 +00:00
|
|
|
let mut iommu_str: &str = "";
|
2019-09-04 17:39:17 +00:00
|
|
|
|
|
|
|
for param in params_list.iter() {
|
|
|
|
if param.starts_with("cid=") {
|
|
|
|
cid_str = ¶m[4..];
|
|
|
|
} else if param.starts_with("sock=") {
|
|
|
|
sock_str = ¶m[5..];
|
2019-10-04 19:06:34 +00:00
|
|
|
} else if param.starts_with("iommu=") {
|
|
|
|
iommu_str = ¶m[6..];
|
2019-09-04 17:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sock_str.is_empty() {
|
|
|
|
return Err(Error::ParseVsockSockParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(VsockConfig {
|
|
|
|
cid: cid_str.parse::<u64>().map_err(Error::ParseVsockCidParam)?,
|
2019-09-23 17:46:35 +00:00
|
|
|
sock: PathBuf::from(sock_str),
|
2019-11-19 23:40:57 +00:00
|
|
|
iommu: parse_on_off(iommu_str)?,
|
2019-09-04 17:39:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 10:56:37 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
2019-09-23 17:46:35 +00:00
|
|
|
pub struct VmConfig {
|
2019-09-27 09:40:50 +00:00
|
|
|
#[serde(default)]
|
2019-05-23 19:48:05 +00:00
|
|
|
pub cpus: CpusConfig,
|
2019-09-27 09:40:50 +00:00
|
|
|
#[serde(default)]
|
2019-09-23 16:51:36 +00:00
|
|
|
pub memory: MemoryConfig,
|
2019-09-19 07:52:55 +00:00
|
|
|
pub kernel: Option<KernelConfig>,
|
2020-03-15 17:51:17 +00:00
|
|
|
pub initramfs: Option<InitramfsConfig>,
|
2019-12-13 11:10:13 +00:00
|
|
|
#[serde(default)]
|
2019-05-23 19:48:05 +00:00
|
|
|
pub cmdline: CmdlineConfig,
|
2019-09-23 17:12:19 +00:00
|
|
|
pub disks: Option<Vec<DiskConfig>>,
|
2019-09-23 17:14:35 +00:00
|
|
|
pub net: Option<Vec<NetConfig>>,
|
2019-09-27 09:40:50 +00:00
|
|
|
#[serde(default)]
|
2019-09-23 17:15:50 +00:00
|
|
|
pub rng: RngConfig,
|
2019-09-23 17:23:17 +00:00
|
|
|
pub fs: Option<Vec<FsConfig>>,
|
2019-09-23 17:27:56 +00:00
|
|
|
pub pmem: Option<Vec<PmemConfig>>,
|
2019-09-27 09:40:50 +00:00
|
|
|
#[serde(default = "ConsoleConfig::default_serial")]
|
2019-09-23 17:30:22 +00:00
|
|
|
pub serial: ConsoleConfig,
|
2019-09-27 09:40:50 +00:00
|
|
|
#[serde(default = "ConsoleConfig::default_console")]
|
2019-09-23 17:30:22 +00:00
|
|
|
pub console: ConsoleConfig,
|
2019-09-23 17:31:41 +00:00
|
|
|
pub devices: Option<Vec<DeviceConfig>>,
|
2019-09-23 17:46:35 +00:00
|
|
|
pub vsock: Option<Vec<VsockConfig>>,
|
2019-09-18 14:13:56 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub iommu: bool,
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 17:46:35 +00:00
|
|
|
impl VmConfig {
|
2019-09-19 07:52:55 +00:00
|
|
|
pub fn valid(&self) -> bool {
|
|
|
|
self.kernel.is_some()
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:46:35 +00:00
|
|
|
pub fn parse(vm_params: VmParams) -> Result<Self> {
|
2019-10-02 21:01:36 +00:00
|
|
|
let mut iommu = false;
|
2019-09-18 14:13:56 +00:00
|
|
|
|
2019-07-08 22:48:39 +00:00
|
|
|
let mut disks: Option<Vec<DiskConfig>> = None;
|
|
|
|
if let Some(disk_list) = &vm_params.disks {
|
|
|
|
let mut disk_config_list = Vec::new();
|
|
|
|
for item in disk_list.iter() {
|
2019-10-02 21:01:36 +00:00
|
|
|
let disk_config = DiskConfig::parse(item)?;
|
|
|
|
if disk_config.iommu {
|
|
|
|
iommu = true;
|
|
|
|
}
|
|
|
|
disk_config_list.push(disk_config);
|
2019-07-08 22:48:39 +00:00
|
|
|
}
|
|
|
|
disks = Some(disk_config_list);
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 22:31:13 +00:00
|
|
|
let mut net: Option<Vec<NetConfig>> = None;
|
|
|
|
if let Some(net_list) = &vm_params.net {
|
|
|
|
let mut net_config_list = Vec::new();
|
|
|
|
for item in net_list.iter() {
|
2019-10-02 21:13:44 +00:00
|
|
|
let net_config = NetConfig::parse(item)?;
|
|
|
|
if net_config.iommu {
|
|
|
|
iommu = true;
|
|
|
|
}
|
|
|
|
net_config_list.push(net_config);
|
2019-07-08 22:31:13 +00:00
|
|
|
}
|
|
|
|
net = Some(net_config_list);
|
|
|
|
}
|
|
|
|
|
2019-10-04 18:18:49 +00:00
|
|
|
let rng = RngConfig::parse(vm_params.rng)?;
|
|
|
|
if rng.iommu {
|
|
|
|
iommu = true;
|
|
|
|
}
|
|
|
|
|
2019-06-27 16:14:11 +00:00
|
|
|
let mut fs: Option<Vec<FsConfig>> = None;
|
|
|
|
if let Some(fs_list) = &vm_params.fs {
|
|
|
|
let mut fs_config_list = Vec::new();
|
|
|
|
for item in fs_list.iter() {
|
|
|
|
fs_config_list.push(FsConfig::parse(item)?);
|
|
|
|
}
|
|
|
|
fs = Some(fs_config_list);
|
|
|
|
}
|
|
|
|
|
2019-06-28 08:45:58 +00:00
|
|
|
let mut pmem: Option<Vec<PmemConfig>> = None;
|
|
|
|
if let Some(pmem_list) = &vm_params.pmem {
|
|
|
|
let mut pmem_config_list = Vec::new();
|
|
|
|
for item in pmem_list.iter() {
|
2019-10-04 18:27:22 +00:00
|
|
|
let pmem_config = PmemConfig::parse(item)?;
|
|
|
|
if pmem_config.iommu {
|
|
|
|
iommu = true;
|
|
|
|
}
|
|
|
|
pmem_config_list.push(pmem_config);
|
2019-06-28 08:45:58 +00:00
|
|
|
}
|
|
|
|
pmem = Some(pmem_config_list);
|
|
|
|
}
|
|
|
|
|
2019-07-22 19:29:02 +00:00
|
|
|
let console = ConsoleConfig::parse(vm_params.console)?;
|
2019-10-04 19:01:32 +00:00
|
|
|
if console.iommu {
|
|
|
|
iommu = true;
|
|
|
|
}
|
2019-07-22 19:29:02 +00:00
|
|
|
let serial = ConsoleConfig::parse(vm_params.serial)?;
|
|
|
|
if console.mode == ConsoleOutputMode::Tty && serial.mode == ConsoleOutputMode::Tty {
|
|
|
|
return Err(Error::ParseTTYParam);
|
|
|
|
}
|
|
|
|
|
2019-07-15 09:42:40 +00:00
|
|
|
let mut devices: Option<Vec<DeviceConfig>> = None;
|
|
|
|
if let Some(device_list) = &vm_params.devices {
|
|
|
|
let mut device_config_list = Vec::new();
|
|
|
|
for item in device_list.iter() {
|
2019-10-07 16:03:58 +00:00
|
|
|
let device_config = DeviceConfig::parse(item)?;
|
|
|
|
if device_config.iommu {
|
|
|
|
iommu = true;
|
|
|
|
}
|
|
|
|
device_config_list.push(device_config);
|
2019-07-15 09:42:40 +00:00
|
|
|
}
|
|
|
|
devices = Some(device_config_list);
|
|
|
|
}
|
|
|
|
|
2019-09-04 17:39:17 +00:00
|
|
|
let mut vsock: Option<Vec<VsockConfig>> = None;
|
|
|
|
if let Some(vsock_list) = &vm_params.vsock {
|
|
|
|
let mut vsock_config_list = Vec::new();
|
|
|
|
for item in vsock_list.iter() {
|
2019-10-04 19:06:34 +00:00
|
|
|
let vsock_config = VsockConfig::parse(item)?;
|
|
|
|
if vsock_config.iommu {
|
|
|
|
iommu = true;
|
|
|
|
}
|
|
|
|
vsock_config_list.push(vsock_config);
|
2019-09-04 17:39:17 +00:00
|
|
|
}
|
|
|
|
vsock = Some(vsock_config_list);
|
|
|
|
}
|
|
|
|
|
2019-09-19 07:52:55 +00:00
|
|
|
let mut kernel: Option<KernelConfig> = None;
|
|
|
|
if let Some(k) = vm_params.kernel {
|
|
|
|
kernel = Some(KernelConfig {
|
|
|
|
path: PathBuf::from(k),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-15 17:51:17 +00:00
|
|
|
let mut initramfs: Option<InitramfsConfig> = None;
|
|
|
|
if let Some(k) = vm_params.initramfs {
|
|
|
|
initramfs = Some(InitramfsConfig {
|
|
|
|
path: PathBuf::from(k),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-23 19:48:05 +00:00
|
|
|
Ok(VmConfig {
|
|
|
|
cpus: CpusConfig::parse(vm_params.cpus)?,
|
|
|
|
memory: MemoryConfig::parse(vm_params.memory)?,
|
2019-09-19 07:52:55 +00:00
|
|
|
kernel,
|
2020-03-15 17:51:17 +00:00
|
|
|
initramfs,
|
2019-05-23 19:48:05 +00:00
|
|
|
cmdline: CmdlineConfig::parse(vm_params.cmdline)?,
|
|
|
|
disks,
|
2019-07-08 22:31:13 +00:00
|
|
|
net,
|
2019-10-04 18:18:49 +00:00
|
|
|
rng,
|
2019-06-27 16:14:11 +00:00
|
|
|
fs,
|
2019-06-28 08:45:58 +00:00
|
|
|
pmem,
|
2019-07-22 19:29:02 +00:00
|
|
|
serial,
|
|
|
|
console,
|
2019-07-15 09:42:40 +00:00
|
|
|
devices,
|
2019-09-04 17:39:17 +00:00
|
|
|
vsock,
|
2019-09-18 14:13:56 +00:00
|
|
|
iommu,
|
2019-05-23 19:48:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 11:15:55 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_option_parser() -> std::result::Result<(), OptionParserError> {
|
|
|
|
let mut parser = OptionParser::new();
|
|
|
|
parser
|
|
|
|
.add("size")
|
|
|
|
.add("file")
|
|
|
|
.add("mergeable")
|
|
|
|
.add("hotplug_method")
|
|
|
|
.add("hotplug_size");
|
|
|
|
|
|
|
|
assert!(parser
|
|
|
|
.parse("size=128M,file=/dev/shm,hanging_param")
|
|
|
|
.is_err());
|
|
|
|
assert!(parser
|
|
|
|
.parse("size=128M,file=/dev/shm,too_many_equals=foo=bar")
|
|
|
|
.is_err());
|
|
|
|
assert!(parser.parse("size=128M,file=/dev/shm").is_ok());
|
|
|
|
|
|
|
|
assert_eq!(parser.get("size"), Some("128M".to_owned()));
|
|
|
|
assert_eq!(parser.get("file"), Some("/dev/shm".to_owned()));
|
|
|
|
assert!(!parser.is_set("mergeable"));
|
|
|
|
assert!(parser.is_set("size"));
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-03-27 12:30:13 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cpu_parsing() -> Result<()> {
|
|
|
|
assert_eq!(CpusConfig::parse("")?, CpusConfig::default());
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
CpusConfig::parse("boot=1")?,
|
|
|
|
CpusConfig {
|
|
|
|
boot_vcpus: 1,
|
|
|
|
max_vcpus: 1
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
CpusConfig::parse("boot=1,max=2")?,
|
|
|
|
CpusConfig {
|
|
|
|
boot_vcpus: 1,
|
|
|
|
max_vcpus: 2,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert!(CpusConfig::parse("boot=2,max=1").is_err());
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-03-27 12:54:48 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mem_parsing() -> Result<()> {
|
|
|
|
assert_eq!(MemoryConfig::parse("")?, MemoryConfig::default());
|
|
|
|
// Default string
|
|
|
|
assert_eq!(MemoryConfig::parse("size=512M")?, MemoryConfig::default());
|
|
|
|
assert_eq!(
|
|
|
|
MemoryConfig::parse("size=512M,file=/some/file")?,
|
|
|
|
MemoryConfig {
|
|
|
|
size: 512 << 20,
|
|
|
|
file: Some(PathBuf::from("/some/file")),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
MemoryConfig::parse("size=512M,mergeable=on")?,
|
|
|
|
MemoryConfig {
|
|
|
|
size: 512 << 20,
|
|
|
|
mergeable: true,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
MemoryConfig::parse("mergeable=on")?,
|
|
|
|
MemoryConfig {
|
|
|
|
mergeable: true,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
MemoryConfig::parse("size=1G,mergeable=off")?,
|
|
|
|
MemoryConfig {
|
|
|
|
size: 1 << 30,
|
|
|
|
mergeable: false,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
MemoryConfig::parse("hotplug_method=acpi")?,
|
|
|
|
MemoryConfig {
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
MemoryConfig::parse("hotplug_method=acpi,hotplug_size=512M")?,
|
|
|
|
MemoryConfig {
|
|
|
|
hotplug_size: Some(512 << 20),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
MemoryConfig::parse("hotplug_method=virtio-mem,hotplug_size=512M")?,
|
|
|
|
MemoryConfig {
|
|
|
|
hotplug_size: Some(512 << 20),
|
|
|
|
hotplug_method: HotplugMethod::VirtioMem,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-03-27 11:15:55 +00:00
|
|
|
}
|