agent/device: Rename and clarify semantics of get_pci_device_address()

get_pci_device_address() has pretty confusing semantics.  Both its input
and output are in other parts of the code described as a "PCI address", but
neither is *actually* a PCI address (in the standard DDDD:BB:DD.F format).

What it's really about is resolving a "PCI path" - that is way to locate a
PCI device by using it's slot number and the slot number of the bridge
leading to it - into a sysfs path.

Rename the function, and change a bunch of variable names to make those
semantics clearer.

Forward port of
https://github.com/kata-containers/agent/pull/855/commits/0eb612f06484

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
David Gibson 2020-10-30 16:45:07 +11:00
parent 7e92831c7a
commit 3715c5775f

View File

@ -45,17 +45,19 @@ pub fn online_device(path: &str) -> Result<()> {
Ok(())
}
// get_pci_device_address fetches the complete PCI address in sysfs, based on the PCI
// identifier provided. This should be in the format: "bridgeAddr/deviceAddr".
// Here, bridgeAddr is the address at which the bridge is attached on the root bus,
// while deviceAddr is the address at which the device is attached on the bridge.
fn get_pci_device_address(pci_id: &str) -> Result<String> {
let tokens: Vec<&str> = pci_id.split('/').collect();
// pcipath_to_sysfs fetches the sysfs path for a PCI device, relative
// to the syfs path for the PCI host bridge, based on the PCI path
// provided. The path should be in the format "bridgeAddr/deviceAddr",
// where bridgeAddr is the address at which the brige is attached on
// the root bus, while deviceAddr is the address at which the device
// is attached on the bridge.
fn pcipath_to_sysfs(pcipath: &str) -> Result<String> {
let tokens: Vec<&str> = pcipath.split('/').collect();
if tokens.len() != 2 {
return Err(anyhow!(
"PCI Identifier for device should be of format [bridgeAddr/deviceAddr], got {}",
pci_id
"PCI path for device should be of format [bridgeAddr/deviceAddr], got {:?}",
pcipath
));
}
@ -89,14 +91,14 @@ fn get_pci_device_address(pci_id: &str) -> Result<String> {
// We do not pass devices as multifunction, hence the trailing 0 in the address.
let pci_device_addr = format!("{}:{}.0", bus, device_id);
let bridge_device_pci_addr = format!("{}/{}", pci_bridge_addr, pci_device_addr);
let sysfs_rel_path = format!("{}/{}", pci_bridge_addr, pci_device_addr);
info!(
sl!(),
"Fetched PCI address for device PCIAddr:{}\n", bridge_device_pci_addr
"Fetched sysfs relative path for PCI device {}\n", sysfs_rel_path
);
Ok(bridge_device_pci_addr)
Ok(sysfs_rel_path)
}
async fn get_device_name(sandbox: &Arc<Mutex<Sandbox>>, dev_addr: &str) -> Result<String> {
@ -155,11 +157,11 @@ pub async fn get_scsi_device_name(
get_device_name(sandbox, &dev_sub_path).await
}
pub async fn get_pci_device_name(sandbox: &Arc<Mutex<Sandbox>>, pci_id: &str) -> Result<String> {
let pci_addr = get_pci_device_address(pci_id)?;
pub async fn get_pci_device_name(sandbox: &Arc<Mutex<Sandbox>>, pcipath: &str) -> Result<String> {
let sysfs_rel_path = pcipath_to_sysfs(pcipath)?;
rescan_pci_bus()?;
get_device_name(sandbox, &pci_addr).await
get_device_name(sandbox, &sysfs_rel_path).await
}
pub async fn get_pmem_device_name(