From c731a943d4c34348a32708af2489774fe149dd47 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Thu, 2 Apr 2020 18:39:19 +0100 Subject: [PATCH] vmm: config: Port vsock to OptionParser Signed-off-by: Rob Bradford --- vmm/src/config.rs | 51 +++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/vmm/src/config.rs b/vmm/src/config.rs index b97e577e2..37be324a9 100644 --- a/vmm/src/config.rs +++ b/vmm/src/config.rs @@ -47,10 +47,10 @@ pub enum Error { ParseSizeParam(std::num::ParseIntError), /// Both console and serial are tty. ParseTTYParam, - /// Failed parsing vsock context ID parameter. - ParseVsockCidParam(std::num::ParseIntError), - /// Failed parsing vsock socket path parameter. - ParseVsockSockParam, + /// Missing vsock socket path parameter. + ParseVsockSockMissing, + /// Missing vsock cid parameter. + ParseVsockCidMissing, /// Missing kernel configuration ValidateMissingKernelConfig, /// Failed parsing generic on|off parameter. @@ -79,6 +79,8 @@ pub enum Error { ParseDevice(OptionParserError), /// Missing path from device, ParseDevicePathMissing, + /// Failed to parse vsock parameters + ParseVsock(OptionParserError), } pub type Result = result::Result; @@ -1047,32 +1049,25 @@ pub struct VsockConfig { impl VsockConfig { pub fn parse(vsock: &str) -> Result { - // Split the parameters based on the comma delimiter - let params_list: Vec<&str> = vsock.split(',').collect(); + let mut parser = OptionParser::new(); + parser.add("sock").add("cid").add("iommu"); + parser.parse(vsock).map_err(Error::ParseVsock)?; - let mut cid_str: &str = ""; - let mut sock_str: &str = ""; - let mut iommu_str: &str = ""; + let sock = parser + .get("sock") + .map(PathBuf::from) + .ok_or(Error::ParseVsockSockMissing)?; + let iommu = parser + .convert::("iommu") + .map_err(Error::ParseVsock)? + .unwrap_or(Toggle(false)) + .0; + let cid = parser + .convert("cid") + .map_err(Error::ParseVsock)? + .ok_or(Error::ParseVsockCidMissing)?; - 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..]; - } else if param.starts_with("iommu=") { - iommu_str = ¶m[6..]; - } - } - - if sock_str.is_empty() { - return Err(Error::ParseVsockSockParam); - } - - Ok(VsockConfig { - cid: cid_str.parse::().map_err(Error::ParseVsockCidParam)?, - sock: PathBuf::from(sock_str), - iommu: parse_on_off(iommu_str)?, - }) + Ok(VsockConfig { cid, sock, iommu }) } }