@@ -247,8 +247,9 @@ fn add_pci_device(
247247
248248fn add_vfio_device (
249249 vmm : & mut Vmm ,
250- fd : DeviceFd ,
250+ fd : & DeviceFd ,
251251 device_path : & Path ,
252+ memory_slot : Arc < dyn Fn ( ) -> u32 + Send + Sync > ,
252253) -> Result < ( ) , StartMicrovmError > {
253254 let pci_segment = vmm. pci_segment . as_ref ( ) . expect ( "pci should be enabled" ) ;
254255
@@ -296,14 +297,7 @@ fn add_vfio_device(
296297 None ,
297298 false ,
298299 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,
307301 None
308302 ) . unwrap ( ) ) ;
309303
@@ -622,8 +616,17 @@ pub fn build_microvm_for_boot(
622616 }
623617
624618 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+ } ) ;
625628 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 ( ) ) ?;
627630 }
628631 }
629632
@@ -1385,19 +1388,6 @@ fn attach_balloon_device(
13851388 attach_virtio_device ( event_manager, vmm, id, balloon. clone ( ) , cmdline, false )
13861389}
13871390
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-
14011391// Adds `O_NONBLOCK` to the stdout flags.
14021392pub ( crate ) fn set_stdout_nonblocking ( ) {
14031393 // SAFETY: Call is safe since parameters are valid.
0 commit comments