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 108C81FF15C
	for <inbox@lore.proxmox.com>; Fri,  4 Apr 2025 18:30:58 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id A4807378F4;
	Fri,  4 Apr 2025 18:29:49 +0200 (CEST)
From: Gabriel Goller <g.goller@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Fri,  4 Apr 2025 18:28:19 +0200
Message-Id: <20250404162908.563060-9-g.goller@proxmox.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20250404162908.563060-1-g.goller@proxmox.com>
References: <20250404162908.563060-1-g.goller@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.023 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 v2 07/15] frr: add generic types
 over openfabric and ospf
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>

Add generic FRR types that contain openfabric and ospf variants. Also
add the FrrConfig, which holds the whole FRR configuration in a single
struct, which will then be serialized.

Signed-off-by: Gabriel Goller <g.goller@proxmox.com>
---
 proxmox-frr/src/lib.rs | 82 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 81 insertions(+), 1 deletion(-)

diff --git a/proxmox-frr/src/lib.rs b/proxmox-frr/src/lib.rs
index 42f0800c536a..e1b0ec8878d2 100644
--- a/proxmox-frr/src/lib.rs
+++ b/proxmox-frr/src/lib.rs
@@ -1,8 +1,9 @@
 pub mod openfabric;
 pub mod ospf;
 pub mod route_map;
-use std::{fmt::Display, str::FromStr};
+use std::{collections::BTreeMap, fmt::Display, str::FromStr};
 
+use crate::route_map::{AccessList, AccessListName, ProtocolRouteMap, RouteMap};
 use serde::{Deserialize, Serialize};
 use serde_with::{DeserializeFromStr, SerializeDisplay};
 use thiserror::Error;
@@ -15,6 +16,46 @@ pub enum RouterNameError {
     FrrWordError(#[from] FrrWordError),
 }
 
+/// Generic FRR router.
+///
+/// This generic FRR router contains all the variants that are allowed.
+#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize, PartialOrd, Ord)]
+pub enum Router {
+    OpenFabric(openfabric::OpenFabricRouter),
+    Ospf(ospf::OspfRouter),
+}
+
+impl From<openfabric::OpenFabricRouter> for Router {
+    fn from(value: openfabric::OpenFabricRouter) -> Self {
+        Router::OpenFabric(value)
+    }
+}
+
+/// Generic FRR routername.
+///
+/// Can vary between different router-types. Some have `router <protocol> <name>`, others have
+/// `router <protocol> <process-id>`.
+#[derive(Clone, Debug, PartialEq, Eq, Hash, SerializeDisplay, PartialOrd, Ord)]
+pub enum RouterName {
+    OpenFabric(openfabric::OpenFabricRouterName),
+    Ospf(ospf::OspfRouterName),
+}
+
+impl From<openfabric::OpenFabricRouterName> for RouterName {
+    fn from(value: openfabric::OpenFabricRouterName) -> Self {
+        Self::OpenFabric(value)
+    }
+}
+
+impl Display for RouterName {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        match self {
+            Self::OpenFabric(r) => r.fmt(f),
+            Self::Ospf(r) => r.fmt(f),
+        }
+    }
+}
+
 /// The interface name is the same on ospf and openfabric, but it is an enum so we can have two
 /// different entries in the hashmap. This allows us to have an interface in an ospf and openfabric
 /// fabric.
@@ -134,3 +175,42 @@ impl Display for CommonInterfaceName {
         self.0.fmt(f)
     }
 }
+
+/// Main FRR config.
+///
+/// Contains the two main frr building blocks: routers and interfaces. To ease construction use the
+/// [`FrrConfigBuilder`], which converts the intermediate representation to the FRR-specific
+/// representation here. Eventually we can add different control options here, such as: `line vty`.
+#[derive(Clone, Debug, PartialEq, Eq, Default)]
+pub struct FrrConfig {
+    pub router: BTreeMap<RouterName, Router>,
+    pub interfaces: BTreeMap<InterfaceName, Interface>,
+    pub access_lists: BTreeMap<AccessListName, AccessList>,
+    pub routemaps: Vec<RouteMap>,
+    pub protocol_routemaps: Vec<ProtocolRouteMap>,
+}
+
+impl FrrConfig {
+    pub fn new() -> Self {
+        Self::default()
+    }
+
+    pub fn router(&self) -> impl Iterator<Item = (&RouterName, &Router)> + '_ {
+        self.router.iter()
+    }
+
+    pub fn interfaces(&self) -> impl Iterator<Item = (&InterfaceName, &Interface)> + '_ {
+        self.interfaces.iter()
+    }
+
+    pub fn access_lists(&self) -> impl Iterator<Item = (&AccessListName, &AccessList)> + '_ {
+        self.access_lists.iter()
+    }
+    pub fn routemaps(&self) -> impl Iterator<Item = &RouteMap> + '_ {
+        self.routemaps.iter()
+    }
+
+    pub fn protocol_routemaps(&self) -> impl Iterator<Item = &ProtocolRouteMap> + '_ {
+        self.protocol_routemaps.iter()
+    }
+}
-- 
2.39.5



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel