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 03EDE1FF0BC for ; Mon, 16 Nov 2026 17:16:22 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 4C91921A57; Mon, 16 Nov 2026 17:12:57 +0100 (CET) From: Gabriel Goller To: pve-devel@lists.proxmox.com Subject: [PATCH pve-network v3 21/32] sdn: simple: support static routes in zone routing tables Date: Mon, 16 Nov 2026 17:11:41 +0100 Message-ID: <20261116161154.343743-22-g.goller@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261116161154.343743-1-g.goller@proxmox.com> References: <20261116161154.343743-1-g.goller@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794845521255 X-SPAM-LEVEL: Spam detection results: 0 AWL -2.268 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 URIBL_DBL_SPAM 5 Contains a spam URL listed in the Spamhaus DBL blocklist [sdn.pm] Message-ID-Hash: IPNK43FSANS2D4G6IZFZXFSWHGJDYLKR X-Message-ID-Hash: IPNK43FSANS2D4G6IZFZXFSWHGJDYLKR X-MailFrom: g.goller@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: Allow simple zones to reach outside networks through static routes, without needing a fabric. Routes use the zone's dedicated VRF, or the default routing table if dedicated-vrf is disabled. Support IPv4 and IPv6 routes, including default routes, with a gateway or an outgoing interface. nexthop-vrf allows a gateway in another VRF, e.g. the default VRF, without moving the uplink into the zone. Routes are managed by FRR, so zones with static routes need FRR even without a controller or fabric. This requires the matching pve-rs support for route validation and FRR config generation. Signed-off-by: Gabriel Goller --- src/PVE/API2/Network/SDN/Zones.pm | 9 ++ src/PVE/Network/SDN.pm | 22 ++- src/PVE/Network/SDN/Zones/Plugin.pm | 55 +++++++ src/PVE/Network/SDN/Zones/SimplePlugin.pm | 1 + src/test/Makefile | 5 +- src/test/run_test_static_routes.pl | 178 ++++++++++++++++++++++ 6 files changed, 266 insertions(+), 4 deletions(-) create mode 100644 src/test/run_test_static_routes.pl diff --git a/src/PVE/API2/Network/SDN/Zones.pm b/src/PVE/API2/Network/SDN/Zones.pm index e4f4b174466e..8df1bf8527fd 100644 --- a/src/PVE/API2/Network/SDN/Zones.pm +++ b/src/PVE/API2/Network/SDN/Zones.pm @@ -68,6 +68,15 @@ my $ZONE_PROPERTIES = { optional => 1, description => 'Create a dedicated VRF for a simple zone.', }, + route => { + type => 'array', + optional => 1, + description => 'Static routes in the zone routing table. Simple zone only.', + items => { + type => 'string', + format => 'pve-sdn-static-route', + }, + }, 'rt-import' => { type => 'string', optional => 1, diff --git a/src/PVE/Network/SDN.pm b/src/PVE/Network/SDN.pm index 9373ec3035fa..626fe7387f54 100644 --- a/src/PVE/Network/SDN.pm +++ b/src/PVE/Network/SDN.pm @@ -17,6 +17,7 @@ use PVE::RESTEnvironment qw(log_warn); use PVE::RPCEnvironment; use PVE::Tools qw(file_get_contents file_set_contents extract_param dir_glob_regex run_command); +use PVE::RS::SDN; use PVE::RS::SDN::Fabrics; use PVE::Network::SDN::Vnets; @@ -130,10 +131,19 @@ sub running_config_has_frr { my $fabrics = $running_config->{fabrics}->{ids} // {}; my $route_maps = $running_config->{'route-maps'}->{ids} // {}; my $prefix_lists = $running_config->{'prefix-lists'}->{ids} // {}; + my $zones = $running_config->{zones}->{ids} // {}; + my $zone_routes = + grep { $_->{type} eq 'simple' && scalar(@{ $_->{route} // [] }) } values %$zones; my $local_frr_config = PVE::Network::SDN::Frr::local_frr_config_exists(); - return %$controllers || %$fabrics || %$route_maps || %$prefix_lists || $local_frr_config; + return + %$controllers + || %$fabrics + || %$route_maps + || %$prefix_lists + || $zone_routes + || $local_frr_config; } sub pending_config { @@ -563,11 +573,14 @@ sub generate_frr_raw_config { my $frr_config = {}; + my $nodename = PVE::INotify::nodename(); + PVE::Network::SDN::Controllers::generate_frr_config($frr_config, $running_config); + my $routes = + PVE::RS::SDN::get_zone_routes($running_config->{zones} // { ids => {} }, $nodename); + push @{ $frr_config->{frr}->{ip_routes} }, @$routes; PVE::Network::SDN::Frr::append_local_config($frr_config); - my $nodename = PVE::INotify::nodename(); - return PVE::RS::SDN::get_frr_raw_config( $frr_config->{'frr'}, $prefix_list_config, @@ -619,6 +632,9 @@ sub generate_dhcp_config { sub encode_value { my ($type, $key, $value) = @_; + # Route strings contain commas themselves, so preserve array element boundaries. + return join("\n", sort @$value) if $key eq 'route' && ref($value) eq 'ARRAY'; + if ( $key eq 'nodes' || $key eq 'exitnodes' diff --git a/src/PVE/Network/SDN/Zones/Plugin.pm b/src/PVE/Network/SDN/Zones/Plugin.pm index dc8f19cb6587..a3863bc9818c 100644 --- a/src/PVE/Network/SDN/Zones/Plugin.pm +++ b/src/PVE/Network/SDN/Zones/Plugin.pm @@ -8,6 +8,7 @@ use Net::IP qw(ip_get_version); use PVE::JSONSchema; use PVE::Cluster; use PVE::Network; +use PVE::RS::SDN; use PVE::JSONSchema qw(get_standard_option); use base qw(PVE::SectionConfig); @@ -29,6 +30,50 @@ PVE::JSONSchema::register_standard_option( }, ); +my $static_route_fmt = { + prefix => { + type => 'string', + description => 'Destination prefix in CIDR notation, including default routes.', + }, + via => { + type => 'string', + format => 'ip', + optional => 1, + description => 'Next-hop IP address. IPv6 link-local addresses require an interface.', + }, + iface => { + type => 'string', + optional => 1, + description => 'Outgoing interface. At least one of via or iface is required.', + }, + distance => { + type => 'integer', + minimum => 1, + maximum => 255, + optional => 1, + description => 'Administrative distance.', + }, + 'nexthop-vrf' => { + type => 'string', + optional => 1, + description => 'VRF of the next hop, or default for the default routing table.', + }, +}; + +PVE::JSONSchema::register_format( + 'pve-sdn-static-route', + $static_route_fmt, + sub { + my ($route) = @_; + + PVE::RS::SDN::validate_static_route( + PVE::JSONSchema::print_property_string($route, $static_route_fmt), + ); + + return $route; + }, +); + my $defaultData = { propertyList => { @@ -39,6 +84,16 @@ my $defaultData = { type => 'string', }, nodes => get_standard_option('pve-node-list', { optional => 1 }), + route => { + type => 'array', + optional => 1, + description => + 'Static routes in the zone VRF, or the default table for Simple zones without dedicated-vrf.', + items => { + type => 'string', + format => 'pve-sdn-static-route', + }, + }, zone => get_standard_option( 'pve-sdn-zone-id', { diff --git a/src/PVE/Network/SDN/Zones/SimplePlugin.pm b/src/PVE/Network/SDN/Zones/SimplePlugin.pm index bc591839819d..1d44f0b078f5 100644 --- a/src/PVE/Network/SDN/Zones/SimplePlugin.pm +++ b/src/PVE/Network/SDN/Zones/SimplePlugin.pm @@ -53,6 +53,7 @@ sub options { ipam => { optional => 1 }, dhcp => { optional => 1 }, 'dedicated-vrf' => { optional => 1 }, + route => { optional => 1 }, }; } diff --git a/src/test/Makefile b/src/test/Makefile index 607153a8a1b5..85aad2a8e439 100644 --- a/src/test/Makefile +++ b/src/test/Makefile @@ -1,10 +1,13 @@ all: test -test: test_zones test_ipams test_dns test_subnets test_vnets_blackbox test_running_config +test: test_zones test_static_routes test_ipams test_dns test_subnets test_vnets_blackbox test_running_config test_zones: run_test_zones.pl ./run_test_zones.pl +test_static_routes: run_test_static_routes.pl + perl ./run_test_static_routes.pl + test_ipams: run_test_ipams.pl ./run_test_ipams.pl diff --git a/src/test/run_test_static_routes.pl b/src/test/run_test_static_routes.pl new file mode 100644 index 000000000000..c6809fe4dc9c --- /dev/null +++ b/src/test/run_test_static_routes.pl @@ -0,0 +1,178 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use lib qw(..); + +use Storable qw(dclone); +use Test::More; +use Test::MockModule; + +use PVE::API2::Network::SDN::Zones; +use PVE::Network::SDN; + +my $routes = [ + 'prefix=0.0.0.0/0,via=192.0.2.1,iface=uplink0,distance=20,nexthop-vrf=default', + 'prefix=::/0,via=fe80::1,iface=uplink0,nexthop-vrf=default', + 'prefix=10.20.0.0/16,iface=vnet0', +]; +my $zones = { + ids => { + isolated => { + type => 'simple', + 'dedicated-vrf' => 1, + nodes => { localhost => 1 }, + route => $routes, + }, + plain => { + type => 'simple', + route => ['prefix=198.51.100.0/24,via=192.0.2.1'], + }, + remote => { + type => 'simple', + nodes => { othernode => 1 }, + route => ['prefix=192.0.2.0/24,iface=remote0'], + }, + }, +}; + +subtest 'route API schema and Rust validation' => sub { + my $create = PVE::API2::Network::SDN::Zones->map_method_by_name('create'); + my $schema = $create->{parameters}->{properties}->{route}; + is($schema->{type}, 'array', 'API accepts repeatable routes'); + + my $errors = {}; + PVE::JSONSchema::check_prop($routes, $schema, 'route', $errors); + is_deeply($errors, {}, 'default routes and interface-only routes are valid'); + + for my $route ( + 'via=192.0.2.1', + 'prefix=0.0.0.0/0', + 'prefix=::/0,via=fe80::1', + 'prefix=0.0.0.0/0,via=2001:db8::1', + 'prefix=::/0,via=192.0.2.1', + 'prefix=0.0.0.0/0,via=192.0.2.1,distance=256', + ) { + my $errors = {}; + PVE::JSONSchema::check_prop([$route], $schema, 'route', $errors); + ok(scalar(keys %$errors), "reject invalid route: $route"); + } + + for my $method ('index', 'read') { + my $info = PVE::API2::Network::SDN::Zones->map_method_by_name($method); + my $properties = + $method eq 'index' + ? $info->{returns}->{items}->{properties} + : $info->{returns}->{properties}; + is_deeply( + $properties->{route}->{items}, + $schema->{items}, + "$method returns route strings", + ); + is( + $properties->{pending}->{properties}->{route}->{type}, + 'array', + "$method pending routes are arrays", + ); + } + + eval { + PVE::Network::SDN::Zones::VlanPlugin->check_config( + 'vlanzone', + { bridge => 'vmbr0', route => $routes }, + 1, + 1, + ); + }; + like($@, qr/unexpected property 'route'/, 'routes are only supported on simple zones'); +}; + +subtest 'repeatable config persistence' => sub { + my $raw = PVE::Network::SDN::Zones::Plugin->write_config('zones.cfg', dclone($zones)); + is(scalar(() = $raw =~ /^\s+route /gm), 5, 'one config line per route'); + my $parsed = PVE::Network::SDN::Zones::Plugin->parse_config('zones.cfg', $raw); + ok(!$parsed->{errors}, 'all routes parse without errors'); + is_deeply($parsed->{ids}, $zones->{ids}, 'routes and node restrictions survive round trip'); +}; + +subtest 'pending arrays compare by value' => sub { + my $running = { zones => dclone($zones) }; + my $configured = dclone($zones); + my $pending = PVE::Network::SDN::pending_config($running, $configured, 'zones'); + ok(!$pending->{ids}->{isolated}->{state}, 'equal arrays are unchanged'); + + $configured->{ids}->{isolated}->{route} = [reverse @$routes]; + $pending = PVE::Network::SDN::pending_config($running, $configured, 'zones'); + ok(!$pending->{ids}->{isolated}->{state}, 'route ordering does not cause pending changes'); + + $configured->{ids}->{isolated}->{route} = ['prefix=::/0,iface=vnet1']; + $pending = PVE::Network::SDN::pending_config($running, $configured, 'zones'); + is($pending->{ids}->{isolated}->{state}, 'changed', 'route modification detected'); + is_deeply( + $pending->{ids}->{isolated}->{pending}->{route}, + ['prefix=::/0,iface=vnet1'], + 'pending retains an array', + ); + + delete $configured->{ids}->{isolated}->{route}; + $pending = PVE::Network::SDN::pending_config($running, $configured, 'zones'); + is($pending->{ids}->{isolated}->{pending}->{route}, 'deleted', 'route removal detected'); +}; + +my $running = { zones => $zones, controllers => { ids => {} }, vnets => { ids => {} } }; +my $sdn = Test::MockModule->new('PVE::Network::SDN'); +$sdn->mock(running_config => sub { return $running; }); +my $inotify = Test::MockModule->new('PVE::INotify'); +$inotify->mock(nodename => sub { return 'localhost'; }); +my $controllers = Test::MockModule->new('PVE::Network::SDN::Controllers'); +$controllers->mock(read_etc_network_interfaces => sub { return { ifaces => {} }; }); +my $frr = Test::MockModule->new('PVE::Network::SDN::Frr'); +$frr->mock(local_frr_config_exists => sub { return 0; }); +$frr->mock( + read_local_frr_config => sub { return 'ip prefix-list LOCAL seq 5 permit 203.0.113.0/24'; }, +); + +subtest 'route-only FRR detection' => sub { + ok(PVE::Network::SDN::running_config_has_frr(), 'routes without controllers require FRR'); + local $running->{zones} = { ids => { empty => { type => 'simple', route => [] } } }; + ok(!PVE::Network::SDN::running_config_has_frr(), 'empty route array does not require FRR'); +}; + +subtest 'zone routes reach FRR with local config' => sub { + my $fabric_config = PVE::RS::SDN::Fabrics->running_config({}); + my $route_maps = PVE::RS::SDN::RouteMaps->running_config({}); + my $prefix_lists = PVE::RS::SDN::PrefixLists->running_config({}); + my $raw = PVE::Network::SDN::generate_frr_raw_config( + $running, $fabric_config, $route_maps, $prefix_lists, + ); + my $config = join("\n", @$raw); + like( + $config, + qr/^ip route 0\.0\.0\.0\/0 192\.0\.2\.1 uplink0 20 nexthop-vrf default vrf vrf_isolated$/m, + 'IPv4 default route with distance and cross-VRF next hop', + ); + like( + $config, + qr/^ipv6 route ::\/0 fe80::1 uplink0 nexthop-vrf default vrf vrf_isolated$/m, + 'IPv6 link-local default route', + ); + like( + $config, + qr/^ip route 10\.20\.0\.0\/16 vnet0 vrf vrf_isolated$/m, + 'interface-only route in zone VRF', + ); + like( + $config, + qr/^ip route 198\.51\.100\.0\/24 192\.0\.2\.1(?: vrf default)?$/m, + 'zone without dedicated VRF uses default table', + ); + unlike($config, qr/remote0/, 'routes restricted to another node are omitted'); + like( + $config, + qr/^ip prefix-list LOCAL seq 5 permit 203\.0\.113\.0\/24$/m, + 'local FRR configuration coexists with zone routes', + ); +}; + +done_testing(); -- 2.47.3