8 use PVE
::Tools
qw(run_command lock_file);
13 use POSIX
qw(ECONNREFUSED);
14 use Socket
qw(NI_NUMERICHOST NI_NUMERICSERV);
16 # host network related utility functions
18 our $PHYSICAL_NIC_RE = qr/(?:eth\d+|en[^:.]+|ib\d+)/;
20 our $ipv4_reverse_mask = [
56 our $ipv4_mask_hash_localnet = {
66 '255.255.128.0' => 17,
67 '255.255.192.0' => 18,
68 '255.255.224.0' => 19,
69 '255.255.240.0' => 20,
70 '255.255.248.0' => 21,
71 '255.255.252.0' => 22,
72 '255.255.254.0' => 23,
73 '255.255.255.0' => 24,
74 '255.255.255.128' => 25,
75 '255.255.255.192' => 26,
76 '255.255.255.224' => 27,
77 '255.255.255.240' => 28,
78 '255.255.255.248' => 29,
79 '255.255.255.252' => 30,
80 '255.255.255.254' => 31,
81 '255.255.255.255' => 32,
84 sub setup_tc_rate_limit
{
85 my ($iface, $rate, $burst) = @_;
87 # these are allowed / expected to fail, e.g. when there is no previous rate limit to remove
88 eval { run_command
("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1"); };
89 eval { run_command
("/sbin/tc filter del dev $iface parent ffff: protocol all pref 50 u32 >/dev/null 2>&1"); };
90 eval { run_command
("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1"); };
91 eval { run_command
("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1"); };
95 # tbf does not work for unknown reason
96 #$TC qdisc add dev $DEV root tbf rate $RATE latency 100ms burst $BURST
97 # so we use htb instead
98 run_command
("/sbin/tc qdisc add dev $iface root handle 1: htb default 1");
99 run_command
("/sbin/tc class add dev $iface parent 1: classid 1:1 " .
100 "htb rate ${rate}bps burst ${burst}b");
102 run_command
("/sbin/tc qdisc add dev $iface handle ffff: ingress");
103 run_command
("/sbin/tc filter add dev $iface parent ffff: " .
105 "police rate ${rate}bps burst ${burst}b mtu 64kb " .
110 my ($iface, $rate) = @_;
112 $rate = int($rate*1024*1024) if $rate;
113 my $burst = 1024*1024;
115 setup_tc_rate_limit
($iface, $rate, $burst);
118 sub read_bridge_mtu
{
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+)$/;
130 my $parse_tap_device_name = sub {
131 my ($iface, $noerr) = @_;
135 if ($iface =~ m/^tap(\d+)i(\d+)$/) {
138 } elsif ($iface =~ m/^veth(\d+)i(\d+)$/) {
142 return undef if $noerr;
143 die "can't create firewall bridge for random interface name '$iface'\n";
146 return ($vmid, $devid);
149 my $compute_fwbr_names = sub {
150 my ($vmid, $devid) = @_;
152 my $fwbr = "fwbr${vmid}i${devid}";
153 # Note: the firewall use 'fwln+' to filter traffic to VMs
154 my $vethfw = "fwln${vmid}i${devid}";
155 my $vethfwpeer = "fwpr${vmid}p${devid}";
156 my $ovsintport = "fwln${vmid}o${devid}";
158 return ($fwbr, $vethfw, $vethfwpeer, $ovsintport);
161 sub iface_delete
($) {
163 run_command
(['/sbin/ip', 'link', 'delete', 'dev', $iface], noerr
=> 1)
164 == 0 or die "failed to delete interface '$iface'\n";
167 sub iface_create
($$@) {
168 my ($iface, $type, @args) = @_;
169 run_command
(['/sbin/ip', 'link', 'add', $iface, 'type', $type, @args], noerr
=> 1)
170 == 0 or die "failed to create interface '$iface'\n";
174 my ($iface, @opts) = @_;
175 run_command
(['/sbin/ip', 'link', 'set', $iface, @opts], noerr
=> 1)
176 == 0 or die "failed to set interface options for '$iface' (".join(' ', @opts).")\n";
179 # helper for nicer error messages:
180 sub iface_set_master
($$) {
181 my ($iface, $master) = @_;
182 if (defined($master)) {
183 eval { iface_set
($iface, 'master', $master) };
184 die "can't enslave '$iface' to '$master'\n" if $@;
186 eval { iface_set
($iface, 'nomaster') };
187 die "can't unenslave '$iface'\n" if $@;
191 my $cond_create_bridge = sub {
194 if (! -d
"/sys/class/net/$bridge") {
195 iface_create
($bridge, 'bridge');
196 disable_ipv6
($bridge);
202 return if !-d
'/proc/sys/net/ipv6'; # ipv6 might be completely disabled
203 my $file = "/proc/sys/net/ipv6/conf/$iface/disable_ipv6";
204 open(my $fh, '>', $file) or die "failed to open $file for writing: $!\n";
205 print {$fh} "1\n" or die "failed to disable link-local ipv6 for $iface\n";
209 my $bridge_add_interface = sub {
210 my ($bridge, $iface, $tag, $trunks) = @_;
212 # drop link local address (it can't be used when on a bridge anyway)
213 disable_ipv6
($iface);
214 iface_set_master
($iface, $bridge);
216 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/bridge/vlan_filtering");
220 eval { run_command
(['/sbin/bridge', 'vlan', 'del', 'dev', $iface, 'vid', '1-4094']) };
221 die "failed to remove default vlan tags of $iface - $@\n" if $@;
224 my @trunks_array = split /;/, $trunks;
225 foreach my $trunk (@trunks_array) {
226 eval { run_command
(['/sbin/bridge', 'vlan', 'add', 'dev', $iface, 'vid', $trunk]) };
227 die "unable to add vlan $trunk to interface $iface - $@\n" if $@;
230 eval { run_command
(['/sbin/bridge', 'vlan', 'add', 'dev', $iface, 'vid', '2-4094']) };
231 die "unable to add default vlan tags to interface $iface - $@\n" if $@;
235 eval { run_command
(['/sbin/bridge', 'vlan', 'add', 'dev', $iface, 'vid', $tag, 'pvid', 'untagged']) };
236 die "unable to add vlan $tag to interface $iface - $@\n" if $@;
240 my $ovs_bridge_add_port = sub {
241 my ($bridge, $iface, $tag, $internal, $trunks) = @_;
243 $trunks =~ s/;/,/g if $trunks;
245 my $cmd = ['/usr/bin/ovs-vsctl'];
247 push @$cmd, '--', 'add-port', $bridge, $iface;
248 push @$cmd, "tag=$tag" if $tag;
249 push @$cmd, "trunks=". join(',', $trunks) if $trunks;
250 push @$cmd, "vlan_mode=native-untagged" if $tag && $trunks;
254 push @$cmd, '--', 'set', 'Interface', $iface, 'type=internal';
257 eval { run_command
($cmd) };
258 die "can't add ovs port '$iface' - $@\n" if $@;
260 disable_ipv6
($iface);
263 my $activate_interface = sub {
266 eval { run_command
(['/sbin/ip', 'link', 'set', $iface, 'up']) };
267 die "can't activate interface '$iface' - $@\n" if $@;
271 my ($iface, $bridge) = @_;
273 die "unable to get bridge setting\n" if !$bridge;
275 my $bridgemtu = read_bridge_mtu
($bridge);
278 disable_ipv6
($iface);
279 PVE
::Tools
::run_command
(['/sbin/ip', 'link', 'set', $iface, 'up', 'promisc', 'on', 'mtu', $bridgemtu]);
281 die "interface activation failed\n" if $@;
285 my ($veth, $vethpeer, $bridge, $mac) = @_;
287 die "unable to get bridge setting\n" if !$bridge;
289 my $bridgemtu = read_bridge_mtu
($bridge);
292 if (! -d
"/sys/class/net/$veth") {
293 my $cmd = ['/sbin/ip', 'link', 'add'];
295 push @$cmd, 'name', $veth;
296 push @$cmd, 'mtu', $bridgemtu;
297 push @$cmd, 'type', 'veth';
299 push @$cmd, 'peer', 'name', $vethpeer, 'mtu', $bridgemtu;
301 push @$cmd, 'addr', $mac if $mac;
303 eval { run_command
($cmd) };
304 die "can't create interface $veth - $@\n" if $@;
309 disable_ipv6
($vethpeer);
310 &$activate_interface($veth);
311 &$activate_interface($vethpeer);
317 if (-d
"/sys/class/net/$veth") {
320 eval { tap_unplug
($veth) };
323 my $create_firewall_bridge_linux = sub {
324 my ($iface, $bridge, $tag, $trunks) = @_;
326 my ($vmid, $devid) = &$parse_tap_device_name($iface);
327 my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid);
329 &$cond_create_bridge($fwbr);
330 &$activate_interface($fwbr);
332 copy_bridge_config
($bridge, $fwbr);
333 veth_create
($vethfw, $vethfwpeer, $bridge);
335 &$bridge_add_interface($fwbr, $vethfw);
336 &$bridge_add_interface($bridge, $vethfwpeer, $tag, $trunks);
338 &$bridge_add_interface($fwbr, $iface);
341 my $create_firewall_bridge_ovs = sub {
342 my ($iface, $bridge, $tag, $trunks) = @_;
344 my ($vmid, $devid) = &$parse_tap_device_name($iface);
345 my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
347 my $bridgemtu = read_bridge_mtu
($bridge);
349 &$cond_create_bridge($fwbr);
350 &$activate_interface($fwbr);
352 &$bridge_add_interface($fwbr, $iface);
354 &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1, $trunks);
355 &$activate_interface($ovsintport);
357 # set the same mtu for ovs int port
358 PVE
::Tools
::run_command
(['/sbin/ip', 'link', 'set', $ovsintport, 'mtu', $bridgemtu]);
360 &$bridge_add_interface($fwbr, $ovsintport);
363 my $cleanup_firewall_bridge = sub {
366 my ($vmid, $devid) = &$parse_tap_device_name($iface, 1);
367 return if !defined($vmid);
368 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
370 # cleanup old port config from any openvswitch bridge
371 if (-d
"/sys/class/net/$ovsintport") {
372 run_command
("/usr/bin/ovs-vsctl del-port $ovsintport", outfunc
=> sub {}, errfunc
=> sub {});
375 # delete old vethfw interface
376 veth_delete
($vethfw);
378 # cleanup fwbr bridge
379 if (-d
"/sys/class/net/$fwbr") {
385 my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_;
387 #cleanup old port config from any openvswitch bridge
388 eval {run_command
("/usr/bin/ovs-vsctl del-port $iface", outfunc
=> sub {}, errfunc
=> sub {}) };
390 if (-d
"/sys/class/net/$bridge/bridge") {
391 &$cleanup_firewall_bridge($iface); # remove stale devices
393 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/bridge/vlan_filtering");
396 die "vlan aware feature need to be enabled to use trunks" if $trunks;
397 my $newbridge = activate_bridge_vlan
($bridge, $tag);
398 copy_bridge_config
($bridge, $newbridge) if $bridge ne $newbridge;
399 $bridge = $newbridge;
404 &$create_firewall_bridge_linux($iface, $bridge, $tag, $trunks);
406 &$bridge_add_interface($bridge, $iface, $tag, $trunks);
410 &$cleanup_firewall_bridge($iface); # remove stale devices
413 &$create_firewall_bridge_ovs($iface, $bridge, $tag, $trunks);
415 &$ovs_bridge_add_port($bridge, $iface, $tag, undef, $trunks);
419 tap_rate_limit
($iface, $rate);
425 my $path= "/sys/class/net/$iface/brport/bridge";
427 my $bridge = basename
(readlink($path));
428 #avoid insecure dependency;
429 ($bridge) = $bridge =~ /(\S+)/;
431 iface_set_master
($iface, undef);
434 &$cleanup_firewall_bridge($iface);
435 #cleanup old port config from any openvswitch bridge
436 eval {run_command
("/usr/bin/ovs-vsctl del-port $iface", outfunc
=> sub {}, errfunc
=> sub {}) };
439 sub copy_bridge_config
{
440 my ($br0, $br1) = @_;
442 return if $br0 eq $br1;
444 my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
445 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier'];
447 foreach my $sysname (@$br_configs) {
449 my $v0 = PVE
::Tools
::file_read_firstline
("/sys/class/net/$br0/bridge/$sysname");
450 my $v1 = PVE
::Tools
::file_read_firstline
("/sys/class/net/$br1/bridge/$sysname");
452 PVE
::ProcFSTools
::write_proc_entry
("/sys/class/net/$br1/bridge/$sysname", $v0);
459 sub activate_bridge_vlan_slave
{
460 my ($bridgevlan, $iface, $tag) = @_;
461 my $ifacevlan = "${iface}.$tag";
463 # create vlan on $iface is not already exist
464 if (! -d
"/sys/class/net/$ifacevlan") {
466 my $cmd = ['/sbin/ip', 'link', 'add'];
467 push @$cmd, 'link', $iface;
468 push @$cmd, 'name', $ifacevlan;
469 push @$cmd, 'type', 'vlan', 'id', $tag;
472 die "can't add vlan tag $tag to interface $iface - $@\n" if $@;
474 # remove ipv6 link-local address before activation
475 disable_ipv6
($ifacevlan);
478 # be sure to have the $ifacevlan up
479 &$activate_interface($ifacevlan);
481 # test if $vlaniface is already enslaved in another bridge
482 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
484 my $tbridge = basename
(readlink($path));
485 if ($tbridge ne $bridgevlan) {
486 die "interface $ifacevlan already exist in bridge $tbridge\n";
488 # Port already attached to bridge: do nothing.
493 # add $ifacevlan to the bridge
494 &$bridge_add_interface($bridgevlan, $ifacevlan);
497 sub activate_bridge_vlan
{
498 my ($bridge, $tag_param) = @_;
500 die "bridge '$bridge' is not active\n" if ! -d
"/sys/class/net/$bridge";
502 return $bridge if !defined($tag_param); # no vlan, simply return
504 my $tag = int($tag_param);
506 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
508 my $bridgevlan = "${bridge}v$tag";
511 my $dir = "/sys/class/net/$bridge/brif";
512 PVE
::Tools
::dir_glob_foreach
($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub {
516 die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0;
519 # add bridgevlan if it doesn't already exist
520 if (! -d
"/sys/class/net/$bridgevlan") {
521 iface_create
($bridgevlan, 'bridge');
524 # for each physical interface (eth or bridge) bind them to bridge vlan
525 foreach my $iface (@ifaces) {
526 activate_bridge_vlan_slave
($bridgevlan, $iface, $tag);
529 #fixme: set other bridge flags
531 # remove ipv6 link-local address before activation
532 disable_ipv6
($bridgevlan);
533 # be sure to have the bridge up
534 &$activate_interface($bridgevlan);
540 my ($host, $port, $timeout) = @_;
544 $timeout = 3 if !$timeout; # sane default
546 # Net::Ping defaults to the echo port
549 # Net::Ping's port_number() implies service_check(1)
555 $result = PVE
::Tools
::run_with_timeout
($timeout, sub {
556 $sock = IO
::Socket
::IP-
>new(PeerHost
=> $host, PeerPort
=> $port, Type
=> SOCK_STREAM
);
557 $result = $refused if $! == ECONNREFUSED
;
568 my ($cidr, $version) = @_;
570 return if $cidr !~ m!^(\S+?)/(\S+)$!;
571 my ($ip, $prefix) = ($1, $2);
573 my $ipobj = Net
::IP-
>new($ip, $version);
576 $version = $ipobj->version();
578 my $binmask = Net
::IP
::ip_get_mask
($prefix, $version);
581 my $masked_binip = $ipobj->binip() & $binmask;
582 my $masked_ip = Net
::IP
::ip_bintoip
($masked_binip, $version);
583 return Net
::IP-
>new("$masked_ip/$prefix");
587 my ($ip, $cidr, $version) = @_;
589 my $cidr_obj = IP_from_cidr
($cidr, $version);
590 return undef if !$cidr_obj;
592 my $ip_obj = Net
::IP-
>new($ip, $version);
593 return undef if !$ip_obj;
595 return $cidr_obj->overlaps($ip_obj) == $Net::IP
::IP_B_IN_A_OVERLAP
;
599 sub get_local_ip_from_cidr
{
603 run_command
(['/sbin/ip', 'address', 'show', 'to', $cidr, 'up'], outfunc
=> sub {
604 if ($_[0] =~ m!^\s*inet(?:6)?\s+($PVE::Tools::IPRE)(?:/\d+|\s+peer\s+)!) {
614 my ($err, $host, $port) = Socket
::getnameinfo
($addr, NI_NUMERICHOST
| NI_NUMERICSERV
);
615 die "failed to get numerical host address: $err\n" if $err;
616 return ($host, $port) if wantarray;
620 sub get_ip_from_hostname
{
621 my ($hostname, $noerr) = @_;
623 my @res = eval { PVE
::Tools
::getaddrinfo_all
($hostname) };
625 die "hostname lookup '$hostname' failed - $@" if !$noerr;
631 $family = $ai->{family
};
632 my $tmpip = addr_to_ip
($ai->{addr
});
633 if ($tmpip !~ m/^127\.|^::1$/) {
638 if (!defined($ip) ) {
639 die "hostname lookup '$hostname' failed - got local IP address '$ip'\n" if !$noerr;
643 return wantarray ?
($ip, $family) : $ip;
647 my ($code, @param) = @_;
648 my $res = lock_file
('/var/lock/pve-network.lck', 10, $code, @param);