net/mlx5: fix setting of Rx hash fields
[dpdk.git] / drivers / net / mlx5 / mlx5_flow.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018 Mellanox Technologies, Ltd
3  */
4
5 #ifndef RTE_PMD_MLX5_FLOW_H_
6 #define RTE_PMD_MLX5_FLOW_H_
7
8 #include <netinet/in.h>
9 #include <sys/queue.h>
10 #include <stdalign.h>
11 #include <stdint.h>
12 #include <string.h>
13
14 /* Verbs header. */
15 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
16 #ifdef PEDANTIC
17 #pragma GCC diagnostic ignored "-Wpedantic"
18 #endif
19 #include <infiniband/verbs.h>
20 #ifdef PEDANTIC
21 #pragma GCC diagnostic error "-Wpedantic"
22 #endif
23
24 #include <rte_atomic.h>
25 #include <rte_alarm.h>
26 #include <rte_mtr.h>
27
28 #include "mlx5.h"
29 #include "mlx5_prm.h"
30
31 /* Private rte flow items. */
32 enum mlx5_rte_flow_item_type {
33         MLX5_RTE_FLOW_ITEM_TYPE_END = INT_MIN,
34         MLX5_RTE_FLOW_ITEM_TYPE_TAG,
35         MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE,
36 };
37
38 /* Private (internal) rte flow actions. */
39 enum mlx5_rte_flow_action_type {
40         MLX5_RTE_FLOW_ACTION_TYPE_END = INT_MIN,
41         MLX5_RTE_FLOW_ACTION_TYPE_TAG,
42         MLX5_RTE_FLOW_ACTION_TYPE_MARK,
43         MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
44 };
45
46 /* Matches on selected register. */
47 struct mlx5_rte_flow_item_tag {
48         enum modify_reg id;
49         uint32_t data;
50 };
51
52 /* Modify selected register. */
53 struct mlx5_rte_flow_action_set_tag {
54         enum modify_reg id;
55         uint32_t data;
56 };
57
58 struct mlx5_flow_action_copy_mreg {
59         enum modify_reg dst;
60         enum modify_reg src;
61 };
62
63 /* Matches on source queue. */
64 struct mlx5_rte_flow_item_tx_queue {
65         uint32_t queue;
66 };
67
68 /* Feature name to allocate metadata register. */
69 enum mlx5_feature_name {
70         MLX5_HAIRPIN_RX,
71         MLX5_HAIRPIN_TX,
72         MLX5_METADATA_RX,
73         MLX5_METADATA_TX,
74         MLX5_METADATA_FDB,
75         MLX5_FLOW_MARK,
76         MLX5_APP_TAG,
77         MLX5_COPY_MARK,
78         MLX5_MTR_COLOR,
79         MLX5_MTR_SFX,
80 };
81
82 /* Pattern outer Layer bits. */
83 #define MLX5_FLOW_LAYER_OUTER_L2 (1u << 0)
84 #define MLX5_FLOW_LAYER_OUTER_L3_IPV4 (1u << 1)
85 #define MLX5_FLOW_LAYER_OUTER_L3_IPV6 (1u << 2)
86 #define MLX5_FLOW_LAYER_OUTER_L4_UDP (1u << 3)
87 #define MLX5_FLOW_LAYER_OUTER_L4_TCP (1u << 4)
88 #define MLX5_FLOW_LAYER_OUTER_VLAN (1u << 5)
89
90 /* Pattern inner Layer bits. */
91 #define MLX5_FLOW_LAYER_INNER_L2 (1u << 6)
92 #define MLX5_FLOW_LAYER_INNER_L3_IPV4 (1u << 7)
93 #define MLX5_FLOW_LAYER_INNER_L3_IPV6 (1u << 8)
94 #define MLX5_FLOW_LAYER_INNER_L4_UDP (1u << 9)
95 #define MLX5_FLOW_LAYER_INNER_L4_TCP (1u << 10)
96 #define MLX5_FLOW_LAYER_INNER_VLAN (1u << 11)
97
98 /* Pattern tunnel Layer bits. */
99 #define MLX5_FLOW_LAYER_VXLAN (1u << 12)
100 #define MLX5_FLOW_LAYER_VXLAN_GPE (1u << 13)
101 #define MLX5_FLOW_LAYER_GRE (1u << 14)
102 #define MLX5_FLOW_LAYER_MPLS (1u << 15)
103 /* List of tunnel Layer bits continued below. */
104
105 /* General pattern items bits. */
106 #define MLX5_FLOW_ITEM_METADATA (1u << 16)
107 #define MLX5_FLOW_ITEM_PORT_ID (1u << 17)
108 #define MLX5_FLOW_ITEM_TAG (1u << 18)
109 #define MLX5_FLOW_ITEM_MARK (1u << 19)
110
111 /* Pattern MISC bits. */
112 #define MLX5_FLOW_LAYER_ICMP (1u << 20)
113 #define MLX5_FLOW_LAYER_ICMP6 (1u << 21)
114 #define MLX5_FLOW_LAYER_GRE_KEY (1u << 22)
115
116 /* Pattern tunnel Layer bits (continued). */
117 #define MLX5_FLOW_LAYER_IPIP (1u << 23)
118 #define MLX5_FLOW_LAYER_IPV6_ENCAP (1u << 24)
119 #define MLX5_FLOW_LAYER_NVGRE (1u << 25)
120 #define MLX5_FLOW_LAYER_GENEVE (1u << 26)
121
122 /* Queue items. */
123 #define MLX5_FLOW_ITEM_TX_QUEUE (1u << 27)
124
125 /* Outer Masks. */
126 #define MLX5_FLOW_LAYER_OUTER_L3 \
127         (MLX5_FLOW_LAYER_OUTER_L3_IPV4 | MLX5_FLOW_LAYER_OUTER_L3_IPV6)
128 #define MLX5_FLOW_LAYER_OUTER_L4 \
129         (MLX5_FLOW_LAYER_OUTER_L4_UDP | MLX5_FLOW_LAYER_OUTER_L4_TCP)
130 #define MLX5_FLOW_LAYER_OUTER \
131         (MLX5_FLOW_LAYER_OUTER_L2 | MLX5_FLOW_LAYER_OUTER_L3 | \
132          MLX5_FLOW_LAYER_OUTER_L4)
133
134 /* Tunnel Masks. */
135 #define MLX5_FLOW_LAYER_TUNNEL \
136         (MLX5_FLOW_LAYER_VXLAN | MLX5_FLOW_LAYER_VXLAN_GPE | \
137          MLX5_FLOW_LAYER_GRE | MLX5_FLOW_LAYER_NVGRE | MLX5_FLOW_LAYER_MPLS | \
138          MLX5_FLOW_LAYER_IPIP | MLX5_FLOW_LAYER_IPV6_ENCAP | \
139          MLX5_FLOW_LAYER_GENEVE)
140
141 /* Inner Masks. */
142 #define MLX5_FLOW_LAYER_INNER_L3 \
143         (MLX5_FLOW_LAYER_INNER_L3_IPV4 | MLX5_FLOW_LAYER_INNER_L3_IPV6)
144 #define MLX5_FLOW_LAYER_INNER_L4 \
145         (MLX5_FLOW_LAYER_INNER_L4_UDP | MLX5_FLOW_LAYER_INNER_L4_TCP)
146 #define MLX5_FLOW_LAYER_INNER \
147         (MLX5_FLOW_LAYER_INNER_L2 | MLX5_FLOW_LAYER_INNER_L3 | \
148          MLX5_FLOW_LAYER_INNER_L4)
149
150 /* Layer Masks. */
151 #define MLX5_FLOW_LAYER_L2 \
152         (MLX5_FLOW_LAYER_OUTER_L2 | MLX5_FLOW_LAYER_INNER_L2)
153 #define MLX5_FLOW_LAYER_L3_IPV4 \
154         (MLX5_FLOW_LAYER_OUTER_L3_IPV4 | MLX5_FLOW_LAYER_INNER_L3_IPV4)
155 #define MLX5_FLOW_LAYER_L3_IPV6 \
156         (MLX5_FLOW_LAYER_OUTER_L3_IPV6 | MLX5_FLOW_LAYER_INNER_L3_IPV6)
157 #define MLX5_FLOW_LAYER_L3 \
158         (MLX5_FLOW_LAYER_L3_IPV4 | MLX5_FLOW_LAYER_L3_IPV6)
159 #define MLX5_FLOW_LAYER_L4 \
160         (MLX5_FLOW_LAYER_OUTER_L4 | MLX5_FLOW_LAYER_INNER_L4)
161
162 /* Actions */
163 #define MLX5_FLOW_ACTION_DROP (1u << 0)
164 #define MLX5_FLOW_ACTION_QUEUE (1u << 1)
165 #define MLX5_FLOW_ACTION_RSS (1u << 2)
166 #define MLX5_FLOW_ACTION_FLAG (1u << 3)
167 #define MLX5_FLOW_ACTION_MARK (1u << 4)
168 #define MLX5_FLOW_ACTION_COUNT (1u << 5)
169 #define MLX5_FLOW_ACTION_PORT_ID (1u << 6)
170 #define MLX5_FLOW_ACTION_OF_POP_VLAN (1u << 7)
171 #define MLX5_FLOW_ACTION_OF_PUSH_VLAN (1u << 8)
172 #define MLX5_FLOW_ACTION_OF_SET_VLAN_VID (1u << 9)
173 #define MLX5_FLOW_ACTION_OF_SET_VLAN_PCP (1u << 10)
174 #define MLX5_FLOW_ACTION_SET_IPV4_SRC (1u << 11)
175 #define MLX5_FLOW_ACTION_SET_IPV4_DST (1u << 12)
176 #define MLX5_FLOW_ACTION_SET_IPV6_SRC (1u << 13)
177 #define MLX5_FLOW_ACTION_SET_IPV6_DST (1u << 14)
178 #define MLX5_FLOW_ACTION_SET_TP_SRC (1u << 15)
179 #define MLX5_FLOW_ACTION_SET_TP_DST (1u << 16)
180 #define MLX5_FLOW_ACTION_JUMP (1u << 17)
181 #define MLX5_FLOW_ACTION_SET_TTL (1u << 18)
182 #define MLX5_FLOW_ACTION_DEC_TTL (1u << 19)
183 #define MLX5_FLOW_ACTION_SET_MAC_SRC (1u << 20)
184 #define MLX5_FLOW_ACTION_SET_MAC_DST (1u << 21)
185 #define MLX5_FLOW_ACTION_VXLAN_ENCAP (1u << 22)
186 #define MLX5_FLOW_ACTION_VXLAN_DECAP (1u << 23)
187 #define MLX5_FLOW_ACTION_NVGRE_ENCAP (1u << 24)
188 #define MLX5_FLOW_ACTION_NVGRE_DECAP (1u << 25)
189 #define MLX5_FLOW_ACTION_RAW_ENCAP (1u << 26)
190 #define MLX5_FLOW_ACTION_RAW_DECAP (1u << 27)
191 #define MLX5_FLOW_ACTION_INC_TCP_SEQ (1u << 28)
192 #define MLX5_FLOW_ACTION_DEC_TCP_SEQ (1u << 29)
193 #define MLX5_FLOW_ACTION_INC_TCP_ACK (1u << 30)
194 #define MLX5_FLOW_ACTION_DEC_TCP_ACK (1u << 31)
195 #define MLX5_FLOW_ACTION_SET_TAG (1ull << 32)
196 #define MLX5_FLOW_ACTION_MARK_EXT (1ull << 33)
197 #define MLX5_FLOW_ACTION_SET_META (1ull << 34)
198 #define MLX5_FLOW_ACTION_METER (1ull << 35)
199
200 #define MLX5_FLOW_FATE_ACTIONS \
201         (MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_QUEUE | \
202          MLX5_FLOW_ACTION_RSS | MLX5_FLOW_ACTION_JUMP)
203
204 #define MLX5_FLOW_FATE_ESWITCH_ACTIONS \
205         (MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_PORT_ID | \
206          MLX5_FLOW_ACTION_JUMP)
207
208 #define MLX5_FLOW_ENCAP_ACTIONS (MLX5_FLOW_ACTION_VXLAN_ENCAP | \
209                                  MLX5_FLOW_ACTION_NVGRE_ENCAP | \
210                                  MLX5_FLOW_ACTION_RAW_ENCAP | \
211                                  MLX5_FLOW_ACTION_OF_PUSH_VLAN)
212
213 #define MLX5_FLOW_DECAP_ACTIONS (MLX5_FLOW_ACTION_VXLAN_DECAP | \
214                                  MLX5_FLOW_ACTION_NVGRE_DECAP | \
215                                  MLX5_FLOW_ACTION_RAW_DECAP | \
216                                  MLX5_FLOW_ACTION_OF_POP_VLAN)
217
218 #define MLX5_FLOW_MODIFY_HDR_ACTIONS (MLX5_FLOW_ACTION_SET_IPV4_SRC | \
219                                       MLX5_FLOW_ACTION_SET_IPV4_DST | \
220                                       MLX5_FLOW_ACTION_SET_IPV6_SRC | \
221                                       MLX5_FLOW_ACTION_SET_IPV6_DST | \
222                                       MLX5_FLOW_ACTION_SET_TP_SRC | \
223                                       MLX5_FLOW_ACTION_SET_TP_DST | \
224                                       MLX5_FLOW_ACTION_SET_TTL | \
225                                       MLX5_FLOW_ACTION_DEC_TTL | \
226                                       MLX5_FLOW_ACTION_SET_MAC_SRC | \
227                                       MLX5_FLOW_ACTION_SET_MAC_DST | \
228                                       MLX5_FLOW_ACTION_INC_TCP_SEQ | \
229                                       MLX5_FLOW_ACTION_DEC_TCP_SEQ | \
230                                       MLX5_FLOW_ACTION_INC_TCP_ACK | \
231                                       MLX5_FLOW_ACTION_DEC_TCP_ACK | \
232                                       MLX5_FLOW_ACTION_OF_SET_VLAN_VID | \
233                                       MLX5_FLOW_ACTION_SET_TAG | \
234                                       MLX5_FLOW_ACTION_MARK_EXT | \
235                                       MLX5_FLOW_ACTION_SET_META)
236
237 #define MLX5_FLOW_VLAN_ACTIONS (MLX5_FLOW_ACTION_OF_POP_VLAN | \
238                                 MLX5_FLOW_ACTION_OF_PUSH_VLAN)
239 #ifndef IPPROTO_MPLS
240 #define IPPROTO_MPLS 137
241 #endif
242
243 /* UDP port number for MPLS */
244 #define MLX5_UDP_PORT_MPLS 6635
245
246 /* UDP port numbers for VxLAN. */
247 #define MLX5_UDP_PORT_VXLAN 4789
248 #define MLX5_UDP_PORT_VXLAN_GPE 4790
249
250 /* UDP port numbers for GENEVE. */
251 #define MLX5_UDP_PORT_GENEVE 6081
252
253 /* Priority reserved for default flows. */
254 #define MLX5_FLOW_PRIO_RSVD ((uint32_t)-1)
255
256 /*
257  * Number of sub priorities.
258  * For each kind of pattern matching i.e. L2, L3, L4 to have a correct
259  * matching on the NIC (firmware dependent) L4 most have the higher priority
260  * followed by L3 and ending with L2.
261  */
262 #define MLX5_PRIORITY_MAP_L2 2
263 #define MLX5_PRIORITY_MAP_L3 1
264 #define MLX5_PRIORITY_MAP_L4 0
265 #define MLX5_PRIORITY_MAP_MAX 3
266
267 /* Valid layer type for IPV4 RSS. */
268 #define MLX5_IPV4_LAYER_TYPES \
269         (ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 | \
270          ETH_RSS_NONFRAG_IPV4_TCP | ETH_RSS_NONFRAG_IPV4_UDP | \
271          ETH_RSS_NONFRAG_IPV4_OTHER)
272
273 /* IBV hash source bits  for IPV4. */
274 #define MLX5_IPV4_IBV_RX_HASH (IBV_RX_HASH_SRC_IPV4 | IBV_RX_HASH_DST_IPV4)
275
276 /* Valid layer type for IPV6 RSS. */
277 #define MLX5_IPV6_LAYER_TYPES \
278         (ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 | ETH_RSS_NONFRAG_IPV6_TCP | \
279          ETH_RSS_NONFRAG_IPV6_UDP | ETH_RSS_IPV6_EX  | ETH_RSS_IPV6_TCP_EX | \
280          ETH_RSS_IPV6_UDP_EX | ETH_RSS_NONFRAG_IPV6_OTHER)
281
282 /* IBV hash source bits  for IPV6. */
283 #define MLX5_IPV6_IBV_RX_HASH (IBV_RX_HASH_SRC_IPV6 | IBV_RX_HASH_DST_IPV6)
284
285 /* IBV hash bits for L3 SRC. */
286 #define MLX5_L3_SRC_IBV_RX_HASH (IBV_RX_HASH_SRC_IPV4 | IBV_RX_HASH_SRC_IPV6)
287
288 /* IBV hash bits for L3 DST. */
289 #define MLX5_L3_DST_IBV_RX_HASH (IBV_RX_HASH_DST_IPV4 | IBV_RX_HASH_DST_IPV6)
290
291 /* IBV hash bits for TCP. */
292 #define MLX5_TCP_IBV_RX_HASH (IBV_RX_HASH_SRC_PORT_TCP | \
293                               IBV_RX_HASH_DST_PORT_TCP)
294
295 /* IBV hash bits for UDP. */
296 #define MLX5_UDP_IBV_RX_HASH (IBV_RX_HASH_SRC_PORT_UDP | \
297                               IBV_RX_HASH_DST_PORT_UDP)
298
299 /* IBV hash bits for L4 SRC. */
300 #define MLX5_L4_SRC_IBV_RX_HASH (IBV_RX_HASH_SRC_PORT_TCP | \
301                                  IBV_RX_HASH_SRC_PORT_UDP)
302
303 /* IBV hash bits for L4 DST. */
304 #define MLX5_L4_DST_IBV_RX_HASH (IBV_RX_HASH_DST_PORT_TCP | \
305                                  IBV_RX_HASH_DST_PORT_UDP)
306
307 /* Geneve header first 16Bit */
308 #define MLX5_GENEVE_VER_MASK 0x3
309 #define MLX5_GENEVE_VER_SHIFT 14
310 #define MLX5_GENEVE_VER_VAL(a) \
311                 (((a) >> (MLX5_GENEVE_VER_SHIFT)) & (MLX5_GENEVE_VER_MASK))
312 #define MLX5_GENEVE_OPTLEN_MASK 0x3F
313 #define MLX5_GENEVE_OPTLEN_SHIFT 7
314 #define MLX5_GENEVE_OPTLEN_VAL(a) \
315             (((a) >> (MLX5_GENEVE_OPTLEN_SHIFT)) & (MLX5_GENEVE_OPTLEN_MASK))
316 #define MLX5_GENEVE_OAMF_MASK 0x1
317 #define MLX5_GENEVE_OAMF_SHIFT 7
318 #define MLX5_GENEVE_OAMF_VAL(a) \
319                 (((a) >> (MLX5_GENEVE_OAMF_SHIFT)) & (MLX5_GENEVE_OAMF_MASK))
320 #define MLX5_GENEVE_CRITO_MASK 0x1
321 #define MLX5_GENEVE_CRITO_SHIFT 6
322 #define MLX5_GENEVE_CRITO_VAL(a) \
323                 (((a) >> (MLX5_GENEVE_CRITO_SHIFT)) & (MLX5_GENEVE_CRITO_MASK))
324 #define MLX5_GENEVE_RSVD_MASK 0x3F
325 #define MLX5_GENEVE_RSVD_VAL(a) ((a) & (MLX5_GENEVE_RSVD_MASK))
326 /*
327  * The length of the Geneve options fields, expressed in four byte multiples,
328  * not including the eight byte fixed tunnel.
329  */
330 #define MLX5_GENEVE_OPT_LEN_0 14
331 #define MLX5_GENEVE_OPT_LEN_1 63
332
333 enum mlx5_flow_drv_type {
334         MLX5_FLOW_TYPE_MIN,
335         MLX5_FLOW_TYPE_DV,
336         MLX5_FLOW_TYPE_VERBS,
337         MLX5_FLOW_TYPE_MAX,
338 };
339
340 /* Matcher PRM representation */
341 struct mlx5_flow_dv_match_params {
342         size_t size;
343         /**< Size of match value. Do NOT split size and key! */
344         uint32_t buf[MLX5_ST_SZ_DW(fte_match_param)];
345         /**< Matcher value. This value is used as the mask or as a key. */
346 };
347
348 /* Matcher structure. */
349 struct mlx5_flow_dv_matcher {
350         LIST_ENTRY(mlx5_flow_dv_matcher) next;
351         /**< Pointer to the next element. */
352         struct mlx5_flow_tbl_resource *tbl;
353         /**< Pointer to the table(group) the matcher associated with. */
354         rte_atomic32_t refcnt; /**< Reference counter. */
355         void *matcher_object; /**< Pointer to DV matcher */
356         uint16_t crc; /**< CRC of key. */
357         uint16_t priority; /**< Priority of matcher. */
358         struct mlx5_flow_dv_match_params mask; /**< Matcher mask. */
359 };
360
361 #define MLX5_ENCAP_MAX_LEN 132
362
363 /* Encap/decap resource structure. */
364 struct mlx5_flow_dv_encap_decap_resource {
365         LIST_ENTRY(mlx5_flow_dv_encap_decap_resource) next;
366         /* Pointer to next element. */
367         rte_atomic32_t refcnt; /**< Reference counter. */
368         void *verbs_action;
369         /**< Verbs encap/decap action object. */
370         uint8_t buf[MLX5_ENCAP_MAX_LEN];
371         size_t size;
372         uint8_t reformat_type;
373         uint8_t ft_type;
374         uint64_t flags; /**< Flags for RDMA API. */
375 };
376
377 /* Tag resource structure. */
378 struct mlx5_flow_dv_tag_resource {
379         struct mlx5_hlist_entry entry;
380         /**< hash list entry for tag resource, tag value as the key. */
381         void *action;
382         /**< Verbs tag action object. */
383         rte_atomic32_t refcnt; /**< Reference counter. */
384 };
385
386 /*
387  * Number of modification commands.
388  * If extensive metadata registers are supported
389  * the maximal actions amount is 16 and 8 otherwise.
390  */
391 #define MLX5_MODIFY_NUM 16
392 #define MLX5_MODIFY_NUM_NO_MREG 8
393
394 /* Modify resource structure */
395 struct mlx5_flow_dv_modify_hdr_resource {
396         LIST_ENTRY(mlx5_flow_dv_modify_hdr_resource) next;
397         /* Pointer to next element. */
398         rte_atomic32_t refcnt; /**< Reference counter. */
399         struct ibv_flow_action *verbs_action;
400         /**< Verbs modify header action object. */
401         uint8_t ft_type; /**< Flow table type, Rx or Tx. */
402         uint32_t actions_num; /**< Number of modification actions. */
403         struct mlx5_modification_cmd actions[MLX5_MODIFY_NUM];
404         /**< Modification actions. */
405         uint64_t flags; /**< Flags for RDMA API. */
406 };
407
408 /* Jump action resource structure. */
409 struct mlx5_flow_dv_jump_tbl_resource {
410         rte_atomic32_t refcnt; /**< Reference counter. */
411         uint8_t ft_type; /**< Flow table type, Rx or Tx. */
412         void *action; /**< Pointer to the rdma core action. */
413 };
414
415 /* Port ID resource structure. */
416 struct mlx5_flow_dv_port_id_action_resource {
417         LIST_ENTRY(mlx5_flow_dv_port_id_action_resource) next;
418         /* Pointer to next element. */
419         rte_atomic32_t refcnt; /**< Reference counter. */
420         void *action;
421         /**< Verbs tag action object. */
422         uint32_t port_id; /**< Port ID value. */
423 };
424
425 /* Push VLAN action resource structure */
426 struct mlx5_flow_dv_push_vlan_action_resource {
427         LIST_ENTRY(mlx5_flow_dv_push_vlan_action_resource) next;
428         /* Pointer to next element. */
429         rte_atomic32_t refcnt; /**< Reference counter. */
430         void *action; /**< Direct verbs action object. */
431         uint8_t ft_type; /**< Flow table type, Rx, Tx or FDB. */
432         rte_be32_t vlan_tag; /**< VLAN tag value. */
433 };
434
435 /* Metadata register copy table entry. */
436 struct mlx5_flow_mreg_copy_resource {
437         /*
438          * Hash list entry for copy table.
439          *  - Key is 32/64-bit MARK action ID.
440          *  - MUST be the first entry.
441          */
442         struct mlx5_hlist_entry hlist_ent;
443         LIST_ENTRY(mlx5_flow_mreg_copy_resource) next;
444         /* List entry for device flows. */
445         uint32_t refcnt; /* Reference counter. */
446         uint32_t appcnt; /* Apply/Remove counter. */
447         struct rte_flow *flow; /* Built flow for copy. */
448 };
449
450 /* Table data structure of the hash organization. */
451 struct mlx5_flow_tbl_data_entry {
452         struct mlx5_hlist_entry entry;
453         /**< hash list entry, 64-bits key inside. */
454         struct mlx5_flow_tbl_resource tbl;
455         /**< flow table resource. */
456         LIST_HEAD(matchers, mlx5_flow_dv_matcher) matchers;
457         /**< matchers' header associated with the flow table. */
458         struct mlx5_flow_dv_jump_tbl_resource jump;
459         /**< jump resource, at most one for each table created. */
460 };
461
462 /*
463  * Max number of actions per DV flow.
464  * See CREATE_FLOW_MAX_FLOW_ACTIONS_SUPPORTED
465  * In rdma-core file providers/mlx5/verbs.c
466  */
467 #define MLX5_DV_MAX_NUMBER_OF_ACTIONS 8
468
469 /* DV flows structure. */
470 struct mlx5_flow_dv {
471         struct mlx5_hrxq *hrxq; /**< Hash Rx queues. */
472         /* Flow DV api: */
473         struct mlx5_flow_dv_matcher *matcher; /**< Cache to matcher. */
474         struct mlx5_flow_dv_match_params value;
475         /**< Holds the value that the packet is compared to. */
476         struct mlx5_flow_dv_encap_decap_resource *encap_decap;
477         /**< Pointer to encap/decap resource in cache. */
478         struct mlx5_flow_dv_modify_hdr_resource *modify_hdr;
479         /**< Pointer to modify header resource in cache. */
480         struct ibv_flow *flow; /**< Installed flow. */
481         struct mlx5_flow_dv_jump_tbl_resource *jump;
482         /**< Pointer to the jump action resource. */
483         struct mlx5_flow_dv_port_id_action_resource *port_id_action;
484         /**< Pointer to port ID action resource. */
485         struct mlx5_vf_vlan vf_vlan;
486         /**< Structure for VF VLAN workaround. */
487         struct mlx5_flow_dv_push_vlan_action_resource *push_vlan_res;
488         /**< Pointer to push VLAN action resource in cache. */
489         struct mlx5_flow_dv_tag_resource *tag_resource;
490         /**< pointer to the tag action. */
491 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
492         void *actions[MLX5_DV_MAX_NUMBER_OF_ACTIONS];
493         /**< Action list. */
494 #endif
495         int actions_n; /**< number of actions. */
496 };
497
498 /* Verbs specification header. */
499 struct ibv_spec_header {
500         enum ibv_flow_spec_type type;
501         uint16_t size;
502 };
503
504 /** Handles information leading to a drop fate. */
505 struct mlx5_flow_verbs {
506         LIST_ENTRY(mlx5_flow_verbs) next;
507         unsigned int size; /**< Size of the attribute. */
508         struct {
509                 struct ibv_flow_attr *attr;
510                 /**< Pointer to the Specification buffer. */
511                 uint8_t *specs; /**< Pointer to the specifications. */
512         };
513         struct ibv_flow *flow; /**< Verbs flow pointer. */
514         struct mlx5_hrxq *hrxq; /**< Hash Rx queue object. */
515         struct mlx5_vf_vlan vf_vlan;
516         /**< Structure for VF VLAN workaround. */
517 };
518
519 struct mlx5_flow_rss {
520         uint32_t level;
521         uint32_t queue_num; /**< Number of entries in @p queue. */
522         uint64_t types; /**< Specific RSS hash types (see ETH_RSS_*). */
523         uint16_t (*queue)[]; /**< Destination queues to redirect traffic to. */
524         uint8_t key[MLX5_RSS_HASH_KEY_LEN]; /**< RSS hash key. */
525 };
526
527 /** Device flow structure. */
528 struct mlx5_flow {
529         LIST_ENTRY(mlx5_flow) next;
530         struct rte_flow *flow; /**< Pointer to the main flow. */
531         uint64_t layers;
532         /**< Bit-fields of present layers, see MLX5_FLOW_LAYER_*. */
533         uint64_t actions;
534         /**< Bit-fields of detected actions, see MLX5_FLOW_ACTION_*. */
535         uint64_t hash_fields; /**< Verbs hash Rx queue hash fields. */
536         uint8_t ingress; /**< 1 if the flow is ingress. */
537         uint32_t group; /**< The group index. */
538         uint8_t transfer; /**< 1 if the flow is E-Switch flow. */
539         union {
540 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
541                 struct mlx5_flow_dv dv;
542 #endif
543                 struct mlx5_flow_verbs verbs;
544         };
545         union {
546                 uint32_t qrss_id; /**< Uniqie Q/RSS suffix subflow tag. */
547                 uint32_t mtr_flow_id; /**< Unique meter match flow id. */
548         };
549         bool external; /**< true if the flow is created external to PMD. */
550 };
551
552 /* Flow meter state. */
553 #define MLX5_FLOW_METER_DISABLE 0
554 #define MLX5_FLOW_METER_ENABLE 1
555
556 #define MLX5_MAN_WIDTH 8
557 /* Modify this value if enum rte_mtr_color changes. */
558 #define RTE_MTR_DROPPED RTE_COLORS
559
560 /* Meter policer statistics */
561 struct mlx5_flow_policer_stats {
562         struct mlx5_flow_counter *cnt[RTE_COLORS + 1];
563         /**< Color counter, extra for drop. */
564         uint64_t stats_mask;
565         /**< Statistics mask for the colors. */
566 };
567
568 /* Meter table structure. */
569 struct mlx5_meter_domain_info {
570         struct mlx5_flow_tbl_resource *tbl;
571         /**< Meter table. */
572         void *any_matcher;
573         /**< Meter color not match default criteria. */
574         void *color_matcher;
575         /**< Meter color match criteria. */
576         void *jump_actn;
577         /**< Meter match action. */
578         void *policer_rules[RTE_MTR_DROPPED + 1];
579         /**< Meter policer for the match. */
580 };
581
582 /* Meter table set for TX RX FDB. */
583 struct mlx5_meter_domains_infos {
584         uint32_t ref_cnt;
585         /**< Table user count. */
586         struct mlx5_meter_domain_info egress;
587         /**< TX meter table. */
588         struct mlx5_meter_domain_info ingress;
589         /**< RX meter table. */
590         struct mlx5_meter_domain_info transfer;
591         /**< FDB meter table. */
592         void *drop_actn;
593         /**< Drop action as not matched. */
594         void *count_actns[RTE_MTR_DROPPED + 1];
595         /**< Counters for match and unmatched statistics. */
596         uint32_t fmp[MLX5_ST_SZ_DW(flow_meter_parameters)];
597         /**< Flow meter parameter. */
598         size_t fmp_size;
599         /**< Flow meter parameter size. */
600         void *meter_action;
601         /**< Flow meter action. */
602 };
603
604 /* Meter parameter structure. */
605 struct mlx5_flow_meter {
606         TAILQ_ENTRY(mlx5_flow_meter) next;
607         /**< Pointer to the next flow meter structure. */
608         uint32_t meter_id;
609         /**< Meter id. */
610         struct rte_mtr_params params;
611         /**< Meter rule parameters. */
612         struct mlx5_flow_meter_profile *profile;
613         /**< Meter profile parameters. */
614         struct rte_flow_attr attr;
615         /**< Flow attributes. */
616         struct mlx5_meter_domains_infos *mfts;
617         /**< Flow table created for this meter. */
618         struct mlx5_flow_policer_stats policer_stats;
619         /**< Meter policer statistics. */
620         uint32_t ref_cnt;
621         /**< Use count. */
622         uint32_t active_state:1;
623         /**< Meter state. */
624         uint32_t shared:1;
625         /**< Meter shared or not. */
626 };
627
628 /* RFC2697 parameter structure. */
629 struct mlx5_flow_meter_srtcm_rfc2697_prm {
630         /* green_saturation_value = cbs_mantissa * 2^cbs_exponent */
631         uint32_t cbs_exponent:5;
632         uint32_t cbs_mantissa:8;
633         /* cir = 8G * cir_mantissa * 1/(2^cir_exponent) Bytes/Sec */
634         uint32_t cir_exponent:5;
635         uint32_t cir_mantissa:8;
636         /* yellow _saturation_value = ebs_mantissa * 2^ebs_exponent */
637         uint32_t ebs_exponent:5;
638         uint32_t ebs_mantissa:8;
639 };
640
641 /* Flow meter profile structure. */
642 struct mlx5_flow_meter_profile {
643         TAILQ_ENTRY(mlx5_flow_meter_profile) next;
644         /**< Pointer to the next flow meter structure. */
645         uint32_t meter_profile_id; /**< Profile id. */
646         struct rte_mtr_meter_profile profile; /**< Profile detail. */
647         union {
648                 struct mlx5_flow_meter_srtcm_rfc2697_prm srtcm_prm;
649                 /**< srtcm_rfc2697 struct. */
650         };
651         uint32_t ref_cnt; /**< Use count. */
652 };
653
654 /* Flow structure. */
655 struct rte_flow {
656         TAILQ_ENTRY(rte_flow) next; /**< Pointer to the next flow structure. */
657         enum mlx5_flow_drv_type drv_type; /**< Driver type. */
658         struct mlx5_flow_rss rss; /**< RSS context. */
659         struct mlx5_flow_counter *counter; /**< Holds flow counter. */
660         struct mlx5_flow_mreg_copy_resource *mreg_copy;
661         /**< pointer to metadata register copy table resource. */
662         struct mlx5_flow_meter *meter; /**< Holds flow meter. */
663         LIST_HEAD(dev_flows, mlx5_flow) dev_flows;
664         /**< Device flows that are part of the flow. */
665         struct mlx5_fdir *fdir; /**< Pointer to associated FDIR if any. */
666         uint32_t hairpin_flow_id; /**< The flow id used for hairpin. */
667         uint32_t copy_applied:1; /**< The MARK copy Flow os applied. */
668 };
669
670 typedef int (*mlx5_flow_validate_t)(struct rte_eth_dev *dev,
671                                     const struct rte_flow_attr *attr,
672                                     const struct rte_flow_item items[],
673                                     const struct rte_flow_action actions[],
674                                     bool external,
675                                     struct rte_flow_error *error);
676 typedef struct mlx5_flow *(*mlx5_flow_prepare_t)
677         (const struct rte_flow_attr *attr, const struct rte_flow_item items[],
678          const struct rte_flow_action actions[], struct rte_flow_error *error);
679 typedef int (*mlx5_flow_translate_t)(struct rte_eth_dev *dev,
680                                      struct mlx5_flow *dev_flow,
681                                      const struct rte_flow_attr *attr,
682                                      const struct rte_flow_item items[],
683                                      const struct rte_flow_action actions[],
684                                      struct rte_flow_error *error);
685 typedef int (*mlx5_flow_apply_t)(struct rte_eth_dev *dev, struct rte_flow *flow,
686                                  struct rte_flow_error *error);
687 typedef void (*mlx5_flow_remove_t)(struct rte_eth_dev *dev,
688                                    struct rte_flow *flow);
689 typedef void (*mlx5_flow_destroy_t)(struct rte_eth_dev *dev,
690                                     struct rte_flow *flow);
691 typedef int (*mlx5_flow_query_t)(struct rte_eth_dev *dev,
692                                  struct rte_flow *flow,
693                                  const struct rte_flow_action *actions,
694                                  void *data,
695                                  struct rte_flow_error *error);
696 typedef struct mlx5_meter_domains_infos *(*mlx5_flow_create_mtr_tbls_t)
697                                             (struct rte_eth_dev *dev,
698                                              const struct mlx5_flow_meter *fm);
699 typedef int (*mlx5_flow_destroy_mtr_tbls_t)(struct rte_eth_dev *dev,
700                                         struct mlx5_meter_domains_infos *tbls);
701 typedef int (*mlx5_flow_create_policer_rules_t)
702                                         (struct rte_eth_dev *dev,
703                                          struct mlx5_flow_meter *fm,
704                                          const struct rte_flow_attr *attr);
705 typedef int (*mlx5_flow_destroy_policer_rules_t)
706                                         (struct rte_eth_dev *dev,
707                                          const struct mlx5_flow_meter *fm,
708                                          const struct rte_flow_attr *attr);
709 typedef struct mlx5_flow_counter * (*mlx5_flow_counter_alloc_t)
710                                    (struct rte_eth_dev *dev);
711 typedef void (*mlx5_flow_counter_free_t)(struct rte_eth_dev *dev,
712                                          struct mlx5_flow_counter *cnt);
713 typedef int (*mlx5_flow_counter_query_t)(struct rte_eth_dev *dev,
714                                          struct mlx5_flow_counter *cnt,
715                                          bool clear, uint64_t *pkts,
716                                          uint64_t *bytes);
717 struct mlx5_flow_driver_ops {
718         mlx5_flow_validate_t validate;
719         mlx5_flow_prepare_t prepare;
720         mlx5_flow_translate_t translate;
721         mlx5_flow_apply_t apply;
722         mlx5_flow_remove_t remove;
723         mlx5_flow_destroy_t destroy;
724         mlx5_flow_query_t query;
725         mlx5_flow_create_mtr_tbls_t create_mtr_tbls;
726         mlx5_flow_destroy_mtr_tbls_t destroy_mtr_tbls;
727         mlx5_flow_create_policer_rules_t create_policer_rules;
728         mlx5_flow_destroy_policer_rules_t destroy_policer_rules;
729         mlx5_flow_counter_alloc_t counter_alloc;
730         mlx5_flow_counter_free_t counter_free;
731         mlx5_flow_counter_query_t counter_query;
732 };
733
734
735 #define MLX5_CNT_CONTAINER(sh, batch, thread) (&(sh)->cmng.ccont \
736         [(((sh)->cmng.mhi[batch] >> (thread)) & 0x1) * 2 + (batch)])
737 #define MLX5_CNT_CONTAINER_UNUSED(sh, batch, thread) (&(sh)->cmng.ccont \
738         [(~((sh)->cmng.mhi[batch] >> (thread)) & 0x1) * 2 + (batch)])
739
740 /* mlx5_flow.c */
741
742 struct mlx5_flow_id_pool *mlx5_flow_id_pool_alloc(void);
743 void mlx5_flow_id_pool_release(struct mlx5_flow_id_pool *pool);
744 uint32_t mlx5_flow_id_get(struct mlx5_flow_id_pool *pool, uint32_t *id);
745 uint32_t mlx5_flow_id_release(struct mlx5_flow_id_pool *pool,
746                               uint32_t id);
747 int mlx5_flow_group_to_table(const struct rte_flow_attr *attributes,
748                              bool external, uint32_t group, uint32_t *table,
749                              struct rte_flow_error *error);
750 uint64_t mlx5_flow_hashfields_adjust(struct mlx5_flow *dev_flow, int tunnel,
751                                      uint64_t layer_types,
752                                      uint64_t hash_fields);
753 uint32_t mlx5_flow_adjust_priority(struct rte_eth_dev *dev, int32_t priority,
754                                    uint32_t subpriority);
755 enum modify_reg mlx5_flow_get_reg_id(struct rte_eth_dev *dev,
756                                      enum mlx5_feature_name feature,
757                                      uint32_t id,
758                                      struct rte_flow_error *error);
759 const struct rte_flow_action *mlx5_flow_find_action
760                                         (const struct rte_flow_action *actions,
761                                          enum rte_flow_action_type action);
762 int mlx5_flow_validate_action_count(struct rte_eth_dev *dev,
763                                     const struct rte_flow_attr *attr,
764                                     struct rte_flow_error *error);
765 int mlx5_flow_validate_action_drop(uint64_t action_flags,
766                                    const struct rte_flow_attr *attr,
767                                    struct rte_flow_error *error);
768 int mlx5_flow_validate_action_flag(uint64_t action_flags,
769                                    const struct rte_flow_attr *attr,
770                                    struct rte_flow_error *error);
771 int mlx5_flow_validate_action_mark(const struct rte_flow_action *action,
772                                    uint64_t action_flags,
773                                    const struct rte_flow_attr *attr,
774                                    struct rte_flow_error *error);
775 int mlx5_flow_validate_action_queue(const struct rte_flow_action *action,
776                                     uint64_t action_flags,
777                                     struct rte_eth_dev *dev,
778                                     const struct rte_flow_attr *attr,
779                                     struct rte_flow_error *error);
780 int mlx5_flow_validate_action_rss(const struct rte_flow_action *action,
781                                   uint64_t action_flags,
782                                   struct rte_eth_dev *dev,
783                                   const struct rte_flow_attr *attr,
784                                   uint64_t item_flags,
785                                   struct rte_flow_error *error);
786 int mlx5_flow_validate_attributes(struct rte_eth_dev *dev,
787                                   const struct rte_flow_attr *attributes,
788                                   struct rte_flow_error *error);
789 int mlx5_flow_item_acceptable(const struct rte_flow_item *item,
790                               const uint8_t *mask,
791                               const uint8_t *nic_mask,
792                               unsigned int size,
793                               struct rte_flow_error *error);
794 int mlx5_flow_validate_item_eth(const struct rte_flow_item *item,
795                                 uint64_t item_flags,
796                                 struct rte_flow_error *error);
797 int mlx5_flow_validate_item_gre(const struct rte_flow_item *item,
798                                 uint64_t item_flags,
799                                 uint8_t target_protocol,
800                                 struct rte_flow_error *error);
801 int mlx5_flow_validate_item_gre_key(const struct rte_flow_item *item,
802                                     uint64_t item_flags,
803                                     const struct rte_flow_item *gre_item,
804                                     struct rte_flow_error *error);
805 int mlx5_flow_validate_item_ipv4(const struct rte_flow_item *item,
806                                  uint64_t item_flags,
807                                  uint64_t last_item,
808                                  uint16_t ether_type,
809                                  const struct rte_flow_item_ipv4 *acc_mask,
810                                  struct rte_flow_error *error);
811 int mlx5_flow_validate_item_ipv6(const struct rte_flow_item *item,
812                                  uint64_t item_flags,
813                                  uint64_t last_item,
814                                  uint16_t ether_type,
815                                  const struct rte_flow_item_ipv6 *acc_mask,
816                                  struct rte_flow_error *error);
817 int mlx5_flow_validate_item_mpls(struct rte_eth_dev *dev,
818                                  const struct rte_flow_item *item,
819                                  uint64_t item_flags,
820                                  uint64_t prev_layer,
821                                  struct rte_flow_error *error);
822 int mlx5_flow_validate_item_tcp(const struct rte_flow_item *item,
823                                 uint64_t item_flags,
824                                 uint8_t target_protocol,
825                                 const struct rte_flow_item_tcp *flow_mask,
826                                 struct rte_flow_error *error);
827 int mlx5_flow_validate_item_udp(const struct rte_flow_item *item,
828                                 uint64_t item_flags,
829                                 uint8_t target_protocol,
830                                 struct rte_flow_error *error);
831 int mlx5_flow_validate_item_vlan(const struct rte_flow_item *item,
832                                  uint64_t item_flags,
833                                  struct rte_eth_dev *dev,
834                                  struct rte_flow_error *error);
835 int mlx5_flow_validate_item_vxlan(const struct rte_flow_item *item,
836                                   uint64_t item_flags,
837                                   struct rte_flow_error *error);
838 int mlx5_flow_validate_item_vxlan_gpe(const struct rte_flow_item *item,
839                                       uint64_t item_flags,
840                                       struct rte_eth_dev *dev,
841                                       struct rte_flow_error *error);
842 int mlx5_flow_validate_item_icmp(const struct rte_flow_item *item,
843                                  uint64_t item_flags,
844                                  uint8_t target_protocol,
845                                  struct rte_flow_error *error);
846 int mlx5_flow_validate_item_icmp6(const struct rte_flow_item *item,
847                                    uint64_t item_flags,
848                                    uint8_t target_protocol,
849                                    struct rte_flow_error *error);
850 int mlx5_flow_validate_item_nvgre(const struct rte_flow_item *item,
851                                   uint64_t item_flags,
852                                   uint8_t target_protocol,
853                                   struct rte_flow_error *error);
854 int mlx5_flow_validate_item_geneve(const struct rte_flow_item *item,
855                                    uint64_t item_flags,
856                                    struct rte_eth_dev *dev,
857                                    struct rte_flow_error *error);
858 struct mlx5_meter_domains_infos *mlx5_flow_create_mtr_tbls
859                                         (struct rte_eth_dev *dev,
860                                          const struct mlx5_flow_meter *fm);
861 int mlx5_flow_destroy_mtr_tbls(struct rte_eth_dev *dev,
862                                struct mlx5_meter_domains_infos *tbl);
863 int mlx5_flow_create_policer_rules(struct rte_eth_dev *dev,
864                                    struct mlx5_flow_meter *fm,
865                                    const struct rte_flow_attr *attr);
866 int mlx5_flow_destroy_policer_rules(struct rte_eth_dev *dev,
867                                     struct mlx5_flow_meter *fm,
868                                     const struct rte_flow_attr *attr);
869 int mlx5_flow_meter_flush(struct rte_eth_dev *dev,
870                           struct rte_mtr_error *error);
871 #endif /* RTE_PMD_MLX5_FLOW_H_ */