]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Controllers/EvpnPlugin.pm
c8bf76a54a49ad2be4e3951f95a96f9c08103ff8
[pve-network.git] / PVE / Network / SDN / Controllers / EvpnPlugin.pm
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
36 return {
37 'asn' => { optional => 0 },
38 'peers' => { optional => 0 },
39 'gateway-nodes' => { optional => 1 },
40 'gateway-external-peers' => { optional => 1 },
41 };
42 }
43
44 # Plugin implementation
45 sub generate_controller_config {
46 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
47
48 my @peers = split(',', $plugin_config->{'peers'}) if $plugin_config->{'peers'};
49
50 my $asn = $plugin_config->{asn};
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;
55
56 my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers);
57
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 }
64
65 my @controller_config = ();
66
67 push @controller_config, "bgp router-id $ifaceip";
68 push @controller_config, "no bgp default ipv4-unicast";
69 push @controller_config, "coalesce-time 1000";
70
71 foreach my $address (@peers) {
72 next if $address eq $ifaceip;
73 push @controller_config, "neighbor $address remote-as $asn";
74 }
75
76 if ($is_gateway) {
77 foreach my $address (@gatewaypeers) {
78 push @controller_config, "neighbor $address remote-as external";
79 }
80 }
81 push(@{$config->{frr}->{router}->{"bgp $asn"}->{""}}, @controller_config);
82
83 @controller_config = ();
84 foreach my $address (@peers) {
85 next if $address eq $ifaceip;
86 push @controller_config, "neighbor $address activate";
87 }
88 push @controller_config, "advertise-all-vni";
89 push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
90
91 if ($is_gateway) {
92
93 @controller_config = ();
94 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
95 foreach my $address (@gatewaypeers) {
96 push @controller_config, "neighbor $address activate";
97 }
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);
100
101 }
102
103 return $config;
104 }
105
106 sub generate_controller_zone_config {
107 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
108
109 my $vrf = $id;
110 my $vrfvxlan = $plugin_config->{'vrf-vxlan'};
111 my $asn = $controller->{asn};
112 my $gatewaynodes = $controller->{'gateway-nodes'};
113
114 return if !$vrf || !$vrfvxlan || !$asn;
115
116 #vrf
117 my @controller_config = ();
118 push @controller_config, "vni $vrfvxlan";
119 push(@{$config->{frr}->{vrf}->{"$vrf"}}, @controller_config);
120
121 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{""}}, "!");
122
123 @controller_config = ();
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
134 @controller_config = ();
135 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
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);
139
140 @controller_config = ();
141 #redistribute connected to be able to route to local vms on the gateway
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);
145
146 @controller_config = ();
147 #add default originate to announce 0.0.0.0/0 type5 route in evpn
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);
151 }
152
153 return $config;
154 }
155
156 sub on_delete_hook {
157 my ($class, $controllerid, $zone_cfg) = @_;
158
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);
164 }
165 }
166
167 sub on_update_hook {
168 my ($class, $controllerid, $controller_cfg) = @_;
169
170 # verify that asn is not already used by another controller
171 my $asn = $controller_cfg->{ids}->{$controllerid}->{asn};
172 foreach my $id (keys %{$controller_cfg->{ids}}) {
173 next if $id eq $controllerid;
174 my $controller = $controller_cfg->{ids}->{$id};
175 die "asn $asn is already used by $id"
176 if (defined($controller->{asn}) && $controller->{asn} eq $asn);
177 }
178 }
179
180 sub sort_frr_config {
181 my $order = {};
182 $order->{''} = 0;
183 $order->{'vrf'} = 1;
184 $order->{'ipv4 unicast'} = 1;
185 $order->{'ipv6 unicast'} = 2;
186 $order->{'l2vpn evpn'} = 3;
187
188 my $a_val = 100;
189 my $b_val = 100;
190
191 $a_val = $order->{$a} if defined($order->{$a});
192 $b_val = $order->{$b} if defined($order->{$b});
193
194 if($a =~ /bgp (\d+)$/) {
195 $a_val = 2;
196 }
197
198 if($b =~ /bgp (\d+)$/) {
199 $b_val = 2;
200 }
201
202 return $a_val <=> $b_val;
203 }
204
205 sub generate_frr_recurse{
206 my ($final_config, $content, $parentkey, $level) = @_;
207
208 my $keylist = {};
209 $keylist->{vrf} = 1;
210 $keylist->{'address-family'} = 1;
211 $keylist->{router} = 1;
212
213 my $exitkeylist = {};
214 $exitkeylist->{vrf} = 1;
215 $exitkeylist->{'address-family'} = 1;
216
217 #fix me, make this generic
218 my $paddinglevel = undef;
219 if($level == 1 || $level == 2) {
220 $paddinglevel = $level - 1;
221 } elsif ($level == 3 || $level == 4) {
222 $paddinglevel = $level - 2;
223 }
224
225 my $padding = "";
226 $padding = ' ' x ($paddinglevel) if $paddinglevel;
227
228 if (ref $content eq ref {}) {
229 foreach my $key (sort sort_frr_config keys %$content) {
230 if ($parentkey && defined($keylist->{$parentkey})) {
231 push @{$final_config}, $padding."!";
232 push @{$final_config}, $padding."$parentkey $key";
233 } else {
234 push @{$final_config}, $padding."$key" if $key ne '' && !defined($keylist->{$key});
235 }
236
237 my $option = $content->{$key};
238 generate_frr_recurse($final_config, $option, $key, $level+1);
239
240 push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
241 }
242 }
243
244 if (ref $content eq 'ARRAY') {
245 foreach my $value (@$content) {
246 push @{$final_config}, $padding."$value";
247 }
248 }
249 }
250
251 sub write_controller_config {
252 my ($class, $plugin_config, $config) = @_;
253
254 my $nodename = PVE::INotify::nodename();
255
256 my $final_config = [];
257 push @{$final_config}, "log syslog informational";
258 push @{$final_config}, "ip forwarding";
259 push @{$final_config}, "ipv6 forwarding";
260 push @{$final_config}, "frr defaults traditional";
261 push @{$final_config}, "service integrated-vtysh-config";
262 push @{$final_config}, "hostname $nodename";
263 push @{$final_config}, "!";
264
265 generate_frr_recurse($final_config, $config->{frr}, undef, 0);
266
267 push @{$final_config}, "!";
268 push @{$final_config}, "line vty";
269 push @{$final_config}, "!";
270
271 my $rawconfig = join("\n", @{$final_config});
272
273
274 return if !$rawconfig;
275 return if !-d "/etc/frr";
276
277 my $frr_config_file = "/etc/frr/frr.conf";
278
279 my $writefh = IO::File->new($frr_config_file,">");
280 print $writefh $rawconfig;
281 $writefh->close();
282 }
283
284 sub reload_controller {
285 my ($class) = @_;
286
287 my $conf_file = "/etc/frr/frr.conf";
288 my $bin_path = "/usr/lib/frr/frr-reload.py";
289
290 if (!-e $bin_path) {
291 warn "missing $bin_path. Please install frr-pythontools package";
292 return;
293 }
294
295 my $err = sub {
296 my $line = shift;
297 if ($line =~ /ERROR:/) {
298 warn "$line \n";
299 }
300 };
301
302 if (-e $conf_file && -e $bin_path) {
303 PVE::Tools::run_command([$bin_path, '--stdout', '--reload', $conf_file], outfunc => {}, errfunc => $err);
304 }
305 }
306
307 1;
308
309