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: [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	[thread overview]
Message-ID: <20261119194547.674512-4-s.hanreich@proxmox.com> (raw)
In-Reply-To: <20261119194547.674512-1-s.hanreich@proxmox.com>

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 <s.hanreich@proxmox.com>
---
 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<u16> 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<String>,
+}
+
+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<Item = &str> + '_ {
+        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<String, VrfCtZoneEntry>,
+}
+
+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<String>) -> 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<Item = (&str, &VrfCtZoneEntry)> + '_ {
+        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<Item = (&str, VrfCtZone)> + '_ {
+        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<u16>| {
+            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::<Vec<_>>(),
+                )
+            })
+            .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





  parent reply	other threads:[~2026-11-19 19:46 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 19:45 [PATCH docs/manager/network/proxmox{,-ve-rs,-firewall} 00/14] sdn: conntrack zones and SNAT for simple zones with a dedicated VRF Stefan Hanreich
2026-11-19 19:45 ` [PATCH proxmox 01/14] network-api: expose the master device of a link Stefan Hanreich
2026-11-19 19:45 ` [PATCH proxmox-ve-rs 02/14] sdn: config: read the conntrack zone of zones with a dedicated VRF Stefan Hanreich
2026-11-19 19:45 ` Stefan Hanreich [this message]
2026-11-19 19:45 ` [PATCH pve-network 04/14] sdn: simple: SNAT via the uplinks of zone VRFs Stefan Hanreich
2026-11-19 19:45 ` [PATCH pve-network 05/14] sdn: zones: assign a conntrack zone to zones with a dedicated VRF Stefan Hanreich
2026-11-19 19:45 ` [PATCH proxmox-firewall 06/14] firewall: apply vnet rules to host traffic from vnets in a VRF Stefan Hanreich
2026-11-19 19:45 ` [PATCH proxmox-firewall 07/14] nftables: expression: add a constructor for map lookups Stefan Hanreich
2026-11-19 19:45 ` [PATCH proxmox-firewall 08/14] nftables: statement: add a helper for setting the conntrack zone Stefan Hanreich
2026-11-19 19:45 ` [PATCH proxmox-firewall 09/14] nftables: expression: convert the conntrack zones of zone VRFs Stefan Hanreich
2026-11-19 19:45 ` [PATCH proxmox-firewall 10/14] firewall: add a command applying " Stefan Hanreich
2026-11-19 19:45 ` [PATCH proxmox-firewall 11/14] firewall: put guest traffic of zone VRFs into their conntrack zone Stefan Hanreich
2026-11-19 19:45 ` [PATCH pve-manager 12/14] network: apply the conntrack zones of zone VRFs Stefan Hanreich
2026-11-19 19:45 ` [PATCH pve-manager 13/14] network: warn if pve-firewall cannot isolate guests in " Stefan Hanreich
2026-11-19 19:45 ` [PATCH pve-docs 14/14] pvesdn: simple zones: document dedicated VRFs 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=20261119194547.674512-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 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