5 use PVE
::Tools
qw(run_command lock_file);
10 use Socket
qw(NI_NUMERICHOST NI_NUMERICSERV);
11 use POSIX
qw(ECONNREFUSED);
15 # host network related utility functions
17 our $PHYSICAL_NIC_RE = qr/(?:eth\d+|en[^:.]+|ib\d+)/;
19 our $ipv4_reverse_mask = [
55 our $ipv4_mask_hash_localnet = {
65 '255.255.128.0' => 17,
66 '255.255.192.0' => 18,
67 '255.255.224.0' => 19,
68 '255.255.240.0' => 20,
69 '255.255.248.0' => 21,
70 '255.255.252.0' => 22,
71 '255.255.254.0' => 23,
72 '255.255.255.0' => 24,
73 '255.255.255.128' => 25,
74 '255.255.255.192' => 26,
75 '255.255.255.224' => 27,
76 '255.255.255.240' => 28,
77 '255.255.255.248' => 29,
78 '255.255.255.252' => 30,
79 '255.255.255.254' => 31,
80 '255.255.255.255' => 32,
83 sub setup_tc_rate_limit
{
84 my ($iface, $rate, $burst, $debug) = @_;
86 # these are allowed / expected to fail, e.g. when there is no previous rate limit to remove
87 eval { run_command
("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1"); };
88 eval { run_command
("/sbin/tc filter del dev $iface parent ffff: protocol all pref 50 u32 >/dev/null 2>&1"); };
89 eval { run_command
("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1"); };
90 eval { run_command
("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1"); };
94 # tbf does not work for unknown reason
95 #$TC qdisc add dev $DEV root tbf rate $RATE latency 100ms burst $BURST
96 # so we use htb instead
97 run_command
("/sbin/tc qdisc add dev $iface root handle 1: htb default 1");
98 run_command
("/sbin/tc class add dev $iface parent 1: classid 1:1 " .
99 "htb rate ${rate}bps burst ${burst}b");
101 run_command
("/sbin/tc qdisc add dev $iface handle ffff: ingress");
102 run_command
("/sbin/tc filter add dev $iface parent ffff: " .
104 "police rate ${rate}bps burst ${burst}b mtu 64kb " .
108 print "DEBUG tc settings\n";
109 system("/sbin/tc qdisc ls dev $iface");
110 system("/sbin/tc class ls dev $iface");
111 system("/sbin/tc filter ls dev $iface parent ffff:");
116 my ($iface, $rate) = @_;
119 $rate = int($rate*1024*1024) if $rate;
120 my $burst = 1024*1024;
122 setup_tc_rate_limit
($iface, $rate, $burst, $debug);
125 my $read_bridge_mtu = sub {
128 my $mtu = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/mtu");
129 die "bridge '$bridge' does not exist\n" if !$mtu;
130 # avoid insecure dependency;
131 die "unable to parse mtu value" if $mtu !~ /^(\d+)$/;
137 my $parse_tap_device_name = sub {
138 my ($iface, $noerr) = @_;
142 if ($iface =~ m/^tap(\d+)i(\d+)$/) {
145 } elsif ($iface =~ m/^veth(\d+)i(\d+)$/) {
149 return undef if $noerr;
150 die "can't create firewall bridge for random interface name '$iface'\n";
153 return ($vmid, $devid);
156 my $compute_fwbr_names = sub {
157 my ($vmid, $devid) = @_;
159 my $fwbr = "fwbr${vmid}i${devid}";
160 # Note: the firewall use 'fwln+' to filter traffic to VMs
161 my $vethfw = "fwln${vmid}i${devid}";
162 my $vethfwpeer = "fwpr${vmid}p${devid}";
163 my $ovsintport = "fwln${vmid}o${devid}";
165 return ($fwbr, $vethfw, $vethfwpeer, $ovsintport);
168 sub iface_delete
($) {
170 run_command
(['/sbin/ip', 'link', 'delete', 'dev', $iface], noerr
=> 1)
171 == 0 or die "failed to delete interface '$iface'\n";
174 sub iface_create
($$@) {
175 my ($iface, $type, @args) = @_;
176 run_command
(['/sbin/ip', 'link', 'add', $iface, 'type', $type, @args], noerr
=> 1)
177 == 0 or die "failed to create interface '$iface'\n";
181 my ($iface, @opts) = @_;
182 run_command
(['/sbin/ip', 'link', 'set', $iface, @opts], noerr
=> 1)
183 == 0 or die "failed to set interface options for '$iface' (".join(' ', @opts).")\n";
186 # helper for nicer error messages:
187 sub iface_set_master
($$) {
188 my ($iface, $master) = @_;
189 if (defined($master)) {
190 eval { iface_set
($iface, 'master', $master) };
191 die "can't enslave '$iface' to '$master'\n" if $@;
193 eval { iface_set
($iface, 'nomaster') };
194 die "can't unenslave '$iface'\n" if $@;
198 my $cond_create_bridge = sub {
201 if (! -d
"/sys/class/net/$bridge") {
202 iface_create
($bridge, 'bridge');
203 disable_ipv6
($bridge);
209 return if !-d
'/proc/sys/net/ipv6'; # ipv6 might be completely disabled
210 my $file = "/proc/sys/net/ipv6/conf/$iface/disable_ipv6";
211 open(my $fh, '>', $file) or die "failed to open $file for writing: $!\n";
212 print {$fh} "1\n" or die "failed to disable link-local ipv6 for $iface\n";
216 my $bridge_add_interface = sub {
217 my ($bridge, $iface, $tag, $trunks) = @_;
219 # drop link local address (it can't be used when on a bridge anyway)
220 disable_ipv6
($iface);
221 iface_set_master
($iface, $bridge);
223 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/bridge/vlan_filtering");
227 system({'/sbin/bridge'} 'bridge', 'vlan', 'del', 'dev', $iface, 'vid', '1-4094') == 0
228 or die "failed to remove default vlan tags of $iface\n";
229 system({'/sbin/bridge'} 'bridge', 'vlan', 'add', 'dev', $iface, 'vid', $tag, 'pvid', 'untagged') == 0
230 or die "unable to add vlan $tag to interface $iface\n";
232 warn "Caution: Setting VLAN ID 1 on a VLAN aware bridge may be dangerous\n" if $tag == 1;
234 system("/sbin/bridge vlan add dev $iface vid 2-4094") == 0 ||
235 die "unable to add default vlan tags to interface $iface\n" if !$trunks;
239 my @trunks_array = split /;/, $trunks;
240 foreach my $trunk (@trunks_array) {
241 system("/sbin/bridge vlan add dev $iface vid $trunk") == 0 ||
242 die "unable to add vlan $trunk to interface $iface\n";
248 my $ovs_bridge_add_port = sub {
249 my ($bridge, $iface, $tag, $internal, $trunks) = @_;
251 $trunks =~ s/;/,/g if $trunks;
253 my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface";
254 $cmd .= " tag=$tag" if $tag;
255 $cmd .= " trunks=". join(',', $trunks) if $trunks;
256 $cmd .= " vlan_mode=native-untagged" if $tag && $trunks;
258 $cmd .= " -- set Interface $iface type=internal" if $internal;
260 die "can't add ovs port '$iface'\n";
261 disable_ipv6
($iface);
264 my $activate_interface = sub {
267 system("/sbin/ip link set $iface up") == 0 ||
268 die "can't activate interface '$iface'\n";
272 my ($iface, $bridge) = @_;
274 die "unable to get bridge setting\n" if !$bridge;
276 my $bridgemtu = &$read_bridge_mtu($bridge);
279 disable_ipv6
($iface);
280 PVE
::Tools
::run_command
(['/sbin/ip', 'link', 'set', $iface, 'up', 'promisc', 'on', 'mtu', $bridgemtu]);
282 die "interface activation failed\n" if $@;
286 my ($veth, $vethpeer, $bridge, $mac) = @_;
288 die "unable to get bridge setting\n" if !$bridge;
290 my $bridgemtu = &$read_bridge_mtu($bridge);
293 if (! -d
"/sys/class/net/$veth") {
294 my $cmd = "/sbin/ip link add name $veth mtu $bridgemtu type veth peer name $vethpeer mtu $bridgemtu";
295 $cmd .= " addr $mac" if $mac;
296 system($cmd) == 0 || die "can't create interface $veth\n";
301 disable_ipv6
($vethpeer);
302 &$activate_interface($veth);
303 &$activate_interface($vethpeer);
309 if (-d
"/sys/class/net/$veth") {
312 eval { tap_unplug
($veth) };
315 my $create_firewall_bridge_linux = sub {
316 my ($iface, $bridge, $tag, $trunks) = @_;
318 my ($vmid, $devid) = &$parse_tap_device_name($iface);
319 my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid);
321 &$cond_create_bridge($fwbr);
322 &$activate_interface($fwbr);
324 copy_bridge_config
($bridge, $fwbr);
325 veth_create
($vethfw, $vethfwpeer, $bridge);
327 &$bridge_add_interface($fwbr, $vethfw);
328 &$bridge_add_interface($bridge, $vethfwpeer, $tag, $trunks);
330 &$bridge_add_interface($fwbr, $iface);
333 my $create_firewall_bridge_ovs = sub {
334 my ($iface, $bridge, $tag, $trunks) = @_;
336 my ($vmid, $devid) = &$parse_tap_device_name($iface);
337 my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
339 my $bridgemtu = &$read_bridge_mtu($bridge);
341 &$cond_create_bridge($fwbr);
342 &$activate_interface($fwbr);
344 &$bridge_add_interface($fwbr, $iface);
346 &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1, $trunks);
347 &$activate_interface($ovsintport);
349 # set the same mtu for ovs int port
350 PVE
::Tools
::run_command
(['/sbin/ip', 'link', 'set', $ovsintport, 'mtu', $bridgemtu]);
352 &$bridge_add_interface($fwbr, $ovsintport);
355 my $cleanup_firewall_bridge = sub {
358 my ($vmid, $devid) = &$parse_tap_device_name($iface, 1);
359 return if !defined($vmid);
360 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
362 # cleanup old port config from any openvswitch bridge
363 if (-d
"/sys/class/net/$ovsintport") {
364 run_command
("/usr/bin/ovs-vsctl del-port $ovsintport", outfunc
=> sub {}, errfunc
=> sub {});
367 # delete old vethfw interface
368 veth_delete
($vethfw);
370 # cleanup fwbr bridge
371 if (-d
"/sys/class/net/$fwbr") {
377 my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_;
379 #cleanup old port config from any openvswitch bridge
380 eval {run_command
("/usr/bin/ovs-vsctl del-port $iface", outfunc
=> sub {}, errfunc
=> sub {}) };
382 if (-d
"/sys/class/net/$bridge/bridge") {
383 &$cleanup_firewall_bridge($iface); # remove stale devices
385 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/bridge/vlan_filtering");
388 die "vlan aware feature need to be enabled to use trunks" if $trunks;
389 my $newbridge = activate_bridge_vlan
($bridge, $tag);
390 copy_bridge_config
($bridge, $newbridge) if $bridge ne $newbridge;
391 $bridge = $newbridge;
396 &$create_firewall_bridge_linux($iface, $bridge, $tag, $trunks);
398 &$bridge_add_interface($bridge, $iface, $tag, $trunks);
402 &$cleanup_firewall_bridge($iface); # remove stale devices
405 &$create_firewall_bridge_ovs($iface, $bridge, $tag, $trunks);
407 &$ovs_bridge_add_port($bridge, $iface, $tag, undef, $trunks);
411 tap_rate_limit
($iface, $rate);
417 my $path= "/sys/class/net/$iface/brport/bridge";
419 my $bridge = basename
(readlink($path));
420 #avoid insecure dependency;
421 ($bridge) = $bridge =~ /(\S+)/;
423 iface_set_master
($iface, undef);
426 &$cleanup_firewall_bridge($iface);
427 #cleanup old port config from any openvswitch bridge
428 eval {run_command
("/usr/bin/ovs-vsctl del-port $iface", outfunc
=> sub {}, errfunc
=> sub {}) };
431 sub copy_bridge_config
{
432 my ($br0, $br1) = @_;
434 return if $br0 eq $br1;
436 my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
437 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier'];
439 foreach my $sysname (@$br_configs) {
441 my $v0 = PVE
::Tools
::file_read_firstline
("/sys/class/net/$br0/bridge/$sysname");
442 my $v1 = PVE
::Tools
::file_read_firstline
("/sys/class/net/$br1/bridge/$sysname");
444 PVE
::ProcFSTools
::write_proc_entry
("/sys/class/net/$br1/bridge/$sysname", $v0);
451 sub activate_bridge_vlan_slave
{
452 my ($bridgevlan, $iface, $tag) = @_;
453 my $ifacevlan = "${iface}.$tag";
455 # create vlan on $iface is not already exist
456 if (! -d
"/sys/class/net/$ifacevlan") {
457 system("/sbin/ip link add link $iface name $ifacevlan type vlan id $tag") == 0 ||
458 die "can't add vlan tag $tag to interface $iface\n";
460 # remove ipv6 link-local address before activation
461 disable_ipv6
($ifacevlan);
464 # be sure to have the $ifacevlan up
465 &$activate_interface($ifacevlan);
467 # test if $vlaniface is already enslaved in another bridge
468 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
470 my $tbridge = basename
(readlink($path));
471 if ($tbridge ne $bridgevlan) {
472 die "interface $ifacevlan already exist in bridge $tbridge\n";
474 # Port already attached to bridge: do nothing.
479 # add $ifacevlan to the bridge
480 &$bridge_add_interface($bridgevlan, $ifacevlan);
483 sub activate_bridge_vlan
{
484 my ($bridge, $tag_param) = @_;
486 die "bridge '$bridge' is not active\n" if ! -d
"/sys/class/net/$bridge";
488 return $bridge if !defined($tag_param); # no vlan, simply return
490 my $tag = int($tag_param);
492 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
494 my $bridgevlan = "${bridge}v$tag";
497 my $dir = "/sys/class/net/$bridge/brif";
498 PVE
::Tools
::dir_glob_foreach
($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub {
502 die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0;
505 # add bridgevlan if it doesn't already exist
506 if (! -d
"/sys/class/net/$bridgevlan") {
507 iface_create
($bridgevlan, 'bridge');
510 # for each physical interface (eth or bridge) bind them to bridge vlan
511 foreach my $iface (@ifaces) {
512 activate_bridge_vlan_slave
($bridgevlan, $iface, $tag);
515 #fixme: set other bridge flags
517 # remove ipv6 link-local address before activation
518 disable_ipv6
($bridgevlan);
519 # be sure to have the bridge up
520 &$activate_interface($bridgevlan);
526 my ($host, $port, $timeout) = @_;
530 $timeout = 3 if !$timeout; # sane default
532 # Net::Ping defaults to the echo port
535 # Net::Ping's port_number() implies service_check(1)
541 $result = PVE
::Tools
::run_with_timeout
($timeout, sub {
542 $sock = IO
::Socket
::IP-
>new(PeerHost
=> $host, PeerPort
=> $port, Type
=> SOCK_STREAM
);
543 $result = $refused if $! == ECONNREFUSED
;
554 my ($cidr, $version) = @_;
556 return if $cidr !~ m!^(\S+?)/(\S+)$!;
557 my ($ip, $prefix) = ($1, $2);
559 my $ipobj = Net
::IP-
>new($ip, $version);
562 $version = $ipobj->version();
564 my $binmask = Net
::IP
::ip_get_mask
($prefix, $version);
567 my $masked_binip = $ipobj->binip() & $binmask;
568 my $masked_ip = Net
::IP
::ip_bintoip
($masked_binip, $version);
569 return Net
::IP-
>new("$masked_ip/$prefix");
573 my ($ip, $cidr, $version) = @_;
575 my $cidr_obj = IP_from_cidr
($cidr, $version);
576 return undef if !$cidr_obj;
578 my $ip_obj = Net
::IP-
>new($ip, $version);
579 return undef if !$ip_obj;
581 return $cidr_obj->overlaps($ip_obj) == $Net::IP
::IP_B_IN_A_OVERLAP
;
585 sub get_local_ip_from_cidr
{
588 my $cmd = ['/sbin/ip', 'address', 'show', 'to', $cidr, 'up'];
595 if ($line =~ m!^\s*inet(?:6)?\s+($PVE::Tools::IPRE)(?:/\d+|\s+peer\s+)!) {
600 PVE
::Tools
::run_command
($cmd, outfunc
=> $code);
607 my ($err, $host, $port) = Socket
::getnameinfo
($addr, NI_NUMERICHOST
| NI_NUMERICSERV
);
608 die "failed to get numerical host address: $err\n" if $err;
609 return ($host, $port) if wantarray;
613 sub get_ip_from_hostname
{
614 my ($hostname, $noerr) = @_;
619 my @res = PVE
::Tools
::getaddrinfo_all
($hostname);
620 $family = $res[0]->{family
};
621 $ip = addr_to_ip
($res[0]->{addr
})
624 die "hostname lookup '$hostname' failed - $@" if !$noerr;
628 if ($ip =~ m/^127\.|^::1$/) {
629 die "hostname lookup '$hostname' failed - got local IP address '$ip'\n" if !$noerr;
633 return wantarray ?
($ip, $family) : $ip;
637 my ($code, @param) = @_;
638 my $res = lock_file
('/var/lock/pve-network.lck', 10, $code, @param);