]>
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 | ||
6cd854d8 SI |
304 | my $check_duplicate_ports = sub { |
305 | my ($config, $newiface, $newparam) = @_; | |
306 | ||
307 | my $param_name; | |
308 | my $get_portlist = sub { | |
309 | my ($param) = @_; | |
310 | my $ports = ''; | |
311 | for my $k (qw(bridge_ports ovs_ports slaves ovs_bonds)) { | |
312 | if ($param->{$k}) { | |
313 | $ports .= " $param->{$k}"; | |
314 | $param_name //= $k; | |
315 | } | |
316 | } | |
317 | return PVE::Tools::split_list($ports); | |
318 | }; | |
319 | ||
320 | my $new_ports = {}; | |
321 | for my $p ($get_portlist->($newparam)) { | |
322 | $new_ports->{$p} = 1; | |
323 | } | |
324 | return if !(keys %$new_ports); | |
325 | ||
326 | for my $iface (keys %$config) { | |
327 | next if $iface eq $newiface; | |
328 | ||
329 | my $d = $config->{$iface}; | |
330 | for my $p ($get_portlist->($d)) { | |
331 | raise_param_exc({ $param_name => "$p is already used on interface '$iface'." }) | |
332 | if $new_ports->{$p}; | |
333 | } | |
334 | } | |
335 | }; | |
336 | ||
3ed15e6c WB |
337 | sub ipv6_tobin { |
338 | return Net::IP::ip_iptobin(Net::IP::ip_expand_address(shift, 6), 6); | |
339 | } | |
340 | ||
341 | my $check_ipv6_settings = sub { | |
342 | my ($address, $netmask) = @_; | |
343 | ||
344 | raise_param_exc({ netmask => "$netmask is not a valid subnet length for ipv6" }) | |
345 | if $netmask < 0 || $netmask > 128; | |
346 | ||
0f6e6f6b | 347 | raise_param_exc({ address => "$address is not a valid host IPv6 address." }) |
3ed15e6c WB |
348 | if !Net::IP::ip_is_ipv6($address); |
349 | ||
350 | my $binip = ipv6_tobin($address); | |
351 | my $binmask = Net::IP::ip_get_mask($netmask, 6); | |
352 | ||
0f6e6f6b | 353 | my $type = ($binip eq $binmask) ? 'ANYCAST' : Net::IP::ip_iptypev6($binip); |
3ed15e6c | 354 | |
0f6e6f6b | 355 | if (defined($type) && $type !~ /^(?:(?:GLOBAL|(?:UNIQUE|LINK)-LOCAL)-UNICAST)$/) { |
68f371d4 | 356 | raise_param_exc({ address => "$address with type '$type', cannot be used as host IPv6 address." }); |
0f6e6f6b | 357 | } |
3ed15e6c WB |
358 | }; |
359 | ||
69106e5c DC |
360 | my $map_cidr_to_address_netmask = sub { |
361 | my ($param) = @_; | |
362 | ||
363 | if ($param->{cidr}) { | |
364 | raise_param_exc({ address => "address conflicts with cidr" }) | |
365 | if $param->{address}; | |
366 | raise_param_exc({ netmask => "netmask conflicts with cidr" }) | |
367 | if $param->{netmask}; | |
368 | ||
e9af22b0 TL |
369 | my ($address, $netmask) = $param->{cidr} =~ m!^(.*)/(\d+)$!; |
370 | $param->{address} = $address; | |
371 | $param->{netmask} = $netmask; | |
69106e5c DC |
372 | delete $param->{cidr}; |
373 | } | |
374 | ||
375 | if ($param->{cidr6}) { | |
376 | raise_param_exc({ address6 => "address6 conflicts with cidr6" }) | |
377 | if $param->{address6}; | |
378 | raise_param_exc({ netmask6 => "netmask6 conflicts with cidr6" }) | |
379 | if $param->{netmask6}; | |
380 | ||
e9af22b0 TL |
381 | my ($address, $netmask) = $param->{cidr6} =~ m!^(.*)/(\d+)$!; |
382 | $param->{address6} = $address; | |
383 | $param->{netmask6} = $netmask; | |
69106e5c DC |
384 | delete $param->{cidr6}; |
385 | } | |
386 | }; | |
387 | ||
aff192e6 DM |
388 | __PACKAGE__->register_method({ |
389 | name => 'create_network', | |
390 | path => '', | |
391 | method => 'POST', | |
392 | permissions => { | |
7d020b42 | 393 | check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]], |
aff192e6 DM |
394 | }, |
395 | description => "Create network device configuration", | |
396 | protected => 1, | |
397 | proxyto => 'node', | |
398 | parameters => { | |
399 | additionalProperties => 0, | |
400 | properties => json_config_properties({ | |
401 | node => get_standard_option('pve-node'), | |
402 | iface => get_standard_option('pve-iface')}), | |
403 | }, | |
404 | returns => { type => 'null' }, | |
405 | code => sub { | |
406 | my ($param) = @_; | |
407 | ||
408 | my $node = extract_param($param, 'node'); | |
409 | my $iface = extract_param($param, 'iface'); | |
410 | ||
411 | my $code = sub { | |
412 | my $config = PVE::INotify::read_file('interfaces'); | |
3ed15e6c | 413 | my $ifaces = $config->{ifaces}; |
aff192e6 DM |
414 | |
415 | raise_param_exc({ iface => "interface already exists" }) | |
3ed15e6c | 416 | if $ifaces->{$iface}; |
aff192e6 | 417 | |
3ed15e6c | 418 | &$check_duplicate_gateway($ifaces, $iface) |
aff192e6 | 419 | if $param->{gateway}; |
3ed15e6c WB |
420 | &$check_duplicate_gateway6($ifaces, $iface) |
421 | if $param->{gateway6}; | |
aff192e6 | 422 | |
6cd854d8 SI |
423 | $check_duplicate_ports->($ifaces, $iface, $param); |
424 | ||
69106e5c DC |
425 | $map_cidr_to_address_netmask->($param); |
426 | ||
3ed15e6c WB |
427 | &$check_ipv6_settings($param->{address6}, int($param->{netmask6})) |
428 | if $param->{address6}; | |
429 | ||
430 | my $families = $param->{families} = []; | |
431 | push @$families, 'inet' | |
432 | if $param->{address} && !grep(/^inet$/, @$families); | |
433 | push @$families, 'inet6' | |
434 | if $param->{address6} && !grep(/^inet6$/, @$families); | |
435 | @$families = ('inet') if !scalar(@$families); | |
e16a27be | 436 | |
aff192e6 | 437 | $param->{method} = $param->{address} ? 'static' : 'manual'; |
3ed15e6c | 438 | $param->{method6} = $param->{address6} ? 'static' : 'manual'; |
aff192e6 | 439 | |
bdfa2498 DM |
440 | if ($param->{type} =~ m/^OVS/) { |
441 | -x '/usr/bin/ovs-vsctl' || | |
442 | die "Open VSwitch is not installed (need package 'openvswitch-switch')\n"; | |
443 | } | |
444 | ||
d11733f8 DM |
445 | if ($param->{type} eq 'OVSIntPort' || $param->{type} eq 'OVSBond') { |
446 | my $brname = $param->{ovs_bridge}; | |
447 | raise_param_exc({ ovs_bridge => "parameter is required" }) if !$brname; | |
3ed15e6c | 448 | my $br = $ifaces->{$brname}; |
d11733f8 DM |
449 | raise_param_exc({ ovs_bridge => "bridge '$brname' does not exist" }) if !$br; |
450 | raise_param_exc({ ovs_bridge => "interface '$brname' is no OVS bridge" }) | |
451 | if $br->{type} ne 'OVSBridge'; | |
452 | ||
453 | my @ports = split (/\s+/, $br->{ovs_ports} || ''); | |
454 | $br->{ovs_ports} = join(' ', @ports, $iface) | |
455 | if ! grep { $_ eq $iface } @ports; | |
456 | } | |
457 | ||
3ed15e6c | 458 | $ifaces->{$iface} = $param; |
aff192e6 DM |
459 | |
460 | PVE::INotify::write_file('interfaces', $config); | |
461 | }; | |
462 | ||
463 | PVE::Tools::lock_file($iflockfn, 10, $code); | |
464 | die $@ if $@; | |
465 | ||
466 | return undef; | |
467 | }}); | |
468 | ||
469 | __PACKAGE__->register_method({ | |
470 | name => 'update_network', | |
471 | path => '{iface}', | |
472 | method => 'PUT', | |
473 | permissions => { | |
7d020b42 | 474 | check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]], |
aff192e6 DM |
475 | }, |
476 | description => "Update network device configuration", | |
477 | protected => 1, | |
478 | proxyto => 'node', | |
479 | parameters => { | |
480 | additionalProperties => 0, | |
481 | properties => json_config_properties({ | |
482 | node => get_standard_option('pve-node'), | |
483 | iface => get_standard_option('pve-iface'), | |
484 | delete => { | |
485 | type => 'string', format => 'pve-configid-list', | |
486 | description => "A list of settings you want to delete.", | |
487 | optional => 1, | |
488 | }}), | |
489 | }, | |
490 | returns => { type => 'null' }, | |
491 | code => sub { | |
492 | my ($param) = @_; | |
493 | ||
494 | my $node = extract_param($param, 'node'); | |
495 | my $iface = extract_param($param, 'iface'); | |
496 | my $delete = extract_param($param, 'delete'); | |
497 | ||
498 | my $code = sub { | |
499 | my $config = PVE::INotify::read_file('interfaces'); | |
3ed15e6c | 500 | my $ifaces = $config->{ifaces}; |
aff192e6 DM |
501 | |
502 | raise_param_exc({ iface => "interface does not exist" }) | |
3ed15e6c | 503 | if !$ifaces->{$iface}; |
aff192e6 | 504 | |
3ed15e6c | 505 | my $families = ($param->{families} ||= []); |
aff192e6 | 506 | foreach my $k (PVE::Tools::split_list($delete)) { |
3ed15e6c WB |
507 | delete $ifaces->{$iface}->{$k}; |
508 | @$families = grep(!/^inet$/, @$families) if $k eq 'address'; | |
509 | @$families = grep(!/^inet6$/, @$families) if $k eq 'address6'; | |
47d13c02 DC |
510 | if ($k eq 'cidr') { |
511 | delete $ifaces->{$iface}->{netmask}; | |
512 | delete $ifaces->{$iface}->{address}; | |
513 | } elsif ($k eq 'cidr6') { | |
514 | delete $ifaces->{$iface}->{netmask6}; | |
515 | delete $ifaces->{$iface}->{address6}; | |
516 | } | |
aff192e6 DM |
517 | } |
518 | ||
69106e5c DC |
519 | $map_cidr_to_address_netmask->($param); |
520 | ||
3ed15e6c | 521 | &$check_duplicate_gateway($ifaces, $iface) |
aff192e6 | 522 | if $param->{gateway}; |
3ed15e6c WB |
523 | &$check_duplicate_gateway6($ifaces, $iface) |
524 | if $param->{gateway6}; | |
525 | ||
6cd854d8 SI |
526 | $check_duplicate_ports->($ifaces, $iface, $param); |
527 | ||
3ed15e6c | 528 | if ($param->{address}) { |
3ed15e6c WB |
529 | push @$families, 'inet' if !grep(/^inet$/, @$families); |
530 | } else { | |
531 | @$families = grep(!/^inet$/, @$families); | |
532 | } | |
533 | if ($param->{address6}) { | |
534 | &$check_ipv6_settings($param->{address6}, int($param->{netmask6})); | |
535 | push @$families, 'inet6' if !grep(/^inet6$/, @$families); | |
536 | } else { | |
537 | @$families = grep(!/^inet6$/, @$families); | |
538 | } | |
539 | @$families = ('inet') if !scalar(@$families); | |
e16a27be | 540 | |
aff192e6 | 541 | $param->{method} = $param->{address} ? 'static' : 'manual'; |
3ed15e6c | 542 | $param->{method6} = $param->{address6} ? 'static' : 'manual'; |
aff192e6 DM |
543 | |
544 | foreach my $k (keys %$param) { | |
3ed15e6c | 545 | $ifaces->{$iface}->{$k} = $param->{$k}; |
aff192e6 DM |
546 | } |
547 | ||
548 | PVE::INotify::write_file('interfaces', $config); | |
549 | }; | |
550 | ||
551 | PVE::Tools::lock_file($iflockfn, 10, $code); | |
552 | die $@ if $@; | |
553 | ||
554 | return undef; | |
555 | }}); | |
556 | ||
557 | __PACKAGE__->register_method({ | |
558 | name => 'network_config', | |
559 | path => '{iface}', | |
560 | method => 'GET', | |
561 | permissions => { | |
7d020b42 | 562 | check => ['perm', '/nodes/{node}', [ 'Sys.Audit' ]], |
aff192e6 DM |
563 | }, |
564 | description => "Read network device configuration", | |
565 | proxyto => 'node', | |
566 | parameters => { | |
567 | additionalProperties => 0, | |
568 | properties => { | |
569 | node => get_standard_option('pve-node'), | |
570 | iface => get_standard_option('pve-iface'), | |
571 | }, | |
572 | }, | |
573 | returns => { | |
574 | type => "object", | |
575 | properties => { | |
576 | type => { | |
577 | type => 'string', | |
578 | }, | |
579 | method => { | |
580 | type => 'string', | |
581 | }, | |
582 | }, | |
583 | }, | |
584 | code => sub { | |
585 | my ($param) = @_; | |
586 | ||
587 | my $config = PVE::INotify::read_file('interfaces'); | |
3ed15e6c | 588 | my $ifaces = $config->{ifaces}; |
aff192e6 DM |
589 | |
590 | raise_param_exc({ iface => "interface does not exist" }) | |
3ed15e6c | 591 | if !$ifaces->{$param->{iface}}; |
aff192e6 | 592 | |
3ed15e6c | 593 | return $ifaces->{$param->{iface}}; |
aff192e6 DM |
594 | }}); |
595 | ||
a6ed0aa6 TL |
596 | sub ifupdown2_version { |
597 | my $v; | |
598 | PVE::Tools::run_command(['ifreload', '-V'], outfunc => sub { $v //= shift }); | |
599 | return if !defined($v) || $v !~ /^\s*ifupdown2:(\S+)\s*$/; | |
600 | $v = $1; | |
601 | my ($major, $minor, $extra, $pve) = split(/\.|-/, $v); | |
602 | my $is_pve = defined($pve) && $pve =~ /pve/; | |
603 | ||
604 | return ($major * 100000 + $minor * 1000 + $extra * 10, $is_pve, $v); | |
605 | } | |
606 | sub assert_ifupdown2_installed { | |
607 | die "you need ifupdown2 to reload network configuration\n" if ! -e '/usr/share/ifupdown2'; | |
608 | my ($v, $pve, $v_str) = ifupdown2_version(); | |
609 | die "incompatible 'ifupdown2' package version '$v_str'! Did you installed from Proxmox repositories?\n" | |
610 | if $v < (1*100000 + 2*1000 + 8*10) || !$pve; | |
611 | } | |
612 | ||
cacd7547 AD |
613 | __PACKAGE__->register_method({ |
614 | name => 'reload_network_config', | |
615 | path => '', | |
616 | method => 'PUT', | |
617 | permissions => { | |
618 | check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]], | |
619 | }, | |
620 | description => "Reload network configuration", | |
621 | protected => 1, | |
622 | proxyto => 'node', | |
623 | parameters => { | |
624 | additionalProperties => 0, | |
625 | properties => { | |
626 | node => get_standard_option('pve-node'), | |
627 | }, | |
628 | }, | |
629 | returns => { type => 'string' }, | |
630 | code => sub { | |
631 | ||
632 | my ($param) = @_; | |
633 | ||
634 | my $rpcenv = PVE::RPCEnvironment::get(); | |
635 | ||
636 | my $authuser = $rpcenv->get_user(); | |
637 | ||
638 | my $current_config_file = "/etc/network/interfaces"; | |
639 | my $new_config_file = "/etc/network/interfaces.new"; | |
640 | ||
a6ed0aa6 | 641 | assert_ifupdown2_installed(); |
6159470e | 642 | |
cacd7547 AD |
643 | my $worker = sub { |
644 | ||
e46bf624 | 645 | rename($new_config_file, $current_config_file) if -e $new_config_file; |
cacd7547 | 646 | |
2bca9b77 | 647 | if ($have_sdn) { |
9ad4656d | 648 | PVE::Network::SDN::generate_zone_config(); |
2bca9b77 AD |
649 | } |
650 | ||
cacd7547 AD |
651 | my $err = sub { |
652 | my $line = shift; | |
653 | if ($line =~ /(warning|error): (\S+):/) { | |
e46bf624 | 654 | print "$2 : $line \n"; |
cacd7547 AD |
655 | } |
656 | }; | |
084e6030 | 657 | PVE::Tools::run_command(['ifreload', '-a'], errfunc => $err); |
cacd7547 | 658 | |
20dc8bbe | 659 | if ($have_sdn) { |
9ad4656d | 660 | PVE::Network::SDN::generate_controller_config(1); |
084e6030 | 661 | } |
cacd7547 AD |
662 | }; |
663 | return $rpcenv->fork_worker('srvreload', 'networking', $authuser, $worker); | |
664 | }}); | |
665 | ||
aff192e6 DM |
666 | __PACKAGE__->register_method({ |
667 | name => 'delete_network', | |
668 | path => '{iface}', | |
669 | method => 'DELETE', | |
670 | permissions => { | |
7d020b42 | 671 | check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]], |
aff192e6 DM |
672 | }, |
673 | description => "Delete network device configuration", | |
674 | protected => 1, | |
675 | proxyto => 'node', | |
676 | parameters => { | |
677 | additionalProperties => 0, | |
678 | properties => { | |
679 | node => get_standard_option('pve-node'), | |
680 | iface => get_standard_option('pve-iface'), | |
681 | }, | |
682 | }, | |
683 | returns => { type => 'null' }, | |
684 | code => sub { | |
685 | my ($param) = @_; | |
686 | ||
687 | my $code = sub { | |
688 | my $config = PVE::INotify::read_file('interfaces'); | |
3ed15e6c | 689 | my $ifaces = $config->{ifaces}; |
aff192e6 DM |
690 | |
691 | raise_param_exc({ iface => "interface does not exist" }) | |
3ed15e6c | 692 | if !$ifaces->{$param->{iface}}; |
aff192e6 | 693 | |
3ed15e6c | 694 | my $d = $ifaces->{$param->{iface}}; |
d11733f8 DM |
695 | if ($d->{type} eq 'OVSIntPort' || $d->{type} eq 'OVSBond') { |
696 | if (my $brname = $d->{ovs_bridge}) { | |
3ed15e6c | 697 | if (my $br = $ifaces->{$brname}) { |
d11733f8 DM |
698 | if ($br->{ovs_ports}) { |
699 | my @ports = split (/\s+/, $br->{ovs_ports}); | |
700 | my @new = grep { $_ ne $param->{iface} } @ports; | |
701 | $br->{ovs_ports} = join(' ', @new); | |
702 | } | |
703 | } | |
704 | } | |
705 | } | |
706 | ||
3ed15e6c | 707 | delete $ifaces->{$param->{iface}}; |
aff192e6 DM |
708 | |
709 | PVE::INotify::write_file('interfaces', $config); | |
710 | }; | |
711 | ||
712 | PVE::Tools::lock_file($iflockfn, 10, $code); | |
713 | die $@ if $@; | |
714 | ||
715 | return undef; | |
716 | }}); |