1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2016-2018 Mellanox Technologies. All rights reserved */
4 #include <linux/kernel.h>
5 #include <linux/types.h>
6 #include <linux/rhashtable.h>
7 #include <linux/bitops.h>
9 #include <linux/notifier.h>
10 #include <linux/inetdevice.h>
11 #include <linux/netdevice.h>
12 #include <linux/if_bridge.h>
13 #include <linux/socket.h>
14 #include <linux/route.h>
15 #include <linux/gcd.h>
16 #include <linux/if_macvlan.h>
17 #include <linux/refcount.h>
18 #include <linux/jhash.h>
19 #include <linux/net_namespace.h>
20 #include <linux/mutex.h>
21 #include <net/netevent.h>
22 #include <net/neighbour.h>
24 #include <net/ip_fib.h>
25 #include <net/ip6_fib.h>
26 #include <net/nexthop.h>
27 #include <net/fib_rules.h>
28 #include <net/ip_tunnels.h>
29 #include <net/l3mdev.h>
30 #include <net/addrconf.h>
31 #include <net/ndisc.h>
33 #include <net/fib_notifier.h>
34 #include <net/switchdev.h>
39 #include "spectrum_cnt.h"
40 #include "spectrum_dpipe.h"
41 #include "spectrum_ipip.h"
42 #include "spectrum_mr.h"
43 #include "spectrum_mr_tcam.h"
44 #include "spectrum_router.h"
45 #include "spectrum_span.h"
49 struct mlxsw_sp_lpm_tree;
50 struct mlxsw_sp_rif_ops;
53 struct list_head nexthop_list;
54 struct list_head neigh_list;
55 struct net_device *dev; /* NULL for underlay RIF */
56 struct mlxsw_sp_fid *fid;
57 unsigned char addr[ETH_ALEN];
61 const struct mlxsw_sp_rif_ops *ops;
62 struct mlxsw_sp *mlxsw_sp;
64 unsigned int counter_ingress;
65 bool counter_ingress_valid;
66 unsigned int counter_egress;
67 bool counter_egress_valid;
70 struct mlxsw_sp_rif_params {
71 struct net_device *dev;
80 struct mlxsw_sp_rif_subport {
81 struct mlxsw_sp_rif common;
91 struct mlxsw_sp_rif_ipip_lb {
92 struct mlxsw_sp_rif common;
93 struct mlxsw_sp_rif_ipip_lb_config lb_config;
94 u16 ul_vr_id; /* Reserved for Spectrum-2. */
95 u16 ul_rif_id; /* Reserved for Spectrum. */
98 struct mlxsw_sp_rif_params_ipip_lb {
99 struct mlxsw_sp_rif_params common;
100 struct mlxsw_sp_rif_ipip_lb_config lb_config;
103 struct mlxsw_sp_rif_ops {
104 enum mlxsw_sp_rif_type type;
107 void (*setup)(struct mlxsw_sp_rif *rif,
108 const struct mlxsw_sp_rif_params *params);
109 int (*configure)(struct mlxsw_sp_rif *rif);
110 void (*deconfigure)(struct mlxsw_sp_rif *rif);
111 struct mlxsw_sp_fid * (*fid_get)(struct mlxsw_sp_rif *rif,
112 struct netlink_ext_ack *extack);
113 void (*fdb_del)(struct mlxsw_sp_rif *rif, const char *mac);
116 static struct mlxsw_sp_rif *
117 mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
118 const struct net_device *dev);
119 static void mlxsw_sp_rif_destroy(struct mlxsw_sp_rif *rif);
120 static void mlxsw_sp_lpm_tree_hold(struct mlxsw_sp_lpm_tree *lpm_tree);
121 static void mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp,
122 struct mlxsw_sp_lpm_tree *lpm_tree);
123 static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp,
124 const struct mlxsw_sp_fib *fib,
126 static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp,
127 const struct mlxsw_sp_fib *fib);
129 static unsigned int *
130 mlxsw_sp_rif_p_counter_get(struct mlxsw_sp_rif *rif,
131 enum mlxsw_sp_rif_counter_dir dir)
134 case MLXSW_SP_RIF_COUNTER_EGRESS:
135 return &rif->counter_egress;
136 case MLXSW_SP_RIF_COUNTER_INGRESS:
137 return &rif->counter_ingress;
143 mlxsw_sp_rif_counter_valid_get(struct mlxsw_sp_rif *rif,
144 enum mlxsw_sp_rif_counter_dir dir)
147 case MLXSW_SP_RIF_COUNTER_EGRESS:
148 return rif->counter_egress_valid;
149 case MLXSW_SP_RIF_COUNTER_INGRESS:
150 return rif->counter_ingress_valid;
156 mlxsw_sp_rif_counter_valid_set(struct mlxsw_sp_rif *rif,
157 enum mlxsw_sp_rif_counter_dir dir,
161 case MLXSW_SP_RIF_COUNTER_EGRESS:
162 rif->counter_egress_valid = valid;
164 case MLXSW_SP_RIF_COUNTER_INGRESS:
165 rif->counter_ingress_valid = valid;
170 static int mlxsw_sp_rif_counter_edit(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
171 unsigned int counter_index, bool enable,
172 enum mlxsw_sp_rif_counter_dir dir)
174 char ritr_pl[MLXSW_REG_RITR_LEN];
175 bool is_egress = false;
178 if (dir == MLXSW_SP_RIF_COUNTER_EGRESS)
180 mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
181 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
185 mlxsw_reg_ritr_counter_pack(ritr_pl, counter_index, enable,
187 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
190 int mlxsw_sp_rif_counter_value_get(struct mlxsw_sp *mlxsw_sp,
191 struct mlxsw_sp_rif *rif,
192 enum mlxsw_sp_rif_counter_dir dir, u64 *cnt)
194 char ricnt_pl[MLXSW_REG_RICNT_LEN];
195 unsigned int *p_counter_index;
199 valid = mlxsw_sp_rif_counter_valid_get(rif, dir);
203 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
204 if (!p_counter_index)
206 mlxsw_reg_ricnt_pack(ricnt_pl, *p_counter_index,
207 MLXSW_REG_RICNT_OPCODE_NOP);
208 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ricnt), ricnt_pl);
211 *cnt = mlxsw_reg_ricnt_good_unicast_packets_get(ricnt_pl);
215 static int mlxsw_sp_rif_counter_clear(struct mlxsw_sp *mlxsw_sp,
216 unsigned int counter_index)
218 char ricnt_pl[MLXSW_REG_RICNT_LEN];
220 mlxsw_reg_ricnt_pack(ricnt_pl, counter_index,
221 MLXSW_REG_RICNT_OPCODE_CLEAR);
222 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ricnt), ricnt_pl);
225 int mlxsw_sp_rif_counter_alloc(struct mlxsw_sp *mlxsw_sp,
226 struct mlxsw_sp_rif *rif,
227 enum mlxsw_sp_rif_counter_dir dir)
229 unsigned int *p_counter_index;
232 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
233 if (!p_counter_index)
235 err = mlxsw_sp_counter_alloc(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
240 err = mlxsw_sp_rif_counter_clear(mlxsw_sp, *p_counter_index);
242 goto err_counter_clear;
244 err = mlxsw_sp_rif_counter_edit(mlxsw_sp, rif->rif_index,
245 *p_counter_index, true, dir);
247 goto err_counter_edit;
248 mlxsw_sp_rif_counter_valid_set(rif, dir, true);
253 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
258 void mlxsw_sp_rif_counter_free(struct mlxsw_sp *mlxsw_sp,
259 struct mlxsw_sp_rif *rif,
260 enum mlxsw_sp_rif_counter_dir dir)
262 unsigned int *p_counter_index;
264 if (!mlxsw_sp_rif_counter_valid_get(rif, dir))
267 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
268 if (WARN_ON(!p_counter_index))
270 mlxsw_sp_rif_counter_edit(mlxsw_sp, rif->rif_index,
271 *p_counter_index, false, dir);
272 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
274 mlxsw_sp_rif_counter_valid_set(rif, dir, false);
277 static void mlxsw_sp_rif_counters_alloc(struct mlxsw_sp_rif *rif)
279 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
280 struct devlink *devlink;
282 devlink = priv_to_devlink(mlxsw_sp->core);
283 if (!devlink_dpipe_table_counter_enabled(devlink,
284 MLXSW_SP_DPIPE_TABLE_NAME_ERIF))
286 mlxsw_sp_rif_counter_alloc(mlxsw_sp, rif, MLXSW_SP_RIF_COUNTER_EGRESS);
289 static void mlxsw_sp_rif_counters_free(struct mlxsw_sp_rif *rif)
291 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
293 mlxsw_sp_rif_counter_free(mlxsw_sp, rif, MLXSW_SP_RIF_COUNTER_EGRESS);
296 #define MLXSW_SP_PREFIX_COUNT (sizeof(struct in6_addr) * BITS_PER_BYTE + 1)
298 struct mlxsw_sp_prefix_usage {
299 DECLARE_BITMAP(b, MLXSW_SP_PREFIX_COUNT);
302 #define mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) \
303 for_each_set_bit(prefix, (prefix_usage)->b, MLXSW_SP_PREFIX_COUNT)
306 mlxsw_sp_prefix_usage_eq(struct mlxsw_sp_prefix_usage *prefix_usage1,
307 struct mlxsw_sp_prefix_usage *prefix_usage2)
309 return !memcmp(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
313 mlxsw_sp_prefix_usage_cpy(struct mlxsw_sp_prefix_usage *prefix_usage1,
314 struct mlxsw_sp_prefix_usage *prefix_usage2)
316 memcpy(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
320 mlxsw_sp_prefix_usage_set(struct mlxsw_sp_prefix_usage *prefix_usage,
321 unsigned char prefix_len)
323 set_bit(prefix_len, prefix_usage->b);
327 mlxsw_sp_prefix_usage_clear(struct mlxsw_sp_prefix_usage *prefix_usage,
328 unsigned char prefix_len)
330 clear_bit(prefix_len, prefix_usage->b);
333 struct mlxsw_sp_fib_key {
334 unsigned char addr[sizeof(struct in6_addr)];
335 unsigned char prefix_len;
338 enum mlxsw_sp_fib_entry_type {
339 MLXSW_SP_FIB_ENTRY_TYPE_REMOTE,
340 MLXSW_SP_FIB_ENTRY_TYPE_LOCAL,
341 MLXSW_SP_FIB_ENTRY_TYPE_TRAP,
342 MLXSW_SP_FIB_ENTRY_TYPE_BLACKHOLE,
343 MLXSW_SP_FIB_ENTRY_TYPE_UNREACHABLE,
345 /* This is a special case of local delivery, where a packet should be
346 * decapsulated on reception. Note that there is no corresponding ENCAP,
347 * because that's a type of next hop, not of FIB entry. (There can be
348 * several next hops in a REMOTE entry, and some of them may be
349 * encapsulating entries.)
351 MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP,
352 MLXSW_SP_FIB_ENTRY_TYPE_NVE_DECAP,
355 struct mlxsw_sp_nexthop_group_info;
356 struct mlxsw_sp_nexthop_group;
357 struct mlxsw_sp_fib_entry;
359 struct mlxsw_sp_fib_node {
360 struct mlxsw_sp_fib_entry *fib_entry;
361 struct list_head list;
362 struct rhash_head ht_node;
363 struct mlxsw_sp_fib *fib;
364 struct mlxsw_sp_fib_key key;
367 struct mlxsw_sp_fib_entry_decap {
368 struct mlxsw_sp_ipip_entry *ipip_entry;
372 static struct mlxsw_sp_fib_entry_priv *
373 mlxsw_sp_fib_entry_priv_create(const struct mlxsw_sp_router_ll_ops *ll_ops)
375 struct mlxsw_sp_fib_entry_priv *priv;
377 if (!ll_ops->fib_entry_priv_size)
378 /* No need to have priv */
381 priv = kzalloc(sizeof(*priv) + ll_ops->fib_entry_priv_size, GFP_KERNEL);
383 return ERR_PTR(-ENOMEM);
384 refcount_set(&priv->refcnt, 1);
389 mlxsw_sp_fib_entry_priv_destroy(struct mlxsw_sp_fib_entry_priv *priv)
394 static void mlxsw_sp_fib_entry_priv_hold(struct mlxsw_sp_fib_entry_priv *priv)
396 refcount_inc(&priv->refcnt);
399 static void mlxsw_sp_fib_entry_priv_put(struct mlxsw_sp_fib_entry_priv *priv)
401 if (!priv || !refcount_dec_and_test(&priv->refcnt))
403 mlxsw_sp_fib_entry_priv_destroy(priv);
406 static void mlxsw_sp_fib_entry_op_ctx_priv_hold(struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
407 struct mlxsw_sp_fib_entry_priv *priv)
411 mlxsw_sp_fib_entry_priv_hold(priv);
412 list_add(&priv->list, &op_ctx->fib_entry_priv_list);
415 static void mlxsw_sp_fib_entry_op_ctx_priv_put_all(struct mlxsw_sp_fib_entry_op_ctx *op_ctx)
417 struct mlxsw_sp_fib_entry_priv *priv, *tmp;
419 list_for_each_entry_safe(priv, tmp, &op_ctx->fib_entry_priv_list, list)
420 mlxsw_sp_fib_entry_priv_put(priv);
421 INIT_LIST_HEAD(&op_ctx->fib_entry_priv_list);
424 struct mlxsw_sp_fib_entry {
425 struct mlxsw_sp_fib_node *fib_node;
426 enum mlxsw_sp_fib_entry_type type;
427 struct list_head nexthop_group_node;
428 struct mlxsw_sp_nexthop_group *nh_group;
429 struct mlxsw_sp_fib_entry_decap decap; /* Valid for decap entries. */
430 struct mlxsw_sp_fib_entry_priv *priv;
433 struct mlxsw_sp_fib4_entry {
434 struct mlxsw_sp_fib_entry common;
441 struct mlxsw_sp_fib6_entry {
442 struct mlxsw_sp_fib_entry common;
443 struct list_head rt6_list;
447 struct mlxsw_sp_rt6 {
448 struct list_head list;
449 struct fib6_info *rt;
452 struct mlxsw_sp_lpm_tree {
454 unsigned int ref_count;
455 enum mlxsw_sp_l3proto proto;
456 unsigned long prefix_ref_count[MLXSW_SP_PREFIX_COUNT];
457 struct mlxsw_sp_prefix_usage prefix_usage;
460 struct mlxsw_sp_fib {
461 struct rhashtable ht;
462 struct list_head node_list;
463 struct mlxsw_sp_vr *vr;
464 struct mlxsw_sp_lpm_tree *lpm_tree;
465 enum mlxsw_sp_l3proto proto;
466 const struct mlxsw_sp_router_ll_ops *ll_ops;
470 u16 id; /* virtual router ID */
471 u32 tb_id; /* kernel fib table id */
472 unsigned int rif_count;
473 struct mlxsw_sp_fib *fib4;
474 struct mlxsw_sp_fib *fib6;
475 struct mlxsw_sp_mr_table *mr_table[MLXSW_SP_L3_PROTO_MAX];
476 struct mlxsw_sp_rif *ul_rif;
477 refcount_t ul_rif_refcnt;
480 static int mlxsw_sp_router_ll_basic_ralta_write(struct mlxsw_sp *mlxsw_sp, char *xralta_pl)
482 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta),
483 xralta_pl + MLXSW_REG_XRALTA_RALTA_OFFSET);
486 static int mlxsw_sp_router_ll_basic_ralst_write(struct mlxsw_sp *mlxsw_sp, char *xralst_pl)
488 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst),
489 xralst_pl + MLXSW_REG_XRALST_RALST_OFFSET);
492 static int mlxsw_sp_router_ll_basic_raltb_write(struct mlxsw_sp *mlxsw_sp, char *xraltb_pl)
494 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb),
495 xraltb_pl + MLXSW_REG_XRALTB_RALTB_OFFSET);
498 static const struct rhashtable_params mlxsw_sp_fib_ht_params;
500 static struct mlxsw_sp_fib *mlxsw_sp_fib_create(struct mlxsw_sp *mlxsw_sp,
501 struct mlxsw_sp_vr *vr,
502 enum mlxsw_sp_l3proto proto)
504 const struct mlxsw_sp_router_ll_ops *ll_ops = mlxsw_sp->router->proto_ll_ops[proto];
505 struct mlxsw_sp_lpm_tree *lpm_tree;
506 struct mlxsw_sp_fib *fib;
509 lpm_tree = mlxsw_sp->router->lpm.proto_trees[proto];
510 fib = kzalloc(sizeof(*fib), GFP_KERNEL);
512 return ERR_PTR(-ENOMEM);
513 err = rhashtable_init(&fib->ht, &mlxsw_sp_fib_ht_params);
515 goto err_rhashtable_init;
516 INIT_LIST_HEAD(&fib->node_list);
519 fib->lpm_tree = lpm_tree;
520 fib->ll_ops = ll_ops;
521 mlxsw_sp_lpm_tree_hold(lpm_tree);
522 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, fib, lpm_tree->id);
524 goto err_lpm_tree_bind;
528 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
534 static void mlxsw_sp_fib_destroy(struct mlxsw_sp *mlxsw_sp,
535 struct mlxsw_sp_fib *fib)
537 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, fib);
538 mlxsw_sp_lpm_tree_put(mlxsw_sp, fib->lpm_tree);
539 WARN_ON(!list_empty(&fib->node_list));
540 rhashtable_destroy(&fib->ht);
544 static struct mlxsw_sp_lpm_tree *
545 mlxsw_sp_lpm_tree_find_unused(struct mlxsw_sp *mlxsw_sp)
547 static struct mlxsw_sp_lpm_tree *lpm_tree;
550 for (i = 0; i < mlxsw_sp->router->lpm.tree_count; i++) {
551 lpm_tree = &mlxsw_sp->router->lpm.trees[i];
552 if (lpm_tree->ref_count == 0)
558 static int mlxsw_sp_lpm_tree_alloc(struct mlxsw_sp *mlxsw_sp,
559 const struct mlxsw_sp_router_ll_ops *ll_ops,
560 struct mlxsw_sp_lpm_tree *lpm_tree)
562 char xralta_pl[MLXSW_REG_XRALTA_LEN];
564 mlxsw_reg_xralta_pack(xralta_pl, true,
565 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
567 return ll_ops->ralta_write(mlxsw_sp, xralta_pl);
570 static void mlxsw_sp_lpm_tree_free(struct mlxsw_sp *mlxsw_sp,
571 const struct mlxsw_sp_router_ll_ops *ll_ops,
572 struct mlxsw_sp_lpm_tree *lpm_tree)
574 char xralta_pl[MLXSW_REG_XRALTA_LEN];
576 mlxsw_reg_xralta_pack(xralta_pl, false,
577 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
579 ll_ops->ralta_write(mlxsw_sp, xralta_pl);
583 mlxsw_sp_lpm_tree_left_struct_set(struct mlxsw_sp *mlxsw_sp,
584 const struct mlxsw_sp_router_ll_ops *ll_ops,
585 struct mlxsw_sp_prefix_usage *prefix_usage,
586 struct mlxsw_sp_lpm_tree *lpm_tree)
588 char xralst_pl[MLXSW_REG_XRALST_LEN];
591 u8 last_prefix = MLXSW_REG_RALST_BIN_NO_CHILD;
593 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage)
596 mlxsw_reg_xralst_pack(xralst_pl, root_bin, lpm_tree->id);
597 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) {
600 mlxsw_reg_xralst_bin_pack(xralst_pl, prefix, last_prefix,
601 MLXSW_REG_RALST_BIN_NO_CHILD);
602 last_prefix = prefix;
604 return ll_ops->ralst_write(mlxsw_sp, xralst_pl);
607 static struct mlxsw_sp_lpm_tree *
608 mlxsw_sp_lpm_tree_create(struct mlxsw_sp *mlxsw_sp,
609 const struct mlxsw_sp_router_ll_ops *ll_ops,
610 struct mlxsw_sp_prefix_usage *prefix_usage,
611 enum mlxsw_sp_l3proto proto)
613 struct mlxsw_sp_lpm_tree *lpm_tree;
616 lpm_tree = mlxsw_sp_lpm_tree_find_unused(mlxsw_sp);
618 return ERR_PTR(-EBUSY);
619 lpm_tree->proto = proto;
620 err = mlxsw_sp_lpm_tree_alloc(mlxsw_sp, ll_ops, lpm_tree);
624 err = mlxsw_sp_lpm_tree_left_struct_set(mlxsw_sp, ll_ops, prefix_usage, lpm_tree);
626 goto err_left_struct_set;
627 memcpy(&lpm_tree->prefix_usage, prefix_usage,
628 sizeof(lpm_tree->prefix_usage));
629 memset(&lpm_tree->prefix_ref_count, 0,
630 sizeof(lpm_tree->prefix_ref_count));
631 lpm_tree->ref_count = 1;
635 mlxsw_sp_lpm_tree_free(mlxsw_sp, ll_ops, lpm_tree);
639 static void mlxsw_sp_lpm_tree_destroy(struct mlxsw_sp *mlxsw_sp,
640 const struct mlxsw_sp_router_ll_ops *ll_ops,
641 struct mlxsw_sp_lpm_tree *lpm_tree)
643 mlxsw_sp_lpm_tree_free(mlxsw_sp, ll_ops, lpm_tree);
646 static struct mlxsw_sp_lpm_tree *
647 mlxsw_sp_lpm_tree_get(struct mlxsw_sp *mlxsw_sp,
648 struct mlxsw_sp_prefix_usage *prefix_usage,
649 enum mlxsw_sp_l3proto proto)
651 const struct mlxsw_sp_router_ll_ops *ll_ops = mlxsw_sp->router->proto_ll_ops[proto];
652 struct mlxsw_sp_lpm_tree *lpm_tree;
655 for (i = 0; i < mlxsw_sp->router->lpm.tree_count; i++) {
656 lpm_tree = &mlxsw_sp->router->lpm.trees[i];
657 if (lpm_tree->ref_count != 0 &&
658 lpm_tree->proto == proto &&
659 mlxsw_sp_prefix_usage_eq(&lpm_tree->prefix_usage,
661 mlxsw_sp_lpm_tree_hold(lpm_tree);
665 return mlxsw_sp_lpm_tree_create(mlxsw_sp, ll_ops, prefix_usage, proto);
668 static void mlxsw_sp_lpm_tree_hold(struct mlxsw_sp_lpm_tree *lpm_tree)
670 lpm_tree->ref_count++;
673 static void mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp,
674 struct mlxsw_sp_lpm_tree *lpm_tree)
676 const struct mlxsw_sp_router_ll_ops *ll_ops =
677 mlxsw_sp->router->proto_ll_ops[lpm_tree->proto];
679 if (--lpm_tree->ref_count == 0)
680 mlxsw_sp_lpm_tree_destroy(mlxsw_sp, ll_ops, lpm_tree);
683 #define MLXSW_SP_LPM_TREE_MIN 1 /* tree 0 is reserved */
685 static int mlxsw_sp_lpm_init(struct mlxsw_sp *mlxsw_sp)
687 struct mlxsw_sp_prefix_usage req_prefix_usage = {{ 0 } };
688 struct mlxsw_sp_lpm_tree *lpm_tree;
692 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LPM_TREES))
695 max_trees = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LPM_TREES);
696 mlxsw_sp->router->lpm.tree_count = max_trees - MLXSW_SP_LPM_TREE_MIN;
697 mlxsw_sp->router->lpm.trees = kcalloc(mlxsw_sp->router->lpm.tree_count,
698 sizeof(struct mlxsw_sp_lpm_tree),
700 if (!mlxsw_sp->router->lpm.trees)
703 for (i = 0; i < mlxsw_sp->router->lpm.tree_count; i++) {
704 lpm_tree = &mlxsw_sp->router->lpm.trees[i];
705 lpm_tree->id = i + MLXSW_SP_LPM_TREE_MIN;
708 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
709 MLXSW_SP_L3_PROTO_IPV4);
710 if (IS_ERR(lpm_tree)) {
711 err = PTR_ERR(lpm_tree);
712 goto err_ipv4_tree_get;
714 mlxsw_sp->router->lpm.proto_trees[MLXSW_SP_L3_PROTO_IPV4] = lpm_tree;
716 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
717 MLXSW_SP_L3_PROTO_IPV6);
718 if (IS_ERR(lpm_tree)) {
719 err = PTR_ERR(lpm_tree);
720 goto err_ipv6_tree_get;
722 mlxsw_sp->router->lpm.proto_trees[MLXSW_SP_L3_PROTO_IPV6] = lpm_tree;
727 lpm_tree = mlxsw_sp->router->lpm.proto_trees[MLXSW_SP_L3_PROTO_IPV4];
728 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
730 kfree(mlxsw_sp->router->lpm.trees);
734 static void mlxsw_sp_lpm_fini(struct mlxsw_sp *mlxsw_sp)
736 struct mlxsw_sp_lpm_tree *lpm_tree;
738 lpm_tree = mlxsw_sp->router->lpm.proto_trees[MLXSW_SP_L3_PROTO_IPV6];
739 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
741 lpm_tree = mlxsw_sp->router->lpm.proto_trees[MLXSW_SP_L3_PROTO_IPV4];
742 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
744 kfree(mlxsw_sp->router->lpm.trees);
747 static bool mlxsw_sp_vr_is_used(const struct mlxsw_sp_vr *vr)
749 return !!vr->fib4 || !!vr->fib6 ||
750 !!vr->mr_table[MLXSW_SP_L3_PROTO_IPV4] ||
751 !!vr->mr_table[MLXSW_SP_L3_PROTO_IPV6];
754 static struct mlxsw_sp_vr *mlxsw_sp_vr_find_unused(struct mlxsw_sp *mlxsw_sp)
756 struct mlxsw_sp_vr *vr;
759 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
760 vr = &mlxsw_sp->router->vrs[i];
761 if (!mlxsw_sp_vr_is_used(vr))
767 static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp,
768 const struct mlxsw_sp_fib *fib, u8 tree_id)
770 char xraltb_pl[MLXSW_REG_XRALTB_LEN];
772 mlxsw_reg_xraltb_pack(xraltb_pl, fib->vr->id,
773 (enum mlxsw_reg_ralxx_protocol) fib->proto,
775 return fib->ll_ops->raltb_write(mlxsw_sp, xraltb_pl);
778 static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp,
779 const struct mlxsw_sp_fib *fib)
781 char xraltb_pl[MLXSW_REG_XRALTB_LEN];
783 /* Bind to tree 0 which is default */
784 mlxsw_reg_xraltb_pack(xraltb_pl, fib->vr->id,
785 (enum mlxsw_reg_ralxx_protocol) fib->proto, 0);
786 return fib->ll_ops->raltb_write(mlxsw_sp, xraltb_pl);
789 static u32 mlxsw_sp_fix_tb_id(u32 tb_id)
791 /* For our purpose, squash main, default and local tables into one */
792 if (tb_id == RT_TABLE_LOCAL || tb_id == RT_TABLE_DEFAULT)
793 tb_id = RT_TABLE_MAIN;
797 static struct mlxsw_sp_vr *mlxsw_sp_vr_find(struct mlxsw_sp *mlxsw_sp,
800 struct mlxsw_sp_vr *vr;
803 tb_id = mlxsw_sp_fix_tb_id(tb_id);
805 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
806 vr = &mlxsw_sp->router->vrs[i];
807 if (mlxsw_sp_vr_is_used(vr) && vr->tb_id == tb_id)
813 int mlxsw_sp_router_tb_id_vr_id(struct mlxsw_sp *mlxsw_sp, u32 tb_id,
816 struct mlxsw_sp_vr *vr;
819 mutex_lock(&mlxsw_sp->router->lock);
820 vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id);
827 mutex_unlock(&mlxsw_sp->router->lock);
831 static struct mlxsw_sp_fib *mlxsw_sp_vr_fib(const struct mlxsw_sp_vr *vr,
832 enum mlxsw_sp_l3proto proto)
835 case MLXSW_SP_L3_PROTO_IPV4:
837 case MLXSW_SP_L3_PROTO_IPV6:
843 static struct mlxsw_sp_vr *mlxsw_sp_vr_create(struct mlxsw_sp *mlxsw_sp,
845 struct netlink_ext_ack *extack)
847 struct mlxsw_sp_mr_table *mr4_table, *mr6_table;
848 struct mlxsw_sp_fib *fib4;
849 struct mlxsw_sp_fib *fib6;
850 struct mlxsw_sp_vr *vr;
853 vr = mlxsw_sp_vr_find_unused(mlxsw_sp);
855 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported virtual routers");
856 return ERR_PTR(-EBUSY);
858 fib4 = mlxsw_sp_fib_create(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV4);
860 return ERR_CAST(fib4);
861 fib6 = mlxsw_sp_fib_create(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV6);
864 goto err_fib6_create;
866 mr4_table = mlxsw_sp_mr_table_create(mlxsw_sp, vr->id,
867 MLXSW_SP_L3_PROTO_IPV4);
868 if (IS_ERR(mr4_table)) {
869 err = PTR_ERR(mr4_table);
870 goto err_mr4_table_create;
872 mr6_table = mlxsw_sp_mr_table_create(mlxsw_sp, vr->id,
873 MLXSW_SP_L3_PROTO_IPV6);
874 if (IS_ERR(mr6_table)) {
875 err = PTR_ERR(mr6_table);
876 goto err_mr6_table_create;
881 vr->mr_table[MLXSW_SP_L3_PROTO_IPV4] = mr4_table;
882 vr->mr_table[MLXSW_SP_L3_PROTO_IPV6] = mr6_table;
886 err_mr6_table_create:
887 mlxsw_sp_mr_table_destroy(mr4_table);
888 err_mr4_table_create:
889 mlxsw_sp_fib_destroy(mlxsw_sp, fib6);
891 mlxsw_sp_fib_destroy(mlxsw_sp, fib4);
895 static void mlxsw_sp_vr_destroy(struct mlxsw_sp *mlxsw_sp,
896 struct mlxsw_sp_vr *vr)
898 mlxsw_sp_mr_table_destroy(vr->mr_table[MLXSW_SP_L3_PROTO_IPV6]);
899 vr->mr_table[MLXSW_SP_L3_PROTO_IPV6] = NULL;
900 mlxsw_sp_mr_table_destroy(vr->mr_table[MLXSW_SP_L3_PROTO_IPV4]);
901 vr->mr_table[MLXSW_SP_L3_PROTO_IPV4] = NULL;
902 mlxsw_sp_fib_destroy(mlxsw_sp, vr->fib6);
904 mlxsw_sp_fib_destroy(mlxsw_sp, vr->fib4);
908 static struct mlxsw_sp_vr *mlxsw_sp_vr_get(struct mlxsw_sp *mlxsw_sp, u32 tb_id,
909 struct netlink_ext_ack *extack)
911 struct mlxsw_sp_vr *vr;
913 tb_id = mlxsw_sp_fix_tb_id(tb_id);
914 vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id);
916 vr = mlxsw_sp_vr_create(mlxsw_sp, tb_id, extack);
920 static void mlxsw_sp_vr_put(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr)
922 if (!vr->rif_count && list_empty(&vr->fib4->node_list) &&
923 list_empty(&vr->fib6->node_list) &&
924 mlxsw_sp_mr_table_empty(vr->mr_table[MLXSW_SP_L3_PROTO_IPV4]) &&
925 mlxsw_sp_mr_table_empty(vr->mr_table[MLXSW_SP_L3_PROTO_IPV6]))
926 mlxsw_sp_vr_destroy(mlxsw_sp, vr);
930 mlxsw_sp_vr_lpm_tree_should_replace(struct mlxsw_sp_vr *vr,
931 enum mlxsw_sp_l3proto proto, u8 tree_id)
933 struct mlxsw_sp_fib *fib = mlxsw_sp_vr_fib(vr, proto);
935 if (!mlxsw_sp_vr_is_used(vr))
937 if (fib->lpm_tree->id == tree_id)
942 static int mlxsw_sp_vr_lpm_tree_replace(struct mlxsw_sp *mlxsw_sp,
943 struct mlxsw_sp_fib *fib,
944 struct mlxsw_sp_lpm_tree *new_tree)
946 struct mlxsw_sp_lpm_tree *old_tree = fib->lpm_tree;
949 fib->lpm_tree = new_tree;
950 mlxsw_sp_lpm_tree_hold(new_tree);
951 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, fib, new_tree->id);
954 mlxsw_sp_lpm_tree_put(mlxsw_sp, old_tree);
958 mlxsw_sp_lpm_tree_put(mlxsw_sp, new_tree);
959 fib->lpm_tree = old_tree;
963 static int mlxsw_sp_vrs_lpm_tree_replace(struct mlxsw_sp *mlxsw_sp,
964 struct mlxsw_sp_fib *fib,
965 struct mlxsw_sp_lpm_tree *new_tree)
967 enum mlxsw_sp_l3proto proto = fib->proto;
968 struct mlxsw_sp_lpm_tree *old_tree;
969 u8 old_id, new_id = new_tree->id;
970 struct mlxsw_sp_vr *vr;
973 old_tree = mlxsw_sp->router->lpm.proto_trees[proto];
974 old_id = old_tree->id;
976 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
977 vr = &mlxsw_sp->router->vrs[i];
978 if (!mlxsw_sp_vr_lpm_tree_should_replace(vr, proto, old_id))
980 err = mlxsw_sp_vr_lpm_tree_replace(mlxsw_sp,
981 mlxsw_sp_vr_fib(vr, proto),
984 goto err_tree_replace;
987 memcpy(new_tree->prefix_ref_count, old_tree->prefix_ref_count,
988 sizeof(new_tree->prefix_ref_count));
989 mlxsw_sp->router->lpm.proto_trees[proto] = new_tree;
990 mlxsw_sp_lpm_tree_put(mlxsw_sp, old_tree);
995 for (i--; i >= 0; i--) {
996 if (!mlxsw_sp_vr_lpm_tree_should_replace(vr, proto, new_id))
998 mlxsw_sp_vr_lpm_tree_replace(mlxsw_sp,
999 mlxsw_sp_vr_fib(vr, proto),
1005 static int mlxsw_sp_vrs_init(struct mlxsw_sp *mlxsw_sp)
1007 struct mlxsw_sp_vr *vr;
1011 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_VRS))
1014 max_vrs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS);
1015 mlxsw_sp->router->vrs = kcalloc(max_vrs, sizeof(struct mlxsw_sp_vr),
1017 if (!mlxsw_sp->router->vrs)
1020 for (i = 0; i < max_vrs; i++) {
1021 vr = &mlxsw_sp->router->vrs[i];
1028 static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp);
1030 static void mlxsw_sp_vrs_fini(struct mlxsw_sp *mlxsw_sp)
1032 /* At this stage we're guaranteed not to have new incoming
1033 * FIB notifications and the work queue is free from FIBs
1034 * sitting on top of mlxsw netdevs. However, we can still
1035 * have other FIBs queued. Flush the queue before flushing
1036 * the device's tables. No need for locks, as we're the only
1039 mlxsw_core_flush_owq();
1040 mlxsw_sp_router_fib_flush(mlxsw_sp);
1041 kfree(mlxsw_sp->router->vrs);
1044 static struct net_device *
1045 __mlxsw_sp_ipip_netdev_ul_dev_get(const struct net_device *ol_dev)
1047 struct ip_tunnel *tun = netdev_priv(ol_dev);
1048 struct net *net = dev_net(ol_dev);
1050 return dev_get_by_index_rcu(net, tun->parms.link);
1053 u32 mlxsw_sp_ipip_dev_ul_tb_id(const struct net_device *ol_dev)
1055 struct net_device *d;
1059 d = __mlxsw_sp_ipip_netdev_ul_dev_get(ol_dev);
1061 tb_id = l3mdev_fib_table(d) ? : RT_TABLE_MAIN;
1063 tb_id = RT_TABLE_MAIN;
1069 static struct mlxsw_sp_rif *
1070 mlxsw_sp_rif_create(struct mlxsw_sp *mlxsw_sp,
1071 const struct mlxsw_sp_rif_params *params,
1072 struct netlink_ext_ack *extack);
1074 static struct mlxsw_sp_rif_ipip_lb *
1075 mlxsw_sp_ipip_ol_ipip_lb_create(struct mlxsw_sp *mlxsw_sp,
1076 enum mlxsw_sp_ipip_type ipipt,
1077 struct net_device *ol_dev,
1078 struct netlink_ext_ack *extack)
1080 struct mlxsw_sp_rif_params_ipip_lb lb_params;
1081 const struct mlxsw_sp_ipip_ops *ipip_ops;
1082 struct mlxsw_sp_rif *rif;
1084 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipipt];
1085 lb_params = (struct mlxsw_sp_rif_params_ipip_lb) {
1086 .common.dev = ol_dev,
1087 .common.lag = false,
1088 .lb_config = ipip_ops->ol_loopback_config(mlxsw_sp, ol_dev),
1091 rif = mlxsw_sp_rif_create(mlxsw_sp, &lb_params.common, extack);
1093 return ERR_CAST(rif);
1094 return container_of(rif, struct mlxsw_sp_rif_ipip_lb, common);
1097 static struct mlxsw_sp_ipip_entry *
1098 mlxsw_sp_ipip_entry_alloc(struct mlxsw_sp *mlxsw_sp,
1099 enum mlxsw_sp_ipip_type ipipt,
1100 struct net_device *ol_dev)
1102 const struct mlxsw_sp_ipip_ops *ipip_ops;
1103 struct mlxsw_sp_ipip_entry *ipip_entry;
1104 struct mlxsw_sp_ipip_entry *ret = NULL;
1106 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipipt];
1107 ipip_entry = kzalloc(sizeof(*ipip_entry), GFP_KERNEL);
1109 return ERR_PTR(-ENOMEM);
1111 ipip_entry->ol_lb = mlxsw_sp_ipip_ol_ipip_lb_create(mlxsw_sp, ipipt,
1113 if (IS_ERR(ipip_entry->ol_lb)) {
1114 ret = ERR_CAST(ipip_entry->ol_lb);
1115 goto err_ol_ipip_lb_create;
1118 ipip_entry->ipipt = ipipt;
1119 ipip_entry->ol_dev = ol_dev;
1121 switch (ipip_ops->ul_proto) {
1122 case MLXSW_SP_L3_PROTO_IPV4:
1123 ipip_entry->parms4 = mlxsw_sp_ipip_netdev_parms4(ol_dev);
1125 case MLXSW_SP_L3_PROTO_IPV6:
1132 err_ol_ipip_lb_create:
1138 mlxsw_sp_ipip_entry_dealloc(struct mlxsw_sp_ipip_entry *ipip_entry)
1140 mlxsw_sp_rif_destroy(&ipip_entry->ol_lb->common);
1145 mlxsw_sp_ipip_entry_saddr_matches(struct mlxsw_sp *mlxsw_sp,
1146 const enum mlxsw_sp_l3proto ul_proto,
1147 union mlxsw_sp_l3addr saddr,
1149 struct mlxsw_sp_ipip_entry *ipip_entry)
1151 u32 tun_ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(ipip_entry->ol_dev);
1152 enum mlxsw_sp_ipip_type ipipt = ipip_entry->ipipt;
1153 union mlxsw_sp_l3addr tun_saddr;
1155 if (mlxsw_sp->router->ipip_ops_arr[ipipt]->ul_proto != ul_proto)
1158 tun_saddr = mlxsw_sp_ipip_netdev_saddr(ul_proto, ipip_entry->ol_dev);
1159 return tun_ul_tb_id == ul_tb_id &&
1160 mlxsw_sp_l3addr_eq(&tun_saddr, &saddr);
1164 mlxsw_sp_fib_entry_decap_init(struct mlxsw_sp *mlxsw_sp,
1165 struct mlxsw_sp_fib_entry *fib_entry,
1166 struct mlxsw_sp_ipip_entry *ipip_entry)
1171 err = mlxsw_sp_kvdl_alloc(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ,
1176 ipip_entry->decap_fib_entry = fib_entry;
1177 fib_entry->decap.ipip_entry = ipip_entry;
1178 fib_entry->decap.tunnel_index = tunnel_index;
1182 static void mlxsw_sp_fib_entry_decap_fini(struct mlxsw_sp *mlxsw_sp,
1183 struct mlxsw_sp_fib_entry *fib_entry)
1185 /* Unlink this node from the IPIP entry that it's the decap entry of. */
1186 fib_entry->decap.ipip_entry->decap_fib_entry = NULL;
1187 fib_entry->decap.ipip_entry = NULL;
1188 mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ,
1189 1, fib_entry->decap.tunnel_index);
1192 static struct mlxsw_sp_fib_node *
1193 mlxsw_sp_fib_node_lookup(struct mlxsw_sp_fib *fib, const void *addr,
1194 size_t addr_len, unsigned char prefix_len);
1195 static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1196 struct mlxsw_sp_fib_entry *fib_entry);
1199 mlxsw_sp_ipip_entry_demote_decap(struct mlxsw_sp *mlxsw_sp,
1200 struct mlxsw_sp_ipip_entry *ipip_entry)
1202 struct mlxsw_sp_fib_entry *fib_entry = ipip_entry->decap_fib_entry;
1204 mlxsw_sp_fib_entry_decap_fini(mlxsw_sp, fib_entry);
1205 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
1207 mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1211 mlxsw_sp_ipip_entry_promote_decap(struct mlxsw_sp *mlxsw_sp,
1212 struct mlxsw_sp_ipip_entry *ipip_entry,
1213 struct mlxsw_sp_fib_entry *decap_fib_entry)
1215 if (mlxsw_sp_fib_entry_decap_init(mlxsw_sp, decap_fib_entry,
1218 decap_fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP;
1220 if (mlxsw_sp_fib_entry_update(mlxsw_sp, decap_fib_entry))
1221 mlxsw_sp_ipip_entry_demote_decap(mlxsw_sp, ipip_entry);
1224 static struct mlxsw_sp_fib_entry *
1225 mlxsw_sp_router_ip2me_fib_entry_find(struct mlxsw_sp *mlxsw_sp, u32 tb_id,
1226 enum mlxsw_sp_l3proto proto,
1227 const union mlxsw_sp_l3addr *addr,
1228 enum mlxsw_sp_fib_entry_type type)
1230 struct mlxsw_sp_fib_node *fib_node;
1231 unsigned char addr_prefix_len;
1232 struct mlxsw_sp_fib *fib;
1233 struct mlxsw_sp_vr *vr;
1238 vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id);
1241 fib = mlxsw_sp_vr_fib(vr, proto);
1244 case MLXSW_SP_L3_PROTO_IPV4:
1245 addr4 = be32_to_cpu(addr->addr4);
1248 addr_prefix_len = 32;
1250 case MLXSW_SP_L3_PROTO_IPV6:
1256 fib_node = mlxsw_sp_fib_node_lookup(fib, addrp, addr_len,
1258 if (!fib_node || fib_node->fib_entry->type != type)
1261 return fib_node->fib_entry;
1264 /* Given an IPIP entry, find the corresponding decap route. */
1265 static struct mlxsw_sp_fib_entry *
1266 mlxsw_sp_ipip_entry_find_decap(struct mlxsw_sp *mlxsw_sp,
1267 struct mlxsw_sp_ipip_entry *ipip_entry)
1269 static struct mlxsw_sp_fib_node *fib_node;
1270 const struct mlxsw_sp_ipip_ops *ipip_ops;
1271 unsigned char saddr_prefix_len;
1272 union mlxsw_sp_l3addr saddr;
1273 struct mlxsw_sp_fib *ul_fib;
1274 struct mlxsw_sp_vr *ul_vr;
1280 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
1282 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(ipip_entry->ol_dev);
1283 ul_vr = mlxsw_sp_vr_find(mlxsw_sp, ul_tb_id);
1287 ul_fib = mlxsw_sp_vr_fib(ul_vr, ipip_ops->ul_proto);
1288 saddr = mlxsw_sp_ipip_netdev_saddr(ipip_ops->ul_proto,
1289 ipip_entry->ol_dev);
1291 switch (ipip_ops->ul_proto) {
1292 case MLXSW_SP_L3_PROTO_IPV4:
1293 saddr4 = be32_to_cpu(saddr.addr4);
1296 saddr_prefix_len = 32;
1303 fib_node = mlxsw_sp_fib_node_lookup(ul_fib, saddrp, saddr_len,
1306 fib_node->fib_entry->type != MLXSW_SP_FIB_ENTRY_TYPE_TRAP)
1309 return fib_node->fib_entry;
1312 static struct mlxsw_sp_ipip_entry *
1313 mlxsw_sp_ipip_entry_create(struct mlxsw_sp *mlxsw_sp,
1314 enum mlxsw_sp_ipip_type ipipt,
1315 struct net_device *ol_dev)
1317 struct mlxsw_sp_ipip_entry *ipip_entry;
1319 ipip_entry = mlxsw_sp_ipip_entry_alloc(mlxsw_sp, ipipt, ol_dev);
1320 if (IS_ERR(ipip_entry))
1323 list_add_tail(&ipip_entry->ipip_list_node,
1324 &mlxsw_sp->router->ipip_list);
1330 mlxsw_sp_ipip_entry_destroy(struct mlxsw_sp *mlxsw_sp,
1331 struct mlxsw_sp_ipip_entry *ipip_entry)
1333 list_del(&ipip_entry->ipip_list_node);
1334 mlxsw_sp_ipip_entry_dealloc(ipip_entry);
1338 mlxsw_sp_ipip_entry_matches_decap(struct mlxsw_sp *mlxsw_sp,
1339 const struct net_device *ul_dev,
1340 enum mlxsw_sp_l3proto ul_proto,
1341 union mlxsw_sp_l3addr ul_dip,
1342 struct mlxsw_sp_ipip_entry *ipip_entry)
1344 u32 ul_tb_id = l3mdev_fib_table(ul_dev) ? : RT_TABLE_MAIN;
1345 enum mlxsw_sp_ipip_type ipipt = ipip_entry->ipipt;
1347 if (mlxsw_sp->router->ipip_ops_arr[ipipt]->ul_proto != ul_proto)
1350 return mlxsw_sp_ipip_entry_saddr_matches(mlxsw_sp, ul_proto, ul_dip,
1351 ul_tb_id, ipip_entry);
1354 /* Given decap parameters, find the corresponding IPIP entry. */
1355 static struct mlxsw_sp_ipip_entry *
1356 mlxsw_sp_ipip_entry_find_by_decap(struct mlxsw_sp *mlxsw_sp,
1357 const struct net_device *ul_dev,
1358 enum mlxsw_sp_l3proto ul_proto,
1359 union mlxsw_sp_l3addr ul_dip)
1361 struct mlxsw_sp_ipip_entry *ipip_entry;
1363 list_for_each_entry(ipip_entry, &mlxsw_sp->router->ipip_list,
1365 if (mlxsw_sp_ipip_entry_matches_decap(mlxsw_sp, ul_dev,
1373 static bool mlxsw_sp_netdev_ipip_type(const struct mlxsw_sp *mlxsw_sp,
1374 const struct net_device *dev,
1375 enum mlxsw_sp_ipip_type *p_type)
1377 struct mlxsw_sp_router *router = mlxsw_sp->router;
1378 const struct mlxsw_sp_ipip_ops *ipip_ops;
1379 enum mlxsw_sp_ipip_type ipipt;
1381 for (ipipt = 0; ipipt < MLXSW_SP_IPIP_TYPE_MAX; ++ipipt) {
1382 ipip_ops = router->ipip_ops_arr[ipipt];
1383 if (dev->type == ipip_ops->dev_type) {
1392 bool mlxsw_sp_netdev_is_ipip_ol(const struct mlxsw_sp *mlxsw_sp,
1393 const struct net_device *dev)
1395 return mlxsw_sp_netdev_ipip_type(mlxsw_sp, dev, NULL);
1398 static struct mlxsw_sp_ipip_entry *
1399 mlxsw_sp_ipip_entry_find_by_ol_dev(struct mlxsw_sp *mlxsw_sp,
1400 const struct net_device *ol_dev)
1402 struct mlxsw_sp_ipip_entry *ipip_entry;
1404 list_for_each_entry(ipip_entry, &mlxsw_sp->router->ipip_list,
1406 if (ipip_entry->ol_dev == ol_dev)
1412 static struct mlxsw_sp_ipip_entry *
1413 mlxsw_sp_ipip_entry_find_by_ul_dev(const struct mlxsw_sp *mlxsw_sp,
1414 const struct net_device *ul_dev,
1415 struct mlxsw_sp_ipip_entry *start)
1417 struct mlxsw_sp_ipip_entry *ipip_entry;
1419 ipip_entry = list_prepare_entry(start, &mlxsw_sp->router->ipip_list,
1421 list_for_each_entry_continue(ipip_entry, &mlxsw_sp->router->ipip_list,
1423 struct net_device *ol_dev = ipip_entry->ol_dev;
1424 struct net_device *ipip_ul_dev;
1427 ipip_ul_dev = __mlxsw_sp_ipip_netdev_ul_dev_get(ol_dev);
1430 if (ipip_ul_dev == ul_dev)
1437 bool mlxsw_sp_netdev_is_ipip_ul(struct mlxsw_sp *mlxsw_sp,
1438 const struct net_device *dev)
1442 mutex_lock(&mlxsw_sp->router->lock);
1443 is_ipip_ul = mlxsw_sp_ipip_entry_find_by_ul_dev(mlxsw_sp, dev, NULL);
1444 mutex_unlock(&mlxsw_sp->router->lock);
1449 static bool mlxsw_sp_netdevice_ipip_can_offload(struct mlxsw_sp *mlxsw_sp,
1450 const struct net_device *ol_dev,
1451 enum mlxsw_sp_ipip_type ipipt)
1453 const struct mlxsw_sp_ipip_ops *ops
1454 = mlxsw_sp->router->ipip_ops_arr[ipipt];
1456 return ops->can_offload(mlxsw_sp, ol_dev);
1459 static int mlxsw_sp_netdevice_ipip_ol_reg_event(struct mlxsw_sp *mlxsw_sp,
1460 struct net_device *ol_dev)
1462 enum mlxsw_sp_ipip_type ipipt = MLXSW_SP_IPIP_TYPE_MAX;
1463 struct mlxsw_sp_ipip_entry *ipip_entry;
1464 enum mlxsw_sp_l3proto ul_proto;
1465 union mlxsw_sp_l3addr saddr;
1468 mlxsw_sp_netdev_ipip_type(mlxsw_sp, ol_dev, &ipipt);
1469 if (mlxsw_sp_netdevice_ipip_can_offload(mlxsw_sp, ol_dev, ipipt)) {
1470 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(ol_dev);
1471 ul_proto = mlxsw_sp->router->ipip_ops_arr[ipipt]->ul_proto;
1472 saddr = mlxsw_sp_ipip_netdev_saddr(ul_proto, ol_dev);
1473 if (!mlxsw_sp_ipip_demote_tunnel_by_saddr(mlxsw_sp, ul_proto,
1476 ipip_entry = mlxsw_sp_ipip_entry_create(mlxsw_sp, ipipt,
1478 if (IS_ERR(ipip_entry))
1479 return PTR_ERR(ipip_entry);
1486 static void mlxsw_sp_netdevice_ipip_ol_unreg_event(struct mlxsw_sp *mlxsw_sp,
1487 struct net_device *ol_dev)
1489 struct mlxsw_sp_ipip_entry *ipip_entry;
1491 ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
1493 mlxsw_sp_ipip_entry_destroy(mlxsw_sp, ipip_entry);
1497 mlxsw_sp_ipip_entry_ol_up_event(struct mlxsw_sp *mlxsw_sp,
1498 struct mlxsw_sp_ipip_entry *ipip_entry)
1500 struct mlxsw_sp_fib_entry *decap_fib_entry;
1502 decap_fib_entry = mlxsw_sp_ipip_entry_find_decap(mlxsw_sp, ipip_entry);
1503 if (decap_fib_entry)
1504 mlxsw_sp_ipip_entry_promote_decap(mlxsw_sp, ipip_entry,
1509 mlxsw_sp_rif_ipip_lb_op(struct mlxsw_sp_rif_ipip_lb *lb_rif, u16 ul_vr_id,
1510 u16 ul_rif_id, bool enable)
1512 struct mlxsw_sp_rif_ipip_lb_config lb_cf = lb_rif->lb_config;
1513 struct mlxsw_sp_rif *rif = &lb_rif->common;
1514 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
1515 char ritr_pl[MLXSW_REG_RITR_LEN];
1518 switch (lb_cf.ul_protocol) {
1519 case MLXSW_SP_L3_PROTO_IPV4:
1520 saddr4 = be32_to_cpu(lb_cf.saddr.addr4);
1521 mlxsw_reg_ritr_pack(ritr_pl, enable, MLXSW_REG_RITR_LOOPBACK_IF,
1522 rif->rif_index, rif->vr_id, rif->dev->mtu);
1523 mlxsw_reg_ritr_loopback_ipip4_pack(ritr_pl, lb_cf.lb_ipipt,
1524 MLXSW_REG_RITR_LOOPBACK_IPIP_OPTIONS_GRE_KEY_PRESET,
1525 ul_vr_id, ul_rif_id, saddr4, lb_cf.okey);
1528 case MLXSW_SP_L3_PROTO_IPV6:
1529 return -EAFNOSUPPORT;
1532 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
1535 static int mlxsw_sp_netdevice_ipip_ol_update_mtu(struct mlxsw_sp *mlxsw_sp,
1536 struct net_device *ol_dev)
1538 struct mlxsw_sp_ipip_entry *ipip_entry;
1539 struct mlxsw_sp_rif_ipip_lb *lb_rif;
1542 ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
1544 lb_rif = ipip_entry->ol_lb;
1545 err = mlxsw_sp_rif_ipip_lb_op(lb_rif, lb_rif->ul_vr_id,
1546 lb_rif->ul_rif_id, true);
1549 lb_rif->common.mtu = ol_dev->mtu;
1556 static void mlxsw_sp_netdevice_ipip_ol_up_event(struct mlxsw_sp *mlxsw_sp,
1557 struct net_device *ol_dev)
1559 struct mlxsw_sp_ipip_entry *ipip_entry;
1561 ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
1563 mlxsw_sp_ipip_entry_ol_up_event(mlxsw_sp, ipip_entry);
1567 mlxsw_sp_ipip_entry_ol_down_event(struct mlxsw_sp *mlxsw_sp,
1568 struct mlxsw_sp_ipip_entry *ipip_entry)
1570 if (ipip_entry->decap_fib_entry)
1571 mlxsw_sp_ipip_entry_demote_decap(mlxsw_sp, ipip_entry);
1574 static void mlxsw_sp_netdevice_ipip_ol_down_event(struct mlxsw_sp *mlxsw_sp,
1575 struct net_device *ol_dev)
1577 struct mlxsw_sp_ipip_entry *ipip_entry;
1579 ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
1581 mlxsw_sp_ipip_entry_ol_down_event(mlxsw_sp, ipip_entry);
1584 static void mlxsw_sp_nexthop_rif_migrate(struct mlxsw_sp *mlxsw_sp,
1585 struct mlxsw_sp_rif *old_rif,
1586 struct mlxsw_sp_rif *new_rif);
1588 mlxsw_sp_ipip_entry_ol_lb_update(struct mlxsw_sp *mlxsw_sp,
1589 struct mlxsw_sp_ipip_entry *ipip_entry,
1591 struct netlink_ext_ack *extack)
1593 struct mlxsw_sp_rif_ipip_lb *old_lb_rif = ipip_entry->ol_lb;
1594 struct mlxsw_sp_rif_ipip_lb *new_lb_rif;
1596 new_lb_rif = mlxsw_sp_ipip_ol_ipip_lb_create(mlxsw_sp,
1600 if (IS_ERR(new_lb_rif))
1601 return PTR_ERR(new_lb_rif);
1602 ipip_entry->ol_lb = new_lb_rif;
1605 mlxsw_sp_nexthop_rif_migrate(mlxsw_sp, &old_lb_rif->common,
1606 &new_lb_rif->common);
1608 mlxsw_sp_rif_destroy(&old_lb_rif->common);
1613 static void mlxsw_sp_nexthop_rif_update(struct mlxsw_sp *mlxsw_sp,
1614 struct mlxsw_sp_rif *rif);
1617 * __mlxsw_sp_ipip_entry_update_tunnel - Update offload related to IPIP entry.
1618 * @mlxsw_sp: mlxsw_sp.
1619 * @ipip_entry: IPIP entry.
1620 * @recreate_loopback: Recreates the associated loopback RIF.
1621 * @keep_encap: Updates next hops that use the tunnel netdevice. This is only
1622 * relevant when recreate_loopback is true.
1623 * @update_nexthops: Updates next hops, keeping the current loopback RIF. This
1624 * is only relevant when recreate_loopback is false.
1627 * Return: Non-zero value on failure.
1629 int __mlxsw_sp_ipip_entry_update_tunnel(struct mlxsw_sp *mlxsw_sp,
1630 struct mlxsw_sp_ipip_entry *ipip_entry,
1631 bool recreate_loopback,
1633 bool update_nexthops,
1634 struct netlink_ext_ack *extack)
1638 /* RIFs can't be edited, so to update loopback, we need to destroy and
1639 * recreate it. That creates a window of opportunity where RALUE and
1640 * RATR registers end up referencing a RIF that's already gone. RATRs
1641 * are handled in mlxsw_sp_ipip_entry_ol_lb_update(), and to take care
1642 * of RALUE, demote the decap route back.
1644 if (ipip_entry->decap_fib_entry)
1645 mlxsw_sp_ipip_entry_demote_decap(mlxsw_sp, ipip_entry);
1647 if (recreate_loopback) {
1648 err = mlxsw_sp_ipip_entry_ol_lb_update(mlxsw_sp, ipip_entry,
1649 keep_encap, extack);
1652 } else if (update_nexthops) {
1653 mlxsw_sp_nexthop_rif_update(mlxsw_sp,
1654 &ipip_entry->ol_lb->common);
1657 if (ipip_entry->ol_dev->flags & IFF_UP)
1658 mlxsw_sp_ipip_entry_ol_up_event(mlxsw_sp, ipip_entry);
1663 static int mlxsw_sp_netdevice_ipip_ol_vrf_event(struct mlxsw_sp *mlxsw_sp,
1664 struct net_device *ol_dev,
1665 struct netlink_ext_ack *extack)
1667 struct mlxsw_sp_ipip_entry *ipip_entry =
1668 mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
1673 return __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
1674 true, false, false, extack);
1678 mlxsw_sp_netdevice_ipip_ul_vrf_event(struct mlxsw_sp *mlxsw_sp,
1679 struct mlxsw_sp_ipip_entry *ipip_entry,
1680 struct net_device *ul_dev,
1682 struct netlink_ext_ack *extack)
1684 u32 ul_tb_id = l3mdev_fib_table(ul_dev) ? : RT_TABLE_MAIN;
1685 enum mlxsw_sp_l3proto ul_proto;
1686 union mlxsw_sp_l3addr saddr;
1688 /* Moving underlay to a different VRF might cause local address
1689 * conflict, and the conflicting tunnels need to be demoted.
1691 ul_proto = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt]->ul_proto;
1692 saddr = mlxsw_sp_ipip_netdev_saddr(ul_proto, ipip_entry->ol_dev);
1693 if (mlxsw_sp_ipip_demote_tunnel_by_saddr(mlxsw_sp, ul_proto,
1696 *demote_this = true;
1700 return __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
1701 true, true, false, extack);
1705 mlxsw_sp_netdevice_ipip_ul_up_event(struct mlxsw_sp *mlxsw_sp,
1706 struct mlxsw_sp_ipip_entry *ipip_entry,
1707 struct net_device *ul_dev)
1709 return __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
1710 false, false, true, NULL);
1714 mlxsw_sp_netdevice_ipip_ul_down_event(struct mlxsw_sp *mlxsw_sp,
1715 struct mlxsw_sp_ipip_entry *ipip_entry,
1716 struct net_device *ul_dev)
1718 /* A down underlay device causes encapsulated packets to not be
1719 * forwarded, but decap still works. So refresh next hops without
1720 * touching anything else.
1722 return __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
1723 false, false, true, NULL);
1727 mlxsw_sp_netdevice_ipip_ol_change_event(struct mlxsw_sp *mlxsw_sp,
1728 struct net_device *ol_dev,
1729 struct netlink_ext_ack *extack)
1731 const struct mlxsw_sp_ipip_ops *ipip_ops;
1732 struct mlxsw_sp_ipip_entry *ipip_entry;
1735 ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
1737 /* A change might make a tunnel eligible for offloading, but
1738 * that is currently not implemented. What falls to slow path
1743 /* A change might make a tunnel not eligible for offloading. */
1744 if (!mlxsw_sp_netdevice_ipip_can_offload(mlxsw_sp, ol_dev,
1745 ipip_entry->ipipt)) {
1746 mlxsw_sp_ipip_entry_demote_tunnel(mlxsw_sp, ipip_entry);
1750 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
1751 err = ipip_ops->ol_netdev_change(mlxsw_sp, ipip_entry, extack);
1755 void mlxsw_sp_ipip_entry_demote_tunnel(struct mlxsw_sp *mlxsw_sp,
1756 struct mlxsw_sp_ipip_entry *ipip_entry)
1758 struct net_device *ol_dev = ipip_entry->ol_dev;
1760 if (ol_dev->flags & IFF_UP)
1761 mlxsw_sp_ipip_entry_ol_down_event(mlxsw_sp, ipip_entry);
1762 mlxsw_sp_ipip_entry_destroy(mlxsw_sp, ipip_entry);
1765 /* The configuration where several tunnels have the same local address in the
1766 * same underlay table needs special treatment in the HW. That is currently not
1767 * implemented in the driver. This function finds and demotes the first tunnel
1768 * with a given source address, except the one passed in in the argument
1772 mlxsw_sp_ipip_demote_tunnel_by_saddr(struct mlxsw_sp *mlxsw_sp,
1773 enum mlxsw_sp_l3proto ul_proto,
1774 union mlxsw_sp_l3addr saddr,
1776 const struct mlxsw_sp_ipip_entry *except)
1778 struct mlxsw_sp_ipip_entry *ipip_entry, *tmp;
1780 list_for_each_entry_safe(ipip_entry, tmp, &mlxsw_sp->router->ipip_list,
1782 if (ipip_entry != except &&
1783 mlxsw_sp_ipip_entry_saddr_matches(mlxsw_sp, ul_proto, saddr,
1784 ul_tb_id, ipip_entry)) {
1785 mlxsw_sp_ipip_entry_demote_tunnel(mlxsw_sp, ipip_entry);
1793 static void mlxsw_sp_ipip_demote_tunnel_by_ul_netdev(struct mlxsw_sp *mlxsw_sp,
1794 struct net_device *ul_dev)
1796 struct mlxsw_sp_ipip_entry *ipip_entry, *tmp;
1798 list_for_each_entry_safe(ipip_entry, tmp, &mlxsw_sp->router->ipip_list,
1800 struct net_device *ol_dev = ipip_entry->ol_dev;
1801 struct net_device *ipip_ul_dev;
1804 ipip_ul_dev = __mlxsw_sp_ipip_netdev_ul_dev_get(ol_dev);
1806 if (ipip_ul_dev == ul_dev)
1807 mlxsw_sp_ipip_entry_demote_tunnel(mlxsw_sp, ipip_entry);
1811 int mlxsw_sp_netdevice_ipip_ol_event(struct mlxsw_sp *mlxsw_sp,
1812 struct net_device *ol_dev,
1813 unsigned long event,
1814 struct netdev_notifier_info *info)
1816 struct netdev_notifier_changeupper_info *chup;
1817 struct netlink_ext_ack *extack;
1820 mutex_lock(&mlxsw_sp->router->lock);
1822 case NETDEV_REGISTER:
1823 err = mlxsw_sp_netdevice_ipip_ol_reg_event(mlxsw_sp, ol_dev);
1825 case NETDEV_UNREGISTER:
1826 mlxsw_sp_netdevice_ipip_ol_unreg_event(mlxsw_sp, ol_dev);
1829 mlxsw_sp_netdevice_ipip_ol_up_event(mlxsw_sp, ol_dev);
1832 mlxsw_sp_netdevice_ipip_ol_down_event(mlxsw_sp, ol_dev);
1834 case NETDEV_CHANGEUPPER:
1835 chup = container_of(info, typeof(*chup), info);
1836 extack = info->extack;
1837 if (netif_is_l3_master(chup->upper_dev))
1838 err = mlxsw_sp_netdevice_ipip_ol_vrf_event(mlxsw_sp,
1843 extack = info->extack;
1844 err = mlxsw_sp_netdevice_ipip_ol_change_event(mlxsw_sp,
1847 case NETDEV_CHANGEMTU:
1848 err = mlxsw_sp_netdevice_ipip_ol_update_mtu(mlxsw_sp, ol_dev);
1851 mutex_unlock(&mlxsw_sp->router->lock);
1856 __mlxsw_sp_netdevice_ipip_ul_event(struct mlxsw_sp *mlxsw_sp,
1857 struct mlxsw_sp_ipip_entry *ipip_entry,
1858 struct net_device *ul_dev,
1860 unsigned long event,
1861 struct netdev_notifier_info *info)
1863 struct netdev_notifier_changeupper_info *chup;
1864 struct netlink_ext_ack *extack;
1867 case NETDEV_CHANGEUPPER:
1868 chup = container_of(info, typeof(*chup), info);
1869 extack = info->extack;
1870 if (netif_is_l3_master(chup->upper_dev))
1871 return mlxsw_sp_netdevice_ipip_ul_vrf_event(mlxsw_sp,
1879 return mlxsw_sp_netdevice_ipip_ul_up_event(mlxsw_sp, ipip_entry,
1882 return mlxsw_sp_netdevice_ipip_ul_down_event(mlxsw_sp,
1890 mlxsw_sp_netdevice_ipip_ul_event(struct mlxsw_sp *mlxsw_sp,
1891 struct net_device *ul_dev,
1892 unsigned long event,
1893 struct netdev_notifier_info *info)
1895 struct mlxsw_sp_ipip_entry *ipip_entry = NULL;
1898 mutex_lock(&mlxsw_sp->router->lock);
1899 while ((ipip_entry = mlxsw_sp_ipip_entry_find_by_ul_dev(mlxsw_sp,
1902 struct mlxsw_sp_ipip_entry *prev;
1903 bool demote_this = false;
1905 err = __mlxsw_sp_netdevice_ipip_ul_event(mlxsw_sp, ipip_entry,
1906 ul_dev, &demote_this,
1909 mlxsw_sp_ipip_demote_tunnel_by_ul_netdev(mlxsw_sp,
1915 if (list_is_first(&ipip_entry->ipip_list_node,
1916 &mlxsw_sp->router->ipip_list))
1919 /* This can't be cached from previous iteration,
1920 * because that entry could be gone now.
1922 prev = list_prev_entry(ipip_entry,
1924 mlxsw_sp_ipip_entry_demote_tunnel(mlxsw_sp, ipip_entry);
1928 mutex_unlock(&mlxsw_sp->router->lock);
1933 int mlxsw_sp_router_nve_promote_decap(struct mlxsw_sp *mlxsw_sp, u32 ul_tb_id,
1934 enum mlxsw_sp_l3proto ul_proto,
1935 const union mlxsw_sp_l3addr *ul_sip,
1938 enum mlxsw_sp_fib_entry_type type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
1939 struct mlxsw_sp_router *router = mlxsw_sp->router;
1940 struct mlxsw_sp_fib_entry *fib_entry;
1943 mutex_lock(&mlxsw_sp->router->lock);
1945 if (WARN_ON_ONCE(router->nve_decap_config.valid)) {
1950 router->nve_decap_config.ul_tb_id = ul_tb_id;
1951 router->nve_decap_config.tunnel_index = tunnel_index;
1952 router->nve_decap_config.ul_proto = ul_proto;
1953 router->nve_decap_config.ul_sip = *ul_sip;
1954 router->nve_decap_config.valid = true;
1956 /* It is valid to create a tunnel with a local IP and only later
1957 * assign this IP address to a local interface
1959 fib_entry = mlxsw_sp_router_ip2me_fib_entry_find(mlxsw_sp, ul_tb_id,
1965 fib_entry->decap.tunnel_index = tunnel_index;
1966 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_NVE_DECAP;
1968 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1970 goto err_fib_entry_update;
1974 err_fib_entry_update:
1975 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
1976 mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1978 mutex_unlock(&mlxsw_sp->router->lock);
1982 void mlxsw_sp_router_nve_demote_decap(struct mlxsw_sp *mlxsw_sp, u32 ul_tb_id,
1983 enum mlxsw_sp_l3proto ul_proto,
1984 const union mlxsw_sp_l3addr *ul_sip)
1986 enum mlxsw_sp_fib_entry_type type = MLXSW_SP_FIB_ENTRY_TYPE_NVE_DECAP;
1987 struct mlxsw_sp_router *router = mlxsw_sp->router;
1988 struct mlxsw_sp_fib_entry *fib_entry;
1990 mutex_lock(&mlxsw_sp->router->lock);
1992 if (WARN_ON_ONCE(!router->nve_decap_config.valid))
1995 router->nve_decap_config.valid = false;
1997 fib_entry = mlxsw_sp_router_ip2me_fib_entry_find(mlxsw_sp, ul_tb_id,
2003 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
2004 mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
2006 mutex_unlock(&mlxsw_sp->router->lock);
2009 static bool mlxsw_sp_router_nve_is_decap(struct mlxsw_sp *mlxsw_sp,
2011 enum mlxsw_sp_l3proto ul_proto,
2012 const union mlxsw_sp_l3addr *ul_sip)
2014 struct mlxsw_sp_router *router = mlxsw_sp->router;
2016 return router->nve_decap_config.valid &&
2017 router->nve_decap_config.ul_tb_id == ul_tb_id &&
2018 router->nve_decap_config.ul_proto == ul_proto &&
2019 !memcmp(&router->nve_decap_config.ul_sip, ul_sip,
2023 struct mlxsw_sp_neigh_key {
2024 struct neighbour *n;
2027 struct mlxsw_sp_neigh_entry {
2028 struct list_head rif_list_node;
2029 struct rhash_head ht_node;
2030 struct mlxsw_sp_neigh_key key;
2033 unsigned char ha[ETH_ALEN];
2034 struct list_head nexthop_list; /* list of nexthops using
2037 struct list_head nexthop_neighs_list_node;
2038 unsigned int counter_index;
2042 static const struct rhashtable_params mlxsw_sp_neigh_ht_params = {
2043 .key_offset = offsetof(struct mlxsw_sp_neigh_entry, key),
2044 .head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node),
2045 .key_len = sizeof(struct mlxsw_sp_neigh_key),
2048 struct mlxsw_sp_neigh_entry *
2049 mlxsw_sp_rif_neigh_next(struct mlxsw_sp_rif *rif,
2050 struct mlxsw_sp_neigh_entry *neigh_entry)
2053 if (list_empty(&rif->neigh_list))
2056 return list_first_entry(&rif->neigh_list,
2057 typeof(*neigh_entry),
2060 if (list_is_last(&neigh_entry->rif_list_node, &rif->neigh_list))
2062 return list_next_entry(neigh_entry, rif_list_node);
2065 int mlxsw_sp_neigh_entry_type(struct mlxsw_sp_neigh_entry *neigh_entry)
2067 return neigh_entry->key.n->tbl->family;
2071 mlxsw_sp_neigh_entry_ha(struct mlxsw_sp_neigh_entry *neigh_entry)
2073 return neigh_entry->ha;
2076 u32 mlxsw_sp_neigh4_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry)
2078 struct neighbour *n;
2080 n = neigh_entry->key.n;
2081 return ntohl(*((__be32 *) n->primary_key));
2085 mlxsw_sp_neigh6_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry)
2087 struct neighbour *n;
2089 n = neigh_entry->key.n;
2090 return (struct in6_addr *) &n->primary_key;
2093 int mlxsw_sp_neigh_counter_get(struct mlxsw_sp *mlxsw_sp,
2094 struct mlxsw_sp_neigh_entry *neigh_entry,
2097 if (!neigh_entry->counter_valid)
2100 return mlxsw_sp_flow_counter_get(mlxsw_sp, neigh_entry->counter_index,
2104 static struct mlxsw_sp_neigh_entry *
2105 mlxsw_sp_neigh_entry_alloc(struct mlxsw_sp *mlxsw_sp, struct neighbour *n,
2108 struct mlxsw_sp_neigh_entry *neigh_entry;
2110 neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_KERNEL);
2114 neigh_entry->key.n = n;
2115 neigh_entry->rif = rif;
2116 INIT_LIST_HEAD(&neigh_entry->nexthop_list);
2121 static void mlxsw_sp_neigh_entry_free(struct mlxsw_sp_neigh_entry *neigh_entry)
2127 mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp,
2128 struct mlxsw_sp_neigh_entry *neigh_entry)
2130 return rhashtable_insert_fast(&mlxsw_sp->router->neigh_ht,
2131 &neigh_entry->ht_node,
2132 mlxsw_sp_neigh_ht_params);
2136 mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp,
2137 struct mlxsw_sp_neigh_entry *neigh_entry)
2139 rhashtable_remove_fast(&mlxsw_sp->router->neigh_ht,
2140 &neigh_entry->ht_node,
2141 mlxsw_sp_neigh_ht_params);
2145 mlxsw_sp_neigh_counter_should_alloc(struct mlxsw_sp *mlxsw_sp,
2146 struct mlxsw_sp_neigh_entry *neigh_entry)
2148 struct devlink *devlink;
2149 const char *table_name;
2151 switch (mlxsw_sp_neigh_entry_type(neigh_entry)) {
2153 table_name = MLXSW_SP_DPIPE_TABLE_NAME_HOST4;
2156 table_name = MLXSW_SP_DPIPE_TABLE_NAME_HOST6;
2163 devlink = priv_to_devlink(mlxsw_sp->core);
2164 return devlink_dpipe_table_counter_enabled(devlink, table_name);
2168 mlxsw_sp_neigh_counter_alloc(struct mlxsw_sp *mlxsw_sp,
2169 struct mlxsw_sp_neigh_entry *neigh_entry)
2171 if (!mlxsw_sp_neigh_counter_should_alloc(mlxsw_sp, neigh_entry))
2174 if (mlxsw_sp_flow_counter_alloc(mlxsw_sp, &neigh_entry->counter_index))
2177 neigh_entry->counter_valid = true;
2181 mlxsw_sp_neigh_counter_free(struct mlxsw_sp *mlxsw_sp,
2182 struct mlxsw_sp_neigh_entry *neigh_entry)
2184 if (!neigh_entry->counter_valid)
2186 mlxsw_sp_flow_counter_free(mlxsw_sp,
2187 neigh_entry->counter_index);
2188 neigh_entry->counter_valid = false;
2191 static struct mlxsw_sp_neigh_entry *
2192 mlxsw_sp_neigh_entry_create(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
2194 struct mlxsw_sp_neigh_entry *neigh_entry;
2195 struct mlxsw_sp_rif *rif;
2198 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, n->dev);
2200 return ERR_PTR(-EINVAL);
2202 neigh_entry = mlxsw_sp_neigh_entry_alloc(mlxsw_sp, n, rif->rif_index);
2204 return ERR_PTR(-ENOMEM);
2206 err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
2208 goto err_neigh_entry_insert;
2210 mlxsw_sp_neigh_counter_alloc(mlxsw_sp, neigh_entry);
2211 list_add(&neigh_entry->rif_list_node, &rif->neigh_list);
2215 err_neigh_entry_insert:
2216 mlxsw_sp_neigh_entry_free(neigh_entry);
2217 return ERR_PTR(err);
2221 mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp *mlxsw_sp,
2222 struct mlxsw_sp_neigh_entry *neigh_entry)
2224 list_del(&neigh_entry->rif_list_node);
2225 mlxsw_sp_neigh_counter_free(mlxsw_sp, neigh_entry);
2226 mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
2227 mlxsw_sp_neigh_entry_free(neigh_entry);
2230 static struct mlxsw_sp_neigh_entry *
2231 mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
2233 struct mlxsw_sp_neigh_key key;
2236 return rhashtable_lookup_fast(&mlxsw_sp->router->neigh_ht,
2237 &key, mlxsw_sp_neigh_ht_params);
2241 mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp)
2243 unsigned long interval;
2245 #if IS_ENABLED(CONFIG_IPV6)
2246 interval = min_t(unsigned long,
2247 NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME),
2248 NEIGH_VAR(&nd_tbl.parms, DELAY_PROBE_TIME));
2250 interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME);
2252 mlxsw_sp->router->neighs_update.interval = jiffies_to_msecs(interval);
2255 static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
2259 struct net_device *dev;
2260 struct neighbour *n;
2265 mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip);
2267 if (!mlxsw_sp->router->rifs[rif]) {
2268 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
2273 dev = mlxsw_sp->router->rifs[rif]->dev;
2274 n = neigh_lookup(&arp_tbl, &dipn, dev);
2278 netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip);
2279 neigh_event_send(n, NULL);
2283 #if IS_ENABLED(CONFIG_IPV6)
2284 static void mlxsw_sp_router_neigh_ent_ipv6_process(struct mlxsw_sp *mlxsw_sp,
2288 struct net_device *dev;
2289 struct neighbour *n;
2290 struct in6_addr dip;
2293 mlxsw_reg_rauhtd_ent_ipv6_unpack(rauhtd_pl, rec_index, &rif,
2296 if (!mlxsw_sp->router->rifs[rif]) {
2297 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
2301 dev = mlxsw_sp->router->rifs[rif]->dev;
2302 n = neigh_lookup(&nd_tbl, &dip, dev);
2306 netdev_dbg(dev, "Updating neighbour with IP=%pI6c\n", &dip);
2307 neigh_event_send(n, NULL);
2311 static void mlxsw_sp_router_neigh_ent_ipv6_process(struct mlxsw_sp *mlxsw_sp,
2318 static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp,
2325 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
2327 /* Hardware starts counting at 0, so add 1. */
2330 /* Each record consists of several neighbour entries. */
2331 for (i = 0; i < num_entries; i++) {
2334 ent_index = rec_index * MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC + i;
2335 mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp, rauhtd_pl,
2341 static void mlxsw_sp_router_neigh_rec_ipv6_process(struct mlxsw_sp *mlxsw_sp,
2345 /* One record contains one entry. */
2346 mlxsw_sp_router_neigh_ent_ipv6_process(mlxsw_sp, rauhtd_pl,
2350 static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp,
2351 char *rauhtd_pl, int rec_index)
2353 switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, rec_index)) {
2354 case MLXSW_REG_RAUHTD_TYPE_IPV4:
2355 mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp, rauhtd_pl,
2358 case MLXSW_REG_RAUHTD_TYPE_IPV6:
2359 mlxsw_sp_router_neigh_rec_ipv6_process(mlxsw_sp, rauhtd_pl,
2365 static bool mlxsw_sp_router_rauhtd_is_full(char *rauhtd_pl)
2367 u8 num_rec, last_rec_index, num_entries;
2369 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
2370 last_rec_index = num_rec - 1;
2372 if (num_rec < MLXSW_REG_RAUHTD_REC_MAX_NUM)
2374 if (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, last_rec_index) ==
2375 MLXSW_REG_RAUHTD_TYPE_IPV6)
2378 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
2380 if (++num_entries == MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC)
2386 __mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp,
2388 enum mlxsw_reg_rauhtd_type type)
2393 /* Ensure the RIF we read from the device does not change mid-dump. */
2394 mutex_lock(&mlxsw_sp->router->lock);
2396 mlxsw_reg_rauhtd_pack(rauhtd_pl, type);
2397 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd),
2400 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour table\n");
2403 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
2404 for (i = 0; i < num_rec; i++)
2405 mlxsw_sp_router_neigh_rec_process(mlxsw_sp, rauhtd_pl,
2407 } while (mlxsw_sp_router_rauhtd_is_full(rauhtd_pl));
2408 mutex_unlock(&mlxsw_sp->router->lock);
2413 static int mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp)
2415 enum mlxsw_reg_rauhtd_type type;
2419 rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL);
2423 type = MLXSW_REG_RAUHTD_TYPE_IPV4;
2424 err = __mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp, rauhtd_pl, type);
2428 type = MLXSW_REG_RAUHTD_TYPE_IPV6;
2429 err = __mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp, rauhtd_pl, type);
2435 static void mlxsw_sp_router_neighs_update_nh(struct mlxsw_sp *mlxsw_sp)
2437 struct mlxsw_sp_neigh_entry *neigh_entry;
2439 mutex_lock(&mlxsw_sp->router->lock);
2440 list_for_each_entry(neigh_entry, &mlxsw_sp->router->nexthop_neighs_list,
2441 nexthop_neighs_list_node)
2442 /* If this neigh have nexthops, make the kernel think this neigh
2443 * is active regardless of the traffic.
2445 neigh_event_send(neigh_entry->key.n, NULL);
2446 mutex_unlock(&mlxsw_sp->router->lock);
2450 mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp)
2452 unsigned long interval = mlxsw_sp->router->neighs_update.interval;
2454 mlxsw_core_schedule_dw(&mlxsw_sp->router->neighs_update.dw,
2455 msecs_to_jiffies(interval));
2458 static void mlxsw_sp_router_neighs_update_work(struct work_struct *work)
2460 struct mlxsw_sp_router *router;
2463 router = container_of(work, struct mlxsw_sp_router,
2464 neighs_update.dw.work);
2465 err = mlxsw_sp_router_neighs_update_rauhtd(router->mlxsw_sp);
2467 dev_err(router->mlxsw_sp->bus_info->dev, "Could not update kernel for neigh activity");
2469 mlxsw_sp_router_neighs_update_nh(router->mlxsw_sp);
2471 mlxsw_sp_router_neighs_update_work_schedule(router->mlxsw_sp);
2474 static void mlxsw_sp_router_probe_unresolved_nexthops(struct work_struct *work)
2476 struct mlxsw_sp_neigh_entry *neigh_entry;
2477 struct mlxsw_sp_router *router;
2479 router = container_of(work, struct mlxsw_sp_router,
2480 nexthop_probe_dw.work);
2481 /* Iterate over nexthop neighbours, find those who are unresolved and
2482 * send arp on them. This solves the chicken-egg problem when
2483 * the nexthop wouldn't get offloaded until the neighbor is resolved
2484 * but it wouldn't get resolved ever in case traffic is flowing in HW
2485 * using different nexthop.
2487 mutex_lock(&router->lock);
2488 list_for_each_entry(neigh_entry, &router->nexthop_neighs_list,
2489 nexthop_neighs_list_node)
2490 if (!neigh_entry->connected)
2491 neigh_event_send(neigh_entry->key.n, NULL);
2492 mutex_unlock(&router->lock);
2494 mlxsw_core_schedule_dw(&router->nexthop_probe_dw,
2495 MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL);
2499 mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
2500 struct mlxsw_sp_neigh_entry *neigh_entry,
2501 bool removing, bool dead);
2503 static enum mlxsw_reg_rauht_op mlxsw_sp_rauht_op(bool adding)
2505 return adding ? MLXSW_REG_RAUHT_OP_WRITE_ADD :
2506 MLXSW_REG_RAUHT_OP_WRITE_DELETE;
2510 mlxsw_sp_router_neigh_entry_op4(struct mlxsw_sp *mlxsw_sp,
2511 struct mlxsw_sp_neigh_entry *neigh_entry,
2512 enum mlxsw_reg_rauht_op op)
2514 struct neighbour *n = neigh_entry->key.n;
2515 u32 dip = ntohl(*((__be32 *) n->primary_key));
2516 char rauht_pl[MLXSW_REG_RAUHT_LEN];
2518 mlxsw_reg_rauht_pack4(rauht_pl, op, neigh_entry->rif, neigh_entry->ha,
2520 if (neigh_entry->counter_valid)
2521 mlxsw_reg_rauht_pack_counter(rauht_pl,
2522 neigh_entry->counter_index);
2523 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
2527 mlxsw_sp_router_neigh_entry_op6(struct mlxsw_sp *mlxsw_sp,
2528 struct mlxsw_sp_neigh_entry *neigh_entry,
2529 enum mlxsw_reg_rauht_op op)
2531 struct neighbour *n = neigh_entry->key.n;
2532 char rauht_pl[MLXSW_REG_RAUHT_LEN];
2533 const char *dip = n->primary_key;
2535 mlxsw_reg_rauht_pack6(rauht_pl, op, neigh_entry->rif, neigh_entry->ha,
2537 if (neigh_entry->counter_valid)
2538 mlxsw_reg_rauht_pack_counter(rauht_pl,
2539 neigh_entry->counter_index);
2540 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
2543 bool mlxsw_sp_neigh_ipv6_ignore(struct mlxsw_sp_neigh_entry *neigh_entry)
2545 struct neighbour *n = neigh_entry->key.n;
2547 /* Packets with a link-local destination address are trapped
2548 * after LPM lookup and never reach the neighbour table, so
2549 * there is no need to program such neighbours to the device.
2551 if (ipv6_addr_type((struct in6_addr *) &n->primary_key) &
2552 IPV6_ADDR_LINKLOCAL)
2558 mlxsw_sp_neigh_entry_update(struct mlxsw_sp *mlxsw_sp,
2559 struct mlxsw_sp_neigh_entry *neigh_entry,
2562 enum mlxsw_reg_rauht_op op = mlxsw_sp_rauht_op(adding);
2565 if (!adding && !neigh_entry->connected)
2567 neigh_entry->connected = adding;
2568 if (neigh_entry->key.n->tbl->family == AF_INET) {
2569 err = mlxsw_sp_router_neigh_entry_op4(mlxsw_sp, neigh_entry,
2573 } else if (neigh_entry->key.n->tbl->family == AF_INET6) {
2574 if (mlxsw_sp_neigh_ipv6_ignore(neigh_entry))
2576 err = mlxsw_sp_router_neigh_entry_op6(mlxsw_sp, neigh_entry,
2586 neigh_entry->key.n->flags |= NTF_OFFLOADED;
2588 neigh_entry->key.n->flags &= ~NTF_OFFLOADED;
2592 mlxsw_sp_neigh_entry_counter_update(struct mlxsw_sp *mlxsw_sp,
2593 struct mlxsw_sp_neigh_entry *neigh_entry,
2597 mlxsw_sp_neigh_counter_alloc(mlxsw_sp, neigh_entry);
2599 mlxsw_sp_neigh_counter_free(mlxsw_sp, neigh_entry);
2600 mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, true);
2603 struct mlxsw_sp_netevent_work {
2604 struct work_struct work;
2605 struct mlxsw_sp *mlxsw_sp;
2606 struct neighbour *n;
2609 static void mlxsw_sp_router_neigh_event_work(struct work_struct *work)
2611 struct mlxsw_sp_netevent_work *net_work =
2612 container_of(work, struct mlxsw_sp_netevent_work, work);
2613 struct mlxsw_sp *mlxsw_sp = net_work->mlxsw_sp;
2614 struct mlxsw_sp_neigh_entry *neigh_entry;
2615 struct neighbour *n = net_work->n;
2616 unsigned char ha[ETH_ALEN];
2617 bool entry_connected;
2620 /* If these parameters are changed after we release the lock,
2621 * then we are guaranteed to receive another event letting us
2624 read_lock_bh(&n->lock);
2625 memcpy(ha, n->ha, ETH_ALEN);
2626 nud_state = n->nud_state;
2628 read_unlock_bh(&n->lock);
2630 mutex_lock(&mlxsw_sp->router->lock);
2631 mlxsw_sp_span_respin(mlxsw_sp);
2633 entry_connected = nud_state & NUD_VALID && !dead;
2634 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
2635 if (!entry_connected && !neigh_entry)
2638 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
2639 if (IS_ERR(neigh_entry))
2643 memcpy(neigh_entry->ha, ha, ETH_ALEN);
2644 mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, entry_connected);
2645 mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, !entry_connected,
2648 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
2649 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
2652 mutex_unlock(&mlxsw_sp->router->lock);
2657 static int mlxsw_sp_mp_hash_init(struct mlxsw_sp *mlxsw_sp);
2659 static void mlxsw_sp_router_mp_hash_event_work(struct work_struct *work)
2661 struct mlxsw_sp_netevent_work *net_work =
2662 container_of(work, struct mlxsw_sp_netevent_work, work);
2663 struct mlxsw_sp *mlxsw_sp = net_work->mlxsw_sp;
2665 mlxsw_sp_mp_hash_init(mlxsw_sp);
2669 static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp);
2671 static void mlxsw_sp_router_update_priority_work(struct work_struct *work)
2673 struct mlxsw_sp_netevent_work *net_work =
2674 container_of(work, struct mlxsw_sp_netevent_work, work);
2675 struct mlxsw_sp *mlxsw_sp = net_work->mlxsw_sp;
2677 __mlxsw_sp_router_init(mlxsw_sp);
2681 static int mlxsw_sp_router_schedule_work(struct net *net,
2682 struct notifier_block *nb,
2683 void (*cb)(struct work_struct *))
2685 struct mlxsw_sp_netevent_work *net_work;
2686 struct mlxsw_sp_router *router;
2688 router = container_of(nb, struct mlxsw_sp_router, netevent_nb);
2689 if (!net_eq(net, mlxsw_sp_net(router->mlxsw_sp)))
2692 net_work = kzalloc(sizeof(*net_work), GFP_ATOMIC);
2696 INIT_WORK(&net_work->work, cb);
2697 net_work->mlxsw_sp = router->mlxsw_sp;
2698 mlxsw_core_schedule_work(&net_work->work);
2702 static int mlxsw_sp_router_netevent_event(struct notifier_block *nb,
2703 unsigned long event, void *ptr)
2705 struct mlxsw_sp_netevent_work *net_work;
2706 struct mlxsw_sp_port *mlxsw_sp_port;
2707 struct mlxsw_sp *mlxsw_sp;
2708 unsigned long interval;
2709 struct neigh_parms *p;
2710 struct neighbour *n;
2713 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
2716 /* We don't care about changes in the default table. */
2717 if (!p->dev || (p->tbl->family != AF_INET &&
2718 p->tbl->family != AF_INET6))
2721 /* We are in atomic context and can't take RTNL mutex,
2722 * so use RCU variant to walk the device chain.
2724 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev);
2728 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2729 interval = jiffies_to_msecs(NEIGH_VAR(p, DELAY_PROBE_TIME));
2730 mlxsw_sp->router->neighs_update.interval = interval;
2732 mlxsw_sp_port_dev_put(mlxsw_sp_port);
2734 case NETEVENT_NEIGH_UPDATE:
2737 if (n->tbl->family != AF_INET && n->tbl->family != AF_INET6)
2740 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(n->dev);
2744 net_work = kzalloc(sizeof(*net_work), GFP_ATOMIC);
2746 mlxsw_sp_port_dev_put(mlxsw_sp_port);
2750 INIT_WORK(&net_work->work, mlxsw_sp_router_neigh_event_work);
2751 net_work->mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2754 /* Take a reference to ensure the neighbour won't be
2755 * destructed until we drop the reference in delayed
2759 mlxsw_core_schedule_work(&net_work->work);
2760 mlxsw_sp_port_dev_put(mlxsw_sp_port);
2762 case NETEVENT_IPV4_MPATH_HASH_UPDATE:
2763 case NETEVENT_IPV6_MPATH_HASH_UPDATE:
2764 return mlxsw_sp_router_schedule_work(ptr, nb,
2765 mlxsw_sp_router_mp_hash_event_work);
2767 case NETEVENT_IPV4_FWD_UPDATE_PRIORITY_UPDATE:
2768 return mlxsw_sp_router_schedule_work(ptr, nb,
2769 mlxsw_sp_router_update_priority_work);
2775 static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp)
2779 err = rhashtable_init(&mlxsw_sp->router->neigh_ht,
2780 &mlxsw_sp_neigh_ht_params);
2784 /* Initialize the polling interval according to the default
2787 mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp);
2789 /* Create the delayed works for the activity_update */
2790 INIT_DELAYED_WORK(&mlxsw_sp->router->neighs_update.dw,
2791 mlxsw_sp_router_neighs_update_work);
2792 INIT_DELAYED_WORK(&mlxsw_sp->router->nexthop_probe_dw,
2793 mlxsw_sp_router_probe_unresolved_nexthops);
2794 mlxsw_core_schedule_dw(&mlxsw_sp->router->neighs_update.dw, 0);
2795 mlxsw_core_schedule_dw(&mlxsw_sp->router->nexthop_probe_dw, 0);
2799 static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp)
2801 cancel_delayed_work_sync(&mlxsw_sp->router->neighs_update.dw);
2802 cancel_delayed_work_sync(&mlxsw_sp->router->nexthop_probe_dw);
2803 rhashtable_destroy(&mlxsw_sp->router->neigh_ht);
2806 static void mlxsw_sp_neigh_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
2807 struct mlxsw_sp_rif *rif)
2809 struct mlxsw_sp_neigh_entry *neigh_entry, *tmp;
2811 list_for_each_entry_safe(neigh_entry, tmp, &rif->neigh_list,
2813 mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, false);
2814 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
2818 enum mlxsw_sp_nexthop_type {
2819 MLXSW_SP_NEXTHOP_TYPE_ETH,
2820 MLXSW_SP_NEXTHOP_TYPE_IPIP,
2823 struct mlxsw_sp_nexthop_key {
2824 struct fib_nh *fib_nh;
2827 struct mlxsw_sp_nexthop {
2828 struct list_head neigh_list_node; /* member of neigh entry list */
2829 struct list_head rif_list_node;
2830 struct list_head router_list_node;
2831 struct mlxsw_sp_nexthop_group_info *nhgi; /* pointer back to the group
2832 * this nexthop belongs to
2834 struct rhash_head ht_node;
2835 struct neigh_table *neigh_tbl;
2836 struct mlxsw_sp_nexthop_key key;
2837 unsigned char gw_addr[sizeof(struct in6_addr)];
2841 int num_adj_entries;
2842 struct mlxsw_sp_rif *rif;
2843 u8 should_offload:1, /* set indicates this neigh is connected and
2844 * should be put to KVD linear area of this group.
2846 offloaded:1, /* set in case the neigh is actually put into
2847 * KVD linear area of this group.
2849 update:1; /* set indicates that MAC of this neigh should be
2852 enum mlxsw_sp_nexthop_type type;
2854 struct mlxsw_sp_neigh_entry *neigh_entry;
2855 struct mlxsw_sp_ipip_entry *ipip_entry;
2857 unsigned int counter_index;
2861 enum mlxsw_sp_nexthop_group_type {
2862 MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV4,
2863 MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV6,
2866 struct mlxsw_sp_nexthop_group_info {
2867 struct mlxsw_sp_nexthop_group *nh_grp;
2871 int sum_norm_weight;
2872 u8 adj_index_valid:1,
2873 gateway:1; /* routes using the group use a gateway */
2874 struct mlxsw_sp_nexthop nexthops[0];
2875 #define nh_rif nexthops[0].rif
2878 struct mlxsw_sp_nexthop_group {
2879 struct rhash_head ht_node;
2880 struct list_head fib_list; /* list of fib entries that use this group */
2883 struct fib_info *fi;
2886 struct mlxsw_sp_nexthop_group_info *nhgi;
2887 enum mlxsw_sp_nexthop_group_type type;
2890 void mlxsw_sp_nexthop_counter_alloc(struct mlxsw_sp *mlxsw_sp,
2891 struct mlxsw_sp_nexthop *nh)
2893 struct devlink *devlink;
2895 devlink = priv_to_devlink(mlxsw_sp->core);
2896 if (!devlink_dpipe_table_counter_enabled(devlink,
2897 MLXSW_SP_DPIPE_TABLE_NAME_ADJ))
2900 if (mlxsw_sp_flow_counter_alloc(mlxsw_sp, &nh->counter_index))
2903 nh->counter_valid = true;
2906 void mlxsw_sp_nexthop_counter_free(struct mlxsw_sp *mlxsw_sp,
2907 struct mlxsw_sp_nexthop *nh)
2909 if (!nh->counter_valid)
2911 mlxsw_sp_flow_counter_free(mlxsw_sp, nh->counter_index);
2912 nh->counter_valid = false;
2915 int mlxsw_sp_nexthop_counter_get(struct mlxsw_sp *mlxsw_sp,
2916 struct mlxsw_sp_nexthop *nh, u64 *p_counter)
2918 if (!nh->counter_valid)
2921 return mlxsw_sp_flow_counter_get(mlxsw_sp, nh->counter_index,
2925 struct mlxsw_sp_nexthop *mlxsw_sp_nexthop_next(struct mlxsw_sp_router *router,
2926 struct mlxsw_sp_nexthop *nh)
2929 if (list_empty(&router->nexthop_list))
2932 return list_first_entry(&router->nexthop_list,
2933 typeof(*nh), router_list_node);
2935 if (list_is_last(&nh->router_list_node, &router->nexthop_list))
2937 return list_next_entry(nh, router_list_node);
2940 bool mlxsw_sp_nexthop_offload(struct mlxsw_sp_nexthop *nh)
2942 return nh->offloaded;
2945 unsigned char *mlxsw_sp_nexthop_ha(struct mlxsw_sp_nexthop *nh)
2949 return nh->neigh_entry->ha;
2952 int mlxsw_sp_nexthop_indexes(struct mlxsw_sp_nexthop *nh, u32 *p_adj_index,
2953 u32 *p_adj_size, u32 *p_adj_hash_index)
2955 struct mlxsw_sp_nexthop_group_info *nhgi = nh->nhgi;
2956 u32 adj_hash_index = 0;
2959 if (!nh->offloaded || !nhgi->adj_index_valid)
2962 *p_adj_index = nhgi->adj_index;
2963 *p_adj_size = nhgi->ecmp_size;
2965 for (i = 0; i < nhgi->count; i++) {
2966 struct mlxsw_sp_nexthop *nh_iter = &nhgi->nexthops[i];
2970 if (nh_iter->offloaded)
2971 adj_hash_index += nh_iter->num_adj_entries;
2974 *p_adj_hash_index = adj_hash_index;
2978 struct mlxsw_sp_rif *mlxsw_sp_nexthop_rif(struct mlxsw_sp_nexthop *nh)
2983 bool mlxsw_sp_nexthop_group_has_ipip(struct mlxsw_sp_nexthop *nh)
2985 struct mlxsw_sp_nexthop_group_info *nhgi = nh->nhgi;
2988 for (i = 0; i < nhgi->count; i++) {
2989 struct mlxsw_sp_nexthop *nh_iter = &nhgi->nexthops[i];
2991 if (nh_iter->type == MLXSW_SP_NEXTHOP_TYPE_IPIP)
2997 struct mlxsw_sp_nexthop_group_cmp_arg {
2998 enum mlxsw_sp_nexthop_group_type type;
3000 struct fib_info *fi;
3001 struct mlxsw_sp_fib6_entry *fib6_entry;
3006 mlxsw_sp_nexthop6_group_has_nexthop(const struct mlxsw_sp_nexthop_group *nh_grp,
3007 const struct in6_addr *gw, int ifindex,
3012 for (i = 0; i < nh_grp->nhgi->count; i++) {
3013 const struct mlxsw_sp_nexthop *nh;
3015 nh = &nh_grp->nhgi->nexthops[i];
3016 if (nh->ifindex == ifindex && nh->nh_weight == weight &&
3017 ipv6_addr_equal(gw, (struct in6_addr *) nh->gw_addr))
3025 mlxsw_sp_nexthop6_group_cmp(const struct mlxsw_sp_nexthop_group *nh_grp,
3026 const struct mlxsw_sp_fib6_entry *fib6_entry)
3028 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
3030 if (nh_grp->nhgi->count != fib6_entry->nrt6)
3033 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
3034 struct fib6_nh *fib6_nh = mlxsw_sp_rt6->rt->fib6_nh;
3035 struct in6_addr *gw;
3036 int ifindex, weight;
3038 ifindex = fib6_nh->fib_nh_dev->ifindex;
3039 weight = fib6_nh->fib_nh_weight;
3040 gw = &fib6_nh->fib_nh_gw6;
3041 if (!mlxsw_sp_nexthop6_group_has_nexthop(nh_grp, gw, ifindex,
3050 mlxsw_sp_nexthop_group_cmp(struct rhashtable_compare_arg *arg, const void *ptr)
3052 const struct mlxsw_sp_nexthop_group_cmp_arg *cmp_arg = arg->key;
3053 const struct mlxsw_sp_nexthop_group *nh_grp = ptr;
3055 if (nh_grp->type != cmp_arg->type)
3058 switch (cmp_arg->type) {
3059 case MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV4:
3060 return cmp_arg->fi != nh_grp->ipv4.fi;
3061 case MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV6:
3062 return !mlxsw_sp_nexthop6_group_cmp(nh_grp,
3063 cmp_arg->fib6_entry);
3070 static u32 mlxsw_sp_nexthop_group_hash_obj(const void *data, u32 len, u32 seed)
3072 const struct mlxsw_sp_nexthop_group *nh_grp = data;
3073 const struct mlxsw_sp_nexthop *nh;
3074 struct fib_info *fi;
3078 switch (nh_grp->type) {
3079 case MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV4:
3080 fi = nh_grp->ipv4.fi;
3081 return jhash(&fi, sizeof(fi), seed);
3082 case MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV6:
3083 val = nh_grp->nhgi->count;
3084 for (i = 0; i < nh_grp->nhgi->count; i++) {
3085 nh = &nh_grp->nhgi->nexthops[i];
3086 val ^= jhash(&nh->ifindex, sizeof(nh->ifindex), seed);
3087 val ^= jhash(&nh->gw_addr, sizeof(nh->gw_addr), seed);
3089 return jhash(&val, sizeof(val), seed);
3097 mlxsw_sp_nexthop6_group_hash(struct mlxsw_sp_fib6_entry *fib6_entry, u32 seed)
3099 unsigned int val = fib6_entry->nrt6;
3100 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
3102 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
3103 struct fib6_nh *fib6_nh = mlxsw_sp_rt6->rt->fib6_nh;
3104 struct net_device *dev = fib6_nh->fib_nh_dev;
3105 struct in6_addr *gw = &fib6_nh->fib_nh_gw6;
3107 val ^= jhash(&dev->ifindex, sizeof(dev->ifindex), seed);
3108 val ^= jhash(gw, sizeof(*gw), seed);
3111 return jhash(&val, sizeof(val), seed);
3115 mlxsw_sp_nexthop_group_hash(const void *data, u32 len, u32 seed)
3117 const struct mlxsw_sp_nexthop_group_cmp_arg *cmp_arg = data;
3119 switch (cmp_arg->type) {
3120 case MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV4:
3121 return jhash(&cmp_arg->fi, sizeof(cmp_arg->fi), seed);
3122 case MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV6:
3123 return mlxsw_sp_nexthop6_group_hash(cmp_arg->fib6_entry, seed);
3130 static const struct rhashtable_params mlxsw_sp_nexthop_group_ht_params = {
3131 .head_offset = offsetof(struct mlxsw_sp_nexthop_group, ht_node),
3132 .hashfn = mlxsw_sp_nexthop_group_hash,
3133 .obj_hashfn = mlxsw_sp_nexthop_group_hash_obj,
3134 .obj_cmpfn = mlxsw_sp_nexthop_group_cmp,
3137 static int mlxsw_sp_nexthop_group_insert(struct mlxsw_sp *mlxsw_sp,
3138 struct mlxsw_sp_nexthop_group *nh_grp)
3140 if (nh_grp->type == MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV6 &&
3141 !nh_grp->nhgi->gateway)
3144 return rhashtable_insert_fast(&mlxsw_sp->router->nexthop_group_ht,
3146 mlxsw_sp_nexthop_group_ht_params);
3149 static void mlxsw_sp_nexthop_group_remove(struct mlxsw_sp *mlxsw_sp,
3150 struct mlxsw_sp_nexthop_group *nh_grp)
3152 if (nh_grp->type == MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV6 &&
3153 !nh_grp->nhgi->gateway)
3156 rhashtable_remove_fast(&mlxsw_sp->router->nexthop_group_ht,
3158 mlxsw_sp_nexthop_group_ht_params);
3161 static struct mlxsw_sp_nexthop_group *
3162 mlxsw_sp_nexthop4_group_lookup(struct mlxsw_sp *mlxsw_sp,
3163 struct fib_info *fi)
3165 struct mlxsw_sp_nexthop_group_cmp_arg cmp_arg;
3167 cmp_arg.type = MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV4;
3169 return rhashtable_lookup_fast(&mlxsw_sp->router->nexthop_group_ht,
3171 mlxsw_sp_nexthop_group_ht_params);
3174 static struct mlxsw_sp_nexthop_group *
3175 mlxsw_sp_nexthop6_group_lookup(struct mlxsw_sp *mlxsw_sp,
3176 struct mlxsw_sp_fib6_entry *fib6_entry)
3178 struct mlxsw_sp_nexthop_group_cmp_arg cmp_arg;
3180 cmp_arg.type = MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV6;
3181 cmp_arg.fib6_entry = fib6_entry;
3182 return rhashtable_lookup_fast(&mlxsw_sp->router->nexthop_group_ht,
3184 mlxsw_sp_nexthop_group_ht_params);
3187 static const struct rhashtable_params mlxsw_sp_nexthop_ht_params = {
3188 .key_offset = offsetof(struct mlxsw_sp_nexthop, key),
3189 .head_offset = offsetof(struct mlxsw_sp_nexthop, ht_node),
3190 .key_len = sizeof(struct mlxsw_sp_nexthop_key),
3193 static int mlxsw_sp_nexthop_insert(struct mlxsw_sp *mlxsw_sp,
3194 struct mlxsw_sp_nexthop *nh)
3196 return rhashtable_insert_fast(&mlxsw_sp->router->nexthop_ht,
3197 &nh->ht_node, mlxsw_sp_nexthop_ht_params);
3200 static void mlxsw_sp_nexthop_remove(struct mlxsw_sp *mlxsw_sp,
3201 struct mlxsw_sp_nexthop *nh)
3203 rhashtable_remove_fast(&mlxsw_sp->router->nexthop_ht, &nh->ht_node,
3204 mlxsw_sp_nexthop_ht_params);
3207 static struct mlxsw_sp_nexthop *
3208 mlxsw_sp_nexthop_lookup(struct mlxsw_sp *mlxsw_sp,
3209 struct mlxsw_sp_nexthop_key key)
3211 return rhashtable_lookup_fast(&mlxsw_sp->router->nexthop_ht, &key,
3212 mlxsw_sp_nexthop_ht_params);
3215 static int mlxsw_sp_adj_index_mass_update_vr(struct mlxsw_sp *mlxsw_sp,
3216 const struct mlxsw_sp_fib *fib,
3217 u32 adj_index, u16 ecmp_size,
3221 char raleu_pl[MLXSW_REG_RALEU_LEN];
3223 mlxsw_reg_raleu_pack(raleu_pl,
3224 (enum mlxsw_reg_ralxx_protocol) fib->proto,
3225 fib->vr->id, adj_index, ecmp_size, new_adj_index,
3227 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raleu), raleu_pl);
3230 static int mlxsw_sp_adj_index_mass_update(struct mlxsw_sp *mlxsw_sp,
3231 struct mlxsw_sp_nexthop_group *nh_grp,
3232 u32 old_adj_index, u16 old_ecmp_size)
3234 struct mlxsw_sp_fib_entry *fib_entry;
3235 struct mlxsw_sp_fib *fib = NULL;
3238 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
3239 struct mlxsw_sp_nexthop_group_info *nhgi = nh_grp->nhgi;
3241 if (fib == fib_entry->fib_node->fib)
3243 fib = fib_entry->fib_node->fib;
3244 err = mlxsw_sp_adj_index_mass_update_vr(mlxsw_sp, fib,
3255 static int __mlxsw_sp_nexthop_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
3256 struct mlxsw_sp_nexthop *nh)
3258 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
3259 char ratr_pl[MLXSW_REG_RATR_LEN];
3261 mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY,
3262 true, MLXSW_REG_RATR_TYPE_ETHERNET,
3263 adj_index, neigh_entry->rif);
3264 mlxsw_reg_ratr_eth_entry_pack(ratr_pl, neigh_entry->ha);
3265 if (nh->counter_valid)
3266 mlxsw_reg_ratr_counter_pack(ratr_pl, nh->counter_index, true);
3268 mlxsw_reg_ratr_counter_pack(ratr_pl, 0, false);
3270 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
3273 int mlxsw_sp_nexthop_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
3274 struct mlxsw_sp_nexthop *nh)
3278 for (i = 0; i < nh->num_adj_entries; i++) {
3281 err = __mlxsw_sp_nexthop_update(mlxsw_sp, adj_index + i, nh);
3289 static int __mlxsw_sp_nexthop_ipip_update(struct mlxsw_sp *mlxsw_sp,
3291 struct mlxsw_sp_nexthop *nh)
3293 const struct mlxsw_sp_ipip_ops *ipip_ops;
3295 ipip_ops = mlxsw_sp->router->ipip_ops_arr[nh->ipip_entry->ipipt];
3296 return ipip_ops->nexthop_update(mlxsw_sp, adj_index, nh->ipip_entry);
3299 static int mlxsw_sp_nexthop_ipip_update(struct mlxsw_sp *mlxsw_sp,
3301 struct mlxsw_sp_nexthop *nh)
3305 for (i = 0; i < nh->num_adj_entries; i++) {
3308 err = __mlxsw_sp_nexthop_ipip_update(mlxsw_sp, adj_index + i,
3318 mlxsw_sp_nexthop_group_update(struct mlxsw_sp *mlxsw_sp,
3319 struct mlxsw_sp_nexthop_group_info *nhgi,
3322 u32 adj_index = nhgi->adj_index; /* base */
3323 struct mlxsw_sp_nexthop *nh;
3326 for (i = 0; i < nhgi->count; i++) {
3327 nh = &nhgi->nexthops[i];
3329 if (!nh->should_offload) {
3334 if (nh->update || reallocate) {
3338 case MLXSW_SP_NEXTHOP_TYPE_ETH:
3339 err = mlxsw_sp_nexthop_update
3340 (mlxsw_sp, adj_index, nh);
3342 case MLXSW_SP_NEXTHOP_TYPE_IPIP:
3343 err = mlxsw_sp_nexthop_ipip_update
3344 (mlxsw_sp, adj_index, nh);
3352 adj_index += nh->num_adj_entries;
3358 mlxsw_sp_nexthop_fib_entries_update(struct mlxsw_sp *mlxsw_sp,
3359 struct mlxsw_sp_nexthop_group *nh_grp)
3361 struct mlxsw_sp_fib_entry *fib_entry;
3364 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
3365 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
3372 static void mlxsw_sp_adj_grp_size_round_up(u16 *p_adj_grp_size)
3374 /* Valid sizes for an adjacency group are:
3375 * 1-64, 512, 1024, 2048 and 4096.
3377 if (*p_adj_grp_size <= 64)
3379 else if (*p_adj_grp_size <= 512)
3380 *p_adj_grp_size = 512;
3381 else if (*p_adj_grp_size <= 1024)
3382 *p_adj_grp_size = 1024;
3383 else if (*p_adj_grp_size <= 2048)
3384 *p_adj_grp_size = 2048;
3386 *p_adj_grp_size = 4096;
3389 static void mlxsw_sp_adj_grp_size_round_down(u16 *p_adj_grp_size,
3390 unsigned int alloc_size)
3392 if (alloc_size >= 4096)
3393 *p_adj_grp_size = 4096;
3394 else if (alloc_size >= 2048)
3395 *p_adj_grp_size = 2048;
3396 else if (alloc_size >= 1024)
3397 *p_adj_grp_size = 1024;
3398 else if (alloc_size >= 512)
3399 *p_adj_grp_size = 512;
3402 static int mlxsw_sp_fix_adj_grp_size(struct mlxsw_sp *mlxsw_sp,
3403 u16 *p_adj_grp_size)
3405 unsigned int alloc_size;
3408 /* Round up the requested group size to the next size supported
3409 * by the device and make sure the request can be satisfied.
3411 mlxsw_sp_adj_grp_size_round_up(p_adj_grp_size);
3412 err = mlxsw_sp_kvdl_alloc_count_query(mlxsw_sp,
3413 MLXSW_SP_KVDL_ENTRY_TYPE_ADJ,
3414 *p_adj_grp_size, &alloc_size);
3417 /* It is possible the allocation results in more allocated
3418 * entries than requested. Try to use as much of them as
3421 mlxsw_sp_adj_grp_size_round_down(p_adj_grp_size, alloc_size);
3427 mlxsw_sp_nexthop_group_normalize(struct mlxsw_sp_nexthop_group_info *nhgi)
3429 int i, g = 0, sum_norm_weight = 0;
3430 struct mlxsw_sp_nexthop *nh;
3432 for (i = 0; i < nhgi->count; i++) {
3433 nh = &nhgi->nexthops[i];
3435 if (!nh->should_offload)
3438 g = gcd(nh->nh_weight, g);
3443 for (i = 0; i < nhgi->count; i++) {
3444 nh = &nhgi->nexthops[i];
3446 if (!nh->should_offload)
3448 nh->norm_nh_weight = nh->nh_weight / g;
3449 sum_norm_weight += nh->norm_nh_weight;
3452 nhgi->sum_norm_weight = sum_norm_weight;
3456 mlxsw_sp_nexthop_group_rebalance(struct mlxsw_sp_nexthop_group_info *nhgi)
3458 int i, weight = 0, lower_bound = 0;
3459 int total = nhgi->sum_norm_weight;
3460 u16 ecmp_size = nhgi->ecmp_size;
3462 for (i = 0; i < nhgi->count; i++) {
3463 struct mlxsw_sp_nexthop *nh = &nhgi->nexthops[i];
3466 if (!nh->should_offload)
3468 weight += nh->norm_nh_weight;
3469 upper_bound = DIV_ROUND_CLOSEST(ecmp_size * weight, total);
3470 nh->num_adj_entries = upper_bound - lower_bound;
3471 lower_bound = upper_bound;
3475 static struct mlxsw_sp_nexthop *
3476 mlxsw_sp_rt6_nexthop(struct mlxsw_sp_nexthop_group *nh_grp,
3477 const struct mlxsw_sp_rt6 *mlxsw_sp_rt6);
3480 mlxsw_sp_nexthop4_group_offload_refresh(struct mlxsw_sp *mlxsw_sp,
3481 struct mlxsw_sp_nexthop_group *nh_grp)
3485 for (i = 0; i < nh_grp->nhgi->count; i++) {
3486 struct mlxsw_sp_nexthop *nh = &nh_grp->nhgi->nexthops[i];
3489 nh->key.fib_nh->fib_nh_flags |= RTNH_F_OFFLOAD;
3491 nh->key.fib_nh->fib_nh_flags &= ~RTNH_F_OFFLOAD;
3496 __mlxsw_sp_nexthop6_group_offload_refresh(struct mlxsw_sp_nexthop_group *nh_grp,
3497 struct mlxsw_sp_fib6_entry *fib6_entry)
3499 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
3501 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
3502 struct fib6_nh *fib6_nh = mlxsw_sp_rt6->rt->fib6_nh;
3503 struct mlxsw_sp_nexthop *nh;
3505 nh = mlxsw_sp_rt6_nexthop(nh_grp, mlxsw_sp_rt6);
3506 if (nh && nh->offloaded)
3507 fib6_nh->fib_nh_flags |= RTNH_F_OFFLOAD;
3509 fib6_nh->fib_nh_flags &= ~RTNH_F_OFFLOAD;
3514 mlxsw_sp_nexthop6_group_offload_refresh(struct mlxsw_sp *mlxsw_sp,
3515 struct mlxsw_sp_nexthop_group *nh_grp)
3517 struct mlxsw_sp_fib6_entry *fib6_entry;
3519 /* Unfortunately, in IPv6 the route and the nexthop are described by
3520 * the same struct, so we need to iterate over all the routes using the
3521 * nexthop group and set / clear the offload indication for them.
3523 list_for_each_entry(fib6_entry, &nh_grp->fib_list,
3524 common.nexthop_group_node)
3525 __mlxsw_sp_nexthop6_group_offload_refresh(nh_grp, fib6_entry);
3529 mlxsw_sp_nexthop_group_offload_refresh(struct mlxsw_sp *mlxsw_sp,
3530 struct mlxsw_sp_nexthop_group *nh_grp)
3532 switch (nh_grp->type) {
3533 case MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV4:
3534 mlxsw_sp_nexthop4_group_offload_refresh(mlxsw_sp, nh_grp);
3536 case MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV6:
3537 mlxsw_sp_nexthop6_group_offload_refresh(mlxsw_sp, nh_grp);
3543 mlxsw_sp_nexthop_group_refresh(struct mlxsw_sp *mlxsw_sp,
3544 struct mlxsw_sp_nexthop_group *nh_grp)
3546 struct mlxsw_sp_nexthop_group_info *nhgi = nh_grp->nhgi;
3547 u16 ecmp_size, old_ecmp_size;
3548 struct mlxsw_sp_nexthop *nh;
3549 bool offload_change = false;
3551 bool old_adj_index_valid;
3556 if (!nhgi->gateway) {
3557 mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
3561 for (i = 0; i < nhgi->count; i++) {
3562 nh = &nhgi->nexthops[i];
3564 if (nh->should_offload != nh->offloaded) {
3565 offload_change = true;
3566 if (nh->should_offload)
3570 if (!offload_change) {
3571 /* Nothing was added or removed, so no need to reallocate. Just
3572 * update MAC on existing adjacency indexes.
3574 err = mlxsw_sp_nexthop_group_update(mlxsw_sp, nhgi, false);
3576 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
3581 mlxsw_sp_nexthop_group_normalize(nhgi);
3582 if (!nhgi->sum_norm_weight)
3583 /* No neigh of this group is connected so we just set
3584 * the trap and let everthing flow through kernel.
3588 ecmp_size = nhgi->sum_norm_weight;
3589 err = mlxsw_sp_fix_adj_grp_size(mlxsw_sp, &ecmp_size);
3591 /* No valid allocation size available. */
3594 err = mlxsw_sp_kvdl_alloc(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ,
3595 ecmp_size, &adj_index);
3597 /* We ran out of KVD linear space, just set the
3598 * trap and let everything flow through kernel.
3600 dev_warn(mlxsw_sp->bus_info->dev, "Failed to allocate KVD linear area for nexthop group.\n");
3603 old_adj_index_valid = nhgi->adj_index_valid;
3604 old_adj_index = nhgi->adj_index;
3605 old_ecmp_size = nhgi->ecmp_size;
3606 nhgi->adj_index_valid = 1;
3607 nhgi->adj_index = adj_index;
3608 nhgi->ecmp_size = ecmp_size;
3609 mlxsw_sp_nexthop_group_rebalance(nhgi);
3610 err = mlxsw_sp_nexthop_group_update(mlxsw_sp, nhgi, true);
3612 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
3616 mlxsw_sp_nexthop_group_offload_refresh(mlxsw_sp, nh_grp);
3618 if (!old_adj_index_valid) {
3619 /* The trap was set for fib entries, so we have to call
3620 * fib entry update to unset it and use adjacency index.
3622 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
3624 dev_warn(mlxsw_sp->bus_info->dev, "Failed to add adjacency index to fib entries.\n");
3630 err = mlxsw_sp_adj_index_mass_update(mlxsw_sp, nh_grp,
3631 old_adj_index, old_ecmp_size);
3632 mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ,
3633 old_ecmp_size, old_adj_index);
3635 dev_warn(mlxsw_sp->bus_info->dev, "Failed to mass-update adjacency index for nexthop group.\n");
3642 old_adj_index_valid = nhgi->adj_index_valid;
3643 nhgi->adj_index_valid = 0;
3644 for (i = 0; i < nhgi->count; i++) {
3645 nh = &nhgi->nexthops[i];
3648 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
3650 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set traps for fib entries.\n");
3651 mlxsw_sp_nexthop_group_offload_refresh(mlxsw_sp, nh_grp);
3652 if (old_adj_index_valid)
3653 mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ,
3654 nhgi->ecmp_size, nhgi->adj_index);
3657 static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop *nh,
3661 nh->should_offload = 1;
3663 nh->should_offload = 0;
3668 mlxsw_sp_nexthop_dead_neigh_replace(struct mlxsw_sp *mlxsw_sp,
3669 struct mlxsw_sp_neigh_entry *neigh_entry)
3671 struct neighbour *n, *old_n = neigh_entry->key.n;
3672 struct mlxsw_sp_nexthop *nh;
3673 bool entry_connected;
3677 nh = list_first_entry(&neigh_entry->nexthop_list,
3678 struct mlxsw_sp_nexthop, neigh_list_node);
3680 n = neigh_lookup(nh->neigh_tbl, &nh->gw_addr, nh->rif->dev);
3682 n = neigh_create(nh->neigh_tbl, &nh->gw_addr, nh->rif->dev);
3685 neigh_event_send(n, NULL);
3688 mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
3689 neigh_entry->key.n = n;
3690 err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
3692 goto err_neigh_entry_insert;
3694 read_lock_bh(&n->lock);
3695 nud_state = n->nud_state;
3697 read_unlock_bh(&n->lock);
3698 entry_connected = nud_state & NUD_VALID && !dead;
3700 list_for_each_entry(nh, &neigh_entry->nexthop_list,
3702 neigh_release(old_n);
3704 __mlxsw_sp_nexthop_neigh_update(nh, !entry_connected);
3705 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nhgi->nh_grp);
3712 err_neigh_entry_insert:
3713 neigh_entry->key.n = old_n;
3714 mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
3720 mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
3721 struct mlxsw_sp_neigh_entry *neigh_entry,
3722 bool removing, bool dead)
3724 struct mlxsw_sp_nexthop *nh;
3726 if (list_empty(&neigh_entry->nexthop_list))
3732 err = mlxsw_sp_nexthop_dead_neigh_replace(mlxsw_sp,
3735 dev_err(mlxsw_sp->bus_info->dev, "Failed to replace dead neigh\n");
3739 list_for_each_entry(nh, &neigh_entry->nexthop_list,
3741 __mlxsw_sp_nexthop_neigh_update(nh, removing);
3742 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nhgi->nh_grp);
3746 static void mlxsw_sp_nexthop_rif_init(struct mlxsw_sp_nexthop *nh,
3747 struct mlxsw_sp_rif *rif)
3753 list_add(&nh->rif_list_node, &rif->nexthop_list);
3756 static void mlxsw_sp_nexthop_rif_fini(struct mlxsw_sp_nexthop *nh)
3761 list_del(&nh->rif_list_node);
3765 static int mlxsw_sp_nexthop_neigh_init(struct mlxsw_sp *mlxsw_sp,
3766 struct mlxsw_sp_nexthop *nh)
3768 struct mlxsw_sp_neigh_entry *neigh_entry;
3769 struct neighbour *n;
3773 if (!nh->nhgi->gateway || nh->neigh_entry)
3776 /* Take a reference of neigh here ensuring that neigh would
3777 * not be destructed before the nexthop entry is finished.
3778 * The reference is taken either in neigh_lookup() or
3779 * in neigh_create() in case n is not found.
3781 n = neigh_lookup(nh->neigh_tbl, &nh->gw_addr, nh->rif->dev);
3783 n = neigh_create(nh->neigh_tbl, &nh->gw_addr, nh->rif->dev);
3786 neigh_event_send(n, NULL);
3788 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
3790 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
3791 if (IS_ERR(neigh_entry)) {
3793 goto err_neigh_entry_create;
3797 /* If that is the first nexthop connected to that neigh, add to
3798 * nexthop_neighs_list
3800 if (list_empty(&neigh_entry->nexthop_list))
3801 list_add_tail(&neigh_entry->nexthop_neighs_list_node,
3802 &mlxsw_sp->router->nexthop_neighs_list);
3804 nh->neigh_entry = neigh_entry;
3805 list_add_tail(&nh->neigh_list_node, &neigh_entry->nexthop_list);
3806 read_lock_bh(&n->lock);
3807 nud_state = n->nud_state;
3809 read_unlock_bh(&n->lock);
3810 __mlxsw_sp_nexthop_neigh_update(nh, !(nud_state & NUD_VALID && !dead));
3814 err_neigh_entry_create:
3819 static void mlxsw_sp_nexthop_neigh_fini(struct mlxsw_sp *mlxsw_sp,
3820 struct mlxsw_sp_nexthop *nh)
3822 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
3823 struct neighbour *n;
3827 n = neigh_entry->key.n;
3829 __mlxsw_sp_nexthop_neigh_update(nh, true);
3830 list_del(&nh->neigh_list_node);
3831 nh->neigh_entry = NULL;
3833 /* If that is the last nexthop connected to that neigh, remove from
3834 * nexthop_neighs_list
3836 if (list_empty(&neigh_entry->nexthop_list))
3837 list_del(&neigh_entry->nexthop_neighs_list_node);
3839 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
3840 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
3845 static bool mlxsw_sp_ipip_netdev_ul_up(struct net_device *ol_dev)
3847 struct net_device *ul_dev;
3851 ul_dev = __mlxsw_sp_ipip_netdev_ul_dev_get(ol_dev);
3852 is_up = ul_dev ? (ul_dev->flags & IFF_UP) : true;
3858 static void mlxsw_sp_nexthop_ipip_init(struct mlxsw_sp *mlxsw_sp,
3859 struct mlxsw_sp_nexthop *nh,
3860 struct mlxsw_sp_ipip_entry *ipip_entry)
3864 if (!nh->nhgi->gateway || nh->ipip_entry)
3867 nh->ipip_entry = ipip_entry;
3868 removing = !mlxsw_sp_ipip_netdev_ul_up(ipip_entry->ol_dev);
3869 __mlxsw_sp_nexthop_neigh_update(nh, removing);
3870 mlxsw_sp_nexthop_rif_init(nh, &ipip_entry->ol_lb->common);
3873 static void mlxsw_sp_nexthop_ipip_fini(struct mlxsw_sp *mlxsw_sp,
3874 struct mlxsw_sp_nexthop *nh)
3876 struct mlxsw_sp_ipip_entry *ipip_entry = nh->ipip_entry;
3881 __mlxsw_sp_nexthop_neigh_update(nh, true);
3882 nh->ipip_entry = NULL;
3885 static bool mlxsw_sp_nexthop4_ipip_type(const struct mlxsw_sp *mlxsw_sp,
3886 const struct fib_nh *fib_nh,
3887 enum mlxsw_sp_ipip_type *p_ipipt)
3889 struct net_device *dev = fib_nh->fib_nh_dev;
3892 fib_nh->nh_parent->fib_type == RTN_UNICAST &&
3893 mlxsw_sp_netdev_ipip_type(mlxsw_sp, dev, p_ipipt);
3896 static int mlxsw_sp_nexthop_type_init(struct mlxsw_sp *mlxsw_sp,
3897 struct mlxsw_sp_nexthop *nh,
3898 const struct net_device *dev)
3900 const struct mlxsw_sp_ipip_ops *ipip_ops;
3901 struct mlxsw_sp_ipip_entry *ipip_entry;
3902 struct mlxsw_sp_rif *rif;
3905 ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, dev);
3907 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
3908 if (ipip_ops->can_offload(mlxsw_sp, dev)) {
3909 nh->type = MLXSW_SP_NEXTHOP_TYPE_IPIP;
3910 mlxsw_sp_nexthop_ipip_init(mlxsw_sp, nh, ipip_entry);
3915 nh->type = MLXSW_SP_NEXTHOP_TYPE_ETH;
3916 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3920 mlxsw_sp_nexthop_rif_init(nh, rif);
3921 err = mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
3923 goto err_neigh_init;
3928 mlxsw_sp_nexthop_rif_fini(nh);
3932 static void mlxsw_sp_nexthop_type_fini(struct mlxsw_sp *mlxsw_sp,
3933 struct mlxsw_sp_nexthop *nh)
3936 case MLXSW_SP_NEXTHOP_TYPE_ETH:
3937 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
3938 mlxsw_sp_nexthop_rif_fini(nh);
3940 case MLXSW_SP_NEXTHOP_TYPE_IPIP:
3941 mlxsw_sp_nexthop_rif_fini(nh);
3942 mlxsw_sp_nexthop_ipip_fini(mlxsw_sp, nh);
3947 static int mlxsw_sp_nexthop4_init(struct mlxsw_sp *mlxsw_sp,
3948 struct mlxsw_sp_nexthop_group *nh_grp,
3949 struct mlxsw_sp_nexthop *nh,
3950 struct fib_nh *fib_nh)
3952 struct net_device *dev = fib_nh->fib_nh_dev;
3953 struct in_device *in_dev;
3956 nh->nhgi = nh_grp->nhgi;
3957 nh->key.fib_nh = fib_nh;
3958 #ifdef CONFIG_IP_ROUTE_MULTIPATH
3959 nh->nh_weight = fib_nh->fib_nh_weight;
3963 memcpy(&nh->gw_addr, &fib_nh->fib_nh_gw4, sizeof(fib_nh->fib_nh_gw4));
3964 nh->neigh_tbl = &arp_tbl;
3965 err = mlxsw_sp_nexthop_insert(mlxsw_sp, nh);
3969 mlxsw_sp_nexthop_counter_alloc(mlxsw_sp, nh);
3970 list_add_tail(&nh->router_list_node, &mlxsw_sp->router->nexthop_list);
3976 in_dev = __in_dev_get_rcu(dev);
3977 if (in_dev && IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
3978 fib_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
3984 err = mlxsw_sp_nexthop_type_init(mlxsw_sp, nh, dev);
3986 goto err_nexthop_neigh_init;
3990 err_nexthop_neigh_init:
3991 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
3995 static void mlxsw_sp_nexthop4_fini(struct mlxsw_sp *mlxsw_sp,
3996 struct mlxsw_sp_nexthop *nh)
3998 mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
3999 list_del(&nh->router_list_node);
4000 mlxsw_sp_nexthop_counter_free(mlxsw_sp, nh);
4001 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
4004 static void mlxsw_sp_nexthop4_event(struct mlxsw_sp *mlxsw_sp,
4005 unsigned long event, struct fib_nh *fib_nh)
4007 struct mlxsw_sp_nexthop_key key;
4008 struct mlxsw_sp_nexthop *nh;
4010 if (mlxsw_sp->router->aborted)
4013 key.fib_nh = fib_nh;
4014 nh = mlxsw_sp_nexthop_lookup(mlxsw_sp, key);
4019 case FIB_EVENT_NH_ADD:
4020 mlxsw_sp_nexthop_type_init(mlxsw_sp, nh, fib_nh->fib_nh_dev);
4022 case FIB_EVENT_NH_DEL:
4023 mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
4027 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nhgi->nh_grp);
4030 static void mlxsw_sp_nexthop_rif_update(struct mlxsw_sp *mlxsw_sp,
4031 struct mlxsw_sp_rif *rif)
4033 struct mlxsw_sp_nexthop *nh;
4036 list_for_each_entry(nh, &rif->nexthop_list, rif_list_node) {
4038 case MLXSW_SP_NEXTHOP_TYPE_ETH:
4041 case MLXSW_SP_NEXTHOP_TYPE_IPIP:
4042 removing = !mlxsw_sp_ipip_netdev_ul_up(rif->dev);
4049 __mlxsw_sp_nexthop_neigh_update(nh, removing);
4050 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nhgi->nh_grp);
4054 static void mlxsw_sp_nexthop_rif_migrate(struct mlxsw_sp *mlxsw_sp,
4055 struct mlxsw_sp_rif *old_rif,
4056 struct mlxsw_sp_rif *new_rif)
4058 struct mlxsw_sp_nexthop *nh;
4060 list_splice_init(&old_rif->nexthop_list, &new_rif->nexthop_list);
4061 list_for_each_entry(nh, &new_rif->nexthop_list, rif_list_node)
4063 mlxsw_sp_nexthop_rif_update(mlxsw_sp, new_rif);
4066 static void mlxsw_sp_nexthop_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
4067 struct mlxsw_sp_rif *rif)
4069 struct mlxsw_sp_nexthop *nh, *tmp;
4071 list_for_each_entry_safe(nh, tmp, &rif->nexthop_list, rif_list_node) {
4072 mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
4073 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nhgi->nh_grp);
4077 static bool mlxsw_sp_fi_is_gateway(const struct mlxsw_sp *mlxsw_sp,
4078 struct fib_info *fi)
4080 const struct fib_nh *nh = fib_info_nh(fi, 0);
4082 return nh->fib_nh_scope == RT_SCOPE_LINK ||
4083 mlxsw_sp_nexthop4_ipip_type(mlxsw_sp, nh, NULL);
4087 mlxsw_sp_nexthop4_group_info_init(struct mlxsw_sp *mlxsw_sp,
4088 struct mlxsw_sp_nexthop_group *nh_grp)
4090 unsigned int nhs = fib_info_num_path(nh_grp->ipv4.fi);
4091 struct mlxsw_sp_nexthop_group_info *nhgi;
4092 struct mlxsw_sp_nexthop *nh;
4095 nhgi = kzalloc(struct_size(nhgi, nexthops, nhs), GFP_KERNEL);
4098 nh_grp->nhgi = nhgi;
4099 nhgi->nh_grp = nh_grp;
4100 nhgi->gateway = mlxsw_sp_fi_is_gateway(mlxsw_sp, nh_grp->ipv4.fi);
4102 for (i = 0; i < nhgi->count; i++) {
4103 struct fib_nh *fib_nh;
4105 nh = &nhgi->nexthops[i];
4106 fib_nh = fib_info_nh(nh_grp->ipv4.fi, i);
4107 err = mlxsw_sp_nexthop4_init(mlxsw_sp, nh_grp, nh, fib_nh);
4109 goto err_nexthop4_init;
4111 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
4116 for (i--; i >= 0; i--) {
4117 nh = &nhgi->nexthops[i];
4118 mlxsw_sp_nexthop4_fini(mlxsw_sp, nh);
4125 mlxsw_sp_nexthop4_group_info_fini(struct mlxsw_sp *mlxsw_sp,
4126 struct mlxsw_sp_nexthop_group *nh_grp)
4128 struct mlxsw_sp_nexthop_group_info *nhgi = nh_grp->nhgi;
4131 for (i = nhgi->count - 1; i >= 0; i--) {
4132 struct mlxsw_sp_nexthop *nh = &nhgi->nexthops[i];
4134 mlxsw_sp_nexthop4_fini(mlxsw_sp, nh);
4136 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
4137 WARN_ON_ONCE(nhgi->adj_index_valid);
4141 static struct mlxsw_sp_nexthop_group *
4142 mlxsw_sp_nexthop4_group_create(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
4144 struct mlxsw_sp_nexthop_group *nh_grp;
4147 nh_grp = kzalloc(sizeof(*nh_grp), GFP_KERNEL);
4149 return ERR_PTR(-ENOMEM);
4150 INIT_LIST_HEAD(&nh_grp->fib_list);
4151 nh_grp->type = MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV4;
4152 nh_grp->ipv4.fi = fi;
4155 err = mlxsw_sp_nexthop4_group_info_init(mlxsw_sp, nh_grp);
4157 goto err_nexthop_group_info_init;
4159 err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp);
4161 goto err_nexthop_group_insert;
4165 err_nexthop_group_insert:
4166 mlxsw_sp_nexthop4_group_info_fini(mlxsw_sp, nh_grp);
4167 err_nexthop_group_info_init:
4170 return ERR_PTR(err);
4174 mlxsw_sp_nexthop4_group_destroy(struct mlxsw_sp *mlxsw_sp,
4175 struct mlxsw_sp_nexthop_group *nh_grp)
4177 mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp);
4178 mlxsw_sp_nexthop4_group_info_fini(mlxsw_sp, nh_grp);
4179 fib_info_put(nh_grp->ipv4.fi);
4183 static int mlxsw_sp_nexthop4_group_get(struct mlxsw_sp *mlxsw_sp,
4184 struct mlxsw_sp_fib_entry *fib_entry,
4185 struct fib_info *fi)
4187 struct mlxsw_sp_nexthop_group *nh_grp;
4189 nh_grp = mlxsw_sp_nexthop4_group_lookup(mlxsw_sp, fi);
4191 nh_grp = mlxsw_sp_nexthop4_group_create(mlxsw_sp, fi);
4193 return PTR_ERR(nh_grp);
4195 list_add_tail(&fib_entry->nexthop_group_node, &nh_grp->fib_list);
4196 fib_entry->nh_group = nh_grp;
4200 static void mlxsw_sp_nexthop4_group_put(struct mlxsw_sp *mlxsw_sp,
4201 struct mlxsw_sp_fib_entry *fib_entry)
4203 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
4205 list_del(&fib_entry->nexthop_group_node);
4206 if (!list_empty(&nh_grp->fib_list))
4208 mlxsw_sp_nexthop4_group_destroy(mlxsw_sp, nh_grp);
4212 mlxsw_sp_fib4_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry)
4214 struct mlxsw_sp_fib4_entry *fib4_entry;
4216 fib4_entry = container_of(fib_entry, struct mlxsw_sp_fib4_entry,
4218 return !fib4_entry->tos;
4222 mlxsw_sp_fib_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry)
4224 struct mlxsw_sp_nexthop_group *nh_group = fib_entry->nh_group;
4226 switch (fib_entry->fib_node->fib->proto) {
4227 case MLXSW_SP_L3_PROTO_IPV4:
4228 if (!mlxsw_sp_fib4_entry_should_offload(fib_entry))
4231 case MLXSW_SP_L3_PROTO_IPV6:
4235 switch (fib_entry->type) {
4236 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
4237 return !!nh_group->nhgi->adj_index_valid;
4238 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
4239 return !!nh_group->nhgi->nh_rif;
4240 case MLXSW_SP_FIB_ENTRY_TYPE_BLACKHOLE:
4241 case MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP:
4242 case MLXSW_SP_FIB_ENTRY_TYPE_NVE_DECAP:
4249 static struct mlxsw_sp_nexthop *
4250 mlxsw_sp_rt6_nexthop(struct mlxsw_sp_nexthop_group *nh_grp,
4251 const struct mlxsw_sp_rt6 *mlxsw_sp_rt6)
4255 for (i = 0; i < nh_grp->nhgi->count; i++) {
4256 struct mlxsw_sp_nexthop *nh = &nh_grp->nhgi->nexthops[i];
4257 struct fib6_info *rt = mlxsw_sp_rt6->rt;
4259 if (nh->rif && nh->rif->dev == rt->fib6_nh->fib_nh_dev &&
4260 ipv6_addr_equal((const struct in6_addr *) &nh->gw_addr,
4261 &rt->fib6_nh->fib_nh_gw6))
4270 mlxsw_sp_fib4_entry_hw_flags_set(struct mlxsw_sp *mlxsw_sp,
4271 struct mlxsw_sp_fib_entry *fib_entry)
4273 u32 *p_dst = (u32 *) fib_entry->fib_node->key.addr;
4274 int dst_len = fib_entry->fib_node->key.prefix_len;
4275 struct mlxsw_sp_fib4_entry *fib4_entry;
4276 struct fib_rt_info fri;
4277 bool should_offload;
4279 should_offload = mlxsw_sp_fib_entry_should_offload(fib_entry);
4280 fib4_entry = container_of(fib_entry, struct mlxsw_sp_fib4_entry,
4282 fri.fi = fib4_entry->fi;
4283 fri.tb_id = fib4_entry->tb_id;
4284 fri.dst = cpu_to_be32(*p_dst);
4285 fri.dst_len = dst_len;
4286 fri.tos = fib4_entry->tos;
4287 fri.type = fib4_entry->type;
4288 fri.offload = should_offload;
4289 fri.trap = !should_offload;
4290 fib_alias_hw_flags_set(mlxsw_sp_net(mlxsw_sp), &fri);
4294 mlxsw_sp_fib4_entry_hw_flags_clear(struct mlxsw_sp *mlxsw_sp,
4295 struct mlxsw_sp_fib_entry *fib_entry)
4297 u32 *p_dst = (u32 *) fib_entry->fib_node->key.addr;
4298 int dst_len = fib_entry->fib_node->key.prefix_len;
4299 struct mlxsw_sp_fib4_entry *fib4_entry;
4300 struct fib_rt_info fri;
4302 fib4_entry = container_of(fib_entry, struct mlxsw_sp_fib4_entry,
4304 fri.fi = fib4_entry->fi;
4305 fri.tb_id = fib4_entry->tb_id;
4306 fri.dst = cpu_to_be32(*p_dst);
4307 fri.dst_len = dst_len;
4308 fri.tos = fib4_entry->tos;
4309 fri.type = fib4_entry->type;
4310 fri.offload = false;
4312 fib_alias_hw_flags_set(mlxsw_sp_net(mlxsw_sp), &fri);
4316 mlxsw_sp_fib6_entry_hw_flags_set(struct mlxsw_sp *mlxsw_sp,
4317 struct mlxsw_sp_fib_entry *fib_entry)
4319 struct mlxsw_sp_fib6_entry *fib6_entry;
4320 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
4321 bool should_offload;
4323 should_offload = mlxsw_sp_fib_entry_should_offload(fib_entry);
4325 /* In IPv6 a multipath route is represented using multiple routes, so
4326 * we need to set the flags on all of them.
4328 fib6_entry = container_of(fib_entry, struct mlxsw_sp_fib6_entry,
4330 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list)
4331 fib6_info_hw_flags_set(mlxsw_sp_rt6->rt, should_offload,
4336 mlxsw_sp_fib6_entry_hw_flags_clear(struct mlxsw_sp *mlxsw_sp,
4337 struct mlxsw_sp_fib_entry *fib_entry)
4339 struct mlxsw_sp_fib6_entry *fib6_entry;
4340 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
4342 fib6_entry = container_of(fib_entry, struct mlxsw_sp_fib6_entry,
4344 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list)
4345 fib6_info_hw_flags_set(mlxsw_sp_rt6->rt, false, false);
4349 mlxsw_sp_fib_entry_hw_flags_set(struct mlxsw_sp *mlxsw_sp,
4350 struct mlxsw_sp_fib_entry *fib_entry)
4352 switch (fib_entry->fib_node->fib->proto) {
4353 case MLXSW_SP_L3_PROTO_IPV4:
4354 mlxsw_sp_fib4_entry_hw_flags_set(mlxsw_sp, fib_entry);
4356 case MLXSW_SP_L3_PROTO_IPV6:
4357 mlxsw_sp_fib6_entry_hw_flags_set(mlxsw_sp, fib_entry);
4363 mlxsw_sp_fib_entry_hw_flags_clear(struct mlxsw_sp *mlxsw_sp,
4364 struct mlxsw_sp_fib_entry *fib_entry)
4366 switch (fib_entry->fib_node->fib->proto) {
4367 case MLXSW_SP_L3_PROTO_IPV4:
4368 mlxsw_sp_fib4_entry_hw_flags_clear(mlxsw_sp, fib_entry);
4370 case MLXSW_SP_L3_PROTO_IPV6:
4371 mlxsw_sp_fib6_entry_hw_flags_clear(mlxsw_sp, fib_entry);
4377 mlxsw_sp_fib_entry_hw_flags_refresh(struct mlxsw_sp *mlxsw_sp,
4378 struct mlxsw_sp_fib_entry *fib_entry,
4379 enum mlxsw_sp_fib_entry_op op)
4382 case MLXSW_SP_FIB_ENTRY_OP_WRITE:
4383 case MLXSW_SP_FIB_ENTRY_OP_UPDATE:
4384 mlxsw_sp_fib_entry_hw_flags_set(mlxsw_sp, fib_entry);
4386 case MLXSW_SP_FIB_ENTRY_OP_DELETE:
4387 mlxsw_sp_fib_entry_hw_flags_clear(mlxsw_sp, fib_entry);
4394 struct mlxsw_sp_fib_entry_op_ctx_basic {
4395 char ralue_pl[MLXSW_REG_RALUE_LEN];
4399 mlxsw_sp_router_ll_basic_fib_entry_pack(struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4400 enum mlxsw_sp_l3proto proto,
4401 enum mlxsw_sp_fib_entry_op op,
4402 u16 virtual_router, u8 prefix_len,
4403 unsigned char *addr,
4404 struct mlxsw_sp_fib_entry_priv *priv)
4406 struct mlxsw_sp_fib_entry_op_ctx_basic *op_ctx_basic = (void *) op_ctx->ll_priv;
4407 enum mlxsw_reg_ralxx_protocol ralxx_proto;
4408 char *ralue_pl = op_ctx_basic->ralue_pl;
4409 enum mlxsw_reg_ralue_op ralue_op;
4411 ralxx_proto = (enum mlxsw_reg_ralxx_protocol) proto;
4414 case MLXSW_SP_FIB_ENTRY_OP_WRITE:
4415 case MLXSW_SP_FIB_ENTRY_OP_UPDATE:
4416 ralue_op = MLXSW_REG_RALUE_OP_WRITE_WRITE;
4418 case MLXSW_SP_FIB_ENTRY_OP_DELETE:
4419 ralue_op = MLXSW_REG_RALUE_OP_WRITE_DELETE;
4427 case MLXSW_SP_L3_PROTO_IPV4:
4428 mlxsw_reg_ralue_pack4(ralue_pl, ralxx_proto, ralue_op,
4429 virtual_router, prefix_len, (u32 *) addr);
4431 case MLXSW_SP_L3_PROTO_IPV6:
4432 mlxsw_reg_ralue_pack6(ralue_pl, ralxx_proto, ralue_op,
4433 virtual_router, prefix_len, addr);
4439 mlxsw_sp_router_ll_basic_fib_entry_act_remote_pack(struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4440 enum mlxsw_reg_ralue_trap_action trap_action,
4441 u16 trap_id, u32 adjacency_index, u16 ecmp_size)
4443 struct mlxsw_sp_fib_entry_op_ctx_basic *op_ctx_basic = (void *) op_ctx->ll_priv;
4445 mlxsw_reg_ralue_act_remote_pack(op_ctx_basic->ralue_pl, trap_action,
4446 trap_id, adjacency_index, ecmp_size);
4450 mlxsw_sp_router_ll_basic_fib_entry_act_local_pack(struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4451 enum mlxsw_reg_ralue_trap_action trap_action,
4452 u16 trap_id, u16 local_erif)
4454 struct mlxsw_sp_fib_entry_op_ctx_basic *op_ctx_basic = (void *) op_ctx->ll_priv;
4456 mlxsw_reg_ralue_act_local_pack(op_ctx_basic->ralue_pl, trap_action,
4457 trap_id, local_erif);
4461 mlxsw_sp_router_ll_basic_fib_entry_act_ip2me_pack(struct mlxsw_sp_fib_entry_op_ctx *op_ctx)
4463 struct mlxsw_sp_fib_entry_op_ctx_basic *op_ctx_basic = (void *) op_ctx->ll_priv;
4465 mlxsw_reg_ralue_act_ip2me_pack(op_ctx_basic->ralue_pl);
4469 mlxsw_sp_router_ll_basic_fib_entry_act_ip2me_tun_pack(struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4472 struct mlxsw_sp_fib_entry_op_ctx_basic *op_ctx_basic = (void *) op_ctx->ll_priv;
4474 mlxsw_reg_ralue_act_ip2me_tun_pack(op_ctx_basic->ralue_pl, tunnel_ptr);
4478 mlxsw_sp_router_ll_basic_fib_entry_commit(struct mlxsw_sp *mlxsw_sp,
4479 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4480 bool *postponed_for_bulk)
4482 struct mlxsw_sp_fib_entry_op_ctx_basic *op_ctx_basic = (void *) op_ctx->ll_priv;
4484 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue),
4485 op_ctx_basic->ralue_pl);
4489 mlxsw_sp_router_ll_basic_fib_entry_is_committed(struct mlxsw_sp_fib_entry_priv *priv)
4494 static void mlxsw_sp_fib_entry_pack(struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4495 struct mlxsw_sp_fib_entry *fib_entry,
4496 enum mlxsw_sp_fib_entry_op op)
4498 struct mlxsw_sp_fib *fib = fib_entry->fib_node->fib;
4500 mlxsw_sp_fib_entry_op_ctx_priv_hold(op_ctx, fib_entry->priv);
4501 fib->ll_ops->fib_entry_pack(op_ctx, fib->proto, op, fib->vr->id,
4502 fib_entry->fib_node->key.prefix_len,
4503 fib_entry->fib_node->key.addr,
4507 int mlxsw_sp_fib_entry_commit(struct mlxsw_sp *mlxsw_sp,
4508 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4509 const struct mlxsw_sp_router_ll_ops *ll_ops)
4511 bool postponed_for_bulk = false;
4514 err = ll_ops->fib_entry_commit(mlxsw_sp, op_ctx, &postponed_for_bulk);
4515 if (!postponed_for_bulk)
4516 mlxsw_sp_fib_entry_op_ctx_priv_put_all(op_ctx);
4520 static int mlxsw_sp_adj_discard_write(struct mlxsw_sp *mlxsw_sp, u16 rif_index)
4522 enum mlxsw_reg_ratr_trap_action trap_action;
4523 char ratr_pl[MLXSW_REG_RATR_LEN];
4526 if (mlxsw_sp->router->adj_discard_index_valid)
4529 err = mlxsw_sp_kvdl_alloc(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ, 1,
4530 &mlxsw_sp->router->adj_discard_index);
4534 trap_action = MLXSW_REG_RATR_TRAP_ACTION_DISCARD_ERRORS;
4535 mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY, true,
4536 MLXSW_REG_RATR_TYPE_ETHERNET,
4537 mlxsw_sp->router->adj_discard_index, rif_index);
4538 mlxsw_reg_ratr_trap_action_set(ratr_pl, trap_action);
4539 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
4541 goto err_ratr_write;
4543 mlxsw_sp->router->adj_discard_index_valid = true;
4548 mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ, 1,
4549 mlxsw_sp->router->adj_discard_index);
4553 static int mlxsw_sp_fib_entry_op_remote(struct mlxsw_sp *mlxsw_sp,
4554 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4555 struct mlxsw_sp_fib_entry *fib_entry,
4556 enum mlxsw_sp_fib_entry_op op)
4558 const struct mlxsw_sp_router_ll_ops *ll_ops = fib_entry->fib_node->fib->ll_ops;
4559 struct mlxsw_sp_nexthop_group *nh_group = fib_entry->nh_group;
4560 struct mlxsw_sp_nexthop_group_info *nhgi = nh_group->nhgi;
4561 enum mlxsw_reg_ralue_trap_action trap_action;
4563 u32 adjacency_index = 0;
4567 /* In case the nexthop group adjacency index is valid, use it
4568 * with provided ECMP size. Otherwise, setup trap and pass
4569 * traffic to kernel.
4571 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
4572 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
4573 adjacency_index = nhgi->adj_index;
4574 ecmp_size = nhgi->ecmp_size;
4575 } else if (!nhgi->adj_index_valid && nhgi->count && nhgi->nh_rif) {
4576 err = mlxsw_sp_adj_discard_write(mlxsw_sp,
4577 nhgi->nh_rif->rif_index);
4580 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
4581 adjacency_index = mlxsw_sp->router->adj_discard_index;
4584 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
4585 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
4588 mlxsw_sp_fib_entry_pack(op_ctx, fib_entry, op);
4589 ll_ops->fib_entry_act_remote_pack(op_ctx, trap_action, trap_id,
4590 adjacency_index, ecmp_size);
4591 return mlxsw_sp_fib_entry_commit(mlxsw_sp, op_ctx, ll_ops);
4594 static int mlxsw_sp_fib_entry_op_local(struct mlxsw_sp *mlxsw_sp,
4595 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4596 struct mlxsw_sp_fib_entry *fib_entry,
4597 enum mlxsw_sp_fib_entry_op op)
4599 const struct mlxsw_sp_router_ll_ops *ll_ops = fib_entry->fib_node->fib->ll_ops;
4600 struct mlxsw_sp_rif *rif = fib_entry->nh_group->nhgi->nh_rif;
4601 enum mlxsw_reg_ralue_trap_action trap_action;
4605 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
4606 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
4607 rif_index = rif->rif_index;
4609 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
4610 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
4613 mlxsw_sp_fib_entry_pack(op_ctx, fib_entry, op);
4614 ll_ops->fib_entry_act_local_pack(op_ctx, trap_action, trap_id, rif_index);
4615 return mlxsw_sp_fib_entry_commit(mlxsw_sp, op_ctx, ll_ops);
4618 static int mlxsw_sp_fib_entry_op_trap(struct mlxsw_sp *mlxsw_sp,
4619 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4620 struct mlxsw_sp_fib_entry *fib_entry,
4621 enum mlxsw_sp_fib_entry_op op)
4623 const struct mlxsw_sp_router_ll_ops *ll_ops = fib_entry->fib_node->fib->ll_ops;
4625 mlxsw_sp_fib_entry_pack(op_ctx, fib_entry, op);
4626 ll_ops->fib_entry_act_ip2me_pack(op_ctx);
4627 return mlxsw_sp_fib_entry_commit(mlxsw_sp, op_ctx, ll_ops);
4630 static int mlxsw_sp_fib_entry_op_blackhole(struct mlxsw_sp *mlxsw_sp,
4631 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4632 struct mlxsw_sp_fib_entry *fib_entry,
4633 enum mlxsw_sp_fib_entry_op op)
4635 const struct mlxsw_sp_router_ll_ops *ll_ops = fib_entry->fib_node->fib->ll_ops;
4636 enum mlxsw_reg_ralue_trap_action trap_action;
4638 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_DISCARD_ERROR;
4639 mlxsw_sp_fib_entry_pack(op_ctx, fib_entry, op);
4640 ll_ops->fib_entry_act_local_pack(op_ctx, trap_action, 0, 0);
4641 return mlxsw_sp_fib_entry_commit(mlxsw_sp, op_ctx, ll_ops);
4645 mlxsw_sp_fib_entry_op_unreachable(struct mlxsw_sp *mlxsw_sp,
4646 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4647 struct mlxsw_sp_fib_entry *fib_entry,
4648 enum mlxsw_sp_fib_entry_op op)
4650 const struct mlxsw_sp_router_ll_ops *ll_ops = fib_entry->fib_node->fib->ll_ops;
4651 enum mlxsw_reg_ralue_trap_action trap_action;
4654 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
4655 trap_id = MLXSW_TRAP_ID_RTR_INGRESS1;
4657 mlxsw_sp_fib_entry_pack(op_ctx, fib_entry, op);
4658 ll_ops->fib_entry_act_local_pack(op_ctx, trap_action, trap_id, 0);
4659 return mlxsw_sp_fib_entry_commit(mlxsw_sp, op_ctx, ll_ops);
4663 mlxsw_sp_fib_entry_op_ipip_decap(struct mlxsw_sp *mlxsw_sp,
4664 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4665 struct mlxsw_sp_fib_entry *fib_entry,
4666 enum mlxsw_sp_fib_entry_op op)
4668 const struct mlxsw_sp_router_ll_ops *ll_ops = fib_entry->fib_node->fib->ll_ops;
4669 struct mlxsw_sp_ipip_entry *ipip_entry = fib_entry->decap.ipip_entry;
4670 const struct mlxsw_sp_ipip_ops *ipip_ops;
4672 if (WARN_ON(!ipip_entry))
4675 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
4676 return ipip_ops->fib_entry_op(mlxsw_sp, ll_ops, op_ctx, ipip_entry, op,
4677 fib_entry->decap.tunnel_index, fib_entry->priv);
4680 static int mlxsw_sp_fib_entry_op_nve_decap(struct mlxsw_sp *mlxsw_sp,
4681 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4682 struct mlxsw_sp_fib_entry *fib_entry,
4683 enum mlxsw_sp_fib_entry_op op)
4685 const struct mlxsw_sp_router_ll_ops *ll_ops = fib_entry->fib_node->fib->ll_ops;
4687 mlxsw_sp_fib_entry_pack(op_ctx, fib_entry, op);
4688 ll_ops->fib_entry_act_ip2me_tun_pack(op_ctx,
4689 fib_entry->decap.tunnel_index);
4690 return mlxsw_sp_fib_entry_commit(mlxsw_sp, op_ctx, ll_ops);
4693 static int __mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
4694 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4695 struct mlxsw_sp_fib_entry *fib_entry,
4696 enum mlxsw_sp_fib_entry_op op)
4698 switch (fib_entry->type) {
4699 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
4700 return mlxsw_sp_fib_entry_op_remote(mlxsw_sp, op_ctx, fib_entry, op);
4701 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
4702 return mlxsw_sp_fib_entry_op_local(mlxsw_sp, op_ctx, fib_entry, op);
4703 case MLXSW_SP_FIB_ENTRY_TYPE_TRAP:
4704 return mlxsw_sp_fib_entry_op_trap(mlxsw_sp, op_ctx, fib_entry, op);
4705 case MLXSW_SP_FIB_ENTRY_TYPE_BLACKHOLE:
4706 return mlxsw_sp_fib_entry_op_blackhole(mlxsw_sp, op_ctx, fib_entry, op);
4707 case MLXSW_SP_FIB_ENTRY_TYPE_UNREACHABLE:
4708 return mlxsw_sp_fib_entry_op_unreachable(mlxsw_sp, op_ctx, fib_entry, op);
4709 case MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP:
4710 return mlxsw_sp_fib_entry_op_ipip_decap(mlxsw_sp, op_ctx, fib_entry, op);
4711 case MLXSW_SP_FIB_ENTRY_TYPE_NVE_DECAP:
4712 return mlxsw_sp_fib_entry_op_nve_decap(mlxsw_sp, op_ctx, fib_entry, op);
4717 static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
4718 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4719 struct mlxsw_sp_fib_entry *fib_entry,
4720 enum mlxsw_sp_fib_entry_op op)
4722 int err = __mlxsw_sp_fib_entry_op(mlxsw_sp, op_ctx, fib_entry, op);
4727 mlxsw_sp_fib_entry_hw_flags_refresh(mlxsw_sp, fib_entry, op);
4732 static int __mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
4733 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4734 struct mlxsw_sp_fib_entry *fib_entry,
4737 return mlxsw_sp_fib_entry_op(mlxsw_sp, op_ctx, fib_entry,
4738 is_new ? MLXSW_SP_FIB_ENTRY_OP_WRITE :
4739 MLXSW_SP_FIB_ENTRY_OP_UPDATE);
4742 static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
4743 struct mlxsw_sp_fib_entry *fib_entry)
4745 struct mlxsw_sp_fib_entry_op_ctx *op_ctx = mlxsw_sp->router->ll_op_ctx;
4747 mlxsw_sp_fib_entry_op_ctx_clear(op_ctx);
4748 return __mlxsw_sp_fib_entry_update(mlxsw_sp, op_ctx, fib_entry, false);
4751 static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
4752 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4753 struct mlxsw_sp_fib_entry *fib_entry)
4755 const struct mlxsw_sp_router_ll_ops *ll_ops = fib_entry->fib_node->fib->ll_ops;
4757 if (!ll_ops->fib_entry_is_committed(fib_entry->priv))
4759 return mlxsw_sp_fib_entry_op(mlxsw_sp, op_ctx, fib_entry,
4760 MLXSW_SP_FIB_ENTRY_OP_DELETE);
4764 mlxsw_sp_fib4_entry_type_set(struct mlxsw_sp *mlxsw_sp,
4765 const struct fib_entry_notifier_info *fen_info,
4766 struct mlxsw_sp_fib_entry *fib_entry)
4768 struct net_device *dev = fib_info_nh(fen_info->fi, 0)->fib_nh_dev;
4769 union mlxsw_sp_l3addr dip = { .addr4 = htonl(fen_info->dst) };
4770 struct mlxsw_sp_router *router = mlxsw_sp->router;
4771 u32 tb_id = mlxsw_sp_fix_tb_id(fen_info->tb_id);
4772 struct mlxsw_sp_ipip_entry *ipip_entry;
4773 struct fib_info *fi = fen_info->fi;
4775 switch (fen_info->type) {
4777 ipip_entry = mlxsw_sp_ipip_entry_find_by_decap(mlxsw_sp, dev,
4778 MLXSW_SP_L3_PROTO_IPV4, dip);
4779 if (ipip_entry && ipip_entry->ol_dev->flags & IFF_UP) {
4780 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP;
4781 return mlxsw_sp_fib_entry_decap_init(mlxsw_sp,
4785 if (mlxsw_sp_router_nve_is_decap(mlxsw_sp, tb_id,
4786 MLXSW_SP_L3_PROTO_IPV4,
4790 tunnel_index = router->nve_decap_config.tunnel_index;
4791 fib_entry->decap.tunnel_index = tunnel_index;
4792 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_NVE_DECAP;
4797 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
4800 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_BLACKHOLE;
4802 case RTN_UNREACHABLE:
4804 /* Packets hitting these routes need to be trapped, but
4805 * can do so with a lower priority than packets directed
4806 * at the host, so use action type local instead of trap.
4808 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_UNREACHABLE;
4811 if (mlxsw_sp_fi_is_gateway(mlxsw_sp, fi))
4812 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
4814 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
4822 mlxsw_sp_fib4_entry_type_unset(struct mlxsw_sp *mlxsw_sp,
4823 struct mlxsw_sp_fib_entry *fib_entry)
4825 switch (fib_entry->type) {
4826 case MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP:
4827 mlxsw_sp_fib_entry_decap_fini(mlxsw_sp, fib_entry);
4834 static struct mlxsw_sp_fib4_entry *
4835 mlxsw_sp_fib4_entry_create(struct mlxsw_sp *mlxsw_sp,
4836 struct mlxsw_sp_fib_node *fib_node,
4837 const struct fib_entry_notifier_info *fen_info)
4839 struct mlxsw_sp_fib4_entry *fib4_entry;
4840 struct mlxsw_sp_fib_entry *fib_entry;
4843 fib4_entry = kzalloc(sizeof(*fib4_entry), GFP_KERNEL);
4845 return ERR_PTR(-ENOMEM);
4846 fib_entry = &fib4_entry->common;
4848 fib_entry->priv = mlxsw_sp_fib_entry_priv_create(fib_node->fib->ll_ops);
4849 if (IS_ERR(fib_entry->priv)) {
4850 err = PTR_ERR(fib_entry->priv);
4851 goto err_fib_entry_priv_create;
4854 err = mlxsw_sp_fib4_entry_type_set(mlxsw_sp, fen_info, fib_entry);
4856 goto err_fib4_entry_type_set;
4858 err = mlxsw_sp_nexthop4_group_get(mlxsw_sp, fib_entry, fen_info->fi);
4860 goto err_nexthop4_group_get;
4862 fib4_entry->fi = fen_info->fi;
4863 fib_info_hold(fib4_entry->fi);
4864 fib4_entry->tb_id = fen_info->tb_id;
4865 fib4_entry->type = fen_info->type;
4866 fib4_entry->tos = fen_info->tos;
4868 fib_entry->fib_node = fib_node;
4872 err_nexthop4_group_get:
4873 mlxsw_sp_fib4_entry_type_unset(mlxsw_sp, fib_entry);
4874 err_fib4_entry_type_set:
4875 mlxsw_sp_fib_entry_priv_put(fib_entry->priv);
4876 err_fib_entry_priv_create:
4878 return ERR_PTR(err);
4881 static void mlxsw_sp_fib4_entry_destroy(struct mlxsw_sp *mlxsw_sp,
4882 struct mlxsw_sp_fib4_entry *fib4_entry)
4884 fib_info_put(fib4_entry->fi);
4885 mlxsw_sp_nexthop4_group_put(mlxsw_sp, &fib4_entry->common);
4886 mlxsw_sp_fib4_entry_type_unset(mlxsw_sp, &fib4_entry->common);
4887 mlxsw_sp_fib_entry_priv_put(fib4_entry->common.priv);
4891 static struct mlxsw_sp_fib4_entry *
4892 mlxsw_sp_fib4_entry_lookup(struct mlxsw_sp *mlxsw_sp,
4893 const struct fib_entry_notifier_info *fen_info)
4895 struct mlxsw_sp_fib4_entry *fib4_entry;
4896 struct mlxsw_sp_fib_node *fib_node;
4897 struct mlxsw_sp_fib *fib;
4898 struct mlxsw_sp_vr *vr;
4900 vr = mlxsw_sp_vr_find(mlxsw_sp, fen_info->tb_id);
4903 fib = mlxsw_sp_vr_fib(vr, MLXSW_SP_L3_PROTO_IPV4);
4905 fib_node = mlxsw_sp_fib_node_lookup(fib, &fen_info->dst,
4906 sizeof(fen_info->dst),
4911 fib4_entry = container_of(fib_node->fib_entry,
4912 struct mlxsw_sp_fib4_entry, common);
4913 if (fib4_entry->tb_id == fen_info->tb_id &&
4914 fib4_entry->tos == fen_info->tos &&
4915 fib4_entry->type == fen_info->type &&
4916 fib4_entry->fi == fen_info->fi)
4922 static const struct rhashtable_params mlxsw_sp_fib_ht_params = {
4923 .key_offset = offsetof(struct mlxsw_sp_fib_node, key),
4924 .head_offset = offsetof(struct mlxsw_sp_fib_node, ht_node),
4925 .key_len = sizeof(struct mlxsw_sp_fib_key),
4926 .automatic_shrinking = true,
4929 static int mlxsw_sp_fib_node_insert(struct mlxsw_sp_fib *fib,
4930 struct mlxsw_sp_fib_node *fib_node)
4932 return rhashtable_insert_fast(&fib->ht, &fib_node->ht_node,
4933 mlxsw_sp_fib_ht_params);
4936 static void mlxsw_sp_fib_node_remove(struct mlxsw_sp_fib *fib,
4937 struct mlxsw_sp_fib_node *fib_node)
4939 rhashtable_remove_fast(&fib->ht, &fib_node->ht_node,
4940 mlxsw_sp_fib_ht_params);
4943 static struct mlxsw_sp_fib_node *
4944 mlxsw_sp_fib_node_lookup(struct mlxsw_sp_fib *fib, const void *addr,
4945 size_t addr_len, unsigned char prefix_len)
4947 struct mlxsw_sp_fib_key key;
4949 memset(&key, 0, sizeof(key));
4950 memcpy(key.addr, addr, addr_len);
4951 key.prefix_len = prefix_len;
4952 return rhashtable_lookup_fast(&fib->ht, &key, mlxsw_sp_fib_ht_params);
4955 static struct mlxsw_sp_fib_node *
4956 mlxsw_sp_fib_node_create(struct mlxsw_sp_fib *fib, const void *addr,
4957 size_t addr_len, unsigned char prefix_len)
4959 struct mlxsw_sp_fib_node *fib_node;
4961 fib_node = kzalloc(sizeof(*fib_node), GFP_KERNEL);
4965 list_add(&fib_node->list, &fib->node_list);
4966 memcpy(fib_node->key.addr, addr, addr_len);
4967 fib_node->key.prefix_len = prefix_len;
4972 static void mlxsw_sp_fib_node_destroy(struct mlxsw_sp_fib_node *fib_node)
4974 list_del(&fib_node->list);
4978 static int mlxsw_sp_fib_lpm_tree_link(struct mlxsw_sp *mlxsw_sp,
4979 struct mlxsw_sp_fib_node *fib_node)
4981 struct mlxsw_sp_prefix_usage req_prefix_usage;
4982 struct mlxsw_sp_fib *fib = fib_node->fib;
4983 struct mlxsw_sp_lpm_tree *lpm_tree;
4986 lpm_tree = mlxsw_sp->router->lpm.proto_trees[fib->proto];
4987 if (lpm_tree->prefix_ref_count[fib_node->key.prefix_len] != 0)
4990 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage, &lpm_tree->prefix_usage);
4991 mlxsw_sp_prefix_usage_set(&req_prefix_usage, fib_node->key.prefix_len);
4992 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
4994 if (IS_ERR(lpm_tree))
4995 return PTR_ERR(lpm_tree);
4997 err = mlxsw_sp_vrs_lpm_tree_replace(mlxsw_sp, fib, lpm_tree);
4999 goto err_lpm_tree_replace;
5002 lpm_tree->prefix_ref_count[fib_node->key.prefix_len]++;
5005 err_lpm_tree_replace:
5006 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
5010 static void mlxsw_sp_fib_lpm_tree_unlink(struct mlxsw_sp *mlxsw_sp,
5011 struct mlxsw_sp_fib_node *fib_node)
5013 struct mlxsw_sp_lpm_tree *lpm_tree = fib_node->fib->lpm_tree;
5014 struct mlxsw_sp_prefix_usage req_prefix_usage;
5015 struct mlxsw_sp_fib *fib = fib_node->fib;
5018 if (--lpm_tree->prefix_ref_count[fib_node->key.prefix_len] != 0)
5020 /* Try to construct a new LPM tree from the current prefix usage
5021 * minus the unused one. If we fail, continue using the old one.
5023 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage, &lpm_tree->prefix_usage);
5024 mlxsw_sp_prefix_usage_clear(&req_prefix_usage,
5025 fib_node->key.prefix_len);
5026 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
5028 if (IS_ERR(lpm_tree))
5031 err = mlxsw_sp_vrs_lpm_tree_replace(mlxsw_sp, fib, lpm_tree);
5033 goto err_lpm_tree_replace;
5037 err_lpm_tree_replace:
5038 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
5041 static int mlxsw_sp_fib_node_init(struct mlxsw_sp *mlxsw_sp,
5042 struct mlxsw_sp_fib_node *fib_node,
5043 struct mlxsw_sp_fib *fib)
5047 err = mlxsw_sp_fib_node_insert(fib, fib_node);
5050 fib_node->fib = fib;
5052 err = mlxsw_sp_fib_lpm_tree_link(mlxsw_sp, fib_node);
5054 goto err_fib_lpm_tree_link;
5058 err_fib_lpm_tree_link:
5059 fib_node->fib = NULL;
5060 mlxsw_sp_fib_node_remove(fib, fib_node);
5064 static void mlxsw_sp_fib_node_fini(struct mlxsw_sp *mlxsw_sp,
5065 struct mlxsw_sp_fib_node *fib_node)
5067 struct mlxsw_sp_fib *fib = fib_node->fib;
5069 mlxsw_sp_fib_lpm_tree_unlink(mlxsw_sp, fib_node);
5070 fib_node->fib = NULL;
5071 mlxsw_sp_fib_node_remove(fib, fib_node);
5074 static struct mlxsw_sp_fib_node *
5075 mlxsw_sp_fib_node_get(struct mlxsw_sp *mlxsw_sp, u32 tb_id, const void *addr,
5076 size_t addr_len, unsigned char prefix_len,
5077 enum mlxsw_sp_l3proto proto)
5079 struct mlxsw_sp_fib_node *fib_node;
5080 struct mlxsw_sp_fib *fib;
5081 struct mlxsw_sp_vr *vr;
5084 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id, NULL);
5086 return ERR_CAST(vr);
5087 fib = mlxsw_sp_vr_fib(vr, proto);
5089 fib_node = mlxsw_sp_fib_node_lookup(fib, addr, addr_len, prefix_len);
5093 fib_node = mlxsw_sp_fib_node_create(fib, addr, addr_len, prefix_len);
5096 goto err_fib_node_create;
5099 err = mlxsw_sp_fib_node_init(mlxsw_sp, fib_node, fib);
5101 goto err_fib_node_init;
5106 mlxsw_sp_fib_node_destroy(fib_node);
5107 err_fib_node_create:
5108 mlxsw_sp_vr_put(mlxsw_sp, vr);
5109 return ERR_PTR(err);
5112 static void mlxsw_sp_fib_node_put(struct mlxsw_sp *mlxsw_sp,
5113 struct mlxsw_sp_fib_node *fib_node)
5115 struct mlxsw_sp_vr *vr = fib_node->fib->vr;
5117 if (fib_node->fib_entry)
5119 mlxsw_sp_fib_node_fini(mlxsw_sp, fib_node);
5120 mlxsw_sp_fib_node_destroy(fib_node);
5121 mlxsw_sp_vr_put(mlxsw_sp, vr);
5124 static int mlxsw_sp_fib_node_entry_link(struct mlxsw_sp *mlxsw_sp,
5125 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5126 struct mlxsw_sp_fib_entry *fib_entry)
5128 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
5129 bool is_new = !fib_node->fib_entry;
5132 fib_node->fib_entry = fib_entry;
5134 err = __mlxsw_sp_fib_entry_update(mlxsw_sp, op_ctx, fib_entry, is_new);
5136 goto err_fib_entry_update;
5140 err_fib_entry_update:
5141 fib_node->fib_entry = NULL;
5145 static int __mlxsw_sp_fib_node_entry_unlink(struct mlxsw_sp *mlxsw_sp,
5146 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5147 struct mlxsw_sp_fib_entry *fib_entry)
5149 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
5152 err = mlxsw_sp_fib_entry_del(mlxsw_sp, op_ctx, fib_entry);
5153 fib_node->fib_entry = NULL;
5157 static void mlxsw_sp_fib_node_entry_unlink(struct mlxsw_sp *mlxsw_sp,
5158 struct mlxsw_sp_fib_entry *fib_entry)
5160 struct mlxsw_sp_fib_entry_op_ctx *op_ctx = mlxsw_sp->router->ll_op_ctx;
5162 mlxsw_sp_fib_entry_op_ctx_clear(op_ctx);
5163 __mlxsw_sp_fib_node_entry_unlink(mlxsw_sp, op_ctx, fib_entry);
5166 static bool mlxsw_sp_fib4_allow_replace(struct mlxsw_sp_fib4_entry *fib4_entry)
5168 struct mlxsw_sp_fib_node *fib_node = fib4_entry->common.fib_node;
5169 struct mlxsw_sp_fib4_entry *fib4_replaced;
5171 if (!fib_node->fib_entry)
5174 fib4_replaced = container_of(fib_node->fib_entry,
5175 struct mlxsw_sp_fib4_entry, common);
5176 if (fib4_entry->tb_id == RT_TABLE_MAIN &&
5177 fib4_replaced->tb_id == RT_TABLE_LOCAL)
5184 mlxsw_sp_router_fib4_replace(struct mlxsw_sp *mlxsw_sp,
5185 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5186 const struct fib_entry_notifier_info *fen_info)
5188 struct mlxsw_sp_fib4_entry *fib4_entry, *fib4_replaced;
5189 struct mlxsw_sp_fib_entry *replaced;
5190 struct mlxsw_sp_fib_node *fib_node;
5193 if (mlxsw_sp->router->aborted)
5196 fib_node = mlxsw_sp_fib_node_get(mlxsw_sp, fen_info->tb_id,
5197 &fen_info->dst, sizeof(fen_info->dst),
5199 MLXSW_SP_L3_PROTO_IPV4);
5200 if (IS_ERR(fib_node)) {
5201 dev_warn(mlxsw_sp->bus_info->dev, "Failed to get FIB node\n");
5202 return PTR_ERR(fib_node);
5205 fib4_entry = mlxsw_sp_fib4_entry_create(mlxsw_sp, fib_node, fen_info);
5206 if (IS_ERR(fib4_entry)) {
5207 dev_warn(mlxsw_sp->bus_info->dev, "Failed to create FIB entry\n");
5208 err = PTR_ERR(fib4_entry);
5209 goto err_fib4_entry_create;
5212 if (!mlxsw_sp_fib4_allow_replace(fib4_entry)) {
5213 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
5214 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5218 replaced = fib_node->fib_entry;
5219 err = mlxsw_sp_fib_node_entry_link(mlxsw_sp, op_ctx, &fib4_entry->common);
5221 dev_warn(mlxsw_sp->bus_info->dev, "Failed to link FIB entry to node\n");
5222 goto err_fib_node_entry_link;
5225 /* Nothing to replace */
5229 mlxsw_sp_fib_entry_hw_flags_clear(mlxsw_sp, replaced);
5230 fib4_replaced = container_of(replaced, struct mlxsw_sp_fib4_entry,
5232 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_replaced);
5236 err_fib_node_entry_link:
5237 fib_node->fib_entry = replaced;
5238 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
5239 err_fib4_entry_create:
5240 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5244 static int mlxsw_sp_router_fib4_del(struct mlxsw_sp *mlxsw_sp,
5245 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5246 struct fib_entry_notifier_info *fen_info)
5248 struct mlxsw_sp_fib4_entry *fib4_entry;
5249 struct mlxsw_sp_fib_node *fib_node;
5252 if (mlxsw_sp->router->aborted)
5255 fib4_entry = mlxsw_sp_fib4_entry_lookup(mlxsw_sp, fen_info);
5258 fib_node = fib4_entry->common.fib_node;
5260 err = __mlxsw_sp_fib_node_entry_unlink(mlxsw_sp, op_ctx, &fib4_entry->common);
5261 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
5262 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5266 static bool mlxsw_sp_fib6_rt_should_ignore(const struct fib6_info *rt)
5268 /* Multicast routes aren't supported, so ignore them. Neighbour
5269 * Discovery packets are specifically trapped.
5271 if (ipv6_addr_type(&rt->fib6_dst.addr) & IPV6_ADDR_MULTICAST)
5274 /* Cloned routes are irrelevant in the forwarding path. */
5275 if (rt->fib6_flags & RTF_CACHE)
5281 static struct mlxsw_sp_rt6 *mlxsw_sp_rt6_create(struct fib6_info *rt)
5283 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
5285 mlxsw_sp_rt6 = kzalloc(sizeof(*mlxsw_sp_rt6), GFP_KERNEL);
5287 return ERR_PTR(-ENOMEM);
5289 /* In case of route replace, replaced route is deleted with
5290 * no notification. Take reference to prevent accessing freed
5293 mlxsw_sp_rt6->rt = rt;
5296 return mlxsw_sp_rt6;
5299 #if IS_ENABLED(CONFIG_IPV6)
5300 static void mlxsw_sp_rt6_release(struct fib6_info *rt)
5302 fib6_info_release(rt);
5305 static void mlxsw_sp_rt6_release(struct fib6_info *rt)
5310 static void mlxsw_sp_rt6_destroy(struct mlxsw_sp_rt6 *mlxsw_sp_rt6)
5312 struct fib6_nh *fib6_nh = mlxsw_sp_rt6->rt->fib6_nh;
5314 fib6_nh->fib_nh_flags &= ~RTNH_F_OFFLOAD;
5315 mlxsw_sp_rt6_release(mlxsw_sp_rt6->rt);
5316 kfree(mlxsw_sp_rt6);
5319 static struct fib6_info *
5320 mlxsw_sp_fib6_entry_rt(const struct mlxsw_sp_fib6_entry *fib6_entry)
5322 return list_first_entry(&fib6_entry->rt6_list, struct mlxsw_sp_rt6,
5326 static struct mlxsw_sp_rt6 *
5327 mlxsw_sp_fib6_entry_rt_find(const struct mlxsw_sp_fib6_entry *fib6_entry,
5328 const struct fib6_info *rt)
5330 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
5332 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
5333 if (mlxsw_sp_rt6->rt == rt)
5334 return mlxsw_sp_rt6;
5340 static bool mlxsw_sp_nexthop6_ipip_type(const struct mlxsw_sp *mlxsw_sp,
5341 const struct fib6_info *rt,
5342 enum mlxsw_sp_ipip_type *ret)
5344 return rt->fib6_nh->fib_nh_dev &&
5345 mlxsw_sp_netdev_ipip_type(mlxsw_sp, rt->fib6_nh->fib_nh_dev, ret);
5348 static int mlxsw_sp_nexthop6_init(struct mlxsw_sp *mlxsw_sp,
5349 struct mlxsw_sp_nexthop_group *nh_grp,
5350 struct mlxsw_sp_nexthop *nh,
5351 const struct fib6_info *rt)
5353 struct net_device *dev = rt->fib6_nh->fib_nh_dev;
5355 nh->nhgi = nh_grp->nhgi;
5356 nh->nh_weight = rt->fib6_nh->fib_nh_weight;
5357 memcpy(&nh->gw_addr, &rt->fib6_nh->fib_nh_gw6, sizeof(nh->gw_addr));
5358 #if IS_ENABLED(CONFIG_IPV6)
5359 nh->neigh_tbl = &nd_tbl;
5361 mlxsw_sp_nexthop_counter_alloc(mlxsw_sp, nh);
5363 list_add_tail(&nh->router_list_node, &mlxsw_sp->router->nexthop_list);
5367 nh->ifindex = dev->ifindex;
5369 return mlxsw_sp_nexthop_type_init(mlxsw_sp, nh, dev);
5372 static void mlxsw_sp_nexthop6_fini(struct mlxsw_sp *mlxsw_sp,
5373 struct mlxsw_sp_nexthop *nh)
5375 mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
5376 list_del(&nh->router_list_node);
5377 mlxsw_sp_nexthop_counter_free(mlxsw_sp, nh);
5380 static bool mlxsw_sp_rt6_is_gateway(const struct mlxsw_sp *mlxsw_sp,
5381 const struct fib6_info *rt)
5383 return rt->fib6_nh->fib_nh_gw_family ||
5384 mlxsw_sp_nexthop6_ipip_type(mlxsw_sp, rt, NULL);
5388 mlxsw_sp_nexthop6_group_info_init(struct mlxsw_sp *mlxsw_sp,
5389 struct mlxsw_sp_nexthop_group *nh_grp,
5390 struct mlxsw_sp_fib6_entry *fib6_entry)
5392 struct mlxsw_sp_nexthop_group_info *nhgi;
5393 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
5394 struct mlxsw_sp_nexthop *nh;
5397 nhgi = kzalloc(struct_size(nhgi, nexthops, fib6_entry->nrt6),
5401 nh_grp->nhgi = nhgi;
5402 nhgi->nh_grp = nh_grp;
5403 mlxsw_sp_rt6 = list_first_entry(&fib6_entry->rt6_list,
5404 struct mlxsw_sp_rt6, list);
5405 nhgi->gateway = mlxsw_sp_rt6_is_gateway(mlxsw_sp, mlxsw_sp_rt6->rt);
5406 nhgi->count = fib6_entry->nrt6;
5407 for (i = 0; i < nhgi->count; i++) {
5408 struct fib6_info *rt = mlxsw_sp_rt6->rt;
5410 nh = &nhgi->nexthops[i];
5411 err = mlxsw_sp_nexthop6_init(mlxsw_sp, nh_grp, nh, rt);
5413 goto err_nexthop6_init;
5414 mlxsw_sp_rt6 = list_next_entry(mlxsw_sp_rt6, list);
5416 nh_grp->nhgi = nhgi;
5417 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
5422 for (i--; i >= 0; i--) {
5423 nh = &nhgi->nexthops[i];
5424 mlxsw_sp_nexthop6_fini(mlxsw_sp, nh);
5431 mlxsw_sp_nexthop6_group_info_fini(struct mlxsw_sp *mlxsw_sp,
5432 struct mlxsw_sp_nexthop_group *nh_grp)
5434 struct mlxsw_sp_nexthop_group_info *nhgi = nh_grp->nhgi;
5437 for (i = nhgi->count - 1; i >= 0; i--) {
5438 struct mlxsw_sp_nexthop *nh = &nhgi->nexthops[i];
5440 mlxsw_sp_nexthop6_fini(mlxsw_sp, nh);
5442 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
5443 WARN_ON_ONCE(nhgi->adj_index_valid);
5447 static struct mlxsw_sp_nexthop_group *
5448 mlxsw_sp_nexthop6_group_create(struct mlxsw_sp *mlxsw_sp,
5449 struct mlxsw_sp_fib6_entry *fib6_entry)
5451 struct mlxsw_sp_nexthop_group *nh_grp;
5454 nh_grp = kzalloc(sizeof(*nh_grp), GFP_KERNEL);
5456 return ERR_PTR(-ENOMEM);
5457 INIT_LIST_HEAD(&nh_grp->fib_list);
5458 nh_grp->type = MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV6;
5460 err = mlxsw_sp_nexthop6_group_info_init(mlxsw_sp, nh_grp, fib6_entry);
5462 goto err_nexthop_group_info_init;
5464 err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp);
5466 goto err_nexthop_group_insert;
5470 err_nexthop_group_insert:
5471 mlxsw_sp_nexthop6_group_info_fini(mlxsw_sp, nh_grp);
5472 err_nexthop_group_info_init:
5474 return ERR_PTR(err);
5478 mlxsw_sp_nexthop6_group_destroy(struct mlxsw_sp *mlxsw_sp,
5479 struct mlxsw_sp_nexthop_group *nh_grp)
5481 mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp);
5482 mlxsw_sp_nexthop6_group_info_fini(mlxsw_sp, nh_grp);
5486 static int mlxsw_sp_nexthop6_group_get(struct mlxsw_sp *mlxsw_sp,
5487 struct mlxsw_sp_fib6_entry *fib6_entry)
5489 struct mlxsw_sp_nexthop_group *nh_grp;
5491 nh_grp = mlxsw_sp_nexthop6_group_lookup(mlxsw_sp, fib6_entry);
5493 nh_grp = mlxsw_sp_nexthop6_group_create(mlxsw_sp, fib6_entry);
5495 return PTR_ERR(nh_grp);
5498 list_add_tail(&fib6_entry->common.nexthop_group_node,
5500 fib6_entry->common.nh_group = nh_grp;
5502 /* The route and the nexthop are described by the same struct, so we
5503 * need to the update the nexthop offload indication for the new route.
5505 __mlxsw_sp_nexthop6_group_offload_refresh(nh_grp, fib6_entry);
5510 static void mlxsw_sp_nexthop6_group_put(struct mlxsw_sp *mlxsw_sp,
5511 struct mlxsw_sp_fib_entry *fib_entry)
5513 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
5515 list_del(&fib_entry->nexthop_group_node);
5516 if (!list_empty(&nh_grp->fib_list))
5518 mlxsw_sp_nexthop6_group_destroy(mlxsw_sp, nh_grp);
5521 static int mlxsw_sp_nexthop6_group_update(struct mlxsw_sp *mlxsw_sp,
5522 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5523 struct mlxsw_sp_fib6_entry *fib6_entry)
5525 struct mlxsw_sp_nexthop_group *old_nh_grp = fib6_entry->common.nh_group;
5528 fib6_entry->common.nh_group = NULL;
5529 list_del(&fib6_entry->common.nexthop_group_node);
5531 err = mlxsw_sp_nexthop6_group_get(mlxsw_sp, fib6_entry);
5533 goto err_nexthop6_group_get;
5535 /* In case this entry is offloaded, then the adjacency index
5536 * currently associated with it in the device's table is that
5537 * of the old group. Start using the new one instead.
5539 err = __mlxsw_sp_fib_entry_update(mlxsw_sp, op_ctx,
5540 &fib6_entry->common, false);
5542 goto err_fib_entry_update;
5544 if (list_empty(&old_nh_grp->fib_list))
5545 mlxsw_sp_nexthop6_group_destroy(mlxsw_sp, old_nh_grp);
5549 err_fib_entry_update:
5550 mlxsw_sp_nexthop6_group_put(mlxsw_sp, &fib6_entry->common);
5551 err_nexthop6_group_get:
5552 list_add_tail(&fib6_entry->common.nexthop_group_node,
5553 &old_nh_grp->fib_list);
5554 fib6_entry->common.nh_group = old_nh_grp;
5559 mlxsw_sp_fib6_entry_nexthop_add(struct mlxsw_sp *mlxsw_sp,
5560 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5561 struct mlxsw_sp_fib6_entry *fib6_entry,
5562 struct fib6_info **rt_arr, unsigned int nrt6)
5564 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
5567 for (i = 0; i < nrt6; i++) {
5568 mlxsw_sp_rt6 = mlxsw_sp_rt6_create(rt_arr[i]);
5569 if (IS_ERR(mlxsw_sp_rt6)) {
5570 err = PTR_ERR(mlxsw_sp_rt6);
5571 goto err_rt6_create;
5574 list_add_tail(&mlxsw_sp_rt6->list, &fib6_entry->rt6_list);
5578 err = mlxsw_sp_nexthop6_group_update(mlxsw_sp, op_ctx, fib6_entry);
5580 goto err_nexthop6_group_update;
5584 err_nexthop6_group_update:
5587 for (i--; i >= 0; i--) {
5589 mlxsw_sp_rt6 = list_last_entry(&fib6_entry->rt6_list,
5590 struct mlxsw_sp_rt6, list);
5591 list_del(&mlxsw_sp_rt6->list);
5592 mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
5598 mlxsw_sp_fib6_entry_nexthop_del(struct mlxsw_sp *mlxsw_sp,
5599 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5600 struct mlxsw_sp_fib6_entry *fib6_entry,
5601 struct fib6_info **rt_arr, unsigned int nrt6)
5603 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
5606 for (i = 0; i < nrt6; i++) {
5607 mlxsw_sp_rt6 = mlxsw_sp_fib6_entry_rt_find(fib6_entry,
5609 if (WARN_ON_ONCE(!mlxsw_sp_rt6))
5613 list_del(&mlxsw_sp_rt6->list);
5614 mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
5617 mlxsw_sp_nexthop6_group_update(mlxsw_sp, op_ctx, fib6_entry);
5620 static void mlxsw_sp_fib6_entry_type_set(struct mlxsw_sp *mlxsw_sp,
5621 struct mlxsw_sp_fib_entry *fib_entry,
5622 const struct fib6_info *rt)
5624 /* Packets hitting RTF_REJECT routes need to be discarded by the
5625 * stack. We can rely on their destination device not having a
5626 * RIF (it's the loopback device) and can thus use action type
5627 * local, which will cause them to be trapped with a lower
5628 * priority than packets that need to be locally received.
5630 if (rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
5631 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
5632 else if (rt->fib6_type == RTN_BLACKHOLE)
5633 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_BLACKHOLE;
5634 else if (rt->fib6_flags & RTF_REJECT)
5635 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_UNREACHABLE;
5636 else if (mlxsw_sp_rt6_is_gateway(mlxsw_sp, rt))
5637 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
5639 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
5643 mlxsw_sp_fib6_entry_rt_destroy_all(struct mlxsw_sp_fib6_entry *fib6_entry)
5645 struct mlxsw_sp_rt6 *mlxsw_sp_rt6, *tmp;
5647 list_for_each_entry_safe(mlxsw_sp_rt6, tmp, &fib6_entry->rt6_list,
5650 list_del(&mlxsw_sp_rt6->list);
5651 mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
5655 static struct mlxsw_sp_fib6_entry *
5656 mlxsw_sp_fib6_entry_create(struct mlxsw_sp *mlxsw_sp,
5657 struct mlxsw_sp_fib_node *fib_node,
5658 struct fib6_info **rt_arr, unsigned int nrt6)
5660 struct mlxsw_sp_fib6_entry *fib6_entry;
5661 struct mlxsw_sp_fib_entry *fib_entry;
5662 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
5665 fib6_entry = kzalloc(sizeof(*fib6_entry), GFP_KERNEL);
5667 return ERR_PTR(-ENOMEM);
5668 fib_entry = &fib6_entry->common;
5670 fib_entry->priv = mlxsw_sp_fib_entry_priv_create(fib_node->fib->ll_ops);
5671 if (IS_ERR(fib_entry->priv)) {
5672 err = PTR_ERR(fib_entry->priv);
5673 goto err_fib_entry_priv_create;
5676 INIT_LIST_HEAD(&fib6_entry->rt6_list);
5678 for (i = 0; i < nrt6; i++) {
5679 mlxsw_sp_rt6 = mlxsw_sp_rt6_create(rt_arr[i]);
5680 if (IS_ERR(mlxsw_sp_rt6)) {
5681 err = PTR_ERR(mlxsw_sp_rt6);
5682 goto err_rt6_create;
5684 list_add_tail(&mlxsw_sp_rt6->list, &fib6_entry->rt6_list);
5688 mlxsw_sp_fib6_entry_type_set(mlxsw_sp, fib_entry, rt_arr[0]);
5690 err = mlxsw_sp_nexthop6_group_get(mlxsw_sp, fib6_entry);
5692 goto err_nexthop6_group_get;
5694 fib_entry->fib_node = fib_node;
5698 err_nexthop6_group_get:
5701 for (i--; i >= 0; i--) {
5703 mlxsw_sp_rt6 = list_last_entry(&fib6_entry->rt6_list,
5704 struct mlxsw_sp_rt6, list);
5705 list_del(&mlxsw_sp_rt6->list);
5706 mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
5708 mlxsw_sp_fib_entry_priv_put(fib_entry->priv);
5709 err_fib_entry_priv_create:
5711 return ERR_PTR(err);
5714 static void mlxsw_sp_fib6_entry_destroy(struct mlxsw_sp *mlxsw_sp,
5715 struct mlxsw_sp_fib6_entry *fib6_entry)
5717 mlxsw_sp_nexthop6_group_put(mlxsw_sp, &fib6_entry->common);
5718 mlxsw_sp_fib6_entry_rt_destroy_all(fib6_entry);
5719 WARN_ON(fib6_entry->nrt6);
5720 mlxsw_sp_fib_entry_priv_put(fib6_entry->common.priv);
5724 static struct mlxsw_sp_fib6_entry *
5725 mlxsw_sp_fib6_entry_lookup(struct mlxsw_sp *mlxsw_sp,
5726 const struct fib6_info *rt)
5728 struct mlxsw_sp_fib6_entry *fib6_entry;
5729 struct mlxsw_sp_fib_node *fib_node;
5730 struct mlxsw_sp_fib *fib;
5731 struct fib6_info *cmp_rt;
5732 struct mlxsw_sp_vr *vr;
5734 vr = mlxsw_sp_vr_find(mlxsw_sp, rt->fib6_table->tb6_id);
5737 fib = mlxsw_sp_vr_fib(vr, MLXSW_SP_L3_PROTO_IPV6);
5739 fib_node = mlxsw_sp_fib_node_lookup(fib, &rt->fib6_dst.addr,
5740 sizeof(rt->fib6_dst.addr),
5745 fib6_entry = container_of(fib_node->fib_entry,
5746 struct mlxsw_sp_fib6_entry, common);
5747 cmp_rt = mlxsw_sp_fib6_entry_rt(fib6_entry);
5748 if (rt->fib6_table->tb6_id == cmp_rt->fib6_table->tb6_id &&
5749 rt->fib6_metric == cmp_rt->fib6_metric &&
5750 mlxsw_sp_fib6_entry_rt_find(fib6_entry, rt))
5756 static bool mlxsw_sp_fib6_allow_replace(struct mlxsw_sp_fib6_entry *fib6_entry)
5758 struct mlxsw_sp_fib_node *fib_node = fib6_entry->common.fib_node;
5759 struct mlxsw_sp_fib6_entry *fib6_replaced;
5760 struct fib6_info *rt, *rt_replaced;
5762 if (!fib_node->fib_entry)
5765 fib6_replaced = container_of(fib_node->fib_entry,
5766 struct mlxsw_sp_fib6_entry,
5768 rt = mlxsw_sp_fib6_entry_rt(fib6_entry);
5769 rt_replaced = mlxsw_sp_fib6_entry_rt(fib6_replaced);
5770 if (rt->fib6_table->tb6_id == RT_TABLE_MAIN &&
5771 rt_replaced->fib6_table->tb6_id == RT_TABLE_LOCAL)
5777 static int mlxsw_sp_router_fib6_replace(struct mlxsw_sp *mlxsw_sp,
5778 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5779 struct fib6_info **rt_arr, unsigned int nrt6)
5781 struct mlxsw_sp_fib6_entry *fib6_entry, *fib6_replaced;
5782 struct mlxsw_sp_fib_entry *replaced;
5783 struct mlxsw_sp_fib_node *fib_node;
5784 struct fib6_info *rt = rt_arr[0];
5787 if (mlxsw_sp->router->aborted)
5790 if (rt->fib6_src.plen)
5793 if (mlxsw_sp_fib6_rt_should_ignore(rt))
5796 fib_node = mlxsw_sp_fib_node_get(mlxsw_sp, rt->fib6_table->tb6_id,
5798 sizeof(rt->fib6_dst.addr),
5800 MLXSW_SP_L3_PROTO_IPV6);
5801 if (IS_ERR(fib_node))
5802 return PTR_ERR(fib_node);
5804 fib6_entry = mlxsw_sp_fib6_entry_create(mlxsw_sp, fib_node, rt_arr,
5806 if (IS_ERR(fib6_entry)) {
5807 err = PTR_ERR(fib6_entry);
5808 goto err_fib6_entry_create;
5811 if (!mlxsw_sp_fib6_allow_replace(fib6_entry)) {
5812 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
5813 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5817 replaced = fib_node->fib_entry;
5818 err = mlxsw_sp_fib_node_entry_link(mlxsw_sp, op_ctx, &fib6_entry->common);
5820 goto err_fib_node_entry_link;
5822 /* Nothing to replace */
5826 mlxsw_sp_fib_entry_hw_flags_clear(mlxsw_sp, replaced);
5827 fib6_replaced = container_of(replaced, struct mlxsw_sp_fib6_entry,
5829 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_replaced);
5833 err_fib_node_entry_link:
5834 fib_node->fib_entry = replaced;
5835 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
5836 err_fib6_entry_create:
5837 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5841 static int mlxsw_sp_router_fib6_append(struct mlxsw_sp *mlxsw_sp,
5842 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5843 struct fib6_info **rt_arr, unsigned int nrt6)
5845 struct mlxsw_sp_fib6_entry *fib6_entry;
5846 struct mlxsw_sp_fib_node *fib_node;
5847 struct fib6_info *rt = rt_arr[0];
5850 if (mlxsw_sp->router->aborted)
5853 if (rt->fib6_src.plen)
5856 if (mlxsw_sp_fib6_rt_should_ignore(rt))
5859 fib_node = mlxsw_sp_fib_node_get(mlxsw_sp, rt->fib6_table->tb6_id,
5861 sizeof(rt->fib6_dst.addr),
5863 MLXSW_SP_L3_PROTO_IPV6);
5864 if (IS_ERR(fib_node))
5865 return PTR_ERR(fib_node);
5867 if (WARN_ON_ONCE(!fib_node->fib_entry)) {
5868 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5872 fib6_entry = container_of(fib_node->fib_entry,
5873 struct mlxsw_sp_fib6_entry, common);
5874 err = mlxsw_sp_fib6_entry_nexthop_add(mlxsw_sp, op_ctx, fib6_entry, rt_arr, nrt6);
5876 goto err_fib6_entry_nexthop_add;
5880 err_fib6_entry_nexthop_add:
5881 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5885 static int mlxsw_sp_router_fib6_del(struct mlxsw_sp *mlxsw_sp,
5886 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5887 struct fib6_info **rt_arr, unsigned int nrt6)
5889 struct mlxsw_sp_fib6_entry *fib6_entry;
5890 struct mlxsw_sp_fib_node *fib_node;
5891 struct fib6_info *rt = rt_arr[0];
5894 if (mlxsw_sp->router->aborted)
5897 if (mlxsw_sp_fib6_rt_should_ignore(rt))
5900 /* Multipath routes are first added to the FIB trie and only then
5901 * notified. If we vetoed the addition, we will get a delete
5902 * notification for a route we do not have. Therefore, do not warn if
5903 * route was not found.
5905 fib6_entry = mlxsw_sp_fib6_entry_lookup(mlxsw_sp, rt);
5909 /* If not all the nexthops are deleted, then only reduce the nexthop
5912 if (nrt6 != fib6_entry->nrt6) {
5913 mlxsw_sp_fib6_entry_nexthop_del(mlxsw_sp, op_ctx, fib6_entry, rt_arr, nrt6);
5917 fib_node = fib6_entry->common.fib_node;
5919 err = __mlxsw_sp_fib_node_entry_unlink(mlxsw_sp, op_ctx, &fib6_entry->common);
5920 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
5921 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5925 static int __mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp,
5926 enum mlxsw_sp_l3proto proto,
5929 const struct mlxsw_sp_router_ll_ops *ll_ops = mlxsw_sp->router->proto_ll_ops[proto];
5930 enum mlxsw_reg_ralxx_protocol ralxx_proto =
5931 (enum mlxsw_reg_ralxx_protocol) proto;
5932 struct mlxsw_sp_fib_entry_priv *priv;
5933 char xralta_pl[MLXSW_REG_XRALTA_LEN];
5934 char xralst_pl[MLXSW_REG_XRALST_LEN];
5937 mlxsw_reg_xralta_pack(xralta_pl, true, ralxx_proto, tree_id);
5938 err = ll_ops->ralta_write(mlxsw_sp, xralta_pl);
5942 mlxsw_reg_xralst_pack(xralst_pl, 0xff, tree_id);
5943 err = ll_ops->ralst_write(mlxsw_sp, xralst_pl);
5947 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
5948 struct mlxsw_sp_fib_entry_op_ctx *op_ctx = mlxsw_sp->router->ll_op_ctx;
5949 struct mlxsw_sp_vr *vr = &mlxsw_sp->router->vrs[i];
5950 char xraltb_pl[MLXSW_REG_XRALTB_LEN];
5952 mlxsw_sp_fib_entry_op_ctx_clear(op_ctx);
5953 mlxsw_reg_xraltb_pack(xraltb_pl, vr->id, ralxx_proto, tree_id);
5954 err = ll_ops->raltb_write(mlxsw_sp, xraltb_pl);
5958 priv = mlxsw_sp_fib_entry_priv_create(ll_ops);
5960 return PTR_ERR(priv);
5962 ll_ops->fib_entry_pack(op_ctx, proto, MLXSW_SP_FIB_ENTRY_OP_WRITE,
5963 vr->id, 0, NULL, priv);
5964 ll_ops->fib_entry_act_ip2me_pack(op_ctx);
5965 err = ll_ops->fib_entry_commit(mlxsw_sp, op_ctx, NULL);
5966 mlxsw_sp_fib_entry_priv_put(priv);
5974 static struct mlxsw_sp_mr_table *
5975 mlxsw_sp_router_fibmr_family_to_table(struct mlxsw_sp_vr *vr, int family)
5977 if (family == RTNL_FAMILY_IPMR)
5978 return vr->mr_table[MLXSW_SP_L3_PROTO_IPV4];
5980 return vr->mr_table[MLXSW_SP_L3_PROTO_IPV6];
5983 static int mlxsw_sp_router_fibmr_add(struct mlxsw_sp *mlxsw_sp,
5984 struct mfc_entry_notifier_info *men_info,
5987 struct mlxsw_sp_mr_table *mrt;
5988 struct mlxsw_sp_vr *vr;
5990 if (mlxsw_sp->router->aborted)
5993 vr = mlxsw_sp_vr_get(mlxsw_sp, men_info->tb_id, NULL);
5997 mrt = mlxsw_sp_router_fibmr_family_to_table(vr, men_info->info.family);
5998 return mlxsw_sp_mr_route_add(mrt, men_info->mfc, replace);
6001 static void mlxsw_sp_router_fibmr_del(struct mlxsw_sp *mlxsw_sp,
6002 struct mfc_entry_notifier_info *men_info)
6004 struct mlxsw_sp_mr_table *mrt;
6005 struct mlxsw_sp_vr *vr;
6007 if (mlxsw_sp->router->aborted)
6010 vr = mlxsw_sp_vr_find(mlxsw_sp, men_info->tb_id);
6014 mrt = mlxsw_sp_router_fibmr_family_to_table(vr, men_info->info.family);
6015 mlxsw_sp_mr_route_del(mrt, men_info->mfc);
6016 mlxsw_sp_vr_put(mlxsw_sp, vr);
6020 mlxsw_sp_router_fibmr_vif_add(struct mlxsw_sp *mlxsw_sp,
6021 struct vif_entry_notifier_info *ven_info)
6023 struct mlxsw_sp_mr_table *mrt;
6024 struct mlxsw_sp_rif *rif;
6025 struct mlxsw_sp_vr *vr;
6027 if (mlxsw_sp->router->aborted)
6030 vr = mlxsw_sp_vr_get(mlxsw_sp, ven_info->tb_id, NULL);
6034 mrt = mlxsw_sp_router_fibmr_family_to_table(vr, ven_info->info.family);
6035 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, ven_info->dev);
6036 return mlxsw_sp_mr_vif_add(mrt, ven_info->dev,
6037 ven_info->vif_index,
6038 ven_info->vif_flags, rif);
6042 mlxsw_sp_router_fibmr_vif_del(struct mlxsw_sp *mlxsw_sp,
6043 struct vif_entry_notifier_info *ven_info)
6045 struct mlxsw_sp_mr_table *mrt;
6046 struct mlxsw_sp_vr *vr;
6048 if (mlxsw_sp->router->aborted)
6051 vr = mlxsw_sp_vr_find(mlxsw_sp, ven_info->tb_id);
6055 mrt = mlxsw_sp_router_fibmr_family_to_table(vr, ven_info->info.family);
6056 mlxsw_sp_mr_vif_del(mrt, ven_info->vif_index);
6057 mlxsw_sp_vr_put(mlxsw_sp, vr);
6060 static int mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp)
6062 enum mlxsw_sp_l3proto proto = MLXSW_SP_L3_PROTO_IPV4;
6065 err = __mlxsw_sp_router_set_abort_trap(mlxsw_sp, proto,
6066 MLXSW_SP_LPM_TREE_MIN);
6070 /* The multicast router code does not need an abort trap as by default,
6071 * packets that don't match any routes are trapped to the CPU.
6074 proto = MLXSW_SP_L3_PROTO_IPV6;
6075 return __mlxsw_sp_router_set_abort_trap(mlxsw_sp, proto,
6076 MLXSW_SP_LPM_TREE_MIN + 1);
6079 static void mlxsw_sp_fib4_node_flush(struct mlxsw_sp *mlxsw_sp,
6080 struct mlxsw_sp_fib_node *fib_node)
6082 struct mlxsw_sp_fib4_entry *fib4_entry;
6084 fib4_entry = container_of(fib_node->fib_entry,
6085 struct mlxsw_sp_fib4_entry, common);
6086 mlxsw_sp_fib_node_entry_unlink(mlxsw_sp, fib_node->fib_entry);
6087 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
6088 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
6091 static void mlxsw_sp_fib6_node_flush(struct mlxsw_sp *mlxsw_sp,
6092 struct mlxsw_sp_fib_node *fib_node)
6094 struct mlxsw_sp_fib6_entry *fib6_entry;
6096 fib6_entry = container_of(fib_node->fib_entry,
6097 struct mlxsw_sp_fib6_entry, common);
6098 mlxsw_sp_fib_node_entry_unlink(mlxsw_sp, fib_node->fib_entry);
6099 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
6100 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
6103 static void mlxsw_sp_fib_node_flush(struct mlxsw_sp *mlxsw_sp,
6104 struct mlxsw_sp_fib_node *fib_node)
6106 switch (fib_node->fib->proto) {
6107 case MLXSW_SP_L3_PROTO_IPV4:
6108 mlxsw_sp_fib4_node_flush(mlxsw_sp, fib_node);
6110 case MLXSW_SP_L3_PROTO_IPV6:
6111 mlxsw_sp_fib6_node_flush(mlxsw_sp, fib_node);
6116 static void mlxsw_sp_vr_fib_flush(struct mlxsw_sp *mlxsw_sp,
6117 struct mlxsw_sp_vr *vr,
6118 enum mlxsw_sp_l3proto proto)
6120 struct mlxsw_sp_fib *fib = mlxsw_sp_vr_fib(vr, proto);
6121 struct mlxsw_sp_fib_node *fib_node, *tmp;
6123 list_for_each_entry_safe(fib_node, tmp, &fib->node_list, list) {
6124 bool do_break = &tmp->list == &fib->node_list;
6126 mlxsw_sp_fib_node_flush(mlxsw_sp, fib_node);
6132 static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp)
6136 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
6137 struct mlxsw_sp_vr *vr = &mlxsw_sp->router->vrs[i];
6139 if (!mlxsw_sp_vr_is_used(vr))
6142 for (j = 0; j < MLXSW_SP_L3_PROTO_MAX; j++)
6143 mlxsw_sp_mr_table_flush(vr->mr_table[j]);
6144 mlxsw_sp_vr_fib_flush(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV4);
6146 /* If virtual router was only used for IPv4, then it's no
6149 if (!mlxsw_sp_vr_is_used(vr))
6151 mlxsw_sp_vr_fib_flush(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV6);
6154 /* After flushing all the routes, it is not possible anyone is still
6155 * using the adjacency index that is discarding packets, so free it in
6156 * case it was allocated.
6158 if (!mlxsw_sp->router->adj_discard_index_valid)
6160 mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ, 1,
6161 mlxsw_sp->router->adj_discard_index);
6162 mlxsw_sp->router->adj_discard_index_valid = false;
6165 static void mlxsw_sp_router_fib_abort(struct mlxsw_sp *mlxsw_sp)
6169 if (mlxsw_sp->router->aborted)
6171 dev_warn(mlxsw_sp->bus_info->dev, "FIB abort triggered. Note that FIB entries are no longer being offloaded to this device.\n");
6172 mlxsw_sp_router_fib_flush(mlxsw_sp);
6173 mlxsw_sp->router->aborted = true;
6174 err = mlxsw_sp_router_set_abort_trap(mlxsw_sp);
6176 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set abort trap.\n");
6179 struct mlxsw_sp_fib6_event {
6180 struct fib6_info **rt_arr;
6184 struct mlxsw_sp_fib_event {
6185 struct list_head list; /* node in fib queue */
6187 struct mlxsw_sp_fib6_event fib6_event;
6188 struct fib_entry_notifier_info fen_info;
6189 struct fib_rule_notifier_info fr_info;
6190 struct fib_nh_notifier_info fnh_info;
6191 struct mfc_entry_notifier_info men_info;
6192 struct vif_entry_notifier_info ven_info;
6194 struct mlxsw_sp *mlxsw_sp;
6195 unsigned long event;
6200 mlxsw_sp_router_fib6_event_init(struct mlxsw_sp_fib6_event *fib6_event,
6201 struct fib6_entry_notifier_info *fen6_info)
6203 struct fib6_info *rt = fen6_info->rt;
6204 struct fib6_info **rt_arr;
6205 struct fib6_info *iter;
6209 nrt6 = fen6_info->nsiblings + 1;
6211 rt_arr = kcalloc(nrt6, sizeof(struct fib6_info *), GFP_ATOMIC);
6215 fib6_event->rt_arr = rt_arr;
6216 fib6_event->nrt6 = nrt6;
6221 if (!fen6_info->nsiblings)
6224 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
6225 if (i == fen6_info->nsiblings)
6228 rt_arr[i + 1] = iter;
6229 fib6_info_hold(iter);
6232 WARN_ON_ONCE(i != fen6_info->nsiblings);
6238 mlxsw_sp_router_fib6_event_fini(struct mlxsw_sp_fib6_event *fib6_event)
6242 for (i = 0; i < fib6_event->nrt6; i++)
6243 mlxsw_sp_rt6_release(fib6_event->rt_arr[i]);
6244 kfree(fib6_event->rt_arr);
6247 static void mlxsw_sp_router_fib4_event_process(struct mlxsw_sp *mlxsw_sp,
6248 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
6249 struct mlxsw_sp_fib_event *fib_event)
6253 mlxsw_sp_span_respin(mlxsw_sp);
6255 switch (fib_event->event) {
6256 case FIB_EVENT_ENTRY_REPLACE:
6257 err = mlxsw_sp_router_fib4_replace(mlxsw_sp, op_ctx, &fib_event->fen_info);
6259 mlxsw_sp_fib_entry_op_ctx_priv_put_all(op_ctx);
6260 mlxsw_sp_router_fib_abort(mlxsw_sp);
6262 fib_info_put(fib_event->fen_info.fi);
6264 case FIB_EVENT_ENTRY_DEL:
6265 err = mlxsw_sp_router_fib4_del(mlxsw_sp, op_ctx, &fib_event->fen_info);
6267 mlxsw_sp_fib_entry_op_ctx_priv_put_all(op_ctx);
6268 fib_info_put(fib_event->fen_info.fi);
6270 case FIB_EVENT_NH_ADD:
6271 case FIB_EVENT_NH_DEL:
6272 mlxsw_sp_nexthop4_event(mlxsw_sp, fib_event->event, fib_event->fnh_info.fib_nh);
6273 fib_info_put(fib_event->fnh_info.fib_nh->nh_parent);
6278 static void mlxsw_sp_router_fib6_event_process(struct mlxsw_sp *mlxsw_sp,
6279 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
6280 struct mlxsw_sp_fib_event *fib_event)
6284 mlxsw_sp_span_respin(mlxsw_sp);
6286 switch (fib_event->event) {
6287 case FIB_EVENT_ENTRY_REPLACE:
6288 err = mlxsw_sp_router_fib6_replace(mlxsw_sp, op_ctx, fib_event->fib6_event.rt_arr,
6289 fib_event->fib6_event.nrt6);
6291 mlxsw_sp_fib_entry_op_ctx_priv_put_all(op_ctx);
6292 mlxsw_sp_router_fib_abort(mlxsw_sp);
6294 mlxsw_sp_router_fib6_event_fini(&fib_event->fib6_event);
6296 case FIB_EVENT_ENTRY_APPEND:
6297 err = mlxsw_sp_router_fib6_append(mlxsw_sp, op_ctx, fib_event->fib6_event.rt_arr,
6298 fib_event->fib6_event.nrt6);
6300 mlxsw_sp_fib_entry_op_ctx_priv_put_all(op_ctx);
6301 mlxsw_sp_router_fib_abort(mlxsw_sp);
6303 mlxsw_sp_router_fib6_event_fini(&fib_event->fib6_event);
6305 case FIB_EVENT_ENTRY_DEL:
6306 err = mlxsw_sp_router_fib6_del(mlxsw_sp, op_ctx, fib_event->fib6_event.rt_arr,
6307 fib_event->fib6_event.nrt6);
6309 mlxsw_sp_fib_entry_op_ctx_priv_put_all(op_ctx);
6310 mlxsw_sp_router_fib6_event_fini(&fib_event->fib6_event);
6315 static void mlxsw_sp_router_fibmr_event_process(struct mlxsw_sp *mlxsw_sp,
6316 struct mlxsw_sp_fib_event *fib_event)
6322 mutex_lock(&mlxsw_sp->router->lock);
6323 switch (fib_event->event) {
6324 case FIB_EVENT_ENTRY_REPLACE:
6325 case FIB_EVENT_ENTRY_ADD:
6326 replace = fib_event->event == FIB_EVENT_ENTRY_REPLACE;
6328 err = mlxsw_sp_router_fibmr_add(mlxsw_sp, &fib_event->men_info, replace);
6330 mlxsw_sp_router_fib_abort(mlxsw_sp);
6331 mr_cache_put(fib_event->men_info.mfc);
6333 case FIB_EVENT_ENTRY_DEL:
6334 mlxsw_sp_router_fibmr_del(mlxsw_sp, &fib_event->men_info);
6335 mr_cache_put(fib_event->men_info.mfc);
6337 case FIB_EVENT_VIF_ADD:
6338 err = mlxsw_sp_router_fibmr_vif_add(mlxsw_sp,
6339 &fib_event->ven_info);
6341 mlxsw_sp_router_fib_abort(mlxsw_sp);
6342 dev_put(fib_event->ven_info.dev);
6344 case FIB_EVENT_VIF_DEL:
6345 mlxsw_sp_router_fibmr_vif_del(mlxsw_sp, &fib_event->ven_info);
6346 dev_put(fib_event->ven_info.dev);
6349 mutex_unlock(&mlxsw_sp->router->lock);
6353 static void mlxsw_sp_router_fib_event_work(struct work_struct *work)
6355 struct mlxsw_sp_router *router = container_of(work, struct mlxsw_sp_router, fib_event_work);
6356 struct mlxsw_sp_fib_entry_op_ctx *op_ctx = router->ll_op_ctx;
6357 struct mlxsw_sp *mlxsw_sp = router->mlxsw_sp;
6358 struct mlxsw_sp_fib_event *next_fib_event;
6359 struct mlxsw_sp_fib_event *fib_event;
6360 int last_family = AF_UNSPEC;
6361 LIST_HEAD(fib_event_queue);
6363 spin_lock_bh(&router->fib_event_queue_lock);
6364 list_splice_init(&router->fib_event_queue, &fib_event_queue);
6365 spin_unlock_bh(&router->fib_event_queue_lock);
6367 /* Router lock is held here to make sure per-instance
6368 * operation context is not used in between FIB4/6 events
6371 mutex_lock(&router->lock);
6372 mlxsw_sp_fib_entry_op_ctx_clear(op_ctx);
6373 list_for_each_entry_safe(fib_event, next_fib_event,
6374 &fib_event_queue, list) {
6375 /* Check if the next entry in the queue exists and it is
6376 * of the same type (family and event) as the currect one.
6377 * In that case it is permitted to do the bulking
6378 * of multiple FIB entries to a single register write.
6380 op_ctx->bulk_ok = !list_is_last(&fib_event->list, &fib_event_queue) &&
6381 fib_event->family == next_fib_event->family &&
6382 fib_event->event == next_fib_event->event;
6384 /* In case family of this and the previous entry are different, context
6385 * reinitialization is going to be needed now, indicate that.
6386 * Note that since last_family is initialized to AF_UNSPEC, this is always
6387 * going to happen for the first entry processed in the work.
6389 if (fib_event->family != last_family)
6390 op_ctx->initialized = false;
6392 switch (fib_event->family) {
6394 mlxsw_sp_router_fib4_event_process(mlxsw_sp, op_ctx,
6398 mlxsw_sp_router_fib6_event_process(mlxsw_sp, op_ctx,
6401 case RTNL_FAMILY_IP6MR:
6402 case RTNL_FAMILY_IPMR:
6403 /* Unlock here as inside FIBMR the lock is taken again
6404 * under RTNL. The per-instance operation context
6405 * is not used by FIBMR.
6407 mutex_unlock(&router->lock);
6408 mlxsw_sp_router_fibmr_event_process(mlxsw_sp,
6410 mutex_lock(&router->lock);
6415 last_family = fib_event->family;
6419 WARN_ON_ONCE(!list_empty(&router->ll_op_ctx->fib_entry_priv_list));
6420 mutex_unlock(&router->lock);
6423 static void mlxsw_sp_router_fib4_event(struct mlxsw_sp_fib_event *fib_event,
6424 struct fib_notifier_info *info)
6426 struct fib_entry_notifier_info *fen_info;
6427 struct fib_nh_notifier_info *fnh_info;
6429 switch (fib_event->event) {
6430 case FIB_EVENT_ENTRY_REPLACE:
6431 case FIB_EVENT_ENTRY_DEL:
6432 fen_info = container_of(info, struct fib_entry_notifier_info,
6434 fib_event->fen_info = *fen_info;
6435 /* Take reference on fib_info to prevent it from being
6436 * freed while event is queued. Release it afterwards.
6438 fib_info_hold(fib_event->fen_info.fi);
6440 case FIB_EVENT_NH_ADD:
6441 case FIB_EVENT_NH_DEL:
6442 fnh_info = container_of(info, struct fib_nh_notifier_info,
6444 fib_event->fnh_info = *fnh_info;
6445 fib_info_hold(fib_event->fnh_info.fib_nh->nh_parent);
6450 static int mlxsw_sp_router_fib6_event(struct mlxsw_sp_fib_event *fib_event,
6451 struct fib_notifier_info *info)
6453 struct fib6_entry_notifier_info *fen6_info;
6456 switch (fib_event->event) {
6457 case FIB_EVENT_ENTRY_REPLACE:
6458 case FIB_EVENT_ENTRY_APPEND:
6459 case FIB_EVENT_ENTRY_DEL:
6460 fen6_info = container_of(info, struct fib6_entry_notifier_info,
6462 err = mlxsw_sp_router_fib6_event_init(&fib_event->fib6_event,
6473 mlxsw_sp_router_fibmr_event(struct mlxsw_sp_fib_event *fib_event,
6474 struct fib_notifier_info *info)
6476 switch (fib_event->event) {
6477 case FIB_EVENT_ENTRY_REPLACE:
6478 case FIB_EVENT_ENTRY_ADD:
6479 case FIB_EVENT_ENTRY_DEL:
6480 memcpy(&fib_event->men_info, info, sizeof(fib_event->men_info));
6481 mr_cache_hold(fib_event->men_info.mfc);
6483 case FIB_EVENT_VIF_ADD:
6484 case FIB_EVENT_VIF_DEL:
6485 memcpy(&fib_event->ven_info, info, sizeof(fib_event->ven_info));
6486 dev_hold(fib_event->ven_info.dev);
6491 static int mlxsw_sp_router_fib_rule_event(unsigned long event,
6492 struct fib_notifier_info *info,
6493 struct mlxsw_sp *mlxsw_sp)
6495 struct netlink_ext_ack *extack = info->extack;
6496 struct fib_rule_notifier_info *fr_info;
6497 struct fib_rule *rule;
6500 /* nothing to do at the moment */
6501 if (event == FIB_EVENT_RULE_DEL)
6504 if (mlxsw_sp->router->aborted)
6507 fr_info = container_of(info, struct fib_rule_notifier_info, info);
6508 rule = fr_info->rule;
6510 /* Rule only affects locally generated traffic */
6511 if (rule->iifindex == mlxsw_sp_net(mlxsw_sp)->loopback_dev->ifindex)
6514 switch (info->family) {
6516 if (!fib4_rule_default(rule) && !rule->l3mdev)
6520 if (!fib6_rule_default(rule) && !rule->l3mdev)
6523 case RTNL_FAMILY_IPMR:
6524 if (!ipmr_rule_default(rule) && !rule->l3mdev)
6527 case RTNL_FAMILY_IP6MR:
6528 if (!ip6mr_rule_default(rule) && !rule->l3mdev)
6534 NL_SET_ERR_MSG_MOD(extack, "FIB rules not supported");
6539 /* Called with rcu_read_lock() */
6540 static int mlxsw_sp_router_fib_event(struct notifier_block *nb,
6541 unsigned long event, void *ptr)
6543 struct mlxsw_sp_fib_event *fib_event;
6544 struct fib_notifier_info *info = ptr;
6545 struct mlxsw_sp_router *router;
6548 if ((info->family != AF_INET && info->family != AF_INET6 &&
6549 info->family != RTNL_FAMILY_IPMR &&
6550 info->family != RTNL_FAMILY_IP6MR))
6553 router = container_of(nb, struct mlxsw_sp_router, fib_nb);
6556 case FIB_EVENT_RULE_ADD:
6557 case FIB_EVENT_RULE_DEL:
6558 err = mlxsw_sp_router_fib_rule_event(event, info,
6560 return notifier_from_errno(err);
6561 case FIB_EVENT_ENTRY_ADD:
6562 case FIB_EVENT_ENTRY_REPLACE:
6563 case FIB_EVENT_ENTRY_APPEND:
6564 if (router->aborted) {
6565 NL_SET_ERR_MSG_MOD(info->extack, "FIB offload was aborted. Not configuring route");
6566 return notifier_from_errno(-EINVAL);
6568 if (info->family == AF_INET) {
6569 struct fib_entry_notifier_info *fen_info = ptr;
6571 if (fen_info->fi->fib_nh_is_v6) {
6572 NL_SET_ERR_MSG_MOD(info->extack, "IPv6 gateway with IPv4 route is not supported");
6573 return notifier_from_errno(-EINVAL);
6575 if (fen_info->fi->nh) {
6576 NL_SET_ERR_MSG_MOD(info->extack, "IPv4 route with nexthop objects is not supported");
6577 return notifier_from_errno(-EINVAL);
6579 } else if (info->family == AF_INET6) {
6580 struct fib6_entry_notifier_info *fen6_info;
6582 fen6_info = container_of(info,
6583 struct fib6_entry_notifier_info,
6585 if (fen6_info->rt->nh) {
6586 NL_SET_ERR_MSG_MOD(info->extack, "IPv6 route with nexthop objects is not supported");
6587 return notifier_from_errno(-EINVAL);
6593 fib_event = kzalloc(sizeof(*fib_event), GFP_ATOMIC);
6597 fib_event->mlxsw_sp = router->mlxsw_sp;
6598 fib_event->event = event;
6599 fib_event->family = info->family;
6601 switch (info->family) {
6603 mlxsw_sp_router_fib4_event(fib_event, info);
6606 err = mlxsw_sp_router_fib6_event(fib_event, info);
6610 case RTNL_FAMILY_IP6MR:
6611 case RTNL_FAMILY_IPMR:
6612 mlxsw_sp_router_fibmr_event(fib_event, info);
6616 /* Enqueue the event and trigger the work */
6617 spin_lock_bh(&router->fib_event_queue_lock);
6618 list_add_tail(&fib_event->list, &router->fib_event_queue);
6619 spin_unlock_bh(&router->fib_event_queue_lock);
6620 mlxsw_core_schedule_work(&router->fib_event_work);
6629 static struct mlxsw_sp_rif *
6630 mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
6631 const struct net_device *dev)
6635 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
6636 if (mlxsw_sp->router->rifs[i] &&
6637 mlxsw_sp->router->rifs[i]->dev == dev)
6638 return mlxsw_sp->router->rifs[i];
6643 bool mlxsw_sp_rif_exists(struct mlxsw_sp *mlxsw_sp,
6644 const struct net_device *dev)
6646 struct mlxsw_sp_rif *rif;
6648 mutex_lock(&mlxsw_sp->router->lock);
6649 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
6650 mutex_unlock(&mlxsw_sp->router->lock);
6655 u16 mlxsw_sp_rif_vid(struct mlxsw_sp *mlxsw_sp, const struct net_device *dev)
6657 struct mlxsw_sp_rif *rif;
6660 mutex_lock(&mlxsw_sp->router->lock);
6661 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
6665 /* We only return the VID for VLAN RIFs. Otherwise we return an
6666 * invalid value (0).
6668 if (rif->ops->type != MLXSW_SP_RIF_TYPE_VLAN)
6671 vid = mlxsw_sp_fid_8021q_vid(rif->fid);
6674 mutex_unlock(&mlxsw_sp->router->lock);
6678 static int mlxsw_sp_router_rif_disable(struct mlxsw_sp *mlxsw_sp, u16 rif)
6680 char ritr_pl[MLXSW_REG_RITR_LEN];
6683 mlxsw_reg_ritr_rif_pack(ritr_pl, rif);
6684 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
6688 mlxsw_reg_ritr_enable_set(ritr_pl, false);
6689 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
6692 static void mlxsw_sp_router_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
6693 struct mlxsw_sp_rif *rif)
6695 mlxsw_sp_router_rif_disable(mlxsw_sp, rif->rif_index);
6696 mlxsw_sp_nexthop_rif_gone_sync(mlxsw_sp, rif);
6697 mlxsw_sp_neigh_rif_gone_sync(mlxsw_sp, rif);
6701 mlxsw_sp_rif_should_config(struct mlxsw_sp_rif *rif, struct net_device *dev,
6702 unsigned long event)
6704 struct inet6_dev *inet6_dev;
6705 bool addr_list_empty = true;
6706 struct in_device *idev;
6713 idev = __in_dev_get_rcu(dev);
6714 if (idev && idev->ifa_list)
6715 addr_list_empty = false;
6717 inet6_dev = __in6_dev_get(dev);
6718 if (addr_list_empty && inet6_dev &&
6719 !list_empty(&inet6_dev->addr_list))
6720 addr_list_empty = false;
6723 /* macvlans do not have a RIF, but rather piggy back on the
6724 * RIF of their lower device.
6726 if (netif_is_macvlan(dev) && addr_list_empty)
6729 if (rif && addr_list_empty &&
6730 !netif_is_l3_slave(rif->dev))
6732 /* It is possible we already removed the RIF ourselves
6733 * if it was assigned to a netdev that is now a bridge
6742 static enum mlxsw_sp_rif_type
6743 mlxsw_sp_dev_rif_type(const struct mlxsw_sp *mlxsw_sp,
6744 const struct net_device *dev)
6746 enum mlxsw_sp_fid_type type;
6748 if (mlxsw_sp_netdev_ipip_type(mlxsw_sp, dev, NULL))
6749 return MLXSW_SP_RIF_TYPE_IPIP_LB;
6751 /* Otherwise RIF type is derived from the type of the underlying FID. */
6752 if (is_vlan_dev(dev) && netif_is_bridge_master(vlan_dev_real_dev(dev)))
6753 type = MLXSW_SP_FID_TYPE_8021Q;
6754 else if (netif_is_bridge_master(dev) && br_vlan_enabled(dev))
6755 type = MLXSW_SP_FID_TYPE_8021Q;
6756 else if (netif_is_bridge_master(dev))
6757 type = MLXSW_SP_FID_TYPE_8021D;
6759 type = MLXSW_SP_FID_TYPE_RFID;
6761 return mlxsw_sp_fid_type_rif_type(mlxsw_sp, type);
6764 static int mlxsw_sp_rif_index_alloc(struct mlxsw_sp *mlxsw_sp, u16 *p_rif_index)
6768 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++) {
6769 if (!mlxsw_sp->router->rifs[i]) {
6778 static struct mlxsw_sp_rif *mlxsw_sp_rif_alloc(size_t rif_size, u16 rif_index,
6780 struct net_device *l3_dev)
6782 struct mlxsw_sp_rif *rif;
6784 rif = kzalloc(rif_size, GFP_KERNEL);
6788 INIT_LIST_HEAD(&rif->nexthop_list);
6789 INIT_LIST_HEAD(&rif->neigh_list);
6791 ether_addr_copy(rif->addr, l3_dev->dev_addr);
6792 rif->mtu = l3_dev->mtu;
6796 rif->rif_index = rif_index;
6801 struct mlxsw_sp_rif *mlxsw_sp_rif_by_index(const struct mlxsw_sp *mlxsw_sp,
6804 return mlxsw_sp->router->rifs[rif_index];
6807 u16 mlxsw_sp_rif_index(const struct mlxsw_sp_rif *rif)
6809 return rif->rif_index;
6812 u16 mlxsw_sp_ipip_lb_rif_index(const struct mlxsw_sp_rif_ipip_lb *lb_rif)
6814 return lb_rif->common.rif_index;
6817 u16 mlxsw_sp_ipip_lb_ul_vr_id(const struct mlxsw_sp_rif_ipip_lb *lb_rif)
6819 u32 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(lb_rif->common.dev);
6820 struct mlxsw_sp_vr *ul_vr;
6822 ul_vr = mlxsw_sp_vr_get(lb_rif->common.mlxsw_sp, ul_tb_id, NULL);
6823 if (WARN_ON(IS_ERR(ul_vr)))
6829 u16 mlxsw_sp_ipip_lb_ul_rif_id(const struct mlxsw_sp_rif_ipip_lb *lb_rif)
6831 return lb_rif->ul_rif_id;
6834 int mlxsw_sp_rif_dev_ifindex(const struct mlxsw_sp_rif *rif)
6836 return rif->dev->ifindex;
6839 const struct net_device *mlxsw_sp_rif_dev(const struct mlxsw_sp_rif *rif)
6844 static struct mlxsw_sp_rif *
6845 mlxsw_sp_rif_create(struct mlxsw_sp *mlxsw_sp,
6846 const struct mlxsw_sp_rif_params *params,
6847 struct netlink_ext_ack *extack)
6849 u32 tb_id = l3mdev_fib_table(params->dev);
6850 const struct mlxsw_sp_rif_ops *ops;
6851 struct mlxsw_sp_fid *fid = NULL;
6852 enum mlxsw_sp_rif_type type;
6853 struct mlxsw_sp_rif *rif;
6854 struct mlxsw_sp_vr *vr;
6858 type = mlxsw_sp_dev_rif_type(mlxsw_sp, params->dev);
6859 ops = mlxsw_sp->rif_ops_arr[type];
6861 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id ? : RT_TABLE_MAIN, extack);
6863 return ERR_CAST(vr);
6866 err = mlxsw_sp_rif_index_alloc(mlxsw_sp, &rif_index);
6868 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported router interfaces");
6869 goto err_rif_index_alloc;
6872 rif = mlxsw_sp_rif_alloc(ops->rif_size, rif_index, vr->id, params->dev);
6878 mlxsw_sp->router->rifs[rif_index] = rif;
6879 rif->mlxsw_sp = mlxsw_sp;
6883 fid = ops->fid_get(rif, extack);
6892 ops->setup(rif, params);
6894 err = ops->configure(rif);
6898 for (i = 0; i < MLXSW_SP_L3_PROTO_MAX; i++) {
6899 err = mlxsw_sp_mr_rif_add(vr->mr_table[i], rif);
6901 goto err_mr_rif_add;
6904 mlxsw_sp_rif_counters_alloc(rif);
6909 for (i--; i >= 0; i--)
6910 mlxsw_sp_mr_rif_del(vr->mr_table[i], rif);
6911 ops->deconfigure(rif);
6914 mlxsw_sp_fid_put(fid);
6916 mlxsw_sp->router->rifs[rif_index] = NULL;
6920 err_rif_index_alloc:
6922 mlxsw_sp_vr_put(mlxsw_sp, vr);
6923 return ERR_PTR(err);
6926 static void mlxsw_sp_rif_destroy(struct mlxsw_sp_rif *rif)
6928 const struct mlxsw_sp_rif_ops *ops = rif->ops;
6929 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6930 struct mlxsw_sp_fid *fid = rif->fid;
6931 struct mlxsw_sp_vr *vr;
6934 mlxsw_sp_router_rif_gone_sync(mlxsw_sp, rif);
6935 vr = &mlxsw_sp->router->vrs[rif->vr_id];
6937 mlxsw_sp_rif_counters_free(rif);
6938 for (i = 0; i < MLXSW_SP_L3_PROTO_MAX; i++)
6939 mlxsw_sp_mr_rif_del(vr->mr_table[i], rif);
6940 ops->deconfigure(rif);
6942 /* Loopback RIFs are not associated with a FID. */
6943 mlxsw_sp_fid_put(fid);
6944 mlxsw_sp->router->rifs[rif->rif_index] = NULL;
6948 mlxsw_sp_vr_put(mlxsw_sp, vr);
6951 void mlxsw_sp_rif_destroy_by_dev(struct mlxsw_sp *mlxsw_sp,
6952 struct net_device *dev)
6954 struct mlxsw_sp_rif *rif;
6956 mutex_lock(&mlxsw_sp->router->lock);
6957 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
6960 mlxsw_sp_rif_destroy(rif);
6962 mutex_unlock(&mlxsw_sp->router->lock);
6966 mlxsw_sp_rif_subport_params_init(struct mlxsw_sp_rif_params *params,
6967 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
6969 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
6971 params->vid = mlxsw_sp_port_vlan->vid;
6972 params->lag = mlxsw_sp_port->lagged;
6974 params->lag_id = mlxsw_sp_port->lag_id;
6976 params->system_port = mlxsw_sp_port->local_port;
6979 static struct mlxsw_sp_rif_subport *
6980 mlxsw_sp_rif_subport_rif(const struct mlxsw_sp_rif *rif)
6982 return container_of(rif, struct mlxsw_sp_rif_subport, common);
6985 static struct mlxsw_sp_rif *
6986 mlxsw_sp_rif_subport_get(struct mlxsw_sp *mlxsw_sp,
6987 const struct mlxsw_sp_rif_params *params,
6988 struct netlink_ext_ack *extack)
6990 struct mlxsw_sp_rif_subport *rif_subport;
6991 struct mlxsw_sp_rif *rif;
6993 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, params->dev);
6995 return mlxsw_sp_rif_create(mlxsw_sp, params, extack);
6997 rif_subport = mlxsw_sp_rif_subport_rif(rif);
6998 refcount_inc(&rif_subport->ref_count);
7002 static void mlxsw_sp_rif_subport_put(struct mlxsw_sp_rif *rif)
7004 struct mlxsw_sp_rif_subport *rif_subport;
7006 rif_subport = mlxsw_sp_rif_subport_rif(rif);
7007 if (!refcount_dec_and_test(&rif_subport->ref_count))
7010 mlxsw_sp_rif_destroy(rif);
7014 mlxsw_sp_port_vlan_router_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
7015 struct net_device *l3_dev,
7016 struct netlink_ext_ack *extack)
7018 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
7019 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
7020 struct mlxsw_sp_rif_params params = {
7023 u16 vid = mlxsw_sp_port_vlan->vid;
7024 struct mlxsw_sp_rif *rif;
7025 struct mlxsw_sp_fid *fid;
7028 mlxsw_sp_rif_subport_params_init(¶ms, mlxsw_sp_port_vlan);
7029 rif = mlxsw_sp_rif_subport_get(mlxsw_sp, ¶ms, extack);
7031 return PTR_ERR(rif);
7033 /* FID was already created, just take a reference */
7034 fid = rif->ops->fid_get(rif, extack);
7035 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
7037 goto err_fid_port_vid_map;
7039 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
7041 goto err_port_vid_learning_set;
7043 err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
7044 BR_STATE_FORWARDING);
7046 goto err_port_vid_stp_set;
7048 mlxsw_sp_port_vlan->fid = fid;
7052 err_port_vid_stp_set:
7053 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
7054 err_port_vid_learning_set:
7055 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
7056 err_fid_port_vid_map:
7057 mlxsw_sp_fid_put(fid);
7058 mlxsw_sp_rif_subport_put(rif);
7063 __mlxsw_sp_port_vlan_router_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
7065 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
7066 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
7067 struct mlxsw_sp_rif *rif = mlxsw_sp_fid_rif(fid);
7068 u16 vid = mlxsw_sp_port_vlan->vid;
7070 if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_RFID))
7073 mlxsw_sp_port_vlan->fid = NULL;
7074 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_BLOCKING);
7075 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
7076 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
7077 mlxsw_sp_fid_put(fid);
7078 mlxsw_sp_rif_subport_put(rif);
7082 mlxsw_sp_port_vlan_router_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
7084 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port_vlan->mlxsw_sp_port->mlxsw_sp;
7086 mutex_lock(&mlxsw_sp->router->lock);
7087 __mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
7088 mutex_unlock(&mlxsw_sp->router->lock);
7091 static int mlxsw_sp_inetaddr_port_vlan_event(struct net_device *l3_dev,
7092 struct net_device *port_dev,
7093 unsigned long event, u16 vid,
7094 struct netlink_ext_ack *extack)
7096 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(port_dev);
7097 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
7099 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
7100 if (WARN_ON(!mlxsw_sp_port_vlan))
7105 return mlxsw_sp_port_vlan_router_join(mlxsw_sp_port_vlan,
7108 __mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
7115 static int mlxsw_sp_inetaddr_port_event(struct net_device *port_dev,
7116 unsigned long event,
7117 struct netlink_ext_ack *extack)
7119 if (netif_is_bridge_port(port_dev) ||
7120 netif_is_lag_port(port_dev) ||
7121 netif_is_ovs_port(port_dev))
7124 return mlxsw_sp_inetaddr_port_vlan_event(port_dev, port_dev, event,
7125 MLXSW_SP_DEFAULT_VID, extack);
7128 static int __mlxsw_sp_inetaddr_lag_event(struct net_device *l3_dev,
7129 struct net_device *lag_dev,
7130 unsigned long event, u16 vid,
7131 struct netlink_ext_ack *extack)
7133 struct net_device *port_dev;
7134 struct list_head *iter;
7137 netdev_for_each_lower_dev(lag_dev, port_dev, iter) {
7138 if (mlxsw_sp_port_dev_check(port_dev)) {
7139 err = mlxsw_sp_inetaddr_port_vlan_event(l3_dev,
7151 static int mlxsw_sp_inetaddr_lag_event(struct net_device *lag_dev,
7152 unsigned long event,
7153 struct netlink_ext_ack *extack)
7155 if (netif_is_bridge_port(lag_dev))
7158 return __mlxsw_sp_inetaddr_lag_event(lag_dev, lag_dev, event,
7159 MLXSW_SP_DEFAULT_VID, extack);
7162 static int mlxsw_sp_inetaddr_bridge_event(struct mlxsw_sp *mlxsw_sp,
7163 struct net_device *l3_dev,
7164 unsigned long event,
7165 struct netlink_ext_ack *extack)
7167 struct mlxsw_sp_rif_params params = {
7170 struct mlxsw_sp_rif *rif;
7174 rif = mlxsw_sp_rif_create(mlxsw_sp, ¶ms, extack);
7176 return PTR_ERR(rif);
7179 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
7180 mlxsw_sp_rif_destroy(rif);
7187 static int mlxsw_sp_inetaddr_vlan_event(struct mlxsw_sp *mlxsw_sp,
7188 struct net_device *vlan_dev,
7189 unsigned long event,
7190 struct netlink_ext_ack *extack)
7192 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
7193 u16 vid = vlan_dev_vlan_id(vlan_dev);
7195 if (netif_is_bridge_port(vlan_dev))
7198 if (mlxsw_sp_port_dev_check(real_dev))
7199 return mlxsw_sp_inetaddr_port_vlan_event(vlan_dev, real_dev,
7200 event, vid, extack);
7201 else if (netif_is_lag_master(real_dev))
7202 return __mlxsw_sp_inetaddr_lag_event(vlan_dev, real_dev, event,
7204 else if (netif_is_bridge_master(real_dev) && br_vlan_enabled(real_dev))
7205 return mlxsw_sp_inetaddr_bridge_event(mlxsw_sp, vlan_dev, event,
7211 static bool mlxsw_sp_rif_macvlan_is_vrrp4(const u8 *mac)
7213 u8 vrrp4[ETH_ALEN] = { 0x00, 0x00, 0x5e, 0x00, 0x01, 0x00 };
7214 u8 mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
7216 return ether_addr_equal_masked(mac, vrrp4, mask);
7219 static bool mlxsw_sp_rif_macvlan_is_vrrp6(const u8 *mac)
7221 u8 vrrp6[ETH_ALEN] = { 0x00, 0x00, 0x5e, 0x00, 0x02, 0x00 };
7222 u8 mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
7224 return ether_addr_equal_masked(mac, vrrp6, mask);
7227 static int mlxsw_sp_rif_vrrp_op(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
7228 const u8 *mac, bool adding)
7230 char ritr_pl[MLXSW_REG_RITR_LEN];
7231 u8 vrrp_id = adding ? mac[5] : 0;
7234 if (!mlxsw_sp_rif_macvlan_is_vrrp4(mac) &&
7235 !mlxsw_sp_rif_macvlan_is_vrrp6(mac))
7238 mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
7239 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
7243 if (mlxsw_sp_rif_macvlan_is_vrrp4(mac))
7244 mlxsw_reg_ritr_if_vrrp_id_ipv4_set(ritr_pl, vrrp_id);
7246 mlxsw_reg_ritr_if_vrrp_id_ipv6_set(ritr_pl, vrrp_id);
7248 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
7251 static int mlxsw_sp_rif_macvlan_add(struct mlxsw_sp *mlxsw_sp,
7252 const struct net_device *macvlan_dev,
7253 struct netlink_ext_ack *extack)
7255 struct macvlan_dev *vlan = netdev_priv(macvlan_dev);
7256 struct mlxsw_sp_rif *rif;
7259 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, vlan->lowerdev);
7261 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
7265 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, macvlan_dev->dev_addr,
7266 mlxsw_sp_fid_index(rif->fid), true);
7270 err = mlxsw_sp_rif_vrrp_op(mlxsw_sp, rif->rif_index,
7271 macvlan_dev->dev_addr, true);
7273 goto err_rif_vrrp_add;
7275 /* Make sure the bridge driver does not have this MAC pointing at
7278 if (rif->ops->fdb_del)
7279 rif->ops->fdb_del(rif, macvlan_dev->dev_addr);
7284 mlxsw_sp_rif_fdb_op(mlxsw_sp, macvlan_dev->dev_addr,
7285 mlxsw_sp_fid_index(rif->fid), false);
7289 static void __mlxsw_sp_rif_macvlan_del(struct mlxsw_sp *mlxsw_sp,
7290 const struct net_device *macvlan_dev)
7292 struct macvlan_dev *vlan = netdev_priv(macvlan_dev);
7293 struct mlxsw_sp_rif *rif;
7295 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, vlan->lowerdev);
7296 /* If we do not have a RIF, then we already took care of
7297 * removing the macvlan's MAC during RIF deletion.
7301 mlxsw_sp_rif_vrrp_op(mlxsw_sp, rif->rif_index, macvlan_dev->dev_addr,
7303 mlxsw_sp_rif_fdb_op(mlxsw_sp, macvlan_dev->dev_addr,
7304 mlxsw_sp_fid_index(rif->fid), false);
7307 void mlxsw_sp_rif_macvlan_del(struct mlxsw_sp *mlxsw_sp,
7308 const struct net_device *macvlan_dev)
7310 mutex_lock(&mlxsw_sp->router->lock);
7311 __mlxsw_sp_rif_macvlan_del(mlxsw_sp, macvlan_dev);
7312 mutex_unlock(&mlxsw_sp->router->lock);
7315 static int mlxsw_sp_inetaddr_macvlan_event(struct mlxsw_sp *mlxsw_sp,
7316 struct net_device *macvlan_dev,
7317 unsigned long event,
7318 struct netlink_ext_ack *extack)
7322 return mlxsw_sp_rif_macvlan_add(mlxsw_sp, macvlan_dev, extack);
7324 __mlxsw_sp_rif_macvlan_del(mlxsw_sp, macvlan_dev);
7331 static int mlxsw_sp_router_port_check_rif_addr(struct mlxsw_sp *mlxsw_sp,
7332 struct net_device *dev,
7333 const unsigned char *dev_addr,
7334 struct netlink_ext_ack *extack)
7336 struct mlxsw_sp_rif *rif;
7339 /* A RIF is not created for macvlan netdevs. Their MAC is used to
7342 if (netif_is_macvlan(dev) || netif_is_l3_master(dev))
7345 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++) {
7346 rif = mlxsw_sp->router->rifs[i];
7347 if (rif && rif->ops &&
7348 rif->ops->type == MLXSW_SP_RIF_TYPE_IPIP_LB)
7350 if (rif && rif->dev && rif->dev != dev &&
7351 !ether_addr_equal_masked(rif->dev->dev_addr, dev_addr,
7352 mlxsw_sp->mac_mask)) {
7353 NL_SET_ERR_MSG_MOD(extack, "All router interface MAC addresses must have the same prefix");
7361 static int __mlxsw_sp_inetaddr_event(struct mlxsw_sp *mlxsw_sp,
7362 struct net_device *dev,
7363 unsigned long event,
7364 struct netlink_ext_ack *extack)
7366 if (mlxsw_sp_port_dev_check(dev))
7367 return mlxsw_sp_inetaddr_port_event(dev, event, extack);
7368 else if (netif_is_lag_master(dev))
7369 return mlxsw_sp_inetaddr_lag_event(dev, event, extack);
7370 else if (netif_is_bridge_master(dev))
7371 return mlxsw_sp_inetaddr_bridge_event(mlxsw_sp, dev, event,
7373 else if (is_vlan_dev(dev))
7374 return mlxsw_sp_inetaddr_vlan_event(mlxsw_sp, dev, event,
7376 else if (netif_is_macvlan(dev))
7377 return mlxsw_sp_inetaddr_macvlan_event(mlxsw_sp, dev, event,
7383 static int mlxsw_sp_inetaddr_event(struct notifier_block *nb,
7384 unsigned long event, void *ptr)
7386 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
7387 struct net_device *dev = ifa->ifa_dev->dev;
7388 struct mlxsw_sp_router *router;
7389 struct mlxsw_sp_rif *rif;
7392 /* NETDEV_UP event is handled by mlxsw_sp_inetaddr_valid_event */
7393 if (event == NETDEV_UP)
7396 router = container_of(nb, struct mlxsw_sp_router, inetaddr_nb);
7397 mutex_lock(&router->lock);
7398 rif = mlxsw_sp_rif_find_by_dev(router->mlxsw_sp, dev);
7399 if (!mlxsw_sp_rif_should_config(rif, dev, event))
7402 err = __mlxsw_sp_inetaddr_event(router->mlxsw_sp, dev, event, NULL);
7404 mutex_unlock(&router->lock);
7405 return notifier_from_errno(err);
7408 int mlxsw_sp_inetaddr_valid_event(struct notifier_block *unused,
7409 unsigned long event, void *ptr)
7411 struct in_validator_info *ivi = (struct in_validator_info *) ptr;
7412 struct net_device *dev = ivi->ivi_dev->dev;
7413 struct mlxsw_sp *mlxsw_sp;
7414 struct mlxsw_sp_rif *rif;
7417 mlxsw_sp = mlxsw_sp_lower_get(dev);
7421 mutex_lock(&mlxsw_sp->router->lock);
7422 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
7423 if (!mlxsw_sp_rif_should_config(rif, dev, event))
7426 err = mlxsw_sp_router_port_check_rif_addr(mlxsw_sp, dev, dev->dev_addr,
7431 err = __mlxsw_sp_inetaddr_event(mlxsw_sp, dev, event, ivi->extack);
7433 mutex_unlock(&mlxsw_sp->router->lock);
7434 return notifier_from_errno(err);
7437 struct mlxsw_sp_inet6addr_event_work {
7438 struct work_struct work;
7439 struct mlxsw_sp *mlxsw_sp;
7440 struct net_device *dev;
7441 unsigned long event;
7444 static void mlxsw_sp_inet6addr_event_work(struct work_struct *work)
7446 struct mlxsw_sp_inet6addr_event_work *inet6addr_work =
7447 container_of(work, struct mlxsw_sp_inet6addr_event_work, work);
7448 struct mlxsw_sp *mlxsw_sp = inet6addr_work->mlxsw_sp;
7449 struct net_device *dev = inet6addr_work->dev;
7450 unsigned long event = inet6addr_work->event;
7451 struct mlxsw_sp_rif *rif;
7454 mutex_lock(&mlxsw_sp->router->lock);
7456 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
7457 if (!mlxsw_sp_rif_should_config(rif, dev, event))
7460 __mlxsw_sp_inetaddr_event(mlxsw_sp, dev, event, NULL);
7462 mutex_unlock(&mlxsw_sp->router->lock);
7465 kfree(inet6addr_work);
7468 /* Called with rcu_read_lock() */
7469 static int mlxsw_sp_inet6addr_event(struct notifier_block *nb,
7470 unsigned long event, void *ptr)
7472 struct inet6_ifaddr *if6 = (struct inet6_ifaddr *) ptr;
7473 struct mlxsw_sp_inet6addr_event_work *inet6addr_work;
7474 struct net_device *dev = if6->idev->dev;
7475 struct mlxsw_sp_router *router;
7477 /* NETDEV_UP event is handled by mlxsw_sp_inet6addr_valid_event */
7478 if (event == NETDEV_UP)
7481 inet6addr_work = kzalloc(sizeof(*inet6addr_work), GFP_ATOMIC);
7482 if (!inet6addr_work)
7485 router = container_of(nb, struct mlxsw_sp_router, inet6addr_nb);
7486 INIT_WORK(&inet6addr_work->work, mlxsw_sp_inet6addr_event_work);
7487 inet6addr_work->mlxsw_sp = router->mlxsw_sp;
7488 inet6addr_work->dev = dev;
7489 inet6addr_work->event = event;
7491 mlxsw_core_schedule_work(&inet6addr_work->work);
7496 int mlxsw_sp_inet6addr_valid_event(struct notifier_block *unused,
7497 unsigned long event, void *ptr)
7499 struct in6_validator_info *i6vi = (struct in6_validator_info *) ptr;
7500 struct net_device *dev = i6vi->i6vi_dev->dev;
7501 struct mlxsw_sp *mlxsw_sp;
7502 struct mlxsw_sp_rif *rif;
7505 mlxsw_sp = mlxsw_sp_lower_get(dev);
7509 mutex_lock(&mlxsw_sp->router->lock);
7510 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
7511 if (!mlxsw_sp_rif_should_config(rif, dev, event))
7514 err = mlxsw_sp_router_port_check_rif_addr(mlxsw_sp, dev, dev->dev_addr,
7519 err = __mlxsw_sp_inetaddr_event(mlxsw_sp, dev, event, i6vi->extack);
7521 mutex_unlock(&mlxsw_sp->router->lock);
7522 return notifier_from_errno(err);
7525 static int mlxsw_sp_rif_edit(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
7526 const char *mac, int mtu)
7528 char ritr_pl[MLXSW_REG_RITR_LEN];
7531 mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
7532 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
7536 mlxsw_reg_ritr_mtu_set(ritr_pl, mtu);
7537 mlxsw_reg_ritr_if_mac_memcpy_to(ritr_pl, mac);
7538 mlxsw_reg_ritr_op_set(ritr_pl, MLXSW_REG_RITR_RIF_CREATE);
7539 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
7543 mlxsw_sp_router_port_change_event(struct mlxsw_sp *mlxsw_sp,
7544 struct mlxsw_sp_rif *rif)
7546 struct net_device *dev = rif->dev;
7550 fid_index = mlxsw_sp_fid_index(rif->fid);
7552 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, fid_index, false);
7556 err = mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, dev->dev_addr,
7561 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, dev->dev_addr, fid_index, true);
7563 goto err_rif_fdb_op;
7565 if (rif->mtu != dev->mtu) {
7566 struct mlxsw_sp_vr *vr;
7569 /* The RIF is relevant only to its mr_table instance, as unlike
7570 * unicast routing, in multicast routing a RIF cannot be shared
7571 * between several multicast routing tables.
7573 vr = &mlxsw_sp->router->vrs[rif->vr_id];
7574 for (i = 0; i < MLXSW_SP_L3_PROTO_MAX; i++)
7575 mlxsw_sp_mr_rif_mtu_update(vr->mr_table[i],
7579 ether_addr_copy(rif->addr, dev->dev_addr);
7580 rif->mtu = dev->mtu;
7582 netdev_dbg(dev, "Updated RIF=%d\n", rif->rif_index);
7587 mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, rif->addr, rif->mtu);
7589 mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, fid_index, true);
7593 static int mlxsw_sp_router_port_pre_changeaddr_event(struct mlxsw_sp_rif *rif,
7594 struct netdev_notifier_pre_changeaddr_info *info)
7596 struct netlink_ext_ack *extack;
7598 extack = netdev_notifier_info_to_extack(&info->info);
7599 return mlxsw_sp_router_port_check_rif_addr(rif->mlxsw_sp, rif->dev,
7600 info->dev_addr, extack);
7603 int mlxsw_sp_netdevice_router_port_event(struct net_device *dev,
7604 unsigned long event, void *ptr)
7606 struct mlxsw_sp *mlxsw_sp;
7607 struct mlxsw_sp_rif *rif;
7610 mlxsw_sp = mlxsw_sp_lower_get(dev);
7614 mutex_lock(&mlxsw_sp->router->lock);
7615 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
7620 case NETDEV_CHANGEMTU:
7621 case NETDEV_CHANGEADDR:
7622 err = mlxsw_sp_router_port_change_event(mlxsw_sp, rif);
7624 case NETDEV_PRE_CHANGEADDR:
7625 err = mlxsw_sp_router_port_pre_changeaddr_event(rif, ptr);
7630 mutex_unlock(&mlxsw_sp->router->lock);
7634 static int mlxsw_sp_port_vrf_join(struct mlxsw_sp *mlxsw_sp,
7635 struct net_device *l3_dev,
7636 struct netlink_ext_ack *extack)
7638 struct mlxsw_sp_rif *rif;
7640 /* If netdev is already associated with a RIF, then we need to
7641 * destroy it and create a new one with the new virtual router ID.
7643 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
7645 __mlxsw_sp_inetaddr_event(mlxsw_sp, l3_dev, NETDEV_DOWN,
7648 return __mlxsw_sp_inetaddr_event(mlxsw_sp, l3_dev, NETDEV_UP, extack);
7651 static void mlxsw_sp_port_vrf_leave(struct mlxsw_sp *mlxsw_sp,
7652 struct net_device *l3_dev)
7654 struct mlxsw_sp_rif *rif;
7656 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
7659 __mlxsw_sp_inetaddr_event(mlxsw_sp, l3_dev, NETDEV_DOWN, NULL);
7662 int mlxsw_sp_netdevice_vrf_event(struct net_device *l3_dev, unsigned long event,
7663 struct netdev_notifier_changeupper_info *info)
7665 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
7668 /* We do not create a RIF for a macvlan, but only use it to
7669 * direct more MAC addresses to the router.
7671 if (!mlxsw_sp || netif_is_macvlan(l3_dev))
7674 mutex_lock(&mlxsw_sp->router->lock);
7676 case NETDEV_PRECHANGEUPPER:
7678 case NETDEV_CHANGEUPPER:
7679 if (info->linking) {
7680 struct netlink_ext_ack *extack;
7682 extack = netdev_notifier_info_to_extack(&info->info);
7683 err = mlxsw_sp_port_vrf_join(mlxsw_sp, l3_dev, extack);
7685 mlxsw_sp_port_vrf_leave(mlxsw_sp, l3_dev);
7689 mutex_unlock(&mlxsw_sp->router->lock);
7694 static int __mlxsw_sp_rif_macvlan_flush(struct net_device *dev,
7695 struct netdev_nested_priv *priv)
7697 struct mlxsw_sp_rif *rif = (struct mlxsw_sp_rif *)priv->data;
7699 if (!netif_is_macvlan(dev))
7702 return mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, dev->dev_addr,
7703 mlxsw_sp_fid_index(rif->fid), false);
7706 static int mlxsw_sp_rif_macvlan_flush(struct mlxsw_sp_rif *rif)
7708 struct netdev_nested_priv priv = {
7709 .data = (void *)rif,
7712 if (!netif_is_macvlan_port(rif->dev))
7715 netdev_warn(rif->dev, "Router interface is deleted. Upper macvlans will not work\n");
7716 return netdev_walk_all_upper_dev_rcu(rif->dev,
7717 __mlxsw_sp_rif_macvlan_flush, &priv);
7720 static void mlxsw_sp_rif_subport_setup(struct mlxsw_sp_rif *rif,
7721 const struct mlxsw_sp_rif_params *params)
7723 struct mlxsw_sp_rif_subport *rif_subport;
7725 rif_subport = mlxsw_sp_rif_subport_rif(rif);
7726 refcount_set(&rif_subport->ref_count, 1);
7727 rif_subport->vid = params->vid;
7728 rif_subport->lag = params->lag;
7730 rif_subport->lag_id = params->lag_id;
7732 rif_subport->system_port = params->system_port;
7735 static int mlxsw_sp_rif_subport_op(struct mlxsw_sp_rif *rif, bool enable)
7737 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
7738 struct mlxsw_sp_rif_subport *rif_subport;
7739 char ritr_pl[MLXSW_REG_RITR_LEN];
7741 rif_subport = mlxsw_sp_rif_subport_rif(rif);
7742 mlxsw_reg_ritr_pack(ritr_pl, enable, MLXSW_REG_RITR_SP_IF,
7743 rif->rif_index, rif->vr_id, rif->dev->mtu);
7744 mlxsw_reg_ritr_mac_pack(ritr_pl, rif->dev->dev_addr);
7745 mlxsw_reg_ritr_sp_if_pack(ritr_pl, rif_subport->lag,
7746 rif_subport->lag ? rif_subport->lag_id :
7747 rif_subport->system_port,
7750 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
7753 static int mlxsw_sp_rif_subport_configure(struct mlxsw_sp_rif *rif)
7757 err = mlxsw_sp_rif_subport_op(rif, true);
7761 err = mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
7762 mlxsw_sp_fid_index(rif->fid), true);
7764 goto err_rif_fdb_op;
7766 mlxsw_sp_fid_rif_set(rif->fid, rif);
7770 mlxsw_sp_rif_subport_op(rif, false);
7774 static void mlxsw_sp_rif_subport_deconfigure(struct mlxsw_sp_rif *rif)
7776 struct mlxsw_sp_fid *fid = rif->fid;
7778 mlxsw_sp_fid_rif_set(fid, NULL);
7779 mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
7780 mlxsw_sp_fid_index(fid), false);
7781 mlxsw_sp_rif_macvlan_flush(rif);
7782 mlxsw_sp_rif_subport_op(rif, false);
7785 static struct mlxsw_sp_fid *
7786 mlxsw_sp_rif_subport_fid_get(struct mlxsw_sp_rif *rif,
7787 struct netlink_ext_ack *extack)
7789 return mlxsw_sp_fid_rfid_get(rif->mlxsw_sp, rif->rif_index);
7792 static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_subport_ops = {
7793 .type = MLXSW_SP_RIF_TYPE_SUBPORT,
7794 .rif_size = sizeof(struct mlxsw_sp_rif_subport),
7795 .setup = mlxsw_sp_rif_subport_setup,
7796 .configure = mlxsw_sp_rif_subport_configure,
7797 .deconfigure = mlxsw_sp_rif_subport_deconfigure,
7798 .fid_get = mlxsw_sp_rif_subport_fid_get,
7801 static int mlxsw_sp_rif_vlan_fid_op(struct mlxsw_sp_rif *rif,
7802 enum mlxsw_reg_ritr_if_type type,
7803 u16 vid_fid, bool enable)
7805 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
7806 char ritr_pl[MLXSW_REG_RITR_LEN];
7808 mlxsw_reg_ritr_pack(ritr_pl, enable, type, rif->rif_index, rif->vr_id,
7810 mlxsw_reg_ritr_mac_pack(ritr_pl, rif->dev->dev_addr);
7811 mlxsw_reg_ritr_fid_set(ritr_pl, type, vid_fid);
7813 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
7816 u8 mlxsw_sp_router_port(const struct mlxsw_sp *mlxsw_sp)
7818 return mlxsw_core_max_ports(mlxsw_sp->core) + 1;
7821 static int mlxsw_sp_rif_fid_configure(struct mlxsw_sp_rif *rif)
7823 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
7824 u16 fid_index = mlxsw_sp_fid_index(rif->fid);
7827 err = mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index,
7832 err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
7833 mlxsw_sp_router_port(mlxsw_sp), true);
7835 goto err_fid_mc_flood_set;
7837 err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
7838 mlxsw_sp_router_port(mlxsw_sp), true);
7840 goto err_fid_bc_flood_set;
7842 err = mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
7843 mlxsw_sp_fid_index(rif->fid), true);
7845 goto err_rif_fdb_op;
7847 mlxsw_sp_fid_rif_set(rif->fid, rif);
7851 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
7852 mlxsw_sp_router_port(mlxsw_sp), false);
7853 err_fid_bc_flood_set:
7854 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
7855 mlxsw_sp_router_port(mlxsw_sp), false);
7856 err_fid_mc_flood_set:
7857 mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index, false);
7861 static void mlxsw_sp_rif_fid_deconfigure(struct mlxsw_sp_rif *rif)
7863 u16 fid_index = mlxsw_sp_fid_index(rif->fid);
7864 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
7865 struct mlxsw_sp_fid *fid = rif->fid;
7867 mlxsw_sp_fid_rif_set(fid, NULL);
7868 mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
7869 mlxsw_sp_fid_index(fid), false);
7870 mlxsw_sp_rif_macvlan_flush(rif);
7871 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
7872 mlxsw_sp_router_port(mlxsw_sp), false);
7873 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
7874 mlxsw_sp_router_port(mlxsw_sp), false);
7875 mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index, false);
7878 static struct mlxsw_sp_fid *
7879 mlxsw_sp_rif_fid_fid_get(struct mlxsw_sp_rif *rif,
7880 struct netlink_ext_ack *extack)
7882 return mlxsw_sp_fid_8021d_get(rif->mlxsw_sp, rif->dev->ifindex);
7885 static void mlxsw_sp_rif_fid_fdb_del(struct mlxsw_sp_rif *rif, const char *mac)
7887 struct switchdev_notifier_fdb_info info;
7888 struct net_device *dev;
7890 dev = br_fdb_find_port(rif->dev, mac, 0);
7896 call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE, dev, &info.info,
7900 static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_fid_ops = {
7901 .type = MLXSW_SP_RIF_TYPE_FID,
7902 .rif_size = sizeof(struct mlxsw_sp_rif),
7903 .configure = mlxsw_sp_rif_fid_configure,
7904 .deconfigure = mlxsw_sp_rif_fid_deconfigure,
7905 .fid_get = mlxsw_sp_rif_fid_fid_get,
7906 .fdb_del = mlxsw_sp_rif_fid_fdb_del,
7909 static struct mlxsw_sp_fid *
7910 mlxsw_sp_rif_vlan_fid_get(struct mlxsw_sp_rif *rif,
7911 struct netlink_ext_ack *extack)
7913 struct net_device *br_dev;
7917 if (is_vlan_dev(rif->dev)) {
7918 vid = vlan_dev_vlan_id(rif->dev);
7919 br_dev = vlan_dev_real_dev(rif->dev);
7920 if (WARN_ON(!netif_is_bridge_master(br_dev)))
7921 return ERR_PTR(-EINVAL);
7923 err = br_vlan_get_pvid(rif->dev, &vid);
7924 if (err < 0 || !vid) {
7925 NL_SET_ERR_MSG_MOD(extack, "Couldn't determine bridge PVID");
7926 return ERR_PTR(-EINVAL);
7930 return mlxsw_sp_fid_8021q_get(rif->mlxsw_sp, vid);
7933 static void mlxsw_sp_rif_vlan_fdb_del(struct mlxsw_sp_rif *rif, const char *mac)
7935 u16 vid = mlxsw_sp_fid_8021q_vid(rif->fid);
7936 struct switchdev_notifier_fdb_info info;
7937 struct net_device *br_dev;
7938 struct net_device *dev;
7940 br_dev = is_vlan_dev(rif->dev) ? vlan_dev_real_dev(rif->dev) : rif->dev;
7941 dev = br_fdb_find_port(br_dev, mac, vid);
7947 call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE, dev, &info.info,
7951 static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_vlan_emu_ops = {
7952 .type = MLXSW_SP_RIF_TYPE_VLAN,
7953 .rif_size = sizeof(struct mlxsw_sp_rif),
7954 .configure = mlxsw_sp_rif_fid_configure,
7955 .deconfigure = mlxsw_sp_rif_fid_deconfigure,
7956 .fid_get = mlxsw_sp_rif_vlan_fid_get,
7957 .fdb_del = mlxsw_sp_rif_vlan_fdb_del,
7960 static struct mlxsw_sp_rif_ipip_lb *
7961 mlxsw_sp_rif_ipip_lb_rif(struct mlxsw_sp_rif *rif)
7963 return container_of(rif, struct mlxsw_sp_rif_ipip_lb, common);
7967 mlxsw_sp_rif_ipip_lb_setup(struct mlxsw_sp_rif *rif,
7968 const struct mlxsw_sp_rif_params *params)
7970 struct mlxsw_sp_rif_params_ipip_lb *params_lb;
7971 struct mlxsw_sp_rif_ipip_lb *rif_lb;
7973 params_lb = container_of(params, struct mlxsw_sp_rif_params_ipip_lb,
7975 rif_lb = mlxsw_sp_rif_ipip_lb_rif(rif);
7976 rif_lb->lb_config = params_lb->lb_config;
7980 mlxsw_sp1_rif_ipip_lb_configure(struct mlxsw_sp_rif *rif)
7982 struct mlxsw_sp_rif_ipip_lb *lb_rif = mlxsw_sp_rif_ipip_lb_rif(rif);
7983 u32 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(rif->dev);
7984 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
7985 struct mlxsw_sp_vr *ul_vr;
7988 ul_vr = mlxsw_sp_vr_get(mlxsw_sp, ul_tb_id, NULL);
7990 return PTR_ERR(ul_vr);
7992 err = mlxsw_sp_rif_ipip_lb_op(lb_rif, ul_vr->id, 0, true);
7994 goto err_loopback_op;
7996 lb_rif->ul_vr_id = ul_vr->id;
7997 lb_rif->ul_rif_id = 0;
8002 mlxsw_sp_vr_put(mlxsw_sp, ul_vr);
8006 static void mlxsw_sp1_rif_ipip_lb_deconfigure(struct mlxsw_sp_rif *rif)
8008 struct mlxsw_sp_rif_ipip_lb *lb_rif = mlxsw_sp_rif_ipip_lb_rif(rif);
8009 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
8010 struct mlxsw_sp_vr *ul_vr;
8012 ul_vr = &mlxsw_sp->router->vrs[lb_rif->ul_vr_id];
8013 mlxsw_sp_rif_ipip_lb_op(lb_rif, ul_vr->id, 0, false);
8016 mlxsw_sp_vr_put(mlxsw_sp, ul_vr);
8019 static const struct mlxsw_sp_rif_ops mlxsw_sp1_rif_ipip_lb_ops = {
8020 .type = MLXSW_SP_RIF_TYPE_IPIP_LB,
8021 .rif_size = sizeof(struct mlxsw_sp_rif_ipip_lb),
8022 .setup = mlxsw_sp_rif_ipip_lb_setup,
8023 .configure = mlxsw_sp1_rif_ipip_lb_configure,
8024 .deconfigure = mlxsw_sp1_rif_ipip_lb_deconfigure,
8027 const struct mlxsw_sp_rif_ops *mlxsw_sp1_rif_ops_arr[] = {
8028 [MLXSW_SP_RIF_TYPE_SUBPORT] = &mlxsw_sp_rif_subport_ops,
8029 [MLXSW_SP_RIF_TYPE_VLAN] = &mlxsw_sp_rif_vlan_emu_ops,
8030 [MLXSW_SP_RIF_TYPE_FID] = &mlxsw_sp_rif_fid_ops,
8031 [MLXSW_SP_RIF_TYPE_IPIP_LB] = &mlxsw_sp1_rif_ipip_lb_ops,
8035 mlxsw_sp_rif_ipip_lb_ul_rif_op(struct mlxsw_sp_rif *ul_rif, bool enable)
8037 struct mlxsw_sp *mlxsw_sp = ul_rif->mlxsw_sp;
8038 char ritr_pl[MLXSW_REG_RITR_LEN];
8040 mlxsw_reg_ritr_pack(ritr_pl, enable, MLXSW_REG_RITR_LOOPBACK_IF,
8041 ul_rif->rif_index, ul_rif->vr_id, IP_MAX_MTU);
8042 mlxsw_reg_ritr_loopback_protocol_set(ritr_pl,
8043 MLXSW_REG_RITR_LOOPBACK_GENERIC);
8045 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
8048 static struct mlxsw_sp_rif *
8049 mlxsw_sp_ul_rif_create(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr,
8050 struct netlink_ext_ack *extack)
8052 struct mlxsw_sp_rif *ul_rif;
8056 err = mlxsw_sp_rif_index_alloc(mlxsw_sp, &rif_index);
8058 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported router interfaces");
8059 return ERR_PTR(err);
8062 ul_rif = mlxsw_sp_rif_alloc(sizeof(*ul_rif), rif_index, vr->id, NULL);
8064 return ERR_PTR(-ENOMEM);
8066 mlxsw_sp->router->rifs[rif_index] = ul_rif;
8067 ul_rif->mlxsw_sp = mlxsw_sp;
8068 err = mlxsw_sp_rif_ipip_lb_ul_rif_op(ul_rif, true);
8075 mlxsw_sp->router->rifs[rif_index] = NULL;
8077 return ERR_PTR(err);
8080 static void mlxsw_sp_ul_rif_destroy(struct mlxsw_sp_rif *ul_rif)
8082 struct mlxsw_sp *mlxsw_sp = ul_rif->mlxsw_sp;
8084 mlxsw_sp_rif_ipip_lb_ul_rif_op(ul_rif, false);
8085 mlxsw_sp->router->rifs[ul_rif->rif_index] = NULL;
8089 static struct mlxsw_sp_rif *
8090 mlxsw_sp_ul_rif_get(struct mlxsw_sp *mlxsw_sp, u32 tb_id,
8091 struct netlink_ext_ack *extack)
8093 struct mlxsw_sp_vr *vr;
8096 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id, extack);
8098 return ERR_CAST(vr);
8100 if (refcount_inc_not_zero(&vr->ul_rif_refcnt))
8103 vr->ul_rif = mlxsw_sp_ul_rif_create(mlxsw_sp, vr, extack);
8104 if (IS_ERR(vr->ul_rif)) {
8105 err = PTR_ERR(vr->ul_rif);
8106 goto err_ul_rif_create;
8110 refcount_set(&vr->ul_rif_refcnt, 1);
8115 mlxsw_sp_vr_put(mlxsw_sp, vr);
8116 return ERR_PTR(err);
8119 static void mlxsw_sp_ul_rif_put(struct mlxsw_sp_rif *ul_rif)
8121 struct mlxsw_sp *mlxsw_sp = ul_rif->mlxsw_sp;
8122 struct mlxsw_sp_vr *vr;
8124 vr = &mlxsw_sp->router->vrs[ul_rif->vr_id];
8126 if (!refcount_dec_and_test(&vr->ul_rif_refcnt))
8130 mlxsw_sp_ul_rif_destroy(ul_rif);
8131 mlxsw_sp_vr_put(mlxsw_sp, vr);
8134 int mlxsw_sp_router_ul_rif_get(struct mlxsw_sp *mlxsw_sp, u32 ul_tb_id,
8137 struct mlxsw_sp_rif *ul_rif;
8140 mutex_lock(&mlxsw_sp->router->lock);
8141 ul_rif = mlxsw_sp_ul_rif_get(mlxsw_sp, ul_tb_id, NULL);
8142 if (IS_ERR(ul_rif)) {
8143 err = PTR_ERR(ul_rif);
8146 *ul_rif_index = ul_rif->rif_index;
8148 mutex_unlock(&mlxsw_sp->router->lock);
8152 void mlxsw_sp_router_ul_rif_put(struct mlxsw_sp *mlxsw_sp, u16 ul_rif_index)
8154 struct mlxsw_sp_rif *ul_rif;
8156 mutex_lock(&mlxsw_sp->router->lock);
8157 ul_rif = mlxsw_sp->router->rifs[ul_rif_index];
8158 if (WARN_ON(!ul_rif))
8161 mlxsw_sp_ul_rif_put(ul_rif);
8163 mutex_unlock(&mlxsw_sp->router->lock);
8167 mlxsw_sp2_rif_ipip_lb_configure(struct mlxsw_sp_rif *rif)
8169 struct mlxsw_sp_rif_ipip_lb *lb_rif = mlxsw_sp_rif_ipip_lb_rif(rif);
8170 u32 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(rif->dev);
8171 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
8172 struct mlxsw_sp_rif *ul_rif;
8175 ul_rif = mlxsw_sp_ul_rif_get(mlxsw_sp, ul_tb_id, NULL);
8177 return PTR_ERR(ul_rif);
8179 err = mlxsw_sp_rif_ipip_lb_op(lb_rif, 0, ul_rif->rif_index, true);
8181 goto err_loopback_op;
8183 lb_rif->ul_vr_id = 0;
8184 lb_rif->ul_rif_id = ul_rif->rif_index;
8189 mlxsw_sp_ul_rif_put(ul_rif);
8193 static void mlxsw_sp2_rif_ipip_lb_deconfigure(struct mlxsw_sp_rif *rif)
8195 struct mlxsw_sp_rif_ipip_lb *lb_rif = mlxsw_sp_rif_ipip_lb_rif(rif);
8196 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
8197 struct mlxsw_sp_rif *ul_rif;
8199 ul_rif = mlxsw_sp_rif_by_index(mlxsw_sp, lb_rif->ul_rif_id);
8200 mlxsw_sp_rif_ipip_lb_op(lb_rif, 0, lb_rif->ul_rif_id, false);
8201 mlxsw_sp_ul_rif_put(ul_rif);
8204 static const struct mlxsw_sp_rif_ops mlxsw_sp2_rif_ipip_lb_ops = {
8205 .type = MLXSW_SP_RIF_TYPE_IPIP_LB,
8206 .rif_size = sizeof(struct mlxsw_sp_rif_ipip_lb),
8207 .setup = mlxsw_sp_rif_ipip_lb_setup,
8208 .configure = mlxsw_sp2_rif_ipip_lb_configure,
8209 .deconfigure = mlxsw_sp2_rif_ipip_lb_deconfigure,
8212 const struct mlxsw_sp_rif_ops *mlxsw_sp2_rif_ops_arr[] = {
8213 [MLXSW_SP_RIF_TYPE_SUBPORT] = &mlxsw_sp_rif_subport_ops,
8214 [MLXSW_SP_RIF_TYPE_VLAN] = &mlxsw_sp_rif_vlan_emu_ops,
8215 [MLXSW_SP_RIF_TYPE_FID] = &mlxsw_sp_rif_fid_ops,
8216 [MLXSW_SP_RIF_TYPE_IPIP_LB] = &mlxsw_sp2_rif_ipip_lb_ops,
8219 static int mlxsw_sp_rifs_init(struct mlxsw_sp *mlxsw_sp)
8221 u64 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
8223 mlxsw_sp->router->rifs = kcalloc(max_rifs,
8224 sizeof(struct mlxsw_sp_rif *),
8226 if (!mlxsw_sp->router->rifs)
8232 static void mlxsw_sp_rifs_fini(struct mlxsw_sp *mlxsw_sp)
8236 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
8237 WARN_ON_ONCE(mlxsw_sp->router->rifs[i]);
8239 kfree(mlxsw_sp->router->rifs);
8243 mlxsw_sp_ipip_config_tigcr(struct mlxsw_sp *mlxsw_sp)
8245 char tigcr_pl[MLXSW_REG_TIGCR_LEN];
8247 mlxsw_reg_tigcr_pack(tigcr_pl, true, 0);
8248 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(tigcr), tigcr_pl);
8251 static int mlxsw_sp_ipips_init(struct mlxsw_sp *mlxsw_sp)
8255 mlxsw_sp->router->ipip_ops_arr = mlxsw_sp_ipip_ops_arr;
8256 INIT_LIST_HEAD(&mlxsw_sp->router->ipip_list);
8258 err = mlxsw_sp_ipip_ecn_encap_init(mlxsw_sp);
8261 err = mlxsw_sp_ipip_ecn_decap_init(mlxsw_sp);
8265 return mlxsw_sp_ipip_config_tigcr(mlxsw_sp);
8268 static void mlxsw_sp_ipips_fini(struct mlxsw_sp *mlxsw_sp)
8270 WARN_ON(!list_empty(&mlxsw_sp->router->ipip_list));
8273 static void mlxsw_sp_router_fib_dump_flush(struct notifier_block *nb)
8275 struct mlxsw_sp_router *router;
8277 /* Flush pending FIB notifications and then flush the device's
8278 * table before requesting another dump. The FIB notification
8279 * block is unregistered, so no need to take RTNL.
8281 mlxsw_core_flush_owq();
8282 router = container_of(nb, struct mlxsw_sp_router, fib_nb);
8283 mlxsw_sp_router_fib_flush(router->mlxsw_sp);
8286 #ifdef CONFIG_IP_ROUTE_MULTIPATH
8287 static void mlxsw_sp_mp_hash_header_set(char *recr2_pl, int header)
8289 mlxsw_reg_recr2_outer_header_enables_set(recr2_pl, header, true);
8292 static void mlxsw_sp_mp_hash_field_set(char *recr2_pl, int field)
8294 mlxsw_reg_recr2_outer_header_fields_enable_set(recr2_pl, field, true);
8297 static void mlxsw_sp_mp4_hash_init(struct mlxsw_sp *mlxsw_sp, char *recr2_pl)
8299 struct net *net = mlxsw_sp_net(mlxsw_sp);
8300 bool only_l3 = !net->ipv4.sysctl_fib_multipath_hash_policy;
8302 mlxsw_sp_mp_hash_header_set(recr2_pl,
8303 MLXSW_REG_RECR2_IPV4_EN_NOT_TCP_NOT_UDP);
8304 mlxsw_sp_mp_hash_header_set(recr2_pl, MLXSW_REG_RECR2_IPV4_EN_TCP_UDP);
8305 mlxsw_reg_recr2_ipv4_sip_enable(recr2_pl);
8306 mlxsw_reg_recr2_ipv4_dip_enable(recr2_pl);
8309 mlxsw_sp_mp_hash_header_set(recr2_pl, MLXSW_REG_RECR2_TCP_UDP_EN_IPV4);
8310 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_IPV4_PROTOCOL);
8311 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_TCP_UDP_SPORT);
8312 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_TCP_UDP_DPORT);
8315 static void mlxsw_sp_mp6_hash_init(struct mlxsw_sp *mlxsw_sp, char *recr2_pl)
8317 bool only_l3 = !ip6_multipath_hash_policy(mlxsw_sp_net(mlxsw_sp));
8319 mlxsw_sp_mp_hash_header_set(recr2_pl,
8320 MLXSW_REG_RECR2_IPV6_EN_NOT_TCP_NOT_UDP);
8321 mlxsw_sp_mp_hash_header_set(recr2_pl, MLXSW_REG_RECR2_IPV6_EN_TCP_UDP);
8322 mlxsw_reg_recr2_ipv6_sip_enable(recr2_pl);
8323 mlxsw_reg_recr2_ipv6_dip_enable(recr2_pl);
8324 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_IPV6_NEXT_HEADER);
8326 mlxsw_sp_mp_hash_field_set(recr2_pl,
8327 MLXSW_REG_RECR2_IPV6_FLOW_LABEL);
8329 mlxsw_sp_mp_hash_header_set(recr2_pl,
8330 MLXSW_REG_RECR2_TCP_UDP_EN_IPV6);
8331 mlxsw_sp_mp_hash_field_set(recr2_pl,
8332 MLXSW_REG_RECR2_TCP_UDP_SPORT);
8333 mlxsw_sp_mp_hash_field_set(recr2_pl,
8334 MLXSW_REG_RECR2_TCP_UDP_DPORT);
8338 static int mlxsw_sp_mp_hash_init(struct mlxsw_sp *mlxsw_sp)
8340 char recr2_pl[MLXSW_REG_RECR2_LEN];
8343 seed = jhash(mlxsw_sp->base_mac, sizeof(mlxsw_sp->base_mac), 0);
8344 mlxsw_reg_recr2_pack(recr2_pl, seed);
8345 mlxsw_sp_mp4_hash_init(mlxsw_sp, recr2_pl);
8346 mlxsw_sp_mp6_hash_init(mlxsw_sp, recr2_pl);
8348 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(recr2), recr2_pl);
8351 static int mlxsw_sp_mp_hash_init(struct mlxsw_sp *mlxsw_sp)
8357 static int mlxsw_sp_dscp_init(struct mlxsw_sp *mlxsw_sp)
8359 char rdpm_pl[MLXSW_REG_RDPM_LEN];
8362 MLXSW_REG_ZERO(rdpm, rdpm_pl);
8364 /* HW is determining switch priority based on DSCP-bits, but the
8365 * kernel is still doing that based on the ToS. Since there's a
8366 * mismatch in bits we need to make sure to translate the right
8367 * value ToS would observe, skipping the 2 least-significant ECN bits.
8369 for (i = 0; i < MLXSW_REG_RDPM_DSCP_ENTRY_REC_MAX_COUNT; i++)
8370 mlxsw_reg_rdpm_pack(rdpm_pl, i, rt_tos2priority(i << 2));
8372 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rdpm), rdpm_pl);
8375 static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
8377 struct net *net = mlxsw_sp_net(mlxsw_sp);
8378 bool usp = net->ipv4.sysctl_ip_fwd_update_priority;
8379 char rgcr_pl[MLXSW_REG_RGCR_LEN];
8382 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_RIFS))
8384 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
8386 mlxsw_reg_rgcr_pack(rgcr_pl, true, true);
8387 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, max_rifs);
8388 mlxsw_reg_rgcr_usp_set(rgcr_pl, usp);
8389 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
8392 static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
8394 char rgcr_pl[MLXSW_REG_RGCR_LEN];
8396 mlxsw_reg_rgcr_pack(rgcr_pl, false, false);
8397 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
8400 static const struct mlxsw_sp_router_ll_ops mlxsw_sp_router_ll_basic_ops = {
8401 .ralta_write = mlxsw_sp_router_ll_basic_ralta_write,
8402 .ralst_write = mlxsw_sp_router_ll_basic_ralst_write,
8403 .raltb_write = mlxsw_sp_router_ll_basic_raltb_write,
8404 .fib_entry_op_ctx_size = sizeof(struct mlxsw_sp_fib_entry_op_ctx_basic),
8405 .fib_entry_pack = mlxsw_sp_router_ll_basic_fib_entry_pack,
8406 .fib_entry_act_remote_pack = mlxsw_sp_router_ll_basic_fib_entry_act_remote_pack,
8407 .fib_entry_act_local_pack = mlxsw_sp_router_ll_basic_fib_entry_act_local_pack,
8408 .fib_entry_act_ip2me_pack = mlxsw_sp_router_ll_basic_fib_entry_act_ip2me_pack,
8409 .fib_entry_act_ip2me_tun_pack = mlxsw_sp_router_ll_basic_fib_entry_act_ip2me_tun_pack,
8410 .fib_entry_commit = mlxsw_sp_router_ll_basic_fib_entry_commit,
8411 .fib_entry_is_committed = mlxsw_sp_router_ll_basic_fib_entry_is_committed,
8414 static int mlxsw_sp_router_ll_op_ctx_init(struct mlxsw_sp_router *router)
8416 size_t max_size = 0;
8419 for (i = 0; i < MLXSW_SP_L3_PROTO_MAX; i++) {
8420 size_t size = router->proto_ll_ops[i]->fib_entry_op_ctx_size;
8422 if (size > max_size)
8425 router->ll_op_ctx = kzalloc(sizeof(*router->ll_op_ctx) + max_size,
8427 if (!router->ll_op_ctx)
8429 INIT_LIST_HEAD(&router->ll_op_ctx->fib_entry_priv_list);
8433 static void mlxsw_sp_router_ll_op_ctx_fini(struct mlxsw_sp_router *router)
8435 WARN_ON(!list_empty(&router->ll_op_ctx->fib_entry_priv_list));
8436 kfree(router->ll_op_ctx);
8439 int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp,
8440 struct netlink_ext_ack *extack)
8442 struct mlxsw_sp_router *router;
8445 router = kzalloc(sizeof(*mlxsw_sp->router), GFP_KERNEL);
8448 mutex_init(&router->lock);
8449 mlxsw_sp->router = router;
8450 router->mlxsw_sp = mlxsw_sp;
8452 router->proto_ll_ops[MLXSW_SP_L3_PROTO_IPV4] = &mlxsw_sp_router_ll_basic_ops;
8453 router->proto_ll_ops[MLXSW_SP_L3_PROTO_IPV6] = &mlxsw_sp_router_ll_basic_ops;
8455 err = mlxsw_sp_router_ll_op_ctx_init(router);
8457 goto err_ll_op_ctx_init;
8459 INIT_LIST_HEAD(&mlxsw_sp->router->nexthop_neighs_list);
8460 err = __mlxsw_sp_router_init(mlxsw_sp);
8462 goto err_router_init;
8464 err = mlxsw_sp_rifs_init(mlxsw_sp);
8468 err = mlxsw_sp_ipips_init(mlxsw_sp);
8470 goto err_ipips_init;
8472 err = rhashtable_init(&mlxsw_sp->router->nexthop_ht,
8473 &mlxsw_sp_nexthop_ht_params);
8475 goto err_nexthop_ht_init;
8477 err = rhashtable_init(&mlxsw_sp->router->nexthop_group_ht,
8478 &mlxsw_sp_nexthop_group_ht_params);
8480 goto err_nexthop_group_ht_init;
8482 INIT_LIST_HEAD(&mlxsw_sp->router->nexthop_list);
8483 err = mlxsw_sp_lpm_init(mlxsw_sp);
8487 err = mlxsw_sp_mr_init(mlxsw_sp, &mlxsw_sp_mr_tcam_ops);
8491 err = mlxsw_sp_vrs_init(mlxsw_sp);
8495 err = mlxsw_sp_neigh_init(mlxsw_sp);
8497 goto err_neigh_init;
8499 err = mlxsw_sp_mp_hash_init(mlxsw_sp);
8501 goto err_mp_hash_init;
8503 err = mlxsw_sp_dscp_init(mlxsw_sp);
8507 INIT_WORK(&router->fib_event_work, mlxsw_sp_router_fib_event_work);
8508 INIT_LIST_HEAD(&router->fib_event_queue);
8509 spin_lock_init(&router->fib_event_queue_lock);
8511 router->inetaddr_nb.notifier_call = mlxsw_sp_inetaddr_event;
8512 err = register_inetaddr_notifier(&router->inetaddr_nb);
8514 goto err_register_inetaddr_notifier;
8516 router->inet6addr_nb.notifier_call = mlxsw_sp_inet6addr_event;
8517 err = register_inet6addr_notifier(&router->inet6addr_nb);
8519 goto err_register_inet6addr_notifier;
8521 mlxsw_sp->router->netevent_nb.notifier_call =
8522 mlxsw_sp_router_netevent_event;
8523 err = register_netevent_notifier(&mlxsw_sp->router->netevent_nb);
8525 goto err_register_netevent_notifier;
8527 mlxsw_sp->router->fib_nb.notifier_call = mlxsw_sp_router_fib_event;
8528 err = register_fib_notifier(mlxsw_sp_net(mlxsw_sp),
8529 &mlxsw_sp->router->fib_nb,
8530 mlxsw_sp_router_fib_dump_flush, extack);
8532 goto err_register_fib_notifier;
8536 err_register_fib_notifier:
8537 unregister_netevent_notifier(&mlxsw_sp->router->netevent_nb);
8538 err_register_netevent_notifier:
8539 unregister_inet6addr_notifier(&router->inet6addr_nb);
8540 err_register_inet6addr_notifier:
8541 unregister_inetaddr_notifier(&router->inetaddr_nb);
8542 err_register_inetaddr_notifier:
8543 mlxsw_core_flush_owq();
8544 WARN_ON(!list_empty(&router->fib_event_queue));
8547 mlxsw_sp_neigh_fini(mlxsw_sp);
8549 mlxsw_sp_vrs_fini(mlxsw_sp);
8551 mlxsw_sp_mr_fini(mlxsw_sp);
8553 mlxsw_sp_lpm_fini(mlxsw_sp);
8555 rhashtable_destroy(&mlxsw_sp->router->nexthop_group_ht);
8556 err_nexthop_group_ht_init:
8557 rhashtable_destroy(&mlxsw_sp->router->nexthop_ht);
8558 err_nexthop_ht_init:
8559 mlxsw_sp_ipips_fini(mlxsw_sp);
8561 mlxsw_sp_rifs_fini(mlxsw_sp);
8563 __mlxsw_sp_router_fini(mlxsw_sp);
8565 mlxsw_sp_router_ll_op_ctx_fini(router);
8567 mutex_destroy(&mlxsw_sp->router->lock);
8568 kfree(mlxsw_sp->router);
8572 void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
8574 unregister_fib_notifier(mlxsw_sp_net(mlxsw_sp),
8575 &mlxsw_sp->router->fib_nb);
8576 unregister_netevent_notifier(&mlxsw_sp->router->netevent_nb);
8577 unregister_inet6addr_notifier(&mlxsw_sp->router->inet6addr_nb);
8578 unregister_inetaddr_notifier(&mlxsw_sp->router->inetaddr_nb);
8579 mlxsw_core_flush_owq();
8580 WARN_ON(!list_empty(&mlxsw_sp->router->fib_event_queue));
8581 mlxsw_sp_neigh_fini(mlxsw_sp);
8582 mlxsw_sp_vrs_fini(mlxsw_sp);
8583 mlxsw_sp_mr_fini(mlxsw_sp);
8584 mlxsw_sp_lpm_fini(mlxsw_sp);
8585 rhashtable_destroy(&mlxsw_sp->router->nexthop_group_ht);
8586 rhashtable_destroy(&mlxsw_sp->router->nexthop_ht);
8587 mlxsw_sp_ipips_fini(mlxsw_sp);
8588 mlxsw_sp_rifs_fini(mlxsw_sp);
8589 __mlxsw_sp_router_fini(mlxsw_sp);
8590 mlxsw_sp_router_ll_op_ctx_fini(mlxsw_sp->router);
8591 mutex_destroy(&mlxsw_sp->router->lock);
8592 kfree(mlxsw_sp->router);