net/enic: flow API for NICs with advanced filters enabled
[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 #ifdef RTE_LIBRTE_ENIC_DEBUG_FLOW
46 #define FLOW_TRACE() \
47         RTE_LOG(DEBUG, PMD, "%s()\n", __func__)
48 #define FLOW_LOG(level, fmt, args...) \
49         RTE_LOG(level, PMD, fmt, ## args)
50 #else
51 #define FLOW_TRACE() do { } while (0)
52 #define FLOW_LOG(level, fmt, args...) do { } while (0)
53 #endif
54
55 /** Info about how to copy items into enic filters. */
56 struct enic_items {
57         /** Function for copying and validating an item. */
58         int (*copy_item)(const struct rte_flow_item *item,
59                          struct filter_v2 *enic_filter, u8 *inner_ofst);
60         /** List of valid previous items. */
61         const enum rte_flow_item_type * const prev_items;
62         /** True if it's OK for this item to be the first item. For some NIC
63          * versions, it's invalid to start the stack above layer 3.
64          */
65         const u8 valid_start_item;
66 };
67
68 /** Filtering capabilities for various NIC and firmware versions. */
69 struct enic_filter_cap {
70         /** list of valid items and their handlers and attributes. */
71         const struct enic_items *item_info;
72 };
73
74 /* functions for copying flow actions into enic actions */
75 typedef int (copy_action_fn)(const struct rte_flow_action actions[],
76                              struct filter_action_v2 *enic_action);
77
78 /* functions for copying items into enic filters */
79 typedef int(enic_copy_item_fn)(const struct rte_flow_item *item,
80                           struct filter_v2 *enic_filter, u8 *inner_ofst);
81
82 /** Action capabilities for various NICs. */
83 struct enic_action_cap {
84         /** list of valid actions */
85         const enum rte_flow_action_type *actions;
86         /** copy function for a particular NIC */
87         int (*copy_fn)(const struct rte_flow_action actions[],
88                        struct filter_action_v2 *enic_action);
89 };
90
91 /* Forward declarations */
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_v2;
102
103 /** NICs with Advanced filters enabled */
104 static const struct enic_items enic_items_v3[] = {
105         [RTE_FLOW_ITEM_TYPE_ETH] = {
106                 .copy_item = enic_copy_item_eth_v2,
107                 .valid_start_item = 1,
108                 .prev_items = (const enum rte_flow_item_type[]) {
109                                RTE_FLOW_ITEM_TYPE_VXLAN,
110                                RTE_FLOW_ITEM_TYPE_END,
111                 },
112         },
113         [RTE_FLOW_ITEM_TYPE_VLAN] = {
114                 .copy_item = enic_copy_item_vlan_v2,
115                 .valid_start_item = 1,
116                 .prev_items = (const enum rte_flow_item_type[]) {
117                                RTE_FLOW_ITEM_TYPE_ETH,
118                                RTE_FLOW_ITEM_TYPE_END,
119                 },
120         },
121         [RTE_FLOW_ITEM_TYPE_IPV4] = {
122                 .copy_item = enic_copy_item_ipv4_v2,
123                 .valid_start_item = 1,
124                 .prev_items = (const enum rte_flow_item_type[]) {
125                                RTE_FLOW_ITEM_TYPE_ETH,
126                                RTE_FLOW_ITEM_TYPE_VLAN,
127                                RTE_FLOW_ITEM_TYPE_END,
128                 },
129         },
130         [RTE_FLOW_ITEM_TYPE_IPV6] = {
131                 .copy_item = enic_copy_item_ipv6_v2,
132                 .valid_start_item = 1,
133                 .prev_items = (const enum rte_flow_item_type[]) {
134                                RTE_FLOW_ITEM_TYPE_ETH,
135                                RTE_FLOW_ITEM_TYPE_VLAN,
136                                RTE_FLOW_ITEM_TYPE_END,
137                 },
138         },
139         [RTE_FLOW_ITEM_TYPE_UDP] = {
140                 .copy_item = enic_copy_item_udp_v2,
141                 .valid_start_item = 1,
142                 .prev_items = (const enum rte_flow_item_type[]) {
143                                RTE_FLOW_ITEM_TYPE_IPV4,
144                                RTE_FLOW_ITEM_TYPE_IPV6,
145                                RTE_FLOW_ITEM_TYPE_END,
146                 },
147         },
148         [RTE_FLOW_ITEM_TYPE_TCP] = {
149                 .copy_item = enic_copy_item_tcp_v2,
150                 .valid_start_item = 1,
151                 .prev_items = (const enum rte_flow_item_type[]) {
152                                RTE_FLOW_ITEM_TYPE_IPV4,
153                                RTE_FLOW_ITEM_TYPE_IPV6,
154                                RTE_FLOW_ITEM_TYPE_END,
155                 },
156         },
157         [RTE_FLOW_ITEM_TYPE_SCTP] = {
158                 .copy_item = enic_copy_item_sctp_v2,
159                 .valid_start_item = 1,
160                 .prev_items = (const enum rte_flow_item_type[]) {
161                                RTE_FLOW_ITEM_TYPE_IPV4,
162                                RTE_FLOW_ITEM_TYPE_IPV6,
163                                RTE_FLOW_ITEM_TYPE_END,
164                 },
165         },
166         [RTE_FLOW_ITEM_TYPE_VXLAN] = {
167                 .copy_item = enic_copy_item_vxlan_v2,
168                 .valid_start_item = 1,
169                 .prev_items = (const enum rte_flow_item_type[]) {
170                                RTE_FLOW_ITEM_TYPE_UDP,
171                                RTE_FLOW_ITEM_TYPE_END,
172                 },
173         },
174 };
175
176 /** Filtering capabilities indexed this NICs supported filter type. */
177 static const struct enic_filter_cap enic_filter_cap[] = {
178         [FILTER_DPDK_1] = {
179                 .item_info = enic_items_v3,
180         },
181 };
182
183 /** Supported actions for newer NICs */
184 static const enum rte_flow_action_type enic_supported_actions_v2[] = {
185         RTE_FLOW_ACTION_TYPE_QUEUE,
186         RTE_FLOW_ACTION_TYPE_MARK,
187         RTE_FLOW_ACTION_TYPE_FLAG,
188         RTE_FLOW_ACTION_TYPE_END,
189 };
190
191 /** Action capabilities indexed by NIC version information */
192 static const struct enic_action_cap enic_action_cap[] = {
193         [FILTER_ACTION_V2_ALL] = {
194                 .actions = enic_supported_actions_v2,
195                 .copy_fn = enic_copy_action_v2,
196         },
197 };
198 /**
199  * Copy ETH item into version 2 NIC filter.
200  *
201  * @param item[in]
202  *   Item specification.
203  * @param enic_filter[out]
204  *   Partially filled in NIC filter structure.
205  * @param inner_ofst[in]
206  *   If zero, this is an outer header. If non-zero, this is the offset into L5
207  *   where the header begins.
208  */
209 static int
210 enic_copy_item_eth_v2(const struct rte_flow_item *item,
211                       struct filter_v2 *enic_filter, u8 *inner_ofst)
212 {
213         struct ether_hdr enic_spec;
214         struct ether_hdr enic_mask;
215         const struct rte_flow_item_eth *spec = item->spec;
216         const struct rte_flow_item_eth *mask = item->mask;
217         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
218
219         FLOW_TRACE();
220
221         /* Match all if no spec */
222         if (!spec)
223                 return 0;
224
225         if (!mask)
226                 mask = &rte_flow_item_eth_mask;
227
228         memcpy(enic_spec.d_addr.addr_bytes, spec->dst.addr_bytes,
229                ETHER_ADDR_LEN);
230         memcpy(enic_spec.s_addr.addr_bytes, spec->src.addr_bytes,
231                ETHER_ADDR_LEN);
232
233         memcpy(enic_mask.d_addr.addr_bytes, mask->dst.addr_bytes,
234                ETHER_ADDR_LEN);
235         memcpy(enic_mask.s_addr.addr_bytes, mask->src.addr_bytes,
236                ETHER_ADDR_LEN);
237         enic_spec.ether_type = spec->type;
238         enic_mask.ether_type = mask->type;
239
240         if (*inner_ofst == 0) {
241                 /* outer header */
242                 memcpy(gp->layer[FILTER_GENERIC_1_L2].mask, &enic_mask,
243                        sizeof(struct ether_hdr));
244                 memcpy(gp->layer[FILTER_GENERIC_1_L2].val, &enic_spec,
245                        sizeof(struct ether_hdr));
246         } else {
247                 /* inner header */
248                 if ((*inner_ofst + sizeof(struct ether_hdr)) >
249                      FILTER_GENERIC_1_KEY_LEN)
250                         return ENOTSUP;
251                 /* Offset into L5 where inner Ethernet header goes */
252                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
253                        &enic_mask, sizeof(struct ether_hdr));
254                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
255                        &enic_spec, sizeof(struct ether_hdr));
256                 *inner_ofst += sizeof(struct ether_hdr);
257         }
258         return 0;
259 }
260
261 /**
262  * Copy VLAN item into version 2 NIC filter.
263  *
264  * @param item[in]
265  *   Item specification.
266  * @param enic_filter[out]
267  *   Partially filled in NIC filter structure.
268  * @param inner_ofst[in]
269  *   If zero, this is an outer header. If non-zero, this is the offset into L5
270  *   where the header begins.
271  */
272 static int
273 enic_copy_item_vlan_v2(const struct rte_flow_item *item,
274                        struct filter_v2 *enic_filter, u8 *inner_ofst)
275 {
276         const struct rte_flow_item_vlan *spec = item->spec;
277         const struct rte_flow_item_vlan *mask = item->mask;
278         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
279
280         FLOW_TRACE();
281
282         /* Match all if no spec */
283         if (!spec)
284                 return 0;
285
286         /* Don't support filtering in tpid */
287         if (mask) {
288                 if (mask->tpid != 0)
289                         return ENOTSUP;
290         } else {
291                 mask = &rte_flow_item_vlan_mask;
292                 RTE_ASSERT(mask->tpid == 0);
293         }
294
295         if (*inner_ofst == 0) {
296                 /* Outer header. Use the vlan mask/val fields */
297                 gp->mask_vlan = mask->tci;
298                 gp->val_vlan = spec->tci;
299         } else {
300                 /* Inner header. Mask/Val start at *inner_ofst into L5 */
301                 if ((*inner_ofst + sizeof(struct vlan_hdr)) >
302                      FILTER_GENERIC_1_KEY_LEN)
303                         return ENOTSUP;
304                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
305                        mask, sizeof(struct vlan_hdr));
306                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
307                        spec, sizeof(struct vlan_hdr));
308                 *inner_ofst += sizeof(struct vlan_hdr);
309         }
310         return 0;
311 }
312
313 /**
314  * Copy IPv4 item into version 2 NIC filter.
315  *
316  * @param item[in]
317  *   Item specification.
318  * @param enic_filter[out]
319  *   Partially filled in NIC filter structure.
320  * @param inner_ofst[in]
321  *   Must be 0. Don't support inner IPv4 filtering.
322  */
323 static int
324 enic_copy_item_ipv4_v2(const struct rte_flow_item *item,
325                        struct filter_v2 *enic_filter, u8 *inner_ofst)
326 {
327         const struct rte_flow_item_ipv4 *spec = item->spec;
328         const struct rte_flow_item_ipv4 *mask = item->mask;
329         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
330
331         FLOW_TRACE();
332
333         if (*inner_ofst == 0) {
334                 /* Match IPv4 */
335                 gp->mask_flags |= FILTER_GENERIC_1_IPV4;
336                 gp->val_flags |= FILTER_GENERIC_1_IPV4;
337
338                 /* Match all if no spec */
339                 if (!spec)
340                         return 0;
341
342                 if (!mask)
343                         mask = &rte_flow_item_ipv4_mask;
344
345                 memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
346                        sizeof(struct ipv4_hdr));
347                 memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
348                        sizeof(struct ipv4_hdr));
349         } else {
350                 /* Inner IPv4 header. Mask/Val start at *inner_ofst into L5 */
351                 if ((*inner_ofst + sizeof(struct ipv4_hdr)) >
352                      FILTER_GENERIC_1_KEY_LEN)
353                         return ENOTSUP;
354                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
355                        mask, sizeof(struct ipv4_hdr));
356                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
357                        spec, sizeof(struct ipv4_hdr));
358                 *inner_ofst += sizeof(struct ipv4_hdr);
359         }
360         return 0;
361 }
362
363 /**
364  * Copy IPv6 item into version 2 NIC filter.
365  *
366  * @param item[in]
367  *   Item specification.
368  * @param enic_filter[out]
369  *   Partially filled in NIC filter structure.
370  * @param inner_ofst[in]
371  *   Must be 0. Don't support inner IPv6 filtering.
372  */
373 static int
374 enic_copy_item_ipv6_v2(const struct rte_flow_item *item,
375                        struct filter_v2 *enic_filter, u8 *inner_ofst)
376 {
377         const struct rte_flow_item_ipv6 *spec = item->spec;
378         const struct rte_flow_item_ipv6 *mask = item->mask;
379         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
380
381         FLOW_TRACE();
382
383         /* Match IPv6 */
384         gp->mask_flags |= FILTER_GENERIC_1_IPV6;
385         gp->val_flags |= FILTER_GENERIC_1_IPV6;
386
387         /* Match all if no spec */
388         if (!spec)
389                 return 0;
390
391         if (!mask)
392                 mask = &rte_flow_item_ipv6_mask;
393
394         if (*inner_ofst == 0) {
395                 memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
396                        sizeof(struct ipv6_hdr));
397                 memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
398                        sizeof(struct ipv6_hdr));
399         } else {
400                 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
401                 if ((*inner_ofst + sizeof(struct ipv6_hdr)) >
402                      FILTER_GENERIC_1_KEY_LEN)
403                         return ENOTSUP;
404                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
405                        mask, sizeof(struct ipv6_hdr));
406                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
407                        spec, sizeof(struct ipv6_hdr));
408                 *inner_ofst += sizeof(struct ipv6_hdr);
409         }
410         return 0;
411 }
412
413 /**
414  * Copy UDP item into version 2 NIC filter.
415  *
416  * @param item[in]
417  *   Item specification.
418  * @param enic_filter[out]
419  *   Partially filled in NIC filter structure.
420  * @param inner_ofst[in]
421  *   Must be 0. Don't support inner UDP filtering.
422  */
423 static int
424 enic_copy_item_udp_v2(const struct rte_flow_item *item,
425                       struct filter_v2 *enic_filter, u8 *inner_ofst)
426 {
427         const struct rte_flow_item_udp *spec = item->spec;
428         const struct rte_flow_item_udp *mask = item->mask;
429         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
430
431         FLOW_TRACE();
432
433         /* Match UDP */
434         gp->mask_flags |= FILTER_GENERIC_1_UDP;
435         gp->val_flags |= FILTER_GENERIC_1_UDP;
436
437         /* Match all if no spec */
438         if (!spec)
439                 return 0;
440
441         if (!mask)
442                 mask = &rte_flow_item_udp_mask;
443
444         if (*inner_ofst == 0) {
445                 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
446                        sizeof(struct udp_hdr));
447                 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
448                        sizeof(struct udp_hdr));
449         } else {
450                 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
451                 if ((*inner_ofst + sizeof(struct udp_hdr)) >
452                      FILTER_GENERIC_1_KEY_LEN)
453                         return ENOTSUP;
454                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
455                        mask, sizeof(struct udp_hdr));
456                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
457                        spec, sizeof(struct udp_hdr));
458                 *inner_ofst += sizeof(struct udp_hdr);
459         }
460         return 0;
461 }
462
463 /**
464  * Copy TCP item into version 2 NIC filter.
465  *
466  * @param item[in]
467  *   Item specification.
468  * @param enic_filter[out]
469  *   Partially filled in NIC filter structure.
470  * @param inner_ofst[in]
471  *   Must be 0. Don't support inner TCP filtering.
472  */
473 static int
474 enic_copy_item_tcp_v2(const struct rte_flow_item *item,
475                       struct filter_v2 *enic_filter, u8 *inner_ofst)
476 {
477         const struct rte_flow_item_tcp *spec = item->spec;
478         const struct rte_flow_item_tcp *mask = item->mask;
479         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
480
481         FLOW_TRACE();
482
483         /* Match TCP */
484         gp->mask_flags |= FILTER_GENERIC_1_TCP;
485         gp->val_flags |= FILTER_GENERIC_1_TCP;
486
487         /* Match all if no spec */
488         if (!spec)
489                 return 0;
490
491         if (!mask)
492                 return ENOTSUP;
493
494         if (*inner_ofst == 0) {
495                 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
496                        sizeof(struct tcp_hdr));
497                 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
498                        sizeof(struct tcp_hdr));
499         } else {
500                 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
501                 if ((*inner_ofst + sizeof(struct tcp_hdr)) >
502                      FILTER_GENERIC_1_KEY_LEN)
503                         return ENOTSUP;
504                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
505                        mask, sizeof(struct tcp_hdr));
506                 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
507                        spec, sizeof(struct tcp_hdr));
508                 *inner_ofst += sizeof(struct tcp_hdr);
509         }
510         return 0;
511 }
512
513 /**
514  * Copy SCTP item into version 2 NIC filter.
515  *
516  * @param item[in]
517  *   Item specification.
518  * @param enic_filter[out]
519  *   Partially filled in NIC filter structure.
520  * @param inner_ofst[in]
521  *   Must be 0. Don't support inner SCTP filtering.
522  */
523 static int
524 enic_copy_item_sctp_v2(const struct rte_flow_item *item,
525                        struct filter_v2 *enic_filter, u8 *inner_ofst)
526 {
527         const struct rte_flow_item_sctp *spec = item->spec;
528         const struct rte_flow_item_sctp *mask = item->mask;
529         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
530
531         FLOW_TRACE();
532
533         if (*inner_ofst)
534                 return ENOTSUP;
535
536         /* Match all if no spec */
537         if (!spec)
538                 return 0;
539
540         if (!mask)
541                 mask = &rte_flow_item_sctp_mask;
542
543         memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
544                sizeof(struct sctp_hdr));
545         memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
546                sizeof(struct sctp_hdr));
547         return 0;
548 }
549
550 /**
551  * Copy UDP item into version 2 NIC filter.
552  *
553  * @param item[in]
554  *   Item specification.
555  * @param enic_filter[out]
556  *   Partially filled in NIC filter structure.
557  * @param inner_ofst[in]
558  *   Must be 0. VxLAN headers always start at the beginning of L5.
559  */
560 static int
561 enic_copy_item_vxlan_v2(const struct rte_flow_item *item,
562                         struct filter_v2 *enic_filter, u8 *inner_ofst)
563 {
564         const struct rte_flow_item_vxlan *spec = item->spec;
565         const struct rte_flow_item_vxlan *mask = item->mask;
566         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
567
568         FLOW_TRACE();
569
570         if (*inner_ofst)
571                 return EINVAL;
572
573         /* Match all if no spec */
574         if (!spec)
575                 return 0;
576
577         if (!mask)
578                 mask = &rte_flow_item_vxlan_mask;
579
580         memcpy(gp->layer[FILTER_GENERIC_1_L5].mask, mask,
581                sizeof(struct vxlan_hdr));
582         memcpy(gp->layer[FILTER_GENERIC_1_L5].val, spec,
583                sizeof(struct vxlan_hdr));
584
585         *inner_ofst = sizeof(struct vxlan_hdr);
586         return 0;
587 }
588
589 /**
590  * Return 1 if current item is valid on top of the previous one.
591  *
592  * @param prev_item[in]
593  *   The item before this one in the pattern or RTE_FLOW_ITEM_TYPE_END if this
594  *   is the first item.
595  * @param item_info[in]
596  *   Info about this item, like valid previous items.
597  * @param is_first[in]
598  *   True if this the first item in the pattern.
599  */
600 static int
601 item_stacking_valid(enum rte_flow_item_type prev_item,
602                     const struct enic_items *item_info, u8 is_first_item)
603 {
604         enum rte_flow_item_type const *allowed_items = item_info->prev_items;
605
606         FLOW_TRACE();
607
608         for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
609                 if (prev_item == *allowed_items)
610                         return 1;
611         }
612
613         /* This is the first item in the stack. Check if that's cool */
614         if (is_first_item && item_info->valid_start_item)
615                 return 1;
616
617         return 0;
618 }
619
620 /**
621  * Build the intenal enic filter structure from the provided pattern. The
622  * pattern is validated as the items are copied.
623  *
624  * @param pattern[in]
625  * @param items_info[in]
626  *   Info about this NICs item support, like valid previous items.
627  * @param enic_filter[out]
628  *   NIC specfilc filters derived from the pattern.
629  * @param error[out]
630  */
631 static int
632 enic_copy_filter(const struct rte_flow_item pattern[],
633                  const struct enic_items *items_info,
634                  struct filter_v2 *enic_filter,
635                  struct rte_flow_error *error)
636 {
637         int ret;
638         const struct rte_flow_item *item = pattern;
639         u8 inner_ofst = 0; /* If encapsulated, ofst into L5 */
640         enum rte_flow_item_type prev_item;
641         const struct enic_items *item_info;
642
643         enic_filter->type = FILTER_DPDK_1;
644         u8 is_first_item = 1;
645
646         FLOW_TRACE();
647
648         prev_item = 0;
649
650         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
651                 /* Get info about how to validate and copy the item. If NULL
652                  * is returned the nic does not support the item.
653                  */
654                 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
655                         continue;
656
657                 item_info = &items_info[item->type];
658
659                 /* check to see if item stacking is valid */
660                 if (!item_stacking_valid(prev_item, item_info, is_first_item))
661                         goto stacking_error;
662
663                 ret = item_info->copy_item(item, enic_filter, &inner_ofst);
664                 if (ret)
665                         goto item_not_supported;
666                 prev_item = item->type;
667                 is_first_item = 0;
668         }
669         return 0;
670
671 item_not_supported:
672         rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_ITEM,
673                            NULL, "enic type error");
674         return -rte_errno;
675
676 stacking_error:
677         rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
678                            item, "stacking error");
679         return -rte_errno;
680 }
681 /**
682  * Build the intenal version 2 NIC action structure from the provided pattern.
683  * The pattern is validated as the items are copied.
684  *
685  * @param actions[in]
686  * @param enic_action[out]
687  *   NIC specfilc actions derived from the actions.
688  * @param error[out]
689  */
690 static int
691 enic_copy_action_v2(const struct rte_flow_action actions[],
692                     struct filter_action_v2 *enic_action)
693 {
694         FLOW_TRACE();
695
696         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
697                 switch (actions->type) {
698                 case RTE_FLOW_ACTION_TYPE_QUEUE: {
699                         const struct rte_flow_action_queue *queue =
700                                 (const struct rte_flow_action_queue *)
701                                 actions->conf;
702                         enic_action->rq_idx =
703                                 enic_rte_rq_idx_to_sop_idx(queue->index);
704                         enic_action->flags |= FILTER_ACTION_RQ_STEERING_FLAG;
705                         break;
706                 }
707                 case RTE_FLOW_ACTION_TYPE_MARK: {
708                         const struct rte_flow_action_mark *mark =
709                                 (const struct rte_flow_action_mark *)
710                                 actions->conf;
711
712                         /* ENIC_MAGIC_FILTER_ID is reserved and is the highest
713                          * in the range of allows mark ids.
714                          */
715                         if (mark->id >= ENIC_MAGIC_FILTER_ID)
716                                 return EINVAL;
717                         enic_action->filter_id = mark->id;
718                         enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
719                         break;
720                 }
721                 case RTE_FLOW_ACTION_TYPE_FLAG: {
722                         enic_action->filter_id = ENIC_MAGIC_FILTER_ID;
723                         enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
724                         break;
725                 }
726                 case RTE_FLOW_ACTION_TYPE_VOID:
727                         continue;
728                 default:
729                         RTE_ASSERT(0);
730                         break;
731                 }
732         }
733         enic_action->type = FILTER_ACTION_V2;
734         return 0;
735 }
736
737 /** Check if the action is supported */
738 static int
739 enic_match_action(const struct rte_flow_action *action,
740                   const enum rte_flow_action_type *supported_actions)
741 {
742         for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
743              supported_actions++) {
744                 if (action->type == *supported_actions)
745                         return 1;
746         }
747         return 0;
748 }
749
750 /** Get the NIC filter capabilties structure */
751 static const struct enic_filter_cap *
752 enic_get_filter_cap(struct enic *enic)
753 {
754         /* FIXME: only support advanced filters for now */
755         if (enic->flow_filter_mode != FILTER_DPDK_1)
756                 return (const struct enic_filter_cap *)NULL;
757
758         if (enic->flow_filter_mode)
759                 return &enic_filter_cap[enic->flow_filter_mode];
760
761         return NULL;
762 }
763
764 /** Get the actions for this NIC version. */
765 static const struct enic_action_cap *
766 enic_get_action_cap(struct enic *enic)
767 {
768         static const struct enic_action_cap *ea;
769
770         if (enic->filter_tags)
771                 ea = &enic_action_cap[FILTER_ACTION_V2_ALL];
772         return ea;
773 }
774 /**
775  * Internal flow parse/validate function.
776  *
777  * @param dev[in]
778  *   This device pointer.
779  * @param pattern[in]
780  * @param actions[in]
781  * @param error[out]
782  * @param enic_filter[out]
783  *   Internal NIC filter structure pointer.
784  * @param enic_action[out]
785  *   Internal NIC action structure pointer.
786  */
787 static int
788 enic_flow_parse(struct rte_eth_dev *dev,
789                 const struct rte_flow_attr *attrs,
790                 const struct rte_flow_item pattern[],
791                 const struct rte_flow_action actions[],
792                 struct rte_flow_error *error,
793                 struct filter_v2 *enic_filter,
794                 struct filter_action_v2 *enic_action)
795 {
796         unsigned int ret = 0;
797         struct enic *enic = pmd_priv(dev);
798         const struct enic_filter_cap *enic_filter_cap;
799         const struct enic_action_cap *enic_action_cap;
800         const struct rte_flow_action *action;
801
802         FLOW_TRACE();
803
804         memset(enic_filter, 0, sizeof(*enic_filter));
805         memset(enic_action, 0, sizeof(*enic_action));
806
807         if (!pattern) {
808                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
809                                    NULL, "No pattern specified");
810                 return -rte_errno;
811         }
812
813         if (!actions) {
814                 rte_flow_error_set(error, EINVAL,
815                                    RTE_FLOW_ERROR_TYPE_ACTION_NUM,
816                                    NULL, "No action specified");
817                 return -rte_errno;
818         }
819
820         if (attrs) {
821                 if (attrs->group) {
822                         rte_flow_error_set(error, ENOTSUP,
823                                            RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
824                                            NULL,
825                                            "priority groups are not supported");
826                         return -rte_errno;
827                 } else if (attrs->priority) {
828                         rte_flow_error_set(error, ENOTSUP,
829                                            RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
830                                            NULL,
831                                            "priorities are not supported");
832                         return -rte_errno;
833                 } else if (attrs->egress) {
834                         rte_flow_error_set(error, ENOTSUP,
835                                            RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
836                                            NULL,
837                                            "egress is not supported");
838                         return -rte_errno;
839                 } else if (!attrs->ingress) {
840                         rte_flow_error_set(error, ENOTSUP,
841                                            RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
842                                            NULL,
843                                            "only ingress is supported");
844                         return -rte_errno;
845                 }
846
847         } else {
848                 rte_flow_error_set(error, EINVAL,
849                                    RTE_FLOW_ERROR_TYPE_ATTR,
850                                    NULL, "No attribute specified");
851                 return -rte_errno;
852         }
853
854         /* Verify Actions. */
855         enic_action_cap =  enic_get_action_cap(enic);
856         for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
857              action++) {
858                 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
859                         continue;
860                 else if (!enic_match_action(action, enic_action_cap->actions))
861                         break;
862         }
863         if (action->type != RTE_FLOW_ACTION_TYPE_END) {
864                 rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
865                                    action, "Invalid action.");
866                 return -rte_errno;
867         }
868         ret = enic_action_cap->copy_fn(actions, enic_action);
869         if (ret) {
870                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
871                            NULL, "Unsupported action.");
872                 return -rte_errno;
873         }
874
875         /* Verify Flow items. If copying the filter from flow format to enic
876          * format fails, the flow is not supported
877          */
878         enic_filter_cap =  enic_get_filter_cap(enic);
879         if (enic_filter_cap == NULL) {
880                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
881                            NULL, "Flow API not available");
882                 return -rte_errno;
883         }
884         ret = enic_copy_filter(pattern, enic_filter_cap->item_info,
885                                        enic_filter, error);
886         return ret;
887 }
888
889 /**
890  * Push filter/action to the NIC.
891  *
892  * @param enic[in]
893  *   Device structure pointer.
894  * @param enic_filter[in]
895  *   Internal NIC filter structure pointer.
896  * @param enic_action[in]
897  *   Internal NIC action structure pointer.
898  * @param error[out]
899  */
900 static struct rte_flow *
901 enic_flow_add_filter(struct enic *enic, struct filter_v2 *enic_filter,
902                    struct filter_action_v2 *enic_action,
903                    struct rte_flow_error *error)
904 {
905         struct rte_flow *flow;
906         int ret;
907         u16 entry;
908
909         FLOW_TRACE();
910
911         flow = rte_calloc(__func__, 1, sizeof(*flow), 0);
912         if (!flow) {
913                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
914                                    NULL, "cannot allocate flow memory");
915                 return NULL;
916         }
917
918         /* entry[in] is the queue id, entry[out] is the filter Id for delete */
919         entry = enic_action->rq_idx;
920         ret = vnic_dev_classifier(enic->vdev, CLSF_ADD, &entry, enic_filter,
921                                   enic_action);
922         if (!ret) {
923                 flow->enic_filter_id = entry;
924                 flow->enic_filter = *enic_filter;
925         } else {
926                 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
927                                    NULL, "vnic_dev_classifier error");
928                 rte_free(flow);
929                 return NULL;
930         }
931         return flow;
932 }
933
934 /**
935  * Remove filter/action from the NIC.
936  *
937  * @param enic[in]
938  *   Device structure pointer.
939  * @param filter_id[in]
940  *   Id of NIC filter.
941  * @param enic_action[in]
942  *   Internal NIC action structure pointer.
943  * @param error[out]
944  */
945 static int
946 enic_flow_del_filter(struct enic *enic, u16 filter_id,
947                    struct rte_flow_error *error)
948 {
949         int ret;
950
951         FLOW_TRACE();
952
953         ret = vnic_dev_classifier(enic->vdev, CLSF_DEL, &filter_id, NULL, NULL);
954         if (!ret)
955                 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
956                                    NULL, "vnic_dev_classifier failed");
957         return ret;
958 }
959
960 /*
961  * The following functions are callbacks for Generic flow API.
962  */
963
964 /**
965  * Validate a flow supported by the NIC.
966  *
967  * @see rte_flow_validate()
968  * @see rte_flow_ops
969  */
970 static int
971 enic_flow_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attrs,
972                    const struct rte_flow_item pattern[],
973                    const struct rte_flow_action actions[],
974                    struct rte_flow_error *error)
975 {
976         struct filter_v2 enic_filter;
977         struct filter_action_v2 enic_action;
978         int ret;
979
980         FLOW_TRACE();
981
982         ret = enic_flow_parse(dev, attrs, pattern, actions, error,
983                                &enic_filter, &enic_action);
984         return ret;
985 }
986
987 /**
988  * Create a flow supported by the NIC.
989  *
990  * @see rte_flow_create()
991  * @see rte_flow_ops
992  */
993 static struct rte_flow *
994 enic_flow_create(struct rte_eth_dev *dev,
995                  const struct rte_flow_attr *attrs,
996                  const struct rte_flow_item pattern[],
997                  const struct rte_flow_action actions[],
998                  struct rte_flow_error *error)
999 {
1000         int ret;
1001         struct filter_v2 enic_filter;
1002         struct filter_action_v2 enic_action;
1003         struct rte_flow *flow;
1004         struct enic *enic = pmd_priv(dev);
1005
1006         FLOW_TRACE();
1007
1008         ret = enic_flow_parse(dev, attrs, pattern, actions, error, &enic_filter,
1009                               &enic_action);
1010         if (ret < 0)
1011                 return NULL;
1012
1013         rte_spinlock_lock(&enic->flows_lock);
1014         flow = enic_flow_add_filter(enic, &enic_filter, &enic_action,
1015                                     error);
1016         if (flow)
1017                 LIST_INSERT_HEAD(&enic->flows, flow, next);
1018         rte_spinlock_unlock(&enic->flows_lock);
1019
1020         return flow;
1021 }
1022
1023 /**
1024  * Destroy a flow supported by the NIC.
1025  *
1026  * @see rte_flow_destroy()
1027  * @see rte_flow_ops
1028  */
1029 static int
1030 enic_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
1031                   __rte_unused struct rte_flow_error *error)
1032 {
1033         struct enic *enic = pmd_priv(dev);
1034
1035         FLOW_TRACE();
1036
1037         rte_spinlock_lock(&enic->flows_lock);
1038         enic_flow_del_filter(enic, flow->enic_filter_id, error);
1039         LIST_REMOVE(flow, next);
1040         rte_spinlock_unlock(&enic->flows_lock);
1041         return 0;
1042 }
1043
1044 /**
1045  * Flush all flows on the device.
1046  *
1047  * @see rte_flow_flush()
1048  * @see rte_flow_ops
1049  */
1050 static int
1051 enic_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1052 {
1053         struct rte_flow *flow;
1054         struct enic *enic = pmd_priv(dev);
1055
1056         FLOW_TRACE();
1057
1058         rte_spinlock_lock(&enic->flows_lock);
1059
1060         while (!LIST_EMPTY(&enic->flows)) {
1061                 flow = LIST_FIRST(&enic->flows);
1062                 enic_flow_del_filter(enic, flow->enic_filter_id, error);
1063                 LIST_REMOVE(flow, next);
1064         }
1065         rte_spinlock_unlock(&enic->flows_lock);
1066         return 0;
1067 }
1068
1069 /**
1070  * Flow callback registration.
1071  *
1072  * @see rte_flow_ops
1073  */
1074 const struct rte_flow_ops enic_flow_ops = {
1075         .validate = enic_flow_validate,
1076         .create = enic_flow_create,
1077         .destroy = enic_flow_destroy,
1078         .flush = enic_flow_flush,
1079 };