| Crates.io | axvirtio-blk |
| lib.rs | axvirtio-blk |
| version | 0.1.0 |
| created_at | 2025-12-04 11:25:25.348762+00 |
| updated_at | 2025-12-04 11:25:25.348762+00 |
| description | VirtIO block device implementation with MMIO transport for hypervisors |
| homepage | https://github.com/arceos-hypervisor/axvisor |
| repository | https://github.com/arceos-hypervisor/axvirtio |
| max_upload_size | |
| id | 1966355 |
| size | 96,339 |
A collection of VirtIO device implementations for the ArceOS-Hypervisor project. This workspace provides no_std compatible VirtIO device emulation following the VirtIO 1.1 specification.
AxVirtIO Devices provides modular, reusable VirtIO device implementations designed for hypervisor and embedded systems development. The project is organized as a Rust workspace with:
axvirtio-common: Common types, traits, and utilities shared across all VirtIO device implementationsaxvirtio-blk: VirtIO block device implementation with MMIO transportno_std Compatible: Designed for bare-metal and embedded environments┌─────────────────────────────────────────────────────────────┐
│ Guest VM │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Guest Driver │ │
│ │ (VirtIO Block Driver) │ │
│ └──────────────────────┬──────────────────────────────┘ │
└─────────────────────────┼───────────────────────────────────┘
│ MMIO Access
┌─────────────────────────┼───────────────────────────────────┐
│ ▼ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ VirtIO MMIO Transport │ │
│ │ (axvirtio-common/mmio) │ │
│ └──────────────────────┬──────────────────────────────┘ │
│ │ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ VirtIO Queue Layer │ │
│ │ (Descriptor Table, Available Ring, Used Ring) │ │
│ │ (axvirtio-common/queue) │ │
│ └──────────────────────┬──────────────────────────────┘ │
│ │ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ VirtIO Block Device │ │
│ │ (axvirtio-blk) │ │
│ └──────────────────────┬──────────────────────────────┘ │
│ │ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Block Backend Trait │ │
│ │ (Pluggable Storage) │ │
│ └─────────────────────────────────────────────────────┘ │
│ Hypervisor │
└─────────────────────────────────────────────────────────────┘
Common infrastructure for VirtIO device implementations:
VirtIO block device implementation:
BlockBackend trait for custom storage implementationsAdd the following to your Cargo.toml:
[dependencies]
axvirtio-blk = { git = "https://github.com/arceos-hypervisor/axvirtio-devices" }
axvirtio-common = { git = "https://github.com/arceos-hypervisor/axvirtio-devices" }
use axvirtio_blk::{VirtioMmioBlockDevice, BlockBackend, VirtioBlockConfig, VirtioResult};
use axaddrspace::{GuestMemoryAccessor, GuestPhysAddr};
use memory_addr::PhysAddr;
// 1. Implement your block backend
struct MyBlockBackend {
// Your storage implementation
}
impl BlockBackend for MyBlockBackend {
fn read(&self, sector: u64, buffer: &mut [u8]) -> VirtioResult<usize> {
// Read sectors from your storage
Ok(buffer.len())
}
fn write(&self, sector: u64, buffer: &[u8]) -> VirtioResult<usize> {
// Write sectors to your storage
Ok(buffer.len())
}
fn flush(&self) -> VirtioResult<()> {
// Flush pending writes
Ok(())
}
}
// 2. Implement guest memory accessor for address translation
#[derive(Clone)]
struct MyMemoryAccessor {
// Your memory translation implementation
}
impl GuestMemoryAccessor for MyMemoryAccessor {
fn translate_and_get_limit(&self, guest_addr: GuestPhysAddr) -> Option<(PhysAddr, usize)> {
// Translate guest physical address to host physical address
// Return (host_addr, accessible_size)
None
}
}
// 3. Create the VirtIO block device
fn create_device() {
let backend = MyBlockBackend { /* ... */ };
let accessor = MyMemoryAccessor { /* ... */ };
let config = VirtioBlockConfig::default();
let base_addr = GuestPhysAddr::from(0x0a000000);
let device = VirtioMmioBlockDevice::new(
base_addr,
0x200, // MMIO region size
backend,
config,
accessor,
).unwrap();
// Handle MMIO accesses from the guest
// device.mmio_read(addr, width)
// device.mmio_write(addr, width, value)
}
use axvirtio_blk::VirtioBlockConfig;
let config = VirtioBlockConfig {
capacity: 2048 * 1024, // Total sectors (1GB with 512-byte sectors)
size_max: 65536, // Maximum segment size
seg_max: 128, // Maximum number of segments
blk_size: 512, // Block size in bytes
// ... other configuration options
..Default::default()
};
| Feature | Status | Description |
|---|---|---|
VIRTIO_F_VERSION_1 |
✅ | VirtIO 1.0+ compliance |
VIRTIO_F_RING_EVENT_IDX |
✅ | Event index support |
VIRTIO_BLK_F_SIZE_MAX |
✅ | Maximum segment size |
VIRTIO_BLK_F_SEG_MAX |
✅ | Maximum segments per request |
VIRTIO_BLK_F_BLK_SIZE |
✅ | Block size reporting |
VIRTIO_BLK_F_FLUSH |
✅ | Flush command support |
| Operation | Status | Description |
|---|---|---|
| Read | ✅ | Read sectors from device |
| Write | ✅ | Write sectors to device |
| Flush | ✅ | Flush pending writes |
Run the test suite:
# Run all tests
cargo test
# Run tests for a specific crate
cargo test --package axvirtio-blk
# Run tests with output
cargo test -- --nocapture
The library supports the following targets:
x86_64-unknown-linux-gnu (with tests)x86_64-unknown-noneriscv64gc-unknown-none-elfaarch64-unknown-none-softfloatGenerate and view documentation:
cargo doc --open
Contributions are welcome! Please feel free to submit issues and pull requests.
This project is licensed under one of the following licenses: