Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/vmm/src/devices/virtio/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::sync::atomic::{AtomicU32, Ordering};

use vmm_sys_util::eventfd::EventFd;

use super::ActivateError;
use super::{ActivateError, ResetError};
use super::mmio::{VIRTIO_MMIO_INT_CONFIG, VIRTIO_MMIO_INT_VRING};
use super::queue::{Queue, QueueError};
use crate::devices::virtio::AsAny;
Expand Down Expand Up @@ -175,10 +175,9 @@ pub trait VirtioDevice: AsAny + Send {
/// Checks if the resources of this device are activated.
fn is_activated(&self) -> bool;

/// Optionally deactivates this device and returns ownership of the guest memory map, interrupt
/// event, and queue events.
fn reset(&mut self) -> Option<(EventFd, Vec<EventFd>)> {
None
/// Optionally deactivates this device.
fn reset(&mut self) -> Result<(), ResetError> {
Err(ResetError::NotImplemented)
Comment on lines +179 to +180
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's what I had in mind honestly. I assume the initial idea of giving back the EventFds was to "ensure" through the type system that they can't be used, but that is a very dubious choice, IMHO.

}

/// Mark pages used by queues as dirty.
Expand Down
10 changes: 7 additions & 3 deletions src/vmm/src/devices/virtio/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,12 @@ impl MmioTransport {
let mut device_status = self.device_status;
let reset_result = self.locked_device().reset();
match reset_result {
Some((_interrupt_evt, mut _queue_evts)) => {}
None => {
Ok(_) => {
// The device MUST initialize device status to 0 upon reset.
device_status = INIT;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I'd actually use 0 here as per the spec. The device_status mod definition is a bit weird actually. Most values are supposed to be individual bits, but 0 is a value for the entire register, and 'INIT' is not a name that appears anywhere on the spec. But that doesn't have to do with your changes, just what I noticed by looking at that code, it might be better to refactor it eventually.

}
Err(e) => {
warn!("failed to reset virtio device: {:?}", e);
device_status |= FAILED;
}
}
Expand Down Expand Up @@ -471,7 +475,7 @@ pub(crate) mod tests {
let m = single_region_mem(0x1000);
let mut dummy = DummyDevice::new();
// Validate reset is no-op.
assert!(dummy.reset().is_none());
assert!(dummy.reset().is_err());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd check for the exact error code here

let mut d = MmioTransport::new(m, Arc::new(Mutex::new(dummy)), false);

// We just make sure here that the implementation of a mmio device behaves as we expect,
Expand Down
7 changes: 7 additions & 0 deletions src/vmm/src/devices/virtio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ pub enum ActivateError {
QueueMemoryError(QueueError),
}

// Errors triggered when resetting a VirtioDevice.
#[derive(Debug, thiserror::Error, displaydoc::Display)]
pub enum ResetError {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd merge this with the second patch where the error code is actually used

/// Reset is not implemented for the device.
NotImplemented,
}

/// Trait that helps in upcasting an object to Any
pub trait AsAny {
/// Return the immutable any encapsulated object.
Expand Down