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,
74 '255.255.255.254' => 31,
75 '255.255.255.255' => 32,
78 sub setup_tc_rate_limit
{
79 my ($iface, $rate, $burst, $debug) = @_;
81 # these are allowed / expected to fail, e.g. when there is no previous rate limit to remove
82 eval { run_command
("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1"); };
83 eval { run_command
("/sbin/tc filter del dev $iface parent ffff: protocol all pref 50 u32 >/dev/null 2>&1"); };
84 eval { run_command
("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1"); };
85 eval { run_command
("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1"); };
89 # tbf does not work for unknown reason
90 #$TC qdisc add dev $DEV root tbf rate $RATE latency 100ms burst $BURST
91 # so we use htb instead
92 run_command
("/sbin/tc qdisc add dev $iface root handle 1: htb default 1");
93 run_command
("/sbin/tc class add dev $iface parent 1: classid 1:1 " .
94 "htb rate ${rate}bps burst ${burst}b");
96 run_command
("/sbin/tc qdisc add dev $iface handle ffff: ingress");
97 run_command
("/sbin/tc filter add dev $iface parent ffff: " .
99 "police rate ${rate}bps burst ${burst}b mtu 64kb " .
103 print "DEBUG tc settings\n";
104 system("/sbin/tc qdisc ls dev $iface");
105 system("/sbin/tc class ls dev $iface");
106 system("/sbin/tc filter ls dev $iface parent ffff:");
111 my ($iface, $rate) = @_;
114 $rate = int($rate*1024*1024) if $rate;
115 my $burst = 1024*1024;
117 setup_tc_rate_limit
($iface, $rate, $burst, $debug);
120 my $read_bridge_mtu = sub {
123 my $mtu = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/mtu");
124 die "bridge '$bridge' does not exist\n" if !$mtu;
125 # avoid insecure dependency;
126 die "unable to parse mtu value" if $mtu !~ /^(\d+)$/;
132 my $parse_tap_device_name = sub {
133 my ($iface, $noerr) = @_;
137 if ($iface =~ m/^tap(\d+)i(\d+)$/) {
140 } elsif ($iface =~ m/^veth(\d+)i(\d+)$/) {
144 return undef if $noerr;
145 die "can't create firewall bridge for random interface name '$iface'\n";
148 return ($vmid, $devid);
151 my $compute_fwbr_names = sub {
152 my ($vmid, $devid) = @_;
154 my $fwbr = "fwbr${vmid}i${devid}";
155 # Note: the firewall use 'fwln+' to filter traffic to VMs
156 my $vethfw = "fwln${vmid}i${devid}";
157 my $vethfwpeer = "fwpr${vmid}p${devid}";
158 my $ovsintport = "fwln${vmid}o${devid}";
160 return ($fwbr, $vethfw, $vethfwpeer, $ovsintport);
163 my $cond_create_bridge = sub {
166 if (! -d
"/sys/class/net/$bridge") {
167 system("/sbin/brctl addbr $bridge") == 0 ||
168 die "can't add bridge '$bridge'\n";
172 my $bridge_add_interface = sub {
173 my ($bridge, $iface, $tag, $trunks) = @_;
175 system("/sbin/brctl addif $bridge $iface") == 0 ||
176 die "can't add interface 'iface' to bridge '$bridge'\n";
178 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/bridge/vlan_filtering");
182 system("/sbin/bridge vlan add dev $iface vid $tag pvid untagged") == 0 ||
183 die "unable to add vlan $tag to interface $iface\n";
185 warn "Caution: Setting VLAN ID 1 on a VLAN aware bridge may be dangerous\n" if $tag == 1;
187 system("/sbin/bridge vlan add dev $iface vid 2-4094") == 0 ||
188 die "unable to add default vlan tags to interface $iface\n" if !$trunks;
192 my @trunks_array = split /;/, $trunks;
193 foreach my $trunk (@trunks_array) {
194 system("/sbin/bridge vlan add dev $iface vid $trunk") == 0 ||
195 die "unable to add vlan $trunk to interface $iface\n";
201 my $ovs_bridge_add_port = sub {
202 my ($bridge, $iface, $tag, $internal, $trunks) = @_;
204 $trunks =~ s/;/,/g if $trunks;
206 my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface";
207 $cmd .= " tag=$tag" if $tag;
208 $cmd .= " trunks=". join(',', $trunks) if $trunks;
209 $cmd .= " vlan_mode=native-untagged" if $tag && $trunks;
211 $cmd .= " -- set Interface $iface type=internal" if $internal;
213 die "can't add ovs port '$iface'\n";
216 my $activate_interface = sub {
219 system("/sbin/ip link set $iface up") == 0 ||
220 die "can't activate interface '$iface'\n";
224 my ($iface, $bridge) = @_;
226 die "unable to get bridge setting\n" if !$bridge;
228 my $bridgemtu = &$read_bridge_mtu($bridge);
231 PVE
::Tools
::run_command
("/sbin/ifconfig $iface 0.0.0.0 promisc up mtu $bridgemtu");
233 die "interface activation failed\n" if $@;
237 my ($veth, $vethpeer, $bridge, $mac) = @_;
239 die "unable to get bridge setting\n" if !$bridge;
241 my $bridgemtu = &$read_bridge_mtu($bridge);
244 if (! -d
"/sys/class/net/$veth") {
245 my $cmd = "/sbin/ip link add name $veth type veth peer name $vethpeer mtu $bridgemtu";
246 $cmd .= " addr $mac" if $mac;
247 system($cmd) == 0 || die "can't create interface $veth\n";
251 &$activate_interface($veth);
252 &$activate_interface($vethpeer);
258 if (-d
"/sys/class/net/$veth") {
259 run_command
("/sbin/ip link delete dev $veth", outfunc
=> sub {}, errfunc
=> sub {});
264 my $create_firewall_bridge_linux = sub {
265 my ($iface, $bridge, $tag, $trunks) = @_;
267 my ($vmid, $devid) = &$parse_tap_device_name($iface);
268 my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid);
270 &$cond_create_bridge($fwbr);
271 &$activate_interface($fwbr);
273 copy_bridge_config
($bridge, $fwbr);
274 veth_create
($vethfw, $vethfwpeer, $bridge);
276 &$bridge_add_interface($fwbr, $vethfw);
277 &$bridge_add_interface($bridge, $vethfwpeer, $tag, $trunks);
279 &$bridge_add_interface($fwbr, $iface);
282 my $create_firewall_bridge_ovs = sub {
283 my ($iface, $bridge, $tag, $trunks) = @_;
285 my ($vmid, $devid) = &$parse_tap_device_name($iface);
286 my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
288 my $bridgemtu = &$read_bridge_mtu($bridge);
290 &$cond_create_bridge($fwbr);
291 &$activate_interface($fwbr);
293 &$bridge_add_interface($fwbr, $iface);
295 &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1, $trunks);
296 &$activate_interface($ovsintport);
298 # set the same mtu for ovs int port
299 PVE
::Tools
::run_command
("/sbin/ifconfig $ovsintport mtu $bridgemtu");
301 &$bridge_add_interface($fwbr, $ovsintport);
304 my $cleanup_firewall_bridge = sub {
307 my ($vmid, $devid) = &$parse_tap_device_name($iface, 1);
308 return if !defined($vmid);
309 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
311 # cleanup old port config from any openvswitch bridge
312 if (-d
"/sys/class/net/$ovsintport") {
313 run_command
("/usr/bin/ovs-vsctl del-port $ovsintport", outfunc
=> sub {}, errfunc
=> sub {});
316 # delete old vethfw interface
317 veth_delete
($vethfw);
319 # cleanup fwbr bridge
320 if (-d
"/sys/class/net/$fwbr") {
321 run_command
("/sbin/ip link set dev $fwbr down", outfunc
=> sub {}, errfunc
=> sub {});
322 run_command
("/sbin/brctl delbr $fwbr", outfunc
=> sub {}, errfunc
=> sub {});
327 my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_;
329 #cleanup old port config from any openvswitch bridge
330 eval {run_command
("/usr/bin/ovs-vsctl del-port $iface", outfunc
=> sub {}, errfunc
=> sub {}) };
332 if (-d
"/sys/class/net/$bridge/bridge") {
333 &$cleanup_firewall_bridge($iface); # remove stale devices
335 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/bridge/vlan_filtering");
338 die "vlan aware feature need to be enabled to use trunks" if $trunks;
339 my $newbridge = activate_bridge_vlan
($bridge, $tag);
340 copy_bridge_config
($bridge, $newbridge) if $bridge ne $newbridge;
341 $bridge = $newbridge;
346 &$create_firewall_bridge_linux($iface, $bridge, $tag, $trunks);
348 &$bridge_add_interface($bridge, $iface, $tag, $trunks);
352 &$cleanup_firewall_bridge($iface); # remove stale devices
355 &$create_firewall_bridge_ovs($iface, $bridge, $tag, $trunks);
357 &$ovs_bridge_add_port($bridge, $iface, $tag, undef, $trunks);
361 tap_rate_limit
($iface, $rate);
367 my $path= "/sys/class/net/$iface/brport/bridge";
369 my $bridge = basename
(readlink($path));
370 #avoid insecure dependency;
371 ($bridge) = $bridge =~ /(\S+)/;
373 system("/sbin/brctl delif $bridge $iface") == 0 ||
374 die "can't del interface '$iface' from bridge '$bridge'\n";
378 &$cleanup_firewall_bridge($iface);
379 #cleanup old port config from any openvswitch bridge
380 eval {run_command
("/usr/bin/ovs-vsctl del-port $iface", outfunc
=> sub {}, errfunc
=> sub {}) };
383 sub copy_bridge_config
{
384 my ($br0, $br1) = @_;
386 return if $br0 eq $br1;
388 my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
389 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier'];
391 foreach my $sysname (@$br_configs) {
393 my $v0 = PVE
::Tools
::file_read_firstline
("/sys/class/net/$br0/bridge/$sysname");
394 my $v1 = PVE
::Tools
::file_read_firstline
("/sys/class/net/$br1/bridge/$sysname");
396 PVE
::ProcFSTools
::write_proc_entry
("/sys/class/net/$br1/bridge/$sysname", $v0);
403 sub activate_bridge_vlan_slave
{
404 my ($bridgevlan, $iface, $tag) = @_;
405 my $ifacevlan = "${iface}.$tag";
407 # create vlan on $iface is not already exist
408 if (! -d
"/sys/class/net/$ifacevlan") {
409 system("/sbin/ip link add link $iface name ${iface}.${tag} type vlan id $tag") == 0 ||
410 die "can't add vlan tag $tag to interface $iface\n";
413 # be sure to have the $ifacevlan up
414 &$activate_interface($ifacevlan);
416 # test if $vlaniface is already enslaved in another bridge
417 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
419 my $tbridge = basename
(readlink($path));
420 if ($tbridge ne $bridgevlan) {
421 die "interface $ifacevlan already exist in bridge $tbridge\n";
423 # Port already attached to bridge: do nothing.
428 # add $ifacevlan to the bridge
429 &$bridge_add_interface($bridgevlan, $ifacevlan);
432 sub activate_bridge_vlan
{
433 my ($bridge, $tag_param) = @_;
435 die "bridge '$bridge' is not active\n" if ! -d
"/sys/class/net/$bridge";
437 return $bridge if !defined($tag_param); # no vlan, simply return
439 my $tag = int($tag_param);
441 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
443 my $bridgevlan = "${bridge}v$tag";
446 my $dir = "/sys/class/net/$bridge/brif";
447 PVE
::Tools
::dir_glob_foreach
($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub {
451 die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0;
453 # add bridgevlan if it doesn't already exist
454 if (! -d
"/sys/class/net/$bridgevlan") {
455 system("/sbin/brctl addbr $bridgevlan") == 0 ||
456 die "can't add bridge $bridgevlan\n";
459 # for each physical interface (eth or bridge) bind them to bridge vlan
460 foreach my $iface (@ifaces) {
461 activate_bridge_vlan_slave
($bridgevlan, $iface, $tag);
464 #fixme: set other bridge flags
466 # be sure to have the bridge up
467 system("/sbin/ip link set $bridgevlan up") == 0 ||
468 die "can't up bridge $bridgevlan\n";
474 my ($host, $port, $timeout) = @_;
478 $timeout = 3 if !$timeout; # sane default
480 # Net::Ping defaults to the echo port
483 # Net::Ping's port_number() implies service_check(1)
489 $result = PVE
::Tools
::run_with_timeout
($timeout, sub {
490 $sock = IO
::Socket
::IP-
>new(PeerHost
=> $host, PeerPort
=> $port, Type
=> SOCK_STREAM
);
491 $result = $refused if $! == ECONNREFUSED
;
502 my ($cidr, $version) = @_;
504 return if $cidr !~ m!^(\S+?)/(\S+)$!;
505 my ($ip, $prefix) = ($1, $2);
507 my $ipobj = Net
::IP-
>new($ip, $version);
510 $version = $ipobj->version();
512 my $binmask = Net
::IP
::ip_get_mask
($prefix, $version);
515 my $masked_binip = $ipobj->binip() & $binmask;
516 my $masked_ip = Net
::IP
::ip_bintoip
($masked_binip, $version);
517 return Net
::IP-
>new("$masked_ip/$prefix");
521 my ($ip, $cidr, $version) = @_;
523 my $cidr_obj = IP_from_cidr
($cidr, $version);
524 return undef if !$cidr_obj;
526 my $ip_obj = Net
::IP-
>new($ip, $version);
527 return undef if !$ip_obj;
529 return $cidr_obj->overlaps($ip_obj) == $Net::IP
::IP_B_IN_A_OVERLAP
;
532 # struct ifreq { // FOR SIOCGIFFLAGS:
533 # char ifrn_name[IFNAMSIZ]
536 my $STRUCT_IFREQ_SIOCGIFFLAGS = 'Z' . IFNAMSIZ
. 's1';
537 sub get_active_interfaces
{
538 # Use the interface name list from /proc/net/dev
539 open my $fh, '<', '/proc/net/dev'
540 or die "failed to open /proc/net/dev: $!\n";
541 # And filter by IFF_UP flag fetched via a PF_INET6 socket ioctl:
543 socket($sock, PF_INET6
, SOCK_DGRAM
, &IPPROTO_IP
)
544 or socket($sock, PF_INET
, SOCK_DGRAM
, &IPPROTO_IP
)
548 while(defined(my $line = <$fh>)) {
549 next if $line !~ /^\s*([^:\s]+):/;
551 my $ifreq = pack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifname, 0);
552 if (!defined(ioctl($sock, SIOCGIFFLAGS
, $ifreq))) {
553 warn "failed to get interface flags for: $ifname\n";
556 my ($name, $flags) = unpack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifreq);
557 push @$ifaces, $ifname if ($flags & IFF_UP
);