From 66a3bed086d4e8308d26b58fae70dee38c68f7f9 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Mon, 8 Feb 2021 15:29:42 +0000 Subject: [PATCH] vmm: config: Add "--tdx" option parsing Signed-off-by: Rob Bradford --- src/main.rs | 13 +++++++++++++ vmm/src/config.rs | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 6815e47f0..f24936d4c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -357,6 +357,17 @@ fn create_app<'a, 'b>( ); } + #[cfg(feature = "tdx")] + { + app = app.arg( + Arg::with_name("tdx") + .long("tdx") + .help("TDX Support: firmware=") + .takes_value(true) + .group("vm-config"), + ); + } + app } @@ -664,6 +675,8 @@ mod unit_tests { sgx_epc: None, numa: None, watchdog: false, + #[cfg(feature = "tdx")] + tdx: None, }; aver_eq!(tb, expected_vm_config, result_vm_config); diff --git a/vmm/src/config.rs b/vmm/src/config.rs index cd2e210f5..c5476a414 100644 --- a/vmm/src/config.rs +++ b/vmm/src/config.rs @@ -79,6 +79,12 @@ pub enum Error { ParseNuma(OptionParserError), /// Failed to validate configuration Validation(ValidationError), + #[cfg(feature = "tdx")] + /// Failed to parse TDX config + ParseTdx(OptionParserError), + #[cfg(feature = "tdx")] + // No TDX firmware + FirmwarePathMissing, } #[derive(Debug)] @@ -190,6 +196,10 @@ impl fmt::Display for Error { write!(f, "Error parsing --restore: source_url missing") } Validation(v) => write!(f, "Error validating configuration: {}", v), + #[cfg(feature = "tdx")] + ParseTdx(o) => write!(f, "Error parsing --tdx: {}", o), + #[cfg(feature = "tdx")] + FirmwarePathMissing => write!(f, "TDX firmware missing"), } } } @@ -217,6 +227,8 @@ pub struct VmParams<'a> { pub sgx_epc: Option>, pub numa: Option>, pub watchdog: bool, + #[cfg(feature = "tdx")] + pub tdx: Option<&'a str>, } impl<'a> VmParams<'a> { @@ -244,7 +256,8 @@ impl<'a> VmParams<'a> { let sgx_epc: Option> = args.values_of("sgx-epc").map(|x| x.collect()); let numa: Option> = args.values_of("numa").map(|x| x.collect()); let watchdog = args.is_present("watchdog"); - + #[cfg(feature = "tdx")] + let tdx = args.value_of("tdx"); VmParams { cpus, memory, @@ -266,6 +279,8 @@ impl<'a> VmParams<'a> { sgx_epc, numa, watchdog, + #[cfg(feature = "tdx")] + tdx, } } } @@ -1326,6 +1341,26 @@ impl VsockConfig { } } +#[cfg(feature = "tdx")] +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, Default)] +pub struct TdxConfig { + pub firmware: PathBuf, +} + +#[cfg(feature = "tdx")] +impl TdxConfig { + pub fn parse(tdx: &str) -> Result { + let mut parser = OptionParser::new(); + parser.add("firmware"); + parser.parse(tdx).map_err(Error::ParseTdx)?; + let firmware = parser + .get("firmware") + .map(PathBuf::from) + .ok_or(Error::FirmwarePathMissing)?; + Ok(TdxConfig { firmware }) + } +} + #[cfg(target_arch = "x86_64")] #[derive(Clone, Debug, PartialEq, Deserialize, Serialize, Default)] pub struct SgxEpcConfig { @@ -1490,6 +1525,8 @@ pub struct VmConfig { pub numa: Option>, #[serde(default)] pub watchdog: bool, + #[cfg(feature = "tdx")] + pub tdx: Option, } impl VmConfig { @@ -1695,6 +1732,9 @@ impl VmConfig { }); } + #[cfg(feature = "tdx")] + let tdx = vm_params.tdx.map(TdxConfig::parse).transpose()?; + let config = VmConfig { cpus: CpusConfig::parse(vm_params.cpus)?, memory: MemoryConfig::parse(vm_params.memory, vm_params.memory_zones)?, @@ -1716,6 +1756,8 @@ impl VmConfig { sgx_epc, numa, watchdog: vm_params.watchdog, + #[cfg(feature = "tdx")] + tdx, }; config.validate().map_err(Error::Validation)?; Ok(config) @@ -2322,6 +2364,8 @@ mod tests { sgx_epc: None, numa: None, watchdog: false, + #[cfg(feature = "tdx")] + tdx: None, }; assert!(valid_config.validate().is_ok());