From 6713a3c859642c2652f7afaef500d2020d7ae6dc Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Wed, 24 Jun 2020 10:50:04 +0100 Subject: [PATCH] vm-virtio: net: Expose network counters through VirtioDevice Through the counters() function on the trait expose the accumulated counters. TEST=Observe that the counters from the VM match those from the tap on the host (RX-TX inverted) and inside the guest (non inverted.) Signed-off-by: Rob Bradford --- vm-virtio/src/net.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/vm-virtio/src/net.rs b/vm-virtio/src/net.rs index 5db3f173c..0cadc1c41 100644 --- a/vm-virtio/src/net.rs +++ b/vm-virtio/src/net.rs @@ -20,6 +20,7 @@ use libc::EAGAIN; use libc::EFD_NONBLOCK; use net_util::{MacAddr, Tap}; use std::cmp; +use std::collections::HashMap; use std::fs::File; use std::io::Read; use std::io::{self, Write}; @@ -739,6 +740,29 @@ impl VirtioDevice for Net { self.queue_evts.take().unwrap(), )) } + + fn counters(&self) -> Option>> { + let mut counters = HashMap::new(); + + counters.insert( + "rx_bytes", + Wrapping(self.counters.rx_bytes.load(Ordering::Acquire)), + ); + counters.insert( + "rx_frames", + Wrapping(self.counters.rx_frames.load(Ordering::Acquire)), + ); + counters.insert( + "tx_bytes", + Wrapping(self.counters.tx_bytes.load(Ordering::Acquire)), + ); + counters.insert( + "tx_frames", + Wrapping(self.counters.tx_frames.load(Ordering::Acquire)), + ); + + Some(counters) + } } virtio_ctrl_q_pausable!(Net);