2c572768b43fe7762147235f309e7c71da021708
[dpdk.git] / drivers / net / ice / ice_generic_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4
5 #include <sys/queue.h>
6 #include <stdio.h>
7 #include <errno.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <stdarg.h>
12
13 #include <rte_ether.h>
14 #include <rte_ethdev_driver.h>
15 #include <rte_malloc.h>
16
17 #include "ice_ethdev.h"
18 #include "ice_generic_flow.h"
19 #include "ice_switch_filter.h"
20
21 static int ice_flow_validate(struct rte_eth_dev *dev,
22                 const struct rte_flow_attr *attr,
23                 const struct rte_flow_item pattern[],
24                 const struct rte_flow_action actions[],
25                 struct rte_flow_error *error);
26 static struct rte_flow *ice_flow_create(struct rte_eth_dev *dev,
27                 const struct rte_flow_attr *attr,
28                 const struct rte_flow_item pattern[],
29                 const struct rte_flow_action actions[],
30                 struct rte_flow_error *error);
31 static int ice_flow_destroy(struct rte_eth_dev *dev,
32                 struct rte_flow *flow,
33                 struct rte_flow_error *error);
34 static int ice_flow_flush(struct rte_eth_dev *dev,
35                 struct rte_flow_error *error);
36
37 const struct rte_flow_ops ice_flow_ops = {
38         .validate = ice_flow_validate,
39         .create = ice_flow_create,
40         .destroy = ice_flow_destroy,
41         .flush = ice_flow_flush,
42 };
43
44 static int
45 ice_flow_valid_attr(const struct rte_flow_attr *attr,
46                      struct rte_flow_error *error)
47 {
48         /* Must be input direction */
49         if (!attr->ingress) {
50                 rte_flow_error_set(error, EINVAL,
51                                    RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
52                                    attr, "Only support ingress.");
53                 return -rte_errno;
54         }
55
56         /* Not supported */
57         if (attr->egress) {
58                 rte_flow_error_set(error, EINVAL,
59                                    RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
60                                    attr, "Not support egress.");
61                 return -rte_errno;
62         }
63
64         /* Not supported */
65         if (attr->priority) {
66                 rte_flow_error_set(error, EINVAL,
67                                    RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
68                                    attr, "Not support priority.");
69                 return -rte_errno;
70         }
71
72         /* Not supported */
73         if (attr->group) {
74                 rte_flow_error_set(error, EINVAL,
75                                    RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
76                                    attr, "Not support group.");
77                 return -rte_errno;
78         }
79
80         return 0;
81 }
82
83 /* Find the first VOID or non-VOID item pointer */
84 static const struct rte_flow_item *
85 ice_find_first_item(const struct rte_flow_item *item, bool is_void)
86 {
87         bool is_find;
88
89         while (item->type != RTE_FLOW_ITEM_TYPE_END) {
90                 if (is_void)
91                         is_find = item->type == RTE_FLOW_ITEM_TYPE_VOID;
92                 else
93                         is_find = item->type != RTE_FLOW_ITEM_TYPE_VOID;
94                 if (is_find)
95                         break;
96                 item++;
97         }
98         return item;
99 }
100
101 /* Skip all VOID items of the pattern */
102 static void
103 ice_pattern_skip_void_item(struct rte_flow_item *items,
104                             const struct rte_flow_item *pattern)
105 {
106         uint32_t cpy_count = 0;
107         const struct rte_flow_item *pb = pattern, *pe = pattern;
108
109         for (;;) {
110                 /* Find a non-void item first */
111                 pb = ice_find_first_item(pb, false);
112                 if (pb->type == RTE_FLOW_ITEM_TYPE_END) {
113                         pe = pb;
114                         break;
115                 }
116
117                 /* Find a void item */
118                 pe = ice_find_first_item(pb + 1, true);
119
120                 cpy_count = pe - pb;
121                 rte_memcpy(items, pb, sizeof(struct rte_flow_item) * cpy_count);
122
123                 items += cpy_count;
124
125                 if (pe->type == RTE_FLOW_ITEM_TYPE_END) {
126                         pb = pe;
127                         break;
128                 }
129
130                 pb = pe + 1;
131         }
132         /* Copy the END item. */
133         rte_memcpy(items, pe, sizeof(struct rte_flow_item));
134 }
135
136 /* Check if the pattern matches a supported item type array */
137 static bool
138 ice_match_pattern(enum rte_flow_item_type *item_array,
139                 const struct rte_flow_item *pattern)
140 {
141         const struct rte_flow_item *item = pattern;
142
143         while ((*item_array == item->type) &&
144                (*item_array != RTE_FLOW_ITEM_TYPE_END)) {
145                 item_array++;
146                 item++;
147         }
148
149         return (*item_array == RTE_FLOW_ITEM_TYPE_END &&
150                 item->type == RTE_FLOW_ITEM_TYPE_END);
151 }
152
153 static uint64_t ice_flow_valid_pattern(const struct rte_flow_item pattern[],
154                 struct rte_flow_error *error)
155 {
156         uint16_t i = 0;
157         uint64_t inset;
158         struct rte_flow_item *items; /* used for pattern without VOID items */
159         uint32_t item_num = 0; /* non-void item number */
160
161         /* Get the non-void item number of pattern */
162         while ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_END) {
163                 if ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_VOID)
164                         item_num++;
165                 i++;
166         }
167         item_num++;
168
169         items = rte_zmalloc("ice_pattern",
170                             item_num * sizeof(struct rte_flow_item), 0);
171         if (!items) {
172                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
173                                    NULL, "No memory for PMD internal items.");
174                 return -ENOMEM;
175         }
176
177         ice_pattern_skip_void_item(items, pattern);
178
179         for (i = 0; i < RTE_DIM(ice_supported_patterns); i++)
180                 if (ice_match_pattern(ice_supported_patterns[i].items,
181                                       items)) {
182                         inset = ice_supported_patterns[i].sw_fields;
183                         rte_free(items);
184                         return inset;
185                 }
186         rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
187                            pattern, "Unsupported pattern");
188
189         rte_free(items);
190         return 0;
191 }
192
193 static uint64_t ice_get_flow_field(const struct rte_flow_item pattern[],
194                         struct rte_flow_error *error)
195 {
196         const struct rte_flow_item *item = pattern;
197         const struct rte_flow_item_eth *eth_spec, *eth_mask;
198         const struct rte_flow_item_ipv4 *ipv4_spec, *ipv4_mask;
199         const struct rte_flow_item_ipv6 *ipv6_spec, *ipv6_mask;
200         const struct rte_flow_item_tcp *tcp_spec, *tcp_mask;
201         const struct rte_flow_item_udp *udp_spec, *udp_mask;
202         const struct rte_flow_item_sctp *sctp_spec, *sctp_mask;
203         const struct rte_flow_item_icmp *icmp_mask;
204         const struct rte_flow_item_icmp6 *icmp6_mask;
205         const struct rte_flow_item_vxlan *vxlan_spec, *vxlan_mask;
206         const struct rte_flow_item_nvgre *nvgre_spec, *nvgre_mask;
207         enum rte_flow_item_type item_type;
208         uint8_t  ipv6_addr_mask[16] = {
209                 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
210                 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
211         uint64_t input_set = ICE_INSET_NONE;
212         bool is_tunnel = false;
213
214         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
215                 if (item->last) {
216                         rte_flow_error_set(error, EINVAL,
217                                            RTE_FLOW_ERROR_TYPE_ITEM,
218                                            item,
219                                            "Not support range");
220                         return 0;
221                 }
222                 item_type = item->type;
223                 switch (item_type) {
224                 case RTE_FLOW_ITEM_TYPE_ETH:
225                         eth_spec = item->spec;
226                         eth_mask = item->mask;
227
228                         if (eth_spec && eth_mask) {
229                                 if (rte_is_broadcast_ether_addr(&eth_mask->src))
230                                         input_set |= ICE_INSET_SMAC;
231                                 if (rte_is_broadcast_ether_addr(&eth_mask->dst))
232                                         input_set |= ICE_INSET_DMAC;
233                                 if (eth_mask->type == RTE_BE16(0xffff))
234                                         input_set |= ICE_INSET_ETHERTYPE;
235                         }
236                         break;
237                 case RTE_FLOW_ITEM_TYPE_IPV4:
238                         ipv4_spec = item->spec;
239                         ipv4_mask = item->mask;
240
241                         if (!(ipv4_spec && ipv4_mask))
242                                 break;
243
244                         /* Check IPv4 mask and update input set */
245                         if (ipv4_mask->hdr.version_ihl ||
246                             ipv4_mask->hdr.total_length ||
247                             ipv4_mask->hdr.packet_id ||
248                             ipv4_mask->hdr.hdr_checksum) {
249                                 rte_flow_error_set(error, EINVAL,
250                                            RTE_FLOW_ERROR_TYPE_ITEM,
251                                            item,
252                                            "Invalid IPv4 mask.");
253                                 return 0;
254                         }
255
256                         if (is_tunnel) {
257                                 if (ipv4_mask->hdr.src_addr == UINT32_MAX)
258                                         input_set |= ICE_INSET_TUN_IPV4_SRC;
259                                 if (ipv4_mask->hdr.dst_addr == UINT32_MAX)
260                                         input_set |= ICE_INSET_TUN_IPV4_DST;
261                                 if (ipv4_mask->hdr.time_to_live == UINT8_MAX)
262                                         input_set |= ICE_INSET_TUN_IPV4_TTL;
263                                 if (ipv4_mask->hdr.next_proto_id == UINT8_MAX)
264                                         input_set |= ICE_INSET_TUN_IPV4_PROTO;
265                         } else {
266                                 if (ipv4_mask->hdr.src_addr == UINT32_MAX)
267                                         input_set |= ICE_INSET_IPV4_SRC;
268                                 if (ipv4_mask->hdr.dst_addr == UINT32_MAX)
269                                         input_set |= ICE_INSET_IPV4_DST;
270                                 if (ipv4_mask->hdr.time_to_live == UINT8_MAX)
271                                         input_set |= ICE_INSET_IPV4_TTL;
272                                 if (ipv4_mask->hdr.next_proto_id == UINT8_MAX)
273                                         input_set |= ICE_INSET_IPV4_PROTO;
274                                 if (ipv4_mask->hdr.type_of_service == UINT8_MAX)
275                                         input_set |= ICE_INSET_IPV4_TOS;
276                         }
277                         break;
278                 case RTE_FLOW_ITEM_TYPE_IPV6:
279                         ipv6_spec = item->spec;
280                         ipv6_mask = item->mask;
281
282                         if (!(ipv6_spec && ipv6_mask))
283                                 break;
284
285                         if (ipv6_mask->hdr.payload_len ||
286                             ipv6_mask->hdr.vtc_flow) {
287                                 rte_flow_error_set(error, EINVAL,
288                                            RTE_FLOW_ERROR_TYPE_ITEM,
289                                            item,
290                                            "Invalid IPv6 mask");
291                                 return 0;
292                         }
293
294                         if (is_tunnel) {
295                                 if (!memcmp(ipv6_mask->hdr.src_addr,
296                                             ipv6_addr_mask,
297                                             RTE_DIM(ipv6_mask->hdr.src_addr)))
298                                         input_set |= ICE_INSET_TUN_IPV6_SRC;
299                                 if (!memcmp(ipv6_mask->hdr.dst_addr,
300                                             ipv6_addr_mask,
301                                             RTE_DIM(ipv6_mask->hdr.dst_addr)))
302                                         input_set |= ICE_INSET_TUN_IPV6_DST;
303                                 if (ipv6_mask->hdr.proto == UINT8_MAX)
304                                         input_set |= ICE_INSET_TUN_IPV6_PROTO;
305                                 if (ipv6_mask->hdr.hop_limits == UINT8_MAX)
306                                         input_set |= ICE_INSET_TUN_IPV6_TTL;
307                         } else {
308                                 if (!memcmp(ipv6_mask->hdr.src_addr,
309                                             ipv6_addr_mask,
310                                             RTE_DIM(ipv6_mask->hdr.src_addr)))
311                                         input_set |= ICE_INSET_IPV6_SRC;
312                                 if (!memcmp(ipv6_mask->hdr.dst_addr,
313                                             ipv6_addr_mask,
314                                             RTE_DIM(ipv6_mask->hdr.dst_addr)))
315                                         input_set |= ICE_INSET_IPV6_DST;
316                                 if (ipv6_mask->hdr.proto == UINT8_MAX)
317                                         input_set |= ICE_INSET_IPV6_PROTO;
318                                 if (ipv6_mask->hdr.hop_limits == UINT8_MAX)
319                                         input_set |= ICE_INSET_IPV6_HOP_LIMIT;
320                         }
321
322                         break;
323                 case RTE_FLOW_ITEM_TYPE_UDP:
324                         udp_spec = item->spec;
325                         udp_mask = item->mask;
326
327                         if (!(udp_spec && udp_mask))
328                                 break;
329
330                         /* Check UDP mask and update input set*/
331                         if (udp_mask->hdr.dgram_len ||
332                             udp_mask->hdr.dgram_cksum) {
333                                 rte_flow_error_set(error, EINVAL,
334                                                    RTE_FLOW_ERROR_TYPE_ITEM,
335                                                    item,
336                                                    "Invalid UDP mask");
337                                 return 0;
338                         }
339
340                         if (is_tunnel) {
341                                 if (udp_mask->hdr.src_port == UINT16_MAX)
342                                         input_set |= ICE_INSET_TUN_SRC_PORT;
343                                 if (udp_mask->hdr.dst_port == UINT16_MAX)
344                                         input_set |= ICE_INSET_TUN_DST_PORT;
345                         } else {
346                                 if (udp_mask->hdr.src_port == UINT16_MAX)
347                                         input_set |= ICE_INSET_SRC_PORT;
348                                 if (udp_mask->hdr.dst_port == UINT16_MAX)
349                                         input_set |= ICE_INSET_DST_PORT;
350                         }
351
352                         break;
353                 case RTE_FLOW_ITEM_TYPE_TCP:
354                         tcp_spec = item->spec;
355                         tcp_mask = item->mask;
356
357                         if (!(tcp_spec && tcp_mask))
358                                 break;
359
360                         /* Check TCP mask and update input set */
361                         if (tcp_mask->hdr.sent_seq ||
362                             tcp_mask->hdr.recv_ack ||
363                             tcp_mask->hdr.data_off ||
364                             tcp_mask->hdr.tcp_flags ||
365                             tcp_mask->hdr.rx_win ||
366                             tcp_mask->hdr.cksum ||
367                             tcp_mask->hdr.tcp_urp) {
368                                 rte_flow_error_set(error, EINVAL,
369                                                    RTE_FLOW_ERROR_TYPE_ITEM,
370                                                    item,
371                                                    "Invalid TCP mask");
372                                 return 0;
373                         }
374
375                         if (is_tunnel) {
376                                 if (tcp_mask->hdr.src_port == UINT16_MAX)
377                                         input_set |= ICE_INSET_TUN_SRC_PORT;
378                                 if (tcp_mask->hdr.dst_port == UINT16_MAX)
379                                         input_set |= ICE_INSET_TUN_DST_PORT;
380                         } else {
381                                 if (tcp_mask->hdr.src_port == UINT16_MAX)
382                                         input_set |= ICE_INSET_SRC_PORT;
383                                 if (tcp_mask->hdr.dst_port == UINT16_MAX)
384                                         input_set |= ICE_INSET_DST_PORT;
385                         }
386
387                         break;
388                 case RTE_FLOW_ITEM_TYPE_SCTP:
389                         sctp_spec = item->spec;
390                         sctp_mask = item->mask;
391
392                         if (!(sctp_spec && sctp_mask))
393                                 break;
394
395                         /* Check SCTP mask and update input set */
396                         if (sctp_mask->hdr.cksum) {
397                                 rte_flow_error_set(error, EINVAL,
398                                            RTE_FLOW_ERROR_TYPE_ITEM,
399                                            item,
400                                            "Invalid SCTP mask");
401                                 return 0;
402                         }
403
404                         if (is_tunnel) {
405                                 if (sctp_mask->hdr.src_port == UINT16_MAX)
406                                         input_set |= ICE_INSET_TUN_SRC_PORT;
407                                 if (sctp_mask->hdr.dst_port == UINT16_MAX)
408                                         input_set |= ICE_INSET_TUN_DST_PORT;
409                         } else {
410                                 if (sctp_mask->hdr.src_port == UINT16_MAX)
411                                         input_set |= ICE_INSET_SRC_PORT;
412                                 if (sctp_mask->hdr.dst_port == UINT16_MAX)
413                                         input_set |= ICE_INSET_DST_PORT;
414                         }
415
416                         break;
417                 case RTE_FLOW_ITEM_TYPE_ICMP:
418                         icmp_mask = item->mask;
419                         if (icmp_mask->hdr.icmp_code ||
420                             icmp_mask->hdr.icmp_cksum ||
421                             icmp_mask->hdr.icmp_ident ||
422                             icmp_mask->hdr.icmp_seq_nb) {
423                                 rte_flow_error_set(error, EINVAL,
424                                                    RTE_FLOW_ERROR_TYPE_ITEM,
425                                                    item,
426                                                    "Invalid ICMP mask");
427                                 return 0;
428                         }
429
430                         if (icmp_mask->hdr.icmp_type == UINT8_MAX)
431                                 input_set |= ICE_INSET_ICMP;
432                         break;
433                 case RTE_FLOW_ITEM_TYPE_ICMP6:
434                         icmp6_mask = item->mask;
435                         if (icmp6_mask->code ||
436                             icmp6_mask->checksum) {
437                                 rte_flow_error_set(error, EINVAL,
438                                                    RTE_FLOW_ERROR_TYPE_ITEM,
439                                                    item,
440                                                    "Invalid ICMP6 mask");
441                                 return 0;
442                         }
443
444                         if (icmp6_mask->type == UINT8_MAX)
445                                 input_set |= ICE_INSET_ICMP6;
446                         break;
447                 case RTE_FLOW_ITEM_TYPE_VXLAN:
448                         vxlan_spec = item->spec;
449                         vxlan_mask = item->mask;
450                         /* Check if VXLAN item is used to describe protocol.
451                          * If yes, both spec and mask should be NULL.
452                          * If no, both spec and mask shouldn't be NULL.
453                          */
454                         if ((!vxlan_spec && vxlan_mask) ||
455                             (vxlan_spec && !vxlan_mask)) {
456                                 rte_flow_error_set(error, EINVAL,
457                                            RTE_FLOW_ERROR_TYPE_ITEM,
458                                            item,
459                                            "Invalid VXLAN item");
460                                 return 0;
461                         }
462                         is_tunnel = 1;
463
464                         break;
465                 case RTE_FLOW_ITEM_TYPE_NVGRE:
466                         nvgre_spec = item->spec;
467                         nvgre_mask = item->mask;
468                         /* Check if NVGRE item is used to describe protocol.
469                          * If yes, both spec and mask should be NULL.
470                          * If no, both spec and mask shouldn't be NULL.
471                          */
472                         if ((!nvgre_spec && nvgre_mask) ||
473                             (nvgre_spec && !nvgre_mask)) {
474                                 rte_flow_error_set(error, EINVAL,
475                                            RTE_FLOW_ERROR_TYPE_ITEM,
476                                            item,
477                                            "Invalid NVGRE item");
478                                 return 0;
479                         }
480                         is_tunnel = 1;
481
482                         break;
483                 case RTE_FLOW_ITEM_TYPE_VOID:
484                         break;
485                 default:
486                         rte_flow_error_set(error, EINVAL,
487                                            RTE_FLOW_ERROR_TYPE_ITEM,
488                                            item,
489                                            "Invalid mask no exist");
490                         break;
491                 }
492         }
493         return input_set;
494 }
495
496 static int ice_flow_valid_inset(const struct rte_flow_item pattern[],
497                         uint64_t inset, struct rte_flow_error *error)
498 {
499         uint64_t fields;
500
501         /* get valid field */
502         fields = ice_get_flow_field(pattern, error);
503         if (!fields || fields & (~inset)) {
504                 rte_flow_error_set(error, EINVAL,
505                                    RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
506                                    pattern,
507                                    "Invalid input set");
508                 return -rte_errno;
509         }
510
511         return 0;
512 }
513
514 static int ice_flow_valid_action(struct rte_eth_dev *dev,
515                                 const struct rte_flow_action *actions,
516                                 struct rte_flow_error *error)
517 {
518         const struct rte_flow_action_queue *act_q;
519         uint16_t queue;
520         const struct rte_flow_action *action;
521         for (action = actions; action->type !=
522                         RTE_FLOW_ACTION_TYPE_END; action++) {
523                 switch (action->type) {
524                 case RTE_FLOW_ACTION_TYPE_QUEUE:
525                         act_q = action->conf;
526                         queue = act_q->index;
527                         if (queue >= dev->data->nb_rx_queues) {
528                                 rte_flow_error_set(error, EINVAL,
529                                                 RTE_FLOW_ERROR_TYPE_ACTION,
530                                                 actions, "Invalid queue ID for"
531                                                 " switch filter.");
532                                 return -rte_errno;
533                         }
534                         break;
535                 case RTE_FLOW_ACTION_TYPE_DROP:
536                 case RTE_FLOW_ACTION_TYPE_VOID:
537                         break;
538                 default:
539                         rte_flow_error_set(error, EINVAL,
540                                            RTE_FLOW_ERROR_TYPE_ACTION, actions,
541                                            "Invalid action.");
542                         return -rte_errno;
543                 }
544         }
545         return 0;
546 }
547
548 static int
549 ice_flow_validate(struct rte_eth_dev *dev,
550                    const struct rte_flow_attr *attr,
551                    const struct rte_flow_item pattern[],
552                    const struct rte_flow_action actions[],
553                    struct rte_flow_error *error)
554 {
555         uint64_t inset = 0;
556         int ret = ICE_ERR_NOT_SUPPORTED;
557
558         if (!pattern) {
559                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
560                                    NULL, "NULL pattern.");
561                 return -rte_errno;
562         }
563
564         if (!actions) {
565                 rte_flow_error_set(error, EINVAL,
566                                    RTE_FLOW_ERROR_TYPE_ACTION_NUM,
567                                    NULL, "NULL action.");
568                 return -rte_errno;
569         }
570
571         if (!attr) {
572                 rte_flow_error_set(error, EINVAL,
573                                    RTE_FLOW_ERROR_TYPE_ATTR,
574                                    NULL, "NULL attribute.");
575                 return -rte_errno;
576         }
577
578         ret = ice_flow_valid_attr(attr, error);
579         if (ret)
580                 return ret;
581
582         inset = ice_flow_valid_pattern(pattern, error);
583         if (!inset)
584                 return -rte_errno;
585
586         ret = ice_flow_valid_inset(pattern, inset, error);
587         if (ret)
588                 return ret;
589
590         ret = ice_flow_valid_action(dev, actions, error);
591         if (ret)
592                 return ret;
593
594         return 0;
595 }
596
597 static struct rte_flow *
598 ice_flow_create(struct rte_eth_dev *dev,
599                  const struct rte_flow_attr *attr,
600                  const struct rte_flow_item pattern[],
601                  const struct rte_flow_action actions[],
602                  struct rte_flow_error *error)
603 {
604         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
605         struct rte_flow *flow = NULL;
606         int ret;
607
608         flow = rte_zmalloc("ice_flow", sizeof(struct rte_flow), 0);
609         if (!flow) {
610                 rte_flow_error_set(error, ENOMEM,
611                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
612                                    "Failed to allocate memory");
613                 return flow;
614         }
615
616         ret = ice_flow_validate(dev, attr, pattern, actions, error);
617         if (ret < 0)
618                 goto free_flow;
619
620         ret = ice_create_switch_filter(pf, pattern, actions, flow, error);
621         if (ret)
622                 goto free_flow;
623
624         TAILQ_INSERT_TAIL(&pf->flow_list, flow, node);
625         return flow;
626
627 free_flow:
628         rte_flow_error_set(error, -ret,
629                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
630                            "Failed to create flow.");
631         rte_free(flow);
632         return NULL;
633 }
634
635 static int
636 ice_flow_destroy(struct rte_eth_dev *dev,
637                  struct rte_flow *flow,
638                  struct rte_flow_error *error)
639 {
640         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
641         int ret = 0;
642
643         ret = ice_destroy_switch_filter(pf, flow, error);
644
645         if (!ret) {
646                 TAILQ_REMOVE(&pf->flow_list, flow, node);
647                 rte_free(flow);
648         } else {
649                 rte_flow_error_set(error, -ret,
650                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
651                                    "Failed to destroy flow.");
652         }
653
654         return ret;
655 }
656
657 static int
658 ice_flow_flush(struct rte_eth_dev *dev,
659                struct rte_flow_error *error)
660 {
661         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
662         struct rte_flow *p_flow;
663         int ret = 0;
664
665         TAILQ_FOREACH(p_flow, &pf->flow_list, node) {
666                 ret = ice_flow_destroy(dev, p_flow, error);
667                 if (ret) {
668                         rte_flow_error_set(error, -ret,
669                                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
670                                            "Failed to flush SW flows.");
671                         return -rte_errno;
672                 }
673         }
674
675         return ret;
676 }