From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 670FC1FF0DA for ; Thu, 19 Nov 2026 20:44:02 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 1AEFB2197C; Thu, 19 Nov 2026 20:42:11 +0100 (CET) From: Gabriel Goller To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox-perl-rs v3 10/28] pve-rs: fabrics: include VRF routes in aggregate status Date: Thu, 19 Nov 2026 20:40:56 +0100 Message-ID: <20261119194116.575520-11-g.goller@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261119194116.575520-1-g.goller@proxmox.com> References: <20261119194116.575520-1-g.goller@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795117281982 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.218 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: MBWU2CTMSRPRRKYTTM4YWUNP2FO7L6KF X-Message-ID-Hash: MBWU2CTMSRPRRKYTTM4YWUNP2FO7L6KF X-MailFrom: g.goller@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Collect OSPF and BGP routes from every VRF used by a fabric on the local node, in addition to routes from the default routing table. Run the related commands in one vtysh process and merge their JSON output, including entries which share the same prefix. Match routes by VRF when deciding whether a fabric has active routes. This prevents routes from another routing table from affecting its status. Add a parser test for duplicate prefixes across VRFs. Signed-off-by: Gabriel Goller --- pve-rs/src/bindings/sdn/fabrics.rs | 211 ++++++++++++++++++----------- pve-rs/src/sdn/status.rs | 149 ++++++++++++++++---- 2 files changed, 251 insertions(+), 109 deletions(-) diff --git a/pve-rs/src/bindings/sdn/fabrics.rs b/pve-rs/src/bindings/sdn/fabrics.rs index 06ecd0d07eaf..c319e1201094 100644 --- a/pve-rs/src/bindings/sdn/fabrics.rs +++ b/pve-rs/src/bindings/sdn/fabrics.rs @@ -5,7 +5,7 @@ pub mod pve_rs_sdn_fabrics { //! This provides the configuration for the SDN fabrics, as well as helper methods for reading //! / writing the configuration, as well as for generating ifupdown2 and FRR configuration. - use std::collections::{BTreeMap, HashMap, HashSet}; + use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::fmt::Write; use std::net::IpAddr; use std::ops::Deref; @@ -980,13 +980,15 @@ pub mod pve_rs_sdn_fabrics { None => "show ip ospf neighbor json".to_string(), }; let ospf_neighbors_string = vtysh(&command)?; - let ospf_neighbors: proxmox_frr::de::ospf::Neighbors = - if ospf_neighbors_string.is_empty() { - proxmox_frr::de::ospf::Neighbors::default() - } else { - serde_json::from_str(&ospf_neighbors_string) - .with_context(|| "error parsing ospf neighbors")? - }; + let ospf_neighbors = if ospf_neighbors_string.is_empty() { + proxmox_frr::de::ospf::Neighbors::default() + } else { + let output = serde_json::from_str(&ospf_neighbors_string) + .with_context(|| "error parsing ospf neighbors")?; + let output = status::extract_vrf_status(output, vrf.as_deref())?; + serde_json::from_value(output) + .with_context(|| "error parsing ospf neighbors")? + }; status::get_neighbors_ospf( fabric_id, @@ -1056,13 +1058,15 @@ pub mod pve_rs_sdn_fabrics { None => "show ip ospf interface json".to_string(), }; let ospf_interfaces_string = vtysh(&command)?; - let ospf_interfaces: proxmox_frr::de::ospf::Interfaces = - if ospf_interfaces_string.is_empty() { - proxmox_frr::de::ospf::Interfaces::default() - } else { - serde_json::from_str(&ospf_interfaces_string) - .with_context(|| "error parsing ospf interfaces")? - }; + let ospf_interfaces = if ospf_interfaces_string.is_empty() { + proxmox_frr::de::ospf::Interfaces::default() + } else { + let output = serde_json::from_str(&ospf_interfaces_string) + .with_context(|| "error parsing ospf interfaces")?; + let output = status::extract_vrf_status(output, vrf.as_deref())?; + serde_json::from_value(output) + .with_context(|| "error parsing ospf interfaces")? + }; status::get_interfaces_ospf( fabric_id, @@ -1092,6 +1096,78 @@ pub mod pve_rs_sdn_fabrics { } } + fn extend_routes(destination: &mut proxmox_frr::de::Routes, source: proxmox_frr::de::Routes) { + for (prefix, routes) in source.0 { + destination.0.entry(prefix).or_default().extend(routes); + } + } + + /// Query and merge routes from multiple FRR commands in a single `vtysh` process. + /// + /// The exit status is ignored, like in the single-command queries: if FRR is not running, + /// `vtysh` fails without printing anything to stdout, which yields no routes. This is polled + /// by pvestatd, so an error here would be logged on every cycle. + fn query_routes( + commands: impl IntoIterator, + protocol: &str, + ) -> Result { + let mut command = Command::new("vtysh"); + command.env("VTYSH_HISTFILE", "/dev/null"); + for query in commands { + command.args(["-c", &query]); + } + + let output = match command.output() { + Ok(output) => output, + Err(err) if err.kind() == std::io::ErrorKind::NotFound => { + return Ok(proxmox_frr::de::Routes::default()); + } + Err(err) => return Err(err.into()), + }; + + parse_routes(&output.stdout, protocol) + } + + fn parse_routes(raw: &[u8], protocol: &str) -> Result { + let mut routes = proxmox_frr::de::Routes::default(); + let route_stream = + serde_json::Deserializer::from_slice(raw).into_iter::(); + for queried_routes in route_stream { + extend_routes( + &mut routes, + queried_routes.with_context(|| format!("error parsing {protocol} routes"))?, + ); + } + + Ok(routes) + } + + #[cfg(test)] + mod tests { + use super::parse_routes; + + #[test] + fn parses_batched_routes_with_duplicate_prefixes() { + let raw = br#" + {"10.0.0.0/24":[{"nexthops":[],"metric":10,"protocol":"bgp","vrfName":"vrf_a"}]} + {"10.0.0.0/24":[{"nexthops":[],"metric":10,"protocol":"bgp","vrfName":"vrf_b"}]} + "#; + + let routes = parse_routes(raw, "BGP").expect("batched routes should parse"); + let routes = routes.0.values().next().expect("route should exist"); + + assert_eq!(routes.len(), 2); + assert_eq!(routes[0].vrf_name, "vrf_a"); + assert_eq!(routes[1].vrf_name, "vrf_b"); + } + + #[test] + fn parses_empty_output() { + let routes = parse_routes(b"", "BGP").expect("empty output should parse"); + assert!(routes.0.is_empty()); + } + } + /// 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. @@ -1107,76 +1183,47 @@ pub mod pve_rs_sdn_fabrics { return Ok(HashMap::new()); }; - 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 bgp_ipv4_routes_string = String::from_utf8( - Command::new("sh") - .env("VTYSH_HISTFILE", "/dev/null") - .args(["-c", "vtysh -c 'show ip route bgp json'"]) - .output()? - .stdout, + let openfabric_routes = query_routes( + [ + "show ip route openfabric json".to_string(), + "show ipv6 route openfabric json".to_string(), + ], + "OpenFabric", )?; - let bgp_ipv6_routes_string = String::from_utf8( - Command::new("sh") - .env("VTYSH_HISTFILE", "/dev/null") - .args(["-c", "vtysh -c 'show ipv6 route bgp json'"]) - .output()? - .stdout, - )?; - - let mut bgp_routes: proxmox_frr::de::Routes = if bgp_ipv4_routes_string.is_empty() { - proxmox_frr::de::Routes::default() - } else { - serde_json::from_str(&bgp_ipv4_routes_string) - .with_context(|| "error parsing bgp ipv4 routes")? - }; - if !bgp_ipv6_routes_string.is_empty() { - let bgp_ipv6_routes: proxmox_frr::de::Routes = - serde_json::from_str(&bgp_ipv6_routes_string) - .with_context(|| "error parsing bgp ipv6 routes")?; - bgp_routes.0.extend(bgp_ipv6_routes.0); + let node_id = NodeId::from_string(proxmox_sys::nodename().to_string())?; + let ospf_vrfs: BTreeSet<_> = config + .values() + .filter(|entry| { + matches!(entry, FabricEntry::Ospf(_)) && entry.get_node(&node_id).is_ok() + }) + .filter_map(FabricEntry::fabric_vrf_name) + .collect(); + let mut ospf_commands = vec!["show ip route ospf json".to_string()]; + ospf_commands.extend( + ospf_vrfs + .iter() + .map(|vrf| format!("show ip route vrf {vrf} ospf json")), + ); + let ospf_routes = query_routes(ospf_commands, "OSPF")?; + + let bgp_vrfs: BTreeSet<_> = config + .values() + .filter(|entry| { + matches!(entry, FabricEntry::Bgp(_)) && entry.get_node(&node_id).is_ok() + }) + .filter_map(FabricEntry::fabric_vrf_name) + .collect(); + let mut bgp_commands = vec![ + "show ip route bgp json".to_string(), + "show ipv6 route bgp json".to_string(), + ]; + for vrf in bgp_vrfs { + bgp_commands.extend( + ["ip", "ipv6"].map(|family| format!("show {family} route vrf {vrf} bgp json")), + ); } + let bgp_routes = query_routes(bgp_commands, "BGP")?; let route_status = status::RoutesParsed { openfabric: openfabric_routes, diff --git a/pve-rs/src/sdn/status.rs b/pve-rs/src/sdn/status.rs index 7a1334d20804..369fe3a982b2 100644 --- a/pve-rs/src/sdn/status.rs +++ b/pve-rs/src/sdn/status.rs @@ -355,6 +355,20 @@ pub fn get_neighbors_openfabric( Ok(stats) } +/// Extract status output nested under a VRF name, or return non-VRF output unchanged. +pub fn extract_vrf_status( + mut output: serde_json::Value, + vrf: Option<&str>, +) -> Result { + match vrf { + Some(vrf) => output + .get_mut(vrf) + .map(serde_json::Value::take) + .ok_or_else(|| anyhow::anyhow!("missing status for VRF '{vrf}'")), + None => Ok(output), + } +} + /// Convert the parsed ospf neighbor neighbor information into a list of [`ospf::NeighborStatus`]. /// /// Ospf does not use the name of the fabric at all, so we again need to retrieve the interfaces of @@ -535,6 +549,10 @@ pub fn get_status( continue; } let fabric_id = node.id().fabric_id(); + let vrf_name = config + .get_fabric(fabric_id)? + .fabric_vrf_name() + .unwrap_or_else(|| "default".to_string()); let (current_protocol, all_routes) = match &node { ConfigNode::Openfabric(_) => (Protocol::Openfabric, &routes.openfabric.0), @@ -565,13 +583,14 @@ pub fn get_status( // determine status by checking if any routes exist for our interfaces let has_routes = all_routes.values().any(|v| { v.iter().any(|route| { - route.nexthops.iter().any(|nexthop| { - if let Some(iface_name) = &nexthop.interface_name { - interface_names.contains(iface_name.as_str()) - } else { - false - } - }) + route.vrf_name == vrf_name + && route.nexthops.iter().any(|nexthop| { + if let Some(iface_name) = &nexthop.interface_name { + interface_names.contains(iface_name.as_str()) + } else { + false + } + }) }) }); @@ -1400,11 +1419,8 @@ mod tests { } "#; - let neighbors: de::ospf::Neighbors = if json_output.is_empty() { - de::ospf::Neighbors::default() - } else { - serde_json::from_str(json_output).expect("error parsing json output") - }; + let neighbors: de::ospf::Neighbors = + serde_json::from_str(json_output).expect("error parsing json output"); let fabric_config = sample_one_fabric_config(); @@ -1436,6 +1452,94 @@ mod tests { assert_eq!(reference, output); } + #[test] + fn neighbors_vrf() { + let json_output = r#" + { + "vrf_vrf": { + "vrfName": "vrf_vrf", + "vrfId": 6, + "neighbors": { + "4.4.4.3": [{ + "nbrState": "Full\/-", + "upTime": "13m35s", + "ifaceAddress": "4.4.4.3", + "ifaceName": "nic1:4.4.4.1" + }], + "4.4.4.2": [{ + "nbrState": "Full\/-", + "upTime": "13m35s", + "ifaceAddress": "4.4.4.2", + "ifaceName": "nic2:4.4.4.1" + }] + } + }, + "other_vrf": { "neighbors": {} } + } + "#; + + let output = serde_json::from_str(json_output).unwrap(); + let output = extract_vrf_status(output, Some("vrf_vrf")).unwrap(); + let neighbors: de::ospf::Neighbors = serde_json::from_value(output).unwrap(); + assert_eq!(neighbors.neighbors.len(), 2); + for (address, interface) in [("4.4.4.3", "nic1"), ("4.4.4.2", "nic2")] { + let neighbor = &neighbors.neighbors[address]; + assert_eq!(neighbor.len(), 1); + assert_eq!(neighbor[0].interface_address, address); + assert_eq!(neighbor[0].interface_name, format!("{interface}:4.4.4.1")); + assert_eq!(neighbor[0].neighbor_state, "Full/-"); + assert_eq!(neighbor[0].up_time, "13m35s"); + } + } + + #[test] + fn interfaces_vrf() { + let json_output = r#" + { + "vrf_vrf": { + "vrfName": "vrf_vrf", + "vrfId": 6, + "interfaces": { + "dummy_fabric": { "ifUp": true, "networkType": "BROADCAST" }, + "nic1": { "ifUp": true, "networkType": "POINTOPOINT" }, + "nic2": { "ifUp": true, "networkType": "POINTOPOINT" } + } + }, + "other_vrf": { "interfaces": {} } + } + "#; + + let output = serde_json::from_str(json_output).unwrap(); + let output = extract_vrf_status(output, Some("vrf_vrf")).unwrap(); + let interfaces: de::ospf::Interfaces = serde_json::from_value(output).unwrap(); + assert_eq!(interfaces.interfaces.len(), 3); + for (name, ty) in [ + ("dummy_fabric", de::ospf::NetworkType::Broadcast), + ("nic1", de::ospf::NetworkType::PointToPoint), + ("nic2", de::ospf::NetworkType::PointToPoint), + ] { + let interface = &interfaces.interfaces[name]; + assert!(interface.if_up); + assert_eq!(interface.network_type, ty); + } + } + + #[test] + fn extract_non_vrf_status() { + for output in [ + serde_json::json!({ "neighbors": {} }), + serde_json::json!({ "interfaces": {} }), + ] { + assert_eq!(extract_vrf_status(output.clone(), None).unwrap(), output); + } + } + + #[test] + fn extract_missing_vrf_status() { + let output = serde_json::json!({ "other_vrf": { "neighbors": {} } }); + assert!(extract_vrf_status(output, Some("vrf_vrf")).is_err()); + } + #[test] fn neighbors_multiple_areas() { let json_output = r#" @@ -1509,11 +1613,8 @@ mod tests { } "#; - let neighbors: de::ospf::Neighbors = if json_output.is_empty() { - de::ospf::Neighbors::default() - } else { - serde_json::from_str(json_output).expect("error parsing json output") - }; + let neighbors: de::ospf::Neighbors = + serde_json::from_str(json_output).expect("error parsing json output"); let fabric_config = sample_two_fabric_config(); @@ -1672,11 +1773,8 @@ mod tests { } "#; - let interfaces: de::ospf::Interfaces = if json_output.is_empty() { - de::ospf::Interfaces::default() - } else { - serde_json::from_str(json_output).expect("error parsing json output") - }; + let interfaces: de::ospf::Interfaces = + serde_json::from_str(json_output).expect("error parsing json output"); let fabric_config = sample_one_fabric_config(); @@ -1904,11 +2002,8 @@ mod tests { } "#; - let interfaces: de::ospf::Interfaces = if json_output.is_empty() { - de::ospf::Interfaces::default() - } else { - serde_json::from_str(json_output).expect("error parsing json output") - }; + let interfaces: de::ospf::Interfaces = + serde_json::from_str(json_output).expect("error parsing json output"); let fabric_config = sample_two_fabric_config(); -- 2.47.3