public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Stefan Hanreich <s.hanreich@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH proxmox-perl-rs 1/1] pve-rs: fabrics: helper for mapping interface names
Date: Thu, 17 Jul 2025 17:28:37 +0200	[thread overview]
Message-ID: <20250717152841.397830-3-s.hanreich@proxmox.com> (raw)
In-Reply-To: <20250717152841.397830-1-s.hanreich@proxmox.com>

Add a helper method for proxmox-network-interface-pinning to rename
all interfaces of a node in the fabric config according to the mapping
generated by the pinning tool. This method renames all occurences of
an interface name to the pinned version. The logic has been taken and
reimplemented from the Perl helper itself.

Signed-off-by: Stefan Hanreich <s.hanreich@proxmox.com>
---
 pve-rs/src/bindings/sdn/fabrics.rs | 64 +++++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)

diff --git a/pve-rs/src/bindings/sdn/fabrics.rs b/pve-rs/src/bindings/sdn/fabrics.rs
index fd81c77..587b1d6 100644
--- a/pve-rs/src/bindings/sdn/fabrics.rs
+++ b/pve-rs/src/bindings/sdn/fabrics.rs
@@ -5,7 +5,7 @@ pub mod pve_rs_sdn_fabrics {
     //! 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 std::collections::{BTreeMap, HashSet};
+    use std::collections::{BTreeMap, HashMap, HashSet};
     use std::fmt::Write;
     use std::net::IpAddr;
     use std::ops::Deref;
@@ -26,6 +26,7 @@ pub mod pve_rs_sdn_fabrics {
         Fabric as ConfigFabric, FabricId,
         api::{Fabric, FabricUpdater},
     };
+    use proxmox_ve_config::sdn::fabric::section_config::interface::InterfaceName;
     use proxmox_ve_config::sdn::fabric::section_config::node::{
         Node as ConfigNode, NodeId,
         api::{Node, NodeUpdater},
@@ -304,6 +305,67 @@ pub mod pve_rs_sdn_fabrics {
             .map_err(anyhow::Error::from)
     }
 
+    fn map_name(
+        mapping: &HashMap<String, String>,
+        name: &str,
+    ) -> Result<Option<InterfaceName>, Error> {
+        match name.split_once('.') {
+            Some((interface_name, vlan_id))
+                if !vlan_id.is_empty() && vlan_id.chars().all(char::is_numeric) =>
+            {
+                mapping
+                    .get(interface_name)
+                    .map(|mapped_name| {
+                        InterfaceName::from_string(format!("{mapped_name}.{vlan_id}"))
+                    })
+                    .transpose()
+            }
+            _ => mapping
+                .get(name)
+                .cloned()
+                .map(InterfaceName::from_string)
+                .transpose(),
+        }
+    }
+
+    /// Method: Map all interface names of a node to a different one, according to the given
+    /// mapping.
+    ///
+    /// Used by proxmox-network-interface-pinning
+    #[export]
+    pub fn map_interfaces(
+        #[try_from_ref] this: &PerlFabricConfig,
+        node_id: NodeId,
+        mapping: HashMap<String, String>,
+    ) -> Result<(), Error> {
+        let mut config = this.fabric_config.lock().unwrap();
+
+        for entry in config.get_fabrics_mut() {
+            let Ok(node) = entry.get_node_mut(&node_id) else {
+                continue;
+            };
+
+            match node {
+                ConfigNode::Openfabric(node_section) => {
+                    for interface in node_section.properties_mut().interfaces_mut() {
+                        if let Some(mapped_name) = map_name(&mapping, &interface.name())? {
+                            interface.set_name(mapped_name);
+                        }
+                    }
+                }
+                ConfigNode::Ospf(node_section) => {
+                    for interface in node_section.properties_mut().interfaces_mut() {
+                        if let Some(mapped_name) = map_name(&mapping, &interface.name())? {
+                            interface.set_name(mapped_name);
+                        }
+                    }
+                }
+            }
+        }
+
+        Ok(())
+    }
+
     /// Method: Convert the configuration into the section config sections.
     ///
     /// Used for writing the running configuration.
-- 
2.39.5


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


  parent reply	other threads:[~2025-07-17 15:28 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-17 15:28 [pve-devel] [PATCH manager/proxmox{-ve-rs, -perl-rs} 0/6] proxmox-network-interface-pinning fixes Stefan Hanreich
2025-07-17 15:28 ` [pve-devel] [PATCH proxmox-ve-rs 1/1] ve-config: fabrics: add helpers for proxmox-network-interface-pinning Stefan Hanreich
2025-07-17 19:27   ` [pve-devel] applied: " Thomas Lamprecht
2025-07-17 15:28 ` Stefan Hanreich [this message]
2025-07-17 19:00   ` [pve-devel] applied: [PATCH proxmox-perl-rs 1/1] pve-rs: fabrics: helper for mapping interface names Thomas Lamprecht
2025-07-17 15:28 ` [pve-devel] [PATCH pve-manager 1/4] pve-sdn-commit: fix reloading logic Stefan Hanreich
2025-07-17 19:27   ` [pve-devel] applied-series: " Thomas Lamprecht
2025-07-17 15:28 ` [pve-devel] [PATCH pve-manager 2/4] proxmox-network-interface-pinning: add fabrics support Stefan Hanreich
2025-07-17 15:28 ` [pve-devel] [PATCH pve-manager 3/4] proxmox-network-interface-pinning: die on failing to write interfaces Stefan Hanreich
2025-07-17 15:28 ` [pve-devel] [PATCH pve-manager 4/4] proxmox-network-interface-pinning: fix pinning after reboot Stefan Hanreich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250717152841.397830-3-s.hanreich@proxmox.com \
    --to=s.hanreich@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal