]> git.proxmox.com Git - pve-network.git/blame - PVE/Network/SDN/Zones/Plugin.pm
qinq plugin :add status
[pve-network.git] / PVE / Network / SDN / Zones / Plugin.pm
CommitLineData
f5eabba0 1package PVE::Network::SDN::Zones::Plugin;
6939693f
AD
2
3use strict;
4use warnings;
5
1f543c5f 6use PVE::Tools qw(run_command);
6939693f
AD
7use PVE::JSONSchema;
8use PVE::Cluster;
9
10use Data::Dumper;
eec580bf 11use PVE::JSONSchema qw(get_standard_option);
6939693f
AD
12use base qw(PVE::SectionConfig);
13
f5eabba0 14PVE::Cluster::cfs_register_file('sdn/zones.cfg',
39d04c82
AD
15 sub { __PACKAGE__->parse_config(@_); });
16
f5eabba0 17PVE::Cluster::cfs_register_file('sdn/zones.cfg.new',
6939693f
AD
18 sub { __PACKAGE__->parse_config(@_); },
19 sub { __PACKAGE__->write_config(@_); });
20
f5eabba0
AD
21PVE::JSONSchema::register_standard_option('pve-sdn-zone-id', {
22 description => "The SDN zone object identifier.",
23 type => 'string', format => 'pve-sdn-zone-id',
fe61b14c
AD
24});
25
f5eabba0
AD
26PVE::JSONSchema::register_format('pve-sdn-zone-id', \&parse_sdn_zone_id);
27sub parse_sdn_zone_id {
28 my ($id, $noerr) = @_;
fe61b14c 29
7c5b0f6d
AD
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";
fe61b14c 33 }
7c5b0f6d 34 die "zone ID '$id' can't be more length than 10 characters\n" if length($id) > 10;
f5eabba0 35 return $id;
fe61b14c
AD
36}
37
6939693f
AD
38my $defaultData = {
39
40 propertyList => {
7d35eaf5 41 type => {
6939693f
AD
42 description => "Plugin type.",
43 type => 'string', format => 'pve-configid',
44 type => 'string',
45 },
c2b9c173 46 nodes => get_standard_option('pve-node-list', { optional => 1 }),
f5eabba0
AD
47 zone => get_standard_option('pve-sdn-zone-id',
48 { completion => \&PVE::Network::SDN::Zones::complete_sdn_zone }),
6939693f
AD
49 },
50};
51
52sub private {
53 return $defaultData;
54}
55
c2b9c173
AD
56sub decode_value {
57 my ($class, $type, $key, $value) = @_;
58
59 if ($key eq 'nodes') {
60 my $res = {};
61
62 foreach my $node (PVE::Tools::split_list($value)) {
63 if (PVE::JSONSchema::pve_verify_node_name($node)) {
64 $res->{$node} = 1;
65 }
66 }
67
68 return $res;
69 }
70
71 return $value;
72}
73
74sub encode_value {
75 my ($class, $type, $key, $value) = @_;
76
77 if ($key eq 'nodes') {
78 return join(',', keys(%$value));
79 }
80
81 return $value;
82}
83
6939693f
AD
84sub parse_section_header {
85 my ($class, $line) = @_;
86
87 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
f5eabba0 88 my ($type, $id) = (lc($1), $2);
6939693f
AD
89 my $errmsg = undef; # set if you want to skip whole section
90 eval { PVE::JSONSchema::pve_verify_configid($type); };
91 $errmsg = $@ if $@;
92 my $config = {}; # to return additional attributes
f5eabba0 93 return ($type, $id, $errmsg, $config);
6939693f
AD
94 }
95 return undef;
96}
97
6bffe819 98sub generate_sdn_config {
ba7ac021 99 my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $controller, $interfaces_config, $config) = @_;
6939693f
AD
100
101 die "please implement inside plugin";
102}
103
8fb1ee7f 104sub generate_controller_config {
56cdcac9 105 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
32602a38
AD
106
107 die "please implement inside plugin";
108}
109
ad03c543 110sub generate_controller_vnet_config {
56cdcac9 111 my ($class, $plugin_config, $controller, $zoneid, $vnetid, $config) = @_;
ad03c543
AD
112
113}
114
8fb1ee7f
AD
115sub write_controller_config {
116 my ($class, $plugin_config, $config) = @_;
117
118 die "please implement inside plugin";
119}
120
fa609bdd
AD
121sub controller_reload {
122 my ($class) = @_;
123
124 die "please implement inside plugin";
125}
126
fe0c6b9e 127sub on_delete_hook {
56cdcac9 128 my ($class, $zoneid, $vnet_cfg) = @_;
e8d5906e 129
56cdcac9
AD
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);
135 }
e8d5906e
AD
136}
137
138sub on_update_hook {
a2b32a94 139 my ($class, $zoneid, $zone_cfg, $controller_cfg) = @_;
fe0c6b9e
AD
140
141 # do nothing by default
142}
143
6939693f
AD
144#helpers
145sub parse_tag_number_or_range {
146 my ($str, $max, $tag) = @_;
147
148 my @elements = split(/,/, $str);
149 my $count = 0;
150 my $allowed = undef;
151
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]+)$/) {
155 $count += 2;
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;
160
161 if ($tag && $tag >= $port1 && $tag <= $port2){
162 $allowed = 1;
163 last;
164 }
165
166 } elsif ($item =~ m/^([0-9]+)$/) {
167 $count += 1;
168 my $port = $1;
169 die "invalid port '$port'\n" if $port > $max;
170
171 if ($tag && $tag == $port){
172 $allowed = 1;
173 last;
174 }
175 }
176 }
177 die "tag $tag is not allowed" if $tag && !$allowed;
178
179 return (scalar(@elements) > 1);
180}
181
627b1694
AD
182sub status {
183 my ($class, $plugin_config, $zone, $id, $vnet, $err_config, $status, $vnet_status, $zone_status) = @_;
184
185 $vnet_status->{$id}->{zone} = $zone;
186 $zone_status->{$zone}->{status} = 'available' if !defined($zone_status->{$zone}->{status});
187
188 if($err_config) {
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'};
195
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';
200 }
201
202 } else {
203 $vnet_status->{$id}->{status} = 'error';
204 $vnet_status->{$id}->{statusmsg} = 'missing';
205 $zone_status->{$zone}->{status} = 'error';
206 }
207}
208
3794e429
AD
209#helper
210
211sub get_uplink_iface {
212 my ($interfaces_config, $uplink) = @_;
213
214 my $iface = undef;
215 foreach my $id (keys %{$interfaces_config->{ifaces}}) {
216 my $interface = $interfaces_config->{ifaces}->{$id};
217 if (my $iface_uplink = $interface->{'uplink-id'}) {
218 next if $iface_uplink ne $uplink;
219 if($interface->{type} ne 'eth' && $interface->{type} ne 'bond') {
220 warn "uplink $uplink is not a physical or bond interface";
221 next;
222 }
223 $iface = $id;
224 }
225 }
226
227 #create a dummy uplink interface if no uplink found
228 if(!$iface) {
229 warn "can't find uplink $uplink in physical interface";
230 $iface = "uplink${uplink}";
231 }
232
233 return $iface;
234}
1f543c5f
AD
235
236sub get_local_route_ip {
237 my ($targetip) = @_;
238
239 my $ip = undef;
240 my $interface = undef;
241
242 run_command(['/sbin/ip', 'route', 'get', $targetip], outfunc => sub {
243 if ($_[0] =~ m/src ($PVE::Tools::IPRE)/) {
244 $ip = $1;
245 }
246 if ($_[0] =~ m/dev (\S+)/) {
247 $interface = $1;
248 }
249
250 });
251 return ($ip, $interface);
252}
253
254
255sub find_local_ip_interface_peers {
256 my ($peers) = @_;
257
258 my $network_config = PVE::INotify::read_file('interfaces');
259 my $ifaces = $network_config->{ifaces};
260 #is a local ip member of peers list ?
261 foreach my $address (@{$peers}) {
262 while (my $interface = each %$ifaces) {
263 my $ip = $ifaces->{$interface}->{address};
264 if ($ip && $ip eq $address) {
265 return ($ip, $interface);
266 }
267 }
268 }
269
270 #if peer is remote, find source with ip route
271 foreach my $address (@{$peers}) {
272 my ($ip, $interface) = get_local_route_ip($address);
273 return ($ip, $interface);
274 }
275}
276
6939693f 2771;