]>
Commit | Line | Data |
---|---|---|
32602a38 AD |
1 | package PVE::Network::SDN::FrrPlugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use PVE::Network::SDN::Plugin; | |
6 | use PVE::Tools; | |
074d270b AD |
7 | use PVE::INotify; |
8 | use PVE::JSONSchema qw(get_standard_option); | |
32602a38 AD |
9 | |
10 | use base('PVE::Network::SDN::Plugin'); | |
11 | ||
12 | sub type { | |
13 | return 'frr'; | |
14 | } | |
15 | ||
16 | sub properties { | |
17 | return { | |
18 | 'asn' => { | |
19 | type => 'integer', | |
20 | description => "autonomous system number", | |
21 | }, | |
22 | 'peers' => { | |
23 | description => "peers address list.", | |
fcfca9ef | 24 | type => 'string', format => 'ip-list' |
32602a38 | 25 | }, |
074d270b AD |
26 | 'gateway-nodes' => get_standard_option('pve-node-list'), |
27 | 'gateway-external-peers' => { | |
28 | description => "upstream bgp peers address list.", | |
fcfca9ef | 29 | type => 'string', format => 'ip-list' |
074d270b | 30 | }, |
32602a38 AD |
31 | }; |
32 | } | |
33 | ||
34 | sub options { | |
35 | ||
36 | return { | |
37 | 'uplink-id' => { optional => 0 }, | |
38 | 'asn' => { optional => 0 }, | |
39 | 'peers' => { optional => 0 }, | |
074d270b AD |
40 | 'gateway-nodes' => { optional => 1 }, |
41 | 'gateway-external-peers' => { optional => 1 }, | |
32602a38 AD |
42 | }; |
43 | } | |
44 | ||
45 | # Plugin implementation | |
87d8b623 | 46 | sub generate_frr_config { |
074d270b | 47 | my ($class, $plugin_config, $router, $id, $uplinks, $config) = @_; |
32602a38 | 48 | |
32602a38 AD |
49 | my @peers = split(',', $plugin_config->{'peers'}) if $plugin_config->{'peers'}; |
50 | ||
074d270b | 51 | my $asn = $plugin_config->{asn}; |
32602a38 | 52 | my $uplink = $plugin_config->{'uplink-id'}; |
074d270b AD |
53 | my $gatewaynodes = $plugin_config->{'gateway-nodes'}; |
54 | my @gatewaypeers = split(',', $plugin_config->{'gateway-external-peers'}) if $plugin_config->{'gateway-external-peers'}; | |
55 | ||
56 | return if !$asn; | |
32602a38 | 57 | |
32602a38 AD |
58 | my $iface = "uplink$uplink"; |
59 | my $ifaceip = ""; | |
60 | ||
61 | if($uplinks->{$uplink}->{name}) { | |
62 | $iface = $uplinks->{$uplink}->{name}; | |
87d8b623 | 63 | $ifaceip = PVE::Network::SDN::Plugin::get_first_local_ipv4_from_interface($iface); |
32602a38 AD |
64 | } |
65 | ||
074d270b AD |
66 | my $is_gateway = undef; |
67 | my $local_node = PVE::INotify::nodename(); | |
68 | ||
69 | foreach my $gatewaynode (PVE::Tools::split_list($gatewaynodes)) { | |
70 | $is_gateway = 1 if $gatewaynode eq $local_node; | |
71 | } | |
17854295 | 72 | |
93dea3aa AD |
73 | my @router_config = (); |
74 | ||
93dea3aa | 75 | push @router_config, "bgp router-id $ifaceip"; |
bc49b410 | 76 | push @router_config, "no bgp default ipv4-unicast"; |
93dea3aa | 77 | push @router_config, "coalesce-time 1000"; |
32602a38 AD |
78 | |
79 | foreach my $address (@peers) { | |
80 | next if $address eq $ifaceip; | |
93dea3aa | 81 | push @router_config, "neighbor $address remote-as $asn"; |
7d35eaf5 | 82 | } |
074d270b AD |
83 | |
84 | if ($is_gateway) { | |
85 | foreach my $address (@gatewaypeers) { | |
86 | push @router_config, "neighbor $address remote-as external"; | |
87 | } | |
88 | } | |
17854295 | 89 | push(@{$config->{router}->{"bgp $asn"}->{""}}, @router_config); |
074d270b | 90 | |
17854295 | 91 | @router_config = (); |
32602a38 AD |
92 | foreach my $address (@peers) { |
93 | next if $address eq $ifaceip; | |
17854295 | 94 | push @router_config, "neighbor $address activate"; |
32602a38 | 95 | } |
17854295 AD |
96 | push @router_config, "advertise-all-vni"; |
97 | push(@{$config->{router}->{"bgp $asn"}->{"address-family"}->{"l2vpn evpn"}}, @router_config); | |
32602a38 | 98 | |
074d270b AD |
99 | if ($is_gateway) { |
100 | ||
101 | @router_config = (); | |
102 | #import /32 routes of evpn network from vrf1 to default vrf (for packet return) | |
103 | #frr 7.1 tag is bugged -> works fine with 7.1 stable branch(20190829-02-g6ba76bbc1) | |
104 | #https://github.com/FRRouting/frr/issues/4905 | |
105 | foreach my $address (@gatewaypeers) { | |
106 | push @router_config, "neighbor $address activate"; | |
107 | } | |
108 | push(@{$config->{router}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @router_config); | |
109 | push(@{$config->{router}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @router_config); | |
110 | ||
111 | } | |
112 | ||
32602a38 AD |
113 | return $config; |
114 | } | |
115 | ||
116 | sub on_delete_hook { | |
5bda8607 | 117 | my ($class, $routerid, $sdn_cfg) = @_; |
32602a38 | 118 | |
5bda8607 AD |
119 | # verify that transport is associated to this router |
120 | foreach my $id (keys %{$sdn_cfg->{ids}}) { | |
121 | my $sdn = $sdn_cfg->{ids}->{$id}; | |
122 | die "router $routerid is used by $id" | |
123 | if (defined($sdn->{router}) && $sdn->{router} eq $routerid); | |
124 | } | |
32602a38 AD |
125 | } |
126 | ||
127 | sub on_update_hook { | |
5bda8607 AD |
128 | my ($class, $routerid, $sdn_cfg) = @_; |
129 | ||
130 | # verify that asn is not already used by another router | |
131 | my $asn = $sdn_cfg->{ids}->{$routerid}->{asn}; | |
132 | foreach my $id (keys %{$sdn_cfg->{ids}}) { | |
133 | next if $id eq $routerid; | |
134 | my $sdn = $sdn_cfg->{ids}->{$id}; | |
135 | die "asn $asn is already used by $id" | |
136 | if (defined($sdn->{asn}) && $sdn->{asn} eq $asn); | |
137 | } | |
32602a38 AD |
138 | } |
139 | ||
140 | 1; | |
141 | ||
142 |