]> git.proxmox.com Git - pve-common.git/blame - data/PVE/Network.pm
we need to match link+ rule from iptables rules, and need to have a name different
[pve-common.git] / data / PVE / Network.pm
CommitLineData
b9436cda
DM
1package PVE::Network;
2
3use strict;
c36f332e 4use warnings;
74d1b045 5use PVE::Tools qw(run_command);
b9436cda
DM
6use PVE::ProcFSTools;
7use PVE::INotify;
8use File::Basename;
9
10# host network related utility functions
11
74d1b045
DM
12sub setup_tc_rate_limit {
13 my ($iface, $rate, $burst, $debug) = @_;
14
957753df
AD
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");
edde1d46 17 system("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1");
74d1b045
DM
18 system("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1");
19
d6f2623b 20 return if !$rate;
957753df 21
74d1b045
DM
22 run_command("/sbin/tc qdisc add dev $iface handle ffff: ingress");
23
0aaf0ca4
DM
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
ca402c95 27 run_command("/sbin/tc filter add dev $iface parent ffff: " .
0aaf0ca4
DM
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");
74d1b045
DM
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
ec9ada18
AD
46sub 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}
74d1b045 55
605bb891
DM
56my $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
68my $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
70ab4434 86my $compute_fwbr_names = sub {
605bb891
DM
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}";
47c710a6 92 my $ovsintport = "link${vmid}o${devid}";
605bb891 93
70ab4434 94 return ($fwbr, $vethfw, $vethfwpeer, $ovsintport);
605bb891
DM
95};
96
97my $cond_create_bridge = sub {
98 my ($bridge) = @_;
99
100 if (! -d "/sys/class/net/$bridge") {
101 system("/sbin/brctl addbr $bridge") == 0 ||
102 die "can't add bridge '$bridge'\n";
103 }
104};
105
106my $bridge_add_interface = sub {
107 my ($bridge, $iface) = @_;
108
109 system("/sbin/brctl addif $bridge $iface") == 0 ||
110 die "can't add interface 'iface' to bridge '$bridge'\n";
111};
112
70ab4434
DM
113my $ovs_bridge_add_port = sub {
114 my ($bridge, $iface, $tag, $internal) = @_;
115
116 my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface";
117 $cmd .= " tag=$tag" if $tag;
118 $cmd .= " -- set Interface $iface type=internal" if $internal;
119 system($cmd) == 0 ||
120 die "can't add ovs port '$iface'\n";
121};
122
605bb891
DM
123my $activate_interface = sub {
124 my ($iface) = @_;
125
126 system("/sbin/ip link set $iface up") == 0 ||
127 die "can't activate interface '$iface'\n";
128};
129
3aa99c70
AD
130sub tap_create {
131 my ($iface, $bridge) = @_;
132
133 die "unable to get bridge setting\n" if !$bridge;
134
605bb891 135 my $bridgemtu = &$read_bridge_mtu($bridge);
3aa99c70 136
098795e0
DM
137 eval {
138 PVE::Tools::run_command("/sbin/ifconfig $iface 0.0.0.0 promisc up mtu $bridgemtu");
139 };
140 die "interface activation failed\n" if $@;
3aa99c70
AD
141}
142
605bb891
DM
143my $create_firewall_bridge_linux = sub {
144 my ($iface, $bridge) = @_;
145
146 my ($vmid, $devid) = &$parse_tap_devive_name($iface);
70ab4434 147 my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid);
605bb891
DM
148
149 my $bridgemtu = &$read_bridge_mtu($bridge);
150
151 &$cond_create_bridge($fwbr);
152 &$activate_interface($fwbr);
153
154 copy_bridge_config($bridge, $fwbr);
155 # create veth pair
156 if (! -d "/sys/class/net/$vethfw") {
157 system("/sbin/ip link add name $vethfw type veth peer name $vethfwpeer mtu $bridgemtu") == 0 ||
158 die "can't create interface $vethfw\n";
159 }
160
161 # up vethpair
162 &$activate_interface($vethfw);
163 &$activate_interface($vethfwpeer);
164
165 &$bridge_add_interface($bridge, $vethfw);
166 &$bridge_add_interface($fwbr, $vethfwpeer);
167
168 return $fwbr;
169};
170
70ab4434
DM
171my $create_firewall_bridge_ovs = sub {
172 my ($iface, $bridge, $tag) = @_;
173
174 my ($vmid, $devid) = &$parse_tap_devive_name($iface);
175 my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
176
177 my $bridgemtu = &$read_bridge_mtu($bridge);
178
179 &$cond_create_bridge($fwbr);
180 &$activate_interface($fwbr);
181
182 &$bridge_add_interface($fwbr, $iface);
183
184 &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1);
185
186 # set the same mtu for ovs int port
187 PVE::Tools::run_command("/sbin/ifconfig $ovsintport mtu $bridgemtu");
188
189 &$bridge_add_interface($fwbr, $ovsintport);
190};
191
192my $cleanup_firewall_bridge = sub {
605bb891
DM
193 my ($iface) = @_;
194
195 my ($vmid, $devid) = &$parse_tap_devive_name($iface);
70ab4434
DM
196 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
197
198 # cleanup old port config from any openvswitch bridge
199 if (-d "/sys/class/net/$ovsintport") {
200 run_command("/usr/bin/ovs-vsctl del-port $ovsintport", outfunc => sub {}, errfunc => sub {});
201 }
605bb891
DM
202
203 # delete old vethfw interface
204 if (-d "/sys/class/net/$vethfw") {
205 run_command("/sbin/ip link delete dev $vethfw", outfunc => sub {}, errfunc => sub {});
206 }
207
208 # cleanup fwbr bridge
209 if (-d "/sys/class/net/$fwbr") {
210 run_command("/sbin/ip link set dev $fwbr down", outfunc => sub {}, errfunc => sub {});
211 run_command("/sbin/brctl delbr $fwbr", outfunc => sub {}, errfunc => sub {});
212 }
213};
214
f0c190ee 215sub tap_plug {
605bb891 216 my ($iface, $bridge, $tag, $firewall) = @_;
f0c190ee 217
4cbabd40
AD
218 #cleanup old port config from any openvswitch bridge
219 eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) };
220
098795e0 221 if (-d "/sys/class/net/$bridge/bridge") {
70ab4434 222 &$cleanup_firewall_bridge($iface); # remove stale devices
605bb891 223
098795e0
DM
224 my $newbridge = activate_bridge_vlan($bridge, $tag);
225 copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge;
226
605bb891
DM
227 $newbridge = &$create_firewall_bridge_linux($iface, $newbridge) if $firewall;
228
229 &$bridge_add_interface($newbridge, $iface);
098795e0 230 } else {
70ab4434
DM
231 &$cleanup_firewall_bridge($iface); # remove stale devices
232
233 if ($firewall) {
234 &$create_firewall_bridge_ovs($iface, $bridge, $tag);
235 } else {
236 &$ovs_bridge_add_port($bridge, $iface, $tag);
237 }
4cbabd40 238 }
f0c190ee
AD
239}
240
a84b65c0 241sub tap_unplug {
2db1cc0d 242 my ($iface) = @_;
a84b65c0 243
2db1cc0d
DM
244 my $path= "/sys/class/net/$iface/brport/bridge";
245 if (-l $path) {
246 my $bridge = basename(readlink($path));
247 #avoid insecure dependency;
248 ($bridge) = $bridge =~ /(\S+)/;
4cbabd40 249
098795e0 250 system("/sbin/brctl delif $bridge $iface") == 0 ||
2db1cc0d 251 die "can't del interface '$iface' from bridge '$bridge'\n";
605bb891 252
4cbabd40 253 }
70ab4434
DM
254
255 &$cleanup_firewall_bridge($iface);
a84b65c0
AD
256}
257
b9436cda
DM
258sub copy_bridge_config {
259 my ($br0, $br1) = @_;
260
261 return if $br0 eq $br1;
262
263 my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
ba4af65b 264 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier'];
b9436cda
DM
265
266 foreach my $sysname (@$br_configs) {
267 eval {
268 my $v0 = PVE::Tools::file_read_firstline("/sys/class/net/$br0/bridge/$sysname");
269 my $v1 = PVE::Tools::file_read_firstline("/sys/class/net/$br1/bridge/$sysname");
270 if ($v0 ne $v1) {
aec04803 271 PVE::ProcFSTools::write_proc_entry("/sys/class/net/$br1/bridge/$sysname", $v0);
b9436cda
DM
272 }
273 };
274 warn $@ if $@;
275 }
276}
277
70d89745
PRG
278sub activate_bridge_vlan_slave {
279 my ($bridgevlan, $iface, $tag) = @_;
b9436cda 280 my $ifacevlan = "${iface}.$tag";
70d89745 281
b9436cda
DM
282 # create vlan on $iface is not already exist
283 if (! -d "/sys/class/net/$ifacevlan") {
02c9a6b4
DM
284 system("/sbin/vconfig add $iface $tag") == 0 ||
285 die "can't add vlan tag $tag to interface $iface\n";
b9436cda
DM
286 }
287
288 # be sure to have the $ifacevlan up
605bb891 289 &$activate_interface($ifacevlan);
b9436cda
DM
290
291 # test if $vlaniface is already enslaved in another bridge
292 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
293 if (-l $path) {
294 my $tbridge = basename(readlink($path));
70d89745 295 if ($tbridge ne $bridgevlan) {
b9436cda 296 die "interface $ifacevlan already exist in bridge $tbridge\n";
eee4b32a
PRG
297 } else {
298 # Port already attached to bridge: do nothing.
299 return;
b9436cda
DM
300 }
301 }
302
70d89745 303 # add $ifacevlan to the bridge
605bb891 304 &$bridge_add_interface($bridgevlan, $ifacevlan);
70d89745
PRG
305}
306
307sub activate_bridge_vlan {
308 my ($bridge, $tag_param) = @_;
309
310 die "bridge '$bridge' is not active\n" if ! -d "/sys/class/net/$bridge";
311
312 return $bridge if !defined($tag_param); # no vlan, simply return
313
314 my $tag = int($tag_param);
315
316 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
317
318 my $bridgevlan = "${bridge}v$tag";
319
c9030d97
PRG
320 my @ifaces = ();
321 my $dir = "/sys/class/net/$bridge/brif";
322 PVE::Tools::dir_glob_foreach($dir, '((eth|bond)\d+)', sub {
5ffa7628 323 push @ifaces, $_[0];
c9030d97
PRG
324 });
325
5ffa7628 326 die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0;
c9030d97 327
b9436cda
DM
328 # add bridgevlan if it doesn't already exist
329 if (! -d "/sys/class/net/$bridgevlan") {
9e14b1b7 330 system("/sbin/brctl addbr $bridgevlan") == 0 ||
b9436cda
DM
331 die "can't add bridge $bridgevlan\n";
332 }
333
70d89745 334 # for each physical interface (eth or bridge) bind them to bridge vlan
c9030d97
PRG
335 foreach my $iface (@ifaces) {
336 activate_bridge_vlan_slave($bridgevlan, $iface, $tag);
337 }
70d89745 338
b9436cda
DM
339 #fixme: set other bridge flags
340
341 # be sure to have the bridge up
342 system("/sbin/ip link set $bridgevlan up") == 0 ||
343 die "can't up bridge $bridgevlan\n";
70d89745 344
b9436cda
DM
345 return $bridgevlan;
346}
347
3481;