]> git.proxmox.com Git - pve-common.git/blame - src/PVE/Network.pm
Tools::run_with_timeout improvement + hires alarm
[pve-common.git] / src / PVE / Network.pm
CommitLineData
b9436cda
DM
1package PVE::Network;
2
3use strict;
c36f332e 4use warnings;
74d1b045 5use PVE::Tools qw(run_command);
b9436cda
DM
6use PVE::ProcFSTools;
7use PVE::INotify;
8use File::Basename;
9
10# host network related utility functions
11
61aa94e4
WB
12our $ipv4_reverse_mask = [
13 '0.0.0.0',
14 '128.0.0.0',
15 '192.0.0.0',
16 '224.0.0.0',
17 '240.0.0.0',
18 '248.0.0.0',
19 '252.0.0.0',
20 '254.0.0.0',
21 '255.0.0.0',
22 '255.128.0.0',
23 '255.192.0.0',
24 '255.224.0.0',
25 '255.240.0.0',
26 '255.248.0.0',
27 '255.252.0.0',
28 '255.254.0.0',
29 '255.255.0.0',
30 '255.255.128.0',
31 '255.255.192.0',
32 '255.255.224.0',
33 '255.255.240.0',
34 '255.255.248.0',
35 '255.255.252.0',
36 '255.255.254.0',
37 '255.255.255.0',
38 '255.255.255.128',
39 '255.255.255.192',
40 '255.255.255.224',
41 '255.255.255.240',
42 '255.255.255.248',
43 '255.255.255.252',
44 '255.255.255.254',
45 '255.255.255.255',
46];
47
48our $ipv4_mask_hash_localnet = {
49 '255.255.0.0' => 16,
50 '255.255.128.0' => 17,
51 '255.255.192.0' => 18,
52 '255.255.224.0' => 19,
53 '255.255.240.0' => 20,
54 '255.255.248.0' => 21,
55 '255.255.252.0' => 22,
56 '255.255.254.0' => 23,
57 '255.255.255.0' => 24,
58 '255.255.255.128' => 25,
59 '255.255.255.192' => 26,
60 '255.255.255.224' => 27,
61 '255.255.255.240' => 28,
62 '255.255.255.248' => 29,
63 '255.255.255.252' => 30,
64};
65
74d1b045
DM
66sub setup_tc_rate_limit {
67 my ($iface, $rate, $burst, $debug) = @_;
68
957753df 69 system("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1");
5d35df41 70 system("/sbin/tc filter del dev $iface parent ffff: protocol all pref 50 u32 >/dev/null 2>&1");
edde1d46 71 system("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1");
74d1b045
DM
72 system("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1");
73
d6f2623b 74 return if !$rate;
957753df 75
74d1b045
DM
76 # tbf does not work for unknown reason
77 #$TC qdisc add dev $DEV root tbf rate $RATE latency 100ms burst $BURST
78 # so we use htb instead
79 run_command("/sbin/tc qdisc add dev $iface root handle 1: htb default 1");
80 run_command("/sbin/tc class add dev $iface parent 1: classid 1:1 " .
81 "htb rate ${rate}bps burst ${burst}b");
82
5d35df41
W
83 run_command("/sbin/tc qdisc add dev $iface handle ffff: ingress");
84 run_command("/sbin/tc filter add dev $iface parent ffff: " .
85 "protocol all prio 50 u32 match u32 0 0 " .
86 "police rate ${rate}bps burst ${burst}b mtu 64kb " .
87 "drop flowid :1");
88
74d1b045
DM
89 if ($debug) {
90 print "DEBUG tc settings\n";
91 system("/sbin/tc qdisc ls dev $iface");
92 system("/sbin/tc class ls dev $iface");
93 system("/sbin/tc filter ls dev $iface parent ffff:");
94 }
95}
96
ec9ada18
AD
97sub tap_rate_limit {
98 my ($iface, $rate) = @_;
99
100 my $debug = 0;
101 $rate = int($rate*1024*1024);
102 my $burst = 1024*1024;
103
104 setup_tc_rate_limit($iface, $rate, $burst, $debug);
105}
74d1b045 106
605bb891
DM
107my $read_bridge_mtu = sub {
108 my ($bridge) = @_;
109
110 my $mtu = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/mtu");
111 die "bridge '$bridge' does not exist\n" if !$mtu;
112 # avoid insecure dependency;
113 die "unable to parse mtu value" if $mtu !~ /^(\d+)$/;
114 $mtu = int($1);
115
116 return $mtu;
117};
118
32cb7d27 119my $parse_tap_device_name = sub {
6c80e6d6 120 my ($iface, $noerr) = @_;
605bb891
DM
121
122 my ($vmid, $devid);
123
124 if ($iface =~ m/^tap(\d+)i(\d+)$/) {
125 $vmid = $1;
126 $devid = $2;
32cb7d27 127 } elsif ($iface =~ m/^veth(\d+)i(\d+)$/) {
605bb891
DM
128 $vmid = $1;
129 $devid = $2;
130 } else {
6c80e6d6
DM
131 return undef if $noerr;
132 die "can't create firewall bridge for random interface name '$iface'\n";
605bb891
DM
133 }
134
135 return ($vmid, $devid);
136};
137
70ab4434 138my $compute_fwbr_names = sub {
605bb891
DM
139 my ($vmid, $devid) = @_;
140
141 my $fwbr = "fwbr${vmid}i${devid}";
f193aa74 142 # Note: the firewall use 'fwln+' to filter traffic to VMs
7d78a966
AD
143 my $vethfw = "fwln${vmid}i${devid}";
144 my $vethfwpeer = "fwpr${vmid}p${devid}";
145 my $ovsintport = "fwln${vmid}o${devid}";
605bb891 146
70ab4434 147 return ($fwbr, $vethfw, $vethfwpeer, $ovsintport);
605bb891
DM
148};
149
150my $cond_create_bridge = sub {
151 my ($bridge) = @_;
152
153 if (! -d "/sys/class/net/$bridge") {
154 system("/sbin/brctl addbr $bridge") == 0 ||
155 die "can't add bridge '$bridge'\n";
156 }
157};
158
159my $bridge_add_interface = sub {
4d25f4aa 160 my ($bridge, $iface, $tag) = @_;
605bb891
DM
161
162 system("/sbin/brctl addif $bridge $iface") == 0 ||
163 die "can't add interface 'iface' to bridge '$bridge'\n";
4d25f4aa
AD
164
165 my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
166
167 if ($vlan_aware) {
168 if ($tag) {
169 system("/sbin/bridge vlan add dev $iface vid $tag pvid untagged") == 0 ||
170 die "unable to add vlan $tag to interface $iface\n";
171 } else {
172 system("/sbin/bridge vlan add dev $iface vid 2-4094") == 0 ||
173 die "unable to add vlan $tag to interface $iface\n";
174 }
175 }
605bb891
DM
176};
177
70ab4434
DM
178my $ovs_bridge_add_port = sub {
179 my ($bridge, $iface, $tag, $internal) = @_;
180
181 my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface";
182 $cmd .= " tag=$tag" if $tag;
183 $cmd .= " -- set Interface $iface type=internal" if $internal;
184 system($cmd) == 0 ||
185 die "can't add ovs port '$iface'\n";
186};
187
605bb891
DM
188my $activate_interface = sub {
189 my ($iface) = @_;
190
191 system("/sbin/ip link set $iface up") == 0 ||
192 die "can't activate interface '$iface'\n";
193};
194
3aa99c70
AD
195sub tap_create {
196 my ($iface, $bridge) = @_;
197
198 die "unable to get bridge setting\n" if !$bridge;
199
605bb891 200 my $bridgemtu = &$read_bridge_mtu($bridge);
3aa99c70 201
098795e0
DM
202 eval {
203 PVE::Tools::run_command("/sbin/ifconfig $iface 0.0.0.0 promisc up mtu $bridgemtu");
204 };
205 die "interface activation failed\n" if $@;
3aa99c70
AD
206}
207
35efc4eb
AD
208sub veth_create {
209 my ($veth, $vethpeer, $bridge, $mac) = @_;
210
211 die "unable to get bridge setting\n" if !$bridge;
212
213 my $bridgemtu = &$read_bridge_mtu($bridge);
214
215 # create veth pair
216 if (! -d "/sys/class/net/$veth") {
217 my $cmd = "/sbin/ip link add name $veth type veth peer name $vethpeer mtu $bridgemtu";
218 $cmd .= " addr $mac" if $mac;
219 system($cmd) == 0 || die "can't create interface $veth\n";
220 }
221
222 # up vethpair
223 &$activate_interface($veth);
224 &$activate_interface($vethpeer);
225}
226
f3f0bc3a
AD
227sub veth_delete {
228 my ($veth) = @_;
229
230 if (-d "/sys/class/net/$veth") {
231 run_command("/sbin/ip link delete dev $veth", outfunc => sub {}, errfunc => sub {});
232 }
233
234}
35efc4eb 235
605bb891 236my $create_firewall_bridge_linux = sub {
4d25f4aa 237 my ($iface, $bridge, $tag) = @_;
605bb891 238
32cb7d27 239 my ($vmid, $devid) = &$parse_tap_device_name($iface);
70ab4434 240 my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid);
605bb891 241
605bb891
DM
242 &$cond_create_bridge($fwbr);
243 &$activate_interface($fwbr);
244
245 copy_bridge_config($bridge, $fwbr);
35efc4eb 246 veth_create($vethfw, $vethfwpeer, $bridge);
605bb891 247
7d78a966 248 &$bridge_add_interface($fwbr, $vethfw);
4d25f4aa 249 &$bridge_add_interface($bridge, $vethfwpeer, $tag);
605bb891 250
4d25f4aa 251 &$bridge_add_interface($fwbr, $iface);
605bb891
DM
252};
253
70ab4434
DM
254my $create_firewall_bridge_ovs = sub {
255 my ($iface, $bridge, $tag) = @_;
256
32cb7d27 257 my ($vmid, $devid) = &$parse_tap_device_name($iface);
70ab4434
DM
258 my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
259
260 my $bridgemtu = &$read_bridge_mtu($bridge);
261
262 &$cond_create_bridge($fwbr);
263 &$activate_interface($fwbr);
264
265 &$bridge_add_interface($fwbr, $iface);
266
267 &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1);
ac3a04b8 268 &$activate_interface($ovsintport);
70ab4434
DM
269
270 # set the same mtu for ovs int port
271 PVE::Tools::run_command("/sbin/ifconfig $ovsintport mtu $bridgemtu");
272
273 &$bridge_add_interface($fwbr, $ovsintport);
274};
275
276my $cleanup_firewall_bridge = sub {
605bb891
DM
277 my ($iface) = @_;
278
32cb7d27 279 my ($vmid, $devid) = &$parse_tap_device_name($iface, 1);
6c80e6d6 280 return if !defined($vmid);
70ab4434
DM
281 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
282
283 # cleanup old port config from any openvswitch bridge
284 if (-d "/sys/class/net/$ovsintport") {
285 run_command("/usr/bin/ovs-vsctl del-port $ovsintport", outfunc => sub {}, errfunc => sub {});
286 }
605bb891
DM
287
288 # delete old vethfw interface
f3f0bc3a 289 veth_delete($vethfw);
605bb891
DM
290
291 # cleanup fwbr bridge
292 if (-d "/sys/class/net/$fwbr") {
293 run_command("/sbin/ip link set dev $fwbr down", outfunc => sub {}, errfunc => sub {});
294 run_command("/sbin/brctl delbr $fwbr", outfunc => sub {}, errfunc => sub {});
295 }
296};
297
f0c190ee 298sub tap_plug {
605bb891 299 my ($iface, $bridge, $tag, $firewall) = @_;
f0c190ee 300
4cbabd40
AD
301 #cleanup old port config from any openvswitch bridge
302 eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) };
303
098795e0 304 if (-d "/sys/class/net/$bridge/bridge") {
70ab4434 305 &$cleanup_firewall_bridge($iface); # remove stale devices
605bb891 306
4d25f4aa 307 my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
098795e0 308
4d25f4aa
AD
309 if (!$vlan_aware) {
310 my $newbridge = activate_bridge_vlan($bridge, $tag);
311 copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge;
ff042056 312 $bridge = $newbridge;
4d25f4aa
AD
313 $tag = undef;
314 }
315
316 if ($firewall) {
317 &$create_firewall_bridge_linux($iface, $bridge, $tag);
318 } else {
319 &$bridge_add_interface($bridge, $iface, $tag);
320 }
605bb891 321
098795e0 322 } else {
70ab4434
DM
323 &$cleanup_firewall_bridge($iface); # remove stale devices
324
325 if ($firewall) {
326 &$create_firewall_bridge_ovs($iface, $bridge, $tag);
327 } else {
328 &$ovs_bridge_add_port($bridge, $iface, $tag);
329 }
4cbabd40 330 }
f0c190ee
AD
331}
332
a84b65c0 333sub tap_unplug {
2db1cc0d 334 my ($iface) = @_;
a84b65c0 335
2db1cc0d
DM
336 my $path= "/sys/class/net/$iface/brport/bridge";
337 if (-l $path) {
338 my $bridge = basename(readlink($path));
339 #avoid insecure dependency;
340 ($bridge) = $bridge =~ /(\S+)/;
4cbabd40 341
098795e0 342 system("/sbin/brctl delif $bridge $iface") == 0 ||
2db1cc0d 343 die "can't del interface '$iface' from bridge '$bridge'\n";
605bb891 344
4cbabd40 345 }
70ab4434
DM
346
347 &$cleanup_firewall_bridge($iface);
a84b65c0
AD
348}
349
b9436cda
DM
350sub copy_bridge_config {
351 my ($br0, $br1) = @_;
352
353 return if $br0 eq $br1;
354
355 my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
ba4af65b 356 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier'];
b9436cda
DM
357
358 foreach my $sysname (@$br_configs) {
359 eval {
360 my $v0 = PVE::Tools::file_read_firstline("/sys/class/net/$br0/bridge/$sysname");
361 my $v1 = PVE::Tools::file_read_firstline("/sys/class/net/$br1/bridge/$sysname");
362 if ($v0 ne $v1) {
aec04803 363 PVE::ProcFSTools::write_proc_entry("/sys/class/net/$br1/bridge/$sysname", $v0);
b9436cda
DM
364 }
365 };
366 warn $@ if $@;
367 }
368}
369
70d89745
PRG
370sub activate_bridge_vlan_slave {
371 my ($bridgevlan, $iface, $tag) = @_;
b9436cda 372 my $ifacevlan = "${iface}.$tag";
70d89745 373
b9436cda
DM
374 # create vlan on $iface is not already exist
375 if (! -d "/sys/class/net/$ifacevlan") {
6fc54cb2 376 system("/sbin/ip link add link $iface name ${iface}.${tag} type vlan id $tag") == 0 ||
02c9a6b4 377 die "can't add vlan tag $tag to interface $iface\n";
b9436cda
DM
378 }
379
380 # be sure to have the $ifacevlan up
605bb891 381 &$activate_interface($ifacevlan);
b9436cda
DM
382
383 # test if $vlaniface is already enslaved in another bridge
384 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
385 if (-l $path) {
386 my $tbridge = basename(readlink($path));
70d89745 387 if ($tbridge ne $bridgevlan) {
b9436cda 388 die "interface $ifacevlan already exist in bridge $tbridge\n";
eee4b32a
PRG
389 } else {
390 # Port already attached to bridge: do nothing.
391 return;
b9436cda
DM
392 }
393 }
394
70d89745 395 # add $ifacevlan to the bridge
605bb891 396 &$bridge_add_interface($bridgevlan, $ifacevlan);
70d89745
PRG
397}
398
399sub activate_bridge_vlan {
400 my ($bridge, $tag_param) = @_;
401
402 die "bridge '$bridge' is not active\n" if ! -d "/sys/class/net/$bridge";
403
404 return $bridge if !defined($tag_param); # no vlan, simply return
405
406 my $tag = int($tag_param);
407
408 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
409
410 my $bridgevlan = "${bridge}v$tag";
411
c9030d97
PRG
412 my @ifaces = ();
413 my $dir = "/sys/class/net/$bridge/brif";
414 PVE::Tools::dir_glob_foreach($dir, '((eth|bond)\d+)', sub {
5ffa7628 415 push @ifaces, $_[0];
c9030d97
PRG
416 });
417
5ffa7628 418 die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0;
c9030d97 419
b9436cda
DM
420 # add bridgevlan if it doesn't already exist
421 if (! -d "/sys/class/net/$bridgevlan") {
9e14b1b7 422 system("/sbin/brctl addbr $bridgevlan") == 0 ||
b9436cda
DM
423 die "can't add bridge $bridgevlan\n";
424 }
425
70d89745 426 # for each physical interface (eth or bridge) bind them to bridge vlan
c9030d97
PRG
427 foreach my $iface (@ifaces) {
428 activate_bridge_vlan_slave($bridgevlan, $iface, $tag);
429 }
70d89745 430
b9436cda
DM
431 #fixme: set other bridge flags
432
433 # be sure to have the bridge up
434 system("/sbin/ip link set $bridgevlan up") == 0 ||
435 die "can't up bridge $bridgevlan\n";
70d89745 436
b9436cda
DM
437 return $bridgevlan;
438}
439
4401;