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