runtime-rs: Handle port allocation in PCIe topology for vfio devices

It's import to handle port allocation in a PCIe topology before vfio
deivce hotplug via QMP.
The code ensures that VFIO devices are properly allocated to available
ports (either root ports or switch ports) and updates the device's bus
and port information accordingly.
It'll first retrieves the PCIe port type from the topology using
pcie_topo.get_pcie_port(). And then, searches for an available node in
the PCIe topology with RootPort or SwitchPort type and allocates the
VFIO device to the found available port. Finally, Updates the device's
bus with the allocated port's ID and type.

Fixes # 10361

Signed-off-by: alex.lyn <alex.lyn@antgroup.com>
This commit is contained in:
alex.lyn
2025-05-20 11:18:46 +08:00
parent 01b822de16
commit 043bab3d3e

View File

@@ -20,7 +20,7 @@ use kata_sys_util::fs::get_base_name;
use crate::{
device::{
pci_path::PciPath,
topology::{do_add_pcie_endpoint, PCIePort, PCIeTopology},
topology::{do_add_pcie_endpoint, AvailableNode, PCIePort, PCIeTopology},
util::{do_decrease_count, do_increase_count},
Device, DeviceType, PCIeDevice,
},
@@ -561,6 +561,34 @@ impl PCIeDevice for VfioDevice {
return Ok(());
}
// handle port devices
if !self.allocated {
let (port_type, _) = pcie_topo
.get_pcie_port()
.ok_or_else(|| anyhow!("No validated port type supported."))?;
let avail_port = match port_type {
PCIePort::RootPort | PCIePort::SwitchPort => pcie_topo.find_available_node(),
_ => {
info!(
sl!(),
"There's no need to set ports used to hot-plug vfio devices"
);
None
}
};
// vfio device attached onto port(root port | switch port)
let port_device = avail_port
.ok_or_else(|| anyhow!("No available node found for {:?} device", port_type))?;
self.bus = match port_device {
AvailableNode::TopologyPortDevice(root_port) => root_port.port_id(),
AvailableNode::SwitchDownPort(swdown_port) => swdown_port.port_id(),
};
self.port = port_type;
self.allocated = true;
info!(sl!(), "bus: {:?}, port type: {:?}", &self.bus, &self.port);
}
self.device_options.clear();
for hostdev in self.devices.iter_mut() {
let pci_path = do_add_pcie_endpoint(