]>
Commit | Line | Data |
---|---|---|
1 | package PVE::Network::SDN::Controllers::EvpnPlugin; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use PVE::Network::SDN::Controllers::Plugin; | |
6 | use PVE::Tools; | |
7 | use PVE::INotify; | |
8 | use PVE::JSONSchema qw(get_standard_option); | |
9 | use PVE::Network::SDN::Zones::Plugin; | |
10 | use base('PVE::Network::SDN::Controllers::Plugin'); | |
11 | ||
12 | sub type { | |
13 | return 'evpn'; | |
14 | } | |
15 | ||
16 | sub properties { | |
17 | return { | |
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 | }, | |
26 | 'gateway-nodes' => get_standard_option('pve-node-list'), | |
27 | 'gateway-external-peers' => { | |
28 | description => "upstream bgp peers address list.", | |
29 | type => 'string', format => 'ip-list' | |
30 | }, | |
31 | }; | |
32 | } | |
33 | ||
34 | sub options { | |
35 | return { | |
36 | 'asn' => { optional => 0 }, | |
37 | 'peers' => { optional => 0 }, | |
38 | 'gateway-nodes' => { optional => 1 }, | |
39 | 'gateway-external-peers' => { optional => 1 }, | |
40 | }; | |
41 | } | |
42 | ||
43 | # Plugin implementation | |
44 | sub generate_controller_config { | |
45 | my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_; | |
46 | ||
47 | my @peers = split(',', $plugin_config->{'peers'}) if $plugin_config->{'peers'}; | |
48 | ||
49 | my $asn = $plugin_config->{asn}; | |
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; | |
54 | ||
55 | my $bgp = $config->{frr}->{router}->{"bgp $asn"} //= {}; | |
56 | ||
57 | my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers); | |
58 | ||
59 | my $is_gateway = undef; | |
60 | my $local_node = PVE::INotify::nodename(); | |
61 | ||
62 | foreach my $gatewaynode (PVE::Tools::split_list($gatewaynodes)) { | |
63 | $is_gateway = 1 if $gatewaynode eq $local_node; | |
64 | } | |
65 | ||
66 | my @controller_config = ( | |
67 | "bgp router-id $ifaceip", | |
68 | "no bgp default ipv4-unicast", | |
69 | "coalesce-time 1000", | |
70 | ); | |
71 | ||
72 | foreach my $address (@peers) { | |
73 | next if $address eq $ifaceip; | |
74 | push @controller_config, "neighbor $address remote-as $asn"; | |
75 | } | |
76 | ||
77 | if ($is_gateway) { | |
78 | foreach my $address (@gatewaypeers) { | |
79 | push @controller_config, "neighbor $address remote-as external"; | |
80 | } | |
81 | } | |
82 | push(@{$bgp->{""}}, @controller_config); | |
83 | ||
84 | @controller_config = (); | |
85 | foreach my $address (@peers) { | |
86 | next if $address eq $ifaceip; | |
87 | push @controller_config, "neighbor $address activate"; | |
88 | } | |
89 | push @controller_config, "advertise-all-vni"; | |
90 | push(@{$bgp->{"address-family"}->{"l2vpn evpn"}}, @controller_config); | |
91 | ||
92 | if ($is_gateway) { | |
93 | # import /32 routes of evpn network from vrf1 to default vrf (for packet return) | |
94 | @controller_config = map { "neighbor $_ activate" } @gatewaypeers; | |
95 | ||
96 | push(@{$bgp->{"address-family"}->{"ipv4 unicast"}}, @controller_config); | |
97 | push(@{$bgp->{"address-family"}->{"ipv6 unicast"}}, @controller_config); | |
98 | } | |
99 | ||
100 | return $config; | |
101 | } | |
102 | ||
103 | sub generate_controller_zone_config { | |
104 | my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_; | |
105 | ||
106 | my $vrf = $id; | |
107 | my $vrfvxlan = $plugin_config->{'vrf-vxlan'}; | |
108 | my $asn = $controller->{asn}; | |
109 | my $gatewaynodes = $controller->{'gateway-nodes'}; | |
110 | ||
111 | return if !$vrf || !$vrfvxlan || !$asn; | |
112 | ||
113 | # vrf | |
114 | my @controller_config = (); | |
115 | push @controller_config, "vni $vrfvxlan"; | |
116 | push(@{$config->{frr}->{vrf}->{"$vrf"}}, @controller_config); | |
117 | ||
118 | push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{""}}, "!"); | |
119 | ||
120 | my $local_node = PVE::INotify::nodename(); | |
121 | ||
122 | my $is_gateway = grep { $_ eq $local_node } PVE::Tools::split_list($gatewaynodes); | |
123 | if ($is_gateway) { | |
124 | ||
125 | @controller_config = (); | |
126 | #import /32 routes of evpn network from vrf1 to default vrf (for packet return) | |
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); | |
130 | ||
131 | @controller_config = (); | |
132 | #redistribute connected to be able to route to local vms on the gateway | |
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); | |
136 | ||
137 | @controller_config = (); | |
138 | #add default originate to announce 0.0.0.0/0 type5 route in evpn | |
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); | |
142 | } | |
143 | ||
144 | return $config; | |
145 | } | |
146 | ||
147 | sub on_delete_hook { | |
148 | my ($class, $controllerid, $zone_cfg) = @_; | |
149 | ||
150 | # verify that zone is associated to this controller | |
151 | foreach my $id (keys %{$zone_cfg->{ids}}) { | |
152 | my $zone = $zone_cfg->{ids}->{$id}; | |
153 | die "controller $controllerid is used by $id" | |
154 | if (defined($zone->{controller}) && $zone->{controller} eq $controllerid); | |
155 | } | |
156 | } | |
157 | ||
158 | sub on_update_hook { | |
159 | my ($class, $controllerid, $controller_cfg) = @_; | |
160 | ||
161 | # we can only have 1 evpn controller / 1 asn by server | |
162 | ||
163 | foreach my $id (keys %{$controller_cfg->{ids}}) { | |
164 | next if $id eq $controllerid; | |
165 | my $controller = $controller_cfg->{ids}->{$id}; | |
166 | die "only 1 evpn controller can be defined" if $controller->{type} eq "evpn"; | |
167 | } | |
168 | } | |
169 | ||
170 | sub 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 | ||
184 | if ($a =~ /bgp (\d+)$/) { | |
185 | $a_val = 2; | |
186 | } | |
187 | ||
188 | if ($b =~ /bgp (\d+)$/) { | |
189 | $b_val = 2; | |
190 | } | |
191 | ||
192 | return $a_val <=> $b_val; | |
193 | } | |
194 | ||
195 | sub 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 | ||
207 | # FIXME: make this generic | |
208 | my $paddinglevel = undef; | |
209 | if ($level == 1 || $level == 2) { | |
210 | $paddinglevel = $level - 1; | |
211 | } elsif ($level == 3 || $level == 4) { | |
212 | $paddinglevel = $level - 2; | |
213 | } | |
214 | ||
215 | my $padding = ""; | |
216 | $padding = ' ' x ($paddinglevel) if $paddinglevel; | |
217 | ||
218 | if (ref $content eq 'HASH') { | |
219 | foreach my $key (sort sort_frr_config keys %$content) { | |
220 | if ($parentkey && defined($keylist->{$parentkey})) { | |
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"; | |
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 | } | |
233 | ||
234 | if (ref $content eq 'ARRAY') { | |
235 | push @{$final_config}, map { $padding . "$_" } @$content; | |
236 | } | |
237 | } | |
238 | ||
239 | sub write_controller_config { | |
240 | my ($class, $plugin_config, $config) = @_; | |
241 | ||
242 | my $nodename = PVE::INotify::nodename(); | |
243 | ||
244 | my $final_config = []; | |
245 | push @{$final_config}, "log syslog informational"; | |
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"; | |
251 | push @{$final_config}, "!"; | |
252 | ||
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 | } | |
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 | ||
283 | sub reload_controller { | |
284 | my ($class) = @_; | |
285 | ||
286 | my $conf_file = "/etc/frr/frr.conf"; | |
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 | } | |
293 | ||
294 | my $err = sub { | |
295 | my $line = shift; | |
296 | if ($line =~ /ERROR:/) { | |
297 | warn "$line \n"; | |
298 | } | |
299 | }; | |
300 | ||
301 | if (-e $conf_file && -e $bin_path) { | |
302 | PVE::Tools::run_command([$bin_path, '--stdout', '--reload', $conf_file], outfunc => {}, errfunc => $err); | |
303 | } | |
304 | } | |
305 | ||
306 | 1; | |
307 | ||
308 |