net/i40e: update actions for FDIR
[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(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 == ETHER_TYPE_LLDP ||
612                             filter->ether_type == outer_tpid) {
613                                 rte_flow_error_set(error, EINVAL,
614                                                    RTE_FLOW_ERROR_TYPE_ITEM,
615                                                    item,
616                                                    "Unsupported ether_type in"
617                                                    " control packet filter.");
618                                 return -rte_errno;
619                         }
620                         break;
621                 default:
622                         break;
623                 }
624         }
625
626         return 0;
627 }
628
629 /* Ethertype action only supports QUEUE or DROP. */
630 static int
631 i40e_flow_parse_ethertype_action(struct rte_eth_dev *dev,
632                                  const struct rte_flow_action *actions,
633                                  struct rte_flow_error *error,
634                                  struct rte_eth_ethertype_filter *filter)
635 {
636         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
637         const struct rte_flow_action *act;
638         const struct rte_flow_action_queue *act_q;
639         uint32_t index = 0;
640
641         /* Check if the first non-void action is QUEUE or DROP. */
642         NEXT_ITEM_OF_ACTION(act, actions, index);
643         if (act->type != RTE_FLOW_ACTION_TYPE_QUEUE &&
644             act->type != RTE_FLOW_ACTION_TYPE_DROP) {
645                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
646                                    act, "Not supported action.");
647                 return -rte_errno;
648         }
649
650         if (act->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
651                 act_q = (const struct rte_flow_action_queue *)act->conf;
652                 filter->queue = act_q->index;
653                 if (filter->queue >= pf->dev_data->nb_rx_queues) {
654                         rte_flow_error_set(error, EINVAL,
655                                            RTE_FLOW_ERROR_TYPE_ACTION,
656                                            act, "Invalid queue ID for"
657                                            " ethertype_filter.");
658                         return -rte_errno;
659                 }
660         } else {
661                 filter->flags |= RTE_ETHTYPE_FLAGS_DROP;
662         }
663
664         /* Check if the next non-void item is END */
665         index++;
666         NEXT_ITEM_OF_ACTION(act, actions, index);
667         if (act->type != RTE_FLOW_ACTION_TYPE_END) {
668                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
669                                    act, "Not supported action.");
670                 return -rte_errno;
671         }
672
673         return 0;
674 }
675
676 static int
677 i40e_flow_parse_ethertype_filter(struct rte_eth_dev *dev,
678                                  const struct rte_flow_attr *attr,
679                                  const struct rte_flow_item pattern[],
680                                  const struct rte_flow_action actions[],
681                                  struct rte_flow_error *error,
682                                  union i40e_filter_t *filter)
683 {
684         struct rte_eth_ethertype_filter *ethertype_filter =
685                 &filter->ethertype_filter;
686         int ret;
687
688         ret = i40e_flow_parse_ethertype_pattern(dev, pattern, error,
689                                                 ethertype_filter);
690         if (ret)
691                 return ret;
692
693         ret = i40e_flow_parse_ethertype_action(dev, actions, error,
694                                                ethertype_filter);
695         if (ret)
696                 return ret;
697
698         ret = i40e_flow_parse_attr(attr, error);
699         if (ret)
700                 return ret;
701
702         cons_filter_type = RTE_ETH_FILTER_ETHERTYPE;
703
704         return ret;
705 }
706
707 /* 1. Last in item should be NULL as range is not supported.
708  * 2. Supported flow type and input set: refer to array
709  *    default_inset_table in i40e_ethdev.c.
710  * 3. Mask of fields which need to be matched should be
711  *    filled with 1.
712  * 4. Mask of fields which needn't to be matched should be
713  *    filled with 0.
714  */
715 static int
716 i40e_flow_parse_fdir_pattern(struct rte_eth_dev *dev,
717                              const struct rte_flow_item *pattern,
718                              struct rte_flow_error *error,
719                              struct rte_eth_fdir_filter *filter)
720 {
721         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
722         const struct rte_flow_item *item = pattern;
723         const struct rte_flow_item_eth *eth_spec, *eth_mask;
724         const struct rte_flow_item_ipv4 *ipv4_spec, *ipv4_mask;
725         const struct rte_flow_item_ipv6 *ipv6_spec, *ipv6_mask;
726         const struct rte_flow_item_tcp *tcp_spec, *tcp_mask;
727         const struct rte_flow_item_udp *udp_spec, *udp_mask;
728         const struct rte_flow_item_sctp *sctp_spec, *sctp_mask;
729         const struct rte_flow_item_vf *vf_spec;
730         uint32_t flow_type = RTE_ETH_FLOW_UNKNOWN;
731         enum i40e_filter_pctype pctype;
732         uint64_t input_set = I40E_INSET_NONE;
733         uint16_t flag_offset;
734         enum rte_flow_item_type item_type;
735         enum rte_flow_item_type l3 = RTE_FLOW_ITEM_TYPE_END;
736         uint32_t j;
737
738         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
739                 if (item->last) {
740                         rte_flow_error_set(error, EINVAL,
741                                            RTE_FLOW_ERROR_TYPE_ITEM,
742                                            item,
743                                            "Not support range");
744                         return -rte_errno;
745                 }
746                 item_type = item->type;
747                 switch (item_type) {
748                 case RTE_FLOW_ITEM_TYPE_ETH:
749                         eth_spec = (const struct rte_flow_item_eth *)item->spec;
750                         eth_mask = (const struct rte_flow_item_eth *)item->mask;
751                         if (eth_spec || eth_mask) {
752                                 rte_flow_error_set(error, EINVAL,
753                                                    RTE_FLOW_ERROR_TYPE_ITEM,
754                                                    item,
755                                                    "Invalid ETH spec/mask");
756                                 return -rte_errno;
757                         }
758                         break;
759                 case RTE_FLOW_ITEM_TYPE_IPV4:
760                         l3 = RTE_FLOW_ITEM_TYPE_IPV4;
761                         ipv4_spec =
762                                 (const struct rte_flow_item_ipv4 *)item->spec;
763                         ipv4_mask =
764                                 (const struct rte_flow_item_ipv4 *)item->mask;
765                         if (!ipv4_spec || !ipv4_mask) {
766                                 rte_flow_error_set(error, EINVAL,
767                                                    RTE_FLOW_ERROR_TYPE_ITEM,
768                                                    item,
769                                                    "NULL IPv4 spec/mask");
770                                 return -rte_errno;
771                         }
772
773                         /* Check IPv4 mask and update input set */
774                         if (ipv4_mask->hdr.version_ihl ||
775                             ipv4_mask->hdr.total_length ||
776                             ipv4_mask->hdr.packet_id ||
777                             ipv4_mask->hdr.fragment_offset ||
778                             ipv4_mask->hdr.hdr_checksum) {
779                                 rte_flow_error_set(error, EINVAL,
780                                                    RTE_FLOW_ERROR_TYPE_ITEM,
781                                                    item,
782                                                    "Invalid IPv4 mask.");
783                                 return -rte_errno;
784                         }
785
786                         if (ipv4_mask->hdr.src_addr == UINT32_MAX)
787                                 input_set |= I40E_INSET_IPV4_SRC;
788                         if (ipv4_mask->hdr.dst_addr == UINT32_MAX)
789                                 input_set |= I40E_INSET_IPV4_DST;
790                         if (ipv4_mask->hdr.type_of_service == UINT8_MAX)
791                                 input_set |= I40E_INSET_IPV4_TOS;
792                         if (ipv4_mask->hdr.time_to_live == UINT8_MAX)
793                                 input_set |= I40E_INSET_IPV4_TTL;
794                         if (ipv4_mask->hdr.next_proto_id == UINT8_MAX)
795                                 input_set |= I40E_INSET_IPV4_PROTO;
796
797                         /* Get filter info */
798                         flow_type = RTE_ETH_FLOW_NONFRAG_IPV4_OTHER;
799                         /* Check if it is fragment. */
800                         flag_offset =
801                               rte_be_to_cpu_16(ipv4_spec->hdr.fragment_offset);
802                         if (flag_offset & IPV4_HDR_OFFSET_MASK ||
803                             flag_offset & IPV4_HDR_MF_FLAG)
804                                 flow_type = RTE_ETH_FLOW_FRAG_IPV4;
805
806                         /* Get the filter info */
807                         filter->input.flow.ip4_flow.proto =
808                                 ipv4_spec->hdr.next_proto_id;
809                         filter->input.flow.ip4_flow.tos =
810                                 ipv4_spec->hdr.type_of_service;
811                         filter->input.flow.ip4_flow.ttl =
812                                 ipv4_spec->hdr.time_to_live;
813                         filter->input.flow.ip4_flow.src_ip =
814                                 ipv4_spec->hdr.src_addr;
815                         filter->input.flow.ip4_flow.dst_ip =
816                                 ipv4_spec->hdr.dst_addr;
817
818                         break;
819                 case RTE_FLOW_ITEM_TYPE_IPV6:
820                         l3 = RTE_FLOW_ITEM_TYPE_IPV6;
821                         ipv6_spec =
822                                 (const struct rte_flow_item_ipv6 *)item->spec;
823                         ipv6_mask =
824                                 (const struct rte_flow_item_ipv6 *)item->mask;
825                         if (!ipv6_spec || !ipv6_mask) {
826                                 rte_flow_error_set(error, EINVAL,
827                                                    RTE_FLOW_ERROR_TYPE_ITEM,
828                                                    item,
829                                                    "NULL IPv6 spec/mask");
830                                 return -rte_errno;
831                         }
832
833                         /* Check IPv6 mask and update input set */
834                         if (ipv6_mask->hdr.payload_len) {
835                                 rte_flow_error_set(error, EINVAL,
836                                                    RTE_FLOW_ERROR_TYPE_ITEM,
837                                                    item,
838                                                    "Invalid IPv6 mask");
839                                 return -rte_errno;
840                         }
841
842                         /* SCR and DST address of IPv6 shouldn't be masked */
843                         for (j = 0; j < RTE_DIM(ipv6_mask->hdr.src_addr); j++) {
844                                 if (ipv6_mask->hdr.src_addr[j] != UINT8_MAX ||
845                                     ipv6_mask->hdr.dst_addr[j] != UINT8_MAX) {
846                                         rte_flow_error_set(error, EINVAL,
847                                                    RTE_FLOW_ERROR_TYPE_ITEM,
848                                                    item,
849                                                    "Invalid IPv6 mask");
850                                         return -rte_errno;
851                                 }
852                         }
853
854                         input_set |= I40E_INSET_IPV6_SRC;
855                         input_set |= I40E_INSET_IPV6_DST;
856
857                         if ((ipv6_mask->hdr.vtc_flow &
858                              rte_cpu_to_be_16(I40E_IPV6_TC_MASK))
859                             == rte_cpu_to_be_16(I40E_IPV6_TC_MASK))
860                                 input_set |= I40E_INSET_IPV6_TC;
861                         if (ipv6_mask->hdr.proto == UINT8_MAX)
862                                 input_set |= I40E_INSET_IPV6_NEXT_HDR;
863                         if (ipv6_mask->hdr.hop_limits == UINT8_MAX)
864                                 input_set |= I40E_INSET_IPV6_HOP_LIMIT;
865
866                         /* Get filter info */
867                         filter->input.flow.ipv6_flow.tc =
868                                 (uint8_t)(ipv6_spec->hdr.vtc_flow <<
869                                           I40E_IPV4_TC_SHIFT);
870                         filter->input.flow.ipv6_flow.proto =
871                                 ipv6_spec->hdr.proto;
872                         filter->input.flow.ipv6_flow.hop_limits =
873                                 ipv6_spec->hdr.hop_limits;
874
875                         rte_memcpy(filter->input.flow.ipv6_flow.src_ip,
876                                    ipv6_spec->hdr.src_addr, 16);
877                         rte_memcpy(filter->input.flow.ipv6_flow.dst_ip,
878                                    ipv6_spec->hdr.dst_addr, 16);
879
880                         /* Check if it is fragment. */
881                         if (ipv6_spec->hdr.proto == I40E_IPV6_FRAG_HEADER)
882                                 flow_type = RTE_ETH_FLOW_FRAG_IPV6;
883                         else
884                                 flow_type = RTE_ETH_FLOW_NONFRAG_IPV6_OTHER;
885                         break;
886                 case RTE_FLOW_ITEM_TYPE_TCP:
887                         tcp_spec = (const struct rte_flow_item_tcp *)item->spec;
888                         tcp_mask = (const struct rte_flow_item_tcp *)item->mask;
889                         if (!tcp_spec || !tcp_mask) {
890                                 rte_flow_error_set(error, EINVAL,
891                                                    RTE_FLOW_ERROR_TYPE_ITEM,
892                                                    item,
893                                                    "NULL TCP spec/mask");
894                                 return -rte_errno;
895                         }
896
897                         /* Check TCP mask and update input set */
898                         if (tcp_mask->hdr.sent_seq ||
899                             tcp_mask->hdr.recv_ack ||
900                             tcp_mask->hdr.data_off ||
901                             tcp_mask->hdr.tcp_flags ||
902                             tcp_mask->hdr.rx_win ||
903                             tcp_mask->hdr.cksum ||
904                             tcp_mask->hdr.tcp_urp) {
905                                 rte_flow_error_set(error, EINVAL,
906                                                    RTE_FLOW_ERROR_TYPE_ITEM,
907                                                    item,
908                                                    "Invalid TCP mask");
909                                 return -rte_errno;
910                         }
911
912                         if (tcp_mask->hdr.src_port != UINT16_MAX ||
913                             tcp_mask->hdr.dst_port != UINT16_MAX) {
914                                 rte_flow_error_set(error, EINVAL,
915                                                    RTE_FLOW_ERROR_TYPE_ITEM,
916                                                    item,
917                                                    "Invalid TCP mask");
918                                 return -rte_errno;
919                         }
920
921                         input_set |= I40E_INSET_SRC_PORT;
922                         input_set |= I40E_INSET_DST_PORT;
923
924                         /* Get filter info */
925                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4)
926                                 flow_type = RTE_ETH_FLOW_NONFRAG_IPV4_TCP;
927                         else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6)
928                                 flow_type = RTE_ETH_FLOW_NONFRAG_IPV6_TCP;
929
930                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4) {
931                                 filter->input.flow.tcp4_flow.src_port =
932                                         tcp_spec->hdr.src_port;
933                                 filter->input.flow.tcp4_flow.dst_port =
934                                         tcp_spec->hdr.dst_port;
935                         } else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6) {
936                                 filter->input.flow.tcp6_flow.src_port =
937                                         tcp_spec->hdr.src_port;
938                                 filter->input.flow.tcp6_flow.dst_port =
939                                         tcp_spec->hdr.dst_port;
940                         }
941                         break;
942                 case RTE_FLOW_ITEM_TYPE_UDP:
943                         udp_spec = (const struct rte_flow_item_udp *)item->spec;
944                         udp_mask = (const struct rte_flow_item_udp *)item->mask;
945                         if (!udp_spec || !udp_mask) {
946                                 rte_flow_error_set(error, EINVAL,
947                                                    RTE_FLOW_ERROR_TYPE_ITEM,
948                                                    item,
949                                                    "NULL UDP spec/mask");
950                                 return -rte_errno;
951                         }
952
953                         /* Check UDP mask and update input set*/
954                         if (udp_mask->hdr.dgram_len ||
955                             udp_mask->hdr.dgram_cksum) {
956                                 rte_flow_error_set(error, EINVAL,
957                                                    RTE_FLOW_ERROR_TYPE_ITEM,
958                                                    item,
959                                                    "Invalid UDP mask");
960                                 return -rte_errno;
961                         }
962
963                         if (udp_mask->hdr.src_port != UINT16_MAX ||
964                             udp_mask->hdr.dst_port != UINT16_MAX) {
965                                 rte_flow_error_set(error, EINVAL,
966                                                    RTE_FLOW_ERROR_TYPE_ITEM,
967                                                    item,
968                                                    "Invalid UDP mask");
969                                 return -rte_errno;
970                         }
971
972                         input_set |= I40E_INSET_SRC_PORT;
973                         input_set |= I40E_INSET_DST_PORT;
974
975                         /* Get filter info */
976                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4)
977                                 flow_type =
978                                         RTE_ETH_FLOW_NONFRAG_IPV4_UDP;
979                         else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6)
980                                 flow_type =
981                                         RTE_ETH_FLOW_NONFRAG_IPV6_UDP;
982
983                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4) {
984                                 filter->input.flow.udp4_flow.src_port =
985                                         udp_spec->hdr.src_port;
986                                 filter->input.flow.udp4_flow.dst_port =
987                                         udp_spec->hdr.dst_port;
988                         } else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6) {
989                                 filter->input.flow.udp6_flow.src_port =
990                                         udp_spec->hdr.src_port;
991                                 filter->input.flow.udp6_flow.dst_port =
992                                         udp_spec->hdr.dst_port;
993                         }
994                         break;
995                 case RTE_FLOW_ITEM_TYPE_SCTP:
996                         sctp_spec =
997                                 (const struct rte_flow_item_sctp *)item->spec;
998                         sctp_mask =
999                                 (const struct rte_flow_item_sctp *)item->mask;
1000                         if (!sctp_spec || !sctp_mask) {
1001                                 rte_flow_error_set(error, EINVAL,
1002                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1003                                                    item,
1004                                                    "NULL SCTP spec/mask");
1005                                 return -rte_errno;
1006                         }
1007
1008                         /* Check SCTP mask and update input set */
1009                         if (sctp_mask->hdr.cksum) {
1010                                 rte_flow_error_set(error, EINVAL,
1011                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1012                                                    item,
1013                                                    "Invalid UDP mask");
1014                                 return -rte_errno;
1015                         }
1016
1017                         if (sctp_mask->hdr.src_port != UINT16_MAX ||
1018                             sctp_mask->hdr.dst_port != UINT16_MAX ||
1019                             sctp_mask->hdr.tag != UINT32_MAX) {
1020                                 rte_flow_error_set(error, EINVAL,
1021                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1022                                                    item,
1023                                                    "Invalid UDP mask");
1024                                 return -rte_errno;
1025                         }
1026                         input_set |= I40E_INSET_SRC_PORT;
1027                         input_set |= I40E_INSET_DST_PORT;
1028                         input_set |= I40E_INSET_SCTP_VT;
1029
1030                         /* Get filter info */
1031                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4)
1032                                 flow_type = RTE_ETH_FLOW_NONFRAG_IPV4_SCTP;
1033                         else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6)
1034                                 flow_type = RTE_ETH_FLOW_NONFRAG_IPV6_SCTP;
1035
1036                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4) {
1037                                 filter->input.flow.sctp4_flow.src_port =
1038                                         sctp_spec->hdr.src_port;
1039                                 filter->input.flow.sctp4_flow.dst_port =
1040                                         sctp_spec->hdr.dst_port;
1041                                 filter->input.flow.sctp4_flow.verify_tag =
1042                                         sctp_spec->hdr.tag;
1043                         } else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6) {
1044                                 filter->input.flow.sctp6_flow.src_port =
1045                                         sctp_spec->hdr.src_port;
1046                                 filter->input.flow.sctp6_flow.dst_port =
1047                                         sctp_spec->hdr.dst_port;
1048                                 filter->input.flow.sctp6_flow.verify_tag =
1049                                         sctp_spec->hdr.tag;
1050                         }
1051                         break;
1052                 case RTE_FLOW_ITEM_TYPE_VF:
1053                         vf_spec = (const struct rte_flow_item_vf *)item->spec;
1054                         filter->input.flow_ext.is_vf = 1;
1055                         filter->input.flow_ext.dst_id = vf_spec->id;
1056                         if (filter->input.flow_ext.is_vf &&
1057                             filter->input.flow_ext.dst_id >= pf->vf_num) {
1058                                 rte_flow_error_set(error, EINVAL,
1059                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1060                                                    item,
1061                                                    "Invalid VF ID for FDIR.");
1062                                 return -rte_errno;
1063                         }
1064                         break;
1065                 default:
1066                         break;
1067                 }
1068         }
1069
1070         pctype = i40e_flowtype_to_pctype(flow_type);
1071         if (pctype == 0 || pctype > I40E_FILTER_PCTYPE_L2_PAYLOAD) {
1072                 rte_flow_error_set(error, EINVAL,
1073                                    RTE_FLOW_ERROR_TYPE_ITEM, item,
1074                                    "Unsupported flow type");
1075                 return -rte_errno;
1076         }
1077
1078         if (input_set != i40e_get_default_input_set(pctype)) {
1079                 rte_flow_error_set(error, EINVAL,
1080                                    RTE_FLOW_ERROR_TYPE_ITEM, item,
1081                                    "Invalid input set.");
1082                 return -rte_errno;
1083         }
1084         filter->input.flow_type = flow_type;
1085
1086         return 0;
1087 }
1088
1089 /* Parse to get the action info of a FDIR filter.
1090  * FDIR action supports QUEUE or (QUEUE + MARK).
1091  */
1092 static int
1093 i40e_flow_parse_fdir_action(struct rte_eth_dev *dev,
1094                             const struct rte_flow_action *actions,
1095                             struct rte_flow_error *error,
1096                             struct rte_eth_fdir_filter *filter)
1097 {
1098         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1099         const struct rte_flow_action *act;
1100         const struct rte_flow_action_queue *act_q;
1101         const struct rte_flow_action_mark *mark_spec;
1102         uint32_t index = 0;
1103
1104         /* Check if the first non-void action is QUEUE or DROP or PASSTHRU. */
1105         NEXT_ITEM_OF_ACTION(act, actions, index);
1106         switch (act->type) {
1107         case RTE_FLOW_ACTION_TYPE_QUEUE:
1108                 act_q = (const struct rte_flow_action_queue *)act->conf;
1109                 filter->action.rx_queue = act_q->index;
1110                 if (filter->action.rx_queue >= pf->dev_data->nb_rx_queues) {
1111                         rte_flow_error_set(error, EINVAL,
1112                                            RTE_FLOW_ERROR_TYPE_ACTION, act,
1113                                            "Invalid queue ID for FDIR.");
1114                         return -rte_errno;
1115                 }
1116                 filter->action.behavior = RTE_ETH_FDIR_ACCEPT;
1117                 break;
1118         case RTE_FLOW_ACTION_TYPE_DROP:
1119                 filter->action.behavior = RTE_ETH_FDIR_REJECT;
1120                 break;
1121         case RTE_FLOW_ACTION_TYPE_PASSTHRU:
1122                 filter->action.behavior = RTE_ETH_FDIR_PASSTHRU;
1123                 break;
1124         default:
1125                 rte_flow_error_set(error, EINVAL,
1126                                    RTE_FLOW_ERROR_TYPE_ACTION, act,
1127                                    "Invalid action.");
1128                 return -rte_errno;
1129         }
1130
1131         /* Check if the next non-void item is MARK or FLAG or END. */
1132         index++;
1133         NEXT_ITEM_OF_ACTION(act, actions, index);
1134         switch (act->type) {
1135         case RTE_FLOW_ACTION_TYPE_MARK:
1136                 mark_spec = (const struct rte_flow_action_mark *)act->conf;
1137                 filter->action.report_status = RTE_ETH_FDIR_REPORT_ID;
1138                 filter->soft_id = mark_spec->id;
1139                 break;
1140         case RTE_FLOW_ACTION_TYPE_FLAG:
1141                 filter->action.report_status = RTE_ETH_FDIR_NO_REPORT_STATUS;
1142                 break;
1143         case RTE_FLOW_ACTION_TYPE_END:
1144                 return 0;
1145         default:
1146                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
1147                                    act, "Invalid action.");
1148                 return -rte_errno;
1149         }
1150
1151         /* Check if the next non-void item is END */
1152         index++;
1153         NEXT_ITEM_OF_ACTION(act, actions, index);
1154         if (act->type != RTE_FLOW_ACTION_TYPE_END) {
1155                 rte_flow_error_set(error, EINVAL,
1156                                    RTE_FLOW_ERROR_TYPE_ACTION,
1157                                    act, "Invalid action.");
1158                 return -rte_errno;
1159         }
1160
1161         return 0;
1162 }
1163
1164 static int
1165 i40e_flow_parse_fdir_filter(struct rte_eth_dev *dev,
1166                             const struct rte_flow_attr *attr,
1167                             const struct rte_flow_item pattern[],
1168                             const struct rte_flow_action actions[],
1169                             struct rte_flow_error *error,
1170                             union i40e_filter_t *filter)
1171 {
1172         struct rte_eth_fdir_filter *fdir_filter =
1173                 &filter->fdir_filter;
1174         int ret;
1175
1176         ret = i40e_flow_parse_fdir_pattern(dev, pattern, error, fdir_filter);
1177         if (ret)
1178                 return ret;
1179
1180         ret = i40e_flow_parse_fdir_action(dev, actions, error, fdir_filter);
1181         if (ret)
1182                 return ret;
1183
1184         ret = i40e_flow_parse_attr(attr, error);
1185         if (ret)
1186                 return ret;
1187
1188         cons_filter_type = RTE_ETH_FILTER_FDIR;
1189
1190         if (dev->data->dev_conf.fdir_conf.mode !=
1191             RTE_FDIR_MODE_PERFECT) {
1192                 rte_flow_error_set(error, ENOTSUP,
1193                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1194                                    NULL,
1195                                    "Check the mode in fdir_conf.");
1196                 return -rte_errno;
1197         }
1198
1199         return 0;
1200 }
1201
1202 /* Parse to get the action info of a tunnel filter
1203  * Tunnel action only supports PF, VF and QUEUE.
1204  */
1205 static int
1206 i40e_flow_parse_tunnel_action(struct rte_eth_dev *dev,
1207                               const struct rte_flow_action *actions,
1208                               struct rte_flow_error *error,
1209                               struct i40e_tunnel_filter_conf *filter)
1210 {
1211         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1212         const struct rte_flow_action *act;
1213         const struct rte_flow_action_queue *act_q;
1214         const struct rte_flow_action_vf *act_vf;
1215         uint32_t index = 0;
1216
1217         /* Check if the first non-void action is PF or VF. */
1218         NEXT_ITEM_OF_ACTION(act, actions, index);
1219         if (act->type != RTE_FLOW_ACTION_TYPE_PF &&
1220             act->type != RTE_FLOW_ACTION_TYPE_VF) {
1221                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
1222                                    act, "Not supported action.");
1223                 return -rte_errno;
1224         }
1225
1226         if (act->type == RTE_FLOW_ACTION_TYPE_VF) {
1227                 act_vf = (const struct rte_flow_action_vf *)act->conf;
1228                 filter->vf_id = act_vf->id;
1229                 filter->is_to_vf = 1;
1230                 if (filter->vf_id >= pf->vf_num) {
1231                         rte_flow_error_set(error, EINVAL,
1232                                    RTE_FLOW_ERROR_TYPE_ACTION,
1233                                    act, "Invalid VF ID for tunnel filter");
1234                         return -rte_errno;
1235                 }
1236         }
1237
1238         /* Check if the next non-void item is QUEUE */
1239         index++;
1240         NEXT_ITEM_OF_ACTION(act, actions, index);
1241         if (act->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
1242                 act_q = (const struct rte_flow_action_queue *)act->conf;
1243                 filter->queue_id = act_q->index;
1244                 if ((!filter->is_to_vf) &&
1245                     (filter->queue_id >= pf->dev_data->nb_rx_queues)) {
1246                         rte_flow_error_set(error, EINVAL,
1247                                    RTE_FLOW_ERROR_TYPE_ACTION,
1248                                    act, "Invalid queue ID for tunnel filter");
1249                         return -rte_errno;
1250                 } else if (filter->is_to_vf &&
1251                            (filter->queue_id >= pf->vf_nb_qps)) {
1252                         rte_flow_error_set(error, EINVAL,
1253                                    RTE_FLOW_ERROR_TYPE_ACTION,
1254                                    act, "Invalid queue ID for tunnel filter");
1255                         return -rte_errno;
1256                 }
1257         }
1258
1259         /* Check if the next non-void item is END */
1260         index++;
1261         NEXT_ITEM_OF_ACTION(act, actions, index);
1262         if (act->type != RTE_FLOW_ACTION_TYPE_END) {
1263                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
1264                                    act, "Not supported action.");
1265                 return -rte_errno;
1266         }
1267
1268         return 0;
1269 }
1270
1271 static int
1272 i40e_check_tenant_id_mask(const uint8_t *mask)
1273 {
1274         uint32_t j;
1275         int is_masked = 0;
1276
1277         for (j = 0; j < I40E_TENANT_ARRAY_NUM; j++) {
1278                 if (*(mask + j) == UINT8_MAX) {
1279                         if (j > 0 && (*(mask + j) != *(mask + j - 1)))
1280                                 return -EINVAL;
1281                         is_masked = 0;
1282                 } else if (*(mask + j) == 0) {
1283                         if (j > 0 && (*(mask + j) != *(mask + j - 1)))
1284                                 return -EINVAL;
1285                         is_masked = 1;
1286                 } else {
1287                         return -EINVAL;
1288                 }
1289         }
1290
1291         return is_masked;
1292 }
1293
1294 /* 1. Last in item should be NULL as range is not supported.
1295  * 2. Supported filter types: IMAC_IVLAN_TENID, IMAC_IVLAN,
1296  *    IMAC_TENID, OMAC_TENID_IMAC and IMAC.
1297  * 3. Mask of fields which need to be matched should be
1298  *    filled with 1.
1299  * 4. Mask of fields which needn't to be matched should be
1300  *    filled with 0.
1301  */
1302 static int
1303 i40e_flow_parse_vxlan_pattern(__rte_unused struct rte_eth_dev *dev,
1304                               const struct rte_flow_item *pattern,
1305                               struct rte_flow_error *error,
1306                               struct i40e_tunnel_filter_conf *filter)
1307 {
1308         const struct rte_flow_item *item = pattern;
1309         const struct rte_flow_item_eth *eth_spec;
1310         const struct rte_flow_item_eth *eth_mask;
1311         const struct rte_flow_item_eth *o_eth_spec = NULL;
1312         const struct rte_flow_item_eth *o_eth_mask = NULL;
1313         const struct rte_flow_item_vxlan *vxlan_spec = NULL;
1314         const struct rte_flow_item_vxlan *vxlan_mask = NULL;
1315         const struct rte_flow_item_eth *i_eth_spec = NULL;
1316         const struct rte_flow_item_eth *i_eth_mask = NULL;
1317         const struct rte_flow_item_vlan *vlan_spec = NULL;
1318         const struct rte_flow_item_vlan *vlan_mask = NULL;
1319         bool is_vni_masked = 0;
1320         enum rte_flow_item_type item_type;
1321         bool vxlan_flag = 0;
1322         uint32_t tenant_id_be = 0;
1323
1324         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1325                 if (item->last) {
1326                         rte_flow_error_set(error, EINVAL,
1327                                            RTE_FLOW_ERROR_TYPE_ITEM,
1328                                            item,
1329                                            "Not support range");
1330                         return -rte_errno;
1331                 }
1332                 item_type = item->type;
1333                 switch (item_type) {
1334                 case RTE_FLOW_ITEM_TYPE_ETH:
1335                         eth_spec = (const struct rte_flow_item_eth *)item->spec;
1336                         eth_mask = (const struct rte_flow_item_eth *)item->mask;
1337                         if ((!eth_spec && eth_mask) ||
1338                             (eth_spec && !eth_mask)) {
1339                                 rte_flow_error_set(error, EINVAL,
1340                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1341                                                    item,
1342                                                    "Invalid ether spec/mask");
1343                                 return -rte_errno;
1344                         }
1345
1346                         if (eth_spec && eth_mask) {
1347                                 /* DST address of inner MAC shouldn't be masked.
1348                                  * SRC address of Inner MAC should be masked.
1349                                  */
1350                                 if (!is_broadcast_ether_addr(&eth_mask->dst) ||
1351                                     !is_zero_ether_addr(&eth_mask->src) ||
1352                                     eth_mask->type) {
1353                                         rte_flow_error_set(error, EINVAL,
1354                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1355                                                    item,
1356                                                    "Invalid ether spec/mask");
1357                                         return -rte_errno;
1358                                 }
1359
1360                                 if (!vxlan_flag)
1361                                         rte_memcpy(&filter->outer_mac,
1362                                                    &eth_spec->dst,
1363                                                    ETHER_ADDR_LEN);
1364                                 else
1365                                         rte_memcpy(&filter->inner_mac,
1366                                                    &eth_spec->dst,
1367                                                    ETHER_ADDR_LEN);
1368                         }
1369
1370                         if (!vxlan_flag) {
1371                                 o_eth_spec = eth_spec;
1372                                 o_eth_mask = eth_mask;
1373                         } else {
1374                                 i_eth_spec = eth_spec;
1375                                 i_eth_mask = eth_mask;
1376                         }
1377
1378                         break;
1379                 case RTE_FLOW_ITEM_TYPE_VLAN:
1380                         vlan_spec =
1381                                 (const struct rte_flow_item_vlan *)item->spec;
1382                         vlan_mask =
1383                                 (const struct rte_flow_item_vlan *)item->mask;
1384                         if (vxlan_flag) {
1385                                 vlan_spec =
1386                                 (const struct rte_flow_item_vlan *)item->spec;
1387                                 vlan_mask =
1388                                 (const struct rte_flow_item_vlan *)item->mask;
1389                                 if (!(vlan_spec && vlan_mask)) {
1390                                         rte_flow_error_set(error, EINVAL,
1391                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1392                                                    item,
1393                                                    "Invalid vlan item");
1394                                         return -rte_errno;
1395                                 }
1396                         } else {
1397                                 if (vlan_spec || vlan_mask)
1398                                         rte_flow_error_set(error, EINVAL,
1399                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1400                                                    item,
1401                                                    "Invalid vlan item");
1402                                 return -rte_errno;
1403                         }
1404                         break;
1405                 case RTE_FLOW_ITEM_TYPE_IPV4:
1406                         filter->ip_type = I40E_TUNNEL_IPTYPE_IPV4;
1407                         /* IPv4 is used to describe protocol,
1408                          * spec and mask should be NULL.
1409                          */
1410                         if (item->spec || item->mask) {
1411                                 rte_flow_error_set(error, EINVAL,
1412                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1413                                                    item,
1414                                                    "Invalid IPv4 item");
1415                                 return -rte_errno;
1416                         }
1417                         break;
1418                 case RTE_FLOW_ITEM_TYPE_IPV6:
1419                         filter->ip_type = I40E_TUNNEL_IPTYPE_IPV6;
1420                         /* IPv6 is used to describe protocol,
1421                          * spec and mask should be NULL.
1422                          */
1423                         if (item->spec || item->mask) {
1424                                 rte_flow_error_set(error, EINVAL,
1425                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1426                                                    item,
1427                                                    "Invalid IPv6 item");
1428                                 return -rte_errno;
1429                         }
1430                         break;
1431                 case RTE_FLOW_ITEM_TYPE_UDP:
1432                         /* UDP is used to describe protocol,
1433                          * spec and mask should be NULL.
1434                          */
1435                         if (item->spec || item->mask) {
1436                                 rte_flow_error_set(error, EINVAL,
1437                                            RTE_FLOW_ERROR_TYPE_ITEM,
1438                                            item,
1439                                            "Invalid UDP item");
1440                                 return -rte_errno;
1441                         }
1442                         break;
1443                 case RTE_FLOW_ITEM_TYPE_VXLAN:
1444                         vxlan_spec =
1445                                 (const struct rte_flow_item_vxlan *)item->spec;
1446                         vxlan_mask =
1447                                 (const struct rte_flow_item_vxlan *)item->mask;
1448                         /* Check if VXLAN item is used to describe protocol.
1449                          * If yes, both spec and mask should be NULL.
1450                          * If no, either spec or mask shouldn't be NULL.
1451                          */
1452                         if ((!vxlan_spec && vxlan_mask) ||
1453                             (vxlan_spec && !vxlan_mask)) {
1454                                 rte_flow_error_set(error, EINVAL,
1455                                            RTE_FLOW_ERROR_TYPE_ITEM,
1456                                            item,
1457                                            "Invalid VXLAN item");
1458                                 return -rte_errno;
1459                         }
1460
1461                         /* Check if VNI is masked. */
1462                         if (vxlan_mask) {
1463                                 is_vni_masked =
1464                                 i40e_check_tenant_id_mask(vxlan_mask->vni);
1465                                 if (is_vni_masked < 0) {
1466                                         rte_flow_error_set(error, EINVAL,
1467                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1468                                                    item,
1469                                                    "Invalid VNI mask");
1470                                         return -rte_errno;
1471                                 }
1472                         }
1473                         vxlan_flag = 1;
1474                         break;
1475                 default:
1476                         break;
1477                 }
1478         }
1479
1480         /* Check specification and mask to get the filter type */
1481         if (vlan_spec && vlan_mask &&
1482             (vlan_mask->tci == rte_cpu_to_be_16(I40E_TCI_MASK))) {
1483                 /* If there's inner vlan */
1484                 filter->inner_vlan = rte_be_to_cpu_16(vlan_spec->tci)
1485                         & I40E_TCI_MASK;
1486                 if (vxlan_spec && vxlan_mask && !is_vni_masked) {
1487                         /* If there's vxlan */
1488                         rte_memcpy(((uint8_t *)&tenant_id_be + 1),
1489                                    vxlan_spec->vni, 3);
1490                         filter->tenant_id = rte_be_to_cpu_32(tenant_id_be);
1491                         if (!o_eth_spec && !o_eth_mask &&
1492                                 i_eth_spec && i_eth_mask)
1493                                 filter->filter_type =
1494                                         RTE_TUNNEL_FILTER_IMAC_IVLAN_TENID;
1495                         else {
1496                                 rte_flow_error_set(error, EINVAL,
1497                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1498                                                    NULL,
1499                                                    "Invalid filter type");
1500                                 return -rte_errno;
1501                         }
1502                 } else if (!vxlan_spec && !vxlan_mask) {
1503                         /* If there's no vxlan */
1504                         if (!o_eth_spec && !o_eth_mask &&
1505                                 i_eth_spec && i_eth_mask)
1506                                 filter->filter_type =
1507                                         RTE_TUNNEL_FILTER_IMAC_IVLAN;
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 {
1516                         rte_flow_error_set(error, EINVAL,
1517                                            RTE_FLOW_ERROR_TYPE_ITEM,
1518                                            NULL,
1519                                            "Invalid filter type");
1520                         return -rte_errno;
1521                 }
1522         } else if ((!vlan_spec && !vlan_mask) ||
1523                    (vlan_spec && vlan_mask && vlan_mask->tci == 0x0)) {
1524                 /* If there's no inner vlan */
1525                 if (vxlan_spec && vxlan_mask && !is_vni_masked) {
1526                         /* If there's vxlan */
1527                         rte_memcpy(((uint8_t *)&tenant_id_be + 1),
1528                                    vxlan_spec->vni, 3);
1529                         filter->tenant_id = rte_be_to_cpu_32(tenant_id_be);
1530                         if (!o_eth_spec && !o_eth_mask &&
1531                                 i_eth_spec && i_eth_mask)
1532                                 filter->filter_type =
1533                                         RTE_TUNNEL_FILTER_IMAC_TENID;
1534                         else if (o_eth_spec && o_eth_mask &&
1535                                 i_eth_spec && i_eth_mask)
1536                                 filter->filter_type =
1537                                         RTE_TUNNEL_FILTER_OMAC_TENID_IMAC;
1538                 } else if (!vxlan_spec && !vxlan_mask) {
1539                         /* If there's no vxlan */
1540                         if (!o_eth_spec && !o_eth_mask &&
1541                                 i_eth_spec && i_eth_mask) {
1542                                 filter->filter_type = ETH_TUNNEL_FILTER_IMAC;
1543                         } else {
1544                                 rte_flow_error_set(error, EINVAL,
1545                                            RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1546                                            "Invalid filter type");
1547                                 return -rte_errno;
1548                         }
1549                 } else {
1550                         rte_flow_error_set(error, EINVAL,
1551                                            RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1552                                            "Invalid filter type");
1553                         return -rte_errno;
1554                 }
1555         } else {
1556                 rte_flow_error_set(error, EINVAL,
1557                                    RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1558                                    "Not supported by tunnel filter.");
1559                 return -rte_errno;
1560         }
1561
1562         filter->tunnel_type = I40E_TUNNEL_TYPE_VXLAN;
1563
1564         return 0;
1565 }
1566
1567 static int
1568 i40e_flow_parse_vxlan_filter(struct rte_eth_dev *dev,
1569                              const struct rte_flow_attr *attr,
1570                              const struct rte_flow_item pattern[],
1571                              const struct rte_flow_action actions[],
1572                              struct rte_flow_error *error,
1573                              union i40e_filter_t *filter)
1574 {
1575         struct i40e_tunnel_filter_conf *tunnel_filter =
1576                 &filter->consistent_tunnel_filter;
1577         int ret;
1578
1579         ret = i40e_flow_parse_vxlan_pattern(dev, pattern,
1580                                             error, tunnel_filter);
1581         if (ret)
1582                 return ret;
1583
1584         ret = i40e_flow_parse_tunnel_action(dev, actions, error, tunnel_filter);
1585         if (ret)
1586                 return ret;
1587
1588         ret = i40e_flow_parse_attr(attr, error);
1589         if (ret)
1590                 return ret;
1591
1592         cons_filter_type = RTE_ETH_FILTER_TUNNEL;
1593
1594         return ret;
1595 }
1596
1597 /* 1. Last in item should be NULL as range is not supported.
1598  * 2. Supported filter types: MPLS label.
1599  * 3. Mask of fields which need to be matched should be
1600  *    filled with 1.
1601  * 4. Mask of fields which needn't to be matched should be
1602  *    filled with 0.
1603  */
1604 static int
1605 i40e_flow_parse_mpls_pattern(__rte_unused struct rte_eth_dev *dev,
1606                              const struct rte_flow_item *pattern,
1607                              struct rte_flow_error *error,
1608                              struct i40e_tunnel_filter_conf *filter)
1609 {
1610         const struct rte_flow_item *item = pattern;
1611         const struct rte_flow_item_mpls *mpls_spec;
1612         const struct rte_flow_item_mpls *mpls_mask;
1613         enum rte_flow_item_type item_type;
1614         bool is_mplsoudp = 0; /* 1 - MPLSoUDP, 0 - MPLSoGRE */
1615         const uint8_t label_mask[3] = {0xFF, 0xFF, 0xF0};
1616         uint32_t label_be = 0;
1617
1618         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1619                 if (item->last) {
1620                         rte_flow_error_set(error, EINVAL,
1621                                            RTE_FLOW_ERROR_TYPE_ITEM,
1622                                            item,
1623                                            "Not support range");
1624                         return -rte_errno;
1625                 }
1626                 item_type = item->type;
1627                 switch (item_type) {
1628                 case RTE_FLOW_ITEM_TYPE_ETH:
1629                         if (item->spec || item->mask) {
1630                                 rte_flow_error_set(error, EINVAL,
1631                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1632                                                    item,
1633                                                    "Invalid ETH item");
1634                                 return -rte_errno;
1635                         }
1636                         break;
1637                 case RTE_FLOW_ITEM_TYPE_IPV4:
1638                         filter->ip_type = I40E_TUNNEL_IPTYPE_IPV4;
1639                         /* IPv4 is used to describe protocol,
1640                          * spec and mask should be NULL.
1641                          */
1642                         if (item->spec || item->mask) {
1643                                 rte_flow_error_set(error, EINVAL,
1644                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1645                                                    item,
1646                                                    "Invalid IPv4 item");
1647                                 return -rte_errno;
1648                         }
1649                         break;
1650                 case RTE_FLOW_ITEM_TYPE_IPV6:
1651                         filter->ip_type = I40E_TUNNEL_IPTYPE_IPV6;
1652                         /* IPv6 is used to describe protocol,
1653                          * spec and mask should be NULL.
1654                          */
1655                         if (item->spec || item->mask) {
1656                                 rte_flow_error_set(error, EINVAL,
1657                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1658                                                    item,
1659                                                    "Invalid IPv6 item");
1660                                 return -rte_errno;
1661                         }
1662                         break;
1663                 case RTE_FLOW_ITEM_TYPE_UDP:
1664                         /* UDP is used to describe protocol,
1665                          * spec and mask should be NULL.
1666                          */
1667                         if (item->spec || item->mask) {
1668                                 rte_flow_error_set(error, EINVAL,
1669                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1670                                                    item,
1671                                                    "Invalid UDP item");
1672                                 return -rte_errno;
1673                         }
1674                         is_mplsoudp = 1;
1675                         break;
1676                 case RTE_FLOW_ITEM_TYPE_GRE:
1677                         /* GRE is used to describe protocol,
1678                          * spec and mask should be NULL.
1679                          */
1680                         if (item->spec || item->mask) {
1681                                 rte_flow_error_set(error, EINVAL,
1682                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1683                                                    item,
1684                                                    "Invalid GRE item");
1685                                 return -rte_errno;
1686                         }
1687                         break;
1688                 case RTE_FLOW_ITEM_TYPE_MPLS:
1689                         mpls_spec =
1690                                 (const struct rte_flow_item_mpls *)item->spec;
1691                         mpls_mask =
1692                                 (const struct rte_flow_item_mpls *)item->mask;
1693
1694                         if (!mpls_spec || !mpls_mask) {
1695                                 rte_flow_error_set(error, EINVAL,
1696                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1697                                                    item,
1698                                                    "Invalid MPLS item");
1699                                 return -rte_errno;
1700                         }
1701
1702                         if (memcmp(mpls_mask->label_tc_s, label_mask, 3)) {
1703                                 rte_flow_error_set(error, EINVAL,
1704                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1705                                                    item,
1706                                                    "Invalid MPLS label mask");
1707                                 return -rte_errno;
1708                         }
1709                         rte_memcpy(((uint8_t *)&label_be + 1),
1710                                    mpls_spec->label_tc_s, 3);
1711                         filter->tenant_id = rte_be_to_cpu_32(label_be) >> 4;
1712                         break;
1713                 default:
1714                         break;
1715                 }
1716         }
1717
1718         if (is_mplsoudp)
1719                 filter->tunnel_type = I40E_TUNNEL_TYPE_MPLSoUDP;
1720         else
1721                 filter->tunnel_type = I40E_TUNNEL_TYPE_MPLSoGRE;
1722
1723         return 0;
1724 }
1725
1726 static int
1727 i40e_flow_parse_mpls_filter(struct rte_eth_dev *dev,
1728                             const struct rte_flow_attr *attr,
1729                             const struct rte_flow_item pattern[],
1730                             const struct rte_flow_action actions[],
1731                             struct rte_flow_error *error,
1732                             union i40e_filter_t *filter)
1733 {
1734         struct i40e_tunnel_filter_conf *tunnel_filter =
1735                 &filter->consistent_tunnel_filter;
1736         int ret;
1737
1738         ret = i40e_flow_parse_mpls_pattern(dev, pattern,
1739                                            error, tunnel_filter);
1740         if (ret)
1741                 return ret;
1742
1743         ret = i40e_flow_parse_tunnel_action(dev, actions, error, tunnel_filter);
1744         if (ret)
1745                 return ret;
1746
1747         ret = i40e_flow_parse_attr(attr, error);
1748         if (ret)
1749                 return ret;
1750
1751         cons_filter_type = RTE_ETH_FILTER_TUNNEL;
1752
1753         return ret;
1754 }
1755
1756 /* 1. Last in item should be NULL as range is not supported.
1757  * 2. Supported filter types: QINQ.
1758  * 3. Mask of fields which need to be matched should be
1759  *    filled with 1.
1760  * 4. Mask of fields which needn't to be matched should be
1761  *    filled with 0.
1762  */
1763 static int
1764 i40e_flow_parse_qinq_pattern(__rte_unused struct rte_eth_dev *dev,
1765                               const struct rte_flow_item *pattern,
1766                               struct rte_flow_error *error,
1767                               struct i40e_tunnel_filter_conf *filter)
1768 {
1769         const struct rte_flow_item *item = pattern;
1770         const struct rte_flow_item_vlan *vlan_spec = NULL;
1771         const struct rte_flow_item_vlan *vlan_mask = NULL;
1772         const struct rte_flow_item_vlan *i_vlan_spec = NULL;
1773         const struct rte_flow_item_vlan *i_vlan_mask = NULL;
1774         const struct rte_flow_item_vlan *o_vlan_spec = NULL;
1775         const struct rte_flow_item_vlan *o_vlan_mask = NULL;
1776
1777         enum rte_flow_item_type item_type;
1778         bool vlan_flag = 0;
1779
1780         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1781                 if (item->last) {
1782                         rte_flow_error_set(error, EINVAL,
1783                                            RTE_FLOW_ERROR_TYPE_ITEM,
1784                                            item,
1785                                            "Not support range");
1786                         return -rte_errno;
1787                 }
1788                 item_type = item->type;
1789                 switch (item_type) {
1790                 case RTE_FLOW_ITEM_TYPE_ETH:
1791                         if (item->spec || item->mask) {
1792                                 rte_flow_error_set(error, EINVAL,
1793                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1794                                                    item,
1795                                                    "Invalid ETH item");
1796                                 return -rte_errno;
1797                         }
1798                         break;
1799                 case RTE_FLOW_ITEM_TYPE_VLAN:
1800                         vlan_spec =
1801                                 (const struct rte_flow_item_vlan *)item->spec;
1802                         vlan_mask =
1803                                 (const struct rte_flow_item_vlan *)item->mask;
1804
1805                         if (!(vlan_spec && vlan_mask)) {
1806                                 rte_flow_error_set(error, EINVAL,
1807                                            RTE_FLOW_ERROR_TYPE_ITEM,
1808                                            item,
1809                                            "Invalid vlan item");
1810                                 return -rte_errno;
1811                         }
1812
1813                         if (!vlan_flag) {
1814                                 o_vlan_spec = vlan_spec;
1815                                 o_vlan_mask = vlan_mask;
1816                                 vlan_flag = 1;
1817                         } else {
1818                                 i_vlan_spec = vlan_spec;
1819                                 i_vlan_mask = vlan_mask;
1820                                 vlan_flag = 0;
1821                         }
1822                         break;
1823
1824                 default:
1825                         break;
1826                 }
1827         }
1828
1829         /* Get filter specification */
1830         if ((o_vlan_mask->tci == rte_cpu_to_be_16(I40E_TCI_MASK)) &&
1831             (i_vlan_mask->tci == rte_cpu_to_be_16(I40E_TCI_MASK))) {
1832                 filter->outer_vlan = rte_be_to_cpu_16(o_vlan_spec->tci)
1833                         & I40E_TCI_MASK;
1834                 filter->inner_vlan = rte_be_to_cpu_16(i_vlan_spec->tci)
1835                         & I40E_TCI_MASK;
1836         } else {
1837                         rte_flow_error_set(error, EINVAL,
1838                                            RTE_FLOW_ERROR_TYPE_ITEM,
1839                                            NULL,
1840                                            "Invalid filter type");
1841                         return -rte_errno;
1842         }
1843
1844         filter->tunnel_type = I40E_TUNNEL_TYPE_QINQ;
1845         return 0;
1846 }
1847
1848 static int
1849 i40e_flow_parse_qinq_filter(struct rte_eth_dev *dev,
1850                               const struct rte_flow_attr *attr,
1851                               const struct rte_flow_item pattern[],
1852                               const struct rte_flow_action actions[],
1853                               struct rte_flow_error *error,
1854                               union i40e_filter_t *filter)
1855 {
1856         struct i40e_tunnel_filter_conf *tunnel_filter =
1857                 &filter->consistent_tunnel_filter;
1858         int ret;
1859
1860         ret = i40e_flow_parse_qinq_pattern(dev, pattern,
1861                                              error, tunnel_filter);
1862         if (ret)
1863                 return ret;
1864
1865         ret = i40e_flow_parse_tunnel_action(dev, actions, error, tunnel_filter);
1866         if (ret)
1867                 return ret;
1868
1869         ret = i40e_flow_parse_attr(attr, error);
1870         if (ret)
1871                 return ret;
1872
1873         cons_filter_type = RTE_ETH_FILTER_TUNNEL;
1874
1875         return ret;
1876 }
1877
1878 static int
1879 i40e_flow_validate(struct rte_eth_dev *dev,
1880                    const struct rte_flow_attr *attr,
1881                    const struct rte_flow_item pattern[],
1882                    const struct rte_flow_action actions[],
1883                    struct rte_flow_error *error)
1884 {
1885         struct rte_flow_item *items; /* internal pattern w/o VOID items */
1886         parse_filter_t parse_filter;
1887         uint32_t item_num = 0; /* non-void item number of pattern*/
1888         uint32_t i = 0;
1889         int ret;
1890
1891         if (!pattern) {
1892                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1893                                    NULL, "NULL pattern.");
1894                 return -rte_errno;
1895         }
1896
1897         if (!actions) {
1898                 rte_flow_error_set(error, EINVAL,
1899                                    RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1900                                    NULL, "NULL action.");
1901                 return -rte_errno;
1902         }
1903
1904         if (!attr) {
1905                 rte_flow_error_set(error, EINVAL,
1906                                    RTE_FLOW_ERROR_TYPE_ATTR,
1907                                    NULL, "NULL attribute.");
1908                 return -rte_errno;
1909         }
1910
1911         memset(&cons_filter, 0, sizeof(cons_filter));
1912
1913         /* Get the non-void item number of pattern */
1914         while ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_END) {
1915                 if ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_VOID)
1916                         item_num++;
1917                 i++;
1918         }
1919         item_num++;
1920
1921         items = rte_zmalloc("i40e_pattern",
1922                             item_num * sizeof(struct rte_flow_item), 0);
1923         if (!items) {
1924                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1925                                    NULL, "No memory for PMD internal items.");
1926                 return -ENOMEM;
1927         }
1928
1929         i40e_pattern_skip_void_item(items, pattern);
1930
1931         /* Find if there's matched parse filter function */
1932         parse_filter = i40e_find_parse_filter_func(items);
1933         if (!parse_filter) {
1934                 rte_flow_error_set(error, EINVAL,
1935                                    RTE_FLOW_ERROR_TYPE_ITEM,
1936                                    pattern, "Unsupported pattern");
1937                 return -rte_errno;
1938         }
1939
1940         ret = parse_filter(dev, attr, items, actions, error, &cons_filter);
1941
1942         rte_free(items);
1943
1944         return ret;
1945 }
1946
1947 static struct rte_flow *
1948 i40e_flow_create(struct rte_eth_dev *dev,
1949                  const struct rte_flow_attr *attr,
1950                  const struct rte_flow_item pattern[],
1951                  const struct rte_flow_action actions[],
1952                  struct rte_flow_error *error)
1953 {
1954         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1955         struct rte_flow *flow;
1956         int ret;
1957
1958         flow = rte_zmalloc("i40e_flow", sizeof(struct rte_flow), 0);
1959         if (!flow) {
1960                 rte_flow_error_set(error, ENOMEM,
1961                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1962                                    "Failed to allocate memory");
1963                 return flow;
1964         }
1965
1966         ret = i40e_flow_validate(dev, attr, pattern, actions, error);
1967         if (ret < 0)
1968                 return NULL;
1969
1970         switch (cons_filter_type) {
1971         case RTE_ETH_FILTER_ETHERTYPE:
1972                 ret = i40e_ethertype_filter_set(pf,
1973                                         &cons_filter.ethertype_filter, 1);
1974                 if (ret)
1975                         goto free_flow;
1976                 flow->rule = TAILQ_LAST(&pf->ethertype.ethertype_list,
1977                                         i40e_ethertype_filter_list);
1978                 break;
1979         case RTE_ETH_FILTER_FDIR:
1980                 ret = i40e_add_del_fdir_filter(dev,
1981                                        &cons_filter.fdir_filter, 1);
1982                 if (ret)
1983                         goto free_flow;
1984                 flow->rule = TAILQ_LAST(&pf->fdir.fdir_list,
1985                                         i40e_fdir_filter_list);
1986                 break;
1987         case RTE_ETH_FILTER_TUNNEL:
1988                 ret = i40e_dev_consistent_tunnel_filter_set(pf,
1989                             &cons_filter.consistent_tunnel_filter, 1);
1990                 if (ret)
1991                         goto free_flow;
1992                 flow->rule = TAILQ_LAST(&pf->tunnel.tunnel_list,
1993                                         i40e_tunnel_filter_list);
1994                 break;
1995         default:
1996                 goto free_flow;
1997         }
1998
1999         flow->filter_type = cons_filter_type;
2000         TAILQ_INSERT_TAIL(&pf->flow_list, flow, node);
2001         return flow;
2002
2003 free_flow:
2004         rte_flow_error_set(error, -ret,
2005                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2006                            "Failed to create flow.");
2007         rte_free(flow);
2008         return NULL;
2009 }
2010
2011 static int
2012 i40e_flow_destroy(struct rte_eth_dev *dev,
2013                   struct rte_flow *flow,
2014                   struct rte_flow_error *error)
2015 {
2016         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2017         enum rte_filter_type filter_type = flow->filter_type;
2018         int ret = 0;
2019
2020         switch (filter_type) {
2021         case RTE_ETH_FILTER_ETHERTYPE:
2022                 ret = i40e_flow_destroy_ethertype_filter(pf,
2023                          (struct i40e_ethertype_filter *)flow->rule);
2024                 break;
2025         case RTE_ETH_FILTER_TUNNEL:
2026                 ret = i40e_flow_destroy_tunnel_filter(pf,
2027                               (struct i40e_tunnel_filter *)flow->rule);
2028                 break;
2029         case RTE_ETH_FILTER_FDIR:
2030                 ret = i40e_add_del_fdir_filter(dev,
2031                        &((struct i40e_fdir_filter *)flow->rule)->fdir, 0);
2032                 break;
2033         default:
2034                 PMD_DRV_LOG(WARNING, "Filter type (%d) not supported",
2035                             filter_type);
2036                 ret = -EINVAL;
2037                 break;
2038         }
2039
2040         if (!ret) {
2041                 TAILQ_REMOVE(&pf->flow_list, flow, node);
2042                 rte_free(flow);
2043         } else
2044                 rte_flow_error_set(error, -ret,
2045                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2046                                    "Failed to destroy flow.");
2047
2048         return ret;
2049 }
2050
2051 static int
2052 i40e_flow_destroy_ethertype_filter(struct i40e_pf *pf,
2053                                    struct i40e_ethertype_filter *filter)
2054 {
2055         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
2056         struct i40e_ethertype_rule *ethertype_rule = &pf->ethertype;
2057         struct i40e_ethertype_filter *node;
2058         struct i40e_control_filter_stats stats;
2059         uint16_t flags = 0;
2060         int ret = 0;
2061
2062         if (!(filter->flags & RTE_ETHTYPE_FLAGS_MAC))
2063                 flags |= I40E_AQC_ADD_CONTROL_PACKET_FLAGS_IGNORE_MAC;
2064         if (filter->flags & RTE_ETHTYPE_FLAGS_DROP)
2065                 flags |= I40E_AQC_ADD_CONTROL_PACKET_FLAGS_DROP;
2066         flags |= I40E_AQC_ADD_CONTROL_PACKET_FLAGS_TO_QUEUE;
2067
2068         memset(&stats, 0, sizeof(stats));
2069         ret = i40e_aq_add_rem_control_packet_filter(hw,
2070                                     filter->input.mac_addr.addr_bytes,
2071                                     filter->input.ether_type,
2072                                     flags, pf->main_vsi->seid,
2073                                     filter->queue, 0, &stats, NULL);
2074         if (ret < 0)
2075                 return ret;
2076
2077         node = i40e_sw_ethertype_filter_lookup(ethertype_rule, &filter->input);
2078         if (!node)
2079                 return -EINVAL;
2080
2081         ret = i40e_sw_ethertype_filter_del(pf, &node->input);
2082
2083         return ret;
2084 }
2085
2086 static int
2087 i40e_flow_destroy_tunnel_filter(struct i40e_pf *pf,
2088                                 struct i40e_tunnel_filter *filter)
2089 {
2090         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
2091         struct i40e_vsi *vsi;
2092         struct i40e_pf_vf *vf;
2093         struct i40e_aqc_add_rm_cloud_filt_elem_ext cld_filter;
2094         struct i40e_tunnel_rule *tunnel_rule = &pf->tunnel;
2095         struct i40e_tunnel_filter *node;
2096         bool big_buffer = 0;
2097         int ret = 0;
2098
2099         memset(&cld_filter, 0, sizeof(cld_filter));
2100         ether_addr_copy((struct ether_addr *)&filter->input.outer_mac,
2101                         (struct ether_addr *)&cld_filter.element.outer_mac);
2102         ether_addr_copy((struct ether_addr *)&filter->input.inner_mac,
2103                         (struct ether_addr *)&cld_filter.element.inner_mac);
2104         cld_filter.element.inner_vlan = filter->input.inner_vlan;
2105         cld_filter.element.flags = filter->input.flags;
2106         cld_filter.element.tenant_id = filter->input.tenant_id;
2107         cld_filter.element.queue_number = filter->queue;
2108         rte_memcpy(cld_filter.general_fields,
2109                    filter->input.general_fields,
2110                    sizeof(cld_filter.general_fields));
2111
2112         if (!filter->is_to_vf)
2113                 vsi = pf->main_vsi;
2114         else {
2115                 vf = &pf->vfs[filter->vf_id];
2116                 vsi = vf->vsi;
2117         }
2118
2119         if (((filter->input.flags & I40E_AQC_ADD_CLOUD_FILTER_TEID_MPLSoUDP) ==
2120             I40E_AQC_ADD_CLOUD_FILTER_TEID_MPLSoUDP) ||
2121             ((filter->input.flags & I40E_AQC_ADD_CLOUD_FILTER_TEID_MPLSoGRE) ==
2122             I40E_AQC_ADD_CLOUD_FILTER_TEID_MPLSoGRE) ||
2123             ((filter->input.flags & I40E_AQC_ADD_CLOUD_FILTER_CUSTOM_QINQ) ==
2124             I40E_AQC_ADD_CLOUD_FILTER_CUSTOM_QINQ))
2125                 big_buffer = 1;
2126
2127         if (big_buffer)
2128                 ret = i40e_aq_remove_cloud_filters_big_buffer(hw, vsi->seid,
2129                                                               &cld_filter, 1);
2130         else
2131                 ret = i40e_aq_remove_cloud_filters(hw, vsi->seid,
2132                                                    &cld_filter.element, 1);
2133         if (ret < 0)
2134                 return -ENOTSUP;
2135
2136         node = i40e_sw_tunnel_filter_lookup(tunnel_rule, &filter->input);
2137         if (!node)
2138                 return -EINVAL;
2139
2140         ret = i40e_sw_tunnel_filter_del(pf, &node->input);
2141
2142         return ret;
2143 }
2144
2145 static int
2146 i40e_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
2147 {
2148         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2149         int ret;
2150
2151         ret = i40e_flow_flush_fdir_filter(pf);
2152         if (ret) {
2153                 rte_flow_error_set(error, -ret,
2154                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2155                                    "Failed to flush FDIR flows.");
2156                 return -rte_errno;
2157         }
2158
2159         ret = i40e_flow_flush_ethertype_filter(pf);
2160         if (ret) {
2161                 rte_flow_error_set(error, -ret,
2162                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2163                                    "Failed to ethertype flush flows.");
2164                 return -rte_errno;
2165         }
2166
2167         ret = i40e_flow_flush_tunnel_filter(pf);
2168         if (ret) {
2169                 rte_flow_error_set(error, -ret,
2170                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2171                                    "Failed to flush tunnel flows.");
2172                 return -rte_errno;
2173         }
2174
2175         return ret;
2176 }
2177
2178 static int
2179 i40e_flow_flush_fdir_filter(struct i40e_pf *pf)
2180 {
2181         struct rte_eth_dev *dev = pf->adapter->eth_dev;
2182         struct i40e_fdir_info *fdir_info = &pf->fdir;
2183         struct i40e_fdir_filter *fdir_filter;
2184         struct rte_flow *flow;
2185         void *temp;
2186         int ret;
2187
2188         ret = i40e_fdir_flush(dev);
2189         if (!ret) {
2190                 /* Delete FDIR filters in FDIR list. */
2191                 while ((fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list))) {
2192                         ret = i40e_sw_fdir_filter_del(pf,
2193                                                       &fdir_filter->fdir.input);
2194                         if (ret < 0)
2195                                 return ret;
2196                 }
2197
2198                 /* Delete FDIR flows in flow list. */
2199                 TAILQ_FOREACH_SAFE(flow, &pf->flow_list, node, temp) {
2200                         if (flow->filter_type == RTE_ETH_FILTER_FDIR) {
2201                                 TAILQ_REMOVE(&pf->flow_list, flow, node);
2202                                 rte_free(flow);
2203                         }
2204                 }
2205         }
2206
2207         return ret;
2208 }
2209
2210 /* Flush all ethertype filters */
2211 static int
2212 i40e_flow_flush_ethertype_filter(struct i40e_pf *pf)
2213 {
2214         struct i40e_ethertype_filter_list
2215                 *ethertype_list = &pf->ethertype.ethertype_list;
2216         struct i40e_ethertype_filter *filter;
2217         struct rte_flow *flow;
2218         void *temp;
2219         int ret = 0;
2220
2221         while ((filter = TAILQ_FIRST(ethertype_list))) {
2222                 ret = i40e_flow_destroy_ethertype_filter(pf, filter);
2223                 if (ret)
2224                         return ret;
2225         }
2226
2227         /* Delete ethertype flows in flow list. */
2228         TAILQ_FOREACH_SAFE(flow, &pf->flow_list, node, temp) {
2229                 if (flow->filter_type == RTE_ETH_FILTER_ETHERTYPE) {
2230                         TAILQ_REMOVE(&pf->flow_list, flow, node);
2231                         rte_free(flow);
2232                 }
2233         }
2234
2235         return ret;
2236 }
2237
2238 /* Flush all tunnel filters */
2239 static int
2240 i40e_flow_flush_tunnel_filter(struct i40e_pf *pf)
2241 {
2242         struct i40e_tunnel_filter_list
2243                 *tunnel_list = &pf->tunnel.tunnel_list;
2244         struct i40e_tunnel_filter *filter;
2245         struct rte_flow *flow;
2246         void *temp;
2247         int ret = 0;
2248
2249         while ((filter = TAILQ_FIRST(tunnel_list))) {
2250                 ret = i40e_flow_destroy_tunnel_filter(pf, filter);
2251                 if (ret)
2252                         return ret;
2253         }
2254
2255         /* Delete tunnel flows in flow list. */
2256         TAILQ_FOREACH_SAFE(flow, &pf->flow_list, node, temp) {
2257                 if (flow->filter_type == RTE_ETH_FILTER_TUNNEL) {
2258                         TAILQ_REMOVE(&pf->flow_list, flow, node);
2259                         rte_free(flow);
2260                 }
2261         }
2262
2263         return ret;
2264 }