]>
Commit | Line | Data |
---|---|---|
f5eabba0 | 1 | package PVE::Network::SDN::Controllers::FrrEvpnPlugin; |
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 { | |
f5eabba0 | 13 | return 'frrevpn'; |
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 { |
074d270b | 52 | my ($class, $plugin_config, $router, $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 | |
93dea3aa AD |
78 | my @router_config = (); |
79 | ||
93dea3aa | 80 | push @router_config, "bgp router-id $ifaceip"; |
bc49b410 | 81 | push @router_config, "no bgp default ipv4-unicast"; |
93dea3aa | 82 | push @router_config, "coalesce-time 1000"; |
32602a38 AD |
83 | |
84 | foreach my $address (@peers) { | |
85 | next if $address eq $ifaceip; | |
93dea3aa | 86 | push @router_config, "neighbor $address remote-as $asn"; |
7d35eaf5 | 87 | } |
074d270b AD |
88 | |
89 | if ($is_gateway) { | |
90 | foreach my $address (@gatewaypeers) { | |
91 | push @router_config, "neighbor $address remote-as external"; | |
92 | } | |
93 | } | |
8fb1ee7f | 94 | push(@{$config->{frr}->{router}->{"bgp $asn"}->{""}}, @router_config); |
074d270b | 95 | |
17854295 | 96 | @router_config = (); |
32602a38 AD |
97 | foreach my $address (@peers) { |
98 | next if $address eq $ifaceip; | |
17854295 | 99 | push @router_config, "neighbor $address activate"; |
32602a38 | 100 | } |
17854295 | 101 | push @router_config, "advertise-all-vni"; |
8fb1ee7f | 102 | push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"l2vpn evpn"}}, @router_config); |
32602a38 | 103 | |
074d270b AD |
104 | if ($is_gateway) { |
105 | ||
106 | @router_config = (); | |
107 | #import /32 routes of evpn network from vrf1 to default vrf (for packet return) | |
108 | #frr 7.1 tag is bugged -> works fine with 7.1 stable branch(20190829-02-g6ba76bbc1) | |
109 | #https://github.com/FRRouting/frr/issues/4905 | |
110 | foreach my $address (@gatewaypeers) { | |
111 | push @router_config, "neighbor $address activate"; | |
112 | } | |
8fb1ee7f AD |
113 | push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @router_config); |
114 | push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @router_config); | |
074d270b AD |
115 | |
116 | } | |
117 | ||
32602a38 AD |
118 | return $config; |
119 | } | |
120 | ||
0589eb09 AD |
121 | sub generate_controller_transport_config { |
122 | my ($class, $plugin_config, $router, $id, $uplinks, $config) = @_; | |
123 | ||
124 | my $vrf = $plugin_config->{'vrf'}; | |
125 | my $vrfvxlan = $plugin_config->{'vrf-vxlan'}; | |
126 | my $asn = $router->{asn}; | |
127 | my $gatewaynodes = $router->{'gateway-nodes'}; | |
128 | ||
129 | return if !$vrf || !$vrfvxlan || !$asn; | |
130 | ||
131 | #vrf | |
132 | my @router_config = (); | |
133 | push @router_config, "vni $vrfvxlan"; | |
134 | push(@{$config->{frr}->{vrf}->{"$vrf"}}, @router_config); | |
135 | ||
136 | @router_config = (); | |
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 | ||
147 | @router_config = (); | |
148 | #import /32 routes of evpn network from vrf1 to default vrf (for packet return) | |
149 | #frr 7.1 tag is bugged -> works fine with 7.1 stable branch(20190829-02-g6ba76bbc1) | |
150 | #https://github.com/FRRouting/frr/issues/4905 | |
151 | push @router_config, "import vrf $vrf"; | |
152 | push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @router_config); | |
153 | push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @router_config); | |
154 | ||
155 | @router_config = (); | |
156 | #redistribute connected to be able to route to local vms on the gateway | |
157 | push @router_config, "redistribute connected"; | |
158 | push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv4 unicast"}}, @router_config); | |
159 | push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv6 unicast"}}, @router_config); | |
160 | ||
161 | @router_config = (); | |
162 | #add default originate to announce 0.0.0.0/0 type5 route in evpn | |
163 | push @router_config, "default-originate ipv4"; | |
164 | push @router_config, "default-originate ipv6"; | |
165 | push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @router_config); | |
166 | } | |
167 | ||
168 | return $config; | |
169 | } | |
170 | ||
32602a38 | 171 | sub on_delete_hook { |
5bda8607 | 172 | my ($class, $routerid, $sdn_cfg) = @_; |
32602a38 | 173 | |
5bda8607 AD |
174 | # verify that transport is associated to this router |
175 | foreach my $id (keys %{$sdn_cfg->{ids}}) { | |
176 | my $sdn = $sdn_cfg->{ids}->{$id}; | |
177 | die "router $routerid is used by $id" | |
178 | if (defined($sdn->{router}) && $sdn->{router} eq $routerid); | |
179 | } | |
32602a38 AD |
180 | } |
181 | ||
182 | sub on_update_hook { | |
5bda8607 AD |
183 | my ($class, $routerid, $sdn_cfg) = @_; |
184 | ||
185 | # verify that asn is not already used by another router | |
186 | my $asn = $sdn_cfg->{ids}->{$routerid}->{asn}; | |
187 | foreach my $id (keys %{$sdn_cfg->{ids}}) { | |
188 | next if $id eq $routerid; | |
189 | my $sdn = $sdn_cfg->{ids}->{$id}; | |
190 | die "asn $asn is already used by $id" | |
191 | if (defined($sdn->{asn}) && $sdn->{asn} eq $asn); | |
192 | } | |
32602a38 AD |
193 | } |
194 | ||
8fb1ee7f AD |
195 | sub sort_frr_config { |
196 | my $order = {}; | |
197 | $order->{''} = 0; | |
198 | $order->{'vrf'} = 1; | |
199 | $order->{'ipv4 unicast'} = 1; | |
200 | $order->{'ipv6 unicast'} = 2; | |
201 | $order->{'l2vpn evpn'} = 3; | |
202 | ||
203 | my $a_val = 100; | |
204 | my $b_val = 100; | |
205 | ||
206 | $a_val = $order->{$a} if defined($order->{$a}); | |
207 | $b_val = $order->{$b} if defined($order->{$b}); | |
208 | ||
209 | if($a =~ /bgp (\d+)$/) { | |
210 | $a_val = 2; | |
211 | } | |
212 | ||
213 | if($b =~ /bgp (\d+)$/) { | |
214 | $b_val = 2; | |
215 | } | |
216 | ||
217 | return $a_val <=> $b_val; | |
218 | } | |
219 | ||
220 | sub generate_frr_recurse{ | |
221 | my ($final_config, $content, $parentkey, $level) = @_; | |
222 | ||
223 | my $keylist = {}; | |
224 | $keylist->{vrf} = 1; | |
225 | $keylist->{'address-family'} = 1; | |
226 | $keylist->{router} = 1; | |
227 | ||
228 | my $exitkeylist = {}; | |
229 | $exitkeylist->{vrf} = 1; | |
230 | $exitkeylist->{'address-family'} = 1; | |
231 | ||
232 | #fix me, make this generic | |
233 | my $paddinglevel = undef; | |
234 | if($level == 1 || $level == 2) { | |
235 | $paddinglevel = $level - 1; | |
236 | } elsif ($level == 3 || $level == 4) { | |
237 | $paddinglevel = $level - 2; | |
238 | } | |
239 | ||
240 | my $padding = ""; | |
241 | $padding = ' ' x ($paddinglevel) if $paddinglevel; | |
242 | ||
243 | if (ref $content eq ref {}) { | |
244 | foreach my $key (sort sort_frr_config keys %$content) { | |
245 | if ($parentkey && defined($keylist->{$parentkey})) { | |
246 | push @{$final_config}, $padding."!"; | |
247 | push @{$final_config}, $padding."$parentkey $key"; | |
248 | } else { | |
249 | push @{$final_config}, $padding."$key" if $key ne '' && !defined($keylist->{$key}); | |
250 | } | |
251 | ||
252 | my $option = $content->{$key}; | |
253 | generate_frr_recurse($final_config, $option, $key, $level+1); | |
254 | ||
255 | push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey}); | |
256 | } | |
257 | } | |
32602a38 | 258 | |
8fb1ee7f AD |
259 | if (ref $content eq 'ARRAY') { |
260 | foreach my $value (@$content) { | |
261 | push @{$final_config}, $padding."$value"; | |
262 | } | |
263 | } | |
264 | } | |
265 | ||
266 | sub write_controller_config { | |
267 | my ($class, $plugin_config, $config) = @_; | |
268 | ||
269 | my $final_config = []; | |
270 | push @{$final_config}, "log syslog informational"; | |
271 | push @{$final_config}, "!"; | |
272 | ||
273 | generate_frr_recurse($final_config, $config->{frr}, undef, 0); | |
274 | ||
275 | push @{$final_config}, "!"; | |
276 | push @{$final_config}, "line vty"; | |
277 | push @{$final_config}, "!"; | |
278 | ||
279 | my $rawconfig = join("\n", @{$final_config}); | |
280 | ||
281 | ||
282 | return if !$rawconfig; | |
283 | return if !-d "/etc/frr"; | |
284 | ||
285 | my $frr_config_file = "/etc/frr/frr.conf"; | |
286 | ||
287 | my $writefh = IO::File->new($frr_config_file,">"); | |
288 | print $writefh $rawconfig; | |
289 | $writefh->close(); | |
290 | } | |
291 | ||
fa609bdd AD |
292 | sub reload_controller { |
293 | my ($class) = @_; | |
294 | ||
295 | my $conf_file = "/etc/frr/frr.conf"; | |
296 | my $bin_path = "/usr/bin/vtysh"; | |
297 | ||
298 | my $err = sub { | |
299 | my $line = shift; | |
300 | if ($line =~ /^line (\S+)/) { | |
301 | print "$line \n"; | |
302 | } | |
303 | }; | |
304 | ||
305 | if (-e $conf_file && -e $bin_path) { | |
306 | PVE::Tools::run_command([$bin_path, '-m', '-f', $conf_file], outfunc => {}, errfunc => $err); | |
307 | } | |
308 | } | |
309 | ||
8fb1ee7f | 310 | 1; |
32602a38 | 311 | |
0589eb09 | 312 |