]> 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 4efd30b10835e8d64cc152fcb4065b54d574f9c2..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;
@@ -11,9 +12,13 @@ use File::Basename;
 sub setup_tc_rate_limit {
     my ($iface, $rate, $burst, $debug) = @_;
 
+    system("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1");
+    system("/sbin/tc filter del dev $iface parent ffff: protocol ip prio 50 estimator 1sec 8sec >/dev/null 2>&1");
     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;
+
     run_command("/sbin/tc qdisc add dev $iface handle ffff: ingress");
 
     # this does not work wit virtio - don't know why (setting "mtu 64kb" does not help)
@@ -48,13 +53,60 @@ sub tap_rate_limit {
     setup_tc_rate_limit($iface, $rate, $burst, $debug);
 }
 
+sub tap_create {
+    my ($iface, $bridge) = @_;
+
+    die "unable to get bridge setting\n" if !$bridge;
+
+    my $bridgemtu = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/mtu");
+       die "bridge '$bridge' does not exist\n" if !$bridgemtu;
+
+    eval{ PVE::Tools::run_command("/sbin/ifconfig $iface 0.0.0.0 promisc up mtu $bridgemtu");};
+       die "interface activation failed\n" if $@;
+}
+
+sub tap_plug {
+    my ($iface, $bridge, $tag) = @_;
+
+    #cleanup old port config from any openvswitch bridge
+    eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) };
+
+    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 {
     my ($br0, $br1) = @_;
 
     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 {
@@ -123,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";
     }
 
@@ -134,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;