xref: /cloud-hypervisor/hypervisor/src/device.rs (revision b440cb7d2330770cd415b63544a371d4caa2db3a)
1 // Copyright © 2019 Intel Corporation
2 //
3 // SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
4 //
5 // Copyright © 2020, Microsoft Corporation
6 //
7 // Copyright 2018-2019 CrowdStrike, Inc.
8 //
9 // Copyright 2020, ARM Limited
10 //
11 
12 use crate::DeviceAttr;
13 use std::any::Any;
14 use std::os::unix::io::AsRawFd;
15 use thiserror::Error;
16 
17 #[derive(Error, Debug)]
18 ///
19 /// Enum for device error
20 pub enum HypervisorDeviceError {
21     ///
22     /// Set device attribute error
23     ///
24     #[error("Failed to set device attribute: {0}")]
25     SetDeviceAttribute(#[source] anyhow::Error),
26     ///
27     /// Get device attribute error
28     ///
29     #[error("Failed to get device attribute: {0}")]
30     GetDeviceAttribute(#[source] anyhow::Error),
31 }
32 
33 ///
34 /// Result type for returning from a function
35 ///
36 pub type Result<T> = std::result::Result<T, HypervisorDeviceError>;
37 
38 ///
39 /// Trait to represent a device
40 ///
41 /// This crate provides a hypervisor-agnostic interfaces for device
42 ///
43 pub trait Device: Send + Sync + AsRawFd {
44     /// Set device attribute.
45     fn set_device_attr(&self, attr: &DeviceAttr) -> Result<()>;
46     /// Get device attribute.
47     fn get_device_attr(&self, attr: &mut DeviceAttr) -> Result<()>;
48     /// Provide a way to downcast to the device fd.
49     fn as_any(&self) -> &dyn Any;
50 }
51