]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Zones.pm
e1a7081f4ed0ef6bca21968ff6b8016adcb55484
[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
29 sub sdn_zones_config {
30 my ($cfg, $id, $noerr) = @_;
31
32 die "no sdn zone ID specified\n" if !$id;
33
34 my $scfg = $cfg->{ids}->{$id};
35 die "sdn '$id' does not exist\n" if (!$noerr && !$scfg);
36
37 return $scfg;
38 }
39
40 sub config {
41 my $config = cfs_read_file("sdn/zones.cfg.new");
42 $config = cfs_read_file("sdn/zones.cfg") if !keys %{$config->{ids}};
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.new", $cfg);
57 }
58
59 sub lock_sdn_zones_config {
60 my ($code, $errmsg) = @_;
61
62 cfs_lock_file("sdn/zones.cfg.new", undef, $code);
63 if (my $err = $@) {
64 $errmsg ? die "$errmsg: $err" : die $err;
65 }
66 }
67
68 sub sdn_zones_ids {
69 my ($cfg) = @_;
70
71 return keys %{$cfg->{ids}};
72 }
73
74 sub complete_sdn_zone {
75 my ($cmdname, $pname, $cvalue) = @_;
76
77 my $cfg = PVE::Network::SDN::config();
78
79 return $cmdname eq 'add' ? [] : [ PVE::Network::SDN::sdn_zones_ids($cfg) ];
80 }
81
82
83 sub generate_etc_network_config {
84
85 my $vnet_cfg = PVE::Cluster::cfs_read_file('sdn/vnets.cfg');
86 my $zone_cfg = PVE::Cluster::cfs_read_file('sdn/zones.cfg');
87 my $controller_cfg = PVE::Cluster::cfs_read_file('sdn/controllers.cfg');
88 return if !$vnet_cfg && !$zone_cfg;
89
90 my $interfaces_config = PVE::INotify::read_file('interfaces');
91
92 #generate configuration
93 my $config = {};
94 my $nodename = PVE::INotify::nodename();
95
96 foreach my $id (keys %{$vnet_cfg->{ids}}) {
97 my $vnet = $vnet_cfg->{ids}->{$id};
98 my $zone = $vnet->{zone};
99
100 if(!$zone) {
101 warn "can't generate vnet $vnet : zone $zone don't exist";
102 next;
103 }
104
105 my $plugin_config = $zone_cfg->{ids}->{$zone};
106
107 if (!defined($plugin_config)) {
108 warn "can't generate vnet $vnet : zone $zone don't exist";
109 next;
110 }
111
112 next if defined($plugin_config->{nodes}) && !$plugin_config->{nodes}->{$nodename};
113
114 my $controller = undef;
115 if($plugin_config->{controller}) {
116 my $controllerid = $plugin_config->{controller};
117 $controller = $controller_cfg->{ids}->{$controllerid};
118 }
119
120 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
121 $plugin->generate_sdn_config($plugin_config, $zone, $id, $vnet, $controller, $interfaces_config, $config);
122 }
123
124 my $raw_network_config = "";
125 foreach my $iface (sort keys %$config) {
126 $raw_network_config .= "\n";
127 $raw_network_config .= "auto $iface\n";
128 $raw_network_config .= "iface $iface\n";
129 foreach my $option (@{$config->{$iface}}) {
130 $raw_network_config .= "\t$option\n";
131 }
132 }
133
134 return $raw_network_config;
135 }
136
137 sub write_etc_network_config {
138 my ($rawconfig) = @_;
139
140 return if !$rawconfig;
141 my $sdn_interfaces_file = "/etc/network/interfaces.d/sdn";
142
143 my $writefh = IO::File->new($sdn_interfaces_file,">");
144 print $writefh $rawconfig;
145 $writefh->close();
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 $cluster_vnet_file = "/etc/pve/sdn/vnets.cfg";
178 my $cluster_zone_file = "/etc/pve/sdn/zones.cfg";
179 my $local_sdn_file = "/etc/network/interfaces.d/sdn";
180 my $err_config = undef;
181
182 return if !-e $cluster_vnet_file && !-e $cluster_zone_file;
183
184 if (!-e $local_sdn_file) {
185
186 $err_config = "local sdn network configuration is not yet generated, please reload";
187 warn "$err_config\n";
188 } else {
189 # fixme : use some kind of versioning info?
190 my $cluster_vnet_timestamp = (stat($cluster_vnet_file))[9];
191 my $cluster_zone_timestamp = (stat($cluster_zone_file))[9];
192 my $local_sdn_timestamp = (stat($local_sdn_file))[9];
193
194 if ($local_sdn_timestamp < $cluster_vnet_timestamp || $local_sdn_timestamp < $cluster_zone_timestamp) {
195 $err_config = "local sdn network configuration is too old, please reload";
196 warn "$err_config\n";
197 }
198 }
199
200 my $status = ifquery_check();
201
202 my $vnet_cfg = PVE::Cluster::cfs_read_file('sdn/vnets.cfg');
203 my $zone_cfg = PVE::Cluster::cfs_read_file('sdn/zones.cfg');
204 my $nodename = PVE::INotify::nodename();
205
206
207 my $vnet_status = {};
208 my $zone_status = {};
209
210 foreach my $id (sort keys %{$vnet_cfg->{ids}}) {
211 my $vnet = $vnet_cfg->{ids}->{$id};
212 my $zone = $vnet->{zone};
213 next if !$zone;
214
215 my $plugin_config = $zone_cfg->{ids}->{$zone};
216 next if defined($plugin_config->{nodes}) && !$plugin_config->{nodes}->{$nodename};
217
218 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
219 $plugin->status($plugin_config, $zone, $id, $vnet, $err_config, $status, $vnet_status, $zone_status);
220 }
221
222 return($zone_status, $vnet_status);
223 }
224
225 sub get_bridge_vlan {
226 my ($vnetid) = @_;
227
228 my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
229
230 return ($vnetid, undef) if !$vnet; # fallback for classic bridge
231
232 my $plugin_config = get_plugin_config($vnet);
233 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
234 return $plugin->get_bridge_vlan($plugin_config, $vnetid, $vnet->{tag});
235 }
236
237 sub tap_create {
238 my ($iface, $bridge) = @_;
239
240 my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge);
241 if (!$vnet) { # fallback for classic bridge
242 PVE::Network::tap_create($iface, $bridge);
243 return;
244 }
245
246 my $plugin_config = get_plugin_config($vnet);
247 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
248 $plugin->tap_create($plugin_config, $vnet, $iface, $bridge);
249 }
250
251 sub veth_create {
252 my ($veth, $vethpeer, $bridge, $hwaddr) = @_;
253
254 my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge);
255 if (!$vnet) { # fallback for classic bridge
256 PVE::Network::veth_create($veth, $vethpeer, $bridge, $hwaddr);
257 return;
258 }
259
260 my $plugin_config = get_plugin_config($vnet);
261 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
262 $plugin->veth_create($plugin_config, $vnet, $veth, $vethpeer, $bridge, $hwaddr);
263 }
264
265 sub tap_plug {
266 my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_;
267
268 my $vnet = PVE::Network::SDN::Vnets::get_vnet($bridge);
269 if (!$vnet) { # fallback for classic bridge
270 PVE::Network::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate);
271 return;
272 }
273
274 my $plugin_config = get_plugin_config($vnet);
275 my $nodename = PVE::INotify::nodename();
276
277 die "vnet $bridge is not allowed on this node\n"
278 if $plugin_config->{nodes} && !defined($plugin_config->{nodes}->{$nodename});
279
280 my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type});
281 $plugin->tap_plug($plugin_config, $vnet, $iface, $bridge, $firewall, $rate);
282 }
283
284 1;
285