]>
git.proxmox.com Git - pve-manager.git/blob - PVE/API2/Network.pm
1 package PVE
::API2
::Network
;
7 use PVE
::Tools
qw(extract_param dir_glob_regex);
10 use PVE
::Exception
qw(raise_param_exc);
12 use PVE
::RPCEnvironment
;
13 use PVE
::JSONSchema
qw(get_standard_option);
14 use PVE
::AccessControl
;
17 use base
qw(PVE::RESTHandler);
19 my $iflockfn = "/etc/network/.pve-interfaces.lock";
21 my $bond_mode_enum = [
23 'active-backup', # OVS and Linux
30 'lacp-balance-slb', # OVS
31 'lacp-balance-tcp', # OVS
34 my $network_type_enum = ['bridge', 'bond', 'eth', 'alias', 'vlan',
35 'OVSBridge', 'OVSBond', 'OVSPort', 'OVSIntPort'];
39 description
=> "Network interface type",
41 enum
=> [@$network_type_enum, 'unknown'],
44 description
=> "Comments",
49 description
=> "Comments",
54 description
=> "Automatically start interface on boot.",
58 bridge_vlan_aware
=> {
59 description
=> "Enable bridge vlan support.",
64 description
=> "Specify the interfaces you want to add to your bridge.",
66 type
=> 'string', format
=> 'pve-iface-list',
69 description
=> "Specify the interfaces you want to add to your bridge.",
71 type
=> 'string', format
=> 'pve-iface-list',
74 description
=> "Specify a VLan tag (used by OVSPort, OVSIntPort, OVSBond)",
81 description
=> "OVS interface options.",
87 description
=> "The OVS bridge associated with a OVS port. This is required when you create an OVS port.",
89 type
=> 'string', format
=> 'pve-iface',
92 description
=> "Specify the interfaces used by the bonding device.",
94 type
=> 'string', format
=> 'pve-iface-list',
97 description
=> "Specify the interfaces used by the bonding device.",
99 type
=> 'string', format
=> 'pve-iface-list',
102 description
=> "Bonding mode.",
104 type
=> 'string', enum
=> $bond_mode_enum,
106 bond_xmit_hash_policy
=> {
107 description
=> "Selects the transmit hash policy to use for slave selection in balance-xor and 802.3ad modes.",
110 enum
=> ['layer2', 'layer2+3', 'layer3+4' ],
113 description
=> 'Default gateway address.',
114 type
=> 'string', format
=> 'ipv4',
118 description
=> 'Network mask.',
119 type
=> 'string', format
=> 'ipv4mask',
121 requires
=> 'address',
124 description
=> 'IP address.',
125 type
=> 'string', format
=> 'ipv4',
127 requires
=> 'netmask',
130 description
=> 'IPv4 CIDR.',
131 type
=> 'string', format
=> 'CIDRv4',
135 description
=> 'Default ipv6 gateway address.',
136 type
=> 'string', format
=> 'ipv6',
140 description
=> 'Network mask.',
141 type
=> 'integer', minimum
=> 0, maximum
=> 128,
143 requires
=> 'address6',
146 description
=> 'IP address.',
147 type
=> 'string', format
=> 'ipv6',
149 requires
=> 'netmask6',
152 description
=> 'IPv6 CIDR.',
153 type
=> 'string', format
=> 'CIDRv6',
158 sub json_config_properties
{
161 foreach my $opt (keys %$confdesc) {
162 $prop->{$opt} = $confdesc->{$opt};
168 __PACKAGE__-
>register_method({
172 permissions
=> { user
=> 'all' },
173 description
=> "List available networks",
176 additionalProperties
=> 0,
178 node
=> get_standard_option
('pve-node'),
180 description
=> "Only list specific interface types.",
182 enum
=> [ @$network_type_enum, 'any_bridge' ],
193 links
=> [ { rel
=> 'child', href
=> "{iface}" } ],
198 my $rpcenv = PVE
::RPCEnvironment
::get
();
200 my $tmp = PVE
::INotify
::read_file
('interfaces', 1);
201 my $config = $tmp->{data
};
202 my $changes = $tmp->{changes
};
204 $rpcenv->set_result_attrib('changes', $changes) if $changes;
206 my $ifaces = $config->{ifaces
};
208 delete $ifaces->{lo
}; # do not list the loopback device
210 if ($param->{type
}) {
211 foreach my $k (keys %$ifaces) {
212 my $type = $ifaces->{$k}->{type
};
213 my $match = ($param->{type
} eq $type) || (
214 ($param->{type
} eq 'any_bridge') &&
215 ($type eq 'bridge' || $type eq 'OVSBridge'));
216 delete $ifaces->{$k} if !$match;
220 return PVE
::RESTHandler
::hash_to_array
($ifaces, 'iface');
223 __PACKAGE__-
>register_method({
224 name
=> 'revert_network_changes',
228 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
231 description
=> "Revert network configuration changes.",
234 additionalProperties
=> 0,
236 node
=> get_standard_option
('pve-node'),
239 returns
=> { type
=> "null" },
243 unlink "/etc/network/interfaces.new";
248 my $check_duplicate = sub {
249 my ($config, $newiface, $key, $name) = @_;
251 foreach my $iface (keys %$config) {
252 raise_param_exc
({ $key => "$name already exists on interface '$iface'." })
253 if ($newiface ne $iface) && $config->{$iface}->{$key};
257 my $check_duplicate_gateway = sub {
258 my ($config, $newiface) = @_;
259 return &$check_duplicate($config, $newiface, 'gateway', 'Default gateway');
262 my $check_duplicate_gateway6 = sub {
263 my ($config, $newiface) = @_;
264 return &$check_duplicate($config, $newiface, 'gateway6', 'Default ipv6 gateway');
268 return Net
::IP
::ip_iptobin
(Net
::IP
::ip_expand_address
(shift, 6), 6);
271 my $check_ipv6_settings = sub {
272 my ($address, $netmask) = @_;
274 raise_param_exc
({ netmask
=> "$netmask is not a valid subnet length for ipv6" })
275 if $netmask < 0 || $netmask > 128;
277 raise_param_exc
({ address
=> "$address is not a valid host IPv6 address." })
278 if !Net
::IP
::ip_is_ipv6
($address);
280 my $binip = ipv6_tobin
($address);
281 my $binmask = Net
::IP
::ip_get_mask
($netmask, 6);
283 my $type = ($binip eq $binmask) ?
'ANYCAST' : Net
::IP
::ip_iptypev6
($binip);
285 if (defined($type) && $type !~ /^(?:(?:GLOBAL|(?:UNIQUE|LINK)-LOCAL)-UNICAST)$/) {
286 raise_param_exc
({ address
=> "$address with type '$type', cannot be used as host IPv6 address." });
290 my $map_cidr_to_address_netmask = sub {
293 if ($param->{cidr
}) {
294 raise_param_exc
({ address
=> "address conflicts with cidr" })
295 if $param->{address
};
296 raise_param_exc
({ netmask
=> "netmask conflicts with cidr" })
297 if $param->{netmask
};
299 my ($address, $netmask) = $param->{cidr
} =~ m!^(.*)/(\d+)$!;
300 $param->{address
} = $address;
301 $param->{netmask
} = $netmask;
302 delete $param->{cidr
};
305 if ($param->{cidr6
}) {
306 raise_param_exc
({ address6
=> "address6 conflicts with cidr6" })
307 if $param->{address6
};
308 raise_param_exc
({ netmask6
=> "netmask6 conflicts with cidr6" })
309 if $param->{netmask6
};
311 my ($address, $netmask) = $param->{cidr6
} =~ m!^(.*)/(\d+)$!;
312 $param->{address6
} = $address;
313 $param->{netmask6
} = $netmask;
314 delete $param->{cidr6
};
318 __PACKAGE__-
>register_method({
319 name
=> 'create_network',
323 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
325 description
=> "Create network device configuration",
329 additionalProperties
=> 0,
330 properties
=> json_config_properties
({
331 node
=> get_standard_option
('pve-node'),
332 iface
=> get_standard_option
('pve-iface')}),
334 returns
=> { type
=> 'null' },
338 my $node = extract_param
($param, 'node');
339 my $iface = extract_param
($param, 'iface');
342 my $config = PVE
::INotify
::read_file
('interfaces');
343 my $ifaces = $config->{ifaces
};
345 raise_param_exc
({ iface
=> "interface already exists" })
346 if $ifaces->{$iface};
348 &$check_duplicate_gateway($ifaces, $iface)
349 if $param->{gateway
};
350 &$check_duplicate_gateway6($ifaces, $iface)
351 if $param->{gateway6
};
353 $map_cidr_to_address_netmask->($param);
355 &$check_ipv6_settings($param->{address6
}, int($param->{netmask6
}))
356 if $param->{address6
};
358 my $families = $param->{families
} = [];
359 push @$families, 'inet'
360 if $param->{address
} && !grep(/^inet$/, @$families);
361 push @$families, 'inet6'
362 if $param->{address6
} && !grep(/^inet6$/, @$families);
363 @$families = ('inet') if !scalar(@$families);
365 $param->{method} = $param->{address
} ?
'static' : 'manual';
366 $param->{method6
} = $param->{address6
} ?
'static' : 'manual';
368 if ($param->{type
} =~ m/^OVS/) {
369 -x
'/usr/bin/ovs-vsctl' ||
370 die "Open VSwitch is not installed (need package 'openvswitch-switch')\n";
373 if ($param->{type
} eq 'OVSIntPort' || $param->{type
} eq 'OVSBond') {
374 my $brname = $param->{ovs_bridge
};
375 raise_param_exc
({ ovs_bridge
=> "parameter is required" }) if !$brname;
376 my $br = $ifaces->{$brname};
377 raise_param_exc
({ ovs_bridge
=> "bridge '$brname' does not exist" }) if !$br;
378 raise_param_exc
({ ovs_bridge
=> "interface '$brname' is no OVS bridge" })
379 if $br->{type
} ne 'OVSBridge';
381 my @ports = split (/\s+/, $br->{ovs_ports
} || '');
382 $br->{ovs_ports
} = join(' ', @ports, $iface)
383 if ! grep { $_ eq $iface } @ports;
386 $ifaces->{$iface} = $param;
388 PVE
::INotify
::write_file
('interfaces', $config);
391 PVE
::Tools
::lock_file
($iflockfn, 10, $code);
397 __PACKAGE__-
>register_method({
398 name
=> 'update_network',
402 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
404 description
=> "Update network device configuration",
408 additionalProperties
=> 0,
409 properties
=> json_config_properties
({
410 node
=> get_standard_option
('pve-node'),
411 iface
=> get_standard_option
('pve-iface'),
413 type
=> 'string', format
=> 'pve-configid-list',
414 description
=> "A list of settings you want to delete.",
418 returns
=> { type
=> 'null' },
422 my $node = extract_param
($param, 'node');
423 my $iface = extract_param
($param, 'iface');
424 my $delete = extract_param
($param, 'delete');
427 my $config = PVE
::INotify
::read_file
('interfaces');
428 my $ifaces = $config->{ifaces
};
430 raise_param_exc
({ iface
=> "interface does not exist" })
431 if !$ifaces->{$iface};
433 my $families = ($param->{families
} ||= []);
434 foreach my $k (PVE
::Tools
::split_list
($delete)) {
435 delete $ifaces->{$iface}->{$k};
436 @$families = grep(!/^inet$/, @$families) if $k eq 'address';
437 @$families = grep(!/^inet6$/, @$families) if $k eq 'address6';
440 $map_cidr_to_address_netmask->($param);
442 &$check_duplicate_gateway($ifaces, $iface)
443 if $param->{gateway
};
444 &$check_duplicate_gateway6($ifaces, $iface)
445 if $param->{gateway6
};
447 if ($param->{address
}) {
448 push @$families, 'inet' if !grep(/^inet$/, @$families);
450 @$families = grep(!/^inet$/, @$families);
452 if ($param->{address6
}) {
453 &$check_ipv6_settings($param->{address6
}, int($param->{netmask6
}));
454 push @$families, 'inet6' if !grep(/^inet6$/, @$families);
456 @$families = grep(!/^inet6$/, @$families);
458 @$families = ('inet') if !scalar(@$families);
460 $param->{method} = $param->{address
} ?
'static' : 'manual';
461 $param->{method6
} = $param->{address6
} ?
'static' : 'manual';
463 foreach my $k (keys %$param) {
464 $ifaces->{$iface}->{$k} = $param->{$k};
467 PVE
::INotify
::write_file
('interfaces', $config);
470 PVE
::Tools
::lock_file
($iflockfn, 10, $code);
476 __PACKAGE__-
>register_method({
477 name
=> 'network_config',
481 check
=> ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
483 description
=> "Read network device configuration",
486 additionalProperties
=> 0,
488 node
=> get_standard_option
('pve-node'),
489 iface
=> get_standard_option
('pve-iface'),
506 my $config = PVE
::INotify
::read_file
('interfaces');
507 my $ifaces = $config->{ifaces
};
509 raise_param_exc
({ iface
=> "interface does not exist" })
510 if !$ifaces->{$param->{iface
}};
512 return $ifaces->{$param->{iface
}};
515 __PACKAGE__-
>register_method({
516 name
=> 'reload_network_config',
520 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
522 description
=> "Reload network configuration",
526 additionalProperties
=> 0,
528 node
=> get_standard_option
('pve-node'),
531 returns
=> { type
=> 'string' },
536 my $rpcenv = PVE
::RPCEnvironment
::get
();
538 my $authuser = $rpcenv->get_user();
540 my $current_config_file = "/etc/network/interfaces";
541 my $new_config_file = "/etc/network/interfaces.new";
543 die "you need ifupdown2 to reload networking\n" if !-e
'/usr/share/ifupdown2';
544 die "ifupdown2 reload is not compatible if openvswitch currently" if -x
'/usr/bin/ovs-vsctl';
548 rename($new_config_file, $current_config_file) if -e
$new_config_file;
550 my $cmd = ['ifreload', '-a'];
554 if ($line =~ /(warning|error): (\S+):/) {
555 print "$2 : $line \n";
559 PVE
::Tools
::run_command
($cmd,errfunc => $err);
561 return $rpcenv->fork_worker('srvreload', 'networking', $authuser, $worker);
564 __PACKAGE__-
>register_method({
565 name
=> 'delete_network',
569 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
571 description
=> "Delete network device configuration",
575 additionalProperties
=> 0,
577 node
=> get_standard_option
('pve-node'),
578 iface
=> get_standard_option
('pve-iface'),
581 returns
=> { type
=> 'null' },
586 my $config = PVE
::INotify
::read_file
('interfaces');
587 my $ifaces = $config->{ifaces
};
589 raise_param_exc
({ iface
=> "interface does not exist" })
590 if !$ifaces->{$param->{iface
}};
592 my $d = $ifaces->{$param->{iface
}};
593 if ($d->{type
} eq 'OVSIntPort' || $d->{type
} eq 'OVSBond') {
594 if (my $brname = $d->{ovs_bridge
}) {
595 if (my $br = $ifaces->{$brname}) {
596 if ($br->{ovs_ports
}) {
597 my @ports = split (/\s+/, $br->{ovs_ports
});
598 my @new = grep { $_ ne $param->{iface
} } @ports;
599 $br->{ovs_ports
} = join(' ', @new);
605 delete $ifaces->{$param->{iface
}};
607 PVE
::INotify
::write_file
('interfaces', $config);
610 PVE
::Tools
::lock_file
($iflockfn, 10, $code);