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
22 changes: 16 additions & 6 deletions cloud-hypervisor/src/bin/ch-remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,14 +371,14 @@ fn rest_api_do_command(matches: &ArgMatches, socket: &mut UnixStream) -> ApiResu
.map_err(Error::HttpApiClient)
}
Some("add-device") => {
let device_config = add_device_config(
let (device_config, fds) = add_device_config(
matches
.subcommand_matches("add-device")
.unwrap()
.get_one::<String>("device_config")
.unwrap(),
)?;
simple_api_command(socket, "PUT", "add-device", Some(&device_config))
simple_api_command_with_fds(socket, "PUT", "add-device", Some(&device_config), &fds)
.map_err(Error::HttpApiClient)
}
Some("remove-device") => {
Expand Down Expand Up @@ -609,7 +609,7 @@ fn dbus_api_do_command(matches: &ArgMatches, proxy: &DBusApi1ProxyBlocking<'_>)
proxy.api_vm_resize_zone(&resize_zone)
}
Some("add-device") => {
let device_config = add_device_config(
let (device_config, _fds) = add_device_config(
matches
.subcommand_matches("add-device")
.unwrap()
Expand Down Expand Up @@ -835,11 +835,21 @@ fn resize_zone_config(id: &str, size: &str) -> Result<String, Error> {
Ok(serde_json::to_string(&resize_zone).unwrap())
}

fn add_device_config(config: &str) -> Result<String, Error> {
let device_config = DeviceConfig::parse(config).map_err(Error::AddDeviceConfig)?;
fn add_device_config(config: &str) -> Result<(String, Vec<i32>), Error> {
let mut device_config = DeviceConfig::parse(config).map_err(Error::AddDeviceConfig)?;

// DeviceConfig is modified on purpose here by taking the file
// descriptor out. Keeping it and sending it over to the server side
// process would not make any sense since the file descriptor may be
// represented with different values.
let fds = device_config
.fd
.take()
.map(|fd| vec![fd])
.unwrap_or_default();
let device_config = serde_json::to_string(&device_config).unwrap();

Ok(device_config)
Ok((device_config, fds))
}

fn add_user_device_config(config: &str) -> Result<String, Error> {
Expand Down
88 changes: 88 additions & 0 deletions cloud-hypervisor/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11128,6 +11128,8 @@ mod windows {

#[cfg(target_arch = "x86_64")]
mod vfio {
use std::io;

use crate::*;

const NVIDIA_VFIO_DEVICE: &str = "/sys/bus/pci/devices/0002:00:01.0";
Expand Down Expand Up @@ -11393,6 +11395,92 @@ mod vfio {
test_nvidia_card_reboot_common(true);
}

// Pass the NVIDIA card to the guest via an externally-opened
// /dev/vfio/devices/<n> FD instead of a sysfs path, and verify it
// survives a guest reboot. Mirrors `_test_tap_from_fd` for VFIO.
#[test]
fn test_iommufd_nvidia_card_reboot_from_fd() {
let disk_config = UbuntuDiskConfig::new(JAMMY_VFIO_IMAGE_NAME.to_string());
let guest = Guest::new(Box::new(disk_config));
let api_socket = temp_api_path(&guest.tmp_dir);

// Discover the cdev for the NVIDIA card. The directory has a
// single entry whose name (e.g. "vfio0") is also the basename
// of the corresponding /dev/vfio/devices/<n> node.
let vfio_dev_dir = format!("{NVIDIA_VFIO_DEVICE}/vfio-dev");
let cdev_name = fs::read_dir(&vfio_dev_dir)
.unwrap_or_else(|e| panic!("read_dir({vfio_dev_dir}) failed: {e}"))
.next()
.expect("no vfio-dev entry")
.unwrap()
.file_name();
let cdev_path = PathBuf::from("/dev/vfio/devices").join(&cdev_name);

let cdev_file = OpenOptions::new()
.read(true)
.write(true)
.open(&cdev_path)
.unwrap_or_else(|e| panic!("open({cdev_path:?}) failed: {e}"));
let iommufd_file = OpenOptions::new()
.read(true)
.write(true)
.open("/dev/iommu")
.unwrap_or_else(|e| panic!("open(/dev/iommu) failed: {e}"));
// OpenOptions sets FD_CLOEXEC by default; clear it for both fds
// so they're inherited by the VMM child via spawn().
for f in [&cdev_file, &iommufd_file] {
// SAFETY: FFI call to fcntl on a valid fd we own.
let ret = unsafe { libc::fcntl(f.as_raw_fd(), libc::F_SETFD, 0) };
assert!(
ret >= 0,
"fcntl(F_SETFD, 0) failed: {}",
io::Error::last_os_error()
);
}

let platform = format!(
"{},iommufd_fd={}",
platform_cfg(true),
iommufd_file.as_raw_fd()
);

let mut child = GuestCommand::new(&guest)
.args(["--cpus", "boot=4"])
.args(["--memory", "size=1G"])
.args(["--platform", &platform])
.args(["--kernel", fw_path(FwType::RustHypervisorFirmware).as_str()])
.args([
"--device",
format!("fd={},iommu=on", cdev_file.as_raw_fd()).as_str(),
])
.args(["--api-socket", &api_socket])
.default_disks()
.default_net()
.capture_output()
.spawn()
.unwrap();

let r = std::panic::catch_unwind(|| {
guest.wait_vm_boot().unwrap();

// VFIO device works after boot from an externally-opened FD.
assert!(guest.check_nvidia_gpu());

guest.reboot_linux(0);

// Both FDs survive a VM reboot
assert!(guest.check_nvidia_gpu());
});

drop(cdev_file);
drop(iommufd_file);

let _ = child.kill();
let output = child.wait_with_output().unwrap();

handle_child_output(r, &output);
}

fn test_nvidia_card_iommu_address_width_common(iommufd: bool) {
let disk_config = UbuntuDiskConfig::new(JAMMY_VFIO_IMAGE_NAME.to_string());
let guest = Guest::new(Box::new(disk_config));
Expand Down
Loading
Loading