xref: /dpdk/drivers/net/mlx5/mlx5_flow.c (revision 6d3fa481ee6644a23ad65b8141f7eaa163e865c5)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2016 6WIND S.A.
5  *   Copyright 2016 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/queue.h>
35 #include <string.h>
36 
37 /* Verbs header. */
38 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
39 #ifdef PEDANTIC
40 #pragma GCC diagnostic ignored "-Wpedantic"
41 #endif
42 #include <infiniband/verbs.h>
43 #ifdef PEDANTIC
44 #pragma GCC diagnostic error "-Wpedantic"
45 #endif
46 
47 #include <rte_ethdev.h>
48 #include <rte_flow.h>
49 #include <rte_flow_driver.h>
50 #include <rte_malloc.h>
51 
52 #include "mlx5.h"
53 #include "mlx5_prm.h"
54 
55 /* Number of Work Queue necessary for the DROP queue. */
56 #define MLX5_DROP_WQ_N 4
57 
58 static int
59 mlx5_flow_create_eth(const struct rte_flow_item *item,
60 		     const void *default_mask,
61 		     void *data);
62 
63 static int
64 mlx5_flow_create_vlan(const struct rte_flow_item *item,
65 		      const void *default_mask,
66 		      void *data);
67 
68 static int
69 mlx5_flow_create_ipv4(const struct rte_flow_item *item,
70 		      const void *default_mask,
71 		      void *data);
72 
73 static int
74 mlx5_flow_create_ipv6(const struct rte_flow_item *item,
75 		      const void *default_mask,
76 		      void *data);
77 
78 static int
79 mlx5_flow_create_udp(const struct rte_flow_item *item,
80 		     const void *default_mask,
81 		     void *data);
82 
83 static int
84 mlx5_flow_create_tcp(const struct rte_flow_item *item,
85 		     const void *default_mask,
86 		     void *data);
87 
88 static int
89 mlx5_flow_create_vxlan(const struct rte_flow_item *item,
90 		       const void *default_mask,
91 		       void *data);
92 
93 struct rte_flow {
94 	LIST_ENTRY(rte_flow) next; /**< Pointer to the next flow structure. */
95 	struct ibv_exp_flow_attr *ibv_attr; /**< Pointer to Verbs attributes. */
96 	struct ibv_exp_rwq_ind_table *ind_table; /**< Indirection table. */
97 	struct ibv_qp *qp; /**< Verbs queue pair. */
98 	struct ibv_exp_flow *ibv_flow; /**< Verbs flow. */
99 	struct ibv_exp_wq *wq; /**< Verbs work queue. */
100 	struct ibv_cq *cq; /**< Verbs completion queue. */
101 	uint16_t rxqs_n; /**< Number of queues in this flow, 0 if drop queue. */
102 	uint32_t mark:1; /**< Set if the flow is marked. */
103 	uint32_t drop:1; /**< Drop queue. */
104 	uint64_t hash_fields; /**< Fields that participate in the hash. */
105 	struct rxq *rxqs[]; /**< Pointer to the queues array. */
106 };
107 
108 /** Static initializer for items. */
109 #define ITEMS(...) \
110 	(const enum rte_flow_item_type []){ \
111 		__VA_ARGS__, RTE_FLOW_ITEM_TYPE_END, \
112 	}
113 
114 /** Structure to generate a simple graph of layers supported by the NIC. */
115 struct mlx5_flow_items {
116 	/** List of possible actions for these items. */
117 	const enum rte_flow_action_type *const actions;
118 	/** Bit-masks corresponding to the possibilities for the item. */
119 	const void *mask;
120 	/**
121 	 * Default bit-masks to use when item->mask is not provided. When
122 	 * \default_mask is also NULL, the full supported bit-mask (\mask) is
123 	 * used instead.
124 	 */
125 	const void *default_mask;
126 	/** Bit-masks size in bytes. */
127 	const unsigned int mask_sz;
128 	/**
129 	 * Conversion function from rte_flow to NIC specific flow.
130 	 *
131 	 * @param item
132 	 *   rte_flow item to convert.
133 	 * @param default_mask
134 	 *   Default bit-masks to use when item->mask is not provided.
135 	 * @param data
136 	 *   Internal structure to store the conversion.
137 	 *
138 	 * @return
139 	 *   0 on success, negative value otherwise.
140 	 */
141 	int (*convert)(const struct rte_flow_item *item,
142 		       const void *default_mask,
143 		       void *data);
144 	/** Size in bytes of the destination structure. */
145 	const unsigned int dst_sz;
146 	/** List of possible following items.  */
147 	const enum rte_flow_item_type *const items;
148 };
149 
150 /** Valid action for this PMD. */
151 static const enum rte_flow_action_type valid_actions[] = {
152 	RTE_FLOW_ACTION_TYPE_DROP,
153 	RTE_FLOW_ACTION_TYPE_QUEUE,
154 	RTE_FLOW_ACTION_TYPE_MARK,
155 	RTE_FLOW_ACTION_TYPE_FLAG,
156 	RTE_FLOW_ACTION_TYPE_END,
157 };
158 
159 /** Graph of supported items and associated actions. */
160 static const struct mlx5_flow_items mlx5_flow_items[] = {
161 	[RTE_FLOW_ITEM_TYPE_END] = {
162 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH,
163 			       RTE_FLOW_ITEM_TYPE_VXLAN),
164 	},
165 	[RTE_FLOW_ITEM_TYPE_ETH] = {
166 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_VLAN,
167 			       RTE_FLOW_ITEM_TYPE_IPV4,
168 			       RTE_FLOW_ITEM_TYPE_IPV6),
169 		.actions = valid_actions,
170 		.mask = &(const struct rte_flow_item_eth){
171 			.dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
172 			.src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
173 			.type = -1,
174 		},
175 		.default_mask = &rte_flow_item_eth_mask,
176 		.mask_sz = sizeof(struct rte_flow_item_eth),
177 		.convert = mlx5_flow_create_eth,
178 		.dst_sz = sizeof(struct ibv_exp_flow_spec_eth),
179 	},
180 	[RTE_FLOW_ITEM_TYPE_VLAN] = {
181 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_IPV4,
182 			       RTE_FLOW_ITEM_TYPE_IPV6),
183 		.actions = valid_actions,
184 		.mask = &(const struct rte_flow_item_vlan){
185 			.tci = -1,
186 		},
187 		.default_mask = &rte_flow_item_vlan_mask,
188 		.mask_sz = sizeof(struct rte_flow_item_vlan),
189 		.convert = mlx5_flow_create_vlan,
190 		.dst_sz = 0,
191 	},
192 	[RTE_FLOW_ITEM_TYPE_IPV4] = {
193 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
194 			       RTE_FLOW_ITEM_TYPE_TCP),
195 		.actions = valid_actions,
196 		.mask = &(const struct rte_flow_item_ipv4){
197 			.hdr = {
198 				.src_addr = -1,
199 				.dst_addr = -1,
200 				.type_of_service = -1,
201 				.next_proto_id = -1,
202 			},
203 		},
204 		.default_mask = &rte_flow_item_ipv4_mask,
205 		.mask_sz = sizeof(struct rte_flow_item_ipv4),
206 		.convert = mlx5_flow_create_ipv4,
207 		.dst_sz = sizeof(struct ibv_exp_flow_spec_ipv4_ext),
208 	},
209 	[RTE_FLOW_ITEM_TYPE_IPV6] = {
210 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
211 			       RTE_FLOW_ITEM_TYPE_TCP),
212 		.actions = valid_actions,
213 		.mask = &(const struct rte_flow_item_ipv6){
214 			.hdr = {
215 				.src_addr = {
216 					0xff, 0xff, 0xff, 0xff,
217 					0xff, 0xff, 0xff, 0xff,
218 					0xff, 0xff, 0xff, 0xff,
219 					0xff, 0xff, 0xff, 0xff,
220 				},
221 				.dst_addr = {
222 					0xff, 0xff, 0xff, 0xff,
223 					0xff, 0xff, 0xff, 0xff,
224 					0xff, 0xff, 0xff, 0xff,
225 					0xff, 0xff, 0xff, 0xff,
226 				},
227 				.vtc_flow = -1,
228 				.proto = -1,
229 				.hop_limits = -1,
230 			},
231 		},
232 		.default_mask = &rte_flow_item_ipv6_mask,
233 		.mask_sz = sizeof(struct rte_flow_item_ipv6),
234 		.convert = mlx5_flow_create_ipv6,
235 		.dst_sz = sizeof(struct ibv_exp_flow_spec_ipv6_ext),
236 	},
237 	[RTE_FLOW_ITEM_TYPE_UDP] = {
238 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_VXLAN),
239 		.actions = valid_actions,
240 		.mask = &(const struct rte_flow_item_udp){
241 			.hdr = {
242 				.src_port = -1,
243 				.dst_port = -1,
244 			},
245 		},
246 		.default_mask = &rte_flow_item_udp_mask,
247 		.mask_sz = sizeof(struct rte_flow_item_udp),
248 		.convert = mlx5_flow_create_udp,
249 		.dst_sz = sizeof(struct ibv_exp_flow_spec_tcp_udp),
250 	},
251 	[RTE_FLOW_ITEM_TYPE_TCP] = {
252 		.actions = valid_actions,
253 		.mask = &(const struct rte_flow_item_tcp){
254 			.hdr = {
255 				.src_port = -1,
256 				.dst_port = -1,
257 			},
258 		},
259 		.default_mask = &rte_flow_item_tcp_mask,
260 		.mask_sz = sizeof(struct rte_flow_item_tcp),
261 		.convert = mlx5_flow_create_tcp,
262 		.dst_sz = sizeof(struct ibv_exp_flow_spec_tcp_udp),
263 	},
264 	[RTE_FLOW_ITEM_TYPE_VXLAN] = {
265 		.items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH),
266 		.actions = valid_actions,
267 		.mask = &(const struct rte_flow_item_vxlan){
268 			.vni = "\xff\xff\xff",
269 		},
270 		.default_mask = &rte_flow_item_vxlan_mask,
271 		.mask_sz = sizeof(struct rte_flow_item_vxlan),
272 		.convert = mlx5_flow_create_vxlan,
273 		.dst_sz = sizeof(struct ibv_exp_flow_spec_tunnel),
274 	},
275 };
276 
277 /** Structure to pass to the conversion function. */
278 struct mlx5_flow {
279 	struct ibv_exp_flow_attr *ibv_attr; /**< Verbs attribute. */
280 	unsigned int offset; /**< Offset in bytes in the ibv_attr buffer. */
281 	uint32_t inner; /**< Set once VXLAN is encountered. */
282 	uint64_t hash_fields; /**< Fields that participate in the hash. */
283 };
284 
285 /** Structure for Drop queue. */
286 struct rte_flow_drop {
287 	struct ibv_exp_rwq_ind_table *ind_table; /**< Indirection table. */
288 	struct ibv_qp *qp; /**< Verbs queue pair. */
289 	struct ibv_exp_wq *wqs[MLX5_DROP_WQ_N]; /**< Verbs work queue. */
290 	struct ibv_cq *cq; /**< Verbs completion queue. */
291 };
292 
293 struct mlx5_flow_action {
294 	uint32_t queue:1; /**< Target is a receive queue. */
295 	uint32_t drop:1; /**< Target is a drop queue. */
296 	uint32_t mark:1; /**< Mark is present in the flow. */
297 	uint32_t mark_id; /**< Mark identifier. */
298 	uint16_t queues[RTE_MAX_QUEUES_PER_PORT]; /**< Queues indexes to use. */
299 	uint16_t queues_n; /**< Number of entries in queue[]. */
300 };
301 
302 /**
303  * Check support for a given item.
304  *
305  * @param item[in]
306  *   Item specification.
307  * @param mask[in]
308  *   Bit-masks covering supported fields to compare with spec, last and mask in
309  *   \item.
310  * @param size
311  *   Bit-Mask size in bytes.
312  *
313  * @return
314  *   0 on success.
315  */
316 static int
317 mlx5_flow_item_validate(const struct rte_flow_item *item,
318 			const uint8_t *mask, unsigned int size)
319 {
320 	int ret = 0;
321 
322 	if (!item->spec && (item->mask || item->last))
323 		return -1;
324 	if (item->spec && !item->mask) {
325 		unsigned int i;
326 		const uint8_t *spec = item->spec;
327 
328 		for (i = 0; i < size; ++i)
329 			if ((spec[i] | mask[i]) != mask[i])
330 				return -1;
331 	}
332 	if (item->last && !item->mask) {
333 		unsigned int i;
334 		const uint8_t *spec = item->last;
335 
336 		for (i = 0; i < size; ++i)
337 			if ((spec[i] | mask[i]) != mask[i])
338 				return -1;
339 	}
340 	if (item->mask) {
341 		unsigned int i;
342 		const uint8_t *spec = item->mask;
343 
344 		for (i = 0; i < size; ++i)
345 			if ((spec[i] | mask[i]) != mask[i])
346 				return -1;
347 	}
348 	if (item->spec && item->last) {
349 		uint8_t spec[size];
350 		uint8_t last[size];
351 		const uint8_t *apply = mask;
352 		unsigned int i;
353 
354 		if (item->mask)
355 			apply = item->mask;
356 		for (i = 0; i < size; ++i) {
357 			spec[i] = ((const uint8_t *)item->spec)[i] & apply[i];
358 			last[i] = ((const uint8_t *)item->last)[i] & apply[i];
359 		}
360 		ret = memcmp(spec, last, size);
361 	}
362 	return ret;
363 }
364 
365 /**
366  * Validate a flow supported by the NIC.
367  *
368  * @param priv
369  *   Pointer to private structure.
370  * @param[in] attr
371  *   Flow rule attributes.
372  * @param[in] pattern
373  *   Pattern specification (list terminated by the END pattern item).
374  * @param[in] actions
375  *   Associated actions (list terminated by the END action).
376  * @param[out] error
377  *   Perform verbose error reporting if not NULL.
378  * @param[in, out] flow
379  *   Flow structure to update.
380  * @param[in, out] action
381  *   Action structure to update.
382  *
383  * @return
384  *   0 on success, a negative errno value otherwise and rte_errno is set.
385  */
386 static int
387 priv_flow_validate(struct priv *priv,
388 		   const struct rte_flow_attr *attr,
389 		   const struct rte_flow_item items[],
390 		   const struct rte_flow_action actions[],
391 		   struct rte_flow_error *error,
392 		   struct mlx5_flow *flow,
393 		   struct mlx5_flow_action *action)
394 {
395 	const struct mlx5_flow_items *cur_item = mlx5_flow_items;
396 
397 	(void)priv;
398 	if (attr->group) {
399 		rte_flow_error_set(error, ENOTSUP,
400 				   RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
401 				   NULL,
402 				   "groups are not supported");
403 		return -rte_errno;
404 	}
405 	if (attr->priority) {
406 		rte_flow_error_set(error, ENOTSUP,
407 				   RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
408 				   NULL,
409 				   "priorities are not supported");
410 		return -rte_errno;
411 	}
412 	if (attr->egress) {
413 		rte_flow_error_set(error, ENOTSUP,
414 				   RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
415 				   NULL,
416 				   "egress is not supported");
417 		return -rte_errno;
418 	}
419 	if (!attr->ingress) {
420 		rte_flow_error_set(error, ENOTSUP,
421 				   RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
422 				   NULL,
423 				   "only ingress is supported");
424 		return -rte_errno;
425 	}
426 	for (; items->type != RTE_FLOW_ITEM_TYPE_END; ++items) {
427 		const struct mlx5_flow_items *token = NULL;
428 		unsigned int i;
429 		int err;
430 
431 		if (items->type == RTE_FLOW_ITEM_TYPE_VOID)
432 			continue;
433 		for (i = 0;
434 		     cur_item->items &&
435 		     cur_item->items[i] != RTE_FLOW_ITEM_TYPE_END;
436 		     ++i) {
437 			if (cur_item->items[i] == items->type) {
438 				token = &mlx5_flow_items[items->type];
439 				break;
440 			}
441 		}
442 		if (!token)
443 			goto exit_item_not_supported;
444 		cur_item = token;
445 		err = mlx5_flow_item_validate(items,
446 					      (const uint8_t *)cur_item->mask,
447 					      cur_item->mask_sz);
448 		if (err)
449 			goto exit_item_not_supported;
450 		if (flow->ibv_attr && cur_item->convert) {
451 			err = cur_item->convert(items,
452 						(cur_item->default_mask ?
453 						 cur_item->default_mask :
454 						 cur_item->mask),
455 						flow);
456 			if (err)
457 				goto exit_item_not_supported;
458 		} else if (items->type == RTE_FLOW_ITEM_TYPE_VXLAN) {
459 			if (flow->inner) {
460 				rte_flow_error_set(error, ENOTSUP,
461 						   RTE_FLOW_ERROR_TYPE_ITEM,
462 						   items,
463 						   "cannot recognize multiple"
464 						   " VXLAN encapsulations");
465 				return -rte_errno;
466 			}
467 			flow->inner = 1;
468 		}
469 		flow->offset += cur_item->dst_sz;
470 	}
471 	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; ++actions) {
472 		if (actions->type == RTE_FLOW_ACTION_TYPE_VOID) {
473 			continue;
474 		} else if (actions->type == RTE_FLOW_ACTION_TYPE_DROP) {
475 			action->drop = 1;
476 		} else if (actions->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
477 			const struct rte_flow_action_queue *queue =
478 				(const struct rte_flow_action_queue *)
479 				actions->conf;
480 			uint16_t n;
481 			uint16_t found = 0;
482 
483 			if (!queue || (queue->index > (priv->rxqs_n - 1)))
484 				goto exit_action_not_supported;
485 			for (n = 0; n < action->queues_n; ++n) {
486 				if (action->queues[n] == queue->index) {
487 					found = 1;
488 					break;
489 				}
490 			}
491 			if (action->queues_n > 1 && !found) {
492 				rte_flow_error_set(error, ENOTSUP,
493 					   RTE_FLOW_ERROR_TYPE_ACTION,
494 					   actions,
495 					   "queue action not in RSS queues");
496 				return -rte_errno;
497 			}
498 			if (!found) {
499 				action->queue = 1;
500 				action->queues_n = 1;
501 				action->queues[0] = queue->index;
502 			}
503 		} else if (actions->type == RTE_FLOW_ACTION_TYPE_RSS) {
504 			const struct rte_flow_action_rss *rss =
505 				(const struct rte_flow_action_rss *)
506 				actions->conf;
507 			uint16_t n;
508 
509 			if (!rss || !rss->num) {
510 				rte_flow_error_set(error, EINVAL,
511 						   RTE_FLOW_ERROR_TYPE_ACTION,
512 						   actions,
513 						   "no valid queues");
514 				return -rte_errno;
515 			}
516 			if (action->queues_n == 1) {
517 				uint16_t found = 0;
518 
519 				assert(action->queues_n);
520 				for (n = 0; n < rss->num; ++n) {
521 					if (action->queues[0] ==
522 					    rss->queue[n]) {
523 						found = 1;
524 						break;
525 					}
526 				}
527 				if (!found) {
528 					rte_flow_error_set(error, ENOTSUP,
529 						   RTE_FLOW_ERROR_TYPE_ACTION,
530 						   actions,
531 						   "queue action not in RSS"
532 						   " queues");
533 					return -rte_errno;
534 				}
535 			}
536 			for (n = 0; n < rss->num; ++n) {
537 				if (rss->queue[n] >= priv->rxqs_n) {
538 					rte_flow_error_set(error, EINVAL,
539 						   RTE_FLOW_ERROR_TYPE_ACTION,
540 						   actions,
541 						   "queue id > number of"
542 						   " queues");
543 					return -rte_errno;
544 				}
545 			}
546 			action->queue = 1;
547 			for (n = 0; n < rss->num; ++n)
548 				action->queues[n] = rss->queue[n];
549 			action->queues_n = rss->num;
550 		} else if (actions->type == RTE_FLOW_ACTION_TYPE_MARK) {
551 			const struct rte_flow_action_mark *mark =
552 				(const struct rte_flow_action_mark *)
553 				actions->conf;
554 
555 			if (!mark) {
556 				rte_flow_error_set(error, EINVAL,
557 						   RTE_FLOW_ERROR_TYPE_ACTION,
558 						   actions,
559 						   "mark must be defined");
560 				return -rte_errno;
561 			} else if (mark->id >= MLX5_FLOW_MARK_MAX) {
562 				rte_flow_error_set(error, ENOTSUP,
563 						   RTE_FLOW_ERROR_TYPE_ACTION,
564 						   actions,
565 						   "mark must be between 0"
566 						   " and 16777199");
567 				return -rte_errno;
568 			}
569 			action->mark = 1;
570 			action->mark_id = mark->id;
571 		} else if (actions->type == RTE_FLOW_ACTION_TYPE_FLAG) {
572 			action->mark = 1;
573 		} else {
574 			goto exit_action_not_supported;
575 		}
576 	}
577 	if (action->mark && !flow->ibv_attr && !action->drop)
578 		flow->offset += sizeof(struct ibv_exp_flow_spec_action_tag);
579 	if (!action->queue && !action->drop) {
580 		rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
581 				   NULL, "no valid action");
582 		return -rte_errno;
583 	}
584 	return 0;
585 exit_item_not_supported:
586 	rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
587 			   items, "item not supported");
588 	return -rte_errno;
589 exit_action_not_supported:
590 	rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
591 			   actions, "action not supported");
592 	return -rte_errno;
593 }
594 
595 /**
596  * Validate a flow supported by the NIC.
597  *
598  * @see rte_flow_validate()
599  * @see rte_flow_ops
600  */
601 int
602 mlx5_flow_validate(struct rte_eth_dev *dev,
603 		   const struct rte_flow_attr *attr,
604 		   const struct rte_flow_item items[],
605 		   const struct rte_flow_action actions[],
606 		   struct rte_flow_error *error)
607 {
608 	struct priv *priv = dev->data->dev_private;
609 	int ret;
610 	struct mlx5_flow flow = { .offset = sizeof(struct ibv_exp_flow_attr) };
611 	struct mlx5_flow_action action = {
612 		.queue = 0,
613 		.drop = 0,
614 		.mark = 0,
615 		.mark_id = MLX5_FLOW_MARK_DEFAULT,
616 		.queues_n = 0,
617 	};
618 
619 	priv_lock(priv);
620 	ret = priv_flow_validate(priv, attr, items, actions, error, &flow,
621 				 &action);
622 	priv_unlock(priv);
623 	return ret;
624 }
625 
626 /**
627  * Convert Ethernet item to Verbs specification.
628  *
629  * @param item[in]
630  *   Item specification.
631  * @param default_mask[in]
632  *   Default bit-masks to use when item->mask is not provided.
633  * @param data[in, out]
634  *   User structure.
635  */
636 static int
637 mlx5_flow_create_eth(const struct rte_flow_item *item,
638 		     const void *default_mask,
639 		     void *data)
640 {
641 	const struct rte_flow_item_eth *spec = item->spec;
642 	const struct rte_flow_item_eth *mask = item->mask;
643 	struct mlx5_flow *flow = (struct mlx5_flow *)data;
644 	struct ibv_exp_flow_spec_eth *eth;
645 	const unsigned int eth_size = sizeof(struct ibv_exp_flow_spec_eth);
646 	unsigned int i;
647 
648 	++flow->ibv_attr->num_of_specs;
649 	flow->ibv_attr->priority = 2;
650 	flow->hash_fields = 0;
651 	eth = (void *)((uintptr_t)flow->ibv_attr + flow->offset);
652 	*eth = (struct ibv_exp_flow_spec_eth) {
653 		.type = flow->inner | IBV_EXP_FLOW_SPEC_ETH,
654 		.size = eth_size,
655 	};
656 	if (!spec)
657 		return 0;
658 	if (!mask)
659 		mask = default_mask;
660 	memcpy(eth->val.dst_mac, spec->dst.addr_bytes, ETHER_ADDR_LEN);
661 	memcpy(eth->val.src_mac, spec->src.addr_bytes, ETHER_ADDR_LEN);
662 	eth->val.ether_type = spec->type;
663 	memcpy(eth->mask.dst_mac, mask->dst.addr_bytes, ETHER_ADDR_LEN);
664 	memcpy(eth->mask.src_mac, mask->src.addr_bytes, ETHER_ADDR_LEN);
665 	eth->mask.ether_type = mask->type;
666 	/* Remove unwanted bits from values. */
667 	for (i = 0; i < ETHER_ADDR_LEN; ++i) {
668 		eth->val.dst_mac[i] &= eth->mask.dst_mac[i];
669 		eth->val.src_mac[i] &= eth->mask.src_mac[i];
670 	}
671 	eth->val.ether_type &= eth->mask.ether_type;
672 	return 0;
673 }
674 
675 /**
676  * Convert VLAN item to Verbs specification.
677  *
678  * @param item[in]
679  *   Item specification.
680  * @param default_mask[in]
681  *   Default bit-masks to use when item->mask is not provided.
682  * @param data[in, out]
683  *   User structure.
684  */
685 static int
686 mlx5_flow_create_vlan(const struct rte_flow_item *item,
687 		      const void *default_mask,
688 		      void *data)
689 {
690 	const struct rte_flow_item_vlan *spec = item->spec;
691 	const struct rte_flow_item_vlan *mask = item->mask;
692 	struct mlx5_flow *flow = (struct mlx5_flow *)data;
693 	struct ibv_exp_flow_spec_eth *eth;
694 	const unsigned int eth_size = sizeof(struct ibv_exp_flow_spec_eth);
695 
696 	eth = (void *)((uintptr_t)flow->ibv_attr + flow->offset - eth_size);
697 	if (!spec)
698 		return 0;
699 	if (!mask)
700 		mask = default_mask;
701 	eth->val.vlan_tag = spec->tci;
702 	eth->mask.vlan_tag = mask->tci;
703 	eth->val.vlan_tag &= eth->mask.vlan_tag;
704 	return 0;
705 }
706 
707 /**
708  * Convert IPv4 item to Verbs specification.
709  *
710  * @param item[in]
711  *   Item specification.
712  * @param default_mask[in]
713  *   Default bit-masks to use when item->mask is not provided.
714  * @param data[in, out]
715  *   User structure.
716  */
717 static int
718 mlx5_flow_create_ipv4(const struct rte_flow_item *item,
719 		      const void *default_mask,
720 		      void *data)
721 {
722 	const struct rte_flow_item_ipv4 *spec = item->spec;
723 	const struct rte_flow_item_ipv4 *mask = item->mask;
724 	struct mlx5_flow *flow = (struct mlx5_flow *)data;
725 	struct ibv_exp_flow_spec_ipv4_ext *ipv4;
726 	unsigned int ipv4_size = sizeof(struct ibv_exp_flow_spec_ipv4_ext);
727 
728 	++flow->ibv_attr->num_of_specs;
729 	flow->ibv_attr->priority = 1;
730 	flow->hash_fields = (IBV_EXP_RX_HASH_SRC_IPV4 |
731 			     IBV_EXP_RX_HASH_DST_IPV4);
732 	ipv4 = (void *)((uintptr_t)flow->ibv_attr + flow->offset);
733 	*ipv4 = (struct ibv_exp_flow_spec_ipv4_ext) {
734 		.type = flow->inner | IBV_EXP_FLOW_SPEC_IPV4_EXT,
735 		.size = ipv4_size,
736 	};
737 	if (!spec)
738 		return 0;
739 	if (!mask)
740 		mask = default_mask;
741 	ipv4->val = (struct ibv_exp_flow_ipv4_ext_filter){
742 		.src_ip = spec->hdr.src_addr,
743 		.dst_ip = spec->hdr.dst_addr,
744 		.proto = spec->hdr.next_proto_id,
745 		.tos = spec->hdr.type_of_service,
746 	};
747 	ipv4->mask = (struct ibv_exp_flow_ipv4_ext_filter){
748 		.src_ip = mask->hdr.src_addr,
749 		.dst_ip = mask->hdr.dst_addr,
750 		.proto = mask->hdr.next_proto_id,
751 		.tos = mask->hdr.type_of_service,
752 	};
753 	/* Remove unwanted bits from values. */
754 	ipv4->val.src_ip &= ipv4->mask.src_ip;
755 	ipv4->val.dst_ip &= ipv4->mask.dst_ip;
756 	ipv4->val.proto &= ipv4->mask.proto;
757 	ipv4->val.tos &= ipv4->mask.tos;
758 	return 0;
759 }
760 
761 /**
762  * Convert IPv6 item to Verbs specification.
763  *
764  * @param item[in]
765  *   Item specification.
766  * @param default_mask[in]
767  *   Default bit-masks to use when item->mask is not provided.
768  * @param data[in, out]
769  *   User structure.
770  */
771 static int
772 mlx5_flow_create_ipv6(const struct rte_flow_item *item,
773 		      const void *default_mask,
774 		      void *data)
775 {
776 	const struct rte_flow_item_ipv6 *spec = item->spec;
777 	const struct rte_flow_item_ipv6 *mask = item->mask;
778 	struct mlx5_flow *flow = (struct mlx5_flow *)data;
779 	struct ibv_exp_flow_spec_ipv6_ext *ipv6;
780 	unsigned int ipv6_size = sizeof(struct ibv_exp_flow_spec_ipv6_ext);
781 	unsigned int i;
782 
783 	++flow->ibv_attr->num_of_specs;
784 	flow->ibv_attr->priority = 1;
785 	flow->hash_fields = (IBV_EXP_RX_HASH_SRC_IPV6 |
786 			     IBV_EXP_RX_HASH_DST_IPV6);
787 	ipv6 = (void *)((uintptr_t)flow->ibv_attr + flow->offset);
788 	*ipv6 = (struct ibv_exp_flow_spec_ipv6_ext) {
789 		.type = flow->inner | IBV_EXP_FLOW_SPEC_IPV6_EXT,
790 		.size = ipv6_size,
791 	};
792 	if (!spec)
793 		return 0;
794 	if (!mask)
795 		mask = default_mask;
796 	memcpy(ipv6->val.src_ip, spec->hdr.src_addr,
797 	       RTE_DIM(ipv6->val.src_ip));
798 	memcpy(ipv6->val.dst_ip, spec->hdr.dst_addr,
799 	       RTE_DIM(ipv6->val.dst_ip));
800 	memcpy(ipv6->mask.src_ip, mask->hdr.src_addr,
801 	       RTE_DIM(ipv6->mask.src_ip));
802 	memcpy(ipv6->mask.dst_ip, mask->hdr.dst_addr,
803 	       RTE_DIM(ipv6->mask.dst_ip));
804 	ipv6->mask.flow_label = mask->hdr.vtc_flow;
805 	ipv6->mask.next_hdr = mask->hdr.proto;
806 	ipv6->mask.hop_limit = mask->hdr.hop_limits;
807 	/* Remove unwanted bits from values. */
808 	for (i = 0; i < RTE_DIM(ipv6->val.src_ip); ++i) {
809 		ipv6->val.src_ip[i] &= ipv6->mask.src_ip[i];
810 		ipv6->val.dst_ip[i] &= ipv6->mask.dst_ip[i];
811 	}
812 	ipv6->val.flow_label &= ipv6->mask.flow_label;
813 	ipv6->val.next_hdr &= ipv6->mask.next_hdr;
814 	ipv6->val.hop_limit &= ipv6->mask.hop_limit;
815 	return 0;
816 }
817 
818 /**
819  * Convert UDP item to Verbs specification.
820  *
821  * @param item[in]
822  *   Item specification.
823  * @param default_mask[in]
824  *   Default bit-masks to use when item->mask is not provided.
825  * @param data[in, out]
826  *   User structure.
827  */
828 static int
829 mlx5_flow_create_udp(const struct rte_flow_item *item,
830 		     const void *default_mask,
831 		     void *data)
832 {
833 	const struct rte_flow_item_udp *spec = item->spec;
834 	const struct rte_flow_item_udp *mask = item->mask;
835 	struct mlx5_flow *flow = (struct mlx5_flow *)data;
836 	struct ibv_exp_flow_spec_tcp_udp *udp;
837 	unsigned int udp_size = sizeof(struct ibv_exp_flow_spec_tcp_udp);
838 
839 	++flow->ibv_attr->num_of_specs;
840 	flow->ibv_attr->priority = 0;
841 	flow->hash_fields |= (IBV_EXP_RX_HASH_SRC_PORT_UDP |
842 			      IBV_EXP_RX_HASH_DST_PORT_UDP);
843 	udp = (void *)((uintptr_t)flow->ibv_attr + flow->offset);
844 	*udp = (struct ibv_exp_flow_spec_tcp_udp) {
845 		.type = flow->inner | IBV_EXP_FLOW_SPEC_UDP,
846 		.size = udp_size,
847 	};
848 	if (!spec)
849 		return 0;
850 	if (!mask)
851 		mask = default_mask;
852 	udp->val.dst_port = spec->hdr.dst_port;
853 	udp->val.src_port = spec->hdr.src_port;
854 	udp->mask.dst_port = mask->hdr.dst_port;
855 	udp->mask.src_port = mask->hdr.src_port;
856 	/* Remove unwanted bits from values. */
857 	udp->val.src_port &= udp->mask.src_port;
858 	udp->val.dst_port &= udp->mask.dst_port;
859 	return 0;
860 }
861 
862 /**
863  * Convert TCP item to Verbs specification.
864  *
865  * @param item[in]
866  *   Item specification.
867  * @param default_mask[in]
868  *   Default bit-masks to use when item->mask is not provided.
869  * @param data[in, out]
870  *   User structure.
871  */
872 static int
873 mlx5_flow_create_tcp(const struct rte_flow_item *item,
874 		     const void *default_mask,
875 		     void *data)
876 {
877 	const struct rte_flow_item_tcp *spec = item->spec;
878 	const struct rte_flow_item_tcp *mask = item->mask;
879 	struct mlx5_flow *flow = (struct mlx5_flow *)data;
880 	struct ibv_exp_flow_spec_tcp_udp *tcp;
881 	unsigned int tcp_size = sizeof(struct ibv_exp_flow_spec_tcp_udp);
882 
883 	++flow->ibv_attr->num_of_specs;
884 	flow->ibv_attr->priority = 0;
885 	flow->hash_fields |= (IBV_EXP_RX_HASH_SRC_PORT_TCP |
886 			      IBV_EXP_RX_HASH_DST_PORT_TCP);
887 	tcp = (void *)((uintptr_t)flow->ibv_attr + flow->offset);
888 	*tcp = (struct ibv_exp_flow_spec_tcp_udp) {
889 		.type = flow->inner | IBV_EXP_FLOW_SPEC_TCP,
890 		.size = tcp_size,
891 	};
892 	if (!spec)
893 		return 0;
894 	if (!mask)
895 		mask = default_mask;
896 	tcp->val.dst_port = spec->hdr.dst_port;
897 	tcp->val.src_port = spec->hdr.src_port;
898 	tcp->mask.dst_port = mask->hdr.dst_port;
899 	tcp->mask.src_port = mask->hdr.src_port;
900 	/* Remove unwanted bits from values. */
901 	tcp->val.src_port &= tcp->mask.src_port;
902 	tcp->val.dst_port &= tcp->mask.dst_port;
903 	return 0;
904 }
905 
906 /**
907  * Convert VXLAN item to Verbs specification.
908  *
909  * @param item[in]
910  *   Item specification.
911  * @param default_mask[in]
912  *   Default bit-masks to use when item->mask is not provided.
913  * @param data[in, out]
914  *   User structure.
915  */
916 static int
917 mlx5_flow_create_vxlan(const struct rte_flow_item *item,
918 		       const void *default_mask,
919 		       void *data)
920 {
921 	const struct rte_flow_item_vxlan *spec = item->spec;
922 	const struct rte_flow_item_vxlan *mask = item->mask;
923 	struct mlx5_flow *flow = (struct mlx5_flow *)data;
924 	struct ibv_exp_flow_spec_tunnel *vxlan;
925 	unsigned int size = sizeof(struct ibv_exp_flow_spec_tunnel);
926 	union vni {
927 		uint32_t vlan_id;
928 		uint8_t vni[4];
929 	} id;
930 
931 	++flow->ibv_attr->num_of_specs;
932 	flow->ibv_attr->priority = 0;
933 	id.vni[0] = 0;
934 	vxlan = (void *)((uintptr_t)flow->ibv_attr + flow->offset);
935 	*vxlan = (struct ibv_exp_flow_spec_tunnel) {
936 		.type = flow->inner | IBV_EXP_FLOW_SPEC_VXLAN_TUNNEL,
937 		.size = size,
938 	};
939 	flow->inner = IBV_EXP_FLOW_SPEC_INNER;
940 	if (!spec)
941 		return 0;
942 	if (!mask)
943 		mask = default_mask;
944 	memcpy(&id.vni[1], spec->vni, 3);
945 	vxlan->val.tunnel_id = id.vlan_id;
946 	memcpy(&id.vni[1], mask->vni, 3);
947 	vxlan->mask.tunnel_id = id.vlan_id;
948 	/* Remove unwanted bits from values. */
949 	vxlan->val.tunnel_id &= vxlan->mask.tunnel_id;
950 	return 0;
951 }
952 
953 /**
954  * Convert mark/flag action to Verbs specification.
955  *
956  * @param flow
957  *   Pointer to MLX5 flow structure.
958  * @param mark_id
959  *   Mark identifier.
960  */
961 static int
962 mlx5_flow_create_flag_mark(struct mlx5_flow *flow, uint32_t mark_id)
963 {
964 	struct ibv_exp_flow_spec_action_tag *tag;
965 	unsigned int size = sizeof(struct ibv_exp_flow_spec_action_tag);
966 
967 	tag = (void *)((uintptr_t)flow->ibv_attr + flow->offset);
968 	*tag = (struct ibv_exp_flow_spec_action_tag){
969 		.type = IBV_EXP_FLOW_SPEC_ACTION_TAG,
970 		.size = size,
971 		.tag_id = mlx5_flow_mark_set(mark_id),
972 	};
973 	++flow->ibv_attr->num_of_specs;
974 	return 0;
975 }
976 
977 /**
978  * Complete flow rule creation with a drop queue.
979  *
980  * @param priv
981  *   Pointer to private structure.
982  * @param flow
983  *   MLX5 flow attributes (filled by mlx5_flow_validate()).
984  * @param[out] error
985  *   Perform verbose error reporting if not NULL.
986  *
987  * @return
988  *   A flow if the rule could be created.
989  */
990 static struct rte_flow *
991 priv_flow_create_action_queue_drop(struct priv *priv,
992 				   struct mlx5_flow *flow,
993 				   struct rte_flow_error *error)
994 {
995 	struct rte_flow *rte_flow;
996 
997 	assert(priv->pd);
998 	assert(priv->ctx);
999 	rte_flow = rte_calloc(__func__, 1, sizeof(*rte_flow), 0);
1000 	if (!rte_flow) {
1001 		rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1002 				   NULL, "cannot allocate flow memory");
1003 		return NULL;
1004 	}
1005 	rte_flow->drop = 1;
1006 	rte_flow->ibv_attr = flow->ibv_attr;
1007 	rte_flow->qp = priv->flow_drop_queue->qp;
1008 	if (!priv->started)
1009 		return rte_flow;
1010 	rte_flow->ibv_flow = ibv_exp_create_flow(rte_flow->qp,
1011 						 rte_flow->ibv_attr);
1012 	if (!rte_flow->ibv_flow) {
1013 		rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1014 				   NULL, "flow rule creation failure");
1015 		goto error;
1016 	}
1017 	return rte_flow;
1018 error:
1019 	assert(rte_flow);
1020 	rte_free(rte_flow);
1021 	return NULL;
1022 }
1023 
1024 /**
1025  * Complete flow rule creation.
1026  *
1027  * @param priv
1028  *   Pointer to private structure.
1029  * @param flow
1030  *   MLX5 flow attributes (filled by mlx5_flow_validate()).
1031  * @param action
1032  *   Target action structure.
1033  * @param[out] error
1034  *   Perform verbose error reporting if not NULL.
1035  *
1036  * @return
1037  *   A flow if the rule could be created.
1038  */
1039 static struct rte_flow *
1040 priv_flow_create_action_queue(struct priv *priv,
1041 			      struct mlx5_flow *flow,
1042 			      struct mlx5_flow_action *action,
1043 			      struct rte_flow_error *error)
1044 {
1045 	struct rte_flow *rte_flow;
1046 	unsigned int i;
1047 	unsigned int j;
1048 	const unsigned int wqs_n = 1 << log2above(action->queues_n);
1049 	struct ibv_exp_wq *wqs[wqs_n];
1050 
1051 	assert(priv->pd);
1052 	assert(priv->ctx);
1053 	assert(!action->drop);
1054 	rte_flow = rte_calloc(__func__, 1, sizeof(*rte_flow) +
1055 			      sizeof(*rte_flow->rxqs) * action->queues_n, 0);
1056 	if (!rte_flow) {
1057 		rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1058 				   NULL, "cannot allocate flow memory");
1059 		return NULL;
1060 	}
1061 	for (i = 0; i < action->queues_n; ++i) {
1062 		struct rxq_ctrl *rxq;
1063 
1064 		rxq = container_of((*priv->rxqs)[action->queues[i]],
1065 				   struct rxq_ctrl, rxq);
1066 		wqs[i] = rxq->wq;
1067 		rte_flow->rxqs[i] = &rxq->rxq;
1068 		++rte_flow->rxqs_n;
1069 		rxq->rxq.mark |= action->mark;
1070 	}
1071 	/* finalise indirection table. */
1072 	for (j = 0; i < wqs_n; ++i, ++j) {
1073 		wqs[i] = wqs[j];
1074 		if (j == action->queues_n)
1075 			j = 0;
1076 	}
1077 	rte_flow->mark = action->mark;
1078 	rte_flow->ibv_attr = flow->ibv_attr;
1079 	rte_flow->hash_fields = flow->hash_fields;
1080 	rte_flow->ind_table = ibv_exp_create_rwq_ind_table(
1081 		priv->ctx,
1082 		&(struct ibv_exp_rwq_ind_table_init_attr){
1083 			.pd = priv->pd,
1084 			.log_ind_tbl_size = log2above(action->queues_n),
1085 			.ind_tbl = wqs,
1086 			.comp_mask = 0,
1087 		});
1088 	if (!rte_flow->ind_table) {
1089 		rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1090 				   NULL, "cannot allocate indirection table");
1091 		goto error;
1092 	}
1093 	rte_flow->qp = ibv_exp_create_qp(
1094 		priv->ctx,
1095 		&(struct ibv_exp_qp_init_attr){
1096 			.qp_type = IBV_QPT_RAW_PACKET,
1097 			.comp_mask =
1098 				IBV_EXP_QP_INIT_ATTR_PD |
1099 				IBV_EXP_QP_INIT_ATTR_PORT |
1100 				IBV_EXP_QP_INIT_ATTR_RX_HASH,
1101 			.pd = priv->pd,
1102 			.rx_hash_conf = &(struct ibv_exp_rx_hash_conf){
1103 				.rx_hash_function =
1104 					IBV_EXP_RX_HASH_FUNC_TOEPLITZ,
1105 				.rx_hash_key_len = rss_hash_default_key_len,
1106 				.rx_hash_key = rss_hash_default_key,
1107 				.rx_hash_fields_mask = rte_flow->hash_fields,
1108 				.rwq_ind_tbl = rte_flow->ind_table,
1109 			},
1110 			.port_num = priv->port,
1111 		});
1112 	if (!rte_flow->qp) {
1113 		rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1114 				   NULL, "cannot allocate QP");
1115 		goto error;
1116 	}
1117 	if (!priv->started)
1118 		return rte_flow;
1119 	rte_flow->ibv_flow = ibv_exp_create_flow(rte_flow->qp,
1120 						 rte_flow->ibv_attr);
1121 	if (!rte_flow->ibv_flow) {
1122 		rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1123 				   NULL, "flow rule creation failure");
1124 		goto error;
1125 	}
1126 	return rte_flow;
1127 error:
1128 	assert(rte_flow);
1129 	if (rte_flow->qp)
1130 		ibv_destroy_qp(rte_flow->qp);
1131 	if (rte_flow->ind_table)
1132 		ibv_exp_destroy_rwq_ind_table(rte_flow->ind_table);
1133 	rte_free(rte_flow);
1134 	return NULL;
1135 }
1136 
1137 /**
1138  * Convert a flow.
1139  *
1140  * @param priv
1141  *   Pointer to private structure.
1142  * @param[in] attr
1143  *   Flow rule attributes.
1144  * @param[in] pattern
1145  *   Pattern specification (list terminated by the END pattern item).
1146  * @param[in] actions
1147  *   Associated actions (list terminated by the END action).
1148  * @param[out] error
1149  *   Perform verbose error reporting if not NULL.
1150  *
1151  * @return
1152  *   A flow on success, NULL otherwise.
1153  */
1154 static struct rte_flow *
1155 priv_flow_create(struct priv *priv,
1156 		 const struct rte_flow_attr *attr,
1157 		 const struct rte_flow_item items[],
1158 		 const struct rte_flow_action actions[],
1159 		 struct rte_flow_error *error)
1160 {
1161 	struct rte_flow *rte_flow;
1162 	struct mlx5_flow flow = { .offset = sizeof(struct ibv_exp_flow_attr), };
1163 	struct mlx5_flow_action action = {
1164 		.queue = 0,
1165 		.drop = 0,
1166 		.mark = 0,
1167 		.mark_id = MLX5_FLOW_MARK_DEFAULT,
1168 		.queues_n = 0,
1169 	};
1170 	int err;
1171 
1172 	err = priv_flow_validate(priv, attr, items, actions, error, &flow,
1173 				 &action);
1174 	if (err)
1175 		goto exit;
1176 	flow.ibv_attr = rte_malloc(__func__, flow.offset, 0);
1177 	flow.offset = sizeof(struct ibv_exp_flow_attr);
1178 	if (!flow.ibv_attr) {
1179 		rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1180 				   NULL, "cannot allocate ibv_attr memory");
1181 		goto exit;
1182 	}
1183 	*flow.ibv_attr = (struct ibv_exp_flow_attr){
1184 		.type = IBV_EXP_FLOW_ATTR_NORMAL,
1185 		.size = sizeof(struct ibv_exp_flow_attr),
1186 		.priority = attr->priority,
1187 		.num_of_specs = 0,
1188 		.port = 0,
1189 		.flags = 0,
1190 		.reserved = 0,
1191 	};
1192 	flow.inner = 0;
1193 	flow.hash_fields = 0;
1194 	claim_zero(priv_flow_validate(priv, attr, items, actions,
1195 				      error, &flow, &action));
1196 	if (action.mark && !action.drop) {
1197 		mlx5_flow_create_flag_mark(&flow, action.mark_id);
1198 		flow.offset += sizeof(struct ibv_exp_flow_spec_action_tag);
1199 	}
1200 	if (action.drop)
1201 		rte_flow =
1202 			priv_flow_create_action_queue_drop(priv, &flow, error);
1203 	else
1204 		rte_flow = priv_flow_create_action_queue(priv, &flow, &action,
1205 							 error);
1206 	if (!rte_flow)
1207 		goto exit;
1208 	return rte_flow;
1209 exit:
1210 	rte_free(flow.ibv_attr);
1211 	return NULL;
1212 }
1213 
1214 /**
1215  * Create a flow.
1216  *
1217  * @see rte_flow_create()
1218  * @see rte_flow_ops
1219  */
1220 struct rte_flow *
1221 mlx5_flow_create(struct rte_eth_dev *dev,
1222 		 const struct rte_flow_attr *attr,
1223 		 const struct rte_flow_item items[],
1224 		 const struct rte_flow_action actions[],
1225 		 struct rte_flow_error *error)
1226 {
1227 	struct priv *priv = dev->data->dev_private;
1228 	struct rte_flow *flow;
1229 
1230 	priv_lock(priv);
1231 	flow = priv_flow_create(priv, attr, items, actions, error);
1232 	if (flow) {
1233 		LIST_INSERT_HEAD(&priv->flows, flow, next);
1234 		DEBUG("Flow created %p", (void *)flow);
1235 	}
1236 	priv_unlock(priv);
1237 	return flow;
1238 }
1239 
1240 /**
1241  * Destroy a flow.
1242  *
1243  * @param priv
1244  *   Pointer to private structure.
1245  * @param[in] flow
1246  *   Flow to destroy.
1247  */
1248 static void
1249 priv_flow_destroy(struct priv *priv,
1250 		  struct rte_flow *flow)
1251 {
1252 	(void)priv;
1253 	LIST_REMOVE(flow, next);
1254 	if (flow->ibv_flow)
1255 		claim_zero(ibv_exp_destroy_flow(flow->ibv_flow));
1256 	if (flow->drop)
1257 		goto free;
1258 	if (flow->qp)
1259 		claim_zero(ibv_destroy_qp(flow->qp));
1260 	if (flow->ind_table)
1261 		claim_zero(ibv_exp_destroy_rwq_ind_table(flow->ind_table));
1262 	if (flow->drop && flow->wq)
1263 		claim_zero(ibv_exp_destroy_wq(flow->wq));
1264 	if (flow->drop && flow->cq)
1265 		claim_zero(ibv_destroy_cq(flow->cq));
1266 	if (flow->mark) {
1267 		struct rte_flow *tmp;
1268 		struct rxq *rxq;
1269 		uint32_t mark_n = 0;
1270 		uint32_t queue_n;
1271 
1272 		/*
1273 		 * To remove the mark from the queue, the queue must not be
1274 		 * present in any other marked flow (RSS or not).
1275 		 */
1276 		for (queue_n = 0; queue_n < flow->rxqs_n; ++queue_n) {
1277 			rxq = flow->rxqs[queue_n];
1278 			for (tmp = LIST_FIRST(&priv->flows);
1279 			     tmp;
1280 			     tmp = LIST_NEXT(tmp, next)) {
1281 				uint32_t tqueue_n;
1282 
1283 				if (tmp->drop)
1284 					continue;
1285 				for (tqueue_n = 0;
1286 				     tqueue_n < tmp->rxqs_n;
1287 				     ++tqueue_n) {
1288 					struct rxq *trxq;
1289 
1290 					trxq = tmp->rxqs[tqueue_n];
1291 					if (rxq == trxq)
1292 						++mark_n;
1293 				}
1294 			}
1295 			rxq->mark = !!mark_n;
1296 		}
1297 	}
1298 free:
1299 	rte_free(flow->ibv_attr);
1300 	DEBUG("Flow destroyed %p", (void *)flow);
1301 	rte_free(flow);
1302 }
1303 
1304 /**
1305  * Destroy a flow.
1306  *
1307  * @see rte_flow_destroy()
1308  * @see rte_flow_ops
1309  */
1310 int
1311 mlx5_flow_destroy(struct rte_eth_dev *dev,
1312 		  struct rte_flow *flow,
1313 		  struct rte_flow_error *error)
1314 {
1315 	struct priv *priv = dev->data->dev_private;
1316 
1317 	(void)error;
1318 	priv_lock(priv);
1319 	priv_flow_destroy(priv, flow);
1320 	priv_unlock(priv);
1321 	return 0;
1322 }
1323 
1324 /**
1325  * Destroy all flows.
1326  *
1327  * @param priv
1328  *   Pointer to private structure.
1329  */
1330 static void
1331 priv_flow_flush(struct priv *priv)
1332 {
1333 	while (!LIST_EMPTY(&priv->flows)) {
1334 		struct rte_flow *flow;
1335 
1336 		flow = LIST_FIRST(&priv->flows);
1337 		priv_flow_destroy(priv, flow);
1338 	}
1339 }
1340 
1341 /**
1342  * Destroy all flows.
1343  *
1344  * @see rte_flow_flush()
1345  * @see rte_flow_ops
1346  */
1347 int
1348 mlx5_flow_flush(struct rte_eth_dev *dev,
1349 		struct rte_flow_error *error)
1350 {
1351 	struct priv *priv = dev->data->dev_private;
1352 
1353 	(void)error;
1354 	priv_lock(priv);
1355 	priv_flow_flush(priv);
1356 	priv_unlock(priv);
1357 	return 0;
1358 }
1359 
1360 /**
1361  * Create drop queue.
1362  *
1363  * @param priv
1364  *   Pointer to private structure.
1365  *
1366  * @return
1367  *   0 on success.
1368  */
1369 static int
1370 priv_flow_create_drop_queue(struct priv *priv)
1371 {
1372 	struct rte_flow_drop *fdq = NULL;
1373 	unsigned int i;
1374 
1375 	assert(priv->pd);
1376 	assert(priv->ctx);
1377 	fdq = rte_calloc(__func__, 1, sizeof(*fdq), 0);
1378 	if (!fdq) {
1379 		WARN("cannot allocate memory for drop queue");
1380 		goto error;
1381 	}
1382 	fdq->cq = ibv_exp_create_cq(priv->ctx, 1, NULL, NULL, 0,
1383 			&(struct ibv_exp_cq_init_attr){
1384 			.comp_mask = 0,
1385 			});
1386 	if (!fdq->cq) {
1387 		WARN("cannot allocate CQ for drop queue");
1388 		goto error;
1389 	}
1390 	for (i = 0; i != MLX5_DROP_WQ_N; ++i) {
1391 		fdq->wqs[i] = ibv_exp_create_wq(priv->ctx,
1392 				&(struct ibv_exp_wq_init_attr){
1393 				.wq_type = IBV_EXP_WQT_RQ,
1394 				.max_recv_wr = 1,
1395 				.max_recv_sge = 1,
1396 				.pd = priv->pd,
1397 				.cq = fdq->cq,
1398 				});
1399 		if (!fdq->wqs[i]) {
1400 			WARN("cannot allocate WQ for drop queue");
1401 			goto error;
1402 		}
1403 	}
1404 	fdq->ind_table = ibv_exp_create_rwq_ind_table(priv->ctx,
1405 			&(struct ibv_exp_rwq_ind_table_init_attr){
1406 			.pd = priv->pd,
1407 			.log_ind_tbl_size = 0,
1408 			.ind_tbl = fdq->wqs,
1409 			.comp_mask = 0,
1410 			});
1411 	if (!fdq->ind_table) {
1412 		WARN("cannot allocate indirection table for drop queue");
1413 		goto error;
1414 	}
1415 	fdq->qp = ibv_exp_create_qp(priv->ctx,
1416 		&(struct ibv_exp_qp_init_attr){
1417 			.qp_type = IBV_QPT_RAW_PACKET,
1418 			.comp_mask =
1419 				IBV_EXP_QP_INIT_ATTR_PD |
1420 				IBV_EXP_QP_INIT_ATTR_PORT |
1421 				IBV_EXP_QP_INIT_ATTR_RX_HASH,
1422 			.pd = priv->pd,
1423 			.rx_hash_conf = &(struct ibv_exp_rx_hash_conf){
1424 				.rx_hash_function =
1425 					IBV_EXP_RX_HASH_FUNC_TOEPLITZ,
1426 				.rx_hash_key_len = rss_hash_default_key_len,
1427 				.rx_hash_key = rss_hash_default_key,
1428 				.rx_hash_fields_mask = 0,
1429 				.rwq_ind_tbl = fdq->ind_table,
1430 				},
1431 			.port_num = priv->port,
1432 			});
1433 	if (!fdq->qp) {
1434 		WARN("cannot allocate QP for drop queue");
1435 		goto error;
1436 	}
1437 	priv->flow_drop_queue = fdq;
1438 	return 0;
1439 error:
1440 	if (fdq->qp)
1441 		claim_zero(ibv_destroy_qp(fdq->qp));
1442 	if (fdq->ind_table)
1443 		claim_zero(ibv_exp_destroy_rwq_ind_table(fdq->ind_table));
1444 	for (i = 0; i != MLX5_DROP_WQ_N; ++i) {
1445 		if (fdq->wqs[i])
1446 			claim_zero(ibv_exp_destroy_wq(fdq->wqs[i]));
1447 	}
1448 	if (fdq->cq)
1449 		claim_zero(ibv_destroy_cq(fdq->cq));
1450 	if (fdq)
1451 		rte_free(fdq);
1452 	priv->flow_drop_queue = NULL;
1453 	return -1;
1454 }
1455 
1456 /**
1457  * Delete drop queue.
1458  *
1459  * @param priv
1460  *   Pointer to private structure.
1461  */
1462 static void
1463 priv_flow_delete_drop_queue(struct priv *priv)
1464 {
1465 	struct rte_flow_drop *fdq = priv->flow_drop_queue;
1466 	unsigned int i;
1467 
1468 	if (!fdq)
1469 		return;
1470 	if (fdq->qp)
1471 		claim_zero(ibv_destroy_qp(fdq->qp));
1472 	if (fdq->ind_table)
1473 		claim_zero(ibv_exp_destroy_rwq_ind_table(fdq->ind_table));
1474 	for (i = 0; i != MLX5_DROP_WQ_N; ++i) {
1475 		if (fdq->wqs[i])
1476 			claim_zero(ibv_exp_destroy_wq(fdq->wqs[i]));
1477 	}
1478 	if (fdq->cq)
1479 		claim_zero(ibv_destroy_cq(fdq->cq));
1480 	rte_free(fdq);
1481 	priv->flow_drop_queue = NULL;
1482 }
1483 
1484 /**
1485  * Remove all flows.
1486  *
1487  * Called by dev_stop() to remove all flows.
1488  *
1489  * @param priv
1490  *   Pointer to private structure.
1491  */
1492 void
1493 priv_flow_stop(struct priv *priv)
1494 {
1495 	struct rte_flow *flow;
1496 
1497 	for (flow = LIST_FIRST(&priv->flows);
1498 	     flow;
1499 	     flow = LIST_NEXT(flow, next)) {
1500 		claim_zero(ibv_exp_destroy_flow(flow->ibv_flow));
1501 		flow->ibv_flow = NULL;
1502 		if (flow->mark) {
1503 			unsigned int n;
1504 
1505 			for (n = 0; n < flow->rxqs_n; ++n)
1506 				flow->rxqs[n]->mark = 0;
1507 		}
1508 		DEBUG("Flow %p removed", (void *)flow);
1509 	}
1510 	priv_flow_delete_drop_queue(priv);
1511 }
1512 
1513 /**
1514  * Add all flows.
1515  *
1516  * @param priv
1517  *   Pointer to private structure.
1518  *
1519  * @return
1520  *   0 on success, a errno value otherwise and rte_errno is set.
1521  */
1522 int
1523 priv_flow_start(struct priv *priv)
1524 {
1525 	int ret;
1526 	struct rte_flow *flow;
1527 
1528 	ret = priv_flow_create_drop_queue(priv);
1529 	if (ret)
1530 		return -1;
1531 	for (flow = LIST_FIRST(&priv->flows);
1532 	     flow;
1533 	     flow = LIST_NEXT(flow, next)) {
1534 		struct ibv_qp *qp;
1535 
1536 		if (flow->drop)
1537 			qp = priv->flow_drop_queue->qp;
1538 		else
1539 			qp = flow->qp;
1540 		flow->ibv_flow = ibv_exp_create_flow(qp, flow->ibv_attr);
1541 		if (!flow->ibv_flow) {
1542 			DEBUG("Flow %p cannot be applied", (void *)flow);
1543 			rte_errno = EINVAL;
1544 			return rte_errno;
1545 		}
1546 		DEBUG("Flow %p applied", (void *)flow);
1547 		if (flow->mark) {
1548 			unsigned int n;
1549 
1550 			for (n = 0; n < flow->rxqs_n; ++n)
1551 				flow->rxqs[n]->mark = 1;
1552 		}
1553 	}
1554 	return 0;
1555 }
1556 
1557 /**
1558  * Verify if the Rx queue is used in a flow.
1559  *
1560  * @param priv
1561  *   Pointer to private structure.
1562  * @param rxq
1563  *   Pointer to the queue to search.
1564  *
1565  * @return
1566  *   Nonzero if the queue is used by a flow.
1567  */
1568 int
1569 priv_flow_rxq_in_use(struct priv *priv, struct rxq *rxq)
1570 {
1571 	struct rte_flow *flow;
1572 
1573 	for (flow = LIST_FIRST(&priv->flows);
1574 	     flow;
1575 	     flow = LIST_NEXT(flow, next)) {
1576 		unsigned int n;
1577 
1578 		if (flow->drop)
1579 			continue;
1580 		for (n = 0; n < flow->rxqs_n; ++n) {
1581 			if (flow->rxqs[n] == rxq)
1582 				return 1;
1583 		}
1584 	}
1585 	return 0;
1586 }
1587