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