[pve-devel] [PATCH pve-network] add tap|veth create|plug

Alexandre Derumier aderumier at odiso.com
Mon Mar 9 10:24:13 CET 2020


---
 PVE/Network/SDN/Vnets.pm            |  8 ++++
 PVE/Network/SDN/Zones.pm            | 74 ++++++++++++++++++++++++++---
 PVE/Network/SDN/Zones/Plugin.pm     | 39 +++++++++++++--
 PVE/Network/SDN/Zones/QinQPlugin.pm |  2 +-
 PVE/Network/SDN/Zones/VlanPlugin.pm |  3 +-
 5 files changed, 112 insertions(+), 14 deletions(-)

diff --git a/PVE/Network/SDN/Vnets.pm b/PVE/Network/SDN/Vnets.pm
index f17c278..725605b 100644
--- a/PVE/Network/SDN/Vnets.pm
+++ b/PVE/Network/SDN/Vnets.pm
@@ -56,4 +56,12 @@ sub complete_sdn_vnet {
     return  $cmdname eq 'add' ? [] : [ PVE::Network::SDN::Vnets::sdn_vnet_ids($cfg) ];
 }
 
+sub get_vnet {
+    my ($vnetid) = @_;
+
+    my $cfg = PVE::Network::SDN::Vnets::config();
+    my $vnet = PVE::Network::SDN::Vnets::sdn_vnets_config($cfg, $vnetid, 1);
+    return $vnet;
+}
+
 1;
diff --git a/PVE/Network/SDN/Zones.pm b/PVE/Network/SDN/Zones.pm
index 4a154f4..17ef507 100644
--- a/PVE/Network/SDN/Zones.pm
+++ b/PVE/Network/SDN/Zones.pm
@@ -8,6 +8,7 @@ use JSON;
 
 use PVE::Tools qw(extract_param dir_glob_regex run_command);
 use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
+use PVE::Network;
 
 use PVE::Network::SDN::Vnets;
 use PVE::Network::SDN::Zones::VlanPlugin;
@@ -217,21 +218,80 @@ sub status {
 sub get_bridge_vlan {
     my ($vnetid) = @_;
 
-    my $vnet_cfg = PVE::Cluster::cfs_read_file('sdn/vnets.cfg');
-    my $zone_cfg = PVE::Cluster::cfs_read_file('sdn/zones.cfg');
-    my $nodename = PVE::INotify::nodename();
+    my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
 
-    my $vnet = $vnet_cfg->{ids}->{$vnetid};
-    return if !$vnet;
+    #fallback if classic bridge
+    return ($vnetid, undef) if !$vnet;
 
+    my $zone_cfg = PVE::Network::SDN::Zones::config();
     my $zoneid = $vnet->{zone};
     my $tag = $vnet->{tag};
 
-    die "vnet $vnetid is not allowed on this node" if defined($zone_cfg->{ids}->{$zoneid}->{nodes}) && !$zone_cfg->{ids}->{$zoneid}->{nodes}->{$nodename};
+    my $plugin_config = $zone_cfg->{ids}->{$zoneid};
+    my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
+    return $plugin->get_bridge_vlan($plugin_config, $vnetid, $tag);
+}
+
+sub tap_create {
+    my ($iface, $bridge) = @_;
+
+    my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge);
+
+    #fallback if classic bridge
+    if(!$vnet) {
+	PVE::Network::tap_create($iface, $bridge);
+        return;
+    }
+
+    my $zone_cfg = PVE::Network::SDN::Zones::config();
+    my $zoneid = $vnet->{zone};
+
+    my $plugin_config = $zone_cfg->{ids}->{$zoneid};
+    my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
+    $plugin->tap_create($plugin_config, $vnet, $iface, $bridge);
+}
+
+sub veth_create {
+    my ($veth, $vethpeer, $bridge, $hwaddr) = @_;
+
+    my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge);
+
+    #fallback if classic bridge
+    if(!$vnet) {
+	PVE::Network::veth_create($veth, $vethpeer, $bridge, $hwaddr);
+        return;
+    }
+
+    my $zone_cfg = PVE::Network::SDN::Zones::config();
+    my $zoneid = $vnet->{zone};
+
+    my $plugin_config = $zone_cfg->{ids}->{$zoneid};
+    my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
+    $plugin->veth_create($plugin_config, $vnet, $veth, $vethpeer, $bridge, $hwaddr);
+}
+
+sub tap_plug {
+    my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_;
+
+    my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge);
+
+    #fallback if classic bridge
+    if(!$vnet) {
+	PVE::Network::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate);
+	return;
+    }
+
+    my $zone_cfg = PVE::Network::SDN::Zones::config();
+    my $nodename = PVE::INotify::nodename();
+
+    my $zoneid = $vnet->{zone};
+    $tag = $vnet->{tag};
+
+    die "vnet $bridge is not allowed on this node" if defined($zone_cfg->{ids}->{$zoneid}->{nodes}) && !$zone_cfg->{ids}->{$zoneid}->{nodes}->{$nodename};
 
     my $plugin_config = $zone_cfg->{ids}->{$zoneid};
     my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
-    return $plugin->get_bridge_vlan($plugin_config, $zoneid, $vnetid, $tag);
+    $plugin->tap_plug($plugin_config, $vnet, $iface, $bridge, $firewall, $rate);
 }
 
 1;
diff --git a/PVE/Network/SDN/Zones/Plugin.pm b/PVE/Network/SDN/Zones/Plugin.pm
index 13a6e57..e4adcbd 100644
--- a/PVE/Network/SDN/Zones/Plugin.pm
+++ b/PVE/Network/SDN/Zones/Plugin.pm
@@ -6,6 +6,7 @@ use warnings;
 use PVE::Tools qw(run_command);
 use PVE::JSONSchema;
 use PVE::Cluster;
+use PVE::Network;
 
 use Data::Dumper;
 use PVE::JSONSchema qw(get_standard_option);
@@ -208,17 +209,45 @@ sub status {
 
 
 sub get_bridge_vlan {
-    my ($class, $plugin_config, $zoneid, $vnetid, $tag) = @_;
+    my ($class, $plugin_config, $vnetid, $tag) = @_;
 
-    my $bridge = $plugin_config->{bridge};
-    die "bridge $bridge is missing" if !-d "/sys/class/net/$bridge/";
-
-    $bridge = $vnetid;
+    my $bridge = $vnetid;
     $tag = undef;
 
+    die "bridge $bridge is missing" if !-d "/sys/class/net/$bridge/";
+
     return ($bridge, $tag);
 }
 
+sub tap_create {
+    my ($class, $plugin_config, $vnet, $iface, $vnetid) = @_;
+
+    my ($bridge, undef) = $class->get_bridge_vlan($plugin_config, $vnetid);
+    die "unable to get bridge setting\n" if !$bridge;
+
+    PVE::Network::tap_create($iface, $bridge);
+}
+
+sub veth_create {
+    my ($class, $plugin_config, $vnet, $veth, $vethpeer, $vnetid, $hwaddr) = @_;
+
+    my ($bridge, undef) = $class->get_bridge_vlan($plugin_config, $vnetid);
+    die "unable to get bridge setting\n" if !$bridge;
+
+    PVE::Network::veth_create($veth, $vethpeer, $bridge, $hwaddr);
+}
+
+sub tap_plug {
+    my ($class, $plugin_config, $vnet, $iface, $vnetid, $firewall, $rate) = @_;
+
+    my $tag = $vnet->{tag};
+
+    ($vnetid, $tag) = $class->get_bridge_vlan($plugin_config, $vnetid, $tag);
+    my $trunks = undef;
+
+    PVE::Network::tap_plug($iface, $vnetid, $tag, $firewall, $trunks, $rate);
+}
+
 #helper
 
 sub get_uplink_iface {
diff --git a/PVE/Network/SDN/Zones/QinQPlugin.pm b/PVE/Network/SDN/Zones/QinQPlugin.pm
index 0557499..3f0697f 100644
--- a/PVE/Network/SDN/Zones/QinQPlugin.pm
+++ b/PVE/Network/SDN/Zones/QinQPlugin.pm
@@ -83,7 +83,7 @@ sub status {
 }
 
 sub get_bridge_vlan {
-    my ($class, $plugin_config, $zoneid, $vnetid, $tag) = @_;
+    my ($class, $plugin_config, $vnetid, $tag) = @_;
 
     my $bridge = $plugin_config->{bridge};
     die "bridge $bridge is missing" if !-d "/sys/class/net/$bridge/";
diff --git a/PVE/Network/SDN/Zones/VlanPlugin.pm b/PVE/Network/SDN/Zones/VlanPlugin.pm
index bc13e67..28d6a00 100644
--- a/PVE/Network/SDN/Zones/VlanPlugin.pm
+++ b/PVE/Network/SDN/Zones/VlanPlugin.pm
@@ -62,9 +62,10 @@ sub status {
 }
 
 sub get_bridge_vlan {
-    my ($class, $plugin_config, $zoneid, $vnetid, $tag) = @_;
+    my ($class, $plugin_config, $vnetid, $tag) = @_;
 
     my $bridge = $plugin_config->{bridge};
+
     die "bridge $bridge is missing" if !-d "/sys/class/net/$bridge/";
 
     my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
-- 
2.20.1




More information about the pve-devel mailing list