]> git.proxmox.com Git - pve-common.git/blobdiff - data/PVE/Network.pm
add openvswitch support to tap_plug / tap_unplug
[pve-common.git] / data / PVE / Network.pm
index a7e55d71d123ce4af44b164dda008cec6512f9a1..380b334af4dedac79f7e711e436d2473e016a3c5 100644 (file)
@@ -1,6 +1,7 @@
 package PVE::Network;
 
 use strict;
+use warnings;
 use PVE::Tools qw(run_command);
 use PVE::ProcFSTools;
 use PVE::INotify;
@@ -16,7 +17,7 @@ sub setup_tc_rate_limit {
     system("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1");
     system("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1");
 
-    return if (!$rate || ($rate && $rate == 0));
+    return if !$rate;
 
     run_command("/sbin/tc qdisc add dev $iface handle ffff: ingress");
 
@@ -67,11 +68,36 @@ sub tap_create {
 sub tap_plug {
     my ($iface, $bridge, $tag) = @_;
 
-    my $newbridge = activate_bridge_vlan($bridge, $tag);
-    copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge;
+    #cleanup old port config from any openvswitch bridge
+    eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) };
 
-    system ("/usr/sbin/brctl addif $newbridge $iface") == 0 ||
-       die "can't add interface to bridge\n";
+    if (-d "/sys/class/net/$bridge/bridge"){
+           my $newbridge = activate_bridge_vlan($bridge, $tag);
+           copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge;
+
+           system ("/sbin/brctl addif $newbridge $iface") == 0 ||
+               die "can't add interface to bridge\n";
+    }else{
+           my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface";
+           $cmd .= " tag=$tag" if $tag;
+           system ($cmd) == 0 ||
+               die "can't add interface to bridge\n";
+    }
+}
+
+sub tap_unplug {
+    my ($iface, $bridge, $tag) = @_;
+
+    if (-d "/sys/class/net/$bridge/bridge"){
+
+       $bridge .= "v$tag" if $tag;
+
+           system ("/sbin/brctl delif $bridge $iface") == 0 ||
+               die "can't del interface from bridge\n";
+    }else{
+           system ("/usr/bin/ovs-vsctl del-port $iface") == 0 ||
+               die "can't del interface from bridge\n";
+    }
 }
 
 sub copy_bridge_config {
@@ -80,7 +106,7 @@ sub copy_bridge_config {
     return if $br0 eq $br1;
 
     my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay', 
-                      'hello_time', 'max_age'];
+                      'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier'];
 
     foreach my $sysname (@$br_configs) {
        eval {
@@ -149,7 +175,7 @@ sub activate_bridge_vlan {
 
     # add bridgevlan if it doesn't already exist
     if (! -d "/sys/class/net/$bridgevlan") {
-        system("/usr/sbin/brctl addbr $bridgevlan") == 0 ||
+        system("/sbin/brctl addbr $bridgevlan") == 0 ||
             die "can't add bridge $bridgevlan\n";
     }
 
@@ -160,7 +186,7 @@ sub activate_bridge_vlan {
         die "can't up bridge $bridgevlan\n";
 
     # add $ifacevlan to the bridge
-    system("/usr/sbin/brctl addif $bridgevlan $ifacevlan") == 0 ||
+    system("/sbin/brctl addif $bridgevlan $ifacevlan") == 0 ||
        die "can't add interface $ifacevlan to bridge $bridgevlan\n";
     
     return $bridgevlan;