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