[pve-devel] [PATCH common v5 3/7] fix #3893: network: add vlan id and range parameter definitions

Aaron Lauterer a.lauterer at proxmox.com
Wed Oct 2 15:11:53 CEST 2024


This is one step to make it possible to define the VLAN IDs and ranges
for bridges.

It is expected to be used in combination with the `-list` magic
property. Therefore it defines and checks the validity of a single list
item that could just be a single VLAN tag ID or a range.

Signed-off-by: Aaron Lauterer <a.lauterer at proxmox.com>
---
changes since
v4:
* reordered, used to be 2/7
* $check_vid got renamed to $valid_vid
* $valid_vid no returns explicit truthy/falsy values
* use `return` instead of `return undef`
* reworked last check ($start >= $end) to handle $noerr better

v3:superfloous
* switched regex to one with non-capturing group
* fixed superfloous space
v2:
* renamed schema to a more generic one with the use case of the guest
  trunk config in mind

 src/PVE/JSONSchema.pm | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/src/PVE/JSONSchema.pm b/src/PVE/JSONSchema.pm
index ce39092..7c63af1 100644
--- a/src/PVE/JSONSchema.pm
+++ b/src/PVE/JSONSchema.pm
@@ -81,6 +81,12 @@ register_standard_option('pve-iface', {
     minLength => 2, maxLength => 20,
 });
 
+register_standard_option('pve-vlan-id-or-range', {
+    description => "VLAN ID or range.",
+    type => 'string', format => 'pve-vlan-id-or-range',
+    minLength => 1, maxLength => 9,
+});
+
 register_standard_option('pve-storage-id', {
     description => "The storage identifier.",
     type => 'string', format => 'pve-storage-id',
@@ -595,6 +601,39 @@ sub pve_verify_iface {
     return $id;
 }
 
+# vlan id (vids), single or range
+register_format('pve-vlan-id-or-range', \&pve_verify_vlan_id_or_range);
+sub pve_verify_vlan_id_or_range {
+    my ($vlan, $noerr) = @_;
+
+    my $valid_vid = sub {
+	my $tag = shift;
+	if ($tag < 2 || $tag > 4094) {
+	    return 0 if $noerr;
+	    die "invalid VLAN tag '$tag'\n";
+	}
+	return 1;
+    };
+
+    if ($vlan !~ m/^(\d+)(?:-(\d+))?$/) {
+	return if $noerr;
+	die "invalid VLAN configuration '$vlan'\n";
+    }
+    my $start = $1;
+    my $end = $2;
+    return if !$valid_vid->($start);
+    if ($end) {
+	return if !$valid_vid->($end);
+
+	if ($start >= $end) {
+	    return if $noerr;
+	    die "VLAN range must go from lower to higher tag '$vlan'\n";
+	}
+    }
+
+    return $vlan;
+}
+
 # general addresses by name or IP
 register_format('address', \&pve_verify_address);
 sub pve_verify_address {
-- 
2.39.5





More information about the pve-devel mailing list