@@ -247,8 +247,9 @@ fn add_pci_device(
247
247
248
248
fn add_vfio_device (
249
249
vmm : & mut Vmm ,
250
- fd : DeviceFd ,
250
+ fd : & DeviceFd ,
251
251
device_path : & Path ,
252
+ memory_slot : Arc < dyn Fn ( ) -> u32 + Send + Sync > ,
252
253
) -> Result < ( ) , StartMicrovmError > {
253
254
let pci_segment = vmm. pci_segment . as_ref ( ) . expect ( "pci should be enabled" ) ;
254
255
@@ -296,14 +297,7 @@ fn add_vfio_device(
296
297
None ,
297
298
false ,
298
299
pci_device_bdf. into ( ) ,
299
- Arc :: new ( move || {
300
- // TODO use allocator for memory slots
301
- static mut CURRENT : u32 = 1 ;
302
- unsafe {
303
- CURRENT += 1 ;
304
- CURRENT
305
- }
306
- } ) ,
300
+ memory_slot,
307
301
None
308
302
) . unwrap ( ) ) ;
309
303
@@ -622,8 +616,17 @@ pub fn build_microvm_for_boot(
622
616
}
623
617
624
618
if let Some ( vfio_devices) = vm_resources. pci_config . as_ref ( ) . map ( |x| x. vfio_devices . as_ref ( ) ) . flatten ( ) {
619
+ let device_fd = create_passthrough_device ( vmm. vm . fd ( ) ) ;
620
+ let memory_slot = Arc :: new ( move || {
621
+ // TODO use allocator for memory slots
622
+ static mut CURRENT : u32 = 1 ;
623
+ unsafe {
624
+ CURRENT += 1 ;
625
+ CURRENT
626
+ }
627
+ } ) ;
625
628
for vfio_device in vfio_devices {
626
- attach_vfio_device ( & mut vmm, Path :: new ( & vfio_device. path ) ) ?;
629
+ add_vfio_device ( & mut vmm, & device_fd , Path :: new ( & vfio_device. path ) , memory_slot . clone ( ) ) ?;
627
630
}
628
631
}
629
632
@@ -1385,19 +1388,6 @@ fn attach_balloon_device(
1385
1388
attach_virtio_device ( event_manager, vmm, id, balloon. clone ( ) , cmdline, false )
1386
1389
}
1387
1390
1388
- fn attach_vfio_device (
1389
- vmm : & mut Vmm ,
1390
- device_path : & Path
1391
- ) -> Result < ( ) , StartMicrovmError > {
1392
- let device_fd = create_passthrough_device ( vmm. vm . fd ( ) ) ;
1393
-
1394
- add_vfio_device (
1395
- vmm,
1396
- device_fd,
1397
- device_path,
1398
- )
1399
- }
1400
-
1401
1391
// Adds `O_NONBLOCK` to the stdout flags.
1402
1392
pub ( crate ) fn set_stdout_nonblocking ( ) {
1403
1393
// SAFETY: Call is safe since parameters are valid.
0 commit comments