mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
vmm: config: Switch VsockConfig to use PciDeviceCommonConfig
Switch VsockConfig 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:
@@ -2313,49 +2313,25 @@ impl VsockConfig {
|
||||
.add("pci_segment");
|
||||
parser.parse(vsock).map_err(Error::ParseVsock)?;
|
||||
|
||||
let pci_common = PciDeviceCommonConfig::parse(vsock)?;
|
||||
let socket = parser
|
||||
.get("socket")
|
||||
.map(PathBuf::from)
|
||||
.ok_or(Error::ParseVsockSockMissing)?;
|
||||
let iommu = parser
|
||||
.convert::<Toggle>("iommu")
|
||||
.map_err(Error::ParseVsock)?
|
||||
.unwrap_or(Toggle(false))
|
||||
.0;
|
||||
let cid = parser
|
||||
.convert("cid")
|
||||
.map_err(Error::ParseVsock)?
|
||||
.ok_or(Error::ParseVsockCidMissing)?;
|
||||
let id = parser.get("id");
|
||||
let pci_segment = parser
|
||||
.convert("pci_segment")
|
||||
.map_err(Error::ParseVsock)?
|
||||
.unwrap_or_default();
|
||||
|
||||
Ok(VsockConfig {
|
||||
pci_common,
|
||||
cid,
|
||||
socket,
|
||||
iommu,
|
||||
id,
|
||||
pci_segment,
|
||||
})
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
self.pci_common.validate(vm_config)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3064,9 +3040,9 @@ impl VmConfig {
|
||||
|
||||
if let Some(vsock) = &self.vsock {
|
||||
vsock.validate(self)?;
|
||||
self.iommu |= vsock.iommu;
|
||||
self.iommu |= vsock.pci_common.iommu;
|
||||
|
||||
Self::validate_identifier(&mut id_list, &vsock.id)?;
|
||||
Self::validate_identifier(&mut id_list, &vsock.pci_common.id)?;
|
||||
}
|
||||
|
||||
let num_pci_segments = match &self.platform {
|
||||
@@ -3446,7 +3422,7 @@ impl VmConfig {
|
||||
|
||||
// Remove if vsock device
|
||||
if let Some(vsock) = self.vsock.as_ref()
|
||||
&& vsock.id.as_ref().map(|id| id.as_ref()) == Some(id)
|
||||
&& vsock.pci_common.id.as_ref().map(|id| id.as_ref()) == Some(id)
|
||||
{
|
||||
self.vsock = None;
|
||||
removed = true;
|
||||
@@ -4467,21 +4443,20 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
|
||||
assert_eq!(
|
||||
VsockConfig::parse("socket=/tmp/sock,cid=3")?,
|
||||
VsockConfig {
|
||||
pci_common: PciDeviceCommonConfig::default(),
|
||||
cid: 3,
|
||||
socket: PathBuf::from("/tmp/sock"),
|
||||
iommu: false,
|
||||
id: None,
|
||||
pci_segment: 0,
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
VsockConfig::parse("socket=/tmp/sock,cid=3,iommu=on")?,
|
||||
VsockConfig {
|
||||
pci_common: PciDeviceCommonConfig {
|
||||
iommu: true,
|
||||
..Default::default()
|
||||
},
|
||||
cid: 3,
|
||||
socket: PathBuf::from("/tmp/sock"),
|
||||
iommu: true,
|
||||
id: None,
|
||||
pci_segment: 0,
|
||||
}
|
||||
);
|
||||
Ok(())
|
||||
@@ -5264,11 +5239,13 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
|
||||
..platform_fixture()
|
||||
});
|
||||
still_valid_config.vsock = Some(VsockConfig {
|
||||
pci_common: PciDeviceCommonConfig {
|
||||
iommu: true,
|
||||
pci_segment: 1,
|
||||
..Default::default()
|
||||
},
|
||||
cid: 3,
|
||||
socket: PathBuf::new(),
|
||||
id: None,
|
||||
iommu: true,
|
||||
pci_segment: 1,
|
||||
});
|
||||
still_valid_config.validate().unwrap();
|
||||
|
||||
@@ -5350,11 +5327,12 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
|
||||
..platform_fixture()
|
||||
});
|
||||
invalid_config.vsock = Some(VsockConfig {
|
||||
pci_common: PciDeviceCommonConfig {
|
||||
pci_segment: 1,
|
||||
..Default::default()
|
||||
},
|
||||
cid: 3,
|
||||
socket: PathBuf::new(),
|
||||
id: None,
|
||||
iommu: false,
|
||||
pci_segment: 1,
|
||||
});
|
||||
assert_eq!(
|
||||
invalid_config.validate(),
|
||||
|
||||
@@ -3469,11 +3469,11 @@ impl DeviceManager {
|
||||
&mut self,
|
||||
vsock_cfg: &mut VsockConfig,
|
||||
) -> DeviceManagerResult<MetaVirtioDevice> {
|
||||
let id = if let Some(id) = &vsock_cfg.id {
|
||||
let id = if let Some(id) = &vsock_cfg.pci_common.id {
|
||||
id.clone()
|
||||
} else {
|
||||
let id = self.next_device_name(VSOCK_DEVICE_NAME_PREFIX)?;
|
||||
vsock_cfg.id = Some(id.clone());
|
||||
vsock_cfg.pci_common.id = Some(id.clone());
|
||||
id
|
||||
};
|
||||
|
||||
@@ -3493,7 +3493,7 @@ impl DeviceManager {
|
||||
vsock_cfg.cid,
|
||||
vsock_cfg.socket.clone(),
|
||||
backend,
|
||||
self.force_iommu | vsock_cfg.iommu,
|
||||
self.force_iommu | vsock_cfg.pci_common.iommu,
|
||||
self.seccomp_action.clone(),
|
||||
self.exit_evt
|
||||
.try_clone()
|
||||
@@ -3515,9 +3515,9 @@ impl DeviceManager {
|
||||
Ok(MetaVirtioDevice {
|
||||
virtio_device: Arc::clone(&vsock_device)
|
||||
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
||||
iommu: vsock_cfg.iommu,
|
||||
iommu: vsock_cfg.pci_common.iommu,
|
||||
id,
|
||||
pci_segment: vsock_cfg.pci_segment,
|
||||
pci_segment: vsock_cfg.pci_common.pci_segment,
|
||||
dma_handler: None,
|
||||
})
|
||||
}
|
||||
@@ -5092,9 +5092,9 @@ impl DeviceManager {
|
||||
}
|
||||
|
||||
pub fn add_vsock(&mut self, vsock_cfg: &mut VsockConfig) -> DeviceManagerResult<PciDeviceInfo> {
|
||||
self.validate_identifier(&vsock_cfg.id)?;
|
||||
self.validate_identifier(&vsock_cfg.pci_common.id)?;
|
||||
|
||||
if vsock_cfg.iommu && !self.is_iommu_segment(vsock_cfg.pci_segment) {
|
||||
if vsock_cfg.pci_common.iommu && !self.is_iommu_segment(vsock_cfg.pci_common.pci_segment) {
|
||||
return Err(DeviceManagerError::InvalidIommuHotplug);
|
||||
}
|
||||
|
||||
|
||||
@@ -667,14 +667,10 @@ impl ApplyLandlock for VdpaConfig {
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub struct VsockConfig {
|
||||
#[serde(flatten)]
|
||||
pub pci_common: PciDeviceCommonConfig,
|
||||
pub cid: u32,
|
||||
pub socket: PathBuf,
|
||||
#[serde(default)]
|
||||
pub iommu: bool,
|
||||
#[serde(default)]
|
||||
pub id: Option<String>,
|
||||
#[serde(default)]
|
||||
pub pci_segment: u16,
|
||||
}
|
||||
|
||||
impl ApplyLandlock for VsockConfig {
|
||||
|
||||
Reference in New Issue
Block a user