1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /* Copyright (c) 2018 Mellanox Technologies. */
3 
4 #ifndef __MLX5E_FLOW_STEER_H__
5 #define __MLX5E_FLOW_STEER_H__
6 
7 #include "mod_hdr.h"
8 
9 enum {
10 	MLX5E_TC_FT_LEVEL = 0,
11 	MLX5E_TC_TTC_FT_LEVEL,
12 };
13 
14 struct mlx5e_tc_table {
15 	/* Protects the dynamic assignment of the t parameter
16 	 * which is the nic tc root table.
17 	 */
18 	struct mutex			t_lock;
19 	struct mlx5_flow_table		*t;
20 	struct mlx5_fs_chains           *chains;
21 
22 	struct rhashtable               ht;
23 
24 	struct mod_hdr_tbl mod_hdr;
25 	struct mutex hairpin_tbl_lock; /* protects hairpin_tbl */
26 	DECLARE_HASHTABLE(hairpin_tbl, 8);
27 
28 	struct notifier_block     netdevice_nb;
29 	struct netdev_net_notifier	netdevice_nn;
30 
31 	struct mlx5_tc_ct_priv         *ct;
32 };
33 
34 struct mlx5e_flow_table {
35 	int num_groups;
36 	struct mlx5_flow_table *t;
37 	struct mlx5_flow_group **g;
38 };
39 
40 struct mlx5e_l2_rule {
41 	u8  addr[ETH_ALEN + 2];
42 	struct mlx5_flow_handle *rule;
43 };
44 
45 #define MLX5E_L2_ADDR_HASH_SIZE BIT(BITS_PER_BYTE)
46 
47 struct mlx5e_vlan_table {
48 	struct mlx5e_flow_table		ft;
49 	DECLARE_BITMAP(active_cvlans, VLAN_N_VID);
50 	DECLARE_BITMAP(active_svlans, VLAN_N_VID);
51 	struct mlx5_flow_handle	*active_cvlans_rule[VLAN_N_VID];
52 	struct mlx5_flow_handle	*active_svlans_rule[VLAN_N_VID];
53 	struct mlx5_flow_handle	*untagged_rule;
54 	struct mlx5_flow_handle	*any_cvlan_rule;
55 	struct mlx5_flow_handle	*any_svlan_rule;
56 	bool			cvlan_filter_disabled;
57 };
58 
59 struct mlx5e_l2_table {
60 	struct mlx5e_flow_table    ft;
61 	struct hlist_head          netdev_uc[MLX5E_L2_ADDR_HASH_SIZE];
62 	struct hlist_head          netdev_mc[MLX5E_L2_ADDR_HASH_SIZE];
63 	struct mlx5e_l2_rule	   broadcast;
64 	struct mlx5e_l2_rule	   allmulti;
65 	struct mlx5e_l2_rule	   promisc;
66 	bool                       broadcast_enabled;
67 	bool                       allmulti_enabled;
68 	bool                       promisc_enabled;
69 };
70 
71 enum mlx5e_traffic_types {
72 	MLX5E_TT_IPV4_TCP,
73 	MLX5E_TT_IPV6_TCP,
74 	MLX5E_TT_IPV4_UDP,
75 	MLX5E_TT_IPV6_UDP,
76 	MLX5E_TT_IPV4_IPSEC_AH,
77 	MLX5E_TT_IPV6_IPSEC_AH,
78 	MLX5E_TT_IPV4_IPSEC_ESP,
79 	MLX5E_TT_IPV6_IPSEC_ESP,
80 	MLX5E_TT_IPV4,
81 	MLX5E_TT_IPV6,
82 	MLX5E_TT_ANY,
83 	MLX5E_NUM_TT,
84 	MLX5E_NUM_INDIR_TIRS = MLX5E_TT_ANY,
85 };
86 
87 struct mlx5e_tirc_config {
88 	u8 l3_prot_type;
89 	u8 l4_prot_type;
90 	u32 rx_hash_fields;
91 };
92 
93 #define MLX5_HASH_IP		(MLX5_HASH_FIELD_SEL_SRC_IP   |\
94 				 MLX5_HASH_FIELD_SEL_DST_IP)
95 #define MLX5_HASH_IP_L4PORTS	(MLX5_HASH_FIELD_SEL_SRC_IP   |\
96 				 MLX5_HASH_FIELD_SEL_DST_IP   |\
97 				 MLX5_HASH_FIELD_SEL_L4_SPORT |\
98 				 MLX5_HASH_FIELD_SEL_L4_DPORT)
99 #define MLX5_HASH_IP_IPSEC_SPI	(MLX5_HASH_FIELD_SEL_SRC_IP   |\
100 				 MLX5_HASH_FIELD_SEL_DST_IP   |\
101 				 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
102 
103 enum mlx5e_tunnel_types {
104 	MLX5E_TT_IPV4_GRE,
105 	MLX5E_TT_IPV6_GRE,
106 	MLX5E_TT_IPV4_IPIP,
107 	MLX5E_TT_IPV6_IPIP,
108 	MLX5E_TT_IPV4_IPV6,
109 	MLX5E_TT_IPV6_IPV6,
110 	MLX5E_NUM_TUNNEL_TT,
111 };
112 
113 bool mlx5e_tunnel_inner_ft_supported(struct mlx5_core_dev *mdev);
114 
115 struct mlx5e_ttc_rule {
116 	struct mlx5_flow_handle *rule;
117 	struct mlx5_flow_destination default_dest;
118 };
119 
120 /* L3/L4 traffic type classifier */
121 struct mlx5e_ttc_table {
122 	struct mlx5e_flow_table ft;
123 	struct mlx5e_ttc_rule rules[MLX5E_NUM_TT];
124 	struct mlx5_flow_handle *tunnel_rules[MLX5E_NUM_TUNNEL_TT];
125 };
126 
127 /* NIC prio FTS */
128 enum {
129 	MLX5E_VLAN_FT_LEVEL = 0,
130 	MLX5E_L2_FT_LEVEL,
131 	MLX5E_TTC_FT_LEVEL,
132 	MLX5E_INNER_TTC_FT_LEVEL,
133 #ifdef CONFIG_MLX5_EN_TLS
134 	MLX5E_ACCEL_FS_TCP_FT_LEVEL,
135 #endif
136 #ifdef CONFIG_MLX5_EN_ARFS
137 	MLX5E_ARFS_FT_LEVEL,
138 #endif
139 #ifdef CONFIG_MLX5_EN_IPSEC
140 	MLX5E_ACCEL_FS_ESP_FT_LEVEL = MLX5E_INNER_TTC_FT_LEVEL + 1,
141 	MLX5E_ACCEL_FS_ESP_FT_ERR_LEVEL,
142 #endif
143 };
144 
145 #define MLX5E_TTC_NUM_GROUPS	3
146 #define MLX5E_TTC_GROUP1_SIZE	(BIT(3) + MLX5E_NUM_TUNNEL_TT)
147 #define MLX5E_TTC_GROUP2_SIZE	 BIT(1)
148 #define MLX5E_TTC_GROUP3_SIZE	 BIT(0)
149 #define MLX5E_TTC_TABLE_SIZE	(MLX5E_TTC_GROUP1_SIZE +\
150 				 MLX5E_TTC_GROUP2_SIZE +\
151 				 MLX5E_TTC_GROUP3_SIZE)
152 
153 #define MLX5E_INNER_TTC_NUM_GROUPS	3
154 #define MLX5E_INNER_TTC_GROUP1_SIZE	BIT(3)
155 #define MLX5E_INNER_TTC_GROUP2_SIZE	BIT(1)
156 #define MLX5E_INNER_TTC_GROUP3_SIZE	BIT(0)
157 #define MLX5E_INNER_TTC_TABLE_SIZE	(MLX5E_INNER_TTC_GROUP1_SIZE +\
158 					 MLX5E_INNER_TTC_GROUP2_SIZE +\
159 					 MLX5E_INNER_TTC_GROUP3_SIZE)
160 
161 #ifdef CONFIG_MLX5_EN_RXNFC
162 
163 struct mlx5e_ethtool_table {
164 	struct mlx5_flow_table *ft;
165 	int                    num_rules;
166 };
167 
168 #define ETHTOOL_NUM_L3_L4_FTS 7
169 #define ETHTOOL_NUM_L2_FTS 4
170 
171 struct mlx5e_ethtool_steering {
172 	struct mlx5e_ethtool_table      l3_l4_ft[ETHTOOL_NUM_L3_L4_FTS];
173 	struct mlx5e_ethtool_table      l2_ft[ETHTOOL_NUM_L2_FTS];
174 	struct list_head                rules;
175 	int                             tot_num_rules;
176 };
177 
178 void mlx5e_ethtool_init_steering(struct mlx5e_priv *priv);
179 void mlx5e_ethtool_cleanup_steering(struct mlx5e_priv *priv);
180 int mlx5e_ethtool_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd);
181 int mlx5e_ethtool_get_rxnfc(struct net_device *dev,
182 			    struct ethtool_rxnfc *info, u32 *rule_locs);
183 #else
184 static inline void mlx5e_ethtool_init_steering(struct mlx5e_priv *priv)    { }
185 static inline void mlx5e_ethtool_cleanup_steering(struct mlx5e_priv *priv) { }
186 static inline int mlx5e_ethtool_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
187 { return -EOPNOTSUPP; }
188 static inline int mlx5e_ethtool_get_rxnfc(struct net_device *dev,
189 					  struct ethtool_rxnfc *info, u32 *rule_locs)
190 { return -EOPNOTSUPP; }
191 #endif /* CONFIG_MLX5_EN_RXNFC */
192 
193 #ifdef CONFIG_MLX5_EN_ARFS
194 #define ARFS_HASH_SHIFT BITS_PER_BYTE
195 #define ARFS_HASH_SIZE BIT(BITS_PER_BYTE)
196 
197 struct arfs_table {
198 	struct mlx5e_flow_table  ft;
199 	struct mlx5_flow_handle	 *default_rule;
200 	struct hlist_head	 rules_hash[ARFS_HASH_SIZE];
201 };
202 
203 enum  arfs_type {
204 	ARFS_IPV4_TCP,
205 	ARFS_IPV6_TCP,
206 	ARFS_IPV4_UDP,
207 	ARFS_IPV6_UDP,
208 	ARFS_NUM_TYPES,
209 };
210 
211 struct mlx5e_arfs_tables {
212 	struct arfs_table arfs_tables[ARFS_NUM_TYPES];
213 	/* Protect aRFS rules list */
214 	spinlock_t                     arfs_lock;
215 	struct list_head               rules;
216 	int                            last_filter_id;
217 	struct workqueue_struct        *wq;
218 };
219 
220 int mlx5e_arfs_create_tables(struct mlx5e_priv *priv);
221 void mlx5e_arfs_destroy_tables(struct mlx5e_priv *priv);
222 int mlx5e_arfs_enable(struct mlx5e_priv *priv);
223 int mlx5e_arfs_disable(struct mlx5e_priv *priv);
224 int mlx5e_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
225 			u16 rxq_index, u32 flow_id);
226 #else
227 static inline int mlx5e_arfs_create_tables(struct mlx5e_priv *priv) { return 0; }
228 static inline void mlx5e_arfs_destroy_tables(struct mlx5e_priv *priv) {}
229 static inline int mlx5e_arfs_enable(struct mlx5e_priv *priv) { return -EOPNOTSUPP; }
230 static inline int mlx5e_arfs_disable(struct mlx5e_priv *priv) {	return -EOPNOTSUPP; }
231 #endif
232 
233 #ifdef CONFIG_MLX5_EN_TLS
234 struct mlx5e_accel_fs_tcp;
235 #endif
236 
237 struct mlx5e_flow_steering {
238 	struct mlx5_flow_namespace      *ns;
239 	struct mlx5_flow_namespace      *egress_ns;
240 #ifdef CONFIG_MLX5_EN_RXNFC
241 	struct mlx5e_ethtool_steering   ethtool;
242 #endif
243 	struct mlx5e_tc_table           tc;
244 	struct mlx5e_vlan_table         vlan;
245 	struct mlx5e_l2_table           l2;
246 	struct mlx5e_ttc_table          ttc;
247 	struct mlx5e_ttc_table          inner_ttc;
248 #ifdef CONFIG_MLX5_EN_ARFS
249 	struct mlx5e_arfs_tables        arfs;
250 #endif
251 #ifdef CONFIG_MLX5_EN_TLS
252 	struct mlx5e_accel_fs_tcp      *accel_tcp;
253 #endif
254 };
255 
256 struct ttc_params {
257 	struct mlx5_flow_table_attr ft_attr;
258 	u32 any_tt_tirn;
259 	u32 indir_tirn[MLX5E_NUM_INDIR_TIRS];
260 	struct mlx5e_ttc_table *inner_ttc;
261 };
262 
263 void mlx5e_set_ttc_basic_params(struct mlx5e_priv *priv, struct ttc_params *ttc_params);
264 void mlx5e_set_ttc_ft_params(struct ttc_params *ttc_params);
265 void mlx5e_set_inner_ttc_ft_params(struct ttc_params *ttc_params);
266 
267 int mlx5e_create_ttc_table(struct mlx5e_priv *priv, struct ttc_params *params,
268 			   struct mlx5e_ttc_table *ttc);
269 void mlx5e_destroy_ttc_table(struct mlx5e_priv *priv,
270 			     struct mlx5e_ttc_table *ttc);
271 
272 int mlx5e_create_inner_ttc_table(struct mlx5e_priv *priv, struct ttc_params *params,
273 				 struct mlx5e_ttc_table *ttc);
274 void mlx5e_destroy_inner_ttc_table(struct mlx5e_priv *priv,
275 				   struct mlx5e_ttc_table *ttc);
276 
277 void mlx5e_destroy_flow_table(struct mlx5e_flow_table *ft);
278 int mlx5e_ttc_fwd_dest(struct mlx5e_priv *priv, enum mlx5e_traffic_types type,
279 		       struct mlx5_flow_destination *new_dest);
280 struct mlx5_flow_destination
281 mlx5e_ttc_get_default_dest(struct mlx5e_priv *priv, enum mlx5e_traffic_types type);
282 int mlx5e_ttc_fwd_default_dest(struct mlx5e_priv *priv, enum mlx5e_traffic_types type);
283 
284 void mlx5e_enable_cvlan_filter(struct mlx5e_priv *priv);
285 void mlx5e_disable_cvlan_filter(struct mlx5e_priv *priv);
286 
287 int mlx5e_create_flow_steering(struct mlx5e_priv *priv);
288 void mlx5e_destroy_flow_steering(struct mlx5e_priv *priv);
289 
290 u8 mlx5e_get_proto_by_tunnel_type(enum mlx5e_tunnel_types tt);
291 
292 #endif /* __MLX5E_FLOW_STEER_H__ */
293 
294