GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / net / ethernet / mellanox / mlxsw / spectrum_ipip.c
1 /*
2  * drivers/net/ethernet/mellanox/mlxsw/spectrum_ipip.c
3  * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2017 Petr Machata <petrm@mellanox.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the names of the copyright holders nor the names of its
15  *    contributors may be used to endorse or promote products derived from
16  *    this software without specific prior written permission.
17  *
18  * Alternatively, this software may be distributed under the terms of the
19  * GNU General Public License ("GPL") version 2 as published by the Free
20  * Software Foundation.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <net/ip_tunnels.h>
36
37 #include "spectrum_ipip.h"
38
39 static bool
40 mlxsw_sp_ipip_netdev_has_ikey(const struct net_device *ol_dev)
41 {
42         struct ip_tunnel *tun = netdev_priv(ol_dev);
43
44         return !!(tun->parms.i_flags & TUNNEL_KEY);
45 }
46
47 static bool
48 mlxsw_sp_ipip_netdev_has_okey(const struct net_device *ol_dev)
49 {
50         struct ip_tunnel *tun = netdev_priv(ol_dev);
51
52         return !!(tun->parms.o_flags & TUNNEL_KEY);
53 }
54
55 static u32 mlxsw_sp_ipip_netdev_ikey(const struct net_device *ol_dev)
56 {
57         struct ip_tunnel *tun = netdev_priv(ol_dev);
58
59         return mlxsw_sp_ipip_netdev_has_ikey(ol_dev) ?
60                 be32_to_cpu(tun->parms.i_key) : 0;
61 }
62
63 static u32 mlxsw_sp_ipip_netdev_okey(const struct net_device *ol_dev)
64 {
65         struct ip_tunnel *tun = netdev_priv(ol_dev);
66
67         return mlxsw_sp_ipip_netdev_has_okey(ol_dev) ?
68                 be32_to_cpu(tun->parms.o_key) : 0;
69 }
70
71 static int
72 mlxsw_sp_ipip_nexthop_update_gre4(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
73                                   struct mlxsw_sp_ipip_entry *ipip_entry)
74 {
75         u16 rif_index = mlxsw_sp_ipip_lb_rif_index(ipip_entry->ol_lb);
76         __be32 daddr4 = mlxsw_sp_ipip_netdev_daddr4(ipip_entry->ol_dev);
77         char ratr_pl[MLXSW_REG_RATR_LEN];
78
79         mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY,
80                             true, MLXSW_REG_RATR_TYPE_IPIP,
81                             adj_index, rif_index);
82         mlxsw_reg_ratr_ipip4_entry_pack(ratr_pl, be32_to_cpu(daddr4));
83
84         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
85 }
86
87 static int
88 mlxsw_sp_ipip_fib_entry_op_gre4_rtdp(struct mlxsw_sp *mlxsw_sp,
89                                      u32 tunnel_index,
90                                      struct mlxsw_sp_ipip_entry *ipip_entry)
91 {
92         bool has_ikey = mlxsw_sp_ipip_netdev_has_ikey(ipip_entry->ol_dev);
93         u16 rif_index = mlxsw_sp_ipip_lb_rif_index(ipip_entry->ol_lb);
94         u32 ikey = mlxsw_sp_ipip_netdev_ikey(ipip_entry->ol_dev);
95         char rtdp_pl[MLXSW_REG_RTDP_LEN];
96         unsigned int type_check;
97         u32 daddr4;
98
99         mlxsw_reg_rtdp_pack(rtdp_pl, MLXSW_REG_RTDP_TYPE_IPIP, tunnel_index);
100
101         type_check = has_ikey ?
102                 MLXSW_REG_RTDP_IPIP_TYPE_CHECK_ALLOW_GRE_KEY :
103                 MLXSW_REG_RTDP_IPIP_TYPE_CHECK_ALLOW_GRE;
104
105         /* Linux demuxes tunnels based on packet SIP (which must match tunnel
106          * remote IP). Thus configure decap so that it filters out packets that
107          * are not IPv4 or have the wrong SIP. IPIP_DECAP_ERROR trap is
108          * generated for packets that fail this criterion. Linux then handles
109          * such packets in slow path and generates ICMP destination unreachable.
110          */
111         daddr4 = be32_to_cpu(mlxsw_sp_ipip_netdev_daddr4(ipip_entry->ol_dev));
112         mlxsw_reg_rtdp_ipip4_pack(rtdp_pl, rif_index,
113                                   MLXSW_REG_RTDP_IPIP_SIP_CHECK_FILTER_IPV4,
114                                   type_check, has_ikey, daddr4, ikey);
115
116         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rtdp), rtdp_pl);
117 }
118
119 static int
120 mlxsw_sp_ipip_fib_entry_op_gre4_ralue(struct mlxsw_sp *mlxsw_sp,
121                                       u32 dip, u8 prefix_len, u16 ul_vr_id,
122                                       enum mlxsw_reg_ralue_op op,
123                                       u32 tunnel_index)
124 {
125         char ralue_pl[MLXSW_REG_RALUE_LEN];
126
127         mlxsw_reg_ralue_pack4(ralue_pl, MLXSW_REG_RALXX_PROTOCOL_IPV4, op,
128                               ul_vr_id, prefix_len, dip);
129         mlxsw_reg_ralue_act_ip2me_tun_pack(ralue_pl, tunnel_index);
130         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
131 }
132
133 static int mlxsw_sp_ipip_fib_entry_op_gre4(struct mlxsw_sp *mlxsw_sp,
134                                         struct mlxsw_sp_ipip_entry *ipip_entry,
135                                         enum mlxsw_reg_ralue_op op,
136                                         u32 tunnel_index)
137 {
138         u16 ul_vr_id = mlxsw_sp_ipip_lb_ul_vr_id(ipip_entry->ol_lb);
139         __be32 dip;
140         int err;
141
142         err = mlxsw_sp_ipip_fib_entry_op_gre4_rtdp(mlxsw_sp, tunnel_index,
143                                                    ipip_entry);
144         if (err)
145                 return err;
146
147         dip = mlxsw_sp_ipip_netdev_saddr(MLXSW_SP_L3_PROTO_IPV4,
148                                          ipip_entry->ol_dev).addr4;
149         return mlxsw_sp_ipip_fib_entry_op_gre4_ralue(mlxsw_sp, be32_to_cpu(dip),
150                                                      32, ul_vr_id, op,
151                                                      tunnel_index);
152 }
153
154 static bool mlxsw_sp_ipip_tunnel_complete(enum mlxsw_sp_l3proto proto,
155                                           const struct net_device *ol_dev)
156 {
157         union mlxsw_sp_l3addr saddr = mlxsw_sp_ipip_netdev_saddr(proto, ol_dev);
158         union mlxsw_sp_l3addr daddr = mlxsw_sp_ipip_netdev_daddr(proto, ol_dev);
159         union mlxsw_sp_l3addr naddr = {0};
160
161         /* Tunnels with unset local or remote address are valid in Linux and
162          * used for lightweight tunnels (LWT) and Non-Broadcast Multi-Access
163          * (NBMA) tunnels. In principle these can be offloaded, but the driver
164          * currently doesn't support this. So punt.
165          */
166         return memcmp(&saddr, &naddr, sizeof(naddr)) &&
167                memcmp(&daddr, &naddr, sizeof(naddr));
168 }
169
170 static bool mlxsw_sp_ipip_can_offload_gre4(const struct mlxsw_sp *mlxsw_sp,
171                                            const struct net_device *ol_dev,
172                                            enum mlxsw_sp_l3proto ol_proto)
173 {
174         struct ip_tunnel *tunnel = netdev_priv(ol_dev);
175         __be16 okflags = TUNNEL_KEY; /* We can't offload any other features. */
176         bool inherit_ttl = tunnel->parms.iph.ttl == 0;
177         bool inherit_tos = tunnel->parms.iph.tos & 0x1;
178
179         return (tunnel->parms.i_flags & ~okflags) == 0 &&
180                (tunnel->parms.o_flags & ~okflags) == 0 &&
181                inherit_ttl && inherit_tos &&
182                mlxsw_sp_ipip_tunnel_complete(MLXSW_SP_L3_PROTO_IPV4, ol_dev);
183 }
184
185 static struct mlxsw_sp_rif_ipip_lb_config
186 mlxsw_sp_ipip_ol_loopback_config_gre4(struct mlxsw_sp *mlxsw_sp,
187                                       const struct net_device *ol_dev)
188 {
189         enum mlxsw_reg_ritr_loopback_ipip_type lb_ipipt;
190
191         lb_ipipt = mlxsw_sp_ipip_netdev_has_okey(ol_dev) ?
192                 MLXSW_REG_RITR_LOOPBACK_IPIP_TYPE_IP_IN_GRE_KEY_IN_IP :
193                 MLXSW_REG_RITR_LOOPBACK_IPIP_TYPE_IP_IN_GRE_IN_IP;
194         return (struct mlxsw_sp_rif_ipip_lb_config){
195                 .lb_ipipt = lb_ipipt,
196                 .okey = mlxsw_sp_ipip_netdev_okey(ol_dev),
197                 .ul_protocol = MLXSW_SP_L3_PROTO_IPV4,
198                 .saddr = mlxsw_sp_ipip_netdev_saddr(MLXSW_SP_L3_PROTO_IPV4,
199                                                     ol_dev),
200         };
201 }
202
203 static const struct mlxsw_sp_ipip_ops mlxsw_sp_ipip_gre4_ops = {
204         .dev_type = ARPHRD_IPGRE,
205         .ul_proto = MLXSW_SP_L3_PROTO_IPV4,
206         .nexthop_update = mlxsw_sp_ipip_nexthop_update_gre4,
207         .fib_entry_op = mlxsw_sp_ipip_fib_entry_op_gre4,
208         .can_offload = mlxsw_sp_ipip_can_offload_gre4,
209         .ol_loopback_config = mlxsw_sp_ipip_ol_loopback_config_gre4,
210 };
211
212 const struct mlxsw_sp_ipip_ops *mlxsw_sp_ipip_ops_arr[] = {
213         [MLXSW_SP_IPIP_TYPE_GRE4] = &mlxsw_sp_ipip_gre4_ops,
214 };