net/i40e: fix parsing tunnel filter
[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                 case RTE_FLOW_ITEM_TYPE_IPV6:
1311                 case RTE_FLOW_ITEM_TYPE_UDP:
1312                         /* IPv4/IPv6/UDP are used to describe protocol,
1313                          * spec amd mask should be NULL.
1314                          */
1315                         if (item->spec || item->mask) {
1316                                 rte_flow_error_set(error, EINVAL,
1317                                            RTE_FLOW_ERROR_TYPE_ITEM,
1318                                            item,
1319                                            "Invalid IPv4 item");
1320                                 return -rte_errno;
1321                         }
1322                         break;
1323                 case RTE_FLOW_ITEM_TYPE_VXLAN:
1324                         vxlan_spec =
1325                                 (const struct rte_flow_item_vxlan *)item->spec;
1326                         vxlan_mask =
1327                                 (const struct rte_flow_item_vxlan *)item->mask;
1328                         /* Check if VXLAN item is used to describe protocol.
1329                          * If yes, both spec and mask should be NULL.
1330                          * If no, either spec or mask shouldn't be NULL.
1331                          */
1332                         if ((!vxlan_spec && vxlan_mask) ||
1333                             (vxlan_spec && !vxlan_mask)) {
1334                                 rte_flow_error_set(error, EINVAL,
1335                                            RTE_FLOW_ERROR_TYPE_ITEM,
1336                                            item,
1337                                            "Invalid VXLAN item");
1338                                 return -rte_errno;
1339                         }
1340
1341                         /* Check if VNI is masked. */
1342                         if (vxlan_mask) {
1343                                 is_vni_masked =
1344                                 i40e_check_tenant_id_mask(vxlan_mask->vni);
1345                                 if (is_vni_masked < 0) {
1346                                         rte_flow_error_set(error, EINVAL,
1347                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1348                                                    item,
1349                                                    "Invalid VNI mask");
1350                                         return -rte_errno;
1351                                 }
1352                         }
1353                         vxlan_flag = 1;
1354                         break;
1355                 default:
1356                         break;
1357                 }
1358         }
1359
1360         /* Check specification and mask to get the filter type */
1361         if (vlan_spec && vlan_mask &&
1362             (vlan_mask->tci == rte_cpu_to_be_16(I40E_TCI_MASK))) {
1363                 /* If there's inner vlan */
1364                 filter->inner_vlan = rte_be_to_cpu_16(vlan_spec->tci)
1365                         & I40E_TCI_MASK;
1366                 if (vxlan_spec && vxlan_mask && !is_vni_masked) {
1367                         /* If there's vxlan */
1368                         rte_memcpy(((uint8_t *)&tenant_id_be + 1),
1369                                    vxlan_spec->vni, 3);
1370                         filter->tenant_id = rte_be_to_cpu_32(tenant_id_be);
1371                         if (!o_eth_spec && !o_eth_mask &&
1372                                 i_eth_spec && i_eth_mask)
1373                                 filter->filter_type =
1374                                         RTE_TUNNEL_FILTER_IMAC_IVLAN_TENID;
1375                         else {
1376                                 rte_flow_error_set(error, EINVAL,
1377                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1378                                                    NULL,
1379                                                    "Invalid filter type");
1380                                 return -rte_errno;
1381                         }
1382                 } else if (!vxlan_spec && !vxlan_mask) {
1383                         /* If there's no vxlan */
1384                         if (!o_eth_spec && !o_eth_mask &&
1385                                 i_eth_spec && i_eth_mask)
1386                                 filter->filter_type =
1387                                         RTE_TUNNEL_FILTER_IMAC_IVLAN;
1388                         else {
1389                                 rte_flow_error_set(error, EINVAL,
1390                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1391                                                    NULL,
1392                                                    "Invalid filter type");
1393                                 return -rte_errno;
1394                         }
1395                 } else {
1396                         rte_flow_error_set(error, EINVAL,
1397                                            RTE_FLOW_ERROR_TYPE_ITEM,
1398                                            NULL,
1399                                            "Invalid filter type");
1400                         return -rte_errno;
1401                 }
1402         } else if ((!vlan_spec && !vlan_mask) ||
1403                    (vlan_spec && vlan_mask && vlan_mask->tci == 0x0)) {
1404                 /* If there's no inner vlan */
1405                 if (vxlan_spec && vxlan_mask && !is_vni_masked) {
1406                         /* If there's vxlan */
1407                         rte_memcpy(((uint8_t *)&tenant_id_be + 1),
1408                                    vxlan_spec->vni, 3);
1409                         filter->tenant_id = rte_be_to_cpu_32(tenant_id_be);
1410                         if (!o_eth_spec && !o_eth_mask &&
1411                                 i_eth_spec && i_eth_mask)
1412                                 filter->filter_type =
1413                                         RTE_TUNNEL_FILTER_IMAC_TENID;
1414                         else if (o_eth_spec && o_eth_mask &&
1415                                 i_eth_spec && i_eth_mask)
1416                                 filter->filter_type =
1417                                         RTE_TUNNEL_FILTER_OMAC_TENID_IMAC;
1418                 } else if (!vxlan_spec && !vxlan_mask) {
1419                         /* If there's no vxlan */
1420                         if (!o_eth_spec && !o_eth_mask &&
1421                                 i_eth_spec && i_eth_mask) {
1422                                 filter->filter_type = ETH_TUNNEL_FILTER_IMAC;
1423                         } else {
1424                                 rte_flow_error_set(error, EINVAL,
1425                                            RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1426                                            "Invalid filter type");
1427                                 return -rte_errno;
1428                         }
1429                 } else {
1430                         rte_flow_error_set(error, EINVAL,
1431                                            RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1432                                            "Invalid filter type");
1433                         return -rte_errno;
1434                 }
1435         } else {
1436                 rte_flow_error_set(error, EINVAL,
1437                                    RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1438                                    "Not supported by tunnel filter.");
1439                 return -rte_errno;
1440         }
1441
1442         filter->tunnel_type = RTE_TUNNEL_TYPE_VXLAN;
1443
1444         return 0;
1445 }
1446
1447 static int
1448 i40e_flow_parse_tunnel_pattern(__rte_unused struct rte_eth_dev *dev,
1449                                const struct rte_flow_item *pattern,
1450                                struct rte_flow_error *error,
1451                                struct rte_eth_tunnel_filter_conf *filter)
1452 {
1453         int ret;
1454
1455         ret = i40e_flow_parse_vxlan_pattern(pattern, error, filter);
1456
1457         return ret;
1458 }
1459
1460 static int
1461 i40e_flow_parse_tunnel_filter(struct rte_eth_dev *dev,
1462                               const struct rte_flow_attr *attr,
1463                               const struct rte_flow_item pattern[],
1464                               const struct rte_flow_action actions[],
1465                               struct rte_flow_error *error,
1466                               union i40e_filter_t *filter)
1467 {
1468         struct rte_eth_tunnel_filter_conf *tunnel_filter =
1469                 &filter->tunnel_filter;
1470         int ret;
1471
1472         ret = i40e_flow_parse_tunnel_pattern(dev, pattern,
1473                                              error, tunnel_filter);
1474         if (ret)
1475                 return ret;
1476
1477         ret = i40e_flow_parse_tunnel_action(dev, actions, error, tunnel_filter);
1478         if (ret)
1479                 return ret;
1480
1481         ret = i40e_flow_parse_attr(attr, error);
1482         if (ret)
1483                 return ret;
1484
1485         cons_filter_type = RTE_ETH_FILTER_TUNNEL;
1486
1487         return ret;
1488 }
1489
1490 static int
1491 i40e_flow_validate(struct rte_eth_dev *dev,
1492                    const struct rte_flow_attr *attr,
1493                    const struct rte_flow_item pattern[],
1494                    const struct rte_flow_action actions[],
1495                    struct rte_flow_error *error)
1496 {
1497         struct rte_flow_item *items; /* internal pattern w/o VOID items */
1498         parse_filter_t parse_filter;
1499         uint32_t item_num = 0; /* non-void item number of pattern*/
1500         uint32_t i = 0;
1501         int ret;
1502
1503         if (!pattern) {
1504                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1505                                    NULL, "NULL pattern.");
1506                 return -rte_errno;
1507         }
1508
1509         if (!actions) {
1510                 rte_flow_error_set(error, EINVAL,
1511                                    RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1512                                    NULL, "NULL action.");
1513                 return -rte_errno;
1514         }
1515
1516         if (!attr) {
1517                 rte_flow_error_set(error, EINVAL,
1518                                    RTE_FLOW_ERROR_TYPE_ATTR,
1519                                    NULL, "NULL attribute.");
1520                 return -rte_errno;
1521         }
1522
1523         memset(&cons_filter, 0, sizeof(cons_filter));
1524
1525         /* Get the non-void item number of pattern */
1526         while ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_END) {
1527                 if ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_VOID)
1528                         item_num++;
1529                 i++;
1530         }
1531         item_num++;
1532
1533         items = rte_zmalloc("i40e_pattern",
1534                             item_num * sizeof(struct rte_flow_item), 0);
1535         if (!items) {
1536                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1537                                    NULL, "No memory for PMD internal items.");
1538                 return -ENOMEM;
1539         }
1540
1541         i40e_pattern_skip_void_item(items, pattern);
1542
1543         /* Find if there's matched parse filter function */
1544         parse_filter = i40e_find_parse_filter_func(items);
1545         if (!parse_filter) {
1546                 rte_flow_error_set(error, EINVAL,
1547                                    RTE_FLOW_ERROR_TYPE_ITEM,
1548                                    pattern, "Unsupported pattern");
1549                 return -rte_errno;
1550         }
1551
1552         ret = parse_filter(dev, attr, items, actions, error, &cons_filter);
1553
1554         rte_free(items);
1555
1556         return ret;
1557 }
1558
1559 static struct rte_flow *
1560 i40e_flow_create(struct rte_eth_dev *dev,
1561                  const struct rte_flow_attr *attr,
1562                  const struct rte_flow_item pattern[],
1563                  const struct rte_flow_action actions[],
1564                  struct rte_flow_error *error)
1565 {
1566         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1567         struct rte_flow *flow;
1568         int ret;
1569
1570         flow = rte_zmalloc("i40e_flow", sizeof(struct rte_flow), 0);
1571         if (!flow) {
1572                 rte_flow_error_set(error, ENOMEM,
1573                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1574                                    "Failed to allocate memory");
1575                 return flow;
1576         }
1577
1578         ret = i40e_flow_validate(dev, attr, pattern, actions, error);
1579         if (ret < 0)
1580                 return NULL;
1581
1582         switch (cons_filter_type) {
1583         case RTE_ETH_FILTER_ETHERTYPE:
1584                 ret = i40e_ethertype_filter_set(pf,
1585                                         &cons_filter.ethertype_filter, 1);
1586                 if (ret)
1587                         goto free_flow;
1588                 flow->rule = TAILQ_LAST(&pf->ethertype.ethertype_list,
1589                                         i40e_ethertype_filter_list);
1590                 break;
1591         case RTE_ETH_FILTER_FDIR:
1592                 ret = i40e_add_del_fdir_filter(dev,
1593                                        &cons_filter.fdir_filter, 1);
1594                 if (ret)
1595                         goto free_flow;
1596                 flow->rule = TAILQ_LAST(&pf->fdir.fdir_list,
1597                                         i40e_fdir_filter_list);
1598                 break;
1599         case RTE_ETH_FILTER_TUNNEL:
1600                 ret = i40e_dev_tunnel_filter_set(pf,
1601                                          &cons_filter.tunnel_filter, 1);
1602                 if (ret)
1603                         goto free_flow;
1604                 flow->rule = TAILQ_LAST(&pf->tunnel.tunnel_list,
1605                                         i40e_tunnel_filter_list);
1606                 break;
1607         default:
1608                 goto free_flow;
1609         }
1610
1611         flow->filter_type = cons_filter_type;
1612         TAILQ_INSERT_TAIL(&pf->flow_list, flow, node);
1613         return flow;
1614
1615 free_flow:
1616         rte_flow_error_set(error, -ret,
1617                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1618                            "Failed to create flow.");
1619         rte_free(flow);
1620         return NULL;
1621 }
1622
1623 static int
1624 i40e_flow_destroy(struct rte_eth_dev *dev,
1625                   struct rte_flow *flow,
1626                   struct rte_flow_error *error)
1627 {
1628         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1629         enum rte_filter_type filter_type = flow->filter_type;
1630         int ret = 0;
1631
1632         switch (filter_type) {
1633         case RTE_ETH_FILTER_ETHERTYPE:
1634                 ret = i40e_flow_destroy_ethertype_filter(pf,
1635                          (struct i40e_ethertype_filter *)flow->rule);
1636                 break;
1637         case RTE_ETH_FILTER_TUNNEL:
1638                 ret = i40e_flow_destroy_tunnel_filter(pf,
1639                               (struct i40e_tunnel_filter *)flow->rule);
1640                 break;
1641         case RTE_ETH_FILTER_FDIR:
1642                 ret = i40e_add_del_fdir_filter(dev,
1643                        &((struct i40e_fdir_filter *)flow->rule)->fdir, 0);
1644                 break;
1645         default:
1646                 PMD_DRV_LOG(WARNING, "Filter type (%d) not supported",
1647                             filter_type);
1648                 ret = -EINVAL;
1649                 break;
1650         }
1651
1652         if (!ret) {
1653                 TAILQ_REMOVE(&pf->flow_list, flow, node);
1654                 rte_free(flow);
1655         } else
1656                 rte_flow_error_set(error, -ret,
1657                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1658                                    "Failed to destroy flow.");
1659
1660         return ret;
1661 }
1662
1663 static int
1664 i40e_flow_destroy_ethertype_filter(struct i40e_pf *pf,
1665                                    struct i40e_ethertype_filter *filter)
1666 {
1667         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
1668         struct i40e_ethertype_rule *ethertype_rule = &pf->ethertype;
1669         struct i40e_ethertype_filter *node;
1670         struct i40e_control_filter_stats stats;
1671         uint16_t flags = 0;
1672         int ret = 0;
1673
1674         if (!(filter->flags & RTE_ETHTYPE_FLAGS_MAC))
1675                 flags |= I40E_AQC_ADD_CONTROL_PACKET_FLAGS_IGNORE_MAC;
1676         if (filter->flags & RTE_ETHTYPE_FLAGS_DROP)
1677                 flags |= I40E_AQC_ADD_CONTROL_PACKET_FLAGS_DROP;
1678         flags |= I40E_AQC_ADD_CONTROL_PACKET_FLAGS_TO_QUEUE;
1679
1680         memset(&stats, 0, sizeof(stats));
1681         ret = i40e_aq_add_rem_control_packet_filter(hw,
1682                                     filter->input.mac_addr.addr_bytes,
1683                                     filter->input.ether_type,
1684                                     flags, pf->main_vsi->seid,
1685                                     filter->queue, 0, &stats, NULL);
1686         if (ret < 0)
1687                 return ret;
1688
1689         node = i40e_sw_ethertype_filter_lookup(ethertype_rule, &filter->input);
1690         if (!node)
1691                 return -EINVAL;
1692
1693         ret = i40e_sw_ethertype_filter_del(pf, &node->input);
1694
1695         return ret;
1696 }
1697
1698 static int
1699 i40e_flow_destroy_tunnel_filter(struct i40e_pf *pf,
1700                                 struct i40e_tunnel_filter *filter)
1701 {
1702         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
1703         struct i40e_vsi *vsi = pf->main_vsi;
1704         struct i40e_aqc_add_remove_cloud_filters_element_data cld_filter;
1705         struct i40e_tunnel_rule *tunnel_rule = &pf->tunnel;
1706         struct i40e_tunnel_filter *node;
1707         int ret = 0;
1708
1709         memset(&cld_filter, 0, sizeof(cld_filter));
1710         ether_addr_copy((struct ether_addr *)&filter->input.outer_mac,
1711                         (struct ether_addr *)&cld_filter.outer_mac);
1712         ether_addr_copy((struct ether_addr *)&filter->input.inner_mac,
1713                         (struct ether_addr *)&cld_filter.inner_mac);
1714         cld_filter.inner_vlan = filter->input.inner_vlan;
1715         cld_filter.flags = filter->input.flags;
1716         cld_filter.tenant_id = filter->input.tenant_id;
1717         cld_filter.queue_number = filter->queue;
1718
1719         ret = i40e_aq_remove_cloud_filters(hw, vsi->seid,
1720                                            &cld_filter, 1);
1721         if (ret < 0)
1722                 return ret;
1723
1724         node = i40e_sw_tunnel_filter_lookup(tunnel_rule, &filter->input);
1725         if (!node)
1726                 return -EINVAL;
1727
1728         ret = i40e_sw_tunnel_filter_del(pf, &node->input);
1729
1730         return ret;
1731 }
1732
1733 static int
1734 i40e_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1735 {
1736         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1737         int ret;
1738
1739         ret = i40e_flow_flush_fdir_filter(pf);
1740         if (ret) {
1741                 rte_flow_error_set(error, -ret,
1742                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1743                                    "Failed to flush FDIR flows.");
1744                 return -rte_errno;
1745         }
1746
1747         ret = i40e_flow_flush_ethertype_filter(pf);
1748         if (ret) {
1749                 rte_flow_error_set(error, -ret,
1750                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1751                                    "Failed to ethertype flush flows.");
1752                 return -rte_errno;
1753         }
1754
1755         ret = i40e_flow_flush_tunnel_filter(pf);
1756         if (ret) {
1757                 rte_flow_error_set(error, -ret,
1758                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1759                                    "Failed to flush tunnel flows.");
1760                 return -rte_errno;
1761         }
1762
1763         return ret;
1764 }
1765
1766 static int
1767 i40e_flow_flush_fdir_filter(struct i40e_pf *pf)
1768 {
1769         struct rte_eth_dev *dev = pf->adapter->eth_dev;
1770         struct i40e_fdir_info *fdir_info = &pf->fdir;
1771         struct i40e_fdir_filter *fdir_filter;
1772         struct rte_flow *flow;
1773         void *temp;
1774         int ret;
1775
1776         ret = i40e_fdir_flush(dev);
1777         if (!ret) {
1778                 /* Delete FDIR filters in FDIR list. */
1779                 while ((fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list))) {
1780                         ret = i40e_sw_fdir_filter_del(pf,
1781                                                       &fdir_filter->fdir.input);
1782                         if (ret < 0)
1783                                 return ret;
1784                 }
1785
1786                 /* Delete FDIR flows in flow list. */
1787                 TAILQ_FOREACH_SAFE(flow, &pf->flow_list, node, temp) {
1788                         if (flow->filter_type == RTE_ETH_FILTER_FDIR) {
1789                                 TAILQ_REMOVE(&pf->flow_list, flow, node);
1790                                 rte_free(flow);
1791                         }
1792                 }
1793         }
1794
1795         return ret;
1796 }
1797
1798 /* Flush all ethertype filters */
1799 static int
1800 i40e_flow_flush_ethertype_filter(struct i40e_pf *pf)
1801 {
1802         struct i40e_ethertype_filter_list
1803                 *ethertype_list = &pf->ethertype.ethertype_list;
1804         struct i40e_ethertype_filter *filter;
1805         struct rte_flow *flow;
1806         void *temp;
1807         int ret = 0;
1808
1809         while ((filter = TAILQ_FIRST(ethertype_list))) {
1810                 ret = i40e_flow_destroy_ethertype_filter(pf, filter);
1811                 if (ret)
1812                         return ret;
1813         }
1814
1815         /* Delete ethertype flows in flow list. */
1816         TAILQ_FOREACH_SAFE(flow, &pf->flow_list, node, temp) {
1817                 if (flow->filter_type == RTE_ETH_FILTER_ETHERTYPE) {
1818                         TAILQ_REMOVE(&pf->flow_list, flow, node);
1819                         rte_free(flow);
1820                 }
1821         }
1822
1823         return ret;
1824 }
1825
1826 /* Flush all tunnel filters */
1827 static int
1828 i40e_flow_flush_tunnel_filter(struct i40e_pf *pf)
1829 {
1830         struct i40e_tunnel_filter_list
1831                 *tunnel_list = &pf->tunnel.tunnel_list;
1832         struct i40e_tunnel_filter *filter;
1833         struct rte_flow *flow;
1834         void *temp;
1835         int ret = 0;
1836
1837         while ((filter = TAILQ_FIRST(tunnel_list))) {
1838                 ret = i40e_flow_destroy_tunnel_filter(pf, filter);
1839                 if (ret)
1840                         return ret;
1841         }
1842
1843         /* Delete tunnel flows in flow list. */
1844         TAILQ_FOREACH_SAFE(flow, &pf->flow_list, node, temp) {
1845                 if (flow->filter_type == RTE_ETH_FILTER_TUNNEL) {
1846                         TAILQ_REMOVE(&pf->flow_list, flow, node);
1847                         rte_free(flow);
1848                 }
1849         }
1850
1851         return ret;
1852 }