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'};
127 my $rt_import = [PVE
::Tools
::split_list
($plugin_config->{'rt-import'})] if $plugin_config->{'rt-import'};
129 my $asn = $controller->{asn
};
130 my @peers = PVE
::Tools
::split_list
($controller->{'peers'}) if $controller->{'peers'};
132 my $loopback = undef;
133 my $autortas = undef;
134 my $bgprouter = find_bgp_controller
($local_node, $controller_cfg);
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;
142 return if !$vrf || !$vrfvxlan || !$asn;
144 my ($ifaceip, $interface) = PVE
::Network
::SDN
::Zones
::Plugin
::find_local_ip_interface_peers
(\
@peers, $loopback);
147 my @controller_config = ();
148 push @controller_config, "vni $vrfvxlan";
149 push(@{$config->{frr
}->{vrf
}->{"$vrf"}}, @controller_config);
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);
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");
162 my $is_gateway = $exitnodes->{$local_node};
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);
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);
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);
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) {
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);
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);
217 @controller_config = ();
218 foreach my $rt (sort @{$rt_import}) {
219 push @controller_config, "route-target import $rt";
221 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
227 sub generate_controller_vnet_config
{
228 my ($class, $plugin_config, $controller, $zone, $zoneid, $vnetid, $config) = @_;
230 my $exitnodes = $zone->{'exitnodes'};
231 my $exitnodes_local_routing = $zone->{'exitnodes-local-routing'};
233 return if !$exitnodes_local_routing;
235 my $local_node = PVE
::INotify
::nodename
();
236 my $is_gateway = $exitnodes->{$local_node};
238 return if !$is_gateway;
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";
247 push(@{$config->{frr
}->{''}}, @controller_config);
251 my ($class, $controllerid, $zone_cfg) = @_;
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);
262 my ($class, $controllerid, $controller_cfg) = @_;
264 # we can only have 1 evpn controller / 1 asn by server
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";
272 die "only 1 global evpn controller can be defined" if $controllernb > 1;
276 sub find_bgp_controller
{
277 my ($nodename, $controller_cfg) = @_;
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;
291 sub sort_frr_config
{
295 $order->{'ipv4 unicast'} = 1;
296 $order->{'ipv6 unicast'} = 2;
297 $order->{'l2vpn evpn'} = 3;
302 $a_val = $order->{$a} if defined($order->{$a});
303 $b_val = $order->{$b} if defined($order->{$b});
305 if ($a =~ /bgp (\d+)$/) {
309 if ($b =~ /bgp (\d+)$/) {
313 return $a_val <=> $b_val;
316 sub generate_frr_recurse
{
317 my ($final_config, $content, $parentkey, $level) = @_;
321 $keylist->{'address-family'} = 1;
322 $keylist->{router
} = 1;
324 my $exitkeylist = {};
325 $exitkeylist->{vrf
} = 1;
326 $exitkeylist->{'address-family'} = 1;
328 my $simple_exitkeylist = {};
329 $simple_exitkeylist->{router
} = 1;
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;
340 $padding = ' ' x
($paddinglevel) if $paddinglevel;
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";
351 my $option = $content->{$key};
352 generate_frr_recurse
($final_config, $option, $key, $level+1);
354 push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
355 push @{$final_config}, $padding."exit" if $parentkey && defined($simple_exitkeylist->{$parentkey});
359 if (ref $content eq 'ARRAY') {
360 push @{$final_config}, map { $padding . "$_" } @$content;
364 sub generate_frr_routemap
{
365 my ($final_config, $routemaps) = @_;
367 foreach my $id (sort keys %$routemaps) {
369 my $routemap = $routemaps->{$id};
371 foreach my $seq (@$routemap) {
373 next if !defined($seq->{action
});
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";
385 sub generate_frr_accesslist
{
386 my ($final_config, $accesslists) = @_;
390 for my $id (sort keys %$accesslists) {
391 my $accesslist = $accesslists->{$id};
393 for my $seq (sort keys %$accesslist) {
394 my $rule = $accesslist->{$seq};
395 push @$config, "access-list $id seq $seq $rule";
400 push @{$final_config}, "!", @$config;
404 sub generate_controller_rawconfig
{
405 my ($class, $plugin_config, $config) = @_;
407 my $nodename = PVE
::INotify
::nodename
();
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}, "!";
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);
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
});
426 push @{$final_config}, "!";
427 push @{$final_config}, "line vty";
428 push @{$final_config}, "!";
430 my $rawconfig = join("\n", @{$final_config});
432 return if !$rawconfig;
436 sub parse_merge_frr_local_config
{
437 my ($config, $local_conf) = @_;
439 my $section = \
$config->{""};
441 my $routemap = undef;
442 my $routemap_config = ();
443 my $routemap_action = undef;
445 while ($local_conf =~ /^\s*(.+?)\s*$/gm) {
447 $line =~ s/^\s+|\s+$//g;
449 if ($line =~ m/^router (.+)$/) {
451 $section = \
$config->{'frr'}->{'router'}->{$router}->{""};
453 } elsif ($line =~ m/^vrf (.+)$/) {
454 $section = \
$config->{'frr'}->{'vrf'}->{$1};
456 } elsif ($line =~ m/address-family (.+)$/) {
457 $section = \
$config->{'frr'}->{'router'}->{$router}->{'address-family'}->{$1};
459 } elsif ($line =~ m/^route-map (.+) (permit|deny) (\d+)/) {
461 $routemap_config = ();
462 $routemap_action = $2;
463 $section = \
$config->{'frr_routemap'}->{$routemap};
465 } elsif ($line =~ m/^access-list (.+) seq (\d+) (.+)$/) {
466 $config->{'frr_access_list'}->{$1}->{$2} = $3;
468 } elsif($line =~ m/^exit-address-family$/) {
470 } elsif($line =~ m/^exit$/) {
472 $section = \
$config->{''};
475 push(@{$$section}, { rule
=> $routemap_config, action
=> $routemap_action });
476 $section = \
$config->{''};
478 $routemap_action = undef;
479 $routemap_config = ();
482 } elsif($line =~ m/!/) {
488 push(@{$routemap_config}, $line);
490 push(@{$$section}, $line);
495 sub write_controller_config
{
496 my ($class, $plugin_config, $config) = @_;
498 my $rawconfig = $class->generate_controller_rawconfig($plugin_config, $config);
499 return if !$rawconfig;
500 return if !-d
"/etc/frr";
502 file_set_contents
("/etc/frr/frr.conf", $rawconfig);
505 sub reload_controller
{
508 my $conf_file = "/etc/frr/frr.conf";
509 my $bin_path = "/usr/lib/frr/frr-reload.py";
512 warn "missing $bin_path. Please install frr-pythontools package";
518 if ($line =~ /ERROR:/) {
523 if (-e
$conf_file && -e
$bin_path) {
525 run_command
([$bin_path, '--stdout', '--reload', $conf_file], outfunc
=> {}, errfunc
=> $err);
528 warn "frr reload command fail. Restarting frr.";
529 eval { run_command
(['systemctl', 'restart', 'frr']); };