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>
6 * Copyright (c) 2016 Yotam Gigi <yotamg@mellanox.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
37 #include <linux/kernel.h>
38 #include <linux/types.h>
39 #include <linux/rhashtable.h>
40 #include <linux/bitops.h>
41 #include <linux/in6.h>
42 #include <linux/notifier.h>
43 #include <net/netevent.h>
44 #include <net/neighbour.h>
46 #include <net/ip_fib.h>
52 #define mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) \
53 for_each_set_bit(prefix, (prefix_usage)->b, MLXSW_SP_PREFIX_COUNT)
56 mlxsw_sp_prefix_usage_subset(struct mlxsw_sp_prefix_usage *prefix_usage1,
57 struct mlxsw_sp_prefix_usage *prefix_usage2)
61 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage1) {
62 if (!test_bit(prefix, prefix_usage2->b))
69 mlxsw_sp_prefix_usage_eq(struct mlxsw_sp_prefix_usage *prefix_usage1,
70 struct mlxsw_sp_prefix_usage *prefix_usage2)
72 return !memcmp(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
76 mlxsw_sp_prefix_usage_none(struct mlxsw_sp_prefix_usage *prefix_usage)
78 struct mlxsw_sp_prefix_usage prefix_usage_none = {{ 0 } };
80 return mlxsw_sp_prefix_usage_eq(prefix_usage, &prefix_usage_none);
84 mlxsw_sp_prefix_usage_cpy(struct mlxsw_sp_prefix_usage *prefix_usage1,
85 struct mlxsw_sp_prefix_usage *prefix_usage2)
87 memcpy(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
91 mlxsw_sp_prefix_usage_zero(struct mlxsw_sp_prefix_usage *prefix_usage)
93 memset(prefix_usage, 0, sizeof(*prefix_usage));
97 mlxsw_sp_prefix_usage_set(struct mlxsw_sp_prefix_usage *prefix_usage,
98 unsigned char prefix_len)
100 set_bit(prefix_len, prefix_usage->b);
104 mlxsw_sp_prefix_usage_clear(struct mlxsw_sp_prefix_usage *prefix_usage,
105 unsigned char prefix_len)
107 clear_bit(prefix_len, prefix_usage->b);
110 struct mlxsw_sp_fib_key {
111 struct net_device *dev;
112 unsigned char addr[sizeof(struct in6_addr)];
113 unsigned char prefix_len;
116 enum mlxsw_sp_fib_entry_type {
117 MLXSW_SP_FIB_ENTRY_TYPE_REMOTE,
118 MLXSW_SP_FIB_ENTRY_TYPE_LOCAL,
119 MLXSW_SP_FIB_ENTRY_TYPE_TRAP,
122 struct mlxsw_sp_nexthop_group;
124 struct mlxsw_sp_fib_entry {
125 struct rhash_head ht_node;
126 struct list_head list;
127 struct mlxsw_sp_fib_key key;
128 enum mlxsw_sp_fib_entry_type type;
129 unsigned int ref_count;
130 u16 rif; /* used for action local */
131 struct mlxsw_sp_vr *vr;
133 struct list_head nexthop_group_node;
134 struct mlxsw_sp_nexthop_group *nh_group;
137 struct mlxsw_sp_fib {
138 struct rhashtable ht;
139 struct list_head entry_list;
140 unsigned long prefix_ref_count[MLXSW_SP_PREFIX_COUNT];
141 struct mlxsw_sp_prefix_usage prefix_usage;
144 static const struct rhashtable_params mlxsw_sp_fib_ht_params = {
145 .key_offset = offsetof(struct mlxsw_sp_fib_entry, key),
146 .head_offset = offsetof(struct mlxsw_sp_fib_entry, ht_node),
147 .key_len = sizeof(struct mlxsw_sp_fib_key),
148 .automatic_shrinking = true,
151 static int mlxsw_sp_fib_entry_insert(struct mlxsw_sp_fib *fib,
152 struct mlxsw_sp_fib_entry *fib_entry)
154 unsigned char prefix_len = fib_entry->key.prefix_len;
157 err = rhashtable_insert_fast(&fib->ht, &fib_entry->ht_node,
158 mlxsw_sp_fib_ht_params);
161 list_add_tail(&fib_entry->list, &fib->entry_list);
162 if (fib->prefix_ref_count[prefix_len]++ == 0)
163 mlxsw_sp_prefix_usage_set(&fib->prefix_usage, prefix_len);
167 static void mlxsw_sp_fib_entry_remove(struct mlxsw_sp_fib *fib,
168 struct mlxsw_sp_fib_entry *fib_entry)
170 unsigned char prefix_len = fib_entry->key.prefix_len;
172 if (--fib->prefix_ref_count[prefix_len] == 0)
173 mlxsw_sp_prefix_usage_clear(&fib->prefix_usage, prefix_len);
174 list_del(&fib_entry->list);
175 rhashtable_remove_fast(&fib->ht, &fib_entry->ht_node,
176 mlxsw_sp_fib_ht_params);
179 static struct mlxsw_sp_fib_entry *
180 mlxsw_sp_fib_entry_create(struct mlxsw_sp_fib *fib, const void *addr,
181 size_t addr_len, unsigned char prefix_len,
182 struct net_device *dev)
184 struct mlxsw_sp_fib_entry *fib_entry;
186 fib_entry = kzalloc(sizeof(*fib_entry), GFP_KERNEL);
189 fib_entry->key.dev = dev;
190 memcpy(fib_entry->key.addr, addr, addr_len);
191 fib_entry->key.prefix_len = prefix_len;
195 static void mlxsw_sp_fib_entry_destroy(struct mlxsw_sp_fib_entry *fib_entry)
200 static struct mlxsw_sp_fib_entry *
201 mlxsw_sp_fib_entry_lookup(struct mlxsw_sp_fib *fib, const void *addr,
202 size_t addr_len, unsigned char prefix_len,
203 struct net_device *dev)
205 struct mlxsw_sp_fib_key key;
207 memset(&key, 0, sizeof(key));
209 memcpy(key.addr, addr, addr_len);
210 key.prefix_len = prefix_len;
211 return rhashtable_lookup_fast(&fib->ht, &key, mlxsw_sp_fib_ht_params);
214 static struct mlxsw_sp_fib *mlxsw_sp_fib_create(void)
216 struct mlxsw_sp_fib *fib;
219 fib = kzalloc(sizeof(*fib), GFP_KERNEL);
221 return ERR_PTR(-ENOMEM);
222 err = rhashtable_init(&fib->ht, &mlxsw_sp_fib_ht_params);
224 goto err_rhashtable_init;
225 INIT_LIST_HEAD(&fib->entry_list);
233 static void mlxsw_sp_fib_destroy(struct mlxsw_sp_fib *fib)
235 rhashtable_destroy(&fib->ht);
239 static struct mlxsw_sp_lpm_tree *
240 mlxsw_sp_lpm_tree_find_unused(struct mlxsw_sp *mlxsw_sp, bool one_reserved)
242 static struct mlxsw_sp_lpm_tree *lpm_tree;
245 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
246 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
247 if (lpm_tree->ref_count == 0) {
249 one_reserved = false;
257 static int mlxsw_sp_lpm_tree_alloc(struct mlxsw_sp *mlxsw_sp,
258 struct mlxsw_sp_lpm_tree *lpm_tree)
260 char ralta_pl[MLXSW_REG_RALTA_LEN];
262 mlxsw_reg_ralta_pack(ralta_pl, true,
263 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
265 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
268 static int mlxsw_sp_lpm_tree_free(struct mlxsw_sp *mlxsw_sp,
269 struct mlxsw_sp_lpm_tree *lpm_tree)
271 char ralta_pl[MLXSW_REG_RALTA_LEN];
273 mlxsw_reg_ralta_pack(ralta_pl, false,
274 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
276 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
280 mlxsw_sp_lpm_tree_left_struct_set(struct mlxsw_sp *mlxsw_sp,
281 struct mlxsw_sp_prefix_usage *prefix_usage,
282 struct mlxsw_sp_lpm_tree *lpm_tree)
284 char ralst_pl[MLXSW_REG_RALST_LEN];
287 u8 last_prefix = MLXSW_REG_RALST_BIN_NO_CHILD;
289 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage)
292 mlxsw_reg_ralst_pack(ralst_pl, root_bin, lpm_tree->id);
293 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) {
296 mlxsw_reg_ralst_bin_pack(ralst_pl, prefix, last_prefix,
297 MLXSW_REG_RALST_BIN_NO_CHILD);
298 last_prefix = prefix;
300 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
303 static struct mlxsw_sp_lpm_tree *
304 mlxsw_sp_lpm_tree_create(struct mlxsw_sp *mlxsw_sp,
305 struct mlxsw_sp_prefix_usage *prefix_usage,
306 enum mlxsw_sp_l3proto proto, bool one_reserved)
308 struct mlxsw_sp_lpm_tree *lpm_tree;
311 lpm_tree = mlxsw_sp_lpm_tree_find_unused(mlxsw_sp, one_reserved);
313 return ERR_PTR(-EBUSY);
314 lpm_tree->proto = proto;
315 err = mlxsw_sp_lpm_tree_alloc(mlxsw_sp, lpm_tree);
319 err = mlxsw_sp_lpm_tree_left_struct_set(mlxsw_sp, prefix_usage,
322 goto err_left_struct_set;
323 memcpy(&lpm_tree->prefix_usage, prefix_usage,
324 sizeof(lpm_tree->prefix_usage));
328 mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
332 static int mlxsw_sp_lpm_tree_destroy(struct mlxsw_sp *mlxsw_sp,
333 struct mlxsw_sp_lpm_tree *lpm_tree)
335 return mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
338 static struct mlxsw_sp_lpm_tree *
339 mlxsw_sp_lpm_tree_get(struct mlxsw_sp *mlxsw_sp,
340 struct mlxsw_sp_prefix_usage *prefix_usage,
341 enum mlxsw_sp_l3proto proto, bool one_reserved)
343 struct mlxsw_sp_lpm_tree *lpm_tree;
346 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
347 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
348 if (lpm_tree->ref_count != 0 &&
349 lpm_tree->proto == proto &&
350 mlxsw_sp_prefix_usage_eq(&lpm_tree->prefix_usage,
354 lpm_tree = mlxsw_sp_lpm_tree_create(mlxsw_sp, prefix_usage,
355 proto, one_reserved);
356 if (IS_ERR(lpm_tree))
360 lpm_tree->ref_count++;
364 static int mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp,
365 struct mlxsw_sp_lpm_tree *lpm_tree)
367 if (--lpm_tree->ref_count == 0)
368 return mlxsw_sp_lpm_tree_destroy(mlxsw_sp, lpm_tree);
372 static void mlxsw_sp_lpm_init(struct mlxsw_sp *mlxsw_sp)
374 struct mlxsw_sp_lpm_tree *lpm_tree;
377 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
378 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
379 lpm_tree->id = i + MLXSW_SP_LPM_TREE_MIN;
383 static struct mlxsw_sp_vr *mlxsw_sp_vr_find_unused(struct mlxsw_sp *mlxsw_sp)
385 struct mlxsw_sp_vr *vr;
388 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
389 vr = &mlxsw_sp->router.vrs[i];
396 static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp,
397 struct mlxsw_sp_vr *vr)
399 char raltb_pl[MLXSW_REG_RALTB_LEN];
401 mlxsw_reg_raltb_pack(raltb_pl, vr->id,
402 (enum mlxsw_reg_ralxx_protocol) vr->proto,
404 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
407 static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp,
408 struct mlxsw_sp_vr *vr)
410 char raltb_pl[MLXSW_REG_RALTB_LEN];
412 /* Bind to tree 0 which is default */
413 mlxsw_reg_raltb_pack(raltb_pl, vr->id,
414 (enum mlxsw_reg_ralxx_protocol) vr->proto, 0);
415 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
418 static u32 mlxsw_sp_fix_tb_id(u32 tb_id)
420 /* For our purpose, squash main and local table into one */
421 if (tb_id == RT_TABLE_LOCAL)
422 tb_id = RT_TABLE_MAIN;
426 static struct mlxsw_sp_vr *mlxsw_sp_vr_find(struct mlxsw_sp *mlxsw_sp,
428 enum mlxsw_sp_l3proto proto)
430 struct mlxsw_sp_vr *vr;
433 tb_id = mlxsw_sp_fix_tb_id(tb_id);
435 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
436 vr = &mlxsw_sp->router.vrs[i];
437 if (vr->used && vr->proto == proto && vr->tb_id == tb_id)
443 static struct mlxsw_sp_vr *mlxsw_sp_vr_create(struct mlxsw_sp *mlxsw_sp,
444 unsigned char prefix_len,
446 enum mlxsw_sp_l3proto proto)
448 struct mlxsw_sp_prefix_usage req_prefix_usage;
449 struct mlxsw_sp_lpm_tree *lpm_tree;
450 struct mlxsw_sp_vr *vr;
453 vr = mlxsw_sp_vr_find_unused(mlxsw_sp);
455 return ERR_PTR(-EBUSY);
456 vr->fib = mlxsw_sp_fib_create();
458 return ERR_CAST(vr->fib);
462 mlxsw_sp_prefix_usage_zero(&req_prefix_usage);
463 mlxsw_sp_prefix_usage_set(&req_prefix_usage, prefix_len);
464 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
466 if (IS_ERR(lpm_tree)) {
467 err = PTR_ERR(lpm_tree);
470 vr->lpm_tree = lpm_tree;
471 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, vr);
479 mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree);
481 mlxsw_sp_fib_destroy(vr->fib);
486 static void mlxsw_sp_vr_destroy(struct mlxsw_sp *mlxsw_sp,
487 struct mlxsw_sp_vr *vr)
489 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, vr);
490 mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree);
491 mlxsw_sp_fib_destroy(vr->fib);
496 mlxsw_sp_vr_lpm_tree_check(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr,
497 struct mlxsw_sp_prefix_usage *req_prefix_usage)
499 struct mlxsw_sp_lpm_tree *lpm_tree;
501 if (mlxsw_sp_prefix_usage_eq(req_prefix_usage,
502 &vr->lpm_tree->prefix_usage))
505 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, req_prefix_usage,
507 if (IS_ERR(lpm_tree)) {
508 /* We failed to get a tree according to the required
509 * prefix usage. However, the current tree might be still good
510 * for us if our requirement is subset of the prefixes used
513 if (mlxsw_sp_prefix_usage_subset(req_prefix_usage,
514 &vr->lpm_tree->prefix_usage))
516 return PTR_ERR(lpm_tree);
519 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, vr);
520 mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree);
521 vr->lpm_tree = lpm_tree;
522 return mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, vr);
525 static struct mlxsw_sp_vr *mlxsw_sp_vr_get(struct mlxsw_sp *mlxsw_sp,
526 unsigned char prefix_len,
528 enum mlxsw_sp_l3proto proto)
530 struct mlxsw_sp_vr *vr;
533 tb_id = mlxsw_sp_fix_tb_id(tb_id);
534 vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id, proto);
536 vr = mlxsw_sp_vr_create(mlxsw_sp, prefix_len, tb_id, proto);
540 struct mlxsw_sp_prefix_usage req_prefix_usage;
542 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage,
543 &vr->fib->prefix_usage);
544 mlxsw_sp_prefix_usage_set(&req_prefix_usage, prefix_len);
545 /* Need to replace LPM tree in case new prefix is required. */
546 err = mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, vr,
554 static void mlxsw_sp_vr_put(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr)
556 /* Destroy virtual router entity in case the associated FIB is empty
557 * and allow it to be used for other tables in future. Otherwise,
558 * check if some prefix usage did not disappear and change tree if
559 * that is the case. Note that in case new, smaller tree cannot be
560 * allocated, the original one will be kept being used.
562 if (mlxsw_sp_prefix_usage_none(&vr->fib->prefix_usage))
563 mlxsw_sp_vr_destroy(mlxsw_sp, vr);
565 mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, vr,
566 &vr->fib->prefix_usage);
569 static int mlxsw_sp_vrs_init(struct mlxsw_sp *mlxsw_sp)
571 struct mlxsw_sp_vr *vr;
575 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_VRS))
578 max_vrs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS);
579 mlxsw_sp->router.vrs = kcalloc(max_vrs, sizeof(struct mlxsw_sp_vr),
581 if (!mlxsw_sp->router.vrs)
584 for (i = 0; i < max_vrs; i++) {
585 vr = &mlxsw_sp->router.vrs[i];
592 static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp);
594 static void mlxsw_sp_vrs_fini(struct mlxsw_sp *mlxsw_sp)
596 /* At this stage we're guaranteed not to have new incoming
597 * FIB notifications and the work queue is free from FIBs
598 * sitting on top of mlxsw netdevs. However, we can still
599 * have other FIBs queued. Flush the queue before flushing
600 * the device's tables. No need for locks, as we're the only
603 mlxsw_core_flush_owq();
604 mlxsw_sp_router_fib_flush(mlxsw_sp);
605 kfree(mlxsw_sp->router.vrs);
608 struct mlxsw_sp_neigh_key {
612 struct mlxsw_sp_neigh_entry {
613 struct rhash_head ht_node;
614 struct mlxsw_sp_neigh_key key;
617 unsigned char ha[ETH_ALEN];
618 struct list_head nexthop_list; /* list of nexthops using
621 struct list_head nexthop_neighs_list_node;
624 static const struct rhashtable_params mlxsw_sp_neigh_ht_params = {
625 .key_offset = offsetof(struct mlxsw_sp_neigh_entry, key),
626 .head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node),
627 .key_len = sizeof(struct mlxsw_sp_neigh_key),
630 static struct mlxsw_sp_neigh_entry *
631 mlxsw_sp_neigh_entry_alloc(struct mlxsw_sp *mlxsw_sp, struct neighbour *n,
634 struct mlxsw_sp_neigh_entry *neigh_entry;
636 neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_KERNEL);
640 neigh_entry->key.n = n;
641 neigh_entry->rif = rif;
642 INIT_LIST_HEAD(&neigh_entry->nexthop_list);
647 static void mlxsw_sp_neigh_entry_free(struct mlxsw_sp_neigh_entry *neigh_entry)
653 mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp,
654 struct mlxsw_sp_neigh_entry *neigh_entry)
656 return rhashtable_insert_fast(&mlxsw_sp->router.neigh_ht,
657 &neigh_entry->ht_node,
658 mlxsw_sp_neigh_ht_params);
662 mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp,
663 struct mlxsw_sp_neigh_entry *neigh_entry)
665 rhashtable_remove_fast(&mlxsw_sp->router.neigh_ht,
666 &neigh_entry->ht_node,
667 mlxsw_sp_neigh_ht_params);
670 static struct mlxsw_sp_neigh_entry *
671 mlxsw_sp_neigh_entry_create(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
673 struct mlxsw_sp_neigh_entry *neigh_entry;
674 struct mlxsw_sp_rif *r;
677 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, n->dev);
679 return ERR_PTR(-EINVAL);
681 neigh_entry = mlxsw_sp_neigh_entry_alloc(mlxsw_sp, n, r->rif);
683 return ERR_PTR(-ENOMEM);
685 err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
687 goto err_neigh_entry_insert;
691 err_neigh_entry_insert:
692 mlxsw_sp_neigh_entry_free(neigh_entry);
697 mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp *mlxsw_sp,
698 struct mlxsw_sp_neigh_entry *neigh_entry)
700 mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
701 mlxsw_sp_neigh_entry_free(neigh_entry);
704 static struct mlxsw_sp_neigh_entry *
705 mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
707 struct mlxsw_sp_neigh_key key;
710 return rhashtable_lookup_fast(&mlxsw_sp->router.neigh_ht,
711 &key, mlxsw_sp_neigh_ht_params);
715 mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp)
717 unsigned long interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME);
719 mlxsw_sp->router.neighs_update.interval = jiffies_to_msecs(interval);
722 static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
726 struct net_device *dev;
732 mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip);
734 if (!mlxsw_sp->rifs[rif]) {
735 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
740 dev = mlxsw_sp->rifs[rif]->dev;
741 n = neigh_lookup(&arp_tbl, &dipn, dev);
743 netdev_err(dev, "Failed to find matching neighbour for IP=%pI4h\n",
748 netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip);
749 neigh_event_send(n, NULL);
753 static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp,
760 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
762 /* Hardware starts counting at 0, so add 1. */
765 /* Each record consists of several neighbour entries. */
766 for (i = 0; i < num_entries; i++) {
769 ent_index = rec_index * MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC + i;
770 mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp, rauhtd_pl,
776 static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp,
777 char *rauhtd_pl, int rec_index)
779 switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, rec_index)) {
780 case MLXSW_REG_RAUHTD_TYPE_IPV4:
781 mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp, rauhtd_pl,
784 case MLXSW_REG_RAUHTD_TYPE_IPV6:
790 static bool mlxsw_sp_router_rauhtd_is_full(char *rauhtd_pl)
792 u8 num_rec, last_rec_index, num_entries;
794 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
795 last_rec_index = num_rec - 1;
797 if (num_rec < MLXSW_REG_RAUHTD_REC_MAX_NUM)
799 if (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, last_rec_index) ==
800 MLXSW_REG_RAUHTD_TYPE_IPV6)
803 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
805 if (++num_entries == MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC)
810 static int mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp)
816 rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL);
820 /* Make sure the neighbour's netdev isn't removed in the
825 mlxsw_reg_rauhtd_pack(rauhtd_pl, MLXSW_REG_RAUHTD_TYPE_IPV4);
826 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd),
829 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour talbe\n");
832 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
833 for (i = 0; i < num_rec; i++)
834 mlxsw_sp_router_neigh_rec_process(mlxsw_sp, rauhtd_pl,
836 } while (mlxsw_sp_router_rauhtd_is_full(rauhtd_pl));
843 static void mlxsw_sp_router_neighs_update_nh(struct mlxsw_sp *mlxsw_sp)
845 struct mlxsw_sp_neigh_entry *neigh_entry;
847 /* Take RTNL mutex here to prevent lists from changes */
849 list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list,
850 nexthop_neighs_list_node)
851 /* If this neigh have nexthops, make the kernel think this neigh
852 * is active regardless of the traffic.
854 neigh_event_send(neigh_entry->key.n, NULL);
859 mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp)
861 unsigned long interval = mlxsw_sp->router.neighs_update.interval;
863 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw,
864 msecs_to_jiffies(interval));
867 static void mlxsw_sp_router_neighs_update_work(struct work_struct *work)
869 struct mlxsw_sp *mlxsw_sp = container_of(work, struct mlxsw_sp,
870 router.neighs_update.dw.work);
873 err = mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp);
875 dev_err(mlxsw_sp->bus_info->dev, "Could not update kernel for neigh activity");
877 mlxsw_sp_router_neighs_update_nh(mlxsw_sp);
879 mlxsw_sp_router_neighs_update_work_schedule(mlxsw_sp);
882 static void mlxsw_sp_router_probe_unresolved_nexthops(struct work_struct *work)
884 struct mlxsw_sp_neigh_entry *neigh_entry;
885 struct mlxsw_sp *mlxsw_sp = container_of(work, struct mlxsw_sp,
886 router.nexthop_probe_dw.work);
888 /* Iterate over nexthop neighbours, find those who are unresolved and
889 * send arp on them. This solves the chicken-egg problem when
890 * the nexthop wouldn't get offloaded until the neighbor is resolved
891 * but it wouldn't get resolved ever in case traffic is flowing in HW
892 * using different nexthop.
894 * Take RTNL mutex here to prevent lists from changes.
897 list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list,
898 nexthop_neighs_list_node)
899 if (!neigh_entry->connected)
900 neigh_event_send(neigh_entry->key.n, NULL);
903 mlxsw_core_schedule_dw(&mlxsw_sp->router.nexthop_probe_dw,
904 MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL);
908 mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
909 struct mlxsw_sp_neigh_entry *neigh_entry,
912 static enum mlxsw_reg_rauht_op mlxsw_sp_rauht_op(bool adding)
914 return adding ? MLXSW_REG_RAUHT_OP_WRITE_ADD :
915 MLXSW_REG_RAUHT_OP_WRITE_DELETE;
919 mlxsw_sp_router_neigh_entry_op4(struct mlxsw_sp *mlxsw_sp,
920 struct mlxsw_sp_neigh_entry *neigh_entry,
921 enum mlxsw_reg_rauht_op op)
923 struct neighbour *n = neigh_entry->key.n;
924 u32 dip = ntohl(*((__be32 *) n->primary_key));
925 char rauht_pl[MLXSW_REG_RAUHT_LEN];
927 mlxsw_reg_rauht_pack4(rauht_pl, op, neigh_entry->rif, neigh_entry->ha,
929 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
933 mlxsw_sp_neigh_entry_update(struct mlxsw_sp *mlxsw_sp,
934 struct mlxsw_sp_neigh_entry *neigh_entry,
937 if (!adding && !neigh_entry->connected)
939 neigh_entry->connected = adding;
940 if (neigh_entry->key.n->tbl == &arp_tbl)
941 mlxsw_sp_router_neigh_entry_op4(mlxsw_sp, neigh_entry,
942 mlxsw_sp_rauht_op(adding));
947 struct mlxsw_sp_neigh_event_work {
948 struct work_struct work;
949 struct mlxsw_sp *mlxsw_sp;
953 static void mlxsw_sp_router_neigh_event_work(struct work_struct *work)
955 struct mlxsw_sp_neigh_event_work *neigh_work =
956 container_of(work, struct mlxsw_sp_neigh_event_work, work);
957 struct mlxsw_sp *mlxsw_sp = neigh_work->mlxsw_sp;
958 struct mlxsw_sp_neigh_entry *neigh_entry;
959 struct neighbour *n = neigh_work->n;
960 unsigned char ha[ETH_ALEN];
961 bool entry_connected;
964 /* If these parameters are changed after we release the lock,
965 * then we are guaranteed to receive another event letting us
968 read_lock_bh(&n->lock);
969 memcpy(ha, n->ha, ETH_ALEN);
970 nud_state = n->nud_state;
972 read_unlock_bh(&n->lock);
975 entry_connected = nud_state & NUD_VALID && !dead;
976 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
977 if (!entry_connected && !neigh_entry)
980 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
981 if (IS_ERR(neigh_entry))
985 memcpy(neigh_entry->ha, ha, ETH_ALEN);
986 mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, entry_connected);
987 mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, !entry_connected);
989 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
990 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
998 int mlxsw_sp_router_netevent_event(struct notifier_block *unused,
999 unsigned long event, void *ptr)
1001 struct mlxsw_sp_neigh_event_work *neigh_work;
1002 struct mlxsw_sp_port *mlxsw_sp_port;
1003 struct mlxsw_sp *mlxsw_sp;
1004 unsigned long interval;
1005 struct neigh_parms *p;
1006 struct neighbour *n;
1009 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
1012 /* We don't care about changes in the default table. */
1013 if (!p->dev || p->tbl != &arp_tbl)
1016 /* We are in atomic context and can't take RTNL mutex,
1017 * so use RCU variant to walk the device chain.
1019 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev);
1023 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1024 interval = jiffies_to_msecs(NEIGH_VAR(p, DELAY_PROBE_TIME));
1025 mlxsw_sp->router.neighs_update.interval = interval;
1027 mlxsw_sp_port_dev_put(mlxsw_sp_port);
1029 case NETEVENT_NEIGH_UPDATE:
1032 if (n->tbl != &arp_tbl)
1035 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(n->dev);
1039 neigh_work = kzalloc(sizeof(*neigh_work), GFP_ATOMIC);
1041 mlxsw_sp_port_dev_put(mlxsw_sp_port);
1045 INIT_WORK(&neigh_work->work, mlxsw_sp_router_neigh_event_work);
1046 neigh_work->mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1049 /* Take a reference to ensure the neighbour won't be
1050 * destructed until we drop the reference in delayed
1054 mlxsw_core_schedule_work(&neigh_work->work);
1055 mlxsw_sp_port_dev_put(mlxsw_sp_port);
1062 static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp)
1066 err = rhashtable_init(&mlxsw_sp->router.neigh_ht,
1067 &mlxsw_sp_neigh_ht_params);
1071 /* Initialize the polling interval according to the default
1074 mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp);
1076 /* Create the delayed works for the activity_update */
1077 INIT_DELAYED_WORK(&mlxsw_sp->router.neighs_update.dw,
1078 mlxsw_sp_router_neighs_update_work);
1079 INIT_DELAYED_WORK(&mlxsw_sp->router.nexthop_probe_dw,
1080 mlxsw_sp_router_probe_unresolved_nexthops);
1081 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw, 0);
1082 mlxsw_core_schedule_dw(&mlxsw_sp->router.nexthop_probe_dw, 0);
1086 static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp)
1088 cancel_delayed_work_sync(&mlxsw_sp->router.neighs_update.dw);
1089 cancel_delayed_work_sync(&mlxsw_sp->router.nexthop_probe_dw);
1090 rhashtable_destroy(&mlxsw_sp->router.neigh_ht);
1093 struct mlxsw_sp_nexthop_key {
1094 struct fib_nh *fib_nh;
1097 struct mlxsw_sp_nexthop {
1098 struct list_head neigh_list_node; /* member of neigh entry list */
1099 struct mlxsw_sp_nexthop_group *nh_grp; /* pointer back to the group
1102 struct rhash_head ht_node;
1103 struct mlxsw_sp_nexthop_key key;
1104 u8 should_offload:1, /* set indicates this neigh is connected and
1105 * should be put to KVD linear area of this group.
1107 offloaded:1, /* set in case the neigh is actually put into
1108 * KVD linear area of this group.
1110 update:1; /* set indicates that MAC of this neigh should be
1113 struct mlxsw_sp_neigh_entry *neigh_entry;
1116 struct mlxsw_sp_nexthop_group_key {
1117 struct fib_info *fi;
1120 struct mlxsw_sp_nexthop_group {
1121 struct rhash_head ht_node;
1122 struct list_head fib_list; /* list of fib entries that use this group */
1123 struct mlxsw_sp_nexthop_group_key key;
1124 u8 adj_index_valid:1;
1128 struct mlxsw_sp_nexthop nexthops[0];
1131 static const struct rhashtable_params mlxsw_sp_nexthop_group_ht_params = {
1132 .key_offset = offsetof(struct mlxsw_sp_nexthop_group, key),
1133 .head_offset = offsetof(struct mlxsw_sp_nexthop_group, ht_node),
1134 .key_len = sizeof(struct mlxsw_sp_nexthop_group_key),
1137 static int mlxsw_sp_nexthop_group_insert(struct mlxsw_sp *mlxsw_sp,
1138 struct mlxsw_sp_nexthop_group *nh_grp)
1140 return rhashtable_insert_fast(&mlxsw_sp->router.nexthop_group_ht,
1142 mlxsw_sp_nexthop_group_ht_params);
1145 static void mlxsw_sp_nexthop_group_remove(struct mlxsw_sp *mlxsw_sp,
1146 struct mlxsw_sp_nexthop_group *nh_grp)
1148 rhashtable_remove_fast(&mlxsw_sp->router.nexthop_group_ht,
1150 mlxsw_sp_nexthop_group_ht_params);
1153 static struct mlxsw_sp_nexthop_group *
1154 mlxsw_sp_nexthop_group_lookup(struct mlxsw_sp *mlxsw_sp,
1155 struct mlxsw_sp_nexthop_group_key key)
1157 return rhashtable_lookup_fast(&mlxsw_sp->router.nexthop_group_ht, &key,
1158 mlxsw_sp_nexthop_group_ht_params);
1161 static const struct rhashtable_params mlxsw_sp_nexthop_ht_params = {
1162 .key_offset = offsetof(struct mlxsw_sp_nexthop, key),
1163 .head_offset = offsetof(struct mlxsw_sp_nexthop, ht_node),
1164 .key_len = sizeof(struct mlxsw_sp_nexthop_key),
1167 static int mlxsw_sp_nexthop_insert(struct mlxsw_sp *mlxsw_sp,
1168 struct mlxsw_sp_nexthop *nh)
1170 return rhashtable_insert_fast(&mlxsw_sp->router.nexthop_ht,
1171 &nh->ht_node, mlxsw_sp_nexthop_ht_params);
1174 static void mlxsw_sp_nexthop_remove(struct mlxsw_sp *mlxsw_sp,
1175 struct mlxsw_sp_nexthop *nh)
1177 rhashtable_remove_fast(&mlxsw_sp->router.nexthop_ht, &nh->ht_node,
1178 mlxsw_sp_nexthop_ht_params);
1181 static int mlxsw_sp_adj_index_mass_update_vr(struct mlxsw_sp *mlxsw_sp,
1182 struct mlxsw_sp_vr *vr,
1183 u32 adj_index, u16 ecmp_size,
1187 char raleu_pl[MLXSW_REG_RALEU_LEN];
1189 mlxsw_reg_raleu_pack(raleu_pl,
1190 (enum mlxsw_reg_ralxx_protocol) vr->proto, vr->id,
1191 adj_index, ecmp_size, new_adj_index,
1193 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raleu), raleu_pl);
1196 static int mlxsw_sp_adj_index_mass_update(struct mlxsw_sp *mlxsw_sp,
1197 struct mlxsw_sp_nexthop_group *nh_grp,
1198 u32 old_adj_index, u16 old_ecmp_size)
1200 struct mlxsw_sp_fib_entry *fib_entry;
1201 struct mlxsw_sp_vr *vr = NULL;
1204 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
1205 if (vr == fib_entry->vr)
1208 err = mlxsw_sp_adj_index_mass_update_vr(mlxsw_sp, vr,
1219 static int mlxsw_sp_nexthop_mac_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
1220 struct mlxsw_sp_nexthop *nh)
1222 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
1223 char ratr_pl[MLXSW_REG_RATR_LEN];
1225 mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY,
1226 true, adj_index, neigh_entry->rif);
1227 mlxsw_reg_ratr_eth_entry_pack(ratr_pl, neigh_entry->ha);
1228 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
1232 mlxsw_sp_nexthop_group_mac_update(struct mlxsw_sp *mlxsw_sp,
1233 struct mlxsw_sp_nexthop_group *nh_grp,
1236 u32 adj_index = nh_grp->adj_index; /* base */
1237 struct mlxsw_sp_nexthop *nh;
1241 for (i = 0; i < nh_grp->count; i++) {
1242 nh = &nh_grp->nexthops[i];
1244 if (!nh->should_offload) {
1249 if (nh->update || reallocate) {
1250 err = mlxsw_sp_nexthop_mac_update(mlxsw_sp,
1262 static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1263 struct mlxsw_sp_fib_entry *fib_entry);
1266 mlxsw_sp_nexthop_fib_entries_update(struct mlxsw_sp *mlxsw_sp,
1267 struct mlxsw_sp_nexthop_group *nh_grp)
1269 struct mlxsw_sp_fib_entry *fib_entry;
1272 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
1273 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1281 mlxsw_sp_nexthop_group_refresh(struct mlxsw_sp *mlxsw_sp,
1282 struct mlxsw_sp_nexthop_group *nh_grp)
1284 struct mlxsw_sp_nexthop *nh;
1285 bool offload_change = false;
1288 bool old_adj_index_valid;
1295 for (i = 0; i < nh_grp->count; i++) {
1296 nh = &nh_grp->nexthops[i];
1298 if (nh->should_offload ^ nh->offloaded) {
1299 offload_change = true;
1300 if (nh->should_offload)
1303 if (nh->should_offload)
1306 if (!offload_change) {
1307 /* Nothing was added or removed, so no need to reallocate. Just
1308 * update MAC on existing adjacency indexes.
1310 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp,
1313 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1319 /* No neigh of this group is connected so we just set
1320 * the trap and let everthing flow through kernel.
1324 ret = mlxsw_sp_kvdl_alloc(mlxsw_sp, ecmp_size);
1326 /* We ran out of KVD linear space, just set the
1327 * trap and let everything flow through kernel.
1329 dev_warn(mlxsw_sp->bus_info->dev, "Failed to allocate KVD linear area for nexthop group.\n");
1333 old_adj_index_valid = nh_grp->adj_index_valid;
1334 old_adj_index = nh_grp->adj_index;
1335 old_ecmp_size = nh_grp->ecmp_size;
1336 nh_grp->adj_index_valid = 1;
1337 nh_grp->adj_index = adj_index;
1338 nh_grp->ecmp_size = ecmp_size;
1339 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp, true);
1341 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1345 if (!old_adj_index_valid) {
1346 /* The trap was set for fib entries, so we have to call
1347 * fib entry update to unset it and use adjacency index.
1349 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1351 dev_warn(mlxsw_sp->bus_info->dev, "Failed to add adjacency index to fib entries.\n");
1357 err = mlxsw_sp_adj_index_mass_update(mlxsw_sp, nh_grp,
1358 old_adj_index, old_ecmp_size);
1359 mlxsw_sp_kvdl_free(mlxsw_sp, old_adj_index);
1361 dev_warn(mlxsw_sp->bus_info->dev, "Failed to mass-update adjacency index for nexthop group.\n");
1367 old_adj_index_valid = nh_grp->adj_index_valid;
1368 nh_grp->adj_index_valid = 0;
1369 for (i = 0; i < nh_grp->count; i++) {
1370 nh = &nh_grp->nexthops[i];
1373 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1375 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set traps for fib entries.\n");
1376 if (old_adj_index_valid)
1377 mlxsw_sp_kvdl_free(mlxsw_sp, nh_grp->adj_index);
1380 static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop *nh,
1383 if (!removing && !nh->should_offload)
1384 nh->should_offload = 1;
1385 else if (removing && nh->offloaded)
1386 nh->should_offload = 0;
1391 mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
1392 struct mlxsw_sp_neigh_entry *neigh_entry,
1395 struct mlxsw_sp_nexthop *nh;
1397 list_for_each_entry(nh, &neigh_entry->nexthop_list,
1399 __mlxsw_sp_nexthop_neigh_update(nh, removing);
1400 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1404 static int mlxsw_sp_nexthop_init(struct mlxsw_sp *mlxsw_sp,
1405 struct mlxsw_sp_nexthop_group *nh_grp,
1406 struct mlxsw_sp_nexthop *nh,
1407 struct fib_nh *fib_nh)
1409 struct mlxsw_sp_neigh_entry *neigh_entry;
1410 struct net_device *dev = fib_nh->nh_dev;
1411 struct neighbour *n;
1415 nh->key.fib_nh = fib_nh;
1416 err = mlxsw_sp_nexthop_insert(mlxsw_sp, nh);
1420 /* Take a reference of neigh here ensuring that neigh would
1421 * not be detructed before the nexthop entry is finished.
1422 * The reference is taken either in neigh_lookup() or
1423 * in neigh_create() in case n is not found.
1425 n = neigh_lookup(&arp_tbl, &fib_nh->nh_gw, dev);
1427 n = neigh_create(&arp_tbl, &fib_nh->nh_gw, dev);
1430 goto err_neigh_create;
1432 neigh_event_send(n, NULL);
1434 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
1436 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
1437 if (IS_ERR(neigh_entry)) {
1439 goto err_neigh_entry_create;
1443 /* If that is the first nexthop connected to that neigh, add to
1444 * nexthop_neighs_list
1446 if (list_empty(&neigh_entry->nexthop_list))
1447 list_add_tail(&neigh_entry->nexthop_neighs_list_node,
1448 &mlxsw_sp->router.nexthop_neighs_list);
1450 nh->nh_grp = nh_grp;
1451 nh->neigh_entry = neigh_entry;
1452 list_add_tail(&nh->neigh_list_node, &neigh_entry->nexthop_list);
1453 read_lock_bh(&n->lock);
1454 nud_state = n->nud_state;
1456 read_unlock_bh(&n->lock);
1457 __mlxsw_sp_nexthop_neigh_update(nh, !(nud_state & NUD_VALID && !dead));
1461 err_neigh_entry_create:
1464 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
1468 static void mlxsw_sp_nexthop_fini(struct mlxsw_sp *mlxsw_sp,
1469 struct mlxsw_sp_nexthop *nh)
1471 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
1472 struct neighbour *n = neigh_entry->key.n;
1474 __mlxsw_sp_nexthop_neigh_update(nh, true);
1475 list_del(&nh->neigh_list_node);
1476 nh->neigh_entry = NULL;
1478 /* If that is the last nexthop connected to that neigh, remove from
1479 * nexthop_neighs_list
1481 if (list_empty(&neigh_entry->nexthop_list))
1482 list_del(&neigh_entry->nexthop_neighs_list_node);
1484 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
1485 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
1489 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
1492 static struct mlxsw_sp_nexthop_group *
1493 mlxsw_sp_nexthop_group_create(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
1495 struct mlxsw_sp_nexthop_group *nh_grp;
1496 struct mlxsw_sp_nexthop *nh;
1497 struct fib_nh *fib_nh;
1502 alloc_size = sizeof(*nh_grp) +
1503 fi->fib_nhs * sizeof(struct mlxsw_sp_nexthop);
1504 nh_grp = kzalloc(alloc_size, GFP_KERNEL);
1506 return ERR_PTR(-ENOMEM);
1507 INIT_LIST_HEAD(&nh_grp->fib_list);
1508 nh_grp->count = fi->fib_nhs;
1509 nh_grp->key.fi = fi;
1510 for (i = 0; i < nh_grp->count; i++) {
1511 nh = &nh_grp->nexthops[i];
1512 fib_nh = &fi->fib_nh[i];
1513 err = mlxsw_sp_nexthop_init(mlxsw_sp, nh_grp, nh, fib_nh);
1515 goto err_nexthop_init;
1517 err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp);
1519 goto err_nexthop_group_insert;
1520 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
1523 err_nexthop_group_insert:
1525 for (i--; i >= 0; i--)
1526 mlxsw_sp_nexthop_fini(mlxsw_sp, nh);
1528 return ERR_PTR(err);
1532 mlxsw_sp_nexthop_group_destroy(struct mlxsw_sp *mlxsw_sp,
1533 struct mlxsw_sp_nexthop_group *nh_grp)
1535 struct mlxsw_sp_nexthop *nh;
1538 mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp);
1539 for (i = 0; i < nh_grp->count; i++) {
1540 nh = &nh_grp->nexthops[i];
1541 mlxsw_sp_nexthop_fini(mlxsw_sp, nh);
1543 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
1544 WARN_ON_ONCE(nh_grp->adj_index_valid);
1548 static int mlxsw_sp_nexthop_group_get(struct mlxsw_sp *mlxsw_sp,
1549 struct mlxsw_sp_fib_entry *fib_entry,
1550 struct fib_info *fi)
1552 struct mlxsw_sp_nexthop_group_key key;
1553 struct mlxsw_sp_nexthop_group *nh_grp;
1556 nh_grp = mlxsw_sp_nexthop_group_lookup(mlxsw_sp, key);
1558 nh_grp = mlxsw_sp_nexthop_group_create(mlxsw_sp, fi);
1560 return PTR_ERR(nh_grp);
1562 list_add_tail(&fib_entry->nexthop_group_node, &nh_grp->fib_list);
1563 fib_entry->nh_group = nh_grp;
1567 static void mlxsw_sp_nexthop_group_put(struct mlxsw_sp *mlxsw_sp,
1568 struct mlxsw_sp_fib_entry *fib_entry)
1570 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
1572 list_del(&fib_entry->nexthop_group_node);
1573 if (!list_empty(&nh_grp->fib_list))
1575 mlxsw_sp_nexthop_group_destroy(mlxsw_sp, nh_grp);
1578 static int mlxsw_sp_fib_entry_op4_remote(struct mlxsw_sp *mlxsw_sp,
1579 struct mlxsw_sp_fib_entry *fib_entry,
1580 enum mlxsw_reg_ralue_op op)
1582 char ralue_pl[MLXSW_REG_RALUE_LEN];
1583 u32 *p_dip = (u32 *) fib_entry->key.addr;
1584 struct mlxsw_sp_vr *vr = fib_entry->vr;
1585 enum mlxsw_reg_ralue_trap_action trap_action;
1587 u32 adjacency_index = 0;
1590 /* In case the nexthop group adjacency index is valid, use it
1591 * with provided ECMP size. Otherwise, setup trap and pass
1592 * traffic to kernel.
1594 if (fib_entry->nh_group->adj_index_valid) {
1595 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
1596 adjacency_index = fib_entry->nh_group->adj_index;
1597 ecmp_size = fib_entry->nh_group->ecmp_size;
1599 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
1600 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
1603 mlxsw_reg_ralue_pack4(ralue_pl,
1604 (enum mlxsw_reg_ralxx_protocol) vr->proto, op,
1605 vr->id, fib_entry->key.prefix_len, *p_dip);
1606 mlxsw_reg_ralue_act_remote_pack(ralue_pl, trap_action, trap_id,
1607 adjacency_index, ecmp_size);
1608 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1611 static int mlxsw_sp_fib_entry_op4_local(struct mlxsw_sp *mlxsw_sp,
1612 struct mlxsw_sp_fib_entry *fib_entry,
1613 enum mlxsw_reg_ralue_op op)
1615 char ralue_pl[MLXSW_REG_RALUE_LEN];
1616 u32 *p_dip = (u32 *) fib_entry->key.addr;
1617 struct mlxsw_sp_vr *vr = fib_entry->vr;
1619 mlxsw_reg_ralue_pack4(ralue_pl,
1620 (enum mlxsw_reg_ralxx_protocol) vr->proto, op,
1621 vr->id, fib_entry->key.prefix_len, *p_dip);
1622 mlxsw_reg_ralue_act_local_pack(ralue_pl,
1623 MLXSW_REG_RALUE_TRAP_ACTION_NOP, 0,
1625 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1628 static int mlxsw_sp_fib_entry_op4_trap(struct mlxsw_sp *mlxsw_sp,
1629 struct mlxsw_sp_fib_entry *fib_entry,
1630 enum mlxsw_reg_ralue_op op)
1632 char ralue_pl[MLXSW_REG_RALUE_LEN];
1633 u32 *p_dip = (u32 *) fib_entry->key.addr;
1634 struct mlxsw_sp_vr *vr = fib_entry->vr;
1636 mlxsw_reg_ralue_pack4(ralue_pl,
1637 (enum mlxsw_reg_ralxx_protocol) vr->proto, op,
1638 vr->id, fib_entry->key.prefix_len, *p_dip);
1639 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
1640 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1643 static int mlxsw_sp_fib_entry_op4(struct mlxsw_sp *mlxsw_sp,
1644 struct mlxsw_sp_fib_entry *fib_entry,
1645 enum mlxsw_reg_ralue_op op)
1647 switch (fib_entry->type) {
1648 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
1649 return mlxsw_sp_fib_entry_op4_remote(mlxsw_sp, fib_entry, op);
1650 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
1651 return mlxsw_sp_fib_entry_op4_local(mlxsw_sp, fib_entry, op);
1652 case MLXSW_SP_FIB_ENTRY_TYPE_TRAP:
1653 return mlxsw_sp_fib_entry_op4_trap(mlxsw_sp, fib_entry, op);
1658 static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
1659 struct mlxsw_sp_fib_entry *fib_entry,
1660 enum mlxsw_reg_ralue_op op)
1662 switch (fib_entry->vr->proto) {
1663 case MLXSW_SP_L3_PROTO_IPV4:
1664 return mlxsw_sp_fib_entry_op4(mlxsw_sp, fib_entry, op);
1665 case MLXSW_SP_L3_PROTO_IPV6:
1671 static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1672 struct mlxsw_sp_fib_entry *fib_entry)
1674 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
1675 MLXSW_REG_RALUE_OP_WRITE_WRITE);
1678 static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
1679 struct mlxsw_sp_fib_entry *fib_entry)
1681 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
1682 MLXSW_REG_RALUE_OP_WRITE_DELETE);
1686 mlxsw_sp_router_fib4_entry_init(struct mlxsw_sp *mlxsw_sp,
1687 const struct fib_entry_notifier_info *fen_info,
1688 struct mlxsw_sp_fib_entry *fib_entry)
1690 struct fib_info *fi = fen_info->fi;
1691 struct mlxsw_sp_rif *r = NULL;
1695 if (fen_info->type == RTN_LOCAL || fen_info->type == RTN_BROADCAST) {
1696 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
1699 if (fen_info->type != RTN_UNICAST)
1702 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1703 const struct fib_nh *nh = &fi->fib_nh[nhsel];
1707 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, nh->nh_dev);
1709 /* In case router interface is not found for
1710 * at least one of the nexthops, that means
1711 * the nexthop points to some device unrelated
1712 * to us. Set trap and pass the packets for
1713 * this prefix to kernel.
1720 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
1724 if (fi->fib_nh->nh_scope != RT_SCOPE_LINK) {
1725 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
1726 fib_entry->rif = r->rif;
1728 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
1729 err = mlxsw_sp_nexthop_group_get(mlxsw_sp, fib_entry, fi);
1733 fib_info_offload_inc(fen_info->fi);
1738 mlxsw_sp_router_fib4_entry_fini(struct mlxsw_sp *mlxsw_sp,
1739 struct mlxsw_sp_fib_entry *fib_entry)
1741 if (fib_entry->type != MLXSW_SP_FIB_ENTRY_TYPE_TRAP)
1742 fib_info_offload_dec(fib_entry->fi);
1743 if (fib_entry->type == MLXSW_SP_FIB_ENTRY_TYPE_REMOTE)
1744 mlxsw_sp_nexthop_group_put(mlxsw_sp, fib_entry);
1747 static struct mlxsw_sp_fib_entry *
1748 mlxsw_sp_fib_entry_get(struct mlxsw_sp *mlxsw_sp,
1749 const struct fib_entry_notifier_info *fen_info)
1751 struct mlxsw_sp_fib_entry *fib_entry;
1752 struct fib_info *fi = fen_info->fi;
1753 struct mlxsw_sp_vr *vr;
1756 vr = mlxsw_sp_vr_get(mlxsw_sp, fen_info->dst_len, fen_info->tb_id,
1757 MLXSW_SP_L3_PROTO_IPV4);
1759 return ERR_CAST(vr);
1761 fib_entry = mlxsw_sp_fib_entry_lookup(vr->fib, &fen_info->dst,
1762 sizeof(fen_info->dst),
1763 fen_info->dst_len, fi->fib_dev);
1765 /* Already exists, just take a reference */
1766 fib_entry->ref_count++;
1769 fib_entry = mlxsw_sp_fib_entry_create(vr->fib, &fen_info->dst,
1770 sizeof(fen_info->dst),
1771 fen_info->dst_len, fi->fib_dev);
1774 goto err_fib_entry_create;
1778 fib_entry->ref_count = 1;
1780 err = mlxsw_sp_router_fib4_entry_init(mlxsw_sp, fen_info, fib_entry);
1782 goto err_fib4_entry_init;
1786 err_fib4_entry_init:
1787 mlxsw_sp_fib_entry_destroy(fib_entry);
1788 err_fib_entry_create:
1789 mlxsw_sp_vr_put(mlxsw_sp, vr);
1791 return ERR_PTR(err);
1794 static struct mlxsw_sp_fib_entry *
1795 mlxsw_sp_fib_entry_find(struct mlxsw_sp *mlxsw_sp,
1796 const struct fib_entry_notifier_info *fen_info)
1798 struct mlxsw_sp_vr *vr;
1800 vr = mlxsw_sp_vr_find(mlxsw_sp, fen_info->tb_id,
1801 MLXSW_SP_L3_PROTO_IPV4);
1805 return mlxsw_sp_fib_entry_lookup(vr->fib, &fen_info->dst,
1806 sizeof(fen_info->dst),
1808 fen_info->fi->fib_dev);
1811 static void mlxsw_sp_fib_entry_put(struct mlxsw_sp *mlxsw_sp,
1812 struct mlxsw_sp_fib_entry *fib_entry)
1814 struct mlxsw_sp_vr *vr = fib_entry->vr;
1816 if (--fib_entry->ref_count == 0) {
1817 mlxsw_sp_router_fib4_entry_fini(mlxsw_sp, fib_entry);
1818 mlxsw_sp_fib_entry_destroy(fib_entry);
1820 mlxsw_sp_vr_put(mlxsw_sp, vr);
1823 static void mlxsw_sp_fib_entry_put_all(struct mlxsw_sp *mlxsw_sp,
1824 struct mlxsw_sp_fib_entry *fib_entry)
1826 unsigned int last_ref_count;
1829 last_ref_count = fib_entry->ref_count;
1830 mlxsw_sp_fib_entry_put(mlxsw_sp, fib_entry);
1831 } while (last_ref_count != 1);
1834 static int mlxsw_sp_router_fib4_add(struct mlxsw_sp *mlxsw_sp,
1835 struct fib_entry_notifier_info *fen_info)
1837 struct mlxsw_sp_fib_entry *fib_entry;
1838 struct mlxsw_sp_vr *vr;
1841 if (mlxsw_sp->router.aborted)
1844 fib_entry = mlxsw_sp_fib_entry_get(mlxsw_sp, fen_info);
1845 if (IS_ERR(fib_entry)) {
1846 dev_warn(mlxsw_sp->bus_info->dev, "Failed to get FIB4 entry being added.\n");
1847 return PTR_ERR(fib_entry);
1850 if (fib_entry->ref_count != 1)
1854 err = mlxsw_sp_fib_entry_insert(vr->fib, fib_entry);
1856 dev_warn(mlxsw_sp->bus_info->dev, "Failed to insert FIB4 entry being added.\n");
1857 goto err_fib_entry_insert;
1859 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1861 goto err_fib_entry_add;
1865 mlxsw_sp_fib_entry_remove(vr->fib, fib_entry);
1866 err_fib_entry_insert:
1867 mlxsw_sp_fib_entry_put(mlxsw_sp, fib_entry);
1871 static void mlxsw_sp_router_fib4_del(struct mlxsw_sp *mlxsw_sp,
1872 struct fib_entry_notifier_info *fen_info)
1874 struct mlxsw_sp_fib_entry *fib_entry;
1876 if (mlxsw_sp->router.aborted)
1879 fib_entry = mlxsw_sp_fib_entry_find(mlxsw_sp, fen_info);
1883 if (fib_entry->ref_count == 1) {
1884 mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry);
1885 mlxsw_sp_fib_entry_remove(fib_entry->vr->fib, fib_entry);
1888 mlxsw_sp_fib_entry_put(mlxsw_sp, fib_entry);
1891 static int mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp)
1893 char ralta_pl[MLXSW_REG_RALTA_LEN];
1894 char ralst_pl[MLXSW_REG_RALST_LEN];
1895 char raltb_pl[MLXSW_REG_RALTB_LEN];
1896 char ralue_pl[MLXSW_REG_RALUE_LEN];
1899 mlxsw_reg_ralta_pack(ralta_pl, true, MLXSW_REG_RALXX_PROTOCOL_IPV4,
1900 MLXSW_SP_LPM_TREE_MIN);
1901 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
1905 mlxsw_reg_ralst_pack(ralst_pl, 0xff, MLXSW_SP_LPM_TREE_MIN);
1906 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
1910 mlxsw_reg_raltb_pack(raltb_pl, 0, MLXSW_REG_RALXX_PROTOCOL_IPV4,
1911 MLXSW_SP_LPM_TREE_MIN);
1912 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
1916 mlxsw_reg_ralue_pack4(ralue_pl, MLXSW_SP_L3_PROTO_IPV4,
1917 MLXSW_REG_RALUE_OP_WRITE_WRITE, 0, 0, 0);
1918 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
1919 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1922 static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp)
1924 struct mlxsw_sp_fib_entry *fib_entry;
1925 struct mlxsw_sp_fib_entry *tmp;
1926 struct mlxsw_sp_vr *vr;
1929 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
1930 vr = &mlxsw_sp->router.vrs[i];
1935 list_for_each_entry_safe(fib_entry, tmp,
1936 &vr->fib->entry_list, list) {
1937 bool do_break = &tmp->list == &vr->fib->entry_list;
1939 mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry);
1940 mlxsw_sp_fib_entry_remove(fib_entry->vr->fib,
1942 mlxsw_sp_fib_entry_put_all(mlxsw_sp, fib_entry);
1949 static void mlxsw_sp_router_fib4_abort(struct mlxsw_sp *mlxsw_sp)
1953 if (mlxsw_sp->router.aborted)
1955 dev_warn(mlxsw_sp->bus_info->dev, "FIB abort triggered. Note that FIB entries are no longer being offloaded to this device.\n");
1956 mlxsw_sp_router_fib_flush(mlxsw_sp);
1957 mlxsw_sp->router.aborted = true;
1958 err = mlxsw_sp_router_set_abort_trap(mlxsw_sp);
1960 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set abort trap.\n");
1963 static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
1965 char rgcr_pl[MLXSW_REG_RGCR_LEN];
1969 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_RIFS))
1972 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
1973 mlxsw_sp->rifs = kcalloc(max_rifs, sizeof(struct mlxsw_sp_rif *),
1975 if (!mlxsw_sp->rifs)
1978 mlxsw_reg_rgcr_pack(rgcr_pl, true);
1979 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, max_rifs);
1980 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
1987 kfree(mlxsw_sp->rifs);
1991 static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
1993 char rgcr_pl[MLXSW_REG_RGCR_LEN];
1996 mlxsw_reg_rgcr_pack(rgcr_pl, false);
1997 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
1999 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
2000 WARN_ON_ONCE(mlxsw_sp->rifs[i]);
2002 kfree(mlxsw_sp->rifs);
2005 struct mlxsw_sp_fib_event_work {
2006 struct work_struct work;
2007 struct fib_entry_notifier_info fen_info;
2008 struct mlxsw_sp *mlxsw_sp;
2009 unsigned long event;
2012 static void mlxsw_sp_router_fib_event_work(struct work_struct *work)
2014 struct mlxsw_sp_fib_event_work *fib_work =
2015 container_of(work, struct mlxsw_sp_fib_event_work, work);
2016 struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp;
2019 /* Protect internal structures from changes */
2021 switch (fib_work->event) {
2022 case FIB_EVENT_ENTRY_ADD:
2023 err = mlxsw_sp_router_fib4_add(mlxsw_sp, &fib_work->fen_info);
2025 mlxsw_sp_router_fib4_abort(mlxsw_sp);
2026 fib_info_put(fib_work->fen_info.fi);
2028 case FIB_EVENT_ENTRY_DEL:
2029 mlxsw_sp_router_fib4_del(mlxsw_sp, &fib_work->fen_info);
2030 fib_info_put(fib_work->fen_info.fi);
2032 case FIB_EVENT_RULE_ADD: /* fall through */
2033 case FIB_EVENT_RULE_DEL:
2034 mlxsw_sp_router_fib4_abort(mlxsw_sp);
2041 /* Called with rcu_read_lock() */
2042 static int mlxsw_sp_router_fib_event(struct notifier_block *nb,
2043 unsigned long event, void *ptr)
2045 struct mlxsw_sp *mlxsw_sp = container_of(nb, struct mlxsw_sp, fib_nb);
2046 struct mlxsw_sp_fib_event_work *fib_work;
2047 struct fib_notifier_info *info = ptr;
2049 if (!net_eq(info->net, &init_net))
2052 fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
2053 if (WARN_ON(!fib_work))
2056 INIT_WORK(&fib_work->work, mlxsw_sp_router_fib_event_work);
2057 fib_work->mlxsw_sp = mlxsw_sp;
2058 fib_work->event = event;
2061 case FIB_EVENT_ENTRY_ADD: /* fall through */
2062 case FIB_EVENT_ENTRY_DEL:
2063 memcpy(&fib_work->fen_info, ptr, sizeof(fib_work->fen_info));
2064 /* Take referece on fib_info to prevent it from being
2065 * freed while work is queued. Release it afterwards.
2067 fib_info_hold(fib_work->fen_info.fi);
2071 mlxsw_core_schedule_work(&fib_work->work);
2076 static void mlxsw_sp_router_fib_dump_flush(struct notifier_block *nb)
2078 struct mlxsw_sp *mlxsw_sp = container_of(nb, struct mlxsw_sp, fib_nb);
2080 /* Flush pending FIB notifications and then flush the device's
2081 * table before requesting another dump. The FIB notification
2082 * block is unregistered, so no need to take RTNL.
2084 mlxsw_core_flush_owq();
2085 mlxsw_sp_router_fib_flush(mlxsw_sp);
2088 int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
2092 INIT_LIST_HEAD(&mlxsw_sp->router.nexthop_neighs_list);
2093 err = __mlxsw_sp_router_init(mlxsw_sp);
2097 err = rhashtable_init(&mlxsw_sp->router.nexthop_ht,
2098 &mlxsw_sp_nexthop_ht_params);
2100 goto err_nexthop_ht_init;
2102 err = rhashtable_init(&mlxsw_sp->router.nexthop_group_ht,
2103 &mlxsw_sp_nexthop_group_ht_params);
2105 goto err_nexthop_group_ht_init;
2107 mlxsw_sp_lpm_init(mlxsw_sp);
2108 err = mlxsw_sp_vrs_init(mlxsw_sp);
2112 err = mlxsw_sp_neigh_init(mlxsw_sp);
2114 goto err_neigh_init;
2116 mlxsw_sp->fib_nb.notifier_call = mlxsw_sp_router_fib_event;
2117 err = register_fib_notifier(&mlxsw_sp->fib_nb,
2118 mlxsw_sp_router_fib_dump_flush);
2120 goto err_register_fib_notifier;
2124 err_register_fib_notifier:
2125 mlxsw_sp_neigh_fini(mlxsw_sp);
2127 mlxsw_sp_vrs_fini(mlxsw_sp);
2129 rhashtable_destroy(&mlxsw_sp->router.nexthop_group_ht);
2130 err_nexthop_group_ht_init:
2131 rhashtable_destroy(&mlxsw_sp->router.nexthop_ht);
2132 err_nexthop_ht_init:
2133 __mlxsw_sp_router_fini(mlxsw_sp);
2137 void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
2139 unregister_fib_notifier(&mlxsw_sp->fib_nb);
2140 mlxsw_sp_neigh_fini(mlxsw_sp);
2141 mlxsw_sp_vrs_fini(mlxsw_sp);
2142 rhashtable_destroy(&mlxsw_sp->router.nexthop_group_ht);
2143 rhashtable_destroy(&mlxsw_sp->router.nexthop_ht);
2144 __mlxsw_sp_router_fini(mlxsw_sp);