Skip to content

Commit 7b4b509

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 5ed78a8 commit 7b4b509

File tree

6 files changed

+33
-39
lines changed

6 files changed

+33
-39
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -952,16 +952,16 @@ impl VirtioDevice for Balloon {
952952
}
953953

954954
fn kick(&mut self) {
955-
// If device is activated, kick the balloon queue(s) to make up for any
956-
// pending or in-flight epoll events we may have not captured in snapshot.
957-
// Stats queue doesn't need kicking as it is notified via a `timer_fd`.
958955
if self.is_activated() {
959-
info!("kick balloon {}.", self.id());
960956
if self.free_page_hinting() {
961-
// On restore we reset back to DONE to ensure everythign is freed
957+
info!(
958+
"[{:?}:{}] resetting free page hinting to DONE",
959+
self.device_type(),
960+
self.id()
961+
);
962962
self.update_free_page_hint_cmd(FREE_PAGE_HINT_DONE);
963963
}
964-
self.process_virtio_queues();
964+
self.notify_queue_events();
965965
}
966966
}
967967
}

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: 22 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
@@ -188,8 +188,28 @@ pub trait VirtioDevice: AsAny + Send {
188188
Ok(())
189189
}
190190

191+
/// Notify all queues by writing to the eventfds.
192+
fn notify_queue_events(&mut self) {
193+
info!("[{:?}:{}] notifying queues", self.device_type(), self.id());
194+
for (i, eventfd) in self.queue_events().iter().enumerate() {
195+
if let Err(err) = eventfd.write(1) {
196+
error!(
197+
"[{:?}:{}] error notifying queue {}: {}",
198+
self.device_type(),
199+
self.id(),
200+
i,
201+
err
202+
);
203+
}
204+
}
205+
}
206+
191207
/// Kick the device, as if it had received external events.
192-
fn kick(&mut self) {}
208+
fn kick(&mut self) {
209+
if self.is_activated() {
210+
self.notify_queue_events();
211+
}
212+
}
193213
}
194214

195215
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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,11 @@ where
385385
// The only reason we still `kick` it is to make guest process
386386
// `TRANSPORT_RESET_EVENT` event we sent during snapshot creation.
387387
if self.is_activated() {
388-
info!("kick vsock {}.", self.id());
388+
info!(
389+
"[{:?}:{}] signaling event queue",
390+
self.device_type(),
391+
self.id()
392+
);
389393
self.signal_used_queue(EVQ_INDEX).unwrap();
390394
}
391395
}

0 commit comments

Comments
 (0)