1 package PVE
::Network
::SDN
::FrrPlugin
;
5 use PVE
::Network
::SDN
::Plugin
;
8 use PVE
::JSONSchema
qw(get_standard_option);
10 use base
('PVE::Network::SDN::Plugin');
26 description
=> "autonomous system number",
29 description
=> "peers address list.",
30 type
=> 'string', format
=> 'ip-list'
32 'gateway-nodes' => get_standard_option
('pve-node-list'),
33 'gateway-external-peers' => {
34 description
=> "upstream bgp peers address list.",
35 type
=> 'string', format
=> 'ip-list'
43 'uplink-id' => { optional
=> 0 },
44 'asn' => { optional
=> 0 },
45 'peers' => { optional
=> 0 },
46 'gateway-nodes' => { optional
=> 1 },
47 'gateway-external-peers' => { optional
=> 1 },
51 # Plugin implementation
52 sub generate_controller_config
{
53 my ($class, $plugin_config, $router, $id, $uplinks, $config) = @_;
55 my @peers = split(',', $plugin_config->{'peers'}) if $plugin_config->{'peers'};
57 my $asn = $plugin_config->{asn
};
58 my $uplink = $plugin_config->{'uplink-id'};
59 my $gatewaynodes = $plugin_config->{'gateway-nodes'};
60 my @gatewaypeers = split(',', $plugin_config->{'gateway-external-peers'}) if $plugin_config->{'gateway-external-peers'};
64 my $iface = "uplink$uplink";
67 if($uplinks->{$uplink}->{name
}) {
68 $iface = $uplinks->{$uplink}->{name
};
69 $ifaceip = PVE
::Network
::SDN
::Plugin
::get_first_local_ipv4_from_interface
($iface);
72 my $is_gateway = undef;
73 my $local_node = PVE
::INotify
::nodename
();
75 foreach my $gatewaynode (PVE
::Tools
::split_list
($gatewaynodes)) {
76 $is_gateway = 1 if $gatewaynode eq $local_node;
79 my @router_config = ();
81 push @router_config, "bgp router-id $ifaceip";
82 push @router_config, "no bgp default ipv4-unicast";
83 push @router_config, "coalesce-time 1000";
85 foreach my $address (@peers) {
86 next if $address eq $ifaceip;
87 push @router_config, "neighbor $address remote-as $asn";
91 foreach my $address (@gatewaypeers) {
92 push @router_config, "neighbor $address remote-as external";
95 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{""}}, @router_config);
98 foreach my $address (@peers) {
99 next if $address eq $ifaceip;
100 push @router_config, "neighbor $address activate";
102 push @router_config, "advertise-all-vni";
103 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"l2vpn evpn"}}, @router_config);
108 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
109 #frr 7.1 tag is bugged -> works fine with 7.1 stable branch(20190829-02-g6ba76bbc1)
110 #https://github.com/FRRouting/frr/issues/4905
111 foreach my $address (@gatewaypeers) {
112 push @router_config, "neighbor $address activate";
114 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @router_config);
115 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @router_config);
122 sub generate_controller_transport_config
{
123 my ($class, $plugin_config, $router, $id, $uplinks, $config) = @_;
125 my $vrf = $plugin_config->{'vrf'};
126 my $vrfvxlan = $plugin_config->{'vrf-vxlan'};
127 my $asn = $router->{asn
};
128 my $gatewaynodes = $router->{'gateway-nodes'};
130 return if !$vrf || !$vrfvxlan || !$asn;
133 my @router_config = ();
134 push @router_config, "vni $vrfvxlan";
135 push(@{$config->{frr
}->{vrf
}->{"$vrf"}}, @router_config);
139 my $is_gateway = undef;
140 my $local_node = PVE
::INotify
::nodename
();
142 foreach my $gatewaynode (PVE
::Tools
::split_list
($gatewaynodes)) {
143 $is_gateway = 1 if $gatewaynode eq $local_node;
149 #import /32 routes of evpn network from vrf1 to default vrf (for packet return)
150 #frr 7.1 tag is bugged -> works fine with 7.1 stable branch(20190829-02-g6ba76bbc1)
151 #https://github.com/FRRouting/frr/issues/4905
152 push @router_config, "import vrf $vrf";
153 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @router_config);
154 push(@{$config->{frr
}->{router
}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @router_config);
157 #redistribute connected to be able to route to local vms on the gateway
158 push @router_config, "redistribute connected";
159 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv4 unicast"}}, @router_config);
160 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"ipv6 unicast"}}, @router_config);
163 #add default originate to announce 0.0.0.0/0 type5 route in evpn
164 push @router_config, "default-originate ipv4";
165 push @router_config, "default-originate ipv6";
166 push(@{$config->{frr
}->{router
}->{"bgp $asn vrf $vrf"}->{"address-family"}->{"l2vpn evpn"}}, @router_config);
173 my ($class, $routerid, $sdn_cfg) = @_;
175 # verify that transport is associated to this router
176 foreach my $id (keys %{$sdn_cfg->{ids
}}) {
177 my $sdn = $sdn_cfg->{ids
}->{$id};
178 die "router $routerid is used by $id"
179 if (defined($sdn->{router
}) && $sdn->{router
} eq $routerid);
184 my ($class, $routerid, $sdn_cfg) = @_;
186 # verify that asn is not already used by another router
187 my $asn = $sdn_cfg->{ids
}->{$routerid}->{asn
};
188 foreach my $id (keys %{$sdn_cfg->{ids
}}) {
189 next if $id eq $routerid;
190 my $sdn = $sdn_cfg->{ids
}->{$id};
191 die "asn $asn is already used by $id"
192 if (defined($sdn->{asn
}) && $sdn->{asn
} eq $asn);
196 sub sort_frr_config
{
200 $order->{'ipv4 unicast'} = 1;
201 $order->{'ipv6 unicast'} = 2;
202 $order->{'l2vpn evpn'} = 3;
207 $a_val = $order->{$a} if defined($order->{$a});
208 $b_val = $order->{$b} if defined($order->{$b});
210 if($a =~ /bgp (\d+)$/) {
214 if($b =~ /bgp (\d+)$/) {
218 return $a_val <=> $b_val;
221 sub generate_frr_recurse
{
222 my ($final_config, $content, $parentkey, $level) = @_;
226 $keylist->{'address-family'} = 1;
227 $keylist->{router
} = 1;
229 my $exitkeylist = {};
230 $exitkeylist->{vrf
} = 1;
231 $exitkeylist->{'address-family'} = 1;
233 #fix me, make this generic
234 my $paddinglevel = undef;
235 if($level == 1 || $level == 2) {
236 $paddinglevel = $level - 1;
237 } elsif ($level == 3 || $level == 4) {
238 $paddinglevel = $level - 2;
242 $padding = ' ' x
($paddinglevel) if $paddinglevel;
244 if (ref $content eq ref {}) {
245 foreach my $key (sort sort_frr_config
keys %$content) {
246 if ($parentkey && defined($keylist->{$parentkey})) {
247 push @{$final_config}, $padding."!";
248 push @{$final_config}, $padding."$parentkey $key";
250 push @{$final_config}, $padding."$key" if $key ne '' && !defined($keylist->{$key});
253 my $option = $content->{$key};
254 generate_frr_recurse
($final_config, $option, $key, $level+1);
256 push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
260 if (ref $content eq 'ARRAY') {
261 foreach my $value (@$content) {
262 push @{$final_config}, $padding."$value";
267 sub write_controller_config
{
268 my ($class, $plugin_config, $config) = @_;
270 my $final_config = [];
271 push @{$final_config}, "log syslog informational";
272 push @{$final_config}, "!";
274 generate_frr_recurse
($final_config, $config->{frr
}, undef, 0);
276 push @{$final_config}, "!";
277 push @{$final_config}, "line vty";
278 push @{$final_config}, "!";
280 my $rawconfig = join("\n", @{$final_config});
283 return if !$rawconfig;
284 return if !-d
"/etc/frr";
286 my $frr_config_file = "/etc/frr/frr.conf";
288 my $writefh = IO
::File-
>new($frr_config_file,">");
289 print $writefh $rawconfig;