]> git.proxmox.com Git - pve-network.git/blame - PVE/Network/SDN/Controllers/EvpnPlugin.pm
vlan/qinq: use vlan-raw-device to support long predictable interfaces name
[pve-network.git] / PVE / Network / SDN / Controllers / EvpnPlugin.pm
CommitLineData
fa253735 1package PVE::Network::SDN::Controllers::EvpnPlugin;
32602a38
AD
2
3use strict;
4use warnings;
f5eabba0 5use PVE::Network::SDN::Controllers::Plugin;
1f543c5f 6use PVE::Tools;
074d270b
AD
7use PVE::INotify;
8use PVE::JSONSchema qw(get_standard_option);
1f543c5f 9use PVE::Network::SDN::Zones::Plugin;
f5eabba0 10use base('PVE::Network::SDN::Controllers::Plugin');
32602a38
AD
11
12sub type {
fa253735 13 return 'evpn';
8fb1ee7f
AD
14}
15
32602a38
AD
16sub properties {
17 return {
18 'asn' => {
19 type => 'integer',
20 description => "autonomous system number",
21 },
22 'peers' => {
23 description => "peers address list.",
fcfca9ef 24 type => 'string', format => 'ip-list'
32602a38 25 },
074d270b
AD
26 'gateway-nodes' => get_standard_option('pve-node-list'),
27 'gateway-external-peers' => {
28 description => "upstream bgp peers address list.",
fcfca9ef 29 type => 'string', format => 'ip-list'
074d270b 30 },
32602a38
AD
31 };
32}
33
34sub options {
35
36 return {
32602a38
AD
37 'asn' => { optional => 0 },
38 'peers' => { optional => 0 },
074d270b
AD
39 'gateway-nodes' => { optional => 1 },
40 'gateway-external-peers' => { optional => 1 },
32602a38
AD
41 };
42}
43
44# Plugin implementation
8fb1ee7f 45sub generate_controller_config {
56cdcac9 46 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
32602a38 47
32602a38
AD
48 my @peers = split(',', $plugin_config->{'peers'}) if $plugin_config->{'peers'};
49
074d270b 50 my $asn = $plugin_config->{asn};
074d270b
AD
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;
32602a38 55
1f543c5f 56 my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers);
32602a38 57
074d270b
AD
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 }
17854295 64
56cdcac9 65 my @controller_config = ();
93dea3aa 66
56cdcac9
AD
67 push @controller_config, "bgp router-id $ifaceip";
68 push @controller_config, "no bgp default ipv4-unicast";
69 push @controller_config, "coalesce-time 1000";
32602a38
AD
70
71 foreach my $address (@peers) {
72 next if $address eq $ifaceip;
56cdcac9 73 push @controller_config, "neighbor $address remote-as $asn";
7d35eaf5 74 }
074d270b
AD
75
76 if ($is_gateway) {
77 foreach my $address (@gatewaypeers) {
56cdcac9 78 push @controller_config, "neighbor $address remote-as external";
074d270b
AD
79 }
80 }
56cdcac9 81 push(@{$config->{frr}->{router}->{"bgp $asn"}->{""}}, @controller_config);
074d270b 82
56cdcac9 83 @controller_config = ();
32602a38
AD
84 foreach my $address (@peers) {
85 next if $address eq $ifaceip;
56cdcac9 86 push @controller_config, "neighbor $address activate";
32602a38 87 }
56cdcac9
AD
88 push @controller_config, "advertise-all-vni";
89 push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
32602a38 90
074d270b
AD
91 if ($is_gateway) {
92
56cdcac9 93 @controller_config = ();
074d270b 94 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
074d270b 95 foreach my $address (@gatewaypeers) {
56cdcac9 96 push @controller_config, "neighbor $address activate";
074d270b 97 }
56cdcac9
AD
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);
074d270b
AD
100
101 }
102
32602a38
AD
103 return $config;
104}
105
56cdcac9
AD
106sub generate_controller_zone_config {
107 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
0589eb09 108
7cb9714d 109 my $vrf = $id;
0589eb09 110 my $vrfvxlan = $plugin_config->{'vrf-vxlan'};
56cdcac9
AD
111 my $asn = $controller->{asn};
112 my $gatewaynodes = $controller->{'gateway-nodes'};
0589eb09
AD
113
114 return if !$vrf || !$vrfvxlan || !$asn;
115
116 #vrf
56cdcac9
AD
117 my @controller_config = ();
118 push @controller_config, "vni $vrfvxlan";
119 push(@{$config->{frr}->{vrf}->{"$vrf"}}, @controller_config);
0589eb09 120
659c27c2
AD
121 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{""}}, "!");
122
56cdcac9 123 @controller_config = ();
0589eb09
AD
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
56cdcac9 134 @controller_config = ();
0589eb09 135 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
56cdcac9
AD
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);
0589eb09 139
56cdcac9 140 @controller_config = ();
0589eb09 141 #redistribute connected to be able to route to local vms on the gateway
56cdcac9
AD
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);
0589eb09 145
56cdcac9 146 @controller_config = ();
0589eb09 147 #add default originate to announce 0.0.0.0/0 type5 route in evpn
56cdcac9
AD
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);
0589eb09
AD
151 }
152
153 return $config;
154}
155
32602a38 156sub on_delete_hook {
56cdcac9 157 my ($class, $controllerid, $zone_cfg) = @_;
32602a38 158
56cdcac9
AD
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);
5bda8607 164 }
32602a38
AD
165}
166
167sub on_update_hook {
56cdcac9 168 my ($class, $controllerid, $controller_cfg) = @_;
5bda8607 169
56cdcac9
AD
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};
5bda8607 175 die "asn $asn is already used by $id"
56cdcac9 176 if (defined($controller->{asn}) && $controller->{asn} eq $asn);
5bda8607 177 }
32602a38
AD
178}
179
8fb1ee7f
AD
180sub 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
205sub 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 }
32602a38 243
8fb1ee7f
AD
244 if (ref $content eq 'ARRAY') {
245 foreach my $value (@$content) {
246 push @{$final_config}, $padding."$value";
247 }
248 }
249}
250
251sub write_controller_config {
252 my ($class, $plugin_config, $config) = @_;
253
659c27c2
AD
254 my $nodename = PVE::INotify::nodename();
255
8fb1ee7f
AD
256 my $final_config = [];
257 push @{$final_config}, "log syslog informational";
659c27c2
AD
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";
8fb1ee7f
AD
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
fa609bdd
AD
284sub reload_controller {
285 my ($class) = @_;
286
287 my $conf_file = "/etc/frr/frr.conf";
659c27c2
AD
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 }
fa609bdd
AD
294
295 my $err = sub {
296 my $line = shift;
659c27c2
AD
297 if ($line =~ /ERROR:/) {
298 warn "$line \n";
fa609bdd
AD
299 }
300 };
301
302 if (-e $conf_file && -e $bin_path) {
659c27c2 303 PVE::Tools::run_command([$bin_path, '--stdout', '--reload', $conf_file], outfunc => {}, errfunc => $err);
fa609bdd
AD
304 }
305}
306
8fb1ee7f 3071;
32602a38 308
0589eb09 309