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
|
package PVE::Network::SDN::Frr;
use strict;
use warnings;
=head1 NAME
C<PVE::Network::SDN::Frr> - Helper module for FRR
=head1 DESCRIPTION
This module contains helpers for handling the various intermediate FRR
configuration formats.
We currently mainly use two different intermediate formats throughout the SDN
module:
=head2 frr config
An frr config represented as a perl hash. The controller plugins generate their
frr configuration in this format. This format is also used for merging the local
FRR config (a user-defined configuration file) with the controller-generated
configuration.
=head2 raw config
This is generated from the frr config. It is an array where every entry is a
string that is a FRR configuration line.
=cut
use PVE::RESTEnvironment qw(log_warn);
use PVE::Tools qw(file_get_contents file_set_contents run_command);
=head3 read_local_frr_config
Returns the contents of `/etc/frr/frr.conf.local` as a string if it exists, otherwise undef.
=cut
sub read_local_frr_config {
if (-e "/etc/frr/frr.conf.local") {
return file_get_contents("/etc/frr/frr.conf.local");
}
}
my $FRR_CONFIG_FILE = "/etc/frr/frr.conf";
=head3 apply()
Tries to reload FRR with the frr-reload.py script from frr-pythontools. If that
isn't installed or doesn't work it falls back to restarting the systemd frr
service. If C<$force_restart> is set, then the FRR daemon will be restarted,
without trying to reload it first.
=cut
sub apply {
my ($force_restart) = @_;
if (!-e $FRR_CONFIG_FILE) {
log_warn("$FRR_CONFIG_FILE is not present.");
return;
}
run_command(['systemctl', 'enable', '--now', 'frr'])
if !-e "/etc/systemd/system/multi-user.target.wants/frr.service";
if (!$force_restart) {
eval { reload() };
return if !$@;
warn "reloading frr configuration failed: $@";
warn "trying to restart frr instead";
}
eval { restart() };
warn "restarting frr failed: $@" if $@;
}
sub reload {
my $bin_path = "/usr/lib/frr/frr-reload.py";
if (!-e $bin_path) {
die "missing $bin_path. Please install the frr-pythontools package";
}
my $err = sub {
my $line = shift;
warn "$line \n";
};
run_command([$bin_path, '--stdout', '--reload', $FRR_CONFIG_FILE], errfunc => $err);
}
sub restart {
# script invoked by the frr systemd service
my $bin_path = "/usr/lib/frr/frrinit.sh";
if (!-e $bin_path) {
die "missing $bin_path. Please install the frr package";
}
my $err = sub {
my $line = shift;
warn "$line \n";
};
run_command(['systemctl', 'restart', 'frr'], errfunc => $err);
}
=head3 to_raw_config(\%frr_config)
Converts a given C<\%frr_config> to the raw config format.
=cut
sub to_raw_config {
my ($frr_config) = @_;
my $raw_config = [];
generate_frr_vrf($raw_config, $frr_config->{frr}->{vrf});
generate_frr_interfaces($raw_config, $frr_config->{frr_interfaces});
generate_frr_recurse($raw_config, $frr_config->{frr}, undef, 0);
generate_frr_list($raw_config, $frr_config->{frr_access_list}, "access-list");
generate_frr_list($raw_config, $frr_config->{frr_prefix_list}, "ip prefix-list");
generate_frr_list($raw_config, $frr_config->{frr_prefix_list_v6}, "ipv6 prefix-list");
generate_frr_simple_list($raw_config, $frr_config->{frr_bgp_community_list});
generate_frr_routemap($raw_config, $frr_config->{frr_routemap});
generate_frr_simple_list($raw_config, $frr_config->{frr_ip_protocol});
return $raw_config;
}
=head3 raw_config_to_string(\@raw_config)
Converts a given C<\@raw_config> to a string representing a complete frr
configuration, ready to be written to /etc/frr/frr.conf. If raw_config is empty,
returns only the FRR config skeleton.
=cut
sub raw_config_to_string {
my ($raw_config) = @_;
my $nodename = PVE::INotify::nodename();
my @final_config = (
"frr version 8.5.2",
"frr defaults datacenter",
"hostname $nodename",
"log syslog informational",
"service integrated-vtysh-config",
"!",
);
push @final_config, @$raw_config;
push @final_config, (
"!", "line vty", "!",
);
return join("\n", @final_config);
}
=head3 raw_config_to_string(\@raw_config)
Writes a given C<\@raw_config> to /etc/frr/frr.conf.
=cut
sub write_raw_config {
my ($raw_config) = @_;
return if !-d "/etc/frr";
return if !$raw_config;
file_set_contents("/etc/frr/frr.conf", raw_config_to_string($raw_config));
}
=head3 append_local_config(\%frr_config, $local_config)
Takes an existing C<\%frr_config> and C<$local_config> (as a string). It parses
the local configuration and appends the values to the existing C<\%frr_config>
in-place.
=cut
sub append_local_config {
my ($frr_config, $local_config) = @_;
$local_config = read_local_frr_config() if !$local_config;
return if !$local_config;
my $section = \$frr_config->{""};
my $router = undef;
my $routemap = undef;
my $routemap_config = ();
my $routemap_action = undef;
while ($local_config =~ /^\s*(.+?)\s*$/gm) {
my $line = $1;
$line =~ s/^\s+|\s+$//g;
if ($line =~ m/^router (.+)$/) {
$router = $1;
$section = \$frr_config->{'frr'}->{'router'}->{$router}->{""};
next;
} elsif ($line =~ m/^vrf (.+)$/) {
$section = \$frr_config->{'frr'}->{'vrf'}->{$1};
next;
} elsif ($line =~ m/^interface (.+)$/) {
$section = \$frr_config->{'frr_interfaces'}->{$1};
next;
} elsif ($line =~ m/^bgp community-list (.+)$/) {
push(@{ $frr_config->{'frr_bgp_community_list'} }, $line);
next;
} elsif ($line =~ m/address-family (.+)$/) {
$section = \$frr_config->{'frr'}->{'router'}->{$router}->{'address-family'}->{$1};
next;
} elsif ($line =~ m/^route-map (.+) (permit|deny) (\d+)/) {
$routemap = $1;
$routemap_config = ();
$routemap_action = $2;
$section = \$frr_config->{'frr_routemap'}->{$routemap};
next;
} elsif ($line =~ m/^access-list (.+) seq (\d+) (.+)$/) {
$frr_config->{'frr_access_list'}->{$1}->{$2} = $3;
next;
} elsif ($line =~ m/^ip prefix-list (.+) seq (\d+) (.*)$/) {
$frr_config->{'frr_prefix_list'}->{$1}->{$2} = $3;
next;
} elsif ($line =~ m/^ipv6 prefix-list (.+) seq (\d+) (.*)$/) {
$frr_config->{'frr_prefix_list_v6'}->{$1}->{$2} = $3;
next;
} elsif ($line =~ m/^exit-address-family$/) {
next;
} elsif ($line =~ m/^exit$/) {
if ($router) {
$section = \$frr_config->{''};
$router = undef;
} elsif ($routemap) {
push(@{$$section}, { rule => $routemap_config, action => $routemap_action });
$section = \$frr_config->{''};
$routemap = undef;
$routemap_action = undef;
$routemap_config = ();
}
next;
} elsif ($line =~ m/!/) {
next;
}
next if !$section;
if ($routemap) {
push(@{$routemap_config}, $line);
} else {
push(@{$$section}, $line);
}
}
}
sub generate_frr_recurse {
my ($final_config, $content, $parentkey, $level) = @_;
my $keylist = {};
$keylist->{'address-family'} = 1;
$keylist->{router} = 1;
my $exitkeylist = {};
$exitkeylist->{'address-family'} = 1;
my $simple_exitkeylist = {};
$simple_exitkeylist->{router} = 1;
# FIXME: make this generic
my $paddinglevel = undef;
if ($level == 1 || $level == 2) {
$paddinglevel = $level - 1;
} elsif ($level == 3 || $level == 4) {
$paddinglevel = $level - 2;
}
my $padding = "";
$padding = ' ' x ($paddinglevel) if $paddinglevel;
if (ref $content eq 'HASH') {
foreach my $key (sort keys %$content) {
next if $key eq 'vrf';
if ($parentkey && defined($keylist->{$parentkey})) {
push @{$final_config}, $padding . "!";
push @{$final_config}, $padding . "$parentkey $key";
} elsif ($key ne '' && !defined($keylist->{$key})) {
push @{$final_config}, $padding . "$key";
}
my $option = $content->{$key};
generate_frr_recurse($final_config, $option, $key, $level + 1);
push @{$final_config}, $padding . "exit-$parentkey"
if $parentkey && defined($exitkeylist->{$parentkey});
push @{$final_config}, $padding . "exit"
if $parentkey && defined($simple_exitkeylist->{$parentkey});
}
}
if (ref $content eq 'ARRAY') {
push @{$final_config}, map { $padding . "$_" } @$content;
}
}
sub generate_frr_vrf {
my ($final_config, $vrfs) = @_;
return if !$vrfs;
my @config = ();
foreach my $id (sort keys %$vrfs) {
my $vrf = $vrfs->{$id};
push @config, "!";
push @config, "vrf $id";
foreach my $rule (@$vrf) {
push @config, " $rule";
}
push @config, "exit-vrf";
}
push @{$final_config}, @config;
}
sub generate_frr_simple_list {
my ($final_config, $rules) = @_;
return if !$rules;
my @config = ();
push @{$final_config}, "!";
foreach my $rule (sort @$rules) {
push @{$final_config}, $rule;
}
}
sub generate_frr_list {
my ($final_config, $lists, $type) = @_;
my $config = [];
for my $id (sort keys %$lists) {
my $list = $lists->{$id};
for my $seq (sort keys %$list) {
my $rule = $list->{$seq};
push @$config, "$type $id seq $seq $rule";
}
}
if (@$config > 0) {
push @{$final_config}, "!", @$config;
}
}
sub generate_frr_interfaces {
my ($final_config, $interfaces) = @_;
foreach my $k (sort keys %$interfaces) {
my $iface = $interfaces->{$k};
push @{$final_config}, "!";
push @{$final_config}, "interface $k";
foreach my $rule (sort @$iface) {
push @{$final_config}, " $rule";
}
}
}
sub generate_frr_routemap {
my ($final_config, $routemaps) = @_;
foreach my $id (sort keys %$routemaps) {
my $routemap = $routemaps->{$id};
my $order = 0;
foreach my $seq (@$routemap) {
$order++;
next if !defined($seq->{action});
my @config = ();
push @config, "!";
push @config, "route-map $id $seq->{action} $order";
my $rule = $seq->{rule};
push @config, map { " $_" } @$rule;
push @{$final_config}, @config;
push @{$final_config}, "exit";
}
}
}
1;
|