vmm: DeviceConfig: Add fd field for an externally-opened vfio cdev

Add a new `fd: Option<i32>` field to DeviceConfig so a caller can
supply a pre-opened vfio cdev FD (e.g. /dev/vfio/devices/vfioN) in
addition to the existing sysfs path. The CLI `--device` option now
accepts `fd=<n>`, parsed alongside the existing options.

Signed-off-by: Bo Chen <bchen@crusoe.ai>
Assisted-by: Claude:Opus-4.7
This commit is contained in:
Bo Chen
2026-04-28 03:49:20 +00:00
committed by Rob Bradford
parent bc5363823a
commit 0e4b98ae8e
2 changed files with 23 additions and 1 deletions

View File

@@ -2413,7 +2413,7 @@ impl DebugConsoleConfig {
impl DeviceConfig {
pub const SYNTAX: &'static str = "Direct device assignment parameters \
\"path=<device_path>,iommu=on|off,id=<device_id>,\
\"path=<device_path>,fd=<vfio_cdev_fd>,iommu=on|off,id=<device_id>,\
pci_segment=<segment_id>,pci_device_id=<pci_slot>,\
x_nv_gpudirect_clique=<clique_id>,\
x_exclude_mmap_bars=[<bar>...]\"";
@@ -2422,6 +2422,7 @@ impl DeviceConfig {
let mut parser = OptionParser::new();
parser
.add("path")
.add("fd")
.add_all(PciDeviceCommonConfig::OPTIONS_IOMMU)
.add("x_nv_gpudirect_clique")
.add("x_exclude_mmap_bars");
@@ -2432,6 +2433,7 @@ impl DeviceConfig {
.get("path")
.map(PathBuf::from)
.ok_or(Error::ParseDevicePathMissing)?;
let fd = parser.convert::<i32>("fd").map_err(Error::ParseDevice)?;
let x_nv_gpudirect_clique = parser
.convert::<u8>("x_nv_gpudirect_clique")
.map_err(Error::ParseDevice)?;
@@ -2443,6 +2445,7 @@ impl DeviceConfig {
Ok(DeviceConfig {
pci_common,
path: Some(path),
fd,
x_nv_gpudirect_clique,
x_exclude_mmap_bars,
})
@@ -4722,6 +4725,7 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
DeviceConfig {
pci_common: PciDeviceCommonConfig::default(),
path: Some(PathBuf::from("/path/to/device")),
fd: None,
x_nv_gpudirect_clique: None,
x_exclude_mmap_bars: Vec::new(),
}

View File

@@ -760,12 +760,30 @@ pub struct DeviceConfig {
pub pci_common: PciDeviceCommonConfig,
#[serde(default)]
pub path: Option<PathBuf>,
// FDs are not serialized and any deserialized value is invalid; see NetConfig::fds.
#[serde(default, deserialize_with = "deserialize_deviceconfig_fd")]
pub fd: Option<i32>,
#[serde(default)]
pub x_nv_gpudirect_clique: Option<u8>,
#[serde(default)]
pub x_exclude_mmap_bars: Vec<u64>,
}
fn deserialize_deviceconfig_fd<'de, D>(d: D) -> Result<Option<i32>, D::Error>
where
D: serde::Deserializer<'de>,
{
let invalid_fd: Option<i32> = Option::deserialize(d)?;
if invalid_fd.is_some() {
debug!(
"FD in 'DeviceConfig' won't be deserialized as it is most likely invalid now. Deserializing it as -1."
);
Ok(Some(-1))
} else {
Ok(None)
}
}
impl ApplyLandlock for DeviceConfig {
fn apply_landlock(&self, landlock: &mut Landlock) -> LandlockResult<()> {
let path = self