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 = {
68 '255.255.128.0' => 17,
69 '255.255.192.0' => 18,
70 '255.255.224.0' => 19,
71 '255.255.240.0' => 20,
72 '255.255.248.0' => 21,
73 '255.255.252.0' => 22,
74 '255.255.254.0' => 23,
75 '255.255.255.0' => 24,
76 '255.255.255.128' => 25,
77 '255.255.255.192' => 26,
78 '255.255.255.224' => 27,
79 '255.255.255.240' => 28,
80 '255.255.255.248' => 29,
81 '255.255.255.252' => 30,
82 '255.255.255.254' => 31,
83 '255.255.255.255' => 32,
86 sub setup_tc_rate_limit
{
87 my ($iface, $rate, $burst, $debug) = @_;
89 # these are allowed / expected to fail, e.g. when there is no previous rate limit to remove
90 eval { run_command
("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1"); };
91 eval { run_command
("/sbin/tc filter del dev $iface parent ffff: protocol all pref 50 u32 >/dev/null 2>&1"); };
92 eval { run_command
("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1"); };
93 eval { run_command
("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1"); };
97 # tbf does not work for unknown reason
98 #$TC qdisc add dev $DEV root tbf rate $RATE latency 100ms burst $BURST
99 # so we use htb instead
100 run_command
("/sbin/tc qdisc add dev $iface root handle 1: htb default 1");
101 run_command
("/sbin/tc class add dev $iface parent 1: classid 1:1 " .
102 "htb rate ${rate}bps burst ${burst}b");
104 run_command
("/sbin/tc qdisc add dev $iface handle ffff: ingress");
105 run_command
("/sbin/tc filter add dev $iface parent ffff: " .
107 "police rate ${rate}bps burst ${burst}b mtu 64kb " .
111 print "DEBUG tc settings\n";
112 system("/sbin/tc qdisc ls dev $iface");
113 system("/sbin/tc class ls dev $iface");
114 system("/sbin/tc filter ls dev $iface parent ffff:");
119 my ($iface, $rate) = @_;
122 $rate = int($rate*1024*1024) if $rate;
123 my $burst = 1024*1024;
125 setup_tc_rate_limit
($iface, $rate, $burst, $debug);
128 my $read_bridge_mtu = sub {
131 my $mtu = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/mtu");
132 die "bridge '$bridge' does not exist\n" if !$mtu;
133 # avoid insecure dependency;
134 die "unable to parse mtu value" if $mtu !~ /^(\d+)$/;
140 my $parse_tap_device_name = sub {
141 my ($iface, $noerr) = @_;
145 if ($iface =~ m/^tap(\d+)i(\d+)$/) {
148 } elsif ($iface =~ m/^veth(\d+)i(\d+)$/) {
152 return undef if $noerr;
153 die "can't create firewall bridge for random interface name '$iface'\n";
156 return ($vmid, $devid);
159 my $compute_fwbr_names = sub {
160 my ($vmid, $devid) = @_;
162 my $fwbr = "fwbr${vmid}i${devid}";
163 # Note: the firewall use 'fwln+' to filter traffic to VMs
164 my $vethfw = "fwln${vmid}i${devid}";
165 my $vethfwpeer = "fwpr${vmid}p${devid}";
166 my $ovsintport = "fwln${vmid}o${devid}";
168 return ($fwbr, $vethfw, $vethfwpeer, $ovsintport);
171 my $cond_create_bridge = sub {
174 if (! -d
"/sys/class/net/$bridge") {
175 system("/sbin/brctl addbr $bridge") == 0 ||
176 die "can't add bridge '$bridge'\n";
180 my $bridge_add_interface = sub {
181 my ($bridge, $iface, $tag, $trunks) = @_;
183 system("/sbin/brctl addif $bridge $iface") == 0 ||
184 die "can't add interface 'iface' to bridge '$bridge'\n";
186 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/bridge/vlan_filtering");
190 system("/sbin/bridge vlan add dev $iface vid $tag pvid untagged") == 0 ||
191 die "unable to add vlan $tag to interface $iface\n";
193 warn "Caution: Setting VLAN ID 1 on a VLAN aware bridge may be dangerous\n" if $tag == 1;
195 system("/sbin/bridge vlan add dev $iface vid 2-4094") == 0 ||
196 die "unable to add default vlan tags to interface $iface\n" if !$trunks;
200 my @trunks_array = split /;/, $trunks;
201 foreach my $trunk (@trunks_array) {
202 system("/sbin/bridge vlan add dev $iface vid $trunk") == 0 ||
203 die "unable to add vlan $trunk to interface $iface\n";
209 my $ovs_bridge_add_port = sub {
210 my ($bridge, $iface, $tag, $internal, $trunks) = @_;
212 $trunks =~ s/;/,/g if $trunks;
214 my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface";
215 $cmd .= " tag=$tag" if $tag;
216 $cmd .= " trunks=". join(',', $trunks) if $trunks;
217 $cmd .= " vlan_mode=native-untagged" if $tag && $trunks;
219 $cmd .= " -- set Interface $iface type=internal" if $internal;
221 die "can't add ovs port '$iface'\n";
224 my $activate_interface = sub {
227 system("/sbin/ip link set $iface up") == 0 ||
228 die "can't activate interface '$iface'\n";
232 my ($iface, $bridge) = @_;
234 die "unable to get bridge setting\n" if !$bridge;
236 my $bridgemtu = &$read_bridge_mtu($bridge);
239 PVE
::Tools
::run_command
("/sbin/ifconfig $iface 0.0.0.0 promisc up mtu $bridgemtu");
241 die "interface activation failed\n" if $@;
245 my ($veth, $vethpeer, $bridge, $mac) = @_;
247 die "unable to get bridge setting\n" if !$bridge;
249 my $bridgemtu = &$read_bridge_mtu($bridge);
252 if (! -d
"/sys/class/net/$veth") {
253 my $cmd = "/sbin/ip link add name $veth type veth peer name $vethpeer mtu $bridgemtu";
254 $cmd .= " addr $mac" if $mac;
255 system($cmd) == 0 || die "can't create interface $veth\n";
259 &$activate_interface($veth);
260 &$activate_interface($vethpeer);
266 if (-d
"/sys/class/net/$veth") {
267 run_command
("/sbin/ip link delete dev $veth", outfunc
=> sub {}, errfunc
=> sub {});
272 my $create_firewall_bridge_linux = sub {
273 my ($iface, $bridge, $tag, $trunks) = @_;
275 my ($vmid, $devid) = &$parse_tap_device_name($iface);
276 my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid);
278 &$cond_create_bridge($fwbr);
279 &$activate_interface($fwbr);
281 copy_bridge_config
($bridge, $fwbr);
282 veth_create
($vethfw, $vethfwpeer, $bridge);
284 &$bridge_add_interface($fwbr, $vethfw);
285 &$bridge_add_interface($bridge, $vethfwpeer, $tag, $trunks);
287 &$bridge_add_interface($fwbr, $iface);
290 my $create_firewall_bridge_ovs = sub {
291 my ($iface, $bridge, $tag, $trunks) = @_;
293 my ($vmid, $devid) = &$parse_tap_device_name($iface);
294 my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
296 my $bridgemtu = &$read_bridge_mtu($bridge);
298 &$cond_create_bridge($fwbr);
299 &$activate_interface($fwbr);
301 &$bridge_add_interface($fwbr, $iface);
303 &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1, $trunks);
304 &$activate_interface($ovsintport);
306 # set the same mtu for ovs int port
307 PVE
::Tools
::run_command
("/sbin/ifconfig $ovsintport mtu $bridgemtu");
309 &$bridge_add_interface($fwbr, $ovsintport);
312 my $cleanup_firewall_bridge = sub {
315 my ($vmid, $devid) = &$parse_tap_device_name($iface, 1);
316 return if !defined($vmid);
317 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
319 # cleanup old port config from any openvswitch bridge
320 if (-d
"/sys/class/net/$ovsintport") {
321 run_command
("/usr/bin/ovs-vsctl del-port $ovsintport", outfunc
=> sub {}, errfunc
=> sub {});
324 # delete old vethfw interface
325 veth_delete
($vethfw);
327 # cleanup fwbr bridge
328 if (-d
"/sys/class/net/$fwbr") {
329 run_command
("/sbin/ip link set dev $fwbr down", outfunc
=> sub {}, errfunc
=> sub {});
330 run_command
("/sbin/brctl delbr $fwbr", outfunc
=> sub {}, errfunc
=> sub {});
335 my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_;
337 #cleanup old port config from any openvswitch bridge
338 eval {run_command
("/usr/bin/ovs-vsctl del-port $iface", outfunc
=> sub {}, errfunc
=> sub {}) };
340 if (-d
"/sys/class/net/$bridge/bridge") {
341 &$cleanup_firewall_bridge($iface); # remove stale devices
343 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/bridge/vlan_filtering");
346 die "vlan aware feature need to be enabled to use trunks" if $trunks;
347 my $newbridge = activate_bridge_vlan
($bridge, $tag);
348 copy_bridge_config
($bridge, $newbridge) if $bridge ne $newbridge;
349 $bridge = $newbridge;
354 &$create_firewall_bridge_linux($iface, $bridge, $tag, $trunks);
356 &$bridge_add_interface($bridge, $iface, $tag, $trunks);
360 &$cleanup_firewall_bridge($iface); # remove stale devices
363 &$create_firewall_bridge_ovs($iface, $bridge, $tag, $trunks);
365 &$ovs_bridge_add_port($bridge, $iface, $tag, undef, $trunks);
369 tap_rate_limit
($iface, $rate);
375 my $path= "/sys/class/net/$iface/brport/bridge";
377 my $bridge = basename
(readlink($path));
378 #avoid insecure dependency;
379 ($bridge) = $bridge =~ /(\S+)/;
381 system("/sbin/brctl delif $bridge $iface") == 0 ||
382 die "can't del interface '$iface' from bridge '$bridge'\n";
386 &$cleanup_firewall_bridge($iface);
387 #cleanup old port config from any openvswitch bridge
388 eval {run_command
("/usr/bin/ovs-vsctl del-port $iface", outfunc
=> sub {}, errfunc
=> sub {}) };
391 sub copy_bridge_config
{
392 my ($br0, $br1) = @_;
394 return if $br0 eq $br1;
396 my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
397 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier'];
399 foreach my $sysname (@$br_configs) {
401 my $v0 = PVE
::Tools
::file_read_firstline
("/sys/class/net/$br0/bridge/$sysname");
402 my $v1 = PVE
::Tools
::file_read_firstline
("/sys/class/net/$br1/bridge/$sysname");
404 PVE
::ProcFSTools
::write_proc_entry
("/sys/class/net/$br1/bridge/$sysname", $v0);
411 sub activate_bridge_vlan_slave
{
412 my ($bridgevlan, $iface, $tag) = @_;
413 my $ifacevlan = "${iface}.$tag";
415 # create vlan on $iface is not already exist
416 if (! -d
"/sys/class/net/$ifacevlan") {
417 system("/sbin/ip link add link $iface name ${iface}.${tag} type vlan id $tag") == 0 ||
418 die "can't add vlan tag $tag to interface $iface\n";
421 # be sure to have the $ifacevlan up
422 &$activate_interface($ifacevlan);
424 # test if $vlaniface is already enslaved in another bridge
425 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
427 my $tbridge = basename
(readlink($path));
428 if ($tbridge ne $bridgevlan) {
429 die "interface $ifacevlan already exist in bridge $tbridge\n";
431 # Port already attached to bridge: do nothing.
436 # add $ifacevlan to the bridge
437 &$bridge_add_interface($bridgevlan, $ifacevlan);
440 sub activate_bridge_vlan
{
441 my ($bridge, $tag_param) = @_;
443 die "bridge '$bridge' is not active\n" if ! -d
"/sys/class/net/$bridge";
445 return $bridge if !defined($tag_param); # no vlan, simply return
447 my $tag = int($tag_param);
449 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
451 my $bridgevlan = "${bridge}v$tag";
454 my $dir = "/sys/class/net/$bridge/brif";
455 PVE
::Tools
::dir_glob_foreach
($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub {
459 die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0;
461 # add bridgevlan if it doesn't already exist
462 if (! -d
"/sys/class/net/$bridgevlan") {
463 system("/sbin/brctl addbr $bridgevlan") == 0 ||
464 die "can't add bridge $bridgevlan\n";
467 # for each physical interface (eth or bridge) bind them to bridge vlan
468 foreach my $iface (@ifaces) {
469 activate_bridge_vlan_slave
($bridgevlan, $iface, $tag);
472 #fixme: set other bridge flags
474 # be sure to have the bridge up
475 system("/sbin/ip link set $bridgevlan up") == 0 ||
476 die "can't up bridge $bridgevlan\n";
482 my ($host, $port, $timeout) = @_;
486 $timeout = 3 if !$timeout; # sane default
488 # Net::Ping defaults to the echo port
491 # Net::Ping's port_number() implies service_check(1)
497 $result = PVE
::Tools
::run_with_timeout
($timeout, sub {
498 $sock = IO
::Socket
::IP-
>new(PeerHost
=> $host, PeerPort
=> $port, Type
=> SOCK_STREAM
);
499 $result = $refused if $! == ECONNREFUSED
;
510 my ($cidr, $version) = @_;
512 return if $cidr !~ m!^(\S+?)/(\S+)$!;
513 my ($ip, $prefix) = ($1, $2);
515 my $ipobj = Net
::IP-
>new($ip, $version);
518 $version = $ipobj->version();
520 my $binmask = Net
::IP
::ip_get_mask
($prefix, $version);
523 my $masked_binip = $ipobj->binip() & $binmask;
524 my $masked_ip = Net
::IP
::ip_bintoip
($masked_binip, $version);
525 return Net
::IP-
>new("$masked_ip/$prefix");
529 my ($ip, $cidr, $version) = @_;
531 my $cidr_obj = IP_from_cidr
($cidr, $version);
532 return undef if !$cidr_obj;
534 my $ip_obj = Net
::IP-
>new($ip, $version);
535 return undef if !$ip_obj;
537 return $cidr_obj->overlaps($ip_obj) == $Net::IP
::IP_B_IN_A_OVERLAP
;
540 # struct ifreq { // FOR SIOCGIFFLAGS:
541 # char ifrn_name[IFNAMSIZ]
544 my $STRUCT_IFREQ_SIOCGIFFLAGS = 'Z' . IFNAMSIZ
. 's1';
545 sub get_active_interfaces
{
546 # Use the interface name list from /proc/net/dev
547 open my $fh, '<', '/proc/net/dev'
548 or die "failed to open /proc/net/dev: $!\n";
549 # And filter by IFF_UP flag fetched via a PF_INET6 socket ioctl:
551 socket($sock, PF_INET6
, SOCK_DGRAM
, &IPPROTO_IP
)
552 or socket($sock, PF_INET
, SOCK_DGRAM
, &IPPROTO_IP
)
556 while(defined(my $line = <$fh>)) {
557 next if $line !~ /^\s*([^:\s]+):/;
559 my $ifreq = pack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifname, 0);
560 if (!defined(ioctl($sock, SIOCGIFFLAGS
, $ifreq))) {
561 warn "failed to get interface flags for: $ifname\n";
564 my ($name, $flags) = unpack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifreq);
565 push @$ifaces, $ifname if ($flags & IFF_UP
);