Skip to content

Commit

Permalink
ref(event_manger): pass the event_manager to an Option
Browse files Browse the repository at this point in the history
As only devices that are implemented in VMM / Device Model
need an event manager in order to handle guest-to-host
notifications, this commit only does so in these situations:

- The thread responsible for running the event manager run
loop is only created for this type of devices
- The event manager object is only created for this type of
devices

Signed-off-by: joaopeixoto13 <[email protected]>
  • Loading branch information
joaopeixoto13 committed May 17, 2024
1 parent 668a993 commit 92b3e3e
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/virtio/src/block/virtio/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl VirtioDeviceT for VirtioBlock {
fn new(
config: &DeviceConfig,
device_manager: Arc<Mutex<IoManager>>,
event_manager: Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>,
event_manager: Option<Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>>,
device_model: Arc<Mutex<BaoDeviceModel>>,
) -> Result<Arc<Mutex<Self>>> {
// Extract the generic features and queues.
Expand All @@ -67,7 +67,7 @@ impl VirtioDeviceT for VirtioBlock {
let common_device = VirtioDeviceCommon::new(config, device_model, virtio_cfg).unwrap();

// Create a remote endpoint object, that allows interacting with the VM EventManager from a different thread.
let remote_endpoint = event_manager.lock().unwrap().remote_endpoint();
let remote_endpoint = event_manager.unwrap().lock().unwrap().remote_endpoint();

// Create the block device.
let block = Arc::new(Mutex::new(VirtioBlock {
Expand Down
2 changes: 1 addition & 1 deletion src/virtio/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ pub trait VirtioDeviceT {
fn new(
config: &DeviceConfig,
device_manager: Arc<Mutex<IoManager>>,
event_manager: Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>,
event_manager: Option<Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>>,
device_model: Arc<Mutex<BaoDeviceModel>>,
) -> Result<Arc<Mutex<Self>>>;

Expand Down
2 changes: 1 addition & 1 deletion src/virtio/src/fs/vhost_user/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl VirtioDeviceT for VhostUserFs {
fn new(
config: &DeviceConfig,
device_manager: Arc<Mutex<IoManager>>,
_event_manager: Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>,
_event_manager: Option<Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>>,
device_model: Arc<Mutex<BaoDeviceModel>>,
) -> Result<Arc<Mutex<Self>>> {
// Extract the generic features and queues.
Expand Down
2 changes: 1 addition & 1 deletion src/virtio/src/net/vhost/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl VirtioDeviceT for VhostNet {
fn new(
config: &DeviceConfig,
device_manager: Arc<Mutex<IoManager>>,
_event_manager: Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>,
_event_manager: Option<Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>>,
device_model: Arc<Mutex<BaoDeviceModel>>,
) -> Result<Arc<Mutex<Self>>> {
// Extract the generic features and queues.
Expand Down
4 changes: 2 additions & 2 deletions src/virtio/src/net/virtio/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl VirtioDeviceT for VirtioNet {
fn new(
config: &DeviceConfig,
device_manager: Arc<Mutex<IoManager>>,
event_manager: Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>,
event_manager: Option<Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>>,
device_model: Arc<Mutex<BaoDeviceModel>>,
) -> Result<Arc<Mutex<Self>>> {
// Extract the generic features and queues.
Expand All @@ -64,7 +64,7 @@ impl VirtioDeviceT for VirtioNet {
let common_device = VirtioDeviceCommon::new(config, device_model, virtio_cfg).unwrap();

// Create a remote endpoint object, that allows interacting with the VM EventManager from a different thread.
let remote_endpoint = event_manager.lock().unwrap().remote_endpoint();
let remote_endpoint = event_manager.unwrap().lock().unwrap().remote_endpoint();

// Create the net device.
let net = Arc::new(Mutex::new(VirtioNet {
Expand Down
2 changes: 1 addition & 1 deletion src/virtio/src/vsock/vhost/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl VirtioDeviceT for VhostVsockDevice {
fn new(
config: &DeviceConfig,
device_manager: Arc<Mutex<IoManager>>,
_event_manager: Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>,
_event_manager: Option<Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>>,
device_model: Arc<Mutex<BaoDeviceModel>>,
) -> Result<Arc<Mutex<Self>>> {
// Extract the generic features and queues.
Expand Down
2 changes: 1 addition & 1 deletion src/virtio/src/vsock/vhost_user/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl VirtioDeviceT for VhostUserVsock {
fn new(
config: &DeviceConfig,
device_manager: Arc<Mutex<IoManager>>,
_event_manager: Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>,
_event_manager: Option<Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>>,
device_model: Arc<Mutex<BaoDeviceModel>>,
) -> Result<Arc<Mutex<Self>>> {
// Extract the generic features and queues.
Expand Down
4 changes: 2 additions & 2 deletions src/virtio/src/vsock/virtio/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl VirtioDeviceT for VirtioVsock {
fn new(
config: &DeviceConfig,
device_manager: Arc<Mutex<IoManager>>,
event_manager: Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>,
event_manager: Option<Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>>,
device_model: Arc<Mutex<BaoDeviceModel>>,
) -> Result<Arc<Mutex<Self>>> {
// Extract the generic features and queues.
Expand All @@ -52,7 +52,7 @@ impl VirtioDeviceT for VirtioVsock {
let common_device = VirtioDeviceCommon::new(config, device_model, virtio_cfg).unwrap();

// Create a remote endpoint object, that allows interacting with the VM EventManager from a different thread.
let remote_endpoint = event_manager.lock().unwrap().remote_endpoint();
let remote_endpoint = event_manager.unwrap().lock().unwrap().remote_endpoint();

// Create the vsock device.
let vsock = Arc::new(Mutex::new(VirtioVsock {
Expand Down
24 changes: 17 additions & 7 deletions src/vmm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Vm {
device_model: Arc<Mutex<BaoDeviceModel>>,
devices: Vec<VirtioDeviceType>,
device_manager: Arc<Mutex<IoManager>>,
event_manager: Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>,
pub event_manager: Option<Arc<Mutex<EventManager<Arc<Mutex<dyn MutEventSubscriber + Send>>>>>>,
}

impl Vm {
Expand All @@ -47,11 +47,15 @@ impl Vm {
// Create the device manager.
let device_manager = Arc::new(Mutex::new(IoManager::new()));

// Create the event manager.
let event_manager = Arc::new(Mutex::new(
EventManager::<Arc<Mutex<dyn MutEventSubscriber + Send>>>::new()
.map_err(Error::EventManager)?,
));
// Create the event manager if the data plane is virtio.
let event_manager = if config.data_plane == "virtio" {
Some(Arc::new(Mutex::new(
EventManager::<Arc<Mutex<dyn MutEventSubscriber + Send>>>::new()
.map_err(Error::EventManager)?,
)))
} else {
None
};

// Create the VM.
let mut vm = Vm {
Expand Down Expand Up @@ -239,7 +243,13 @@ impl Vm {
/// device.
pub fn run_event_manager(self: Arc<Self>) {
loop {
self.event_manager.lock().unwrap().run().unwrap();
self.event_manager
.as_ref()
.unwrap()
.lock()
.unwrap()
.run()
.unwrap();
}
}
}
Expand Down
20 changes: 11 additions & 9 deletions src/vmm/src/vmm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,17 @@ impl Vmm {
);

// Create a new vCPU/thread to run the VM event manager.
let vm_evm = vm.clone();
self.vcpus.lock().unwrap().push(
Builder::new()
.name(format!("vm_{}_evm", vm_evm.id))
.spawn(move || {
vm_evm.run_event_manager();
})
.unwrap(),
);
if vm.event_manager.is_some() {
let vm_evm = vm.clone();
self.vcpus.lock().unwrap().push(
Builder::new()
.name(format!("vm_{}_evm", vm_evm.id))
.spawn(move || {
vm_evm.run_event_manager();
})
.unwrap(),
);
}
}
Ok(())
}
Expand Down

0 comments on commit 92b3e3e

Please sign in to comment.