net/enic: support drop flow action
[dpdk.git] / drivers / net / enic / enic_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2008-2017 Cisco Systems, Inc.  All rights reserved.
3  */
4
5 #include <errno.h>
6 #include <rte_log.h>
7 #include <rte_ethdev_driver.h>
8 #include <rte_flow_driver.h>
9 #include <rte_ether.h>
10 #include <rte_ip.h>
11 #include <rte_udp.h>
12
13 #include "enic_compat.h"
14 #include "enic.h"
15 #include "vnic_dev.h"
16 #include "vnic_nic.h"
17
18 #define FLOW_TRACE() \
19         rte_log(RTE_LOG_DEBUG, enicpmd_logtype_flow, \
20                 "%s()\n", __func__)
21 #define FLOW_LOG(level, fmt, args...) \
22         rte_log(RTE_LOG_ ## level, enicpmd_logtype_flow, \
23                 fmt "\n", ##args)
24
25 /** Info about how to copy items into enic filters. */
26 struct enic_items {
27         /** Function for copying and validating an item. */
28         int (*copy_item)(const struct rte_flow_item *item,
29                          struct filter_v2 *enic_filter, u8 *inner_ofst);
30         /** List of valid previous items. */
31         const enum rte_flow_item_type * const prev_items;
32         /** True if it's OK for this item to be the first item. For some NIC
33          * versions, it's invalid to start the stack above layer 3.
34          */
35         const u8 valid_start_item;
36 };
37
38 /** Filtering capabilities for various NIC and firmware versions. */
39 struct enic_filter_cap {
40         /** list of valid items and their handlers and attributes. */
41         const struct enic_items *item_info;
42 };
43
44 /* functions for copying flow actions into enic actions */
45 typedef int (copy_action_fn)(const struct rte_flow_action actions[],
46                              struct filter_action_v2 *enic_action);
47
48 /* functions for copying items into enic filters */
49 typedef int(enic_copy_item_fn)(const struct rte_flow_item *item,
50                           struct filter_v2 *enic_filter, u8 *inner_ofst);
51
52 /** Action capabilities for various NICs. */
53 struct enic_action_cap {
54         /** list of valid actions */
55         const enum rte_flow_action_type *actions;
56         /** copy function for a particular NIC */
57         int (*copy_fn)(const struct rte_flow_action actions[],
58                        struct filter_action_v2 *enic_action);
59 };
60
61 /* Forward declarations */
62 static enic_copy_item_fn enic_copy_item_ipv4_v1;
63 static enic_copy_item_fn enic_copy_item_udp_v1;
64 static enic_copy_item_fn enic_copy_item_tcp_v1;
65 static enic_copy_item_fn enic_copy_item_eth_v2;
66 static enic_copy_item_fn enic_copy_item_vlan_v2;
67 static enic_copy_item_fn enic_copy_item_ipv4_v2;
68 static enic_copy_item_fn enic_copy_item_ipv6_v2;
69 static enic_copy_item_fn enic_copy_item_udp_v2;
70 static enic_copy_item_fn enic_copy_item_tcp_v2;
71 static enic_copy_item_fn enic_copy_item_sctp_v2;
72 static enic_copy_item_fn enic_copy_item_sctp_v2;
73 static enic_copy_item_fn enic_copy_item_vxlan_v2;
74 static copy_action_fn enic_copy_action_v1;
75 static copy_action_fn enic_copy_action_v2;
76
77 /**
78  * Legacy NICs or NICs with outdated firmware. Only 5-tuple perfect match
79  * is supported.
80  */
81 static const struct enic_items enic_items_v1[] = {
82         [RTE_FLOW_ITEM_TYPE_IPV4] = {
83                 .copy_item = enic_copy_item_ipv4_v1,
84                 .valid_start_item = 1,
85                 .prev_items = (const enum rte_flow_item_type[]) {
86                                RTE_FLOW_ITEM_TYPE_END,
87                 },
88         },
89         [RTE_FLOW_ITEM_TYPE_UDP] = {
90                 .copy_item = enic_copy_item_udp_v1,
91                 .valid_start_item = 0,
92                 .prev_items = (const enum rte_flow_item_type[]) {
93                                RTE_FLOW_ITEM_TYPE_IPV4,
94                                RTE_FLOW_ITEM_TYPE_END,
95                 },
96         },
97         [RTE_FLOW_ITEM_TYPE_TCP] = {
98                 .copy_item = enic_copy_item_tcp_v1,
99                 .valid_start_item = 0,
100                 .prev_items = (const enum rte_flow_item_type[]) {
101                                RTE_FLOW_ITEM_TYPE_IPV4,
102                                RTE_FLOW_ITEM_TYPE_END,
103                 },
104         },
105 };
106
107 /**
108  * NICs have Advanced Filters capability but they are disabled. This means
109  * that layer 3 must be specified.
110  */
111 static const struct enic_items enic_items_v2[] = {
112         [RTE_FLOW_ITEM_TYPE_ETH] = {
113                 .copy_item = enic_copy_item_eth_v2,
114                 .valid_start_item = 1,
115                 .prev_items = (const enum rte_flow_item_type[]) {
116                                RTE_FLOW_ITEM_TYPE_VXLAN,
117                                RTE_FLOW_ITEM_TYPE_END,
118                 },
119         },
120         [RTE_FLOW_ITEM_TYPE_VLAN] = {
121                 .copy_item = enic_copy_item_vlan_v2,
122                 .valid_start_item = 1,
123                 .prev_items = (const enum rte_flow_item_type[]) {
124                                RTE_FLOW_ITEM_TYPE_ETH,
125                                RTE_FLOW_ITEM_TYPE_END,
126                 },
127         },
128         [RTE_FLOW_ITEM_TYPE_IPV4] = {
129                 .copy_item = enic_copy_item_ipv4_v2,
130                 .valid_start_item = 1,
131                 .prev_items = (const enum rte_flow_item_type[]) {
132                                RTE_FLOW_ITEM_TYPE_ETH,
133                                RTE_FLOW_ITEM_TYPE_VLAN,
134                                RTE_FLOW_ITEM_TYPE_END,
135                 },
136         },
137         [RTE_FLOW_ITEM_TYPE_IPV6] = {
138                 .copy_item = enic_copy_item_ipv6_v2,
139                 .valid_start_item = 1,
140                 .prev_items = (const enum rte_flow_item_type[]) {
141                                RTE_FLOW_ITEM_TYPE_ETH,
142                                RTE_FLOW_ITEM_TYPE_VLAN,
143                                RTE_FLOW_ITEM_TYPE_END,
144                 },
145         },
146         [RTE_FLOW_ITEM_TYPE_UDP] = {
147                 .copy_item = enic_copy_item_udp_v2,
148                 .valid_start_item = 0,
149                 .prev_items = (const enum rte_flow_item_type[]) {
150                                RTE_FLOW_ITEM_TYPE_IPV4,
151                                RTE_FLOW_ITEM_TYPE_IPV6,
152                                RTE_FLOW_ITEM_TYPE_END,
153                 },
154         },
155         [RTE_FLOW_ITEM_TYPE_TCP] = {
156                 .copy_item = enic_copy_item_tcp_v2,
157                 .valid_start_item = 0,
158                 .prev_items = (const enum rte_flow_item_type[]) {
159                                RTE_FLOW_ITEM_TYPE_IPV4,
160                                RTE_FLOW_ITEM_TYPE_IPV6,
161                                RTE_FLOW_ITEM_TYPE_END,
162                 },
163         },
164         [RTE_FLOW_ITEM_TYPE_SCTP] = {
165                 .copy_item = enic_copy_item_sctp_v2,
166                 .valid_start_item = 0,
167                 .prev_items = (const enum rte_flow_item_type[]) {
168                                RTE_FLOW_ITEM_TYPE_IPV4,
169                                RTE_FLOW_ITEM_TYPE_IPV6,
170                                RTE_FLOW_ITEM_TYPE_END,
171                 },
172         },
173         [RTE_FLOW_ITEM_TYPE_VXLAN] = {
174                 .copy_item = enic_copy_item_vxlan_v2,
175                 .valid_start_item = 0,
176                 .prev_items = (const enum rte_flow_item_type[]) {
177                                RTE_FLOW_ITEM_TYPE_UDP,
178                                RTE_FLOW_ITEM_TYPE_END,
179                 },
180         },
181 };
182
183 /** NICs with Advanced filters enabled */
184 static const struct enic_items enic_items_v3[] = {
185         [RTE_FLOW_ITEM_TYPE_ETH] = {
186                 .copy_item = enic_copy_item_eth_v2,
187                 .valid_start_item = 1,
188                 .prev_items = (const enum rte_flow_item_type[]) {
189                                RTE_FLOW_ITEM_TYPE_VXLAN,
190                                RTE_FLOW_ITEM_TYPE_END,
191                 },
192         },
193         [RTE_FLOW_ITEM_TYPE_VLAN] = {
194                 .copy_item = enic_copy_item_vlan_v2,
195                 .valid_start_item = 1,
196                 .prev_items = (const enum rte_flow_item_type[]) {
197                                RTE_FLOW_ITEM_TYPE_ETH,
198                                RTE_FLOW_ITEM_TYPE_END,
199                 },
200         },
201         [RTE_FLOW_ITEM_TYPE_IPV4] = {
202                 .copy_item = enic_copy_item_ipv4_v2,
203                 .valid_start_item = 1,
204                 .prev_items = (const enum rte_flow_item_type[]) {
205                                RTE_FLOW_ITEM_TYPE_ETH,
206                                RTE_FLOW_ITEM_TYPE_VLAN,
207                                RTE_FLOW_ITEM_TYPE_END,
208                 },
209         },
210         [RTE_FLOW_ITEM_TYPE_IPV6] = {
211                 .copy_item = enic_copy_item_ipv6_v2,
212                 .valid_start_item = 1,
213                 .prev_items = (const enum rte_flow_item_type[]) {
214                                RTE_FLOW_ITEM_TYPE_ETH,
215                                RTE_FLOW_ITEM_TYPE_VLAN,
216                                RTE_FLOW_ITEM_TYPE_END,
217                 },
218         },
219         [RTE_FLOW_ITEM_TYPE_UDP] = {
220                 .copy_item = enic_copy_item_udp_v2,
221                 .valid_start_item = 1,
222                 .prev_items = (const enum rte_flow_item_type[]) {
223                                RTE_FLOW_ITEM_TYPE_IPV4,
224                                RTE_FLOW_ITEM_TYPE_IPV6,
225                                RTE_FLOW_ITEM_TYPE_END,
226                 },
227         },
228         [RTE_FLOW_ITEM_TYPE_TCP] = {
229                 .copy_item = enic_copy_item_tcp_v2,
230                 .valid_start_item = 1,
231                 .prev_items = (const enum rte_flow_item_type[]) {
232                                RTE_FLOW_ITEM_TYPE_IPV4,
233                                RTE_FLOW_ITEM_TYPE_IPV6,
234                                RTE_FLOW_ITEM_TYPE_END,
235                 },
236         },
237         [RTE_FLOW_ITEM_TYPE_SCTP] = {
238                 .copy_item = enic_copy_item_sctp_v2,
239                 .valid_start_item = 1,
240                 .prev_items = (const enum rte_flow_item_type[]) {
241                                RTE_FLOW_ITEM_TYPE_IPV4,
242                                RTE_FLOW_ITEM_TYPE_IPV6,
243                                RTE_FLOW_ITEM_TYPE_END,
244                 },
245         },
246         [RTE_FLOW_ITEM_TYPE_VXLAN] = {
247                 .copy_item = enic_copy_item_vxlan_v2,
248                 .valid_start_item = 1,
249                 .prev_items = (const enum rte_flow_item_type[]) {
250                                RTE_FLOW_ITEM_TYPE_UDP,
251                                RTE_FLOW_ITEM_TYPE_END,
252                 },
253         },
254 };
255
256 /** Filtering capabilities indexed this NICs supported filter type. */
257 static const struct enic_filter_cap enic_filter_cap[] = {
258         [FILTER_IPV4_5TUPLE] = {
259                 .item_info = enic_items_v1,
260         },
261         [FILTER_USNIC_IP] = {
262                 .item_info = enic_items_v2,
263         },
264         [FILTER_DPDK_1] = {
265                 .item_info = enic_items_v3,
266         },
267 };
268
269 /** Supported actions for older NICs */
270 static const enum rte_flow_action_type enic_supported_actions_v1[] = {
271         RTE_FLOW_ACTION_TYPE_QUEUE,
272         RTE_FLOW_ACTION_TYPE_END,
273 };
274
275 /** Supported actions for newer NICs */
276 static const enum rte_flow_action_type enic_supported_actions_v2_id[] = {
277         RTE_FLOW_ACTION_TYPE_QUEUE,
278         RTE_FLOW_ACTION_TYPE_MARK,
279         RTE_FLOW_ACTION_TYPE_FLAG,
280         RTE_FLOW_ACTION_TYPE_END,
281 };
282
283 static const enum rte_flow_action_type enic_supported_actions_v2_drop[] = {
284         RTE_FLOW_ACTION_TYPE_QUEUE,
285         RTE_FLOW_ACTION_TYPE_MARK,
286         RTE_FLOW_ACTION_TYPE_FLAG,
287         RTE_FLOW_ACTION_TYPE_DROP,
288         RTE_FLOW_ACTION_TYPE_END,
289 };
290
291 /** Action capabilities indexed by NIC version information */
292 static const struct enic_action_cap enic_action_cap[] = {
293         [FILTER_ACTION_RQ_STEERING_FLAG] = {
294                 .actions = enic_supported_actions_v1,
295                 .copy_fn = enic_copy_action_v1,
296         },
297         [FILTER_ACTION_FILTER_ID_FLAG] = {
298                 .actions = enic_supported_actions_v2_id,
299                 .copy_fn = enic_copy_action_v2,
300         },
301         [FILTER_ACTION_DROP_FLAG] = {
302                 .actions = enic_supported_actions_v2_drop,
303                 .copy_fn = enic_copy_action_v2,
304         },
305 };
306
307 static int
308 mask_exact_match(const u8 *supported, const u8 *supplied,
309                  unsigned int size)
310 {
311         unsigned int i;
312         for (i = 0; i < size; i++) {
313                 if (supported[i] != supplied[i])
314                         return 0;
315         }
316         return 1;
317 }
318
319 /**
320  * Copy IPv4 item into version 1 NIC filter.
321  *
322  * @param item[in]
323  *   Item specification.
324  * @param enic_filter[out]
325  *   Partially filled in NIC filter structure.
326  * @param inner_ofst[in]
327  *   Should always be 0 for version 1.
328  */
329 static int
330 enic_copy_item_ipv4_v1(const struct rte_flow_item *item,
331                        struct filter_v2 *enic_filter, u8 *inner_ofst)
332 {
333         const struct rte_flow_item_ipv4 *spec = item->spec;
334         const struct rte_flow_item_ipv4 *mask = item->mask;
335         struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
336         struct ipv4_hdr supported_mask = {
337                 .src_addr = 0xffffffff,
338                 .dst_addr = 0xffffffff,
339         };
340
341         FLOW_TRACE();
342
343         if (*inner_ofst)
344                 return ENOTSUP;
345
346         if (!mask)
347                 mask = &rte_flow_item_ipv4_mask;
348
349         /* This is an exact match filter, both fields must be set */
350         if (!spec || !spec->hdr.src_addr || !spec->hdr.dst_addr) {
351                 FLOW_LOG(ERR, "IPv4 exact match src/dst addr");
352                 return ENOTSUP;
353         }
354
355         /* check that the suppied mask exactly matches capabilty */
356         if (!mask_exact_match((const u8 *)&supported_mask,
357                               (const u8 *)item->mask, sizeof(*mask))) {
358                 FLOW_LOG(ERR, "IPv4 exact match mask");
359                 return ENOTSUP;
360         }
361
362         enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
363         enic_5tup->src_addr = spec->hdr.src_addr;
364         enic_5tup->dst_addr = spec->hdr.dst_addr;
365
366         return 0;
367 }
368
369 /**
370  * Copy UDP item into version 1 NIC filter.
371  *
372  * @param item[in]
373  *   Item specification.
374  * @param enic_filter[out]
375  *   Partially filled in NIC filter structure.
376  * @param inner_ofst[in]
377  *   Should always be 0 for version 1.
378  */
379 static int
380 enic_copy_item_udp_v1(const struct rte_flow_item *item,
381                       struct filter_v2 *enic_filter, u8 *inner_ofst)
382 {
383         const struct rte_flow_item_udp *spec = item->spec;
384         const struct rte_flow_item_udp *mask = item->mask;
385         struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
386         struct udp_hdr supported_mask = {
387                 .src_port = 0xffff,
388                 .dst_port = 0xffff,
389         };
390
391         FLOW_TRACE();
392
393         if (*inner_ofst)
394                 return ENOTSUP;
395
396         if (!mask)
397                 mask = &rte_flow_item_udp_mask;
398
399         /* This is an exact match filter, both ports must be set */
400         if (!spec || !spec->hdr.src_port || !spec->hdr.dst_port) {
401                 FLOW_LOG(ERR, "UDP exact match src/dst addr");
402                 return ENOTSUP;
403         }
404
405         /* check that the suppied mask exactly matches capabilty */
406         if (!mask_exact_match((const u8 *)&supported_mask,
407                               (const u8 *)item->mask, sizeof(*mask))) {
408                 FLOW_LOG(ERR, "UDP exact match mask");
409                 return ENOTSUP;
410         }
411
412         enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
413         enic_5tup->src_port = spec->hdr.src_port;
414         enic_5tup->dst_port = spec->hdr.dst_port;
415         enic_5tup->protocol = PROTO_UDP;
416
417         return 0;
418 }
419
420 /**
421  * Copy TCP item into version 1 NIC filter.
422  *
423  * @param item[in]
424  *   Item specification.
425  * @param enic_filter[out]
426  *   Partially filled in NIC filter structure.
427  * @param inner_ofst[in]
428  *   Should always be 0 for version 1.
429  */
430 static int
431 enic_copy_item_tcp_v1(const struct rte_flow_item *item,
432                       struct filter_v2 *enic_filter, u8 *inner_ofst)
433 {
434         const struct rte_flow_item_tcp *spec = item->spec;
435         const struct rte_flow_item_tcp *mask = item->mask;
436         struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
437         struct tcp_hdr supported_mask = {
438                 .src_port = 0xffff,
439                 .dst_port = 0xffff,
440         };
441
442         FLOW_TRACE();
443
444         if (*inner_ofst)
445                 return ENOTSUP;
446
447         if (!mask)
448                 mask = &rte_flow_item_tcp_mask;
449
450         /* This is an exact match filter, both ports must be set */
451         if (!spec || !spec->hdr.src_port || !spec->hdr.dst_port) {
452                 FLOW_LOG(ERR, "TCPIPv4 exact match src/dst addr");
453                 return ENOTSUP;
454         }
455
456         /* check that the suppied mask exactly matches capabilty */
457         if (!mask_exact_match((const u8 *)&supported_mask,
458                              (const u8 *)item->mask, sizeof(*mask))) {
459                 FLOW_LOG(ERR, "TCP exact match mask");
460                 return ENOTSUP;
461         }
462
463         enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
464         enic_5tup->src_port = spec->hdr.src_port;
465         enic_5tup->dst_port = spec->hdr.dst_port;
466         enic_5tup->protocol = PROTO_TCP;
467
468         return 0;
469 }
470
471 /**
472  * Copy ETH item into version 2 NIC filter.
473  *
474  * @param item[in]
475  *   Item specification.
476  * @param enic_filter[out]
477  *   Partially filled in NIC filter structure.
478  * @param inner_ofst[in]
479  *   If zero, this is an outer header. If non-zero, this is the offset into L5
480  *   where the header begins.
481  */
482 static int
483 enic_copy_item_eth_v2(const struct rte_flow_item *item,
484                       struct filter_v2 *enic_filter, u8 *inner_ofst)
485 {
486         struct ether_hdr enic_spec;
487         struct ether_hdr enic_mask;
488         const struct rte_flow_item_eth *spec = item->spec;
489         const struct rte_flow_item_eth *mask = item->mask;
490         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
491
492         FLOW_TRACE();
493
494         /* Match all if no spec */
495         if (!spec)
496                 return 0;
497
498         if (!mask)
499                 mask = &rte_flow_item_eth_mask;
500
501         memcpy(enic_spec.d_addr.addr_bytes, spec->dst.addr_bytes,
502                ETHER_ADDR_LEN);
503         memcpy(enic_spec.s_addr.addr_bytes, spec->src.addr_bytes,
504                ETHER_ADDR_LEN);
505
506         memcpy(enic_mask.d_addr.addr_bytes, mask->dst.addr_bytes,
507                ETHER_ADDR_LEN);
508         memcpy(enic_mask.s_addr.addr_bytes, mask->src.addr_bytes,
509                ETHER_ADDR_LEN);
510         enic_spec.ether_type = spec->type;
511         enic_mask.ether_type = mask->type;
512
513         if (*inner_ofst == 0) {
514                 /* outer header */
515                 memcpy(gp->layer[FILTER_GENERIC_1_L2].mask, &enic_mask,
516                        sizeof(struct ether_hdr));
517                 memcpy(gp->layer[FILTER_GENERIC_1_L2].val, &enic_spec,
518                        sizeof(struct ether_hdr));
519         } else {
520                 /* inner header */
521                 if ((*inner_ofst + sizeof(struct ether_hdr)) >
522                      FILTER_GENERIC_1_KEY_LEN)
523                         return ENOTSUP;
524                 /* Offset into L5 where inner Ethernet header goes */
525                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
526                        &enic_mask, sizeof(struct ether_hdr));
527                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
528                        &enic_spec, sizeof(struct ether_hdr));
529                 *inner_ofst += sizeof(struct ether_hdr);
530         }
531         return 0;
532 }
533
534 /**
535  * Copy VLAN item into version 2 NIC filter.
536  *
537  * @param item[in]
538  *   Item specification.
539  * @param enic_filter[out]
540  *   Partially filled in NIC filter structure.
541  * @param inner_ofst[in]
542  *   If zero, this is an outer header. If non-zero, this is the offset into L5
543  *   where the header begins.
544  */
545 static int
546 enic_copy_item_vlan_v2(const struct rte_flow_item *item,
547                        struct filter_v2 *enic_filter, u8 *inner_ofst)
548 {
549         const struct rte_flow_item_vlan *spec = item->spec;
550         const struct rte_flow_item_vlan *mask = item->mask;
551         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
552
553         FLOW_TRACE();
554
555         /* Match all if no spec */
556         if (!spec)
557                 return 0;
558
559         /* Don't support filtering in tpid */
560         if (mask) {
561                 if (mask->tpid != 0)
562                         return ENOTSUP;
563         } else {
564                 mask = &rte_flow_item_vlan_mask;
565                 RTE_ASSERT(mask->tpid == 0);
566         }
567
568         if (*inner_ofst == 0) {
569                 /* Outer header. Use the vlan mask/val fields */
570                 gp->mask_vlan = mask->tci;
571                 gp->val_vlan = spec->tci;
572         } else {
573                 /* Inner header. Mask/Val start at *inner_ofst into L5 */
574                 if ((*inner_ofst + sizeof(struct vlan_hdr)) >
575                      FILTER_GENERIC_1_KEY_LEN)
576                         return ENOTSUP;
577                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
578                        mask, sizeof(struct vlan_hdr));
579                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
580                        spec, sizeof(struct vlan_hdr));
581                 *inner_ofst += sizeof(struct vlan_hdr);
582         }
583         return 0;
584 }
585
586 /**
587  * Copy IPv4 item into version 2 NIC filter.
588  *
589  * @param item[in]
590  *   Item specification.
591  * @param enic_filter[out]
592  *   Partially filled in NIC filter structure.
593  * @param inner_ofst[in]
594  *   Must be 0. Don't support inner IPv4 filtering.
595  */
596 static int
597 enic_copy_item_ipv4_v2(const struct rte_flow_item *item,
598                        struct filter_v2 *enic_filter, u8 *inner_ofst)
599 {
600         const struct rte_flow_item_ipv4 *spec = item->spec;
601         const struct rte_flow_item_ipv4 *mask = item->mask;
602         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
603
604         FLOW_TRACE();
605
606         if (*inner_ofst == 0) {
607                 /* Match IPv4 */
608                 gp->mask_flags |= FILTER_GENERIC_1_IPV4;
609                 gp->val_flags |= FILTER_GENERIC_1_IPV4;
610
611                 /* Match all if no spec */
612                 if (!spec)
613                         return 0;
614
615                 if (!mask)
616                         mask = &rte_flow_item_ipv4_mask;
617
618                 memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
619                        sizeof(struct ipv4_hdr));
620                 memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
621                        sizeof(struct ipv4_hdr));
622         } else {
623                 /* Inner IPv4 header. Mask/Val start at *inner_ofst into L5 */
624                 if ((*inner_ofst + sizeof(struct ipv4_hdr)) >
625                      FILTER_GENERIC_1_KEY_LEN)
626                         return ENOTSUP;
627                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
628                        mask, sizeof(struct ipv4_hdr));
629                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
630                        spec, sizeof(struct ipv4_hdr));
631                 *inner_ofst += sizeof(struct ipv4_hdr);
632         }
633         return 0;
634 }
635
636 /**
637  * Copy IPv6 item into version 2 NIC filter.
638  *
639  * @param item[in]
640  *   Item specification.
641  * @param enic_filter[out]
642  *   Partially filled in NIC filter structure.
643  * @param inner_ofst[in]
644  *   Must be 0. Don't support inner IPv6 filtering.
645  */
646 static int
647 enic_copy_item_ipv6_v2(const struct rte_flow_item *item,
648                        struct filter_v2 *enic_filter, u8 *inner_ofst)
649 {
650         const struct rte_flow_item_ipv6 *spec = item->spec;
651         const struct rte_flow_item_ipv6 *mask = item->mask;
652         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
653
654         FLOW_TRACE();
655
656         /* Match IPv6 */
657         gp->mask_flags |= FILTER_GENERIC_1_IPV6;
658         gp->val_flags |= FILTER_GENERIC_1_IPV6;
659
660         /* Match all if no spec */
661         if (!spec)
662                 return 0;
663
664         if (!mask)
665                 mask = &rte_flow_item_ipv6_mask;
666
667         if (*inner_ofst == 0) {
668                 memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
669                        sizeof(struct ipv6_hdr));
670                 memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
671                        sizeof(struct ipv6_hdr));
672         } else {
673                 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
674                 if ((*inner_ofst + sizeof(struct ipv6_hdr)) >
675                      FILTER_GENERIC_1_KEY_LEN)
676                         return ENOTSUP;
677                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
678                        mask, sizeof(struct ipv6_hdr));
679                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
680                        spec, sizeof(struct ipv6_hdr));
681                 *inner_ofst += sizeof(struct ipv6_hdr);
682         }
683         return 0;
684 }
685
686 /**
687  * Copy UDP item into version 2 NIC filter.
688  *
689  * @param item[in]
690  *   Item specification.
691  * @param enic_filter[out]
692  *   Partially filled in NIC filter structure.
693  * @param inner_ofst[in]
694  *   Must be 0. Don't support inner UDP filtering.
695  */
696 static int
697 enic_copy_item_udp_v2(const struct rte_flow_item *item,
698                       struct filter_v2 *enic_filter, u8 *inner_ofst)
699 {
700         const struct rte_flow_item_udp *spec = item->spec;
701         const struct rte_flow_item_udp *mask = item->mask;
702         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
703
704         FLOW_TRACE();
705
706         /* Match UDP */
707         gp->mask_flags |= FILTER_GENERIC_1_UDP;
708         gp->val_flags |= FILTER_GENERIC_1_UDP;
709
710         /* Match all if no spec */
711         if (!spec)
712                 return 0;
713
714         if (!mask)
715                 mask = &rte_flow_item_udp_mask;
716
717         if (*inner_ofst == 0) {
718                 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
719                        sizeof(struct udp_hdr));
720                 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
721                        sizeof(struct udp_hdr));
722         } else {
723                 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
724                 if ((*inner_ofst + sizeof(struct udp_hdr)) >
725                      FILTER_GENERIC_1_KEY_LEN)
726                         return ENOTSUP;
727                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
728                        mask, sizeof(struct udp_hdr));
729                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
730                        spec, sizeof(struct udp_hdr));
731                 *inner_ofst += sizeof(struct udp_hdr);
732         }
733         return 0;
734 }
735
736 /**
737  * Copy TCP item into version 2 NIC filter.
738  *
739  * @param item[in]
740  *   Item specification.
741  * @param enic_filter[out]
742  *   Partially filled in NIC filter structure.
743  * @param inner_ofst[in]
744  *   Must be 0. Don't support inner TCP filtering.
745  */
746 static int
747 enic_copy_item_tcp_v2(const struct rte_flow_item *item,
748                       struct filter_v2 *enic_filter, u8 *inner_ofst)
749 {
750         const struct rte_flow_item_tcp *spec = item->spec;
751         const struct rte_flow_item_tcp *mask = item->mask;
752         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
753
754         FLOW_TRACE();
755
756         /* Match TCP */
757         gp->mask_flags |= FILTER_GENERIC_1_TCP;
758         gp->val_flags |= FILTER_GENERIC_1_TCP;
759
760         /* Match all if no spec */
761         if (!spec)
762                 return 0;
763
764         if (!mask)
765                 return ENOTSUP;
766
767         if (*inner_ofst == 0) {
768                 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
769                        sizeof(struct tcp_hdr));
770                 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
771                        sizeof(struct tcp_hdr));
772         } else {
773                 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
774                 if ((*inner_ofst + sizeof(struct tcp_hdr)) >
775                      FILTER_GENERIC_1_KEY_LEN)
776                         return ENOTSUP;
777                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
778                        mask, sizeof(struct tcp_hdr));
779                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
780                        spec, sizeof(struct tcp_hdr));
781                 *inner_ofst += sizeof(struct tcp_hdr);
782         }
783         return 0;
784 }
785
786 /**
787  * Copy SCTP item into version 2 NIC filter.
788  *
789  * @param item[in]
790  *   Item specification.
791  * @param enic_filter[out]
792  *   Partially filled in NIC filter structure.
793  * @param inner_ofst[in]
794  *   Must be 0. Don't support inner SCTP filtering.
795  */
796 static int
797 enic_copy_item_sctp_v2(const struct rte_flow_item *item,
798                        struct filter_v2 *enic_filter, u8 *inner_ofst)
799 {
800         const struct rte_flow_item_sctp *spec = item->spec;
801         const struct rte_flow_item_sctp *mask = item->mask;
802         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
803
804         FLOW_TRACE();
805
806         if (*inner_ofst)
807                 return ENOTSUP;
808
809         /* Match all if no spec */
810         if (!spec)
811                 return 0;
812
813         if (!mask)
814                 mask = &rte_flow_item_sctp_mask;
815
816         memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
817                sizeof(struct sctp_hdr));
818         memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
819                sizeof(struct sctp_hdr));
820         return 0;
821 }
822
823 /**
824  * Copy UDP item into version 2 NIC filter.
825  *
826  * @param item[in]
827  *   Item specification.
828  * @param enic_filter[out]
829  *   Partially filled in NIC filter structure.
830  * @param inner_ofst[in]
831  *   Must be 0. VxLAN headers always start at the beginning of L5.
832  */
833 static int
834 enic_copy_item_vxlan_v2(const struct rte_flow_item *item,
835                         struct filter_v2 *enic_filter, u8 *inner_ofst)
836 {
837         const struct rte_flow_item_vxlan *spec = item->spec;
838         const struct rte_flow_item_vxlan *mask = item->mask;
839         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
840
841         FLOW_TRACE();
842
843         if (*inner_ofst)
844                 return EINVAL;
845
846         /* Match all if no spec */
847         if (!spec)
848                 return 0;
849
850         if (!mask)
851                 mask = &rte_flow_item_vxlan_mask;
852
853         memcpy(gp->layer[FILTER_GENERIC_1_L5].mask, mask,
854                sizeof(struct vxlan_hdr));
855         memcpy(gp->layer[FILTER_GENERIC_1_L5].val, spec,
856                sizeof(struct vxlan_hdr));
857
858         *inner_ofst = sizeof(struct vxlan_hdr);
859         return 0;
860 }
861
862 /**
863  * Return 1 if current item is valid on top of the previous one.
864  *
865  * @param prev_item[in]
866  *   The item before this one in the pattern or RTE_FLOW_ITEM_TYPE_END if this
867  *   is the first item.
868  * @param item_info[in]
869  *   Info about this item, like valid previous items.
870  * @param is_first[in]
871  *   True if this the first item in the pattern.
872  */
873 static int
874 item_stacking_valid(enum rte_flow_item_type prev_item,
875                     const struct enic_items *item_info, u8 is_first_item)
876 {
877         enum rte_flow_item_type const *allowed_items = item_info->prev_items;
878
879         FLOW_TRACE();
880
881         for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
882                 if (prev_item == *allowed_items)
883                         return 1;
884         }
885
886         /* This is the first item in the stack. Check if that's cool */
887         if (is_first_item && item_info->valid_start_item)
888                 return 1;
889
890         return 0;
891 }
892
893 /**
894  * Build the intenal enic filter structure from the provided pattern. The
895  * pattern is validated as the items are copied.
896  *
897  * @param pattern[in]
898  * @param items_info[in]
899  *   Info about this NICs item support, like valid previous items.
900  * @param enic_filter[out]
901  *   NIC specfilc filters derived from the pattern.
902  * @param error[out]
903  */
904 static int
905 enic_copy_filter(const struct rte_flow_item pattern[],
906                  const struct enic_items *items_info,
907                  struct filter_v2 *enic_filter,
908                  struct rte_flow_error *error)
909 {
910         int ret;
911         const struct rte_flow_item *item = pattern;
912         u8 inner_ofst = 0; /* If encapsulated, ofst into L5 */
913         enum rte_flow_item_type prev_item;
914         const struct enic_items *item_info;
915
916         u8 is_first_item = 1;
917
918         FLOW_TRACE();
919
920         prev_item = 0;
921
922         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
923                 /* Get info about how to validate and copy the item. If NULL
924                  * is returned the nic does not support the item.
925                  */
926                 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
927                         continue;
928
929                 item_info = &items_info[item->type];
930
931                 /* check to see if item stacking is valid */
932                 if (!item_stacking_valid(prev_item, item_info, is_first_item))
933                         goto stacking_error;
934
935                 ret = item_info->copy_item(item, enic_filter, &inner_ofst);
936                 if (ret)
937                         goto item_not_supported;
938                 prev_item = item->type;
939                 is_first_item = 0;
940         }
941         return 0;
942
943 item_not_supported:
944         rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_ITEM,
945                            NULL, "enic type error");
946         return -rte_errno;
947
948 stacking_error:
949         rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
950                            item, "stacking error");
951         return -rte_errno;
952 }
953
954 /**
955  * Build the intenal version 1 NIC action structure from the provided pattern.
956  * The pattern is validated as the items are copied.
957  *
958  * @param actions[in]
959  * @param enic_action[out]
960  *   NIC specfilc actions derived from the actions.
961  * @param error[out]
962  */
963 static int
964 enic_copy_action_v1(const struct rte_flow_action actions[],
965                     struct filter_action_v2 *enic_action)
966 {
967         FLOW_TRACE();
968
969         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
970                 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID)
971                         continue;
972
973                 switch (actions->type) {
974                 case RTE_FLOW_ACTION_TYPE_QUEUE: {
975                         const struct rte_flow_action_queue *queue =
976                                 (const struct rte_flow_action_queue *)
977                                 actions->conf;
978                         enic_action->rq_idx =
979                                 enic_rte_rq_idx_to_sop_idx(queue->index);
980                         break;
981                 }
982                 default:
983                         RTE_ASSERT(0);
984                         break;
985                 }
986         }
987         enic_action->type = FILTER_ACTION_RQ_STEERING;
988         return 0;
989 }
990
991 /**
992  * Build the intenal version 2 NIC action structure from the provided pattern.
993  * The pattern is validated as the items are copied.
994  *
995  * @param actions[in]
996  * @param enic_action[out]
997  *   NIC specfilc actions derived from the actions.
998  * @param error[out]
999  */
1000 static int
1001 enic_copy_action_v2(const struct rte_flow_action actions[],
1002                     struct filter_action_v2 *enic_action)
1003 {
1004         FLOW_TRACE();
1005
1006         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1007                 switch (actions->type) {
1008                 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1009                         const struct rte_flow_action_queue *queue =
1010                                 (const struct rte_flow_action_queue *)
1011                                 actions->conf;
1012                         enic_action->rq_idx =
1013                                 enic_rte_rq_idx_to_sop_idx(queue->index);
1014                         enic_action->flags |= FILTER_ACTION_RQ_STEERING_FLAG;
1015                         break;
1016                 }
1017                 case RTE_FLOW_ACTION_TYPE_MARK: {
1018                         const struct rte_flow_action_mark *mark =
1019                                 (const struct rte_flow_action_mark *)
1020                                 actions->conf;
1021
1022                         /* ENIC_MAGIC_FILTER_ID is reserved and is the highest
1023                          * in the range of allows mark ids.
1024                          */
1025                         if (mark->id >= ENIC_MAGIC_FILTER_ID)
1026                                 return EINVAL;
1027                         enic_action->filter_id = mark->id;
1028                         enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1029                         break;
1030                 }
1031                 case RTE_FLOW_ACTION_TYPE_FLAG: {
1032                         enic_action->filter_id = ENIC_MAGIC_FILTER_ID;
1033                         enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1034                         break;
1035                 }
1036                 case RTE_FLOW_ACTION_TYPE_DROP: {
1037                         enic_action->flags |= FILTER_ACTION_DROP_FLAG;
1038                         break;
1039                 }
1040                 case RTE_FLOW_ACTION_TYPE_VOID:
1041                         continue;
1042                 default:
1043                         RTE_ASSERT(0);
1044                         break;
1045                 }
1046         }
1047         enic_action->type = FILTER_ACTION_V2;
1048         return 0;
1049 }
1050
1051 /** Check if the action is supported */
1052 static int
1053 enic_match_action(const struct rte_flow_action *action,
1054                   const enum rte_flow_action_type *supported_actions)
1055 {
1056         for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
1057              supported_actions++) {
1058                 if (action->type == *supported_actions)
1059                         return 1;
1060         }
1061         return 0;
1062 }
1063
1064 /** Get the NIC filter capabilties structure */
1065 static const struct enic_filter_cap *
1066 enic_get_filter_cap(struct enic *enic)
1067 {
1068         if (enic->flow_filter_mode)
1069                 return &enic_filter_cap[enic->flow_filter_mode];
1070
1071         return NULL;
1072 }
1073
1074 /** Get the actions for this NIC version. */
1075 static const struct enic_action_cap *
1076 enic_get_action_cap(struct enic *enic)
1077 {
1078         const struct enic_action_cap *ea;
1079         uint8_t actions;
1080
1081         actions = enic->filter_actions;
1082         if (actions & FILTER_ACTION_DROP_FLAG)
1083                 ea = &enic_action_cap[FILTER_ACTION_DROP_FLAG];
1084         else if (actions & FILTER_ACTION_FILTER_ID_FLAG)
1085                 ea = &enic_action_cap[FILTER_ACTION_FILTER_ID_FLAG];
1086         else
1087                 ea = &enic_action_cap[FILTER_ACTION_RQ_STEERING_FLAG];
1088         return ea;
1089 }
1090
1091 /* Debug function to dump internal NIC action structure. */
1092 static void
1093 enic_dump_actions(const struct filter_action_v2 *ea)
1094 {
1095         if (ea->type == FILTER_ACTION_RQ_STEERING) {
1096                 FLOW_LOG(INFO, "Action(V1), queue: %u\n", ea->rq_idx);
1097         } else if (ea->type == FILTER_ACTION_V2) {
1098                 FLOW_LOG(INFO, "Actions(V2)\n");
1099                 if (ea->flags & FILTER_ACTION_RQ_STEERING_FLAG)
1100                         FLOW_LOG(INFO, "\tqueue: %u\n",
1101                                enic_sop_rq_idx_to_rte_idx(ea->rq_idx));
1102                 if (ea->flags & FILTER_ACTION_FILTER_ID_FLAG)
1103                         FLOW_LOG(INFO, "\tfilter_id: %u\n", ea->filter_id);
1104         }
1105 }
1106
1107 /* Debug function to dump internal NIC filter structure. */
1108 static void
1109 enic_dump_filter(const struct filter_v2 *filt)
1110 {
1111         const struct filter_generic_1 *gp;
1112         int i, j, mbyte;
1113         char buf[128], *bp;
1114         char ip4[16], ip6[16], udp[16], tcp[16], tcpudp[16], ip4csum[16];
1115         char l4csum[16], ipfrag[16];
1116
1117         switch (filt->type) {
1118         case FILTER_IPV4_5TUPLE:
1119                 FLOW_LOG(INFO, "FILTER_IPV4_5TUPLE\n");
1120                 break;
1121         case FILTER_USNIC_IP:
1122         case FILTER_DPDK_1:
1123                 /* FIXME: this should be a loop */
1124                 gp = &filt->u.generic_1;
1125                 FLOW_LOG(INFO, "Filter: vlan: 0x%04x, mask: 0x%04x\n",
1126                        gp->val_vlan, gp->mask_vlan);
1127
1128                 if (gp->mask_flags & FILTER_GENERIC_1_IPV4)
1129                         sprintf(ip4, "%s ",
1130                                 (gp->val_flags & FILTER_GENERIC_1_IPV4)
1131                                  ? "ip4(y)" : "ip4(n)");
1132                 else
1133                         sprintf(ip4, "%s ", "ip4(x)");
1134
1135                 if (gp->mask_flags & FILTER_GENERIC_1_IPV6)
1136                         sprintf(ip6, "%s ",
1137                                 (gp->val_flags & FILTER_GENERIC_1_IPV4)
1138                                  ? "ip6(y)" : "ip6(n)");
1139                 else
1140                         sprintf(ip6, "%s ", "ip6(x)");
1141
1142                 if (gp->mask_flags & FILTER_GENERIC_1_UDP)
1143                         sprintf(udp, "%s ",
1144                                 (gp->val_flags & FILTER_GENERIC_1_UDP)
1145                                  ? "udp(y)" : "udp(n)");
1146                 else
1147                         sprintf(udp, "%s ", "udp(x)");
1148
1149                 if (gp->mask_flags & FILTER_GENERIC_1_TCP)
1150                         sprintf(tcp, "%s ",
1151                                 (gp->val_flags & FILTER_GENERIC_1_TCP)
1152                                  ? "tcp(y)" : "tcp(n)");
1153                 else
1154                         sprintf(tcp, "%s ", "tcp(x)");
1155
1156                 if (gp->mask_flags & FILTER_GENERIC_1_TCP_OR_UDP)
1157                         sprintf(tcpudp, "%s ",
1158                                 (gp->val_flags & FILTER_GENERIC_1_TCP_OR_UDP)
1159                                  ? "tcpudp(y)" : "tcpudp(n)");
1160                 else
1161                         sprintf(tcpudp, "%s ", "tcpudp(x)");
1162
1163                 if (gp->mask_flags & FILTER_GENERIC_1_IP4SUM_OK)
1164                         sprintf(ip4csum, "%s ",
1165                                 (gp->val_flags & FILTER_GENERIC_1_IP4SUM_OK)
1166                                  ? "ip4csum(y)" : "ip4csum(n)");
1167                 else
1168                         sprintf(ip4csum, "%s ", "ip4csum(x)");
1169
1170                 if (gp->mask_flags & FILTER_GENERIC_1_L4SUM_OK)
1171                         sprintf(l4csum, "%s ",
1172                                 (gp->val_flags & FILTER_GENERIC_1_L4SUM_OK)
1173                                  ? "l4csum(y)" : "l4csum(n)");
1174                 else
1175                         sprintf(l4csum, "%s ", "l4csum(x)");
1176
1177                 if (gp->mask_flags & FILTER_GENERIC_1_IPFRAG)
1178                         sprintf(ipfrag, "%s ",
1179                                 (gp->val_flags & FILTER_GENERIC_1_IPFRAG)
1180                                  ? "ipfrag(y)" : "ipfrag(n)");
1181                 else
1182                         sprintf(ipfrag, "%s ", "ipfrag(x)");
1183                 FLOW_LOG(INFO, "\tFlags: %s%s%s%s%s%s%s%s\n", ip4, ip6, udp,
1184                          tcp, tcpudp, ip4csum, l4csum, ipfrag);
1185
1186                 for (i = 0; i < FILTER_GENERIC_1_NUM_LAYERS; i++) {
1187                         mbyte = FILTER_GENERIC_1_KEY_LEN - 1;
1188                         while (mbyte && !gp->layer[i].mask[mbyte])
1189                                 mbyte--;
1190                         if (mbyte == 0)
1191                                 continue;
1192
1193                         bp = buf;
1194                         for (j = 0; j <= mbyte; j++) {
1195                                 sprintf(bp, "%02x",
1196                                         gp->layer[i].mask[j]);
1197                                 bp += 2;
1198                         }
1199                         *bp = '\0';
1200                         FLOW_LOG(INFO, "\tL%u mask: %s\n", i + 2, buf);
1201                         bp = buf;
1202                         for (j = 0; j <= mbyte; j++) {
1203                                 sprintf(bp, "%02x",
1204                                         gp->layer[i].val[j]);
1205                                 bp += 2;
1206                         }
1207                         *bp = '\0';
1208                         FLOW_LOG(INFO, "\tL%u  val: %s\n", i + 2, buf);
1209                 }
1210                 break;
1211         default:
1212                 FLOW_LOG(INFO, "FILTER UNKNOWN\n");
1213                 break;
1214         }
1215 }
1216
1217 /* Debug function to dump internal NIC flow structures. */
1218 static void
1219 enic_dump_flow(const struct filter_action_v2 *ea, const struct filter_v2 *filt)
1220 {
1221         enic_dump_filter(filt);
1222         enic_dump_actions(ea);
1223 }
1224
1225
1226 /**
1227  * Internal flow parse/validate function.
1228  *
1229  * @param dev[in]
1230  *   This device pointer.
1231  * @param pattern[in]
1232  * @param actions[in]
1233  * @param error[out]
1234  * @param enic_filter[out]
1235  *   Internal NIC filter structure pointer.
1236  * @param enic_action[out]
1237  *   Internal NIC action structure pointer.
1238  */
1239 static int
1240 enic_flow_parse(struct rte_eth_dev *dev,
1241                 const struct rte_flow_attr *attrs,
1242                 const struct rte_flow_item pattern[],
1243                 const struct rte_flow_action actions[],
1244                 struct rte_flow_error *error,
1245                 struct filter_v2 *enic_filter,
1246                 struct filter_action_v2 *enic_action)
1247 {
1248         unsigned int ret = 0;
1249         struct enic *enic = pmd_priv(dev);
1250         const struct enic_filter_cap *enic_filter_cap;
1251         const struct enic_action_cap *enic_action_cap;
1252         const struct rte_flow_action *action;
1253
1254         FLOW_TRACE();
1255
1256         memset(enic_filter, 0, sizeof(*enic_filter));
1257         memset(enic_action, 0, sizeof(*enic_action));
1258
1259         if (!pattern) {
1260                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1261                                    NULL, "No pattern specified");
1262                 return -rte_errno;
1263         }
1264
1265         if (!actions) {
1266                 rte_flow_error_set(error, EINVAL,
1267                                    RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1268                                    NULL, "No action specified");
1269                 return -rte_errno;
1270         }
1271
1272         if (attrs) {
1273                 if (attrs->group) {
1274                         rte_flow_error_set(error, ENOTSUP,
1275                                            RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1276                                            NULL,
1277                                            "priority groups are not supported");
1278                         return -rte_errno;
1279                 } else if (attrs->priority) {
1280                         rte_flow_error_set(error, ENOTSUP,
1281                                            RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1282                                            NULL,
1283                                            "priorities are not supported");
1284                         return -rte_errno;
1285                 } else if (attrs->egress) {
1286                         rte_flow_error_set(error, ENOTSUP,
1287                                            RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
1288                                            NULL,
1289                                            "egress is not supported");
1290                         return -rte_errno;
1291                 } else if (!attrs->ingress) {
1292                         rte_flow_error_set(error, ENOTSUP,
1293                                            RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1294                                            NULL,
1295                                            "only ingress is supported");
1296                         return -rte_errno;
1297                 }
1298
1299         } else {
1300                 rte_flow_error_set(error, EINVAL,
1301                                    RTE_FLOW_ERROR_TYPE_ATTR,
1302                                    NULL, "No attribute specified");
1303                 return -rte_errno;
1304         }
1305
1306         /* Verify Actions. */
1307         enic_action_cap =  enic_get_action_cap(enic);
1308         for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
1309              action++) {
1310                 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1311                         continue;
1312                 else if (!enic_match_action(action, enic_action_cap->actions))
1313                         break;
1314         }
1315         if (action->type != RTE_FLOW_ACTION_TYPE_END) {
1316                 rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
1317                                    action, "Invalid action.");
1318                 return -rte_errno;
1319         }
1320         ret = enic_action_cap->copy_fn(actions, enic_action);
1321         if (ret) {
1322                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1323                            NULL, "Unsupported action.");
1324                 return -rte_errno;
1325         }
1326
1327         /* Verify Flow items. If copying the filter from flow format to enic
1328          * format fails, the flow is not supported
1329          */
1330         enic_filter_cap =  enic_get_filter_cap(enic);
1331         if (enic_filter_cap == NULL) {
1332                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1333                            NULL, "Flow API not available");
1334                 return -rte_errno;
1335         }
1336         enic_filter->type = enic->flow_filter_mode;
1337         ret = enic_copy_filter(pattern, enic_filter_cap->item_info,
1338                                        enic_filter, error);
1339         return ret;
1340 }
1341
1342 /**
1343  * Push filter/action to the NIC.
1344  *
1345  * @param enic[in]
1346  *   Device structure pointer.
1347  * @param enic_filter[in]
1348  *   Internal NIC filter structure pointer.
1349  * @param enic_action[in]
1350  *   Internal NIC action structure pointer.
1351  * @param error[out]
1352  */
1353 static struct rte_flow *
1354 enic_flow_add_filter(struct enic *enic, struct filter_v2 *enic_filter,
1355                    struct filter_action_v2 *enic_action,
1356                    struct rte_flow_error *error)
1357 {
1358         struct rte_flow *flow;
1359         int ret;
1360         u16 entry;
1361
1362         FLOW_TRACE();
1363
1364         flow = rte_calloc(__func__, 1, sizeof(*flow), 0);
1365         if (!flow) {
1366                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1367                                    NULL, "cannot allocate flow memory");
1368                 return NULL;
1369         }
1370
1371         /* entry[in] is the queue id, entry[out] is the filter Id for delete */
1372         entry = enic_action->rq_idx;
1373         ret = vnic_dev_classifier(enic->vdev, CLSF_ADD, &entry, enic_filter,
1374                                   enic_action);
1375         if (!ret) {
1376                 flow->enic_filter_id = entry;
1377                 flow->enic_filter = *enic_filter;
1378         } else {
1379                 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
1380                                    NULL, "vnic_dev_classifier error");
1381                 rte_free(flow);
1382                 return NULL;
1383         }
1384         return flow;
1385 }
1386
1387 /**
1388  * Remove filter/action from the NIC.
1389  *
1390  * @param enic[in]
1391  *   Device structure pointer.
1392  * @param filter_id[in]
1393  *   Id of NIC filter.
1394  * @param enic_action[in]
1395  *   Internal NIC action structure pointer.
1396  * @param error[out]
1397  */
1398 static int
1399 enic_flow_del_filter(struct enic *enic, u16 filter_id,
1400                    struct rte_flow_error *error)
1401 {
1402         int ret;
1403
1404         FLOW_TRACE();
1405
1406         ret = vnic_dev_classifier(enic->vdev, CLSF_DEL, &filter_id, NULL, NULL);
1407         if (!ret)
1408                 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
1409                                    NULL, "vnic_dev_classifier failed");
1410         return ret;
1411 }
1412
1413 /*
1414  * The following functions are callbacks for Generic flow API.
1415  */
1416
1417 /**
1418  * Validate a flow supported by the NIC.
1419  *
1420  * @see rte_flow_validate()
1421  * @see rte_flow_ops
1422  */
1423 static int
1424 enic_flow_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attrs,
1425                    const struct rte_flow_item pattern[],
1426                    const struct rte_flow_action actions[],
1427                    struct rte_flow_error *error)
1428 {
1429         struct filter_v2 enic_filter;
1430         struct filter_action_v2 enic_action;
1431         int ret;
1432
1433         FLOW_TRACE();
1434
1435         ret = enic_flow_parse(dev, attrs, pattern, actions, error,
1436                                &enic_filter, &enic_action);
1437         if (!ret)
1438                 enic_dump_flow(&enic_action, &enic_filter);
1439         return ret;
1440 }
1441
1442 /**
1443  * Create a flow supported by the NIC.
1444  *
1445  * @see rte_flow_create()
1446  * @see rte_flow_ops
1447  */
1448 static struct rte_flow *
1449 enic_flow_create(struct rte_eth_dev *dev,
1450                  const struct rte_flow_attr *attrs,
1451                  const struct rte_flow_item pattern[],
1452                  const struct rte_flow_action actions[],
1453                  struct rte_flow_error *error)
1454 {
1455         int ret;
1456         struct filter_v2 enic_filter;
1457         struct filter_action_v2 enic_action;
1458         struct rte_flow *flow;
1459         struct enic *enic = pmd_priv(dev);
1460
1461         FLOW_TRACE();
1462
1463         ret = enic_flow_parse(dev, attrs, pattern, actions, error, &enic_filter,
1464                               &enic_action);
1465         if (ret < 0)
1466                 return NULL;
1467
1468         rte_spinlock_lock(&enic->flows_lock);
1469         flow = enic_flow_add_filter(enic, &enic_filter, &enic_action,
1470                                     error);
1471         if (flow)
1472                 LIST_INSERT_HEAD(&enic->flows, flow, next);
1473         rte_spinlock_unlock(&enic->flows_lock);
1474
1475         return flow;
1476 }
1477
1478 /**
1479  * Destroy a flow supported by the NIC.
1480  *
1481  * @see rte_flow_destroy()
1482  * @see rte_flow_ops
1483  */
1484 static int
1485 enic_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
1486                   __rte_unused struct rte_flow_error *error)
1487 {
1488         struct enic *enic = pmd_priv(dev);
1489
1490         FLOW_TRACE();
1491
1492         rte_spinlock_lock(&enic->flows_lock);
1493         enic_flow_del_filter(enic, flow->enic_filter_id, error);
1494         LIST_REMOVE(flow, next);
1495         rte_spinlock_unlock(&enic->flows_lock);
1496         return 0;
1497 }
1498
1499 /**
1500  * Flush all flows on the device.
1501  *
1502  * @see rte_flow_flush()
1503  * @see rte_flow_ops
1504  */
1505 static int
1506 enic_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1507 {
1508         struct rte_flow *flow;
1509         struct enic *enic = pmd_priv(dev);
1510
1511         FLOW_TRACE();
1512
1513         rte_spinlock_lock(&enic->flows_lock);
1514
1515         while (!LIST_EMPTY(&enic->flows)) {
1516                 flow = LIST_FIRST(&enic->flows);
1517                 enic_flow_del_filter(enic, flow->enic_filter_id, error);
1518                 LIST_REMOVE(flow, next);
1519         }
1520         rte_spinlock_unlock(&enic->flows_lock);
1521         return 0;
1522 }
1523
1524 /**
1525  * Flow callback registration.
1526  *
1527  * @see rte_flow_ops
1528  */
1529 const struct rte_flow_ops enic_flow_ops = {
1530         .validate = enic_flow_validate,
1531         .create = enic_flow_create,
1532         .destroy = enic_flow_destroy,
1533         .flush = enic_flow_flush,
1534 };