net/i40e: add check for invalid VF queue number
[dpdk.git] / drivers / net / i40e / i40e_flow.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2016-2017 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_action(struct rte_eth_dev *dev,
94                                  const struct rte_flow_action *actions,
95                                  struct rte_flow_error *error,
96                                  struct i40e_tunnel_filter_conf *filter);
97 static int i40e_flow_parse_attr(const struct rte_flow_attr *attr,
98                                 struct rte_flow_error *error);
99 static int i40e_flow_parse_ethertype_filter(struct rte_eth_dev *dev,
100                                     const struct rte_flow_attr *attr,
101                                     const struct rte_flow_item pattern[],
102                                     const struct rte_flow_action actions[],
103                                     struct rte_flow_error *error,
104                                     union i40e_filter_t *filter);
105 static int i40e_flow_parse_fdir_filter(struct rte_eth_dev *dev,
106                                        const struct rte_flow_attr *attr,
107                                        const struct rte_flow_item pattern[],
108                                        const struct rte_flow_action actions[],
109                                        struct rte_flow_error *error,
110                                        union i40e_filter_t *filter);
111 static int i40e_flow_parse_vxlan_filter(struct rte_eth_dev *dev,
112                                         const struct rte_flow_attr *attr,
113                                         const struct rte_flow_item pattern[],
114                                         const struct rte_flow_action actions[],
115                                         struct rte_flow_error *error,
116                                         union i40e_filter_t *filter);
117 static int i40e_flow_parse_mpls_filter(struct rte_eth_dev *dev,
118                                        const struct rte_flow_attr *attr,
119                                        const struct rte_flow_item pattern[],
120                                        const struct rte_flow_action actions[],
121                                        struct rte_flow_error *error,
122                                        union i40e_filter_t *filter);
123 static int i40e_flow_destroy_ethertype_filter(struct i40e_pf *pf,
124                                       struct i40e_ethertype_filter *filter);
125 static int i40e_flow_destroy_tunnel_filter(struct i40e_pf *pf,
126                                            struct i40e_tunnel_filter *filter);
127 static int i40e_flow_flush_fdir_filter(struct i40e_pf *pf);
128 static int i40e_flow_flush_ethertype_filter(struct i40e_pf *pf);
129 static int i40e_flow_flush_tunnel_filter(struct i40e_pf *pf);
130 static int
131 i40e_flow_parse_qinq_filter(struct rte_eth_dev *dev,
132                               const struct rte_flow_attr *attr,
133                               const struct rte_flow_item pattern[],
134                               const struct rte_flow_action actions[],
135                               struct rte_flow_error *error,
136                               union i40e_filter_t *filter);
137 static int
138 i40e_flow_parse_qinq_pattern(__rte_unused struct rte_eth_dev *dev,
139                               const struct rte_flow_item *pattern,
140                               struct rte_flow_error *error,
141                               struct i40e_tunnel_filter_conf *filter);
142
143 const struct rte_flow_ops i40e_flow_ops = {
144         .validate = i40e_flow_validate,
145         .create = i40e_flow_create,
146         .destroy = i40e_flow_destroy,
147         .flush = i40e_flow_flush,
148 };
149
150 union i40e_filter_t cons_filter;
151 enum rte_filter_type cons_filter_type = RTE_ETH_FILTER_NONE;
152
153 /* Pattern matched ethertype filter */
154 static enum rte_flow_item_type pattern_ethertype[] = {
155         RTE_FLOW_ITEM_TYPE_ETH,
156         RTE_FLOW_ITEM_TYPE_END,
157 };
158
159 /* Pattern matched flow director filter */
160 static enum rte_flow_item_type pattern_fdir_ipv4[] = {
161         RTE_FLOW_ITEM_TYPE_IPV4,
162         RTE_FLOW_ITEM_TYPE_END,
163 };
164
165 static enum rte_flow_item_type pattern_fdir_ipv4_ext[] = {
166         RTE_FLOW_ITEM_TYPE_ETH,
167         RTE_FLOW_ITEM_TYPE_IPV4,
168         RTE_FLOW_ITEM_TYPE_END,
169 };
170
171 static enum rte_flow_item_type pattern_fdir_ipv4_udp[] = {
172         RTE_FLOW_ITEM_TYPE_IPV4,
173         RTE_FLOW_ITEM_TYPE_UDP,
174         RTE_FLOW_ITEM_TYPE_END,
175 };
176
177 static enum rte_flow_item_type pattern_fdir_ipv4_udp_ext[] = {
178         RTE_FLOW_ITEM_TYPE_ETH,
179         RTE_FLOW_ITEM_TYPE_IPV4,
180         RTE_FLOW_ITEM_TYPE_UDP,
181         RTE_FLOW_ITEM_TYPE_END,
182 };
183
184 static enum rte_flow_item_type pattern_fdir_ipv4_tcp[] = {
185         RTE_FLOW_ITEM_TYPE_IPV4,
186         RTE_FLOW_ITEM_TYPE_TCP,
187         RTE_FLOW_ITEM_TYPE_END,
188 };
189
190 static enum rte_flow_item_type pattern_fdir_ipv4_tcp_ext[] = {
191         RTE_FLOW_ITEM_TYPE_ETH,
192         RTE_FLOW_ITEM_TYPE_IPV4,
193         RTE_FLOW_ITEM_TYPE_TCP,
194         RTE_FLOW_ITEM_TYPE_END,
195 };
196
197 static enum rte_flow_item_type pattern_fdir_ipv4_sctp[] = {
198         RTE_FLOW_ITEM_TYPE_IPV4,
199         RTE_FLOW_ITEM_TYPE_SCTP,
200         RTE_FLOW_ITEM_TYPE_END,
201 };
202
203 static enum rte_flow_item_type pattern_fdir_ipv4_sctp_ext[] = {
204         RTE_FLOW_ITEM_TYPE_ETH,
205         RTE_FLOW_ITEM_TYPE_IPV4,
206         RTE_FLOW_ITEM_TYPE_SCTP,
207         RTE_FLOW_ITEM_TYPE_END,
208 };
209
210 static enum rte_flow_item_type pattern_fdir_ipv6[] = {
211         RTE_FLOW_ITEM_TYPE_IPV6,
212         RTE_FLOW_ITEM_TYPE_END,
213 };
214
215 static enum rte_flow_item_type pattern_fdir_ipv6_ext[] = {
216         RTE_FLOW_ITEM_TYPE_ETH,
217         RTE_FLOW_ITEM_TYPE_IPV6,
218         RTE_FLOW_ITEM_TYPE_END,
219 };
220
221 static enum rte_flow_item_type pattern_fdir_ipv6_udp[] = {
222         RTE_FLOW_ITEM_TYPE_IPV6,
223         RTE_FLOW_ITEM_TYPE_UDP,
224         RTE_FLOW_ITEM_TYPE_END,
225 };
226
227 static enum rte_flow_item_type pattern_fdir_ipv6_udp_ext[] = {
228         RTE_FLOW_ITEM_TYPE_ETH,
229         RTE_FLOW_ITEM_TYPE_IPV6,
230         RTE_FLOW_ITEM_TYPE_UDP,
231         RTE_FLOW_ITEM_TYPE_END,
232 };
233
234 static enum rte_flow_item_type pattern_fdir_ipv6_tcp[] = {
235         RTE_FLOW_ITEM_TYPE_IPV6,
236         RTE_FLOW_ITEM_TYPE_TCP,
237         RTE_FLOW_ITEM_TYPE_END,
238 };
239
240 static enum rte_flow_item_type pattern_fdir_ipv6_tcp_ext[] = {
241         RTE_FLOW_ITEM_TYPE_ETH,
242         RTE_FLOW_ITEM_TYPE_IPV6,
243         RTE_FLOW_ITEM_TYPE_TCP,
244         RTE_FLOW_ITEM_TYPE_END,
245 };
246
247 static enum rte_flow_item_type pattern_fdir_ipv6_sctp[] = {
248         RTE_FLOW_ITEM_TYPE_IPV6,
249         RTE_FLOW_ITEM_TYPE_SCTP,
250         RTE_FLOW_ITEM_TYPE_END,
251 };
252
253 static enum rte_flow_item_type pattern_fdir_ipv6_sctp_ext[] = {
254         RTE_FLOW_ITEM_TYPE_ETH,
255         RTE_FLOW_ITEM_TYPE_IPV6,
256         RTE_FLOW_ITEM_TYPE_SCTP,
257         RTE_FLOW_ITEM_TYPE_END,
258 };
259
260 /* Pattern matched tunnel filter */
261 static enum rte_flow_item_type pattern_vxlan_1[] = {
262         RTE_FLOW_ITEM_TYPE_ETH,
263         RTE_FLOW_ITEM_TYPE_IPV4,
264         RTE_FLOW_ITEM_TYPE_UDP,
265         RTE_FLOW_ITEM_TYPE_VXLAN,
266         RTE_FLOW_ITEM_TYPE_ETH,
267         RTE_FLOW_ITEM_TYPE_END,
268 };
269
270 static enum rte_flow_item_type pattern_vxlan_2[] = {
271         RTE_FLOW_ITEM_TYPE_ETH,
272         RTE_FLOW_ITEM_TYPE_IPV6,
273         RTE_FLOW_ITEM_TYPE_UDP,
274         RTE_FLOW_ITEM_TYPE_VXLAN,
275         RTE_FLOW_ITEM_TYPE_ETH,
276         RTE_FLOW_ITEM_TYPE_END,
277 };
278
279 static enum rte_flow_item_type pattern_vxlan_3[] = {
280         RTE_FLOW_ITEM_TYPE_ETH,
281         RTE_FLOW_ITEM_TYPE_IPV4,
282         RTE_FLOW_ITEM_TYPE_UDP,
283         RTE_FLOW_ITEM_TYPE_VXLAN,
284         RTE_FLOW_ITEM_TYPE_ETH,
285         RTE_FLOW_ITEM_TYPE_VLAN,
286         RTE_FLOW_ITEM_TYPE_END,
287 };
288
289 static enum rte_flow_item_type pattern_vxlan_4[] = {
290         RTE_FLOW_ITEM_TYPE_ETH,
291         RTE_FLOW_ITEM_TYPE_IPV6,
292         RTE_FLOW_ITEM_TYPE_UDP,
293         RTE_FLOW_ITEM_TYPE_VXLAN,
294         RTE_FLOW_ITEM_TYPE_ETH,
295         RTE_FLOW_ITEM_TYPE_VLAN,
296         RTE_FLOW_ITEM_TYPE_END,
297 };
298
299 /* Pattern matched MPLS */
300 static enum rte_flow_item_type pattern_mpls_1[] = {
301         RTE_FLOW_ITEM_TYPE_ETH,
302         RTE_FLOW_ITEM_TYPE_IPV4,
303         RTE_FLOW_ITEM_TYPE_UDP,
304         RTE_FLOW_ITEM_TYPE_MPLS,
305         RTE_FLOW_ITEM_TYPE_END,
306 };
307
308 static enum rte_flow_item_type pattern_mpls_2[] = {
309         RTE_FLOW_ITEM_TYPE_ETH,
310         RTE_FLOW_ITEM_TYPE_IPV6,
311         RTE_FLOW_ITEM_TYPE_UDP,
312         RTE_FLOW_ITEM_TYPE_MPLS,
313         RTE_FLOW_ITEM_TYPE_END,
314 };
315
316 static enum rte_flow_item_type pattern_mpls_3[] = {
317         RTE_FLOW_ITEM_TYPE_ETH,
318         RTE_FLOW_ITEM_TYPE_IPV4,
319         RTE_FLOW_ITEM_TYPE_GRE,
320         RTE_FLOW_ITEM_TYPE_MPLS,
321         RTE_FLOW_ITEM_TYPE_END,
322 };
323
324 static enum rte_flow_item_type pattern_mpls_4[] = {
325         RTE_FLOW_ITEM_TYPE_ETH,
326         RTE_FLOW_ITEM_TYPE_IPV6,
327         RTE_FLOW_ITEM_TYPE_GRE,
328         RTE_FLOW_ITEM_TYPE_MPLS,
329         RTE_FLOW_ITEM_TYPE_END,
330 };
331
332 /* Pattern matched QINQ */
333 static enum rte_flow_item_type pattern_qinq_1[] = {
334         RTE_FLOW_ITEM_TYPE_ETH,
335         RTE_FLOW_ITEM_TYPE_VLAN,
336         RTE_FLOW_ITEM_TYPE_VLAN,
337         RTE_FLOW_ITEM_TYPE_END,
338 };
339
340 static struct i40e_valid_pattern i40e_supported_patterns[] = {
341         /* Ethertype */
342         { pattern_ethertype, i40e_flow_parse_ethertype_filter },
343         /* FDIR */
344         { pattern_fdir_ipv4, i40e_flow_parse_fdir_filter },
345         { pattern_fdir_ipv4_ext, i40e_flow_parse_fdir_filter },
346         { pattern_fdir_ipv4_udp, i40e_flow_parse_fdir_filter },
347         { pattern_fdir_ipv4_udp_ext, i40e_flow_parse_fdir_filter },
348         { pattern_fdir_ipv4_tcp, i40e_flow_parse_fdir_filter },
349         { pattern_fdir_ipv4_tcp_ext, i40e_flow_parse_fdir_filter },
350         { pattern_fdir_ipv4_sctp, i40e_flow_parse_fdir_filter },
351         { pattern_fdir_ipv4_sctp_ext, i40e_flow_parse_fdir_filter },
352         { pattern_fdir_ipv6, i40e_flow_parse_fdir_filter },
353         { pattern_fdir_ipv6_ext, i40e_flow_parse_fdir_filter },
354         { pattern_fdir_ipv6_udp, i40e_flow_parse_fdir_filter },
355         { pattern_fdir_ipv6_udp_ext, i40e_flow_parse_fdir_filter },
356         { pattern_fdir_ipv6_tcp, i40e_flow_parse_fdir_filter },
357         { pattern_fdir_ipv6_tcp_ext, i40e_flow_parse_fdir_filter },
358         { pattern_fdir_ipv6_sctp, i40e_flow_parse_fdir_filter },
359         { pattern_fdir_ipv6_sctp_ext, i40e_flow_parse_fdir_filter },
360         /* VXLAN */
361         { pattern_vxlan_1, i40e_flow_parse_vxlan_filter },
362         { pattern_vxlan_2, i40e_flow_parse_vxlan_filter },
363         { pattern_vxlan_3, i40e_flow_parse_vxlan_filter },
364         { pattern_vxlan_4, i40e_flow_parse_vxlan_filter },
365         /* MPLSoUDP & MPLSoGRE */
366         { pattern_mpls_1, i40e_flow_parse_mpls_filter },
367         { pattern_mpls_2, i40e_flow_parse_mpls_filter },
368         { pattern_mpls_3, i40e_flow_parse_mpls_filter },
369         { pattern_mpls_4, i40e_flow_parse_mpls_filter },
370         /* QINQ */
371         { pattern_qinq_1, i40e_flow_parse_qinq_filter },
372 };
373
374 #define NEXT_ITEM_OF_ACTION(act, actions, index)                        \
375         do {                                                            \
376                 act = actions + index;                                  \
377                 while (act->type == RTE_FLOW_ACTION_TYPE_VOID) {        \
378                         index++;                                        \
379                         act = actions + index;                          \
380                 }                                                       \
381         } while (0)
382
383 /* Find the first VOID or non-VOID item pointer */
384 static const struct rte_flow_item *
385 i40e_find_first_item(const struct rte_flow_item *item, bool is_void)
386 {
387         bool is_find;
388
389         while (item->type != RTE_FLOW_ITEM_TYPE_END) {
390                 if (is_void)
391                         is_find = item->type == RTE_FLOW_ITEM_TYPE_VOID;
392                 else
393                         is_find = item->type != RTE_FLOW_ITEM_TYPE_VOID;
394                 if (is_find)
395                         break;
396                 item++;
397         }
398         return item;
399 }
400
401 /* Skip all VOID items of the pattern */
402 static void
403 i40e_pattern_skip_void_item(struct rte_flow_item *items,
404                             const struct rte_flow_item *pattern)
405 {
406         uint32_t cpy_count = 0;
407         const struct rte_flow_item *pb = pattern, *pe = pattern;
408
409         for (;;) {
410                 /* Find a non-void item first */
411                 pb = i40e_find_first_item(pb, false);
412                 if (pb->type == RTE_FLOW_ITEM_TYPE_END) {
413                         pe = pb;
414                         break;
415                 }
416
417                 /* Find a void item */
418                 pe = i40e_find_first_item(pb + 1, true);
419
420                 cpy_count = pe - pb;
421                 rte_memcpy(items, pb, sizeof(struct rte_flow_item) * cpy_count);
422
423                 items += cpy_count;
424
425                 if (pe->type == RTE_FLOW_ITEM_TYPE_END) {
426                         pb = pe;
427                         break;
428                 }
429
430                 pb = pe + 1;
431         }
432         /* Copy the END item. */
433         rte_memcpy(items, pe, sizeof(struct rte_flow_item));
434 }
435
436 /* Check if the pattern matches a supported item type array */
437 static bool
438 i40e_match_pattern(enum rte_flow_item_type *item_array,
439                    struct rte_flow_item *pattern)
440 {
441         struct rte_flow_item *item = pattern;
442
443         while ((*item_array == item->type) &&
444                (*item_array != RTE_FLOW_ITEM_TYPE_END)) {
445                 item_array++;
446                 item++;
447         }
448
449         return (*item_array == RTE_FLOW_ITEM_TYPE_END &&
450                 item->type == RTE_FLOW_ITEM_TYPE_END);
451 }
452
453 /* Find if there's parse filter function matched */
454 static parse_filter_t
455 i40e_find_parse_filter_func(struct rte_flow_item *pattern)
456 {
457         parse_filter_t parse_filter = NULL;
458         uint8_t i = 0;
459
460         for (; i < RTE_DIM(i40e_supported_patterns); i++) {
461                 if (i40e_match_pattern(i40e_supported_patterns[i].items,
462                                         pattern)) {
463                         parse_filter = i40e_supported_patterns[i].parse_filter;
464                         break;
465                 }
466         }
467
468         return parse_filter;
469 }
470
471 /* Parse attributes */
472 static int
473 i40e_flow_parse_attr(const struct rte_flow_attr *attr,
474                      struct rte_flow_error *error)
475 {
476         /* Must be input direction */
477         if (!attr->ingress) {
478                 rte_flow_error_set(error, EINVAL,
479                                    RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
480                                    attr, "Only support ingress.");
481                 return -rte_errno;
482         }
483
484         /* Not supported */
485         if (attr->egress) {
486                 rte_flow_error_set(error, EINVAL,
487                                    RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
488                                    attr, "Not support egress.");
489                 return -rte_errno;
490         }
491
492         /* Not supported */
493         if (attr->priority) {
494                 rte_flow_error_set(error, EINVAL,
495                                    RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
496                                    attr, "Not support priority.");
497                 return -rte_errno;
498         }
499
500         /* Not supported */
501         if (attr->group) {
502                 rte_flow_error_set(error, EINVAL,
503                                    RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
504                                    attr, "Not support group.");
505                 return -rte_errno;
506         }
507
508         return 0;
509 }
510
511 static uint16_t
512 i40e_get_outer_vlan(struct rte_eth_dev *dev)
513 {
514         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
515         int qinq = dev->data->dev_conf.rxmode.hw_vlan_extend;
516         uint64_t reg_r = 0;
517         uint16_t reg_id;
518         uint16_t tpid;
519
520         if (qinq)
521                 reg_id = 2;
522         else
523                 reg_id = 3;
524
525         i40e_aq_debug_read_register(hw, I40E_GL_SWT_L2TAGCTRL(reg_id),
526                                     &reg_r, NULL);
527
528         tpid = (reg_r >> I40E_GL_SWT_L2TAGCTRL_ETHERTYPE_SHIFT) & 0xFFFF;
529
530         return tpid;
531 }
532
533 /* 1. Last in item should be NULL as range is not supported.
534  * 2. Supported filter types: MAC_ETHTYPE and ETHTYPE.
535  * 3. SRC mac_addr mask should be 00:00:00:00:00:00.
536  * 4. DST mac_addr mask should be 00:00:00:00:00:00 or
537  *    FF:FF:FF:FF:FF:FF
538  * 5. Ether_type mask should be 0xFFFF.
539  */
540 static int
541 i40e_flow_parse_ethertype_pattern(struct rte_eth_dev *dev,
542                                   const struct rte_flow_item *pattern,
543                                   struct rte_flow_error *error,
544                                   struct rte_eth_ethertype_filter *filter)
545 {
546         const struct rte_flow_item *item = pattern;
547         const struct rte_flow_item_eth *eth_spec;
548         const struct rte_flow_item_eth *eth_mask;
549         enum rte_flow_item_type item_type;
550         uint16_t outer_tpid;
551
552         outer_tpid = i40e_get_outer_vlan(dev);
553
554         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
555                 if (item->last) {
556                         rte_flow_error_set(error, EINVAL,
557                                            RTE_FLOW_ERROR_TYPE_ITEM,
558                                            item,
559                                            "Not support range");
560                         return -rte_errno;
561                 }
562                 item_type = item->type;
563                 switch (item_type) {
564                 case RTE_FLOW_ITEM_TYPE_ETH:
565                         eth_spec = (const struct rte_flow_item_eth *)item->spec;
566                         eth_mask = (const struct rte_flow_item_eth *)item->mask;
567                         /* Get the MAC info. */
568                         if (!eth_spec || !eth_mask) {
569                                 rte_flow_error_set(error, EINVAL,
570                                                    RTE_FLOW_ERROR_TYPE_ITEM,
571                                                    item,
572                                                    "NULL ETH spec/mask");
573                                 return -rte_errno;
574                         }
575
576                         /* Mask bits of source MAC address must be full of 0.
577                          * Mask bits of destination MAC address must be full
578                          * of 1 or full of 0.
579                          */
580                         if (!is_zero_ether_addr(&eth_mask->src) ||
581                             (!is_zero_ether_addr(&eth_mask->dst) &&
582                              !is_broadcast_ether_addr(&eth_mask->dst))) {
583                                 rte_flow_error_set(error, EINVAL,
584                                                    RTE_FLOW_ERROR_TYPE_ITEM,
585                                                    item,
586                                                    "Invalid MAC_addr mask");
587                                 return -rte_errno;
588                         }
589
590                         if ((eth_mask->type & UINT16_MAX) != UINT16_MAX) {
591                                 rte_flow_error_set(error, EINVAL,
592                                                    RTE_FLOW_ERROR_TYPE_ITEM,
593                                                    item,
594                                                    "Invalid ethertype mask");
595                                 return -rte_errno;
596                         }
597
598                         /* If mask bits of destination MAC address
599                          * are full of 1, set RTE_ETHTYPE_FLAGS_MAC.
600                          */
601                         if (is_broadcast_ether_addr(&eth_mask->dst)) {
602                                 filter->mac_addr = eth_spec->dst;
603                                 filter->flags |= RTE_ETHTYPE_FLAGS_MAC;
604                         } else {
605                                 filter->flags &= ~RTE_ETHTYPE_FLAGS_MAC;
606                         }
607                         filter->ether_type = rte_be_to_cpu_16(eth_spec->type);
608
609                         if (filter->ether_type == ETHER_TYPE_IPv4 ||
610                             filter->ether_type == ETHER_TYPE_IPv6 ||
611                             filter->ether_type == outer_tpid) {
612                                 rte_flow_error_set(error, EINVAL,
613                                                    RTE_FLOW_ERROR_TYPE_ITEM,
614                                                    item,
615                                                    "Unsupported ether_type in"
616                                                    " control packet filter.");
617                                 return -rte_errno;
618                         }
619                         break;
620                 default:
621                         break;
622                 }
623         }
624
625         return 0;
626 }
627
628 /* Ethertype action only supports QUEUE or DROP. */
629 static int
630 i40e_flow_parse_ethertype_action(struct rte_eth_dev *dev,
631                                  const struct rte_flow_action *actions,
632                                  struct rte_flow_error *error,
633                                  struct rte_eth_ethertype_filter *filter)
634 {
635         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
636         const struct rte_flow_action *act;
637         const struct rte_flow_action_queue *act_q;
638         uint32_t index = 0;
639
640         /* Check if the first non-void action is QUEUE or DROP. */
641         NEXT_ITEM_OF_ACTION(act, actions, index);
642         if (act->type != RTE_FLOW_ACTION_TYPE_QUEUE &&
643             act->type != RTE_FLOW_ACTION_TYPE_DROP) {
644                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
645                                    act, "Not supported action.");
646                 return -rte_errno;
647         }
648
649         if (act->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
650                 act_q = (const struct rte_flow_action_queue *)act->conf;
651                 filter->queue = act_q->index;
652                 if (filter->queue >= pf->dev_data->nb_rx_queues) {
653                         rte_flow_error_set(error, EINVAL,
654                                            RTE_FLOW_ERROR_TYPE_ACTION,
655                                            act, "Invalid queue ID for"
656                                            " ethertype_filter.");
657                         return -rte_errno;
658                 }
659         } else {
660                 filter->flags |= RTE_ETHTYPE_FLAGS_DROP;
661         }
662
663         /* Check if the next non-void item is END */
664         index++;
665         NEXT_ITEM_OF_ACTION(act, actions, index);
666         if (act->type != RTE_FLOW_ACTION_TYPE_END) {
667                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
668                                    act, "Not supported action.");
669                 return -rte_errno;
670         }
671
672         return 0;
673 }
674
675 static int
676 i40e_flow_parse_ethertype_filter(struct rte_eth_dev *dev,
677                                  const struct rte_flow_attr *attr,
678                                  const struct rte_flow_item pattern[],
679                                  const struct rte_flow_action actions[],
680                                  struct rte_flow_error *error,
681                                  union i40e_filter_t *filter)
682 {
683         struct rte_eth_ethertype_filter *ethertype_filter =
684                 &filter->ethertype_filter;
685         int ret;
686
687         ret = i40e_flow_parse_ethertype_pattern(dev, pattern, error,
688                                                 ethertype_filter);
689         if (ret)
690                 return ret;
691
692         ret = i40e_flow_parse_ethertype_action(dev, actions, error,
693                                                ethertype_filter);
694         if (ret)
695                 return ret;
696
697         ret = i40e_flow_parse_attr(attr, error);
698         if (ret)
699                 return ret;
700
701         cons_filter_type = RTE_ETH_FILTER_ETHERTYPE;
702
703         return ret;
704 }
705
706 /* 1. Last in item should be NULL as range is not supported.
707  * 2. Supported flow type and input set: refer to array
708  *    default_inset_table in i40e_ethdev.c.
709  * 3. Mask of fields which need to be matched should be
710  *    filled with 1.
711  * 4. Mask of fields which needn't to be matched should be
712  *    filled with 0.
713  */
714 static int
715 i40e_flow_parse_fdir_pattern(struct rte_eth_dev *dev,
716                              const struct rte_flow_item *pattern,
717                              struct rte_flow_error *error,
718                              struct rte_eth_fdir_filter *filter)
719 {
720         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
721         const struct rte_flow_item *item = pattern;
722         const struct rte_flow_item_eth *eth_spec, *eth_mask;
723         const struct rte_flow_item_ipv4 *ipv4_spec, *ipv4_mask;
724         const struct rte_flow_item_ipv6 *ipv6_spec, *ipv6_mask;
725         const struct rte_flow_item_tcp *tcp_spec, *tcp_mask;
726         const struct rte_flow_item_udp *udp_spec, *udp_mask;
727         const struct rte_flow_item_sctp *sctp_spec, *sctp_mask;
728         const struct rte_flow_item_vf *vf_spec;
729         uint32_t flow_type = RTE_ETH_FLOW_UNKNOWN;
730         enum i40e_filter_pctype pctype;
731         uint64_t input_set = I40E_INSET_NONE;
732         uint16_t flag_offset;
733         enum rte_flow_item_type item_type;
734         enum rte_flow_item_type l3 = RTE_FLOW_ITEM_TYPE_END;
735         uint32_t j;
736
737         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
738                 if (item->last) {
739                         rte_flow_error_set(error, EINVAL,
740                                            RTE_FLOW_ERROR_TYPE_ITEM,
741                                            item,
742                                            "Not support range");
743                         return -rte_errno;
744                 }
745                 item_type = item->type;
746                 switch (item_type) {
747                 case RTE_FLOW_ITEM_TYPE_ETH:
748                         eth_spec = (const struct rte_flow_item_eth *)item->spec;
749                         eth_mask = (const struct rte_flow_item_eth *)item->mask;
750                         if (eth_spec || eth_mask) {
751                                 rte_flow_error_set(error, EINVAL,
752                                                    RTE_FLOW_ERROR_TYPE_ITEM,
753                                                    item,
754                                                    "Invalid ETH spec/mask");
755                                 return -rte_errno;
756                         }
757                         break;
758                 case RTE_FLOW_ITEM_TYPE_IPV4:
759                         l3 = RTE_FLOW_ITEM_TYPE_IPV4;
760                         ipv4_spec =
761                                 (const struct rte_flow_item_ipv4 *)item->spec;
762                         ipv4_mask =
763                                 (const struct rte_flow_item_ipv4 *)item->mask;
764                         if (!ipv4_spec || !ipv4_mask) {
765                                 rte_flow_error_set(error, EINVAL,
766                                                    RTE_FLOW_ERROR_TYPE_ITEM,
767                                                    item,
768                                                    "NULL IPv4 spec/mask");
769                                 return -rte_errno;
770                         }
771
772                         /* Check IPv4 mask and update input set */
773                         if (ipv4_mask->hdr.version_ihl ||
774                             ipv4_mask->hdr.total_length ||
775                             ipv4_mask->hdr.packet_id ||
776                             ipv4_mask->hdr.fragment_offset ||
777                             ipv4_mask->hdr.hdr_checksum) {
778                                 rte_flow_error_set(error, EINVAL,
779                                                    RTE_FLOW_ERROR_TYPE_ITEM,
780                                                    item,
781                                                    "Invalid IPv4 mask.");
782                                 return -rte_errno;
783                         }
784
785                         if (ipv4_mask->hdr.src_addr == UINT32_MAX)
786                                 input_set |= I40E_INSET_IPV4_SRC;
787                         if (ipv4_mask->hdr.dst_addr == UINT32_MAX)
788                                 input_set |= I40E_INSET_IPV4_DST;
789                         if (ipv4_mask->hdr.type_of_service == UINT8_MAX)
790                                 input_set |= I40E_INSET_IPV4_TOS;
791                         if (ipv4_mask->hdr.time_to_live == UINT8_MAX)
792                                 input_set |= I40E_INSET_IPV4_TTL;
793                         if (ipv4_mask->hdr.next_proto_id == UINT8_MAX)
794                                 input_set |= I40E_INSET_IPV4_PROTO;
795
796                         /* Get filter info */
797                         flow_type = RTE_ETH_FLOW_NONFRAG_IPV4_OTHER;
798                         /* Check if it is fragment. */
799                         flag_offset =
800                               rte_be_to_cpu_16(ipv4_spec->hdr.fragment_offset);
801                         if (flag_offset & IPV4_HDR_OFFSET_MASK ||
802                             flag_offset & IPV4_HDR_MF_FLAG)
803                                 flow_type = RTE_ETH_FLOW_FRAG_IPV4;
804
805                         /* Get the filter info */
806                         filter->input.flow.ip4_flow.proto =
807                                 ipv4_spec->hdr.next_proto_id;
808                         filter->input.flow.ip4_flow.tos =
809                                 ipv4_spec->hdr.type_of_service;
810                         filter->input.flow.ip4_flow.ttl =
811                                 ipv4_spec->hdr.time_to_live;
812                         filter->input.flow.ip4_flow.src_ip =
813                                 ipv4_spec->hdr.src_addr;
814                         filter->input.flow.ip4_flow.dst_ip =
815                                 ipv4_spec->hdr.dst_addr;
816
817                         break;
818                 case RTE_FLOW_ITEM_TYPE_IPV6:
819                         l3 = RTE_FLOW_ITEM_TYPE_IPV6;
820                         ipv6_spec =
821                                 (const struct rte_flow_item_ipv6 *)item->spec;
822                         ipv6_mask =
823                                 (const struct rte_flow_item_ipv6 *)item->mask;
824                         if (!ipv6_spec || !ipv6_mask) {
825                                 rte_flow_error_set(error, EINVAL,
826                                                    RTE_FLOW_ERROR_TYPE_ITEM,
827                                                    item,
828                                                    "NULL IPv6 spec/mask");
829                                 return -rte_errno;
830                         }
831
832                         /* Check IPv6 mask and update input set */
833                         if (ipv6_mask->hdr.payload_len) {
834                                 rte_flow_error_set(error, EINVAL,
835                                                    RTE_FLOW_ERROR_TYPE_ITEM,
836                                                    item,
837                                                    "Invalid IPv6 mask");
838                                 return -rte_errno;
839                         }
840
841                         /* SCR and DST address of IPv6 shouldn't be masked */
842                         for (j = 0; j < RTE_DIM(ipv6_mask->hdr.src_addr); j++) {
843                                 if (ipv6_mask->hdr.src_addr[j] != UINT8_MAX ||
844                                     ipv6_mask->hdr.dst_addr[j] != UINT8_MAX) {
845                                         rte_flow_error_set(error, EINVAL,
846                                                    RTE_FLOW_ERROR_TYPE_ITEM,
847                                                    item,
848                                                    "Invalid IPv6 mask");
849                                         return -rte_errno;
850                                 }
851                         }
852
853                         input_set |= I40E_INSET_IPV6_SRC;
854                         input_set |= I40E_INSET_IPV6_DST;
855
856                         if ((ipv6_mask->hdr.vtc_flow &
857                              rte_cpu_to_be_16(I40E_IPV6_TC_MASK))
858                             == rte_cpu_to_be_16(I40E_IPV6_TC_MASK))
859                                 input_set |= I40E_INSET_IPV6_TC;
860                         if (ipv6_mask->hdr.proto == UINT8_MAX)
861                                 input_set |= I40E_INSET_IPV6_NEXT_HDR;
862                         if (ipv6_mask->hdr.hop_limits == UINT8_MAX)
863                                 input_set |= I40E_INSET_IPV6_HOP_LIMIT;
864
865                         /* Get filter info */
866                         filter->input.flow.ipv6_flow.tc =
867                                 (uint8_t)(ipv6_spec->hdr.vtc_flow <<
868                                           I40E_IPV4_TC_SHIFT);
869                         filter->input.flow.ipv6_flow.proto =
870                                 ipv6_spec->hdr.proto;
871                         filter->input.flow.ipv6_flow.hop_limits =
872                                 ipv6_spec->hdr.hop_limits;
873
874                         rte_memcpy(filter->input.flow.ipv6_flow.src_ip,
875                                    ipv6_spec->hdr.src_addr, 16);
876                         rte_memcpy(filter->input.flow.ipv6_flow.dst_ip,
877                                    ipv6_spec->hdr.dst_addr, 16);
878
879                         /* Check if it is fragment. */
880                         if (ipv6_spec->hdr.proto == I40E_IPV6_FRAG_HEADER)
881                                 flow_type = RTE_ETH_FLOW_FRAG_IPV6;
882                         else
883                                 flow_type = RTE_ETH_FLOW_NONFRAG_IPV6_OTHER;
884                         break;
885                 case RTE_FLOW_ITEM_TYPE_TCP:
886                         tcp_spec = (const struct rte_flow_item_tcp *)item->spec;
887                         tcp_mask = (const struct rte_flow_item_tcp *)item->mask;
888                         if (!tcp_spec || !tcp_mask) {
889                                 rte_flow_error_set(error, EINVAL,
890                                                    RTE_FLOW_ERROR_TYPE_ITEM,
891                                                    item,
892                                                    "NULL TCP spec/mask");
893                                 return -rte_errno;
894                         }
895
896                         /* Check TCP mask and update input set */
897                         if (tcp_mask->hdr.sent_seq ||
898                             tcp_mask->hdr.recv_ack ||
899                             tcp_mask->hdr.data_off ||
900                             tcp_mask->hdr.tcp_flags ||
901                             tcp_mask->hdr.rx_win ||
902                             tcp_mask->hdr.cksum ||
903                             tcp_mask->hdr.tcp_urp) {
904                                 rte_flow_error_set(error, EINVAL,
905                                                    RTE_FLOW_ERROR_TYPE_ITEM,
906                                                    item,
907                                                    "Invalid TCP mask");
908                                 return -rte_errno;
909                         }
910
911                         if (tcp_mask->hdr.src_port != UINT16_MAX ||
912                             tcp_mask->hdr.dst_port != UINT16_MAX) {
913                                 rte_flow_error_set(error, EINVAL,
914                                                    RTE_FLOW_ERROR_TYPE_ITEM,
915                                                    item,
916                                                    "Invalid TCP mask");
917                                 return -rte_errno;
918                         }
919
920                         input_set |= I40E_INSET_SRC_PORT;
921                         input_set |= I40E_INSET_DST_PORT;
922
923                         /* Get filter info */
924                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4)
925                                 flow_type = RTE_ETH_FLOW_NONFRAG_IPV4_TCP;
926                         else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6)
927                                 flow_type = RTE_ETH_FLOW_NONFRAG_IPV6_TCP;
928
929                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4) {
930                                 filter->input.flow.tcp4_flow.src_port =
931                                         tcp_spec->hdr.src_port;
932                                 filter->input.flow.tcp4_flow.dst_port =
933                                         tcp_spec->hdr.dst_port;
934                         } else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6) {
935                                 filter->input.flow.tcp6_flow.src_port =
936                                         tcp_spec->hdr.src_port;
937                                 filter->input.flow.tcp6_flow.dst_port =
938                                         tcp_spec->hdr.dst_port;
939                         }
940                         break;
941                 case RTE_FLOW_ITEM_TYPE_UDP:
942                         udp_spec = (const struct rte_flow_item_udp *)item->spec;
943                         udp_mask = (const struct rte_flow_item_udp *)item->mask;
944                         if (!udp_spec || !udp_mask) {
945                                 rte_flow_error_set(error, EINVAL,
946                                                    RTE_FLOW_ERROR_TYPE_ITEM,
947                                                    item,
948                                                    "NULL UDP spec/mask");
949                                 return -rte_errno;
950                         }
951
952                         /* Check UDP mask and update input set*/
953                         if (udp_mask->hdr.dgram_len ||
954                             udp_mask->hdr.dgram_cksum) {
955                                 rte_flow_error_set(error, EINVAL,
956                                                    RTE_FLOW_ERROR_TYPE_ITEM,
957                                                    item,
958                                                    "Invalid UDP mask");
959                                 return -rte_errno;
960                         }
961
962                         if (udp_mask->hdr.src_port != UINT16_MAX ||
963                             udp_mask->hdr.dst_port != UINT16_MAX) {
964                                 rte_flow_error_set(error, EINVAL,
965                                                    RTE_FLOW_ERROR_TYPE_ITEM,
966                                                    item,
967                                                    "Invalid UDP mask");
968                                 return -rte_errno;
969                         }
970
971                         input_set |= I40E_INSET_SRC_PORT;
972                         input_set |= I40E_INSET_DST_PORT;
973
974                         /* Get filter info */
975                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4)
976                                 flow_type =
977                                         RTE_ETH_FLOW_NONFRAG_IPV4_UDP;
978                         else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6)
979                                 flow_type =
980                                         RTE_ETH_FLOW_NONFRAG_IPV6_UDP;
981
982                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4) {
983                                 filter->input.flow.udp4_flow.src_port =
984                                         udp_spec->hdr.src_port;
985                                 filter->input.flow.udp4_flow.dst_port =
986                                         udp_spec->hdr.dst_port;
987                         } else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6) {
988                                 filter->input.flow.udp6_flow.src_port =
989                                         udp_spec->hdr.src_port;
990                                 filter->input.flow.udp6_flow.dst_port =
991                                         udp_spec->hdr.dst_port;
992                         }
993                         break;
994                 case RTE_FLOW_ITEM_TYPE_SCTP:
995                         sctp_spec =
996                                 (const struct rte_flow_item_sctp *)item->spec;
997                         sctp_mask =
998                                 (const struct rte_flow_item_sctp *)item->mask;
999                         if (!sctp_spec || !sctp_mask) {
1000                                 rte_flow_error_set(error, EINVAL,
1001                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1002                                                    item,
1003                                                    "NULL SCTP spec/mask");
1004                                 return -rte_errno;
1005                         }
1006
1007                         /* Check SCTP mask and update input set */
1008                         if (sctp_mask->hdr.cksum) {
1009                                 rte_flow_error_set(error, EINVAL,
1010                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1011                                                    item,
1012                                                    "Invalid UDP mask");
1013                                 return -rte_errno;
1014                         }
1015
1016                         if (sctp_mask->hdr.src_port != UINT16_MAX ||
1017                             sctp_mask->hdr.dst_port != UINT16_MAX ||
1018                             sctp_mask->hdr.tag != UINT32_MAX) {
1019                                 rte_flow_error_set(error, EINVAL,
1020                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1021                                                    item,
1022                                                    "Invalid UDP mask");
1023                                 return -rte_errno;
1024                         }
1025                         input_set |= I40E_INSET_SRC_PORT;
1026                         input_set |= I40E_INSET_DST_PORT;
1027                         input_set |= I40E_INSET_SCTP_VT;
1028
1029                         /* Get filter info */
1030                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4)
1031                                 flow_type = RTE_ETH_FLOW_NONFRAG_IPV4_SCTP;
1032                         else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6)
1033                                 flow_type = RTE_ETH_FLOW_NONFRAG_IPV6_SCTP;
1034
1035                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4) {
1036                                 filter->input.flow.sctp4_flow.src_port =
1037                                         sctp_spec->hdr.src_port;
1038                                 filter->input.flow.sctp4_flow.dst_port =
1039                                         sctp_spec->hdr.dst_port;
1040                                 filter->input.flow.sctp4_flow.verify_tag =
1041                                         sctp_spec->hdr.tag;
1042                         } else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6) {
1043                                 filter->input.flow.sctp6_flow.src_port =
1044                                         sctp_spec->hdr.src_port;
1045                                 filter->input.flow.sctp6_flow.dst_port =
1046                                         sctp_spec->hdr.dst_port;
1047                                 filter->input.flow.sctp6_flow.verify_tag =
1048                                         sctp_spec->hdr.tag;
1049                         }
1050                         break;
1051                 case RTE_FLOW_ITEM_TYPE_VF:
1052                         vf_spec = (const struct rte_flow_item_vf *)item->spec;
1053                         filter->input.flow_ext.is_vf = 1;
1054                         filter->input.flow_ext.dst_id = vf_spec->id;
1055                         if (filter->input.flow_ext.is_vf &&
1056                             filter->input.flow_ext.dst_id >= pf->vf_num) {
1057                                 rte_flow_error_set(error, EINVAL,
1058                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1059                                                    item,
1060                                                    "Invalid VF ID for FDIR.");
1061                                 return -rte_errno;
1062                         }
1063                         break;
1064                 default:
1065                         break;
1066                 }
1067         }
1068
1069         pctype = i40e_flowtype_to_pctype(flow_type);
1070         if (pctype == 0 || pctype > I40E_FILTER_PCTYPE_L2_PAYLOAD) {
1071                 rte_flow_error_set(error, EINVAL,
1072                                    RTE_FLOW_ERROR_TYPE_ITEM, item,
1073                                    "Unsupported flow type");
1074                 return -rte_errno;
1075         }
1076
1077         if (input_set != i40e_get_default_input_set(pctype)) {
1078                 rte_flow_error_set(error, EINVAL,
1079                                    RTE_FLOW_ERROR_TYPE_ITEM, item,
1080                                    "Invalid input set.");
1081                 return -rte_errno;
1082         }
1083         filter->input.flow_type = flow_type;
1084
1085         return 0;
1086 }
1087
1088 /* Parse to get the action info of a FDIR filter.
1089  * FDIR action supports QUEUE or (QUEUE + MARK).
1090  */
1091 static int
1092 i40e_flow_parse_fdir_action(struct rte_eth_dev *dev,
1093                             const struct rte_flow_action *actions,
1094                             struct rte_flow_error *error,
1095                             struct rte_eth_fdir_filter *filter)
1096 {
1097         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1098         const struct rte_flow_action *act;
1099         const struct rte_flow_action_queue *act_q;
1100         const struct rte_flow_action_mark *mark_spec;
1101         uint32_t index = 0;
1102
1103         /* Check if the first non-void action is QUEUE or DROP. */
1104         NEXT_ITEM_OF_ACTION(act, actions, index);
1105         if (act->type != RTE_FLOW_ACTION_TYPE_QUEUE &&
1106             act->type != RTE_FLOW_ACTION_TYPE_DROP) {
1107                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
1108                                    act, "Invalid action.");
1109                 return -rte_errno;
1110         }
1111
1112         act_q = (const struct rte_flow_action_queue *)act->conf;
1113         filter->action.flex_off = 0;
1114         if (act->type == RTE_FLOW_ACTION_TYPE_QUEUE)
1115                 filter->action.behavior = RTE_ETH_FDIR_ACCEPT;
1116         else
1117                 filter->action.behavior = RTE_ETH_FDIR_REJECT;
1118
1119         filter->action.report_status = RTE_ETH_FDIR_REPORT_ID;
1120         filter->action.rx_queue = act_q->index;
1121
1122         if (filter->action.rx_queue >= pf->dev_data->nb_rx_queues) {
1123                 rte_flow_error_set(error, EINVAL,
1124                                    RTE_FLOW_ERROR_TYPE_ACTION, act,
1125                                    "Invalid queue ID for FDIR.");
1126                 return -rte_errno;
1127         }
1128
1129         /* Check if the next non-void item is MARK or END. */
1130         index++;
1131         NEXT_ITEM_OF_ACTION(act, actions, index);
1132         if (act->type != RTE_FLOW_ACTION_TYPE_MARK &&
1133             act->type != RTE_FLOW_ACTION_TYPE_END) {
1134                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
1135                                    act, "Invalid action.");
1136                 return -rte_errno;
1137         }
1138
1139         if (act->type == RTE_FLOW_ACTION_TYPE_MARK) {
1140                 mark_spec = (const struct rte_flow_action_mark *)act->conf;
1141                 filter->soft_id = mark_spec->id;
1142
1143                 /* Check if the next non-void item is END */
1144                 index++;
1145                 NEXT_ITEM_OF_ACTION(act, actions, index);
1146                 if (act->type != RTE_FLOW_ACTION_TYPE_END) {
1147                         rte_flow_error_set(error, EINVAL,
1148                                            RTE_FLOW_ERROR_TYPE_ACTION,
1149                                            act, "Invalid action.");
1150                         return -rte_errno;
1151                 }
1152         }
1153
1154         return 0;
1155 }
1156
1157 static int
1158 i40e_flow_parse_fdir_filter(struct rte_eth_dev *dev,
1159                             const struct rte_flow_attr *attr,
1160                             const struct rte_flow_item pattern[],
1161                             const struct rte_flow_action actions[],
1162                             struct rte_flow_error *error,
1163                             union i40e_filter_t *filter)
1164 {
1165         struct rte_eth_fdir_filter *fdir_filter =
1166                 &filter->fdir_filter;
1167         int ret;
1168
1169         ret = i40e_flow_parse_fdir_pattern(dev, pattern, error, fdir_filter);
1170         if (ret)
1171                 return ret;
1172
1173         ret = i40e_flow_parse_fdir_action(dev, actions, error, fdir_filter);
1174         if (ret)
1175                 return ret;
1176
1177         ret = i40e_flow_parse_attr(attr, error);
1178         if (ret)
1179                 return ret;
1180
1181         cons_filter_type = RTE_ETH_FILTER_FDIR;
1182
1183         if (dev->data->dev_conf.fdir_conf.mode !=
1184             RTE_FDIR_MODE_PERFECT) {
1185                 rte_flow_error_set(error, ENOTSUP,
1186                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1187                                    NULL,
1188                                    "Check the mode in fdir_conf.");
1189                 return -rte_errno;
1190         }
1191
1192         return 0;
1193 }
1194
1195 /* Parse to get the action info of a tunnel filter
1196  * Tunnel action only supports PF, VF and QUEUE.
1197  */
1198 static int
1199 i40e_flow_parse_tunnel_action(struct rte_eth_dev *dev,
1200                               const struct rte_flow_action *actions,
1201                               struct rte_flow_error *error,
1202                               struct i40e_tunnel_filter_conf *filter)
1203 {
1204         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1205         const struct rte_flow_action *act;
1206         const struct rte_flow_action_queue *act_q;
1207         const struct rte_flow_action_vf *act_vf;
1208         uint32_t index = 0;
1209
1210         /* Check if the first non-void action is PF or VF. */
1211         NEXT_ITEM_OF_ACTION(act, actions, index);
1212         if (act->type != RTE_FLOW_ACTION_TYPE_PF &&
1213             act->type != RTE_FLOW_ACTION_TYPE_VF) {
1214                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
1215                                    act, "Not supported action.");
1216                 return -rte_errno;
1217         }
1218
1219         if (act->type == RTE_FLOW_ACTION_TYPE_VF) {
1220                 act_vf = (const struct rte_flow_action_vf *)act->conf;
1221                 filter->vf_id = act_vf->id;
1222                 filter->is_to_vf = 1;
1223                 if (filter->vf_id >= pf->vf_num) {
1224                         rte_flow_error_set(error, EINVAL,
1225                                    RTE_FLOW_ERROR_TYPE_ACTION,
1226                                    act, "Invalid VF ID for tunnel filter");
1227                         return -rte_errno;
1228                 }
1229         }
1230
1231         /* Check if the next non-void item is QUEUE */
1232         index++;
1233         NEXT_ITEM_OF_ACTION(act, actions, index);
1234         if (act->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
1235                 act_q = (const struct rte_flow_action_queue *)act->conf;
1236                 filter->queue_id = act_q->index;
1237                 if ((!filter->is_to_vf) &&
1238                     (filter->queue_id >= pf->dev_data->nb_rx_queues)) {
1239                         rte_flow_error_set(error, EINVAL,
1240                                    RTE_FLOW_ERROR_TYPE_ACTION,
1241                                    act, "Invalid queue ID for tunnel filter");
1242                         return -rte_errno;
1243                 } else if (filter->is_to_vf &&
1244                            (filter->queue_id >= pf->vf_nb_qps)) {
1245                         rte_flow_error_set(error, EINVAL,
1246                                    RTE_FLOW_ERROR_TYPE_ACTION,
1247                                    act, "Invalid queue ID for tunnel filter");
1248                         return -rte_errno;
1249                 }
1250         }
1251
1252         /* Check if the next non-void item is END */
1253         index++;
1254         NEXT_ITEM_OF_ACTION(act, actions, index);
1255         if (act->type != RTE_FLOW_ACTION_TYPE_END) {
1256                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
1257                                    act, "Not supported action.");
1258                 return -rte_errno;
1259         }
1260
1261         return 0;
1262 }
1263
1264 static int
1265 i40e_check_tenant_id_mask(const uint8_t *mask)
1266 {
1267         uint32_t j;
1268         int is_masked = 0;
1269
1270         for (j = 0; j < I40E_TENANT_ARRAY_NUM; j++) {
1271                 if (*(mask + j) == UINT8_MAX) {
1272                         if (j > 0 && (*(mask + j) != *(mask + j - 1)))
1273                                 return -EINVAL;
1274                         is_masked = 0;
1275                 } else if (*(mask + j) == 0) {
1276                         if (j > 0 && (*(mask + j) != *(mask + j - 1)))
1277                                 return -EINVAL;
1278                         is_masked = 1;
1279                 } else {
1280                         return -EINVAL;
1281                 }
1282         }
1283
1284         return is_masked;
1285 }
1286
1287 /* 1. Last in item should be NULL as range is not supported.
1288  * 2. Supported filter types: IMAC_IVLAN_TENID, IMAC_IVLAN,
1289  *    IMAC_TENID, OMAC_TENID_IMAC and IMAC.
1290  * 3. Mask of fields which need to be matched should be
1291  *    filled with 1.
1292  * 4. Mask of fields which needn't to be matched should be
1293  *    filled with 0.
1294  */
1295 static int
1296 i40e_flow_parse_vxlan_pattern(__rte_unused struct rte_eth_dev *dev,
1297                               const struct rte_flow_item *pattern,
1298                               struct rte_flow_error *error,
1299                               struct i40e_tunnel_filter_conf *filter)
1300 {
1301         const struct rte_flow_item *item = pattern;
1302         const struct rte_flow_item_eth *eth_spec;
1303         const struct rte_flow_item_eth *eth_mask;
1304         const struct rte_flow_item_eth *o_eth_spec = NULL;
1305         const struct rte_flow_item_eth *o_eth_mask = NULL;
1306         const struct rte_flow_item_vxlan *vxlan_spec = NULL;
1307         const struct rte_flow_item_vxlan *vxlan_mask = NULL;
1308         const struct rte_flow_item_eth *i_eth_spec = NULL;
1309         const struct rte_flow_item_eth *i_eth_mask = NULL;
1310         const struct rte_flow_item_vlan *vlan_spec = NULL;
1311         const struct rte_flow_item_vlan *vlan_mask = NULL;
1312         bool is_vni_masked = 0;
1313         enum rte_flow_item_type item_type;
1314         bool vxlan_flag = 0;
1315         uint32_t tenant_id_be = 0;
1316
1317         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1318                 if (item->last) {
1319                         rte_flow_error_set(error, EINVAL,
1320                                            RTE_FLOW_ERROR_TYPE_ITEM,
1321                                            item,
1322                                            "Not support range");
1323                         return -rte_errno;
1324                 }
1325                 item_type = item->type;
1326                 switch (item_type) {
1327                 case RTE_FLOW_ITEM_TYPE_ETH:
1328                         eth_spec = (const struct rte_flow_item_eth *)item->spec;
1329                         eth_mask = (const struct rte_flow_item_eth *)item->mask;
1330                         if ((!eth_spec && eth_mask) ||
1331                             (eth_spec && !eth_mask)) {
1332                                 rte_flow_error_set(error, EINVAL,
1333                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1334                                                    item,
1335                                                    "Invalid ether spec/mask");
1336                                 return -rte_errno;
1337                         }
1338
1339                         if (eth_spec && eth_mask) {
1340                                 /* DST address of inner MAC shouldn't be masked.
1341                                  * SRC address of Inner MAC should be masked.
1342                                  */
1343                                 if (!is_broadcast_ether_addr(&eth_mask->dst) ||
1344                                     !is_zero_ether_addr(&eth_mask->src) ||
1345                                     eth_mask->type) {
1346                                         rte_flow_error_set(error, EINVAL,
1347                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1348                                                    item,
1349                                                    "Invalid ether spec/mask");
1350                                         return -rte_errno;
1351                                 }
1352
1353                                 if (!vxlan_flag)
1354                                         rte_memcpy(&filter->outer_mac,
1355                                                    &eth_spec->dst,
1356                                                    ETHER_ADDR_LEN);
1357                                 else
1358                                         rte_memcpy(&filter->inner_mac,
1359                                                    &eth_spec->dst,
1360                                                    ETHER_ADDR_LEN);
1361                         }
1362
1363                         if (!vxlan_flag) {
1364                                 o_eth_spec = eth_spec;
1365                                 o_eth_mask = eth_mask;
1366                         } else {
1367                                 i_eth_spec = eth_spec;
1368                                 i_eth_mask = eth_mask;
1369                         }
1370
1371                         break;
1372                 case RTE_FLOW_ITEM_TYPE_VLAN:
1373                         vlan_spec =
1374                                 (const struct rte_flow_item_vlan *)item->spec;
1375                         vlan_mask =
1376                                 (const struct rte_flow_item_vlan *)item->mask;
1377                         if (vxlan_flag) {
1378                                 vlan_spec =
1379                                 (const struct rte_flow_item_vlan *)item->spec;
1380                                 vlan_mask =
1381                                 (const struct rte_flow_item_vlan *)item->mask;
1382                                 if (!(vlan_spec && vlan_mask)) {
1383                                         rte_flow_error_set(error, EINVAL,
1384                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1385                                                    item,
1386                                                    "Invalid vlan item");
1387                                         return -rte_errno;
1388                                 }
1389                         } else {
1390                                 if (vlan_spec || vlan_mask)
1391                                         rte_flow_error_set(error, EINVAL,
1392                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1393                                                    item,
1394                                                    "Invalid vlan item");
1395                                 return -rte_errno;
1396                         }
1397                         break;
1398                 case RTE_FLOW_ITEM_TYPE_IPV4:
1399                         filter->ip_type = I40E_TUNNEL_IPTYPE_IPV4;
1400                         /* IPv4 is used to describe protocol,
1401                          * spec and mask should be NULL.
1402                          */
1403                         if (item->spec || item->mask) {
1404                                 rte_flow_error_set(error, EINVAL,
1405                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1406                                                    item,
1407                                                    "Invalid IPv4 item");
1408                                 return -rte_errno;
1409                         }
1410                         break;
1411                 case RTE_FLOW_ITEM_TYPE_IPV6:
1412                         filter->ip_type = I40E_TUNNEL_IPTYPE_IPV6;
1413                         /* IPv6 is used to describe protocol,
1414                          * spec and mask should be NULL.
1415                          */
1416                         if (item->spec || item->mask) {
1417                                 rte_flow_error_set(error, EINVAL,
1418                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1419                                                    item,
1420                                                    "Invalid IPv6 item");
1421                                 return -rte_errno;
1422                         }
1423                         break;
1424                 case RTE_FLOW_ITEM_TYPE_UDP:
1425                         /* UDP is used to describe protocol,
1426                          * spec and mask should be NULL.
1427                          */
1428                         if (item->spec || item->mask) {
1429                                 rte_flow_error_set(error, EINVAL,
1430                                            RTE_FLOW_ERROR_TYPE_ITEM,
1431                                            item,
1432                                            "Invalid UDP item");
1433                                 return -rte_errno;
1434                         }
1435                         break;
1436                 case RTE_FLOW_ITEM_TYPE_VXLAN:
1437                         vxlan_spec =
1438                                 (const struct rte_flow_item_vxlan *)item->spec;
1439                         vxlan_mask =
1440                                 (const struct rte_flow_item_vxlan *)item->mask;
1441                         /* Check if VXLAN item is used to describe protocol.
1442                          * If yes, both spec and mask should be NULL.
1443                          * If no, either spec or mask shouldn't be NULL.
1444                          */
1445                         if ((!vxlan_spec && vxlan_mask) ||
1446                             (vxlan_spec && !vxlan_mask)) {
1447                                 rte_flow_error_set(error, EINVAL,
1448                                            RTE_FLOW_ERROR_TYPE_ITEM,
1449                                            item,
1450                                            "Invalid VXLAN item");
1451                                 return -rte_errno;
1452                         }
1453
1454                         /* Check if VNI is masked. */
1455                         if (vxlan_mask) {
1456                                 is_vni_masked =
1457                                 i40e_check_tenant_id_mask(vxlan_mask->vni);
1458                                 if (is_vni_masked < 0) {
1459                                         rte_flow_error_set(error, EINVAL,
1460                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1461                                                    item,
1462                                                    "Invalid VNI mask");
1463                                         return -rte_errno;
1464                                 }
1465                         }
1466                         vxlan_flag = 1;
1467                         break;
1468                 default:
1469                         break;
1470                 }
1471         }
1472
1473         /* Check specification and mask to get the filter type */
1474         if (vlan_spec && vlan_mask &&
1475             (vlan_mask->tci == rte_cpu_to_be_16(I40E_TCI_MASK))) {
1476                 /* If there's inner vlan */
1477                 filter->inner_vlan = rte_be_to_cpu_16(vlan_spec->tci)
1478                         & I40E_TCI_MASK;
1479                 if (vxlan_spec && vxlan_mask && !is_vni_masked) {
1480                         /* If there's vxlan */
1481                         rte_memcpy(((uint8_t *)&tenant_id_be + 1),
1482                                    vxlan_spec->vni, 3);
1483                         filter->tenant_id = rte_be_to_cpu_32(tenant_id_be);
1484                         if (!o_eth_spec && !o_eth_mask &&
1485                                 i_eth_spec && i_eth_mask)
1486                                 filter->filter_type =
1487                                         RTE_TUNNEL_FILTER_IMAC_IVLAN_TENID;
1488                         else {
1489                                 rte_flow_error_set(error, EINVAL,
1490                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1491                                                    NULL,
1492                                                    "Invalid filter type");
1493                                 return -rte_errno;
1494                         }
1495                 } else if (!vxlan_spec && !vxlan_mask) {
1496                         /* If there's no vxlan */
1497                         if (!o_eth_spec && !o_eth_mask &&
1498                                 i_eth_spec && i_eth_mask)
1499                                 filter->filter_type =
1500                                         RTE_TUNNEL_FILTER_IMAC_IVLAN;
1501                         else {
1502                                 rte_flow_error_set(error, EINVAL,
1503                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1504                                                    NULL,
1505                                                    "Invalid filter type");
1506                                 return -rte_errno;
1507                         }
1508                 } else {
1509                         rte_flow_error_set(error, EINVAL,
1510                                            RTE_FLOW_ERROR_TYPE_ITEM,
1511                                            NULL,
1512                                            "Invalid filter type");
1513                         return -rte_errno;
1514                 }
1515         } else if ((!vlan_spec && !vlan_mask) ||
1516                    (vlan_spec && vlan_mask && vlan_mask->tci == 0x0)) {
1517                 /* If there's no inner vlan */
1518                 if (vxlan_spec && vxlan_mask && !is_vni_masked) {
1519                         /* If there's vxlan */
1520                         rte_memcpy(((uint8_t *)&tenant_id_be + 1),
1521                                    vxlan_spec->vni, 3);
1522                         filter->tenant_id = rte_be_to_cpu_32(tenant_id_be);
1523                         if (!o_eth_spec && !o_eth_mask &&
1524                                 i_eth_spec && i_eth_mask)
1525                                 filter->filter_type =
1526                                         RTE_TUNNEL_FILTER_IMAC_TENID;
1527                         else if (o_eth_spec && o_eth_mask &&
1528                                 i_eth_spec && i_eth_mask)
1529                                 filter->filter_type =
1530                                         RTE_TUNNEL_FILTER_OMAC_TENID_IMAC;
1531                 } else if (!vxlan_spec && !vxlan_mask) {
1532                         /* If there's no vxlan */
1533                         if (!o_eth_spec && !o_eth_mask &&
1534                                 i_eth_spec && i_eth_mask) {
1535                                 filter->filter_type = ETH_TUNNEL_FILTER_IMAC;
1536                         } else {
1537                                 rte_flow_error_set(error, EINVAL,
1538                                            RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1539                                            "Invalid filter type");
1540                                 return -rte_errno;
1541                         }
1542                 } else {
1543                         rte_flow_error_set(error, EINVAL,
1544                                            RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1545                                            "Invalid filter type");
1546                         return -rte_errno;
1547                 }
1548         } else {
1549                 rte_flow_error_set(error, EINVAL,
1550                                    RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1551                                    "Not supported by tunnel filter.");
1552                 return -rte_errno;
1553         }
1554
1555         filter->tunnel_type = I40E_TUNNEL_TYPE_VXLAN;
1556
1557         return 0;
1558 }
1559
1560 static int
1561 i40e_flow_parse_vxlan_filter(struct rte_eth_dev *dev,
1562                              const struct rte_flow_attr *attr,
1563                              const struct rte_flow_item pattern[],
1564                              const struct rte_flow_action actions[],
1565                              struct rte_flow_error *error,
1566                              union i40e_filter_t *filter)
1567 {
1568         struct i40e_tunnel_filter_conf *tunnel_filter =
1569                 &filter->consistent_tunnel_filter;
1570         int ret;
1571
1572         ret = i40e_flow_parse_vxlan_pattern(dev, pattern,
1573                                             error, tunnel_filter);
1574         if (ret)
1575                 return ret;
1576
1577         ret = i40e_flow_parse_tunnel_action(dev, actions, error, tunnel_filter);
1578         if (ret)
1579                 return ret;
1580
1581         ret = i40e_flow_parse_attr(attr, error);
1582         if (ret)
1583                 return ret;
1584
1585         cons_filter_type = RTE_ETH_FILTER_TUNNEL;
1586
1587         return ret;
1588 }
1589
1590 /* 1. Last in item should be NULL as range is not supported.
1591  * 2. Supported filter types: MPLS label.
1592  * 3. Mask of fields which need to be matched should be
1593  *    filled with 1.
1594  * 4. Mask of fields which needn't to be matched should be
1595  *    filled with 0.
1596  */
1597 static int
1598 i40e_flow_parse_mpls_pattern(__rte_unused struct rte_eth_dev *dev,
1599                              const struct rte_flow_item *pattern,
1600                              struct rte_flow_error *error,
1601                              struct i40e_tunnel_filter_conf *filter)
1602 {
1603         const struct rte_flow_item *item = pattern;
1604         const struct rte_flow_item_mpls *mpls_spec;
1605         const struct rte_flow_item_mpls *mpls_mask;
1606         enum rte_flow_item_type item_type;
1607         bool is_mplsoudp = 0; /* 1 - MPLSoUDP, 0 - MPLSoGRE */
1608         const uint8_t label_mask[3] = {0xFF, 0xFF, 0xF0};
1609         uint32_t label_be = 0;
1610
1611         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1612                 if (item->last) {
1613                         rte_flow_error_set(error, EINVAL,
1614                                            RTE_FLOW_ERROR_TYPE_ITEM,
1615                                            item,
1616                                            "Not support range");
1617                         return -rte_errno;
1618                 }
1619                 item_type = item->type;
1620                 switch (item_type) {
1621                 case RTE_FLOW_ITEM_TYPE_ETH:
1622                         if (item->spec || item->mask) {
1623                                 rte_flow_error_set(error, EINVAL,
1624                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1625                                                    item,
1626                                                    "Invalid ETH item");
1627                                 return -rte_errno;
1628                         }
1629                         break;
1630                 case RTE_FLOW_ITEM_TYPE_IPV4:
1631                         filter->ip_type = I40E_TUNNEL_IPTYPE_IPV4;
1632                         /* IPv4 is used to describe protocol,
1633                          * spec and mask should be NULL.
1634                          */
1635                         if (item->spec || item->mask) {
1636                                 rte_flow_error_set(error, EINVAL,
1637                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1638                                                    item,
1639                                                    "Invalid IPv4 item");
1640                                 return -rte_errno;
1641                         }
1642                         break;
1643                 case RTE_FLOW_ITEM_TYPE_IPV6:
1644                         filter->ip_type = I40E_TUNNEL_IPTYPE_IPV6;
1645                         /* IPv6 is used to describe protocol,
1646                          * spec and mask should be NULL.
1647                          */
1648                         if (item->spec || item->mask) {
1649                                 rte_flow_error_set(error, EINVAL,
1650                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1651                                                    item,
1652                                                    "Invalid IPv6 item");
1653                                 return -rte_errno;
1654                         }
1655                         break;
1656                 case RTE_FLOW_ITEM_TYPE_UDP:
1657                         /* UDP is used to describe protocol,
1658                          * spec and mask should be NULL.
1659                          */
1660                         if (item->spec || item->mask) {
1661                                 rte_flow_error_set(error, EINVAL,
1662                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1663                                                    item,
1664                                                    "Invalid UDP item");
1665                                 return -rte_errno;
1666                         }
1667                         is_mplsoudp = 1;
1668                         break;
1669                 case RTE_FLOW_ITEM_TYPE_GRE:
1670                         /* GRE is used to describe protocol,
1671                          * spec and mask should be NULL.
1672                          */
1673                         if (item->spec || item->mask) {
1674                                 rte_flow_error_set(error, EINVAL,
1675                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1676                                                    item,
1677                                                    "Invalid GRE item");
1678                                 return -rte_errno;
1679                         }
1680                         break;
1681                 case RTE_FLOW_ITEM_TYPE_MPLS:
1682                         mpls_spec =
1683                                 (const struct rte_flow_item_mpls *)item->spec;
1684                         mpls_mask =
1685                                 (const struct rte_flow_item_mpls *)item->mask;
1686
1687                         if (!mpls_spec || !mpls_mask) {
1688                                 rte_flow_error_set(error, EINVAL,
1689                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1690                                                    item,
1691                                                    "Invalid MPLS item");
1692                                 return -rte_errno;
1693                         }
1694
1695                         if (memcmp(mpls_mask->label_tc_s, label_mask, 3)) {
1696                                 rte_flow_error_set(error, EINVAL,
1697                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1698                                                    item,
1699                                                    "Invalid MPLS label mask");
1700                                 return -rte_errno;
1701                         }
1702                         rte_memcpy(((uint8_t *)&label_be + 1),
1703                                    mpls_spec->label_tc_s, 3);
1704                         filter->tenant_id = rte_be_to_cpu_32(label_be) >> 4;
1705                         break;
1706                 default:
1707                         break;
1708                 }
1709         }
1710
1711         if (is_mplsoudp)
1712                 filter->tunnel_type = I40E_TUNNEL_TYPE_MPLSoUDP;
1713         else
1714                 filter->tunnel_type = I40E_TUNNEL_TYPE_MPLSoGRE;
1715
1716         return 0;
1717 }
1718
1719 static int
1720 i40e_flow_parse_mpls_filter(struct rte_eth_dev *dev,
1721                             const struct rte_flow_attr *attr,
1722                             const struct rte_flow_item pattern[],
1723                             const struct rte_flow_action actions[],
1724                             struct rte_flow_error *error,
1725                             union i40e_filter_t *filter)
1726 {
1727         struct i40e_tunnel_filter_conf *tunnel_filter =
1728                 &filter->consistent_tunnel_filter;
1729         int ret;
1730
1731         ret = i40e_flow_parse_mpls_pattern(dev, pattern,
1732                                            error, tunnel_filter);
1733         if (ret)
1734                 return ret;
1735
1736         ret = i40e_flow_parse_tunnel_action(dev, actions, error, tunnel_filter);
1737         if (ret)
1738                 return ret;
1739
1740         ret = i40e_flow_parse_attr(attr, error);
1741         if (ret)
1742                 return ret;
1743
1744         cons_filter_type = RTE_ETH_FILTER_TUNNEL;
1745
1746         return ret;
1747 }
1748
1749 /* 1. Last in item should be NULL as range is not supported.
1750  * 2. Supported filter types: QINQ.
1751  * 3. Mask of fields which need to be matched should be
1752  *    filled with 1.
1753  * 4. Mask of fields which needn't to be matched should be
1754  *    filled with 0.
1755  */
1756 static int
1757 i40e_flow_parse_qinq_pattern(__rte_unused struct rte_eth_dev *dev,
1758                               const struct rte_flow_item *pattern,
1759                               struct rte_flow_error *error,
1760                               struct i40e_tunnel_filter_conf *filter)
1761 {
1762         const struct rte_flow_item *item = pattern;
1763         const struct rte_flow_item_vlan *vlan_spec = NULL;
1764         const struct rte_flow_item_vlan *vlan_mask = NULL;
1765         const struct rte_flow_item_vlan *i_vlan_spec = NULL;
1766         const struct rte_flow_item_vlan *i_vlan_mask = NULL;
1767         const struct rte_flow_item_vlan *o_vlan_spec = NULL;
1768         const struct rte_flow_item_vlan *o_vlan_mask = NULL;
1769
1770         enum rte_flow_item_type item_type;
1771         bool vlan_flag = 0;
1772
1773         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1774                 if (item->last) {
1775                         rte_flow_error_set(error, EINVAL,
1776                                            RTE_FLOW_ERROR_TYPE_ITEM,
1777                                            item,
1778                                            "Not support range");
1779                         return -rte_errno;
1780                 }
1781                 item_type = item->type;
1782                 switch (item_type) {
1783                 case RTE_FLOW_ITEM_TYPE_ETH:
1784                         if (item->spec || item->mask) {
1785                                 rte_flow_error_set(error, EINVAL,
1786                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1787                                                    item,
1788                                                    "Invalid ETH item");
1789                                 return -rte_errno;
1790                         }
1791                         break;
1792                 case RTE_FLOW_ITEM_TYPE_VLAN:
1793                         vlan_spec =
1794                                 (const struct rte_flow_item_vlan *)item->spec;
1795                         vlan_mask =
1796                                 (const struct rte_flow_item_vlan *)item->mask;
1797
1798                         if (!(vlan_spec && vlan_mask)) {
1799                                 rte_flow_error_set(error, EINVAL,
1800                                            RTE_FLOW_ERROR_TYPE_ITEM,
1801                                            item,
1802                                            "Invalid vlan item");
1803                                 return -rte_errno;
1804                         }
1805
1806                         if (!vlan_flag) {
1807                                 o_vlan_spec = vlan_spec;
1808                                 o_vlan_mask = vlan_mask;
1809                                 vlan_flag = 1;
1810                         } else {
1811                                 i_vlan_spec = vlan_spec;
1812                                 i_vlan_mask = vlan_mask;
1813                                 vlan_flag = 0;
1814                         }
1815                         break;
1816
1817                 default:
1818                         break;
1819                 }
1820         }
1821
1822         /* Get filter specification */
1823         if ((o_vlan_mask->tci == rte_cpu_to_be_16(I40E_TCI_MASK)) &&
1824             (i_vlan_mask->tci == rte_cpu_to_be_16(I40E_TCI_MASK))) {
1825                 filter->outer_vlan = rte_be_to_cpu_16(o_vlan_spec->tci)
1826                         & I40E_TCI_MASK;
1827                 filter->inner_vlan = rte_be_to_cpu_16(i_vlan_spec->tci)
1828                         & I40E_TCI_MASK;
1829         } else {
1830                         rte_flow_error_set(error, EINVAL,
1831                                            RTE_FLOW_ERROR_TYPE_ITEM,
1832                                            NULL,
1833                                            "Invalid filter type");
1834                         return -rte_errno;
1835         }
1836
1837         filter->tunnel_type = I40E_TUNNEL_TYPE_QINQ;
1838         return 0;
1839 }
1840
1841 static int
1842 i40e_flow_parse_qinq_filter(struct rte_eth_dev *dev,
1843                               const struct rte_flow_attr *attr,
1844                               const struct rte_flow_item pattern[],
1845                               const struct rte_flow_action actions[],
1846                               struct rte_flow_error *error,
1847                               union i40e_filter_t *filter)
1848 {
1849         struct i40e_tunnel_filter_conf *tunnel_filter =
1850                 &filter->consistent_tunnel_filter;
1851         int ret;
1852
1853         ret = i40e_flow_parse_qinq_pattern(dev, pattern,
1854                                              error, tunnel_filter);
1855         if (ret)
1856                 return ret;
1857
1858         ret = i40e_flow_parse_tunnel_action(dev, actions, error, tunnel_filter);
1859         if (ret)
1860                 return ret;
1861
1862         ret = i40e_flow_parse_attr(attr, error);
1863         if (ret)
1864                 return ret;
1865
1866         cons_filter_type = RTE_ETH_FILTER_TUNNEL;
1867
1868         return ret;
1869 }
1870
1871 static int
1872 i40e_flow_validate(struct rte_eth_dev *dev,
1873                    const struct rte_flow_attr *attr,
1874                    const struct rte_flow_item pattern[],
1875                    const struct rte_flow_action actions[],
1876                    struct rte_flow_error *error)
1877 {
1878         struct rte_flow_item *items; /* internal pattern w/o VOID items */
1879         parse_filter_t parse_filter;
1880         uint32_t item_num = 0; /* non-void item number of pattern*/
1881         uint32_t i = 0;
1882         int ret;
1883
1884         if (!pattern) {
1885                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1886                                    NULL, "NULL pattern.");
1887                 return -rte_errno;
1888         }
1889
1890         if (!actions) {
1891                 rte_flow_error_set(error, EINVAL,
1892                                    RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1893                                    NULL, "NULL action.");
1894                 return -rte_errno;
1895         }
1896
1897         if (!attr) {
1898                 rte_flow_error_set(error, EINVAL,
1899                                    RTE_FLOW_ERROR_TYPE_ATTR,
1900                                    NULL, "NULL attribute.");
1901                 return -rte_errno;
1902         }
1903
1904         memset(&cons_filter, 0, sizeof(cons_filter));
1905
1906         /* Get the non-void item number of pattern */
1907         while ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_END) {
1908                 if ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_VOID)
1909                         item_num++;
1910                 i++;
1911         }
1912         item_num++;
1913
1914         items = rte_zmalloc("i40e_pattern",
1915                             item_num * sizeof(struct rte_flow_item), 0);
1916         if (!items) {
1917                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1918                                    NULL, "No memory for PMD internal items.");
1919                 return -ENOMEM;
1920         }
1921
1922         i40e_pattern_skip_void_item(items, pattern);
1923
1924         /* Find if there's matched parse filter function */
1925         parse_filter = i40e_find_parse_filter_func(items);
1926         if (!parse_filter) {
1927                 rte_flow_error_set(error, EINVAL,
1928                                    RTE_FLOW_ERROR_TYPE_ITEM,
1929                                    pattern, "Unsupported pattern");
1930                 return -rte_errno;
1931         }
1932
1933         ret = parse_filter(dev, attr, items, actions, error, &cons_filter);
1934
1935         rte_free(items);
1936
1937         return ret;
1938 }
1939
1940 static struct rte_flow *
1941 i40e_flow_create(struct rte_eth_dev *dev,
1942                  const struct rte_flow_attr *attr,
1943                  const struct rte_flow_item pattern[],
1944                  const struct rte_flow_action actions[],
1945                  struct rte_flow_error *error)
1946 {
1947         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1948         struct rte_flow *flow;
1949         int ret;
1950
1951         flow = rte_zmalloc("i40e_flow", sizeof(struct rte_flow), 0);
1952         if (!flow) {
1953                 rte_flow_error_set(error, ENOMEM,
1954                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1955                                    "Failed to allocate memory");
1956                 return flow;
1957         }
1958
1959         ret = i40e_flow_validate(dev, attr, pattern, actions, error);
1960         if (ret < 0)
1961                 return NULL;
1962
1963         switch (cons_filter_type) {
1964         case RTE_ETH_FILTER_ETHERTYPE:
1965                 ret = i40e_ethertype_filter_set(pf,
1966                                         &cons_filter.ethertype_filter, 1);
1967                 if (ret)
1968                         goto free_flow;
1969                 flow->rule = TAILQ_LAST(&pf->ethertype.ethertype_list,
1970                                         i40e_ethertype_filter_list);
1971                 break;
1972         case RTE_ETH_FILTER_FDIR:
1973                 ret = i40e_add_del_fdir_filter(dev,
1974                                        &cons_filter.fdir_filter, 1);
1975                 if (ret)
1976                         goto free_flow;
1977                 flow->rule = TAILQ_LAST(&pf->fdir.fdir_list,
1978                                         i40e_fdir_filter_list);
1979                 break;
1980         case RTE_ETH_FILTER_TUNNEL:
1981                 ret = i40e_dev_consistent_tunnel_filter_set(pf,
1982                             &cons_filter.consistent_tunnel_filter, 1);
1983                 if (ret)
1984                         goto free_flow;
1985                 flow->rule = TAILQ_LAST(&pf->tunnel.tunnel_list,
1986                                         i40e_tunnel_filter_list);
1987                 break;
1988         default:
1989                 goto free_flow;
1990         }
1991
1992         flow->filter_type = cons_filter_type;
1993         TAILQ_INSERT_TAIL(&pf->flow_list, flow, node);
1994         return flow;
1995
1996 free_flow:
1997         rte_flow_error_set(error, -ret,
1998                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1999                            "Failed to create flow.");
2000         rte_free(flow);
2001         return NULL;
2002 }
2003
2004 static int
2005 i40e_flow_destroy(struct rte_eth_dev *dev,
2006                   struct rte_flow *flow,
2007                   struct rte_flow_error *error)
2008 {
2009         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2010         enum rte_filter_type filter_type = flow->filter_type;
2011         int ret = 0;
2012
2013         switch (filter_type) {
2014         case RTE_ETH_FILTER_ETHERTYPE:
2015                 ret = i40e_flow_destroy_ethertype_filter(pf,
2016                          (struct i40e_ethertype_filter *)flow->rule);
2017                 break;
2018         case RTE_ETH_FILTER_TUNNEL:
2019                 ret = i40e_flow_destroy_tunnel_filter(pf,
2020                               (struct i40e_tunnel_filter *)flow->rule);
2021                 break;
2022         case RTE_ETH_FILTER_FDIR:
2023                 ret = i40e_add_del_fdir_filter(dev,
2024                        &((struct i40e_fdir_filter *)flow->rule)->fdir, 0);
2025                 break;
2026         default:
2027                 PMD_DRV_LOG(WARNING, "Filter type (%d) not supported",
2028                             filter_type);
2029                 ret = -EINVAL;
2030                 break;
2031         }
2032
2033         if (!ret) {
2034                 TAILQ_REMOVE(&pf->flow_list, flow, node);
2035                 rte_free(flow);
2036         } else
2037                 rte_flow_error_set(error, -ret,
2038                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2039                                    "Failed to destroy flow.");
2040
2041         return ret;
2042 }
2043
2044 static int
2045 i40e_flow_destroy_ethertype_filter(struct i40e_pf *pf,
2046                                    struct i40e_ethertype_filter *filter)
2047 {
2048         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
2049         struct i40e_ethertype_rule *ethertype_rule = &pf->ethertype;
2050         struct i40e_ethertype_filter *node;
2051         struct i40e_control_filter_stats stats;
2052         uint16_t flags = 0;
2053         int ret = 0;
2054
2055         if (!(filter->flags & RTE_ETHTYPE_FLAGS_MAC))
2056                 flags |= I40E_AQC_ADD_CONTROL_PACKET_FLAGS_IGNORE_MAC;
2057         if (filter->flags & RTE_ETHTYPE_FLAGS_DROP)
2058                 flags |= I40E_AQC_ADD_CONTROL_PACKET_FLAGS_DROP;
2059         flags |= I40E_AQC_ADD_CONTROL_PACKET_FLAGS_TO_QUEUE;
2060
2061         memset(&stats, 0, sizeof(stats));
2062         ret = i40e_aq_add_rem_control_packet_filter(hw,
2063                                     filter->input.mac_addr.addr_bytes,
2064                                     filter->input.ether_type,
2065                                     flags, pf->main_vsi->seid,
2066                                     filter->queue, 0, &stats, NULL);
2067         if (ret < 0)
2068                 return ret;
2069
2070         node = i40e_sw_ethertype_filter_lookup(ethertype_rule, &filter->input);
2071         if (!node)
2072                 return -EINVAL;
2073
2074         ret = i40e_sw_ethertype_filter_del(pf, &node->input);
2075
2076         return ret;
2077 }
2078
2079 static int
2080 i40e_flow_destroy_tunnel_filter(struct i40e_pf *pf,
2081                                 struct i40e_tunnel_filter *filter)
2082 {
2083         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
2084         struct i40e_vsi *vsi;
2085         struct i40e_pf_vf *vf;
2086         struct i40e_aqc_add_rm_cloud_filt_elem_ext cld_filter;
2087         struct i40e_tunnel_rule *tunnel_rule = &pf->tunnel;
2088         struct i40e_tunnel_filter *node;
2089         bool big_buffer = 0;
2090         int ret = 0;
2091
2092         memset(&cld_filter, 0, sizeof(cld_filter));
2093         ether_addr_copy((struct ether_addr *)&filter->input.outer_mac,
2094                         (struct ether_addr *)&cld_filter.element.outer_mac);
2095         ether_addr_copy((struct ether_addr *)&filter->input.inner_mac,
2096                         (struct ether_addr *)&cld_filter.element.inner_mac);
2097         cld_filter.element.inner_vlan = filter->input.inner_vlan;
2098         cld_filter.element.flags = filter->input.flags;
2099         cld_filter.element.tenant_id = filter->input.tenant_id;
2100         cld_filter.element.queue_number = filter->queue;
2101         rte_memcpy(cld_filter.general_fields,
2102                    filter->input.general_fields,
2103                    sizeof(cld_filter.general_fields));
2104
2105         if (!filter->is_to_vf)
2106                 vsi = pf->main_vsi;
2107         else {
2108                 vf = &pf->vfs[filter->vf_id];
2109                 vsi = vf->vsi;
2110         }
2111
2112         if (((filter->input.flags & I40E_AQC_ADD_CLOUD_FILTER_TEID_MPLSoUDP) ==
2113             I40E_AQC_ADD_CLOUD_FILTER_TEID_MPLSoUDP) ||
2114             ((filter->input.flags & I40E_AQC_ADD_CLOUD_FILTER_TEID_MPLSoGRE) ==
2115             I40E_AQC_ADD_CLOUD_FILTER_TEID_MPLSoGRE) ||
2116             ((filter->input.flags & I40E_AQC_ADD_CLOUD_FILTER_CUSTOM_QINQ) ==
2117             I40E_AQC_ADD_CLOUD_FILTER_CUSTOM_QINQ))
2118                 big_buffer = 1;
2119
2120         if (big_buffer)
2121                 ret = i40e_aq_remove_cloud_filters_big_buffer(hw, vsi->seid,
2122                                                               &cld_filter, 1);
2123         else
2124                 ret = i40e_aq_remove_cloud_filters(hw, vsi->seid,
2125                                                    &cld_filter.element, 1);
2126         if (ret < 0)
2127                 return -ENOTSUP;
2128
2129         node = i40e_sw_tunnel_filter_lookup(tunnel_rule, &filter->input);
2130         if (!node)
2131                 return -EINVAL;
2132
2133         ret = i40e_sw_tunnel_filter_del(pf, &node->input);
2134
2135         return ret;
2136 }
2137
2138 static int
2139 i40e_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
2140 {
2141         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2142         int ret;
2143
2144         ret = i40e_flow_flush_fdir_filter(pf);
2145         if (ret) {
2146                 rte_flow_error_set(error, -ret,
2147                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2148                                    "Failed to flush FDIR flows.");
2149                 return -rte_errno;
2150         }
2151
2152         ret = i40e_flow_flush_ethertype_filter(pf);
2153         if (ret) {
2154                 rte_flow_error_set(error, -ret,
2155                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2156                                    "Failed to ethertype flush flows.");
2157                 return -rte_errno;
2158         }
2159
2160         ret = i40e_flow_flush_tunnel_filter(pf);
2161         if (ret) {
2162                 rte_flow_error_set(error, -ret,
2163                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2164                                    "Failed to flush tunnel flows.");
2165                 return -rte_errno;
2166         }
2167
2168         return ret;
2169 }
2170
2171 static int
2172 i40e_flow_flush_fdir_filter(struct i40e_pf *pf)
2173 {
2174         struct rte_eth_dev *dev = pf->adapter->eth_dev;
2175         struct i40e_fdir_info *fdir_info = &pf->fdir;
2176         struct i40e_fdir_filter *fdir_filter;
2177         struct rte_flow *flow;
2178         void *temp;
2179         int ret;
2180
2181         ret = i40e_fdir_flush(dev);
2182         if (!ret) {
2183                 /* Delete FDIR filters in FDIR list. */
2184                 while ((fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list))) {
2185                         ret = i40e_sw_fdir_filter_del(pf,
2186                                                       &fdir_filter->fdir.input);
2187                         if (ret < 0)
2188                                 return ret;
2189                 }
2190
2191                 /* Delete FDIR flows in flow list. */
2192                 TAILQ_FOREACH_SAFE(flow, &pf->flow_list, node, temp) {
2193                         if (flow->filter_type == RTE_ETH_FILTER_FDIR) {
2194                                 TAILQ_REMOVE(&pf->flow_list, flow, node);
2195                                 rte_free(flow);
2196                         }
2197                 }
2198         }
2199
2200         return ret;
2201 }
2202
2203 /* Flush all ethertype filters */
2204 static int
2205 i40e_flow_flush_ethertype_filter(struct i40e_pf *pf)
2206 {
2207         struct i40e_ethertype_filter_list
2208                 *ethertype_list = &pf->ethertype.ethertype_list;
2209         struct i40e_ethertype_filter *filter;
2210         struct rte_flow *flow;
2211         void *temp;
2212         int ret = 0;
2213
2214         while ((filter = TAILQ_FIRST(ethertype_list))) {
2215                 ret = i40e_flow_destroy_ethertype_filter(pf, filter);
2216                 if (ret)
2217                         return ret;
2218         }
2219
2220         /* Delete ethertype flows in flow list. */
2221         TAILQ_FOREACH_SAFE(flow, &pf->flow_list, node, temp) {
2222                 if (flow->filter_type == RTE_ETH_FILTER_ETHERTYPE) {
2223                         TAILQ_REMOVE(&pf->flow_list, flow, node);
2224                         rte_free(flow);
2225                 }
2226         }
2227
2228         return ret;
2229 }
2230
2231 /* Flush all tunnel filters */
2232 static int
2233 i40e_flow_flush_tunnel_filter(struct i40e_pf *pf)
2234 {
2235         struct i40e_tunnel_filter_list
2236                 *tunnel_list = &pf->tunnel.tunnel_list;
2237         struct i40e_tunnel_filter *filter;
2238         struct rte_flow *flow;
2239         void *temp;
2240         int ret = 0;
2241
2242         while ((filter = TAILQ_FIRST(tunnel_list))) {
2243                 ret = i40e_flow_destroy_tunnel_filter(pf, filter);
2244                 if (ret)
2245                         return ret;
2246         }
2247
2248         /* Delete tunnel flows in flow list. */
2249         TAILQ_FOREACH_SAFE(flow, &pf->flow_list, node, temp) {
2250                 if (flow->filter_type == RTE_ETH_FILTER_TUNNEL) {
2251                         TAILQ_REMOVE(&pf->flow_list, flow, node);
2252                         rte_free(flow);
2253                 }
2254         }
2255
2256         return ret;
2257 }