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