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);
3974 nh->ifindex = dev->ifindex;
3977 in_dev = __in_dev_get_rcu(dev);
3978 if (in_dev && IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
3979 fib_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
3985 err = mlxsw_sp_nexthop_type_init(mlxsw_sp, nh, dev);
3987 goto err_nexthop_neigh_init;
3991 err_nexthop_neigh_init:
3992 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
3996 static void mlxsw_sp_nexthop4_fini(struct mlxsw_sp *mlxsw_sp,
3997 struct mlxsw_sp_nexthop *nh)
3999 mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
4000 list_del(&nh->router_list_node);
4001 mlxsw_sp_nexthop_counter_free(mlxsw_sp, nh);
4002 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
4005 static void mlxsw_sp_nexthop4_event(struct mlxsw_sp *mlxsw_sp,
4006 unsigned long event, struct fib_nh *fib_nh)
4008 struct mlxsw_sp_nexthop_key key;
4009 struct mlxsw_sp_nexthop *nh;
4011 if (mlxsw_sp->router->aborted)
4014 key.fib_nh = fib_nh;
4015 nh = mlxsw_sp_nexthop_lookup(mlxsw_sp, key);
4020 case FIB_EVENT_NH_ADD:
4021 mlxsw_sp_nexthop_type_init(mlxsw_sp, nh, fib_nh->fib_nh_dev);
4023 case FIB_EVENT_NH_DEL:
4024 mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
4028 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nhgi->nh_grp);
4031 static void mlxsw_sp_nexthop_rif_update(struct mlxsw_sp *mlxsw_sp,
4032 struct mlxsw_sp_rif *rif)
4034 struct mlxsw_sp_nexthop *nh;
4037 list_for_each_entry(nh, &rif->nexthop_list, rif_list_node) {
4039 case MLXSW_SP_NEXTHOP_TYPE_ETH:
4042 case MLXSW_SP_NEXTHOP_TYPE_IPIP:
4043 removing = !mlxsw_sp_ipip_netdev_ul_up(rif->dev);
4050 __mlxsw_sp_nexthop_neigh_update(nh, removing);
4051 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nhgi->nh_grp);
4055 static void mlxsw_sp_nexthop_rif_migrate(struct mlxsw_sp *mlxsw_sp,
4056 struct mlxsw_sp_rif *old_rif,
4057 struct mlxsw_sp_rif *new_rif)
4059 struct mlxsw_sp_nexthop *nh;
4061 list_splice_init(&old_rif->nexthop_list, &new_rif->nexthop_list);
4062 list_for_each_entry(nh, &new_rif->nexthop_list, rif_list_node)
4064 mlxsw_sp_nexthop_rif_update(mlxsw_sp, new_rif);
4067 static void mlxsw_sp_nexthop_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
4068 struct mlxsw_sp_rif *rif)
4070 struct mlxsw_sp_nexthop *nh, *tmp;
4072 list_for_each_entry_safe(nh, tmp, &rif->nexthop_list, rif_list_node) {
4073 mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
4074 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nhgi->nh_grp);
4078 static bool mlxsw_sp_fi_is_gateway(const struct mlxsw_sp *mlxsw_sp,
4079 struct fib_info *fi)
4081 const struct fib_nh *nh = fib_info_nh(fi, 0);
4083 return nh->fib_nh_scope == RT_SCOPE_LINK ||
4084 mlxsw_sp_nexthop4_ipip_type(mlxsw_sp, nh, NULL);
4088 mlxsw_sp_nexthop4_group_info_init(struct mlxsw_sp *mlxsw_sp,
4089 struct mlxsw_sp_nexthop_group *nh_grp)
4091 unsigned int nhs = fib_info_num_path(nh_grp->ipv4.fi);
4092 struct mlxsw_sp_nexthop_group_info *nhgi;
4093 struct mlxsw_sp_nexthop *nh;
4096 nhgi = kzalloc(struct_size(nhgi, nexthops, nhs), GFP_KERNEL);
4099 nh_grp->nhgi = nhgi;
4100 nhgi->nh_grp = nh_grp;
4101 nhgi->gateway = mlxsw_sp_fi_is_gateway(mlxsw_sp, nh_grp->ipv4.fi);
4103 for (i = 0; i < nhgi->count; i++) {
4104 struct fib_nh *fib_nh;
4106 nh = &nhgi->nexthops[i];
4107 fib_nh = fib_info_nh(nh_grp->ipv4.fi, i);
4108 err = mlxsw_sp_nexthop4_init(mlxsw_sp, nh_grp, nh, fib_nh);
4110 goto err_nexthop4_init;
4112 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
4117 for (i--; i >= 0; i--) {
4118 nh = &nhgi->nexthops[i];
4119 mlxsw_sp_nexthop4_fini(mlxsw_sp, nh);
4126 mlxsw_sp_nexthop4_group_info_fini(struct mlxsw_sp *mlxsw_sp,
4127 struct mlxsw_sp_nexthop_group *nh_grp)
4129 struct mlxsw_sp_nexthop_group_info *nhgi = nh_grp->nhgi;
4132 for (i = nhgi->count - 1; i >= 0; i--) {
4133 struct mlxsw_sp_nexthop *nh = &nhgi->nexthops[i];
4135 mlxsw_sp_nexthop4_fini(mlxsw_sp, nh);
4137 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
4138 WARN_ON_ONCE(nhgi->adj_index_valid);
4142 static struct mlxsw_sp_nexthop_group *
4143 mlxsw_sp_nexthop4_group_create(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
4145 struct mlxsw_sp_nexthop_group *nh_grp;
4148 nh_grp = kzalloc(sizeof(*nh_grp), GFP_KERNEL);
4150 return ERR_PTR(-ENOMEM);
4151 INIT_LIST_HEAD(&nh_grp->fib_list);
4152 nh_grp->type = MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV4;
4153 nh_grp->ipv4.fi = fi;
4156 err = mlxsw_sp_nexthop4_group_info_init(mlxsw_sp, nh_grp);
4158 goto err_nexthop_group_info_init;
4160 err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp);
4162 goto err_nexthop_group_insert;
4166 err_nexthop_group_insert:
4167 mlxsw_sp_nexthop4_group_info_fini(mlxsw_sp, nh_grp);
4168 err_nexthop_group_info_init:
4171 return ERR_PTR(err);
4175 mlxsw_sp_nexthop4_group_destroy(struct mlxsw_sp *mlxsw_sp,
4176 struct mlxsw_sp_nexthop_group *nh_grp)
4178 mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp);
4179 mlxsw_sp_nexthop4_group_info_fini(mlxsw_sp, nh_grp);
4180 fib_info_put(nh_grp->ipv4.fi);
4184 static int mlxsw_sp_nexthop4_group_get(struct mlxsw_sp *mlxsw_sp,
4185 struct mlxsw_sp_fib_entry *fib_entry,
4186 struct fib_info *fi)
4188 struct mlxsw_sp_nexthop_group *nh_grp;
4190 nh_grp = mlxsw_sp_nexthop4_group_lookup(mlxsw_sp, fi);
4192 nh_grp = mlxsw_sp_nexthop4_group_create(mlxsw_sp, fi);
4194 return PTR_ERR(nh_grp);
4196 list_add_tail(&fib_entry->nexthop_group_node, &nh_grp->fib_list);
4197 fib_entry->nh_group = nh_grp;
4201 static void mlxsw_sp_nexthop4_group_put(struct mlxsw_sp *mlxsw_sp,
4202 struct mlxsw_sp_fib_entry *fib_entry)
4204 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
4206 list_del(&fib_entry->nexthop_group_node);
4207 if (!list_empty(&nh_grp->fib_list))
4209 mlxsw_sp_nexthop4_group_destroy(mlxsw_sp, nh_grp);
4213 mlxsw_sp_fib4_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry)
4215 struct mlxsw_sp_fib4_entry *fib4_entry;
4217 fib4_entry = container_of(fib_entry, struct mlxsw_sp_fib4_entry,
4219 return !fib4_entry->tos;
4223 mlxsw_sp_fib_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry)
4225 struct mlxsw_sp_nexthop_group *nh_group = fib_entry->nh_group;
4227 switch (fib_entry->fib_node->fib->proto) {
4228 case MLXSW_SP_L3_PROTO_IPV4:
4229 if (!mlxsw_sp_fib4_entry_should_offload(fib_entry))
4232 case MLXSW_SP_L3_PROTO_IPV6:
4236 switch (fib_entry->type) {
4237 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
4238 return !!nh_group->nhgi->adj_index_valid;
4239 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
4240 return !!nh_group->nhgi->nh_rif;
4241 case MLXSW_SP_FIB_ENTRY_TYPE_BLACKHOLE:
4242 case MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP:
4243 case MLXSW_SP_FIB_ENTRY_TYPE_NVE_DECAP:
4250 static struct mlxsw_sp_nexthop *
4251 mlxsw_sp_rt6_nexthop(struct mlxsw_sp_nexthop_group *nh_grp,
4252 const struct mlxsw_sp_rt6 *mlxsw_sp_rt6)
4256 for (i = 0; i < nh_grp->nhgi->count; i++) {
4257 struct mlxsw_sp_nexthop *nh = &nh_grp->nhgi->nexthops[i];
4258 struct fib6_info *rt = mlxsw_sp_rt6->rt;
4260 if (nh->rif && nh->rif->dev == rt->fib6_nh->fib_nh_dev &&
4261 ipv6_addr_equal((const struct in6_addr *) &nh->gw_addr,
4262 &rt->fib6_nh->fib_nh_gw6))
4271 mlxsw_sp_fib4_entry_hw_flags_set(struct mlxsw_sp *mlxsw_sp,
4272 struct mlxsw_sp_fib_entry *fib_entry)
4274 u32 *p_dst = (u32 *) fib_entry->fib_node->key.addr;
4275 int dst_len = fib_entry->fib_node->key.prefix_len;
4276 struct mlxsw_sp_fib4_entry *fib4_entry;
4277 struct fib_rt_info fri;
4278 bool should_offload;
4280 should_offload = mlxsw_sp_fib_entry_should_offload(fib_entry);
4281 fib4_entry = container_of(fib_entry, struct mlxsw_sp_fib4_entry,
4283 fri.fi = fib4_entry->fi;
4284 fri.tb_id = fib4_entry->tb_id;
4285 fri.dst = cpu_to_be32(*p_dst);
4286 fri.dst_len = dst_len;
4287 fri.tos = fib4_entry->tos;
4288 fri.type = fib4_entry->type;
4289 fri.offload = should_offload;
4290 fri.trap = !should_offload;
4291 fib_alias_hw_flags_set(mlxsw_sp_net(mlxsw_sp), &fri);
4295 mlxsw_sp_fib4_entry_hw_flags_clear(struct mlxsw_sp *mlxsw_sp,
4296 struct mlxsw_sp_fib_entry *fib_entry)
4298 u32 *p_dst = (u32 *) fib_entry->fib_node->key.addr;
4299 int dst_len = fib_entry->fib_node->key.prefix_len;
4300 struct mlxsw_sp_fib4_entry *fib4_entry;
4301 struct fib_rt_info fri;
4303 fib4_entry = container_of(fib_entry, struct mlxsw_sp_fib4_entry,
4305 fri.fi = fib4_entry->fi;
4306 fri.tb_id = fib4_entry->tb_id;
4307 fri.dst = cpu_to_be32(*p_dst);
4308 fri.dst_len = dst_len;
4309 fri.tos = fib4_entry->tos;
4310 fri.type = fib4_entry->type;
4311 fri.offload = false;
4313 fib_alias_hw_flags_set(mlxsw_sp_net(mlxsw_sp), &fri);
4317 mlxsw_sp_fib6_entry_hw_flags_set(struct mlxsw_sp *mlxsw_sp,
4318 struct mlxsw_sp_fib_entry *fib_entry)
4320 struct mlxsw_sp_fib6_entry *fib6_entry;
4321 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
4322 bool should_offload;
4324 should_offload = mlxsw_sp_fib_entry_should_offload(fib_entry);
4326 /* In IPv6 a multipath route is represented using multiple routes, so
4327 * we need to set the flags on all of them.
4329 fib6_entry = container_of(fib_entry, struct mlxsw_sp_fib6_entry,
4331 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list)
4332 fib6_info_hw_flags_set(mlxsw_sp_rt6->rt, should_offload,
4337 mlxsw_sp_fib6_entry_hw_flags_clear(struct mlxsw_sp *mlxsw_sp,
4338 struct mlxsw_sp_fib_entry *fib_entry)
4340 struct mlxsw_sp_fib6_entry *fib6_entry;
4341 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
4343 fib6_entry = container_of(fib_entry, struct mlxsw_sp_fib6_entry,
4345 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list)
4346 fib6_info_hw_flags_set(mlxsw_sp_rt6->rt, false, false);
4350 mlxsw_sp_fib_entry_hw_flags_set(struct mlxsw_sp *mlxsw_sp,
4351 struct mlxsw_sp_fib_entry *fib_entry)
4353 switch (fib_entry->fib_node->fib->proto) {
4354 case MLXSW_SP_L3_PROTO_IPV4:
4355 mlxsw_sp_fib4_entry_hw_flags_set(mlxsw_sp, fib_entry);
4357 case MLXSW_SP_L3_PROTO_IPV6:
4358 mlxsw_sp_fib6_entry_hw_flags_set(mlxsw_sp, fib_entry);
4364 mlxsw_sp_fib_entry_hw_flags_clear(struct mlxsw_sp *mlxsw_sp,
4365 struct mlxsw_sp_fib_entry *fib_entry)
4367 switch (fib_entry->fib_node->fib->proto) {
4368 case MLXSW_SP_L3_PROTO_IPV4:
4369 mlxsw_sp_fib4_entry_hw_flags_clear(mlxsw_sp, fib_entry);
4371 case MLXSW_SP_L3_PROTO_IPV6:
4372 mlxsw_sp_fib6_entry_hw_flags_clear(mlxsw_sp, fib_entry);
4378 mlxsw_sp_fib_entry_hw_flags_refresh(struct mlxsw_sp *mlxsw_sp,
4379 struct mlxsw_sp_fib_entry *fib_entry,
4380 enum mlxsw_sp_fib_entry_op op)
4383 case MLXSW_SP_FIB_ENTRY_OP_WRITE:
4384 case MLXSW_SP_FIB_ENTRY_OP_UPDATE:
4385 mlxsw_sp_fib_entry_hw_flags_set(mlxsw_sp, fib_entry);
4387 case MLXSW_SP_FIB_ENTRY_OP_DELETE:
4388 mlxsw_sp_fib_entry_hw_flags_clear(mlxsw_sp, fib_entry);
4395 struct mlxsw_sp_fib_entry_op_ctx_basic {
4396 char ralue_pl[MLXSW_REG_RALUE_LEN];
4400 mlxsw_sp_router_ll_basic_fib_entry_pack(struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4401 enum mlxsw_sp_l3proto proto,
4402 enum mlxsw_sp_fib_entry_op op,
4403 u16 virtual_router, u8 prefix_len,
4404 unsigned char *addr,
4405 struct mlxsw_sp_fib_entry_priv *priv)
4407 struct mlxsw_sp_fib_entry_op_ctx_basic *op_ctx_basic = (void *) op_ctx->ll_priv;
4408 enum mlxsw_reg_ralxx_protocol ralxx_proto;
4409 char *ralue_pl = op_ctx_basic->ralue_pl;
4410 enum mlxsw_reg_ralue_op ralue_op;
4412 ralxx_proto = (enum mlxsw_reg_ralxx_protocol) proto;
4415 case MLXSW_SP_FIB_ENTRY_OP_WRITE:
4416 case MLXSW_SP_FIB_ENTRY_OP_UPDATE:
4417 ralue_op = MLXSW_REG_RALUE_OP_WRITE_WRITE;
4419 case MLXSW_SP_FIB_ENTRY_OP_DELETE:
4420 ralue_op = MLXSW_REG_RALUE_OP_WRITE_DELETE;
4428 case MLXSW_SP_L3_PROTO_IPV4:
4429 mlxsw_reg_ralue_pack4(ralue_pl, ralxx_proto, ralue_op,
4430 virtual_router, prefix_len, (u32 *) addr);
4432 case MLXSW_SP_L3_PROTO_IPV6:
4433 mlxsw_reg_ralue_pack6(ralue_pl, ralxx_proto, ralue_op,
4434 virtual_router, prefix_len, addr);
4440 mlxsw_sp_router_ll_basic_fib_entry_act_remote_pack(struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4441 enum mlxsw_reg_ralue_trap_action trap_action,
4442 u16 trap_id, u32 adjacency_index, u16 ecmp_size)
4444 struct mlxsw_sp_fib_entry_op_ctx_basic *op_ctx_basic = (void *) op_ctx->ll_priv;
4446 mlxsw_reg_ralue_act_remote_pack(op_ctx_basic->ralue_pl, trap_action,
4447 trap_id, adjacency_index, ecmp_size);
4451 mlxsw_sp_router_ll_basic_fib_entry_act_local_pack(struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4452 enum mlxsw_reg_ralue_trap_action trap_action,
4453 u16 trap_id, u16 local_erif)
4455 struct mlxsw_sp_fib_entry_op_ctx_basic *op_ctx_basic = (void *) op_ctx->ll_priv;
4457 mlxsw_reg_ralue_act_local_pack(op_ctx_basic->ralue_pl, trap_action,
4458 trap_id, local_erif);
4462 mlxsw_sp_router_ll_basic_fib_entry_act_ip2me_pack(struct mlxsw_sp_fib_entry_op_ctx *op_ctx)
4464 struct mlxsw_sp_fib_entry_op_ctx_basic *op_ctx_basic = (void *) op_ctx->ll_priv;
4466 mlxsw_reg_ralue_act_ip2me_pack(op_ctx_basic->ralue_pl);
4470 mlxsw_sp_router_ll_basic_fib_entry_act_ip2me_tun_pack(struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4473 struct mlxsw_sp_fib_entry_op_ctx_basic *op_ctx_basic = (void *) op_ctx->ll_priv;
4475 mlxsw_reg_ralue_act_ip2me_tun_pack(op_ctx_basic->ralue_pl, tunnel_ptr);
4479 mlxsw_sp_router_ll_basic_fib_entry_commit(struct mlxsw_sp *mlxsw_sp,
4480 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4481 bool *postponed_for_bulk)
4483 struct mlxsw_sp_fib_entry_op_ctx_basic *op_ctx_basic = (void *) op_ctx->ll_priv;
4485 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue),
4486 op_ctx_basic->ralue_pl);
4490 mlxsw_sp_router_ll_basic_fib_entry_is_committed(struct mlxsw_sp_fib_entry_priv *priv)
4495 static void mlxsw_sp_fib_entry_pack(struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4496 struct mlxsw_sp_fib_entry *fib_entry,
4497 enum mlxsw_sp_fib_entry_op op)
4499 struct mlxsw_sp_fib *fib = fib_entry->fib_node->fib;
4501 mlxsw_sp_fib_entry_op_ctx_priv_hold(op_ctx, fib_entry->priv);
4502 fib->ll_ops->fib_entry_pack(op_ctx, fib->proto, op, fib->vr->id,
4503 fib_entry->fib_node->key.prefix_len,
4504 fib_entry->fib_node->key.addr,
4508 int mlxsw_sp_fib_entry_commit(struct mlxsw_sp *mlxsw_sp,
4509 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4510 const struct mlxsw_sp_router_ll_ops *ll_ops)
4512 bool postponed_for_bulk = false;
4515 err = ll_ops->fib_entry_commit(mlxsw_sp, op_ctx, &postponed_for_bulk);
4516 if (!postponed_for_bulk)
4517 mlxsw_sp_fib_entry_op_ctx_priv_put_all(op_ctx);
4521 static int mlxsw_sp_adj_discard_write(struct mlxsw_sp *mlxsw_sp, u16 rif_index)
4523 enum mlxsw_reg_ratr_trap_action trap_action;
4524 char ratr_pl[MLXSW_REG_RATR_LEN];
4527 if (mlxsw_sp->router->adj_discard_index_valid)
4530 err = mlxsw_sp_kvdl_alloc(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ, 1,
4531 &mlxsw_sp->router->adj_discard_index);
4535 trap_action = MLXSW_REG_RATR_TRAP_ACTION_DISCARD_ERRORS;
4536 mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY, true,
4537 MLXSW_REG_RATR_TYPE_ETHERNET,
4538 mlxsw_sp->router->adj_discard_index, rif_index);
4539 mlxsw_reg_ratr_trap_action_set(ratr_pl, trap_action);
4540 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
4542 goto err_ratr_write;
4544 mlxsw_sp->router->adj_discard_index_valid = true;
4549 mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ, 1,
4550 mlxsw_sp->router->adj_discard_index);
4554 static int mlxsw_sp_fib_entry_op_remote(struct mlxsw_sp *mlxsw_sp,
4555 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4556 struct mlxsw_sp_fib_entry *fib_entry,
4557 enum mlxsw_sp_fib_entry_op op)
4559 const struct mlxsw_sp_router_ll_ops *ll_ops = fib_entry->fib_node->fib->ll_ops;
4560 struct mlxsw_sp_nexthop_group *nh_group = fib_entry->nh_group;
4561 struct mlxsw_sp_nexthop_group_info *nhgi = nh_group->nhgi;
4562 enum mlxsw_reg_ralue_trap_action trap_action;
4564 u32 adjacency_index = 0;
4568 /* In case the nexthop group adjacency index is valid, use it
4569 * with provided ECMP size. Otherwise, setup trap and pass
4570 * traffic to kernel.
4572 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
4573 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
4574 adjacency_index = nhgi->adj_index;
4575 ecmp_size = nhgi->ecmp_size;
4576 } else if (!nhgi->adj_index_valid && nhgi->count && nhgi->nh_rif) {
4577 err = mlxsw_sp_adj_discard_write(mlxsw_sp,
4578 nhgi->nh_rif->rif_index);
4581 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
4582 adjacency_index = mlxsw_sp->router->adj_discard_index;
4585 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
4586 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
4589 mlxsw_sp_fib_entry_pack(op_ctx, fib_entry, op);
4590 ll_ops->fib_entry_act_remote_pack(op_ctx, trap_action, trap_id,
4591 adjacency_index, ecmp_size);
4592 return mlxsw_sp_fib_entry_commit(mlxsw_sp, op_ctx, ll_ops);
4595 static int mlxsw_sp_fib_entry_op_local(struct mlxsw_sp *mlxsw_sp,
4596 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4597 struct mlxsw_sp_fib_entry *fib_entry,
4598 enum mlxsw_sp_fib_entry_op op)
4600 const struct mlxsw_sp_router_ll_ops *ll_ops = fib_entry->fib_node->fib->ll_ops;
4601 struct mlxsw_sp_rif *rif = fib_entry->nh_group->nhgi->nh_rif;
4602 enum mlxsw_reg_ralue_trap_action trap_action;
4606 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
4607 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
4608 rif_index = rif->rif_index;
4610 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
4611 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
4614 mlxsw_sp_fib_entry_pack(op_ctx, fib_entry, op);
4615 ll_ops->fib_entry_act_local_pack(op_ctx, trap_action, trap_id, rif_index);
4616 return mlxsw_sp_fib_entry_commit(mlxsw_sp, op_ctx, ll_ops);
4619 static int mlxsw_sp_fib_entry_op_trap(struct mlxsw_sp *mlxsw_sp,
4620 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4621 struct mlxsw_sp_fib_entry *fib_entry,
4622 enum mlxsw_sp_fib_entry_op op)
4624 const struct mlxsw_sp_router_ll_ops *ll_ops = fib_entry->fib_node->fib->ll_ops;
4626 mlxsw_sp_fib_entry_pack(op_ctx, fib_entry, op);
4627 ll_ops->fib_entry_act_ip2me_pack(op_ctx);
4628 return mlxsw_sp_fib_entry_commit(mlxsw_sp, op_ctx, ll_ops);
4631 static int mlxsw_sp_fib_entry_op_blackhole(struct mlxsw_sp *mlxsw_sp,
4632 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4633 struct mlxsw_sp_fib_entry *fib_entry,
4634 enum mlxsw_sp_fib_entry_op op)
4636 const struct mlxsw_sp_router_ll_ops *ll_ops = fib_entry->fib_node->fib->ll_ops;
4637 enum mlxsw_reg_ralue_trap_action trap_action;
4639 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_DISCARD_ERROR;
4640 mlxsw_sp_fib_entry_pack(op_ctx, fib_entry, op);
4641 ll_ops->fib_entry_act_local_pack(op_ctx, trap_action, 0, 0);
4642 return mlxsw_sp_fib_entry_commit(mlxsw_sp, op_ctx, ll_ops);
4646 mlxsw_sp_fib_entry_op_unreachable(struct mlxsw_sp *mlxsw_sp,
4647 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4648 struct mlxsw_sp_fib_entry *fib_entry,
4649 enum mlxsw_sp_fib_entry_op op)
4651 const struct mlxsw_sp_router_ll_ops *ll_ops = fib_entry->fib_node->fib->ll_ops;
4652 enum mlxsw_reg_ralue_trap_action trap_action;
4655 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
4656 trap_id = MLXSW_TRAP_ID_RTR_INGRESS1;
4658 mlxsw_sp_fib_entry_pack(op_ctx, fib_entry, op);
4659 ll_ops->fib_entry_act_local_pack(op_ctx, trap_action, trap_id, 0);
4660 return mlxsw_sp_fib_entry_commit(mlxsw_sp, op_ctx, ll_ops);
4664 mlxsw_sp_fib_entry_op_ipip_decap(struct mlxsw_sp *mlxsw_sp,
4665 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4666 struct mlxsw_sp_fib_entry *fib_entry,
4667 enum mlxsw_sp_fib_entry_op op)
4669 const struct mlxsw_sp_router_ll_ops *ll_ops = fib_entry->fib_node->fib->ll_ops;
4670 struct mlxsw_sp_ipip_entry *ipip_entry = fib_entry->decap.ipip_entry;
4671 const struct mlxsw_sp_ipip_ops *ipip_ops;
4673 if (WARN_ON(!ipip_entry))
4676 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
4677 return ipip_ops->fib_entry_op(mlxsw_sp, ll_ops, op_ctx, ipip_entry, op,
4678 fib_entry->decap.tunnel_index, fib_entry->priv);
4681 static int mlxsw_sp_fib_entry_op_nve_decap(struct mlxsw_sp *mlxsw_sp,
4682 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4683 struct mlxsw_sp_fib_entry *fib_entry,
4684 enum mlxsw_sp_fib_entry_op op)
4686 const struct mlxsw_sp_router_ll_ops *ll_ops = fib_entry->fib_node->fib->ll_ops;
4688 mlxsw_sp_fib_entry_pack(op_ctx, fib_entry, op);
4689 ll_ops->fib_entry_act_ip2me_tun_pack(op_ctx,
4690 fib_entry->decap.tunnel_index);
4691 return mlxsw_sp_fib_entry_commit(mlxsw_sp, op_ctx, ll_ops);
4694 static int __mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
4695 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4696 struct mlxsw_sp_fib_entry *fib_entry,
4697 enum mlxsw_sp_fib_entry_op op)
4699 switch (fib_entry->type) {
4700 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
4701 return mlxsw_sp_fib_entry_op_remote(mlxsw_sp, op_ctx, fib_entry, op);
4702 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
4703 return mlxsw_sp_fib_entry_op_local(mlxsw_sp, op_ctx, fib_entry, op);
4704 case MLXSW_SP_FIB_ENTRY_TYPE_TRAP:
4705 return mlxsw_sp_fib_entry_op_trap(mlxsw_sp, op_ctx, fib_entry, op);
4706 case MLXSW_SP_FIB_ENTRY_TYPE_BLACKHOLE:
4707 return mlxsw_sp_fib_entry_op_blackhole(mlxsw_sp, op_ctx, fib_entry, op);
4708 case MLXSW_SP_FIB_ENTRY_TYPE_UNREACHABLE:
4709 return mlxsw_sp_fib_entry_op_unreachable(mlxsw_sp, op_ctx, fib_entry, op);
4710 case MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP:
4711 return mlxsw_sp_fib_entry_op_ipip_decap(mlxsw_sp, op_ctx, fib_entry, op);
4712 case MLXSW_SP_FIB_ENTRY_TYPE_NVE_DECAP:
4713 return mlxsw_sp_fib_entry_op_nve_decap(mlxsw_sp, op_ctx, fib_entry, op);
4718 static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
4719 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4720 struct mlxsw_sp_fib_entry *fib_entry,
4721 enum mlxsw_sp_fib_entry_op op)
4723 int err = __mlxsw_sp_fib_entry_op(mlxsw_sp, op_ctx, fib_entry, op);
4728 mlxsw_sp_fib_entry_hw_flags_refresh(mlxsw_sp, fib_entry, op);
4733 static int __mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
4734 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4735 struct mlxsw_sp_fib_entry *fib_entry,
4738 return mlxsw_sp_fib_entry_op(mlxsw_sp, op_ctx, fib_entry,
4739 is_new ? MLXSW_SP_FIB_ENTRY_OP_WRITE :
4740 MLXSW_SP_FIB_ENTRY_OP_UPDATE);
4743 static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
4744 struct mlxsw_sp_fib_entry *fib_entry)
4746 struct mlxsw_sp_fib_entry_op_ctx *op_ctx = mlxsw_sp->router->ll_op_ctx;
4748 mlxsw_sp_fib_entry_op_ctx_clear(op_ctx);
4749 return __mlxsw_sp_fib_entry_update(mlxsw_sp, op_ctx, fib_entry, false);
4752 static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
4753 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
4754 struct mlxsw_sp_fib_entry *fib_entry)
4756 const struct mlxsw_sp_router_ll_ops *ll_ops = fib_entry->fib_node->fib->ll_ops;
4758 if (!ll_ops->fib_entry_is_committed(fib_entry->priv))
4760 return mlxsw_sp_fib_entry_op(mlxsw_sp, op_ctx, fib_entry,
4761 MLXSW_SP_FIB_ENTRY_OP_DELETE);
4765 mlxsw_sp_fib4_entry_type_set(struct mlxsw_sp *mlxsw_sp,
4766 const struct fib_entry_notifier_info *fen_info,
4767 struct mlxsw_sp_fib_entry *fib_entry)
4769 struct net_device *dev = fib_info_nh(fen_info->fi, 0)->fib_nh_dev;
4770 union mlxsw_sp_l3addr dip = { .addr4 = htonl(fen_info->dst) };
4771 struct mlxsw_sp_router *router = mlxsw_sp->router;
4772 u32 tb_id = mlxsw_sp_fix_tb_id(fen_info->tb_id);
4773 struct mlxsw_sp_ipip_entry *ipip_entry;
4774 struct fib_info *fi = fen_info->fi;
4776 switch (fen_info->type) {
4778 ipip_entry = mlxsw_sp_ipip_entry_find_by_decap(mlxsw_sp, dev,
4779 MLXSW_SP_L3_PROTO_IPV4, dip);
4780 if (ipip_entry && ipip_entry->ol_dev->flags & IFF_UP) {
4781 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP;
4782 return mlxsw_sp_fib_entry_decap_init(mlxsw_sp,
4786 if (mlxsw_sp_router_nve_is_decap(mlxsw_sp, tb_id,
4787 MLXSW_SP_L3_PROTO_IPV4,
4791 tunnel_index = router->nve_decap_config.tunnel_index;
4792 fib_entry->decap.tunnel_index = tunnel_index;
4793 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_NVE_DECAP;
4798 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
4801 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_BLACKHOLE;
4803 case RTN_UNREACHABLE:
4805 /* Packets hitting these routes need to be trapped, but
4806 * can do so with a lower priority than packets directed
4807 * at the host, so use action type local instead of trap.
4809 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_UNREACHABLE;
4812 if (mlxsw_sp_fi_is_gateway(mlxsw_sp, fi))
4813 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
4815 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
4823 mlxsw_sp_fib4_entry_type_unset(struct mlxsw_sp *mlxsw_sp,
4824 struct mlxsw_sp_fib_entry *fib_entry)
4826 switch (fib_entry->type) {
4827 case MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP:
4828 mlxsw_sp_fib_entry_decap_fini(mlxsw_sp, fib_entry);
4835 static struct mlxsw_sp_fib4_entry *
4836 mlxsw_sp_fib4_entry_create(struct mlxsw_sp *mlxsw_sp,
4837 struct mlxsw_sp_fib_node *fib_node,
4838 const struct fib_entry_notifier_info *fen_info)
4840 struct mlxsw_sp_fib4_entry *fib4_entry;
4841 struct mlxsw_sp_fib_entry *fib_entry;
4844 fib4_entry = kzalloc(sizeof(*fib4_entry), GFP_KERNEL);
4846 return ERR_PTR(-ENOMEM);
4847 fib_entry = &fib4_entry->common;
4849 fib_entry->priv = mlxsw_sp_fib_entry_priv_create(fib_node->fib->ll_ops);
4850 if (IS_ERR(fib_entry->priv)) {
4851 err = PTR_ERR(fib_entry->priv);
4852 goto err_fib_entry_priv_create;
4855 err = mlxsw_sp_fib4_entry_type_set(mlxsw_sp, fen_info, fib_entry);
4857 goto err_fib4_entry_type_set;
4859 err = mlxsw_sp_nexthop4_group_get(mlxsw_sp, fib_entry, fen_info->fi);
4861 goto err_nexthop4_group_get;
4863 fib4_entry->fi = fen_info->fi;
4864 fib_info_hold(fib4_entry->fi);
4865 fib4_entry->tb_id = fen_info->tb_id;
4866 fib4_entry->type = fen_info->type;
4867 fib4_entry->tos = fen_info->tos;
4869 fib_entry->fib_node = fib_node;
4873 err_nexthop4_group_get:
4874 mlxsw_sp_fib4_entry_type_unset(mlxsw_sp, fib_entry);
4875 err_fib4_entry_type_set:
4876 mlxsw_sp_fib_entry_priv_put(fib_entry->priv);
4877 err_fib_entry_priv_create:
4879 return ERR_PTR(err);
4882 static void mlxsw_sp_fib4_entry_destroy(struct mlxsw_sp *mlxsw_sp,
4883 struct mlxsw_sp_fib4_entry *fib4_entry)
4885 fib_info_put(fib4_entry->fi);
4886 mlxsw_sp_nexthop4_group_put(mlxsw_sp, &fib4_entry->common);
4887 mlxsw_sp_fib4_entry_type_unset(mlxsw_sp, &fib4_entry->common);
4888 mlxsw_sp_fib_entry_priv_put(fib4_entry->common.priv);
4892 static struct mlxsw_sp_fib4_entry *
4893 mlxsw_sp_fib4_entry_lookup(struct mlxsw_sp *mlxsw_sp,
4894 const struct fib_entry_notifier_info *fen_info)
4896 struct mlxsw_sp_fib4_entry *fib4_entry;
4897 struct mlxsw_sp_fib_node *fib_node;
4898 struct mlxsw_sp_fib *fib;
4899 struct mlxsw_sp_vr *vr;
4901 vr = mlxsw_sp_vr_find(mlxsw_sp, fen_info->tb_id);
4904 fib = mlxsw_sp_vr_fib(vr, MLXSW_SP_L3_PROTO_IPV4);
4906 fib_node = mlxsw_sp_fib_node_lookup(fib, &fen_info->dst,
4907 sizeof(fen_info->dst),
4912 fib4_entry = container_of(fib_node->fib_entry,
4913 struct mlxsw_sp_fib4_entry, common);
4914 if (fib4_entry->tb_id == fen_info->tb_id &&
4915 fib4_entry->tos == fen_info->tos &&
4916 fib4_entry->type == fen_info->type &&
4917 fib4_entry->fi == fen_info->fi)
4923 static const struct rhashtable_params mlxsw_sp_fib_ht_params = {
4924 .key_offset = offsetof(struct mlxsw_sp_fib_node, key),
4925 .head_offset = offsetof(struct mlxsw_sp_fib_node, ht_node),
4926 .key_len = sizeof(struct mlxsw_sp_fib_key),
4927 .automatic_shrinking = true,
4930 static int mlxsw_sp_fib_node_insert(struct mlxsw_sp_fib *fib,
4931 struct mlxsw_sp_fib_node *fib_node)
4933 return rhashtable_insert_fast(&fib->ht, &fib_node->ht_node,
4934 mlxsw_sp_fib_ht_params);
4937 static void mlxsw_sp_fib_node_remove(struct mlxsw_sp_fib *fib,
4938 struct mlxsw_sp_fib_node *fib_node)
4940 rhashtable_remove_fast(&fib->ht, &fib_node->ht_node,
4941 mlxsw_sp_fib_ht_params);
4944 static struct mlxsw_sp_fib_node *
4945 mlxsw_sp_fib_node_lookup(struct mlxsw_sp_fib *fib, const void *addr,
4946 size_t addr_len, unsigned char prefix_len)
4948 struct mlxsw_sp_fib_key key;
4950 memset(&key, 0, sizeof(key));
4951 memcpy(key.addr, addr, addr_len);
4952 key.prefix_len = prefix_len;
4953 return rhashtable_lookup_fast(&fib->ht, &key, mlxsw_sp_fib_ht_params);
4956 static struct mlxsw_sp_fib_node *
4957 mlxsw_sp_fib_node_create(struct mlxsw_sp_fib *fib, const void *addr,
4958 size_t addr_len, unsigned char prefix_len)
4960 struct mlxsw_sp_fib_node *fib_node;
4962 fib_node = kzalloc(sizeof(*fib_node), GFP_KERNEL);
4966 list_add(&fib_node->list, &fib->node_list);
4967 memcpy(fib_node->key.addr, addr, addr_len);
4968 fib_node->key.prefix_len = prefix_len;
4973 static void mlxsw_sp_fib_node_destroy(struct mlxsw_sp_fib_node *fib_node)
4975 list_del(&fib_node->list);
4979 static int mlxsw_sp_fib_lpm_tree_link(struct mlxsw_sp *mlxsw_sp,
4980 struct mlxsw_sp_fib_node *fib_node)
4982 struct mlxsw_sp_prefix_usage req_prefix_usage;
4983 struct mlxsw_sp_fib *fib = fib_node->fib;
4984 struct mlxsw_sp_lpm_tree *lpm_tree;
4987 lpm_tree = mlxsw_sp->router->lpm.proto_trees[fib->proto];
4988 if (lpm_tree->prefix_ref_count[fib_node->key.prefix_len] != 0)
4991 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage, &lpm_tree->prefix_usage);
4992 mlxsw_sp_prefix_usage_set(&req_prefix_usage, fib_node->key.prefix_len);
4993 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
4995 if (IS_ERR(lpm_tree))
4996 return PTR_ERR(lpm_tree);
4998 err = mlxsw_sp_vrs_lpm_tree_replace(mlxsw_sp, fib, lpm_tree);
5000 goto err_lpm_tree_replace;
5003 lpm_tree->prefix_ref_count[fib_node->key.prefix_len]++;
5006 err_lpm_tree_replace:
5007 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
5011 static void mlxsw_sp_fib_lpm_tree_unlink(struct mlxsw_sp *mlxsw_sp,
5012 struct mlxsw_sp_fib_node *fib_node)
5014 struct mlxsw_sp_lpm_tree *lpm_tree = fib_node->fib->lpm_tree;
5015 struct mlxsw_sp_prefix_usage req_prefix_usage;
5016 struct mlxsw_sp_fib *fib = fib_node->fib;
5019 if (--lpm_tree->prefix_ref_count[fib_node->key.prefix_len] != 0)
5021 /* Try to construct a new LPM tree from the current prefix usage
5022 * minus the unused one. If we fail, continue using the old one.
5024 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage, &lpm_tree->prefix_usage);
5025 mlxsw_sp_prefix_usage_clear(&req_prefix_usage,
5026 fib_node->key.prefix_len);
5027 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
5029 if (IS_ERR(lpm_tree))
5032 err = mlxsw_sp_vrs_lpm_tree_replace(mlxsw_sp, fib, lpm_tree);
5034 goto err_lpm_tree_replace;
5038 err_lpm_tree_replace:
5039 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
5042 static int mlxsw_sp_fib_node_init(struct mlxsw_sp *mlxsw_sp,
5043 struct mlxsw_sp_fib_node *fib_node,
5044 struct mlxsw_sp_fib *fib)
5048 err = mlxsw_sp_fib_node_insert(fib, fib_node);
5051 fib_node->fib = fib;
5053 err = mlxsw_sp_fib_lpm_tree_link(mlxsw_sp, fib_node);
5055 goto err_fib_lpm_tree_link;
5059 err_fib_lpm_tree_link:
5060 fib_node->fib = NULL;
5061 mlxsw_sp_fib_node_remove(fib, fib_node);
5065 static void mlxsw_sp_fib_node_fini(struct mlxsw_sp *mlxsw_sp,
5066 struct mlxsw_sp_fib_node *fib_node)
5068 struct mlxsw_sp_fib *fib = fib_node->fib;
5070 mlxsw_sp_fib_lpm_tree_unlink(mlxsw_sp, fib_node);
5071 fib_node->fib = NULL;
5072 mlxsw_sp_fib_node_remove(fib, fib_node);
5075 static struct mlxsw_sp_fib_node *
5076 mlxsw_sp_fib_node_get(struct mlxsw_sp *mlxsw_sp, u32 tb_id, const void *addr,
5077 size_t addr_len, unsigned char prefix_len,
5078 enum mlxsw_sp_l3proto proto)
5080 struct mlxsw_sp_fib_node *fib_node;
5081 struct mlxsw_sp_fib *fib;
5082 struct mlxsw_sp_vr *vr;
5085 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id, NULL);
5087 return ERR_CAST(vr);
5088 fib = mlxsw_sp_vr_fib(vr, proto);
5090 fib_node = mlxsw_sp_fib_node_lookup(fib, addr, addr_len, prefix_len);
5094 fib_node = mlxsw_sp_fib_node_create(fib, addr, addr_len, prefix_len);
5097 goto err_fib_node_create;
5100 err = mlxsw_sp_fib_node_init(mlxsw_sp, fib_node, fib);
5102 goto err_fib_node_init;
5107 mlxsw_sp_fib_node_destroy(fib_node);
5108 err_fib_node_create:
5109 mlxsw_sp_vr_put(mlxsw_sp, vr);
5110 return ERR_PTR(err);
5113 static void mlxsw_sp_fib_node_put(struct mlxsw_sp *mlxsw_sp,
5114 struct mlxsw_sp_fib_node *fib_node)
5116 struct mlxsw_sp_vr *vr = fib_node->fib->vr;
5118 if (fib_node->fib_entry)
5120 mlxsw_sp_fib_node_fini(mlxsw_sp, fib_node);
5121 mlxsw_sp_fib_node_destroy(fib_node);
5122 mlxsw_sp_vr_put(mlxsw_sp, vr);
5125 static int mlxsw_sp_fib_node_entry_link(struct mlxsw_sp *mlxsw_sp,
5126 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5127 struct mlxsw_sp_fib_entry *fib_entry)
5129 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
5130 bool is_new = !fib_node->fib_entry;
5133 fib_node->fib_entry = fib_entry;
5135 err = __mlxsw_sp_fib_entry_update(mlxsw_sp, op_ctx, fib_entry, is_new);
5137 goto err_fib_entry_update;
5141 err_fib_entry_update:
5142 fib_node->fib_entry = NULL;
5146 static int __mlxsw_sp_fib_node_entry_unlink(struct mlxsw_sp *mlxsw_sp,
5147 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5148 struct mlxsw_sp_fib_entry *fib_entry)
5150 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
5153 err = mlxsw_sp_fib_entry_del(mlxsw_sp, op_ctx, fib_entry);
5154 fib_node->fib_entry = NULL;
5158 static void mlxsw_sp_fib_node_entry_unlink(struct mlxsw_sp *mlxsw_sp,
5159 struct mlxsw_sp_fib_entry *fib_entry)
5161 struct mlxsw_sp_fib_entry_op_ctx *op_ctx = mlxsw_sp->router->ll_op_ctx;
5163 mlxsw_sp_fib_entry_op_ctx_clear(op_ctx);
5164 __mlxsw_sp_fib_node_entry_unlink(mlxsw_sp, op_ctx, fib_entry);
5167 static bool mlxsw_sp_fib4_allow_replace(struct mlxsw_sp_fib4_entry *fib4_entry)
5169 struct mlxsw_sp_fib_node *fib_node = fib4_entry->common.fib_node;
5170 struct mlxsw_sp_fib4_entry *fib4_replaced;
5172 if (!fib_node->fib_entry)
5175 fib4_replaced = container_of(fib_node->fib_entry,
5176 struct mlxsw_sp_fib4_entry, common);
5177 if (fib4_entry->tb_id == RT_TABLE_MAIN &&
5178 fib4_replaced->tb_id == RT_TABLE_LOCAL)
5185 mlxsw_sp_router_fib4_replace(struct mlxsw_sp *mlxsw_sp,
5186 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5187 const struct fib_entry_notifier_info *fen_info)
5189 struct mlxsw_sp_fib4_entry *fib4_entry, *fib4_replaced;
5190 struct mlxsw_sp_fib_entry *replaced;
5191 struct mlxsw_sp_fib_node *fib_node;
5194 if (mlxsw_sp->router->aborted)
5197 fib_node = mlxsw_sp_fib_node_get(mlxsw_sp, fen_info->tb_id,
5198 &fen_info->dst, sizeof(fen_info->dst),
5200 MLXSW_SP_L3_PROTO_IPV4);
5201 if (IS_ERR(fib_node)) {
5202 dev_warn(mlxsw_sp->bus_info->dev, "Failed to get FIB node\n");
5203 return PTR_ERR(fib_node);
5206 fib4_entry = mlxsw_sp_fib4_entry_create(mlxsw_sp, fib_node, fen_info);
5207 if (IS_ERR(fib4_entry)) {
5208 dev_warn(mlxsw_sp->bus_info->dev, "Failed to create FIB entry\n");
5209 err = PTR_ERR(fib4_entry);
5210 goto err_fib4_entry_create;
5213 if (!mlxsw_sp_fib4_allow_replace(fib4_entry)) {
5214 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
5215 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5219 replaced = fib_node->fib_entry;
5220 err = mlxsw_sp_fib_node_entry_link(mlxsw_sp, op_ctx, &fib4_entry->common);
5222 dev_warn(mlxsw_sp->bus_info->dev, "Failed to link FIB entry to node\n");
5223 goto err_fib_node_entry_link;
5226 /* Nothing to replace */
5230 mlxsw_sp_fib_entry_hw_flags_clear(mlxsw_sp, replaced);
5231 fib4_replaced = container_of(replaced, struct mlxsw_sp_fib4_entry,
5233 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_replaced);
5237 err_fib_node_entry_link:
5238 fib_node->fib_entry = replaced;
5239 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
5240 err_fib4_entry_create:
5241 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5245 static int mlxsw_sp_router_fib4_del(struct mlxsw_sp *mlxsw_sp,
5246 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5247 struct fib_entry_notifier_info *fen_info)
5249 struct mlxsw_sp_fib4_entry *fib4_entry;
5250 struct mlxsw_sp_fib_node *fib_node;
5253 if (mlxsw_sp->router->aborted)
5256 fib4_entry = mlxsw_sp_fib4_entry_lookup(mlxsw_sp, fen_info);
5259 fib_node = fib4_entry->common.fib_node;
5261 err = __mlxsw_sp_fib_node_entry_unlink(mlxsw_sp, op_ctx, &fib4_entry->common);
5262 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
5263 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5267 static bool mlxsw_sp_fib6_rt_should_ignore(const struct fib6_info *rt)
5269 /* Multicast routes aren't supported, so ignore them. Neighbour
5270 * Discovery packets are specifically trapped.
5272 if (ipv6_addr_type(&rt->fib6_dst.addr) & IPV6_ADDR_MULTICAST)
5275 /* Cloned routes are irrelevant in the forwarding path. */
5276 if (rt->fib6_flags & RTF_CACHE)
5282 static struct mlxsw_sp_rt6 *mlxsw_sp_rt6_create(struct fib6_info *rt)
5284 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
5286 mlxsw_sp_rt6 = kzalloc(sizeof(*mlxsw_sp_rt6), GFP_KERNEL);
5288 return ERR_PTR(-ENOMEM);
5290 /* In case of route replace, replaced route is deleted with
5291 * no notification. Take reference to prevent accessing freed
5294 mlxsw_sp_rt6->rt = rt;
5297 return mlxsw_sp_rt6;
5300 #if IS_ENABLED(CONFIG_IPV6)
5301 static void mlxsw_sp_rt6_release(struct fib6_info *rt)
5303 fib6_info_release(rt);
5306 static void mlxsw_sp_rt6_release(struct fib6_info *rt)
5311 static void mlxsw_sp_rt6_destroy(struct mlxsw_sp_rt6 *mlxsw_sp_rt6)
5313 struct fib6_nh *fib6_nh = mlxsw_sp_rt6->rt->fib6_nh;
5315 fib6_nh->fib_nh_flags &= ~RTNH_F_OFFLOAD;
5316 mlxsw_sp_rt6_release(mlxsw_sp_rt6->rt);
5317 kfree(mlxsw_sp_rt6);
5320 static struct fib6_info *
5321 mlxsw_sp_fib6_entry_rt(const struct mlxsw_sp_fib6_entry *fib6_entry)
5323 return list_first_entry(&fib6_entry->rt6_list, struct mlxsw_sp_rt6,
5327 static struct mlxsw_sp_rt6 *
5328 mlxsw_sp_fib6_entry_rt_find(const struct mlxsw_sp_fib6_entry *fib6_entry,
5329 const struct fib6_info *rt)
5331 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
5333 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
5334 if (mlxsw_sp_rt6->rt == rt)
5335 return mlxsw_sp_rt6;
5341 static bool mlxsw_sp_nexthop6_ipip_type(const struct mlxsw_sp *mlxsw_sp,
5342 const struct fib6_info *rt,
5343 enum mlxsw_sp_ipip_type *ret)
5345 return rt->fib6_nh->fib_nh_dev &&
5346 mlxsw_sp_netdev_ipip_type(mlxsw_sp, rt->fib6_nh->fib_nh_dev, ret);
5349 static int mlxsw_sp_nexthop6_init(struct mlxsw_sp *mlxsw_sp,
5350 struct mlxsw_sp_nexthop_group *nh_grp,
5351 struct mlxsw_sp_nexthop *nh,
5352 const struct fib6_info *rt)
5354 struct net_device *dev = rt->fib6_nh->fib_nh_dev;
5356 nh->nhgi = nh_grp->nhgi;
5357 nh->nh_weight = rt->fib6_nh->fib_nh_weight;
5358 memcpy(&nh->gw_addr, &rt->fib6_nh->fib_nh_gw6, sizeof(nh->gw_addr));
5359 #if IS_ENABLED(CONFIG_IPV6)
5360 nh->neigh_tbl = &nd_tbl;
5362 mlxsw_sp_nexthop_counter_alloc(mlxsw_sp, nh);
5364 list_add_tail(&nh->router_list_node, &mlxsw_sp->router->nexthop_list);
5368 nh->ifindex = dev->ifindex;
5370 return mlxsw_sp_nexthop_type_init(mlxsw_sp, nh, dev);
5373 static void mlxsw_sp_nexthop6_fini(struct mlxsw_sp *mlxsw_sp,
5374 struct mlxsw_sp_nexthop *nh)
5376 mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
5377 list_del(&nh->router_list_node);
5378 mlxsw_sp_nexthop_counter_free(mlxsw_sp, nh);
5381 static bool mlxsw_sp_rt6_is_gateway(const struct mlxsw_sp *mlxsw_sp,
5382 const struct fib6_info *rt)
5384 return rt->fib6_nh->fib_nh_gw_family ||
5385 mlxsw_sp_nexthop6_ipip_type(mlxsw_sp, rt, NULL);
5389 mlxsw_sp_nexthop6_group_info_init(struct mlxsw_sp *mlxsw_sp,
5390 struct mlxsw_sp_nexthop_group *nh_grp,
5391 struct mlxsw_sp_fib6_entry *fib6_entry)
5393 struct mlxsw_sp_nexthop_group_info *nhgi;
5394 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
5395 struct mlxsw_sp_nexthop *nh;
5398 nhgi = kzalloc(struct_size(nhgi, nexthops, fib6_entry->nrt6),
5402 nh_grp->nhgi = nhgi;
5403 nhgi->nh_grp = nh_grp;
5404 mlxsw_sp_rt6 = list_first_entry(&fib6_entry->rt6_list,
5405 struct mlxsw_sp_rt6, list);
5406 nhgi->gateway = mlxsw_sp_rt6_is_gateway(mlxsw_sp, mlxsw_sp_rt6->rt);
5407 nhgi->count = fib6_entry->nrt6;
5408 for (i = 0; i < nhgi->count; i++) {
5409 struct fib6_info *rt = mlxsw_sp_rt6->rt;
5411 nh = &nhgi->nexthops[i];
5412 err = mlxsw_sp_nexthop6_init(mlxsw_sp, nh_grp, nh, rt);
5414 goto err_nexthop6_init;
5415 mlxsw_sp_rt6 = list_next_entry(mlxsw_sp_rt6, list);
5417 nh_grp->nhgi = nhgi;
5418 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
5423 for (i--; i >= 0; i--) {
5424 nh = &nhgi->nexthops[i];
5425 mlxsw_sp_nexthop6_fini(mlxsw_sp, nh);
5432 mlxsw_sp_nexthop6_group_info_fini(struct mlxsw_sp *mlxsw_sp,
5433 struct mlxsw_sp_nexthop_group *nh_grp)
5435 struct mlxsw_sp_nexthop_group_info *nhgi = nh_grp->nhgi;
5438 for (i = nhgi->count - 1; i >= 0; i--) {
5439 struct mlxsw_sp_nexthop *nh = &nhgi->nexthops[i];
5441 mlxsw_sp_nexthop6_fini(mlxsw_sp, nh);
5443 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
5444 WARN_ON_ONCE(nhgi->adj_index_valid);
5448 static struct mlxsw_sp_nexthop_group *
5449 mlxsw_sp_nexthop6_group_create(struct mlxsw_sp *mlxsw_sp,
5450 struct mlxsw_sp_fib6_entry *fib6_entry)
5452 struct mlxsw_sp_nexthop_group *nh_grp;
5455 nh_grp = kzalloc(sizeof(*nh_grp), GFP_KERNEL);
5457 return ERR_PTR(-ENOMEM);
5458 INIT_LIST_HEAD(&nh_grp->fib_list);
5459 nh_grp->type = MLXSW_SP_NEXTHOP_GROUP_TYPE_IPV6;
5461 err = mlxsw_sp_nexthop6_group_info_init(mlxsw_sp, nh_grp, fib6_entry);
5463 goto err_nexthop_group_info_init;
5465 err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp);
5467 goto err_nexthop_group_insert;
5471 err_nexthop_group_insert:
5472 mlxsw_sp_nexthop6_group_info_fini(mlxsw_sp, nh_grp);
5473 err_nexthop_group_info_init:
5475 return ERR_PTR(err);
5479 mlxsw_sp_nexthop6_group_destroy(struct mlxsw_sp *mlxsw_sp,
5480 struct mlxsw_sp_nexthop_group *nh_grp)
5482 mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp);
5483 mlxsw_sp_nexthop6_group_info_fini(mlxsw_sp, nh_grp);
5487 static int mlxsw_sp_nexthop6_group_get(struct mlxsw_sp *mlxsw_sp,
5488 struct mlxsw_sp_fib6_entry *fib6_entry)
5490 struct mlxsw_sp_nexthop_group *nh_grp;
5492 nh_grp = mlxsw_sp_nexthop6_group_lookup(mlxsw_sp, fib6_entry);
5494 nh_grp = mlxsw_sp_nexthop6_group_create(mlxsw_sp, fib6_entry);
5496 return PTR_ERR(nh_grp);
5499 list_add_tail(&fib6_entry->common.nexthop_group_node,
5501 fib6_entry->common.nh_group = nh_grp;
5503 /* The route and the nexthop are described by the same struct, so we
5504 * need to the update the nexthop offload indication for the new route.
5506 __mlxsw_sp_nexthop6_group_offload_refresh(nh_grp, fib6_entry);
5511 static void mlxsw_sp_nexthop6_group_put(struct mlxsw_sp *mlxsw_sp,
5512 struct mlxsw_sp_fib_entry *fib_entry)
5514 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
5516 list_del(&fib_entry->nexthop_group_node);
5517 if (!list_empty(&nh_grp->fib_list))
5519 mlxsw_sp_nexthop6_group_destroy(mlxsw_sp, nh_grp);
5522 static int mlxsw_sp_nexthop6_group_update(struct mlxsw_sp *mlxsw_sp,
5523 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5524 struct mlxsw_sp_fib6_entry *fib6_entry)
5526 struct mlxsw_sp_nexthop_group *old_nh_grp = fib6_entry->common.nh_group;
5529 fib6_entry->common.nh_group = NULL;
5530 list_del(&fib6_entry->common.nexthop_group_node);
5532 err = mlxsw_sp_nexthop6_group_get(mlxsw_sp, fib6_entry);
5534 goto err_nexthop6_group_get;
5536 /* In case this entry is offloaded, then the adjacency index
5537 * currently associated with it in the device's table is that
5538 * of the old group. Start using the new one instead.
5540 err = __mlxsw_sp_fib_entry_update(mlxsw_sp, op_ctx,
5541 &fib6_entry->common, false);
5543 goto err_fib_entry_update;
5545 if (list_empty(&old_nh_grp->fib_list))
5546 mlxsw_sp_nexthop6_group_destroy(mlxsw_sp, old_nh_grp);
5550 err_fib_entry_update:
5551 mlxsw_sp_nexthop6_group_put(mlxsw_sp, &fib6_entry->common);
5552 err_nexthop6_group_get:
5553 list_add_tail(&fib6_entry->common.nexthop_group_node,
5554 &old_nh_grp->fib_list);
5555 fib6_entry->common.nh_group = old_nh_grp;
5560 mlxsw_sp_fib6_entry_nexthop_add(struct mlxsw_sp *mlxsw_sp,
5561 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5562 struct mlxsw_sp_fib6_entry *fib6_entry,
5563 struct fib6_info **rt_arr, unsigned int nrt6)
5565 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
5568 for (i = 0; i < nrt6; i++) {
5569 mlxsw_sp_rt6 = mlxsw_sp_rt6_create(rt_arr[i]);
5570 if (IS_ERR(mlxsw_sp_rt6)) {
5571 err = PTR_ERR(mlxsw_sp_rt6);
5572 goto err_rt6_create;
5575 list_add_tail(&mlxsw_sp_rt6->list, &fib6_entry->rt6_list);
5579 err = mlxsw_sp_nexthop6_group_update(mlxsw_sp, op_ctx, fib6_entry);
5581 goto err_nexthop6_group_update;
5585 err_nexthop6_group_update:
5588 for (i--; i >= 0; i--) {
5590 mlxsw_sp_rt6 = list_last_entry(&fib6_entry->rt6_list,
5591 struct mlxsw_sp_rt6, list);
5592 list_del(&mlxsw_sp_rt6->list);
5593 mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
5599 mlxsw_sp_fib6_entry_nexthop_del(struct mlxsw_sp *mlxsw_sp,
5600 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5601 struct mlxsw_sp_fib6_entry *fib6_entry,
5602 struct fib6_info **rt_arr, unsigned int nrt6)
5604 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
5607 for (i = 0; i < nrt6; i++) {
5608 mlxsw_sp_rt6 = mlxsw_sp_fib6_entry_rt_find(fib6_entry,
5610 if (WARN_ON_ONCE(!mlxsw_sp_rt6))
5614 list_del(&mlxsw_sp_rt6->list);
5615 mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
5618 mlxsw_sp_nexthop6_group_update(mlxsw_sp, op_ctx, fib6_entry);
5621 static void mlxsw_sp_fib6_entry_type_set(struct mlxsw_sp *mlxsw_sp,
5622 struct mlxsw_sp_fib_entry *fib_entry,
5623 const struct fib6_info *rt)
5625 if (rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
5626 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
5627 else if (rt->fib6_type == RTN_BLACKHOLE)
5628 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_BLACKHOLE;
5629 else if (rt->fib6_flags & RTF_REJECT)
5630 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_UNREACHABLE;
5631 else if (mlxsw_sp_rt6_is_gateway(mlxsw_sp, rt))
5632 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
5634 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
5638 mlxsw_sp_fib6_entry_rt_destroy_all(struct mlxsw_sp_fib6_entry *fib6_entry)
5640 struct mlxsw_sp_rt6 *mlxsw_sp_rt6, *tmp;
5642 list_for_each_entry_safe(mlxsw_sp_rt6, tmp, &fib6_entry->rt6_list,
5645 list_del(&mlxsw_sp_rt6->list);
5646 mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
5650 static struct mlxsw_sp_fib6_entry *
5651 mlxsw_sp_fib6_entry_create(struct mlxsw_sp *mlxsw_sp,
5652 struct mlxsw_sp_fib_node *fib_node,
5653 struct fib6_info **rt_arr, unsigned int nrt6)
5655 struct mlxsw_sp_fib6_entry *fib6_entry;
5656 struct mlxsw_sp_fib_entry *fib_entry;
5657 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
5660 fib6_entry = kzalloc(sizeof(*fib6_entry), GFP_KERNEL);
5662 return ERR_PTR(-ENOMEM);
5663 fib_entry = &fib6_entry->common;
5665 fib_entry->priv = mlxsw_sp_fib_entry_priv_create(fib_node->fib->ll_ops);
5666 if (IS_ERR(fib_entry->priv)) {
5667 err = PTR_ERR(fib_entry->priv);
5668 goto err_fib_entry_priv_create;
5671 INIT_LIST_HEAD(&fib6_entry->rt6_list);
5673 for (i = 0; i < nrt6; i++) {
5674 mlxsw_sp_rt6 = mlxsw_sp_rt6_create(rt_arr[i]);
5675 if (IS_ERR(mlxsw_sp_rt6)) {
5676 err = PTR_ERR(mlxsw_sp_rt6);
5677 goto err_rt6_create;
5679 list_add_tail(&mlxsw_sp_rt6->list, &fib6_entry->rt6_list);
5683 mlxsw_sp_fib6_entry_type_set(mlxsw_sp, fib_entry, rt_arr[0]);
5685 err = mlxsw_sp_nexthop6_group_get(mlxsw_sp, fib6_entry);
5687 goto err_nexthop6_group_get;
5689 fib_entry->fib_node = fib_node;
5693 err_nexthop6_group_get:
5696 for (i--; i >= 0; i--) {
5698 mlxsw_sp_rt6 = list_last_entry(&fib6_entry->rt6_list,
5699 struct mlxsw_sp_rt6, list);
5700 list_del(&mlxsw_sp_rt6->list);
5701 mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
5703 mlxsw_sp_fib_entry_priv_put(fib_entry->priv);
5704 err_fib_entry_priv_create:
5706 return ERR_PTR(err);
5709 static void mlxsw_sp_fib6_entry_destroy(struct mlxsw_sp *mlxsw_sp,
5710 struct mlxsw_sp_fib6_entry *fib6_entry)
5712 mlxsw_sp_nexthop6_group_put(mlxsw_sp, &fib6_entry->common);
5713 mlxsw_sp_fib6_entry_rt_destroy_all(fib6_entry);
5714 WARN_ON(fib6_entry->nrt6);
5715 mlxsw_sp_fib_entry_priv_put(fib6_entry->common.priv);
5719 static struct mlxsw_sp_fib6_entry *
5720 mlxsw_sp_fib6_entry_lookup(struct mlxsw_sp *mlxsw_sp,
5721 const struct fib6_info *rt)
5723 struct mlxsw_sp_fib6_entry *fib6_entry;
5724 struct mlxsw_sp_fib_node *fib_node;
5725 struct mlxsw_sp_fib *fib;
5726 struct fib6_info *cmp_rt;
5727 struct mlxsw_sp_vr *vr;
5729 vr = mlxsw_sp_vr_find(mlxsw_sp, rt->fib6_table->tb6_id);
5732 fib = mlxsw_sp_vr_fib(vr, MLXSW_SP_L3_PROTO_IPV6);
5734 fib_node = mlxsw_sp_fib_node_lookup(fib, &rt->fib6_dst.addr,
5735 sizeof(rt->fib6_dst.addr),
5740 fib6_entry = container_of(fib_node->fib_entry,
5741 struct mlxsw_sp_fib6_entry, common);
5742 cmp_rt = mlxsw_sp_fib6_entry_rt(fib6_entry);
5743 if (rt->fib6_table->tb6_id == cmp_rt->fib6_table->tb6_id &&
5744 rt->fib6_metric == cmp_rt->fib6_metric &&
5745 mlxsw_sp_fib6_entry_rt_find(fib6_entry, rt))
5751 static bool mlxsw_sp_fib6_allow_replace(struct mlxsw_sp_fib6_entry *fib6_entry)
5753 struct mlxsw_sp_fib_node *fib_node = fib6_entry->common.fib_node;
5754 struct mlxsw_sp_fib6_entry *fib6_replaced;
5755 struct fib6_info *rt, *rt_replaced;
5757 if (!fib_node->fib_entry)
5760 fib6_replaced = container_of(fib_node->fib_entry,
5761 struct mlxsw_sp_fib6_entry,
5763 rt = mlxsw_sp_fib6_entry_rt(fib6_entry);
5764 rt_replaced = mlxsw_sp_fib6_entry_rt(fib6_replaced);
5765 if (rt->fib6_table->tb6_id == RT_TABLE_MAIN &&
5766 rt_replaced->fib6_table->tb6_id == RT_TABLE_LOCAL)
5772 static int mlxsw_sp_router_fib6_replace(struct mlxsw_sp *mlxsw_sp,
5773 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5774 struct fib6_info **rt_arr, unsigned int nrt6)
5776 struct mlxsw_sp_fib6_entry *fib6_entry, *fib6_replaced;
5777 struct mlxsw_sp_fib_entry *replaced;
5778 struct mlxsw_sp_fib_node *fib_node;
5779 struct fib6_info *rt = rt_arr[0];
5782 if (mlxsw_sp->router->aborted)
5785 if (rt->fib6_src.plen)
5788 if (mlxsw_sp_fib6_rt_should_ignore(rt))
5791 fib_node = mlxsw_sp_fib_node_get(mlxsw_sp, rt->fib6_table->tb6_id,
5793 sizeof(rt->fib6_dst.addr),
5795 MLXSW_SP_L3_PROTO_IPV6);
5796 if (IS_ERR(fib_node))
5797 return PTR_ERR(fib_node);
5799 fib6_entry = mlxsw_sp_fib6_entry_create(mlxsw_sp, fib_node, rt_arr,
5801 if (IS_ERR(fib6_entry)) {
5802 err = PTR_ERR(fib6_entry);
5803 goto err_fib6_entry_create;
5806 if (!mlxsw_sp_fib6_allow_replace(fib6_entry)) {
5807 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
5808 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5812 replaced = fib_node->fib_entry;
5813 err = mlxsw_sp_fib_node_entry_link(mlxsw_sp, op_ctx, &fib6_entry->common);
5815 goto err_fib_node_entry_link;
5817 /* Nothing to replace */
5821 mlxsw_sp_fib_entry_hw_flags_clear(mlxsw_sp, replaced);
5822 fib6_replaced = container_of(replaced, struct mlxsw_sp_fib6_entry,
5824 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_replaced);
5828 err_fib_node_entry_link:
5829 fib_node->fib_entry = replaced;
5830 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
5831 err_fib6_entry_create:
5832 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5836 static int mlxsw_sp_router_fib6_append(struct mlxsw_sp *mlxsw_sp,
5837 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5838 struct fib6_info **rt_arr, unsigned int nrt6)
5840 struct mlxsw_sp_fib6_entry *fib6_entry;
5841 struct mlxsw_sp_fib_node *fib_node;
5842 struct fib6_info *rt = rt_arr[0];
5845 if (mlxsw_sp->router->aborted)
5848 if (rt->fib6_src.plen)
5851 if (mlxsw_sp_fib6_rt_should_ignore(rt))
5854 fib_node = mlxsw_sp_fib_node_get(mlxsw_sp, rt->fib6_table->tb6_id,
5856 sizeof(rt->fib6_dst.addr),
5858 MLXSW_SP_L3_PROTO_IPV6);
5859 if (IS_ERR(fib_node))
5860 return PTR_ERR(fib_node);
5862 if (WARN_ON_ONCE(!fib_node->fib_entry)) {
5863 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5867 fib6_entry = container_of(fib_node->fib_entry,
5868 struct mlxsw_sp_fib6_entry, common);
5869 err = mlxsw_sp_fib6_entry_nexthop_add(mlxsw_sp, op_ctx, fib6_entry, rt_arr, nrt6);
5871 goto err_fib6_entry_nexthop_add;
5875 err_fib6_entry_nexthop_add:
5876 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5880 static int mlxsw_sp_router_fib6_del(struct mlxsw_sp *mlxsw_sp,
5881 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
5882 struct fib6_info **rt_arr, unsigned int nrt6)
5884 struct mlxsw_sp_fib6_entry *fib6_entry;
5885 struct mlxsw_sp_fib_node *fib_node;
5886 struct fib6_info *rt = rt_arr[0];
5889 if (mlxsw_sp->router->aborted)
5892 if (mlxsw_sp_fib6_rt_should_ignore(rt))
5895 /* Multipath routes are first added to the FIB trie and only then
5896 * notified. If we vetoed the addition, we will get a delete
5897 * notification for a route we do not have. Therefore, do not warn if
5898 * route was not found.
5900 fib6_entry = mlxsw_sp_fib6_entry_lookup(mlxsw_sp, rt);
5904 /* If not all the nexthops are deleted, then only reduce the nexthop
5907 if (nrt6 != fib6_entry->nrt6) {
5908 mlxsw_sp_fib6_entry_nexthop_del(mlxsw_sp, op_ctx, fib6_entry, rt_arr, nrt6);
5912 fib_node = fib6_entry->common.fib_node;
5914 err = __mlxsw_sp_fib_node_entry_unlink(mlxsw_sp, op_ctx, &fib6_entry->common);
5915 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
5916 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5920 static int __mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp,
5921 enum mlxsw_sp_l3proto proto,
5924 const struct mlxsw_sp_router_ll_ops *ll_ops = mlxsw_sp->router->proto_ll_ops[proto];
5925 enum mlxsw_reg_ralxx_protocol ralxx_proto =
5926 (enum mlxsw_reg_ralxx_protocol) proto;
5927 struct mlxsw_sp_fib_entry_priv *priv;
5928 char xralta_pl[MLXSW_REG_XRALTA_LEN];
5929 char xralst_pl[MLXSW_REG_XRALST_LEN];
5932 mlxsw_reg_xralta_pack(xralta_pl, true, ralxx_proto, tree_id);
5933 err = ll_ops->ralta_write(mlxsw_sp, xralta_pl);
5937 mlxsw_reg_xralst_pack(xralst_pl, 0xff, tree_id);
5938 err = ll_ops->ralst_write(mlxsw_sp, xralst_pl);
5942 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
5943 struct mlxsw_sp_fib_entry_op_ctx *op_ctx = mlxsw_sp->router->ll_op_ctx;
5944 struct mlxsw_sp_vr *vr = &mlxsw_sp->router->vrs[i];
5945 char xraltb_pl[MLXSW_REG_XRALTB_LEN];
5947 mlxsw_sp_fib_entry_op_ctx_clear(op_ctx);
5948 mlxsw_reg_xraltb_pack(xraltb_pl, vr->id, ralxx_proto, tree_id);
5949 err = ll_ops->raltb_write(mlxsw_sp, xraltb_pl);
5953 priv = mlxsw_sp_fib_entry_priv_create(ll_ops);
5955 return PTR_ERR(priv);
5957 ll_ops->fib_entry_pack(op_ctx, proto, MLXSW_SP_FIB_ENTRY_OP_WRITE,
5958 vr->id, 0, NULL, priv);
5959 ll_ops->fib_entry_act_ip2me_pack(op_ctx);
5960 err = ll_ops->fib_entry_commit(mlxsw_sp, op_ctx, NULL);
5961 mlxsw_sp_fib_entry_priv_put(priv);
5969 static struct mlxsw_sp_mr_table *
5970 mlxsw_sp_router_fibmr_family_to_table(struct mlxsw_sp_vr *vr, int family)
5972 if (family == RTNL_FAMILY_IPMR)
5973 return vr->mr_table[MLXSW_SP_L3_PROTO_IPV4];
5975 return vr->mr_table[MLXSW_SP_L3_PROTO_IPV6];
5978 static int mlxsw_sp_router_fibmr_add(struct mlxsw_sp *mlxsw_sp,
5979 struct mfc_entry_notifier_info *men_info,
5982 struct mlxsw_sp_mr_table *mrt;
5983 struct mlxsw_sp_vr *vr;
5985 if (mlxsw_sp->router->aborted)
5988 vr = mlxsw_sp_vr_get(mlxsw_sp, men_info->tb_id, NULL);
5992 mrt = mlxsw_sp_router_fibmr_family_to_table(vr, men_info->info.family);
5993 return mlxsw_sp_mr_route_add(mrt, men_info->mfc, replace);
5996 static void mlxsw_sp_router_fibmr_del(struct mlxsw_sp *mlxsw_sp,
5997 struct mfc_entry_notifier_info *men_info)
5999 struct mlxsw_sp_mr_table *mrt;
6000 struct mlxsw_sp_vr *vr;
6002 if (mlxsw_sp->router->aborted)
6005 vr = mlxsw_sp_vr_find(mlxsw_sp, men_info->tb_id);
6009 mrt = mlxsw_sp_router_fibmr_family_to_table(vr, men_info->info.family);
6010 mlxsw_sp_mr_route_del(mrt, men_info->mfc);
6011 mlxsw_sp_vr_put(mlxsw_sp, vr);
6015 mlxsw_sp_router_fibmr_vif_add(struct mlxsw_sp *mlxsw_sp,
6016 struct vif_entry_notifier_info *ven_info)
6018 struct mlxsw_sp_mr_table *mrt;
6019 struct mlxsw_sp_rif *rif;
6020 struct mlxsw_sp_vr *vr;
6022 if (mlxsw_sp->router->aborted)
6025 vr = mlxsw_sp_vr_get(mlxsw_sp, ven_info->tb_id, NULL);
6029 mrt = mlxsw_sp_router_fibmr_family_to_table(vr, ven_info->info.family);
6030 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, ven_info->dev);
6031 return mlxsw_sp_mr_vif_add(mrt, ven_info->dev,
6032 ven_info->vif_index,
6033 ven_info->vif_flags, rif);
6037 mlxsw_sp_router_fibmr_vif_del(struct mlxsw_sp *mlxsw_sp,
6038 struct vif_entry_notifier_info *ven_info)
6040 struct mlxsw_sp_mr_table *mrt;
6041 struct mlxsw_sp_vr *vr;
6043 if (mlxsw_sp->router->aborted)
6046 vr = mlxsw_sp_vr_find(mlxsw_sp, ven_info->tb_id);
6050 mrt = mlxsw_sp_router_fibmr_family_to_table(vr, ven_info->info.family);
6051 mlxsw_sp_mr_vif_del(mrt, ven_info->vif_index);
6052 mlxsw_sp_vr_put(mlxsw_sp, vr);
6055 static int mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp)
6057 enum mlxsw_sp_l3proto proto = MLXSW_SP_L3_PROTO_IPV4;
6060 err = __mlxsw_sp_router_set_abort_trap(mlxsw_sp, proto,
6061 MLXSW_SP_LPM_TREE_MIN);
6065 /* The multicast router code does not need an abort trap as by default,
6066 * packets that don't match any routes are trapped to the CPU.
6069 proto = MLXSW_SP_L3_PROTO_IPV6;
6070 return __mlxsw_sp_router_set_abort_trap(mlxsw_sp, proto,
6071 MLXSW_SP_LPM_TREE_MIN + 1);
6074 static void mlxsw_sp_fib4_node_flush(struct mlxsw_sp *mlxsw_sp,
6075 struct mlxsw_sp_fib_node *fib_node)
6077 struct mlxsw_sp_fib4_entry *fib4_entry;
6079 fib4_entry = container_of(fib_node->fib_entry,
6080 struct mlxsw_sp_fib4_entry, common);
6081 mlxsw_sp_fib_node_entry_unlink(mlxsw_sp, fib_node->fib_entry);
6082 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
6083 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
6086 static void mlxsw_sp_fib6_node_flush(struct mlxsw_sp *mlxsw_sp,
6087 struct mlxsw_sp_fib_node *fib_node)
6089 struct mlxsw_sp_fib6_entry *fib6_entry;
6091 fib6_entry = container_of(fib_node->fib_entry,
6092 struct mlxsw_sp_fib6_entry, common);
6093 mlxsw_sp_fib_node_entry_unlink(mlxsw_sp, fib_node->fib_entry);
6094 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
6095 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
6098 static void mlxsw_sp_fib_node_flush(struct mlxsw_sp *mlxsw_sp,
6099 struct mlxsw_sp_fib_node *fib_node)
6101 switch (fib_node->fib->proto) {
6102 case MLXSW_SP_L3_PROTO_IPV4:
6103 mlxsw_sp_fib4_node_flush(mlxsw_sp, fib_node);
6105 case MLXSW_SP_L3_PROTO_IPV6:
6106 mlxsw_sp_fib6_node_flush(mlxsw_sp, fib_node);
6111 static void mlxsw_sp_vr_fib_flush(struct mlxsw_sp *mlxsw_sp,
6112 struct mlxsw_sp_vr *vr,
6113 enum mlxsw_sp_l3proto proto)
6115 struct mlxsw_sp_fib *fib = mlxsw_sp_vr_fib(vr, proto);
6116 struct mlxsw_sp_fib_node *fib_node, *tmp;
6118 list_for_each_entry_safe(fib_node, tmp, &fib->node_list, list) {
6119 bool do_break = &tmp->list == &fib->node_list;
6121 mlxsw_sp_fib_node_flush(mlxsw_sp, fib_node);
6127 static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp)
6131 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
6132 struct mlxsw_sp_vr *vr = &mlxsw_sp->router->vrs[i];
6134 if (!mlxsw_sp_vr_is_used(vr))
6137 for (j = 0; j < MLXSW_SP_L3_PROTO_MAX; j++)
6138 mlxsw_sp_mr_table_flush(vr->mr_table[j]);
6139 mlxsw_sp_vr_fib_flush(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV4);
6141 /* If virtual router was only used for IPv4, then it's no
6144 if (!mlxsw_sp_vr_is_used(vr))
6146 mlxsw_sp_vr_fib_flush(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV6);
6149 /* After flushing all the routes, it is not possible anyone is still
6150 * using the adjacency index that is discarding packets, so free it in
6151 * case it was allocated.
6153 if (!mlxsw_sp->router->adj_discard_index_valid)
6155 mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ, 1,
6156 mlxsw_sp->router->adj_discard_index);
6157 mlxsw_sp->router->adj_discard_index_valid = false;
6160 static void mlxsw_sp_router_fib_abort(struct mlxsw_sp *mlxsw_sp)
6164 if (mlxsw_sp->router->aborted)
6166 dev_warn(mlxsw_sp->bus_info->dev, "FIB abort triggered. Note that FIB entries are no longer being offloaded to this device.\n");
6167 mlxsw_sp_router_fib_flush(mlxsw_sp);
6168 mlxsw_sp->router->aborted = true;
6169 err = mlxsw_sp_router_set_abort_trap(mlxsw_sp);
6171 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set abort trap.\n");
6174 struct mlxsw_sp_fib6_event {
6175 struct fib6_info **rt_arr;
6179 struct mlxsw_sp_fib_event {
6180 struct list_head list; /* node in fib queue */
6182 struct mlxsw_sp_fib6_event fib6_event;
6183 struct fib_entry_notifier_info fen_info;
6184 struct fib_rule_notifier_info fr_info;
6185 struct fib_nh_notifier_info fnh_info;
6186 struct mfc_entry_notifier_info men_info;
6187 struct vif_entry_notifier_info ven_info;
6189 struct mlxsw_sp *mlxsw_sp;
6190 unsigned long event;
6195 mlxsw_sp_router_fib6_event_init(struct mlxsw_sp_fib6_event *fib6_event,
6196 struct fib6_entry_notifier_info *fen6_info)
6198 struct fib6_info *rt = fen6_info->rt;
6199 struct fib6_info **rt_arr;
6200 struct fib6_info *iter;
6204 nrt6 = fen6_info->nsiblings + 1;
6206 rt_arr = kcalloc(nrt6, sizeof(struct fib6_info *), GFP_ATOMIC);
6210 fib6_event->rt_arr = rt_arr;
6211 fib6_event->nrt6 = nrt6;
6216 if (!fen6_info->nsiblings)
6219 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
6220 if (i == fen6_info->nsiblings)
6223 rt_arr[i + 1] = iter;
6224 fib6_info_hold(iter);
6227 WARN_ON_ONCE(i != fen6_info->nsiblings);
6233 mlxsw_sp_router_fib6_event_fini(struct mlxsw_sp_fib6_event *fib6_event)
6237 for (i = 0; i < fib6_event->nrt6; i++)
6238 mlxsw_sp_rt6_release(fib6_event->rt_arr[i]);
6239 kfree(fib6_event->rt_arr);
6242 static void mlxsw_sp_router_fib4_event_process(struct mlxsw_sp *mlxsw_sp,
6243 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
6244 struct mlxsw_sp_fib_event *fib_event)
6248 mlxsw_sp_span_respin(mlxsw_sp);
6250 switch (fib_event->event) {
6251 case FIB_EVENT_ENTRY_REPLACE:
6252 err = mlxsw_sp_router_fib4_replace(mlxsw_sp, op_ctx, &fib_event->fen_info);
6254 mlxsw_sp_fib_entry_op_ctx_priv_put_all(op_ctx);
6255 mlxsw_sp_router_fib_abort(mlxsw_sp);
6257 fib_info_put(fib_event->fen_info.fi);
6259 case FIB_EVENT_ENTRY_DEL:
6260 err = mlxsw_sp_router_fib4_del(mlxsw_sp, op_ctx, &fib_event->fen_info);
6262 mlxsw_sp_fib_entry_op_ctx_priv_put_all(op_ctx);
6263 fib_info_put(fib_event->fen_info.fi);
6265 case FIB_EVENT_NH_ADD:
6266 case FIB_EVENT_NH_DEL:
6267 mlxsw_sp_nexthop4_event(mlxsw_sp, fib_event->event, fib_event->fnh_info.fib_nh);
6268 fib_info_put(fib_event->fnh_info.fib_nh->nh_parent);
6273 static void mlxsw_sp_router_fib6_event_process(struct mlxsw_sp *mlxsw_sp,
6274 struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
6275 struct mlxsw_sp_fib_event *fib_event)
6279 mlxsw_sp_span_respin(mlxsw_sp);
6281 switch (fib_event->event) {
6282 case FIB_EVENT_ENTRY_REPLACE:
6283 err = mlxsw_sp_router_fib6_replace(mlxsw_sp, op_ctx, fib_event->fib6_event.rt_arr,
6284 fib_event->fib6_event.nrt6);
6286 mlxsw_sp_fib_entry_op_ctx_priv_put_all(op_ctx);
6287 mlxsw_sp_router_fib_abort(mlxsw_sp);
6289 mlxsw_sp_router_fib6_event_fini(&fib_event->fib6_event);
6291 case FIB_EVENT_ENTRY_APPEND:
6292 err = mlxsw_sp_router_fib6_append(mlxsw_sp, op_ctx, fib_event->fib6_event.rt_arr,
6293 fib_event->fib6_event.nrt6);
6295 mlxsw_sp_fib_entry_op_ctx_priv_put_all(op_ctx);
6296 mlxsw_sp_router_fib_abort(mlxsw_sp);
6298 mlxsw_sp_router_fib6_event_fini(&fib_event->fib6_event);
6300 case FIB_EVENT_ENTRY_DEL:
6301 err = mlxsw_sp_router_fib6_del(mlxsw_sp, op_ctx, fib_event->fib6_event.rt_arr,
6302 fib_event->fib6_event.nrt6);
6304 mlxsw_sp_fib_entry_op_ctx_priv_put_all(op_ctx);
6305 mlxsw_sp_router_fib6_event_fini(&fib_event->fib6_event);
6310 static void mlxsw_sp_router_fibmr_event_process(struct mlxsw_sp *mlxsw_sp,
6311 struct mlxsw_sp_fib_event *fib_event)
6317 mutex_lock(&mlxsw_sp->router->lock);
6318 switch (fib_event->event) {
6319 case FIB_EVENT_ENTRY_REPLACE:
6320 case FIB_EVENT_ENTRY_ADD:
6321 replace = fib_event->event == FIB_EVENT_ENTRY_REPLACE;
6323 err = mlxsw_sp_router_fibmr_add(mlxsw_sp, &fib_event->men_info, replace);
6325 mlxsw_sp_router_fib_abort(mlxsw_sp);
6326 mr_cache_put(fib_event->men_info.mfc);
6328 case FIB_EVENT_ENTRY_DEL:
6329 mlxsw_sp_router_fibmr_del(mlxsw_sp, &fib_event->men_info);
6330 mr_cache_put(fib_event->men_info.mfc);
6332 case FIB_EVENT_VIF_ADD:
6333 err = mlxsw_sp_router_fibmr_vif_add(mlxsw_sp,
6334 &fib_event->ven_info);
6336 mlxsw_sp_router_fib_abort(mlxsw_sp);
6337 dev_put(fib_event->ven_info.dev);
6339 case FIB_EVENT_VIF_DEL:
6340 mlxsw_sp_router_fibmr_vif_del(mlxsw_sp, &fib_event->ven_info);
6341 dev_put(fib_event->ven_info.dev);
6344 mutex_unlock(&mlxsw_sp->router->lock);
6348 static void mlxsw_sp_router_fib_event_work(struct work_struct *work)
6350 struct mlxsw_sp_router *router = container_of(work, struct mlxsw_sp_router, fib_event_work);
6351 struct mlxsw_sp_fib_entry_op_ctx *op_ctx = router->ll_op_ctx;
6352 struct mlxsw_sp *mlxsw_sp = router->mlxsw_sp;
6353 struct mlxsw_sp_fib_event *next_fib_event;
6354 struct mlxsw_sp_fib_event *fib_event;
6355 int last_family = AF_UNSPEC;
6356 LIST_HEAD(fib_event_queue);
6358 spin_lock_bh(&router->fib_event_queue_lock);
6359 list_splice_init(&router->fib_event_queue, &fib_event_queue);
6360 spin_unlock_bh(&router->fib_event_queue_lock);
6362 /* Router lock is held here to make sure per-instance
6363 * operation context is not used in between FIB4/6 events
6366 mutex_lock(&router->lock);
6367 mlxsw_sp_fib_entry_op_ctx_clear(op_ctx);
6368 list_for_each_entry_safe(fib_event, next_fib_event,
6369 &fib_event_queue, list) {
6370 /* Check if the next entry in the queue exists and it is
6371 * of the same type (family and event) as the currect one.
6372 * In that case it is permitted to do the bulking
6373 * of multiple FIB entries to a single register write.
6375 op_ctx->bulk_ok = !list_is_last(&fib_event->list, &fib_event_queue) &&
6376 fib_event->family == next_fib_event->family &&
6377 fib_event->event == next_fib_event->event;
6379 /* In case family of this and the previous entry are different, context
6380 * reinitialization is going to be needed now, indicate that.
6381 * Note that since last_family is initialized to AF_UNSPEC, this is always
6382 * going to happen for the first entry processed in the work.
6384 if (fib_event->family != last_family)
6385 op_ctx->initialized = false;
6387 switch (fib_event->family) {
6389 mlxsw_sp_router_fib4_event_process(mlxsw_sp, op_ctx,
6393 mlxsw_sp_router_fib6_event_process(mlxsw_sp, op_ctx,
6396 case RTNL_FAMILY_IP6MR:
6397 case RTNL_FAMILY_IPMR:
6398 /* Unlock here as inside FIBMR the lock is taken again
6399 * under RTNL. The per-instance operation context
6400 * is not used by FIBMR.
6402 mutex_unlock(&router->lock);
6403 mlxsw_sp_router_fibmr_event_process(mlxsw_sp,
6405 mutex_lock(&router->lock);
6410 last_family = fib_event->family;
6414 WARN_ON_ONCE(!list_empty(&router->ll_op_ctx->fib_entry_priv_list));
6415 mutex_unlock(&router->lock);
6418 static void mlxsw_sp_router_fib4_event(struct mlxsw_sp_fib_event *fib_event,
6419 struct fib_notifier_info *info)
6421 struct fib_entry_notifier_info *fen_info;
6422 struct fib_nh_notifier_info *fnh_info;
6424 switch (fib_event->event) {
6425 case FIB_EVENT_ENTRY_REPLACE:
6426 case FIB_EVENT_ENTRY_DEL:
6427 fen_info = container_of(info, struct fib_entry_notifier_info,
6429 fib_event->fen_info = *fen_info;
6430 /* Take reference on fib_info to prevent it from being
6431 * freed while event is queued. Release it afterwards.
6433 fib_info_hold(fib_event->fen_info.fi);
6435 case FIB_EVENT_NH_ADD:
6436 case FIB_EVENT_NH_DEL:
6437 fnh_info = container_of(info, struct fib_nh_notifier_info,
6439 fib_event->fnh_info = *fnh_info;
6440 fib_info_hold(fib_event->fnh_info.fib_nh->nh_parent);
6445 static int mlxsw_sp_router_fib6_event(struct mlxsw_sp_fib_event *fib_event,
6446 struct fib_notifier_info *info)
6448 struct fib6_entry_notifier_info *fen6_info;
6451 switch (fib_event->event) {
6452 case FIB_EVENT_ENTRY_REPLACE:
6453 case FIB_EVENT_ENTRY_APPEND:
6454 case FIB_EVENT_ENTRY_DEL:
6455 fen6_info = container_of(info, struct fib6_entry_notifier_info,
6457 err = mlxsw_sp_router_fib6_event_init(&fib_event->fib6_event,
6468 mlxsw_sp_router_fibmr_event(struct mlxsw_sp_fib_event *fib_event,
6469 struct fib_notifier_info *info)
6471 switch (fib_event->event) {
6472 case FIB_EVENT_ENTRY_REPLACE:
6473 case FIB_EVENT_ENTRY_ADD:
6474 case FIB_EVENT_ENTRY_DEL:
6475 memcpy(&fib_event->men_info, info, sizeof(fib_event->men_info));
6476 mr_cache_hold(fib_event->men_info.mfc);
6478 case FIB_EVENT_VIF_ADD:
6479 case FIB_EVENT_VIF_DEL:
6480 memcpy(&fib_event->ven_info, info, sizeof(fib_event->ven_info));
6481 dev_hold(fib_event->ven_info.dev);
6486 static int mlxsw_sp_router_fib_rule_event(unsigned long event,
6487 struct fib_notifier_info *info,
6488 struct mlxsw_sp *mlxsw_sp)
6490 struct netlink_ext_ack *extack = info->extack;
6491 struct fib_rule_notifier_info *fr_info;
6492 struct fib_rule *rule;
6495 /* nothing to do at the moment */
6496 if (event == FIB_EVENT_RULE_DEL)
6499 if (mlxsw_sp->router->aborted)
6502 fr_info = container_of(info, struct fib_rule_notifier_info, info);
6503 rule = fr_info->rule;
6505 /* Rule only affects locally generated traffic */
6506 if (rule->iifindex == mlxsw_sp_net(mlxsw_sp)->loopback_dev->ifindex)
6509 switch (info->family) {
6511 if (!fib4_rule_default(rule) && !rule->l3mdev)
6515 if (!fib6_rule_default(rule) && !rule->l3mdev)
6518 case RTNL_FAMILY_IPMR:
6519 if (!ipmr_rule_default(rule) && !rule->l3mdev)
6522 case RTNL_FAMILY_IP6MR:
6523 if (!ip6mr_rule_default(rule) && !rule->l3mdev)
6529 NL_SET_ERR_MSG_MOD(extack, "FIB rules not supported");
6534 /* Called with rcu_read_lock() */
6535 static int mlxsw_sp_router_fib_event(struct notifier_block *nb,
6536 unsigned long event, void *ptr)
6538 struct mlxsw_sp_fib_event *fib_event;
6539 struct fib_notifier_info *info = ptr;
6540 struct mlxsw_sp_router *router;
6543 if ((info->family != AF_INET && info->family != AF_INET6 &&
6544 info->family != RTNL_FAMILY_IPMR &&
6545 info->family != RTNL_FAMILY_IP6MR))
6548 router = container_of(nb, struct mlxsw_sp_router, fib_nb);
6551 case FIB_EVENT_RULE_ADD:
6552 case FIB_EVENT_RULE_DEL:
6553 err = mlxsw_sp_router_fib_rule_event(event, info,
6555 return notifier_from_errno(err);
6556 case FIB_EVENT_ENTRY_ADD:
6557 case FIB_EVENT_ENTRY_REPLACE:
6558 case FIB_EVENT_ENTRY_APPEND:
6559 if (router->aborted) {
6560 NL_SET_ERR_MSG_MOD(info->extack, "FIB offload was aborted. Not configuring route");
6561 return notifier_from_errno(-EINVAL);
6563 if (info->family == AF_INET) {
6564 struct fib_entry_notifier_info *fen_info = ptr;
6566 if (fen_info->fi->fib_nh_is_v6) {
6567 NL_SET_ERR_MSG_MOD(info->extack, "IPv6 gateway with IPv4 route is not supported");
6568 return notifier_from_errno(-EINVAL);
6570 if (fen_info->fi->nh) {
6571 NL_SET_ERR_MSG_MOD(info->extack, "IPv4 route with nexthop objects is not supported");
6572 return notifier_from_errno(-EINVAL);
6574 } else if (info->family == AF_INET6) {
6575 struct fib6_entry_notifier_info *fen6_info;
6577 fen6_info = container_of(info,
6578 struct fib6_entry_notifier_info,
6580 if (fen6_info->rt->nh) {
6581 NL_SET_ERR_MSG_MOD(info->extack, "IPv6 route with nexthop objects is not supported");
6582 return notifier_from_errno(-EINVAL);
6588 fib_event = kzalloc(sizeof(*fib_event), GFP_ATOMIC);
6592 fib_event->mlxsw_sp = router->mlxsw_sp;
6593 fib_event->event = event;
6594 fib_event->family = info->family;
6596 switch (info->family) {
6598 mlxsw_sp_router_fib4_event(fib_event, info);
6601 err = mlxsw_sp_router_fib6_event(fib_event, info);
6605 case RTNL_FAMILY_IP6MR:
6606 case RTNL_FAMILY_IPMR:
6607 mlxsw_sp_router_fibmr_event(fib_event, info);
6611 /* Enqueue the event and trigger the work */
6612 spin_lock_bh(&router->fib_event_queue_lock);
6613 list_add_tail(&fib_event->list, &router->fib_event_queue);
6614 spin_unlock_bh(&router->fib_event_queue_lock);
6615 mlxsw_core_schedule_work(&router->fib_event_work);
6624 static struct mlxsw_sp_rif *
6625 mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
6626 const struct net_device *dev)
6630 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
6631 if (mlxsw_sp->router->rifs[i] &&
6632 mlxsw_sp->router->rifs[i]->dev == dev)
6633 return mlxsw_sp->router->rifs[i];
6638 bool mlxsw_sp_rif_exists(struct mlxsw_sp *mlxsw_sp,
6639 const struct net_device *dev)
6641 struct mlxsw_sp_rif *rif;
6643 mutex_lock(&mlxsw_sp->router->lock);
6644 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
6645 mutex_unlock(&mlxsw_sp->router->lock);
6650 u16 mlxsw_sp_rif_vid(struct mlxsw_sp *mlxsw_sp, const struct net_device *dev)
6652 struct mlxsw_sp_rif *rif;
6655 mutex_lock(&mlxsw_sp->router->lock);
6656 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
6660 /* We only return the VID for VLAN RIFs. Otherwise we return an
6661 * invalid value (0).
6663 if (rif->ops->type != MLXSW_SP_RIF_TYPE_VLAN)
6666 vid = mlxsw_sp_fid_8021q_vid(rif->fid);
6669 mutex_unlock(&mlxsw_sp->router->lock);
6673 static int mlxsw_sp_router_rif_disable(struct mlxsw_sp *mlxsw_sp, u16 rif)
6675 char ritr_pl[MLXSW_REG_RITR_LEN];
6678 mlxsw_reg_ritr_rif_pack(ritr_pl, rif);
6679 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
6683 mlxsw_reg_ritr_enable_set(ritr_pl, false);
6684 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
6687 static void mlxsw_sp_router_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
6688 struct mlxsw_sp_rif *rif)
6690 mlxsw_sp_router_rif_disable(mlxsw_sp, rif->rif_index);
6691 mlxsw_sp_nexthop_rif_gone_sync(mlxsw_sp, rif);
6692 mlxsw_sp_neigh_rif_gone_sync(mlxsw_sp, rif);
6696 mlxsw_sp_rif_should_config(struct mlxsw_sp_rif *rif, struct net_device *dev,
6697 unsigned long event)
6699 struct inet6_dev *inet6_dev;
6700 bool addr_list_empty = true;
6701 struct in_device *idev;
6708 idev = __in_dev_get_rcu(dev);
6709 if (idev && idev->ifa_list)
6710 addr_list_empty = false;
6712 inet6_dev = __in6_dev_get(dev);
6713 if (addr_list_empty && inet6_dev &&
6714 !list_empty(&inet6_dev->addr_list))
6715 addr_list_empty = false;
6718 /* macvlans do not have a RIF, but rather piggy back on the
6719 * RIF of their lower device.
6721 if (netif_is_macvlan(dev) && addr_list_empty)
6724 if (rif && addr_list_empty &&
6725 !netif_is_l3_slave(rif->dev))
6727 /* It is possible we already removed the RIF ourselves
6728 * if it was assigned to a netdev that is now a bridge
6737 static enum mlxsw_sp_rif_type
6738 mlxsw_sp_dev_rif_type(const struct mlxsw_sp *mlxsw_sp,
6739 const struct net_device *dev)
6741 enum mlxsw_sp_fid_type type;
6743 if (mlxsw_sp_netdev_ipip_type(mlxsw_sp, dev, NULL))
6744 return MLXSW_SP_RIF_TYPE_IPIP_LB;
6746 /* Otherwise RIF type is derived from the type of the underlying FID. */
6747 if (is_vlan_dev(dev) && netif_is_bridge_master(vlan_dev_real_dev(dev)))
6748 type = MLXSW_SP_FID_TYPE_8021Q;
6749 else if (netif_is_bridge_master(dev) && br_vlan_enabled(dev))
6750 type = MLXSW_SP_FID_TYPE_8021Q;
6751 else if (netif_is_bridge_master(dev))
6752 type = MLXSW_SP_FID_TYPE_8021D;
6754 type = MLXSW_SP_FID_TYPE_RFID;
6756 return mlxsw_sp_fid_type_rif_type(mlxsw_sp, type);
6759 static int mlxsw_sp_rif_index_alloc(struct mlxsw_sp *mlxsw_sp, u16 *p_rif_index)
6763 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++) {
6764 if (!mlxsw_sp->router->rifs[i]) {
6773 static struct mlxsw_sp_rif *mlxsw_sp_rif_alloc(size_t rif_size, u16 rif_index,
6775 struct net_device *l3_dev)
6777 struct mlxsw_sp_rif *rif;
6779 rif = kzalloc(rif_size, GFP_KERNEL);
6783 INIT_LIST_HEAD(&rif->nexthop_list);
6784 INIT_LIST_HEAD(&rif->neigh_list);
6786 ether_addr_copy(rif->addr, l3_dev->dev_addr);
6787 rif->mtu = l3_dev->mtu;
6791 rif->rif_index = rif_index;
6796 struct mlxsw_sp_rif *mlxsw_sp_rif_by_index(const struct mlxsw_sp *mlxsw_sp,
6799 return mlxsw_sp->router->rifs[rif_index];
6802 u16 mlxsw_sp_rif_index(const struct mlxsw_sp_rif *rif)
6804 return rif->rif_index;
6807 u16 mlxsw_sp_ipip_lb_rif_index(const struct mlxsw_sp_rif_ipip_lb *lb_rif)
6809 return lb_rif->common.rif_index;
6812 u16 mlxsw_sp_ipip_lb_ul_vr_id(const struct mlxsw_sp_rif_ipip_lb *lb_rif)
6814 u32 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(lb_rif->common.dev);
6815 struct mlxsw_sp_vr *ul_vr;
6817 ul_vr = mlxsw_sp_vr_get(lb_rif->common.mlxsw_sp, ul_tb_id, NULL);
6818 if (WARN_ON(IS_ERR(ul_vr)))
6824 u16 mlxsw_sp_ipip_lb_ul_rif_id(const struct mlxsw_sp_rif_ipip_lb *lb_rif)
6826 return lb_rif->ul_rif_id;
6829 int mlxsw_sp_rif_dev_ifindex(const struct mlxsw_sp_rif *rif)
6831 return rif->dev->ifindex;
6834 const struct net_device *mlxsw_sp_rif_dev(const struct mlxsw_sp_rif *rif)
6839 static struct mlxsw_sp_rif *
6840 mlxsw_sp_rif_create(struct mlxsw_sp *mlxsw_sp,
6841 const struct mlxsw_sp_rif_params *params,
6842 struct netlink_ext_ack *extack)
6844 u32 tb_id = l3mdev_fib_table(params->dev);
6845 const struct mlxsw_sp_rif_ops *ops;
6846 struct mlxsw_sp_fid *fid = NULL;
6847 enum mlxsw_sp_rif_type type;
6848 struct mlxsw_sp_rif *rif;
6849 struct mlxsw_sp_vr *vr;
6853 type = mlxsw_sp_dev_rif_type(mlxsw_sp, params->dev);
6854 ops = mlxsw_sp->rif_ops_arr[type];
6856 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id ? : RT_TABLE_MAIN, extack);
6858 return ERR_CAST(vr);
6861 err = mlxsw_sp_rif_index_alloc(mlxsw_sp, &rif_index);
6863 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported router interfaces");
6864 goto err_rif_index_alloc;
6867 rif = mlxsw_sp_rif_alloc(ops->rif_size, rif_index, vr->id, params->dev);
6873 mlxsw_sp->router->rifs[rif_index] = rif;
6874 rif->mlxsw_sp = mlxsw_sp;
6878 fid = ops->fid_get(rif, extack);
6887 ops->setup(rif, params);
6889 err = ops->configure(rif);
6893 for (i = 0; i < MLXSW_SP_L3_PROTO_MAX; i++) {
6894 err = mlxsw_sp_mr_rif_add(vr->mr_table[i], rif);
6896 goto err_mr_rif_add;
6899 mlxsw_sp_rif_counters_alloc(rif);
6904 for (i--; i >= 0; i--)
6905 mlxsw_sp_mr_rif_del(vr->mr_table[i], rif);
6906 ops->deconfigure(rif);
6909 mlxsw_sp_fid_put(fid);
6911 mlxsw_sp->router->rifs[rif_index] = NULL;
6915 err_rif_index_alloc:
6917 mlxsw_sp_vr_put(mlxsw_sp, vr);
6918 return ERR_PTR(err);
6921 static void mlxsw_sp_rif_destroy(struct mlxsw_sp_rif *rif)
6923 const struct mlxsw_sp_rif_ops *ops = rif->ops;
6924 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6925 struct mlxsw_sp_fid *fid = rif->fid;
6926 struct mlxsw_sp_vr *vr;
6929 mlxsw_sp_router_rif_gone_sync(mlxsw_sp, rif);
6930 vr = &mlxsw_sp->router->vrs[rif->vr_id];
6932 mlxsw_sp_rif_counters_free(rif);
6933 for (i = 0; i < MLXSW_SP_L3_PROTO_MAX; i++)
6934 mlxsw_sp_mr_rif_del(vr->mr_table[i], rif);
6935 ops->deconfigure(rif);
6937 /* Loopback RIFs are not associated with a FID. */
6938 mlxsw_sp_fid_put(fid);
6939 mlxsw_sp->router->rifs[rif->rif_index] = NULL;
6943 mlxsw_sp_vr_put(mlxsw_sp, vr);
6946 void mlxsw_sp_rif_destroy_by_dev(struct mlxsw_sp *mlxsw_sp,
6947 struct net_device *dev)
6949 struct mlxsw_sp_rif *rif;
6951 mutex_lock(&mlxsw_sp->router->lock);
6952 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
6955 mlxsw_sp_rif_destroy(rif);
6957 mutex_unlock(&mlxsw_sp->router->lock);
6961 mlxsw_sp_rif_subport_params_init(struct mlxsw_sp_rif_params *params,
6962 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
6964 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
6966 params->vid = mlxsw_sp_port_vlan->vid;
6967 params->lag = mlxsw_sp_port->lagged;
6969 params->lag_id = mlxsw_sp_port->lag_id;
6971 params->system_port = mlxsw_sp_port->local_port;
6974 static struct mlxsw_sp_rif_subport *
6975 mlxsw_sp_rif_subport_rif(const struct mlxsw_sp_rif *rif)
6977 return container_of(rif, struct mlxsw_sp_rif_subport, common);
6980 static struct mlxsw_sp_rif *
6981 mlxsw_sp_rif_subport_get(struct mlxsw_sp *mlxsw_sp,
6982 const struct mlxsw_sp_rif_params *params,
6983 struct netlink_ext_ack *extack)
6985 struct mlxsw_sp_rif_subport *rif_subport;
6986 struct mlxsw_sp_rif *rif;
6988 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, params->dev);
6990 return mlxsw_sp_rif_create(mlxsw_sp, params, extack);
6992 rif_subport = mlxsw_sp_rif_subport_rif(rif);
6993 refcount_inc(&rif_subport->ref_count);
6997 static void mlxsw_sp_rif_subport_put(struct mlxsw_sp_rif *rif)
6999 struct mlxsw_sp_rif_subport *rif_subport;
7001 rif_subport = mlxsw_sp_rif_subport_rif(rif);
7002 if (!refcount_dec_and_test(&rif_subport->ref_count))
7005 mlxsw_sp_rif_destroy(rif);
7009 mlxsw_sp_port_vlan_router_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
7010 struct net_device *l3_dev,
7011 struct netlink_ext_ack *extack)
7013 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
7014 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
7015 struct mlxsw_sp_rif_params params = {
7018 u16 vid = mlxsw_sp_port_vlan->vid;
7019 struct mlxsw_sp_rif *rif;
7020 struct mlxsw_sp_fid *fid;
7023 mlxsw_sp_rif_subport_params_init(¶ms, mlxsw_sp_port_vlan);
7024 rif = mlxsw_sp_rif_subport_get(mlxsw_sp, ¶ms, extack);
7026 return PTR_ERR(rif);
7028 /* FID was already created, just take a reference */
7029 fid = rif->ops->fid_get(rif, extack);
7030 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
7032 goto err_fid_port_vid_map;
7034 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
7036 goto err_port_vid_learning_set;
7038 err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
7039 BR_STATE_FORWARDING);
7041 goto err_port_vid_stp_set;
7043 mlxsw_sp_port_vlan->fid = fid;
7047 err_port_vid_stp_set:
7048 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
7049 err_port_vid_learning_set:
7050 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
7051 err_fid_port_vid_map:
7052 mlxsw_sp_fid_put(fid);
7053 mlxsw_sp_rif_subport_put(rif);
7058 __mlxsw_sp_port_vlan_router_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
7060 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
7061 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
7062 struct mlxsw_sp_rif *rif = mlxsw_sp_fid_rif(fid);
7063 u16 vid = mlxsw_sp_port_vlan->vid;
7065 if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_RFID))
7068 mlxsw_sp_port_vlan->fid = NULL;
7069 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_BLOCKING);
7070 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
7071 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
7072 mlxsw_sp_fid_put(fid);
7073 mlxsw_sp_rif_subport_put(rif);
7077 mlxsw_sp_port_vlan_router_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
7079 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port_vlan->mlxsw_sp_port->mlxsw_sp;
7081 mutex_lock(&mlxsw_sp->router->lock);
7082 __mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
7083 mutex_unlock(&mlxsw_sp->router->lock);
7086 static int mlxsw_sp_inetaddr_port_vlan_event(struct net_device *l3_dev,
7087 struct net_device *port_dev,
7088 unsigned long event, u16 vid,
7089 struct netlink_ext_ack *extack)
7091 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(port_dev);
7092 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
7094 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
7095 if (WARN_ON(!mlxsw_sp_port_vlan))
7100 return mlxsw_sp_port_vlan_router_join(mlxsw_sp_port_vlan,
7103 __mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
7110 static int mlxsw_sp_inetaddr_port_event(struct net_device *port_dev,
7111 unsigned long event,
7112 struct netlink_ext_ack *extack)
7114 if (netif_is_bridge_port(port_dev) ||
7115 netif_is_lag_port(port_dev) ||
7116 netif_is_ovs_port(port_dev))
7119 return mlxsw_sp_inetaddr_port_vlan_event(port_dev, port_dev, event,
7120 MLXSW_SP_DEFAULT_VID, extack);
7123 static int __mlxsw_sp_inetaddr_lag_event(struct net_device *l3_dev,
7124 struct net_device *lag_dev,
7125 unsigned long event, u16 vid,
7126 struct netlink_ext_ack *extack)
7128 struct net_device *port_dev;
7129 struct list_head *iter;
7132 netdev_for_each_lower_dev(lag_dev, port_dev, iter) {
7133 if (mlxsw_sp_port_dev_check(port_dev)) {
7134 err = mlxsw_sp_inetaddr_port_vlan_event(l3_dev,
7146 static int mlxsw_sp_inetaddr_lag_event(struct net_device *lag_dev,
7147 unsigned long event,
7148 struct netlink_ext_ack *extack)
7150 if (netif_is_bridge_port(lag_dev))
7153 return __mlxsw_sp_inetaddr_lag_event(lag_dev, lag_dev, event,
7154 MLXSW_SP_DEFAULT_VID, extack);
7157 static int mlxsw_sp_inetaddr_bridge_event(struct mlxsw_sp *mlxsw_sp,
7158 struct net_device *l3_dev,
7159 unsigned long event,
7160 struct netlink_ext_ack *extack)
7162 struct mlxsw_sp_rif_params params = {
7165 struct mlxsw_sp_rif *rif;
7169 rif = mlxsw_sp_rif_create(mlxsw_sp, ¶ms, extack);
7171 return PTR_ERR(rif);
7174 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
7175 mlxsw_sp_rif_destroy(rif);
7182 static int mlxsw_sp_inetaddr_vlan_event(struct mlxsw_sp *mlxsw_sp,
7183 struct net_device *vlan_dev,
7184 unsigned long event,
7185 struct netlink_ext_ack *extack)
7187 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
7188 u16 vid = vlan_dev_vlan_id(vlan_dev);
7190 if (netif_is_bridge_port(vlan_dev))
7193 if (mlxsw_sp_port_dev_check(real_dev))
7194 return mlxsw_sp_inetaddr_port_vlan_event(vlan_dev, real_dev,
7195 event, vid, extack);
7196 else if (netif_is_lag_master(real_dev))
7197 return __mlxsw_sp_inetaddr_lag_event(vlan_dev, real_dev, event,
7199 else if (netif_is_bridge_master(real_dev) && br_vlan_enabled(real_dev))
7200 return mlxsw_sp_inetaddr_bridge_event(mlxsw_sp, vlan_dev, event,
7206 static bool mlxsw_sp_rif_macvlan_is_vrrp4(const u8 *mac)
7208 u8 vrrp4[ETH_ALEN] = { 0x00, 0x00, 0x5e, 0x00, 0x01, 0x00 };
7209 u8 mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
7211 return ether_addr_equal_masked(mac, vrrp4, mask);
7214 static bool mlxsw_sp_rif_macvlan_is_vrrp6(const u8 *mac)
7216 u8 vrrp6[ETH_ALEN] = { 0x00, 0x00, 0x5e, 0x00, 0x02, 0x00 };
7217 u8 mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
7219 return ether_addr_equal_masked(mac, vrrp6, mask);
7222 static int mlxsw_sp_rif_vrrp_op(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
7223 const u8 *mac, bool adding)
7225 char ritr_pl[MLXSW_REG_RITR_LEN];
7226 u8 vrrp_id = adding ? mac[5] : 0;
7229 if (!mlxsw_sp_rif_macvlan_is_vrrp4(mac) &&
7230 !mlxsw_sp_rif_macvlan_is_vrrp6(mac))
7233 mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
7234 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
7238 if (mlxsw_sp_rif_macvlan_is_vrrp4(mac))
7239 mlxsw_reg_ritr_if_vrrp_id_ipv4_set(ritr_pl, vrrp_id);
7241 mlxsw_reg_ritr_if_vrrp_id_ipv6_set(ritr_pl, vrrp_id);
7243 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
7246 static int mlxsw_sp_rif_macvlan_add(struct mlxsw_sp *mlxsw_sp,
7247 const struct net_device *macvlan_dev,
7248 struct netlink_ext_ack *extack)
7250 struct macvlan_dev *vlan = netdev_priv(macvlan_dev);
7251 struct mlxsw_sp_rif *rif;
7254 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, vlan->lowerdev);
7256 NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
7260 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, macvlan_dev->dev_addr,
7261 mlxsw_sp_fid_index(rif->fid), true);
7265 err = mlxsw_sp_rif_vrrp_op(mlxsw_sp, rif->rif_index,
7266 macvlan_dev->dev_addr, true);
7268 goto err_rif_vrrp_add;
7270 /* Make sure the bridge driver does not have this MAC pointing at
7273 if (rif->ops->fdb_del)
7274 rif->ops->fdb_del(rif, macvlan_dev->dev_addr);
7279 mlxsw_sp_rif_fdb_op(mlxsw_sp, macvlan_dev->dev_addr,
7280 mlxsw_sp_fid_index(rif->fid), false);
7284 static void __mlxsw_sp_rif_macvlan_del(struct mlxsw_sp *mlxsw_sp,
7285 const struct net_device *macvlan_dev)
7287 struct macvlan_dev *vlan = netdev_priv(macvlan_dev);
7288 struct mlxsw_sp_rif *rif;
7290 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, vlan->lowerdev);
7291 /* If we do not have a RIF, then we already took care of
7292 * removing the macvlan's MAC during RIF deletion.
7296 mlxsw_sp_rif_vrrp_op(mlxsw_sp, rif->rif_index, macvlan_dev->dev_addr,
7298 mlxsw_sp_rif_fdb_op(mlxsw_sp, macvlan_dev->dev_addr,
7299 mlxsw_sp_fid_index(rif->fid), false);
7302 void mlxsw_sp_rif_macvlan_del(struct mlxsw_sp *mlxsw_sp,
7303 const struct net_device *macvlan_dev)
7305 mutex_lock(&mlxsw_sp->router->lock);
7306 __mlxsw_sp_rif_macvlan_del(mlxsw_sp, macvlan_dev);
7307 mutex_unlock(&mlxsw_sp->router->lock);
7310 static int mlxsw_sp_inetaddr_macvlan_event(struct mlxsw_sp *mlxsw_sp,
7311 struct net_device *macvlan_dev,
7312 unsigned long event,
7313 struct netlink_ext_ack *extack)
7317 return mlxsw_sp_rif_macvlan_add(mlxsw_sp, macvlan_dev, extack);
7319 __mlxsw_sp_rif_macvlan_del(mlxsw_sp, macvlan_dev);
7326 static int mlxsw_sp_router_port_check_rif_addr(struct mlxsw_sp *mlxsw_sp,
7327 struct net_device *dev,
7328 const unsigned char *dev_addr,
7329 struct netlink_ext_ack *extack)
7331 struct mlxsw_sp_rif *rif;
7334 /* A RIF is not created for macvlan netdevs. Their MAC is used to
7337 if (netif_is_macvlan(dev) || netif_is_l3_master(dev))
7340 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++) {
7341 rif = mlxsw_sp->router->rifs[i];
7342 if (rif && rif->ops &&
7343 rif->ops->type == MLXSW_SP_RIF_TYPE_IPIP_LB)
7345 if (rif && rif->dev && rif->dev != dev &&
7346 !ether_addr_equal_masked(rif->dev->dev_addr, dev_addr,
7347 mlxsw_sp->mac_mask)) {
7348 NL_SET_ERR_MSG_MOD(extack, "All router interface MAC addresses must have the same prefix");
7356 static int __mlxsw_sp_inetaddr_event(struct mlxsw_sp *mlxsw_sp,
7357 struct net_device *dev,
7358 unsigned long event,
7359 struct netlink_ext_ack *extack)
7361 if (mlxsw_sp_port_dev_check(dev))
7362 return mlxsw_sp_inetaddr_port_event(dev, event, extack);
7363 else if (netif_is_lag_master(dev))
7364 return mlxsw_sp_inetaddr_lag_event(dev, event, extack);
7365 else if (netif_is_bridge_master(dev))
7366 return mlxsw_sp_inetaddr_bridge_event(mlxsw_sp, dev, event,
7368 else if (is_vlan_dev(dev))
7369 return mlxsw_sp_inetaddr_vlan_event(mlxsw_sp, dev, event,
7371 else if (netif_is_macvlan(dev))
7372 return mlxsw_sp_inetaddr_macvlan_event(mlxsw_sp, dev, event,
7378 static int mlxsw_sp_inetaddr_event(struct notifier_block *nb,
7379 unsigned long event, void *ptr)
7381 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
7382 struct net_device *dev = ifa->ifa_dev->dev;
7383 struct mlxsw_sp_router *router;
7384 struct mlxsw_sp_rif *rif;
7387 /* NETDEV_UP event is handled by mlxsw_sp_inetaddr_valid_event */
7388 if (event == NETDEV_UP)
7391 router = container_of(nb, struct mlxsw_sp_router, inetaddr_nb);
7392 mutex_lock(&router->lock);
7393 rif = mlxsw_sp_rif_find_by_dev(router->mlxsw_sp, dev);
7394 if (!mlxsw_sp_rif_should_config(rif, dev, event))
7397 err = __mlxsw_sp_inetaddr_event(router->mlxsw_sp, dev, event, NULL);
7399 mutex_unlock(&router->lock);
7400 return notifier_from_errno(err);
7403 int mlxsw_sp_inetaddr_valid_event(struct notifier_block *unused,
7404 unsigned long event, void *ptr)
7406 struct in_validator_info *ivi = (struct in_validator_info *) ptr;
7407 struct net_device *dev = ivi->ivi_dev->dev;
7408 struct mlxsw_sp *mlxsw_sp;
7409 struct mlxsw_sp_rif *rif;
7412 mlxsw_sp = mlxsw_sp_lower_get(dev);
7416 mutex_lock(&mlxsw_sp->router->lock);
7417 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
7418 if (!mlxsw_sp_rif_should_config(rif, dev, event))
7421 err = mlxsw_sp_router_port_check_rif_addr(mlxsw_sp, dev, dev->dev_addr,
7426 err = __mlxsw_sp_inetaddr_event(mlxsw_sp, dev, event, ivi->extack);
7428 mutex_unlock(&mlxsw_sp->router->lock);
7429 return notifier_from_errno(err);
7432 struct mlxsw_sp_inet6addr_event_work {
7433 struct work_struct work;
7434 struct mlxsw_sp *mlxsw_sp;
7435 struct net_device *dev;
7436 unsigned long event;
7439 static void mlxsw_sp_inet6addr_event_work(struct work_struct *work)
7441 struct mlxsw_sp_inet6addr_event_work *inet6addr_work =
7442 container_of(work, struct mlxsw_sp_inet6addr_event_work, work);
7443 struct mlxsw_sp *mlxsw_sp = inet6addr_work->mlxsw_sp;
7444 struct net_device *dev = inet6addr_work->dev;
7445 unsigned long event = inet6addr_work->event;
7446 struct mlxsw_sp_rif *rif;
7449 mutex_lock(&mlxsw_sp->router->lock);
7451 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
7452 if (!mlxsw_sp_rif_should_config(rif, dev, event))
7455 __mlxsw_sp_inetaddr_event(mlxsw_sp, dev, event, NULL);
7457 mutex_unlock(&mlxsw_sp->router->lock);
7460 kfree(inet6addr_work);
7463 /* Called with rcu_read_lock() */
7464 static int mlxsw_sp_inet6addr_event(struct notifier_block *nb,
7465 unsigned long event, void *ptr)
7467 struct inet6_ifaddr *if6 = (struct inet6_ifaddr *) ptr;
7468 struct mlxsw_sp_inet6addr_event_work *inet6addr_work;
7469 struct net_device *dev = if6->idev->dev;
7470 struct mlxsw_sp_router *router;
7472 /* NETDEV_UP event is handled by mlxsw_sp_inet6addr_valid_event */
7473 if (event == NETDEV_UP)
7476 inet6addr_work = kzalloc(sizeof(*inet6addr_work), GFP_ATOMIC);
7477 if (!inet6addr_work)
7480 router = container_of(nb, struct mlxsw_sp_router, inet6addr_nb);
7481 INIT_WORK(&inet6addr_work->work, mlxsw_sp_inet6addr_event_work);
7482 inet6addr_work->mlxsw_sp = router->mlxsw_sp;
7483 inet6addr_work->dev = dev;
7484 inet6addr_work->event = event;
7486 mlxsw_core_schedule_work(&inet6addr_work->work);
7491 int mlxsw_sp_inet6addr_valid_event(struct notifier_block *unused,
7492 unsigned long event, void *ptr)
7494 struct in6_validator_info *i6vi = (struct in6_validator_info *) ptr;
7495 struct net_device *dev = i6vi->i6vi_dev->dev;
7496 struct mlxsw_sp *mlxsw_sp;
7497 struct mlxsw_sp_rif *rif;
7500 mlxsw_sp = mlxsw_sp_lower_get(dev);
7504 mutex_lock(&mlxsw_sp->router->lock);
7505 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
7506 if (!mlxsw_sp_rif_should_config(rif, dev, event))
7509 err = mlxsw_sp_router_port_check_rif_addr(mlxsw_sp, dev, dev->dev_addr,
7514 err = __mlxsw_sp_inetaddr_event(mlxsw_sp, dev, event, i6vi->extack);
7516 mutex_unlock(&mlxsw_sp->router->lock);
7517 return notifier_from_errno(err);
7520 static int mlxsw_sp_rif_edit(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
7521 const char *mac, int mtu)
7523 char ritr_pl[MLXSW_REG_RITR_LEN];
7526 mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
7527 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
7531 mlxsw_reg_ritr_mtu_set(ritr_pl, mtu);
7532 mlxsw_reg_ritr_if_mac_memcpy_to(ritr_pl, mac);
7533 mlxsw_reg_ritr_op_set(ritr_pl, MLXSW_REG_RITR_RIF_CREATE);
7534 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
7538 mlxsw_sp_router_port_change_event(struct mlxsw_sp *mlxsw_sp,
7539 struct mlxsw_sp_rif *rif)
7541 struct net_device *dev = rif->dev;
7545 fid_index = mlxsw_sp_fid_index(rif->fid);
7547 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, fid_index, false);
7551 err = mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, dev->dev_addr,
7556 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, dev->dev_addr, fid_index, true);
7558 goto err_rif_fdb_op;
7560 if (rif->mtu != dev->mtu) {
7561 struct mlxsw_sp_vr *vr;
7564 /* The RIF is relevant only to its mr_table instance, as unlike
7565 * unicast routing, in multicast routing a RIF cannot be shared
7566 * between several multicast routing tables.
7568 vr = &mlxsw_sp->router->vrs[rif->vr_id];
7569 for (i = 0; i < MLXSW_SP_L3_PROTO_MAX; i++)
7570 mlxsw_sp_mr_rif_mtu_update(vr->mr_table[i],
7574 ether_addr_copy(rif->addr, dev->dev_addr);
7575 rif->mtu = dev->mtu;
7577 netdev_dbg(dev, "Updated RIF=%d\n", rif->rif_index);
7582 mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, rif->addr, rif->mtu);
7584 mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, fid_index, true);
7588 static int mlxsw_sp_router_port_pre_changeaddr_event(struct mlxsw_sp_rif *rif,
7589 struct netdev_notifier_pre_changeaddr_info *info)
7591 struct netlink_ext_ack *extack;
7593 extack = netdev_notifier_info_to_extack(&info->info);
7594 return mlxsw_sp_router_port_check_rif_addr(rif->mlxsw_sp, rif->dev,
7595 info->dev_addr, extack);
7598 int mlxsw_sp_netdevice_router_port_event(struct net_device *dev,
7599 unsigned long event, void *ptr)
7601 struct mlxsw_sp *mlxsw_sp;
7602 struct mlxsw_sp_rif *rif;
7605 mlxsw_sp = mlxsw_sp_lower_get(dev);
7609 mutex_lock(&mlxsw_sp->router->lock);
7610 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
7615 case NETDEV_CHANGEMTU:
7616 case NETDEV_CHANGEADDR:
7617 err = mlxsw_sp_router_port_change_event(mlxsw_sp, rif);
7619 case NETDEV_PRE_CHANGEADDR:
7620 err = mlxsw_sp_router_port_pre_changeaddr_event(rif, ptr);
7625 mutex_unlock(&mlxsw_sp->router->lock);
7629 static int mlxsw_sp_port_vrf_join(struct mlxsw_sp *mlxsw_sp,
7630 struct net_device *l3_dev,
7631 struct netlink_ext_ack *extack)
7633 struct mlxsw_sp_rif *rif;
7635 /* If netdev is already associated with a RIF, then we need to
7636 * destroy it and create a new one with the new virtual router ID.
7638 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
7640 __mlxsw_sp_inetaddr_event(mlxsw_sp, l3_dev, NETDEV_DOWN,
7643 return __mlxsw_sp_inetaddr_event(mlxsw_sp, l3_dev, NETDEV_UP, extack);
7646 static void mlxsw_sp_port_vrf_leave(struct mlxsw_sp *mlxsw_sp,
7647 struct net_device *l3_dev)
7649 struct mlxsw_sp_rif *rif;
7651 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
7654 __mlxsw_sp_inetaddr_event(mlxsw_sp, l3_dev, NETDEV_DOWN, NULL);
7657 int mlxsw_sp_netdevice_vrf_event(struct net_device *l3_dev, unsigned long event,
7658 struct netdev_notifier_changeupper_info *info)
7660 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
7663 /* We do not create a RIF for a macvlan, but only use it to
7664 * direct more MAC addresses to the router.
7666 if (!mlxsw_sp || netif_is_macvlan(l3_dev))
7669 mutex_lock(&mlxsw_sp->router->lock);
7671 case NETDEV_PRECHANGEUPPER:
7673 case NETDEV_CHANGEUPPER:
7674 if (info->linking) {
7675 struct netlink_ext_ack *extack;
7677 extack = netdev_notifier_info_to_extack(&info->info);
7678 err = mlxsw_sp_port_vrf_join(mlxsw_sp, l3_dev, extack);
7680 mlxsw_sp_port_vrf_leave(mlxsw_sp, l3_dev);
7684 mutex_unlock(&mlxsw_sp->router->lock);
7689 static int __mlxsw_sp_rif_macvlan_flush(struct net_device *dev,
7690 struct netdev_nested_priv *priv)
7692 struct mlxsw_sp_rif *rif = (struct mlxsw_sp_rif *)priv->data;
7694 if (!netif_is_macvlan(dev))
7697 return mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, dev->dev_addr,
7698 mlxsw_sp_fid_index(rif->fid), false);
7701 static int mlxsw_sp_rif_macvlan_flush(struct mlxsw_sp_rif *rif)
7703 struct netdev_nested_priv priv = {
7704 .data = (void *)rif,
7707 if (!netif_is_macvlan_port(rif->dev))
7710 netdev_warn(rif->dev, "Router interface is deleted. Upper macvlans will not work\n");
7711 return netdev_walk_all_upper_dev_rcu(rif->dev,
7712 __mlxsw_sp_rif_macvlan_flush, &priv);
7715 static void mlxsw_sp_rif_subport_setup(struct mlxsw_sp_rif *rif,
7716 const struct mlxsw_sp_rif_params *params)
7718 struct mlxsw_sp_rif_subport *rif_subport;
7720 rif_subport = mlxsw_sp_rif_subport_rif(rif);
7721 refcount_set(&rif_subport->ref_count, 1);
7722 rif_subport->vid = params->vid;
7723 rif_subport->lag = params->lag;
7725 rif_subport->lag_id = params->lag_id;
7727 rif_subport->system_port = params->system_port;
7730 static int mlxsw_sp_rif_subport_op(struct mlxsw_sp_rif *rif, bool enable)
7732 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
7733 struct mlxsw_sp_rif_subport *rif_subport;
7734 char ritr_pl[MLXSW_REG_RITR_LEN];
7736 rif_subport = mlxsw_sp_rif_subport_rif(rif);
7737 mlxsw_reg_ritr_pack(ritr_pl, enable, MLXSW_REG_RITR_SP_IF,
7738 rif->rif_index, rif->vr_id, rif->dev->mtu);
7739 mlxsw_reg_ritr_mac_pack(ritr_pl, rif->dev->dev_addr);
7740 mlxsw_reg_ritr_sp_if_pack(ritr_pl, rif_subport->lag,
7741 rif_subport->lag ? rif_subport->lag_id :
7742 rif_subport->system_port,
7745 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
7748 static int mlxsw_sp_rif_subport_configure(struct mlxsw_sp_rif *rif)
7752 err = mlxsw_sp_rif_subport_op(rif, true);
7756 err = mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
7757 mlxsw_sp_fid_index(rif->fid), true);
7759 goto err_rif_fdb_op;
7761 mlxsw_sp_fid_rif_set(rif->fid, rif);
7765 mlxsw_sp_rif_subport_op(rif, false);
7769 static void mlxsw_sp_rif_subport_deconfigure(struct mlxsw_sp_rif *rif)
7771 struct mlxsw_sp_fid *fid = rif->fid;
7773 mlxsw_sp_fid_rif_set(fid, NULL);
7774 mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
7775 mlxsw_sp_fid_index(fid), false);
7776 mlxsw_sp_rif_macvlan_flush(rif);
7777 mlxsw_sp_rif_subport_op(rif, false);
7780 static struct mlxsw_sp_fid *
7781 mlxsw_sp_rif_subport_fid_get(struct mlxsw_sp_rif *rif,
7782 struct netlink_ext_ack *extack)
7784 return mlxsw_sp_fid_rfid_get(rif->mlxsw_sp, rif->rif_index);
7787 static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_subport_ops = {
7788 .type = MLXSW_SP_RIF_TYPE_SUBPORT,
7789 .rif_size = sizeof(struct mlxsw_sp_rif_subport),
7790 .setup = mlxsw_sp_rif_subport_setup,
7791 .configure = mlxsw_sp_rif_subport_configure,
7792 .deconfigure = mlxsw_sp_rif_subport_deconfigure,
7793 .fid_get = mlxsw_sp_rif_subport_fid_get,
7796 static int mlxsw_sp_rif_vlan_fid_op(struct mlxsw_sp_rif *rif,
7797 enum mlxsw_reg_ritr_if_type type,
7798 u16 vid_fid, bool enable)
7800 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
7801 char ritr_pl[MLXSW_REG_RITR_LEN];
7803 mlxsw_reg_ritr_pack(ritr_pl, enable, type, rif->rif_index, rif->vr_id,
7805 mlxsw_reg_ritr_mac_pack(ritr_pl, rif->dev->dev_addr);
7806 mlxsw_reg_ritr_fid_set(ritr_pl, type, vid_fid);
7808 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
7811 u8 mlxsw_sp_router_port(const struct mlxsw_sp *mlxsw_sp)
7813 return mlxsw_core_max_ports(mlxsw_sp->core) + 1;
7816 static int mlxsw_sp_rif_fid_configure(struct mlxsw_sp_rif *rif)
7818 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
7819 u16 fid_index = mlxsw_sp_fid_index(rif->fid);
7822 err = mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index,
7827 err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
7828 mlxsw_sp_router_port(mlxsw_sp), true);
7830 goto err_fid_mc_flood_set;
7832 err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
7833 mlxsw_sp_router_port(mlxsw_sp), true);
7835 goto err_fid_bc_flood_set;
7837 err = mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
7838 mlxsw_sp_fid_index(rif->fid), true);
7840 goto err_rif_fdb_op;
7842 mlxsw_sp_fid_rif_set(rif->fid, rif);
7846 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
7847 mlxsw_sp_router_port(mlxsw_sp), false);
7848 err_fid_bc_flood_set:
7849 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
7850 mlxsw_sp_router_port(mlxsw_sp), false);
7851 err_fid_mc_flood_set:
7852 mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index, false);
7856 static void mlxsw_sp_rif_fid_deconfigure(struct mlxsw_sp_rif *rif)
7858 u16 fid_index = mlxsw_sp_fid_index(rif->fid);
7859 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
7860 struct mlxsw_sp_fid *fid = rif->fid;
7862 mlxsw_sp_fid_rif_set(fid, NULL);
7863 mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
7864 mlxsw_sp_fid_index(fid), false);
7865 mlxsw_sp_rif_macvlan_flush(rif);
7866 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
7867 mlxsw_sp_router_port(mlxsw_sp), false);
7868 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
7869 mlxsw_sp_router_port(mlxsw_sp), false);
7870 mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index, false);
7873 static struct mlxsw_sp_fid *
7874 mlxsw_sp_rif_fid_fid_get(struct mlxsw_sp_rif *rif,
7875 struct netlink_ext_ack *extack)
7877 return mlxsw_sp_fid_8021d_get(rif->mlxsw_sp, rif->dev->ifindex);
7880 static void mlxsw_sp_rif_fid_fdb_del(struct mlxsw_sp_rif *rif, const char *mac)
7882 struct switchdev_notifier_fdb_info info;
7883 struct net_device *dev;
7885 dev = br_fdb_find_port(rif->dev, mac, 0);
7891 call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE, dev, &info.info,
7895 static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_fid_ops = {
7896 .type = MLXSW_SP_RIF_TYPE_FID,
7897 .rif_size = sizeof(struct mlxsw_sp_rif),
7898 .configure = mlxsw_sp_rif_fid_configure,
7899 .deconfigure = mlxsw_sp_rif_fid_deconfigure,
7900 .fid_get = mlxsw_sp_rif_fid_fid_get,
7901 .fdb_del = mlxsw_sp_rif_fid_fdb_del,
7904 static struct mlxsw_sp_fid *
7905 mlxsw_sp_rif_vlan_fid_get(struct mlxsw_sp_rif *rif,
7906 struct netlink_ext_ack *extack)
7908 struct net_device *br_dev;
7912 if (is_vlan_dev(rif->dev)) {
7913 vid = vlan_dev_vlan_id(rif->dev);
7914 br_dev = vlan_dev_real_dev(rif->dev);
7915 if (WARN_ON(!netif_is_bridge_master(br_dev)))
7916 return ERR_PTR(-EINVAL);
7918 err = br_vlan_get_pvid(rif->dev, &vid);
7919 if (err < 0 || !vid) {
7920 NL_SET_ERR_MSG_MOD(extack, "Couldn't determine bridge PVID");
7921 return ERR_PTR(-EINVAL);
7925 return mlxsw_sp_fid_8021q_get(rif->mlxsw_sp, vid);
7928 static void mlxsw_sp_rif_vlan_fdb_del(struct mlxsw_sp_rif *rif, const char *mac)
7930 u16 vid = mlxsw_sp_fid_8021q_vid(rif->fid);
7931 struct switchdev_notifier_fdb_info info;
7932 struct net_device *br_dev;
7933 struct net_device *dev;
7935 br_dev = is_vlan_dev(rif->dev) ? vlan_dev_real_dev(rif->dev) : rif->dev;
7936 dev = br_fdb_find_port(br_dev, mac, vid);
7942 call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE, dev, &info.info,
7946 static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_vlan_emu_ops = {
7947 .type = MLXSW_SP_RIF_TYPE_VLAN,
7948 .rif_size = sizeof(struct mlxsw_sp_rif),
7949 .configure = mlxsw_sp_rif_fid_configure,
7950 .deconfigure = mlxsw_sp_rif_fid_deconfigure,
7951 .fid_get = mlxsw_sp_rif_vlan_fid_get,
7952 .fdb_del = mlxsw_sp_rif_vlan_fdb_del,
7955 static struct mlxsw_sp_rif_ipip_lb *
7956 mlxsw_sp_rif_ipip_lb_rif(struct mlxsw_sp_rif *rif)
7958 return container_of(rif, struct mlxsw_sp_rif_ipip_lb, common);
7962 mlxsw_sp_rif_ipip_lb_setup(struct mlxsw_sp_rif *rif,
7963 const struct mlxsw_sp_rif_params *params)
7965 struct mlxsw_sp_rif_params_ipip_lb *params_lb;
7966 struct mlxsw_sp_rif_ipip_lb *rif_lb;
7968 params_lb = container_of(params, struct mlxsw_sp_rif_params_ipip_lb,
7970 rif_lb = mlxsw_sp_rif_ipip_lb_rif(rif);
7971 rif_lb->lb_config = params_lb->lb_config;
7975 mlxsw_sp1_rif_ipip_lb_configure(struct mlxsw_sp_rif *rif)
7977 struct mlxsw_sp_rif_ipip_lb *lb_rif = mlxsw_sp_rif_ipip_lb_rif(rif);
7978 u32 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(rif->dev);
7979 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
7980 struct mlxsw_sp_vr *ul_vr;
7983 ul_vr = mlxsw_sp_vr_get(mlxsw_sp, ul_tb_id, NULL);
7985 return PTR_ERR(ul_vr);
7987 err = mlxsw_sp_rif_ipip_lb_op(lb_rif, ul_vr->id, 0, true);
7989 goto err_loopback_op;
7991 lb_rif->ul_vr_id = ul_vr->id;
7992 lb_rif->ul_rif_id = 0;
7997 mlxsw_sp_vr_put(mlxsw_sp, ul_vr);
8001 static void mlxsw_sp1_rif_ipip_lb_deconfigure(struct mlxsw_sp_rif *rif)
8003 struct mlxsw_sp_rif_ipip_lb *lb_rif = mlxsw_sp_rif_ipip_lb_rif(rif);
8004 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
8005 struct mlxsw_sp_vr *ul_vr;
8007 ul_vr = &mlxsw_sp->router->vrs[lb_rif->ul_vr_id];
8008 mlxsw_sp_rif_ipip_lb_op(lb_rif, ul_vr->id, 0, false);
8011 mlxsw_sp_vr_put(mlxsw_sp, ul_vr);
8014 static const struct mlxsw_sp_rif_ops mlxsw_sp1_rif_ipip_lb_ops = {
8015 .type = MLXSW_SP_RIF_TYPE_IPIP_LB,
8016 .rif_size = sizeof(struct mlxsw_sp_rif_ipip_lb),
8017 .setup = mlxsw_sp_rif_ipip_lb_setup,
8018 .configure = mlxsw_sp1_rif_ipip_lb_configure,
8019 .deconfigure = mlxsw_sp1_rif_ipip_lb_deconfigure,
8022 const struct mlxsw_sp_rif_ops *mlxsw_sp1_rif_ops_arr[] = {
8023 [MLXSW_SP_RIF_TYPE_SUBPORT] = &mlxsw_sp_rif_subport_ops,
8024 [MLXSW_SP_RIF_TYPE_VLAN] = &mlxsw_sp_rif_vlan_emu_ops,
8025 [MLXSW_SP_RIF_TYPE_FID] = &mlxsw_sp_rif_fid_ops,
8026 [MLXSW_SP_RIF_TYPE_IPIP_LB] = &mlxsw_sp1_rif_ipip_lb_ops,
8030 mlxsw_sp_rif_ipip_lb_ul_rif_op(struct mlxsw_sp_rif *ul_rif, bool enable)
8032 struct mlxsw_sp *mlxsw_sp = ul_rif->mlxsw_sp;
8033 char ritr_pl[MLXSW_REG_RITR_LEN];
8035 mlxsw_reg_ritr_pack(ritr_pl, enable, MLXSW_REG_RITR_LOOPBACK_IF,
8036 ul_rif->rif_index, ul_rif->vr_id, IP_MAX_MTU);
8037 mlxsw_reg_ritr_loopback_protocol_set(ritr_pl,
8038 MLXSW_REG_RITR_LOOPBACK_GENERIC);
8040 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
8043 static struct mlxsw_sp_rif *
8044 mlxsw_sp_ul_rif_create(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr,
8045 struct netlink_ext_ack *extack)
8047 struct mlxsw_sp_rif *ul_rif;
8051 err = mlxsw_sp_rif_index_alloc(mlxsw_sp, &rif_index);
8053 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported router interfaces");
8054 return ERR_PTR(err);
8057 ul_rif = mlxsw_sp_rif_alloc(sizeof(*ul_rif), rif_index, vr->id, NULL);
8059 return ERR_PTR(-ENOMEM);
8061 mlxsw_sp->router->rifs[rif_index] = ul_rif;
8062 ul_rif->mlxsw_sp = mlxsw_sp;
8063 err = mlxsw_sp_rif_ipip_lb_ul_rif_op(ul_rif, true);
8070 mlxsw_sp->router->rifs[rif_index] = NULL;
8072 return ERR_PTR(err);
8075 static void mlxsw_sp_ul_rif_destroy(struct mlxsw_sp_rif *ul_rif)
8077 struct mlxsw_sp *mlxsw_sp = ul_rif->mlxsw_sp;
8079 mlxsw_sp_rif_ipip_lb_ul_rif_op(ul_rif, false);
8080 mlxsw_sp->router->rifs[ul_rif->rif_index] = NULL;
8084 static struct mlxsw_sp_rif *
8085 mlxsw_sp_ul_rif_get(struct mlxsw_sp *mlxsw_sp, u32 tb_id,
8086 struct netlink_ext_ack *extack)
8088 struct mlxsw_sp_vr *vr;
8091 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id, extack);
8093 return ERR_CAST(vr);
8095 if (refcount_inc_not_zero(&vr->ul_rif_refcnt))
8098 vr->ul_rif = mlxsw_sp_ul_rif_create(mlxsw_sp, vr, extack);
8099 if (IS_ERR(vr->ul_rif)) {
8100 err = PTR_ERR(vr->ul_rif);
8101 goto err_ul_rif_create;
8105 refcount_set(&vr->ul_rif_refcnt, 1);
8110 mlxsw_sp_vr_put(mlxsw_sp, vr);
8111 return ERR_PTR(err);
8114 static void mlxsw_sp_ul_rif_put(struct mlxsw_sp_rif *ul_rif)
8116 struct mlxsw_sp *mlxsw_sp = ul_rif->mlxsw_sp;
8117 struct mlxsw_sp_vr *vr;
8119 vr = &mlxsw_sp->router->vrs[ul_rif->vr_id];
8121 if (!refcount_dec_and_test(&vr->ul_rif_refcnt))
8125 mlxsw_sp_ul_rif_destroy(ul_rif);
8126 mlxsw_sp_vr_put(mlxsw_sp, vr);
8129 int mlxsw_sp_router_ul_rif_get(struct mlxsw_sp *mlxsw_sp, u32 ul_tb_id,
8132 struct mlxsw_sp_rif *ul_rif;
8135 mutex_lock(&mlxsw_sp->router->lock);
8136 ul_rif = mlxsw_sp_ul_rif_get(mlxsw_sp, ul_tb_id, NULL);
8137 if (IS_ERR(ul_rif)) {
8138 err = PTR_ERR(ul_rif);
8141 *ul_rif_index = ul_rif->rif_index;
8143 mutex_unlock(&mlxsw_sp->router->lock);
8147 void mlxsw_sp_router_ul_rif_put(struct mlxsw_sp *mlxsw_sp, u16 ul_rif_index)
8149 struct mlxsw_sp_rif *ul_rif;
8151 mutex_lock(&mlxsw_sp->router->lock);
8152 ul_rif = mlxsw_sp->router->rifs[ul_rif_index];
8153 if (WARN_ON(!ul_rif))
8156 mlxsw_sp_ul_rif_put(ul_rif);
8158 mutex_unlock(&mlxsw_sp->router->lock);
8162 mlxsw_sp2_rif_ipip_lb_configure(struct mlxsw_sp_rif *rif)
8164 struct mlxsw_sp_rif_ipip_lb *lb_rif = mlxsw_sp_rif_ipip_lb_rif(rif);
8165 u32 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(rif->dev);
8166 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
8167 struct mlxsw_sp_rif *ul_rif;
8170 ul_rif = mlxsw_sp_ul_rif_get(mlxsw_sp, ul_tb_id, NULL);
8172 return PTR_ERR(ul_rif);
8174 err = mlxsw_sp_rif_ipip_lb_op(lb_rif, 0, ul_rif->rif_index, true);
8176 goto err_loopback_op;
8178 lb_rif->ul_vr_id = 0;
8179 lb_rif->ul_rif_id = ul_rif->rif_index;
8184 mlxsw_sp_ul_rif_put(ul_rif);
8188 static void mlxsw_sp2_rif_ipip_lb_deconfigure(struct mlxsw_sp_rif *rif)
8190 struct mlxsw_sp_rif_ipip_lb *lb_rif = mlxsw_sp_rif_ipip_lb_rif(rif);
8191 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
8192 struct mlxsw_sp_rif *ul_rif;
8194 ul_rif = mlxsw_sp_rif_by_index(mlxsw_sp, lb_rif->ul_rif_id);
8195 mlxsw_sp_rif_ipip_lb_op(lb_rif, 0, lb_rif->ul_rif_id, false);
8196 mlxsw_sp_ul_rif_put(ul_rif);
8199 static const struct mlxsw_sp_rif_ops mlxsw_sp2_rif_ipip_lb_ops = {
8200 .type = MLXSW_SP_RIF_TYPE_IPIP_LB,
8201 .rif_size = sizeof(struct mlxsw_sp_rif_ipip_lb),
8202 .setup = mlxsw_sp_rif_ipip_lb_setup,
8203 .configure = mlxsw_sp2_rif_ipip_lb_configure,
8204 .deconfigure = mlxsw_sp2_rif_ipip_lb_deconfigure,
8207 const struct mlxsw_sp_rif_ops *mlxsw_sp2_rif_ops_arr[] = {
8208 [MLXSW_SP_RIF_TYPE_SUBPORT] = &mlxsw_sp_rif_subport_ops,
8209 [MLXSW_SP_RIF_TYPE_VLAN] = &mlxsw_sp_rif_vlan_emu_ops,
8210 [MLXSW_SP_RIF_TYPE_FID] = &mlxsw_sp_rif_fid_ops,
8211 [MLXSW_SP_RIF_TYPE_IPIP_LB] = &mlxsw_sp2_rif_ipip_lb_ops,
8214 static int mlxsw_sp_rifs_init(struct mlxsw_sp *mlxsw_sp)
8216 u64 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
8218 mlxsw_sp->router->rifs = kcalloc(max_rifs,
8219 sizeof(struct mlxsw_sp_rif *),
8221 if (!mlxsw_sp->router->rifs)
8227 static void mlxsw_sp_rifs_fini(struct mlxsw_sp *mlxsw_sp)
8231 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
8232 WARN_ON_ONCE(mlxsw_sp->router->rifs[i]);
8234 kfree(mlxsw_sp->router->rifs);
8238 mlxsw_sp_ipip_config_tigcr(struct mlxsw_sp *mlxsw_sp)
8240 char tigcr_pl[MLXSW_REG_TIGCR_LEN];
8242 mlxsw_reg_tigcr_pack(tigcr_pl, true, 0);
8243 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(tigcr), tigcr_pl);
8246 static int mlxsw_sp_ipips_init(struct mlxsw_sp *mlxsw_sp)
8250 mlxsw_sp->router->ipip_ops_arr = mlxsw_sp_ipip_ops_arr;
8251 INIT_LIST_HEAD(&mlxsw_sp->router->ipip_list);
8253 err = mlxsw_sp_ipip_ecn_encap_init(mlxsw_sp);
8256 err = mlxsw_sp_ipip_ecn_decap_init(mlxsw_sp);
8260 return mlxsw_sp_ipip_config_tigcr(mlxsw_sp);
8263 static void mlxsw_sp_ipips_fini(struct mlxsw_sp *mlxsw_sp)
8265 WARN_ON(!list_empty(&mlxsw_sp->router->ipip_list));
8268 static void mlxsw_sp_router_fib_dump_flush(struct notifier_block *nb)
8270 struct mlxsw_sp_router *router;
8272 /* Flush pending FIB notifications and then flush the device's
8273 * table before requesting another dump. The FIB notification
8274 * block is unregistered, so no need to take RTNL.
8276 mlxsw_core_flush_owq();
8277 router = container_of(nb, struct mlxsw_sp_router, fib_nb);
8278 mlxsw_sp_router_fib_flush(router->mlxsw_sp);
8281 #ifdef CONFIG_IP_ROUTE_MULTIPATH
8282 static void mlxsw_sp_mp_hash_header_set(char *recr2_pl, int header)
8284 mlxsw_reg_recr2_outer_header_enables_set(recr2_pl, header, true);
8287 static void mlxsw_sp_mp_hash_field_set(char *recr2_pl, int field)
8289 mlxsw_reg_recr2_outer_header_fields_enable_set(recr2_pl, field, true);
8292 static void mlxsw_sp_mp4_hash_init(struct mlxsw_sp *mlxsw_sp, char *recr2_pl)
8294 struct net *net = mlxsw_sp_net(mlxsw_sp);
8295 bool only_l3 = !net->ipv4.sysctl_fib_multipath_hash_policy;
8297 mlxsw_sp_mp_hash_header_set(recr2_pl,
8298 MLXSW_REG_RECR2_IPV4_EN_NOT_TCP_NOT_UDP);
8299 mlxsw_sp_mp_hash_header_set(recr2_pl, MLXSW_REG_RECR2_IPV4_EN_TCP_UDP);
8300 mlxsw_reg_recr2_ipv4_sip_enable(recr2_pl);
8301 mlxsw_reg_recr2_ipv4_dip_enable(recr2_pl);
8304 mlxsw_sp_mp_hash_header_set(recr2_pl, MLXSW_REG_RECR2_TCP_UDP_EN_IPV4);
8305 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_IPV4_PROTOCOL);
8306 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_TCP_UDP_SPORT);
8307 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_TCP_UDP_DPORT);
8310 static void mlxsw_sp_mp6_hash_init(struct mlxsw_sp *mlxsw_sp, char *recr2_pl)
8312 bool only_l3 = !ip6_multipath_hash_policy(mlxsw_sp_net(mlxsw_sp));
8314 mlxsw_sp_mp_hash_header_set(recr2_pl,
8315 MLXSW_REG_RECR2_IPV6_EN_NOT_TCP_NOT_UDP);
8316 mlxsw_sp_mp_hash_header_set(recr2_pl, MLXSW_REG_RECR2_IPV6_EN_TCP_UDP);
8317 mlxsw_reg_recr2_ipv6_sip_enable(recr2_pl);
8318 mlxsw_reg_recr2_ipv6_dip_enable(recr2_pl);
8319 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_IPV6_NEXT_HEADER);
8321 mlxsw_sp_mp_hash_field_set(recr2_pl,
8322 MLXSW_REG_RECR2_IPV6_FLOW_LABEL);
8324 mlxsw_sp_mp_hash_header_set(recr2_pl,
8325 MLXSW_REG_RECR2_TCP_UDP_EN_IPV6);
8326 mlxsw_sp_mp_hash_field_set(recr2_pl,
8327 MLXSW_REG_RECR2_TCP_UDP_SPORT);
8328 mlxsw_sp_mp_hash_field_set(recr2_pl,
8329 MLXSW_REG_RECR2_TCP_UDP_DPORT);
8333 static int mlxsw_sp_mp_hash_init(struct mlxsw_sp *mlxsw_sp)
8335 char recr2_pl[MLXSW_REG_RECR2_LEN];
8338 seed = jhash(mlxsw_sp->base_mac, sizeof(mlxsw_sp->base_mac), 0);
8339 mlxsw_reg_recr2_pack(recr2_pl, seed);
8340 mlxsw_sp_mp4_hash_init(mlxsw_sp, recr2_pl);
8341 mlxsw_sp_mp6_hash_init(mlxsw_sp, recr2_pl);
8343 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(recr2), recr2_pl);
8346 static int mlxsw_sp_mp_hash_init(struct mlxsw_sp *mlxsw_sp)
8352 static int mlxsw_sp_dscp_init(struct mlxsw_sp *mlxsw_sp)
8354 char rdpm_pl[MLXSW_REG_RDPM_LEN];
8357 MLXSW_REG_ZERO(rdpm, rdpm_pl);
8359 /* HW is determining switch priority based on DSCP-bits, but the
8360 * kernel is still doing that based on the ToS. Since there's a
8361 * mismatch in bits we need to make sure to translate the right
8362 * value ToS would observe, skipping the 2 least-significant ECN bits.
8364 for (i = 0; i < MLXSW_REG_RDPM_DSCP_ENTRY_REC_MAX_COUNT; i++)
8365 mlxsw_reg_rdpm_pack(rdpm_pl, i, rt_tos2priority(i << 2));
8367 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rdpm), rdpm_pl);
8370 static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
8372 struct net *net = mlxsw_sp_net(mlxsw_sp);
8373 bool usp = net->ipv4.sysctl_ip_fwd_update_priority;
8374 char rgcr_pl[MLXSW_REG_RGCR_LEN];
8377 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_RIFS))
8379 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
8381 mlxsw_reg_rgcr_pack(rgcr_pl, true, true);
8382 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, max_rifs);
8383 mlxsw_reg_rgcr_usp_set(rgcr_pl, usp);
8384 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
8387 static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
8389 char rgcr_pl[MLXSW_REG_RGCR_LEN];
8391 mlxsw_reg_rgcr_pack(rgcr_pl, false, false);
8392 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
8395 static const struct mlxsw_sp_router_ll_ops mlxsw_sp_router_ll_basic_ops = {
8396 .ralta_write = mlxsw_sp_router_ll_basic_ralta_write,
8397 .ralst_write = mlxsw_sp_router_ll_basic_ralst_write,
8398 .raltb_write = mlxsw_sp_router_ll_basic_raltb_write,
8399 .fib_entry_op_ctx_size = sizeof(struct mlxsw_sp_fib_entry_op_ctx_basic),
8400 .fib_entry_pack = mlxsw_sp_router_ll_basic_fib_entry_pack,
8401 .fib_entry_act_remote_pack = mlxsw_sp_router_ll_basic_fib_entry_act_remote_pack,
8402 .fib_entry_act_local_pack = mlxsw_sp_router_ll_basic_fib_entry_act_local_pack,
8403 .fib_entry_act_ip2me_pack = mlxsw_sp_router_ll_basic_fib_entry_act_ip2me_pack,
8404 .fib_entry_act_ip2me_tun_pack = mlxsw_sp_router_ll_basic_fib_entry_act_ip2me_tun_pack,
8405 .fib_entry_commit = mlxsw_sp_router_ll_basic_fib_entry_commit,
8406 .fib_entry_is_committed = mlxsw_sp_router_ll_basic_fib_entry_is_committed,
8409 static int mlxsw_sp_router_ll_op_ctx_init(struct mlxsw_sp_router *router)
8411 size_t max_size = 0;
8414 for (i = 0; i < MLXSW_SP_L3_PROTO_MAX; i++) {
8415 size_t size = router->proto_ll_ops[i]->fib_entry_op_ctx_size;
8417 if (size > max_size)
8420 router->ll_op_ctx = kzalloc(sizeof(*router->ll_op_ctx) + max_size,
8422 if (!router->ll_op_ctx)
8424 INIT_LIST_HEAD(&router->ll_op_ctx->fib_entry_priv_list);
8428 static void mlxsw_sp_router_ll_op_ctx_fini(struct mlxsw_sp_router *router)
8430 WARN_ON(!list_empty(&router->ll_op_ctx->fib_entry_priv_list));
8431 kfree(router->ll_op_ctx);
8434 int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp,
8435 struct netlink_ext_ack *extack)
8437 struct mlxsw_sp_router *router;
8440 router = kzalloc(sizeof(*mlxsw_sp->router), GFP_KERNEL);
8443 mutex_init(&router->lock);
8444 mlxsw_sp->router = router;
8445 router->mlxsw_sp = mlxsw_sp;
8447 router->proto_ll_ops[MLXSW_SP_L3_PROTO_IPV4] = &mlxsw_sp_router_ll_basic_ops;
8448 router->proto_ll_ops[MLXSW_SP_L3_PROTO_IPV6] = &mlxsw_sp_router_ll_basic_ops;
8450 err = mlxsw_sp_router_ll_op_ctx_init(router);
8452 goto err_ll_op_ctx_init;
8454 INIT_LIST_HEAD(&mlxsw_sp->router->nexthop_neighs_list);
8455 err = __mlxsw_sp_router_init(mlxsw_sp);
8457 goto err_router_init;
8459 err = mlxsw_sp_rifs_init(mlxsw_sp);
8463 err = mlxsw_sp_ipips_init(mlxsw_sp);
8465 goto err_ipips_init;
8467 err = rhashtable_init(&mlxsw_sp->router->nexthop_ht,
8468 &mlxsw_sp_nexthop_ht_params);
8470 goto err_nexthop_ht_init;
8472 err = rhashtable_init(&mlxsw_sp->router->nexthop_group_ht,
8473 &mlxsw_sp_nexthop_group_ht_params);
8475 goto err_nexthop_group_ht_init;
8477 INIT_LIST_HEAD(&mlxsw_sp->router->nexthop_list);
8478 err = mlxsw_sp_lpm_init(mlxsw_sp);
8482 err = mlxsw_sp_mr_init(mlxsw_sp, &mlxsw_sp_mr_tcam_ops);
8486 err = mlxsw_sp_vrs_init(mlxsw_sp);
8490 err = mlxsw_sp_neigh_init(mlxsw_sp);
8492 goto err_neigh_init;
8494 err = mlxsw_sp_mp_hash_init(mlxsw_sp);
8496 goto err_mp_hash_init;
8498 err = mlxsw_sp_dscp_init(mlxsw_sp);
8502 INIT_WORK(&router->fib_event_work, mlxsw_sp_router_fib_event_work);
8503 INIT_LIST_HEAD(&router->fib_event_queue);
8504 spin_lock_init(&router->fib_event_queue_lock);
8506 router->inetaddr_nb.notifier_call = mlxsw_sp_inetaddr_event;
8507 err = register_inetaddr_notifier(&router->inetaddr_nb);
8509 goto err_register_inetaddr_notifier;
8511 router->inet6addr_nb.notifier_call = mlxsw_sp_inet6addr_event;
8512 err = register_inet6addr_notifier(&router->inet6addr_nb);
8514 goto err_register_inet6addr_notifier;
8516 mlxsw_sp->router->netevent_nb.notifier_call =
8517 mlxsw_sp_router_netevent_event;
8518 err = register_netevent_notifier(&mlxsw_sp->router->netevent_nb);
8520 goto err_register_netevent_notifier;
8522 mlxsw_sp->router->fib_nb.notifier_call = mlxsw_sp_router_fib_event;
8523 err = register_fib_notifier(mlxsw_sp_net(mlxsw_sp),
8524 &mlxsw_sp->router->fib_nb,
8525 mlxsw_sp_router_fib_dump_flush, extack);
8527 goto err_register_fib_notifier;
8531 err_register_fib_notifier:
8532 unregister_netevent_notifier(&mlxsw_sp->router->netevent_nb);
8533 err_register_netevent_notifier:
8534 unregister_inet6addr_notifier(&router->inet6addr_nb);
8535 err_register_inet6addr_notifier:
8536 unregister_inetaddr_notifier(&router->inetaddr_nb);
8537 err_register_inetaddr_notifier:
8538 mlxsw_core_flush_owq();
8539 WARN_ON(!list_empty(&router->fib_event_queue));
8542 mlxsw_sp_neigh_fini(mlxsw_sp);
8544 mlxsw_sp_vrs_fini(mlxsw_sp);
8546 mlxsw_sp_mr_fini(mlxsw_sp);
8548 mlxsw_sp_lpm_fini(mlxsw_sp);
8550 rhashtable_destroy(&mlxsw_sp->router->nexthop_group_ht);
8551 err_nexthop_group_ht_init:
8552 rhashtable_destroy(&mlxsw_sp->router->nexthop_ht);
8553 err_nexthop_ht_init:
8554 mlxsw_sp_ipips_fini(mlxsw_sp);
8556 mlxsw_sp_rifs_fini(mlxsw_sp);
8558 __mlxsw_sp_router_fini(mlxsw_sp);
8560 mlxsw_sp_router_ll_op_ctx_fini(router);
8562 mutex_destroy(&mlxsw_sp->router->lock);
8563 kfree(mlxsw_sp->router);
8567 void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
8569 unregister_fib_notifier(mlxsw_sp_net(mlxsw_sp),
8570 &mlxsw_sp->router->fib_nb);
8571 unregister_netevent_notifier(&mlxsw_sp->router->netevent_nb);
8572 unregister_inet6addr_notifier(&mlxsw_sp->router->inet6addr_nb);
8573 unregister_inetaddr_notifier(&mlxsw_sp->router->inetaddr_nb);
8574 mlxsw_core_flush_owq();
8575 WARN_ON(!list_empty(&mlxsw_sp->router->fib_event_queue));
8576 mlxsw_sp_neigh_fini(mlxsw_sp);
8577 mlxsw_sp_vrs_fini(mlxsw_sp);
8578 mlxsw_sp_mr_fini(mlxsw_sp);
8579 mlxsw_sp_lpm_fini(mlxsw_sp);
8580 rhashtable_destroy(&mlxsw_sp->router->nexthop_group_ht);
8581 rhashtable_destroy(&mlxsw_sp->router->nexthop_ht);
8582 mlxsw_sp_ipips_fini(mlxsw_sp);
8583 mlxsw_sp_rifs_fini(mlxsw_sp);
8584 __mlxsw_sp_router_fini(mlxsw_sp);
8585 mlxsw_sp_router_ll_op_ctx_fini(mlxsw_sp->router);
8586 mutex_destroy(&mlxsw_sp->router->lock);
8587 kfree(mlxsw_sp->router);