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