mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
vmm: config: Switch DeviceConfig to use PciDeviceCommonConfig
Switch DeviceConfig 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:
+25
-38
@@ -2206,45 +2206,23 @@ impl DeviceConfig {
|
||||
.add("x_nv_gpudirect_clique");
|
||||
parser.parse(device).map_err(Error::ParseDevice)?;
|
||||
|
||||
let pci_common = PciDeviceCommonConfig::parse(device)?;
|
||||
let path = parser
|
||||
.get("path")
|
||||
.map(PathBuf::from)
|
||||
.ok_or(Error::ParseDevicePathMissing)?;
|
||||
let iommu = parser
|
||||
.convert::<Toggle>("iommu")
|
||||
.map_err(Error::ParseDevice)?
|
||||
.unwrap_or(Toggle(false))
|
||||
.0;
|
||||
let id = parser.get("id");
|
||||
let pci_segment = parser
|
||||
.convert::<u16>("pci_segment")
|
||||
.map_err(Error::ParseDevice)?
|
||||
.unwrap_or_default();
|
||||
let x_nv_gpudirect_clique = parser
|
||||
.convert::<u8>("x_nv_gpudirect_clique")
|
||||
.map_err(Error::ParseDevice)?;
|
||||
Ok(DeviceConfig {
|
||||
pci_common,
|
||||
path,
|
||||
iommu,
|
||||
id,
|
||||
pci_segment,
|
||||
x_nv_gpudirect_clique,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn validate(&self, vm_config: &VmConfig) -> ValidationResult<()> {
|
||||
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));
|
||||
}
|
||||
}
|
||||
self.pci_common.validate(vm_config)?;
|
||||
|
||||
if self.x_nv_gpudirect_clique.is_some() {
|
||||
let vfio_p2p_dma = vm_config.platform.as_ref().is_none_or(|p| p.vfio_p2p_dma);
|
||||
@@ -3120,9 +3098,9 @@ impl VmConfig {
|
||||
}
|
||||
|
||||
device.validate(self)?;
|
||||
self.iommu |= device.iommu;
|
||||
self.iommu |= device.pci_common.iommu;
|
||||
|
||||
Self::validate_identifier(&mut id_list, &device.id)?;
|
||||
Self::validate_identifier(&mut id_list, &device.pci_common.id)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3454,7 +3432,7 @@ impl VmConfig {
|
||||
// Remove if VFIO device
|
||||
if let Some(devices) = self.devices.as_mut() {
|
||||
let len = devices.len();
|
||||
devices.retain(|dev| dev.id.as_ref().map(|id| id.as_ref()) != Some(id));
|
||||
devices.retain(|dev| dev.pci_common.id.as_ref().map(|id| id.as_ref()) != Some(id));
|
||||
removed |= devices.len() != len;
|
||||
}
|
||||
|
||||
@@ -4443,10 +4421,8 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
|
||||
|
||||
fn device_fixture() -> DeviceConfig {
|
||||
DeviceConfig {
|
||||
pci_common: PciDeviceCommonConfig::default(),
|
||||
path: PathBuf::from("/path/to/device"),
|
||||
id: None,
|
||||
iommu: false,
|
||||
pci_segment: 0,
|
||||
x_nv_gpudirect_clique: None,
|
||||
}
|
||||
}
|
||||
@@ -4463,7 +4439,10 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
|
||||
assert_eq!(
|
||||
DeviceConfig::parse("path=/path/to/device,iommu=on")?,
|
||||
DeviceConfig {
|
||||
iommu: true,
|
||||
pci_common: PciDeviceCommonConfig {
|
||||
iommu: true,
|
||||
..Default::default()
|
||||
},
|
||||
..device_fixture()
|
||||
}
|
||||
);
|
||||
@@ -4471,8 +4450,11 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
|
||||
assert_eq!(
|
||||
DeviceConfig::parse("path=/path/to/device,iommu=on,id=mydevice0")?,
|
||||
DeviceConfig {
|
||||
id: Some("mydevice0".to_owned()),
|
||||
iommu: true,
|
||||
pci_common: PciDeviceCommonConfig {
|
||||
id: Some("mydevice0".to_owned()),
|
||||
iommu: true,
|
||||
..Default::default()
|
||||
},
|
||||
..device_fixture()
|
||||
}
|
||||
);
|
||||
@@ -5308,8 +5290,11 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
|
||||
..platform_fixture()
|
||||
});
|
||||
still_valid_config.devices = Some(vec![DeviceConfig {
|
||||
iommu: true,
|
||||
pci_segment: 1,
|
||||
pci_common: PciDeviceCommonConfig {
|
||||
iommu: true,
|
||||
pci_segment: 1,
|
||||
..Default::default()
|
||||
},
|
||||
..device_fixture()
|
||||
}]);
|
||||
still_valid_config.validate().unwrap();
|
||||
@@ -5389,8 +5374,10 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
|
||||
..platform_fixture()
|
||||
});
|
||||
invalid_config.devices = Some(vec![DeviceConfig {
|
||||
iommu: false,
|
||||
pci_segment: 1,
|
||||
pci_common: PciDeviceCommonConfig {
|
||||
pci_segment: 1,
|
||||
..Default::default()
|
||||
},
|
||||
..device_fixture()
|
||||
}]);
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user