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