Skip to content

Commit ed4d917

Browse files
committed
refactor(virtio): converge kick methods to re-trigger queue events
Replace different `kick` implementations with a default implementation in the `VirtioDevice` trait. Change the `kick` logic to just re-trigger queue events instead of manually calling different internal queue processing functions. Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
1 parent 1d91776 commit ed4d917

File tree

6 files changed

+17
-55
lines changed

6 files changed

+17
-55
lines changed

src/vmm/src/devices/virtio/balloon/device.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -956,16 +956,6 @@ impl VirtioDevice for Balloon {
956956
fn is_activated(&self) -> bool {
957957
self.device_state.is_activated()
958958
}
959-
960-
fn kick(&mut self) {
961-
// If device is activated, kick the balloon queue(s) to make up for any
962-
// pending or in-flight epoll events we may have not captured in snapshot.
963-
// Stats queue doesn't need kicking as it is notified via a `timer_fd`.
964-
if self.is_activated() {
965-
info!("kick balloon {}.", self.id());
966-
self.process_virtio_queues();
967-
}
968-
}
969959
}
970960

971961
#[cfg(test)]

src/vmm/src/devices/virtio/block/device.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,6 @@ impl VirtioDevice for Block {
214214
Self::VhostUser(b) => b.device_state.is_activated(),
215215
}
216216
}
217-
218-
fn kick(&mut self) {
219-
// If device is activated, kick the block queue(s) to make up for any
220-
// pending or in-flight epoll events we may have not captured in
221-
// snapshot. No need to kick Ratelimiters
222-
// because they are restored 'unblocked' so
223-
// any inflight `timer_fd` events can be safely discarded.
224-
if self.is_activated() {
225-
info!("kick block {}.", self.id());
226-
self.process_virtio_queues();
227-
}
228-
}
229217
}
230218

231219
impl MutEventSubscriber for Block {

src/vmm/src/devices/virtio/device.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use super::queue::{Queue, QueueError};
1717
use super::transport::VirtioInterrupt;
1818
use crate::devices::virtio::AsAny;
1919
use crate::devices::virtio::generated::virtio_ids;
20-
use crate::logger::warn;
20+
use crate::logger::{error, info, warn};
2121
use crate::vstate::memory::GuestMemoryMmap;
2222

2323
/// State of an active VirtIO device
@@ -189,7 +189,22 @@ pub trait VirtioDevice: AsAny + Send {
189189
}
190190

191191
/// Kick the device, as if it had received external events.
192-
fn kick(&mut self) {}
192+
fn kick(&mut self) {
193+
if self.is_activated() {
194+
info!("[{:?}:{}] kicking queues", self.device_type(), self.id());
195+
for (i, eventfd) in self.queue_events().iter().enumerate() {
196+
if let Err(err) = eventfd.write(1) {
197+
error!(
198+
"[{:?}:{}] error kicking queue {}: {}",
199+
self.device_type(),
200+
self.id(),
201+
i,
202+
err
203+
);
204+
}
205+
}
206+
}
207+
}
193208
}
194209

195210
impl fmt::Debug for dyn VirtioDevice {

src/vmm/src/devices/virtio/net/device.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,17 +1056,6 @@ impl VirtioDevice for Net {
10561056
fn is_activated(&self) -> bool {
10571057
self.device_state.is_activated()
10581058
}
1059-
1060-
fn kick(&mut self) {
1061-
// If device is activated, kick the net queue(s) to make up for any
1062-
// pending or in-flight epoll events we may have not captured in snapshot.
1063-
// No need to kick Ratelimiters because they are restored 'unblocked' so
1064-
// any inflight `timer_fd` events can be safely discarded.
1065-
if self.is_activated() {
1066-
info!("kick net {}.", self.id());
1067-
self.process_virtio_queues();
1068-
}
1069-
}
10701059
}
10711060

10721061
#[cfg(test)]

src/vmm/src/devices/virtio/rng/device.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,6 @@ impl VirtioDevice for Entropy {
312312
self.device_state = DeviceState::Activated(ActiveState { mem, interrupt });
313313
Ok(())
314314
}
315-
316-
fn kick(&mut self) {
317-
if self.is_activated() {
318-
info!("kick entropy {}.", self.id());
319-
self.process_virtio_queues();
320-
}
321-
}
322315
}
323316

324317
#[cfg(test)]

src/vmm/src/devices/virtio/vsock/device.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -376,19 +376,6 @@ where
376376
fn is_activated(&self) -> bool {
377377
self.device_state.is_activated()
378378
}
379-
380-
fn kick(&mut self) {
381-
// Vsock has complicated protocol that isn't resilient to any packet loss,
382-
// so for Vsock we don't support connection persistence through snapshot.
383-
// Any in-flight packets or events are simply lost.
384-
// Vsock is restored 'empty'.
385-
// The only reason we still `kick` it is to make guest process
386-
// `TRANSPORT_RESET_EVENT` event we sent during snapshot creation.
387-
if self.is_activated() {
388-
info!("kick vsock {}.", self.id());
389-
self.signal_used_queue(EVQ_INDEX).unwrap();
390-
}
391-
}
392379
}
393380

394381
#[cfg(test)]

0 commit comments

Comments
 (0)