Skip to content

Commit

Permalink
vmm: Implement basic reset capability for net device
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Jensen <[email protected]>
  • Loading branch information
acj committed Jan 31, 2024
1 parent 3f548cf commit 32cde21
Showing 1 changed file with 31 additions and 10 deletions.
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());
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;
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

0 comments on commit 32cde21

Please sign in to comment.