blob: 0744452a0b188190a1da2fe0e14651aad31748de [file] [log] [blame]
Ido Schimmel464dce12016-07-02 11:00:15 +02001/*
2 * drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c
3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
4 * 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>
Ido Schimmel464dce12016-07-02 11:00:15 +02007 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include <linux/kernel.h>
38#include <linux/types.h>
Jiri Pirko5e9c16c2016-07-04 08:23:04 +020039#include <linux/rhashtable.h>
40#include <linux/bitops.h>
41#include <linux/in6.h>
Yotam Gigic723c7352016-07-05 11:27:43 +020042#include <linux/notifier.h>
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +010043#include <linux/inetdevice.h>
Ido Schimmel9db032b2017-03-16 09:08:17 +010044#include <linux/netdevice.h>
Yotam Gigic723c7352016-07-05 11:27:43 +020045#include <net/netevent.h>
Jiri Pirko6cf3c972016-07-05 11:27:39 +020046#include <net/neighbour.h>
47#include <net/arp.h>
Jiri Pirkob45f64d2016-09-26 12:52:31 +020048#include <net/ip_fib.h>
Ido Schimmel5d7bfd12017-03-16 09:08:14 +010049#include <net/fib_rules.h>
Ido Schimmel57837882017-03-16 09:08:16 +010050#include <net/l3mdev.h>
Ido Schimmel464dce12016-07-02 11:00:15 +020051
52#include "spectrum.h"
53#include "core.h"
54#include "reg.h"
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +020055#include "spectrum_cnt.h"
56#include "spectrum_dpipe.h"
57#include "spectrum_router.h"
Ido Schimmel464dce12016-07-02 11:00:15 +020058
Ido Schimmel4724ba562017-03-10 08:53:39 +010059struct mlxsw_sp_rif {
60 struct list_head nexthop_list;
61 struct list_head neigh_list;
62 struct net_device *dev;
63 struct mlxsw_sp_fid *f;
64 unsigned char addr[ETH_ALEN];
65 int mtu;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +010066 u16 rif_index;
Ido Schimmel69132292017-03-10 08:53:42 +010067 u16 vr_id;
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +020068 unsigned int counter_ingress;
69 bool counter_ingress_valid;
70 unsigned int counter_egress;
71 bool counter_egress_valid;
Ido Schimmel4724ba562017-03-10 08:53:39 +010072};
73
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +020074static unsigned int *
75mlxsw_sp_rif_p_counter_get(struct mlxsw_sp_rif *rif,
76 enum mlxsw_sp_rif_counter_dir dir)
77{
78 switch (dir) {
79 case MLXSW_SP_RIF_COUNTER_EGRESS:
80 return &rif->counter_egress;
81 case MLXSW_SP_RIF_COUNTER_INGRESS:
82 return &rif->counter_ingress;
83 }
84 return NULL;
85}
86
87static bool
88mlxsw_sp_rif_counter_valid_get(struct mlxsw_sp_rif *rif,
89 enum mlxsw_sp_rif_counter_dir dir)
90{
91 switch (dir) {
92 case MLXSW_SP_RIF_COUNTER_EGRESS:
93 return rif->counter_egress_valid;
94 case MLXSW_SP_RIF_COUNTER_INGRESS:
95 return rif->counter_ingress_valid;
96 }
97 return false;
98}
99
100static void
101mlxsw_sp_rif_counter_valid_set(struct mlxsw_sp_rif *rif,
102 enum mlxsw_sp_rif_counter_dir dir,
103 bool valid)
104{
105 switch (dir) {
106 case MLXSW_SP_RIF_COUNTER_EGRESS:
107 rif->counter_egress_valid = valid;
108 break;
109 case MLXSW_SP_RIF_COUNTER_INGRESS:
110 rif->counter_ingress_valid = valid;
111 break;
112 }
113}
114
115static int mlxsw_sp_rif_counter_edit(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
116 unsigned int counter_index, bool enable,
117 enum mlxsw_sp_rif_counter_dir dir)
118{
119 char ritr_pl[MLXSW_REG_RITR_LEN];
120 bool is_egress = false;
121 int err;
122
123 if (dir == MLXSW_SP_RIF_COUNTER_EGRESS)
124 is_egress = true;
125 mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
126 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
127 if (err)
128 return err;
129
130 mlxsw_reg_ritr_counter_pack(ritr_pl, counter_index, enable,
131 is_egress);
132 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
133}
134
135int mlxsw_sp_rif_counter_value_get(struct mlxsw_sp *mlxsw_sp,
136 struct mlxsw_sp_rif *rif,
137 enum mlxsw_sp_rif_counter_dir dir, u64 *cnt)
138{
139 char ricnt_pl[MLXSW_REG_RICNT_LEN];
140 unsigned int *p_counter_index;
141 bool valid;
142 int err;
143
144 valid = mlxsw_sp_rif_counter_valid_get(rif, dir);
145 if (!valid)
146 return -EINVAL;
147
148 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
149 if (!p_counter_index)
150 return -EINVAL;
151 mlxsw_reg_ricnt_pack(ricnt_pl, *p_counter_index,
152 MLXSW_REG_RICNT_OPCODE_NOP);
153 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ricnt), ricnt_pl);
154 if (err)
155 return err;
156 *cnt = mlxsw_reg_ricnt_good_unicast_packets_get(ricnt_pl);
157 return 0;
158}
159
160static int mlxsw_sp_rif_counter_clear(struct mlxsw_sp *mlxsw_sp,
161 unsigned int counter_index)
162{
163 char ricnt_pl[MLXSW_REG_RICNT_LEN];
164
165 mlxsw_reg_ricnt_pack(ricnt_pl, counter_index,
166 MLXSW_REG_RICNT_OPCODE_CLEAR);
167 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ricnt), ricnt_pl);
168}
169
170int mlxsw_sp_rif_counter_alloc(struct mlxsw_sp *mlxsw_sp,
171 struct mlxsw_sp_rif *rif,
172 enum mlxsw_sp_rif_counter_dir dir)
173{
174 unsigned int *p_counter_index;
175 int err;
176
177 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
178 if (!p_counter_index)
179 return -EINVAL;
180 err = mlxsw_sp_counter_alloc(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
181 p_counter_index);
182 if (err)
183 return err;
184
185 err = mlxsw_sp_rif_counter_clear(mlxsw_sp, *p_counter_index);
186 if (err)
187 goto err_counter_clear;
188
189 err = mlxsw_sp_rif_counter_edit(mlxsw_sp, rif->rif_index,
190 *p_counter_index, true, dir);
191 if (err)
192 goto err_counter_edit;
193 mlxsw_sp_rif_counter_valid_set(rif, dir, true);
194 return 0;
195
196err_counter_edit:
197err_counter_clear:
198 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
199 *p_counter_index);
200 return err;
201}
202
203void mlxsw_sp_rif_counter_free(struct mlxsw_sp *mlxsw_sp,
204 struct mlxsw_sp_rif *rif,
205 enum mlxsw_sp_rif_counter_dir dir)
206{
207 unsigned int *p_counter_index;
208
Arkadi Sharshevsky6b1206b2017-05-18 09:18:53 +0200209 if (!mlxsw_sp_rif_counter_valid_get(rif, dir))
210 return;
211
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +0200212 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
213 if (WARN_ON(!p_counter_index))
214 return;
215 mlxsw_sp_rif_counter_edit(mlxsw_sp, rif->rif_index,
216 *p_counter_index, false, dir);
217 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
218 *p_counter_index);
219 mlxsw_sp_rif_counter_valid_set(rif, dir, false);
220}
221
Ido Schimmel4724ba562017-03-10 08:53:39 +0100222static struct mlxsw_sp_rif *
223mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
224 const struct net_device *dev);
225
Jiri Pirko53342022016-07-04 08:23:08 +0200226#define mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) \
227 for_each_set_bit(prefix, (prefix_usage)->b, MLXSW_SP_PREFIX_COUNT)
228
229static bool
Jiri Pirko6b75c482016-07-04 08:23:09 +0200230mlxsw_sp_prefix_usage_subset(struct mlxsw_sp_prefix_usage *prefix_usage1,
231 struct mlxsw_sp_prefix_usage *prefix_usage2)
232{
233 unsigned char prefix;
234
235 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage1) {
236 if (!test_bit(prefix, prefix_usage2->b))
237 return false;
238 }
239 return true;
240}
241
242static bool
Jiri Pirko53342022016-07-04 08:23:08 +0200243mlxsw_sp_prefix_usage_eq(struct mlxsw_sp_prefix_usage *prefix_usage1,
244 struct mlxsw_sp_prefix_usage *prefix_usage2)
245{
246 return !memcmp(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
247}
248
Jiri Pirko6b75c482016-07-04 08:23:09 +0200249static bool
250mlxsw_sp_prefix_usage_none(struct mlxsw_sp_prefix_usage *prefix_usage)
251{
252 struct mlxsw_sp_prefix_usage prefix_usage_none = {{ 0 } };
253
254 return mlxsw_sp_prefix_usage_eq(prefix_usage, &prefix_usage_none);
255}
256
257static void
258mlxsw_sp_prefix_usage_cpy(struct mlxsw_sp_prefix_usage *prefix_usage1,
259 struct mlxsw_sp_prefix_usage *prefix_usage2)
260{
261 memcpy(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
262}
263
264static void
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200265mlxsw_sp_prefix_usage_set(struct mlxsw_sp_prefix_usage *prefix_usage,
266 unsigned char prefix_len)
267{
268 set_bit(prefix_len, prefix_usage->b);
269}
270
271static void
272mlxsw_sp_prefix_usage_clear(struct mlxsw_sp_prefix_usage *prefix_usage,
273 unsigned char prefix_len)
274{
275 clear_bit(prefix_len, prefix_usage->b);
276}
277
278struct mlxsw_sp_fib_key {
279 unsigned char addr[sizeof(struct in6_addr)];
280 unsigned char prefix_len;
281};
282
Jiri Pirko61c503f2016-07-04 08:23:11 +0200283enum mlxsw_sp_fib_entry_type {
284 MLXSW_SP_FIB_ENTRY_TYPE_REMOTE,
285 MLXSW_SP_FIB_ENTRY_TYPE_LOCAL,
286 MLXSW_SP_FIB_ENTRY_TYPE_TRAP,
287};
288
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200289struct mlxsw_sp_nexthop_group;
290
Ido Schimmel9aecce12017-02-09 10:28:42 +0100291struct mlxsw_sp_fib_node {
292 struct list_head entry_list;
Jiri Pirkob45f64d2016-09-26 12:52:31 +0200293 struct list_head list;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100294 struct rhash_head ht_node;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100295 struct mlxsw_sp_fib *fib;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100296 struct mlxsw_sp_fib_key key;
297};
298
299struct mlxsw_sp_fib_entry_params {
300 u32 tb_id;
301 u32 prio;
302 u8 tos;
303 u8 type;
304};
305
306struct mlxsw_sp_fib_entry {
307 struct list_head list;
308 struct mlxsw_sp_fib_node *fib_node;
309 enum mlxsw_sp_fib_entry_type type;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200310 struct list_head nexthop_group_node;
311 struct mlxsw_sp_nexthop_group *nh_group;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100312 struct mlxsw_sp_fib_entry_params params;
Ido Schimmel013b20f2017-02-08 11:16:36 +0100313 bool offloaded;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200314};
315
316struct mlxsw_sp_fib {
317 struct rhashtable ht;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100318 struct list_head node_list;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100319 struct mlxsw_sp_vr *vr;
320 struct mlxsw_sp_lpm_tree *lpm_tree;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200321 unsigned long prefix_ref_count[MLXSW_SP_PREFIX_COUNT];
322 struct mlxsw_sp_prefix_usage prefix_usage;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100323 enum mlxsw_sp_l3proto proto;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200324};
325
Ido Schimmel9aecce12017-02-09 10:28:42 +0100326static const struct rhashtable_params mlxsw_sp_fib_ht_params;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200327
Ido Schimmel76610eb2017-03-10 08:53:41 +0100328static struct mlxsw_sp_fib *mlxsw_sp_fib_create(struct mlxsw_sp_vr *vr,
329 enum mlxsw_sp_l3proto proto)
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200330{
331 struct mlxsw_sp_fib *fib;
332 int err;
333
334 fib = kzalloc(sizeof(*fib), GFP_KERNEL);
335 if (!fib)
336 return ERR_PTR(-ENOMEM);
337 err = rhashtable_init(&fib->ht, &mlxsw_sp_fib_ht_params);
338 if (err)
339 goto err_rhashtable_init;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100340 INIT_LIST_HEAD(&fib->node_list);
Ido Schimmel76610eb2017-03-10 08:53:41 +0100341 fib->proto = proto;
342 fib->vr = vr;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200343 return fib;
344
345err_rhashtable_init:
346 kfree(fib);
347 return ERR_PTR(err);
348}
349
350static void mlxsw_sp_fib_destroy(struct mlxsw_sp_fib *fib)
351{
Ido Schimmel9aecce12017-02-09 10:28:42 +0100352 WARN_ON(!list_empty(&fib->node_list));
Ido Schimmel76610eb2017-03-10 08:53:41 +0100353 WARN_ON(fib->lpm_tree);
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200354 rhashtable_destroy(&fib->ht);
355 kfree(fib);
356}
357
Jiri Pirko53342022016-07-04 08:23:08 +0200358static struct mlxsw_sp_lpm_tree *
Ido Schimmel382dbb42017-03-10 08:53:40 +0100359mlxsw_sp_lpm_tree_find_unused(struct mlxsw_sp *mlxsw_sp)
Jiri Pirko53342022016-07-04 08:23:08 +0200360{
361 static struct mlxsw_sp_lpm_tree *lpm_tree;
362 int i;
363
Ido Schimmel8494ab02017-03-24 08:02:47 +0100364 for (i = 0; i < mlxsw_sp->router.lpm.tree_count; i++) {
365 lpm_tree = &mlxsw_sp->router.lpm.trees[i];
Ido Schimmel382dbb42017-03-10 08:53:40 +0100366 if (lpm_tree->ref_count == 0)
367 return lpm_tree;
Jiri Pirko53342022016-07-04 08:23:08 +0200368 }
369 return NULL;
370}
371
372static int mlxsw_sp_lpm_tree_alloc(struct mlxsw_sp *mlxsw_sp,
373 struct mlxsw_sp_lpm_tree *lpm_tree)
374{
375 char ralta_pl[MLXSW_REG_RALTA_LEN];
376
Ido Schimmel1a9234e662016-09-19 08:29:26 +0200377 mlxsw_reg_ralta_pack(ralta_pl, true,
378 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
379 lpm_tree->id);
Jiri Pirko53342022016-07-04 08:23:08 +0200380 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
381}
382
383static int mlxsw_sp_lpm_tree_free(struct mlxsw_sp *mlxsw_sp,
384 struct mlxsw_sp_lpm_tree *lpm_tree)
385{
386 char ralta_pl[MLXSW_REG_RALTA_LEN];
387
Ido Schimmel1a9234e662016-09-19 08:29:26 +0200388 mlxsw_reg_ralta_pack(ralta_pl, false,
389 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
390 lpm_tree->id);
Jiri Pirko53342022016-07-04 08:23:08 +0200391 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
392}
393
394static int
395mlxsw_sp_lpm_tree_left_struct_set(struct mlxsw_sp *mlxsw_sp,
396 struct mlxsw_sp_prefix_usage *prefix_usage,
397 struct mlxsw_sp_lpm_tree *lpm_tree)
398{
399 char ralst_pl[MLXSW_REG_RALST_LEN];
400 u8 root_bin = 0;
401 u8 prefix;
402 u8 last_prefix = MLXSW_REG_RALST_BIN_NO_CHILD;
403
404 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage)
405 root_bin = prefix;
406
407 mlxsw_reg_ralst_pack(ralst_pl, root_bin, lpm_tree->id);
408 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) {
409 if (prefix == 0)
410 continue;
411 mlxsw_reg_ralst_bin_pack(ralst_pl, prefix, last_prefix,
412 MLXSW_REG_RALST_BIN_NO_CHILD);
413 last_prefix = prefix;
414 }
415 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
416}
417
418static struct mlxsw_sp_lpm_tree *
419mlxsw_sp_lpm_tree_create(struct mlxsw_sp *mlxsw_sp,
420 struct mlxsw_sp_prefix_usage *prefix_usage,
Ido Schimmel382dbb42017-03-10 08:53:40 +0100421 enum mlxsw_sp_l3proto proto)
Jiri Pirko53342022016-07-04 08:23:08 +0200422{
423 struct mlxsw_sp_lpm_tree *lpm_tree;
424 int err;
425
Ido Schimmel382dbb42017-03-10 08:53:40 +0100426 lpm_tree = mlxsw_sp_lpm_tree_find_unused(mlxsw_sp);
Jiri Pirko53342022016-07-04 08:23:08 +0200427 if (!lpm_tree)
428 return ERR_PTR(-EBUSY);
429 lpm_tree->proto = proto;
430 err = mlxsw_sp_lpm_tree_alloc(mlxsw_sp, lpm_tree);
431 if (err)
432 return ERR_PTR(err);
433
434 err = mlxsw_sp_lpm_tree_left_struct_set(mlxsw_sp, prefix_usage,
435 lpm_tree);
436 if (err)
437 goto err_left_struct_set;
Jiri Pirko2083d362016-10-25 11:25:56 +0200438 memcpy(&lpm_tree->prefix_usage, prefix_usage,
439 sizeof(lpm_tree->prefix_usage));
Jiri Pirko53342022016-07-04 08:23:08 +0200440 return lpm_tree;
441
442err_left_struct_set:
443 mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
444 return ERR_PTR(err);
445}
446
447static int mlxsw_sp_lpm_tree_destroy(struct mlxsw_sp *mlxsw_sp,
448 struct mlxsw_sp_lpm_tree *lpm_tree)
449{
450 return mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
451}
452
453static struct mlxsw_sp_lpm_tree *
454mlxsw_sp_lpm_tree_get(struct mlxsw_sp *mlxsw_sp,
455 struct mlxsw_sp_prefix_usage *prefix_usage,
Ido Schimmel382dbb42017-03-10 08:53:40 +0100456 enum mlxsw_sp_l3proto proto)
Jiri Pirko53342022016-07-04 08:23:08 +0200457{
458 struct mlxsw_sp_lpm_tree *lpm_tree;
459 int i;
460
Ido Schimmel8494ab02017-03-24 08:02:47 +0100461 for (i = 0; i < mlxsw_sp->router.lpm.tree_count; i++) {
462 lpm_tree = &mlxsw_sp->router.lpm.trees[i];
Jiri Pirko8b99bec2016-10-25 11:25:57 +0200463 if (lpm_tree->ref_count != 0 &&
464 lpm_tree->proto == proto &&
Jiri Pirko53342022016-07-04 08:23:08 +0200465 mlxsw_sp_prefix_usage_eq(&lpm_tree->prefix_usage,
466 prefix_usage))
467 goto inc_ref_count;
468 }
469 lpm_tree = mlxsw_sp_lpm_tree_create(mlxsw_sp, prefix_usage,
Ido Schimmel382dbb42017-03-10 08:53:40 +0100470 proto);
Jiri Pirko53342022016-07-04 08:23:08 +0200471 if (IS_ERR(lpm_tree))
472 return lpm_tree;
473
474inc_ref_count:
475 lpm_tree->ref_count++;
476 return lpm_tree;
477}
478
479static int mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp,
480 struct mlxsw_sp_lpm_tree *lpm_tree)
481{
482 if (--lpm_tree->ref_count == 0)
483 return mlxsw_sp_lpm_tree_destroy(mlxsw_sp, lpm_tree);
484 return 0;
485}
486
Ido Schimmel8494ab02017-03-24 08:02:47 +0100487#define MLXSW_SP_LPM_TREE_MIN 2 /* trees 0 and 1 are reserved */
488
489static int mlxsw_sp_lpm_init(struct mlxsw_sp *mlxsw_sp)
Jiri Pirko53342022016-07-04 08:23:08 +0200490{
491 struct mlxsw_sp_lpm_tree *lpm_tree;
Ido Schimmel8494ab02017-03-24 08:02:47 +0100492 u64 max_trees;
Jiri Pirko53342022016-07-04 08:23:08 +0200493 int i;
494
Ido Schimmel8494ab02017-03-24 08:02:47 +0100495 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LPM_TREES))
496 return -EIO;
497
498 max_trees = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LPM_TREES);
499 mlxsw_sp->router.lpm.tree_count = max_trees - MLXSW_SP_LPM_TREE_MIN;
500 mlxsw_sp->router.lpm.trees = kcalloc(mlxsw_sp->router.lpm.tree_count,
501 sizeof(struct mlxsw_sp_lpm_tree),
502 GFP_KERNEL);
503 if (!mlxsw_sp->router.lpm.trees)
504 return -ENOMEM;
505
506 for (i = 0; i < mlxsw_sp->router.lpm.tree_count; i++) {
507 lpm_tree = &mlxsw_sp->router.lpm.trees[i];
Jiri Pirko53342022016-07-04 08:23:08 +0200508 lpm_tree->id = i + MLXSW_SP_LPM_TREE_MIN;
509 }
Ido Schimmel8494ab02017-03-24 08:02:47 +0100510
511 return 0;
512}
513
514static void mlxsw_sp_lpm_fini(struct mlxsw_sp *mlxsw_sp)
515{
516 kfree(mlxsw_sp->router.lpm.trees);
Jiri Pirko53342022016-07-04 08:23:08 +0200517}
518
Ido Schimmel76610eb2017-03-10 08:53:41 +0100519static bool mlxsw_sp_vr_is_used(const struct mlxsw_sp_vr *vr)
520{
521 return !!vr->fib4;
522}
523
Jiri Pirko6b75c482016-07-04 08:23:09 +0200524static struct mlxsw_sp_vr *mlxsw_sp_vr_find_unused(struct mlxsw_sp *mlxsw_sp)
525{
526 struct mlxsw_sp_vr *vr;
527 int i;
528
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200529 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Jiri Pirko6b75c482016-07-04 08:23:09 +0200530 vr = &mlxsw_sp->router.vrs[i];
Ido Schimmel76610eb2017-03-10 08:53:41 +0100531 if (!mlxsw_sp_vr_is_used(vr))
Jiri Pirko6b75c482016-07-04 08:23:09 +0200532 return vr;
533 }
534 return NULL;
535}
536
537static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel76610eb2017-03-10 08:53:41 +0100538 const struct mlxsw_sp_fib *fib)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200539{
540 char raltb_pl[MLXSW_REG_RALTB_LEN];
541
Ido Schimmel76610eb2017-03-10 08:53:41 +0100542 mlxsw_reg_raltb_pack(raltb_pl, fib->vr->id,
543 (enum mlxsw_reg_ralxx_protocol) fib->proto,
544 fib->lpm_tree->id);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200545 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
546}
547
548static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel76610eb2017-03-10 08:53:41 +0100549 const struct mlxsw_sp_fib *fib)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200550{
551 char raltb_pl[MLXSW_REG_RALTB_LEN];
552
553 /* Bind to tree 0 which is default */
Ido Schimmel76610eb2017-03-10 08:53:41 +0100554 mlxsw_reg_raltb_pack(raltb_pl, fib->vr->id,
555 (enum mlxsw_reg_ralxx_protocol) fib->proto, 0);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200556 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
557}
558
559static u32 mlxsw_sp_fix_tb_id(u32 tb_id)
560{
561 /* For our purpose, squash main and local table into one */
562 if (tb_id == RT_TABLE_LOCAL)
563 tb_id = RT_TABLE_MAIN;
564 return tb_id;
565}
566
567static struct mlxsw_sp_vr *mlxsw_sp_vr_find(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel76610eb2017-03-10 08:53:41 +0100568 u32 tb_id)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200569{
570 struct mlxsw_sp_vr *vr;
571 int i;
572
573 tb_id = mlxsw_sp_fix_tb_id(tb_id);
Nogah Frankel9497c042016-09-20 11:16:54 +0200574
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200575 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Jiri Pirko6b75c482016-07-04 08:23:09 +0200576 vr = &mlxsw_sp->router.vrs[i];
Ido Schimmel76610eb2017-03-10 08:53:41 +0100577 if (mlxsw_sp_vr_is_used(vr) && vr->tb_id == tb_id)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200578 return vr;
579 }
580 return NULL;
581}
582
Ido Schimmel76610eb2017-03-10 08:53:41 +0100583static struct mlxsw_sp_fib *mlxsw_sp_vr_fib(const struct mlxsw_sp_vr *vr,
584 enum mlxsw_sp_l3proto proto)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200585{
Ido Schimmel76610eb2017-03-10 08:53:41 +0100586 switch (proto) {
587 case MLXSW_SP_L3_PROTO_IPV4:
588 return vr->fib4;
589 case MLXSW_SP_L3_PROTO_IPV6:
590 BUG_ON(1);
591 }
592 return NULL;
593}
594
595static struct mlxsw_sp_vr *mlxsw_sp_vr_create(struct mlxsw_sp *mlxsw_sp,
596 u32 tb_id)
597{
Jiri Pirko6b75c482016-07-04 08:23:09 +0200598 struct mlxsw_sp_vr *vr;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200599
600 vr = mlxsw_sp_vr_find_unused(mlxsw_sp);
601 if (!vr)
602 return ERR_PTR(-EBUSY);
Ido Schimmel76610eb2017-03-10 08:53:41 +0100603 vr->fib4 = mlxsw_sp_fib_create(vr, MLXSW_SP_L3_PROTO_IPV4);
604 if (IS_ERR(vr->fib4))
605 return ERR_CAST(vr->fib4);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200606 vr->tb_id = tb_id;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200607 return vr;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200608}
609
Ido Schimmel76610eb2017-03-10 08:53:41 +0100610static void mlxsw_sp_vr_destroy(struct mlxsw_sp_vr *vr)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200611{
Ido Schimmel76610eb2017-03-10 08:53:41 +0100612 mlxsw_sp_fib_destroy(vr->fib4);
613 vr->fib4 = NULL;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200614}
615
616static int
Ido Schimmel76610eb2017-03-10 08:53:41 +0100617mlxsw_sp_vr_lpm_tree_check(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_fib *fib,
Jiri Pirko6b75c482016-07-04 08:23:09 +0200618 struct mlxsw_sp_prefix_usage *req_prefix_usage)
619{
Ido Schimmel76610eb2017-03-10 08:53:41 +0100620 struct mlxsw_sp_lpm_tree *lpm_tree = fib->lpm_tree;
Ido Schimmelf7df4922017-02-28 08:55:40 +0100621 struct mlxsw_sp_lpm_tree *new_tree;
622 int err;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200623
Ido Schimmelf7df4922017-02-28 08:55:40 +0100624 if (mlxsw_sp_prefix_usage_eq(req_prefix_usage, &lpm_tree->prefix_usage))
Jiri Pirko6b75c482016-07-04 08:23:09 +0200625 return 0;
626
Ido Schimmelf7df4922017-02-28 08:55:40 +0100627 new_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, req_prefix_usage,
Ido Schimmel76610eb2017-03-10 08:53:41 +0100628 fib->proto);
Ido Schimmelf7df4922017-02-28 08:55:40 +0100629 if (IS_ERR(new_tree)) {
Jiri Pirko6b75c482016-07-04 08:23:09 +0200630 /* We failed to get a tree according to the required
631 * prefix usage. However, the current tree might be still good
632 * for us if our requirement is subset of the prefixes used
633 * in the tree.
634 */
635 if (mlxsw_sp_prefix_usage_subset(req_prefix_usage,
Ido Schimmelf7df4922017-02-28 08:55:40 +0100636 &lpm_tree->prefix_usage))
Jiri Pirko6b75c482016-07-04 08:23:09 +0200637 return 0;
Ido Schimmelf7df4922017-02-28 08:55:40 +0100638 return PTR_ERR(new_tree);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200639 }
640
Ido Schimmelf7df4922017-02-28 08:55:40 +0100641 /* Prevent packet loss by overwriting existing binding */
Ido Schimmel76610eb2017-03-10 08:53:41 +0100642 fib->lpm_tree = new_tree;
643 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, fib);
Ido Schimmelf7df4922017-02-28 08:55:40 +0100644 if (err)
645 goto err_tree_bind;
646 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
647
648 return 0;
649
650err_tree_bind:
Ido Schimmel76610eb2017-03-10 08:53:41 +0100651 fib->lpm_tree = lpm_tree;
Ido Schimmelf7df4922017-02-28 08:55:40 +0100652 mlxsw_sp_lpm_tree_put(mlxsw_sp, new_tree);
653 return err;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200654}
655
Ido Schimmel76610eb2017-03-10 08:53:41 +0100656static struct mlxsw_sp_vr *mlxsw_sp_vr_get(struct mlxsw_sp *mlxsw_sp, u32 tb_id)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200657{
658 struct mlxsw_sp_vr *vr;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200659
660 tb_id = mlxsw_sp_fix_tb_id(tb_id);
Ido Schimmel76610eb2017-03-10 08:53:41 +0100661 vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id);
662 if (!vr)
663 vr = mlxsw_sp_vr_create(mlxsw_sp, tb_id);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200664 return vr;
665}
666
Ido Schimmel76610eb2017-03-10 08:53:41 +0100667static void mlxsw_sp_vr_put(struct mlxsw_sp_vr *vr)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200668{
Ido Schimmel69132292017-03-10 08:53:42 +0100669 if (!vr->rif_count && list_empty(&vr->fib4->node_list))
Ido Schimmel76610eb2017-03-10 08:53:41 +0100670 mlxsw_sp_vr_destroy(vr);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200671}
672
Nogah Frankel9497c042016-09-20 11:16:54 +0200673static int mlxsw_sp_vrs_init(struct mlxsw_sp *mlxsw_sp)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200674{
675 struct mlxsw_sp_vr *vr;
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200676 u64 max_vrs;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200677 int i;
678
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200679 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_VRS))
Nogah Frankel9497c042016-09-20 11:16:54 +0200680 return -EIO;
681
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200682 max_vrs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS);
683 mlxsw_sp->router.vrs = kcalloc(max_vrs, sizeof(struct mlxsw_sp_vr),
Nogah Frankel9497c042016-09-20 11:16:54 +0200684 GFP_KERNEL);
685 if (!mlxsw_sp->router.vrs)
686 return -ENOMEM;
687
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200688 for (i = 0; i < max_vrs; i++) {
Jiri Pirko6b75c482016-07-04 08:23:09 +0200689 vr = &mlxsw_sp->router.vrs[i];
690 vr->id = i;
691 }
Nogah Frankel9497c042016-09-20 11:16:54 +0200692
693 return 0;
694}
695
Ido Schimmelac571de2016-11-14 11:26:32 +0100696static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp);
697
Nogah Frankel9497c042016-09-20 11:16:54 +0200698static void mlxsw_sp_vrs_fini(struct mlxsw_sp *mlxsw_sp)
699{
Ido Schimmel30572242016-12-03 16:45:01 +0100700 /* At this stage we're guaranteed not to have new incoming
701 * FIB notifications and the work queue is free from FIBs
702 * sitting on top of mlxsw netdevs. However, we can still
703 * have other FIBs queued. Flush the queue before flushing
704 * the device's tables. No need for locks, as we're the only
705 * writer.
706 */
707 mlxsw_core_flush_owq();
Ido Schimmelac571de2016-11-14 11:26:32 +0100708 mlxsw_sp_router_fib_flush(mlxsw_sp);
Nogah Frankel9497c042016-09-20 11:16:54 +0200709 kfree(mlxsw_sp->router.vrs);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200710}
711
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200712struct mlxsw_sp_neigh_key {
Jiri Pirko33b13412016-11-10 12:31:04 +0100713 struct neighbour *n;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200714};
715
716struct mlxsw_sp_neigh_entry {
Ido Schimmel9665b742017-02-08 11:16:42 +0100717 struct list_head rif_list_node;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200718 struct rhash_head ht_node;
719 struct mlxsw_sp_neigh_key key;
720 u16 rif;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100721 bool connected;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200722 unsigned char ha[ETH_ALEN];
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200723 struct list_head nexthop_list; /* list of nexthops using
724 * this neigh entry
725 */
Yotam Gigib2157142016-07-05 11:27:51 +0200726 struct list_head nexthop_neighs_list_node;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200727};
728
729static const struct rhashtable_params mlxsw_sp_neigh_ht_params = {
730 .key_offset = offsetof(struct mlxsw_sp_neigh_entry, key),
731 .head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node),
732 .key_len = sizeof(struct mlxsw_sp_neigh_key),
733};
734
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100735static struct mlxsw_sp_neigh_entry *
736mlxsw_sp_neigh_entry_alloc(struct mlxsw_sp *mlxsw_sp, struct neighbour *n,
737 u16 rif)
738{
739 struct mlxsw_sp_neigh_entry *neigh_entry;
740
741 neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_KERNEL);
742 if (!neigh_entry)
743 return NULL;
744
745 neigh_entry->key.n = n;
746 neigh_entry->rif = rif;
747 INIT_LIST_HEAD(&neigh_entry->nexthop_list);
748
749 return neigh_entry;
750}
751
752static void mlxsw_sp_neigh_entry_free(struct mlxsw_sp_neigh_entry *neigh_entry)
753{
754 kfree(neigh_entry);
755}
756
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200757static int
758mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp,
759 struct mlxsw_sp_neigh_entry *neigh_entry)
760{
761 return rhashtable_insert_fast(&mlxsw_sp->router.neigh_ht,
762 &neigh_entry->ht_node,
763 mlxsw_sp_neigh_ht_params);
764}
765
766static void
767mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp,
768 struct mlxsw_sp_neigh_entry *neigh_entry)
769{
770 rhashtable_remove_fast(&mlxsw_sp->router.neigh_ht,
771 &neigh_entry->ht_node,
772 mlxsw_sp_neigh_ht_params);
773}
774
775static struct mlxsw_sp_neigh_entry *
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100776mlxsw_sp_neigh_entry_create(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200777{
778 struct mlxsw_sp_neigh_entry *neigh_entry;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +0100779 struct mlxsw_sp_rif *rif;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100780 int err;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200781
Arkadi Sharshevskybf952332017-03-17 09:38:00 +0100782 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, n->dev);
783 if (!rif)
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100784 return ERR_PTR(-EINVAL);
785
Arkadi Sharshevskybf952332017-03-17 09:38:00 +0100786 neigh_entry = mlxsw_sp_neigh_entry_alloc(mlxsw_sp, n, rif->rif_index);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200787 if (!neigh_entry)
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100788 return ERR_PTR(-ENOMEM);
789
790 err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
791 if (err)
792 goto err_neigh_entry_insert;
793
Arkadi Sharshevskybf952332017-03-17 09:38:00 +0100794 list_add(&neigh_entry->rif_list_node, &rif->neigh_list);
Ido Schimmel9665b742017-02-08 11:16:42 +0100795
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200796 return neigh_entry;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100797
798err_neigh_entry_insert:
799 mlxsw_sp_neigh_entry_free(neigh_entry);
800 return ERR_PTR(err);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200801}
802
803static void
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100804mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp *mlxsw_sp,
805 struct mlxsw_sp_neigh_entry *neigh_entry)
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200806{
Ido Schimmel9665b742017-02-08 11:16:42 +0100807 list_del(&neigh_entry->rif_list_node);
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100808 mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
809 mlxsw_sp_neigh_entry_free(neigh_entry);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200810}
811
812static struct mlxsw_sp_neigh_entry *
Jiri Pirko33b13412016-11-10 12:31:04 +0100813mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200814{
Jiri Pirko33b13412016-11-10 12:31:04 +0100815 struct mlxsw_sp_neigh_key key;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200816
Jiri Pirko33b13412016-11-10 12:31:04 +0100817 key.n = n;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200818 return rhashtable_lookup_fast(&mlxsw_sp->router.neigh_ht,
819 &key, mlxsw_sp_neigh_ht_params);
820}
821
Yotam Gigic723c7352016-07-05 11:27:43 +0200822static void
823mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp)
824{
825 unsigned long interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME);
826
827 mlxsw_sp->router.neighs_update.interval = jiffies_to_msecs(interval);
828}
829
830static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
831 char *rauhtd_pl,
832 int ent_index)
833{
834 struct net_device *dev;
835 struct neighbour *n;
836 __be32 dipn;
837 u32 dip;
838 u16 rif;
839
840 mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip);
841
842 if (!mlxsw_sp->rifs[rif]) {
843 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
844 return;
845 }
846
847 dipn = htonl(dip);
848 dev = mlxsw_sp->rifs[rif]->dev;
849 n = neigh_lookup(&arp_tbl, &dipn, dev);
850 if (!n) {
851 netdev_err(dev, "Failed to find matching neighbour for IP=%pI4h\n",
852 &dip);
853 return;
854 }
855
856 netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip);
857 neigh_event_send(n, NULL);
858 neigh_release(n);
859}
860
861static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp,
862 char *rauhtd_pl,
863 int rec_index)
864{
865 u8 num_entries;
866 int i;
867
868 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
869 rec_index);
870 /* Hardware starts counting at 0, so add 1. */
871 num_entries++;
872
873 /* Each record consists of several neighbour entries. */
874 for (i = 0; i < num_entries; i++) {
875 int ent_index;
876
877 ent_index = rec_index * MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC + i;
878 mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp, rauhtd_pl,
879 ent_index);
880 }
881
882}
883
884static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp,
885 char *rauhtd_pl, int rec_index)
886{
887 switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, rec_index)) {
888 case MLXSW_REG_RAUHTD_TYPE_IPV4:
889 mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp, rauhtd_pl,
890 rec_index);
891 break;
892 case MLXSW_REG_RAUHTD_TYPE_IPV6:
893 WARN_ON_ONCE(1);
894 break;
895 }
896}
897
Arkadi Sharshevsky42cdb332016-11-11 16:34:26 +0100898static bool mlxsw_sp_router_rauhtd_is_full(char *rauhtd_pl)
899{
900 u8 num_rec, last_rec_index, num_entries;
901
902 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
903 last_rec_index = num_rec - 1;
904
905 if (num_rec < MLXSW_REG_RAUHTD_REC_MAX_NUM)
906 return false;
907 if (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, last_rec_index) ==
908 MLXSW_REG_RAUHTD_TYPE_IPV6)
909 return true;
910
911 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
912 last_rec_index);
913 if (++num_entries == MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC)
914 return true;
915 return false;
916}
917
Yotam Gigib2157142016-07-05 11:27:51 +0200918static int mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp)
Yotam Gigic723c7352016-07-05 11:27:43 +0200919{
Yotam Gigic723c7352016-07-05 11:27:43 +0200920 char *rauhtd_pl;
921 u8 num_rec;
922 int i, err;
923
924 rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL);
925 if (!rauhtd_pl)
Yotam Gigib2157142016-07-05 11:27:51 +0200926 return -ENOMEM;
Yotam Gigic723c7352016-07-05 11:27:43 +0200927
928 /* Make sure the neighbour's netdev isn't removed in the
929 * process.
930 */
931 rtnl_lock();
932 do {
933 mlxsw_reg_rauhtd_pack(rauhtd_pl, MLXSW_REG_RAUHTD_TYPE_IPV4);
934 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd),
935 rauhtd_pl);
936 if (err) {
937 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour talbe\n");
938 break;
939 }
940 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
941 for (i = 0; i < num_rec; i++)
942 mlxsw_sp_router_neigh_rec_process(mlxsw_sp, rauhtd_pl,
943 i);
Arkadi Sharshevsky42cdb332016-11-11 16:34:26 +0100944 } while (mlxsw_sp_router_rauhtd_is_full(rauhtd_pl));
Yotam Gigic723c7352016-07-05 11:27:43 +0200945 rtnl_unlock();
946
947 kfree(rauhtd_pl);
Yotam Gigib2157142016-07-05 11:27:51 +0200948 return err;
949}
950
951static void mlxsw_sp_router_neighs_update_nh(struct mlxsw_sp *mlxsw_sp)
952{
953 struct mlxsw_sp_neigh_entry *neigh_entry;
954
955 /* Take RTNL mutex here to prevent lists from changes */
956 rtnl_lock();
957 list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list,
Ido Schimmel8a0b7272017-02-06 16:20:15 +0100958 nexthop_neighs_list_node)
Yotam Gigib2157142016-07-05 11:27:51 +0200959 /* If this neigh have nexthops, make the kernel think this neigh
960 * is active regardless of the traffic.
961 */
Ido Schimmel8a0b7272017-02-06 16:20:15 +0100962 neigh_event_send(neigh_entry->key.n, NULL);
Yotam Gigib2157142016-07-05 11:27:51 +0200963 rtnl_unlock();
964}
965
966static void
967mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp)
968{
969 unsigned long interval = mlxsw_sp->router.neighs_update.interval;
970
971 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw,
972 msecs_to_jiffies(interval));
973}
974
975static void mlxsw_sp_router_neighs_update_work(struct work_struct *work)
976{
977 struct mlxsw_sp *mlxsw_sp = container_of(work, struct mlxsw_sp,
978 router.neighs_update.dw.work);
979 int err;
980
981 err = mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp);
982 if (err)
983 dev_err(mlxsw_sp->bus_info->dev, "Could not update kernel for neigh activity");
984
985 mlxsw_sp_router_neighs_update_nh(mlxsw_sp);
986
Yotam Gigic723c7352016-07-05 11:27:43 +0200987 mlxsw_sp_router_neighs_update_work_schedule(mlxsw_sp);
988}
989
Yotam Gigi0b2361d2016-07-05 11:27:52 +0200990static void mlxsw_sp_router_probe_unresolved_nexthops(struct work_struct *work)
991{
992 struct mlxsw_sp_neigh_entry *neigh_entry;
993 struct mlxsw_sp *mlxsw_sp = container_of(work, struct mlxsw_sp,
994 router.nexthop_probe_dw.work);
995
996 /* Iterate over nexthop neighbours, find those who are unresolved and
997 * send arp on them. This solves the chicken-egg problem when
998 * the nexthop wouldn't get offloaded until the neighbor is resolved
999 * but it wouldn't get resolved ever in case traffic is flowing in HW
1000 * using different nexthop.
1001 *
1002 * Take RTNL mutex here to prevent lists from changes.
1003 */
1004 rtnl_lock();
1005 list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list,
Ido Schimmel8a0b7272017-02-06 16:20:15 +01001006 nexthop_neighs_list_node)
Ido Schimmel01b1aa32017-02-06 16:20:16 +01001007 if (!neigh_entry->connected)
Jiri Pirko33b13412016-11-10 12:31:04 +01001008 neigh_event_send(neigh_entry->key.n, NULL);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001009 rtnl_unlock();
1010
1011 mlxsw_core_schedule_dw(&mlxsw_sp->router.nexthop_probe_dw,
1012 MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL);
1013}
1014
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001015static void
1016mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
1017 struct mlxsw_sp_neigh_entry *neigh_entry,
1018 bool removing);
1019
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001020static enum mlxsw_reg_rauht_op mlxsw_sp_rauht_op(bool adding)
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001021{
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001022 return adding ? MLXSW_REG_RAUHT_OP_WRITE_ADD :
1023 MLXSW_REG_RAUHT_OP_WRITE_DELETE;
1024}
1025
1026static void
1027mlxsw_sp_router_neigh_entry_op4(struct mlxsw_sp *mlxsw_sp,
1028 struct mlxsw_sp_neigh_entry *neigh_entry,
1029 enum mlxsw_reg_rauht_op op)
1030{
Jiri Pirko33b13412016-11-10 12:31:04 +01001031 struct neighbour *n = neigh_entry->key.n;
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001032 u32 dip = ntohl(*((__be32 *) n->primary_key));
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001033 char rauht_pl[MLXSW_REG_RAUHT_LEN];
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001034
1035 mlxsw_reg_rauht_pack4(rauht_pl, op, neigh_entry->rif, neigh_entry->ha,
1036 dip);
1037 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
1038}
1039
1040static void
1041mlxsw_sp_neigh_entry_update(struct mlxsw_sp *mlxsw_sp,
1042 struct mlxsw_sp_neigh_entry *neigh_entry,
1043 bool adding)
1044{
1045 if (!adding && !neigh_entry->connected)
1046 return;
1047 neigh_entry->connected = adding;
1048 if (neigh_entry->key.n->tbl == &arp_tbl)
1049 mlxsw_sp_router_neigh_entry_op4(mlxsw_sp, neigh_entry,
1050 mlxsw_sp_rauht_op(adding));
1051 else
1052 WARN_ON_ONCE(1);
1053}
1054
1055struct mlxsw_sp_neigh_event_work {
1056 struct work_struct work;
1057 struct mlxsw_sp *mlxsw_sp;
1058 struct neighbour *n;
1059};
1060
1061static void mlxsw_sp_router_neigh_event_work(struct work_struct *work)
1062{
1063 struct mlxsw_sp_neigh_event_work *neigh_work =
1064 container_of(work, struct mlxsw_sp_neigh_event_work, work);
1065 struct mlxsw_sp *mlxsw_sp = neigh_work->mlxsw_sp;
1066 struct mlxsw_sp_neigh_entry *neigh_entry;
1067 struct neighbour *n = neigh_work->n;
1068 unsigned char ha[ETH_ALEN];
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001069 bool entry_connected;
Ido Schimmel93a87e52016-12-23 09:32:49 +01001070 u8 nud_state, dead;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001071
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001072 /* If these parameters are changed after we release the lock,
1073 * then we are guaranteed to receive another event letting us
1074 * know about it.
1075 */
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001076 read_lock_bh(&n->lock);
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001077 memcpy(ha, n->ha, ETH_ALEN);
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001078 nud_state = n->nud_state;
Ido Schimmel93a87e52016-12-23 09:32:49 +01001079 dead = n->dead;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001080 read_unlock_bh(&n->lock);
1081
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001082 rtnl_lock();
Ido Schimmel93a87e52016-12-23 09:32:49 +01001083 entry_connected = nud_state & NUD_VALID && !dead;
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001084 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
1085 if (!entry_connected && !neigh_entry)
1086 goto out;
1087 if (!neigh_entry) {
1088 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
1089 if (IS_ERR(neigh_entry))
1090 goto out;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001091 }
1092
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001093 memcpy(neigh_entry->ha, ha, ETH_ALEN);
1094 mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, entry_connected);
1095 mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, !entry_connected);
1096
1097 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
1098 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
1099
1100out:
1101 rtnl_unlock();
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001102 neigh_release(n);
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001103 kfree(neigh_work);
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001104}
1105
Jiri Pirkoe7322632016-09-01 10:37:43 +02001106int mlxsw_sp_router_netevent_event(struct notifier_block *unused,
1107 unsigned long event, void *ptr)
Yotam Gigic723c7352016-07-05 11:27:43 +02001108{
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001109 struct mlxsw_sp_neigh_event_work *neigh_work;
Yotam Gigic723c7352016-07-05 11:27:43 +02001110 struct mlxsw_sp_port *mlxsw_sp_port;
1111 struct mlxsw_sp *mlxsw_sp;
1112 unsigned long interval;
1113 struct neigh_parms *p;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001114 struct neighbour *n;
Yotam Gigic723c7352016-07-05 11:27:43 +02001115
1116 switch (event) {
1117 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
1118 p = ptr;
1119
1120 /* We don't care about changes in the default table. */
1121 if (!p->dev || p->tbl != &arp_tbl)
1122 return NOTIFY_DONE;
1123
1124 /* We are in atomic context and can't take RTNL mutex,
1125 * so use RCU variant to walk the device chain.
1126 */
1127 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev);
1128 if (!mlxsw_sp_port)
1129 return NOTIFY_DONE;
1130
1131 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1132 interval = jiffies_to_msecs(NEIGH_VAR(p, DELAY_PROBE_TIME));
1133 mlxsw_sp->router.neighs_update.interval = interval;
1134
1135 mlxsw_sp_port_dev_put(mlxsw_sp_port);
1136 break;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001137 case NETEVENT_NEIGH_UPDATE:
1138 n = ptr;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001139
1140 if (n->tbl != &arp_tbl)
1141 return NOTIFY_DONE;
1142
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001143 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(n->dev);
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001144 if (!mlxsw_sp_port)
1145 return NOTIFY_DONE;
1146
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001147 neigh_work = kzalloc(sizeof(*neigh_work), GFP_ATOMIC);
1148 if (!neigh_work) {
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001149 mlxsw_sp_port_dev_put(mlxsw_sp_port);
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001150 return NOTIFY_BAD;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001151 }
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001152
1153 INIT_WORK(&neigh_work->work, mlxsw_sp_router_neigh_event_work);
1154 neigh_work->mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1155 neigh_work->n = n;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001156
1157 /* Take a reference to ensure the neighbour won't be
1158 * destructed until we drop the reference in delayed
1159 * work.
1160 */
1161 neigh_clone(n);
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001162 mlxsw_core_schedule_work(&neigh_work->work);
1163 mlxsw_sp_port_dev_put(mlxsw_sp_port);
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001164 break;
Yotam Gigic723c7352016-07-05 11:27:43 +02001165 }
1166
1167 return NOTIFY_DONE;
1168}
1169
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001170static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp)
1171{
Yotam Gigic723c7352016-07-05 11:27:43 +02001172 int err;
1173
1174 err = rhashtable_init(&mlxsw_sp->router.neigh_ht,
1175 &mlxsw_sp_neigh_ht_params);
1176 if (err)
1177 return err;
1178
1179 /* Initialize the polling interval according to the default
1180 * table.
1181 */
1182 mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp);
1183
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001184 /* Create the delayed works for the activity_update */
Yotam Gigic723c7352016-07-05 11:27:43 +02001185 INIT_DELAYED_WORK(&mlxsw_sp->router.neighs_update.dw,
1186 mlxsw_sp_router_neighs_update_work);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001187 INIT_DELAYED_WORK(&mlxsw_sp->router.nexthop_probe_dw,
1188 mlxsw_sp_router_probe_unresolved_nexthops);
Yotam Gigic723c7352016-07-05 11:27:43 +02001189 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw, 0);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001190 mlxsw_core_schedule_dw(&mlxsw_sp->router.nexthop_probe_dw, 0);
Yotam Gigic723c7352016-07-05 11:27:43 +02001191 return 0;
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001192}
1193
1194static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp)
1195{
Yotam Gigic723c7352016-07-05 11:27:43 +02001196 cancel_delayed_work_sync(&mlxsw_sp->router.neighs_update.dw);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001197 cancel_delayed_work_sync(&mlxsw_sp->router.nexthop_probe_dw);
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001198 rhashtable_destroy(&mlxsw_sp->router.neigh_ht);
1199}
1200
Ido Schimmel9665b742017-02-08 11:16:42 +01001201static int mlxsw_sp_neigh_rif_flush(struct mlxsw_sp *mlxsw_sp,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001202 const struct mlxsw_sp_rif *rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001203{
1204 char rauht_pl[MLXSW_REG_RAUHT_LEN];
1205
1206 mlxsw_reg_rauht_pack(rauht_pl, MLXSW_REG_RAUHT_OP_WRITE_DELETE_ALL,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001207 rif->rif_index, rif->addr);
Ido Schimmel9665b742017-02-08 11:16:42 +01001208 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
1209}
1210
1211static void mlxsw_sp_neigh_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001212 struct mlxsw_sp_rif *rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001213{
1214 struct mlxsw_sp_neigh_entry *neigh_entry, *tmp;
1215
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001216 mlxsw_sp_neigh_rif_flush(mlxsw_sp, rif);
1217 list_for_each_entry_safe(neigh_entry, tmp, &rif->neigh_list,
Ido Schimmel9665b742017-02-08 11:16:42 +01001218 rif_list_node)
1219 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
1220}
1221
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001222struct mlxsw_sp_nexthop_key {
1223 struct fib_nh *fib_nh;
1224};
1225
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001226struct mlxsw_sp_nexthop {
1227 struct list_head neigh_list_node; /* member of neigh entry list */
Ido Schimmel9665b742017-02-08 11:16:42 +01001228 struct list_head rif_list_node;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001229 struct mlxsw_sp_nexthop_group *nh_grp; /* pointer back to the group
1230 * this belongs to
1231 */
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001232 struct rhash_head ht_node;
1233 struct mlxsw_sp_nexthop_key key;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001234 struct mlxsw_sp_rif *rif;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001235 u8 should_offload:1, /* set indicates this neigh is connected and
1236 * should be put to KVD linear area of this group.
1237 */
1238 offloaded:1, /* set in case the neigh is actually put into
1239 * KVD linear area of this group.
1240 */
1241 update:1; /* set indicates that MAC of this neigh should be
1242 * updated in HW
1243 */
1244 struct mlxsw_sp_neigh_entry *neigh_entry;
1245};
1246
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001247struct mlxsw_sp_nexthop_group_key {
1248 struct fib_info *fi;
1249};
1250
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001251struct mlxsw_sp_nexthop_group {
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001252 struct rhash_head ht_node;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001253 struct list_head fib_list; /* list of fib entries that use this group */
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001254 struct mlxsw_sp_nexthop_group_key key;
Ido Schimmelb3e8d1e2017-02-08 11:16:32 +01001255 u8 adj_index_valid:1,
1256 gateway:1; /* routes using the group use a gateway */
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001257 u32 adj_index;
1258 u16 ecmp_size;
1259 u16 count;
1260 struct mlxsw_sp_nexthop nexthops[0];
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001261#define nh_rif nexthops[0].rif
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001262};
1263
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001264static const struct rhashtable_params mlxsw_sp_nexthop_group_ht_params = {
1265 .key_offset = offsetof(struct mlxsw_sp_nexthop_group, key),
1266 .head_offset = offsetof(struct mlxsw_sp_nexthop_group, ht_node),
1267 .key_len = sizeof(struct mlxsw_sp_nexthop_group_key),
1268};
1269
1270static int mlxsw_sp_nexthop_group_insert(struct mlxsw_sp *mlxsw_sp,
1271 struct mlxsw_sp_nexthop_group *nh_grp)
1272{
1273 return rhashtable_insert_fast(&mlxsw_sp->router.nexthop_group_ht,
1274 &nh_grp->ht_node,
1275 mlxsw_sp_nexthop_group_ht_params);
1276}
1277
1278static void mlxsw_sp_nexthop_group_remove(struct mlxsw_sp *mlxsw_sp,
1279 struct mlxsw_sp_nexthop_group *nh_grp)
1280{
1281 rhashtable_remove_fast(&mlxsw_sp->router.nexthop_group_ht,
1282 &nh_grp->ht_node,
1283 mlxsw_sp_nexthop_group_ht_params);
1284}
1285
1286static struct mlxsw_sp_nexthop_group *
1287mlxsw_sp_nexthop_group_lookup(struct mlxsw_sp *mlxsw_sp,
1288 struct mlxsw_sp_nexthop_group_key key)
1289{
1290 return rhashtable_lookup_fast(&mlxsw_sp->router.nexthop_group_ht, &key,
1291 mlxsw_sp_nexthop_group_ht_params);
1292}
1293
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001294static const struct rhashtable_params mlxsw_sp_nexthop_ht_params = {
1295 .key_offset = offsetof(struct mlxsw_sp_nexthop, key),
1296 .head_offset = offsetof(struct mlxsw_sp_nexthop, ht_node),
1297 .key_len = sizeof(struct mlxsw_sp_nexthop_key),
1298};
1299
1300static int mlxsw_sp_nexthop_insert(struct mlxsw_sp *mlxsw_sp,
1301 struct mlxsw_sp_nexthop *nh)
1302{
1303 return rhashtable_insert_fast(&mlxsw_sp->router.nexthop_ht,
1304 &nh->ht_node, mlxsw_sp_nexthop_ht_params);
1305}
1306
1307static void mlxsw_sp_nexthop_remove(struct mlxsw_sp *mlxsw_sp,
1308 struct mlxsw_sp_nexthop *nh)
1309{
1310 rhashtable_remove_fast(&mlxsw_sp->router.nexthop_ht, &nh->ht_node,
1311 mlxsw_sp_nexthop_ht_params);
1312}
1313
Ido Schimmelad178c82017-02-08 11:16:40 +01001314static struct mlxsw_sp_nexthop *
1315mlxsw_sp_nexthop_lookup(struct mlxsw_sp *mlxsw_sp,
1316 struct mlxsw_sp_nexthop_key key)
1317{
1318 return rhashtable_lookup_fast(&mlxsw_sp->router.nexthop_ht, &key,
1319 mlxsw_sp_nexthop_ht_params);
1320}
1321
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001322static int mlxsw_sp_adj_index_mass_update_vr(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel76610eb2017-03-10 08:53:41 +01001323 const struct mlxsw_sp_fib *fib,
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001324 u32 adj_index, u16 ecmp_size,
1325 u32 new_adj_index,
1326 u16 new_ecmp_size)
1327{
1328 char raleu_pl[MLXSW_REG_RALEU_LEN];
1329
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001330 mlxsw_reg_raleu_pack(raleu_pl,
Ido Schimmel76610eb2017-03-10 08:53:41 +01001331 (enum mlxsw_reg_ralxx_protocol) fib->proto,
1332 fib->vr->id, adj_index, ecmp_size, new_adj_index,
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001333 new_ecmp_size);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001334 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raleu), raleu_pl);
1335}
1336
1337static int mlxsw_sp_adj_index_mass_update(struct mlxsw_sp *mlxsw_sp,
1338 struct mlxsw_sp_nexthop_group *nh_grp,
1339 u32 old_adj_index, u16 old_ecmp_size)
1340{
1341 struct mlxsw_sp_fib_entry *fib_entry;
Ido Schimmel76610eb2017-03-10 08:53:41 +01001342 struct mlxsw_sp_fib *fib = NULL;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001343 int err;
1344
1345 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
Ido Schimmel76610eb2017-03-10 08:53:41 +01001346 if (fib == fib_entry->fib_node->fib)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001347 continue;
Ido Schimmel76610eb2017-03-10 08:53:41 +01001348 fib = fib_entry->fib_node->fib;
1349 err = mlxsw_sp_adj_index_mass_update_vr(mlxsw_sp, fib,
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001350 old_adj_index,
1351 old_ecmp_size,
1352 nh_grp->adj_index,
1353 nh_grp->ecmp_size);
1354 if (err)
1355 return err;
1356 }
1357 return 0;
1358}
1359
1360static int mlxsw_sp_nexthop_mac_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
1361 struct mlxsw_sp_nexthop *nh)
1362{
1363 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
1364 char ratr_pl[MLXSW_REG_RATR_LEN];
1365
1366 mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY,
1367 true, adj_index, neigh_entry->rif);
1368 mlxsw_reg_ratr_eth_entry_pack(ratr_pl, neigh_entry->ha);
1369 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
1370}
1371
1372static int
1373mlxsw_sp_nexthop_group_mac_update(struct mlxsw_sp *mlxsw_sp,
Ido Schimmela59b7e02017-01-23 11:11:42 +01001374 struct mlxsw_sp_nexthop_group *nh_grp,
1375 bool reallocate)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001376{
1377 u32 adj_index = nh_grp->adj_index; /* base */
1378 struct mlxsw_sp_nexthop *nh;
1379 int i;
1380 int err;
1381
1382 for (i = 0; i < nh_grp->count; i++) {
1383 nh = &nh_grp->nexthops[i];
1384
1385 if (!nh->should_offload) {
1386 nh->offloaded = 0;
1387 continue;
1388 }
1389
Ido Schimmela59b7e02017-01-23 11:11:42 +01001390 if (nh->update || reallocate) {
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001391 err = mlxsw_sp_nexthop_mac_update(mlxsw_sp,
1392 adj_index, nh);
1393 if (err)
1394 return err;
1395 nh->update = 0;
1396 nh->offloaded = 1;
1397 }
1398 adj_index++;
1399 }
1400 return 0;
1401}
1402
1403static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1404 struct mlxsw_sp_fib_entry *fib_entry);
1405
1406static int
1407mlxsw_sp_nexthop_fib_entries_update(struct mlxsw_sp *mlxsw_sp,
1408 struct mlxsw_sp_nexthop_group *nh_grp)
1409{
1410 struct mlxsw_sp_fib_entry *fib_entry;
1411 int err;
1412
1413 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
1414 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1415 if (err)
1416 return err;
1417 }
1418 return 0;
1419}
1420
1421static void
1422mlxsw_sp_nexthop_group_refresh(struct mlxsw_sp *mlxsw_sp,
1423 struct mlxsw_sp_nexthop_group *nh_grp)
1424{
1425 struct mlxsw_sp_nexthop *nh;
1426 bool offload_change = false;
1427 u32 adj_index;
1428 u16 ecmp_size = 0;
1429 bool old_adj_index_valid;
1430 u32 old_adj_index;
1431 u16 old_ecmp_size;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001432 int i;
1433 int err;
1434
Ido Schimmelb3e8d1e2017-02-08 11:16:32 +01001435 if (!nh_grp->gateway) {
1436 mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1437 return;
1438 }
1439
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001440 for (i = 0; i < nh_grp->count; i++) {
1441 nh = &nh_grp->nexthops[i];
1442
1443 if (nh->should_offload ^ nh->offloaded) {
1444 offload_change = true;
1445 if (nh->should_offload)
1446 nh->update = 1;
1447 }
1448 if (nh->should_offload)
1449 ecmp_size++;
1450 }
1451 if (!offload_change) {
1452 /* Nothing was added or removed, so no need to reallocate. Just
1453 * update MAC on existing adjacency indexes.
1454 */
Ido Schimmela59b7e02017-01-23 11:11:42 +01001455 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp,
1456 false);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001457 if (err) {
1458 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1459 goto set_trap;
1460 }
1461 return;
1462 }
1463 if (!ecmp_size)
1464 /* No neigh of this group is connected so we just set
1465 * the trap and let everthing flow through kernel.
1466 */
1467 goto set_trap;
1468
Arkadi Sharshevsky13124442017-03-25 08:28:22 +01001469 err = mlxsw_sp_kvdl_alloc(mlxsw_sp, ecmp_size, &adj_index);
1470 if (err) {
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001471 /* We ran out of KVD linear space, just set the
1472 * trap and let everything flow through kernel.
1473 */
1474 dev_warn(mlxsw_sp->bus_info->dev, "Failed to allocate KVD linear area for nexthop group.\n");
1475 goto set_trap;
1476 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001477 old_adj_index_valid = nh_grp->adj_index_valid;
1478 old_adj_index = nh_grp->adj_index;
1479 old_ecmp_size = nh_grp->ecmp_size;
1480 nh_grp->adj_index_valid = 1;
1481 nh_grp->adj_index = adj_index;
1482 nh_grp->ecmp_size = ecmp_size;
Ido Schimmela59b7e02017-01-23 11:11:42 +01001483 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp, true);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001484 if (err) {
1485 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1486 goto set_trap;
1487 }
1488
1489 if (!old_adj_index_valid) {
1490 /* The trap was set for fib entries, so we have to call
1491 * fib entry update to unset it and use adjacency index.
1492 */
1493 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1494 if (err) {
1495 dev_warn(mlxsw_sp->bus_info->dev, "Failed to add adjacency index to fib entries.\n");
1496 goto set_trap;
1497 }
1498 return;
1499 }
1500
1501 err = mlxsw_sp_adj_index_mass_update(mlxsw_sp, nh_grp,
1502 old_adj_index, old_ecmp_size);
1503 mlxsw_sp_kvdl_free(mlxsw_sp, old_adj_index);
1504 if (err) {
1505 dev_warn(mlxsw_sp->bus_info->dev, "Failed to mass-update adjacency index for nexthop group.\n");
1506 goto set_trap;
1507 }
1508 return;
1509
1510set_trap:
1511 old_adj_index_valid = nh_grp->adj_index_valid;
1512 nh_grp->adj_index_valid = 0;
1513 for (i = 0; i < nh_grp->count; i++) {
1514 nh = &nh_grp->nexthops[i];
1515 nh->offloaded = 0;
1516 }
1517 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1518 if (err)
1519 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set traps for fib entries.\n");
1520 if (old_adj_index_valid)
1521 mlxsw_sp_kvdl_free(mlxsw_sp, nh_grp->adj_index);
1522}
1523
1524static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop *nh,
1525 bool removing)
1526{
1527 if (!removing && !nh->should_offload)
1528 nh->should_offload = 1;
1529 else if (removing && nh->offloaded)
1530 nh->should_offload = 0;
1531 nh->update = 1;
1532}
1533
1534static void
1535mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
1536 struct mlxsw_sp_neigh_entry *neigh_entry,
1537 bool removing)
1538{
1539 struct mlxsw_sp_nexthop *nh;
1540
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001541 list_for_each_entry(nh, &neigh_entry->nexthop_list,
1542 neigh_list_node) {
1543 __mlxsw_sp_nexthop_neigh_update(nh, removing);
1544 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1545 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001546}
1547
Ido Schimmel9665b742017-02-08 11:16:42 +01001548static void mlxsw_sp_nexthop_rif_init(struct mlxsw_sp_nexthop *nh,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001549 struct mlxsw_sp_rif *rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001550{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001551 if (nh->rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001552 return;
1553
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001554 nh->rif = rif;
1555 list_add(&nh->rif_list_node, &rif->nexthop_list);
Ido Schimmel9665b742017-02-08 11:16:42 +01001556}
1557
1558static void mlxsw_sp_nexthop_rif_fini(struct mlxsw_sp_nexthop *nh)
1559{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001560 if (!nh->rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001561 return;
1562
1563 list_del(&nh->rif_list_node);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001564 nh->rif = NULL;
Ido Schimmel9665b742017-02-08 11:16:42 +01001565}
1566
Ido Schimmela8c97012017-02-08 11:16:35 +01001567static int mlxsw_sp_nexthop_neigh_init(struct mlxsw_sp *mlxsw_sp,
1568 struct mlxsw_sp_nexthop *nh)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001569{
1570 struct mlxsw_sp_neigh_entry *neigh_entry;
Ido Schimmela8c97012017-02-08 11:16:35 +01001571 struct fib_nh *fib_nh = nh->key.fib_nh;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001572 struct neighbour *n;
Ido Schimmel93a87e52016-12-23 09:32:49 +01001573 u8 nud_state, dead;
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001574 int err;
1575
Ido Schimmelad178c82017-02-08 11:16:40 +01001576 if (!nh->nh_grp->gateway || nh->neigh_entry)
Ido Schimmelb8399a12017-02-08 11:16:33 +01001577 return 0;
1578
Jiri Pirko33b13412016-11-10 12:31:04 +01001579 /* Take a reference of neigh here ensuring that neigh would
1580 * not be detructed before the nexthop entry is finished.
1581 * The reference is taken either in neigh_lookup() or
Ido Schimmelfd76d912017-02-06 16:20:17 +01001582 * in neigh_create() in case n is not found.
Jiri Pirko33b13412016-11-10 12:31:04 +01001583 */
Ido Schimmela8c97012017-02-08 11:16:35 +01001584 n = neigh_lookup(&arp_tbl, &fib_nh->nh_gw, fib_nh->nh_dev);
Jiri Pirko33b13412016-11-10 12:31:04 +01001585 if (!n) {
Ido Schimmela8c97012017-02-08 11:16:35 +01001586 n = neigh_create(&arp_tbl, &fib_nh->nh_gw, fib_nh->nh_dev);
1587 if (IS_ERR(n))
1588 return PTR_ERR(n);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001589 neigh_event_send(n, NULL);
Jiri Pirko33b13412016-11-10 12:31:04 +01001590 }
1591 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
1592 if (!neigh_entry) {
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001593 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
1594 if (IS_ERR(neigh_entry)) {
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001595 err = -EINVAL;
1596 goto err_neigh_entry_create;
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001597 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001598 }
Yotam Gigib2157142016-07-05 11:27:51 +02001599
1600 /* If that is the first nexthop connected to that neigh, add to
1601 * nexthop_neighs_list
1602 */
1603 if (list_empty(&neigh_entry->nexthop_list))
1604 list_add_tail(&neigh_entry->nexthop_neighs_list_node,
1605 &mlxsw_sp->router.nexthop_neighs_list);
1606
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001607 nh->neigh_entry = neigh_entry;
1608 list_add_tail(&nh->neigh_list_node, &neigh_entry->nexthop_list);
1609 read_lock_bh(&n->lock);
1610 nud_state = n->nud_state;
Ido Schimmel93a87e52016-12-23 09:32:49 +01001611 dead = n->dead;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001612 read_unlock_bh(&n->lock);
Ido Schimmel93a87e52016-12-23 09:32:49 +01001613 __mlxsw_sp_nexthop_neigh_update(nh, !(nud_state & NUD_VALID && !dead));
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001614
1615 return 0;
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001616
1617err_neigh_entry_create:
1618 neigh_release(n);
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001619 return err;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001620}
1621
Ido Schimmela8c97012017-02-08 11:16:35 +01001622static void mlxsw_sp_nexthop_neigh_fini(struct mlxsw_sp *mlxsw_sp,
1623 struct mlxsw_sp_nexthop *nh)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001624{
1625 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
Ido Schimmela8c97012017-02-08 11:16:35 +01001626 struct neighbour *n;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001627
Ido Schimmelb8399a12017-02-08 11:16:33 +01001628 if (!neigh_entry)
Ido Schimmela8c97012017-02-08 11:16:35 +01001629 return;
1630 n = neigh_entry->key.n;
Ido Schimmelb8399a12017-02-08 11:16:33 +01001631
Ido Schimmel58312122016-12-23 09:32:50 +01001632 __mlxsw_sp_nexthop_neigh_update(nh, true);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001633 list_del(&nh->neigh_list_node);
Ido Schimmele58be792017-02-08 11:16:28 +01001634 nh->neigh_entry = NULL;
Yotam Gigib2157142016-07-05 11:27:51 +02001635
1636 /* If that is the last nexthop connected to that neigh, remove from
1637 * nexthop_neighs_list
1638 */
Ido Schimmele58be792017-02-08 11:16:28 +01001639 if (list_empty(&neigh_entry->nexthop_list))
1640 list_del(&neigh_entry->nexthop_neighs_list_node);
Yotam Gigib2157142016-07-05 11:27:51 +02001641
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001642 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
1643 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
1644
1645 neigh_release(n);
Ido Schimmela8c97012017-02-08 11:16:35 +01001646}
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001647
Ido Schimmela8c97012017-02-08 11:16:35 +01001648static int mlxsw_sp_nexthop_init(struct mlxsw_sp *mlxsw_sp,
1649 struct mlxsw_sp_nexthop_group *nh_grp,
1650 struct mlxsw_sp_nexthop *nh,
1651 struct fib_nh *fib_nh)
1652{
1653 struct net_device *dev = fib_nh->nh_dev;
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +01001654 struct in_device *in_dev;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001655 struct mlxsw_sp_rif *rif;
Ido Schimmela8c97012017-02-08 11:16:35 +01001656 int err;
1657
1658 nh->nh_grp = nh_grp;
1659 nh->key.fib_nh = fib_nh;
1660 err = mlxsw_sp_nexthop_insert(mlxsw_sp, nh);
1661 if (err)
1662 return err;
1663
Ido Schimmel97989ee2017-03-10 08:53:38 +01001664 if (!dev)
1665 return 0;
1666
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +01001667 in_dev = __in_dev_get_rtnl(dev);
1668 if (in_dev && IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
1669 fib_nh->nh_flags & RTNH_F_LINKDOWN)
1670 return 0;
1671
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001672 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
1673 if (!rif)
Ido Schimmela8c97012017-02-08 11:16:35 +01001674 return 0;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001675 mlxsw_sp_nexthop_rif_init(nh, rif);
Ido Schimmela8c97012017-02-08 11:16:35 +01001676
1677 err = mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
1678 if (err)
1679 goto err_nexthop_neigh_init;
1680
1681 return 0;
1682
1683err_nexthop_neigh_init:
1684 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
1685 return err;
1686}
1687
1688static void mlxsw_sp_nexthop_fini(struct mlxsw_sp *mlxsw_sp,
1689 struct mlxsw_sp_nexthop *nh)
1690{
1691 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
Ido Schimmel9665b742017-02-08 11:16:42 +01001692 mlxsw_sp_nexthop_rif_fini(nh);
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001693 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001694}
1695
Ido Schimmelad178c82017-02-08 11:16:40 +01001696static void mlxsw_sp_nexthop_event(struct mlxsw_sp *mlxsw_sp,
1697 unsigned long event, struct fib_nh *fib_nh)
1698{
1699 struct mlxsw_sp_nexthop_key key;
1700 struct mlxsw_sp_nexthop *nh;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001701 struct mlxsw_sp_rif *rif;
Ido Schimmelad178c82017-02-08 11:16:40 +01001702
1703 if (mlxsw_sp->router.aborted)
1704 return;
1705
1706 key.fib_nh = fib_nh;
1707 nh = mlxsw_sp_nexthop_lookup(mlxsw_sp, key);
1708 if (WARN_ON_ONCE(!nh))
1709 return;
1710
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001711 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, fib_nh->nh_dev);
1712 if (!rif)
Ido Schimmelad178c82017-02-08 11:16:40 +01001713 return;
1714
1715 switch (event) {
1716 case FIB_EVENT_NH_ADD:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001717 mlxsw_sp_nexthop_rif_init(nh, rif);
Ido Schimmelad178c82017-02-08 11:16:40 +01001718 mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
1719 break;
1720 case FIB_EVENT_NH_DEL:
1721 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
Ido Schimmel9665b742017-02-08 11:16:42 +01001722 mlxsw_sp_nexthop_rif_fini(nh);
Ido Schimmelad178c82017-02-08 11:16:40 +01001723 break;
1724 }
1725
1726 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1727}
1728
Ido Schimmel9665b742017-02-08 11:16:42 +01001729static void mlxsw_sp_nexthop_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001730 struct mlxsw_sp_rif *rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001731{
1732 struct mlxsw_sp_nexthop *nh, *tmp;
1733
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001734 list_for_each_entry_safe(nh, tmp, &rif->nexthop_list, rif_list_node) {
Ido Schimmel9665b742017-02-08 11:16:42 +01001735 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
1736 mlxsw_sp_nexthop_rif_fini(nh);
1737 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1738 }
1739}
1740
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001741static struct mlxsw_sp_nexthop_group *
1742mlxsw_sp_nexthop_group_create(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
1743{
1744 struct mlxsw_sp_nexthop_group *nh_grp;
1745 struct mlxsw_sp_nexthop *nh;
1746 struct fib_nh *fib_nh;
1747 size_t alloc_size;
1748 int i;
1749 int err;
1750
1751 alloc_size = sizeof(*nh_grp) +
1752 fi->fib_nhs * sizeof(struct mlxsw_sp_nexthop);
1753 nh_grp = kzalloc(alloc_size, GFP_KERNEL);
1754 if (!nh_grp)
1755 return ERR_PTR(-ENOMEM);
1756 INIT_LIST_HEAD(&nh_grp->fib_list);
Ido Schimmelb3e8d1e2017-02-08 11:16:32 +01001757 nh_grp->gateway = fi->fib_nh->nh_scope == RT_SCOPE_LINK;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001758 nh_grp->count = fi->fib_nhs;
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001759 nh_grp->key.fi = fi;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001760 for (i = 0; i < nh_grp->count; i++) {
1761 nh = &nh_grp->nexthops[i];
1762 fib_nh = &fi->fib_nh[i];
1763 err = mlxsw_sp_nexthop_init(mlxsw_sp, nh_grp, nh, fib_nh);
1764 if (err)
1765 goto err_nexthop_init;
1766 }
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001767 err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp);
1768 if (err)
1769 goto err_nexthop_group_insert;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001770 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
1771 return nh_grp;
1772
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001773err_nexthop_group_insert:
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001774err_nexthop_init:
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +01001775 for (i--; i >= 0; i--) {
1776 nh = &nh_grp->nexthops[i];
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001777 mlxsw_sp_nexthop_fini(mlxsw_sp, nh);
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +01001778 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001779 kfree(nh_grp);
1780 return ERR_PTR(err);
1781}
1782
1783static void
1784mlxsw_sp_nexthop_group_destroy(struct mlxsw_sp *mlxsw_sp,
1785 struct mlxsw_sp_nexthop_group *nh_grp)
1786{
1787 struct mlxsw_sp_nexthop *nh;
1788 int i;
1789
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001790 mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001791 for (i = 0; i < nh_grp->count; i++) {
1792 nh = &nh_grp->nexthops[i];
1793 mlxsw_sp_nexthop_fini(mlxsw_sp, nh);
1794 }
Ido Schimmel58312122016-12-23 09:32:50 +01001795 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
1796 WARN_ON_ONCE(nh_grp->adj_index_valid);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001797 kfree(nh_grp);
1798}
1799
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001800static int mlxsw_sp_nexthop_group_get(struct mlxsw_sp *mlxsw_sp,
1801 struct mlxsw_sp_fib_entry *fib_entry,
1802 struct fib_info *fi)
1803{
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001804 struct mlxsw_sp_nexthop_group_key key;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001805 struct mlxsw_sp_nexthop_group *nh_grp;
1806
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001807 key.fi = fi;
1808 nh_grp = mlxsw_sp_nexthop_group_lookup(mlxsw_sp, key);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001809 if (!nh_grp) {
1810 nh_grp = mlxsw_sp_nexthop_group_create(mlxsw_sp, fi);
1811 if (IS_ERR(nh_grp))
1812 return PTR_ERR(nh_grp);
1813 }
1814 list_add_tail(&fib_entry->nexthop_group_node, &nh_grp->fib_list);
1815 fib_entry->nh_group = nh_grp;
1816 return 0;
1817}
1818
1819static void mlxsw_sp_nexthop_group_put(struct mlxsw_sp *mlxsw_sp,
1820 struct mlxsw_sp_fib_entry *fib_entry)
1821{
1822 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
1823
1824 list_del(&fib_entry->nexthop_group_node);
1825 if (!list_empty(&nh_grp->fib_list))
1826 return;
1827 mlxsw_sp_nexthop_group_destroy(mlxsw_sp, nh_grp);
1828}
1829
Ido Schimmel013b20f2017-02-08 11:16:36 +01001830static bool
1831mlxsw_sp_fib_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry)
1832{
1833 struct mlxsw_sp_nexthop_group *nh_group = fib_entry->nh_group;
1834
Ido Schimmel9aecce12017-02-09 10:28:42 +01001835 if (fib_entry->params.tos)
1836 return false;
1837
Ido Schimmel013b20f2017-02-08 11:16:36 +01001838 switch (fib_entry->type) {
1839 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
1840 return !!nh_group->adj_index_valid;
1841 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
Ido Schimmel70ad3502017-02-08 11:16:38 +01001842 return !!nh_group->nh_rif;
Ido Schimmel013b20f2017-02-08 11:16:36 +01001843 default:
1844 return false;
1845 }
1846}
1847
1848static void mlxsw_sp_fib_entry_offload_set(struct mlxsw_sp_fib_entry *fib_entry)
1849{
1850 fib_entry->offloaded = true;
1851
Ido Schimmel76610eb2017-03-10 08:53:41 +01001852 switch (fib_entry->fib_node->fib->proto) {
Ido Schimmel013b20f2017-02-08 11:16:36 +01001853 case MLXSW_SP_L3_PROTO_IPV4:
1854 fib_info_offload_inc(fib_entry->nh_group->key.fi);
1855 break;
1856 case MLXSW_SP_L3_PROTO_IPV6:
1857 WARN_ON_ONCE(1);
1858 }
1859}
1860
1861static void
1862mlxsw_sp_fib_entry_offload_unset(struct mlxsw_sp_fib_entry *fib_entry)
1863{
Ido Schimmel76610eb2017-03-10 08:53:41 +01001864 switch (fib_entry->fib_node->fib->proto) {
Ido Schimmel013b20f2017-02-08 11:16:36 +01001865 case MLXSW_SP_L3_PROTO_IPV4:
1866 fib_info_offload_dec(fib_entry->nh_group->key.fi);
1867 break;
1868 case MLXSW_SP_L3_PROTO_IPV6:
1869 WARN_ON_ONCE(1);
1870 }
1871
1872 fib_entry->offloaded = false;
1873}
1874
1875static void
1876mlxsw_sp_fib_entry_offload_refresh(struct mlxsw_sp_fib_entry *fib_entry,
1877 enum mlxsw_reg_ralue_op op, int err)
1878{
1879 switch (op) {
1880 case MLXSW_REG_RALUE_OP_WRITE_DELETE:
1881 if (!fib_entry->offloaded)
1882 return;
1883 return mlxsw_sp_fib_entry_offload_unset(fib_entry);
1884 case MLXSW_REG_RALUE_OP_WRITE_WRITE:
1885 if (err)
1886 return;
1887 if (mlxsw_sp_fib_entry_should_offload(fib_entry) &&
1888 !fib_entry->offloaded)
1889 mlxsw_sp_fib_entry_offload_set(fib_entry);
1890 else if (!mlxsw_sp_fib_entry_should_offload(fib_entry) &&
1891 fib_entry->offloaded)
1892 mlxsw_sp_fib_entry_offload_unset(fib_entry);
1893 return;
1894 default:
1895 return;
1896 }
1897}
1898
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001899static int mlxsw_sp_fib_entry_op4_remote(struct mlxsw_sp *mlxsw_sp,
1900 struct mlxsw_sp_fib_entry *fib_entry,
1901 enum mlxsw_reg_ralue_op op)
1902{
1903 char ralue_pl[MLXSW_REG_RALUE_LEN];
Ido Schimmel76610eb2017-03-10 08:53:41 +01001904 struct mlxsw_sp_fib *fib = fib_entry->fib_node->fib;
Ido Schimmel9aecce12017-02-09 10:28:42 +01001905 u32 *p_dip = (u32 *) fib_entry->fib_node->key.addr;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001906 enum mlxsw_reg_ralue_trap_action trap_action;
1907 u16 trap_id = 0;
1908 u32 adjacency_index = 0;
1909 u16 ecmp_size = 0;
1910
1911 /* In case the nexthop group adjacency index is valid, use it
1912 * with provided ECMP size. Otherwise, setup trap and pass
1913 * traffic to kernel.
1914 */
Ido Schimmel4b411472017-02-08 11:16:37 +01001915 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001916 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
1917 adjacency_index = fib_entry->nh_group->adj_index;
1918 ecmp_size = fib_entry->nh_group->ecmp_size;
1919 } else {
1920 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
1921 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
1922 }
1923
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001924 mlxsw_reg_ralue_pack4(ralue_pl,
Ido Schimmel76610eb2017-03-10 08:53:41 +01001925 (enum mlxsw_reg_ralxx_protocol) fib->proto, op,
1926 fib->vr->id, fib_entry->fib_node->key.prefix_len,
Ido Schimmel9aecce12017-02-09 10:28:42 +01001927 *p_dip);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001928 mlxsw_reg_ralue_act_remote_pack(ralue_pl, trap_action, trap_id,
1929 adjacency_index, ecmp_size);
1930 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1931}
1932
Jiri Pirko61c503f2016-07-04 08:23:11 +02001933static int mlxsw_sp_fib_entry_op4_local(struct mlxsw_sp *mlxsw_sp,
1934 struct mlxsw_sp_fib_entry *fib_entry,
1935 enum mlxsw_reg_ralue_op op)
1936{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001937 struct mlxsw_sp_rif *rif = fib_entry->nh_group->nh_rif;
Ido Schimmel76610eb2017-03-10 08:53:41 +01001938 struct mlxsw_sp_fib *fib = fib_entry->fib_node->fib;
Ido Schimmel70ad3502017-02-08 11:16:38 +01001939 enum mlxsw_reg_ralue_trap_action trap_action;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001940 char ralue_pl[MLXSW_REG_RALUE_LEN];
Ido Schimmel9aecce12017-02-09 10:28:42 +01001941 u32 *p_dip = (u32 *) fib_entry->fib_node->key.addr;
Ido Schimmel70ad3502017-02-08 11:16:38 +01001942 u16 trap_id = 0;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001943 u16 rif_index = 0;
Ido Schimmel70ad3502017-02-08 11:16:38 +01001944
1945 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
1946 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001947 rif_index = rif->rif_index;
Ido Schimmel70ad3502017-02-08 11:16:38 +01001948 } else {
1949 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
1950 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
1951 }
Jiri Pirko61c503f2016-07-04 08:23:11 +02001952
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001953 mlxsw_reg_ralue_pack4(ralue_pl,
Ido Schimmel76610eb2017-03-10 08:53:41 +01001954 (enum mlxsw_reg_ralxx_protocol) fib->proto, op,
1955 fib->vr->id, fib_entry->fib_node->key.prefix_len,
Ido Schimmel9aecce12017-02-09 10:28:42 +01001956 *p_dip);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001957 mlxsw_reg_ralue_act_local_pack(ralue_pl, trap_action, trap_id,
1958 rif_index);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001959 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1960}
1961
1962static int mlxsw_sp_fib_entry_op4_trap(struct mlxsw_sp *mlxsw_sp,
1963 struct mlxsw_sp_fib_entry *fib_entry,
1964 enum mlxsw_reg_ralue_op op)
1965{
Ido Schimmel76610eb2017-03-10 08:53:41 +01001966 struct mlxsw_sp_fib *fib = fib_entry->fib_node->fib;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001967 char ralue_pl[MLXSW_REG_RALUE_LEN];
Ido Schimmel9aecce12017-02-09 10:28:42 +01001968 u32 *p_dip = (u32 *) fib_entry->fib_node->key.addr;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001969
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001970 mlxsw_reg_ralue_pack4(ralue_pl,
Ido Schimmel76610eb2017-03-10 08:53:41 +01001971 (enum mlxsw_reg_ralxx_protocol) fib->proto, op,
1972 fib->vr->id, fib_entry->fib_node->key.prefix_len,
Ido Schimmel9aecce12017-02-09 10:28:42 +01001973 *p_dip);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001974 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
1975 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1976}
1977
1978static int mlxsw_sp_fib_entry_op4(struct mlxsw_sp *mlxsw_sp,
1979 struct mlxsw_sp_fib_entry *fib_entry,
1980 enum mlxsw_reg_ralue_op op)
1981{
1982 switch (fib_entry->type) {
1983 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001984 return mlxsw_sp_fib_entry_op4_remote(mlxsw_sp, fib_entry, op);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001985 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
1986 return mlxsw_sp_fib_entry_op4_local(mlxsw_sp, fib_entry, op);
1987 case MLXSW_SP_FIB_ENTRY_TYPE_TRAP:
1988 return mlxsw_sp_fib_entry_op4_trap(mlxsw_sp, fib_entry, op);
1989 }
1990 return -EINVAL;
1991}
1992
1993static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
1994 struct mlxsw_sp_fib_entry *fib_entry,
1995 enum mlxsw_reg_ralue_op op)
1996{
Ido Schimmel013b20f2017-02-08 11:16:36 +01001997 int err = -EINVAL;
1998
Ido Schimmel76610eb2017-03-10 08:53:41 +01001999 switch (fib_entry->fib_node->fib->proto) {
Jiri Pirko61c503f2016-07-04 08:23:11 +02002000 case MLXSW_SP_L3_PROTO_IPV4:
Ido Schimmel013b20f2017-02-08 11:16:36 +01002001 err = mlxsw_sp_fib_entry_op4(mlxsw_sp, fib_entry, op);
2002 break;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002003 case MLXSW_SP_L3_PROTO_IPV6:
Ido Schimmel013b20f2017-02-08 11:16:36 +01002004 return err;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002005 }
Ido Schimmel013b20f2017-02-08 11:16:36 +01002006 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, err);
2007 return err;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002008}
2009
2010static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
2011 struct mlxsw_sp_fib_entry *fib_entry)
2012{
Jiri Pirko7146da32016-09-01 10:37:41 +02002013 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
2014 MLXSW_REG_RALUE_OP_WRITE_WRITE);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002015}
2016
2017static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
2018 struct mlxsw_sp_fib_entry *fib_entry)
2019{
2020 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
2021 MLXSW_REG_RALUE_OP_WRITE_DELETE);
2022}
2023
Jiri Pirko61c503f2016-07-04 08:23:11 +02002024static int
Ido Schimmel013b20f2017-02-08 11:16:36 +01002025mlxsw_sp_fib4_entry_type_set(struct mlxsw_sp *mlxsw_sp,
2026 const struct fib_entry_notifier_info *fen_info,
2027 struct mlxsw_sp_fib_entry *fib_entry)
Jiri Pirko61c503f2016-07-04 08:23:11 +02002028{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002029 struct fib_info *fi = fen_info->fi;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002030
Ido Schimmel97989ee2017-03-10 08:53:38 +01002031 switch (fen_info->type) {
2032 case RTN_BROADCAST: /* fall through */
2033 case RTN_LOCAL:
Jiri Pirko61c503f2016-07-04 08:23:11 +02002034 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
2035 return 0;
Ido Schimmel97989ee2017-03-10 08:53:38 +01002036 case RTN_UNREACHABLE: /* fall through */
2037 case RTN_BLACKHOLE: /* fall through */
2038 case RTN_PROHIBIT:
2039 /* Packets hitting these routes need to be trapped, but
2040 * can do so with a lower priority than packets directed
2041 * at the host, so use action type local instead of trap.
2042 */
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002043 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
Ido Schimmel97989ee2017-03-10 08:53:38 +01002044 return 0;
2045 case RTN_UNICAST:
2046 if (fi->fib_nh->nh_scope != RT_SCOPE_LINK)
2047 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
2048 else
2049 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
2050 return 0;
2051 default:
2052 return -EINVAL;
2053 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002054}
2055
Jiri Pirko5b004412016-09-01 10:37:40 +02002056static struct mlxsw_sp_fib_entry *
Ido Schimmel9aecce12017-02-09 10:28:42 +01002057mlxsw_sp_fib4_entry_create(struct mlxsw_sp *mlxsw_sp,
2058 struct mlxsw_sp_fib_node *fib_node,
2059 const struct fib_entry_notifier_info *fen_info)
Jiri Pirko5b004412016-09-01 10:37:40 +02002060{
2061 struct mlxsw_sp_fib_entry *fib_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002062 int err;
2063
2064 fib_entry = kzalloc(sizeof(*fib_entry), GFP_KERNEL);
2065 if (!fib_entry) {
2066 err = -ENOMEM;
2067 goto err_fib_entry_alloc;
2068 }
2069
2070 err = mlxsw_sp_fib4_entry_type_set(mlxsw_sp, fen_info, fib_entry);
2071 if (err)
2072 goto err_fib4_entry_type_set;
2073
2074 err = mlxsw_sp_nexthop_group_get(mlxsw_sp, fib_entry, fen_info->fi);
2075 if (err)
2076 goto err_nexthop_group_get;
2077
2078 fib_entry->params.prio = fen_info->fi->fib_priority;
2079 fib_entry->params.tb_id = fen_info->tb_id;
2080 fib_entry->params.type = fen_info->type;
2081 fib_entry->params.tos = fen_info->tos;
2082
2083 fib_entry->fib_node = fib_node;
2084
2085 return fib_entry;
2086
2087err_nexthop_group_get:
2088err_fib4_entry_type_set:
2089 kfree(fib_entry);
2090err_fib_entry_alloc:
2091 return ERR_PTR(err);
2092}
2093
2094static void mlxsw_sp_fib4_entry_destroy(struct mlxsw_sp *mlxsw_sp,
2095 struct mlxsw_sp_fib_entry *fib_entry)
2096{
2097 mlxsw_sp_nexthop_group_put(mlxsw_sp, fib_entry);
2098 kfree(fib_entry);
2099}
2100
2101static struct mlxsw_sp_fib_node *
2102mlxsw_sp_fib4_node_get(struct mlxsw_sp *mlxsw_sp,
2103 const struct fib_entry_notifier_info *fen_info);
2104
2105static struct mlxsw_sp_fib_entry *
2106mlxsw_sp_fib4_entry_lookup(struct mlxsw_sp *mlxsw_sp,
2107 const struct fib_entry_notifier_info *fen_info)
2108{
2109 struct mlxsw_sp_fib_entry *fib_entry;
2110 struct mlxsw_sp_fib_node *fib_node;
2111
2112 fib_node = mlxsw_sp_fib4_node_get(mlxsw_sp, fen_info);
2113 if (IS_ERR(fib_node))
2114 return NULL;
2115
2116 list_for_each_entry(fib_entry, &fib_node->entry_list, list) {
2117 if (fib_entry->params.tb_id == fen_info->tb_id &&
2118 fib_entry->params.tos == fen_info->tos &&
2119 fib_entry->params.type == fen_info->type &&
2120 fib_entry->nh_group->key.fi == fen_info->fi) {
2121 return fib_entry;
2122 }
2123 }
2124
2125 return NULL;
2126}
2127
2128static const struct rhashtable_params mlxsw_sp_fib_ht_params = {
2129 .key_offset = offsetof(struct mlxsw_sp_fib_node, key),
2130 .head_offset = offsetof(struct mlxsw_sp_fib_node, ht_node),
2131 .key_len = sizeof(struct mlxsw_sp_fib_key),
2132 .automatic_shrinking = true,
2133};
2134
2135static int mlxsw_sp_fib_node_insert(struct mlxsw_sp_fib *fib,
2136 struct mlxsw_sp_fib_node *fib_node)
2137{
2138 return rhashtable_insert_fast(&fib->ht, &fib_node->ht_node,
2139 mlxsw_sp_fib_ht_params);
2140}
2141
2142static void mlxsw_sp_fib_node_remove(struct mlxsw_sp_fib *fib,
2143 struct mlxsw_sp_fib_node *fib_node)
2144{
2145 rhashtable_remove_fast(&fib->ht, &fib_node->ht_node,
2146 mlxsw_sp_fib_ht_params);
2147}
2148
2149static struct mlxsw_sp_fib_node *
2150mlxsw_sp_fib_node_lookup(struct mlxsw_sp_fib *fib, const void *addr,
2151 size_t addr_len, unsigned char prefix_len)
2152{
2153 struct mlxsw_sp_fib_key key;
2154
2155 memset(&key, 0, sizeof(key));
2156 memcpy(key.addr, addr, addr_len);
2157 key.prefix_len = prefix_len;
2158 return rhashtable_lookup_fast(&fib->ht, &key, mlxsw_sp_fib_ht_params);
2159}
2160
2161static struct mlxsw_sp_fib_node *
Ido Schimmel76610eb2017-03-10 08:53:41 +01002162mlxsw_sp_fib_node_create(struct mlxsw_sp_fib *fib, const void *addr,
Ido Schimmel9aecce12017-02-09 10:28:42 +01002163 size_t addr_len, unsigned char prefix_len)
2164{
2165 struct mlxsw_sp_fib_node *fib_node;
2166
2167 fib_node = kzalloc(sizeof(*fib_node), GFP_KERNEL);
2168 if (!fib_node)
2169 return NULL;
2170
2171 INIT_LIST_HEAD(&fib_node->entry_list);
Ido Schimmel76610eb2017-03-10 08:53:41 +01002172 list_add(&fib_node->list, &fib->node_list);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002173 memcpy(fib_node->key.addr, addr, addr_len);
2174 fib_node->key.prefix_len = prefix_len;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002175
2176 return fib_node;
2177}
2178
2179static void mlxsw_sp_fib_node_destroy(struct mlxsw_sp_fib_node *fib_node)
2180{
Ido Schimmel9aecce12017-02-09 10:28:42 +01002181 list_del(&fib_node->list);
2182 WARN_ON(!list_empty(&fib_node->entry_list));
2183 kfree(fib_node);
2184}
2185
2186static bool
2187mlxsw_sp_fib_node_entry_is_first(const struct mlxsw_sp_fib_node *fib_node,
2188 const struct mlxsw_sp_fib_entry *fib_entry)
2189{
2190 return list_first_entry(&fib_node->entry_list,
2191 struct mlxsw_sp_fib_entry, list) == fib_entry;
2192}
2193
2194static void mlxsw_sp_fib_node_prefix_inc(struct mlxsw_sp_fib_node *fib_node)
2195{
2196 unsigned char prefix_len = fib_node->key.prefix_len;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002197 struct mlxsw_sp_fib *fib = fib_node->fib;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002198
2199 if (fib->prefix_ref_count[prefix_len]++ == 0)
2200 mlxsw_sp_prefix_usage_set(&fib->prefix_usage, prefix_len);
2201}
2202
2203static void mlxsw_sp_fib_node_prefix_dec(struct mlxsw_sp_fib_node *fib_node)
2204{
2205 unsigned char prefix_len = fib_node->key.prefix_len;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002206 struct mlxsw_sp_fib *fib = fib_node->fib;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002207
2208 if (--fib->prefix_ref_count[prefix_len] == 0)
2209 mlxsw_sp_prefix_usage_clear(&fib->prefix_usage, prefix_len);
2210}
2211
Ido Schimmel76610eb2017-03-10 08:53:41 +01002212static int mlxsw_sp_fib_node_init(struct mlxsw_sp *mlxsw_sp,
2213 struct mlxsw_sp_fib_node *fib_node,
2214 struct mlxsw_sp_fib *fib)
2215{
2216 struct mlxsw_sp_prefix_usage req_prefix_usage;
2217 struct mlxsw_sp_lpm_tree *lpm_tree;
2218 int err;
2219
2220 err = mlxsw_sp_fib_node_insert(fib, fib_node);
2221 if (err)
2222 return err;
2223 fib_node->fib = fib;
2224
2225 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage, &fib->prefix_usage);
2226 mlxsw_sp_prefix_usage_set(&req_prefix_usage, fib_node->key.prefix_len);
2227
2228 if (!mlxsw_sp_prefix_usage_none(&fib->prefix_usage)) {
2229 err = mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, fib,
2230 &req_prefix_usage);
2231 if (err)
2232 goto err_tree_check;
2233 } else {
2234 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
2235 fib->proto);
2236 if (IS_ERR(lpm_tree))
2237 return PTR_ERR(lpm_tree);
2238 fib->lpm_tree = lpm_tree;
2239 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, fib);
2240 if (err)
2241 goto err_tree_bind;
2242 }
2243
2244 mlxsw_sp_fib_node_prefix_inc(fib_node);
2245
2246 return 0;
2247
2248err_tree_bind:
2249 fib->lpm_tree = NULL;
2250 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
2251err_tree_check:
2252 fib_node->fib = NULL;
2253 mlxsw_sp_fib_node_remove(fib, fib_node);
2254 return err;
2255}
2256
2257static void mlxsw_sp_fib_node_fini(struct mlxsw_sp *mlxsw_sp,
2258 struct mlxsw_sp_fib_node *fib_node)
2259{
2260 struct mlxsw_sp_lpm_tree *lpm_tree = fib_node->fib->lpm_tree;
2261 struct mlxsw_sp_fib *fib = fib_node->fib;
2262
2263 mlxsw_sp_fib_node_prefix_dec(fib_node);
2264
2265 if (mlxsw_sp_prefix_usage_none(&fib->prefix_usage)) {
2266 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, fib);
2267 fib->lpm_tree = NULL;
2268 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
2269 } else {
2270 mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, fib, &fib->prefix_usage);
2271 }
2272
2273 fib_node->fib = NULL;
2274 mlxsw_sp_fib_node_remove(fib, fib_node);
2275}
2276
Ido Schimmel9aecce12017-02-09 10:28:42 +01002277static struct mlxsw_sp_fib_node *
2278mlxsw_sp_fib4_node_get(struct mlxsw_sp *mlxsw_sp,
2279 const struct fib_entry_notifier_info *fen_info)
2280{
2281 struct mlxsw_sp_fib_node *fib_node;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002282 struct mlxsw_sp_fib *fib;
Jiri Pirko5b004412016-09-01 10:37:40 +02002283 struct mlxsw_sp_vr *vr;
2284 int err;
2285
Ido Schimmel76610eb2017-03-10 08:53:41 +01002286 vr = mlxsw_sp_vr_get(mlxsw_sp, fen_info->tb_id);
Jiri Pirko5b004412016-09-01 10:37:40 +02002287 if (IS_ERR(vr))
2288 return ERR_CAST(vr);
Ido Schimmel76610eb2017-03-10 08:53:41 +01002289 fib = mlxsw_sp_vr_fib(vr, MLXSW_SP_L3_PROTO_IPV4);
Jiri Pirko5b004412016-09-01 10:37:40 +02002290
Ido Schimmel76610eb2017-03-10 08:53:41 +01002291 fib_node = mlxsw_sp_fib_node_lookup(fib, &fen_info->dst,
Ido Schimmel9aecce12017-02-09 10:28:42 +01002292 sizeof(fen_info->dst),
2293 fen_info->dst_len);
2294 if (fib_node)
2295 return fib_node;
2296
Ido Schimmel76610eb2017-03-10 08:53:41 +01002297 fib_node = mlxsw_sp_fib_node_create(fib, &fen_info->dst,
Ido Schimmel9aecce12017-02-09 10:28:42 +01002298 sizeof(fen_info->dst),
2299 fen_info->dst_len);
2300 if (!fib_node) {
Jiri Pirko5b004412016-09-01 10:37:40 +02002301 err = -ENOMEM;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002302 goto err_fib_node_create;
Jiri Pirko5b004412016-09-01 10:37:40 +02002303 }
Jiri Pirko5b004412016-09-01 10:37:40 +02002304
Ido Schimmel76610eb2017-03-10 08:53:41 +01002305 err = mlxsw_sp_fib_node_init(mlxsw_sp, fib_node, fib);
2306 if (err)
2307 goto err_fib_node_init;
2308
Ido Schimmel9aecce12017-02-09 10:28:42 +01002309 return fib_node;
Jiri Pirko5b004412016-09-01 10:37:40 +02002310
Ido Schimmel76610eb2017-03-10 08:53:41 +01002311err_fib_node_init:
2312 mlxsw_sp_fib_node_destroy(fib_node);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002313err_fib_node_create:
Ido Schimmel76610eb2017-03-10 08:53:41 +01002314 mlxsw_sp_vr_put(vr);
Jiri Pirko5b004412016-09-01 10:37:40 +02002315 return ERR_PTR(err);
2316}
2317
Ido Schimmel9aecce12017-02-09 10:28:42 +01002318static void mlxsw_sp_fib4_node_put(struct mlxsw_sp *mlxsw_sp,
2319 struct mlxsw_sp_fib_node *fib_node)
Jiri Pirko5b004412016-09-01 10:37:40 +02002320{
Ido Schimmel76610eb2017-03-10 08:53:41 +01002321 struct mlxsw_sp_vr *vr = fib_node->fib->vr;
Jiri Pirko5b004412016-09-01 10:37:40 +02002322
Ido Schimmel9aecce12017-02-09 10:28:42 +01002323 if (!list_empty(&fib_node->entry_list))
2324 return;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002325 mlxsw_sp_fib_node_fini(mlxsw_sp, fib_node);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002326 mlxsw_sp_fib_node_destroy(fib_node);
Ido Schimmel76610eb2017-03-10 08:53:41 +01002327 mlxsw_sp_vr_put(vr);
Jiri Pirko5b004412016-09-01 10:37:40 +02002328}
2329
Ido Schimmel9aecce12017-02-09 10:28:42 +01002330static struct mlxsw_sp_fib_entry *
2331mlxsw_sp_fib4_node_entry_find(const struct mlxsw_sp_fib_node *fib_node,
2332 const struct mlxsw_sp_fib_entry_params *params)
Jiri Pirko61c503f2016-07-04 08:23:11 +02002333{
Jiri Pirko61c503f2016-07-04 08:23:11 +02002334 struct mlxsw_sp_fib_entry *fib_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002335
2336 list_for_each_entry(fib_entry, &fib_node->entry_list, list) {
2337 if (fib_entry->params.tb_id > params->tb_id)
2338 continue;
2339 if (fib_entry->params.tb_id != params->tb_id)
2340 break;
2341 if (fib_entry->params.tos > params->tos)
2342 continue;
2343 if (fib_entry->params.prio >= params->prio ||
2344 fib_entry->params.tos < params->tos)
2345 return fib_entry;
2346 }
2347
2348 return NULL;
2349}
2350
Ido Schimmel4283bce2017-02-09 10:28:43 +01002351static int mlxsw_sp_fib4_node_list_append(struct mlxsw_sp_fib_entry *fib_entry,
2352 struct mlxsw_sp_fib_entry *new_entry)
2353{
2354 struct mlxsw_sp_fib_node *fib_node;
2355
2356 if (WARN_ON(!fib_entry))
2357 return -EINVAL;
2358
2359 fib_node = fib_entry->fib_node;
2360 list_for_each_entry_from(fib_entry, &fib_node->entry_list, list) {
2361 if (fib_entry->params.tb_id != new_entry->params.tb_id ||
2362 fib_entry->params.tos != new_entry->params.tos ||
2363 fib_entry->params.prio != new_entry->params.prio)
2364 break;
2365 }
2366
2367 list_add_tail(&new_entry->list, &fib_entry->list);
2368 return 0;
2369}
2370
Ido Schimmel9aecce12017-02-09 10:28:42 +01002371static int
2372mlxsw_sp_fib4_node_list_insert(struct mlxsw_sp_fib_node *fib_node,
Ido Schimmel4283bce2017-02-09 10:28:43 +01002373 struct mlxsw_sp_fib_entry *new_entry,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002374 bool replace, bool append)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002375{
2376 struct mlxsw_sp_fib_entry *fib_entry;
2377
2378 fib_entry = mlxsw_sp_fib4_node_entry_find(fib_node, &new_entry->params);
2379
Ido Schimmel4283bce2017-02-09 10:28:43 +01002380 if (append)
2381 return mlxsw_sp_fib4_node_list_append(fib_entry, new_entry);
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002382 if (replace && WARN_ON(!fib_entry))
2383 return -EINVAL;
Ido Schimmel4283bce2017-02-09 10:28:43 +01002384
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002385 /* Insert new entry before replaced one, so that we can later
2386 * remove the second.
2387 */
Ido Schimmel9aecce12017-02-09 10:28:42 +01002388 if (fib_entry) {
2389 list_add_tail(&new_entry->list, &fib_entry->list);
2390 } else {
2391 struct mlxsw_sp_fib_entry *last;
2392
2393 list_for_each_entry(last, &fib_node->entry_list, list) {
2394 if (new_entry->params.tb_id > last->params.tb_id)
2395 break;
2396 fib_entry = last;
2397 }
2398
2399 if (fib_entry)
2400 list_add(&new_entry->list, &fib_entry->list);
2401 else
2402 list_add(&new_entry->list, &fib_node->entry_list);
2403 }
2404
2405 return 0;
2406}
2407
2408static void
2409mlxsw_sp_fib4_node_list_remove(struct mlxsw_sp_fib_entry *fib_entry)
2410{
2411 list_del(&fib_entry->list);
2412}
2413
2414static int
2415mlxsw_sp_fib4_node_entry_add(struct mlxsw_sp *mlxsw_sp,
2416 const struct mlxsw_sp_fib_node *fib_node,
2417 struct mlxsw_sp_fib_entry *fib_entry)
2418{
2419 if (!mlxsw_sp_fib_node_entry_is_first(fib_node, fib_entry))
2420 return 0;
2421
2422 /* To prevent packet loss, overwrite the previously offloaded
2423 * entry.
2424 */
2425 if (!list_is_singular(&fib_node->entry_list)) {
2426 enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_DELETE;
2427 struct mlxsw_sp_fib_entry *n = list_next_entry(fib_entry, list);
2428
2429 mlxsw_sp_fib_entry_offload_refresh(n, op, 0);
2430 }
2431
2432 return mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
2433}
2434
2435static void
2436mlxsw_sp_fib4_node_entry_del(struct mlxsw_sp *mlxsw_sp,
2437 const struct mlxsw_sp_fib_node *fib_node,
2438 struct mlxsw_sp_fib_entry *fib_entry)
2439{
2440 if (!mlxsw_sp_fib_node_entry_is_first(fib_node, fib_entry))
2441 return;
2442
2443 /* Promote the next entry by overwriting the deleted entry */
2444 if (!list_is_singular(&fib_node->entry_list)) {
2445 struct mlxsw_sp_fib_entry *n = list_next_entry(fib_entry, list);
2446 enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_DELETE;
2447
2448 mlxsw_sp_fib_entry_update(mlxsw_sp, n);
2449 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, 0);
2450 return;
2451 }
2452
2453 mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry);
2454}
2455
2456static int mlxsw_sp_fib4_node_entry_link(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel4283bce2017-02-09 10:28:43 +01002457 struct mlxsw_sp_fib_entry *fib_entry,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002458 bool replace, bool append)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002459{
2460 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
2461 int err;
2462
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002463 err = mlxsw_sp_fib4_node_list_insert(fib_node, fib_entry, replace,
2464 append);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002465 if (err)
2466 return err;
2467
2468 err = mlxsw_sp_fib4_node_entry_add(mlxsw_sp, fib_node, fib_entry);
2469 if (err)
2470 goto err_fib4_node_entry_add;
2471
Ido Schimmel9aecce12017-02-09 10:28:42 +01002472 return 0;
2473
2474err_fib4_node_entry_add:
2475 mlxsw_sp_fib4_node_list_remove(fib_entry);
2476 return err;
2477}
2478
2479static void
2480mlxsw_sp_fib4_node_entry_unlink(struct mlxsw_sp *mlxsw_sp,
2481 struct mlxsw_sp_fib_entry *fib_entry)
2482{
2483 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
2484
Ido Schimmel9aecce12017-02-09 10:28:42 +01002485 mlxsw_sp_fib4_node_entry_del(mlxsw_sp, fib_node, fib_entry);
2486 mlxsw_sp_fib4_node_list_remove(fib_entry);
2487}
2488
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002489static void mlxsw_sp_fib4_entry_replace(struct mlxsw_sp *mlxsw_sp,
2490 struct mlxsw_sp_fib_entry *fib_entry,
2491 bool replace)
2492{
2493 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
2494 struct mlxsw_sp_fib_entry *replaced;
2495
2496 if (!replace)
2497 return;
2498
2499 /* We inserted the new entry before replaced one */
2500 replaced = list_next_entry(fib_entry, list);
2501
2502 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, replaced);
2503 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, replaced);
2504 mlxsw_sp_fib4_node_put(mlxsw_sp, fib_node);
2505}
2506
Ido Schimmel9aecce12017-02-09 10:28:42 +01002507static int
2508mlxsw_sp_router_fib4_add(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel4283bce2017-02-09 10:28:43 +01002509 const struct fib_entry_notifier_info *fen_info,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002510 bool replace, bool append)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002511{
2512 struct mlxsw_sp_fib_entry *fib_entry;
2513 struct mlxsw_sp_fib_node *fib_node;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002514 int err;
2515
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002516 if (mlxsw_sp->router.aborted)
2517 return 0;
2518
Ido Schimmel9aecce12017-02-09 10:28:42 +01002519 fib_node = mlxsw_sp_fib4_node_get(mlxsw_sp, fen_info);
2520 if (IS_ERR(fib_node)) {
2521 dev_warn(mlxsw_sp->bus_info->dev, "Failed to get FIB node\n");
2522 return PTR_ERR(fib_node);
2523 }
2524
2525 fib_entry = mlxsw_sp_fib4_entry_create(mlxsw_sp, fib_node, fen_info);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002526 if (IS_ERR(fib_entry)) {
Ido Schimmel9aecce12017-02-09 10:28:42 +01002527 dev_warn(mlxsw_sp->bus_info->dev, "Failed to create FIB entry\n");
2528 err = PTR_ERR(fib_entry);
2529 goto err_fib4_entry_create;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002530 }
Jiri Pirko61c503f2016-07-04 08:23:11 +02002531
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002532 err = mlxsw_sp_fib4_node_entry_link(mlxsw_sp, fib_entry, replace,
2533 append);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002534 if (err) {
Ido Schimmel9aecce12017-02-09 10:28:42 +01002535 dev_warn(mlxsw_sp->bus_info->dev, "Failed to link FIB entry to node\n");
2536 goto err_fib4_node_entry_link;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002537 }
Ido Schimmel9aecce12017-02-09 10:28:42 +01002538
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002539 mlxsw_sp_fib4_entry_replace(mlxsw_sp, fib_entry, replace);
2540
Jiri Pirko61c503f2016-07-04 08:23:11 +02002541 return 0;
2542
Ido Schimmel9aecce12017-02-09 10:28:42 +01002543err_fib4_node_entry_link:
2544 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib_entry);
2545err_fib4_entry_create:
2546 mlxsw_sp_fib4_node_put(mlxsw_sp, fib_node);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002547 return err;
2548}
2549
Jiri Pirko37956d72016-10-20 16:05:43 +02002550static void mlxsw_sp_router_fib4_del(struct mlxsw_sp *mlxsw_sp,
2551 struct fib_entry_notifier_info *fen_info)
Jiri Pirko61c503f2016-07-04 08:23:11 +02002552{
Jiri Pirko61c503f2016-07-04 08:23:11 +02002553 struct mlxsw_sp_fib_entry *fib_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002554 struct mlxsw_sp_fib_node *fib_node;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002555
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002556 if (mlxsw_sp->router.aborted)
Jiri Pirko37956d72016-10-20 16:05:43 +02002557 return;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002558
Ido Schimmel9aecce12017-02-09 10:28:42 +01002559 fib_entry = mlxsw_sp_fib4_entry_lookup(mlxsw_sp, fen_info);
2560 if (WARN_ON(!fib_entry))
Jiri Pirko37956d72016-10-20 16:05:43 +02002561 return;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002562 fib_node = fib_entry->fib_node;
Jiri Pirko5b004412016-09-01 10:37:40 +02002563
Ido Schimmel9aecce12017-02-09 10:28:42 +01002564 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, fib_entry);
2565 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib_entry);
2566 mlxsw_sp_fib4_node_put(mlxsw_sp, fib_node);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002567}
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002568
2569static int mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp)
2570{
2571 char ralta_pl[MLXSW_REG_RALTA_LEN];
2572 char ralst_pl[MLXSW_REG_RALST_LEN];
Ido Schimmelb5d90e62017-03-10 08:53:43 +01002573 int i, err;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002574
2575 mlxsw_reg_ralta_pack(ralta_pl, true, MLXSW_REG_RALXX_PROTOCOL_IPV4,
2576 MLXSW_SP_LPM_TREE_MIN);
2577 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
2578 if (err)
2579 return err;
2580
2581 mlxsw_reg_ralst_pack(ralst_pl, 0xff, MLXSW_SP_LPM_TREE_MIN);
2582 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
2583 if (err)
2584 return err;
2585
Ido Schimmelb5d90e62017-03-10 08:53:43 +01002586 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
2587 struct mlxsw_sp_vr *vr = &mlxsw_sp->router.vrs[i];
2588 char raltb_pl[MLXSW_REG_RALTB_LEN];
2589 char ralue_pl[MLXSW_REG_RALUE_LEN];
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002590
Ido Schimmelb5d90e62017-03-10 08:53:43 +01002591 if (!mlxsw_sp_vr_is_used(vr))
2592 continue;
2593
2594 mlxsw_reg_raltb_pack(raltb_pl, vr->id,
2595 MLXSW_REG_RALXX_PROTOCOL_IPV4,
2596 MLXSW_SP_LPM_TREE_MIN);
2597 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb),
2598 raltb_pl);
2599 if (err)
2600 return err;
2601
2602 mlxsw_reg_ralue_pack4(ralue_pl, MLXSW_SP_L3_PROTO_IPV4,
2603 MLXSW_REG_RALUE_OP_WRITE_WRITE, vr->id, 0,
2604 0);
2605 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
2606 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue),
2607 ralue_pl);
2608 if (err)
2609 return err;
2610 }
2611
2612 return 0;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002613}
2614
Ido Schimmel9aecce12017-02-09 10:28:42 +01002615static void mlxsw_sp_fib4_node_flush(struct mlxsw_sp *mlxsw_sp,
2616 struct mlxsw_sp_fib_node *fib_node)
2617{
2618 struct mlxsw_sp_fib_entry *fib_entry, *tmp;
2619
2620 list_for_each_entry_safe(fib_entry, tmp, &fib_node->entry_list, list) {
2621 bool do_break = &tmp->list == &fib_node->entry_list;
2622
2623 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, fib_entry);
2624 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib_entry);
2625 mlxsw_sp_fib4_node_put(mlxsw_sp, fib_node);
2626 /* Break when entry list is empty and node was freed.
2627 * Otherwise, we'll access freed memory in the next
2628 * iteration.
2629 */
2630 if (do_break)
2631 break;
2632 }
2633}
2634
2635static void mlxsw_sp_fib_node_flush(struct mlxsw_sp *mlxsw_sp,
2636 struct mlxsw_sp_fib_node *fib_node)
2637{
Ido Schimmel76610eb2017-03-10 08:53:41 +01002638 switch (fib_node->fib->proto) {
Ido Schimmel9aecce12017-02-09 10:28:42 +01002639 case MLXSW_SP_L3_PROTO_IPV4:
2640 mlxsw_sp_fib4_node_flush(mlxsw_sp, fib_node);
2641 break;
2642 case MLXSW_SP_L3_PROTO_IPV6:
2643 WARN_ON_ONCE(1);
2644 break;
2645 }
2646}
2647
Ido Schimmel76610eb2017-03-10 08:53:41 +01002648static void mlxsw_sp_vr_fib_flush(struct mlxsw_sp *mlxsw_sp,
2649 struct mlxsw_sp_vr *vr,
2650 enum mlxsw_sp_l3proto proto)
2651{
2652 struct mlxsw_sp_fib *fib = mlxsw_sp_vr_fib(vr, proto);
2653 struct mlxsw_sp_fib_node *fib_node, *tmp;
2654
2655 list_for_each_entry_safe(fib_node, tmp, &fib->node_list, list) {
2656 bool do_break = &tmp->list == &fib->node_list;
2657
2658 mlxsw_sp_fib_node_flush(mlxsw_sp, fib_node);
2659 if (do_break)
2660 break;
2661 }
2662}
2663
Ido Schimmelac571de2016-11-14 11:26:32 +01002664static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002665{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002666 int i;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002667
Jiri Pirkoc1a38312016-10-21 16:07:23 +02002668 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Ido Schimmel76610eb2017-03-10 08:53:41 +01002669 struct mlxsw_sp_vr *vr = &mlxsw_sp->router.vrs[i];
Ido Schimmelac571de2016-11-14 11:26:32 +01002670
Ido Schimmel76610eb2017-03-10 08:53:41 +01002671 if (!mlxsw_sp_vr_is_used(vr))
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002672 continue;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002673 mlxsw_sp_vr_fib_flush(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV4);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002674 }
Ido Schimmelac571de2016-11-14 11:26:32 +01002675}
2676
2677static void mlxsw_sp_router_fib4_abort(struct mlxsw_sp *mlxsw_sp)
2678{
2679 int err;
2680
Ido Schimmeld331d302016-11-16 09:51:58 +01002681 if (mlxsw_sp->router.aborted)
2682 return;
2683 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 +01002684 mlxsw_sp_router_fib_flush(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002685 mlxsw_sp->router.aborted = true;
2686 err = mlxsw_sp_router_set_abort_trap(mlxsw_sp);
2687 if (err)
2688 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set abort trap.\n");
2689}
2690
Ido Schimmel30572242016-12-03 16:45:01 +01002691struct mlxsw_sp_fib_event_work {
Ido Schimmela0e47612017-02-06 16:20:10 +01002692 struct work_struct work;
Ido Schimmelad178c82017-02-08 11:16:40 +01002693 union {
2694 struct fib_entry_notifier_info fen_info;
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01002695 struct fib_rule_notifier_info fr_info;
Ido Schimmelad178c82017-02-08 11:16:40 +01002696 struct fib_nh_notifier_info fnh_info;
2697 };
Ido Schimmel30572242016-12-03 16:45:01 +01002698 struct mlxsw_sp *mlxsw_sp;
2699 unsigned long event;
2700};
2701
2702static void mlxsw_sp_router_fib_event_work(struct work_struct *work)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002703{
Ido Schimmel30572242016-12-03 16:45:01 +01002704 struct mlxsw_sp_fib_event_work *fib_work =
Ido Schimmela0e47612017-02-06 16:20:10 +01002705 container_of(work, struct mlxsw_sp_fib_event_work, work);
Ido Schimmel30572242016-12-03 16:45:01 +01002706 struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp;
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01002707 struct fib_rule *rule;
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002708 bool replace, append;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002709 int err;
2710
Ido Schimmel30572242016-12-03 16:45:01 +01002711 /* Protect internal structures from changes */
2712 rtnl_lock();
2713 switch (fib_work->event) {
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002714 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
Ido Schimmel4283bce2017-02-09 10:28:43 +01002715 case FIB_EVENT_ENTRY_APPEND: /* fall through */
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002716 case FIB_EVENT_ENTRY_ADD:
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002717 replace = fib_work->event == FIB_EVENT_ENTRY_REPLACE;
Ido Schimmel4283bce2017-02-09 10:28:43 +01002718 append = fib_work->event == FIB_EVENT_ENTRY_APPEND;
2719 err = mlxsw_sp_router_fib4_add(mlxsw_sp, &fib_work->fen_info,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002720 replace, append);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002721 if (err)
2722 mlxsw_sp_router_fib4_abort(mlxsw_sp);
Ido Schimmel30572242016-12-03 16:45:01 +01002723 fib_info_put(fib_work->fen_info.fi);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002724 break;
2725 case FIB_EVENT_ENTRY_DEL:
Ido Schimmel30572242016-12-03 16:45:01 +01002726 mlxsw_sp_router_fib4_del(mlxsw_sp, &fib_work->fen_info);
2727 fib_info_put(fib_work->fen_info.fi);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002728 break;
2729 case FIB_EVENT_RULE_ADD: /* fall through */
2730 case FIB_EVENT_RULE_DEL:
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01002731 rule = fib_work->fr_info.rule;
Ido Schimmelc7f6e662017-03-16 09:08:20 +01002732 if (!fib4_rule_default(rule) && !rule->l3mdev)
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01002733 mlxsw_sp_router_fib4_abort(mlxsw_sp);
2734 fib_rule_put(rule);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002735 break;
Ido Schimmelad178c82017-02-08 11:16:40 +01002736 case FIB_EVENT_NH_ADD: /* fall through */
2737 case FIB_EVENT_NH_DEL:
2738 mlxsw_sp_nexthop_event(mlxsw_sp, fib_work->event,
2739 fib_work->fnh_info.fib_nh);
2740 fib_info_put(fib_work->fnh_info.fib_nh->nh_parent);
2741 break;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002742 }
Ido Schimmel30572242016-12-03 16:45:01 +01002743 rtnl_unlock();
2744 kfree(fib_work);
2745}
2746
2747/* Called with rcu_read_lock() */
2748static int mlxsw_sp_router_fib_event(struct notifier_block *nb,
2749 unsigned long event, void *ptr)
2750{
2751 struct mlxsw_sp *mlxsw_sp = container_of(nb, struct mlxsw_sp, fib_nb);
2752 struct mlxsw_sp_fib_event_work *fib_work;
2753 struct fib_notifier_info *info = ptr;
2754
2755 if (!net_eq(info->net, &init_net))
2756 return NOTIFY_DONE;
2757
2758 fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
2759 if (WARN_ON(!fib_work))
2760 return NOTIFY_BAD;
2761
Ido Schimmela0e47612017-02-06 16:20:10 +01002762 INIT_WORK(&fib_work->work, mlxsw_sp_router_fib_event_work);
Ido Schimmel30572242016-12-03 16:45:01 +01002763 fib_work->mlxsw_sp = mlxsw_sp;
2764 fib_work->event = event;
2765
2766 switch (event) {
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002767 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
Ido Schimmel4283bce2017-02-09 10:28:43 +01002768 case FIB_EVENT_ENTRY_APPEND: /* fall through */
Ido Schimmel30572242016-12-03 16:45:01 +01002769 case FIB_EVENT_ENTRY_ADD: /* fall through */
2770 case FIB_EVENT_ENTRY_DEL:
2771 memcpy(&fib_work->fen_info, ptr, sizeof(fib_work->fen_info));
2772 /* Take referece on fib_info to prevent it from being
2773 * freed while work is queued. Release it afterwards.
2774 */
2775 fib_info_hold(fib_work->fen_info.fi);
2776 break;
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01002777 case FIB_EVENT_RULE_ADD: /* fall through */
2778 case FIB_EVENT_RULE_DEL:
2779 memcpy(&fib_work->fr_info, ptr, sizeof(fib_work->fr_info));
2780 fib_rule_get(fib_work->fr_info.rule);
2781 break;
Ido Schimmelad178c82017-02-08 11:16:40 +01002782 case FIB_EVENT_NH_ADD: /* fall through */
2783 case FIB_EVENT_NH_DEL:
2784 memcpy(&fib_work->fnh_info, ptr, sizeof(fib_work->fnh_info));
2785 fib_info_hold(fib_work->fnh_info.fib_nh->nh_parent);
2786 break;
Ido Schimmel30572242016-12-03 16:45:01 +01002787 }
2788
Ido Schimmela0e47612017-02-06 16:20:10 +01002789 mlxsw_core_schedule_work(&fib_work->work);
Ido Schimmel30572242016-12-03 16:45:01 +01002790
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002791 return NOTIFY_DONE;
2792}
2793
Ido Schimmel4724ba562017-03-10 08:53:39 +01002794static struct mlxsw_sp_rif *
2795mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
2796 const struct net_device *dev)
2797{
2798 int i;
2799
2800 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
2801 if (mlxsw_sp->rifs[i] && mlxsw_sp->rifs[i]->dev == dev)
2802 return mlxsw_sp->rifs[i];
2803
2804 return NULL;
2805}
2806
2807static int mlxsw_sp_router_rif_disable(struct mlxsw_sp *mlxsw_sp, u16 rif)
2808{
2809 char ritr_pl[MLXSW_REG_RITR_LEN];
2810 int err;
2811
2812 mlxsw_reg_ritr_rif_pack(ritr_pl, rif);
2813 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
2814 if (WARN_ON_ONCE(err))
2815 return err;
2816
2817 mlxsw_reg_ritr_enable_set(ritr_pl, false);
2818 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
2819}
2820
2821static void mlxsw_sp_router_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002822 struct mlxsw_sp_rif *rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002823{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002824 mlxsw_sp_router_rif_disable(mlxsw_sp, rif->rif_index);
2825 mlxsw_sp_nexthop_rif_gone_sync(mlxsw_sp, rif);
2826 mlxsw_sp_neigh_rif_gone_sync(mlxsw_sp, rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002827}
2828
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002829static bool mlxsw_sp_rif_should_config(struct mlxsw_sp_rif *rif,
Ido Schimmel4724ba562017-03-10 08:53:39 +01002830 const struct in_device *in_dev,
2831 unsigned long event)
2832{
2833 switch (event) {
2834 case NETDEV_UP:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002835 if (!rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002836 return true;
2837 return false;
2838 case NETDEV_DOWN:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002839 if (rif && !in_dev->ifa_list &&
2840 !netif_is_l3_slave(rif->dev))
Ido Schimmel4724ba562017-03-10 08:53:39 +01002841 return true;
2842 /* It is possible we already removed the RIF ourselves
2843 * if it was assigned to a netdev that is now a bridge
2844 * or LAG slave.
2845 */
2846 return false;
2847 }
2848
2849 return false;
2850}
2851
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002852#define MLXSW_SP_INVALID_INDEX_RIF 0xffff
Ido Schimmel4724ba562017-03-10 08:53:39 +01002853static int mlxsw_sp_avail_rif_get(struct mlxsw_sp *mlxsw_sp)
2854{
2855 int i;
2856
2857 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
2858 if (!mlxsw_sp->rifs[i])
2859 return i;
2860
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002861 return MLXSW_SP_INVALID_INDEX_RIF;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002862}
2863
2864static void mlxsw_sp_vport_rif_sp_attr_get(struct mlxsw_sp_port *mlxsw_sp_vport,
2865 bool *p_lagged, u16 *p_system_port)
2866{
2867 u8 local_port = mlxsw_sp_vport->local_port;
2868
2869 *p_lagged = mlxsw_sp_vport->lagged;
2870 *p_system_port = *p_lagged ? mlxsw_sp_vport->lag_id : local_port;
2871}
2872
2873static int mlxsw_sp_vport_rif_sp_op(struct mlxsw_sp_port *mlxsw_sp_vport,
Ido Schimmel69132292017-03-10 08:53:42 +01002874 u16 vr_id, struct net_device *l3_dev,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002875 u16 rif_index, bool create)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002876{
2877 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
2878 bool lagged = mlxsw_sp_vport->lagged;
2879 char ritr_pl[MLXSW_REG_RITR_LEN];
2880 u16 system_port;
2881
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002882 mlxsw_reg_ritr_pack(ritr_pl, create, MLXSW_REG_RITR_SP_IF, rif_index,
2883 vr_id, l3_dev->mtu, l3_dev->dev_addr);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002884
2885 mlxsw_sp_vport_rif_sp_attr_get(mlxsw_sp_vport, &lagged, &system_port);
2886 mlxsw_reg_ritr_sp_if_pack(ritr_pl, lagged, system_port,
2887 mlxsw_sp_vport_vid_get(mlxsw_sp_vport));
2888
2889 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
2890}
2891
2892static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port *mlxsw_sp_vport);
2893
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002894static u16 mlxsw_sp_rif_sp_to_fid(u16 rif_index)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002895{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002896 return MLXSW_SP_RFID_BASE + rif_index;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002897}
2898
2899static struct mlxsw_sp_fid *
2900mlxsw_sp_rfid_alloc(u16 fid, struct net_device *l3_dev)
2901{
2902 struct mlxsw_sp_fid *f;
2903
2904 f = kzalloc(sizeof(*f), GFP_KERNEL);
2905 if (!f)
2906 return NULL;
2907
2908 f->leave = mlxsw_sp_vport_rif_sp_leave;
2909 f->ref_count = 0;
2910 f->dev = l3_dev;
2911 f->fid = fid;
2912
2913 return f;
2914}
2915
2916static struct mlxsw_sp_rif *
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002917mlxsw_sp_rif_alloc(u16 rif_index, u16 vr_id, struct net_device *l3_dev,
Ido Schimmel69132292017-03-10 08:53:42 +01002918 struct mlxsw_sp_fid *f)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002919{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002920 struct mlxsw_sp_rif *rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002921
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002922 rif = kzalloc(sizeof(*rif), GFP_KERNEL);
2923 if (!rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002924 return NULL;
2925
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002926 INIT_LIST_HEAD(&rif->nexthop_list);
2927 INIT_LIST_HEAD(&rif->neigh_list);
2928 ether_addr_copy(rif->addr, l3_dev->dev_addr);
2929 rif->mtu = l3_dev->mtu;
2930 rif->vr_id = vr_id;
2931 rif->dev = l3_dev;
2932 rif->rif_index = rif_index;
2933 rif->f = f;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002934
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002935 return rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002936}
2937
Arkadi Sharshevskyfd1b9d42017-03-28 17:24:16 +02002938u16 mlxsw_sp_rif_index(const struct mlxsw_sp_rif *rif)
2939{
2940 return rif->rif_index;
2941}
2942
2943int mlxsw_sp_rif_dev_ifindex(const struct mlxsw_sp_rif *rif)
2944{
2945 return rif->dev->ifindex;
2946}
2947
Ido Schimmel4724ba562017-03-10 08:53:39 +01002948static struct mlxsw_sp_rif *
2949mlxsw_sp_vport_rif_sp_create(struct mlxsw_sp_port *mlxsw_sp_vport,
2950 struct net_device *l3_dev)
2951{
2952 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
Ido Schimmel57837882017-03-16 09:08:16 +01002953 u32 tb_id = l3mdev_fib_table(l3_dev);
Ido Schimmel69132292017-03-10 08:53:42 +01002954 struct mlxsw_sp_vr *vr;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002955 struct mlxsw_sp_fid *f;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002956 struct mlxsw_sp_rif *rif;
2957 u16 fid, rif_index;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002958 int err;
2959
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002960 rif_index = mlxsw_sp_avail_rif_get(mlxsw_sp);
2961 if (rif_index == MLXSW_SP_INVALID_INDEX_RIF)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002962 return ERR_PTR(-ERANGE);
2963
Ido Schimmel57837882017-03-16 09:08:16 +01002964 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id ? : RT_TABLE_MAIN);
Ido Schimmel69132292017-03-10 08:53:42 +01002965 if (IS_ERR(vr))
2966 return ERR_CAST(vr);
2967
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002968 err = mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, vr->id, l3_dev,
2969 rif_index, true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002970 if (err)
Ido Schimmel69132292017-03-10 08:53:42 +01002971 goto err_vport_rif_sp_op;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002972
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002973 fid = mlxsw_sp_rif_sp_to_fid(rif_index);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002974 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, true);
2975 if (err)
2976 goto err_rif_fdb_op;
2977
2978 f = mlxsw_sp_rfid_alloc(fid, l3_dev);
2979 if (!f) {
2980 err = -ENOMEM;
2981 goto err_rfid_alloc;
2982 }
2983
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002984 rif = mlxsw_sp_rif_alloc(rif_index, vr->id, l3_dev, f);
2985 if (!rif) {
Ido Schimmel4724ba562017-03-10 08:53:39 +01002986 err = -ENOMEM;
2987 goto err_rif_alloc;
2988 }
2989
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +02002990 if (devlink_dpipe_table_counter_enabled(priv_to_devlink(mlxsw_sp->core),
2991 MLXSW_SP_DPIPE_TABLE_NAME_ERIF)) {
2992 err = mlxsw_sp_rif_counter_alloc(mlxsw_sp, rif,
2993 MLXSW_SP_RIF_COUNTER_EGRESS);
2994 if (err)
2995 netdev_dbg(mlxsw_sp_vport->dev,
2996 "Counter alloc Failed err=%d\n", err);
2997 }
2998
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002999 f->rif = rif;
3000 mlxsw_sp->rifs[rif_index] = rif;
Ido Schimmel69132292017-03-10 08:53:42 +01003001 vr->rif_count++;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003002
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003003 return rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003004
3005err_rif_alloc:
3006 kfree(f);
3007err_rfid_alloc:
3008 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, false);
3009err_rif_fdb_op:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003010 mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, vr->id, l3_dev, rif_index,
3011 false);
Ido Schimmel69132292017-03-10 08:53:42 +01003012err_vport_rif_sp_op:
3013 mlxsw_sp_vr_put(vr);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003014 return ERR_PTR(err);
3015}
3016
3017static void mlxsw_sp_vport_rif_sp_destroy(struct mlxsw_sp_port *mlxsw_sp_vport,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003018 struct mlxsw_sp_rif *rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003019{
3020 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003021 struct mlxsw_sp_vr *vr = &mlxsw_sp->router.vrs[rif->vr_id];
3022 struct net_device *l3_dev = rif->dev;
3023 struct mlxsw_sp_fid *f = rif->f;
3024 u16 rif_index = rif->rif_index;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003025 u16 fid = f->fid;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003026
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003027 mlxsw_sp_router_rif_gone_sync(mlxsw_sp, rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003028
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +02003029 mlxsw_sp_rif_counter_free(mlxsw_sp, rif, MLXSW_SP_RIF_COUNTER_EGRESS);
3030 mlxsw_sp_rif_counter_free(mlxsw_sp, rif, MLXSW_SP_RIF_COUNTER_INGRESS);
3031
Ido Schimmel69132292017-03-10 08:53:42 +01003032 vr->rif_count--;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003033 mlxsw_sp->rifs[rif_index] = NULL;
3034 f->rif = NULL;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003035
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003036 kfree(rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003037
3038 kfree(f);
3039
3040 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, false);
3041
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003042 mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, vr->id, l3_dev, rif_index,
3043 false);
Ido Schimmel69132292017-03-10 08:53:42 +01003044 mlxsw_sp_vr_put(vr);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003045}
3046
3047static int mlxsw_sp_vport_rif_sp_join(struct mlxsw_sp_port *mlxsw_sp_vport,
3048 struct net_device *l3_dev)
3049{
3050 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003051 struct mlxsw_sp_rif *rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003052
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003053 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
3054 if (!rif) {
3055 rif = mlxsw_sp_vport_rif_sp_create(mlxsw_sp_vport, l3_dev);
3056 if (IS_ERR(rif))
3057 return PTR_ERR(rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003058 }
3059
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003060 mlxsw_sp_vport_fid_set(mlxsw_sp_vport, rif->f);
3061 rif->f->ref_count++;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003062
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003063 netdev_dbg(mlxsw_sp_vport->dev, "Joined FID=%d\n", rif->f->fid);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003064
3065 return 0;
3066}
3067
3068static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port *mlxsw_sp_vport)
3069{
3070 struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
3071
3072 netdev_dbg(mlxsw_sp_vport->dev, "Left FID=%d\n", f->fid);
3073
3074 mlxsw_sp_vport_fid_set(mlxsw_sp_vport, NULL);
3075 if (--f->ref_count == 0)
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003076 mlxsw_sp_vport_rif_sp_destroy(mlxsw_sp_vport, f->rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003077}
3078
3079static int mlxsw_sp_inetaddr_vport_event(struct net_device *l3_dev,
3080 struct net_device *port_dev,
3081 unsigned long event, u16 vid)
3082{
3083 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(port_dev);
3084 struct mlxsw_sp_port *mlxsw_sp_vport;
3085
3086 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
3087 if (WARN_ON(!mlxsw_sp_vport))
3088 return -EINVAL;
3089
3090 switch (event) {
3091 case NETDEV_UP:
3092 return mlxsw_sp_vport_rif_sp_join(mlxsw_sp_vport, l3_dev);
3093 case NETDEV_DOWN:
3094 mlxsw_sp_vport_rif_sp_leave(mlxsw_sp_vport);
3095 break;
3096 }
3097
3098 return 0;
3099}
3100
3101static int mlxsw_sp_inetaddr_port_event(struct net_device *port_dev,
3102 unsigned long event)
3103{
Jiri Pirko2b94e582017-04-18 16:55:37 +02003104 if (netif_is_bridge_port(port_dev) ||
3105 netif_is_lag_port(port_dev) ||
3106 netif_is_ovs_port(port_dev))
Ido Schimmel4724ba562017-03-10 08:53:39 +01003107 return 0;
3108
3109 return mlxsw_sp_inetaddr_vport_event(port_dev, port_dev, event, 1);
3110}
3111
3112static int __mlxsw_sp_inetaddr_lag_event(struct net_device *l3_dev,
3113 struct net_device *lag_dev,
3114 unsigned long event, u16 vid)
3115{
3116 struct net_device *port_dev;
3117 struct list_head *iter;
3118 int err;
3119
3120 netdev_for_each_lower_dev(lag_dev, port_dev, iter) {
3121 if (mlxsw_sp_port_dev_check(port_dev)) {
3122 err = mlxsw_sp_inetaddr_vport_event(l3_dev, port_dev,
3123 event, vid);
3124 if (err)
3125 return err;
3126 }
3127 }
3128
3129 return 0;
3130}
3131
3132static int mlxsw_sp_inetaddr_lag_event(struct net_device *lag_dev,
3133 unsigned long event)
3134{
3135 if (netif_is_bridge_port(lag_dev))
3136 return 0;
3137
3138 return __mlxsw_sp_inetaddr_lag_event(lag_dev, lag_dev, event, 1);
3139}
3140
3141static struct mlxsw_sp_fid *mlxsw_sp_bridge_fid_get(struct mlxsw_sp *mlxsw_sp,
3142 struct net_device *l3_dev)
3143{
3144 u16 fid;
3145
3146 if (is_vlan_dev(l3_dev))
3147 fid = vlan_dev_vlan_id(l3_dev);
3148 else if (mlxsw_sp->master_bridge.dev == l3_dev)
3149 fid = 1;
3150 else
3151 return mlxsw_sp_vfid_find(mlxsw_sp, l3_dev);
3152
3153 return mlxsw_sp_fid_find(mlxsw_sp, fid);
3154}
3155
Ido Schimmel5ec2ee72017-03-24 08:02:48 +01003156static u8 mlxsw_sp_router_port(const struct mlxsw_sp *mlxsw_sp)
3157{
3158 return mlxsw_core_max_ports(mlxsw_sp->core) + 1;
3159}
3160
Ido Schimmel4724ba562017-03-10 08:53:39 +01003161static enum mlxsw_flood_table_type mlxsw_sp_flood_table_type_get(u16 fid)
3162{
3163 return mlxsw_sp_fid_is_vfid(fid) ? MLXSW_REG_SFGC_TABLE_TYPE_FID :
3164 MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST;
3165}
3166
3167static u16 mlxsw_sp_flood_table_index_get(u16 fid)
3168{
3169 return mlxsw_sp_fid_is_vfid(fid) ? mlxsw_sp_fid_to_vfid(fid) : fid;
3170}
3171
3172static int mlxsw_sp_router_port_flood_set(struct mlxsw_sp *mlxsw_sp, u16 fid,
3173 bool set)
3174{
Ido Schimmel5ec2ee72017-03-24 08:02:48 +01003175 u8 router_port = mlxsw_sp_router_port(mlxsw_sp);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003176 enum mlxsw_flood_table_type table_type;
3177 char *sftr_pl;
3178 u16 index;
3179 int err;
3180
3181 sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL);
3182 if (!sftr_pl)
3183 return -ENOMEM;
3184
3185 table_type = mlxsw_sp_flood_table_type_get(fid);
3186 index = mlxsw_sp_flood_table_index_get(fid);
3187 mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_BC, index, table_type,
Ido Schimmel5ec2ee72017-03-24 08:02:48 +01003188 1, router_port, set);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003189 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
3190
3191 kfree(sftr_pl);
3192 return err;
3193}
3194
3195static enum mlxsw_reg_ritr_if_type mlxsw_sp_rif_type_get(u16 fid)
3196{
3197 if (mlxsw_sp_fid_is_vfid(fid))
3198 return MLXSW_REG_RITR_FID_IF;
3199 else
3200 return MLXSW_REG_RITR_VLAN_IF;
3201}
3202
Ido Schimmel69132292017-03-10 08:53:42 +01003203static int mlxsw_sp_rif_bridge_op(struct mlxsw_sp *mlxsw_sp, u16 vr_id,
Ido Schimmel4724ba562017-03-10 08:53:39 +01003204 struct net_device *l3_dev,
3205 u16 fid, u16 rif,
3206 bool create)
3207{
3208 enum mlxsw_reg_ritr_if_type rif_type;
3209 char ritr_pl[MLXSW_REG_RITR_LEN];
3210
3211 rif_type = mlxsw_sp_rif_type_get(fid);
Ido Schimmel69132292017-03-10 08:53:42 +01003212 mlxsw_reg_ritr_pack(ritr_pl, create, rif_type, rif, vr_id, l3_dev->mtu,
Ido Schimmel4724ba562017-03-10 08:53:39 +01003213 l3_dev->dev_addr);
3214 mlxsw_reg_ritr_fid_set(ritr_pl, rif_type, fid);
3215
3216 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3217}
3218
3219static int mlxsw_sp_rif_bridge_create(struct mlxsw_sp *mlxsw_sp,
3220 struct net_device *l3_dev,
3221 struct mlxsw_sp_fid *f)
3222{
Ido Schimmel57837882017-03-16 09:08:16 +01003223 u32 tb_id = l3mdev_fib_table(l3_dev);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003224 struct mlxsw_sp_rif *rif;
Ido Schimmel69132292017-03-10 08:53:42 +01003225 struct mlxsw_sp_vr *vr;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003226 u16 rif_index;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003227 int err;
3228
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003229 rif_index = mlxsw_sp_avail_rif_get(mlxsw_sp);
3230 if (rif_index == MLXSW_SP_INVALID_INDEX_RIF)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003231 return -ERANGE;
3232
Ido Schimmel57837882017-03-16 09:08:16 +01003233 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id ? : RT_TABLE_MAIN);
Ido Schimmel69132292017-03-10 08:53:42 +01003234 if (IS_ERR(vr))
3235 return PTR_ERR(vr);
3236
Ido Schimmel4724ba562017-03-10 08:53:39 +01003237 err = mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, true);
3238 if (err)
Ido Schimmel69132292017-03-10 08:53:42 +01003239 goto err_port_flood_set;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003240
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003241 err = mlxsw_sp_rif_bridge_op(mlxsw_sp, vr->id, l3_dev, f->fid,
3242 rif_index, true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003243 if (err)
3244 goto err_rif_bridge_op;
3245
3246 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, true);
3247 if (err)
3248 goto err_rif_fdb_op;
3249
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003250 rif = mlxsw_sp_rif_alloc(rif_index, vr->id, l3_dev, f);
3251 if (!rif) {
Ido Schimmel4724ba562017-03-10 08:53:39 +01003252 err = -ENOMEM;
3253 goto err_rif_alloc;
3254 }
3255
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003256 f->rif = rif;
3257 mlxsw_sp->rifs[rif_index] = rif;
Ido Schimmel69132292017-03-10 08:53:42 +01003258 vr->rif_count++;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003259
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003260 netdev_dbg(l3_dev, "RIF=%d created\n", rif_index);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003261
3262 return 0;
3263
3264err_rif_alloc:
3265 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, false);
3266err_rif_fdb_op:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003267 mlxsw_sp_rif_bridge_op(mlxsw_sp, vr->id, l3_dev, f->fid, rif_index,
3268 false);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003269err_rif_bridge_op:
3270 mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, false);
Ido Schimmel69132292017-03-10 08:53:42 +01003271err_port_flood_set:
3272 mlxsw_sp_vr_put(vr);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003273 return err;
3274}
3275
3276void mlxsw_sp_rif_bridge_destroy(struct mlxsw_sp *mlxsw_sp,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003277 struct mlxsw_sp_rif *rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003278{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003279 struct mlxsw_sp_vr *vr = &mlxsw_sp->router.vrs[rif->vr_id];
3280 struct net_device *l3_dev = rif->dev;
3281 struct mlxsw_sp_fid *f = rif->f;
3282 u16 rif_index = rif->rif_index;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003283
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003284 mlxsw_sp_router_rif_gone_sync(mlxsw_sp, rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003285
Ido Schimmel69132292017-03-10 08:53:42 +01003286 vr->rif_count--;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003287 mlxsw_sp->rifs[rif_index] = NULL;
3288 f->rif = NULL;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003289
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003290 kfree(rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003291
3292 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, false);
3293
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003294 mlxsw_sp_rif_bridge_op(mlxsw_sp, vr->id, l3_dev, f->fid, rif_index,
3295 false);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003296
3297 mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, false);
3298
Ido Schimmel69132292017-03-10 08:53:42 +01003299 mlxsw_sp_vr_put(vr);
3300
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003301 netdev_dbg(l3_dev, "RIF=%d destroyed\n", rif_index);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003302}
3303
3304static int mlxsw_sp_inetaddr_bridge_event(struct net_device *l3_dev,
3305 struct net_device *br_dev,
3306 unsigned long event)
3307{
3308 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
3309 struct mlxsw_sp_fid *f;
3310
3311 /* FID can either be an actual FID if the L3 device is the
3312 * VLAN-aware bridge or a VLAN device on top. Otherwise, the
3313 * L3 device is a VLAN-unaware bridge and we get a vFID.
3314 */
3315 f = mlxsw_sp_bridge_fid_get(mlxsw_sp, l3_dev);
3316 if (WARN_ON(!f))
3317 return -EINVAL;
3318
3319 switch (event) {
3320 case NETDEV_UP:
3321 return mlxsw_sp_rif_bridge_create(mlxsw_sp, l3_dev, f);
3322 case NETDEV_DOWN:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003323 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003324 break;
3325 }
3326
3327 return 0;
3328}
3329
3330static int mlxsw_sp_inetaddr_vlan_event(struct net_device *vlan_dev,
3331 unsigned long event)
3332{
3333 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
3334 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(vlan_dev);
3335 u16 vid = vlan_dev_vlan_id(vlan_dev);
3336
Ido Schimmel6b27c8a2017-06-28 09:03:12 +03003337 if (netif_is_bridge_port(vlan_dev))
3338 return 0;
3339
Ido Schimmel4724ba562017-03-10 08:53:39 +01003340 if (mlxsw_sp_port_dev_check(real_dev))
3341 return mlxsw_sp_inetaddr_vport_event(vlan_dev, real_dev, event,
3342 vid);
3343 else if (netif_is_lag_master(real_dev))
3344 return __mlxsw_sp_inetaddr_lag_event(vlan_dev, real_dev, event,
3345 vid);
3346 else if (netif_is_bridge_master(real_dev) &&
3347 mlxsw_sp->master_bridge.dev == real_dev)
3348 return mlxsw_sp_inetaddr_bridge_event(vlan_dev, real_dev,
3349 event);
3350
3351 return 0;
3352}
3353
Ido Schimmelb1e45522017-04-30 19:47:14 +03003354static int __mlxsw_sp_inetaddr_event(struct net_device *dev,
3355 unsigned long event)
3356{
3357 if (mlxsw_sp_port_dev_check(dev))
3358 return mlxsw_sp_inetaddr_port_event(dev, event);
3359 else if (netif_is_lag_master(dev))
3360 return mlxsw_sp_inetaddr_lag_event(dev, event);
3361 else if (netif_is_bridge_master(dev))
3362 return mlxsw_sp_inetaddr_bridge_event(dev, dev, event);
3363 else if (is_vlan_dev(dev))
3364 return mlxsw_sp_inetaddr_vlan_event(dev, event);
3365 else
3366 return 0;
3367}
3368
Ido Schimmel4724ba562017-03-10 08:53:39 +01003369int mlxsw_sp_inetaddr_event(struct notifier_block *unused,
3370 unsigned long event, void *ptr)
3371{
3372 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
3373 struct net_device *dev = ifa->ifa_dev->dev;
3374 struct mlxsw_sp *mlxsw_sp;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003375 struct mlxsw_sp_rif *rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003376 int err = 0;
3377
3378 mlxsw_sp = mlxsw_sp_lower_get(dev);
3379 if (!mlxsw_sp)
3380 goto out;
3381
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003382 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3383 if (!mlxsw_sp_rif_should_config(rif, ifa->ifa_dev, event))
Ido Schimmel4724ba562017-03-10 08:53:39 +01003384 goto out;
3385
Ido Schimmelb1e45522017-04-30 19:47:14 +03003386 err = __mlxsw_sp_inetaddr_event(dev, event);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003387out:
3388 return notifier_from_errno(err);
3389}
3390
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003391static int mlxsw_sp_rif_edit(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
Ido Schimmel4724ba562017-03-10 08:53:39 +01003392 const char *mac, int mtu)
3393{
3394 char ritr_pl[MLXSW_REG_RITR_LEN];
3395 int err;
3396
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003397 mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003398 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3399 if (err)
3400 return err;
3401
3402 mlxsw_reg_ritr_mtu_set(ritr_pl, mtu);
3403 mlxsw_reg_ritr_if_mac_memcpy_to(ritr_pl, mac);
3404 mlxsw_reg_ritr_op_set(ritr_pl, MLXSW_REG_RITR_RIF_CREATE);
3405 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3406}
3407
3408int mlxsw_sp_netdevice_router_port_event(struct net_device *dev)
3409{
3410 struct mlxsw_sp *mlxsw_sp;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003411 struct mlxsw_sp_rif *rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003412 int err;
3413
3414 mlxsw_sp = mlxsw_sp_lower_get(dev);
3415 if (!mlxsw_sp)
3416 return 0;
3417
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003418 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3419 if (!rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003420 return 0;
3421
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003422 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, rif->f->fid, false);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003423 if (err)
3424 return err;
3425
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003426 err = mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, dev->dev_addr,
3427 dev->mtu);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003428 if (err)
3429 goto err_rif_edit;
3430
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003431 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, dev->dev_addr, rif->f->fid, true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003432 if (err)
3433 goto err_rif_fdb_op;
3434
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003435 ether_addr_copy(rif->addr, dev->dev_addr);
3436 rif->mtu = dev->mtu;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003437
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003438 netdev_dbg(dev, "Updated RIF=%d\n", rif->rif_index);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003439
3440 return 0;
3441
3442err_rif_fdb_op:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003443 mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, rif->addr, rif->mtu);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003444err_rif_edit:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003445 mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, rif->f->fid, true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003446 return err;
3447}
3448
Ido Schimmelb1e45522017-04-30 19:47:14 +03003449static int mlxsw_sp_port_vrf_join(struct mlxsw_sp *mlxsw_sp,
3450 struct net_device *l3_dev)
Ido Schimmel7179eb52017-03-16 09:08:18 +01003451{
Ido Schimmelb1e45522017-04-30 19:47:14 +03003452 struct mlxsw_sp_rif *rif;
Ido Schimmel7179eb52017-03-16 09:08:18 +01003453
Ido Schimmelb1e45522017-04-30 19:47:14 +03003454 /* If netdev is already associated with a RIF, then we need to
3455 * destroy it and create a new one with the new virtual router ID.
Ido Schimmel7179eb52017-03-16 09:08:18 +01003456 */
Ido Schimmelb1e45522017-04-30 19:47:14 +03003457 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
3458 if (rif)
3459 __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_DOWN);
Ido Schimmel7179eb52017-03-16 09:08:18 +01003460
Ido Schimmelb1e45522017-04-30 19:47:14 +03003461 return __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_UP);
Ido Schimmel7179eb52017-03-16 09:08:18 +01003462}
3463
Ido Schimmelb1e45522017-04-30 19:47:14 +03003464static void mlxsw_sp_port_vrf_leave(struct mlxsw_sp *mlxsw_sp,
3465 struct net_device *l3_dev)
Ido Schimmel7179eb52017-03-16 09:08:18 +01003466{
Ido Schimmelb1e45522017-04-30 19:47:14 +03003467 struct mlxsw_sp_rif *rif;
Ido Schimmel7179eb52017-03-16 09:08:18 +01003468
Ido Schimmelb1e45522017-04-30 19:47:14 +03003469 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
3470 if (!rif)
Ido Schimmel7179eb52017-03-16 09:08:18 +01003471 return;
Ido Schimmelb1e45522017-04-30 19:47:14 +03003472 __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_DOWN);
Ido Schimmel7179eb52017-03-16 09:08:18 +01003473}
3474
Ido Schimmelb1e45522017-04-30 19:47:14 +03003475int mlxsw_sp_netdevice_vrf_event(struct net_device *l3_dev, unsigned long event,
3476 struct netdev_notifier_changeupper_info *info)
Ido Schimmel3d70e4582017-03-16 09:08:19 +01003477{
Ido Schimmelb1e45522017-04-30 19:47:14 +03003478 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
3479 int err = 0;
Ido Schimmel3d70e4582017-03-16 09:08:19 +01003480
Ido Schimmelb1e45522017-04-30 19:47:14 +03003481 if (!mlxsw_sp)
3482 return 0;
Ido Schimmel3d70e4582017-03-16 09:08:19 +01003483
Ido Schimmelb1e45522017-04-30 19:47:14 +03003484 switch (event) {
3485 case NETDEV_PRECHANGEUPPER:
3486 return 0;
3487 case NETDEV_CHANGEUPPER:
3488 if (info->linking)
3489 err = mlxsw_sp_port_vrf_join(mlxsw_sp, l3_dev);
3490 else
3491 mlxsw_sp_port_vrf_leave(mlxsw_sp, l3_dev);
3492 break;
3493 }
Ido Schimmel3d70e4582017-03-16 09:08:19 +01003494
Ido Schimmelb1e45522017-04-30 19:47:14 +03003495 return err;
Ido Schimmel3d70e4582017-03-16 09:08:19 +01003496}
3497
Ido Schimmelc3852ef2016-12-03 16:45:07 +01003498static void mlxsw_sp_router_fib_dump_flush(struct notifier_block *nb)
3499{
3500 struct mlxsw_sp *mlxsw_sp = container_of(nb, struct mlxsw_sp, fib_nb);
3501
3502 /* Flush pending FIB notifications and then flush the device's
3503 * table before requesting another dump. The FIB notification
3504 * block is unregistered, so no need to take RTNL.
3505 */
3506 mlxsw_core_flush_owq();
3507 mlxsw_sp_router_fib_flush(mlxsw_sp);
3508}
3509
Ido Schimmel4724ba562017-03-10 08:53:39 +01003510static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
3511{
3512 char rgcr_pl[MLXSW_REG_RGCR_LEN];
3513 u64 max_rifs;
3514 int err;
3515
3516 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_RIFS))
3517 return -EIO;
3518
3519 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
3520 mlxsw_sp->rifs = kcalloc(max_rifs, sizeof(struct mlxsw_sp_rif *),
3521 GFP_KERNEL);
3522 if (!mlxsw_sp->rifs)
3523 return -ENOMEM;
3524
3525 mlxsw_reg_rgcr_pack(rgcr_pl, true);
3526 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, max_rifs);
3527 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
3528 if (err)
3529 goto err_rgcr_fail;
3530
3531 return 0;
3532
3533err_rgcr_fail:
3534 kfree(mlxsw_sp->rifs);
3535 return err;
3536}
3537
3538static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
3539{
3540 char rgcr_pl[MLXSW_REG_RGCR_LEN];
3541 int i;
3542
3543 mlxsw_reg_rgcr_pack(rgcr_pl, false);
3544 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
3545
3546 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
3547 WARN_ON_ONCE(mlxsw_sp->rifs[i]);
3548
3549 kfree(mlxsw_sp->rifs);
3550}
3551
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003552int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
3553{
3554 int err;
3555
3556 INIT_LIST_HEAD(&mlxsw_sp->router.nexthop_neighs_list);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003557 err = __mlxsw_sp_router_init(mlxsw_sp);
3558 if (err)
3559 return err;
3560
Ido Schimmelc53b8e12017-02-08 11:16:30 +01003561 err = rhashtable_init(&mlxsw_sp->router.nexthop_ht,
3562 &mlxsw_sp_nexthop_ht_params);
3563 if (err)
3564 goto err_nexthop_ht_init;
3565
Ido Schimmele9ad5e72017-02-08 11:16:29 +01003566 err = rhashtable_init(&mlxsw_sp->router.nexthop_group_ht,
3567 &mlxsw_sp_nexthop_group_ht_params);
3568 if (err)
3569 goto err_nexthop_group_ht_init;
3570
Ido Schimmel8494ab02017-03-24 08:02:47 +01003571 err = mlxsw_sp_lpm_init(mlxsw_sp);
3572 if (err)
3573 goto err_lpm_init;
3574
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003575 err = mlxsw_sp_vrs_init(mlxsw_sp);
3576 if (err)
3577 goto err_vrs_init;
3578
Ido Schimmel8c9583a2016-10-27 15:12:57 +02003579 err = mlxsw_sp_neigh_init(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003580 if (err)
3581 goto err_neigh_init;
3582
3583 mlxsw_sp->fib_nb.notifier_call = mlxsw_sp_router_fib_event;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01003584 err = register_fib_notifier(&mlxsw_sp->fib_nb,
3585 mlxsw_sp_router_fib_dump_flush);
3586 if (err)
3587 goto err_register_fib_notifier;
3588
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003589 return 0;
3590
Ido Schimmelc3852ef2016-12-03 16:45:07 +01003591err_register_fib_notifier:
3592 mlxsw_sp_neigh_fini(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003593err_neigh_init:
3594 mlxsw_sp_vrs_fini(mlxsw_sp);
3595err_vrs_init:
Ido Schimmel8494ab02017-03-24 08:02:47 +01003596 mlxsw_sp_lpm_fini(mlxsw_sp);
3597err_lpm_init:
Ido Schimmele9ad5e72017-02-08 11:16:29 +01003598 rhashtable_destroy(&mlxsw_sp->router.nexthop_group_ht);
3599err_nexthop_group_ht_init:
Ido Schimmelc53b8e12017-02-08 11:16:30 +01003600 rhashtable_destroy(&mlxsw_sp->router.nexthop_ht);
3601err_nexthop_ht_init:
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003602 __mlxsw_sp_router_fini(mlxsw_sp);
3603 return err;
3604}
3605
3606void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
3607{
3608 unregister_fib_notifier(&mlxsw_sp->fib_nb);
3609 mlxsw_sp_neigh_fini(mlxsw_sp);
3610 mlxsw_sp_vrs_fini(mlxsw_sp);
Ido Schimmel8494ab02017-03-24 08:02:47 +01003611 mlxsw_sp_lpm_fini(mlxsw_sp);
Ido Schimmele9ad5e72017-02-08 11:16:29 +01003612 rhashtable_destroy(&mlxsw_sp->router.nexthop_group_ht);
Ido Schimmelc53b8e12017-02-08 11:16:30 +01003613 rhashtable_destroy(&mlxsw_sp->router.nexthop_ht);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003614 __mlxsw_sp_router_fini(mlxsw_sp);
3615}