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, $router, $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 @router_config = ();
80 push @router_config, "bgp router-id $ifaceip";
81 push @router_config, "no bgp default ipv4-unicast";
82 push @router_config, "coalesce-time 1000";
84 foreach my $address (@peers) {
85 next if $address eq $ifaceip;
86 push @router_config, "neighbor $address remote-as $asn";
90 foreach my $address (@gatewaypeers) {
91 push @router_config, "neighbor $address remote-as external";
94 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{""}}, @router_config);
97 foreach my $address (@peers) {
98 next if $address eq $ifaceip;
99 push @router_config, "neighbor $address activate";
101 push @router_config, "advertise-all-vni";
102 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"l2vpn evpn"}}, @router_config);
107 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
108 #frr 7.1 tag is bugged -> works fine with 7.1 stable branch(20190829-02-g6ba76bbc1)
109 #https://github.com/FRRouting/frr/issues/4905
110 foreach my $address (@gatewaypeers) {
111 push @router_config, "neighbor $address activate";
113 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @router_config);
114 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @router_config);
121 sub generate_controller_transport_config
{
122 my ($class, $plugin_config, $router, $id, $uplinks, $config) = @_;
124 my $vrf = $plugin_config->{'vrf'};
125 my $vrfvxlan = $plugin_config->{'vrf-vxlan'};
126 my $asn = $router->{asn
};
127 my $gatewaynodes = $router->{'gateway-nodes'};
129 return if !$vrf || !$vrfvxlan || !$asn;
132 my @router_config = ();
133 push @router_config, "vni $vrfvxlan";
134 push(@{$config->{frr
}->{vrf
}->{"$vrf"}}, @router_config);
138 my $is_gateway = undef;
139 my $local_node = PVE
::INotify
::nodename
();
141 foreach my $gatewaynode (PVE
::Tools
::split_list
($gatewaynodes)) {
142 $is_gateway = 1 if $gatewaynode eq $local_node;
148 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
149 #frr 7.1 tag is bugged -> works fine with 7.1 stable branch(20190829-02-g6ba76bbc1)
150 #https://github.com/FRRouting/frr/issues/4905
151 push @router_config, "import vrf $vrf";
152 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @router_config);
153 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @router_config);
156 #redistribute connected to be able to route to local vms on the gateway
157 push @router_config, "redistribute connected";
158 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv4 unicast"}}, @router_config);
159 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv6 unicast"}}, @router_config);
162 #add default originate to announce 0.0.0.0/0 type5 route in evpn
163 push @router_config, "default-originate ipv4";
164 push @router_config, "default-originate ipv6";
165 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @router_config);
172 my ($class, $routerid, $sdn_cfg) = @_;
174 # verify that transport is associated to this router
175 foreach my $id (keys %{$sdn_cfg->{ids
}}) {
176 my $sdn = $sdn_cfg->{ids
}->{$id};
177 die "router $routerid is used by $id"
178 if (defined($sdn->{router
}) && $sdn->{router
} eq $routerid);
183 my ($class, $routerid, $sdn_cfg) = @_;
185 # verify that asn is not already used by another router
186 my $asn = $sdn_cfg->{ids
}->{$routerid}->{asn
};
187 foreach my $id (keys %{$sdn_cfg->{ids
}}) {
188 next if $id eq $routerid;
189 my $sdn = $sdn_cfg->{ids
}->{$id};
190 die "asn $asn is already used by $id"
191 if (defined($sdn->{asn
}) && $sdn->{asn
} eq $asn);
195 sub sort_frr_config
{
199 $order->{'ipv4 unicast'} = 1;
200 $order->{'ipv6 unicast'} = 2;
201 $order->{'l2vpn evpn'} = 3;
206 $a_val = $order->{$a} if defined($order->{$a});
207 $b_val = $order->{$b} if defined($order->{$b});
209 if($a =~ /bgp (\d+)$/) {
213 if($b =~ /bgp (\d+)$/) {
217 return $a_val <=> $b_val;
220 sub generate_frr_recurse
{
221 my ($final_config, $content, $parentkey, $level) = @_;
225 $keylist->{'address-family'} = 1;
226 $keylist->{router
} = 1;
228 my $exitkeylist = {};
229 $exitkeylist->{vrf
} = 1;
230 $exitkeylist->{'address-family'} = 1;
232 #fix me, make this generic
233 my $paddinglevel = undef;
234 if($level == 1 || $level == 2) {
235 $paddinglevel = $level - 1;
236 } elsif ($level == 3 || $level == 4) {
237 $paddinglevel = $level - 2;
241 $padding = ' ' x
($paddinglevel) if $paddinglevel;
243 if (ref $content eq ref {}) {
244 foreach my $key (sort sort_frr_config
keys %$content) {
245 if ($parentkey && defined($keylist->{$parentkey})) {
246 push @{$final_config}, $padding."!";
247 push @{$final_config}, $padding."$parentkey $key";
249 push @{$final_config}, $padding."$key" if $key ne '' && !defined($keylist->{$key});
252 my $option = $content->{$key};
253 generate_frr_recurse
($final_config, $option, $key, $level+1);
255 push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
259 if (ref $content eq 'ARRAY') {
260 foreach my $value (@$content) {
261 push @{$final_config}, $padding."$value";
266 sub write_controller_config
{
267 my ($class, $plugin_config, $config) = @_;
269 my $final_config = [];
270 push @{$final_config}, "log syslog informational";
271 push @{$final_config}, "!";
273 generate_frr_recurse
($final_config, $config->{frr
}, undef, 0);
275 push @{$final_config}, "!";
276 push @{$final_config}, "line vty";
277 push @{$final_config}, "!";
279 my $rawconfig = join("\n", @{$final_config});
282 return if !$rawconfig;
283 return if !-d
"/etc/frr";
285 my $frr_config_file = "/etc/frr/frr.conf";
287 my $writefh = IO
::File-
>new($frr_config_file,">");
288 print $writefh $rawconfig;
292 sub reload_controller
{
295 my $conf_file = "/etc/frr/frr.conf";
296 my $bin_path = "/usr/bin/vtysh";
300 if ($line =~ /^line (\S+)/) {
305 if (-e
$conf_file && -e
$bin_path) {
306 PVE
::Tools
::run_command
([$bin_path, '-m', '-f', $conf_file], outfunc
=> {}, errfunc
=> $err);