setup_tc_rate_limit : fix typo ingres -> ingress
[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 ingress >/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 # this does not work wit virtio - don't know why (setting "mtu 64kb" does not help)
20 #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");
21 # so we use avrate instead
22 run_command("/sbin/tc filter add dev $iface parent ffff: " .
23 "protocol ip prio 50 estimator 1sec 8sec " .
24 "u32 match ip src 0.0.0.0/0 police avrate ${rate}bps 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 sub tap_rate_limit {
42 my ($iface, $rate) = @_;
43
44 my $debug = 0;
45 $rate = int($rate*1024*1024);
46 my $burst = 1024*1024;
47
48 setup_tc_rate_limit($iface, $rate, $burst, $debug);
49 }
50
51 sub copy_bridge_config {
52 my ($br0, $br1) = @_;
53
54 return if $br0 eq $br1;
55
56 my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
57 'hello_time', 'max_age'];
58
59 foreach my $sysname (@$br_configs) {
60 eval {
61 my $v0 = PVE::Tools::file_read_firstline("/sys/class/net/$br0/bridge/$sysname");
62 my $v1 = PVE::Tools::file_read_firstline("/sys/class/net/$br1/bridge/$sysname");
63 if ($v0 ne $v1) {
64 system("echo \"$v0\" > /sys/class/net/$br1/bridge/$sysname") == 0 ||
65 warn "unable to set bridge config '$sysname'\n";
66 }
67 };
68 warn $@ if $@;
69 }
70 }
71
72 sub activate_bridge_vlan {
73 my ($bridge, $tag_param) = @_;
74
75 die "bridge '$bridge' is not active\n" if ! -d "/sys/class/net/$bridge";
76
77 return $bridge if !defined($tag_param); # no vlan, simply return
78
79 my $tag = int($tag_param);
80
81 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
82
83 my $bridgevlan = "${bridge}v$tag";
84
85 my $dir = "/sys/class/net/$bridge/brif";
86
87 #check if we have an only one ethX or bondX interface in the bridge
88
89 my $iface;
90 PVE::Tools::dir_glob_foreach($dir, '((eth|bond)\d+)', sub {
91 my ($slave) = @_;
92
93 die "more then one physical interfaces on bridge '$bridge'\n" if $iface;
94 $iface = $slave;
95
96 });
97
98 die "no physical interface on bridge '$bridge'\n" if !$iface;
99
100 my $ifacevlan = "${iface}.$tag";
101
102 # create vlan on $iface is not already exist
103 if (! -d "/sys/class/net/$ifacevlan") {
104 system("/sbin/vconfig add $iface $tag") == 0 ||
105 die "can't add vlan tag $tag to interface $iface\n";
106 }
107
108 # be sure to have the $ifacevlan up
109 system("/sbin/ip link set $ifacevlan up") == 0 ||
110 die "can't up interface $ifacevlan\n";
111
112 # test if $vlaniface is already enslaved in another bridge
113 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
114 if (-l $path) {
115 my $tbridge = basename(readlink($path));
116 if ($tbridge eq $bridgevlan) {
117 # already member of bridge - assume setup is already done
118 return $bridgevlan;
119 } else {
120 die "interface $ifacevlan already exist in bridge $tbridge\n";
121 }
122 }
123
124 # add bridgevlan if it doesn't already exist
125 if (! -d "/sys/class/net/$bridgevlan") {
126 system("/usr/sbin/brctl addbr $bridgevlan") == 0 ||
127 die "can't add bridge $bridgevlan\n";
128 }
129
130 #fixme: set other bridge flags
131
132 # be sure to have the bridge up
133 system("/sbin/ip link set $bridgevlan up") == 0 ||
134 die "can't up bridge $bridgevlan\n";
135
136 # add $ifacevlan to the bridge
137 system("/usr/sbin/brctl addif $bridgevlan $ifacevlan") == 0 ||
138 die "can't add interface $ifacevlan to bridge $bridgevlan\n";
139
140 return $bridgevlan;
141 }
142
143 1;