5 use PVE
::Tools
qw(run_command);
10 # host network related utility functions
12 our $ipv4_reverse_mask = [
48 our $ipv4_mask_hash_localnet = {
50 '255.255.128.0' => 17,
51 '255.255.192.0' => 18,
52 '255.255.224.0' => 19,
53 '255.255.240.0' => 20,
54 '255.255.248.0' => 21,
55 '255.255.252.0' => 22,
56 '255.255.254.0' => 23,
57 '255.255.255.0' => 24,
58 '255.255.255.128' => 25,
59 '255.255.255.192' => 26,
60 '255.255.255.224' => 27,
61 '255.255.255.240' => 28,
62 '255.255.255.248' => 29,
63 '255.255.255.252' => 30,
66 sub setup_tc_rate_limit
{
67 my ($iface, $rate, $burst, $debug) = @_;
69 system("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1");
70 system("/sbin/tc filter del dev $iface parent ffff: protocol all pref 50 u32 >/dev/null 2>&1");
71 system("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1");
72 system("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1");
76 # tbf does not work for unknown reason
77 #$TC qdisc add dev $DEV root tbf rate $RATE latency 100ms burst $BURST
78 # so we use htb instead
79 run_command
("/sbin/tc qdisc add dev $iface root handle 1: htb default 1");
80 run_command
("/sbin/tc class add dev $iface parent 1: classid 1:1 " .
81 "htb rate ${rate}bps burst ${burst}b");
83 run_command
("/sbin/tc qdisc add dev $iface handle ffff: ingress");
84 run_command
("/sbin/tc filter add dev $iface parent ffff: " .
85 "protocol all prio 50 u32 match u32 0 0 " .
86 "police rate ${rate}bps burst ${burst}b mtu 64kb " .
90 print "DEBUG tc settings\n";
91 system("/sbin/tc qdisc ls dev $iface");
92 system("/sbin/tc class ls dev $iface");
93 system("/sbin/tc filter ls dev $iface parent ffff:");
98 my ($iface, $rate) = @_;
101 $rate = int($rate*1024*1024);
102 my $burst = 1024*1024;
104 setup_tc_rate_limit
($iface, $rate, $burst, $debug);
107 my $read_bridge_mtu = sub {
110 my $mtu = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/mtu");
111 die "bridge '$bridge' does not exist\n" if !$mtu;
112 # avoid insecure dependency;
113 die "unable to parse mtu value" if $mtu !~ /^(\d+)$/;
119 my $parse_tap_device_name = sub {
120 my ($iface, $noerr) = @_;
124 if ($iface =~ m/^tap(\d+)i(\d+)$/) {
127 } elsif ($iface =~ m/^veth(\d+)i(\d+)$/) {
131 return undef if $noerr;
132 die "can't create firewall bridge for random interface name '$iface'\n";
135 return ($vmid, $devid);
138 my $compute_fwbr_names = sub {
139 my ($vmid, $devid) = @_;
141 my $fwbr = "fwbr${vmid}i${devid}";
142 # Note: the firewall use 'fwln+' to filter traffic to VMs
143 my $vethfw = "fwln${vmid}i${devid}";
144 my $vethfwpeer = "fwpr${vmid}p${devid}";
145 my $ovsintport = "fwln${vmid}o${devid}";
147 return ($fwbr, $vethfw, $vethfwpeer, $ovsintport);
150 my $cond_create_bridge = sub {
153 if (! -d
"/sys/class/net/$bridge") {
154 system("/sbin/brctl addbr $bridge") == 0 ||
155 die "can't add bridge '$bridge'\n";
159 my $bridge_add_interface = sub {
160 my ($bridge, $iface, $tag) = @_;
162 system("/sbin/brctl addif $bridge $iface") == 0 ||
163 die "can't add interface 'iface' to bridge '$bridge'\n";
165 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/bridge/vlan_filtering");
169 system("/sbin/bridge vlan add dev $iface vid $tag pvid untagged") == 0 ||
170 die "unable to add vlan $tag to interface $iface\n";
172 system("/sbin/bridge vlan add dev $iface vid 2-4094") == 0 ||
173 die "unable to add vlan $tag to interface $iface\n";
178 my $ovs_bridge_add_port = sub {
179 my ($bridge, $iface, $tag, $internal) = @_;
181 my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface";
182 $cmd .= " tag=$tag" if $tag;
183 $cmd .= " -- set Interface $iface type=internal" if $internal;
185 die "can't add ovs port '$iface'\n";
188 my $activate_interface = sub {
191 system("/sbin/ip link set $iface up") == 0 ||
192 die "can't activate interface '$iface'\n";
196 my ($iface, $bridge) = @_;
198 die "unable to get bridge setting\n" if !$bridge;
200 my $bridgemtu = &$read_bridge_mtu($bridge);
203 PVE
::Tools
::run_command
("/sbin/ifconfig $iface 0.0.0.0 promisc up mtu $bridgemtu");
205 die "interface activation failed\n" if $@;
209 my ($veth, $vethpeer, $bridge, $mac) = @_;
211 die "unable to get bridge setting\n" if !$bridge;
213 my $bridgemtu = &$read_bridge_mtu($bridge);
216 if (! -d
"/sys/class/net/$veth") {
217 my $cmd = "/sbin/ip link add name $veth type veth peer name $vethpeer mtu $bridgemtu";
218 $cmd .= " addr $mac" if $mac;
219 system($cmd) == 0 || die "can't create interface $veth\n";
223 &$activate_interface($veth);
224 &$activate_interface($vethpeer);
230 if (-d
"/sys/class/net/$veth") {
231 run_command
("/sbin/ip link delete dev $veth", outfunc
=> sub {}, errfunc
=> sub {});
236 my $create_firewall_bridge_linux = sub {
237 my ($iface, $bridge, $tag) = @_;
239 my ($vmid, $devid) = &$parse_tap_device_name($iface);
240 my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid);
242 &$cond_create_bridge($fwbr);
243 &$activate_interface($fwbr);
245 copy_bridge_config
($bridge, $fwbr);
246 veth_create
($vethfw, $vethfwpeer, $bridge);
248 &$bridge_add_interface($fwbr, $vethfw);
249 &$bridge_add_interface($bridge, $vethfwpeer, $tag);
251 &$bridge_add_interface($fwbr, $iface);
254 my $create_firewall_bridge_ovs = sub {
255 my ($iface, $bridge, $tag) = @_;
257 my ($vmid, $devid) = &$parse_tap_device_name($iface);
258 my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
260 my $bridgemtu = &$read_bridge_mtu($bridge);
262 &$cond_create_bridge($fwbr);
263 &$activate_interface($fwbr);
265 &$bridge_add_interface($fwbr, $iface);
267 &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1);
268 &$activate_interface($ovsintport);
270 # set the same mtu for ovs int port
271 PVE
::Tools
::run_command
("/sbin/ifconfig $ovsintport mtu $bridgemtu");
273 &$bridge_add_interface($fwbr, $ovsintport);
276 my $cleanup_firewall_bridge = sub {
279 my ($vmid, $devid) = &$parse_tap_device_name($iface, 1);
280 return if !defined($vmid);
281 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
283 # cleanup old port config from any openvswitch bridge
284 if (-d
"/sys/class/net/$ovsintport") {
285 run_command
("/usr/bin/ovs-vsctl del-port $ovsintport", outfunc
=> sub {}, errfunc
=> sub {});
288 # delete old vethfw interface
289 veth_delete
($vethfw);
291 # cleanup fwbr bridge
292 if (-d
"/sys/class/net/$fwbr") {
293 run_command
("/sbin/ip link set dev $fwbr down", outfunc
=> sub {}, errfunc
=> sub {});
294 run_command
("/sbin/brctl delbr $fwbr", outfunc
=> sub {}, errfunc
=> sub {});
299 my ($iface, $bridge, $tag, $firewall) = @_;
301 #cleanup old port config from any openvswitch bridge
302 eval {run_command
("/usr/bin/ovs-vsctl del-port $iface", outfunc
=> sub {}, errfunc
=> sub {}) };
304 if (-d
"/sys/class/net/$bridge/bridge") {
305 &$cleanup_firewall_bridge($iface); # remove stale devices
307 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$bridge/bridge/vlan_filtering");
310 my $newbridge = activate_bridge_vlan
($bridge, $tag);
311 copy_bridge_config
($bridge, $newbridge) if $bridge ne $newbridge;
312 $bridge = $newbridge;
317 &$create_firewall_bridge_linux($iface, $bridge, $tag);
319 &$bridge_add_interface($bridge, $iface, $tag);
323 &$cleanup_firewall_bridge($iface); # remove stale devices
326 &$create_firewall_bridge_ovs($iface, $bridge, $tag);
328 &$ovs_bridge_add_port($bridge, $iface, $tag);
336 my $path= "/sys/class/net/$iface/brport/bridge";
338 my $bridge = basename
(readlink($path));
339 #avoid insecure dependency;
340 ($bridge) = $bridge =~ /(\S+)/;
342 system("/sbin/brctl delif $bridge $iface") == 0 ||
343 die "can't del interface '$iface' from bridge '$bridge'\n";
347 &$cleanup_firewall_bridge($iface);
350 sub copy_bridge_config
{
351 my ($br0, $br1) = @_;
353 return if $br0 eq $br1;
355 my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
356 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier'];
358 foreach my $sysname (@$br_configs) {
360 my $v0 = PVE
::Tools
::file_read_firstline
("/sys/class/net/$br0/bridge/$sysname");
361 my $v1 = PVE
::Tools
::file_read_firstline
("/sys/class/net/$br1/bridge/$sysname");
363 PVE
::ProcFSTools
::write_proc_entry
("/sys/class/net/$br1/bridge/$sysname", $v0);
370 sub activate_bridge_vlan_slave
{
371 my ($bridgevlan, $iface, $tag) = @_;
372 my $ifacevlan = "${iface}.$tag";
374 # create vlan on $iface is not already exist
375 if (! -d
"/sys/class/net/$ifacevlan") {
376 system("/sbin/ip link add link $iface name ${iface}.${tag} type vlan id $tag") == 0 ||
377 die "can't add vlan tag $tag to interface $iface\n";
380 # be sure to have the $ifacevlan up
381 &$activate_interface($ifacevlan);
383 # test if $vlaniface is already enslaved in another bridge
384 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
386 my $tbridge = basename
(readlink($path));
387 if ($tbridge ne $bridgevlan) {
388 die "interface $ifacevlan already exist in bridge $tbridge\n";
390 # Port already attached to bridge: do nothing.
395 # add $ifacevlan to the bridge
396 &$bridge_add_interface($bridgevlan, $ifacevlan);
399 sub activate_bridge_vlan
{
400 my ($bridge, $tag_param) = @_;
402 die "bridge '$bridge' is not active\n" if ! -d
"/sys/class/net/$bridge";
404 return $bridge if !defined($tag_param); # no vlan, simply return
406 my $tag = int($tag_param);
408 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
410 my $bridgevlan = "${bridge}v$tag";
413 my $dir = "/sys/class/net/$bridge/brif";
414 PVE
::Tools
::dir_glob_foreach
($dir, '((eth|bond)\d+)', sub {
418 die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0;
420 # add bridgevlan if it doesn't already exist
421 if (! -d
"/sys/class/net/$bridgevlan") {
422 system("/sbin/brctl addbr $bridgevlan") == 0 ||
423 die "can't add bridge $bridgevlan\n";
426 # for each physical interface (eth or bridge) bind them to bridge vlan
427 foreach my $iface (@ifaces) {
428 activate_bridge_vlan_slave
($bridgevlan, $iface, $tag);
431 #fixme: set other bridge flags
433 # be sure to have the bridge up
434 system("/sbin/ip link set $bridgevlan up") == 0 ||
435 die "can't up bridge $bridgevlan\n";