1 package PVE
::Network
::SDN
::Controllers
::EvpnPlugin
;
5 use PVE
::Network
::SDN
::Controllers
::Plugin
;
8 use PVE
::JSONSchema
qw(get_standard_option);
10 use base
('PVE::Network::SDN::Controllers::Plugin');
20 minimum
=> 1, maximum
=> 4096,
21 description
=> 'Uplink interface',
25 description
=> "autonomous system number",
28 description
=> "peers address list.",
29 type
=> 'string', format
=> 'ip-list'
31 'gateway-nodes' => get_standard_option
('pve-node-list'),
32 'gateway-external-peers' => {
33 description
=> "upstream bgp peers address list.",
34 type
=> 'string', format
=> 'ip-list'
42 'uplink-id' => { optional
=> 0 },
43 'asn' => { optional
=> 0 },
44 'peers' => { optional
=> 0 },
45 'gateway-nodes' => { optional
=> 1 },
46 'gateway-external-peers' => { optional
=> 1 },
50 # Plugin implementation
51 sub generate_controller_config
{
52 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
54 my @peers = split(',', $plugin_config->{'peers'}) if $plugin_config->{'peers'};
56 my $asn = $plugin_config->{asn
};
57 my $uplink = $plugin_config->{'uplink-id'};
58 my $gatewaynodes = $plugin_config->{'gateway-nodes'};
59 my @gatewaypeers = split(',', $plugin_config->{'gateway-external-peers'}) if $plugin_config->{'gateway-external-peers'};
63 my $iface = "uplink$uplink";
66 if($uplinks->{$uplink}->{name
}) {
67 $iface = $uplinks->{$uplink}->{name
};
68 $ifaceip = PVE
::Network
::SDN
::Controllers
::Plugin
::get_first_local_ipv4_from_interface
($iface);
71 my $is_gateway = undef;
72 my $local_node = PVE
::INotify
::nodename
();
74 foreach my $gatewaynode (PVE
::Tools
::split_list
($gatewaynodes)) {
75 $is_gateway = 1 if $gatewaynode eq $local_node;
78 my @controller_config = ();
80 push @controller_config, "bgp router-id $ifaceip";
81 push @controller_config, "no bgp default ipv4-unicast";
82 push @controller_config, "coalesce-time 1000";
84 foreach my $address (@peers) {
85 next if $address eq $ifaceip;
86 push @controller_config, "neighbor $address remote-as $asn";
90 foreach my $address (@gatewaypeers) {
91 push @controller_config, "neighbor $address remote-as external";
94 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{""}}, @controller_config);
96 @controller_config = ();
97 foreach my $address (@peers) {
98 next if $address eq $ifaceip;
99 push @controller_config, "neighbor $address activate";
101 push @controller_config, "advertise-all-vni";
102 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
106 @controller_config = ();
107 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
108 foreach my $address (@gatewaypeers) {
109 push @controller_config, "neighbor $address activate";
111 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
112 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
119 sub generate_controller_zone_config
{
120 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
123 my $vrfvxlan = $plugin_config->{'vrf-vxlan'};
124 my $asn = $controller->{asn
};
125 my $gatewaynodes = $controller->{'gateway-nodes'};
127 return if !$vrf || !$vrfvxlan || !$asn;
130 my @controller_config = ();
131 push @controller_config, "vni $vrfvxlan";
132 push(@{$config->{frr
}->{vrf
}->{"$vrf"}}, @controller_config);
134 @controller_config = ();
136 my $is_gateway = undef;
137 my $local_node = PVE
::INotify
::nodename
();
139 foreach my $gatewaynode (PVE
::Tools
::split_list
($gatewaynodes)) {
140 $is_gateway = 1 if $gatewaynode eq $local_node;
145 @controller_config = ();
146 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
147 push @controller_config, "import vrf $vrf";
148 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
149 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
151 @controller_config = ();
152 #redistribute connected to be able to route to local vms on the gateway
153 push @controller_config, "redistribute connected";
154 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv4 unicast"}}, @controller_config);
155 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv6 unicast"}}, @controller_config);
157 @controller_config = ();
158 #add default originate to announce 0.0.0.0/0 type5 route in evpn
159 push @controller_config, "default-originate ipv4";
160 push @controller_config, "default-originate ipv6";
161 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @controller_config);
168 my ($class, $controllerid, $zone_cfg) = @_;
170 # verify that zone is associated to this controller
171 foreach my $id (keys %{$zone_cfg->{ids
}}) {
172 my $zone = $zone_cfg->{ids
}->{$id};
173 die "controller $controllerid is used by $id"
174 if (defined($zone->{controller
}) && $zone->{controller
} eq $controllerid);
179 my ($class, $controllerid, $controller_cfg) = @_;
181 # verify that asn is not already used by another controller
182 my $asn = $controller_cfg->{ids
}->{$controllerid}->{asn
};
183 foreach my $id (keys %{$controller_cfg->{ids
}}) {
184 next if $id eq $controllerid;
185 my $controller = $controller_cfg->{ids
}->{$id};
186 die "asn $asn is already used by $id"
187 if (defined($controller->{asn
}) && $controller->{asn
} eq $asn);
191 sub sort_frr_config
{
195 $order->{'ipv4 unicast'} = 1;
196 $order->{'ipv6 unicast'} = 2;
197 $order->{'l2vpn evpn'} = 3;
202 $a_val = $order->{$a} if defined($order->{$a});
203 $b_val = $order->{$b} if defined($order->{$b});
205 if($a =~ /bgp (\d+)$/) {
209 if($b =~ /bgp (\d+)$/) {
213 return $a_val <=> $b_val;
216 sub generate_frr_recurse
{
217 my ($final_config, $content, $parentkey, $level) = @_;
221 $keylist->{'address-family'} = 1;
222 $keylist->{router
} = 1;
224 my $exitkeylist = {};
225 $exitkeylist->{vrf
} = 1;
226 $exitkeylist->{'address-family'} = 1;
228 #fix me, make this generic
229 my $paddinglevel = undef;
230 if($level == 1 || $level == 2) {
231 $paddinglevel = $level - 1;
232 } elsif ($level == 3 || $level == 4) {
233 $paddinglevel = $level - 2;
237 $padding = ' ' x
($paddinglevel) if $paddinglevel;
239 if (ref $content eq ref {}) {
240 foreach my $key (sort sort_frr_config
keys %$content) {
241 if ($parentkey && defined($keylist->{$parentkey})) {
242 push @{$final_config}, $padding."!";
243 push @{$final_config}, $padding."$parentkey $key";
245 push @{$final_config}, $padding."$key" if $key ne '' && !defined($keylist->{$key});
248 my $option = $content->{$key};
249 generate_frr_recurse
($final_config, $option, $key, $level+1);
251 push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
255 if (ref $content eq 'ARRAY') {
256 foreach my $value (@$content) {
257 push @{$final_config}, $padding."$value";
262 sub write_controller_config
{
263 my ($class, $plugin_config, $config) = @_;
265 my $final_config = [];
266 push @{$final_config}, "log syslog informational";
267 push @{$final_config}, "!";
269 generate_frr_recurse
($final_config, $config->{frr
}, undef, 0);
271 push @{$final_config}, "!";
272 push @{$final_config}, "line vty";
273 push @{$final_config}, "!";
275 my $rawconfig = join("\n", @{$final_config});
278 return if !$rawconfig;
279 return if !-d
"/etc/frr";
281 my $frr_config_file = "/etc/frr/frr.conf";
283 my $writefh = IO
::File-
>new($frr_config_file,">");
284 print $writefh $rawconfig;
288 sub reload_controller
{
291 my $conf_file = "/etc/frr/frr.conf";
292 my $bin_path = "/usr/bin/vtysh";
296 if ($line =~ /^line (\S+)/) {
301 if (-e
$conf_file && -e
$bin_path) {
302 PVE
::Tools
::run_command
([$bin_path, '-m', '-f', $conf_file], outfunc
=> {}, errfunc
=> $err);