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) = @_;
50 my @peers = split(',', $plugin_config->{'peers'}) if $plugin_config->{'peers'};
52 my $asn = $plugin_config->{asn
};
53 my $gatewaynodes = $plugin_config->{'gateway-nodes'};
54 my @gatewaypeers = split(',', $plugin_config->{'gateway-external-peers'}) if $plugin_config->{'gateway-external-peers'};
58 my $bgp = $config->{frr
}->{router
}->{"bgp $asn"} //= {};
60 my ($ifaceip, $interface) = PVE
::Network
::SDN
::Zones
::Plugin
::find_local_ip_interface_peers
(\
@peers);
62 my $is_gateway = undef;
63 my $local_node = PVE
::INotify
::nodename
();
65 foreach my $gatewaynode (PVE
::Tools
::split_list
($gatewaynodes)) {
66 $is_gateway = 1 if $gatewaynode eq $local_node;
69 my @controller_config = (
70 "bgp router-id $ifaceip",
71 "no bgp default ipv4-unicast",
75 foreach my $address (@peers) {
76 next if $address eq $ifaceip;
77 push @controller_config, "neighbor $address remote-as $asn";
81 foreach my $address (@gatewaypeers) {
82 push @controller_config, "neighbor $address remote-as external";
85 push(@{$bgp->{""}}, @controller_config);
87 @controller_config = ();
88 foreach my $address (@peers) {
89 next if $address eq $ifaceip;
90 push @controller_config, "neighbor $address activate";
92 push @controller_config, "advertise-all-vni";
93 push(@{$bgp->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
96 # import /32 routes of evpn network from vrf1 to default vrf (for packet return)
97 @controller_config = map { "neighbor $_ activate" } @gatewaypeers;
99 push(@{$bgp->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
100 push(@{$bgp->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
106 sub generate_controller_zone_config
{
107 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
110 my $vrfvxlan = $plugin_config->{'vrf-vxlan'};
111 my $asn = $controller->{asn
};
112 my $gatewaynodes = $controller->{'gateway-nodes'};
114 return if !$vrf || !$vrfvxlan || !$asn;
117 my @controller_config = ();
118 push @controller_config, "vni $vrfvxlan";
119 push(@{$config->{frr
}->{vrf
}->{"$vrf"}}, @controller_config);
121 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{""}}, "!");
123 my $local_node = PVE
::INotify
::nodename
();
125 my $is_gateway = grep { $_ eq $local_node } PVE
::Tools
::split_list
($gatewaynodes);
128 @controller_config = ();
129 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
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);
134 @controller_config = ();
135 #redistribute connected to be able to route to local vms on the gateway
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);
140 @controller_config = ();
141 #add default originate to announce 0.0.0.0/0 type5 route in evpn
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);
151 my ($class, $controllerid, $zone_cfg) = @_;
153 # verify that zone is associated to this controller
154 foreach my $id (keys %{$zone_cfg->{ids
}}) {
155 my $zone = $zone_cfg->{ids
}->{$id};
156 die "controller $controllerid is used by $id"
157 if (defined($zone->{controller
}) && $zone->{controller
} eq $controllerid);
162 my ($class, $controllerid, $controller_cfg) = @_;
164 # we can only have 1 evpn controller / 1 asn by server
166 foreach my $id (keys %{$controller_cfg->{ids
}}) {
167 next if $id eq $controllerid;
168 my $controller = $controller_cfg->{ids
}->{$id};
169 die "only 1 evpn controller can be defined" if $controller->{type
} eq "evpn";
173 sub sort_frr_config
{
177 $order->{'ipv4 unicast'} = 1;
178 $order->{'ipv6 unicast'} = 2;
179 $order->{'l2vpn evpn'} = 3;
184 $a_val = $order->{$a} if defined($order->{$a});
185 $b_val = $order->{$b} if defined($order->{$b});
187 if ($a =~ /bgp (\d+)$/) {
191 if ($b =~ /bgp (\d+)$/) {
195 return $a_val <=> $b_val;
198 sub generate_frr_recurse
{
199 my ($final_config, $content, $parentkey, $level) = @_;
203 $keylist->{'address-family'} = 1;
204 $keylist->{router
} = 1;
206 my $exitkeylist = {};
207 $exitkeylist->{vrf
} = 1;
208 $exitkeylist->{'address-family'} = 1;
210 # FIXME: make this generic
211 my $paddinglevel = undef;
212 if ($level == 1 || $level == 2) {
213 $paddinglevel = $level - 1;
214 } elsif ($level == 3 || $level == 4) {
215 $paddinglevel = $level - 2;
219 $padding = ' ' x
($paddinglevel) if $paddinglevel;
221 if (ref $content eq 'HASH') {
222 foreach my $key (sort sort_frr_config
keys %$content) {
223 if ($parentkey && defined($keylist->{$parentkey})) {
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";
230 my $option = $content->{$key};
231 generate_frr_recurse
($final_config, $option, $key, $level+1);
233 push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
237 if (ref $content eq 'ARRAY') {
238 push @{$final_config}, map { $padding . "$_" } @$content;
242 sub write_controller_config
{
243 my ($class, $plugin_config, $config) = @_;
245 my $nodename = PVE
::INotify
::nodename
();
247 my $final_config = [];
248 push @{$final_config}, "log syslog informational";
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";
254 push @{$final_config}, "!";
256 if (-e
"/etc/frr/frr.conf.local") {
257 generate_frr_recurse
($final_config, $config->{frr
}->{vrf
}, "vrf", 1);
258 push @{$final_config}, "!";
260 my $local_conf = file_get_contents
("/etc/frr/frr.conf.local");
262 push @{$final_config}, $local_conf;
264 generate_frr_recurse
($final_config, $config->{frr
}, undef, 0);
267 push @{$final_config}, "!";
268 push @{$final_config}, "line vty";
269 push @{$final_config}, "!";
271 my $rawconfig = join("\n", @{$final_config});
273 return if !$rawconfig;
274 return if !-d
"/etc/frr";
276 file_set_contents
("/etc/frr/frr.conf", $rawconfig);
279 sub reload_controller
{
282 my $conf_file = "/etc/frr/frr.conf";
283 my $bin_path = "/usr/lib/frr/frr-reload.py";
286 warn "missing $bin_path. Please install frr-pythontools package";
292 if ($line =~ /ERROR:/) {
297 if (-e
$conf_file && -e
$bin_path) {
298 run_command
([$bin_path, '--stdout', '--reload', $conf_file], outfunc
=> {}, errfunc
=> $err);