]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Controllers/EvpnPlugin.pm
evpn zone plugin : remove vrf option
[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
10 use base('PVE::Network::SDN::Controllers::Plugin');
11
12 sub type {
13 return 'evpn';
14 }
15
16 sub properties {
17 return {
18 'uplink-id' => {
19 type => 'integer',
20 minimum => 1, maximum => 4096,
21 description => 'Uplink interface',
22 },
23 'asn' => {
24 type => 'integer',
25 description => "autonomous system number",
26 },
27 'peers' => {
28 description => "peers address list.",
29 type => 'string', format => 'ip-list'
30 },
31 'gateway-nodes' => get_standard_option('pve-node-list'),
32 'gateway-external-peers' => {
33 description => "upstream bgp peers address list.",
34 type => 'string', format => 'ip-list'
35 },
36 };
37 }
38
39 sub options {
40
41 return {
42 'uplink-id' => { optional => 0 },
43 'asn' => { optional => 0 },
44 'peers' => { optional => 0 },
45 'gateway-nodes' => { optional => 1 },
46 'gateway-external-peers' => { optional => 1 },
47 };
48 }
49
50 # Plugin implementation
51 sub generate_controller_config {
52 my ($class, $plugin_config, $router, $id, $uplinks, $config) = @_;
53
54 my @peers = split(',', $plugin_config->{'peers'}) if $plugin_config->{'peers'};
55
56 my $asn = $plugin_config->{asn};
57 my $uplink = $plugin_config->{'uplink-id'};
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;
62
63 my $iface = "uplink$uplink";
64 my $ifaceip = "";
65
66 if($uplinks->{$uplink}->{name}) {
67 $iface = $uplinks->{$uplink}->{name};
68 $ifaceip = PVE::Network::SDN::Controllers::Plugin::get_first_local_ipv4_from_interface($iface);
69 }
70
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 }
77
78 my @router_config = ();
79
80 push @router_config, "bgp router-id $ifaceip";
81 push @router_config, "no bgp default ipv4-unicast";
82 push @router_config, "coalesce-time 1000";
83
84 foreach my $address (@peers) {
85 next if $address eq $ifaceip;
86 push @router_config, "neighbor $address remote-as $asn";
87 }
88
89 if ($is_gateway) {
90 foreach my $address (@gatewaypeers) {
91 push @router_config, "neighbor $address remote-as external";
92 }
93 }
94 push(@{$config->{frr}->{router}->{"bgp $asn"}->{""}}, @router_config);
95
96 @router_config = ();
97 foreach my $address (@peers) {
98 next if $address eq $ifaceip;
99 push @router_config, "neighbor $address activate";
100 }
101 push @router_config, "advertise-all-vni";
102 push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"l2vpn evpn"}}, @router_config);
103
104 if ($is_gateway) {
105
106 @router_config = ();
107 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
108 foreach my $address (@gatewaypeers) {
109 push @router_config, "neighbor $address activate";
110 }
111 push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @router_config);
112 push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @router_config);
113
114 }
115
116 return $config;
117 }
118
119 sub generate_controller_transport_config {
120 my ($class, $plugin_config, $router, $id, $uplinks, $config) = @_;
121
122 my $vrf = $id;
123 my $vrfvxlan = $plugin_config->{'vrf-vxlan'};
124 my $asn = $router->{asn};
125 my $gatewaynodes = $router->{'gateway-nodes'};
126
127 return if !$vrf || !$vrfvxlan || !$asn;
128
129 #vrf
130 my @router_config = ();
131 push @router_config, "vni $vrfvxlan";
132 push(@{$config->{frr}->{vrf}->{"$vrf"}}, @router_config);
133
134 @router_config = ();
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
145 @router_config = ();
146 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
147 #frr 7.1 tag is bugged -> works fine with 7.1 stable branch(20190829-02-g6ba76bbc1)
148 #https://github.com/FRRouting/frr/issues/4905
149 push @router_config, "import vrf $vrf";
150 push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @router_config);
151 push(@{$config->{frr}->{router}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @router_config);
152
153 @router_config = ();
154 #redistribute connected to be able to route to local vms on the gateway
155 push @router_config, "redistribute connected";
156 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv4 unicast"}}, @router_config);
157 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv6 unicast"}}, @router_config);
158
159 @router_config = ();
160 #add default originate to announce 0.0.0.0/0 type5 route in evpn
161 push @router_config, "default-originate ipv4";
162 push @router_config, "default-originate ipv6";
163 push(@{$config->{frr}->{router}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @router_config);
164 }
165
166 return $config;
167 }
168
169 sub on_delete_hook {
170 my ($class, $routerid, $sdn_cfg) = @_;
171
172 # verify that transport is associated to this router
173 foreach my $id (keys %{$sdn_cfg->{ids}}) {
174 my $sdn = $sdn_cfg->{ids}->{$id};
175 die "router $routerid is used by $id"
176 if (defined($sdn->{router}) && $sdn->{router} eq $routerid);
177 }
178 }
179
180 sub on_update_hook {
181 my ($class, $routerid, $sdn_cfg) = @_;
182
183 # verify that asn is not already used by another router
184 my $asn = $sdn_cfg->{ids}->{$routerid}->{asn};
185 foreach my $id (keys %{$sdn_cfg->{ids}}) {
186 next if $id eq $routerid;
187 my $sdn = $sdn_cfg->{ids}->{$id};
188 die "asn $asn is already used by $id"
189 if (defined($sdn->{asn}) && $sdn->{asn} eq $asn);
190 }
191 }
192
193 sub sort_frr_config {
194 my $order = {};
195 $order->{''} = 0;
196 $order->{'vrf'} = 1;
197 $order->{'ipv4 unicast'} = 1;
198 $order->{'ipv6 unicast'} = 2;
199 $order->{'l2vpn evpn'} = 3;
200
201 my $a_val = 100;
202 my $b_val = 100;
203
204 $a_val = $order->{$a} if defined($order->{$a});
205 $b_val = $order->{$b} if defined($order->{$b});
206
207 if($a =~ /bgp (\d+)$/) {
208 $a_val = 2;
209 }
210
211 if($b =~ /bgp (\d+)$/) {
212 $b_val = 2;
213 }
214
215 return $a_val <=> $b_val;
216 }
217
218 sub generate_frr_recurse{
219 my ($final_config, $content, $parentkey, $level) = @_;
220
221 my $keylist = {};
222 $keylist->{vrf} = 1;
223 $keylist->{'address-family'} = 1;
224 $keylist->{router} = 1;
225
226 my $exitkeylist = {};
227 $exitkeylist->{vrf} = 1;
228 $exitkeylist->{'address-family'} = 1;
229
230 #fix me, make this generic
231 my $paddinglevel = undef;
232 if($level == 1 || $level == 2) {
233 $paddinglevel = $level - 1;
234 } elsif ($level == 3 || $level == 4) {
235 $paddinglevel = $level - 2;
236 }
237
238 my $padding = "";
239 $padding = ' ' x ($paddinglevel) if $paddinglevel;
240
241 if (ref $content eq ref {}) {
242 foreach my $key (sort sort_frr_config keys %$content) {
243 if ($parentkey && defined($keylist->{$parentkey})) {
244 push @{$final_config}, $padding."!";
245 push @{$final_config}, $padding."$parentkey $key";
246 } else {
247 push @{$final_config}, $padding."$key" if $key ne '' && !defined($keylist->{$key});
248 }
249
250 my $option = $content->{$key};
251 generate_frr_recurse($final_config, $option, $key, $level+1);
252
253 push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
254 }
255 }
256
257 if (ref $content eq 'ARRAY') {
258 foreach my $value (@$content) {
259 push @{$final_config}, $padding."$value";
260 }
261 }
262 }
263
264 sub write_controller_config {
265 my ($class, $plugin_config, $config) = @_;
266
267 my $final_config = [];
268 push @{$final_config}, "log syslog informational";
269 push @{$final_config}, "!";
270
271 generate_frr_recurse($final_config, $config->{frr}, undef, 0);
272
273 push @{$final_config}, "!";
274 push @{$final_config}, "line vty";
275 push @{$final_config}, "!";
276
277 my $rawconfig = join("\n", @{$final_config});
278
279
280 return if !$rawconfig;
281 return if !-d "/etc/frr";
282
283 my $frr_config_file = "/etc/frr/frr.conf";
284
285 my $writefh = IO::File->new($frr_config_file,">");
286 print $writefh $rawconfig;
287 $writefh->close();
288 }
289
290 sub reload_controller {
291 my ($class) = @_;
292
293 my $conf_file = "/etc/frr/frr.conf";
294 my $bin_path = "/usr/bin/vtysh";
295
296 my $err = sub {
297 my $line = shift;
298 if ($line =~ /^line (\S+)/) {
299 print "$line \n";
300 }
301 };
302
303 if (-e $conf_file && -e $bin_path) {
304 PVE::Tools::run_command([$bin_path, '-m', '-f', $conf_file], outfunc => {}, errfunc => $err);
305 }
306 }
307
308 1;
309
310