From 1d44ce704733eeac5e892d4e70a84d4e2ed72775 Mon Sep 17 00:00:00 2001 From: Alexandre Derumier Date: Wed, 1 Jul 2020 09:10:37 +0200 Subject: [PATCH] vnet: make tag optional and verify value in zone plugins Signed-off-by: Alexandre Derumier --- PVE/API2/Network/SDN/Vnets.pm | 15 +++++++++++++++ PVE/Network/SDN/VnetPlugin.pm | 5 +++-- PVE/Network/SDN/Zones/EvpnPlugin.pm | 7 +++++++ PVE/Network/SDN/Zones/Plugin.pm | 5 +++++ PVE/Network/SDN/Zones/QinQPlugin.pm | 8 ++++++++ PVE/Network/SDN/Zones/SimplePlugin.pm | 7 +++++++ PVE/Network/SDN/Zones/VlanPlugin.pm | 8 ++++++++ PVE/Network/SDN/Zones/VxlanPlugin.pm | 8 ++++++++ 8 files changed, 61 insertions(+), 2 deletions(-) diff --git a/PVE/API2/Network/SDN/Vnets.pm b/PVE/API2/Network/SDN/Vnets.pm index 8f70bab..5d66908 100644 --- a/PVE/API2/Network/SDN/Vnets.pm +++ b/PVE/API2/Network/SDN/Vnets.pm @@ -7,6 +7,8 @@ use PVE::SafeSyslog; use PVE::Tools qw(extract_param); use PVE::Cluster qw(cfs_read_file cfs_write_file); use PVE::Network::SDN; +use PVE::Network::SDN::Zones; +use PVE::Network::SDN::Zones::Plugin; use PVE::Network::SDN::Vnets; use PVE::Network::SDN::VnetPlugin; @@ -129,6 +131,13 @@ __PACKAGE__->register_method ({ } $cfg->{ids}->{$id} = $opts; + + my $zone_cfg = PVE::Network::SDN::Zones::config(); + my $zoneid = $cfg->{ids}->{$id}->{zone}; + my $plugin_config = $zone_cfg->{ids}->{$zoneid}; + my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type}); + $plugin->verify_tag($opts->{tag}); + PVE::Network::SDN::VnetPlugin->on_update_hook($id, $cfg); PVE::Network::SDN::Vnets::write_config($cfg); @@ -168,6 +177,12 @@ __PACKAGE__->register_method ({ my $opts = PVE::Network::SDN::VnetPlugin->check_config($id, $param, 0, 1); $cfg->{ids}->{$id} = $opts; + my $zone_cfg = PVE::Network::SDN::Zones::config(); + my $zoneid = $cfg->{ids}->{$id}->{zone}; + my $plugin_config = $zone_cfg->{ids}->{$zoneid}; + my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type}); + $plugin->verify_tag($opts->{tag}); + PVE::Network::SDN::VnetPlugin->on_update_hook($id, $cfg); PVE::Network::SDN::Vnets::write_config($cfg); diff --git a/PVE/Network/SDN/VnetPlugin.pm b/PVE/Network/SDN/VnetPlugin.pm index 2433013..384358c 100644 --- a/PVE/Network/SDN/VnetPlugin.pm +++ b/PVE/Network/SDN/VnetPlugin.pm @@ -6,6 +6,7 @@ use warnings; use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file); use base qw(PVE::SectionConfig); use PVE::JSONSchema qw(get_standard_option); +use PVE::Exception qw(raise raise_param_exc); PVE::Cluster::cfs_register_file('sdn/vnets.cfg', sub { __PACKAGE__->parse_config(@_); }, @@ -88,7 +89,7 @@ sub properties { sub options { return { zone => { optional => 0}, - tag => { optional => 0}, + tag => { optional => 1}, alias => { optional => 1 }, ipv4 => { optional => 1 }, ipv6 => { optional => 1 }, @@ -112,7 +113,7 @@ sub on_update_hook { next if $id eq $vnetid; my $vnet = $vnet_cfg->{ids}->{$id}; if ($vnet->{type} eq 'vnet' && defined($vnet->{tag})) { - die "tag $tag already exist in vnet $id" if $tag eq $vnet->{tag}; + raise_param_exc({ tag => "tag $tag already exist in vnet $id"}) if $tag eq $vnet->{tag}; } } } diff --git a/PVE/Network/SDN/Zones/EvpnPlugin.pm b/PVE/Network/SDN/Zones/EvpnPlugin.pm index b2f57ee..a916579 100644 --- a/PVE/Network/SDN/Zones/EvpnPlugin.pm +++ b/PVE/Network/SDN/Zones/EvpnPlugin.pm @@ -140,6 +140,13 @@ sub on_update_hook { } } +sub verify_tag { + my ($class, $tag) = @_; + + raise_param_exc({ tag => "missing vxlan tag"}) if !defined($tag); + raise_param_exc({ tag => "vxlan tag max value is 16777216"}) if $tag > 16777216; +} + 1; diff --git a/PVE/Network/SDN/Zones/Plugin.pm b/PVE/Network/SDN/Zones/Plugin.pm index 5e3fdfd..d96e069 100644 --- a/PVE/Network/SDN/Zones/Plugin.pm +++ b/PVE/Network/SDN/Zones/Plugin.pm @@ -139,6 +139,11 @@ sub on_update_hook { # do nothing by default } +sub verify_tag { + my ($class, $tag) = @_; + # do nothing by default +} + #helpers sub parse_tag_number_or_range { my ($str, $max, $tag) = @_; diff --git a/PVE/Network/SDN/Zones/QinQPlugin.pm b/PVE/Network/SDN/Zones/QinQPlugin.pm index c8dd0ab..b39732a 100644 --- a/PVE/Network/SDN/Zones/QinQPlugin.pm +++ b/PVE/Network/SDN/Zones/QinQPlugin.pm @@ -3,6 +3,7 @@ package PVE::Network::SDN::Zones::QinQPlugin; use strict; use warnings; use PVE::Network::SDN::Zones::Plugin; +use PVE::Exception qw(raise raise_param_exc); use base('PVE::Network::SDN::Zones::Plugin'); @@ -210,6 +211,13 @@ sub status { return $err_msg; } +sub verify_tag { + my ($class, $tag) = @_; + + raise_param_exc({ tag => "missing vlan tag"}) if !defined($tag); + raise_param_exc({ tag => "vlan tag max value is 4096"}) if $tag > 4096; +} + 1; diff --git a/PVE/Network/SDN/Zones/SimplePlugin.pm b/PVE/Network/SDN/Zones/SimplePlugin.pm index 60fb7db..9fd3b29 100644 --- a/PVE/Network/SDN/Zones/SimplePlugin.pm +++ b/PVE/Network/SDN/Zones/SimplePlugin.pm @@ -3,6 +3,7 @@ package PVE::Network::SDN::Zones::SimplePlugin; use strict; use warnings; use PVE::Network::SDN::Zones::Plugin; +use PVE::Exception qw(raise raise_param_exc); use base('PVE::Network::SDN::Zones::Plugin'); @@ -65,6 +66,12 @@ sub status { return $err_msg; } +sub verify_tag { + my ($class, $tag) = @_; + + raise_param_exc({ tag => "vlan tag is not allowed on simple bridge"}) if defined($tag); +} + 1; diff --git a/PVE/Network/SDN/Zones/VlanPlugin.pm b/PVE/Network/SDN/Zones/VlanPlugin.pm index dedb32c..db719a0 100644 --- a/PVE/Network/SDN/Zones/VlanPlugin.pm +++ b/PVE/Network/SDN/Zones/VlanPlugin.pm @@ -3,6 +3,7 @@ package PVE::Network::SDN::Zones::VlanPlugin; use strict; use warnings; use PVE::Network::SDN::Zones::Plugin; +use PVE::Exception qw(raise raise_param_exc); use base('PVE::Network::SDN::Zones::Plugin'); @@ -169,6 +170,13 @@ sub status { return $err_msg; } +sub verify_tag { + my ($class, $tag) = @_; + + raise_param_exc({ tag => "missing vlan tag"}) if !defined($tag); + raise_param_exc({ tag => "vlan tag max value is 4096"}) if $tag > 4096; +} + 1; diff --git a/PVE/Network/SDN/Zones/VxlanPlugin.pm b/PVE/Network/SDN/Zones/VxlanPlugin.pm index e8cf1bd..a256268 100644 --- a/PVE/Network/SDN/Zones/VxlanPlugin.pm +++ b/PVE/Network/SDN/Zones/VxlanPlugin.pm @@ -6,6 +6,7 @@ use PVE::Network::SDN::Zones::Plugin; use PVE::Tools qw($IPV4RE); use PVE::INotify; use PVE::Network::SDN::Controllers::EvpnPlugin; +use PVE::Exception qw(raise raise_param_exc); use base('PVE::Network::SDN::Zones::Plugin'); @@ -94,6 +95,13 @@ sub generate_sdn_config { return $config; } +sub verify_tag { + my ($class, $tag) = @_; + + raise_param_exc({ tag => "missing vxlan tag"}) if !defined($tag); + raise_param_exc({ tag => "vxlan tag max value is 16777216"}) if $tag > 16777216; +} + 1; -- 2.39.5