2019-11-26 10:00:24 +01:00
|
|
|
package PVE::Network::SDN::Controllers::EvpnPlugin;
|
2019-08-29 12:32:47 +02:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
2020-01-16 11:14:53 +01:00
|
|
|
|
2019-09-09 08:45:53 +02:00
|
|
|
use PVE::INotify;
|
|
|
|
use PVE::JSONSchema qw(get_standard_option);
|
2020-01-16 11:14:53 +01:00
|
|
|
use PVE::Tools qw(run_command file_set_contents file_get_contents);
|
|
|
|
|
|
|
|
use PVE::Network::SDN::Controllers::Plugin;
|
2019-11-28 09:40:27 +01:00
|
|
|
use PVE::Network::SDN::Zones::Plugin;
|
2020-11-25 10:01:36 +01:00
|
|
|
use Net::IP;
|
2020-01-16 11:14:53 +01:00
|
|
|
|
2019-11-26 10:00:16 +01:00
|
|
|
use base('PVE::Network::SDN::Controllers::Plugin');
|
2019-08-29 12:32:47 +02:00
|
|
|
|
|
|
|
sub type {
|
2019-11-26 10:00:24 +01:00
|
|
|
return 'evpn';
|
2019-09-30 11:03:32 +02:00
|
|
|
}
|
|
|
|
|
2019-08-29 12:32:47 +02:00
|
|
|
sub properties {
|
|
|
|
return {
|
2020-01-16 11:14:15 +01:00
|
|
|
asn => {
|
|
|
|
type => 'integer',
|
|
|
|
description => "autonomous system number",
|
2022-02-11 10:33:25 +01:00
|
|
|
minimum => 0,
|
|
|
|
maximum => 4294967296
|
2020-01-16 11:14:15 +01:00
|
|
|
},
|
|
|
|
peers => {
|
|
|
|
description => "peers address list.",
|
|
|
|
type => 'string', format => 'ip-list'
|
|
|
|
},
|
2019-08-29 12:32:47 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub options {
|
|
|
|
return {
|
2020-01-16 11:14:15 +01:00
|
|
|
'asn' => { optional => 0 },
|
|
|
|
'peers' => { optional => 0 },
|
2019-08-29 12:32:47 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
# Plugin implementation
|
2019-09-30 11:03:32 +02:00
|
|
|
sub generate_controller_config {
|
2020-11-25 10:01:36 +01:00
|
|
|
my ($class, $plugin_config, $controller_cfg, $id, $uplinks, $config) = @_;
|
2019-08-29 12:32:47 +02:00
|
|
|
|
2020-07-01 09:57:32 +02:00
|
|
|
my @peers;
|
|
|
|
@peers = PVE::Tools::split_list($plugin_config->{'peers'}) if $plugin_config->{'peers'};
|
2019-08-29 12:32:47 +02:00
|
|
|
|
2020-11-25 10:01:36 +01:00
|
|
|
my $local_node = PVE::INotify::nodename();
|
|
|
|
|
2019-09-09 08:45:53 +02:00
|
|
|
my $asn = $plugin_config->{asn};
|
2020-11-25 10:01:36 +01:00
|
|
|
my $ebgp = undef;
|
|
|
|
my $loopback = undef;
|
|
|
|
my $autortas = undef;
|
|
|
|
my $bgprouter = find_bgp_controller($local_node, $controller_cfg);
|
2022-08-31 10:56:01 +02:00
|
|
|
if ($bgprouter) {
|
2020-11-25 10:01:36 +01:00
|
|
|
$ebgp = 1 if $plugin_config->{'asn'} ne $bgprouter->{asn};
|
|
|
|
$loopback = $bgprouter->{loopback} if $bgprouter->{loopback};
|
|
|
|
$asn = $bgprouter->{asn} if $bgprouter->{asn};
|
|
|
|
$autortas = $plugin_config->{'asn'} if $ebgp;
|
|
|
|
}
|
2019-09-09 08:45:53 +02:00
|
|
|
|
|
|
|
return if !$asn;
|
2019-08-29 12:32:47 +02:00
|
|
|
|
2020-01-16 11:14:15 +01:00
|
|
|
my $bgp = $config->{frr}->{router}->{"bgp $asn"} //= {};
|
|
|
|
|
2020-11-25 10:01:36 +01:00
|
|
|
my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers, $loopback);
|
2019-08-29 12:32:47 +02:00
|
|
|
|
2020-11-25 10:01:36 +01:00
|
|
|
my $remoteas = $ebgp ? "external" : $asn;
|
2019-09-09 08:45:50 +02:00
|
|
|
|
2020-11-25 10:01:36 +01:00
|
|
|
#global options
|
2020-01-16 11:14:15 +01:00
|
|
|
my @controller_config = (
|
|
|
|
"bgp router-id $ifaceip",
|
2023-05-23 08:47:26 +02:00
|
|
|
"no bgp hard-administrative-reset",
|
|
|
|
"no bgp graceful-restart notification",
|
2020-01-16 11:14:15 +01:00
|
|
|
"no bgp default ipv4-unicast",
|
|
|
|
"coalesce-time 1000",
|
|
|
|
);
|
2019-08-29 12:32:47 +02:00
|
|
|
|
2020-11-25 10:01:36 +01:00
|
|
|
push(@{$bgp->{""}}, @controller_config) if keys %{$bgp} == 0;
|
|
|
|
|
|
|
|
@controller_config = ();
|
2022-08-31 10:56:01 +02:00
|
|
|
|
2020-11-25 10:01:36 +01:00
|
|
|
#VTEP neighbors
|
|
|
|
push @controller_config, "neighbor VTEP peer-group";
|
|
|
|
push @controller_config, "neighbor VTEP remote-as $remoteas";
|
|
|
|
push @controller_config, "neighbor VTEP bfd";
|
|
|
|
|
|
|
|
if($ebgp && $loopback) {
|
|
|
|
push @controller_config, "neighbor VTEP ebgp-multihop 10";
|
|
|
|
push @controller_config, "neighbor VTEP update-source $loopback";
|
|
|
|
}
|
|
|
|
|
|
|
|
# VTEP peers
|
2019-08-29 12:32:47 +02:00
|
|
|
foreach my $address (@peers) {
|
|
|
|
next if $address eq $ifaceip;
|
2020-11-25 10:01:36 +01:00
|
|
|
push @controller_config, "neighbor $address peer-group VTEP";
|
2019-09-03 08:25:00 +02:00
|
|
|
}
|
2019-09-09 08:45:53 +02:00
|
|
|
|
2020-01-16 11:14:15 +01:00
|
|
|
push(@{$bgp->{""}}, @controller_config);
|
2019-09-09 08:45:53 +02:00
|
|
|
|
2020-11-25 10:01:36 +01:00
|
|
|
# address-family l2vpn
|
2019-11-26 10:00:29 +01:00
|
|
|
@controller_config = ();
|
2022-04-20 16:19:30 +02:00
|
|
|
push @controller_config, "neighbor VTEP route-map MAP_VTEP_IN in";
|
2022-02-11 10:33:26 +01:00
|
|
|
push @controller_config, "neighbor VTEP route-map MAP_VTEP_OUT out";
|
2020-11-25 10:01:36 +01:00
|
|
|
push @controller_config, "neighbor VTEP activate";
|
2019-11-26 10:00:29 +01:00
|
|
|
push @controller_config, "advertise-all-vni";
|
2020-11-25 10:01:36 +01:00
|
|
|
push @controller_config, "autort as $autortas" if $autortas;
|
2020-01-16 11:14:15 +01:00
|
|
|
push(@{$bgp->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
|
2019-08-29 12:32:47 +02:00
|
|
|
|
2022-04-20 16:19:30 +02:00
|
|
|
my $routemap = { rule => undef, action => "permit" };
|
|
|
|
push(@{$config->{frr_routemap}->{'MAP_VTEP_IN'}}, $routemap );
|
|
|
|
push(@{$config->{frr_routemap}->{'MAP_VTEP_OUT'}}, $routemap );
|
2022-02-11 10:33:26 +01:00
|
|
|
|
2019-08-29 12:32:47 +02:00
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
2019-11-26 10:00:29 +01:00
|
|
|
sub generate_controller_zone_config {
|
2020-11-25 10:01:36 +01:00
|
|
|
my ($class, $plugin_config, $controller, $controller_cfg, $id, $uplinks, $config) = @_;
|
|
|
|
|
|
|
|
my $local_node = PVE::INotify::nodename();
|
2019-09-30 11:03:33 +02:00
|
|
|
|
2020-05-19 18:37:44 +02:00
|
|
|
my $vrf = "vrf_$id";
|
2019-09-30 11:03:33 +02:00
|
|
|
my $vrfvxlan = $plugin_config->{'vrf-vxlan'};
|
2020-11-25 10:01:36 +01:00
|
|
|
my $exitnodes = $plugin_config->{'exitnodes'};
|
2022-02-11 10:33:26 +01:00
|
|
|
my $exitnodes_primary = $plugin_config->{'exitnodes-primary'};
|
2021-08-23 15:22:15 +02:00
|
|
|
my $advertisesubnets = $plugin_config->{'advertise-subnets'};
|
2021-08-23 15:22:16 +02:00
|
|
|
my $exitnodes_local_routing = $plugin_config->{'exitnodes-local-routing'};
|
2022-08-31 10:56:27 +02:00
|
|
|
my $rt_import;
|
|
|
|
$rt_import = [PVE::Tools::split_list($plugin_config->{'rt-import'})] if $plugin_config->{'rt-import'};
|
2020-11-25 10:01:36 +01:00
|
|
|
|
2019-11-26 10:00:29 +01:00
|
|
|
my $asn = $controller->{asn};
|
2022-08-31 10:56:27 +02:00
|
|
|
my @peers;
|
|
|
|
@peers = PVE::Tools::split_list($controller->{'peers'}) if $controller->{'peers'};
|
2020-11-25 10:01:36 +01:00
|
|
|
my $ebgp = undef;
|
|
|
|
my $loopback = undef;
|
|
|
|
my $autortas = undef;
|
|
|
|
my $bgprouter = find_bgp_controller($local_node, $controller_cfg);
|
|
|
|
if($bgprouter) {
|
|
|
|
$ebgp = 1 if $controller->{'asn'} ne $bgprouter->{asn};
|
|
|
|
$loopback = $bgprouter->{loopback} if $bgprouter->{loopback};
|
|
|
|
$asn = $bgprouter->{asn} if $bgprouter->{asn};
|
|
|
|
$autortas = $controller->{'asn'} if $ebgp;
|
|
|
|
}
|
2019-09-30 11:03:33 +02:00
|
|
|
|
|
|
|
return if !$vrf || !$vrfvxlan || !$asn;
|
|
|
|
|
2022-02-11 10:33:27 +01:00
|
|
|
my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers, $loopback);
|
2023-04-20 23:36:20 +02:00
|
|
|
my $is_gateway = $exitnodes->{$local_node};
|
2022-02-11 10:33:27 +01:00
|
|
|
|
2020-01-16 11:14:15 +01:00
|
|
|
# vrf
|
2019-11-26 10:00:29 +01:00
|
|
|
my @controller_config = ();
|
|
|
|
push @controller_config, "vni $vrfvxlan";
|
2023-04-20 23:36:20 +02:00
|
|
|
#avoid to routes between nodes through the exit nodes
|
|
|
|
#null routes subnets of other zones
|
|
|
|
if ($is_gateway) {
|
|
|
|
my $subnets = PVE::Network::SDN::Vnets::get_subnets();
|
|
|
|
foreach my $subnetid (sort keys %{$subnets}) {
|
|
|
|
my $subnet = $subnets->{$subnetid};
|
|
|
|
my $cidr = $subnet->{cidr};
|
|
|
|
my $zone = $subnet->{zone};
|
|
|
|
push @controller_config, "ip route $cidr null0" if $zone ne $id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 10:00:29 +01:00
|
|
|
push(@{$config->{frr}->{vrf}->{"$vrf"}}, @controller_config);
|
2019-09-30 11:03:33 +02:00
|
|
|
|
2020-11-25 10:01:36 +01:00
|
|
|
#main vrf router
|
|
|
|
@controller_config = ();
|
2022-02-11 10:33:27 +01:00
|
|
|
push @controller_config, "bgp router-id $ifaceip";
|
2023-05-23 08:47:26 +02:00
|
|
|
push @controller_config, "no bgp hard-administrative-reset";
|
|
|
|
push @controller_config, "no bgp graceful-restart notification";
|
|
|
|
|
2020-11-25 10:01:36 +01:00
|
|
|
# push @controller_config, "!";
|
|
|
|
push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{""}}, @controller_config);
|
2019-11-26 10:00:30 +01:00
|
|
|
|
2020-11-25 10:01:36 +01:00
|
|
|
if ($autortas) {
|
|
|
|
push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, "route-target import $autortas:$vrfvxlan");
|
|
|
|
push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, "route-target export $autortas:$vrfvxlan");
|
|
|
|
}
|
2019-09-30 11:03:33 +02:00
|
|
|
|
|
|
|
if ($is_gateway) {
|
|
|
|
|
2022-08-31 10:56:01 +02:00
|
|
|
if (!$exitnodes_primary || $exitnodes_primary eq $local_node) {
|
2022-04-20 16:19:30 +02:00
|
|
|
#filter default type5 route coming from other exit nodes on primary node or both nodes if no primary is defined.
|
|
|
|
my $routemap_config = ();
|
2023-04-20 23:36:17 +02:00
|
|
|
push @{$routemap_config}, "match evpn vni $vrfvxlan";
|
2022-04-20 16:19:30 +02:00
|
|
|
push @{$routemap_config}, "match evpn route-type prefix";
|
|
|
|
my $routemap = { rule => $routemap_config, action => "deny" };
|
|
|
|
unshift(@{$config->{frr_routemap}->{'MAP_VTEP_IN'}}, $routemap);
|
|
|
|
} elsif ($exitnodes_primary ne $local_node) {
|
2022-02-11 10:33:26 +01:00
|
|
|
my $routemap_config = ();
|
|
|
|
push @{$routemap_config}, "match evpn vni $vrfvxlan";
|
|
|
|
push @{$routemap_config}, "match evpn route-type prefix";
|
|
|
|
push @{$routemap_config}, "set metric 200";
|
2022-04-20 16:19:30 +02:00
|
|
|
my $routemap = { rule => $routemap_config, action => "permit" };
|
|
|
|
unshift(@{$config->{frr_routemap}->{'MAP_VTEP_OUT'}}, $routemap);
|
2022-02-11 10:33:26 +01:00
|
|
|
}
|
|
|
|
|
2022-04-20 16:19:30 +02:00
|
|
|
|
2021-08-23 15:22:16 +02:00
|
|
|
if (!$exitnodes_local_routing) {
|
|
|
|
@controller_config = ();
|
|
|
|
#import /32 routes of evpn network from vrf1 to default vrf (for packet return)
|
|
|
|
push @controller_config, "import vrf $vrf";
|
|
|
|
push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
|
|
|
|
push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
|
2019-09-30 11:03:33 +02:00
|
|
|
|
2021-08-23 15:22:16 +02:00
|
|
|
@controller_config = ();
|
|
|
|
#redistribute connected to be able to route to local vms on the gateway
|
|
|
|
push @controller_config, "redistribute connected";
|
|
|
|
push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
|
|
|
|
push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
|
|
|
|
}
|
2019-09-30 11:03:33 +02:00
|
|
|
|
2019-11-26 10:00:29 +01:00
|
|
|
@controller_config = ();
|
2019-09-30 11:03:33 +02:00
|
|
|
#add default originate to announce 0.0.0.0/0 type5 route in evpn
|
2019-11-26 10:00:29 +01:00
|
|
|
push @controller_config, "default-originate ipv4";
|
|
|
|
push @controller_config, "default-originate ipv6";
|
|
|
|
push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
|
2021-08-23 15:22:15 +02:00
|
|
|
} elsif ($advertisesubnets) {
|
|
|
|
|
|
|
|
@controller_config = ();
|
|
|
|
#redistribute connected networks
|
|
|
|
push @controller_config, "redistribute connected";
|
|
|
|
push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
|
|
|
|
push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
|
|
|
|
|
|
|
|
@controller_config = ();
|
|
|
|
#advertise connected networks type5 route in evpn
|
|
|
|
push @controller_config, "advertise ipv4 unicast";
|
|
|
|
push @controller_config, "advertise ipv6 unicast";
|
|
|
|
push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
|
2019-09-30 11:03:33 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 10:56:01 +02:00
|
|
|
if ($rt_import) {
|
2022-02-11 10:33:28 +01:00
|
|
|
@controller_config = ();
|
|
|
|
foreach my $rt (sort @{$rt_import}) {
|
|
|
|
push @controller_config, "route-target import $rt";
|
|
|
|
}
|
|
|
|
push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
|
|
|
|
}
|
|
|
|
|
2019-09-30 11:03:33 +02:00
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
2021-08-23 15:22:16 +02:00
|
|
|
sub generate_controller_vnet_config {
|
|
|
|
my ($class, $plugin_config, $controller, $zone, $zoneid, $vnetid, $config) = @_;
|
|
|
|
|
|
|
|
my $exitnodes = $zone->{'exitnodes'};
|
|
|
|
my $exitnodes_local_routing = $zone->{'exitnodes-local-routing'};
|
|
|
|
|
|
|
|
return if !$exitnodes_local_routing;
|
|
|
|
|
|
|
|
my $local_node = PVE::INotify::nodename();
|
|
|
|
my $is_gateway = $exitnodes->{$local_node};
|
2022-08-31 10:56:01 +02:00
|
|
|
|
2021-08-23 15:22:16 +02:00
|
|
|
return if !$is_gateway;
|
|
|
|
|
|
|
|
my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid, 1);
|
|
|
|
my @controller_config = ();
|
|
|
|
foreach my $subnetid (sort keys %{$subnets}) {
|
|
|
|
my $subnet = $subnets->{$subnetid};
|
|
|
|
my $cidr = $subnet->{cidr};
|
|
|
|
push @controller_config, "ip route $cidr 10.255.255.2 xvrf_$zoneid";
|
|
|
|
}
|
2023-04-20 23:36:19 +02:00
|
|
|
push(@{$config->{frr_ip_protocol}}, @controller_config);
|
2021-08-23 15:22:16 +02:00
|
|
|
}
|
|
|
|
|
2019-08-29 12:32:47 +02:00
|
|
|
sub on_delete_hook {
|
2019-11-26 10:00:29 +01:00
|
|
|
my ($class, $controllerid, $zone_cfg) = @_;
|
2019-08-29 12:32:47 +02:00
|
|
|
|
2019-11-26 10:00:29 +01:00
|
|
|
# verify that zone is associated to this controller
|
|
|
|
foreach my $id (keys %{$zone_cfg->{ids}}) {
|
2020-01-16 11:14:15 +01:00
|
|
|
my $zone = $zone_cfg->{ids}->{$id};
|
|
|
|
die "controller $controllerid is used by $id"
|
|
|
|
if (defined($zone->{controller}) && $zone->{controller} eq $controllerid);
|
2019-08-29 12:32:52 +02:00
|
|
|
}
|
2019-08-29 12:32:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub on_update_hook {
|
2019-11-26 10:00:29 +01:00
|
|
|
my ($class, $controllerid, $controller_cfg) = @_;
|
2019-08-29 12:32:52 +02:00
|
|
|
|
2020-01-16 09:15:16 +01:00
|
|
|
# we can only have 1 evpn controller / 1 asn by server
|
|
|
|
|
2020-11-25 10:01:36 +01:00
|
|
|
my $controllernb = 0;
|
2019-11-26 10:00:29 +01:00
|
|
|
foreach my $id (keys %{$controller_cfg->{ids}}) {
|
|
|
|
next if $id eq $controllerid;
|
2020-01-16 11:14:15 +01:00
|
|
|
my $controller = $controller_cfg->{ids}->{$id};
|
2020-11-25 10:01:36 +01:00
|
|
|
next if $controller->{type} ne "evpn";
|
|
|
|
$controllernb++;
|
2022-12-27 08:17:19 +01:00
|
|
|
die "only 1 global evpn controller can be defined" if $controllernb >= 1;
|
2020-11-25 10:01:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub find_bgp_controller {
|
|
|
|
my ($nodename, $controller_cfg) = @_;
|
|
|
|
|
2023-09-13 13:38:40 +02:00
|
|
|
my $res = undef;
|
2020-11-25 10:01:36 +01:00
|
|
|
foreach my $id (keys %{$controller_cfg->{ids}}) {
|
2023-09-13 13:38:40 +02:00
|
|
|
my $controller = $controller_cfg->{ids}->{$id};
|
|
|
|
next if $controller->{type} ne 'bgp';
|
|
|
|
next if $controller->{node} ne $nodename;
|
|
|
|
$res = $controller;
|
2020-11-25 10:01:36 +01:00
|
|
|
last;
|
2019-08-29 12:32:52 +02:00
|
|
|
}
|
2023-09-13 13:38:40 +02:00
|
|
|
return $res;
|
2019-08-29 12:32:47 +02:00
|
|
|
}
|
|
|
|
|
2019-09-30 11:03:32 +02:00
|
|
|
sub generate_frr_recurse{
|
|
|
|
my ($final_config, $content, $parentkey, $level) = @_;
|
|
|
|
|
|
|
|
my $keylist = {};
|
|
|
|
$keylist->{'address-family'} = 1;
|
|
|
|
$keylist->{router} = 1;
|
|
|
|
|
|
|
|
my $exitkeylist = {};
|
|
|
|
$exitkeylist->{'address-family'} = 1;
|
|
|
|
|
2022-08-24 10:56:46 +02:00
|
|
|
my $simple_exitkeylist = {};
|
|
|
|
$simple_exitkeylist->{router} = 1;
|
|
|
|
|
2020-01-16 11:14:15 +01:00
|
|
|
# FIXME: make this generic
|
2019-09-30 11:03:32 +02:00
|
|
|
my $paddinglevel = undef;
|
2020-01-16 11:14:15 +01:00
|
|
|
if ($level == 1 || $level == 2) {
|
|
|
|
$paddinglevel = $level - 1;
|
2019-09-30 11:03:32 +02:00
|
|
|
} elsif ($level == 3 || $level == 4) {
|
2020-01-16 11:14:15 +01:00
|
|
|
$paddinglevel = $level - 2;
|
2019-09-30 11:03:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
my $padding = "";
|
|
|
|
$padding = ' ' x ($paddinglevel) if $paddinglevel;
|
|
|
|
|
2020-01-16 11:14:15 +01:00
|
|
|
if (ref $content eq 'HASH') {
|
2023-04-20 23:36:19 +02:00
|
|
|
foreach my $key (sort keys %$content) {
|
|
|
|
next if $key eq 'vrf';
|
2019-09-30 11:03:32 +02:00
|
|
|
if ($parentkey && defined($keylist->{$parentkey})) {
|
2020-01-16 11:14:15 +01:00
|
|
|
push @{$final_config}, $padding."!";
|
|
|
|
push @{$final_config}, $padding."$parentkey $key";
|
|
|
|
} elsif ($key ne '' && !defined($keylist->{$key})) {
|
|
|
|
push @{$final_config}, $padding."$key";
|
2019-09-30 11:03:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
my $option = $content->{$key};
|
|
|
|
generate_frr_recurse($final_config, $option, $key, $level+1);
|
|
|
|
|
|
|
|
push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
|
2022-08-24 10:56:46 +02:00
|
|
|
push @{$final_config}, $padding."exit" if $parentkey && defined($simple_exitkeylist->{$parentkey});
|
2019-09-30 11:03:32 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-29 12:32:47 +02:00
|
|
|
|
2019-09-30 11:03:32 +02:00
|
|
|
if (ref $content eq 'ARRAY') {
|
2020-01-16 11:14:15 +01:00
|
|
|
push @{$final_config}, map { $padding . "$_" } @$content;
|
2019-09-30 11:03:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 23:36:19 +02:00
|
|
|
sub generate_frr_vrf {
|
|
|
|
my ($final_config, $vrfs) = @_;
|
|
|
|
|
|
|
|
return if !$vrfs;
|
|
|
|
|
|
|
|
my @config = ();
|
|
|
|
|
|
|
|
foreach my $id (sort keys %$vrfs) {
|
|
|
|
my $vrf = $vrfs->{$id};
|
|
|
|
push @config, "!";
|
|
|
|
push @config, "vrf $id";
|
|
|
|
foreach my $rule (@$vrf) {
|
|
|
|
push @config, " $rule";
|
|
|
|
|
|
|
|
}
|
|
|
|
push @config, "exit-vrf";
|
|
|
|
}
|
|
|
|
|
|
|
|
push @{$final_config}, @config;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub generate_frr_ip_protocol {
|
|
|
|
my ($final_config, $ips) = @_;
|
|
|
|
|
|
|
|
return if !$ips;
|
|
|
|
|
|
|
|
my @config = ();
|
|
|
|
push @{$final_config}, "!";
|
|
|
|
foreach my $rule (sort @$ips) {
|
|
|
|
push @{$final_config}, $rule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 10:33:26 +01:00
|
|
|
sub generate_frr_routemap {
|
|
|
|
my ($final_config, $routemaps) = @_;
|
|
|
|
|
|
|
|
foreach my $id (sort keys %$routemaps) {
|
|
|
|
|
|
|
|
my $routemap = $routemaps->{$id};
|
|
|
|
my $order = 0;
|
|
|
|
foreach my $seq (@$routemap) {
|
|
|
|
$order++;
|
2022-04-20 16:19:30 +02:00
|
|
|
next if !defined($seq->{action});
|
2022-02-11 10:33:26 +01:00
|
|
|
my @config = ();
|
|
|
|
push @config, "!";
|
2022-04-20 16:19:30 +02:00
|
|
|
push @config, "route-map $id $seq->{action} $order";
|
|
|
|
my $rule = $seq->{rule};
|
|
|
|
push @config, map { " $_" } @$rule;
|
2022-02-11 10:33:26 +01:00
|
|
|
push @{$final_config}, @config;
|
2022-08-24 10:56:46 +02:00
|
|
|
push @{$final_config}, "exit";
|
2022-02-11 10:33:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-24 10:56:45 +02:00
|
|
|
|
2022-11-30 16:18:59 +01:00
|
|
|
sub generate_frr_list {
|
|
|
|
my ($final_config, $lists, $type) = @_;
|
2022-08-24 10:56:45 +02:00
|
|
|
|
2022-08-31 10:56:01 +02:00
|
|
|
my $config = [];
|
2022-08-24 10:56:45 +02:00
|
|
|
|
2022-11-30 16:18:59 +01:00
|
|
|
for my $id (sort keys %$lists) {
|
|
|
|
my $list = $lists->{$id};
|
2022-08-24 10:56:45 +02:00
|
|
|
|
2022-11-30 16:18:59 +01:00
|
|
|
for my $seq (sort keys %$list) {
|
|
|
|
my $rule = $list->{$seq};
|
|
|
|
push @$config, "$type $id seq $seq $rule";
|
2022-08-24 10:56:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 10:56:01 +02:00
|
|
|
if (@$config > 0) {
|
|
|
|
push @{$final_config}, "!", @$config;
|
2022-08-24 10:56:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 16:11:18 +01:00
|
|
|
sub generate_controller_rawconfig {
|
2019-09-30 11:03:32 +02:00
|
|
|
my ($class, $plugin_config, $config) = @_;
|
|
|
|
|
2019-11-26 10:00:30 +01:00
|
|
|
my $nodename = PVE::INotify::nodename();
|
|
|
|
|
2019-09-30 11:03:32 +02:00
|
|
|
my $final_config = [];
|
2023-05-23 08:47:26 +02:00
|
|
|
push @{$final_config}, "frr version 8.5.1";
|
2020-12-03 10:19:36 +01:00
|
|
|
push @{$final_config}, "frr defaults datacenter";
|
2019-11-26 10:00:30 +01:00
|
|
|
push @{$final_config}, "hostname $nodename";
|
2022-02-11 10:33:31 +01:00
|
|
|
push @{$final_config}, "log syslog informational";
|
|
|
|
push @{$final_config}, "service integrated-vtysh-config";
|
2019-09-30 11:03:32 +02:00
|
|
|
push @{$final_config}, "!";
|
|
|
|
|
2020-01-16 09:15:15 +01:00
|
|
|
if (-e "/etc/frr/frr.conf.local") {
|
2020-01-16 11:14:53 +01:00
|
|
|
my $local_conf = file_get_contents("/etc/frr/frr.conf.local");
|
2022-08-24 10:56:45 +02:00
|
|
|
parse_merge_frr_local_config($config, $local_conf);
|
2020-01-16 09:15:15 +01:00
|
|
|
}
|
2019-09-30 11:03:32 +02:00
|
|
|
|
2023-04-20 23:36:19 +02:00
|
|
|
generate_frr_vrf($final_config, $config->{frr}->{vrf});
|
2022-08-24 10:56:45 +02:00
|
|
|
generate_frr_recurse($final_config, $config->{frr}, undef, 0);
|
2022-11-30 16:18:59 +01:00
|
|
|
generate_frr_list($final_config, $config->{frr_access_list}, "access-list");
|
|
|
|
generate_frr_list($final_config, $config->{frr_prefix_list}, "ip prefix-list");
|
2022-08-24 10:56:45 +02:00
|
|
|
generate_frr_routemap($final_config, $config->{frr_routemap});
|
2023-04-20 23:36:19 +02:00
|
|
|
generate_frr_ip_protocol($final_config, $config->{frr_ip_protocol});
|
2022-08-24 10:56:45 +02:00
|
|
|
|
2019-09-30 11:03:32 +02:00
|
|
|
push @{$final_config}, "!";
|
|
|
|
push @{$final_config}, "line vty";
|
|
|
|
push @{$final_config}, "!";
|
|
|
|
|
|
|
|
my $rawconfig = join("\n", @{$final_config});
|
|
|
|
|
2020-12-09 16:11:18 +01:00
|
|
|
return if !$rawconfig;
|
|
|
|
return $rawconfig;
|
|
|
|
}
|
|
|
|
|
2022-08-24 10:56:45 +02:00
|
|
|
sub parse_merge_frr_local_config {
|
|
|
|
my ($config, $local_conf) = @_;
|
|
|
|
|
|
|
|
my $section = \$config->{""};
|
|
|
|
my $router = undef;
|
|
|
|
my $routemap = undef;
|
|
|
|
my $routemap_config = ();
|
|
|
|
my $routemap_action = undef;
|
|
|
|
|
|
|
|
while ($local_conf =~ /^\s*(.+?)\s*$/gm) {
|
|
|
|
my $line = $1;
|
|
|
|
$line =~ s/^\s+|\s+$//g;
|
|
|
|
|
|
|
|
if ($line =~ m/^router (.+)$/) {
|
|
|
|
$router = $1;
|
|
|
|
$section = \$config->{'frr'}->{'router'}->{$router}->{""};
|
|
|
|
next;
|
|
|
|
} elsif ($line =~ m/^vrf (.+)$/) {
|
|
|
|
$section = \$config->{'frr'}->{'vrf'}->{$1};
|
|
|
|
next;
|
|
|
|
} elsif ($line =~ m/address-family (.+)$/) {
|
|
|
|
$section = \$config->{'frr'}->{'router'}->{$router}->{'address-family'}->{$1};
|
|
|
|
next;
|
|
|
|
} elsif ($line =~ m/^route-map (.+) (permit|deny) (\d+)/) {
|
|
|
|
$routemap = $1;
|
|
|
|
$routemap_config = ();
|
|
|
|
$routemap_action = $2;
|
|
|
|
$section = \$config->{'frr_routemap'}->{$routemap};
|
|
|
|
next;
|
|
|
|
} elsif ($line =~ m/^access-list (.+) seq (\d+) (.+)$/) {
|
|
|
|
$config->{'frr_access_list'}->{$1}->{$2} = $3;
|
|
|
|
next;
|
2022-11-30 16:18:59 +01:00
|
|
|
} elsif ($line =~ m/^ip prefix-list (.+) seq (\d+) (.*)$/) {
|
|
|
|
$config->{'frr_prefix_list'}->{$1}->{$2} = $3;
|
|
|
|
next;
|
2022-08-24 10:56:45 +02:00
|
|
|
} elsif($line =~ m/^exit-address-family$/) {
|
|
|
|
next;
|
|
|
|
} elsif($line =~ m/^exit$/) {
|
|
|
|
if($router) {
|
|
|
|
$section = \$config->{''};
|
|
|
|
$router = undef;
|
|
|
|
} elsif($routemap) {
|
|
|
|
push(@{$$section}, { rule => $routemap_config, action => $routemap_action });
|
|
|
|
$section = \$config->{''};
|
|
|
|
$routemap = undef;
|
|
|
|
$routemap_action = undef;
|
|
|
|
$routemap_config = ();
|
|
|
|
}
|
|
|
|
next;
|
|
|
|
} elsif($line =~ m/!/) {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
next if !$section;
|
|
|
|
if($routemap) {
|
|
|
|
push(@{$routemap_config}, $line);
|
|
|
|
} else {
|
|
|
|
push(@{$$section}, $line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 16:11:18 +01:00
|
|
|
sub write_controller_config {
|
|
|
|
my ($class, $plugin_config, $config) = @_;
|
|
|
|
|
|
|
|
my $rawconfig = $class->generate_controller_rawconfig($plugin_config, $config);
|
2019-09-30 11:03:32 +02:00
|
|
|
return if !$rawconfig;
|
|
|
|
return if !-d "/etc/frr";
|
|
|
|
|
2020-01-16 11:14:53 +01:00
|
|
|
file_set_contents("/etc/frr/frr.conf", $rawconfig);
|
2019-09-30 11:03:32 +02:00
|
|
|
}
|
|
|
|
|
2019-09-30 11:03:36 +02:00
|
|
|
sub reload_controller {
|
|
|
|
my ($class) = @_;
|
|
|
|
|
|
|
|
my $conf_file = "/etc/frr/frr.conf";
|
2019-11-26 10:00:30 +01:00
|
|
|
my $bin_path = "/usr/lib/frr/frr-reload.py";
|
|
|
|
|
|
|
|
if (!-e $bin_path) {
|
|
|
|
warn "missing $bin_path. Please install frr-pythontools package";
|
|
|
|
return;
|
|
|
|
}
|
2019-09-30 11:03:36 +02:00
|
|
|
|
|
|
|
my $err = sub {
|
|
|
|
my $line = shift;
|
2019-11-26 10:00:30 +01:00
|
|
|
if ($line =~ /ERROR:/) {
|
|
|
|
warn "$line \n";
|
2019-09-30 11:03:36 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (-e $conf_file && -e $bin_path) {
|
2022-02-23 08:11:43 +01:00
|
|
|
eval {
|
|
|
|
run_command([$bin_path, '--stdout', '--reload', $conf_file], outfunc => {}, errfunc => $err);
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
warn "frr reload command fail. Restarting frr.";
|
|
|
|
eval { run_command(['systemctl', 'restart', 'frr']); };
|
|
|
|
}
|
2019-09-30 11:03:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-30 11:03:32 +02:00
|
|
|
1;
|
2019-08-29 12:32:47 +02:00
|
|
|
|
2019-09-30 11:03:33 +02:00
|
|
|
|