]> git.proxmox.com Git - pve-network.git/blame - PVE/Network/SDN/Controllers/EvpnPlugin.pm
evpn: indentation, whitespace cleanup and code refactoring
[pve-network.git] / PVE / Network / SDN / Controllers / EvpnPlugin.pm
CommitLineData
fa253735 1package PVE::Network::SDN::Controllers::EvpnPlugin;
32602a38
AD
2
3use strict;
4use warnings;
f5eabba0 5use PVE::Network::SDN::Controllers::Plugin;
1f543c5f 6use PVE::Tools;
074d270b
AD
7use PVE::INotify;
8use PVE::JSONSchema qw(get_standard_option);
1f543c5f 9use PVE::Network::SDN::Zones::Plugin;
f5eabba0 10use base('PVE::Network::SDN::Controllers::Plugin');
32602a38
AD
11
12sub type {
fa253735 13 return 'evpn';
8fb1ee7f
AD
14}
15
32602a38
AD
16sub properties {
17 return {
92526f0e
TL
18 asn => {
19 type => 'integer',
20 description => "autonomous system number",
21 },
22 peers => {
23 description => "peers address list.",
24 type => 'string', format => 'ip-list'
25 },
074d270b 26 'gateway-nodes' => get_standard_option('pve-node-list'),
92526f0e
TL
27 'gateway-external-peers' => {
28 description => "upstream bgp peers address list.",
29 type => 'string', format => 'ip-list'
30 },
32602a38
AD
31 };
32}
33
34sub options {
32602a38 35 return {
92526f0e
TL
36 'asn' => { optional => 0 },
37 'peers' => { optional => 0 },
074d270b
AD
38 'gateway-nodes' => { optional => 1 },
39 'gateway-external-peers' => { optional => 1 },
32602a38
AD
40 };
41}
42
43# Plugin implementation
8fb1ee7f 44sub generate_controller_config {
56cdcac9 45 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
32602a38 46
32602a38
AD
47 my @peers = split(',', $plugin_config->{'peers'}) if $plugin_config->{'peers'};
48
074d270b 49 my $asn = $plugin_config->{asn};
074d270b
AD
50 my $gatewaynodes = $plugin_config->{'gateway-nodes'};
51 my @gatewaypeers = split(',', $plugin_config->{'gateway-external-peers'}) if $plugin_config->{'gateway-external-peers'};
52
53 return if !$asn;
32602a38 54
92526f0e
TL
55 my $bgp = $config->{frr}->{router}->{"bgp $asn"} //= {};
56
1f543c5f 57 my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers);
32602a38 58
074d270b
AD
59 my $is_gateway = undef;
60 my $local_node = PVE::INotify::nodename();
61
62 foreach my $gatewaynode (PVE::Tools::split_list($gatewaynodes)) {
92526f0e 63 $is_gateway = 1 if $gatewaynode eq $local_node;
074d270b 64 }
17854295 65
92526f0e
TL
66 my @controller_config = (
67 "bgp router-id $ifaceip",
68 "no bgp default ipv4-unicast",
69 "coalesce-time 1000",
70 );
32602a38
AD
71
72 foreach my $address (@peers) {
73 next if $address eq $ifaceip;
56cdcac9 74 push @controller_config, "neighbor $address remote-as $asn";
7d35eaf5 75 }
074d270b
AD
76
77 if ($is_gateway) {
78 foreach my $address (@gatewaypeers) {
56cdcac9 79 push @controller_config, "neighbor $address remote-as external";
074d270b
AD
80 }
81 }
92526f0e 82 push(@{$bgp->{""}}, @controller_config);
074d270b 83
56cdcac9 84 @controller_config = ();
32602a38
AD
85 foreach my $address (@peers) {
86 next if $address eq $ifaceip;
56cdcac9 87 push @controller_config, "neighbor $address activate";
32602a38 88 }
56cdcac9 89 push @controller_config, "advertise-all-vni";
92526f0e 90 push(@{$bgp->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
32602a38 91
074d270b 92 if ($is_gateway) {
92526f0e
TL
93 # import /32 routes of evpn network from vrf1 to default vrf (for packet return)
94 @controller_config = map { "neighbor $_ activate" } @gatewaypeers;
074d270b 95
92526f0e
TL
96 push(@{$bgp->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
97 push(@{$bgp->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
074d270b
AD
98 }
99
32602a38
AD
100 return $config;
101}
102
56cdcac9
AD
103sub generate_controller_zone_config {
104 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
0589eb09 105
7cb9714d 106 my $vrf = $id;
0589eb09 107 my $vrfvxlan = $plugin_config->{'vrf-vxlan'};
56cdcac9
AD
108 my $asn = $controller->{asn};
109 my $gatewaynodes = $controller->{'gateway-nodes'};
0589eb09
AD
110
111 return if !$vrf || !$vrfvxlan || !$asn;
112
92526f0e 113 # vrf
56cdcac9
AD
114 my @controller_config = ();
115 push @controller_config, "vni $vrfvxlan";
116 push(@{$config->{frr}->{vrf}->{"$vrf"}}, @controller_config);
0589eb09 117
659c27c2
AD
118 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{""}}, "!");
119
0589eb09
AD
120 my $local_node = PVE::INotify::nodename();
121
92526f0e 122 my $is_gateway = grep { $_ eq $local_node } PVE::Tools::split_list($gatewaynodes);
0589eb09
AD
123 if ($is_gateway) {
124
56cdcac9 125 @controller_config = ();
0589eb09 126 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
56cdcac9
AD
127 push @controller_config, "import vrf $vrf";
128 push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
129 push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
0589eb09 130
56cdcac9 131 @controller_config = ();
0589eb09 132 #redistribute connected to be able to route to local vms on the gateway
56cdcac9
AD
133 push @controller_config, "redistribute connected";
134 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
135 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
0589eb09 136
56cdcac9 137 @controller_config = ();
0589eb09 138 #add default originate to announce 0.0.0.0/0 type5 route in evpn
56cdcac9
AD
139 push @controller_config, "default-originate ipv4";
140 push @controller_config, "default-originate ipv6";
141 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
0589eb09
AD
142 }
143
144 return $config;
145}
146
32602a38 147sub on_delete_hook {
56cdcac9 148 my ($class, $controllerid, $zone_cfg) = @_;
32602a38 149
56cdcac9
AD
150 # verify that zone is associated to this controller
151 foreach my $id (keys %{$zone_cfg->{ids}}) {
92526f0e
TL
152 my $zone = $zone_cfg->{ids}->{$id};
153 die "controller $controllerid is used by $id"
154 if (defined($zone->{controller}) && $zone->{controller} eq $controllerid);
5bda8607 155 }
32602a38
AD
156}
157
158sub on_update_hook {
56cdcac9 159 my ($class, $controllerid, $controller_cfg) = @_;
5bda8607 160
c7bb4ac5
AD
161 # we can only have 1 evpn controller / 1 asn by server
162
56cdcac9
AD
163 foreach my $id (keys %{$controller_cfg->{ids}}) {
164 next if $id eq $controllerid;
92526f0e
TL
165 my $controller = $controller_cfg->{ids}->{$id};
166 die "only 1 evpn controller can be defined" if $controller->{type} eq "evpn";
5bda8607 167 }
32602a38
AD
168}
169
8fb1ee7f
AD
170sub sort_frr_config {
171 my $order = {};
172 $order->{''} = 0;
173 $order->{'vrf'} = 1;
174 $order->{'ipv4 unicast'} = 1;
175 $order->{'ipv6 unicast'} = 2;
176 $order->{'l2vpn evpn'} = 3;
177
178 my $a_val = 100;
179 my $b_val = 100;
180
181 $a_val = $order->{$a} if defined($order->{$a});
182 $b_val = $order->{$b} if defined($order->{$b});
183
92526f0e 184 if ($a =~ /bgp (\d+)$/) {
8fb1ee7f
AD
185 $a_val = 2;
186 }
187
92526f0e 188 if ($b =~ /bgp (\d+)$/) {
8fb1ee7f
AD
189 $b_val = 2;
190 }
191
192 return $a_val <=> $b_val;
193}
194
195sub generate_frr_recurse{
196 my ($final_config, $content, $parentkey, $level) = @_;
197
198 my $keylist = {};
199 $keylist->{vrf} = 1;
200 $keylist->{'address-family'} = 1;
201 $keylist->{router} = 1;
202
203 my $exitkeylist = {};
204 $exitkeylist->{vrf} = 1;
205 $exitkeylist->{'address-family'} = 1;
206
92526f0e 207 # FIXME: make this generic
8fb1ee7f 208 my $paddinglevel = undef;
92526f0e
TL
209 if ($level == 1 || $level == 2) {
210 $paddinglevel = $level - 1;
8fb1ee7f 211 } elsif ($level == 3 || $level == 4) {
92526f0e 212 $paddinglevel = $level - 2;
8fb1ee7f
AD
213 }
214
215 my $padding = "";
216 $padding = ' ' x ($paddinglevel) if $paddinglevel;
217
92526f0e 218 if (ref $content eq 'HASH') {
8fb1ee7f
AD
219 foreach my $key (sort sort_frr_config keys %$content) {
220 if ($parentkey && defined($keylist->{$parentkey})) {
92526f0e
TL
221 push @{$final_config}, $padding."!";
222 push @{$final_config}, $padding."$parentkey $key";
223 } elsif ($key ne '' && !defined($keylist->{$key})) {
224 push @{$final_config}, $padding."$key";
8fb1ee7f
AD
225 }
226
227 my $option = $content->{$key};
228 generate_frr_recurse($final_config, $option, $key, $level+1);
229
230 push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
231 }
232 }
32602a38 233
8fb1ee7f 234 if (ref $content eq 'ARRAY') {
92526f0e 235 push @{$final_config}, map { $padding . "$_" } @$content;
8fb1ee7f
AD
236 }
237}
238
239sub write_controller_config {
240 my ($class, $plugin_config, $config) = @_;
241
659c27c2
AD
242 my $nodename = PVE::INotify::nodename();
243
8fb1ee7f
AD
244 my $final_config = [];
245 push @{$final_config}, "log syslog informational";
659c27c2
AD
246 push @{$final_config}, "ip forwarding";
247 push @{$final_config}, "ipv6 forwarding";
248 push @{$final_config}, "frr defaults traditional";
249 push @{$final_config}, "service integrated-vtysh-config";
250 push @{$final_config}, "hostname $nodename";
8fb1ee7f
AD
251 push @{$final_config}, "!";
252
0d1ab7dc
AD
253 if (-e "/etc/frr/frr.conf.local") {
254 open my $fh, '<', '/etc/frr/frr.conf.local' or die "Can't open file $!";
255 generate_frr_recurse($final_config, $config->{frr}->{vrf}, "vrf", 1);
256 push @{$final_config}, "!";
257
258 while (my $line = <$fh>) {
259 chomp ($line);
260 push @{$final_config}, $line;
261 }
262 } else {
263 generate_frr_recurse($final_config, $config->{frr}, undef, 0);
264 }
8fb1ee7f
AD
265
266 push @{$final_config}, "!";
267 push @{$final_config}, "line vty";
268 push @{$final_config}, "!";
269
270 my $rawconfig = join("\n", @{$final_config});
271
272
273 return if !$rawconfig;
274 return if !-d "/etc/frr";
275
276 my $frr_config_file = "/etc/frr/frr.conf";
277
278 my $writefh = IO::File->new($frr_config_file,">");
279 print $writefh $rawconfig;
280 $writefh->close();
281}
282
fa609bdd
AD
283sub reload_controller {
284 my ($class) = @_;
285
286 my $conf_file = "/etc/frr/frr.conf";
659c27c2
AD
287 my $bin_path = "/usr/lib/frr/frr-reload.py";
288
289 if (!-e $bin_path) {
290 warn "missing $bin_path. Please install frr-pythontools package";
291 return;
292 }
fa609bdd
AD
293
294 my $err = sub {
295 my $line = shift;
659c27c2
AD
296 if ($line =~ /ERROR:/) {
297 warn "$line \n";
fa609bdd
AD
298 }
299 };
300
301 if (-e $conf_file && -e $bin_path) {
659c27c2 302 PVE::Tools::run_command([$bin_path, '--stdout', '--reload', $conf_file], outfunc => {}, errfunc => $err);
fa609bdd
AD
303 }
304}
305
8fb1ee7f 3061;
32602a38 307
0589eb09 308