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
}),
55 my ($class, $type, $key, $value) = @_;
57 if ($key eq 'nodes') {
60 foreach my $node (PVE
::Tools
::split_list
($value)) {
61 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
73 my ($class, $type, $key, $value) = @_;
75 if ($key eq 'nodes') {
76 return join(',', keys(%$value));
82 sub parse_section_header
{
83 my ($class, $line) = @_;
85 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
86 my ($type, $id) = (lc($1), $2);
87 my $errmsg = undef; # set if you want to skip whole section
88 eval { PVE
::JSONSchema
::pve_verify_configid
($type); };
90 my $config = {}; # to return additional attributes
91 return ($type, $id, $errmsg, $config);
96 sub generate_sdn_config
{
97 my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $controller, $interfaces_config, $config) = @_;
99 die "please implement inside plugin";
102 sub generate_controller_config
{
103 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
105 die "please implement inside plugin";
108 sub generate_controller_vnet_config
{
109 my ($class, $plugin_config, $controller, $zoneid, $vnetid, $config) = @_;
113 sub write_controller_config
{
114 my ($class, $plugin_config, $config) = @_;
116 die "please implement inside plugin";
119 sub controller_reload
{
122 die "please implement inside plugin";
126 my ($class, $zoneid, $vnet_cfg) = @_;
128 # verify that no vnet are associated to this zone
129 foreach my $id (keys %{$vnet_cfg->{ids
}}) {
130 my $vnet = $vnet_cfg->{ids
}->{$id};
131 die "zone $zoneid is used by vnet $id"
132 if ($vnet->{type
} eq 'vnet' && defined($vnet->{zone
}) && $vnet->{zone
} eq $zoneid);
137 my ($class, $zoneid, $zone_cfg, $controller_cfg) = @_;
139 # do nothing by default
143 sub parse_tag_number_or_range
{
144 my ($str, $max, $tag) = @_;
146 my @elements = split(/,/, $str);
150 die "extraneous commas in list\n" if $str ne join(',', @elements);
151 foreach my $item (@elements) {
152 if ($item =~ m/^([0-9]+)-([0-9]+)$/) {
154 my ($port1, $port2) = ($1, $2);
155 die "invalid port '$port1'\n" if $port1 > $max;
156 die "invalid port '$port2'\n" if $port2 > $max;
157 die "backwards range '$port1:$port2' not allowed, did you mean '$port2:$port1'?\n" if $port1 > $port2;
159 if ($tag && $tag >= $port1 && $tag <= $port2){
164 } elsif ($item =~ m/^([0-9]+)$/) {
167 die "invalid port '$port'\n" if $port > $max;
169 if ($tag && $tag == $port){
175 die "tag $tag is not allowed" if $tag && !$allowed;
177 return (scalar(@elements) > 1);
181 my ($class, $plugin_config, $zone, $id, $vnet, $err_config, $status, $vnet_status, $zone_status) = @_;
183 $vnet_status->{$id}->{zone
} = $zone;
184 $zone_status->{$zone}->{status
} = 'available' if !defined($zone_status->{$zone}->{status
});
187 $vnet_status->{$id}->{status
} = 'pending';
188 $vnet_status->{$id}->{statusmsg
} = $err_config;
189 $zone_status->{$zone}->{status
} = 'pending';
190 } elsif ($status->{$id}->{status
} && $status->{$id}->{status
} eq 'pass') {
191 $vnet_status->{$id}->{status
} = 'available';
192 my $bridgeport = $status->{$id}->{config
}->{'bridge-ports'};
194 if ($bridgeport && $status->{$bridgeport}->{status
} && $status->{$bridgeport}->{status
} ne 'pass') {
195 $vnet_status->{$id}->{status
} = 'error';
196 $vnet_status->{$id}->{statusmsg
} = 'configuration not fully applied';
197 $zone_status->{$zone}->{status
} = 'error';
201 $vnet_status->{$id}->{status
} = 'error';
202 $vnet_status->{$id}->{statusmsg
} = 'missing';
203 $zone_status->{$zone}->{status
} = 'error';
209 my ($class, $plugin_config, $vnet, $iface, $vnetid) = @_;
211 PVE
::Network
::tap_create
($iface, $vnetid);
215 my ($class, $plugin_config, $vnet, $veth, $vethpeer, $vnetid, $hwaddr) = @_;
217 PVE
::Network
::veth_create
($veth, $vethpeer, $vnetid, $hwaddr);
221 my ($class, $plugin_config, $vnet, $tag, $iface, $vnetid, $firewall, $trunks, $rate) = @_;
223 my $vlan_aware = PVE
::Tools
::file_read_firstline
("/sys/class/net/$vnetid/bridge/vlan_filtering");
224 die "vm vlans are not allowed on vnet $vnetid" if !$vlan_aware && ($tag || $trunks);
226 PVE
::Network
::tap_plug
($iface, $vnetid, $tag, $firewall, $trunks, $rate);
231 sub get_uplink_iface
{
232 my ($interfaces_config, $uplink) = @_;
235 foreach my $id (keys %{$interfaces_config->{ifaces
}}) {
236 my $interface = $interfaces_config->{ifaces
}->{$id};
237 if (my $iface_uplink = $interface->{'uplink-id'}) {
238 next if $iface_uplink ne $uplink;
239 if($interface->{type
} ne 'eth' && $interface->{type
} ne 'bond') {
240 warn "uplink $uplink is not a physical or bond interface";
247 #create a dummy uplink interface if no uplink found
249 warn "can't find uplink $uplink in physical interface";
250 $iface = "uplink${uplink}";
256 sub get_local_route_ip
{
260 my $interface = undef;
262 run_command
(['/sbin/ip', 'route', 'get', $targetip], outfunc
=> sub {
263 if ($_[0] =~ m/src ($PVE::Tools::IPRE)/) {
266 if ($_[0] =~ m/dev (\S+)/) {
271 return ($ip, $interface);
275 sub find_local_ip_interface_peers
{
278 my $network_config = PVE
::INotify
::read_file
('interfaces');
279 my $ifaces = $network_config->{ifaces
};
280 #is a local ip member of peers list ?
281 foreach my $address (@{$peers}) {
282 while (my $interface = each %$ifaces) {
283 my $ip = $ifaces->{$interface}->{address
};
284 if ($ip && $ip eq $address) {
285 return ($ip, $interface);
290 #if peer is remote, find source with ip route
291 foreach my $address (@{$peers}) {
292 my ($ip, $interface) = get_local_route_ip
($address);
293 return ($ip, $interface);