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 	struct mapping_ctx             *mapping;
33 };
34 
35 struct mlx5e_flow_table {
36 	int num_groups;
37 	struct mlx5_flow_table *t;
38 	struct mlx5_flow_group **g;
39 };
40 
41 struct mlx5e_l2_rule {
42 	u8  addr[ETH_ALEN + 2];
43 	struct mlx5_flow_handle *rule;
44 };
45 
46 #define MLX5E_L2_ADDR_HASH_SIZE BIT(BITS_PER_BYTE)
47 
48 struct mlx5e_promisc_table {
49 	struct mlx5e_flow_table	ft;
50 	struct mlx5_flow_handle	*rule;
51 };
52 
53 /* Forward declaration and APIs to get private fields of vlan_table */
54 struct mlx5e_vlan_table;
55 unsigned long *mlx5e_vlan_get_active_svlans(struct mlx5e_vlan_table *vlan);
56 struct mlx5_flow_table *mlx5e_vlan_get_flowtable(struct mlx5e_vlan_table *vlan);
57 
58 struct mlx5e_l2_table {
59 	struct mlx5e_flow_table    ft;
60 	struct hlist_head          netdev_uc[MLX5E_L2_ADDR_HASH_SIZE];
61 	struct hlist_head          netdev_mc[MLX5E_L2_ADDR_HASH_SIZE];
62 	struct mlx5e_l2_rule	   broadcast;
63 	struct mlx5e_l2_rule	   allmulti;
64 	struct mlx5_flow_handle    *trap_rule;
65 	bool                       broadcast_enabled;
66 	bool                       allmulti_enabled;
67 	bool                       promisc_enabled;
68 };
69 
70 enum mlx5e_traffic_types {
71 	MLX5E_TT_IPV4_TCP,
72 	MLX5E_TT_IPV6_TCP,
73 	MLX5E_TT_IPV4_UDP,
74 	MLX5E_TT_IPV6_UDP,
75 	MLX5E_TT_IPV4_IPSEC_AH,
76 	MLX5E_TT_IPV6_IPSEC_AH,
77 	MLX5E_TT_IPV4_IPSEC_ESP,
78 	MLX5E_TT_IPV6_IPSEC_ESP,
79 	MLX5E_TT_IPV4,
80 	MLX5E_TT_IPV6,
81 	MLX5E_TT_ANY,
82 	MLX5E_NUM_TT,
83 	MLX5E_NUM_INDIR_TIRS = MLX5E_TT_ANY,
84 };
85 
86 struct mlx5e_tirc_config {
87 	u8 l3_prot_type;
88 	u8 l4_prot_type;
89 	u32 rx_hash_fields;
90 };
91 
92 #define MLX5_HASH_IP		(MLX5_HASH_FIELD_SEL_SRC_IP   |\
93 				 MLX5_HASH_FIELD_SEL_DST_IP)
94 #define MLX5_HASH_IP_L4PORTS	(MLX5_HASH_FIELD_SEL_SRC_IP   |\
95 				 MLX5_HASH_FIELD_SEL_DST_IP   |\
96 				 MLX5_HASH_FIELD_SEL_L4_SPORT |\
97 				 MLX5_HASH_FIELD_SEL_L4_DPORT)
98 #define MLX5_HASH_IP_IPSEC_SPI	(MLX5_HASH_FIELD_SEL_SRC_IP   |\
99 				 MLX5_HASH_FIELD_SEL_DST_IP   |\
100 				 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
101 
102 enum mlx5e_tunnel_types {
103 	MLX5E_TT_IPV4_GRE,
104 	MLX5E_TT_IPV6_GRE,
105 	MLX5E_TT_IPV4_IPIP,
106 	MLX5E_TT_IPV6_IPIP,
107 	MLX5E_TT_IPV4_IPV6,
108 	MLX5E_TT_IPV6_IPV6,
109 	MLX5E_NUM_TUNNEL_TT,
110 };
111 
112 bool mlx5e_tunnel_inner_ft_supported(struct mlx5_core_dev *mdev);
113 
114 struct mlx5e_ttc_rule {
115 	struct mlx5_flow_handle *rule;
116 	struct mlx5_flow_destination default_dest;
117 };
118 
119 /* L3/L4 traffic type classifier */
120 struct mlx5e_ttc_table {
121 	struct mlx5e_flow_table ft;
122 	struct mlx5e_ttc_rule rules[MLX5E_NUM_TT];
123 	struct mlx5_flow_handle *tunnel_rules[MLX5E_NUM_TUNNEL_TT];
124 };
125 
126 /* NIC prio FTS */
127 enum {
128 	MLX5E_PROMISC_FT_LEVEL,
129 	MLX5E_VLAN_FT_LEVEL,
130 	MLX5E_L2_FT_LEVEL,
131 	MLX5E_TTC_FT_LEVEL,
132 	MLX5E_INNER_TTC_FT_LEVEL,
133 	MLX5E_FS_TT_UDP_FT_LEVEL = MLX5E_INNER_TTC_FT_LEVEL + 1,
134 	MLX5E_FS_TT_ANY_FT_LEVEL = MLX5E_INNER_TTC_FT_LEVEL + 1,
135 #ifdef CONFIG_MLX5_EN_TLS
136 	MLX5E_ACCEL_FS_TCP_FT_LEVEL = MLX5E_INNER_TTC_FT_LEVEL + 1,
137 #endif
138 #ifdef CONFIG_MLX5_EN_ARFS
139 	MLX5E_ARFS_FT_LEVEL = MLX5E_INNER_TTC_FT_LEVEL + 1,
140 #endif
141 #ifdef CONFIG_MLX5_EN_IPSEC
142 	MLX5E_ACCEL_FS_ESP_FT_LEVEL = MLX5E_INNER_TTC_FT_LEVEL + 1,
143 	MLX5E_ACCEL_FS_ESP_FT_ERR_LEVEL,
144 #endif
145 };
146 
147 #define MLX5E_TTC_NUM_GROUPS	3
148 #define MLX5E_TTC_GROUP1_SIZE	(BIT(3) + MLX5E_NUM_TUNNEL_TT)
149 #define MLX5E_TTC_GROUP2_SIZE	 BIT(1)
150 #define MLX5E_TTC_GROUP3_SIZE	 BIT(0)
151 #define MLX5E_TTC_TABLE_SIZE	(MLX5E_TTC_GROUP1_SIZE +\
152 				 MLX5E_TTC_GROUP2_SIZE +\
153 				 MLX5E_TTC_GROUP3_SIZE)
154 
155 #define MLX5E_INNER_TTC_NUM_GROUPS	3
156 #define MLX5E_INNER_TTC_GROUP1_SIZE	BIT(3)
157 #define MLX5E_INNER_TTC_GROUP2_SIZE	BIT(1)
158 #define MLX5E_INNER_TTC_GROUP3_SIZE	BIT(0)
159 #define MLX5E_INNER_TTC_TABLE_SIZE	(MLX5E_INNER_TTC_GROUP1_SIZE +\
160 					 MLX5E_INNER_TTC_GROUP2_SIZE +\
161 					 MLX5E_INNER_TTC_GROUP3_SIZE)
162 
163 #ifdef CONFIG_MLX5_EN_RXNFC
164 
165 struct mlx5e_ethtool_table {
166 	struct mlx5_flow_table *ft;
167 	int                    num_rules;
168 };
169 
170 #define ETHTOOL_NUM_L3_L4_FTS 7
171 #define ETHTOOL_NUM_L2_FTS 4
172 
173 struct mlx5e_ethtool_steering {
174 	struct mlx5e_ethtool_table      l3_l4_ft[ETHTOOL_NUM_L3_L4_FTS];
175 	struct mlx5e_ethtool_table      l2_ft[ETHTOOL_NUM_L2_FTS];
176 	struct list_head                rules;
177 	int                             tot_num_rules;
178 };
179 
180 void mlx5e_ethtool_init_steering(struct mlx5e_priv *priv);
181 void mlx5e_ethtool_cleanup_steering(struct mlx5e_priv *priv);
182 int mlx5e_ethtool_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd);
183 int mlx5e_ethtool_get_rxnfc(struct net_device *dev,
184 			    struct ethtool_rxnfc *info, u32 *rule_locs);
185 #else
186 static inline void mlx5e_ethtool_init_steering(struct mlx5e_priv *priv)    { }
187 static inline void mlx5e_ethtool_cleanup_steering(struct mlx5e_priv *priv) { }
188 static inline int mlx5e_ethtool_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
189 { return -EOPNOTSUPP; }
190 static inline int mlx5e_ethtool_get_rxnfc(struct net_device *dev,
191 					  struct ethtool_rxnfc *info, u32 *rule_locs)
192 { return -EOPNOTSUPP; }
193 #endif /* CONFIG_MLX5_EN_RXNFC */
194 
195 #ifdef CONFIG_MLX5_EN_ARFS
196 struct mlx5e_arfs_tables;
197 
198 int mlx5e_arfs_create_tables(struct mlx5e_priv *priv);
199 void mlx5e_arfs_destroy_tables(struct mlx5e_priv *priv);
200 int mlx5e_arfs_enable(struct mlx5e_priv *priv);
201 int mlx5e_arfs_disable(struct mlx5e_priv *priv);
202 int mlx5e_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
203 			u16 rxq_index, u32 flow_id);
204 #else
205 static inline int mlx5e_arfs_create_tables(struct mlx5e_priv *priv) { return 0; }
206 static inline void mlx5e_arfs_destroy_tables(struct mlx5e_priv *priv) {}
207 static inline int mlx5e_arfs_enable(struct mlx5e_priv *priv) { return -EOPNOTSUPP; }
208 static inline int mlx5e_arfs_disable(struct mlx5e_priv *priv) {	return -EOPNOTSUPP; }
209 #endif
210 
211 #ifdef CONFIG_MLX5_EN_TLS
212 struct mlx5e_accel_fs_tcp;
213 #endif
214 
215 struct mlx5e_fs_udp;
216 struct mlx5e_fs_any;
217 struct mlx5e_ptp_fs;
218 
219 struct mlx5e_flow_steering {
220 	struct mlx5_flow_namespace      *ns;
221 	struct mlx5_flow_namespace      *egress_ns;
222 #ifdef CONFIG_MLX5_EN_RXNFC
223 	struct mlx5e_ethtool_steering   ethtool;
224 #endif
225 	struct mlx5e_tc_table           tc;
226 	struct mlx5e_promisc_table      promisc;
227 	struct mlx5e_vlan_table         *vlan;
228 	struct mlx5e_l2_table           l2;
229 	struct mlx5e_ttc_table          ttc;
230 	struct mlx5e_ttc_table          inner_ttc;
231 #ifdef CONFIG_MLX5_EN_ARFS
232 	struct mlx5e_arfs_tables       *arfs;
233 #endif
234 #ifdef CONFIG_MLX5_EN_TLS
235 	struct mlx5e_accel_fs_tcp      *accel_tcp;
236 #endif
237 	struct mlx5e_fs_udp            *udp;
238 	struct mlx5e_fs_any            *any;
239 	struct mlx5e_ptp_fs            *ptp_fs;
240 };
241 
242 struct ttc_params {
243 	struct mlx5_flow_table_attr ft_attr;
244 	u32 any_tt_tirn;
245 	u32 indir_tirn[MLX5E_NUM_INDIR_TIRS];
246 	struct mlx5e_ttc_table *inner_ttc;
247 };
248 
249 void mlx5e_set_ttc_basic_params(struct mlx5e_priv *priv, struct ttc_params *ttc_params);
250 void mlx5e_set_ttc_ft_params(struct ttc_params *ttc_params);
251 void mlx5e_set_inner_ttc_ft_params(struct ttc_params *ttc_params);
252 
253 int mlx5e_create_ttc_table(struct mlx5e_priv *priv, struct ttc_params *params,
254 			   struct mlx5e_ttc_table *ttc);
255 void mlx5e_destroy_ttc_table(struct mlx5e_priv *priv,
256 			     struct mlx5e_ttc_table *ttc);
257 
258 int mlx5e_create_inner_ttc_table(struct mlx5e_priv *priv, struct ttc_params *params,
259 				 struct mlx5e_ttc_table *ttc);
260 void mlx5e_destroy_inner_ttc_table(struct mlx5e_priv *priv,
261 				   struct mlx5e_ttc_table *ttc);
262 
263 void mlx5e_destroy_flow_table(struct mlx5e_flow_table *ft);
264 int mlx5e_ttc_fwd_dest(struct mlx5e_priv *priv, enum mlx5e_traffic_types type,
265 		       struct mlx5_flow_destination *new_dest);
266 struct mlx5_flow_destination
267 mlx5e_ttc_get_default_dest(struct mlx5e_priv *priv, enum mlx5e_traffic_types type);
268 int mlx5e_ttc_fwd_default_dest(struct mlx5e_priv *priv, enum mlx5e_traffic_types type);
269 
270 void mlx5e_enable_cvlan_filter(struct mlx5e_priv *priv);
271 void mlx5e_disable_cvlan_filter(struct mlx5e_priv *priv);
272 
273 int mlx5e_create_flow_steering(struct mlx5e_priv *priv);
274 void mlx5e_destroy_flow_steering(struct mlx5e_priv *priv);
275 
276 u8 mlx5e_get_proto_by_tunnel_type(enum mlx5e_tunnel_types tt);
277 int mlx5e_add_vlan_trap(struct mlx5e_priv *priv, int  trap_id, int tir_num);
278 void mlx5e_remove_vlan_trap(struct mlx5e_priv *priv);
279 int mlx5e_add_mac_trap(struct mlx5e_priv *priv, int  trap_id, int tir_num);
280 void mlx5e_remove_mac_trap(struct mlx5e_priv *priv);
281 
282 #endif /* __MLX5E_FLOW_STEER_H__ */
283 
284