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