]> git.proxmox.com Git - pve-network.git/blame - PVE/Network/SDN/Zones/Plugin.pm
vnets: allow duplicate tags in differents zones
[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;
eb1549e7 9use PVE::Network;
6939693f
AD
10
11use Data::Dumper;
eec580bf 12use PVE::JSONSchema qw(get_standard_option);
6939693f
AD
13use base qw(PVE::SectionConfig);
14
f5eabba0 15PVE::Cluster::cfs_register_file('sdn/zones.cfg',
6939693f
AD
16 sub { __PACKAGE__->parse_config(@_); },
17 sub { __PACKAGE__->write_config(@_); });
18
f5eabba0
AD
19PVE::JSONSchema::register_standard_option('pve-sdn-zone-id', {
20 description => "The SDN zone object identifier.",
21 type => 'string', format => 'pve-sdn-zone-id',
fe61b14c
AD
22});
23
f5eabba0
AD
24PVE::JSONSchema::register_format('pve-sdn-zone-id', \&parse_sdn_zone_id);
25sub parse_sdn_zone_id {
26 my ($id, $noerr) = @_;
fe61b14c 27
7c5b0f6d
AD
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";
fe61b14c 31 }
4d7d91da 32 die "zone ID '$id' can't be more length than 8 characters\n" if length($id) > 8;
f5eabba0 33 return $id;
fe61b14c
AD
34}
35
6939693f
AD
36my $defaultData = {
37
38 propertyList => {
7d35eaf5 39 type => {
6939693f
AD
40 description => "Plugin type.",
41 type => 'string', format => 'pve-configid',
42 type => 'string',
43 },
c2b9c173 44 nodes => get_standard_option('pve-node-list', { optional => 1 }),
f5eabba0
AD
45 zone => get_standard_option('pve-sdn-zone-id',
46 { completion => \&PVE::Network::SDN::Zones::complete_sdn_zone }),
331e2330
AD
47 ipam => {
48 type => 'string',
49 description => "use a specific ipam",
50 },
6939693f
AD
51 },
52};
53
54sub private {
55 return $defaultData;
56}
57
c2b9c173
AD
58sub decode_value {
59 my ($class, $type, $key, $value) = @_;
60
61 if ($key eq 'nodes') {
62 my $res = {};
63
64 foreach my $node (PVE::Tools::split_list($value)) {
65 if (PVE::JSONSchema::pve_verify_node_name($node)) {
66 $res->{$node} = 1;
67 }
68 }
69
70 return $res;
0f700635 71 }
c2b9c173
AD
72
73 return $value;
74}
75
76sub encode_value {
77 my ($class, $type, $key, $value) = @_;
78
79 if ($key eq 'nodes') {
80 return join(',', keys(%$value));
81 }
82
83 return $value;
84}
85
6939693f
AD
86sub parse_section_header {
87 my ($class, $line) = @_;
88
89 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
f5eabba0 90 my ($type, $id) = (lc($1), $2);
6939693f
AD
91 my $errmsg = undef; # set if you want to skip whole section
92 eval { PVE::JSONSchema::pve_verify_configid($type); };
93 $errmsg = $@ if $@;
94 my $config = {}; # to return additional attributes
f5eabba0 95 return ($type, $id, $errmsg, $config);
6939693f
AD
96 }
97 return undef;
98}
99
6bffe819 100sub generate_sdn_config {
7024ec2b 101 my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $controller, $subnet_cfg, $interfaces_config, $config) = @_;
6939693f
AD
102
103 die "please implement inside plugin";
104}
105
8fb1ee7f 106sub generate_controller_config {
56cdcac9 107 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
32602a38
AD
108
109 die "please implement inside plugin";
110}
111
ad03c543 112sub generate_controller_vnet_config {
56cdcac9 113 my ($class, $plugin_config, $controller, $zoneid, $vnetid, $config) = @_;
ad03c543
AD
114
115}
116
8fb1ee7f
AD
117sub write_controller_config {
118 my ($class, $plugin_config, $config) = @_;
119
120 die "please implement inside plugin";
121}
122
fa609bdd
AD
123sub controller_reload {
124 my ($class) = @_;
125
126 die "please implement inside plugin";
127}
128
fe0c6b9e 129sub on_delete_hook {
56cdcac9 130 my ($class, $zoneid, $vnet_cfg) = @_;
e8d5906e 131
56cdcac9
AD
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);
137 }
e8d5906e
AD
138}
139
140sub on_update_hook {
a2b32a94 141 my ($class, $zoneid, $zone_cfg, $controller_cfg) = @_;
fe0c6b9e
AD
142
143 # do nothing by default
144}
145
5ca07ed9 146sub vnet_update_hook {
88d9562b 147 my ($class, $vnet_cfg, $vnetid, $zone_cfg) = @_;
5ca07ed9 148
1d44ce70
AD
149 # do nothing by default
150}
151
6939693f
AD
152#helpers
153sub parse_tag_number_or_range {
154 my ($str, $max, $tag) = @_;
155
156 my @elements = split(/,/, $str);
157 my $count = 0;
158 my $allowed = undef;
159
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]+)$/) {
163 $count += 2;
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;
168
169 if ($tag && $tag >= $port1 && $tag <= $port2){
170 $allowed = 1;
171 last;
172 }
173
174 } elsif ($item =~ m/^([0-9]+)$/) {
175 $count += 1;
176 my $port = $1;
177 die "invalid port '$port'\n" if $port > $max;
178
179 if ($tag && $tag == $port){
180 $allowed = 1;
181 last;
182 }
183 }
184 }
185 die "tag $tag is not allowed" if $tag && !$allowed;
186
187 return (scalar(@elements) > 1);
188}
189
627b1694 190sub status {
4d7cc94f
AD
191 my ($class, $plugin_config, $zone, $vnetid, $vnet, $status) = @_;
192
193 my $err_msg = [];
627b1694 194
4d7cc94f
AD
195 # ifaces to check
196 my $ifaces = [ $vnetid ];
197
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";
203 }
627b1694 204 }
4d7cc94f 205 return $err_msg;
627b1694
AD
206}
207
2ba9613b 208
eb1549e7
AD
209sub tap_create {
210 my ($class, $plugin_config, $vnet, $iface, $vnetid) = @_;
211
912fb443 212 PVE::Network::tap_create($iface, $vnetid);
eb1549e7
AD
213}
214
215sub veth_create {
216 my ($class, $plugin_config, $vnet, $veth, $vethpeer, $vnetid, $hwaddr) = @_;
217
912fb443 218 PVE::Network::veth_create($veth, $vethpeer, $vnetid, $hwaddr);
eb1549e7
AD
219}
220
221sub tap_plug {
912fb443 222 my ($class, $plugin_config, $vnet, $tag, $iface, $vnetid, $firewall, $trunks, $rate) = @_;
eb1549e7 223
912fb443
AD
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);
eb1549e7
AD
226
227 PVE::Network::tap_plug($iface, $vnetid, $tag, $firewall, $trunks, $rate);
228}
229
3794e429
AD
230#helper
231
232sub get_uplink_iface {
233 my ($interfaces_config, $uplink) = @_;
234
235 my $iface = undef;
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";
242 next;
243 }
244 $iface = $id;
245 }
246 }
247
248 #create a dummy uplink interface if no uplink found
249 if(!$iface) {
250 warn "can't find uplink $uplink in physical interface";
251 $iface = "uplink${uplink}";
252 }
253
254 return $iface;
255}
1f543c5f
AD
256
257sub get_local_route_ip {
258 my ($targetip) = @_;
259
260 my $ip = undef;
261 my $interface = undef;
262
263 run_command(['/sbin/ip', 'route', 'get', $targetip], outfunc => sub {
264 if ($_[0] =~ m/src ($PVE::Tools::IPRE)/) {
265 $ip = $1;
266 }
267 if ($_[0] =~ m/dev (\S+)/) {
268 $interface = $1;
269 }
270
271 });
272 return ($ip, $interface);
273}
274
275
276sub find_local_ip_interface_peers {
277 my ($peers) = @_;
278
279 my $network_config = PVE::INotify::read_file('interfaces');
280 my $ifaces = $network_config->{ifaces};
281 #is a local ip member of peers list ?
282 foreach my $address (@{$peers}) {
283 while (my $interface = each %$ifaces) {
284 my $ip = $ifaces->{$interface}->{address};
285 if ($ip && $ip eq $address) {
286 return ($ip, $interface);
287 }
288 }
289 }
290
291 #if peer is remote, find source with ip route
292 foreach my $address (@{$peers}) {
293 my ($ip, $interface) = get_local_route_ip($address);
294 return ($ip, $interface);
295 }
296}
297
6939693f 2981;