vmm: Simplify epoll handling for VMM main loop

Remove the indirection of a dispatch table and simply use the enum as
the event data for the events.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2021-08-31 16:33:13 +01:00
parent d74a219add
commit 63637eba31

View File

@ -142,19 +142,36 @@ pub enum Error {
pub type Result<T> = result::Result<T, Error>; pub type Result<T> = result::Result<T, Error>;
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
#[repr(u64)]
pub enum EpollDispatch { pub enum EpollDispatch {
Exit, Exit = 0,
Reset, Reset = 1,
Stdin, Stdin = 2,
Api, Api = 3,
ActivateVirtioDevices, ActivateVirtioDevices = 4,
ConsolePty, ConsolePty = 5,
SerialPty, SerialPty = 6,
Unknown,
}
impl From<u64> for EpollDispatch {
fn from(v: u64) -> Self {
use EpollDispatch::*;
match v {
0 => Exit,
1 => Reset,
2 => Stdin,
3 => Api,
4 => ActivateVirtioDevices,
5 => ConsolePty,
6 => SerialPty,
_ => Unknown,
}
}
} }
pub struct EpollContext { pub struct EpollContext {
epoll_file: File, epoll_file: File,
dispatch_table: Vec<Option<EpollDispatch>>,
} }
impl EpollContext { impl EpollContext {
@ -163,22 +180,11 @@ impl EpollContext {
// Use 'File' to enforce closing on 'epoll_fd' // Use 'File' to enforce closing on 'epoll_fd'
let epoll_file = unsafe { File::from_raw_fd(epoll_fd) }; let epoll_file = unsafe { File::from_raw_fd(epoll_fd) };
// Initial capacity needs to be large enough to hold: Ok(EpollContext { epoll_file })
// * 1 exit event
// * 1 reset event
// * 1 stdin event
// * 1 API event
let mut dispatch_table = Vec::with_capacity(5);
dispatch_table.push(None);
Ok(EpollContext {
epoll_file,
dispatch_table,
})
} }
pub fn add_stdin(&mut self) -> result::Result<(), io::Error> { pub fn add_stdin(&mut self) -> result::Result<(), io::Error> {
let dispatch_index = self.dispatch_table.len() as u64; let dispatch_index = EpollDispatch::Stdin as u64;
epoll::ctl( epoll::ctl(
self.epoll_file.as_raw_fd(), self.epoll_file.as_raw_fd(),
epoll::ControlOptions::EPOLL_CTL_ADD, epoll::ControlOptions::EPOLL_CTL_ADD,
@ -186,8 +192,6 @@ impl EpollContext {
epoll::Event::new(epoll::Events::EPOLLIN, dispatch_index), epoll::Event::new(epoll::Events::EPOLLIN, dispatch_index),
)?; )?;
self.dispatch_table.push(Some(EpollDispatch::Stdin));
Ok(()) Ok(())
} }
@ -195,14 +199,13 @@ impl EpollContext {
where where
T: AsRawFd, T: AsRawFd,
{ {
let dispatch_index = self.dispatch_table.len() as u64; let dispatch_index = token as u64;
epoll::ctl( epoll::ctl(
self.epoll_file.as_raw_fd(), self.epoll_file.as_raw_fd(),
epoll::ControlOptions::EPOLL_CTL_ADD, epoll::ControlOptions::EPOLL_CTL_ADD,
fd.as_raw_fd(), fd.as_raw_fd(),
epoll::Event::new(epoll::Events::EPOLLIN, dispatch_index), epoll::Event::new(epoll::Events::EPOLLIN, dispatch_index),
)?; )?;
self.dispatch_table.push(Some(token));
Ok(()) Ok(())
} }
@ -1262,10 +1265,12 @@ impl Vmm {
}; };
for event in events.iter().take(num_events) { for event in events.iter().take(num_events) {
let dispatch_idx = event.data as usize; let dispatch_event: EpollDispatch = event.data.into();
match dispatch_event {
if let Some(dispatch_type) = self.epoll.dispatch_table[dispatch_idx] { EpollDispatch::Unknown => {
match dispatch_type { let event = event.data;
warn!("Unknown VMM loop event: {}", event);
}
EpollDispatch::Exit => { EpollDispatch::Exit => {
info!("VM exit event"); info!("VM exit event");
// Consume the event. // Consume the event.
@ -1492,9 +1497,7 @@ impl Vmm {
} }
ApiRequest::VmReceiveMigration(receive_migration_data, sender) => { ApiRequest::VmReceiveMigration(receive_migration_data, sender) => {
let response = self let response = self
.vm_receive_migration( .vm_receive_migration(receive_migration_data.as_ref().clone())
receive_migration_data.as_ref().clone(),
)
.map_err(ApiError::VmReceiveMigration) .map_err(ApiError::VmReceiveMigration)
.map(|_| ApiResponsePayload::Empty); .map(|_| ApiResponsePayload::Empty);
sender.send(response).map_err(Error::ApiResponseSend)?; sender.send(response).map_err(Error::ApiResponseSend)?;
@ -1519,7 +1522,6 @@ impl Vmm {
} }
} }
} }
}
Ok(()) Ok(())
} }