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