X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=blobdiff_plain;f=data%2FPVE%2FNetwork.pm;h=e7521f7531c0b8edc65d39b7ef39bda703229653;hp=4181450baba9522f5fa498134272b35c9e8b51db;hb=ba4af65b3550c91782e2c1471f345d578aa4366b;hpb=b9436cda61380b2e37370065cd6b3e790225b132 diff --git a/data/PVE/Network.pm b/data/PVE/Network.pm index 4181450..e7521f7 100644 --- a/data/PVE/Network.pm +++ b/data/PVE/Network.pm @@ -1,20 +1,95 @@ package PVE::Network; use strict; -use PVE::Tools; +use PVE::Tools qw(run_command); use PVE::ProcFSTools; use PVE::INotify; use File::Basename; # host network related utility functions +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) + #run_command("/sbin/tc filter add dev $iface parent ffff: protocol ip prio 50 u32 match ip src 0.0.0.0/0 police rate ${rate}bps burst ${burst}b drop flowid :1"); + # so we use avrate instead + run_command("/sbin/tc filter add dev $iface parent ffff: " . + "protocol ip prio 50 estimator 1sec 8sec " . + "u32 match ip src 0.0.0.0/0 police avrate ${rate}bps drop flowid :1"); + + # tbf does not work for unknown reason + #$TC qdisc add dev $DEV root tbf rate $RATE latency 100ms burst $BURST + # so we use htb instead + run_command("/sbin/tc qdisc add dev $iface root handle 1: htb default 1"); + run_command("/sbin/tc class add dev $iface parent 1: classid 1:1 " . + "htb rate ${rate}bps burst ${burst}b"); + + if ($debug) { + print "DEBUG tc settings\n"; + system("/sbin/tc qdisc ls dev $iface"); + system("/sbin/tc class ls dev $iface"); + system("/sbin/tc filter ls dev $iface parent ffff:"); + } +} + +sub tap_rate_limit { + my ($iface, $rate) = @_; + + my $debug = 0; + $rate = int($rate*1024*1024); + my $burst = 1024*1024; + + 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) = @_; + + my $newbridge = activate_bridge_vlan($bridge, $tag); + copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge; + + system ("/usr/sbin/brctl addif $newbridge $iface") == 0 || + die "can't add interface to bridge\n"; +} + +sub tap_unplug { + my ($iface, $bridge, $tag) = @_; + + $bridge .= "v$tag" if $tag; + + system ("/usr/sbin/brctl delif $bridge $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 {