1 package PVE
::Network
::SDN
::Controllers
::EvpnPlugin
;
7 use PVE
::JSONSchema
qw(get_standard_option);
8 use PVE
::Tools
qw(run_command file_set_contents file_get_contents);
10 use PVE
::Network
::SDN
::Controllers
::Plugin
;
11 use PVE
::Network
::SDN
::Zones
::Plugin
;
14 use base
('PVE::Network::SDN::Controllers::Plugin');
24 description
=> "autonomous system number",
29 description
=> "peers address list.",
30 type
=> 'string', format
=> 'ip-list'
37 'asn' => { optional
=> 0 },
38 'peers' => { optional
=> 0 },
42 # Plugin implementation
43 sub generate_controller_config
{
44 my ($class, $plugin_config, $controller_cfg, $id, $uplinks, $config) = @_;
47 @peers = PVE
::Tools
::split_list
($plugin_config->{'peers'}) if $plugin_config->{'peers'};
49 my $local_node = PVE
::INotify
::nodename
();
51 my $asn = $plugin_config->{asn
};
55 my $bgprouter = find_bgp_controller
($local_node, $controller_cfg);
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;
65 my $bgp = $config->{frr
}->{router
}->{"bgp $asn"} //= {};
67 my ($ifaceip, $interface) = PVE
::Network
::SDN
::Zones
::Plugin
::find_local_ip_interface_peers
(\
@peers, $loopback);
69 my $remoteas = $ebgp ?
"external" : $asn;
72 my @controller_config = (
73 "bgp router-id $ifaceip",
74 "no bgp default ipv4-unicast",
78 push(@{$bgp->{""}}, @controller_config) if keys %{$bgp} == 0;
80 @controller_config = ();
83 push @controller_config, "neighbor VTEP peer-group";
84 push @controller_config, "neighbor VTEP remote-as $remoteas";
85 push @controller_config, "neighbor VTEP bfd";
87 if($ebgp && $loopback) {
88 push @controller_config, "neighbor VTEP ebgp-multihop 10";
89 push @controller_config, "neighbor VTEP update-source $loopback";
93 foreach my $address (@peers) {
94 next if $address eq $ifaceip;
95 push @controller_config, "neighbor $address peer-group VTEP";
98 push(@{$bgp->{""}}, @controller_config);
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);
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 );
116 sub generate_controller_zone_config
{
117 my ($class, $plugin_config, $controller, $controller_cfg, $id, $uplinks, $config) = @_;
119 my $local_node = PVE
::INotify
::nodename
();
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'};
128 $rt_import = [PVE
::Tools
::split_list
($plugin_config->{'rt-import'})] if $plugin_config->{'rt-import'};
130 my $asn = $controller->{asn
};
132 @peers = PVE
::Tools
::split_list
($controller->{'peers'}) if $controller->{'peers'};
134 my $loopback = undef;
135 my $autortas = undef;
136 my $bgprouter = find_bgp_controller
($local_node, $controller_cfg);
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;
144 return if !$vrf || !$vrfvxlan || !$asn;
146 my ($ifaceip, $interface) = PVE
::Network
::SDN
::Zones
::Plugin
::find_local_ip_interface_peers
(\
@peers, $loopback);
149 my @controller_config = ();
150 push @controller_config, "vni $vrfvxlan";
151 push(@{$config->{frr
}->{vrf
}->{"$vrf"}}, @controller_config);
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);
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");
164 my $is_gateway = $exitnodes->{$local_node};
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);
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);
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);
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) {
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);
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);
219 @controller_config = ();
220 foreach my $rt (sort @{$rt_import}) {
221 push @controller_config, "route-target import $rt";
223 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
229 sub generate_controller_vnet_config
{
230 my ($class, $plugin_config, $controller, $zone, $zoneid, $vnetid, $config) = @_;
232 my $exitnodes = $zone->{'exitnodes'};
233 my $exitnodes_local_routing = $zone->{'exitnodes-local-routing'};
235 return if !$exitnodes_local_routing;
237 my $local_node = PVE
::INotify
::nodename
();
238 my $is_gateway = $exitnodes->{$local_node};
240 return if !$is_gateway;
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";
249 push(@{$config->{frr
}->{''}}, @controller_config);
253 my ($class, $controllerid, $zone_cfg) = @_;
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);
264 my ($class, $controllerid, $controller_cfg) = @_;
266 # we can only have 1 evpn controller / 1 asn by server
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";
274 die "only 1 global evpn controller can be defined" if $controllernb > 1;
278 sub find_bgp_controller
{
279 my ($nodename, $controller_cfg) = @_;
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;
293 sub sort_frr_config
{
297 $order->{'ipv4 unicast'} = 1;
298 $order->{'ipv6 unicast'} = 2;
299 $order->{'l2vpn evpn'} = 3;
304 $a_val = $order->{$a} if defined($order->{$a});
305 $b_val = $order->{$b} if defined($order->{$b});
307 if ($a =~ /bgp (\d+)$/) {
311 if ($b =~ /bgp (\d+)$/) {
315 return $a_val <=> $b_val;
318 sub generate_frr_recurse
{
319 my ($final_config, $content, $parentkey, $level) = @_;
323 $keylist->{'address-family'} = 1;
324 $keylist->{router
} = 1;
326 my $exitkeylist = {};
327 $exitkeylist->{vrf
} = 1;
328 $exitkeylist->{'address-family'} = 1;
330 my $simple_exitkeylist = {};
331 $simple_exitkeylist->{router
} = 1;
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;
342 $padding = ' ' x
($paddinglevel) if $paddinglevel;
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";
353 my $option = $content->{$key};
354 generate_frr_recurse
($final_config, $option, $key, $level+1);
356 push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
357 push @{$final_config}, $padding."exit" if $parentkey && defined($simple_exitkeylist->{$parentkey});
361 if (ref $content eq 'ARRAY') {
362 push @{$final_config}, map { $padding . "$_" } @$content;
366 sub generate_frr_routemap
{
367 my ($final_config, $routemaps) = @_;
369 foreach my $id (sort keys %$routemaps) {
371 my $routemap = $routemaps->{$id};
373 foreach my $seq (@$routemap) {
375 next if !defined($seq->{action
});
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";
387 sub generate_frr_accesslist
{
388 my ($final_config, $accesslists) = @_;
392 for my $id (sort keys %$accesslists) {
393 my $accesslist = $accesslists->{$id};
395 for my $seq (sort keys %$accesslist) {
396 my $rule = $accesslist->{$seq};
397 push @$config, "access-list $id seq $seq $rule";
402 push @{$final_config}, "!", @$config;
406 sub generate_controller_rawconfig
{
407 my ($class, $plugin_config, $config) = @_;
409 my $nodename = PVE
::INotify
::nodename
();
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}, "!";
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);
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
});
428 push @{$final_config}, "!";
429 push @{$final_config}, "line vty";
430 push @{$final_config}, "!";
432 my $rawconfig = join("\n", @{$final_config});
434 return if !$rawconfig;
438 sub parse_merge_frr_local_config
{
439 my ($config, $local_conf) = @_;
441 my $section = \
$config->{""};
443 my $routemap = undef;
444 my $routemap_config = ();
445 my $routemap_action = undef;
447 while ($local_conf =~ /^\s*(.+?)\s*$/gm) {
449 $line =~ s/^\s+|\s+$//g;
451 if ($line =~ m/^router (.+)$/) {
453 $section = \
$config->{'frr'}->{'router'}->{$router}->{""};
455 } elsif ($line =~ m/^vrf (.+)$/) {
456 $section = \
$config->{'frr'}->{'vrf'}->{$1};
458 } elsif ($line =~ m/address-family (.+)$/) {
459 $section = \
$config->{'frr'}->{'router'}->{$router}->{'address-family'}->{$1};
461 } elsif ($line =~ m/^route-map (.+) (permit|deny) (\d+)/) {
463 $routemap_config = ();
464 $routemap_action = $2;
465 $section = \
$config->{'frr_routemap'}->{$routemap};
467 } elsif ($line =~ m/^access-list (.+) seq (\d+) (.+)$/) {
468 $config->{'frr_access_list'}->{$1}->{$2} = $3;
470 } elsif($line =~ m/^exit-address-family$/) {
472 } elsif($line =~ m/^exit$/) {
474 $section = \
$config->{''};
477 push(@{$$section}, { rule
=> $routemap_config, action
=> $routemap_action });
478 $section = \
$config->{''};
480 $routemap_action = undef;
481 $routemap_config = ();
484 } elsif($line =~ m/!/) {
490 push(@{$routemap_config}, $line);
492 push(@{$$section}, $line);
497 sub write_controller_config
{
498 my ($class, $plugin_config, $config) = @_;
500 my $rawconfig = $class->generate_controller_rawconfig($plugin_config, $config);
501 return if !$rawconfig;
502 return if !-d
"/etc/frr";
504 file_set_contents
("/etc/frr/frr.conf", $rawconfig);
507 sub reload_controller
{
510 my $conf_file = "/etc/frr/frr.conf";
511 my $bin_path = "/usr/lib/frr/frr-reload.py";
514 warn "missing $bin_path. Please install frr-pythontools package";
520 if ($line =~ /ERROR:/) {
525 if (-e
$conf_file && -e
$bin_path) {
527 run_command
([$bin_path, '--stdout', '--reload', $conf_file], outfunc
=> {}, errfunc
=> $err);
530 warn "frr reload command fail. Restarting frr.";
531 eval { run_command
(['systemctl', 'restart', 'frr']); };