@@ -34,7 +34,7 @@ use api_server::ApiRequest;
3434use api_server:: request:: async:: { AsyncOutcome , AsyncRequest } ;
3535use api_server:: request:: sync:: { DriveError , Error as SyncError , GenerateResponse ,
3636 NetworkInterfaceBody , OkStatus as SyncOkStatus , PutDriveOutcome ,
37- SyncRequest } ;
37+ SyncRequest , VsockJsonBody } ;
3838use api_server:: request:: sync:: boot_source:: { PutBootSourceConfigError , PutBootSourceOutcome } ;
3939use api_server:: request:: sync:: machine_configuration:: { PutMachineConfigurationError ,
4040 PutMachineConfigurationOutcome } ;
@@ -245,14 +245,16 @@ impl Default for VirtualMachineConfig {
245245}
246246
247247pub struct Vmm {
248- cfg : MachineCfg ,
248+ // TODO: no longer used apparently; refactor&remove
249+ _cfg : MachineCfg ,
249250 vm_config : VirtualMachineConfig ,
250251 core : Option < VmmCore > ,
251252 kernel_config : Option < KernelConfig > ,
252253 // If there is a Root Block Device, this should be added as the first element of the list
253254 // This is necessary because we want the root to always be mounted on /dev/vda
254255 block_device_configs : BlockDeviceConfigs ,
255256 network_interface_configs : NetworkInterfaceConfigs ,
257+ vsock_device_configs : VsockDeviceConfigs ,
256258
257259 /// api resources
258260 api_event_fd : EventFd ,
@@ -273,12 +275,13 @@ impl Vmm {
273275 . expect ( "cannot add API eventfd to epoll" ) ;
274276 let block_device_configs = BlockDeviceConfigs :: new ( ) ;
275277 Ok ( Vmm {
276- cfg,
278+ _cfg : cfg,
277279 vm_config : VirtualMachineConfig :: default ( ) ,
278280 core : None ,
279281 kernel_config : None ,
280282 block_device_configs,
281283 network_interface_configs : NetworkInterfaceConfigs :: new ( ) ,
284+ vsock_device_configs : VsockDeviceConfigs :: new ( ) ,
282285 api_event_fd,
283286 epoll_context,
284287 from_api,
@@ -394,6 +397,39 @@ impl Vmm {
394397 Ok ( ( ) )
395398 }
396399
400+ pub fn put_vsock_device (
401+ & mut self ,
402+ body : VsockJsonBody ,
403+ ) -> result:: Result < SyncOkStatus , SyncError > {
404+ self . vsock_device_configs . put ( body)
405+ }
406+
407+ fn attach_vsock_devices (
408+ & mut self ,
409+ guest_mem : & GuestMemory ,
410+ device_manager : & mut DeviceManager ,
411+ ) -> Result < ( ) > {
412+ let kernel_config = match self . kernel_config . as_mut ( ) {
413+ Some ( x) => x,
414+ None => return Err ( Error :: MissingKernelConfig ) ,
415+ } ;
416+
417+ for cfg in self . vsock_device_configs . iter ( ) {
418+ let epoll_config = self . epoll_context . allocate_virtio_vsock_tokens ( ) ;
419+
420+ let vsock_box = Box :: new ( devices:: virtio:: Vsock :: new (
421+ cfg. get_guest_cid ( ) as u64 ,
422+ guest_mem,
423+ epoll_config,
424+ ) . map_err ( Error :: CreateVirtioVsock ) ?) ;
425+ device_manager
426+ . register_mmio ( vsock_box, & mut kernel_config. cmdline )
427+ . map_err ( Error :: RegisterMMIOVsockDevice ) ?;
428+ }
429+
430+ Ok ( ( ) )
431+ }
432+
397433 pub fn configure_kernel ( & mut self , kernel_config : KernelConfig ) {
398434 self . kernel_config = Some ( kernel_config) ;
399435 }
@@ -416,6 +452,7 @@ impl Vmm {
416452
417453 self . attach_block_devices ( & mut device_manager) ?;
418454 self . attach_net_devices ( & mut device_manager) ?;
455+ self . attach_vsock_devices ( & guest_mem, & mut device_manager) ?;
419456
420457 let epoll_context = & mut self . epoll_context ;
421458
@@ -428,16 +465,6 @@ impl Vmm {
428465 None => return Err ( Error :: MissingKernelConfig ) ,
429466 } ;
430467
431- if let Some ( cid) = self . cfg . vsock_guest_cid {
432- let epoll_config = epoll_context. allocate_virtio_vsock_tokens ( ) ;
433-
434- let vsock_box = Box :: new ( devices:: virtio:: Vsock :: new ( cid, & guest_mem, epoll_config)
435- . map_err ( Error :: CreateVirtioVsock ) ?) ;
436- device_manager
437- . register_mmio ( vsock_box, & mut kernel_config. cmdline )
438- . map_err ( Error :: RegisterMMIOVsockDevice ) ?;
439- }
440-
441468 let kvm = Kvm :: new ( ) . map_err ( Error :: Kvm ) ?;
442469 let vm = Vm :: new ( & kvm, guest_mem) . map_err ( Error :: Vm ) ?;
443470
@@ -798,10 +825,13 @@ impl Vmm {
798825 . send ( Box :: new ( self . put_net_device ( body) ) )
799826 . map_err ( |_| ( ) )
800827 . expect ( "one-shot channel closed" ) ,
801- _ => unreachable ! ( ) ,
802- } ;
828+ SyncRequest :: PutVsock ( body, outcome_sender) => outcome_sender
829+ . send ( Box :: new ( self . put_vsock_device ( body) ) )
830+ . map_err ( |_| ( ) )
831+ . expect ( "one-shot channel closed" ) ,
832+ }
803833 }
804- } ;
834+ }
805835
806836 Ok ( ( ) )
807837 }
0 commit comments