Skip to main content

virtio_accel_device/
lib.rs

1//! Transport-neutral device-side state and validation.
2//!
3//! A future rust-vmm adapter will translate descriptor chains into this layer. Provider backends
4//! must never receive guest addresses or virtqueue descriptors.
5
6#![no_std]
7#![forbid(unsafe_code)]
8
9extern crate alloc;
10
11mod decoder;
12mod engine;
13mod frame;
14mod object_table;
15mod regions;
16mod response;
17mod state;
18
19pub use decoder::{
20    DecodedBinding, DecodedRequest, DecodedRequestBody, DecoderLimits, DecoderLimitsError,
21    FrameDecodeError, FrameDecoder, UnrecoverableDecodeError,
22};
23pub use engine::{
24    AcceleratorState, CommandOutcome, CommandProcessError, CommandProcessor,
25    CommandProcessorInitError, DeviceHealth, ResetDisposition, ResetError, ResetReport,
26};
27pub use frame::{FramePreflight, FramePreflightError, UnusableFrame, preflight_command_frame};
28pub use object_table::{ObjectId, ObjectKind, ObjectNamespace, ObjectTable, ObjectTableError};
29pub use regions::{
30    ChainLayout, ChainLayoutError, ChainRegion, ReadableRegion, RegionDirection,
31    SegmentedRegionError, SegmentedSink, SegmentedSource, WritableRegion, validate_chain_layout,
32};
33pub use response::{ResponsePayload, ResponseWriteError, ResponseWriter};
34pub use state::{
35    BufferCreateOutcome, BufferRecord, ChildCounts, ContextRecord, CreateError, DeviceState,
36    DeviceStateConfigError, DeviceStateError, EventRecord, ProgramRecord, QueueRecord,
37    ReleaseState, ResourceCounts, ResourcePolicy, RestoreError, RetainedBytes, SubmissionResources,
38};
39use virtio_accel_core::BackendError;
40use virtio_accel_proto::StatusCode;
41
42pub const fn status_from_backend_error(error: BackendError) -> StatusCode {
43    match error {
44        BackendError::Unsupported => StatusCode::UNSUPPORTED,
45        BackendError::Incompatible => StatusCode::INCOMPATIBLE,
46        BackendError::InvalidArgument => StatusCode::INVALID_ARGUMENT,
47        BackendError::OutOfBounds => StatusCode::OUT_OF_BOUNDS,
48        BackendError::Busy => StatusCode::BUSY,
49        BackendError::OutOfMemory => StatusCode::OUT_OF_MEMORY,
50        BackendError::ResourceLimit => StatusCode::RESOURCE_LIMIT,
51        BackendError::DeadlineExpired => StatusCode::DEADLINE_EXPIRED,
52        BackendError::DeviceLost => StatusCode::DEVICE_LOST,
53        BackendError::PermissionDenied => StatusCode::PERMISSION_DENIED,
54        BackendError::External { .. } => StatusCode::INTERNAL_ERROR,
55    }
56}
57
58pub const fn status_from_object_table_error(error: ObjectTableError) -> StatusCode {
59    match error {
60        ObjectTableError::InvalidId => StatusCode::INVALID_ARGUMENT,
61        ObjectTableError::WrongKind | ObjectTableError::StaleId => StatusCode::STALE_OBJECT,
62        ObjectTableError::Full => StatusCode::RESOURCE_LIMIT,
63        ObjectTableError::AllocationFailed => StatusCode::OUT_OF_MEMORY,
64    }
65}
66
67pub const fn status_from_device_state_error(error: DeviceStateError) -> StatusCode {
68    match error {
69        DeviceStateError::InvalidArgument | DeviceStateError::InvalidObject => {
70            StatusCode::INVALID_ARGUMENT
71        }
72        DeviceStateError::StaleObject | DeviceStateError::ContextMismatch => {
73            StatusCode::STALE_OBJECT
74        }
75        DeviceStateError::Busy | DeviceStateError::Releasing => StatusCode::BUSY,
76        DeviceStateError::ResourceLimit | DeviceStateError::ReferenceCountOverflow => {
77            StatusCode::RESOURCE_LIMIT
78        }
79        DeviceStateError::OutOfMemory => StatusCode::OUT_OF_MEMORY,
80        DeviceStateError::InvalidTransition => StatusCode::INTERNAL_ERROR,
81    }
82}