all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Stefan Hanreich <s.hanreich@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH proxmox-firewall 3/4] firewall: config: query the network interfaces only once
Date: Fri, 20 Nov 2026 16:28:08 +0100	[thread overview]
Message-ID: <20261120152811.473269-4-s.hanreich@proxmox.com> (raw)
In-Reply-To: <20261120152811.473269-1-s.hanreich@proxmox.com>

Generating the ruleset queried the network interfaces of the host via
'ip -details -json link show' twice when there is a zone with a
conntrack zone: once for the conntrack zones of bridges in zone VRFs
and once for the altname mapping. Besides running the command twice per
ruleset update, the kernel state could change in between, so both
could be built from different views of the interfaces.

Query the interfaces once and build both from the same list. Drop
interface_mapping() from the config loader, as it is now derived from
network_links().

As the interfaces are always queried now, build the conntrack zones of
bridges whenever there is an SDN config, instead of only if a zone has
a conntrack zone. Pass the interfaces by reference, so that they do not
need to be cloned for the altname mapping.

Signed-off-by: Stefan Hanreich <s.hanreich@proxmox.com>
---
 proxmox-firewall/src/bin/proxmox-firewall.rs |  4 ++--
 proxmox-firewall/src/config.rs               | 23 ++++++--------------
 proxmox-firewall/src/sdn.rs                  | 23 +++++++++-----------
 proxmox-firewall/tests/integration_tests.rs  |  6 +----
 4 files changed, 20 insertions(+), 36 deletions(-)

diff --git a/proxmox-firewall/src/bin/proxmox-firewall.rs b/proxmox-firewall/src/bin/proxmox-firewall.rs
index 3281135..4ea3a76 100644
--- a/proxmox-firewall/src/bin/proxmox-firewall.rs
+++ b/proxmox-firewall/src/bin/proxmox-firewall.rs
@@ -81,9 +81,9 @@ fn handle_firewall() -> Result<(), Error> {
 
 fn handle_sdn_ct_zones(print: bool) -> Result<(), Error> {
     let sdn = load_sdn_config(&PveFirewallConfigLoader::new())?;
-    let links = get_network_interfaces()?.into_values();
+    let links = get_network_interfaces()?;
 
-    let ct_zones = vrf_ct_zones(sdn.as_ref().map(FirewallSdnConfig::config), links);
+    let ct_zones = vrf_ct_zones(sdn.as_ref().map(FirewallSdnConfig::config), links.values());
     let commands = vrf_ct_zones_commands(&ct_zones);
 
     if print {
diff --git a/proxmox-firewall/src/config.rs b/proxmox-firewall/src/config.rs
index 78ae162..587fcf9 100644
--- a/proxmox-firewall/src/config.rs
+++ b/proxmox-firewall/src/config.rs
@@ -26,7 +26,6 @@ use proxmox_nftables::types::ListChain;
 use proxmox_ve_config::sdn::{
     VnetName,
     config::{RunningConfig, SdnConfig},
-    conntrack::VrfCtZones,
     ipam::{Ipam, IpamJson},
 };
 
@@ -54,7 +53,6 @@ pub trait FirewallConfigLoader {
         &self,
         bridge_name: &BridgeName,
     ) -> Result<Option<Box<dyn io::BufRead>>, Error>;
-    fn interface_mapping(&self) -> Result<AltnameMapping, Error>;
     /// The network interfaces of the host, as reported by the kernel.
     fn network_links(&self) -> Result<Vec<IpLink>, Error>;
 }
@@ -283,12 +281,6 @@ impl FirewallConfigLoader for PveFirewallConfigLoader {
         Ok(None)
     }
 
-    fn interface_mapping(&self) -> Result<AltnameMapping, Error> {
-        Ok(AltnameMapping::from_iter(
-            get_network_interfaces()?.into_values(),
-        ))
-    }
-
     fn network_links(&self) -> Result<Vec<IpLink>, Error> {
         Ok(get_network_interfaces()?.into_values().collect())
     }
@@ -545,13 +537,12 @@ impl FirewallConfig {
         let bridge_config = Self::parse_bridges(firewall_loader)?;
         let (sdn_config, ipam_config, sdn_invalid) = Self::parse_sdn_and_ipam(firewall_loader)?;
 
-        // only query the kernel if there is any zone with a conntrack zone
-        let bridge_ct_zones = match &sdn_config {
-            Some(sdn) if !VrfCtZones::from(sdn.config()).is_empty() => {
-                BridgeCtZones::new(sdn.config(), firewall_loader.network_links()?)
-            }
-            _ => BridgeCtZones::default(),
-        };
+        let links = firewall_loader.network_links()?;
+
+        let bridge_ct_zones = sdn_config
+            .as_ref()
+            .map(|sdn| BridgeCtZones::new(sdn.config(), &links))
+            .unwrap_or_default();
 
         firewall_loader.check_mounted()?;
 
@@ -563,7 +554,7 @@ impl FirewallConfig {
             sdn_config,
             ipam_config,
             nft_config: Self::parse_nft(nft_loader)?,
-            interface_mapping: firewall_loader.interface_mapping()?,
+            interface_mapping: AltnameMapping::from_iter(links),
             sdn_invalid,
             bridge_ct_zones,
         })
diff --git a/proxmox-firewall/src/sdn.rs b/proxmox-firewall/src/sdn.rs
index ad86f85..3bcfdad 100644
--- a/proxmox-firewall/src/sdn.rs
+++ b/proxmox-firewall/src/sdn.rs
@@ -47,9 +47,9 @@ pub fn load_sdn_config(
 ///
 /// The members are taken from the kernel, which reflects all sources that put an interface into a
 /// VRF once the network configuration got applied.
-pub fn vrf_ct_zones(
+pub fn vrf_ct_zones<'a>(
     sdn: Option<&SdnConfig>,
-    links: impl IntoIterator<Item = IpLink>,
+    links: impl IntoIterator<Item = &'a IpLink>,
 ) -> VrfCtZones {
     let mut ct_zones = sdn.map(VrfCtZones::from).unwrap_or_default();
 
@@ -88,23 +88,20 @@ impl BridgeCtZones {
     /// Vnets are taken from the kernel as well, not from the SDN config, so that their conntrack
     /// zone follows the VRF their traffic is routed in, as for the inet hooks, and a bridge on a
     /// node that is not part of the zone does not get it just because of its name.
-    pub fn new(sdn: &SdnConfig, links: Vec<IpLink>) -> Self {
+    pub fn new(sdn: &SdnConfig, links: &[IpLink]) -> Self {
         let mut ct_zones = Self::default();
 
-        let links: HashMap<String, IpLink> = links
-            .into_iter()
-            .map(|link| (link.name().to_string(), link))
-            .collect();
+        let links: HashMap<&str, &IpLink> = links.iter().map(|link| (link.name(), link)).collect();
 
         let is_bridge = |name: &str| {
             links
                 .get(name)
-                .and_then(IpLink::linkinfo)
+                .and_then(|link| link.linkinfo())
                 .and_then(LinkInfo::info_kind)
                 .is_some_and(|kind| kind == "bridge")
         };
 
-        let vrf_ct_zones = vrf_ct_zones(Some(sdn), links.values().cloned());
+        let vrf_ct_zones = vrf_ct_zones(Some(sdn), links.values().copied());
 
         for (_, entry) in vrf_ct_zones.iter() {
             for name in entry.interfaces() {
@@ -240,7 +237,7 @@ mod tests {
             link("lo", None),
         ];
 
-        let interfaces: Vec<_> = vrf_ct_zones(Some(&sdn_config()), links)
+        let interfaces: Vec<_> = vrf_ct_zones(Some(&sdn_config()), &links)
             .interfaces()
             .map(|(interface, ct_zone)| (interface.to_string(), ct_zone.id()))
             .collect();
@@ -256,7 +253,7 @@ mod tests {
             ]
         );
 
-        assert!(vrf_ct_zones(None, [link("vnetr1", Some("vrf_red"))]).is_empty());
+        assert!(vrf_ct_zones(None, &[link("vnetr1", Some("vrf_red"))]).is_empty());
     }
 
     #[test]
@@ -308,7 +305,7 @@ mod tests {
             link("eth1.62", vlan("802.1Q", 62), Some("vrf_red")),
         ];
 
-        let ct_zones = BridgeCtZones::new(&sdn, links);
+        let ct_zones = BridgeCtZones::new(&sdn, &links);
 
         assert_eq!(
             ct_zones
@@ -341,7 +338,7 @@ mod tests {
 
     #[test]
     fn vrf_ct_zones_commands_with_zones() {
-        let ct_zones = vrf_ct_zones(Some(&sdn_config()), [link("vnetb", Some("vrf_blue"))]);
+        let ct_zones = vrf_ct_zones(Some(&sdn_config()), &[link("vnetb", Some("vrf_blue"))]);
 
         let elements = json!({"set": [
             ["vrf_blue", 16389],
diff --git a/proxmox-firewall/tests/integration_tests.rs b/proxmox-firewall/tests/integration_tests.rs
index 5b17e2e..ca557be 100644
--- a/proxmox-firewall/tests/integration_tests.rs
+++ b/proxmox-firewall/tests/integration_tests.rs
@@ -3,7 +3,7 @@ use std::collections::HashMap;
 
 use proxmox_firewall::config::{FirewallConfig, FirewallConfigLoader, NftConfigLoader};
 use proxmox_firewall::firewall::Firewall;
-use proxmox_network_api::{AltnameMapping, IpLink};
+use proxmox_network_api::IpLink;
 use proxmox_nftables::command::CommandOutput;
 use proxmox_sys::nodename;
 use proxmox_ve_config::guest::types::Vmid;
@@ -135,10 +135,6 @@ impl FirewallConfigLoader for MockFirewallConfigLoader {
         Ok(None)
     }
 
-    fn interface_mapping(&self) -> Result<AltnameMapping, Error> {
-        Ok(AltnameMapping::from_iter(vec![]))
-    }
-
     fn network_links(&self) -> Result<Vec<IpLink>, Error> {
         let link = |name: &str, kind: Option<&str>, master: Option<&str>, parent: Option<&str>| {
             let linkinfo = kind.map(|kind| match kind {
-- 
2.47.3





  parent reply	other threads:[~2026-11-20 15:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 15:28 [PATCH proxmox{-ve-rs,-firewall} 0/4] sdn: zone VRF conntrack zones: follow-ups Stefan Hanreich
2026-11-20 15:28 ` [PATCH proxmox-ve-rs 1/4] sdn: config: read the nodes of zones from the running config Stefan Hanreich
2026-11-20 15:28 ` [PATCH proxmox-ve-rs 2/4] sdn: conntrack: only map the VRFs of zones that exist on the node Stefan Hanreich
2026-11-20 15:28 ` Stefan Hanreich [this message]
2026-11-20 15:28 ` [PATCH proxmox-firewall 4/4] firewall: sdn: only apply the conntrack zones of zones on this node 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=20261120152811.473269-4-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal