]> git.proxmox.com Git - pve-network.git/blame_incremental - PVE/Network/SDN/Controllers/EvpnPlugin.pm
evpn controller: style fixes
[pve-network.git] / PVE / Network / SDN / Controllers / EvpnPlugin.pm
... / ...
CommitLineData
1package PVE::Network::SDN::Controllers::EvpnPlugin;
2
3use strict;
4use warnings;
5
6use PVE::INotify;
7use PVE::JSONSchema qw(get_standard_option);
8use PVE::Tools qw(run_command file_set_contents file_get_contents);
9
10use PVE::Network::SDN::Controllers::Plugin;
11use PVE::Network::SDN::Zones::Plugin;
12use Net::IP;
13
14use base('PVE::Network::SDN::Controllers::Plugin');
15
16sub type {
17 return 'evpn';
18}
19
20sub properties {
21 return {
22 asn => {
23 type => 'integer',
24 description => "autonomous system number",
25 minimum => 0,
26 maximum => 4294967296
27 },
28 peers => {
29 description => "peers address list.",
30 type => 'string', format => 'ip-list'
31 },
32 };
33}
34
35sub options {
36 return {
37 'asn' => { optional => 0 },
38 'peers' => { optional => 0 },
39 };
40}
41
42# Plugin implementation
43sub generate_controller_config {
44 my ($class, $plugin_config, $controller_cfg, $id, $uplinks, $config) = @_;
45
46 my @peers;
47 @peers = PVE::Tools::split_list($plugin_config->{'peers'}) if $plugin_config->{'peers'};
48
49 my $local_node = PVE::INotify::nodename();
50
51 my $asn = $plugin_config->{asn};
52 my $ebgp = undef;
53 my $loopback = undef;
54 my $autortas = undef;
55 my $bgprouter = find_bgp_controller($local_node, $controller_cfg);
56 if ($bgprouter) {
57 $ebgp = 1 if $plugin_config->{'asn'} ne $bgprouter->{asn};
58 $loopback = $bgprouter->{loopback} if $bgprouter->{loopback};
59 $asn = $bgprouter->{asn} if $bgprouter->{asn};
60 $autortas = $plugin_config->{'asn'} if $ebgp;
61 }
62
63 return if !$asn;
64
65 my $bgp = $config->{frr}->{router}->{"bgp $asn"} //= {};
66
67 my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers, $loopback);
68
69 my $remoteas = $ebgp ? "external" : $asn;
70
71 #global options
72 my @controller_config = (
73 "bgp router-id $ifaceip",
74 "no bgp default ipv4-unicast",
75 "coalesce-time 1000",
76 );
77
78 push(@{$bgp->{""}}, @controller_config) if keys %{$bgp} == 0;
79
80 @controller_config = ();
81
82 #VTEP neighbors
83 push @controller_config, "neighbor VTEP peer-group";
84 push @controller_config, "neighbor VTEP remote-as $remoteas";
85 push @controller_config, "neighbor VTEP bfd";
86
87 if($ebgp && $loopback) {
88 push @controller_config, "neighbor VTEP ebgp-multihop 10";
89 push @controller_config, "neighbor VTEP update-source $loopback";
90 }
91
92 # VTEP peers
93 foreach my $address (@peers) {
94 next if $address eq $ifaceip;
95 push @controller_config, "neighbor $address peer-group VTEP";
96 }
97
98 push(@{$bgp->{""}}, @controller_config);
99
100 # address-family l2vpn
101 @controller_config = ();
102 push @controller_config, "neighbor VTEP route-map MAP_VTEP_IN in";
103 push @controller_config, "neighbor VTEP route-map MAP_VTEP_OUT out";
104 push @controller_config, "neighbor VTEP activate";
105 push @controller_config, "advertise-all-vni";
106 push @controller_config, "autort as $autortas" if $autortas;
107 push(@{$bgp->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
108
109 my $routemap = { rule => undef, action => "permit" };
110 push(@{$config->{frr_routemap}->{'MAP_VTEP_IN'}}, $routemap );
111 push(@{$config->{frr_routemap}->{'MAP_VTEP_OUT'}}, $routemap );
112
113 return $config;
114}
115
116sub generate_controller_zone_config {
117 my ($class, $plugin_config, $controller, $controller_cfg, $id, $uplinks, $config) = @_;
118
119 my $local_node = PVE::INotify::nodename();
120
121 my $vrf = "vrf_$id";
122 my $vrfvxlan = $plugin_config->{'vrf-vxlan'};
123 my $exitnodes = $plugin_config->{'exitnodes'};
124 my $exitnodes_primary = $plugin_config->{'exitnodes-primary'};
125 my $advertisesubnets = $plugin_config->{'advertise-subnets'};
126 my $exitnodes_local_routing = $plugin_config->{'exitnodes-local-routing'};
127 my $rt_import = [PVE::Tools::split_list($plugin_config->{'rt-import'})] if $plugin_config->{'rt-import'};
128
129 my $asn = $controller->{asn};
130 my @peers = PVE::Tools::split_list($controller->{'peers'}) if $controller->{'peers'};
131 my $ebgp = undef;
132 my $loopback = undef;
133 my $autortas = undef;
134 my $bgprouter = find_bgp_controller($local_node, $controller_cfg);
135 if($bgprouter) {
136 $ebgp = 1 if $controller->{'asn'} ne $bgprouter->{asn};
137 $loopback = $bgprouter->{loopback} if $bgprouter->{loopback};
138 $asn = $bgprouter->{asn} if $bgprouter->{asn};
139 $autortas = $controller->{'asn'} if $ebgp;
140 }
141
142 return if !$vrf || !$vrfvxlan || !$asn;
143
144 my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers, $loopback);
145
146 # vrf
147 my @controller_config = ();
148 push @controller_config, "vni $vrfvxlan";
149 push(@{$config->{frr}->{vrf}->{"$vrf"}}, @controller_config);
150
151 #main vrf router
152 @controller_config = ();
153 push @controller_config, "bgp router-id $ifaceip";
154# push @controller_config, "!";
155 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{""}}, @controller_config);
156
157 if ($autortas) {
158 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, "route-target import $autortas:$vrfvxlan");
159 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, "route-target export $autortas:$vrfvxlan");
160 }
161
162 my $is_gateway = $exitnodes->{$local_node};
163
164 if ($is_gateway) {
165
166 if (!$exitnodes_primary || $exitnodes_primary eq $local_node) {
167 #filter default type5 route coming from other exit nodes on primary node or both nodes if no primary is defined.
168 my $routemap_config = ();
169 push @{$routemap_config}, "match evpn route-type prefix";
170 my $routemap = { rule => $routemap_config, action => "deny" };
171 unshift(@{$config->{frr_routemap}->{'MAP_VTEP_IN'}}, $routemap);
172 } elsif ($exitnodes_primary ne $local_node) {
173 my $routemap_config = ();
174 push @{$routemap_config}, "match evpn vni $vrfvxlan";
175 push @{$routemap_config}, "match evpn route-type prefix";
176 push @{$routemap_config}, "set metric 200";
177 my $routemap = { rule => $routemap_config, action => "permit" };
178 unshift(@{$config->{frr_routemap}->{'MAP_VTEP_OUT'}}, $routemap);
179 }
180
181
182 if (!$exitnodes_local_routing) {
183 @controller_config = ();
184 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
185 push @controller_config, "import vrf $vrf";
186 push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
187 push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
188
189 @controller_config = ();
190 #redistribute connected to be able to route to local vms on the gateway
191 push @controller_config, "redistribute connected";
192 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
193 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
194 }
195
196 @controller_config = ();
197 #add default originate to announce 0.0.0.0/0 type5 route in evpn
198 push @controller_config, "default-originate ipv4";
199 push @controller_config, "default-originate ipv6";
200 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
201 } elsif ($advertisesubnets) {
202
203 @controller_config = ();
204 #redistribute connected networks
205 push @controller_config, "redistribute connected";
206 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
207 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
208
209 @controller_config = ();
210 #advertise connected networks type5 route in evpn
211 push @controller_config, "advertise ipv4 unicast";
212 push @controller_config, "advertise ipv6 unicast";
213 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
214 }
215
216 if ($rt_import) {
217 @controller_config = ();
218 foreach my $rt (sort @{$rt_import}) {
219 push @controller_config, "route-target import $rt";
220 }
221 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
222 }
223
224 return $config;
225}
226
227sub generate_controller_vnet_config {
228 my ($class, $plugin_config, $controller, $zone, $zoneid, $vnetid, $config) = @_;
229
230 my $exitnodes = $zone->{'exitnodes'};
231 my $exitnodes_local_routing = $zone->{'exitnodes-local-routing'};
232
233 return if !$exitnodes_local_routing;
234
235 my $local_node = PVE::INotify::nodename();
236 my $is_gateway = $exitnodes->{$local_node};
237
238 return if !$is_gateway;
239
240 my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid, 1);
241 my @controller_config = ();
242 foreach my $subnetid (sort keys %{$subnets}) {
243 my $subnet = $subnets->{$subnetid};
244 my $cidr = $subnet->{cidr};
245 push @controller_config, "ip route $cidr 10.255.255.2 xvrf_$zoneid";
246 }
247 push(@{$config->{frr}->{''}}, @controller_config);
248}
249
250sub on_delete_hook {
251 my ($class, $controllerid, $zone_cfg) = @_;
252
253 # verify that zone is associated to this controller
254 foreach my $id (keys %{$zone_cfg->{ids}}) {
255 my $zone = $zone_cfg->{ids}->{$id};
256 die "controller $controllerid is used by $id"
257 if (defined($zone->{controller}) && $zone->{controller} eq $controllerid);
258 }
259}
260
261sub on_update_hook {
262 my ($class, $controllerid, $controller_cfg) = @_;
263
264 # we can only have 1 evpn controller / 1 asn by server
265
266 my $controllernb = 0;
267 foreach my $id (keys %{$controller_cfg->{ids}}) {
268 next if $id eq $controllerid;
269 my $controller = $controller_cfg->{ids}->{$id};
270 next if $controller->{type} ne "evpn";
271 $controllernb++;
272 die "only 1 global evpn controller can be defined" if $controllernb > 1;
273 }
274}
275
276sub find_bgp_controller {
277 my ($nodename, $controller_cfg) = @_;
278
279 my $controller = undef;
280 foreach my $id (keys %{$controller_cfg->{ids}}) {
281 $controller = $controller_cfg->{ids}->{$id};
282 next if $controller->{type} ne 'bgp';
283 next if $controller->{node} ne $nodename;
284 last;
285 }
286
287 return $controller;
288}
289
290
291sub sort_frr_config {
292 my $order = {};
293 $order->{''} = 0;
294 $order->{'vrf'} = 1;
295 $order->{'ipv4 unicast'} = 1;
296 $order->{'ipv6 unicast'} = 2;
297 $order->{'l2vpn evpn'} = 3;
298
299 my $a_val = 100;
300 my $b_val = 100;
301
302 $a_val = $order->{$a} if defined($order->{$a});
303 $b_val = $order->{$b} if defined($order->{$b});
304
305 if ($a =~ /bgp (\d+)$/) {
306 $a_val = 2;
307 }
308
309 if ($b =~ /bgp (\d+)$/) {
310 $b_val = 2;
311 }
312
313 return $a_val <=> $b_val;
314}
315
316sub generate_frr_recurse{
317 my ($final_config, $content, $parentkey, $level) = @_;
318
319 my $keylist = {};
320 $keylist->{vrf} = 1;
321 $keylist->{'address-family'} = 1;
322 $keylist->{router} = 1;
323
324 my $exitkeylist = {};
325 $exitkeylist->{vrf} = 1;
326 $exitkeylist->{'address-family'} = 1;
327
328 my $simple_exitkeylist = {};
329 $simple_exitkeylist->{router} = 1;
330
331 # FIXME: make this generic
332 my $paddinglevel = undef;
333 if ($level == 1 || $level == 2) {
334 $paddinglevel = $level - 1;
335 } elsif ($level == 3 || $level == 4) {
336 $paddinglevel = $level - 2;
337 }
338
339 my $padding = "";
340 $padding = ' ' x ($paddinglevel) if $paddinglevel;
341
342 if (ref $content eq 'HASH') {
343 foreach my $key (sort sort_frr_config keys %$content) {
344 if ($parentkey && defined($keylist->{$parentkey})) {
345 push @{$final_config}, $padding."!";
346 push @{$final_config}, $padding."$parentkey $key";
347 } elsif ($key ne '' && !defined($keylist->{$key})) {
348 push @{$final_config}, $padding."$key";
349 }
350
351 my $option = $content->{$key};
352 generate_frr_recurse($final_config, $option, $key, $level+1);
353
354 push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
355 push @{$final_config}, $padding."exit" if $parentkey && defined($simple_exitkeylist->{$parentkey});
356 }
357 }
358
359 if (ref $content eq 'ARRAY') {
360 push @{$final_config}, map { $padding . "$_" } @$content;
361 }
362}
363
364sub generate_frr_routemap {
365 my ($final_config, $routemaps) = @_;
366
367 foreach my $id (sort keys %$routemaps) {
368
369 my $routemap = $routemaps->{$id};
370 my $order = 0;
371 foreach my $seq (@$routemap) {
372 $order++;
373 next if !defined($seq->{action});
374 my @config = ();
375 push @config, "!";
376 push @config, "route-map $id $seq->{action} $order";
377 my $rule = $seq->{rule};
378 push @config, map { " $_" } @$rule;
379 push @{$final_config}, @config;
380 push @{$final_config}, "exit";
381 }
382 }
383}
384
385sub generate_frr_accesslist {
386 my ($final_config, $accesslists) = @_;
387
388 my $config = [];
389
390 for my $id (sort keys %$accesslists) {
391 my $accesslist = $accesslists->{$id};
392
393 for my $seq (sort keys %$accesslist) {
394 my $rule = $accesslist->{$seq};
395 push @$config, "access-list $id seq $seq $rule";
396 }
397 }
398
399 if (@$config > 0) {
400 push @{$final_config}, "!", @$config;
401 }
402}
403
404sub generate_controller_rawconfig {
405 my ($class, $plugin_config, $config) = @_;
406
407 my $nodename = PVE::INotify::nodename();
408
409 my $final_config = [];
410 push @{$final_config}, "frr version 8.2.2";
411 push @{$final_config}, "frr defaults datacenter";
412 push @{$final_config}, "hostname $nodename";
413 push @{$final_config}, "log syslog informational";
414 push @{$final_config}, "service integrated-vtysh-config";
415 push @{$final_config}, "!";
416
417 if (-e "/etc/frr/frr.conf.local") {
418 my $local_conf = file_get_contents("/etc/frr/frr.conf.local");
419 parse_merge_frr_local_config($config, $local_conf);
420 }
421
422 generate_frr_recurse($final_config, $config->{frr}, undef, 0);
423 generate_frr_accesslist($final_config, $config->{frr_access_list});
424 generate_frr_routemap($final_config, $config->{frr_routemap});
425
426 push @{$final_config}, "!";
427 push @{$final_config}, "line vty";
428 push @{$final_config}, "!";
429
430 my $rawconfig = join("\n", @{$final_config});
431
432 return if !$rawconfig;
433 return $rawconfig;
434}
435
436sub parse_merge_frr_local_config {
437 my ($config, $local_conf) = @_;
438
439 my $section = \$config->{""};
440 my $router = undef;
441 my $routemap = undef;
442 my $routemap_config = ();
443 my $routemap_action = undef;
444
445 while ($local_conf =~ /^\s*(.+?)\s*$/gm) {
446 my $line = $1;
447 $line =~ s/^\s+|\s+$//g;
448
449 if ($line =~ m/^router (.+)$/) {
450 $router = $1;
451 $section = \$config->{'frr'}->{'router'}->{$router}->{""};
452 next;
453 } elsif ($line =~ m/^vrf (.+)$/) {
454 $section = \$config->{'frr'}->{'vrf'}->{$1};
455 next;
456 } elsif ($line =~ m/address-family (.+)$/) {
457 $section = \$config->{'frr'}->{'router'}->{$router}->{'address-family'}->{$1};
458 next;
459 } elsif ($line =~ m/^route-map (.+) (permit|deny) (\d+)/) {
460 $routemap = $1;
461 $routemap_config = ();
462 $routemap_action = $2;
463 $section = \$config->{'frr_routemap'}->{$routemap};
464 next;
465 } elsif ($line =~ m/^access-list (.+) seq (\d+) (.+)$/) {
466 $config->{'frr_access_list'}->{$1}->{$2} = $3;
467 next;
468 } elsif($line =~ m/^exit-address-family$/) {
469 next;
470 } elsif($line =~ m/^exit$/) {
471 if($router) {
472 $section = \$config->{''};
473 $router = undef;
474 } elsif($routemap) {
475 push(@{$$section}, { rule => $routemap_config, action => $routemap_action });
476 $section = \$config->{''};
477 $routemap = undef;
478 $routemap_action = undef;
479 $routemap_config = ();
480 }
481 next;
482 } elsif($line =~ m/!/) {
483 next;
484 }
485
486 next if !$section;
487 if($routemap) {
488 push(@{$routemap_config}, $line);
489 } else {
490 push(@{$$section}, $line);
491 }
492 }
493}
494
495sub write_controller_config {
496 my ($class, $plugin_config, $config) = @_;
497
498 my $rawconfig = $class->generate_controller_rawconfig($plugin_config, $config);
499 return if !$rawconfig;
500 return if !-d "/etc/frr";
501
502 file_set_contents("/etc/frr/frr.conf", $rawconfig);
503}
504
505sub reload_controller {
506 my ($class) = @_;
507
508 my $conf_file = "/etc/frr/frr.conf";
509 my $bin_path = "/usr/lib/frr/frr-reload.py";
510
511 if (!-e $bin_path) {
512 warn "missing $bin_path. Please install frr-pythontools package";
513 return;
514 }
515
516 my $err = sub {
517 my $line = shift;
518 if ($line =~ /ERROR:/) {
519 warn "$line \n";
520 }
521 };
522
523 if (-e $conf_file && -e $bin_path) {
524 eval {
525 run_command([$bin_path, '--stdout', '--reload', $conf_file], outfunc => {}, errfunc => $err);
526 };
527 if ($@) {
528 warn "frr reload command fail. Restarting frr.";
529 eval { run_command(['systemctl', 'restart', 'frr']); };
530 }
531 }
532}
533
5341;
535
536