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);
21 require PVE
::API2
::Network
::SDN
;
25 my $iflockfn = "/etc/network/.pve-interfaces.lock";
27 my $bond_mode_enum = [
29 'active-backup', # OVS and Linux
36 'lacp-balance-slb', # OVS
37 'lacp-balance-tcp', # OVS
40 my $network_type_enum = ['bridge', 'bond', 'eth', 'alias', 'vlan',
41 'OVSBridge', 'OVSBond', 'OVSPort', 'OVSIntPort'];
45 description
=> "Network interface type",
47 enum
=> [@$network_type_enum, 'unknown'],
50 description
=> "Comments",
55 description
=> "Comments",
60 description
=> "Automatically start interface on boot.",
64 bridge_vlan_aware
=> {
65 description
=> "Enable bridge vlan support.",
70 description
=> "Specify the interfaces you want to add to your bridge.",
72 type
=> 'string', format
=> 'pve-iface-list',
75 description
=> "Specify the interfaces you want to add to your bridge.",
77 type
=> 'string', format
=> 'pve-iface-list',
80 description
=> "Specify a VLan tag (used by OVSPort, OVSIntPort, OVSBond)",
87 description
=> "OVS interface options.",
93 description
=> "The OVS bridge associated with a OVS port. This is required when you create an OVS port.",
95 type
=> 'string', format
=> 'pve-iface',
98 description
=> "Specify the interfaces used by the bonding device.",
100 type
=> 'string', format
=> 'pve-iface-list',
103 description
=> "Specify the interfaces used by the bonding device.",
105 type
=> 'string', format
=> 'pve-iface-list',
108 description
=> "Bonding mode.",
110 type
=> 'string', enum
=> $bond_mode_enum,
112 bond_xmit_hash_policy
=> {
113 description
=> "Selects the transmit hash policy to use for slave selection in balance-xor and 802.3ad modes.",
116 enum
=> ['layer2', 'layer2+3', 'layer3+4' ],
119 description
=> 'Default gateway address.',
120 type
=> 'string', format
=> 'ipv4',
124 description
=> 'Network mask.',
125 type
=> 'string', format
=> 'ipv4mask',
127 requires
=> 'address',
130 description
=> 'IP address.',
131 type
=> 'string', format
=> 'ipv4',
133 requires
=> 'netmask',
136 description
=> 'IPv4 CIDR.',
137 type
=> 'string', format
=> 'CIDRv4',
141 description
=> 'Default ipv6 gateway address.',
142 type
=> 'string', format
=> 'ipv6',
146 description
=> 'Network mask.',
147 type
=> 'integer', minimum
=> 0, maximum
=> 128,
149 requires
=> 'address6',
152 description
=> 'IP address.',
153 type
=> 'string', format
=> 'ipv6',
155 requires
=> 'netmask6',
158 description
=> 'IPv6 CIDR.',
159 type
=> 'string', format
=> 'CIDRv6',
164 sub json_config_properties
{
167 foreach my $opt (keys %$confdesc) {
168 $prop->{$opt} = $confdesc->{$opt};
174 __PACKAGE__-
>register_method({
178 permissions
=> { user
=> 'all' },
179 description
=> "List available networks",
182 additionalProperties
=> 0,
184 node
=> get_standard_option
('pve-node'),
186 description
=> "Only list specific interface types.",
188 enum
=> [ @$network_type_enum, 'any_bridge' ],
199 links
=> [ { rel
=> 'child', href
=> "{iface}" } ],
204 my $rpcenv = PVE
::RPCEnvironment
::get
();
206 my $tmp = PVE
::INotify
::read_file
('interfaces', 1);
207 my $config = $tmp->{data
};
208 my $changes = $tmp->{changes
};
210 $rpcenv->set_result_attrib('changes', $changes) if $changes;
212 my $ifaces = $config->{ifaces
};
214 delete $ifaces->{lo
}; # do not list the loopback device
216 if ($param->{type
}) {
217 foreach my $k (keys %$ifaces) {
218 my $type = $ifaces->{$k}->{type
};
219 my $match = ($param->{type
} eq $type) || (
220 ($param->{type
} eq 'any_bridge') &&
221 ($type eq 'bridge' || $type eq 'OVSBridge'));
222 delete $ifaces->{$k} if !$match;
226 return PVE
::RESTHandler
::hash_to_array
($ifaces, 'iface');
229 __PACKAGE__-
>register_method({
230 name
=> 'revert_network_changes',
234 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
237 description
=> "Revert network configuration changes.",
240 additionalProperties
=> 0,
242 node
=> get_standard_option
('pve-node'),
245 returns
=> { type
=> "null" },
249 unlink "/etc/network/interfaces.new";
254 my $check_duplicate = sub {
255 my ($config, $newiface, $key, $name) = @_;
257 foreach my $iface (keys %$config) {
258 raise_param_exc
({ $key => "$name already exists on interface '$iface'." })
259 if ($newiface ne $iface) && $config->{$iface}->{$key};
263 my $check_duplicate_gateway = sub {
264 my ($config, $newiface) = @_;
265 return &$check_duplicate($config, $newiface, 'gateway', 'Default gateway');
268 my $check_duplicate_gateway6 = sub {
269 my ($config, $newiface) = @_;
270 return &$check_duplicate($config, $newiface, 'gateway6', 'Default ipv6 gateway');
274 return Net
::IP
::ip_iptobin
(Net
::IP
::ip_expand_address
(shift, 6), 6);
277 my $check_ipv6_settings = sub {
278 my ($address, $netmask) = @_;
280 raise_param_exc
({ netmask
=> "$netmask is not a valid subnet length for ipv6" })
281 if $netmask < 0 || $netmask > 128;
283 raise_param_exc
({ address
=> "$address is not a valid host IPv6 address." })
284 if !Net
::IP
::ip_is_ipv6
($address);
286 my $binip = ipv6_tobin
($address);
287 my $binmask = Net
::IP
::ip_get_mask
($netmask, 6);
289 my $type = ($binip eq $binmask) ?
'ANYCAST' : Net
::IP
::ip_iptypev6
($binip);
291 if (defined($type) && $type !~ /^(?:(?:GLOBAL|(?:UNIQUE|LINK)-LOCAL)-UNICAST)$/) {
292 raise_param_exc
({ address
=> "$address with type '$type', cannot be used as host IPv6 address." });
296 my $map_cidr_to_address_netmask = sub {
299 if ($param->{cidr
}) {
300 raise_param_exc
({ address
=> "address conflicts with cidr" })
301 if $param->{address
};
302 raise_param_exc
({ netmask
=> "netmask conflicts with cidr" })
303 if $param->{netmask
};
305 my ($address, $netmask) = $param->{cidr
} =~ m!^(.*)/(\d+)$!;
306 $param->{address
} = $address;
307 $param->{netmask
} = $netmask;
308 delete $param->{cidr
};
311 if ($param->{cidr6
}) {
312 raise_param_exc
({ address6
=> "address6 conflicts with cidr6" })
313 if $param->{address6
};
314 raise_param_exc
({ netmask6
=> "netmask6 conflicts with cidr6" })
315 if $param->{netmask6
};
317 my ($address, $netmask) = $param->{cidr6
} =~ m!^(.*)/(\d+)$!;
318 $param->{address6
} = $address;
319 $param->{netmask6
} = $netmask;
320 delete $param->{cidr6
};
324 __PACKAGE__-
>register_method({
325 name
=> 'create_network',
329 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
331 description
=> "Create network device configuration",
335 additionalProperties
=> 0,
336 properties
=> json_config_properties
({
337 node
=> get_standard_option
('pve-node'),
338 iface
=> get_standard_option
('pve-iface')}),
340 returns
=> { type
=> 'null' },
344 my $node = extract_param
($param, 'node');
345 my $iface = extract_param
($param, 'iface');
348 my $config = PVE
::INotify
::read_file
('interfaces');
349 my $ifaces = $config->{ifaces
};
351 raise_param_exc
({ iface
=> "interface already exists" })
352 if $ifaces->{$iface};
354 &$check_duplicate_gateway($ifaces, $iface)
355 if $param->{gateway
};
356 &$check_duplicate_gateway6($ifaces, $iface)
357 if $param->{gateway6
};
359 $map_cidr_to_address_netmask->($param);
361 &$check_ipv6_settings($param->{address6
}, int($param->{netmask6
}))
362 if $param->{address6
};
364 my $families = $param->{families
} = [];
365 push @$families, 'inet'
366 if $param->{address
} && !grep(/^inet$/, @$families);
367 push @$families, 'inet6'
368 if $param->{address6
} && !grep(/^inet6$/, @$families);
369 @$families = ('inet') if !scalar(@$families);
371 $param->{method} = $param->{address
} ?
'static' : 'manual';
372 $param->{method6
} = $param->{address6
} ?
'static' : 'manual';
374 if ($param->{type
} =~ m/^OVS/) {
375 -x
'/usr/bin/ovs-vsctl' ||
376 die "Open VSwitch is not installed (need package 'openvswitch-switch')\n";
379 if ($param->{type
} eq 'OVSIntPort' || $param->{type
} eq 'OVSBond') {
380 my $brname = $param->{ovs_bridge
};
381 raise_param_exc
({ ovs_bridge
=> "parameter is required" }) if !$brname;
382 my $br = $ifaces->{$brname};
383 raise_param_exc
({ ovs_bridge
=> "bridge '$brname' does not exist" }) if !$br;
384 raise_param_exc
({ ovs_bridge
=> "interface '$brname' is no OVS bridge" })
385 if $br->{type
} ne 'OVSBridge';
387 my @ports = split (/\s+/, $br->{ovs_ports
} || '');
388 $br->{ovs_ports
} = join(' ', @ports, $iface)
389 if ! grep { $_ eq $iface } @ports;
392 $ifaces->{$iface} = $param;
394 PVE
::INotify
::write_file
('interfaces', $config);
397 PVE
::Tools
::lock_file
($iflockfn, 10, $code);
403 __PACKAGE__-
>register_method({
404 name
=> 'update_network',
408 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
410 description
=> "Update network device configuration",
414 additionalProperties
=> 0,
415 properties
=> json_config_properties
({
416 node
=> get_standard_option
('pve-node'),
417 iface
=> get_standard_option
('pve-iface'),
419 type
=> 'string', format
=> 'pve-configid-list',
420 description
=> "A list of settings you want to delete.",
424 returns
=> { type
=> 'null' },
428 my $node = extract_param
($param, 'node');
429 my $iface = extract_param
($param, 'iface');
430 my $delete = extract_param
($param, 'delete');
433 my $config = PVE
::INotify
::read_file
('interfaces');
434 my $ifaces = $config->{ifaces
};
436 raise_param_exc
({ iface
=> "interface does not exist" })
437 if !$ifaces->{$iface};
439 my $families = ($param->{families
} ||= []);
440 foreach my $k (PVE
::Tools
::split_list
($delete)) {
441 delete $ifaces->{$iface}->{$k};
442 @$families = grep(!/^inet$/, @$families) if $k eq 'address';
443 @$families = grep(!/^inet6$/, @$families) if $k eq 'address6';
445 delete $ifaces->{$iface}->{netmask
};
446 delete $ifaces->{$iface}->{address
};
447 } elsif ($k eq 'cidr6') {
448 delete $ifaces->{$iface}->{netmask6
};
449 delete $ifaces->{$iface}->{address6
};
453 $map_cidr_to_address_netmask->($param);
455 &$check_duplicate_gateway($ifaces, $iface)
456 if $param->{gateway
};
457 &$check_duplicate_gateway6($ifaces, $iface)
458 if $param->{gateway6
};
460 if ($param->{address
}) {
461 push @$families, 'inet' if !grep(/^inet$/, @$families);
463 @$families = grep(!/^inet$/, @$families);
465 if ($param->{address6
}) {
466 &$check_ipv6_settings($param->{address6
}, int($param->{netmask6
}));
467 push @$families, 'inet6' if !grep(/^inet6$/, @$families);
469 @$families = grep(!/^inet6$/, @$families);
471 @$families = ('inet') if !scalar(@$families);
473 $param->{method} = $param->{address
} ?
'static' : 'manual';
474 $param->{method6
} = $param->{address6
} ?
'static' : 'manual';
476 foreach my $k (keys %$param) {
477 $ifaces->{$iface}->{$k} = $param->{$k};
480 PVE
::INotify
::write_file
('interfaces', $config);
483 PVE
::Tools
::lock_file
($iflockfn, 10, $code);
489 __PACKAGE__-
>register_method({
490 name
=> 'network_config',
494 check
=> ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
496 description
=> "Read network device configuration",
499 additionalProperties
=> 0,
501 node
=> get_standard_option
('pve-node'),
502 iface
=> get_standard_option
('pve-iface'),
519 my $config = PVE
::INotify
::read_file
('interfaces');
520 my $ifaces = $config->{ifaces
};
522 raise_param_exc
({ iface
=> "interface does not exist" })
523 if !$ifaces->{$param->{iface
}};
525 return $ifaces->{$param->{iface
}};
528 __PACKAGE__-
>register_method({
529 name
=> 'reload_network_config',
533 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
535 description
=> "Reload network configuration",
539 additionalProperties
=> 0,
541 node
=> get_standard_option
('pve-node'),
544 returns
=> { type
=> 'string' },
549 my $rpcenv = PVE
::RPCEnvironment
::get
();
551 my $authuser = $rpcenv->get_user();
553 my $current_config_file = "/etc/network/interfaces";
554 my $new_config_file = "/etc/network/interfaces.new";
556 die "you need ifupdown2 to reload networking\n" if !-e
'/usr/share/ifupdown2';
558 if (-x
'/usr/bin/ovs-vsctl') {
559 my $ovs_configured = sub {
561 my @ovstypes = grep { $_->{type
} =~ /^ovs\S+/i } values %$ifaces;
562 return scalar(@ovstypes) > 0;
564 my $tmp = PVE
::INotify
::read_file
('interfaces', 1);
565 my $ifaces = $tmp->{data
}->{ifaces
};
566 my $changes = $tmp->{changes
};
568 if ($ovs_configured->($ifaces)) {
569 die "There are OpenVSwitch configured interfaces, but ifupdown2 ".
570 " reload is not compatible with openvswitch currently\n";
571 } elsif ($changes && $changes =~ /^\s*(?:[+-])?\s*(ovs_type|allow-ovs)/mi) {
572 die "Changes include OpenVSwitch interfaces, but ifupdown2 ".
573 "reload is not compatible with openvswitch currently\n";
579 rename($new_config_file, $current_config_file) if -e
$new_config_file;
583 my $network_config = PVE
::Network
::SDN
::generate_etc_network_config
();
584 PVE
::Network
::SDN
::write_etc_network_config
($network_config);
586 $frr_config = PVE
::Network
::SDN
::generate_frr_config
();
587 PVE
::Network
::SDN
::write_frr_config
($frr_config) if $frr_config;
593 if ($line =~ /(warning|error): (\S+):/) {
594 print "$2 : $line \n";
597 PVE
::Tools
::run_command
(['ifreload', '-a'], errfunc
=> $err);
601 if ($line =~ /^line (\S+)/) {
606 if ($frr_config && -e
"/usr/bin/vtysh") {
607 PVE
::Tools
::run_command
(['/usr/bin/vtysh', '-m', '-f', '/etc/frr/frr.conf'], outfunc
=> {}, errfunc
=> $err_frr);
610 return $rpcenv->fork_worker('srvreload', 'networking', $authuser, $worker);
613 __PACKAGE__-
>register_method({
614 name
=> 'delete_network',
618 check
=> ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
620 description
=> "Delete network device configuration",
624 additionalProperties
=> 0,
626 node
=> get_standard_option
('pve-node'),
627 iface
=> get_standard_option
('pve-iface'),
630 returns
=> { type
=> 'null' },
635 my $config = PVE
::INotify
::read_file
('interfaces');
636 my $ifaces = $config->{ifaces
};
638 raise_param_exc
({ iface
=> "interface does not exist" })
639 if !$ifaces->{$param->{iface
}};
641 my $d = $ifaces->{$param->{iface
}};
642 if ($d->{type
} eq 'OVSIntPort' || $d->{type
} eq 'OVSBond') {
643 if (my $brname = $d->{ovs_bridge
}) {
644 if (my $br = $ifaces->{$brname}) {
645 if ($br->{ovs_ports
}) {
646 my @ports = split (/\s+/, $br->{ovs_ports
});
647 my @new = grep { $_ ne $param->{iface
} } @ports;
648 $br->{ovs_ports
} = join(' ', @new);
654 delete $ifaces->{$param->{iface
}};
656 PVE
::INotify
::write_file
('interfaces', $config);
659 PVE
::Tools
::lock_file
($iflockfn, 10, $code);