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