xref: /dpdk/drivers/net/mlx5/mlx5_flow.c (revision 250c9eb3ca895127f21a729caf4a928eb2f04d2c)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016 6WIND S.A.
3  * Copyright 2016 Mellanox Technologies, Ltd
4  */
5 
6 #include <sys/queue.h>
7 #include <stdint.h>
8 #include <string.h>
9 
10 /* Verbs header. */
11 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
12 #ifdef PEDANTIC
13 #pragma GCC diagnostic ignored "-Wpedantic"
14 #endif
15 #include <infiniband/verbs.h>
16 #ifdef PEDANTIC
17 #pragma GCC diagnostic error "-Wpedantic"
18 #endif
19 
20 #include <rte_common.h>
21 #include <rte_ether.h>
22 #include <rte_eth_ctrl.h>
23 #include <rte_ethdev_driver.h>
24 #include <rte_flow.h>
25 #include <rte_flow_driver.h>
26 #include <rte_malloc.h>
27 #include <rte_ip.h>
28 
29 #include "mlx5.h"
30 #include "mlx5_defs.h"
31 #include "mlx5_prm.h"
32 #include "mlx5_glue.h"
33 
34 /* Flow priority for control plane flows. */
35 #define MLX5_CTRL_FLOW_PRIORITY 1
36 
37 /* Internet Protocol versions. */
38 #define MLX5_IPV4 4
39 #define MLX5_IPV6 6
40 #define MLX5_GRE 47
41 
42 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
43 struct ibv_flow_spec_counter_action {
44 	int dummy;
45 };
46 #endif
47 
48 /* Dev ops structure defined in mlx5.c */
49 extern const struct eth_dev_ops mlx5_dev_ops;
50 extern const struct eth_dev_ops mlx5_dev_ops_isolate;
51 
52 /** Structure give to the conversion functions. */
53 struct mlx5_flow_data {
54 	struct rte_eth_dev *dev; /** Ethernet device. */
55 	struct mlx5_flow_parse *parser; /** Parser context. */
56 	struct rte_flow_error *error; /** Error context. */
57 };
58 
59 static int
60 mlx5_flow_create_eth(const struct rte_flow_item *item,
61 		     const void *default_mask,
62 		     struct mlx5_flow_data *data);
63 
64 static int
65 mlx5_flow_create_vlan(const struct rte_flow_item *item,
66 		      const void *default_mask,
67 		      struct mlx5_flow_data *data);
68 
69 static int
70 mlx5_flow_create_ipv4(const struct rte_flow_item *item,
71 		      const void *default_mask,
72 		      struct mlx5_flow_data *data);
73 
74 static int
75 mlx5_flow_create_ipv6(const struct rte_flow_item *item,
76 		      const void *default_mask,
77 		      struct mlx5_flow_data *data);
78 
79 static int
80 mlx5_flow_create_udp(const struct rte_flow_item *item,
81 		     const void *default_mask,
82 		     struct mlx5_flow_data *data);
83 
84 static int
85 mlx5_flow_create_tcp(const struct rte_flow_item *item,
86 		     const void *default_mask,
87 		     struct mlx5_flow_data *data);
88 
89 static int
90 mlx5_flow_create_vxlan(const struct rte_flow_item *item,
91 		       const void *default_mask,
92 		       struct mlx5_flow_data *data);
93 
94 static int
95 mlx5_flow_create_vxlan_gpe(const struct rte_flow_item *item,
96 			   const void *default_mask,
97 			   struct mlx5_flow_data *data);
98 
99 static int
100 mlx5_flow_create_gre(const struct rte_flow_item *item,
101 		     const void *default_mask,
102 		     struct mlx5_flow_data *data);
103 
104 static int
105 mlx5_flow_create_mpls(const struct rte_flow_item *item,
106 		      const void *default_mask,
107 		      struct mlx5_flow_data *data);
108 
109 struct mlx5_flow_parse;
110 
111 static void
112 mlx5_flow_create_copy(struct mlx5_flow_parse *parser, void *src,
113 		      unsigned int size);
114 
115 static int
116 mlx5_flow_create_flag_mark(struct mlx5_flow_parse *parser, uint32_t mark_id);
117 
118 static int
119 mlx5_flow_create_count(struct rte_eth_dev *dev, struct mlx5_flow_parse *parser);
120 
121 /* Hash RX queue types. */
122 enum hash_rxq_type {
123 	HASH_RXQ_TCPV4,
124 	HASH_RXQ_UDPV4,
125 	HASH_RXQ_IPV4,
126 	HASH_RXQ_TCPV6,
127 	HASH_RXQ_UDPV6,
128 	HASH_RXQ_IPV6,
129 	HASH_RXQ_ETH,
130 	HASH_RXQ_TUNNEL,
131 };
132 
133 /* Initialization data for hash RX queue. */
134 struct hash_rxq_init {
135 	uint64_t hash_fields; /* Fields that participate in the hash. */
136 	uint64_t dpdk_rss_hf; /* Matching DPDK RSS hash fields. */
137 	unsigned int flow_priority; /* Flow priority to use. */
138 	unsigned int ip_version; /* Internet protocol. */
139 };
140 
141 /* Initialization data for hash RX queues. */
142 const struct hash_rxq_init hash_rxq_init[] = {
143 	[HASH_RXQ_TCPV4] = {
144 		.hash_fields = (IBV_RX_HASH_SRC_IPV4 |
145 				IBV_RX_HASH_DST_IPV4 |
146 				IBV_RX_HASH_SRC_PORT_TCP |
147 				IBV_RX_HASH_DST_PORT_TCP),
148 		.dpdk_rss_hf = ETH_RSS_NONFRAG_IPV4_TCP,
149 		.flow_priority = 0,
150 		.ip_version = MLX5_IPV4,
151 	},
152 	[HASH_RXQ_UDPV4] = {
153 		.hash_fields = (IBV_RX_HASH_SRC_IPV4 |
154 				IBV_RX_HASH_DST_IPV4 |
155 				IBV_RX_HASH_SRC_PORT_UDP |
156 				IBV_RX_HASH_DST_PORT_UDP),
157 		.dpdk_rss_hf = ETH_RSS_NONFRAG_IPV4_UDP,
158 		.flow_priority = 0,
159 		.ip_version = MLX5_IPV4,
160 	},
161 	[HASH_RXQ_IPV4] = {
162 		.hash_fields = (IBV_RX_HASH_SRC_IPV4 |
163 				IBV_RX_HASH_DST_IPV4),
164 		.dpdk_rss_hf = (ETH_RSS_IPV4 |
165 				ETH_RSS_FRAG_IPV4),
166 		.flow_priority = 1,
167 		.ip_version = MLX5_IPV4,
168 	},
169 	[HASH_RXQ_TCPV6] = {
170 		.hash_fields = (IBV_RX_HASH_SRC_IPV6 |
171 				IBV_RX_HASH_DST_IPV6 |
172 				IBV_RX_HASH_SRC_PORT_TCP |
173 				IBV_RX_HASH_DST_PORT_TCP),
174 		.dpdk_rss_hf = ETH_RSS_NONFRAG_IPV6_TCP,
175 		.flow_priority = 0,
176 		.ip_version = MLX5_IPV6,
177 	},
178 	[HASH_RXQ_UDPV6] = {
179 		.hash_fields = (IBV_RX_HASH_SRC_IPV6 |
180 				IBV_RX_HASH_DST_IPV6 |
181 				IBV_RX_HASH_SRC_PORT_UDP |
182 				IBV_RX_HASH_DST_PORT_UDP),
183 		.dpdk_rss_hf = ETH_RSS_NONFRAG_IPV6_UDP,
184 		.flow_priority = 0,
185 		.ip_version = MLX5_IPV6,
186 	},
187 	[HASH_RXQ_IPV6] = {
188 		.hash_fields = (IBV_RX_HASH_SRC_IPV6 |
189 				IBV_RX_HASH_DST_IPV6),
190 		.dpdk_rss_hf = (ETH_RSS_IPV6 |
191 				ETH_RSS_FRAG_IPV6),
192 		.flow_priority = 1,
193 		.ip_version = MLX5_IPV6,
194 	},
195 	[HASH_RXQ_ETH] = {
196 		.hash_fields = 0,
197 		.dpdk_rss_hf = 0,
198 		.flow_priority = 2,
199 	},
200 };
201 
202 /* Number of entries in hash_rxq_init[]. */
203 const unsigned int hash_rxq_init_n = RTE_DIM(hash_rxq_init);
204 
205 /** Structure for holding counter stats. */
206 struct mlx5_flow_counter_stats {
207 	uint64_t hits; /**< Number of packets matched by the rule. */
208 	uint64_t bytes; /**< Number of bytes matched by the rule. */
209 };
210 
211 /** Structure for Drop queue. */
212 struct mlx5_hrxq_drop {
213 	struct ibv_rwq_ind_table *ind_table; /**< Indirection table. */
214 	struct ibv_qp *qp; /**< Verbs queue pair. */
215 	struct ibv_wq *wq; /**< Verbs work queue. */
216 	struct ibv_cq *cq; /**< Verbs completion queue. */
217 };
218 
219 /* Flows structures. */
220 struct mlx5_flow {
221 	uint64_t hash_fields; /**< Fields that participate in the hash. */
222 	struct ibv_flow_attr *ibv_attr; /**< Pointer to Verbs attributes. */
223 	struct ibv_flow *ibv_flow; /**< Verbs flow. */
224 	struct mlx5_hrxq *hrxq; /**< Hash Rx queues. */
225 };
226 
227 /* Drop flows structures. */
228 struct mlx5_flow_drop {
229 	struct ibv_flow_attr *ibv_attr; /**< Pointer to Verbs attributes. */
230 	struct ibv_flow *ibv_flow; /**< Verbs flow. */
231 };
232 
233 struct rte_flow {
234 	TAILQ_ENTRY(rte_flow) next; /**< Pointer to the next flow structure. */
235 	uint32_t mark:1; /**< Set if the flow is marked. */
236 	uint32_t drop:1; /**< Drop queue. */
237 	struct rte_flow_action_rss rss_conf; /**< RSS configuration */
238 	uint16_t (*queues)[]; /**< Queues indexes to use. */
239 	uint8_t rss_key[40]; /**< copy of the RSS key. */
240 	uint32_t tunnel; /**< Tunnel type of RTE_PTYPE_TUNNEL_XXX. */
241 	struct ibv_counter_set *cs; /**< Holds the counters for the rule. */
242 	struct mlx5_flow_counter_stats counter_stats;/**<The counter stats. */
243 	struct mlx5_flow frxq[RTE_DIM(hash_rxq_init)];
244 	/**< Flow with Rx queue. */
245 };
246 
247 /** Static initializer for items. */
248 #define ITEMS(...) \
249 	(const enum rte_flow_item_type []){ \
250 		__VA_ARGS__, RTE_FLOW_ITEM_TYPE_END, \
251 	}
252 
253 #define IS_TUNNEL(type) ( \
254 	(type) == RTE_FLOW_ITEM_TYPE_VXLAN || \
255 	(type) == RTE_FLOW_ITEM_TYPE_VXLAN_GPE || \
256 	(type) == RTE_FLOW_ITEM_TYPE_GRE || \
257 	(type) == RTE_FLOW_ITEM_TYPE_MPLS)
258 
259 const uint32_t flow_ptype[] = {
260 	[RTE_FLOW_ITEM_TYPE_VXLAN] = RTE_PTYPE_TUNNEL_VXLAN,
261 	[RTE_FLOW_ITEM_TYPE_VXLAN_GPE] = RTE_PTYPE_TUNNEL_VXLAN_GPE,
262 	[RTE_FLOW_ITEM_TYPE_GRE] = RTE_PTYPE_TUNNEL_GRE,
263 	[RTE_FLOW_ITEM_TYPE_MPLS] = RTE_PTYPE_TUNNEL_MPLS_IN_GRE,
264 };
265 
266 #define PTYPE_IDX(t) ((RTE_PTYPE_TUNNEL_MASK & (t)) >> 12)
267 
268 const uint32_t ptype_ext[] = {
269 	[PTYPE_IDX(RTE_PTYPE_TUNNEL_VXLAN)] = RTE_PTYPE_TUNNEL_VXLAN |
270 					      RTE_PTYPE_L4_UDP,
271 	[PTYPE_IDX(RTE_PTYPE_TUNNEL_VXLAN_GPE)]	= RTE_PTYPE_TUNNEL_VXLAN_GPE |
272 						  RTE_PTYPE_L4_UDP,
273 	[PTYPE_IDX(RTE_PTYPE_TUNNEL_GRE)] = RTE_PTYPE_TUNNEL_GRE,
274 	[PTYPE_IDX(RTE_PTYPE_TUNNEL_MPLS_IN_GRE)] =
275 		RTE_PTYPE_TUNNEL_MPLS_IN_GRE,
276 	[PTYPE_IDX(RTE_PTYPE_TUNNEL_MPLS_IN_UDP)] =
277 		RTE_PTYPE_TUNNEL_MPLS_IN_GRE | RTE_PTYPE_L4_UDP,
278 };
279 
280 /** Structure to generate a simple graph of layers supported by the NIC. */
281 struct mlx5_flow_items {
282 	/** List of possible actions for these items. */
283 	const enum rte_flow_action_type *const actions;
284 	/** Bit-masks corresponding to the possibilities for the item. */
285 	const void *mask;
286 	/**
287 	 * Default bit-masks to use when item->mask is not provided. When
288 	 * \default_mask is also NULL, the full supported bit-mask (\mask) is
289 	 * used instead.
290 	 */
291 	const void *default_mask;
292 	/** Bit-masks size in bytes. */
293 	const unsigned int mask_sz;
294 	/**
295 	 * Conversion function from rte_flow to NIC specific flow.
296 	 *
297 	 * @param item
298 	 *   rte_flow item to convert.
299 	 * @param default_mask
300 	 *   Default bit-masks to use when item->mask is not provided.
301 	 * @param data
302 	 *   Internal structure to store the conversion.
303 	 *
304 	 * @return
305 	 *   0 on success, a negative errno value otherwise and rte_errno is
306 	 *   set.
307 	 */
308 	int (*convert)(const struct rte_flow_item *item,
309 		       const void *default_mask,
310 		       struct mlx5_flow_data *data);
311 	/** Size in bytes of the destination structure. */
312 	const unsigned int dst_sz;
313 	/** List of possible following items.  */
314 	const enum rte_flow_item_type *const items;
315 };
316 
317 /** Valid action for this PMD. */
318 static const enum rte_flow_action_type valid_actions[] = {
319 	RTE_FLOW_ACTION_TYPE_DROP,
320 	RTE_FLOW_ACTION_TYPE_QUEUE,
321 	RTE_FLOW_ACTION_TYPE_MARK,
322 	RTE_FLOW_ACTION_TYPE_FLAG,
323 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
324 	RTE_FLOW_ACTION_TYPE_COUNT,
325 #endif
326 	RTE_FLOW_ACTION_TYPE_END,
327 };
328 
329 /** Graph of supported items and associated actions. */
330 static const struct mlx5_flow_items mlx5_flow_items[] = {
331 	[RTE_FLOW_ITEM_TYPE_END] = {
332 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH,
333 			       RTE_FLOW_ITEM_TYPE_VXLAN,
334 			       RTE_FLOW_ITEM_TYPE_VXLAN_GPE,
335 			       RTE_FLOW_ITEM_TYPE_GRE),
336 	},
337 	[RTE_FLOW_ITEM_TYPE_ETH] = {
338 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_VLAN,
339 			       RTE_FLOW_ITEM_TYPE_IPV4,
340 			       RTE_FLOW_ITEM_TYPE_IPV6),
341 		.actions = valid_actions,
342 		.mask = &(const struct rte_flow_item_eth){
343 			.dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
344 			.src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
345 			.type = -1,
346 		},
347 		.default_mask = &rte_flow_item_eth_mask,
348 		.mask_sz = sizeof(struct rte_flow_item_eth),
349 		.convert = mlx5_flow_create_eth,
350 		.dst_sz = sizeof(struct ibv_flow_spec_eth),
351 	},
352 	[RTE_FLOW_ITEM_TYPE_VLAN] = {
353 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_IPV4,
354 			       RTE_FLOW_ITEM_TYPE_IPV6),
355 		.actions = valid_actions,
356 		.mask = &(const struct rte_flow_item_vlan){
357 			.tci = -1,
358 			.inner_type = -1,
359 		},
360 		.default_mask = &rte_flow_item_vlan_mask,
361 		.mask_sz = sizeof(struct rte_flow_item_vlan),
362 		.convert = mlx5_flow_create_vlan,
363 		.dst_sz = 0,
364 	},
365 	[RTE_FLOW_ITEM_TYPE_IPV4] = {
366 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
367 			       RTE_FLOW_ITEM_TYPE_TCP,
368 			       RTE_FLOW_ITEM_TYPE_GRE),
369 		.actions = valid_actions,
370 		.mask = &(const struct rte_flow_item_ipv4){
371 			.hdr = {
372 				.src_addr = -1,
373 				.dst_addr = -1,
374 				.type_of_service = -1,
375 				.next_proto_id = -1,
376 			},
377 		},
378 		.default_mask = &rte_flow_item_ipv4_mask,
379 		.mask_sz = sizeof(struct rte_flow_item_ipv4),
380 		.convert = mlx5_flow_create_ipv4,
381 		.dst_sz = sizeof(struct ibv_flow_spec_ipv4_ext),
382 	},
383 	[RTE_FLOW_ITEM_TYPE_IPV6] = {
384 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
385 			       RTE_FLOW_ITEM_TYPE_TCP,
386 			       RTE_FLOW_ITEM_TYPE_GRE),
387 		.actions = valid_actions,
388 		.mask = &(const struct rte_flow_item_ipv6){
389 			.hdr = {
390 				.src_addr = {
391 					0xff, 0xff, 0xff, 0xff,
392 					0xff, 0xff, 0xff, 0xff,
393 					0xff, 0xff, 0xff, 0xff,
394 					0xff, 0xff, 0xff, 0xff,
395 				},
396 				.dst_addr = {
397 					0xff, 0xff, 0xff, 0xff,
398 					0xff, 0xff, 0xff, 0xff,
399 					0xff, 0xff, 0xff, 0xff,
400 					0xff, 0xff, 0xff, 0xff,
401 				},
402 				.vtc_flow = -1,
403 				.proto = -1,
404 				.hop_limits = -1,
405 			},
406 		},
407 		.default_mask = &rte_flow_item_ipv6_mask,
408 		.mask_sz = sizeof(struct rte_flow_item_ipv6),
409 		.convert = mlx5_flow_create_ipv6,
410 		.dst_sz = sizeof(struct ibv_flow_spec_ipv6),
411 	},
412 	[RTE_FLOW_ITEM_TYPE_UDP] = {
413 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_VXLAN,
414 			       RTE_FLOW_ITEM_TYPE_VXLAN_GPE,
415 			       RTE_FLOW_ITEM_TYPE_MPLS),
416 		.actions = valid_actions,
417 		.mask = &(const struct rte_flow_item_udp){
418 			.hdr = {
419 				.src_port = -1,
420 				.dst_port = -1,
421 			},
422 		},
423 		.default_mask = &rte_flow_item_udp_mask,
424 		.mask_sz = sizeof(struct rte_flow_item_udp),
425 		.convert = mlx5_flow_create_udp,
426 		.dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
427 	},
428 	[RTE_FLOW_ITEM_TYPE_TCP] = {
429 		.actions = valid_actions,
430 		.mask = &(const struct rte_flow_item_tcp){
431 			.hdr = {
432 				.src_port = -1,
433 				.dst_port = -1,
434 			},
435 		},
436 		.default_mask = &rte_flow_item_tcp_mask,
437 		.mask_sz = sizeof(struct rte_flow_item_tcp),
438 		.convert = mlx5_flow_create_tcp,
439 		.dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
440 	},
441 	[RTE_FLOW_ITEM_TYPE_GRE] = {
442 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH,
443 			       RTE_FLOW_ITEM_TYPE_IPV4,
444 			       RTE_FLOW_ITEM_TYPE_IPV6,
445 			       RTE_FLOW_ITEM_TYPE_MPLS),
446 		.actions = valid_actions,
447 		.mask = &(const struct rte_flow_item_gre){
448 			.protocol = -1,
449 		},
450 		.default_mask = &rte_flow_item_gre_mask,
451 		.mask_sz = sizeof(struct rte_flow_item_gre),
452 		.convert = mlx5_flow_create_gre,
453 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
454 		.dst_sz = sizeof(struct ibv_flow_spec_gre),
455 #else
456 		.dst_sz = sizeof(struct ibv_flow_spec_tunnel),
457 #endif
458 	},
459 	[RTE_FLOW_ITEM_TYPE_MPLS] = {
460 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH,
461 			       RTE_FLOW_ITEM_TYPE_IPV4,
462 			       RTE_FLOW_ITEM_TYPE_IPV6),
463 		.actions = valid_actions,
464 		.mask = &(const struct rte_flow_item_mpls){
465 			.label_tc_s = "\xff\xff\xf0",
466 		},
467 		.default_mask = &rte_flow_item_mpls_mask,
468 		.mask_sz = sizeof(struct rte_flow_item_mpls),
469 		.convert = mlx5_flow_create_mpls,
470 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
471 		.dst_sz = sizeof(struct ibv_flow_spec_mpls),
472 #endif
473 	},
474 	[RTE_FLOW_ITEM_TYPE_VXLAN] = {
475 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH,
476 			       RTE_FLOW_ITEM_TYPE_IPV4, /* For L3 VXLAN. */
477 			       RTE_FLOW_ITEM_TYPE_IPV6), /* For L3 VXLAN. */
478 		.actions = valid_actions,
479 		.mask = &(const struct rte_flow_item_vxlan){
480 			.vni = "\xff\xff\xff",
481 		},
482 		.default_mask = &rte_flow_item_vxlan_mask,
483 		.mask_sz = sizeof(struct rte_flow_item_vxlan),
484 		.convert = mlx5_flow_create_vxlan,
485 		.dst_sz = sizeof(struct ibv_flow_spec_tunnel),
486 	},
487 	[RTE_FLOW_ITEM_TYPE_VXLAN_GPE] = {
488 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH,
489 			       RTE_FLOW_ITEM_TYPE_IPV4,
490 			       RTE_FLOW_ITEM_TYPE_IPV6),
491 		.actions = valid_actions,
492 		.mask = &(const struct rte_flow_item_vxlan_gpe){
493 			.vni = "\xff\xff\xff",
494 		},
495 		.default_mask = &rte_flow_item_vxlan_gpe_mask,
496 		.mask_sz = sizeof(struct rte_flow_item_vxlan_gpe),
497 		.convert = mlx5_flow_create_vxlan_gpe,
498 		.dst_sz = sizeof(struct ibv_flow_spec_tunnel),
499 	},
500 };
501 
502 /** Structure to pass to the conversion function. */
503 struct mlx5_flow_parse {
504 	uint32_t inner; /**< Verbs value, set once tunnel is encountered. */
505 	uint32_t create:1;
506 	/**< Whether resources should remain after a validate. */
507 	uint32_t drop:1; /**< Target is a drop queue. */
508 	uint32_t mark:1; /**< Mark is present in the flow. */
509 	uint32_t count:1; /**< Count is present in the flow. */
510 	uint32_t mark_id; /**< Mark identifier. */
511 	struct rte_flow_action_rss rss_conf; /**< RSS configuration */
512 	uint16_t queues[RTE_MAX_QUEUES_PER_PORT]; /**< Queues indexes to use. */
513 	uint8_t rss_key[40]; /**< copy of the RSS key. */
514 	enum hash_rxq_type layer; /**< Last pattern layer detected. */
515 	enum hash_rxq_type out_layer; /**< Last outer pattern layer detected. */
516 	uint32_t tunnel; /**< Tunnel type of RTE_PTYPE_TUNNEL_XXX. */
517 	struct ibv_counter_set *cs; /**< Holds the counter set for the rule */
518 	struct {
519 		struct ibv_flow_attr *ibv_attr;
520 		/**< Pointer to Verbs attributes. */
521 		unsigned int offset;
522 		/**< Current position or total size of the attribute. */
523 		uint64_t hash_fields; /**< Verbs hash fields. */
524 	} queue[RTE_DIM(hash_rxq_init)];
525 };
526 
527 static const struct rte_flow_ops mlx5_flow_ops = {
528 	.validate = mlx5_flow_validate,
529 	.create = mlx5_flow_create,
530 	.destroy = mlx5_flow_destroy,
531 	.flush = mlx5_flow_flush,
532 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
533 	.query = mlx5_flow_query,
534 #else
535 	.query = NULL,
536 #endif
537 	.isolate = mlx5_flow_isolate,
538 };
539 
540 /* Convert FDIR request to Generic flow. */
541 struct mlx5_fdir {
542 	struct rte_flow_attr attr;
543 	struct rte_flow_action actions[2];
544 	struct rte_flow_item items[4];
545 	struct rte_flow_item_eth l2;
546 	struct rte_flow_item_eth l2_mask;
547 	union {
548 		struct rte_flow_item_ipv4 ipv4;
549 		struct rte_flow_item_ipv6 ipv6;
550 	} l3;
551 	union {
552 		struct rte_flow_item_ipv4 ipv4;
553 		struct rte_flow_item_ipv6 ipv6;
554 	} l3_mask;
555 	union {
556 		struct rte_flow_item_udp udp;
557 		struct rte_flow_item_tcp tcp;
558 	} l4;
559 	union {
560 		struct rte_flow_item_udp udp;
561 		struct rte_flow_item_tcp tcp;
562 	} l4_mask;
563 	struct rte_flow_action_queue queue;
564 };
565 
566 /* Verbs specification header. */
567 struct ibv_spec_header {
568 	enum ibv_flow_spec_type type;
569 	uint16_t size;
570 };
571 
572 /**
573  * Check item is fully supported by the NIC matching capability.
574  *
575  * @param item[in]
576  *   Item specification.
577  * @param mask[in]
578  *   Bit-masks covering supported fields to compare with spec, last and mask in
579  *   \item.
580  * @param size
581  *   Bit-Mask size in bytes.
582  *
583  * @return
584  *   0 on success, a negative errno value otherwise and rte_errno is set.
585  */
586 static int
587 mlx5_flow_item_validate(const struct rte_flow_item *item,
588 			const uint8_t *mask, unsigned int size)
589 {
590 	unsigned int i;
591 	const uint8_t *spec = item->spec;
592 	const uint8_t *last = item->last;
593 	const uint8_t *m = item->mask ? item->mask : mask;
594 
595 	if (!spec && (item->mask || last))
596 		goto error;
597 	if (!spec)
598 		return 0;
599 	/*
600 	 * Single-pass check to make sure that:
601 	 * - item->mask is supported, no bits are set outside mask.
602 	 * - Both masked item->spec and item->last are equal (no range
603 	 *   supported).
604 	 */
605 	for (i = 0; i < size; i++) {
606 		if (!m[i])
607 			continue;
608 		if ((m[i] | mask[i]) != mask[i])
609 			goto error;
610 		if (last && ((spec[i] & m[i]) != (last[i] & m[i])))
611 			goto error;
612 	}
613 	return 0;
614 error:
615 	rte_errno = ENOTSUP;
616 	return -rte_errno;
617 }
618 
619 /**
620  * Extract attribute to the parser.
621  *
622  * @param[in] attr
623  *   Flow rule attributes.
624  * @param[out] error
625  *   Perform verbose error reporting if not NULL.
626  *
627  * @return
628  *   0 on success, a negative errno value otherwise and rte_errno is set.
629  */
630 static int
631 mlx5_flow_convert_attributes(const struct rte_flow_attr *attr,
632 			     struct rte_flow_error *error)
633 {
634 	if (attr->group) {
635 		rte_flow_error_set(error, ENOTSUP,
636 				   RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
637 				   NULL,
638 				   "groups are not supported");
639 		return -rte_errno;
640 	}
641 	if (attr->priority && attr->priority != MLX5_CTRL_FLOW_PRIORITY) {
642 		rte_flow_error_set(error, ENOTSUP,
643 				   RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
644 				   NULL,
645 				   "priorities are not supported");
646 		return -rte_errno;
647 	}
648 	if (attr->egress) {
649 		rte_flow_error_set(error, ENOTSUP,
650 				   RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
651 				   NULL,
652 				   "egress is not supported");
653 		return -rte_errno;
654 	}
655 	if (attr->transfer) {
656 		rte_flow_error_set(error, ENOTSUP,
657 				   RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
658 				   NULL,
659 				   "transfer is not supported");
660 		return -rte_errno;
661 	}
662 	if (!attr->ingress) {
663 		rte_flow_error_set(error, ENOTSUP,
664 				   RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
665 				   NULL,
666 				   "only ingress is supported");
667 		return -rte_errno;
668 	}
669 	return 0;
670 }
671 
672 /**
673  * Extract actions request to the parser.
674  *
675  * @param dev
676  *   Pointer to Ethernet device.
677  * @param[in] actions
678  *   Associated actions (list terminated by the END action).
679  * @param[out] error
680  *   Perform verbose error reporting if not NULL.
681  * @param[in, out] parser
682  *   Internal parser structure.
683  *
684  * @return
685  *   0 on success, a negative errno value otherwise and rte_errno is set.
686  */
687 static int
688 mlx5_flow_convert_actions(struct rte_eth_dev *dev,
689 			  const struct rte_flow_action actions[],
690 			  struct rte_flow_error *error,
691 			  struct mlx5_flow_parse *parser)
692 {
693 	enum { FATE = 1, MARK = 2, COUNT = 4, };
694 	uint32_t overlap = 0;
695 	struct priv *priv = dev->data->dev_private;
696 
697 	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; ++actions) {
698 		if (actions->type == RTE_FLOW_ACTION_TYPE_VOID) {
699 			continue;
700 		} else if (actions->type == RTE_FLOW_ACTION_TYPE_DROP) {
701 			if (overlap & FATE)
702 				goto exit_action_overlap;
703 			overlap |= FATE;
704 			parser->drop = 1;
705 		} else if (actions->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
706 			const struct rte_flow_action_queue *queue =
707 				(const struct rte_flow_action_queue *)
708 				actions->conf;
709 
710 			if (overlap & FATE)
711 				goto exit_action_overlap;
712 			overlap |= FATE;
713 			if (!queue || (queue->index > (priv->rxqs_n - 1)))
714 				goto exit_action_not_supported;
715 			parser->queues[0] = queue->index;
716 			parser->rss_conf = (struct rte_flow_action_rss){
717 				.queue_num = 1,
718 				.queue = parser->queues,
719 			};
720 		} else if (actions->type == RTE_FLOW_ACTION_TYPE_RSS) {
721 			const struct rte_flow_action_rss *rss =
722 				(const struct rte_flow_action_rss *)
723 				actions->conf;
724 			const uint8_t *rss_key;
725 			uint32_t rss_key_len;
726 			uint16_t n;
727 
728 			if (overlap & FATE)
729 				goto exit_action_overlap;
730 			overlap |= FATE;
731 			if (rss->func &&
732 			    rss->func != RTE_ETH_HASH_FUNCTION_TOEPLITZ) {
733 				rte_flow_error_set(error, EINVAL,
734 						   RTE_FLOW_ERROR_TYPE_ACTION,
735 						   actions,
736 						   "the only supported RSS hash"
737 						   " function is Toeplitz");
738 				return -rte_errno;
739 			}
740 #ifndef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
741 			if (parser->rss_conf.level > 1) {
742 				rte_flow_error_set(error, EINVAL,
743 						   RTE_FLOW_ERROR_TYPE_ACTION,
744 						   actions,
745 						   "a nonzero RSS encapsulation"
746 						   " level is not supported");
747 				return -rte_errno;
748 			}
749 #endif
750 			if (parser->rss_conf.level > 2) {
751 				rte_flow_error_set(error, EINVAL,
752 						   RTE_FLOW_ERROR_TYPE_ACTION,
753 						   actions,
754 						   "RSS encapsulation level"
755 						   " > 1 is not supported");
756 				return -rte_errno;
757 			}
758 			if (rss->types & MLX5_RSS_HF_MASK) {
759 				rte_flow_error_set(error, EINVAL,
760 						   RTE_FLOW_ERROR_TYPE_ACTION,
761 						   actions,
762 						   "unsupported RSS type"
763 						   " requested");
764 				return -rte_errno;
765 			}
766 			if (rss->key_len) {
767 				rss_key_len = rss->key_len;
768 				rss_key = rss->key;
769 			} else {
770 				rss_key_len = rss_hash_default_key_len;
771 				rss_key = rss_hash_default_key;
772 			}
773 			if (rss_key_len != RTE_DIM(parser->rss_key)) {
774 				rte_flow_error_set(error, EINVAL,
775 						   RTE_FLOW_ERROR_TYPE_ACTION,
776 						   actions,
777 						   "RSS hash key must be"
778 						   " exactly 40 bytes long");
779 				return -rte_errno;
780 			}
781 			if (!rss->queue_num) {
782 				rte_flow_error_set(error, EINVAL,
783 						   RTE_FLOW_ERROR_TYPE_ACTION,
784 						   actions,
785 						   "no valid queues");
786 				return -rte_errno;
787 			}
788 			if (rss->queue_num > RTE_DIM(parser->queues)) {
789 				rte_flow_error_set(error, EINVAL,
790 						   RTE_FLOW_ERROR_TYPE_ACTION,
791 						   actions,
792 						   "too many queues for RSS"
793 						   " context");
794 				return -rte_errno;
795 			}
796 			for (n = 0; n < rss->queue_num; ++n) {
797 				if (rss->queue[n] >= priv->rxqs_n) {
798 					rte_flow_error_set(error, EINVAL,
799 						   RTE_FLOW_ERROR_TYPE_ACTION,
800 						   actions,
801 						   "queue id > number of"
802 						   " queues");
803 					return -rte_errno;
804 				}
805 			}
806 			parser->rss_conf = (struct rte_flow_action_rss){
807 				.func = RTE_ETH_HASH_FUNCTION_DEFAULT,
808 				.level = rss->level ? rss->level : 1,
809 				.types = rss->types,
810 				.key_len = rss_key_len,
811 				.queue_num = rss->queue_num,
812 				.key = memcpy(parser->rss_key, rss_key,
813 					      sizeof(*rss_key) * rss_key_len),
814 				.queue = memcpy(parser->queues, rss->queue,
815 						sizeof(*rss->queue) *
816 						rss->queue_num),
817 			};
818 		} else if (actions->type == RTE_FLOW_ACTION_TYPE_MARK) {
819 			const struct rte_flow_action_mark *mark =
820 				(const struct rte_flow_action_mark *)
821 				actions->conf;
822 
823 			if (overlap & MARK)
824 				goto exit_action_overlap;
825 			overlap |= MARK;
826 			if (!mark) {
827 				rte_flow_error_set(error, EINVAL,
828 						   RTE_FLOW_ERROR_TYPE_ACTION,
829 						   actions,
830 						   "mark must be defined");
831 				return -rte_errno;
832 			} else if (mark->id >= MLX5_FLOW_MARK_MAX) {
833 				rte_flow_error_set(error, ENOTSUP,
834 						   RTE_FLOW_ERROR_TYPE_ACTION,
835 						   actions,
836 						   "mark must be between 0"
837 						   " and 16777199");
838 				return -rte_errno;
839 			}
840 			parser->mark = 1;
841 			parser->mark_id = mark->id;
842 		} else if (actions->type == RTE_FLOW_ACTION_TYPE_FLAG) {
843 			if (overlap & MARK)
844 				goto exit_action_overlap;
845 			overlap |= MARK;
846 			parser->mark = 1;
847 		} else if (actions->type == RTE_FLOW_ACTION_TYPE_COUNT &&
848 			   priv->config.flow_counter_en) {
849 			if (overlap & COUNT)
850 				goto exit_action_overlap;
851 			overlap |= COUNT;
852 			parser->count = 1;
853 		} else {
854 			goto exit_action_not_supported;
855 		}
856 	}
857 	/* When fate is unknown, drop traffic. */
858 	if (!(overlap & FATE))
859 		parser->drop = 1;
860 	if (parser->drop && parser->mark)
861 		parser->mark = 0;
862 	if (!parser->rss_conf.queue_num && !parser->drop) {
863 		rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
864 				   NULL, "no valid action");
865 		return -rte_errno;
866 	}
867 	return 0;
868 exit_action_not_supported:
869 	rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
870 			   actions, "action not supported");
871 	return -rte_errno;
872 exit_action_overlap:
873 	rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
874 			   actions, "overlapping actions are not supported");
875 	return -rte_errno;
876 }
877 
878 /**
879  * Validate items.
880  *
881  * @param[in] items
882  *   Pattern specification (list terminated by the END pattern item).
883  * @param[out] error
884  *   Perform verbose error reporting if not NULL.
885  * @param[in, out] parser
886  *   Internal parser structure.
887  *
888  * @return
889  *   0 on success, a negative errno value otherwise and rte_errno is set.
890  */
891 static int
892 mlx5_flow_convert_items_validate(struct rte_eth_dev *dev,
893 				 const struct rte_flow_item items[],
894 				 struct rte_flow_error *error,
895 				 struct mlx5_flow_parse *parser)
896 {
897 	struct priv *priv = dev->data->dev_private;
898 	const struct mlx5_flow_items *cur_item = mlx5_flow_items;
899 	unsigned int i;
900 	unsigned int last_voids = 0;
901 	int ret = 0;
902 
903 	/* Initialise the offsets to start after verbs attribute. */
904 	for (i = 0; i != hash_rxq_init_n; ++i)
905 		parser->queue[i].offset = sizeof(struct ibv_flow_attr);
906 	for (; items->type != RTE_FLOW_ITEM_TYPE_END; ++items) {
907 		const struct mlx5_flow_items *token = NULL;
908 		unsigned int n;
909 
910 		if (items->type == RTE_FLOW_ITEM_TYPE_VOID) {
911 			last_voids++;
912 			continue;
913 		}
914 		for (i = 0;
915 		     cur_item->items &&
916 		     cur_item->items[i] != RTE_FLOW_ITEM_TYPE_END;
917 		     ++i) {
918 			if (cur_item->items[i] == items->type) {
919 				token = &mlx5_flow_items[items->type];
920 				break;
921 			}
922 		}
923 		if (!token) {
924 			ret = -ENOTSUP;
925 			goto exit_item_not_supported;
926 		}
927 		cur_item = token;
928 		ret = mlx5_flow_item_validate(items,
929 					      (const uint8_t *)cur_item->mask,
930 					      cur_item->mask_sz);
931 		if (ret)
932 			goto exit_item_not_supported;
933 		if (IS_TUNNEL(items->type)) {
934 			if (parser->tunnel &&
935 			    !((items - last_voids - 1)->type ==
936 			      RTE_FLOW_ITEM_TYPE_GRE && items->type ==
937 			      RTE_FLOW_ITEM_TYPE_MPLS)) {
938 				rte_flow_error_set(error, ENOTSUP,
939 						   RTE_FLOW_ERROR_TYPE_ITEM,
940 						   items,
941 						   "Cannot recognize multiple"
942 						   " tunnel encapsulations.");
943 				return -rte_errno;
944 			}
945 			if (items->type == RTE_FLOW_ITEM_TYPE_MPLS &&
946 			    !priv->config.mpls_en) {
947 				rte_flow_error_set(error, ENOTSUP,
948 						   RTE_FLOW_ERROR_TYPE_ITEM,
949 						   items,
950 						   "MPLS not supported or"
951 						   " disabled in firmware"
952 						   " configuration.");
953 				return -rte_errno;
954 			}
955 			if (!priv->config.tunnel_en &&
956 			    parser->rss_conf.level > 1) {
957 				rte_flow_error_set(error, ENOTSUP,
958 					RTE_FLOW_ERROR_TYPE_ITEM,
959 					items,
960 					"RSS on tunnel is not supported");
961 				return -rte_errno;
962 			}
963 			parser->inner = IBV_FLOW_SPEC_INNER;
964 			parser->tunnel = flow_ptype[items->type];
965 		}
966 		if (parser->drop) {
967 			parser->queue[HASH_RXQ_ETH].offset += cur_item->dst_sz;
968 		} else {
969 			for (n = 0; n != hash_rxq_init_n; ++n)
970 				parser->queue[n].offset += cur_item->dst_sz;
971 		}
972 		last_voids = 0;
973 	}
974 	if (parser->drop) {
975 		parser->queue[HASH_RXQ_ETH].offset +=
976 			sizeof(struct ibv_flow_spec_action_drop);
977 	}
978 	if (parser->mark) {
979 		for (i = 0; i != hash_rxq_init_n; ++i)
980 			parser->queue[i].offset +=
981 				sizeof(struct ibv_flow_spec_action_tag);
982 	}
983 	if (parser->count) {
984 		unsigned int size = sizeof(struct ibv_flow_spec_counter_action);
985 
986 		for (i = 0; i != hash_rxq_init_n; ++i)
987 			parser->queue[i].offset += size;
988 	}
989 	return 0;
990 exit_item_not_supported:
991 	return rte_flow_error_set(error, -ret, RTE_FLOW_ERROR_TYPE_ITEM,
992 				  items, "item not supported");
993 }
994 
995 /**
996  * Allocate memory space to store verbs flow attributes.
997  *
998  * @param[in] size
999  *   Amount of byte to allocate.
1000  * @param[out] error
1001  *   Perform verbose error reporting if not NULL.
1002  *
1003  * @return
1004  *   A verbs flow attribute on success, NULL otherwise and rte_errno is set.
1005  */
1006 static struct ibv_flow_attr *
1007 mlx5_flow_convert_allocate(unsigned int size, struct rte_flow_error *error)
1008 {
1009 	struct ibv_flow_attr *ibv_attr;
1010 
1011 	ibv_attr = rte_calloc(__func__, 1, size, 0);
1012 	if (!ibv_attr) {
1013 		rte_flow_error_set(error, ENOMEM,
1014 				   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1015 				   NULL,
1016 				   "cannot allocate verbs spec attributes");
1017 		return NULL;
1018 	}
1019 	return ibv_attr;
1020 }
1021 
1022 /**
1023  * Make inner packet matching with an higher priority from the non Inner
1024  * matching.
1025  *
1026  * @param dev
1027  *   Pointer to Ethernet device.
1028  * @param[in, out] parser
1029  *   Internal parser structure.
1030  * @param attr
1031  *   User flow attribute.
1032  */
1033 static void
1034 mlx5_flow_update_priority(struct rte_eth_dev *dev,
1035 			  struct mlx5_flow_parse *parser,
1036 			  const struct rte_flow_attr *attr)
1037 {
1038 	struct priv *priv = dev->data->dev_private;
1039 	unsigned int i;
1040 	uint16_t priority;
1041 
1042 	/*			8 priorities	>= 16 priorities
1043 	 * Control flow:	4-7		8-15
1044 	 * User normal flow:	1-3		4-7
1045 	 * User tunnel flow:	0-2		0-3
1046 	 */
1047 	priority = attr->priority * MLX5_VERBS_FLOW_PRIO_8;
1048 	if (priv->config.max_verbs_prio == MLX5_VERBS_FLOW_PRIO_8)
1049 		priority /= 2;
1050 	/*
1051 	 * Lower non-tunnel flow Verbs priority 1 if only support 8 Verbs
1052 	 * priorities, lower 4 otherwise.
1053 	 */
1054 	if (!parser->inner) {
1055 		if (priv->config.max_verbs_prio == MLX5_VERBS_FLOW_PRIO_8)
1056 			priority += 1;
1057 		else
1058 			priority += MLX5_VERBS_FLOW_PRIO_8 / 2;
1059 	}
1060 	if (parser->drop) {
1061 		parser->queue[HASH_RXQ_ETH].ibv_attr->priority = priority +
1062 				hash_rxq_init[HASH_RXQ_ETH].flow_priority;
1063 		return;
1064 	}
1065 	for (i = 0; i != hash_rxq_init_n; ++i) {
1066 		if (!parser->queue[i].ibv_attr)
1067 			continue;
1068 		parser->queue[i].ibv_attr->priority = priority +
1069 				hash_rxq_init[i].flow_priority;
1070 	}
1071 }
1072 
1073 /**
1074  * Finalise verbs flow attributes.
1075  *
1076  * @param[in, out] parser
1077  *   Internal parser structure.
1078  */
1079 static void
1080 mlx5_flow_convert_finalise(struct mlx5_flow_parse *parser)
1081 {
1082 	unsigned int i;
1083 	uint32_t inner = parser->inner;
1084 
1085 	/* Don't create extra flows for outer RSS. */
1086 	if (parser->tunnel && parser->rss_conf.level < 2)
1087 		return;
1088 	/*
1089 	 * Fill missing layers in verbs specifications, or compute the correct
1090 	 * offset to allocate the memory space for the attributes and
1091 	 * specifications.
1092 	 */
1093 	for (i = 0; i != hash_rxq_init_n - 1; ++i) {
1094 		union {
1095 			struct ibv_flow_spec_ipv4_ext ipv4;
1096 			struct ibv_flow_spec_ipv6 ipv6;
1097 			struct ibv_flow_spec_tcp_udp udp_tcp;
1098 			struct ibv_flow_spec_eth eth;
1099 		} specs;
1100 		void *dst;
1101 		uint16_t size;
1102 
1103 		if (i == parser->layer)
1104 			continue;
1105 		if (parser->layer == HASH_RXQ_ETH ||
1106 		    parser->layer == HASH_RXQ_TUNNEL) {
1107 			if (hash_rxq_init[i].ip_version == MLX5_IPV4) {
1108 				size = sizeof(struct ibv_flow_spec_ipv4_ext);
1109 				specs.ipv4 = (struct ibv_flow_spec_ipv4_ext){
1110 					.type = inner | IBV_FLOW_SPEC_IPV4_EXT,
1111 					.size = size,
1112 				};
1113 			} else {
1114 				size = sizeof(struct ibv_flow_spec_ipv6);
1115 				specs.ipv6 = (struct ibv_flow_spec_ipv6){
1116 					.type = inner | IBV_FLOW_SPEC_IPV6,
1117 					.size = size,
1118 				};
1119 			}
1120 			if (parser->queue[i].ibv_attr) {
1121 				dst = (void *)((uintptr_t)
1122 					       parser->queue[i].ibv_attr +
1123 					       parser->queue[i].offset);
1124 				memcpy(dst, &specs, size);
1125 				++parser->queue[i].ibv_attr->num_of_specs;
1126 			}
1127 			parser->queue[i].offset += size;
1128 		}
1129 		if ((i == HASH_RXQ_UDPV4) || (i == HASH_RXQ_TCPV4) ||
1130 		    (i == HASH_RXQ_UDPV6) || (i == HASH_RXQ_TCPV6)) {
1131 			size = sizeof(struct ibv_flow_spec_tcp_udp);
1132 			specs.udp_tcp = (struct ibv_flow_spec_tcp_udp) {
1133 				.type = inner | ((i == HASH_RXQ_UDPV4 ||
1134 					  i == HASH_RXQ_UDPV6) ?
1135 					 IBV_FLOW_SPEC_UDP :
1136 					 IBV_FLOW_SPEC_TCP),
1137 				.size = size,
1138 			};
1139 			if (parser->queue[i].ibv_attr) {
1140 				dst = (void *)((uintptr_t)
1141 					       parser->queue[i].ibv_attr +
1142 					       parser->queue[i].offset);
1143 				memcpy(dst, &specs, size);
1144 				++parser->queue[i].ibv_attr->num_of_specs;
1145 			}
1146 			parser->queue[i].offset += size;
1147 		}
1148 	}
1149 }
1150 
1151 /**
1152  * Update flows according to pattern and RSS hash fields.
1153  *
1154  * @param[in, out] parser
1155  *   Internal parser structure.
1156  *
1157  * @return
1158  *   0 on success, a negative errno value otherwise and rte_errno is set.
1159  */
1160 static int
1161 mlx5_flow_convert_rss(struct mlx5_flow_parse *parser)
1162 {
1163 	unsigned int i;
1164 	enum hash_rxq_type start;
1165 	enum hash_rxq_type layer;
1166 	int outer = parser->tunnel && parser->rss_conf.level < 2;
1167 	uint64_t rss = parser->rss_conf.types;
1168 
1169 	layer = outer ? parser->out_layer : parser->layer;
1170 	if (layer == HASH_RXQ_TUNNEL)
1171 		layer = HASH_RXQ_ETH;
1172 	if (outer) {
1173 		/* Only one hash type for outer RSS. */
1174 		if (rss && layer == HASH_RXQ_ETH) {
1175 			start = HASH_RXQ_TCPV4;
1176 		} else if (rss && layer != HASH_RXQ_ETH &&
1177 			   !(rss & hash_rxq_init[layer].dpdk_rss_hf)) {
1178 			/* If RSS not match L4 pattern, try L3 RSS. */
1179 			if (layer < HASH_RXQ_IPV4)
1180 				layer = HASH_RXQ_IPV4;
1181 			else if (layer > HASH_RXQ_IPV4 && layer < HASH_RXQ_IPV6)
1182 				layer = HASH_RXQ_IPV6;
1183 			start = layer;
1184 		} else {
1185 			start = layer;
1186 		}
1187 		/* Scan first valid hash type. */
1188 		for (i = start; rss && i <= layer; ++i) {
1189 			if (!parser->queue[i].ibv_attr)
1190 				continue;
1191 			if (hash_rxq_init[i].dpdk_rss_hf & rss)
1192 				break;
1193 		}
1194 		if (rss && i <= layer)
1195 			parser->queue[layer].hash_fields =
1196 					hash_rxq_init[i].hash_fields;
1197 		/* Trim unused hash types. */
1198 		for (i = 0; i != hash_rxq_init_n; ++i) {
1199 			if (parser->queue[i].ibv_attr && i != layer) {
1200 				rte_free(parser->queue[i].ibv_attr);
1201 				parser->queue[i].ibv_attr = NULL;
1202 			}
1203 		}
1204 	} else {
1205 		/* Expand for inner or normal RSS. */
1206 		if (rss && (layer == HASH_RXQ_ETH || layer == HASH_RXQ_IPV4))
1207 			start = HASH_RXQ_TCPV4;
1208 		else if (rss && layer == HASH_RXQ_IPV6)
1209 			start = HASH_RXQ_TCPV6;
1210 		else
1211 			start = layer;
1212 		/* For L4 pattern, try L3 RSS if no L4 RSS. */
1213 		/* Trim unused hash types. */
1214 		for (i = 0; i != hash_rxq_init_n; ++i) {
1215 			if (!parser->queue[i].ibv_attr)
1216 				continue;
1217 			if (i < start || i > layer) {
1218 				rte_free(parser->queue[i].ibv_attr);
1219 				parser->queue[i].ibv_attr = NULL;
1220 				continue;
1221 			}
1222 			if (!rss)
1223 				continue;
1224 			if (hash_rxq_init[i].dpdk_rss_hf & rss) {
1225 				parser->queue[i].hash_fields =
1226 						hash_rxq_init[i].hash_fields;
1227 			} else if (i != layer) {
1228 				/* Remove unused RSS expansion. */
1229 				rte_free(parser->queue[i].ibv_attr);
1230 				parser->queue[i].ibv_attr = NULL;
1231 			} else if (layer < HASH_RXQ_IPV4 &&
1232 				   (hash_rxq_init[HASH_RXQ_IPV4].dpdk_rss_hf &
1233 				    rss)) {
1234 				/* Allow IPv4 RSS on L4 pattern. */
1235 				parser->queue[i].hash_fields =
1236 					hash_rxq_init[HASH_RXQ_IPV4]
1237 						.hash_fields;
1238 			} else if (i > HASH_RXQ_IPV4 && i < HASH_RXQ_IPV6 &&
1239 				   (hash_rxq_init[HASH_RXQ_IPV6].dpdk_rss_hf &
1240 				    rss)) {
1241 				/* Allow IPv4 RSS on L4 pattern. */
1242 				parser->queue[i].hash_fields =
1243 					hash_rxq_init[HASH_RXQ_IPV6]
1244 						.hash_fields;
1245 			}
1246 		}
1247 	}
1248 	return 0;
1249 }
1250 
1251 /**
1252  * Validate and convert a flow supported by the NIC.
1253  *
1254  * @param dev
1255  *   Pointer to Ethernet device.
1256  * @param[in] attr
1257  *   Flow rule attributes.
1258  * @param[in] pattern
1259  *   Pattern specification (list terminated by the END pattern item).
1260  * @param[in] actions
1261  *   Associated actions (list terminated by the END action).
1262  * @param[out] error
1263  *   Perform verbose error reporting if not NULL.
1264  * @param[in, out] parser
1265  *   Internal parser structure.
1266  *
1267  * @return
1268  *   0 on success, a negative errno value otherwise and rte_errno is set.
1269  */
1270 static int
1271 mlx5_flow_convert(struct rte_eth_dev *dev,
1272 		  const struct rte_flow_attr *attr,
1273 		  const struct rte_flow_item items[],
1274 		  const struct rte_flow_action actions[],
1275 		  struct rte_flow_error *error,
1276 		  struct mlx5_flow_parse *parser)
1277 {
1278 	const struct mlx5_flow_items *cur_item = mlx5_flow_items;
1279 	unsigned int i;
1280 	int ret;
1281 
1282 	/* First step. Validate the attributes, items and actions. */
1283 	*parser = (struct mlx5_flow_parse){
1284 		.create = parser->create,
1285 		.layer = HASH_RXQ_ETH,
1286 		.mark_id = MLX5_FLOW_MARK_DEFAULT,
1287 	};
1288 	ret = mlx5_flow_convert_attributes(attr, error);
1289 	if (ret)
1290 		return ret;
1291 	ret = mlx5_flow_convert_actions(dev, actions, error, parser);
1292 	if (ret)
1293 		return ret;
1294 	ret = mlx5_flow_convert_items_validate(dev, items, error, parser);
1295 	if (ret)
1296 		return ret;
1297 	mlx5_flow_convert_finalise(parser);
1298 	/*
1299 	 * Second step.
1300 	 * Allocate the memory space to store verbs specifications.
1301 	 */
1302 	if (parser->drop) {
1303 		unsigned int offset = parser->queue[HASH_RXQ_ETH].offset;
1304 
1305 		parser->queue[HASH_RXQ_ETH].ibv_attr =
1306 			mlx5_flow_convert_allocate(offset, error);
1307 		if (!parser->queue[HASH_RXQ_ETH].ibv_attr)
1308 			goto exit_enomem;
1309 		parser->queue[HASH_RXQ_ETH].offset =
1310 			sizeof(struct ibv_flow_attr);
1311 	} else {
1312 		for (i = 0; i != hash_rxq_init_n; ++i) {
1313 			unsigned int offset;
1314 
1315 			offset = parser->queue[i].offset;
1316 			parser->queue[i].ibv_attr =
1317 				mlx5_flow_convert_allocate(offset, error);
1318 			if (!parser->queue[i].ibv_attr)
1319 				goto exit_enomem;
1320 			parser->queue[i].offset = sizeof(struct ibv_flow_attr);
1321 		}
1322 	}
1323 	/* Third step. Conversion parse, fill the specifications. */
1324 	parser->inner = 0;
1325 	parser->tunnel = 0;
1326 	parser->layer = HASH_RXQ_ETH;
1327 	for (; items->type != RTE_FLOW_ITEM_TYPE_END; ++items) {
1328 		struct mlx5_flow_data data = {
1329 			.dev = dev,
1330 			.parser = parser,
1331 			.error = error,
1332 		};
1333 
1334 		if (items->type == RTE_FLOW_ITEM_TYPE_VOID)
1335 			continue;
1336 		cur_item = &mlx5_flow_items[items->type];
1337 		ret = cur_item->convert(items,
1338 					(cur_item->default_mask ?
1339 					 cur_item->default_mask :
1340 					 cur_item->mask),
1341 					 &data);
1342 		if (ret)
1343 			goto exit_free;
1344 	}
1345 	if (!parser->drop) {
1346 		/* RSS check, remove unused hash types. */
1347 		ret = mlx5_flow_convert_rss(parser);
1348 		if (ret)
1349 			goto exit_free;
1350 		/* Complete missing specification. */
1351 		mlx5_flow_convert_finalise(parser);
1352 	}
1353 	mlx5_flow_update_priority(dev, parser, attr);
1354 	if (parser->mark)
1355 		mlx5_flow_create_flag_mark(parser, parser->mark_id);
1356 	if (parser->count && parser->create) {
1357 		mlx5_flow_create_count(dev, parser);
1358 		if (!parser->cs)
1359 			goto exit_count_error;
1360 	}
1361 exit_free:
1362 	/* Only verification is expected, all resources should be released. */
1363 	if (!parser->create) {
1364 		for (i = 0; i != hash_rxq_init_n; ++i) {
1365 			if (parser->queue[i].ibv_attr) {
1366 				rte_free(parser->queue[i].ibv_attr);
1367 				parser->queue[i].ibv_attr = NULL;
1368 			}
1369 		}
1370 	}
1371 	return ret;
1372 exit_enomem:
1373 	for (i = 0; i != hash_rxq_init_n; ++i) {
1374 		if (parser->queue[i].ibv_attr) {
1375 			rte_free(parser->queue[i].ibv_attr);
1376 			parser->queue[i].ibv_attr = NULL;
1377 		}
1378 	}
1379 	rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1380 			   NULL, "cannot allocate verbs spec attributes");
1381 	return -rte_errno;
1382 exit_count_error:
1383 	rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1384 			   NULL, "cannot create counter");
1385 	return -rte_errno;
1386 }
1387 
1388 /**
1389  * Copy the specification created into the flow.
1390  *
1391  * @param parser
1392  *   Internal parser structure.
1393  * @param src
1394  *   Create specification.
1395  * @param size
1396  *   Size in bytes of the specification to copy.
1397  */
1398 static void
1399 mlx5_flow_create_copy(struct mlx5_flow_parse *parser, void *src,
1400 		      unsigned int size)
1401 {
1402 	unsigned int i;
1403 	void *dst;
1404 
1405 	for (i = 0; i != hash_rxq_init_n; ++i) {
1406 		if (!parser->queue[i].ibv_attr)
1407 			continue;
1408 		dst = (void *)((uintptr_t)parser->queue[i].ibv_attr +
1409 				parser->queue[i].offset);
1410 		memcpy(dst, src, size);
1411 		++parser->queue[i].ibv_attr->num_of_specs;
1412 		parser->queue[i].offset += size;
1413 	}
1414 }
1415 
1416 /**
1417  * Convert Ethernet item to Verbs specification.
1418  *
1419  * @param item[in]
1420  *   Item specification.
1421  * @param default_mask[in]
1422  *   Default bit-masks to use when item->mask is not provided.
1423  * @param data[in, out]
1424  *   User structure.
1425  *
1426  * @return
1427  *   0 on success, a negative errno value otherwise and rte_errno is set.
1428  */
1429 static int
1430 mlx5_flow_create_eth(const struct rte_flow_item *item,
1431 		     const void *default_mask,
1432 		     struct mlx5_flow_data *data)
1433 {
1434 	const struct rte_flow_item_eth *spec = item->spec;
1435 	const struct rte_flow_item_eth *mask = item->mask;
1436 	struct mlx5_flow_parse *parser = data->parser;
1437 	const unsigned int eth_size = sizeof(struct ibv_flow_spec_eth);
1438 	struct ibv_flow_spec_eth eth = {
1439 		.type = parser->inner | IBV_FLOW_SPEC_ETH,
1440 		.size = eth_size,
1441 	};
1442 
1443 	parser->layer = HASH_RXQ_ETH;
1444 	if (spec) {
1445 		unsigned int i;
1446 
1447 		if (!mask)
1448 			mask = default_mask;
1449 		memcpy(&eth.val.dst_mac, spec->dst.addr_bytes, ETHER_ADDR_LEN);
1450 		memcpy(&eth.val.src_mac, spec->src.addr_bytes, ETHER_ADDR_LEN);
1451 		eth.val.ether_type = spec->type;
1452 		memcpy(&eth.mask.dst_mac, mask->dst.addr_bytes, ETHER_ADDR_LEN);
1453 		memcpy(&eth.mask.src_mac, mask->src.addr_bytes, ETHER_ADDR_LEN);
1454 		eth.mask.ether_type = mask->type;
1455 		/* Remove unwanted bits from values. */
1456 		for (i = 0; i < ETHER_ADDR_LEN; ++i) {
1457 			eth.val.dst_mac[i] &= eth.mask.dst_mac[i];
1458 			eth.val.src_mac[i] &= eth.mask.src_mac[i];
1459 		}
1460 		eth.val.ether_type &= eth.mask.ether_type;
1461 	}
1462 	mlx5_flow_create_copy(parser, &eth, eth_size);
1463 	return 0;
1464 }
1465 
1466 /**
1467  * Convert VLAN item to Verbs specification.
1468  *
1469  * @param item[in]
1470  *   Item specification.
1471  * @param default_mask[in]
1472  *   Default bit-masks to use when item->mask is not provided.
1473  * @param data[in, out]
1474  *   User structure.
1475  *
1476  * @return
1477  *   0 on success, a negative errno value otherwise and rte_errno is set.
1478  */
1479 static int
1480 mlx5_flow_create_vlan(const struct rte_flow_item *item,
1481 		      const void *default_mask,
1482 		      struct mlx5_flow_data *data)
1483 {
1484 	const struct rte_flow_item_vlan *spec = item->spec;
1485 	const struct rte_flow_item_vlan *mask = item->mask;
1486 	struct mlx5_flow_parse *parser = data->parser;
1487 	struct ibv_flow_spec_eth *eth;
1488 	const unsigned int eth_size = sizeof(struct ibv_flow_spec_eth);
1489 	const char *msg = "VLAN cannot be empty";
1490 
1491 	if (spec) {
1492 		unsigned int i;
1493 		if (!mask)
1494 			mask = default_mask;
1495 
1496 		for (i = 0; i != hash_rxq_init_n; ++i) {
1497 			if (!parser->queue[i].ibv_attr)
1498 				continue;
1499 
1500 			eth = (void *)((uintptr_t)parser->queue[i].ibv_attr +
1501 				       parser->queue[i].offset - eth_size);
1502 			eth->val.vlan_tag = spec->tci;
1503 			eth->mask.vlan_tag = mask->tci;
1504 			eth->val.vlan_tag &= eth->mask.vlan_tag;
1505 			/*
1506 			 * From verbs perspective an empty VLAN is equivalent
1507 			 * to a packet without VLAN layer.
1508 			 */
1509 			if (!eth->mask.vlan_tag)
1510 				goto error;
1511 			/* Outer TPID cannot be matched. */
1512 			if (eth->mask.ether_type) {
1513 				msg = "VLAN TPID matching is not supported";
1514 				goto error;
1515 			}
1516 			eth->val.ether_type = spec->inner_type;
1517 			eth->mask.ether_type = mask->inner_type;
1518 			eth->val.ether_type &= eth->mask.ether_type;
1519 		}
1520 		return 0;
1521 	}
1522 error:
1523 	return rte_flow_error_set(data->error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
1524 				  item, msg);
1525 }
1526 
1527 /**
1528  * Convert IPv4 item to Verbs specification.
1529  *
1530  * @param item[in]
1531  *   Item specification.
1532  * @param default_mask[in]
1533  *   Default bit-masks to use when item->mask is not provided.
1534  * @param data[in, out]
1535  *   User structure.
1536  *
1537  * @return
1538  *   0 on success, a negative errno value otherwise and rte_errno is set.
1539  */
1540 static int
1541 mlx5_flow_create_ipv4(const struct rte_flow_item *item,
1542 		      const void *default_mask,
1543 		      struct mlx5_flow_data *data)
1544 {
1545 	struct priv *priv = data->dev->data->dev_private;
1546 	const struct rte_flow_item_ipv4 *spec = item->spec;
1547 	const struct rte_flow_item_ipv4 *mask = item->mask;
1548 	struct mlx5_flow_parse *parser = data->parser;
1549 	unsigned int ipv4_size = sizeof(struct ibv_flow_spec_ipv4_ext);
1550 	struct ibv_flow_spec_ipv4_ext ipv4 = {
1551 		.type = parser->inner | IBV_FLOW_SPEC_IPV4_EXT,
1552 		.size = ipv4_size,
1553 	};
1554 
1555 	if (parser->layer == HASH_RXQ_TUNNEL &&
1556 	    parser->tunnel == ptype_ext[PTYPE_IDX(RTE_PTYPE_TUNNEL_VXLAN)] &&
1557 	    !priv->config.l3_vxlan_en)
1558 		return rte_flow_error_set(data->error, EINVAL,
1559 					  RTE_FLOW_ERROR_TYPE_ITEM,
1560 					  item,
1561 					  "L3 VXLAN not enabled by device"
1562 					  " parameter and/or not configured"
1563 					  " in firmware");
1564 	parser->layer = HASH_RXQ_IPV4;
1565 	if (spec) {
1566 		if (!mask)
1567 			mask = default_mask;
1568 		ipv4.val = (struct ibv_flow_ipv4_ext_filter){
1569 			.src_ip = spec->hdr.src_addr,
1570 			.dst_ip = spec->hdr.dst_addr,
1571 			.proto = spec->hdr.next_proto_id,
1572 			.tos = spec->hdr.type_of_service,
1573 		};
1574 		ipv4.mask = (struct ibv_flow_ipv4_ext_filter){
1575 			.src_ip = mask->hdr.src_addr,
1576 			.dst_ip = mask->hdr.dst_addr,
1577 			.proto = mask->hdr.next_proto_id,
1578 			.tos = mask->hdr.type_of_service,
1579 		};
1580 		/* Remove unwanted bits from values. */
1581 		ipv4.val.src_ip &= ipv4.mask.src_ip;
1582 		ipv4.val.dst_ip &= ipv4.mask.dst_ip;
1583 		ipv4.val.proto &= ipv4.mask.proto;
1584 		ipv4.val.tos &= ipv4.mask.tos;
1585 	}
1586 	mlx5_flow_create_copy(parser, &ipv4, ipv4_size);
1587 	return 0;
1588 }
1589 
1590 /**
1591  * Convert IPv6 item to Verbs specification.
1592  *
1593  * @param item[in]
1594  *   Item specification.
1595  * @param default_mask[in]
1596  *   Default bit-masks to use when item->mask is not provided.
1597  * @param data[in, out]
1598  *   User structure.
1599  *
1600  * @return
1601  *   0 on success, a negative errno value otherwise and rte_errno is set.
1602  */
1603 static int
1604 mlx5_flow_create_ipv6(const struct rte_flow_item *item,
1605 		      const void *default_mask,
1606 		      struct mlx5_flow_data *data)
1607 {
1608 	struct priv *priv = data->dev->data->dev_private;
1609 	const struct rte_flow_item_ipv6 *spec = item->spec;
1610 	const struct rte_flow_item_ipv6 *mask = item->mask;
1611 	struct mlx5_flow_parse *parser = data->parser;
1612 	unsigned int ipv6_size = sizeof(struct ibv_flow_spec_ipv6);
1613 	struct ibv_flow_spec_ipv6 ipv6 = {
1614 		.type = parser->inner | IBV_FLOW_SPEC_IPV6,
1615 		.size = ipv6_size,
1616 	};
1617 
1618 	if (parser->layer == HASH_RXQ_TUNNEL &&
1619 	    parser->tunnel == ptype_ext[PTYPE_IDX(RTE_PTYPE_TUNNEL_VXLAN)] &&
1620 	    !priv->config.l3_vxlan_en)
1621 		return rte_flow_error_set(data->error, EINVAL,
1622 					  RTE_FLOW_ERROR_TYPE_ITEM,
1623 					  item,
1624 					  "L3 VXLAN not enabled by device"
1625 					  " parameter and/or not configured"
1626 					  " in firmware");
1627 	parser->layer = HASH_RXQ_IPV6;
1628 	if (spec) {
1629 		unsigned int i;
1630 		uint32_t vtc_flow_val;
1631 		uint32_t vtc_flow_mask;
1632 
1633 		if (!mask)
1634 			mask = default_mask;
1635 		memcpy(&ipv6.val.src_ip, spec->hdr.src_addr,
1636 		       RTE_DIM(ipv6.val.src_ip));
1637 		memcpy(&ipv6.val.dst_ip, spec->hdr.dst_addr,
1638 		       RTE_DIM(ipv6.val.dst_ip));
1639 		memcpy(&ipv6.mask.src_ip, mask->hdr.src_addr,
1640 		       RTE_DIM(ipv6.mask.src_ip));
1641 		memcpy(&ipv6.mask.dst_ip, mask->hdr.dst_addr,
1642 		       RTE_DIM(ipv6.mask.dst_ip));
1643 		vtc_flow_val = rte_be_to_cpu_32(spec->hdr.vtc_flow);
1644 		vtc_flow_mask = rte_be_to_cpu_32(mask->hdr.vtc_flow);
1645 		ipv6.val.flow_label =
1646 			rte_cpu_to_be_32((vtc_flow_val & IPV6_HDR_FL_MASK) >>
1647 					 IPV6_HDR_FL_SHIFT);
1648 		ipv6.val.traffic_class = (vtc_flow_val & IPV6_HDR_TC_MASK) >>
1649 					 IPV6_HDR_TC_SHIFT;
1650 		ipv6.val.next_hdr = spec->hdr.proto;
1651 		ipv6.val.hop_limit = spec->hdr.hop_limits;
1652 		ipv6.mask.flow_label =
1653 			rte_cpu_to_be_32((vtc_flow_mask & IPV6_HDR_FL_MASK) >>
1654 					 IPV6_HDR_FL_SHIFT);
1655 		ipv6.mask.traffic_class = (vtc_flow_mask & IPV6_HDR_TC_MASK) >>
1656 					  IPV6_HDR_TC_SHIFT;
1657 		ipv6.mask.next_hdr = mask->hdr.proto;
1658 		ipv6.mask.hop_limit = mask->hdr.hop_limits;
1659 		/* Remove unwanted bits from values. */
1660 		for (i = 0; i < RTE_DIM(ipv6.val.src_ip); ++i) {
1661 			ipv6.val.src_ip[i] &= ipv6.mask.src_ip[i];
1662 			ipv6.val.dst_ip[i] &= ipv6.mask.dst_ip[i];
1663 		}
1664 		ipv6.val.flow_label &= ipv6.mask.flow_label;
1665 		ipv6.val.traffic_class &= ipv6.mask.traffic_class;
1666 		ipv6.val.next_hdr &= ipv6.mask.next_hdr;
1667 		ipv6.val.hop_limit &= ipv6.mask.hop_limit;
1668 	}
1669 	mlx5_flow_create_copy(parser, &ipv6, ipv6_size);
1670 	return 0;
1671 }
1672 
1673 /**
1674  * Convert UDP item to Verbs specification.
1675  *
1676  * @param item[in]
1677  *   Item specification.
1678  * @param default_mask[in]
1679  *   Default bit-masks to use when item->mask is not provided.
1680  * @param data[in, out]
1681  *   User structure.
1682  *
1683  * @return
1684  *   0 on success, a negative errno value otherwise and rte_errno is set.
1685  */
1686 static int
1687 mlx5_flow_create_udp(const struct rte_flow_item *item,
1688 		     const void *default_mask,
1689 		     struct mlx5_flow_data *data)
1690 {
1691 	const struct rte_flow_item_udp *spec = item->spec;
1692 	const struct rte_flow_item_udp *mask = item->mask;
1693 	struct mlx5_flow_parse *parser = data->parser;
1694 	unsigned int udp_size = sizeof(struct ibv_flow_spec_tcp_udp);
1695 	struct ibv_flow_spec_tcp_udp udp = {
1696 		.type = parser->inner | IBV_FLOW_SPEC_UDP,
1697 		.size = udp_size,
1698 	};
1699 
1700 	if (parser->layer == HASH_RXQ_IPV4)
1701 		parser->layer = HASH_RXQ_UDPV4;
1702 	else
1703 		parser->layer = HASH_RXQ_UDPV6;
1704 	if (spec) {
1705 		if (!mask)
1706 			mask = default_mask;
1707 		udp.val.dst_port = spec->hdr.dst_port;
1708 		udp.val.src_port = spec->hdr.src_port;
1709 		udp.mask.dst_port = mask->hdr.dst_port;
1710 		udp.mask.src_port = mask->hdr.src_port;
1711 		/* Remove unwanted bits from values. */
1712 		udp.val.src_port &= udp.mask.src_port;
1713 		udp.val.dst_port &= udp.mask.dst_port;
1714 	}
1715 	mlx5_flow_create_copy(parser, &udp, udp_size);
1716 	return 0;
1717 }
1718 
1719 /**
1720  * Convert TCP item to Verbs specification.
1721  *
1722  * @param item[in]
1723  *   Item specification.
1724  * @param default_mask[in]
1725  *   Default bit-masks to use when item->mask is not provided.
1726  * @param data[in, out]
1727  *   User structure.
1728  *
1729  * @return
1730  *   0 on success, a negative errno value otherwise and rte_errno is set.
1731  */
1732 static int
1733 mlx5_flow_create_tcp(const struct rte_flow_item *item,
1734 		     const void *default_mask,
1735 		     struct mlx5_flow_data *data)
1736 {
1737 	const struct rte_flow_item_tcp *spec = item->spec;
1738 	const struct rte_flow_item_tcp *mask = item->mask;
1739 	struct mlx5_flow_parse *parser = data->parser;
1740 	unsigned int tcp_size = sizeof(struct ibv_flow_spec_tcp_udp);
1741 	struct ibv_flow_spec_tcp_udp tcp = {
1742 		.type = parser->inner | IBV_FLOW_SPEC_TCP,
1743 		.size = tcp_size,
1744 	};
1745 
1746 	if (parser->layer == HASH_RXQ_IPV4)
1747 		parser->layer = HASH_RXQ_TCPV4;
1748 	else
1749 		parser->layer = HASH_RXQ_TCPV6;
1750 	if (spec) {
1751 		if (!mask)
1752 			mask = default_mask;
1753 		tcp.val.dst_port = spec->hdr.dst_port;
1754 		tcp.val.src_port = spec->hdr.src_port;
1755 		tcp.mask.dst_port = mask->hdr.dst_port;
1756 		tcp.mask.src_port = mask->hdr.src_port;
1757 		/* Remove unwanted bits from values. */
1758 		tcp.val.src_port &= tcp.mask.src_port;
1759 		tcp.val.dst_port &= tcp.mask.dst_port;
1760 	}
1761 	mlx5_flow_create_copy(parser, &tcp, tcp_size);
1762 	return 0;
1763 }
1764 
1765 /**
1766  * Convert VXLAN item to Verbs specification.
1767  *
1768  * @param item[in]
1769  *   Item specification.
1770  * @param default_mask[in]
1771  *   Default bit-masks to use when item->mask is not provided.
1772  * @param data[in, out]
1773  *   User structure.
1774  *
1775  * @return
1776  *   0 on success, a negative errno value otherwise and rte_errno is set.
1777  */
1778 static int
1779 mlx5_flow_create_vxlan(const struct rte_flow_item *item,
1780 		       const void *default_mask,
1781 		       struct mlx5_flow_data *data)
1782 {
1783 	const struct rte_flow_item_vxlan *spec = item->spec;
1784 	const struct rte_flow_item_vxlan *mask = item->mask;
1785 	struct mlx5_flow_parse *parser = data->parser;
1786 	unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
1787 	struct ibv_flow_spec_tunnel vxlan = {
1788 		.type = parser->inner | IBV_FLOW_SPEC_VXLAN_TUNNEL,
1789 		.size = size,
1790 	};
1791 	union vni {
1792 		uint32_t vlan_id;
1793 		uint8_t vni[4];
1794 	} id;
1795 
1796 	id.vni[0] = 0;
1797 	parser->inner = IBV_FLOW_SPEC_INNER;
1798 	parser->tunnel = ptype_ext[PTYPE_IDX(RTE_PTYPE_TUNNEL_VXLAN)];
1799 	parser->out_layer = parser->layer;
1800 	parser->layer = HASH_RXQ_TUNNEL;
1801 	if (spec) {
1802 		if (!mask)
1803 			mask = default_mask;
1804 		memcpy(&id.vni[1], spec->vni, 3);
1805 		vxlan.val.tunnel_id = id.vlan_id;
1806 		memcpy(&id.vni[1], mask->vni, 3);
1807 		vxlan.mask.tunnel_id = id.vlan_id;
1808 		/* Remove unwanted bits from values. */
1809 		vxlan.val.tunnel_id &= vxlan.mask.tunnel_id;
1810 	}
1811 	/*
1812 	 * Tunnel id 0 is equivalent as not adding a VXLAN layer, if only this
1813 	 * layer is defined in the Verbs specification it is interpreted as
1814 	 * wildcard and all packets will match this rule, if it follows a full
1815 	 * stack layer (ex: eth / ipv4 / udp), all packets matching the layers
1816 	 * before will also match this rule.
1817 	 * To avoid such situation, VNI 0 is currently refused.
1818 	 */
1819 	/* Only allow tunnel w/o tunnel id pattern after proper outer spec. */
1820 	if (parser->out_layer == HASH_RXQ_ETH && !vxlan.val.tunnel_id)
1821 		return rte_flow_error_set(data->error, EINVAL,
1822 					  RTE_FLOW_ERROR_TYPE_ITEM,
1823 					  item,
1824 					  "VxLAN vni cannot be 0");
1825 	mlx5_flow_create_copy(parser, &vxlan, size);
1826 	return 0;
1827 }
1828 
1829 /**
1830  * Convert VXLAN-GPE item to Verbs specification.
1831  *
1832  * @param item[in]
1833  *   Item specification.
1834  * @param default_mask[in]
1835  *   Default bit-masks to use when item->mask is not provided.
1836  * @param data[in, out]
1837  *   User structure.
1838  *
1839  * @return
1840  *   0 on success, a negative errno value otherwise and rte_errno is set.
1841  */
1842 static int
1843 mlx5_flow_create_vxlan_gpe(const struct rte_flow_item *item,
1844 			   const void *default_mask,
1845 			   struct mlx5_flow_data *data)
1846 {
1847 	struct priv *priv = data->dev->data->dev_private;
1848 	const struct rte_flow_item_vxlan_gpe *spec = item->spec;
1849 	const struct rte_flow_item_vxlan_gpe *mask = item->mask;
1850 	struct mlx5_flow_parse *parser = data->parser;
1851 	unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
1852 	struct ibv_flow_spec_tunnel vxlan = {
1853 		.type = parser->inner | IBV_FLOW_SPEC_VXLAN_TUNNEL,
1854 		.size = size,
1855 	};
1856 	union vni {
1857 		uint32_t vlan_id;
1858 		uint8_t vni[4];
1859 	} id;
1860 
1861 	if (!priv->config.l3_vxlan_en)
1862 		return rte_flow_error_set(data->error, EINVAL,
1863 					  RTE_FLOW_ERROR_TYPE_ITEM,
1864 					  item,
1865 					  "L3 VXLAN not enabled by device"
1866 					  " parameter and/or not configured"
1867 					  " in firmware");
1868 	id.vni[0] = 0;
1869 	parser->inner = IBV_FLOW_SPEC_INNER;
1870 	parser->tunnel = ptype_ext[PTYPE_IDX(RTE_PTYPE_TUNNEL_VXLAN_GPE)];
1871 	parser->out_layer = parser->layer;
1872 	parser->layer = HASH_RXQ_TUNNEL;
1873 	if (spec) {
1874 		if (!mask)
1875 			mask = default_mask;
1876 		memcpy(&id.vni[1], spec->vni, 3);
1877 		vxlan.val.tunnel_id = id.vlan_id;
1878 		memcpy(&id.vni[1], mask->vni, 3);
1879 		vxlan.mask.tunnel_id = id.vlan_id;
1880 		if (spec->protocol)
1881 			return rte_flow_error_set(data->error, EINVAL,
1882 						  RTE_FLOW_ERROR_TYPE_ITEM,
1883 						  item,
1884 						  "VxLAN-GPE protocol not"
1885 						  " supported");
1886 		/* Remove unwanted bits from values. */
1887 		vxlan.val.tunnel_id &= vxlan.mask.tunnel_id;
1888 	}
1889 	/*
1890 	 * Tunnel id 0 is equivalent as not adding a VXLAN layer, if only this
1891 	 * layer is defined in the Verbs specification it is interpreted as
1892 	 * wildcard and all packets will match this rule, if it follows a full
1893 	 * stack layer (ex: eth / ipv4 / udp), all packets matching the layers
1894 	 * before will also match this rule.
1895 	 * To avoid such situation, VNI 0 is currently refused.
1896 	 */
1897 	/* Only allow tunnel w/o tunnel id pattern after proper outer spec. */
1898 	if (parser->out_layer == HASH_RXQ_ETH && !vxlan.val.tunnel_id)
1899 		return rte_flow_error_set(data->error, EINVAL,
1900 					  RTE_FLOW_ERROR_TYPE_ITEM,
1901 					  item,
1902 					  "VxLAN-GPE vni cannot be 0");
1903 	mlx5_flow_create_copy(parser, &vxlan, size);
1904 	return 0;
1905 }
1906 
1907 /**
1908  * Convert GRE item to Verbs specification.
1909  *
1910  * @param item[in]
1911  *   Item specification.
1912  * @param default_mask[in]
1913  *   Default bit-masks to use when item->mask is not provided.
1914  * @param data[in, out]
1915  *   User structure.
1916  *
1917  * @return
1918  *   0 on success, a negative errno value otherwise and rte_errno is set.
1919  */
1920 static int
1921 mlx5_flow_create_gre(const struct rte_flow_item *item,
1922 		     const void *default_mask,
1923 		     struct mlx5_flow_data *data)
1924 {
1925 	struct mlx5_flow_parse *parser = data->parser;
1926 #ifndef HAVE_IBV_DEVICE_MPLS_SUPPORT
1927 	(void)default_mask;
1928 	unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
1929 	struct ibv_flow_spec_tunnel tunnel = {
1930 		.type = parser->inner | IBV_FLOW_SPEC_VXLAN_TUNNEL,
1931 		.size = size,
1932 	};
1933 #else
1934 	const struct rte_flow_item_gre *spec = item->spec;
1935 	const struct rte_flow_item_gre *mask = item->mask;
1936 	unsigned int size = sizeof(struct ibv_flow_spec_gre);
1937 	struct ibv_flow_spec_gre tunnel = {
1938 		.type = parser->inner | IBV_FLOW_SPEC_GRE,
1939 		.size = size,
1940 	};
1941 #endif
1942 	struct ibv_flow_spec_ipv4_ext *ipv4;
1943 	struct ibv_flow_spec_ipv6 *ipv6;
1944 	unsigned int i;
1945 
1946 	parser->inner = IBV_FLOW_SPEC_INNER;
1947 	parser->tunnel = ptype_ext[PTYPE_IDX(RTE_PTYPE_TUNNEL_GRE)];
1948 	parser->out_layer = parser->layer;
1949 	parser->layer = HASH_RXQ_TUNNEL;
1950 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1951 	if (spec) {
1952 		if (!mask)
1953 			mask = default_mask;
1954 		tunnel.val.c_ks_res0_ver = spec->c_rsvd0_ver;
1955 		tunnel.val.protocol = spec->protocol;
1956 		tunnel.mask.c_ks_res0_ver = mask->c_rsvd0_ver;
1957 		tunnel.mask.protocol = mask->protocol;
1958 		/* Remove unwanted bits from values. */
1959 		tunnel.val.c_ks_res0_ver &= tunnel.mask.c_ks_res0_ver;
1960 		tunnel.val.protocol &= tunnel.mask.protocol;
1961 		tunnel.val.key &= tunnel.mask.key;
1962 	}
1963 #endif
1964 	/* Update encapsulation IP layer protocol. */
1965 	for (i = 0; i != hash_rxq_init_n; ++i) {
1966 		if (!parser->queue[i].ibv_attr)
1967 			continue;
1968 		if (parser->out_layer == HASH_RXQ_IPV4) {
1969 			ipv4 = (void *)((uintptr_t)parser->queue[i].ibv_attr +
1970 				parser->queue[i].offset -
1971 				sizeof(struct ibv_flow_spec_ipv4_ext));
1972 			if (ipv4->mask.proto && ipv4->val.proto != MLX5_GRE)
1973 				break;
1974 			ipv4->val.proto = MLX5_GRE;
1975 			ipv4->mask.proto = 0xff;
1976 		} else if (parser->out_layer == HASH_RXQ_IPV6) {
1977 			ipv6 = (void *)((uintptr_t)parser->queue[i].ibv_attr +
1978 				parser->queue[i].offset -
1979 				sizeof(struct ibv_flow_spec_ipv6));
1980 			if (ipv6->mask.next_hdr &&
1981 			    ipv6->val.next_hdr != MLX5_GRE)
1982 				break;
1983 			ipv6->val.next_hdr = MLX5_GRE;
1984 			ipv6->mask.next_hdr = 0xff;
1985 		}
1986 	}
1987 	if (i != hash_rxq_init_n)
1988 		return rte_flow_error_set(data->error, EINVAL,
1989 					  RTE_FLOW_ERROR_TYPE_ITEM,
1990 					  item,
1991 					  "IP protocol of GRE must be 47");
1992 	mlx5_flow_create_copy(parser, &tunnel, size);
1993 	return 0;
1994 }
1995 
1996 /**
1997  * Convert MPLS item to Verbs specification.
1998  * MPLS tunnel types currently supported are MPLS-in-GRE and MPLS-in-UDP.
1999  *
2000  * @param item[in]
2001  *   Item specification.
2002  * @param default_mask[in]
2003  *   Default bit-masks to use when item->mask is not provided.
2004  * @param data[in, out]
2005  *   User structure.
2006  *
2007  * @return
2008  *   0 on success, a negative errno value otherwise and rte_errno is set.
2009  */
2010 static int
2011 mlx5_flow_create_mpls(const struct rte_flow_item *item,
2012 		      const void *default_mask,
2013 		      struct mlx5_flow_data *data)
2014 {
2015 #ifndef HAVE_IBV_DEVICE_MPLS_SUPPORT
2016 	(void)default_mask;
2017 	return rte_flow_error_set(data->error, ENOTSUP,
2018 				  RTE_FLOW_ERROR_TYPE_ITEM,
2019 				  item,
2020 				  "MPLS is not supported by driver");
2021 #else
2022 	const struct rte_flow_item_mpls *spec = item->spec;
2023 	const struct rte_flow_item_mpls *mask = item->mask;
2024 	struct mlx5_flow_parse *parser = data->parser;
2025 	unsigned int size = sizeof(struct ibv_flow_spec_mpls);
2026 	struct ibv_flow_spec_mpls mpls = {
2027 		.type = IBV_FLOW_SPEC_MPLS,
2028 		.size = size,
2029 	};
2030 
2031 	parser->inner = IBV_FLOW_SPEC_INNER;
2032 	if (parser->layer == HASH_RXQ_UDPV4 ||
2033 	    parser->layer == HASH_RXQ_UDPV6) {
2034 		parser->tunnel =
2035 			ptype_ext[PTYPE_IDX(RTE_PTYPE_TUNNEL_MPLS_IN_UDP)];
2036 		parser->out_layer = parser->layer;
2037 	} else {
2038 		parser->tunnel =
2039 			ptype_ext[PTYPE_IDX(RTE_PTYPE_TUNNEL_MPLS_IN_GRE)];
2040 		/* parser->out_layer stays as in GRE out_layer. */
2041 	}
2042 	parser->layer = HASH_RXQ_TUNNEL;
2043 	if (spec) {
2044 		if (!mask)
2045 			mask = default_mask;
2046 		/*
2047 		 * The verbs label field includes the entire MPLS header:
2048 		 * bits 0:19 - label value field.
2049 		 * bits 20:22 - traffic class field.
2050 		 * bits 23 - bottom of stack bit.
2051 		 * bits 24:31 - ttl field.
2052 		 */
2053 		mpls.val.label = *(const uint32_t *)spec;
2054 		mpls.mask.label = *(const uint32_t *)mask;
2055 		/* Remove unwanted bits from values. */
2056 		mpls.val.label &= mpls.mask.label;
2057 	}
2058 	mlx5_flow_create_copy(parser, &mpls, size);
2059 	return 0;
2060 #endif
2061 }
2062 
2063 /**
2064  * Convert mark/flag action to Verbs specification.
2065  *
2066  * @param parser
2067  *   Internal parser structure.
2068  * @param mark_id
2069  *   Mark identifier.
2070  *
2071  * @return
2072  *   0 on success, a negative errno value otherwise and rte_errno is set.
2073  */
2074 static int
2075 mlx5_flow_create_flag_mark(struct mlx5_flow_parse *parser, uint32_t mark_id)
2076 {
2077 	unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
2078 	struct ibv_flow_spec_action_tag tag = {
2079 		.type = IBV_FLOW_SPEC_ACTION_TAG,
2080 		.size = size,
2081 		.tag_id = mlx5_flow_mark_set(mark_id),
2082 	};
2083 
2084 	assert(parser->mark);
2085 	mlx5_flow_create_copy(parser, &tag, size);
2086 	return 0;
2087 }
2088 
2089 /**
2090  * Convert count action to Verbs specification.
2091  *
2092  * @param dev
2093  *   Pointer to Ethernet device.
2094  * @param parser
2095  *   Pointer to MLX5 flow parser structure.
2096  *
2097  * @return
2098  *   0 on success, a negative errno value otherwise and rte_errno is set.
2099  */
2100 static int
2101 mlx5_flow_create_count(struct rte_eth_dev *dev __rte_unused,
2102 		       struct mlx5_flow_parse *parser __rte_unused)
2103 {
2104 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
2105 	struct priv *priv = dev->data->dev_private;
2106 	unsigned int size = sizeof(struct ibv_flow_spec_counter_action);
2107 	struct ibv_counter_set_init_attr init_attr = {0};
2108 	struct ibv_flow_spec_counter_action counter = {
2109 		.type = IBV_FLOW_SPEC_ACTION_COUNT,
2110 		.size = size,
2111 		.counter_set_handle = 0,
2112 	};
2113 
2114 	init_attr.counter_set_id = 0;
2115 	parser->cs = mlx5_glue->create_counter_set(priv->ctx, &init_attr);
2116 	if (!parser->cs) {
2117 		rte_errno = EINVAL;
2118 		return -rte_errno;
2119 	}
2120 	counter.counter_set_handle = parser->cs->handle;
2121 	mlx5_flow_create_copy(parser, &counter, size);
2122 #endif
2123 	return 0;
2124 }
2125 
2126 /**
2127  * Complete flow rule creation with a drop queue.
2128  *
2129  * @param dev
2130  *   Pointer to Ethernet device.
2131  * @param parser
2132  *   Internal parser structure.
2133  * @param flow
2134  *   Pointer to the rte_flow.
2135  * @param[out] error
2136  *   Perform verbose error reporting if not NULL.
2137  *
2138  * @return
2139  *   0 on success, a negative errno value otherwise and rte_errno is set.
2140  */
2141 static int
2142 mlx5_flow_create_action_queue_drop(struct rte_eth_dev *dev,
2143 				   struct mlx5_flow_parse *parser,
2144 				   struct rte_flow *flow,
2145 				   struct rte_flow_error *error)
2146 {
2147 	struct priv *priv = dev->data->dev_private;
2148 	struct ibv_flow_spec_action_drop *drop;
2149 	unsigned int size = sizeof(struct ibv_flow_spec_action_drop);
2150 
2151 	assert(priv->pd);
2152 	assert(priv->ctx);
2153 	flow->drop = 1;
2154 	drop = (void *)((uintptr_t)parser->queue[HASH_RXQ_ETH].ibv_attr +
2155 			parser->queue[HASH_RXQ_ETH].offset);
2156 	*drop = (struct ibv_flow_spec_action_drop){
2157 			.type = IBV_FLOW_SPEC_ACTION_DROP,
2158 			.size = size,
2159 	};
2160 	++parser->queue[HASH_RXQ_ETH].ibv_attr->num_of_specs;
2161 	parser->queue[HASH_RXQ_ETH].offset += size;
2162 	flow->frxq[HASH_RXQ_ETH].ibv_attr =
2163 		parser->queue[HASH_RXQ_ETH].ibv_attr;
2164 	if (parser->count)
2165 		flow->cs = parser->cs;
2166 	if (!dev->data->dev_started)
2167 		return 0;
2168 	parser->queue[HASH_RXQ_ETH].ibv_attr = NULL;
2169 	flow->frxq[HASH_RXQ_ETH].ibv_flow =
2170 		mlx5_glue->create_flow(priv->flow_drop_queue->qp,
2171 				       flow->frxq[HASH_RXQ_ETH].ibv_attr);
2172 	if (!flow->frxq[HASH_RXQ_ETH].ibv_flow) {
2173 		rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
2174 				   NULL, "flow rule creation failure");
2175 		goto error;
2176 	}
2177 	return 0;
2178 error:
2179 	assert(flow);
2180 	if (flow->frxq[HASH_RXQ_ETH].ibv_flow) {
2181 		claim_zero(mlx5_glue->destroy_flow
2182 			   (flow->frxq[HASH_RXQ_ETH].ibv_flow));
2183 		flow->frxq[HASH_RXQ_ETH].ibv_flow = NULL;
2184 	}
2185 	if (flow->frxq[HASH_RXQ_ETH].ibv_attr) {
2186 		rte_free(flow->frxq[HASH_RXQ_ETH].ibv_attr);
2187 		flow->frxq[HASH_RXQ_ETH].ibv_attr = NULL;
2188 	}
2189 	if (flow->cs) {
2190 		claim_zero(mlx5_glue->destroy_counter_set(flow->cs));
2191 		flow->cs = NULL;
2192 		parser->cs = NULL;
2193 	}
2194 	return -rte_errno;
2195 }
2196 
2197 /**
2198  * Create hash Rx queues when RSS is enabled.
2199  *
2200  * @param dev
2201  *   Pointer to Ethernet device.
2202  * @param parser
2203  *   Internal parser structure.
2204  * @param flow
2205  *   Pointer to the rte_flow.
2206  * @param[out] error
2207  *   Perform verbose error reporting if not NULL.
2208  *
2209  * @return
2210  *   0 on success, a negative errno value otherwise and rte_errno is set.
2211  */
2212 static int
2213 mlx5_flow_create_action_queue_rss(struct rte_eth_dev *dev,
2214 				  struct mlx5_flow_parse *parser,
2215 				  struct rte_flow *flow,
2216 				  struct rte_flow_error *error)
2217 {
2218 	unsigned int i;
2219 
2220 	for (i = 0; i != hash_rxq_init_n; ++i) {
2221 		if (!parser->queue[i].ibv_attr)
2222 			continue;
2223 		flow->frxq[i].ibv_attr = parser->queue[i].ibv_attr;
2224 		parser->queue[i].ibv_attr = NULL;
2225 		flow->frxq[i].hash_fields = parser->queue[i].hash_fields;
2226 		if (!dev->data->dev_started)
2227 			continue;
2228 		flow->frxq[i].hrxq =
2229 			mlx5_hrxq_get(dev,
2230 				      parser->rss_conf.key,
2231 				      parser->rss_conf.key_len,
2232 				      flow->frxq[i].hash_fields,
2233 				      parser->rss_conf.queue,
2234 				      parser->rss_conf.queue_num,
2235 				      parser->tunnel,
2236 				      parser->rss_conf.level);
2237 		if (flow->frxq[i].hrxq)
2238 			continue;
2239 		flow->frxq[i].hrxq =
2240 			mlx5_hrxq_new(dev,
2241 				      parser->rss_conf.key,
2242 				      parser->rss_conf.key_len,
2243 				      flow->frxq[i].hash_fields,
2244 				      parser->rss_conf.queue,
2245 				      parser->rss_conf.queue_num,
2246 				      parser->tunnel,
2247 				      parser->rss_conf.level);
2248 		if (!flow->frxq[i].hrxq) {
2249 			return rte_flow_error_set(error, ENOMEM,
2250 						  RTE_FLOW_ERROR_TYPE_HANDLE,
2251 						  NULL,
2252 						  "cannot create hash rxq");
2253 		}
2254 	}
2255 	return 0;
2256 }
2257 
2258 /**
2259  * RXQ update after flow rule creation.
2260  *
2261  * @param dev
2262  *   Pointer to Ethernet device.
2263  * @param flow
2264  *   Pointer to the flow rule.
2265  */
2266 static void
2267 mlx5_flow_create_update_rxqs(struct rte_eth_dev *dev, struct rte_flow *flow)
2268 {
2269 	struct priv *priv = dev->data->dev_private;
2270 	unsigned int i;
2271 	unsigned int j;
2272 
2273 	if (!dev->data->dev_started)
2274 		return;
2275 	for (i = 0; i != flow->rss_conf.queue_num; ++i) {
2276 		struct mlx5_rxq_data *rxq_data = (*priv->rxqs)
2277 						 [(*flow->queues)[i]];
2278 		struct mlx5_rxq_ctrl *rxq_ctrl =
2279 			container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
2280 		uint8_t tunnel = PTYPE_IDX(flow->tunnel);
2281 
2282 		rxq_data->mark |= flow->mark;
2283 		if (!tunnel)
2284 			continue;
2285 		rxq_ctrl->tunnel_types[tunnel] += 1;
2286 		/* Clear tunnel type if more than one tunnel types set. */
2287 		for (j = 0; j != RTE_DIM(rxq_ctrl->tunnel_types); ++j) {
2288 			if (j == tunnel)
2289 				continue;
2290 			if (rxq_ctrl->tunnel_types[j] > 0) {
2291 				rxq_data->tunnel = 0;
2292 				break;
2293 			}
2294 		}
2295 		if (j == RTE_DIM(rxq_ctrl->tunnel_types))
2296 			rxq_data->tunnel = flow->tunnel;
2297 	}
2298 }
2299 
2300 /**
2301  * Dump flow hash RX queue detail.
2302  *
2303  * @param dev
2304  *   Pointer to Ethernet device.
2305  * @param flow
2306  *   Pointer to the rte_flow.
2307  * @param hrxq_idx
2308  *   Hash RX queue index.
2309  */
2310 static void
2311 mlx5_flow_dump(struct rte_eth_dev *dev __rte_unused,
2312 	       struct rte_flow *flow __rte_unused,
2313 	       unsigned int hrxq_idx __rte_unused)
2314 {
2315 #ifndef NDEBUG
2316 	uintptr_t spec_ptr;
2317 	uint16_t j;
2318 	char buf[256];
2319 	uint8_t off;
2320 	uint64_t extra_hash_fields = 0;
2321 
2322 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
2323 	if (flow->tunnel && flow->rss_conf.level > 1)
2324 		extra_hash_fields = (uint32_t)IBV_RX_HASH_INNER;
2325 #endif
2326 	spec_ptr = (uintptr_t)(flow->frxq[hrxq_idx].ibv_attr + 1);
2327 	for (j = 0, off = 0; j < flow->frxq[hrxq_idx].ibv_attr->num_of_specs;
2328 	     j++) {
2329 		struct ibv_flow_spec *spec = (void *)spec_ptr;
2330 		off += sprintf(buf + off, " %x(%hu)", spec->hdr.type,
2331 			       spec->hdr.size);
2332 		spec_ptr += spec->hdr.size;
2333 	}
2334 	DRV_LOG(DEBUG,
2335 		"port %u Verbs flow %p type %u: hrxq:%p qp:%p ind:%p,"
2336 		" hash:%" PRIx64 "/%u specs:%hhu(%hu), priority:%hu, type:%d,"
2337 		" flags:%x, comp_mask:%x specs:%s",
2338 		dev->data->port_id, (void *)flow, hrxq_idx,
2339 		(void *)flow->frxq[hrxq_idx].hrxq,
2340 		(void *)flow->frxq[hrxq_idx].hrxq->qp,
2341 		(void *)flow->frxq[hrxq_idx].hrxq->ind_table,
2342 		(flow->frxq[hrxq_idx].hash_fields | extra_hash_fields),
2343 		flow->rss_conf.queue_num,
2344 		flow->frxq[hrxq_idx].ibv_attr->num_of_specs,
2345 		flow->frxq[hrxq_idx].ibv_attr->size,
2346 		flow->frxq[hrxq_idx].ibv_attr->priority,
2347 		flow->frxq[hrxq_idx].ibv_attr->type,
2348 		flow->frxq[hrxq_idx].ibv_attr->flags,
2349 		flow->frxq[hrxq_idx].ibv_attr->comp_mask,
2350 		buf);
2351 #endif
2352 }
2353 
2354 /**
2355  * Complete flow rule creation.
2356  *
2357  * @param dev
2358  *   Pointer to Ethernet device.
2359  * @param parser
2360  *   Internal parser structure.
2361  * @param flow
2362  *   Pointer to the rte_flow.
2363  * @param[out] error
2364  *   Perform verbose error reporting if not NULL.
2365  *
2366  * @return
2367  *   0 on success, a negative errno value otherwise and rte_errno is set.
2368  */
2369 static int
2370 mlx5_flow_create_action_queue(struct rte_eth_dev *dev,
2371 			      struct mlx5_flow_parse *parser,
2372 			      struct rte_flow *flow,
2373 			      struct rte_flow_error *error)
2374 {
2375 	struct priv *priv __rte_unused = dev->data->dev_private;
2376 	int ret;
2377 	unsigned int i;
2378 	unsigned int flows_n = 0;
2379 
2380 	assert(priv->pd);
2381 	assert(priv->ctx);
2382 	assert(!parser->drop);
2383 	ret = mlx5_flow_create_action_queue_rss(dev, parser, flow, error);
2384 	if (ret)
2385 		goto error;
2386 	if (parser->count)
2387 		flow->cs = parser->cs;
2388 	if (!dev->data->dev_started)
2389 		return 0;
2390 	for (i = 0; i != hash_rxq_init_n; ++i) {
2391 		if (!flow->frxq[i].hrxq)
2392 			continue;
2393 		flow->frxq[i].ibv_flow =
2394 			mlx5_glue->create_flow(flow->frxq[i].hrxq->qp,
2395 					       flow->frxq[i].ibv_attr);
2396 		mlx5_flow_dump(dev, flow, i);
2397 		if (!flow->frxq[i].ibv_flow) {
2398 			rte_flow_error_set(error, ENOMEM,
2399 					   RTE_FLOW_ERROR_TYPE_HANDLE,
2400 					   NULL, "flow rule creation failure");
2401 			goto error;
2402 		}
2403 		++flows_n;
2404 	}
2405 	if (!flows_n) {
2406 		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
2407 				   NULL, "internal error in flow creation");
2408 		goto error;
2409 	}
2410 	mlx5_flow_create_update_rxqs(dev, flow);
2411 	return 0;
2412 error:
2413 	ret = rte_errno; /* Save rte_errno before cleanup. */
2414 	assert(flow);
2415 	for (i = 0; i != hash_rxq_init_n; ++i) {
2416 		if (flow->frxq[i].ibv_flow) {
2417 			struct ibv_flow *ibv_flow = flow->frxq[i].ibv_flow;
2418 
2419 			claim_zero(mlx5_glue->destroy_flow(ibv_flow));
2420 		}
2421 		if (flow->frxq[i].hrxq)
2422 			mlx5_hrxq_release(dev, flow->frxq[i].hrxq);
2423 		if (flow->frxq[i].ibv_attr)
2424 			rte_free(flow->frxq[i].ibv_attr);
2425 	}
2426 	if (flow->cs) {
2427 		claim_zero(mlx5_glue->destroy_counter_set(flow->cs));
2428 		flow->cs = NULL;
2429 		parser->cs = NULL;
2430 	}
2431 	rte_errno = ret; /* Restore rte_errno. */
2432 	return -rte_errno;
2433 }
2434 
2435 /**
2436  * Convert a flow.
2437  *
2438  * @param dev
2439  *   Pointer to Ethernet device.
2440  * @param list
2441  *   Pointer to a TAILQ flow list.
2442  * @param[in] attr
2443  *   Flow rule attributes.
2444  * @param[in] pattern
2445  *   Pattern specification (list terminated by the END pattern item).
2446  * @param[in] actions
2447  *   Associated actions (list terminated by the END action).
2448  * @param[out] error
2449  *   Perform verbose error reporting if not NULL.
2450  *
2451  * @return
2452  *   A flow on success, NULL otherwise and rte_errno is set.
2453  */
2454 static struct rte_flow *
2455 mlx5_flow_list_create(struct rte_eth_dev *dev,
2456 		      struct mlx5_flows *list,
2457 		      const struct rte_flow_attr *attr,
2458 		      const struct rte_flow_item items[],
2459 		      const struct rte_flow_action actions[],
2460 		      struct rte_flow_error *error)
2461 {
2462 	struct mlx5_flow_parse parser = { .create = 1, };
2463 	struct rte_flow *flow = NULL;
2464 	unsigned int i;
2465 	int ret;
2466 
2467 	ret = mlx5_flow_convert(dev, attr, items, actions, error, &parser);
2468 	if (ret)
2469 		goto exit;
2470 	flow = rte_calloc(__func__, 1,
2471 			  sizeof(*flow) +
2472 			  parser.rss_conf.queue_num * sizeof(uint16_t),
2473 			  0);
2474 	if (!flow) {
2475 		rte_flow_error_set(error, ENOMEM,
2476 				   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2477 				   NULL,
2478 				   "cannot allocate flow memory");
2479 		return NULL;
2480 	}
2481 	/* Copy configuration. */
2482 	flow->queues = (uint16_t (*)[])(flow + 1);
2483 	flow->tunnel = parser.tunnel;
2484 	flow->rss_conf = (struct rte_flow_action_rss){
2485 		.func = RTE_ETH_HASH_FUNCTION_DEFAULT,
2486 		.level = parser.rss_conf.level,
2487 		.types = parser.rss_conf.types,
2488 		.key_len = parser.rss_conf.key_len,
2489 		.queue_num = parser.rss_conf.queue_num,
2490 		.key = memcpy(flow->rss_key, parser.rss_conf.key,
2491 			      sizeof(*parser.rss_conf.key) *
2492 			      parser.rss_conf.key_len),
2493 		.queue = memcpy(flow->queues, parser.rss_conf.queue,
2494 				sizeof(*parser.rss_conf.queue) *
2495 				parser.rss_conf.queue_num),
2496 	};
2497 	flow->mark = parser.mark;
2498 	/* finalise the flow. */
2499 	if (parser.drop)
2500 		ret = mlx5_flow_create_action_queue_drop(dev, &parser, flow,
2501 							 error);
2502 	else
2503 		ret = mlx5_flow_create_action_queue(dev, &parser, flow, error);
2504 	if (ret)
2505 		goto exit;
2506 	TAILQ_INSERT_TAIL(list, flow, next);
2507 	DRV_LOG(DEBUG, "port %u flow created %p", dev->data->port_id,
2508 		(void *)flow);
2509 	return flow;
2510 exit:
2511 	DRV_LOG(ERR, "port %u flow creation error: %s", dev->data->port_id,
2512 		error->message);
2513 	for (i = 0; i != hash_rxq_init_n; ++i) {
2514 		if (parser.queue[i].ibv_attr)
2515 			rte_free(parser.queue[i].ibv_attr);
2516 	}
2517 	rte_free(flow);
2518 	return NULL;
2519 }
2520 
2521 /**
2522  * Validate a flow supported by the NIC.
2523  *
2524  * @see rte_flow_validate()
2525  * @see rte_flow_ops
2526  */
2527 int
2528 mlx5_flow_validate(struct rte_eth_dev *dev,
2529 		   const struct rte_flow_attr *attr,
2530 		   const struct rte_flow_item items[],
2531 		   const struct rte_flow_action actions[],
2532 		   struct rte_flow_error *error)
2533 {
2534 	struct mlx5_flow_parse parser = { .create = 0, };
2535 
2536 	return mlx5_flow_convert(dev, attr, items, actions, error, &parser);
2537 }
2538 
2539 /**
2540  * Create a flow.
2541  *
2542  * @see rte_flow_create()
2543  * @see rte_flow_ops
2544  */
2545 struct rte_flow *
2546 mlx5_flow_create(struct rte_eth_dev *dev,
2547 		 const struct rte_flow_attr *attr,
2548 		 const struct rte_flow_item items[],
2549 		 const struct rte_flow_action actions[],
2550 		 struct rte_flow_error *error)
2551 {
2552 	struct priv *priv = dev->data->dev_private;
2553 
2554 	return mlx5_flow_list_create(dev, &priv->flows, attr, items, actions,
2555 				     error);
2556 }
2557 
2558 /**
2559  * Destroy a flow in a list.
2560  *
2561  * @param dev
2562  *   Pointer to Ethernet device.
2563  * @param list
2564  *   Pointer to a TAILQ flow list.
2565  * @param[in] flow
2566  *   Flow to destroy.
2567  */
2568 static void
2569 mlx5_flow_list_destroy(struct rte_eth_dev *dev, struct mlx5_flows *list,
2570 		       struct rte_flow *flow)
2571 {
2572 	struct priv *priv = dev->data->dev_private;
2573 	unsigned int i;
2574 
2575 	if (flow->drop || !dev->data->dev_started)
2576 		goto free;
2577 	for (i = 0; flow->tunnel && i != flow->rss_conf.queue_num; ++i) {
2578 		/* Update queue tunnel type. */
2579 		struct mlx5_rxq_data *rxq_data = (*priv->rxqs)
2580 						 [(*flow->queues)[i]];
2581 		struct mlx5_rxq_ctrl *rxq_ctrl =
2582 			container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
2583 		uint8_t tunnel = PTYPE_IDX(flow->tunnel);
2584 
2585 		assert(rxq_ctrl->tunnel_types[tunnel] > 0);
2586 		rxq_ctrl->tunnel_types[tunnel] -= 1;
2587 		if (!rxq_ctrl->tunnel_types[tunnel]) {
2588 			/* Update tunnel type. */
2589 			uint8_t j;
2590 			uint8_t types = 0;
2591 			uint8_t last;
2592 
2593 			for (j = 0; j < RTE_DIM(rxq_ctrl->tunnel_types); j++)
2594 				if (rxq_ctrl->tunnel_types[j]) {
2595 					types += 1;
2596 					last = j;
2597 				}
2598 			/* Keep same if more than one tunnel types left. */
2599 			if (types == 1)
2600 				rxq_data->tunnel = ptype_ext[last];
2601 			else if (types == 0)
2602 				/* No tunnel type left. */
2603 				rxq_data->tunnel = 0;
2604 		}
2605 	}
2606 	for (i = 0; flow->mark && i != flow->rss_conf.queue_num; ++i) {
2607 		struct rte_flow *tmp;
2608 		int mark = 0;
2609 
2610 		/*
2611 		 * To remove the mark from the queue, the queue must not be
2612 		 * present in any other marked flow (RSS or not).
2613 		 */
2614 		TAILQ_FOREACH(tmp, list, next) {
2615 			unsigned int j;
2616 			uint16_t *tqs = NULL;
2617 			uint16_t tq_n = 0;
2618 
2619 			if (!tmp->mark)
2620 				continue;
2621 			for (j = 0; j != hash_rxq_init_n; ++j) {
2622 				if (!tmp->frxq[j].hrxq)
2623 					continue;
2624 				tqs = tmp->frxq[j].hrxq->ind_table->queues;
2625 				tq_n = tmp->frxq[j].hrxq->ind_table->queues_n;
2626 			}
2627 			if (!tq_n)
2628 				continue;
2629 			for (j = 0; (j != tq_n) && !mark; j++)
2630 				if (tqs[j] == (*flow->queues)[i])
2631 					mark = 1;
2632 		}
2633 		(*priv->rxqs)[(*flow->queues)[i]]->mark = mark;
2634 	}
2635 free:
2636 	if (flow->drop) {
2637 		if (flow->frxq[HASH_RXQ_ETH].ibv_flow)
2638 			claim_zero(mlx5_glue->destroy_flow
2639 				   (flow->frxq[HASH_RXQ_ETH].ibv_flow));
2640 		rte_free(flow->frxq[HASH_RXQ_ETH].ibv_attr);
2641 	} else {
2642 		for (i = 0; i != hash_rxq_init_n; ++i) {
2643 			struct mlx5_flow *frxq = &flow->frxq[i];
2644 
2645 			if (frxq->ibv_flow)
2646 				claim_zero(mlx5_glue->destroy_flow
2647 					   (frxq->ibv_flow));
2648 			if (frxq->hrxq)
2649 				mlx5_hrxq_release(dev, frxq->hrxq);
2650 			if (frxq->ibv_attr)
2651 				rte_free(frxq->ibv_attr);
2652 		}
2653 	}
2654 	if (flow->cs) {
2655 		claim_zero(mlx5_glue->destroy_counter_set(flow->cs));
2656 		flow->cs = NULL;
2657 	}
2658 	TAILQ_REMOVE(list, flow, next);
2659 	DRV_LOG(DEBUG, "port %u flow destroyed %p", dev->data->port_id,
2660 		(void *)flow);
2661 	rte_free(flow);
2662 }
2663 
2664 /**
2665  * Destroy all flows.
2666  *
2667  * @param dev
2668  *   Pointer to Ethernet device.
2669  * @param list
2670  *   Pointer to a TAILQ flow list.
2671  */
2672 void
2673 mlx5_flow_list_flush(struct rte_eth_dev *dev, struct mlx5_flows *list)
2674 {
2675 	while (!TAILQ_EMPTY(list)) {
2676 		struct rte_flow *flow;
2677 
2678 		flow = TAILQ_FIRST(list);
2679 		mlx5_flow_list_destroy(dev, list, flow);
2680 	}
2681 }
2682 
2683 /**
2684  * Create drop queue.
2685  *
2686  * @param dev
2687  *   Pointer to Ethernet device.
2688  *
2689  * @return
2690  *   0 on success, a negative errno value otherwise and rte_errno is set.
2691  */
2692 int
2693 mlx5_flow_create_drop_queue(struct rte_eth_dev *dev)
2694 {
2695 	struct priv *priv = dev->data->dev_private;
2696 	struct mlx5_hrxq_drop *fdq = NULL;
2697 
2698 	assert(priv->pd);
2699 	assert(priv->ctx);
2700 	fdq = rte_calloc(__func__, 1, sizeof(*fdq), 0);
2701 	if (!fdq) {
2702 		DRV_LOG(WARNING,
2703 			"port %u cannot allocate memory for drop queue",
2704 			dev->data->port_id);
2705 		rte_errno = ENOMEM;
2706 		return -rte_errno;
2707 	}
2708 	fdq->cq = mlx5_glue->create_cq(priv->ctx, 1, NULL, NULL, 0);
2709 	if (!fdq->cq) {
2710 		DRV_LOG(WARNING, "port %u cannot allocate CQ for drop queue",
2711 			dev->data->port_id);
2712 		rte_errno = errno;
2713 		goto error;
2714 	}
2715 	fdq->wq = mlx5_glue->create_wq
2716 		(priv->ctx,
2717 		 &(struct ibv_wq_init_attr){
2718 			.wq_type = IBV_WQT_RQ,
2719 			.max_wr = 1,
2720 			.max_sge = 1,
2721 			.pd = priv->pd,
2722 			.cq = fdq->cq,
2723 		 });
2724 	if (!fdq->wq) {
2725 		DRV_LOG(WARNING, "port %u cannot allocate WQ for drop queue",
2726 			dev->data->port_id);
2727 		rte_errno = errno;
2728 		goto error;
2729 	}
2730 	fdq->ind_table = mlx5_glue->create_rwq_ind_table
2731 		(priv->ctx,
2732 		 &(struct ibv_rwq_ind_table_init_attr){
2733 			.log_ind_tbl_size = 0,
2734 			.ind_tbl = &fdq->wq,
2735 			.comp_mask = 0,
2736 		 });
2737 	if (!fdq->ind_table) {
2738 		DRV_LOG(WARNING,
2739 			"port %u cannot allocate indirection table for drop"
2740 			" queue",
2741 			dev->data->port_id);
2742 		rte_errno = errno;
2743 		goto error;
2744 	}
2745 	fdq->qp = mlx5_glue->create_qp_ex
2746 		(priv->ctx,
2747 		 &(struct ibv_qp_init_attr_ex){
2748 			.qp_type = IBV_QPT_RAW_PACKET,
2749 			.comp_mask =
2750 				IBV_QP_INIT_ATTR_PD |
2751 				IBV_QP_INIT_ATTR_IND_TABLE |
2752 				IBV_QP_INIT_ATTR_RX_HASH,
2753 			.rx_hash_conf = (struct ibv_rx_hash_conf){
2754 				.rx_hash_function =
2755 					IBV_RX_HASH_FUNC_TOEPLITZ,
2756 				.rx_hash_key_len = rss_hash_default_key_len,
2757 				.rx_hash_key = rss_hash_default_key,
2758 				.rx_hash_fields_mask = 0,
2759 				},
2760 			.rwq_ind_tbl = fdq->ind_table,
2761 			.pd = priv->pd
2762 		 });
2763 	if (!fdq->qp) {
2764 		DRV_LOG(WARNING, "port %u cannot allocate QP for drop queue",
2765 			dev->data->port_id);
2766 		rte_errno = errno;
2767 		goto error;
2768 	}
2769 	priv->flow_drop_queue = fdq;
2770 	return 0;
2771 error:
2772 	if (fdq->qp)
2773 		claim_zero(mlx5_glue->destroy_qp(fdq->qp));
2774 	if (fdq->ind_table)
2775 		claim_zero(mlx5_glue->destroy_rwq_ind_table(fdq->ind_table));
2776 	if (fdq->wq)
2777 		claim_zero(mlx5_glue->destroy_wq(fdq->wq));
2778 	if (fdq->cq)
2779 		claim_zero(mlx5_glue->destroy_cq(fdq->cq));
2780 	if (fdq)
2781 		rte_free(fdq);
2782 	priv->flow_drop_queue = NULL;
2783 	return -rte_errno;
2784 }
2785 
2786 /**
2787  * Delete drop queue.
2788  *
2789  * @param dev
2790  *   Pointer to Ethernet device.
2791  */
2792 void
2793 mlx5_flow_delete_drop_queue(struct rte_eth_dev *dev)
2794 {
2795 	struct priv *priv = dev->data->dev_private;
2796 	struct mlx5_hrxq_drop *fdq = priv->flow_drop_queue;
2797 
2798 	if (!fdq)
2799 		return;
2800 	if (fdq->qp)
2801 		claim_zero(mlx5_glue->destroy_qp(fdq->qp));
2802 	if (fdq->ind_table)
2803 		claim_zero(mlx5_glue->destroy_rwq_ind_table(fdq->ind_table));
2804 	if (fdq->wq)
2805 		claim_zero(mlx5_glue->destroy_wq(fdq->wq));
2806 	if (fdq->cq)
2807 		claim_zero(mlx5_glue->destroy_cq(fdq->cq));
2808 	rte_free(fdq);
2809 	priv->flow_drop_queue = NULL;
2810 }
2811 
2812 /**
2813  * Remove all flows.
2814  *
2815  * @param dev
2816  *   Pointer to Ethernet device.
2817  * @param list
2818  *   Pointer to a TAILQ flow list.
2819  */
2820 void
2821 mlx5_flow_stop(struct rte_eth_dev *dev, struct mlx5_flows *list)
2822 {
2823 	struct priv *priv = dev->data->dev_private;
2824 	struct rte_flow *flow;
2825 	unsigned int i;
2826 
2827 	TAILQ_FOREACH_REVERSE(flow, list, mlx5_flows, next) {
2828 		struct mlx5_ind_table_ibv *ind_tbl = NULL;
2829 
2830 		if (flow->drop) {
2831 			if (!flow->frxq[HASH_RXQ_ETH].ibv_flow)
2832 				continue;
2833 			claim_zero(mlx5_glue->destroy_flow
2834 				   (flow->frxq[HASH_RXQ_ETH].ibv_flow));
2835 			flow->frxq[HASH_RXQ_ETH].ibv_flow = NULL;
2836 			DRV_LOG(DEBUG, "port %u flow %p removed",
2837 				dev->data->port_id, (void *)flow);
2838 			/* Next flow. */
2839 			continue;
2840 		}
2841 		/* Verify the flow has not already been cleaned. */
2842 		for (i = 0; i != hash_rxq_init_n; ++i) {
2843 			if (!flow->frxq[i].ibv_flow)
2844 				continue;
2845 			/*
2846 			 * Indirection table may be necessary to remove the
2847 			 * flags in the Rx queues.
2848 			 * This helps to speed-up the process by avoiding
2849 			 * another loop.
2850 			 */
2851 			ind_tbl = flow->frxq[i].hrxq->ind_table;
2852 			break;
2853 		}
2854 		if (i == hash_rxq_init_n)
2855 			return;
2856 		if (flow->mark) {
2857 			assert(ind_tbl);
2858 			for (i = 0; i != ind_tbl->queues_n; ++i)
2859 				(*priv->rxqs)[ind_tbl->queues[i]]->mark = 0;
2860 		}
2861 		for (i = 0; i != hash_rxq_init_n; ++i) {
2862 			if (!flow->frxq[i].ibv_flow)
2863 				continue;
2864 			claim_zero(mlx5_glue->destroy_flow
2865 				   (flow->frxq[i].ibv_flow));
2866 			flow->frxq[i].ibv_flow = NULL;
2867 			mlx5_hrxq_release(dev, flow->frxq[i].hrxq);
2868 			flow->frxq[i].hrxq = NULL;
2869 		}
2870 		DRV_LOG(DEBUG, "port %u flow %p removed", dev->data->port_id,
2871 			(void *)flow);
2872 	}
2873 	/* Cleanup Rx queue tunnel info. */
2874 	for (i = 0; i != priv->rxqs_n; ++i) {
2875 		struct mlx5_rxq_data *q = (*priv->rxqs)[i];
2876 		struct mlx5_rxq_ctrl *rxq_ctrl =
2877 			container_of(q, struct mlx5_rxq_ctrl, rxq);
2878 
2879 		if (!q)
2880 			continue;
2881 		memset((void *)rxq_ctrl->tunnel_types, 0,
2882 		       sizeof(rxq_ctrl->tunnel_types));
2883 		q->tunnel = 0;
2884 	}
2885 }
2886 
2887 /**
2888  * Add all flows.
2889  *
2890  * @param dev
2891  *   Pointer to Ethernet device.
2892  * @param list
2893  *   Pointer to a TAILQ flow list.
2894  *
2895  * @return
2896  *   0 on success, a negative errno value otherwise and rte_errno is set.
2897  */
2898 int
2899 mlx5_flow_start(struct rte_eth_dev *dev, struct mlx5_flows *list)
2900 {
2901 	struct priv *priv = dev->data->dev_private;
2902 	struct rte_flow *flow;
2903 
2904 	TAILQ_FOREACH(flow, list, next) {
2905 		unsigned int i;
2906 
2907 		if (flow->drop) {
2908 			flow->frxq[HASH_RXQ_ETH].ibv_flow =
2909 				mlx5_glue->create_flow
2910 				(priv->flow_drop_queue->qp,
2911 				 flow->frxq[HASH_RXQ_ETH].ibv_attr);
2912 			if (!flow->frxq[HASH_RXQ_ETH].ibv_flow) {
2913 				DRV_LOG(DEBUG,
2914 					"port %u flow %p cannot be applied",
2915 					dev->data->port_id, (void *)flow);
2916 				rte_errno = EINVAL;
2917 				return -rte_errno;
2918 			}
2919 			DRV_LOG(DEBUG, "port %u flow %p applied",
2920 				dev->data->port_id, (void *)flow);
2921 			/* Next flow. */
2922 			continue;
2923 		}
2924 		for (i = 0; i != hash_rxq_init_n; ++i) {
2925 			if (!flow->frxq[i].ibv_attr)
2926 				continue;
2927 			flow->frxq[i].hrxq =
2928 				mlx5_hrxq_get(dev, flow->rss_conf.key,
2929 					      flow->rss_conf.key_len,
2930 					      flow->frxq[i].hash_fields,
2931 					      flow->rss_conf.queue,
2932 					      flow->rss_conf.queue_num,
2933 					      flow->tunnel,
2934 					      flow->rss_conf.level);
2935 			if (flow->frxq[i].hrxq)
2936 				goto flow_create;
2937 			flow->frxq[i].hrxq =
2938 				mlx5_hrxq_new(dev, flow->rss_conf.key,
2939 					      flow->rss_conf.key_len,
2940 					      flow->frxq[i].hash_fields,
2941 					      flow->rss_conf.queue,
2942 					      flow->rss_conf.queue_num,
2943 					      flow->tunnel,
2944 					      flow->rss_conf.level);
2945 			if (!flow->frxq[i].hrxq) {
2946 				DRV_LOG(DEBUG,
2947 					"port %u flow %p cannot create hash"
2948 					" rxq",
2949 					dev->data->port_id, (void *)flow);
2950 				rte_errno = EINVAL;
2951 				return -rte_errno;
2952 			}
2953 flow_create:
2954 			mlx5_flow_dump(dev, flow, i);
2955 			flow->frxq[i].ibv_flow =
2956 				mlx5_glue->create_flow(flow->frxq[i].hrxq->qp,
2957 						       flow->frxq[i].ibv_attr);
2958 			if (!flow->frxq[i].ibv_flow) {
2959 				DRV_LOG(DEBUG,
2960 					"port %u flow %p type %u cannot be"
2961 					" applied",
2962 					dev->data->port_id, (void *)flow, i);
2963 				rte_errno = EINVAL;
2964 				return -rte_errno;
2965 			}
2966 		}
2967 		mlx5_flow_create_update_rxqs(dev, flow);
2968 	}
2969 	return 0;
2970 }
2971 
2972 /**
2973  * Verify the flow list is empty
2974  *
2975  * @param dev
2976  *  Pointer to Ethernet device.
2977  *
2978  * @return the number of flows not released.
2979  */
2980 int
2981 mlx5_flow_verify(struct rte_eth_dev *dev)
2982 {
2983 	struct priv *priv = dev->data->dev_private;
2984 	struct rte_flow *flow;
2985 	int ret = 0;
2986 
2987 	TAILQ_FOREACH(flow, &priv->flows, next) {
2988 		DRV_LOG(DEBUG, "port %u flow %p still referenced",
2989 			dev->data->port_id, (void *)flow);
2990 		++ret;
2991 	}
2992 	return ret;
2993 }
2994 
2995 /**
2996  * Enable a control flow configured from the control plane.
2997  *
2998  * @param dev
2999  *   Pointer to Ethernet device.
3000  * @param eth_spec
3001  *   An Ethernet flow spec to apply.
3002  * @param eth_mask
3003  *   An Ethernet flow mask to apply.
3004  * @param vlan_spec
3005  *   A VLAN flow spec to apply.
3006  * @param vlan_mask
3007  *   A VLAN flow mask to apply.
3008  *
3009  * @return
3010  *   0 on success, a negative errno value otherwise and rte_errno is set.
3011  */
3012 int
3013 mlx5_ctrl_flow_vlan(struct rte_eth_dev *dev,
3014 		    struct rte_flow_item_eth *eth_spec,
3015 		    struct rte_flow_item_eth *eth_mask,
3016 		    struct rte_flow_item_vlan *vlan_spec,
3017 		    struct rte_flow_item_vlan *vlan_mask)
3018 {
3019 	struct priv *priv = dev->data->dev_private;
3020 	const struct rte_flow_attr attr = {
3021 		.ingress = 1,
3022 		.priority = MLX5_CTRL_FLOW_PRIORITY,
3023 	};
3024 	struct rte_flow_item items[] = {
3025 		{
3026 			.type = RTE_FLOW_ITEM_TYPE_ETH,
3027 			.spec = eth_spec,
3028 			.last = NULL,
3029 			.mask = eth_mask,
3030 		},
3031 		{
3032 			.type = (vlan_spec) ? RTE_FLOW_ITEM_TYPE_VLAN :
3033 				RTE_FLOW_ITEM_TYPE_END,
3034 			.spec = vlan_spec,
3035 			.last = NULL,
3036 			.mask = vlan_mask,
3037 		},
3038 		{
3039 			.type = RTE_FLOW_ITEM_TYPE_END,
3040 		},
3041 	};
3042 	uint16_t queue[priv->reta_idx_n];
3043 	struct rte_flow_action_rss action_rss = {
3044 		.func = RTE_ETH_HASH_FUNCTION_DEFAULT,
3045 		.level = 0,
3046 		.types = priv->rss_conf.rss_hf,
3047 		.key_len = priv->rss_conf.rss_key_len,
3048 		.queue_num = priv->reta_idx_n,
3049 		.key = priv->rss_conf.rss_key,
3050 		.queue = queue,
3051 	};
3052 	struct rte_flow_action actions[] = {
3053 		{
3054 			.type = RTE_FLOW_ACTION_TYPE_RSS,
3055 			.conf = &action_rss,
3056 		},
3057 		{
3058 			.type = RTE_FLOW_ACTION_TYPE_END,
3059 		},
3060 	};
3061 	struct rte_flow *flow;
3062 	struct rte_flow_error error;
3063 	unsigned int i;
3064 
3065 	if (!priv->reta_idx_n) {
3066 		rte_errno = EINVAL;
3067 		return -rte_errno;
3068 	}
3069 	for (i = 0; i != priv->reta_idx_n; ++i)
3070 		queue[i] = (*priv->reta_idx)[i];
3071 	flow = mlx5_flow_list_create(dev, &priv->ctrl_flows, &attr, items,
3072 				     actions, &error);
3073 	if (!flow)
3074 		return -rte_errno;
3075 	return 0;
3076 }
3077 
3078 /**
3079  * Enable a flow control configured from the control plane.
3080  *
3081  * @param dev
3082  *   Pointer to Ethernet device.
3083  * @param eth_spec
3084  *   An Ethernet flow spec to apply.
3085  * @param eth_mask
3086  *   An Ethernet flow mask to apply.
3087  *
3088  * @return
3089  *   0 on success, a negative errno value otherwise and rte_errno is set.
3090  */
3091 int
3092 mlx5_ctrl_flow(struct rte_eth_dev *dev,
3093 	       struct rte_flow_item_eth *eth_spec,
3094 	       struct rte_flow_item_eth *eth_mask)
3095 {
3096 	return mlx5_ctrl_flow_vlan(dev, eth_spec, eth_mask, NULL, NULL);
3097 }
3098 
3099 /**
3100  * Destroy a flow.
3101  *
3102  * @see rte_flow_destroy()
3103  * @see rte_flow_ops
3104  */
3105 int
3106 mlx5_flow_destroy(struct rte_eth_dev *dev,
3107 		  struct rte_flow *flow,
3108 		  struct rte_flow_error *error __rte_unused)
3109 {
3110 	struct priv *priv = dev->data->dev_private;
3111 
3112 	mlx5_flow_list_destroy(dev, &priv->flows, flow);
3113 	return 0;
3114 }
3115 
3116 /**
3117  * Destroy all flows.
3118  *
3119  * @see rte_flow_flush()
3120  * @see rte_flow_ops
3121  */
3122 int
3123 mlx5_flow_flush(struct rte_eth_dev *dev,
3124 		struct rte_flow_error *error __rte_unused)
3125 {
3126 	struct priv *priv = dev->data->dev_private;
3127 
3128 	mlx5_flow_list_flush(dev, &priv->flows);
3129 	return 0;
3130 }
3131 
3132 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
3133 /**
3134  * Query flow counter.
3135  *
3136  * @param cs
3137  *   the counter set.
3138  * @param counter_value
3139  *   returned data from the counter.
3140  *
3141  * @return
3142  *   0 on success, a negative errno value otherwise and rte_errno is set.
3143  */
3144 static int
3145 mlx5_flow_query_count(struct ibv_counter_set *cs,
3146 		      struct mlx5_flow_counter_stats *counter_stats,
3147 		      struct rte_flow_query_count *query_count,
3148 		      struct rte_flow_error *error)
3149 {
3150 	uint64_t counters[2];
3151 	struct ibv_query_counter_set_attr query_cs_attr = {
3152 		.cs = cs,
3153 		.query_flags = IBV_COUNTER_SET_FORCE_UPDATE,
3154 	};
3155 	struct ibv_counter_set_data query_out = {
3156 		.out = counters,
3157 		.outlen = 2 * sizeof(uint64_t),
3158 	};
3159 	int err = mlx5_glue->query_counter_set(&query_cs_attr, &query_out);
3160 
3161 	if (err)
3162 		return rte_flow_error_set(error, err,
3163 					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3164 					  NULL,
3165 					  "cannot read counter");
3166 	query_count->hits_set = 1;
3167 	query_count->bytes_set = 1;
3168 	query_count->hits = counters[0] - counter_stats->hits;
3169 	query_count->bytes = counters[1] - counter_stats->bytes;
3170 	if (query_count->reset) {
3171 		counter_stats->hits = counters[0];
3172 		counter_stats->bytes = counters[1];
3173 	}
3174 	return 0;
3175 }
3176 
3177 /**
3178  * Query a flows.
3179  *
3180  * @see rte_flow_query()
3181  * @see rte_flow_ops
3182  */
3183 int
3184 mlx5_flow_query(struct rte_eth_dev *dev __rte_unused,
3185 		struct rte_flow *flow,
3186 		const struct rte_flow_action *action __rte_unused,
3187 		void *data,
3188 		struct rte_flow_error *error)
3189 {
3190 	if (flow->cs) {
3191 		int ret;
3192 
3193 		ret = mlx5_flow_query_count(flow->cs,
3194 					    &flow->counter_stats,
3195 					    (struct rte_flow_query_count *)data,
3196 					    error);
3197 		if (ret)
3198 			return ret;
3199 	} else {
3200 		return rte_flow_error_set(error, EINVAL,
3201 					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3202 					  NULL,
3203 					  "no counter found for flow");
3204 	}
3205 	return 0;
3206 }
3207 #endif
3208 
3209 /**
3210  * Isolated mode.
3211  *
3212  * @see rte_flow_isolate()
3213  * @see rte_flow_ops
3214  */
3215 int
3216 mlx5_flow_isolate(struct rte_eth_dev *dev,
3217 		  int enable,
3218 		  struct rte_flow_error *error)
3219 {
3220 	struct priv *priv = dev->data->dev_private;
3221 
3222 	if (dev->data->dev_started) {
3223 		rte_flow_error_set(error, EBUSY,
3224 				   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3225 				   NULL,
3226 				   "port must be stopped first");
3227 		return -rte_errno;
3228 	}
3229 	priv->isolated = !!enable;
3230 	if (enable)
3231 		dev->dev_ops = &mlx5_dev_ops_isolate;
3232 	else
3233 		dev->dev_ops = &mlx5_dev_ops;
3234 	return 0;
3235 }
3236 
3237 /**
3238  * Convert a flow director filter to a generic flow.
3239  *
3240  * @param dev
3241  *   Pointer to Ethernet device.
3242  * @param fdir_filter
3243  *   Flow director filter to add.
3244  * @param attributes
3245  *   Generic flow parameters structure.
3246  *
3247  * @return
3248  *   0 on success, a negative errno value otherwise and rte_errno is set.
3249  */
3250 static int
3251 mlx5_fdir_filter_convert(struct rte_eth_dev *dev,
3252 			 const struct rte_eth_fdir_filter *fdir_filter,
3253 			 struct mlx5_fdir *attributes)
3254 {
3255 	struct priv *priv = dev->data->dev_private;
3256 	const struct rte_eth_fdir_input *input = &fdir_filter->input;
3257 	const struct rte_eth_fdir_masks *mask =
3258 		&dev->data->dev_conf.fdir_conf.mask;
3259 
3260 	/* Validate queue number. */
3261 	if (fdir_filter->action.rx_queue >= priv->rxqs_n) {
3262 		DRV_LOG(ERR, "port %u invalid queue number %d",
3263 			dev->data->port_id, fdir_filter->action.rx_queue);
3264 		rte_errno = EINVAL;
3265 		return -rte_errno;
3266 	}
3267 	attributes->attr.ingress = 1;
3268 	attributes->items[0] = (struct rte_flow_item) {
3269 		.type = RTE_FLOW_ITEM_TYPE_ETH,
3270 		.spec = &attributes->l2,
3271 		.mask = &attributes->l2_mask,
3272 	};
3273 	switch (fdir_filter->action.behavior) {
3274 	case RTE_ETH_FDIR_ACCEPT:
3275 		attributes->actions[0] = (struct rte_flow_action){
3276 			.type = RTE_FLOW_ACTION_TYPE_QUEUE,
3277 			.conf = &attributes->queue,
3278 		};
3279 		break;
3280 	case RTE_ETH_FDIR_REJECT:
3281 		attributes->actions[0] = (struct rte_flow_action){
3282 			.type = RTE_FLOW_ACTION_TYPE_DROP,
3283 		};
3284 		break;
3285 	default:
3286 		DRV_LOG(ERR, "port %u invalid behavior %d",
3287 			dev->data->port_id,
3288 			fdir_filter->action.behavior);
3289 		rte_errno = ENOTSUP;
3290 		return -rte_errno;
3291 	}
3292 	attributes->queue.index = fdir_filter->action.rx_queue;
3293 	/* Handle L3. */
3294 	switch (fdir_filter->input.flow_type) {
3295 	case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
3296 	case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
3297 	case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
3298 		attributes->l3.ipv4.hdr = (struct ipv4_hdr){
3299 			.src_addr = input->flow.ip4_flow.src_ip,
3300 			.dst_addr = input->flow.ip4_flow.dst_ip,
3301 			.time_to_live = input->flow.ip4_flow.ttl,
3302 			.type_of_service = input->flow.ip4_flow.tos,
3303 			.next_proto_id = input->flow.ip4_flow.proto,
3304 		};
3305 		attributes->l3_mask.ipv4.hdr = (struct ipv4_hdr){
3306 			.src_addr = mask->ipv4_mask.src_ip,
3307 			.dst_addr = mask->ipv4_mask.dst_ip,
3308 			.time_to_live = mask->ipv4_mask.ttl,
3309 			.type_of_service = mask->ipv4_mask.tos,
3310 			.next_proto_id = mask->ipv4_mask.proto,
3311 		};
3312 		attributes->items[1] = (struct rte_flow_item){
3313 			.type = RTE_FLOW_ITEM_TYPE_IPV4,
3314 			.spec = &attributes->l3,
3315 			.mask = &attributes->l3_mask,
3316 		};
3317 		break;
3318 	case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
3319 	case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
3320 	case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
3321 		attributes->l3.ipv6.hdr = (struct ipv6_hdr){
3322 			.hop_limits = input->flow.ipv6_flow.hop_limits,
3323 			.proto = input->flow.ipv6_flow.proto,
3324 		};
3325 
3326 		memcpy(attributes->l3.ipv6.hdr.src_addr,
3327 		       input->flow.ipv6_flow.src_ip,
3328 		       RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
3329 		memcpy(attributes->l3.ipv6.hdr.dst_addr,
3330 		       input->flow.ipv6_flow.dst_ip,
3331 		       RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
3332 		memcpy(attributes->l3_mask.ipv6.hdr.src_addr,
3333 		       mask->ipv6_mask.src_ip,
3334 		       RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
3335 		memcpy(attributes->l3_mask.ipv6.hdr.dst_addr,
3336 		       mask->ipv6_mask.dst_ip,
3337 		       RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
3338 		attributes->items[1] = (struct rte_flow_item){
3339 			.type = RTE_FLOW_ITEM_TYPE_IPV6,
3340 			.spec = &attributes->l3,
3341 			.mask = &attributes->l3_mask,
3342 		};
3343 		break;
3344 	default:
3345 		DRV_LOG(ERR, "port %u invalid flow type%d",
3346 			dev->data->port_id, fdir_filter->input.flow_type);
3347 		rte_errno = ENOTSUP;
3348 		return -rte_errno;
3349 	}
3350 	/* Handle L4. */
3351 	switch (fdir_filter->input.flow_type) {
3352 	case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
3353 		attributes->l4.udp.hdr = (struct udp_hdr){
3354 			.src_port = input->flow.udp4_flow.src_port,
3355 			.dst_port = input->flow.udp4_flow.dst_port,
3356 		};
3357 		attributes->l4_mask.udp.hdr = (struct udp_hdr){
3358 			.src_port = mask->src_port_mask,
3359 			.dst_port = mask->dst_port_mask,
3360 		};
3361 		attributes->items[2] = (struct rte_flow_item){
3362 			.type = RTE_FLOW_ITEM_TYPE_UDP,
3363 			.spec = &attributes->l4,
3364 			.mask = &attributes->l4_mask,
3365 		};
3366 		break;
3367 	case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
3368 		attributes->l4.tcp.hdr = (struct tcp_hdr){
3369 			.src_port = input->flow.tcp4_flow.src_port,
3370 			.dst_port = input->flow.tcp4_flow.dst_port,
3371 		};
3372 		attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
3373 			.src_port = mask->src_port_mask,
3374 			.dst_port = mask->dst_port_mask,
3375 		};
3376 		attributes->items[2] = (struct rte_flow_item){
3377 			.type = RTE_FLOW_ITEM_TYPE_TCP,
3378 			.spec = &attributes->l4,
3379 			.mask = &attributes->l4_mask,
3380 		};
3381 		break;
3382 	case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
3383 		attributes->l4.udp.hdr = (struct udp_hdr){
3384 			.src_port = input->flow.udp6_flow.src_port,
3385 			.dst_port = input->flow.udp6_flow.dst_port,
3386 		};
3387 		attributes->l4_mask.udp.hdr = (struct udp_hdr){
3388 			.src_port = mask->src_port_mask,
3389 			.dst_port = mask->dst_port_mask,
3390 		};
3391 		attributes->items[2] = (struct rte_flow_item){
3392 			.type = RTE_FLOW_ITEM_TYPE_UDP,
3393 			.spec = &attributes->l4,
3394 			.mask = &attributes->l4_mask,
3395 		};
3396 		break;
3397 	case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
3398 		attributes->l4.tcp.hdr = (struct tcp_hdr){
3399 			.src_port = input->flow.tcp6_flow.src_port,
3400 			.dst_port = input->flow.tcp6_flow.dst_port,
3401 		};
3402 		attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
3403 			.src_port = mask->src_port_mask,
3404 			.dst_port = mask->dst_port_mask,
3405 		};
3406 		attributes->items[2] = (struct rte_flow_item){
3407 			.type = RTE_FLOW_ITEM_TYPE_TCP,
3408 			.spec = &attributes->l4,
3409 			.mask = &attributes->l4_mask,
3410 		};
3411 		break;
3412 	case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
3413 	case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
3414 		break;
3415 	default:
3416 		DRV_LOG(ERR, "port %u invalid flow type%d",
3417 			dev->data->port_id, fdir_filter->input.flow_type);
3418 		rte_errno = ENOTSUP;
3419 		return -rte_errno;
3420 	}
3421 	return 0;
3422 }
3423 
3424 /**
3425  * Add new flow director filter and store it in list.
3426  *
3427  * @param dev
3428  *   Pointer to Ethernet device.
3429  * @param fdir_filter
3430  *   Flow director filter to add.
3431  *
3432  * @return
3433  *   0 on success, a negative errno value otherwise and rte_errno is set.
3434  */
3435 static int
3436 mlx5_fdir_filter_add(struct rte_eth_dev *dev,
3437 		     const struct rte_eth_fdir_filter *fdir_filter)
3438 {
3439 	struct priv *priv = dev->data->dev_private;
3440 	struct mlx5_fdir attributes = {
3441 		.attr.group = 0,
3442 		.l2_mask = {
3443 			.dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
3444 			.src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
3445 			.type = 0,
3446 		},
3447 	};
3448 	struct mlx5_flow_parse parser = {
3449 		.layer = HASH_RXQ_ETH,
3450 	};
3451 	struct rte_flow_error error;
3452 	struct rte_flow *flow;
3453 	int ret;
3454 
3455 	ret = mlx5_fdir_filter_convert(dev, fdir_filter, &attributes);
3456 	if (ret)
3457 		return ret;
3458 	ret = mlx5_flow_convert(dev, &attributes.attr, attributes.items,
3459 				attributes.actions, &error, &parser);
3460 	if (ret)
3461 		return ret;
3462 	flow = mlx5_flow_list_create(dev, &priv->flows, &attributes.attr,
3463 				     attributes.items, attributes.actions,
3464 				     &error);
3465 	if (flow) {
3466 		DRV_LOG(DEBUG, "port %u FDIR created %p", dev->data->port_id,
3467 			(void *)flow);
3468 		return 0;
3469 	}
3470 	return -rte_errno;
3471 }
3472 
3473 /**
3474  * Delete specific filter.
3475  *
3476  * @param dev
3477  *   Pointer to Ethernet device.
3478  * @param fdir_filter
3479  *   Filter to be deleted.
3480  *
3481  * @return
3482  *   0 on success, a negative errno value otherwise and rte_errno is set.
3483  */
3484 static int
3485 mlx5_fdir_filter_delete(struct rte_eth_dev *dev,
3486 			const struct rte_eth_fdir_filter *fdir_filter)
3487 {
3488 	struct priv *priv = dev->data->dev_private;
3489 	struct mlx5_fdir attributes = {
3490 		.attr.group = 0,
3491 	};
3492 	struct mlx5_flow_parse parser = {
3493 		.create = 1,
3494 		.layer = HASH_RXQ_ETH,
3495 	};
3496 	struct rte_flow_error error;
3497 	struct rte_flow *flow;
3498 	unsigned int i;
3499 	int ret;
3500 
3501 	ret = mlx5_fdir_filter_convert(dev, fdir_filter, &attributes);
3502 	if (ret)
3503 		return ret;
3504 	ret = mlx5_flow_convert(dev, &attributes.attr, attributes.items,
3505 				attributes.actions, &error, &parser);
3506 	if (ret)
3507 		goto exit;
3508 	/*
3509 	 * Special case for drop action which is only set in the
3510 	 * specifications when the flow is created.  In this situation the
3511 	 * drop specification is missing.
3512 	 */
3513 	if (parser.drop) {
3514 		struct ibv_flow_spec_action_drop *drop;
3515 
3516 		drop = (void *)((uintptr_t)parser.queue[HASH_RXQ_ETH].ibv_attr +
3517 				parser.queue[HASH_RXQ_ETH].offset);
3518 		*drop = (struct ibv_flow_spec_action_drop){
3519 			.type = IBV_FLOW_SPEC_ACTION_DROP,
3520 			.size = sizeof(struct ibv_flow_spec_action_drop),
3521 		};
3522 		parser.queue[HASH_RXQ_ETH].ibv_attr->num_of_specs++;
3523 	}
3524 	TAILQ_FOREACH(flow, &priv->flows, next) {
3525 		struct ibv_flow_attr *attr;
3526 		struct ibv_spec_header *attr_h;
3527 		void *spec;
3528 		struct ibv_flow_attr *flow_attr;
3529 		struct ibv_spec_header *flow_h;
3530 		void *flow_spec;
3531 		unsigned int specs_n;
3532 		unsigned int queue_id = parser.drop ? HASH_RXQ_ETH :
3533 						      parser.layer;
3534 
3535 		attr = parser.queue[queue_id].ibv_attr;
3536 		flow_attr = flow->frxq[queue_id].ibv_attr;
3537 		/* Compare first the attributes. */
3538 		if (!flow_attr ||
3539 		    memcmp(attr, flow_attr, sizeof(struct ibv_flow_attr)))
3540 			continue;
3541 		if (attr->num_of_specs == 0)
3542 			continue;
3543 		spec = (void *)((uintptr_t)attr +
3544 				sizeof(struct ibv_flow_attr));
3545 		flow_spec = (void *)((uintptr_t)flow_attr +
3546 				     sizeof(struct ibv_flow_attr));
3547 		specs_n = RTE_MIN(attr->num_of_specs, flow_attr->num_of_specs);
3548 		for (i = 0; i != specs_n; ++i) {
3549 			attr_h = spec;
3550 			flow_h = flow_spec;
3551 			if (memcmp(spec, flow_spec,
3552 				   RTE_MIN(attr_h->size, flow_h->size)))
3553 				goto wrong_flow;
3554 			spec = (void *)((uintptr_t)spec + attr_h->size);
3555 			flow_spec = (void *)((uintptr_t)flow_spec +
3556 					     flow_h->size);
3557 		}
3558 		/* At this point, the flow match. */
3559 		break;
3560 wrong_flow:
3561 		/* The flow does not match. */
3562 		continue;
3563 	}
3564 	if (flow)
3565 		mlx5_flow_list_destroy(dev, &priv->flows, flow);
3566 exit:
3567 	if (ret)
3568 		ret = rte_errno; /* Save rte_errno before cleanup. */
3569 	for (i = 0; i != hash_rxq_init_n; ++i) {
3570 		if (parser.queue[i].ibv_attr)
3571 			rte_free(parser.queue[i].ibv_attr);
3572 	}
3573 	if (ret)
3574 		rte_errno = ret; /* Restore rte_errno. */
3575 	return -rte_errno;
3576 }
3577 
3578 /**
3579  * Update queue for specific filter.
3580  *
3581  * @param dev
3582  *   Pointer to Ethernet device.
3583  * @param fdir_filter
3584  *   Filter to be updated.
3585  *
3586  * @return
3587  *   0 on success, a negative errno value otherwise and rte_errno is set.
3588  */
3589 static int
3590 mlx5_fdir_filter_update(struct rte_eth_dev *dev,
3591 			const struct rte_eth_fdir_filter *fdir_filter)
3592 {
3593 	int ret;
3594 
3595 	ret = mlx5_fdir_filter_delete(dev, fdir_filter);
3596 	if (ret)
3597 		return ret;
3598 	return mlx5_fdir_filter_add(dev, fdir_filter);
3599 }
3600 
3601 /**
3602  * Flush all filters.
3603  *
3604  * @param dev
3605  *   Pointer to Ethernet device.
3606  */
3607 static void
3608 mlx5_fdir_filter_flush(struct rte_eth_dev *dev)
3609 {
3610 	struct priv *priv = dev->data->dev_private;
3611 
3612 	mlx5_flow_list_flush(dev, &priv->flows);
3613 }
3614 
3615 /**
3616  * Get flow director information.
3617  *
3618  * @param dev
3619  *   Pointer to Ethernet device.
3620  * @param[out] fdir_info
3621  *   Resulting flow director information.
3622  */
3623 static void
3624 mlx5_fdir_info_get(struct rte_eth_dev *dev, struct rte_eth_fdir_info *fdir_info)
3625 {
3626 	struct rte_eth_fdir_masks *mask =
3627 		&dev->data->dev_conf.fdir_conf.mask;
3628 
3629 	fdir_info->mode = dev->data->dev_conf.fdir_conf.mode;
3630 	fdir_info->guarant_spc = 0;
3631 	rte_memcpy(&fdir_info->mask, mask, sizeof(fdir_info->mask));
3632 	fdir_info->max_flexpayload = 0;
3633 	fdir_info->flow_types_mask[0] = 0;
3634 	fdir_info->flex_payload_unit = 0;
3635 	fdir_info->max_flex_payload_segment_num = 0;
3636 	fdir_info->flex_payload_limit = 0;
3637 	memset(&fdir_info->flex_conf, 0, sizeof(fdir_info->flex_conf));
3638 }
3639 
3640 /**
3641  * Deal with flow director operations.
3642  *
3643  * @param dev
3644  *   Pointer to Ethernet device.
3645  * @param filter_op
3646  *   Operation to perform.
3647  * @param arg
3648  *   Pointer to operation-specific structure.
3649  *
3650  * @return
3651  *   0 on success, a negative errno value otherwise and rte_errno is set.
3652  */
3653 static int
3654 mlx5_fdir_ctrl_func(struct rte_eth_dev *dev, enum rte_filter_op filter_op,
3655 		    void *arg)
3656 {
3657 	enum rte_fdir_mode fdir_mode =
3658 		dev->data->dev_conf.fdir_conf.mode;
3659 
3660 	if (filter_op == RTE_ETH_FILTER_NOP)
3661 		return 0;
3662 	if (fdir_mode != RTE_FDIR_MODE_PERFECT &&
3663 	    fdir_mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
3664 		DRV_LOG(ERR, "port %u flow director mode %d not supported",
3665 			dev->data->port_id, fdir_mode);
3666 		rte_errno = EINVAL;
3667 		return -rte_errno;
3668 	}
3669 	switch (filter_op) {
3670 	case RTE_ETH_FILTER_ADD:
3671 		return mlx5_fdir_filter_add(dev, arg);
3672 	case RTE_ETH_FILTER_UPDATE:
3673 		return mlx5_fdir_filter_update(dev, arg);
3674 	case RTE_ETH_FILTER_DELETE:
3675 		return mlx5_fdir_filter_delete(dev, arg);
3676 	case RTE_ETH_FILTER_FLUSH:
3677 		mlx5_fdir_filter_flush(dev);
3678 		break;
3679 	case RTE_ETH_FILTER_INFO:
3680 		mlx5_fdir_info_get(dev, arg);
3681 		break;
3682 	default:
3683 		DRV_LOG(DEBUG, "port %u unknown operation %u",
3684 			dev->data->port_id, filter_op);
3685 		rte_errno = EINVAL;
3686 		return -rte_errno;
3687 	}
3688 	return 0;
3689 }
3690 
3691 /**
3692  * Manage filter operations.
3693  *
3694  * @param dev
3695  *   Pointer to Ethernet device structure.
3696  * @param filter_type
3697  *   Filter type.
3698  * @param filter_op
3699  *   Operation to perform.
3700  * @param arg
3701  *   Pointer to operation-specific structure.
3702  *
3703  * @return
3704  *   0 on success, a negative errno value otherwise and rte_errno is set.
3705  */
3706 int
3707 mlx5_dev_filter_ctrl(struct rte_eth_dev *dev,
3708 		     enum rte_filter_type filter_type,
3709 		     enum rte_filter_op filter_op,
3710 		     void *arg)
3711 {
3712 	switch (filter_type) {
3713 	case RTE_ETH_FILTER_GENERIC:
3714 		if (filter_op != RTE_ETH_FILTER_GET) {
3715 			rte_errno = EINVAL;
3716 			return -rte_errno;
3717 		}
3718 		*(const void **)arg = &mlx5_flow_ops;
3719 		return 0;
3720 	case RTE_ETH_FILTER_FDIR:
3721 		return mlx5_fdir_ctrl_func(dev, filter_op, arg);
3722 	default:
3723 		DRV_LOG(ERR, "port %u filter type (%d) not supported",
3724 			dev->data->port_id, filter_type);
3725 		rte_errno = ENOTSUP;
3726 		return -rte_errno;
3727 	}
3728 	return 0;
3729 }
3730 
3731 /**
3732  * Detect number of Verbs flow priorities supported.
3733  *
3734  * @param dev
3735  *   Pointer to Ethernet device.
3736  *
3737  * @return
3738  *   number of supported Verbs flow priority.
3739  */
3740 unsigned int
3741 mlx5_get_max_verbs_prio(struct rte_eth_dev *dev)
3742 {
3743 	struct priv *priv = dev->data->dev_private;
3744 	unsigned int verb_priorities = MLX5_VERBS_FLOW_PRIO_8;
3745 	struct {
3746 		struct ibv_flow_attr attr;
3747 		struct ibv_flow_spec_eth eth;
3748 		struct ibv_flow_spec_action_drop drop;
3749 	} flow_attr = {
3750 		.attr = {
3751 			.num_of_specs = 2,
3752 		},
3753 		.eth = {
3754 			.type = IBV_FLOW_SPEC_ETH,
3755 			.size = sizeof(struct ibv_flow_spec_eth),
3756 		},
3757 		.drop = {
3758 			.size = sizeof(struct ibv_flow_spec_action_drop),
3759 			.type = IBV_FLOW_SPEC_ACTION_DROP,
3760 		},
3761 	};
3762 	struct ibv_flow *flow;
3763 
3764 	do {
3765 		flow_attr.attr.priority = verb_priorities - 1;
3766 		flow = mlx5_glue->create_flow(priv->flow_drop_queue->qp,
3767 					      &flow_attr.attr);
3768 		if (flow) {
3769 			claim_zero(mlx5_glue->destroy_flow(flow));
3770 			/* Try more priorities. */
3771 			verb_priorities *= 2;
3772 		} else {
3773 			/* Failed, restore last right number. */
3774 			verb_priorities /= 2;
3775 			break;
3776 		}
3777 	} while (1);
3778 	DRV_LOG(DEBUG, "port %u Verbs flow priorities: %d,"
3779 		" user flow priorities: %d",
3780 		dev->data->port_id, verb_priorities, MLX5_CTRL_FLOW_PRIORITY);
3781 	return verb_priorities;
3782 }
3783