2019-08-29 12:32:47 +02:00
|
|
|
package PVE::Network::SDN::FrrPlugin;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use PVE::Network::SDN::Plugin;
|
|
|
|
use PVE::Tools;
|
2019-09-09 08:45:53 +02:00
|
|
|
use PVE::INotify;
|
|
|
|
use PVE::JSONSchema qw(get_standard_option);
|
2019-08-29 12:32:47 +02:00
|
|
|
|
|
|
|
use base('PVE::Network::SDN::Plugin');
|
|
|
|
|
|
|
|
sub type {
|
|
|
|
return 'frr';
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
'uplink-id' => { optional => 0 },
|
|
|
|
'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-08-29 12:32:51 +02:00
|
|
|
sub generate_frr_config {
|
2019-09-09 08:45:53 +02:00
|
|
|
my ($class, $plugin_config, $router, $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};
|
2019-08-29 12:32:47 +02:00
|
|
|
my $uplink = $plugin_config->{'uplink-id'};
|
2019-09-09 08:45:53 +02:00
|
|
|
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
|
|
|
|
|
|
|
my $iface = "uplink$uplink";
|
|
|
|
my $ifaceip = "";
|
|
|
|
|
|
|
|
if($uplinks->{$uplink}->{name}) {
|
|
|
|
$iface = $uplinks->{$uplink}->{name};
|
2019-08-29 12:32:51 +02:00
|
|
|
$ifaceip = PVE::Network::SDN::Plugin::get_first_local_ipv4_from_interface($iface);
|
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-08-29 12:32:50 +02:00
|
|
|
my @router_config = ();
|
|
|
|
|
|
|
|
push @router_config, "bgp router-id $ifaceip";
|
2019-09-09 08:45:52 +02:00
|
|
|
push @router_config, "no bgp default ipv4-unicast";
|
2019-08-29 12:32:50 +02:00
|
|
|
push @router_config, "coalesce-time 1000";
|
2019-08-29 12:32:47 +02:00
|
|
|
|
|
|
|
foreach my $address (@peers) {
|
|
|
|
next if $address eq $ifaceip;
|
2019-08-29 12:32:50 +02:00
|
|
|
push @router_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) {
|
|
|
|
push @router_config, "neighbor $address remote-as external";
|
|
|
|
}
|
|
|
|
}
|
2019-09-09 08:45:50 +02:00
|
|
|
push(@{$config->{router}->{"bgp $asn"}->{""}}, @router_config);
|
2019-09-09 08:45:53 +02:00
|
|
|
|
2019-09-09 08:45:50 +02:00
|
|
|
@router_config = ();
|
2019-08-29 12:32:47 +02:00
|
|
|
foreach my $address (@peers) {
|
|
|
|
next if $address eq $ifaceip;
|
2019-09-09 08:45:50 +02:00
|
|
|
push @router_config, "neighbor $address activate";
|
2019-08-29 12:32:47 +02:00
|
|
|
}
|
2019-09-09 08:45:50 +02:00
|
|
|
push @router_config, "advertise-all-vni";
|
|
|
|
push(@{$config->{router}->{"bgp $asn"}->{"address-family"}->{"l2vpn evpn"}}, @router_config);
|
2019-08-29 12:32:47 +02:00
|
|
|
|
2019-09-09 08:45:53 +02:00
|
|
|
if ($is_gateway) {
|
|
|
|
|
|
|
|
@router_config = ();
|
|
|
|
#import /32 routes of evpn network from vrf1 to default vrf (for packet return)
|
|
|
|
#frr 7.1 tag is bugged -> works fine with 7.1 stable branch(20190829-02-g6ba76bbc1)
|
|
|
|
#https://github.com/FRRouting/frr/issues/4905
|
|
|
|
foreach my $address (@gatewaypeers) {
|
|
|
|
push @router_config, "neighbor $address activate";
|
|
|
|
}
|
|
|
|
push(@{$config->{router}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @router_config);
|
|
|
|
push(@{$config->{router}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @router_config);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-29 12:32:47 +02:00
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub on_delete_hook {
|
2019-08-29 12:32:52 +02:00
|
|
|
my ($class, $routerid, $sdn_cfg) = @_;
|
2019-08-29 12:32:47 +02:00
|
|
|
|
2019-08-29 12:32:52 +02:00
|
|
|
# verify that transport is associated to this router
|
|
|
|
foreach my $id (keys %{$sdn_cfg->{ids}}) {
|
|
|
|
my $sdn = $sdn_cfg->{ids}->{$id};
|
|
|
|
die "router $routerid is used by $id"
|
|
|
|
if (defined($sdn->{router}) && $sdn->{router} eq $routerid);
|
|
|
|
}
|
2019-08-29 12:32:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub on_update_hook {
|
2019-08-29 12:32:52 +02:00
|
|
|
my ($class, $routerid, $sdn_cfg) = @_;
|
|
|
|
|
|
|
|
# verify that asn is not already used by another router
|
|
|
|
my $asn = $sdn_cfg->{ids}->{$routerid}->{asn};
|
|
|
|
foreach my $id (keys %{$sdn_cfg->{ids}}) {
|
|
|
|
next if $id eq $routerid;
|
|
|
|
my $sdn = $sdn_cfg->{ids}->{$id};
|
|
|
|
die "asn $asn is already used by $id"
|
|
|
|
if (defined($sdn->{asn}) && $sdn->{asn} eq $asn);
|
|
|
|
}
|
2019-08-29 12:32:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
|
|
|