5 use PVE
::Tools
qw(run_command lock_file);
10 use POSIX
qw(ECONNREFUSED);
14 # host network related utility functions
16 our $ipv4_reverse_mask = [
52 our $ipv4_mask_hash_localnet = {
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,
80 sub setup_tc_rate_limit
{
81 my ($iface, $rate, $burst, $debug) = @_;
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"); };
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");
98 run_command
("/sbin/tc qdisc add dev $iface handle ffff: ingress");
99 run_command
("/sbin/tc filter add dev $iface parent ffff: " .
101 "police rate ${rate}bps burst ${burst}b mtu 64kb " .
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:");
113 my ($iface, $rate) = @_;
116 $rate = int($rate*1024*1024) if $rate;
117 my $burst = 1024*1024;
119 setup_tc_rate_limit
($iface, $rate, $burst, $debug);
122 my $read_bridge_mtu = sub {
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+)$/;
134 my $parse_tap_device_name = sub {
135 my ($iface, $noerr) = @_;
139 if ($iface =~ m/^tap(\d+)i(\d+)$/) {
142 } elsif ($iface =~ m/^veth(\d+)i(\d+)$/) {
146 return undef if $noerr;
147 die "can't create firewall bridge for random interface name '$iface'\n";
150 return ($vmid, $devid);
153 my $compute_fwbr_names = sub {
154 my ($vmid, $devid) = @_;
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}";
162 return ($fwbr, $vethfw, $vethfwpeer, $ovsintport);
165 my $cond_create_bridge = sub {
168 if (! -d
"/sys/class/net/$bridge") {
169 system("/sbin/brctl addbr $bridge") == 0 ||
170 die "can't add bridge '$bridge'\n";
171 disable_ipv6
($bridge);
177 return if !-d
'/proc/sys/net/ipv6'; # ipv6 might be completely disabled
178 my $file = "/proc/sys/net/ipv6/conf/$iface/disable_ipv6";
179 open(my $fh, '>', $file) or die "failed to open $file for writing: $!\n";
180 print {$fh} "1\n" or die "failed to disable link-local ipv6 for $iface\n";
184 my $bridge_add_interface = sub {
185 my ($bridge, $iface, $tag, $trunks) = @_;
187 # drop link local address (it can't be used when on a bridge anyway)
188 disable_ipv6
($iface);
189 system("/sbin/brctl addif $bridge $iface") == 0 ||
190 die "can't add interface 'iface' to bridge '$bridge'\n";
192 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/bridge/vlan_filtering");
196 system({'/sbin/bridge'} 'bridge', 'vlan', 'del', 'dev', $iface, 'vid', '1-4094') == 0
197 or die "failed to remove default vlan tags of $iface\n";
198 system({'/sbin/bridge'} 'bridge', 'vlan', 'add', 'dev', $iface, 'vid', $tag, 'pvid', 'untagged') == 0
199 or die "unable to add vlan $tag to interface $iface\n";
201 warn "Caution: Setting VLAN ID 1 on a VLAN aware bridge may be dangerous\n" if $tag == 1;
203 system("/sbin/bridge vlan add dev $iface vid 2-4094") == 0 ||
204 die "unable to add default vlan tags to interface $iface\n" if !$trunks;
208 my @trunks_array = split /;/, $trunks;
209 foreach my $trunk (@trunks_array) {
210 system("/sbin/bridge vlan add dev $iface vid $trunk") == 0 ||
211 die "unable to add vlan $trunk to interface $iface\n";
217 my $ovs_bridge_add_port = sub {
218 my ($bridge, $iface, $tag, $internal, $trunks) = @_;
220 $trunks =~ s/;/,/g if $trunks;
222 my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface";
223 $cmd .= " tag=$tag" if $tag;
224 $cmd .= " trunks=". join(',', $trunks) if $trunks;
225 $cmd .= " vlan_mode=native-untagged" if $tag && $trunks;
227 $cmd .= " -- set Interface $iface type=internal" if $internal;
229 die "can't add ovs port '$iface'\n";
230 disable_ipv6
($iface);
233 my $activate_interface = sub {
236 system("/sbin/ip link set $iface up") == 0 ||
237 die "can't activate interface '$iface'\n";
241 my ($iface, $bridge) = @_;
243 die "unable to get bridge setting\n" if !$bridge;
245 my $bridgemtu = &$read_bridge_mtu($bridge);
248 disable_ipv6
($iface);
249 PVE
::Tools
::run_command
(['/sbin/ip', 'link', 'set', $iface, 'up', 'promisc', 'on', 'mtu', $bridgemtu]);
251 die "interface activation failed\n" if $@;
255 my ($veth, $vethpeer, $bridge, $mac) = @_;
257 die "unable to get bridge setting\n" if !$bridge;
259 my $bridgemtu = &$read_bridge_mtu($bridge);
262 if (! -d
"/sys/class/net/$veth") {
263 my $cmd = "/sbin/ip link add name $veth type veth peer name $vethpeer mtu $bridgemtu";
264 $cmd .= " addr $mac" if $mac;
265 system($cmd) == 0 || die "can't create interface $veth\n";
270 disable_ipv6
($vethpeer);
271 &$activate_interface($veth);
272 &$activate_interface($vethpeer);
278 if (-d
"/sys/class/net/$veth") {
279 run_command
("/sbin/ip link delete dev $veth", outfunc
=> sub {}, errfunc
=> sub {});
281 eval { tap_unplug
($veth) };
284 my $create_firewall_bridge_linux = sub {
285 my ($iface, $bridge, $tag, $trunks) = @_;
287 my ($vmid, $devid) = &$parse_tap_device_name($iface);
288 my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid);
290 &$cond_create_bridge($fwbr);
291 &$activate_interface($fwbr);
293 copy_bridge_config
($bridge, $fwbr);
294 veth_create
($vethfw, $vethfwpeer, $bridge);
296 &$bridge_add_interface($fwbr, $vethfw);
297 &$bridge_add_interface($bridge, $vethfwpeer, $tag, $trunks);
299 &$bridge_add_interface($fwbr, $iface);
302 my $create_firewall_bridge_ovs = sub {
303 my ($iface, $bridge, $tag, $trunks) = @_;
305 my ($vmid, $devid) = &$parse_tap_device_name($iface);
306 my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
308 my $bridgemtu = &$read_bridge_mtu($bridge);
310 &$cond_create_bridge($fwbr);
311 &$activate_interface($fwbr);
313 &$bridge_add_interface($fwbr, $iface);
315 &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1, $trunks);
316 &$activate_interface($ovsintport);
318 # set the same mtu for ovs int port
319 PVE
::Tools
::run_command
(['/sbin/ip', 'link', 'set', $ovsintport, 'mtu', $bridgemtu]);
321 &$bridge_add_interface($fwbr, $ovsintport);
324 my $cleanup_firewall_bridge = sub {
327 my ($vmid, $devid) = &$parse_tap_device_name($iface, 1);
328 return if !defined($vmid);
329 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
331 # cleanup old port config from any openvswitch bridge
332 if (-d
"/sys/class/net/$ovsintport") {
333 run_command
("/usr/bin/ovs-vsctl del-port $ovsintport", outfunc
=> sub {}, errfunc
=> sub {});
336 # delete old vethfw interface
337 veth_delete
($vethfw);
339 # cleanup fwbr bridge
340 if (-d
"/sys/class/net/$fwbr") {
341 run_command
("/sbin/ip link set dev $fwbr down", outfunc
=> sub {}, errfunc
=> sub {});
342 run_command
("/sbin/brctl delbr $fwbr", outfunc
=> sub {}, errfunc
=> sub {});
347 my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_;
349 #cleanup old port config from any openvswitch bridge
350 eval {run_command
("/usr/bin/ovs-vsctl del-port $iface", outfunc
=> sub {}, errfunc
=> sub {}) };
352 if (-d
"/sys/class/net/$bridge/bridge") {
353 &$cleanup_firewall_bridge($iface); # remove stale devices
355 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/bridge/vlan_filtering");
358 die "vlan aware feature need to be enabled to use trunks" if $trunks;
359 my $newbridge = activate_bridge_vlan
($bridge, $tag);
360 copy_bridge_config
($bridge, $newbridge) if $bridge ne $newbridge;
361 $bridge = $newbridge;
366 &$create_firewall_bridge_linux($iface, $bridge, $tag, $trunks);
368 &$bridge_add_interface($bridge, $iface, $tag, $trunks);
372 &$cleanup_firewall_bridge($iface); # remove stale devices
375 &$create_firewall_bridge_ovs($iface, $bridge, $tag, $trunks);
377 &$ovs_bridge_add_port($bridge, $iface, $tag, undef, $trunks);
381 tap_rate_limit
($iface, $rate);
387 my $path= "/sys/class/net/$iface/brport/bridge";
389 my $bridge = basename
(readlink($path));
390 #avoid insecure dependency;
391 ($bridge) = $bridge =~ /(\S+)/;
393 system("/sbin/brctl delif $bridge $iface") == 0 ||
394 die "can't del interface '$iface' from bridge '$bridge'\n";
398 &$cleanup_firewall_bridge($iface);
399 #cleanup old port config from any openvswitch bridge
400 eval {run_command
("/usr/bin/ovs-vsctl del-port $iface", outfunc
=> sub {}, errfunc
=> sub {}) };
403 sub copy_bridge_config
{
404 my ($br0, $br1) = @_;
406 return if $br0 eq $br1;
408 my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
409 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier'];
411 foreach my $sysname (@$br_configs) {
413 my $v0 = PVE
::Tools
::file_read_firstline
("/sys/class/net/$br0/bridge/$sysname");
414 my $v1 = PVE
::Tools
::file_read_firstline
("/sys/class/net/$br1/bridge/$sysname");
416 PVE
::ProcFSTools
::write_proc_entry
("/sys/class/net/$br1/bridge/$sysname", $v0);
423 sub activate_bridge_vlan_slave
{
424 my ($bridgevlan, $iface, $tag) = @_;
425 my $ifacevlan = "${iface}.$tag";
427 # create vlan on $iface is not already exist
428 if (! -d
"/sys/class/net/$ifacevlan") {
429 system("/sbin/ip link add link $iface name $ifacevlan type vlan id $tag") == 0 ||
430 die "can't add vlan tag $tag to interface $iface\n";
432 # remove ipv6 link-local address before activation
433 disable_ipv6
($ifacevlan);
436 # be sure to have the $ifacevlan up
437 &$activate_interface($ifacevlan);
439 # test if $vlaniface is already enslaved in another bridge
440 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
442 my $tbridge = basename
(readlink($path));
443 if ($tbridge ne $bridgevlan) {
444 die "interface $ifacevlan already exist in bridge $tbridge\n";
446 # Port already attached to bridge: do nothing.
451 # add $ifacevlan to the bridge
452 &$bridge_add_interface($bridgevlan, $ifacevlan);
455 sub activate_bridge_vlan
{
456 my ($bridge, $tag_param) = @_;
458 die "bridge '$bridge' is not active\n" if ! -d
"/sys/class/net/$bridge";
460 return $bridge if !defined($tag_param); # no vlan, simply return
462 my $tag = int($tag_param);
464 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
466 my $bridgevlan = "${bridge}v$tag";
469 my $dir = "/sys/class/net/$bridge/brif";
470 PVE
::Tools
::dir_glob_foreach
($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub {
474 die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0;
477 # add bridgevlan if it doesn't already exist
478 if (! -d
"/sys/class/net/$bridgevlan") {
479 system("/sbin/brctl addbr $bridgevlan") == 0 ||
480 die "can't add bridge $bridgevlan\n";
483 # for each physical interface (eth or bridge) bind them to bridge vlan
484 foreach my $iface (@ifaces) {
485 activate_bridge_vlan_slave
($bridgevlan, $iface, $tag);
488 #fixme: set other bridge flags
490 # remove ipv6 link-local address before activation
491 disable_ipv6
($bridgevlan);
492 # be sure to have the bridge up
493 &$activate_interface($bridgevlan);
499 my ($host, $port, $timeout) = @_;
503 $timeout = 3 if !$timeout; # sane default
505 # Net::Ping defaults to the echo port
508 # Net::Ping's port_number() implies service_check(1)
514 $result = PVE
::Tools
::run_with_timeout
($timeout, sub {
515 $sock = IO
::Socket
::IP-
>new(PeerHost
=> $host, PeerPort
=> $port, Type
=> SOCK_STREAM
);
516 $result = $refused if $! == ECONNREFUSED
;
527 my ($cidr, $version) = @_;
529 return if $cidr !~ m!^(\S+?)/(\S+)$!;
530 my ($ip, $prefix) = ($1, $2);
532 my $ipobj = Net
::IP-
>new($ip, $version);
535 $version = $ipobj->version();
537 my $binmask = Net
::IP
::ip_get_mask
($prefix, $version);
540 my $masked_binip = $ipobj->binip() & $binmask;
541 my $masked_ip = Net
::IP
::ip_bintoip
($masked_binip, $version);
542 return Net
::IP-
>new("$masked_ip/$prefix");
546 my ($ip, $cidr, $version) = @_;
548 my $cidr_obj = IP_from_cidr
($cidr, $version);
549 return undef if !$cidr_obj;
551 my $ip_obj = Net
::IP-
>new($ip, $version);
552 return undef if !$ip_obj;
554 return $cidr_obj->overlaps($ip_obj) == $Net::IP
::IP_B_IN_A_OVERLAP
;
558 sub get_local_ip_from_cidr
{
561 my $cmd = ['/sbin/ip', 'address', 'show', 'to', $cidr, 'up'];
568 if ($line =~ m!^\s*inet(?:6)?\s+($PVE::Tools::IPRE)/\d+!) {
573 PVE
::Tools
::run_command
($cmd, outfunc
=> $code);
579 my ($code, @param) = @_;
580 my $res = lock_file
('/var/lock/pve-network.lck', 10, $code, @param);