virtio_spec/
lib.rs

1//! This crate contains the Rust equivalents of the definitions from the [Virtual I/O Device (VIRTIO) Specification](https://github.com/oasis-tcs/virtio-spec).
2//! This crate aims to be unopinionated regarding actual VIRTIO drivers that are implemented on top of this crate.
3//!
4//! # Usage
5//!
6//! We recommend to rename this crate to `virtio` when adding the dependency.
7//! This allows closely matching the specification when using definitions:
8//!
9//! - `VIRTIO_NET_F_CSUM` from the specification becomes [`virtio::net::F::CSUM`] in this crate.
10//! - `virtio_net_config` from the specification becomes [`virtio::net::Config`] in this crate.
11//!  
12//! [`virtio::net::F::CSUM`]: net::F::CSUM
13//! [`virtio::net::Config`]: net::Config
14//!
15//! Either run
16//!
17//! ```bash
18//! cargo add virtio-spec --rename virtio
19//! ```
20//!
21//! or manually edit your `Cargo.toml`:
22//!
23//! ```toml
24//! [dependencies]
25//! virtio = { package = "virtio-spec", version = "x.y.z" }
26//! ```
27//!
28//! ## Features
29//!
30//! This crate has the following Cargo features:
31//!
32//! - `alloc` enables allocating unsized structs such as [`virtq::Avail`] and [`virtq::Used`] via the [`allocator_api2`] crate.
33//! - `mmio` enables the [`mmio`] module for Virtio Over MMIO.
34//! - `nightly` enables nightly-only functionality.
35//! - `pci` enables the [`pci`] module for Virtio Over PCI via the [`pci_types`] crate.
36//! - `zerocopy` derives the following traits for most structs:
37//!   - [`zerocopy::KnownLayout`]
38//!   - [`zerocopy::Immutable`]
39//!   - [`zerocopy::FromBytes`]
40//!   - [`zerocopy::IntoBytes`]
41//!
42//! # Implementation Status
43//!
44//! This crate adds new modules by demand.
45//! If you need anything that is not available yet, please open an issue.
46//!
47//! ## Virtqueues
48//!
49//! | Virtqueue         | Available | Module     |
50//! | ----------------- | --------- | ---------- |
51//! | Split Virtqueues  | ✅        | [`virtq`]  |
52//! | Packed Virtqueues | ✅        | [`pvirtq`] |
53//!
54//! ## Transport Options
55//!
56//! | Transport Option | Available | Module   |
57//! | ---------------- | --------- | -------- |
58//! | PCI Bus          | ✅        | [`pci`]  |
59//! | MMIO             | ✅        | [`mmio`] |
60//! | Channel I/O      | ❌        |          |
61//!
62//! ## Device Types
63//!
64//! | Device Type                       | Available | Module    |
65//! | --------------------------------- | --------- | --------- |
66//! | Network Device                    | ✅        | [`net`]   |
67//! | Block Device                      | ❌        |           |
68//! | Console Device                    | ❌        |           |
69//! | Entropy Device                    | ❌        |           |
70//! | Traditional Memory Balloon Device | ❌        |           |
71//! | SCSI Host Device                  | ❌        |           |
72//! | GPU Device                        | ❌        |           |
73//! | Input Device                      | ❌        |           |
74//! | Crypto Device                     | ❌        |           |
75//! | Socket Device                     | ✅        | [`vsock`] |
76//! | File System Device                | ✅        | [`fs`]    |
77//! | RPMB Device                       | ❌        |           |
78//! | IOMMU Device                      | ❌        |           |
79//! | Sound Device                      | ❌        |           |
80//! | Memory Device                     | ❌        |           |
81//! | I2C Adapter Device                | ❌        |           |
82//! | SCMI Device                       | ❌        |           |
83//! | GPIO Device                       | ❌        |           |
84//! | PMEM Device                       | ❌        |           |
85
86#![cfg_attr(not(test), no_std)]
87#![cfg_attr(docsrs, feature(doc_auto_cfg))]
88#![cfg_attr(feature = "nightly", feature(allocator_api))]
89
90#[cfg(feature = "alloc")]
91extern crate alloc;
92
93#[macro_use]
94mod bitflags;
95#[macro_use]
96pub mod volatile;
97#[cfg(any(feature = "mmio", feature = "pci"))]
98mod driver_notifications;
99mod features;
100pub mod fs;
101#[cfg(feature = "mmio")]
102pub mod mmio;
103pub mod net;
104#[cfg(feature = "pci")]
105pub mod pci;
106pub mod pvirtq;
107pub mod virtq;
108pub mod vsock;
109
110pub use endian_num::{be128, be16, be32, be64, le128, le16, le32, le64, Be, Le};
111use num_enum::{FromPrimitive, IntoPrimitive, TryFromPrimitive};
112
113pub use self::features::{FeatureBits, F};
114
115virtio_bitflags! {
116    /// Device Status Field
117    ///
118    /// During device initialization by a driver,
119    /// the driver follows the sequence of steps specified in
120    /// _General Initialization And Device Operation / Device
121    /// Initialization_.
122    ///
123    /// The `device status` field provides a simple low-level
124    /// indication of the completed steps of this sequence.
125    /// It's most useful to imagine it hooked up to traffic
126    /// lights on the console indicating the status of each device.  The
127    /// following bits are defined (listed below in the order in which
128    /// they would be typically set):
129    pub struct DeviceStatus: u8 {
130        /// Indicates that the guest OS has found the
131        /// device and recognized it as a valid virtio device.
132        const ACKNOWLEDGE = 1;
133
134        /// Indicates that the guest OS knows how to drive the
135        /// device.
136        ///
137        /// <div class="warning">
138        ///
139        /// There could be a significant (or infinite) delay before setting
140        /// this bit.  For example, under Linux, drivers can be loadable modules.
141        ///
142        /// </div>
143        const DRIVER = 2;
144
145        /// Indicates that something went wrong in the guest,
146        /// and it has given up on the device. This could be an internal
147        /// error, or the driver didn't like the device for some reason, or
148        /// even a fatal error during device operation.
149        const FAILED = 128;
150
151        /// Indicates that the driver has acknowledged all the
152        /// features it understands, and feature negotiation is complete.
153        const FEATURES_OK = 8;
154
155        /// Indicates that the driver is set up and ready to
156        /// drive the device.
157        const DRIVER_OK = 4;
158
159        /// Indicates that the device has experienced
160        /// an error from which it can't recover.
161        const DEVICE_NEEDS_RESET = 64;
162    }
163}
164
165/// Virtio Device IDs
166#[derive(IntoPrimitive, FromPrimitive, PartialEq, Eq, Clone, Copy, Debug)]
167#[non_exhaustive]
168#[repr(u8)]
169pub enum Id {
170    /// reserved (invalid)
171    Reserved = 0,
172
173    /// network card
174    Net = 1,
175
176    /// block device
177    Block = 2,
178
179    /// console
180    Console = 3,
181
182    /// entropy source
183    Rng = 4,
184
185    /// memory ballooning (traditional)
186    Balloon = 5,
187
188    /// ioMemory
189    IoMem = 6,
190
191    /// rpmsg
192    Rpmsg = 7,
193
194    /// SCSI host
195    Scsi = 8,
196
197    /// 9P transport
198    NineP = 9,
199
200    /// mac80211 wlan
201    Mac80211Wlan = 10,
202
203    /// rproc serial
204    RprocSerial = 11,
205
206    /// virtio CAIF
207    Caif = 12,
208
209    /// memory balloon
210    MemoryBalloon = 13,
211
212    /// GPU device
213    Gpu = 16,
214
215    /// Timer/Clock device
216    Clock = 17,
217
218    /// Input device
219    Input = 18,
220
221    /// Socket device
222    Vsock = 19,
223
224    /// Crypto device
225    Crypto = 20,
226
227    /// Signal Distribution Module
228    SignalDist = 21,
229
230    /// pstore device
231    Pstore = 22,
232
233    /// IOMMU device
234    Iommu = 23,
235
236    /// Memory device
237    Mem = 24,
238
239    /// Audio device
240    Sound = 25,
241
242    /// file system device
243    Fs = 26,
244
245    /// PMEM device
246    Pmem = 27,
247
248    /// RPMB device
249    Rpmb = 28,
250
251    /// mac80211 hwsim wireless simulation device
252    Mac80211Hwsim = 29,
253
254    /// Video encoder device
255    VideoEncoder = 30,
256
257    /// Video decoder device
258    VideoDecoder = 31,
259
260    /// SCMI device
261    Scmi = 32,
262
263    /// NitroSecureModule
264    NitroSecMod = 33,
265
266    /// I2C adapter
267    I2cAdapter = 34,
268
269    /// Watchdog
270    Watchdog = 35,
271
272    /// CAN device
273    Can = 36,
274
275    /// Parameter Server
276    ParamServ = 38,
277
278    /// Audio policy device
279    AudioPolicy = 39,
280
281    /// Bluetooth device
282    Bt = 40,
283
284    /// GPIO device
285    Gpio = 41,
286
287    /// RDMA device
288    Rdma = 42,
289
290    /// Unknown device
291    #[num_enum(catch_all)]
292    Unknown(u8),
293}
294
295/// Descriptor Ring Change Event Flags
296#[doc(alias = "RING_EVENT_FLAGS")]
297#[derive(IntoPrimitive, TryFromPrimitive, PartialEq, Eq, Clone, Copy, Debug)]
298#[non_exhaustive]
299#[repr(u8)]
300pub enum RingEventFlags {
301    /// Enable events
302    #[doc(alias = "RING_EVENT_FLAGS_ENABLE")]
303    Enable = 0x0,
304
305    /// Disable events
306    #[doc(alias = "RING_EVENT_FLAGS_DISABLE")]
307    Disable = 0x1,
308
309    /// Enable events for a specific descriptor
310    /// (as specified by Descriptor Ring Change Event Offset/Wrap Counter).
311    /// Only valid if VIRTIO_F_EVENT_IDX has been negotiated.
312    #[doc(alias = "RING_EVENT_FLAGS_DESC")]
313    Desc = 0x2,
314
315    Reserved = 0x3,
316}
317
318impl RingEventFlags {
319    const fn from_bits(bits: u8) -> Self {
320        match bits {
321            0x0 => Self::Enable,
322            0x1 => Self::Disable,
323            0x2 => Self::Desc,
324            0x3 => Self::Reserved,
325            _ => unreachable!(),
326        }
327    }
328
329    const fn into_bits(self) -> u8 {
330        self as u8
331    }
332}
333
334/// Common device configuration space functionality.
335pub trait DeviceConfigSpace: Sized {
336    /// Read from device configuration space.
337    ///
338    /// This function should be used when reading from fields greater than
339    /// 32 bits wide or when reading from multiple fields.
340    ///
341    /// As described in _Driver Requirements: Device Configuration Space_,
342    /// this method checks the configuration atomicity value of the device
343    /// and only returns once the value was the same before and after the
344    /// provided function.
345    ///
346    /// # Examples
347    ///
348    /// ```rust
349    /// # use virtio_spec as virtio;
350    /// use virtio::net::ConfigVolatileFieldAccess;
351    /// use virtio::DeviceConfigSpace;
352    /// use volatile::access::ReadOnly;
353    /// use volatile::VolatilePtr;
354    ///
355    /// fn read_mac(
356    ///     common_cfg: VolatilePtr<'_, virtio::pci::CommonCfg, ReadOnly>,
357    ///     net_cfg: VolatilePtr<'_, virtio::net::Config, ReadOnly>,
358    /// ) -> [u8; 6] {
359    ///     common_cfg.read_config_with(|| net_cfg.mac().read())
360    /// }
361    /// ```
362    fn read_config_with<F, T>(self, f: F) -> T
363    where
364        F: FnMut() -> T;
365}