mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2025-01-03 11:25:20 +00:00
virtio-devices: balloon: Port to VirtioCommon
Use VirtioCommon to handle activate() preparation, reset() and Pausable. Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
parent
3487524950
commit
3c6fd5634d
@ -311,14 +311,7 @@ pub struct Balloon {
|
|||||||
common: VirtioCommon,
|
common: VirtioCommon,
|
||||||
id: String,
|
id: String,
|
||||||
resize: VirtioBalloonResize,
|
resize: VirtioBalloonResize,
|
||||||
kill_evt: Option<EventFd>,
|
|
||||||
pause_evt: Option<EventFd>,
|
|
||||||
config: Arc<Mutex<VirtioBalloonConfig>>,
|
config: Arc<Mutex<VirtioBalloonConfig>>,
|
||||||
queue_evts: Option<Vec<EventFd>>,
|
|
||||||
interrupt_cb: Option<Arc<dyn VirtioInterrupt>>,
|
|
||||||
epoll_threads: Option<Vec<thread::JoinHandle<()>>>,
|
|
||||||
paused: Arc<AtomicBool>,
|
|
||||||
paused_sync: Arc<Barrier>,
|
|
||||||
seccomp_action: SeccompAction,
|
seccomp_action: SeccompAction,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -332,19 +325,15 @@ impl Balloon {
|
|||||||
|
|
||||||
Ok(Balloon {
|
Ok(Balloon {
|
||||||
common: VirtioCommon {
|
common: VirtioCommon {
|
||||||
|
device_type: VirtioDeviceType::TYPE_BALLOON as u32,
|
||||||
avail_features,
|
avail_features,
|
||||||
|
paused_sync: Some(Arc::new(Barrier::new(2))),
|
||||||
|
queue_sizes: QUEUE_SIZES.to_vec(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
id,
|
id,
|
||||||
resize: VirtioBalloonResize::new()?,
|
resize: VirtioBalloonResize::new()?,
|
||||||
kill_evt: None,
|
|
||||||
pause_evt: None,
|
|
||||||
config: Arc::new(Mutex::new(config)),
|
config: Arc::new(Mutex::new(config)),
|
||||||
queue_evts: None,
|
|
||||||
interrupt_cb: None,
|
|
||||||
epoll_threads: None,
|
|
||||||
paused: Arc::new(AtomicBool::new(false)),
|
|
||||||
paused_sync: Arc::new(Barrier::new(2)),
|
|
||||||
seccomp_action,
|
seccomp_action,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -356,7 +345,7 @@ impl Balloon {
|
|||||||
|
|
||||||
impl Drop for Balloon {
|
impl Drop for Balloon {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Some(kill_evt) = self.kill_evt.take() {
|
if let Some(kill_evt) = self.common.kill_evt.take() {
|
||||||
// Ignore the result because there is nothing we can do about it.
|
// Ignore the result because there is nothing we can do about it.
|
||||||
let _ = kill_evt.write(1);
|
let _ = kill_evt.write(1);
|
||||||
}
|
}
|
||||||
@ -365,11 +354,11 @@ impl Drop for Balloon {
|
|||||||
|
|
||||||
impl VirtioDevice for Balloon {
|
impl VirtioDevice for Balloon {
|
||||||
fn device_type(&self) -> u32 {
|
fn device_type(&self) -> u32 {
|
||||||
VirtioDeviceType::TYPE_BALLOON as u32
|
self.common.device_type
|
||||||
}
|
}
|
||||||
|
|
||||||
fn queue_max_sizes(&self) -> &[u16] {
|
fn queue_max_sizes(&self) -> &[u16] {
|
||||||
QUEUE_SIZES
|
&self.common.queue_sizes
|
||||||
}
|
}
|
||||||
|
|
||||||
fn features(&self) -> u64 {
|
fn features(&self) -> u64 {
|
||||||
@ -391,43 +380,27 @@ impl VirtioDevice for Balloon {
|
|||||||
queues: Vec<Queue>,
|
queues: Vec<Queue>,
|
||||||
mut queue_evts: Vec<EventFd>,
|
mut queue_evts: Vec<EventFd>,
|
||||||
) -> ActivateResult {
|
) -> ActivateResult {
|
||||||
if queues.len() != NUM_QUEUES || queue_evts.len() != NUM_QUEUES {
|
self.common.activate(&queues, &queue_evts, &interrupt_cb)?;
|
||||||
error!(
|
let kill_evt = self
|
||||||
"Cannot perform activate. Expected {} queue(s), got {}",
|
.common
|
||||||
NUM_QUEUES,
|
.kill_evt
|
||||||
queues.len()
|
.as_ref()
|
||||||
);
|
.unwrap()
|
||||||
return Err(ActivateError::BadActivate);
|
.try_clone()
|
||||||
}
|
|
||||||
|
|
||||||
let (self_kill_evt, kill_evt) = EventFd::new(EFD_NONBLOCK)
|
|
||||||
.and_then(|e| Ok((e.try_clone()?, e)))
|
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
error!("failed creating kill EventFd pair: {}", e);
|
error!("failed to clone kill_evt eventfd: {}", e);
|
||||||
ActivateError::BadActivate
|
ActivateError::BadActivate
|
||||||
})?;
|
})?;
|
||||||
self.kill_evt = Some(self_kill_evt);
|
let pause_evt = self
|
||||||
|
.common
|
||||||
let (self_pause_evt, pause_evt) = EventFd::new(EFD_NONBLOCK)
|
.pause_evt
|
||||||
.and_then(|e| Ok((e.try_clone()?, e)))
|
.as_ref()
|
||||||
|
.unwrap()
|
||||||
|
.try_clone()
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
error!("failed creating pause EventFd pair: {}", e);
|
error!("failed to clone pause_evt eventfd: {}", e);
|
||||||
ActivateError::BadActivate
|
ActivateError::BadActivate
|
||||||
})?;
|
})?;
|
||||||
self.pause_evt = Some(self_pause_evt);
|
|
||||||
|
|
||||||
self.interrupt_cb = Some(interrupt_cb.clone());
|
|
||||||
|
|
||||||
let mut tmp_queue_evts: Vec<EventFd> = Vec::new();
|
|
||||||
for queue_evt in queue_evts.iter() {
|
|
||||||
// Save the queue EventFD as we need to return it on reset
|
|
||||||
// but clone it to pass into the thread.
|
|
||||||
tmp_queue_evts.push(queue_evt.try_clone().map_err(|e| {
|
|
||||||
error!("failed to clone queue EventFd: {}", e);
|
|
||||||
ActivateError::BadActivate
|
|
||||||
})?);
|
|
||||||
}
|
|
||||||
self.queue_evts = Some(tmp_queue_evts);
|
|
||||||
|
|
||||||
let mut handler = BalloonEpollHandler {
|
let mut handler = BalloonEpollHandler {
|
||||||
config: self.config.clone(),
|
config: self.config.clone(),
|
||||||
@ -444,8 +417,8 @@ impl VirtioDevice for Balloon {
|
|||||||
pause_evt,
|
pause_evt,
|
||||||
};
|
};
|
||||||
|
|
||||||
let paused = self.paused.clone();
|
let paused = self.common.paused.clone();
|
||||||
let paused_sync = self.paused_sync.clone();
|
let paused_sync = self.common.paused_sync.clone();
|
||||||
let mut epoll_threads = Vec::new();
|
let mut epoll_threads = Vec::new();
|
||||||
let virtio_balloon_seccomp_filter =
|
let virtio_balloon_seccomp_filter =
|
||||||
get_seccomp_filter(&self.seccomp_action, Thread::VirtioBalloon)
|
get_seccomp_filter(&self.seccomp_action, Thread::VirtioBalloon)
|
||||||
@ -455,7 +428,7 @@ impl VirtioDevice for Balloon {
|
|||||||
.spawn(move || {
|
.spawn(move || {
|
||||||
if let Err(e) = SeccompFilter::apply(virtio_balloon_seccomp_filter) {
|
if let Err(e) = SeccompFilter::apply(virtio_balloon_seccomp_filter) {
|
||||||
error!("Error applying seccomp filter: {:?}", e);
|
error!("Error applying seccomp filter: {:?}", e);
|
||||||
} else if let Err(e) = handler.run(paused, paused_sync) {
|
} else if let Err(e) = handler.run(paused, paused_sync.unwrap()) {
|
||||||
error!("Error running worker: {:?}", e);
|
error!("Error running worker: {:?}", e);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -464,31 +437,26 @@ impl VirtioDevice for Balloon {
|
|||||||
error!("failed to clone virtio-balloon epoll thread: {}", e);
|
error!("failed to clone virtio-balloon epoll thread: {}", e);
|
||||||
ActivateError::BadActivate
|
ActivateError::BadActivate
|
||||||
})?;
|
})?;
|
||||||
self.epoll_threads = Some(epoll_threads);
|
self.common.epoll_threads = Some(epoll_threads);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reset(&mut self) -> Option<(Arc<dyn VirtioInterrupt>, Vec<EventFd>)> {
|
fn reset(&mut self) -> Option<(Arc<dyn VirtioInterrupt>, Vec<EventFd>)> {
|
||||||
// We first must resume the virtio thread if it was paused.
|
self.common.reset()
|
||||||
if self.pause_evt.take().is_some() {
|
}
|
||||||
self.resume().ok()?;
|
}
|
||||||
}
|
|
||||||
|
impl Pausable for Balloon {
|
||||||
if let Some(kill_evt) = self.kill_evt.take() {
|
fn pause(&mut self) -> result::Result<(), MigratableError> {
|
||||||
// Ignore the result because there is nothing we can do about it.
|
self.common.pause()
|
||||||
let _ = kill_evt.write(1);
|
}
|
||||||
}
|
|
||||||
|
fn resume(&mut self) -> result::Result<(), MigratableError> {
|
||||||
// Return the interrupt and queue EventFDs
|
self.common.resume()
|
||||||
Some((
|
|
||||||
self.interrupt_cb.take().unwrap(),
|
|
||||||
self.queue_evts.take().unwrap(),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
virtio_pausable!(Balloon);
|
|
||||||
impl Snapshottable for Balloon {
|
impl Snapshottable for Balloon {
|
||||||
fn id(&self) -> String {
|
fn id(&self) -> String {
|
||||||
self.id.clone()
|
self.id.clone()
|
||||||
|
Loading…
Reference in New Issue
Block a user