]> git.proxmox.com Git - pve-common.git/blame - src/PVE/Network.pm
use Encode::Locale to encode parameters read from console(getopt)
[pve-common.git] / src / PVE / Network.pm
CommitLineData
b9436cda
DM
1package PVE::Network;
2
3use strict;
c36f332e 4use warnings;
a712bf6e 5use PVE::Tools qw(run_command lock_file);
b9436cda
DM
6use PVE::ProcFSTools;
7use PVE::INotify;
8use File::Basename;
b6bff92e
WB
9use IO::Socket::IP;
10use POSIX qw(ECONNREFUSED);
b9436cda 11
bf52d27b
WB
12use Net::IP;
13
b9436cda
DM
14# host network related utility functions
15
61aa94e4
WB
16our $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
52our $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
80sub 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
112sub 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
122my $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 134my $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 153my $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
165my $cond_create_bridge = sub {
166 my ($bridge) = @_;
167
168 if (! -d "/sys/class/net/$bridge") {
86b84237
WB
169 system("/sbin/brctl addbr $bridge") == 0 ||
170 die "can't add bridge '$bridge'\n";
171 disable_ipv6($bridge);
605bb891
DM
172 }
173};
174
f3ccd9b4
WB
175sub disable_ipv6 {
176 my ($iface) = @_;
177 return if !-d '/proc/sys/net/ipv6'; # ipv6 might be completely disabled
178 my $file = "/proc/sys/net/ipv6/conf/$iface/disable_ipv6";
179 open(my $fh, '>', $file) or die "failed to open $file for writing: $!\n";
180 print {$fh} "1\n" or die "failed to disable link-local ipv6 for $iface\n";
181 close($fh);
182}
183
605bb891 184my $bridge_add_interface = sub {
b0b34ffd 185 my ($bridge, $iface, $tag, $trunks) = @_;
605bb891 186
f3ccd9b4
WB
187 # drop link local address (it can't be used when on a bridge anyway)
188 disable_ipv6($iface);
605bb891
DM
189 system("/sbin/brctl addif $bridge $iface") == 0 ||
190 die "can't add interface 'iface' to bridge '$bridge'\n";
4d25f4aa
AD
191
192 my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
193
194 if ($vlan_aware) {
195 if ($tag) {
78e912a3
WB
196 system({'/sbin/bridge'} 'bridge', 'vlan', 'del', 'dev', $iface, 'vid', '1-4094') == 0
197 or die "failed to remove default vlan tags of $iface\n";
198 system({'/sbin/bridge'} 'bridge', 'vlan', 'add', 'dev', $iface, 'vid', $tag, 'pvid', 'untagged') == 0
199 or die "unable to add vlan $tag to interface $iface\n";
5d662b31
DC
200
201 warn "Caution: Setting VLAN ID 1 on a VLAN aware bridge may be dangerous\n" if $tag == 1;
4d25f4aa
AD
202 } else {
203 system("/sbin/bridge vlan add dev $iface vid 2-4094") == 0 ||
b0b34ffd 204 die "unable to add default vlan tags to interface $iface\n" if !$trunks;
4d25f4aa 205 }
b0b34ffd 206
846337ad
WB
207 if ($trunks) {
208 my @trunks_array = split /;/, $trunks;
209 foreach my $trunk (@trunks_array) {
210 system("/sbin/bridge vlan add dev $iface vid $trunk") == 0 ||
211 die "unable to add vlan $trunk to interface $iface\n";
212 }
b0b34ffd 213 }
4d25f4aa 214 }
605bb891
DM
215};
216
70ab4434 217my $ovs_bridge_add_port = sub {
b0b34ffd
AD
218 my ($bridge, $iface, $tag, $internal, $trunks) = @_;
219
220 $trunks =~ s/;/,/g if $trunks;
70ab4434
DM
221
222 my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface";
223 $cmd .= " tag=$tag" if $tag;
b0b34ffd
AD
224 $cmd .= " trunks=". join(',', $trunks) if $trunks;
225 $cmd .= " vlan_mode=native-untagged" if $tag && $trunks;
226
70ab4434
DM
227 $cmd .= " -- set Interface $iface type=internal" if $internal;
228 system($cmd) == 0 ||
229 die "can't add ovs port '$iface'\n";
f3ccd9b4 230 disable_ipv6($iface);
70ab4434
DM
231};
232
605bb891
DM
233my $activate_interface = sub {
234 my ($iface) = @_;
235
236 system("/sbin/ip link set $iface up") == 0 ||
237 die "can't activate interface '$iface'\n";
238};
239
3aa99c70
AD
240sub tap_create {
241 my ($iface, $bridge) = @_;
242
243 die "unable to get bridge setting\n" if !$bridge;
244
605bb891 245 my $bridgemtu = &$read_bridge_mtu($bridge);
3aa99c70 246
098795e0 247 eval {
f3ccd9b4 248 disable_ipv6($iface);
86330049 249 PVE::Tools::run_command(['/sbin/ip', 'link', 'set', $iface, 'up', 'promisc', 'on', 'mtu', $bridgemtu]);
098795e0
DM
250 };
251 die "interface activation failed\n" if $@;
3aa99c70
AD
252}
253
35efc4eb
AD
254sub veth_create {
255 my ($veth, $vethpeer, $bridge, $mac) = @_;
256
257 die "unable to get bridge setting\n" if !$bridge;
258
259 my $bridgemtu = &$read_bridge_mtu($bridge);
260
261 # create veth pair
262 if (! -d "/sys/class/net/$veth") {
263 my $cmd = "/sbin/ip link add name $veth type veth peer name $vethpeer mtu $bridgemtu";
264 $cmd .= " addr $mac" if $mac;
265 system($cmd) == 0 || die "can't create interface $veth\n";
266 }
267
268 # up vethpair
f3ccd9b4
WB
269 disable_ipv6($veth);
270 disable_ipv6($vethpeer);
35efc4eb
AD
271 &$activate_interface($veth);
272 &$activate_interface($vethpeer);
273}
274
f3f0bc3a
AD
275sub veth_delete {
276 my ($veth) = @_;
277
278 if (-d "/sys/class/net/$veth") {
279 run_command("/sbin/ip link delete dev $veth", outfunc => sub {}, errfunc => sub {});
280 }
e0a862e2 281 eval { tap_unplug($veth) };
f3f0bc3a 282}
35efc4eb 283
605bb891 284my $create_firewall_bridge_linux = sub {
b0b34ffd 285 my ($iface, $bridge, $tag, $trunks) = @_;
605bb891 286
32cb7d27 287 my ($vmid, $devid) = &$parse_tap_device_name($iface);
70ab4434 288 my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid);
605bb891 289
605bb891
DM
290 &$cond_create_bridge($fwbr);
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 302my $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);
311 &$activate_interface($fwbr);
312
313 &$bridge_add_interface($fwbr, $iface);
314
b0b34ffd 315 &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1, $trunks);
ac3a04b8 316 &$activate_interface($ovsintport);
70ab4434
DM
317
318 # set the same mtu for ovs int port
86330049 319 PVE::Tools::run_command(['/sbin/ip', 'link', 'set', $ovsintport, 'mtu', $bridgemtu]);
70ab4434
DM
320
321 &$bridge_add_interface($fwbr, $ovsintport);
322};
323
324my $cleanup_firewall_bridge = sub {
605bb891
DM
325 my ($iface) = @_;
326
32cb7d27 327 my ($vmid, $devid) = &$parse_tap_device_name($iface, 1);
6c80e6d6 328 return if !defined($vmid);
70ab4434
DM
329 my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
330
331 # cleanup old port config from any openvswitch bridge
332 if (-d "/sys/class/net/$ovsintport") {
333 run_command("/usr/bin/ovs-vsctl del-port $ovsintport", outfunc => sub {}, errfunc => sub {});
334 }
605bb891
DM
335
336 # delete old vethfw interface
f3f0bc3a 337 veth_delete($vethfw);
605bb891
DM
338
339 # cleanup fwbr bridge
340 if (-d "/sys/class/net/$fwbr") {
341 run_command("/sbin/ip link set dev $fwbr down", outfunc => sub {}, errfunc => sub {});
342 run_command("/sbin/brctl delbr $fwbr", outfunc => sub {}, errfunc => sub {});
343 }
344};
345
f0c190ee 346sub tap_plug {
bce2a5b3 347 my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_;
f0c190ee 348
4cbabd40
AD
349 #cleanup old port config from any openvswitch bridge
350 eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) };
351
098795e0 352 if (-d "/sys/class/net/$bridge/bridge") {
70ab4434 353 &$cleanup_firewall_bridge($iface); # remove stale devices
605bb891 354
4d25f4aa 355 my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
098795e0 356
4d25f4aa 357 if (!$vlan_aware) {
b0b34ffd 358 die "vlan aware feature need to be enabled to use trunks" if $trunks;
4d25f4aa
AD
359 my $newbridge = activate_bridge_vlan($bridge, $tag);
360 copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge;
ff042056 361 $bridge = $newbridge;
4d25f4aa
AD
362 $tag = undef;
363 }
364
365 if ($firewall) {
b0b34ffd 366 &$create_firewall_bridge_linux($iface, $bridge, $tag, $trunks);
4d25f4aa 367 } else {
b0b34ffd 368 &$bridge_add_interface($bridge, $iface, $tag, $trunks);
4d25f4aa 369 }
605bb891 370
098795e0 371 } else {
70ab4434
DM
372 &$cleanup_firewall_bridge($iface); # remove stale devices
373
374 if ($firewall) {
b0b34ffd 375 &$create_firewall_bridge_ovs($iface, $bridge, $tag, $trunks);
70ab4434 376 } else {
b0b34ffd 377 &$ovs_bridge_add_port($bridge, $iface, $tag, undef, $trunks);
70ab4434 378 }
4cbabd40 379 }
bce2a5b3
WB
380
381 tap_rate_limit($iface, $rate);
f0c190ee
AD
382}
383
a84b65c0 384sub tap_unplug {
2db1cc0d 385 my ($iface) = @_;
a84b65c0 386
2db1cc0d
DM
387 my $path= "/sys/class/net/$iface/brport/bridge";
388 if (-l $path) {
389 my $bridge = basename(readlink($path));
390 #avoid insecure dependency;
391 ($bridge) = $bridge =~ /(\S+)/;
4cbabd40 392
098795e0 393 system("/sbin/brctl delif $bridge $iface") == 0 ||
2db1cc0d 394 die "can't del interface '$iface' from bridge '$bridge'\n";
605bb891 395
4cbabd40 396 }
70ab4434
DM
397
398 &$cleanup_firewall_bridge($iface);
dd44486e
WB
399 #cleanup old port config from any openvswitch bridge
400 eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) };
a84b65c0
AD
401}
402
b9436cda
DM
403sub copy_bridge_config {
404 my ($br0, $br1) = @_;
405
406 return if $br0 eq $br1;
407
408 my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
ba4af65b 409 'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier'];
b9436cda
DM
410
411 foreach my $sysname (@$br_configs) {
412 eval {
413 my $v0 = PVE::Tools::file_read_firstline("/sys/class/net/$br0/bridge/$sysname");
414 my $v1 = PVE::Tools::file_read_firstline("/sys/class/net/$br1/bridge/$sysname");
415 if ($v0 ne $v1) {
aec04803 416 PVE::ProcFSTools::write_proc_entry("/sys/class/net/$br1/bridge/$sysname", $v0);
b9436cda
DM
417 }
418 };
419 warn $@ if $@;
420 }
421}
422
70d89745
PRG
423sub activate_bridge_vlan_slave {
424 my ($bridgevlan, $iface, $tag) = @_;
b9436cda 425 my $ifacevlan = "${iface}.$tag";
70d89745 426
b9436cda
DM
427 # create vlan on $iface is not already exist
428 if (! -d "/sys/class/net/$ifacevlan") {
f3ccd9b4 429 system("/sbin/ip link add link $iface name $ifacevlan type vlan id $tag") == 0 ||
02c9a6b4 430 die "can't add vlan tag $tag to interface $iface\n";
b9436cda 431
86b84237
WB
432 # remove ipv6 link-local address before activation
433 disable_ipv6($ifacevlan);
434 }
f3ccd9b4 435
b9436cda 436 # be sure to have the $ifacevlan up
605bb891 437 &$activate_interface($ifacevlan);
b9436cda
DM
438
439 # test if $vlaniface is already enslaved in another bridge
440 my $path= "/sys/class/net/$ifacevlan/brport/bridge";
441 if (-l $path) {
442 my $tbridge = basename(readlink($path));
70d89745 443 if ($tbridge ne $bridgevlan) {
b9436cda 444 die "interface $ifacevlan already exist in bridge $tbridge\n";
eee4b32a
PRG
445 } else {
446 # Port already attached to bridge: do nothing.
447 return;
b9436cda
DM
448 }
449 }
450
70d89745 451 # add $ifacevlan to the bridge
605bb891 452 &$bridge_add_interface($bridgevlan, $ifacevlan);
70d89745
PRG
453}
454
455sub activate_bridge_vlan {
456 my ($bridge, $tag_param) = @_;
457
458 die "bridge '$bridge' is not active\n" if ! -d "/sys/class/net/$bridge";
459
460 return $bridge if !defined($tag_param); # no vlan, simply return
461
462 my $tag = int($tag_param);
463
464 die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
465
466 my $bridgevlan = "${bridge}v$tag";
467
c9030d97
PRG
468 my @ifaces = ();
469 my $dir = "/sys/class/net/$bridge/brif";
899f8c4a 470 PVE::Tools::dir_glob_foreach($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub {
5ffa7628 471 push @ifaces, $_[0];
c9030d97
PRG
472 });
473
5ffa7628 474 die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0;
c9030d97 475
a712bf6e
WB
476 lock_network(sub {
477 # add bridgevlan if it doesn't already exist
478 if (! -d "/sys/class/net/$bridgevlan") {
479 system("/sbin/brctl addbr $bridgevlan") == 0 ||
480 die "can't add bridge $bridgevlan\n";
481 }
b9436cda 482
a712bf6e
WB
483 # for each physical interface (eth or bridge) bind them to bridge vlan
484 foreach my $iface (@ifaces) {
485 activate_bridge_vlan_slave($bridgevlan, $iface, $tag);
486 }
70d89745 487
a712bf6e 488 #fixme: set other bridge flags
b9436cda 489
f3ccd9b4
WB
490 # remove ipv6 link-local address before activation
491 disable_ipv6($bridgevlan);
a712bf6e 492 # be sure to have the bridge up
f3ccd9b4 493 &$activate_interface($bridgevlan);
a712bf6e 494 });
b9436cda
DM
495 return $bridgevlan;
496}
497
b6bff92e
WB
498sub tcp_ping {
499 my ($host, $port, $timeout) = @_;
500
501 my $refused = 1;
502
503 $timeout = 3 if !$timeout; # sane default
504 if (!$port) {
505 # Net::Ping defaults to the echo port
506 $port = 7;
507 } else {
508 # Net::Ping's port_number() implies service_check(1)
509 $refused = 0;
510 }
511
512 my ($sock, $result);
513 eval {
514 $result = PVE::Tools::run_with_timeout($timeout, sub {
515 $sock = IO::Socket::IP->new(PeerHost => $host, PeerPort => $port, Type => SOCK_STREAM);
516 $result = $refused if $! == ECONNREFUSED;
517 });
518 };
519 if ($sock) {
520 $sock->close();
521 $result = 1;
522 }
523 return $result;
524}
525
bf52d27b
WB
526sub IP_from_cidr {
527 my ($cidr, $version) = @_;
528
529 return if $cidr !~ m!^(\S+?)/(\S+)$!;
530 my ($ip, $prefix) = ($1, $2);
531
532 my $ipobj = Net::IP->new($ip, $version);
533 return if !$ipobj;
534
535 $version = $ipobj->version();
536
537 my $binmask = Net::IP::ip_get_mask($prefix, $version);
538 return if !$binmask;
539
540 my $masked_binip = $ipobj->binip() & $binmask;
541 my $masked_ip = Net::IP::ip_bintoip($masked_binip, $version);
542 return Net::IP->new("$masked_ip/$prefix");
543}
544
545sub is_ip_in_cidr {
546 my ($ip, $cidr, $version) = @_;
547
548 my $cidr_obj = IP_from_cidr($cidr, $version);
549 return undef if !$cidr_obj;
550
551 my $ip_obj = Net::IP->new($ip, $version);
552 return undef if !$ip_obj;
553
554 return $cidr_obj->overlaps($ip_obj) == $Net::IP::IP_B_IN_A_OVERLAP;
555}
556
beb9820f
TL
557
558sub get_local_ip_from_cidr {
559 my ($cidr) = @_;
560
561 my $cmd = ['/sbin/ip', 'address', 'show', 'to', $cidr, 'up'];
562
563 my $IPs = [];
564
565 my $code = sub {
566 my $line = shift;
567
568 if ($line =~ m!^\s*inet(?:6)?\s+($PVE::Tools::IPRE)/\d+!) {
569 push @$IPs, $1;
570 }
571 };
572
573 PVE::Tools::run_command($cmd, outfunc => $code);
574
575 return $IPs;
576}
577
a712bf6e
WB
578sub lock_network {
579 my ($code, @param) = @_;
580 my $res = lock_file('/var/lock/pve-network.lck', 10, $code, @param);
581 die $@ if $@;
582 return $res;
583}
584
b9436cda 5851;