]>
git.proxmox.com Git - pmg-api.git/blob - 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,
104 bond_xmit_hash_policy
=> {
105 description
=> "Selects the transmit hash policy to use for slave selection in balance-xor and 802.3ad modes.",
108 enum
=> ['layer2', 'layer2+3', 'layer3+4' ],
111 description
=> 'Default gateway address.',
112 type
=> 'string', format
=> 'ipv4',
116 description
=> 'Network mask.',
117 type
=> 'string', format
=> 'ipv4mask',
119 requires
=> 'address',
122 description
=> 'IP address.',
123 type
=> 'string', format
=> 'ipv4',
125 requires
=> 'netmask',
128 description
=> 'Default ipv6 gateway address.',
129 type
=> 'string', format
=> 'ipv6',
133 description
=> 'Network mask.',
134 type
=> 'integer', minimum
=> 0, maximum
=> 128,
136 requires
=> 'address6',
139 description
=> 'IP address.',
140 type
=> 'string', format
=> 'ipv6',
142 requires
=> 'netmask6',
146 sub json_config_properties
{
149 foreach my $opt (keys %$confdesc) {
150 $prop->{$opt} = $confdesc->{$opt};
156 __PACKAGE__-
>register_method({
160 description
=> "List available networks",
162 permissions
=> { check
=> [ 'admin', 'audit' ] },
164 additionalProperties
=> 0,
166 node
=> get_standard_option
('pve-node'),
168 description
=> "Only list specific interface types.",
170 enum
=> [ @$network_type_enum, 'any_bridge' ],
181 links
=> [ { rel
=> 'child', href
=> "{iface}" } ],
186 my $restenv = PMG
::RESTEnvironment-
>get();
188 my $tmp = PVE
::INotify
::read_file
('interfaces', 1);
189 my $config = $tmp->{data
};
190 my $changes = $tmp->{changes
};
192 $restenv->set_result_attrib('changes', $changes) if $changes;
194 my $ifaces = $config->{ifaces
};
196 delete $ifaces->{lo
}; # do not list the loopback device
198 if ($param->{type
}) {
199 foreach my $k (keys %$ifaces) {
200 my $type = $ifaces->{$k}->{type
};
201 my $match = ($param->{type
} eq $type) || (
202 ($param->{type
} eq 'any_bridge') &&
203 ($type eq 'bridge' || $type eq 'OVSBridge'));
204 delete $ifaces->{$k} if !$match;
208 return PVE
::RESTHandler
::hash_to_array
($ifaces, 'iface');
211 __PACKAGE__-
>register_method({
212 name
=> 'revert_network_changes',
216 description
=> "Revert network configuration changes.",
219 additionalProperties
=> 0,
221 node
=> get_standard_option
('pve-node'),
224 returns
=> { type
=> "null" },
228 unlink "/etc/network/interfaces.new";
233 my $check_duplicate = sub {
234 my ($config, $newiface, $key, $name) = @_;
236 foreach my $iface (keys %$config) {
237 raise_param_exc
({ $key => "$name already exists on interface '$iface'." })
238 if ($newiface ne $iface) && $config->{$iface}->{$key};
242 my $check_duplicate_gateway = sub {
243 my ($config, $newiface) = @_;
244 return &$check_duplicate($config, $newiface, 'gateway', 'Default gateway');
247 my $check_duplicate_gateway6 = sub {
248 my ($config, $newiface) = @_;
249 return &$check_duplicate($config, $newiface, 'gateway6', 'Default ipv6 gateway');
253 return Net
::IP
::ip_iptobin
(Net
::IP
::ip_expand_address
(shift, 6), 6);
256 my $check_ipv6_settings = sub {
257 my ($address, $netmask) = @_;
259 raise_param_exc
({ netmask
=> "$netmask is not a valid subnet length for ipv6" })
260 if $netmask < 0 || $netmask > 128;
262 raise_param_exc
({ address
=> "$address is not a valid host ip address." })
263 if !Net
::IP
::ip_is_ipv6
($address);
265 my $binip = ipv6_tobin
($address);
266 my $binmask = Net
::IP
::ip_get_mask
($netmask, 6);
268 my $type = Net
::IP
::ip_iptypev6
($binip);
270 raise_param_exc
({ address
=> "$address is not a valid host ip address." })
271 if ($binip eq $binmask) ||
272 (defined($type) && $type !~ /^(?:(?:GLOBAL|(?:UNIQUE|LINK)-LOCAL)-UNICAST)$/);
275 __PACKAGE__-
>register_method({
276 name
=> 'create_network',
279 description
=> "Create network device configuration",
283 additionalProperties
=> 0,
284 properties
=> json_config_properties
({
285 node
=> get_standard_option
('pve-node'),
286 iface
=> get_standard_option
('pve-iface')}),
288 returns
=> { type
=> 'null' },
292 my $node = extract_param
($param, 'node');
293 my $iface = extract_param
($param, 'iface');
296 my $config = PVE
::INotify
::read_file
('interfaces');
297 my $ifaces = $config->{ifaces
};
299 raise_param_exc
({ iface
=> "interface already exists" })
300 if $ifaces->{$iface};
302 &$check_duplicate_gateway($ifaces, $iface)
303 if $param->{gateway
};
304 &$check_duplicate_gateway6($ifaces, $iface)
305 if $param->{gateway6
};
307 &$check_ipv6_settings($param->{address6
}, int($param->{netmask6
}))
308 if $param->{address6
};
310 my $families = $param->{families
} = [];
311 push @$families, 'inet'
312 if $param->{address
} && !grep(/^inet$/, @$families);
313 push @$families, 'inet6'
314 if $param->{address6
} && !grep(/^inet6$/, @$families);
315 @$families = ('inet') if !scalar(@$families);
317 $param->{method} = $param->{address
} ?
'static' : 'manual';
318 $param->{method6
} = $param->{address6
} ?
'static' : 'manual';
320 if ($param->{type
} =~ m/^OVS/) {
321 -x
'/usr/bin/ovs-vsctl' ||
322 die "Open VSwitch is not installed (need package 'openvswitch-switch')\n";
325 if ($param->{type
} eq 'OVSIntPort' || $param->{type
} eq 'OVSBond') {
326 my $brname = $param->{ovs_bridge
};
327 raise_param_exc
({ ovs_bridge
=> "parameter is required" }) if !$brname;
328 my $br = $ifaces->{$brname};
329 raise_param_exc
({ ovs_bridge
=> "bridge '$brname' does not exist" }) if !$br;
330 raise_param_exc
({ ovs_bridge
=> "interface '$brname' is no OVS bridge" })
331 if $br->{type
} ne 'OVSBridge';
333 my @ports = split (/\s+/, $br->{ovs_ports
} || '');
334 $br->{ovs_ports
} = join(' ', @ports, $iface)
335 if ! grep { $_ eq $iface } @ports;
338 $ifaces->{$iface} = $param;
340 PVE
::INotify
::write_file
('interfaces', $config);
343 PVE
::Tools
::lock_file
($iflockfn, 10, $code);
349 __PACKAGE__-
>register_method({
350 name
=> 'update_network',
353 description
=> "Update network device configuration",
357 additionalProperties
=> 0,
358 properties
=> json_config_properties
({
359 node
=> get_standard_option
('pve-node'),
360 iface
=> get_standard_option
('pve-iface'),
362 type
=> 'string', format
=> 'pve-configid-list',
363 description
=> "A list of settings you want to delete.",
367 returns
=> { type
=> 'null' },
371 my $node = extract_param
($param, 'node');
372 my $iface = extract_param
($param, 'iface');
373 my $delete = extract_param
($param, 'delete');
376 my $config = PVE
::INotify
::read_file
('interfaces');
377 my $ifaces = $config->{ifaces
};
379 raise_param_exc
({ iface
=> "interface does not exist" })
380 if !$ifaces->{$iface};
382 my $families = ($param->{families
} ||= []);
383 foreach my $k (PVE
::Tools
::split_list
($delete)) {
384 delete $ifaces->{$iface}->{$k};
385 @$families = grep(!/^inet$/, @$families) if $k eq 'address';
386 @$families = grep(!/^inet6$/, @$families) if $k eq 'address6';
389 &$check_duplicate_gateway($ifaces, $iface)
390 if $param->{gateway
};
391 &$check_duplicate_gateway6($ifaces, $iface)
392 if $param->{gateway6
};
394 if ($param->{address
}) {
395 push @$families, 'inet' if !grep(/^inet$/, @$families);
397 @$families = grep(!/^inet$/, @$families);
399 if ($param->{address6
}) {
400 &$check_ipv6_settings($param->{address6
}, int($param->{netmask6
}));
401 push @$families, 'inet6' if !grep(/^inet6$/, @$families);
403 @$families = grep(!/^inet6$/, @$families);
405 @$families = ('inet') if !scalar(@$families);
407 $param->{method} = $param->{address
} ?
'static' : 'manual';
408 $param->{method6
} = $param->{address6
} ?
'static' : 'manual';
410 foreach my $k (keys %$param) {
411 $ifaces->{$iface}->{$k} = $param->{$k};
414 PVE
::INotify
::write_file
('interfaces', $config);
417 PVE
::Tools
::lock_file
($iflockfn, 10, $code);
423 __PACKAGE__-
>register_method({
424 name
=> 'network_config',
427 description
=> "Read network device configuration",
429 permissions
=> { check
=> [ 'admin', 'audit' ] },
431 additionalProperties
=> 0,
433 node
=> get_standard_option
('pve-node'),
434 iface
=> get_standard_option
('pve-iface'),
451 my $config = PVE
::INotify
::read_file
('interfaces');
452 my $ifaces = $config->{ifaces
};
454 raise_param_exc
({ iface
=> "interface does not exist" })
455 if !$ifaces->{$param->{iface
}};
457 return $ifaces->{$param->{iface
}};
460 __PACKAGE__-
>register_method({
461 name
=> 'delete_network',
464 description
=> "Delete network device configuration",
468 additionalProperties
=> 0,
470 node
=> get_standard_option
('pve-node'),
471 iface
=> get_standard_option
('pve-iface'),
474 returns
=> { type
=> 'null' },
479 my $config = PVE
::INotify
::read_file
('interfaces');
480 my $ifaces = $config->{ifaces
};
482 raise_param_exc
({ iface
=> "interface does not exist" })
483 if !$ifaces->{$param->{iface
}};
485 my $d = $ifaces->{$param->{iface
}};
486 if ($d->{type
} eq 'OVSIntPort' || $d->{type
} eq 'OVSBond') {
487 if (my $brname = $d->{ovs_bridge
}) {
488 if (my $br = $ifaces->{$brname}) {
489 if ($br->{ovs_ports
}) {
490 my @ports = split (/\s+/, $br->{ovs_ports
});
491 my @new = grep { $_ ne $param->{iface
} } @ports;
492 $br->{ovs_ports
} = join(' ', @new);
498 delete $ifaces->{$param->{iface
}};
500 PVE
::INotify
::write_file
('interfaces', $config);
503 PVE
::Tools
::lock_file
($iflockfn, 10, $code);