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 extern const struct mlx5e_rx_handlers mlx5e_rx_handlers_rep;
45 
46 struct mlx5e_neigh_update_table {
47 	struct rhashtable       neigh_ht;
48 	/* Save the neigh hash entries in a list in addition to the hash table
49 	 * (neigh_ht). In order to iterate easily over the neigh entries.
50 	 * Used for stats query.
51 	 */
52 	struct list_head	neigh_list;
53 	/* protect lookup/remove operations */
54 	struct mutex		encap_lock;
55 	struct notifier_block   netevent_nb;
56 	struct delayed_work     neigh_stats_work;
57 	unsigned long           min_interval; /* jiffies */
58 };
59 
60 struct mlx5_tc_ct_priv;
61 struct mlx5_tc_int_port_priv;
62 struct mlx5e_rep_bond;
63 struct mlx5e_tc_tun_encap;
64 struct mlx5e_post_act;
65 struct mlx5e_flow_meters;
66 
67 struct mlx5_rep_uplink_priv {
68 	/* indirect block callbacks are invoked on bind/unbind events
69 	 * on registered higher level devices (e.g. tunnel devices)
70 	 *
71 	 * tc_indr_block_cb_priv_list is used to lookup indirect callback
72 	 * private data
73 	 *
74 	 */
75 	struct list_head	    tc_indr_block_priv_list;
76 
77 	struct mlx5_tun_entropy tun_entropy;
78 
79 	/* protects unready_flows */
80 	struct mutex                unready_flows_lock;
81 	struct list_head            unready_flows;
82 	struct work_struct          reoffload_flows_work;
83 
84 	/* maps tun_info to a unique id*/
85 	struct mapping_ctx *tunnel_mapping;
86 	/* maps tun_enc_opts to a unique id*/
87 	struct mapping_ctx *tunnel_enc_opts_mapping;
88 
89 	struct mlx5e_post_act *post_act;
90 	struct mlx5_tc_ct_priv *ct_priv;
91 	struct mlx5e_tc_psample *tc_psample;
92 
93 	/* support eswitch vports bonding */
94 	struct mlx5e_rep_bond *bond;
95 
96 	/* tc tunneling encapsulation private data */
97 	struct mlx5e_tc_tun_encap *encap;
98 
99 	/* OVS internal port support */
100 	struct mlx5e_tc_int_port_priv *int_port_priv;
101 
102 	struct mlx5e_flow_meters *flow_meters;
103 
104 	/* tc action stats */
105 	struct mlx5e_tc_act_stats_handle *action_stats_handle;
106 
107 	struct work_struct mpesw_work;
108 };
109 
110 struct mlx5e_rep_priv {
111 	struct mlx5_eswitch_rep *rep;
112 	struct mlx5e_neigh_update_table neigh_update;
113 	struct net_device      *netdev;
114 	struct mlx5_flow_table *root_ft;
115 	struct mlx5_flow_handle *vport_rx_rule;
116 	struct list_head       vport_sqs_list;
117 	struct mlx5_rep_uplink_priv uplink_priv; /* valid for uplink rep */
118 	struct rtnl_link_stats64 prev_vf_vport_stats;
119 	struct mlx5_flow_handle *send_to_vport_meta_rule;
120 	struct rhashtable tc_ht;
121 };
122 
123 static inline
mlx5e_rep_to_rep_priv(struct mlx5_eswitch_rep * rep)124 struct mlx5e_rep_priv *mlx5e_rep_to_rep_priv(struct mlx5_eswitch_rep *rep)
125 {
126 	return rep->rep_data[REP_ETH].priv;
127 }
128 
129 struct mlx5e_neigh {
130 	union {
131 		__be32	v4;
132 		struct in6_addr v6;
133 	} dst_ip;
134 	int family;
135 };
136 
137 struct mlx5e_neigh_hash_entry {
138 	struct rhash_head rhash_node;
139 	struct mlx5e_neigh m_neigh;
140 	struct mlx5e_priv *priv;
141 	struct net_device *neigh_dev;
142 
143 	/* Save the neigh hash entry in a list on the representor in
144 	 * addition to the hash table. In order to iterate easily over the
145 	 * neighbour entries. Used for stats query.
146 	 */
147 	struct list_head neigh_list;
148 
149 	/* protects encap list */
150 	spinlock_t encap_list_lock;
151 	/* encap list sharing the same neigh */
152 	struct list_head encap_list;
153 
154 	/* neigh hash entry can be deleted only when the refcount is zero.
155 	 * refcount is needed to avoid neigh hash entry removal by TC, while
156 	 * it's used by the neigh notification call.
157 	 */
158 	refcount_t refcnt;
159 
160 	/* Save the last reported time offloaded traffic pass over one of the
161 	 * neigh hash entry flows. Use it to periodically update the neigh
162 	 * 'used' value and avoid neigh deleting by the kernel.
163 	 */
164 	unsigned long reported_lastuse;
165 
166 	struct rcu_head rcu;
167 };
168 
169 enum {
170 	/* set when the encap entry is successfully offloaded into HW */
171 	MLX5_ENCAP_ENTRY_VALID     = BIT(0),
172 	MLX5_REFORMAT_DECAP        = BIT(1),
173 	MLX5_ENCAP_ENTRY_NO_ROUTE  = BIT(2),
174 };
175 
176 struct mlx5e_decap_key {
177 	struct ethhdr key;
178 };
179 
180 struct mlx5e_decap_entry {
181 	struct mlx5e_decap_key key;
182 	struct list_head flows;
183 	struct hlist_node hlist;
184 	refcount_t refcnt;
185 	struct completion res_ready;
186 	int compl_result;
187 	struct mlx5_pkt_reformat *pkt_reformat;
188 	struct rcu_head rcu;
189 };
190 
191 struct mlx5e_mpls_info {
192 	u32             label;
193 	u8              tc;
194 	u8              bos;
195 	u8              ttl;
196 };
197 
198 struct mlx5e_encap_entry {
199 	/* attached neigh hash entry */
200 	struct mlx5e_neigh_hash_entry *nhe;
201 	/* neigh hash entry list of encaps sharing the same neigh */
202 	struct list_head encap_list;
203 	/* a node of the eswitch encap hash table which keeping all the encap
204 	 * entries
205 	 */
206 	struct hlist_node encap_hlist;
207 	struct list_head flows;
208 	struct list_head route_list;
209 	struct mlx5_pkt_reformat *pkt_reformat;
210 	const struct ip_tunnel_info *tun_info;
211 	struct mlx5e_mpls_info mpls_info;
212 	unsigned char h_dest[ETH_ALEN];	/* destination eth addr	*/
213 
214 	struct net_device *out_dev;
215 	int route_dev_ifindex;
216 	struct mlx5e_tc_tunnel *tunnel;
217 	int reformat_type;
218 	u8 flags;
219 	char *encap_header;
220 	int encap_size;
221 	refcount_t refcnt;
222 	struct completion res_ready;
223 	int compl_result;
224 	struct rcu_head rcu;
225 };
226 
227 struct mlx5e_rep_sq {
228 	struct mlx5_flow_handle	*send_to_vport_rule;
229 	struct mlx5_flow_handle *send_to_vport_rule_peer;
230 	u32 sqn;
231 	struct list_head	 list;
232 };
233 
234 int mlx5e_rep_init(void);
235 void mlx5e_rep_cleanup(void);
236 int mlx5e_rep_bond_init(struct mlx5e_rep_priv *rpriv);
237 void mlx5e_rep_bond_cleanup(struct mlx5e_rep_priv *rpriv);
238 int mlx5e_rep_bond_enslave(struct mlx5_eswitch *esw, struct net_device *netdev,
239 			   struct net_device *lag_dev);
240 void mlx5e_rep_bond_unslave(struct mlx5_eswitch *esw,
241 			    const struct net_device *netdev,
242 			    const struct net_device *lag_dev);
243 int mlx5e_rep_bond_update(struct mlx5e_priv *priv, bool cleanup);
244 
245 bool mlx5e_rep_has_offload_stats(const struct net_device *dev, int attr_id);
246 int mlx5e_rep_get_offload_stats(int attr_id, const struct net_device *dev,
247 				void *sp);
248 
249 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv);
250 void mlx5e_rep_activate_channels(struct mlx5e_priv *priv);
251 void mlx5e_rep_deactivate_channels(struct mlx5e_priv *priv);
252 
253 void mlx5e_rep_queue_neigh_stats_work(struct mlx5e_priv *priv);
254 
255 bool mlx5e_eswitch_vf_rep(const struct net_device *netdev);
256 bool mlx5e_eswitch_uplink_rep(const struct net_device *netdev);
mlx5e_eswitch_rep(const struct net_device * netdev)257 static inline bool mlx5e_eswitch_rep(const struct net_device *netdev)
258 {
259 	return mlx5e_eswitch_vf_rep(netdev) ||
260 	       mlx5e_eswitch_uplink_rep(netdev);
261 }
262 
263 #else /* CONFIG_MLX5_ESWITCH */
mlx5e_is_uplink_rep(struct mlx5e_priv * priv)264 static inline bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv) { return false; }
mlx5e_rep_activate_channels(struct mlx5e_priv * priv)265 static inline void mlx5e_rep_activate_channels(struct mlx5e_priv *priv) {}
mlx5e_rep_deactivate_channels(struct mlx5e_priv * priv)266 static inline void mlx5e_rep_deactivate_channels(struct mlx5e_priv *priv) {}
mlx5e_rep_init(void)267 static inline int mlx5e_rep_init(void) { return 0; };
mlx5e_rep_cleanup(void)268 static inline void mlx5e_rep_cleanup(void) {};
mlx5e_rep_has_offload_stats(const struct net_device * dev,int attr_id)269 static inline bool mlx5e_rep_has_offload_stats(const struct net_device *dev,
270 					       int attr_id) { return false; }
mlx5e_rep_get_offload_stats(int attr_id,const struct net_device * dev,void * sp)271 static inline int mlx5e_rep_get_offload_stats(int attr_id,
272 					      const struct net_device *dev,
273 					      void *sp) { return -EOPNOTSUPP; }
274 #endif
275 
mlx5e_is_vport_rep(struct mlx5e_priv * priv)276 static inline bool mlx5e_is_vport_rep(struct mlx5e_priv *priv)
277 {
278 	return (MLX5_ESWITCH_MANAGER(priv->mdev) && priv->ppriv);
279 }
280 #endif /* __MLX5E_REP_H__ */
281