]>
Commit | Line | Data |
---|---|---|
f5eabba0 | 1 | package PVE::Network::SDN::Zones::EvpnPlugin; |
63586d2f AD |
2 | |
3 | use strict; | |
4 | use warnings; | |
f5eabba0 | 5 | use PVE::Network::SDN::Zones::VxlanPlugin; |
7fdeb441 | 6 | use PVE::Exception qw(raise raise_param_exc); |
f23633dc | 7 | use PVE::JSONSchema qw(get_standard_option); |
63586d2f AD |
8 | use PVE::Tools qw($IPV4RE); |
9 | use PVE::INotify; | |
5ca07ed9 AD |
10 | use PVE::Cluster; |
11 | use PVE::Tools; | |
12 | ||
4405f2de | 13 | use PVE::Network::SDN::Controllers::EvpnPlugin; |
63586d2f | 14 | |
f5eabba0 | 15 | use base('PVE::Network::SDN::Zones::VxlanPlugin'); |
63586d2f AD |
16 | |
17 | sub type { | |
18 | return 'evpn'; | |
19 | } | |
20 | ||
63586d2f AD |
21 | sub properties { |
22 | return { | |
63586d2f AD |
23 | 'vrf-vxlan' => { |
24 | type => 'integer', | |
25 | description => "l3vni.", | |
26 | }, | |
27 | 'controller' => { | |
28 | type => 'string', | |
29 | description => "Frr router name", | |
30 | }, | |
f23633dc | 31 | 'exitnodes' => get_standard_option('pve-node-list'), |
63586d2f AD |
32 | }; |
33 | } | |
34 | ||
35 | sub options { | |
36 | ||
37 | return { | |
c2b9c173 | 38 | nodes => { optional => 1}, |
63586d2f | 39 | 'vrf-vxlan' => { optional => 0 }, |
f23633dc AD |
40 | controller => { optional => 0 }, |
41 | exitnodes => { optional => 1 }, | |
823f2e2a | 42 | mtu => { optional => 1 }, |
4ad78442 AD |
43 | dns => { optional => 1 }, |
44 | reversedns => { optional => 1 }, | |
45 | dnszone => { optional => 1 }, | |
331e2330 | 46 | ipam => { optional => 0 }, |
63586d2f AD |
47 | }; |
48 | } | |
49 | ||
50 | # Plugin implementation | |
51 | sub generate_sdn_config { | |
efffa0ff | 52 | my ($class, $plugin_config, $zoneid, $vnetid, $vnet, $controller, $controller_cfg, $subnet_cfg, $interfaces_config, $config) = @_; |
63586d2f AD |
53 | |
54 | my $tag = $vnet->{tag}; | |
55 | my $alias = $vnet->{alias}; | |
56 | my $ipv4 = $vnet->{ipv4}; | |
57 | my $ipv6 = $vnet->{ipv6}; | |
58 | my $mac = $vnet->{mac}; | |
59 | ||
1de0abc0 | 60 | my $vrf_iface = "vrf_$zoneid"; |
63586d2f | 61 | my $vrfvxlan = $plugin_config->{'vrf-vxlan'}; |
53b2cc90 | 62 | my $local_node = PVE::INotify::nodename(); |
63586d2f AD |
63 | |
64 | die "missing vxlan tag" if !$tag; | |
f23633dc | 65 | die "missing controller" if !$controller; |
912fb443 | 66 | warn "vlan-aware vnet can't be enabled with evpn plugin" if $vnet->{vlanaware}; |
63586d2f | 67 | |
5a60da84 | 68 | my @peers = PVE::Tools::split_list($controller->{'peers'}); |
efffa0ff AD |
69 | my $bgprouter = PVE::Network::SDN::Controllers::EvpnPlugin::find_bgp_controller($local_node, $controller_cfg); |
70 | my $loopback = $bgprouter->{loopback} if $bgprouter->{loopback}; | |
f23633dc | 71 | my ($ifaceip, $iface) = PVE::Network::SDN::Zones::Plugin::find_local_ip_interface_peers(\@peers, $loopback); |
63586d2f AD |
72 | |
73 | my $mtu = 1450; | |
ba7ac021 | 74 | $mtu = $interfaces_config->{$iface}->{mtu} - 50 if $interfaces_config->{$iface}->{mtu}; |
0251ed2f | 75 | $mtu = $plugin_config->{mtu} if $plugin_config->{mtu}; |
63586d2f AD |
76 | |
77 | #vxlan interface | |
1de0abc0 | 78 | my $vxlan_iface = "vxlan_$vnetid"; |
63586d2f AD |
79 | my @iface_config = (); |
80 | push @iface_config, "vxlan-id $tag"; | |
63586d2f AD |
81 | push @iface_config, "vxlan-local-tunnelip $ifaceip" if $ifaceip; |
82 | push @iface_config, "bridge-learning off"; | |
83 | push @iface_config, "bridge-arp-nd-suppress on"; | |
84 | ||
85 | push @iface_config, "mtu $mtu" if $mtu; | |
1de0abc0 | 86 | push(@{$config->{$vxlan_iface}}, @iface_config) if !$config->{$vxlan_iface}; |
63586d2f AD |
87 | |
88 | #vnet bridge | |
89 | @iface_config = (); | |
7024ec2b | 90 | |
e612faf6 | 91 | my $address = {}; |
5d3e0248 | 92 | my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid, 1); |
e612faf6 AD |
93 | foreach my $subnetid (sort keys %{$subnets}) { |
94 | my $subnet = $subnets->{$subnetid}; | |
e8736dac | 95 | my $cidr = $subnet->{cidr}; |
9f4f6c2e | 96 | my $mask = $subnet->{mask}; |
e8736dac | 97 | |
e612faf6 AD |
98 | my $gateway = $subnet->{gateway}; |
99 | if ($gateway) { | |
9f4f6c2e | 100 | push @iface_config, "address $gateway/$mask" if !defined($address->{$gateway}); |
e612faf6 AD |
101 | $address->{$gateway} = 1; |
102 | } | |
b634e577 | 103 | |
53b2cc90 | 104 | if ($subnet->{snat}) { |
b634e577 AD |
105 | |
106 | my $is_evpn_gateway = $plugin_config->{'exitnodes'}->{$local_node}; | |
107 | ||
53b2cc90 AD |
108 | #find outgoing interface |
109 | my ($outip, $outiface) = PVE::Network::SDN::Zones::Plugin::get_local_route_ip('8.8.8.8'); | |
110 | if ($outip && $outiface && $is_evpn_gateway) { | |
111 | #use snat, faster than masquerade | |
112 | push @iface_config, "post-up iptables -t nat -A POSTROUTING -s '$cidr' -o $outiface -j SNAT --to-source $outip"; | |
113 | push @iface_config, "post-down iptables -t nat -D POSTROUTING -s '$cidr' -o $outiface -j SNAT --to-source $outip"; | |
114 | #add conntrack zone once on outgoing interface | |
115 | push @iface_config, "post-up iptables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1"; | |
116 | push @iface_config, "post-down iptables -t raw -D PREROUTING -i fwbr+ -j CT --zone 1"; | |
117 | } | |
118 | } | |
7024ec2b AD |
119 | } |
120 | ||
63586d2f | 121 | push @iface_config, "hwaddress $mac" if $mac; |
1de0abc0 | 122 | push @iface_config, "bridge_ports $vxlan_iface"; |
63586d2f AD |
123 | push @iface_config, "bridge_stp off"; |
124 | push @iface_config, "bridge_fd 0"; | |
125 | push @iface_config, "mtu $mtu" if $mtu; | |
126 | push @iface_config, "alias $alias" if $alias; | |
127 | push @iface_config, "ip-forward on" if $ipv4; | |
128 | push @iface_config, "ip6-forward on" if $ipv6; | |
129 | push @iface_config, "arp-accept on" if $ipv4||$ipv6; | |
1de0abc0 | 130 | push @iface_config, "vrf $vrf_iface" if $vrf_iface; |
63586d2f AD |
131 | push(@{$config->{$vnetid}}, @iface_config) if !$config->{$vnetid}; |
132 | ||
1de0abc0 | 133 | if ($vrf_iface) { |
63586d2f AD |
134 | #vrf interface |
135 | @iface_config = (); | |
136 | push @iface_config, "vrf-table auto"; | |
d9ee9544 | 137 | push @iface_config, "post-up ip route add vrf $vrf_iface unreachable default metric 4278198272"; |
1de0abc0 | 138 | push(@{$config->{$vrf_iface}}, @iface_config) if !$config->{$vrf_iface}; |
63586d2f AD |
139 | |
140 | if ($vrfvxlan) { | |
141 | #l3vni vxlan interface | |
1de0abc0 | 142 | my $iface_vrf_vxlan = "vrfvx_$zoneid"; |
63586d2f AD |
143 | @iface_config = (); |
144 | push @iface_config, "vxlan-id $vrfvxlan"; | |
145 | push @iface_config, "vxlan-local-tunnelip $ifaceip" if $ifaceip; | |
146 | push @iface_config, "bridge-learning off"; | |
147 | push @iface_config, "bridge-arp-nd-suppress on"; | |
148 | push @iface_config, "mtu $mtu" if $mtu; | |
1de0abc0 | 149 | push(@{$config->{$iface_vrf_vxlan}}, @iface_config) if !$config->{$iface_vrf_vxlan}; |
63586d2f AD |
150 | |
151 | #l3vni bridge | |
1de0abc0 | 152 | my $brvrf = "vrfbr_$zoneid"; |
63586d2f | 153 | @iface_config = (); |
1de0abc0 | 154 | push @iface_config, "bridge-ports $iface_vrf_vxlan"; |
63586d2f AD |
155 | push @iface_config, "bridge_stp off"; |
156 | push @iface_config, "bridge_fd 0"; | |
157 | push @iface_config, "mtu $mtu" if $mtu; | |
1de0abc0 | 158 | push @iface_config, "vrf $vrf_iface"; |
63586d2f AD |
159 | push(@{$config->{$brvrf}}, @iface_config) if !$config->{$brvrf}; |
160 | } | |
161 | } | |
162 | ||
163 | return $config; | |
164 | } | |
165 | ||
166 | sub on_update_hook { | |
a2b32a94 AD |
167 | my ($class, $zoneid, $zone_cfg, $controller_cfg) = @_; |
168 | ||
169 | # verify that controller exist | |
170 | my $controller = $zone_cfg->{ids}->{$zoneid}->{controller}; | |
171 | if (!defined($controller_cfg->{ids}->{$controller})) { | |
172 | die "controller $controller don't exist"; | |
173 | } else { | |
174 | die "$controller is not a evpn controller type" if $controller_cfg->{ids}->{$controller}->{type} ne 'evpn'; | |
175 | } | |
63586d2f | 176 | |
7cb9714d | 177 | #vrf-vxlan need to be defined |
a2b32a94 AD |
178 | |
179 | my $vrfvxlan = $zone_cfg->{ids}->{$zoneid}->{'vrf-vxlan'}; | |
180 | # verify that vrf-vxlan is not already declared in another zone | |
181 | foreach my $id (keys %{$zone_cfg->{ids}}) { | |
182 | next if $id eq $zoneid; | |
183 | die "vrf-vxlan $vrfvxlan is already declared in $id" | |
184 | if (defined($zone_cfg->{ids}->{$id}->{'vrf-vxlan'}) && $zone_cfg->{ids}->{$id}->{'vrf-vxlan'} eq $vrfvxlan); | |
63586d2f | 185 | } |
5ca07ed9 | 186 | |
63586d2f AD |
187 | } |
188 | ||
1d44ce70 | 189 | |
5ca07ed9 | 190 | sub vnet_update_hook { |
88d9562b | 191 | my ($class, $vnet_cfg, $vnetid, $zone_cfg) = @_; |
5ca07ed9 | 192 | |
88d9562b AD |
193 | my $vnet = $vnet_cfg->{ids}->{$vnetid}; |
194 | my $tag = $vnet->{tag}; | |
195 | ||
196 | raise_param_exc({ tag => "missing vxlan tag"}) if !defined($tag); | |
197 | raise_param_exc({ tag => "vxlan tag max value is 16777216"}) if $tag > 16777216; | |
198 | ||
199 | # verify that tag is not already defined globally (vxlan-id are unique) | |
200 | foreach my $id (keys %{$vnet_cfg->{ids}}) { | |
201 | next if $id eq $vnetid; | |
202 | my $othervnet = $vnet_cfg->{ids}->{$id}; | |
203 | my $other_tag = $othervnet->{tag}; | |
204 | my $other_zoneid = $othervnet->{zone}; | |
205 | my $other_zone = $zone_cfg->{ids}->{$other_zoneid}; | |
206 | next if $other_zone->{type} ne 'vxlan' && $other_zone->{type} ne 'evpn'; | |
207 | raise_param_exc({ tag => "vxlan tag $tag already exist in vnet $id in zone $other_zoneid "}) if $other_tag && $tag eq $other_tag; | |
208 | } | |
5ca07ed9 AD |
209 | |
210 | if (!defined($vnet->{mac})) { | |
211 | my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg'); | |
212 | $vnet->{mac} = PVE::Tools::random_ether_addr($dc->{mac_prefix}); | |
213 | } | |
1d44ce70 AD |
214 | } |
215 | ||
5ca07ed9 | 216 | |
63586d2f AD |
217 | 1; |
218 | ||
219 |