]>
Commit | Line | Data |
---|---|---|
aff192e6 DM |
1 | package PVE::API2::Network; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
d09f6f7d | 6 | use Net::IP qw(:PROC); |
cacd7547 | 7 | use PVE::Tools qw(extract_param dir_glob_regex); |
aff192e6 DM |
8 | use PVE::SafeSyslog; |
9 | use PVE::INotify; | |
10 | use PVE::Exception qw(raise_param_exc); | |
11 | use PVE::RESTHandler; | |
12 | use PVE::RPCEnvironment; | |
13 | use PVE::JSONSchema qw(get_standard_option); | |
14 | use PVE::AccessControl; | |
15 | use IO::File; | |
16 | ||
17 | use base qw(PVE::RESTHandler); | |
18 | ||
2bca9b77 AD |
19 | my $have_sdn; |
20 | eval { | |
30f5d476 | 21 | require PVE::Network::SDN; |
2bca9b77 AD |
22 | $have_sdn = 1; |
23 | }; | |
24 | ||
aff192e6 DM |
25 | my $iflockfn = "/etc/network/.pve-interfaces.lock"; |
26 | ||
27 | my $bond_mode_enum = [ | |
28 | 'balance-rr', | |
10a9563e | 29 | 'active-backup', # OVS and Linux |
aff192e6 DM |
30 | 'balance-xor', |
31 | 'broadcast', | |
32 | '802.3ad', | |
33 | 'balance-tlb', | |
d11733f8 | 34 | 'balance-alb', |
10a9563e DM |
35 | 'balance-slb', # OVS |
36 | 'lacp-balance-slb', # OVS | |
37 | 'lacp-balance-tcp', # OVS | |
aff192e6 DM |
38 | ]; |
39 | ||
3f0d1a4b | 40 | my $network_type_enum = ['bridge', 'bond', 'eth', 'alias', 'vlan', |
d11733f8 DM |
41 | 'OVSBridge', 'OVSBond', 'OVSPort', 'OVSIntPort']; |
42 | ||
aff192e6 | 43 | my $confdesc = { |
d11733f8 DM |
44 | type => { |
45 | description => "Network interface type", | |
46 | type => 'string', | |
47 | enum => [@$network_type_enum, 'unknown'], | |
48 | }, | |
44d353a7 DM |
49 | comments => { |
50 | description => "Comments", | |
51 | type => 'string', | |
b5eda023 | 52 | optional => 1, |
44d353a7 | 53 | }, |
3ed15e6c WB |
54 | comments6 => { |
55 | description => "Comments", | |
56 | type => 'string', | |
57 | optional => 1, | |
58 | }, | |
aff192e6 DM |
59 | autostart => { |
60 | description => "Automatically start interface on boot.", | |
61 | type => 'boolean', | |
62 | optional => 1, | |
63 | }, | |
a1604d70 AD |
64 | bridge_vlan_aware => { |
65 | description => "Enable bridge vlan support.", | |
66 | type => 'boolean', | |
67 | optional => 1, | |
68 | }, | |
aff192e6 | 69 | bridge_ports => { |
76189130 | 70 | description => "Specify the interfaces you want to add to your bridge.", |
aff192e6 DM |
71 | optional => 1, |
72 | type => 'string', format => 'pve-iface-list', | |
73 | }, | |
d11733f8 | 74 | ovs_ports => { |
76189130 | 75 | description => "Specify the interfaces you want to add to your bridge.", |
d11733f8 DM |
76 | optional => 1, |
77 | type => 'string', format => 'pve-iface-list', | |
78 | }, | |
4c917e97 DM |
79 | ovs_tag => { |
80 | description => "Specify a VLan tag (used by OVSPort, OVSIntPort, OVSBond)", | |
81 | optional => 1, | |
82 | type => 'integer', | |
83 | minimum => 1, | |
84 | maximum => 4094, | |
85 | }, | |
d11733f8 DM |
86 | ovs_options => { |
87 | description => "OVS interface options.", | |
88 | optional => 1, | |
89 | type => 'string', | |
90 | maxLength => 1024, | |
91 | }, | |
92 | ovs_bridge => { | |
93 | description => "The OVS bridge associated with a OVS port. This is required when you create an OVS port.", | |
94 | optional => 1, | |
95 | type => 'string', format => 'pve-iface', | |
96 | }, | |
aff192e6 DM |
97 | slaves => { |
98 | description => "Specify the interfaces used by the bonding device.", | |
99 | optional => 1, | |
100 | type => 'string', format => 'pve-iface-list', | |
101 | }, | |
d11733f8 DM |
102 | ovs_bonds => { |
103 | description => "Specify the interfaces used by the bonding device.", | |
104 | optional => 1, | |
105 | type => 'string', format => 'pve-iface-list', | |
106 | }, | |
aff192e6 DM |
107 | bond_mode => { |
108 | description => "Bonding mode.", | |
109 | optional => 1, | |
110 | type => 'string', enum => $bond_mode_enum, | |
111 | }, | |
7942a7bb AD |
112 | 'bond-primary' => { |
113 | description => "Specify the primary interface for active-backup bond.", | |
114 | optional => 1, | |
115 | type => 'string', format => 'pve-iface', | |
116 | }, | |
ffffb625 DM |
117 | bond_xmit_hash_policy => { |
118 | description => "Selects the transmit hash policy to use for slave selection in balance-xor and 802.3ad modes.", | |
119 | optional => 1, | |
120 | type => 'string', | |
121 | enum => ['layer2', 'layer2+3', 'layer3+4' ], | |
122 | }, | |
9d2e1c8b AD |
123 | 'vlan-raw-device' => { |
124 | description => "Specify the raw interface for the vlan interface.", | |
125 | optional => 1, | |
126 | type => 'string', format => 'pve-iface', | |
127 | }, | |
128 | 'vlan-id' => { | |
129 | description => "vlan-id for a custom named vlan interface (ifupdown2 only).", | |
130 | optional => 1, | |
131 | type => 'integer', | |
132 | minimum => 1, | |
133 | maximum => 4094, | |
134 | }, | |
aff192e6 DM |
135 | gateway => { |
136 | description => 'Default gateway address.', | |
137 | type => 'string', format => 'ipv4', | |
138 | optional => 1, | |
139 | }, | |
140 | netmask => { | |
141 | description => 'Network mask.', | |
142 | type => 'string', format => 'ipv4mask', | |
143 | optional => 1, | |
1904114e | 144 | requires => 'address', |
aff192e6 DM |
145 | }, |
146 | address => { | |
147 | description => 'IP address.', | |
148 | type => 'string', format => 'ipv4', | |
149 | optional => 1, | |
150 | requires => 'netmask', | |
3ed15e6c | 151 | }, |
69106e5c DC |
152 | cidr => { |
153 | description => 'IPv4 CIDR.', | |
e9af22b0 | 154 | type => 'string', format => 'CIDRv4', |
69106e5c DC |
155 | optional => 1, |
156 | }, | |
94011309 AD |
157 | mtu => { |
158 | description => 'MTU.', | |
159 | optional => 1, | |
160 | type => 'integer', | |
161 | minimum => 1280, | |
162 | maximum => 65520, | |
163 | }, | |
3ed15e6c WB |
164 | gateway6 => { |
165 | description => 'Default ipv6 gateway address.', | |
166 | type => 'string', format => 'ipv6', | |
167 | optional => 1, | |
168 | }, | |
169 | netmask6 => { | |
170 | description => 'Network mask.', | |
171 | type => 'integer', minimum => 0, maximum => 128, | |
172 | optional => 1, | |
173 | requires => 'address6', | |
174 | }, | |
175 | address6 => { | |
176 | description => 'IP address.', | |
177 | type => 'string', format => 'ipv6', | |
178 | optional => 1, | |
179 | requires => 'netmask6', | |
69106e5c DC |
180 | }, |
181 | cidr6 => { | |
182 | description => 'IPv6 CIDR.', | |
e9af22b0 | 183 | type => 'string', format => 'CIDRv6', |
69106e5c DC |
184 | optional => 1, |
185 | }, | |
aff192e6 DM |
186 | }; |
187 | ||
188 | sub json_config_properties { | |
189 | my $prop = shift; | |
190 | ||
191 | foreach my $opt (keys %$confdesc) { | |
192 | $prop->{$opt} = $confdesc->{$opt}; | |
193 | } | |
194 | ||
195 | return $prop; | |
196 | } | |
197 | ||
198 | __PACKAGE__->register_method({ | |
199 | name => 'index', | |
200 | path => '', | |
201 | method => 'GET', | |
202 | permissions => { user => 'all' }, | |
203 | description => "List available networks", | |
204 | proxyto => 'node', | |
205 | parameters => { | |
206 | additionalProperties => 0, | |
207 | properties => { | |
208 | node => get_standard_option('pve-node'), | |
209 | type => { | |
210 | description => "Only list specific interface types.", | |
211 | type => 'string', | |
4c917e97 | 212 | enum => [ @$network_type_enum, 'any_bridge' ], |
aff192e6 DM |
213 | optional => 1, |
214 | }, | |
215 | }, | |
216 | }, | |
217 | returns => { | |
218 | type => "array", | |
219 | items => { | |
220 | type => "object", | |
221 | properties => {}, | |
222 | }, | |
223 | links => [ { rel => 'child', href => "{iface}" } ], | |
224 | }, | |
225 | code => sub { | |
226 | my ($param) = @_; | |
227 | ||
e4d5bf72 DM |
228 | my $rpcenv = PVE::RPCEnvironment::get(); |
229 | ||
230 | my $tmp = PVE::INotify::read_file('interfaces', 1); | |
231 | my $config = $tmp->{data}; | |
232 | my $changes = $tmp->{changes}; | |
233 | ||
234 | $rpcenv->set_result_attrib('changes', $changes) if $changes; | |
aff192e6 | 235 | |
3ed15e6c WB |
236 | my $ifaces = $config->{ifaces}; |
237 | ||
238 | delete $ifaces->{lo}; # do not list the loopback device | |
aff192e6 DM |
239 | |
240 | if ($param->{type}) { | |
3ed15e6c WB |
241 | foreach my $k (keys %$ifaces) { |
242 | my $type = $ifaces->{$k}->{type}; | |
4c917e97 DM |
243 | my $match = ($param->{type} eq $type) || ( |
244 | ($param->{type} eq 'any_bridge') && | |
245 | ($type eq 'bridge' || $type eq 'OVSBridge')); | |
3ed15e6c | 246 | delete $ifaces->{$k} if !$match; |
aff192e6 | 247 | } |
30f5d476 AD |
248 | |
249 | if ($have_sdn && $param->{type} eq 'any_bridge') { | |
250 | my $vnets = PVE::Network::SDN::get_local_vnets(); | |
251 | map { | |
252 | $ifaces->{$_} = $vnets->{$_}; | |
253 | } keys %$vnets; | |
254 | } | |
aff192e6 DM |
255 | } |
256 | ||
3ed15e6c | 257 | return PVE::RESTHandler::hash_to_array($ifaces, 'iface'); |
aff192e6 DM |
258 | }}); |
259 | ||
e4d5bf72 DM |
260 | __PACKAGE__->register_method({ |
261 | name => 'revert_network_changes', | |
262 | path => '', | |
263 | method => 'DELETE', | |
264 | permissions => { | |
265 | check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]], | |
266 | }, | |
267 | protected => 1, | |
268 | description => "Revert network configuration changes.", | |
269 | proxyto => 'node', | |
270 | parameters => { | |
271 | additionalProperties => 0, | |
272 | properties => { | |
273 | node => get_standard_option('pve-node'), | |
274 | }, | |
275 | }, | |
276 | returns => { type => "null" }, | |
277 | code => sub { | |
278 | my ($param) = @_; | |
279 | ||
280 | unlink "/etc/network/interfaces.new"; | |
281 | ||
282 | return undef; | |
283 | }}); | |
aff192e6 | 284 | |
3ed15e6c WB |
285 | my $check_duplicate = sub { |
286 | my ($config, $newiface, $key, $name) = @_; | |
aff192e6 DM |
287 | |
288 | foreach my $iface (keys %$config) { | |
3ed15e6c WB |
289 | raise_param_exc({ $key => "$name already exists on interface '$iface'." }) |
290 | if ($newiface ne $iface) && $config->{$iface}->{$key}; | |
aff192e6 DM |
291 | } |
292 | }; | |
293 | ||
3ed15e6c WB |
294 | my $check_duplicate_gateway = sub { |
295 | my ($config, $newiface) = @_; | |
296 | return &$check_duplicate($config, $newiface, 'gateway', 'Default gateway'); | |
297 | }; | |
298 | ||
299 | my $check_duplicate_gateway6 = sub { | |
300 | my ($config, $newiface) = @_; | |
301 | return &$check_duplicate($config, $newiface, 'gateway6', 'Default ipv6 gateway'); | |
302 | }; | |
303 | ||
3ed15e6c WB |
304 | sub ipv6_tobin { |
305 | return Net::IP::ip_iptobin(Net::IP::ip_expand_address(shift, 6), 6); | |
306 | } | |
307 | ||
308 | my $check_ipv6_settings = sub { | |
309 | my ($address, $netmask) = @_; | |
310 | ||
311 | raise_param_exc({ netmask => "$netmask is not a valid subnet length for ipv6" }) | |
312 | if $netmask < 0 || $netmask > 128; | |
313 | ||
0f6e6f6b | 314 | raise_param_exc({ address => "$address is not a valid host IPv6 address." }) |
3ed15e6c WB |
315 | if !Net::IP::ip_is_ipv6($address); |
316 | ||
317 | my $binip = ipv6_tobin($address); | |
318 | my $binmask = Net::IP::ip_get_mask($netmask, 6); | |
319 | ||
0f6e6f6b | 320 | my $type = ($binip eq $binmask) ? 'ANYCAST' : Net::IP::ip_iptypev6($binip); |
3ed15e6c | 321 | |
0f6e6f6b | 322 | if (defined($type) && $type !~ /^(?:(?:GLOBAL|(?:UNIQUE|LINK)-LOCAL)-UNICAST)$/) { |
68f371d4 | 323 | raise_param_exc({ address => "$address with type '$type', cannot be used as host IPv6 address." }); |
0f6e6f6b | 324 | } |
3ed15e6c WB |
325 | }; |
326 | ||
69106e5c DC |
327 | my $map_cidr_to_address_netmask = sub { |
328 | my ($param) = @_; | |
329 | ||
330 | if ($param->{cidr}) { | |
331 | raise_param_exc({ address => "address conflicts with cidr" }) | |
332 | if $param->{address}; | |
333 | raise_param_exc({ netmask => "netmask conflicts with cidr" }) | |
334 | if $param->{netmask}; | |
335 | ||
e9af22b0 TL |
336 | my ($address, $netmask) = $param->{cidr} =~ m!^(.*)/(\d+)$!; |
337 | $param->{address} = $address; | |
338 | $param->{netmask} = $netmask; | |
69106e5c DC |
339 | delete $param->{cidr}; |
340 | } | |
341 | ||
342 | if ($param->{cidr6}) { | |
343 | raise_param_exc({ address6 => "address6 conflicts with cidr6" }) | |
344 | if $param->{address6}; | |
345 | raise_param_exc({ netmask6 => "netmask6 conflicts with cidr6" }) | |
346 | if $param->{netmask6}; | |
347 | ||
e9af22b0 TL |
348 | my ($address, $netmask) = $param->{cidr6} =~ m!^(.*)/(\d+)$!; |
349 | $param->{address6} = $address; | |
350 | $param->{netmask6} = $netmask; | |
69106e5c DC |
351 | delete $param->{cidr6}; |
352 | } | |
353 | }; | |
354 | ||
aff192e6 DM |
355 | __PACKAGE__->register_method({ |
356 | name => 'create_network', | |
357 | path => '', | |
358 | method => 'POST', | |
359 | permissions => { | |
7d020b42 | 360 | check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]], |
aff192e6 DM |
361 | }, |
362 | description => "Create network device configuration", | |
363 | protected => 1, | |
364 | proxyto => 'node', | |
365 | parameters => { | |
366 | additionalProperties => 0, | |
367 | properties => json_config_properties({ | |
368 | node => get_standard_option('pve-node'), | |
369 | iface => get_standard_option('pve-iface')}), | |
370 | }, | |
371 | returns => { type => 'null' }, | |
372 | code => sub { | |
373 | my ($param) = @_; | |
374 | ||
375 | my $node = extract_param($param, 'node'); | |
376 | my $iface = extract_param($param, 'iface'); | |
377 | ||
378 | my $code = sub { | |
379 | my $config = PVE::INotify::read_file('interfaces'); | |
3ed15e6c | 380 | my $ifaces = $config->{ifaces}; |
aff192e6 DM |
381 | |
382 | raise_param_exc({ iface => "interface already exists" }) | |
3ed15e6c | 383 | if $ifaces->{$iface}; |
aff192e6 | 384 | |
3ed15e6c | 385 | &$check_duplicate_gateway($ifaces, $iface) |
aff192e6 | 386 | if $param->{gateway}; |
3ed15e6c WB |
387 | &$check_duplicate_gateway6($ifaces, $iface) |
388 | if $param->{gateway6}; | |
aff192e6 | 389 | |
69106e5c DC |
390 | $map_cidr_to_address_netmask->($param); |
391 | ||
3ed15e6c WB |
392 | &$check_ipv6_settings($param->{address6}, int($param->{netmask6})) |
393 | if $param->{address6}; | |
394 | ||
395 | my $families = $param->{families} = []; | |
396 | push @$families, 'inet' | |
397 | if $param->{address} && !grep(/^inet$/, @$families); | |
398 | push @$families, 'inet6' | |
399 | if $param->{address6} && !grep(/^inet6$/, @$families); | |
400 | @$families = ('inet') if !scalar(@$families); | |
e16a27be | 401 | |
aff192e6 | 402 | $param->{method} = $param->{address} ? 'static' : 'manual'; |
3ed15e6c | 403 | $param->{method6} = $param->{address6} ? 'static' : 'manual'; |
aff192e6 | 404 | |
bdfa2498 DM |
405 | if ($param->{type} =~ m/^OVS/) { |
406 | -x '/usr/bin/ovs-vsctl' || | |
407 | die "Open VSwitch is not installed (need package 'openvswitch-switch')\n"; | |
408 | } | |
409 | ||
d11733f8 DM |
410 | if ($param->{type} eq 'OVSIntPort' || $param->{type} eq 'OVSBond') { |
411 | my $brname = $param->{ovs_bridge}; | |
412 | raise_param_exc({ ovs_bridge => "parameter is required" }) if !$brname; | |
3ed15e6c | 413 | my $br = $ifaces->{$brname}; |
d11733f8 DM |
414 | raise_param_exc({ ovs_bridge => "bridge '$brname' does not exist" }) if !$br; |
415 | raise_param_exc({ ovs_bridge => "interface '$brname' is no OVS bridge" }) | |
416 | if $br->{type} ne 'OVSBridge'; | |
417 | ||
418 | my @ports = split (/\s+/, $br->{ovs_ports} || ''); | |
419 | $br->{ovs_ports} = join(' ', @ports, $iface) | |
420 | if ! grep { $_ eq $iface } @ports; | |
421 | } | |
422 | ||
3ed15e6c | 423 | $ifaces->{$iface} = $param; |
aff192e6 DM |
424 | |
425 | PVE::INotify::write_file('interfaces', $config); | |
426 | }; | |
427 | ||
428 | PVE::Tools::lock_file($iflockfn, 10, $code); | |
429 | die $@ if $@; | |
430 | ||
431 | return undef; | |
432 | }}); | |
433 | ||
434 | __PACKAGE__->register_method({ | |
435 | name => 'update_network', | |
436 | path => '{iface}', | |
437 | method => 'PUT', | |
438 | permissions => { | |
7d020b42 | 439 | check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]], |
aff192e6 DM |
440 | }, |
441 | description => "Update network device configuration", | |
442 | protected => 1, | |
443 | proxyto => 'node', | |
444 | parameters => { | |
445 | additionalProperties => 0, | |
446 | properties => json_config_properties({ | |
447 | node => get_standard_option('pve-node'), | |
448 | iface => get_standard_option('pve-iface'), | |
449 | delete => { | |
450 | type => 'string', format => 'pve-configid-list', | |
451 | description => "A list of settings you want to delete.", | |
452 | optional => 1, | |
453 | }}), | |
454 | }, | |
455 | returns => { type => 'null' }, | |
456 | code => sub { | |
457 | my ($param) = @_; | |
458 | ||
459 | my $node = extract_param($param, 'node'); | |
460 | my $iface = extract_param($param, 'iface'); | |
461 | my $delete = extract_param($param, 'delete'); | |
462 | ||
463 | my $code = sub { | |
464 | my $config = PVE::INotify::read_file('interfaces'); | |
3ed15e6c | 465 | my $ifaces = $config->{ifaces}; |
aff192e6 DM |
466 | |
467 | raise_param_exc({ iface => "interface does not exist" }) | |
3ed15e6c | 468 | if !$ifaces->{$iface}; |
aff192e6 | 469 | |
3ed15e6c | 470 | my $families = ($param->{families} ||= []); |
aff192e6 | 471 | foreach my $k (PVE::Tools::split_list($delete)) { |
3ed15e6c WB |
472 | delete $ifaces->{$iface}->{$k}; |
473 | @$families = grep(!/^inet$/, @$families) if $k eq 'address'; | |
474 | @$families = grep(!/^inet6$/, @$families) if $k eq 'address6'; | |
47d13c02 DC |
475 | if ($k eq 'cidr') { |
476 | delete $ifaces->{$iface}->{netmask}; | |
477 | delete $ifaces->{$iface}->{address}; | |
478 | } elsif ($k eq 'cidr6') { | |
479 | delete $ifaces->{$iface}->{netmask6}; | |
480 | delete $ifaces->{$iface}->{address6}; | |
481 | } | |
aff192e6 DM |
482 | } |
483 | ||
69106e5c DC |
484 | $map_cidr_to_address_netmask->($param); |
485 | ||
3ed15e6c | 486 | &$check_duplicate_gateway($ifaces, $iface) |
aff192e6 | 487 | if $param->{gateway}; |
3ed15e6c WB |
488 | &$check_duplicate_gateway6($ifaces, $iface) |
489 | if $param->{gateway6}; | |
490 | ||
491 | if ($param->{address}) { | |
3ed15e6c WB |
492 | push @$families, 'inet' if !grep(/^inet$/, @$families); |
493 | } else { | |
494 | @$families = grep(!/^inet$/, @$families); | |
495 | } | |
496 | if ($param->{address6}) { | |
497 | &$check_ipv6_settings($param->{address6}, int($param->{netmask6})); | |
498 | push @$families, 'inet6' if !grep(/^inet6$/, @$families); | |
499 | } else { | |
500 | @$families = grep(!/^inet6$/, @$families); | |
501 | } | |
502 | @$families = ('inet') if !scalar(@$families); | |
e16a27be | 503 | |
aff192e6 | 504 | $param->{method} = $param->{address} ? 'static' : 'manual'; |
3ed15e6c | 505 | $param->{method6} = $param->{address6} ? 'static' : 'manual'; |
aff192e6 DM |
506 | |
507 | foreach my $k (keys %$param) { | |
3ed15e6c | 508 | $ifaces->{$iface}->{$k} = $param->{$k}; |
aff192e6 DM |
509 | } |
510 | ||
511 | PVE::INotify::write_file('interfaces', $config); | |
512 | }; | |
513 | ||
514 | PVE::Tools::lock_file($iflockfn, 10, $code); | |
515 | die $@ if $@; | |
516 | ||
517 | return undef; | |
518 | }}); | |
519 | ||
520 | __PACKAGE__->register_method({ | |
521 | name => 'network_config', | |
522 | path => '{iface}', | |
523 | method => 'GET', | |
524 | permissions => { | |
7d020b42 | 525 | check => ['perm', '/nodes/{node}', [ 'Sys.Audit' ]], |
aff192e6 DM |
526 | }, |
527 | description => "Read network device configuration", | |
528 | proxyto => 'node', | |
529 | parameters => { | |
530 | additionalProperties => 0, | |
531 | properties => { | |
532 | node => get_standard_option('pve-node'), | |
533 | iface => get_standard_option('pve-iface'), | |
534 | }, | |
535 | }, | |
536 | returns => { | |
537 | type => "object", | |
538 | properties => { | |
539 | type => { | |
540 | type => 'string', | |
541 | }, | |
542 | method => { | |
543 | type => 'string', | |
544 | }, | |
545 | }, | |
546 | }, | |
547 | code => sub { | |
548 | my ($param) = @_; | |
549 | ||
550 | my $config = PVE::INotify::read_file('interfaces'); | |
3ed15e6c | 551 | my $ifaces = $config->{ifaces}; |
aff192e6 DM |
552 | |
553 | raise_param_exc({ iface => "interface does not exist" }) | |
3ed15e6c | 554 | if !$ifaces->{$param->{iface}}; |
aff192e6 | 555 | |
3ed15e6c | 556 | return $ifaces->{$param->{iface}}; |
aff192e6 DM |
557 | }}); |
558 | ||
a6ed0aa6 TL |
559 | sub ifupdown2_version { |
560 | my $v; | |
561 | PVE::Tools::run_command(['ifreload', '-V'], outfunc => sub { $v //= shift }); | |
562 | return if !defined($v) || $v !~ /^\s*ifupdown2:(\S+)\s*$/; | |
563 | $v = $1; | |
564 | my ($major, $minor, $extra, $pve) = split(/\.|-/, $v); | |
565 | my $is_pve = defined($pve) && $pve =~ /pve/; | |
566 | ||
567 | return ($major * 100000 + $minor * 1000 + $extra * 10, $is_pve, $v); | |
568 | } | |
569 | sub assert_ifupdown2_installed { | |
570 | die "you need ifupdown2 to reload network configuration\n" if ! -e '/usr/share/ifupdown2'; | |
571 | my ($v, $pve, $v_str) = ifupdown2_version(); | |
572 | die "incompatible 'ifupdown2' package version '$v_str'! Did you installed from Proxmox repositories?\n" | |
573 | if $v < (1*100000 + 2*1000 + 8*10) || !$pve; | |
574 | } | |
575 | ||
cacd7547 AD |
576 | __PACKAGE__->register_method({ |
577 | name => 'reload_network_config', | |
578 | path => '', | |
579 | method => 'PUT', | |
580 | permissions => { | |
581 | check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]], | |
582 | }, | |
583 | description => "Reload network configuration", | |
584 | protected => 1, | |
585 | proxyto => 'node', | |
586 | parameters => { | |
587 | additionalProperties => 0, | |
588 | properties => { | |
589 | node => get_standard_option('pve-node'), | |
590 | }, | |
591 | }, | |
592 | returns => { type => 'string' }, | |
593 | code => sub { | |
594 | ||
595 | my ($param) = @_; | |
596 | ||
597 | my $rpcenv = PVE::RPCEnvironment::get(); | |
598 | ||
599 | my $authuser = $rpcenv->get_user(); | |
600 | ||
601 | my $current_config_file = "/etc/network/interfaces"; | |
602 | my $new_config_file = "/etc/network/interfaces.new"; | |
603 | ||
a6ed0aa6 | 604 | assert_ifupdown2_installed(); |
6159470e | 605 | |
cacd7547 AD |
606 | my $worker = sub { |
607 | ||
e46bf624 | 608 | rename($new_config_file, $current_config_file) if -e $new_config_file; |
cacd7547 | 609 | |
2bca9b77 | 610 | if ($have_sdn) { |
9ad4656d | 611 | PVE::Network::SDN::generate_zone_config(); |
2bca9b77 AD |
612 | } |
613 | ||
cacd7547 AD |
614 | my $err = sub { |
615 | my $line = shift; | |
616 | if ($line =~ /(warning|error): (\S+):/) { | |
e46bf624 | 617 | print "$2 : $line \n"; |
cacd7547 AD |
618 | } |
619 | }; | |
084e6030 | 620 | PVE::Tools::run_command(['ifreload', '-a'], errfunc => $err); |
cacd7547 | 621 | |
20dc8bbe | 622 | if ($have_sdn) { |
9ad4656d | 623 | PVE::Network::SDN::generate_controller_config(1); |
084e6030 | 624 | } |
cacd7547 AD |
625 | }; |
626 | return $rpcenv->fork_worker('srvreload', 'networking', $authuser, $worker); | |
627 | }}); | |
628 | ||
aff192e6 DM |
629 | __PACKAGE__->register_method({ |
630 | name => 'delete_network', | |
631 | path => '{iface}', | |
632 | method => 'DELETE', | |
633 | permissions => { | |
7d020b42 | 634 | check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]], |
aff192e6 DM |
635 | }, |
636 | description => "Delete network device configuration", | |
637 | protected => 1, | |
638 | proxyto => 'node', | |
639 | parameters => { | |
640 | additionalProperties => 0, | |
641 | properties => { | |
642 | node => get_standard_option('pve-node'), | |
643 | iface => get_standard_option('pve-iface'), | |
644 | }, | |
645 | }, | |
646 | returns => { type => 'null' }, | |
647 | code => sub { | |
648 | my ($param) = @_; | |
649 | ||
650 | my $code = sub { | |
651 | my $config = PVE::INotify::read_file('interfaces'); | |
3ed15e6c | 652 | my $ifaces = $config->{ifaces}; |
aff192e6 DM |
653 | |
654 | raise_param_exc({ iface => "interface does not exist" }) | |
3ed15e6c | 655 | if !$ifaces->{$param->{iface}}; |
aff192e6 | 656 | |
3ed15e6c | 657 | my $d = $ifaces->{$param->{iface}}; |
d11733f8 DM |
658 | if ($d->{type} eq 'OVSIntPort' || $d->{type} eq 'OVSBond') { |
659 | if (my $brname = $d->{ovs_bridge}) { | |
3ed15e6c | 660 | if (my $br = $ifaces->{$brname}) { |
d11733f8 DM |
661 | if ($br->{ovs_ports}) { |
662 | my @ports = split (/\s+/, $br->{ovs_ports}); | |
663 | my @new = grep { $_ ne $param->{iface} } @ports; | |
664 | $br->{ovs_ports} = join(' ', @new); | |
665 | } | |
666 | } | |
667 | } | |
668 | } | |
669 | ||
3ed15e6c | 670 | delete $ifaces->{$param->{iface}}; |
aff192e6 DM |
671 | |
672 | PVE::INotify::write_file('interfaces', $config); | |
673 | }; | |
674 | ||
675 | PVE::Tools::lock_file($iflockfn, 10, $code); | |
676 | die $@ if $@; | |
677 | ||
678 | return undef; | |
679 | }}); |