net/enic: fix inner packet matching
[dpdk.git] / drivers / net / enic / enic_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2008-2017 Cisco Systems, Inc.  All rights reserved.
3  */
4
5 #include <errno.h>
6 #include <stdint.h>
7 #include <rte_log.h>
8 #include <rte_ethdev_driver.h>
9 #include <rte_flow_driver.h>
10 #include <rte_ether.h>
11 #include <rte_ip.h>
12 #include <rte_udp.h>
13
14 #include "enic_compat.h"
15 #include "enic.h"
16 #include "vnic_dev.h"
17 #include "vnic_nic.h"
18
19 #define FLOW_TRACE() \
20         rte_log(RTE_LOG_DEBUG, enicpmd_logtype_flow, \
21                 "%s()\n", __func__)
22 #define FLOW_LOG(level, fmt, args...) \
23         rte_log(RTE_LOG_ ## level, enicpmd_logtype_flow, \
24                 fmt "\n", ##args)
25
26 /*
27  * Common arguments passed to copy_item functions. Use this structure
28  * so we can easily add new arguments.
29  * item: Item specification.
30  * filter: Partially filled in NIC filter structure.
31  * inner_ofst: If zero, this is an outer header. If non-zero, this is
32  *   the offset into L5 where the header begins.
33  * l2_proto_off: offset to EtherType eth or vlan header.
34  * l3_proto_off: offset to next protocol field in IPv4 or 6 header.
35  */
36 struct copy_item_args {
37         const struct rte_flow_item *item;
38         struct filter_v2 *filter;
39         uint8_t *inner_ofst;
40         uint8_t l2_proto_off;
41         uint8_t l3_proto_off;
42 };
43
44 /* functions for copying items into enic filters */
45 typedef int (enic_copy_item_fn)(struct copy_item_args *arg);
46
47 /** Info about how to copy items into enic filters. */
48 struct enic_items {
49         /** Function for copying and validating an item. */
50         enic_copy_item_fn *copy_item;
51         /** List of valid previous items. */
52         const enum rte_flow_item_type * const prev_items;
53         /** True if it's OK for this item to be the first item. For some NIC
54          * versions, it's invalid to start the stack above layer 3.
55          */
56         const u8 valid_start_item;
57         /* Inner packet version of copy_item. */
58         enic_copy_item_fn *inner_copy_item;
59 };
60
61 /** Filtering capabilities for various NIC and firmware versions. */
62 struct enic_filter_cap {
63         /** list of valid items and their handlers and attributes. */
64         const struct enic_items *item_info;
65         /* Max type in the above list, used to detect unsupported types */
66         enum rte_flow_item_type max_item_type;
67 };
68
69 /* functions for copying flow actions into enic actions */
70 typedef int (copy_action_fn)(struct enic *enic,
71                              const struct rte_flow_action actions[],
72                              struct filter_action_v2 *enic_action);
73
74 /** Action capabilities for various NICs. */
75 struct enic_action_cap {
76         /** list of valid actions */
77         const enum rte_flow_action_type *actions;
78         /** copy function for a particular NIC */
79         copy_action_fn *copy_fn;
80 };
81
82 /* Forward declarations */
83 static enic_copy_item_fn enic_copy_item_ipv4_v1;
84 static enic_copy_item_fn enic_copy_item_udp_v1;
85 static enic_copy_item_fn enic_copy_item_tcp_v1;
86 static enic_copy_item_fn enic_copy_item_raw_v2;
87 static enic_copy_item_fn enic_copy_item_eth_v2;
88 static enic_copy_item_fn enic_copy_item_vlan_v2;
89 static enic_copy_item_fn enic_copy_item_ipv4_v2;
90 static enic_copy_item_fn enic_copy_item_ipv6_v2;
91 static enic_copy_item_fn enic_copy_item_udp_v2;
92 static enic_copy_item_fn enic_copy_item_tcp_v2;
93 static enic_copy_item_fn enic_copy_item_sctp_v2;
94 static enic_copy_item_fn enic_copy_item_vxlan_v2;
95 static enic_copy_item_fn enic_copy_item_inner_eth_v2;
96 static enic_copy_item_fn enic_copy_item_inner_vlan_v2;
97 static enic_copy_item_fn enic_copy_item_inner_ipv4_v2;
98 static enic_copy_item_fn enic_copy_item_inner_ipv6_v2;
99 static enic_copy_item_fn enic_copy_item_inner_udp_v2;
100 static enic_copy_item_fn enic_copy_item_inner_tcp_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                 .inner_copy_item = NULL,
116         },
117         [RTE_FLOW_ITEM_TYPE_UDP] = {
118                 .copy_item = enic_copy_item_udp_v1,
119                 .valid_start_item = 0,
120                 .prev_items = (const enum rte_flow_item_type[]) {
121                                RTE_FLOW_ITEM_TYPE_IPV4,
122                                RTE_FLOW_ITEM_TYPE_END,
123                 },
124                 .inner_copy_item = NULL,
125         },
126         [RTE_FLOW_ITEM_TYPE_TCP] = {
127                 .copy_item = enic_copy_item_tcp_v1,
128                 .valid_start_item = 0,
129                 .prev_items = (const enum rte_flow_item_type[]) {
130                                RTE_FLOW_ITEM_TYPE_IPV4,
131                                RTE_FLOW_ITEM_TYPE_END,
132                 },
133                 .inner_copy_item = NULL,
134         },
135 };
136
137 /**
138  * NICs have Advanced Filters capability but they are disabled. This means
139  * that layer 3 must be specified.
140  */
141 static const struct enic_items enic_items_v2[] = {
142         [RTE_FLOW_ITEM_TYPE_RAW] = {
143                 .copy_item = enic_copy_item_raw_v2,
144                 .valid_start_item = 0,
145                 .prev_items = (const enum rte_flow_item_type[]) {
146                                RTE_FLOW_ITEM_TYPE_UDP,
147                                RTE_FLOW_ITEM_TYPE_END,
148                 },
149                 .inner_copy_item = NULL,
150         },
151         [RTE_FLOW_ITEM_TYPE_ETH] = {
152                 .copy_item = enic_copy_item_eth_v2,
153                 .valid_start_item = 1,
154                 .prev_items = (const enum rte_flow_item_type[]) {
155                                RTE_FLOW_ITEM_TYPE_VXLAN,
156                                RTE_FLOW_ITEM_TYPE_END,
157                 },
158                 .inner_copy_item = enic_copy_item_inner_eth_v2,
159         },
160         [RTE_FLOW_ITEM_TYPE_VLAN] = {
161                 .copy_item = enic_copy_item_vlan_v2,
162                 .valid_start_item = 1,
163                 .prev_items = (const enum rte_flow_item_type[]) {
164                                RTE_FLOW_ITEM_TYPE_ETH,
165                                RTE_FLOW_ITEM_TYPE_END,
166                 },
167                 .inner_copy_item = enic_copy_item_inner_vlan_v2,
168         },
169         [RTE_FLOW_ITEM_TYPE_IPV4] = {
170                 .copy_item = enic_copy_item_ipv4_v2,
171                 .valid_start_item = 1,
172                 .prev_items = (const enum rte_flow_item_type[]) {
173                                RTE_FLOW_ITEM_TYPE_ETH,
174                                RTE_FLOW_ITEM_TYPE_VLAN,
175                                RTE_FLOW_ITEM_TYPE_END,
176                 },
177                 .inner_copy_item = enic_copy_item_inner_ipv4_v2,
178         },
179         [RTE_FLOW_ITEM_TYPE_IPV6] = {
180                 .copy_item = enic_copy_item_ipv6_v2,
181                 .valid_start_item = 1,
182                 .prev_items = (const enum rte_flow_item_type[]) {
183                                RTE_FLOW_ITEM_TYPE_ETH,
184                                RTE_FLOW_ITEM_TYPE_VLAN,
185                                RTE_FLOW_ITEM_TYPE_END,
186                 },
187                 .inner_copy_item = enic_copy_item_inner_ipv6_v2,
188         },
189         [RTE_FLOW_ITEM_TYPE_UDP] = {
190                 .copy_item = enic_copy_item_udp_v2,
191                 .valid_start_item = 0,
192                 .prev_items = (const enum rte_flow_item_type[]) {
193                                RTE_FLOW_ITEM_TYPE_IPV4,
194                                RTE_FLOW_ITEM_TYPE_IPV6,
195                                RTE_FLOW_ITEM_TYPE_END,
196                 },
197                 .inner_copy_item = enic_copy_item_inner_udp_v2,
198         },
199         [RTE_FLOW_ITEM_TYPE_TCP] = {
200                 .copy_item = enic_copy_item_tcp_v2,
201                 .valid_start_item = 0,
202                 .prev_items = (const enum rte_flow_item_type[]) {
203                                RTE_FLOW_ITEM_TYPE_IPV4,
204                                RTE_FLOW_ITEM_TYPE_IPV6,
205                                RTE_FLOW_ITEM_TYPE_END,
206                 },
207                 .inner_copy_item = enic_copy_item_inner_tcp_v2,
208         },
209         [RTE_FLOW_ITEM_TYPE_SCTP] = {
210                 .copy_item = enic_copy_item_sctp_v2,
211                 .valid_start_item = 0,
212                 .prev_items = (const enum rte_flow_item_type[]) {
213                                RTE_FLOW_ITEM_TYPE_IPV4,
214                                RTE_FLOW_ITEM_TYPE_IPV6,
215                                RTE_FLOW_ITEM_TYPE_END,
216                 },
217                 .inner_copy_item = NULL,
218         },
219         [RTE_FLOW_ITEM_TYPE_VXLAN] = {
220                 .copy_item = enic_copy_item_vxlan_v2,
221                 .valid_start_item = 0,
222                 .prev_items = (const enum rte_flow_item_type[]) {
223                                RTE_FLOW_ITEM_TYPE_UDP,
224                                RTE_FLOW_ITEM_TYPE_END,
225                 },
226                 .inner_copy_item = NULL,
227         },
228 };
229
230 /** NICs with Advanced filters enabled */
231 static const struct enic_items enic_items_v3[] = {
232         [RTE_FLOW_ITEM_TYPE_RAW] = {
233                 .copy_item = enic_copy_item_raw_v2,
234                 .valid_start_item = 0,
235                 .prev_items = (const enum rte_flow_item_type[]) {
236                                RTE_FLOW_ITEM_TYPE_UDP,
237                                RTE_FLOW_ITEM_TYPE_END,
238                 },
239                 .inner_copy_item = NULL,
240         },
241         [RTE_FLOW_ITEM_TYPE_ETH] = {
242                 .copy_item = enic_copy_item_eth_v2,
243                 .valid_start_item = 1,
244                 .prev_items = (const enum rte_flow_item_type[]) {
245                                RTE_FLOW_ITEM_TYPE_VXLAN,
246                                RTE_FLOW_ITEM_TYPE_END,
247                 },
248                 .inner_copy_item = enic_copy_item_inner_eth_v2,
249         },
250         [RTE_FLOW_ITEM_TYPE_VLAN] = {
251                 .copy_item = enic_copy_item_vlan_v2,
252                 .valid_start_item = 1,
253                 .prev_items = (const enum rte_flow_item_type[]) {
254                                RTE_FLOW_ITEM_TYPE_ETH,
255                                RTE_FLOW_ITEM_TYPE_END,
256                 },
257                 .inner_copy_item = enic_copy_item_inner_vlan_v2,
258         },
259         [RTE_FLOW_ITEM_TYPE_IPV4] = {
260                 .copy_item = enic_copy_item_ipv4_v2,
261                 .valid_start_item = 1,
262                 .prev_items = (const enum rte_flow_item_type[]) {
263                                RTE_FLOW_ITEM_TYPE_ETH,
264                                RTE_FLOW_ITEM_TYPE_VLAN,
265                                RTE_FLOW_ITEM_TYPE_END,
266                 },
267                 .inner_copy_item = enic_copy_item_inner_ipv4_v2,
268         },
269         [RTE_FLOW_ITEM_TYPE_IPV6] = {
270                 .copy_item = enic_copy_item_ipv6_v2,
271                 .valid_start_item = 1,
272                 .prev_items = (const enum rte_flow_item_type[]) {
273                                RTE_FLOW_ITEM_TYPE_ETH,
274                                RTE_FLOW_ITEM_TYPE_VLAN,
275                                RTE_FLOW_ITEM_TYPE_END,
276                 },
277                 .inner_copy_item = enic_copy_item_inner_ipv6_v2,
278         },
279         [RTE_FLOW_ITEM_TYPE_UDP] = {
280                 .copy_item = enic_copy_item_udp_v2,
281                 .valid_start_item = 1,
282                 .prev_items = (const enum rte_flow_item_type[]) {
283                                RTE_FLOW_ITEM_TYPE_IPV4,
284                                RTE_FLOW_ITEM_TYPE_IPV6,
285                                RTE_FLOW_ITEM_TYPE_END,
286                 },
287                 .inner_copy_item = enic_copy_item_inner_udp_v2,
288         },
289         [RTE_FLOW_ITEM_TYPE_TCP] = {
290                 .copy_item = enic_copy_item_tcp_v2,
291                 .valid_start_item = 1,
292                 .prev_items = (const enum rte_flow_item_type[]) {
293                                RTE_FLOW_ITEM_TYPE_IPV4,
294                                RTE_FLOW_ITEM_TYPE_IPV6,
295                                RTE_FLOW_ITEM_TYPE_END,
296                 },
297                 .inner_copy_item = enic_copy_item_inner_tcp_v2,
298         },
299         [RTE_FLOW_ITEM_TYPE_SCTP] = {
300                 .copy_item = enic_copy_item_sctp_v2,
301                 .valid_start_item = 0,
302                 .prev_items = (const enum rte_flow_item_type[]) {
303                                RTE_FLOW_ITEM_TYPE_IPV4,
304                                RTE_FLOW_ITEM_TYPE_IPV6,
305                                RTE_FLOW_ITEM_TYPE_END,
306                 },
307                 .inner_copy_item = NULL,
308         },
309         [RTE_FLOW_ITEM_TYPE_VXLAN] = {
310                 .copy_item = enic_copy_item_vxlan_v2,
311                 .valid_start_item = 1,
312                 .prev_items = (const enum rte_flow_item_type[]) {
313                                RTE_FLOW_ITEM_TYPE_UDP,
314                                RTE_FLOW_ITEM_TYPE_END,
315                 },
316                 .inner_copy_item = NULL,
317         },
318 };
319
320 /** Filtering capabilities indexed this NICs supported filter type. */
321 static const struct enic_filter_cap enic_filter_cap[] = {
322         [FILTER_IPV4_5TUPLE] = {
323                 .item_info = enic_items_v1,
324                 .max_item_type = RTE_FLOW_ITEM_TYPE_TCP,
325         },
326         [FILTER_USNIC_IP] = {
327                 .item_info = enic_items_v2,
328                 .max_item_type = RTE_FLOW_ITEM_TYPE_VXLAN,
329         },
330         [FILTER_DPDK_1] = {
331                 .item_info = enic_items_v3,
332                 .max_item_type = RTE_FLOW_ITEM_TYPE_VXLAN,
333         },
334 };
335
336 /** Supported actions for older NICs */
337 static const enum rte_flow_action_type enic_supported_actions_v1[] = {
338         RTE_FLOW_ACTION_TYPE_QUEUE,
339         RTE_FLOW_ACTION_TYPE_END,
340 };
341
342 /** Supported actions for newer NICs */
343 static const enum rte_flow_action_type enic_supported_actions_v2_id[] = {
344         RTE_FLOW_ACTION_TYPE_QUEUE,
345         RTE_FLOW_ACTION_TYPE_MARK,
346         RTE_FLOW_ACTION_TYPE_FLAG,
347         RTE_FLOW_ACTION_TYPE_RSS,
348         RTE_FLOW_ACTION_TYPE_PASSTHRU,
349         RTE_FLOW_ACTION_TYPE_END,
350 };
351
352 static const enum rte_flow_action_type enic_supported_actions_v2_drop[] = {
353         RTE_FLOW_ACTION_TYPE_QUEUE,
354         RTE_FLOW_ACTION_TYPE_MARK,
355         RTE_FLOW_ACTION_TYPE_FLAG,
356         RTE_FLOW_ACTION_TYPE_DROP,
357         RTE_FLOW_ACTION_TYPE_RSS,
358         RTE_FLOW_ACTION_TYPE_PASSTHRU,
359         RTE_FLOW_ACTION_TYPE_END,
360 };
361
362 static const enum rte_flow_action_type enic_supported_actions_v2_count[] = {
363         RTE_FLOW_ACTION_TYPE_QUEUE,
364         RTE_FLOW_ACTION_TYPE_MARK,
365         RTE_FLOW_ACTION_TYPE_FLAG,
366         RTE_FLOW_ACTION_TYPE_DROP,
367         RTE_FLOW_ACTION_TYPE_COUNT,
368         RTE_FLOW_ACTION_TYPE_RSS,
369         RTE_FLOW_ACTION_TYPE_PASSTHRU,
370         RTE_FLOW_ACTION_TYPE_END,
371 };
372
373 /** Action capabilities indexed by NIC version information */
374 static const struct enic_action_cap enic_action_cap[] = {
375         [FILTER_ACTION_RQ_STEERING_FLAG] = {
376                 .actions = enic_supported_actions_v1,
377                 .copy_fn = enic_copy_action_v1,
378         },
379         [FILTER_ACTION_FILTER_ID_FLAG] = {
380                 .actions = enic_supported_actions_v2_id,
381                 .copy_fn = enic_copy_action_v2,
382         },
383         [FILTER_ACTION_DROP_FLAG] = {
384                 .actions = enic_supported_actions_v2_drop,
385                 .copy_fn = enic_copy_action_v2,
386         },
387         [FILTER_ACTION_COUNTER_FLAG] = {
388                 .actions = enic_supported_actions_v2_count,
389                 .copy_fn = enic_copy_action_v2,
390         },
391 };
392
393 static int
394 mask_exact_match(const u8 *supported, const u8 *supplied,
395                  unsigned int size)
396 {
397         unsigned int i;
398         for (i = 0; i < size; i++) {
399                 if (supported[i] != supplied[i])
400                         return 0;
401         }
402         return 1;
403 }
404
405 static int
406 enic_copy_item_ipv4_v1(struct copy_item_args *arg)
407 {
408         const struct rte_flow_item *item = arg->item;
409         struct filter_v2 *enic_filter = arg->filter;
410         const struct rte_flow_item_ipv4 *spec = item->spec;
411         const struct rte_flow_item_ipv4 *mask = item->mask;
412         struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
413         struct ipv4_hdr supported_mask = {
414                 .src_addr = 0xffffffff,
415                 .dst_addr = 0xffffffff,
416         };
417
418         FLOW_TRACE();
419
420         if (!mask)
421                 mask = &rte_flow_item_ipv4_mask;
422
423         /* This is an exact match filter, both fields must be set */
424         if (!spec || !spec->hdr.src_addr || !spec->hdr.dst_addr) {
425                 FLOW_LOG(ERR, "IPv4 exact match src/dst addr");
426                 return ENOTSUP;
427         }
428
429         /* check that the suppied mask exactly matches capabilty */
430         if (!mask_exact_match((const u8 *)&supported_mask,
431                               (const u8 *)item->mask, sizeof(*mask))) {
432                 FLOW_LOG(ERR, "IPv4 exact match mask");
433                 return ENOTSUP;
434         }
435
436         enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
437         enic_5tup->src_addr = spec->hdr.src_addr;
438         enic_5tup->dst_addr = spec->hdr.dst_addr;
439
440         return 0;
441 }
442
443 static int
444 enic_copy_item_udp_v1(struct copy_item_args *arg)
445 {
446         const struct rte_flow_item *item = arg->item;
447         struct filter_v2 *enic_filter = arg->filter;
448         const struct rte_flow_item_udp *spec = item->spec;
449         const struct rte_flow_item_udp *mask = item->mask;
450         struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
451         struct udp_hdr supported_mask = {
452                 .src_port = 0xffff,
453                 .dst_port = 0xffff,
454         };
455
456         FLOW_TRACE();
457
458         if (!mask)
459                 mask = &rte_flow_item_udp_mask;
460
461         /* This is an exact match filter, both ports must be set */
462         if (!spec || !spec->hdr.src_port || !spec->hdr.dst_port) {
463                 FLOW_LOG(ERR, "UDP exact match src/dst addr");
464                 return ENOTSUP;
465         }
466
467         /* check that the suppied mask exactly matches capabilty */
468         if (!mask_exact_match((const u8 *)&supported_mask,
469                               (const u8 *)item->mask, sizeof(*mask))) {
470                 FLOW_LOG(ERR, "UDP exact match mask");
471                 return ENOTSUP;
472         }
473
474         enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
475         enic_5tup->src_port = spec->hdr.src_port;
476         enic_5tup->dst_port = spec->hdr.dst_port;
477         enic_5tup->protocol = PROTO_UDP;
478
479         return 0;
480 }
481
482 static int
483 enic_copy_item_tcp_v1(struct copy_item_args *arg)
484 {
485         const struct rte_flow_item *item = arg->item;
486         struct filter_v2 *enic_filter = arg->filter;
487         const struct rte_flow_item_tcp *spec = item->spec;
488         const struct rte_flow_item_tcp *mask = item->mask;
489         struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
490         struct tcp_hdr supported_mask = {
491                 .src_port = 0xffff,
492                 .dst_port = 0xffff,
493         };
494
495         FLOW_TRACE();
496
497         if (!mask)
498                 mask = &rte_flow_item_tcp_mask;
499
500         /* This is an exact match filter, both ports must be set */
501         if (!spec || !spec->hdr.src_port || !spec->hdr.dst_port) {
502                 FLOW_LOG(ERR, "TCPIPv4 exact match src/dst addr");
503                 return ENOTSUP;
504         }
505
506         /* check that the suppied mask exactly matches capabilty */
507         if (!mask_exact_match((const u8 *)&supported_mask,
508                              (const u8 *)item->mask, sizeof(*mask))) {
509                 FLOW_LOG(ERR, "TCP exact match mask");
510                 return ENOTSUP;
511         }
512
513         enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
514         enic_5tup->src_port = spec->hdr.src_port;
515         enic_5tup->dst_port = spec->hdr.dst_port;
516         enic_5tup->protocol = PROTO_TCP;
517
518         return 0;
519 }
520
521 /*
522  * The common 'copy' function for all inner packet patterns. Patterns are
523  * first appended to the L5 pattern buffer. Then, since the NIC filter
524  * API has no special support for inner packet matching at the moment,
525  * we set EtherType and IP proto as necessary.
526  */
527 static int
528 copy_inner_common(struct filter_generic_1 *gp, uint8_t *inner_ofst,
529                   const void *val, const void *mask, uint8_t val_size,
530                   uint8_t proto_off, uint16_t proto_val, uint8_t proto_size)
531 {
532         uint8_t *l5_mask, *l5_val;
533         uint8_t start_off;
534
535         /* No space left in the L5 pattern buffer. */
536         start_off = *inner_ofst;
537         if ((start_off + val_size) > FILTER_GENERIC_1_KEY_LEN)
538                 return ENOTSUP;
539         l5_mask = gp->layer[FILTER_GENERIC_1_L5].mask;
540         l5_val = gp->layer[FILTER_GENERIC_1_L5].val;
541         /* Copy the pattern into the L5 buffer. */
542         if (val) {
543                 memcpy(l5_mask + start_off, mask, val_size);
544                 memcpy(l5_val + start_off, val, val_size);
545         }
546         /* Set the protocol field in the previous header. */
547         if (proto_off) {
548                 void *m, *v;
549
550                 m = l5_mask + proto_off;
551                 v = l5_val + proto_off;
552                 if (proto_size == 1) {
553                         *(uint8_t *)m = 0xff;
554                         *(uint8_t *)v = (uint8_t)proto_val;
555                 } else if (proto_size == 2) {
556                         *(uint16_t *)m = 0xffff;
557                         *(uint16_t *)v = proto_val;
558                 }
559         }
560         /* All inner headers land in L5 buffer even if their spec is null. */
561         *inner_ofst += val_size;
562         return 0;
563 }
564
565 static int
566 enic_copy_item_inner_eth_v2(struct copy_item_args *arg)
567 {
568         const void *mask = arg->item->mask;
569         uint8_t *off = arg->inner_ofst;
570
571         FLOW_TRACE();
572         if (!mask)
573                 mask = &rte_flow_item_eth_mask;
574         arg->l2_proto_off = *off + offsetof(struct ether_hdr, ether_type);
575         return copy_inner_common(&arg->filter->u.generic_1, off,
576                 arg->item->spec, mask, sizeof(struct ether_hdr),
577                 0 /* no previous protocol */, 0, 0);
578 }
579
580 static int
581 enic_copy_item_inner_vlan_v2(struct copy_item_args *arg)
582 {
583         const void *mask = arg->item->mask;
584         uint8_t *off = arg->inner_ofst;
585         uint8_t eth_type_off;
586
587         FLOW_TRACE();
588         if (!mask)
589                 mask = &rte_flow_item_vlan_mask;
590         /* Append vlan header to L5 and set ether type = TPID */
591         eth_type_off = arg->l2_proto_off;
592         arg->l2_proto_off = *off + offsetof(struct vlan_hdr, eth_proto);
593         return copy_inner_common(&arg->filter->u.generic_1, off,
594                 arg->item->spec, mask, sizeof(struct vlan_hdr),
595                 eth_type_off, rte_cpu_to_be_16(ETHER_TYPE_VLAN), 2);
596 }
597
598 static int
599 enic_copy_item_inner_ipv4_v2(struct copy_item_args *arg)
600 {
601         const void *mask = arg->item->mask;
602         uint8_t *off = arg->inner_ofst;
603
604         FLOW_TRACE();
605         if (!mask)
606                 mask = &rte_flow_item_ipv4_mask;
607         /* Append ipv4 header to L5 and set ether type = ipv4 */
608         arg->l3_proto_off = *off + offsetof(struct ipv4_hdr, next_proto_id);
609         return copy_inner_common(&arg->filter->u.generic_1, off,
610                 arg->item->spec, mask, sizeof(struct ipv4_hdr),
611                 arg->l2_proto_off, rte_cpu_to_be_16(ETHER_TYPE_IPv4), 2);
612 }
613
614 static int
615 enic_copy_item_inner_ipv6_v2(struct copy_item_args *arg)
616 {
617         const void *mask = arg->item->mask;
618         uint8_t *off = arg->inner_ofst;
619
620         FLOW_TRACE();
621         if (!mask)
622                 mask = &rte_flow_item_ipv6_mask;
623         /* Append ipv6 header to L5 and set ether type = ipv6 */
624         arg->l3_proto_off = *off + offsetof(struct ipv6_hdr, proto);
625         return copy_inner_common(&arg->filter->u.generic_1, off,
626                 arg->item->spec, mask, sizeof(struct ipv6_hdr),
627                 arg->l2_proto_off, rte_cpu_to_be_16(ETHER_TYPE_IPv6), 2);
628 }
629
630 static int
631 enic_copy_item_inner_udp_v2(struct copy_item_args *arg)
632 {
633         const void *mask = arg->item->mask;
634         uint8_t *off = arg->inner_ofst;
635
636         FLOW_TRACE();
637         if (!mask)
638                 mask = &rte_flow_item_udp_mask;
639         /* Append udp header to L5 and set ip proto = udp */
640         return copy_inner_common(&arg->filter->u.generic_1, off,
641                 arg->item->spec, mask, sizeof(struct udp_hdr),
642                 arg->l3_proto_off, IPPROTO_UDP, 1);
643 }
644
645 static int
646 enic_copy_item_inner_tcp_v2(struct copy_item_args *arg)
647 {
648         const void *mask = arg->item->mask;
649         uint8_t *off = arg->inner_ofst;
650
651         FLOW_TRACE();
652         if (!mask)
653                 mask = &rte_flow_item_tcp_mask;
654         /* Append tcp header to L5 and set ip proto = tcp */
655         return copy_inner_common(&arg->filter->u.generic_1, off,
656                 arg->item->spec, mask, sizeof(struct tcp_hdr),
657                 arg->l3_proto_off, IPPROTO_TCP, 1);
658 }
659
660 static int
661 enic_copy_item_eth_v2(struct copy_item_args *arg)
662 {
663         const struct rte_flow_item *item = arg->item;
664         struct filter_v2 *enic_filter = arg->filter;
665         struct ether_hdr enic_spec;
666         struct ether_hdr enic_mask;
667         const struct rte_flow_item_eth *spec = item->spec;
668         const struct rte_flow_item_eth *mask = item->mask;
669         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
670
671         FLOW_TRACE();
672
673         /* Match all if no spec */
674         if (!spec)
675                 return 0;
676
677         if (!mask)
678                 mask = &rte_flow_item_eth_mask;
679
680         memcpy(enic_spec.d_addr.addr_bytes, spec->dst.addr_bytes,
681                ETHER_ADDR_LEN);
682         memcpy(enic_spec.s_addr.addr_bytes, spec->src.addr_bytes,
683                ETHER_ADDR_LEN);
684
685         memcpy(enic_mask.d_addr.addr_bytes, mask->dst.addr_bytes,
686                ETHER_ADDR_LEN);
687         memcpy(enic_mask.s_addr.addr_bytes, mask->src.addr_bytes,
688                ETHER_ADDR_LEN);
689         enic_spec.ether_type = spec->type;
690         enic_mask.ether_type = mask->type;
691
692         /* outer header */
693         memcpy(gp->layer[FILTER_GENERIC_1_L2].mask, &enic_mask,
694                sizeof(struct ether_hdr));
695         memcpy(gp->layer[FILTER_GENERIC_1_L2].val, &enic_spec,
696                sizeof(struct ether_hdr));
697         return 0;
698 }
699
700 static int
701 enic_copy_item_vlan_v2(struct copy_item_args *arg)
702 {
703         const struct rte_flow_item *item = arg->item;
704         struct filter_v2 *enic_filter = arg->filter;
705         const struct rte_flow_item_vlan *spec = item->spec;
706         const struct rte_flow_item_vlan *mask = item->mask;
707         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
708         struct ether_hdr *eth_mask;
709         struct ether_hdr *eth_val;
710
711         FLOW_TRACE();
712
713         /* Match all if no spec */
714         if (!spec)
715                 return 0;
716
717         if (!mask)
718                 mask = &rte_flow_item_vlan_mask;
719
720         eth_mask = (void *)gp->layer[FILTER_GENERIC_1_L2].mask;
721         eth_val = (void *)gp->layer[FILTER_GENERIC_1_L2].val;
722         /* Outer TPID cannot be matched */
723         if (eth_mask->ether_type)
724                 return ENOTSUP;
725         /*
726          * When packet matching, the VIC always compares vlan-stripped
727          * L2, regardless of vlan stripping settings. So, the inner type
728          * from vlan becomes the ether type of the eth header.
729          */
730         eth_mask->ether_type = mask->inner_type;
731         eth_val->ether_type = spec->inner_type;
732         /* For TCI, use the vlan mask/val fields (little endian). */
733         gp->mask_vlan = rte_be_to_cpu_16(mask->tci);
734         gp->val_vlan = rte_be_to_cpu_16(spec->tci);
735         return 0;
736 }
737
738 static int
739 enic_copy_item_ipv4_v2(struct copy_item_args *arg)
740 {
741         const struct rte_flow_item *item = arg->item;
742         struct filter_v2 *enic_filter = arg->filter;
743         const struct rte_flow_item_ipv4 *spec = item->spec;
744         const struct rte_flow_item_ipv4 *mask = item->mask;
745         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
746
747         FLOW_TRACE();
748
749         /* Match IPv4 */
750         gp->mask_flags |= FILTER_GENERIC_1_IPV4;
751         gp->val_flags |= FILTER_GENERIC_1_IPV4;
752
753         /* Match all if no spec */
754         if (!spec)
755                 return 0;
756
757         if (!mask)
758                 mask = &rte_flow_item_ipv4_mask;
759
760         memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
761                sizeof(struct ipv4_hdr));
762         memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
763                sizeof(struct ipv4_hdr));
764         return 0;
765 }
766
767 static int
768 enic_copy_item_ipv6_v2(struct copy_item_args *arg)
769 {
770         const struct rte_flow_item *item = arg->item;
771         struct filter_v2 *enic_filter = arg->filter;
772         const struct rte_flow_item_ipv6 *spec = item->spec;
773         const struct rte_flow_item_ipv6 *mask = item->mask;
774         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
775
776         FLOW_TRACE();
777
778         /* Match IPv6 */
779         gp->mask_flags |= FILTER_GENERIC_1_IPV6;
780         gp->val_flags |= FILTER_GENERIC_1_IPV6;
781
782         /* Match all if no spec */
783         if (!spec)
784                 return 0;
785
786         if (!mask)
787                 mask = &rte_flow_item_ipv6_mask;
788
789         memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
790                sizeof(struct ipv6_hdr));
791         memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
792                sizeof(struct ipv6_hdr));
793         return 0;
794 }
795
796 static int
797 enic_copy_item_udp_v2(struct copy_item_args *arg)
798 {
799         const struct rte_flow_item *item = arg->item;
800         struct filter_v2 *enic_filter = arg->filter;
801         const struct rte_flow_item_udp *spec = item->spec;
802         const struct rte_flow_item_udp *mask = item->mask;
803         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
804
805         FLOW_TRACE();
806
807         /* Match UDP */
808         gp->mask_flags |= FILTER_GENERIC_1_UDP;
809         gp->val_flags |= FILTER_GENERIC_1_UDP;
810
811         /* Match all if no spec */
812         if (!spec)
813                 return 0;
814
815         if (!mask)
816                 mask = &rte_flow_item_udp_mask;
817
818         memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
819                sizeof(struct udp_hdr));
820         memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
821                sizeof(struct udp_hdr));
822         return 0;
823 }
824
825 static int
826 enic_copy_item_tcp_v2(struct copy_item_args *arg)
827 {
828         const struct rte_flow_item *item = arg->item;
829         struct filter_v2 *enic_filter = arg->filter;
830         const struct rte_flow_item_tcp *spec = item->spec;
831         const struct rte_flow_item_tcp *mask = item->mask;
832         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
833
834         FLOW_TRACE();
835
836         /* Match TCP */
837         gp->mask_flags |= FILTER_GENERIC_1_TCP;
838         gp->val_flags |= FILTER_GENERIC_1_TCP;
839
840         /* Match all if no spec */
841         if (!spec)
842                 return 0;
843
844         if (!mask)
845                 return ENOTSUP;
846
847         memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
848                sizeof(struct tcp_hdr));
849         memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
850                sizeof(struct tcp_hdr));
851         return 0;
852 }
853
854 static int
855 enic_copy_item_sctp_v2(struct copy_item_args *arg)
856 {
857         const struct rte_flow_item *item = arg->item;
858         struct filter_v2 *enic_filter = arg->filter;
859         const struct rte_flow_item_sctp *spec = item->spec;
860         const struct rte_flow_item_sctp *mask = item->mask;
861         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
862         uint8_t *ip_proto_mask = NULL;
863         uint8_t *ip_proto = NULL;
864
865         FLOW_TRACE();
866
867         /*
868          * The NIC filter API has no flags for "match sctp", so explicitly set
869          * the protocol number in the IP pattern.
870          */
871         if (gp->val_flags & FILTER_GENERIC_1_IPV4) {
872                 struct ipv4_hdr *ip;
873                 ip = (struct ipv4_hdr *)gp->layer[FILTER_GENERIC_1_L3].mask;
874                 ip_proto_mask = &ip->next_proto_id;
875                 ip = (struct ipv4_hdr *)gp->layer[FILTER_GENERIC_1_L3].val;
876                 ip_proto = &ip->next_proto_id;
877         } else if (gp->val_flags & FILTER_GENERIC_1_IPV6) {
878                 struct ipv6_hdr *ip;
879                 ip = (struct ipv6_hdr *)gp->layer[FILTER_GENERIC_1_L3].mask;
880                 ip_proto_mask = &ip->proto;
881                 ip = (struct ipv6_hdr *)gp->layer[FILTER_GENERIC_1_L3].val;
882                 ip_proto = &ip->proto;
883         } else {
884                 /* Need IPv4/IPv6 pattern first */
885                 return EINVAL;
886         }
887         *ip_proto = IPPROTO_SCTP;
888         *ip_proto_mask = 0xff;
889
890         /* Match all if no spec */
891         if (!spec)
892                 return 0;
893
894         if (!mask)
895                 mask = &rte_flow_item_sctp_mask;
896
897         memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
898                sizeof(struct sctp_hdr));
899         memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
900                sizeof(struct sctp_hdr));
901         return 0;
902 }
903
904 static int
905 enic_copy_item_vxlan_v2(struct copy_item_args *arg)
906 {
907         const struct rte_flow_item *item = arg->item;
908         struct filter_v2 *enic_filter = arg->filter;
909         uint8_t *inner_ofst = arg->inner_ofst;
910         const struct rte_flow_item_vxlan *spec = item->spec;
911         const struct rte_flow_item_vxlan *mask = item->mask;
912         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
913         struct udp_hdr *udp;
914
915         FLOW_TRACE();
916
917         /*
918          * The NIC filter API has no flags for "match vxlan". Set UDP port to
919          * avoid false positives.
920          */
921         gp->mask_flags |= FILTER_GENERIC_1_UDP;
922         gp->val_flags |= FILTER_GENERIC_1_UDP;
923         udp = (struct udp_hdr *)gp->layer[FILTER_GENERIC_1_L4].mask;
924         udp->dst_port = 0xffff;
925         udp = (struct udp_hdr *)gp->layer[FILTER_GENERIC_1_L4].val;
926         udp->dst_port = RTE_BE16(4789);
927         /* Match all if no spec */
928         if (!spec)
929                 return 0;
930
931         if (!mask)
932                 mask = &rte_flow_item_vxlan_mask;
933
934         memcpy(gp->layer[FILTER_GENERIC_1_L5].mask, mask,
935                sizeof(struct vxlan_hdr));
936         memcpy(gp->layer[FILTER_GENERIC_1_L5].val, spec,
937                sizeof(struct vxlan_hdr));
938
939         *inner_ofst = sizeof(struct vxlan_hdr);
940         return 0;
941 }
942
943 /*
944  * Copy raw item into version 2 NIC filter. Currently, raw pattern match is
945  * very limited. It is intended for matching UDP tunnel header (e.g. vxlan
946  * or geneve).
947  */
948 static int
949 enic_copy_item_raw_v2(struct copy_item_args *arg)
950 {
951         const struct rte_flow_item *item = arg->item;
952         struct filter_v2 *enic_filter = arg->filter;
953         uint8_t *inner_ofst = arg->inner_ofst;
954         const struct rte_flow_item_raw *spec = item->spec;
955         const struct rte_flow_item_raw *mask = item->mask;
956         struct filter_generic_1 *gp = &enic_filter->u.generic_1;
957
958         FLOW_TRACE();
959
960         /* Cannot be used for inner packet */
961         if (*inner_ofst)
962                 return EINVAL;
963         /* Need both spec and mask */
964         if (!spec || !mask)
965                 return EINVAL;
966         /* Only supports relative with offset 0 */
967         if (!spec->relative || spec->offset != 0 || spec->search || spec->limit)
968                 return EINVAL;
969         /* Need non-null pattern that fits within the NIC's filter pattern */
970         if (spec->length == 0 || spec->length > FILTER_GENERIC_1_KEY_LEN ||
971             !spec->pattern || !mask->pattern)
972                 return EINVAL;
973         /*
974          * Mask fields, including length, are often set to zero. Assume that
975          * means "same as spec" to avoid breaking existing apps. If length
976          * is not zero, then it should be >= spec length.
977          *
978          * No more pattern follows this, so append to the L4 layer instead of
979          * L5 to work with both recent and older VICs.
980          */
981         if (mask->length != 0 && mask->length < spec->length)
982                 return EINVAL;
983         memcpy(gp->layer[FILTER_GENERIC_1_L4].mask + sizeof(struct udp_hdr),
984                mask->pattern, spec->length);
985         memcpy(gp->layer[FILTER_GENERIC_1_L4].val + sizeof(struct udp_hdr),
986                spec->pattern, spec->length);
987
988         return 0;
989 }
990
991 /**
992  * Return 1 if current item is valid on top of the previous one.
993  *
994  * @param prev_item[in]
995  *   The item before this one in the pattern or RTE_FLOW_ITEM_TYPE_END if this
996  *   is the first item.
997  * @param item_info[in]
998  *   Info about this item, like valid previous items.
999  * @param is_first[in]
1000  *   True if this the first item in the pattern.
1001  */
1002 static int
1003 item_stacking_valid(enum rte_flow_item_type prev_item,
1004                     const struct enic_items *item_info, u8 is_first_item)
1005 {
1006         enum rte_flow_item_type const *allowed_items = item_info->prev_items;
1007
1008         FLOW_TRACE();
1009
1010         for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
1011                 if (prev_item == *allowed_items)
1012                         return 1;
1013         }
1014
1015         /* This is the first item in the stack. Check if that's cool */
1016         if (is_first_item && item_info->valid_start_item)
1017                 return 1;
1018
1019         return 0;
1020 }
1021
1022 /*
1023  * Fix up the L5 layer.. HW vxlan parsing removes vxlan header from L5.
1024  * Instead it is in L4 following the UDP header. Append the vxlan
1025  * pattern to L4 (udp) and shift any inner packet pattern in L5.
1026  */
1027 static void
1028 fixup_l5_layer(struct enic *enic, struct filter_generic_1 *gp,
1029                uint8_t inner_ofst)
1030 {
1031         uint8_t layer[FILTER_GENERIC_1_KEY_LEN];
1032         uint8_t inner;
1033         uint8_t vxlan;
1034
1035         if (!(inner_ofst > 0 && enic->vxlan))
1036                 return;
1037         FLOW_TRACE();
1038         vxlan = sizeof(struct vxlan_hdr);
1039         memcpy(gp->layer[FILTER_GENERIC_1_L4].mask + sizeof(struct udp_hdr),
1040                gp->layer[FILTER_GENERIC_1_L5].mask, vxlan);
1041         memcpy(gp->layer[FILTER_GENERIC_1_L4].val + sizeof(struct udp_hdr),
1042                gp->layer[FILTER_GENERIC_1_L5].val, vxlan);
1043         inner = inner_ofst - vxlan;
1044         memset(layer, 0, sizeof(layer));
1045         memcpy(layer, gp->layer[FILTER_GENERIC_1_L5].mask + vxlan, inner);
1046         memcpy(gp->layer[FILTER_GENERIC_1_L5].mask, layer, sizeof(layer));
1047         memset(layer, 0, sizeof(layer));
1048         memcpy(layer, gp->layer[FILTER_GENERIC_1_L5].val + vxlan, inner);
1049         memcpy(gp->layer[FILTER_GENERIC_1_L5].val, layer, sizeof(layer));
1050 }
1051
1052 /**
1053  * Build the intenal enic filter structure from the provided pattern. The
1054  * pattern is validated as the items are copied.
1055  *
1056  * @param pattern[in]
1057  * @param items_info[in]
1058  *   Info about this NICs item support, like valid previous items.
1059  * @param enic_filter[out]
1060  *   NIC specfilc filters derived from the pattern.
1061  * @param error[out]
1062  */
1063 static int
1064 enic_copy_filter(const struct rte_flow_item pattern[],
1065                  const struct enic_filter_cap *cap,
1066                  struct enic *enic,
1067                  struct filter_v2 *enic_filter,
1068                  struct rte_flow_error *error)
1069 {
1070         int ret;
1071         const struct rte_flow_item *item = pattern;
1072         u8 inner_ofst = 0; /* If encapsulated, ofst into L5 */
1073         enum rte_flow_item_type prev_item;
1074         const struct enic_items *item_info;
1075         struct copy_item_args args;
1076         enic_copy_item_fn *copy_fn;
1077         u8 is_first_item = 1;
1078
1079         FLOW_TRACE();
1080
1081         prev_item = 0;
1082
1083         args.filter = enic_filter;
1084         args.inner_ofst = &inner_ofst;
1085         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1086                 /* Get info about how to validate and copy the item. If NULL
1087                  * is returned the nic does not support the item.
1088                  */
1089                 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
1090                         continue;
1091
1092                 item_info = &cap->item_info[item->type];
1093                 if (item->type > cap->max_item_type ||
1094                     item_info->copy_item == NULL ||
1095                     (inner_ofst > 0 && item_info->inner_copy_item == NULL)) {
1096                         rte_flow_error_set(error, ENOTSUP,
1097                                 RTE_FLOW_ERROR_TYPE_ITEM,
1098                                 NULL, "Unsupported item.");
1099                         return -rte_errno;
1100                 }
1101
1102                 /* check to see if item stacking is valid */
1103                 if (!item_stacking_valid(prev_item, item_info, is_first_item))
1104                         goto stacking_error;
1105
1106                 args.item = item;
1107                 copy_fn = inner_ofst > 0 ? item_info->inner_copy_item :
1108                         item_info->copy_item;
1109                 ret = copy_fn(&args);
1110                 if (ret)
1111                         goto item_not_supported;
1112                 prev_item = item->type;
1113                 is_first_item = 0;
1114         }
1115         fixup_l5_layer(enic, &enic_filter->u.generic_1, inner_ofst);
1116
1117         return 0;
1118
1119 item_not_supported:
1120         rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_ITEM,
1121                            NULL, "enic type error");
1122         return -rte_errno;
1123
1124 stacking_error:
1125         rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
1126                            item, "stacking error");
1127         return -rte_errno;
1128 }
1129
1130 /**
1131  * Build the intenal version 1 NIC action structure from the provided pattern.
1132  * The pattern is validated as the items are copied.
1133  *
1134  * @param actions[in]
1135  * @param enic_action[out]
1136  *   NIC specfilc actions derived from the actions.
1137  * @param error[out]
1138  */
1139 static int
1140 enic_copy_action_v1(__rte_unused struct enic *enic,
1141                     const struct rte_flow_action actions[],
1142                     struct filter_action_v2 *enic_action)
1143 {
1144         enum { FATE = 1, };
1145         uint32_t overlap = 0;
1146
1147         FLOW_TRACE();
1148
1149         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1150                 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID)
1151                         continue;
1152
1153                 switch (actions->type) {
1154                 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1155                         const struct rte_flow_action_queue *queue =
1156                                 (const struct rte_flow_action_queue *)
1157                                 actions->conf;
1158
1159                         if (overlap & FATE)
1160                                 return ENOTSUP;
1161                         overlap |= FATE;
1162                         enic_action->rq_idx =
1163                                 enic_rte_rq_idx_to_sop_idx(queue->index);
1164                         break;
1165                 }
1166                 default:
1167                         RTE_ASSERT(0);
1168                         break;
1169                 }
1170         }
1171         if (!(overlap & FATE))
1172                 return ENOTSUP;
1173         enic_action->type = FILTER_ACTION_RQ_STEERING;
1174         return 0;
1175 }
1176
1177 /**
1178  * Build the intenal version 2 NIC action structure from the provided pattern.
1179  * The pattern is validated as the items are copied.
1180  *
1181  * @param actions[in]
1182  * @param enic_action[out]
1183  *   NIC specfilc actions derived from the actions.
1184  * @param error[out]
1185  */
1186 static int
1187 enic_copy_action_v2(struct enic *enic,
1188                     const struct rte_flow_action actions[],
1189                     struct filter_action_v2 *enic_action)
1190 {
1191         enum { FATE = 1, MARK = 2, };
1192         uint32_t overlap = 0;
1193         bool passthru = false;
1194
1195         FLOW_TRACE();
1196
1197         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1198                 switch (actions->type) {
1199                 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1200                         const struct rte_flow_action_queue *queue =
1201                                 (const struct rte_flow_action_queue *)
1202                                 actions->conf;
1203
1204                         if (overlap & FATE)
1205                                 return ENOTSUP;
1206                         overlap |= FATE;
1207                         enic_action->rq_idx =
1208                                 enic_rte_rq_idx_to_sop_idx(queue->index);
1209                         enic_action->flags |= FILTER_ACTION_RQ_STEERING_FLAG;
1210                         break;
1211                 }
1212                 case RTE_FLOW_ACTION_TYPE_MARK: {
1213                         const struct rte_flow_action_mark *mark =
1214                                 (const struct rte_flow_action_mark *)
1215                                 actions->conf;
1216
1217                         if (overlap & MARK)
1218                                 return ENOTSUP;
1219                         overlap |= MARK;
1220                         /*
1221                          * Map mark ID (32-bit) to filter ID (16-bit):
1222                          * - Reject values > 16 bits
1223                          * - Filter ID 0 is reserved for filters that steer
1224                          *   but not mark. So add 1 to the mark ID to avoid
1225                          *   using 0.
1226                          * - Filter ID (ENIC_MAGIC_FILTER_ID = 0xffff) is
1227                          *   reserved for the "flag" action below.
1228                          */
1229                         if (mark->id >= ENIC_MAGIC_FILTER_ID - 1)
1230                                 return EINVAL;
1231                         enic_action->filter_id = mark->id + 1;
1232                         enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1233                         break;
1234                 }
1235                 case RTE_FLOW_ACTION_TYPE_FLAG: {
1236                         if (overlap & MARK)
1237                                 return ENOTSUP;
1238                         overlap |= MARK;
1239                         /* ENIC_MAGIC_FILTER_ID is reserved for flagging */
1240                         enic_action->filter_id = ENIC_MAGIC_FILTER_ID;
1241                         enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1242                         break;
1243                 }
1244                 case RTE_FLOW_ACTION_TYPE_DROP: {
1245                         if (overlap & FATE)
1246                                 return ENOTSUP;
1247                         overlap |= FATE;
1248                         enic_action->flags |= FILTER_ACTION_DROP_FLAG;
1249                         break;
1250                 }
1251                 case RTE_FLOW_ACTION_TYPE_COUNT: {
1252                         enic_action->flags |= FILTER_ACTION_COUNTER_FLAG;
1253                         break;
1254                 }
1255                 case RTE_FLOW_ACTION_TYPE_RSS: {
1256                         const struct rte_flow_action_rss *rss =
1257                                 (const struct rte_flow_action_rss *)
1258                                 actions->conf;
1259                         bool allow;
1260                         uint16_t i;
1261
1262                         /*
1263                          * Hardware does not support general RSS actions, but
1264                          * we can still support the dummy one that is used to
1265                          * "receive normally".
1266                          */
1267                         allow = rss->func == RTE_ETH_HASH_FUNCTION_DEFAULT &&
1268                                 rss->level == 0 &&
1269                                 (rss->types == 0 ||
1270                                  rss->types == enic->rss_hf) &&
1271                                 rss->queue_num == enic->rq_count &&
1272                                 rss->key_len == 0;
1273                         /* Identity queue map is ok */
1274                         for (i = 0; i < rss->queue_num; i++)
1275                                 allow = allow && (i == rss->queue[i]);
1276                         if (!allow)
1277                                 return ENOTSUP;
1278                         if (overlap & FATE)
1279                                 return ENOTSUP;
1280                         /* Need MARK or FLAG */
1281                         if (!(overlap & MARK))
1282                                 return ENOTSUP;
1283                         overlap |= FATE;
1284                         break;
1285                 }
1286                 case RTE_FLOW_ACTION_TYPE_PASSTHRU: {
1287                         /*
1288                          * Like RSS above, PASSTHRU + MARK may be used to
1289                          * "mark and then receive normally". MARK usually comes
1290                          * after PASSTHRU, so remember we have seen passthru
1291                          * and check for mark later.
1292                          */
1293                         if (overlap & FATE)
1294                                 return ENOTSUP;
1295                         overlap |= FATE;
1296                         passthru = true;
1297                         break;
1298                 }
1299                 case RTE_FLOW_ACTION_TYPE_VOID:
1300                         continue;
1301                 default:
1302                         RTE_ASSERT(0);
1303                         break;
1304                 }
1305         }
1306         /* Only PASSTHRU + MARK is allowed */
1307         if (passthru && !(overlap & MARK))
1308                 return ENOTSUP;
1309         if (!(overlap & FATE))
1310                 return ENOTSUP;
1311         enic_action->type = FILTER_ACTION_V2;
1312         return 0;
1313 }
1314
1315 /** Check if the action is supported */
1316 static int
1317 enic_match_action(const struct rte_flow_action *action,
1318                   const enum rte_flow_action_type *supported_actions)
1319 {
1320         for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
1321              supported_actions++) {
1322                 if (action->type == *supported_actions)
1323                         return 1;
1324         }
1325         return 0;
1326 }
1327
1328 /** Get the NIC filter capabilties structure */
1329 static const struct enic_filter_cap *
1330 enic_get_filter_cap(struct enic *enic)
1331 {
1332         if (enic->flow_filter_mode)
1333                 return &enic_filter_cap[enic->flow_filter_mode];
1334
1335         return NULL;
1336 }
1337
1338 /** Get the actions for this NIC version. */
1339 static const struct enic_action_cap *
1340 enic_get_action_cap(struct enic *enic)
1341 {
1342         const struct enic_action_cap *ea;
1343         uint8_t actions;
1344
1345         actions = enic->filter_actions;
1346         if (actions & FILTER_ACTION_COUNTER_FLAG)
1347                 ea = &enic_action_cap[FILTER_ACTION_COUNTER_FLAG];
1348         else if (actions & FILTER_ACTION_DROP_FLAG)
1349                 ea = &enic_action_cap[FILTER_ACTION_DROP_FLAG];
1350         else if (actions & FILTER_ACTION_FILTER_ID_FLAG)
1351                 ea = &enic_action_cap[FILTER_ACTION_FILTER_ID_FLAG];
1352         else
1353                 ea = &enic_action_cap[FILTER_ACTION_RQ_STEERING_FLAG];
1354         return ea;
1355 }
1356
1357 /* Debug function to dump internal NIC action structure. */
1358 static void
1359 enic_dump_actions(const struct filter_action_v2 *ea)
1360 {
1361         if (ea->type == FILTER_ACTION_RQ_STEERING) {
1362                 FLOW_LOG(INFO, "Action(V1), queue: %u\n", ea->rq_idx);
1363         } else if (ea->type == FILTER_ACTION_V2) {
1364                 FLOW_LOG(INFO, "Actions(V2)\n");
1365                 if (ea->flags & FILTER_ACTION_RQ_STEERING_FLAG)
1366                         FLOW_LOG(INFO, "\tqueue: %u\n",
1367                                enic_sop_rq_idx_to_rte_idx(ea->rq_idx));
1368                 if (ea->flags & FILTER_ACTION_FILTER_ID_FLAG)
1369                         FLOW_LOG(INFO, "\tfilter_id: %u\n", ea->filter_id);
1370         }
1371 }
1372
1373 /* Debug function to dump internal NIC filter structure. */
1374 static void
1375 enic_dump_filter(const struct filter_v2 *filt)
1376 {
1377         const struct filter_generic_1 *gp;
1378         int i, j, mbyte;
1379         char buf[128], *bp;
1380         char ip4[16], ip6[16], udp[16], tcp[16], tcpudp[16], ip4csum[16];
1381         char l4csum[16], ipfrag[16];
1382
1383         switch (filt->type) {
1384         case FILTER_IPV4_5TUPLE:
1385                 FLOW_LOG(INFO, "FILTER_IPV4_5TUPLE\n");
1386                 break;
1387         case FILTER_USNIC_IP:
1388         case FILTER_DPDK_1:
1389                 /* FIXME: this should be a loop */
1390                 gp = &filt->u.generic_1;
1391                 FLOW_LOG(INFO, "Filter: vlan: 0x%04x, mask: 0x%04x\n",
1392                        gp->val_vlan, gp->mask_vlan);
1393
1394                 if (gp->mask_flags & FILTER_GENERIC_1_IPV4)
1395                         sprintf(ip4, "%s ",
1396                                 (gp->val_flags & FILTER_GENERIC_1_IPV4)
1397                                  ? "ip4(y)" : "ip4(n)");
1398                 else
1399                         sprintf(ip4, "%s ", "ip4(x)");
1400
1401                 if (gp->mask_flags & FILTER_GENERIC_1_IPV6)
1402                         sprintf(ip6, "%s ",
1403                                 (gp->val_flags & FILTER_GENERIC_1_IPV4)
1404                                  ? "ip6(y)" : "ip6(n)");
1405                 else
1406                         sprintf(ip6, "%s ", "ip6(x)");
1407
1408                 if (gp->mask_flags & FILTER_GENERIC_1_UDP)
1409                         sprintf(udp, "%s ",
1410                                 (gp->val_flags & FILTER_GENERIC_1_UDP)
1411                                  ? "udp(y)" : "udp(n)");
1412                 else
1413                         sprintf(udp, "%s ", "udp(x)");
1414
1415                 if (gp->mask_flags & FILTER_GENERIC_1_TCP)
1416                         sprintf(tcp, "%s ",
1417                                 (gp->val_flags & FILTER_GENERIC_1_TCP)
1418                                  ? "tcp(y)" : "tcp(n)");
1419                 else
1420                         sprintf(tcp, "%s ", "tcp(x)");
1421
1422                 if (gp->mask_flags & FILTER_GENERIC_1_TCP_OR_UDP)
1423                         sprintf(tcpudp, "%s ",
1424                                 (gp->val_flags & FILTER_GENERIC_1_TCP_OR_UDP)
1425                                  ? "tcpudp(y)" : "tcpudp(n)");
1426                 else
1427                         sprintf(tcpudp, "%s ", "tcpudp(x)");
1428
1429                 if (gp->mask_flags & FILTER_GENERIC_1_IP4SUM_OK)
1430                         sprintf(ip4csum, "%s ",
1431                                 (gp->val_flags & FILTER_GENERIC_1_IP4SUM_OK)
1432                                  ? "ip4csum(y)" : "ip4csum(n)");
1433                 else
1434                         sprintf(ip4csum, "%s ", "ip4csum(x)");
1435
1436                 if (gp->mask_flags & FILTER_GENERIC_1_L4SUM_OK)
1437                         sprintf(l4csum, "%s ",
1438                                 (gp->val_flags & FILTER_GENERIC_1_L4SUM_OK)
1439                                  ? "l4csum(y)" : "l4csum(n)");
1440                 else
1441                         sprintf(l4csum, "%s ", "l4csum(x)");
1442
1443                 if (gp->mask_flags & FILTER_GENERIC_1_IPFRAG)
1444                         sprintf(ipfrag, "%s ",
1445                                 (gp->val_flags & FILTER_GENERIC_1_IPFRAG)
1446                                  ? "ipfrag(y)" : "ipfrag(n)");
1447                 else
1448                         sprintf(ipfrag, "%s ", "ipfrag(x)");
1449                 FLOW_LOG(INFO, "\tFlags: %s%s%s%s%s%s%s%s\n", ip4, ip6, udp,
1450                          tcp, tcpudp, ip4csum, l4csum, ipfrag);
1451
1452                 for (i = 0; i < FILTER_GENERIC_1_NUM_LAYERS; i++) {
1453                         mbyte = FILTER_GENERIC_1_KEY_LEN - 1;
1454                         while (mbyte && !gp->layer[i].mask[mbyte])
1455                                 mbyte--;
1456                         if (mbyte == 0)
1457                                 continue;
1458
1459                         bp = buf;
1460                         for (j = 0; j <= mbyte; j++) {
1461                                 sprintf(bp, "%02x",
1462                                         gp->layer[i].mask[j]);
1463                                 bp += 2;
1464                         }
1465                         *bp = '\0';
1466                         FLOW_LOG(INFO, "\tL%u mask: %s\n", i + 2, buf);
1467                         bp = buf;
1468                         for (j = 0; j <= mbyte; j++) {
1469                                 sprintf(bp, "%02x",
1470                                         gp->layer[i].val[j]);
1471                                 bp += 2;
1472                         }
1473                         *bp = '\0';
1474                         FLOW_LOG(INFO, "\tL%u  val: %s\n", i + 2, buf);
1475                 }
1476                 break;
1477         default:
1478                 FLOW_LOG(INFO, "FILTER UNKNOWN\n");
1479                 break;
1480         }
1481 }
1482
1483 /* Debug function to dump internal NIC flow structures. */
1484 static void
1485 enic_dump_flow(const struct filter_action_v2 *ea, const struct filter_v2 *filt)
1486 {
1487         enic_dump_filter(filt);
1488         enic_dump_actions(ea);
1489 }
1490
1491
1492 /**
1493  * Internal flow parse/validate function.
1494  *
1495  * @param dev[in]
1496  *   This device pointer.
1497  * @param pattern[in]
1498  * @param actions[in]
1499  * @param error[out]
1500  * @param enic_filter[out]
1501  *   Internal NIC filter structure pointer.
1502  * @param enic_action[out]
1503  *   Internal NIC action structure pointer.
1504  */
1505 static int
1506 enic_flow_parse(struct rte_eth_dev *dev,
1507                 const struct rte_flow_attr *attrs,
1508                 const struct rte_flow_item pattern[],
1509                 const struct rte_flow_action actions[],
1510                 struct rte_flow_error *error,
1511                 struct filter_v2 *enic_filter,
1512                 struct filter_action_v2 *enic_action)
1513 {
1514         unsigned int ret = 0;
1515         struct enic *enic = pmd_priv(dev);
1516         const struct enic_filter_cap *enic_filter_cap;
1517         const struct enic_action_cap *enic_action_cap;
1518         const struct rte_flow_action *action;
1519
1520         FLOW_TRACE();
1521
1522         memset(enic_filter, 0, sizeof(*enic_filter));
1523         memset(enic_action, 0, sizeof(*enic_action));
1524
1525         if (!pattern) {
1526                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1527                                    NULL, "No pattern specified");
1528                 return -rte_errno;
1529         }
1530
1531         if (!actions) {
1532                 rte_flow_error_set(error, EINVAL,
1533                                    RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1534                                    NULL, "No action specified");
1535                 return -rte_errno;
1536         }
1537
1538         if (attrs) {
1539                 if (attrs->group) {
1540                         rte_flow_error_set(error, ENOTSUP,
1541                                            RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1542                                            NULL,
1543                                            "priority groups are not supported");
1544                         return -rte_errno;
1545                 } else if (attrs->priority) {
1546                         rte_flow_error_set(error, ENOTSUP,
1547                                            RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1548                                            NULL,
1549                                            "priorities are not supported");
1550                         return -rte_errno;
1551                 } else if (attrs->egress) {
1552                         rte_flow_error_set(error, ENOTSUP,
1553                                            RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
1554                                            NULL,
1555                                            "egress is not supported");
1556                         return -rte_errno;
1557                 } else if (attrs->transfer) {
1558                         rte_flow_error_set(error, ENOTSUP,
1559                                            RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1560                                            NULL,
1561                                            "transfer is not supported");
1562                         return -rte_errno;
1563                 } else if (!attrs->ingress) {
1564                         rte_flow_error_set(error, ENOTSUP,
1565                                            RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1566                                            NULL,
1567                                            "only ingress is supported");
1568                         return -rte_errno;
1569                 }
1570
1571         } else {
1572                 rte_flow_error_set(error, EINVAL,
1573                                    RTE_FLOW_ERROR_TYPE_ATTR,
1574                                    NULL, "No attribute specified");
1575                 return -rte_errno;
1576         }
1577
1578         /* Verify Actions. */
1579         enic_action_cap =  enic_get_action_cap(enic);
1580         for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
1581              action++) {
1582                 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1583                         continue;
1584                 else if (!enic_match_action(action, enic_action_cap->actions))
1585                         break;
1586         }
1587         if (action->type != RTE_FLOW_ACTION_TYPE_END) {
1588                 rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
1589                                    action, "Invalid action.");
1590                 return -rte_errno;
1591         }
1592         ret = enic_action_cap->copy_fn(enic, actions, enic_action);
1593         if (ret) {
1594                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1595                            NULL, "Unsupported action.");
1596                 return -rte_errno;
1597         }
1598
1599         /* Verify Flow items. If copying the filter from flow format to enic
1600          * format fails, the flow is not supported
1601          */
1602         enic_filter_cap =  enic_get_filter_cap(enic);
1603         if (enic_filter_cap == NULL) {
1604                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1605                            NULL, "Flow API not available");
1606                 return -rte_errno;
1607         }
1608         enic_filter->type = enic->flow_filter_mode;
1609         ret = enic_copy_filter(pattern, enic_filter_cap, enic,
1610                                        enic_filter, error);
1611         return ret;
1612 }
1613
1614 /**
1615  * Push filter/action to the NIC.
1616  *
1617  * @param enic[in]
1618  *   Device structure pointer.
1619  * @param enic_filter[in]
1620  *   Internal NIC filter structure pointer.
1621  * @param enic_action[in]
1622  *   Internal NIC action structure pointer.
1623  * @param error[out]
1624  */
1625 static struct rte_flow *
1626 enic_flow_add_filter(struct enic *enic, struct filter_v2 *enic_filter,
1627                    struct filter_action_v2 *enic_action,
1628                    struct rte_flow_error *error)
1629 {
1630         struct rte_flow *flow;
1631         int err;
1632         uint16_t entry;
1633         int ctr_idx;
1634         int last_max_flow_ctr;
1635
1636         FLOW_TRACE();
1637
1638         flow = rte_calloc(__func__, 1, sizeof(*flow), 0);
1639         if (!flow) {
1640                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1641                                    NULL, "cannot allocate flow memory");
1642                 return NULL;
1643         }
1644
1645         flow->counter_idx = -1;
1646         last_max_flow_ctr = -1;
1647         if (enic_action->flags & FILTER_ACTION_COUNTER_FLAG) {
1648                 if (!vnic_dev_counter_alloc(enic->vdev, (uint32_t *)&ctr_idx)) {
1649                         rte_flow_error_set(error, ENOMEM,
1650                                            RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1651                                            NULL, "cannot allocate counter");
1652                         goto unwind_flow_alloc;
1653                 }
1654                 flow->counter_idx = ctr_idx;
1655                 enic_action->counter_index = ctr_idx;
1656
1657                 /* If index is the largest, increase the counter DMA size */
1658                 if (ctr_idx > enic->max_flow_counter) {
1659                         err = vnic_dev_counter_dma_cfg(enic->vdev,
1660                                                  VNIC_FLOW_COUNTER_UPDATE_MSECS,
1661                                                  ctr_idx + 1);
1662                         if (err) {
1663                                 rte_flow_error_set(error, -err,
1664                                            RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1665                                            NULL, "counter DMA config failed");
1666                                 goto unwind_ctr_alloc;
1667                         }
1668                         last_max_flow_ctr = enic->max_flow_counter;
1669                         enic->max_flow_counter = ctr_idx;
1670                 }
1671         }
1672
1673         /* entry[in] is the queue id, entry[out] is the filter Id for delete */
1674         entry = enic_action->rq_idx;
1675         err = vnic_dev_classifier(enic->vdev, CLSF_ADD, &entry, enic_filter,
1676                                   enic_action);
1677         if (err) {
1678                 rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_HANDLE,
1679                                    NULL, "vnic_dev_classifier error");
1680                 goto unwind_ctr_dma_cfg;
1681         }
1682
1683         flow->enic_filter_id = entry;
1684         flow->enic_filter = *enic_filter;
1685
1686         return flow;
1687
1688 /* unwind if there are errors */
1689 unwind_ctr_dma_cfg:
1690         if (last_max_flow_ctr != -1) {
1691                 /* reduce counter DMA size */
1692                 vnic_dev_counter_dma_cfg(enic->vdev,
1693                                          VNIC_FLOW_COUNTER_UPDATE_MSECS,
1694                                          last_max_flow_ctr + 1);
1695                 enic->max_flow_counter = last_max_flow_ctr;
1696         }
1697 unwind_ctr_alloc:
1698         if (flow->counter_idx != -1)
1699                 vnic_dev_counter_free(enic->vdev, ctr_idx);
1700 unwind_flow_alloc:
1701         rte_free(flow);
1702         return NULL;
1703 }
1704
1705 /**
1706  * Remove filter/action from the NIC.
1707  *
1708  * @param enic[in]
1709  *   Device structure pointer.
1710  * @param filter_id[in]
1711  *   Id of NIC filter.
1712  * @param enic_action[in]
1713  *   Internal NIC action structure pointer.
1714  * @param error[out]
1715  */
1716 static int
1717 enic_flow_del_filter(struct enic *enic, struct rte_flow *flow,
1718                    struct rte_flow_error *error)
1719 {
1720         u16 filter_id;
1721         int err;
1722
1723         FLOW_TRACE();
1724
1725         filter_id = flow->enic_filter_id;
1726         err = vnic_dev_classifier(enic->vdev, CLSF_DEL, &filter_id, NULL, NULL);
1727         if (err) {
1728                 rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_HANDLE,
1729                                    NULL, "vnic_dev_classifier failed");
1730                 return -err;
1731         }
1732
1733         if (flow->counter_idx != -1) {
1734                 if (!vnic_dev_counter_free(enic->vdev, flow->counter_idx))
1735                         dev_err(enic, "counter free failed, idx: %d\n",
1736                                 flow->counter_idx);
1737                 flow->counter_idx = -1;
1738         }
1739         return 0;
1740 }
1741
1742 /*
1743  * The following functions are callbacks for Generic flow API.
1744  */
1745
1746 /**
1747  * Validate a flow supported by the NIC.
1748  *
1749  * @see rte_flow_validate()
1750  * @see rte_flow_ops
1751  */
1752 static int
1753 enic_flow_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attrs,
1754                    const struct rte_flow_item pattern[],
1755                    const struct rte_flow_action actions[],
1756                    struct rte_flow_error *error)
1757 {
1758         struct filter_v2 enic_filter;
1759         struct filter_action_v2 enic_action;
1760         int ret;
1761
1762         FLOW_TRACE();
1763
1764         ret = enic_flow_parse(dev, attrs, pattern, actions, error,
1765                                &enic_filter, &enic_action);
1766         if (!ret)
1767                 enic_dump_flow(&enic_action, &enic_filter);
1768         return ret;
1769 }
1770
1771 /**
1772  * Create a flow supported by the NIC.
1773  *
1774  * @see rte_flow_create()
1775  * @see rte_flow_ops
1776  */
1777 static struct rte_flow *
1778 enic_flow_create(struct rte_eth_dev *dev,
1779                  const struct rte_flow_attr *attrs,
1780                  const struct rte_flow_item pattern[],
1781                  const struct rte_flow_action actions[],
1782                  struct rte_flow_error *error)
1783 {
1784         int ret;
1785         struct filter_v2 enic_filter;
1786         struct filter_action_v2 enic_action;
1787         struct rte_flow *flow;
1788         struct enic *enic = pmd_priv(dev);
1789
1790         FLOW_TRACE();
1791
1792         ret = enic_flow_parse(dev, attrs, pattern, actions, error, &enic_filter,
1793                               &enic_action);
1794         if (ret < 0)
1795                 return NULL;
1796
1797         rte_spinlock_lock(&enic->flows_lock);
1798         flow = enic_flow_add_filter(enic, &enic_filter, &enic_action,
1799                                     error);
1800         if (flow)
1801                 LIST_INSERT_HEAD(&enic->flows, flow, next);
1802         rte_spinlock_unlock(&enic->flows_lock);
1803
1804         return flow;
1805 }
1806
1807 /**
1808  * Destroy a flow supported by the NIC.
1809  *
1810  * @see rte_flow_destroy()
1811  * @see rte_flow_ops
1812  */
1813 static int
1814 enic_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
1815                   __rte_unused struct rte_flow_error *error)
1816 {
1817         struct enic *enic = pmd_priv(dev);
1818
1819         FLOW_TRACE();
1820
1821         rte_spinlock_lock(&enic->flows_lock);
1822         enic_flow_del_filter(enic, flow, error);
1823         LIST_REMOVE(flow, next);
1824         rte_spinlock_unlock(&enic->flows_lock);
1825         rte_free(flow);
1826         return 0;
1827 }
1828
1829 /**
1830  * Flush all flows on the device.
1831  *
1832  * @see rte_flow_flush()
1833  * @see rte_flow_ops
1834  */
1835 static int
1836 enic_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1837 {
1838         struct rte_flow *flow;
1839         struct enic *enic = pmd_priv(dev);
1840
1841         FLOW_TRACE();
1842
1843         rte_spinlock_lock(&enic->flows_lock);
1844
1845         while (!LIST_EMPTY(&enic->flows)) {
1846                 flow = LIST_FIRST(&enic->flows);
1847                 enic_flow_del_filter(enic, flow, error);
1848                 LIST_REMOVE(flow, next);
1849                 rte_free(flow);
1850         }
1851         rte_spinlock_unlock(&enic->flows_lock);
1852         return 0;
1853 }
1854
1855 static int
1856 enic_flow_query_count(struct rte_eth_dev *dev,
1857                       struct rte_flow *flow, void *data,
1858                       struct rte_flow_error *error)
1859 {
1860         struct enic *enic = pmd_priv(dev);
1861         struct rte_flow_query_count *query;
1862         uint64_t packets, bytes;
1863
1864         FLOW_TRACE();
1865
1866         if (flow->counter_idx == -1) {
1867                 return rte_flow_error_set(error, ENOTSUP,
1868                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1869                                           NULL,
1870                                           "flow does not have counter");
1871         }
1872         query = (struct rte_flow_query_count *)data;
1873         if (!vnic_dev_counter_query(enic->vdev, flow->counter_idx,
1874                                     !!query->reset, &packets, &bytes)) {
1875                 return rte_flow_error_set
1876                         (error, EINVAL,
1877                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1878                          NULL,
1879                          "cannot read counter");
1880         }
1881         query->hits_set = 1;
1882         query->bytes_set = 1;
1883         query->hits = packets;
1884         query->bytes = bytes;
1885         return 0;
1886 }
1887
1888 static int
1889 enic_flow_query(struct rte_eth_dev *dev,
1890                 struct rte_flow *flow,
1891                 const struct rte_flow_action *actions,
1892                 void *data,
1893                 struct rte_flow_error *error)
1894 {
1895         int ret = 0;
1896
1897         FLOW_TRACE();
1898
1899         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1900                 switch (actions->type) {
1901                 case RTE_FLOW_ACTION_TYPE_VOID:
1902                         break;
1903                 case RTE_FLOW_ACTION_TYPE_COUNT:
1904                         ret = enic_flow_query_count(dev, flow, data, error);
1905                         break;
1906                 default:
1907                         return rte_flow_error_set(error, ENOTSUP,
1908                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1909                                                   actions,
1910                                                   "action not supported");
1911                 }
1912                 if (ret < 0)
1913                         return ret;
1914         }
1915         return 0;
1916 }
1917
1918 /**
1919  * Flow callback registration.
1920  *
1921  * @see rte_flow_ops
1922  */
1923 const struct rte_flow_ops enic_flow_ops = {
1924         .validate = enic_flow_validate,
1925         .create = enic_flow_create,
1926         .destroy = enic_flow_destroy,
1927         .flush = enic_flow_flush,
1928         .query = enic_flow_query,
1929 };