xref: /linux/drivers/net/ethernet/mellanox/mlx5/core/en_rep.h (revision e5a52fd2b8cdb700b3c07b030e050a49ef3156b9)
1 /*
2  * Copyright (c) 2017, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #ifndef __MLX5E_REP_H__
34 #define __MLX5E_REP_H__
35 
36 #include <net/ip_tunnels.h>
37 #include <linux/rhashtable.h>
38 #include <linux/mutex.h>
39 #include "eswitch.h"
40 #include "en.h"
41 #include "lib/port_tun.h"
42 
43 #ifdef CONFIG_MLX5_ESWITCH
44 struct mlx5e_neigh_update_table {
45 	struct rhashtable       neigh_ht;
46 	/* Save the neigh hash entries in a list in addition to the hash table
47 	 * (neigh_ht). In order to iterate easily over the neigh entries.
48 	 * Used for stats query.
49 	 */
50 	struct list_head	neigh_list;
51 	/* protect lookup/remove operations */
52 	struct mutex		encap_lock;
53 	struct notifier_block   netevent_nb;
54 	struct delayed_work     neigh_stats_work;
55 	unsigned long           min_interval; /* jiffies */
56 };
57 
58 struct mlx5_tc_ct_priv;
59 struct mlx5e_rep_bond;
60 struct mlx5_rep_uplink_priv {
61 	/* Filters DB - instantiated by the uplink representor and shared by
62 	 * the uplink's VFs
63 	 */
64 	struct rhashtable  tc_ht;
65 
66 	/* indirect block callbacks are invoked on bind/unbind events
67 	 * on registered higher level devices (e.g. tunnel devices)
68 	 *
69 	 * tc_indr_block_cb_priv_list is used to lookup indirect callback
70 	 * private data
71 	 *
72 	 */
73 	struct list_head	    tc_indr_block_priv_list;
74 
75 	struct mlx5_tun_entropy tun_entropy;
76 
77 	/* protects unready_flows */
78 	struct mutex                unready_flows_lock;
79 	struct list_head            unready_flows;
80 	struct work_struct          reoffload_flows_work;
81 
82 	/* maps tun_info to a unique id*/
83 	struct mapping_ctx *tunnel_mapping;
84 	/* maps tun_enc_opts to a unique id*/
85 	struct mapping_ctx *tunnel_enc_opts_mapping;
86 
87 	struct mlx5_tc_ct_priv *ct_priv;
88 
89 	/* support eswitch vports bonding */
90 	struct mlx5e_rep_bond *bond;
91 };
92 
93 struct mlx5e_rep_priv {
94 	struct mlx5_eswitch_rep *rep;
95 	struct mlx5e_neigh_update_table neigh_update;
96 	struct net_device      *netdev;
97 	struct mlx5_flow_table *root_ft;
98 	struct mlx5_flow_handle *vport_rx_rule;
99 	struct list_head       vport_sqs_list;
100 	struct mlx5_rep_uplink_priv uplink_priv; /* valid for uplink rep */
101 	struct rtnl_link_stats64 prev_vf_vport_stats;
102 	struct devlink_port dl_port;
103 };
104 
105 static inline
106 struct mlx5e_rep_priv *mlx5e_rep_to_rep_priv(struct mlx5_eswitch_rep *rep)
107 {
108 	return rep->rep_data[REP_ETH].priv;
109 }
110 
111 struct mlx5e_neigh {
112 	struct net_device *dev;
113 	union {
114 		__be32	v4;
115 		struct in6_addr v6;
116 	} dst_ip;
117 	int family;
118 };
119 
120 struct mlx5e_neigh_hash_entry {
121 	struct rhash_head rhash_node;
122 	struct mlx5e_neigh m_neigh;
123 	struct mlx5e_priv *priv;
124 
125 	/* Save the neigh hash entry in a list on the representor in
126 	 * addition to the hash table. In order to iterate easily over the
127 	 * neighbour entries. Used for stats query.
128 	 */
129 	struct list_head neigh_list;
130 
131 	/* protects encap list */
132 	spinlock_t encap_list_lock;
133 	/* encap list sharing the same neigh */
134 	struct list_head encap_list;
135 
136 	/* valid only when the neigh reference is taken during
137 	 * neigh_update_work workqueue callback.
138 	 */
139 	struct neighbour *n;
140 	struct work_struct neigh_update_work;
141 
142 	/* neigh hash entry can be deleted only when the refcount is zero.
143 	 * refcount is needed to avoid neigh hash entry removal by TC, while
144 	 * it's used by the neigh notification call.
145 	 */
146 	refcount_t refcnt;
147 
148 	/* Save the last reported time offloaded trafic pass over one of the
149 	 * neigh hash entry flows. Use it to periodically update the neigh
150 	 * 'used' value and avoid neigh deleting by the kernel.
151 	 */
152 	unsigned long reported_lastuse;
153 
154 	struct rcu_head rcu;
155 };
156 
157 enum {
158 	/* set when the encap entry is successfully offloaded into HW */
159 	MLX5_ENCAP_ENTRY_VALID     = BIT(0),
160 	MLX5_REFORMAT_DECAP        = BIT(1),
161 };
162 
163 struct mlx5e_decap_key {
164 	struct ethhdr key;
165 };
166 
167 struct mlx5e_decap_entry {
168 	struct mlx5e_decap_key key;
169 	struct list_head flows;
170 	struct hlist_node hlist;
171 	refcount_t refcnt;
172 	struct completion res_ready;
173 	int compl_result;
174 	struct mlx5_pkt_reformat *pkt_reformat;
175 	struct rcu_head rcu;
176 };
177 
178 struct mlx5e_encap_entry {
179 	/* attached neigh hash entry */
180 	struct mlx5e_neigh_hash_entry *nhe;
181 	/* neigh hash entry list of encaps sharing the same neigh */
182 	struct list_head encap_list;
183 	struct mlx5e_neigh m_neigh;
184 	/* a node of the eswitch encap hash table which keeping all the encap
185 	 * entries
186 	 */
187 	struct hlist_node encap_hlist;
188 	struct list_head flows;
189 	struct mlx5_pkt_reformat *pkt_reformat;
190 	const struct ip_tunnel_info *tun_info;
191 	unsigned char h_dest[ETH_ALEN];	/* destination eth addr	*/
192 
193 	struct net_device *out_dev;
194 	struct net_device *route_dev;
195 	struct mlx5e_tc_tunnel *tunnel;
196 	int reformat_type;
197 	u8 flags;
198 	char *encap_header;
199 	int encap_size;
200 	refcount_t refcnt;
201 	struct completion res_ready;
202 	int compl_result;
203 	struct rcu_head rcu;
204 };
205 
206 struct mlx5e_rep_sq {
207 	struct mlx5_flow_handle	*send_to_vport_rule;
208 	struct list_head	 list;
209 };
210 
211 void mlx5e_rep_register_vport_reps(struct mlx5_core_dev *mdev);
212 void mlx5e_rep_unregister_vport_reps(struct mlx5_core_dev *mdev);
213 int mlx5e_rep_bond_init(struct mlx5e_rep_priv *rpriv);
214 void mlx5e_rep_bond_cleanup(struct mlx5e_rep_priv *rpriv);
215 int mlx5e_rep_bond_enslave(struct mlx5_eswitch *esw, struct net_device *netdev,
216 			   struct net_device *lag_dev);
217 void mlx5e_rep_bond_unslave(struct mlx5_eswitch *esw,
218 			    const struct net_device *netdev,
219 			    const struct net_device *lag_dev);
220 int mlx5e_rep_bond_update(struct mlx5e_priv *priv, bool cleanup);
221 
222 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv);
223 int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv);
224 void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv);
225 
226 void mlx5e_handle_rx_cqe_rep(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe);
227 void mlx5e_handle_rx_cqe_mpwrq_rep(struct mlx5e_rq *rq,
228 				   struct mlx5_cqe64 *cqe);
229 
230 void mlx5e_rep_queue_neigh_stats_work(struct mlx5e_priv *priv);
231 
232 bool mlx5e_eswitch_vf_rep(struct net_device *netdev);
233 bool mlx5e_eswitch_uplink_rep(struct net_device *netdev);
234 static inline bool mlx5e_eswitch_rep(struct net_device *netdev)
235 {
236 	return mlx5e_eswitch_vf_rep(netdev) ||
237 	       mlx5e_eswitch_uplink_rep(netdev);
238 }
239 
240 #else /* CONFIG_MLX5_ESWITCH */
241 static inline bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv) { return false; }
242 static inline int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv) { return 0; }
243 static inline void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv) {}
244 #endif
245 
246 static inline bool mlx5e_is_vport_rep(struct mlx5e_priv *priv)
247 {
248 	return (MLX5_ESWITCH_MANAGER(priv->mdev) && priv->ppriv);
249 }
250 #endif /* __MLX5E_REP_H__ */
251