From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id EF7C11FF0DA for ; Thu, 19 Nov 2026 20:47:32 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id EB40A21905; Thu, 19 Nov 2026 20:46:29 +0100 (CET) From: Stefan Hanreich To: pve-devel@lists.proxmox.com Subject: [PATCH pve-network 05/14] sdn: zones: assign a conntrack zone to zones with a dedicated VRF Date: Thu, 19 Nov 2026 20:45:37 +0100 Message-ID: <20261119194547.674512-6-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: 1795117557077 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.227 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: O7IULMT6KNRHVZJTPO3VMU6NN7XYDASX X-Message-ID-Hash: O7IULMT6KNRHVZJTPO3VMU6NN7XYDASX 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: The subnets of zones with a dedicated VRF may overlap, so connections in different VRFs must not share a conntrack entry, or the firewall would treat a new connection in one VRF as part of an established one in another VRF, and conntrack state migration would mix up the entries. Assign each such zone a conntrack zone from the first 1024 zones (16384 to 17407) of the block 16384 to 32767 that Proxmox VE reserves, stored with the zone as ct-zone, so that it is the same on all nodes and stays the same as long as the zone has a dedicated VRF. It is removed when the dedicated VRF is disabled, and cannot be set via the API. A new conntrack zone follows the highest one in use, including the running config, so the conntrack zone of a zone that is still active or was just deleted is not reused right away, which could match stale conntrack entries of that zone. Signed-off-by: Stefan Hanreich --- src/PVE/API2/Network/SDN/Zones.pm | 13 +++ src/PVE/Network/SDN/Zones.pm | 42 +++++++++ src/PVE/Network/SDN/Zones/Plugin.pm | 4 + src/PVE/Network/SDN/Zones/SimplePlugin.pm | 7 ++ src/test/Makefile | 5 +- src/test/run_test_ct_zones.pl | 103 ++++++++++++++++++++++ 6 files changed, 173 insertions(+), 1 deletion(-) create mode 100755 src/test/run_test_ct_zones.pl diff --git a/src/PVE/API2/Network/SDN/Zones.pm b/src/PVE/API2/Network/SDN/Zones.pm index 5952f40..5ff7bb4 100644 --- a/src/PVE/API2/Network/SDN/Zones.pm +++ b/src/PVE/API2/Network/SDN/Zones.pm @@ -70,6 +70,11 @@ my $ZONE_PROPERTIES = { optional => 1, description => 'Create a dedicated VRF for a simple zone.', }, + 'ct-zone' => { + type => 'integer', + optional => 1, + description => 'Conntrack zone of the dedicated VRF, assigned automatically.', + }, route => { type => 'array', optional => 1, @@ -407,6 +412,9 @@ __PACKAGE__->register_method({ my $id = extract_param($param, 'zone'); my $lock_token = extract_param($param, 'lock-token'); + raise_param_exc({ 'ct-zone' => 'is assigned automatically' }) + if defined($param->{'ct-zone'}); + my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($type); my $opts = $plugin->check_config($id, $param, 1, 1); @@ -434,6 +442,7 @@ __PACKAGE__->register_method({ if $dnszone && !$dnsserver; $zone_cfg->{ids}->{$id} = $opts; + PVE::Network::SDN::Zones::update_ct_zone($zone_cfg, $id); $plugin->on_update_hook($id, $zone_cfg, $controller_cfg); PVE::Network::SDN::Zones::write_config($zone_cfg); @@ -478,6 +487,9 @@ __PACKAGE__->register_method({ $delete = [PVE::Tools::split_list($delete)]; } + raise_param_exc({ 'ct-zone' => 'is assigned automatically' }) + if defined($param->{'ct-zone'}) || grep { $_ eq 'ct-zone' } ($delete // [])->@*; + PVE::Network::SDN::lock_sdn_config( sub { my $zone_cfg = PVE::Network::SDN::Zones::config(); @@ -541,6 +553,7 @@ __PACKAGE__->register_method({ my $dnszone = $opts->{dnszone}; raise_param_exc({ dnszone => "missing dns server" }) if $dnszone && !$dnsserver; + PVE::Network::SDN::Zones::update_ct_zone($zone_cfg, $id); $plugin->on_update_hook($id, $zone_cfg, $controller_cfg); PVE::Network::SDN::Zones::write_config($zone_cfg); diff --git a/src/PVE/Network/SDN/Zones.pm b/src/PVE/Network/SDN/Zones.pm index 3b59f4f..c8ff20b 100644 --- a/src/PVE/Network/SDN/Zones.pm +++ b/src/PVE/Network/SDN/Zones.pm @@ -59,6 +59,48 @@ sub config { return cfs_read_file("sdn/zones.cfg"); } +# Assigns a conntrack zone to a simple zone with a dedicated VRF and removes it from other zones. +# +# A new conntrack zone follows the highest one in use, also in the running config, so that the +# conntrack zone of a zone that is still active or was just deleted is not reused right away. +sub update_ct_zone { + my ($zone_cfg, $id) = @_; + + my $zone = $zone_cfg->{ids}->{$id}; + + if ($zone->{type} ne 'simple' || !$zone->{'dedicated-vrf'}) { + delete $zone->{'ct-zone'}; + return; + } + + return if defined($zone->{'ct-zone'}); + + my $first = $PVE::Network::SDN::Zones::Plugin::VRF_CT_ZONE_FIRST; + my $last = $PVE::Network::SDN::Zones::Plugin::VRF_CT_ZONE_LAST; + + my $running_zones = PVE::Network::SDN::Zones::config(1)->{ids}; + my $used = {}; + for my $zones ($zone_cfg->{ids}, $running_zones) { + for my $ct_zone (map { $_->{'ct-zone'} } values $zones->%*) { + $used->{$ct_zone} = 1 if defined($ct_zone) && $ct_zone >= $first && $ct_zone <= $last; + } + } + + my $count = $last - $first + 1; + die "no conntrack zone left for the VRF of zone '$id'\n" if scalar(keys $used->%*) >= $count; + + my ($highest) = sort { $b <=> $a } keys $used->%*; + $highest //= $last; + + for my $offset (1 .. $count) { + my $ct_zone = $first + ($highest - $first + $offset) % $count; + if (!$used->{$ct_zone}) { + $zone->{'ct-zone'} = $ct_zone; + return; + } + } +} + sub get_plugin_config { my ($vnet) = @_; my $zoneid = $vnet->{zone}; diff --git a/src/PVE/Network/SDN/Zones/Plugin.pm b/src/PVE/Network/SDN/Zones/Plugin.pm index 288edfc..d92bfd1 100644 --- a/src/PVE/Network/SDN/Zones/Plugin.pm +++ b/src/PVE/Network/SDN/Zones/Plugin.pm @@ -31,6 +31,10 @@ PVE::JSONSchema::register_standard_option( }, ); +# conntrack zones reserved for the VRFs of zones, see also VrfCtZone in proxmox-ve-config +our $VRF_CT_ZONE_FIRST = 16384; +our $VRF_CT_ZONE_LAST = 17407; + my $static_route_fmt = { prefix => { type => 'string', diff --git a/src/PVE/Network/SDN/Zones/SimplePlugin.pm b/src/PVE/Network/SDN/Zones/SimplePlugin.pm index 4221521..3e866ac 100644 --- a/src/PVE/Network/SDN/Zones/SimplePlugin.pm +++ b/src/PVE/Network/SDN/Zones/SimplePlugin.pm @@ -40,6 +40,12 @@ sub properties { type => 'boolean', default => 0, }, + 'ct-zone' => { + description => 'Conntrack zone of the dedicated VRF, assigned automatically.', + type => 'integer', + minimum => $PVE::Network::SDN::Zones::Plugin::VRF_CT_ZONE_FIRST, + maximum => $PVE::Network::SDN::Zones::Plugin::VRF_CT_ZONE_LAST, + }, }; } @@ -53,6 +59,7 @@ sub options { ipam => { optional => 1 }, dhcp => { optional => 1 }, 'dedicated-vrf' => { optional => 1 }, + 'ct-zone' => { optional => 1 }, route => { optional => 1 }, }; } diff --git a/src/test/Makefile b/src/test/Makefile index 0d5f3c1..242901b 100644 --- a/src/test/Makefile +++ b/src/test/Makefile @@ -1,6 +1,6 @@ all: test -test: test_zones test_static_routes test_fabric_host_vrf test_ipams test_dns test_subnets test_vnets_blackbox test_running_config test_api_index +test: test_zones test_static_routes test_ct_zones test_fabric_host_vrf test_ipams test_dns test_subnets test_vnets_blackbox test_running_config test_api_index test_zones: run_test_zones.pl ./run_test_zones.pl @@ -11,6 +11,9 @@ test_static_routes: run_test_static_routes.pl test_fabric_host_vrf: run_test_fabric_host_vrf.pl ./run_test_fabric_host_vrf.pl +test_ct_zones: run_test_ct_zones.pl + ./run_test_ct_zones.pl + test_ipams: run_test_ipams.pl ./run_test_ipams.pl diff --git a/src/test/run_test_ct_zones.pl b/src/test/run_test_ct_zones.pl new file mode 100755 index 0000000..1b71522 --- /dev/null +++ b/src/test/run_test_ct_zones.pl @@ -0,0 +1,103 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use lib qw(..); + +use Test::More; +use Test::MockModule; + +use PVE::API2::Network::SDN::Zones; +use PVE::Network::SDN::Zones; + +my $running_zones = {}; + +my $mocked_zones = Test::MockModule->new('PVE::Network::SDN::Zones'); +$mocked_zones->mock( + config => sub { + my ($running) = @_; + die "unexpected read of zones.cfg\n" if !$running; + return { ids => $running_zones }; + }, +); + +sub vrf_zone { + my ($ct_zone) = @_; + my $zone = { type => 'simple', 'dedicated-vrf' => 1 }; + $zone->{'ct-zone'} = $ct_zone if defined($ct_zone); + return $zone; +} + +sub ct_zone_of { + my ($zones, $id) = @_; + my $zone_cfg = { ids => $zones }; + PVE::Network::SDN::Zones::update_ct_zone($zone_cfg, $id); + return $zone_cfg->{ids}->{$id}->{'ct-zone'}; +} + +is(ct_zone_of({ new => vrf_zone() }, 'new'), 16384, 'first zone VRF gets the first conntrack zone'); + +is( + ct_zone_of({ a => vrf_zone(16384), b => vrf_zone(16390), new => vrf_zone() }, 'new'), + 16391, + 'new conntrack zone follows the highest one in use', +); + +is(ct_zone_of({ a => vrf_zone(16500) }, 'a'), 16500, 'existing conntrack zone is kept'); + +is( + ct_zone_of({ a => { type => 'simple', 'ct-zone' => 16400 } }, 'a'), + undef, + 'conntrack zone is removed without a dedicated VRF', +); + +is( + ct_zone_of({ a => { type => 'evpn', 'dedicated-vrf' => 1, 'ct-zone' => 16400 } }, 'a'), + undef, + 'only simple zones get a conntrack zone', +); + +$running_zones = { old => vrf_zone(16395) }; +is( + ct_zone_of({ a => vrf_zone(16384), new => vrf_zone() }, 'new'), + 16396, + 'conntrack zone of a zone that is only in the running config is not reused', +); +$running_zones = {}; + +is( + ct_zone_of({ a => vrf_zone(17407), b => vrf_zone(16384), new => vrf_zone() }, 'new'), + 16385, + 'allocation wraps around after the last conntrack zone', +); + +is( + ct_zone_of({ a => { type => 'vlan', 'ct-zone' => 17000 }, new => vrf_zone() }, 'new'), + 17001, + 'conntrack zones of other zones are taken into account', +); + +is( + ct_zone_of({ a => vrf_zone(5), new => vrf_zone() }, 'new'), + 16384, + 'conntrack zones out of range are ignored', +); + +my $all = { map { ("z$_" => vrf_zone($_)) } 16384 .. 17407 }; +$all->{new} = vrf_zone(); +eval { ct_zone_of($all, 'new') }; +like($@, qr/no conntrack zone left/, 'allocation fails if all conntrack zones are in use'); + +for my $method ('create', 'update') { + my $param = { zone => 'zone0', 'ct-zone' => 16384 }; + $param->{type} = 'simple' if $method eq 'create'; + + eval { PVE::API2::Network::SDN::Zones->$method($param) }; + like($@, qr/ct-zone.*assigned automatically/s, "$method rejects setting ct-zone"); +} + +eval { PVE::API2::Network::SDN::Zones->update({ zone => 'zone0', delete => 'ct-zone' }) }; +like($@, qr/ct-zone.*assigned automatically/s, 'update rejects deleting ct-zone'); + +done_testing(); -- 2.47.3