1 package PVE
::Network
::SDN
::Controllers
::EvpnPlugin
;
5 use PVE
::Network
::SDN
::Controllers
::Plugin
;
8 use PVE
::JSONSchema
qw(get_standard_option);
9 use PVE
::Network
::SDN
::Zones
::Plugin
;
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'
36 'asn' => { optional
=> 0 },
37 'peers' => { optional
=> 0 },
38 'gateway-nodes' => { optional
=> 1 },
39 'gateway-external-peers' => { optional
=> 1 },
43 # Plugin implementation
44 sub generate_controller_config
{
45 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
47 my @peers = split(',', $plugin_config->{'peers'}) if $plugin_config->{'peers'};
49 my $asn = $plugin_config->{asn
};
50 my $gatewaynodes = $plugin_config->{'gateway-nodes'};
51 my @gatewaypeers = split(',', $plugin_config->{'gateway-external-peers'}) if $plugin_config->{'gateway-external-peers'};
55 my $bgp = $config->{frr
}->{router
}->{"bgp $asn"} //= {};
57 my ($ifaceip, $interface) = PVE
::Network
::SDN
::Zones
::Plugin
::find_local_ip_interface_peers
(\
@peers);
59 my $is_gateway = undef;
60 my $local_node = PVE
::INotify
::nodename
();
62 foreach my $gatewaynode (PVE
::Tools
::split_list
($gatewaynodes)) {
63 $is_gateway = 1 if $gatewaynode eq $local_node;
66 my @controller_config = (
67 "bgp router-id $ifaceip",
68 "no bgp default ipv4-unicast",
72 foreach my $address (@peers) {
73 next if $address eq $ifaceip;
74 push @controller_config, "neighbor $address remote-as $asn";
78 foreach my $address (@gatewaypeers) {
79 push @controller_config, "neighbor $address remote-as external";
82 push(@{$bgp->{""}}, @controller_config);
84 @controller_config = ();
85 foreach my $address (@peers) {
86 next if $address eq $ifaceip;
87 push @controller_config, "neighbor $address activate";
89 push @controller_config, "advertise-all-vni";
90 push(@{$bgp->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
93 # import /32 routes of evpn network from vrf1 to default vrf (for packet return)
94 @controller_config = map { "neighbor $_ activate" } @gatewaypeers;
96 push(@{$bgp->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
97 push(@{$bgp->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
103 sub generate_controller_zone_config
{
104 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
107 my $vrfvxlan = $plugin_config->{'vrf-vxlan'};
108 my $asn = $controller->{asn
};
109 my $gatewaynodes = $controller->{'gateway-nodes'};
111 return if !$vrf || !$vrfvxlan || !$asn;
114 my @controller_config = ();
115 push @controller_config, "vni $vrfvxlan";
116 push(@{$config->{frr
}->{vrf
}->{"$vrf"}}, @controller_config);
118 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{""}}, "!");
120 my $local_node = PVE
::INotify
::nodename
();
122 my $is_gateway = grep { $_ eq $local_node } PVE
::Tools
::split_list
($gatewaynodes);
125 @controller_config = ();
126 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
127 push @controller_config, "import vrf $vrf";
128 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
129 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
131 @controller_config = ();
132 #redistribute connected to be able to route to local vms on the gateway
133 push @controller_config, "redistribute connected";
134 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
135 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
137 @controller_config = ();
138 #add default originate to announce 0.0.0.0/0 type5 route in evpn
139 push @controller_config, "default-originate ipv4";
140 push @controller_config, "default-originate ipv6";
141 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
148 my ($class, $controllerid, $zone_cfg) = @_;
150 # verify that zone is associated to this controller
151 foreach my $id (keys %{$zone_cfg->{ids
}}) {
152 my $zone = $zone_cfg->{ids
}->{$id};
153 die "controller $controllerid is used by $id"
154 if (defined($zone->{controller
}) && $zone->{controller
} eq $controllerid);
159 my ($class, $controllerid, $controller_cfg) = @_;
161 # we can only have 1 evpn controller / 1 asn by server
163 foreach my $id (keys %{$controller_cfg->{ids
}}) {
164 next if $id eq $controllerid;
165 my $controller = $controller_cfg->{ids
}->{$id};
166 die "only 1 evpn controller can be defined" if $controller->{type
} eq "evpn";
170 sub sort_frr_config
{
174 $order->{'ipv4 unicast'} = 1;
175 $order->{'ipv6 unicast'} = 2;
176 $order->{'l2vpn evpn'} = 3;
181 $a_val = $order->{$a} if defined($order->{$a});
182 $b_val = $order->{$b} if defined($order->{$b});
184 if ($a =~ /bgp (\d+)$/) {
188 if ($b =~ /bgp (\d+)$/) {
192 return $a_val <=> $b_val;
195 sub generate_frr_recurse
{
196 my ($final_config, $content, $parentkey, $level) = @_;
200 $keylist->{'address-family'} = 1;
201 $keylist->{router
} = 1;
203 my $exitkeylist = {};
204 $exitkeylist->{vrf
} = 1;
205 $exitkeylist->{'address-family'} = 1;
207 # FIXME: make this generic
208 my $paddinglevel = undef;
209 if ($level == 1 || $level == 2) {
210 $paddinglevel = $level - 1;
211 } elsif ($level == 3 || $level == 4) {
212 $paddinglevel = $level - 2;
216 $padding = ' ' x
($paddinglevel) if $paddinglevel;
218 if (ref $content eq 'HASH') {
219 foreach my $key (sort sort_frr_config
keys %$content) {
220 if ($parentkey && defined($keylist->{$parentkey})) {
221 push @{$final_config}, $padding."!";
222 push @{$final_config}, $padding."$parentkey $key";
223 } elsif ($key ne '' && !defined($keylist->{$key})) {
224 push @{$final_config}, $padding."$key";
227 my $option = $content->{$key};
228 generate_frr_recurse
($final_config, $option, $key, $level+1);
230 push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
234 if (ref $content eq 'ARRAY') {
235 push @{$final_config}, map { $padding . "$_" } @$content;
239 sub write_controller_config
{
240 my ($class, $plugin_config, $config) = @_;
242 my $nodename = PVE
::INotify
::nodename
();
244 my $final_config = [];
245 push @{$final_config}, "log syslog informational";
246 push @{$final_config}, "ip forwarding";
247 push @{$final_config}, "ipv6 forwarding";
248 push @{$final_config}, "frr defaults traditional";
249 push @{$final_config}, "service integrated-vtysh-config";
250 push @{$final_config}, "hostname $nodename";
251 push @{$final_config}, "!";
253 if (-e
"/etc/frr/frr.conf.local") {
254 open my $fh, '<', '/etc/frr/frr.conf.local' or die "Can't open file $!";
255 generate_frr_recurse
($final_config, $config->{frr
}->{vrf
}, "vrf", 1);
256 push @{$final_config}, "!";
258 while (my $line = <$fh>) {
260 push @{$final_config}, $line;
263 generate_frr_recurse
($final_config, $config->{frr
}, undef, 0);
266 push @{$final_config}, "!";
267 push @{$final_config}, "line vty";
268 push @{$final_config}, "!";
270 my $rawconfig = join("\n", @{$final_config});
273 return if !$rawconfig;
274 return if !-d
"/etc/frr";
276 my $frr_config_file = "/etc/frr/frr.conf";
278 my $writefh = IO
::File-
>new($frr_config_file,">");
279 print $writefh $rawconfig;
283 sub reload_controller
{
286 my $conf_file = "/etc/frr/frr.conf";
287 my $bin_path = "/usr/lib/frr/frr-reload.py";
290 warn "missing $bin_path. Please install frr-pythontools package";
296 if ($line =~ /ERROR:/) {
301 if (-e
$conf_file && -e
$bin_path) {
302 PVE
::Tools
::run_command
([$bin_path, '--stdout', '--reload', $conf_file], outfunc
=> {}, errfunc
=> $err);