we need to match link+ rule from iptables rules, and need to have a name different
[pve-common.git] / data / PVE / Network.pm
1 package PVE::Network;
2
3 use strict;
4 use warnings;
5 use PVE::Tools qw(run_command);
6 use PVE::ProcFSTools;
7 use PVE::INotify;
8 use File::Basename;
9
10 # host network related utility functions
11
12 sub setup_tc_rate_limit {
13 my ($iface, $rate, $burst, $debug) = @_;
14
15 system("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1");
16 system("/sbin/tc filter del dev $iface parent ffff: protocol ip prio 50 estimator 1sec 8sec >/dev/null 2>&1");
17 system("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1");
18 system("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1");
19
20 return if !$rate;
21
22 run_command("/sbin/tc qdisc add dev $iface handle ffff: ingress");
23
24 # this does not work wit virtio - don't know why (setting "mtu 64kb" does not help)
25 #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");
26 # so we use avrate instead
27 run_command("/sbin/tc filter add dev $iface parent ffff: " .
28 "protocol ip prio 50 estimator 1sec 8sec " .
29 "u32 match ip src 0.0.0.0/0 police avrate ${rate}bps drop flowid :1");
30
31 # tbf does not work for unknown reason
32 #$TC qdisc add dev $DEV root tbf rate $RATE latency 100ms burst $BURST
33 # so we use htb instead
34 run_command("/sbin/tc qdisc add dev $iface root handle 1: htb default 1");
35 run_command("/sbin/tc class add dev $iface parent 1: classid 1:1 " .
36 "htb rate ${rate}bps burst ${burst}b");
37
38 if ($debug) {
39 print "DEBUG tc settings\n";
40 system("/sbin/tc qdisc ls dev $iface");
41 system("/sbin/tc class ls dev $iface");
42 system("/sbin/tc filter ls dev $iface parent ffff:");
43 }
44 }
45
46 sub tap_rate_limit {
47 my ($iface, $rate) = @_;
48
49 my $debug = 0;
50 $rate = int($rate*1024*1024);
51 my $burst = 1024*1024;
52
53 setup_tc_rate_limit($iface, $rate, $burst, $debug);
54 }
55
56 my $read_bridge_mtu = sub {
57 my ($bridge) = @_;
58
59 my $mtu = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/mtu");
60 die "bridge '$bridge' does not exist\n" if !$mtu;
61 # avoid insecure dependency;
62 die "unable to parse mtu value" if $mtu !~ /^(\d+)$/;
63 $mtu = int($1);
64
65 return $mtu;
66 };
67
68 my $parse_tap_devive_name = sub {
69 my ($iface) = @_;
70
71 my ($vmid, $devid);
72
73 if ($iface =~ m/^tap(\d+)i(\d+)$/) {
74 $vmid = $1;
75 $devid = $2;
76 } elsif ($iface =~ m/^veth(\d+)\.(\d+)$/) {
77 $vmid = $1;
78 $devid = $2;
79 } else {
80 die "wrong interface name $iface";
81 }
82
83 return ($vmid, $devid);
84 };
85
86 my $compute_fwbr_names = sub {
87 my ($vmid, $devid) = @_;
88
89 my $fwbr = "fwbr${vmid}i${devid}";
90 my $vethfw = "link${vmid}i${devid}";
91 my $vethfwpeer = "link${vmid}p${devid}";
92 my $ovsintport = "link${vmid}o${devid}";
93
94 return ($fwbr, $vethfw, $vethfwpeer, $ovsintport);
95 };
96
97 my $cond_create_bridge = sub {
98 my ($bridge) = @_;
99
100 if (! -d "/sys/class/net/$bridge") {
101 system("/sbin/brctl addbr $bridge") == 0 ||
102 die "can't add bridge '$bridge'\n";
103 }
104 };
105
106 my $bridge_add_interface = sub {
107 my ($bridge, $iface) = @_;
108
109 system("/sbin/brctl addif $bridge $iface") == 0 ||
110 die "can't add interface 'iface' to bridge '$bridge'\n";
111 };
112
113 my $ovs_bridge_add_port = sub {
114 my ($bridge, $iface, $tag, $internal) = @_;
115
116 my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface";
117 $cmd .= " tag=$tag" if $tag;
118 $cmd .= " -- set Interface $iface type=internal" if $internal;
119 system($cmd) == 0 ||
120 die "can't add ovs port '$iface'\n";
121 };
122
123 my $activate_interface = sub {
124 my ($iface) = @_;
125
126 system("/sbin/ip link set $iface up") == 0 ||
127 die "can't activate interface '$iface'\n";
128 };
129
130 sub tap_create {
131 my ($iface, $bridge) = @_;
132
133 die "unable to get bridge setting\n" if !$bridge;
134
135 my $bridgemtu = &$read_bridge_mtu($bridge);
136
137 eval {
138 PVE::Tools::run_command("/sbin/ifconfig $iface 0.0.0.0 promisc up mtu $bridgemtu");
139 };
140 die "interface activation failed\n" if $@;
141 }
142
143 my $create_firewall_bridge_linux = sub {
144 my ($iface, $bridge) = @_;
145
146 my ($vmid, $devid) = &$parse_tap_devive_name($iface);
147 my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid);
148
149 my $bridgemtu = &$read_bridge_mtu($bridge);
150
151 &$cond_create_bridge($fwbr);
152 &$activate_interface($fwbr);
153
154 copy_bridge_config($bridge, $fwbr);
155 # create veth pair
156 if (! -d "/sys/class/net/$vethfw") {
157 system("/sbin/ip link add name $vethfw type veth peer name $vethfwpeer mtu $bridgemtu") == 0 ||
158 die "can't create interface $vethfw\n";
159 }
160
161 # up vethpair
162 &$activate_interface($vethfw);
163 &$activate_interface($vethfwpeer);
164
165 &$bridge_add_interface($bridge, $vethfw);
166 &$bridge_add_interface($fwbr, $vethfwpeer);
167
168 return $fwbr;
169 };
170
171 my $create_firewall_bridge_ovs = sub {
172 my ($iface, $bridge, $tag) = @_;
173
174 my ($vmid, $devid) = &$parse_tap_devive_name($iface);
175 my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
176
177 my $bridgemtu = &$read_bridge_mtu($bridge);
178
179 &$cond_create_bridge($fwbr);
180 &$activate_interface($fwbr);
181
182 &$bridge_add_interface($fwbr, $iface);
183
184 &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1);
185
186 # set the same mtu for ovs int port
187 PVE::Tools::run_command("/sbin/ifconfig $ovsintport mtu $bridgemtu");
188
189 &$bridge_add_interface($fwbr, $ovsintport);
190 };
191
192 my $cleanup_firewall_bridge = sub {
193 my ($iface) = @_;
194
195 my ($vmid, $devid) = &$parse_tap_devive_name($iface);
196 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
197
198 # cleanup old port config from any openvswitch bridge
199 if (-d "/sys/class/net/$ovsintport") {
200 run_command("/usr/bin/ovs-vsctl del-port $ovsintport", outfunc => sub {}, errfunc => sub {});
201 }
202
203 # delete old vethfw interface
204 if (-d "/sys/class/net/$vethfw") {
205 run_command("/sbin/ip link delete dev $vethfw", outfunc => sub {}, errfunc => sub {});
206 }
207
208 # cleanup fwbr bridge
209 if (-d "/sys/class/net/$fwbr") {
210 run_command("/sbin/ip link set dev $fwbr down", outfunc => sub {}, errfunc => sub {});
211 run_command("/sbin/brctl delbr $fwbr", outfunc => sub {}, errfunc => sub {});
212 }
213 };
214
215 sub tap_plug {
216 my ($iface, $bridge, $tag, $firewall) = @_;
217
218 #cleanup old port config from any openvswitch bridge
219 eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) };
220
221 if (-d "/sys/class/net/$bridge/bridge") {
222 &$cleanup_firewall_bridge($iface); # remove stale devices
223
224 my $newbridge = activate_bridge_vlan($bridge, $tag);
225 copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge;
226
227 $newbridge = &$create_firewall_bridge_linux($iface, $newbridge) if $firewall;
228
229 &$bridge_add_interface($newbridge, $iface);
230 } else {
231 &$cleanup_firewall_bridge($iface); # remove stale devices
232
233 if ($firewall) {
234 &$create_firewall_bridge_ovs($iface, $bridge, $tag);
235 } else {
236 &$ovs_bridge_add_port($bridge, $iface, $tag);
237 }
238 }
239 }
240
241 sub tap_unplug {
242 my ($iface) = @_;
243
244 my $path= "/sys/class/net/$iface/brport/bridge";
245 if (-l $path) {
246 my $bridge = basename(readlink($path));
247 #avoid insecure dependency;
248 ($bridge) = $bridge =~ /(\S+)/;
249
250 system("/sbin/brctl delif $bridge $iface") == 0 ||
251 die "can't del interface '$iface' from bridge '$bridge'\n";
252
253 }
254
255 &$cleanup_firewall_bridge($iface);
256 }
257
258 sub copy_bridge_config {
259 my ($br0, $br1) = @_;
260
261 return if $br0 eq $br1;
262
263 my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
264 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier'];
265
266 foreach my $sysname (@$br_configs) {
267 eval {
268 my $v0 = PVE::Tools::file_read_firstline("/sys/class/net/$br0/bridge/$sysname");
269 my $v1 = PVE::Tools::file_read_firstline("/sys/class/net/$br1/bridge/$sysname");
270 if ($v0 ne $v1) {
271 PVE::ProcFSTools::write_proc_entry("/sys/class/net/$br1/bridge/$sysname", $v0);
272 }
273 };
274 warn $@ if $@;
275 }
276 }
277
278 sub activate_bridge_vlan_slave {
279 my ($bridgevlan, $iface, $tag) = @_;
280 my $ifacevlan = "${iface}.$tag";
281
282 # create vlan on $iface is not already exist
283 if (! -d "/sys/class/net/$ifacevlan") {
284 system("/sbin/vconfig add $iface $tag") == 0 ||
285 die "can't add vlan tag $tag to interface $iface\n";
286 }
287
288 # be sure to have the $ifacevlan up
289 &$activate_interface($ifacevlan);
290
291 # test if $vlaniface is already enslaved in another bridge
292 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
293 if (-l $path) {
294 my $tbridge = basename(readlink($path));
295 if ($tbridge ne $bridgevlan) {
296 die "interface $ifacevlan already exist in bridge $tbridge\n";
297 } else {
298 # Port already attached to bridge: do nothing.
299 return;
300 }
301 }
302
303 # add $ifacevlan to the bridge
304 &$bridge_add_interface($bridgevlan, $ifacevlan);
305 }
306
307 sub activate_bridge_vlan {
308 my ($bridge, $tag_param) = @_;
309
310 die "bridge '$bridge' is not active\n" if ! -d "/sys/class/net/$bridge";
311
312 return $bridge if !defined($tag_param); # no vlan, simply return
313
314 my $tag = int($tag_param);
315
316 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
317
318 my $bridgevlan = "${bridge}v$tag";
319
320 my @ifaces = ();
321 my $dir = "/sys/class/net/$bridge/brif";
322 PVE::Tools::dir_glob_foreach($dir, '((eth|bond)\d+)', sub {
323 push @ifaces, $_[0];
324 });
325
326 die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0;
327
328 # add bridgevlan if it doesn't already exist
329 if (! -d "/sys/class/net/$bridgevlan") {
330 system("/sbin/brctl addbr $bridgevlan") == 0 ||
331 die "can't add bridge $bridgevlan\n";
332 }
333
334 # for each physical interface (eth or bridge) bind them to bridge vlan
335 foreach my $iface (@ifaces) {
336 activate_bridge_vlan_slave($bridgevlan, $iface, $tag);
337 }
338
339 #fixme: set other bridge flags
340
341 # be sure to have the bridge up
342 system("/sbin/ip link set $bridgevlan up") == 0 ||
343 die "can't up bridge $bridgevlan\n";
344
345 return $bridgevlan;
346 }
347
348 1;