[pve-devel] [PATCH pve-network 1/3] zones: add bridge helpers

Alexandre Derumier aderumier at odiso.com
Wed Dec 9 16:11:17 CET 2020


Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/Network/SDN/Zones/Plugin.pm       | 30 +++++++++++++++++++++++++++
 PVE/Network/SDN/Zones/QinQPlugin.pm   | 17 +++++++--------
 PVE/Network/SDN/Zones/SimplePlugin.pm |  1 +
 PVE/Network/SDN/Zones/VlanPlugin.pm   | 16 ++++++--------
 4 files changed, 44 insertions(+), 20 deletions(-)

diff --git a/PVE/Network/SDN/Zones/Plugin.pm b/PVE/Network/SDN/Zones/Plugin.pm
index 9db2791..9eabe73 100644
--- a/PVE/Network/SDN/Zones/Plugin.pm
+++ b/PVE/Network/SDN/Zones/Plugin.pm
@@ -302,4 +302,34 @@ sub find_local_ip_interface_peers {
     }
 }
 
+sub find_bridge {
+    my ($bridge) = @_;
+
+    die "can't find bridge $bridge" if !-d "/sys/class/net/$bridge";
+}
+
+sub is_vlanaware {
+    my ($bridge) = @_;
+
+    return PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
+}
+
+sub is_ovs {
+    my ($bridge) = @_;
+
+    my $is_ovs = !-d "/sys/class/net/$bridge/brif";
+    return $is_ovs;    
+}
+
+sub get_bridge_ifaces {
+    my ($bridge) = @_;
+
+    my @bridge_ifaces = ();
+    my $dir = "/sys/class/net/$bridge/brif";
+    PVE::Tools::dir_glob_foreach($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub {
+	push @bridge_ifaces, $_[0];
+    });
+
+    return @bridge_ifaces;
+}
 1;
diff --git a/PVE/Network/SDN/Zones/QinQPlugin.pm b/PVE/Network/SDN/Zones/QinQPlugin.pm
index 2bd60db..9064adb 100644
--- a/PVE/Network/SDN/Zones/QinQPlugin.pm
+++ b/PVE/Network/SDN/Zones/QinQPlugin.pm
@@ -57,10 +57,11 @@ sub generate_sdn_config {
     my $vlanprotocol = $plugin_config->{'vlan-protocol'};
     my $ctag = $vnet->{tag};
     my $alias = $vnet->{alias};
-    die "can't find bridge $bridge" if !-d "/sys/class/net/$bridge";
 
-    my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
-    my $is_ovs = !-d "/sys/class/net/$bridge/brif";
+    PVE::Network::SDN::Zones::Plugin::find_bridge($bridge);
+
+    my $vlan_aware = PVE::Network::SDN::Zones::Plugin::is_vlanaware($bridge);
+    my $is_ovs = PVE::Network::SDN::Zones::Plugin::is_ovs($bridge);
 
     my @iface_config = ();
     my $vnet_bridge_ports = "";
@@ -126,11 +127,7 @@ sub generate_sdn_config {
 
 	#eth--->eth.x(svlan)--->eth.x.y(cvlan)---->vnet
 
-	my @bridge_ifaces = ();
-	my $dir = "/sys/class/net/$bridge/brif";
-	PVE::Tools::dir_glob_foreach($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub {
-	    push @bridge_ifaces, $_[0];
-	});
+	my @bridge_ifaces = PVE::Network::SDN::Zones::Plugin::get_bridge_ifaces($bridge);
 
 	foreach my $bridge_iface (@bridge_ifaces) {
 
@@ -181,8 +178,8 @@ sub status {
         return $err_msg;
     }
 
-    my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
-    my $is_ovs = !-d "/sys/class/net/$bridge/brif";
+    my $vlan_aware = PVE::Network::SDN::Zones::Plugin::is_vlanaware($bridge);
+    my $is_ovs = PVE::Network::SDN::Zones::Plugin::is_ovs($bridge);
 
     my $tag = $vnet->{tag};
     my $vnet_uplink = "ln_".$vnetid;
diff --git a/PVE/Network/SDN/Zones/SimplePlugin.pm b/PVE/Network/SDN/Zones/SimplePlugin.pm
index 94452a0..9f74f3e 100644
--- a/PVE/Network/SDN/Zones/SimplePlugin.pm
+++ b/PVE/Network/SDN/Zones/SimplePlugin.pm
@@ -58,6 +58,7 @@ sub generate_sdn_config {
 
     my $address = {};
     my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid, 1);
+
     foreach my $subnetid (sort keys %{$subnets}) {
 	my $subnet = $subnets->{$subnetid};
 	my $cidr = $subnet->{cidr};
diff --git a/PVE/Network/SDN/Zones/VlanPlugin.pm b/PVE/Network/SDN/Zones/VlanPlugin.pm
index ca6bd8f..cda2a36 100644
--- a/PVE/Network/SDN/Zones/VlanPlugin.pm
+++ b/PVE/Network/SDN/Zones/VlanPlugin.pm
@@ -46,10 +46,10 @@ sub generate_sdn_config {
     my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $controller, $controller_cfg, $subnet_cfg, $interfaces_config, $config) = @_;
 
     my $bridge = $plugin_config->{bridge};
-    die "can't find bridge $bridge" if !-d "/sys/class/net/$bridge";
+    PVE::Network::SDN::Zones::Plugin::find_bridge($bridge);
 
-    my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
-    my $is_ovs = !-d "/sys/class/net/$bridge/brif";
+    my $vlan_aware = PVE::Network::SDN::Zones::Plugin::is_vlanaware($bridge);
+    my $is_ovs = PVE::Network::SDN::Zones::Plugin::is_ovs($bridge);
 
     my $tag = $vnet->{tag};
     my $alias = $vnet->{alias};
@@ -91,11 +91,7 @@ sub generate_sdn_config {
 
 	my $bridgevlan = $bridge."v".$tag;
 
-	my @bridge_ifaces = ();
-	my $dir = "/sys/class/net/$bridge/brif";
-	PVE::Tools::dir_glob_foreach($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub {
-	    push @bridge_ifaces, $_[0];
-	});
+	my @bridge_ifaces = PVE::Network::SDN::Zones::Plugin::get_bridge_ifaces($bridge);
 
 	my $bridge_ports = "";
 	foreach my $bridge_iface (@bridge_ifaces) {
@@ -146,8 +142,8 @@ sub status {
 	return $err_msg;
     }
 
-    my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
-    my $is_ovs = !-d "/sys/class/net/$bridge/brif";
+    my $vlan_aware = PVE::Network::SDN::Zones::Plugin::is_vlanaware($bridge);
+    my $is_ovs = PVE::Network::SDN::Zones::Plugin::is_ovs($bridge);
 
     my $tag = $vnet->{tag};
     my $vnet_uplink = "ln_".$vnetid;
-- 
2.20.1





More information about the pve-devel mailing list