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;
|
2019-11-26 10:00:16 +01:00
|
|
|
use PVE::Network::SDN::Controllers::Plugin;
|
2019-11-28 09:40:27 +01:00
|
|
|
use PVE::Tools;
|
2019-09-09 08:45:53 +02:00
|
|
|
use PVE::INotify;
|
|
|
|
use PVE::JSONSchema qw(get_standard_option);
|
2019-11-28 09:40:27 +01:00
|
|
|
use PVE::Network::SDN::Zones::Plugin;
|
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 {
|
|
|
|
'asn' => {
|
|
|
|
type => 'integer',
|
|
|
|
description => "autonomous system number",
|
|
|
|
},
|
|
|
|
'peers' => {
|
|
|
|
description => "peers address list.",
|
2019-09-13 12:33:44 +02:00
|
|
|
type => 'string', format => 'ip-list'
|
2019-08-29 12:32:47 +02:00
|
|
|
},
|
2019-09-09 08:45:53 +02:00
|
|
|
'gateway-nodes' => get_standard_option('pve-node-list'),
|
|
|
|
'gateway-external-peers' => {
|
|
|
|
description => "upstream bgp peers address list.",
|
2019-09-13 12:33:44 +02:00
|
|
|
type => 'string', format => 'ip-list'
|
2019-09-09 08:45:53 +02:00
|
|
|
},
|
2019-08-29 12:32:47 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub options {
|
|
|
|
|
|
|
|
return {
|
|
|
|
'asn' => { optional => 0 },
|
|
|
|
'peers' => { optional => 0 },
|
2019-09-09 08:45:53 +02:00
|
|
|
'gateway-nodes' => { optional => 1 },
|
|
|
|
'gateway-external-peers' => { optional => 1 },
|
2019-08-29 12:32:47 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
# Plugin implementation
|
2019-09-30 11:03:32 +02:00
|
|
|
sub generate_controller_config {
|
2019-11-26 10:00:29 +01:00
|
|
|
my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
|
2019-08-29 12:32:47 +02:00
|
|
|
|
|
|
|
my @peers = split(',', $plugin_config->{'peers'}) if $plugin_config->{'peers'};
|
|
|
|
|
2019-09-09 08:45:53 +02:00
|
|
|
my $asn = $plugin_config->{asn};
|
|
|
|
my $gatewaynodes = $plugin_config->{'gateway-nodes'};
|
|
|
|
my @gatewaypeers = split(',', $plugin_config->{'gateway-external-peers'}) if $plugin_config->{'gateway-external-peers'};
|
|
|
|
|
|
|
|
return if !$asn;
|
2019-08-29 12:32:47 +02:00
|
|
|
|
2019-11-28 09:40:27 +01:00
|
|
|
my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers);
|
2019-08-29 12:32:47 +02:00
|
|
|
|
2019-09-09 08:45:53 +02:00
|
|
|
my $is_gateway = undef;
|
|
|
|
my $local_node = PVE::INotify::nodename();
|
|
|
|
|
|
|
|
foreach my $gatewaynode (PVE::Tools::split_list($gatewaynodes)) {
|
|
|
|
$is_gateway = 1 if $gatewaynode eq $local_node;
|
|
|
|
}
|
2019-09-09 08:45:50 +02:00
|
|
|
|
2019-11-26 10:00:29 +01:00
|
|
|
my @controller_config = ();
|
2019-08-29 12:32:50 +02:00
|
|
|
|
2019-11-26 10:00:29 +01:00
|
|
|
push @controller_config, "bgp router-id $ifaceip";
|
|
|
|
push @controller_config, "no bgp default ipv4-unicast";
|
|
|
|
push @controller_config, "coalesce-time 1000";
|
2019-08-29 12:32:47 +02:00
|
|
|
|
|
|
|
foreach my $address (@peers) {
|
|
|
|
next if $address eq $ifaceip;
|
2019-11-26 10:00:29 +01:00
|
|
|
push @controller_config, "neighbor $address remote-as $asn";
|
2019-09-03 08:25:00 +02:00
|
|
|
}
|
2019-09-09 08:45:53 +02:00
|
|
|
|
|
|
|
if ($is_gateway) {
|
|
|
|
foreach my $address (@gatewaypeers) {
|
2019-11-26 10:00:29 +01:00
|
|
|
push @controller_config, "neighbor $address remote-as external";
|
2019-09-09 08:45:53 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-26 10:00:29 +01:00
|
|
|
push(@{$config->{frr}->{router}->{"bgp $asn"}->{""}}, @controller_config);
|
2019-09-09 08:45:53 +02:00
|
|
|
|
2019-11-26 10:00:29 +01:00
|
|
|
@controller_config = ();
|
2019-08-29 12:32:47 +02:00
|
|
|
foreach my $address (@peers) {
|
|
|
|
next if $address eq $ifaceip;
|
2019-11-26 10:00:29 +01:00
|
|
|
push @controller_config, "neighbor $address activate";
|
2019-08-29 12:32:47 +02:00
|
|
|
}
|
2019-11-26 10:00:29 +01:00
|
|
|
push @controller_config, "advertise-all-vni";
|
|
|
|
push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
|
2019-08-29 12:32:47 +02:00
|
|
|
|
2019-09-09 08:45:53 +02:00
|
|
|
if ($is_gateway) {
|
|
|
|
|
2019-11-26 10:00:29 +01:00
|
|
|
@controller_config = ();
|
2019-09-09 08:45:53 +02:00
|
|
|
#import /32 routes of evpn network from vrf1 to default vrf (for packet return)
|
|
|
|
foreach my $address (@gatewaypeers) {
|
2019-11-26 10:00:29 +01:00
|
|
|
push @controller_config, "neighbor $address activate";
|
2019-09-09 08:45:53 +02:00
|
|
|
}
|
2019-11-26 10:00:29 +01:00
|
|
|
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-09 08:45:53 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-29 12:32:47 +02:00
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
2019-11-26 10:00:29 +01:00
|
|
|
sub generate_controller_zone_config {
|
|
|
|
my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
|
2019-09-30 11:03:33 +02:00
|
|
|
|
2019-11-26 10:00:28 +01:00
|
|
|
my $vrf = $id;
|
2019-09-30 11:03:33 +02:00
|
|
|
my $vrfvxlan = $plugin_config->{'vrf-vxlan'};
|
2019-11-26 10:00:29 +01:00
|
|
|
my $asn = $controller->{asn};
|
|
|
|
my $gatewaynodes = $controller->{'gateway-nodes'};
|
2019-09-30 11:03:33 +02:00
|
|
|
|
|
|
|
return if !$vrf || !$vrfvxlan || !$asn;
|
|
|
|
|
|
|
|
#vrf
|
2019-11-26 10:00:29 +01:00
|
|
|
my @controller_config = ();
|
|
|
|
push @controller_config, "vni $vrfvxlan";
|
|
|
|
push(@{$config->{frr}->{vrf}->{"$vrf"}}, @controller_config);
|
2019-09-30 11:03:33 +02:00
|
|
|
|
2019-11-26 10:00:30 +01:00
|
|
|
push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{""}}, "!");
|
|
|
|
|
2019-11-26 10:00:29 +01:00
|
|
|
@controller_config = ();
|
2019-09-30 11:03:33 +02:00
|
|
|
|
|
|
|
my $is_gateway = undef;
|
|
|
|
my $local_node = PVE::INotify::nodename();
|
|
|
|
|
|
|
|
foreach my $gatewaynode (PVE::Tools::split_list($gatewaynodes)) {
|
|
|
|
$is_gateway = 1 if $gatewaynode eq $local_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_gateway) {
|
|
|
|
|
2019-11-26 10:00:29 +01:00
|
|
|
@controller_config = ();
|
2019-09-30 11:03:33 +02:00
|
|
|
#import /32 routes of evpn network from vrf1 to default vrf (for packet return)
|
2019-11-26 10:00:29 +01:00
|
|
|
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
|
|
|
|
2019-11-26 10:00:29 +01:00
|
|
|
@controller_config = ();
|
2019-09-30 11:03:33 +02:00
|
|
|
#redistribute connected to be able to route to local vms on the gateway
|
2019-11-26 10:00:29 +01:00
|
|
|
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);
|
2019-09-30 11:03:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
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}}) {
|
|
|
|
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
|
|
|
|
2019-11-26 10:00:29 +01:00
|
|
|
# verify that asn is not already used by another controller
|
|
|
|
my $asn = $controller_cfg->{ids}->{$controllerid}->{asn};
|
|
|
|
foreach my $id (keys %{$controller_cfg->{ids}}) {
|
|
|
|
next if $id eq $controllerid;
|
|
|
|
my $controller = $controller_cfg->{ids}->{$id};
|
2019-08-29 12:32:52 +02:00
|
|
|
die "asn $asn is already used by $id"
|
2019-11-26 10:00:29 +01:00
|
|
|
if (defined($controller->{asn}) && $controller->{asn} eq $asn);
|
2019-08-29 12:32:52 +02:00
|
|
|
}
|
2019-08-29 12:32:47 +02:00
|
|
|
}
|
|
|
|
|
2019-09-30 11:03:32 +02:00
|
|
|
sub sort_frr_config {
|
|
|
|
my $order = {};
|
|
|
|
$order->{''} = 0;
|
|
|
|
$order->{'vrf'} = 1;
|
|
|
|
$order->{'ipv4 unicast'} = 1;
|
|
|
|
$order->{'ipv6 unicast'} = 2;
|
|
|
|
$order->{'l2vpn evpn'} = 3;
|
|
|
|
|
|
|
|
my $a_val = 100;
|
|
|
|
my $b_val = 100;
|
|
|
|
|
|
|
|
$a_val = $order->{$a} if defined($order->{$a});
|
|
|
|
$b_val = $order->{$b} if defined($order->{$b});
|
|
|
|
|
|
|
|
if($a =~ /bgp (\d+)$/) {
|
|
|
|
$a_val = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($b =~ /bgp (\d+)$/) {
|
|
|
|
$b_val = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $a_val <=> $b_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub generate_frr_recurse{
|
|
|
|
my ($final_config, $content, $parentkey, $level) = @_;
|
|
|
|
|
|
|
|
my $keylist = {};
|
|
|
|
$keylist->{vrf} = 1;
|
|
|
|
$keylist->{'address-family'} = 1;
|
|
|
|
$keylist->{router} = 1;
|
|
|
|
|
|
|
|
my $exitkeylist = {};
|
|
|
|
$exitkeylist->{vrf} = 1;
|
|
|
|
$exitkeylist->{'address-family'} = 1;
|
|
|
|
|
|
|
|
#fix me, make this generic
|
|
|
|
my $paddinglevel = undef;
|
|
|
|
if($level == 1 || $level == 2) {
|
|
|
|
$paddinglevel = $level - 1;
|
|
|
|
} elsif ($level == 3 || $level == 4) {
|
|
|
|
$paddinglevel = $level - 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $padding = "";
|
|
|
|
$padding = ' ' x ($paddinglevel) if $paddinglevel;
|
|
|
|
|
|
|
|
if (ref $content eq ref {}) {
|
|
|
|
foreach my $key (sort sort_frr_config keys %$content) {
|
|
|
|
if ($parentkey && defined($keylist->{$parentkey})) {
|
|
|
|
push @{$final_config}, $padding."!";
|
|
|
|
push @{$final_config}, $padding."$parentkey $key";
|
|
|
|
} else {
|
|
|
|
push @{$final_config}, $padding."$key" if $key ne '' && !defined($keylist->{$key});
|
|
|
|
}
|
|
|
|
|
|
|
|
my $option = $content->{$key};
|
|
|
|
generate_frr_recurse($final_config, $option, $key, $level+1);
|
|
|
|
|
|
|
|
push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
|
|
|
|
}
|
|
|
|
}
|
2019-08-29 12:32:47 +02:00
|
|
|
|
2019-09-30 11:03:32 +02:00
|
|
|
if (ref $content eq 'ARRAY') {
|
|
|
|
foreach my $value (@$content) {
|
|
|
|
push @{$final_config}, $padding."$value";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub write_controller_config {
|
|
|
|
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 = [];
|
|
|
|
push @{$final_config}, "log syslog informational";
|
2019-11-26 10:00:30 +01:00
|
|
|
push @{$final_config}, "ip forwarding";
|
|
|
|
push @{$final_config}, "ipv6 forwarding";
|
|
|
|
push @{$final_config}, "frr defaults traditional";
|
|
|
|
push @{$final_config}, "service integrated-vtysh-config";
|
|
|
|
push @{$final_config}, "hostname $nodename";
|
2019-09-30 11:03:32 +02:00
|
|
|
push @{$final_config}, "!";
|
|
|
|
|
|
|
|
generate_frr_recurse($final_config, $config->{frr}, undef, 0);
|
|
|
|
|
|
|
|
push @{$final_config}, "!";
|
|
|
|
push @{$final_config}, "line vty";
|
|
|
|
push @{$final_config}, "!";
|
|
|
|
|
|
|
|
my $rawconfig = join("\n", @{$final_config});
|
|
|
|
|
|
|
|
|
|
|
|
return if !$rawconfig;
|
|
|
|
return if !-d "/etc/frr";
|
|
|
|
|
|
|
|
my $frr_config_file = "/etc/frr/frr.conf";
|
|
|
|
|
|
|
|
my $writefh = IO::File->new($frr_config_file,">");
|
|
|
|
print $writefh $rawconfig;
|
|
|
|
$writefh->close();
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-11-26 10:00:30 +01:00
|
|
|
PVE::Tools::run_command([$bin_path, '--stdout', '--reload', $conf_file], outfunc => {}, errfunc => $err);
|
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
|
|
|
|