]>
Commit | Line | Data |
---|---|---|
fa253735 | 1 | package PVE::Network::SDN::Controllers::EvpnPlugin; |
32602a38 AD |
2 | |
3 | use strict; | |
4 | use warnings; | |
cdf2c819 | 5 | |
074d270b AD |
6 | use PVE::INotify; |
7 | use PVE::JSONSchema qw(get_standard_option); | |
cdf2c819 TL |
8 | use PVE::Tools qw(run_command file_set_contents file_get_contents); |
9 | ||
10 | use PVE::Network::SDN::Controllers::Plugin; | |
1f543c5f | 11 | use PVE::Network::SDN::Zones::Plugin; |
f23633dc | 12 | use Net::IP; |
cdf2c819 | 13 | |
f5eabba0 | 14 | use base('PVE::Network::SDN::Controllers::Plugin'); |
32602a38 AD |
15 | |
16 | sub type { | |
fa253735 | 17 | return 'evpn'; |
8fb1ee7f AD |
18 | } |
19 | ||
32602a38 AD |
20 | sub properties { |
21 | return { | |
92526f0e TL |
22 | asn => { |
23 | type => 'integer', | |
24 | description => "autonomous system number", | |
9e6b99fd AD |
25 | minimum => 0, |
26 | maximum => 4294967296 | |
92526f0e TL |
27 | }, |
28 | peers => { | |
29 | description => "peers address list.", | |
30 | type => 'string', format => 'ip-list' | |
31 | }, | |
32602a38 AD |
32 | }; |
33 | } | |
34 | ||
35 | sub options { | |
32602a38 | 36 | return { |
92526f0e TL |
37 | 'asn' => { optional => 0 }, |
38 | 'peers' => { optional => 0 }, | |
32602a38 AD |
39 | }; |
40 | } | |
41 | ||
42 | # Plugin implementation | |
8fb1ee7f | 43 | sub generate_controller_config { |
f23633dc | 44 | my ($class, $plugin_config, $controller_cfg, $id, $uplinks, $config) = @_; |
32602a38 | 45 | |
3caa7687 FG |
46 | my @peers; |
47 | @peers = PVE::Tools::split_list($plugin_config->{'peers'}) if $plugin_config->{'peers'}; | |
32602a38 | 48 | |
f23633dc AD |
49 | my $local_node = PVE::INotify::nodename(); |
50 | ||
074d270b | 51 | my $asn = $plugin_config->{asn}; |
f23633dc AD |
52 | my $ebgp = undef; |
53 | my $loopback = undef; | |
54 | my $autortas = undef; | |
55 | my $bgprouter = find_bgp_controller($local_node, $controller_cfg); | |
56 | if($bgprouter) { | |
57 | $ebgp = 1 if $plugin_config->{'asn'} ne $bgprouter->{asn}; | |
58 | $loopback = $bgprouter->{loopback} if $bgprouter->{loopback}; | |
59 | $asn = $bgprouter->{asn} if $bgprouter->{asn}; | |
60 | $autortas = $plugin_config->{'asn'} if $ebgp; | |
61 | } | |
074d270b AD |
62 | |
63 | return if !$asn; | |
32602a38 | 64 | |
92526f0e TL |
65 | my $bgp = $config->{frr}->{router}->{"bgp $asn"} //= {}; |
66 | ||
f23633dc | 67 | my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers, $loopback); |
32602a38 | 68 | |
f23633dc | 69 | my $remoteas = $ebgp ? "external" : $asn; |
17854295 | 70 | |
f23633dc | 71 | #global options |
92526f0e TL |
72 | my @controller_config = ( |
73 | "bgp router-id $ifaceip", | |
74 | "no bgp default ipv4-unicast", | |
75 | "coalesce-time 1000", | |
76 | ); | |
32602a38 | 77 | |
f23633dc AD |
78 | push(@{$bgp->{""}}, @controller_config) if keys %{$bgp} == 0; |
79 | ||
80 | @controller_config = (); | |
81 | ||
82 | #VTEP neighbors | |
83 | push @controller_config, "neighbor VTEP peer-group"; | |
84 | push @controller_config, "neighbor VTEP remote-as $remoteas"; | |
85 | push @controller_config, "neighbor VTEP bfd"; | |
86 | ||
87 | if($ebgp && $loopback) { | |
88 | push @controller_config, "neighbor VTEP ebgp-multihop 10"; | |
89 | push @controller_config, "neighbor VTEP update-source $loopback"; | |
90 | } | |
91 | ||
92 | # VTEP peers | |
32602a38 AD |
93 | foreach my $address (@peers) { |
94 | next if $address eq $ifaceip; | |
f23633dc | 95 | push @controller_config, "neighbor $address peer-group VTEP"; |
7d35eaf5 | 96 | } |
074d270b | 97 | |
92526f0e | 98 | push(@{$bgp->{""}}, @controller_config); |
074d270b | 99 | |
f23633dc | 100 | # address-family l2vpn |
56cdcac9 | 101 | @controller_config = (); |
f23633dc | 102 | push @controller_config, "neighbor VTEP activate"; |
56cdcac9 | 103 | push @controller_config, "advertise-all-vni"; |
f23633dc | 104 | push @controller_config, "autort as $autortas" if $autortas; |
92526f0e | 105 | push(@{$bgp->{"address-family"}->{"l2vpn evpn"}}, @controller_config); |
32602a38 AD |
106 | |
107 | return $config; | |
108 | } | |
109 | ||
56cdcac9 | 110 | sub generate_controller_zone_config { |
f23633dc AD |
111 | my ($class, $plugin_config, $controller, $controller_cfg, $id, $uplinks, $config) = @_; |
112 | ||
113 | my $local_node = PVE::INotify::nodename(); | |
0589eb09 | 114 | |
1de0abc0 | 115 | my $vrf = "vrf_$id"; |
0589eb09 | 116 | my $vrfvxlan = $plugin_config->{'vrf-vxlan'}; |
f23633dc | 117 | my $exitnodes = $plugin_config->{'exitnodes'}; |
92d8effb | 118 | my $advertisesubnets = $plugin_config->{'advertise-subnets'}; |
3d135423 | 119 | my $exitnodes_local_routing = $plugin_config->{'exitnodes-local-routing'}; |
f23633dc | 120 | |
56cdcac9 | 121 | my $asn = $controller->{asn}; |
f23633dc AD |
122 | my $ebgp = undef; |
123 | my $loopback = undef; | |
124 | my $autortas = undef; | |
125 | my $bgprouter = find_bgp_controller($local_node, $controller_cfg); | |
126 | if($bgprouter) { | |
127 | $ebgp = 1 if $controller->{'asn'} ne $bgprouter->{asn}; | |
128 | $loopback = $bgprouter->{loopback} if $bgprouter->{loopback}; | |
129 | $asn = $bgprouter->{asn} if $bgprouter->{asn}; | |
130 | $autortas = $controller->{'asn'} if $ebgp; | |
131 | } | |
0589eb09 AD |
132 | |
133 | return if !$vrf || !$vrfvxlan || !$asn; | |
134 | ||
92526f0e | 135 | # vrf |
56cdcac9 AD |
136 | my @controller_config = (); |
137 | push @controller_config, "vni $vrfvxlan"; | |
138 | push(@{$config->{frr}->{vrf}->{"$vrf"}}, @controller_config); | |
0589eb09 | 139 | |
f23633dc AD |
140 | #main vrf router |
141 | @controller_config = (); | |
142 | push @controller_config, "no bgp ebgp-requires-policy" if $ebgp; | |
143 | # push @controller_config, "!"; | |
144 | push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{""}}, @controller_config); | |
659c27c2 | 145 | |
f23633dc AD |
146 | if ($autortas) { |
147 | push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, "route-target import $autortas:$vrfvxlan"); | |
148 | push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, "route-target export $autortas:$vrfvxlan"); | |
149 | } | |
0589eb09 | 150 | |
b634e577 AD |
151 | my $is_gateway = $exitnodes->{$local_node}; |
152 | ||
0589eb09 AD |
153 | if ($is_gateway) { |
154 | ||
3d135423 AD |
155 | if (!$exitnodes_local_routing) { |
156 | @controller_config = (); | |
157 | #import /32 routes of evpn network from vrf1 to default vrf (for packet return) | |
158 | push @controller_config, "import vrf $vrf"; | |
159 | push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config); | |
160 | push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config); | |
0589eb09 | 161 | |
3d135423 AD |
162 | @controller_config = (); |
163 | #redistribute connected to be able to route to local vms on the gateway | |
164 | push @controller_config, "redistribute connected"; | |
165 | push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config); | |
166 | push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config); | |
167 | } | |
0589eb09 | 168 | |
56cdcac9 | 169 | @controller_config = (); |
0589eb09 | 170 | #add default originate to announce 0.0.0.0/0 type5 route in evpn |
56cdcac9 AD |
171 | push @controller_config, "default-originate ipv4"; |
172 | push @controller_config, "default-originate ipv6"; | |
173 | push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config); | |
92d8effb AD |
174 | } elsif ($advertisesubnets) { |
175 | ||
176 | @controller_config = (); | |
177 | #redistribute connected networks | |
178 | push @controller_config, "redistribute connected"; | |
179 | push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config); | |
180 | push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config); | |
181 | ||
182 | @controller_config = (); | |
183 | #advertise connected networks type5 route in evpn | |
184 | push @controller_config, "advertise ipv4 unicast"; | |
185 | push @controller_config, "advertise ipv6 unicast"; | |
186 | push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config); | |
0589eb09 AD |
187 | } |
188 | ||
189 | return $config; | |
190 | } | |
191 | ||
3d135423 AD |
192 | sub generate_controller_vnet_config { |
193 | my ($class, $plugin_config, $controller, $zone, $zoneid, $vnetid, $config) = @_; | |
194 | ||
195 | my $exitnodes = $zone->{'exitnodes'}; | |
196 | my $exitnodes_local_routing = $zone->{'exitnodes-local-routing'}; | |
197 | ||
198 | return if !$exitnodes_local_routing; | |
199 | ||
200 | my $local_node = PVE::INotify::nodename(); | |
201 | my $is_gateway = $exitnodes->{$local_node}; | |
202 | ||
203 | return if !$is_gateway; | |
204 | ||
205 | my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid, 1); | |
206 | my @controller_config = (); | |
207 | foreach my $subnetid (sort keys %{$subnets}) { | |
208 | my $subnet = $subnets->{$subnetid}; | |
209 | my $cidr = $subnet->{cidr}; | |
210 | push @controller_config, "ip route $cidr 10.255.255.2 xvrf_$zoneid"; | |
211 | } | |
212 | push(@{$config->{frr}->{''}}, @controller_config); | |
213 | } | |
214 | ||
32602a38 | 215 | sub on_delete_hook { |
56cdcac9 | 216 | my ($class, $controllerid, $zone_cfg) = @_; |
32602a38 | 217 | |
56cdcac9 AD |
218 | # verify that zone is associated to this controller |
219 | foreach my $id (keys %{$zone_cfg->{ids}}) { | |
92526f0e TL |
220 | my $zone = $zone_cfg->{ids}->{$id}; |
221 | die "controller $controllerid is used by $id" | |
222 | if (defined($zone->{controller}) && $zone->{controller} eq $controllerid); | |
5bda8607 | 223 | } |
32602a38 AD |
224 | } |
225 | ||
226 | sub on_update_hook { | |
56cdcac9 | 227 | my ($class, $controllerid, $controller_cfg) = @_; |
5bda8607 | 228 | |
c7bb4ac5 AD |
229 | # we can only have 1 evpn controller / 1 asn by server |
230 | ||
f23633dc | 231 | my $controllernb = 0; |
56cdcac9 AD |
232 | foreach my $id (keys %{$controller_cfg->{ids}}) { |
233 | next if $id eq $controllerid; | |
92526f0e | 234 | my $controller = $controller_cfg->{ids}->{$id}; |
f23633dc AD |
235 | next if $controller->{type} ne "evpn"; |
236 | $controllernb++; | |
237 | die "only 1 global evpn controller can be defined" if $controllernb > 1; | |
238 | } | |
239 | } | |
240 | ||
241 | sub find_bgp_controller { | |
242 | my ($nodename, $controller_cfg) = @_; | |
243 | ||
244 | my $controller = undef; | |
245 | foreach my $id (keys %{$controller_cfg->{ids}}) { | |
246 | $controller = $controller_cfg->{ids}->{$id}; | |
247 | next if $controller->{type} ne 'bgp'; | |
248 | next if $controller->{node} ne $nodename; | |
249 | last; | |
5bda8607 | 250 | } |
f23633dc AD |
251 | |
252 | return $controller; | |
32602a38 AD |
253 | } |
254 | ||
f23633dc | 255 | |
8fb1ee7f AD |
256 | sub sort_frr_config { |
257 | my $order = {}; | |
258 | $order->{''} = 0; | |
259 | $order->{'vrf'} = 1; | |
260 | $order->{'ipv4 unicast'} = 1; | |
261 | $order->{'ipv6 unicast'} = 2; | |
262 | $order->{'l2vpn evpn'} = 3; | |
bbf4e4b1 | 263 | $order->{'route-map'} = 200; |
8fb1ee7f AD |
264 | |
265 | my $a_val = 100; | |
266 | my $b_val = 100; | |
267 | ||
268 | $a_val = $order->{$a} if defined($order->{$a}); | |
269 | $b_val = $order->{$b} if defined($order->{$b}); | |
270 | ||
92526f0e | 271 | if ($a =~ /bgp (\d+)$/) { |
8fb1ee7f AD |
272 | $a_val = 2; |
273 | } | |
274 | ||
92526f0e | 275 | if ($b =~ /bgp (\d+)$/) { |
8fb1ee7f AD |
276 | $b_val = 2; |
277 | } | |
278 | ||
279 | return $a_val <=> $b_val; | |
280 | } | |
281 | ||
282 | sub generate_frr_recurse{ | |
283 | my ($final_config, $content, $parentkey, $level) = @_; | |
284 | ||
285 | my $keylist = {}; | |
286 | $keylist->{vrf} = 1; | |
287 | $keylist->{'address-family'} = 1; | |
288 | $keylist->{router} = 1; | |
bbf4e4b1 | 289 | $keylist->{'route-map'} = 1; |
8fb1ee7f AD |
290 | |
291 | my $exitkeylist = {}; | |
292 | $exitkeylist->{vrf} = 1; | |
293 | $exitkeylist->{'address-family'} = 1; | |
294 | ||
92526f0e | 295 | # FIXME: make this generic |
8fb1ee7f | 296 | my $paddinglevel = undef; |
92526f0e TL |
297 | if ($level == 1 || $level == 2) { |
298 | $paddinglevel = $level - 1; | |
8fb1ee7f | 299 | } elsif ($level == 3 || $level == 4) { |
92526f0e | 300 | $paddinglevel = $level - 2; |
8fb1ee7f AD |
301 | } |
302 | ||
303 | my $padding = ""; | |
304 | $padding = ' ' x ($paddinglevel) if $paddinglevel; | |
305 | ||
92526f0e | 306 | if (ref $content eq 'HASH') { |
8fb1ee7f AD |
307 | foreach my $key (sort sort_frr_config keys %$content) { |
308 | if ($parentkey && defined($keylist->{$parentkey})) { | |
92526f0e TL |
309 | push @{$final_config}, $padding."!"; |
310 | push @{$final_config}, $padding."$parentkey $key"; | |
311 | } elsif ($key ne '' && !defined($keylist->{$key})) { | |
312 | push @{$final_config}, $padding."$key"; | |
8fb1ee7f AD |
313 | } |
314 | ||
315 | my $option = $content->{$key}; | |
316 | generate_frr_recurse($final_config, $option, $key, $level+1); | |
317 | ||
318 | push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey}); | |
319 | } | |
320 | } | |
32602a38 | 321 | |
8fb1ee7f | 322 | if (ref $content eq 'ARRAY') { |
92526f0e | 323 | push @{$final_config}, map { $padding . "$_" } @$content; |
8fb1ee7f AD |
324 | } |
325 | } | |
326 | ||
9cef13e9 | 327 | sub generate_controller_rawconfig { |
8fb1ee7f AD |
328 | my ($class, $plugin_config, $config) = @_; |
329 | ||
659c27c2 AD |
330 | my $nodename = PVE::INotify::nodename(); |
331 | ||
8fb1ee7f AD |
332 | my $final_config = []; |
333 | push @{$final_config}, "log syslog informational"; | |
659c27c2 AD |
334 | push @{$final_config}, "ip forwarding"; |
335 | push @{$final_config}, "ipv6 forwarding"; | |
67a0f815 | 336 | push @{$final_config}, "frr defaults datacenter"; |
659c27c2 AD |
337 | push @{$final_config}, "service integrated-vtysh-config"; |
338 | push @{$final_config}, "hostname $nodename"; | |
8fb1ee7f AD |
339 | push @{$final_config}, "!"; |
340 | ||
0d1ab7dc | 341 | if (-e "/etc/frr/frr.conf.local") { |
0d1ab7dc AD |
342 | generate_frr_recurse($final_config, $config->{frr}->{vrf}, "vrf", 1); |
343 | push @{$final_config}, "!"; | |
344 | ||
cdf2c819 TL |
345 | my $local_conf = file_get_contents("/etc/frr/frr.conf.local"); |
346 | chomp ($local_conf); | |
347 | push @{$final_config}, $local_conf; | |
0d1ab7dc AD |
348 | } else { |
349 | generate_frr_recurse($final_config, $config->{frr}, undef, 0); | |
350 | } | |
8fb1ee7f AD |
351 | |
352 | push @{$final_config}, "!"; | |
353 | push @{$final_config}, "line vty"; | |
354 | push @{$final_config}, "!"; | |
355 | ||
356 | my $rawconfig = join("\n", @{$final_config}); | |
357 | ||
9cef13e9 AD |
358 | return if !$rawconfig; |
359 | return $rawconfig; | |
360 | } | |
361 | ||
362 | sub write_controller_config { | |
363 | my ($class, $plugin_config, $config) = @_; | |
364 | ||
365 | my $rawconfig = $class->generate_controller_rawconfig($plugin_config, $config); | |
8fb1ee7f AD |
366 | return if !$rawconfig; |
367 | return if !-d "/etc/frr"; | |
368 | ||
cdf2c819 | 369 | file_set_contents("/etc/frr/frr.conf", $rawconfig); |
8fb1ee7f AD |
370 | } |
371 | ||
fa609bdd AD |
372 | sub reload_controller { |
373 | my ($class) = @_; | |
374 | ||
375 | my $conf_file = "/etc/frr/frr.conf"; | |
659c27c2 AD |
376 | my $bin_path = "/usr/lib/frr/frr-reload.py"; |
377 | ||
378 | if (!-e $bin_path) { | |
379 | warn "missing $bin_path. Please install frr-pythontools package"; | |
380 | return; | |
381 | } | |
fa609bdd AD |
382 | |
383 | my $err = sub { | |
384 | my $line = shift; | |
659c27c2 AD |
385 | if ($line =~ /ERROR:/) { |
386 | warn "$line \n"; | |
fa609bdd AD |
387 | } |
388 | }; | |
389 | ||
390 | if (-e $conf_file && -e $bin_path) { | |
cdf2c819 | 391 | run_command([$bin_path, '--stdout', '--reload', $conf_file], outfunc => {}, errfunc => $err); |
fa609bdd AD |
392 | } |
393 | } | |
394 | ||
8fb1ee7f | 395 | 1; |
32602a38 | 396 | |
0589eb09 | 397 |