]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Zones.pm
move dns options from subnets to zone
[pve-network.git] / PVE / Network / SDN / Zones.pm
1 package PVE::Network::SDN::Zones;
2
3 use strict;
4 use warnings;
5
6 use Data::Dumper;
7 use JSON;
8
9 use PVE::Tools qw(extract_param dir_glob_regex run_command);
10 use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
11 use PVE::Network;
12
13 use PVE::Network::SDN::Vnets;
14 use PVE::Network::SDN::Zones::VlanPlugin;
15 use PVE::Network::SDN::Zones::QinQPlugin;
16 use PVE::Network::SDN::Zones::VxlanPlugin;
17 use PVE::Network::SDN::Zones::EvpnPlugin;
18 use PVE::Network::SDN::Zones::FaucetPlugin;
19 use PVE::Network::SDN::Zones::SimplePlugin;
20 use PVE::Network::SDN::Zones::Plugin;
21
22 PVE::Network::SDN::Zones::VlanPlugin->register();
23 PVE::Network::SDN::Zones::QinQPlugin->register();
24 PVE::Network::SDN::Zones::VxlanPlugin->register();
25 PVE::Network::SDN::Zones::EvpnPlugin->register();
26 PVE::Network::SDN::Zones::FaucetPlugin->register();
27 PVE::Network::SDN::Zones::SimplePlugin->register();
28 PVE::Network::SDN::Zones::Plugin->init();
29
30 my $local_network_sdn_file = "/etc/network/interfaces.d/sdn";
31
32 sub sdn_zones_config {
33 my ($cfg, $id, $noerr) = @_;
34
35 die "no sdn zone ID specified\n" if !$id;
36
37 my $scfg = $cfg->{ids}->{$id};
38 die "sdn '$id' does not exist\n" if (!$noerr && !$scfg);
39
40 return $scfg;
41 }
42
43 sub config {
44 my $config = cfs_read_file("sdn/zones.cfg");
45 return $config;
46 }
47
48 sub get_plugin_config {
49 my ($vnet) = @_;
50 my $zoneid = $vnet->{zone};
51 my $zone_cfg = PVE::Network::SDN::Zones::config();
52 return $zone_cfg->{ids}->{$zoneid};
53 }
54
55 sub write_config {
56 my ($cfg) = @_;
57
58 cfs_write_file("sdn/zones.cfg", $cfg);
59 }
60
61 sub sdn_zones_ids {
62 my ($cfg) = @_;
63
64 return keys %{$cfg->{ids}};
65 }
66
67 sub complete_sdn_zone {
68 my ($cmdname, $pname, $cvalue) = @_;
69
70 my $cfg = PVE::Network::SDN::config();
71
72 return $cmdname eq 'add' ? [] : [ PVE::Network::SDN::sdn_zones_ids($cfg) ];
73 }
74
75 sub get_zone {
76 my ($zoneid, $running) = @_;
77
78 my $cfg = {};
79 if($running) {
80 my $cfg = PVE::Network::SDN::config();
81 $cfg = $cfg->{vnets};
82 } else {
83 $cfg = PVE::Network::SDN::Zones::config();
84 }
85
86 my $zone = PVE::Network::SDN::Zones::sdn_zones_config($cfg, $zoneid, 1);
87
88 return $zone;
89 }
90
91
92 sub generate_etc_network_config {
93
94 my $cfg = PVE::Network::SDN::config();
95
96 my $version = $cfg->{version};
97 my $vnet_cfg = $cfg->{vnets};
98 my $zone_cfg = $cfg->{zones};
99 my $subnet_cfg = $cfg->{subnets};
100 my $controller_cfg = $cfg->{controllers};
101 return if !$vnet_cfg && !$zone_cfg;
102
103 my $interfaces_config = PVE::INotify::read_file('interfaces');
104
105 #generate configuration
106 my $config = {};
107 my $nodename = PVE::INotify::nodename();
108
109 for my $id (sort keys %{$vnet_cfg->{ids}}) {
110 my $vnet = $vnet_cfg->{ids}->{$id};
111 my $zone = $vnet->{zone};
112
113 if (!$zone) {
114 warn "can't generate vnet '$id': no zone assigned!\n";
115 next;
116 }
117
118 my $plugin_config = $zone_cfg->{ids}->{$zone};
119
120 if (!defined($plugin_config)) {
121 warn "can't generate vnet '$id': zone $zone don't exist\n";
122 next;
123 }
124
125 next if defined($plugin_config->{nodes}) && !$plugin_config->{nodes}->{$nodename};
126
127 my $controller;
128 if (my $controllerid = $plugin_config->{controller}) {
129 $controller = $controller_cfg->{ids}->{$controllerid};
130 }
131
132 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
133 eval {
134 $plugin->generate_sdn_config($plugin_config, $zone, $id, $vnet, $controller, $subnet_cfg, $interfaces_config, $config);
135 };
136 if (my $err = $@) {
137 warn "zone $zone : vnet $id : $err\n";
138 next;
139 }
140 }
141
142 my $raw_network_config = "\#version:$version\n";
143 foreach my $iface (sort keys %$config) {
144 $raw_network_config .= "\n";
145 $raw_network_config .= "auto $iface\n";
146 $raw_network_config .= "iface $iface\n";
147 foreach my $option (@{$config->{$iface}}) {
148 $raw_network_config .= "\t$option\n";
149 }
150 }
151
152 return $raw_network_config;
153 }
154
155 sub write_etc_network_config {
156 my ($rawconfig) = @_;
157
158 return if !$rawconfig;
159
160 my $writefh = IO::File->new($local_network_sdn_file,">");
161 print $writefh $rawconfig;
162 $writefh->close();
163 }
164
165 sub read_etc_network_config_version {
166 my $versionstr = PVE::Tools::file_read_firstline($local_network_sdn_file);
167
168 return if !defined($versionstr);
169
170 if ($versionstr =~ m/^\#version:(\d+)$/) {
171 return $1;
172 }
173 }
174
175 sub ifquery_check {
176
177 my $cmd = ['ifquery', '-a', '-c', '-o','json'];
178
179 my $result = '';
180 my $reader = sub { $result .= shift };
181
182 eval {
183 run_command($cmd, outfunc => $reader);
184 };
185
186 my $resultjson = decode_json($result);
187 my $interfaces = {};
188
189 foreach my $interface (@$resultjson) {
190 my $name = $interface->{name};
191 $interfaces->{$name} = {
192 status => $interface->{status},
193 config => $interface->{config},
194 config_status => $interface->{config_status},
195 };
196 }
197
198 return $interfaces;
199 }
200
201 my $warned_about_reload;
202
203 sub status {
204
205 my $err_config = undef;
206
207 my $local_version = PVE::Network::SDN::Zones::read_etc_network_config_version();
208 my $cfg = PVE::Network::SDN::config();
209 my $sdn_version = $cfg->{version};
210
211 return if !$sdn_version;
212
213 if (!$local_version) {
214 $err_config = "local sdn network configuration is not yet generated, please reload";
215 if (!$warned_about_reload) {
216 $warned_about_reload = 1;
217 warn "$err_config\n";
218 }
219 } elsif ($local_version < $sdn_version) {
220 $err_config = "local sdn network configuration is too old, please reload";
221 if (!$warned_about_reload) {
222 $warned_about_reload = 1;
223 warn "$err_config\n";
224 }
225 } else {
226 $warned_about_reload = 0;
227 }
228
229 my $status = ifquery_check();
230
231
232 my $vnet_cfg = $cfg->{vnets};
233 my $zone_cfg = $cfg->{zones};
234 my $nodename = PVE::INotify::nodename();
235
236 my $vnet_status = {};
237 my $zone_status = {};
238
239 for my $id (sort keys %{$zone_cfg->{ids}}) {
240 $zone_status->{$id}->{status} = $err_config ? 'pending' : 'available';
241 }
242
243 foreach my $id (sort keys %{$vnet_cfg->{ids}}) {
244 my $vnet = $vnet_cfg->{ids}->{$id};
245 my $zone = $vnet->{zone};
246 next if !$zone;
247
248 my $plugin_config = $zone_cfg->{ids}->{$zone};
249 next if defined($plugin_config->{nodes}) && !$plugin_config->{nodes}->{$nodename};
250
251 $vnet_status->{$id}->{zone} = $zone;
252 $vnet_status->{$id}->{status} = 'available';
253
254 if ($err_config) {
255 $vnet_status->{$id}->{status} = 'pending';
256 $vnet_status->{$id}->{statusmsg} = $err_config;
257 next;
258 }
259
260 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
261 my $err_msg = $plugin->status($plugin_config, $zone, $id, $vnet, $status);
262 if (@{$err_msg} > 0) {
263 $vnet_status->{$id}->{status} = 'error';
264 $vnet_status->{$id}->{statusmsg} = join(',', @{$err_msg});
265 $zone_status->{$id}->{status} = 'error';
266 }
267 }
268
269 return ($zone_status, $vnet_status);
270 }
271
272 sub tap_create {
273 my ($iface, $bridge) = @_;
274
275 my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge, 1);
276 if (!$vnet) { # fallback for classic bridge
277 PVE::Network::tap_create($iface, $bridge);
278 return;
279 }
280
281 my $plugin_config = get_plugin_config($vnet);
282 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
283 $plugin->tap_create($plugin_config, $vnet, $iface, $bridge);
284 }
285
286 sub veth_create {
287 my ($veth, $vethpeer, $bridge, $hwaddr) = @_;
288
289 my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge, 1);
290 if (!$vnet) { # fallback for classic bridge
291 PVE::Network::veth_create($veth, $vethpeer, $bridge, $hwaddr);
292 return;
293 }
294
295 my $plugin_config = get_plugin_config($vnet);
296 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
297 $plugin->veth_create($plugin_config, $vnet, $veth, $vethpeer, $bridge, $hwaddr);
298 }
299
300 sub tap_plug {
301 my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_;
302
303 my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge, 1);
304 if (!$vnet) { # fallback for classic bridge
305 PVE::Network::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate);
306 return;
307 }
308
309 my $plugin_config = get_plugin_config($vnet);
310 my $nodename = PVE::INotify::nodename();
311
312 die "vnet $bridge is not allowed on this node\n"
313 if $plugin_config->{nodes} && !defined($plugin_config->{nodes}->{$nodename});
314
315 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
316 $plugin->tap_plug($plugin_config, $vnet, $tag, $iface, $bridge, $firewall, $trunks, $rate);
317 }
318
319 1;
320