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