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