]> git.proxmox.com Git - pve-network.git/blame - PVE/Network/SDN/Controllers/BgpPlugin.pm
controllers: evpn: fix multiple exit-nodes with route-map filtering
[pve-network.git] / PVE / Network / SDN / Controllers / BgpPlugin.pm
CommitLineData
f23633dc
AD
1package PVE::Network::SDN::Controllers::BgpPlugin;
2
3use strict;
4use warnings;
5
6use PVE::INotify;
7use PVE::JSONSchema qw(get_standard_option);
8use PVE::Tools qw(run_command file_set_contents file_get_contents);
9
10use PVE::Network::SDN::Controllers::Plugin;
11use PVE::Network::SDN::Zones::Plugin;
12use Net::IP;
13
14use base('PVE::Network::SDN::Controllers::Plugin');
15
16sub type {
17 return 'bgp';
18}
19
20sub properties {
21 return {
1262519c
AD
22 'bgp-multipath-as-path-relax' => {
23 type => 'boolean',
24 optional => 1,
25 },
f23633dc
AD
26 ebgp => {
27 type => 'boolean',
28 optional => 1,
29 description => "Enable ebgp. (remote-as external)",
30 },
4083537b
AD
31 'ebgp-multihop' => {
32 type => 'integer',
33 optional => 1,
34 },
f23633dc
AD
35 loopback => {
36 description => "source loopback interface.",
37 type => 'string'
38 },
39 node => get_standard_option('pve-node'),
40 };
41}
42
43sub options {
44 return {
45 'node' => { optional => 0 },
46 'asn' => { optional => 0 },
47 'peers' => { optional => 0 },
1262519c 48 'bgp-multipath-as-path-relax' => { optional => 1 },
f23633dc 49 'ebgp' => { optional => 1 },
4083537b 50 'ebgp-multihop' => { optional => 1 },
f23633dc
AD
51 'loopback' => { optional => 1 },
52 };
53}
54
55# Plugin implementation
56sub generate_controller_config {
57 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
58
59 my @peers;
60 @peers = PVE::Tools::split_list($plugin_config->{'peers'}) if $plugin_config->{'peers'};
61
62 my $asn = $plugin_config->{asn};
63 my $ebgp = $plugin_config->{ebgp};
4083537b 64 my $ebgp_multihop = $plugin_config->{'ebgp-multihop'};
f23633dc 65 my $loopback = $plugin_config->{loopback};
1262519c
AD
66 my $multipath_relax = $plugin_config->{'bgp-multipath-as-path-relax'};
67
f23633dc
AD
68 my $local_node = PVE::INotify::nodename();
69
70
71 return if !$asn;
72 return if $local_node ne $plugin_config->{node};
73
74 my $bgp = $config->{frr}->{router}->{"bgp $asn"} //= {};
75
76 my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers, $loopback);
77
78 my $remoteas = $ebgp ? "external" : $asn;
79
80 #global options
81 my @controller_config = (
82 "bgp router-id $ifaceip",
83 "no bgp default ipv4-unicast",
3a46bcb1 84 "coalesce-time 1000"
f23633dc
AD
85 );
86
87 push(@{$bgp->{""}}, @controller_config) if keys %{$bgp} == 0;
88
89 @controller_config = ();
90 if($ebgp) {
f23633dc
AD
91 push @controller_config, "bgp disable-ebgp-connected-route-check" if $loopback;
92 }
93
1262519c
AD
94 push @controller_config, "bgp bestpath as-path multipath-relax" if $multipath_relax;
95
f23633dc
AD
96 #BGP neighbors
97 if(@peers) {
98 push @controller_config, "neighbor BGP peer-group";
99 push @controller_config, "neighbor BGP remote-as $remoteas";
100 push @controller_config, "neighbor BGP bfd";
4083537b 101 push @controller_config, "neighbor BGP ebgp-multihop $ebgp_multihop" if $ebgp && $ebgp_multihop;
f23633dc
AD
102 }
103
104 # BGP peers
105 foreach my $address (@peers) {
106 push @controller_config, "neighbor $address peer-group BGP";
107 }
108 push(@{$bgp->{""}}, @controller_config);
109
110 # address-family unicast
111 if (@peers) {
112 my $ipversion = Net::IP::ip_is_ipv6($ifaceip) ? "ipv6" : "ipv4";
113 my $mask = Net::IP::ip_is_ipv6($ifaceip) ? "/128" : "32";
114
115 push(@{$bgp->{"address-family"}->{"$ipversion unicast"}}, "network $ifaceip/$mask") if $loopback;
116 push(@{$bgp->{"address-family"}->{"$ipversion unicast"}}, "neighbor BGP activate");
117 push(@{$bgp->{"address-family"}->{"$ipversion unicast"}}, "neighbor BGP soft-reconfiguration inbound");
118 }
119
bbf4e4b1
AD
120 if ($loopback) {
121 push(@{$config->{frr}->{''}}, "ip prefix-list loopbacks_ips seq 10 permit 0.0.0.0/0 le 32");
122 push(@{$config->{frr}->{''}}, "ip protocol bgp route-map correct_src");
847f5144 123
916488cc 124 my $routemap_config = ();
847f5144
AD
125 push @{$routemap_config}, "match ip address prefix-list loopbacks_ips";
126 push @{$routemap_config}, "set src $ifaceip";
916488cc
AD
127 my $routemap = { rule => $routemap_config, action => "permit" };
128 push(@{$config->{frr_routemap}->{'correct_src'}}, $routemap);
bbf4e4b1
AD
129 }
130
f23633dc
AD
131 return $config;
132}
133
134sub generate_controller_zone_config {
135 my ($class, $plugin_config, $controller, $controller_cfg, $id, $uplinks, $config) = @_;
136
137}
138
139sub on_delete_hook {
140 my ($class, $controllerid, $zone_cfg) = @_;
141
142 # verify that zone is associated to this controller
143 foreach my $id (keys %{$zone_cfg->{ids}}) {
144 my $zone = $zone_cfg->{ids}->{$id};
145 die "controller $controllerid is used by $id"
146 if (defined($zone->{controller}) && $zone->{controller} eq $controllerid);
147 }
148}
149
150sub on_update_hook {
151 my ($class, $controllerid, $controller_cfg) = @_;
152
153 # we can only have 1 bgp controller by node
154 my $local_node = PVE::INotify::nodename();
155 my $controllernb = 0;
156 foreach my $id (keys %{$controller_cfg->{ids}}) {
157 next if $id eq $controllerid;
158 my $controller = $controller_cfg->{ids}->{$id};
159 next if $controller->{type} ne "bgp";
160 next if $controller->{node} ne $local_node;
161 $controllernb++;
162 die "only 1 bgp controller can be defined" if $controllernb > 1;
163 }
164}
165
9cef13e9
AD
166sub generate_controller_rawconfig {
167 my ($class, $plugin_config, $config) = @_;
168 return "";
169}
170
f23633dc
AD
171sub write_controller_config {
172 my ($class, $plugin_config, $config) = @_;
173 return;
174}
175
176sub reload_controller {
177 my ($class) = @_;
178 return;
179}
180
1811;
182
183