Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion alioth-cli/src/boot/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use alioth::hv::{CocoSpec, HvSpec, Hypervisor};
use alioth::loader::{Executable, PayloadSpec};
use alioth::mem::{MemBackend, MemSpec};
#[cfg(target_os = "linux")]
use alioth::vfio::{VfioCdevSpec, VfioContainerSpec, VfioGroupSpec, VfioIoasSpec};
use alioth::vfio::{VfioCdevSpec, VfioContainerSpec, VfioGroupSpec, VfioIoasSpec, VfioUserSpec};
#[cfg(target_os = "linux")]
use alioth::virtio::DeviceId;
use alioth::virtio::dev::balloon::BalloonSpec;
Expand Down Expand Up @@ -182,6 +182,10 @@ pub struct BootArgs {
#[arg(long, help(help_text::<VfioContainerSpec>("Add a new VFIO container.")))]
vfio_container: Vec<String>,

#[cfg(target_os = "linux")]
#[arg(long, help(help_text::<VfioUserSpec>("Assign a vfio-user device to the guest.")))]
vfio_user: Vec<String>,

#[arg(long)]
#[arg(long, help(help_text::<BalloonSpec>("Add a VirtIO balloon device.")))]
balloon: Option<String>,
Expand Down Expand Up @@ -358,6 +362,11 @@ fn parse_args(mut args: BootArgs, objects: HashMap<&str, &str>) -> Result<VmSpec
let param = serde_aco::from_args(&arg, &objects).context(error::ParseArg { arg })?;
spec.vfio_group.push(param);
}
#[cfg(target_os = "linux")]
for arg in args.vfio_user {
let param = serde_aco::from_args(&arg, &objects).context(error::ParseArg { arg })?;
spec.vfio_user.push(param);
}

Ok(spec)
}
Expand Down Expand Up @@ -460,6 +469,10 @@ fn create<H: Hypervisor>(hypervisor: &H, spec: VmSpec) -> Result<Machine<H>, ali
for (index, cdev_spec) in spec.vfio_cdev.into_iter().enumerate() {
vm.add_vfio_cdev(format!("vfio-{index}").into(), cdev_spec)?;
}
#[cfg(target_os = "linux")]
for (index, user_spec) in spec.vfio_user.into_iter().enumerate() {
vm.add_vfio_user_dev(format!("vfio-user-{index}").into(), user_spec)?;
}

#[cfg(target_os = "linux")]
for container_spec in spec.vfio_container.into_iter() {
Expand Down
8 changes: 7 additions & 1 deletion alioth-cli/src/boot/boot_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use alioth::device::net::MacAddr;
use alioth::loader::{Executable, PayloadSpec};
use alioth::mem::{MemBackend, MemSpec};
#[cfg(target_os = "linux")]
use alioth::vfio::{VfioCdevSpec, VfioContainerSpec, VfioGroupSpec, VfioIoasSpec};
use alioth::vfio::{VfioCdevSpec, VfioContainerSpec, VfioGroupSpec, VfioIoasSpec, VfioUserSpec};
use alioth::virtio::dev::balloon::BalloonSpec;
use alioth::virtio::dev::blk::BlkFileSpec;
use alioth::virtio::dev::entropy::EntropySpec;
Expand Down Expand Up @@ -89,6 +89,8 @@ fn test_parse_args() {
vfio_group: vec!["path=/dev/vfio/26,container=gpu_container,devices=id_gpus".into()],
#[cfg(target_os = "linux")]
vfio_container: vec!["name=gpu_container,dev_vfio=/dev/vfio/vfio".into()],
#[cfg(target_os = "linux")]
vfio_user: vec!["socket=/tmp/nvme.sock".into()],
..Default::default()
};
let objects = HashMap::from([
Expand Down Expand Up @@ -212,6 +214,10 @@ fn test_parse_args() {
container: Some("gpu_container".into()),
devices: vec!["0000:06:0d.0".into(), "0000:06:0d.1".into()],
}],
#[cfg(target_os = "linux")]
vfio_user: vec![VfioUserSpec {
socket: Path::new("/tmp/nvme.sock").into(),
}],
};
assert_eq!(spec, want);
}
Expand Down
4 changes: 3 additions & 1 deletion alioth-cli/src/boot/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use alioth::device::console::ConsoleSpec;
use alioth::device::fw_cfg::FwCfgItemSpec;
use alioth::loader::PayloadSpec;
#[cfg(target_os = "linux")]
use alioth::vfio::{VfioCdevSpec, VfioContainerSpec, VfioGroupSpec, VfioIoasSpec};
use alioth::vfio::{VfioCdevSpec, VfioContainerSpec, VfioGroupSpec, VfioIoasSpec, VfioUserSpec};
use alioth::virtio::dev::balloon::BalloonSpec;
use alioth::virtio::dev::blk::BlkFileSpec;
use alioth::virtio::dev::entropy::EntropySpec;
Expand Down Expand Up @@ -119,4 +119,6 @@ pub struct VmSpec {
pub vfio_group: Vec<VfioGroupSpec>,
#[cfg(target_os = "linux")]
pub vfio_container: Vec<VfioContainerSpec>,
#[cfg(target_os = "linux")]
pub vfio_user: Vec<VfioUserSpec>,
}
5 changes: 3 additions & 2 deletions alioth/src/sys/linux/vfio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use bitfield::bitfield;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

use crate::sys::ioctl::ioctl_io;
use crate::{
Expand Down Expand Up @@ -80,7 +81,7 @@ consts! {
}

#[repr(C)]
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, KnownLayout, Immutable, FromBytes, IntoBytes)]
pub struct VfioRegionInfo {
pub argsz: u32,
pub flags: VfioRegionInfoFlag,
Expand Down Expand Up @@ -116,7 +117,7 @@ consts! {
}

#[repr(C)]
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, KnownLayout, Immutable, FromBytes, IntoBytes)]
pub struct VfioIrqInfo {
pub argsz: u32,
pub flags: VfioIrqInfoFlag,
Expand Down
130 changes: 130 additions & 0 deletions alioth/src/vfio/user/bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use bitfield::bitfield;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

use crate::sys::vfio::{VfioDeviceInfoFlag, VfioIrqSetFlag};
use crate::{bitflags, consts};

consts! {
pub struct VfioUserCmd(u16) {
VERSION = 1;
DMA_MAP = 2;
DMA_UNMAP = 3;
DEVICE_GET_INFO = 4;
DEVICE_GET_REGION_INFO = 5;
DEVICE_GET_REGION_IO_FDS = 6;
DEVICE_GET_IRQ_INFO = 7;
DEVICE_SET_IRQS = 8;
REGION_READ = 9;
REGION_WRITE = 10;
DMA_READ = 11;
DMA_WRITE = 12;
DEVICE_RESET = 13;
REGION_WRITE_MULTI = 15;
DEVICE_FEATURE = 16;
MIG_DATA_READ = 17;
MIG_DATA_WRITE = 18;
}
}

consts! {
pub struct VfioUserMessageType(u8) {
COMMAND = 0;
REPLY = 1;
}
}

bitfield! {
#[derive(Copy, Clone, Default, IntoBytes, FromBytes, Immutable, KnownLayout)]
pub struct VfioUserHeaderFlag(u32);
impl Debug;
impl new;
pub u8, from into VfioUserMessageType, ty, set_ty: 3, 0;
pub no_reply, set_no_reply: 4;
pub error, set_error: 5;
}

#[derive(Debug, Copy, Clone, FromBytes, IntoBytes, Immutable, Default)]
#[repr(C)]
pub struct VfioUserHeader {
pub msg_id: u16,
pub cmd: VfioUserCmd,
pub msg_size: u32,
pub flags: VfioUserHeaderFlag,
pub error_no: u32,
}

#[derive(Debug, Copy, Clone, FromBytes, IntoBytes, Immutable, Default)]
#[repr(C)]
pub struct VfioUserVersion {
pub major: u16,
pub minor: u16,
}

#[derive(Debug, Copy, Clone, FromBytes, IntoBytes, Immutable, Default)]
#[repr(C)]
pub struct VfioUserDeviceInfo {
pub argsz: u32,
pub flags: VfioDeviceInfoFlag,
pub num_regions: u32,
pub num_irqs: u32,
}

#[derive(Debug, Copy, Clone, FromBytes, IntoBytes, Immutable, Default)]
#[repr(C)]
pub struct VfioUserIrqSet {
pub argsz: u32,
pub flags: VfioIrqSetFlag,
pub index: u32,
pub start: u32,
pub count: u32,
}

#[derive(Debug, Copy, Clone, FromBytes, IntoBytes, Immutable, Default)]
#[repr(C)]
pub struct VfioUserRegionAccess {
pub offset: u64,
pub region: u32,
pub count: u32,
}

bitflags! {
pub struct VfioUserDmaMapFlag(u32) {
READ = 1 << 0;
WRITE = 1 << 1;
MMAP = 1 << 2;
FILE_IO = 1 << 3;
}
}

#[derive(Debug, Copy, Clone, FromBytes, IntoBytes, Immutable, Default)]
#[repr(C)]
pub struct VfioUserDmaMap {
pub argsz: u32,
pub flags: VfioUserDmaMapFlag,
pub offset: u64,
pub addr: u64,
pub size: u64,
}

#[derive(Debug, Copy, Clone, FromBytes, IntoBytes, Immutable, Default)]
#[repr(C)]
pub struct VfioUserDmaUnmap {
pub argsz: u32,
pub flags: u32,
pub addr: u64,
pub size: u64,
}
Loading