1 /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */
2 /* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
3 
4 #ifndef _MLXSW_ROUTER_H_
5 #define _MLXSW_ROUTER_H_
6 
7 #include "spectrum.h"
8 #include "reg.h"
9 
10 struct mlxsw_sp_router_nve_decap {
11 	u32 ul_tb_id;
12 	u32 tunnel_index;
13 	enum mlxsw_sp_l3proto ul_proto;
14 	union mlxsw_sp_l3addr ul_sip;
15 	u8 valid:1;
16 };
17 
18 struct mlxsw_sp_fib_entry_op_ctx {
19 	u8 bulk_ok:1, /* Indicate to the low-level op it is ok to bulk
20 		       * the actual entry with the one that is the next
21 		       * in queue.
22 		       */
23 	   initialized:1; /* Bit that the low-level op sets in case
24 			   * the context priv is initialized.
25 			   */
26 	struct list_head fib_entry_priv_list;
27 	unsigned long event;
28 	unsigned long ll_priv[];
29 };
30 
31 static inline void
32 mlxsw_sp_fib_entry_op_ctx_clear(struct mlxsw_sp_fib_entry_op_ctx *op_ctx)
33 {
34 	WARN_ON_ONCE(!list_empty(&op_ctx->fib_entry_priv_list));
35 	memset(op_ctx, 0, sizeof(*op_ctx));
36 	INIT_LIST_HEAD(&op_ctx->fib_entry_priv_list);
37 }
38 
39 struct mlxsw_sp_router {
40 	struct mlxsw_sp *mlxsw_sp;
41 	struct mlxsw_sp_rif **rifs;
42 	struct mlxsw_sp_vr *vrs;
43 	struct rhashtable neigh_ht;
44 	struct rhashtable nexthop_group_ht;
45 	struct rhashtable nexthop_ht;
46 	struct list_head nexthop_list;
47 	struct {
48 		/* One tree for each protocol: IPv4 and IPv6 */
49 		struct mlxsw_sp_lpm_tree *proto_trees[2];
50 		struct mlxsw_sp_lpm_tree *trees;
51 		unsigned int tree_count;
52 	} lpm;
53 	struct {
54 		struct delayed_work dw;
55 		unsigned long interval;	/* ms */
56 	} neighs_update;
57 	struct delayed_work nexthop_probe_dw;
58 #define MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL 5000 /* ms */
59 	struct list_head nexthop_neighs_list;
60 	struct list_head ipip_list;
61 	bool aborted;
62 	struct notifier_block nexthop_nb;
63 	struct notifier_block fib_nb;
64 	struct notifier_block netevent_nb;
65 	struct notifier_block inetaddr_nb;
66 	struct notifier_block inet6addr_nb;
67 	const struct mlxsw_sp_rif_ops **rif_ops_arr;
68 	const struct mlxsw_sp_ipip_ops **ipip_ops_arr;
69 	u32 adj_discard_index;
70 	bool adj_discard_index_valid;
71 	struct mlxsw_sp_router_nve_decap nve_decap_config;
72 	struct mutex lock; /* Protects shared router resources */
73 	struct work_struct fib_event_work;
74 	struct list_head fib_event_queue;
75 	spinlock_t fib_event_queue_lock; /* Protects fib event queue list */
76 	/* One set of ops for each protocol: IPv4 and IPv6 */
77 	const struct mlxsw_sp_router_ll_ops *proto_ll_ops[MLXSW_SP_L3_PROTO_MAX];
78 	struct mlxsw_sp_fib_entry_op_ctx *ll_op_ctx;
79 	u16 lb_rif_index;
80 	struct mlxsw_sp_router_xm *xm;
81 };
82 
83 struct mlxsw_sp_fib_entry_priv {
84 	refcount_t refcnt;
85 	struct list_head list; /* Member in op_ctx->fib_entry_priv_list */
86 	unsigned long priv[];
87 };
88 
89 enum mlxsw_sp_fib_entry_op {
90 	MLXSW_SP_FIB_ENTRY_OP_WRITE,
91 	MLXSW_SP_FIB_ENTRY_OP_UPDATE,
92 	MLXSW_SP_FIB_ENTRY_OP_DELETE,
93 };
94 
95 /* Low-level router ops. Basically this is to handle the different
96  * register sets to work with ordinary and XM trees and FIB entries.
97  */
98 struct mlxsw_sp_router_ll_ops {
99 	int (*init)(struct mlxsw_sp *mlxsw_sp, u16 vr_id,
100 		    enum mlxsw_sp_l3proto proto);
101 	int (*ralta_write)(struct mlxsw_sp *mlxsw_sp, char *xralta_pl);
102 	int (*ralst_write)(struct mlxsw_sp *mlxsw_sp, char *xralst_pl);
103 	int (*raltb_write)(struct mlxsw_sp *mlxsw_sp, char *xraltb_pl);
104 	size_t fib_entry_op_ctx_size;
105 	size_t fib_entry_priv_size;
106 	void (*fib_entry_pack)(struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
107 			       enum mlxsw_sp_l3proto proto, enum mlxsw_sp_fib_entry_op op,
108 			       u16 virtual_router, u8 prefix_len, unsigned char *addr,
109 			       struct mlxsw_sp_fib_entry_priv *priv);
110 	void (*fib_entry_act_remote_pack)(struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
111 					  enum mlxsw_reg_ralue_trap_action trap_action,
112 					  u16 trap_id, u32 adjacency_index, u16 ecmp_size);
113 	void (*fib_entry_act_local_pack)(struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
114 					 enum mlxsw_reg_ralue_trap_action trap_action,
115 					 u16 trap_id, u16 local_erif);
116 	void (*fib_entry_act_ip2me_pack)(struct mlxsw_sp_fib_entry_op_ctx *op_ctx);
117 	void (*fib_entry_act_ip2me_tun_pack)(struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
118 					     u32 tunnel_ptr);
119 	int (*fib_entry_commit)(struct mlxsw_sp *mlxsw_sp,
120 				struct mlxsw_sp_fib_entry_op_ctx *op_ctx,
121 				bool *postponed_for_bulk);
122 	bool (*fib_entry_is_committed)(struct mlxsw_sp_fib_entry_priv *priv);
123 };
124 
125 struct mlxsw_sp_rif_ipip_lb;
126 struct mlxsw_sp_rif_ipip_lb_config {
127 	enum mlxsw_reg_ritr_loopback_ipip_type lb_ipipt;
128 	u32 okey;
129 	enum mlxsw_sp_l3proto ul_protocol; /* Underlay. */
130 	union mlxsw_sp_l3addr saddr;
131 };
132 
133 enum mlxsw_sp_rif_counter_dir {
134 	MLXSW_SP_RIF_COUNTER_INGRESS,
135 	MLXSW_SP_RIF_COUNTER_EGRESS,
136 };
137 
138 struct mlxsw_sp_neigh_entry;
139 struct mlxsw_sp_nexthop;
140 struct mlxsw_sp_ipip_entry;
141 
142 struct mlxsw_sp_rif *mlxsw_sp_rif_by_index(const struct mlxsw_sp *mlxsw_sp,
143 					   u16 rif_index);
144 u16 mlxsw_sp_rif_index(const struct mlxsw_sp_rif *rif);
145 u16 mlxsw_sp_ipip_lb_rif_index(const struct mlxsw_sp_rif_ipip_lb *rif);
146 u16 mlxsw_sp_ipip_lb_ul_vr_id(const struct mlxsw_sp_rif_ipip_lb *rif);
147 u16 mlxsw_sp_ipip_lb_ul_rif_id(const struct mlxsw_sp_rif_ipip_lb *lb_rif);
148 u32 mlxsw_sp_ipip_dev_ul_tb_id(const struct net_device *ol_dev);
149 int mlxsw_sp_rif_dev_ifindex(const struct mlxsw_sp_rif *rif);
150 const struct net_device *mlxsw_sp_rif_dev(const struct mlxsw_sp_rif *rif);
151 int mlxsw_sp_rif_counter_value_get(struct mlxsw_sp *mlxsw_sp,
152 				   struct mlxsw_sp_rif *rif,
153 				   enum mlxsw_sp_rif_counter_dir dir,
154 				   u64 *cnt);
155 void mlxsw_sp_rif_counter_free(struct mlxsw_sp *mlxsw_sp,
156 			       struct mlxsw_sp_rif *rif,
157 			       enum mlxsw_sp_rif_counter_dir dir);
158 int mlxsw_sp_rif_counter_alloc(struct mlxsw_sp *mlxsw_sp,
159 			       struct mlxsw_sp_rif *rif,
160 			       enum mlxsw_sp_rif_counter_dir dir);
161 struct mlxsw_sp_neigh_entry *
162 mlxsw_sp_rif_neigh_next(struct mlxsw_sp_rif *rif,
163 			struct mlxsw_sp_neigh_entry *neigh_entry);
164 int mlxsw_sp_neigh_entry_type(struct mlxsw_sp_neigh_entry *neigh_entry);
165 unsigned char *
166 mlxsw_sp_neigh_entry_ha(struct mlxsw_sp_neigh_entry *neigh_entry);
167 u32 mlxsw_sp_neigh4_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry);
168 struct in6_addr *
169 mlxsw_sp_neigh6_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry);
170 
171 #define mlxsw_sp_rif_neigh_for_each(neigh_entry, rif)				\
172 	for (neigh_entry = mlxsw_sp_rif_neigh_next(rif, NULL); neigh_entry;	\
173 	     neigh_entry = mlxsw_sp_rif_neigh_next(rif, neigh_entry))
174 int mlxsw_sp_neigh_counter_get(struct mlxsw_sp *mlxsw_sp,
175 			       struct mlxsw_sp_neigh_entry *neigh_entry,
176 			       u64 *p_counter);
177 void
178 mlxsw_sp_neigh_entry_counter_update(struct mlxsw_sp *mlxsw_sp,
179 				    struct mlxsw_sp_neigh_entry *neigh_entry,
180 				    bool adding);
181 bool mlxsw_sp_neigh_ipv6_ignore(struct mlxsw_sp_neigh_entry *neigh_entry);
182 int __mlxsw_sp_ipip_entry_update_tunnel(struct mlxsw_sp *mlxsw_sp,
183 					struct mlxsw_sp_ipip_entry *ipip_entry,
184 					bool recreate_loopback,
185 					bool keep_encap,
186 					bool update_nexthops,
187 					struct netlink_ext_ack *extack);
188 void mlxsw_sp_ipip_entry_demote_tunnel(struct mlxsw_sp *mlxsw_sp,
189 				       struct mlxsw_sp_ipip_entry *ipip_entry);
190 bool
191 mlxsw_sp_ipip_demote_tunnel_by_saddr(struct mlxsw_sp *mlxsw_sp,
192 				     enum mlxsw_sp_l3proto ul_proto,
193 				     union mlxsw_sp_l3addr saddr,
194 				     u32 ul_tb_id,
195 				     const struct mlxsw_sp_ipip_entry *except);
196 struct mlxsw_sp_nexthop *mlxsw_sp_nexthop_next(struct mlxsw_sp_router *router,
197 					       struct mlxsw_sp_nexthop *nh);
198 bool mlxsw_sp_nexthop_offload(struct mlxsw_sp_nexthop *nh);
199 unsigned char *mlxsw_sp_nexthop_ha(struct mlxsw_sp_nexthop *nh);
200 int mlxsw_sp_nexthop_indexes(struct mlxsw_sp_nexthop *nh, u32 *p_adj_index,
201 			     u32 *p_adj_size, u32 *p_adj_hash_index);
202 struct mlxsw_sp_rif *mlxsw_sp_nexthop_rif(struct mlxsw_sp_nexthop *nh);
203 bool mlxsw_sp_nexthop_group_has_ipip(struct mlxsw_sp_nexthop *nh);
204 bool mlxsw_sp_nexthop_is_discard(const struct mlxsw_sp_nexthop *nh);
205 #define mlxsw_sp_nexthop_for_each(nh, router)				\
206 	for (nh = mlxsw_sp_nexthop_next(router, NULL); nh;		\
207 	     nh = mlxsw_sp_nexthop_next(router, nh))
208 int mlxsw_sp_nexthop_counter_get(struct mlxsw_sp *mlxsw_sp,
209 				 struct mlxsw_sp_nexthop *nh, u64 *p_counter);
210 int mlxsw_sp_nexthop_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
211 			    struct mlxsw_sp_nexthop *nh);
212 void mlxsw_sp_nexthop_counter_alloc(struct mlxsw_sp *mlxsw_sp,
213 				    struct mlxsw_sp_nexthop *nh);
214 void mlxsw_sp_nexthop_counter_free(struct mlxsw_sp *mlxsw_sp,
215 				   struct mlxsw_sp_nexthop *nh);
216 
217 static inline bool mlxsw_sp_l3addr_eq(const union mlxsw_sp_l3addr *addr1,
218 				      const union mlxsw_sp_l3addr *addr2)
219 {
220 	return !memcmp(addr1, addr2, sizeof(*addr1));
221 }
222 
223 int mlxsw_sp_ipip_ecn_encap_init(struct mlxsw_sp *mlxsw_sp);
224 int mlxsw_sp_ipip_ecn_decap_init(struct mlxsw_sp *mlxsw_sp);
225 
226 extern const struct mlxsw_sp_router_ll_ops mlxsw_sp_router_ll_xm_ops;
227 
228 int mlxsw_sp_router_xm_init(struct mlxsw_sp *mlxsw_sp);
229 void mlxsw_sp_router_xm_fini(struct mlxsw_sp *mlxsw_sp);
230 bool mlxsw_sp_router_xm_ipv4_is_supported(const struct mlxsw_sp *mlxsw_sp);
231 
232 #endif /* _MLXSW_ROUTER_H_*/
233