public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Sichert <l.sichert@proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: Lukas Sichert <l.sichert@proxmox.com>
Subject: [PATCH network v3 5/7] fix #5066: simple zone: prepare SNAT rules in temporary chain
Date: Thu, 26 Nov 2026 16:29:03 +0100	[thread overview]
Message-ID: <20261126153925.54600-6-l.sichert@proxmox.com> (raw)
In-Reply-To: <20261126153925.54600-1-l.sichert@proxmox.com>

Disabling SNAT for an existing subnet can leave its generated iptables
rule active after applying the SDN configuration.

Use the common SNAT rule generator for Simple Zones, including zones
with a dedicated VRF. Instead of adding rules directly to POSTROUTING,
generate them in the temporary PROXMOX-SDN-NEW chain. This allows the
complete generated ruleset to be activated together and the previous
ruleset to be removed separately.

Update the expected network configuration accordingly.

Signed-off-by: Lukas Sichert <l.sichert@proxmox.com>
Link: https://bugzilla.proxmox.com/show_bug.cgi?id=5066
---

Notes:
    changes v2 -> v3:
    - rebased on top of master
    - use the common SNAT rule generator and prepare the rules in a
      temporary chain, instead of flushing the chain on reload
    - also handle zones with a dedicated VRF
    
    changes v1 -> v2:
    - rebased on top of master

 src/PVE/Network/SDN/Zones/SimplePlugin.pm     | 44 +++++--------------
 .../simple/ipv4snat/expected_sdn_interfaces   |  8 ++--
 .../simple/ipv6snat/expected_sdn_interfaces   |  8 ++--
 .../simple/vrf_snat/expected_sdn_interfaces   | 35 +++++++--------
 4 files changed, 36 insertions(+), 59 deletions(-)

diff --git a/src/PVE/Network/SDN/Zones/SimplePlugin.pm b/src/PVE/Network/SDN/Zones/SimplePlugin.pm
index 3e866ac..016a186 100644
--- a/src/PVE/Network/SDN/Zones/SimplePlugin.pm
+++ b/src/PVE/Network/SDN/Zones/SimplePlugin.pm
@@ -154,40 +154,18 @@ sub generate_sdn_config {
                 log_warn("no uplink in vrf_$zone_id, not adding SNAT for subnet $cidr");
                 next;
             }
-            # the routing table picks the uplink per connection (e.g. with ECMP), and its
-            # address may be dynamic, so let each uplink masquerade with its own address
-            for my $uplink (@uplinks) {
-                push @iface_config,
-                    "post-up $iptables -t nat -A POSTROUTING -s '$cidr' -o $uplink -j MASQUERADE";
-                push @iface_config,
-                    "post-down $iptables -t nat -D POSTROUTING -s '$cidr' -o $uplink -j MASQUERADE";
-            }
-            push @iface_config, "post-up $iptables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1";
-            push @iface_config, "post-down $iptables -t raw -D PREROUTING -i fwbr+ -j CT --zone 1";
+
+            my @snat_config = PVE::Network::SDN::Zones::Plugin::generate_new_snat_rules(
+                $checkrouteip, $iptables, $cidr, \@uplinks,
+            );
+
+            push @iface_config, @snat_config;
         } elsif ($subnet->{snat}) {
-            #find outgoing interface
-            my ($outip, $outiface);
-            eval {
-                ($outip, $outiface) =
-                    PVE::Network::SDN::Zones::Plugin::get_local_route_ip($checkrouteip);
-            };
-            if ($@) {
-                my $msg = "interface for SNAT could not be resolved: $@";
-                log_warn($msg);
-                next;
-            }
-            if ($outip && $outiface) {
-                #use snat, faster than masquerade
-                push @iface_config,
-                    "post-up $iptables -t nat -A POSTROUTING -s '$cidr' -o $outiface -j SNAT --to-source $outip";
-                push @iface_config,
-                    "post-down $iptables -t nat -D POSTROUTING -s '$cidr' -o $outiface -j SNAT --to-source $outip";
-                #add conntrack zone once on outgoing interface
-                push @iface_config,
-                    "post-up $iptables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1";
-                push @iface_config,
-                    "post-down $iptables -t raw -D PREROUTING -i fwbr+ -j CT --zone 1";
-            }
+            my @snat_config = PVE::Network::SDN::Zones::Plugin::generate_new_snat_rules(
+                $checkrouteip, $iptables, $cidr,
+            );
+
+            push @iface_config, @snat_config;
         }
     }
 
diff --git a/src/test/zones/simple/ipv4snat/expected_sdn_interfaces b/src/test/zones/simple/ipv4snat/expected_sdn_interfaces
index 69d7986..03de88d 100644
--- a/src/test/zones/simple/ipv4snat/expected_sdn_interfaces
+++ b/src/test/zones/simple/ipv4snat/expected_sdn_interfaces
@@ -3,10 +3,10 @@
 auto myvnet
 iface myvnet
 	address 10.0.0.1/24
-	post-up iptables -t nat -A POSTROUTING -s '10.0.0.0/24' -o vmbr0 -j SNAT --to-source 192.168.0.1
-	post-down iptables -t nat -D POSTROUTING -s '10.0.0.0/24' -o vmbr0 -j SNAT --to-source 192.168.0.1
-	post-up iptables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1
-	post-down iptables -t raw -D PREROUTING -i fwbr+ -j CT --zone 1
+	post-up iptables -t nat -L PROXMOX-SDN-NEW 2>/dev/null || iptables -t nat -N PROXMOX-SDN-NEW
+	post-up iptables -t nat -C POSTROUTING -j PROXMOX-SDN-NEW 2>/dev/null || iptables -t nat -A POSTROUTING -j PROXMOX-SDN-NEW
+	post-up iptables -t nat -A PROXMOX-SDN-NEW -s '10.0.0.0/24' -o vmbr0 -j SNAT --to-source 192.168.0.1
+	post-up iptables -t raw -C PREROUTING -i fwbr+ -j CT --zone 1 2>/dev/null || iptables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1
 	bridge_ports none
 	bridge_stp off
 	bridge_fd 0
diff --git a/src/test/zones/simple/ipv6snat/expected_sdn_interfaces b/src/test/zones/simple/ipv6snat/expected_sdn_interfaces
index 5f6d40b..d3e1fdf 100644
--- a/src/test/zones/simple/ipv6snat/expected_sdn_interfaces
+++ b/src/test/zones/simple/ipv6snat/expected_sdn_interfaces
@@ -3,10 +3,10 @@
 auto myvnet
 iface myvnet
 	address 2a08:2142:302:3::1/64
-	post-up ip6tables -t nat -A POSTROUTING -s '2a08:2142:302:3::/64' -o vmbr0 -j SNAT --to-source 2001:db8::2
-	post-down ip6tables -t nat -D POSTROUTING -s '2a08:2142:302:3::/64' -o vmbr0 -j SNAT --to-source 2001:db8::2
-	post-up ip6tables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1
-	post-down ip6tables -t raw -D PREROUTING -i fwbr+ -j CT --zone 1
+	post-up ip6tables -t nat -L PROXMOX-SDN-NEW 2>/dev/null || ip6tables -t nat -N PROXMOX-SDN-NEW
+	post-up ip6tables -t nat -C POSTROUTING -j PROXMOX-SDN-NEW 2>/dev/null || ip6tables -t nat -A POSTROUTING -j PROXMOX-SDN-NEW
+	post-up ip6tables -t nat -A PROXMOX-SDN-NEW -s '2a08:2142:302:3::/64' -o vmbr0 -j SNAT --to-source 2001:db8::2
+	post-up ip6tables -t raw -C PREROUTING -i fwbr+ -j CT --zone 1 2>/dev/null || ip6tables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1
 	bridge_ports none
 	bridge_stp off
 	bridge_fd 0
diff --git a/src/test/zones/simple/vrf_snat/expected_sdn_interfaces b/src/test/zones/simple/vrf_snat/expected_sdn_interfaces
index 2a9804c..2eab64a 100644
--- a/src/test/zones/simple/vrf_snat/expected_sdn_interfaces
+++ b/src/test/zones/simple/vrf_snat/expected_sdn_interfaces
@@ -3,15 +3,15 @@
 auto vneta
 iface vneta
 	address 10.0.0.1/24
-	post-up iptables -t nat -A POSTROUTING -s '10.0.0.0/24' -o eth1 -j MASQUERADE
-	post-down iptables -t nat -D POSTROUTING -s '10.0.0.0/24' -o eth1 -j MASQUERADE
-	post-up iptables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1
-	post-down iptables -t raw -D PREROUTING -i fwbr+ -j CT --zone 1
+	post-up iptables -t nat -L PROXMOX-SDN-NEW 2>/dev/null || iptables -t nat -N PROXMOX-SDN-NEW
+	post-up iptables -t nat -C POSTROUTING -j PROXMOX-SDN-NEW 2>/dev/null || iptables -t nat -A POSTROUTING -j PROXMOX-SDN-NEW
+	post-up iptables -t nat -A PROXMOX-SDN-NEW -s '10.0.0.0/24' -o eth1 -j MASQUERADE
+	post-up iptables -t raw -C PREROUTING -i fwbr+ -j CT --zone 1 2>/dev/null || iptables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1
 	address 2001:db8:a::1/64
-	post-up ip6tables -t nat -A POSTROUTING -s '2001:db8:a::/64' -o eth1 -j MASQUERADE
-	post-down ip6tables -t nat -D POSTROUTING -s '2001:db8:a::/64' -o eth1 -j MASQUERADE
-	post-up ip6tables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1
-	post-down ip6tables -t raw -D PREROUTING -i fwbr+ -j CT --zone 1
+	post-up ip6tables -t nat -L PROXMOX-SDN-NEW 2>/dev/null || ip6tables -t nat -N PROXMOX-SDN-NEW
+	post-up ip6tables -t nat -C POSTROUTING -j PROXMOX-SDN-NEW 2>/dev/null || ip6tables -t nat -A POSTROUTING -j PROXMOX-SDN-NEW
+	post-up ip6tables -t nat -A PROXMOX-SDN-NEW -s '2001:db8:a::/64' -o eth1 -j MASQUERADE
+	post-up ip6tables -t raw -C PREROUTING -i fwbr+ -j CT --zone 1 2>/dev/null || ip6tables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1
 	bridge_ports none
 	bridge_stp off
 	bridge_fd 0
@@ -22,10 +22,10 @@ iface vneta
 auto vnetb
 iface vnetb
 	address 10.0.0.1/24
-	post-up iptables -t nat -A POSTROUTING -s '10.0.0.0/24' -o eth2 -j MASQUERADE
-	post-down iptables -t nat -D POSTROUTING -s '10.0.0.0/24' -o eth2 -j MASQUERADE
-	post-up iptables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1
-	post-down iptables -t raw -D PREROUTING -i fwbr+ -j CT --zone 1
+	post-up iptables -t nat -L PROXMOX-SDN-NEW 2>/dev/null || iptables -t nat -N PROXMOX-SDN-NEW
+	post-up iptables -t nat -C POSTROUTING -j PROXMOX-SDN-NEW 2>/dev/null || iptables -t nat -A POSTROUTING -j PROXMOX-SDN-NEW
+	post-up iptables -t nat -A PROXMOX-SDN-NEW -s '10.0.0.0/24' -o eth2 -j MASQUERADE
+	post-up iptables -t raw -C PREROUTING -i fwbr+ -j CT --zone 1 2>/dev/null || iptables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1
 	bridge_ports none
 	bridge_stp off
 	bridge_fd 0
@@ -44,12 +44,11 @@ iface vnetc
 auto vnetd
 iface vnetd
 	address 10.0.0.1/24
-	post-up iptables -t nat -A POSTROUTING -s '10.0.0.0/24' -o eth3 -j MASQUERADE
-	post-down iptables -t nat -D POSTROUTING -s '10.0.0.0/24' -o eth3 -j MASQUERADE
-	post-up iptables -t nat -A POSTROUTING -s '10.0.0.0/24' -o eth4 -j MASQUERADE
-	post-down iptables -t nat -D POSTROUTING -s '10.0.0.0/24' -o eth4 -j MASQUERADE
-	post-up iptables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1
-	post-down iptables -t raw -D PREROUTING -i fwbr+ -j CT --zone 1
+	post-up iptables -t nat -L PROXMOX-SDN-NEW 2>/dev/null || iptables -t nat -N PROXMOX-SDN-NEW
+	post-up iptables -t nat -C POSTROUTING -j PROXMOX-SDN-NEW 2>/dev/null || iptables -t nat -A POSTROUTING -j PROXMOX-SDN-NEW
+	post-up iptables -t nat -A PROXMOX-SDN-NEW -s '10.0.0.0/24' -o eth3 -j MASQUERADE
+	post-up iptables -t nat -A PROXMOX-SDN-NEW -s '10.0.0.0/24' -o eth4 -j MASQUERADE
+	post-up iptables -t raw -C PREROUTING -i fwbr+ -j CT --zone 1 2>/dev/null || iptables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1
 	bridge_ports none
 	bridge_stp off
 	bridge_fd 0
-- 
2.47.3





  parent reply	other threads:[~2026-11-26 15:40 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 15:28 [PATCH manager/network v3 0/7] fix #5066: make generated snat rules flushable Lukas Sichert
2026-11-26 15:28 ` [PATCH network v3 1/7] style: zones: use fully qualified JSONSchema calls for standard options Lukas Sichert
2026-11-26 15:29 ` [PATCH network v3 2/7] fix #5066: zones: factor out SNAT rule generation Lukas Sichert
2026-11-26 15:29 ` [PATCH network v3 3/7] fix #5066: zones: add helper to activate prepared SNAT rules Lukas Sichert
2026-11-26 15:29 ` [PATCH network v3 4/7] fix #5066: evpn: prepare SNAT rules in temporary chain Lukas Sichert
2026-11-26 15:29 ` Lukas Sichert [this message]
2026-11-26 15:29 ` [PATCH network v3 6/7] fix #5066: zones: add test for generating and applying SNAT rules Lukas Sichert
2026-11-26 15:29 ` [PATCH manager v3 7/7] fix #5066: network: activate prepared SDN " Lukas Sichert

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=20261126153925.54600-6-l.sichert@proxmox.com \
    --to=l.sichert@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