]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Zones/Plugin.pm
zone: add vnet_update_hook
[pve-network.git] / PVE / Network / SDN / Zones / Plugin.pm
1 package PVE::Network::SDN::Zones::Plugin;
2
3 use strict;
4 use warnings;
5
6 use PVE::Tools qw(run_command);
7 use PVE::JSONSchema;
8 use PVE::Cluster;
9 use PVE::Network;
10
11 use Data::Dumper;
12 use PVE::JSONSchema qw(get_standard_option);
13 use base qw(PVE::SectionConfig);
14
15 PVE::Cluster::cfs_register_file('sdn/zones.cfg',
16 sub { __PACKAGE__->parse_config(@_); },
17 sub { __PACKAGE__->write_config(@_); });
18
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',
22 });
23
24 PVE::JSONSchema::register_format('pve-sdn-zone-id', \&parse_sdn_zone_id);
25 sub parse_sdn_zone_id {
26 my ($id, $noerr) = @_;
27
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";
31 }
32 die "zone ID '$id' can't be more length than 8 characters\n" if length($id) > 8;
33 return $id;
34 }
35
36 my $defaultData = {
37
38 propertyList => {
39 type => {
40 description => "Plugin type.",
41 type => 'string', format => 'pve-configid',
42 type => 'string',
43 },
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 }),
47 },
48 };
49
50 sub private {
51 return $defaultData;
52 }
53
54 sub 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;
67 }
68
69 return $value;
70 }
71
72 sub 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
82 sub parse_section_header {
83 my ($class, $line) = @_;
84
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); };
89 $errmsg = $@ if $@;
90 my $config = {}; # to return additional attributes
91 return ($type, $id, $errmsg, $config);
92 }
93 return undef;
94 }
95
96 sub generate_sdn_config {
97 my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $controller, $subnet_cfg, $interfaces_config, $config) = @_;
98
99 die "please implement inside plugin";
100 }
101
102 sub generate_controller_config {
103 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
104
105 die "please implement inside plugin";
106 }
107
108 sub generate_controller_vnet_config {
109 my ($class, $plugin_config, $controller, $zoneid, $vnetid, $config) = @_;
110
111 }
112
113 sub write_controller_config {
114 my ($class, $plugin_config, $config) = @_;
115
116 die "please implement inside plugin";
117 }
118
119 sub controller_reload {
120 my ($class) = @_;
121
122 die "please implement inside plugin";
123 }
124
125 sub on_delete_hook {
126 my ($class, $zoneid, $vnet_cfg) = @_;
127
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 }
134 }
135
136 sub on_update_hook {
137 my ($class, $zoneid, $zone_cfg, $controller_cfg) = @_;
138
139 # do nothing by default
140 }
141
142 sub vnet_update_hook {
143 my ($class, $vnet) = @_;
144
145 # do nothing by default
146 }
147
148 #helpers
149 sub parse_tag_number_or_range {
150 my ($str, $max, $tag) = @_;
151
152 my @elements = split(/,/, $str);
153 my $count = 0;
154 my $allowed = undef;
155
156 die "extraneous commas in list\n" if $str ne join(',', @elements);
157 foreach my $item (@elements) {
158 if ($item =~ m/^([0-9]+)-([0-9]+)$/) {
159 $count += 2;
160 my ($port1, $port2) = ($1, $2);
161 die "invalid port '$port1'\n" if $port1 > $max;
162 die "invalid port '$port2'\n" if $port2 > $max;
163 die "backwards range '$port1:$port2' not allowed, did you mean '$port2:$port1'?\n" if $port1 > $port2;
164
165 if ($tag && $tag >= $port1 && $tag <= $port2){
166 $allowed = 1;
167 last;
168 }
169
170 } elsif ($item =~ m/^([0-9]+)$/) {
171 $count += 1;
172 my $port = $1;
173 die "invalid port '$port'\n" if $port > $max;
174
175 if ($tag && $tag == $port){
176 $allowed = 1;
177 last;
178 }
179 }
180 }
181 die "tag $tag is not allowed" if $tag && !$allowed;
182
183 return (scalar(@elements) > 1);
184 }
185
186 sub status {
187 my ($class, $plugin_config, $zone, $vnetid, $vnet, $status) = @_;
188
189 my $err_msg = [];
190
191 # ifaces to check
192 my $ifaces = [ $vnetid ];
193
194 foreach my $iface (@{$ifaces}) {
195 if (!$status->{$iface}->{status}) {
196 push @$err_msg, "missing $iface";
197 } elsif ($status->{$iface}->{status} ne 'pass') {
198 push @$err_msg, "error $iface";
199 }
200 }
201 return $err_msg;
202 }
203
204
205 sub tap_create {
206 my ($class, $plugin_config, $vnet, $iface, $vnetid) = @_;
207
208 PVE::Network::tap_create($iface, $vnetid);
209 }
210
211 sub veth_create {
212 my ($class, $plugin_config, $vnet, $veth, $vethpeer, $vnetid, $hwaddr) = @_;
213
214 PVE::Network::veth_create($veth, $vethpeer, $vnetid, $hwaddr);
215 }
216
217 sub tap_plug {
218 my ($class, $plugin_config, $vnet, $tag, $iface, $vnetid, $firewall, $trunks, $rate) = @_;
219
220 my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$vnetid/bridge/vlan_filtering");
221 die "vm vlans are not allowed on vnet $vnetid" if !$vlan_aware && ($tag || $trunks);
222
223 PVE::Network::tap_plug($iface, $vnetid, $tag, $firewall, $trunks, $rate);
224 }
225
226 #helper
227
228 sub get_uplink_iface {
229 my ($interfaces_config, $uplink) = @_;
230
231 my $iface = undef;
232 foreach my $id (keys %{$interfaces_config->{ifaces}}) {
233 my $interface = $interfaces_config->{ifaces}->{$id};
234 if (my $iface_uplink = $interface->{'uplink-id'}) {
235 next if $iface_uplink ne $uplink;
236 if($interface->{type} ne 'eth' && $interface->{type} ne 'bond') {
237 warn "uplink $uplink is not a physical or bond interface";
238 next;
239 }
240 $iface = $id;
241 }
242 }
243
244 #create a dummy uplink interface if no uplink found
245 if(!$iface) {
246 warn "can't find uplink $uplink in physical interface";
247 $iface = "uplink${uplink}";
248 }
249
250 return $iface;
251 }
252
253 sub get_local_route_ip {
254 my ($targetip) = @_;
255
256 my $ip = undef;
257 my $interface = undef;
258
259 run_command(['/sbin/ip', 'route', 'get', $targetip], outfunc => sub {
260 if ($_[0] =~ m/src ($PVE::Tools::IPRE)/) {
261 $ip = $1;
262 }
263 if ($_[0] =~ m/dev (\S+)/) {
264 $interface = $1;
265 }
266
267 });
268 return ($ip, $interface);
269 }
270
271
272 sub find_local_ip_interface_peers {
273 my ($peers) = @_;
274
275 my $network_config = PVE::INotify::read_file('interfaces');
276 my $ifaces = $network_config->{ifaces};
277 #is a local ip member of peers list ?
278 foreach my $address (@{$peers}) {
279 while (my $interface = each %$ifaces) {
280 my $ip = $ifaces->{$interface}->{address};
281 if ($ip && $ip eq $address) {
282 return ($ip, $interface);
283 }
284 }
285 }
286
287 #if peer is remote, find source with ip route
288 foreach my $address (@{$peers}) {
289 my ($ip, $interface) = get_local_route_ip($address);
290 return ($ip, $interface);
291 }
292 }
293
294 1;