1 package PVE
::Network
::SDN
::Zones
::Plugin
;
6 use PVE
::Tools
qw(run_command);
12 use PVE
::JSONSchema
qw(get_standard_option);
13 use base
qw(PVE::SectionConfig);
15 PVE
::Cluster
::cfs_register_file
('sdn/zones.cfg',
16 sub { __PACKAGE__-
>parse_config(@_); },
17 sub { __PACKAGE__-
>write_config(@_); });
19 PVE
::JSONSchema
::register_standard_option
('pve-sdn-zone-id', {
20 description
=> "The SDN zone object identifier.",
21 type
=> 'string', format
=> 'pve-sdn-zone-id',
24 PVE
::JSONSchema
::register_format
('pve-sdn-zone-id', \
&parse_sdn_zone_id
);
25 sub parse_sdn_zone_id
{
26 my ($id, $noerr) = @_;
28 if ($id !~ m/^[a-z][a-z0-9]*[a-z0-9]$/i) {
29 return undef if $noerr;
30 die "zone ID '$id' contains illegal characters\n";
32 die "zone ID '$id' can't be more length than 8 characters\n" if length($id) > 8;
40 description
=> "Plugin type.",
41 type
=> 'string', format
=> 'pve-configid',
44 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
45 zone
=> get_standard_option
('pve-sdn-zone-id',
46 { completion
=> \
&PVE
::Network
::SDN
::Zones
::complete_sdn_zone
}),
49 description
=> "use a specific ipam",
58 sub parse_section_header
{
59 my ($class, $line) = @_;
61 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
62 my ($type, $id) = (lc($1), $2);
63 my $errmsg = undef; # set if you want to skip whole section
64 eval { PVE
::JSONSchema
::pve_verify_configid
($type); };
66 my $config = {}; # to return additional attributes
67 return ($type, $id, $errmsg, $config);
73 my ($class, $type, $key, $value) = @_;
75 if ($key eq 'nodes' || $key eq 'exitnodes') {
78 foreach my $node (PVE
::Tools
::split_list
($value)) {
79 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
91 my ($class, $type, $key, $value) = @_;
93 if ($key eq 'nodes' || $key eq 'exitnodes') {
94 return join(',', keys(%$value));
100 sub generate_sdn_config
{
101 my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $controller, $controller_cfg, $subnet_cfg, $interfaces_config, $config) = @_;
103 die "please implement inside plugin";
106 sub generate_controller_config
{
107 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
109 die "please implement inside plugin";
112 sub generate_controller_vnet_config
{
113 my ($class, $plugin_config, $controller, $zoneid, $vnetid, $config) = @_;
117 sub write_controller_config
{
118 my ($class, $plugin_config, $config) = @_;
120 die "please implement inside plugin";
123 sub controller_reload
{
126 die "please implement inside plugin";
130 my ($class, $zoneid, $vnet_cfg) = @_;
132 # verify that no vnet are associated to this zone
133 foreach my $id (keys %{$vnet_cfg->{ids
}}) {
134 my $vnet = $vnet_cfg->{ids
}->{$id};
135 die "zone $zoneid is used by vnet $id"
136 if ($vnet->{type
} eq 'vnet' && defined($vnet->{zone
}) && $vnet->{zone
} eq $zoneid);
141 my ($class, $zoneid, $zone_cfg, $controller_cfg) = @_;
143 # do nothing by default
146 sub vnet_update_hook
{
147 my ($class, $vnet_cfg, $vnetid, $zone_cfg) = @_;
149 # do nothing by default
153 sub parse_tag_number_or_range
{
154 my ($str, $max, $tag) = @_;
156 my @elements = split(/,/, $str);
160 die "extraneous commas in list\n" if $str ne join(',', @elements);
161 foreach my $item (@elements) {
162 if ($item =~ m/^([0-9]+)-([0-9]+)$/) {
164 my ($port1, $port2) = ($1, $2);
165 die "invalid port '$port1'\n" if $port1 > $max;
166 die "invalid port '$port2'\n" if $port2 > $max;
167 die "backwards range '$port1:$port2' not allowed, did you mean '$port2:$port1'?\n" if $port1 > $port2;
169 if ($tag && $tag >= $port1 && $tag <= $port2){
174 } elsif ($item =~ m/^([0-9]+)$/) {
177 die "invalid port '$port'\n" if $port > $max;
179 if ($tag && $tag == $port){
185 die "tag $tag is not allowed" if $tag && !$allowed;
187 return (scalar(@elements) > 1);
191 my ($class, $plugin_config, $zone, $vnetid, $vnet, $status) = @_;
196 my $ifaces = [ $vnetid ];
198 foreach my $iface (@{$ifaces}) {
199 if (!$status->{$iface}->{status
}) {
200 push @$err_msg, "missing $iface";
201 } elsif ($status->{$iface}->{status
} ne 'pass') {
202 push @$err_msg, "error $iface";
210 my ($class, $plugin_config, $vnet, $iface, $vnetid) = @_;
212 PVE
::Network
::tap_create
($iface, $vnetid);
216 my ($class, $plugin_config, $vnet, $veth, $vethpeer, $vnetid, $hwaddr) = @_;
218 PVE
::Network
::veth_create
($veth, $vethpeer, $vnetid, $hwaddr);
222 my ($class, $plugin_config, $vnet, $tag, $iface, $vnetid, $firewall, $trunks, $rate) = @_;
224 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$vnetid/bridge/vlan_filtering");
225 die "vm vlans are not allowed on vnet $vnetid" if !$vlan_aware && ($tag || $trunks);
227 PVE
::Network
::tap_plug
($iface, $vnetid, $tag, $firewall, $trunks, $rate);
232 sub get_uplink_iface
{
233 my ($interfaces_config, $uplink) = @_;
236 foreach my $id (keys %{$interfaces_config->{ifaces
}}) {
237 my $interface = $interfaces_config->{ifaces
}->{$id};
238 if (my $iface_uplink = $interface->{'uplink-id'}) {
239 next if $iface_uplink ne $uplink;
240 if($interface->{type
} ne 'eth' && $interface->{type
} ne 'bond') {
241 warn "uplink $uplink is not a physical or bond interface";
248 #create a dummy uplink interface if no uplink found
250 warn "can't find uplink $uplink in physical interface";
251 $iface = "uplink${uplink}";
257 sub get_local_route_ip
{
261 my $interface = undef;
263 run_command
(['/sbin/ip', 'route', 'get', $targetip], outfunc
=> sub {
264 if ($_[0] =~ m/src ($PVE::Tools::IPRE)/) {
267 if ($_[0] =~ m/dev (\S+)/) {
272 return ($ip, $interface);
276 sub find_local_ip_interface_peers
{
277 my ($peers, $iface) = @_;
279 my $network_config = PVE
::INotify
::read_file
('interfaces');
280 my $ifaces = $network_config->{ifaces
};
282 #if iface is defined, return ip if exist (if not,try to find it on other ifaces)
284 my $ip = $ifaces->{$iface}->{address
};
285 return ($ip,$iface) if $ip;
288 #is a local ip member of peers list ?
289 foreach my $address (@{$peers}) {
290 while (my $interface = each %$ifaces) {
291 my $ip = $ifaces->{$interface}->{address
};
292 if ($ip && $ip eq $address) {
293 return ($ip, $interface);
298 #if peer is remote, find source with ip route
299 foreach my $address (@{$peers}) {
300 my ($ip, $interface) = get_local_route_ip
($address);
301 return ($ip, $interface);