Skip to content

Commit 99365ff

Browse files
committed
refactor: remove unnecessary usage of anyhow
Prefer proper error types over ambiguous anyhow errors. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent a69bca6 commit 99365ff

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use std::io::{ErrorKind, Write};
1414
use std::sync::atomic::{AtomicBool, AtomicU16, AtomicU32, AtomicUsize, Ordering};
1515
use std::sync::{Arc, Barrier, Mutex};
1616

17-
use anyhow::anyhow;
1817
use kvm_ioctls::{IoEventAddress, NoDatamatch};
1918
use log::warn;
2019
use pci::{
@@ -39,7 +38,7 @@ use crate::devices::virtio::transport::{VirtioInterrupt, VirtioInterruptType};
3938
use crate::logger::{debug, error};
4039
use crate::pci::configuration::{PciCapability, PciConfiguration, PciConfigurationState};
4140
use crate::pci::msix::{MsixCap, MsixConfig, MsixConfigState};
42-
use crate::pci::{BarReprogrammingParams, PciDevice};
41+
use crate::pci::{BarReprogrammingParams, DeviceRelocationError, PciDevice};
4342
use crate::snapshot::Persist;
4443
use crate::utils::u64_to_usize;
4544
use crate::vstate::bus::BusDevice;
@@ -251,7 +250,7 @@ pub struct VirtioPciDeviceState {
251250
#[derive(Debug, thiserror::Error, displaydoc::Display)]
252251
pub enum VirtioPciDeviceError {
253252
/// Failed creating VirtioPciDevice: {0}
254-
CreateVirtioPciDevice(#[from] anyhow::Error),
253+
CreateVirtioPciDevice(#[from] DeviceRelocationError),
255254
/// Error creating MSI configuration: {0}
256255
Msi(#[from] InterruptError),
257256
}
@@ -782,7 +781,7 @@ impl PciDevice for VirtioPciDevice {
782781
self.configuration.detect_bar_reprogramming(reg_idx, data)
783782
}
784783

785-
fn move_bar(&mut self, old_base: u64, new_base: u64) -> Result<(), anyhow::Error> {
784+
fn move_bar(&mut self, old_base: u64, new_base: u64) -> Result<(), DeviceRelocationError> {
786785
// We only update our idea of the bar in order to support free_bars() above.
787786
// The majority of the reallocation is done inside DeviceManager.
788787
if self.bar_address == old_base {

src/vmm/src/pci/bus.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ mod tests {
458458
use super::{PciBus, PciConfigIo, PciConfigMmio, PciRoot};
459459
use crate::pci::bus::{DEVICE_ID_INTEL_VIRT_PCIE_HOST, VENDOR_ID_INTEL};
460460
use crate::pci::configuration::PciConfiguration;
461-
use crate::pci::{BarReprogrammingParams, DeviceRelocation, PciDevice};
461+
use crate::pci::{BarReprogrammingParams, DeviceRelocation, DeviceRelocationError, PciDevice};
462462
use crate::vstate::bus::BusDevice;
463463

464464
#[derive(Debug, Default)]
@@ -479,7 +479,7 @@ mod tests {
479479
_new_base: u64,
480480
_len: u64,
481481
_pci_dev: &mut dyn PciDevice,
482-
) -> Result<(), anyhow::Error> {
482+
) -> Result<(), DeviceRelocationError> {
483483
self.reloc_cnt
484484
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
485485
Ok(())

src/vmm/src/pci/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,18 @@ pub trait PciDevice: Send {
5959
None
6060
}
6161
/// Relocates the BAR to a different address in guest address space.
62-
fn move_bar(&mut self, _old_base: u64, _new_base: u64) -> Result<(), anyhow::Error> {
62+
fn move_bar(&mut self, _old_base: u64, _new_base: u64) -> Result<(), DeviceRelocationError> {
6363
Ok(())
6464
}
6565
}
6666

67+
/// Errors for device manager.
68+
#[derive(Debug, thiserror::Error, displaydoc::Display)]
69+
pub enum DeviceRelocationError {
70+
/// Device relocation not supported.
71+
NotSupported,
72+
}
73+
6774
/// This trait defines a set of functions which can be triggered whenever a
6875
/// PCI device is modified in any way.
6976
pub trait DeviceRelocation: Send + Sync {
@@ -75,5 +82,5 @@ pub trait DeviceRelocation: Send + Sync {
7582
new_base: u64,
7683
len: u64,
7784
pci_dev: &mut dyn PciDevice,
78-
) -> Result<(), anyhow::Error>;
85+
) -> Result<(), DeviceRelocationError>;
7986
}

src/vmm/src/vstate/vm.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use std::io::Write;
1111
use std::path::Path;
1212
use std::sync::{Arc, Mutex, MutexGuard};
1313

14-
use anyhow::anyhow;
1514
#[cfg(target_arch = "x86_64")]
1615
use kvm_bindings::KVM_IRQCHIP_IOAPIC;
1716
use kvm_bindings::{
@@ -27,7 +26,7 @@ use vmm_sys_util::eventfd::EventFd;
2726
pub use crate::arch::{ArchVm as Vm, ArchVmError, VmState};
2827
use crate::arch::{GSI_MSI_END, host_page_size};
2928
use crate::logger::info;
30-
use crate::pci::{DeviceRelocation, PciDevice};
29+
use crate::pci::{DeviceRelocation, DeviceRelocationError, PciDevice};
3130
use crate::persist::CreateSnapshotError;
3231
use crate::utils::u64_to_usize;
3332
use crate::vmm_config::snapshot::SnapshotType;
@@ -481,8 +480,8 @@ impl DeviceRelocation for Vm {
481480
_new_base: u64,
482481
_len: u64,
483482
_pci_dev: &mut dyn PciDevice,
484-
) -> Result<(), anyhow::Error> {
485-
Err(anyhow!("pci: device relocation not supported"))
483+
) -> Result<(), DeviceRelocationError> {
484+
Err(DeviceRelocationError::NotSupported)
486485
}
487486
}
488487

0 commit comments

Comments
 (0)