From 8ea2ce9a317a119cf4149740ff29bee6c25c9421 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Wed, 7 Apr 2021 16:48:42 +1000 Subject: [PATCH] agent/device: Remove legacy uevent matching DevAddrMatcher existed purely as a transitional step as we refined the uevent matching logic for each of the different device types we care about. We've now done that, so it can be removed along with several related pieces. fixes #1628 Signed-off-by: David Gibson --- src/agent/src/device.rs | 73 ++++++++++---------------------------- src/agent/src/linux_abi.rs | 1 - 2 files changed, 18 insertions(+), 56 deletions(-) diff --git a/src/agent/src/device.rs b/src/agent/src/device.rs index 09ead0e49..28f503b37 100644 --- a/src/agent/src/device.rs +++ b/src/agent/src/device.rs @@ -88,57 +88,6 @@ fn pcipath_to_sysfs(root_bus_sysfs: &str, pcipath: &pci::Path) -> Result Ok(relpath) } -#[derive(Debug, Clone)] -struct DevAddrMatcher { - dev_addr: String, -} - -impl DevAddrMatcher { - fn new(dev_addr: &str) -> DevAddrMatcher { - DevAddrMatcher { - dev_addr: dev_addr.to_string(), - } - } -} - -impl UeventMatcher for DevAddrMatcher { - fn is_match(&self, uev: &Uevent) -> bool { - let pci_root_bus_path = create_pci_root_bus_path(); - let pci_p = format!("{}{}", pci_root_bus_path, self.dev_addr); - let pmem_suffix = format!("/{}/{}", SCSI_BLOCK_SUFFIX, uev.devname); - - uev.subsystem == "block" - && { - uev.devpath.starts_with(pci_root_bus_path.as_str()) - || uev.devpath.starts_with(ACPI_DEV_PATH) // NVDIMM/PMEM devices - } - && !uev.devname.is_empty() - && { - // blk block device - uev.devpath.starts_with(pci_p.as_str()) - // scsi block device - || ( - self.dev_addr.ends_with(SCSI_BLOCK_SUFFIX) && - uev.devpath.contains(self.dev_addr.as_str()) - ) - // nvdimm/pmem device - || ( - uev.devpath.starts_with(ACPI_DEV_PATH) && - uev.devpath.ends_with(pmem_suffix.as_str()) && - self.dev_addr.ends_with(pmem_suffix.as_str()) - ) - } - } -} - -async fn get_device_name(sandbox: &Arc>, dev_addr: &str) -> Result { - let matcher = DevAddrMatcher::new(dev_addr); - - let uev = wait_for_uevent(sandbox, matcher).await?; - - Ok(format!("{}/{}", SYSTEM_DEV_PATH, &uev.devname)) -} - // FIXME: This matcher is only correct if the guest has at most one // SCSI host. #[derive(Debug)] @@ -871,6 +820,20 @@ mod tests { assert_eq!(relpath.unwrap(), "/0000:00:02.0/0000:01:03.0/0000:02:04.0"); } + // We use device specific variants of this for real cases, but + // they have some complications that make them troublesome to unit + // test + async fn example_get_device_name( + sandbox: &Arc>, + relpath: &str, + ) -> Result { + let matcher = VirtioBlkPciMatcher::new(relpath)?; + + let uev = wait_for_uevent(sandbox, matcher).await?; + + Ok(uev.devname) + } + #[tokio::test] async fn test_get_device_name() { let devname = "vda"; @@ -891,9 +854,9 @@ mod tests { sb.uevent_map.insert(devpath.clone(), uev); drop(sb); // unlock - let name = get_device_name(&sandbox, relpath).await; + let name = example_get_device_name(&sandbox, relpath).await; assert!(name.is_ok(), "{}", name.unwrap_err()); - assert_eq!(name.unwrap(), format!("{}/{}", SYSTEM_DEV_PATH, devname)); + assert_eq!(name.unwrap(), devname); let mut sb = sandbox.lock().await; let uev = sb.uevent_map.remove(&devpath).unwrap(); @@ -901,9 +864,9 @@ mod tests { spawn_test_watcher(sandbox.clone(), uev); - let name = get_device_name(&sandbox, relpath).await; + let name = example_get_device_name(&sandbox, relpath).await; assert!(name.is_ok(), "{}", name.unwrap_err()); - assert_eq!(name.unwrap(), format!("{}/{}", SYSTEM_DEV_PATH, devname)); + assert_eq!(name.unwrap(), devname); } #[tokio::test] diff --git a/src/agent/src/linux_abi.rs b/src/agent/src/linux_abi.rs index c95ea0966..de695a129 100644 --- a/src/agent/src/linux_abi.rs +++ b/src/agent/src/linux_abi.rs @@ -78,7 +78,6 @@ pub const SYSFS_MEMORY_BLOCK_SIZE_PATH: &str = "/sys/devices/system/memory/block pub const SYSFS_MEMORY_HOTPLUG_PROBE_PATH: &str = "/sys/devices/system/memory/probe"; pub const SYSFS_MEMORY_ONLINE_PATH: &str = "/sys/devices/system/memory"; -pub const SCSI_BLOCK_SUFFIX: &str = "block"; pub const SYSFS_SCSI_HOST_PATH: &str = "/sys/class/scsi_host"; pub const SYSFS_CGROUPPATH: &str = "/sys/fs/cgroup";