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