]>
Commit | Line | Data |
---|---|---|
86d22462 | 1 | package PVE::Network::SDN; |
92b6f291 AD |
2 | |
3 | use strict; | |
4 | use warnings; | |
434125ce | 5 | |
92b6f291 | 6 | use Data::Dumper; |
c665cefc | 7 | use JSON; |
434125ce TL |
8 | |
9 | use PVE::Tools qw(extract_param dir_glob_regex run_command); | |
92b6f291 | 10 | use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file); |
86d22462 AD |
11 | use PVE::Network::SDN::Plugin; |
12 | use PVE::Network::SDN::VnetPlugin; | |
13 | use PVE::Network::SDN::VlanPlugin; | |
3ee45e4c | 14 | use PVE::Network::SDN::VxlanPlugin; |
32602a38 | 15 | use PVE::Network::SDN::FrrPlugin; |
92b6f291 | 16 | |
86d22462 AD |
17 | PVE::Network::SDN::VnetPlugin->register(); |
18 | PVE::Network::SDN::VlanPlugin->register(); | |
3ee45e4c | 19 | PVE::Network::SDN::VxlanPlugin->register(); |
32602a38 | 20 | PVE::Network::SDN::FrrPlugin->register(); |
86d22462 | 21 | PVE::Network::SDN::Plugin->init(); |
92b6f291 AD |
22 | |
23 | ||
6bffe819 AD |
24 | sub sdn_config { |
25 | my ($cfg, $sdnid, $noerr) = @_; | |
92b6f291 | 26 | |
6bffe819 | 27 | die "no sdn ID specified\n" if !$sdnid; |
92b6f291 | 28 | |
6bffe819 AD |
29 | my $scfg = $cfg->{ids}->{$sdnid}; |
30 | die "sdn '$sdnid' does not exists\n" if (!$noerr && !$scfg); | |
92b6f291 AD |
31 | |
32 | return $scfg; | |
33 | } | |
34 | ||
35 | sub config { | |
6bffe819 AD |
36 | my $config = cfs_read_file("sdn.cfg.new"); |
37 | $config = cfs_read_file("sdn.cfg") if !keys %{$config->{ids}}; | |
39d04c82 | 38 | return $config; |
92b6f291 AD |
39 | } |
40 | ||
41 | sub write_config { | |
42 | my ($cfg) = @_; | |
43 | ||
6bffe819 | 44 | cfs_write_file("sdn.cfg.new", $cfg); |
92b6f291 AD |
45 | } |
46 | ||
6bffe819 | 47 | sub lock_sdn_config { |
92b6f291 AD |
48 | my ($code, $errmsg) = @_; |
49 | ||
6bffe819 | 50 | cfs_lock_file("sdn.cfg.new", undef, $code); |
0c5021ad | 51 | if (my $err = $@) { |
92b6f291 AD |
52 | $errmsg ? die "$errmsg: $err" : die $err; |
53 | } | |
54 | } | |
55 | ||
6bffe819 | 56 | sub sdn_ids { |
92b6f291 AD |
57 | my ($cfg) = @_; |
58 | ||
59 | return keys %{$cfg->{ids}}; | |
60 | } | |
61 | ||
6bffe819 | 62 | sub complete_sdn { |
92b6f291 AD |
63 | my ($cmdname, $pname, $cvalue) = @_; |
64 | ||
86d22462 | 65 | my $cfg = PVE::Network::SDN::config(); |
92b6f291 | 66 | |
6bffe819 | 67 | return $cmdname eq 'add' ? [] : [ PVE::Network::SDN::sdn_ids($cfg) ]; |
92b6f291 AD |
68 | } |
69 | ||
e424c7ac | 70 | sub ifquery_check { |
c665cefc AD |
71 | |
72 | my $cmd = ['ifquery', '-a', '-c', '-o','json']; | |
c665cefc | 73 | |
0c5021ad TL |
74 | my $result = ''; |
75 | my $reader = sub { $result .= shift }; | |
c665cefc AD |
76 | |
77 | eval { | |
6e9fff39 | 78 | run_command($cmd, outfunc => $reader); |
c665cefc AD |
79 | }; |
80 | ||
6e9fff39 | 81 | my $resultjson = decode_json($result); |
c665cefc AD |
82 | my $interfaces = {}; |
83 | ||
84 | foreach my $interface (@$resultjson) { | |
6e9fff39 AD |
85 | my $name = $interface->{name}; |
86 | $interfaces->{$name} = { | |
87 | status => $interface->{status}, | |
88 | config => $interface->{config}, | |
89 | config_status => $interface->{config_status}, | |
90 | }; | |
c665cefc AD |
91 | } |
92 | ||
93 | return $interfaces; | |
94 | } | |
95 | ||
80348b2d AD |
96 | |
97 | sub generate_etc_network_config { | |
98 | ||
99 | my $sdn_cfg = PVE::Cluster::cfs_read_file('sdn.cfg'); | |
100 | return if !$sdn_cfg; | |
101 | ||
102 | #read main config for physical interfaces | |
103 | my $current_config_file = "/etc/network/interfaces"; | |
104 | my $fh = IO::File->new($current_config_file); | |
105 | my $interfaces_config = PVE::INotify::read_etc_network_interfaces(1,$fh); | |
106 | $fh->close(); | |
107 | ||
108 | #check uplinks | |
109 | my $uplinks = {}; | |
110 | foreach my $id (keys %{$interfaces_config->{ifaces}}) { | |
111 | my $interface = $interfaces_config->{ifaces}->{$id}; | |
112 | if (my $uplink = $interface->{'uplink-id'}) { | |
113 | die "uplink-id $uplink is already defined on $uplinks->{$uplink}" if $uplinks->{$uplink}; | |
114 | $interface->{name} = $id; | |
115 | $uplinks->{$interface->{'uplink-id'}} = $interface; | |
116 | } | |
117 | } | |
118 | ||
119 | my $vnet_cfg = undef; | |
120 | my $transport_cfg = undef; | |
121 | ||
122 | foreach my $id (keys %{$sdn_cfg->{ids}}) { | |
123 | if ($sdn_cfg->{ids}->{$id}->{type} eq 'vnet') { | |
124 | $vnet_cfg->{ids}->{$id} = $sdn_cfg->{ids}->{$id}; | |
125 | } else { | |
126 | $transport_cfg->{ids}->{$id} = $sdn_cfg->{ids}->{$id}; | |
127 | } | |
128 | } | |
129 | ||
130 | #generate configuration | |
93dea3aa | 131 | my $config = {}; |
80348b2d AD |
132 | foreach my $id (keys %{$vnet_cfg->{ids}}) { |
133 | my $vnet = $vnet_cfg->{ids}->{$id}; | |
134 | my $zone = $vnet->{transportzone}; | |
135 | ||
136 | if(!$zone) { | |
137 | warn "can't generate vnet $vnet : zone $zone don't exist"; | |
138 | next; | |
139 | } | |
140 | ||
141 | my $plugin_config = $transport_cfg->{ids}->{$zone}; | |
142 | ||
143 | if (!defined($plugin_config)) { | |
144 | warn "can't generate vnet $vnet : zone $zone don't exist"; | |
145 | next; | |
146 | } | |
147 | ||
148 | my $plugin = PVE::Network::SDN::Plugin->lookup($plugin_config->{type}); | |
93dea3aa | 149 | $plugin->generate_sdn_config($plugin_config, $zone, $id, $vnet, $uplinks, $config); |
80348b2d AD |
150 | } |
151 | ||
93dea3aa | 152 | my $raw_network_config = ""; |
87d8b623 | 153 | foreach my $iface (keys %$config) { |
93dea3aa AD |
154 | $raw_network_config .= "\n"; |
155 | $raw_network_config .= "auto $iface\n"; | |
156 | $raw_network_config .= "iface $iface\n"; | |
87d8b623 | 157 | foreach my $option (@{$config->{$iface}}) { |
93dea3aa AD |
158 | $raw_network_config .= "\t$option\n"; |
159 | } | |
160 | } | |
161 | ||
87d8b623 AD |
162 | return $raw_network_config; |
163 | } | |
164 | ||
8fb1ee7f | 165 | sub generate_controller_config { |
87d8b623 AD |
166 | |
167 | my $sdn_cfg = PVE::Cluster::cfs_read_file('sdn.cfg'); | |
168 | return if !$sdn_cfg; | |
169 | ||
170 | #read main config for physical interfaces | |
171 | my $current_config_file = "/etc/network/interfaces"; | |
172 | my $fh = IO::File->new($current_config_file); | |
173 | my $interfaces_config = PVE::INotify::read_etc_network_interfaces(1,$fh); | |
174 | $fh->close(); | |
175 | ||
176 | #check uplinks | |
177 | my $uplinks = {}; | |
178 | foreach my $id (keys %{$interfaces_config->{ifaces}}) { | |
179 | my $interface = $interfaces_config->{ifaces}->{$id}; | |
180 | if (my $uplink = $interface->{'uplink-id'}) { | |
181 | die "uplink-id $uplink is already defined on $uplinks->{$uplink}" if $uplinks->{$uplink}; | |
182 | $interface->{name} = $id; | |
183 | $uplinks->{$interface->{'uplink-id'}} = $interface; | |
184 | } | |
185 | } | |
186 | ||
87d8b623 AD |
187 | #generate configuration |
188 | my $config = {}; | |
189 | ||
8fb1ee7f AD |
190 | foreach my $id (keys %{$sdn_cfg->{ids}}) { |
191 | my $plugin_config = $sdn_cfg->{ids}->{$id}; | |
074d270b | 192 | my $plugin = PVE::Network::SDN::Plugin->lookup($plugin_config->{type}); |
8fb1ee7f AD |
193 | my $pd = $plugin->plugindata(); |
194 | my $role = $pd->{role}; | |
195 | if ($role eq 'controller') { | |
196 | $plugin->generate_controller_config($plugin_config, $plugin_config, $id, $uplinks, $config); | |
197 | } elsif ($role eq 'transport') { | |
198 | my $controllerid = $plugin_config->{controller}; | |
199 | if ($controllerid) { | |
200 | my $controller = $sdn_cfg->{ids}->{$controllerid}; | |
201 | if ($controller) { | |
0589eb09 AD |
202 | my $controller_plugin = PVE::Network::SDN::Plugin->lookup($controller->{type}); |
203 | $controller_plugin->generate_controller_transport_config($plugin_config, $controller, $id, $uplinks, $config); | |
8fb1ee7f | 204 | } |
87d8b623 AD |
205 | } |
206 | } | |
207 | } | |
208 | ||
8fb1ee7f | 209 | return $config; |
17854295 AD |
210 | } |
211 | ||
80348b2d AD |
212 | sub write_etc_network_config { |
213 | my ($rawconfig) = @_; | |
214 | ||
215 | return if !$rawconfig; | |
216 | my $sdn_interfaces_file = "/etc/network/interfaces.d/sdn"; | |
217 | ||
218 | my $writefh = IO::File->new($sdn_interfaces_file,">"); | |
219 | print $writefh $rawconfig; | |
220 | $writefh->close(); | |
221 | } | |
222 | ||
8fb1ee7f AD |
223 | sub write_controller_config { |
224 | my ($config) = @_; | |
ecdd8c12 | 225 | |
8fb1ee7f AD |
226 | my $sdn_cfg = PVE::Cluster::cfs_read_file('sdn.cfg'); |
227 | return if !$sdn_cfg; | |
ecdd8c12 | 228 | |
8fb1ee7f AD |
229 | foreach my $id (keys %{$sdn_cfg->{ids}}) { |
230 | my $plugin_config = $sdn_cfg->{ids}->{$id}; | |
231 | my $plugin = PVE::Network::SDN::Plugin->lookup($plugin_config->{type}); | |
232 | my $pd = $plugin->plugindata(); | |
233 | my $role = $pd->{role}; | |
234 | if ($role eq 'controller') { | |
235 | $plugin->write_controller_config($plugin_config, $config); | |
236 | } | |
237 | } | |
ecdd8c12 AD |
238 | } |
239 | ||
e424c7ac AD |
240 | sub status { |
241 | ||
242 | my $cluster_sdn_file = "/etc/pve/sdn.cfg"; | |
243 | my $local_sdn_file = "/etc/network/interfaces.d/sdn"; | |
244 | my $err_config = undef; | |
245 | ||
246 | return if !-e $cluster_sdn_file; | |
247 | ||
248 | if (!-e $local_sdn_file) { | |
249 | warn "local sdn network configuration is not yet generated, please reload"; | |
250 | $err_config = 'pending'; | |
251 | } else { | |
252 | # fixme : use some kind of versioning info? | |
253 | my $cluster_sdn_timestamp = (stat($cluster_sdn_file))[9]; | |
254 | my $local_sdn_timestamp = (stat($local_sdn_file))[9]; | |
255 | ||
256 | if ($local_sdn_timestamp < $cluster_sdn_timestamp) { | |
257 | warn "local sdn network configuration is too old, please reload"; | |
258 | $err_config = 'unknown'; | |
259 | } | |
260 | } | |
261 | ||
262 | my $status = ifquery_check(); | |
80348b2d | 263 | |
e424c7ac AD |
264 | my $network_cfg = PVE::Cluster::cfs_read_file('sdn.cfg'); |
265 | my $vnet_cfg = undef; | |
266 | my $transport_cfg = undef; | |
267 | ||
268 | my $vnet_status = {}; | |
269 | my $transport_status = {}; | |
270 | ||
271 | foreach my $id (keys %{$network_cfg->{ids}}) { | |
272 | if ($network_cfg->{ids}->{$id}->{type} eq 'vnet') { | |
273 | my $transportzone = $network_cfg->{ids}->{$id}->{transportzone}; | |
274 | $vnet_status->{$id}->{transportzone} = $transportzone; | |
275 | $transport_status->{$transportzone}->{status} = 'available' if !defined($transport_status->{$transportzone}->{status}); | |
276 | ||
277 | if($err_config) { | |
278 | $vnet_status->{$id}->{status} = $err_config; | |
279 | $transport_status->{$transportzone}->{status} = $err_config; | |
280 | } elsif ($status->{$id}->{status} && $status->{$id}->{status} eq 'pass') { | |
281 | $vnet_status->{$id}->{status} = 'available'; | |
282 | my $bridgeport = $status->{$id}->{config}->{'bridge-ports'}; | |
283 | ||
284 | if ($status->{$bridgeport}->{status} && $status->{$bridgeport}->{status} ne 'pass') { | |
285 | $vnet_status->{$id}->{status} = 'error'; | |
286 | $transport_status->{$transportzone}->{status} = 'error'; | |
287 | } | |
288 | } else { | |
289 | $vnet_status->{$id}->{status} = 'error'; | |
290 | $transport_status->{$transportzone}->{status} = 'error'; | |
291 | } | |
292 | } | |
293 | } | |
294 | return($transport_status, $vnet_status); | |
295 | } | |
296 | ||
297 | 1; | |
80348b2d | 298 |