Skip to content

Commit bb8fb62

Browse files
committed
refactor: centralize activation failure logging
Log activation failures at the only call-site of `activate`, instead of inside each individual `activate` function. For this, untangle some of the `ActivationError` variants - `BadActivate` was almost exclusively used in the case where writing to the activation eventfd failed, except in the vsock device, where it was also used to indicate that the number of queues the guest gave us was wrong (which this commit factors out into its own error variant). Signed-off-by: Patrick Roy <[email protected]>
1 parent adae355 commit bb8fb62

File tree

7 files changed

+31
-36
lines changed

7 files changed

+31
-36
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,9 @@ impl VirtioDevice for Balloon {
613613
fn activate(&mut self, mem: GuestMemoryMmap) -> Result<(), ActivateError> {
614614
self.device_state = DeviceState::Activated(mem);
615615
if self.activate_evt.write(1).is_err() {
616-
error!("Balloon: Cannot write to activate_evt");
617616
METRICS.activate_fails.inc();
618617
self.device_state = DeviceState::Inactive;
619-
return Err(ActivateError::BadActivate);
618+
return Err(ActivateError::EventFd);
620619
}
621620

622621
if self.stats_enabled() {

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,7 @@ impl VirtioDevice for VirtioBlock {
652652
}
653653

654654
if self.activate_evt.write(1).is_err() {
655-
error!("Block: Cannot write to activate_evt");
656-
return Err(ActivateError::BadActivate);
655+
return Err(ActivateError::EventFd);
657656
}
658657
self.device_state = DeviceState::Activated(mem);
659658
Ok(())

src/vmm/src/devices/virtio/mmio.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use utils::byte_order;
1414
use crate::devices::virtio::device::{IrqType, VirtioDevice};
1515
use crate::devices::virtio::device_status;
1616
use crate::devices::virtio::queue::Queue;
17-
use crate::logger::warn;
17+
use crate::logger::{error, warn};
1818
use crate::vstate::memory::{GuestAddress, GuestMemoryMmap};
1919

2020
// TODO crosvm uses 0 here, but IIRC virtio specified some other vendor id that should be used
@@ -186,18 +186,21 @@ impl MmioTransport {
186186
DRIVER_OK if self.device_status == (ACKNOWLEDGE | DRIVER | FEATURES_OK) => {
187187
self.device_status = status;
188188
let device_activated = self.locked_device().is_activated();
189-
if !device_activated
190-
&& self.are_queues_valid()
191-
&& self.locked_device().activate(self.mem.clone()).is_err()
192-
{
193-
self.device_status |= DEVICE_NEEDS_RESET;
194-
195-
// Section 2.1.2 of the specification states that we need to send a device
196-
// configuration change interrupt
197-
let _ = self
198-
.locked_device()
199-
.interrupt_trigger()
200-
.trigger_irq(IrqType::Config);
189+
if !device_activated && self.are_queues_valid() {
190+
// temporary variable needed for borrow checker
191+
let activate_result = self.locked_device().activate(self.mem.clone());
192+
if let Err(err) = activate_result {
193+
self.device_status |= DEVICE_NEEDS_RESET;
194+
195+
// Section 2.1.2 of the specification states that we need to send a device
196+
// configuration change interrupt
197+
let _ = self
198+
.locked_device()
199+
.interrupt_trigger()
200+
.trigger_irq(IrqType::Config);
201+
202+
error!("Failed to activate virtio device: {}", err)
203+
}
201204
}
202205
}
203206
_ if (status & FAILED) != 0 => {
@@ -462,7 +465,7 @@ pub(crate) mod tests {
462465
fn activate(&mut self, _: GuestMemoryMmap) -> Result<(), ActivateError> {
463466
self.device_activated = true;
464467
if self.activate_should_error {
465-
Err(ActivateError::BadActivate)
468+
Err(ActivateError::EventFd)
466469
} else {
467470
Ok(())
468471
}

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
//! Implements virtio devices, queues, and transport mechanisms.
99
1010
use std::any::Any;
11-
use std::io::Error as IOError;
1211

1312
pub mod balloon;
1413
pub mod block;
@@ -61,10 +60,10 @@ pub const NOTIFY_REG_OFFSET: u32 = 0x50;
6160
/// Errors triggered when activating a VirtioDevice.
6261
#[derive(Debug, thiserror::Error, displaydoc::Display)]
6362
pub enum ActivateError {
64-
/// Epoll error.
65-
EpollCtl(IOError),
66-
/// General error at activation.
67-
BadActivate,
63+
/// Wrong number of queue for virtio device: expected {expected}, got {got}
64+
QueueMismatch { expected: usize, got: usize },
65+
/// Failed to write to activate eventfd
66+
EventFd,
6867
/// Vhost user: {0}
6968
VhostUser(vhost_user::VhostUserError),
7069
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,7 @@ impl VirtioDevice for Net {
862862
}
863863

864864
if self.activate_evt.write(1).is_err() {
865-
error!("Net: Cannot write to activate_evt");
866-
return Err(super::super::ActivateError::BadActivate);
865+
return Err(ActivateError::EventFd);
867866
}
868867
self.device_state = DeviceState::Activated(mem);
869868
Ok(())

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,9 @@ impl VirtioDevice for Entropy {
284284
}
285285

286286
fn activate(&mut self, mem: GuestMemoryMmap) -> Result<(), ActivateError> {
287-
self.activate_event.write(1).map_err(|err| {
288-
error!("entropy: Cannot write to activate_evt: {err}");
287+
self.activate_event.write(1).map_err(|_| {
289288
METRICS.activate_fails.inc();
290-
super::super::ActivateError::BadActivate
289+
ActivateError::EventFd
291290
})?;
292291
self.device_state = DeviceState::Activated(mem);
293292
Ok(())

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -325,18 +325,15 @@ where
325325
fn activate(&mut self, mem: GuestMemoryMmap) -> Result<(), ActivateError> {
326326
if self.queues.len() != defs::VSOCK_NUM_QUEUES {
327327
METRICS.activate_fails.inc();
328-
error!(
329-
"Cannot perform activate. Expected {} queue(s), got {}",
330-
defs::VSOCK_NUM_QUEUES,
331-
self.queues.len()
332-
);
333-
return Err(ActivateError::BadActivate);
328+
return Err(ActivateError::QueueMismatch {
329+
expected: defs::VSOCK_NUM_QUEUES,
330+
got: self.queues.len(),
331+
});
334332
}
335333

336334
if self.activate_evt.write(1).is_err() {
337335
METRICS.activate_fails.inc();
338-
error!("Cannot write to activate_evt",);
339-
return Err(ActivateError::BadActivate);
336+
return Err(ActivateError::EventFd);
340337
}
341338

342339
self.device_state = DeviceState::Activated(mem);

0 commit comments

Comments
 (0)