Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reset for net device #4389

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 @@ -13,7 +13,7 @@ use utils::eventfd::EventFd;

use super::mmio::{VIRTIO_MMIO_INT_CONFIG, VIRTIO_MMIO_INT_VRING};
use super::queue::Queue;
use super::ActivateError;
use super::{ActivateError, ResetError};
use crate::devices::virtio::AsAny;
use crate::logger::{error, warn};
use crate::vstate::memory::GuestMemoryMmap;
Expand Down Expand Up @@ -174,10 +174,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 +178 to +179
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.

}
}

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 @@ -201,8 +201,12 @@
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;
}

Check warning on line 207 in src/vmm/src/devices/virtio/mmio.rs

View check run for this annotation

Codecov / codecov/patch

src/vmm/src/devices/virtio/mmio.rs#L204-L207

Added lines #L204 - L207 were not covered by tests
Err(e) => {
warn!("failed to reset virtio device: {:?}", e);
device_status |= FAILED;
}
}
Expand Down Expand Up @@ -469,7 +473,7 @@
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());
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 @@ -70,6 +70,13 @@
VhostUser(vhost_user::VhostUserError),
}

// Errors triggered when resetting a VirtioDevice.
#[derive(Debug, thiserror::Error, displaydoc::Display)]

Check warning on line 74 in src/vmm/src/devices/virtio/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/vmm/src/devices/virtio/mod.rs#L74

Added line #L74 was not covered by tests
pub enum ResetError {
/// 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
41 changes: 31 additions & 10 deletions src/vmm/src/devices/virtio/net/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::devices::virtio::net::{
gen, NetError, NetQueue, MAX_BUFFER_SIZE, NET_QUEUE_SIZES, RX_INDEX, TX_INDEX,
};
use crate::devices::virtio::queue::{DescriptorChain, Queue};
use crate::devices::virtio::{ActivateError, TYPE_NET};
use crate::devices::virtio::{ActivateError, ResetError, TYPE_NET};
use crate::devices::{report_net_event_fail, DeviceError};
use crate::dumbo::pdu::arp::ETH_IPV4_FRAME_LEN;
use crate::dumbo::pdu::ethernet::{EthernetFrame, PAYLOAD_OFFSET};
Expand Down Expand Up @@ -870,6 +870,15 @@ impl VirtioDevice for Net {
fn is_activated(&self) -> bool {
self.device_state.is_activated()
}

fn reset(&mut self) -> Result<(), ResetError> {
self.device_state = DeviceState::Inactive;
self.rx_bytes_read = 0;
self.rx_deferred_frame = false;
self.rx_frame_buf = [0u8; MAX_BUFFER_SIZE];
self.metrics = NetMetricsPerDevice::alloc(self.id.clone());
Comment on lines +875 to +879
Copy link
Contributor

Choose a reason for hiding this comment

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

We definitely need to reset the acked_features here, too.

Also, @sudanl0 do you think it is ok to reset the metrics here? I think it's more of a philosophical question, on whether we want our metrics to reflect the fact that the device has been reset by zeroing them out. Maybe, it would make sense to add a reset counter metric as well.

Ok(())
}
}

#[cfg(test)]
Expand Down Expand Up @@ -2015,17 +2024,29 @@ pub mod tests {
th.activate_net();
let net = th.net.lock().unwrap();

// Test queues count (TX and RX).
let queues = net.queues();
assert_eq!(queues.len(), NET_QUEUE_SIZES.len());
assert_eq!(queues[RX_INDEX].size, th.rxq.size());
assert_eq!(queues[TX_INDEX].size, th.txq.size());
let validate = |net: &Net| {
// Test queues count (TX and RX).
let queues = net.queues();
assert_eq!(queues.len(), NET_QUEUE_SIZES.len());
assert_eq!(queues[RX_INDEX].size, th.rxq.size());
assert_eq!(queues[TX_INDEX].size, th.txq.size());

// Test corresponding queues events.
assert_eq!(net.queue_events().len(), NET_QUEUE_SIZES.len());

// Test interrupts.
assert!(!&net.irq_trigger.has_pending_irq(IrqType::Vring));
};

validate(&net);

// Test corresponding queues events.
assert_eq!(net.queue_events().len(), NET_QUEUE_SIZES.len());
// Test reset.
let mut net = net;
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess you can just mark as mut the net device at line 2025?

assert!(net.device_state.is_activated());
net.reset().unwrap();
assert!(!net.device_state.is_activated());

// Test interrupts.
assert!(!&net.irq_trigger.has_pending_irq(IrqType::Vring));
validate(&net);
}

#[test]
Expand Down