From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id C30431FF165 for ; Thu, 22 May 2025 18:26:53 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 4F97CBDB9; Thu, 22 May 2025 18:26:17 +0200 (CEST) From: Stefan Hanreich To: pve-devel@lists.proxmox.com Date: Thu, 22 May 2025 18:16:46 +0200 Message-Id: <20250522161731.537011-31-s.hanreich@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250522161731.537011-1-s.hanreich@proxmox.com> References: <20250522161731.537011-1-s.hanreich@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.219 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 KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RDNS_NONE 0.793 Delivered to internal network by a host with no rDNS SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an SPF Record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [fabrics.rs] Subject: [pve-devel] [PATCH proxmox-perl-rs v3 4/5] pve-rs: sdn: fabrics: add helper to generate ifupdown2 configuration X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" SDN fabrics can be used to configure IP addresses on interfaces directly, so we need to generate the respective ifupdown2 configuration from the fabrics configuration. We also set some additional properties that are required for interfaces that are part of a fabric (IP forwarding). We use dummy interfaces, instead of loopback interfaces, for configuring the router IP of the node, so for each fabric we generate a dummy interface that carries the IP. Currently this is a simple implementation that builds a String from the SDN fabrics configuration, but in the future we intend to create a full-fledged crate for reading / writing ifupdown2 configuration files. Co-authored-by: Gabriel Goller Signed-off-by: Stefan Hanreich --- pve-rs/Cargo.toml | 1 + pve-rs/src/bindings/sdn/fabrics.rs | 104 +++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/pve-rs/Cargo.toml b/pve-rs/Cargo.toml index fbeff72..24184fd 100644 --- a/pve-rs/Cargo.toml +++ b/pve-rs/Cargo.toml @@ -37,6 +37,7 @@ proxmox-frr = { version = "0.1" } proxmox-http = { version = "0.9", features = ["client-sync", "client-trait"] } proxmox-http-error = "0.1.0" proxmox-log = "0.2" +proxmox-network-types = "0.1" proxmox-notify = { version = "0.5.4", features = ["pve-context"] } proxmox-openid = "0.10.4" proxmox-resource-scheduling = "0.3.0" diff --git a/pve-rs/src/bindings/sdn/fabrics.rs b/pve-rs/src/bindings/sdn/fabrics.rs index a7a740f..099c1a7 100644 --- a/pve-rs/src/bindings/sdn/fabrics.rs +++ b/pve-rs/src/bindings/sdn/fabrics.rs @@ -6,6 +6,8 @@ pub mod pve_rs_sdn_fabrics { //! / writing the configuration, as well as for generating ifupdown2 and FRR configuration. use std::collections::{BTreeMap, HashSet}; + use std::fmt::Write; + use std::net::IpAddr; use std::ops::Deref; use std::sync::Mutex; @@ -15,6 +17,7 @@ pub mod pve_rs_sdn_fabrics { use perlmod::Value; use proxmox_frr::serializer::to_raw_config; + use proxmox_network_types::ip_address::Cidr; use proxmox_section_config::typed::SectionConfigData; use proxmox_ve_config::common::valid::Validatable; @@ -349,4 +352,105 @@ pub mod pve_rs_sdn_fabrics { to_raw_config(&frr_config) } + + /// Helper method to generate the default /e/n/i config for a given CIDR. + fn render_interface(name: &str, cidr: Cidr, is_dummy: bool) -> Result { + let mut interface = String::new(); + + writeln!(interface)?; + writeln!(interface, "auto {name}")?; + match cidr { + Cidr::Ipv4(_) => writeln!(interface, "iface {name} inet static")?, + Cidr::Ipv6(_) => writeln!(interface, "iface {name} inet6 static")?, + } + writeln!(interface, "\taddress {cidr}")?; + if is_dummy { + writeln!(interface, "\tlink-type dummy")?; + } + writeln!(interface, "\tip-forward 1")?; + + Ok(interface) + } + + /// Class method: Generate the ifupdown2 configuration for a given node. + #[export] + fn get_interfaces_etc_network_config( + #[try_from_ref] this: &PerlFabricConfig, + node_id: NodeId, + ) -> Result { + let config = this.fabric_config.lock().unwrap(); + let mut interfaces = String::new(); + + let node_fabrics = config.values().filter_map(|entry| { + entry + .get_node(&node_id) + .map(|node| (entry.fabric(), node)) + .ok() + }); + + for (fabric, node) in node_fabrics { + // dummy interface + if let Some(ip) = node.ip() { + let interface = render_interface( + &format!("dummy_{}", fabric.id()), + Cidr::new_v4(ip, 32)?, + true, + )?; + write!(interfaces, "{interface}")?; + } + if let Some(ip6) = node.ip6() { + let interface = render_interface( + &format!("dummy_{}", fabric.id()), + Cidr::new_v6(ip6, 128)?, + true, + )?; + write!(interfaces, "{interface}")?; + } + match node { + ConfigNode::Openfabric(node_section) => { + for interface in node_section.properties().interfaces() { + if let Some(ip) = interface.ip() { + let interface = + render_interface(interface.name(), Cidr::from(ip), false)?; + write!(interfaces, "{interface}")?; + } + if let Some(ip) = interface.ip6() { + let interface = + render_interface(interface.name(), Cidr::from(ip), false)?; + write!(interfaces, "{interface}")?; + } + + // If not ip is configured, add auto and empty iface to bring interface up + if let (None, None) = (interface.ip(), interface.ip6()) { + writeln!(interfaces)?; + writeln!(interfaces, "auto {}", interface.name())?; + writeln!(interfaces, "iface {}", interface.name())?; + writeln!(interfaces, "\tip-forward 1")?; + } + } + } + ConfigNode::Ospf(node_section) => { + for interface in node_section.properties().interfaces() { + if let Some(ip) = interface.ip() { + let interface = + render_interface(interface.name(), Cidr::from(ip), false)?; + write!(interfaces, "{interface}")?; + } else { + let interface = render_interface( + interface.name(), + Cidr::from(IpAddr::from( + node.ip() + .ok_or(anyhow::anyhow!("there has to be a ipv4 address"))?, + )), + false, + )?; + write!(interfaces, "{interface}")?; + } + } + } + } + } + + Ok(interfaces) + } } -- 2.39.5 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel