]> git.proxmox.com Git - pve-manager.git/blob - PVE/API2/Network.pm
92256863ece5db230f620d6bcf4d99d8cd0a52dc
[pve-manager.git] / PVE / API2 / Network.pm
1 package PVE::API2::Network;
2
3 use strict;
4 use warnings;
5
6 use Net::IP qw(:PROC);
7 use PVE::Tools qw(extract_param);
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
19 my $iflockfn = "/etc/network/.pve-interfaces.lock";
20
21 my $bond_mode_enum = [
22 'balance-rr',
23 'active-backup', # OVS and Linux
24 'balance-xor',
25 'broadcast',
26 '802.3ad',
27 'balance-tlb',
28 'balance-alb',
29 'balance-slb', # OVS
30 'lacp-balance-slb', # OVS
31 'lacp-balance-tcp', # OVS
32 ];
33
34 my $network_type_enum = ['bridge', 'bond', 'eth', 'alias', 'vlan',
35 'OVSBridge', 'OVSBond', 'OVSPort', 'OVSIntPort'];
36
37 my $confdesc = {
38 type => {
39 description => "Network interface type",
40 type => 'string',
41 enum => [@$network_type_enum, 'unknown'],
42 },
43 comments => {
44 description => "Comments",
45 type => 'string',
46 optional => 1,
47 },
48 comments6 => {
49 description => "Comments",
50 type => 'string',
51 optional => 1,
52 },
53 autostart => {
54 description => "Automatically start interface on boot.",
55 type => 'boolean',
56 optional => 1,
57 },
58 bridge_vlan_aware => {
59 description => "Enable bridge vlan support.",
60 type => 'boolean',
61 optional => 1,
62 },
63 bridge_ports => {
64 description => "Specify the iterfaces you want to add to your bridge.",
65 optional => 1,
66 type => 'string', format => 'pve-iface-list',
67 },
68 ovs_ports => {
69 description => "Specify the iterfaces you want to add to your bridge.",
70 optional => 1,
71 type => 'string', format => 'pve-iface-list',
72 },
73 ovs_tag => {
74 description => "Specify a VLan tag (used by OVSPort, OVSIntPort, OVSBond)",
75 optional => 1,
76 type => 'integer',
77 minimum => 1,
78 maximum => 4094,
79 },
80 ovs_options => {
81 description => "OVS interface options.",
82 optional => 1,
83 type => 'string',
84 maxLength => 1024,
85 },
86 ovs_bridge => {
87 description => "The OVS bridge associated with a OVS port. This is required when you create an OVS port.",
88 optional => 1,
89 type => 'string', format => 'pve-iface',
90 },
91 slaves => {
92 description => "Specify the interfaces used by the bonding device.",
93 optional => 1,
94 type => 'string', format => 'pve-iface-list',
95 },
96 ovs_bonds => {
97 description => "Specify the interfaces used by the bonding device.",
98 optional => 1,
99 type => 'string', format => 'pve-iface-list',
100 },
101 bond_mode => {
102 description => "Bonding mode.",
103 optional => 1,
104 type => 'string', enum => $bond_mode_enum,
105 },
106 bond_xmit_hash_policy => {
107 description => "Selects the transmit hash policy to use for slave selection in balance-xor and 802.3ad modes.",
108 optional => 1,
109 type => 'string',
110 enum => ['layer2', 'layer2+3', 'layer3+4' ],
111 },
112 gateway => {
113 description => 'Default gateway address.',
114 type => 'string', format => 'ipv4',
115 optional => 1,
116 },
117 netmask => {
118 description => 'Network mask.',
119 type => 'string', format => 'ipv4mask',
120 optional => 1,
121 requires => 'address',
122 },
123 address => {
124 description => 'IP address.',
125 type => 'string', format => 'ipv4',
126 optional => 1,
127 requires => 'netmask',
128 },
129 gateway6 => {
130 description => 'Default ipv6 gateway address.',
131 type => 'string', format => 'ipv6',
132 optional => 1,
133 },
134 netmask6 => {
135 description => 'Network mask.',
136 type => 'integer', minimum => 0, maximum => 128,
137 optional => 1,
138 requires => 'address6',
139 },
140 address6 => {
141 description => 'IP address.',
142 type => 'string', format => 'ipv6',
143 optional => 1,
144 requires => 'netmask6',
145 }
146 };
147
148 sub json_config_properties {
149 my $prop = shift;
150
151 foreach my $opt (keys %$confdesc) {
152 $prop->{$opt} = $confdesc->{$opt};
153 }
154
155 return $prop;
156 }
157
158 __PACKAGE__->register_method({
159 name => 'index',
160 path => '',
161 method => 'GET',
162 permissions => { user => 'all' },
163 description => "List available networks",
164 proxyto => 'node',
165 parameters => {
166 additionalProperties => 0,
167 properties => {
168 node => get_standard_option('pve-node'),
169 type => {
170 description => "Only list specific interface types.",
171 type => 'string',
172 enum => [ @$network_type_enum, 'any_bridge' ],
173 optional => 1,
174 },
175 },
176 },
177 returns => {
178 type => "array",
179 items => {
180 type => "object",
181 properties => {},
182 },
183 links => [ { rel => 'child', href => "{iface}" } ],
184 },
185 code => sub {
186 my ($param) = @_;
187
188 my $rpcenv = PVE::RPCEnvironment::get();
189
190 my $tmp = PVE::INotify::read_file('interfaces', 1);
191 my $config = $tmp->{data};
192 my $changes = $tmp->{changes};
193
194 $rpcenv->set_result_attrib('changes', $changes) if $changes;
195
196 my $ifaces = $config->{ifaces};
197
198 delete $ifaces->{lo}; # do not list the loopback device
199
200 if ($param->{type}) {
201 foreach my $k (keys %$ifaces) {
202 my $type = $ifaces->{$k}->{type};
203 my $match = ($param->{type} eq $type) || (
204 ($param->{type} eq 'any_bridge') &&
205 ($type eq 'bridge' || $type eq 'OVSBridge'));
206 delete $ifaces->{$k} if !$match;
207 }
208 }
209
210 return PVE::RESTHandler::hash_to_array($ifaces, 'iface');
211 }});
212
213 __PACKAGE__->register_method({
214 name => 'revert_network_changes',
215 path => '',
216 method => 'DELETE',
217 permissions => {
218 check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
219 },
220 protected => 1,
221 description => "Revert network configuration changes.",
222 proxyto => 'node',
223 parameters => {
224 additionalProperties => 0,
225 properties => {
226 node => get_standard_option('pve-node'),
227 },
228 },
229 returns => { type => "null" },
230 code => sub {
231 my ($param) = @_;
232
233 unlink "/etc/network/interfaces.new";
234
235 return undef;
236 }});
237
238 my $check_duplicate = sub {
239 my ($config, $newiface, $key, $name) = @_;
240
241 foreach my $iface (keys %$config) {
242 raise_param_exc({ $key => "$name already exists on interface '$iface'." })
243 if ($newiface ne $iface) && $config->{$iface}->{$key};
244 }
245 };
246
247 my $check_duplicate_gateway = sub {
248 my ($config, $newiface) = @_;
249 return &$check_duplicate($config, $newiface, 'gateway', 'Default gateway');
250 };
251
252 my $check_duplicate_gateway6 = sub {
253 my ($config, $newiface) = @_;
254 return &$check_duplicate($config, $newiface, 'gateway6', 'Default ipv6 gateway');
255 };
256
257 sub ipv6_tobin {
258 return Net::IP::ip_iptobin(Net::IP::ip_expand_address(shift, 6), 6);
259 }
260
261 my $check_ipv6_settings = sub {
262 my ($address, $netmask) = @_;
263
264 raise_param_exc({ netmask => "$netmask is not a valid subnet length for ipv6" })
265 if $netmask < 0 || $netmask > 128;
266
267 raise_param_exc({ address => "$address is not a valid host ip address." })
268 if !Net::IP::ip_is_ipv6($address);
269
270 my $binip = ipv6_tobin($address);
271 my $binmask = Net::IP::ip_get_mask($netmask, 6);
272
273 my $type = Net::IP::ip_iptypev6($binip);
274
275 raise_param_exc({ address => "$address is not a valid host ip address." })
276 if ($binip eq $binmask) ||
277 (defined($type) && $type !~ /^(?:(?:GLOBAL|(?:UNIQUE|LINK)-LOCAL)-UNICAST)$/);
278 };
279
280 __PACKAGE__->register_method({
281 name => 'create_network',
282 path => '',
283 method => 'POST',
284 permissions => {
285 check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
286 },
287 description => "Create network device configuration",
288 protected => 1,
289 proxyto => 'node',
290 parameters => {
291 additionalProperties => 0,
292 properties => json_config_properties({
293 node => get_standard_option('pve-node'),
294 iface => get_standard_option('pve-iface')}),
295 },
296 returns => { type => 'null' },
297 code => sub {
298 my ($param) = @_;
299
300 my $node = extract_param($param, 'node');
301 my $iface = extract_param($param, 'iface');
302
303 my $code = sub {
304 my $config = PVE::INotify::read_file('interfaces');
305 my $ifaces = $config->{ifaces};
306
307 raise_param_exc({ iface => "interface already exists" })
308 if $ifaces->{$iface};
309
310 &$check_duplicate_gateway($ifaces, $iface)
311 if $param->{gateway};
312 &$check_duplicate_gateway6($ifaces, $iface)
313 if $param->{gateway6};
314
315 &$check_ipv6_settings($param->{address6}, int($param->{netmask6}))
316 if $param->{address6};
317
318 my $families = $param->{families} = [];
319 push @$families, 'inet'
320 if $param->{address} && !grep(/^inet$/, @$families);
321 push @$families, 'inet6'
322 if $param->{address6} && !grep(/^inet6$/, @$families);
323 @$families = ('inet') if !scalar(@$families);
324
325 $param->{method} = $param->{address} ? 'static' : 'manual';
326 $param->{method6} = $param->{address6} ? 'static' : 'manual';
327
328 if ($param->{type} =~ m/^OVS/) {
329 -x '/usr/bin/ovs-vsctl' ||
330 die "Open VSwitch is not installed (need package 'openvswitch-switch')\n";
331 }
332
333 if ($param->{type} eq 'OVSIntPort' || $param->{type} eq 'OVSBond') {
334 my $brname = $param->{ovs_bridge};
335 raise_param_exc({ ovs_bridge => "parameter is required" }) if !$brname;
336 my $br = $ifaces->{$brname};
337 raise_param_exc({ ovs_bridge => "bridge '$brname' does not exist" }) if !$br;
338 raise_param_exc({ ovs_bridge => "interface '$brname' is no OVS bridge" })
339 if $br->{type} ne 'OVSBridge';
340
341 my @ports = split (/\s+/, $br->{ovs_ports} || '');
342 $br->{ovs_ports} = join(' ', @ports, $iface)
343 if ! grep { $_ eq $iface } @ports;
344 }
345
346 $ifaces->{$iface} = $param;
347
348 PVE::INotify::write_file('interfaces', $config);
349 };
350
351 PVE::Tools::lock_file($iflockfn, 10, $code);
352 die $@ if $@;
353
354 return undef;
355 }});
356
357 __PACKAGE__->register_method({
358 name => 'update_network',
359 path => '{iface}',
360 method => 'PUT',
361 permissions => {
362 check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
363 },
364 description => "Update 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 delete => {
373 type => 'string', format => 'pve-configid-list',
374 description => "A list of settings you want to delete.",
375 optional => 1,
376 }}),
377 },
378 returns => { type => 'null' },
379 code => sub {
380 my ($param) = @_;
381
382 my $node = extract_param($param, 'node');
383 my $iface = extract_param($param, 'iface');
384 my $delete = extract_param($param, 'delete');
385
386 my $code = sub {
387 my $config = PVE::INotify::read_file('interfaces');
388 my $ifaces = $config->{ifaces};
389
390 raise_param_exc({ iface => "interface does not exist" })
391 if !$ifaces->{$iface};
392
393 my $families = ($param->{families} ||= []);
394 foreach my $k (PVE::Tools::split_list($delete)) {
395 delete $ifaces->{$iface}->{$k};
396 @$families = grep(!/^inet$/, @$families) if $k eq 'address';
397 @$families = grep(!/^inet6$/, @$families) if $k eq 'address6';
398 }
399
400 &$check_duplicate_gateway($ifaces, $iface)
401 if $param->{gateway};
402 &$check_duplicate_gateway6($ifaces, $iface)
403 if $param->{gateway6};
404
405 if ($param->{address}) {
406 push @$families, 'inet' if !grep(/^inet$/, @$families);
407 } else {
408 @$families = grep(!/^inet$/, @$families);
409 }
410 if ($param->{address6}) {
411 &$check_ipv6_settings($param->{address6}, int($param->{netmask6}));
412 push @$families, 'inet6' if !grep(/^inet6$/, @$families);
413 } else {
414 @$families = grep(!/^inet6$/, @$families);
415 }
416 @$families = ('inet') if !scalar(@$families);
417
418 $param->{method} = $param->{address} ? 'static' : 'manual';
419 $param->{method6} = $param->{address6} ? 'static' : 'manual';
420
421 foreach my $k (keys %$param) {
422 $ifaces->{$iface}->{$k} = $param->{$k};
423 }
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 => 'network_config',
436 path => '{iface}',
437 method => 'GET',
438 permissions => {
439 check => ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
440 },
441 description => "Read network device configuration",
442 proxyto => 'node',
443 parameters => {
444 additionalProperties => 0,
445 properties => {
446 node => get_standard_option('pve-node'),
447 iface => get_standard_option('pve-iface'),
448 },
449 },
450 returns => {
451 type => "object",
452 properties => {
453 type => {
454 type => 'string',
455 },
456 method => {
457 type => 'string',
458 },
459 },
460 },
461 code => sub {
462 my ($param) = @_;
463
464 my $config = PVE::INotify::read_file('interfaces');
465 my $ifaces = $config->{ifaces};
466
467 raise_param_exc({ iface => "interface does not exist" })
468 if !$ifaces->{$param->{iface}};
469
470 return $ifaces->{$param->{iface}};
471 }});
472
473 __PACKAGE__->register_method({
474 name => 'delete_network',
475 path => '{iface}',
476 method => 'DELETE',
477 permissions => {
478 check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
479 },
480 description => "Delete network device configuration",
481 protected => 1,
482 proxyto => 'node',
483 parameters => {
484 additionalProperties => 0,
485 properties => {
486 node => get_standard_option('pve-node'),
487 iface => get_standard_option('pve-iface'),
488 },
489 },
490 returns => { type => 'null' },
491 code => sub {
492 my ($param) = @_;
493
494 my $code = sub {
495 my $config = PVE::INotify::read_file('interfaces');
496 my $ifaces = $config->{ifaces};
497
498 raise_param_exc({ iface => "interface does not exist" })
499 if !$ifaces->{$param->{iface}};
500
501 my $d = $ifaces->{$param->{iface}};
502 if ($d->{type} eq 'OVSIntPort' || $d->{type} eq 'OVSBond') {
503 if (my $brname = $d->{ovs_bridge}) {
504 if (my $br = $ifaces->{$brname}) {
505 if ($br->{ovs_ports}) {
506 my @ports = split (/\s+/, $br->{ovs_ports});
507 my @new = grep { $_ ne $param->{iface} } @ports;
508 $br->{ovs_ports} = join(' ', @new);
509 }
510 }
511 }
512 }
513
514 delete $ifaces->{$param->{iface}};
515
516 PVE::INotify::write_file('interfaces', $config);
517 };
518
519 PVE::Tools::lock_file($iflockfn, 10, $code);
520 die $@ if $@;
521
522 return undef;
523 }});