add firewall bridge support for linux bridge
[pve-common.git] / data / PVE / Network.pm
1 package PVE::Network;
2
3 use strict;
4 use warnings;
5 use PVE::Tools qw(run_command);
6 use PVE::ProcFSTools;
7 use PVE::INotify;
8 use File::Basename;
9
10 # host network related utility functions
11
12 sub setup_tc_rate_limit {
13 my ($iface, $rate, $burst, $debug) = @_;
14
15 system("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1");
16 system("/sbin/tc filter del dev $iface parent ffff: protocol ip prio 50 estimator 1sec 8sec >/dev/null 2>&1");
17 system("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1");
18 system("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1");
19
20 return if !$rate;
21
22 run_command("/sbin/tc qdisc add dev $iface handle ffff: ingress");
23
24 # this does not work wit virtio - don't know why (setting "mtu 64kb" does not help)
25 #run_command("/sbin/tc filter add dev $iface parent ffff: protocol ip prio 50 u32 match ip src 0.0.0.0/0 police rate ${rate}bps burst ${burst}b drop flowid :1");
26 # so we use avrate instead
27 run_command("/sbin/tc filter add dev $iface parent ffff: " .
28 "protocol ip prio 50 estimator 1sec 8sec " .
29 "u32 match ip src 0.0.0.0/0 police avrate ${rate}bps drop flowid :1");
30
31 # tbf does not work for unknown reason
32 #$TC qdisc add dev $DEV root tbf rate $RATE latency 100ms burst $BURST
33 # so we use htb instead
34 run_command("/sbin/tc qdisc add dev $iface root handle 1: htb default 1");
35 run_command("/sbin/tc class add dev $iface parent 1: classid 1:1 " .
36 "htb rate ${rate}bps burst ${burst}b");
37
38 if ($debug) {
39 print "DEBUG tc settings\n";
40 system("/sbin/tc qdisc ls dev $iface");
41 system("/sbin/tc class ls dev $iface");
42 system("/sbin/tc filter ls dev $iface parent ffff:");
43 }
44 }
45
46 sub tap_rate_limit {
47 my ($iface, $rate) = @_;
48
49 my $debug = 0;
50 $rate = int($rate*1024*1024);
51 my $burst = 1024*1024;
52
53 setup_tc_rate_limit($iface, $rate, $burst, $debug);
54 }
55
56 my $read_bridge_mtu = sub {
57 my ($bridge) = @_;
58
59 my $mtu = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/mtu");
60 die "bridge '$bridge' does not exist\n" if !$mtu;
61 # avoid insecure dependency;
62 die "unable to parse mtu value" if $mtu !~ /^(\d+)$/;
63 $mtu = int($1);
64
65 return $mtu;
66 };
67
68 my $parse_tap_devive_name = sub {
69 my ($iface) = @_;
70
71 my ($vmid, $devid);
72
73 if ($iface =~ m/^tap(\d+)i(\d+)$/) {
74 $vmid = $1;
75 $devid = $2;
76 } elsif ($iface =~ m/^veth(\d+)\.(\d+)$/) {
77 $vmid = $1;
78 $devid = $2;
79 } else {
80 die "wrong interface name $iface";
81 }
82
83 return ($vmid, $devid);
84 };
85
86 my $compute_fwbr_names_linux = sub {
87 my ($vmid, $devid) = @_;
88
89 my $fwbr = "fwbr${vmid}i${devid}";
90 my $vethfw = "link${vmid}i${devid}";
91 my $vethfwpeer = "link${vmid}p${devid}";
92
93 return ($fwbr, $vethfw, $vethfwpeer);
94 };
95
96 my $compute_ovs_firewall_port_name = sub {
97 my ($vmid, $devid) = @_;
98
99 return "fwint${vmid}i${devid}";
100 };
101
102 my $cond_create_bridge = sub {
103 my ($bridge) = @_;
104
105 if (! -d "/sys/class/net/$bridge") {
106 system("/sbin/brctl addbr $bridge") == 0 ||
107 die "can't add bridge '$bridge'\n";
108 }
109 };
110
111 my $bridge_add_interface = sub {
112 my ($bridge, $iface) = @_;
113
114 system("/sbin/brctl addif $bridge $iface") == 0 ||
115 die "can't add interface 'iface' to bridge '$bridge'\n";
116 };
117
118 my $activate_interface = sub {
119 my ($iface) = @_;
120
121 system("/sbin/ip link set $iface up") == 0 ||
122 die "can't activate interface '$iface'\n";
123 };
124
125 sub tap_create {
126 my ($iface, $bridge) = @_;
127
128 die "unable to get bridge setting\n" if !$bridge;
129
130 my $bridgemtu = &$read_bridge_mtu($bridge);
131
132 eval {
133 PVE::Tools::run_command("/sbin/ifconfig $iface 0.0.0.0 promisc up mtu $bridgemtu");
134 };
135 die "interface activation failed\n" if $@;
136 }
137
138
139 my $create_firewall_bridge_linux = sub {
140 my ($iface, $bridge) = @_;
141
142 my ($vmid, $devid) = &$parse_tap_devive_name($iface);
143 my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names_linux($vmid, $devid);
144
145 my $bridgemtu = &$read_bridge_mtu($bridge);
146
147 &$cond_create_bridge($fwbr);
148 &$activate_interface($fwbr);
149
150 copy_bridge_config($bridge, $fwbr);
151 # create veth pair
152 if (! -d "/sys/class/net/$vethfw") {
153 system("/sbin/ip link add name $vethfw type veth peer name $vethfwpeer mtu $bridgemtu") == 0 ||
154 die "can't create interface $vethfw\n";
155 }
156
157 # up vethpair
158 &$activate_interface($vethfw);
159 &$activate_interface($vethfwpeer);
160
161 &$bridge_add_interface($bridge, $vethfw);
162 &$bridge_add_interface($fwbr, $vethfwpeer);
163
164 return $fwbr;
165 };
166
167 my $cleanup_firewall_bridge_linux = sub {
168 my ($iface) = @_;
169
170 my ($vmid, $devid) = &$parse_tap_devive_name($iface);
171 my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names_linux($vmid, $devid);
172
173 # delete old vethfw interface
174 if (-d "/sys/class/net/$vethfw") {
175 run_command("/sbin/ip link delete dev $vethfw", outfunc => sub {}, errfunc => sub {});
176 }
177
178 # cleanup fwbr bridge
179 if (-d "/sys/class/net/$fwbr") {
180 run_command("/sbin/ip link set dev $fwbr down", outfunc => sub {}, errfunc => sub {});
181 run_command("/sbin/brctl delbr $fwbr", outfunc => sub {}, errfunc => sub {});
182 }
183 };
184
185 sub tap_plug {
186 my ($iface, $bridge, $tag, $firewall) = @_;
187
188 #cleanup old port config from any openvswitch bridge
189 eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) };
190
191 if (-d "/sys/class/net/$bridge/bridge") {
192 &$cleanup_firewall_bridge_linux($iface); # remove stale devices
193
194 my $newbridge = activate_bridge_vlan($bridge, $tag);
195 copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge;
196
197 $newbridge = &$create_firewall_bridge_linux($iface, $newbridge) if $firewall;
198
199 &$bridge_add_interface($newbridge, $iface);
200 } else {
201 my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface";
202 $cmd .= " tag=$tag" if $tag;
203 system($cmd) == 0 ||
204 die "can't add interface to bridge\n";
205 }
206 }
207
208 sub tap_unplug {
209 my ($iface) = @_;
210
211 my $path= "/sys/class/net/$iface/brport/bridge";
212 if (-l $path) {
213 my $bridge = basename(readlink($path));
214 #avoid insecure dependency;
215 ($bridge) = $bridge =~ /(\S+)/;
216
217 system("/sbin/brctl delif $bridge $iface") == 0 ||
218 die "can't del interface '$iface' from bridge '$bridge'\n";
219
220 &$cleanup_firewall_bridge_linux($iface);
221 } else {
222 system ("/usr/bin/ovs-vsctl del-port $iface") == 0 ||
223 die "can't del ovs port '$iface'\n";
224 }
225 }
226
227 sub copy_bridge_config {
228 my ($br0, $br1) = @_;
229
230 return if $br0 eq $br1;
231
232 my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
233 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier'];
234
235 foreach my $sysname (@$br_configs) {
236 eval {
237 my $v0 = PVE::Tools::file_read_firstline("/sys/class/net/$br0/bridge/$sysname");
238 my $v1 = PVE::Tools::file_read_firstline("/sys/class/net/$br1/bridge/$sysname");
239 if ($v0 ne $v1) {
240 PVE::ProcFSTools::write_proc_entry("/sys/class/net/$br1/bridge/$sysname", $v0);
241 }
242 };
243 warn $@ if $@;
244 }
245 }
246
247 sub activate_bridge_vlan_slave {
248 my ($bridgevlan, $iface, $tag) = @_;
249 my $ifacevlan = "${iface}.$tag";
250
251 # create vlan on $iface is not already exist
252 if (! -d "/sys/class/net/$ifacevlan") {
253 system("/sbin/vconfig add $iface $tag") == 0 ||
254 die "can't add vlan tag $tag to interface $iface\n";
255 }
256
257 # be sure to have the $ifacevlan up
258 &$activate_interface($ifacevlan);
259
260 # test if $vlaniface is already enslaved in another bridge
261 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
262 if (-l $path) {
263 my $tbridge = basename(readlink($path));
264 if ($tbridge ne $bridgevlan) {
265 die "interface $ifacevlan already exist in bridge $tbridge\n";
266 } else {
267 # Port already attached to bridge: do nothing.
268 return;
269 }
270 }
271
272 # add $ifacevlan to the bridge
273 &$bridge_add_interface($bridgevlan, $ifacevlan);
274 }
275
276 sub activate_bridge_vlan {
277 my ($bridge, $tag_param) = @_;
278
279 die "bridge '$bridge' is not active\n" if ! -d "/sys/class/net/$bridge";
280
281 return $bridge if !defined($tag_param); # no vlan, simply return
282
283 my $tag = int($tag_param);
284
285 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
286
287 my $bridgevlan = "${bridge}v$tag";
288
289 my @ifaces = ();
290 my $dir = "/sys/class/net/$bridge/brif";
291 PVE::Tools::dir_glob_foreach($dir, '((eth|bond)\d+)', sub {
292 push @ifaces, $_[0];
293 });
294
295 die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0;
296
297 # add bridgevlan if it doesn't already exist
298 if (! -d "/sys/class/net/$bridgevlan") {
299 system("/sbin/brctl addbr $bridgevlan") == 0 ||
300 die "can't add bridge $bridgevlan\n";
301 }
302
303 # for each physical interface (eth or bridge) bind them to bridge vlan
304 foreach my $iface (@ifaces) {
305 activate_bridge_vlan_slave($bridgevlan, $iface, $tag);
306 }
307
308 #fixme: set other bridge flags
309
310 # be sure to have the bridge up
311 system("/sbin/ip link set $bridgevlan up") == 0 ||
312 die "can't up bridge $bridgevlan\n";
313
314 return $bridgevlan;
315 }
316
317 1;