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);
123 my ($class, $routerid, $sdn_cfg) = @_;
125 # verify that transport is associated to this router
126 foreach my $id (keys %{$sdn_cfg->{ids
}}) {
127 my $sdn = $sdn_cfg->{ids
}->{$id};
128 die "router $routerid is used by $id"
129 if (defined($sdn->{router
}) && $sdn->{router
} eq $routerid);
134 my ($class, $routerid, $sdn_cfg) = @_;
136 # verify that asn is not already used by another router
137 my $asn = $sdn_cfg->{ids
}->{$routerid}->{asn
};
138 foreach my $id (keys %{$sdn_cfg->{ids
}}) {
139 next if $id eq $routerid;
140 my $sdn = $sdn_cfg->{ids
}->{$id};
141 die "asn $asn is already used by $id"
142 if (defined($sdn->{asn
}) && $sdn->{asn
} eq $asn);
146 sub sort_frr_config
{
150 $order->{'ipv4 unicast'} = 1;
151 $order->{'ipv6 unicast'} = 2;
152 $order->{'l2vpn evpn'} = 3;
157 $a_val = $order->{$a} if defined($order->{$a});
158 $b_val = $order->{$b} if defined($order->{$b});
160 if($a =~ /bgp (\d+)$/) {
164 if($b =~ /bgp (\d+)$/) {
168 return $a_val <=> $b_val;
171 sub generate_frr_recurse
{
172 my ($final_config, $content, $parentkey, $level) = @_;
176 $keylist->{'address-family'} = 1;
177 $keylist->{router
} = 1;
179 my $exitkeylist = {};
180 $exitkeylist->{vrf
} = 1;
181 $exitkeylist->{'address-family'} = 1;
183 #fix me, make this generic
184 my $paddinglevel = undef;
185 if($level == 1 || $level == 2) {
186 $paddinglevel = $level - 1;
187 } elsif ($level == 3 || $level == 4) {
188 $paddinglevel = $level - 2;
192 $padding = ' ' x
($paddinglevel) if $paddinglevel;
194 if (ref $content eq ref {}) {
195 foreach my $key (sort sort_frr_config
keys %$content) {
196 if ($parentkey && defined($keylist->{$parentkey})) {
197 push @{$final_config}, $padding."!";
198 push @{$final_config}, $padding."$parentkey $key";
200 push @{$final_config}, $padding."$key" if $key ne '' && !defined($keylist->{$key});
203 my $option = $content->{$key};
204 generate_frr_recurse
($final_config, $option, $key, $level+1);
206 push @{$final_config}, $padding."exit-$parentkey" if $parentkey && defined($exitkeylist->{$parentkey});
210 if (ref $content eq 'ARRAY') {
211 foreach my $value (@$content) {
212 push @{$final_config}, $padding."$value";
217 sub write_controller_config
{
218 my ($class, $plugin_config, $config) = @_;
220 my $final_config = [];
221 push @{$final_config}, "log syslog informational";
222 push @{$final_config}, "!";
224 generate_frr_recurse
($final_config, $config->{frr
}, undef, 0);
226 push @{$final_config}, "!";
227 push @{$final_config}, "line vty";
228 push @{$final_config}, "!";
230 my $rawconfig = join("\n", @{$final_config});
233 return if !$rawconfig;
234 return if !-d
"/etc/frr";
236 my $frr_config_file = "/etc/frr/frr.conf";
238 my $writefh = IO
::File-
>new($frr_config_file,">");
239 print $writefh $rawconfig;