]>
git.proxmox.com Git - pmg-api.git/blob - src/PMG/API2/Network.pm
1 package PMG
::API2
::Network
;
7 use PVE
::Tools
qw(extract_param);
10 use PVE
::Exception
qw(raise_param_exc);
12 use PMG
::RESTEnvironment
;
13 use PVE
::JSONSchema
qw(get_standard_option);
15 use base
qw(PVE::RESTHandler);
17 my $iflockfn = "/etc/network/.pve-interfaces.lock";
19 my $bond_mode_enum = [
21 'active-backup', # OVS and Linux
28 'lacp-balance-slb', # OVS
29 'lacp-balance-tcp', # OVS
32 my $network_type_enum = ['bridge', 'bond', 'eth', 'alias', 'vlan',
33 'OVSBridge', 'OVSBond', 'OVSPort', 'OVSIntPort'];
37 description
=> "Network interface type",
39 enum
=> [@$network_type_enum, 'unknown'],
42 description
=> "Comments",
47 description
=> "Comments",
52 description
=> "Automatically start interface on boot.",
56 bridge_vlan_aware
=> {
57 description
=> "Enable bridge vlan support.",
62 description
=> "Specify the iterfaces you want to add to your bridge.",
64 type
=> 'string', format
=> 'pve-iface-list',
67 description
=> "Specify the iterfaces you want to add to your bridge.",
69 type
=> 'string', format
=> 'pve-iface-list',
72 description
=> "Specify a VLan tag (used by OVSPort, OVSIntPort, OVSBond)",
79 description
=> "OVS interface options.",
85 description
=> "The OVS bridge associated with a OVS port. This is required when you create an OVS port.",
87 type
=> 'string', format
=> 'pve-iface',
90 description
=> "Specify the interfaces used by the bonding device.",
92 type
=> 'string', format
=> 'pve-iface-list',
95 description
=> "Specify the interfaces used by the bonding device.",
97 type
=> 'string', format
=> 'pve-iface-list',
100 description
=> "Bonding mode.",
102 type
=> 'string', enum
=> $bond_mode_enum,
105 description
=> "Specify the primary interface for active-backup bond.",
107 type
=> 'string', format
=> 'pve-iface',
109 bond_xmit_hash_policy
=> {
110 description
=> "Selects the transmit hash policy to use for slave selection in balance-xor and 802.3ad modes.",
113 enum
=> ['layer2', 'layer2+3', 'layer3+4' ],
116 description
=> 'Default gateway address.',
117 type
=> 'string', format
=> 'ipv4',
121 description
=> 'Network mask.',
122 type
=> 'string', format
=> 'ipv4mask',
124 requires
=> 'address',
127 description
=> 'IP address.',
128 type
=> 'string', format
=> 'ipv4',
130 requires
=> 'netmask',
133 description
=> 'IPv4 CIDR.',
134 type
=> 'string', format
=> 'CIDRv4',
138 description
=> 'Default ipv6 gateway address.',
139 type
=> 'string', format
=> 'ipv6',
143 description
=> 'Network mask.',
144 type
=> 'integer', minimum
=> 0, maximum
=> 128,
146 requires
=> 'address6',
149 description
=> 'IP address.',
150 type
=> 'string', format
=> 'ipv6',
152 requires
=> 'netmask6',
155 description
=> 'IPv6 CIDR.',
156 type
=> 'string', format
=> 'CIDRv6',
161 sub json_config_properties
{
164 foreach my $opt (keys %$confdesc) {
165 $prop->{$opt} = $confdesc->{$opt};
171 __PACKAGE__-
>register_method({
175 description
=> "List available networks",
177 permissions
=> { check
=> [ 'admin', 'audit' ] },
179 additionalProperties
=> 0,
181 node
=> get_standard_option
('pve-node'),
183 description
=> "Only list specific interface types.",
185 enum
=> [ @$network_type_enum, 'any_bridge' ],
196 links
=> [ { rel
=> 'child', href
=> "{iface}" } ],
201 my $restenv = PMG
::RESTEnvironment-
>get();
203 my $tmp = PVE
::INotify
::read_file
('interfaces', 1);
204 my $config = $tmp->{data
};
205 my $changes = $tmp->{changes
};
207 $restenv->set_result_attrib('changes', $changes) if $changes;
209 my $ifaces = $config->{ifaces
};
211 delete $ifaces->{lo
}; # do not list the loopback device
213 if ($param->{type
}) {
214 foreach my $k (keys %$ifaces) {
215 my $type = $ifaces->{$k}->{type
};
216 my $match = ($param->{type
} eq $type) || (
217 ($param->{type
} eq 'any_bridge') &&
218 ($type eq 'bridge' || $type eq 'OVSBridge'));
219 delete $ifaces->{$k} if !$match;
223 return PVE
::RESTHandler
::hash_to_array
($ifaces, 'iface');
226 __PACKAGE__-
>register_method({
227 name
=> 'revert_network_changes',
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 ip 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 = Net
::IP
::ip_iptypev6
($binip);
285 raise_param_exc
({ address
=> "$address is not a valid host ip address." })
286 if ($binip eq $binmask) ||
287 (defined($type) && $type !~ /^(?:(?:GLOBAL|(?:UNIQUE|LINK)-LOCAL)-UNICAST)$/);
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',
322 description
=> "Create network device configuration",
326 additionalProperties
=> 0,
327 properties
=> json_config_properties
({
328 node
=> get_standard_option
('pve-node'),
329 iface
=> get_standard_option
('pve-iface')}),
331 returns
=> { type
=> 'null' },
335 my $node = extract_param
($param, 'node');
336 my $iface = extract_param
($param, 'iface');
339 my $config = PVE
::INotify
::read_file
('interfaces');
340 my $ifaces = $config->{ifaces
};
342 raise_param_exc
({ iface
=> "interface already exists" })
343 if $ifaces->{$iface};
345 &$check_duplicate_gateway($ifaces, $iface)
346 if $param->{gateway
};
347 &$check_duplicate_gateway6($ifaces, $iface)
348 if $param->{gateway6
};
350 $map_cidr_to_address_netmask->($param);
352 &$check_ipv6_settings($param->{address6
}, int($param->{netmask6
}))
353 if $param->{address6
};
355 my $families = $param->{families
} = [];
356 push @$families, 'inet'
357 if $param->{address
} && !grep(/^inet$/, @$families);
358 push @$families, 'inet6'
359 if $param->{address6
} && !grep(/^inet6$/, @$families);
360 @$families = ('inet') if !scalar(@$families);
362 $param->{method} = $param->{address
} ?
'static' : 'manual';
363 $param->{method6
} = $param->{address6
} ?
'static' : 'manual';
365 if ($param->{type
} =~ m/^OVS/) {
366 -x
'/usr/bin/ovs-vsctl' ||
367 die "Open VSwitch is not installed (need package 'openvswitch-switch')\n";
370 if ($param->{type
} eq 'OVSIntPort' || $param->{type
} eq 'OVSBond') {
371 my $brname = $param->{ovs_bridge
};
372 raise_param_exc
({ ovs_bridge
=> "parameter is required" }) if !$brname;
373 my $br = $ifaces->{$brname};
374 raise_param_exc
({ ovs_bridge
=> "bridge '$brname' does not exist" }) if !$br;
375 raise_param_exc
({ ovs_bridge
=> "interface '$brname' is no OVS bridge" })
376 if $br->{type
} ne 'OVSBridge';
378 my @ports = split (/\s+/, $br->{ovs_ports
} || '');
379 $br->{ovs_ports
} = join(' ', @ports, $iface)
380 if ! grep { $_ eq $iface } @ports;
383 $ifaces->{$iface} = $param;
385 PVE
::INotify
::write_file
('interfaces', $config);
388 PVE
::Tools
::lock_file
($iflockfn, 10, $code);
394 __PACKAGE__-
>register_method({
395 name
=> 'update_network',
398 description
=> "Update network device configuration",
402 additionalProperties
=> 0,
403 properties
=> json_config_properties
({
404 node
=> get_standard_option
('pve-node'),
405 iface
=> get_standard_option
('pve-iface'),
407 type
=> 'string', format
=> 'pve-configid-list',
408 description
=> "A list of settings you want to delete.",
412 returns
=> { type
=> 'null' },
416 my $node = extract_param
($param, 'node');
417 my $iface = extract_param
($param, 'iface');
418 my $delete = extract_param
($param, 'delete');
421 my $config = PVE
::INotify
::read_file
('interfaces');
422 my $ifaces = $config->{ifaces
};
424 raise_param_exc
({ iface
=> "interface does not exist" })
425 if !$ifaces->{$iface};
427 my $families = ($param->{families
} ||= []);
428 foreach my $k (PVE
::Tools
::split_list
($delete)) {
429 delete $ifaces->{$iface}->{$k};
430 @$families = grep(!/^inet$/, @$families) if $k eq 'address';
431 @$families = grep(!/^inet6$/, @$families) if $k eq 'address6';
434 $map_cidr_to_address_netmask->($param);
436 &$check_duplicate_gateway($ifaces, $iface)
437 if $param->{gateway
};
438 &$check_duplicate_gateway6($ifaces, $iface)
439 if $param->{gateway6
};
441 if ($param->{address
}) {
442 push @$families, 'inet' if !grep(/^inet$/, @$families);
444 @$families = grep(!/^inet$/, @$families);
446 if ($param->{address6
}) {
447 &$check_ipv6_settings($param->{address6
}, int($param->{netmask6
}));
448 push @$families, 'inet6' if !grep(/^inet6$/, @$families);
450 @$families = grep(!/^inet6$/, @$families);
452 @$families = ('inet') if !scalar(@$families);
454 $param->{method} = $param->{address
} ?
'static' : 'manual';
455 $param->{method6
} = $param->{address6
} ?
'static' : 'manual';
457 foreach my $k (keys %$param) {
458 $ifaces->{$iface}->{$k} = $param->{$k};
461 PVE
::INotify
::write_file
('interfaces', $config);
464 PVE
::Tools
::lock_file
($iflockfn, 10, $code);
470 __PACKAGE__-
>register_method({
471 name
=> 'network_config',
474 description
=> "Read network device configuration",
476 permissions
=> { check
=> [ 'admin', 'audit' ] },
478 additionalProperties
=> 0,
480 node
=> get_standard_option
('pve-node'),
481 iface
=> get_standard_option
('pve-iface'),
498 my $config = PVE
::INotify
::read_file
('interfaces');
499 my $ifaces = $config->{ifaces
};
501 raise_param_exc
({ iface
=> "interface does not exist" })
502 if !$ifaces->{$param->{iface
}};
504 return $ifaces->{$param->{iface
}};
507 __PACKAGE__-
>register_method({
508 name
=> 'delete_network',
511 description
=> "Delete network device configuration",
515 additionalProperties
=> 0,
517 node
=> get_standard_option
('pve-node'),
518 iface
=> get_standard_option
('pve-iface'),
521 returns
=> { type
=> 'null' },
526 my $config = PVE
::INotify
::read_file
('interfaces');
527 my $ifaces = $config->{ifaces
};
529 raise_param_exc
({ iface
=> "interface does not exist" })
530 if !$ifaces->{$param->{iface
}};
532 my $d = $ifaces->{$param->{iface
}};
533 if ($d->{type
} eq 'OVSIntPort' || $d->{type
} eq 'OVSBond') {
534 if (my $brname = $d->{ovs_bridge
}) {
535 if (my $br = $ifaces->{$brname}) {
536 if ($br->{ovs_ports
}) {
537 my @ports = split (/\s+/, $br->{ovs_ports
});
538 my @new = grep { $_ ne $param->{iface
} } @ports;
539 $br->{ovs_ports
} = join(' ', @new);
545 delete $ifaces->{$param->{iface
}};
547 PVE
::INotify
::write_file
('interfaces', $config);
550 PVE
::Tools
::lock_file
($iflockfn, 10, $code);