blob: e9187841d82a5cbcf7380962eb6fe012f6f4ecb1 [file] [log] [blame]
Ido Schimmel464dce12016-07-02 11:00:15 +02001/*
2 * drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c
Petr Machata6ddb7422017-09-02 23:49:19 +02003 * Copyright (c) 2016-2017 Mellanox Technologies. All rights reserved.
Ido Schimmel464dce12016-07-02 11:00:15 +02004 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
5 * Copyright (c) 2016 Ido Schimmel <idosch@mellanox.com>
Yotam Gigic723c7352016-07-05 11:27:43 +02006 * Copyright (c) 2016 Yotam Gigi <yotamg@mellanox.com>
Petr Machata6ddb7422017-09-02 23:49:19 +02007 * Copyright (c) 2017 Petr Machata <petrm@mellanox.com>
Ido Schimmel464dce12016-07-02 11:00:15 +02008 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * Alternatively, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL") version 2 as published by the Free
23 * Software Foundation.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/kernel.h>
39#include <linux/types.h>
Jiri Pirko5e9c16c2016-07-04 08:23:04 +020040#include <linux/rhashtable.h>
41#include <linux/bitops.h>
42#include <linux/in6.h>
Yotam Gigic723c7352016-07-05 11:27:43 +020043#include <linux/notifier.h>
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +010044#include <linux/inetdevice.h>
Ido Schimmel9db032b2017-03-16 09:08:17 +010045#include <linux/netdevice.h>
Ido Schimmel03ea01e2017-05-23 21:56:30 +020046#include <linux/if_bridge.h>
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +020047#include <linux/socket.h>
Ido Schimmel428b8512017-08-03 13:28:28 +020048#include <linux/route.h>
Ido Schimmeleb789982017-10-22 23:11:48 +020049#include <linux/gcd.h>
Ido Schimmelaf658b62017-11-02 17:14:09 +010050#include <linux/random.h>
Yotam Gigic723c7352016-07-05 11:27:43 +020051#include <net/netevent.h>
Jiri Pirko6cf3c972016-07-05 11:27:39 +020052#include <net/neighbour.h>
53#include <net/arp.h>
Jiri Pirkob45f64d2016-09-26 12:52:31 +020054#include <net/ip_fib.h>
Ido Schimmel583419f2017-08-03 13:28:27 +020055#include <net/ip6_fib.h>
Ido Schimmel5d7bfd12017-03-16 09:08:14 +010056#include <net/fib_rules.h>
Petr Machata6ddb7422017-09-02 23:49:19 +020057#include <net/ip_tunnels.h>
Ido Schimmel57837882017-03-16 09:08:16 +010058#include <net/l3mdev.h>
Arkadi Sharshevsky5ea12372017-07-18 10:10:13 +020059#include <net/addrconf.h>
Arkadi Sharshevskyd5eb89c2017-07-18 10:10:15 +020060#include <net/ndisc.h>
61#include <net/ipv6.h>
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020062#include <net/fib_notifier.h>
Ido Schimmel464dce12016-07-02 11:00:15 +020063
64#include "spectrum.h"
65#include "core.h"
66#include "reg.h"
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +020067#include "spectrum_cnt.h"
68#include "spectrum_dpipe.h"
Petr Machata38ebc0f2017-09-02 23:49:17 +020069#include "spectrum_ipip.h"
Yotam Gigid42b0962017-09-27 08:23:20 +020070#include "spectrum_mr.h"
71#include "spectrum_mr_tcam.h"
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +020072#include "spectrum_router.h"
Ido Schimmel464dce12016-07-02 11:00:15 +020073
Ido Schimmel9011b672017-05-16 19:38:25 +020074struct mlxsw_sp_vr;
75struct mlxsw_sp_lpm_tree;
Ido Schimmele4f3c1c2017-05-26 08:37:40 +020076struct mlxsw_sp_rif_ops;
Ido Schimmel9011b672017-05-16 19:38:25 +020077
78struct mlxsw_sp_router {
79 struct mlxsw_sp *mlxsw_sp;
Ido Schimmel5f9efff2017-05-16 19:38:27 +020080 struct mlxsw_sp_rif **rifs;
Ido Schimmel9011b672017-05-16 19:38:25 +020081 struct mlxsw_sp_vr *vrs;
82 struct rhashtable neigh_ht;
83 struct rhashtable nexthop_group_ht;
84 struct rhashtable nexthop_ht;
Arkadi Sharshevskydbe45982017-09-25 10:32:23 +020085 struct list_head nexthop_list;
Ido Schimmel9011b672017-05-16 19:38:25 +020086 struct {
87 struct mlxsw_sp_lpm_tree *trees;
88 unsigned int tree_count;
89 } lpm;
90 struct {
91 struct delayed_work dw;
92 unsigned long interval; /* ms */
93 } neighs_update;
94 struct delayed_work nexthop_probe_dw;
95#define MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL 5000 /* ms */
96 struct list_head nexthop_neighs_list;
Petr Machata1012b9a2017-09-02 23:49:23 +020097 struct list_head ipip_list;
Ido Schimmel9011b672017-05-16 19:38:25 +020098 bool aborted;
Ido Schimmel7e39d112017-05-16 19:38:28 +020099 struct notifier_block fib_nb;
Ido Schimmel48fac882017-11-02 17:14:06 +0100100 struct notifier_block netevent_nb;
Ido Schimmele4f3c1c2017-05-26 08:37:40 +0200101 const struct mlxsw_sp_rif_ops **rif_ops_arr;
Petr Machata38ebc0f2017-09-02 23:49:17 +0200102 const struct mlxsw_sp_ipip_ops **ipip_ops_arr;
Ido Schimmel9011b672017-05-16 19:38:25 +0200103};
104
Ido Schimmel4724ba562017-03-10 08:53:39 +0100105struct mlxsw_sp_rif {
106 struct list_head nexthop_list;
107 struct list_head neigh_list;
108 struct net_device *dev;
Ido Schimmela1107482017-05-26 08:37:39 +0200109 struct mlxsw_sp_fid *fid;
Ido Schimmel4724ba562017-03-10 08:53:39 +0100110 unsigned char addr[ETH_ALEN];
111 int mtu;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +0100112 u16 rif_index;
Ido Schimmel69132292017-03-10 08:53:42 +0100113 u16 vr_id;
Ido Schimmele4f3c1c2017-05-26 08:37:40 +0200114 const struct mlxsw_sp_rif_ops *ops;
115 struct mlxsw_sp *mlxsw_sp;
116
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +0200117 unsigned int counter_ingress;
118 bool counter_ingress_valid;
119 unsigned int counter_egress;
120 bool counter_egress_valid;
Ido Schimmel4724ba562017-03-10 08:53:39 +0100121};
122
Ido Schimmele4f3c1c2017-05-26 08:37:40 +0200123struct mlxsw_sp_rif_params {
124 struct net_device *dev;
125 union {
126 u16 system_port;
127 u16 lag_id;
128 };
129 u16 vid;
130 bool lag;
131};
132
Ido Schimmel4d93cee2017-05-26 08:37:34 +0200133struct mlxsw_sp_rif_subport {
134 struct mlxsw_sp_rif common;
135 union {
136 u16 system_port;
137 u16 lag_id;
138 };
139 u16 vid;
140 bool lag;
141};
142
Petr Machata6ddb7422017-09-02 23:49:19 +0200143struct mlxsw_sp_rif_ipip_lb {
144 struct mlxsw_sp_rif common;
145 struct mlxsw_sp_rif_ipip_lb_config lb_config;
146 u16 ul_vr_id; /* Reserved for Spectrum-2. */
147};
148
149struct mlxsw_sp_rif_params_ipip_lb {
150 struct mlxsw_sp_rif_params common;
151 struct mlxsw_sp_rif_ipip_lb_config lb_config;
152};
153
Ido Schimmele4f3c1c2017-05-26 08:37:40 +0200154struct mlxsw_sp_rif_ops {
155 enum mlxsw_sp_rif_type type;
156 size_t rif_size;
157
158 void (*setup)(struct mlxsw_sp_rif *rif,
159 const struct mlxsw_sp_rif_params *params);
160 int (*configure)(struct mlxsw_sp_rif *rif);
161 void (*deconfigure)(struct mlxsw_sp_rif *rif);
162 struct mlxsw_sp_fid * (*fid_get)(struct mlxsw_sp_rif *rif);
163};
164
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +0200165static unsigned int *
166mlxsw_sp_rif_p_counter_get(struct mlxsw_sp_rif *rif,
167 enum mlxsw_sp_rif_counter_dir dir)
168{
169 switch (dir) {
170 case MLXSW_SP_RIF_COUNTER_EGRESS:
171 return &rif->counter_egress;
172 case MLXSW_SP_RIF_COUNTER_INGRESS:
173 return &rif->counter_ingress;
174 }
175 return NULL;
176}
177
178static bool
179mlxsw_sp_rif_counter_valid_get(struct mlxsw_sp_rif *rif,
180 enum mlxsw_sp_rif_counter_dir dir)
181{
182 switch (dir) {
183 case MLXSW_SP_RIF_COUNTER_EGRESS:
184 return rif->counter_egress_valid;
185 case MLXSW_SP_RIF_COUNTER_INGRESS:
186 return rif->counter_ingress_valid;
187 }
188 return false;
189}
190
191static void
192mlxsw_sp_rif_counter_valid_set(struct mlxsw_sp_rif *rif,
193 enum mlxsw_sp_rif_counter_dir dir,
194 bool valid)
195{
196 switch (dir) {
197 case MLXSW_SP_RIF_COUNTER_EGRESS:
198 rif->counter_egress_valid = valid;
199 break;
200 case MLXSW_SP_RIF_COUNTER_INGRESS:
201 rif->counter_ingress_valid = valid;
202 break;
203 }
204}
205
206static int mlxsw_sp_rif_counter_edit(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
207 unsigned int counter_index, bool enable,
208 enum mlxsw_sp_rif_counter_dir dir)
209{
210 char ritr_pl[MLXSW_REG_RITR_LEN];
211 bool is_egress = false;
212 int err;
213
214 if (dir == MLXSW_SP_RIF_COUNTER_EGRESS)
215 is_egress = true;
216 mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
217 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
218 if (err)
219 return err;
220
221 mlxsw_reg_ritr_counter_pack(ritr_pl, counter_index, enable,
222 is_egress);
223 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
224}
225
226int mlxsw_sp_rif_counter_value_get(struct mlxsw_sp *mlxsw_sp,
227 struct mlxsw_sp_rif *rif,
228 enum mlxsw_sp_rif_counter_dir dir, u64 *cnt)
229{
230 char ricnt_pl[MLXSW_REG_RICNT_LEN];
231 unsigned int *p_counter_index;
232 bool valid;
233 int err;
234
235 valid = mlxsw_sp_rif_counter_valid_get(rif, dir);
236 if (!valid)
237 return -EINVAL;
238
239 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
240 if (!p_counter_index)
241 return -EINVAL;
242 mlxsw_reg_ricnt_pack(ricnt_pl, *p_counter_index,
243 MLXSW_REG_RICNT_OPCODE_NOP);
244 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ricnt), ricnt_pl);
245 if (err)
246 return err;
247 *cnt = mlxsw_reg_ricnt_good_unicast_packets_get(ricnt_pl);
248 return 0;
249}
250
251static int mlxsw_sp_rif_counter_clear(struct mlxsw_sp *mlxsw_sp,
252 unsigned int counter_index)
253{
254 char ricnt_pl[MLXSW_REG_RICNT_LEN];
255
256 mlxsw_reg_ricnt_pack(ricnt_pl, counter_index,
257 MLXSW_REG_RICNT_OPCODE_CLEAR);
258 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ricnt), ricnt_pl);
259}
260
261int mlxsw_sp_rif_counter_alloc(struct mlxsw_sp *mlxsw_sp,
262 struct mlxsw_sp_rif *rif,
263 enum mlxsw_sp_rif_counter_dir dir)
264{
265 unsigned int *p_counter_index;
266 int err;
267
268 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
269 if (!p_counter_index)
270 return -EINVAL;
271 err = mlxsw_sp_counter_alloc(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
272 p_counter_index);
273 if (err)
274 return err;
275
276 err = mlxsw_sp_rif_counter_clear(mlxsw_sp, *p_counter_index);
277 if (err)
278 goto err_counter_clear;
279
280 err = mlxsw_sp_rif_counter_edit(mlxsw_sp, rif->rif_index,
281 *p_counter_index, true, dir);
282 if (err)
283 goto err_counter_edit;
284 mlxsw_sp_rif_counter_valid_set(rif, dir, true);
285 return 0;
286
287err_counter_edit:
288err_counter_clear:
289 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
290 *p_counter_index);
291 return err;
292}
293
294void mlxsw_sp_rif_counter_free(struct mlxsw_sp *mlxsw_sp,
295 struct mlxsw_sp_rif *rif,
296 enum mlxsw_sp_rif_counter_dir dir)
297{
298 unsigned int *p_counter_index;
299
Arkadi Sharshevsky6b1206b2017-05-18 09:18:53 +0200300 if (!mlxsw_sp_rif_counter_valid_get(rif, dir))
301 return;
302
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +0200303 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
304 if (WARN_ON(!p_counter_index))
305 return;
306 mlxsw_sp_rif_counter_edit(mlxsw_sp, rif->rif_index,
307 *p_counter_index, false, dir);
308 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
309 *p_counter_index);
310 mlxsw_sp_rif_counter_valid_set(rif, dir, false);
311}
312
Ido Schimmele4f3c1c2017-05-26 08:37:40 +0200313static void mlxsw_sp_rif_counters_alloc(struct mlxsw_sp_rif *rif)
314{
315 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
316 struct devlink *devlink;
317
318 devlink = priv_to_devlink(mlxsw_sp->core);
319 if (!devlink_dpipe_table_counter_enabled(devlink,
320 MLXSW_SP_DPIPE_TABLE_NAME_ERIF))
321 return;
322 mlxsw_sp_rif_counter_alloc(mlxsw_sp, rif, MLXSW_SP_RIF_COUNTER_EGRESS);
323}
324
325static void mlxsw_sp_rif_counters_free(struct mlxsw_sp_rif *rif)
326{
327 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
328
329 mlxsw_sp_rif_counter_free(mlxsw_sp, rif, MLXSW_SP_RIF_COUNTER_EGRESS);
330}
331
Ido Schimmel4724ba562017-03-10 08:53:39 +0100332static struct mlxsw_sp_rif *
333mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
334 const struct net_device *dev);
335
Ido Schimmel7dcc18a2017-07-18 10:10:30 +0200336#define MLXSW_SP_PREFIX_COUNT (sizeof(struct in6_addr) * BITS_PER_BYTE + 1)
Ido Schimmel9011b672017-05-16 19:38:25 +0200337
338struct mlxsw_sp_prefix_usage {
339 DECLARE_BITMAP(b, MLXSW_SP_PREFIX_COUNT);
340};
341
Jiri Pirko53342022016-07-04 08:23:08 +0200342#define mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) \
343 for_each_set_bit(prefix, (prefix_usage)->b, MLXSW_SP_PREFIX_COUNT)
344
345static bool
346mlxsw_sp_prefix_usage_eq(struct mlxsw_sp_prefix_usage *prefix_usage1,
347 struct mlxsw_sp_prefix_usage *prefix_usage2)
348{
349 return !memcmp(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
350}
351
Jiri Pirko6b75c482016-07-04 08:23:09 +0200352static bool
353mlxsw_sp_prefix_usage_none(struct mlxsw_sp_prefix_usage *prefix_usage)
354{
355 struct mlxsw_sp_prefix_usage prefix_usage_none = {{ 0 } };
356
357 return mlxsw_sp_prefix_usage_eq(prefix_usage, &prefix_usage_none);
358}
359
360static void
361mlxsw_sp_prefix_usage_cpy(struct mlxsw_sp_prefix_usage *prefix_usage1,
362 struct mlxsw_sp_prefix_usage *prefix_usage2)
363{
364 memcpy(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
365}
366
367static void
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200368mlxsw_sp_prefix_usage_set(struct mlxsw_sp_prefix_usage *prefix_usage,
369 unsigned char prefix_len)
370{
371 set_bit(prefix_len, prefix_usage->b);
372}
373
374static void
375mlxsw_sp_prefix_usage_clear(struct mlxsw_sp_prefix_usage *prefix_usage,
376 unsigned char prefix_len)
377{
378 clear_bit(prefix_len, prefix_usage->b);
379}
380
381struct mlxsw_sp_fib_key {
382 unsigned char addr[sizeof(struct in6_addr)];
383 unsigned char prefix_len;
384};
385
Jiri Pirko61c503f2016-07-04 08:23:11 +0200386enum mlxsw_sp_fib_entry_type {
387 MLXSW_SP_FIB_ENTRY_TYPE_REMOTE,
388 MLXSW_SP_FIB_ENTRY_TYPE_LOCAL,
389 MLXSW_SP_FIB_ENTRY_TYPE_TRAP,
Petr Machata4607f6d2017-09-02 23:49:25 +0200390
391 /* This is a special case of local delivery, where a packet should be
392 * decapsulated on reception. Note that there is no corresponding ENCAP,
393 * because that's a type of next hop, not of FIB entry. (There can be
394 * several next hops in a REMOTE entry, and some of them may be
395 * encapsulating entries.)
396 */
397 MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP,
Jiri Pirko61c503f2016-07-04 08:23:11 +0200398};
399
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200400struct mlxsw_sp_nexthop_group;
Ido Schimmel9011b672017-05-16 19:38:25 +0200401struct mlxsw_sp_fib;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200402
Ido Schimmel9aecce12017-02-09 10:28:42 +0100403struct mlxsw_sp_fib_node {
404 struct list_head entry_list;
Jiri Pirkob45f64d2016-09-26 12:52:31 +0200405 struct list_head list;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100406 struct rhash_head ht_node;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100407 struct mlxsw_sp_fib *fib;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100408 struct mlxsw_sp_fib_key key;
409};
410
Petr Machata4607f6d2017-09-02 23:49:25 +0200411struct mlxsw_sp_fib_entry_decap {
412 struct mlxsw_sp_ipip_entry *ipip_entry;
413 u32 tunnel_index;
414};
415
Ido Schimmel9aecce12017-02-09 10:28:42 +0100416struct mlxsw_sp_fib_entry {
417 struct list_head list;
418 struct mlxsw_sp_fib_node *fib_node;
419 enum mlxsw_sp_fib_entry_type type;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200420 struct list_head nexthop_group_node;
421 struct mlxsw_sp_nexthop_group *nh_group;
Petr Machata4607f6d2017-09-02 23:49:25 +0200422 struct mlxsw_sp_fib_entry_decap decap; /* Valid for decap entries. */
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200423};
424
Ido Schimmel4f1c7f12017-07-18 10:10:26 +0200425struct mlxsw_sp_fib4_entry {
426 struct mlxsw_sp_fib_entry common;
427 u32 tb_id;
428 u32 prio;
429 u8 tos;
430 u8 type;
431};
432
Ido Schimmel428b8512017-08-03 13:28:28 +0200433struct mlxsw_sp_fib6_entry {
434 struct mlxsw_sp_fib_entry common;
435 struct list_head rt6_list;
436 unsigned int nrt6;
437};
438
439struct mlxsw_sp_rt6 {
440 struct list_head list;
441 struct rt6_info *rt;
442};
443
Ido Schimmel9011b672017-05-16 19:38:25 +0200444struct mlxsw_sp_lpm_tree {
445 u8 id; /* tree ID */
446 unsigned int ref_count;
447 enum mlxsw_sp_l3proto proto;
448 struct mlxsw_sp_prefix_usage prefix_usage;
449};
450
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200451struct mlxsw_sp_fib {
452 struct rhashtable ht;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100453 struct list_head node_list;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100454 struct mlxsw_sp_vr *vr;
455 struct mlxsw_sp_lpm_tree *lpm_tree;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200456 unsigned long prefix_ref_count[MLXSW_SP_PREFIX_COUNT];
457 struct mlxsw_sp_prefix_usage prefix_usage;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100458 enum mlxsw_sp_l3proto proto;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200459};
460
Ido Schimmel9011b672017-05-16 19:38:25 +0200461struct mlxsw_sp_vr {
462 u16 id; /* virtual router ID */
463 u32 tb_id; /* kernel fib table id */
464 unsigned int rif_count;
465 struct mlxsw_sp_fib *fib4;
Ido Schimmela3d9bc52017-07-18 10:10:22 +0200466 struct mlxsw_sp_fib *fib6;
Yotam Gigid42b0962017-09-27 08:23:20 +0200467 struct mlxsw_sp_mr_table *mr4_table;
Ido Schimmel9011b672017-05-16 19:38:25 +0200468};
469
Ido Schimmel9aecce12017-02-09 10:28:42 +0100470static const struct rhashtable_params mlxsw_sp_fib_ht_params;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200471
Ido Schimmel76610eb2017-03-10 08:53:41 +0100472static struct mlxsw_sp_fib *mlxsw_sp_fib_create(struct mlxsw_sp_vr *vr,
473 enum mlxsw_sp_l3proto proto)
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200474{
475 struct mlxsw_sp_fib *fib;
476 int err;
477
478 fib = kzalloc(sizeof(*fib), GFP_KERNEL);
479 if (!fib)
480 return ERR_PTR(-ENOMEM);
481 err = rhashtable_init(&fib->ht, &mlxsw_sp_fib_ht_params);
482 if (err)
483 goto err_rhashtable_init;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100484 INIT_LIST_HEAD(&fib->node_list);
Ido Schimmel76610eb2017-03-10 08:53:41 +0100485 fib->proto = proto;
486 fib->vr = vr;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200487 return fib;
488
489err_rhashtable_init:
490 kfree(fib);
491 return ERR_PTR(err);
492}
493
494static void mlxsw_sp_fib_destroy(struct mlxsw_sp_fib *fib)
495{
Ido Schimmel9aecce12017-02-09 10:28:42 +0100496 WARN_ON(!list_empty(&fib->node_list));
Ido Schimmel76610eb2017-03-10 08:53:41 +0100497 WARN_ON(fib->lpm_tree);
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200498 rhashtable_destroy(&fib->ht);
499 kfree(fib);
500}
501
Jiri Pirko53342022016-07-04 08:23:08 +0200502static struct mlxsw_sp_lpm_tree *
Ido Schimmel382dbb42017-03-10 08:53:40 +0100503mlxsw_sp_lpm_tree_find_unused(struct mlxsw_sp *mlxsw_sp)
Jiri Pirko53342022016-07-04 08:23:08 +0200504{
505 static struct mlxsw_sp_lpm_tree *lpm_tree;
506 int i;
507
Ido Schimmel9011b672017-05-16 19:38:25 +0200508 for (i = 0; i < mlxsw_sp->router->lpm.tree_count; i++) {
509 lpm_tree = &mlxsw_sp->router->lpm.trees[i];
Ido Schimmel382dbb42017-03-10 08:53:40 +0100510 if (lpm_tree->ref_count == 0)
511 return lpm_tree;
Jiri Pirko53342022016-07-04 08:23:08 +0200512 }
513 return NULL;
514}
515
516static int mlxsw_sp_lpm_tree_alloc(struct mlxsw_sp *mlxsw_sp,
517 struct mlxsw_sp_lpm_tree *lpm_tree)
518{
519 char ralta_pl[MLXSW_REG_RALTA_LEN];
520
Ido Schimmel1a9234e662016-09-19 08:29:26 +0200521 mlxsw_reg_ralta_pack(ralta_pl, true,
522 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
523 lpm_tree->id);
Jiri Pirko53342022016-07-04 08:23:08 +0200524 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
525}
526
Ido Schimmelcc702672017-08-14 10:54:03 +0200527static void mlxsw_sp_lpm_tree_free(struct mlxsw_sp *mlxsw_sp,
528 struct mlxsw_sp_lpm_tree *lpm_tree)
Jiri Pirko53342022016-07-04 08:23:08 +0200529{
530 char ralta_pl[MLXSW_REG_RALTA_LEN];
531
Ido Schimmel1a9234e662016-09-19 08:29:26 +0200532 mlxsw_reg_ralta_pack(ralta_pl, false,
533 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
534 lpm_tree->id);
Ido Schimmelcc702672017-08-14 10:54:03 +0200535 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
Jiri Pirko53342022016-07-04 08:23:08 +0200536}
537
538static int
539mlxsw_sp_lpm_tree_left_struct_set(struct mlxsw_sp *mlxsw_sp,
540 struct mlxsw_sp_prefix_usage *prefix_usage,
541 struct mlxsw_sp_lpm_tree *lpm_tree)
542{
543 char ralst_pl[MLXSW_REG_RALST_LEN];
544 u8 root_bin = 0;
545 u8 prefix;
546 u8 last_prefix = MLXSW_REG_RALST_BIN_NO_CHILD;
547
548 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage)
549 root_bin = prefix;
550
551 mlxsw_reg_ralst_pack(ralst_pl, root_bin, lpm_tree->id);
552 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) {
553 if (prefix == 0)
554 continue;
555 mlxsw_reg_ralst_bin_pack(ralst_pl, prefix, last_prefix,
556 MLXSW_REG_RALST_BIN_NO_CHILD);
557 last_prefix = prefix;
558 }
559 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
560}
561
562static struct mlxsw_sp_lpm_tree *
563mlxsw_sp_lpm_tree_create(struct mlxsw_sp *mlxsw_sp,
564 struct mlxsw_sp_prefix_usage *prefix_usage,
Ido Schimmel382dbb42017-03-10 08:53:40 +0100565 enum mlxsw_sp_l3proto proto)
Jiri Pirko53342022016-07-04 08:23:08 +0200566{
567 struct mlxsw_sp_lpm_tree *lpm_tree;
568 int err;
569
Ido Schimmel382dbb42017-03-10 08:53:40 +0100570 lpm_tree = mlxsw_sp_lpm_tree_find_unused(mlxsw_sp);
Jiri Pirko53342022016-07-04 08:23:08 +0200571 if (!lpm_tree)
572 return ERR_PTR(-EBUSY);
573 lpm_tree->proto = proto;
574 err = mlxsw_sp_lpm_tree_alloc(mlxsw_sp, lpm_tree);
575 if (err)
576 return ERR_PTR(err);
577
578 err = mlxsw_sp_lpm_tree_left_struct_set(mlxsw_sp, prefix_usage,
579 lpm_tree);
580 if (err)
581 goto err_left_struct_set;
Jiri Pirko2083d362016-10-25 11:25:56 +0200582 memcpy(&lpm_tree->prefix_usage, prefix_usage,
583 sizeof(lpm_tree->prefix_usage));
Jiri Pirko53342022016-07-04 08:23:08 +0200584 return lpm_tree;
585
586err_left_struct_set:
587 mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
588 return ERR_PTR(err);
589}
590
Ido Schimmelcc702672017-08-14 10:54:03 +0200591static void mlxsw_sp_lpm_tree_destroy(struct mlxsw_sp *mlxsw_sp,
592 struct mlxsw_sp_lpm_tree *lpm_tree)
Jiri Pirko53342022016-07-04 08:23:08 +0200593{
Ido Schimmelcc702672017-08-14 10:54:03 +0200594 mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
Jiri Pirko53342022016-07-04 08:23:08 +0200595}
596
597static struct mlxsw_sp_lpm_tree *
598mlxsw_sp_lpm_tree_get(struct mlxsw_sp *mlxsw_sp,
599 struct mlxsw_sp_prefix_usage *prefix_usage,
Ido Schimmel382dbb42017-03-10 08:53:40 +0100600 enum mlxsw_sp_l3proto proto)
Jiri Pirko53342022016-07-04 08:23:08 +0200601{
602 struct mlxsw_sp_lpm_tree *lpm_tree;
603 int i;
604
Ido Schimmel9011b672017-05-16 19:38:25 +0200605 for (i = 0; i < mlxsw_sp->router->lpm.tree_count; i++) {
606 lpm_tree = &mlxsw_sp->router->lpm.trees[i];
Jiri Pirko8b99bec2016-10-25 11:25:57 +0200607 if (lpm_tree->ref_count != 0 &&
608 lpm_tree->proto == proto &&
Jiri Pirko53342022016-07-04 08:23:08 +0200609 mlxsw_sp_prefix_usage_eq(&lpm_tree->prefix_usage,
610 prefix_usage))
Ido Schimmelfc922bb2017-08-14 10:54:05 +0200611 return lpm_tree;
Jiri Pirko53342022016-07-04 08:23:08 +0200612 }
Ido Schimmelfc922bb2017-08-14 10:54:05 +0200613 return mlxsw_sp_lpm_tree_create(mlxsw_sp, prefix_usage, proto);
614}
Jiri Pirko53342022016-07-04 08:23:08 +0200615
Ido Schimmelfc922bb2017-08-14 10:54:05 +0200616static void mlxsw_sp_lpm_tree_hold(struct mlxsw_sp_lpm_tree *lpm_tree)
617{
Jiri Pirko53342022016-07-04 08:23:08 +0200618 lpm_tree->ref_count++;
Jiri Pirko53342022016-07-04 08:23:08 +0200619}
620
Ido Schimmelcc702672017-08-14 10:54:03 +0200621static void mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp,
622 struct mlxsw_sp_lpm_tree *lpm_tree)
Jiri Pirko53342022016-07-04 08:23:08 +0200623{
624 if (--lpm_tree->ref_count == 0)
Ido Schimmelcc702672017-08-14 10:54:03 +0200625 mlxsw_sp_lpm_tree_destroy(mlxsw_sp, lpm_tree);
Jiri Pirko53342022016-07-04 08:23:08 +0200626}
627
Ido Schimmeld7a60302017-06-08 08:47:43 +0200628#define MLXSW_SP_LPM_TREE_MIN 1 /* tree 0 is reserved */
Ido Schimmel8494ab02017-03-24 08:02:47 +0100629
630static int mlxsw_sp_lpm_init(struct mlxsw_sp *mlxsw_sp)
Jiri Pirko53342022016-07-04 08:23:08 +0200631{
632 struct mlxsw_sp_lpm_tree *lpm_tree;
Ido Schimmel8494ab02017-03-24 08:02:47 +0100633 u64 max_trees;
Jiri Pirko53342022016-07-04 08:23:08 +0200634 int i;
635
Ido Schimmel8494ab02017-03-24 08:02:47 +0100636 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LPM_TREES))
637 return -EIO;
638
639 max_trees = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LPM_TREES);
Ido Schimmel9011b672017-05-16 19:38:25 +0200640 mlxsw_sp->router->lpm.tree_count = max_trees - MLXSW_SP_LPM_TREE_MIN;
641 mlxsw_sp->router->lpm.trees = kcalloc(mlxsw_sp->router->lpm.tree_count,
Ido Schimmel8494ab02017-03-24 08:02:47 +0100642 sizeof(struct mlxsw_sp_lpm_tree),
643 GFP_KERNEL);
Ido Schimmel9011b672017-05-16 19:38:25 +0200644 if (!mlxsw_sp->router->lpm.trees)
Ido Schimmel8494ab02017-03-24 08:02:47 +0100645 return -ENOMEM;
646
Ido Schimmel9011b672017-05-16 19:38:25 +0200647 for (i = 0; i < mlxsw_sp->router->lpm.tree_count; i++) {
648 lpm_tree = &mlxsw_sp->router->lpm.trees[i];
Jiri Pirko53342022016-07-04 08:23:08 +0200649 lpm_tree->id = i + MLXSW_SP_LPM_TREE_MIN;
650 }
Ido Schimmel8494ab02017-03-24 08:02:47 +0100651
652 return 0;
653}
654
655static void mlxsw_sp_lpm_fini(struct mlxsw_sp *mlxsw_sp)
656{
Ido Schimmel9011b672017-05-16 19:38:25 +0200657 kfree(mlxsw_sp->router->lpm.trees);
Jiri Pirko53342022016-07-04 08:23:08 +0200658}
659
Ido Schimmel76610eb2017-03-10 08:53:41 +0100660static bool mlxsw_sp_vr_is_used(const struct mlxsw_sp_vr *vr)
661{
Yotam Gigid42b0962017-09-27 08:23:20 +0200662 return !!vr->fib4 || !!vr->fib6 || !!vr->mr4_table;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100663}
664
Jiri Pirko6b75c482016-07-04 08:23:09 +0200665static struct mlxsw_sp_vr *mlxsw_sp_vr_find_unused(struct mlxsw_sp *mlxsw_sp)
666{
667 struct mlxsw_sp_vr *vr;
668 int i;
669
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200670 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Ido Schimmel9011b672017-05-16 19:38:25 +0200671 vr = &mlxsw_sp->router->vrs[i];
Ido Schimmel76610eb2017-03-10 08:53:41 +0100672 if (!mlxsw_sp_vr_is_used(vr))
Jiri Pirko6b75c482016-07-04 08:23:09 +0200673 return vr;
674 }
675 return NULL;
676}
677
678static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel0adb2142017-08-14 10:54:04 +0200679 const struct mlxsw_sp_fib *fib, u8 tree_id)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200680{
681 char raltb_pl[MLXSW_REG_RALTB_LEN];
682
Ido Schimmel76610eb2017-03-10 08:53:41 +0100683 mlxsw_reg_raltb_pack(raltb_pl, fib->vr->id,
684 (enum mlxsw_reg_ralxx_protocol) fib->proto,
Ido Schimmel0adb2142017-08-14 10:54:04 +0200685 tree_id);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200686 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
687}
688
689static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel76610eb2017-03-10 08:53:41 +0100690 const struct mlxsw_sp_fib *fib)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200691{
692 char raltb_pl[MLXSW_REG_RALTB_LEN];
693
694 /* Bind to tree 0 which is default */
Ido Schimmel76610eb2017-03-10 08:53:41 +0100695 mlxsw_reg_raltb_pack(raltb_pl, fib->vr->id,
696 (enum mlxsw_reg_ralxx_protocol) fib->proto, 0);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200697 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
698}
699
700static u32 mlxsw_sp_fix_tb_id(u32 tb_id)
701{
Yotam Gigi7e50d432017-09-27 08:23:19 +0200702 /* For our purpose, squash main, default and local tables into one */
703 if (tb_id == RT_TABLE_LOCAL || tb_id == RT_TABLE_DEFAULT)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200704 tb_id = RT_TABLE_MAIN;
705 return tb_id;
706}
707
708static struct mlxsw_sp_vr *mlxsw_sp_vr_find(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel76610eb2017-03-10 08:53:41 +0100709 u32 tb_id)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200710{
711 struct mlxsw_sp_vr *vr;
712 int i;
713
714 tb_id = mlxsw_sp_fix_tb_id(tb_id);
Nogah Frankel9497c042016-09-20 11:16:54 +0200715
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200716 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Ido Schimmel9011b672017-05-16 19:38:25 +0200717 vr = &mlxsw_sp->router->vrs[i];
Ido Schimmel76610eb2017-03-10 08:53:41 +0100718 if (mlxsw_sp_vr_is_used(vr) && vr->tb_id == tb_id)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200719 return vr;
720 }
721 return NULL;
722}
723
Ido Schimmel76610eb2017-03-10 08:53:41 +0100724static struct mlxsw_sp_fib *mlxsw_sp_vr_fib(const struct mlxsw_sp_vr *vr,
725 enum mlxsw_sp_l3proto proto)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200726{
Ido Schimmel76610eb2017-03-10 08:53:41 +0100727 switch (proto) {
728 case MLXSW_SP_L3_PROTO_IPV4:
729 return vr->fib4;
730 case MLXSW_SP_L3_PROTO_IPV6:
Ido Schimmela3d9bc52017-07-18 10:10:22 +0200731 return vr->fib6;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100732 }
733 return NULL;
734}
735
736static struct mlxsw_sp_vr *mlxsw_sp_vr_create(struct mlxsw_sp *mlxsw_sp,
David Ahernf8fa9b42017-10-18 09:56:56 -0700737 u32 tb_id,
738 struct netlink_ext_ack *extack)
Ido Schimmel76610eb2017-03-10 08:53:41 +0100739{
Jiri Pirko6b75c482016-07-04 08:23:09 +0200740 struct mlxsw_sp_vr *vr;
Ido Schimmela3d9bc52017-07-18 10:10:22 +0200741 int err;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200742
743 vr = mlxsw_sp_vr_find_unused(mlxsw_sp);
David Ahernf8fa9b42017-10-18 09:56:56 -0700744 if (!vr) {
745 NL_SET_ERR_MSG(extack, "spectrum: Exceeded number of supported virtual routers");
Jiri Pirko6b75c482016-07-04 08:23:09 +0200746 return ERR_PTR(-EBUSY);
David Ahernf8fa9b42017-10-18 09:56:56 -0700747 }
Ido Schimmel76610eb2017-03-10 08:53:41 +0100748 vr->fib4 = mlxsw_sp_fib_create(vr, MLXSW_SP_L3_PROTO_IPV4);
749 if (IS_ERR(vr->fib4))
750 return ERR_CAST(vr->fib4);
Ido Schimmela3d9bc52017-07-18 10:10:22 +0200751 vr->fib6 = mlxsw_sp_fib_create(vr, MLXSW_SP_L3_PROTO_IPV6);
752 if (IS_ERR(vr->fib6)) {
753 err = PTR_ERR(vr->fib6);
754 goto err_fib6_create;
755 }
Yotam Gigid42b0962017-09-27 08:23:20 +0200756 vr->mr4_table = mlxsw_sp_mr_table_create(mlxsw_sp, vr->id,
757 MLXSW_SP_L3_PROTO_IPV4);
758 if (IS_ERR(vr->mr4_table)) {
759 err = PTR_ERR(vr->mr4_table);
760 goto err_mr_table_create;
761 }
Jiri Pirko6b75c482016-07-04 08:23:09 +0200762 vr->tb_id = tb_id;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200763 return vr;
Ido Schimmela3d9bc52017-07-18 10:10:22 +0200764
Yotam Gigid42b0962017-09-27 08:23:20 +0200765err_mr_table_create:
766 mlxsw_sp_fib_destroy(vr->fib6);
767 vr->fib6 = NULL;
Ido Schimmela3d9bc52017-07-18 10:10:22 +0200768err_fib6_create:
769 mlxsw_sp_fib_destroy(vr->fib4);
770 vr->fib4 = NULL;
771 return ERR_PTR(err);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200772}
773
Ido Schimmel76610eb2017-03-10 08:53:41 +0100774static void mlxsw_sp_vr_destroy(struct mlxsw_sp_vr *vr)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200775{
Yotam Gigid42b0962017-09-27 08:23:20 +0200776 mlxsw_sp_mr_table_destroy(vr->mr4_table);
777 vr->mr4_table = NULL;
Ido Schimmela3d9bc52017-07-18 10:10:22 +0200778 mlxsw_sp_fib_destroy(vr->fib6);
779 vr->fib6 = NULL;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100780 mlxsw_sp_fib_destroy(vr->fib4);
781 vr->fib4 = NULL;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200782}
783
David Ahernf8fa9b42017-10-18 09:56:56 -0700784static struct mlxsw_sp_vr *mlxsw_sp_vr_get(struct mlxsw_sp *mlxsw_sp, u32 tb_id,
785 struct netlink_ext_ack *extack)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200786{
787 struct mlxsw_sp_vr *vr;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200788
789 tb_id = mlxsw_sp_fix_tb_id(tb_id);
Ido Schimmel76610eb2017-03-10 08:53:41 +0100790 vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id);
791 if (!vr)
David Ahernf8fa9b42017-10-18 09:56:56 -0700792 vr = mlxsw_sp_vr_create(mlxsw_sp, tb_id, extack);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200793 return vr;
794}
795
Ido Schimmel76610eb2017-03-10 08:53:41 +0100796static void mlxsw_sp_vr_put(struct mlxsw_sp_vr *vr)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200797{
Ido Schimmela3d9bc52017-07-18 10:10:22 +0200798 if (!vr->rif_count && list_empty(&vr->fib4->node_list) &&
Yotam Gigid42b0962017-09-27 08:23:20 +0200799 list_empty(&vr->fib6->node_list) &&
800 mlxsw_sp_mr_table_empty(vr->mr4_table))
Ido Schimmel76610eb2017-03-10 08:53:41 +0100801 mlxsw_sp_vr_destroy(vr);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200802}
803
Ido Schimmelfc922bb2017-08-14 10:54:05 +0200804static bool
805mlxsw_sp_vr_lpm_tree_should_replace(struct mlxsw_sp_vr *vr,
806 enum mlxsw_sp_l3proto proto, u8 tree_id)
807{
808 struct mlxsw_sp_fib *fib = mlxsw_sp_vr_fib(vr, proto);
809
810 if (!mlxsw_sp_vr_is_used(vr))
811 return false;
812 if (fib->lpm_tree && fib->lpm_tree->id == tree_id)
813 return true;
814 return false;
815}
816
817static int mlxsw_sp_vr_lpm_tree_replace(struct mlxsw_sp *mlxsw_sp,
818 struct mlxsw_sp_fib *fib,
819 struct mlxsw_sp_lpm_tree *new_tree)
820{
821 struct mlxsw_sp_lpm_tree *old_tree = fib->lpm_tree;
822 int err;
823
824 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, fib, new_tree->id);
825 if (err)
826 return err;
827 fib->lpm_tree = new_tree;
828 mlxsw_sp_lpm_tree_hold(new_tree);
829 mlxsw_sp_lpm_tree_put(mlxsw_sp, old_tree);
830 return 0;
831}
832
833static int mlxsw_sp_vrs_lpm_tree_replace(struct mlxsw_sp *mlxsw_sp,
834 struct mlxsw_sp_fib *fib,
835 struct mlxsw_sp_lpm_tree *new_tree)
836{
837 struct mlxsw_sp_lpm_tree *old_tree = fib->lpm_tree;
838 enum mlxsw_sp_l3proto proto = fib->proto;
839 u8 old_id, new_id = new_tree->id;
840 struct mlxsw_sp_vr *vr;
841 int i, err;
842
843 if (!old_tree)
844 goto no_replace;
845 old_id = old_tree->id;
846
847 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
848 vr = &mlxsw_sp->router->vrs[i];
849 if (!mlxsw_sp_vr_lpm_tree_should_replace(vr, proto, old_id))
850 continue;
851 err = mlxsw_sp_vr_lpm_tree_replace(mlxsw_sp,
852 mlxsw_sp_vr_fib(vr, proto),
853 new_tree);
854 if (err)
855 goto err_tree_replace;
856 }
857
858 return 0;
859
860err_tree_replace:
861 for (i--; i >= 0; i--) {
862 if (!mlxsw_sp_vr_lpm_tree_should_replace(vr, proto, new_id))
863 continue;
864 mlxsw_sp_vr_lpm_tree_replace(mlxsw_sp,
865 mlxsw_sp_vr_fib(vr, proto),
866 old_tree);
867 }
868 return err;
869
870no_replace:
871 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, fib, new_tree->id);
872 if (err)
873 return err;
874 fib->lpm_tree = new_tree;
875 mlxsw_sp_lpm_tree_hold(new_tree);
876 return 0;
877}
878
879static void
880mlxsw_sp_vrs_prefixes(struct mlxsw_sp *mlxsw_sp,
881 enum mlxsw_sp_l3proto proto,
882 struct mlxsw_sp_prefix_usage *req_prefix_usage)
883{
884 int i;
885
886 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
887 struct mlxsw_sp_vr *vr = &mlxsw_sp->router->vrs[i];
888 struct mlxsw_sp_fib *fib = mlxsw_sp_vr_fib(vr, proto);
889 unsigned char prefix;
890
891 if (!mlxsw_sp_vr_is_used(vr))
892 continue;
893 mlxsw_sp_prefix_usage_for_each(prefix, &fib->prefix_usage)
894 mlxsw_sp_prefix_usage_set(req_prefix_usage, prefix);
895 }
896}
897
Nogah Frankel9497c042016-09-20 11:16:54 +0200898static int mlxsw_sp_vrs_init(struct mlxsw_sp *mlxsw_sp)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200899{
900 struct mlxsw_sp_vr *vr;
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200901 u64 max_vrs;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200902 int i;
903
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200904 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_VRS))
Nogah Frankel9497c042016-09-20 11:16:54 +0200905 return -EIO;
906
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200907 max_vrs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS);
Ido Schimmel9011b672017-05-16 19:38:25 +0200908 mlxsw_sp->router->vrs = kcalloc(max_vrs, sizeof(struct mlxsw_sp_vr),
909 GFP_KERNEL);
910 if (!mlxsw_sp->router->vrs)
Nogah Frankel9497c042016-09-20 11:16:54 +0200911 return -ENOMEM;
912
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200913 for (i = 0; i < max_vrs; i++) {
Ido Schimmel9011b672017-05-16 19:38:25 +0200914 vr = &mlxsw_sp->router->vrs[i];
Jiri Pirko6b75c482016-07-04 08:23:09 +0200915 vr->id = i;
916 }
Nogah Frankel9497c042016-09-20 11:16:54 +0200917
918 return 0;
919}
920
Ido Schimmelac571de2016-11-14 11:26:32 +0100921static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp);
922
Nogah Frankel9497c042016-09-20 11:16:54 +0200923static void mlxsw_sp_vrs_fini(struct mlxsw_sp *mlxsw_sp)
924{
Ido Schimmel30572242016-12-03 16:45:01 +0100925 /* At this stage we're guaranteed not to have new incoming
926 * FIB notifications and the work queue is free from FIBs
927 * sitting on top of mlxsw netdevs. However, we can still
928 * have other FIBs queued. Flush the queue before flushing
929 * the device's tables. No need for locks, as we're the only
930 * writer.
931 */
932 mlxsw_core_flush_owq();
Ido Schimmelac571de2016-11-14 11:26:32 +0100933 mlxsw_sp_router_fib_flush(mlxsw_sp);
Ido Schimmel9011b672017-05-16 19:38:25 +0200934 kfree(mlxsw_sp->router->vrs);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200935}
936
Petr Machata6ddb7422017-09-02 23:49:19 +0200937static struct net_device *
938__mlxsw_sp_ipip_netdev_ul_dev_get(const struct net_device *ol_dev)
939{
940 struct ip_tunnel *tun = netdev_priv(ol_dev);
941 struct net *net = dev_net(ol_dev);
942
943 return __dev_get_by_index(net, tun->parms.link);
944}
945
Petr Machata4cf04f32017-11-03 10:03:42 +0100946u32 mlxsw_sp_ipip_dev_ul_tb_id(const struct net_device *ol_dev)
Petr Machata6ddb7422017-09-02 23:49:19 +0200947{
948 struct net_device *d = __mlxsw_sp_ipip_netdev_ul_dev_get(ol_dev);
949
950 if (d)
951 return l3mdev_fib_table(d) ? : RT_TABLE_MAIN;
952 else
953 return l3mdev_fib_table(ol_dev) ? : RT_TABLE_MAIN;
954}
955
Petr Machata1012b9a2017-09-02 23:49:23 +0200956static struct mlxsw_sp_rif *
957mlxsw_sp_rif_create(struct mlxsw_sp *mlxsw_sp,
David Ahernf8fa9b42017-10-18 09:56:56 -0700958 const struct mlxsw_sp_rif_params *params,
959 struct netlink_ext_ack *extack);
Petr Machata1012b9a2017-09-02 23:49:23 +0200960
961static struct mlxsw_sp_rif_ipip_lb *
962mlxsw_sp_ipip_ol_ipip_lb_create(struct mlxsw_sp *mlxsw_sp,
963 enum mlxsw_sp_ipip_type ipipt,
Petr Machata7e75af62017-11-03 10:03:36 +0100964 struct net_device *ol_dev,
965 struct netlink_ext_ack *extack)
Petr Machata1012b9a2017-09-02 23:49:23 +0200966{
967 struct mlxsw_sp_rif_params_ipip_lb lb_params;
968 const struct mlxsw_sp_ipip_ops *ipip_ops;
969 struct mlxsw_sp_rif *rif;
970
971 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipipt];
972 lb_params = (struct mlxsw_sp_rif_params_ipip_lb) {
973 .common.dev = ol_dev,
974 .common.lag = false,
975 .lb_config = ipip_ops->ol_loopback_config(mlxsw_sp, ol_dev),
976 };
977
Petr Machata7e75af62017-11-03 10:03:36 +0100978 rif = mlxsw_sp_rif_create(mlxsw_sp, &lb_params.common, extack);
Petr Machata1012b9a2017-09-02 23:49:23 +0200979 if (IS_ERR(rif))
980 return ERR_CAST(rif);
981 return container_of(rif, struct mlxsw_sp_rif_ipip_lb, common);
982}
983
984static struct mlxsw_sp_ipip_entry *
985mlxsw_sp_ipip_entry_alloc(struct mlxsw_sp *mlxsw_sp,
986 enum mlxsw_sp_ipip_type ipipt,
987 struct net_device *ol_dev)
988{
989 struct mlxsw_sp_ipip_entry *ipip_entry;
990 struct mlxsw_sp_ipip_entry *ret = NULL;
991
992 ipip_entry = kzalloc(sizeof(*ipip_entry), GFP_KERNEL);
993 if (!ipip_entry)
994 return ERR_PTR(-ENOMEM);
995
996 ipip_entry->ol_lb = mlxsw_sp_ipip_ol_ipip_lb_create(mlxsw_sp, ipipt,
Petr Machata7e75af62017-11-03 10:03:36 +0100997 ol_dev, NULL);
Petr Machata1012b9a2017-09-02 23:49:23 +0200998 if (IS_ERR(ipip_entry->ol_lb)) {
999 ret = ERR_CAST(ipip_entry->ol_lb);
1000 goto err_ol_ipip_lb_create;
1001 }
1002
1003 ipip_entry->ipipt = ipipt;
1004 ipip_entry->ol_dev = ol_dev;
Petr Machata4cf04f32017-11-03 10:03:42 +01001005 ipip_entry->parms = mlxsw_sp_ipip_netdev_parms(ol_dev);
Petr Machata1012b9a2017-09-02 23:49:23 +02001006
1007 return ipip_entry;
1008
1009err_ol_ipip_lb_create:
1010 kfree(ipip_entry);
1011 return ret;
1012}
1013
1014static void
Petr Machata4cccb732017-10-16 16:26:39 +02001015mlxsw_sp_ipip_entry_dealloc(struct mlxsw_sp_ipip_entry *ipip_entry)
Petr Machata1012b9a2017-09-02 23:49:23 +02001016{
Petr Machata1012b9a2017-09-02 23:49:23 +02001017 mlxsw_sp_rif_destroy(&ipip_entry->ol_lb->common);
1018 kfree(ipip_entry);
1019}
1020
Petr Machata1012b9a2017-09-02 23:49:23 +02001021static bool
1022mlxsw_sp_ipip_entry_saddr_matches(struct mlxsw_sp *mlxsw_sp,
1023 const enum mlxsw_sp_l3proto ul_proto,
1024 union mlxsw_sp_l3addr saddr,
1025 u32 ul_tb_id,
1026 struct mlxsw_sp_ipip_entry *ipip_entry)
1027{
1028 u32 tun_ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(ipip_entry->ol_dev);
1029 enum mlxsw_sp_ipip_type ipipt = ipip_entry->ipipt;
1030 union mlxsw_sp_l3addr tun_saddr;
1031
1032 if (mlxsw_sp->router->ipip_ops_arr[ipipt]->ul_proto != ul_proto)
1033 return false;
1034
1035 tun_saddr = mlxsw_sp_ipip_netdev_saddr(ul_proto, ipip_entry->ol_dev);
1036 return tun_ul_tb_id == ul_tb_id &&
1037 mlxsw_sp_l3addr_eq(&tun_saddr, &saddr);
1038}
1039
Petr Machata4607f6d2017-09-02 23:49:25 +02001040static int
1041mlxsw_sp_fib_entry_decap_init(struct mlxsw_sp *mlxsw_sp,
1042 struct mlxsw_sp_fib_entry *fib_entry,
1043 struct mlxsw_sp_ipip_entry *ipip_entry)
1044{
1045 u32 tunnel_index;
1046 int err;
1047
1048 err = mlxsw_sp_kvdl_alloc(mlxsw_sp, 1, &tunnel_index);
1049 if (err)
1050 return err;
1051
1052 ipip_entry->decap_fib_entry = fib_entry;
1053 fib_entry->decap.ipip_entry = ipip_entry;
1054 fib_entry->decap.tunnel_index = tunnel_index;
1055 return 0;
1056}
1057
1058static void mlxsw_sp_fib_entry_decap_fini(struct mlxsw_sp *mlxsw_sp,
1059 struct mlxsw_sp_fib_entry *fib_entry)
1060{
1061 /* Unlink this node from the IPIP entry that it's the decap entry of. */
1062 fib_entry->decap.ipip_entry->decap_fib_entry = NULL;
1063 fib_entry->decap.ipip_entry = NULL;
1064 mlxsw_sp_kvdl_free(mlxsw_sp, fib_entry->decap.tunnel_index);
1065}
1066
Petr Machata1cc38fb2017-09-02 23:49:26 +02001067static struct mlxsw_sp_fib_node *
1068mlxsw_sp_fib_node_lookup(struct mlxsw_sp_fib *fib, const void *addr,
1069 size_t addr_len, unsigned char prefix_len);
Petr Machata4607f6d2017-09-02 23:49:25 +02001070static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1071 struct mlxsw_sp_fib_entry *fib_entry);
1072
1073static void
1074mlxsw_sp_ipip_entry_demote_decap(struct mlxsw_sp *mlxsw_sp,
1075 struct mlxsw_sp_ipip_entry *ipip_entry)
1076{
1077 struct mlxsw_sp_fib_entry *fib_entry = ipip_entry->decap_fib_entry;
1078
1079 mlxsw_sp_fib_entry_decap_fini(mlxsw_sp, fib_entry);
1080 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
1081
1082 mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1083}
1084
Petr Machata1cc38fb2017-09-02 23:49:26 +02001085static void
1086mlxsw_sp_ipip_entry_promote_decap(struct mlxsw_sp *mlxsw_sp,
1087 struct mlxsw_sp_ipip_entry *ipip_entry,
1088 struct mlxsw_sp_fib_entry *decap_fib_entry)
1089{
1090 if (mlxsw_sp_fib_entry_decap_init(mlxsw_sp, decap_fib_entry,
1091 ipip_entry))
1092 return;
1093 decap_fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP;
1094
1095 if (mlxsw_sp_fib_entry_update(mlxsw_sp, decap_fib_entry))
1096 mlxsw_sp_ipip_entry_demote_decap(mlxsw_sp, ipip_entry);
1097}
1098
1099/* Given an IPIP entry, find the corresponding decap route. */
1100static struct mlxsw_sp_fib_entry *
1101mlxsw_sp_ipip_entry_find_decap(struct mlxsw_sp *mlxsw_sp,
1102 struct mlxsw_sp_ipip_entry *ipip_entry)
1103{
1104 static struct mlxsw_sp_fib_node *fib_node;
1105 const struct mlxsw_sp_ipip_ops *ipip_ops;
1106 struct mlxsw_sp_fib_entry *fib_entry;
1107 unsigned char saddr_prefix_len;
1108 union mlxsw_sp_l3addr saddr;
1109 struct mlxsw_sp_fib *ul_fib;
1110 struct mlxsw_sp_vr *ul_vr;
1111 const void *saddrp;
1112 size_t saddr_len;
1113 u32 ul_tb_id;
1114 u32 saddr4;
1115
1116 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
1117
1118 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(ipip_entry->ol_dev);
1119 ul_vr = mlxsw_sp_vr_find(mlxsw_sp, ul_tb_id);
1120 if (!ul_vr)
1121 return NULL;
1122
1123 ul_fib = mlxsw_sp_vr_fib(ul_vr, ipip_ops->ul_proto);
1124 saddr = mlxsw_sp_ipip_netdev_saddr(ipip_ops->ul_proto,
1125 ipip_entry->ol_dev);
1126
1127 switch (ipip_ops->ul_proto) {
1128 case MLXSW_SP_L3_PROTO_IPV4:
1129 saddr4 = be32_to_cpu(saddr.addr4);
1130 saddrp = &saddr4;
1131 saddr_len = 4;
1132 saddr_prefix_len = 32;
1133 break;
1134 case MLXSW_SP_L3_PROTO_IPV6:
1135 WARN_ON(1);
1136 return NULL;
1137 }
1138
1139 fib_node = mlxsw_sp_fib_node_lookup(ul_fib, saddrp, saddr_len,
1140 saddr_prefix_len);
1141 if (!fib_node || list_empty(&fib_node->entry_list))
1142 return NULL;
1143
1144 fib_entry = list_first_entry(&fib_node->entry_list,
1145 struct mlxsw_sp_fib_entry, list);
1146 if (fib_entry->type != MLXSW_SP_FIB_ENTRY_TYPE_TRAP)
1147 return NULL;
1148
1149 return fib_entry;
1150}
1151
Petr Machata1012b9a2017-09-02 23:49:23 +02001152static struct mlxsw_sp_ipip_entry *
Petr Machata4cccb732017-10-16 16:26:39 +02001153mlxsw_sp_ipip_entry_create(struct mlxsw_sp *mlxsw_sp,
1154 enum mlxsw_sp_ipip_type ipipt,
1155 struct net_device *ol_dev)
Petr Machata1012b9a2017-09-02 23:49:23 +02001156{
Petr Machata1012b9a2017-09-02 23:49:23 +02001157 struct mlxsw_sp_ipip_entry *ipip_entry;
Petr Machata1012b9a2017-09-02 23:49:23 +02001158
1159 ipip_entry = mlxsw_sp_ipip_entry_alloc(mlxsw_sp, ipipt, ol_dev);
1160 if (IS_ERR(ipip_entry))
1161 return ipip_entry;
1162
1163 list_add_tail(&ipip_entry->ipip_list_node,
1164 &mlxsw_sp->router->ipip_list);
1165
Petr Machata1012b9a2017-09-02 23:49:23 +02001166 return ipip_entry;
1167}
1168
1169static void
Petr Machata4cccb732017-10-16 16:26:39 +02001170mlxsw_sp_ipip_entry_destroy(struct mlxsw_sp *mlxsw_sp,
1171 struct mlxsw_sp_ipip_entry *ipip_entry)
Petr Machata1012b9a2017-09-02 23:49:23 +02001172{
Petr Machata4cccb732017-10-16 16:26:39 +02001173 list_del(&ipip_entry->ipip_list_node);
1174 mlxsw_sp_ipip_entry_dealloc(ipip_entry);
Petr Machata1012b9a2017-09-02 23:49:23 +02001175}
1176
Petr Machata4607f6d2017-09-02 23:49:25 +02001177static bool
1178mlxsw_sp_ipip_entry_matches_decap(struct mlxsw_sp *mlxsw_sp,
1179 const struct net_device *ul_dev,
1180 enum mlxsw_sp_l3proto ul_proto,
1181 union mlxsw_sp_l3addr ul_dip,
1182 struct mlxsw_sp_ipip_entry *ipip_entry)
1183{
1184 u32 ul_tb_id = l3mdev_fib_table(ul_dev) ? : RT_TABLE_MAIN;
1185 enum mlxsw_sp_ipip_type ipipt = ipip_entry->ipipt;
1186 struct net_device *ipip_ul_dev;
1187
1188 if (mlxsw_sp->router->ipip_ops_arr[ipipt]->ul_proto != ul_proto)
1189 return false;
1190
1191 ipip_ul_dev = __mlxsw_sp_ipip_netdev_ul_dev_get(ipip_entry->ol_dev);
1192 return mlxsw_sp_ipip_entry_saddr_matches(mlxsw_sp, ul_proto, ul_dip,
1193 ul_tb_id, ipip_entry) &&
1194 (!ipip_ul_dev || ipip_ul_dev == ul_dev);
1195}
1196
1197/* Given decap parameters, find the corresponding IPIP entry. */
1198static struct mlxsw_sp_ipip_entry *
1199mlxsw_sp_ipip_entry_find_by_decap(struct mlxsw_sp *mlxsw_sp,
1200 const struct net_device *ul_dev,
1201 enum mlxsw_sp_l3proto ul_proto,
1202 union mlxsw_sp_l3addr ul_dip)
1203{
1204 struct mlxsw_sp_ipip_entry *ipip_entry;
1205
1206 list_for_each_entry(ipip_entry, &mlxsw_sp->router->ipip_list,
1207 ipip_list_node)
1208 if (mlxsw_sp_ipip_entry_matches_decap(mlxsw_sp, ul_dev,
1209 ul_proto, ul_dip,
1210 ipip_entry))
1211 return ipip_entry;
1212
1213 return NULL;
1214}
1215
Petr Machata6698c162017-10-16 16:26:36 +02001216static bool mlxsw_sp_netdev_ipip_type(const struct mlxsw_sp *mlxsw_sp,
1217 const struct net_device *dev,
1218 enum mlxsw_sp_ipip_type *p_type)
1219{
1220 struct mlxsw_sp_router *router = mlxsw_sp->router;
1221 const struct mlxsw_sp_ipip_ops *ipip_ops;
1222 enum mlxsw_sp_ipip_type ipipt;
1223
1224 for (ipipt = 0; ipipt < MLXSW_SP_IPIP_TYPE_MAX; ++ipipt) {
1225 ipip_ops = router->ipip_ops_arr[ipipt];
1226 if (dev->type == ipip_ops->dev_type) {
1227 if (p_type)
1228 *p_type = ipipt;
1229 return true;
1230 }
1231 }
1232 return false;
1233}
1234
Petr Machata796ec772017-11-03 10:03:29 +01001235bool mlxsw_sp_netdev_is_ipip_ol(const struct mlxsw_sp *mlxsw_sp,
1236 const struct net_device *dev)
Petr Machata00635872017-10-16 16:26:37 +02001237{
1238 return mlxsw_sp_netdev_ipip_type(mlxsw_sp, dev, NULL);
1239}
1240
1241static struct mlxsw_sp_ipip_entry *
1242mlxsw_sp_ipip_entry_find_by_ol_dev(struct mlxsw_sp *mlxsw_sp,
1243 const struct net_device *ol_dev)
1244{
1245 struct mlxsw_sp_ipip_entry *ipip_entry;
1246
1247 list_for_each_entry(ipip_entry, &mlxsw_sp->router->ipip_list,
1248 ipip_list_node)
1249 if (ipip_entry->ol_dev == ol_dev)
1250 return ipip_entry;
1251
1252 return NULL;
1253}
1254
Petr Machata61481f22017-11-03 10:03:41 +01001255static struct mlxsw_sp_ipip_entry *
1256mlxsw_sp_ipip_entry_find_by_ul_dev(const struct mlxsw_sp *mlxsw_sp,
1257 const struct net_device *ul_dev,
1258 struct mlxsw_sp_ipip_entry *start)
1259{
1260 struct mlxsw_sp_ipip_entry *ipip_entry;
1261
1262 ipip_entry = list_prepare_entry(start, &mlxsw_sp->router->ipip_list,
1263 ipip_list_node);
1264 list_for_each_entry_continue(ipip_entry, &mlxsw_sp->router->ipip_list,
1265 ipip_list_node) {
1266 struct net_device *ipip_ul_dev =
1267 __mlxsw_sp_ipip_netdev_ul_dev_get(ipip_entry->ol_dev);
1268
1269 if (ipip_ul_dev == ul_dev)
1270 return ipip_entry;
1271 }
1272
1273 return NULL;
1274}
1275
1276bool mlxsw_sp_netdev_is_ipip_ul(const struct mlxsw_sp *mlxsw_sp,
1277 const struct net_device *dev)
1278{
1279 return mlxsw_sp_ipip_entry_find_by_ul_dev(mlxsw_sp, dev, NULL);
1280}
1281
Petr Machatacafdb2a2017-11-03 10:03:30 +01001282static bool mlxsw_sp_netdevice_ipip_can_offload(struct mlxsw_sp *mlxsw_sp,
1283 const struct net_device *ol_dev,
1284 enum mlxsw_sp_ipip_type ipipt)
1285{
1286 const struct mlxsw_sp_ipip_ops *ops
1287 = mlxsw_sp->router->ipip_ops_arr[ipipt];
1288
1289 /* For deciding whether decap should be offloaded, we don't care about
1290 * overlay protocol, so ask whether either one is supported.
1291 */
1292 return ops->can_offload(mlxsw_sp, ol_dev, MLXSW_SP_L3_PROTO_IPV4) ||
1293 ops->can_offload(mlxsw_sp, ol_dev, MLXSW_SP_L3_PROTO_IPV6);
1294}
1295
Petr Machata796ec772017-11-03 10:03:29 +01001296static int mlxsw_sp_netdevice_ipip_ol_reg_event(struct mlxsw_sp *mlxsw_sp,
1297 struct net_device *ol_dev)
Petr Machata00635872017-10-16 16:26:37 +02001298{
Petr Machata00635872017-10-16 16:26:37 +02001299 struct mlxsw_sp_ipip_entry *ipip_entry;
Petr Machataaf641712017-11-03 10:03:40 +01001300 enum mlxsw_sp_l3proto ul_proto;
Petr Machata00635872017-10-16 16:26:37 +02001301 enum mlxsw_sp_ipip_type ipipt;
Petr Machataaf641712017-11-03 10:03:40 +01001302 union mlxsw_sp_l3addr saddr;
1303 u32 ul_tb_id;
Petr Machata00635872017-10-16 16:26:37 +02001304
1305 mlxsw_sp_netdev_ipip_type(mlxsw_sp, ol_dev, &ipipt);
Petr Machatacafdb2a2017-11-03 10:03:30 +01001306 if (mlxsw_sp_netdevice_ipip_can_offload(mlxsw_sp, ol_dev, ipipt)) {
Petr Machataaf641712017-11-03 10:03:40 +01001307 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(ol_dev);
1308 ul_proto = mlxsw_sp->router->ipip_ops_arr[ipipt]->ul_proto;
1309 saddr = mlxsw_sp_ipip_netdev_saddr(ul_proto, ol_dev);
1310 if (!mlxsw_sp_ipip_demote_tunnel_by_saddr(mlxsw_sp, ul_proto,
1311 saddr, ul_tb_id,
1312 NULL)) {
1313 ipip_entry = mlxsw_sp_ipip_entry_create(mlxsw_sp, ipipt,
1314 ol_dev);
1315 if (IS_ERR(ipip_entry))
1316 return PTR_ERR(ipip_entry);
1317 }
Petr Machata00635872017-10-16 16:26:37 +02001318 }
1319
1320 return 0;
1321}
1322
Petr Machata796ec772017-11-03 10:03:29 +01001323static void mlxsw_sp_netdevice_ipip_ol_unreg_event(struct mlxsw_sp *mlxsw_sp,
1324 struct net_device *ol_dev)
Petr Machata00635872017-10-16 16:26:37 +02001325{
1326 struct mlxsw_sp_ipip_entry *ipip_entry;
1327
1328 ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
1329 if (ipip_entry)
Petr Machata4cccb732017-10-16 16:26:39 +02001330 mlxsw_sp_ipip_entry_destroy(mlxsw_sp, ipip_entry);
Petr Machata00635872017-10-16 16:26:37 +02001331}
1332
Petr Machata47518ca2017-11-03 10:03:35 +01001333static void
1334mlxsw_sp_ipip_entry_ol_up_event(struct mlxsw_sp *mlxsw_sp,
1335 struct mlxsw_sp_ipip_entry *ipip_entry)
1336{
1337 struct mlxsw_sp_fib_entry *decap_fib_entry;
1338
1339 decap_fib_entry = mlxsw_sp_ipip_entry_find_decap(mlxsw_sp, ipip_entry);
1340 if (decap_fib_entry)
1341 mlxsw_sp_ipip_entry_promote_decap(mlxsw_sp, ipip_entry,
1342 decap_fib_entry);
1343}
1344
Petr Machata6d4de442017-11-03 10:03:34 +01001345static void mlxsw_sp_netdevice_ipip_ol_up_event(struct mlxsw_sp *mlxsw_sp,
1346 struct net_device *ol_dev)
Petr Machata00635872017-10-16 16:26:37 +02001347{
Petr Machata00635872017-10-16 16:26:37 +02001348 struct mlxsw_sp_ipip_entry *ipip_entry;
1349
1350 ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
Petr Machata47518ca2017-11-03 10:03:35 +01001351 if (ipip_entry)
1352 mlxsw_sp_ipip_entry_ol_up_event(mlxsw_sp, ipip_entry);
Petr Machata00635872017-10-16 16:26:37 +02001353}
1354
Petr Machataa3fe1982017-11-03 10:03:33 +01001355static void
1356mlxsw_sp_ipip_entry_ol_down_event(struct mlxsw_sp *mlxsw_sp,
1357 struct mlxsw_sp_ipip_entry *ipip_entry)
1358{
1359 if (ipip_entry->decap_fib_entry)
1360 mlxsw_sp_ipip_entry_demote_decap(mlxsw_sp, ipip_entry);
1361}
1362
Petr Machata796ec772017-11-03 10:03:29 +01001363static void mlxsw_sp_netdevice_ipip_ol_down_event(struct mlxsw_sp *mlxsw_sp,
1364 struct net_device *ol_dev)
Petr Machata00635872017-10-16 16:26:37 +02001365{
1366 struct mlxsw_sp_ipip_entry *ipip_entry;
1367
1368 ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
Petr Machataa3fe1982017-11-03 10:03:33 +01001369 if (ipip_entry)
1370 mlxsw_sp_ipip_entry_ol_down_event(mlxsw_sp, ipip_entry);
Petr Machata00635872017-10-16 16:26:37 +02001371}
1372
Petr Machata0c5f1cd2017-11-03 10:03:38 +01001373static void mlxsw_sp_nexthop_rif_update(struct mlxsw_sp *mlxsw_sp,
1374 struct mlxsw_sp_rif *rif);
Petr Machata65a61212017-11-03 10:03:37 +01001375static int
1376mlxsw_sp_ipip_entry_ol_lb_update(struct mlxsw_sp *mlxsw_sp,
1377 struct mlxsw_sp_ipip_entry *ipip_entry,
Petr Machata0c5f1cd2017-11-03 10:03:38 +01001378 bool keep_encap,
Petr Machata65a61212017-11-03 10:03:37 +01001379 struct netlink_ext_ack *extack)
Petr Machataf63ce4e2017-10-16 16:26:38 +02001380{
Petr Machata65a61212017-11-03 10:03:37 +01001381 struct mlxsw_sp_rif_ipip_lb *old_lb_rif = ipip_entry->ol_lb;
1382 struct mlxsw_sp_rif_ipip_lb *new_lb_rif;
Petr Machataf63ce4e2017-10-16 16:26:38 +02001383
Petr Machata65a61212017-11-03 10:03:37 +01001384 new_lb_rif = mlxsw_sp_ipip_ol_ipip_lb_create(mlxsw_sp,
1385 ipip_entry->ipipt,
1386 ipip_entry->ol_dev,
1387 extack);
1388 if (IS_ERR(new_lb_rif))
1389 return PTR_ERR(new_lb_rif);
1390 ipip_entry->ol_lb = new_lb_rif;
Petr Machata0c5f1cd2017-11-03 10:03:38 +01001391
1392 if (keep_encap) {
1393 list_splice_init(&old_lb_rif->common.nexthop_list,
1394 &new_lb_rif->common.nexthop_list);
1395 mlxsw_sp_nexthop_rif_update(mlxsw_sp, &new_lb_rif->common);
1396 }
1397
Petr Machata65a61212017-11-03 10:03:37 +01001398 mlxsw_sp_rif_destroy(&old_lb_rif->common);
Petr Machataf63ce4e2017-10-16 16:26:38 +02001399
Petr Machata65a61212017-11-03 10:03:37 +01001400 return 0;
1401}
1402
Petr Machata0c5f1cd2017-11-03 10:03:38 +01001403/**
1404 * Update the offload related to an IPIP entry. This always updates decap, and
1405 * in addition to that it also:
1406 * @recreate_loopback: recreates the associated loopback RIF
1407 * @keep_encap: updates next hops that use the tunnel netdevice. This is only
1408 * relevant when recreate_loopback is true.
1409 * @update_nexthops: updates next hops, keeping the current loopback RIF. This
1410 * is only relevant when recreate_loopback is false.
1411 */
Petr Machata65a61212017-11-03 10:03:37 +01001412int __mlxsw_sp_ipip_entry_update_tunnel(struct mlxsw_sp *mlxsw_sp,
1413 struct mlxsw_sp_ipip_entry *ipip_entry,
Petr Machata0c5f1cd2017-11-03 10:03:38 +01001414 bool recreate_loopback,
1415 bool keep_encap,
1416 bool update_nexthops,
Petr Machata65a61212017-11-03 10:03:37 +01001417 struct netlink_ext_ack *extack)
1418{
1419 int err;
1420
1421 /* RIFs can't be edited, so to update loopback, we need to destroy and
1422 * recreate it. That creates a window of opportunity where RALUE and
1423 * RATR registers end up referencing a RIF that's already gone. RATRs
1424 * are handled in mlxsw_sp_ipip_entry_ol_lb_update(), and to take care
Petr Machataf63ce4e2017-10-16 16:26:38 +02001425 * of RALUE, demote the decap route back.
1426 */
1427 if (ipip_entry->decap_fib_entry)
1428 mlxsw_sp_ipip_entry_demote_decap(mlxsw_sp, ipip_entry);
1429
Petr Machata0c5f1cd2017-11-03 10:03:38 +01001430 if (recreate_loopback) {
1431 err = mlxsw_sp_ipip_entry_ol_lb_update(mlxsw_sp, ipip_entry,
1432 keep_encap, extack);
1433 if (err)
1434 return err;
1435 } else if (update_nexthops) {
1436 mlxsw_sp_nexthop_rif_update(mlxsw_sp,
1437 &ipip_entry->ol_lb->common);
1438 }
Petr Machataf63ce4e2017-10-16 16:26:38 +02001439
Petr Machata65a61212017-11-03 10:03:37 +01001440 if (ipip_entry->ol_dev->flags & IFF_UP)
1441 mlxsw_sp_ipip_entry_ol_up_event(mlxsw_sp, ipip_entry);
Petr Machataf63ce4e2017-10-16 16:26:38 +02001442
1443 return 0;
1444}
1445
Petr Machata65a61212017-11-03 10:03:37 +01001446static int mlxsw_sp_netdevice_ipip_ol_vrf_event(struct mlxsw_sp *mlxsw_sp,
1447 struct net_device *ol_dev,
1448 struct netlink_ext_ack *extack)
1449{
1450 struct mlxsw_sp_ipip_entry *ipip_entry =
1451 mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
1452
1453 if (!ipip_entry)
1454 return 0;
1455 return __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
Petr Machata0c5f1cd2017-11-03 10:03:38 +01001456 true, false, false, extack);
Petr Machata65a61212017-11-03 10:03:37 +01001457}
1458
Petr Machata61481f22017-11-03 10:03:41 +01001459static int
1460mlxsw_sp_netdevice_ipip_ul_vrf_event(struct mlxsw_sp *mlxsw_sp,
1461 struct mlxsw_sp_ipip_entry *ipip_entry,
1462 struct net_device *ul_dev,
1463 struct netlink_ext_ack *extack)
1464{
1465 return __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
1466 true, true, false, extack);
1467}
1468
Petr Machata4cf04f32017-11-03 10:03:42 +01001469static int
Petr Machata44b0fff2017-11-03 10:03:44 +01001470mlxsw_sp_netdevice_ipip_ul_up_event(struct mlxsw_sp *mlxsw_sp,
1471 struct mlxsw_sp_ipip_entry *ipip_entry,
1472 struct net_device *ul_dev)
1473{
1474 return __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
1475 false, false, true, NULL);
1476}
1477
1478static int
1479mlxsw_sp_netdevice_ipip_ul_down_event(struct mlxsw_sp *mlxsw_sp,
1480 struct mlxsw_sp_ipip_entry *ipip_entry,
1481 struct net_device *ul_dev)
1482{
1483 /* A down underlay device causes encapsulated packets to not be
1484 * forwarded, but decap still works. So refresh next hops without
1485 * touching anything else.
1486 */
1487 return __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
1488 false, false, true, NULL);
1489}
1490
1491static int
Petr Machata4cf04f32017-11-03 10:03:42 +01001492mlxsw_sp_netdevice_ipip_ol_change_event(struct mlxsw_sp *mlxsw_sp,
1493 struct net_device *ol_dev,
1494 struct netlink_ext_ack *extack)
1495{
1496 const struct mlxsw_sp_ipip_ops *ipip_ops;
1497 struct mlxsw_sp_ipip_entry *ipip_entry;
1498 int err;
1499
1500 ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
1501 if (!ipip_entry)
1502 /* A change might make a tunnel eligible for offloading, but
1503 * that is currently not implemented. What falls to slow path
1504 * stays there.
1505 */
1506 return 0;
1507
1508 /* A change might make a tunnel not eligible for offloading. */
1509 if (!mlxsw_sp_netdevice_ipip_can_offload(mlxsw_sp, ol_dev,
1510 ipip_entry->ipipt)) {
1511 mlxsw_sp_ipip_entry_demote_tunnel(mlxsw_sp, ipip_entry);
1512 return 0;
1513 }
1514
1515 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
1516 err = ipip_ops->ol_netdev_change(mlxsw_sp, ipip_entry, extack);
1517 return err;
1518}
1519
Petr Machataaf641712017-11-03 10:03:40 +01001520void mlxsw_sp_ipip_entry_demote_tunnel(struct mlxsw_sp *mlxsw_sp,
1521 struct mlxsw_sp_ipip_entry *ipip_entry)
1522{
1523 struct net_device *ol_dev = ipip_entry->ol_dev;
1524
1525 if (ol_dev->flags & IFF_UP)
1526 mlxsw_sp_ipip_entry_ol_down_event(mlxsw_sp, ipip_entry);
1527 mlxsw_sp_ipip_entry_destroy(mlxsw_sp, ipip_entry);
1528}
1529
1530/* The configuration where several tunnels have the same local address in the
1531 * same underlay table needs special treatment in the HW. That is currently not
1532 * implemented in the driver. This function finds and demotes the first tunnel
1533 * with a given source address, except the one passed in in the argument
1534 * `except'.
1535 */
1536bool
1537mlxsw_sp_ipip_demote_tunnel_by_saddr(struct mlxsw_sp *mlxsw_sp,
1538 enum mlxsw_sp_l3proto ul_proto,
1539 union mlxsw_sp_l3addr saddr,
1540 u32 ul_tb_id,
1541 const struct mlxsw_sp_ipip_entry *except)
1542{
1543 struct mlxsw_sp_ipip_entry *ipip_entry, *tmp;
1544
1545 list_for_each_entry_safe(ipip_entry, tmp, &mlxsw_sp->router->ipip_list,
1546 ipip_list_node) {
1547 if (ipip_entry != except &&
1548 mlxsw_sp_ipip_entry_saddr_matches(mlxsw_sp, ul_proto, saddr,
1549 ul_tb_id, ipip_entry)) {
1550 mlxsw_sp_ipip_entry_demote_tunnel(mlxsw_sp, ipip_entry);
1551 return true;
1552 }
1553 }
1554
1555 return false;
1556}
1557
Petr Machata61481f22017-11-03 10:03:41 +01001558static void mlxsw_sp_ipip_demote_tunnel_by_ul_netdev(struct mlxsw_sp *mlxsw_sp,
1559 struct net_device *ul_dev)
1560{
1561 struct mlxsw_sp_ipip_entry *ipip_entry, *tmp;
1562
1563 list_for_each_entry_safe(ipip_entry, tmp, &mlxsw_sp->router->ipip_list,
1564 ipip_list_node) {
1565 struct net_device *ipip_ul_dev =
1566 __mlxsw_sp_ipip_netdev_ul_dev_get(ipip_entry->ol_dev);
1567
1568 if (ipip_ul_dev == ul_dev)
1569 mlxsw_sp_ipip_entry_demote_tunnel(mlxsw_sp, ipip_entry);
1570 }
1571}
1572
Petr Machata7e75af62017-11-03 10:03:36 +01001573int mlxsw_sp_netdevice_ipip_ol_event(struct mlxsw_sp *mlxsw_sp,
1574 struct net_device *ol_dev,
1575 unsigned long event,
1576 struct netdev_notifier_info *info)
Petr Machata00635872017-10-16 16:26:37 +02001577{
Petr Machata7e75af62017-11-03 10:03:36 +01001578 struct netdev_notifier_changeupper_info *chup;
1579 struct netlink_ext_ack *extack;
1580
Petr Machata00635872017-10-16 16:26:37 +02001581 switch (event) {
1582 case NETDEV_REGISTER:
Petr Machata796ec772017-11-03 10:03:29 +01001583 return mlxsw_sp_netdevice_ipip_ol_reg_event(mlxsw_sp, ol_dev);
Petr Machata00635872017-10-16 16:26:37 +02001584 case NETDEV_UNREGISTER:
Petr Machata796ec772017-11-03 10:03:29 +01001585 mlxsw_sp_netdevice_ipip_ol_unreg_event(mlxsw_sp, ol_dev);
Petr Machata00635872017-10-16 16:26:37 +02001586 return 0;
1587 case NETDEV_UP:
Petr Machata6d4de442017-11-03 10:03:34 +01001588 mlxsw_sp_netdevice_ipip_ol_up_event(mlxsw_sp, ol_dev);
1589 return 0;
Petr Machata00635872017-10-16 16:26:37 +02001590 case NETDEV_DOWN:
Petr Machata796ec772017-11-03 10:03:29 +01001591 mlxsw_sp_netdevice_ipip_ol_down_event(mlxsw_sp, ol_dev);
Petr Machata00635872017-10-16 16:26:37 +02001592 return 0;
Petr Machataf63ce4e2017-10-16 16:26:38 +02001593 case NETDEV_CHANGEUPPER:
Petr Machata7e75af62017-11-03 10:03:36 +01001594 chup = container_of(info, typeof(*chup), info);
1595 extack = info->extack;
1596 if (netif_is_l3_master(chup->upper_dev))
Petr Machata796ec772017-11-03 10:03:29 +01001597 return mlxsw_sp_netdevice_ipip_ol_vrf_event(mlxsw_sp,
Petr Machata7e75af62017-11-03 10:03:36 +01001598 ol_dev,
1599 extack);
Petr Machataf63ce4e2017-10-16 16:26:38 +02001600 return 0;
Petr Machata4cf04f32017-11-03 10:03:42 +01001601 case NETDEV_CHANGE:
1602 extack = info->extack;
1603 return mlxsw_sp_netdevice_ipip_ol_change_event(mlxsw_sp,
1604 ol_dev, extack);
Petr Machata00635872017-10-16 16:26:37 +02001605 }
1606 return 0;
1607}
1608
Petr Machata61481f22017-11-03 10:03:41 +01001609static int
1610__mlxsw_sp_netdevice_ipip_ul_event(struct mlxsw_sp *mlxsw_sp,
1611 struct mlxsw_sp_ipip_entry *ipip_entry,
1612 struct net_device *ul_dev,
1613 unsigned long event,
1614 struct netdev_notifier_info *info)
1615{
1616 struct netdev_notifier_changeupper_info *chup;
1617 struct netlink_ext_ack *extack;
1618
1619 switch (event) {
1620 case NETDEV_CHANGEUPPER:
1621 chup = container_of(info, typeof(*chup), info);
1622 extack = info->extack;
1623 if (netif_is_l3_master(chup->upper_dev))
1624 return mlxsw_sp_netdevice_ipip_ul_vrf_event(mlxsw_sp,
1625 ipip_entry,
1626 ul_dev,
1627 extack);
1628 break;
Petr Machata44b0fff2017-11-03 10:03:44 +01001629
1630 case NETDEV_UP:
1631 return mlxsw_sp_netdevice_ipip_ul_up_event(mlxsw_sp, ipip_entry,
1632 ul_dev);
1633 case NETDEV_DOWN:
1634 return mlxsw_sp_netdevice_ipip_ul_down_event(mlxsw_sp,
1635 ipip_entry,
1636 ul_dev);
Petr Machata61481f22017-11-03 10:03:41 +01001637 }
1638 return 0;
1639}
1640
1641int
1642mlxsw_sp_netdevice_ipip_ul_event(struct mlxsw_sp *mlxsw_sp,
1643 struct net_device *ul_dev,
1644 unsigned long event,
1645 struct netdev_notifier_info *info)
1646{
1647 struct mlxsw_sp_ipip_entry *ipip_entry = NULL;
1648 int err;
1649
1650 while ((ipip_entry = mlxsw_sp_ipip_entry_find_by_ul_dev(mlxsw_sp,
1651 ul_dev,
1652 ipip_entry))) {
1653 err = __mlxsw_sp_netdevice_ipip_ul_event(mlxsw_sp, ipip_entry,
1654 ul_dev, event, info);
1655 if (err) {
1656 mlxsw_sp_ipip_demote_tunnel_by_ul_netdev(mlxsw_sp,
1657 ul_dev);
1658 return err;
1659 }
1660 }
1661
1662 return 0;
1663}
1664
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001665struct mlxsw_sp_neigh_key {
Jiri Pirko33b13412016-11-10 12:31:04 +01001666 struct neighbour *n;
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001667};
1668
1669struct mlxsw_sp_neigh_entry {
Ido Schimmel9665b742017-02-08 11:16:42 +01001670 struct list_head rif_list_node;
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001671 struct rhash_head ht_node;
1672 struct mlxsw_sp_neigh_key key;
1673 u16 rif;
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001674 bool connected;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001675 unsigned char ha[ETH_ALEN];
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001676 struct list_head nexthop_list; /* list of nexthops using
1677 * this neigh entry
1678 */
Yotam Gigib2157142016-07-05 11:27:51 +02001679 struct list_head nexthop_neighs_list_node;
Arkadi Sharshevsky7cfcbc72017-08-24 08:40:08 +02001680 unsigned int counter_index;
1681 bool counter_valid;
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001682};
1683
1684static const struct rhashtable_params mlxsw_sp_neigh_ht_params = {
1685 .key_offset = offsetof(struct mlxsw_sp_neigh_entry, key),
1686 .head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node),
1687 .key_len = sizeof(struct mlxsw_sp_neigh_key),
1688};
1689
Arkadi Sharshevskyf17cc842017-08-24 08:40:04 +02001690struct mlxsw_sp_neigh_entry *
1691mlxsw_sp_rif_neigh_next(struct mlxsw_sp_rif *rif,
1692 struct mlxsw_sp_neigh_entry *neigh_entry)
1693{
1694 if (!neigh_entry) {
1695 if (list_empty(&rif->neigh_list))
1696 return NULL;
1697 else
1698 return list_first_entry(&rif->neigh_list,
1699 typeof(*neigh_entry),
1700 rif_list_node);
1701 }
Arkadi Sharshevskyec2437f2017-09-25 10:32:24 +02001702 if (list_is_last(&neigh_entry->rif_list_node, &rif->neigh_list))
Arkadi Sharshevskyf17cc842017-08-24 08:40:04 +02001703 return NULL;
1704 return list_next_entry(neigh_entry, rif_list_node);
1705}
1706
1707int mlxsw_sp_neigh_entry_type(struct mlxsw_sp_neigh_entry *neigh_entry)
1708{
1709 return neigh_entry->key.n->tbl->family;
1710}
1711
1712unsigned char *
1713mlxsw_sp_neigh_entry_ha(struct mlxsw_sp_neigh_entry *neigh_entry)
1714{
1715 return neigh_entry->ha;
1716}
1717
1718u32 mlxsw_sp_neigh4_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry)
1719{
1720 struct neighbour *n;
1721
1722 n = neigh_entry->key.n;
1723 return ntohl(*((__be32 *) n->primary_key));
1724}
1725
Arkadi Sharshevsky02507682017-08-31 17:59:15 +02001726struct in6_addr *
1727mlxsw_sp_neigh6_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry)
1728{
1729 struct neighbour *n;
1730
1731 n = neigh_entry->key.n;
1732 return (struct in6_addr *) &n->primary_key;
1733}
1734
Arkadi Sharshevsky7cfcbc72017-08-24 08:40:08 +02001735int mlxsw_sp_neigh_counter_get(struct mlxsw_sp *mlxsw_sp,
1736 struct mlxsw_sp_neigh_entry *neigh_entry,
1737 u64 *p_counter)
1738{
1739 if (!neigh_entry->counter_valid)
1740 return -EINVAL;
1741
1742 return mlxsw_sp_flow_counter_get(mlxsw_sp, neigh_entry->counter_index,
1743 p_counter, NULL);
1744}
1745
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001746static struct mlxsw_sp_neigh_entry *
1747mlxsw_sp_neigh_entry_alloc(struct mlxsw_sp *mlxsw_sp, struct neighbour *n,
1748 u16 rif)
1749{
1750 struct mlxsw_sp_neigh_entry *neigh_entry;
1751
1752 neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_KERNEL);
1753 if (!neigh_entry)
1754 return NULL;
1755
1756 neigh_entry->key.n = n;
1757 neigh_entry->rif = rif;
1758 INIT_LIST_HEAD(&neigh_entry->nexthop_list);
1759
1760 return neigh_entry;
1761}
1762
1763static void mlxsw_sp_neigh_entry_free(struct mlxsw_sp_neigh_entry *neigh_entry)
1764{
1765 kfree(neigh_entry);
1766}
1767
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001768static int
1769mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp,
1770 struct mlxsw_sp_neigh_entry *neigh_entry)
1771{
Ido Schimmel9011b672017-05-16 19:38:25 +02001772 return rhashtable_insert_fast(&mlxsw_sp->router->neigh_ht,
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001773 &neigh_entry->ht_node,
1774 mlxsw_sp_neigh_ht_params);
1775}
1776
1777static void
1778mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp,
1779 struct mlxsw_sp_neigh_entry *neigh_entry)
1780{
Ido Schimmel9011b672017-05-16 19:38:25 +02001781 rhashtable_remove_fast(&mlxsw_sp->router->neigh_ht,
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001782 &neigh_entry->ht_node,
1783 mlxsw_sp_neigh_ht_params);
1784}
1785
Arkadi Sharshevsky7cfcbc72017-08-24 08:40:08 +02001786static bool
Arkadi Sharshevsky1ed55742017-08-31 17:59:18 +02001787mlxsw_sp_neigh_counter_should_alloc(struct mlxsw_sp *mlxsw_sp,
1788 struct mlxsw_sp_neigh_entry *neigh_entry)
Arkadi Sharshevsky7cfcbc72017-08-24 08:40:08 +02001789{
1790 struct devlink *devlink;
Arkadi Sharshevsky1ed55742017-08-31 17:59:18 +02001791 const char *table_name;
1792
1793 switch (mlxsw_sp_neigh_entry_type(neigh_entry)) {
1794 case AF_INET:
1795 table_name = MLXSW_SP_DPIPE_TABLE_NAME_HOST4;
1796 break;
1797 case AF_INET6:
1798 table_name = MLXSW_SP_DPIPE_TABLE_NAME_HOST6;
1799 break;
1800 default:
1801 WARN_ON(1);
1802 return false;
1803 }
Arkadi Sharshevsky7cfcbc72017-08-24 08:40:08 +02001804
1805 devlink = priv_to_devlink(mlxsw_sp->core);
Arkadi Sharshevsky1ed55742017-08-31 17:59:18 +02001806 return devlink_dpipe_table_counter_enabled(devlink, table_name);
Arkadi Sharshevsky7cfcbc72017-08-24 08:40:08 +02001807}
1808
1809static void
1810mlxsw_sp_neigh_counter_alloc(struct mlxsw_sp *mlxsw_sp,
1811 struct mlxsw_sp_neigh_entry *neigh_entry)
1812{
Arkadi Sharshevsky1ed55742017-08-31 17:59:18 +02001813 if (!mlxsw_sp_neigh_counter_should_alloc(mlxsw_sp, neigh_entry))
Arkadi Sharshevsky7cfcbc72017-08-24 08:40:08 +02001814 return;
1815
1816 if (mlxsw_sp_flow_counter_alloc(mlxsw_sp, &neigh_entry->counter_index))
1817 return;
1818
1819 neigh_entry->counter_valid = true;
1820}
1821
1822static void
1823mlxsw_sp_neigh_counter_free(struct mlxsw_sp *mlxsw_sp,
1824 struct mlxsw_sp_neigh_entry *neigh_entry)
1825{
1826 if (!neigh_entry->counter_valid)
1827 return;
1828 mlxsw_sp_flow_counter_free(mlxsw_sp,
1829 neigh_entry->counter_index);
1830 neigh_entry->counter_valid = false;
1831}
1832
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001833static struct mlxsw_sp_neigh_entry *
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001834mlxsw_sp_neigh_entry_create(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001835{
1836 struct mlxsw_sp_neigh_entry *neigh_entry;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001837 struct mlxsw_sp_rif *rif;
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001838 int err;
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001839
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001840 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, n->dev);
1841 if (!rif)
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001842 return ERR_PTR(-EINVAL);
1843
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001844 neigh_entry = mlxsw_sp_neigh_entry_alloc(mlxsw_sp, n, rif->rif_index);
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001845 if (!neigh_entry)
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001846 return ERR_PTR(-ENOMEM);
1847
1848 err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
1849 if (err)
1850 goto err_neigh_entry_insert;
1851
Arkadi Sharshevsky7cfcbc72017-08-24 08:40:08 +02001852 mlxsw_sp_neigh_counter_alloc(mlxsw_sp, neigh_entry);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001853 list_add(&neigh_entry->rif_list_node, &rif->neigh_list);
Ido Schimmel9665b742017-02-08 11:16:42 +01001854
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001855 return neigh_entry;
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001856
1857err_neigh_entry_insert:
1858 mlxsw_sp_neigh_entry_free(neigh_entry);
1859 return ERR_PTR(err);
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001860}
1861
1862static void
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001863mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp *mlxsw_sp,
1864 struct mlxsw_sp_neigh_entry *neigh_entry)
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001865{
Ido Schimmel9665b742017-02-08 11:16:42 +01001866 list_del(&neigh_entry->rif_list_node);
Arkadi Sharshevsky7cfcbc72017-08-24 08:40:08 +02001867 mlxsw_sp_neigh_counter_free(mlxsw_sp, neigh_entry);
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001868 mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
1869 mlxsw_sp_neigh_entry_free(neigh_entry);
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001870}
1871
1872static struct mlxsw_sp_neigh_entry *
Jiri Pirko33b13412016-11-10 12:31:04 +01001873mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001874{
Jiri Pirko33b13412016-11-10 12:31:04 +01001875 struct mlxsw_sp_neigh_key key;
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001876
Jiri Pirko33b13412016-11-10 12:31:04 +01001877 key.n = n;
Ido Schimmel9011b672017-05-16 19:38:25 +02001878 return rhashtable_lookup_fast(&mlxsw_sp->router->neigh_ht,
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001879 &key, mlxsw_sp_neigh_ht_params);
1880}
1881
Yotam Gigic723c7352016-07-05 11:27:43 +02001882static void
1883mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp)
1884{
Arkadi Sharshevskya6c9b5d2017-07-18 10:10:18 +02001885 unsigned long interval;
Yotam Gigic723c7352016-07-05 11:27:43 +02001886
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +02001887#if IS_ENABLED(CONFIG_IPV6)
Arkadi Sharshevskya6c9b5d2017-07-18 10:10:18 +02001888 interval = min_t(unsigned long,
1889 NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME),
1890 NEIGH_VAR(&nd_tbl.parms, DELAY_PROBE_TIME));
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +02001891#else
1892 interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME);
1893#endif
Ido Schimmel9011b672017-05-16 19:38:25 +02001894 mlxsw_sp->router->neighs_update.interval = jiffies_to_msecs(interval);
Yotam Gigic723c7352016-07-05 11:27:43 +02001895}
1896
1897static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
1898 char *rauhtd_pl,
1899 int ent_index)
1900{
1901 struct net_device *dev;
1902 struct neighbour *n;
1903 __be32 dipn;
1904 u32 dip;
1905 u16 rif;
1906
1907 mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip);
1908
Ido Schimmel5f9efff2017-05-16 19:38:27 +02001909 if (!mlxsw_sp->router->rifs[rif]) {
Yotam Gigic723c7352016-07-05 11:27:43 +02001910 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
1911 return;
1912 }
1913
1914 dipn = htonl(dip);
Ido Schimmel5f9efff2017-05-16 19:38:27 +02001915 dev = mlxsw_sp->router->rifs[rif]->dev;
Yotam Gigic723c7352016-07-05 11:27:43 +02001916 n = neigh_lookup(&arp_tbl, &dipn, dev);
1917 if (!n) {
1918 netdev_err(dev, "Failed to find matching neighbour for IP=%pI4h\n",
1919 &dip);
1920 return;
1921 }
1922
1923 netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip);
1924 neigh_event_send(n, NULL);
1925 neigh_release(n);
1926}
1927
Ido Schimmeldf9a21f2017-08-15 09:10:33 +02001928#if IS_ENABLED(CONFIG_IPV6)
Arkadi Sharshevsky60f040c2017-07-18 10:10:17 +02001929static void mlxsw_sp_router_neigh_ent_ipv6_process(struct mlxsw_sp *mlxsw_sp,
1930 char *rauhtd_pl,
1931 int rec_index)
1932{
1933 struct net_device *dev;
1934 struct neighbour *n;
1935 struct in6_addr dip;
1936 u16 rif;
1937
1938 mlxsw_reg_rauhtd_ent_ipv6_unpack(rauhtd_pl, rec_index, &rif,
1939 (char *) &dip);
1940
1941 if (!mlxsw_sp->router->rifs[rif]) {
1942 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
1943 return;
1944 }
1945
1946 dev = mlxsw_sp->router->rifs[rif]->dev;
1947 n = neigh_lookup(&nd_tbl, &dip, dev);
1948 if (!n) {
1949 netdev_err(dev, "Failed to find matching neighbour for IP=%pI6c\n",
1950 &dip);
1951 return;
1952 }
1953
1954 netdev_dbg(dev, "Updating neighbour with IP=%pI6c\n", &dip);
1955 neigh_event_send(n, NULL);
1956 neigh_release(n);
1957}
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +02001958#else
1959static void mlxsw_sp_router_neigh_ent_ipv6_process(struct mlxsw_sp *mlxsw_sp,
1960 char *rauhtd_pl,
1961 int rec_index)
1962{
1963}
1964#endif
Arkadi Sharshevsky60f040c2017-07-18 10:10:17 +02001965
Yotam Gigic723c7352016-07-05 11:27:43 +02001966static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp,
1967 char *rauhtd_pl,
1968 int rec_index)
1969{
1970 u8 num_entries;
1971 int i;
1972
1973 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
1974 rec_index);
1975 /* Hardware starts counting at 0, so add 1. */
1976 num_entries++;
1977
1978 /* Each record consists of several neighbour entries. */
1979 for (i = 0; i < num_entries; i++) {
1980 int ent_index;
1981
1982 ent_index = rec_index * MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC + i;
1983 mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp, rauhtd_pl,
1984 ent_index);
1985 }
1986
1987}
1988
Arkadi Sharshevsky60f040c2017-07-18 10:10:17 +02001989static void mlxsw_sp_router_neigh_rec_ipv6_process(struct mlxsw_sp *mlxsw_sp,
1990 char *rauhtd_pl,
1991 int rec_index)
1992{
1993 /* One record contains one entry. */
1994 mlxsw_sp_router_neigh_ent_ipv6_process(mlxsw_sp, rauhtd_pl,
1995 rec_index);
1996}
1997
Yotam Gigic723c7352016-07-05 11:27:43 +02001998static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp,
1999 char *rauhtd_pl, int rec_index)
2000{
2001 switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, rec_index)) {
2002 case MLXSW_REG_RAUHTD_TYPE_IPV4:
2003 mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp, rauhtd_pl,
2004 rec_index);
2005 break;
2006 case MLXSW_REG_RAUHTD_TYPE_IPV6:
Arkadi Sharshevsky60f040c2017-07-18 10:10:17 +02002007 mlxsw_sp_router_neigh_rec_ipv6_process(mlxsw_sp, rauhtd_pl,
2008 rec_index);
Yotam Gigic723c7352016-07-05 11:27:43 +02002009 break;
2010 }
2011}
2012
Arkadi Sharshevsky42cdb332016-11-11 16:34:26 +01002013static bool mlxsw_sp_router_rauhtd_is_full(char *rauhtd_pl)
2014{
2015 u8 num_rec, last_rec_index, num_entries;
2016
2017 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
2018 last_rec_index = num_rec - 1;
2019
2020 if (num_rec < MLXSW_REG_RAUHTD_REC_MAX_NUM)
2021 return false;
2022 if (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, last_rec_index) ==
2023 MLXSW_REG_RAUHTD_TYPE_IPV6)
2024 return true;
2025
2026 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
2027 last_rec_index);
2028 if (++num_entries == MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC)
2029 return true;
2030 return false;
2031}
2032
Arkadi Sharshevsky60f040c2017-07-18 10:10:17 +02002033static int
2034__mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp,
2035 char *rauhtd_pl,
2036 enum mlxsw_reg_rauhtd_type type)
Yotam Gigic723c7352016-07-05 11:27:43 +02002037{
Arkadi Sharshevsky60f040c2017-07-18 10:10:17 +02002038 int i, num_rec;
2039 int err;
Yotam Gigic723c7352016-07-05 11:27:43 +02002040
2041 /* Make sure the neighbour's netdev isn't removed in the
2042 * process.
2043 */
2044 rtnl_lock();
2045 do {
Arkadi Sharshevsky60f040c2017-07-18 10:10:17 +02002046 mlxsw_reg_rauhtd_pack(rauhtd_pl, type);
Yotam Gigic723c7352016-07-05 11:27:43 +02002047 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd),
2048 rauhtd_pl);
2049 if (err) {
Petr Machata7ff176f2017-10-02 12:21:57 +02002050 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour table\n");
Yotam Gigic723c7352016-07-05 11:27:43 +02002051 break;
2052 }
2053 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
2054 for (i = 0; i < num_rec; i++)
2055 mlxsw_sp_router_neigh_rec_process(mlxsw_sp, rauhtd_pl,
2056 i);
Arkadi Sharshevsky42cdb332016-11-11 16:34:26 +01002057 } while (mlxsw_sp_router_rauhtd_is_full(rauhtd_pl));
Yotam Gigic723c7352016-07-05 11:27:43 +02002058 rtnl_unlock();
2059
Arkadi Sharshevsky60f040c2017-07-18 10:10:17 +02002060 return err;
2061}
2062
2063static int mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp)
2064{
2065 enum mlxsw_reg_rauhtd_type type;
2066 char *rauhtd_pl;
2067 int err;
2068
2069 rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL);
2070 if (!rauhtd_pl)
2071 return -ENOMEM;
2072
2073 type = MLXSW_REG_RAUHTD_TYPE_IPV4;
2074 err = __mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp, rauhtd_pl, type);
2075 if (err)
2076 goto out;
2077
2078 type = MLXSW_REG_RAUHTD_TYPE_IPV6;
2079 err = __mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp, rauhtd_pl, type);
2080out:
Yotam Gigic723c7352016-07-05 11:27:43 +02002081 kfree(rauhtd_pl);
Yotam Gigib2157142016-07-05 11:27:51 +02002082 return err;
2083}
2084
2085static void mlxsw_sp_router_neighs_update_nh(struct mlxsw_sp *mlxsw_sp)
2086{
2087 struct mlxsw_sp_neigh_entry *neigh_entry;
2088
2089 /* Take RTNL mutex here to prevent lists from changes */
2090 rtnl_lock();
Ido Schimmel9011b672017-05-16 19:38:25 +02002091 list_for_each_entry(neigh_entry, &mlxsw_sp->router->nexthop_neighs_list,
Ido Schimmel8a0b7272017-02-06 16:20:15 +01002092 nexthop_neighs_list_node)
Yotam Gigib2157142016-07-05 11:27:51 +02002093 /* If this neigh have nexthops, make the kernel think this neigh
2094 * is active regardless of the traffic.
2095 */
Ido Schimmel8a0b7272017-02-06 16:20:15 +01002096 neigh_event_send(neigh_entry->key.n, NULL);
Yotam Gigib2157142016-07-05 11:27:51 +02002097 rtnl_unlock();
2098}
2099
2100static void
2101mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp)
2102{
Ido Schimmel9011b672017-05-16 19:38:25 +02002103 unsigned long interval = mlxsw_sp->router->neighs_update.interval;
Yotam Gigib2157142016-07-05 11:27:51 +02002104
Ido Schimmel9011b672017-05-16 19:38:25 +02002105 mlxsw_core_schedule_dw(&mlxsw_sp->router->neighs_update.dw,
Yotam Gigib2157142016-07-05 11:27:51 +02002106 msecs_to_jiffies(interval));
2107}
2108
2109static void mlxsw_sp_router_neighs_update_work(struct work_struct *work)
2110{
Ido Schimmel9011b672017-05-16 19:38:25 +02002111 struct mlxsw_sp_router *router;
Yotam Gigib2157142016-07-05 11:27:51 +02002112 int err;
2113
Ido Schimmel9011b672017-05-16 19:38:25 +02002114 router = container_of(work, struct mlxsw_sp_router,
2115 neighs_update.dw.work);
2116 err = mlxsw_sp_router_neighs_update_rauhtd(router->mlxsw_sp);
Yotam Gigib2157142016-07-05 11:27:51 +02002117 if (err)
Ido Schimmel9011b672017-05-16 19:38:25 +02002118 dev_err(router->mlxsw_sp->bus_info->dev, "Could not update kernel for neigh activity");
Yotam Gigib2157142016-07-05 11:27:51 +02002119
Ido Schimmel9011b672017-05-16 19:38:25 +02002120 mlxsw_sp_router_neighs_update_nh(router->mlxsw_sp);
Yotam Gigib2157142016-07-05 11:27:51 +02002121
Ido Schimmel9011b672017-05-16 19:38:25 +02002122 mlxsw_sp_router_neighs_update_work_schedule(router->mlxsw_sp);
Yotam Gigic723c7352016-07-05 11:27:43 +02002123}
2124
Yotam Gigi0b2361d2016-07-05 11:27:52 +02002125static void mlxsw_sp_router_probe_unresolved_nexthops(struct work_struct *work)
2126{
2127 struct mlxsw_sp_neigh_entry *neigh_entry;
Ido Schimmel9011b672017-05-16 19:38:25 +02002128 struct mlxsw_sp_router *router;
Yotam Gigi0b2361d2016-07-05 11:27:52 +02002129
Ido Schimmel9011b672017-05-16 19:38:25 +02002130 router = container_of(work, struct mlxsw_sp_router,
2131 nexthop_probe_dw.work);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02002132 /* Iterate over nexthop neighbours, find those who are unresolved and
2133 * send arp on them. This solves the chicken-egg problem when
2134 * the nexthop wouldn't get offloaded until the neighbor is resolved
2135 * but it wouldn't get resolved ever in case traffic is flowing in HW
2136 * using different nexthop.
2137 *
2138 * Take RTNL mutex here to prevent lists from changes.
2139 */
2140 rtnl_lock();
Ido Schimmel9011b672017-05-16 19:38:25 +02002141 list_for_each_entry(neigh_entry, &router->nexthop_neighs_list,
Ido Schimmel8a0b7272017-02-06 16:20:15 +01002142 nexthop_neighs_list_node)
Ido Schimmel01b1aa32017-02-06 16:20:16 +01002143 if (!neigh_entry->connected)
Jiri Pirko33b13412016-11-10 12:31:04 +01002144 neigh_event_send(neigh_entry->key.n, NULL);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02002145 rtnl_unlock();
2146
Ido Schimmel9011b672017-05-16 19:38:25 +02002147 mlxsw_core_schedule_dw(&router->nexthop_probe_dw,
Yotam Gigi0b2361d2016-07-05 11:27:52 +02002148 MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL);
2149}
2150
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002151static void
2152mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
2153 struct mlxsw_sp_neigh_entry *neigh_entry,
2154 bool removing);
2155
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002156static enum mlxsw_reg_rauht_op mlxsw_sp_rauht_op(bool adding)
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002157{
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002158 return adding ? MLXSW_REG_RAUHT_OP_WRITE_ADD :
2159 MLXSW_REG_RAUHT_OP_WRITE_DELETE;
2160}
2161
2162static void
2163mlxsw_sp_router_neigh_entry_op4(struct mlxsw_sp *mlxsw_sp,
2164 struct mlxsw_sp_neigh_entry *neigh_entry,
2165 enum mlxsw_reg_rauht_op op)
2166{
Jiri Pirko33b13412016-11-10 12:31:04 +01002167 struct neighbour *n = neigh_entry->key.n;
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002168 u32 dip = ntohl(*((__be32 *) n->primary_key));
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002169 char rauht_pl[MLXSW_REG_RAUHT_LEN];
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002170
2171 mlxsw_reg_rauht_pack4(rauht_pl, op, neigh_entry->rif, neigh_entry->ha,
2172 dip);
Arkadi Sharshevsky7cfcbc72017-08-24 08:40:08 +02002173 if (neigh_entry->counter_valid)
2174 mlxsw_reg_rauht_pack_counter(rauht_pl,
2175 neigh_entry->counter_index);
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002176 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
2177}
2178
2179static void
Arkadi Sharshevskyd5eb89c2017-07-18 10:10:15 +02002180mlxsw_sp_router_neigh_entry_op6(struct mlxsw_sp *mlxsw_sp,
2181 struct mlxsw_sp_neigh_entry *neigh_entry,
2182 enum mlxsw_reg_rauht_op op)
2183{
2184 struct neighbour *n = neigh_entry->key.n;
2185 char rauht_pl[MLXSW_REG_RAUHT_LEN];
2186 const char *dip = n->primary_key;
2187
2188 mlxsw_reg_rauht_pack6(rauht_pl, op, neigh_entry->rif, neigh_entry->ha,
2189 dip);
Arkadi Sharshevsky7cfcbc72017-08-24 08:40:08 +02002190 if (neigh_entry->counter_valid)
2191 mlxsw_reg_rauht_pack_counter(rauht_pl,
2192 neigh_entry->counter_index);
Arkadi Sharshevskyd5eb89c2017-07-18 10:10:15 +02002193 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
2194}
2195
Arkadi Sharshevsky1d1056d82017-08-31 17:59:13 +02002196bool mlxsw_sp_neigh_ipv6_ignore(struct mlxsw_sp_neigh_entry *neigh_entry)
Arkadi Sharshevskyd5eb89c2017-07-18 10:10:15 +02002197{
Arkadi Sharshevsky1d1056d82017-08-31 17:59:13 +02002198 struct neighbour *n = neigh_entry->key.n;
2199
Arkadi Sharshevskyd5eb89c2017-07-18 10:10:15 +02002200 /* Packets with a link-local destination address are trapped
2201 * after LPM lookup and never reach the neighbour table, so
2202 * there is no need to program such neighbours to the device.
2203 */
2204 if (ipv6_addr_type((struct in6_addr *) &n->primary_key) &
2205 IPV6_ADDR_LINKLOCAL)
2206 return true;
2207 return false;
2208}
2209
2210static void
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002211mlxsw_sp_neigh_entry_update(struct mlxsw_sp *mlxsw_sp,
2212 struct mlxsw_sp_neigh_entry *neigh_entry,
2213 bool adding)
2214{
2215 if (!adding && !neigh_entry->connected)
2216 return;
2217 neigh_entry->connected = adding;
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +02002218 if (neigh_entry->key.n->tbl->family == AF_INET) {
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002219 mlxsw_sp_router_neigh_entry_op4(mlxsw_sp, neigh_entry,
2220 mlxsw_sp_rauht_op(adding));
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +02002221 } else if (neigh_entry->key.n->tbl->family == AF_INET6) {
Arkadi Sharshevsky1d1056d82017-08-31 17:59:13 +02002222 if (mlxsw_sp_neigh_ipv6_ignore(neigh_entry))
Arkadi Sharshevskyd5eb89c2017-07-18 10:10:15 +02002223 return;
2224 mlxsw_sp_router_neigh_entry_op6(mlxsw_sp, neigh_entry,
2225 mlxsw_sp_rauht_op(adding));
2226 } else {
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002227 WARN_ON_ONCE(1);
Arkadi Sharshevskyd5eb89c2017-07-18 10:10:15 +02002228 }
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002229}
2230
Arkadi Sharshevskya481d712017-08-24 08:40:10 +02002231void
2232mlxsw_sp_neigh_entry_counter_update(struct mlxsw_sp *mlxsw_sp,
2233 struct mlxsw_sp_neigh_entry *neigh_entry,
2234 bool adding)
2235{
2236 if (adding)
2237 mlxsw_sp_neigh_counter_alloc(mlxsw_sp, neigh_entry);
2238 else
2239 mlxsw_sp_neigh_counter_free(mlxsw_sp, neigh_entry);
2240 mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, true);
2241}
2242
Ido Schimmelceb88812017-11-02 17:14:07 +01002243struct mlxsw_sp_netevent_work {
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002244 struct work_struct work;
2245 struct mlxsw_sp *mlxsw_sp;
2246 struct neighbour *n;
2247};
2248
2249static void mlxsw_sp_router_neigh_event_work(struct work_struct *work)
2250{
Ido Schimmelceb88812017-11-02 17:14:07 +01002251 struct mlxsw_sp_netevent_work *net_work =
2252 container_of(work, struct mlxsw_sp_netevent_work, work);
2253 struct mlxsw_sp *mlxsw_sp = net_work->mlxsw_sp;
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002254 struct mlxsw_sp_neigh_entry *neigh_entry;
Ido Schimmelceb88812017-11-02 17:14:07 +01002255 struct neighbour *n = net_work->n;
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002256 unsigned char ha[ETH_ALEN];
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002257 bool entry_connected;
Ido Schimmel93a87e52016-12-23 09:32:49 +01002258 u8 nud_state, dead;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002259
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002260 /* If these parameters are changed after we release the lock,
2261 * then we are guaranteed to receive another event letting us
2262 * know about it.
2263 */
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002264 read_lock_bh(&n->lock);
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002265 memcpy(ha, n->ha, ETH_ALEN);
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002266 nud_state = n->nud_state;
Ido Schimmel93a87e52016-12-23 09:32:49 +01002267 dead = n->dead;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002268 read_unlock_bh(&n->lock);
2269
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002270 rtnl_lock();
Ido Schimmel93a87e52016-12-23 09:32:49 +01002271 entry_connected = nud_state & NUD_VALID && !dead;
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002272 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
2273 if (!entry_connected && !neigh_entry)
2274 goto out;
2275 if (!neigh_entry) {
2276 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
2277 if (IS_ERR(neigh_entry))
2278 goto out;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002279 }
2280
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002281 memcpy(neigh_entry->ha, ha, ETH_ALEN);
2282 mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, entry_connected);
2283 mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, !entry_connected);
2284
2285 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
2286 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
2287
2288out:
2289 rtnl_unlock();
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002290 neigh_release(n);
Ido Schimmelceb88812017-11-02 17:14:07 +01002291 kfree(net_work);
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002292}
2293
Ido Schimmel28678f02017-11-02 17:14:10 +01002294static int mlxsw_sp_mp_hash_init(struct mlxsw_sp *mlxsw_sp);
2295
2296static void mlxsw_sp_router_mp_hash_event_work(struct work_struct *work)
2297{
2298 struct mlxsw_sp_netevent_work *net_work =
2299 container_of(work, struct mlxsw_sp_netevent_work, work);
2300 struct mlxsw_sp *mlxsw_sp = net_work->mlxsw_sp;
2301
2302 mlxsw_sp_mp_hash_init(mlxsw_sp);
2303 kfree(net_work);
2304}
2305
2306static int mlxsw_sp_router_netevent_event(struct notifier_block *nb,
Ido Schimmel48fac882017-11-02 17:14:06 +01002307 unsigned long event, void *ptr)
Yotam Gigic723c7352016-07-05 11:27:43 +02002308{
Ido Schimmelceb88812017-11-02 17:14:07 +01002309 struct mlxsw_sp_netevent_work *net_work;
Yotam Gigic723c7352016-07-05 11:27:43 +02002310 struct mlxsw_sp_port *mlxsw_sp_port;
Ido Schimmel28678f02017-11-02 17:14:10 +01002311 struct mlxsw_sp_router *router;
Yotam Gigic723c7352016-07-05 11:27:43 +02002312 struct mlxsw_sp *mlxsw_sp;
2313 unsigned long interval;
2314 struct neigh_parms *p;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002315 struct neighbour *n;
Ido Schimmel28678f02017-11-02 17:14:10 +01002316 struct net *net;
Yotam Gigic723c7352016-07-05 11:27:43 +02002317
2318 switch (event) {
2319 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
2320 p = ptr;
2321
2322 /* We don't care about changes in the default table. */
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +02002323 if (!p->dev || (p->tbl->family != AF_INET &&
2324 p->tbl->family != AF_INET6))
Yotam Gigic723c7352016-07-05 11:27:43 +02002325 return NOTIFY_DONE;
2326
2327 /* We are in atomic context and can't take RTNL mutex,
2328 * so use RCU variant to walk the device chain.
2329 */
2330 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev);
2331 if (!mlxsw_sp_port)
2332 return NOTIFY_DONE;
2333
2334 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2335 interval = jiffies_to_msecs(NEIGH_VAR(p, DELAY_PROBE_TIME));
Ido Schimmel9011b672017-05-16 19:38:25 +02002336 mlxsw_sp->router->neighs_update.interval = interval;
Yotam Gigic723c7352016-07-05 11:27:43 +02002337
2338 mlxsw_sp_port_dev_put(mlxsw_sp_port);
2339 break;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002340 case NETEVENT_NEIGH_UPDATE:
2341 n = ptr;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002342
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +02002343 if (n->tbl->family != AF_INET && n->tbl->family != AF_INET6)
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002344 return NOTIFY_DONE;
2345
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002346 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(n->dev);
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002347 if (!mlxsw_sp_port)
2348 return NOTIFY_DONE;
2349
Ido Schimmelceb88812017-11-02 17:14:07 +01002350 net_work = kzalloc(sizeof(*net_work), GFP_ATOMIC);
2351 if (!net_work) {
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002352 mlxsw_sp_port_dev_put(mlxsw_sp_port);
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002353 return NOTIFY_BAD;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002354 }
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002355
Ido Schimmelceb88812017-11-02 17:14:07 +01002356 INIT_WORK(&net_work->work, mlxsw_sp_router_neigh_event_work);
2357 net_work->mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2358 net_work->n = n;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002359
2360 /* Take a reference to ensure the neighbour won't be
2361 * destructed until we drop the reference in delayed
2362 * work.
2363 */
2364 neigh_clone(n);
Ido Schimmelceb88812017-11-02 17:14:07 +01002365 mlxsw_core_schedule_work(&net_work->work);
Ido Schimmel5c8802f2017-02-06 16:20:13 +01002366 mlxsw_sp_port_dev_put(mlxsw_sp_port);
Yotam Gigia6bf9e92016-07-05 11:27:44 +02002367 break;
Ido Schimmel28678f02017-11-02 17:14:10 +01002368 case NETEVENT_MULTIPATH_HASH_UPDATE:
2369 net = ptr;
2370
2371 if (!net_eq(net, &init_net))
2372 return NOTIFY_DONE;
2373
2374 net_work = kzalloc(sizeof(*net_work), GFP_ATOMIC);
2375 if (!net_work)
2376 return NOTIFY_BAD;
2377
2378 router = container_of(nb, struct mlxsw_sp_router, netevent_nb);
2379 INIT_WORK(&net_work->work, mlxsw_sp_router_mp_hash_event_work);
2380 net_work->mlxsw_sp = router->mlxsw_sp;
2381 mlxsw_core_schedule_work(&net_work->work);
2382 break;
Yotam Gigic723c7352016-07-05 11:27:43 +02002383 }
2384
2385 return NOTIFY_DONE;
2386}
2387
Jiri Pirko6cf3c972016-07-05 11:27:39 +02002388static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp)
2389{
Yotam Gigic723c7352016-07-05 11:27:43 +02002390 int err;
2391
Ido Schimmel9011b672017-05-16 19:38:25 +02002392 err = rhashtable_init(&mlxsw_sp->router->neigh_ht,
Yotam Gigic723c7352016-07-05 11:27:43 +02002393 &mlxsw_sp_neigh_ht_params);
2394 if (err)
2395 return err;
2396
2397 /* Initialize the polling interval according to the default
2398 * table.
2399 */
2400 mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp);
2401
Yotam Gigi0b2361d2016-07-05 11:27:52 +02002402 /* Create the delayed works for the activity_update */
Ido Schimmel9011b672017-05-16 19:38:25 +02002403 INIT_DELAYED_WORK(&mlxsw_sp->router->neighs_update.dw,
Yotam Gigic723c7352016-07-05 11:27:43 +02002404 mlxsw_sp_router_neighs_update_work);
Ido Schimmel9011b672017-05-16 19:38:25 +02002405 INIT_DELAYED_WORK(&mlxsw_sp->router->nexthop_probe_dw,
Yotam Gigi0b2361d2016-07-05 11:27:52 +02002406 mlxsw_sp_router_probe_unresolved_nexthops);
Ido Schimmel9011b672017-05-16 19:38:25 +02002407 mlxsw_core_schedule_dw(&mlxsw_sp->router->neighs_update.dw, 0);
2408 mlxsw_core_schedule_dw(&mlxsw_sp->router->nexthop_probe_dw, 0);
Yotam Gigic723c7352016-07-05 11:27:43 +02002409 return 0;
Jiri Pirko6cf3c972016-07-05 11:27:39 +02002410}
2411
2412static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp)
2413{
Ido Schimmel9011b672017-05-16 19:38:25 +02002414 cancel_delayed_work_sync(&mlxsw_sp->router->neighs_update.dw);
2415 cancel_delayed_work_sync(&mlxsw_sp->router->nexthop_probe_dw);
2416 rhashtable_destroy(&mlxsw_sp->router->neigh_ht);
Jiri Pirko6cf3c972016-07-05 11:27:39 +02002417}
2418
Ido Schimmel9665b742017-02-08 11:16:42 +01002419static void mlxsw_sp_neigh_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002420 struct mlxsw_sp_rif *rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01002421{
2422 struct mlxsw_sp_neigh_entry *neigh_entry, *tmp;
2423
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002424 list_for_each_entry_safe(neigh_entry, tmp, &rif->neigh_list,
Ido Schimmel4a3c67a2017-07-21 20:31:38 +02002425 rif_list_node) {
2426 mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, false);
Ido Schimmel9665b742017-02-08 11:16:42 +01002427 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
Ido Schimmel4a3c67a2017-07-21 20:31:38 +02002428 }
Ido Schimmel9665b742017-02-08 11:16:42 +01002429}
2430
Petr Machata35225e42017-09-02 23:49:22 +02002431enum mlxsw_sp_nexthop_type {
2432 MLXSW_SP_NEXTHOP_TYPE_ETH,
Petr Machata1012b9a2017-09-02 23:49:23 +02002433 MLXSW_SP_NEXTHOP_TYPE_IPIP,
Petr Machata35225e42017-09-02 23:49:22 +02002434};
2435
Ido Schimmelc53b8e12017-02-08 11:16:30 +01002436struct mlxsw_sp_nexthop_key {
2437 struct fib_nh *fib_nh;
2438};
2439
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002440struct mlxsw_sp_nexthop {
2441 struct list_head neigh_list_node; /* member of neigh entry list */
Ido Schimmel9665b742017-02-08 11:16:42 +01002442 struct list_head rif_list_node;
Arkadi Sharshevskydbe45982017-09-25 10:32:23 +02002443 struct list_head router_list_node;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002444 struct mlxsw_sp_nexthop_group *nh_grp; /* pointer back to the group
2445 * this belongs to
2446 */
Ido Schimmelc53b8e12017-02-08 11:16:30 +01002447 struct rhash_head ht_node;
2448 struct mlxsw_sp_nexthop_key key;
Ido Schimmel58adf2c2017-07-18 10:10:19 +02002449 unsigned char gw_addr[sizeof(struct in6_addr)];
Arkadi Sharshevskye6f3b372017-08-14 21:09:20 +02002450 int ifindex;
Ido Schimmel408bd942017-10-22 23:11:46 +02002451 int nh_weight;
Ido Schimmeleb789982017-10-22 23:11:48 +02002452 int norm_nh_weight;
2453 int num_adj_entries;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002454 struct mlxsw_sp_rif *rif;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002455 u8 should_offload:1, /* set indicates this neigh is connected and
2456 * should be put to KVD linear area of this group.
2457 */
2458 offloaded:1, /* set in case the neigh is actually put into
2459 * KVD linear area of this group.
2460 */
2461 update:1; /* set indicates that MAC of this neigh should be
2462 * updated in HW
2463 */
Petr Machata35225e42017-09-02 23:49:22 +02002464 enum mlxsw_sp_nexthop_type type;
2465 union {
2466 struct mlxsw_sp_neigh_entry *neigh_entry;
Petr Machata1012b9a2017-09-02 23:49:23 +02002467 struct mlxsw_sp_ipip_entry *ipip_entry;
Petr Machata35225e42017-09-02 23:49:22 +02002468 };
Arkadi Sharshevskya5390272017-09-25 10:32:28 +02002469 unsigned int counter_index;
2470 bool counter_valid;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002471};
2472
2473struct mlxsw_sp_nexthop_group {
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02002474 void *priv;
Ido Schimmele9ad5e72017-02-08 11:16:29 +01002475 struct rhash_head ht_node;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002476 struct list_head fib_list; /* list of fib entries that use this group */
Ido Schimmel58adf2c2017-07-18 10:10:19 +02002477 struct neigh_table *neigh_tbl;
Ido Schimmelb3e8d1e2017-02-08 11:16:32 +01002478 u8 adj_index_valid:1,
2479 gateway:1; /* routes using the group use a gateway */
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002480 u32 adj_index;
2481 u16 ecmp_size;
2482 u16 count;
Ido Schimmeleb789982017-10-22 23:11:48 +02002483 int sum_norm_weight;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002484 struct mlxsw_sp_nexthop nexthops[0];
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002485#define nh_rif nexthops[0].rif
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002486};
2487
Arkadi Sharshevsky427e6522017-09-25 10:32:30 +02002488void mlxsw_sp_nexthop_counter_alloc(struct mlxsw_sp *mlxsw_sp,
2489 struct mlxsw_sp_nexthop *nh)
Arkadi Sharshevskya5390272017-09-25 10:32:28 +02002490{
2491 struct devlink *devlink;
2492
2493 devlink = priv_to_devlink(mlxsw_sp->core);
2494 if (!devlink_dpipe_table_counter_enabled(devlink,
2495 MLXSW_SP_DPIPE_TABLE_NAME_ADJ))
2496 return;
2497
2498 if (mlxsw_sp_flow_counter_alloc(mlxsw_sp, &nh->counter_index))
2499 return;
2500
2501 nh->counter_valid = true;
2502}
2503
Arkadi Sharshevsky427e6522017-09-25 10:32:30 +02002504void mlxsw_sp_nexthop_counter_free(struct mlxsw_sp *mlxsw_sp,
2505 struct mlxsw_sp_nexthop *nh)
Arkadi Sharshevskya5390272017-09-25 10:32:28 +02002506{
2507 if (!nh->counter_valid)
2508 return;
2509 mlxsw_sp_flow_counter_free(mlxsw_sp, nh->counter_index);
2510 nh->counter_valid = false;
2511}
2512
2513int mlxsw_sp_nexthop_counter_get(struct mlxsw_sp *mlxsw_sp,
2514 struct mlxsw_sp_nexthop *nh, u64 *p_counter)
2515{
2516 if (!nh->counter_valid)
2517 return -EINVAL;
2518
2519 return mlxsw_sp_flow_counter_get(mlxsw_sp, nh->counter_index,
2520 p_counter, NULL);
2521}
2522
Arkadi Sharshevskyc556cd22017-09-25 10:32:25 +02002523struct mlxsw_sp_nexthop *mlxsw_sp_nexthop_next(struct mlxsw_sp_router *router,
2524 struct mlxsw_sp_nexthop *nh)
2525{
2526 if (!nh) {
2527 if (list_empty(&router->nexthop_list))
2528 return NULL;
2529 else
2530 return list_first_entry(&router->nexthop_list,
2531 typeof(*nh), router_list_node);
2532 }
2533 if (list_is_last(&nh->router_list_node, &router->nexthop_list))
2534 return NULL;
2535 return list_next_entry(nh, router_list_node);
2536}
2537
2538bool mlxsw_sp_nexthop_offload(struct mlxsw_sp_nexthop *nh)
2539{
2540 return nh->offloaded;
2541}
2542
2543unsigned char *mlxsw_sp_nexthop_ha(struct mlxsw_sp_nexthop *nh)
2544{
2545 if (!nh->offloaded)
2546 return NULL;
2547 return nh->neigh_entry->ha;
2548}
2549
2550int mlxsw_sp_nexthop_indexes(struct mlxsw_sp_nexthop *nh, u32 *p_adj_index,
Ido Schimmele69cd9d2017-10-22 23:11:43 +02002551 u32 *p_adj_size, u32 *p_adj_hash_index)
Arkadi Sharshevskyc556cd22017-09-25 10:32:25 +02002552{
2553 struct mlxsw_sp_nexthop_group *nh_grp = nh->nh_grp;
2554 u32 adj_hash_index = 0;
2555 int i;
2556
2557 if (!nh->offloaded || !nh_grp->adj_index_valid)
2558 return -EINVAL;
2559
2560 *p_adj_index = nh_grp->adj_index;
Ido Schimmele69cd9d2017-10-22 23:11:43 +02002561 *p_adj_size = nh_grp->ecmp_size;
Arkadi Sharshevskyc556cd22017-09-25 10:32:25 +02002562
2563 for (i = 0; i < nh_grp->count; i++) {
2564 struct mlxsw_sp_nexthop *nh_iter = &nh_grp->nexthops[i];
2565
2566 if (nh_iter == nh)
2567 break;
2568 if (nh_iter->offloaded)
Ido Schimmeleb789982017-10-22 23:11:48 +02002569 adj_hash_index += nh_iter->num_adj_entries;
Arkadi Sharshevskyc556cd22017-09-25 10:32:25 +02002570 }
2571
2572 *p_adj_hash_index = adj_hash_index;
2573 return 0;
2574}
2575
2576struct mlxsw_sp_rif *mlxsw_sp_nexthop_rif(struct mlxsw_sp_nexthop *nh)
2577{
2578 return nh->rif;
2579}
2580
2581bool mlxsw_sp_nexthop_group_has_ipip(struct mlxsw_sp_nexthop *nh)
2582{
2583 struct mlxsw_sp_nexthop_group *nh_grp = nh->nh_grp;
2584 int i;
2585
2586 for (i = 0; i < nh_grp->count; i++) {
2587 struct mlxsw_sp_nexthop *nh_iter = &nh_grp->nexthops[i];
2588
2589 if (nh_iter->type == MLXSW_SP_NEXTHOP_TYPE_IPIP)
2590 return true;
2591 }
2592 return false;
2593}
2594
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02002595static struct fib_info *
2596mlxsw_sp_nexthop4_group_fi(const struct mlxsw_sp_nexthop_group *nh_grp)
2597{
2598 return nh_grp->priv;
2599}
2600
2601struct mlxsw_sp_nexthop_group_cmp_arg {
Arkadi Sharshevskye6f3b372017-08-14 21:09:20 +02002602 enum mlxsw_sp_l3proto proto;
2603 union {
2604 struct fib_info *fi;
2605 struct mlxsw_sp_fib6_entry *fib6_entry;
2606 };
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02002607};
2608
Arkadi Sharshevskye6f3b372017-08-14 21:09:20 +02002609static bool
2610mlxsw_sp_nexthop6_group_has_nexthop(const struct mlxsw_sp_nexthop_group *nh_grp,
2611 const struct in6_addr *gw, int ifindex)
2612{
2613 int i;
2614
2615 for (i = 0; i < nh_grp->count; i++) {
2616 const struct mlxsw_sp_nexthop *nh;
2617
2618 nh = &nh_grp->nexthops[i];
2619 if (nh->ifindex == ifindex &&
2620 ipv6_addr_equal(gw, (struct in6_addr *) nh->gw_addr))
2621 return true;
2622 }
2623
2624 return false;
2625}
2626
2627static bool
2628mlxsw_sp_nexthop6_group_cmp(const struct mlxsw_sp_nexthop_group *nh_grp,
2629 const struct mlxsw_sp_fib6_entry *fib6_entry)
2630{
2631 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
2632
2633 if (nh_grp->count != fib6_entry->nrt6)
2634 return false;
2635
2636 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
2637 struct in6_addr *gw;
2638 int ifindex;
2639
2640 ifindex = mlxsw_sp_rt6->rt->dst.dev->ifindex;
2641 gw = &mlxsw_sp_rt6->rt->rt6i_gateway;
2642 if (!mlxsw_sp_nexthop6_group_has_nexthop(nh_grp, gw, ifindex))
2643 return false;
2644 }
2645
2646 return true;
2647}
2648
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02002649static int
2650mlxsw_sp_nexthop_group_cmp(struct rhashtable_compare_arg *arg, const void *ptr)
2651{
2652 const struct mlxsw_sp_nexthop_group_cmp_arg *cmp_arg = arg->key;
2653 const struct mlxsw_sp_nexthop_group *nh_grp = ptr;
2654
Arkadi Sharshevskye6f3b372017-08-14 21:09:20 +02002655 switch (cmp_arg->proto) {
2656 case MLXSW_SP_L3_PROTO_IPV4:
2657 return cmp_arg->fi != mlxsw_sp_nexthop4_group_fi(nh_grp);
2658 case MLXSW_SP_L3_PROTO_IPV6:
2659 return !mlxsw_sp_nexthop6_group_cmp(nh_grp,
2660 cmp_arg->fib6_entry);
2661 default:
2662 WARN_ON(1);
2663 return 1;
2664 }
2665}
2666
2667static int
2668mlxsw_sp_nexthop_group_type(const struct mlxsw_sp_nexthop_group *nh_grp)
2669{
2670 return nh_grp->neigh_tbl->family;
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02002671}
2672
2673static u32 mlxsw_sp_nexthop_group_hash_obj(const void *data, u32 len, u32 seed)
2674{
2675 const struct mlxsw_sp_nexthop_group *nh_grp = data;
Arkadi Sharshevskye6f3b372017-08-14 21:09:20 +02002676 const struct mlxsw_sp_nexthop *nh;
2677 struct fib_info *fi;
2678 unsigned int val;
2679 int i;
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02002680
Arkadi Sharshevskye6f3b372017-08-14 21:09:20 +02002681 switch (mlxsw_sp_nexthop_group_type(nh_grp)) {
2682 case AF_INET:
2683 fi = mlxsw_sp_nexthop4_group_fi(nh_grp);
2684 return jhash(&fi, sizeof(fi), seed);
2685 case AF_INET6:
2686 val = nh_grp->count;
2687 for (i = 0; i < nh_grp->count; i++) {
2688 nh = &nh_grp->nexthops[i];
2689 val ^= nh->ifindex;
2690 }
2691 return jhash(&val, sizeof(val), seed);
2692 default:
2693 WARN_ON(1);
2694 return 0;
2695 }
2696}
2697
2698static u32
2699mlxsw_sp_nexthop6_group_hash(struct mlxsw_sp_fib6_entry *fib6_entry, u32 seed)
2700{
2701 unsigned int val = fib6_entry->nrt6;
2702 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
2703 struct net_device *dev;
2704
2705 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
2706 dev = mlxsw_sp_rt6->rt->dst.dev;
2707 val ^= dev->ifindex;
2708 }
2709
2710 return jhash(&val, sizeof(val), seed);
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02002711}
2712
2713static u32
2714mlxsw_sp_nexthop_group_hash(const void *data, u32 len, u32 seed)
2715{
2716 const struct mlxsw_sp_nexthop_group_cmp_arg *cmp_arg = data;
2717
Arkadi Sharshevskye6f3b372017-08-14 21:09:20 +02002718 switch (cmp_arg->proto) {
2719 case MLXSW_SP_L3_PROTO_IPV4:
2720 return jhash(&cmp_arg->fi, sizeof(cmp_arg->fi), seed);
2721 case MLXSW_SP_L3_PROTO_IPV6:
2722 return mlxsw_sp_nexthop6_group_hash(cmp_arg->fib6_entry, seed);
2723 default:
2724 WARN_ON(1);
2725 return 0;
2726 }
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02002727}
2728
Ido Schimmele9ad5e72017-02-08 11:16:29 +01002729static const struct rhashtable_params mlxsw_sp_nexthop_group_ht_params = {
Ido Schimmele9ad5e72017-02-08 11:16:29 +01002730 .head_offset = offsetof(struct mlxsw_sp_nexthop_group, ht_node),
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02002731 .hashfn = mlxsw_sp_nexthop_group_hash,
2732 .obj_hashfn = mlxsw_sp_nexthop_group_hash_obj,
2733 .obj_cmpfn = mlxsw_sp_nexthop_group_cmp,
Ido Schimmele9ad5e72017-02-08 11:16:29 +01002734};
2735
2736static int mlxsw_sp_nexthop_group_insert(struct mlxsw_sp *mlxsw_sp,
2737 struct mlxsw_sp_nexthop_group *nh_grp)
2738{
Arkadi Sharshevskye6f3b372017-08-14 21:09:20 +02002739 if (mlxsw_sp_nexthop_group_type(nh_grp) == AF_INET6 &&
2740 !nh_grp->gateway)
2741 return 0;
2742
Ido Schimmel9011b672017-05-16 19:38:25 +02002743 return rhashtable_insert_fast(&mlxsw_sp->router->nexthop_group_ht,
Ido Schimmele9ad5e72017-02-08 11:16:29 +01002744 &nh_grp->ht_node,
2745 mlxsw_sp_nexthop_group_ht_params);
2746}
2747
2748static void mlxsw_sp_nexthop_group_remove(struct mlxsw_sp *mlxsw_sp,
2749 struct mlxsw_sp_nexthop_group *nh_grp)
2750{
Arkadi Sharshevskye6f3b372017-08-14 21:09:20 +02002751 if (mlxsw_sp_nexthop_group_type(nh_grp) == AF_INET6 &&
2752 !nh_grp->gateway)
2753 return;
2754
Ido Schimmel9011b672017-05-16 19:38:25 +02002755 rhashtable_remove_fast(&mlxsw_sp->router->nexthop_group_ht,
Ido Schimmele9ad5e72017-02-08 11:16:29 +01002756 &nh_grp->ht_node,
2757 mlxsw_sp_nexthop_group_ht_params);
2758}
2759
2760static struct mlxsw_sp_nexthop_group *
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02002761mlxsw_sp_nexthop4_group_lookup(struct mlxsw_sp *mlxsw_sp,
2762 struct fib_info *fi)
Ido Schimmele9ad5e72017-02-08 11:16:29 +01002763{
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02002764 struct mlxsw_sp_nexthop_group_cmp_arg cmp_arg;
2765
Arkadi Sharshevskye6f3b372017-08-14 21:09:20 +02002766 cmp_arg.proto = MLXSW_SP_L3_PROTO_IPV4;
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02002767 cmp_arg.fi = fi;
2768 return rhashtable_lookup_fast(&mlxsw_sp->router->nexthop_group_ht,
2769 &cmp_arg,
Ido Schimmele9ad5e72017-02-08 11:16:29 +01002770 mlxsw_sp_nexthop_group_ht_params);
2771}
2772
Arkadi Sharshevskye6f3b372017-08-14 21:09:20 +02002773static struct mlxsw_sp_nexthop_group *
2774mlxsw_sp_nexthop6_group_lookup(struct mlxsw_sp *mlxsw_sp,
2775 struct mlxsw_sp_fib6_entry *fib6_entry)
2776{
2777 struct mlxsw_sp_nexthop_group_cmp_arg cmp_arg;
2778
2779 cmp_arg.proto = MLXSW_SP_L3_PROTO_IPV6;
2780 cmp_arg.fib6_entry = fib6_entry;
2781 return rhashtable_lookup_fast(&mlxsw_sp->router->nexthop_group_ht,
2782 &cmp_arg,
2783 mlxsw_sp_nexthop_group_ht_params);
2784}
2785
Ido Schimmelc53b8e12017-02-08 11:16:30 +01002786static const struct rhashtable_params mlxsw_sp_nexthop_ht_params = {
2787 .key_offset = offsetof(struct mlxsw_sp_nexthop, key),
2788 .head_offset = offsetof(struct mlxsw_sp_nexthop, ht_node),
2789 .key_len = sizeof(struct mlxsw_sp_nexthop_key),
2790};
2791
2792static int mlxsw_sp_nexthop_insert(struct mlxsw_sp *mlxsw_sp,
2793 struct mlxsw_sp_nexthop *nh)
2794{
Ido Schimmel9011b672017-05-16 19:38:25 +02002795 return rhashtable_insert_fast(&mlxsw_sp->router->nexthop_ht,
Ido Schimmelc53b8e12017-02-08 11:16:30 +01002796 &nh->ht_node, mlxsw_sp_nexthop_ht_params);
2797}
2798
2799static void mlxsw_sp_nexthop_remove(struct mlxsw_sp *mlxsw_sp,
2800 struct mlxsw_sp_nexthop *nh)
2801{
Ido Schimmel9011b672017-05-16 19:38:25 +02002802 rhashtable_remove_fast(&mlxsw_sp->router->nexthop_ht, &nh->ht_node,
Ido Schimmelc53b8e12017-02-08 11:16:30 +01002803 mlxsw_sp_nexthop_ht_params);
2804}
2805
Ido Schimmelad178c82017-02-08 11:16:40 +01002806static struct mlxsw_sp_nexthop *
2807mlxsw_sp_nexthop_lookup(struct mlxsw_sp *mlxsw_sp,
2808 struct mlxsw_sp_nexthop_key key)
2809{
Ido Schimmel9011b672017-05-16 19:38:25 +02002810 return rhashtable_lookup_fast(&mlxsw_sp->router->nexthop_ht, &key,
Ido Schimmelad178c82017-02-08 11:16:40 +01002811 mlxsw_sp_nexthop_ht_params);
2812}
2813
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002814static int mlxsw_sp_adj_index_mass_update_vr(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel76610eb2017-03-10 08:53:41 +01002815 const struct mlxsw_sp_fib *fib,
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002816 u32 adj_index, u16 ecmp_size,
2817 u32 new_adj_index,
2818 u16 new_ecmp_size)
2819{
2820 char raleu_pl[MLXSW_REG_RALEU_LEN];
2821
Ido Schimmel1a9234e662016-09-19 08:29:26 +02002822 mlxsw_reg_raleu_pack(raleu_pl,
Ido Schimmel76610eb2017-03-10 08:53:41 +01002823 (enum mlxsw_reg_ralxx_protocol) fib->proto,
2824 fib->vr->id, adj_index, ecmp_size, new_adj_index,
Ido Schimmel1a9234e662016-09-19 08:29:26 +02002825 new_ecmp_size);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002826 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raleu), raleu_pl);
2827}
2828
2829static int mlxsw_sp_adj_index_mass_update(struct mlxsw_sp *mlxsw_sp,
2830 struct mlxsw_sp_nexthop_group *nh_grp,
2831 u32 old_adj_index, u16 old_ecmp_size)
2832{
2833 struct mlxsw_sp_fib_entry *fib_entry;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002834 struct mlxsw_sp_fib *fib = NULL;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002835 int err;
2836
2837 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
Ido Schimmel76610eb2017-03-10 08:53:41 +01002838 if (fib == fib_entry->fib_node->fib)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002839 continue;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002840 fib = fib_entry->fib_node->fib;
2841 err = mlxsw_sp_adj_index_mass_update_vr(mlxsw_sp, fib,
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002842 old_adj_index,
2843 old_ecmp_size,
2844 nh_grp->adj_index,
2845 nh_grp->ecmp_size);
2846 if (err)
2847 return err;
2848 }
2849 return 0;
2850}
2851
Ido Schimmeleb789982017-10-22 23:11:48 +02002852static int __mlxsw_sp_nexthop_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
2853 struct mlxsw_sp_nexthop *nh)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002854{
2855 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
2856 char ratr_pl[MLXSW_REG_RATR_LEN];
2857
2858 mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY,
Petr Machata89e41982017-09-02 23:49:15 +02002859 true, MLXSW_REG_RATR_TYPE_ETHERNET,
2860 adj_index, neigh_entry->rif);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002861 mlxsw_reg_ratr_eth_entry_pack(ratr_pl, neigh_entry->ha);
Arkadi Sharshevskya5390272017-09-25 10:32:28 +02002862 if (nh->counter_valid)
2863 mlxsw_reg_ratr_counter_pack(ratr_pl, nh->counter_index, true);
2864 else
2865 mlxsw_reg_ratr_counter_pack(ratr_pl, 0, false);
2866
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002867 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
2868}
2869
Ido Schimmeleb789982017-10-22 23:11:48 +02002870int mlxsw_sp_nexthop_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
2871 struct mlxsw_sp_nexthop *nh)
2872{
2873 int i;
2874
2875 for (i = 0; i < nh->num_adj_entries; i++) {
2876 int err;
2877
2878 err = __mlxsw_sp_nexthop_update(mlxsw_sp, adj_index + i, nh);
2879 if (err)
2880 return err;
2881 }
2882
2883 return 0;
2884}
2885
2886static int __mlxsw_sp_nexthop_ipip_update(struct mlxsw_sp *mlxsw_sp,
2887 u32 adj_index,
2888 struct mlxsw_sp_nexthop *nh)
Petr Machata1012b9a2017-09-02 23:49:23 +02002889{
2890 const struct mlxsw_sp_ipip_ops *ipip_ops;
2891
2892 ipip_ops = mlxsw_sp->router->ipip_ops_arr[nh->ipip_entry->ipipt];
2893 return ipip_ops->nexthop_update(mlxsw_sp, adj_index, nh->ipip_entry);
2894}
2895
Ido Schimmeleb789982017-10-22 23:11:48 +02002896static int mlxsw_sp_nexthop_ipip_update(struct mlxsw_sp *mlxsw_sp,
2897 u32 adj_index,
2898 struct mlxsw_sp_nexthop *nh)
2899{
2900 int i;
2901
2902 for (i = 0; i < nh->num_adj_entries; i++) {
2903 int err;
2904
2905 err = __mlxsw_sp_nexthop_ipip_update(mlxsw_sp, adj_index + i,
2906 nh);
2907 if (err)
2908 return err;
2909 }
2910
2911 return 0;
2912}
2913
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002914static int
Petr Machata35225e42017-09-02 23:49:22 +02002915mlxsw_sp_nexthop_group_update(struct mlxsw_sp *mlxsw_sp,
2916 struct mlxsw_sp_nexthop_group *nh_grp,
2917 bool reallocate)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002918{
2919 u32 adj_index = nh_grp->adj_index; /* base */
2920 struct mlxsw_sp_nexthop *nh;
2921 int i;
2922 int err;
2923
2924 for (i = 0; i < nh_grp->count; i++) {
2925 nh = &nh_grp->nexthops[i];
2926
2927 if (!nh->should_offload) {
2928 nh->offloaded = 0;
2929 continue;
2930 }
2931
Ido Schimmela59b7e02017-01-23 11:11:42 +01002932 if (nh->update || reallocate) {
Petr Machata35225e42017-09-02 23:49:22 +02002933 switch (nh->type) {
2934 case MLXSW_SP_NEXTHOP_TYPE_ETH:
Arkadi Sharshevskya5390272017-09-25 10:32:28 +02002935 err = mlxsw_sp_nexthop_update
Petr Machata35225e42017-09-02 23:49:22 +02002936 (mlxsw_sp, adj_index, nh);
2937 break;
Petr Machata1012b9a2017-09-02 23:49:23 +02002938 case MLXSW_SP_NEXTHOP_TYPE_IPIP:
2939 err = mlxsw_sp_nexthop_ipip_update
2940 (mlxsw_sp, adj_index, nh);
2941 break;
Petr Machata35225e42017-09-02 23:49:22 +02002942 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002943 if (err)
2944 return err;
2945 nh->update = 0;
2946 nh->offloaded = 1;
2947 }
Ido Schimmeleb789982017-10-22 23:11:48 +02002948 adj_index += nh->num_adj_entries;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002949 }
2950 return 0;
2951}
2952
Ido Schimmel1819ae32017-07-21 18:04:28 +02002953static bool
2954mlxsw_sp_fib_node_entry_is_first(const struct mlxsw_sp_fib_node *fib_node,
2955 const struct mlxsw_sp_fib_entry *fib_entry);
2956
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002957static int
2958mlxsw_sp_nexthop_fib_entries_update(struct mlxsw_sp *mlxsw_sp,
2959 struct mlxsw_sp_nexthop_group *nh_grp)
2960{
2961 struct mlxsw_sp_fib_entry *fib_entry;
2962 int err;
2963
2964 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
Ido Schimmel1819ae32017-07-21 18:04:28 +02002965 if (!mlxsw_sp_fib_node_entry_is_first(fib_entry->fib_node,
2966 fib_entry))
2967 continue;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002968 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
2969 if (err)
2970 return err;
2971 }
2972 return 0;
2973}
2974
2975static void
Ido Schimmel77d964e2017-08-02 09:56:05 +02002976mlxsw_sp_fib_entry_offload_refresh(struct mlxsw_sp_fib_entry *fib_entry,
2977 enum mlxsw_reg_ralue_op op, int err);
2978
2979static void
2980mlxsw_sp_nexthop_fib_entries_refresh(struct mlxsw_sp_nexthop_group *nh_grp)
2981{
2982 enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_WRITE;
2983 struct mlxsw_sp_fib_entry *fib_entry;
2984
2985 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
2986 if (!mlxsw_sp_fib_node_entry_is_first(fib_entry->fib_node,
2987 fib_entry))
2988 continue;
2989 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, 0);
2990 }
2991}
2992
Ido Schimmel425a08c2017-10-22 23:11:47 +02002993static void mlxsw_sp_adj_grp_size_round_up(u16 *p_adj_grp_size)
2994{
2995 /* Valid sizes for an adjacency group are:
2996 * 1-64, 512, 1024, 2048 and 4096.
2997 */
2998 if (*p_adj_grp_size <= 64)
2999 return;
3000 else if (*p_adj_grp_size <= 512)
3001 *p_adj_grp_size = 512;
3002 else if (*p_adj_grp_size <= 1024)
3003 *p_adj_grp_size = 1024;
3004 else if (*p_adj_grp_size <= 2048)
3005 *p_adj_grp_size = 2048;
3006 else
3007 *p_adj_grp_size = 4096;
3008}
3009
3010static void mlxsw_sp_adj_grp_size_round_down(u16 *p_adj_grp_size,
3011 unsigned int alloc_size)
3012{
3013 if (alloc_size >= 4096)
3014 *p_adj_grp_size = 4096;
3015 else if (alloc_size >= 2048)
3016 *p_adj_grp_size = 2048;
3017 else if (alloc_size >= 1024)
3018 *p_adj_grp_size = 1024;
3019 else if (alloc_size >= 512)
3020 *p_adj_grp_size = 512;
3021}
3022
3023static int mlxsw_sp_fix_adj_grp_size(struct mlxsw_sp *mlxsw_sp,
3024 u16 *p_adj_grp_size)
3025{
3026 unsigned int alloc_size;
3027 int err;
3028
3029 /* Round up the requested group size to the next size supported
3030 * by the device and make sure the request can be satisfied.
3031 */
3032 mlxsw_sp_adj_grp_size_round_up(p_adj_grp_size);
3033 err = mlxsw_sp_kvdl_alloc_size_query(mlxsw_sp, *p_adj_grp_size,
3034 &alloc_size);
3035 if (err)
3036 return err;
3037 /* It is possible the allocation results in more allocated
3038 * entries than requested. Try to use as much of them as
3039 * possible.
3040 */
3041 mlxsw_sp_adj_grp_size_round_down(p_adj_grp_size, alloc_size);
3042
3043 return 0;
3044}
3045
Ido Schimmel77d964e2017-08-02 09:56:05 +02003046static void
Ido Schimmeleb789982017-10-22 23:11:48 +02003047mlxsw_sp_nexthop_group_normalize(struct mlxsw_sp_nexthop_group *nh_grp)
3048{
3049 int i, g = 0, sum_norm_weight = 0;
3050 struct mlxsw_sp_nexthop *nh;
3051
3052 for (i = 0; i < nh_grp->count; i++) {
3053 nh = &nh_grp->nexthops[i];
3054
3055 if (!nh->should_offload)
3056 continue;
3057 if (g > 0)
3058 g = gcd(nh->nh_weight, g);
3059 else
3060 g = nh->nh_weight;
3061 }
3062
3063 for (i = 0; i < nh_grp->count; i++) {
3064 nh = &nh_grp->nexthops[i];
3065
3066 if (!nh->should_offload)
3067 continue;
3068 nh->norm_nh_weight = nh->nh_weight / g;
3069 sum_norm_weight += nh->norm_nh_weight;
3070 }
3071
3072 nh_grp->sum_norm_weight = sum_norm_weight;
3073}
3074
3075static void
3076mlxsw_sp_nexthop_group_rebalance(struct mlxsw_sp_nexthop_group *nh_grp)
3077{
3078 int total = nh_grp->sum_norm_weight;
3079 u16 ecmp_size = nh_grp->ecmp_size;
3080 int i, weight = 0, lower_bound = 0;
3081
3082 for (i = 0; i < nh_grp->count; i++) {
3083 struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
3084 int upper_bound;
3085
3086 if (!nh->should_offload)
3087 continue;
3088 weight += nh->norm_nh_weight;
3089 upper_bound = DIV_ROUND_CLOSEST(ecmp_size * weight, total);
3090 nh->num_adj_entries = upper_bound - lower_bound;
3091 lower_bound = upper_bound;
3092 }
3093}
3094
3095static void
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003096mlxsw_sp_nexthop_group_refresh(struct mlxsw_sp *mlxsw_sp,
3097 struct mlxsw_sp_nexthop_group *nh_grp)
3098{
Ido Schimmeleb789982017-10-22 23:11:48 +02003099 u16 ecmp_size, old_ecmp_size;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003100 struct mlxsw_sp_nexthop *nh;
3101 bool offload_change = false;
3102 u32 adj_index;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003103 bool old_adj_index_valid;
3104 u32 old_adj_index;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003105 int i;
3106 int err;
3107
Ido Schimmelb3e8d1e2017-02-08 11:16:32 +01003108 if (!nh_grp->gateway) {
3109 mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
3110 return;
3111 }
3112
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003113 for (i = 0; i < nh_grp->count; i++) {
3114 nh = &nh_grp->nexthops[i];
3115
Petr Machata56b8a9e2017-07-31 09:27:29 +02003116 if (nh->should_offload != nh->offloaded) {
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003117 offload_change = true;
3118 if (nh->should_offload)
3119 nh->update = 1;
3120 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003121 }
3122 if (!offload_change) {
3123 /* Nothing was added or removed, so no need to reallocate. Just
3124 * update MAC on existing adjacency indexes.
3125 */
Petr Machata35225e42017-09-02 23:49:22 +02003126 err = mlxsw_sp_nexthop_group_update(mlxsw_sp, nh_grp, false);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003127 if (err) {
3128 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
3129 goto set_trap;
3130 }
3131 return;
3132 }
Ido Schimmeleb789982017-10-22 23:11:48 +02003133 mlxsw_sp_nexthop_group_normalize(nh_grp);
3134 if (!nh_grp->sum_norm_weight)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003135 /* No neigh of this group is connected so we just set
3136 * the trap and let everthing flow through kernel.
3137 */
3138 goto set_trap;
3139
Ido Schimmeleb789982017-10-22 23:11:48 +02003140 ecmp_size = nh_grp->sum_norm_weight;
Ido Schimmel425a08c2017-10-22 23:11:47 +02003141 err = mlxsw_sp_fix_adj_grp_size(mlxsw_sp, &ecmp_size);
3142 if (err)
3143 /* No valid allocation size available. */
3144 goto set_trap;
3145
Arkadi Sharshevsky13124442017-03-25 08:28:22 +01003146 err = mlxsw_sp_kvdl_alloc(mlxsw_sp, ecmp_size, &adj_index);
3147 if (err) {
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003148 /* We ran out of KVD linear space, just set the
3149 * trap and let everything flow through kernel.
3150 */
3151 dev_warn(mlxsw_sp->bus_info->dev, "Failed to allocate KVD linear area for nexthop group.\n");
3152 goto set_trap;
3153 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003154 old_adj_index_valid = nh_grp->adj_index_valid;
3155 old_adj_index = nh_grp->adj_index;
3156 old_ecmp_size = nh_grp->ecmp_size;
3157 nh_grp->adj_index_valid = 1;
3158 nh_grp->adj_index = adj_index;
3159 nh_grp->ecmp_size = ecmp_size;
Ido Schimmeleb789982017-10-22 23:11:48 +02003160 mlxsw_sp_nexthop_group_rebalance(nh_grp);
Petr Machata35225e42017-09-02 23:49:22 +02003161 err = mlxsw_sp_nexthop_group_update(mlxsw_sp, nh_grp, true);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003162 if (err) {
3163 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
3164 goto set_trap;
3165 }
3166
3167 if (!old_adj_index_valid) {
3168 /* The trap was set for fib entries, so we have to call
3169 * fib entry update to unset it and use adjacency index.
3170 */
3171 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
3172 if (err) {
3173 dev_warn(mlxsw_sp->bus_info->dev, "Failed to add adjacency index to fib entries.\n");
3174 goto set_trap;
3175 }
3176 return;
3177 }
3178
3179 err = mlxsw_sp_adj_index_mass_update(mlxsw_sp, nh_grp,
3180 old_adj_index, old_ecmp_size);
3181 mlxsw_sp_kvdl_free(mlxsw_sp, old_adj_index);
3182 if (err) {
3183 dev_warn(mlxsw_sp->bus_info->dev, "Failed to mass-update adjacency index for nexthop group.\n");
3184 goto set_trap;
3185 }
Ido Schimmel77d964e2017-08-02 09:56:05 +02003186
3187 /* Offload state within the group changed, so update the flags. */
3188 mlxsw_sp_nexthop_fib_entries_refresh(nh_grp);
3189
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003190 return;
3191
3192set_trap:
3193 old_adj_index_valid = nh_grp->adj_index_valid;
3194 nh_grp->adj_index_valid = 0;
3195 for (i = 0; i < nh_grp->count; i++) {
3196 nh = &nh_grp->nexthops[i];
3197 nh->offloaded = 0;
3198 }
3199 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
3200 if (err)
3201 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set traps for fib entries.\n");
3202 if (old_adj_index_valid)
3203 mlxsw_sp_kvdl_free(mlxsw_sp, nh_grp->adj_index);
3204}
3205
3206static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop *nh,
3207 bool removing)
3208{
Petr Machata213666a2017-07-31 09:27:30 +02003209 if (!removing)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003210 nh->should_offload = 1;
Petr Machata213666a2017-07-31 09:27:30 +02003211 else if (nh->offloaded)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003212 nh->should_offload = 0;
3213 nh->update = 1;
3214}
3215
3216static void
3217mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
3218 struct mlxsw_sp_neigh_entry *neigh_entry,
3219 bool removing)
3220{
3221 struct mlxsw_sp_nexthop *nh;
3222
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003223 list_for_each_entry(nh, &neigh_entry->nexthop_list,
3224 neigh_list_node) {
3225 __mlxsw_sp_nexthop_neigh_update(nh, removing);
3226 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
3227 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003228}
3229
Ido Schimmel9665b742017-02-08 11:16:42 +01003230static void mlxsw_sp_nexthop_rif_init(struct mlxsw_sp_nexthop *nh,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003231 struct mlxsw_sp_rif *rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01003232{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003233 if (nh->rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01003234 return;
3235
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003236 nh->rif = rif;
3237 list_add(&nh->rif_list_node, &rif->nexthop_list);
Ido Schimmel9665b742017-02-08 11:16:42 +01003238}
3239
3240static void mlxsw_sp_nexthop_rif_fini(struct mlxsw_sp_nexthop *nh)
3241{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003242 if (!nh->rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01003243 return;
3244
3245 list_del(&nh->rif_list_node);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003246 nh->rif = NULL;
Ido Schimmel9665b742017-02-08 11:16:42 +01003247}
3248
Ido Schimmela8c97012017-02-08 11:16:35 +01003249static int mlxsw_sp_nexthop_neigh_init(struct mlxsw_sp *mlxsw_sp,
3250 struct mlxsw_sp_nexthop *nh)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003251{
3252 struct mlxsw_sp_neigh_entry *neigh_entry;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003253 struct neighbour *n;
Ido Schimmel93a87e52016-12-23 09:32:49 +01003254 u8 nud_state, dead;
Ido Schimmelc53b8e12017-02-08 11:16:30 +01003255 int err;
3256
Ido Schimmelad178c82017-02-08 11:16:40 +01003257 if (!nh->nh_grp->gateway || nh->neigh_entry)
Ido Schimmelb8399a12017-02-08 11:16:33 +01003258 return 0;
3259
Jiri Pirko33b13412016-11-10 12:31:04 +01003260 /* Take a reference of neigh here ensuring that neigh would
Petr Machata8de3c172017-07-31 09:27:25 +02003261 * not be destructed before the nexthop entry is finished.
Jiri Pirko33b13412016-11-10 12:31:04 +01003262 * The reference is taken either in neigh_lookup() or
Ido Schimmelfd76d912017-02-06 16:20:17 +01003263 * in neigh_create() in case n is not found.
Jiri Pirko33b13412016-11-10 12:31:04 +01003264 */
Ido Schimmel58adf2c2017-07-18 10:10:19 +02003265 n = neigh_lookup(nh->nh_grp->neigh_tbl, &nh->gw_addr, nh->rif->dev);
Jiri Pirko33b13412016-11-10 12:31:04 +01003266 if (!n) {
Ido Schimmel58adf2c2017-07-18 10:10:19 +02003267 n = neigh_create(nh->nh_grp->neigh_tbl, &nh->gw_addr,
3268 nh->rif->dev);
Ido Schimmela8c97012017-02-08 11:16:35 +01003269 if (IS_ERR(n))
3270 return PTR_ERR(n);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003271 neigh_event_send(n, NULL);
Jiri Pirko33b13412016-11-10 12:31:04 +01003272 }
3273 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
3274 if (!neigh_entry) {
Ido Schimmel5c8802f2017-02-06 16:20:13 +01003275 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
3276 if (IS_ERR(neigh_entry)) {
Ido Schimmelc53b8e12017-02-08 11:16:30 +01003277 err = -EINVAL;
3278 goto err_neigh_entry_create;
Ido Schimmel5c8802f2017-02-06 16:20:13 +01003279 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003280 }
Yotam Gigib2157142016-07-05 11:27:51 +02003281
3282 /* If that is the first nexthop connected to that neigh, add to
3283 * nexthop_neighs_list
3284 */
3285 if (list_empty(&neigh_entry->nexthop_list))
3286 list_add_tail(&neigh_entry->nexthop_neighs_list_node,
Ido Schimmel9011b672017-05-16 19:38:25 +02003287 &mlxsw_sp->router->nexthop_neighs_list);
Yotam Gigib2157142016-07-05 11:27:51 +02003288
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003289 nh->neigh_entry = neigh_entry;
3290 list_add_tail(&nh->neigh_list_node, &neigh_entry->nexthop_list);
3291 read_lock_bh(&n->lock);
3292 nud_state = n->nud_state;
Ido Schimmel93a87e52016-12-23 09:32:49 +01003293 dead = n->dead;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003294 read_unlock_bh(&n->lock);
Ido Schimmel93a87e52016-12-23 09:32:49 +01003295 __mlxsw_sp_nexthop_neigh_update(nh, !(nud_state & NUD_VALID && !dead));
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003296
3297 return 0;
Ido Schimmelc53b8e12017-02-08 11:16:30 +01003298
3299err_neigh_entry_create:
3300 neigh_release(n);
Ido Schimmelc53b8e12017-02-08 11:16:30 +01003301 return err;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003302}
3303
Ido Schimmela8c97012017-02-08 11:16:35 +01003304static void mlxsw_sp_nexthop_neigh_fini(struct mlxsw_sp *mlxsw_sp,
3305 struct mlxsw_sp_nexthop *nh)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003306{
3307 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
Ido Schimmela8c97012017-02-08 11:16:35 +01003308 struct neighbour *n;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003309
Ido Schimmelb8399a12017-02-08 11:16:33 +01003310 if (!neigh_entry)
Ido Schimmela8c97012017-02-08 11:16:35 +01003311 return;
3312 n = neigh_entry->key.n;
Ido Schimmelb8399a12017-02-08 11:16:33 +01003313
Ido Schimmel58312122016-12-23 09:32:50 +01003314 __mlxsw_sp_nexthop_neigh_update(nh, true);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003315 list_del(&nh->neigh_list_node);
Ido Schimmele58be792017-02-08 11:16:28 +01003316 nh->neigh_entry = NULL;
Yotam Gigib2157142016-07-05 11:27:51 +02003317
3318 /* If that is the last nexthop connected to that neigh, remove from
3319 * nexthop_neighs_list
3320 */
Ido Schimmele58be792017-02-08 11:16:28 +01003321 if (list_empty(&neigh_entry->nexthop_list))
3322 list_del(&neigh_entry->nexthop_neighs_list_node);
Yotam Gigib2157142016-07-05 11:27:51 +02003323
Ido Schimmel5c8802f2017-02-06 16:20:13 +01003324 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
3325 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
3326
3327 neigh_release(n);
Ido Schimmela8c97012017-02-08 11:16:35 +01003328}
Ido Schimmelc53b8e12017-02-08 11:16:30 +01003329
Petr Machata44b0fff2017-11-03 10:03:44 +01003330static bool mlxsw_sp_ipip_netdev_ul_up(struct net_device *ol_dev)
3331{
3332 struct net_device *ul_dev = __mlxsw_sp_ipip_netdev_ul_dev_get(ol_dev);
3333
3334 return ul_dev ? (ul_dev->flags & IFF_UP) : true;
3335}
3336
Petr Machata1012b9a2017-09-02 23:49:23 +02003337static int mlxsw_sp_nexthop_ipip_init(struct mlxsw_sp *mlxsw_sp,
Petr Machata1012b9a2017-09-02 23:49:23 +02003338 struct mlxsw_sp_nexthop *nh,
3339 struct net_device *ol_dev)
3340{
Petr Machata44b0fff2017-11-03 10:03:44 +01003341 bool removing;
3342
Petr Machata1012b9a2017-09-02 23:49:23 +02003343 if (!nh->nh_grp->gateway || nh->ipip_entry)
3344 return 0;
3345
Petr Machata4cccb732017-10-16 16:26:39 +02003346 nh->ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
3347 if (!nh->ipip_entry)
3348 return -ENOENT;
Petr Machata1012b9a2017-09-02 23:49:23 +02003349
Petr Machata44b0fff2017-11-03 10:03:44 +01003350 removing = !mlxsw_sp_ipip_netdev_ul_up(ol_dev);
3351 __mlxsw_sp_nexthop_neigh_update(nh, removing);
Petr Machata1012b9a2017-09-02 23:49:23 +02003352 return 0;
3353}
3354
3355static void mlxsw_sp_nexthop_ipip_fini(struct mlxsw_sp *mlxsw_sp,
3356 struct mlxsw_sp_nexthop *nh)
3357{
3358 struct mlxsw_sp_ipip_entry *ipip_entry = nh->ipip_entry;
3359
3360 if (!ipip_entry)
3361 return;
3362
3363 __mlxsw_sp_nexthop_neigh_update(nh, true);
Petr Machata1012b9a2017-09-02 23:49:23 +02003364 nh->ipip_entry = NULL;
3365}
3366
3367static bool mlxsw_sp_nexthop4_ipip_type(const struct mlxsw_sp *mlxsw_sp,
3368 const struct fib_nh *fib_nh,
3369 enum mlxsw_sp_ipip_type *p_ipipt)
3370{
3371 struct net_device *dev = fib_nh->nh_dev;
3372
3373 return dev &&
3374 fib_nh->nh_parent->fib_type == RTN_UNICAST &&
3375 mlxsw_sp_netdev_ipip_type(mlxsw_sp, dev, p_ipipt);
3376}
3377
Petr Machata35225e42017-09-02 23:49:22 +02003378static void mlxsw_sp_nexthop_type_fini(struct mlxsw_sp *mlxsw_sp,
3379 struct mlxsw_sp_nexthop *nh)
3380{
3381 switch (nh->type) {
3382 case MLXSW_SP_NEXTHOP_TYPE_ETH:
3383 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
3384 mlxsw_sp_nexthop_rif_fini(nh);
3385 break;
Petr Machata1012b9a2017-09-02 23:49:23 +02003386 case MLXSW_SP_NEXTHOP_TYPE_IPIP:
Petr Machatade0f43c2017-10-02 12:14:57 +02003387 mlxsw_sp_nexthop_rif_fini(nh);
Petr Machata1012b9a2017-09-02 23:49:23 +02003388 mlxsw_sp_nexthop_ipip_fini(mlxsw_sp, nh);
3389 break;
Petr Machata35225e42017-09-02 23:49:22 +02003390 }
3391}
3392
3393static int mlxsw_sp_nexthop4_type_init(struct mlxsw_sp *mlxsw_sp,
3394 struct mlxsw_sp_nexthop *nh,
3395 struct fib_nh *fib_nh)
3396{
Petr Machata1012b9a2017-09-02 23:49:23 +02003397 struct mlxsw_sp_router *router = mlxsw_sp->router;
Petr Machata35225e42017-09-02 23:49:22 +02003398 struct net_device *dev = fib_nh->nh_dev;
Petr Machata1012b9a2017-09-02 23:49:23 +02003399 enum mlxsw_sp_ipip_type ipipt;
Petr Machata35225e42017-09-02 23:49:22 +02003400 struct mlxsw_sp_rif *rif;
3401 int err;
3402
Petr Machata1012b9a2017-09-02 23:49:23 +02003403 if (mlxsw_sp_nexthop4_ipip_type(mlxsw_sp, fib_nh, &ipipt) &&
3404 router->ipip_ops_arr[ipipt]->can_offload(mlxsw_sp, dev,
3405 MLXSW_SP_L3_PROTO_IPV4)) {
3406 nh->type = MLXSW_SP_NEXTHOP_TYPE_IPIP;
Petr Machata4cccb732017-10-16 16:26:39 +02003407 err = mlxsw_sp_nexthop_ipip_init(mlxsw_sp, nh, dev);
Petr Machatade0f43c2017-10-02 12:14:57 +02003408 if (err)
3409 return err;
3410 mlxsw_sp_nexthop_rif_init(nh, &nh->ipip_entry->ol_lb->common);
3411 return 0;
Petr Machata1012b9a2017-09-02 23:49:23 +02003412 }
3413
Petr Machata35225e42017-09-02 23:49:22 +02003414 nh->type = MLXSW_SP_NEXTHOP_TYPE_ETH;
3415 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3416 if (!rif)
3417 return 0;
3418
3419 mlxsw_sp_nexthop_rif_init(nh, rif);
3420 err = mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
3421 if (err)
3422 goto err_neigh_init;
3423
3424 return 0;
3425
3426err_neigh_init:
3427 mlxsw_sp_nexthop_rif_fini(nh);
3428 return err;
3429}
3430
3431static void mlxsw_sp_nexthop4_type_fini(struct mlxsw_sp *mlxsw_sp,
3432 struct mlxsw_sp_nexthop *nh)
3433{
3434 mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
3435}
3436
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02003437static int mlxsw_sp_nexthop4_init(struct mlxsw_sp *mlxsw_sp,
3438 struct mlxsw_sp_nexthop_group *nh_grp,
3439 struct mlxsw_sp_nexthop *nh,
3440 struct fib_nh *fib_nh)
Ido Schimmela8c97012017-02-08 11:16:35 +01003441{
3442 struct net_device *dev = fib_nh->nh_dev;
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +01003443 struct in_device *in_dev;
Ido Schimmela8c97012017-02-08 11:16:35 +01003444 int err;
3445
3446 nh->nh_grp = nh_grp;
3447 nh->key.fib_nh = fib_nh;
Ido Schimmel408bd942017-10-22 23:11:46 +02003448#ifdef CONFIG_IP_ROUTE_MULTIPATH
3449 nh->nh_weight = fib_nh->nh_weight;
3450#else
3451 nh->nh_weight = 1;
3452#endif
Ido Schimmel58adf2c2017-07-18 10:10:19 +02003453 memcpy(&nh->gw_addr, &fib_nh->nh_gw, sizeof(fib_nh->nh_gw));
Ido Schimmela8c97012017-02-08 11:16:35 +01003454 err = mlxsw_sp_nexthop_insert(mlxsw_sp, nh);
3455 if (err)
3456 return err;
3457
Arkadi Sharshevskya5390272017-09-25 10:32:28 +02003458 mlxsw_sp_nexthop_counter_alloc(mlxsw_sp, nh);
Arkadi Sharshevskydbe45982017-09-25 10:32:23 +02003459 list_add_tail(&nh->router_list_node, &mlxsw_sp->router->nexthop_list);
3460
Ido Schimmel97989ee2017-03-10 08:53:38 +01003461 if (!dev)
3462 return 0;
3463
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +01003464 in_dev = __in_dev_get_rtnl(dev);
3465 if (in_dev && IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
3466 fib_nh->nh_flags & RTNH_F_LINKDOWN)
3467 return 0;
3468
Petr Machata35225e42017-09-02 23:49:22 +02003469 err = mlxsw_sp_nexthop4_type_init(mlxsw_sp, nh, fib_nh);
Ido Schimmela8c97012017-02-08 11:16:35 +01003470 if (err)
3471 goto err_nexthop_neigh_init;
3472
3473 return 0;
3474
3475err_nexthop_neigh_init:
3476 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
3477 return err;
3478}
3479
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02003480static void mlxsw_sp_nexthop4_fini(struct mlxsw_sp *mlxsw_sp,
3481 struct mlxsw_sp_nexthop *nh)
Ido Schimmela8c97012017-02-08 11:16:35 +01003482{
Petr Machata35225e42017-09-02 23:49:22 +02003483 mlxsw_sp_nexthop4_type_fini(mlxsw_sp, nh);
Arkadi Sharshevskydbe45982017-09-25 10:32:23 +02003484 list_del(&nh->router_list_node);
Arkadi Sharshevskya5390272017-09-25 10:32:28 +02003485 mlxsw_sp_nexthop_counter_free(mlxsw_sp, nh);
Ido Schimmelc53b8e12017-02-08 11:16:30 +01003486 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003487}
3488
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02003489static void mlxsw_sp_nexthop4_event(struct mlxsw_sp *mlxsw_sp,
3490 unsigned long event, struct fib_nh *fib_nh)
Ido Schimmelad178c82017-02-08 11:16:40 +01003491{
3492 struct mlxsw_sp_nexthop_key key;
3493 struct mlxsw_sp_nexthop *nh;
Ido Schimmelad178c82017-02-08 11:16:40 +01003494
Ido Schimmel9011b672017-05-16 19:38:25 +02003495 if (mlxsw_sp->router->aborted)
Ido Schimmelad178c82017-02-08 11:16:40 +01003496 return;
3497
3498 key.fib_nh = fib_nh;
3499 nh = mlxsw_sp_nexthop_lookup(mlxsw_sp, key);
3500 if (WARN_ON_ONCE(!nh))
3501 return;
3502
Ido Schimmelad178c82017-02-08 11:16:40 +01003503 switch (event) {
3504 case FIB_EVENT_NH_ADD:
Petr Machata35225e42017-09-02 23:49:22 +02003505 mlxsw_sp_nexthop4_type_init(mlxsw_sp, nh, fib_nh);
Ido Schimmelad178c82017-02-08 11:16:40 +01003506 break;
3507 case FIB_EVENT_NH_DEL:
Petr Machata35225e42017-09-02 23:49:22 +02003508 mlxsw_sp_nexthop4_type_fini(mlxsw_sp, nh);
Ido Schimmelad178c82017-02-08 11:16:40 +01003509 break;
3510 }
3511
3512 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
3513}
3514
Petr Machata0c5f1cd2017-11-03 10:03:38 +01003515static void mlxsw_sp_nexthop_rif_update(struct mlxsw_sp *mlxsw_sp,
3516 struct mlxsw_sp_rif *rif)
3517{
3518 struct mlxsw_sp_nexthop *nh;
Petr Machata44b0fff2017-11-03 10:03:44 +01003519 bool removing;
Petr Machata0c5f1cd2017-11-03 10:03:38 +01003520
3521 list_for_each_entry(nh, &rif->nexthop_list, rif_list_node) {
Petr Machata44b0fff2017-11-03 10:03:44 +01003522 switch (nh->type) {
3523 case MLXSW_SP_NEXTHOP_TYPE_ETH:
3524 removing = false;
3525 break;
3526 case MLXSW_SP_NEXTHOP_TYPE_IPIP:
3527 removing = !mlxsw_sp_ipip_netdev_ul_up(rif->dev);
3528 break;
3529 default:
3530 WARN_ON(1);
3531 continue;
3532 }
3533
3534 __mlxsw_sp_nexthop_neigh_update(nh, removing);
Petr Machata0c5f1cd2017-11-03 10:03:38 +01003535 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
3536 }
3537}
3538
Ido Schimmel9665b742017-02-08 11:16:42 +01003539static void mlxsw_sp_nexthop_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003540 struct mlxsw_sp_rif *rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01003541{
3542 struct mlxsw_sp_nexthop *nh, *tmp;
3543
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003544 list_for_each_entry_safe(nh, tmp, &rif->nexthop_list, rif_list_node) {
Petr Machata35225e42017-09-02 23:49:22 +02003545 mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
Ido Schimmel9665b742017-02-08 11:16:42 +01003546 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
3547 }
3548}
3549
Petr Machata9b014512017-09-02 23:49:20 +02003550static bool mlxsw_sp_fi_is_gateway(const struct mlxsw_sp *mlxsw_sp,
3551 const struct fib_info *fi)
3552{
Petr Machata1012b9a2017-09-02 23:49:23 +02003553 return fi->fib_nh->nh_scope == RT_SCOPE_LINK ||
3554 mlxsw_sp_nexthop4_ipip_type(mlxsw_sp, fi->fib_nh, NULL);
Petr Machata9b014512017-09-02 23:49:20 +02003555}
3556
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003557static struct mlxsw_sp_nexthop_group *
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02003558mlxsw_sp_nexthop4_group_create(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003559{
3560 struct mlxsw_sp_nexthop_group *nh_grp;
3561 struct mlxsw_sp_nexthop *nh;
3562 struct fib_nh *fib_nh;
3563 size_t alloc_size;
3564 int i;
3565 int err;
3566
3567 alloc_size = sizeof(*nh_grp) +
3568 fi->fib_nhs * sizeof(struct mlxsw_sp_nexthop);
3569 nh_grp = kzalloc(alloc_size, GFP_KERNEL);
3570 if (!nh_grp)
3571 return ERR_PTR(-ENOMEM);
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02003572 nh_grp->priv = fi;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003573 INIT_LIST_HEAD(&nh_grp->fib_list);
Ido Schimmel58adf2c2017-07-18 10:10:19 +02003574 nh_grp->neigh_tbl = &arp_tbl;
3575
Petr Machata9b014512017-09-02 23:49:20 +02003576 nh_grp->gateway = mlxsw_sp_fi_is_gateway(mlxsw_sp, fi);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003577 nh_grp->count = fi->fib_nhs;
Ido Schimmel7387dbb2017-07-12 09:12:53 +02003578 fib_info_hold(fi);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003579 for (i = 0; i < nh_grp->count; i++) {
3580 nh = &nh_grp->nexthops[i];
3581 fib_nh = &fi->fib_nh[i];
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02003582 err = mlxsw_sp_nexthop4_init(mlxsw_sp, nh_grp, nh, fib_nh);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003583 if (err)
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02003584 goto err_nexthop4_init;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003585 }
Ido Schimmele9ad5e72017-02-08 11:16:29 +01003586 err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp);
3587 if (err)
3588 goto err_nexthop_group_insert;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003589 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
3590 return nh_grp;
3591
Ido Schimmele9ad5e72017-02-08 11:16:29 +01003592err_nexthop_group_insert:
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02003593err_nexthop4_init:
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +01003594 for (i--; i >= 0; i--) {
3595 nh = &nh_grp->nexthops[i];
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02003596 mlxsw_sp_nexthop4_fini(mlxsw_sp, nh);
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +01003597 }
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02003598 fib_info_put(fi);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003599 kfree(nh_grp);
3600 return ERR_PTR(err);
3601}
3602
3603static void
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02003604mlxsw_sp_nexthop4_group_destroy(struct mlxsw_sp *mlxsw_sp,
3605 struct mlxsw_sp_nexthop_group *nh_grp)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003606{
3607 struct mlxsw_sp_nexthop *nh;
3608 int i;
3609
Ido Schimmele9ad5e72017-02-08 11:16:29 +01003610 mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003611 for (i = 0; i < nh_grp->count; i++) {
3612 nh = &nh_grp->nexthops[i];
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02003613 mlxsw_sp_nexthop4_fini(mlxsw_sp, nh);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003614 }
Ido Schimmel58312122016-12-23 09:32:50 +01003615 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
3616 WARN_ON_ONCE(nh_grp->adj_index_valid);
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02003617 fib_info_put(mlxsw_sp_nexthop4_group_fi(nh_grp));
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003618 kfree(nh_grp);
3619}
3620
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02003621static int mlxsw_sp_nexthop4_group_get(struct mlxsw_sp *mlxsw_sp,
3622 struct mlxsw_sp_fib_entry *fib_entry,
3623 struct fib_info *fi)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003624{
3625 struct mlxsw_sp_nexthop_group *nh_grp;
3626
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02003627 nh_grp = mlxsw_sp_nexthop4_group_lookup(mlxsw_sp, fi);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003628 if (!nh_grp) {
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02003629 nh_grp = mlxsw_sp_nexthop4_group_create(mlxsw_sp, fi);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003630 if (IS_ERR(nh_grp))
3631 return PTR_ERR(nh_grp);
3632 }
3633 list_add_tail(&fib_entry->nexthop_group_node, &nh_grp->fib_list);
3634 fib_entry->nh_group = nh_grp;
3635 return 0;
3636}
3637
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02003638static void mlxsw_sp_nexthop4_group_put(struct mlxsw_sp *mlxsw_sp,
3639 struct mlxsw_sp_fib_entry *fib_entry)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003640{
3641 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
3642
3643 list_del(&fib_entry->nexthop_group_node);
3644 if (!list_empty(&nh_grp->fib_list))
3645 return;
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02003646 mlxsw_sp_nexthop4_group_destroy(mlxsw_sp, nh_grp);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003647}
3648
Ido Schimmel013b20f2017-02-08 11:16:36 +01003649static bool
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02003650mlxsw_sp_fib4_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry)
3651{
3652 struct mlxsw_sp_fib4_entry *fib4_entry;
3653
3654 fib4_entry = container_of(fib_entry, struct mlxsw_sp_fib4_entry,
3655 common);
3656 return !fib4_entry->tos;
3657}
3658
3659static bool
Ido Schimmel013b20f2017-02-08 11:16:36 +01003660mlxsw_sp_fib_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry)
3661{
3662 struct mlxsw_sp_nexthop_group *nh_group = fib_entry->nh_group;
3663
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02003664 switch (fib_entry->fib_node->fib->proto) {
3665 case MLXSW_SP_L3_PROTO_IPV4:
3666 if (!mlxsw_sp_fib4_entry_should_offload(fib_entry))
3667 return false;
3668 break;
3669 case MLXSW_SP_L3_PROTO_IPV6:
3670 break;
3671 }
Ido Schimmel9aecce12017-02-09 10:28:42 +01003672
Ido Schimmel013b20f2017-02-08 11:16:36 +01003673 switch (fib_entry->type) {
3674 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
3675 return !!nh_group->adj_index_valid;
3676 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
Ido Schimmel70ad3502017-02-08 11:16:38 +01003677 return !!nh_group->nh_rif;
Petr Machata4607f6d2017-09-02 23:49:25 +02003678 case MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP:
3679 return true;
Ido Schimmel013b20f2017-02-08 11:16:36 +01003680 default:
3681 return false;
3682 }
3683}
3684
Ido Schimmel428b8512017-08-03 13:28:28 +02003685static struct mlxsw_sp_nexthop *
3686mlxsw_sp_rt6_nexthop(struct mlxsw_sp_nexthop_group *nh_grp,
3687 const struct mlxsw_sp_rt6 *mlxsw_sp_rt6)
3688{
3689 int i;
3690
3691 for (i = 0; i < nh_grp->count; i++) {
3692 struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
3693 struct rt6_info *rt = mlxsw_sp_rt6->rt;
3694
3695 if (nh->rif && nh->rif->dev == rt->dst.dev &&
3696 ipv6_addr_equal((const struct in6_addr *) &nh->gw_addr,
3697 &rt->rt6i_gateway))
3698 return nh;
3699 continue;
3700 }
3701
3702 return NULL;
3703}
3704
Ido Schimmel3984d1a2017-08-02 09:56:03 +02003705static void
3706mlxsw_sp_fib4_entry_offload_set(struct mlxsw_sp_fib_entry *fib_entry)
3707{
3708 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
3709 int i;
3710
Petr Machata4607f6d2017-09-02 23:49:25 +02003711 if (fib_entry->type == MLXSW_SP_FIB_ENTRY_TYPE_LOCAL ||
3712 fib_entry->type == MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP) {
Ido Schimmel3984d1a2017-08-02 09:56:03 +02003713 nh_grp->nexthops->key.fib_nh->nh_flags |= RTNH_F_OFFLOAD;
3714 return;
3715 }
3716
3717 for (i = 0; i < nh_grp->count; i++) {
3718 struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
3719
3720 if (nh->offloaded)
3721 nh->key.fib_nh->nh_flags |= RTNH_F_OFFLOAD;
3722 else
3723 nh->key.fib_nh->nh_flags &= ~RTNH_F_OFFLOAD;
3724 }
3725}
3726
3727static void
3728mlxsw_sp_fib4_entry_offload_unset(struct mlxsw_sp_fib_entry *fib_entry)
3729{
3730 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
3731 int i;
3732
3733 for (i = 0; i < nh_grp->count; i++) {
3734 struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
3735
3736 nh->key.fib_nh->nh_flags &= ~RTNH_F_OFFLOAD;
3737 }
3738}
3739
Ido Schimmel428b8512017-08-03 13:28:28 +02003740static void
3741mlxsw_sp_fib6_entry_offload_set(struct mlxsw_sp_fib_entry *fib_entry)
3742{
3743 struct mlxsw_sp_fib6_entry *fib6_entry;
3744 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
3745
3746 fib6_entry = container_of(fib_entry, struct mlxsw_sp_fib6_entry,
3747 common);
3748
3749 if (fib_entry->type == MLXSW_SP_FIB_ENTRY_TYPE_LOCAL) {
3750 list_first_entry(&fib6_entry->rt6_list, struct mlxsw_sp_rt6,
Ido Schimmelfe400792017-08-15 09:09:49 +02003751 list)->rt->rt6i_nh_flags |= RTNH_F_OFFLOAD;
Ido Schimmel428b8512017-08-03 13:28:28 +02003752 return;
3753 }
3754
3755 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
3756 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
3757 struct mlxsw_sp_nexthop *nh;
3758
3759 nh = mlxsw_sp_rt6_nexthop(nh_grp, mlxsw_sp_rt6);
3760 if (nh && nh->offloaded)
Ido Schimmelfe400792017-08-15 09:09:49 +02003761 mlxsw_sp_rt6->rt->rt6i_nh_flags |= RTNH_F_OFFLOAD;
Ido Schimmel428b8512017-08-03 13:28:28 +02003762 else
Ido Schimmelfe400792017-08-15 09:09:49 +02003763 mlxsw_sp_rt6->rt->rt6i_nh_flags &= ~RTNH_F_OFFLOAD;
Ido Schimmel428b8512017-08-03 13:28:28 +02003764 }
3765}
3766
3767static void
3768mlxsw_sp_fib6_entry_offload_unset(struct mlxsw_sp_fib_entry *fib_entry)
3769{
3770 struct mlxsw_sp_fib6_entry *fib6_entry;
3771 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
3772
3773 fib6_entry = container_of(fib_entry, struct mlxsw_sp_fib6_entry,
3774 common);
3775 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
3776 struct rt6_info *rt = mlxsw_sp_rt6->rt;
3777
Ido Schimmelfe400792017-08-15 09:09:49 +02003778 rt->rt6i_nh_flags &= ~RTNH_F_OFFLOAD;
Ido Schimmel428b8512017-08-03 13:28:28 +02003779 }
3780}
3781
Ido Schimmel013b20f2017-02-08 11:16:36 +01003782static void mlxsw_sp_fib_entry_offload_set(struct mlxsw_sp_fib_entry *fib_entry)
3783{
Ido Schimmel76610eb2017-03-10 08:53:41 +01003784 switch (fib_entry->fib_node->fib->proto) {
Ido Schimmel013b20f2017-02-08 11:16:36 +01003785 case MLXSW_SP_L3_PROTO_IPV4:
Ido Schimmel3984d1a2017-08-02 09:56:03 +02003786 mlxsw_sp_fib4_entry_offload_set(fib_entry);
Ido Schimmel013b20f2017-02-08 11:16:36 +01003787 break;
3788 case MLXSW_SP_L3_PROTO_IPV6:
Ido Schimmel428b8512017-08-03 13:28:28 +02003789 mlxsw_sp_fib6_entry_offload_set(fib_entry);
3790 break;
Ido Schimmel013b20f2017-02-08 11:16:36 +01003791 }
3792}
3793
3794static void
3795mlxsw_sp_fib_entry_offload_unset(struct mlxsw_sp_fib_entry *fib_entry)
3796{
Ido Schimmel76610eb2017-03-10 08:53:41 +01003797 switch (fib_entry->fib_node->fib->proto) {
Ido Schimmel013b20f2017-02-08 11:16:36 +01003798 case MLXSW_SP_L3_PROTO_IPV4:
Ido Schimmel3984d1a2017-08-02 09:56:03 +02003799 mlxsw_sp_fib4_entry_offload_unset(fib_entry);
Ido Schimmel013b20f2017-02-08 11:16:36 +01003800 break;
3801 case MLXSW_SP_L3_PROTO_IPV6:
Ido Schimmel428b8512017-08-03 13:28:28 +02003802 mlxsw_sp_fib6_entry_offload_unset(fib_entry);
3803 break;
Ido Schimmel013b20f2017-02-08 11:16:36 +01003804 }
Ido Schimmel013b20f2017-02-08 11:16:36 +01003805}
3806
3807static void
3808mlxsw_sp_fib_entry_offload_refresh(struct mlxsw_sp_fib_entry *fib_entry,
3809 enum mlxsw_reg_ralue_op op, int err)
3810{
3811 switch (op) {
3812 case MLXSW_REG_RALUE_OP_WRITE_DELETE:
Ido Schimmel013b20f2017-02-08 11:16:36 +01003813 return mlxsw_sp_fib_entry_offload_unset(fib_entry);
3814 case MLXSW_REG_RALUE_OP_WRITE_WRITE:
3815 if (err)
3816 return;
Ido Schimmel1353ee72017-08-02 09:56:04 +02003817 if (mlxsw_sp_fib_entry_should_offload(fib_entry))
Ido Schimmel013b20f2017-02-08 11:16:36 +01003818 mlxsw_sp_fib_entry_offload_set(fib_entry);
Petr Machata85f44a12017-10-02 12:21:58 +02003819 else
Ido Schimmel013b20f2017-02-08 11:16:36 +01003820 mlxsw_sp_fib_entry_offload_unset(fib_entry);
3821 return;
3822 default:
3823 return;
3824 }
3825}
3826
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02003827static void
3828mlxsw_sp_fib_entry_ralue_pack(char *ralue_pl,
3829 const struct mlxsw_sp_fib_entry *fib_entry,
3830 enum mlxsw_reg_ralue_op op)
3831{
3832 struct mlxsw_sp_fib *fib = fib_entry->fib_node->fib;
3833 enum mlxsw_reg_ralxx_protocol proto;
3834 u32 *p_dip;
3835
3836 proto = (enum mlxsw_reg_ralxx_protocol) fib->proto;
3837
3838 switch (fib->proto) {
3839 case MLXSW_SP_L3_PROTO_IPV4:
3840 p_dip = (u32 *) fib_entry->fib_node->key.addr;
3841 mlxsw_reg_ralue_pack4(ralue_pl, proto, op, fib->vr->id,
3842 fib_entry->fib_node->key.prefix_len,
3843 *p_dip);
3844 break;
3845 case MLXSW_SP_L3_PROTO_IPV6:
3846 mlxsw_reg_ralue_pack6(ralue_pl, proto, op, fib->vr->id,
3847 fib_entry->fib_node->key.prefix_len,
3848 fib_entry->fib_node->key.addr);
3849 break;
3850 }
3851}
3852
3853static int mlxsw_sp_fib_entry_op_remote(struct mlxsw_sp *mlxsw_sp,
3854 struct mlxsw_sp_fib_entry *fib_entry,
3855 enum mlxsw_reg_ralue_op op)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003856{
3857 char ralue_pl[MLXSW_REG_RALUE_LEN];
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003858 enum mlxsw_reg_ralue_trap_action trap_action;
3859 u16 trap_id = 0;
3860 u32 adjacency_index = 0;
3861 u16 ecmp_size = 0;
3862
3863 /* In case the nexthop group adjacency index is valid, use it
3864 * with provided ECMP size. Otherwise, setup trap and pass
3865 * traffic to kernel.
3866 */
Ido Schimmel4b411472017-02-08 11:16:37 +01003867 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003868 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
3869 adjacency_index = fib_entry->nh_group->adj_index;
3870 ecmp_size = fib_entry->nh_group->ecmp_size;
3871 } else {
3872 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
3873 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
3874 }
3875
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02003876 mlxsw_sp_fib_entry_ralue_pack(ralue_pl, fib_entry, op);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02003877 mlxsw_reg_ralue_act_remote_pack(ralue_pl, trap_action, trap_id,
3878 adjacency_index, ecmp_size);
3879 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
3880}
3881
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02003882static int mlxsw_sp_fib_entry_op_local(struct mlxsw_sp *mlxsw_sp,
3883 struct mlxsw_sp_fib_entry *fib_entry,
3884 enum mlxsw_reg_ralue_op op)
Jiri Pirko61c503f2016-07-04 08:23:11 +02003885{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003886 struct mlxsw_sp_rif *rif = fib_entry->nh_group->nh_rif;
Ido Schimmel70ad3502017-02-08 11:16:38 +01003887 enum mlxsw_reg_ralue_trap_action trap_action;
Jiri Pirko61c503f2016-07-04 08:23:11 +02003888 char ralue_pl[MLXSW_REG_RALUE_LEN];
Ido Schimmel70ad3502017-02-08 11:16:38 +01003889 u16 trap_id = 0;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003890 u16 rif_index = 0;
Ido Schimmel70ad3502017-02-08 11:16:38 +01003891
3892 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
3893 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003894 rif_index = rif->rif_index;
Ido Schimmel70ad3502017-02-08 11:16:38 +01003895 } else {
3896 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
3897 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
3898 }
Jiri Pirko61c503f2016-07-04 08:23:11 +02003899
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02003900 mlxsw_sp_fib_entry_ralue_pack(ralue_pl, fib_entry, op);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003901 mlxsw_reg_ralue_act_local_pack(ralue_pl, trap_action, trap_id,
3902 rif_index);
Jiri Pirko61c503f2016-07-04 08:23:11 +02003903 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
3904}
3905
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02003906static int mlxsw_sp_fib_entry_op_trap(struct mlxsw_sp *mlxsw_sp,
3907 struct mlxsw_sp_fib_entry *fib_entry,
3908 enum mlxsw_reg_ralue_op op)
Jiri Pirko61c503f2016-07-04 08:23:11 +02003909{
3910 char ralue_pl[MLXSW_REG_RALUE_LEN];
Jiri Pirko61c503f2016-07-04 08:23:11 +02003911
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02003912 mlxsw_sp_fib_entry_ralue_pack(ralue_pl, fib_entry, op);
Jiri Pirko61c503f2016-07-04 08:23:11 +02003913 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
3914 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
3915}
3916
Petr Machata4607f6d2017-09-02 23:49:25 +02003917static int
3918mlxsw_sp_fib_entry_op_ipip_decap(struct mlxsw_sp *mlxsw_sp,
3919 struct mlxsw_sp_fib_entry *fib_entry,
3920 enum mlxsw_reg_ralue_op op)
3921{
3922 struct mlxsw_sp_ipip_entry *ipip_entry = fib_entry->decap.ipip_entry;
3923 const struct mlxsw_sp_ipip_ops *ipip_ops;
3924
3925 if (WARN_ON(!ipip_entry))
3926 return -EINVAL;
3927
3928 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
3929 return ipip_ops->fib_entry_op(mlxsw_sp, ipip_entry, op,
3930 fib_entry->decap.tunnel_index);
3931}
3932
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02003933static int __mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
3934 struct mlxsw_sp_fib_entry *fib_entry,
3935 enum mlxsw_reg_ralue_op op)
Jiri Pirko61c503f2016-07-04 08:23:11 +02003936{
3937 switch (fib_entry->type) {
3938 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02003939 return mlxsw_sp_fib_entry_op_remote(mlxsw_sp, fib_entry, op);
Jiri Pirko61c503f2016-07-04 08:23:11 +02003940 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02003941 return mlxsw_sp_fib_entry_op_local(mlxsw_sp, fib_entry, op);
Jiri Pirko61c503f2016-07-04 08:23:11 +02003942 case MLXSW_SP_FIB_ENTRY_TYPE_TRAP:
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02003943 return mlxsw_sp_fib_entry_op_trap(mlxsw_sp, fib_entry, op);
Petr Machata4607f6d2017-09-02 23:49:25 +02003944 case MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP:
3945 return mlxsw_sp_fib_entry_op_ipip_decap(mlxsw_sp,
3946 fib_entry, op);
Jiri Pirko61c503f2016-07-04 08:23:11 +02003947 }
3948 return -EINVAL;
3949}
3950
3951static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
3952 struct mlxsw_sp_fib_entry *fib_entry,
3953 enum mlxsw_reg_ralue_op op)
3954{
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02003955 int err = __mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry, op);
Ido Schimmel013b20f2017-02-08 11:16:36 +01003956
Ido Schimmel013b20f2017-02-08 11:16:36 +01003957 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, err);
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02003958
Ido Schimmel013b20f2017-02-08 11:16:36 +01003959 return err;
Jiri Pirko61c503f2016-07-04 08:23:11 +02003960}
3961
3962static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
3963 struct mlxsw_sp_fib_entry *fib_entry)
3964{
Jiri Pirko7146da32016-09-01 10:37:41 +02003965 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
3966 MLXSW_REG_RALUE_OP_WRITE_WRITE);
Jiri Pirko61c503f2016-07-04 08:23:11 +02003967}
3968
3969static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
3970 struct mlxsw_sp_fib_entry *fib_entry)
3971{
3972 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
3973 MLXSW_REG_RALUE_OP_WRITE_DELETE);
3974}
3975
Jiri Pirko61c503f2016-07-04 08:23:11 +02003976static int
Ido Schimmel013b20f2017-02-08 11:16:36 +01003977mlxsw_sp_fib4_entry_type_set(struct mlxsw_sp *mlxsw_sp,
3978 const struct fib_entry_notifier_info *fen_info,
3979 struct mlxsw_sp_fib_entry *fib_entry)
Jiri Pirko61c503f2016-07-04 08:23:11 +02003980{
Petr Machata4607f6d2017-09-02 23:49:25 +02003981 union mlxsw_sp_l3addr dip = { .addr4 = htonl(fen_info->dst) };
3982 struct net_device *dev = fen_info->fi->fib_dev;
3983 struct mlxsw_sp_ipip_entry *ipip_entry;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003984 struct fib_info *fi = fen_info->fi;
Jiri Pirko61c503f2016-07-04 08:23:11 +02003985
Ido Schimmel97989ee2017-03-10 08:53:38 +01003986 switch (fen_info->type) {
Ido Schimmel97989ee2017-03-10 08:53:38 +01003987 case RTN_LOCAL:
Petr Machata4607f6d2017-09-02 23:49:25 +02003988 ipip_entry = mlxsw_sp_ipip_entry_find_by_decap(mlxsw_sp, dev,
3989 MLXSW_SP_L3_PROTO_IPV4, dip);
3990 if (ipip_entry) {
3991 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP;
3992 return mlxsw_sp_fib_entry_decap_init(mlxsw_sp,
3993 fib_entry,
3994 ipip_entry);
3995 }
3996 /* fall through */
3997 case RTN_BROADCAST:
Jiri Pirko61c503f2016-07-04 08:23:11 +02003998 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
3999 return 0;
Ido Schimmel97989ee2017-03-10 08:53:38 +01004000 case RTN_UNREACHABLE: /* fall through */
4001 case RTN_BLACKHOLE: /* fall through */
4002 case RTN_PROHIBIT:
4003 /* Packets hitting these routes need to be trapped, but
4004 * can do so with a lower priority than packets directed
4005 * at the host, so use action type local instead of trap.
4006 */
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004007 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
Ido Schimmel97989ee2017-03-10 08:53:38 +01004008 return 0;
4009 case RTN_UNICAST:
Petr Machata9b014512017-09-02 23:49:20 +02004010 if (mlxsw_sp_fi_is_gateway(mlxsw_sp, fi))
Ido Schimmel97989ee2017-03-10 08:53:38 +01004011 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
Petr Machata9b014512017-09-02 23:49:20 +02004012 else
4013 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
Ido Schimmel97989ee2017-03-10 08:53:38 +01004014 return 0;
4015 default:
4016 return -EINVAL;
4017 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02004018}
4019
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004020static struct mlxsw_sp_fib4_entry *
Ido Schimmel9aecce12017-02-09 10:28:42 +01004021mlxsw_sp_fib4_entry_create(struct mlxsw_sp *mlxsw_sp,
4022 struct mlxsw_sp_fib_node *fib_node,
4023 const struct fib_entry_notifier_info *fen_info)
Jiri Pirko5b004412016-09-01 10:37:40 +02004024{
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004025 struct mlxsw_sp_fib4_entry *fib4_entry;
Jiri Pirko5b004412016-09-01 10:37:40 +02004026 struct mlxsw_sp_fib_entry *fib_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004027 int err;
4028
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004029 fib4_entry = kzalloc(sizeof(*fib4_entry), GFP_KERNEL);
4030 if (!fib4_entry)
4031 return ERR_PTR(-ENOMEM);
4032 fib_entry = &fib4_entry->common;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004033
4034 err = mlxsw_sp_fib4_entry_type_set(mlxsw_sp, fen_info, fib_entry);
4035 if (err)
4036 goto err_fib4_entry_type_set;
4037
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02004038 err = mlxsw_sp_nexthop4_group_get(mlxsw_sp, fib_entry, fen_info->fi);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004039 if (err)
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02004040 goto err_nexthop4_group_get;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004041
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004042 fib4_entry->prio = fen_info->fi->fib_priority;
4043 fib4_entry->tb_id = fen_info->tb_id;
4044 fib4_entry->type = fen_info->type;
4045 fib4_entry->tos = fen_info->tos;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004046
4047 fib_entry->fib_node = fib_node;
4048
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004049 return fib4_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004050
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02004051err_nexthop4_group_get:
Ido Schimmel9aecce12017-02-09 10:28:42 +01004052err_fib4_entry_type_set:
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004053 kfree(fib4_entry);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004054 return ERR_PTR(err);
4055}
4056
4057static void mlxsw_sp_fib4_entry_destroy(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004058 struct mlxsw_sp_fib4_entry *fib4_entry)
Ido Schimmel9aecce12017-02-09 10:28:42 +01004059{
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02004060 mlxsw_sp_nexthop4_group_put(mlxsw_sp, &fib4_entry->common);
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004061 kfree(fib4_entry);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004062}
4063
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004064static struct mlxsw_sp_fib4_entry *
Ido Schimmel9aecce12017-02-09 10:28:42 +01004065mlxsw_sp_fib4_entry_lookup(struct mlxsw_sp *mlxsw_sp,
4066 const struct fib_entry_notifier_info *fen_info)
4067{
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004068 struct mlxsw_sp_fib4_entry *fib4_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004069 struct mlxsw_sp_fib_node *fib_node;
Ido Schimmel160e22a2017-07-18 10:10:20 +02004070 struct mlxsw_sp_fib *fib;
4071 struct mlxsw_sp_vr *vr;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004072
Ido Schimmel160e22a2017-07-18 10:10:20 +02004073 vr = mlxsw_sp_vr_find(mlxsw_sp, fen_info->tb_id);
4074 if (!vr)
4075 return NULL;
4076 fib = mlxsw_sp_vr_fib(vr, MLXSW_SP_L3_PROTO_IPV4);
4077
4078 fib_node = mlxsw_sp_fib_node_lookup(fib, &fen_info->dst,
4079 sizeof(fen_info->dst),
4080 fen_info->dst_len);
4081 if (!fib_node)
Ido Schimmel9aecce12017-02-09 10:28:42 +01004082 return NULL;
4083
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004084 list_for_each_entry(fib4_entry, &fib_node->entry_list, common.list) {
4085 if (fib4_entry->tb_id == fen_info->tb_id &&
4086 fib4_entry->tos == fen_info->tos &&
4087 fib4_entry->type == fen_info->type &&
Arkadi Sharshevskyba31d362017-08-14 21:09:19 +02004088 mlxsw_sp_nexthop4_group_fi(fib4_entry->common.nh_group) ==
4089 fen_info->fi) {
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004090 return fib4_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004091 }
4092 }
4093
4094 return NULL;
4095}
4096
4097static const struct rhashtable_params mlxsw_sp_fib_ht_params = {
4098 .key_offset = offsetof(struct mlxsw_sp_fib_node, key),
4099 .head_offset = offsetof(struct mlxsw_sp_fib_node, ht_node),
4100 .key_len = sizeof(struct mlxsw_sp_fib_key),
4101 .automatic_shrinking = true,
4102};
4103
4104static int mlxsw_sp_fib_node_insert(struct mlxsw_sp_fib *fib,
4105 struct mlxsw_sp_fib_node *fib_node)
4106{
4107 return rhashtable_insert_fast(&fib->ht, &fib_node->ht_node,
4108 mlxsw_sp_fib_ht_params);
4109}
4110
4111static void mlxsw_sp_fib_node_remove(struct mlxsw_sp_fib *fib,
4112 struct mlxsw_sp_fib_node *fib_node)
4113{
4114 rhashtable_remove_fast(&fib->ht, &fib_node->ht_node,
4115 mlxsw_sp_fib_ht_params);
4116}
4117
4118static struct mlxsw_sp_fib_node *
4119mlxsw_sp_fib_node_lookup(struct mlxsw_sp_fib *fib, const void *addr,
4120 size_t addr_len, unsigned char prefix_len)
4121{
4122 struct mlxsw_sp_fib_key key;
4123
4124 memset(&key, 0, sizeof(key));
4125 memcpy(key.addr, addr, addr_len);
4126 key.prefix_len = prefix_len;
4127 return rhashtable_lookup_fast(&fib->ht, &key, mlxsw_sp_fib_ht_params);
4128}
4129
4130static struct mlxsw_sp_fib_node *
Ido Schimmel76610eb2017-03-10 08:53:41 +01004131mlxsw_sp_fib_node_create(struct mlxsw_sp_fib *fib, const void *addr,
Ido Schimmel9aecce12017-02-09 10:28:42 +01004132 size_t addr_len, unsigned char prefix_len)
4133{
4134 struct mlxsw_sp_fib_node *fib_node;
4135
4136 fib_node = kzalloc(sizeof(*fib_node), GFP_KERNEL);
4137 if (!fib_node)
4138 return NULL;
4139
4140 INIT_LIST_HEAD(&fib_node->entry_list);
Ido Schimmel76610eb2017-03-10 08:53:41 +01004141 list_add(&fib_node->list, &fib->node_list);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004142 memcpy(fib_node->key.addr, addr, addr_len);
4143 fib_node->key.prefix_len = prefix_len;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004144
4145 return fib_node;
4146}
4147
4148static void mlxsw_sp_fib_node_destroy(struct mlxsw_sp_fib_node *fib_node)
4149{
Ido Schimmel9aecce12017-02-09 10:28:42 +01004150 list_del(&fib_node->list);
4151 WARN_ON(!list_empty(&fib_node->entry_list));
4152 kfree(fib_node);
4153}
4154
4155static bool
4156mlxsw_sp_fib_node_entry_is_first(const struct mlxsw_sp_fib_node *fib_node,
4157 const struct mlxsw_sp_fib_entry *fib_entry)
4158{
4159 return list_first_entry(&fib_node->entry_list,
4160 struct mlxsw_sp_fib_entry, list) == fib_entry;
4161}
4162
Ido Schimmelfc922bb2017-08-14 10:54:05 +02004163static int mlxsw_sp_fib_lpm_tree_link(struct mlxsw_sp *mlxsw_sp,
4164 struct mlxsw_sp_fib *fib,
4165 struct mlxsw_sp_fib_node *fib_node)
4166{
4167 struct mlxsw_sp_prefix_usage req_prefix_usage = {{ 0 } };
4168 struct mlxsw_sp_lpm_tree *lpm_tree;
4169 int err;
4170
4171 /* Since the tree is shared between all virtual routers we must
4172 * make sure it contains all the required prefix lengths. This
4173 * can be computed by either adding the new prefix length to the
4174 * existing prefix usage of a bound tree, or by aggregating the
4175 * prefix lengths across all virtual routers and adding the new
4176 * one as well.
4177 */
4178 if (fib->lpm_tree)
4179 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage,
4180 &fib->lpm_tree->prefix_usage);
4181 else
4182 mlxsw_sp_vrs_prefixes(mlxsw_sp, fib->proto, &req_prefix_usage);
4183 mlxsw_sp_prefix_usage_set(&req_prefix_usage, fib_node->key.prefix_len);
4184
4185 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
4186 fib->proto);
4187 if (IS_ERR(lpm_tree))
4188 return PTR_ERR(lpm_tree);
4189
4190 if (fib->lpm_tree && fib->lpm_tree->id == lpm_tree->id)
4191 return 0;
4192
4193 err = mlxsw_sp_vrs_lpm_tree_replace(mlxsw_sp, fib, lpm_tree);
4194 if (err)
4195 return err;
4196
4197 return 0;
4198}
4199
4200static void mlxsw_sp_fib_lpm_tree_unlink(struct mlxsw_sp *mlxsw_sp,
4201 struct mlxsw_sp_fib *fib)
4202{
Ido Schimmelfc922bb2017-08-14 10:54:05 +02004203 if (!mlxsw_sp_prefix_usage_none(&fib->prefix_usage))
4204 return;
4205 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, fib);
4206 mlxsw_sp_lpm_tree_put(mlxsw_sp, fib->lpm_tree);
4207 fib->lpm_tree = NULL;
4208}
4209
Ido Schimmel9aecce12017-02-09 10:28:42 +01004210static void mlxsw_sp_fib_node_prefix_inc(struct mlxsw_sp_fib_node *fib_node)
4211{
4212 unsigned char prefix_len = fib_node->key.prefix_len;
Ido Schimmel76610eb2017-03-10 08:53:41 +01004213 struct mlxsw_sp_fib *fib = fib_node->fib;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004214
4215 if (fib->prefix_ref_count[prefix_len]++ == 0)
4216 mlxsw_sp_prefix_usage_set(&fib->prefix_usage, prefix_len);
4217}
4218
4219static void mlxsw_sp_fib_node_prefix_dec(struct mlxsw_sp_fib_node *fib_node)
4220{
4221 unsigned char prefix_len = fib_node->key.prefix_len;
Ido Schimmel76610eb2017-03-10 08:53:41 +01004222 struct mlxsw_sp_fib *fib = fib_node->fib;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004223
4224 if (--fib->prefix_ref_count[prefix_len] == 0)
4225 mlxsw_sp_prefix_usage_clear(&fib->prefix_usage, prefix_len);
4226}
4227
Ido Schimmel76610eb2017-03-10 08:53:41 +01004228static int mlxsw_sp_fib_node_init(struct mlxsw_sp *mlxsw_sp,
4229 struct mlxsw_sp_fib_node *fib_node,
4230 struct mlxsw_sp_fib *fib)
4231{
Ido Schimmel76610eb2017-03-10 08:53:41 +01004232 int err;
4233
4234 err = mlxsw_sp_fib_node_insert(fib, fib_node);
4235 if (err)
4236 return err;
4237 fib_node->fib = fib;
4238
Ido Schimmelfc922bb2017-08-14 10:54:05 +02004239 err = mlxsw_sp_fib_lpm_tree_link(mlxsw_sp, fib, fib_node);
4240 if (err)
4241 goto err_fib_lpm_tree_link;
Ido Schimmel76610eb2017-03-10 08:53:41 +01004242
4243 mlxsw_sp_fib_node_prefix_inc(fib_node);
4244
4245 return 0;
4246
Ido Schimmelfc922bb2017-08-14 10:54:05 +02004247err_fib_lpm_tree_link:
Ido Schimmel76610eb2017-03-10 08:53:41 +01004248 fib_node->fib = NULL;
4249 mlxsw_sp_fib_node_remove(fib, fib_node);
4250 return err;
4251}
4252
4253static void mlxsw_sp_fib_node_fini(struct mlxsw_sp *mlxsw_sp,
4254 struct mlxsw_sp_fib_node *fib_node)
4255{
Ido Schimmel76610eb2017-03-10 08:53:41 +01004256 struct mlxsw_sp_fib *fib = fib_node->fib;
4257
4258 mlxsw_sp_fib_node_prefix_dec(fib_node);
Ido Schimmelfc922bb2017-08-14 10:54:05 +02004259 mlxsw_sp_fib_lpm_tree_unlink(mlxsw_sp, fib);
Ido Schimmel76610eb2017-03-10 08:53:41 +01004260 fib_node->fib = NULL;
4261 mlxsw_sp_fib_node_remove(fib, fib_node);
4262}
4263
Ido Schimmel9aecce12017-02-09 10:28:42 +01004264static struct mlxsw_sp_fib_node *
Ido Schimmel731ea1c2017-07-18 10:10:21 +02004265mlxsw_sp_fib_node_get(struct mlxsw_sp *mlxsw_sp, u32 tb_id, const void *addr,
4266 size_t addr_len, unsigned char prefix_len,
4267 enum mlxsw_sp_l3proto proto)
Ido Schimmel9aecce12017-02-09 10:28:42 +01004268{
4269 struct mlxsw_sp_fib_node *fib_node;
Ido Schimmel76610eb2017-03-10 08:53:41 +01004270 struct mlxsw_sp_fib *fib;
Jiri Pirko5b004412016-09-01 10:37:40 +02004271 struct mlxsw_sp_vr *vr;
4272 int err;
4273
David Ahernf8fa9b42017-10-18 09:56:56 -07004274 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id, NULL);
Jiri Pirko5b004412016-09-01 10:37:40 +02004275 if (IS_ERR(vr))
4276 return ERR_CAST(vr);
Ido Schimmel731ea1c2017-07-18 10:10:21 +02004277 fib = mlxsw_sp_vr_fib(vr, proto);
Jiri Pirko5b004412016-09-01 10:37:40 +02004278
Ido Schimmel731ea1c2017-07-18 10:10:21 +02004279 fib_node = mlxsw_sp_fib_node_lookup(fib, addr, addr_len, prefix_len);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004280 if (fib_node)
4281 return fib_node;
4282
Ido Schimmel731ea1c2017-07-18 10:10:21 +02004283 fib_node = mlxsw_sp_fib_node_create(fib, addr, addr_len, prefix_len);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004284 if (!fib_node) {
Jiri Pirko5b004412016-09-01 10:37:40 +02004285 err = -ENOMEM;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004286 goto err_fib_node_create;
Jiri Pirko5b004412016-09-01 10:37:40 +02004287 }
Jiri Pirko5b004412016-09-01 10:37:40 +02004288
Ido Schimmel76610eb2017-03-10 08:53:41 +01004289 err = mlxsw_sp_fib_node_init(mlxsw_sp, fib_node, fib);
4290 if (err)
4291 goto err_fib_node_init;
4292
Ido Schimmel9aecce12017-02-09 10:28:42 +01004293 return fib_node;
Jiri Pirko5b004412016-09-01 10:37:40 +02004294
Ido Schimmel76610eb2017-03-10 08:53:41 +01004295err_fib_node_init:
4296 mlxsw_sp_fib_node_destroy(fib_node);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004297err_fib_node_create:
Ido Schimmel76610eb2017-03-10 08:53:41 +01004298 mlxsw_sp_vr_put(vr);
Jiri Pirko5b004412016-09-01 10:37:40 +02004299 return ERR_PTR(err);
4300}
4301
Ido Schimmel731ea1c2017-07-18 10:10:21 +02004302static void mlxsw_sp_fib_node_put(struct mlxsw_sp *mlxsw_sp,
4303 struct mlxsw_sp_fib_node *fib_node)
Jiri Pirko5b004412016-09-01 10:37:40 +02004304{
Ido Schimmel76610eb2017-03-10 08:53:41 +01004305 struct mlxsw_sp_vr *vr = fib_node->fib->vr;
Jiri Pirko5b004412016-09-01 10:37:40 +02004306
Ido Schimmel9aecce12017-02-09 10:28:42 +01004307 if (!list_empty(&fib_node->entry_list))
4308 return;
Ido Schimmel76610eb2017-03-10 08:53:41 +01004309 mlxsw_sp_fib_node_fini(mlxsw_sp, fib_node);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004310 mlxsw_sp_fib_node_destroy(fib_node);
Ido Schimmel76610eb2017-03-10 08:53:41 +01004311 mlxsw_sp_vr_put(vr);
Jiri Pirko5b004412016-09-01 10:37:40 +02004312}
4313
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004314static struct mlxsw_sp_fib4_entry *
Ido Schimmel9aecce12017-02-09 10:28:42 +01004315mlxsw_sp_fib4_node_entry_find(const struct mlxsw_sp_fib_node *fib_node,
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004316 const struct mlxsw_sp_fib4_entry *new4_entry)
Jiri Pirko61c503f2016-07-04 08:23:11 +02004317{
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004318 struct mlxsw_sp_fib4_entry *fib4_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004319
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004320 list_for_each_entry(fib4_entry, &fib_node->entry_list, common.list) {
4321 if (fib4_entry->tb_id > new4_entry->tb_id)
Ido Schimmel9aecce12017-02-09 10:28:42 +01004322 continue;
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004323 if (fib4_entry->tb_id != new4_entry->tb_id)
Ido Schimmel9aecce12017-02-09 10:28:42 +01004324 break;
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004325 if (fib4_entry->tos > new4_entry->tos)
Ido Schimmel9aecce12017-02-09 10:28:42 +01004326 continue;
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004327 if (fib4_entry->prio >= new4_entry->prio ||
4328 fib4_entry->tos < new4_entry->tos)
4329 return fib4_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004330 }
4331
4332 return NULL;
4333}
4334
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004335static int
4336mlxsw_sp_fib4_node_list_append(struct mlxsw_sp_fib4_entry *fib4_entry,
4337 struct mlxsw_sp_fib4_entry *new4_entry)
Ido Schimmel4283bce2017-02-09 10:28:43 +01004338{
4339 struct mlxsw_sp_fib_node *fib_node;
4340
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004341 if (WARN_ON(!fib4_entry))
Ido Schimmel4283bce2017-02-09 10:28:43 +01004342 return -EINVAL;
4343
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004344 fib_node = fib4_entry->common.fib_node;
4345 list_for_each_entry_from(fib4_entry, &fib_node->entry_list,
4346 common.list) {
4347 if (fib4_entry->tb_id != new4_entry->tb_id ||
4348 fib4_entry->tos != new4_entry->tos ||
4349 fib4_entry->prio != new4_entry->prio)
Ido Schimmel4283bce2017-02-09 10:28:43 +01004350 break;
4351 }
4352
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004353 list_add_tail(&new4_entry->common.list, &fib4_entry->common.list);
Ido Schimmel4283bce2017-02-09 10:28:43 +01004354 return 0;
4355}
4356
Ido Schimmel9aecce12017-02-09 10:28:42 +01004357static int
Ido Schimmel9efbee62017-07-18 10:10:28 +02004358mlxsw_sp_fib4_node_list_insert(struct mlxsw_sp_fib4_entry *new4_entry,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01004359 bool replace, bool append)
Ido Schimmel9aecce12017-02-09 10:28:42 +01004360{
Ido Schimmel9efbee62017-07-18 10:10:28 +02004361 struct mlxsw_sp_fib_node *fib_node = new4_entry->common.fib_node;
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004362 struct mlxsw_sp_fib4_entry *fib4_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004363
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004364 fib4_entry = mlxsw_sp_fib4_node_entry_find(fib_node, new4_entry);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004365
Ido Schimmel4283bce2017-02-09 10:28:43 +01004366 if (append)
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004367 return mlxsw_sp_fib4_node_list_append(fib4_entry, new4_entry);
4368 if (replace && WARN_ON(!fib4_entry))
Ido Schimmel599cf8f2017-02-09 10:28:44 +01004369 return -EINVAL;
Ido Schimmel4283bce2017-02-09 10:28:43 +01004370
Ido Schimmel599cf8f2017-02-09 10:28:44 +01004371 /* Insert new entry before replaced one, so that we can later
4372 * remove the second.
4373 */
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004374 if (fib4_entry) {
4375 list_add_tail(&new4_entry->common.list,
4376 &fib4_entry->common.list);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004377 } else {
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004378 struct mlxsw_sp_fib4_entry *last;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004379
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004380 list_for_each_entry(last, &fib_node->entry_list, common.list) {
4381 if (new4_entry->tb_id > last->tb_id)
Ido Schimmel9aecce12017-02-09 10:28:42 +01004382 break;
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004383 fib4_entry = last;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004384 }
4385
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004386 if (fib4_entry)
4387 list_add(&new4_entry->common.list,
4388 &fib4_entry->common.list);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004389 else
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004390 list_add(&new4_entry->common.list,
4391 &fib_node->entry_list);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004392 }
4393
4394 return 0;
4395}
4396
4397static void
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004398mlxsw_sp_fib4_node_list_remove(struct mlxsw_sp_fib4_entry *fib4_entry)
Ido Schimmel9aecce12017-02-09 10:28:42 +01004399{
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004400 list_del(&fib4_entry->common.list);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004401}
4402
Ido Schimmel80c238f2017-07-18 10:10:29 +02004403static int mlxsw_sp_fib_node_entry_add(struct mlxsw_sp *mlxsw_sp,
4404 struct mlxsw_sp_fib_entry *fib_entry)
Ido Schimmel9aecce12017-02-09 10:28:42 +01004405{
Ido Schimmel9efbee62017-07-18 10:10:28 +02004406 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
4407
Ido Schimmel9aecce12017-02-09 10:28:42 +01004408 if (!mlxsw_sp_fib_node_entry_is_first(fib_node, fib_entry))
4409 return 0;
4410
4411 /* To prevent packet loss, overwrite the previously offloaded
4412 * entry.
4413 */
4414 if (!list_is_singular(&fib_node->entry_list)) {
4415 enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_DELETE;
4416 struct mlxsw_sp_fib_entry *n = list_next_entry(fib_entry, list);
4417
4418 mlxsw_sp_fib_entry_offload_refresh(n, op, 0);
4419 }
4420
4421 return mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
4422}
4423
Ido Schimmel80c238f2017-07-18 10:10:29 +02004424static void mlxsw_sp_fib_node_entry_del(struct mlxsw_sp *mlxsw_sp,
4425 struct mlxsw_sp_fib_entry *fib_entry)
Ido Schimmel9aecce12017-02-09 10:28:42 +01004426{
Ido Schimmel9efbee62017-07-18 10:10:28 +02004427 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
4428
Ido Schimmel9aecce12017-02-09 10:28:42 +01004429 if (!mlxsw_sp_fib_node_entry_is_first(fib_node, fib_entry))
4430 return;
4431
4432 /* Promote the next entry by overwriting the deleted entry */
4433 if (!list_is_singular(&fib_node->entry_list)) {
4434 struct mlxsw_sp_fib_entry *n = list_next_entry(fib_entry, list);
4435 enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_DELETE;
4436
4437 mlxsw_sp_fib_entry_update(mlxsw_sp, n);
4438 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, 0);
4439 return;
4440 }
4441
4442 mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry);
4443}
4444
4445static int mlxsw_sp_fib4_node_entry_link(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004446 struct mlxsw_sp_fib4_entry *fib4_entry,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01004447 bool replace, bool append)
Ido Schimmel9aecce12017-02-09 10:28:42 +01004448{
Ido Schimmel9aecce12017-02-09 10:28:42 +01004449 int err;
4450
Ido Schimmel9efbee62017-07-18 10:10:28 +02004451 err = mlxsw_sp_fib4_node_list_insert(fib4_entry, replace, append);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004452 if (err)
4453 return err;
4454
Ido Schimmel80c238f2017-07-18 10:10:29 +02004455 err = mlxsw_sp_fib_node_entry_add(mlxsw_sp, &fib4_entry->common);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004456 if (err)
Ido Schimmel80c238f2017-07-18 10:10:29 +02004457 goto err_fib_node_entry_add;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004458
Ido Schimmel9aecce12017-02-09 10:28:42 +01004459 return 0;
4460
Ido Schimmel80c238f2017-07-18 10:10:29 +02004461err_fib_node_entry_add:
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004462 mlxsw_sp_fib4_node_list_remove(fib4_entry);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004463 return err;
4464}
4465
4466static void
4467mlxsw_sp_fib4_node_entry_unlink(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004468 struct mlxsw_sp_fib4_entry *fib4_entry)
Ido Schimmel9aecce12017-02-09 10:28:42 +01004469{
Ido Schimmel80c238f2017-07-18 10:10:29 +02004470 mlxsw_sp_fib_node_entry_del(mlxsw_sp, &fib4_entry->common);
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004471 mlxsw_sp_fib4_node_list_remove(fib4_entry);
Petr Machata4607f6d2017-09-02 23:49:25 +02004472
4473 if (fib4_entry->common.type == MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP)
4474 mlxsw_sp_fib_entry_decap_fini(mlxsw_sp, &fib4_entry->common);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004475}
4476
Ido Schimmel599cf8f2017-02-09 10:28:44 +01004477static void mlxsw_sp_fib4_entry_replace(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004478 struct mlxsw_sp_fib4_entry *fib4_entry,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01004479 bool replace)
4480{
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004481 struct mlxsw_sp_fib_node *fib_node = fib4_entry->common.fib_node;
4482 struct mlxsw_sp_fib4_entry *replaced;
Ido Schimmel599cf8f2017-02-09 10:28:44 +01004483
4484 if (!replace)
4485 return;
4486
4487 /* We inserted the new entry before replaced one */
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004488 replaced = list_next_entry(fib4_entry, common.list);
Ido Schimmel599cf8f2017-02-09 10:28:44 +01004489
4490 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, replaced);
4491 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, replaced);
Ido Schimmel731ea1c2017-07-18 10:10:21 +02004492 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
Ido Schimmel599cf8f2017-02-09 10:28:44 +01004493}
4494
Ido Schimmel9aecce12017-02-09 10:28:42 +01004495static int
4496mlxsw_sp_router_fib4_add(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel4283bce2017-02-09 10:28:43 +01004497 const struct fib_entry_notifier_info *fen_info,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01004498 bool replace, bool append)
Ido Schimmel9aecce12017-02-09 10:28:42 +01004499{
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004500 struct mlxsw_sp_fib4_entry *fib4_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004501 struct mlxsw_sp_fib_node *fib_node;
Jiri Pirko61c503f2016-07-04 08:23:11 +02004502 int err;
4503
Ido Schimmel9011b672017-05-16 19:38:25 +02004504 if (mlxsw_sp->router->aborted)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004505 return 0;
4506
Ido Schimmel731ea1c2017-07-18 10:10:21 +02004507 fib_node = mlxsw_sp_fib_node_get(mlxsw_sp, fen_info->tb_id,
4508 &fen_info->dst, sizeof(fen_info->dst),
4509 fen_info->dst_len,
4510 MLXSW_SP_L3_PROTO_IPV4);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004511 if (IS_ERR(fib_node)) {
4512 dev_warn(mlxsw_sp->bus_info->dev, "Failed to get FIB node\n");
4513 return PTR_ERR(fib_node);
4514 }
4515
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004516 fib4_entry = mlxsw_sp_fib4_entry_create(mlxsw_sp, fib_node, fen_info);
4517 if (IS_ERR(fib4_entry)) {
Ido Schimmel9aecce12017-02-09 10:28:42 +01004518 dev_warn(mlxsw_sp->bus_info->dev, "Failed to create FIB entry\n");
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004519 err = PTR_ERR(fib4_entry);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004520 goto err_fib4_entry_create;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004521 }
Jiri Pirko61c503f2016-07-04 08:23:11 +02004522
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004523 err = mlxsw_sp_fib4_node_entry_link(mlxsw_sp, fib4_entry, replace,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01004524 append);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004525 if (err) {
Ido Schimmel9aecce12017-02-09 10:28:42 +01004526 dev_warn(mlxsw_sp->bus_info->dev, "Failed to link FIB entry to node\n");
4527 goto err_fib4_node_entry_link;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004528 }
Ido Schimmel9aecce12017-02-09 10:28:42 +01004529
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004530 mlxsw_sp_fib4_entry_replace(mlxsw_sp, fib4_entry, replace);
Ido Schimmel599cf8f2017-02-09 10:28:44 +01004531
Jiri Pirko61c503f2016-07-04 08:23:11 +02004532 return 0;
4533
Ido Schimmel9aecce12017-02-09 10:28:42 +01004534err_fib4_node_entry_link:
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004535 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
Ido Schimmel9aecce12017-02-09 10:28:42 +01004536err_fib4_entry_create:
Ido Schimmel731ea1c2017-07-18 10:10:21 +02004537 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
Jiri Pirko61c503f2016-07-04 08:23:11 +02004538 return err;
4539}
4540
Jiri Pirko37956d72016-10-20 16:05:43 +02004541static void mlxsw_sp_router_fib4_del(struct mlxsw_sp *mlxsw_sp,
4542 struct fib_entry_notifier_info *fen_info)
Jiri Pirko61c503f2016-07-04 08:23:11 +02004543{
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004544 struct mlxsw_sp_fib4_entry *fib4_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01004545 struct mlxsw_sp_fib_node *fib_node;
Jiri Pirko61c503f2016-07-04 08:23:11 +02004546
Ido Schimmel9011b672017-05-16 19:38:25 +02004547 if (mlxsw_sp->router->aborted)
Jiri Pirko37956d72016-10-20 16:05:43 +02004548 return;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004549
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004550 fib4_entry = mlxsw_sp_fib4_entry_lookup(mlxsw_sp, fen_info);
4551 if (WARN_ON(!fib4_entry))
Jiri Pirko37956d72016-10-20 16:05:43 +02004552 return;
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004553 fib_node = fib4_entry->common.fib_node;
Jiri Pirko5b004412016-09-01 10:37:40 +02004554
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02004555 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, fib4_entry);
4556 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
Ido Schimmel731ea1c2017-07-18 10:10:21 +02004557 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
Jiri Pirko61c503f2016-07-04 08:23:11 +02004558}
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004559
Ido Schimmel428b8512017-08-03 13:28:28 +02004560static bool mlxsw_sp_fib6_rt_should_ignore(const struct rt6_info *rt)
4561{
4562 /* Packets with link-local destination IP arriving to the router
4563 * are trapped to the CPU, so no need to program specific routes
4564 * for them.
4565 */
4566 if (ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LINKLOCAL)
4567 return true;
4568
4569 /* Multicast routes aren't supported, so ignore them. Neighbour
4570 * Discovery packets are specifically trapped.
4571 */
4572 if (ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_MULTICAST)
4573 return true;
4574
4575 /* Cloned routes are irrelevant in the forwarding path. */
4576 if (rt->rt6i_flags & RTF_CACHE)
4577 return true;
4578
4579 return false;
4580}
4581
4582static struct mlxsw_sp_rt6 *mlxsw_sp_rt6_create(struct rt6_info *rt)
4583{
4584 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
4585
4586 mlxsw_sp_rt6 = kzalloc(sizeof(*mlxsw_sp_rt6), GFP_KERNEL);
4587 if (!mlxsw_sp_rt6)
4588 return ERR_PTR(-ENOMEM);
4589
4590 /* In case of route replace, replaced route is deleted with
4591 * no notification. Take reference to prevent accessing freed
4592 * memory.
4593 */
4594 mlxsw_sp_rt6->rt = rt;
4595 rt6_hold(rt);
4596
4597 return mlxsw_sp_rt6;
4598}
4599
4600#if IS_ENABLED(CONFIG_IPV6)
4601static void mlxsw_sp_rt6_release(struct rt6_info *rt)
4602{
4603 rt6_release(rt);
4604}
4605#else
4606static void mlxsw_sp_rt6_release(struct rt6_info *rt)
4607{
4608}
4609#endif
4610
4611static void mlxsw_sp_rt6_destroy(struct mlxsw_sp_rt6 *mlxsw_sp_rt6)
4612{
4613 mlxsw_sp_rt6_release(mlxsw_sp_rt6->rt);
4614 kfree(mlxsw_sp_rt6);
4615}
4616
4617static bool mlxsw_sp_fib6_rt_can_mp(const struct rt6_info *rt)
4618{
4619 /* RTF_CACHE routes are ignored */
4620 return (rt->rt6i_flags & (RTF_GATEWAY | RTF_ADDRCONF)) == RTF_GATEWAY;
4621}
4622
4623static struct rt6_info *
4624mlxsw_sp_fib6_entry_rt(const struct mlxsw_sp_fib6_entry *fib6_entry)
4625{
4626 return list_first_entry(&fib6_entry->rt6_list, struct mlxsw_sp_rt6,
4627 list)->rt;
4628}
4629
4630static struct mlxsw_sp_fib6_entry *
4631mlxsw_sp_fib6_node_mp_entry_find(const struct mlxsw_sp_fib_node *fib_node,
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02004632 const struct rt6_info *nrt, bool replace)
Ido Schimmel428b8512017-08-03 13:28:28 +02004633{
4634 struct mlxsw_sp_fib6_entry *fib6_entry;
4635
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02004636 if (!mlxsw_sp_fib6_rt_can_mp(nrt) || replace)
Ido Schimmel428b8512017-08-03 13:28:28 +02004637 return NULL;
4638
4639 list_for_each_entry(fib6_entry, &fib_node->entry_list, common.list) {
4640 struct rt6_info *rt = mlxsw_sp_fib6_entry_rt(fib6_entry);
4641
4642 /* RT6_TABLE_LOCAL and RT6_TABLE_MAIN share the same
4643 * virtual router.
4644 */
4645 if (rt->rt6i_table->tb6_id > nrt->rt6i_table->tb6_id)
4646 continue;
4647 if (rt->rt6i_table->tb6_id != nrt->rt6i_table->tb6_id)
4648 break;
4649 if (rt->rt6i_metric < nrt->rt6i_metric)
4650 continue;
4651 if (rt->rt6i_metric == nrt->rt6i_metric &&
4652 mlxsw_sp_fib6_rt_can_mp(rt))
4653 return fib6_entry;
4654 if (rt->rt6i_metric > nrt->rt6i_metric)
4655 break;
4656 }
4657
4658 return NULL;
4659}
4660
4661static struct mlxsw_sp_rt6 *
4662mlxsw_sp_fib6_entry_rt_find(const struct mlxsw_sp_fib6_entry *fib6_entry,
4663 const struct rt6_info *rt)
4664{
4665 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
4666
4667 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
4668 if (mlxsw_sp_rt6->rt == rt)
4669 return mlxsw_sp_rt6;
4670 }
4671
4672 return NULL;
4673}
4674
Petr Machata8f28a302017-09-02 23:49:24 +02004675static bool mlxsw_sp_nexthop6_ipip_type(const struct mlxsw_sp *mlxsw_sp,
4676 const struct rt6_info *rt,
4677 enum mlxsw_sp_ipip_type *ret)
4678{
4679 return rt->dst.dev &&
4680 mlxsw_sp_netdev_ipip_type(mlxsw_sp, rt->dst.dev, ret);
4681}
4682
Petr Machata35225e42017-09-02 23:49:22 +02004683static int mlxsw_sp_nexthop6_type_init(struct mlxsw_sp *mlxsw_sp,
4684 struct mlxsw_sp_nexthop_group *nh_grp,
4685 struct mlxsw_sp_nexthop *nh,
4686 const struct rt6_info *rt)
Ido Schimmel428b8512017-08-03 13:28:28 +02004687{
Petr Machata8f28a302017-09-02 23:49:24 +02004688 struct mlxsw_sp_router *router = mlxsw_sp->router;
Ido Schimmel428b8512017-08-03 13:28:28 +02004689 struct net_device *dev = rt->dst.dev;
Petr Machata8f28a302017-09-02 23:49:24 +02004690 enum mlxsw_sp_ipip_type ipipt;
Ido Schimmel428b8512017-08-03 13:28:28 +02004691 struct mlxsw_sp_rif *rif;
4692 int err;
4693
Petr Machata8f28a302017-09-02 23:49:24 +02004694 if (mlxsw_sp_nexthop6_ipip_type(mlxsw_sp, rt, &ipipt) &&
4695 router->ipip_ops_arr[ipipt]->can_offload(mlxsw_sp, dev,
4696 MLXSW_SP_L3_PROTO_IPV6)) {
4697 nh->type = MLXSW_SP_NEXTHOP_TYPE_IPIP;
Petr Machata4cccb732017-10-16 16:26:39 +02004698 err = mlxsw_sp_nexthop_ipip_init(mlxsw_sp, nh, dev);
Petr Machatade0f43c2017-10-02 12:14:57 +02004699 if (err)
4700 return err;
4701 mlxsw_sp_nexthop_rif_init(nh, &nh->ipip_entry->ol_lb->common);
4702 return 0;
Petr Machata8f28a302017-09-02 23:49:24 +02004703 }
4704
Petr Machata35225e42017-09-02 23:49:22 +02004705 nh->type = MLXSW_SP_NEXTHOP_TYPE_ETH;
Ido Schimmel428b8512017-08-03 13:28:28 +02004706 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
4707 if (!rif)
4708 return 0;
4709 mlxsw_sp_nexthop_rif_init(nh, rif);
4710
4711 err = mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
4712 if (err)
4713 goto err_nexthop_neigh_init;
4714
4715 return 0;
4716
4717err_nexthop_neigh_init:
4718 mlxsw_sp_nexthop_rif_fini(nh);
4719 return err;
4720}
4721
Petr Machata35225e42017-09-02 23:49:22 +02004722static void mlxsw_sp_nexthop6_type_fini(struct mlxsw_sp *mlxsw_sp,
4723 struct mlxsw_sp_nexthop *nh)
4724{
4725 mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
4726}
4727
4728static int mlxsw_sp_nexthop6_init(struct mlxsw_sp *mlxsw_sp,
4729 struct mlxsw_sp_nexthop_group *nh_grp,
4730 struct mlxsw_sp_nexthop *nh,
4731 const struct rt6_info *rt)
4732{
4733 struct net_device *dev = rt->dst.dev;
4734
4735 nh->nh_grp = nh_grp;
Ido Schimmel408bd942017-10-22 23:11:46 +02004736 nh->nh_weight = 1;
Petr Machata35225e42017-09-02 23:49:22 +02004737 memcpy(&nh->gw_addr, &rt->rt6i_gateway, sizeof(nh->gw_addr));
Arkadi Sharshevskya5390272017-09-25 10:32:28 +02004738 mlxsw_sp_nexthop_counter_alloc(mlxsw_sp, nh);
Petr Machata35225e42017-09-02 23:49:22 +02004739
Arkadi Sharshevskydbe45982017-09-25 10:32:23 +02004740 list_add_tail(&nh->router_list_node, &mlxsw_sp->router->nexthop_list);
4741
Petr Machata35225e42017-09-02 23:49:22 +02004742 if (!dev)
4743 return 0;
4744 nh->ifindex = dev->ifindex;
4745
4746 return mlxsw_sp_nexthop6_type_init(mlxsw_sp, nh_grp, nh, rt);
4747}
4748
Ido Schimmel428b8512017-08-03 13:28:28 +02004749static void mlxsw_sp_nexthop6_fini(struct mlxsw_sp *mlxsw_sp,
4750 struct mlxsw_sp_nexthop *nh)
4751{
Petr Machata35225e42017-09-02 23:49:22 +02004752 mlxsw_sp_nexthop6_type_fini(mlxsw_sp, nh);
Arkadi Sharshevskydbe45982017-09-25 10:32:23 +02004753 list_del(&nh->router_list_node);
Arkadi Sharshevskya5390272017-09-25 10:32:28 +02004754 mlxsw_sp_nexthop_counter_free(mlxsw_sp, nh);
Ido Schimmel428b8512017-08-03 13:28:28 +02004755}
4756
Petr Machataf6050ee2017-09-02 23:49:21 +02004757static bool mlxsw_sp_rt6_is_gateway(const struct mlxsw_sp *mlxsw_sp,
4758 const struct rt6_info *rt)
4759{
Petr Machata8f28a302017-09-02 23:49:24 +02004760 return rt->rt6i_flags & RTF_GATEWAY ||
4761 mlxsw_sp_nexthop6_ipip_type(mlxsw_sp, rt, NULL);
Petr Machataf6050ee2017-09-02 23:49:21 +02004762}
4763
Ido Schimmel428b8512017-08-03 13:28:28 +02004764static struct mlxsw_sp_nexthop_group *
4765mlxsw_sp_nexthop6_group_create(struct mlxsw_sp *mlxsw_sp,
4766 struct mlxsw_sp_fib6_entry *fib6_entry)
4767{
4768 struct mlxsw_sp_nexthop_group *nh_grp;
4769 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
4770 struct mlxsw_sp_nexthop *nh;
4771 size_t alloc_size;
4772 int i = 0;
4773 int err;
4774
4775 alloc_size = sizeof(*nh_grp) +
4776 fib6_entry->nrt6 * sizeof(struct mlxsw_sp_nexthop);
4777 nh_grp = kzalloc(alloc_size, GFP_KERNEL);
4778 if (!nh_grp)
4779 return ERR_PTR(-ENOMEM);
4780 INIT_LIST_HEAD(&nh_grp->fib_list);
4781#if IS_ENABLED(CONFIG_IPV6)
4782 nh_grp->neigh_tbl = &nd_tbl;
4783#endif
4784 mlxsw_sp_rt6 = list_first_entry(&fib6_entry->rt6_list,
4785 struct mlxsw_sp_rt6, list);
Petr Machataf6050ee2017-09-02 23:49:21 +02004786 nh_grp->gateway = mlxsw_sp_rt6_is_gateway(mlxsw_sp, mlxsw_sp_rt6->rt);
Ido Schimmel428b8512017-08-03 13:28:28 +02004787 nh_grp->count = fib6_entry->nrt6;
4788 for (i = 0; i < nh_grp->count; i++) {
4789 struct rt6_info *rt = mlxsw_sp_rt6->rt;
4790
4791 nh = &nh_grp->nexthops[i];
4792 err = mlxsw_sp_nexthop6_init(mlxsw_sp, nh_grp, nh, rt);
4793 if (err)
4794 goto err_nexthop6_init;
4795 mlxsw_sp_rt6 = list_next_entry(mlxsw_sp_rt6, list);
4796 }
Arkadi Sharshevskye6f3b372017-08-14 21:09:20 +02004797
4798 err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp);
4799 if (err)
4800 goto err_nexthop_group_insert;
4801
Ido Schimmel428b8512017-08-03 13:28:28 +02004802 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
4803 return nh_grp;
4804
Arkadi Sharshevskye6f3b372017-08-14 21:09:20 +02004805err_nexthop_group_insert:
Ido Schimmel428b8512017-08-03 13:28:28 +02004806err_nexthop6_init:
4807 for (i--; i >= 0; i--) {
4808 nh = &nh_grp->nexthops[i];
4809 mlxsw_sp_nexthop6_fini(mlxsw_sp, nh);
4810 }
4811 kfree(nh_grp);
4812 return ERR_PTR(err);
4813}
4814
4815static void
4816mlxsw_sp_nexthop6_group_destroy(struct mlxsw_sp *mlxsw_sp,
4817 struct mlxsw_sp_nexthop_group *nh_grp)
4818{
4819 struct mlxsw_sp_nexthop *nh;
4820 int i = nh_grp->count;
4821
Arkadi Sharshevskye6f3b372017-08-14 21:09:20 +02004822 mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp);
Ido Schimmel428b8512017-08-03 13:28:28 +02004823 for (i--; i >= 0; i--) {
4824 nh = &nh_grp->nexthops[i];
4825 mlxsw_sp_nexthop6_fini(mlxsw_sp, nh);
4826 }
4827 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
4828 WARN_ON(nh_grp->adj_index_valid);
4829 kfree(nh_grp);
4830}
4831
4832static int mlxsw_sp_nexthop6_group_get(struct mlxsw_sp *mlxsw_sp,
4833 struct mlxsw_sp_fib6_entry *fib6_entry)
4834{
4835 struct mlxsw_sp_nexthop_group *nh_grp;
4836
Arkadi Sharshevskye6f3b372017-08-14 21:09:20 +02004837 nh_grp = mlxsw_sp_nexthop6_group_lookup(mlxsw_sp, fib6_entry);
4838 if (!nh_grp) {
4839 nh_grp = mlxsw_sp_nexthop6_group_create(mlxsw_sp, fib6_entry);
4840 if (IS_ERR(nh_grp))
4841 return PTR_ERR(nh_grp);
4842 }
Ido Schimmel428b8512017-08-03 13:28:28 +02004843
4844 list_add_tail(&fib6_entry->common.nexthop_group_node,
4845 &nh_grp->fib_list);
4846 fib6_entry->common.nh_group = nh_grp;
4847
4848 return 0;
4849}
4850
4851static void mlxsw_sp_nexthop6_group_put(struct mlxsw_sp *mlxsw_sp,
4852 struct mlxsw_sp_fib_entry *fib_entry)
4853{
4854 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
4855
4856 list_del(&fib_entry->nexthop_group_node);
4857 if (!list_empty(&nh_grp->fib_list))
4858 return;
4859 mlxsw_sp_nexthop6_group_destroy(mlxsw_sp, nh_grp);
4860}
4861
4862static int
4863mlxsw_sp_nexthop6_group_update(struct mlxsw_sp *mlxsw_sp,
4864 struct mlxsw_sp_fib6_entry *fib6_entry)
4865{
4866 struct mlxsw_sp_nexthop_group *old_nh_grp = fib6_entry->common.nh_group;
4867 int err;
4868
4869 fib6_entry->common.nh_group = NULL;
4870 list_del(&fib6_entry->common.nexthop_group_node);
4871
4872 err = mlxsw_sp_nexthop6_group_get(mlxsw_sp, fib6_entry);
4873 if (err)
4874 goto err_nexthop6_group_get;
4875
4876 /* In case this entry is offloaded, then the adjacency index
4877 * currently associated with it in the device's table is that
4878 * of the old group. Start using the new one instead.
4879 */
4880 err = mlxsw_sp_fib_node_entry_add(mlxsw_sp, &fib6_entry->common);
4881 if (err)
4882 goto err_fib_node_entry_add;
4883
4884 if (list_empty(&old_nh_grp->fib_list))
4885 mlxsw_sp_nexthop6_group_destroy(mlxsw_sp, old_nh_grp);
4886
4887 return 0;
4888
4889err_fib_node_entry_add:
4890 mlxsw_sp_nexthop6_group_put(mlxsw_sp, &fib6_entry->common);
4891err_nexthop6_group_get:
4892 list_add_tail(&fib6_entry->common.nexthop_group_node,
4893 &old_nh_grp->fib_list);
4894 fib6_entry->common.nh_group = old_nh_grp;
4895 return err;
4896}
4897
4898static int
4899mlxsw_sp_fib6_entry_nexthop_add(struct mlxsw_sp *mlxsw_sp,
4900 struct mlxsw_sp_fib6_entry *fib6_entry,
4901 struct rt6_info *rt)
4902{
4903 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
4904 int err;
4905
4906 mlxsw_sp_rt6 = mlxsw_sp_rt6_create(rt);
4907 if (IS_ERR(mlxsw_sp_rt6))
4908 return PTR_ERR(mlxsw_sp_rt6);
4909
4910 list_add_tail(&mlxsw_sp_rt6->list, &fib6_entry->rt6_list);
4911 fib6_entry->nrt6++;
4912
4913 err = mlxsw_sp_nexthop6_group_update(mlxsw_sp, fib6_entry);
4914 if (err)
4915 goto err_nexthop6_group_update;
4916
4917 return 0;
4918
4919err_nexthop6_group_update:
4920 fib6_entry->nrt6--;
4921 list_del(&mlxsw_sp_rt6->list);
4922 mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
4923 return err;
4924}
4925
4926static void
4927mlxsw_sp_fib6_entry_nexthop_del(struct mlxsw_sp *mlxsw_sp,
4928 struct mlxsw_sp_fib6_entry *fib6_entry,
4929 struct rt6_info *rt)
4930{
4931 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
4932
4933 mlxsw_sp_rt6 = mlxsw_sp_fib6_entry_rt_find(fib6_entry, rt);
4934 if (WARN_ON(!mlxsw_sp_rt6))
4935 return;
4936
4937 fib6_entry->nrt6--;
4938 list_del(&mlxsw_sp_rt6->list);
4939 mlxsw_sp_nexthop6_group_update(mlxsw_sp, fib6_entry);
4940 mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
4941}
4942
Petr Machataf6050ee2017-09-02 23:49:21 +02004943static void mlxsw_sp_fib6_entry_type_set(struct mlxsw_sp *mlxsw_sp,
4944 struct mlxsw_sp_fib_entry *fib_entry,
Ido Schimmel428b8512017-08-03 13:28:28 +02004945 const struct rt6_info *rt)
4946{
4947 /* Packets hitting RTF_REJECT routes need to be discarded by the
4948 * stack. We can rely on their destination device not having a
4949 * RIF (it's the loopback device) and can thus use action type
4950 * local, which will cause them to be trapped with a lower
4951 * priority than packets that need to be locally received.
4952 */
Ido Schimmeld3b6d372017-09-01 10:58:55 +02004953 if (rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel428b8512017-08-03 13:28:28 +02004954 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
4955 else if (rt->rt6i_flags & RTF_REJECT)
4956 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
Petr Machataf6050ee2017-09-02 23:49:21 +02004957 else if (mlxsw_sp_rt6_is_gateway(mlxsw_sp, rt))
Ido Schimmel428b8512017-08-03 13:28:28 +02004958 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
4959 else
4960 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
4961}
4962
4963static void
4964mlxsw_sp_fib6_entry_rt_destroy_all(struct mlxsw_sp_fib6_entry *fib6_entry)
4965{
4966 struct mlxsw_sp_rt6 *mlxsw_sp_rt6, *tmp;
4967
4968 list_for_each_entry_safe(mlxsw_sp_rt6, tmp, &fib6_entry->rt6_list,
4969 list) {
4970 fib6_entry->nrt6--;
4971 list_del(&mlxsw_sp_rt6->list);
4972 mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
4973 }
4974}
4975
4976static struct mlxsw_sp_fib6_entry *
4977mlxsw_sp_fib6_entry_create(struct mlxsw_sp *mlxsw_sp,
4978 struct mlxsw_sp_fib_node *fib_node,
4979 struct rt6_info *rt)
4980{
4981 struct mlxsw_sp_fib6_entry *fib6_entry;
4982 struct mlxsw_sp_fib_entry *fib_entry;
4983 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
4984 int err;
4985
4986 fib6_entry = kzalloc(sizeof(*fib6_entry), GFP_KERNEL);
4987 if (!fib6_entry)
4988 return ERR_PTR(-ENOMEM);
4989 fib_entry = &fib6_entry->common;
4990
4991 mlxsw_sp_rt6 = mlxsw_sp_rt6_create(rt);
4992 if (IS_ERR(mlxsw_sp_rt6)) {
4993 err = PTR_ERR(mlxsw_sp_rt6);
4994 goto err_rt6_create;
4995 }
4996
Petr Machataf6050ee2017-09-02 23:49:21 +02004997 mlxsw_sp_fib6_entry_type_set(mlxsw_sp, fib_entry, mlxsw_sp_rt6->rt);
Ido Schimmel428b8512017-08-03 13:28:28 +02004998
4999 INIT_LIST_HEAD(&fib6_entry->rt6_list);
5000 list_add_tail(&mlxsw_sp_rt6->list, &fib6_entry->rt6_list);
5001 fib6_entry->nrt6 = 1;
5002 err = mlxsw_sp_nexthop6_group_get(mlxsw_sp, fib6_entry);
5003 if (err)
5004 goto err_nexthop6_group_get;
5005
5006 fib_entry->fib_node = fib_node;
5007
5008 return fib6_entry;
5009
5010err_nexthop6_group_get:
5011 list_del(&mlxsw_sp_rt6->list);
5012 mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
5013err_rt6_create:
5014 kfree(fib6_entry);
5015 return ERR_PTR(err);
5016}
5017
5018static void mlxsw_sp_fib6_entry_destroy(struct mlxsw_sp *mlxsw_sp,
5019 struct mlxsw_sp_fib6_entry *fib6_entry)
5020{
5021 mlxsw_sp_nexthop6_group_put(mlxsw_sp, &fib6_entry->common);
5022 mlxsw_sp_fib6_entry_rt_destroy_all(fib6_entry);
5023 WARN_ON(fib6_entry->nrt6);
5024 kfree(fib6_entry);
5025}
5026
5027static struct mlxsw_sp_fib6_entry *
5028mlxsw_sp_fib6_node_entry_find(const struct mlxsw_sp_fib_node *fib_node,
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005029 const struct rt6_info *nrt, bool replace)
Ido Schimmel428b8512017-08-03 13:28:28 +02005030{
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005031 struct mlxsw_sp_fib6_entry *fib6_entry, *fallback = NULL;
Ido Schimmel428b8512017-08-03 13:28:28 +02005032
5033 list_for_each_entry(fib6_entry, &fib_node->entry_list, common.list) {
5034 struct rt6_info *rt = mlxsw_sp_fib6_entry_rt(fib6_entry);
5035
5036 if (rt->rt6i_table->tb6_id > nrt->rt6i_table->tb6_id)
5037 continue;
5038 if (rt->rt6i_table->tb6_id != nrt->rt6i_table->tb6_id)
5039 break;
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005040 if (replace && rt->rt6i_metric == nrt->rt6i_metric) {
5041 if (mlxsw_sp_fib6_rt_can_mp(rt) ==
5042 mlxsw_sp_fib6_rt_can_mp(nrt))
5043 return fib6_entry;
5044 if (mlxsw_sp_fib6_rt_can_mp(nrt))
5045 fallback = fallback ?: fib6_entry;
5046 }
Ido Schimmel428b8512017-08-03 13:28:28 +02005047 if (rt->rt6i_metric > nrt->rt6i_metric)
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005048 return fallback ?: fib6_entry;
Ido Schimmel428b8512017-08-03 13:28:28 +02005049 }
5050
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005051 return fallback;
Ido Schimmel428b8512017-08-03 13:28:28 +02005052}
5053
5054static int
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005055mlxsw_sp_fib6_node_list_insert(struct mlxsw_sp_fib6_entry *new6_entry,
5056 bool replace)
Ido Schimmel428b8512017-08-03 13:28:28 +02005057{
5058 struct mlxsw_sp_fib_node *fib_node = new6_entry->common.fib_node;
5059 struct rt6_info *nrt = mlxsw_sp_fib6_entry_rt(new6_entry);
5060 struct mlxsw_sp_fib6_entry *fib6_entry;
5061
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005062 fib6_entry = mlxsw_sp_fib6_node_entry_find(fib_node, nrt, replace);
5063
5064 if (replace && WARN_ON(!fib6_entry))
5065 return -EINVAL;
Ido Schimmel428b8512017-08-03 13:28:28 +02005066
5067 if (fib6_entry) {
5068 list_add_tail(&new6_entry->common.list,
5069 &fib6_entry->common.list);
5070 } else {
5071 struct mlxsw_sp_fib6_entry *last;
5072
5073 list_for_each_entry(last, &fib_node->entry_list, common.list) {
5074 struct rt6_info *rt = mlxsw_sp_fib6_entry_rt(last);
5075
5076 if (nrt->rt6i_table->tb6_id > rt->rt6i_table->tb6_id)
5077 break;
5078 fib6_entry = last;
5079 }
5080
5081 if (fib6_entry)
5082 list_add(&new6_entry->common.list,
5083 &fib6_entry->common.list);
5084 else
5085 list_add(&new6_entry->common.list,
5086 &fib_node->entry_list);
5087 }
5088
5089 return 0;
5090}
5091
5092static void
5093mlxsw_sp_fib6_node_list_remove(struct mlxsw_sp_fib6_entry *fib6_entry)
5094{
5095 list_del(&fib6_entry->common.list);
5096}
5097
5098static int mlxsw_sp_fib6_node_entry_link(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005099 struct mlxsw_sp_fib6_entry *fib6_entry,
5100 bool replace)
Ido Schimmel428b8512017-08-03 13:28:28 +02005101{
5102 int err;
5103
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005104 err = mlxsw_sp_fib6_node_list_insert(fib6_entry, replace);
Ido Schimmel428b8512017-08-03 13:28:28 +02005105 if (err)
5106 return err;
5107
5108 err = mlxsw_sp_fib_node_entry_add(mlxsw_sp, &fib6_entry->common);
5109 if (err)
5110 goto err_fib_node_entry_add;
5111
5112 return 0;
5113
5114err_fib_node_entry_add:
5115 mlxsw_sp_fib6_node_list_remove(fib6_entry);
5116 return err;
5117}
5118
5119static void
5120mlxsw_sp_fib6_node_entry_unlink(struct mlxsw_sp *mlxsw_sp,
5121 struct mlxsw_sp_fib6_entry *fib6_entry)
5122{
5123 mlxsw_sp_fib_node_entry_del(mlxsw_sp, &fib6_entry->common);
5124 mlxsw_sp_fib6_node_list_remove(fib6_entry);
5125}
5126
5127static struct mlxsw_sp_fib6_entry *
5128mlxsw_sp_fib6_entry_lookup(struct mlxsw_sp *mlxsw_sp,
5129 const struct rt6_info *rt)
5130{
5131 struct mlxsw_sp_fib6_entry *fib6_entry;
5132 struct mlxsw_sp_fib_node *fib_node;
5133 struct mlxsw_sp_fib *fib;
5134 struct mlxsw_sp_vr *vr;
5135
5136 vr = mlxsw_sp_vr_find(mlxsw_sp, rt->rt6i_table->tb6_id);
5137 if (!vr)
5138 return NULL;
5139 fib = mlxsw_sp_vr_fib(vr, MLXSW_SP_L3_PROTO_IPV6);
5140
5141 fib_node = mlxsw_sp_fib_node_lookup(fib, &rt->rt6i_dst.addr,
5142 sizeof(rt->rt6i_dst.addr),
5143 rt->rt6i_dst.plen);
5144 if (!fib_node)
5145 return NULL;
5146
5147 list_for_each_entry(fib6_entry, &fib_node->entry_list, common.list) {
5148 struct rt6_info *iter_rt = mlxsw_sp_fib6_entry_rt(fib6_entry);
5149
5150 if (rt->rt6i_table->tb6_id == iter_rt->rt6i_table->tb6_id &&
5151 rt->rt6i_metric == iter_rt->rt6i_metric &&
5152 mlxsw_sp_fib6_entry_rt_find(fib6_entry, rt))
5153 return fib6_entry;
5154 }
5155
5156 return NULL;
5157}
5158
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005159static void mlxsw_sp_fib6_entry_replace(struct mlxsw_sp *mlxsw_sp,
5160 struct mlxsw_sp_fib6_entry *fib6_entry,
5161 bool replace)
5162{
5163 struct mlxsw_sp_fib_node *fib_node = fib6_entry->common.fib_node;
5164 struct mlxsw_sp_fib6_entry *replaced;
5165
5166 if (!replace)
5167 return;
5168
5169 replaced = list_next_entry(fib6_entry, common.list);
5170
5171 mlxsw_sp_fib6_node_entry_unlink(mlxsw_sp, replaced);
5172 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, replaced);
5173 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5174}
5175
Ido Schimmel428b8512017-08-03 13:28:28 +02005176static int mlxsw_sp_router_fib6_add(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005177 struct rt6_info *rt, bool replace)
Ido Schimmel428b8512017-08-03 13:28:28 +02005178{
5179 struct mlxsw_sp_fib6_entry *fib6_entry;
5180 struct mlxsw_sp_fib_node *fib_node;
5181 int err;
5182
5183 if (mlxsw_sp->router->aborted)
5184 return 0;
5185
Ido Schimmelf36f5ac2017-08-03 13:28:30 +02005186 if (rt->rt6i_src.plen)
5187 return -EINVAL;
5188
Ido Schimmel428b8512017-08-03 13:28:28 +02005189 if (mlxsw_sp_fib6_rt_should_ignore(rt))
5190 return 0;
5191
5192 fib_node = mlxsw_sp_fib_node_get(mlxsw_sp, rt->rt6i_table->tb6_id,
5193 &rt->rt6i_dst.addr,
5194 sizeof(rt->rt6i_dst.addr),
5195 rt->rt6i_dst.plen,
5196 MLXSW_SP_L3_PROTO_IPV6);
5197 if (IS_ERR(fib_node))
5198 return PTR_ERR(fib_node);
5199
5200 /* Before creating a new entry, try to append route to an existing
5201 * multipath entry.
5202 */
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005203 fib6_entry = mlxsw_sp_fib6_node_mp_entry_find(fib_node, rt, replace);
Ido Schimmel428b8512017-08-03 13:28:28 +02005204 if (fib6_entry) {
5205 err = mlxsw_sp_fib6_entry_nexthop_add(mlxsw_sp, fib6_entry, rt);
5206 if (err)
5207 goto err_fib6_entry_nexthop_add;
5208 return 0;
5209 }
5210
5211 fib6_entry = mlxsw_sp_fib6_entry_create(mlxsw_sp, fib_node, rt);
5212 if (IS_ERR(fib6_entry)) {
5213 err = PTR_ERR(fib6_entry);
5214 goto err_fib6_entry_create;
5215 }
5216
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005217 err = mlxsw_sp_fib6_node_entry_link(mlxsw_sp, fib6_entry, replace);
Ido Schimmel428b8512017-08-03 13:28:28 +02005218 if (err)
5219 goto err_fib6_node_entry_link;
5220
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005221 mlxsw_sp_fib6_entry_replace(mlxsw_sp, fib6_entry, replace);
5222
Ido Schimmel428b8512017-08-03 13:28:28 +02005223 return 0;
5224
5225err_fib6_node_entry_link:
5226 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
5227err_fib6_entry_create:
5228err_fib6_entry_nexthop_add:
5229 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5230 return err;
5231}
5232
5233static void mlxsw_sp_router_fib6_del(struct mlxsw_sp *mlxsw_sp,
5234 struct rt6_info *rt)
5235{
5236 struct mlxsw_sp_fib6_entry *fib6_entry;
5237 struct mlxsw_sp_fib_node *fib_node;
5238
5239 if (mlxsw_sp->router->aborted)
5240 return;
5241
5242 if (mlxsw_sp_fib6_rt_should_ignore(rt))
5243 return;
5244
5245 fib6_entry = mlxsw_sp_fib6_entry_lookup(mlxsw_sp, rt);
5246 if (WARN_ON(!fib6_entry))
5247 return;
5248
5249 /* If route is part of a multipath entry, but not the last one
5250 * removed, then only reduce its nexthop group.
5251 */
5252 if (!list_is_singular(&fib6_entry->rt6_list)) {
5253 mlxsw_sp_fib6_entry_nexthop_del(mlxsw_sp, fib6_entry, rt);
5254 return;
5255 }
5256
5257 fib_node = fib6_entry->common.fib_node;
5258
5259 mlxsw_sp_fib6_node_entry_unlink(mlxsw_sp, fib6_entry);
5260 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
5261 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5262}
5263
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02005264static int __mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp,
5265 enum mlxsw_reg_ralxx_protocol proto,
5266 u8 tree_id)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005267{
5268 char ralta_pl[MLXSW_REG_RALTA_LEN];
5269 char ralst_pl[MLXSW_REG_RALST_LEN];
Ido Schimmelb5d90e62017-03-10 08:53:43 +01005270 int i, err;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005271
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02005272 mlxsw_reg_ralta_pack(ralta_pl, true, proto, tree_id);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005273 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
5274 if (err)
5275 return err;
5276
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02005277 mlxsw_reg_ralst_pack(ralst_pl, 0xff, tree_id);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005278 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
5279 if (err)
5280 return err;
5281
Ido Schimmelb5d90e62017-03-10 08:53:43 +01005282 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Ido Schimmel9011b672017-05-16 19:38:25 +02005283 struct mlxsw_sp_vr *vr = &mlxsw_sp->router->vrs[i];
Ido Schimmelb5d90e62017-03-10 08:53:43 +01005284 char raltb_pl[MLXSW_REG_RALTB_LEN];
5285 char ralue_pl[MLXSW_REG_RALUE_LEN];
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005286
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02005287 mlxsw_reg_raltb_pack(raltb_pl, vr->id, proto, tree_id);
Ido Schimmelb5d90e62017-03-10 08:53:43 +01005288 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb),
5289 raltb_pl);
5290 if (err)
5291 return err;
5292
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02005293 mlxsw_reg_ralue_pack(ralue_pl, proto,
5294 MLXSW_REG_RALUE_OP_WRITE_WRITE, vr->id, 0);
Ido Schimmelb5d90e62017-03-10 08:53:43 +01005295 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
5296 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue),
5297 ralue_pl);
5298 if (err)
5299 return err;
5300 }
5301
5302 return 0;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005303}
5304
Yotam Gigid42b0962017-09-27 08:23:20 +02005305static int mlxsw_sp_router_fibmr_add(struct mlxsw_sp *mlxsw_sp,
5306 struct mfc_entry_notifier_info *men_info,
5307 bool replace)
5308{
5309 struct mlxsw_sp_vr *vr;
5310
5311 if (mlxsw_sp->router->aborted)
5312 return 0;
5313
David Ahernf8fa9b42017-10-18 09:56:56 -07005314 vr = mlxsw_sp_vr_get(mlxsw_sp, men_info->tb_id, NULL);
Yotam Gigid42b0962017-09-27 08:23:20 +02005315 if (IS_ERR(vr))
5316 return PTR_ERR(vr);
5317
5318 return mlxsw_sp_mr_route4_add(vr->mr4_table, men_info->mfc, replace);
5319}
5320
5321static void mlxsw_sp_router_fibmr_del(struct mlxsw_sp *mlxsw_sp,
5322 struct mfc_entry_notifier_info *men_info)
5323{
5324 struct mlxsw_sp_vr *vr;
5325
5326 if (mlxsw_sp->router->aborted)
5327 return;
5328
5329 vr = mlxsw_sp_vr_find(mlxsw_sp, men_info->tb_id);
5330 if (WARN_ON(!vr))
5331 return;
5332
5333 mlxsw_sp_mr_route4_del(vr->mr4_table, men_info->mfc);
5334 mlxsw_sp_vr_put(vr);
5335}
5336
5337static int
5338mlxsw_sp_router_fibmr_vif_add(struct mlxsw_sp *mlxsw_sp,
5339 struct vif_entry_notifier_info *ven_info)
5340{
5341 struct mlxsw_sp_rif *rif;
5342 struct mlxsw_sp_vr *vr;
5343
5344 if (mlxsw_sp->router->aborted)
5345 return 0;
5346
David Ahernf8fa9b42017-10-18 09:56:56 -07005347 vr = mlxsw_sp_vr_get(mlxsw_sp, ven_info->tb_id, NULL);
Yotam Gigid42b0962017-09-27 08:23:20 +02005348 if (IS_ERR(vr))
5349 return PTR_ERR(vr);
5350
5351 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, ven_info->dev);
5352 return mlxsw_sp_mr_vif_add(vr->mr4_table, ven_info->dev,
5353 ven_info->vif_index,
5354 ven_info->vif_flags, rif);
5355}
5356
5357static void
5358mlxsw_sp_router_fibmr_vif_del(struct mlxsw_sp *mlxsw_sp,
5359 struct vif_entry_notifier_info *ven_info)
5360{
5361 struct mlxsw_sp_vr *vr;
5362
5363 if (mlxsw_sp->router->aborted)
5364 return;
5365
5366 vr = mlxsw_sp_vr_find(mlxsw_sp, ven_info->tb_id);
5367 if (WARN_ON(!vr))
5368 return;
5369
5370 mlxsw_sp_mr_vif_del(vr->mr4_table, ven_info->vif_index);
5371 mlxsw_sp_vr_put(vr);
5372}
5373
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02005374static int mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp)
5375{
5376 enum mlxsw_reg_ralxx_protocol proto = MLXSW_REG_RALXX_PROTOCOL_IPV4;
5377 int err;
5378
5379 err = __mlxsw_sp_router_set_abort_trap(mlxsw_sp, proto,
5380 MLXSW_SP_LPM_TREE_MIN);
5381 if (err)
5382 return err;
5383
Yotam Gigid42b0962017-09-27 08:23:20 +02005384 /* The multicast router code does not need an abort trap as by default,
5385 * packets that don't match any routes are trapped to the CPU.
5386 */
5387
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02005388 proto = MLXSW_REG_RALXX_PROTOCOL_IPV6;
5389 return __mlxsw_sp_router_set_abort_trap(mlxsw_sp, proto,
5390 MLXSW_SP_LPM_TREE_MIN + 1);
5391}
5392
Ido Schimmel9aecce12017-02-09 10:28:42 +01005393static void mlxsw_sp_fib4_node_flush(struct mlxsw_sp *mlxsw_sp,
5394 struct mlxsw_sp_fib_node *fib_node)
5395{
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02005396 struct mlxsw_sp_fib4_entry *fib4_entry, *tmp;
Ido Schimmel9aecce12017-02-09 10:28:42 +01005397
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02005398 list_for_each_entry_safe(fib4_entry, tmp, &fib_node->entry_list,
5399 common.list) {
5400 bool do_break = &tmp->common.list == &fib_node->entry_list;
Ido Schimmel9aecce12017-02-09 10:28:42 +01005401
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02005402 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, fib4_entry);
5403 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
Ido Schimmel731ea1c2017-07-18 10:10:21 +02005404 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
Ido Schimmel9aecce12017-02-09 10:28:42 +01005405 /* Break when entry list is empty and node was freed.
5406 * Otherwise, we'll access freed memory in the next
5407 * iteration.
5408 */
5409 if (do_break)
5410 break;
5411 }
5412}
5413
Ido Schimmel428b8512017-08-03 13:28:28 +02005414static void mlxsw_sp_fib6_node_flush(struct mlxsw_sp *mlxsw_sp,
5415 struct mlxsw_sp_fib_node *fib_node)
5416{
5417 struct mlxsw_sp_fib6_entry *fib6_entry, *tmp;
5418
5419 list_for_each_entry_safe(fib6_entry, tmp, &fib_node->entry_list,
5420 common.list) {
5421 bool do_break = &tmp->common.list == &fib_node->entry_list;
5422
5423 mlxsw_sp_fib6_node_entry_unlink(mlxsw_sp, fib6_entry);
5424 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
5425 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5426 if (do_break)
5427 break;
5428 }
5429}
5430
Ido Schimmel9aecce12017-02-09 10:28:42 +01005431static void mlxsw_sp_fib_node_flush(struct mlxsw_sp *mlxsw_sp,
5432 struct mlxsw_sp_fib_node *fib_node)
5433{
Ido Schimmel76610eb2017-03-10 08:53:41 +01005434 switch (fib_node->fib->proto) {
Ido Schimmel9aecce12017-02-09 10:28:42 +01005435 case MLXSW_SP_L3_PROTO_IPV4:
5436 mlxsw_sp_fib4_node_flush(mlxsw_sp, fib_node);
5437 break;
5438 case MLXSW_SP_L3_PROTO_IPV6:
Ido Schimmel428b8512017-08-03 13:28:28 +02005439 mlxsw_sp_fib6_node_flush(mlxsw_sp, fib_node);
Ido Schimmel9aecce12017-02-09 10:28:42 +01005440 break;
5441 }
5442}
5443
Ido Schimmel76610eb2017-03-10 08:53:41 +01005444static void mlxsw_sp_vr_fib_flush(struct mlxsw_sp *mlxsw_sp,
5445 struct mlxsw_sp_vr *vr,
5446 enum mlxsw_sp_l3proto proto)
5447{
5448 struct mlxsw_sp_fib *fib = mlxsw_sp_vr_fib(vr, proto);
5449 struct mlxsw_sp_fib_node *fib_node, *tmp;
5450
5451 list_for_each_entry_safe(fib_node, tmp, &fib->node_list, list) {
5452 bool do_break = &tmp->list == &fib->node_list;
5453
5454 mlxsw_sp_fib_node_flush(mlxsw_sp, fib_node);
5455 if (do_break)
5456 break;
5457 }
5458}
5459
Ido Schimmelac571de2016-11-14 11:26:32 +01005460static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005461{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005462 int i;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005463
Jiri Pirkoc1a38312016-10-21 16:07:23 +02005464 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Ido Schimmel9011b672017-05-16 19:38:25 +02005465 struct mlxsw_sp_vr *vr = &mlxsw_sp->router->vrs[i];
Ido Schimmelac571de2016-11-14 11:26:32 +01005466
Ido Schimmel76610eb2017-03-10 08:53:41 +01005467 if (!mlxsw_sp_vr_is_used(vr))
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005468 continue;
Yotam Gigid42b0962017-09-27 08:23:20 +02005469
5470 mlxsw_sp_mr_table_flush(vr->mr4_table);
Ido Schimmel76610eb2017-03-10 08:53:41 +01005471 mlxsw_sp_vr_fib_flush(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV4);
Ido Schimmela3d9bc52017-07-18 10:10:22 +02005472
5473 /* If virtual router was only used for IPv4, then it's no
5474 * longer used.
5475 */
5476 if (!mlxsw_sp_vr_is_used(vr))
5477 continue;
5478 mlxsw_sp_vr_fib_flush(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV6);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005479 }
Ido Schimmelac571de2016-11-14 11:26:32 +01005480}
5481
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02005482static void mlxsw_sp_router_fib_abort(struct mlxsw_sp *mlxsw_sp)
Ido Schimmelac571de2016-11-14 11:26:32 +01005483{
5484 int err;
5485
Ido Schimmel9011b672017-05-16 19:38:25 +02005486 if (mlxsw_sp->router->aborted)
Ido Schimmeld331d302016-11-16 09:51:58 +01005487 return;
5488 dev_warn(mlxsw_sp->bus_info->dev, "FIB abort triggered. Note that FIB entries are no longer being offloaded to this device.\n");
Ido Schimmelac571de2016-11-14 11:26:32 +01005489 mlxsw_sp_router_fib_flush(mlxsw_sp);
Ido Schimmel9011b672017-05-16 19:38:25 +02005490 mlxsw_sp->router->aborted = true;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005491 err = mlxsw_sp_router_set_abort_trap(mlxsw_sp);
5492 if (err)
5493 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set abort trap.\n");
5494}
5495
Ido Schimmel30572242016-12-03 16:45:01 +01005496struct mlxsw_sp_fib_event_work {
Ido Schimmela0e47612017-02-06 16:20:10 +01005497 struct work_struct work;
Ido Schimmelad178c82017-02-08 11:16:40 +01005498 union {
Ido Schimmel428b8512017-08-03 13:28:28 +02005499 struct fib6_entry_notifier_info fen6_info;
Ido Schimmelad178c82017-02-08 11:16:40 +01005500 struct fib_entry_notifier_info fen_info;
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01005501 struct fib_rule_notifier_info fr_info;
Ido Schimmelad178c82017-02-08 11:16:40 +01005502 struct fib_nh_notifier_info fnh_info;
Yotam Gigid42b0962017-09-27 08:23:20 +02005503 struct mfc_entry_notifier_info men_info;
5504 struct vif_entry_notifier_info ven_info;
Ido Schimmelad178c82017-02-08 11:16:40 +01005505 };
Ido Schimmel30572242016-12-03 16:45:01 +01005506 struct mlxsw_sp *mlxsw_sp;
5507 unsigned long event;
5508};
5509
Ido Schimmel66a57632017-08-03 13:28:26 +02005510static void mlxsw_sp_router_fib4_event_work(struct work_struct *work)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005511{
Ido Schimmel30572242016-12-03 16:45:01 +01005512 struct mlxsw_sp_fib_event_work *fib_work =
Ido Schimmela0e47612017-02-06 16:20:10 +01005513 container_of(work, struct mlxsw_sp_fib_event_work, work);
Ido Schimmel30572242016-12-03 16:45:01 +01005514 struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp;
Ido Schimmel599cf8f2017-02-09 10:28:44 +01005515 bool replace, append;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005516 int err;
5517
Ido Schimmel30572242016-12-03 16:45:01 +01005518 /* Protect internal structures from changes */
5519 rtnl_lock();
5520 switch (fib_work->event) {
Ido Schimmel599cf8f2017-02-09 10:28:44 +01005521 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
Ido Schimmel4283bce2017-02-09 10:28:43 +01005522 case FIB_EVENT_ENTRY_APPEND: /* fall through */
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005523 case FIB_EVENT_ENTRY_ADD:
Ido Schimmel599cf8f2017-02-09 10:28:44 +01005524 replace = fib_work->event == FIB_EVENT_ENTRY_REPLACE;
Ido Schimmel4283bce2017-02-09 10:28:43 +01005525 append = fib_work->event == FIB_EVENT_ENTRY_APPEND;
5526 err = mlxsw_sp_router_fib4_add(mlxsw_sp, &fib_work->fen_info,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01005527 replace, append);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005528 if (err)
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02005529 mlxsw_sp_router_fib_abort(mlxsw_sp);
Ido Schimmel30572242016-12-03 16:45:01 +01005530 fib_info_put(fib_work->fen_info.fi);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005531 break;
5532 case FIB_EVENT_ENTRY_DEL:
Ido Schimmel30572242016-12-03 16:45:01 +01005533 mlxsw_sp_router_fib4_del(mlxsw_sp, &fib_work->fen_info);
5534 fib_info_put(fib_work->fen_info.fi);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005535 break;
David Ahern1f279232017-10-27 17:37:14 -07005536 case FIB_EVENT_RULE_ADD:
5537 /* if we get here, a rule was added that we do not support.
5538 * just do the fib_abort
5539 */
5540 mlxsw_sp_router_fib_abort(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005541 break;
Ido Schimmelad178c82017-02-08 11:16:40 +01005542 case FIB_EVENT_NH_ADD: /* fall through */
5543 case FIB_EVENT_NH_DEL:
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02005544 mlxsw_sp_nexthop4_event(mlxsw_sp, fib_work->event,
5545 fib_work->fnh_info.fib_nh);
Ido Schimmelad178c82017-02-08 11:16:40 +01005546 fib_info_put(fib_work->fnh_info.fib_nh->nh_parent);
5547 break;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005548 }
Ido Schimmel30572242016-12-03 16:45:01 +01005549 rtnl_unlock();
5550 kfree(fib_work);
5551}
5552
Ido Schimmel66a57632017-08-03 13:28:26 +02005553static void mlxsw_sp_router_fib6_event_work(struct work_struct *work)
5554{
Ido Schimmel583419f2017-08-03 13:28:27 +02005555 struct mlxsw_sp_fib_event_work *fib_work =
5556 container_of(work, struct mlxsw_sp_fib_event_work, work);
5557 struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp;
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005558 bool replace;
Ido Schimmel428b8512017-08-03 13:28:28 +02005559 int err;
Ido Schimmel583419f2017-08-03 13:28:27 +02005560
5561 rtnl_lock();
5562 switch (fib_work->event) {
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005563 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
Ido Schimmel428b8512017-08-03 13:28:28 +02005564 case FIB_EVENT_ENTRY_ADD:
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005565 replace = fib_work->event == FIB_EVENT_ENTRY_REPLACE;
Ido Schimmel428b8512017-08-03 13:28:28 +02005566 err = mlxsw_sp_router_fib6_add(mlxsw_sp,
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005567 fib_work->fen6_info.rt, replace);
Ido Schimmel428b8512017-08-03 13:28:28 +02005568 if (err)
5569 mlxsw_sp_router_fib_abort(mlxsw_sp);
5570 mlxsw_sp_rt6_release(fib_work->fen6_info.rt);
5571 break;
5572 case FIB_EVENT_ENTRY_DEL:
5573 mlxsw_sp_router_fib6_del(mlxsw_sp, fib_work->fen6_info.rt);
5574 mlxsw_sp_rt6_release(fib_work->fen6_info.rt);
5575 break;
David Ahern1f279232017-10-27 17:37:14 -07005576 case FIB_EVENT_RULE_ADD:
5577 /* if we get here, a rule was added that we do not support.
5578 * just do the fib_abort
5579 */
5580 mlxsw_sp_router_fib_abort(mlxsw_sp);
Ido Schimmel583419f2017-08-03 13:28:27 +02005581 break;
5582 }
5583 rtnl_unlock();
5584 kfree(fib_work);
Ido Schimmel66a57632017-08-03 13:28:26 +02005585}
5586
Yotam Gigid42b0962017-09-27 08:23:20 +02005587static void mlxsw_sp_router_fibmr_event_work(struct work_struct *work)
5588{
5589 struct mlxsw_sp_fib_event_work *fib_work =
5590 container_of(work, struct mlxsw_sp_fib_event_work, work);
5591 struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp;
Yotam Gigid42b0962017-09-27 08:23:20 +02005592 bool replace;
5593 int err;
5594
5595 rtnl_lock();
5596 switch (fib_work->event) {
5597 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
5598 case FIB_EVENT_ENTRY_ADD:
5599 replace = fib_work->event == FIB_EVENT_ENTRY_REPLACE;
5600
5601 err = mlxsw_sp_router_fibmr_add(mlxsw_sp, &fib_work->men_info,
5602 replace);
5603 if (err)
5604 mlxsw_sp_router_fib_abort(mlxsw_sp);
5605 ipmr_cache_put(fib_work->men_info.mfc);
5606 break;
5607 case FIB_EVENT_ENTRY_DEL:
5608 mlxsw_sp_router_fibmr_del(mlxsw_sp, &fib_work->men_info);
5609 ipmr_cache_put(fib_work->men_info.mfc);
5610 break;
5611 case FIB_EVENT_VIF_ADD:
5612 err = mlxsw_sp_router_fibmr_vif_add(mlxsw_sp,
5613 &fib_work->ven_info);
5614 if (err)
5615 mlxsw_sp_router_fib_abort(mlxsw_sp);
5616 dev_put(fib_work->ven_info.dev);
5617 break;
5618 case FIB_EVENT_VIF_DEL:
5619 mlxsw_sp_router_fibmr_vif_del(mlxsw_sp,
5620 &fib_work->ven_info);
5621 dev_put(fib_work->ven_info.dev);
5622 break;
David Ahern1f279232017-10-27 17:37:14 -07005623 case FIB_EVENT_RULE_ADD:
5624 /* if we get here, a rule was added that we do not support.
5625 * just do the fib_abort
5626 */
5627 mlxsw_sp_router_fib_abort(mlxsw_sp);
Yotam Gigid42b0962017-09-27 08:23:20 +02005628 break;
5629 }
5630 rtnl_unlock();
5631 kfree(fib_work);
5632}
5633
Ido Schimmel66a57632017-08-03 13:28:26 +02005634static void mlxsw_sp_router_fib4_event(struct mlxsw_sp_fib_event_work *fib_work,
5635 struct fib_notifier_info *info)
5636{
David Ahern3c75f9b2017-10-18 15:01:38 -07005637 struct fib_entry_notifier_info *fen_info;
David Ahern3c75f9b2017-10-18 15:01:38 -07005638 struct fib_nh_notifier_info *fnh_info;
5639
Ido Schimmel66a57632017-08-03 13:28:26 +02005640 switch (fib_work->event) {
5641 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
5642 case FIB_EVENT_ENTRY_APPEND: /* fall through */
5643 case FIB_EVENT_ENTRY_ADD: /* fall through */
5644 case FIB_EVENT_ENTRY_DEL:
David Ahern3c75f9b2017-10-18 15:01:38 -07005645 fen_info = container_of(info, struct fib_entry_notifier_info,
5646 info);
5647 fib_work->fen_info = *fen_info;
5648 /* Take reference on fib_info to prevent it from being
Ido Schimmel66a57632017-08-03 13:28:26 +02005649 * freed while work is queued. Release it afterwards.
5650 */
5651 fib_info_hold(fib_work->fen_info.fi);
5652 break;
Ido Schimmel66a57632017-08-03 13:28:26 +02005653 case FIB_EVENT_NH_ADD: /* fall through */
5654 case FIB_EVENT_NH_DEL:
David Ahern3c75f9b2017-10-18 15:01:38 -07005655 fnh_info = container_of(info, struct fib_nh_notifier_info,
5656 info);
5657 fib_work->fnh_info = *fnh_info;
Ido Schimmel66a57632017-08-03 13:28:26 +02005658 fib_info_hold(fib_work->fnh_info.fib_nh->nh_parent);
5659 break;
5660 }
5661}
5662
5663static void mlxsw_sp_router_fib6_event(struct mlxsw_sp_fib_event_work *fib_work,
5664 struct fib_notifier_info *info)
5665{
David Ahern3c75f9b2017-10-18 15:01:38 -07005666 struct fib6_entry_notifier_info *fen6_info;
David Ahern3c75f9b2017-10-18 15:01:38 -07005667
Ido Schimmel583419f2017-08-03 13:28:27 +02005668 switch (fib_work->event) {
Ido Schimmel0a7fd1a2017-08-03 13:28:29 +02005669 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
Ido Schimmel428b8512017-08-03 13:28:28 +02005670 case FIB_EVENT_ENTRY_ADD: /* fall through */
5671 case FIB_EVENT_ENTRY_DEL:
David Ahern3c75f9b2017-10-18 15:01:38 -07005672 fen6_info = container_of(info, struct fib6_entry_notifier_info,
5673 info);
5674 fib_work->fen6_info = *fen6_info;
Ido Schimmel428b8512017-08-03 13:28:28 +02005675 rt6_hold(fib_work->fen6_info.rt);
5676 break;
Ido Schimmel583419f2017-08-03 13:28:27 +02005677 }
Ido Schimmel66a57632017-08-03 13:28:26 +02005678}
5679
Yotam Gigid42b0962017-09-27 08:23:20 +02005680static void
5681mlxsw_sp_router_fibmr_event(struct mlxsw_sp_fib_event_work *fib_work,
5682 struct fib_notifier_info *info)
5683{
5684 switch (fib_work->event) {
5685 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
5686 case FIB_EVENT_ENTRY_ADD: /* fall through */
5687 case FIB_EVENT_ENTRY_DEL:
5688 memcpy(&fib_work->men_info, info, sizeof(fib_work->men_info));
5689 ipmr_cache_hold(fib_work->men_info.mfc);
5690 break;
5691 case FIB_EVENT_VIF_ADD: /* fall through */
5692 case FIB_EVENT_VIF_DEL:
5693 memcpy(&fib_work->ven_info, info, sizeof(fib_work->ven_info));
5694 dev_hold(fib_work->ven_info.dev);
5695 break;
David Ahern1f279232017-10-27 17:37:14 -07005696 }
5697}
5698
5699static int mlxsw_sp_router_fib_rule_event(unsigned long event,
5700 struct fib_notifier_info *info,
5701 struct mlxsw_sp *mlxsw_sp)
5702{
5703 struct netlink_ext_ack *extack = info->extack;
5704 struct fib_rule_notifier_info *fr_info;
5705 struct fib_rule *rule;
5706 int err = 0;
5707
5708 /* nothing to do at the moment */
5709 if (event == FIB_EVENT_RULE_DEL)
5710 return 0;
5711
5712 if (mlxsw_sp->router->aborted)
5713 return 0;
5714
5715 fr_info = container_of(info, struct fib_rule_notifier_info, info);
5716 rule = fr_info->rule;
5717
5718 switch (info->family) {
5719 case AF_INET:
5720 if (!fib4_rule_default(rule) && !rule->l3mdev)
5721 err = -1;
5722 break;
5723 case AF_INET6:
5724 if (!fib6_rule_default(rule) && !rule->l3mdev)
5725 err = -1;
5726 break;
5727 case RTNL_FAMILY_IPMR:
5728 if (!ipmr_rule_default(rule) && !rule->l3mdev)
5729 err = -1;
Yotam Gigid42b0962017-09-27 08:23:20 +02005730 break;
5731 }
David Ahern1f279232017-10-27 17:37:14 -07005732
5733 if (err < 0)
5734 NL_SET_ERR_MSG(extack, "spectrum: FIB rules not supported. Aborting offload");
5735
5736 return err;
Yotam Gigid42b0962017-09-27 08:23:20 +02005737}
5738
Ido Schimmel30572242016-12-03 16:45:01 +01005739/* Called with rcu_read_lock() */
5740static int mlxsw_sp_router_fib_event(struct notifier_block *nb,
5741 unsigned long event, void *ptr)
5742{
Ido Schimmel30572242016-12-03 16:45:01 +01005743 struct mlxsw_sp_fib_event_work *fib_work;
5744 struct fib_notifier_info *info = ptr;
Ido Schimmel7e39d112017-05-16 19:38:28 +02005745 struct mlxsw_sp_router *router;
David Ahern1f279232017-10-27 17:37:14 -07005746 int err;
Ido Schimmel30572242016-12-03 16:45:01 +01005747
Ido Schimmel8e29f972017-09-15 15:31:07 +02005748 if (!net_eq(info->net, &init_net) ||
Yotam Gigi664375e2017-09-27 08:23:22 +02005749 (info->family != AF_INET && info->family != AF_INET6 &&
5750 info->family != RTNL_FAMILY_IPMR))
Ido Schimmel30572242016-12-03 16:45:01 +01005751 return NOTIFY_DONE;
5752
David Ahern1f279232017-10-27 17:37:14 -07005753 router = container_of(nb, struct mlxsw_sp_router, fib_nb);
5754
5755 switch (event) {
5756 case FIB_EVENT_RULE_ADD: /* fall through */
5757 case FIB_EVENT_RULE_DEL:
5758 err = mlxsw_sp_router_fib_rule_event(event, info,
5759 router->mlxsw_sp);
5760 if (!err)
5761 return NOTIFY_DONE;
5762 }
5763
Ido Schimmel30572242016-12-03 16:45:01 +01005764 fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
5765 if (WARN_ON(!fib_work))
5766 return NOTIFY_BAD;
5767
Ido Schimmel7e39d112017-05-16 19:38:28 +02005768 fib_work->mlxsw_sp = router->mlxsw_sp;
Ido Schimmel30572242016-12-03 16:45:01 +01005769 fib_work->event = event;
5770
Ido Schimmel66a57632017-08-03 13:28:26 +02005771 switch (info->family) {
5772 case AF_INET:
5773 INIT_WORK(&fib_work->work, mlxsw_sp_router_fib4_event_work);
5774 mlxsw_sp_router_fib4_event(fib_work, info);
Ido Schimmel30572242016-12-03 16:45:01 +01005775 break;
Ido Schimmel66a57632017-08-03 13:28:26 +02005776 case AF_INET6:
5777 INIT_WORK(&fib_work->work, mlxsw_sp_router_fib6_event_work);
5778 mlxsw_sp_router_fib6_event(fib_work, info);
Ido Schimmelad178c82017-02-08 11:16:40 +01005779 break;
Yotam Gigid42b0962017-09-27 08:23:20 +02005780 case RTNL_FAMILY_IPMR:
5781 INIT_WORK(&fib_work->work, mlxsw_sp_router_fibmr_event_work);
5782 mlxsw_sp_router_fibmr_event(fib_work, info);
5783 break;
Ido Schimmel30572242016-12-03 16:45:01 +01005784 }
5785
Ido Schimmela0e47612017-02-06 16:20:10 +01005786 mlxsw_core_schedule_work(&fib_work->work);
Ido Schimmel30572242016-12-03 16:45:01 +01005787
Jiri Pirkob45f64d2016-09-26 12:52:31 +02005788 return NOTIFY_DONE;
5789}
5790
Ido Schimmel4724ba562017-03-10 08:53:39 +01005791static struct mlxsw_sp_rif *
5792mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
5793 const struct net_device *dev)
5794{
5795 int i;
5796
5797 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
Ido Schimmel5f9efff2017-05-16 19:38:27 +02005798 if (mlxsw_sp->router->rifs[i] &&
5799 mlxsw_sp->router->rifs[i]->dev == dev)
5800 return mlxsw_sp->router->rifs[i];
Ido Schimmel4724ba562017-03-10 08:53:39 +01005801
5802 return NULL;
5803}
5804
5805static int mlxsw_sp_router_rif_disable(struct mlxsw_sp *mlxsw_sp, u16 rif)
5806{
5807 char ritr_pl[MLXSW_REG_RITR_LEN];
5808 int err;
5809
5810 mlxsw_reg_ritr_rif_pack(ritr_pl, rif);
5811 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
5812 if (WARN_ON_ONCE(err))
5813 return err;
5814
5815 mlxsw_reg_ritr_enable_set(ritr_pl, false);
5816 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
5817}
5818
5819static void mlxsw_sp_router_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01005820 struct mlxsw_sp_rif *rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01005821{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01005822 mlxsw_sp_router_rif_disable(mlxsw_sp, rif->rif_index);
5823 mlxsw_sp_nexthop_rif_gone_sync(mlxsw_sp, rif);
5824 mlxsw_sp_neigh_rif_gone_sync(mlxsw_sp, rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01005825}
5826
Arkadi Sharshevsky5ea12372017-07-18 10:10:13 +02005827static bool
5828mlxsw_sp_rif_should_config(struct mlxsw_sp_rif *rif, struct net_device *dev,
5829 unsigned long event)
Ido Schimmel4724ba562017-03-10 08:53:39 +01005830{
Arkadi Sharshevsky5ea12372017-07-18 10:10:13 +02005831 struct inet6_dev *inet6_dev;
5832 bool addr_list_empty = true;
5833 struct in_device *idev;
5834
Ido Schimmel4724ba562017-03-10 08:53:39 +01005835 switch (event) {
5836 case NETDEV_UP:
Petr Machataf1b1f272017-07-31 09:27:28 +02005837 return rif == NULL;
Ido Schimmel4724ba562017-03-10 08:53:39 +01005838 case NETDEV_DOWN:
Arkadi Sharshevsky5ea12372017-07-18 10:10:13 +02005839 idev = __in_dev_get_rtnl(dev);
5840 if (idev && idev->ifa_list)
5841 addr_list_empty = false;
5842
5843 inet6_dev = __in6_dev_get(dev);
5844 if (addr_list_empty && inet6_dev &&
5845 !list_empty(&inet6_dev->addr_list))
5846 addr_list_empty = false;
5847
5848 if (rif && addr_list_empty &&
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01005849 !netif_is_l3_slave(rif->dev))
Ido Schimmel4724ba562017-03-10 08:53:39 +01005850 return true;
5851 /* It is possible we already removed the RIF ourselves
5852 * if it was assigned to a netdev that is now a bridge
5853 * or LAG slave.
5854 */
5855 return false;
5856 }
5857
5858 return false;
5859}
5860
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02005861static enum mlxsw_sp_rif_type
5862mlxsw_sp_dev_rif_type(const struct mlxsw_sp *mlxsw_sp,
5863 const struct net_device *dev)
5864{
5865 enum mlxsw_sp_fid_type type;
5866
Petr Machata6ddb7422017-09-02 23:49:19 +02005867 if (mlxsw_sp_netdev_ipip_type(mlxsw_sp, dev, NULL))
5868 return MLXSW_SP_RIF_TYPE_IPIP_LB;
5869
5870 /* Otherwise RIF type is derived from the type of the underlying FID. */
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02005871 if (is_vlan_dev(dev) && netif_is_bridge_master(vlan_dev_real_dev(dev)))
5872 type = MLXSW_SP_FID_TYPE_8021Q;
5873 else if (netif_is_bridge_master(dev) && br_vlan_enabled(dev))
5874 type = MLXSW_SP_FID_TYPE_8021Q;
5875 else if (netif_is_bridge_master(dev))
5876 type = MLXSW_SP_FID_TYPE_8021D;
5877 else
5878 type = MLXSW_SP_FID_TYPE_RFID;
5879
5880 return mlxsw_sp_fid_type_rif_type(mlxsw_sp, type);
5881}
5882
Ido Schimmelde5ed992017-06-04 16:53:40 +02005883static int mlxsw_sp_rif_index_alloc(struct mlxsw_sp *mlxsw_sp, u16 *p_rif_index)
Ido Schimmel4724ba562017-03-10 08:53:39 +01005884{
5885 int i;
5886
Ido Schimmelde5ed992017-06-04 16:53:40 +02005887 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++) {
5888 if (!mlxsw_sp->router->rifs[i]) {
5889 *p_rif_index = i;
5890 return 0;
5891 }
5892 }
Ido Schimmel4724ba562017-03-10 08:53:39 +01005893
Ido Schimmelde5ed992017-06-04 16:53:40 +02005894 return -ENOBUFS;
Ido Schimmel4724ba562017-03-10 08:53:39 +01005895}
5896
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02005897static struct mlxsw_sp_rif *mlxsw_sp_rif_alloc(size_t rif_size, u16 rif_index,
5898 u16 vr_id,
5899 struct net_device *l3_dev)
Ido Schimmel4724ba562017-03-10 08:53:39 +01005900{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01005901 struct mlxsw_sp_rif *rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01005902
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02005903 rif = kzalloc(rif_size, GFP_KERNEL);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01005904 if (!rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01005905 return NULL;
5906
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01005907 INIT_LIST_HEAD(&rif->nexthop_list);
5908 INIT_LIST_HEAD(&rif->neigh_list);
5909 ether_addr_copy(rif->addr, l3_dev->dev_addr);
5910 rif->mtu = l3_dev->mtu;
5911 rif->vr_id = vr_id;
5912 rif->dev = l3_dev;
5913 rif->rif_index = rif_index;
Ido Schimmel4724ba562017-03-10 08:53:39 +01005914
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01005915 return rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01005916}
5917
Ido Schimmel5f9efff2017-05-16 19:38:27 +02005918struct mlxsw_sp_rif *mlxsw_sp_rif_by_index(const struct mlxsw_sp *mlxsw_sp,
5919 u16 rif_index)
5920{
5921 return mlxsw_sp->router->rifs[rif_index];
5922}
5923
Arkadi Sharshevskyfd1b9d42017-03-28 17:24:16 +02005924u16 mlxsw_sp_rif_index(const struct mlxsw_sp_rif *rif)
5925{
5926 return rif->rif_index;
5927}
5928
Petr Machata92107cf2017-09-02 23:49:28 +02005929u16 mlxsw_sp_ipip_lb_rif_index(const struct mlxsw_sp_rif_ipip_lb *lb_rif)
5930{
5931 return lb_rif->common.rif_index;
5932}
5933
5934u16 mlxsw_sp_ipip_lb_ul_vr_id(const struct mlxsw_sp_rif_ipip_lb *lb_rif)
5935{
5936 return lb_rif->ul_vr_id;
5937}
5938
Arkadi Sharshevskyfd1b9d42017-03-28 17:24:16 +02005939int mlxsw_sp_rif_dev_ifindex(const struct mlxsw_sp_rif *rif)
5940{
5941 return rif->dev->ifindex;
5942}
5943
Yotam Gigi91e4d592017-09-19 10:00:19 +02005944const struct net_device *mlxsw_sp_rif_dev(const struct mlxsw_sp_rif *rif)
5945{
5946 return rif->dev;
5947}
5948
Ido Schimmel4724ba562017-03-10 08:53:39 +01005949static struct mlxsw_sp_rif *
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02005950mlxsw_sp_rif_create(struct mlxsw_sp *mlxsw_sp,
David Ahernf8fa9b42017-10-18 09:56:56 -07005951 const struct mlxsw_sp_rif_params *params,
5952 struct netlink_ext_ack *extack)
Ido Schimmel4724ba562017-03-10 08:53:39 +01005953{
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02005954 u32 tb_id = l3mdev_fib_table(params->dev);
5955 const struct mlxsw_sp_rif_ops *ops;
Petr Machata010cadf2017-09-02 23:49:18 +02005956 struct mlxsw_sp_fid *fid = NULL;
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02005957 enum mlxsw_sp_rif_type type;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01005958 struct mlxsw_sp_rif *rif;
Ido Schimmela1107482017-05-26 08:37:39 +02005959 struct mlxsw_sp_vr *vr;
5960 u16 rif_index;
Ido Schimmel4724ba562017-03-10 08:53:39 +01005961 int err;
5962
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02005963 type = mlxsw_sp_dev_rif_type(mlxsw_sp, params->dev);
5964 ops = mlxsw_sp->router->rif_ops_arr[type];
5965
David Ahernf8fa9b42017-10-18 09:56:56 -07005966 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id ? : RT_TABLE_MAIN, extack);
Ido Schimmelc9ec53f2017-05-26 08:37:38 +02005967 if (IS_ERR(vr))
5968 return ERR_CAST(vr);
Petr Machata28a04c72017-10-02 12:14:56 +02005969 vr->rif_count++;
Ido Schimmelc9ec53f2017-05-26 08:37:38 +02005970
Ido Schimmelde5ed992017-06-04 16:53:40 +02005971 err = mlxsw_sp_rif_index_alloc(mlxsw_sp, &rif_index);
David Ahernf8fa9b42017-10-18 09:56:56 -07005972 if (err) {
5973 NL_SET_ERR_MSG(extack, "spectrum: Exceeded number of supported router interfaces");
Ido Schimmelde5ed992017-06-04 16:53:40 +02005974 goto err_rif_index_alloc;
David Ahernf8fa9b42017-10-18 09:56:56 -07005975 }
Ido Schimmel4724ba562017-03-10 08:53:39 +01005976
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02005977 rif = mlxsw_sp_rif_alloc(ops->rif_size, rif_index, vr->id, params->dev);
Ido Schimmela13a5942017-05-26 08:37:33 +02005978 if (!rif) {
5979 err = -ENOMEM;
5980 goto err_rif_alloc;
5981 }
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02005982 rif->mlxsw_sp = mlxsw_sp;
5983 rif->ops = ops;
Ido Schimmela13a5942017-05-26 08:37:33 +02005984
Petr Machata010cadf2017-09-02 23:49:18 +02005985 if (ops->fid_get) {
5986 fid = ops->fid_get(rif);
5987 if (IS_ERR(fid)) {
5988 err = PTR_ERR(fid);
5989 goto err_fid_get;
5990 }
5991 rif->fid = fid;
Ido Schimmel4d93cee2017-05-26 08:37:34 +02005992 }
5993
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02005994 if (ops->setup)
5995 ops->setup(rif, params);
5996
5997 err = ops->configure(rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01005998 if (err)
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02005999 goto err_configure;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006000
Yotam Gigid42b0962017-09-27 08:23:20 +02006001 err = mlxsw_sp_mr_rif_add(vr->mr4_table, rif);
6002 if (err)
6003 goto err_mr_rif_add;
6004
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006005 mlxsw_sp_rif_counters_alloc(rif);
Ido Schimmel5f9efff2017-05-16 19:38:27 +02006006 mlxsw_sp->router->rifs[rif_index] = rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006007
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01006008 return rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006009
Yotam Gigid42b0962017-09-27 08:23:20 +02006010err_mr_rif_add:
6011 ops->deconfigure(rif);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006012err_configure:
Petr Machata010cadf2017-09-02 23:49:18 +02006013 if (fid)
6014 mlxsw_sp_fid_put(fid);
Ido Schimmela1107482017-05-26 08:37:39 +02006015err_fid_get:
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006016 kfree(rif);
6017err_rif_alloc:
Ido Schimmelde5ed992017-06-04 16:53:40 +02006018err_rif_index_alloc:
Petr Machata28a04c72017-10-02 12:14:56 +02006019 vr->rif_count--;
Ido Schimmelc9ec53f2017-05-26 08:37:38 +02006020 mlxsw_sp_vr_put(vr);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006021 return ERR_PTR(err);
6022}
6023
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006024void mlxsw_sp_rif_destroy(struct mlxsw_sp_rif *rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01006025{
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006026 const struct mlxsw_sp_rif_ops *ops = rif->ops;
6027 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
Ido Schimmela1107482017-05-26 08:37:39 +02006028 struct mlxsw_sp_fid *fid = rif->fid;
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006029 struct mlxsw_sp_vr *vr;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006030
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01006031 mlxsw_sp_router_rif_gone_sync(mlxsw_sp, rif);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006032 vr = &mlxsw_sp->router->vrs[rif->vr_id];
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +02006033
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006034 mlxsw_sp->router->rifs[rif->rif_index] = NULL;
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006035 mlxsw_sp_rif_counters_free(rif);
Yotam Gigid42b0962017-09-27 08:23:20 +02006036 mlxsw_sp_mr_rif_del(vr->mr4_table, rif);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006037 ops->deconfigure(rif);
Petr Machata010cadf2017-09-02 23:49:18 +02006038 if (fid)
6039 /* Loopback RIFs are not associated with a FID. */
6040 mlxsw_sp_fid_put(fid);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006041 kfree(rif);
Petr Machata28a04c72017-10-02 12:14:56 +02006042 vr->rif_count--;
Ido Schimmelc9ec53f2017-05-26 08:37:38 +02006043 mlxsw_sp_vr_put(vr);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006044}
6045
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006046static void
6047mlxsw_sp_rif_subport_params_init(struct mlxsw_sp_rif_params *params,
6048 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
6049{
6050 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
6051
6052 params->vid = mlxsw_sp_port_vlan->vid;
6053 params->lag = mlxsw_sp_port->lagged;
6054 if (params->lag)
6055 params->lag_id = mlxsw_sp_port->lag_id;
6056 else
6057 params->system_port = mlxsw_sp_port->local_port;
6058}
6059
Ido Schimmel7cbecf22017-05-26 08:37:28 +02006060static int
Ido Schimmela1107482017-05-26 08:37:39 +02006061mlxsw_sp_port_vlan_router_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
David Ahernf8fa9b42017-10-18 09:56:56 -07006062 struct net_device *l3_dev,
6063 struct netlink_ext_ack *extack)
Ido Schimmel4724ba562017-03-10 08:53:39 +01006064{
Ido Schimmel7cbecf22017-05-26 08:37:28 +02006065 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
Ido Schimmel1b8f09a2017-05-26 08:37:36 +02006066 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
Ido Schimmel7cbecf22017-05-26 08:37:28 +02006067 u16 vid = mlxsw_sp_port_vlan->vid;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01006068 struct mlxsw_sp_rif *rif;
Ido Schimmela1107482017-05-26 08:37:39 +02006069 struct mlxsw_sp_fid *fid;
Ido Schimmel03ea01e2017-05-23 21:56:30 +02006070 int err;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006071
Ido Schimmel1b8f09a2017-05-26 08:37:36 +02006072 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01006073 if (!rif) {
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006074 struct mlxsw_sp_rif_params params = {
6075 .dev = l3_dev,
6076 };
6077
6078 mlxsw_sp_rif_subport_params_init(&params, mlxsw_sp_port_vlan);
David Ahernf8fa9b42017-10-18 09:56:56 -07006079 rif = mlxsw_sp_rif_create(mlxsw_sp, &params, extack);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01006080 if (IS_ERR(rif))
6081 return PTR_ERR(rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006082 }
6083
Ido Schimmela1107482017-05-26 08:37:39 +02006084 /* FID was already created, just take a reference */
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006085 fid = rif->ops->fid_get(rif);
Ido Schimmela1107482017-05-26 08:37:39 +02006086 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
6087 if (err)
6088 goto err_fid_port_vid_map;
6089
Ido Schimmel7cbecf22017-05-26 08:37:28 +02006090 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
Ido Schimmel03ea01e2017-05-23 21:56:30 +02006091 if (err)
6092 goto err_port_vid_learning_set;
6093
Ido Schimmel7cbecf22017-05-26 08:37:28 +02006094 err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
Ido Schimmel03ea01e2017-05-23 21:56:30 +02006095 BR_STATE_FORWARDING);
6096 if (err)
6097 goto err_port_vid_stp_set;
6098
Ido Schimmela1107482017-05-26 08:37:39 +02006099 mlxsw_sp_port_vlan->fid = fid;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006100
Ido Schimmel4724ba562017-03-10 08:53:39 +01006101 return 0;
Ido Schimmel03ea01e2017-05-23 21:56:30 +02006102
6103err_port_vid_stp_set:
Ido Schimmel7cbecf22017-05-26 08:37:28 +02006104 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
Ido Schimmel03ea01e2017-05-23 21:56:30 +02006105err_port_vid_learning_set:
Ido Schimmela1107482017-05-26 08:37:39 +02006106 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
6107err_fid_port_vid_map:
6108 mlxsw_sp_fid_put(fid);
Ido Schimmel03ea01e2017-05-23 21:56:30 +02006109 return err;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006110}
6111
Ido Schimmela1107482017-05-26 08:37:39 +02006112void
6113mlxsw_sp_port_vlan_router_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
Ido Schimmel4724ba562017-03-10 08:53:39 +01006114{
Ido Schimmelce95e152017-05-26 08:37:27 +02006115 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
Ido Schimmel7cbecf22017-05-26 08:37:28 +02006116 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
Ido Schimmelce95e152017-05-26 08:37:27 +02006117 u16 vid = mlxsw_sp_port_vlan->vid;
Ido Schimmelce95e152017-05-26 08:37:27 +02006118
Ido Schimmela1107482017-05-26 08:37:39 +02006119 if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_RFID))
6120 return;
Ido Schimmel4aafc362017-05-26 08:37:25 +02006121
Ido Schimmela1107482017-05-26 08:37:39 +02006122 mlxsw_sp_port_vlan->fid = NULL;
Ido Schimmel7cbecf22017-05-26 08:37:28 +02006123 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_BLOCKING);
6124 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
Ido Schimmela1107482017-05-26 08:37:39 +02006125 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
6126 /* If router port holds the last reference on the rFID, then the
6127 * associated Sub-port RIF will be destroyed.
6128 */
6129 mlxsw_sp_fid_put(fid);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006130}
6131
Ido Schimmel7cbecf22017-05-26 08:37:28 +02006132static int mlxsw_sp_inetaddr_port_vlan_event(struct net_device *l3_dev,
6133 struct net_device *port_dev,
David Ahernf8fa9b42017-10-18 09:56:56 -07006134 unsigned long event, u16 vid,
6135 struct netlink_ext_ack *extack)
Ido Schimmel4724ba562017-03-10 08:53:39 +01006136{
6137 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(port_dev);
Ido Schimmelce95e152017-05-26 08:37:27 +02006138 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006139
Ido Schimmelce95e152017-05-26 08:37:27 +02006140 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
Ido Schimmel7cbecf22017-05-26 08:37:28 +02006141 if (WARN_ON(!mlxsw_sp_port_vlan))
6142 return -EINVAL;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006143
6144 switch (event) {
6145 case NETDEV_UP:
Ido Schimmela1107482017-05-26 08:37:39 +02006146 return mlxsw_sp_port_vlan_router_join(mlxsw_sp_port_vlan,
David Ahernf8fa9b42017-10-18 09:56:56 -07006147 l3_dev, extack);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006148 case NETDEV_DOWN:
Ido Schimmela1107482017-05-26 08:37:39 +02006149 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006150 break;
6151 }
6152
6153 return 0;
6154}
6155
6156static int mlxsw_sp_inetaddr_port_event(struct net_device *port_dev,
David Ahernf8fa9b42017-10-18 09:56:56 -07006157 unsigned long event,
6158 struct netlink_ext_ack *extack)
Ido Schimmel4724ba562017-03-10 08:53:39 +01006159{
Jiri Pirko2b94e582017-04-18 16:55:37 +02006160 if (netif_is_bridge_port(port_dev) ||
6161 netif_is_lag_port(port_dev) ||
6162 netif_is_ovs_port(port_dev))
Ido Schimmel4724ba562017-03-10 08:53:39 +01006163 return 0;
6164
David Ahernf8fa9b42017-10-18 09:56:56 -07006165 return mlxsw_sp_inetaddr_port_vlan_event(port_dev, port_dev, event, 1,
6166 extack);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006167}
6168
6169static int __mlxsw_sp_inetaddr_lag_event(struct net_device *l3_dev,
6170 struct net_device *lag_dev,
David Ahernf8fa9b42017-10-18 09:56:56 -07006171 unsigned long event, u16 vid,
6172 struct netlink_ext_ack *extack)
Ido Schimmel4724ba562017-03-10 08:53:39 +01006173{
6174 struct net_device *port_dev;
6175 struct list_head *iter;
6176 int err;
6177
6178 netdev_for_each_lower_dev(lag_dev, port_dev, iter) {
6179 if (mlxsw_sp_port_dev_check(port_dev)) {
Ido Schimmel7cbecf22017-05-26 08:37:28 +02006180 err = mlxsw_sp_inetaddr_port_vlan_event(l3_dev,
6181 port_dev,
David Ahernf8fa9b42017-10-18 09:56:56 -07006182 event, vid,
6183 extack);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006184 if (err)
6185 return err;
6186 }
6187 }
6188
6189 return 0;
6190}
6191
6192static int mlxsw_sp_inetaddr_lag_event(struct net_device *lag_dev,
David Ahernf8fa9b42017-10-18 09:56:56 -07006193 unsigned long event,
6194 struct netlink_ext_ack *extack)
Ido Schimmel4724ba562017-03-10 08:53:39 +01006195{
6196 if (netif_is_bridge_port(lag_dev))
6197 return 0;
6198
David Ahernf8fa9b42017-10-18 09:56:56 -07006199 return __mlxsw_sp_inetaddr_lag_event(lag_dev, lag_dev, event, 1,
6200 extack);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006201}
6202
Ido Schimmel4724ba562017-03-10 08:53:39 +01006203static int mlxsw_sp_inetaddr_bridge_event(struct net_device *l3_dev,
David Ahernf8fa9b42017-10-18 09:56:56 -07006204 unsigned long event,
6205 struct netlink_ext_ack *extack)
Ido Schimmel4724ba562017-03-10 08:53:39 +01006206{
6207 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006208 struct mlxsw_sp_rif_params params = {
6209 .dev = l3_dev,
6210 };
Ido Schimmela1107482017-05-26 08:37:39 +02006211 struct mlxsw_sp_rif *rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006212
6213 switch (event) {
6214 case NETDEV_UP:
David Ahernf8fa9b42017-10-18 09:56:56 -07006215 rif = mlxsw_sp_rif_create(mlxsw_sp, &params, extack);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006216 if (IS_ERR(rif))
6217 return PTR_ERR(rif);
6218 break;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006219 case NETDEV_DOWN:
Ido Schimmela1107482017-05-26 08:37:39 +02006220 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006221 mlxsw_sp_rif_destroy(rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006222 break;
6223 }
6224
6225 return 0;
6226}
6227
6228static int mlxsw_sp_inetaddr_vlan_event(struct net_device *vlan_dev,
David Ahernf8fa9b42017-10-18 09:56:56 -07006229 unsigned long event,
6230 struct netlink_ext_ack *extack)
Ido Schimmel4724ba562017-03-10 08:53:39 +01006231{
6232 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006233 u16 vid = vlan_dev_vlan_id(vlan_dev);
6234
Ido Schimmel6b27c8a2017-06-28 09:03:12 +03006235 if (netif_is_bridge_port(vlan_dev))
6236 return 0;
6237
Ido Schimmel4724ba562017-03-10 08:53:39 +01006238 if (mlxsw_sp_port_dev_check(real_dev))
Ido Schimmel7cbecf22017-05-26 08:37:28 +02006239 return mlxsw_sp_inetaddr_port_vlan_event(vlan_dev, real_dev,
David Ahernf8fa9b42017-10-18 09:56:56 -07006240 event, vid, extack);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006241 else if (netif_is_lag_master(real_dev))
6242 return __mlxsw_sp_inetaddr_lag_event(vlan_dev, real_dev, event,
David Ahernf8fa9b42017-10-18 09:56:56 -07006243 vid, extack);
Ido Schimmelc57529e2017-05-26 08:37:31 +02006244 else if (netif_is_bridge_master(real_dev) && br_vlan_enabled(real_dev))
David Ahernf8fa9b42017-10-18 09:56:56 -07006245 return mlxsw_sp_inetaddr_bridge_event(vlan_dev, event, extack);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006246
6247 return 0;
6248}
6249
Ido Schimmelb1e45522017-04-30 19:47:14 +03006250static int __mlxsw_sp_inetaddr_event(struct net_device *dev,
David Ahernf8fa9b42017-10-18 09:56:56 -07006251 unsigned long event,
6252 struct netlink_ext_ack *extack)
Ido Schimmelb1e45522017-04-30 19:47:14 +03006253{
6254 if (mlxsw_sp_port_dev_check(dev))
David Ahernf8fa9b42017-10-18 09:56:56 -07006255 return mlxsw_sp_inetaddr_port_event(dev, event, extack);
Ido Schimmelb1e45522017-04-30 19:47:14 +03006256 else if (netif_is_lag_master(dev))
David Ahernf8fa9b42017-10-18 09:56:56 -07006257 return mlxsw_sp_inetaddr_lag_event(dev, event, extack);
Ido Schimmelb1e45522017-04-30 19:47:14 +03006258 else if (netif_is_bridge_master(dev))
David Ahernf8fa9b42017-10-18 09:56:56 -07006259 return mlxsw_sp_inetaddr_bridge_event(dev, event, extack);
Ido Schimmelb1e45522017-04-30 19:47:14 +03006260 else if (is_vlan_dev(dev))
David Ahernf8fa9b42017-10-18 09:56:56 -07006261 return mlxsw_sp_inetaddr_vlan_event(dev, event, extack);
Ido Schimmelb1e45522017-04-30 19:47:14 +03006262 else
6263 return 0;
6264}
6265
Ido Schimmel4724ba562017-03-10 08:53:39 +01006266int mlxsw_sp_inetaddr_event(struct notifier_block *unused,
6267 unsigned long event, void *ptr)
6268{
6269 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
6270 struct net_device *dev = ifa->ifa_dev->dev;
6271 struct mlxsw_sp *mlxsw_sp;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01006272 struct mlxsw_sp_rif *rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006273 int err = 0;
6274
David Ahern89d5dd22017-10-18 09:56:55 -07006275 /* NETDEV_UP event is handled by mlxsw_sp_inetaddr_valid_event */
6276 if (event == NETDEV_UP)
6277 goto out;
6278
6279 mlxsw_sp = mlxsw_sp_lower_get(dev);
6280 if (!mlxsw_sp)
6281 goto out;
6282
6283 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
6284 if (!mlxsw_sp_rif_should_config(rif, dev, event))
6285 goto out;
6286
David Ahernf8fa9b42017-10-18 09:56:56 -07006287 err = __mlxsw_sp_inetaddr_event(dev, event, NULL);
David Ahern89d5dd22017-10-18 09:56:55 -07006288out:
6289 return notifier_from_errno(err);
6290}
6291
6292int mlxsw_sp_inetaddr_valid_event(struct notifier_block *unused,
6293 unsigned long event, void *ptr)
6294{
6295 struct in_validator_info *ivi = (struct in_validator_info *) ptr;
6296 struct net_device *dev = ivi->ivi_dev->dev;
6297 struct mlxsw_sp *mlxsw_sp;
6298 struct mlxsw_sp_rif *rif;
6299 int err = 0;
6300
Ido Schimmel4724ba562017-03-10 08:53:39 +01006301 mlxsw_sp = mlxsw_sp_lower_get(dev);
6302 if (!mlxsw_sp)
6303 goto out;
6304
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01006305 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
Arkadi Sharshevsky5ea12372017-07-18 10:10:13 +02006306 if (!mlxsw_sp_rif_should_config(rif, dev, event))
Ido Schimmel4724ba562017-03-10 08:53:39 +01006307 goto out;
6308
David Ahernf8fa9b42017-10-18 09:56:56 -07006309 err = __mlxsw_sp_inetaddr_event(dev, event, ivi->extack);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006310out:
6311 return notifier_from_errno(err);
6312}
6313
Arkadi Sharshevsky5ea12372017-07-18 10:10:13 +02006314struct mlxsw_sp_inet6addr_event_work {
6315 struct work_struct work;
6316 struct net_device *dev;
6317 unsigned long event;
6318};
6319
6320static void mlxsw_sp_inet6addr_event_work(struct work_struct *work)
6321{
6322 struct mlxsw_sp_inet6addr_event_work *inet6addr_work =
6323 container_of(work, struct mlxsw_sp_inet6addr_event_work, work);
6324 struct net_device *dev = inet6addr_work->dev;
6325 unsigned long event = inet6addr_work->event;
6326 struct mlxsw_sp *mlxsw_sp;
6327 struct mlxsw_sp_rif *rif;
6328
6329 rtnl_lock();
6330 mlxsw_sp = mlxsw_sp_lower_get(dev);
6331 if (!mlxsw_sp)
6332 goto out;
6333
6334 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
6335 if (!mlxsw_sp_rif_should_config(rif, dev, event))
6336 goto out;
6337
David Ahernf8fa9b42017-10-18 09:56:56 -07006338 __mlxsw_sp_inetaddr_event(dev, event, NULL);
Arkadi Sharshevsky5ea12372017-07-18 10:10:13 +02006339out:
6340 rtnl_unlock();
6341 dev_put(dev);
6342 kfree(inet6addr_work);
6343}
6344
6345/* Called with rcu_read_lock() */
6346int mlxsw_sp_inet6addr_event(struct notifier_block *unused,
6347 unsigned long event, void *ptr)
6348{
6349 struct inet6_ifaddr *if6 = (struct inet6_ifaddr *) ptr;
6350 struct mlxsw_sp_inet6addr_event_work *inet6addr_work;
6351 struct net_device *dev = if6->idev->dev;
6352
David Ahern89d5dd22017-10-18 09:56:55 -07006353 /* NETDEV_UP event is handled by mlxsw_sp_inet6addr_valid_event */
6354 if (event == NETDEV_UP)
6355 return NOTIFY_DONE;
6356
Arkadi Sharshevsky5ea12372017-07-18 10:10:13 +02006357 if (!mlxsw_sp_port_dev_lower_find_rcu(dev))
6358 return NOTIFY_DONE;
6359
6360 inet6addr_work = kzalloc(sizeof(*inet6addr_work), GFP_ATOMIC);
6361 if (!inet6addr_work)
6362 return NOTIFY_BAD;
6363
6364 INIT_WORK(&inet6addr_work->work, mlxsw_sp_inet6addr_event_work);
6365 inet6addr_work->dev = dev;
6366 inet6addr_work->event = event;
6367 dev_hold(dev);
6368 mlxsw_core_schedule_work(&inet6addr_work->work);
6369
6370 return NOTIFY_DONE;
6371}
6372
David Ahern89d5dd22017-10-18 09:56:55 -07006373int mlxsw_sp_inet6addr_valid_event(struct notifier_block *unused,
6374 unsigned long event, void *ptr)
6375{
6376 struct in6_validator_info *i6vi = (struct in6_validator_info *) ptr;
6377 struct net_device *dev = i6vi->i6vi_dev->dev;
6378 struct mlxsw_sp *mlxsw_sp;
6379 struct mlxsw_sp_rif *rif;
6380 int err = 0;
6381
6382 mlxsw_sp = mlxsw_sp_lower_get(dev);
6383 if (!mlxsw_sp)
6384 goto out;
6385
6386 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
6387 if (!mlxsw_sp_rif_should_config(rif, dev, event))
6388 goto out;
6389
David Ahernf8fa9b42017-10-18 09:56:56 -07006390 err = __mlxsw_sp_inetaddr_event(dev, event, i6vi->extack);
David Ahern89d5dd22017-10-18 09:56:55 -07006391out:
6392 return notifier_from_errno(err);
6393}
6394
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01006395static int mlxsw_sp_rif_edit(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
Ido Schimmel4724ba562017-03-10 08:53:39 +01006396 const char *mac, int mtu)
6397{
6398 char ritr_pl[MLXSW_REG_RITR_LEN];
6399 int err;
6400
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01006401 mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006402 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
6403 if (err)
6404 return err;
6405
6406 mlxsw_reg_ritr_mtu_set(ritr_pl, mtu);
6407 mlxsw_reg_ritr_if_mac_memcpy_to(ritr_pl, mac);
6408 mlxsw_reg_ritr_op_set(ritr_pl, MLXSW_REG_RITR_RIF_CREATE);
6409 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
6410}
6411
6412int mlxsw_sp_netdevice_router_port_event(struct net_device *dev)
6413{
6414 struct mlxsw_sp *mlxsw_sp;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01006415 struct mlxsw_sp_rif *rif;
Ido Schimmela1107482017-05-26 08:37:39 +02006416 u16 fid_index;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006417 int err;
6418
6419 mlxsw_sp = mlxsw_sp_lower_get(dev);
6420 if (!mlxsw_sp)
6421 return 0;
6422
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01006423 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
6424 if (!rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01006425 return 0;
Ido Schimmela1107482017-05-26 08:37:39 +02006426 fid_index = mlxsw_sp_fid_index(rif->fid);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006427
Ido Schimmela1107482017-05-26 08:37:39 +02006428 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, fid_index, false);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006429 if (err)
6430 return err;
6431
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01006432 err = mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, dev->dev_addr,
6433 dev->mtu);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006434 if (err)
6435 goto err_rif_edit;
6436
Ido Schimmela1107482017-05-26 08:37:39 +02006437 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, dev->dev_addr, fid_index, true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006438 if (err)
6439 goto err_rif_fdb_op;
6440
Yotam Gigifd890fe2017-09-27 08:23:21 +02006441 if (rif->mtu != dev->mtu) {
6442 struct mlxsw_sp_vr *vr;
6443
6444 /* The RIF is relevant only to its mr_table instance, as unlike
6445 * unicast routing, in multicast routing a RIF cannot be shared
6446 * between several multicast routing tables.
6447 */
6448 vr = &mlxsw_sp->router->vrs[rif->vr_id];
6449 mlxsw_sp_mr_rif_mtu_update(vr->mr4_table, rif, dev->mtu);
6450 }
6451
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01006452 ether_addr_copy(rif->addr, dev->dev_addr);
6453 rif->mtu = dev->mtu;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006454
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01006455 netdev_dbg(dev, "Updated RIF=%d\n", rif->rif_index);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006456
6457 return 0;
6458
6459err_rif_fdb_op:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01006460 mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, rif->addr, rif->mtu);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006461err_rif_edit:
Ido Schimmela1107482017-05-26 08:37:39 +02006462 mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, fid_index, true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006463 return err;
6464}
6465
Ido Schimmelb1e45522017-04-30 19:47:14 +03006466static int mlxsw_sp_port_vrf_join(struct mlxsw_sp *mlxsw_sp,
David Ahernf8fa9b42017-10-18 09:56:56 -07006467 struct net_device *l3_dev,
6468 struct netlink_ext_ack *extack)
Ido Schimmel7179eb52017-03-16 09:08:18 +01006469{
Ido Schimmelb1e45522017-04-30 19:47:14 +03006470 struct mlxsw_sp_rif *rif;
Ido Schimmel7179eb52017-03-16 09:08:18 +01006471
Ido Schimmelb1e45522017-04-30 19:47:14 +03006472 /* If netdev is already associated with a RIF, then we need to
6473 * destroy it and create a new one with the new virtual router ID.
Ido Schimmel7179eb52017-03-16 09:08:18 +01006474 */
Ido Schimmelb1e45522017-04-30 19:47:14 +03006475 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
6476 if (rif)
David Ahernf8fa9b42017-10-18 09:56:56 -07006477 __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_DOWN, extack);
Ido Schimmel7179eb52017-03-16 09:08:18 +01006478
David Ahernf8fa9b42017-10-18 09:56:56 -07006479 return __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_UP, extack);
Ido Schimmel7179eb52017-03-16 09:08:18 +01006480}
6481
Ido Schimmelb1e45522017-04-30 19:47:14 +03006482static void mlxsw_sp_port_vrf_leave(struct mlxsw_sp *mlxsw_sp,
6483 struct net_device *l3_dev)
Ido Schimmel7179eb52017-03-16 09:08:18 +01006484{
Ido Schimmelb1e45522017-04-30 19:47:14 +03006485 struct mlxsw_sp_rif *rif;
Ido Schimmel7179eb52017-03-16 09:08:18 +01006486
Ido Schimmelb1e45522017-04-30 19:47:14 +03006487 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
6488 if (!rif)
Ido Schimmel7179eb52017-03-16 09:08:18 +01006489 return;
David Ahernf8fa9b42017-10-18 09:56:56 -07006490 __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_DOWN, NULL);
Ido Schimmel7179eb52017-03-16 09:08:18 +01006491}
6492
Ido Schimmelb1e45522017-04-30 19:47:14 +03006493int mlxsw_sp_netdevice_vrf_event(struct net_device *l3_dev, unsigned long event,
6494 struct netdev_notifier_changeupper_info *info)
Ido Schimmel3d70e4582017-03-16 09:08:19 +01006495{
Ido Schimmelb1e45522017-04-30 19:47:14 +03006496 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
6497 int err = 0;
Ido Schimmel3d70e4582017-03-16 09:08:19 +01006498
Ido Schimmelb1e45522017-04-30 19:47:14 +03006499 if (!mlxsw_sp)
6500 return 0;
Ido Schimmel3d70e4582017-03-16 09:08:19 +01006501
Ido Schimmelb1e45522017-04-30 19:47:14 +03006502 switch (event) {
6503 case NETDEV_PRECHANGEUPPER:
6504 return 0;
6505 case NETDEV_CHANGEUPPER:
David Ahernf8fa9b42017-10-18 09:56:56 -07006506 if (info->linking) {
6507 struct netlink_ext_ack *extack;
6508
6509 extack = netdev_notifier_info_to_extack(&info->info);
6510 err = mlxsw_sp_port_vrf_join(mlxsw_sp, l3_dev, extack);
6511 } else {
Ido Schimmelb1e45522017-04-30 19:47:14 +03006512 mlxsw_sp_port_vrf_leave(mlxsw_sp, l3_dev);
David Ahernf8fa9b42017-10-18 09:56:56 -07006513 }
Ido Schimmelb1e45522017-04-30 19:47:14 +03006514 break;
6515 }
Ido Schimmel3d70e4582017-03-16 09:08:19 +01006516
Ido Schimmelb1e45522017-04-30 19:47:14 +03006517 return err;
Ido Schimmel3d70e4582017-03-16 09:08:19 +01006518}
6519
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006520static struct mlxsw_sp_rif_subport *
6521mlxsw_sp_rif_subport_rif(const struct mlxsw_sp_rif *rif)
Ido Schimmela1107482017-05-26 08:37:39 +02006522{
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006523 return container_of(rif, struct mlxsw_sp_rif_subport, common);
Ido Schimmela1107482017-05-26 08:37:39 +02006524}
6525
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006526static void mlxsw_sp_rif_subport_setup(struct mlxsw_sp_rif *rif,
6527 const struct mlxsw_sp_rif_params *params)
6528{
6529 struct mlxsw_sp_rif_subport *rif_subport;
6530
6531 rif_subport = mlxsw_sp_rif_subport_rif(rif);
6532 rif_subport->vid = params->vid;
6533 rif_subport->lag = params->lag;
6534 if (params->lag)
6535 rif_subport->lag_id = params->lag_id;
6536 else
6537 rif_subport->system_port = params->system_port;
6538}
6539
6540static int mlxsw_sp_rif_subport_op(struct mlxsw_sp_rif *rif, bool enable)
6541{
6542 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6543 struct mlxsw_sp_rif_subport *rif_subport;
6544 char ritr_pl[MLXSW_REG_RITR_LEN];
6545
6546 rif_subport = mlxsw_sp_rif_subport_rif(rif);
6547 mlxsw_reg_ritr_pack(ritr_pl, enable, MLXSW_REG_RITR_SP_IF,
Petr Machata9571e822017-09-02 23:49:14 +02006548 rif->rif_index, rif->vr_id, rif->dev->mtu);
6549 mlxsw_reg_ritr_mac_pack(ritr_pl, rif->dev->dev_addr);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006550 mlxsw_reg_ritr_sp_if_pack(ritr_pl, rif_subport->lag,
6551 rif_subport->lag ? rif_subport->lag_id :
6552 rif_subport->system_port,
6553 rif_subport->vid);
6554
6555 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
6556}
6557
6558static int mlxsw_sp_rif_subport_configure(struct mlxsw_sp_rif *rif)
6559{
Petr Machata010cadf2017-09-02 23:49:18 +02006560 int err;
6561
6562 err = mlxsw_sp_rif_subport_op(rif, true);
6563 if (err)
6564 return err;
6565
6566 err = mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
6567 mlxsw_sp_fid_index(rif->fid), true);
6568 if (err)
6569 goto err_rif_fdb_op;
6570
6571 mlxsw_sp_fid_rif_set(rif->fid, rif);
6572 return 0;
6573
6574err_rif_fdb_op:
6575 mlxsw_sp_rif_subport_op(rif, false);
6576 return err;
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006577}
6578
6579static void mlxsw_sp_rif_subport_deconfigure(struct mlxsw_sp_rif *rif)
6580{
Petr Machata010cadf2017-09-02 23:49:18 +02006581 struct mlxsw_sp_fid *fid = rif->fid;
6582
6583 mlxsw_sp_fid_rif_set(fid, NULL);
6584 mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
6585 mlxsw_sp_fid_index(fid), false);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006586 mlxsw_sp_rif_subport_op(rif, false);
6587}
6588
6589static struct mlxsw_sp_fid *
6590mlxsw_sp_rif_subport_fid_get(struct mlxsw_sp_rif *rif)
6591{
6592 return mlxsw_sp_fid_rfid_get(rif->mlxsw_sp, rif->rif_index);
6593}
6594
6595static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_subport_ops = {
6596 .type = MLXSW_SP_RIF_TYPE_SUBPORT,
6597 .rif_size = sizeof(struct mlxsw_sp_rif_subport),
6598 .setup = mlxsw_sp_rif_subport_setup,
6599 .configure = mlxsw_sp_rif_subport_configure,
6600 .deconfigure = mlxsw_sp_rif_subport_deconfigure,
6601 .fid_get = mlxsw_sp_rif_subport_fid_get,
6602};
6603
6604static int mlxsw_sp_rif_vlan_fid_op(struct mlxsw_sp_rif *rif,
6605 enum mlxsw_reg_ritr_if_type type,
6606 u16 vid_fid, bool enable)
6607{
6608 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6609 char ritr_pl[MLXSW_REG_RITR_LEN];
6610
6611 mlxsw_reg_ritr_pack(ritr_pl, enable, type, rif->rif_index, rif->vr_id,
Petr Machata9571e822017-09-02 23:49:14 +02006612 rif->dev->mtu);
6613 mlxsw_reg_ritr_mac_pack(ritr_pl, rif->dev->dev_addr);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006614 mlxsw_reg_ritr_fid_set(ritr_pl, type, vid_fid);
6615
6616 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
6617}
6618
Yotam Gigib35750f2017-10-09 11:15:33 +02006619u8 mlxsw_sp_router_port(const struct mlxsw_sp *mlxsw_sp)
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006620{
6621 return mlxsw_core_max_ports(mlxsw_sp->core) + 1;
6622}
6623
6624static int mlxsw_sp_rif_vlan_configure(struct mlxsw_sp_rif *rif)
6625{
6626 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6627 u16 vid = mlxsw_sp_fid_8021q_vid(rif->fid);
6628 int err;
6629
6630 err = mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_VLAN_IF, vid, true);
6631 if (err)
6632 return err;
6633
Ido Schimmel0d284812017-07-18 10:10:12 +02006634 err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
6635 mlxsw_sp_router_port(mlxsw_sp), true);
6636 if (err)
6637 goto err_fid_mc_flood_set;
6638
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006639 err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
6640 mlxsw_sp_router_port(mlxsw_sp), true);
6641 if (err)
6642 goto err_fid_bc_flood_set;
6643
Petr Machata010cadf2017-09-02 23:49:18 +02006644 err = mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
6645 mlxsw_sp_fid_index(rif->fid), true);
6646 if (err)
6647 goto err_rif_fdb_op;
6648
6649 mlxsw_sp_fid_rif_set(rif->fid, rif);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006650 return 0;
6651
Petr Machata010cadf2017-09-02 23:49:18 +02006652err_rif_fdb_op:
6653 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
6654 mlxsw_sp_router_port(mlxsw_sp), false);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006655err_fid_bc_flood_set:
Ido Schimmel0d284812017-07-18 10:10:12 +02006656 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
6657 mlxsw_sp_router_port(mlxsw_sp), false);
6658err_fid_mc_flood_set:
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006659 mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_VLAN_IF, vid, false);
6660 return err;
6661}
6662
6663static void mlxsw_sp_rif_vlan_deconfigure(struct mlxsw_sp_rif *rif)
6664{
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006665 u16 vid = mlxsw_sp_fid_8021q_vid(rif->fid);
Petr Machata010cadf2017-09-02 23:49:18 +02006666 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6667 struct mlxsw_sp_fid *fid = rif->fid;
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006668
Petr Machata010cadf2017-09-02 23:49:18 +02006669 mlxsw_sp_fid_rif_set(fid, NULL);
6670 mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
6671 mlxsw_sp_fid_index(fid), false);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006672 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
6673 mlxsw_sp_router_port(mlxsw_sp), false);
Ido Schimmel0d284812017-07-18 10:10:12 +02006674 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
6675 mlxsw_sp_router_port(mlxsw_sp), false);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006676 mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_VLAN_IF, vid, false);
6677}
6678
6679static struct mlxsw_sp_fid *
6680mlxsw_sp_rif_vlan_fid_get(struct mlxsw_sp_rif *rif)
6681{
6682 u16 vid = is_vlan_dev(rif->dev) ? vlan_dev_vlan_id(rif->dev) : 1;
6683
6684 return mlxsw_sp_fid_8021q_get(rif->mlxsw_sp, vid);
6685}
6686
6687static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_vlan_ops = {
6688 .type = MLXSW_SP_RIF_TYPE_VLAN,
6689 .rif_size = sizeof(struct mlxsw_sp_rif),
6690 .configure = mlxsw_sp_rif_vlan_configure,
6691 .deconfigure = mlxsw_sp_rif_vlan_deconfigure,
6692 .fid_get = mlxsw_sp_rif_vlan_fid_get,
6693};
6694
6695static int mlxsw_sp_rif_fid_configure(struct mlxsw_sp_rif *rif)
6696{
6697 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6698 u16 fid_index = mlxsw_sp_fid_index(rif->fid);
6699 int err;
6700
6701 err = mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index,
6702 true);
6703 if (err)
6704 return err;
6705
Ido Schimmel0d284812017-07-18 10:10:12 +02006706 err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
6707 mlxsw_sp_router_port(mlxsw_sp), true);
6708 if (err)
6709 goto err_fid_mc_flood_set;
6710
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006711 err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
6712 mlxsw_sp_router_port(mlxsw_sp), true);
6713 if (err)
6714 goto err_fid_bc_flood_set;
6715
Petr Machata010cadf2017-09-02 23:49:18 +02006716 err = mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
6717 mlxsw_sp_fid_index(rif->fid), true);
6718 if (err)
6719 goto err_rif_fdb_op;
6720
6721 mlxsw_sp_fid_rif_set(rif->fid, rif);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006722 return 0;
6723
Petr Machata010cadf2017-09-02 23:49:18 +02006724err_rif_fdb_op:
6725 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
6726 mlxsw_sp_router_port(mlxsw_sp), false);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006727err_fid_bc_flood_set:
Ido Schimmel0d284812017-07-18 10:10:12 +02006728 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
6729 mlxsw_sp_router_port(mlxsw_sp), false);
6730err_fid_mc_flood_set:
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006731 mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index, false);
6732 return err;
6733}
6734
6735static void mlxsw_sp_rif_fid_deconfigure(struct mlxsw_sp_rif *rif)
6736{
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006737 u16 fid_index = mlxsw_sp_fid_index(rif->fid);
Petr Machata010cadf2017-09-02 23:49:18 +02006738 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6739 struct mlxsw_sp_fid *fid = rif->fid;
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006740
Petr Machata010cadf2017-09-02 23:49:18 +02006741 mlxsw_sp_fid_rif_set(fid, NULL);
6742 mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
6743 mlxsw_sp_fid_index(fid), false);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006744 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
6745 mlxsw_sp_router_port(mlxsw_sp), false);
Ido Schimmel0d284812017-07-18 10:10:12 +02006746 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
6747 mlxsw_sp_router_port(mlxsw_sp), false);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006748 mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index, false);
6749}
6750
6751static struct mlxsw_sp_fid *
6752mlxsw_sp_rif_fid_fid_get(struct mlxsw_sp_rif *rif)
6753{
6754 return mlxsw_sp_fid_8021d_get(rif->mlxsw_sp, rif->dev->ifindex);
6755}
6756
6757static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_fid_ops = {
6758 .type = MLXSW_SP_RIF_TYPE_FID,
6759 .rif_size = sizeof(struct mlxsw_sp_rif),
6760 .configure = mlxsw_sp_rif_fid_configure,
6761 .deconfigure = mlxsw_sp_rif_fid_deconfigure,
6762 .fid_get = mlxsw_sp_rif_fid_fid_get,
6763};
6764
Petr Machata6ddb7422017-09-02 23:49:19 +02006765static struct mlxsw_sp_rif_ipip_lb *
6766mlxsw_sp_rif_ipip_lb_rif(struct mlxsw_sp_rif *rif)
6767{
6768 return container_of(rif, struct mlxsw_sp_rif_ipip_lb, common);
6769}
6770
6771static void
6772mlxsw_sp_rif_ipip_lb_setup(struct mlxsw_sp_rif *rif,
6773 const struct mlxsw_sp_rif_params *params)
6774{
6775 struct mlxsw_sp_rif_params_ipip_lb *params_lb;
6776 struct mlxsw_sp_rif_ipip_lb *rif_lb;
6777
6778 params_lb = container_of(params, struct mlxsw_sp_rif_params_ipip_lb,
6779 common);
6780 rif_lb = mlxsw_sp_rif_ipip_lb_rif(rif);
6781 rif_lb->lb_config = params_lb->lb_config;
6782}
6783
6784static int
6785mlxsw_sp_rif_ipip_lb_op(struct mlxsw_sp_rif_ipip_lb *lb_rif,
6786 struct mlxsw_sp_vr *ul_vr, bool enable)
6787{
6788 struct mlxsw_sp_rif_ipip_lb_config lb_cf = lb_rif->lb_config;
6789 struct mlxsw_sp_rif *rif = &lb_rif->common;
6790 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6791 char ritr_pl[MLXSW_REG_RITR_LEN];
6792 u32 saddr4;
6793
6794 switch (lb_cf.ul_protocol) {
6795 case MLXSW_SP_L3_PROTO_IPV4:
6796 saddr4 = be32_to_cpu(lb_cf.saddr.addr4);
6797 mlxsw_reg_ritr_pack(ritr_pl, enable, MLXSW_REG_RITR_LOOPBACK_IF,
6798 rif->rif_index, rif->vr_id, rif->dev->mtu);
6799 mlxsw_reg_ritr_loopback_ipip4_pack(ritr_pl, lb_cf.lb_ipipt,
6800 MLXSW_REG_RITR_LOOPBACK_IPIP_OPTIONS_GRE_KEY_PRESET,
6801 ul_vr->id, saddr4, lb_cf.okey);
6802 break;
6803
6804 case MLXSW_SP_L3_PROTO_IPV6:
6805 return -EAFNOSUPPORT;
6806 }
6807
6808 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
6809}
6810
6811static int
6812mlxsw_sp_rif_ipip_lb_configure(struct mlxsw_sp_rif *rif)
6813{
6814 struct mlxsw_sp_rif_ipip_lb *lb_rif = mlxsw_sp_rif_ipip_lb_rif(rif);
6815 u32 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(rif->dev);
6816 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6817 struct mlxsw_sp_vr *ul_vr;
6818 int err;
6819
David Ahernf8fa9b42017-10-18 09:56:56 -07006820 ul_vr = mlxsw_sp_vr_get(mlxsw_sp, ul_tb_id, NULL);
Petr Machata6ddb7422017-09-02 23:49:19 +02006821 if (IS_ERR(ul_vr))
6822 return PTR_ERR(ul_vr);
6823
6824 err = mlxsw_sp_rif_ipip_lb_op(lb_rif, ul_vr, true);
6825 if (err)
6826 goto err_loopback_op;
6827
6828 lb_rif->ul_vr_id = ul_vr->id;
6829 ++ul_vr->rif_count;
6830 return 0;
6831
6832err_loopback_op:
6833 mlxsw_sp_vr_put(ul_vr);
6834 return err;
6835}
6836
6837static void mlxsw_sp_rif_ipip_lb_deconfigure(struct mlxsw_sp_rif *rif)
6838{
6839 struct mlxsw_sp_rif_ipip_lb *lb_rif = mlxsw_sp_rif_ipip_lb_rif(rif);
6840 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6841 struct mlxsw_sp_vr *ul_vr;
6842
6843 ul_vr = &mlxsw_sp->router->vrs[lb_rif->ul_vr_id];
6844 mlxsw_sp_rif_ipip_lb_op(lb_rif, ul_vr, false);
6845
6846 --ul_vr->rif_count;
6847 mlxsw_sp_vr_put(ul_vr);
6848}
6849
6850static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_ipip_lb_ops = {
6851 .type = MLXSW_SP_RIF_TYPE_IPIP_LB,
6852 .rif_size = sizeof(struct mlxsw_sp_rif_ipip_lb),
6853 .setup = mlxsw_sp_rif_ipip_lb_setup,
6854 .configure = mlxsw_sp_rif_ipip_lb_configure,
6855 .deconfigure = mlxsw_sp_rif_ipip_lb_deconfigure,
6856};
6857
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006858static const struct mlxsw_sp_rif_ops *mlxsw_sp_rif_ops_arr[] = {
6859 [MLXSW_SP_RIF_TYPE_SUBPORT] = &mlxsw_sp_rif_subport_ops,
6860 [MLXSW_SP_RIF_TYPE_VLAN] = &mlxsw_sp_rif_vlan_ops,
6861 [MLXSW_SP_RIF_TYPE_FID] = &mlxsw_sp_rif_fid_ops,
Petr Machata6ddb7422017-09-02 23:49:19 +02006862 [MLXSW_SP_RIF_TYPE_IPIP_LB] = &mlxsw_sp_rif_ipip_lb_ops,
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006863};
6864
Ido Schimmel348b8fc2017-05-16 19:38:29 +02006865static int mlxsw_sp_rifs_init(struct mlxsw_sp *mlxsw_sp)
6866{
6867 u64 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
6868
6869 mlxsw_sp->router->rifs = kcalloc(max_rifs,
6870 sizeof(struct mlxsw_sp_rif *),
6871 GFP_KERNEL);
6872 if (!mlxsw_sp->router->rifs)
6873 return -ENOMEM;
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02006874
6875 mlxsw_sp->router->rif_ops_arr = mlxsw_sp_rif_ops_arr;
6876
Ido Schimmel348b8fc2017-05-16 19:38:29 +02006877 return 0;
6878}
6879
6880static void mlxsw_sp_rifs_fini(struct mlxsw_sp *mlxsw_sp)
6881{
6882 int i;
6883
6884 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
6885 WARN_ON_ONCE(mlxsw_sp->router->rifs[i]);
6886
6887 kfree(mlxsw_sp->router->rifs);
6888}
6889
Petr Machatadcbda282017-10-20 09:16:16 +02006890static int
6891mlxsw_sp_ipip_config_tigcr(struct mlxsw_sp *mlxsw_sp)
6892{
6893 char tigcr_pl[MLXSW_REG_TIGCR_LEN];
6894
6895 mlxsw_reg_tigcr_pack(tigcr_pl, true, 0);
6896 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(tigcr), tigcr_pl);
6897}
6898
Petr Machata38ebc0f2017-09-02 23:49:17 +02006899static int mlxsw_sp_ipips_init(struct mlxsw_sp *mlxsw_sp)
6900{
6901 mlxsw_sp->router->ipip_ops_arr = mlxsw_sp_ipip_ops_arr;
Petr Machata1012b9a2017-09-02 23:49:23 +02006902 INIT_LIST_HEAD(&mlxsw_sp->router->ipip_list);
Petr Machatadcbda282017-10-20 09:16:16 +02006903 return mlxsw_sp_ipip_config_tigcr(mlxsw_sp);
Petr Machata38ebc0f2017-09-02 23:49:17 +02006904}
6905
6906static void mlxsw_sp_ipips_fini(struct mlxsw_sp *mlxsw_sp)
6907{
Petr Machata1012b9a2017-09-02 23:49:23 +02006908 WARN_ON(!list_empty(&mlxsw_sp->router->ipip_list));
Petr Machata38ebc0f2017-09-02 23:49:17 +02006909}
6910
Ido Schimmelc3852ef2016-12-03 16:45:07 +01006911static void mlxsw_sp_router_fib_dump_flush(struct notifier_block *nb)
6912{
Ido Schimmel7e39d112017-05-16 19:38:28 +02006913 struct mlxsw_sp_router *router;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01006914
6915 /* Flush pending FIB notifications and then flush the device's
6916 * table before requesting another dump. The FIB notification
6917 * block is unregistered, so no need to take RTNL.
6918 */
6919 mlxsw_core_flush_owq();
Ido Schimmel7e39d112017-05-16 19:38:28 +02006920 router = container_of(nb, struct mlxsw_sp_router, fib_nb);
6921 mlxsw_sp_router_fib_flush(router->mlxsw_sp);
Ido Schimmelc3852ef2016-12-03 16:45:07 +01006922}
6923
Ido Schimmelaf658b62017-11-02 17:14:09 +01006924#ifdef CONFIG_IP_ROUTE_MULTIPATH
6925static void mlxsw_sp_mp_hash_header_set(char *recr2_pl, int header)
6926{
6927 mlxsw_reg_recr2_outer_header_enables_set(recr2_pl, header, true);
6928}
6929
6930static void mlxsw_sp_mp_hash_field_set(char *recr2_pl, int field)
6931{
6932 mlxsw_reg_recr2_outer_header_fields_enable_set(recr2_pl, field, true);
6933}
6934
6935static void mlxsw_sp_mp4_hash_init(char *recr2_pl)
6936{
6937 bool only_l3 = !init_net.ipv4.sysctl_fib_multipath_hash_policy;
6938
6939 mlxsw_sp_mp_hash_header_set(recr2_pl,
6940 MLXSW_REG_RECR2_IPV4_EN_NOT_TCP_NOT_UDP);
6941 mlxsw_sp_mp_hash_header_set(recr2_pl, MLXSW_REG_RECR2_IPV4_EN_TCP_UDP);
6942 mlxsw_reg_recr2_ipv4_sip_enable(recr2_pl);
6943 mlxsw_reg_recr2_ipv4_dip_enable(recr2_pl);
6944 if (only_l3)
6945 return;
6946 mlxsw_sp_mp_hash_header_set(recr2_pl, MLXSW_REG_RECR2_TCP_UDP_EN_IPV4);
6947 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_IPV4_PROTOCOL);
6948 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_TCP_UDP_SPORT);
6949 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_TCP_UDP_DPORT);
6950}
6951
6952static void mlxsw_sp_mp6_hash_init(char *recr2_pl)
6953{
6954 mlxsw_sp_mp_hash_header_set(recr2_pl,
6955 MLXSW_REG_RECR2_IPV6_EN_NOT_TCP_NOT_UDP);
6956 mlxsw_sp_mp_hash_header_set(recr2_pl, MLXSW_REG_RECR2_IPV6_EN_TCP_UDP);
6957 mlxsw_reg_recr2_ipv6_sip_enable(recr2_pl);
6958 mlxsw_reg_recr2_ipv6_dip_enable(recr2_pl);
6959 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_IPV6_FLOW_LABEL);
6960 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_IPV6_NEXT_HEADER);
6961}
6962
6963static int mlxsw_sp_mp_hash_init(struct mlxsw_sp *mlxsw_sp)
6964{
6965 char recr2_pl[MLXSW_REG_RECR2_LEN];
6966 u32 seed;
6967
6968 get_random_bytes(&seed, sizeof(seed));
6969 mlxsw_reg_recr2_pack(recr2_pl, seed);
6970 mlxsw_sp_mp4_hash_init(recr2_pl);
6971 mlxsw_sp_mp6_hash_init(recr2_pl);
6972
6973 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(recr2), recr2_pl);
6974}
6975#else
6976static int mlxsw_sp_mp_hash_init(struct mlxsw_sp *mlxsw_sp)
6977{
6978 return 0;
6979}
6980#endif
6981
Ido Schimmel4724ba562017-03-10 08:53:39 +01006982static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
6983{
6984 char rgcr_pl[MLXSW_REG_RGCR_LEN];
6985 u64 max_rifs;
6986 int err;
6987
6988 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_RIFS))
6989 return -EIO;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006990 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006991
Arkadi Sharshevskye29237e2017-07-18 10:10:09 +02006992 mlxsw_reg_rgcr_pack(rgcr_pl, true, true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01006993 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, max_rifs);
6994 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
6995 if (err)
Ido Schimmel348b8fc2017-05-16 19:38:29 +02006996 return err;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006997 return 0;
Ido Schimmel4724ba562017-03-10 08:53:39 +01006998}
6999
7000static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
7001{
7002 char rgcr_pl[MLXSW_REG_RGCR_LEN];
Ido Schimmel4724ba562017-03-10 08:53:39 +01007003
Arkadi Sharshevskye29237e2017-07-18 10:10:09 +02007004 mlxsw_reg_rgcr_pack(rgcr_pl, false, false);
Ido Schimmel4724ba562017-03-10 08:53:39 +01007005 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
Ido Schimmel4724ba562017-03-10 08:53:39 +01007006}
7007
Jiri Pirkob45f64d2016-09-26 12:52:31 +02007008int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
7009{
Ido Schimmel9011b672017-05-16 19:38:25 +02007010 struct mlxsw_sp_router *router;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02007011 int err;
7012
Ido Schimmel9011b672017-05-16 19:38:25 +02007013 router = kzalloc(sizeof(*mlxsw_sp->router), GFP_KERNEL);
7014 if (!router)
7015 return -ENOMEM;
7016 mlxsw_sp->router = router;
7017 router->mlxsw_sp = mlxsw_sp;
7018
7019 INIT_LIST_HEAD(&mlxsw_sp->router->nexthop_neighs_list);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02007020 err = __mlxsw_sp_router_init(mlxsw_sp);
7021 if (err)
Ido Schimmel9011b672017-05-16 19:38:25 +02007022 goto err_router_init;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02007023
Ido Schimmel348b8fc2017-05-16 19:38:29 +02007024 err = mlxsw_sp_rifs_init(mlxsw_sp);
7025 if (err)
7026 goto err_rifs_init;
7027
Petr Machata38ebc0f2017-09-02 23:49:17 +02007028 err = mlxsw_sp_ipips_init(mlxsw_sp);
7029 if (err)
7030 goto err_ipips_init;
7031
Ido Schimmel9011b672017-05-16 19:38:25 +02007032 err = rhashtable_init(&mlxsw_sp->router->nexthop_ht,
Ido Schimmelc53b8e12017-02-08 11:16:30 +01007033 &mlxsw_sp_nexthop_ht_params);
7034 if (err)
7035 goto err_nexthop_ht_init;
7036
Ido Schimmel9011b672017-05-16 19:38:25 +02007037 err = rhashtable_init(&mlxsw_sp->router->nexthop_group_ht,
Ido Schimmele9ad5e72017-02-08 11:16:29 +01007038 &mlxsw_sp_nexthop_group_ht_params);
7039 if (err)
7040 goto err_nexthop_group_ht_init;
7041
Arkadi Sharshevskydbe45982017-09-25 10:32:23 +02007042 INIT_LIST_HEAD(&mlxsw_sp->router->nexthop_list);
Ido Schimmel8494ab02017-03-24 08:02:47 +01007043 err = mlxsw_sp_lpm_init(mlxsw_sp);
7044 if (err)
7045 goto err_lpm_init;
7046
Yotam Gigid42b0962017-09-27 08:23:20 +02007047 err = mlxsw_sp_mr_init(mlxsw_sp, &mlxsw_sp_mr_tcam_ops);
7048 if (err)
7049 goto err_mr_init;
7050
Jiri Pirkob45f64d2016-09-26 12:52:31 +02007051 err = mlxsw_sp_vrs_init(mlxsw_sp);
7052 if (err)
7053 goto err_vrs_init;
7054
Ido Schimmel8c9583a2016-10-27 15:12:57 +02007055 err = mlxsw_sp_neigh_init(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02007056 if (err)
7057 goto err_neigh_init;
7058
Ido Schimmel48fac882017-11-02 17:14:06 +01007059 mlxsw_sp->router->netevent_nb.notifier_call =
7060 mlxsw_sp_router_netevent_event;
7061 err = register_netevent_notifier(&mlxsw_sp->router->netevent_nb);
7062 if (err)
7063 goto err_register_netevent_notifier;
7064
Ido Schimmelaf658b62017-11-02 17:14:09 +01007065 err = mlxsw_sp_mp_hash_init(mlxsw_sp);
7066 if (err)
7067 goto err_mp_hash_init;
7068
Ido Schimmel7e39d112017-05-16 19:38:28 +02007069 mlxsw_sp->router->fib_nb.notifier_call = mlxsw_sp_router_fib_event;
7070 err = register_fib_notifier(&mlxsw_sp->router->fib_nb,
Ido Schimmelc3852ef2016-12-03 16:45:07 +01007071 mlxsw_sp_router_fib_dump_flush);
7072 if (err)
7073 goto err_register_fib_notifier;
7074
Jiri Pirkob45f64d2016-09-26 12:52:31 +02007075 return 0;
7076
Ido Schimmelc3852ef2016-12-03 16:45:07 +01007077err_register_fib_notifier:
Ido Schimmelaf658b62017-11-02 17:14:09 +01007078err_mp_hash_init:
Ido Schimmel48fac882017-11-02 17:14:06 +01007079 unregister_netevent_notifier(&mlxsw_sp->router->netevent_nb);
7080err_register_netevent_notifier:
Ido Schimmelc3852ef2016-12-03 16:45:07 +01007081 mlxsw_sp_neigh_fini(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02007082err_neigh_init:
7083 mlxsw_sp_vrs_fini(mlxsw_sp);
7084err_vrs_init:
Yotam Gigid42b0962017-09-27 08:23:20 +02007085 mlxsw_sp_mr_fini(mlxsw_sp);
7086err_mr_init:
Ido Schimmel8494ab02017-03-24 08:02:47 +01007087 mlxsw_sp_lpm_fini(mlxsw_sp);
7088err_lpm_init:
Ido Schimmel9011b672017-05-16 19:38:25 +02007089 rhashtable_destroy(&mlxsw_sp->router->nexthop_group_ht);
Ido Schimmele9ad5e72017-02-08 11:16:29 +01007090err_nexthop_group_ht_init:
Ido Schimmel9011b672017-05-16 19:38:25 +02007091 rhashtable_destroy(&mlxsw_sp->router->nexthop_ht);
Ido Schimmelc53b8e12017-02-08 11:16:30 +01007092err_nexthop_ht_init:
Petr Machata38ebc0f2017-09-02 23:49:17 +02007093 mlxsw_sp_ipips_fini(mlxsw_sp);
7094err_ipips_init:
Ido Schimmel348b8fc2017-05-16 19:38:29 +02007095 mlxsw_sp_rifs_fini(mlxsw_sp);
7096err_rifs_init:
Jiri Pirkob45f64d2016-09-26 12:52:31 +02007097 __mlxsw_sp_router_fini(mlxsw_sp);
Ido Schimmel9011b672017-05-16 19:38:25 +02007098err_router_init:
7099 kfree(mlxsw_sp->router);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02007100 return err;
7101}
7102
7103void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
7104{
Ido Schimmel7e39d112017-05-16 19:38:28 +02007105 unregister_fib_notifier(&mlxsw_sp->router->fib_nb);
Ido Schimmel48fac882017-11-02 17:14:06 +01007106 unregister_netevent_notifier(&mlxsw_sp->router->netevent_nb);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02007107 mlxsw_sp_neigh_fini(mlxsw_sp);
7108 mlxsw_sp_vrs_fini(mlxsw_sp);
Yotam Gigid42b0962017-09-27 08:23:20 +02007109 mlxsw_sp_mr_fini(mlxsw_sp);
Ido Schimmel8494ab02017-03-24 08:02:47 +01007110 mlxsw_sp_lpm_fini(mlxsw_sp);
Ido Schimmel9011b672017-05-16 19:38:25 +02007111 rhashtable_destroy(&mlxsw_sp->router->nexthop_group_ht);
7112 rhashtable_destroy(&mlxsw_sp->router->nexthop_ht);
Petr Machata38ebc0f2017-09-02 23:49:17 +02007113 mlxsw_sp_ipips_fini(mlxsw_sp);
Ido Schimmel348b8fc2017-05-16 19:38:29 +02007114 mlxsw_sp_rifs_fini(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02007115 __mlxsw_sp_router_fini(mlxsw_sp);
Ido Schimmel9011b672017-05-16 19:38:25 +02007116 kfree(mlxsw_sp->router);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02007117}