5 use PVE
::Tools
qw(run_command);
10 use POSIX
qw(ECONNREFUSED);
12 # host network related utility functions
14 our $ipv4_reverse_mask = [
50 our $ipv4_mask_hash_localnet = {
52 '255.255.128.0' => 17,
53 '255.255.192.0' => 18,
54 '255.255.224.0' => 19,
55 '255.255.240.0' => 20,
56 '255.255.248.0' => 21,
57 '255.255.252.0' => 22,
58 '255.255.254.0' => 23,
59 '255.255.255.0' => 24,
60 '255.255.255.128' => 25,
61 '255.255.255.192' => 26,
62 '255.255.255.224' => 27,
63 '255.255.255.240' => 28,
64 '255.255.255.248' => 29,
65 '255.255.255.252' => 30,
68 sub setup_tc_rate_limit
{
69 my ($iface, $rate, $burst, $debug) = @_;
71 system("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1");
72 system("/sbin/tc filter del dev $iface parent ffff: protocol all pref 50 u32 >/dev/null 2>&1");
73 system("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1");
74 system("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1");
78 # tbf does not work for unknown reason
79 #$TC qdisc add dev $DEV root tbf rate $RATE latency 100ms burst $BURST
80 # so we use htb instead
81 run_command
("/sbin/tc qdisc add dev $iface root handle 1: htb default 1");
82 run_command
("/sbin/tc class add dev $iface parent 1: classid 1:1 " .
83 "htb rate ${rate}bps burst ${burst}b");
85 run_command
("/sbin/tc qdisc add dev $iface handle ffff: ingress");
86 run_command
("/sbin/tc filter add dev $iface parent ffff: " .
87 "protocol all prio 50 u32 match u32 0 0 " .
88 "police rate ${rate}bps burst ${burst}b mtu 64kb " .
92 print "DEBUG tc settings\n";
93 system("/sbin/tc qdisc ls dev $iface");
94 system("/sbin/tc class ls dev $iface");
95 system("/sbin/tc filter ls dev $iface parent ffff:");
100 my ($iface, $rate) = @_;
103 $rate = int($rate*1024*1024);
104 my $burst = 1024*1024;
106 setup_tc_rate_limit
($iface, $rate, $burst, $debug);
109 my $read_bridge_mtu = sub {
112 my $mtu = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/mtu");
113 die "bridge '$bridge' does not exist\n" if !$mtu;
114 # avoid insecure dependency;
115 die "unable to parse mtu value" if $mtu !~ /^(\d+)$/;
121 my $parse_tap_device_name = sub {
122 my ($iface, $noerr) = @_;
126 if ($iface =~ m/^tap(\d+)i(\d+)$/) {
129 } elsif ($iface =~ m/^veth(\d+)i(\d+)$/) {
133 return undef if $noerr;
134 die "can't create firewall bridge for random interface name '$iface'\n";
137 return ($vmid, $devid);
140 my $compute_fwbr_names = sub {
141 my ($vmid, $devid) = @_;
143 my $fwbr = "fwbr${vmid}i${devid}";
144 # Note: the firewall use 'fwln+' to filter traffic to VMs
145 my $vethfw = "fwln${vmid}i${devid}";
146 my $vethfwpeer = "fwpr${vmid}p${devid}";
147 my $ovsintport = "fwln${vmid}o${devid}";
149 return ($fwbr, $vethfw, $vethfwpeer, $ovsintport);
152 my $cond_create_bridge = sub {
155 if (! -d
"/sys/class/net/$bridge") {
156 system("/sbin/brctl addbr $bridge") == 0 ||
157 die "can't add bridge '$bridge'\n";
161 my $bridge_add_interface = sub {
162 my ($bridge, $iface, $tag) = @_;
164 system("/sbin/brctl addif $bridge $iface") == 0 ||
165 die "can't add interface 'iface' to bridge '$bridge'\n";
167 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/bridge/vlan_filtering");
171 system("/sbin/bridge vlan add dev $iface vid $tag pvid untagged") == 0 ||
172 die "unable to add vlan $tag to interface $iface\n";
174 system("/sbin/bridge vlan add dev $iface vid 2-4094") == 0 ||
175 die "unable to add vlan $tag to interface $iface\n";
180 my $ovs_bridge_add_port = sub {
181 my ($bridge, $iface, $tag, $internal) = @_;
183 my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface";
184 $cmd .= " tag=$tag" if $tag;
185 $cmd .= " -- set Interface $iface type=internal" if $internal;
187 die "can't add ovs port '$iface'\n";
190 my $activate_interface = sub {
193 system("/sbin/ip link set $iface up") == 0 ||
194 die "can't activate interface '$iface'\n";
198 my ($iface, $bridge) = @_;
200 die "unable to get bridge setting\n" if !$bridge;
202 my $bridgemtu = &$read_bridge_mtu($bridge);
205 PVE
::Tools
::run_command
("/sbin/ifconfig $iface 0.0.0.0 promisc up mtu $bridgemtu");
207 die "interface activation failed\n" if $@;
211 my ($veth, $vethpeer, $bridge, $mac) = @_;
213 die "unable to get bridge setting\n" if !$bridge;
215 my $bridgemtu = &$read_bridge_mtu($bridge);
218 if (! -d
"/sys/class/net/$veth") {
219 my $cmd = "/sbin/ip link add name $veth type veth peer name $vethpeer mtu $bridgemtu";
220 $cmd .= " addr $mac" if $mac;
221 system($cmd) == 0 || die "can't create interface $veth\n";
225 &$activate_interface($veth);
226 &$activate_interface($vethpeer);
232 if (-d
"/sys/class/net/$veth") {
233 run_command
("/sbin/ip link delete dev $veth", outfunc
=> sub {}, errfunc
=> sub {});
238 my $create_firewall_bridge_linux = sub {
239 my ($iface, $bridge, $tag) = @_;
241 my ($vmid, $devid) = &$parse_tap_device_name($iface);
242 my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid);
244 &$cond_create_bridge($fwbr);
245 &$activate_interface($fwbr);
247 copy_bridge_config
($bridge, $fwbr);
248 veth_create
($vethfw, $vethfwpeer, $bridge);
250 &$bridge_add_interface($fwbr, $vethfw);
251 &$bridge_add_interface($bridge, $vethfwpeer, $tag);
253 &$bridge_add_interface($fwbr, $iface);
256 my $create_firewall_bridge_ovs = sub {
257 my ($iface, $bridge, $tag) = @_;
259 my ($vmid, $devid) = &$parse_tap_device_name($iface);
260 my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
262 my $bridgemtu = &$read_bridge_mtu($bridge);
264 &$cond_create_bridge($fwbr);
265 &$activate_interface($fwbr);
267 &$bridge_add_interface($fwbr, $iface);
269 &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1);
270 &$activate_interface($ovsintport);
272 # set the same mtu for ovs int port
273 PVE
::Tools
::run_command
("/sbin/ifconfig $ovsintport mtu $bridgemtu");
275 &$bridge_add_interface($fwbr, $ovsintport);
278 my $cleanup_firewall_bridge = sub {
281 my ($vmid, $devid) = &$parse_tap_device_name($iface, 1);
282 return if !defined($vmid);
283 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
285 # cleanup old port config from any openvswitch bridge
286 if (-d
"/sys/class/net/$ovsintport") {
287 run_command
("/usr/bin/ovs-vsctl del-port $ovsintport", outfunc
=> sub {}, errfunc
=> sub {});
290 # delete old vethfw interface
291 veth_delete
($vethfw);
293 # cleanup fwbr bridge
294 if (-d
"/sys/class/net/$fwbr") {
295 run_command
("/sbin/ip link set dev $fwbr down", outfunc
=> sub {}, errfunc
=> sub {});
296 run_command
("/sbin/brctl delbr $fwbr", outfunc
=> sub {}, errfunc
=> sub {});
301 my ($iface, $bridge, $tag, $firewall) = @_;
303 #cleanup old port config from any openvswitch bridge
304 eval {run_command
("/usr/bin/ovs-vsctl del-port $iface", outfunc
=> sub {}, errfunc
=> sub {}) };
306 if (-d
"/sys/class/net/$bridge/bridge") {
307 &$cleanup_firewall_bridge($iface); # remove stale devices
309 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/bridge/vlan_filtering");
312 my $newbridge = activate_bridge_vlan
($bridge, $tag);
313 copy_bridge_config
($bridge, $newbridge) if $bridge ne $newbridge;
314 $bridge = $newbridge;
319 &$create_firewall_bridge_linux($iface, $bridge, $tag);
321 &$bridge_add_interface($bridge, $iface, $tag);
325 &$cleanup_firewall_bridge($iface); # remove stale devices
328 &$create_firewall_bridge_ovs($iface, $bridge, $tag);
330 &$ovs_bridge_add_port($bridge, $iface, $tag);
338 my $path= "/sys/class/net/$iface/brport/bridge";
340 my $bridge = basename
(readlink($path));
341 #avoid insecure dependency;
342 ($bridge) = $bridge =~ /(\S+)/;
344 system("/sbin/brctl delif $bridge $iface") == 0 ||
345 die "can't del interface '$iface' from bridge '$bridge'\n";
349 &$cleanup_firewall_bridge($iface);
352 sub copy_bridge_config
{
353 my ($br0, $br1) = @_;
355 return if $br0 eq $br1;
357 my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
358 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier'];
360 foreach my $sysname (@$br_configs) {
362 my $v0 = PVE
::Tools
::file_read_firstline
("/sys/class/net/$br0/bridge/$sysname");
363 my $v1 = PVE
::Tools
::file_read_firstline
("/sys/class/net/$br1/bridge/$sysname");
365 PVE
::ProcFSTools
::write_proc_entry
("/sys/class/net/$br1/bridge/$sysname", $v0);
372 sub activate_bridge_vlan_slave
{
373 my ($bridgevlan, $iface, $tag) = @_;
374 my $ifacevlan = "${iface}.$tag";
376 # create vlan on $iface is not already exist
377 if (! -d
"/sys/class/net/$ifacevlan") {
378 system("/sbin/ip link add link $iface name ${iface}.${tag} type vlan id $tag") == 0 ||
379 die "can't add vlan tag $tag to interface $iface\n";
382 # be sure to have the $ifacevlan up
383 &$activate_interface($ifacevlan);
385 # test if $vlaniface is already enslaved in another bridge
386 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
388 my $tbridge = basename
(readlink($path));
389 if ($tbridge ne $bridgevlan) {
390 die "interface $ifacevlan already exist in bridge $tbridge\n";
392 # Port already attached to bridge: do nothing.
397 # add $ifacevlan to the bridge
398 &$bridge_add_interface($bridgevlan, $ifacevlan);
401 sub activate_bridge_vlan
{
402 my ($bridge, $tag_param) = @_;
404 die "bridge '$bridge' is not active\n" if ! -d
"/sys/class/net/$bridge";
406 return $bridge if !defined($tag_param); # no vlan, simply return
408 my $tag = int($tag_param);
410 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
412 my $bridgevlan = "${bridge}v$tag";
415 my $dir = "/sys/class/net/$bridge/brif";
416 PVE
::Tools
::dir_glob_foreach
($dir, '((eth|bond)\d+(\.\d+)?)', sub {
420 die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0;
422 # add bridgevlan if it doesn't already exist
423 if (! -d
"/sys/class/net/$bridgevlan") {
424 system("/sbin/brctl addbr $bridgevlan") == 0 ||
425 die "can't add bridge $bridgevlan\n";
428 # for each physical interface (eth or bridge) bind them to bridge vlan
429 foreach my $iface (@ifaces) {
430 activate_bridge_vlan_slave
($bridgevlan, $iface, $tag);
433 #fixme: set other bridge flags
435 # be sure to have the bridge up
436 system("/sbin/ip link set $bridgevlan up") == 0 ||
437 die "can't up bridge $bridgevlan\n";
443 my ($host, $port, $timeout) = @_;
447 $timeout = 3 if !$timeout; # sane default
449 # Net::Ping defaults to the echo port
452 # Net::Ping's port_number() implies service_check(1)
458 $result = PVE
::Tools
::run_with_timeout
($timeout, sub {
459 $sock = IO
::Socket
::IP-
>new(PeerHost
=> $host, PeerPort
=> $port, Type
=> SOCK_STREAM
);
460 $result = $refused if $! == ECONNREFUSED
;