]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Zones.pm
zones: improve error messages and code style
[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 $plugin->generate_sdn_config($plugin_config, $zone, $id, $vnet, $controller, $interfaces_config, $config);
113 }
114
115 my $raw_network_config = "\#version:$version\n";
116 foreach my $iface (sort keys %$config) {
117 $raw_network_config .= "\n";
118 $raw_network_config .= "auto $iface\n";
119 $raw_network_config .= "iface $iface\n";
120 foreach my $option (@{$config->{$iface}}) {
121 $raw_network_config .= "\t$option\n";
122 }
123 }
124
125 return $raw_network_config;
126 }
127
128 sub write_etc_network_config {
129 my ($rawconfig) = @_;
130
131 return if !$rawconfig;
132
133 my $writefh = IO::File->new($local_network_sdn_file,">");
134 print $writefh $rawconfig;
135 $writefh->close();
136 }
137
138 sub read_etc_network_config_version {
139 my $versionstr = PVE::Tools::file_read_firstline($local_network_sdn_file);
140
141 return if !defined($versionstr);
142
143 if ($versionstr =~ m/^\#version:(\d+)$/) {
144 return $1;
145 }
146 }
147
148 sub ifquery_check {
149
150 my $cmd = ['ifquery', '-a', '-c', '-o','json'];
151
152 my $result = '';
153 my $reader = sub { $result .= shift };
154
155 eval {
156 run_command($cmd, outfunc => $reader);
157 };
158
159 my $resultjson = decode_json($result);
160 my $interfaces = {};
161
162 foreach my $interface (@$resultjson) {
163 my $name = $interface->{name};
164 $interfaces->{$name} = {
165 status => $interface->{status},
166 config => $interface->{config},
167 config_status => $interface->{config_status},
168 };
169 }
170
171 return $interfaces;
172 }
173
174 # improve me : move status code inside plugins ?
175 sub status {
176
177 my $err_config = undef;
178
179 my $local_version = PVE::Network::SDN::Zones::read_etc_network_config_version();
180 my $sdn_version = PVE::Cluster::cfs_read_file('sdn/.version');
181
182 return if !$sdn_version;
183
184 if (!$local_version) {
185 $err_config = "local sdn network configuration is not yet generated, please reload";
186 warn "$err_config\n";
187 } elsif ($local_version < $sdn_version) {
188 $err_config = "local sdn network configuration is too old, please reload";
189 warn "$err_config\n";
190 }
191
192 my $status = ifquery_check();
193
194 my $vnet_cfg = PVE::Cluster::cfs_read_file('sdn/vnets.cfg');
195 my $zone_cfg = PVE::Cluster::cfs_read_file('sdn/zones.cfg');
196 my $nodename = PVE::INotify::nodename();
197
198
199 my $vnet_status = {};
200 my $zone_status = {};
201
202 foreach my $id (sort keys %{$vnet_cfg->{ids}}) {
203 my $vnet = $vnet_cfg->{ids}->{$id};
204 my $zone = $vnet->{zone};
205 next if !$zone;
206
207 my $plugin_config = $zone_cfg->{ids}->{$zone};
208 next if defined($plugin_config->{nodes}) && !$plugin_config->{nodes}->{$nodename};
209
210 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
211 $plugin->status($plugin_config, $zone, $id, $vnet, $err_config, $status, $vnet_status, $zone_status);
212 }
213
214 return($zone_status, $vnet_status);
215 }
216
217 sub get_bridge_vlan {
218 my ($vnetid) = @_;
219
220 my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
221
222 return ($vnetid, undef) if !$vnet; # fallback for classic bridge
223
224 my $plugin_config = get_plugin_config($vnet);
225 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
226 return $plugin->get_bridge_vlan($plugin_config, $vnetid, $vnet->{tag});
227 }
228
229 sub tap_create {
230 my ($iface, $bridge) = @_;
231
232 my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge);
233 if (!$vnet) { # fallback for classic bridge
234 PVE::Network::tap_create($iface, $bridge);
235 return;
236 }
237
238 my $plugin_config = get_plugin_config($vnet);
239 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
240 $plugin->tap_create($plugin_config, $vnet, $iface, $bridge);
241 }
242
243 sub veth_create {
244 my ($veth, $vethpeer, $bridge, $hwaddr) = @_;
245
246 my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge);
247 if (!$vnet) { # fallback for classic bridge
248 PVE::Network::veth_create($veth, $vethpeer, $bridge, $hwaddr);
249 return;
250 }
251
252 my $plugin_config = get_plugin_config($vnet);
253 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
254 $plugin->veth_create($plugin_config, $vnet, $veth, $vethpeer, $bridge, $hwaddr);
255 }
256
257 sub tap_plug {
258 my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_;
259
260 my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge);
261 if (!$vnet) { # fallback for classic bridge
262 PVE::Network::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate);
263 return;
264 }
265
266 my $plugin_config = get_plugin_config($vnet);
267 my $nodename = PVE::INotify::nodename();
268
269 die "vnet $bridge is not allowed on this node\n"
270 if $plugin_config->{nodes} && !defined($plugin_config->{nodes}->{$nodename});
271
272 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
273 $plugin->tap_plug($plugin_config, $vnet, $iface, $bridge, $firewall, $rate);
274 }
275
276 1;
277