]> git.proxmox.com Git - pve-network.git/blame - PVE/Network/SDN/Controllers/EvpnPlugin.pm
qinq: use new model + ovs/classic bridge support
[pve-network.git] / PVE / Network / SDN / Controllers / EvpnPlugin.pm
CommitLineData
fa253735 1package PVE::Network::SDN::Controllers::EvpnPlugin;
32602a38
AD
2
3use strict;
4use warnings;
cdf2c819 5
074d270b
AD
6use PVE::INotify;
7use PVE::JSONSchema qw(get_standard_option);
cdf2c819
TL
8use PVE::Tools qw(run_command file_set_contents file_get_contents);
9
10use PVE::Network::SDN::Controllers::Plugin;
1f543c5f 11use PVE::Network::SDN::Zones::Plugin;
cdf2c819 12
f5eabba0 13use base('PVE::Network::SDN::Controllers::Plugin');
32602a38
AD
14
15sub type {
fa253735 16 return 'evpn';
8fb1ee7f
AD
17}
18
32602a38
AD
19sub properties {
20 return {
92526f0e
TL
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 },
074d270b 29 'gateway-nodes' => get_standard_option('pve-node-list'),
92526f0e
TL
30 'gateway-external-peers' => {
31 description => "upstream bgp peers address list.",
32 type => 'string', format => 'ip-list'
33 },
32602a38
AD
34 };
35}
36
37sub options {
32602a38 38 return {
92526f0e
TL
39 'asn' => { optional => 0 },
40 'peers' => { optional => 0 },
074d270b
AD
41 'gateway-nodes' => { optional => 1 },
42 'gateway-external-peers' => { optional => 1 },
32602a38
AD
43 };
44}
45
46# Plugin implementation
8fb1ee7f 47sub generate_controller_config {
56cdcac9 48 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
32602a38 49
32602a38
AD
50 my @peers = split(',', $plugin_config->{'peers'}) if $plugin_config->{'peers'};
51
074d270b 52 my $asn = $plugin_config->{asn};
074d270b
AD
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;
32602a38 57
92526f0e
TL
58 my $bgp = $config->{frr}->{router}->{"bgp $asn"} //= {};
59
1f543c5f 60 my ($ifaceip, $interface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers);
32602a38 61
074d270b
AD
62 my $is_gateway = undef;
63 my $local_node = PVE::INotify::nodename();
64
65 foreach my $gatewaynode (PVE::Tools::split_list($gatewaynodes)) {
92526f0e 66 $is_gateway = 1 if $gatewaynode eq $local_node;
074d270b 67 }
17854295 68
92526f0e
TL
69 my @controller_config = (
70 "bgp router-id $ifaceip",
71 "no bgp default ipv4-unicast",
72 "coalesce-time 1000",
73 );
32602a38
AD
74
75 foreach my $address (@peers) {
76 next if $address eq $ifaceip;
56cdcac9 77 push @controller_config, "neighbor $address remote-as $asn";
7d35eaf5 78 }
074d270b
AD
79
80 if ($is_gateway) {
81 foreach my $address (@gatewaypeers) {
56cdcac9 82 push @controller_config, "neighbor $address remote-as external";
074d270b
AD
83 }
84 }
92526f0e 85 push(@{$bgp->{""}}, @controller_config);
074d270b 86
56cdcac9 87 @controller_config = ();
32602a38
AD
88 foreach my $address (@peers) {
89 next if $address eq $ifaceip;
56cdcac9 90 push @controller_config, "neighbor $address activate";
32602a38 91 }
56cdcac9 92 push @controller_config, "advertise-all-vni";
92526f0e 93 push(@{$bgp->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
32602a38 94
074d270b 95 if ($is_gateway) {
92526f0e
TL
96 # import /32 routes of evpn network from vrf1 to default vrf (for packet return)
97 @controller_config = map { "neighbor $_ activate" } @gatewaypeers;
074d270b 98
92526f0e
TL
99 push(@{$bgp->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
100 push(@{$bgp->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
074d270b
AD
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
92526f0e 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
0589eb09
AD
123 my $local_node = PVE::INotify::nodename();
124
92526f0e 125 my $is_gateway = grep { $_ eq $local_node } PVE::Tools::split_list($gatewaynodes);
0589eb09
AD
126 if ($is_gateway) {
127
56cdcac9 128 @controller_config = ();
0589eb09 129 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
56cdcac9
AD
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);
0589eb09 133
56cdcac9 134 @controller_config = ();
0589eb09 135 #redistribute connected to be able to route to local vms on the gateway
56cdcac9
AD
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);
0589eb09 139
56cdcac9 140 @controller_config = ();
0589eb09 141 #add default originate to announce 0.0.0.0/0 type5 route in evpn
56cdcac9
AD
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);
0589eb09
AD
145 }
146
147 return $config;
148}
149
32602a38 150sub on_delete_hook {
56cdcac9 151 my ($class, $controllerid, $zone_cfg) = @_;
32602a38 152
56cdcac9
AD
153 # verify that zone is associated to this controller
154 foreach my $id (keys %{$zone_cfg->{ids}}) {
92526f0e
TL
155 my $zone = $zone_cfg->{ids}->{$id};
156 die "controller $controllerid is used by $id"
157 if (defined($zone->{controller}) && $zone->{controller} eq $controllerid);
5bda8607 158 }
32602a38
AD
159}
160
161sub on_update_hook {
56cdcac9 162 my ($class, $controllerid, $controller_cfg) = @_;
5bda8607 163
c7bb4ac5
AD
164 # we can only have 1 evpn controller / 1 asn by server
165
56cdcac9
AD
166 foreach my $id (keys %{$controller_cfg->{ids}}) {
167 next if $id eq $controllerid;
92526f0e
TL
168 my $controller = $controller_cfg->{ids}->{$id};
169 die "only 1 evpn controller can be defined" if $controller->{type} eq "evpn";
5bda8607 170 }
32602a38
AD
171}
172
8fb1ee7f
AD
173sub 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
92526f0e 187 if ($a =~ /bgp (\d+)$/) {
8fb1ee7f
AD
188 $a_val = 2;
189 }
190
92526f0e 191 if ($b =~ /bgp (\d+)$/) {
8fb1ee7f
AD
192 $b_val = 2;
193 }
194
195 return $a_val <=> $b_val;
196}
197
198sub 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
92526f0e 210 # FIXME: make this generic
8fb1ee7f 211 my $paddinglevel = undef;
92526f0e
TL
212 if ($level == 1 || $level == 2) {
213 $paddinglevel = $level - 1;
8fb1ee7f 214 } elsif ($level == 3 || $level == 4) {
92526f0e 215 $paddinglevel = $level - 2;
8fb1ee7f
AD
216 }
217
218 my $padding = "";
219 $padding = ' ' x ($paddinglevel) if $paddinglevel;
220
92526f0e 221 if (ref $content eq 'HASH') {
8fb1ee7f
AD
222 foreach my $key (sort sort_frr_config keys %$content) {
223 if ($parentkey && defined($keylist->{$parentkey})) {
92526f0e
TL
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";
8fb1ee7f
AD
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 }
32602a38 236
8fb1ee7f 237 if (ref $content eq 'ARRAY') {
92526f0e 238 push @{$final_config}, map { $padding . "$_" } @$content;
8fb1ee7f
AD
239 }
240}
241
242sub write_controller_config {
243 my ($class, $plugin_config, $config) = @_;
244
659c27c2
AD
245 my $nodename = PVE::INotify::nodename();
246
8fb1ee7f
AD
247 my $final_config = [];
248 push @{$final_config}, "log syslog informational";
659c27c2
AD
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";
8fb1ee7f
AD
254 push @{$final_config}, "!";
255
0d1ab7dc 256 if (-e "/etc/frr/frr.conf.local") {
0d1ab7dc
AD
257 generate_frr_recurse($final_config, $config->{frr}->{vrf}, "vrf", 1);
258 push @{$final_config}, "!";
259
cdf2c819
TL
260 my $local_conf = file_get_contents("/etc/frr/frr.conf.local");
261 chomp ($local_conf);
262 push @{$final_config}, $local_conf;
0d1ab7dc
AD
263 } else {
264 generate_frr_recurse($final_config, $config->{frr}, undef, 0);
265 }
8fb1ee7f
AD
266
267 push @{$final_config}, "!";
268 push @{$final_config}, "line vty";
269 push @{$final_config}, "!";
270
271 my $rawconfig = join("\n", @{$final_config});
272
8fb1ee7f
AD
273 return if !$rawconfig;
274 return if !-d "/etc/frr";
275
cdf2c819 276 file_set_contents("/etc/frr/frr.conf", $rawconfig);
8fb1ee7f
AD
277}
278
fa609bdd
AD
279sub reload_controller {
280 my ($class) = @_;
281
282 my $conf_file = "/etc/frr/frr.conf";
659c27c2
AD
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 }
fa609bdd
AD
289
290 my $err = sub {
291 my $line = shift;
659c27c2
AD
292 if ($line =~ /ERROR:/) {
293 warn "$line \n";
fa609bdd
AD
294 }
295 };
296
297 if (-e $conf_file && -e $bin_path) {
cdf2c819 298 run_command([$bin_path, '--stdout', '--reload', $conf_file], outfunc => {}, errfunc => $err);
fa609bdd
AD
299 }
300}
301
8fb1ee7f 3021;
32602a38 303
0589eb09 304