mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2025-01-22 04:25:21 +00:00
vitio-devices: watchdog: Avoid panic and propagate errors properly
Signed-off-by: Bo Chen <chen.bo@intel.com>
This commit is contained in:
parent
1c1bff93a1
commit
a4f781e142
@ -24,6 +24,7 @@ use std::result;
|
|||||||
use std::sync::atomic::AtomicBool;
|
use std::sync::atomic::AtomicBool;
|
||||||
use std::sync::{Arc, Barrier, Mutex};
|
use std::sync::{Arc, Barrier, Mutex};
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
use thiserror::Error;
|
||||||
use versionize::{VersionMap, Versionize, VersionizeResult};
|
use versionize::{VersionMap, Versionize, VersionizeResult};
|
||||||
use versionize_derive::Versionize;
|
use versionize_derive::Versionize;
|
||||||
use virtio_queue::{Queue, QueueT};
|
use virtio_queue::{Queue, QueueT};
|
||||||
@ -47,6 +48,16 @@ const WATCHDOG_TIMER_INTERVAL: i64 = 15;
|
|||||||
// Number of seconds since last ping to trigger reboot
|
// Number of seconds since last ping to trigger reboot
|
||||||
const WATCHDOG_TIMEOUT: u64 = WATCHDOG_TIMER_INTERVAL as u64 + 5;
|
const WATCHDOG_TIMEOUT: u64 = WATCHDOG_TIMER_INTERVAL as u64 + 5;
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
enum Error {
|
||||||
|
#[error("Error programming timer fd: {0}")]
|
||||||
|
TimerfdSetup(io::Error),
|
||||||
|
#[error("Descriptor chain too short")]
|
||||||
|
DescriptorChainTooShort,
|
||||||
|
#[error("Failed adding used index: {0}")]
|
||||||
|
QueueAddUsed(virtio_queue::Error),
|
||||||
|
}
|
||||||
|
|
||||||
struct WatchdogEpollHandler {
|
struct WatchdogEpollHandler {
|
||||||
mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
||||||
queue: Queue,
|
queue: Queue,
|
||||||
@ -62,12 +73,12 @@ struct WatchdogEpollHandler {
|
|||||||
impl WatchdogEpollHandler {
|
impl WatchdogEpollHandler {
|
||||||
// The main queue is very simple - the driver "pings" the device by passing it a (write-only)
|
// The main queue is very simple - the driver "pings" the device by passing it a (write-only)
|
||||||
// descriptor. In response the device writes a 1 into the descriptor and returns it to the driver
|
// descriptor. In response the device writes a 1 into the descriptor and returns it to the driver
|
||||||
fn process_queue(&mut self) -> bool {
|
fn process_queue(&mut self) -> result::Result<bool, Error> {
|
||||||
let queue = &mut self.queue;
|
let queue = &mut self.queue;
|
||||||
let mut used_desc_heads = [(0, 0); QUEUE_SIZE as usize];
|
let mut used_desc_heads = [(0, 0); QUEUE_SIZE as usize];
|
||||||
let mut used_count = 0;
|
let mut used_count = 0;
|
||||||
while let Some(mut desc_chain) = queue.pop_descriptor_chain(self.mem.memory()) {
|
while let Some(mut desc_chain) = queue.pop_descriptor_chain(self.mem.memory()) {
|
||||||
let desc = desc_chain.next().unwrap();
|
let desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?;
|
||||||
|
|
||||||
let mut len = 0;
|
let mut len = 0;
|
||||||
|
|
||||||
@ -79,9 +90,8 @@ impl WatchdogEpollHandler {
|
|||||||
"First ping received. Starting timer (every {} seconds)",
|
"First ping received. Starting timer (every {} seconds)",
|
||||||
WATCHDOG_TIMER_INTERVAL
|
WATCHDOG_TIMER_INTERVAL
|
||||||
);
|
);
|
||||||
if let Err(e) = timerfd_setup(&self.timer, WATCHDOG_TIMER_INTERVAL) {
|
timerfd_setup(&self.timer, WATCHDOG_TIMER_INTERVAL)
|
||||||
error!("Error programming timer fd: {:?}", e);
|
.map_err(Error::TimerfdSetup)?;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
self.last_ping_time.lock().unwrap().replace(Instant::now());
|
self.last_ping_time.lock().unwrap().replace(Instant::now());
|
||||||
}
|
}
|
||||||
@ -92,9 +102,11 @@ impl WatchdogEpollHandler {
|
|||||||
|
|
||||||
let mem = self.mem.memory();
|
let mem = self.mem.memory();
|
||||||
for &(desc_index, len) in &used_desc_heads[..used_count] {
|
for &(desc_index, len) in &used_desc_heads[..used_count] {
|
||||||
queue.add_used(mem.deref(), desc_index, len).unwrap();
|
queue
|
||||||
|
.add_used(mem.deref(), desc_index, len)
|
||||||
|
.map_err(Error::QueueAddUsed)?;
|
||||||
}
|
}
|
||||||
used_count > 0
|
Ok(used_count > 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn signal_used_queue(&self) -> result::Result<(), DeviceError> {
|
fn signal_used_queue(&self) -> result::Result<(), DeviceError> {
|
||||||
@ -133,7 +145,10 @@ impl EpollHelperHandler for WatchdogEpollHandler {
|
|||||||
EpollHelperError::HandleEvent(anyhow!("Failed to get queue event: {:?}", e))
|
EpollHelperError::HandleEvent(anyhow!("Failed to get queue event: {:?}", e))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
if self.process_queue() {
|
let needs_notification = self.process_queue().map_err(|e| {
|
||||||
|
EpollHelperError::HandleEvent(anyhow!("Failed to process queue : {:?}", e))
|
||||||
|
})?;
|
||||||
|
if needs_notification {
|
||||||
self.signal_used_queue().map_err(|e| {
|
self.signal_used_queue().map_err(|e| {
|
||||||
EpollHelperError::HandleEvent(anyhow!(
|
EpollHelperError::HandleEvent(anyhow!(
|
||||||
"Failed to signal used queue: {:?}",
|
"Failed to signal used queue: {:?}",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user