]>
git.proxmox.com Git - pve-manager.git/blob - PVE/API2/Network.pm
92256863ece5db230f620d6bcf4d99d8cd0a52dc
1 package PVE
::API2
::Network
;
7 use PVE
::Tools
qw(extract_param);
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 iterfaces you want to add to your bridge.",
66 type
=> 'string', format
=> 'pve-iface-list',
69 description
=> "Specify the iterfaces 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
=> 'Default ipv6 gateway address.',
131 type
=> 'string', format
=> 'ipv6',
135 description
=> 'Network mask.',
136 type
=> 'integer', minimum
=> 0, maximum
=> 128,
138 requires
=> 'address6',
141 description
=> 'IP address.',
142 type
=> 'string', format
=> 'ipv6',
144 requires
=> 'netmask6',
148 sub json_config_properties
{
151 foreach my $opt (keys %$confdesc) {
152 $prop->{$opt} = $confdesc->{$opt};
158 __PACKAGE__-
>register_method({
162 permissions
=> { user
=> 'all' },
163 description
=> "List available networks",
166 additionalProperties
=> 0,
168 node
=> get_standard_option
('pve-node'),
170 description
=> "Only list specific interface types.",
172 enum
=> [ @$network_type_enum, 'any_bridge' ],
183 links
=> [ { rel
=> 'child', href
=> "{iface}" } ],
188 my $rpcenv = PVE
::RPCEnvironment
::get
();
190 my $tmp = PVE
::INotify
::read_file
('interfaces', 1);
191 my $config = $tmp->{data
};
192 my $changes = $tmp->{changes
};
194 $rpcenv->set_result_attrib('changes', $changes) if $changes;
196 my $ifaces = $config->{ifaces
};
198 delete $ifaces->{lo
}; # do not list the loopback device
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;
210 return PVE
::RESTHandler
::hash_to_array
($ifaces, 'iface');
213 __PACKAGE__-
>register_method({
214 name
=> 'revert_network_changes',
218 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
221 description
=> "Revert network configuration changes.",
224 additionalProperties
=> 0,
226 node
=> get_standard_option
('pve-node'),
229 returns
=> { type
=> "null" },
233 unlink "/etc/network/interfaces.new";
238 my $check_duplicate = sub {
239 my ($config, $newiface, $key, $name) = @_;
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};
247 my $check_duplicate_gateway = sub {
248 my ($config, $newiface) = @_;
249 return &$check_duplicate($config, $newiface, 'gateway', 'Default gateway');
252 my $check_duplicate_gateway6 = sub {
253 my ($config, $newiface) = @_;
254 return &$check_duplicate($config, $newiface, 'gateway6', 'Default ipv6 gateway');
258 return Net
::IP
::ip_iptobin
(Net
::IP
::ip_expand_address
(shift, 6), 6);
261 my $check_ipv6_settings = sub {
262 my ($address, $netmask) = @_;
264 raise_param_exc
({ netmask
=> "$netmask is not a valid subnet length for ipv6" })
265 if $netmask < 0 || $netmask > 128;
267 raise_param_exc
({ address
=> "$address is not a valid host ip address." })
268 if !Net
::IP
::ip_is_ipv6
($address);
270 my $binip = ipv6_tobin
($address);
271 my $binmask = Net
::IP
::ip_get_mask
($netmask, 6);
273 my $type = Net
::IP
::ip_iptypev6
($binip);
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)$/);
280 __PACKAGE__-
>register_method({
281 name
=> 'create_network',
285 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
287 description
=> "Create network device configuration",
291 additionalProperties
=> 0,
292 properties
=> json_config_properties
({
293 node
=> get_standard_option
('pve-node'),
294 iface
=> get_standard_option
('pve-iface')}),
296 returns
=> { type
=> 'null' },
300 my $node = extract_param
($param, 'node');
301 my $iface = extract_param
($param, 'iface');
304 my $config = PVE
::INotify
::read_file
('interfaces');
305 my $ifaces = $config->{ifaces
};
307 raise_param_exc
({ iface
=> "interface already exists" })
308 if $ifaces->{$iface};
310 &$check_duplicate_gateway($ifaces, $iface)
311 if $param->{gateway
};
312 &$check_duplicate_gateway6($ifaces, $iface)
313 if $param->{gateway6
};
315 &$check_ipv6_settings($param->{address6
}, int($param->{netmask6
}))
316 if $param->{address6
};
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);
325 $param->{method} = $param->{address
} ?
'static' : 'manual';
326 $param->{method6
} = $param->{address6
} ?
'static' : 'manual';
328 if ($param->{type
} =~ m/^OVS/) {
329 -x
'/usr/bin/ovs-vsctl' ||
330 die "Open VSwitch is not installed (need package 'openvswitch-switch')\n";
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';
341 my @ports = split (/\s+/, $br->{ovs_ports
} || '');
342 $br->{ovs_ports
} = join(' ', @ports, $iface)
343 if ! grep { $_ eq $iface } @ports;
346 $ifaces->{$iface} = $param;
348 PVE
::INotify
::write_file
('interfaces', $config);
351 PVE
::Tools
::lock_file
($iflockfn, 10, $code);
357 __PACKAGE__-
>register_method({
358 name
=> 'update_network',
362 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
364 description
=> "Update network device configuration",
368 additionalProperties
=> 0,
369 properties
=> json_config_properties
({
370 node
=> get_standard_option
('pve-node'),
371 iface
=> get_standard_option
('pve-iface'),
373 type
=> 'string', format
=> 'pve-configid-list',
374 description
=> "A list of settings you want to delete.",
378 returns
=> { type
=> 'null' },
382 my $node = extract_param
($param, 'node');
383 my $iface = extract_param
($param, 'iface');
384 my $delete = extract_param
($param, 'delete');
387 my $config = PVE
::INotify
::read_file
('interfaces');
388 my $ifaces = $config->{ifaces
};
390 raise_param_exc
({ iface
=> "interface does not exist" })
391 if !$ifaces->{$iface};
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';
400 &$check_duplicate_gateway($ifaces, $iface)
401 if $param->{gateway
};
402 &$check_duplicate_gateway6($ifaces, $iface)
403 if $param->{gateway6
};
405 if ($param->{address
}) {
406 push @$families, 'inet' if !grep(/^inet$/, @$families);
408 @$families = grep(!/^inet$/, @$families);
410 if ($param->{address6
}) {
411 &$check_ipv6_settings($param->{address6
}, int($param->{netmask6
}));
412 push @$families, 'inet6' if !grep(/^inet6$/, @$families);
414 @$families = grep(!/^inet6$/, @$families);
416 @$families = ('inet') if !scalar(@$families);
418 $param->{method} = $param->{address
} ?
'static' : 'manual';
419 $param->{method6
} = $param->{address6
} ?
'static' : 'manual';
421 foreach my $k (keys %$param) {
422 $ifaces->{$iface}->{$k} = $param->{$k};
425 PVE
::INotify
::write_file
('interfaces', $config);
428 PVE
::Tools
::lock_file
($iflockfn, 10, $code);
434 __PACKAGE__-
>register_method({
435 name
=> 'network_config',
439 check
=> ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
441 description
=> "Read network device configuration",
444 additionalProperties
=> 0,
446 node
=> get_standard_option
('pve-node'),
447 iface
=> get_standard_option
('pve-iface'),
464 my $config = PVE
::INotify
::read_file
('interfaces');
465 my $ifaces = $config->{ifaces
};
467 raise_param_exc
({ iface
=> "interface does not exist" })
468 if !$ifaces->{$param->{iface
}};
470 return $ifaces->{$param->{iface
}};
473 __PACKAGE__-
>register_method({
474 name
=> 'delete_network',
478 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
480 description
=> "Delete network device configuration",
484 additionalProperties
=> 0,
486 node
=> get_standard_option
('pve-node'),
487 iface
=> get_standard_option
('pve-iface'),
490 returns
=> { type
=> 'null' },
495 my $config = PVE
::INotify
::read_file
('interfaces');
496 my $ifaces = $config->{ifaces
};
498 raise_param_exc
({ iface
=> "interface does not exist" })
499 if !$ifaces->{$param->{iface
}};
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);
514 delete $ifaces->{$param->{iface
}};
516 PVE
::INotify
::write_file
('interfaces', $config);
519 PVE
::Tools
::lock_file
($iflockfn, 10, $code);