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>
51 #define mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) \
52 for_each_set_bit(prefix, (prefix_usage)->b, MLXSW_SP_PREFIX_COUNT)
55 mlxsw_sp_prefix_usage_subset(struct mlxsw_sp_prefix_usage
*prefix_usage1
,
56 struct mlxsw_sp_prefix_usage
*prefix_usage2
)
60 mlxsw_sp_prefix_usage_for_each(prefix
, prefix_usage1
) {
61 if (!test_bit(prefix
, prefix_usage2
->b
))
68 mlxsw_sp_prefix_usage_eq(struct mlxsw_sp_prefix_usage
*prefix_usage1
,
69 struct mlxsw_sp_prefix_usage
*prefix_usage2
)
71 return !memcmp(prefix_usage1
, prefix_usage2
, sizeof(*prefix_usage1
));
75 mlxsw_sp_prefix_usage_none(struct mlxsw_sp_prefix_usage
*prefix_usage
)
77 struct mlxsw_sp_prefix_usage prefix_usage_none
= {{ 0 } };
79 return mlxsw_sp_prefix_usage_eq(prefix_usage
, &prefix_usage_none
);
83 mlxsw_sp_prefix_usage_cpy(struct mlxsw_sp_prefix_usage
*prefix_usage1
,
84 struct mlxsw_sp_prefix_usage
*prefix_usage2
)
86 memcpy(prefix_usage1
, prefix_usage2
, sizeof(*prefix_usage1
));
90 mlxsw_sp_prefix_usage_zero(struct mlxsw_sp_prefix_usage
*prefix_usage
)
92 memset(prefix_usage
, 0, sizeof(*prefix_usage
));
96 mlxsw_sp_prefix_usage_set(struct mlxsw_sp_prefix_usage
*prefix_usage
,
97 unsigned char prefix_len
)
99 set_bit(prefix_len
, prefix_usage
->b
);
103 mlxsw_sp_prefix_usage_clear(struct mlxsw_sp_prefix_usage
*prefix_usage
,
104 unsigned char prefix_len
)
106 clear_bit(prefix_len
, prefix_usage
->b
);
109 struct mlxsw_sp_fib_key
{
110 unsigned char addr
[sizeof(struct in6_addr
)];
111 unsigned char prefix_len
;
114 enum mlxsw_sp_fib_entry_type
{
115 MLXSW_SP_FIB_ENTRY_TYPE_REMOTE
,
116 MLXSW_SP_FIB_ENTRY_TYPE_LOCAL
,
117 MLXSW_SP_FIB_ENTRY_TYPE_TRAP
,
120 struct mlxsw_sp_nexthop_group
;
122 struct mlxsw_sp_fib_entry
{
123 struct rhash_head ht_node
;
124 struct mlxsw_sp_fib_key key
;
125 enum mlxsw_sp_fib_entry_type type
;
127 u16 rif
; /* used for action local */
128 struct mlxsw_sp_vr
*vr
;
129 struct list_head nexthop_group_node
;
130 struct mlxsw_sp_nexthop_group
*nh_group
;
133 struct mlxsw_sp_fib
{
134 struct rhashtable ht
;
135 unsigned long prefix_ref_count
[MLXSW_SP_PREFIX_COUNT
];
136 struct mlxsw_sp_prefix_usage prefix_usage
;
139 static const struct rhashtable_params mlxsw_sp_fib_ht_params
= {
140 .key_offset
= offsetof(struct mlxsw_sp_fib_entry
, key
),
141 .head_offset
= offsetof(struct mlxsw_sp_fib_entry
, ht_node
),
142 .key_len
= sizeof(struct mlxsw_sp_fib_key
),
143 .automatic_shrinking
= true,
146 static int mlxsw_sp_fib_entry_insert(struct mlxsw_sp_fib
*fib
,
147 struct mlxsw_sp_fib_entry
*fib_entry
)
149 unsigned char prefix_len
= fib_entry
->key
.prefix_len
;
152 err
= rhashtable_insert_fast(&fib
->ht
, &fib_entry
->ht_node
,
153 mlxsw_sp_fib_ht_params
);
156 if (fib
->prefix_ref_count
[prefix_len
]++ == 0)
157 mlxsw_sp_prefix_usage_set(&fib
->prefix_usage
, prefix_len
);
161 static void mlxsw_sp_fib_entry_remove(struct mlxsw_sp_fib
*fib
,
162 struct mlxsw_sp_fib_entry
*fib_entry
)
164 unsigned char prefix_len
= fib_entry
->key
.prefix_len
;
166 if (--fib
->prefix_ref_count
[prefix_len
] == 0)
167 mlxsw_sp_prefix_usage_clear(&fib
->prefix_usage
, prefix_len
);
168 rhashtable_remove_fast(&fib
->ht
, &fib_entry
->ht_node
,
169 mlxsw_sp_fib_ht_params
);
172 static struct mlxsw_sp_fib_entry
*
173 mlxsw_sp_fib_entry_create(struct mlxsw_sp_fib
*fib
, const void *addr
,
174 size_t addr_len
, unsigned char prefix_len
)
176 struct mlxsw_sp_fib_entry
*fib_entry
;
178 fib_entry
= kzalloc(sizeof(*fib_entry
), GFP_KERNEL
);
181 memcpy(fib_entry
->key
.addr
, addr
, addr_len
);
182 fib_entry
->key
.prefix_len
= prefix_len
;
186 static void mlxsw_sp_fib_entry_destroy(struct mlxsw_sp_fib_entry
*fib_entry
)
191 static struct mlxsw_sp_fib_entry
*
192 mlxsw_sp_fib_entry_lookup(struct mlxsw_sp_fib
*fib
, const void *addr
,
193 size_t addr_len
, unsigned char prefix_len
)
195 struct mlxsw_sp_fib_key key
= {{ 0 } };
197 memcpy(key
.addr
, addr
, addr_len
);
198 key
.prefix_len
= prefix_len
;
199 return rhashtable_lookup_fast(&fib
->ht
, &key
, mlxsw_sp_fib_ht_params
);
202 static struct mlxsw_sp_fib
*mlxsw_sp_fib_create(void)
204 struct mlxsw_sp_fib
*fib
;
207 fib
= kzalloc(sizeof(*fib
), GFP_KERNEL
);
209 return ERR_PTR(-ENOMEM
);
210 err
= rhashtable_init(&fib
->ht
, &mlxsw_sp_fib_ht_params
);
212 goto err_rhashtable_init
;
220 static void mlxsw_sp_fib_destroy(struct mlxsw_sp_fib
*fib
)
222 rhashtable_destroy(&fib
->ht
);
226 static struct mlxsw_sp_lpm_tree
*
227 mlxsw_sp_lpm_tree_find_unused(struct mlxsw_sp
*mlxsw_sp
, bool one_reserved
)
229 static struct mlxsw_sp_lpm_tree
*lpm_tree
;
232 for (i
= 0; i
< MLXSW_SP_LPM_TREE_COUNT
; i
++) {
233 lpm_tree
= &mlxsw_sp
->router
.lpm_trees
[i
];
234 if (lpm_tree
->ref_count
== 0) {
236 one_reserved
= false;
244 static int mlxsw_sp_lpm_tree_alloc(struct mlxsw_sp
*mlxsw_sp
,
245 struct mlxsw_sp_lpm_tree
*lpm_tree
)
247 char ralta_pl
[MLXSW_REG_RALTA_LEN
];
249 mlxsw_reg_ralta_pack(ralta_pl
, true, lpm_tree
->proto
, lpm_tree
->id
);
250 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ralta
), ralta_pl
);
253 static int mlxsw_sp_lpm_tree_free(struct mlxsw_sp
*mlxsw_sp
,
254 struct mlxsw_sp_lpm_tree
*lpm_tree
)
256 char ralta_pl
[MLXSW_REG_RALTA_LEN
];
258 mlxsw_reg_ralta_pack(ralta_pl
, false, lpm_tree
->proto
, lpm_tree
->id
);
259 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ralta
), ralta_pl
);
263 mlxsw_sp_lpm_tree_left_struct_set(struct mlxsw_sp
*mlxsw_sp
,
264 struct mlxsw_sp_prefix_usage
*prefix_usage
,
265 struct mlxsw_sp_lpm_tree
*lpm_tree
)
267 char ralst_pl
[MLXSW_REG_RALST_LEN
];
270 u8 last_prefix
= MLXSW_REG_RALST_BIN_NO_CHILD
;
272 mlxsw_sp_prefix_usage_for_each(prefix
, prefix_usage
)
275 mlxsw_reg_ralst_pack(ralst_pl
, root_bin
, lpm_tree
->id
);
276 mlxsw_sp_prefix_usage_for_each(prefix
, prefix_usage
) {
279 mlxsw_reg_ralst_bin_pack(ralst_pl
, prefix
, last_prefix
,
280 MLXSW_REG_RALST_BIN_NO_CHILD
);
281 last_prefix
= prefix
;
283 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ralst
), ralst_pl
);
286 static struct mlxsw_sp_lpm_tree
*
287 mlxsw_sp_lpm_tree_create(struct mlxsw_sp
*mlxsw_sp
,
288 struct mlxsw_sp_prefix_usage
*prefix_usage
,
289 enum mlxsw_sp_l3proto proto
, bool one_reserved
)
291 struct mlxsw_sp_lpm_tree
*lpm_tree
;
294 lpm_tree
= mlxsw_sp_lpm_tree_find_unused(mlxsw_sp
, one_reserved
);
296 return ERR_PTR(-EBUSY
);
297 lpm_tree
->proto
= proto
;
298 err
= mlxsw_sp_lpm_tree_alloc(mlxsw_sp
, lpm_tree
);
302 err
= mlxsw_sp_lpm_tree_left_struct_set(mlxsw_sp
, prefix_usage
,
305 goto err_left_struct_set
;
309 mlxsw_sp_lpm_tree_free(mlxsw_sp
, lpm_tree
);
313 static int mlxsw_sp_lpm_tree_destroy(struct mlxsw_sp
*mlxsw_sp
,
314 struct mlxsw_sp_lpm_tree
*lpm_tree
)
316 return mlxsw_sp_lpm_tree_free(mlxsw_sp
, lpm_tree
);
319 static struct mlxsw_sp_lpm_tree
*
320 mlxsw_sp_lpm_tree_get(struct mlxsw_sp
*mlxsw_sp
,
321 struct mlxsw_sp_prefix_usage
*prefix_usage
,
322 enum mlxsw_sp_l3proto proto
, bool one_reserved
)
324 struct mlxsw_sp_lpm_tree
*lpm_tree
;
327 for (i
= 0; i
< MLXSW_SP_LPM_TREE_COUNT
; i
++) {
328 lpm_tree
= &mlxsw_sp
->router
.lpm_trees
[i
];
329 if (lpm_tree
->proto
== proto
&&
330 mlxsw_sp_prefix_usage_eq(&lpm_tree
->prefix_usage
,
334 lpm_tree
= mlxsw_sp_lpm_tree_create(mlxsw_sp
, prefix_usage
,
335 proto
, one_reserved
);
336 if (IS_ERR(lpm_tree
))
340 lpm_tree
->ref_count
++;
344 static int mlxsw_sp_lpm_tree_put(struct mlxsw_sp
*mlxsw_sp
,
345 struct mlxsw_sp_lpm_tree
*lpm_tree
)
347 if (--lpm_tree
->ref_count
== 0)
348 return mlxsw_sp_lpm_tree_destroy(mlxsw_sp
, lpm_tree
);
352 static void mlxsw_sp_lpm_init(struct mlxsw_sp
*mlxsw_sp
)
354 struct mlxsw_sp_lpm_tree
*lpm_tree
;
357 for (i
= 0; i
< MLXSW_SP_LPM_TREE_COUNT
; i
++) {
358 lpm_tree
= &mlxsw_sp
->router
.lpm_trees
[i
];
359 lpm_tree
->id
= i
+ MLXSW_SP_LPM_TREE_MIN
;
363 static struct mlxsw_sp_vr
*mlxsw_sp_vr_find_unused(struct mlxsw_sp
*mlxsw_sp
)
365 struct mlxsw_sp_vr
*vr
;
368 for (i
= 0; i
< MLXSW_SP_VIRTUAL_ROUTER_MAX
; i
++) {
369 vr
= &mlxsw_sp
->router
.vrs
[i
];
376 static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp
*mlxsw_sp
,
377 struct mlxsw_sp_vr
*vr
)
379 char raltb_pl
[MLXSW_REG_RALTB_LEN
];
381 mlxsw_reg_raltb_pack(raltb_pl
, vr
->id
, vr
->proto
, vr
->lpm_tree
->id
);
382 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(raltb
), raltb_pl
);
385 static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp
*mlxsw_sp
,
386 struct mlxsw_sp_vr
*vr
)
388 char raltb_pl
[MLXSW_REG_RALTB_LEN
];
390 /* Bind to tree 0 which is default */
391 mlxsw_reg_raltb_pack(raltb_pl
, vr
->id
, vr
->proto
, 0);
392 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(raltb
), raltb_pl
);
395 static u32
mlxsw_sp_fix_tb_id(u32 tb_id
)
397 /* For our purpose, squash main and local table into one */
398 if (tb_id
== RT_TABLE_LOCAL
)
399 tb_id
= RT_TABLE_MAIN
;
403 static struct mlxsw_sp_vr
*mlxsw_sp_vr_find(struct mlxsw_sp
*mlxsw_sp
,
405 enum mlxsw_sp_l3proto proto
)
407 struct mlxsw_sp_vr
*vr
;
410 tb_id
= mlxsw_sp_fix_tb_id(tb_id
);
411 for (i
= 0; i
< MLXSW_SP_VIRTUAL_ROUTER_MAX
; i
++) {
412 vr
= &mlxsw_sp
->router
.vrs
[i
];
413 if (vr
->used
&& vr
->proto
== proto
&& vr
->tb_id
== tb_id
)
419 static struct mlxsw_sp_vr
*mlxsw_sp_vr_create(struct mlxsw_sp
*mlxsw_sp
,
420 unsigned char prefix_len
,
422 enum mlxsw_sp_l3proto proto
)
424 struct mlxsw_sp_prefix_usage req_prefix_usage
;
425 struct mlxsw_sp_lpm_tree
*lpm_tree
;
426 struct mlxsw_sp_vr
*vr
;
429 vr
= mlxsw_sp_vr_find_unused(mlxsw_sp
);
431 return ERR_PTR(-EBUSY
);
432 vr
->fib
= mlxsw_sp_fib_create();
434 return ERR_CAST(vr
->fib
);
438 mlxsw_sp_prefix_usage_zero(&req_prefix_usage
);
439 mlxsw_sp_prefix_usage_set(&req_prefix_usage
, prefix_len
);
440 lpm_tree
= mlxsw_sp_lpm_tree_get(mlxsw_sp
, &req_prefix_usage
,
442 if (IS_ERR(lpm_tree
)) {
443 err
= PTR_ERR(lpm_tree
);
446 vr
->lpm_tree
= lpm_tree
;
447 err
= mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp
, vr
);
455 mlxsw_sp_lpm_tree_put(mlxsw_sp
, vr
->lpm_tree
);
457 mlxsw_sp_fib_destroy(vr
->fib
);
462 static void mlxsw_sp_vr_destroy(struct mlxsw_sp
*mlxsw_sp
,
463 struct mlxsw_sp_vr
*vr
)
465 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp
, vr
);
466 mlxsw_sp_lpm_tree_put(mlxsw_sp
, vr
->lpm_tree
);
467 mlxsw_sp_fib_destroy(vr
->fib
);
472 mlxsw_sp_vr_lpm_tree_check(struct mlxsw_sp
*mlxsw_sp
, struct mlxsw_sp_vr
*vr
,
473 struct mlxsw_sp_prefix_usage
*req_prefix_usage
)
475 struct mlxsw_sp_lpm_tree
*lpm_tree
;
477 if (mlxsw_sp_prefix_usage_eq(req_prefix_usage
,
478 &vr
->lpm_tree
->prefix_usage
))
481 lpm_tree
= mlxsw_sp_lpm_tree_get(mlxsw_sp
, req_prefix_usage
,
483 if (IS_ERR(lpm_tree
)) {
484 /* We failed to get a tree according to the required
485 * prefix usage. However, the current tree might be still good
486 * for us if our requirement is subset of the prefixes used
489 if (mlxsw_sp_prefix_usage_subset(req_prefix_usage
,
490 &vr
->lpm_tree
->prefix_usage
))
492 return PTR_ERR(lpm_tree
);
495 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp
, vr
);
496 mlxsw_sp_lpm_tree_put(mlxsw_sp
, vr
->lpm_tree
);
497 vr
->lpm_tree
= lpm_tree
;
498 return mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp
, vr
);
501 static struct mlxsw_sp_vr
*mlxsw_sp_vr_get(struct mlxsw_sp
*mlxsw_sp
,
502 unsigned char prefix_len
,
504 enum mlxsw_sp_l3proto proto
)
506 struct mlxsw_sp_vr
*vr
;
509 tb_id
= mlxsw_sp_fix_tb_id(tb_id
);
510 vr
= mlxsw_sp_vr_find(mlxsw_sp
, tb_id
, proto
);
512 vr
= mlxsw_sp_vr_create(mlxsw_sp
, prefix_len
, tb_id
, proto
);
516 struct mlxsw_sp_prefix_usage req_prefix_usage
;
518 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage
,
519 &vr
->fib
->prefix_usage
);
520 mlxsw_sp_prefix_usage_set(&req_prefix_usage
, prefix_len
);
521 /* Need to replace LPM tree in case new prefix is required. */
522 err
= mlxsw_sp_vr_lpm_tree_check(mlxsw_sp
, vr
,
530 static void mlxsw_sp_vr_put(struct mlxsw_sp
*mlxsw_sp
, struct mlxsw_sp_vr
*vr
)
532 /* Destroy virtual router entity in case the associated FIB is empty
533 * and allow it to be used for other tables in future. Otherwise,
534 * check if some prefix usage did not disappear and change tree if
535 * that is the case. Note that in case new, smaller tree cannot be
536 * allocated, the original one will be kept being used.
538 if (mlxsw_sp_prefix_usage_none(&vr
->fib
->prefix_usage
))
539 mlxsw_sp_vr_destroy(mlxsw_sp
, vr
);
541 mlxsw_sp_vr_lpm_tree_check(mlxsw_sp
, vr
,
542 &vr
->fib
->prefix_usage
);
545 static void mlxsw_sp_vrs_init(struct mlxsw_sp
*mlxsw_sp
)
547 struct mlxsw_sp_vr
*vr
;
550 for (i
= 0; i
< MLXSW_SP_VIRTUAL_ROUTER_MAX
; i
++) {
551 vr
= &mlxsw_sp
->router
.vrs
[i
];
556 struct mlxsw_sp_neigh_key
{
557 unsigned char addr
[sizeof(struct in6_addr
)];
558 struct net_device
*dev
;
561 struct mlxsw_sp_neigh_entry
{
562 struct rhash_head ht_node
;
563 struct mlxsw_sp_neigh_key key
;
567 struct delayed_work dw
;
568 struct mlxsw_sp_port
*mlxsw_sp_port
;
569 unsigned char ha
[ETH_ALEN
];
570 struct list_head nexthop_list
; /* list of nexthops using
573 struct list_head nexthop_neighs_list_node
;
576 static const struct rhashtable_params mlxsw_sp_neigh_ht_params
= {
577 .key_offset
= offsetof(struct mlxsw_sp_neigh_entry
, key
),
578 .head_offset
= offsetof(struct mlxsw_sp_neigh_entry
, ht_node
),
579 .key_len
= sizeof(struct mlxsw_sp_neigh_key
),
583 mlxsw_sp_neigh_entry_insert(struct mlxsw_sp
*mlxsw_sp
,
584 struct mlxsw_sp_neigh_entry
*neigh_entry
)
586 return rhashtable_insert_fast(&mlxsw_sp
->router
.neigh_ht
,
587 &neigh_entry
->ht_node
,
588 mlxsw_sp_neigh_ht_params
);
592 mlxsw_sp_neigh_entry_remove(struct mlxsw_sp
*mlxsw_sp
,
593 struct mlxsw_sp_neigh_entry
*neigh_entry
)
595 rhashtable_remove_fast(&mlxsw_sp
->router
.neigh_ht
,
596 &neigh_entry
->ht_node
,
597 mlxsw_sp_neigh_ht_params
);
600 static void mlxsw_sp_router_neigh_update_hw(struct work_struct
*work
);
602 static struct mlxsw_sp_neigh_entry
*
603 mlxsw_sp_neigh_entry_create(const void *addr
, size_t addr_len
,
604 struct net_device
*dev
, u16 rif
,
607 struct mlxsw_sp_neigh_entry
*neigh_entry
;
609 neigh_entry
= kzalloc(sizeof(*neigh_entry
), GFP_ATOMIC
);
612 memcpy(neigh_entry
->key
.addr
, addr
, addr_len
);
613 neigh_entry
->key
.dev
= dev
;
614 neigh_entry
->rif
= rif
;
616 INIT_DELAYED_WORK(&neigh_entry
->dw
, mlxsw_sp_router_neigh_update_hw
);
617 INIT_LIST_HEAD(&neigh_entry
->nexthop_list
);
622 mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp_neigh_entry
*neigh_entry
)
627 static struct mlxsw_sp_neigh_entry
*
628 mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp
*mlxsw_sp
, const void *addr
,
629 size_t addr_len
, struct net_device
*dev
)
631 struct mlxsw_sp_neigh_key key
= {{ 0 } };
633 memcpy(key
.addr
, addr
, addr_len
);
635 return rhashtable_lookup_fast(&mlxsw_sp
->router
.neigh_ht
,
636 &key
, mlxsw_sp_neigh_ht_params
);
639 int mlxsw_sp_router_neigh_construct(struct net_device
*dev
,
642 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
643 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
644 struct mlxsw_sp_neigh_entry
*neigh_entry
;
645 struct mlxsw_sp_rif
*r
;
649 if (n
->tbl
!= &arp_tbl
)
652 dip
= ntohl(*((__be32
*) n
->primary_key
));
653 neigh_entry
= mlxsw_sp_neigh_entry_lookup(mlxsw_sp
, &dip
, sizeof(dip
),
656 WARN_ON(neigh_entry
->n
!= n
);
660 r
= mlxsw_sp_rif_find_by_dev(mlxsw_sp
, dev
);
664 neigh_entry
= mlxsw_sp_neigh_entry_create(&dip
, sizeof(dip
), n
->dev
,
668 err
= mlxsw_sp_neigh_entry_insert(mlxsw_sp
, neigh_entry
);
670 goto err_neigh_entry_insert
;
673 err_neigh_entry_insert
:
674 mlxsw_sp_neigh_entry_destroy(neigh_entry
);
678 void mlxsw_sp_router_neigh_destroy(struct net_device
*dev
,
681 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
682 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
683 struct mlxsw_sp_neigh_entry
*neigh_entry
;
686 if (n
->tbl
!= &arp_tbl
)
689 dip
= ntohl(*((__be32
*) n
->primary_key
));
690 neigh_entry
= mlxsw_sp_neigh_entry_lookup(mlxsw_sp
, &dip
, sizeof(dip
),
694 mlxsw_sp_neigh_entry_remove(mlxsw_sp
, neigh_entry
);
695 mlxsw_sp_neigh_entry_destroy(neigh_entry
);
699 mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp
*mlxsw_sp
)
701 unsigned long interval
= NEIGH_VAR(&arp_tbl
.parms
, DELAY_PROBE_TIME
);
703 mlxsw_sp
->router
.neighs_update
.interval
= jiffies_to_msecs(interval
);
706 static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp
*mlxsw_sp
,
710 struct net_device
*dev
;
716 mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl
, ent_index
, &rif
, &dip
);
718 if (!mlxsw_sp
->rifs
[rif
]) {
719 dev_err_ratelimited(mlxsw_sp
->bus_info
->dev
, "Incorrect RIF in neighbour entry\n");
724 dev
= mlxsw_sp
->rifs
[rif
]->dev
;
725 n
= neigh_lookup(&arp_tbl
, &dipn
, dev
);
727 netdev_err(dev
, "Failed to find matching neighbour for IP=%pI4h\n",
732 netdev_dbg(dev
, "Updating neighbour with IP=%pI4h\n", &dip
);
733 neigh_event_send(n
, NULL
);
737 static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp
*mlxsw_sp
,
744 num_entries
= mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl
,
746 /* Hardware starts counting at 0, so add 1. */
749 /* Each record consists of several neighbour entries. */
750 for (i
= 0; i
< num_entries
; i
++) {
753 ent_index
= rec_index
* MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC
+ i
;
754 mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp
, rauhtd_pl
,
760 static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp
*mlxsw_sp
,
761 char *rauhtd_pl
, int rec_index
)
763 switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl
, rec_index
)) {
764 case MLXSW_REG_RAUHTD_TYPE_IPV4
:
765 mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp
, rauhtd_pl
,
768 case MLXSW_REG_RAUHTD_TYPE_IPV6
:
774 static int mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp
*mlxsw_sp
)
780 rauhtd_pl
= kmalloc(MLXSW_REG_RAUHTD_LEN
, GFP_KERNEL
);
784 /* Make sure the neighbour's netdev isn't removed in the
789 mlxsw_reg_rauhtd_pack(rauhtd_pl
, MLXSW_REG_RAUHTD_TYPE_IPV4
);
790 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(rauhtd
),
793 dev_err_ratelimited(mlxsw_sp
->bus_info
->dev
, "Failed to dump neighbour talbe\n");
796 num_rec
= mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl
);
797 for (i
= 0; i
< num_rec
; i
++)
798 mlxsw_sp_router_neigh_rec_process(mlxsw_sp
, rauhtd_pl
,
807 static void mlxsw_sp_router_neighs_update_nh(struct mlxsw_sp
*mlxsw_sp
)
809 struct mlxsw_sp_neigh_entry
*neigh_entry
;
811 /* Take RTNL mutex here to prevent lists from changes */
813 list_for_each_entry(neigh_entry
, &mlxsw_sp
->router
.nexthop_neighs_list
,
814 nexthop_neighs_list_node
) {
815 /* If this neigh have nexthops, make the kernel think this neigh
816 * is active regardless of the traffic.
818 if (!list_empty(&neigh_entry
->nexthop_list
))
819 neigh_event_send(neigh_entry
->n
, NULL
);
825 mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp
*mlxsw_sp
)
827 unsigned long interval
= mlxsw_sp
->router
.neighs_update
.interval
;
829 mlxsw_core_schedule_dw(&mlxsw_sp
->router
.neighs_update
.dw
,
830 msecs_to_jiffies(interval
));
833 static void mlxsw_sp_router_neighs_update_work(struct work_struct
*work
)
835 struct mlxsw_sp
*mlxsw_sp
= container_of(work
, struct mlxsw_sp
,
836 router
.neighs_update
.dw
.work
);
839 err
= mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp
);
841 dev_err(mlxsw_sp
->bus_info
->dev
, "Could not update kernel for neigh activity");
843 mlxsw_sp_router_neighs_update_nh(mlxsw_sp
);
845 mlxsw_sp_router_neighs_update_work_schedule(mlxsw_sp
);
848 static void mlxsw_sp_router_probe_unresolved_nexthops(struct work_struct
*work
)
850 struct mlxsw_sp_neigh_entry
*neigh_entry
;
851 struct mlxsw_sp
*mlxsw_sp
= container_of(work
, struct mlxsw_sp
,
852 router
.nexthop_probe_dw
.work
);
854 /* Iterate over nexthop neighbours, find those who are unresolved and
855 * send arp on them. This solves the chicken-egg problem when
856 * the nexthop wouldn't get offloaded until the neighbor is resolved
857 * but it wouldn't get resolved ever in case traffic is flowing in HW
858 * using different nexthop.
860 * Take RTNL mutex here to prevent lists from changes.
863 list_for_each_entry(neigh_entry
, &mlxsw_sp
->router
.nexthop_neighs_list
,
864 nexthop_neighs_list_node
) {
865 if (!(neigh_entry
->n
->nud_state
& NUD_VALID
) &&
866 !list_empty(&neigh_entry
->nexthop_list
))
867 neigh_event_send(neigh_entry
->n
, NULL
);
871 mlxsw_core_schedule_dw(&mlxsw_sp
->router
.nexthop_probe_dw
,
872 MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL
);
876 mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp
*mlxsw_sp
,
877 struct mlxsw_sp_neigh_entry
*neigh_entry
,
880 static void mlxsw_sp_router_neigh_update_hw(struct work_struct
*work
)
882 struct mlxsw_sp_neigh_entry
*neigh_entry
=
883 container_of(work
, struct mlxsw_sp_neigh_entry
, dw
.work
);
884 struct neighbour
*n
= neigh_entry
->n
;
885 struct mlxsw_sp_port
*mlxsw_sp_port
= neigh_entry
->mlxsw_sp_port
;
886 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
887 char rauht_pl
[MLXSW_REG_RAUHT_LEN
];
888 struct net_device
*dev
;
889 bool entry_connected
;
897 read_lock_bh(&n
->lock
);
898 dip
= ntohl(*((__be32
*) n
->primary_key
));
899 memcpy(neigh_entry
->ha
, n
->ha
, sizeof(neigh_entry
->ha
));
900 nud_state
= n
->nud_state
;
902 read_unlock_bh(&n
->lock
);
904 entry_connected
= nud_state
& NUD_VALID
;
905 adding
= (!neigh_entry
->offloaded
) && entry_connected
;
906 updating
= neigh_entry
->offloaded
&& entry_connected
;
907 removing
= neigh_entry
->offloaded
&& !entry_connected
;
909 if (adding
|| updating
) {
910 mlxsw_reg_rauht_pack4(rauht_pl
, MLXSW_REG_RAUHT_OP_WRITE_ADD
,
912 neigh_entry
->ha
, dip
);
913 err
= mlxsw_reg_write(mlxsw_sp
->core
,
914 MLXSW_REG(rauht
), rauht_pl
);
916 netdev_err(dev
, "Could not add neigh %pI4h\n", &dip
);
917 neigh_entry
->offloaded
= false;
919 neigh_entry
->offloaded
= true;
921 mlxsw_sp_nexthop_neigh_update(mlxsw_sp
, neigh_entry
, false);
922 } else if (removing
) {
923 mlxsw_reg_rauht_pack4(rauht_pl
, MLXSW_REG_RAUHT_OP_WRITE_DELETE
,
925 neigh_entry
->ha
, dip
);
926 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(rauht
),
929 netdev_err(dev
, "Could not delete neigh %pI4h\n", &dip
);
930 neigh_entry
->offloaded
= true;
932 neigh_entry
->offloaded
= false;
934 mlxsw_sp_nexthop_neigh_update(mlxsw_sp
, neigh_entry
, true);
938 mlxsw_sp_port_dev_put(mlxsw_sp_port
);
941 static int mlxsw_sp_router_netevent_event(struct notifier_block
*unused
,
942 unsigned long event
, void *ptr
)
944 struct mlxsw_sp_neigh_entry
*neigh_entry
;
945 struct mlxsw_sp_port
*mlxsw_sp_port
;
946 struct mlxsw_sp
*mlxsw_sp
;
947 unsigned long interval
;
948 struct net_device
*dev
;
949 struct neigh_parms
*p
;
954 case NETEVENT_DELAY_PROBE_TIME_UPDATE
:
957 /* We don't care about changes in the default table. */
958 if (!p
->dev
|| p
->tbl
!= &arp_tbl
)
961 /* We are in atomic context and can't take RTNL mutex,
962 * so use RCU variant to walk the device chain.
964 mlxsw_sp_port
= mlxsw_sp_port_lower_dev_hold(p
->dev
);
968 mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
969 interval
= jiffies_to_msecs(NEIGH_VAR(p
, DELAY_PROBE_TIME
));
970 mlxsw_sp
->router
.neighs_update
.interval
= interval
;
972 mlxsw_sp_port_dev_put(mlxsw_sp_port
);
974 case NETEVENT_NEIGH_UPDATE
:
978 if (n
->tbl
!= &arp_tbl
)
981 mlxsw_sp_port
= mlxsw_sp_port_lower_dev_hold(dev
);
985 mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
986 dip
= ntohl(*((__be32
*) n
->primary_key
));
987 neigh_entry
= mlxsw_sp_neigh_entry_lookup(mlxsw_sp
,
991 if (WARN_ON(!neigh_entry
) || WARN_ON(neigh_entry
->n
!= n
)) {
992 mlxsw_sp_port_dev_put(mlxsw_sp_port
);
995 neigh_entry
->mlxsw_sp_port
= mlxsw_sp_port
;
997 /* Take a reference to ensure the neighbour won't be
998 * destructed until we drop the reference in delayed
1002 if (!mlxsw_core_schedule_dw(&neigh_entry
->dw
, 0)) {
1004 mlxsw_sp_port_dev_put(mlxsw_sp_port
);
1012 static struct notifier_block mlxsw_sp_router_netevent_nb __read_mostly
= {
1013 .notifier_call
= mlxsw_sp_router_netevent_event
,
1016 static int mlxsw_sp_neigh_init(struct mlxsw_sp
*mlxsw_sp
)
1020 err
= rhashtable_init(&mlxsw_sp
->router
.neigh_ht
,
1021 &mlxsw_sp_neigh_ht_params
);
1025 /* Initialize the polling interval according to the default
1028 mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp
);
1030 err
= register_netevent_notifier(&mlxsw_sp_router_netevent_nb
);
1032 goto err_register_netevent_notifier
;
1034 /* Create the delayed works for the activity_update */
1035 INIT_DELAYED_WORK(&mlxsw_sp
->router
.neighs_update
.dw
,
1036 mlxsw_sp_router_neighs_update_work
);
1037 INIT_DELAYED_WORK(&mlxsw_sp
->router
.nexthop_probe_dw
,
1038 mlxsw_sp_router_probe_unresolved_nexthops
);
1039 mlxsw_core_schedule_dw(&mlxsw_sp
->router
.neighs_update
.dw
, 0);
1040 mlxsw_core_schedule_dw(&mlxsw_sp
->router
.nexthop_probe_dw
, 0);
1043 err_register_netevent_notifier
:
1044 rhashtable_destroy(&mlxsw_sp
->router
.neigh_ht
);
1048 static void mlxsw_sp_neigh_fini(struct mlxsw_sp
*mlxsw_sp
)
1050 cancel_delayed_work_sync(&mlxsw_sp
->router
.neighs_update
.dw
);
1051 cancel_delayed_work_sync(&mlxsw_sp
->router
.nexthop_probe_dw
);
1052 unregister_netevent_notifier(&mlxsw_sp_router_netevent_nb
);
1053 rhashtable_destroy(&mlxsw_sp
->router
.neigh_ht
);
1056 struct mlxsw_sp_nexthop
{
1057 struct list_head neigh_list_node
; /* member of neigh entry list */
1058 struct mlxsw_sp_nexthop_group
*nh_grp
; /* pointer back to the group
1061 u8 should_offload
:1, /* set indicates this neigh is connected and
1062 * should be put to KVD linear area of this group.
1064 offloaded
:1, /* set in case the neigh is actually put into
1065 * KVD linear area of this group.
1067 update
:1; /* set indicates that MAC of this neigh should be
1070 struct mlxsw_sp_neigh_entry
*neigh_entry
;
1073 struct mlxsw_sp_nexthop_group
{
1074 struct list_head list
; /* node in mlxsw->router.nexthop_group_list */
1075 struct list_head fib_list
; /* list of fib entries that use this group */
1076 u8 adj_index_valid
:1;
1080 struct mlxsw_sp_nexthop nexthops
[0];
1083 static int mlxsw_sp_adj_index_mass_update_vr(struct mlxsw_sp
*mlxsw_sp
,
1084 struct mlxsw_sp_vr
*vr
,
1085 u32 adj_index
, u16 ecmp_size
,
1089 char raleu_pl
[MLXSW_REG_RALEU_LEN
];
1091 mlxsw_reg_raleu_pack(raleu_pl
, vr
->proto
, vr
->id
,
1092 adj_index
, ecmp_size
,
1093 new_adj_index
, new_ecmp_size
);
1094 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(raleu
), raleu_pl
);
1097 static int mlxsw_sp_adj_index_mass_update(struct mlxsw_sp
*mlxsw_sp
,
1098 struct mlxsw_sp_nexthop_group
*nh_grp
,
1099 u32 old_adj_index
, u16 old_ecmp_size
)
1101 struct mlxsw_sp_fib_entry
*fib_entry
;
1102 struct mlxsw_sp_vr
*vr
= NULL
;
1105 list_for_each_entry(fib_entry
, &nh_grp
->fib_list
, nexthop_group_node
) {
1106 if (vr
== fib_entry
->vr
)
1109 err
= mlxsw_sp_adj_index_mass_update_vr(mlxsw_sp
, vr
,
1120 static int mlxsw_sp_nexthop_mac_update(struct mlxsw_sp
*mlxsw_sp
, u32 adj_index
,
1121 struct mlxsw_sp_nexthop
*nh
)
1123 struct mlxsw_sp_neigh_entry
*neigh_entry
= nh
->neigh_entry
;
1124 char ratr_pl
[MLXSW_REG_RATR_LEN
];
1126 mlxsw_reg_ratr_pack(ratr_pl
, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY
,
1127 true, adj_index
, neigh_entry
->rif
);
1128 mlxsw_reg_ratr_eth_entry_pack(ratr_pl
, neigh_entry
->ha
);
1129 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ratr
), ratr_pl
);
1133 mlxsw_sp_nexthop_group_mac_update(struct mlxsw_sp
*mlxsw_sp
,
1134 struct mlxsw_sp_nexthop_group
*nh_grp
)
1136 u32 adj_index
= nh_grp
->adj_index
; /* base */
1137 struct mlxsw_sp_nexthop
*nh
;
1141 for (i
= 0; i
< nh_grp
->count
; i
++) {
1142 nh
= &nh_grp
->nexthops
[i
];
1144 if (!nh
->should_offload
) {
1150 err
= mlxsw_sp_nexthop_mac_update(mlxsw_sp
,
1162 static int mlxsw_sp_fib_entry_update(struct mlxsw_sp
*mlxsw_sp
,
1163 struct mlxsw_sp_fib_entry
*fib_entry
);
1166 mlxsw_sp_nexthop_fib_entries_update(struct mlxsw_sp
*mlxsw_sp
,
1167 struct mlxsw_sp_nexthop_group
*nh_grp
)
1169 struct mlxsw_sp_fib_entry
*fib_entry
;
1172 list_for_each_entry(fib_entry
, &nh_grp
->fib_list
, nexthop_group_node
) {
1173 err
= mlxsw_sp_fib_entry_update(mlxsw_sp
, fib_entry
);
1181 mlxsw_sp_nexthop_group_refresh(struct mlxsw_sp
*mlxsw_sp
,
1182 struct mlxsw_sp_nexthop_group
*nh_grp
)
1184 struct mlxsw_sp_nexthop
*nh
;
1185 bool offload_change
= false;
1188 bool old_adj_index_valid
;
1195 for (i
= 0; i
< nh_grp
->count
; i
++) {
1196 nh
= &nh_grp
->nexthops
[i
];
1198 if (nh
->should_offload
^ nh
->offloaded
) {
1199 offload_change
= true;
1200 if (nh
->should_offload
)
1203 if (nh
->should_offload
)
1206 if (!offload_change
) {
1207 /* Nothing was added or removed, so no need to reallocate. Just
1208 * update MAC on existing adjacency indexes.
1210 err
= mlxsw_sp_nexthop_group_mac_update(mlxsw_sp
, nh_grp
);
1212 dev_warn(mlxsw_sp
->bus_info
->dev
, "Failed to update neigh MAC in adjacency table.\n");
1218 /* No neigh of this group is connected so we just set
1219 * the trap and let everthing flow through kernel.
1223 ret
= mlxsw_sp_kvdl_alloc(mlxsw_sp
, ecmp_size
);
1225 /* We ran out of KVD linear space, just set the
1226 * trap and let everything flow through kernel.
1228 dev_warn(mlxsw_sp
->bus_info
->dev
, "Failed to allocate KVD linear area for nexthop group.\n");
1232 old_adj_index_valid
= nh_grp
->adj_index_valid
;
1233 old_adj_index
= nh_grp
->adj_index
;
1234 old_ecmp_size
= nh_grp
->ecmp_size
;
1235 nh_grp
->adj_index_valid
= 1;
1236 nh_grp
->adj_index
= adj_index
;
1237 nh_grp
->ecmp_size
= ecmp_size
;
1238 err
= mlxsw_sp_nexthop_group_mac_update(mlxsw_sp
, nh_grp
);
1240 dev_warn(mlxsw_sp
->bus_info
->dev
, "Failed to update neigh MAC in adjacency table.\n");
1244 if (!old_adj_index_valid
) {
1245 /* The trap was set for fib entries, so we have to call
1246 * fib entry update to unset it and use adjacency index.
1248 err
= mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp
, nh_grp
);
1250 dev_warn(mlxsw_sp
->bus_info
->dev
, "Failed to add adjacency index to fib entries.\n");
1256 err
= mlxsw_sp_adj_index_mass_update(mlxsw_sp
, nh_grp
,
1257 old_adj_index
, old_ecmp_size
);
1258 mlxsw_sp_kvdl_free(mlxsw_sp
, old_adj_index
);
1260 dev_warn(mlxsw_sp
->bus_info
->dev
, "Failed to mass-update adjacency index for nexthop group.\n");
1266 old_adj_index_valid
= nh_grp
->adj_index_valid
;
1267 nh_grp
->adj_index_valid
= 0;
1268 for (i
= 0; i
< nh_grp
->count
; i
++) {
1269 nh
= &nh_grp
->nexthops
[i
];
1272 err
= mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp
, nh_grp
);
1274 dev_warn(mlxsw_sp
->bus_info
->dev
, "Failed to set traps for fib entries.\n");
1275 if (old_adj_index_valid
)
1276 mlxsw_sp_kvdl_free(mlxsw_sp
, nh_grp
->adj_index
);
1279 static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop
*nh
,
1282 if (!removing
&& !nh
->should_offload
)
1283 nh
->should_offload
= 1;
1284 else if (removing
&& nh
->offloaded
)
1285 nh
->should_offload
= 0;
1290 mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp
*mlxsw_sp
,
1291 struct mlxsw_sp_neigh_entry
*neigh_entry
,
1294 struct mlxsw_sp_nexthop
*nh
;
1296 /* Take RTNL mutex here to prevent lists from changes */
1298 list_for_each_entry(nh
, &neigh_entry
->nexthop_list
,
1300 __mlxsw_sp_nexthop_neigh_update(nh
, removing
);
1301 mlxsw_sp_nexthop_group_refresh(mlxsw_sp
, nh
->nh_grp
);
1306 static int mlxsw_sp_nexthop_init(struct mlxsw_sp
*mlxsw_sp
,
1307 struct mlxsw_sp_nexthop_group
*nh_grp
,
1308 struct mlxsw_sp_nexthop
*nh
,
1309 struct fib_nh
*fib_nh
)
1311 struct mlxsw_sp_neigh_entry
*neigh_entry
;
1312 u32 gwip
= ntohl(fib_nh
->nh_gw
);
1313 struct net_device
*dev
= fib_nh
->nh_dev
;
1314 struct neighbour
*n
;
1317 neigh_entry
= mlxsw_sp_neigh_entry_lookup(mlxsw_sp
, &gwip
,
1320 __be32 gwipn
= htonl(gwip
);
1322 n
= neigh_create(&arp_tbl
, &gwipn
, dev
);
1325 neigh_event_send(n
, NULL
);
1326 neigh_entry
= mlxsw_sp_neigh_entry_lookup(mlxsw_sp
, &gwip
,
1333 /* Take a reference of neigh here ensuring that neigh would
1334 * not be detructed before the nexthop entry is finished.
1335 * The second branch takes the reference in neith_create()
1341 /* If that is the first nexthop connected to that neigh, add to
1342 * nexthop_neighs_list
1344 if (list_empty(&neigh_entry
->nexthop_list
))
1345 list_add_tail(&neigh_entry
->nexthop_neighs_list_node
,
1346 &mlxsw_sp
->router
.nexthop_neighs_list
);
1348 nh
->nh_grp
= nh_grp
;
1349 nh
->neigh_entry
= neigh_entry
;
1350 list_add_tail(&nh
->neigh_list_node
, &neigh_entry
->nexthop_list
);
1351 read_lock_bh(&n
->lock
);
1352 nud_state
= n
->nud_state
;
1353 read_unlock_bh(&n
->lock
);
1354 __mlxsw_sp_nexthop_neigh_update(nh
, !(nud_state
& NUD_VALID
));
1359 static void mlxsw_sp_nexthop_fini(struct mlxsw_sp
*mlxsw_sp
,
1360 struct mlxsw_sp_nexthop
*nh
)
1362 struct mlxsw_sp_neigh_entry
*neigh_entry
= nh
->neigh_entry
;
1364 list_del(&nh
->neigh_list_node
);
1366 /* If that is the last nexthop connected to that neigh, remove from
1367 * nexthop_neighs_list
1369 if (list_empty(&nh
->neigh_entry
->nexthop_list
))
1370 list_del(&nh
->neigh_entry
->nexthop_neighs_list_node
);
1372 neigh_release(neigh_entry
->n
);
1375 static struct mlxsw_sp_nexthop_group
*
1376 mlxsw_sp_nexthop_group_create(struct mlxsw_sp
*mlxsw_sp
, struct fib_info
*fi
)
1378 struct mlxsw_sp_nexthop_group
*nh_grp
;
1379 struct mlxsw_sp_nexthop
*nh
;
1380 struct fib_nh
*fib_nh
;
1385 alloc_size
= sizeof(*nh_grp
) +
1386 fi
->fib_nhs
* sizeof(struct mlxsw_sp_nexthop
);
1387 nh_grp
= kzalloc(alloc_size
, GFP_KERNEL
);
1389 return ERR_PTR(-ENOMEM
);
1390 INIT_LIST_HEAD(&nh_grp
->fib_list
);
1391 nh_grp
->count
= fi
->fib_nhs
;
1392 for (i
= 0; i
< nh_grp
->count
; i
++) {
1393 nh
= &nh_grp
->nexthops
[i
];
1394 fib_nh
= &fi
->fib_nh
[i
];
1395 err
= mlxsw_sp_nexthop_init(mlxsw_sp
, nh_grp
, nh
, fib_nh
);
1397 goto err_nexthop_init
;
1399 list_add_tail(&nh_grp
->list
, &mlxsw_sp
->router
.nexthop_group_list
);
1400 mlxsw_sp_nexthop_group_refresh(mlxsw_sp
, nh_grp
);
1404 for (i
--; i
>= 0; i
--)
1405 mlxsw_sp_nexthop_fini(mlxsw_sp
, nh
);
1407 return ERR_PTR(err
);
1411 mlxsw_sp_nexthop_group_destroy(struct mlxsw_sp
*mlxsw_sp
,
1412 struct mlxsw_sp_nexthop_group
*nh_grp
)
1414 struct mlxsw_sp_nexthop
*nh
;
1417 list_del(&nh_grp
->list
);
1418 for (i
= 0; i
< nh_grp
->count
; i
++) {
1419 nh
= &nh_grp
->nexthops
[i
];
1420 mlxsw_sp_nexthop_fini(mlxsw_sp
, nh
);
1425 static bool mlxsw_sp_nexthop_match(struct mlxsw_sp_nexthop
*nh
,
1426 struct fib_info
*fi
)
1430 for (i
= 0; i
< fi
->fib_nhs
; i
++) {
1431 struct fib_nh
*fib_nh
= &fi
->fib_nh
[i
];
1432 u32 gwip
= ntohl(fib_nh
->nh_gw
);
1434 if (memcmp(nh
->neigh_entry
->key
.addr
,
1435 &gwip
, sizeof(u32
)) == 0 &&
1436 nh
->neigh_entry
->key
.dev
== fib_nh
->nh_dev
)
1442 static bool mlxsw_sp_nexthop_group_match(struct mlxsw_sp_nexthop_group
*nh_grp
,
1443 struct fib_info
*fi
)
1447 if (nh_grp
->count
!= fi
->fib_nhs
)
1449 for (i
= 0; i
< nh_grp
->count
; i
++) {
1450 struct mlxsw_sp_nexthop
*nh
= &nh_grp
->nexthops
[i
];
1452 if (!mlxsw_sp_nexthop_match(nh
, fi
))
1458 static struct mlxsw_sp_nexthop_group
*
1459 mlxsw_sp_nexthop_group_find(struct mlxsw_sp
*mlxsw_sp
, struct fib_info
*fi
)
1461 struct mlxsw_sp_nexthop_group
*nh_grp
;
1463 list_for_each_entry(nh_grp
, &mlxsw_sp
->router
.nexthop_group_list
,
1465 if (mlxsw_sp_nexthop_group_match(nh_grp
, fi
))
1471 static int mlxsw_sp_nexthop_group_get(struct mlxsw_sp
*mlxsw_sp
,
1472 struct mlxsw_sp_fib_entry
*fib_entry
,
1473 struct fib_info
*fi
)
1475 struct mlxsw_sp_nexthop_group
*nh_grp
;
1477 nh_grp
= mlxsw_sp_nexthop_group_find(mlxsw_sp
, fi
);
1479 nh_grp
= mlxsw_sp_nexthop_group_create(mlxsw_sp
, fi
);
1481 return PTR_ERR(nh_grp
);
1483 list_add_tail(&fib_entry
->nexthop_group_node
, &nh_grp
->fib_list
);
1484 fib_entry
->nh_group
= nh_grp
;
1488 static void mlxsw_sp_nexthop_group_put(struct mlxsw_sp
*mlxsw_sp
,
1489 struct mlxsw_sp_fib_entry
*fib_entry
)
1491 struct mlxsw_sp_nexthop_group
*nh_grp
= fib_entry
->nh_group
;
1493 list_del(&fib_entry
->nexthop_group_node
);
1494 if (!list_empty(&nh_grp
->fib_list
))
1496 mlxsw_sp_nexthop_group_destroy(mlxsw_sp
, nh_grp
);
1499 static int __mlxsw_sp_router_init(struct mlxsw_sp
*mlxsw_sp
)
1501 char rgcr_pl
[MLXSW_REG_RGCR_LEN
];
1503 mlxsw_reg_rgcr_pack(rgcr_pl
, true);
1504 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl
, MLXSW_SP_RIF_MAX
);
1505 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(rgcr
), rgcr_pl
);
1508 static void __mlxsw_sp_router_fini(struct mlxsw_sp
*mlxsw_sp
)
1510 char rgcr_pl
[MLXSW_REG_RGCR_LEN
];
1512 mlxsw_reg_rgcr_pack(rgcr_pl
, false);
1513 mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(rgcr
), rgcr_pl
);
1516 int mlxsw_sp_router_init(struct mlxsw_sp
*mlxsw_sp
)
1520 INIT_LIST_HEAD(&mlxsw_sp
->router
.nexthop_neighs_list
);
1521 INIT_LIST_HEAD(&mlxsw_sp
->router
.nexthop_group_list
);
1522 err
= __mlxsw_sp_router_init(mlxsw_sp
);
1525 mlxsw_sp_lpm_init(mlxsw_sp
);
1526 mlxsw_sp_vrs_init(mlxsw_sp
);
1527 return mlxsw_sp_neigh_init(mlxsw_sp
);
1530 void mlxsw_sp_router_fini(struct mlxsw_sp
*mlxsw_sp
)
1532 mlxsw_sp_neigh_fini(mlxsw_sp
);
1533 __mlxsw_sp_router_fini(mlxsw_sp
);
1536 static int mlxsw_sp_fib_entry_op4_remote(struct mlxsw_sp
*mlxsw_sp
,
1537 struct mlxsw_sp_fib_entry
*fib_entry
,
1538 enum mlxsw_reg_ralue_op op
)
1540 char ralue_pl
[MLXSW_REG_RALUE_LEN
];
1541 u32
*p_dip
= (u32
*) fib_entry
->key
.addr
;
1542 struct mlxsw_sp_vr
*vr
= fib_entry
->vr
;
1543 enum mlxsw_reg_ralue_trap_action trap_action
;
1545 u32 adjacency_index
= 0;
1548 /* In case the nexthop group adjacency index is valid, use it
1549 * with provided ECMP size. Otherwise, setup trap and pass
1550 * traffic to kernel.
1552 if (fib_entry
->nh_group
->adj_index_valid
) {
1553 trap_action
= MLXSW_REG_RALUE_TRAP_ACTION_NOP
;
1554 adjacency_index
= fib_entry
->nh_group
->adj_index
;
1555 ecmp_size
= fib_entry
->nh_group
->ecmp_size
;
1557 trap_action
= MLXSW_REG_RALUE_TRAP_ACTION_TRAP
;
1558 trap_id
= MLXSW_TRAP_ID_RTR_INGRESS0
;
1561 mlxsw_reg_ralue_pack4(ralue_pl
, vr
->proto
, op
, vr
->id
,
1562 fib_entry
->key
.prefix_len
, *p_dip
);
1563 mlxsw_reg_ralue_act_remote_pack(ralue_pl
, trap_action
, trap_id
,
1564 adjacency_index
, ecmp_size
);
1565 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ralue
), ralue_pl
);
1568 static int mlxsw_sp_fib_entry_op4_local(struct mlxsw_sp
*mlxsw_sp
,
1569 struct mlxsw_sp_fib_entry
*fib_entry
,
1570 enum mlxsw_reg_ralue_op op
)
1572 char ralue_pl
[MLXSW_REG_RALUE_LEN
];
1573 u32
*p_dip
= (u32
*) fib_entry
->key
.addr
;
1574 struct mlxsw_sp_vr
*vr
= fib_entry
->vr
;
1576 mlxsw_reg_ralue_pack4(ralue_pl
, vr
->proto
, op
, vr
->id
,
1577 fib_entry
->key
.prefix_len
, *p_dip
);
1578 mlxsw_reg_ralue_act_local_pack(ralue_pl
,
1579 MLXSW_REG_RALUE_TRAP_ACTION_NOP
, 0,
1581 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ralue
), ralue_pl
);
1584 static int mlxsw_sp_fib_entry_op4_trap(struct mlxsw_sp
*mlxsw_sp
,
1585 struct mlxsw_sp_fib_entry
*fib_entry
,
1586 enum mlxsw_reg_ralue_op op
)
1588 char ralue_pl
[MLXSW_REG_RALUE_LEN
];
1589 u32
*p_dip
= (u32
*) fib_entry
->key
.addr
;
1590 struct mlxsw_sp_vr
*vr
= fib_entry
->vr
;
1592 mlxsw_reg_ralue_pack4(ralue_pl
, vr
->proto
, op
, vr
->id
,
1593 fib_entry
->key
.prefix_len
, *p_dip
);
1594 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl
);
1595 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ralue
), ralue_pl
);
1598 static int mlxsw_sp_fib_entry_op4(struct mlxsw_sp
*mlxsw_sp
,
1599 struct mlxsw_sp_fib_entry
*fib_entry
,
1600 enum mlxsw_reg_ralue_op op
)
1602 switch (fib_entry
->type
) {
1603 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE
:
1604 return mlxsw_sp_fib_entry_op4_remote(mlxsw_sp
, fib_entry
, op
);
1605 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL
:
1606 return mlxsw_sp_fib_entry_op4_local(mlxsw_sp
, fib_entry
, op
);
1607 case MLXSW_SP_FIB_ENTRY_TYPE_TRAP
:
1608 return mlxsw_sp_fib_entry_op4_trap(mlxsw_sp
, fib_entry
, op
);
1613 static int mlxsw_sp_fib_entry_op(struct mlxsw_sp
*mlxsw_sp
,
1614 struct mlxsw_sp_fib_entry
*fib_entry
,
1615 enum mlxsw_reg_ralue_op op
)
1617 switch (fib_entry
->vr
->proto
) {
1618 case MLXSW_SP_L3_PROTO_IPV4
:
1619 return mlxsw_sp_fib_entry_op4(mlxsw_sp
, fib_entry
, op
);
1620 case MLXSW_SP_L3_PROTO_IPV6
:
1626 static int mlxsw_sp_fib_entry_update(struct mlxsw_sp
*mlxsw_sp
,
1627 struct mlxsw_sp_fib_entry
*fib_entry
)
1629 enum mlxsw_reg_ralue_op op
;
1631 op
= !fib_entry
->added
? MLXSW_REG_RALUE_OP_WRITE_WRITE
:
1632 MLXSW_REG_RALUE_OP_WRITE_UPDATE
;
1633 return mlxsw_sp_fib_entry_op(mlxsw_sp
, fib_entry
, op
);
1636 static int mlxsw_sp_fib_entry_del(struct mlxsw_sp
*mlxsw_sp
,
1637 struct mlxsw_sp_fib_entry
*fib_entry
)
1639 return mlxsw_sp_fib_entry_op(mlxsw_sp
, fib_entry
,
1640 MLXSW_REG_RALUE_OP_WRITE_DELETE
);
1643 struct mlxsw_sp_router_fib4_add_info
{
1644 struct switchdev_trans_item tritem
;
1645 struct mlxsw_sp
*mlxsw_sp
;
1646 struct mlxsw_sp_fib_entry
*fib_entry
;
1649 static void mlxsw_sp_router_fib4_add_info_destroy(void const *data
)
1651 const struct mlxsw_sp_router_fib4_add_info
*info
= data
;
1652 struct mlxsw_sp_fib_entry
*fib_entry
= info
->fib_entry
;
1653 struct mlxsw_sp
*mlxsw_sp
= info
->mlxsw_sp
;
1654 struct mlxsw_sp_vr
*vr
= fib_entry
->vr
;
1656 mlxsw_sp_fib_entry_destroy(fib_entry
);
1657 mlxsw_sp_vr_put(mlxsw_sp
, vr
);
1662 mlxsw_sp_router_fib4_entry_init(struct mlxsw_sp
*mlxsw_sp
,
1663 const struct switchdev_obj_ipv4_fib
*fib4
,
1664 struct mlxsw_sp_fib_entry
*fib_entry
)
1666 struct fib_info
*fi
= fib4
->fi
;
1668 if (fib4
->type
== RTN_LOCAL
|| fib4
->type
== RTN_BROADCAST
) {
1669 fib_entry
->type
= MLXSW_SP_FIB_ENTRY_TYPE_TRAP
;
1672 if (fib4
->type
!= RTN_UNICAST
)
1675 if (fi
->fib_scope
!= RT_SCOPE_UNIVERSE
) {
1676 struct mlxsw_sp_rif
*r
;
1678 fib_entry
->type
= MLXSW_SP_FIB_ENTRY_TYPE_LOCAL
;
1679 r
= mlxsw_sp_rif_find_by_dev(mlxsw_sp
, fi
->fib_dev
);
1682 fib_entry
->rif
= r
->rif
;
1685 fib_entry
->type
= MLXSW_SP_FIB_ENTRY_TYPE_REMOTE
;
1686 return mlxsw_sp_nexthop_group_get(mlxsw_sp
, fib_entry
, fi
);
1690 mlxsw_sp_router_fib4_entry_fini(struct mlxsw_sp
*mlxsw_sp
,
1691 struct mlxsw_sp_fib_entry
*fib_entry
)
1693 if (fib_entry
->type
!= MLXSW_SP_FIB_ENTRY_TYPE_REMOTE
)
1695 mlxsw_sp_nexthop_group_put(mlxsw_sp
, fib_entry
);
1699 mlxsw_sp_router_fib4_add_prepare(struct mlxsw_sp_port
*mlxsw_sp_port
,
1700 const struct switchdev_obj_ipv4_fib
*fib4
,
1701 struct switchdev_trans
*trans
)
1703 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1704 struct mlxsw_sp_router_fib4_add_info
*info
;
1705 struct mlxsw_sp_fib_entry
*fib_entry
;
1706 struct mlxsw_sp_vr
*vr
;
1709 vr
= mlxsw_sp_vr_get(mlxsw_sp
, fib4
->dst_len
, fib4
->tb_id
,
1710 MLXSW_SP_L3_PROTO_IPV4
);
1714 fib_entry
= mlxsw_sp_fib_entry_create(vr
->fib
, &fib4
->dst
,
1715 sizeof(fib4
->dst
), fib4
->dst_len
);
1718 goto err_fib_entry_create
;
1722 err
= mlxsw_sp_router_fib4_entry_init(mlxsw_sp
, fib4
, fib_entry
);
1724 goto err_fib4_entry_init
;
1726 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
1729 goto err_alloc_info
;
1731 info
->mlxsw_sp
= mlxsw_sp
;
1732 info
->fib_entry
= fib_entry
;
1733 switchdev_trans_item_enqueue(trans
, info
,
1734 mlxsw_sp_router_fib4_add_info_destroy
,
1739 mlxsw_sp_router_fib4_entry_fini(mlxsw_sp
, fib_entry
);
1740 err_fib4_entry_init
:
1741 mlxsw_sp_fib_entry_destroy(fib_entry
);
1742 err_fib_entry_create
:
1743 mlxsw_sp_vr_put(mlxsw_sp
, vr
);
1748 mlxsw_sp_router_fib4_add_commit(struct mlxsw_sp_port
*mlxsw_sp_port
,
1749 const struct switchdev_obj_ipv4_fib
*fib4
,
1750 struct switchdev_trans
*trans
)
1752 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1753 struct mlxsw_sp_router_fib4_add_info
*info
;
1754 struct mlxsw_sp_fib_entry
*fib_entry
;
1755 struct mlxsw_sp_vr
*vr
;
1758 info
= switchdev_trans_item_dequeue(trans
);
1759 fib_entry
= info
->fib_entry
;
1763 err
= mlxsw_sp_fib_entry_insert(fib_entry
->vr
->fib
, fib_entry
);
1765 goto err_fib_entry_insert
;
1766 err
= mlxsw_sp_fib_entry_update(mlxsw_sp
, fib_entry
);
1768 goto err_fib_entry_add
;
1772 mlxsw_sp_fib_entry_remove(vr
->fib
, fib_entry
);
1773 err_fib_entry_insert
:
1774 mlxsw_sp_router_fib4_entry_fini(mlxsw_sp
, fib_entry
);
1775 mlxsw_sp_fib_entry_destroy(fib_entry
);
1776 mlxsw_sp_vr_put(mlxsw_sp
, vr
);
1780 int mlxsw_sp_router_fib4_add(struct mlxsw_sp_port
*mlxsw_sp_port
,
1781 const struct switchdev_obj_ipv4_fib
*fib4
,
1782 struct switchdev_trans
*trans
)
1784 if (switchdev_trans_ph_prepare(trans
))
1785 return mlxsw_sp_router_fib4_add_prepare(mlxsw_sp_port
,
1787 return mlxsw_sp_router_fib4_add_commit(mlxsw_sp_port
,
1791 int mlxsw_sp_router_fib4_del(struct mlxsw_sp_port
*mlxsw_sp_port
,
1792 const struct switchdev_obj_ipv4_fib
*fib4
)
1794 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1795 struct mlxsw_sp_fib_entry
*fib_entry
;
1796 struct mlxsw_sp_vr
*vr
;
1798 vr
= mlxsw_sp_vr_find(mlxsw_sp
, fib4
->tb_id
, MLXSW_SP_L3_PROTO_IPV4
);
1800 dev_warn(mlxsw_sp
->bus_info
->dev
, "Failed to find virtual router for FIB4 entry being removed.\n");
1803 fib_entry
= mlxsw_sp_fib_entry_lookup(vr
->fib
, &fib4
->dst
,
1804 sizeof(fib4
->dst
), fib4
->dst_len
);
1806 dev_warn(mlxsw_sp
->bus_info
->dev
, "Failed to find FIB4 entry being removed.\n");
1809 mlxsw_sp_fib_entry_del(mlxsw_sp_port
->mlxsw_sp
, fib_entry
);
1810 mlxsw_sp_fib_entry_remove(vr
->fib
, fib_entry
);
1811 mlxsw_sp_router_fib4_entry_fini(mlxsw_sp
, fib_entry
);
1812 mlxsw_sp_fib_entry_destroy(fib_entry
);
1813 mlxsw_sp_vr_put(mlxsw_sp
, vr
);