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