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 <linux/inetdevice.h>
44 #include <linux/netdevice.h>
45 #include <net/netevent.h>
46 #include <net/neighbour.h>
48 #include <net/ip_fib.h>
49 #include <net/fib_rules.h>
50 #include <net/l3mdev.h>
55 #include "spectrum_cnt.h"
56 #include "spectrum_dpipe.h"
57 #include "spectrum_router.h"
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];
68 unsigned int counter_ingress;
69 bool counter_ingress_valid;
70 unsigned int counter_egress;
71 bool counter_egress_valid;
75 mlxsw_sp_rif_p_counter_get(struct mlxsw_sp_rif *rif,
76 enum mlxsw_sp_rif_counter_dir 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;
88 mlxsw_sp_rif_counter_valid_get(struct mlxsw_sp_rif *rif,
89 enum mlxsw_sp_rif_counter_dir 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;
101 mlxsw_sp_rif_counter_valid_set(struct mlxsw_sp_rif *rif,
102 enum mlxsw_sp_rif_counter_dir dir,
106 case MLXSW_SP_RIF_COUNTER_EGRESS:
107 rif->counter_egress_valid = valid;
109 case MLXSW_SP_RIF_COUNTER_INGRESS:
110 rif->counter_ingress_valid = valid;
115 static 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)
119 char ritr_pl[MLXSW_REG_RITR_LEN];
120 bool is_egress = false;
123 if (dir == MLXSW_SP_RIF_COUNTER_EGRESS)
125 mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
126 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
130 mlxsw_reg_ritr_counter_pack(ritr_pl, counter_index, enable,
132 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
135 int 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)
139 char ricnt_pl[MLXSW_REG_RICNT_LEN];
140 unsigned int *p_counter_index;
144 valid = mlxsw_sp_rif_counter_valid_get(rif, dir);
148 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
149 if (!p_counter_index)
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);
156 *cnt = mlxsw_reg_ricnt_good_unicast_packets_get(ricnt_pl);
160 static int mlxsw_sp_rif_counter_clear(struct mlxsw_sp *mlxsw_sp,
161 unsigned int counter_index)
163 char ricnt_pl[MLXSW_REG_RICNT_LEN];
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);
170 int mlxsw_sp_rif_counter_alloc(struct mlxsw_sp *mlxsw_sp,
171 struct mlxsw_sp_rif *rif,
172 enum mlxsw_sp_rif_counter_dir dir)
174 unsigned int *p_counter_index;
177 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
178 if (!p_counter_index)
180 err = mlxsw_sp_counter_alloc(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
185 err = mlxsw_sp_rif_counter_clear(mlxsw_sp, *p_counter_index);
187 goto err_counter_clear;
189 err = mlxsw_sp_rif_counter_edit(mlxsw_sp, rif->rif_index,
190 *p_counter_index, true, dir);
192 goto err_counter_edit;
193 mlxsw_sp_rif_counter_valid_set(rif, dir, true);
198 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
203 void mlxsw_sp_rif_counter_free(struct mlxsw_sp *mlxsw_sp,
204 struct mlxsw_sp_rif *rif,
205 enum mlxsw_sp_rif_counter_dir dir)
207 unsigned int *p_counter_index;
209 if (!mlxsw_sp_rif_counter_valid_get(rif, dir))
212 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
213 if (WARN_ON(!p_counter_index))
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,
219 mlxsw_sp_rif_counter_valid_set(rif, dir, false);
222 static struct mlxsw_sp_rif *
223 mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
224 const struct net_device *dev);
226 #define mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) \
227 for_each_set_bit(prefix, (prefix_usage)->b, MLXSW_SP_PREFIX_COUNT)
230 mlxsw_sp_prefix_usage_subset(struct mlxsw_sp_prefix_usage *prefix_usage1,
231 struct mlxsw_sp_prefix_usage *prefix_usage2)
233 unsigned char prefix;
235 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage1) {
236 if (!test_bit(prefix, prefix_usage2->b))
243 mlxsw_sp_prefix_usage_eq(struct mlxsw_sp_prefix_usage *prefix_usage1,
244 struct mlxsw_sp_prefix_usage *prefix_usage2)
246 return !memcmp(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
250 mlxsw_sp_prefix_usage_none(struct mlxsw_sp_prefix_usage *prefix_usage)
252 struct mlxsw_sp_prefix_usage prefix_usage_none = {{ 0 } };
254 return mlxsw_sp_prefix_usage_eq(prefix_usage, &prefix_usage_none);
258 mlxsw_sp_prefix_usage_cpy(struct mlxsw_sp_prefix_usage *prefix_usage1,
259 struct mlxsw_sp_prefix_usage *prefix_usage2)
261 memcpy(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
265 mlxsw_sp_prefix_usage_set(struct mlxsw_sp_prefix_usage *prefix_usage,
266 unsigned char prefix_len)
268 set_bit(prefix_len, prefix_usage->b);
272 mlxsw_sp_prefix_usage_clear(struct mlxsw_sp_prefix_usage *prefix_usage,
273 unsigned char prefix_len)
275 clear_bit(prefix_len, prefix_usage->b);
278 struct mlxsw_sp_fib_key {
279 unsigned char addr[sizeof(struct in6_addr)];
280 unsigned char prefix_len;
283 enum 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,
289 struct mlxsw_sp_nexthop_group;
291 struct mlxsw_sp_fib_node {
292 struct list_head entry_list;
293 struct list_head list;
294 struct rhash_head ht_node;
295 struct mlxsw_sp_fib *fib;
296 struct mlxsw_sp_fib_key key;
299 struct mlxsw_sp_fib_entry_params {
306 struct 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;
310 struct list_head nexthop_group_node;
311 struct mlxsw_sp_nexthop_group *nh_group;
312 struct mlxsw_sp_fib_entry_params params;
316 struct mlxsw_sp_fib {
317 struct rhashtable ht;
318 struct list_head node_list;
319 struct mlxsw_sp_vr *vr;
320 struct mlxsw_sp_lpm_tree *lpm_tree;
321 unsigned long prefix_ref_count[MLXSW_SP_PREFIX_COUNT];
322 struct mlxsw_sp_prefix_usage prefix_usage;
323 enum mlxsw_sp_l3proto proto;
326 static const struct rhashtable_params mlxsw_sp_fib_ht_params;
328 static struct mlxsw_sp_fib *mlxsw_sp_fib_create(struct mlxsw_sp_vr *vr,
329 enum mlxsw_sp_l3proto proto)
331 struct mlxsw_sp_fib *fib;
334 fib = kzalloc(sizeof(*fib), GFP_KERNEL);
336 return ERR_PTR(-ENOMEM);
337 err = rhashtable_init(&fib->ht, &mlxsw_sp_fib_ht_params);
339 goto err_rhashtable_init;
340 INIT_LIST_HEAD(&fib->node_list);
350 static void mlxsw_sp_fib_destroy(struct mlxsw_sp_fib *fib)
352 WARN_ON(!list_empty(&fib->node_list));
353 WARN_ON(fib->lpm_tree);
354 rhashtable_destroy(&fib->ht);
358 static struct mlxsw_sp_lpm_tree *
359 mlxsw_sp_lpm_tree_find_unused(struct mlxsw_sp *mlxsw_sp)
361 static struct mlxsw_sp_lpm_tree *lpm_tree;
364 for (i = 0; i < mlxsw_sp->router.lpm.tree_count; i++) {
365 lpm_tree = &mlxsw_sp->router.lpm.trees[i];
366 if (lpm_tree->ref_count == 0)
372 static int mlxsw_sp_lpm_tree_alloc(struct mlxsw_sp *mlxsw_sp,
373 struct mlxsw_sp_lpm_tree *lpm_tree)
375 char ralta_pl[MLXSW_REG_RALTA_LEN];
377 mlxsw_reg_ralta_pack(ralta_pl, true,
378 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
380 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
383 static int mlxsw_sp_lpm_tree_free(struct mlxsw_sp *mlxsw_sp,
384 struct mlxsw_sp_lpm_tree *lpm_tree)
386 char ralta_pl[MLXSW_REG_RALTA_LEN];
388 mlxsw_reg_ralta_pack(ralta_pl, false,
389 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
391 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
395 mlxsw_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)
399 char ralst_pl[MLXSW_REG_RALST_LEN];
402 u8 last_prefix = MLXSW_REG_RALST_BIN_NO_CHILD;
404 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage)
407 mlxsw_reg_ralst_pack(ralst_pl, root_bin, lpm_tree->id);
408 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) {
411 mlxsw_reg_ralst_bin_pack(ralst_pl, prefix, last_prefix,
412 MLXSW_REG_RALST_BIN_NO_CHILD);
413 last_prefix = prefix;
415 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
418 static struct mlxsw_sp_lpm_tree *
419 mlxsw_sp_lpm_tree_create(struct mlxsw_sp *mlxsw_sp,
420 struct mlxsw_sp_prefix_usage *prefix_usage,
421 enum mlxsw_sp_l3proto proto)
423 struct mlxsw_sp_lpm_tree *lpm_tree;
426 lpm_tree = mlxsw_sp_lpm_tree_find_unused(mlxsw_sp);
428 return ERR_PTR(-EBUSY);
429 lpm_tree->proto = proto;
430 err = mlxsw_sp_lpm_tree_alloc(mlxsw_sp, lpm_tree);
434 err = mlxsw_sp_lpm_tree_left_struct_set(mlxsw_sp, prefix_usage,
437 goto err_left_struct_set;
438 memcpy(&lpm_tree->prefix_usage, prefix_usage,
439 sizeof(lpm_tree->prefix_usage));
443 mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
447 static int mlxsw_sp_lpm_tree_destroy(struct mlxsw_sp *mlxsw_sp,
448 struct mlxsw_sp_lpm_tree *lpm_tree)
450 return mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
453 static struct mlxsw_sp_lpm_tree *
454 mlxsw_sp_lpm_tree_get(struct mlxsw_sp *mlxsw_sp,
455 struct mlxsw_sp_prefix_usage *prefix_usage,
456 enum mlxsw_sp_l3proto proto)
458 struct mlxsw_sp_lpm_tree *lpm_tree;
461 for (i = 0; i < mlxsw_sp->router.lpm.tree_count; i++) {
462 lpm_tree = &mlxsw_sp->router.lpm.trees[i];
463 if (lpm_tree->ref_count != 0 &&
464 lpm_tree->proto == proto &&
465 mlxsw_sp_prefix_usage_eq(&lpm_tree->prefix_usage,
469 lpm_tree = mlxsw_sp_lpm_tree_create(mlxsw_sp, prefix_usage,
471 if (IS_ERR(lpm_tree))
475 lpm_tree->ref_count++;
479 static int mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp,
480 struct mlxsw_sp_lpm_tree *lpm_tree)
482 if (--lpm_tree->ref_count == 0)
483 return mlxsw_sp_lpm_tree_destroy(mlxsw_sp, lpm_tree);
487 #define MLXSW_SP_LPM_TREE_MIN 2 /* trees 0 and 1 are reserved */
489 static int mlxsw_sp_lpm_init(struct mlxsw_sp *mlxsw_sp)
491 struct mlxsw_sp_lpm_tree *lpm_tree;
495 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LPM_TREES))
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),
503 if (!mlxsw_sp->router.lpm.trees)
506 for (i = 0; i < mlxsw_sp->router.lpm.tree_count; i++) {
507 lpm_tree = &mlxsw_sp->router.lpm.trees[i];
508 lpm_tree->id = i + MLXSW_SP_LPM_TREE_MIN;
514 static void mlxsw_sp_lpm_fini(struct mlxsw_sp *mlxsw_sp)
516 kfree(mlxsw_sp->router.lpm.trees);
519 static bool mlxsw_sp_vr_is_used(const struct mlxsw_sp_vr *vr)
524 static struct mlxsw_sp_vr *mlxsw_sp_vr_find_unused(struct mlxsw_sp *mlxsw_sp)
526 struct mlxsw_sp_vr *vr;
529 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
530 vr = &mlxsw_sp->router.vrs[i];
531 if (!mlxsw_sp_vr_is_used(vr))
537 static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp,
538 const struct mlxsw_sp_fib *fib)
540 char raltb_pl[MLXSW_REG_RALTB_LEN];
542 mlxsw_reg_raltb_pack(raltb_pl, fib->vr->id,
543 (enum mlxsw_reg_ralxx_protocol) fib->proto,
545 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
548 static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp,
549 const struct mlxsw_sp_fib *fib)
551 char raltb_pl[MLXSW_REG_RALTB_LEN];
553 /* Bind to tree 0 which is default */
554 mlxsw_reg_raltb_pack(raltb_pl, fib->vr->id,
555 (enum mlxsw_reg_ralxx_protocol) fib->proto, 0);
556 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
559 static u32 mlxsw_sp_fix_tb_id(u32 tb_id)
561 /* For our purpose, squash main and local table into one */
562 if (tb_id == RT_TABLE_LOCAL)
563 tb_id = RT_TABLE_MAIN;
567 static struct mlxsw_sp_vr *mlxsw_sp_vr_find(struct mlxsw_sp *mlxsw_sp,
570 struct mlxsw_sp_vr *vr;
573 tb_id = mlxsw_sp_fix_tb_id(tb_id);
575 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
576 vr = &mlxsw_sp->router.vrs[i];
577 if (mlxsw_sp_vr_is_used(vr) && vr->tb_id == tb_id)
583 static struct mlxsw_sp_fib *mlxsw_sp_vr_fib(const struct mlxsw_sp_vr *vr,
584 enum mlxsw_sp_l3proto proto)
587 case MLXSW_SP_L3_PROTO_IPV4:
589 case MLXSW_SP_L3_PROTO_IPV6:
595 static struct mlxsw_sp_vr *mlxsw_sp_vr_create(struct mlxsw_sp *mlxsw_sp,
598 struct mlxsw_sp_vr *vr;
600 vr = mlxsw_sp_vr_find_unused(mlxsw_sp);
602 return ERR_PTR(-EBUSY);
603 vr->fib4 = mlxsw_sp_fib_create(vr, MLXSW_SP_L3_PROTO_IPV4);
604 if (IS_ERR(vr->fib4))
605 return ERR_CAST(vr->fib4);
610 static void mlxsw_sp_vr_destroy(struct mlxsw_sp_vr *vr)
612 mlxsw_sp_fib_destroy(vr->fib4);
617 mlxsw_sp_vr_lpm_tree_check(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_fib *fib,
618 struct mlxsw_sp_prefix_usage *req_prefix_usage)
620 struct mlxsw_sp_lpm_tree *lpm_tree = fib->lpm_tree;
621 struct mlxsw_sp_lpm_tree *new_tree;
624 if (mlxsw_sp_prefix_usage_eq(req_prefix_usage, &lpm_tree->prefix_usage))
627 new_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, req_prefix_usage,
629 if (IS_ERR(new_tree)) {
630 /* 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
635 if (mlxsw_sp_prefix_usage_subset(req_prefix_usage,
636 &lpm_tree->prefix_usage))
638 return PTR_ERR(new_tree);
641 /* Prevent packet loss by overwriting existing binding */
642 fib->lpm_tree = new_tree;
643 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, fib);
646 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
651 fib->lpm_tree = lpm_tree;
652 mlxsw_sp_lpm_tree_put(mlxsw_sp, new_tree);
656 static struct mlxsw_sp_vr *mlxsw_sp_vr_get(struct mlxsw_sp *mlxsw_sp, u32 tb_id)
658 struct mlxsw_sp_vr *vr;
660 tb_id = mlxsw_sp_fix_tb_id(tb_id);
661 vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id);
663 vr = mlxsw_sp_vr_create(mlxsw_sp, tb_id);
667 static void mlxsw_sp_vr_put(struct mlxsw_sp_vr *vr)
669 if (!vr->rif_count && list_empty(&vr->fib4->node_list))
670 mlxsw_sp_vr_destroy(vr);
673 static int mlxsw_sp_vrs_init(struct mlxsw_sp *mlxsw_sp)
675 struct mlxsw_sp_vr *vr;
679 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_VRS))
682 max_vrs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS);
683 mlxsw_sp->router.vrs = kcalloc(max_vrs, sizeof(struct mlxsw_sp_vr),
685 if (!mlxsw_sp->router.vrs)
688 for (i = 0; i < max_vrs; i++) {
689 vr = &mlxsw_sp->router.vrs[i];
696 static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp);
698 static void mlxsw_sp_vrs_fini(struct mlxsw_sp *mlxsw_sp)
700 /* 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
707 mlxsw_core_flush_owq();
708 mlxsw_sp_router_fib_flush(mlxsw_sp);
709 kfree(mlxsw_sp->router.vrs);
712 struct mlxsw_sp_neigh_key {
716 struct mlxsw_sp_neigh_entry {
717 struct list_head rif_list_node;
718 struct rhash_head ht_node;
719 struct mlxsw_sp_neigh_key key;
722 unsigned char ha[ETH_ALEN];
723 struct list_head nexthop_list; /* list of nexthops using
726 struct list_head nexthop_neighs_list_node;
729 static 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),
735 static struct mlxsw_sp_neigh_entry *
736 mlxsw_sp_neigh_entry_alloc(struct mlxsw_sp *mlxsw_sp, struct neighbour *n,
739 struct mlxsw_sp_neigh_entry *neigh_entry;
741 neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_KERNEL);
745 neigh_entry->key.n = n;
746 neigh_entry->rif = rif;
747 INIT_LIST_HEAD(&neigh_entry->nexthop_list);
752 static void mlxsw_sp_neigh_entry_free(struct mlxsw_sp_neigh_entry *neigh_entry)
758 mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp,
759 struct mlxsw_sp_neigh_entry *neigh_entry)
761 return rhashtable_insert_fast(&mlxsw_sp->router.neigh_ht,
762 &neigh_entry->ht_node,
763 mlxsw_sp_neigh_ht_params);
767 mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp,
768 struct mlxsw_sp_neigh_entry *neigh_entry)
770 rhashtable_remove_fast(&mlxsw_sp->router.neigh_ht,
771 &neigh_entry->ht_node,
772 mlxsw_sp_neigh_ht_params);
775 static struct mlxsw_sp_neigh_entry *
776 mlxsw_sp_neigh_entry_create(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
778 struct mlxsw_sp_neigh_entry *neigh_entry;
779 struct mlxsw_sp_rif *rif;
782 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, n->dev);
784 return ERR_PTR(-EINVAL);
786 neigh_entry = mlxsw_sp_neigh_entry_alloc(mlxsw_sp, n, rif->rif_index);
788 return ERR_PTR(-ENOMEM);
790 err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
792 goto err_neigh_entry_insert;
794 list_add(&neigh_entry->rif_list_node, &rif->neigh_list);
798 err_neigh_entry_insert:
799 mlxsw_sp_neigh_entry_free(neigh_entry);
804 mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp *mlxsw_sp,
805 struct mlxsw_sp_neigh_entry *neigh_entry)
807 list_del(&neigh_entry->rif_list_node);
808 mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
809 mlxsw_sp_neigh_entry_free(neigh_entry);
812 static struct mlxsw_sp_neigh_entry *
813 mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
815 struct mlxsw_sp_neigh_key key;
818 return rhashtable_lookup_fast(&mlxsw_sp->router.neigh_ht,
819 &key, mlxsw_sp_neigh_ht_params);
823 mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp)
825 unsigned long interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME);
827 mlxsw_sp->router.neighs_update.interval = jiffies_to_msecs(interval);
830 static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
834 struct net_device *dev;
840 mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip);
842 if (!mlxsw_sp->rifs[rif]) {
843 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
848 dev = mlxsw_sp->rifs[rif]->dev;
849 n = neigh_lookup(&arp_tbl, &dipn, dev);
851 netdev_err(dev, "Failed to find matching neighbour for IP=%pI4h\n",
856 netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip);
857 neigh_event_send(n, NULL);
861 static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp,
868 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
870 /* Hardware starts counting at 0, so add 1. */
873 /* Each record consists of several neighbour entries. */
874 for (i = 0; i < num_entries; i++) {
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,
884 static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp,
885 char *rauhtd_pl, int rec_index)
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,
892 case MLXSW_REG_RAUHTD_TYPE_IPV6:
898 static bool mlxsw_sp_router_rauhtd_is_full(char *rauhtd_pl)
900 u8 num_rec, last_rec_index, num_entries;
902 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
903 last_rec_index = num_rec - 1;
905 if (num_rec < MLXSW_REG_RAUHTD_REC_MAX_NUM)
907 if (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, last_rec_index) ==
908 MLXSW_REG_RAUHTD_TYPE_IPV6)
911 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
913 if (++num_entries == MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC)
918 static int mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp)
924 rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL);
928 /* Make sure the neighbour's netdev isn't removed in the
933 mlxsw_reg_rauhtd_pack(rauhtd_pl, MLXSW_REG_RAUHTD_TYPE_IPV4);
934 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd),
937 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour talbe\n");
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,
944 } while (mlxsw_sp_router_rauhtd_is_full(rauhtd_pl));
951 static void mlxsw_sp_router_neighs_update_nh(struct mlxsw_sp *mlxsw_sp)
953 struct mlxsw_sp_neigh_entry *neigh_entry;
955 /* Take RTNL mutex here to prevent lists from changes */
957 list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list,
958 nexthop_neighs_list_node)
959 /* If this neigh have nexthops, make the kernel think this neigh
960 * is active regardless of the traffic.
962 neigh_event_send(neigh_entry->key.n, NULL);
967 mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp)
969 unsigned long interval = mlxsw_sp->router.neighs_update.interval;
971 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw,
972 msecs_to_jiffies(interval));
975 static void mlxsw_sp_router_neighs_update_work(struct work_struct *work)
977 struct mlxsw_sp *mlxsw_sp = container_of(work, struct mlxsw_sp,
978 router.neighs_update.dw.work);
981 err = mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp);
983 dev_err(mlxsw_sp->bus_info->dev, "Could not update kernel for neigh activity");
985 mlxsw_sp_router_neighs_update_nh(mlxsw_sp);
987 mlxsw_sp_router_neighs_update_work_schedule(mlxsw_sp);
990 static void mlxsw_sp_router_probe_unresolved_nexthops(struct work_struct *work)
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);
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.
1002 * Take RTNL mutex here to prevent lists from changes.
1005 list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list,
1006 nexthop_neighs_list_node)
1007 if (!neigh_entry->connected)
1008 neigh_event_send(neigh_entry->key.n, NULL);
1011 mlxsw_core_schedule_dw(&mlxsw_sp->router.nexthop_probe_dw,
1012 MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL);
1016 mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
1017 struct mlxsw_sp_neigh_entry *neigh_entry,
1020 static enum mlxsw_reg_rauht_op mlxsw_sp_rauht_op(bool adding)
1022 return adding ? MLXSW_REG_RAUHT_OP_WRITE_ADD :
1023 MLXSW_REG_RAUHT_OP_WRITE_DELETE;
1027 mlxsw_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)
1031 struct neighbour *n = neigh_entry->key.n;
1032 u32 dip = ntohl(*((__be32 *) n->primary_key));
1033 char rauht_pl[MLXSW_REG_RAUHT_LEN];
1035 mlxsw_reg_rauht_pack4(rauht_pl, op, neigh_entry->rif, neigh_entry->ha,
1037 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
1041 mlxsw_sp_neigh_entry_update(struct mlxsw_sp *mlxsw_sp,
1042 struct mlxsw_sp_neigh_entry *neigh_entry,
1045 if (!adding && !neigh_entry->connected)
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));
1055 struct mlxsw_sp_neigh_event_work {
1056 struct work_struct work;
1057 struct mlxsw_sp *mlxsw_sp;
1058 struct neighbour *n;
1061 static void mlxsw_sp_router_neigh_event_work(struct work_struct *work)
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];
1069 bool entry_connected;
1072 /* If these parameters are changed after we release the lock,
1073 * then we are guaranteed to receive another event letting us
1076 read_lock_bh(&n->lock);
1077 memcpy(ha, n->ha, ETH_ALEN);
1078 nud_state = n->nud_state;
1080 read_unlock_bh(&n->lock);
1083 entry_connected = nud_state & NUD_VALID && !dead;
1084 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
1085 if (!entry_connected && !neigh_entry)
1088 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
1089 if (IS_ERR(neigh_entry))
1093 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);
1097 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
1098 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
1106 int mlxsw_sp_router_netevent_event(struct notifier_block *unused,
1107 unsigned long event, void *ptr)
1109 struct mlxsw_sp_neigh_event_work *neigh_work;
1110 struct mlxsw_sp_port *mlxsw_sp_port;
1111 struct mlxsw_sp *mlxsw_sp;
1112 unsigned long interval;
1113 struct neigh_parms *p;
1114 struct neighbour *n;
1117 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
1120 /* We don't care about changes in the default table. */
1121 if (!p->dev || p->tbl != &arp_tbl)
1124 /* We are in atomic context and can't take RTNL mutex,
1125 * so use RCU variant to walk the device chain.
1127 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev);
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;
1135 mlxsw_sp_port_dev_put(mlxsw_sp_port);
1137 case NETEVENT_NEIGH_UPDATE:
1140 if (n->tbl != &arp_tbl)
1143 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(n->dev);
1147 neigh_work = kzalloc(sizeof(*neigh_work), GFP_ATOMIC);
1149 mlxsw_sp_port_dev_put(mlxsw_sp_port);
1153 INIT_WORK(&neigh_work->work, mlxsw_sp_router_neigh_event_work);
1154 neigh_work->mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1157 /* Take a reference to ensure the neighbour won't be
1158 * destructed until we drop the reference in delayed
1162 mlxsw_core_schedule_work(&neigh_work->work);
1163 mlxsw_sp_port_dev_put(mlxsw_sp_port);
1170 static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp)
1174 err = rhashtable_init(&mlxsw_sp->router.neigh_ht,
1175 &mlxsw_sp_neigh_ht_params);
1179 /* Initialize the polling interval according to the default
1182 mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp);
1184 /* Create the delayed works for the activity_update */
1185 INIT_DELAYED_WORK(&mlxsw_sp->router.neighs_update.dw,
1186 mlxsw_sp_router_neighs_update_work);
1187 INIT_DELAYED_WORK(&mlxsw_sp->router.nexthop_probe_dw,
1188 mlxsw_sp_router_probe_unresolved_nexthops);
1189 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw, 0);
1190 mlxsw_core_schedule_dw(&mlxsw_sp->router.nexthop_probe_dw, 0);
1194 static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp)
1196 cancel_delayed_work_sync(&mlxsw_sp->router.neighs_update.dw);
1197 cancel_delayed_work_sync(&mlxsw_sp->router.nexthop_probe_dw);
1198 rhashtable_destroy(&mlxsw_sp->router.neigh_ht);
1201 static int mlxsw_sp_neigh_rif_flush(struct mlxsw_sp *mlxsw_sp,
1202 const struct mlxsw_sp_rif *rif)
1204 char rauht_pl[MLXSW_REG_RAUHT_LEN];
1206 mlxsw_reg_rauht_pack(rauht_pl, MLXSW_REG_RAUHT_OP_WRITE_DELETE_ALL,
1207 rif->rif_index, rif->addr);
1208 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
1211 static void mlxsw_sp_neigh_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
1212 struct mlxsw_sp_rif *rif)
1214 struct mlxsw_sp_neigh_entry *neigh_entry, *tmp;
1216 mlxsw_sp_neigh_rif_flush(mlxsw_sp, rif);
1217 list_for_each_entry_safe(neigh_entry, tmp, &rif->neigh_list,
1219 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
1222 struct mlxsw_sp_nexthop_key {
1223 struct fib_nh *fib_nh;
1226 struct mlxsw_sp_nexthop {
1227 struct list_head neigh_list_node; /* member of neigh entry list */
1228 struct list_head rif_list_node;
1229 struct mlxsw_sp_nexthop_group *nh_grp; /* pointer back to the group
1232 struct rhash_head ht_node;
1233 struct mlxsw_sp_nexthop_key key;
1234 struct mlxsw_sp_rif *rif;
1235 u8 should_offload:1, /* set indicates this neigh is connected and
1236 * should be put to KVD linear area of this group.
1238 offloaded:1, /* set in case the neigh is actually put into
1239 * KVD linear area of this group.
1241 update:1; /* set indicates that MAC of this neigh should be
1244 struct mlxsw_sp_neigh_entry *neigh_entry;
1247 struct mlxsw_sp_nexthop_group_key {
1248 struct fib_info *fi;
1251 struct mlxsw_sp_nexthop_group {
1252 struct rhash_head ht_node;
1253 struct list_head fib_list; /* list of fib entries that use this group */
1254 struct mlxsw_sp_nexthop_group_key key;
1255 u8 adj_index_valid:1,
1256 gateway:1; /* routes using the group use a gateway */
1260 struct mlxsw_sp_nexthop nexthops[0];
1261 #define nh_rif nexthops[0].rif
1264 static 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),
1270 static int mlxsw_sp_nexthop_group_insert(struct mlxsw_sp *mlxsw_sp,
1271 struct mlxsw_sp_nexthop_group *nh_grp)
1273 return rhashtable_insert_fast(&mlxsw_sp->router.nexthop_group_ht,
1275 mlxsw_sp_nexthop_group_ht_params);
1278 static void mlxsw_sp_nexthop_group_remove(struct mlxsw_sp *mlxsw_sp,
1279 struct mlxsw_sp_nexthop_group *nh_grp)
1281 rhashtable_remove_fast(&mlxsw_sp->router.nexthop_group_ht,
1283 mlxsw_sp_nexthop_group_ht_params);
1286 static struct mlxsw_sp_nexthop_group *
1287 mlxsw_sp_nexthop_group_lookup(struct mlxsw_sp *mlxsw_sp,
1288 struct mlxsw_sp_nexthop_group_key key)
1290 return rhashtable_lookup_fast(&mlxsw_sp->router.nexthop_group_ht, &key,
1291 mlxsw_sp_nexthop_group_ht_params);
1294 static 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),
1300 static int mlxsw_sp_nexthop_insert(struct mlxsw_sp *mlxsw_sp,
1301 struct mlxsw_sp_nexthop *nh)
1303 return rhashtable_insert_fast(&mlxsw_sp->router.nexthop_ht,
1304 &nh->ht_node, mlxsw_sp_nexthop_ht_params);
1307 static void mlxsw_sp_nexthop_remove(struct mlxsw_sp *mlxsw_sp,
1308 struct mlxsw_sp_nexthop *nh)
1310 rhashtable_remove_fast(&mlxsw_sp->router.nexthop_ht, &nh->ht_node,
1311 mlxsw_sp_nexthop_ht_params);
1314 static struct mlxsw_sp_nexthop *
1315 mlxsw_sp_nexthop_lookup(struct mlxsw_sp *mlxsw_sp,
1316 struct mlxsw_sp_nexthop_key key)
1318 return rhashtable_lookup_fast(&mlxsw_sp->router.nexthop_ht, &key,
1319 mlxsw_sp_nexthop_ht_params);
1322 static int mlxsw_sp_adj_index_mass_update_vr(struct mlxsw_sp *mlxsw_sp,
1323 const struct mlxsw_sp_fib *fib,
1324 u32 adj_index, u16 ecmp_size,
1328 char raleu_pl[MLXSW_REG_RALEU_LEN];
1330 mlxsw_reg_raleu_pack(raleu_pl,
1331 (enum mlxsw_reg_ralxx_protocol) fib->proto,
1332 fib->vr->id, adj_index, ecmp_size, new_adj_index,
1334 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raleu), raleu_pl);
1337 static 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)
1341 struct mlxsw_sp_fib_entry *fib_entry;
1342 struct mlxsw_sp_fib *fib = NULL;
1345 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
1346 if (fib == fib_entry->fib_node->fib)
1348 fib = fib_entry->fib_node->fib;
1349 err = mlxsw_sp_adj_index_mass_update_vr(mlxsw_sp, fib,
1360 static int mlxsw_sp_nexthop_mac_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
1361 struct mlxsw_sp_nexthop *nh)
1363 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
1364 char ratr_pl[MLXSW_REG_RATR_LEN];
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);
1373 mlxsw_sp_nexthop_group_mac_update(struct mlxsw_sp *mlxsw_sp,
1374 struct mlxsw_sp_nexthop_group *nh_grp,
1377 u32 adj_index = nh_grp->adj_index; /* base */
1378 struct mlxsw_sp_nexthop *nh;
1382 for (i = 0; i < nh_grp->count; i++) {
1383 nh = &nh_grp->nexthops[i];
1385 if (!nh->should_offload) {
1390 if (nh->update || reallocate) {
1391 err = mlxsw_sp_nexthop_mac_update(mlxsw_sp,
1403 static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1404 struct mlxsw_sp_fib_entry *fib_entry);
1407 mlxsw_sp_nexthop_fib_entries_update(struct mlxsw_sp *mlxsw_sp,
1408 struct mlxsw_sp_nexthop_group *nh_grp)
1410 struct mlxsw_sp_fib_entry *fib_entry;
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);
1422 mlxsw_sp_nexthop_group_refresh(struct mlxsw_sp *mlxsw_sp,
1423 struct mlxsw_sp_nexthop_group *nh_grp)
1425 struct mlxsw_sp_nexthop *nh;
1426 bool offload_change = false;
1429 bool old_adj_index_valid;
1435 if (!nh_grp->gateway) {
1436 mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1440 for (i = 0; i < nh_grp->count; i++) {
1441 nh = &nh_grp->nexthops[i];
1443 if (nh->should_offload ^ nh->offloaded) {
1444 offload_change = true;
1445 if (nh->should_offload)
1448 if (nh->should_offload)
1451 if (!offload_change) {
1452 /* Nothing was added or removed, so no need to reallocate. Just
1453 * update MAC on existing adjacency indexes.
1455 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp,
1458 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1464 /* No neigh of this group is connected so we just set
1465 * the trap and let everthing flow through kernel.
1469 err = mlxsw_sp_kvdl_alloc(mlxsw_sp, ecmp_size, &adj_index);
1471 /* We ran out of KVD linear space, just set the
1472 * trap and let everything flow through kernel.
1474 dev_warn(mlxsw_sp->bus_info->dev, "Failed to allocate KVD linear area for nexthop group.\n");
1477 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;
1483 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp, true);
1485 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
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.
1493 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1495 dev_warn(mlxsw_sp->bus_info->dev, "Failed to add adjacency index to fib entries.\n");
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);
1505 dev_warn(mlxsw_sp->bus_info->dev, "Failed to mass-update adjacency index for nexthop group.\n");
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];
1517 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
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);
1524 static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop *nh,
1527 if (!removing && !nh->should_offload)
1528 nh->should_offload = 1;
1529 else if (removing && nh->offloaded)
1530 nh->should_offload = 0;
1535 mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
1536 struct mlxsw_sp_neigh_entry *neigh_entry,
1539 struct mlxsw_sp_nexthop *nh;
1541 list_for_each_entry(nh, &neigh_entry->nexthop_list,
1543 __mlxsw_sp_nexthop_neigh_update(nh, removing);
1544 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1548 static void mlxsw_sp_nexthop_rif_init(struct mlxsw_sp_nexthop *nh,
1549 struct mlxsw_sp_rif *rif)
1555 list_add(&nh->rif_list_node, &rif->nexthop_list);
1558 static void mlxsw_sp_nexthop_rif_fini(struct mlxsw_sp_nexthop *nh)
1563 list_del(&nh->rif_list_node);
1567 static int mlxsw_sp_nexthop_neigh_init(struct mlxsw_sp *mlxsw_sp,
1568 struct mlxsw_sp_nexthop *nh)
1570 struct mlxsw_sp_neigh_entry *neigh_entry;
1571 struct fib_nh *fib_nh = nh->key.fib_nh;
1572 struct neighbour *n;
1576 if (!nh->nh_grp->gateway || nh->neigh_entry)
1579 /* 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
1582 * in neigh_create() in case n is not found.
1584 n = neigh_lookup(&arp_tbl, &fib_nh->nh_gw, fib_nh->nh_dev);
1586 n = neigh_create(&arp_tbl, &fib_nh->nh_gw, fib_nh->nh_dev);
1589 neigh_event_send(n, NULL);
1591 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
1593 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
1594 if (IS_ERR(neigh_entry)) {
1596 goto err_neigh_entry_create;
1600 /* If that is the first nexthop connected to that neigh, add to
1601 * nexthop_neighs_list
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);
1607 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;
1612 read_unlock_bh(&n->lock);
1613 __mlxsw_sp_nexthop_neigh_update(nh, !(nud_state & NUD_VALID && !dead));
1617 err_neigh_entry_create:
1622 static void mlxsw_sp_nexthop_neigh_fini(struct mlxsw_sp *mlxsw_sp,
1623 struct mlxsw_sp_nexthop *nh)
1625 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
1626 struct neighbour *n;
1630 n = neigh_entry->key.n;
1632 __mlxsw_sp_nexthop_neigh_update(nh, true);
1633 list_del(&nh->neigh_list_node);
1634 nh->neigh_entry = NULL;
1636 /* If that is the last nexthop connected to that neigh, remove from
1637 * nexthop_neighs_list
1639 if (list_empty(&neigh_entry->nexthop_list))
1640 list_del(&neigh_entry->nexthop_neighs_list_node);
1642 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
1643 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
1648 static 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)
1653 struct net_device *dev = fib_nh->nh_dev;
1654 struct in_device *in_dev;
1655 struct mlxsw_sp_rif *rif;
1658 nh->nh_grp = nh_grp;
1659 nh->key.fib_nh = fib_nh;
1660 err = mlxsw_sp_nexthop_insert(mlxsw_sp, nh);
1667 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)
1672 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
1675 mlxsw_sp_nexthop_rif_init(nh, rif);
1677 err = mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
1679 goto err_nexthop_neigh_init;
1683 err_nexthop_neigh_init:
1684 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
1688 static void mlxsw_sp_nexthop_fini(struct mlxsw_sp *mlxsw_sp,
1689 struct mlxsw_sp_nexthop *nh)
1691 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
1692 mlxsw_sp_nexthop_rif_fini(nh);
1693 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
1696 static void mlxsw_sp_nexthop_event(struct mlxsw_sp *mlxsw_sp,
1697 unsigned long event, struct fib_nh *fib_nh)
1699 struct mlxsw_sp_nexthop_key key;
1700 struct mlxsw_sp_nexthop *nh;
1701 struct mlxsw_sp_rif *rif;
1703 if (mlxsw_sp->router.aborted)
1706 key.fib_nh = fib_nh;
1707 nh = mlxsw_sp_nexthop_lookup(mlxsw_sp, key);
1708 if (WARN_ON_ONCE(!nh))
1711 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, fib_nh->nh_dev);
1716 case FIB_EVENT_NH_ADD:
1717 mlxsw_sp_nexthop_rif_init(nh, rif);
1718 mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
1720 case FIB_EVENT_NH_DEL:
1721 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
1722 mlxsw_sp_nexthop_rif_fini(nh);
1726 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1729 static void mlxsw_sp_nexthop_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
1730 struct mlxsw_sp_rif *rif)
1732 struct mlxsw_sp_nexthop *nh, *tmp;
1734 list_for_each_entry_safe(nh, tmp, &rif->nexthop_list, rif_list_node) {
1735 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);
1741 static struct mlxsw_sp_nexthop_group *
1742 mlxsw_sp_nexthop_group_create(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
1744 struct mlxsw_sp_nexthop_group *nh_grp;
1745 struct mlxsw_sp_nexthop *nh;
1746 struct fib_nh *fib_nh;
1751 alloc_size = sizeof(*nh_grp) +
1752 fi->fib_nhs * sizeof(struct mlxsw_sp_nexthop);
1753 nh_grp = kzalloc(alloc_size, GFP_KERNEL);
1755 return ERR_PTR(-ENOMEM);
1756 INIT_LIST_HEAD(&nh_grp->fib_list);
1757 nh_grp->gateway = fi->fib_nh->nh_scope == RT_SCOPE_LINK;
1758 nh_grp->count = fi->fib_nhs;
1759 nh_grp->key.fi = fi;
1760 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);
1765 goto err_nexthop_init;
1767 err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp);
1769 goto err_nexthop_group_insert;
1770 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
1773 err_nexthop_group_insert:
1775 for (i--; i >= 0; i--) {
1776 nh = &nh_grp->nexthops[i];
1777 mlxsw_sp_nexthop_fini(mlxsw_sp, nh);
1780 return ERR_PTR(err);
1784 mlxsw_sp_nexthop_group_destroy(struct mlxsw_sp *mlxsw_sp,
1785 struct mlxsw_sp_nexthop_group *nh_grp)
1787 struct mlxsw_sp_nexthop *nh;
1790 mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp);
1791 for (i = 0; i < nh_grp->count; i++) {
1792 nh = &nh_grp->nexthops[i];
1793 mlxsw_sp_nexthop_fini(mlxsw_sp, nh);
1795 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
1796 WARN_ON_ONCE(nh_grp->adj_index_valid);
1800 static int mlxsw_sp_nexthop_group_get(struct mlxsw_sp *mlxsw_sp,
1801 struct mlxsw_sp_fib_entry *fib_entry,
1802 struct fib_info *fi)
1804 struct mlxsw_sp_nexthop_group_key key;
1805 struct mlxsw_sp_nexthop_group *nh_grp;
1808 nh_grp = mlxsw_sp_nexthop_group_lookup(mlxsw_sp, key);
1810 nh_grp = mlxsw_sp_nexthop_group_create(mlxsw_sp, fi);
1812 return PTR_ERR(nh_grp);
1814 list_add_tail(&fib_entry->nexthop_group_node, &nh_grp->fib_list);
1815 fib_entry->nh_group = nh_grp;
1819 static void mlxsw_sp_nexthop_group_put(struct mlxsw_sp *mlxsw_sp,
1820 struct mlxsw_sp_fib_entry *fib_entry)
1822 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
1824 list_del(&fib_entry->nexthop_group_node);
1825 if (!list_empty(&nh_grp->fib_list))
1827 mlxsw_sp_nexthop_group_destroy(mlxsw_sp, nh_grp);
1831 mlxsw_sp_fib_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry)
1833 struct mlxsw_sp_nexthop_group *nh_group = fib_entry->nh_group;
1835 if (fib_entry->params.tos)
1838 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:
1842 return !!nh_group->nh_rif;
1848 static void mlxsw_sp_fib_entry_offload_set(struct mlxsw_sp_fib_entry *fib_entry)
1850 fib_entry->offloaded = true;
1852 switch (fib_entry->fib_node->fib->proto) {
1853 case MLXSW_SP_L3_PROTO_IPV4:
1854 fib_info_offload_inc(fib_entry->nh_group->key.fi);
1856 case MLXSW_SP_L3_PROTO_IPV6:
1862 mlxsw_sp_fib_entry_offload_unset(struct mlxsw_sp_fib_entry *fib_entry)
1864 switch (fib_entry->fib_node->fib->proto) {
1865 case MLXSW_SP_L3_PROTO_IPV4:
1866 fib_info_offload_dec(fib_entry->nh_group->key.fi);
1868 case MLXSW_SP_L3_PROTO_IPV6:
1872 fib_entry->offloaded = false;
1876 mlxsw_sp_fib_entry_offload_refresh(struct mlxsw_sp_fib_entry *fib_entry,
1877 enum mlxsw_reg_ralue_op op, int err)
1880 case MLXSW_REG_RALUE_OP_WRITE_DELETE:
1881 if (!fib_entry->offloaded)
1883 return mlxsw_sp_fib_entry_offload_unset(fib_entry);
1884 case MLXSW_REG_RALUE_OP_WRITE_WRITE:
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);
1899 static 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)
1903 char ralue_pl[MLXSW_REG_RALUE_LEN];
1904 struct mlxsw_sp_fib *fib = fib_entry->fib_node->fib;
1905 u32 *p_dip = (u32 *) fib_entry->fib_node->key.addr;
1906 enum mlxsw_reg_ralue_trap_action trap_action;
1908 u32 adjacency_index = 0;
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.
1915 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
1916 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;
1920 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
1921 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
1924 mlxsw_reg_ralue_pack4(ralue_pl,
1925 (enum mlxsw_reg_ralxx_protocol) fib->proto, op,
1926 fib->vr->id, fib_entry->fib_node->key.prefix_len,
1928 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);
1933 static 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)
1937 struct mlxsw_sp_rif *rif = fib_entry->nh_group->nh_rif;
1938 struct mlxsw_sp_fib *fib = fib_entry->fib_node->fib;
1939 enum mlxsw_reg_ralue_trap_action trap_action;
1940 char ralue_pl[MLXSW_REG_RALUE_LEN];
1941 u32 *p_dip = (u32 *) fib_entry->fib_node->key.addr;
1945 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
1946 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
1947 rif_index = rif->rif_index;
1949 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
1950 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
1953 mlxsw_reg_ralue_pack4(ralue_pl,
1954 (enum mlxsw_reg_ralxx_protocol) fib->proto, op,
1955 fib->vr->id, fib_entry->fib_node->key.prefix_len,
1957 mlxsw_reg_ralue_act_local_pack(ralue_pl, trap_action, trap_id,
1959 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1962 static 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)
1966 struct mlxsw_sp_fib *fib = fib_entry->fib_node->fib;
1967 char ralue_pl[MLXSW_REG_RALUE_LEN];
1968 u32 *p_dip = (u32 *) fib_entry->fib_node->key.addr;
1970 mlxsw_reg_ralue_pack4(ralue_pl,
1971 (enum mlxsw_reg_ralxx_protocol) fib->proto, op,
1972 fib->vr->id, fib_entry->fib_node->key.prefix_len,
1974 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
1975 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1978 static 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)
1982 switch (fib_entry->type) {
1983 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
1984 return mlxsw_sp_fib_entry_op4_remote(mlxsw_sp, fib_entry, op);
1985 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);
1993 static 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)
1999 switch (fib_entry->fib_node->fib->proto) {
2000 case MLXSW_SP_L3_PROTO_IPV4:
2001 err = mlxsw_sp_fib_entry_op4(mlxsw_sp, fib_entry, op);
2003 case MLXSW_SP_L3_PROTO_IPV6:
2006 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, err);
2010 static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
2011 struct mlxsw_sp_fib_entry *fib_entry)
2013 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
2014 MLXSW_REG_RALUE_OP_WRITE_WRITE);
2017 static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
2018 struct mlxsw_sp_fib_entry *fib_entry)
2020 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
2021 MLXSW_REG_RALUE_OP_WRITE_DELETE);
2025 mlxsw_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)
2029 struct fib_info *fi = fen_info->fi;
2031 switch (fen_info->type) {
2032 case RTN_BROADCAST: /* fall through */
2034 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
2036 case RTN_UNREACHABLE: /* fall through */
2037 case RTN_BLACKHOLE: /* fall through */
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.
2043 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
2046 if (fi->fib_nh->nh_scope != RT_SCOPE_LINK)
2047 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
2049 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
2056 static struct mlxsw_sp_fib_entry *
2057 mlxsw_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)
2061 struct mlxsw_sp_fib_entry *fib_entry;
2064 fib_entry = kzalloc(sizeof(*fib_entry), GFP_KERNEL);
2067 goto err_fib_entry_alloc;
2070 err = mlxsw_sp_fib4_entry_type_set(mlxsw_sp, fen_info, fib_entry);
2072 goto err_fib4_entry_type_set;
2074 err = mlxsw_sp_nexthop_group_get(mlxsw_sp, fib_entry, fen_info->fi);
2076 goto err_nexthop_group_get;
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;
2083 fib_entry->fib_node = fib_node;
2087 err_nexthop_group_get:
2088 err_fib4_entry_type_set:
2090 err_fib_entry_alloc:
2091 return ERR_PTR(err);
2094 static void mlxsw_sp_fib4_entry_destroy(struct mlxsw_sp *mlxsw_sp,
2095 struct mlxsw_sp_fib_entry *fib_entry)
2097 mlxsw_sp_nexthop_group_put(mlxsw_sp, fib_entry);
2101 static struct mlxsw_sp_fib_node *
2102 mlxsw_sp_fib4_node_get(struct mlxsw_sp *mlxsw_sp,
2103 const struct fib_entry_notifier_info *fen_info);
2105 static struct mlxsw_sp_fib_entry *
2106 mlxsw_sp_fib4_entry_lookup(struct mlxsw_sp *mlxsw_sp,
2107 const struct fib_entry_notifier_info *fen_info)
2109 struct mlxsw_sp_fib_entry *fib_entry;
2110 struct mlxsw_sp_fib_node *fib_node;
2112 fib_node = mlxsw_sp_fib4_node_get(mlxsw_sp, fen_info);
2113 if (IS_ERR(fib_node))
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) {
2128 static 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,
2135 static int mlxsw_sp_fib_node_insert(struct mlxsw_sp_fib *fib,
2136 struct mlxsw_sp_fib_node *fib_node)
2138 return rhashtable_insert_fast(&fib->ht, &fib_node->ht_node,
2139 mlxsw_sp_fib_ht_params);
2142 static void mlxsw_sp_fib_node_remove(struct mlxsw_sp_fib *fib,
2143 struct mlxsw_sp_fib_node *fib_node)
2145 rhashtable_remove_fast(&fib->ht, &fib_node->ht_node,
2146 mlxsw_sp_fib_ht_params);
2149 static struct mlxsw_sp_fib_node *
2150 mlxsw_sp_fib_node_lookup(struct mlxsw_sp_fib *fib, const void *addr,
2151 size_t addr_len, unsigned char prefix_len)
2153 struct mlxsw_sp_fib_key key;
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);
2161 static struct mlxsw_sp_fib_node *
2162 mlxsw_sp_fib_node_create(struct mlxsw_sp_fib *fib, const void *addr,
2163 size_t addr_len, unsigned char prefix_len)
2165 struct mlxsw_sp_fib_node *fib_node;
2167 fib_node = kzalloc(sizeof(*fib_node), GFP_KERNEL);
2171 INIT_LIST_HEAD(&fib_node->entry_list);
2172 list_add(&fib_node->list, &fib->node_list);
2173 memcpy(fib_node->key.addr, addr, addr_len);
2174 fib_node->key.prefix_len = prefix_len;
2179 static void mlxsw_sp_fib_node_destroy(struct mlxsw_sp_fib_node *fib_node)
2181 list_del(&fib_node->list);
2182 WARN_ON(!list_empty(&fib_node->entry_list));
2187 mlxsw_sp_fib_node_entry_is_first(const struct mlxsw_sp_fib_node *fib_node,
2188 const struct mlxsw_sp_fib_entry *fib_entry)
2190 return list_first_entry(&fib_node->entry_list,
2191 struct mlxsw_sp_fib_entry, list) == fib_entry;
2194 static void mlxsw_sp_fib_node_prefix_inc(struct mlxsw_sp_fib_node *fib_node)
2196 unsigned char prefix_len = fib_node->key.prefix_len;
2197 struct mlxsw_sp_fib *fib = fib_node->fib;
2199 if (fib->prefix_ref_count[prefix_len]++ == 0)
2200 mlxsw_sp_prefix_usage_set(&fib->prefix_usage, prefix_len);
2203 static void mlxsw_sp_fib_node_prefix_dec(struct mlxsw_sp_fib_node *fib_node)
2205 unsigned char prefix_len = fib_node->key.prefix_len;
2206 struct mlxsw_sp_fib *fib = fib_node->fib;
2208 if (--fib->prefix_ref_count[prefix_len] == 0)
2209 mlxsw_sp_prefix_usage_clear(&fib->prefix_usage, prefix_len);
2212 static 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)
2216 struct mlxsw_sp_prefix_usage req_prefix_usage;
2217 struct mlxsw_sp_lpm_tree *lpm_tree;
2220 err = mlxsw_sp_fib_node_insert(fib, fib_node);
2223 fib_node->fib = fib;
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);
2228 if (!mlxsw_sp_prefix_usage_none(&fib->prefix_usage)) {
2229 err = mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, fib,
2232 goto err_tree_check;
2234 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
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);
2244 mlxsw_sp_fib_node_prefix_inc(fib_node);
2249 fib->lpm_tree = NULL;
2250 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
2252 fib_node->fib = NULL;
2253 mlxsw_sp_fib_node_remove(fib, fib_node);
2257 static void mlxsw_sp_fib_node_fini(struct mlxsw_sp *mlxsw_sp,
2258 struct mlxsw_sp_fib_node *fib_node)
2260 struct mlxsw_sp_lpm_tree *lpm_tree = fib_node->fib->lpm_tree;
2261 struct mlxsw_sp_fib *fib = fib_node->fib;
2263 mlxsw_sp_fib_node_prefix_dec(fib_node);
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);
2270 mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, fib, &fib->prefix_usage);
2273 fib_node->fib = NULL;
2274 mlxsw_sp_fib_node_remove(fib, fib_node);
2277 static struct mlxsw_sp_fib_node *
2278 mlxsw_sp_fib4_node_get(struct mlxsw_sp *mlxsw_sp,
2279 const struct fib_entry_notifier_info *fen_info)
2281 struct mlxsw_sp_fib_node *fib_node;
2282 struct mlxsw_sp_fib *fib;
2283 struct mlxsw_sp_vr *vr;
2286 vr = mlxsw_sp_vr_get(mlxsw_sp, fen_info->tb_id);
2288 return ERR_CAST(vr);
2289 fib = mlxsw_sp_vr_fib(vr, MLXSW_SP_L3_PROTO_IPV4);
2291 fib_node = mlxsw_sp_fib_node_lookup(fib, &fen_info->dst,
2292 sizeof(fen_info->dst),
2297 fib_node = mlxsw_sp_fib_node_create(fib, &fen_info->dst,
2298 sizeof(fen_info->dst),
2302 goto err_fib_node_create;
2305 err = mlxsw_sp_fib_node_init(mlxsw_sp, fib_node, fib);
2307 goto err_fib_node_init;
2312 mlxsw_sp_fib_node_destroy(fib_node);
2313 err_fib_node_create:
2314 mlxsw_sp_vr_put(vr);
2315 return ERR_PTR(err);
2318 static void mlxsw_sp_fib4_node_put(struct mlxsw_sp *mlxsw_sp,
2319 struct mlxsw_sp_fib_node *fib_node)
2321 struct mlxsw_sp_vr *vr = fib_node->fib->vr;
2323 if (!list_empty(&fib_node->entry_list))
2325 mlxsw_sp_fib_node_fini(mlxsw_sp, fib_node);
2326 mlxsw_sp_fib_node_destroy(fib_node);
2327 mlxsw_sp_vr_put(vr);
2330 static struct mlxsw_sp_fib_entry *
2331 mlxsw_sp_fib4_node_entry_find(const struct mlxsw_sp_fib_node *fib_node,
2332 const struct mlxsw_sp_fib_entry_params *params)
2334 struct mlxsw_sp_fib_entry *fib_entry;
2336 list_for_each_entry(fib_entry, &fib_node->entry_list, list) {
2337 if (fib_entry->params.tb_id > params->tb_id)
2339 if (fib_entry->params.tb_id != params->tb_id)
2341 if (fib_entry->params.tos > params->tos)
2343 if (fib_entry->params.prio >= params->prio ||
2344 fib_entry->params.tos < params->tos)
2351 static int mlxsw_sp_fib4_node_list_append(struct mlxsw_sp_fib_entry *fib_entry,
2352 struct mlxsw_sp_fib_entry *new_entry)
2354 struct mlxsw_sp_fib_node *fib_node;
2356 if (WARN_ON(!fib_entry))
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)
2367 list_add_tail(&new_entry->list, &fib_entry->list);
2372 mlxsw_sp_fib4_node_list_insert(struct mlxsw_sp_fib_node *fib_node,
2373 struct mlxsw_sp_fib_entry *new_entry,
2374 bool replace, bool append)
2376 struct mlxsw_sp_fib_entry *fib_entry;
2378 fib_entry = mlxsw_sp_fib4_node_entry_find(fib_node, &new_entry->params);
2381 return mlxsw_sp_fib4_node_list_append(fib_entry, new_entry);
2382 if (replace && WARN_ON(!fib_entry))
2385 /* Insert new entry before replaced one, so that we can later
2386 * remove the second.
2389 list_add_tail(&new_entry->list, &fib_entry->list);
2391 struct mlxsw_sp_fib_entry *last;
2393 list_for_each_entry(last, &fib_node->entry_list, list) {
2394 if (new_entry->params.tb_id > last->params.tb_id)
2400 list_add(&new_entry->list, &fib_entry->list);
2402 list_add(&new_entry->list, &fib_node->entry_list);
2409 mlxsw_sp_fib4_node_list_remove(struct mlxsw_sp_fib_entry *fib_entry)
2411 list_del(&fib_entry->list);
2415 mlxsw_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)
2419 if (!mlxsw_sp_fib_node_entry_is_first(fib_node, fib_entry))
2422 /* To prevent packet loss, overwrite the previously offloaded
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);
2429 mlxsw_sp_fib_entry_offload_refresh(n, op, 0);
2432 return mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
2436 mlxsw_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)
2440 if (!mlxsw_sp_fib_node_entry_is_first(fib_node, fib_entry))
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;
2448 mlxsw_sp_fib_entry_update(mlxsw_sp, n);
2449 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, 0);
2453 mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry);
2456 static int mlxsw_sp_fib4_node_entry_link(struct mlxsw_sp *mlxsw_sp,
2457 struct mlxsw_sp_fib_entry *fib_entry,
2458 bool replace, bool append)
2460 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
2463 err = mlxsw_sp_fib4_node_list_insert(fib_node, fib_entry, replace,
2468 err = mlxsw_sp_fib4_node_entry_add(mlxsw_sp, fib_node, fib_entry);
2470 goto err_fib4_node_entry_add;
2474 err_fib4_node_entry_add:
2475 mlxsw_sp_fib4_node_list_remove(fib_entry);
2480 mlxsw_sp_fib4_node_entry_unlink(struct mlxsw_sp *mlxsw_sp,
2481 struct mlxsw_sp_fib_entry *fib_entry)
2483 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
2485 mlxsw_sp_fib4_node_entry_del(mlxsw_sp, fib_node, fib_entry);
2486 mlxsw_sp_fib4_node_list_remove(fib_entry);
2489 static void mlxsw_sp_fib4_entry_replace(struct mlxsw_sp *mlxsw_sp,
2490 struct mlxsw_sp_fib_entry *fib_entry,
2493 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
2494 struct mlxsw_sp_fib_entry *replaced;
2499 /* We inserted the new entry before replaced one */
2500 replaced = list_next_entry(fib_entry, list);
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);
2508 mlxsw_sp_router_fib4_add(struct mlxsw_sp *mlxsw_sp,
2509 const struct fib_entry_notifier_info *fen_info,
2510 bool replace, bool append)
2512 struct mlxsw_sp_fib_entry *fib_entry;
2513 struct mlxsw_sp_fib_node *fib_node;
2516 if (mlxsw_sp->router.aborted)
2519 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);
2525 fib_entry = mlxsw_sp_fib4_entry_create(mlxsw_sp, fib_node, fen_info);
2526 if (IS_ERR(fib_entry)) {
2527 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;
2532 err = mlxsw_sp_fib4_node_entry_link(mlxsw_sp, fib_entry, replace,
2535 dev_warn(mlxsw_sp->bus_info->dev, "Failed to link FIB entry to node\n");
2536 goto err_fib4_node_entry_link;
2539 mlxsw_sp_fib4_entry_replace(mlxsw_sp, fib_entry, replace);
2543 err_fib4_node_entry_link:
2544 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib_entry);
2545 err_fib4_entry_create:
2546 mlxsw_sp_fib4_node_put(mlxsw_sp, fib_node);
2550 static void mlxsw_sp_router_fib4_del(struct mlxsw_sp *mlxsw_sp,
2551 struct fib_entry_notifier_info *fen_info)
2553 struct mlxsw_sp_fib_entry *fib_entry;
2554 struct mlxsw_sp_fib_node *fib_node;
2556 if (mlxsw_sp->router.aborted)
2559 fib_entry = mlxsw_sp_fib4_entry_lookup(mlxsw_sp, fen_info);
2560 if (WARN_ON(!fib_entry))
2562 fib_node = fib_entry->fib_node;
2564 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);
2569 static int mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp)
2571 char ralta_pl[MLXSW_REG_RALTA_LEN];
2572 char ralst_pl[MLXSW_REG_RALST_LEN];
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);
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);
2586 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];
2591 if (!mlxsw_sp_vr_is_used(vr))
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),
2602 mlxsw_reg_ralue_pack4(ralue_pl, MLXSW_SP_L3_PROTO_IPV4,
2603 MLXSW_REG_RALUE_OP_WRITE_WRITE, vr->id, 0,
2605 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
2606 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue),
2615 static void mlxsw_sp_fib4_node_flush(struct mlxsw_sp *mlxsw_sp,
2616 struct mlxsw_sp_fib_node *fib_node)
2618 struct mlxsw_sp_fib_entry *fib_entry, *tmp;
2620 list_for_each_entry_safe(fib_entry, tmp, &fib_node->entry_list, list) {
2621 bool do_break = &tmp->list == &fib_node->entry_list;
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
2635 static void mlxsw_sp_fib_node_flush(struct mlxsw_sp *mlxsw_sp,
2636 struct mlxsw_sp_fib_node *fib_node)
2638 switch (fib_node->fib->proto) {
2639 case MLXSW_SP_L3_PROTO_IPV4:
2640 mlxsw_sp_fib4_node_flush(mlxsw_sp, fib_node);
2642 case MLXSW_SP_L3_PROTO_IPV6:
2648 static void mlxsw_sp_vr_fib_flush(struct mlxsw_sp *mlxsw_sp,
2649 struct mlxsw_sp_vr *vr,
2650 enum mlxsw_sp_l3proto proto)
2652 struct mlxsw_sp_fib *fib = mlxsw_sp_vr_fib(vr, proto);
2653 struct mlxsw_sp_fib_node *fib_node, *tmp;
2655 list_for_each_entry_safe(fib_node, tmp, &fib->node_list, list) {
2656 bool do_break = &tmp->list == &fib->node_list;
2658 mlxsw_sp_fib_node_flush(mlxsw_sp, fib_node);
2664 static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp)
2668 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
2669 struct mlxsw_sp_vr *vr = &mlxsw_sp->router.vrs[i];
2671 if (!mlxsw_sp_vr_is_used(vr))
2673 mlxsw_sp_vr_fib_flush(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV4);
2677 static void mlxsw_sp_router_fib4_abort(struct mlxsw_sp *mlxsw_sp)
2681 if (mlxsw_sp->router.aborted)
2683 dev_warn(mlxsw_sp->bus_info->dev, "FIB abort triggered. Note that FIB entries are no longer being offloaded to this device.\n");
2684 mlxsw_sp_router_fib_flush(mlxsw_sp);
2685 mlxsw_sp->router.aborted = true;
2686 err = mlxsw_sp_router_set_abort_trap(mlxsw_sp);
2688 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set abort trap.\n");
2691 struct mlxsw_sp_fib_event_work {
2692 struct work_struct work;
2694 struct fib_entry_notifier_info fen_info;
2695 struct fib_rule_notifier_info fr_info;
2696 struct fib_nh_notifier_info fnh_info;
2698 struct mlxsw_sp *mlxsw_sp;
2699 unsigned long event;
2702 static void mlxsw_sp_router_fib_event_work(struct work_struct *work)
2704 struct mlxsw_sp_fib_event_work *fib_work =
2705 container_of(work, struct mlxsw_sp_fib_event_work, work);
2706 struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp;
2707 struct fib_rule *rule;
2708 bool replace, append;
2711 /* Protect internal structures from changes */
2713 switch (fib_work->event) {
2714 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
2715 case FIB_EVENT_ENTRY_APPEND: /* fall through */
2716 case FIB_EVENT_ENTRY_ADD:
2717 replace = fib_work->event == FIB_EVENT_ENTRY_REPLACE;
2718 append = fib_work->event == FIB_EVENT_ENTRY_APPEND;
2719 err = mlxsw_sp_router_fib4_add(mlxsw_sp, &fib_work->fen_info,
2722 mlxsw_sp_router_fib4_abort(mlxsw_sp);
2723 fib_info_put(fib_work->fen_info.fi);
2725 case FIB_EVENT_ENTRY_DEL:
2726 mlxsw_sp_router_fib4_del(mlxsw_sp, &fib_work->fen_info);
2727 fib_info_put(fib_work->fen_info.fi);
2729 case FIB_EVENT_RULE_ADD: /* fall through */
2730 case FIB_EVENT_RULE_DEL:
2731 rule = fib_work->fr_info.rule;
2732 if (!fib4_rule_default(rule) && !rule->l3mdev)
2733 mlxsw_sp_router_fib4_abort(mlxsw_sp);
2736 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);
2747 /* Called with rcu_read_lock() */
2748 static int mlxsw_sp_router_fib_event(struct notifier_block *nb,
2749 unsigned long event, void *ptr)
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;
2755 if (!net_eq(info->net, &init_net))
2758 fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
2759 if (WARN_ON(!fib_work))
2762 INIT_WORK(&fib_work->work, mlxsw_sp_router_fib_event_work);
2763 fib_work->mlxsw_sp = mlxsw_sp;
2764 fib_work->event = event;
2767 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
2768 case FIB_EVENT_ENTRY_APPEND: /* fall through */
2769 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.
2775 fib_info_hold(fib_work->fen_info.fi);
2777 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);
2782 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);
2789 mlxsw_core_schedule_work(&fib_work->work);
2794 static struct mlxsw_sp_rif *
2795 mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
2796 const struct net_device *dev)
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];
2807 static int mlxsw_sp_router_rif_disable(struct mlxsw_sp *mlxsw_sp, u16 rif)
2809 char ritr_pl[MLXSW_REG_RITR_LEN];
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))
2817 mlxsw_reg_ritr_enable_set(ritr_pl, false);
2818 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
2821 static void mlxsw_sp_router_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
2822 struct mlxsw_sp_rif *rif)
2824 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);
2829 static bool mlxsw_sp_rif_should_config(struct mlxsw_sp_rif *rif,
2830 const struct in_device *in_dev,
2831 unsigned long event)
2839 if (rif && !in_dev->ifa_list &&
2840 !netif_is_l3_slave(rif->dev))
2842 /* It is possible we already removed the RIF ourselves
2843 * if it was assigned to a netdev that is now a bridge
2852 #define MLXSW_SP_INVALID_INDEX_RIF 0xffff
2853 static int mlxsw_sp_avail_rif_get(struct mlxsw_sp *mlxsw_sp)
2857 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
2858 if (!mlxsw_sp->rifs[i])
2861 return MLXSW_SP_INVALID_INDEX_RIF;
2864 static void mlxsw_sp_vport_rif_sp_attr_get(struct mlxsw_sp_port *mlxsw_sp_vport,
2865 bool *p_lagged, u16 *p_system_port)
2867 u8 local_port = mlxsw_sp_vport->local_port;
2869 *p_lagged = mlxsw_sp_vport->lagged;
2870 *p_system_port = *p_lagged ? mlxsw_sp_vport->lag_id : local_port;
2873 static int mlxsw_sp_vport_rif_sp_op(struct mlxsw_sp_port *mlxsw_sp_vport,
2874 u16 vr_id, struct net_device *l3_dev,
2875 u16 rif_index, bool create)
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];
2882 mlxsw_reg_ritr_pack(ritr_pl, create, MLXSW_REG_RITR_SP_IF, rif_index,
2883 vr_id, l3_dev->mtu, l3_dev->dev_addr);
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));
2889 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
2892 static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port *mlxsw_sp_vport);
2894 static u16 mlxsw_sp_rif_sp_to_fid(u16 rif_index)
2896 return MLXSW_SP_RFID_BASE + rif_index;
2899 static struct mlxsw_sp_fid *
2900 mlxsw_sp_rfid_alloc(u16 fid, struct net_device *l3_dev)
2902 struct mlxsw_sp_fid *f;
2904 f = kzalloc(sizeof(*f), GFP_KERNEL);
2908 f->leave = mlxsw_sp_vport_rif_sp_leave;
2916 static struct mlxsw_sp_rif *
2917 mlxsw_sp_rif_alloc(u16 rif_index, u16 vr_id, struct net_device *l3_dev,
2918 struct mlxsw_sp_fid *f)
2920 struct mlxsw_sp_rif *rif;
2922 rif = kzalloc(sizeof(*rif), GFP_KERNEL);
2926 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;
2932 rif->rif_index = rif_index;
2938 u16 mlxsw_sp_rif_index(const struct mlxsw_sp_rif *rif)
2940 return rif->rif_index;
2943 int mlxsw_sp_rif_dev_ifindex(const struct mlxsw_sp_rif *rif)
2945 return rif->dev->ifindex;
2948 static struct mlxsw_sp_rif *
2949 mlxsw_sp_vport_rif_sp_create(struct mlxsw_sp_port *mlxsw_sp_vport,
2950 struct net_device *l3_dev)
2952 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
2953 u32 tb_id = l3mdev_fib_table(l3_dev);
2954 struct mlxsw_sp_vr *vr;
2955 struct mlxsw_sp_fid *f;
2956 struct mlxsw_sp_rif *rif;
2960 rif_index = mlxsw_sp_avail_rif_get(mlxsw_sp);
2961 if (rif_index == MLXSW_SP_INVALID_INDEX_RIF)
2962 return ERR_PTR(-ERANGE);
2964 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id ? : RT_TABLE_MAIN);
2966 return ERR_CAST(vr);
2968 err = mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, vr->id, l3_dev,
2971 goto err_vport_rif_sp_op;
2973 fid = mlxsw_sp_rif_sp_to_fid(rif_index);
2974 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, true);
2976 goto err_rif_fdb_op;
2978 f = mlxsw_sp_rfid_alloc(fid, l3_dev);
2981 goto err_rfid_alloc;
2984 rif = mlxsw_sp_rif_alloc(rif_index, vr->id, l3_dev, f);
2990 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);
2995 netdev_dbg(mlxsw_sp_vport->dev,
2996 "Counter alloc Failed err=%d\n", err);
3000 mlxsw_sp->rifs[rif_index] = rif;
3008 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, false);
3010 mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, vr->id, l3_dev, rif_index,
3012 err_vport_rif_sp_op:
3013 mlxsw_sp_vr_put(vr);
3014 return ERR_PTR(err);
3017 static void mlxsw_sp_vport_rif_sp_destroy(struct mlxsw_sp_port *mlxsw_sp_vport,
3018 struct mlxsw_sp_rif *rif)
3020 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
3021 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;
3027 mlxsw_sp_router_rif_gone_sync(mlxsw_sp, rif);
3029 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);
3033 mlxsw_sp->rifs[rif_index] = NULL;
3040 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, false);
3042 mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, vr->id, l3_dev, rif_index,
3044 mlxsw_sp_vr_put(vr);
3047 static int mlxsw_sp_vport_rif_sp_join(struct mlxsw_sp_port *mlxsw_sp_vport,
3048 struct net_device *l3_dev)
3050 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
3051 struct mlxsw_sp_rif *rif;
3053 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
3055 rif = mlxsw_sp_vport_rif_sp_create(mlxsw_sp_vport, l3_dev);
3057 return PTR_ERR(rif);
3060 mlxsw_sp_vport_fid_set(mlxsw_sp_vport, rif->f);
3061 rif->f->ref_count++;
3063 netdev_dbg(mlxsw_sp_vport->dev, "Joined FID=%d\n", rif->f->fid);
3068 static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port *mlxsw_sp_vport)
3070 struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
3072 netdev_dbg(mlxsw_sp_vport->dev, "Left FID=%d\n", f->fid);
3074 mlxsw_sp_vport_fid_set(mlxsw_sp_vport, NULL);
3075 if (--f->ref_count == 0)
3076 mlxsw_sp_vport_rif_sp_destroy(mlxsw_sp_vport, f->rif);
3079 static int mlxsw_sp_inetaddr_vport_event(struct net_device *l3_dev,
3080 struct net_device *port_dev,
3081 unsigned long event, u16 vid)
3083 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(port_dev);
3084 struct mlxsw_sp_port *mlxsw_sp_vport;
3086 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
3087 if (WARN_ON(!mlxsw_sp_vport))
3092 return mlxsw_sp_vport_rif_sp_join(mlxsw_sp_vport, l3_dev);
3094 mlxsw_sp_vport_rif_sp_leave(mlxsw_sp_vport);
3101 static int mlxsw_sp_inetaddr_port_event(struct net_device *port_dev,
3102 unsigned long event)
3104 if (netif_is_bridge_port(port_dev) ||
3105 netif_is_lag_port(port_dev) ||
3106 netif_is_ovs_port(port_dev))
3109 return mlxsw_sp_inetaddr_vport_event(port_dev, port_dev, event, 1);
3112 static int __mlxsw_sp_inetaddr_lag_event(struct net_device *l3_dev,
3113 struct net_device *lag_dev,
3114 unsigned long event, u16 vid)
3116 struct net_device *port_dev;
3117 struct list_head *iter;
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,
3132 static int mlxsw_sp_inetaddr_lag_event(struct net_device *lag_dev,
3133 unsigned long event)
3135 if (netif_is_bridge_port(lag_dev))
3138 return __mlxsw_sp_inetaddr_lag_event(lag_dev, lag_dev, event, 1);
3141 static struct mlxsw_sp_fid *mlxsw_sp_bridge_fid_get(struct mlxsw_sp *mlxsw_sp,
3142 struct net_device *l3_dev)
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)
3151 return mlxsw_sp_vfid_find(mlxsw_sp, l3_dev);
3153 return mlxsw_sp_fid_find(mlxsw_sp, fid);
3156 static u8 mlxsw_sp_router_port(const struct mlxsw_sp *mlxsw_sp)
3158 return mlxsw_core_max_ports(mlxsw_sp->core) + 1;
3161 static enum mlxsw_flood_table_type mlxsw_sp_flood_table_type_get(u16 fid)
3163 return mlxsw_sp_fid_is_vfid(fid) ? MLXSW_REG_SFGC_TABLE_TYPE_FID :
3164 MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST;
3167 static u16 mlxsw_sp_flood_table_index_get(u16 fid)
3169 return mlxsw_sp_fid_is_vfid(fid) ? mlxsw_sp_fid_to_vfid(fid) : fid;
3172 static int mlxsw_sp_router_port_flood_set(struct mlxsw_sp *mlxsw_sp, u16 fid,
3175 u8 router_port = mlxsw_sp_router_port(mlxsw_sp);
3176 enum mlxsw_flood_table_type table_type;
3181 sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL);
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,
3188 1, router_port, set);
3189 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
3195 static enum mlxsw_reg_ritr_if_type mlxsw_sp_rif_type_get(u16 fid)
3197 if (mlxsw_sp_fid_is_vfid(fid))
3198 return MLXSW_REG_RITR_FID_IF;
3200 return MLXSW_REG_RITR_VLAN_IF;
3203 static int mlxsw_sp_rif_bridge_op(struct mlxsw_sp *mlxsw_sp, u16 vr_id,
3204 struct net_device *l3_dev,
3208 enum mlxsw_reg_ritr_if_type rif_type;
3209 char ritr_pl[MLXSW_REG_RITR_LEN];
3211 rif_type = mlxsw_sp_rif_type_get(fid);
3212 mlxsw_reg_ritr_pack(ritr_pl, create, rif_type, rif, vr_id, l3_dev->mtu,
3214 mlxsw_reg_ritr_fid_set(ritr_pl, rif_type, fid);
3216 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3219 static int mlxsw_sp_rif_bridge_create(struct mlxsw_sp *mlxsw_sp,
3220 struct net_device *l3_dev,
3221 struct mlxsw_sp_fid *f)
3223 u32 tb_id = l3mdev_fib_table(l3_dev);
3224 struct mlxsw_sp_rif *rif;
3225 struct mlxsw_sp_vr *vr;
3229 rif_index = mlxsw_sp_avail_rif_get(mlxsw_sp);
3230 if (rif_index == MLXSW_SP_INVALID_INDEX_RIF)
3233 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id ? : RT_TABLE_MAIN);
3237 err = mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, true);
3239 goto err_port_flood_set;
3241 err = mlxsw_sp_rif_bridge_op(mlxsw_sp, vr->id, l3_dev, f->fid,
3244 goto err_rif_bridge_op;
3246 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, true);
3248 goto err_rif_fdb_op;
3250 rif = mlxsw_sp_rif_alloc(rif_index, vr->id, l3_dev, f);
3257 mlxsw_sp->rifs[rif_index] = rif;
3260 netdev_dbg(l3_dev, "RIF=%d created\n", rif_index);
3265 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, false);
3267 mlxsw_sp_rif_bridge_op(mlxsw_sp, vr->id, l3_dev, f->fid, rif_index,
3270 mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, false);
3272 mlxsw_sp_vr_put(vr);
3276 void mlxsw_sp_rif_bridge_destroy(struct mlxsw_sp *mlxsw_sp,
3277 struct mlxsw_sp_rif *rif)
3279 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;
3284 mlxsw_sp_router_rif_gone_sync(mlxsw_sp, rif);
3287 mlxsw_sp->rifs[rif_index] = NULL;
3292 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, false);
3294 mlxsw_sp_rif_bridge_op(mlxsw_sp, vr->id, l3_dev, f->fid, rif_index,
3297 mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, false);
3299 mlxsw_sp_vr_put(vr);
3301 netdev_dbg(l3_dev, "RIF=%d destroyed\n", rif_index);
3304 static int mlxsw_sp_inetaddr_bridge_event(struct net_device *l3_dev,
3305 struct net_device *br_dev,
3306 unsigned long event)
3308 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
3309 struct mlxsw_sp_fid *f;
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.
3315 f = mlxsw_sp_bridge_fid_get(mlxsw_sp, l3_dev);
3321 return mlxsw_sp_rif_bridge_create(mlxsw_sp, l3_dev, f);
3323 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->rif);
3330 static int mlxsw_sp_inetaddr_vlan_event(struct net_device *vlan_dev,
3331 unsigned long event)
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);
3337 if (mlxsw_sp_port_dev_check(real_dev))
3338 return mlxsw_sp_inetaddr_vport_event(vlan_dev, real_dev, event,
3340 else if (netif_is_lag_master(real_dev))
3341 return __mlxsw_sp_inetaddr_lag_event(vlan_dev, real_dev, event,
3343 else if (netif_is_bridge_master(real_dev) &&
3344 mlxsw_sp->master_bridge.dev == real_dev)
3345 return mlxsw_sp_inetaddr_bridge_event(vlan_dev, real_dev,
3351 static int __mlxsw_sp_inetaddr_event(struct net_device *dev,
3352 unsigned long event)
3354 if (mlxsw_sp_port_dev_check(dev))
3355 return mlxsw_sp_inetaddr_port_event(dev, event);
3356 else if (netif_is_lag_master(dev))
3357 return mlxsw_sp_inetaddr_lag_event(dev, event);
3358 else if (netif_is_bridge_master(dev))
3359 return mlxsw_sp_inetaddr_bridge_event(dev, dev, event);
3360 else if (is_vlan_dev(dev))
3361 return mlxsw_sp_inetaddr_vlan_event(dev, event);
3366 int mlxsw_sp_inetaddr_event(struct notifier_block *unused,
3367 unsigned long event, void *ptr)
3369 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
3370 struct net_device *dev = ifa->ifa_dev->dev;
3371 struct mlxsw_sp *mlxsw_sp;
3372 struct mlxsw_sp_rif *rif;
3375 mlxsw_sp = mlxsw_sp_lower_get(dev);
3379 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3380 if (!mlxsw_sp_rif_should_config(rif, ifa->ifa_dev, event))
3383 err = __mlxsw_sp_inetaddr_event(dev, event);
3385 return notifier_from_errno(err);
3388 static int mlxsw_sp_rif_edit(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
3389 const char *mac, int mtu)
3391 char ritr_pl[MLXSW_REG_RITR_LEN];
3394 mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
3395 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3399 mlxsw_reg_ritr_mtu_set(ritr_pl, mtu);
3400 mlxsw_reg_ritr_if_mac_memcpy_to(ritr_pl, mac);
3401 mlxsw_reg_ritr_op_set(ritr_pl, MLXSW_REG_RITR_RIF_CREATE);
3402 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3405 int mlxsw_sp_netdevice_router_port_event(struct net_device *dev)
3407 struct mlxsw_sp *mlxsw_sp;
3408 struct mlxsw_sp_rif *rif;
3411 mlxsw_sp = mlxsw_sp_lower_get(dev);
3415 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3419 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, rif->f->fid, false);
3423 err = mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, dev->dev_addr,
3428 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, dev->dev_addr, rif->f->fid, true);
3430 goto err_rif_fdb_op;
3432 ether_addr_copy(rif->addr, dev->dev_addr);
3433 rif->mtu = dev->mtu;
3435 netdev_dbg(dev, "Updated RIF=%d\n", rif->rif_index);
3440 mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, rif->addr, rif->mtu);
3442 mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, rif->f->fid, true);
3446 static int mlxsw_sp_port_vrf_join(struct mlxsw_sp *mlxsw_sp,
3447 struct net_device *l3_dev)
3449 struct mlxsw_sp_rif *rif;
3451 /* If netdev is already associated with a RIF, then we need to
3452 * destroy it and create a new one with the new virtual router ID.
3454 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
3456 __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_DOWN);
3458 return __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_UP);
3461 static void mlxsw_sp_port_vrf_leave(struct mlxsw_sp *mlxsw_sp,
3462 struct net_device *l3_dev)
3464 struct mlxsw_sp_rif *rif;
3466 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
3469 __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_DOWN);
3472 int mlxsw_sp_netdevice_vrf_event(struct net_device *l3_dev, unsigned long event,
3473 struct netdev_notifier_changeupper_info *info)
3475 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
3482 case NETDEV_PRECHANGEUPPER:
3484 case NETDEV_CHANGEUPPER:
3486 err = mlxsw_sp_port_vrf_join(mlxsw_sp, l3_dev);
3488 mlxsw_sp_port_vrf_leave(mlxsw_sp, l3_dev);
3495 static void mlxsw_sp_router_fib_dump_flush(struct notifier_block *nb)
3497 struct mlxsw_sp *mlxsw_sp = container_of(nb, struct mlxsw_sp, fib_nb);
3499 /* Flush pending FIB notifications and then flush the device's
3500 * table before requesting another dump. The FIB notification
3501 * block is unregistered, so no need to take RTNL.
3503 mlxsw_core_flush_owq();
3504 mlxsw_sp_router_fib_flush(mlxsw_sp);
3507 static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
3509 char rgcr_pl[MLXSW_REG_RGCR_LEN];
3513 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_RIFS))
3516 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
3517 mlxsw_sp->rifs = kcalloc(max_rifs, sizeof(struct mlxsw_sp_rif *),
3519 if (!mlxsw_sp->rifs)
3522 mlxsw_reg_rgcr_pack(rgcr_pl, true);
3523 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, max_rifs);
3524 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
3531 kfree(mlxsw_sp->rifs);
3535 static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
3537 char rgcr_pl[MLXSW_REG_RGCR_LEN];
3540 mlxsw_reg_rgcr_pack(rgcr_pl, false);
3541 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
3543 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
3544 WARN_ON_ONCE(mlxsw_sp->rifs[i]);
3546 kfree(mlxsw_sp->rifs);
3549 int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
3553 INIT_LIST_HEAD(&mlxsw_sp->router.nexthop_neighs_list);
3554 err = __mlxsw_sp_router_init(mlxsw_sp);
3558 err = rhashtable_init(&mlxsw_sp->router.nexthop_ht,
3559 &mlxsw_sp_nexthop_ht_params);
3561 goto err_nexthop_ht_init;
3563 err = rhashtable_init(&mlxsw_sp->router.nexthop_group_ht,
3564 &mlxsw_sp_nexthop_group_ht_params);
3566 goto err_nexthop_group_ht_init;
3568 err = mlxsw_sp_lpm_init(mlxsw_sp);
3572 err = mlxsw_sp_vrs_init(mlxsw_sp);
3576 err = mlxsw_sp_neigh_init(mlxsw_sp);
3578 goto err_neigh_init;
3580 mlxsw_sp->fib_nb.notifier_call = mlxsw_sp_router_fib_event;
3581 err = register_fib_notifier(&mlxsw_sp->fib_nb,
3582 mlxsw_sp_router_fib_dump_flush);
3584 goto err_register_fib_notifier;
3588 err_register_fib_notifier:
3589 mlxsw_sp_neigh_fini(mlxsw_sp);
3591 mlxsw_sp_vrs_fini(mlxsw_sp);
3593 mlxsw_sp_lpm_fini(mlxsw_sp);
3595 rhashtable_destroy(&mlxsw_sp->router.nexthop_group_ht);
3596 err_nexthop_group_ht_init:
3597 rhashtable_destroy(&mlxsw_sp->router.nexthop_ht);
3598 err_nexthop_ht_init:
3599 __mlxsw_sp_router_fini(mlxsw_sp);
3603 void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
3605 unregister_fib_notifier(&mlxsw_sp->fib_nb);
3606 mlxsw_sp_neigh_fini(mlxsw_sp);
3607 mlxsw_sp_vrs_fini(mlxsw_sp);
3608 mlxsw_sp_lpm_fini(mlxsw_sp);
3609 rhashtable_destroy(&mlxsw_sp->router.nexthop_group_ht);
3610 rhashtable_destroy(&mlxsw_sp->router.nexthop_ht);
3611 __mlxsw_sp_router_fini(mlxsw_sp);