From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 94E4E1FF0AF for ; Fri, 20 Nov 2026 16:28:45 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 17F7C21819; Fri, 20 Nov 2026 16:28:25 +0100 (CET) From: Stefan Hanreich 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 Message-ID: <20261120152811.473269-4-s.hanreich@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261120152811.473269-1-s.hanreich@proxmox.com> References: <20261120152811.473269-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: 1795188499630 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.214 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust 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: 7SJTGIZTA4O7Z6I34QMGCP5YGMJPUNWU X-Message-ID-Hash: 7SJTGIZTA4O7Z6I34QMGCP5YGMJPUNWU 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: 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 --- 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>, Error>; - fn interface_mapping(&self) -> Result; /// The network interfaces of the host, as reported by the kernel. fn network_links(&self) -> Result, Error>; } @@ -283,12 +281,6 @@ impl FirewallConfigLoader for PveFirewallConfigLoader { Ok(None) } - fn interface_mapping(&self) -> Result { - Ok(AltnameMapping::from_iter( - get_network_interfaces()?.into_values(), - )) - } - fn network_links(&self) -> Result, 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, + links: impl IntoIterator, ) -> 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) -> Self { + pub fn new(sdn: &SdnConfig, links: &[IpLink]) -> Self { let mut ct_zones = Self::default(); - let links: HashMap = 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 { - Ok(AltnameMapping::from_iter(vec![])) - } - fn network_links(&self) -> Result, Error> { let link = |name: &str, kind: Option<&str>, master: Option<&str>, parent: Option<&str>| { let linkinfo = kind.map(|kind| match kind { -- 2.47.3