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