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