vmm: config: Port device parsing to OptionParser

Also make the "path" option required and generate an error if it is not
provided.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-04-02 18:25:52 +01:00
parent a85e2fa735
commit 8665898ff3

View File

@ -75,6 +75,10 @@ pub enum Error {
ParseConsoleFileMissing, ParseConsoleFileMissing,
/// No mode given for console /// No mode given for console
ParseConsoleInvalidModeGiven, ParseConsoleInvalidModeGiven,
/// Failed parsing device parameters
ParseDevice(OptionParserError),
/// Missing path from device,
ParseDevicePathMissing,
} }
pub type Result<T> = result::Result<T, Error>; pub type Result<T> = result::Result<T, Error>;
@ -1015,34 +1019,21 @@ impl DeviceConfig {
pub const SYNTAX: &'static str = pub const SYNTAX: &'static str =
"Direct device assignment parameters \"path=<device_path>,iommu=on|off,id=<device_id>\""; "Direct device assignment parameters \"path=<device_path>,iommu=on|off,id=<device_id>\"";
pub fn parse(device: &str) -> Result<Self> { pub fn parse(device: &str) -> Result<Self> {
// Split the parameters based on the comma delimiter let mut parser = OptionParser::new();
let params_list: Vec<&str> = device.split(',').collect(); parser.add("path").add("id").add("iommu");
parser.parse(device).map_err(Error::ParseDevice)?;
let mut path_str: &str = ""; let path = parser
let mut iommu_str: &str = ""; .get("path")
let mut id_str: &str = ""; .map(PathBuf::from)
.ok_or(Error::ParseDevicePathMissing)?;
for param in params_list.iter() { let iommu = parser
if param.starts_with("path=") { .convert::<Toggle>("iommu")
path_str = &param[5..]; .map_err(Error::ParseDevice)?
} else if param.starts_with("iommu=") { .unwrap_or(Toggle(false))
iommu_str = &param[6..]; .0;
} else if param.starts_with("id=") { let id = parser.get("id");
id_str = &param[3..]; Ok(DeviceConfig { path, iommu, id })
}
}
let id = if !id_str.is_empty() {
Some(String::from(id_str))
} else {
None
};
Ok(DeviceConfig {
path: PathBuf::from(path_str),
iommu: parse_on_off(iommu_str)?,
id,
})
} }
} }
@ -1660,6 +1651,8 @@ mod tests {
#[test] #[test]
fn test_device_parsing() -> Result<()> { fn test_device_parsing() -> Result<()> {
// Device must have a path provided
assert!(DeviceConfig::parse("").is_err());
assert_eq!( assert_eq!(
DeviceConfig::parse("path=/path/to/device")?, DeviceConfig::parse("path=/path/to/device")?,
DeviceConfig { DeviceConfig {