xref: /dpdk/drivers/net/i40e/i40e_flow.c (revision df2b82ca)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2016 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/queue.h>
34 #include <stdio.h>
35 #include <errno.h>
36 #include <stdint.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <stdarg.h>
40 
41 #include <rte_ether.h>
42 #include <rte_ethdev.h>
43 #include <rte_log.h>
44 #include <rte_memzone.h>
45 #include <rte_malloc.h>
46 #include <rte_eth_ctrl.h>
47 #include <rte_tailq.h>
48 #include <rte_flow_driver.h>
49 
50 #include "i40e_logs.h"
51 #include "base/i40e_type.h"
52 #include "base/i40e_prototype.h"
53 #include "i40e_ethdev.h"
54 
55 #define I40E_IPV4_TC_SHIFT	4
56 #define I40E_IPV6_TC_MASK	(0x00FF << I40E_IPV4_TC_SHIFT)
57 #define I40E_IPV6_FRAG_HEADER	44
58 #define I40E_TENANT_ARRAY_NUM	3
59 #define I40E_TCI_MASK		0xFFFF
60 
61 static int i40e_flow_validate(struct rte_eth_dev *dev,
62 			      const struct rte_flow_attr *attr,
63 			      const struct rte_flow_item pattern[],
64 			      const struct rte_flow_action actions[],
65 			      struct rte_flow_error *error);
66 static struct rte_flow *i40e_flow_create(struct rte_eth_dev *dev,
67 					 const struct rte_flow_attr *attr,
68 					 const struct rte_flow_item pattern[],
69 					 const struct rte_flow_action actions[],
70 					 struct rte_flow_error *error);
71 static int i40e_flow_destroy(struct rte_eth_dev *dev,
72 			     struct rte_flow *flow,
73 			     struct rte_flow_error *error);
74 static int i40e_flow_flush(struct rte_eth_dev *dev,
75 			   struct rte_flow_error *error);
76 static int
77 i40e_flow_parse_ethertype_pattern(struct rte_eth_dev *dev,
78 				  const struct rte_flow_item *pattern,
79 				  struct rte_flow_error *error,
80 				  struct rte_eth_ethertype_filter *filter);
81 static int i40e_flow_parse_ethertype_action(struct rte_eth_dev *dev,
82 				    const struct rte_flow_action *actions,
83 				    struct rte_flow_error *error,
84 				    struct rte_eth_ethertype_filter *filter);
85 static int i40e_flow_parse_fdir_pattern(struct rte_eth_dev *dev,
86 					const struct rte_flow_item *pattern,
87 					struct rte_flow_error *error,
88 					struct rte_eth_fdir_filter *filter);
89 static int i40e_flow_parse_fdir_action(struct rte_eth_dev *dev,
90 				       const struct rte_flow_action *actions,
91 				       struct rte_flow_error *error,
92 				       struct rte_eth_fdir_filter *filter);
93 static int i40e_flow_parse_tunnel_pattern(__rte_unused struct rte_eth_dev *dev,
94 				  const struct rte_flow_item *pattern,
95 				  struct rte_flow_error *error,
96 				  struct rte_eth_tunnel_filter_conf *filter);
97 static int i40e_flow_parse_tunnel_action(struct rte_eth_dev *dev,
98 				 const struct rte_flow_action *actions,
99 				 struct rte_flow_error *error,
100 				 struct rte_eth_tunnel_filter_conf *filter);
101 static int i40e_flow_parse_attr(const struct rte_flow_attr *attr,
102 				struct rte_flow_error *error);
103 static int i40e_flow_parse_ethertype_filter(struct rte_eth_dev *dev,
104 				    const struct rte_flow_attr *attr,
105 				    const struct rte_flow_item pattern[],
106 				    const struct rte_flow_action actions[],
107 				    struct rte_flow_error *error,
108 				    union i40e_filter_t *filter);
109 static int i40e_flow_parse_fdir_filter(struct rte_eth_dev *dev,
110 				       const struct rte_flow_attr *attr,
111 				       const struct rte_flow_item pattern[],
112 				       const struct rte_flow_action actions[],
113 				       struct rte_flow_error *error,
114 				       union i40e_filter_t *filter);
115 static int i40e_flow_parse_tunnel_filter(struct rte_eth_dev *dev,
116 					 const struct rte_flow_attr *attr,
117 					 const struct rte_flow_item pattern[],
118 					 const struct rte_flow_action actions[],
119 					 struct rte_flow_error *error,
120 					 union i40e_filter_t *filter);
121 static int i40e_flow_destroy_ethertype_filter(struct i40e_pf *pf,
122 				      struct i40e_ethertype_filter *filter);
123 static int i40e_flow_destroy_tunnel_filter(struct i40e_pf *pf,
124 					   struct i40e_tunnel_filter *filter);
125 static int i40e_flow_flush_fdir_filter(struct i40e_pf *pf);
126 static int i40e_flow_flush_ethertype_filter(struct i40e_pf *pf);
127 static int i40e_flow_flush_tunnel_filter(struct i40e_pf *pf);
128 
129 const struct rte_flow_ops i40e_flow_ops = {
130 	.validate = i40e_flow_validate,
131 	.create = i40e_flow_create,
132 	.destroy = i40e_flow_destroy,
133 	.flush = i40e_flow_flush,
134 };
135 
136 union i40e_filter_t cons_filter;
137 enum rte_filter_type cons_filter_type = RTE_ETH_FILTER_NONE;
138 
139 /* Pattern matched ethertype filter */
140 static enum rte_flow_item_type pattern_ethertype[] = {
141 	RTE_FLOW_ITEM_TYPE_ETH,
142 	RTE_FLOW_ITEM_TYPE_END,
143 };
144 
145 /* Pattern matched flow director filter */
146 static enum rte_flow_item_type pattern_fdir_ipv4[] = {
147 	RTE_FLOW_ITEM_TYPE_IPV4,
148 	RTE_FLOW_ITEM_TYPE_END,
149 };
150 
151 static enum rte_flow_item_type pattern_fdir_ipv4_ext[] = {
152 	RTE_FLOW_ITEM_TYPE_ETH,
153 	RTE_FLOW_ITEM_TYPE_IPV4,
154 	RTE_FLOW_ITEM_TYPE_END,
155 };
156 
157 static enum rte_flow_item_type pattern_fdir_ipv4_udp[] = {
158 	RTE_FLOW_ITEM_TYPE_IPV4,
159 	RTE_FLOW_ITEM_TYPE_UDP,
160 	RTE_FLOW_ITEM_TYPE_END,
161 };
162 
163 static enum rte_flow_item_type pattern_fdir_ipv4_udp_ext[] = {
164 	RTE_FLOW_ITEM_TYPE_ETH,
165 	RTE_FLOW_ITEM_TYPE_IPV4,
166 	RTE_FLOW_ITEM_TYPE_UDP,
167 	RTE_FLOW_ITEM_TYPE_END,
168 };
169 
170 static enum rte_flow_item_type pattern_fdir_ipv4_tcp[] = {
171 	RTE_FLOW_ITEM_TYPE_IPV4,
172 	RTE_FLOW_ITEM_TYPE_TCP,
173 	RTE_FLOW_ITEM_TYPE_END,
174 };
175 
176 static enum rte_flow_item_type pattern_fdir_ipv4_tcp_ext[] = {
177 	RTE_FLOW_ITEM_TYPE_ETH,
178 	RTE_FLOW_ITEM_TYPE_IPV4,
179 	RTE_FLOW_ITEM_TYPE_TCP,
180 	RTE_FLOW_ITEM_TYPE_END,
181 };
182 
183 static enum rte_flow_item_type pattern_fdir_ipv4_sctp[] = {
184 	RTE_FLOW_ITEM_TYPE_IPV4,
185 	RTE_FLOW_ITEM_TYPE_SCTP,
186 	RTE_FLOW_ITEM_TYPE_END,
187 };
188 
189 static enum rte_flow_item_type pattern_fdir_ipv4_sctp_ext[] = {
190 	RTE_FLOW_ITEM_TYPE_ETH,
191 	RTE_FLOW_ITEM_TYPE_IPV4,
192 	RTE_FLOW_ITEM_TYPE_SCTP,
193 	RTE_FLOW_ITEM_TYPE_END,
194 };
195 
196 static enum rte_flow_item_type pattern_fdir_ipv6[] = {
197 	RTE_FLOW_ITEM_TYPE_IPV6,
198 	RTE_FLOW_ITEM_TYPE_END,
199 };
200 
201 static enum rte_flow_item_type pattern_fdir_ipv6_ext[] = {
202 	RTE_FLOW_ITEM_TYPE_ETH,
203 	RTE_FLOW_ITEM_TYPE_IPV6,
204 	RTE_FLOW_ITEM_TYPE_END,
205 };
206 
207 static enum rte_flow_item_type pattern_fdir_ipv6_udp[] = {
208 	RTE_FLOW_ITEM_TYPE_IPV6,
209 	RTE_FLOW_ITEM_TYPE_UDP,
210 	RTE_FLOW_ITEM_TYPE_END,
211 };
212 
213 static enum rte_flow_item_type pattern_fdir_ipv6_udp_ext[] = {
214 	RTE_FLOW_ITEM_TYPE_ETH,
215 	RTE_FLOW_ITEM_TYPE_IPV6,
216 	RTE_FLOW_ITEM_TYPE_UDP,
217 	RTE_FLOW_ITEM_TYPE_END,
218 };
219 
220 static enum rte_flow_item_type pattern_fdir_ipv6_tcp[] = {
221 	RTE_FLOW_ITEM_TYPE_IPV6,
222 	RTE_FLOW_ITEM_TYPE_TCP,
223 	RTE_FLOW_ITEM_TYPE_END,
224 };
225 
226 static enum rte_flow_item_type pattern_fdir_ipv6_tcp_ext[] = {
227 	RTE_FLOW_ITEM_TYPE_ETH,
228 	RTE_FLOW_ITEM_TYPE_IPV6,
229 	RTE_FLOW_ITEM_TYPE_TCP,
230 	RTE_FLOW_ITEM_TYPE_END,
231 };
232 
233 static enum rte_flow_item_type pattern_fdir_ipv6_sctp[] = {
234 	RTE_FLOW_ITEM_TYPE_IPV6,
235 	RTE_FLOW_ITEM_TYPE_SCTP,
236 	RTE_FLOW_ITEM_TYPE_END,
237 };
238 
239 static enum rte_flow_item_type pattern_fdir_ipv6_sctp_ext[] = {
240 	RTE_FLOW_ITEM_TYPE_ETH,
241 	RTE_FLOW_ITEM_TYPE_IPV6,
242 	RTE_FLOW_ITEM_TYPE_SCTP,
243 	RTE_FLOW_ITEM_TYPE_END,
244 };
245 
246 /* Pattern matched tunnel filter */
247 static enum rte_flow_item_type pattern_vxlan_1[] = {
248 	RTE_FLOW_ITEM_TYPE_ETH,
249 	RTE_FLOW_ITEM_TYPE_IPV4,
250 	RTE_FLOW_ITEM_TYPE_UDP,
251 	RTE_FLOW_ITEM_TYPE_VXLAN,
252 	RTE_FLOW_ITEM_TYPE_ETH,
253 	RTE_FLOW_ITEM_TYPE_END,
254 };
255 
256 static enum rte_flow_item_type pattern_vxlan_2[] = {
257 	RTE_FLOW_ITEM_TYPE_ETH,
258 	RTE_FLOW_ITEM_TYPE_IPV6,
259 	RTE_FLOW_ITEM_TYPE_UDP,
260 	RTE_FLOW_ITEM_TYPE_VXLAN,
261 	RTE_FLOW_ITEM_TYPE_ETH,
262 	RTE_FLOW_ITEM_TYPE_END,
263 };
264 
265 static enum rte_flow_item_type pattern_vxlan_3[] = {
266 	RTE_FLOW_ITEM_TYPE_ETH,
267 	RTE_FLOW_ITEM_TYPE_IPV4,
268 	RTE_FLOW_ITEM_TYPE_UDP,
269 	RTE_FLOW_ITEM_TYPE_VXLAN,
270 	RTE_FLOW_ITEM_TYPE_ETH,
271 	RTE_FLOW_ITEM_TYPE_VLAN,
272 	RTE_FLOW_ITEM_TYPE_END,
273 };
274 
275 static enum rte_flow_item_type pattern_vxlan_4[] = {
276 	RTE_FLOW_ITEM_TYPE_ETH,
277 	RTE_FLOW_ITEM_TYPE_IPV6,
278 	RTE_FLOW_ITEM_TYPE_UDP,
279 	RTE_FLOW_ITEM_TYPE_VXLAN,
280 	RTE_FLOW_ITEM_TYPE_ETH,
281 	RTE_FLOW_ITEM_TYPE_VLAN,
282 	RTE_FLOW_ITEM_TYPE_END,
283 };
284 
285 static struct i40e_valid_pattern i40e_supported_patterns[] = {
286 	/* Ethertype */
287 	{ pattern_ethertype, i40e_flow_parse_ethertype_filter },
288 	/* FDIR */
289 	{ pattern_fdir_ipv4, i40e_flow_parse_fdir_filter },
290 	{ pattern_fdir_ipv4_ext, i40e_flow_parse_fdir_filter },
291 	{ pattern_fdir_ipv4_udp, i40e_flow_parse_fdir_filter },
292 	{ pattern_fdir_ipv4_udp_ext, i40e_flow_parse_fdir_filter },
293 	{ pattern_fdir_ipv4_tcp, i40e_flow_parse_fdir_filter },
294 	{ pattern_fdir_ipv4_tcp_ext, i40e_flow_parse_fdir_filter },
295 	{ pattern_fdir_ipv4_sctp, i40e_flow_parse_fdir_filter },
296 	{ pattern_fdir_ipv4_sctp_ext, i40e_flow_parse_fdir_filter },
297 	{ pattern_fdir_ipv6, i40e_flow_parse_fdir_filter },
298 	{ pattern_fdir_ipv6_ext, i40e_flow_parse_fdir_filter },
299 	{ pattern_fdir_ipv6_udp, i40e_flow_parse_fdir_filter },
300 	{ pattern_fdir_ipv6_udp_ext, i40e_flow_parse_fdir_filter },
301 	{ pattern_fdir_ipv6_tcp, i40e_flow_parse_fdir_filter },
302 	{ pattern_fdir_ipv6_tcp_ext, i40e_flow_parse_fdir_filter },
303 	{ pattern_fdir_ipv6_sctp, i40e_flow_parse_fdir_filter },
304 	{ pattern_fdir_ipv6_sctp_ext, i40e_flow_parse_fdir_filter },
305 	/* tunnel */
306 	{ pattern_vxlan_1, i40e_flow_parse_tunnel_filter },
307 	{ pattern_vxlan_2, i40e_flow_parse_tunnel_filter },
308 	{ pattern_vxlan_3, i40e_flow_parse_tunnel_filter },
309 	{ pattern_vxlan_4, i40e_flow_parse_tunnel_filter },
310 };
311 
312 #define NEXT_ITEM_OF_ACTION(act, actions, index)                        \
313 	do {                                                            \
314 		act = actions + index;                                  \
315 		while (act->type == RTE_FLOW_ACTION_TYPE_VOID) {        \
316 			index++;                                        \
317 			act = actions + index;                          \
318 		}                                                       \
319 	} while (0)
320 
321 /* Find the first VOID or non-VOID item pointer */
322 static const struct rte_flow_item *
323 i40e_find_first_item(const struct rte_flow_item *item, bool is_void)
324 {
325 	bool is_find;
326 
327 	while (item->type != RTE_FLOW_ITEM_TYPE_END) {
328 		if (is_void)
329 			is_find = item->type == RTE_FLOW_ITEM_TYPE_VOID;
330 		else
331 			is_find = item->type != RTE_FLOW_ITEM_TYPE_VOID;
332 		if (is_find)
333 			break;
334 		item++;
335 	}
336 	return item;
337 }
338 
339 /* Skip all VOID items of the pattern */
340 static void
341 i40e_pattern_skip_void_item(struct rte_flow_item *items,
342 			    const struct rte_flow_item *pattern)
343 {
344 	uint32_t cpy_count = 0;
345 	const struct rte_flow_item *pb = pattern, *pe = pattern;
346 
347 	for (;;) {
348 		/* Find a non-void item first */
349 		pb = i40e_find_first_item(pb, false);
350 		if (pb->type == RTE_FLOW_ITEM_TYPE_END) {
351 			pe = pb;
352 			break;
353 		}
354 
355 		/* Find a void item */
356 		pe = i40e_find_first_item(pb + 1, true);
357 
358 		cpy_count = pe - pb;
359 		rte_memcpy(items, pb, sizeof(struct rte_flow_item) * cpy_count);
360 
361 		items += cpy_count;
362 
363 		if (pe->type == RTE_FLOW_ITEM_TYPE_END) {
364 			pb = pe;
365 			break;
366 		}
367 
368 		pb = pe + 1;
369 	}
370 	/* Copy the END item. */
371 	rte_memcpy(items, pe, sizeof(struct rte_flow_item));
372 }
373 
374 /* Check if the pattern matches a supported item type array */
375 static bool
376 i40e_match_pattern(enum rte_flow_item_type *item_array,
377 		   struct rte_flow_item *pattern)
378 {
379 	struct rte_flow_item *item = pattern;
380 
381 	while ((*item_array == item->type) &&
382 	       (*item_array != RTE_FLOW_ITEM_TYPE_END)) {
383 		item_array++;
384 		item++;
385 	}
386 
387 	return (*item_array == RTE_FLOW_ITEM_TYPE_END &&
388 		item->type == RTE_FLOW_ITEM_TYPE_END);
389 }
390 
391 /* Find if there's parse filter function matched */
392 static parse_filter_t
393 i40e_find_parse_filter_func(struct rte_flow_item *pattern)
394 {
395 	parse_filter_t parse_filter = NULL;
396 	uint8_t i = 0;
397 
398 	for (; i < RTE_DIM(i40e_supported_patterns); i++) {
399 		if (i40e_match_pattern(i40e_supported_patterns[i].items,
400 					pattern)) {
401 			parse_filter = i40e_supported_patterns[i].parse_filter;
402 			break;
403 		}
404 	}
405 
406 	return parse_filter;
407 }
408 
409 /* Parse attributes */
410 static int
411 i40e_flow_parse_attr(const struct rte_flow_attr *attr,
412 		     struct rte_flow_error *error)
413 {
414 	/* Must be input direction */
415 	if (!attr->ingress) {
416 		rte_flow_error_set(error, EINVAL,
417 				   RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
418 				   attr, "Only support ingress.");
419 		return -rte_errno;
420 	}
421 
422 	/* Not supported */
423 	if (attr->egress) {
424 		rte_flow_error_set(error, EINVAL,
425 				   RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
426 				   attr, "Not support egress.");
427 		return -rte_errno;
428 	}
429 
430 	/* Not supported */
431 	if (attr->priority) {
432 		rte_flow_error_set(error, EINVAL,
433 				   RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
434 				   attr, "Not support priority.");
435 		return -rte_errno;
436 	}
437 
438 	/* Not supported */
439 	if (attr->group) {
440 		rte_flow_error_set(error, EINVAL,
441 				   RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
442 				   attr, "Not support group.");
443 		return -rte_errno;
444 	}
445 
446 	return 0;
447 }
448 
449 static uint16_t
450 i40e_get_outer_vlan(struct rte_eth_dev *dev)
451 {
452 	struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
453 	int qinq = dev->data->dev_conf.rxmode.hw_vlan_extend;
454 	uint64_t reg_r = 0;
455 	uint16_t reg_id;
456 	uint16_t tpid;
457 
458 	if (qinq)
459 		reg_id = 2;
460 	else
461 		reg_id = 3;
462 
463 	i40e_aq_debug_read_register(hw, I40E_GL_SWT_L2TAGCTRL(reg_id),
464 				    &reg_r, NULL);
465 
466 	tpid = (reg_r >> I40E_GL_SWT_L2TAGCTRL_ETHERTYPE_SHIFT) & 0xFFFF;
467 
468 	return tpid;
469 }
470 
471 /* 1. Last in item should be NULL as range is not supported.
472  * 2. Supported filter types: MAC_ETHTYPE and ETHTYPE.
473  * 3. SRC mac_addr mask should be 00:00:00:00:00:00.
474  * 4. DST mac_addr mask should be 00:00:00:00:00:00 or
475  *    FF:FF:FF:FF:FF:FF
476  * 5. Ether_type mask should be 0xFFFF.
477  */
478 static int
479 i40e_flow_parse_ethertype_pattern(struct rte_eth_dev *dev,
480 				  const struct rte_flow_item *pattern,
481 				  struct rte_flow_error *error,
482 				  struct rte_eth_ethertype_filter *filter)
483 {
484 	const struct rte_flow_item *item = pattern;
485 	const struct rte_flow_item_eth *eth_spec;
486 	const struct rte_flow_item_eth *eth_mask;
487 	enum rte_flow_item_type item_type;
488 	uint16_t outer_tpid;
489 
490 	outer_tpid = i40e_get_outer_vlan(dev);
491 
492 	for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
493 		if (item->last) {
494 			rte_flow_error_set(error, EINVAL,
495 					   RTE_FLOW_ERROR_TYPE_ITEM,
496 					   item,
497 					   "Not support range");
498 			return -rte_errno;
499 		}
500 		item_type = item->type;
501 		switch (item_type) {
502 		case RTE_FLOW_ITEM_TYPE_ETH:
503 			eth_spec = (const struct rte_flow_item_eth *)item->spec;
504 			eth_mask = (const struct rte_flow_item_eth *)item->mask;
505 			/* Get the MAC info. */
506 			if (!eth_spec || !eth_mask) {
507 				rte_flow_error_set(error, EINVAL,
508 						   RTE_FLOW_ERROR_TYPE_ITEM,
509 						   item,
510 						   "NULL ETH spec/mask");
511 				return -rte_errno;
512 			}
513 
514 			/* Mask bits of source MAC address must be full of 0.
515 			 * Mask bits of destination MAC address must be full
516 			 * of 1 or full of 0.
517 			 */
518 			if (!is_zero_ether_addr(&eth_mask->src) ||
519 			    (!is_zero_ether_addr(&eth_mask->dst) &&
520 			     !is_broadcast_ether_addr(&eth_mask->dst))) {
521 				rte_flow_error_set(error, EINVAL,
522 						   RTE_FLOW_ERROR_TYPE_ITEM,
523 						   item,
524 						   "Invalid MAC_addr mask");
525 				return -rte_errno;
526 			}
527 
528 			if ((eth_mask->type & UINT16_MAX) != UINT16_MAX) {
529 				rte_flow_error_set(error, EINVAL,
530 						   RTE_FLOW_ERROR_TYPE_ITEM,
531 						   item,
532 						   "Invalid ethertype mask");
533 				return -rte_errno;
534 			}
535 
536 			/* If mask bits of destination MAC address
537 			 * are full of 1, set RTE_ETHTYPE_FLAGS_MAC.
538 			 */
539 			if (is_broadcast_ether_addr(&eth_mask->dst)) {
540 				filter->mac_addr = eth_spec->dst;
541 				filter->flags |= RTE_ETHTYPE_FLAGS_MAC;
542 			} else {
543 				filter->flags &= ~RTE_ETHTYPE_FLAGS_MAC;
544 			}
545 			filter->ether_type = rte_be_to_cpu_16(eth_spec->type);
546 
547 			if (filter->ether_type == ETHER_TYPE_IPv4 ||
548 			    filter->ether_type == ETHER_TYPE_IPv6 ||
549 			    filter->ether_type == outer_tpid) {
550 				rte_flow_error_set(error, EINVAL,
551 						   RTE_FLOW_ERROR_TYPE_ITEM,
552 						   item,
553 						   "Unsupported ether_type in"
554 						   " control packet filter.");
555 				return -rte_errno;
556 			}
557 			break;
558 		default:
559 			break;
560 		}
561 	}
562 
563 	return 0;
564 }
565 
566 /* Ethertype action only supports QUEUE or DROP. */
567 static int
568 i40e_flow_parse_ethertype_action(struct rte_eth_dev *dev,
569 				 const struct rte_flow_action *actions,
570 				 struct rte_flow_error *error,
571 				 struct rte_eth_ethertype_filter *filter)
572 {
573 	struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
574 	const struct rte_flow_action *act;
575 	const struct rte_flow_action_queue *act_q;
576 	uint32_t index = 0;
577 
578 	/* Check if the first non-void action is QUEUE or DROP. */
579 	NEXT_ITEM_OF_ACTION(act, actions, index);
580 	if (act->type != RTE_FLOW_ACTION_TYPE_QUEUE &&
581 	    act->type != RTE_FLOW_ACTION_TYPE_DROP) {
582 		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
583 				   act, "Not supported action.");
584 		return -rte_errno;
585 	}
586 
587 	if (act->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
588 		act_q = (const struct rte_flow_action_queue *)act->conf;
589 		filter->queue = act_q->index;
590 		if (filter->queue >= pf->dev_data->nb_rx_queues) {
591 			rte_flow_error_set(error, EINVAL,
592 					   RTE_FLOW_ERROR_TYPE_ACTION,
593 					   act, "Invalid queue ID for"
594 					   " ethertype_filter.");
595 			return -rte_errno;
596 		}
597 	} else {
598 		filter->flags |= RTE_ETHTYPE_FLAGS_DROP;
599 	}
600 
601 	/* Check if the next non-void item is END */
602 	index++;
603 	NEXT_ITEM_OF_ACTION(act, actions, index);
604 	if (act->type != RTE_FLOW_ACTION_TYPE_END) {
605 		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
606 				   act, "Not supported action.");
607 		return -rte_errno;
608 	}
609 
610 	return 0;
611 }
612 
613 static int
614 i40e_flow_parse_ethertype_filter(struct rte_eth_dev *dev,
615 				 const struct rte_flow_attr *attr,
616 				 const struct rte_flow_item pattern[],
617 				 const struct rte_flow_action actions[],
618 				 struct rte_flow_error *error,
619 				 union i40e_filter_t *filter)
620 {
621 	struct rte_eth_ethertype_filter *ethertype_filter =
622 		&filter->ethertype_filter;
623 	int ret;
624 
625 	ret = i40e_flow_parse_ethertype_pattern(dev, pattern, error,
626 						ethertype_filter);
627 	if (ret)
628 		return ret;
629 
630 	ret = i40e_flow_parse_ethertype_action(dev, actions, error,
631 					       ethertype_filter);
632 	if (ret)
633 		return ret;
634 
635 	ret = i40e_flow_parse_attr(attr, error);
636 	if (ret)
637 		return ret;
638 
639 	cons_filter_type = RTE_ETH_FILTER_ETHERTYPE;
640 
641 	return ret;
642 }
643 
644 /* 1. Last in item should be NULL as range is not supported.
645  * 2. Supported flow type and input set: refer to array
646  *    default_inset_table in i40e_ethdev.c.
647  * 3. Mask of fields which need to be matched should be
648  *    filled with 1.
649  * 4. Mask of fields which needn't to be matched should be
650  *    filled with 0.
651  */
652 static int
653 i40e_flow_parse_fdir_pattern(struct rte_eth_dev *dev,
654 			     const struct rte_flow_item *pattern,
655 			     struct rte_flow_error *error,
656 			     struct rte_eth_fdir_filter *filter)
657 {
658 	struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
659 	const struct rte_flow_item *item = pattern;
660 	const struct rte_flow_item_eth *eth_spec, *eth_mask;
661 	const struct rte_flow_item_ipv4 *ipv4_spec, *ipv4_mask;
662 	const struct rte_flow_item_ipv6 *ipv6_spec, *ipv6_mask;
663 	const struct rte_flow_item_tcp *tcp_spec, *tcp_mask;
664 	const struct rte_flow_item_udp *udp_spec, *udp_mask;
665 	const struct rte_flow_item_sctp *sctp_spec, *sctp_mask;
666 	const struct rte_flow_item_vf *vf_spec;
667 	uint32_t flow_type = RTE_ETH_FLOW_UNKNOWN;
668 	enum i40e_filter_pctype pctype;
669 	uint64_t input_set = I40E_INSET_NONE;
670 	uint16_t flag_offset;
671 	enum rte_flow_item_type item_type;
672 	enum rte_flow_item_type l3 = RTE_FLOW_ITEM_TYPE_END;
673 	uint32_t j;
674 
675 	for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
676 		if (item->last) {
677 			rte_flow_error_set(error, EINVAL,
678 					   RTE_FLOW_ERROR_TYPE_ITEM,
679 					   item,
680 					   "Not support range");
681 			return -rte_errno;
682 		}
683 		item_type = item->type;
684 		switch (item_type) {
685 		case RTE_FLOW_ITEM_TYPE_ETH:
686 			eth_spec = (const struct rte_flow_item_eth *)item->spec;
687 			eth_mask = (const struct rte_flow_item_eth *)item->mask;
688 			if (eth_spec || eth_mask) {
689 				rte_flow_error_set(error, EINVAL,
690 						   RTE_FLOW_ERROR_TYPE_ITEM,
691 						   item,
692 						   "Invalid ETH spec/mask");
693 				return -rte_errno;
694 			}
695 			break;
696 		case RTE_FLOW_ITEM_TYPE_IPV4:
697 			l3 = RTE_FLOW_ITEM_TYPE_IPV4;
698 			ipv4_spec =
699 				(const struct rte_flow_item_ipv4 *)item->spec;
700 			ipv4_mask =
701 				(const struct rte_flow_item_ipv4 *)item->mask;
702 			if (!ipv4_spec || !ipv4_mask) {
703 				rte_flow_error_set(error, EINVAL,
704 						   RTE_FLOW_ERROR_TYPE_ITEM,
705 						   item,
706 						   "NULL IPv4 spec/mask");
707 				return -rte_errno;
708 			}
709 
710 			/* Check IPv4 mask and update input set */
711 			if (ipv4_mask->hdr.version_ihl ||
712 			    ipv4_mask->hdr.total_length ||
713 			    ipv4_mask->hdr.packet_id ||
714 			    ipv4_mask->hdr.fragment_offset ||
715 			    ipv4_mask->hdr.hdr_checksum) {
716 				rte_flow_error_set(error, EINVAL,
717 						   RTE_FLOW_ERROR_TYPE_ITEM,
718 						   item,
719 						   "Invalid IPv4 mask.");
720 				return -rte_errno;
721 			}
722 
723 			if (ipv4_mask->hdr.src_addr == UINT32_MAX)
724 				input_set |= I40E_INSET_IPV4_SRC;
725 			if (ipv4_mask->hdr.dst_addr == UINT32_MAX)
726 				input_set |= I40E_INSET_IPV4_DST;
727 			if (ipv4_mask->hdr.type_of_service == UINT8_MAX)
728 				input_set |= I40E_INSET_IPV4_TOS;
729 			if (ipv4_mask->hdr.time_to_live == UINT8_MAX)
730 				input_set |= I40E_INSET_IPV4_TTL;
731 			if (ipv4_mask->hdr.next_proto_id == UINT8_MAX)
732 				input_set |= I40E_INSET_IPV4_PROTO;
733 
734 			/* Get filter info */
735 			flow_type = RTE_ETH_FLOW_NONFRAG_IPV4_OTHER;
736 			/* Check if it is fragment. */
737 			flag_offset =
738 			      rte_be_to_cpu_16(ipv4_spec->hdr.fragment_offset);
739 			if (flag_offset & IPV4_HDR_OFFSET_MASK ||
740 			    flag_offset & IPV4_HDR_MF_FLAG)
741 				flow_type = RTE_ETH_FLOW_FRAG_IPV4;
742 
743 			/* Get the filter info */
744 			filter->input.flow.ip4_flow.proto =
745 				ipv4_spec->hdr.next_proto_id;
746 			filter->input.flow.ip4_flow.tos =
747 				ipv4_spec->hdr.type_of_service;
748 			filter->input.flow.ip4_flow.ttl =
749 				ipv4_spec->hdr.time_to_live;
750 			filter->input.flow.ip4_flow.src_ip =
751 				ipv4_spec->hdr.src_addr;
752 			filter->input.flow.ip4_flow.dst_ip =
753 				ipv4_spec->hdr.dst_addr;
754 
755 			break;
756 		case RTE_FLOW_ITEM_TYPE_IPV6:
757 			l3 = RTE_FLOW_ITEM_TYPE_IPV6;
758 			ipv6_spec =
759 				(const struct rte_flow_item_ipv6 *)item->spec;
760 			ipv6_mask =
761 				(const struct rte_flow_item_ipv6 *)item->mask;
762 			if (!ipv6_spec || !ipv6_mask) {
763 				rte_flow_error_set(error, EINVAL,
764 						   RTE_FLOW_ERROR_TYPE_ITEM,
765 						   item,
766 						   "NULL IPv6 spec/mask");
767 				return -rte_errno;
768 			}
769 
770 			/* Check IPv6 mask and update input set */
771 			if (ipv6_mask->hdr.payload_len) {
772 				rte_flow_error_set(error, EINVAL,
773 						   RTE_FLOW_ERROR_TYPE_ITEM,
774 						   item,
775 						   "Invalid IPv6 mask");
776 				return -rte_errno;
777 			}
778 
779 			/* SCR and DST address of IPv6 shouldn't be masked */
780 			for (j = 0; j < RTE_DIM(ipv6_mask->hdr.src_addr); j++) {
781 				if (ipv6_mask->hdr.src_addr[j] != UINT8_MAX ||
782 				    ipv6_mask->hdr.dst_addr[j] != UINT8_MAX) {
783 					rte_flow_error_set(error, EINVAL,
784 						   RTE_FLOW_ERROR_TYPE_ITEM,
785 						   item,
786 						   "Invalid IPv6 mask");
787 					return -rte_errno;
788 				}
789 			}
790 
791 			input_set |= I40E_INSET_IPV6_SRC;
792 			input_set |= I40E_INSET_IPV6_DST;
793 
794 			if ((ipv6_mask->hdr.vtc_flow &
795 			     rte_cpu_to_be_16(I40E_IPV6_TC_MASK))
796 			    == rte_cpu_to_be_16(I40E_IPV6_TC_MASK))
797 				input_set |= I40E_INSET_IPV6_TC;
798 			if (ipv6_mask->hdr.proto == UINT8_MAX)
799 				input_set |= I40E_INSET_IPV6_NEXT_HDR;
800 			if (ipv6_mask->hdr.hop_limits == UINT8_MAX)
801 				input_set |= I40E_INSET_IPV6_HOP_LIMIT;
802 
803 			/* Get filter info */
804 			filter->input.flow.ipv6_flow.tc =
805 				(uint8_t)(ipv6_spec->hdr.vtc_flow <<
806 					  I40E_IPV4_TC_SHIFT);
807 			filter->input.flow.ipv6_flow.proto =
808 				ipv6_spec->hdr.proto;
809 			filter->input.flow.ipv6_flow.hop_limits =
810 				ipv6_spec->hdr.hop_limits;
811 
812 			rte_memcpy(filter->input.flow.ipv6_flow.src_ip,
813 				   ipv6_spec->hdr.src_addr, 16);
814 			rte_memcpy(filter->input.flow.ipv6_flow.dst_ip,
815 				   ipv6_spec->hdr.dst_addr, 16);
816 
817 			/* Check if it is fragment. */
818 			if (ipv6_spec->hdr.proto == I40E_IPV6_FRAG_HEADER)
819 				flow_type = RTE_ETH_FLOW_FRAG_IPV6;
820 			else
821 				flow_type = RTE_ETH_FLOW_NONFRAG_IPV6_OTHER;
822 			break;
823 		case RTE_FLOW_ITEM_TYPE_TCP:
824 			tcp_spec = (const struct rte_flow_item_tcp *)item->spec;
825 			tcp_mask = (const struct rte_flow_item_tcp *)item->mask;
826 			if (!tcp_spec || !tcp_mask) {
827 				rte_flow_error_set(error, EINVAL,
828 						   RTE_FLOW_ERROR_TYPE_ITEM,
829 						   item,
830 						   "NULL TCP spec/mask");
831 				return -rte_errno;
832 			}
833 
834 			/* Check TCP mask and update input set */
835 			if (tcp_mask->hdr.sent_seq ||
836 			    tcp_mask->hdr.recv_ack ||
837 			    tcp_mask->hdr.data_off ||
838 			    tcp_mask->hdr.tcp_flags ||
839 			    tcp_mask->hdr.rx_win ||
840 			    tcp_mask->hdr.cksum ||
841 			    tcp_mask->hdr.tcp_urp) {
842 				rte_flow_error_set(error, EINVAL,
843 						   RTE_FLOW_ERROR_TYPE_ITEM,
844 						   item,
845 						   "Invalid TCP mask");
846 				return -rte_errno;
847 			}
848 
849 			if (tcp_mask->hdr.src_port != UINT16_MAX ||
850 			    tcp_mask->hdr.dst_port != UINT16_MAX) {
851 				rte_flow_error_set(error, EINVAL,
852 						   RTE_FLOW_ERROR_TYPE_ITEM,
853 						   item,
854 						   "Invalid TCP mask");
855 				return -rte_errno;
856 			}
857 
858 			input_set |= I40E_INSET_SRC_PORT;
859 			input_set |= I40E_INSET_DST_PORT;
860 
861 			/* Get filter info */
862 			if (l3 == RTE_FLOW_ITEM_TYPE_IPV4)
863 				flow_type = RTE_ETH_FLOW_NONFRAG_IPV4_TCP;
864 			else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6)
865 				flow_type = RTE_ETH_FLOW_NONFRAG_IPV6_TCP;
866 
867 			if (l3 == RTE_FLOW_ITEM_TYPE_IPV4) {
868 				filter->input.flow.tcp4_flow.src_port =
869 					tcp_spec->hdr.src_port;
870 				filter->input.flow.tcp4_flow.dst_port =
871 					tcp_spec->hdr.dst_port;
872 			} else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6) {
873 				filter->input.flow.tcp6_flow.src_port =
874 					tcp_spec->hdr.src_port;
875 				filter->input.flow.tcp6_flow.dst_port =
876 					tcp_spec->hdr.dst_port;
877 			}
878 			break;
879 		case RTE_FLOW_ITEM_TYPE_UDP:
880 			udp_spec = (const struct rte_flow_item_udp *)item->spec;
881 			udp_mask = (const struct rte_flow_item_udp *)item->mask;
882 			if (!udp_spec || !udp_mask) {
883 				rte_flow_error_set(error, EINVAL,
884 						   RTE_FLOW_ERROR_TYPE_ITEM,
885 						   item,
886 						   "NULL UDP spec/mask");
887 				return -rte_errno;
888 			}
889 
890 			/* Check UDP mask and update input set*/
891 			if (udp_mask->hdr.dgram_len ||
892 			    udp_mask->hdr.dgram_cksum) {
893 				rte_flow_error_set(error, EINVAL,
894 						   RTE_FLOW_ERROR_TYPE_ITEM,
895 						   item,
896 						   "Invalid UDP mask");
897 				return -rte_errno;
898 			}
899 
900 			if (udp_mask->hdr.src_port != UINT16_MAX ||
901 			    udp_mask->hdr.dst_port != UINT16_MAX) {
902 				rte_flow_error_set(error, EINVAL,
903 						   RTE_FLOW_ERROR_TYPE_ITEM,
904 						   item,
905 						   "Invalid UDP mask");
906 				return -rte_errno;
907 			}
908 
909 			input_set |= I40E_INSET_SRC_PORT;
910 			input_set |= I40E_INSET_DST_PORT;
911 
912 			/* Get filter info */
913 			if (l3 == RTE_FLOW_ITEM_TYPE_IPV4)
914 				flow_type =
915 					RTE_ETH_FLOW_NONFRAG_IPV4_UDP;
916 			else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6)
917 				flow_type =
918 					RTE_ETH_FLOW_NONFRAG_IPV6_UDP;
919 
920 			if (l3 == RTE_FLOW_ITEM_TYPE_IPV4) {
921 				filter->input.flow.udp4_flow.src_port =
922 					udp_spec->hdr.src_port;
923 				filter->input.flow.udp4_flow.dst_port =
924 					udp_spec->hdr.dst_port;
925 			} else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6) {
926 				filter->input.flow.udp6_flow.src_port =
927 					udp_spec->hdr.src_port;
928 				filter->input.flow.udp6_flow.dst_port =
929 					udp_spec->hdr.dst_port;
930 			}
931 			break;
932 		case RTE_FLOW_ITEM_TYPE_SCTP:
933 			sctp_spec =
934 				(const struct rte_flow_item_sctp *)item->spec;
935 			sctp_mask =
936 				(const struct rte_flow_item_sctp *)item->mask;
937 			if (!sctp_spec || !sctp_mask) {
938 				rte_flow_error_set(error, EINVAL,
939 						   RTE_FLOW_ERROR_TYPE_ITEM,
940 						   item,
941 						   "NULL SCTP spec/mask");
942 				return -rte_errno;
943 			}
944 
945 			/* Check SCTP mask and update input set */
946 			if (sctp_mask->hdr.cksum) {
947 				rte_flow_error_set(error, EINVAL,
948 						   RTE_FLOW_ERROR_TYPE_ITEM,
949 						   item,
950 						   "Invalid UDP mask");
951 				return -rte_errno;
952 			}
953 
954 			if (sctp_mask->hdr.src_port != UINT16_MAX ||
955 			    sctp_mask->hdr.dst_port != UINT16_MAX ||
956 			    sctp_mask->hdr.tag != UINT32_MAX) {
957 				rte_flow_error_set(error, EINVAL,
958 						   RTE_FLOW_ERROR_TYPE_ITEM,
959 						   item,
960 						   "Invalid UDP mask");
961 				return -rte_errno;
962 			}
963 			input_set |= I40E_INSET_SRC_PORT;
964 			input_set |= I40E_INSET_DST_PORT;
965 			input_set |= I40E_INSET_SCTP_VT;
966 
967 			/* Get filter info */
968 			if (l3 == RTE_FLOW_ITEM_TYPE_IPV4)
969 				flow_type = RTE_ETH_FLOW_NONFRAG_IPV4_SCTP;
970 			else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6)
971 				flow_type = RTE_ETH_FLOW_NONFRAG_IPV6_SCTP;
972 
973 			if (l3 == RTE_FLOW_ITEM_TYPE_IPV4) {
974 				filter->input.flow.sctp4_flow.src_port =
975 					sctp_spec->hdr.src_port;
976 				filter->input.flow.sctp4_flow.dst_port =
977 					sctp_spec->hdr.dst_port;
978 				filter->input.flow.sctp4_flow.verify_tag =
979 					sctp_spec->hdr.tag;
980 			} else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6) {
981 				filter->input.flow.sctp6_flow.src_port =
982 					sctp_spec->hdr.src_port;
983 				filter->input.flow.sctp6_flow.dst_port =
984 					sctp_spec->hdr.dst_port;
985 				filter->input.flow.sctp6_flow.verify_tag =
986 					sctp_spec->hdr.tag;
987 			}
988 			break;
989 		case RTE_FLOW_ITEM_TYPE_VF:
990 			vf_spec = (const struct rte_flow_item_vf *)item->spec;
991 			filter->input.flow_ext.is_vf = 1;
992 			filter->input.flow_ext.dst_id = vf_spec->id;
993 			if (filter->input.flow_ext.is_vf &&
994 			    filter->input.flow_ext.dst_id >= pf->vf_num) {
995 				rte_flow_error_set(error, EINVAL,
996 						   RTE_FLOW_ERROR_TYPE_ITEM,
997 						   item,
998 						   "Invalid VF ID for FDIR.");
999 				return -rte_errno;
1000 			}
1001 			break;
1002 		default:
1003 			break;
1004 		}
1005 	}
1006 
1007 	pctype = i40e_flowtype_to_pctype(flow_type);
1008 	if (pctype == 0 || pctype > I40E_FILTER_PCTYPE_L2_PAYLOAD) {
1009 		rte_flow_error_set(error, EINVAL,
1010 				   RTE_FLOW_ERROR_TYPE_ITEM, item,
1011 				   "Unsupported flow type");
1012 		return -rte_errno;
1013 	}
1014 
1015 	if (input_set != i40e_get_default_input_set(pctype)) {
1016 		rte_flow_error_set(error, EINVAL,
1017 				   RTE_FLOW_ERROR_TYPE_ITEM, item,
1018 				   "Invalid input set.");
1019 		return -rte_errno;
1020 	}
1021 	filter->input.flow_type = flow_type;
1022 
1023 	return 0;
1024 }
1025 
1026 /* Parse to get the action info of a FDIR filter.
1027  * FDIR action supports QUEUE or (QUEUE + MARK).
1028  */
1029 static int
1030 i40e_flow_parse_fdir_action(struct rte_eth_dev *dev,
1031 			    const struct rte_flow_action *actions,
1032 			    struct rte_flow_error *error,
1033 			    struct rte_eth_fdir_filter *filter)
1034 {
1035 	struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1036 	const struct rte_flow_action *act;
1037 	const struct rte_flow_action_queue *act_q;
1038 	const struct rte_flow_action_mark *mark_spec;
1039 	uint32_t index = 0;
1040 
1041 	/* Check if the first non-void action is QUEUE or DROP. */
1042 	NEXT_ITEM_OF_ACTION(act, actions, index);
1043 	if (act->type != RTE_FLOW_ACTION_TYPE_QUEUE &&
1044 	    act->type != RTE_FLOW_ACTION_TYPE_DROP) {
1045 		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
1046 				   act, "Invalid action.");
1047 		return -rte_errno;
1048 	}
1049 
1050 	act_q = (const struct rte_flow_action_queue *)act->conf;
1051 	filter->action.flex_off = 0;
1052 	if (act->type == RTE_FLOW_ACTION_TYPE_QUEUE)
1053 		filter->action.behavior = RTE_ETH_FDIR_ACCEPT;
1054 	else
1055 		filter->action.behavior = RTE_ETH_FDIR_REJECT;
1056 
1057 	filter->action.report_status = RTE_ETH_FDIR_REPORT_ID;
1058 	filter->action.rx_queue = act_q->index;
1059 
1060 	if (filter->action.rx_queue >= pf->dev_data->nb_rx_queues) {
1061 		rte_flow_error_set(error, EINVAL,
1062 				   RTE_FLOW_ERROR_TYPE_ACTION, act,
1063 				   "Invalid queue ID for FDIR.");
1064 		return -rte_errno;
1065 	}
1066 
1067 	/* Check if the next non-void item is MARK or END. */
1068 	index++;
1069 	NEXT_ITEM_OF_ACTION(act, actions, index);
1070 	if (act->type != RTE_FLOW_ACTION_TYPE_MARK &&
1071 	    act->type != RTE_FLOW_ACTION_TYPE_END) {
1072 		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
1073 				   act, "Invalid action.");
1074 		return -rte_errno;
1075 	}
1076 
1077 	if (act->type == RTE_FLOW_ACTION_TYPE_MARK) {
1078 		mark_spec = (const struct rte_flow_action_mark *)act->conf;
1079 		filter->soft_id = mark_spec->id;
1080 
1081 		/* Check if the next non-void item is END */
1082 		index++;
1083 		NEXT_ITEM_OF_ACTION(act, actions, index);
1084 		if (act->type != RTE_FLOW_ACTION_TYPE_END) {
1085 			rte_flow_error_set(error, EINVAL,
1086 					   RTE_FLOW_ERROR_TYPE_ACTION,
1087 					   act, "Invalid action.");
1088 			return -rte_errno;
1089 		}
1090 	}
1091 
1092 	return 0;
1093 }
1094 
1095 static int
1096 i40e_flow_parse_fdir_filter(struct rte_eth_dev *dev,
1097 			    const struct rte_flow_attr *attr,
1098 			    const struct rte_flow_item pattern[],
1099 			    const struct rte_flow_action actions[],
1100 			    struct rte_flow_error *error,
1101 			    union i40e_filter_t *filter)
1102 {
1103 	struct rte_eth_fdir_filter *fdir_filter =
1104 		&filter->fdir_filter;
1105 	int ret;
1106 
1107 	ret = i40e_flow_parse_fdir_pattern(dev, pattern, error, fdir_filter);
1108 	if (ret)
1109 		return ret;
1110 
1111 	ret = i40e_flow_parse_fdir_action(dev, actions, error, fdir_filter);
1112 	if (ret)
1113 		return ret;
1114 
1115 	ret = i40e_flow_parse_attr(attr, error);
1116 	if (ret)
1117 		return ret;
1118 
1119 	cons_filter_type = RTE_ETH_FILTER_FDIR;
1120 
1121 	if (dev->data->dev_conf.fdir_conf.mode !=
1122 	    RTE_FDIR_MODE_PERFECT) {
1123 		rte_flow_error_set(error, ENOTSUP,
1124 				   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1125 				   NULL,
1126 				   "Check the mode in fdir_conf.");
1127 		return -rte_errno;
1128 	}
1129 
1130 	return 0;
1131 }
1132 
1133 /* Parse to get the action info of a tunnle filter
1134  * Tunnel action only supports QUEUE.
1135  */
1136 static int
1137 i40e_flow_parse_tunnel_action(struct rte_eth_dev *dev,
1138 			      const struct rte_flow_action *actions,
1139 			      struct rte_flow_error *error,
1140 			      struct rte_eth_tunnel_filter_conf *filter)
1141 {
1142 	struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1143 	const struct rte_flow_action *act;
1144 	const struct rte_flow_action_queue *act_q;
1145 	uint32_t index = 0;
1146 
1147 	/* Check if the first non-void action is QUEUE. */
1148 	NEXT_ITEM_OF_ACTION(act, actions, index);
1149 	if (act->type != RTE_FLOW_ACTION_TYPE_QUEUE) {
1150 		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
1151 				   act, "Not supported action.");
1152 		return -rte_errno;
1153 	}
1154 
1155 	act_q = (const struct rte_flow_action_queue *)act->conf;
1156 	filter->queue_id = act_q->index;
1157 	if (filter->queue_id >= pf->dev_data->nb_rx_queues) {
1158 		rte_flow_error_set(error, EINVAL,
1159 				   RTE_FLOW_ERROR_TYPE_ACTION,
1160 				   act, "Invalid queue ID for tunnel filter");
1161 		return -rte_errno;
1162 	}
1163 
1164 	/* Check if the next non-void item is END */
1165 	index++;
1166 	NEXT_ITEM_OF_ACTION(act, actions, index);
1167 	if (act->type != RTE_FLOW_ACTION_TYPE_END) {
1168 		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
1169 				   act, "Not supported action.");
1170 		return -rte_errno;
1171 	}
1172 
1173 	return 0;
1174 }
1175 
1176 static int
1177 i40e_check_tenant_id_mask(const uint8_t *mask)
1178 {
1179 	uint32_t j;
1180 	int is_masked = 0;
1181 
1182 	for (j = 0; j < I40E_TENANT_ARRAY_NUM; j++) {
1183 		if (*(mask + j) == UINT8_MAX) {
1184 			if (j > 0 && (*(mask + j) != *(mask + j - 1)))
1185 				return -EINVAL;
1186 			is_masked = 0;
1187 		} else if (*(mask + j) == 0) {
1188 			if (j > 0 && (*(mask + j) != *(mask + j - 1)))
1189 				return -EINVAL;
1190 			is_masked = 1;
1191 		} else {
1192 			return -EINVAL;
1193 		}
1194 	}
1195 
1196 	return is_masked;
1197 }
1198 
1199 /* 1. Last in item should be NULL as range is not supported.
1200  * 2. Supported filter types: IMAC_IVLAN_TENID, IMAC_IVLAN,
1201  *    IMAC_TENID, OMAC_TENID_IMAC and IMAC.
1202  * 3. Mask of fields which need to be matched should be
1203  *    filled with 1.
1204  * 4. Mask of fields which needn't to be matched should be
1205  *    filled with 0.
1206  */
1207 static int
1208 i40e_flow_parse_vxlan_pattern(const struct rte_flow_item *pattern,
1209 			      struct rte_flow_error *error,
1210 			      struct rte_eth_tunnel_filter_conf *filter)
1211 {
1212 	const struct rte_flow_item *item = pattern;
1213 	const struct rte_flow_item_eth *eth_spec;
1214 	const struct rte_flow_item_eth *eth_mask;
1215 	const struct rte_flow_item_eth *o_eth_spec = NULL;
1216 	const struct rte_flow_item_eth *o_eth_mask = NULL;
1217 	const struct rte_flow_item_vxlan *vxlan_spec = NULL;
1218 	const struct rte_flow_item_vxlan *vxlan_mask = NULL;
1219 	const struct rte_flow_item_eth *i_eth_spec = NULL;
1220 	const struct rte_flow_item_eth *i_eth_mask = NULL;
1221 	const struct rte_flow_item_vlan *vlan_spec = NULL;
1222 	const struct rte_flow_item_vlan *vlan_mask = NULL;
1223 	bool is_vni_masked = 0;
1224 	enum rte_flow_item_type item_type;
1225 	bool vxlan_flag = 0;
1226 
1227 	for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1228 		if (item->last) {
1229 			rte_flow_error_set(error, EINVAL,
1230 					   RTE_FLOW_ERROR_TYPE_ITEM,
1231 					   item,
1232 					   "Not support range");
1233 			return -rte_errno;
1234 		}
1235 		item_type = item->type;
1236 		switch (item_type) {
1237 		case RTE_FLOW_ITEM_TYPE_ETH:
1238 			eth_spec = (const struct rte_flow_item_eth *)item->spec;
1239 			eth_mask = (const struct rte_flow_item_eth *)item->mask;
1240 			if ((!eth_spec && eth_mask) ||
1241 			    (eth_spec && !eth_mask)) {
1242 				rte_flow_error_set(error, EINVAL,
1243 						   RTE_FLOW_ERROR_TYPE_ITEM,
1244 						   item,
1245 						   "Invalid ether spec/mask");
1246 				return -rte_errno;
1247 			}
1248 
1249 			if (eth_spec && eth_mask) {
1250 				/* DST address of inner MAC shouldn't be masked.
1251 				 * SRC address of Inner MAC should be masked.
1252 				 */
1253 				if (!is_broadcast_ether_addr(&eth_mask->dst) ||
1254 				    !is_zero_ether_addr(&eth_mask->src) ||
1255 				    eth_mask->type) {
1256 					rte_flow_error_set(error, EINVAL,
1257 						   RTE_FLOW_ERROR_TYPE_ITEM,
1258 						   item,
1259 						   "Invalid ether spec/mask");
1260 					return -rte_errno;
1261 				}
1262 
1263 				if (!vxlan_flag)
1264 					rte_memcpy(&filter->outer_mac,
1265 						   &eth_spec->dst,
1266 						   ETHER_ADDR_LEN);
1267 				else
1268 					rte_memcpy(&filter->inner_mac,
1269 						   &eth_spec->dst,
1270 						   ETHER_ADDR_LEN);
1271 			}
1272 
1273 			if (!vxlan_flag) {
1274 				o_eth_spec = eth_spec;
1275 				o_eth_mask = eth_mask;
1276 			} else {
1277 				i_eth_spec = eth_spec;
1278 				i_eth_mask = eth_mask;
1279 			}
1280 
1281 			break;
1282 		case RTE_FLOW_ITEM_TYPE_VLAN:
1283 			vlan_spec =
1284 				(const struct rte_flow_item_vlan *)item->spec;
1285 			vlan_mask =
1286 				(const struct rte_flow_item_vlan *)item->mask;
1287 			if (vxlan_flag) {
1288 				vlan_spec =
1289 				(const struct rte_flow_item_vlan *)item->spec;
1290 				vlan_mask =
1291 				(const struct rte_flow_item_vlan *)item->mask;
1292 				if (!(vlan_spec && vlan_mask)) {
1293 					rte_flow_error_set(error, EINVAL,
1294 						   RTE_FLOW_ERROR_TYPE_ITEM,
1295 						   item,
1296 						   "Invalid vlan item");
1297 					return -rte_errno;
1298 				}
1299 			} else {
1300 				if (vlan_spec || vlan_mask)
1301 					rte_flow_error_set(error, EINVAL,
1302 						   RTE_FLOW_ERROR_TYPE_ITEM,
1303 						   item,
1304 						   "Invalid vlan item");
1305 				return -rte_errno;
1306 			}
1307 			break;
1308 		case RTE_FLOW_ITEM_TYPE_IPV4:
1309 		case RTE_FLOW_ITEM_TYPE_IPV6:
1310 		case RTE_FLOW_ITEM_TYPE_UDP:
1311 			/* IPv4/IPv6/UDP are used to describe protocol,
1312 			 * spec amd mask should be NULL.
1313 			 */
1314 			if (item->spec || item->mask) {
1315 				rte_flow_error_set(error, EINVAL,
1316 					   RTE_FLOW_ERROR_TYPE_ITEM,
1317 					   item,
1318 					   "Invalid IPv4 item");
1319 				return -rte_errno;
1320 			}
1321 			break;
1322 		case RTE_FLOW_ITEM_TYPE_VXLAN:
1323 			vxlan_spec =
1324 				(const struct rte_flow_item_vxlan *)item->spec;
1325 			vxlan_mask =
1326 				(const struct rte_flow_item_vxlan *)item->mask;
1327 			/* Check if VXLAN item is used to describe protocol.
1328 			 * If yes, both spec and mask should be NULL.
1329 			 * If no, either spec or mask shouldn't be NULL.
1330 			 */
1331 			if ((!vxlan_spec && vxlan_mask) ||
1332 			    (vxlan_spec && !vxlan_mask)) {
1333 				rte_flow_error_set(error, EINVAL,
1334 					   RTE_FLOW_ERROR_TYPE_ITEM,
1335 					   item,
1336 					   "Invalid VXLAN item");
1337 				return -rte_errno;
1338 			}
1339 
1340 			/* Check if VNI is masked. */
1341 			if (vxlan_mask) {
1342 				is_vni_masked =
1343 				i40e_check_tenant_id_mask(vxlan_mask->vni);
1344 				if (is_vni_masked < 0) {
1345 					rte_flow_error_set(error, EINVAL,
1346 						   RTE_FLOW_ERROR_TYPE_ITEM,
1347 						   item,
1348 						   "Invalid VNI mask");
1349 					return -rte_errno;
1350 				}
1351 			}
1352 			vxlan_flag = 1;
1353 			break;
1354 		default:
1355 			break;
1356 		}
1357 	}
1358 
1359 	/* Check specification and mask to get the filter type */
1360 	if (vlan_spec && vlan_mask &&
1361 	    (vlan_mask->tci == rte_cpu_to_be_16(I40E_TCI_MASK))) {
1362 		/* If there's inner vlan */
1363 		filter->inner_vlan = rte_be_to_cpu_16(vlan_spec->tci)
1364 			& I40E_TCI_MASK;
1365 		if (vxlan_spec && vxlan_mask && !is_vni_masked) {
1366 			/* If there's vxlan */
1367 			rte_memcpy(&filter->tenant_id, vxlan_spec->vni,
1368 				   RTE_DIM(vxlan_spec->vni));
1369 			if (!o_eth_spec && !o_eth_mask &&
1370 				i_eth_spec && i_eth_mask)
1371 				filter->filter_type =
1372 					RTE_TUNNEL_FILTER_IMAC_IVLAN_TENID;
1373 			else {
1374 				rte_flow_error_set(error, EINVAL,
1375 						   RTE_FLOW_ERROR_TYPE_ITEM,
1376 						   NULL,
1377 						   "Invalid filter type");
1378 				return -rte_errno;
1379 			}
1380 		} else if (!vxlan_spec && !vxlan_mask) {
1381 			/* If there's no vxlan */
1382 			if (!o_eth_spec && !o_eth_mask &&
1383 				i_eth_spec && i_eth_mask)
1384 				filter->filter_type =
1385 					RTE_TUNNEL_FILTER_IMAC_IVLAN;
1386 			else {
1387 				rte_flow_error_set(error, EINVAL,
1388 						   RTE_FLOW_ERROR_TYPE_ITEM,
1389 						   NULL,
1390 						   "Invalid filter type");
1391 				return -rte_errno;
1392 			}
1393 		} else {
1394 			rte_flow_error_set(error, EINVAL,
1395 					   RTE_FLOW_ERROR_TYPE_ITEM,
1396 					   NULL,
1397 					   "Invalid filter type");
1398 			return -rte_errno;
1399 		}
1400 	} else if ((!vlan_spec && !vlan_mask) ||
1401 		   (vlan_spec && vlan_mask && vlan_mask->tci == 0x0)) {
1402 		/* If there's no inner vlan */
1403 		if (vxlan_spec && vxlan_mask && !is_vni_masked) {
1404 			/* If there's vxlan */
1405 			rte_memcpy(&filter->tenant_id, vxlan_spec->vni,
1406 				   RTE_DIM(vxlan_spec->vni));
1407 			if (!o_eth_spec && !o_eth_mask &&
1408 				i_eth_spec && i_eth_mask)
1409 				filter->filter_type =
1410 					RTE_TUNNEL_FILTER_IMAC_TENID;
1411 			else if (o_eth_spec && o_eth_mask &&
1412 				i_eth_spec && i_eth_mask)
1413 				filter->filter_type =
1414 					RTE_TUNNEL_FILTER_OMAC_TENID_IMAC;
1415 		} else if (!vxlan_spec && !vxlan_mask) {
1416 			/* If there's no vxlan */
1417 			if (!o_eth_spec && !o_eth_mask &&
1418 				i_eth_spec && i_eth_mask) {
1419 				filter->filter_type = ETH_TUNNEL_FILTER_IMAC;
1420 			} else {
1421 				rte_flow_error_set(error, EINVAL,
1422 					   RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1423 					   "Invalid filter type");
1424 				return -rte_errno;
1425 			}
1426 		} else {
1427 			rte_flow_error_set(error, EINVAL,
1428 					   RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1429 					   "Invalid filter type");
1430 			return -rte_errno;
1431 		}
1432 	} else {
1433 		rte_flow_error_set(error, EINVAL,
1434 				   RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1435 				   "Not supported by tunnel filter.");
1436 		return -rte_errno;
1437 	}
1438 
1439 	filter->tunnel_type = RTE_TUNNEL_TYPE_VXLAN;
1440 
1441 	return 0;
1442 }
1443 
1444 static int
1445 i40e_flow_parse_tunnel_pattern(__rte_unused struct rte_eth_dev *dev,
1446 			       const struct rte_flow_item *pattern,
1447 			       struct rte_flow_error *error,
1448 			       struct rte_eth_tunnel_filter_conf *filter)
1449 {
1450 	int ret;
1451 
1452 	ret = i40e_flow_parse_vxlan_pattern(pattern, error, filter);
1453 
1454 	return ret;
1455 }
1456 
1457 static int
1458 i40e_flow_parse_tunnel_filter(struct rte_eth_dev *dev,
1459 			      const struct rte_flow_attr *attr,
1460 			      const struct rte_flow_item pattern[],
1461 			      const struct rte_flow_action actions[],
1462 			      struct rte_flow_error *error,
1463 			      union i40e_filter_t *filter)
1464 {
1465 	struct rte_eth_tunnel_filter_conf *tunnel_filter =
1466 		&filter->tunnel_filter;
1467 	int ret;
1468 
1469 	ret = i40e_flow_parse_tunnel_pattern(dev, pattern,
1470 					     error, tunnel_filter);
1471 	if (ret)
1472 		return ret;
1473 
1474 	ret = i40e_flow_parse_tunnel_action(dev, actions, error, tunnel_filter);
1475 	if (ret)
1476 		return ret;
1477 
1478 	ret = i40e_flow_parse_attr(attr, error);
1479 	if (ret)
1480 		return ret;
1481 
1482 	cons_filter_type = RTE_ETH_FILTER_TUNNEL;
1483 
1484 	return ret;
1485 }
1486 
1487 static int
1488 i40e_flow_validate(struct rte_eth_dev *dev,
1489 		   const struct rte_flow_attr *attr,
1490 		   const struct rte_flow_item pattern[],
1491 		   const struct rte_flow_action actions[],
1492 		   struct rte_flow_error *error)
1493 {
1494 	struct rte_flow_item *items; /* internal pattern w/o VOID items */
1495 	parse_filter_t parse_filter;
1496 	uint32_t item_num = 0; /* non-void item number of pattern*/
1497 	uint32_t i = 0;
1498 	int ret;
1499 
1500 	if (!pattern) {
1501 		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1502 				   NULL, "NULL pattern.");
1503 		return -rte_errno;
1504 	}
1505 
1506 	if (!actions) {
1507 		rte_flow_error_set(error, EINVAL,
1508 				   RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1509 				   NULL, "NULL action.");
1510 		return -rte_errno;
1511 	}
1512 
1513 	if (!attr) {
1514 		rte_flow_error_set(error, EINVAL,
1515 				   RTE_FLOW_ERROR_TYPE_ATTR,
1516 				   NULL, "NULL attribute.");
1517 		return -rte_errno;
1518 	}
1519 
1520 	memset(&cons_filter, 0, sizeof(cons_filter));
1521 
1522 	/* Get the non-void item number of pattern */
1523 	while ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_END) {
1524 		if ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_VOID)
1525 			item_num++;
1526 		i++;
1527 	}
1528 	item_num++;
1529 
1530 	items = rte_zmalloc("i40e_pattern",
1531 			    item_num * sizeof(struct rte_flow_item), 0);
1532 	if (!items) {
1533 		rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1534 				   NULL, "No memory for PMD internal items.");
1535 		return -ENOMEM;
1536 	}
1537 
1538 	i40e_pattern_skip_void_item(items, pattern);
1539 
1540 	/* Find if there's matched parse filter function */
1541 	parse_filter = i40e_find_parse_filter_func(items);
1542 	if (!parse_filter) {
1543 		rte_flow_error_set(error, EINVAL,
1544 				   RTE_FLOW_ERROR_TYPE_ITEM,
1545 				   pattern, "Unsupported pattern");
1546 		return -rte_errno;
1547 	}
1548 
1549 	ret = parse_filter(dev, attr, items, actions, error, &cons_filter);
1550 
1551 	rte_free(items);
1552 
1553 	return ret;
1554 }
1555 
1556 static struct rte_flow *
1557 i40e_flow_create(struct rte_eth_dev *dev,
1558 		 const struct rte_flow_attr *attr,
1559 		 const struct rte_flow_item pattern[],
1560 		 const struct rte_flow_action actions[],
1561 		 struct rte_flow_error *error)
1562 {
1563 	struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1564 	struct rte_flow *flow;
1565 	int ret;
1566 
1567 	flow = rte_zmalloc("i40e_flow", sizeof(struct rte_flow), 0);
1568 	if (!flow) {
1569 		rte_flow_error_set(error, ENOMEM,
1570 				   RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1571 				   "Failed to allocate memory");
1572 		return flow;
1573 	}
1574 
1575 	ret = i40e_flow_validate(dev, attr, pattern, actions, error);
1576 	if (ret < 0)
1577 		return NULL;
1578 
1579 	switch (cons_filter_type) {
1580 	case RTE_ETH_FILTER_ETHERTYPE:
1581 		ret = i40e_ethertype_filter_set(pf,
1582 					&cons_filter.ethertype_filter, 1);
1583 		if (ret)
1584 			goto free_flow;
1585 		flow->rule = TAILQ_LAST(&pf->ethertype.ethertype_list,
1586 					i40e_ethertype_filter_list);
1587 		break;
1588 	case RTE_ETH_FILTER_FDIR:
1589 		ret = i40e_add_del_fdir_filter(dev,
1590 				       &cons_filter.fdir_filter, 1);
1591 		if (ret)
1592 			goto free_flow;
1593 		flow->rule = TAILQ_LAST(&pf->fdir.fdir_list,
1594 					i40e_fdir_filter_list);
1595 		break;
1596 	case RTE_ETH_FILTER_TUNNEL:
1597 		ret = i40e_dev_tunnel_filter_set(pf,
1598 					 &cons_filter.tunnel_filter, 1);
1599 		if (ret)
1600 			goto free_flow;
1601 		flow->rule = TAILQ_LAST(&pf->tunnel.tunnel_list,
1602 					i40e_tunnel_filter_list);
1603 		break;
1604 	default:
1605 		goto free_flow;
1606 	}
1607 
1608 	flow->filter_type = cons_filter_type;
1609 	TAILQ_INSERT_TAIL(&pf->flow_list, flow, node);
1610 	return flow;
1611 
1612 free_flow:
1613 	rte_flow_error_set(error, -ret,
1614 			   RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1615 			   "Failed to create flow.");
1616 	rte_free(flow);
1617 	return NULL;
1618 }
1619 
1620 static int
1621 i40e_flow_destroy(struct rte_eth_dev *dev,
1622 		  struct rte_flow *flow,
1623 		  struct rte_flow_error *error)
1624 {
1625 	struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1626 	enum rte_filter_type filter_type = flow->filter_type;
1627 	int ret = 0;
1628 
1629 	switch (filter_type) {
1630 	case RTE_ETH_FILTER_ETHERTYPE:
1631 		ret = i40e_flow_destroy_ethertype_filter(pf,
1632 			 (struct i40e_ethertype_filter *)flow->rule);
1633 		break;
1634 	case RTE_ETH_FILTER_TUNNEL:
1635 		ret = i40e_flow_destroy_tunnel_filter(pf,
1636 			      (struct i40e_tunnel_filter *)flow->rule);
1637 		break;
1638 	case RTE_ETH_FILTER_FDIR:
1639 		ret = i40e_add_del_fdir_filter(dev,
1640 		       &((struct i40e_fdir_filter *)flow->rule)->fdir, 0);
1641 		break;
1642 	default:
1643 		PMD_DRV_LOG(WARNING, "Filter type (%d) not supported",
1644 			    filter_type);
1645 		ret = -EINVAL;
1646 		break;
1647 	}
1648 
1649 	if (!ret) {
1650 		TAILQ_REMOVE(&pf->flow_list, flow, node);
1651 		rte_free(flow);
1652 	} else
1653 		rte_flow_error_set(error, -ret,
1654 				   RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1655 				   "Failed to destroy flow.");
1656 
1657 	return ret;
1658 }
1659 
1660 static int
1661 i40e_flow_destroy_ethertype_filter(struct i40e_pf *pf,
1662 				   struct i40e_ethertype_filter *filter)
1663 {
1664 	struct i40e_hw *hw = I40E_PF_TO_HW(pf);
1665 	struct i40e_ethertype_rule *ethertype_rule = &pf->ethertype;
1666 	struct i40e_ethertype_filter *node;
1667 	struct i40e_control_filter_stats stats;
1668 	uint16_t flags = 0;
1669 	int ret = 0;
1670 
1671 	if (!(filter->flags & RTE_ETHTYPE_FLAGS_MAC))
1672 		flags |= I40E_AQC_ADD_CONTROL_PACKET_FLAGS_IGNORE_MAC;
1673 	if (filter->flags & RTE_ETHTYPE_FLAGS_DROP)
1674 		flags |= I40E_AQC_ADD_CONTROL_PACKET_FLAGS_DROP;
1675 	flags |= I40E_AQC_ADD_CONTROL_PACKET_FLAGS_TO_QUEUE;
1676 
1677 	memset(&stats, 0, sizeof(stats));
1678 	ret = i40e_aq_add_rem_control_packet_filter(hw,
1679 				    filter->input.mac_addr.addr_bytes,
1680 				    filter->input.ether_type,
1681 				    flags, pf->main_vsi->seid,
1682 				    filter->queue, 0, &stats, NULL);
1683 	if (ret < 0)
1684 		return ret;
1685 
1686 	node = i40e_sw_ethertype_filter_lookup(ethertype_rule, &filter->input);
1687 	if (!node)
1688 		return -EINVAL;
1689 
1690 	ret = i40e_sw_ethertype_filter_del(pf, &node->input);
1691 
1692 	return ret;
1693 }
1694 
1695 static int
1696 i40e_flow_destroy_tunnel_filter(struct i40e_pf *pf,
1697 				struct i40e_tunnel_filter *filter)
1698 {
1699 	struct i40e_hw *hw = I40E_PF_TO_HW(pf);
1700 	struct i40e_vsi *vsi = pf->main_vsi;
1701 	struct i40e_aqc_add_remove_cloud_filters_element_data cld_filter;
1702 	struct i40e_tunnel_rule *tunnel_rule = &pf->tunnel;
1703 	struct i40e_tunnel_filter *node;
1704 	int ret = 0;
1705 
1706 	memset(&cld_filter, 0, sizeof(cld_filter));
1707 	ether_addr_copy((struct ether_addr *)&filter->input.outer_mac,
1708 			(struct ether_addr *)&cld_filter.outer_mac);
1709 	ether_addr_copy((struct ether_addr *)&filter->input.inner_mac,
1710 			(struct ether_addr *)&cld_filter.inner_mac);
1711 	cld_filter.inner_vlan = filter->input.inner_vlan;
1712 	cld_filter.flags = filter->input.flags;
1713 	cld_filter.tenant_id = filter->input.tenant_id;
1714 	cld_filter.queue_number = filter->queue;
1715 
1716 	ret = i40e_aq_remove_cloud_filters(hw, vsi->seid,
1717 					   &cld_filter, 1);
1718 	if (ret < 0)
1719 		return ret;
1720 
1721 	node = i40e_sw_tunnel_filter_lookup(tunnel_rule, &filter->input);
1722 	if (!node)
1723 		return -EINVAL;
1724 
1725 	ret = i40e_sw_tunnel_filter_del(pf, &node->input);
1726 
1727 	return ret;
1728 }
1729 
1730 static int
1731 i40e_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1732 {
1733 	struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1734 	int ret;
1735 
1736 	ret = i40e_flow_flush_fdir_filter(pf);
1737 	if (ret) {
1738 		rte_flow_error_set(error, -ret,
1739 				   RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1740 				   "Failed to flush FDIR flows.");
1741 		return -rte_errno;
1742 	}
1743 
1744 	ret = i40e_flow_flush_ethertype_filter(pf);
1745 	if (ret) {
1746 		rte_flow_error_set(error, -ret,
1747 				   RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1748 				   "Failed to ethertype flush flows.");
1749 		return -rte_errno;
1750 	}
1751 
1752 	ret = i40e_flow_flush_tunnel_filter(pf);
1753 	if (ret) {
1754 		rte_flow_error_set(error, -ret,
1755 				   RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1756 				   "Failed to flush tunnel flows.");
1757 		return -rte_errno;
1758 	}
1759 
1760 	return ret;
1761 }
1762 
1763 static int
1764 i40e_flow_flush_fdir_filter(struct i40e_pf *pf)
1765 {
1766 	struct rte_eth_dev *dev = pf->adapter->eth_dev;
1767 	struct i40e_fdir_info *fdir_info = &pf->fdir;
1768 	struct i40e_fdir_filter *fdir_filter;
1769 	struct rte_flow *flow;
1770 	void *temp;
1771 	int ret;
1772 
1773 	ret = i40e_fdir_flush(dev);
1774 	if (!ret) {
1775 		/* Delete FDIR filters in FDIR list. */
1776 		while ((fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list))) {
1777 			ret = i40e_sw_fdir_filter_del(pf,
1778 						      &fdir_filter->fdir.input);
1779 			if (ret < 0)
1780 				return ret;
1781 		}
1782 
1783 		/* Delete FDIR flows in flow list. */
1784 		TAILQ_FOREACH_SAFE(flow, &pf->flow_list, node, temp) {
1785 			if (flow->filter_type == RTE_ETH_FILTER_FDIR) {
1786 				TAILQ_REMOVE(&pf->flow_list, flow, node);
1787 				rte_free(flow);
1788 			}
1789 		}
1790 	}
1791 
1792 	return ret;
1793 }
1794 
1795 /* Flush all ethertype filters */
1796 static int
1797 i40e_flow_flush_ethertype_filter(struct i40e_pf *pf)
1798 {
1799 	struct i40e_ethertype_filter_list
1800 		*ethertype_list = &pf->ethertype.ethertype_list;
1801 	struct i40e_ethertype_filter *filter;
1802 	struct rte_flow *flow;
1803 	void *temp;
1804 	int ret = 0;
1805 
1806 	while ((filter = TAILQ_FIRST(ethertype_list))) {
1807 		ret = i40e_flow_destroy_ethertype_filter(pf, filter);
1808 		if (ret)
1809 			return ret;
1810 	}
1811 
1812 	/* Delete ethertype flows in flow list. */
1813 	TAILQ_FOREACH_SAFE(flow, &pf->flow_list, node, temp) {
1814 		if (flow->filter_type == RTE_ETH_FILTER_ETHERTYPE) {
1815 			TAILQ_REMOVE(&pf->flow_list, flow, node);
1816 			rte_free(flow);
1817 		}
1818 	}
1819 
1820 	return ret;
1821 }
1822 
1823 /* Flush all tunnel filters */
1824 static int
1825 i40e_flow_flush_tunnel_filter(struct i40e_pf *pf)
1826 {
1827 	struct i40e_tunnel_filter_list
1828 		*tunnel_list = &pf->tunnel.tunnel_list;
1829 	struct i40e_tunnel_filter *filter;
1830 	struct rte_flow *flow;
1831 	void *temp;
1832 	int ret = 0;
1833 
1834 	while ((filter = TAILQ_FIRST(tunnel_list))) {
1835 		ret = i40e_flow_destroy_tunnel_filter(pf, filter);
1836 		if (ret)
1837 			return ret;
1838 	}
1839 
1840 	/* Delete tunnel flows in flow list. */
1841 	TAILQ_FOREACH_SAFE(flow, &pf->flow_list, node, temp) {
1842 		if (flow->filter_type == RTE_ETH_FILTER_TUNNEL) {
1843 			TAILQ_REMOVE(&pf->flow_list, flow, node);
1844 			rte_free(flow);
1845 		}
1846 	}
1847 
1848 	return ret;
1849 }
1850