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