[pve-devel] [PATCH proxmox-ve-rs 11/17] frr: add serializer for all FRR types
Gabriel Goller
g.goller at proxmox.com
Fri Mar 28 18:13:00 CET 2025
This custom serializer will serialize all the FRR types into a string,
which is the FRR config.
Signed-off-by: Gabriel Goller <g.goller at 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
More information about the pve-devel
mailing list