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'
37 'asn' => { optional
=> 0 },
38 'peers' => { optional
=> 0 },
39 'gateway-nodes' => { optional
=> 1 },
40 'gateway-external-peers' => { optional
=> 1 },
44 # Plugin implementation
45 sub generate_controller_config
{
46 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
48 my @peers = split(',', $plugin_config->{'peers'}) if $plugin_config->{'peers'};
50 my $asn = $plugin_config->{asn
};
51 my $gatewaynodes = $plugin_config->{'gateway-nodes'};
52 my @gatewaypeers = split(',', $plugin_config->{'gateway-external-peers'}) if $plugin_config->{'gateway-external-peers'};
56 my ($ifaceip, $interface) = PVE
::Network
::SDN
::Zones
::Plugin
::find_local_ip_interface_peers
(\
@peers);
58 my $is_gateway = undef;
59 my $local_node = PVE
::INotify
::nodename
();
61 foreach my $gatewaynode (PVE
::Tools
::split_list
($gatewaynodes)) {
62 $is_gateway = 1 if $gatewaynode eq $local_node;
65 my @controller_config = ();
67 push @controller_config, "bgp router-id $ifaceip";
68 push @controller_config, "no bgp default ipv4-unicast";
69 push @controller_config, "coalesce-time 1000";
71 foreach my $address (@peers) {
72 next if $address eq $ifaceip;
73 push @controller_config, "neighbor $address remote-as $asn";
77 foreach my $address (@gatewaypeers) {
78 push @controller_config, "neighbor $address remote-as external";
81 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{""}}, @controller_config);
83 @controller_config = ();
84 foreach my $address (@peers) {
85 next if $address eq $ifaceip;
86 push @controller_config, "neighbor $address activate";
88 push @controller_config, "advertise-all-vni";
89 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
93 @controller_config = ();
94 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
95 foreach my $address (@gatewaypeers) {
96 push @controller_config, "neighbor $address activate";
98 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
99 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"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 @controller_config = ();
125 my $is_gateway = undef;
126 my $local_node = PVE
::INotify
::nodename
();
128 foreach my $gatewaynode (PVE
::Tools
::split_list
($gatewaynodes)) {
129 $is_gateway = 1 if $gatewaynode eq $local_node;
134 @controller_config = ();
135 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
136 push @controller_config, "import vrf $vrf";
137 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
138 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
140 @controller_config = ();
141 #redistribute connected to be able to route to local vms on the gateway
142 push @controller_config, "redistribute connected";
143 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
144 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
146 @controller_config = ();
147 #add default originate to announce 0.0.0.0/0 type5 route in evpn
148 push @controller_config, "default-originate ipv4";
149 push @controller_config, "default-originate ipv6";
150 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
157 my ($class, $controllerid, $zone_cfg) = @_;
159 # verify that zone is associated to this controller
160 foreach my $id (keys %{$zone_cfg->{ids
}}) {
161 my $zone = $zone_cfg->{ids
}->{$id};
162 die "controller $controllerid is used by $id"
163 if (defined($zone->{controller
}) && $zone->{controller
} eq $controllerid);
168 my ($class, $controllerid, $controller_cfg) = @_;
170 # verify that asn is not already used by another controller
171 my $asn = $controller_cfg->{ids
}->{$controllerid}->{asn
};
172 foreach my $id (keys %{$controller_cfg->{ids
}}) {
173 next if $id eq $controllerid;
174 my $controller = $controller_cfg->{ids
}->{$id};
175 die "asn $asn is already used by $id"
176 if (defined($controller->{asn
}) && $controller->{asn
} eq $asn);
180 sub sort_frr_config
{
184 $order->{'ipv4 unicast'} = 1;
185 $order->{'ipv6 unicast'} = 2;
186 $order->{'l2vpn evpn'} = 3;
191 $a_val = $order->{$a} if defined($order->{$a});
192 $b_val = $order->{$b} if defined($order->{$b});
194 if($a =~ /bgp (\d+)$/) {
198 if($b =~ /bgp (\d+)$/) {
202 return $a_val <=> $b_val;
205 sub generate_frr_recurse
{
206 my ($final_config, $content, $parentkey, $level) = @_;
210 $keylist->{'address-family'} = 1;
211 $keylist->{router
} = 1;
213 my $exitkeylist = {};
214 $exitkeylist->{vrf
} = 1;
215 $exitkeylist->{'address-family'} = 1;
217 #fix me, make this generic
218 my $paddinglevel = undef;
219 if($level == 1 || $level == 2) {
220 $paddinglevel = $level - 1;
221 } elsif ($level == 3 || $level == 4) {
222 $paddinglevel = $level - 2;
226 $padding = ' ' x
($paddinglevel) if $paddinglevel;
228 if (ref $content eq ref {}) {
229 foreach my $key (sort sort_frr_config
keys %$content) {
230 if ($parentkey && defined($keylist->{$parentkey})) {
231 push @{$final_config}, $padding."!";
232 push @{$final_config}, $padding."$parentkey $key";
234 push @{$final_config}, $padding."$key" if $key ne '' && !defined($keylist->{$key});
237 my $option = $content->{$key};
238 generate_frr_recurse
($final_config, $option, $key, $level+1);
240 push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
244 if (ref $content eq 'ARRAY') {
245 foreach my $value (@$content) {
246 push @{$final_config}, $padding."$value";
251 sub write_controller_config
{
252 my ($class, $plugin_config, $config) = @_;
254 my $nodename = PVE
::INotify
::nodename
();
256 my $final_config = [];
257 push @{$final_config}, "log syslog informational";
258 push @{$final_config}, "ip forwarding";
259 push @{$final_config}, "ipv6 forwarding";
260 push @{$final_config}, "frr defaults traditional";
261 push @{$final_config}, "service integrated-vtysh-config";
262 push @{$final_config}, "hostname $nodename";
263 push @{$final_config}, "!";
265 if (-e
"/etc/frr/frr.conf.local") {
266 open my $fh, '<', '/etc/frr/frr.conf.local' or die "Can't open file $!";
267 generate_frr_recurse
($final_config, $config->{frr
}->{vrf
}, "vrf", 1);
268 push @{$final_config}, "!";
270 while (my $line = <$fh>) {
272 push @{$final_config}, $line;
275 generate_frr_recurse
($final_config, $config->{frr
}, undef, 0);
278 push @{$final_config}, "!";
279 push @{$final_config}, "line vty";
280 push @{$final_config}, "!";
282 my $rawconfig = join("\n", @{$final_config});
285 return if !$rawconfig;
286 return if !-d
"/etc/frr";
288 my $frr_config_file = "/etc/frr/frr.conf";
290 my $writefh = IO
::File-
>new($frr_config_file,">");
291 print $writefh $rawconfig;
295 sub reload_controller
{
298 my $conf_file = "/etc/frr/frr.conf";
299 my $bin_path = "/usr/lib/frr/frr-reload.py";
302 warn "missing $bin_path. Please install frr-pythontools package";
308 if ($line =~ /ERROR:/) {
313 if (-e
$conf_file && -e
$bin_path) {
314 PVE
::Tools
::run_command
([$bin_path, '--stdout', '--reload', $conf_file], outfunc
=> {}, errfunc
=> $err);