5 use PVE
::Tools
qw(run_command);
10 use POSIX
qw(ECONNREFUSED);
14 use Socket
qw(IPPROTO_IP);
16 use constant IFF_UP
=> 1;
17 use constant IFNAMSIZ
=> 16;
18 use constant SIOCGIFFLAGS
=> 0x8913;
20 # host network related utility functions
22 our $ipv4_reverse_mask = [
58 our $ipv4_mask_hash_localnet = {
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,
76 sub setup_tc_rate_limit
{
77 my ($iface, $rate, $burst, $debug) = @_;
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"); };
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");
94 run_command
("/sbin/tc qdisc add dev $iface handle ffff: ingress");
95 run_command
("/sbin/tc filter add dev $iface parent ffff: " .
97 "police rate ${rate}bps burst ${burst}b mtu 64kb " .
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:");
109 my ($iface, $rate) = @_;
112 $rate = int($rate*1024*1024) if $rate;
113 my $burst = 1024*1024;
115 setup_tc_rate_limit
($iface, $rate, $burst, $debug);
118 my $read_bridge_mtu = sub {
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 my $cond_create_bridge = sub {
164 if (! -d
"/sys/class/net/$bridge") {
165 system("/sbin/brctl addbr $bridge") == 0 ||
166 die "can't add bridge '$bridge'\n";
170 my $bridge_add_interface = sub {
171 my ($bridge, $iface, $tag, $trunks) = @_;
173 system("/sbin/brctl addif $bridge $iface") == 0 ||
174 die "can't add interface 'iface' to bridge '$bridge'\n";
176 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/bridge/vlan_filtering");
180 system("/sbin/bridge vlan add dev $iface vid $tag pvid untagged") == 0 ||
181 die "unable to add vlan $tag to interface $iface\n";
183 warn "Caution: Setting VLAN ID 1 on a VLAN aware bridge may be dangerous\n" if $tag == 1;
185 system("/sbin/bridge vlan add dev $iface vid 2-4094") == 0 ||
186 die "unable to add default vlan tags to interface $iface\n" 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";
199 my $ovs_bridge_add_port = sub {
200 my ($bridge, $iface, $tag, $internal, $trunks) = @_;
202 $trunks =~ s/;/,/g if $trunks;
204 my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface";
205 $cmd .= " tag=$tag" if $tag;
206 $cmd .= " trunks=". join(',', $trunks) if $trunks;
207 $cmd .= " vlan_mode=native-untagged" if $tag && $trunks;
209 $cmd .= " -- set Interface $iface type=internal" if $internal;
211 die "can't add ovs port '$iface'\n";
214 my $activate_interface = sub {
217 system("/sbin/ip link set $iface up") == 0 ||
218 die "can't activate interface '$iface'\n";
222 my ($iface, $bridge) = @_;
224 die "unable to get bridge setting\n" if !$bridge;
226 my $bridgemtu = &$read_bridge_mtu($bridge);
229 PVE
::Tools
::run_command
("/sbin/ifconfig $iface 0.0.0.0 promisc up mtu $bridgemtu");
231 die "interface activation failed\n" if $@;
235 my ($veth, $vethpeer, $bridge, $mac) = @_;
237 die "unable to get bridge setting\n" if !$bridge;
239 my $bridgemtu = &$read_bridge_mtu($bridge);
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";
249 &$activate_interface($veth);
250 &$activate_interface($vethpeer);
256 if (-d
"/sys/class/net/$veth") {
257 run_command
("/sbin/ip link delete dev $veth", outfunc
=> sub {}, errfunc
=> sub {});
262 my $create_firewall_bridge_linux = sub {
263 my ($iface, $bridge, $tag, $trunks) = @_;
265 my ($vmid, $devid) = &$parse_tap_device_name($iface);
266 my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid);
268 &$cond_create_bridge($fwbr);
269 &$activate_interface($fwbr);
271 copy_bridge_config
($bridge, $fwbr);
272 veth_create
($vethfw, $vethfwpeer, $bridge);
274 &$bridge_add_interface($fwbr, $vethfw);
275 &$bridge_add_interface($bridge, $vethfwpeer, $tag, $trunks);
277 &$bridge_add_interface($fwbr, $iface);
280 my $create_firewall_bridge_ovs = sub {
281 my ($iface, $bridge, $tag, $trunks) = @_;
283 my ($vmid, $devid) = &$parse_tap_device_name($iface);
284 my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
286 my $bridgemtu = &$read_bridge_mtu($bridge);
288 &$cond_create_bridge($fwbr);
289 &$activate_interface($fwbr);
291 &$bridge_add_interface($fwbr, $iface);
293 &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1, $trunks);
294 &$activate_interface($ovsintport);
296 # set the same mtu for ovs int port
297 PVE
::Tools
::run_command
("/sbin/ifconfig $ovsintport mtu $bridgemtu");
299 &$bridge_add_interface($fwbr, $ovsintport);
302 my $cleanup_firewall_bridge = sub {
305 my ($vmid, $devid) = &$parse_tap_device_name($iface, 1);
306 return if !defined($vmid);
307 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
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 {});
314 # delete old vethfw interface
315 veth_delete
($vethfw);
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 {});
325 my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_;
327 #cleanup old port config from any openvswitch bridge
328 eval {run_command
("/usr/bin/ovs-vsctl del-port $iface", outfunc
=> sub {}, errfunc
=> sub {}) };
330 if (-d
"/sys/class/net/$bridge/bridge") {
331 &$cleanup_firewall_bridge($iface); # remove stale devices
333 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/bridge/vlan_filtering");
336 die "vlan aware feature need to be enabled to use trunks" if $trunks;
337 my $newbridge = activate_bridge_vlan
($bridge, $tag);
338 copy_bridge_config
($bridge, $newbridge) if $bridge ne $newbridge;
339 $bridge = $newbridge;
344 &$create_firewall_bridge_linux($iface, $bridge, $tag, $trunks);
346 &$bridge_add_interface($bridge, $iface, $tag, $trunks);
350 &$cleanup_firewall_bridge($iface); # remove stale devices
353 &$create_firewall_bridge_ovs($iface, $bridge, $tag, $trunks);
355 &$ovs_bridge_add_port($bridge, $iface, $tag, undef, $trunks);
359 tap_rate_limit
($iface, $rate);
365 my $path= "/sys/class/net/$iface/brport/bridge";
367 my $bridge = basename
(readlink($path));
368 #avoid insecure dependency;
369 ($bridge) = $bridge =~ /(\S+)/;
371 system("/sbin/brctl delif $bridge $iface") == 0 ||
372 die "can't del interface '$iface' from bridge '$bridge'\n";
376 &$cleanup_firewall_bridge($iface);
377 #cleanup old port config from any openvswitch bridge
378 eval {run_command
("/usr/bin/ovs-vsctl del-port $iface", outfunc
=> sub {}, errfunc
=> sub {}) };
381 sub copy_bridge_config
{
382 my ($br0, $br1) = @_;
384 return if $br0 eq $br1;
386 my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
387 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier'];
389 foreach my $sysname (@$br_configs) {
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");
394 PVE
::ProcFSTools
::write_proc_entry
("/sys/class/net/$br1/bridge/$sysname", $v0);
401 sub activate_bridge_vlan_slave
{
402 my ($bridgevlan, $iface, $tag) = @_;
403 my $ifacevlan = "${iface}.$tag";
405 # create vlan on $iface is not already exist
406 if (! -d
"/sys/class/net/$ifacevlan") {
407 system("/sbin/ip link add link $iface name ${iface}.${tag} type vlan id $tag") == 0 ||
408 die "can't add vlan tag $tag to interface $iface\n";
411 # be sure to have the $ifacevlan up
412 &$activate_interface($ifacevlan);
414 # test if $vlaniface is already enslaved in another bridge
415 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
417 my $tbridge = basename
(readlink($path));
418 if ($tbridge ne $bridgevlan) {
419 die "interface $ifacevlan already exist in bridge $tbridge\n";
421 # Port already attached to bridge: do nothing.
426 # add $ifacevlan to the bridge
427 &$bridge_add_interface($bridgevlan, $ifacevlan);
430 sub activate_bridge_vlan
{
431 my ($bridge, $tag_param) = @_;
433 die "bridge '$bridge' is not active\n" if ! -d
"/sys/class/net/$bridge";
435 return $bridge if !defined($tag_param); # no vlan, simply return
437 my $tag = int($tag_param);
439 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
441 my $bridgevlan = "${bridge}v$tag";
444 my $dir = "/sys/class/net/$bridge/brif";
445 PVE
::Tools
::dir_glob_foreach
($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub {
449 die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0;
451 # add bridgevlan if it doesn't already exist
452 if (! -d
"/sys/class/net/$bridgevlan") {
453 system("/sbin/brctl addbr $bridgevlan") == 0 ||
454 die "can't add bridge $bridgevlan\n";
457 # for each physical interface (eth or bridge) bind them to bridge vlan
458 foreach my $iface (@ifaces) {
459 activate_bridge_vlan_slave
($bridgevlan, $iface, $tag);
462 #fixme: set other bridge flags
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";
472 my ($host, $port, $timeout) = @_;
476 $timeout = 3 if !$timeout; # sane default
478 # Net::Ping defaults to the echo port
481 # Net::Ping's port_number() implies service_check(1)
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
;
500 my ($cidr, $version) = @_;
502 return if $cidr !~ m!^(\S+?)/(\S+)$!;
503 my ($ip, $prefix) = ($1, $2);
505 my $ipobj = Net
::IP-
>new($ip, $version);
508 $version = $ipobj->version();
510 my $binmask = Net
::IP
::ip_get_mask
($prefix, $version);
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");
519 my ($ip, $cidr, $version) = @_;
521 my $cidr_obj = IP_from_cidr
($cidr, $version);
522 return undef if !$cidr_obj;
524 my $ip_obj = Net
::IP-
>new($ip, $version);
525 return undef if !$ip_obj;
527 return $cidr_obj->overlaps($ip_obj) == $Net::IP
::IP_B_IN_A_OVERLAP
;
530 # struct ifreq { // FOR SIOCGIFFLAGS:
531 # char ifrn_name[IFNAMSIZ]
534 my $STRUCT_IFREQ_SIOCGIFFLAGS = 'Z' . IFNAMSIZ
. 's1';
535 sub 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:
541 socket($sock, PF_INET6
, SOCK_DGRAM
, &IPPROTO_IP
)
542 or socket($sock, PF_INET
, SOCK_DGRAM
, &IPPROTO_IP
)
546 while(defined(my $line = <$fh>)) {
547 next if $line !~ /^\s*([^:\s]+):/;
549 my $ifreq = pack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifname, 0);
550 if (!defined(ioctl($sock, SIOCGIFFLAGS
, $ifreq))) {
551 warn "failed to get interface flags for: $ifname\n";
554 my ($name, $flags) = unpack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifreq);
555 push @$ifaces, $ifname if ($flags & IFF_UP
);