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