1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
package PVE::Network::SDN;
use strict;
use warnings;
use HTTP::Request;
use IO::Socket::SSL; # important for SSL_verify_callback
use JSON qw(decode_json from_json to_json);
use LWP::UserAgent;
use Net::SSLeay;
use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
use PVE::INotify;
use PVE::RESTEnvironment qw(log_warn);
use PVE::RPCEnvironment;
use PVE::Tools qw(extract_param dir_glob_regex run_command);
use PVE::Network::SDN::Vnets;
use PVE::Network::SDN::Zones;
use PVE::Network::SDN::Controllers;
use PVE::Network::SDN::Subnets;
use PVE::Network::SDN::Dhcp;
use PVE::Network::SDN::Frr;
use PVE::Network::SDN::Fabrics;
my $running_cfg = "sdn/.running-config";
my $parse_running_cfg = sub {
my ($filename, $raw) = @_;
my $cfg = {};
return $cfg if !defined($raw) || $raw eq '';
eval { $cfg = from_json($raw); };
return {} if $@;
return $cfg;
};
my $write_running_cfg = sub {
my ($filename, $cfg) = @_;
my $json = to_json($cfg);
return $json;
};
PVE::Cluster::cfs_register_file($running_cfg, $parse_running_cfg, $write_running_cfg);
# improve me : move status code inside plugins ?
sub ifquery_check {
my $cmd = ['ifquery', '-a', '-c', '-o', 'json'];
my $result = '';
my $reader = sub { $result .= shift };
eval { run_command($cmd, outfunc => $reader); };
my $resultjson = decode_json($result);
my $interfaces = {};
foreach my $interface (@$resultjson) {
my $name = $interface->{name};
$interfaces->{$name} = {
status => $interface->{status},
config => $interface->{config},
config_status => $interface->{config_status},
};
}
return $interfaces;
}
sub status {
my ($zone_status, $vnet_status) = PVE::Network::SDN::Zones::status();
return ($zone_status, $vnet_status);
}
sub running_config {
return cfs_read_file($running_cfg);
}
=head3 running_config_has_frr(\%running_config)
Determines whether C<\%running_config> contains any entities that generate an
FRR configuration. This is used by pve-manager to determine whether a rewrite of
the FRR configuration is required or not.
If C<\%running_config> is not provided, it will query the current running
configuration and then evaluate it.
=cut
sub running_config_has_frr {
my $running_config = PVE::Network::SDN::running_config();
# both can be empty if the SDN configuration was never applied
my $controllers = $running_config->{controllers}->{ids} // {};
my $fabrics = $running_config->{fabrics}->{ids} // {};
return %$controllers || %$fabrics;
}
sub pending_config {
my ($running_cfg, $cfg, $type) = @_;
my $pending = {};
my $running_objects = $running_cfg->{$type}->{ids};
my $config_objects = $cfg->{ids};
foreach my $id (sort keys %{$running_objects}) {
my $running_object = $running_objects->{$id};
my $config_object = $config_objects->{$id};
foreach my $key (sort keys %{$running_object}) {
$pending->{$id}->{$key} = $running_object->{$key};
if (!keys %{$config_object}) {
$pending->{$id}->{state} = "deleted";
} elsif (!defined($config_object->{$key})) {
$pending->{$id}->{"pending"}->{$key} = 'deleted';
$pending->{$id}->{state} = "changed";
} elsif (PVE::Network::SDN::encode_value(undef, $key, $running_object->{$key}) ne
PVE::Network::SDN::encode_value(undef, $key, $config_object->{$key})
) {
$pending->{$id}->{state} = "changed";
}
}
$pending->{$id}->{"pending"} = {}
if $pending->{$id}->{state} && !defined($pending->{$id}->{"pending"});
}
foreach my $id (sort keys %{$config_objects}) {
my $running_object = $running_objects->{$id};
my $config_object = $config_objects->{$id};
foreach my $key (sort keys %{$config_object}) {
my $config_value = PVE::Network::SDN::encode_value(undef, $key, $config_object->{$key});
my $running_value =
PVE::Network::SDN::encode_value(undef, $key, $running_object->{$key});
if ($key eq 'type' || $key eq 'vnet') {
$pending->{$id}->{$key} = $config_value;
} else {
$pending->{$id}->{"pending"}->{$key} = $config_object->{$key}
if !defined($running_value)
|| ($config_value ne $running_value);
}
if (!keys %{$running_object}) {
$pending->{$id}->{state} = "new";
} elsif (!defined($running_value) && defined($config_value)) {
$pending->{$id}->{state} = "changed";
}
}
$pending->{$id}->{"pending"} = {}
if $pending->{$id}->{state} && !defined($pending->{$id}->{"pending"});
}
return { ids => $pending };
}
sub commit_config {
my $cfg = cfs_read_file($running_cfg);
my $version = $cfg->{version};
if ($version) {
$version++;
} else {
$version = 1;
}
my $vnets_cfg = PVE::Network::SDN::Vnets::config();
my $zones_cfg = PVE::Network::SDN::Zones::config();
my $controllers_cfg = PVE::Network::SDN::Controllers::config();
my $subnets_cfg = PVE::Network::SDN::Subnets::config();
my $fabrics_cfg = PVE::Network::SDN::Fabrics::config();
my $vnets = { ids => $vnets_cfg->{ids} };
my $zones = { ids => $zones_cfg->{ids} };
my $controllers = { ids => $controllers_cfg->{ids} };
my $subnets = { ids => $subnets_cfg->{ids} };
my $fabrics = { ids => $fabrics_cfg->to_sections() };
$cfg = {
version => $version,
vnets => $vnets,
zones => $zones,
controllers => $controllers,
subnets => $subnets,
fabrics => $fabrics,
};
cfs_write_file($running_cfg, $cfg);
}
sub lock_sdn_config {
my ($code, $errmsg) = @_;
cfs_lock_file($running_cfg, undef, $code);
if (my $err = $@) {
$errmsg ? die "$errmsg: $err" : die $err;
}
}
sub get_local_vnets {
my $rpcenv = PVE::RPCEnvironment::get();
my $authuser = $rpcenv->get_user();
my $nodename = PVE::INotify::nodename();
my $cfg = PVE::Network::SDN::running_config();
my $vnets_cfg = $cfg->{vnets};
my $zones_cfg = $cfg->{zones};
my @vnetids = PVE::Network::SDN::Vnets::sdn_vnets_ids($vnets_cfg);
my $vnets = {};
foreach my $vnetid (@vnetids) {
my $vnet = PVE::Network::SDN::Vnets::sdn_vnets_config($vnets_cfg, $vnetid);
my $zoneid = $vnet->{zone};
my $comments = $vnet->{alias};
my $privs = ['SDN.Audit', 'SDN.Use'];
next if !$zoneid;
next if !$rpcenv->check_sdn_bridge($authuser, $zoneid, $vnetid, $privs, 1);
my $zone_config = PVE::Network::SDN::Zones::sdn_zones_config($zones_cfg, $zoneid);
next if defined($zone_config->{nodes}) && !$zone_config->{nodes}->{$nodename};
my $ipam = $zone_config->{ipam} ? 1 : 0;
my $vlanaware = $vnet->{vlanaware} ? 1 : 0;
$vnets->{$vnetid} = {
type => 'vnet',
active => '1',
ipam => $ipam,
vlanaware => $vlanaware,
comments => $comments,
};
}
return $vnets;
}
sub generate_zone_config {
my $raw_config = PVE::Network::SDN::Zones::generate_etc_network_config();
if ($raw_config) {
eval {
my $net_cfg = PVE::INotify::read_file('interfaces', 1);
my $opts = $net_cfg->{data}->{options};
log_warn(
"missing 'source /etc/network/interfaces.d/sdn' directive for SDN support!\n")
if !grep { $_->[1] =~ m!^source /etc/network/interfaces.d/(:?sdn|\*)! } @$opts;
};
log_warn("Failed to read network interfaces definition - $@") if $@;
}
PVE::Network::SDN::Zones::write_etc_network_config($raw_config);
}
=head3 generate_frr_raw_config(\%running_config, \%fabric_config)
Generates the raw frr config (as documented in the C<PVE::Network::SDN::Frr>
module) for all SDN plugins combined.
If provided, uses the passed C<\%running_config> und C<\%fabric_config> to avoid
re-parsing and re-reading both configurations. If not provided, this function
will obtain them via the SDN and SDN::Fabrics modules and then generate the FRR
configuration.
=cut
sub generate_frr_raw_config {
my ($running_config, $fabric_config) = @_;
$running_config = PVE::Network::SDN::running_config() if !$running_config;
$fabric_config = PVE::Network::SDN::Fabrics::config(1) if !$fabric_config;
my $frr_config = {};
PVE::Network::SDN::Controllers::generate_frr_config($frr_config, $running_config);
PVE::Network::SDN::Frr::append_local_config($frr_config);
my $raw_config = PVE::Network::SDN::Frr::to_raw_config($frr_config);
my $fabrics_config = PVE::Network::SDN::Fabrics::generate_frr_raw_config($fabric_config);
push @$raw_config, @$fabrics_config;
return $raw_config;
}
=head3 get_frr_daemon_status(\%fabric_config)
Returns a hash that indicates which FRR daemons, that are managed by SDN, should
be enabled / disabled.
=cut
sub get_frr_daemon_status {
my ($fabric_config) = @_;
return PVE::Network::SDN::Fabrics::get_frr_daemon_status($fabric_config);
}
sub generate_frr_config {
my ($apply) = @_;
my $running_config = PVE::Network::SDN::running_config();
my $fabric_config = PVE::Network::SDN::Fabrics::config(1);
my $daemon_status = PVE::Network::SDN::get_frr_daemon_status($fabric_config);
my $needs_restart = PVE::Network::SDN::Frr::set_daemon_status($daemon_status, 1);
my $raw_config = PVE::Network::SDN::generate_frr_raw_config($running_config, $fabric_config);
PVE::Network::SDN::Frr::write_raw_config($raw_config);
PVE::Network::SDN::Frr::apply($needs_restart) if $apply;
}
sub generate_dhcp_config {
my ($reload) = @_;
PVE::Network::SDN::Dhcp::regenerate_config($reload);
}
sub encode_value {
my ($type, $key, $value) = @_;
if ($key eq 'nodes' || $key eq 'exitnodes' || $key eq 'dhcp-range') {
if (ref($value) eq 'HASH') {
return join(',', sort keys(%$value));
} elsif (ref($value) eq 'ARRAY') {
return join(',', sort @$value);
} else {
return $value;
}
}
return $value;
}
#helpers
sub api_request {
my ($method, $url, $headers, $data, $expected_fingerprint) = @_;
my $encoded_data = $data ? to_json($data) : undef;
my $req = HTTP::Request->new($method, $url, $headers, $encoded_data);
my $ua = LWP::UserAgent->new(protocols_allowed => ['http', 'https'], timeout => 30);
my $datacenter_cfg = PVE::Cluster::cfs_read_file('datacenter.cfg');
if (my $proxy = $datacenter_cfg->{http_proxy}) {
$ua->proxy(['http', 'https'], $proxy);
} else {
$ua->env_proxy;
}
if (defined($expected_fingerprint)) {
my $ssl_verify_callback = sub {
my (undef, undef, undef, undef, $cert, $depth) = @_;
# we don't care about intermediate or root certificates, always return as valid as the
# callback will be executed for all levels and all must be valid.
return 1 if $depth != 0;
my $fingerprint = Net::SSLeay::X509_get_fingerprint($cert, 'sha256');
return $fingerprint eq $expected_fingerprint ? 1 : 0;
};
$ua->ssl_opts(
verify_hostname => 0,
SSL_verify_mode => SSL_VERIFY_PEER,
SSL_verify_callback => $ssl_verify_callback,
);
}
my $response = $ua->request($req);
if (!$response->is_success) {
my $msg = $response->message || 'unknown';
my $code = $response->code;
die "Invalid response from server: $code $msg\n";
}
my $raw = '';
if (defined($response->decoded_content)) {
$raw = $response->decoded_content;
} else {
$raw = $response->content;
}
return if $raw eq '';
my $res = eval { from_json($raw) };
die "api response is not a json" if $@;
return $res;
}
1;
|