]> git.proxmox.com Git - pve-network.git/blame - PVE/Network/SDN/Zones/Plugin.pm
vnets: add subnets
[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 }),
6939693f
AD
47 },
48};
49
50sub private {
51 return $defaultData;
52}
53
c2b9c173
AD
54sub decode_value {
55 my ($class, $type, $key, $value) = @_;
56
57 if ($key eq 'nodes') {
58 my $res = {};
59
60 foreach my $node (PVE::Tools::split_list($value)) {
61 if (PVE::JSONSchema::pve_verify_node_name($node)) {
62 $res->{$node} = 1;
63 }
64 }
65
66 return $res;
0f700635 67 }
c2b9c173
AD
68
69 return $value;
70}
71
72sub encode_value {
73 my ($class, $type, $key, $value) = @_;
74
75 if ($key eq 'nodes') {
76 return join(',', keys(%$value));
77 }
78
79 return $value;
80}
81
6939693f
AD
82sub parse_section_header {
83 my ($class, $line) = @_;
84
85 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
f5eabba0 86 my ($type, $id) = (lc($1), $2);
6939693f
AD
87 my $errmsg = undef; # set if you want to skip whole section
88 eval { PVE::JSONSchema::pve_verify_configid($type); };
89 $errmsg = $@ if $@;
90 my $config = {}; # to return additional attributes
f5eabba0 91 return ($type, $id, $errmsg, $config);
6939693f
AD
92 }
93 return undef;
94}
95
6bffe819 96sub generate_sdn_config {
ba7ac021 97 my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $controller, $interfaces_config, $config) = @_;
6939693f
AD
98
99 die "please implement inside plugin";
100}
101
8fb1ee7f 102sub generate_controller_config {
56cdcac9 103 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
32602a38
AD
104
105 die "please implement inside plugin";
106}
107
ad03c543 108sub generate_controller_vnet_config {
56cdcac9 109 my ($class, $plugin_config, $controller, $zoneid, $vnetid, $config) = @_;
ad03c543
AD
110
111}
112
8fb1ee7f
AD
113sub write_controller_config {
114 my ($class, $plugin_config, $config) = @_;
115
116 die "please implement inside plugin";
117}
118
fa609bdd
AD
119sub controller_reload {
120 my ($class) = @_;
121
122 die "please implement inside plugin";
123}
124
fe0c6b9e 125sub on_delete_hook {
56cdcac9 126 my ($class, $zoneid, $vnet_cfg) = @_;
e8d5906e 127
56cdcac9
AD
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);
133 }
e8d5906e
AD
134}
135
136sub on_update_hook {
a2b32a94 137 my ($class, $zoneid, $zone_cfg, $controller_cfg) = @_;
fe0c6b9e
AD
138
139 # do nothing by default
140}
141
1d44ce70
AD
142sub verify_tag {
143 my ($class, $tag) = @_;
144 # do nothing by default
145}
146
6939693f
AD
147#helpers
148sub parse_tag_number_or_range {
149 my ($str, $max, $tag) = @_;
150
151 my @elements = split(/,/, $str);
152 my $count = 0;
153 my $allowed = undef;
154
155 die "extraneous commas in list\n" if $str ne join(',', @elements);
156 foreach my $item (@elements) {
157 if ($item =~ m/^([0-9]+)-([0-9]+)$/) {
158 $count += 2;
159 my ($port1, $port2) = ($1, $2);
160 die "invalid port '$port1'\n" if $port1 > $max;
161 die "invalid port '$port2'\n" if $port2 > $max;
162 die "backwards range '$port1:$port2' not allowed, did you mean '$port2:$port1'?\n" if $port1 > $port2;
163
164 if ($tag && $tag >= $port1 && $tag <= $port2){
165 $allowed = 1;
166 last;
167 }
168
169 } elsif ($item =~ m/^([0-9]+)$/) {
170 $count += 1;
171 my $port = $1;
172 die "invalid port '$port'\n" if $port > $max;
173
174 if ($tag && $tag == $port){
175 $allowed = 1;
176 last;
177 }
178 }
179 }
180 die "tag $tag is not allowed" if $tag && !$allowed;
181
182 return (scalar(@elements) > 1);
183}
184
627b1694 185sub status {
4d7cc94f
AD
186 my ($class, $plugin_config, $zone, $vnetid, $vnet, $status) = @_;
187
188 my $err_msg = [];
627b1694 189
4d7cc94f
AD
190 # ifaces to check
191 my $ifaces = [ $vnetid ];
192
193 foreach my $iface (@{$ifaces}) {
194 if (!$status->{$iface}->{status}) {
195 push @$err_msg, "missing $iface";
196 } elsif ($status->{$iface}->{status} ne 'pass') {
197 push @$err_msg, "error $iface";
198 }
627b1694 199 }
4d7cc94f 200 return $err_msg;
627b1694
AD
201}
202
2ba9613b 203
eb1549e7
AD
204sub tap_create {
205 my ($class, $plugin_config, $vnet, $iface, $vnetid) = @_;
206
912fb443 207 PVE::Network::tap_create($iface, $vnetid);
eb1549e7
AD
208}
209
210sub veth_create {
211 my ($class, $plugin_config, $vnet, $veth, $vethpeer, $vnetid, $hwaddr) = @_;
212
912fb443 213 PVE::Network::veth_create($veth, $vethpeer, $vnetid, $hwaddr);
eb1549e7
AD
214}
215
216sub tap_plug {
912fb443 217 my ($class, $plugin_config, $vnet, $tag, $iface, $vnetid, $firewall, $trunks, $rate) = @_;
eb1549e7 218
912fb443
AD
219 my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$vnetid/bridge/vlan_filtering");
220 die "vm vlans are not allowed on vnet $vnetid" if !$vlan_aware && ($tag || $trunks);
eb1549e7
AD
221
222 PVE::Network::tap_plug($iface, $vnetid, $tag, $firewall, $trunks, $rate);
223}
224
3794e429
AD
225#helper
226
227sub get_uplink_iface {
228 my ($interfaces_config, $uplink) = @_;
229
230 my $iface = undef;
231 foreach my $id (keys %{$interfaces_config->{ifaces}}) {
232 my $interface = $interfaces_config->{ifaces}->{$id};
233 if (my $iface_uplink = $interface->{'uplink-id'}) {
234 next if $iface_uplink ne $uplink;
235 if($interface->{type} ne 'eth' && $interface->{type} ne 'bond') {
236 warn "uplink $uplink is not a physical or bond interface";
237 next;
238 }
239 $iface = $id;
240 }
241 }
242
243 #create a dummy uplink interface if no uplink found
244 if(!$iface) {
245 warn "can't find uplink $uplink in physical interface";
246 $iface = "uplink${uplink}";
247 }
248
249 return $iface;
250}
1f543c5f
AD
251
252sub get_local_route_ip {
253 my ($targetip) = @_;
254
255 my $ip = undef;
256 my $interface = undef;
257
258 run_command(['/sbin/ip', 'route', 'get', $targetip], outfunc => sub {
259 if ($_[0] =~ m/src ($PVE::Tools::IPRE)/) {
260 $ip = $1;
261 }
262 if ($_[0] =~ m/dev (\S+)/) {
263 $interface = $1;
264 }
265
266 });
267 return ($ip, $interface);
268}
269
270
271sub find_local_ip_interface_peers {
272 my ($peers) = @_;
273
274 my $network_config = PVE::INotify::read_file('interfaces');
275 my $ifaces = $network_config->{ifaces};
276 #is a local ip member of peers list ?
277 foreach my $address (@{$peers}) {
278 while (my $interface = each %$ifaces) {
279 my $ip = $ifaces->{$interface}->{address};
280 if ($ip && $ip eq $address) {
281 return ($ip, $interface);
282 }
283 }
284 }
285
286 #if peer is remote, find source with ip route
287 foreach my $address (@{$peers}) {
288 my ($ip, $interface) = get_local_route_ip($address);
289 return ($ip, $interface);
290 }
291}
292
6939693f 2931;