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-05-23 19:48:05 +00:00
|
|
|
use linux_loader::cmdline::Cmdline;
|
|
|
|
use net_util::MacAddr;
|
|
|
|
use std::convert::From;
|
|
|
|
use std::net::AddrParseError;
|
|
|
|
use std::net::Ipv4Addr;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::result;
|
|
|
|
use vm_memory::GuestAddress;
|
2019-08-28 10:01:01 +00:00
|
|
|
use vm_virtio::vhost_user::VhostUserConfig;
|
2019-05-23 19:48:05 +00:00
|
|
|
|
|
|
|
pub const DEFAULT_VCPUS: &str = "1";
|
2019-07-10 05:53:39 +00:00
|
|
|
pub const DEFAULT_MEMORY: &str = "size=512M";
|
2019-05-23 19:48:05 +00:00
|
|
|
pub const DEFAULT_RNG_SOURCE: &str = "/dev/urandom";
|
|
|
|
const CMDLINE_OFFSET: GuestAddress = GuestAddress(0x20000);
|
|
|
|
|
|
|
|
/// Errors associated with VM configuration parameters.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error<'a> {
|
|
|
|
/// Failed parsing cpus parameters.
|
|
|
|
ParseCpusParams(std::num::ParseIntError),
|
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,
|
|
|
|
/// 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.
|
|
|
|
ParseNetMacParam(&'a str),
|
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.
|
|
|
|
ParseVuNetMacParam(&'a str),
|
|
|
|
/// Failed parsing vhost-user-net sock parameter.
|
|
|
|
ParseVuNetSockParam,
|
|
|
|
/// Failed parsing vhost-user-net queue number parameter.
|
|
|
|
ParseVuNetNumQueuesParam(std::num::ParseIntError),
|
|
|
|
/// Failed parsing vhost-user-net queue size parameter.
|
|
|
|
ParseVuNetQueueSizeParam(std::num::ParseIntError),
|
|
|
|
/// Failed parsing vhost-user-net server parameter.
|
|
|
|
ParseVuNetServerParam(std::num::ParseIntError),
|
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-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
pub type Result<'a, T> = result::Result<T, Error<'a>>;
|
|
|
|
|
|
|
|
pub struct VmParams<'a> {
|
|
|
|
pub cpus: &'a str,
|
|
|
|
pub memory: &'a str,
|
|
|
|
pub kernel: &'a str,
|
|
|
|
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-08-28 10:01:01 +00:00
|
|
|
pub vhost_user_net: 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-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-05-23 19:48:05 +00:00
|
|
|
pub struct CpusConfig(pub u8);
|
|
|
|
|
|
|
|
impl CpusConfig {
|
|
|
|
pub fn parse(cpus: &str) -> Result<Self> {
|
|
|
|
Ok(CpusConfig(
|
|
|
|
cpus.parse::<u8>().map_err(Error::ParseCpusParams)?,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&CpusConfig> for u8 {
|
|
|
|
fn from(val: &CpusConfig) -> Self {
|
|
|
|
val.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-24 19:21:23 +00:00
|
|
|
pub struct MemoryConfig<'a> {
|
|
|
|
pub size: u64,
|
|
|
|
pub file: Option<&'a Path>,
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 19:21:23 +00:00
|
|
|
impl<'a> MemoryConfig<'a> {
|
|
|
|
pub fn parse(memory: &'a str) -> Result<Self> {
|
|
|
|
// Split the parameters based on the comma delimiter
|
|
|
|
let params_list: Vec<&str> = memory.split(',').collect();
|
|
|
|
|
|
|
|
let mut size_str: &str = "";
|
|
|
|
let mut file_str: &str = "";
|
|
|
|
let mut backed = false;
|
|
|
|
|
|
|
|
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..];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let file = if backed {
|
|
|
|
if file_str.is_empty() {
|
|
|
|
return Err(Error::ParseMemoryFileParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(Path::new(file_str))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
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-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct KernelConfig<'a> {
|
|
|
|
pub path: &'a Path,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> KernelConfig<'a> {
|
|
|
|
pub fn parse(kernel: &'a str) -> Result<Self> {
|
|
|
|
Ok(KernelConfig {
|
|
|
|
path: Path::new(kernel),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CmdlineConfig {
|
|
|
|
pub args: Cmdline,
|
|
|
|
pub offset: GuestAddress,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CmdlineConfig {
|
|
|
|
pub fn parse(cmdline: Option<&str>) -> Result<Self> {
|
|
|
|
let cmdline_str = cmdline
|
|
|
|
.map(std::string::ToString::to_string)
|
|
|
|
.unwrap_or_else(String::new);
|
|
|
|
let mut args = Cmdline::new(arch::CMDLINE_MAX_SIZE);
|
|
|
|
args.insert_str(cmdline_str).unwrap();
|
|
|
|
|
|
|
|
Ok(CmdlineConfig {
|
|
|
|
args,
|
|
|
|
offset: CMDLINE_OFFSET,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct DiskConfig<'a> {
|
|
|
|
pub path: &'a Path,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> DiskConfig<'a> {
|
|
|
|
pub fn parse(disk: &'a str) -> Result<Self> {
|
|
|
|
Ok(DiskConfig {
|
|
|
|
path: Path::new(disk),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NetConfig<'a> {
|
|
|
|
pub tap: Option<&'a str>,
|
|
|
|
pub ip: Ipv4Addr,
|
|
|
|
pub mask: Ipv4Addr,
|
|
|
|
pub mac: MacAddr,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> NetConfig<'a> {
|
2019-07-08 22:31:13 +00:00
|
|
|
pub fn parse(net: &'a 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 = "";
|
|
|
|
|
|
|
|
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..];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut tap: Option<&str> = None;
|
|
|
|
let mut ip: Ipv4Addr = Ipv4Addr::new(192, 168, 249, 1);
|
|
|
|
let mut mask: Ipv4Addr = Ipv4Addr::new(255, 255, 255, 0);;
|
|
|
|
let mut mac: MacAddr = MacAddr::local_random();
|
|
|
|
|
|
|
|
if !tap_str.is_empty() {
|
|
|
|
tap = Some(tap_str);
|
|
|
|
}
|
|
|
|
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-07-08 22:31:13 +00:00
|
|
|
Ok(NetConfig { tap, ip, mask, mac })
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 20:06:49 +00:00
|
|
|
pub struct RngConfig<'a> {
|
|
|
|
pub src: &'a Path,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> RngConfig<'a> {
|
|
|
|
pub fn parse(rng: &'a str) -> Result<Self> {
|
|
|
|
Ok(RngConfig {
|
|
|
|
src: Path::new(rng),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 16:14:11 +00:00
|
|
|
#[derive(Debug)]
|
2019-05-22 20:06:49 +00:00
|
|
|
pub struct FsConfig<'a> {
|
|
|
|
pub tag: &'a str,
|
|
|
|
pub sock: &'a Path,
|
|
|
|
pub num_queues: usize,
|
|
|
|
pub queue_size: u16,
|
2019-08-05 19:45:58 +00:00
|
|
|
pub cache_size: Option<u64>,
|
2019-05-22 20:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FsConfig<'a> {
|
2019-06-27 16:14:11 +00:00
|
|
|
pub fn parse(fs: &'a 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut num_queues: usize = 1;
|
|
|
|
let mut queue_size: u16 = 1024;
|
2019-08-05 19:45:58 +00:00
|
|
|
let mut dax: bool = true;
|
|
|
|
// Default cache size set to 8Gib.
|
|
|
|
let mut cache_size: Option<u64> = Some(0x0002_0000_0000);
|
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);
|
|
|
|
}
|
|
|
|
cache_size = None;
|
|
|
|
} else if !cache_size_str.is_empty() {
|
|
|
|
cache_size = Some(parse_size(cache_size_str)?);
|
|
|
|
}
|
2019-05-22 20:06:49 +00:00
|
|
|
|
2019-06-27 16:14:11 +00:00
|
|
|
Ok(FsConfig {
|
2019-05-22 20:06:49 +00:00
|
|
|
tag,
|
|
|
|
sock: Path::new(sock),
|
|
|
|
num_queues,
|
|
|
|
queue_size,
|
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-06-19 20:48:37 +00:00
|
|
|
pub struct PmemConfig<'a> {
|
|
|
|
pub file: &'a Path,
|
|
|
|
pub size: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PmemConfig<'a> {
|
2019-06-28 08:45:58 +00:00
|
|
|
pub fn parse(pmem: &'a 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 = "";
|
|
|
|
|
|
|
|
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..];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if file_str.is_empty() {
|
|
|
|
return Err(Error::ParsePmemFileParam);
|
|
|
|
}
|
|
|
|
|
2019-06-28 08:45:58 +00:00
|
|
|
Ok(PmemConfig {
|
2019-06-19 20:48:37 +00:00
|
|
|
file: Path::new(file_str),
|
2019-07-09 03:13:02 +00:00
|
|
|
size: parse_size(size_str)?,
|
2019-06-28 08:45:58 +00:00
|
|
|
})
|
2019-06-19 20:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 14:41:46 +00:00
|
|
|
#[derive(PartialEq)]
|
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-07-22 19:29:02 +00:00
|
|
|
pub struct ConsoleConfig<'a> {
|
2019-07-10 14:41:46 +00:00
|
|
|
pub file: Option<&'a Path>,
|
2019-07-22 19:29:02 +00:00
|
|
|
pub mode: ConsoleOutputMode,
|
2019-07-10 14:41:46 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 19:29:02 +00:00
|
|
|
impl<'a> ConsoleConfig<'a> {
|
2019-07-10 14:41:46 +00:00
|
|
|
pub fn parse(param: &'a str) -> Result<Self> {
|
|
|
|
if param == "off" {
|
|
|
|
Ok(Self {
|
2019-07-22 19:29:02 +00:00
|
|
|
mode: ConsoleOutputMode::Off,
|
2019-07-10 14:41:46 +00:00
|
|
|
file: None,
|
|
|
|
})
|
|
|
|
} else if param == "tty" {
|
|
|
|
Ok(Self {
|
2019-07-22 19:29:02 +00:00
|
|
|
mode: ConsoleOutputMode::Tty,
|
2019-07-10 14:41:46 +00:00
|
|
|
file: None,
|
|
|
|
})
|
|
|
|
} else if param.starts_with("file=") {
|
|
|
|
Ok(Self {
|
2019-07-22 19:29:02 +00:00
|
|
|
mode: ConsoleOutputMode::File,
|
2019-07-10 14:41:46 +00:00
|
|
|
file: Some(Path::new(¶m[5..])),
|
|
|
|
})
|
2019-08-09 07:56:10 +00:00
|
|
|
} else if param.starts_with("null") {
|
|
|
|
Ok(Self {
|
|
|
|
mode: ConsoleOutputMode::Null,
|
|
|
|
file: None,
|
|
|
|
})
|
2019-07-10 14:41:46 +00:00
|
|
|
} else {
|
2019-07-22 19:29:02 +00:00
|
|
|
Err(Error::ParseConsoleParam)
|
2019-07-10 14:41:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-15 09:42:40 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct DeviceConfig<'a> {
|
|
|
|
pub path: &'a Path,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> DeviceConfig<'a> {
|
|
|
|
pub fn parse(device: &'a str) -> Result<Self> {
|
|
|
|
Ok(DeviceConfig {
|
|
|
|
path: Path::new(device),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 10:01:01 +00:00
|
|
|
pub struct VhostUserNetConfig<'a> {
|
|
|
|
pub mac: MacAddr,
|
|
|
|
pub vu_cfg: VhostUserConfig<'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> VhostUserNetConfig<'a> {
|
|
|
|
pub fn parse(vhost_user_net: &'a str) -> Result<Self> {
|
|
|
|
// Split the parameters based on the comma delimiter
|
|
|
|
let params_list: Vec<&str> = vhost_user_net.split(',').collect();
|
|
|
|
|
|
|
|
let mut mac_str: &str = "";
|
|
|
|
let mut sock: &str = "";
|
|
|
|
let mut num_queues_str: &str = "";
|
|
|
|
let mut queue_size_str: &str = "";
|
|
|
|
|
|
|
|
for param in params_list.iter() {
|
|
|
|
if param.starts_with("mac=") {
|
|
|
|
mac_str = ¶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..];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut mac: MacAddr = MacAddr::local_random();
|
|
|
|
let mut num_queues: usize = 2;
|
|
|
|
let mut queue_size: u16 = 256;
|
|
|
|
|
|
|
|
if !mac_str.is_empty() {
|
|
|
|
mac = MacAddr::parse_str(mac_str).map_err(Error::ParseVuNetMacParam)?;
|
|
|
|
}
|
|
|
|
if sock.is_empty() {
|
|
|
|
return Err(Error::ParseVuNetSockParam);
|
|
|
|
}
|
|
|
|
if !num_queues_str.is_empty() {
|
|
|
|
num_queues = num_queues_str
|
|
|
|
.parse()
|
|
|
|
.map_err(Error::ParseVuNetNumQueuesParam)?;
|
|
|
|
}
|
|
|
|
if !queue_size_str.is_empty() {
|
|
|
|
queue_size = queue_size_str
|
|
|
|
.parse()
|
|
|
|
.map_err(Error::ParseVuNetQueueSizeParam)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let vu_cfg = VhostUserConfig {
|
|
|
|
sock,
|
|
|
|
num_queues,
|
|
|
|
queue_size,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(VhostUserNetConfig { mac, vu_cfg })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 17:39:17 +00:00
|
|
|
pub struct VsockConfig<'a> {
|
|
|
|
pub cid: u64,
|
|
|
|
pub sock: &'a Path,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> VsockConfig<'a> {
|
|
|
|
pub fn parse(vsock: &'a str) -> Result<Self> {
|
|
|
|
// 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 = "";
|
|
|
|
|
|
|
|
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..];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sock_str.is_empty() {
|
|
|
|
return Err(Error::ParseVsockSockParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(VsockConfig {
|
|
|
|
cid: cid_str.parse::<u64>().map_err(Error::ParseVsockCidParam)?,
|
|
|
|
sock: Path::new(sock_str),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 19:48:05 +00:00
|
|
|
pub struct VmConfig<'a> {
|
|
|
|
pub cpus: CpusConfig,
|
2019-05-24 19:21:23 +00:00
|
|
|
pub memory: MemoryConfig<'a>,
|
2019-05-23 19:48:05 +00:00
|
|
|
pub kernel: KernelConfig<'a>,
|
|
|
|
pub cmdline: CmdlineConfig,
|
2019-07-08 22:48:39 +00:00
|
|
|
pub disks: Option<Vec<DiskConfig<'a>>>,
|
2019-07-08 22:31:13 +00:00
|
|
|
pub net: Option<Vec<NetConfig<'a>>>,
|
2019-05-22 20:06:49 +00:00
|
|
|
pub rng: RngConfig<'a>,
|
2019-06-27 16:14:11 +00:00
|
|
|
pub fs: Option<Vec<FsConfig<'a>>>,
|
2019-06-28 08:45:58 +00:00
|
|
|
pub pmem: Option<Vec<PmemConfig<'a>>>,
|
2019-07-22 19:29:02 +00:00
|
|
|
pub serial: ConsoleConfig<'a>,
|
|
|
|
pub console: ConsoleConfig<'a>,
|
2019-07-15 09:42:40 +00:00
|
|
|
pub devices: Option<Vec<DeviceConfig<'a>>>,
|
2019-08-28 10:01:01 +00:00
|
|
|
pub vhost_user_net: Option<Vec<VhostUserNetConfig<'a>>>,
|
2019-09-04 17:39:17 +00:00
|
|
|
pub vsock: Option<Vec<VsockConfig<'a>>>,
|
2019-05-23 19:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> VmConfig<'a> {
|
|
|
|
pub fn parse(vm_params: VmParams<'a>) -> Result<Self> {
|
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() {
|
|
|
|
disk_config_list.push(DiskConfig::parse(item)?);
|
|
|
|
}
|
|
|
|
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() {
|
|
|
|
net_config_list.push(NetConfig::parse(item)?);
|
|
|
|
}
|
|
|
|
net = Some(net_config_list);
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
pmem_config_list.push(PmemConfig::parse(item)?);
|
|
|
|
}
|
|
|
|
pmem = Some(pmem_config_list);
|
|
|
|
}
|
|
|
|
|
2019-07-22 19:29:02 +00:00
|
|
|
let console = ConsoleConfig::parse(vm_params.console)?;
|
|
|
|
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() {
|
|
|
|
device_config_list.push(DeviceConfig::parse(item)?);
|
|
|
|
}
|
|
|
|
devices = Some(device_config_list);
|
|
|
|
}
|
|
|
|
|
2019-08-28 10:01:01 +00:00
|
|
|
let mut vhost_user_net: Option<Vec<VhostUserNetConfig>> = None;
|
|
|
|
if let Some(vhost_user_net_list) = &vm_params.vhost_user_net {
|
|
|
|
let mut vhost_user_net_config_list = Vec::new();
|
|
|
|
for item in vhost_user_net_list.iter() {
|
|
|
|
vhost_user_net_config_list.push(VhostUserNetConfig::parse(item)?);
|
|
|
|
}
|
|
|
|
vhost_user_net = Some(vhost_user_net_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() {
|
|
|
|
vsock_config_list.push(VsockConfig::parse(item)?);
|
|
|
|
}
|
|
|
|
vsock = Some(vsock_config_list);
|
|
|
|
}
|
|
|
|
|
2019-05-23 19:48:05 +00:00
|
|
|
Ok(VmConfig {
|
|
|
|
cpus: CpusConfig::parse(vm_params.cpus)?,
|
|
|
|
memory: MemoryConfig::parse(vm_params.memory)?,
|
|
|
|
kernel: KernelConfig::parse(vm_params.kernel)?,
|
|
|
|
cmdline: CmdlineConfig::parse(vm_params.cmdline)?,
|
|
|
|
disks,
|
2019-07-08 22:31:13 +00:00
|
|
|
net,
|
2019-05-22 20:06:49 +00:00
|
|
|
rng: RngConfig::parse(vm_params.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-08-28 10:01:01 +00:00
|
|
|
vhost_user_net,
|
2019-09-04 17:39:17 +00:00
|
|
|
vsock,
|
2019-05-23 19:48:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|