1 package PVE
::Network
::SDN
::Zones
::Plugin
;
6 use PVE
::Tools
qw(run_command);
11 use PVE
::JSONSchema
qw(get_standard_option);
12 use base
qw(PVE::SectionConfig);
14 PVE
::Cluster
::cfs_register_file
('sdn/zones.cfg',
15 sub { __PACKAGE__-
>parse_config(@_); });
17 PVE
::Cluster
::cfs_register_file
('sdn/zones.cfg.new',
18 sub { __PACKAGE__-
>parse_config(@_); },
19 sub { __PACKAGE__-
>write_config(@_); });
21 PVE
::JSONSchema
::register_standard_option
('pve-sdn-zone-id', {
22 description
=> "The SDN zone object identifier.",
23 type
=> 'string', format
=> 'pve-sdn-zone-id',
26 PVE
::JSONSchema
::register_format
('pve-sdn-zone-id', \
&parse_sdn_zone_id
);
27 sub parse_sdn_zone_id
{
28 my ($id, $noerr) = @_;
30 if ($id !~ m/^[a-z][a-z0-9]*[a-z0-9]$/i) {
31 return undef if $noerr;
32 die "zone ID '$id' contains illegal characters\n";
34 die "zone ID '$id' can't be more length than 10 characters\n" if length($id) > 10;
42 description
=> "Plugin type.",
43 type
=> 'string', format
=> 'pve-configid',
46 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
47 zone
=> get_standard_option
('pve-sdn-zone-id',
48 { completion
=> \
&PVE
::Network
::SDN
::Zones
::complete_sdn_zone
}),
57 my ($class, $type, $key, $value) = @_;
59 if ($key eq 'nodes') {
62 foreach my $node (PVE
::Tools
::split_list
($value)) {
63 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
75 my ($class, $type, $key, $value) = @_;
77 if ($key eq 'nodes') {
78 return join(',', keys(%$value));
84 sub parse_section_header
{
85 my ($class, $line) = @_;
87 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
88 my ($type, $id) = (lc($1), $2);
89 my $errmsg = undef; # set if you want to skip whole section
90 eval { PVE
::JSONSchema
::pve_verify_configid
($type); };
92 my $config = {}; # to return additional attributes
93 return ($type, $id, $errmsg, $config);
98 sub generate_sdn_config
{
99 my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $controller, $interfaces_config, $config) = @_;
101 die "please implement inside plugin";
104 sub generate_controller_config
{
105 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
107 die "please implement inside plugin";
110 sub generate_controller_vnet_config
{
111 my ($class, $plugin_config, $controller, $zoneid, $vnetid, $config) = @_;
115 sub write_controller_config
{
116 my ($class, $plugin_config, $config) = @_;
118 die "please implement inside plugin";
121 sub controller_reload
{
124 die "please implement inside plugin";
128 my ($class, $zoneid, $vnet_cfg) = @_;
130 # verify that no vnet are associated to this zone
131 foreach my $id (keys %{$vnet_cfg->{ids
}}) {
132 my $vnet = $vnet_cfg->{ids
}->{$id};
133 die "zone $zoneid is used by vnet $id"
134 if ($vnet->{type
} eq 'vnet' && defined($vnet->{zone
}) && $vnet->{zone
} eq $zoneid);
139 my ($class, $zoneid, $zone_cfg, $controller_cfg) = @_;
141 # do nothing by default
145 sub parse_tag_number_or_range
{
146 my ($str, $max, $tag) = @_;
148 my @elements = split(/,/, $str);
152 die "extraneous commas in list\n" if $str ne join(',', @elements);
153 foreach my $item (@elements) {
154 if ($item =~ m/^([0-9]+)-([0-9]+)$/) {
156 my ($port1, $port2) = ($1, $2);
157 die "invalid port '$port1'\n" if $port1 > $max;
158 die "invalid port '$port2'\n" if $port2 > $max;
159 die "backwards range '$port1:$port2' not allowed, did you mean '$port2:$port1'?\n" if $port1 > $port2;
161 if ($tag && $tag >= $port1 && $tag <= $port2){
166 } elsif ($item =~ m/^([0-9]+)$/) {
169 die "invalid port '$port'\n" if $port > $max;
171 if ($tag && $tag == $port){
177 die "tag $tag is not allowed" if $tag && !$allowed;
179 return (scalar(@elements) > 1);
183 my ($class, $plugin_config, $zone, $id, $vnet, $err_config, $status, $vnet_status, $zone_status) = @_;
185 $vnet_status->{$id}->{zone
} = $zone;
186 $zone_status->{$zone}->{status
} = 'available' if !defined($zone_status->{$zone}->{status
});
189 $vnet_status->{$id}->{status
} = 'pending';
190 $vnet_status->{$id}->{statusmsg
} = $err_config;
191 $zone_status->{$zone}->{status
} = 'pending';
192 } elsif ($status->{$id}->{status
} && $status->{$id}->{status
} eq 'pass') {
193 $vnet_status->{$id}->{status
} = 'available';
194 my $bridgeport = $status->{$id}->{config
}->{'bridge-ports'};
196 if ($bridgeport && $status->{$bridgeport}->{status
} && $status->{$bridgeport}->{status
} ne 'pass') {
197 $vnet_status->{$id}->{status
} = 'error';
198 $vnet_status->{$id}->{statusmsg
} = 'configuration not fully applied';
199 $zone_status->{$zone}->{status
} = 'error';
203 $vnet_status->{$id}->{status
} = 'error';
204 $vnet_status->{$id}->{statusmsg
} = 'missing';
205 $zone_status->{$zone}->{status
} = 'error';
210 sub get_bridge_vlan
{
211 my ($class, $plugin_config, $zoneid, $vnetid, $tag) = @_;
213 my $bridge = $plugin_config->{bridge
};
214 die "bridge $bridge is missing" if !-d
"/sys/class/net/$bridge/";
219 return ($bridge, $tag);
224 sub get_uplink_iface
{
225 my ($interfaces_config, $uplink) = @_;
228 foreach my $id (keys %{$interfaces_config->{ifaces
}}) {
229 my $interface = $interfaces_config->{ifaces
}->{$id};
230 if (my $iface_uplink = $interface->{'uplink-id'}) {
231 next if $iface_uplink ne $uplink;
232 if($interface->{type
} ne 'eth' && $interface->{type
} ne 'bond') {
233 warn "uplink $uplink is not a physical or bond interface";
240 #create a dummy uplink interface if no uplink found
242 warn "can't find uplink $uplink in physical interface";
243 $iface = "uplink${uplink}";
249 sub get_local_route_ip
{
253 my $interface = undef;
255 run_command
(['/sbin/ip', 'route', 'get', $targetip], outfunc
=> sub {
256 if ($_[0] =~ m/src ($PVE::Tools::IPRE)/) {
259 if ($_[0] =~ m/dev (\S+)/) {
264 return ($ip, $interface);
268 sub find_local_ip_interface_peers
{
271 my $network_config = PVE
::INotify
::read_file
('interfaces');
272 my $ifaces = $network_config->{ifaces
};
273 #is a local ip member of peers list ?
274 foreach my $address (@{$peers}) {
275 while (my $interface = each %$ifaces) {
276 my $ip = $ifaces->{$interface}->{address
};
277 if ($ip && $ip eq $address) {
278 return ($ip, $interface);
283 #if peer is remote, find source with ip route
284 foreach my $address (@{$peers}) {
285 my ($ip, $interface) = get_local_route_ip
($address);
286 return ($ip, $interface);