1 package PVE
::Network
::SDN
::Controllers
::BgpPlugin
;
7 use PVE
::JSONSchema
qw(get_standard_option);
8 use PVE
::Tools
qw(run_command file_set_contents file_get_contents);
10 use PVE
::Network
::SDN
::Controllers
::Plugin
;
11 use PVE
::Network
::SDN
::Zones
::Plugin
;
14 use base
('PVE::Network::SDN::Controllers::Plugin');
22 'bgp-multipath-as-path-relax' => {
29 description
=> "Enable ebgp. (remote-as external)",
36 description
=> "source loopback interface.",
39 node
=> get_standard_option
('pve-node'),
45 'node' => { optional
=> 0 },
46 'asn' => { optional
=> 0 },
47 'peers' => { optional
=> 0 },
48 'bgp-multipath-as-path-relax' => { optional
=> 1 },
49 'ebgp' => { optional
=> 1 },
50 'ebgp-multihop' => { optional
=> 1 },
51 'loopback' => { optional
=> 1 },
55 # Plugin implementation
56 sub generate_controller_config
{
57 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
60 @peers = PVE
::Tools
::split_list
($plugin_config->{'peers'}) if $plugin_config->{'peers'};
62 my $asn = $plugin_config->{asn
};
63 my $ebgp = $plugin_config->{ebgp
};
64 my $ebgp_multihop = $plugin_config->{'ebgp-multihop'};
65 my $loopback = $plugin_config->{loopback
};
66 my $multipath_relax = $plugin_config->{'bgp-multipath-as-path-relax'};
68 my $local_node = PVE
::INotify
::nodename
();
72 return if $local_node ne $plugin_config->{node
};
74 my $bgp = $config->{frr
}->{router
}->{"bgp $asn"} //= {};
76 my ($ifaceip, $interface) = PVE
::Network
::SDN
::Zones
::Plugin
::find_local_ip_interface_peers
(\
@peers, $loopback);
78 my $remoteas = $ebgp ?
"external" : $asn;
81 my @controller_config = (
82 "bgp router-id $ifaceip",
83 "no bgp default ipv4-unicast",
85 "bgp network import-check"
88 push(@{$bgp->{""}}, @controller_config) if keys %{$bgp} == 0;
90 @controller_config = ();
92 push @controller_config, "no bgp ebgp-requires-policy";
93 push @controller_config, "bgp disable-ebgp-connected-route-check" if $loopback;
96 push @controller_config, "bgp bestpath as-path multipath-relax" if $multipath_relax;
100 push @controller_config, "neighbor BGP peer-group";
101 push @controller_config, "neighbor BGP remote-as $remoteas";
102 push @controller_config, "neighbor BGP bfd";
103 push @controller_config, "neighbor BGP ebgp-multihop $ebgp_multihop" if $ebgp && $ebgp_multihop;
107 foreach my $address (@peers) {
108 push @controller_config, "neighbor $address peer-group BGP";
110 push(@{$bgp->{""}}, @controller_config);
112 # address-family unicast
114 my $ipversion = Net
::IP
::ip_is_ipv6
($ifaceip) ?
"ipv6" : "ipv4";
115 my $mask = Net
::IP
::ip_is_ipv6
($ifaceip) ?
"/128" : "32";
117 push(@{$bgp->{"address-family"}->{"$ipversion unicast"}}, "network $ifaceip/$mask") if $loopback;
118 push(@{$bgp->{"address-family"}->{"$ipversion unicast"}}, "neighbor BGP activate");
119 push(@{$bgp->{"address-family"}->{"$ipversion unicast"}}, "neighbor BGP soft-reconfiguration inbound");
123 push(@{$config->{frr
}->{''}}, "ip prefix-list loopbacks_ips seq 10 permit 0.0.0.0/0 le 32");
124 push(@{$config->{frr
}->{''}}, "ip protocol bgp route-map correct_src");
126 my $routemap_config = [];
127 push @{$routemap_config}, "match ip address prefix-list loopbacks_ips";
128 push @{$routemap_config}, "set src $ifaceip";
129 push(@{$config->{frr_routemap
}->{'correct_src'}}, $routemap_config);
135 sub generate_controller_zone_config
{
136 my ($class, $plugin_config, $controller, $controller_cfg, $id, $uplinks, $config) = @_;
141 my ($class, $controllerid, $zone_cfg) = @_;
143 # verify that zone is associated to this controller
144 foreach my $id (keys %{$zone_cfg->{ids
}}) {
145 my $zone = $zone_cfg->{ids
}->{$id};
146 die "controller $controllerid is used by $id"
147 if (defined($zone->{controller
}) && $zone->{controller
} eq $controllerid);
152 my ($class, $controllerid, $controller_cfg) = @_;
154 # we can only have 1 bgp controller by node
155 my $local_node = PVE
::INotify
::nodename
();
156 my $controllernb = 0;
157 foreach my $id (keys %{$controller_cfg->{ids
}}) {
158 next if $id eq $controllerid;
159 my $controller = $controller_cfg->{ids
}->{$id};
160 next if $controller->{type
} ne "bgp";
161 next if $controller->{node
} ne $local_node;
163 die "only 1 bgp controller can be defined" if $controllernb > 1;
167 sub generate_controller_rawconfig
{
168 my ($class, $plugin_config, $config) = @_;
172 sub write_controller_config
{
173 my ($class, $plugin_config, $config) = @_;
177 sub reload_controller
{