Skip to content

Commit 3845280

Browse files
committed
test: VirtIO PCI device creation and restoration
Do a bit of refactoring on the test code that inserts VirtIO devices in a Vmm object and then add a test for PCI devices which creates a Vmm with PCI devices and then restores them and makes sure that everything is restored as expected. Signed-off-by: Babis Chalios <[email protected]>
1 parent 46306df commit 3845280

File tree

4 files changed

+245
-30
lines changed

4 files changed

+245
-30
lines changed

src/vmm/src/builder.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,6 @@ pub(crate) mod tests {
848848

849849
assert!(
850850
vmm.device_manager
851-
.mmio_devices
852851
.get_virtio_device(TYPE_VSOCK, &vsock_dev_id)
853852
.is_some()
854853
);
@@ -874,7 +873,6 @@ pub(crate) mod tests {
874873

875874
assert!(
876875
vmm.device_manager
877-
.mmio_devices
878876
.get_virtio_device(TYPE_RNG, ENTROPY_DEV_ID)
879877
.is_some()
880878
);
@@ -909,7 +907,6 @@ pub(crate) mod tests {
909907

910908
assert!(
911909
vmm.device_manager
912-
.mmio_devices
913910
.get_virtio_device(TYPE_BALLOON, BALLOON_DEV_ID)
914911
.is_some()
915912
);
@@ -961,7 +958,6 @@ pub(crate) mod tests {
961958
assert!(cmdline_contains(&cmdline, "root=/dev/vda ro"));
962959
assert!(
963960
vmm.device_manager
964-
.mmio_devices
965961
.get_virtio_device(TYPE_BLOCK, drive_id.as_str())
966962
.is_some()
967963
);
@@ -983,7 +979,6 @@ pub(crate) mod tests {
983979
assert!(cmdline_contains(&cmdline, "root=PARTUUID=0eaa91a0-01 rw"));
984980
assert!(
985981
vmm.device_manager
986-
.mmio_devices
987982
.get_virtio_device(TYPE_BLOCK, drive_id.as_str())
988983
.is_some()
989984
);
@@ -1006,7 +1001,6 @@ pub(crate) mod tests {
10061001
assert!(!cmdline_contains(&cmdline, "root=/dev/vda"));
10071002
assert!(
10081003
vmm.device_manager
1009-
.mmio_devices
10101004
.get_virtio_device(TYPE_BLOCK, drive_id.as_str())
10111005
.is_some()
10121006
);
@@ -1044,19 +1038,16 @@ pub(crate) mod tests {
10441038
assert!(cmdline_contains(&cmdline, "root=PARTUUID=0eaa91a0-01 rw"));
10451039
assert!(
10461040
vmm.device_manager
1047-
.mmio_devices
10481041
.get_virtio_device(TYPE_BLOCK, "root")
10491042
.is_some()
10501043
);
10511044
assert!(
10521045
vmm.device_manager
1053-
.mmio_devices
10541046
.get_virtio_device(TYPE_BLOCK, "secondary")
10551047
.is_some()
10561048
);
10571049
assert!(
10581050
vmm.device_manager
1059-
.mmio_devices
10601051
.get_virtio_device(TYPE_BLOCK, "third")
10611052
.is_some()
10621053
);
@@ -1086,7 +1077,6 @@ pub(crate) mod tests {
10861077
assert!(cmdline_contains(&cmdline, "root=/dev/vda rw"));
10871078
assert!(
10881079
vmm.device_manager
1089-
.mmio_devices
10901080
.get_virtio_device(TYPE_BLOCK, drive_id.as_str())
10911081
.is_some()
10921082
);
@@ -1108,7 +1098,6 @@ pub(crate) mod tests {
11081098
assert!(cmdline_contains(&cmdline, "root=PARTUUID=0eaa91a0-01 ro"));
11091099
assert!(
11101100
vmm.device_manager
1111-
.mmio_devices
11121101
.get_virtio_device(TYPE_BLOCK, drive_id.as_str())
11131102
.is_some()
11141103
);
@@ -1130,7 +1119,6 @@ pub(crate) mod tests {
11301119
assert!(cmdline_contains(&cmdline, "root=/dev/vda rw"));
11311120
assert!(
11321121
vmm.device_manager
1133-
.mmio_devices
11341122
.get_virtio_device(TYPE_BLOCK, drive_id.as_str())
11351123
.is_some()
11361124
);

src/vmm/src/device_manager/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,36 @@ impl DeviceManager {
375375
Self::do_mark_virtio_queue_memory_dirty(virtio_device, mem);
376376
}
377377
}
378+
379+
/// Get a VirtIO device of type `virtio_type` with ID `device_id`
380+
pub fn get_virtio_device(
381+
&self,
382+
virtio_type: u32,
383+
device_id: &str,
384+
) -> Option<Arc<Mutex<dyn VirtioDevice>>> {
385+
if self.pci_devices.pci_segment.is_some() {
386+
let pci_device = self.pci_devices.get_virtio_device(virtio_type, device_id)?;
387+
Some(
388+
pci_device
389+
.lock()
390+
.expect("Poisoned lock")
391+
.virtio_device()
392+
.clone(),
393+
)
394+
} else {
395+
let mmio_device = self
396+
.mmio_devices
397+
.get_virtio_device(virtio_type, device_id)?;
398+
Some(
399+
mmio_device
400+
.inner
401+
.lock()
402+
.expect("Poisoned lock")
403+
.device()
404+
.clone(),
405+
)
406+
}
407+
}
378408
}
379409

380410
#[derive(Debug, Default, Clone, Serialize, Deserialize)]

0 commit comments

Comments
 (0)