1 package PVE
::Network
::SDN
::Controllers
::EvpnPlugin
;
5 use PVE
::Network
::SDN
::Controllers
::Plugin
;
6 use PVE
::Tools
qw(run_command);
8 use PVE
::JSONSchema
qw(get_standard_option);
10 use base
('PVE::Network::SDN::Controllers::Plugin');
20 description
=> "autonomous system number",
23 description
=> "peers address list.",
24 type
=> 'string', format
=> 'ip-list'
26 'gateway-nodes' => get_standard_option
('pve-node-list'),
27 'gateway-external-peers' => {
28 description
=> "upstream bgp peers address list.",
29 type
=> 'string', format
=> 'ip-list'
37 'asn' => { optional
=> 0 },
38 'peers' => { optional
=> 0 },
39 'gateway-nodes' => { optional
=> 1 },
40 'gateway-external-peers' => { optional
=> 1 },
44 sub get_local_route_ip
{
48 my $interface = undef;
50 run_command
(['/sbin/ip', 'route', 'get', $targetip], outfunc
=> sub {
51 if ($_[0] =~ m/src ($PVE::Tools::IPRE)/) {
54 if ($_[0] =~ m/dev (\S+)/) {
59 return ($ip, $interface);
62 sub find_local_ip_interface
{
65 my $network_config = PVE
::INotify
::read_file
('interfaces');
66 my $ifaces = $network_config->{ifaces
};
67 #is a local ip member of peers list ?
68 foreach my $address (@{$peers}) {
69 while (my $interface = each %$ifaces) {
70 my $ip = $ifaces->{$interface}->{address
};
71 if ($ip && $ip eq $address) {
72 return ($ip, $interface);
77 #if peer is remote, find source with ip route
78 foreach my $address (@{$peers}) {
79 my ($ip, $interface) = get_local_route_ip
($address);
80 return ($ip, $interface);
84 # Plugin implementation
85 sub generate_controller_config
{
86 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
88 my @peers = split(',', $plugin_config->{'peers'}) if $plugin_config->{'peers'};
90 my $asn = $plugin_config->{asn
};
91 my $gatewaynodes = $plugin_config->{'gateway-nodes'};
92 my @gatewaypeers = split(',', $plugin_config->{'gateway-external-peers'}) if $plugin_config->{'gateway-external-peers'};
96 my ($ifaceip, $interface) = find_local_ip_interface
(\
@peers);
98 my $is_gateway = undef;
99 my $local_node = PVE
::INotify
::nodename
();
101 foreach my $gatewaynode (PVE
::Tools
::split_list
($gatewaynodes)) {
102 $is_gateway = 1 if $gatewaynode eq $local_node;
105 my @controller_config = ();
107 push @controller_config, "bgp router-id $ifaceip";
108 push @controller_config, "no bgp default ipv4-unicast";
109 push @controller_config, "coalesce-time 1000";
111 foreach my $address (@peers) {
112 next if $address eq $ifaceip;
113 push @controller_config, "neighbor $address remote-as $asn";
117 foreach my $address (@gatewaypeers) {
118 push @controller_config, "neighbor $address remote-as external";
121 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{""}}, @controller_config);
123 @controller_config = ();
124 foreach my $address (@peers) {
125 next if $address eq $ifaceip;
126 push @controller_config, "neighbor $address activate";
128 push @controller_config, "advertise-all-vni";
129 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
133 @controller_config = ();
134 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
135 foreach my $address (@gatewaypeers) {
136 push @controller_config, "neighbor $address activate";
138 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
139 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
146 sub generate_controller_zone_config
{
147 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
150 my $vrfvxlan = $plugin_config->{'vrf-vxlan'};
151 my $asn = $controller->{asn
};
152 my $gatewaynodes = $controller->{'gateway-nodes'};
154 return if !$vrf || !$vrfvxlan || !$asn;
157 my @controller_config = ();
158 push @controller_config, "vni $vrfvxlan";
159 push(@{$config->{frr
}->{vrf
}->{"$vrf"}}, @controller_config);
161 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{""}}, "!");
163 @controller_config = ();
165 my $is_gateway = undef;
166 my $local_node = PVE
::INotify
::nodename
();
168 foreach my $gatewaynode (PVE
::Tools
::split_list
($gatewaynodes)) {
169 $is_gateway = 1 if $gatewaynode eq $local_node;
174 @controller_config = ();
175 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
176 push @controller_config, "import vrf $vrf";
177 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
178 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
180 @controller_config = ();
181 #redistribute connected to be able to route to local vms on the gateway
182 push @controller_config, "redistribute connected";
183 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
184 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
186 @controller_config = ();
187 #add default originate to announce 0.0.0.0/0 type5 route in evpn
188 push @controller_config, "default-originate ipv4";
189 push @controller_config, "default-originate ipv6";
190 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
197 my ($class, $controllerid, $zone_cfg) = @_;
199 # verify that zone is associated to this controller
200 foreach my $id (keys %{$zone_cfg->{ids
}}) {
201 my $zone = $zone_cfg->{ids
}->{$id};
202 die "controller $controllerid is used by $id"
203 if (defined($zone->{controller
}) && $zone->{controller
} eq $controllerid);
208 my ($class, $controllerid, $controller_cfg) = @_;
210 # verify that asn is not already used by another controller
211 my $asn = $controller_cfg->{ids
}->{$controllerid}->{asn
};
212 foreach my $id (keys %{$controller_cfg->{ids
}}) {
213 next if $id eq $controllerid;
214 my $controller = $controller_cfg->{ids
}->{$id};
215 die "asn $asn is already used by $id"
216 if (defined($controller->{asn
}) && $controller->{asn
} eq $asn);
220 sub sort_frr_config
{
224 $order->{'ipv4 unicast'} = 1;
225 $order->{'ipv6 unicast'} = 2;
226 $order->{'l2vpn evpn'} = 3;
231 $a_val = $order->{$a} if defined($order->{$a});
232 $b_val = $order->{$b} if defined($order->{$b});
234 if($a =~ /bgp (\d+)$/) {
238 if($b =~ /bgp (\d+)$/) {
242 return $a_val <=> $b_val;
245 sub generate_frr_recurse
{
246 my ($final_config, $content, $parentkey, $level) = @_;
250 $keylist->{'address-family'} = 1;
251 $keylist->{router
} = 1;
253 my $exitkeylist = {};
254 $exitkeylist->{vrf
} = 1;
255 $exitkeylist->{'address-family'} = 1;
257 #fix me, make this generic
258 my $paddinglevel = undef;
259 if($level == 1 || $level == 2) {
260 $paddinglevel = $level - 1;
261 } elsif ($level == 3 || $level == 4) {
262 $paddinglevel = $level - 2;
266 $padding = ' ' x
($paddinglevel) if $paddinglevel;
268 if (ref $content eq ref {}) {
269 foreach my $key (sort sort_frr_config
keys %$content) {
270 if ($parentkey && defined($keylist->{$parentkey})) {
271 push @{$final_config}, $padding."!";
272 push @{$final_config}, $padding."$parentkey $key";
274 push @{$final_config}, $padding."$key" if $key ne '' && !defined($keylist->{$key});
277 my $option = $content->{$key};
278 generate_frr_recurse
($final_config, $option, $key, $level+1);
280 push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
284 if (ref $content eq 'ARRAY') {
285 foreach my $value (@$content) {
286 push @{$final_config}, $padding."$value";
291 sub write_controller_config
{
292 my ($class, $plugin_config, $config) = @_;
294 my $nodename = PVE
::INotify
::nodename
();
296 my $final_config = [];
297 push @{$final_config}, "log syslog informational";
298 push @{$final_config}, "ip forwarding";
299 push @{$final_config}, "ipv6 forwarding";
300 push @{$final_config}, "frr defaults traditional";
301 push @{$final_config}, "service integrated-vtysh-config";
302 push @{$final_config}, "hostname $nodename";
303 push @{$final_config}, "!";
305 generate_frr_recurse
($final_config, $config->{frr
}, undef, 0);
307 push @{$final_config}, "!";
308 push @{$final_config}, "line vty";
309 push @{$final_config}, "!";
311 my $rawconfig = join("\n", @{$final_config});
314 return if !$rawconfig;
315 return if !-d
"/etc/frr";
317 my $frr_config_file = "/etc/frr/frr.conf";
319 my $writefh = IO
::File-
>new($frr_config_file,">");
320 print $writefh $rawconfig;
324 sub reload_controller
{
327 my $conf_file = "/etc/frr/frr.conf";
328 my $bin_path = "/usr/lib/frr/frr-reload.py";
331 warn "missing $bin_path. Please install frr-pythontools package";
337 if ($line =~ /ERROR:/) {
342 if (-e
$conf_file && -e
$bin_path) {
343 PVE
::Tools
::run_command
([$bin_path, '--stdout', '--reload', $conf_file], outfunc
=> {}, errfunc
=> $err);