vmm: add validation for network parameters

Signed-off-by: Praveen Paladugu <prapal@microsoft.com>
This commit is contained in:
Praveen Paladugu 2020-07-29 20:20:50 +00:00 committed by Sebastien Boeuf
parent a52b614a61
commit afa8ecc90c

View File

@ -93,6 +93,8 @@ pub enum ValidationError {
CpuTopologyCount, CpuTopologyCount,
/// One part of the CPU topology was zero /// One part of the CPU topology was zero
CpuTopologyZeroPart, CpuTopologyZeroPart,
/// Virtio needs a min of 2 queues
VnetQueueLowerThan2,
} }
type ValidationResult<T> = std::result::Result<T, ValidationError>; type ValidationResult<T> = std::result::Result<T, ValidationError>;
@ -116,6 +118,7 @@ impl fmt::Display for ValidationError {
f, f,
"Product of CPU topology parts does not match maximum vCPUs" "Product of CPU topology parts does not match maximum vCPUs"
), ),
VnetQueueLowerThan2 => write!(f, "Number of queues to virtio_net less than 2"),
} }
} }
} }
@ -711,8 +714,7 @@ impl NetConfig {
.0; .0;
let vhost_socket = parser.get("socket"); let vhost_socket = parser.get("socket");
let id = parser.get("id"); let id = parser.get("id");
let config = NetConfig {
Ok(NetConfig {
tap, tap,
ip, ip,
mask, mask,
@ -724,7 +726,15 @@ impl NetConfig {
vhost_user, vhost_user,
vhost_socket, vhost_socket,
id, id,
}) };
config.validate().map_err(Error::Validation)?;
Ok(config)
}
pub fn validate(&self) -> ValidationResult<()> {
if self.num_queues < 2 {
return Err(ValidationError::VnetQueueLowerThan2);
}
Ok(())
} }
} }