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