runtime-rs: Get available port node in the PCIe topology

This commit implements the `find_available_node` function,
which searches the PCIe topology for the first available
`TopologyPortDevice` or `SwitchDownPort`.
If no available node is found in either the `pcie_port_devices`
or the connected switches' downstream ports, the function returns
`None`.

Fixes # 10361

Signed-off-by: alex.lyn <alex.lyn@antgroup.com>
This commit is contained in:
alex.lyn 2025-05-20 11:06:32 +08:00
parent 533d07a2c3
commit 01b822de16

View File

@ -318,6 +318,13 @@ pub enum Strategy {
MultipleRootPorts,
}
/// Represents an available node in the PCIe topology.
#[derive(Clone, Debug)]
pub enum AvailableNode {
TopologyPortDevice(TopologyPortDevice),
SwitchDownPort(SwitchDownPort),
}
#[derive(Clone, Debug, Default)]
pub struct PCIeTopology {
pub hypervisor_name: String,
@ -648,6 +655,31 @@ impl PCIeTopology {
Ok(())
}
/// Finds an availabled node in the PCIe topology.
/// Returns the first available node found, either a TopologyPortDevice or a SwitchDownPort.
pub fn find_available_node(&mut self) -> Option<AvailableNode> {
// search in pcie_port_devices
for port_device in self.pcie_port_devices.values_mut() {
if !port_device.allocated {
port_device.allocated = true;
return Some(AvailableNode::TopologyPortDevice(port_device.clone()));
}
// search in connected switch's downstream ports
if let Some(switch) = &mut port_device.connected_switch {
for switch_port in switch.switch_ports.values_mut() {
if !switch_port.allocated {
switch_port.allocated = true;
return Some(AvailableNode::SwitchDownPort(switch_port.clone()));
}
}
}
}
// No available node found
None
}
}
// do_add_pcie_endpoint do add a device into PCIe topology with pcie endpoint