1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2008-2017 Cisco Systems, Inc. All rights reserved.
8 #include <rte_ethdev_driver.h>
9 #include <rte_flow_driver.h>
10 #include <rte_ether.h>
14 #include "enic_compat.h"
19 #define FLOW_TRACE() \
20 rte_log(RTE_LOG_DEBUG, enicpmd_logtype_flow, \
22 #define FLOW_LOG(level, fmt, args...) \
23 rte_log(RTE_LOG_ ## level, enicpmd_logtype_flow, \
26 /** Info about how to copy items into enic filters. */
28 /** Function for copying and validating an item. */
29 int (*copy_item)(const struct rte_flow_item *item,
30 struct filter_v2 *enic_filter, u8 *inner_ofst);
31 /** List of valid previous items. */
32 const enum rte_flow_item_type * const prev_items;
33 /** True if it's OK for this item to be the first item. For some NIC
34 * versions, it's invalid to start the stack above layer 3.
36 const u8 valid_start_item;
39 /** Filtering capabilities for various NIC and firmware versions. */
40 struct enic_filter_cap {
41 /** list of valid items and their handlers and attributes. */
42 const struct enic_items *item_info;
45 /* functions for copying flow actions into enic actions */
46 typedef int (copy_action_fn)(const struct rte_flow_action actions[],
47 struct filter_action_v2 *enic_action);
49 /* functions for copying items into enic filters */
50 typedef int(enic_copy_item_fn)(const struct rte_flow_item *item,
51 struct filter_v2 *enic_filter, u8 *inner_ofst);
53 /** Action capabilities for various NICs. */
54 struct enic_action_cap {
55 /** list of valid actions */
56 const enum rte_flow_action_type *actions;
57 /** copy function for a particular NIC */
58 int (*copy_fn)(const struct rte_flow_action actions[],
59 struct filter_action_v2 *enic_action);
62 /* Forward declarations */
63 static enic_copy_item_fn enic_copy_item_ipv4_v1;
64 static enic_copy_item_fn enic_copy_item_udp_v1;
65 static enic_copy_item_fn enic_copy_item_tcp_v1;
66 static enic_copy_item_fn enic_copy_item_eth_v2;
67 static enic_copy_item_fn enic_copy_item_vlan_v2;
68 static enic_copy_item_fn enic_copy_item_ipv4_v2;
69 static enic_copy_item_fn enic_copy_item_ipv6_v2;
70 static enic_copy_item_fn enic_copy_item_udp_v2;
71 static enic_copy_item_fn enic_copy_item_tcp_v2;
72 static enic_copy_item_fn enic_copy_item_sctp_v2;
73 static enic_copy_item_fn enic_copy_item_vxlan_v2;
74 static copy_action_fn enic_copy_action_v1;
75 static copy_action_fn enic_copy_action_v2;
78 * Legacy NICs or NICs with outdated firmware. Only 5-tuple perfect match
81 static const struct enic_items enic_items_v1[] = {
82 [RTE_FLOW_ITEM_TYPE_IPV4] = {
83 .copy_item = enic_copy_item_ipv4_v1,
84 .valid_start_item = 1,
85 .prev_items = (const enum rte_flow_item_type[]) {
86 RTE_FLOW_ITEM_TYPE_END,
89 [RTE_FLOW_ITEM_TYPE_UDP] = {
90 .copy_item = enic_copy_item_udp_v1,
91 .valid_start_item = 0,
92 .prev_items = (const enum rte_flow_item_type[]) {
93 RTE_FLOW_ITEM_TYPE_IPV4,
94 RTE_FLOW_ITEM_TYPE_END,
97 [RTE_FLOW_ITEM_TYPE_TCP] = {
98 .copy_item = enic_copy_item_tcp_v1,
99 .valid_start_item = 0,
100 .prev_items = (const enum rte_flow_item_type[]) {
101 RTE_FLOW_ITEM_TYPE_IPV4,
102 RTE_FLOW_ITEM_TYPE_END,
108 * NICs have Advanced Filters capability but they are disabled. This means
109 * that layer 3 must be specified.
111 static const struct enic_items enic_items_v2[] = {
112 [RTE_FLOW_ITEM_TYPE_ETH] = {
113 .copy_item = enic_copy_item_eth_v2,
114 .valid_start_item = 1,
115 .prev_items = (const enum rte_flow_item_type[]) {
116 RTE_FLOW_ITEM_TYPE_VXLAN,
117 RTE_FLOW_ITEM_TYPE_END,
120 [RTE_FLOW_ITEM_TYPE_VLAN] = {
121 .copy_item = enic_copy_item_vlan_v2,
122 .valid_start_item = 1,
123 .prev_items = (const enum rte_flow_item_type[]) {
124 RTE_FLOW_ITEM_TYPE_ETH,
125 RTE_FLOW_ITEM_TYPE_END,
128 [RTE_FLOW_ITEM_TYPE_IPV4] = {
129 .copy_item = enic_copy_item_ipv4_v2,
130 .valid_start_item = 1,
131 .prev_items = (const enum rte_flow_item_type[]) {
132 RTE_FLOW_ITEM_TYPE_ETH,
133 RTE_FLOW_ITEM_TYPE_VLAN,
134 RTE_FLOW_ITEM_TYPE_END,
137 [RTE_FLOW_ITEM_TYPE_IPV6] = {
138 .copy_item = enic_copy_item_ipv6_v2,
139 .valid_start_item = 1,
140 .prev_items = (const enum rte_flow_item_type[]) {
141 RTE_FLOW_ITEM_TYPE_ETH,
142 RTE_FLOW_ITEM_TYPE_VLAN,
143 RTE_FLOW_ITEM_TYPE_END,
146 [RTE_FLOW_ITEM_TYPE_UDP] = {
147 .copy_item = enic_copy_item_udp_v2,
148 .valid_start_item = 0,
149 .prev_items = (const enum rte_flow_item_type[]) {
150 RTE_FLOW_ITEM_TYPE_IPV4,
151 RTE_FLOW_ITEM_TYPE_IPV6,
152 RTE_FLOW_ITEM_TYPE_END,
155 [RTE_FLOW_ITEM_TYPE_TCP] = {
156 .copy_item = enic_copy_item_tcp_v2,
157 .valid_start_item = 0,
158 .prev_items = (const enum rte_flow_item_type[]) {
159 RTE_FLOW_ITEM_TYPE_IPV4,
160 RTE_FLOW_ITEM_TYPE_IPV6,
161 RTE_FLOW_ITEM_TYPE_END,
164 [RTE_FLOW_ITEM_TYPE_SCTP] = {
165 .copy_item = enic_copy_item_sctp_v2,
166 .valid_start_item = 0,
167 .prev_items = (const enum rte_flow_item_type[]) {
168 RTE_FLOW_ITEM_TYPE_IPV4,
169 RTE_FLOW_ITEM_TYPE_IPV6,
170 RTE_FLOW_ITEM_TYPE_END,
173 [RTE_FLOW_ITEM_TYPE_VXLAN] = {
174 .copy_item = enic_copy_item_vxlan_v2,
175 .valid_start_item = 0,
176 .prev_items = (const enum rte_flow_item_type[]) {
177 RTE_FLOW_ITEM_TYPE_UDP,
178 RTE_FLOW_ITEM_TYPE_END,
183 /** NICs with Advanced filters enabled */
184 static const struct enic_items enic_items_v3[] = {
185 [RTE_FLOW_ITEM_TYPE_ETH] = {
186 .copy_item = enic_copy_item_eth_v2,
187 .valid_start_item = 1,
188 .prev_items = (const enum rte_flow_item_type[]) {
189 RTE_FLOW_ITEM_TYPE_VXLAN,
190 RTE_FLOW_ITEM_TYPE_END,
193 [RTE_FLOW_ITEM_TYPE_VLAN] = {
194 .copy_item = enic_copy_item_vlan_v2,
195 .valid_start_item = 1,
196 .prev_items = (const enum rte_flow_item_type[]) {
197 RTE_FLOW_ITEM_TYPE_ETH,
198 RTE_FLOW_ITEM_TYPE_END,
201 [RTE_FLOW_ITEM_TYPE_IPV4] = {
202 .copy_item = enic_copy_item_ipv4_v2,
203 .valid_start_item = 1,
204 .prev_items = (const enum rte_flow_item_type[]) {
205 RTE_FLOW_ITEM_TYPE_ETH,
206 RTE_FLOW_ITEM_TYPE_VLAN,
207 RTE_FLOW_ITEM_TYPE_END,
210 [RTE_FLOW_ITEM_TYPE_IPV6] = {
211 .copy_item = enic_copy_item_ipv6_v2,
212 .valid_start_item = 1,
213 .prev_items = (const enum rte_flow_item_type[]) {
214 RTE_FLOW_ITEM_TYPE_ETH,
215 RTE_FLOW_ITEM_TYPE_VLAN,
216 RTE_FLOW_ITEM_TYPE_END,
219 [RTE_FLOW_ITEM_TYPE_UDP] = {
220 .copy_item = enic_copy_item_udp_v2,
221 .valid_start_item = 1,
222 .prev_items = (const enum rte_flow_item_type[]) {
223 RTE_FLOW_ITEM_TYPE_IPV4,
224 RTE_FLOW_ITEM_TYPE_IPV6,
225 RTE_FLOW_ITEM_TYPE_END,
228 [RTE_FLOW_ITEM_TYPE_TCP] = {
229 .copy_item = enic_copy_item_tcp_v2,
230 .valid_start_item = 1,
231 .prev_items = (const enum rte_flow_item_type[]) {
232 RTE_FLOW_ITEM_TYPE_IPV4,
233 RTE_FLOW_ITEM_TYPE_IPV6,
234 RTE_FLOW_ITEM_TYPE_END,
237 [RTE_FLOW_ITEM_TYPE_SCTP] = {
238 .copy_item = enic_copy_item_sctp_v2,
239 .valid_start_item = 0,
240 .prev_items = (const enum rte_flow_item_type[]) {
241 RTE_FLOW_ITEM_TYPE_IPV4,
242 RTE_FLOW_ITEM_TYPE_IPV6,
243 RTE_FLOW_ITEM_TYPE_END,
246 [RTE_FLOW_ITEM_TYPE_VXLAN] = {
247 .copy_item = enic_copy_item_vxlan_v2,
248 .valid_start_item = 1,
249 .prev_items = (const enum rte_flow_item_type[]) {
250 RTE_FLOW_ITEM_TYPE_UDP,
251 RTE_FLOW_ITEM_TYPE_END,
256 /** Filtering capabilities indexed this NICs supported filter type. */
257 static const struct enic_filter_cap enic_filter_cap[] = {
258 [FILTER_IPV4_5TUPLE] = {
259 .item_info = enic_items_v1,
261 [FILTER_USNIC_IP] = {
262 .item_info = enic_items_v2,
265 .item_info = enic_items_v3,
269 /** Supported actions for older NICs */
270 static const enum rte_flow_action_type enic_supported_actions_v1[] = {
271 RTE_FLOW_ACTION_TYPE_QUEUE,
272 RTE_FLOW_ACTION_TYPE_END,
275 /** Supported actions for newer NICs */
276 static const enum rte_flow_action_type enic_supported_actions_v2_id[] = {
277 RTE_FLOW_ACTION_TYPE_QUEUE,
278 RTE_FLOW_ACTION_TYPE_MARK,
279 RTE_FLOW_ACTION_TYPE_FLAG,
280 RTE_FLOW_ACTION_TYPE_END,
283 static const enum rte_flow_action_type enic_supported_actions_v2_drop[] = {
284 RTE_FLOW_ACTION_TYPE_QUEUE,
285 RTE_FLOW_ACTION_TYPE_MARK,
286 RTE_FLOW_ACTION_TYPE_FLAG,
287 RTE_FLOW_ACTION_TYPE_DROP,
288 RTE_FLOW_ACTION_TYPE_END,
291 static const enum rte_flow_action_type enic_supported_actions_v2_count[] = {
292 RTE_FLOW_ACTION_TYPE_QUEUE,
293 RTE_FLOW_ACTION_TYPE_MARK,
294 RTE_FLOW_ACTION_TYPE_FLAG,
295 RTE_FLOW_ACTION_TYPE_DROP,
296 RTE_FLOW_ACTION_TYPE_COUNT,
297 RTE_FLOW_ACTION_TYPE_END,
300 /** Action capabilities indexed by NIC version information */
301 static const struct enic_action_cap enic_action_cap[] = {
302 [FILTER_ACTION_RQ_STEERING_FLAG] = {
303 .actions = enic_supported_actions_v1,
304 .copy_fn = enic_copy_action_v1,
306 [FILTER_ACTION_FILTER_ID_FLAG] = {
307 .actions = enic_supported_actions_v2_id,
308 .copy_fn = enic_copy_action_v2,
310 [FILTER_ACTION_DROP_FLAG] = {
311 .actions = enic_supported_actions_v2_drop,
312 .copy_fn = enic_copy_action_v2,
314 [FILTER_ACTION_COUNTER_FLAG] = {
315 .actions = enic_supported_actions_v2_count,
316 .copy_fn = enic_copy_action_v2,
321 mask_exact_match(const u8 *supported, const u8 *supplied,
325 for (i = 0; i < size; i++) {
326 if (supported[i] != supplied[i])
333 * Copy IPv4 item into version 1 NIC filter.
336 * Item specification.
337 * @param enic_filter[out]
338 * Partially filled in NIC filter structure.
339 * @param inner_ofst[in]
340 * Should always be 0 for version 1.
343 enic_copy_item_ipv4_v1(const struct rte_flow_item *item,
344 struct filter_v2 *enic_filter, u8 *inner_ofst)
346 const struct rte_flow_item_ipv4 *spec = item->spec;
347 const struct rte_flow_item_ipv4 *mask = item->mask;
348 struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
349 struct ipv4_hdr supported_mask = {
350 .src_addr = 0xffffffff,
351 .dst_addr = 0xffffffff,
360 mask = &rte_flow_item_ipv4_mask;
362 /* This is an exact match filter, both fields must be set */
363 if (!spec || !spec->hdr.src_addr || !spec->hdr.dst_addr) {
364 FLOW_LOG(ERR, "IPv4 exact match src/dst addr");
368 /* check that the suppied mask exactly matches capabilty */
369 if (!mask_exact_match((const u8 *)&supported_mask,
370 (const u8 *)item->mask, sizeof(*mask))) {
371 FLOW_LOG(ERR, "IPv4 exact match mask");
375 enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
376 enic_5tup->src_addr = spec->hdr.src_addr;
377 enic_5tup->dst_addr = spec->hdr.dst_addr;
383 * Copy UDP item into version 1 NIC filter.
386 * Item specification.
387 * @param enic_filter[out]
388 * Partially filled in NIC filter structure.
389 * @param inner_ofst[in]
390 * Should always be 0 for version 1.
393 enic_copy_item_udp_v1(const struct rte_flow_item *item,
394 struct filter_v2 *enic_filter, u8 *inner_ofst)
396 const struct rte_flow_item_udp *spec = item->spec;
397 const struct rte_flow_item_udp *mask = item->mask;
398 struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
399 struct udp_hdr supported_mask = {
410 mask = &rte_flow_item_udp_mask;
412 /* This is an exact match filter, both ports must be set */
413 if (!spec || !spec->hdr.src_port || !spec->hdr.dst_port) {
414 FLOW_LOG(ERR, "UDP exact match src/dst addr");
418 /* check that the suppied mask exactly matches capabilty */
419 if (!mask_exact_match((const u8 *)&supported_mask,
420 (const u8 *)item->mask, sizeof(*mask))) {
421 FLOW_LOG(ERR, "UDP exact match mask");
425 enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
426 enic_5tup->src_port = spec->hdr.src_port;
427 enic_5tup->dst_port = spec->hdr.dst_port;
428 enic_5tup->protocol = PROTO_UDP;
434 * Copy TCP item into version 1 NIC filter.
437 * Item specification.
438 * @param enic_filter[out]
439 * Partially filled in NIC filter structure.
440 * @param inner_ofst[in]
441 * Should always be 0 for version 1.
444 enic_copy_item_tcp_v1(const struct rte_flow_item *item,
445 struct filter_v2 *enic_filter, u8 *inner_ofst)
447 const struct rte_flow_item_tcp *spec = item->spec;
448 const struct rte_flow_item_tcp *mask = item->mask;
449 struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
450 struct tcp_hdr supported_mask = {
461 mask = &rte_flow_item_tcp_mask;
463 /* This is an exact match filter, both ports must be set */
464 if (!spec || !spec->hdr.src_port || !spec->hdr.dst_port) {
465 FLOW_LOG(ERR, "TCPIPv4 exact match src/dst addr");
469 /* check that the suppied mask exactly matches capabilty */
470 if (!mask_exact_match((const u8 *)&supported_mask,
471 (const u8 *)item->mask, sizeof(*mask))) {
472 FLOW_LOG(ERR, "TCP exact match mask");
476 enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
477 enic_5tup->src_port = spec->hdr.src_port;
478 enic_5tup->dst_port = spec->hdr.dst_port;
479 enic_5tup->protocol = PROTO_TCP;
485 * Copy ETH item into version 2 NIC filter.
488 * Item specification.
489 * @param enic_filter[out]
490 * Partially filled in NIC filter structure.
491 * @param inner_ofst[in]
492 * If zero, this is an outer header. If non-zero, this is the offset into L5
493 * where the header begins.
496 enic_copy_item_eth_v2(const struct rte_flow_item *item,
497 struct filter_v2 *enic_filter, u8 *inner_ofst)
499 struct ether_hdr enic_spec;
500 struct ether_hdr enic_mask;
501 const struct rte_flow_item_eth *spec = item->spec;
502 const struct rte_flow_item_eth *mask = item->mask;
503 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
507 /* Match all if no spec */
512 mask = &rte_flow_item_eth_mask;
514 memcpy(enic_spec.d_addr.addr_bytes, spec->dst.addr_bytes,
516 memcpy(enic_spec.s_addr.addr_bytes, spec->src.addr_bytes,
519 memcpy(enic_mask.d_addr.addr_bytes, mask->dst.addr_bytes,
521 memcpy(enic_mask.s_addr.addr_bytes, mask->src.addr_bytes,
523 enic_spec.ether_type = spec->type;
524 enic_mask.ether_type = mask->type;
526 if (*inner_ofst == 0) {
528 memcpy(gp->layer[FILTER_GENERIC_1_L2].mask, &enic_mask,
529 sizeof(struct ether_hdr));
530 memcpy(gp->layer[FILTER_GENERIC_1_L2].val, &enic_spec,
531 sizeof(struct ether_hdr));
534 if ((*inner_ofst + sizeof(struct ether_hdr)) >
535 FILTER_GENERIC_1_KEY_LEN)
537 /* Offset into L5 where inner Ethernet header goes */
538 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
539 &enic_mask, sizeof(struct ether_hdr));
540 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
541 &enic_spec, sizeof(struct ether_hdr));
542 *inner_ofst += sizeof(struct ether_hdr);
548 * Copy VLAN item into version 2 NIC filter.
551 * Item specification.
552 * @param enic_filter[out]
553 * Partially filled in NIC filter structure.
554 * @param inner_ofst[in]
555 * If zero, this is an outer header. If non-zero, this is the offset into L5
556 * where the header begins.
559 enic_copy_item_vlan_v2(const struct rte_flow_item *item,
560 struct filter_v2 *enic_filter, u8 *inner_ofst)
562 const struct rte_flow_item_vlan *spec = item->spec;
563 const struct rte_flow_item_vlan *mask = item->mask;
564 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
568 /* Match all if no spec */
573 mask = &rte_flow_item_vlan_mask;
575 if (*inner_ofst == 0) {
576 struct ether_hdr *eth_mask =
577 (void *)gp->layer[FILTER_GENERIC_1_L2].mask;
578 struct ether_hdr *eth_val =
579 (void *)gp->layer[FILTER_GENERIC_1_L2].val;
581 /* Outer TPID cannot be matched */
582 if (eth_mask->ether_type)
584 eth_mask->ether_type = mask->inner_type;
585 eth_val->ether_type = spec->inner_type;
587 /* Outer header. Use the vlan mask/val fields */
588 gp->mask_vlan = mask->tci;
589 gp->val_vlan = spec->tci;
591 /* Inner header. Mask/Val start at *inner_ofst into L5 */
592 if ((*inner_ofst + sizeof(struct vlan_hdr)) >
593 FILTER_GENERIC_1_KEY_LEN)
595 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
596 mask, sizeof(struct vlan_hdr));
597 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
598 spec, sizeof(struct vlan_hdr));
599 *inner_ofst += sizeof(struct vlan_hdr);
605 * Copy IPv4 item into version 2 NIC filter.
608 * Item specification.
609 * @param enic_filter[out]
610 * Partially filled in NIC filter structure.
611 * @param inner_ofst[in]
612 * Must be 0. Don't support inner IPv4 filtering.
615 enic_copy_item_ipv4_v2(const struct rte_flow_item *item,
616 struct filter_v2 *enic_filter, u8 *inner_ofst)
618 const struct rte_flow_item_ipv4 *spec = item->spec;
619 const struct rte_flow_item_ipv4 *mask = item->mask;
620 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
624 if (*inner_ofst == 0) {
626 gp->mask_flags |= FILTER_GENERIC_1_IPV4;
627 gp->val_flags |= FILTER_GENERIC_1_IPV4;
629 /* Match all if no spec */
634 mask = &rte_flow_item_ipv4_mask;
636 memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
637 sizeof(struct ipv4_hdr));
638 memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
639 sizeof(struct ipv4_hdr));
641 /* Inner IPv4 header. Mask/Val start at *inner_ofst into L5 */
642 if ((*inner_ofst + sizeof(struct ipv4_hdr)) >
643 FILTER_GENERIC_1_KEY_LEN)
645 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
646 mask, sizeof(struct ipv4_hdr));
647 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
648 spec, sizeof(struct ipv4_hdr));
649 *inner_ofst += sizeof(struct ipv4_hdr);
655 * Copy IPv6 item into version 2 NIC filter.
658 * Item specification.
659 * @param enic_filter[out]
660 * Partially filled in NIC filter structure.
661 * @param inner_ofst[in]
662 * Must be 0. Don't support inner IPv6 filtering.
665 enic_copy_item_ipv6_v2(const struct rte_flow_item *item,
666 struct filter_v2 *enic_filter, u8 *inner_ofst)
668 const struct rte_flow_item_ipv6 *spec = item->spec;
669 const struct rte_flow_item_ipv6 *mask = item->mask;
670 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
675 gp->mask_flags |= FILTER_GENERIC_1_IPV6;
676 gp->val_flags |= FILTER_GENERIC_1_IPV6;
678 /* Match all if no spec */
683 mask = &rte_flow_item_ipv6_mask;
685 if (*inner_ofst == 0) {
686 memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
687 sizeof(struct ipv6_hdr));
688 memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
689 sizeof(struct ipv6_hdr));
691 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
692 if ((*inner_ofst + sizeof(struct ipv6_hdr)) >
693 FILTER_GENERIC_1_KEY_LEN)
695 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
696 mask, sizeof(struct ipv6_hdr));
697 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
698 spec, sizeof(struct ipv6_hdr));
699 *inner_ofst += sizeof(struct ipv6_hdr);
705 * Copy UDP item into version 2 NIC filter.
708 * Item specification.
709 * @param enic_filter[out]
710 * Partially filled in NIC filter structure.
711 * @param inner_ofst[in]
712 * Must be 0. Don't support inner UDP filtering.
715 enic_copy_item_udp_v2(const struct rte_flow_item *item,
716 struct filter_v2 *enic_filter, u8 *inner_ofst)
718 const struct rte_flow_item_udp *spec = item->spec;
719 const struct rte_flow_item_udp *mask = item->mask;
720 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
725 gp->mask_flags |= FILTER_GENERIC_1_UDP;
726 gp->val_flags |= FILTER_GENERIC_1_UDP;
728 /* Match all if no spec */
733 mask = &rte_flow_item_udp_mask;
735 if (*inner_ofst == 0) {
736 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
737 sizeof(struct udp_hdr));
738 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
739 sizeof(struct udp_hdr));
741 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
742 if ((*inner_ofst + sizeof(struct udp_hdr)) >
743 FILTER_GENERIC_1_KEY_LEN)
745 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
746 mask, sizeof(struct udp_hdr));
747 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
748 spec, sizeof(struct udp_hdr));
749 *inner_ofst += sizeof(struct udp_hdr);
755 * Copy TCP item into version 2 NIC filter.
758 * Item specification.
759 * @param enic_filter[out]
760 * Partially filled in NIC filter structure.
761 * @param inner_ofst[in]
762 * Must be 0. Don't support inner TCP filtering.
765 enic_copy_item_tcp_v2(const struct rte_flow_item *item,
766 struct filter_v2 *enic_filter, u8 *inner_ofst)
768 const struct rte_flow_item_tcp *spec = item->spec;
769 const struct rte_flow_item_tcp *mask = item->mask;
770 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
775 gp->mask_flags |= FILTER_GENERIC_1_TCP;
776 gp->val_flags |= FILTER_GENERIC_1_TCP;
778 /* Match all if no spec */
785 if (*inner_ofst == 0) {
786 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
787 sizeof(struct tcp_hdr));
788 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
789 sizeof(struct tcp_hdr));
791 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
792 if ((*inner_ofst + sizeof(struct tcp_hdr)) >
793 FILTER_GENERIC_1_KEY_LEN)
795 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
796 mask, sizeof(struct tcp_hdr));
797 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
798 spec, sizeof(struct tcp_hdr));
799 *inner_ofst += sizeof(struct tcp_hdr);
805 * Copy SCTP item into version 2 NIC filter.
808 * Item specification.
809 * @param enic_filter[out]
810 * Partially filled in NIC filter structure.
811 * @param inner_ofst[in]
812 * Must be 0. Don't support inner SCTP filtering.
815 enic_copy_item_sctp_v2(const struct rte_flow_item *item,
816 struct filter_v2 *enic_filter, u8 *inner_ofst)
818 const struct rte_flow_item_sctp *spec = item->spec;
819 const struct rte_flow_item_sctp *mask = item->mask;
820 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
821 uint8_t *ip_proto_mask = NULL;
822 uint8_t *ip_proto = NULL;
830 * The NIC filter API has no flags for "match sctp", so explicitly set
831 * the protocol number in the IP pattern.
833 if (gp->val_flags & FILTER_GENERIC_1_IPV4) {
835 ip = (struct ipv4_hdr *)gp->layer[FILTER_GENERIC_1_L3].mask;
836 ip_proto_mask = &ip->next_proto_id;
837 ip = (struct ipv4_hdr *)gp->layer[FILTER_GENERIC_1_L3].val;
838 ip_proto = &ip->next_proto_id;
839 } else if (gp->val_flags & FILTER_GENERIC_1_IPV6) {
841 ip = (struct ipv6_hdr *)gp->layer[FILTER_GENERIC_1_L3].mask;
842 ip_proto_mask = &ip->proto;
843 ip = (struct ipv6_hdr *)gp->layer[FILTER_GENERIC_1_L3].val;
844 ip_proto = &ip->proto;
846 /* Need IPv4/IPv6 pattern first */
849 *ip_proto = IPPROTO_SCTP;
850 *ip_proto_mask = 0xff;
852 /* Match all if no spec */
857 mask = &rte_flow_item_sctp_mask;
859 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
860 sizeof(struct sctp_hdr));
861 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
862 sizeof(struct sctp_hdr));
867 * Copy UDP item into version 2 NIC filter.
870 * Item specification.
871 * @param enic_filter[out]
872 * Partially filled in NIC filter structure.
873 * @param inner_ofst[in]
874 * Must be 0. VxLAN headers always start at the beginning of L5.
877 enic_copy_item_vxlan_v2(const struct rte_flow_item *item,
878 struct filter_v2 *enic_filter, u8 *inner_ofst)
880 const struct rte_flow_item_vxlan *spec = item->spec;
881 const struct rte_flow_item_vxlan *mask = item->mask;
882 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
889 /* Match all if no spec */
894 mask = &rte_flow_item_vxlan_mask;
896 memcpy(gp->layer[FILTER_GENERIC_1_L5].mask, mask,
897 sizeof(struct vxlan_hdr));
898 memcpy(gp->layer[FILTER_GENERIC_1_L5].val, spec,
899 sizeof(struct vxlan_hdr));
901 *inner_ofst = sizeof(struct vxlan_hdr);
906 * Return 1 if current item is valid on top of the previous one.
908 * @param prev_item[in]
909 * The item before this one in the pattern or RTE_FLOW_ITEM_TYPE_END if this
911 * @param item_info[in]
912 * Info about this item, like valid previous items.
913 * @param is_first[in]
914 * True if this the first item in the pattern.
917 item_stacking_valid(enum rte_flow_item_type prev_item,
918 const struct enic_items *item_info, u8 is_first_item)
920 enum rte_flow_item_type const *allowed_items = item_info->prev_items;
924 for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
925 if (prev_item == *allowed_items)
929 /* This is the first item in the stack. Check if that's cool */
930 if (is_first_item && item_info->valid_start_item)
937 * Build the intenal enic filter structure from the provided pattern. The
938 * pattern is validated as the items are copied.
941 * @param items_info[in]
942 * Info about this NICs item support, like valid previous items.
943 * @param enic_filter[out]
944 * NIC specfilc filters derived from the pattern.
948 enic_copy_filter(const struct rte_flow_item pattern[],
949 const struct enic_items *items_info,
950 struct filter_v2 *enic_filter,
951 struct rte_flow_error *error)
954 const struct rte_flow_item *item = pattern;
955 u8 inner_ofst = 0; /* If encapsulated, ofst into L5 */
956 enum rte_flow_item_type prev_item;
957 const struct enic_items *item_info;
959 u8 is_first_item = 1;
965 for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
966 /* Get info about how to validate and copy the item. If NULL
967 * is returned the nic does not support the item.
969 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
972 item_info = &items_info[item->type];
974 /* check to see if item stacking is valid */
975 if (!item_stacking_valid(prev_item, item_info, is_first_item))
978 ret = item_info->copy_item(item, enic_filter, &inner_ofst);
980 goto item_not_supported;
981 prev_item = item->type;
987 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_ITEM,
988 NULL, "enic type error");
992 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
993 item, "stacking error");
998 * Build the intenal version 1 NIC action structure from the provided pattern.
999 * The pattern is validated as the items are copied.
1001 * @param actions[in]
1002 * @param enic_action[out]
1003 * NIC specfilc actions derived from the actions.
1007 enic_copy_action_v1(const struct rte_flow_action actions[],
1008 struct filter_action_v2 *enic_action)
1011 uint32_t overlap = 0;
1015 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1016 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID)
1019 switch (actions->type) {
1020 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1021 const struct rte_flow_action_queue *queue =
1022 (const struct rte_flow_action_queue *)
1028 enic_action->rq_idx =
1029 enic_rte_rq_idx_to_sop_idx(queue->index);
1037 if (!(overlap & FATE))
1039 enic_action->type = FILTER_ACTION_RQ_STEERING;
1044 * Build the intenal version 2 NIC action structure from the provided pattern.
1045 * The pattern is validated as the items are copied.
1047 * @param actions[in]
1048 * @param enic_action[out]
1049 * NIC specfilc actions derived from the actions.
1053 enic_copy_action_v2(const struct rte_flow_action actions[],
1054 struct filter_action_v2 *enic_action)
1056 enum { FATE = 1, MARK = 2, };
1057 uint32_t overlap = 0;
1061 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1062 switch (actions->type) {
1063 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1064 const struct rte_flow_action_queue *queue =
1065 (const struct rte_flow_action_queue *)
1071 enic_action->rq_idx =
1072 enic_rte_rq_idx_to_sop_idx(queue->index);
1073 enic_action->flags |= FILTER_ACTION_RQ_STEERING_FLAG;
1076 case RTE_FLOW_ACTION_TYPE_MARK: {
1077 const struct rte_flow_action_mark *mark =
1078 (const struct rte_flow_action_mark *)
1085 * Map mark ID (32-bit) to filter ID (16-bit):
1086 * - Reject values > 16 bits
1087 * - Filter ID 0 is reserved for filters that steer
1088 * but not mark. So add 1 to the mark ID to avoid
1090 * - Filter ID (ENIC_MAGIC_FILTER_ID = 0xffff) is
1091 * reserved for the "flag" action below.
1093 if (mark->id >= ENIC_MAGIC_FILTER_ID - 1)
1095 enic_action->filter_id = mark->id + 1;
1096 enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1099 case RTE_FLOW_ACTION_TYPE_FLAG: {
1103 /* ENIC_MAGIC_FILTER_ID is reserved for flagging */
1104 enic_action->filter_id = ENIC_MAGIC_FILTER_ID;
1105 enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1108 case RTE_FLOW_ACTION_TYPE_DROP: {
1112 enic_action->flags |= FILTER_ACTION_DROP_FLAG;
1115 case RTE_FLOW_ACTION_TYPE_COUNT: {
1116 enic_action->flags |= FILTER_ACTION_COUNTER_FLAG;
1119 case RTE_FLOW_ACTION_TYPE_VOID:
1126 if (!(overlap & FATE))
1128 enic_action->type = FILTER_ACTION_V2;
1132 /** Check if the action is supported */
1134 enic_match_action(const struct rte_flow_action *action,
1135 const enum rte_flow_action_type *supported_actions)
1137 for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
1138 supported_actions++) {
1139 if (action->type == *supported_actions)
1145 /** Get the NIC filter capabilties structure */
1146 static const struct enic_filter_cap *
1147 enic_get_filter_cap(struct enic *enic)
1149 if (enic->flow_filter_mode)
1150 return &enic_filter_cap[enic->flow_filter_mode];
1155 /** Get the actions for this NIC version. */
1156 static const struct enic_action_cap *
1157 enic_get_action_cap(struct enic *enic)
1159 const struct enic_action_cap *ea;
1162 actions = enic->filter_actions;
1163 if (actions & FILTER_ACTION_COUNTER_FLAG)
1164 ea = &enic_action_cap[FILTER_ACTION_COUNTER_FLAG];
1165 else if (actions & FILTER_ACTION_DROP_FLAG)
1166 ea = &enic_action_cap[FILTER_ACTION_DROP_FLAG];
1167 else if (actions & FILTER_ACTION_FILTER_ID_FLAG)
1168 ea = &enic_action_cap[FILTER_ACTION_FILTER_ID_FLAG];
1170 ea = &enic_action_cap[FILTER_ACTION_RQ_STEERING_FLAG];
1174 /* Debug function to dump internal NIC action structure. */
1176 enic_dump_actions(const struct filter_action_v2 *ea)
1178 if (ea->type == FILTER_ACTION_RQ_STEERING) {
1179 FLOW_LOG(INFO, "Action(V1), queue: %u\n", ea->rq_idx);
1180 } else if (ea->type == FILTER_ACTION_V2) {
1181 FLOW_LOG(INFO, "Actions(V2)\n");
1182 if (ea->flags & FILTER_ACTION_RQ_STEERING_FLAG)
1183 FLOW_LOG(INFO, "\tqueue: %u\n",
1184 enic_sop_rq_idx_to_rte_idx(ea->rq_idx));
1185 if (ea->flags & FILTER_ACTION_FILTER_ID_FLAG)
1186 FLOW_LOG(INFO, "\tfilter_id: %u\n", ea->filter_id);
1190 /* Debug function to dump internal NIC filter structure. */
1192 enic_dump_filter(const struct filter_v2 *filt)
1194 const struct filter_generic_1 *gp;
1197 char ip4[16], ip6[16], udp[16], tcp[16], tcpudp[16], ip4csum[16];
1198 char l4csum[16], ipfrag[16];
1200 switch (filt->type) {
1201 case FILTER_IPV4_5TUPLE:
1202 FLOW_LOG(INFO, "FILTER_IPV4_5TUPLE\n");
1204 case FILTER_USNIC_IP:
1206 /* FIXME: this should be a loop */
1207 gp = &filt->u.generic_1;
1208 FLOW_LOG(INFO, "Filter: vlan: 0x%04x, mask: 0x%04x\n",
1209 gp->val_vlan, gp->mask_vlan);
1211 if (gp->mask_flags & FILTER_GENERIC_1_IPV4)
1213 (gp->val_flags & FILTER_GENERIC_1_IPV4)
1214 ? "ip4(y)" : "ip4(n)");
1216 sprintf(ip4, "%s ", "ip4(x)");
1218 if (gp->mask_flags & FILTER_GENERIC_1_IPV6)
1220 (gp->val_flags & FILTER_GENERIC_1_IPV4)
1221 ? "ip6(y)" : "ip6(n)");
1223 sprintf(ip6, "%s ", "ip6(x)");
1225 if (gp->mask_flags & FILTER_GENERIC_1_UDP)
1227 (gp->val_flags & FILTER_GENERIC_1_UDP)
1228 ? "udp(y)" : "udp(n)");
1230 sprintf(udp, "%s ", "udp(x)");
1232 if (gp->mask_flags & FILTER_GENERIC_1_TCP)
1234 (gp->val_flags & FILTER_GENERIC_1_TCP)
1235 ? "tcp(y)" : "tcp(n)");
1237 sprintf(tcp, "%s ", "tcp(x)");
1239 if (gp->mask_flags & FILTER_GENERIC_1_TCP_OR_UDP)
1240 sprintf(tcpudp, "%s ",
1241 (gp->val_flags & FILTER_GENERIC_1_TCP_OR_UDP)
1242 ? "tcpudp(y)" : "tcpudp(n)");
1244 sprintf(tcpudp, "%s ", "tcpudp(x)");
1246 if (gp->mask_flags & FILTER_GENERIC_1_IP4SUM_OK)
1247 sprintf(ip4csum, "%s ",
1248 (gp->val_flags & FILTER_GENERIC_1_IP4SUM_OK)
1249 ? "ip4csum(y)" : "ip4csum(n)");
1251 sprintf(ip4csum, "%s ", "ip4csum(x)");
1253 if (gp->mask_flags & FILTER_GENERIC_1_L4SUM_OK)
1254 sprintf(l4csum, "%s ",
1255 (gp->val_flags & FILTER_GENERIC_1_L4SUM_OK)
1256 ? "l4csum(y)" : "l4csum(n)");
1258 sprintf(l4csum, "%s ", "l4csum(x)");
1260 if (gp->mask_flags & FILTER_GENERIC_1_IPFRAG)
1261 sprintf(ipfrag, "%s ",
1262 (gp->val_flags & FILTER_GENERIC_1_IPFRAG)
1263 ? "ipfrag(y)" : "ipfrag(n)");
1265 sprintf(ipfrag, "%s ", "ipfrag(x)");
1266 FLOW_LOG(INFO, "\tFlags: %s%s%s%s%s%s%s%s\n", ip4, ip6, udp,
1267 tcp, tcpudp, ip4csum, l4csum, ipfrag);
1269 for (i = 0; i < FILTER_GENERIC_1_NUM_LAYERS; i++) {
1270 mbyte = FILTER_GENERIC_1_KEY_LEN - 1;
1271 while (mbyte && !gp->layer[i].mask[mbyte])
1277 for (j = 0; j <= mbyte; j++) {
1279 gp->layer[i].mask[j]);
1283 FLOW_LOG(INFO, "\tL%u mask: %s\n", i + 2, buf);
1285 for (j = 0; j <= mbyte; j++) {
1287 gp->layer[i].val[j]);
1291 FLOW_LOG(INFO, "\tL%u val: %s\n", i + 2, buf);
1295 FLOW_LOG(INFO, "FILTER UNKNOWN\n");
1300 /* Debug function to dump internal NIC flow structures. */
1302 enic_dump_flow(const struct filter_action_v2 *ea, const struct filter_v2 *filt)
1304 enic_dump_filter(filt);
1305 enic_dump_actions(ea);
1310 * Internal flow parse/validate function.
1313 * This device pointer.
1314 * @param pattern[in]
1315 * @param actions[in]
1317 * @param enic_filter[out]
1318 * Internal NIC filter structure pointer.
1319 * @param enic_action[out]
1320 * Internal NIC action structure pointer.
1323 enic_flow_parse(struct rte_eth_dev *dev,
1324 const struct rte_flow_attr *attrs,
1325 const struct rte_flow_item pattern[],
1326 const struct rte_flow_action actions[],
1327 struct rte_flow_error *error,
1328 struct filter_v2 *enic_filter,
1329 struct filter_action_v2 *enic_action)
1331 unsigned int ret = 0;
1332 struct enic *enic = pmd_priv(dev);
1333 const struct enic_filter_cap *enic_filter_cap;
1334 const struct enic_action_cap *enic_action_cap;
1335 const struct rte_flow_action *action;
1339 memset(enic_filter, 0, sizeof(*enic_filter));
1340 memset(enic_action, 0, sizeof(*enic_action));
1343 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1344 NULL, "No pattern specified");
1349 rte_flow_error_set(error, EINVAL,
1350 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1351 NULL, "No action specified");
1357 rte_flow_error_set(error, ENOTSUP,
1358 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1360 "priority groups are not supported");
1362 } else if (attrs->priority) {
1363 rte_flow_error_set(error, ENOTSUP,
1364 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1366 "priorities are not supported");
1368 } else if (attrs->egress) {
1369 rte_flow_error_set(error, ENOTSUP,
1370 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
1372 "egress is not supported");
1374 } else if (attrs->transfer) {
1375 rte_flow_error_set(error, ENOTSUP,
1376 RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1378 "transfer is not supported");
1380 } else if (!attrs->ingress) {
1381 rte_flow_error_set(error, ENOTSUP,
1382 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1384 "only ingress is supported");
1389 rte_flow_error_set(error, EINVAL,
1390 RTE_FLOW_ERROR_TYPE_ATTR,
1391 NULL, "No attribute specified");
1395 /* Verify Actions. */
1396 enic_action_cap = enic_get_action_cap(enic);
1397 for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
1399 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1401 else if (!enic_match_action(action, enic_action_cap->actions))
1404 if (action->type != RTE_FLOW_ACTION_TYPE_END) {
1405 rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
1406 action, "Invalid action.");
1409 ret = enic_action_cap->copy_fn(actions, enic_action);
1411 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1412 NULL, "Unsupported action.");
1416 /* Verify Flow items. If copying the filter from flow format to enic
1417 * format fails, the flow is not supported
1419 enic_filter_cap = enic_get_filter_cap(enic);
1420 if (enic_filter_cap == NULL) {
1421 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1422 NULL, "Flow API not available");
1425 enic_filter->type = enic->flow_filter_mode;
1426 ret = enic_copy_filter(pattern, enic_filter_cap->item_info,
1427 enic_filter, error);
1432 * Push filter/action to the NIC.
1435 * Device structure pointer.
1436 * @param enic_filter[in]
1437 * Internal NIC filter structure pointer.
1438 * @param enic_action[in]
1439 * Internal NIC action structure pointer.
1442 static struct rte_flow *
1443 enic_flow_add_filter(struct enic *enic, struct filter_v2 *enic_filter,
1444 struct filter_action_v2 *enic_action,
1445 struct rte_flow_error *error)
1447 struct rte_flow *flow;
1451 int last_max_flow_ctr;
1455 flow = rte_calloc(__func__, 1, sizeof(*flow), 0);
1457 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1458 NULL, "cannot allocate flow memory");
1462 flow->counter_idx = -1;
1463 last_max_flow_ctr = -1;
1464 if (enic_action->flags & FILTER_ACTION_COUNTER_FLAG) {
1465 if (!vnic_dev_counter_alloc(enic->vdev, (uint32_t *)&ctr_idx)) {
1466 rte_flow_error_set(error, ENOMEM,
1467 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1468 NULL, "cannot allocate counter");
1469 goto unwind_flow_alloc;
1471 flow->counter_idx = ctr_idx;
1472 enic_action->counter_index = ctr_idx;
1474 /* If index is the largest, increase the counter DMA size */
1475 if (ctr_idx > enic->max_flow_counter) {
1476 err = vnic_dev_counter_dma_cfg(enic->vdev,
1477 VNIC_FLOW_COUNTER_UPDATE_MSECS,
1480 rte_flow_error_set(error, -err,
1481 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1482 NULL, "counter DMA config failed");
1483 goto unwind_ctr_alloc;
1485 last_max_flow_ctr = enic->max_flow_counter;
1486 enic->max_flow_counter = ctr_idx;
1490 /* entry[in] is the queue id, entry[out] is the filter Id for delete */
1491 entry = enic_action->rq_idx;
1492 err = vnic_dev_classifier(enic->vdev, CLSF_ADD, &entry, enic_filter,
1495 rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_HANDLE,
1496 NULL, "vnic_dev_classifier error");
1497 goto unwind_ctr_dma_cfg;
1500 flow->enic_filter_id = entry;
1501 flow->enic_filter = *enic_filter;
1505 /* unwind if there are errors */
1507 if (last_max_flow_ctr != -1) {
1508 /* reduce counter DMA size */
1509 vnic_dev_counter_dma_cfg(enic->vdev,
1510 VNIC_FLOW_COUNTER_UPDATE_MSECS,
1511 last_max_flow_ctr + 1);
1512 enic->max_flow_counter = last_max_flow_ctr;
1515 if (flow->counter_idx != -1)
1516 vnic_dev_counter_free(enic->vdev, ctr_idx);
1523 * Remove filter/action from the NIC.
1526 * Device structure pointer.
1527 * @param filter_id[in]
1529 * @param enic_action[in]
1530 * Internal NIC action structure pointer.
1534 enic_flow_del_filter(struct enic *enic, struct rte_flow *flow,
1535 struct rte_flow_error *error)
1542 filter_id = flow->enic_filter_id;
1543 err = vnic_dev_classifier(enic->vdev, CLSF_DEL, &filter_id, NULL, NULL);
1545 rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_HANDLE,
1546 NULL, "vnic_dev_classifier failed");
1550 if (flow->counter_idx != -1) {
1551 if (!vnic_dev_counter_free(enic->vdev, flow->counter_idx))
1552 dev_err(enic, "counter free failed, idx: %d\n",
1554 flow->counter_idx = -1;
1560 * The following functions are callbacks for Generic flow API.
1564 * Validate a flow supported by the NIC.
1566 * @see rte_flow_validate()
1570 enic_flow_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attrs,
1571 const struct rte_flow_item pattern[],
1572 const struct rte_flow_action actions[],
1573 struct rte_flow_error *error)
1575 struct filter_v2 enic_filter;
1576 struct filter_action_v2 enic_action;
1581 ret = enic_flow_parse(dev, attrs, pattern, actions, error,
1582 &enic_filter, &enic_action);
1584 enic_dump_flow(&enic_action, &enic_filter);
1589 * Create a flow supported by the NIC.
1591 * @see rte_flow_create()
1594 static struct rte_flow *
1595 enic_flow_create(struct rte_eth_dev *dev,
1596 const struct rte_flow_attr *attrs,
1597 const struct rte_flow_item pattern[],
1598 const struct rte_flow_action actions[],
1599 struct rte_flow_error *error)
1602 struct filter_v2 enic_filter;
1603 struct filter_action_v2 enic_action;
1604 struct rte_flow *flow;
1605 struct enic *enic = pmd_priv(dev);
1609 ret = enic_flow_parse(dev, attrs, pattern, actions, error, &enic_filter,
1614 rte_spinlock_lock(&enic->flows_lock);
1615 flow = enic_flow_add_filter(enic, &enic_filter, &enic_action,
1618 LIST_INSERT_HEAD(&enic->flows, flow, next);
1619 rte_spinlock_unlock(&enic->flows_lock);
1625 * Destroy a flow supported by the NIC.
1627 * @see rte_flow_destroy()
1631 enic_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
1632 __rte_unused struct rte_flow_error *error)
1634 struct enic *enic = pmd_priv(dev);
1638 rte_spinlock_lock(&enic->flows_lock);
1639 enic_flow_del_filter(enic, flow, error);
1640 LIST_REMOVE(flow, next);
1641 rte_spinlock_unlock(&enic->flows_lock);
1647 * Flush all flows on the device.
1649 * @see rte_flow_flush()
1653 enic_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1655 struct rte_flow *flow;
1656 struct enic *enic = pmd_priv(dev);
1660 rte_spinlock_lock(&enic->flows_lock);
1662 while (!LIST_EMPTY(&enic->flows)) {
1663 flow = LIST_FIRST(&enic->flows);
1664 enic_flow_del_filter(enic, flow, error);
1665 LIST_REMOVE(flow, next);
1668 rte_spinlock_unlock(&enic->flows_lock);
1673 enic_flow_query_count(struct rte_eth_dev *dev,
1674 struct rte_flow *flow, void *data,
1675 struct rte_flow_error *error)
1677 struct enic *enic = pmd_priv(dev);
1678 struct rte_flow_query_count *query;
1679 uint64_t packets, bytes;
1683 if (flow->counter_idx == -1) {
1684 return rte_flow_error_set(error, ENOTSUP,
1685 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1687 "flow does not have counter");
1689 query = (struct rte_flow_query_count *)data;
1690 if (!vnic_dev_counter_query(enic->vdev, flow->counter_idx,
1691 !!query->reset, &packets, &bytes)) {
1692 return rte_flow_error_set
1694 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1696 "cannot read counter");
1698 query->hits_set = 1;
1699 query->bytes_set = 1;
1700 query->hits = packets;
1701 query->bytes = bytes;
1706 enic_flow_query(struct rte_eth_dev *dev,
1707 struct rte_flow *flow,
1708 const struct rte_flow_action *actions,
1710 struct rte_flow_error *error)
1716 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1717 switch (actions->type) {
1718 case RTE_FLOW_ACTION_TYPE_VOID:
1720 case RTE_FLOW_ACTION_TYPE_COUNT:
1721 ret = enic_flow_query_count(dev, flow, data, error);
1724 return rte_flow_error_set(error, ENOTSUP,
1725 RTE_FLOW_ERROR_TYPE_ACTION,
1727 "action not supported");
1736 * Flow callback registration.
1740 const struct rte_flow_ops enic_flow_ops = {
1741 .validate = enic_flow_validate,
1742 .create = enic_flow_create,
1743 .destroy = enic_flow_destroy,
1744 .flush = enic_flow_flush,
1745 .query = enic_flow_query,