]> git.proxmox.com Git - pve-network.git/blame - PVE/Network/SDN/Controllers/EvpnPlugin.pm
evpn controller: avoid declaration in conditional statement
[pve-network.git] / PVE / Network / SDN / Controllers / EvpnPlugin.pm
CommitLineData
fa253735 1package PVE::Network::SDN::Controllers::EvpnPlugin;
32602a38
AD
2
3use strict;
4use warnings;
cdf2c819 5
074d270b
AD
6use PVE::INotify;
7use PVE::JSONSchema qw(get_standard_option);
cdf2c819
TL
8use PVE::Tools qw(run_command file_set_contents file_get_contents);
9
10use PVE::Network::SDN::Controllers::Plugin;
1f543c5f 11use PVE::Network::SDN::Zones::Plugin;
f23633dc 12use Net::IP;
cdf2c819 13
f5eabba0 14use base('PVE::Network::SDN::Controllers::Plugin');
32602a38
AD
15
16sub type {
fa253735 17 return 'evpn';
8fb1ee7f
AD
18}
19
32602a38
AD
20sub properties {
21 return {
92526f0e
TL
22 asn => {
23 type => 'integer',
24 description => "autonomous system number",
9e6b99fd
AD
25 minimum => 0,
26 maximum => 4294967296
92526f0e
TL
27 },
28 peers => {
29 description => "peers address list.",
30 type => 'string', format => 'ip-list'
31 },
32602a38
AD
32 };
33}
34
35sub options {
32602a38 36 return {
92526f0e
TL
37 'asn' => { optional => 0 },
38 'peers' => { optional => 0 },
32602a38
AD
39 };
40}
41
42# Plugin implementation
8fb1ee7f 43sub generate_controller_config {
f23633dc 44 my ($class, $plugin_config, $controller_cfg, $id, $uplinks, $config) = @_;
32602a38 45
3caa7687
FG
46 my @peers;
47 @peers = PVE::Tools::split_list($plugin_config->{'peers'}) if $plugin_config->{'peers'};
32602a38 48
f23633dc
AD
49 my $local_node = PVE::INotify::nodename();
50
074d270b 51 my $asn = $plugin_config->{asn};
f23633dc
AD
52 my $ebgp = undef;
53 my $loopback = undef;
54 my $autortas = undef;
55 my $bgprouter = find_bgp_controller($local_node, $controller_cfg);
359796b0 56 if ($bgprouter) {
f23633dc
AD
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 }
074d270b
AD
62
63 return if !$asn;
32602a38 64
92526f0e
TL
65 my $bgp = $config->{frr}->{router}->{"bgp $asn"} //= {};
66
f23633dc 67 my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers, $loopback);
32602a38 68
f23633dc 69 my $remoteas = $ebgp ? "external" : $asn;
17854295 70
f23633dc 71 #global options
92526f0e
TL
72 my @controller_config = (
73 "bgp router-id $ifaceip",
74 "no bgp default ipv4-unicast",
75 "coalesce-time 1000",
76 );
32602a38 77
f23633dc
AD
78 push(@{$bgp->{""}}, @controller_config) if keys %{$bgp} == 0;
79
80 @controller_config = ();
359796b0 81
f23633dc
AD
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
32602a38
AD
93 foreach my $address (@peers) {
94 next if $address eq $ifaceip;
f23633dc 95 push @controller_config, "neighbor $address peer-group VTEP";
7d35eaf5 96 }
074d270b 97
92526f0e 98 push(@{$bgp->{""}}, @controller_config);
074d270b 99
f23633dc 100 # address-family l2vpn
56cdcac9 101 @controller_config = ();
916488cc 102 push @controller_config, "neighbor VTEP route-map MAP_VTEP_IN in";
847f5144 103 push @controller_config, "neighbor VTEP route-map MAP_VTEP_OUT out";
f23633dc 104 push @controller_config, "neighbor VTEP activate";
56cdcac9 105 push @controller_config, "advertise-all-vni";
f23633dc 106 push @controller_config, "autort as $autortas" if $autortas;
92526f0e 107 push(@{$bgp->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
32602a38 108
916488cc
AD
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 );
847f5144 112
32602a38
AD
113 return $config;
114}
115
56cdcac9 116sub generate_controller_zone_config {
f23633dc
AD
117 my ($class, $plugin_config, $controller, $controller_cfg, $id, $uplinks, $config) = @_;
118
119 my $local_node = PVE::INotify::nodename();
0589eb09 120
1de0abc0 121 my $vrf = "vrf_$id";
0589eb09 122 my $vrfvxlan = $plugin_config->{'vrf-vxlan'};
f23633dc 123 my $exitnodes = $plugin_config->{'exitnodes'};
847f5144 124 my $exitnodes_primary = $plugin_config->{'exitnodes-primary'};
92d8effb 125 my $advertisesubnets = $plugin_config->{'advertise-subnets'};
3d135423 126 my $exitnodes_local_routing = $plugin_config->{'exitnodes-local-routing'};
be39cee9
TL
127 my $rt_import;
128 $rt_import = [PVE::Tools::split_list($plugin_config->{'rt-import'})] if $plugin_config->{'rt-import'};
f23633dc 129
56cdcac9 130 my $asn = $controller->{asn};
be39cee9
TL
131 my @peers;
132 @peers = PVE::Tools::split_list($controller->{'peers'}) if $controller->{'peers'};
f23633dc
AD
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 }
0589eb09
AD
143
144 return if !$vrf || !$vrfvxlan || !$asn;
145
f34a898e
AD
146 my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers, $loopback);
147
92526f0e 148 # vrf
56cdcac9
AD
149 my @controller_config = ();
150 push @controller_config, "vni $vrfvxlan";
151 push(@{$config->{frr}->{vrf}->{"$vrf"}}, @controller_config);
0589eb09 152
f23633dc
AD
153 #main vrf router
154 @controller_config = ();
f34a898e 155 push @controller_config, "bgp router-id $ifaceip";
f23633dc
AD
156# push @controller_config, "!";
157 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{""}}, @controller_config);
659c27c2 158
f23633dc
AD
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 }
0589eb09 163
b634e577
AD
164 my $is_gateway = $exitnodes->{$local_node};
165
0589eb09
AD
166 if ($is_gateway) {
167
359796b0 168 if (!$exitnodes_primary || $exitnodes_primary eq $local_node) {
916488cc
AD
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) {
847f5144
AD
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";
916488cc
AD
179 my $routemap = { rule => $routemap_config, action => "permit" };
180 unshift(@{$config->{frr_routemap}->{'MAP_VTEP_OUT'}}, $routemap);
847f5144
AD
181 }
182
916488cc 183
3d135423
AD
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);
0589eb09 190
3d135423
AD
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 }
0589eb09 197
56cdcac9 198 @controller_config = ();
0589eb09 199 #add default originate to announce 0.0.0.0/0 type5 route in evpn
56cdcac9
AD
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);
92d8effb
AD
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);
0589eb09
AD
216 }
217
359796b0 218 if ($rt_import) {
96794fd6
AD
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
0589eb09
AD
226 return $config;
227}
228
3d135423
AD
229sub 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};
359796b0 239
3d135423
AD
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
32602a38 252sub on_delete_hook {
56cdcac9 253 my ($class, $controllerid, $zone_cfg) = @_;
32602a38 254
56cdcac9
AD
255 # verify that zone is associated to this controller
256 foreach my $id (keys %{$zone_cfg->{ids}}) {
92526f0e
TL
257 my $zone = $zone_cfg->{ids}->{$id};
258 die "controller $controllerid is used by $id"
259 if (defined($zone->{controller}) && $zone->{controller} eq $controllerid);
5bda8607 260 }
32602a38
AD
261}
262
263sub on_update_hook {
56cdcac9 264 my ($class, $controllerid, $controller_cfg) = @_;
5bda8607 265
c7bb4ac5
AD
266 # we can only have 1 evpn controller / 1 asn by server
267
f23633dc 268 my $controllernb = 0;
56cdcac9
AD
269 foreach my $id (keys %{$controller_cfg->{ids}}) {
270 next if $id eq $controllerid;
92526f0e 271 my $controller = $controller_cfg->{ids}->{$id};
f23633dc
AD
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
278sub 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;
5bda8607 287 }
f23633dc
AD
288
289 return $controller;
32602a38
AD
290}
291
f23633dc 292
8fb1ee7f
AD
293sub 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
92526f0e 307 if ($a =~ /bgp (\d+)$/) {
8fb1ee7f
AD
308 $a_val = 2;
309 }
310
92526f0e 311 if ($b =~ /bgp (\d+)$/) {
8fb1ee7f
AD
312 $b_val = 2;
313 }
314
315 return $a_val <=> $b_val;
316}
317
318sub 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
32870bdc
AD
330 my $simple_exitkeylist = {};
331 $simple_exitkeylist->{router} = 1;
332
92526f0e 333 # FIXME: make this generic
8fb1ee7f 334 my $paddinglevel = undef;
92526f0e
TL
335 if ($level == 1 || $level == 2) {
336 $paddinglevel = $level - 1;
8fb1ee7f 337 } elsif ($level == 3 || $level == 4) {
92526f0e 338 $paddinglevel = $level - 2;
8fb1ee7f
AD
339 }
340
341 my $padding = "";
342 $padding = ' ' x ($paddinglevel) if $paddinglevel;
343
92526f0e 344 if (ref $content eq 'HASH') {
8fb1ee7f
AD
345 foreach my $key (sort sort_frr_config keys %$content) {
346 if ($parentkey && defined($keylist->{$parentkey})) {
92526f0e
TL
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";
8fb1ee7f
AD
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});
32870bdc 357 push @{$final_config}, $padding."exit" if $parentkey && defined($simple_exitkeylist->{$parentkey});
8fb1ee7f
AD
358 }
359 }
32602a38 360
8fb1ee7f 361 if (ref $content eq 'ARRAY') {
92526f0e 362 push @{$final_config}, map { $padding . "$_" } @$content;
8fb1ee7f
AD
363 }
364}
365
847f5144
AD
366sub 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++;
916488cc 375 next if !defined($seq->{action});
847f5144
AD
376 my @config = ();
377 push @config, "!";
916488cc
AD
378 push @config, "route-map $id $seq->{action} $order";
379 my $rule = $seq->{rule};
380 push @config, map { " $_" } @$rule;
847f5144 381 push @{$final_config}, @config;
32870bdc 382 push @{$final_config}, "exit";
847f5144
AD
383 }
384 }
385}
78f249bc
AD
386
387sub generate_frr_accesslist {
388 my ($final_config, $accesslists) = @_;
389
359796b0 390 my $config = [];
78f249bc
AD
391
392 for my $id (sort keys %$accesslists) {
78f249bc
AD
393 my $accesslist = $accesslists->{$id};
394
395 for my $seq (sort keys %$accesslist) {
396 my $rule = $accesslist->{$seq};
359796b0 397 push @$config, "access-list $id seq $seq $rule";
78f249bc
AD
398 }
399 }
400
359796b0
TL
401 if (@$config > 0) {
402 push @{$final_config}, "!", @$config;
78f249bc
AD
403 }
404}
405
9cef13e9 406sub generate_controller_rawconfig {
8fb1ee7f
AD
407 my ($class, $plugin_config, $config) = @_;
408
659c27c2
AD
409 my $nodename = PVE::INotify::nodename();
410
8fb1ee7f 411 my $final_config = [];
4bd3d7bf 412 push @{$final_config}, "frr version 8.2.2";
67a0f815 413 push @{$final_config}, "frr defaults datacenter";
659c27c2 414 push @{$final_config}, "hostname $nodename";
9c7dded6
AD
415 push @{$final_config}, "log syslog informational";
416 push @{$final_config}, "service integrated-vtysh-config";
8fb1ee7f
AD
417 push @{$final_config}, "!";
418
0d1ab7dc 419 if (-e "/etc/frr/frr.conf.local") {
cdf2c819 420 my $local_conf = file_get_contents("/etc/frr/frr.conf.local");
78f249bc 421 parse_merge_frr_local_config($config, $local_conf);
0d1ab7dc 422 }
8fb1ee7f 423
78f249bc
AD
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
8fb1ee7f
AD
428 push @{$final_config}, "!";
429 push @{$final_config}, "line vty";
430 push @{$final_config}, "!";
431
432 my $rawconfig = join("\n", @{$final_config});
433
9cef13e9
AD
434 return if !$rawconfig;
435 return $rawconfig;
436}
437
78f249bc
AD
438sub 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
9cef13e9
AD
497sub write_controller_config {
498 my ($class, $plugin_config, $config) = @_;
499
500 my $rawconfig = $class->generate_controller_rawconfig($plugin_config, $config);
8fb1ee7f
AD
501 return if !$rawconfig;
502 return if !-d "/etc/frr";
503
cdf2c819 504 file_set_contents("/etc/frr/frr.conf", $rawconfig);
8fb1ee7f
AD
505}
506
fa609bdd
AD
507sub reload_controller {
508 my ($class) = @_;
509
510 my $conf_file = "/etc/frr/frr.conf";
659c27c2
AD
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 }
fa609bdd
AD
517
518 my $err = sub {
519 my $line = shift;
659c27c2
AD
520 if ($line =~ /ERROR:/) {
521 warn "$line \n";
fa609bdd
AD
522 }
523 };
524
525 if (-e $conf_file && -e $bin_path) {
9c24bcc5
AD
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 }
fa609bdd
AD
533 }
534}
535
8fb1ee7f 5361;
32602a38 537
0589eb09 538