From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <pve-devel-bounces@lists.proxmox.com> Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id E599A1FF164 for <inbox@lore.proxmox.com>; Fri, 28 Mar 2025 18:17:41 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id A65C39527; Fri, 28 Mar 2025 18:14:27 +0100 (CET) From: Gabriel Goller <g.goller@proxmox.com> To: pve-devel@lists.proxmox.com Date: Fri, 28 Mar 2025 18:13:00 +0100 Message-Id: <20250328171340.885413-13-g.goller@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250328171340.885413-1-g.goller@proxmox.com> References: <20250328171340.885413-1-g.goller@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.025 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH proxmox-ve-rs 11/17] frr: add serializer for all FRR types X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com> List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe> List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/> List-Post: <mailto:pve-devel@lists.proxmox.com> List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help> List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe> Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com> This custom serializer will serialize all the FRR types into a string, which is the FRR config. Signed-off-by: Gabriel Goller <g.goller@proxmox.com> --- proxmox-frr/src/lib.rs | 2 + proxmox-frr/src/serializer.rs | 192 ++++++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 proxmox-frr/src/serializer.rs diff --git a/proxmox-frr/src/lib.rs b/proxmox-frr/src/lib.rs index 08abdd371653..5b2bdc2c9d72 100644 --- a/proxmox-frr/src/lib.rs +++ b/proxmox-frr/src/lib.rs @@ -1,6 +1,8 @@ pub mod openfabric; pub mod ospf; pub mod route_map; +pub mod serializer; + use std::{collections::BTreeMap, fmt::Display, str::FromStr}; use route_map::{AccessList, AccessListName, ProtocolRouteMap, RouteMap}; diff --git a/proxmox-frr/src/serializer.rs b/proxmox-frr/src/serializer.rs new file mode 100644 index 000000000000..631956aeb099 --- /dev/null +++ b/proxmox-frr/src/serializer.rs @@ -0,0 +1,192 @@ +use std::fmt::{self, Write}; + +use crate::{ + openfabric::{OpenFabricInterface, OpenFabricRouter}, + ospf::{OspfInterface, OspfRouter}, + route_map::{AccessList, AccessListName, ProtocolRouteMap, RouteMap}, + FrrConfig, Interface, InterfaceName, Router, RouterName, +}; + +pub struct FrrConfigBlob<'a> { + buf: &'a mut (dyn Write + 'a), +} + +impl Write for FrrConfigBlob<'_> { + fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> { + self.buf.write_str(s) + } +} + +pub trait FrrSerializer { + fn serialize(&self, f: &mut FrrConfigBlob<'_>) -> fmt::Result; +} + +pub fn to_raw_config(frr_config: &FrrConfig) -> Result<Vec<String>, anyhow::Error> { + let mut out = String::new(); + let mut blob = FrrConfigBlob { buf: &mut out }; + frr_config.serialize(&mut blob)?; + + Ok(out.as_str().lines().map(String::from).collect()) +} + +pub fn dump(config: &FrrConfig) -> Result<String, anyhow::Error> { + let mut out = String::new(); + let mut blob = FrrConfigBlob { buf: &mut out }; + config.serialize(&mut blob)?; + Ok(out) +} + +impl FrrSerializer for &FrrConfig { + fn serialize(&self, f: &mut FrrConfigBlob<'_>) -> fmt::Result { + self.router().try_for_each(|router| router.serialize(f))?; + self.interfaces() + .try_for_each(|interface| interface.serialize(f))?; + self.access_lists().try_for_each(|list| list.serialize(f))?; + self.routemaps().try_for_each(|map| map.serialize(f))?; + self.protocol_routemaps() + .try_for_each(|pm| pm.serialize(f))?; + Ok(()) + } +} + +impl FrrSerializer for (&RouterName, &Router) { + fn serialize(&self, f: &mut FrrConfigBlob<'_>) -> fmt::Result { + let router_name = self.0; + let router = self.1; + writeln!(f, "router {router_name}")?; + router.serialize(f)?; + writeln!(f, "exit")?; + writeln!(f, "!")?; + Ok(()) + } +} + +impl FrrSerializer for (&InterfaceName, &Interface) { + fn serialize(&self, f: &mut FrrConfigBlob<'_>) -> fmt::Result { + let interface_name = self.0; + let interface = self.1; + writeln!(f, "interface {interface_name}")?; + interface.serialize(f)?; + writeln!(f, "exit")?; + writeln!(f, "!")?; + Ok(()) + } +} + +impl FrrSerializer for (&AccessListName, &AccessList) { + fn serialize(&self, f: &mut FrrConfigBlob<'_>) -> fmt::Result { + self.1.serialize(f)?; + writeln!(f, "!") + } +} + +impl FrrSerializer for Interface { + fn serialize(&self, f: &mut FrrConfigBlob<'_>) -> fmt::Result { + match self { + Interface::OpenFabric(openfabric_interface) => openfabric_interface.serialize(f)?, + Interface::Ospf(ospf_interface) => ospf_interface.serialize(f)?, + } + Ok(()) + } +} + +impl FrrSerializer for OpenFabricInterface { + fn serialize(&self, f: &mut FrrConfigBlob<'_>) -> fmt::Result { + if self.is_ipv6 { + writeln!(f, " ipv6 router {}", self.fabric_id())?; + } else { + writeln!(f, " ip router {}", self.fabric_id())?; + } + if self.passive() == Some(true) { + writeln!(f, " openfabric passive")?; + } + if let Some(interval) = self.hello_interval() { + writeln!(f, " openfabric hello-interval {interval}",)?; + } + if let Some(multiplier) = self.hello_multiplier() { + writeln!(f, " openfabric hello-multiplier {multiplier}",)?; + } + if let Some(interval) = self.csnp_interval() { + writeln!(f, " openfabric csnp-interval {interval}",)?; + } + Ok(()) + } +} + +impl FrrSerializer for OspfInterface { + fn serialize(&self, f: &mut FrrConfigBlob<'_>) -> fmt::Result { + writeln!(f, " ip ospf {}", self.area())?; + if *self.passive() == Some(true) { + writeln!(f, " ip ospf passive")?; + } + if let Some(network_type) = self.network_type() { + writeln!(f, " ip ospf network {network_type}")?; + } + Ok(()) + } +} + +impl FrrSerializer for &Router { + fn serialize(&self, f: &mut FrrConfigBlob<'_>) -> fmt::Result { + match self { + Router::OpenFabric(open_fabric_router) => open_fabric_router.serialize(f), + Router::Ospf(ospf_router) => ospf_router.serialize(f), + } + } +} + +impl FrrSerializer for &OpenFabricRouter { + fn serialize(&self, f: &mut FrrConfigBlob<'_>) -> fmt::Result { + writeln!(f, " net {}", self.net())?; + Ok(()) + } +} + +impl FrrSerializer for &OspfRouter { + fn serialize(&self, f: &mut FrrConfigBlob<'_>) -> fmt::Result { + writeln!(f, " ospf router-id {}", self.router_id())?; + Ok(()) + } +} + +impl FrrSerializer for &AccessList { + fn serialize(&self, f: &mut FrrConfigBlob<'_>) -> fmt::Result { + for i in &self.rules { + if i.network.is_ipv6() { + write!(f, "ipv6 ")?; + } + write!(f, "access-list {} ", self.name)?; + if let Some(seq) = i.seq { + write!(f, "seq {seq} ")?; + } + write!(f, "{} ", i.action)?; + writeln!(f, "{}", i.network)?; + } + Ok(()) + } +} + +impl FrrSerializer for &RouteMap { + fn serialize(&self, f: &mut FrrConfigBlob<'_>) -> fmt::Result { + writeln!(f, "route-map {} {} {}", self.name, self.action, self.seq)?; + for i in &self.matches { + writeln!(f, " {}", i)?; + } + for i in &self.sets { + writeln!(f, " {}", i)?; + } + writeln!(f, "exit")?; + writeln!(f, "!") + } +} + +impl FrrSerializer for &ProtocolRouteMap { + fn serialize(&self, f: &mut FrrConfigBlob<'_>) -> fmt::Result { + writeln!( + f, + "ip protocol {} route-map {}", + self.protocol, self.routemap_name + )?; + Ok(()) + } +} -- 2.39.5 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel