[PATCH pve-network v5 24/46] sdn: add prefix lists module

Stefan Hanreich
2026-05-05T15:39:55Z

Defines helpers for common operations (reading / writing
configuration) as well as the required formats / schema definitions
for the route map API.

Signed-off-by: Stefan Hanreich <s.hanreich@proxmox.com>
---
src/PVE/Network/SDN/Makefile | 13 ++-
src/PVE/Network/SDN/PrefixLists.pm | 134 +++++++++++++++++++++++++++++
2 files changed, 146 insertions(+), 1 deletion(-)
create mode 100644 src/PVE/Network/SDN/PrefixLists.pm

diff --git a/src/PVE/Network/SDN/Makefile b/src/PVE/Network/SDN/Makefile
index d1ffef9..e8bed83 100644
--- a/src/PVE/Network/SDN/Makefile
+++ b/src/PVE/Network/SDN/Makefile
@@ -1,4 +1,15 @@
-SOURCES=Vnets.pm VnetPlugin.pm Zones.pm Controllers.pm Subnets.pm SubnetPlugin.pm Ipams.pm Dns.pm Dhcp.pm Fabrics.pm Frr.pm
+SOURCES=Vnets.pm\
+ VnetPlugin.pm\
+ Zones.pm\
+ Controllers.pm\
+ Subnets.pm\
+ SubnetPlugin.pm\
+ Ipams.pm\
+ Dns.pm\
+ Dhcp.pm\
+ Fabrics.pm\
+ Frr.pm\
+ PrefixLists.pm

PERL5DIR=${DESTDIR}/usr/share/perl5
diff --git a/src/PVE/Network/SDN/PrefixLists.pm b/src/PVE/Network/SDN/PrefixLists.pm
new file mode 100644
index 0000000..ced2ebf
--- /dev/null
+++ b/src/PVE/Network/SDN/PrefixLists.pm
@@ -0,0 +1,134 @@
+package PVE::Network::SDN::PrefixLists;
+
+use strict;
+use warnings;
+
+use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_lock_file cfs_write_file);
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::INotify;
+use PVE::Network::SDN;
+use PVE::RS::SDN::PrefixLists;
+
+PVE::JSONSchema::register_format(
+ 'pve-sdn-prefix-list-id',
+ sub {
+ my ($id, $noerr) = @_;
+
+ if ($id =~ m/^(only_default|only_default_v6|loopbacks_ips)$/) {
+ return undef if $noerr;
+ die "prefix list ID '$id' is currently reserved and cannot be used\n";
+ }
+
+ if ($id !~ m/^[a-zA-Z0-9][a-zA-Z0-9-_]{0,30}[a-zA-Z0-9]?$/i) {
+ return undef if $noerr;
+ die "prefix list ID '$id' contains illegal characters\n";
+ }
+
+ return $id;
+ },
+);
+
+PVE::JSONSchema::register_standard_option(
+ 'pve-sdn-prefix-list-id',
+ {
+ description => "The SDN prefix list identifier",
+ type => 'string',
+ format => 'pve-sdn-prefix-list-id',
+ },
+);
+
+cfs_register_file(
+ 'sdn/prefix-lists.cfg', \&parse_prefix_lists_config, \&write_prefix_lists_config,
+);
+
+sub parse_prefix_lists_config {
+ my ($filename, $raw) = @_;
+ return $raw // '';
+}
+
+sub write_prefix_lists_config {
+ my ($filename, $config) = @_;
+ return $config // '';
+}
+
+sub config {
+ my ($running) = @_;
+
+ if ($running) {
+ my $running_config = PVE::Network::SDN::running_config();
+
+ # if the config hasn't yet been applied after the introduction of
+ # prefix lists then the key does not exist in the running config so we
+ # default to an empty hash
+ my $prefix_lists_config = $running_config->{'prefix-lists'}->{ids} // {};
+ return PVE::RS::SDN::PrefixLists->running_config($prefix_lists_config);
+ }
+
+ my $prefix_lists_config = cfs_read_file("sdn/prefix-lists.cfg");
+ return PVE::RS::SDN::PrefixLists->config($prefix_lists_config);
+}
+
+sub write_config {
+ my ($config) = @_;
+ cfs_write_file("sdn/prefix-lists.cfg", $config->to_raw(), 1);
+}
+
+sub prefix_list_properties {
+ my ($update) = @_;
+
+ my $properties = {
+ digest => get_standard_option('pve-config-digest'),
+ entries => {
+ type => 'array',
+ optional => 1,
+ items => {
+ type => 'string',
+ format => {
+ action => {
+ type => 'string',
+ enum => ['permit', 'deny'],
+ },
+ prefix => {
+ type => 'string',
+ format => 'CIDR',
+ },
+ le => {
+ type => 'integer',
+ minimum => 0,
+ maximum => 128,
+ optional => 1,
+ },
+ ge => {
+ type => 'integer',
+ minimum => 0,
+ maximum => 128,
+ optional => 1,
+ },
+ seq => {
+ type => 'integer',
+ minimum => 0,
+ maximum => 2**32 - 1,
+ optional => 1,
+ },
+ },
+ },
+ },
+ };
+
+ if ($update) {
+ $properties->{delete} = {
+ type => 'array',
+ optional => 1,
+ items => {
+ type => 'string',
+ enum => ['entries'],
+ },
+ };
+ } else {
+ $properties->{id} = get_standard_option('pve-sdn-prefix-list-id');
+ }
+
+ return $properties;
+}
+
+1;
--
2.47.3