1 package PVE
::Network
::SDN
::Controllers
::EvpnPlugin
;
7 use PVE
::JSONSchema
qw(get_standard_option);
8 use PVE
::Tools
qw(run_command file_set_contents file_get_contents);
10 use PVE
::Network
::SDN
::Controllers
::Plugin
;
11 use PVE
::Network
::SDN
::Zones
::Plugin
;
13 use base
('PVE::Network::SDN::Controllers::Plugin');
23 description
=> "autonomous system number",
26 description
=> "peers address list.",
27 type
=> 'string', format
=> 'ip-list'
29 'gateway-nodes' => get_standard_option
('pve-node-list'),
30 'gateway-external-peers' => {
31 description
=> "upstream bgp peers address list.",
32 type
=> 'string', format
=> 'ip-list'
39 'asn' => { optional
=> 0 },
40 'peers' => { optional
=> 0 },
41 'gateway-nodes' => { optional
=> 1 },
42 'gateway-external-peers' => { optional
=> 1 },
46 # Plugin implementation
47 sub generate_controller_config
{
48 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
51 @peers = PVE
::Tools
::split_list
($plugin_config->{'peers'}) if $plugin_config->{'peers'};
53 my $asn = $plugin_config->{asn
};
54 my $gatewaynodes = $plugin_config->{'gateway-nodes'};
56 @gatewaypeers = PVE
::Tools
::split_list
($plugin_config->{'gateway-external-peers'}) if $plugin_config->{'gateway-external-peers'};
60 my $bgp = $config->{frr
}->{router
}->{"bgp $asn"} //= {};
62 my ($ifaceip, $interface) = PVE
::Network
::SDN
::Zones
::Plugin
::find_local_ip_interface_peers
(\
@peers);
64 my $is_gateway = undef;
65 my $local_node = PVE
::INotify
::nodename
();
67 foreach my $gatewaynode (PVE
::Tools
::split_list
($gatewaynodes)) {
68 $is_gateway = 1 if $gatewaynode eq $local_node;
71 my @controller_config = (
72 "bgp router-id $ifaceip",
73 "no bgp default ipv4-unicast",
77 foreach my $address (@peers) {
78 next if $address eq $ifaceip;
79 push @controller_config, "neighbor $address remote-as $asn";
83 foreach my $address (@gatewaypeers) {
84 push @controller_config, "neighbor $address remote-as external";
87 push(@{$bgp->{""}}, @controller_config);
89 @controller_config = ();
90 foreach my $address (@peers) {
91 next if $address eq $ifaceip;
92 push @controller_config, "neighbor $address activate";
94 push @controller_config, "advertise-all-vni";
95 push(@{$bgp->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
98 # import /32 routes of evpn network from vrf1 to default vrf (for packet return)
99 @controller_config = map { "neighbor $_ activate" } @gatewaypeers;
101 push(@{$bgp->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
102 push(@{$bgp->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
108 sub generate_controller_zone_config
{
109 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
112 my $vrfvxlan = $plugin_config->{'vrf-vxlan'};
113 my $asn = $controller->{asn
};
114 my $gatewaynodes = $controller->{'gateway-nodes'};
116 return if !$vrf || !$vrfvxlan || !$asn;
119 my @controller_config = ();
120 push @controller_config, "vni $vrfvxlan";
121 push(@{$config->{frr
}->{vrf
}->{"$vrf"}}, @controller_config);
123 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{""}}, "!");
125 my $local_node = PVE
::INotify
::nodename
();
127 my $is_gateway = grep { $_ eq $local_node } PVE
::Tools
::split_list
($gatewaynodes);
130 @controller_config = ();
131 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
132 push @controller_config, "import vrf $vrf";
133 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
134 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
136 @controller_config = ();
137 #redistribute connected to be able to route to local vms on the gateway
138 push @controller_config, "redistribute connected";
139 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
140 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
142 @controller_config = ();
143 #add default originate to announce 0.0.0.0/0 type5 route in evpn
144 push @controller_config, "default-originate ipv4";
145 push @controller_config, "default-originate ipv6";
146 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
153 my ($class, $controllerid, $zone_cfg) = @_;
155 # verify that zone is associated to this controller
156 foreach my $id (keys %{$zone_cfg->{ids
}}) {
157 my $zone = $zone_cfg->{ids
}->{$id};
158 die "controller $controllerid is used by $id"
159 if (defined($zone->{controller
}) && $zone->{controller
} eq $controllerid);
164 my ($class, $controllerid, $controller_cfg) = @_;
166 # we can only have 1 evpn controller / 1 asn by server
168 foreach my $id (keys %{$controller_cfg->{ids
}}) {
169 next if $id eq $controllerid;
170 my $controller = $controller_cfg->{ids
}->{$id};
171 die "only 1 evpn controller can be defined" if $controller->{type
} eq "evpn";
175 sub sort_frr_config
{
179 $order->{'ipv4 unicast'} = 1;
180 $order->{'ipv6 unicast'} = 2;
181 $order->{'l2vpn evpn'} = 3;
186 $a_val = $order->{$a} if defined($order->{$a});
187 $b_val = $order->{$b} if defined($order->{$b});
189 if ($a =~ /bgp (\d+)$/) {
193 if ($b =~ /bgp (\d+)$/) {
197 return $a_val <=> $b_val;
200 sub generate_frr_recurse
{
201 my ($final_config, $content, $parentkey, $level) = @_;
205 $keylist->{'address-family'} = 1;
206 $keylist->{router
} = 1;
208 my $exitkeylist = {};
209 $exitkeylist->{vrf
} = 1;
210 $exitkeylist->{'address-family'} = 1;
212 # FIXME: make this generic
213 my $paddinglevel = undef;
214 if ($level == 1 || $level == 2) {
215 $paddinglevel = $level - 1;
216 } elsif ($level == 3 || $level == 4) {
217 $paddinglevel = $level - 2;
221 $padding = ' ' x
($paddinglevel) if $paddinglevel;
223 if (ref $content eq 'HASH') {
224 foreach my $key (sort sort_frr_config
keys %$content) {
225 if ($parentkey && defined($keylist->{$parentkey})) {
226 push @{$final_config}, $padding."!";
227 push @{$final_config}, $padding."$parentkey $key";
228 } elsif ($key ne '' && !defined($keylist->{$key})) {
229 push @{$final_config}, $padding."$key";
232 my $option = $content->{$key};
233 generate_frr_recurse
($final_config, $option, $key, $level+1);
235 push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
239 if (ref $content eq 'ARRAY') {
240 push @{$final_config}, map { $padding . "$_" } @$content;
244 sub write_controller_config
{
245 my ($class, $plugin_config, $config) = @_;
247 my $nodename = PVE
::INotify
::nodename
();
249 my $final_config = [];
250 push @{$final_config}, "log syslog informational";
251 push @{$final_config}, "ip forwarding";
252 push @{$final_config}, "ipv6 forwarding";
253 push @{$final_config}, "frr defaults traditional";
254 push @{$final_config}, "service integrated-vtysh-config";
255 push @{$final_config}, "hostname $nodename";
256 push @{$final_config}, "!";
258 if (-e
"/etc/frr/frr.conf.local") {
259 generate_frr_recurse
($final_config, $config->{frr
}->{vrf
}, "vrf", 1);
260 push @{$final_config}, "!";
262 my $local_conf = file_get_contents
("/etc/frr/frr.conf.local");
264 push @{$final_config}, $local_conf;
266 generate_frr_recurse
($final_config, $config->{frr
}, undef, 0);
269 push @{$final_config}, "!";
270 push @{$final_config}, "line vty";
271 push @{$final_config}, "!";
273 my $rawconfig = join("\n", @{$final_config});
275 return if !$rawconfig;
276 return if !-d
"/etc/frr";
278 file_set_contents
("/etc/frr/frr.conf", $rawconfig);
281 sub reload_controller
{
284 my $conf_file = "/etc/frr/frr.conf";
285 my $bin_path = "/usr/lib/frr/frr-reload.py";
288 warn "missing $bin_path. Please install frr-pythontools package";
294 if ($line =~ /ERROR:/) {
299 if (-e
$conf_file && -e
$bin_path) {
300 run_command
([$bin_path, '--stdout', '--reload', $conf_file], outfunc
=> {}, errfunc
=> $err);