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