Skip to content
Closed
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
3 changes: 3 additions & 0 deletions iommufd-ioctls/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

## Added

- `IommuFd::new_from_path()`, opening the iommufd character device at a
caller-provided path instead of `/dev/iommu`

## Fixed

# [v0.4.0]
Expand Down
28 changes: 27 additions & 1 deletion iommufd-ioctls/src/iommufd_ioctls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use std::fs::{File, OpenOptions};
use std::mem::ManuallyDrop;
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
use std::path::Path;
use std::sync::Arc;

use iommufd_bindings::iommufd::*;
Expand All @@ -19,11 +20,21 @@ pub struct IommuFd {
}

impl IommuFd {
/// Open the default `/dev/iommu` character device.
pub fn new() -> Result<Self> {
Self::new_from_path(Path::new("/dev/iommu"))
}

/// Open the iommufd character device at `path`.
///
/// This lets a caller use an iommufd device node that is not exposed at
/// `/dev/iommu`, for instance in a container or a sandbox that mounts
/// the device nodes elsewhere.
pub fn new_from_path(path: &Path) -> Result<Self> {
let iommufd = OpenOptions::new()
.read(true)
.write(true)
.open("/dev/iommu")
.open(path)
.map_err(IommufdError::OpenIommufd)?;

Ok(IommuFd { iommufd })
Expand Down Expand Up @@ -1066,8 +1077,23 @@ pub(crate) mod iommufd_syscall {

#[cfg(test)]
mod tests {
use vmm_sys_util::tempfile::TempFile;

use super::*;

#[test]
fn test_iommufd_new_from_path() {
let tmp = TempFile::new().unwrap();
let iommufd = IommuFd::new_from_path(tmp.as_path()).unwrap();
assert!(iommufd.as_raw_fd() >= 0);

let missing = tmp.as_path().with_file_name("nonexistent_iommufd");
assert!(matches!(
IommuFd::new_from_path(&missing),
Err(IommufdError::OpenIommufd(_))
));
}

#[test]
fn test_iommufd_ioctl_code() {
assert_eq!(IOMMU_DESTROY(), 15232);
Expand Down
2 changes: 1 addition & 1 deletion iommufd-ioctls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use iommufd_bindings::iommufd::*;

#[derive(Debug, Error)]
pub enum IommufdError {
#[error("failed to open /dev/iommufd: {0}")]
#[error("failed to open iommufd: {0}")]
OpenIommufd(#[source] io::Error),
#[error("failed to destroy iommufd object: {0}")]
IommuDestroy(#[source] SysError),
Expand Down