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 6ED741FF0DA for ; Thu, 19 Nov 2026 20:46:37 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id EA73121841; Thu, 19 Nov 2026 20:46:11 +0100 (CET) From: Stefan Hanreich To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox-ve-rs 03/14] sdn: conntrack: map the interfaces of zone VRFs to their conntrack zone Date: Thu, 19 Nov 2026 20:45:35 +0100 Message-ID: <20261119194547.674512-4-s.hanreich@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261119194547.674512-1-s.hanreich@proxmox.com> References: <20261119194547.674512-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: 1795117556946 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.237 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: XWXHUKLREYZAQ7TTVZUDJWEO2RQA2VJZ X-Message-ID-Hash: XWXHUKLREYZAQ7TTVZUDJWEO2RQA2VJZ 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: Add VrfCtZones, which maps the VRF device of each zone with a conntrack zone to the interfaces in that VRF. Interfaces are added by the name of their VRF device, so the mapping can be filled from the kernel, which reports the master of every interface. The kernel state is what forwards the traffic once the network configuration is applied, and it covers every way an interface ends up in a VRF (vnets, fabrics, /etc/network/interfaces) without collecting them from the configuration. Signed-off-by: Stefan Hanreich --- proxmox-ve-config/src/sdn/conntrack.rs | 160 +++++++++++++++++++++++++ proxmox-ve-config/tests/sdn/main.rs | 15 ++- 2 files changed, 174 insertions(+), 1 deletion(-) diff --git a/proxmox-ve-config/src/sdn/conntrack.rs b/proxmox-ve-config/src/sdn/conntrack.rs index ff363646..82f93de0 100644 --- a/proxmox-ve-config/src/sdn/conntrack.rs +++ b/proxmox-ve-config/src/sdn/conntrack.rs @@ -5,8 +5,12 @@ //! (`ct zone & 0xc000 == 0x4000`). Zones with a dedicated VRF get their conntrack zone from the //! first 1024 zones of this block. +use std::collections::{BTreeMap, BTreeSet}; + use serde::Deserialize; +use crate::sdn::config::{SdnConfig, ZoneConfig}; + /// The conntrack zone of an SDN zone with a dedicated VRF. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Deserialize)] #[serde(try_from = "u16")] @@ -51,6 +55,98 @@ impl TryFrom for VrfCtZone { } } +/// A zone VRF with a conntrack zone and the interfaces enslaved to it. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct VrfCtZoneEntry { + ct_zone: VrfCtZone, + interfaces: BTreeSet, +} + +impl VrfCtZoneEntry { + /// The conntrack zone of the VRF. + pub fn ct_zone(&self) -> VrfCtZone { + self.ct_zone + } + + /// The interfaces enslaved to the VRF, not including the VRF device itself. + pub fn interfaces(&self) -> impl Iterator + '_ { + self.interfaces.iter().map(String::as_str) + } +} + +/// The zone VRFs with a conntrack zone, keyed by the name of their VRF device. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct VrfCtZones { + vrfs: BTreeMap, +} + +impl VrfCtZones { + /// The name of the VRF device of a zone with a dedicated VRF. + pub fn vrf_name(zone: &ZoneConfig) -> String { + format!("vrf_{}", zone.name()) + } + + /// Adds the VRF of a zone, if the zone has a conntrack zone. + pub fn add_zone(&mut self, zone: &ZoneConfig) { + if let Some(ct_zone) = zone.ct_zone() { + self.vrfs.insert( + Self::vrf_name(zone), + VrfCtZoneEntry { + ct_zone, + interfaces: BTreeSet::new(), + }, + ); + } + } + + /// Adds an interface to the VRF device `vrf`. + /// + /// Returns `false` and ignores the interface if `vrf` is not the VRF of a zone with a + /// conntrack zone. + pub fn add_interface(&mut self, vrf: &str, interface: impl Into) -> bool { + match self.vrfs.get_mut(vrf) { + Some(entry) => { + entry.interfaces.insert(interface.into()); + true + } + None => false, + } + } + + pub fn is_empty(&self) -> bool { + self.vrfs.is_empty() + } + + /// Iterates over the VRF devices and their entries. + pub fn iter(&self) -> impl Iterator + '_ { + self.vrfs.iter().map(|(vrf, entry)| (vrf.as_str(), entry)) + } + + /// Iterates over all interfaces whose traffic belongs to a conntrack zone. + /// + /// These are the VRF devices themselves, as packets are looked up again when passing them, and + /// the interfaces enslaved to them. + pub fn interfaces(&self) -> impl Iterator + '_ { + self.iter().flat_map(|(vrf, entry)| { + std::iter::once(vrf) + .chain(entry.interfaces()) + .map(|interface| (interface, entry.ct_zone())) + }) + } +} + +impl From<&SdnConfig> for VrfCtZones { + fn from(config: &SdnConfig) -> Self { + let mut ct_zones = Self::default(); + + for zone in config.zones() { + ct_zones.add_zone(zone); + } + + ct_zones + } +} + #[cfg(test)] mod tests { use super::*; @@ -62,4 +158,68 @@ mod tests { assert_eq!(VrfCtZone::new(16383), None); assert_eq!(VrfCtZone::new(17408), None); } + + #[test] + fn vrf_ct_zones() { + use crate::sdn::ZoneName; + use crate::sdn::config::ZoneType; + + let zone = |name: &str, ct_zone: Option| { + let mut zone = + ZoneConfig::new(ZoneName::new(name.to_string()).unwrap(), ZoneType::Simple); + zone.set_ct_zone(ct_zone.and_then(VrfCtZone::new)); + zone + }; + + let mut ct_zones = VrfCtZones::default(); + assert!(ct_zones.is_empty()); + + ct_zones.add_zone(&zone("red", Some(16384))); + ct_zones.add_zone(&zone("blue", Some(16389))); + ct_zones.add_zone(&zone("green", None)); + + assert!(ct_zones.add_interface("vrf_red", "vnetr1")); + assert!(ct_zones.add_interface("vrf_red", "bond0.101")); + assert!(ct_zones.add_interface("vrf_blue", "vnetb")); + // VRFs that are not zone VRFs with a conntrack zone + assert!(!ct_zones.add_interface("vrf_green", "vnetg")); + assert!(!ct_zones.add_interface("vrf_mgmt", "eth0")); + // the prefix is part of the name + assert!(!ct_zones.add_interface("red", "eth1")); + + let entries: Vec<_> = ct_zones + .iter() + .map(|(vrf, entry)| { + ( + vrf, + entry.ct_zone().id(), + entry.interfaces().collect::>(), + ) + }) + .collect(); + + assert_eq!( + entries, + [ + ("vrf_blue", 16389, vec!["vnetb"]), + ("vrf_red", 16384, vec!["bond0.101", "vnetr1"]), + ] + ); + + let interfaces: Vec<_> = ct_zones + .interfaces() + .map(|(interface, ct_zone)| (interface, ct_zone.id())) + .collect(); + + assert_eq!( + interfaces, + [ + ("vrf_blue", 16389), + ("vnetb", 16389), + ("vrf_red", 16384), + ("bond0.101", 16384), + ("vnetr1", 16384), + ] + ); + } } diff --git a/proxmox-ve-config/tests/sdn/main.rs b/proxmox-ve-config/tests/sdn/main.rs index ad982bd1..76ef25f2 100644 --- a/proxmox-ve-config/tests/sdn/main.rs +++ b/proxmox-ve-config/tests/sdn/main.rs @@ -11,7 +11,7 @@ use proxmox_ve_config::sdn::{ config::{ RunningConfig, SdnConfig, SdnConfigError, SubnetConfig, VnetConfig, ZoneConfig, ZoneType, }, - conntrack::VrfCtZone, + conntrack::{VrfCtZone, VrfCtZones}, ipam::{Ipam, IpamDataVm, IpamEntry, IpamJson}, }; @@ -169,6 +169,19 @@ fn parse_running_config_ct_zone() { assert_eq!(ct_zone("zone2"), None); assert_eq!(ct_zone("zone3"), None); assert_eq!(ct_zone("zone4"), None); + + let vrfs: Vec<_> = VrfCtZones::from(&sdn_config) + .iter() + .map(|(vrf, entry)| (vrf.to_string(), entry.ct_zone().id())) + .collect(); + + assert_eq!( + vrfs, + [ + ("vrf_zone0".to_string(), 16384), + ("vrf_zone1".to_string(), 17407) + ] + ); } #[test] -- 2.47.3