Add a function that returns a list of all the routes which are distributed using the fabrics. For this we again need to read the config (in order to get the interface names and thus connect the fabric to the discovered route) and we need to query frr (using vtysh) for all the routes (ipv4 and ipv6) distributed by a specific protocol (once for openfabric and once for ospf). This method is used in the FabricContentView so that clicking on the fabric resource shows the routes distributed by the fabric.
Signed-off-by: Gabriel Goller <g.gol...@proxmox.com> --- pve-rs/src/bindings/sdn/fabrics.rs | 57 +++++++++++++++++++ pve-rs/src/sdn/status.rs | 88 ++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) diff --git a/pve-rs/src/bindings/sdn/fabrics.rs b/pve-rs/src/bindings/sdn/fabrics.rs index ef18ee80b0e1..14dad216dbf4 100644 --- a/pve-rs/src/bindings/sdn/fabrics.rs +++ b/pve-rs/src/bindings/sdn/fabrics.rs @@ -583,6 +583,63 @@ pub mod pve_rs_sdn_fabrics { Ok(interfaces) } + /// Get all the routes for all the fabrics on this node. + /// + /// Use FRR to get all the routes that have been inserted by either `openfabric` or 'ospf` and + /// associate them with the respective fabric by checking the interface they point to. Return a + /// single array with all routes. + #[export] + fn routes() -> Result<Vec<status::RouteStatus>, Error> { + let openfabric_ipv4_routes_string = String::from_utf8( + Command::new("sh") + .args(["-c", "vtysh -c 'show ip route openfabric json'"]) + .output()? + .stdout, + )?; + + let openfabric_ipv6_routes_string = String::from_utf8( + Command::new("sh") + .args(["-c", "vtysh -c 'show ipv6 route openfabric json'"]) + .output()? + .stdout, + )?; + + let ospf_routes_string = String::from_utf8( + Command::new("sh") + .args(["-c", "vtysh -c 'show ip route ospf json'"]) + .output()? + .stdout, + )?; + + let mut openfabric_routes: proxmox_frr::de::Routes = + if openfabric_ipv4_routes_string.is_empty() { + proxmox_frr::de::Routes::default() + } else { + serde_json::from_str(&openfabric_ipv4_routes_string) + .with_context(|| "error parsing openfabric ipv4 routes")? + }; + if !openfabric_ipv6_routes_string.is_empty() { + let openfabric_ipv6_routes: proxmox_frr::de::Routes = + serde_json::from_str(&openfabric_ipv6_routes_string) + .with_context(|| "error parsing openfabric ipv6 routes")?; + openfabric_routes.0.extend(openfabric_ipv6_routes.0); + } + + let ospf_routes: proxmox_frr::de::Routes = if ospf_routes_string.is_empty() { + proxmox_frr::de::Routes::default() + } else { + serde_json::from_str(&ospf_routes_string) + .with_context(|| "error parsing ospf routes")? + }; + + let route_status = status::RoutesParsed { + openfabric: openfabric_routes, + ospf: ospf_routes, + }; + + status::get_routes(route_status) + } + /// 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. diff --git a/pve-rs/src/sdn/status.rs b/pve-rs/src/sdn/status.rs index 5502ed3a10f5..b15ad0330732 100644 --- a/pve-rs/src/sdn/status.rs +++ b/pve-rs/src/sdn/status.rs @@ -8,6 +8,18 @@ use proxmox_ve_config::sdn::fabric::{ section_config::{fabric::FabricId, node::Node as ConfigNode}, }; +/// The status of a route. +/// +/// Contains the route, the fabric and protocol it belongs to and some extra nexthop +/// information. +#[derive(Debug, Serialize)] +pub struct RouteStatus { + route: String, + via: Vec<String>, + fabric_id: FabricId, + protocol: Protocol, +} + /// Protocol #[derive(Debug, Serialize, Clone, Copy)] pub enum Protocol { @@ -53,6 +65,82 @@ pub struct RoutesParsed { /// All ospf routes in FRR pub ospf: de::Routes, } + +/// Convert the passed frr routes into a list of all routes with properties associating +/// them to a fabric. +pub fn get_routes(routes: RoutesParsed) -> Result<Vec<RouteStatus>, anyhow::Error> { + let hostname = proxmox_sys::nodename(); + + // to associate a route to a fabric, we get all the interfaces which are associated + // with a fabric on this node and compare them with the interfaces on the route. + 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<RouteStatus> = Vec::new(); + + for (nodeid, node) in config.all_nodes() { + if nodeid.as_str() != hostname { + continue; + } + let fabric_id = node.id().fabric_id(); + + // get interfaces + let interface_names: HashSet<&str> = match node { + ConfigNode::Openfabric(n) => n + .properties() + .interfaces() + .map(|i| i.name().as_str()) + .collect(), + ConfigNode::Ospf(n) => n + .properties() + .interfaces() + .map(|i| i.name().as_str()) + .collect(), + }; + + let (current_protocol, all_routes) = match &node { + ConfigNode::Openfabric(_) => (Protocol::Openfabric, &routes.openfabric.0), + ConfigNode::Ospf(_) => (Protocol::Ospf, &routes.ospf.0), + }; + + for (route_key, route_list) in all_routes { + let mut route_belongs_to_fabric = false; + for route in route_list { + for nexthop in &route.nexthops { + if interface_names.contains(&nexthop.interface_name.as_str()) { + route_belongs_to_fabric = true; + break; + } + } + if route_belongs_to_fabric { + break; + } + } + + if route_belongs_to_fabric { + let mut via_list = Vec::new(); + for route in route_list { + for nexthop in &route.nexthops { + let via = if let Some(ip) = nexthop.ip { + ip.to_string() + } else { + nexthop.interface_name.clone() + }; + via_list.push(via); + } + } + + stats.push(RouteStatus { + route: route_key.to_string(), + via: via_list, + protocol: current_protocol, + fabric_id: fabric_id.clone(), + }); + } + } + } + Ok(stats) +} /// Get the status for each fabric using the parsed routes from frr /// /// Using the parsed routes we get from frr, filter and map them to a HashMap mapping every -- 2.47.2 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel