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