]>
Commit | Line | Data |
---|---|---|
b9436cda DM |
1 | package PVE::Network; |
2 | ||
3 | use strict; | |
c36f332e | 4 | use warnings; |
a712bf6e | 5 | use PVE::Tools qw(run_command lock_file); |
b9436cda DM |
6 | use PVE::ProcFSTools; |
7 | use PVE::INotify; | |
8 | use File::Basename; | |
b6bff92e WB |
9 | use IO::Socket::IP; |
10 | use POSIX qw(ECONNREFUSED); | |
b9436cda | 11 | |
bf52d27b WB |
12 | use Net::IP; |
13 | ||
b9436cda DM |
14 | # host network related utility functions |
15 | ||
61aa94e4 WB |
16 | our $ipv4_reverse_mask = [ |
17 | '0.0.0.0', | |
18 | '128.0.0.0', | |
19 | '192.0.0.0', | |
20 | '224.0.0.0', | |
21 | '240.0.0.0', | |
22 | '248.0.0.0', | |
23 | '252.0.0.0', | |
24 | '254.0.0.0', | |
25 | '255.0.0.0', | |
26 | '255.128.0.0', | |
27 | '255.192.0.0', | |
28 | '255.224.0.0', | |
29 | '255.240.0.0', | |
30 | '255.248.0.0', | |
31 | '255.252.0.0', | |
32 | '255.254.0.0', | |
33 | '255.255.0.0', | |
34 | '255.255.128.0', | |
35 | '255.255.192.0', | |
36 | '255.255.224.0', | |
37 | '255.255.240.0', | |
38 | '255.255.248.0', | |
39 | '255.255.252.0', | |
40 | '255.255.254.0', | |
41 | '255.255.255.0', | |
42 | '255.255.255.128', | |
43 | '255.255.255.192', | |
44 | '255.255.255.224', | |
45 | '255.255.255.240', | |
46 | '255.255.255.248', | |
47 | '255.255.255.252', | |
48 | '255.255.255.254', | |
49 | '255.255.255.255', | |
50 | ]; | |
51 | ||
52 | our $ipv4_mask_hash_localnet = { | |
19e609fd WB |
53 | '255.0.0.0' => 8, |
54 | '255.128.0.0' => 9, | |
55 | '255.192.0.0' => 10, | |
56 | '255.224.0.0' => 11, | |
57 | '255.240.0.0' => 12, | |
58 | '255.248.0.0' => 13, | |
59 | '255.252.0.0' => 14, | |
60 | '255.254.0.0' => 15, | |
61aa94e4 WB |
61 | '255.255.0.0' => 16, |
62 | '255.255.128.0' => 17, | |
63 | '255.255.192.0' => 18, | |
64 | '255.255.224.0' => 19, | |
65 | '255.255.240.0' => 20, | |
66 | '255.255.248.0' => 21, | |
67 | '255.255.252.0' => 22, | |
68 | '255.255.254.0' => 23, | |
69 | '255.255.255.0' => 24, | |
70 | '255.255.255.128' => 25, | |
71 | '255.255.255.192' => 26, | |
72 | '255.255.255.224' => 27, | |
73 | '255.255.255.240' => 28, | |
74 | '255.255.255.248' => 29, | |
75 | '255.255.255.252' => 30, | |
e43faad9 WB |
76 | '255.255.255.254' => 31, |
77 | '255.255.255.255' => 32, | |
61aa94e4 WB |
78 | }; |
79 | ||
74d1b045 DM |
80 | sub setup_tc_rate_limit { |
81 | my ($iface, $rate, $burst, $debug) = @_; | |
82 | ||
2d6b3a90 FG |
83 | # these are allowed / expected to fail, e.g. when there is no previous rate limit to remove |
84 | eval { run_command("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1"); }; | |
85 | eval { run_command("/sbin/tc filter del dev $iface parent ffff: protocol all pref 50 u32 >/dev/null 2>&1"); }; | |
86 | eval { run_command("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1"); }; | |
87 | eval { run_command("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1"); }; | |
74d1b045 | 88 | |
d6f2623b | 89 | return if !$rate; |
957753df | 90 | |
74d1b045 DM |
91 | # tbf does not work for unknown reason |
92 | #$TC qdisc add dev $DEV root tbf rate $RATE latency 100ms burst $BURST | |
93 | # so we use htb instead | |
94 | run_command("/sbin/tc qdisc add dev $iface root handle 1: htb default 1"); | |
95 | run_command("/sbin/tc class add dev $iface parent 1: classid 1:1 " . | |
96 | "htb rate ${rate}bps burst ${burst}b"); | |
97 | ||
5d35df41 W |
98 | run_command("/sbin/tc qdisc add dev $iface handle ffff: ingress"); |
99 | run_command("/sbin/tc filter add dev $iface parent ffff: " . | |
1b915170 | 100 | "prio 50 basic " . |
5d35df41 W |
101 | "police rate ${rate}bps burst ${burst}b mtu 64kb " . |
102 | "drop flowid :1"); | |
103 | ||
74d1b045 DM |
104 | if ($debug) { |
105 | print "DEBUG tc settings\n"; | |
106 | system("/sbin/tc qdisc ls dev $iface"); | |
107 | system("/sbin/tc class ls dev $iface"); | |
108 | system("/sbin/tc filter ls dev $iface parent ffff:"); | |
109 | } | |
110 | } | |
111 | ||
ec9ada18 AD |
112 | sub tap_rate_limit { |
113 | my ($iface, $rate) = @_; | |
114 | ||
115 | my $debug = 0; | |
ad066ae2 | 116 | $rate = int($rate*1024*1024) if $rate; |
ec9ada18 AD |
117 | my $burst = 1024*1024; |
118 | ||
119 | setup_tc_rate_limit($iface, $rate, $burst, $debug); | |
120 | } | |
74d1b045 | 121 | |
605bb891 DM |
122 | my $read_bridge_mtu = sub { |
123 | my ($bridge) = @_; | |
124 | ||
125 | my $mtu = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/mtu"); | |
126 | die "bridge '$bridge' does not exist\n" if !$mtu; | |
127 | # avoid insecure dependency; | |
128 | die "unable to parse mtu value" if $mtu !~ /^(\d+)$/; | |
129 | $mtu = int($1); | |
130 | ||
131 | return $mtu; | |
132 | }; | |
133 | ||
32cb7d27 | 134 | my $parse_tap_device_name = sub { |
6c80e6d6 | 135 | my ($iface, $noerr) = @_; |
605bb891 DM |
136 | |
137 | my ($vmid, $devid); | |
138 | ||
139 | if ($iface =~ m/^tap(\d+)i(\d+)$/) { | |
140 | $vmid = $1; | |
141 | $devid = $2; | |
32cb7d27 | 142 | } elsif ($iface =~ m/^veth(\d+)i(\d+)$/) { |
605bb891 DM |
143 | $vmid = $1; |
144 | $devid = $2; | |
145 | } else { | |
6c80e6d6 DM |
146 | return undef if $noerr; |
147 | die "can't create firewall bridge for random interface name '$iface'\n"; | |
605bb891 DM |
148 | } |
149 | ||
150 | return ($vmid, $devid); | |
151 | }; | |
152 | ||
70ab4434 | 153 | my $compute_fwbr_names = sub { |
605bb891 DM |
154 | my ($vmid, $devid) = @_; |
155 | ||
156 | my $fwbr = "fwbr${vmid}i${devid}"; | |
f193aa74 | 157 | # Note: the firewall use 'fwln+' to filter traffic to VMs |
7d78a966 AD |
158 | my $vethfw = "fwln${vmid}i${devid}"; |
159 | my $vethfwpeer = "fwpr${vmid}p${devid}"; | |
160 | my $ovsintport = "fwln${vmid}o${devid}"; | |
605bb891 | 161 | |
70ab4434 | 162 | return ($fwbr, $vethfw, $vethfwpeer, $ovsintport); |
605bb891 DM |
163 | }; |
164 | ||
165 | my $cond_create_bridge = sub { | |
166 | my ($bridge) = @_; | |
167 | ||
168 | if (! -d "/sys/class/net/$bridge") { | |
169 | system("/sbin/brctl addbr $bridge") == 0 || | |
170 | die "can't add bridge '$bridge'\n"; | |
171 | } | |
172 | }; | |
173 | ||
f3ccd9b4 WB |
174 | sub disable_ipv6 { |
175 | my ($iface) = @_; | |
176 | return if !-d '/proc/sys/net/ipv6'; # ipv6 might be completely disabled | |
177 | my $file = "/proc/sys/net/ipv6/conf/$iface/disable_ipv6"; | |
178 | open(my $fh, '>', $file) or die "failed to open $file for writing: $!\n"; | |
179 | print {$fh} "1\n" or die "failed to disable link-local ipv6 for $iface\n"; | |
180 | close($fh); | |
181 | } | |
182 | ||
605bb891 | 183 | my $bridge_add_interface = sub { |
b0b34ffd | 184 | my ($bridge, $iface, $tag, $trunks) = @_; |
605bb891 | 185 | |
f3ccd9b4 WB |
186 | # drop link local address (it can't be used when on a bridge anyway) |
187 | disable_ipv6($iface); | |
605bb891 DM |
188 | system("/sbin/brctl addif $bridge $iface") == 0 || |
189 | die "can't add interface 'iface' to bridge '$bridge'\n"; | |
4d25f4aa AD |
190 | |
191 | my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering"); | |
192 | ||
193 | if ($vlan_aware) { | |
194 | if ($tag) { | |
78e912a3 WB |
195 | system({'/sbin/bridge'} 'bridge', 'vlan', 'del', 'dev', $iface, 'vid', '1-4094') == 0 |
196 | or die "failed to remove default vlan tags of $iface\n"; | |
197 | system({'/sbin/bridge'} 'bridge', 'vlan', 'add', 'dev', $iface, 'vid', $tag, 'pvid', 'untagged') == 0 | |
198 | or die "unable to add vlan $tag to interface $iface\n"; | |
5d662b31 DC |
199 | |
200 | warn "Caution: Setting VLAN ID 1 on a VLAN aware bridge may be dangerous\n" if $tag == 1; | |
4d25f4aa AD |
201 | } else { |
202 | system("/sbin/bridge vlan add dev $iface vid 2-4094") == 0 || | |
b0b34ffd | 203 | die "unable to add default vlan tags to interface $iface\n" if !$trunks; |
4d25f4aa | 204 | } |
b0b34ffd | 205 | |
846337ad WB |
206 | if ($trunks) { |
207 | my @trunks_array = split /;/, $trunks; | |
208 | foreach my $trunk (@trunks_array) { | |
209 | system("/sbin/bridge vlan add dev $iface vid $trunk") == 0 || | |
210 | die "unable to add vlan $trunk to interface $iface\n"; | |
211 | } | |
b0b34ffd | 212 | } |
4d25f4aa | 213 | } |
605bb891 DM |
214 | }; |
215 | ||
70ab4434 | 216 | my $ovs_bridge_add_port = sub { |
b0b34ffd AD |
217 | my ($bridge, $iface, $tag, $internal, $trunks) = @_; |
218 | ||
219 | $trunks =~ s/;/,/g if $trunks; | |
70ab4434 DM |
220 | |
221 | my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface"; | |
222 | $cmd .= " tag=$tag" if $tag; | |
b0b34ffd AD |
223 | $cmd .= " trunks=". join(',', $trunks) if $trunks; |
224 | $cmd .= " vlan_mode=native-untagged" if $tag && $trunks; | |
225 | ||
70ab4434 DM |
226 | $cmd .= " -- set Interface $iface type=internal" if $internal; |
227 | system($cmd) == 0 || | |
228 | die "can't add ovs port '$iface'\n"; | |
f3ccd9b4 | 229 | disable_ipv6($iface); |
70ab4434 DM |
230 | }; |
231 | ||
605bb891 DM |
232 | my $activate_interface = sub { |
233 | my ($iface) = @_; | |
234 | ||
235 | system("/sbin/ip link set $iface up") == 0 || | |
236 | die "can't activate interface '$iface'\n"; | |
237 | }; | |
238 | ||
3aa99c70 AD |
239 | sub tap_create { |
240 | my ($iface, $bridge) = @_; | |
241 | ||
242 | die "unable to get bridge setting\n" if !$bridge; | |
243 | ||
605bb891 | 244 | my $bridgemtu = &$read_bridge_mtu($bridge); |
3aa99c70 | 245 | |
098795e0 | 246 | eval { |
f3ccd9b4 | 247 | disable_ipv6($iface); |
098795e0 DM |
248 | PVE::Tools::run_command("/sbin/ifconfig $iface 0.0.0.0 promisc up mtu $bridgemtu"); |
249 | }; | |
250 | die "interface activation failed\n" if $@; | |
3aa99c70 AD |
251 | } |
252 | ||
35efc4eb AD |
253 | sub veth_create { |
254 | my ($veth, $vethpeer, $bridge, $mac) = @_; | |
255 | ||
256 | die "unable to get bridge setting\n" if !$bridge; | |
257 | ||
258 | my $bridgemtu = &$read_bridge_mtu($bridge); | |
259 | ||
260 | # create veth pair | |
261 | if (! -d "/sys/class/net/$veth") { | |
262 | my $cmd = "/sbin/ip link add name $veth type veth peer name $vethpeer mtu $bridgemtu"; | |
263 | $cmd .= " addr $mac" if $mac; | |
264 | system($cmd) == 0 || die "can't create interface $veth\n"; | |
265 | } | |
266 | ||
267 | # up vethpair | |
f3ccd9b4 WB |
268 | disable_ipv6($veth); |
269 | disable_ipv6($vethpeer); | |
35efc4eb AD |
270 | &$activate_interface($veth); |
271 | &$activate_interface($vethpeer); | |
272 | } | |
273 | ||
f3f0bc3a AD |
274 | sub veth_delete { |
275 | my ($veth) = @_; | |
276 | ||
277 | if (-d "/sys/class/net/$veth") { | |
278 | run_command("/sbin/ip link delete dev $veth", outfunc => sub {}, errfunc => sub {}); | |
279 | } | |
e0a862e2 | 280 | eval { tap_unplug($veth) }; |
f3f0bc3a | 281 | } |
35efc4eb | 282 | |
605bb891 | 283 | my $create_firewall_bridge_linux = sub { |
b0b34ffd | 284 | my ($iface, $bridge, $tag, $trunks) = @_; |
605bb891 | 285 | |
32cb7d27 | 286 | my ($vmid, $devid) = &$parse_tap_device_name($iface); |
70ab4434 | 287 | my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid); |
605bb891 | 288 | |
605bb891 | 289 | &$cond_create_bridge($fwbr); |
f3ccd9b4 | 290 | disable_ipv6($fwbr); |
605bb891 DM |
291 | &$activate_interface($fwbr); |
292 | ||
293 | copy_bridge_config($bridge, $fwbr); | |
35efc4eb | 294 | veth_create($vethfw, $vethfwpeer, $bridge); |
605bb891 | 295 | |
7d78a966 | 296 | &$bridge_add_interface($fwbr, $vethfw); |
b0b34ffd | 297 | &$bridge_add_interface($bridge, $vethfwpeer, $tag, $trunks); |
605bb891 | 298 | |
4d25f4aa | 299 | &$bridge_add_interface($fwbr, $iface); |
605bb891 DM |
300 | }; |
301 | ||
70ab4434 | 302 | my $create_firewall_bridge_ovs = sub { |
b0b34ffd | 303 | my ($iface, $bridge, $tag, $trunks) = @_; |
70ab4434 | 304 | |
32cb7d27 | 305 | my ($vmid, $devid) = &$parse_tap_device_name($iface); |
70ab4434 DM |
306 | my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid); |
307 | ||
308 | my $bridgemtu = &$read_bridge_mtu($bridge); | |
309 | ||
310 | &$cond_create_bridge($fwbr); | |
f3ccd9b4 | 311 | disable_ipv6($fwbr); |
70ab4434 DM |
312 | &$activate_interface($fwbr); |
313 | ||
314 | &$bridge_add_interface($fwbr, $iface); | |
315 | ||
b0b34ffd | 316 | &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1, $trunks); |
ac3a04b8 | 317 | &$activate_interface($ovsintport); |
70ab4434 DM |
318 | |
319 | # set the same mtu for ovs int port | |
320 | PVE::Tools::run_command("/sbin/ifconfig $ovsintport mtu $bridgemtu"); | |
321 | ||
322 | &$bridge_add_interface($fwbr, $ovsintport); | |
323 | }; | |
324 | ||
325 | my $cleanup_firewall_bridge = sub { | |
605bb891 DM |
326 | my ($iface) = @_; |
327 | ||
32cb7d27 | 328 | my ($vmid, $devid) = &$parse_tap_device_name($iface, 1); |
6c80e6d6 | 329 | return if !defined($vmid); |
70ab4434 DM |
330 | my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid); |
331 | ||
332 | # cleanup old port config from any openvswitch bridge | |
333 | if (-d "/sys/class/net/$ovsintport") { | |
334 | run_command("/usr/bin/ovs-vsctl del-port $ovsintport", outfunc => sub {}, errfunc => sub {}); | |
335 | } | |
605bb891 DM |
336 | |
337 | # delete old vethfw interface | |
f3f0bc3a | 338 | veth_delete($vethfw); |
605bb891 DM |
339 | |
340 | # cleanup fwbr bridge | |
341 | if (-d "/sys/class/net/$fwbr") { | |
342 | run_command("/sbin/ip link set dev $fwbr down", outfunc => sub {}, errfunc => sub {}); | |
343 | run_command("/sbin/brctl delbr $fwbr", outfunc => sub {}, errfunc => sub {}); | |
344 | } | |
345 | }; | |
346 | ||
f0c190ee | 347 | sub tap_plug { |
bce2a5b3 | 348 | my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_; |
f0c190ee | 349 | |
4cbabd40 AD |
350 | #cleanup old port config from any openvswitch bridge |
351 | eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) }; | |
352 | ||
098795e0 | 353 | if (-d "/sys/class/net/$bridge/bridge") { |
70ab4434 | 354 | &$cleanup_firewall_bridge($iface); # remove stale devices |
605bb891 | 355 | |
4d25f4aa | 356 | my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering"); |
098795e0 | 357 | |
4d25f4aa | 358 | if (!$vlan_aware) { |
b0b34ffd | 359 | die "vlan aware feature need to be enabled to use trunks" if $trunks; |
4d25f4aa AD |
360 | my $newbridge = activate_bridge_vlan($bridge, $tag); |
361 | copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge; | |
ff042056 | 362 | $bridge = $newbridge; |
4d25f4aa AD |
363 | $tag = undef; |
364 | } | |
365 | ||
366 | if ($firewall) { | |
b0b34ffd | 367 | &$create_firewall_bridge_linux($iface, $bridge, $tag, $trunks); |
4d25f4aa | 368 | } else { |
b0b34ffd | 369 | &$bridge_add_interface($bridge, $iface, $tag, $trunks); |
4d25f4aa | 370 | } |
605bb891 | 371 | |
098795e0 | 372 | } else { |
70ab4434 DM |
373 | &$cleanup_firewall_bridge($iface); # remove stale devices |
374 | ||
375 | if ($firewall) { | |
b0b34ffd | 376 | &$create_firewall_bridge_ovs($iface, $bridge, $tag, $trunks); |
70ab4434 | 377 | } else { |
b0b34ffd | 378 | &$ovs_bridge_add_port($bridge, $iface, $tag, undef, $trunks); |
70ab4434 | 379 | } |
4cbabd40 | 380 | } |
bce2a5b3 WB |
381 | |
382 | tap_rate_limit($iface, $rate); | |
f0c190ee AD |
383 | } |
384 | ||
a84b65c0 | 385 | sub tap_unplug { |
2db1cc0d | 386 | my ($iface) = @_; |
a84b65c0 | 387 | |
2db1cc0d DM |
388 | my $path= "/sys/class/net/$iface/brport/bridge"; |
389 | if (-l $path) { | |
390 | my $bridge = basename(readlink($path)); | |
391 | #avoid insecure dependency; | |
392 | ($bridge) = $bridge =~ /(\S+)/; | |
4cbabd40 | 393 | |
098795e0 | 394 | system("/sbin/brctl delif $bridge $iface") == 0 || |
2db1cc0d | 395 | die "can't del interface '$iface' from bridge '$bridge'\n"; |
605bb891 | 396 | |
4cbabd40 | 397 | } |
70ab4434 DM |
398 | |
399 | &$cleanup_firewall_bridge($iface); | |
dd44486e WB |
400 | #cleanup old port config from any openvswitch bridge |
401 | eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) }; | |
a84b65c0 AD |
402 | } |
403 | ||
b9436cda DM |
404 | sub copy_bridge_config { |
405 | my ($br0, $br1) = @_; | |
406 | ||
407 | return if $br0 eq $br1; | |
408 | ||
409 | my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay', | |
ba4af65b | 410 | 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier']; |
b9436cda DM |
411 | |
412 | foreach my $sysname (@$br_configs) { | |
413 | eval { | |
414 | my $v0 = PVE::Tools::file_read_firstline("/sys/class/net/$br0/bridge/$sysname"); | |
415 | my $v1 = PVE::Tools::file_read_firstline("/sys/class/net/$br1/bridge/$sysname"); | |
416 | if ($v0 ne $v1) { | |
aec04803 | 417 | PVE::ProcFSTools::write_proc_entry("/sys/class/net/$br1/bridge/$sysname", $v0); |
b9436cda DM |
418 | } |
419 | }; | |
420 | warn $@ if $@; | |
421 | } | |
422 | } | |
423 | ||
70d89745 PRG |
424 | sub activate_bridge_vlan_slave { |
425 | my ($bridgevlan, $iface, $tag) = @_; | |
b9436cda | 426 | my $ifacevlan = "${iface}.$tag"; |
70d89745 | 427 | |
b9436cda DM |
428 | # create vlan on $iface is not already exist |
429 | if (! -d "/sys/class/net/$ifacevlan") { | |
f3ccd9b4 | 430 | system("/sbin/ip link add link $iface name $ifacevlan type vlan id $tag") == 0 || |
02c9a6b4 | 431 | die "can't add vlan tag $tag to interface $iface\n"; |
b9436cda DM |
432 | } |
433 | ||
f3ccd9b4 WB |
434 | # remove ipv6 link-local address before activation |
435 | disable_ipv6($ifacevlan); | |
436 | ||
b9436cda | 437 | # be sure to have the $ifacevlan up |
605bb891 | 438 | &$activate_interface($ifacevlan); |
b9436cda DM |
439 | |
440 | # test if $vlaniface is already enslaved in another bridge | |
441 | my $path= "/sys/class/net/$ifacevlan/brport/bridge"; | |
442 | if (-l $path) { | |
443 | my $tbridge = basename(readlink($path)); | |
70d89745 | 444 | if ($tbridge ne $bridgevlan) { |
b9436cda | 445 | die "interface $ifacevlan already exist in bridge $tbridge\n"; |
eee4b32a PRG |
446 | } else { |
447 | # Port already attached to bridge: do nothing. | |
448 | return; | |
b9436cda DM |
449 | } |
450 | } | |
451 | ||
70d89745 | 452 | # add $ifacevlan to the bridge |
605bb891 | 453 | &$bridge_add_interface($bridgevlan, $ifacevlan); |
70d89745 PRG |
454 | } |
455 | ||
456 | sub activate_bridge_vlan { | |
457 | my ($bridge, $tag_param) = @_; | |
458 | ||
459 | die "bridge '$bridge' is not active\n" if ! -d "/sys/class/net/$bridge"; | |
460 | ||
461 | return $bridge if !defined($tag_param); # no vlan, simply return | |
462 | ||
463 | my $tag = int($tag_param); | |
464 | ||
465 | die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094; | |
466 | ||
467 | my $bridgevlan = "${bridge}v$tag"; | |
468 | ||
c9030d97 PRG |
469 | my @ifaces = (); |
470 | my $dir = "/sys/class/net/$bridge/brif"; | |
899f8c4a | 471 | PVE::Tools::dir_glob_foreach($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub { |
5ffa7628 | 472 | push @ifaces, $_[0]; |
c9030d97 PRG |
473 | }); |
474 | ||
5ffa7628 | 475 | die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0; |
c9030d97 | 476 | |
a712bf6e WB |
477 | lock_network(sub { |
478 | # add bridgevlan if it doesn't already exist | |
479 | if (! -d "/sys/class/net/$bridgevlan") { | |
480 | system("/sbin/brctl addbr $bridgevlan") == 0 || | |
481 | die "can't add bridge $bridgevlan\n"; | |
482 | } | |
b9436cda | 483 | |
a712bf6e WB |
484 | # for each physical interface (eth or bridge) bind them to bridge vlan |
485 | foreach my $iface (@ifaces) { | |
486 | activate_bridge_vlan_slave($bridgevlan, $iface, $tag); | |
487 | } | |
70d89745 | 488 | |
a712bf6e | 489 | #fixme: set other bridge flags |
b9436cda | 490 | |
f3ccd9b4 WB |
491 | # remove ipv6 link-local address before activation |
492 | disable_ipv6($bridgevlan); | |
a712bf6e | 493 | # be sure to have the bridge up |
f3ccd9b4 | 494 | &$activate_interface($bridgevlan); |
a712bf6e | 495 | }); |
b9436cda DM |
496 | return $bridgevlan; |
497 | } | |
498 | ||
b6bff92e WB |
499 | sub tcp_ping { |
500 | my ($host, $port, $timeout) = @_; | |
501 | ||
502 | my $refused = 1; | |
503 | ||
504 | $timeout = 3 if !$timeout; # sane default | |
505 | if (!$port) { | |
506 | # Net::Ping defaults to the echo port | |
507 | $port = 7; | |
508 | } else { | |
509 | # Net::Ping's port_number() implies service_check(1) | |
510 | $refused = 0; | |
511 | } | |
512 | ||
513 | my ($sock, $result); | |
514 | eval { | |
515 | $result = PVE::Tools::run_with_timeout($timeout, sub { | |
516 | $sock = IO::Socket::IP->new(PeerHost => $host, PeerPort => $port, Type => SOCK_STREAM); | |
517 | $result = $refused if $! == ECONNREFUSED; | |
518 | }); | |
519 | }; | |
520 | if ($sock) { | |
521 | $sock->close(); | |
522 | $result = 1; | |
523 | } | |
524 | return $result; | |
525 | } | |
526 | ||
bf52d27b WB |
527 | sub IP_from_cidr { |
528 | my ($cidr, $version) = @_; | |
529 | ||
530 | return if $cidr !~ m!^(\S+?)/(\S+)$!; | |
531 | my ($ip, $prefix) = ($1, $2); | |
532 | ||
533 | my $ipobj = Net::IP->new($ip, $version); | |
534 | return if !$ipobj; | |
535 | ||
536 | $version = $ipobj->version(); | |
537 | ||
538 | my $binmask = Net::IP::ip_get_mask($prefix, $version); | |
539 | return if !$binmask; | |
540 | ||
541 | my $masked_binip = $ipobj->binip() & $binmask; | |
542 | my $masked_ip = Net::IP::ip_bintoip($masked_binip, $version); | |
543 | return Net::IP->new("$masked_ip/$prefix"); | |
544 | } | |
545 | ||
546 | sub is_ip_in_cidr { | |
547 | my ($ip, $cidr, $version) = @_; | |
548 | ||
549 | my $cidr_obj = IP_from_cidr($cidr, $version); | |
550 | return undef if !$cidr_obj; | |
551 | ||
552 | my $ip_obj = Net::IP->new($ip, $version); | |
553 | return undef if !$ip_obj; | |
554 | ||
555 | return $cidr_obj->overlaps($ip_obj) == $Net::IP::IP_B_IN_A_OVERLAP; | |
556 | } | |
557 | ||
beb9820f TL |
558 | |
559 | sub get_local_ip_from_cidr { | |
560 | my ($cidr) = @_; | |
561 | ||
562 | my $cmd = ['/sbin/ip', 'address', 'show', 'to', $cidr, 'up']; | |
563 | ||
564 | my $IPs = []; | |
565 | ||
566 | my $code = sub { | |
567 | my $line = shift; | |
568 | ||
569 | if ($line =~ m!^\s*inet(?:6)?\s+($PVE::Tools::IPRE)/\d+!) { | |
570 | push @$IPs, $1; | |
571 | } | |
572 | }; | |
573 | ||
574 | PVE::Tools::run_command($cmd, outfunc => $code); | |
575 | ||
576 | return $IPs; | |
577 | } | |
578 | ||
a712bf6e WB |
579 | sub lock_network { |
580 | my ($code, @param) = @_; | |
581 | my $res = lock_file('/var/lock/pve-network.lck', 10, $code, @param); | |
582 | die $@ if $@; | |
583 | return $res; | |
584 | } | |
585 | ||
b9436cda | 586 | 1; |