]>
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
=> 'IPv4 CIDR.',
129 type
=> 'string', format
=> 'CIDRv4',
133 description
=> 'Default ipv6 gateway address.',
134 type
=> 'string', format
=> 'ipv6',
138 description
=> 'Network mask.',
139 type
=> 'integer', minimum
=> 0, maximum
=> 128,
141 requires
=> 'address6',
144 description
=> 'IP address.',
145 type
=> 'string', format
=> 'ipv6',
147 requires
=> 'netmask6',
150 description
=> 'IPv6 CIDR.',
151 type
=> 'string', format
=> 'CIDRv6',
156 sub json_config_properties
{
159 foreach my $opt (keys %$confdesc) {
160 $prop->{$opt} = $confdesc->{$opt};
166 __PACKAGE__-
>register_method({
170 description
=> "List available networks",
172 permissions
=> { check
=> [ 'admin', 'audit' ] },
174 additionalProperties
=> 0,
176 node
=> get_standard_option
('pve-node'),
178 description
=> "Only list specific interface types.",
180 enum
=> [ @$network_type_enum, 'any_bridge' ],
191 links
=> [ { rel
=> 'child', href
=> "{iface}" } ],
196 my $restenv = PMG
::RESTEnvironment-
>get();
198 my $tmp = PVE
::INotify
::read_file
('interfaces', 1);
199 my $config = $tmp->{data
};
200 my $changes = $tmp->{changes
};
202 $restenv->set_result_attrib('changes', $changes) if $changes;
204 my $ifaces = $config->{ifaces
};
206 delete $ifaces->{lo
}; # do not list the loopback device
208 if ($param->{type
}) {
209 foreach my $k (keys %$ifaces) {
210 my $type = $ifaces->{$k}->{type
};
211 my $match = ($param->{type
} eq $type) || (
212 ($param->{type
} eq 'any_bridge') &&
213 ($type eq 'bridge' || $type eq 'OVSBridge'));
214 delete $ifaces->{$k} if !$match;
218 return PVE
::RESTHandler
::hash_to_array
($ifaces, 'iface');
221 __PACKAGE__-
>register_method({
222 name
=> 'revert_network_changes',
226 description
=> "Revert network configuration changes.",
229 additionalProperties
=> 0,
231 node
=> get_standard_option
('pve-node'),
234 returns
=> { type
=> "null" },
238 unlink "/etc/network/interfaces.new";
243 my $check_duplicate = sub {
244 my ($config, $newiface, $key, $name) = @_;
246 foreach my $iface (keys %$config) {
247 raise_param_exc
({ $key => "$name already exists on interface '$iface'." })
248 if ($newiface ne $iface) && $config->{$iface}->{$key};
252 my $check_duplicate_gateway = sub {
253 my ($config, $newiface) = @_;
254 return &$check_duplicate($config, $newiface, 'gateway', 'Default gateway');
257 my $check_duplicate_gateway6 = sub {
258 my ($config, $newiface) = @_;
259 return &$check_duplicate($config, $newiface, 'gateway6', 'Default ipv6 gateway');
263 return Net
::IP
::ip_iptobin
(Net
::IP
::ip_expand_address
(shift, 6), 6);
266 my $check_ipv6_settings = sub {
267 my ($address, $netmask) = @_;
269 raise_param_exc
({ netmask
=> "$netmask is not a valid subnet length for ipv6" })
270 if $netmask < 0 || $netmask > 128;
272 raise_param_exc
({ address
=> "$address is not a valid host ip address." })
273 if !Net
::IP
::ip_is_ipv6
($address);
275 my $binip = ipv6_tobin
($address);
276 my $binmask = Net
::IP
::ip_get_mask
($netmask, 6);
278 my $type = Net
::IP
::ip_iptypev6
($binip);
280 raise_param_exc
({ address
=> "$address is not a valid host ip address." })
281 if ($binip eq $binmask) ||
282 (defined($type) && $type !~ /^(?:(?:GLOBAL|(?:UNIQUE|LINK)-LOCAL)-UNICAST)$/);
285 my $map_cidr_to_address_netmask = sub {
288 if ($param->{cidr
}) {
289 raise_param_exc
({ address
=> "address conflicts with cidr" })
290 if $param->{address
};
291 raise_param_exc
({ netmask
=> "netmask conflicts with cidr" })
292 if $param->{netmask
};
294 my ($address, $netmask) = $param->{cidr
} =~ m!^(.*)/(\d+)$!;
295 $param->{address
} = $address;
296 $param->{netmask
} = $netmask;
297 delete $param->{cidr
};
300 if ($param->{cidr6
}) {
301 raise_param_exc
({ address6
=> "address6 conflicts with cidr6" })
302 if $param->{address6
};
303 raise_param_exc
({ netmask6
=> "netmask6 conflicts with cidr6" })
304 if $param->{netmask6
};
306 my ($address, $netmask) = $param->{cidr6
} =~ m!^(.*)/(\d+)$!;
307 $param->{address6
} = $address;
308 $param->{netmask6
} = $netmask;
309 delete $param->{cidr6
};
313 __PACKAGE__-
>register_method({
314 name
=> 'create_network',
317 description
=> "Create network device configuration",
321 additionalProperties
=> 0,
322 properties
=> json_config_properties
({
323 node
=> get_standard_option
('pve-node'),
324 iface
=> get_standard_option
('pve-iface')}),
326 returns
=> { type
=> 'null' },
330 my $node = extract_param
($param, 'node');
331 my $iface = extract_param
($param, 'iface');
334 my $config = PVE
::INotify
::read_file
('interfaces');
335 my $ifaces = $config->{ifaces
};
337 raise_param_exc
({ iface
=> "interface already exists" })
338 if $ifaces->{$iface};
340 &$check_duplicate_gateway($ifaces, $iface)
341 if $param->{gateway
};
342 &$check_duplicate_gateway6($ifaces, $iface)
343 if $param->{gateway6
};
345 $map_cidr_to_address_netmask->($param);
347 &$check_ipv6_settings($param->{address6
}, int($param->{netmask6
}))
348 if $param->{address6
};
350 my $families = $param->{families
} = [];
351 push @$families, 'inet'
352 if $param->{address
} && !grep(/^inet$/, @$families);
353 push @$families, 'inet6'
354 if $param->{address6
} && !grep(/^inet6$/, @$families);
355 @$families = ('inet') if !scalar(@$families);
357 $param->{method} = $param->{address
} ?
'static' : 'manual';
358 $param->{method6
} = $param->{address6
} ?
'static' : 'manual';
360 if ($param->{type
} =~ m/^OVS/) {
361 -x
'/usr/bin/ovs-vsctl' ||
362 die "Open VSwitch is not installed (need package 'openvswitch-switch')\n";
365 if ($param->{type
} eq 'OVSIntPort' || $param->{type
} eq 'OVSBond') {
366 my $brname = $param->{ovs_bridge
};
367 raise_param_exc
({ ovs_bridge
=> "parameter is required" }) if !$brname;
368 my $br = $ifaces->{$brname};
369 raise_param_exc
({ ovs_bridge
=> "bridge '$brname' does not exist" }) if !$br;
370 raise_param_exc
({ ovs_bridge
=> "interface '$brname' is no OVS bridge" })
371 if $br->{type
} ne 'OVSBridge';
373 my @ports = split (/\s+/, $br->{ovs_ports
} || '');
374 $br->{ovs_ports
} = join(' ', @ports, $iface)
375 if ! grep { $_ eq $iface } @ports;
378 $ifaces->{$iface} = $param;
380 PVE
::INotify
::write_file
('interfaces', $config);
383 PVE
::Tools
::lock_file
($iflockfn, 10, $code);
389 __PACKAGE__-
>register_method({
390 name
=> 'update_network',
393 description
=> "Update network device configuration",
397 additionalProperties
=> 0,
398 properties
=> json_config_properties
({
399 node
=> get_standard_option
('pve-node'),
400 iface
=> get_standard_option
('pve-iface'),
402 type
=> 'string', format
=> 'pve-configid-list',
403 description
=> "A list of settings you want to delete.",
407 returns
=> { type
=> 'null' },
411 my $node = extract_param
($param, 'node');
412 my $iface = extract_param
($param, 'iface');
413 my $delete = extract_param
($param, 'delete');
416 my $config = PVE
::INotify
::read_file
('interfaces');
417 my $ifaces = $config->{ifaces
};
419 raise_param_exc
({ iface
=> "interface does not exist" })
420 if !$ifaces->{$iface};
422 my $families = ($param->{families
} ||= []);
423 foreach my $k (PVE
::Tools
::split_list
($delete)) {
424 delete $ifaces->{$iface}->{$k};
425 @$families = grep(!/^inet$/, @$families) if $k eq 'address';
426 @$families = grep(!/^inet6$/, @$families) if $k eq 'address6';
429 $map_cidr_to_address_netmask->($param);
431 &$check_duplicate_gateway($ifaces, $iface)
432 if $param->{gateway
};
433 &$check_duplicate_gateway6($ifaces, $iface)
434 if $param->{gateway6
};
436 if ($param->{address
}) {
437 push @$families, 'inet' if !grep(/^inet$/, @$families);
439 @$families = grep(!/^inet$/, @$families);
441 if ($param->{address6
}) {
442 &$check_ipv6_settings($param->{address6
}, int($param->{netmask6
}));
443 push @$families, 'inet6' if !grep(/^inet6$/, @$families);
445 @$families = grep(!/^inet6$/, @$families);
447 @$families = ('inet') if !scalar(@$families);
449 $param->{method} = $param->{address
} ?
'static' : 'manual';
450 $param->{method6
} = $param->{address6
} ?
'static' : 'manual';
452 foreach my $k (keys %$param) {
453 $ifaces->{$iface}->{$k} = $param->{$k};
456 PVE
::INotify
::write_file
('interfaces', $config);
459 PVE
::Tools
::lock_file
($iflockfn, 10, $code);
465 __PACKAGE__-
>register_method({
466 name
=> 'network_config',
469 description
=> "Read network device configuration",
471 permissions
=> { check
=> [ 'admin', 'audit' ] },
473 additionalProperties
=> 0,
475 node
=> get_standard_option
('pve-node'),
476 iface
=> get_standard_option
('pve-iface'),
493 my $config = PVE
::INotify
::read_file
('interfaces');
494 my $ifaces = $config->{ifaces
};
496 raise_param_exc
({ iface
=> "interface does not exist" })
497 if !$ifaces->{$param->{iface
}};
499 return $ifaces->{$param->{iface
}};
502 __PACKAGE__-
>register_method({
503 name
=> 'delete_network',
506 description
=> "Delete network device configuration",
510 additionalProperties
=> 0,
512 node
=> get_standard_option
('pve-node'),
513 iface
=> get_standard_option
('pve-iface'),
516 returns
=> { type
=> 'null' },
521 my $config = PVE
::INotify
::read_file
('interfaces');
522 my $ifaces = $config->{ifaces
};
524 raise_param_exc
({ iface
=> "interface does not exist" })
525 if !$ifaces->{$param->{iface
}};
527 my $d = $ifaces->{$param->{iface
}};
528 if ($d->{type
} eq 'OVSIntPort' || $d->{type
} eq 'OVSBond') {
529 if (my $brname = $d->{ovs_bridge
}) {
530 if (my $br = $ifaces->{$brname}) {
531 if ($br->{ovs_ports
}) {
532 my @ports = split (/\s+/, $br->{ovs_ports
});
533 my @new = grep { $_ ne $param->{iface
} } @ports;
534 $br->{ovs_ports
} = join(' ', @new);
540 delete $ifaces->{$param->{iface
}};
542 PVE
::INotify
::write_file
('interfaces', $config);
545 PVE
::Tools
::lock_file
($iflockfn, 10, $code);