From: Lukas Sichert <l.sichert@proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: Lukas Sichert <l.sichert@proxmox.com>
Subject: [PATCH network v3 6/7] fix #5066: zones: add test for generating and applying SNAT rules
Date: Thu, 26 Nov 2026 16:29:04 +0100 [thread overview]
Message-ID: <20261126153925.54600-7-l.sichert@proxmox.com> (raw)
In-Reply-To: <20261126153925.54600-1-l.sichert@proxmox.com>
The zone tests only compare the generated interfaces file, so nothing
checks how the post-up hooks and the activation of the prepared rules
work together.
Run both against a fake iptables and check the rules after the first
apply, after applying again, and after SNAT was disabled for all
subnets. This includes zones with a dedicated VRF, which masquerade on
each of their uplinks.
Signed-off-by: Lukas Sichert <l.sichert@proxmox.com>
Link: https://bugzilla.proxmox.com/show_bug.cgi?id=5066
---
Notes:
new in v3
src/test/Makefile | 5 +-
src/test/run_test_apply_snat.pl | 209 ++++++++++++++++++++++++++++++++
2 files changed, 213 insertions(+), 1 deletion(-)
create mode 100755 src/test/run_test_apply_snat.pl
diff --git a/src/test/Makefile b/src/test/Makefile
index d9160f3..adb8866 100644
--- a/src/test/Makefile
+++ b/src/test/Makefile
@@ -1,6 +1,6 @@
all: test
-test: test_zones test_zone_host_vrf test_static_routes test_ct_zones test_fabric_host_vrf test_fabric_zone_wireguard test_ipams test_dns test_subnets test_vnets_blackbox test_running_config test_api_index
+test: test_zones test_zone_host_vrf test_static_routes test_ct_zones test_fabric_host_vrf test_fabric_zone_wireguard test_ipams test_dns test_subnets test_vnets_blackbox test_running_config test_api_index test_apply_snat
test_zones: run_test_zones.pl
./run_test_zones.pl
@@ -38,4 +38,7 @@ test_running_config: run_test_running_config.pl
test_api_index: run_test_api_index.pl
./run_test_api_index.pl
+test_apply_snat: run_test_apply_snat.pl
+ ./run_test_apply_snat.pl
+
clean:
diff --git a/src/test/run_test_apply_snat.pl b/src/test/run_test_apply_snat.pl
new file mode 100755
index 0000000..d69c4fd
--- /dev/null
+++ b/src/test/run_test_apply_snat.pl
@@ -0,0 +1,209 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use lib qw(..);
+
+use Net::IP;
+use Test::More;
+use Test::MockModule;
+use Test::Differences;
+use Text::ParseWords qw(shellwords);
+
+use PVE::Network::SDN;
+use PVE::Network::SDN::Zones;
+use PVE::INotify;
+
+sub read_sdn_config {
+ my ($file) = @_;
+
+ # Read structure back in again
+ open my $in, '<', $file or die $!;
+ my $sdn_config;
+ {
+ local $/; # slurp mode
+ $sdn_config = eval <$in>;
+ }
+ close $in;
+
+ return $sdn_config;
+}
+
+# state of the mocked iptables: $tables->{$bin}->{$table}->{$chain} = [rules]
+my $tables;
+
+sub mock_run_command {
+ my ($cmd) = @_;
+
+ # hooks run through a shell, which handles the redirection
+ my @args = grep { $_ ne '2>/dev/null' } @$cmd;
+
+ my ($bin, undef, $table, $op, $chain, @rule) = @args;
+ my $chains = $tables->{$bin}->{$table};
+ my $rules = $chains->{$chain};
+ my $rule = "@rule";
+
+ die "no chain/target/match by that name\n" if !$rules && $op ne '-N';
+
+ if ($op eq '-N') {
+ die "chain already exists\n" if $rules;
+ $chains->{$chain} = [];
+ } elsif ($op eq '-A') {
+ push @$rules, $rule;
+ } elsif ($op eq '-I') {
+ unshift @$rules, $rule;
+ } elsif ($op eq '-C' || $op eq '-D') {
+ my ($idx) = grep { $rules->[$_] eq $rule } 0 .. $#$rules;
+ die "no matching rule\n" if !defined($idx);
+ splice(@$rules, $idx, 1) if $op eq '-D';
+ } elsif ($op eq '-F') {
+ @$rules = ();
+ } elsif ($op eq '-X') {
+ die "chain is not empty or still in use\n"
+ if @$rules || grep { $_ eq "-j $chain" } map { @$_ } values %$chains;
+ delete $chains->{$chain};
+ } elsif ($op eq '-E') {
+ $chains->{$rule} = delete $chains->{$chain};
+ for my $list (values %$chains) {
+ s/^-j \Q$chain\E$/-j $rule/ for @$list;
+ }
+ } elsif ($op ne '-L' && $op ne '-S') {
+ die "unsupported operation $op\n";
+ }
+}
+
+# runs the iptables post-up hooks like ifupdown2 does, 'a || b' only runs b if a failed
+sub split_post_up_hooks {
+ my ($interfaces) = @_;
+
+ for my $line (split(/\n/, $interfaces)) {
+ next if $line !~ m/^\s*post-up (ip6?tables .*)/;
+
+ my ($cmd_a, $cmd_b) = split(/ \|\| /, $1, 2);
+ eval { mock_run_command([shellwords($cmd_a)]) };
+ mock_run_command([shellwords($cmd_b)]) if $@ && defined($cmd_b);
+ die $@ if $@ && !defined($cmd_b);
+ }
+}
+
+sub get_iptables {
+ my %state;
+
+ foreach my $bin (qw(iptables ip6tables)) {
+ my $nat = $tables->{$bin}->{nat};
+ $state{$bin} = {
+ chains => [sort grep { /^PROXMOX/ } keys %$nat],
+ jumps => $nat->{POSTROUTING},
+ snat => [sort map { s/^-s (\S+) .*/$1/r } @{ $nat->{'PROXMOX-SDN'} // [] }],
+ ct_rules => scalar(@{ $tables->{$bin}->{raw}->{PREROUTING} }),
+ };
+ }
+
+ return \%state;
+}
+
+my $mocked_zones;
+$mocked_zones = Test::MockModule->new('PVE::Network::SDN::Zones');
+$mocked_zones->mock(run_command => \&mock_run_command);
+
+my @tests = sort glob('./zones/*/*snat');
+
+foreach my $name (@tests) {
+
+ my $sdn_config = read_sdn_config("./$name/sdn_config");
+
+ open(my $fh1, '<', "./$name/interfaces") or die "can't read interfaces file - $!";
+ my $interfaces_config = PVE::INotify::__read_etc_network_interfaces($fh1, undef, undef);
+ close $fh1;
+
+ my $pve_common_inotify;
+ $pve_common_inotify = Test::MockModule->new('PVE::INotify');
+ $pve_common_inotify->mock(
+ nodename => sub {
+ return 'localhost';
+ },
+ read_file => sub {
+ # HACK this assumes we are always calling PVE::INotify::read_file('interfaces');
+ return $interfaces_config;
+ },
+ );
+
+ my $pve_sdn_zones_plugin;
+ $pve_sdn_zones_plugin = Test::MockModule->new('PVE::Network::SDN::Zones::Plugin');
+ $pve_sdn_zones_plugin->mock(
+ get_local_route_ip => sub {
+ my ($targetip) = @_;
+ my $outiface = "vmbr0";
+ my $outip = $interfaces_config->{ifaces}->{$outiface}->{address};
+ if (Net::IP::ip_is_ipv6($targetip)) {
+ $outip = $interfaces_config->{ifaces}->{$outiface}->{address6};
+ }
+ return ($outip, $outiface);
+ },
+ );
+
+ my $sdn_module = Test::MockModule->new("PVE::Network::SDN");
+ $sdn_module->mock(
+ running_config => sub {
+ return $sdn_config;
+ },
+ );
+
+ foreach my $bin (qw(iptables ip6tables)) {
+ $tables->{$bin} = { nat => { POSTROUTING => [] }, raw => { PREROUTING => [] } };
+ }
+
+ my $bring_up_interfaces = sub {
+ split_post_up_hooks(PVE::Network::SDN::generate_raw_etc_network_config($sdn_config));
+ };
+
+ my $apply_sdn = sub {
+ $bring_up_interfaces->();
+ PVE::Network::SDN::Zones::apply_new_snat_rules();
+
+ return get_iptables();
+ };
+
+ # every SNAT subnet must show up in the chain of the matching binary (iptables/ip6tables)
+ my $expected = { iptables => [], ip6tables => [] };
+ for my $id (sort keys $sdn_config->{subnets}->{ids}->%*) {
+ next if !$sdn_config->{subnets}->{ids}->{$id}->{snat};
+
+ my ($zone, $network, $mask) = split(/-/, $id);
+ my $bin = Net::IP::ip_is_ipv6($network) ? 'ip6tables' : 'iptables';
+
+ # zones with a dedicated VRF masquerade on each uplink, others use a single SNAT rule
+ my $rules = 1;
+ if ($sdn_config->{zones}->{ids}->{$zone}->{'dedicated-vrf'}) {
+ my $ifaces = $interfaces_config->{ifaces};
+ $rules = grep { ($ifaces->{$_}->{vrf} // '') eq "vrf_$zone" } keys $ifaces->%*;
+ }
+
+ push $expected->{$bin}->@*, ("$network/$mask") x $rules;
+ }
+
+ my $active = {};
+ foreach my $bin (qw(iptables ip6tables)) {
+ my @snat = sort $expected->{$bin}->@*;
+ $active->{$bin} = {
+ chains => @snat ? ['PROXMOX-SDN'] : [],
+ jumps => @snat ? ['-j PROXMOX-SDN'] : [],
+ snat => \@snat,
+ ct_rules => @snat ? 1 : 0,
+ };
+ }
+
+ eq_or_diff($apply_sdn->(), $active, "$name: SNAT rules are active after the first apply");
+ eq_or_diff($apply_sdn->(), $active, "$name: applying again does not duplicate any rule");
+
+ # check that no new chain is prepared if no subnet with SNAT exists anymore
+ delete $_->{snat} for values $sdn_config->{subnets}->{ids}->%*;
+ my $disabled = {};
+ foreach my $bin (qw(iptables ip6tables)) {
+ $disabled->{$bin} = { %{ $active->{$bin} }, chains => [], jumps => [], snat => [] };
+ }
+ eq_or_diff($apply_sdn->(), $disabled, "$name: SNAT chain is removed if SNAT is disabled");
+}
+
+done_testing();
--
2.47.3
next prev 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 ` [PATCH network v3 5/7] fix #5066: simple zone: " Lukas Sichert
2026-11-26 15:29 ` Lukas Sichert [this message]
2026-11-26 15:29 ` [PATCH manager v3 7/7] fix #5066: network: activate prepared SDN SNAT rules 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-7-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.