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 6600C1FF0DA for ; Thu, 19 Nov 2026 20:47:01 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 3422B2187D; Thu, 19 Nov 2026 20:46:23 +0100 (CET) From: Stefan Hanreich To: pve-devel@lists.proxmox.com Subject: [PATCH pve-network 04/14] sdn: simple: SNAT via the uplinks of zone VRFs Date: Thu, 19 Nov 2026 20:45:36 +0100 Message-ID: <20261119194547.674512-5-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: 1795117557009 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.233 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: FVYF6A7JRHBISPUBSIYN2VVK7C7OBTM4 X-Message-ID-Hash: FVYF6A7JRHBISPUBSIYN2VVK7C7OBTM4 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: SNAT for subnets in a zone with a dedicated VRF resolved the outgoing interface and source address in the default routing table, so the generated rule never matched traffic leaving through the VRF. Add a MASQUERADE rule for every interface that the host network configuration puts into the zone VRF. Routing picks the uplink per connection, so this also works with several uplinks and ECMP, and MASQUERADE needs no address when generating the configuration, which covers uplinks configured via DHCP or SLAAC as well. Interfaces that only become VRF members otherwise, such as fabric interfaces, are not used for SNAT. Without an uplink in the VRF, warn and skip SNAT for the subnet. Zones without a dedicated VRF keep using SNAT via the default routing table. Originally-by: Gabriel Goller Signed-off-by: Stefan Hanreich --- src/PVE/Network/SDN/Zones/Plugin.pm | 9 +++ src/PVE/Network/SDN/Zones/SimplePlugin.pm | 20 ++++- .../simple/vrf_snat/expected_sdn_interfaces | 81 +++++++++++++++++++ src/test/zones/simple/vrf_snat/interfaces | 29 +++++++ src/test/zones/simple/vrf_snat/sdn_config | 53 ++++++++++++ 5 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 src/test/zones/simple/vrf_snat/expected_sdn_interfaces create mode 100644 src/test/zones/simple/vrf_snat/interfaces create mode 100644 src/test/zones/simple/vrf_snat/sdn_config diff --git a/src/PVE/Network/SDN/Zones/Plugin.pm b/src/PVE/Network/SDN/Zones/Plugin.pm index 2c561ea..288edfc 100644 --- a/src/PVE/Network/SDN/Zones/Plugin.pm +++ b/src/PVE/Network/SDN/Zones/Plugin.pm @@ -398,6 +398,15 @@ sub get_local_route_ip { return ($ip, $interface); } +# Returns the interfaces of the host network configuration that are members of $vrf, sorted by +# name. +sub get_vrf_member_interfaces { + my ($vrf, $interfaces_config) = @_; + + my $ifaces = $interfaces_config->{ifaces} // {}; + return sort grep { ($ifaces->{$_}->{vrf} // '') eq $vrf } keys %$ifaces; +} + sub find_local_ip_interface_peers { my ($peers, $iface) = @_; diff --git a/src/PVE/Network/SDN/Zones/SimplePlugin.pm b/src/PVE/Network/SDN/Zones/SimplePlugin.pm index 1d44f0b..4221521 100644 --- a/src/PVE/Network/SDN/Zones/SimplePlugin.pm +++ b/src/PVE/Network/SDN/Zones/SimplePlugin.pm @@ -139,7 +139,25 @@ sub generate_sdn_config { my $vrf = $plugin_config->{'dedicated-vrf'} ? " vrf vrf_$zone_id" : ''; push @iface_config, "up ip route add $cidr dev $vnetid$vrf"; } - if ($subnet->{snat}) { + if ($subnet->{snat} && $plugin_config->{'dedicated-vrf'}) { + my @uplinks = PVE::Network::SDN::Zones::Plugin::get_vrf_member_interfaces( + "vrf_$zone_id", $interfaces_config, + ); + if (!@uplinks) { + 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"; + } elsif ($subnet->{snat}) { #find outgoing interface my ($outip, $outiface); eval { diff --git a/src/test/zones/simple/vrf_snat/expected_sdn_interfaces b/src/test/zones/simple/vrf_snat/expected_sdn_interfaces new file mode 100644 index 0000000..2a9804c --- /dev/null +++ b/src/test/zones/simple/vrf_snat/expected_sdn_interfaces @@ -0,0 +1,81 @@ +#version:1 + +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 + 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 + bridge_ports none + bridge_stp off + bridge_fd 0 + ip-forward on + ip6-forward on + vrf vrf_zonea + +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 + bridge_ports none + bridge_stp off + bridge_fd 0 + ip-forward on + vrf vrf_zoneb + +auto vnetc +iface vnetc + address 10.0.0.1/24 + bridge_ports none + bridge_stp off + bridge_fd 0 + ip-forward on + vrf vrf_zonec + +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 + bridge_ports none + bridge_stp off + bridge_fd 0 + ip-forward on + vrf vrf_zoned + +auto vrf_zonea +iface vrf_zonea + vrf-table auto + post-up ip route add vrf vrf_zonea unreachable default metric 4278198272 + post-up ip -6 route add vrf vrf_zonea unreachable default metric 4278198272 || true + +auto vrf_zoneb +iface vrf_zoneb + vrf-table auto + post-up ip route add vrf vrf_zoneb unreachable default metric 4278198272 + post-up ip -6 route add vrf vrf_zoneb unreachable default metric 4278198272 || true + +auto vrf_zonec +iface vrf_zonec + vrf-table auto + post-up ip route add vrf vrf_zonec unreachable default metric 4278198272 + post-up ip -6 route add vrf vrf_zonec unreachable default metric 4278198272 || true + +auto vrf_zoned +iface vrf_zoned + vrf-table auto + post-up ip route add vrf vrf_zoned unreachable default metric 4278198272 + post-up ip -6 route add vrf vrf_zoned unreachable default metric 4278198272 || true diff --git a/src/test/zones/simple/vrf_snat/interfaces b/src/test/zones/simple/vrf_snat/interfaces new file mode 100644 index 0000000..67ad22f --- /dev/null +++ b/src/test/zones/simple/vrf_snat/interfaces @@ -0,0 +1,29 @@ +auto vmbr0 +iface vmbr0 inet static + address 192.168.0.1/24 + gateway 192.168.0.254 + bridge-ports eth0 + bridge-stp off + bridge-fd 0 + +auto eth1 +iface eth1 inet static + address 203.0.113.10/24 + vrf vrf_zonea + +iface eth1 inet6 auto + vrf vrf_zonea + +auto eth2 +iface eth2 inet dhcp + vrf vrf_zoneb + +auto eth3 +iface eth3 inet static + address 198.51.100.10/29 + vrf vrf_zoned + +auto eth4 +iface eth4 inet static + address 198.51.100.18/29 + vrf vrf_zoned diff --git a/src/test/zones/simple/vrf_snat/sdn_config b/src/test/zones/simple/vrf_snat/sdn_config new file mode 100644 index 0000000..fa74493 --- /dev/null +++ b/src/test/zones/simple/vrf_snat/sdn_config @@ -0,0 +1,53 @@ +{ + version => 1, + vnets => { + ids => { + vneta => { type => "vnet", zone => "zonea" }, + vnetb => { type => "vnet", zone => "zoneb" }, + vnetc => { type => "vnet", zone => "zonec" }, + vnetd => { type => "vnet", zone => "zoned" }, + }, + }, + zones => { + ids => { + zonea => { ipam => "pve", type => "simple", "dedicated-vrf" => 1 }, + zoneb => { ipam => "pve", type => "simple", "dedicated-vrf" => 1 }, + zonec => { ipam => "pve", type => "simple", "dedicated-vrf" => 1 }, + zoned => { ipam => "pve", type => "simple", "dedicated-vrf" => 1 }, + }, + }, + subnets => { + ids => { + 'zonea-10.0.0.0-24' => { + type => 'subnet', + vnet => 'vneta', + gateway => '10.0.0.1', + snat => 1, + }, + 'zonea-2001:db8:a::-64' => { + type => 'subnet', + vnet => 'vneta', + gateway => '2001:db8:a::1', + snat => 1, + }, + 'zoneb-10.0.0.0-24' => { + type => 'subnet', + vnet => 'vnetb', + gateway => '10.0.0.1', + snat => 1, + }, + 'zonec-10.0.0.0-24' => { + type => 'subnet', + vnet => 'vnetc', + gateway => '10.0.0.1', + snat => 1, + }, + 'zoned-10.0.0.0-24' => { + type => 'subnet', + vnet => 'vnetd', + gateway => '10.0.0.1', + snat => 1, + }, + }, + }, +} -- 2.47.3