]>
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.", | |
7d35eaf5 | 24 | type => 'string', #fixme: format |
32602a38 | 25 | }, |
074d270b AD |
26 | 'gateway-nodes' => get_standard_option('pve-node-list'), |
27 | 'gateway-external-peers' => { | |
28 | description => "upstream bgp peers address list.", | |
29 | type => 'string', #fixme: format | |
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 AD |
76 | push @router_config, "no bgp default ipv4-unicast"; |
77 | push @router_config, "no bgp default ipv6-unicast"; | |
93dea3aa | 78 | push @router_config, "coalesce-time 1000"; |
32602a38 AD |
79 | |
80 | foreach my $address (@peers) { | |
81 | next if $address eq $ifaceip; | |
93dea3aa | 82 | push @router_config, "neighbor $address remote-as $asn"; |
7d35eaf5 | 83 | } |
074d270b AD |
84 | |
85 | if ($is_gateway) { | |
86 | foreach my $address (@gatewaypeers) { | |
87 | push @router_config, "neighbor $address remote-as external"; | |
88 | } | |
89 | } | |
17854295 | 90 | push(@{$config->{router}->{"bgp $asn"}->{""}}, @router_config); |
074d270b | 91 | |
17854295 | 92 | @router_config = (); |
32602a38 AD |
93 | foreach my $address (@peers) { |
94 | next if $address eq $ifaceip; | |
17854295 | 95 | push @router_config, "neighbor $address activate"; |
32602a38 | 96 | } |
17854295 AD |
97 | push @router_config, "advertise-all-vni"; |
98 | push(@{$config->{router}->{"bgp $asn"}->{"address-family"}->{"l2vpn evpn"}}, @router_config); | |
32602a38 | 99 | |
074d270b AD |
100 | if ($is_gateway) { |
101 | ||
102 | @router_config = (); | |
103 | #import /32 routes of evpn network from vrf1 to default vrf (for packet return) | |
104 | #frr 7.1 tag is bugged -> works fine with 7.1 stable branch(20190829-02-g6ba76bbc1) | |
105 | #https://github.com/FRRouting/frr/issues/4905 | |
106 | foreach my $address (@gatewaypeers) { | |
107 | push @router_config, "neighbor $address activate"; | |
108 | } | |
109 | push(@{$config->{router}->{"bgp $asn"}->{"address-family"}->{"ipv4 unicast"}}, @router_config); | |
110 | push(@{$config->{router}->{"bgp $asn"}->{"address-family"}->{"ipv6 unicast"}}, @router_config); | |
111 | ||
112 | } | |
113 | ||
32602a38 AD |
114 | return $config; |
115 | } | |
116 | ||
117 | sub on_delete_hook { | |
5bda8607 | 118 | my ($class, $routerid, $sdn_cfg) = @_; |
32602a38 | 119 | |
5bda8607 AD |
120 | # verify that transport is associated to this router |
121 | foreach my $id (keys %{$sdn_cfg->{ids}}) { | |
122 | my $sdn = $sdn_cfg->{ids}->{$id}; | |
123 | die "router $routerid is used by $id" | |
124 | if (defined($sdn->{router}) && $sdn->{router} eq $routerid); | |
125 | } | |
32602a38 AD |
126 | } |
127 | ||
128 | sub on_update_hook { | |
5bda8607 AD |
129 | my ($class, $routerid, $sdn_cfg) = @_; |
130 | ||
131 | # verify that asn is not already used by another router | |
132 | my $asn = $sdn_cfg->{ids}->{$routerid}->{asn}; | |
133 | foreach my $id (keys %{$sdn_cfg->{ids}}) { | |
134 | next if $id eq $routerid; | |
135 | my $sdn = $sdn_cfg->{ids}->{$id}; | |
136 | die "asn $asn is already used by $id" | |
137 | if (defined($sdn->{asn}) && $sdn->{asn} eq $asn); | |
138 | } | |
32602a38 AD |
139 | } |
140 | ||
141 | 1; | |
142 | ||
143 |