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