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