[pve-devel] [PATCH proxmox-perl-rs 3/3] fabrics: add function to get all neighbors of the fabric
Gabriel Goller
g.goller at proxmox.com
Wed Aug 13 15:30:09 CEST 2025
In order to also display the neighbors of a specific node in the
FabricContentView resource window get the Neighbors of the all the
fabrics. Query frr (vtysh) to get the neighbors of both openefabric and
ospf, parse it and then compile a array containing all neighbors and
the fabric it relates to.
Signed-off-by: Gabriel Goller <g.goller at proxmox.com>
---
pve-rs/src/bindings/sdn/fabrics.rs | 262 +++++++++++++++++++++++++++++
1 file changed, 262 insertions(+)
diff --git a/pve-rs/src/bindings/sdn/fabrics.rs b/pve-rs/src/bindings/sdn/fabrics.rs
index e211ce4af92f..28667b8b9bf6 100644
--- a/pve-rs/src/bindings/sdn/fabrics.rs
+++ b/pve-rs/src/bindings/sdn/fabrics.rs
@@ -596,6 +596,17 @@ pub mod pve_rs_sdn_fabrics {
};
use serde::{Deserialize, Serialize};
+ /// The status of a neighbor.
+ ///
+ /// Contains the neighbor, the fabric and protocol it belongs to and the some status
+ /// information.
+ #[derive(Debug, Serialize)]
+ pub struct NeighborStatus {
+ neighbor: String,
+ status: String,
+ fabric_id: FabricId,
+ protocol: Protocol,
+ }
/// The status of a route.
///
@@ -654,6 +665,19 @@ pub mod pve_rs_sdn_fabrics {
pub ospf: Routes,
}
+ /// Parsed neighbors for all protocols
+ ///
+ /// These are the neighbors parsed from the json output of:
+ /// `vtysh -c 'show openfabric neighbor json'` and
+ /// `vtysh -c 'show ip ospf neighbor json'`.
+ #[derive(Debug, Serialize)]
+ pub struct NeighborsParsed {
+ /// The openfabric neighbors in FRR
+ pub openfabric: openfabric::Neighbors,
+ /// The ospf neighbors in FRR
+ pub ospf: ospf::Neighbors,
+ }
+
impl TryInto<Vec<RouteStatus>> for RoutesParsed {
type Error = anyhow::Error;
@@ -742,6 +766,90 @@ pub mod pve_rs_sdn_fabrics {
}
}
+ impl TryInto<Vec<NeighborStatus>> for NeighborsParsed {
+ type Error = anyhow::Error;
+
+ fn try_into(self) -> Result<Vec<NeighborStatus>, Self::Error> {
+ let hostname = proxmox_sys::nodename();
+
+ // get all nodes
+ let raw_config = std::fs::read_to_string("/etc/pve/sdn/fabrics.cfg")?;
+ let config = FabricConfig::parse_section_config(&raw_config)?;
+
+ let mut stats: Vec<NeighborStatus> = Vec::new();
+
+ for (nodeid, node) in config.values().flat_map(|entry| {
+ entry
+ .nodes()
+ .map(|(id, node)| (id.to_string(), node.clone()))
+ }) {
+ if nodeid != hostname {
+ continue;
+ }
+ let fabric_id = node.id().fabric_id().clone();
+
+ match node {
+ ConfigNode::Openfabric(_) => {
+ for area in &self.openfabric.areas {
+ if area.area == fabric_id.as_str() {
+ for circuit in &area.circuits {
+ if let (Some(adj), Some(state)) =
+ (&circuit.adj, &circuit.state)
+ {
+ stats.push(NeighborStatus {
+ neighbor: adj.clone(),
+ status: state.clone(),
+ protocol: Protocol::Openfabric,
+ fabric_id: fabric_id.clone(),
+ });
+ }
+ }
+ }
+ }
+ }
+ ConfigNode::Ospf(node) => {
+ let interface_names: HashSet<&str> = node
+ .properties()
+ .interfaces()
+ .map(|i| i.name().as_str())
+ .collect();
+
+ for (neighbor_key, neighbor_list) in &self.ospf.neighbors {
+ let mut has_matching_neighbor = false;
+ for neighbor in neighbor_list {
+ match neighbor.interface_name.split_once(":") {
+ Some((interface_name, _)) => {
+ if interface_names.contains(interface_name) {
+ has_matching_neighbor = true;
+ break;
+ }
+ }
+ _ => {
+ continue;
+ }
+ }
+ }
+ if has_matching_neighbor {
+ let status = neighbor_list
+ .first()
+ .map(|n| n.neighbor_state.clone())
+ .unwrap_or_default();
+ stats.push(NeighborStatus {
+ neighbor: neighbor_key.clone(),
+ status,
+ protocol: Protocol::Ospf,
+ fabric_id: fabric_id.clone(),
+ });
+ }
+ }
+ }
+ }
+ }
+
+ Ok(stats)
+ }
+ }
+
impl TryInto<HashMap<FabricId, Status>> for RoutesParsed {
type Error = anyhow::Error;
@@ -914,6 +1022,117 @@ pub mod pve_rs_sdn_fabrics {
/// This struct can be used the deserialize the output of that command.
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Routes(pub HashMap<Cidr, Vec<Route>>);
+
+ /// Types to parse the output of vtysh commands on the ospf daemon.
+ ///
+ /// Currently it contains structs to parse the `show ip ospf neighbor json` command.
+ pub mod ospf {
+ use std::collections::HashMap;
+
+ use serde::{Deserialize, Serialize};
+
+ /// Information about the Neighbor (Peer) of the Adjacency.
+ #[derive(Debug, Serialize, Deserialize)]
+ #[serde(rename_all = "camelCase")]
+ pub struct Neighbor {
+ /// The full state of the neighbor. This is "{converged}/{role}".
+ #[serde(rename = "nbrState")]
+ pub neighbor_state: String,
+ /// Priority of the Neighbor
+ #[serde(rename = "nbrPriority")]
+ pub neighbor_priority: u32,
+ /// The current state of the adjancecy, this is a complex state machine with many
+ /// states. The most important ones are "Full" if the full table has been exchanged
+ /// and "Init" when the adjacency has been formed but no routing information has
+ /// been exchanged.
+ pub converged: String,
+ /// Role of the peer (If he's a designated router (DR) or not (DROther)
+ pub role: String,
+ /// Uptime in milliseconds
+ #[serde(rename = "upTimeInMsec")]
+ pub up_time_in_msec: u64,
+ /// Router Dead Interval Timer Due in milliseconds
+ #[serde(rename = "routerDeadIntervalTimerDueMsec")]
+ pub router_dead_interval_timer_due_msec: u64,
+ /// Uptime of the adjacency
+ #[serde(rename = "upTime")]
+ pub up_time: String,
+ /// Expires in countdown
+ #[serde(rename = "deadTime")]
+ pub dead_time: String,
+ /// The remote interface address, so the address of the other peer.
+ #[serde(rename = "ifaceAddress")]
+ pub interface_address: String,
+ /// The interface name of this adjacency. This is always a combination of interface
+ /// name and address. e.g. "ens21:5.5.5.3".
+ #[serde(rename = "ifaceName")]
+ pub interface_name: String,
+ /// Link State Retransmission List Counter
+ #[serde(rename = "linkStateRetransmissionListCounter")]
+ pub link_state_retransmission_list_counter: u32,
+ /// Link State Request List Counter
+ #[serde(rename = "linkStateRequestListCounter")]
+ pub link_state_request_list_counter: u32,
+ /// Database Summary List Counter
+ #[serde(rename = "databaseSummaryListCounter")]
+ pub database_summary_list_counter: u32,
+ }
+
+ /// The parsed OSPF neighbors
+ #[derive(Debug, Serialize, Deserialize, Default)]
+ pub struct Neighbors {
+ /// The OSPF neighbors. This is nearly always a ip-address - neighbor mapping.
+ pub neighbors: HashMap<String, Vec<Neighbor>>,
+ }
+ }
+
+ /// Structs to parse the vtysh output of the openfabric daemon.
+ ///
+ /// Currently only the output of: `show openfabric neighbor json` is modeled here.
+ pub mod openfabric {
+ use serde::{Deserialize, Serialize};
+
+ /// Adjacency information
+ ///
+ /// Circuits are Layer-2 Broadcast domains (Either point-to-point or LAN).
+ #[derive(Debug, Serialize, Deserialize)]
+ pub struct Circuit {
+ /// The circuit id
+ pub circuit: u32,
+ /// The hostname of the adjacency peer
+ pub adj: Option<String>,
+ /// The interface on which this adjacency exists
+ pub interface: Option<String>,
+ /// If the adjacent router is a L1 or L2 router
+ pub level: Option<u32>,
+ /// The state of the adjacency, this is "Up" when everything is well
+ pub state: Option<String>,
+ /// When the adjacency expires
+ #[serde(rename = "expires-in")]
+ pub expires_in: Option<String>,
+ /// Subnetwork Point of Attachment
+ pub snpa: Option<String>,
+ }
+
+ /// An openfabric area the same as SDN fabric.
+ #[derive(Debug, Serialize, Deserialize)]
+ pub struct Area {
+ /// The are name, this is the same as the fabric_id, so the name of the fabric.
+ pub area: String,
+ /// Circuits are Layer-2 Broadcast domains (Either point-to-point or LAN).
+ pub circuits: Vec<Circuit>,
+ }
+
+ /// The parsed neighbors.
+ ///
+ /// This models the output of:
+ /// `vtysh -c 'show openfabric neighbor json'`.
+ #[derive(Debug, Serialize, Deserialize, Default)]
+ pub struct Neighbors {
+ /// Every sdn fabric is also an openfabric 'area'
+ pub areas: Vec<Area>,
+ }
+ }
}
/// Get all the routes for all the fabrics on this node.
@@ -972,6 +1191,49 @@ pub mod pve_rs_sdn_fabrics {
route_status.try_into()
}
+ /// Get all the neighbors of all the fabrics on this node.
+ ///
+ /// Go through all fabrics that exist on this node. Then get the neighbors of them all and
+ /// concat them into a single array.
+ #[export]
+ fn neighbors() -> Result<Vec<status::NeighborStatus>, Error> {
+ let openfabric_neighbors_string = String::from_utf8(
+ Command::new("sh")
+ .args(["-c", "vtysh -c 'show openfabric neighbor json'"])
+ .output()?
+ .stdout,
+ )?;
+
+ let ospf_neighbors_string = String::from_utf8(
+ Command::new("sh")
+ .args(["-c", "vtysh -c 'show ip ospf neighbor json'"])
+ .output()?
+ .stdout,
+ )?;
+
+ let openfabric_neighbors: status::openfabric::Neighbors =
+ if openfabric_neighbors_string.is_empty() {
+ status::openfabric::Neighbors::default()
+ } else {
+ serde_json::from_str(&openfabric_neighbors_string)
+ .with_context(|| "error parsing openfabric neighbors")?
+ };
+
+ let ospf_neighbors: status::ospf::Neighbors = if ospf_neighbors_string.is_empty() {
+ status::ospf::Neighbors::default()
+ } else {
+ serde_json::from_str(&ospf_neighbors_string)
+ .with_context(|| "error parsing ospf neighbors")?
+ };
+
+ let neighbor_status = status::NeighborsParsed {
+ openfabric: openfabric_neighbors,
+ ospf: ospf_neighbors,
+ };
+
+ neighbor_status.try_into()
+ }
+
/// Return the status of all fabrics on this node.
///
/// Go through all fabrics in the config, then filter out the ones that exist on this node.
--
2.47.2
More information about the pve-devel
mailing list