setup_tc_rate_limit : delete class and filter before update
[pve-common.git] / data / PVE / Network.pm
1 package PVE::Network;
2
3 use strict;
4 use PVE::Tools qw(run_command);
5 use PVE::ProcFSTools;
6 use PVE::INotify;
7 use File::Basename;
8
9 # host network related utility functions
10
11 sub setup_tc_rate_limit {
12 my ($iface, $rate, $burst, $debug) = @_;
13
14 system("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1");
15 system("/sbin/tc filter del dev $iface parent ffff: protocol ip prio 50 estimator 1sec 8sec >/dev/null 2>&1");
16 system("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1");
17 system("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1");
18
19 return if (!$rate || ($rate && $rate == 0));
20
21 run_command("/sbin/tc qdisc add dev $iface handle ffff: ingress");
22
23 # this does not work wit virtio - don't know why (setting "mtu 64kb" does not help)
24 #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");
25 # so we use avrate instead
26 run_command("/sbin/tc filter add dev $iface parent ffff: " .
27 "protocol ip prio 50 estimator 1sec 8sec " .
28 "u32 match ip src 0.0.0.0/0 police avrate ${rate}bps drop flowid :1");
29
30 # tbf does not work for unknown reason
31 #$TC qdisc add dev $DEV root tbf rate $RATE latency 100ms burst $BURST
32 # so we use htb instead
33 run_command("/sbin/tc qdisc add dev $iface root handle 1: htb default 1");
34 run_command("/sbin/tc class add dev $iface parent 1: classid 1:1 " .
35 "htb rate ${rate}bps burst ${burst}b");
36
37 if ($debug) {
38 print "DEBUG tc settings\n";
39 system("/sbin/tc qdisc ls dev $iface");
40 system("/sbin/tc class ls dev $iface");
41 system("/sbin/tc filter ls dev $iface parent ffff:");
42 }
43 }
44
45 sub tap_rate_limit {
46 my ($iface, $rate) = @_;
47
48 my $debug = 0;
49 $rate = int($rate*1024*1024);
50 my $burst = 1024*1024;
51
52 setup_tc_rate_limit($iface, $rate, $burst, $debug);
53 }
54
55 sub copy_bridge_config {
56 my ($br0, $br1) = @_;
57
58 return if $br0 eq $br1;
59
60 my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
61 'hello_time', 'max_age'];
62
63 foreach my $sysname (@$br_configs) {
64 eval {
65 my $v0 = PVE::Tools::file_read_firstline("/sys/class/net/$br0/bridge/$sysname");
66 my $v1 = PVE::Tools::file_read_firstline("/sys/class/net/$br1/bridge/$sysname");
67 if ($v0 ne $v1) {
68 system("echo \"$v0\" > /sys/class/net/$br1/bridge/$sysname") == 0 ||
69 warn "unable to set bridge config '$sysname'\n";
70 }
71 };
72 warn $@ if $@;
73 }
74 }
75
76 sub activate_bridge_vlan {
77 my ($bridge, $tag_param) = @_;
78
79 die "bridge '$bridge' is not active\n" if ! -d "/sys/class/net/$bridge";
80
81 return $bridge if !defined($tag_param); # no vlan, simply return
82
83 my $tag = int($tag_param);
84
85 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
86
87 my $bridgevlan = "${bridge}v$tag";
88
89 my $dir = "/sys/class/net/$bridge/brif";
90
91 #check if we have an only one ethX or bondX interface in the bridge
92
93 my $iface;
94 PVE::Tools::dir_glob_foreach($dir, '((eth|bond)\d+)', sub {
95 my ($slave) = @_;
96
97 die "more then one physical interfaces on bridge '$bridge'\n" if $iface;
98 $iface = $slave;
99
100 });
101
102 die "no physical interface on bridge '$bridge'\n" if !$iface;
103
104 my $ifacevlan = "${iface}.$tag";
105
106 # create vlan on $iface is not already exist
107 if (! -d "/sys/class/net/$ifacevlan") {
108 system("/sbin/vconfig add $iface $tag") == 0 ||
109 die "can't add vlan tag $tag to interface $iface\n";
110 }
111
112 # be sure to have the $ifacevlan up
113 system("/sbin/ip link set $ifacevlan up") == 0 ||
114 die "can't up interface $ifacevlan\n";
115
116 # test if $vlaniface is already enslaved in another bridge
117 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
118 if (-l $path) {
119 my $tbridge = basename(readlink($path));
120 if ($tbridge eq $bridgevlan) {
121 # already member of bridge - assume setup is already done
122 return $bridgevlan;
123 } else {
124 die "interface $ifacevlan already exist in bridge $tbridge\n";
125 }
126 }
127
128 # add bridgevlan if it doesn't already exist
129 if (! -d "/sys/class/net/$bridgevlan") {
130 system("/usr/sbin/brctl addbr $bridgevlan") == 0 ||
131 die "can't add bridge $bridgevlan\n";
132 }
133
134 #fixme: set other bridge flags
135
136 # be sure to have the bridge up
137 system("/sbin/ip link set $bridgevlan up") == 0 ||
138 die "can't up bridge $bridgevlan\n";
139
140 # add $ifacevlan to the bridge
141 system("/usr/sbin/brctl addif $bridgevlan $ifacevlan") == 0 ||
142 die "can't add interface $ifacevlan to bridge $bridgevlan\n";
143
144 return $bridgevlan;
145 }
146
147 1;