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 AFCBF1FF13A for ; Wed, 01 Apr 2026 16:42:40 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 870C5323A2; Wed, 1 Apr 2026 16:41:01 +0200 (CEST) From: Stefan Hanreich To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox-perl-rs v2 18/34] pve-rs: sdn: add prefix lists module Date: Wed, 1 Apr 2026 16:39:27 +0200 Message-ID: <20260401143957.386809-19-s.hanreich@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260401143957.386809-1-s.hanreich@proxmox.com> References: <20260401143957.386809-1-s.hanreich@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1775054349058 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.296 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_MAILER 2 Automated Mailer Tag Left in Email 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: ORGIFZXCN3HCZVQBENIQOQT7F75DGR3Y X-Message-ID-Hash: ORGIFZXCN3HCZVQBENIQOQT7F75DGR3Y X-MailFrom: s.hanreich@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: Exposes the functionality from ve-config to Perl by providing helpers for instantiating the Rust configuration from Perl. The module also contains the implementation for the CRUD API methods, which will be used for implementing the API methods in pve-network. Signed-off-by: Stefan Hanreich --- pve-rs/Makefile | 1 + pve-rs/src/bindings/sdn/mod.rs | 1 + pve-rs/src/bindings/sdn/prefix_lists.rs | 192 ++++++++++++++++++++++++ 3 files changed, 194 insertions(+) create mode 100644 pve-rs/src/bindings/sdn/prefix_lists.rs diff --git a/pve-rs/Makefile b/pve-rs/Makefile index d662b00..e7dfe2e 100644 --- a/pve-rs/Makefile +++ b/pve-rs/Makefile @@ -31,6 +31,7 @@ PERLMOD_PACKAGES := \ PVE::RS::OpenId \ PVE::RS::ResourceScheduling::Static \ PVE::RS::SDN::Fabrics \ + PVE::RS::SDN::PrefixLists \ PVE::RS::SDN::RouteMaps \ PVE::RS::SDN \ PVE::RS::TFA diff --git a/pve-rs/src/bindings/sdn/mod.rs b/pve-rs/src/bindings/sdn/mod.rs index c571d28..9bddf1c 100644 --- a/pve-rs/src/bindings/sdn/mod.rs +++ b/pve-rs/src/bindings/sdn/mod.rs @@ -1,4 +1,5 @@ pub(crate) mod fabrics; +pub(crate) mod prefix_lists; pub(crate) mod route_maps; #[perlmod::package(name = "PVE::RS::SDN", lib = "pve_rs")] diff --git a/pve-rs/src/bindings/sdn/prefix_lists.rs b/pve-rs/src/bindings/sdn/prefix_lists.rs new file mode 100644 index 0000000..e3d7598 --- /dev/null +++ b/pve-rs/src/bindings/sdn/prefix_lists.rs @@ -0,0 +1,192 @@ +#[perlmod::package(name = "PVE::RS::SDN::PrefixLists", lib = "pve_rs")] +pub mod pve_rs_sdn_prefix_lists { + //! The `PVE::RS::SDN::PrefixLists` package. + //! + //! 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 core::clone::Clone; + use std::collections::hash_map::Entry; + use std::collections::HashMap; + use std::ops::Deref; + use std::sync::Mutex; + + use anyhow::{anyhow, Error}; + use openssl::hash::{hash, MessageDigest}; + use serde::{Deserialize, Serialize}; + + use perlmod::Value; + use proxmox_section_config::typed::{ApiSectionDataEntry, SectionConfigData}; + use proxmox_ve_config::sdn::prefix_list::api::{ + PrefixList as ApiPrefixList, PrefixListDeletableProperties, PrefixListUpdater, + }; + use proxmox_ve_config::sdn::prefix_list::{PrefixList as ConfigPrefixList, PrefixListId}; + + /// A SDN PrefixList config instance. + #[derive(Serialize, Deserialize)] + pub struct PerlPrefixListConfig { + /// The fabric config instance + pub prefix_lists: Mutex>, + } + + perlmod::declare_magic!(Box : &PerlPrefixListConfig as "PVE::RS::SDN::PrefixLists::Config"); + + /// Class method: Parse the raw configuration from `/etc/pve/sdn/prefix-lists.cfg`. + #[export] + pub fn config(#[raw] class: Value, raw_config: &[u8]) -> Result { + let raw_config = std::str::from_utf8(raw_config)?; + let config = ConfigPrefixList::parse_section_config("prefix-lists.cfg", raw_config)?; + + Ok( + perlmod::instantiate_magic!(&class, MAGIC => Box::new(PerlPrefixListConfig { + prefix_lists: Mutex::new(config.deref().clone()), + })), + ) + } + + /// Class method: Parse the configuration from `/etc/pve/sdn/.running_config`. + #[export] + pub fn running_config( + #[raw] class: Value, + prefix_lists: HashMap, + ) -> Result { + let prefix_lists: SectionConfigData = + SectionConfigData::from_iter(prefix_lists); + + Ok( + perlmod::instantiate_magic!(&class, MAGIC => Box::new(PerlPrefixListConfig { + prefix_lists: Mutex::new(prefix_lists.deref().clone()), + })), + ) + } + + /// Method: Used for writing the running configuration. + #[export] + pub fn to_sections( + #[try_from_ref] this: &PerlPrefixListConfig, + ) -> Result, Error> { + let config = this.prefix_lists.lock().unwrap(); + Ok(config.deref().clone()) + } + + /// Method: Convert the configuration into the section config string. + /// + /// Used for writing `/etc/pve/sdn/prefix-lists.cfg` + #[export] + pub fn to_raw(#[try_from_ref] this: &PerlPrefixListConfig) -> Result { + let config = this.prefix_lists.lock().unwrap(); + + let prefix_lists: SectionConfigData = + SectionConfigData::from_iter(config.deref().clone()); + + ConfigPrefixList::write_section_config("prefix-lists.cfg", &prefix_lists) + } + + /// Method: Generate a digest for the whole configuration + #[export] + pub fn digest(#[try_from_ref] this: &PerlPrefixListConfig) -> Result { + let config = to_raw(this)?; + let hash = hash(MessageDigest::sha256(), config.as_bytes())?; + + Ok(hex::encode(hash)) + } + + /// Method: Returns all prefix lists as a hash indexed with the IDs of the prefix lists. + #[export] + pub fn list( + #[try_from_ref] this: &PerlPrefixListConfig, + ) -> Result, Error> { + Ok(this + .prefix_lists + .lock() + .unwrap() + .iter() + .map(|(id, prefix_list)| { + let ConfigPrefixList::PrefixList(prefix_list) = prefix_list; + (id.clone(), prefix_list.clone()) + }) + .collect()) + } + + /// Method: Create a new PrefixList. + #[export] + pub fn create( + #[try_from_ref] this: &PerlPrefixListConfig, + prefix_list: ApiPrefixList, + ) -> Result<(), Error> { + let mut prefix_lists = this.prefix_lists.lock().unwrap(); + + match prefix_lists.entry(prefix_list.id().to_string()) { + Entry::Occupied(_) => anyhow::bail!( + "prefix list already exists in configuration: {}", + prefix_list.id() + ), + Entry::Vacant(vacancy) => vacancy.insert(ConfigPrefixList::PrefixList(prefix_list)), + }; + + Ok(()) + } + + /// Method: Get a specific PrefixList. + #[export] + pub fn get( + #[try_from_ref] this: &PerlPrefixListConfig, + id: PrefixListId, + ) -> Result, Error> { + Ok(this + .prefix_lists + .lock() + .unwrap() + .get(&id.to_string()) + .map(|prefix_list| { + let ConfigPrefixList::PrefixList(prefix_list) = prefix_list; + prefix_list.clone() + })) + } + + /// Method: Update a PrefixList. + #[export] + pub fn update( + #[try_from_ref] this: &PerlPrefixListConfig, + id: PrefixListId, + updater: PrefixListUpdater, + delete: Option>, + ) -> Result<(), Error> { + let mut prefix_lists = this.prefix_lists.lock().unwrap(); + + let ConfigPrefixList::PrefixList(prefix_list) = prefix_lists + .get_mut(id.as_str()) + .ok_or_else(|| anyhow!("Could not find prefix list with id: {}", id))?; + + let PrefixListUpdater { entries } = updater; + + if let Some(entries) = entries { + prefix_list.entries = entries; + } + + for deletable_property in delete.unwrap_or_default() { + match deletable_property { + PrefixListDeletableProperties::Entries => { + prefix_list.entries = Vec::new(); + } + } + } + + Ok(()) + } + + /// Method: Delete a PrefixList. + #[export] + pub fn delete( + #[try_from_ref] this: &PerlPrefixListConfig, + id: PrefixListId, + ) -> Result<(), Error> { + this.prefix_lists + .lock() + .unwrap() + .remove(&id.to_string()) + .ok_or_else(|| anyhow!("could not find route map entry with id: {id}"))?; + + Ok(()) + } +} -- 2.47.3