Skip to content

Commit 2e573c1

Browse files
committed
refactor: Remove function pointer from MMIODevManagerConstructorArgs
The function pointer is called for every device being restored, however all use-sites of it pointed to the same function (VmResources::update_from_restored_device), so the indirection/abstraction is not needed. Removing this function point removes restrictions on the signature of the function, and thus makes adding error handling easier (for example, we can now update update_from_restored_device to validate whether the devices read from the snapshot are actually consistent with the rest of the vm configuration). The entire `SharedDeviceType` enum can probably be removed too, but lets keep the changes minimal in this commit, as its for the 1.7 release branch. Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
1 parent 5f95011 commit 2e573c1

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

src/vmm/src/builder.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,6 @@ pub fn build_microvm_from_snapshot(
470470
mem: guest_memory,
471471
vm: vmm.vm.fd(),
472472
event_manager,
473-
for_each_restored_device: VmResources::update_from_restored_device,
474473
vm_resources,
475474
instance_id: &instance_info.id,
476475
};

src/vmm/src/device_manager/persist.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ pub struct MMIODevManagerConstructorArgs<'a> {
206206
pub mem: GuestMemoryMmap,
207207
pub vm: &'a VmFd,
208208
pub event_manager: &'a mut EventManager,
209-
pub for_each_restored_device: fn(&mut VmResources, SharedDeviceType),
210209
pub vm_resources: &'a mut VmResources,
211210
pub instance_id: &'a str,
212211
}
@@ -460,10 +459,9 @@ impl<'a> Persist<'a> for MMIODeviceManager {
460459
&balloon_state.device_state,
461460
)?));
462461

463-
(constructor_args.for_each_restored_device)(
464-
constructor_args.vm_resources,
465-
SharedDeviceType::Balloon(device.clone()),
466-
);
462+
constructor_args
463+
.vm_resources
464+
.update_from_restored_device(SharedDeviceType::Balloon(device.clone()));
467465

468466
restore_helper(
469467
device.clone(),
@@ -482,10 +480,9 @@ impl<'a> Persist<'a> for MMIODeviceManager {
482480
&block_state.device_state,
483481
)?));
484482

485-
(constructor_args.for_each_restored_device)(
486-
constructor_args.vm_resources,
487-
SharedDeviceType::VirtioBlock(device.clone()),
488-
);
483+
constructor_args
484+
.vm_resources
485+
.update_from_restored_device(SharedDeviceType::VirtioBlock(device.clone()));
489486

490487
restore_helper(
491488
device.clone(),
@@ -528,10 +525,9 @@ impl<'a> Persist<'a> for MMIODeviceManager {
528525
&net_state.device_state,
529526
)?));
530527

531-
(constructor_args.for_each_restored_device)(
532-
constructor_args.vm_resources,
533-
SharedDeviceType::Network(device.clone()),
534-
);
528+
constructor_args
529+
.vm_resources
530+
.update_from_restored_device(SharedDeviceType::Network(device.clone()));
535531

536532
restore_helper(
537533
device.clone(),
@@ -557,10 +553,9 @@ impl<'a> Persist<'a> for MMIODeviceManager {
557553
&vsock_state.device_state.frontend,
558554
)?));
559555

560-
(constructor_args.for_each_restored_device)(
561-
constructor_args.vm_resources,
562-
SharedDeviceType::Vsock(device.clone()),
563-
);
556+
constructor_args
557+
.vm_resources
558+
.update_from_restored_device(SharedDeviceType::Vsock(device.clone()));
564559

565560
restore_helper(
566561
device.clone(),
@@ -581,10 +576,9 @@ impl<'a> Persist<'a> for MMIODeviceManager {
581576
&entropy_state.device_state,
582577
)?));
583578

584-
(constructor_args.for_each_restored_device)(
585-
constructor_args.vm_resources,
586-
SharedDeviceType::Entropy(device.clone()),
587-
);
579+
constructor_args
580+
.vm_resources
581+
.update_from_restored_device(SharedDeviceType::Entropy(device.clone()));
588582

589583
restore_helper(
590584
device.clone(),
@@ -759,7 +753,6 @@ mod tests {
759753
mem: vmm.guest_memory().clone(),
760754
vm: vmm.vm.fd(),
761755
event_manager: &mut event_manager,
762-
for_each_restored_device: VmResources::update_from_restored_device,
763756
vm_resources,
764757
instance_id: "microvm-id",
765758
};

0 commit comments

Comments
 (0)