mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
vmm: config: Switch NetConfig to use PciDeviceCommonConfig
Switch NetConfig over to using the newly extracted struct members as used by all PCI based devices. The use of #[serde(flatten)] means that this change has no impact on the JSON format that the data is stored as. Signed-off-by: Rob Bradford <rbradford@meta.com>
This commit is contained in:
@@ -1554,11 +1554,6 @@ impl NetConfig {
|
||||
.unwrap_or(Toggle(true))
|
||||
.0;
|
||||
let mtu = parser.convert("mtu").map_err(Error::ParseNetwork)?;
|
||||
let iommu = parser
|
||||
.convert::<Toggle>("iommu")
|
||||
.map_err(Error::ParseNetwork)?
|
||||
.unwrap_or(Toggle(false))
|
||||
.0;
|
||||
let queue_size = parser
|
||||
.convert("queue_size")
|
||||
.map_err(Error::ParseNetwork)?
|
||||
@@ -1577,15 +1572,10 @@ impl NetConfig {
|
||||
.convert("vhost_mode")
|
||||
.map_err(Error::ParseNetwork)?
|
||||
.unwrap_or_default();
|
||||
let id = parser.get("id");
|
||||
let fds = parser
|
||||
.convert::<IntegerList>("fd")
|
||||
.map_err(Error::ParseNetwork)?
|
||||
.map(|v| v.0.iter().map(|e| *e as i32).collect());
|
||||
let pci_segment = parser
|
||||
.convert("pci_segment")
|
||||
.map_err(Error::ParseNetwork)?
|
||||
.unwrap_or_default();
|
||||
let bw_size = parser
|
||||
.convert("bw_size")
|
||||
.map_err(Error::ParseNetwork)?
|
||||
@@ -1637,23 +1627,23 @@ impl NetConfig {
|
||||
None
|
||||
};
|
||||
|
||||
let pci_common = PciDeviceCommonConfig::parse(net)?;
|
||||
|
||||
let config = NetConfig {
|
||||
pci_common,
|
||||
tap,
|
||||
ip,
|
||||
mask,
|
||||
mac,
|
||||
host_mac,
|
||||
mtu,
|
||||
iommu,
|
||||
num_queues,
|
||||
queue_size,
|
||||
vhost_user,
|
||||
vhost_socket,
|
||||
vhost_mode,
|
||||
id,
|
||||
fds,
|
||||
rate_limiter_config,
|
||||
pci_segment,
|
||||
offload_tso,
|
||||
offload_ufo,
|
||||
offload_csum,
|
||||
@@ -1662,6 +1652,8 @@ impl NetConfig {
|
||||
}
|
||||
|
||||
pub fn validate(&self, vm_config: &VmConfig) -> ValidationResult<()> {
|
||||
self.pci_common.validate(vm_config)?;
|
||||
|
||||
if self.num_queues < 2 {
|
||||
return Err(ValidationError::VnetQueueLowerThan2(self.num_queues));
|
||||
}
|
||||
@@ -1689,23 +1681,10 @@ impl NetConfig {
|
||||
));
|
||||
}
|
||||
|
||||
if self.vhost_user && self.iommu {
|
||||
if self.vhost_user && self.pci_common.iommu {
|
||||
return Err(ValidationError::IommuNotSupported);
|
||||
}
|
||||
|
||||
if let Some(platform_config) = vm_config.platform.as_ref() {
|
||||
if self.pci_segment >= platform_config.num_pci_segments {
|
||||
return Err(ValidationError::InvalidPciSegment(self.pci_segment));
|
||||
}
|
||||
|
||||
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref()
|
||||
&& iommu_segments.contains(&self.pci_segment)
|
||||
&& !self.iommu
|
||||
{
|
||||
return Err(ValidationError::OnIommuSegment(self.pci_segment));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(mtu) = self.mtu
|
||||
&& mtu < virtio_devices::net::MIN_MTU
|
||||
{
|
||||
@@ -2778,6 +2757,7 @@ impl RestoreConfig {
|
||||
for net_fds in vm_config.net.iter().flatten() {
|
||||
if let Some(expected_fds) = &net_fds.fds {
|
||||
let expected_id = net_fds
|
||||
.pci_common
|
||||
.id
|
||||
.as_ref()
|
||||
.expect("Invalid 'NetConfig' with empty 'id' for VM restore.");
|
||||
@@ -3066,9 +3046,9 @@ impl VmConfig {
|
||||
return Err(ValidationError::VhostUserRequiresSharedMemory);
|
||||
}
|
||||
net.validate(self)?;
|
||||
self.iommu |= net.iommu;
|
||||
self.iommu |= net.pci_common.iommu;
|
||||
|
||||
Self::validate_identifier(&mut id_list, &net.id)?;
|
||||
Self::validate_identifier(&mut id_list, &net.pci_common.id)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3564,7 +3544,7 @@ impl VmConfig {
|
||||
// Remove if net device
|
||||
if let Some(net) = self.net.as_mut() {
|
||||
let len = net.len();
|
||||
net.retain(|dev| dev.id.as_ref().map(|id| id.as_ref()) != Some(id));
|
||||
net.retain(|dev| dev.pci_common.id.as_ref().map(|id| id.as_ref()) != Some(id));
|
||||
removed |= net.len() != len;
|
||||
}
|
||||
|
||||
@@ -4148,22 +4128,20 @@ mod unit_tests {
|
||||
|
||||
fn net_fixture() -> NetConfig {
|
||||
NetConfig {
|
||||
pci_common: PciDeviceCommonConfig::default(),
|
||||
tap: None,
|
||||
ip: None,
|
||||
mask: None,
|
||||
mac: MacAddr::parse_str("de:ad:be:ef:12:34").unwrap(),
|
||||
host_mac: Some(MacAddr::parse_str("12:34:de:ad:be:ef").unwrap()),
|
||||
mtu: None,
|
||||
iommu: false,
|
||||
num_queues: 2,
|
||||
queue_size: 256,
|
||||
vhost_user: false,
|
||||
vhost_socket: None,
|
||||
vhost_mode: VhostMode::Client,
|
||||
id: None,
|
||||
fds: None,
|
||||
rate_limiter_config: None,
|
||||
pci_segment: 0,
|
||||
offload_tso: true,
|
||||
offload_ufo: true,
|
||||
offload_csum: true,
|
||||
@@ -4181,7 +4159,10 @@ mod unit_tests {
|
||||
assert_eq!(
|
||||
NetConfig::parse("mac=de:ad:be:ef:12:34,host_mac=12:34:de:ad:be:ef,id=mynet0")?,
|
||||
NetConfig {
|
||||
id: Some("mynet0".to_owned()),
|
||||
pci_common: PciDeviceCommonConfig {
|
||||
id: Some("mynet0".to_owned()),
|
||||
..Default::default()
|
||||
},
|
||||
..net_fixture()
|
||||
}
|
||||
);
|
||||
@@ -4214,9 +4195,12 @@ mod unit_tests {
|
||||
"mac=de:ad:be:ef:12:34,host_mac=12:34:de:ad:be:ef,num_queues=4,queue_size=1024,iommu=on"
|
||||
)?,
|
||||
NetConfig {
|
||||
pci_common: PciDeviceCommonConfig {
|
||||
iommu: true,
|
||||
..Default::default()
|
||||
},
|
||||
num_queues: 4,
|
||||
queue_size: 1024,
|
||||
iommu: true,
|
||||
..net_fixture()
|
||||
}
|
||||
);
|
||||
@@ -4840,19 +4824,28 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
|
||||
preserved_fds: None,
|
||||
net: Some(vec![
|
||||
NetConfig {
|
||||
id: Some("net0".to_owned()),
|
||||
pci_common: PciDeviceCommonConfig {
|
||||
id: Some("net0".to_owned()),
|
||||
..Default::default()
|
||||
},
|
||||
num_queues: 2,
|
||||
fds: Some(vec![-1, -1, -1, -1]),
|
||||
..net_fixture()
|
||||
},
|
||||
NetConfig {
|
||||
id: Some("net1".to_owned()),
|
||||
pci_common: PciDeviceCommonConfig {
|
||||
id: Some("net1".to_owned()),
|
||||
..Default::default()
|
||||
},
|
||||
num_queues: 1,
|
||||
fds: Some(vec![-1, -1]),
|
||||
..net_fixture()
|
||||
},
|
||||
NetConfig {
|
||||
id: Some("net2".to_owned()),
|
||||
pci_common: PciDeviceCommonConfig {
|
||||
id: Some("net2".to_owned()),
|
||||
..Default::default()
|
||||
},
|
||||
fds: None,
|
||||
..net_fixture()
|
||||
},
|
||||
@@ -4947,7 +4940,10 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
|
||||
resume: false,
|
||||
};
|
||||
snapshot_vm_config.net = Some(vec![NetConfig {
|
||||
id: Some("net2".to_owned()),
|
||||
pci_common: PciDeviceCommonConfig {
|
||||
id: Some("net2".to_owned()),
|
||||
..Default::default()
|
||||
},
|
||||
fds: None,
|
||||
..net_fixture()
|
||||
}]);
|
||||
@@ -5330,8 +5326,11 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
|
||||
..platform_fixture()
|
||||
});
|
||||
still_valid_config.net = Some(vec![NetConfig {
|
||||
iommu: true,
|
||||
pci_segment: 1,
|
||||
pci_common: PciDeviceCommonConfig {
|
||||
iommu: true,
|
||||
pci_segment: 1,
|
||||
..Default::default()
|
||||
},
|
||||
..net_fixture()
|
||||
}]);
|
||||
still_valid_config.validate().unwrap();
|
||||
@@ -5398,8 +5397,11 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
|
||||
..platform_fixture()
|
||||
});
|
||||
invalid_config.net = Some(vec![NetConfig {
|
||||
iommu: false,
|
||||
pci_segment: 1,
|
||||
pci_common: PciDeviceCommonConfig {
|
||||
iommu: false,
|
||||
pci_segment: 1,
|
||||
..Default::default()
|
||||
},
|
||||
..net_fixture()
|
||||
}]);
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user