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, \
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.
34 struct copy_item_args {
35 const struct rte_flow_item *item;
36 struct filter_v2 *filter;
40 /* functions for copying items into enic filters */
41 typedef int (enic_copy_item_fn)(struct copy_item_args *arg);
43 /** Info about how to copy items into enic filters. */
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.
52 const u8 valid_start_item;
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;
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);
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;
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;
93 * Legacy NICs or NICs with outdated firmware. Only 5-tuple perfect match
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,
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,
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,
123 * NICs have Advanced Filters capability but they are disabled. This means
124 * that layer 3 must be specified.
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
293 [FILTER_USNIC_IP] = {
294 .item_info = enic_items_v2,
295 .max_item_type = RTE_FLOW_ITEM_TYPE_VXLAN,
298 .item_info = enic_items_v3,
299 .max_item_type = RTE_FLOW_ITEM_TYPE_VXLAN,
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,
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,
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,
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,
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,
346 [FILTER_ACTION_FILTER_ID_FLAG] = {
347 .actions = enic_supported_actions_v2_id,
348 .copy_fn = enic_copy_action_v2,
350 [FILTER_ACTION_DROP_FLAG] = {
351 .actions = enic_supported_actions_v2_drop,
352 .copy_fn = enic_copy_action_v2,
354 [FILTER_ACTION_COUNTER_FLAG] = {
355 .actions = enic_supported_actions_v2_count,
356 .copy_fn = enic_copy_action_v2,
361 mask_exact_match(const u8 *supported, const u8 *supplied,
365 for (i = 0; i < size; i++) {
366 if (supported[i] != supplied[i])
373 enic_copy_item_ipv4_v1(struct copy_item_args *arg)
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,
392 mask = &rte_flow_item_ipv4_mask;
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");
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");
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;
415 enic_copy_item_udp_v1(struct copy_item_args *arg)
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 = {
434 mask = &rte_flow_item_udp_mask;
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");
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");
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;
458 enic_copy_item_tcp_v1(struct copy_item_args *arg)
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 = {
477 mask = &rte_flow_item_tcp_mask;
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");
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");
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;
501 enic_copy_item_eth_v2(struct copy_item_args *arg)
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;
514 /* Match all if no spec */
519 mask = &rte_flow_item_eth_mask;
521 memcpy(enic_spec.d_addr.addr_bytes, spec->dst.addr_bytes,
523 memcpy(enic_spec.s_addr.addr_bytes, spec->src.addr_bytes,
526 memcpy(enic_mask.d_addr.addr_bytes, mask->dst.addr_bytes,
528 memcpy(enic_mask.s_addr.addr_bytes, mask->src.addr_bytes,
530 enic_spec.ether_type = spec->type;
531 enic_mask.ether_type = mask->type;
533 if (*inner_ofst == 0) {
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));
541 if ((*inner_ofst + sizeof(struct ether_hdr)) >
542 FILTER_GENERIC_1_KEY_LEN)
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);
555 enic_copy_item_vlan_v2(struct copy_item_args *arg)
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;
566 /* Match all if no spec */
571 mask = &rte_flow_item_vlan_mask;
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;
579 /* Outer TPID cannot be matched */
580 if (eth_mask->ether_type)
582 eth_mask->ether_type = mask->inner_type;
583 eth_val->ether_type = spec->inner_type;
585 /* Outer header. Use the vlan mask/val fields */
586 gp->mask_vlan = mask->tci;
587 gp->val_vlan = spec->tci;
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)
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);
603 enic_copy_item_ipv4_v2(struct copy_item_args *arg)
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;
614 if (*inner_ofst == 0) {
616 gp->mask_flags |= FILTER_GENERIC_1_IPV4;
617 gp->val_flags |= FILTER_GENERIC_1_IPV4;
619 /* Match all if no spec */
624 mask = &rte_flow_item_ipv4_mask;
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));
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)
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);
645 enic_copy_item_ipv6_v2(struct copy_item_args *arg)
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;
657 gp->mask_flags |= FILTER_GENERIC_1_IPV6;
658 gp->val_flags |= FILTER_GENERIC_1_IPV6;
660 /* Match all if no spec */
665 mask = &rte_flow_item_ipv6_mask;
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));
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)
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);
687 enic_copy_item_udp_v2(struct copy_item_args *arg)
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;
699 gp->mask_flags |= FILTER_GENERIC_1_UDP;
700 gp->val_flags |= FILTER_GENERIC_1_UDP;
702 /* Match all if no spec */
707 mask = &rte_flow_item_udp_mask;
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));
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)
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);
729 enic_copy_item_tcp_v2(struct copy_item_args *arg)
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;
741 gp->mask_flags |= FILTER_GENERIC_1_TCP;
742 gp->val_flags |= FILTER_GENERIC_1_TCP;
744 /* Match all if no spec */
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));
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)
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);
771 enic_copy_item_sctp_v2(struct copy_item_args *arg)
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;
788 * The NIC filter API has no flags for "match sctp", so explicitly set
789 * the protocol number in the IP pattern.
791 if (gp->val_flags & FILTER_GENERIC_1_IPV4) {
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) {
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;
804 /* Need IPv4/IPv6 pattern first */
807 *ip_proto = IPPROTO_SCTP;
808 *ip_proto_mask = 0xff;
810 /* Match all if no spec */
815 mask = &rte_flow_item_sctp_mask;
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));
825 enic_copy_item_vxlan_v2(struct copy_item_args *arg)
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;
839 /* Match all if no spec */
844 mask = &rte_flow_item_vxlan_mask;
846 memcpy(gp->layer[FILTER_GENERIC_1_L5].mask, mask,
847 sizeof(struct vxlan_hdr));
848 memcpy(gp->layer[FILTER_GENERIC_1_L5].val, spec,
849 sizeof(struct vxlan_hdr));
851 *inner_ofst = sizeof(struct vxlan_hdr);
856 * Copy raw item into version 2 NIC filter. Currently, raw pattern match is
857 * very limited. It is intended for matching UDP tunnel header (e.g. vxlan
861 enic_copy_item_raw_v2(struct copy_item_args *arg)
863 const struct rte_flow_item *item = arg->item;
864 struct filter_v2 *enic_filter = arg->filter;
865 uint8_t *inner_ofst = arg->inner_ofst;
866 const struct rte_flow_item_raw *spec = item->spec;
867 const struct rte_flow_item_raw *mask = item->mask;
868 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
872 /* Cannot be used for inner packet */
875 /* Need both spec and mask */
878 /* Only supports relative with offset 0 */
879 if (!spec->relative || spec->offset != 0 || spec->search || spec->limit)
881 /* Need non-null pattern that fits within the NIC's filter pattern */
882 if (spec->length == 0 || spec->length > FILTER_GENERIC_1_KEY_LEN ||
883 !spec->pattern || !mask->pattern)
886 * Mask fields, including length, are often set to zero. Assume that
887 * means "same as spec" to avoid breaking existing apps. If length
888 * is not zero, then it should be >= spec length.
890 * No more pattern follows this, so append to the L4 layer instead of
891 * L5 to work with both recent and older VICs.
893 if (mask->length != 0 && mask->length < spec->length)
895 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask + sizeof(struct udp_hdr),
896 mask->pattern, spec->length);
897 memcpy(gp->layer[FILTER_GENERIC_1_L4].val + sizeof(struct udp_hdr),
898 spec->pattern, spec->length);
904 * Return 1 if current item is valid on top of the previous one.
906 * @param prev_item[in]
907 * The item before this one in the pattern or RTE_FLOW_ITEM_TYPE_END if this
909 * @param item_info[in]
910 * Info about this item, like valid previous items.
911 * @param is_first[in]
912 * True if this the first item in the pattern.
915 item_stacking_valid(enum rte_flow_item_type prev_item,
916 const struct enic_items *item_info, u8 is_first_item)
918 enum rte_flow_item_type const *allowed_items = item_info->prev_items;
922 for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
923 if (prev_item == *allowed_items)
927 /* This is the first item in the stack. Check if that's cool */
928 if (is_first_item && item_info->valid_start_item)
935 * Build the intenal enic filter structure from the provided pattern. The
936 * pattern is validated as the items are copied.
939 * @param items_info[in]
940 * Info about this NICs item support, like valid previous items.
941 * @param enic_filter[out]
942 * NIC specfilc filters derived from the pattern.
946 enic_copy_filter(const struct rte_flow_item pattern[],
947 const struct enic_filter_cap *cap,
948 struct filter_v2 *enic_filter,
949 struct rte_flow_error *error)
952 const struct rte_flow_item *item = pattern;
953 u8 inner_ofst = 0; /* If encapsulated, ofst into L5 */
954 enum rte_flow_item_type prev_item;
955 const struct enic_items *item_info;
956 struct copy_item_args args;
957 u8 is_first_item = 1;
963 args.filter = enic_filter;
964 args.inner_ofst = &inner_ofst;
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 = &cap->item_info[item->type];
973 if (item->type > cap->max_item_type ||
974 item_info->copy_item == NULL) {
975 rte_flow_error_set(error, ENOTSUP,
976 RTE_FLOW_ERROR_TYPE_ITEM,
977 NULL, "Unsupported item.");
981 /* check to see if item stacking is valid */
982 if (!item_stacking_valid(prev_item, item_info, is_first_item))
986 ret = item_info->copy_item(&args);
988 goto item_not_supported;
989 prev_item = item->type;
995 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_ITEM,
996 NULL, "enic type error");
1000 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
1001 item, "stacking error");
1006 * Build the intenal version 1 NIC action structure from the provided pattern.
1007 * The pattern is validated as the items are copied.
1009 * @param actions[in]
1010 * @param enic_action[out]
1011 * NIC specfilc actions derived from the actions.
1015 enic_copy_action_v1(__rte_unused struct enic *enic,
1016 const struct rte_flow_action actions[],
1017 struct filter_action_v2 *enic_action)
1020 uint32_t overlap = 0;
1024 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1025 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID)
1028 switch (actions->type) {
1029 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1030 const struct rte_flow_action_queue *queue =
1031 (const struct rte_flow_action_queue *)
1037 enic_action->rq_idx =
1038 enic_rte_rq_idx_to_sop_idx(queue->index);
1046 if (!(overlap & FATE))
1048 enic_action->type = FILTER_ACTION_RQ_STEERING;
1053 * Build the intenal version 2 NIC action structure from the provided pattern.
1054 * The pattern is validated as the items are copied.
1056 * @param actions[in]
1057 * @param enic_action[out]
1058 * NIC specfilc actions derived from the actions.
1062 enic_copy_action_v2(struct enic *enic,
1063 const struct rte_flow_action actions[],
1064 struct filter_action_v2 *enic_action)
1066 enum { FATE = 1, MARK = 2, };
1067 uint32_t overlap = 0;
1068 bool passthru = false;
1072 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1073 switch (actions->type) {
1074 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1075 const struct rte_flow_action_queue *queue =
1076 (const struct rte_flow_action_queue *)
1082 enic_action->rq_idx =
1083 enic_rte_rq_idx_to_sop_idx(queue->index);
1084 enic_action->flags |= FILTER_ACTION_RQ_STEERING_FLAG;
1087 case RTE_FLOW_ACTION_TYPE_MARK: {
1088 const struct rte_flow_action_mark *mark =
1089 (const struct rte_flow_action_mark *)
1096 * Map mark ID (32-bit) to filter ID (16-bit):
1097 * - Reject values > 16 bits
1098 * - Filter ID 0 is reserved for filters that steer
1099 * but not mark. So add 1 to the mark ID to avoid
1101 * - Filter ID (ENIC_MAGIC_FILTER_ID = 0xffff) is
1102 * reserved for the "flag" action below.
1104 if (mark->id >= ENIC_MAGIC_FILTER_ID - 1)
1106 enic_action->filter_id = mark->id + 1;
1107 enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1110 case RTE_FLOW_ACTION_TYPE_FLAG: {
1114 /* ENIC_MAGIC_FILTER_ID is reserved for flagging */
1115 enic_action->filter_id = ENIC_MAGIC_FILTER_ID;
1116 enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1119 case RTE_FLOW_ACTION_TYPE_DROP: {
1123 enic_action->flags |= FILTER_ACTION_DROP_FLAG;
1126 case RTE_FLOW_ACTION_TYPE_COUNT: {
1127 enic_action->flags |= FILTER_ACTION_COUNTER_FLAG;
1130 case RTE_FLOW_ACTION_TYPE_RSS: {
1131 const struct rte_flow_action_rss *rss =
1132 (const struct rte_flow_action_rss *)
1138 * Hardware does not support general RSS actions, but
1139 * we can still support the dummy one that is used to
1140 * "receive normally".
1142 allow = rss->func == RTE_ETH_HASH_FUNCTION_DEFAULT &&
1145 rss->types == enic->rss_hf) &&
1146 rss->queue_num == enic->rq_count &&
1148 /* Identity queue map is ok */
1149 for (i = 0; i < rss->queue_num; i++)
1150 allow = allow && (i == rss->queue[i]);
1155 /* Need MARK or FLAG */
1156 if (!(overlap & MARK))
1161 case RTE_FLOW_ACTION_TYPE_PASSTHRU: {
1163 * Like RSS above, PASSTHRU + MARK may be used to
1164 * "mark and then receive normally". MARK usually comes
1165 * after PASSTHRU, so remember we have seen passthru
1166 * and check for mark later.
1174 case RTE_FLOW_ACTION_TYPE_VOID:
1181 /* Only PASSTHRU + MARK is allowed */
1182 if (passthru && !(overlap & MARK))
1184 if (!(overlap & FATE))
1186 enic_action->type = FILTER_ACTION_V2;
1190 /** Check if the action is supported */
1192 enic_match_action(const struct rte_flow_action *action,
1193 const enum rte_flow_action_type *supported_actions)
1195 for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
1196 supported_actions++) {
1197 if (action->type == *supported_actions)
1203 /** Get the NIC filter capabilties structure */
1204 static const struct enic_filter_cap *
1205 enic_get_filter_cap(struct enic *enic)
1207 if (enic->flow_filter_mode)
1208 return &enic_filter_cap[enic->flow_filter_mode];
1213 /** Get the actions for this NIC version. */
1214 static const struct enic_action_cap *
1215 enic_get_action_cap(struct enic *enic)
1217 const struct enic_action_cap *ea;
1220 actions = enic->filter_actions;
1221 if (actions & FILTER_ACTION_COUNTER_FLAG)
1222 ea = &enic_action_cap[FILTER_ACTION_COUNTER_FLAG];
1223 else if (actions & FILTER_ACTION_DROP_FLAG)
1224 ea = &enic_action_cap[FILTER_ACTION_DROP_FLAG];
1225 else if (actions & FILTER_ACTION_FILTER_ID_FLAG)
1226 ea = &enic_action_cap[FILTER_ACTION_FILTER_ID_FLAG];
1228 ea = &enic_action_cap[FILTER_ACTION_RQ_STEERING_FLAG];
1232 /* Debug function to dump internal NIC action structure. */
1234 enic_dump_actions(const struct filter_action_v2 *ea)
1236 if (ea->type == FILTER_ACTION_RQ_STEERING) {
1237 FLOW_LOG(INFO, "Action(V1), queue: %u\n", ea->rq_idx);
1238 } else if (ea->type == FILTER_ACTION_V2) {
1239 FLOW_LOG(INFO, "Actions(V2)\n");
1240 if (ea->flags & FILTER_ACTION_RQ_STEERING_FLAG)
1241 FLOW_LOG(INFO, "\tqueue: %u\n",
1242 enic_sop_rq_idx_to_rte_idx(ea->rq_idx));
1243 if (ea->flags & FILTER_ACTION_FILTER_ID_FLAG)
1244 FLOW_LOG(INFO, "\tfilter_id: %u\n", ea->filter_id);
1248 /* Debug function to dump internal NIC filter structure. */
1250 enic_dump_filter(const struct filter_v2 *filt)
1252 const struct filter_generic_1 *gp;
1255 char ip4[16], ip6[16], udp[16], tcp[16], tcpudp[16], ip4csum[16];
1256 char l4csum[16], ipfrag[16];
1258 switch (filt->type) {
1259 case FILTER_IPV4_5TUPLE:
1260 FLOW_LOG(INFO, "FILTER_IPV4_5TUPLE\n");
1262 case FILTER_USNIC_IP:
1264 /* FIXME: this should be a loop */
1265 gp = &filt->u.generic_1;
1266 FLOW_LOG(INFO, "Filter: vlan: 0x%04x, mask: 0x%04x\n",
1267 gp->val_vlan, gp->mask_vlan);
1269 if (gp->mask_flags & FILTER_GENERIC_1_IPV4)
1271 (gp->val_flags & FILTER_GENERIC_1_IPV4)
1272 ? "ip4(y)" : "ip4(n)");
1274 sprintf(ip4, "%s ", "ip4(x)");
1276 if (gp->mask_flags & FILTER_GENERIC_1_IPV6)
1278 (gp->val_flags & FILTER_GENERIC_1_IPV4)
1279 ? "ip6(y)" : "ip6(n)");
1281 sprintf(ip6, "%s ", "ip6(x)");
1283 if (gp->mask_flags & FILTER_GENERIC_1_UDP)
1285 (gp->val_flags & FILTER_GENERIC_1_UDP)
1286 ? "udp(y)" : "udp(n)");
1288 sprintf(udp, "%s ", "udp(x)");
1290 if (gp->mask_flags & FILTER_GENERIC_1_TCP)
1292 (gp->val_flags & FILTER_GENERIC_1_TCP)
1293 ? "tcp(y)" : "tcp(n)");
1295 sprintf(tcp, "%s ", "tcp(x)");
1297 if (gp->mask_flags & FILTER_GENERIC_1_TCP_OR_UDP)
1298 sprintf(tcpudp, "%s ",
1299 (gp->val_flags & FILTER_GENERIC_1_TCP_OR_UDP)
1300 ? "tcpudp(y)" : "tcpudp(n)");
1302 sprintf(tcpudp, "%s ", "tcpudp(x)");
1304 if (gp->mask_flags & FILTER_GENERIC_1_IP4SUM_OK)
1305 sprintf(ip4csum, "%s ",
1306 (gp->val_flags & FILTER_GENERIC_1_IP4SUM_OK)
1307 ? "ip4csum(y)" : "ip4csum(n)");
1309 sprintf(ip4csum, "%s ", "ip4csum(x)");
1311 if (gp->mask_flags & FILTER_GENERIC_1_L4SUM_OK)
1312 sprintf(l4csum, "%s ",
1313 (gp->val_flags & FILTER_GENERIC_1_L4SUM_OK)
1314 ? "l4csum(y)" : "l4csum(n)");
1316 sprintf(l4csum, "%s ", "l4csum(x)");
1318 if (gp->mask_flags & FILTER_GENERIC_1_IPFRAG)
1319 sprintf(ipfrag, "%s ",
1320 (gp->val_flags & FILTER_GENERIC_1_IPFRAG)
1321 ? "ipfrag(y)" : "ipfrag(n)");
1323 sprintf(ipfrag, "%s ", "ipfrag(x)");
1324 FLOW_LOG(INFO, "\tFlags: %s%s%s%s%s%s%s%s\n", ip4, ip6, udp,
1325 tcp, tcpudp, ip4csum, l4csum, ipfrag);
1327 for (i = 0; i < FILTER_GENERIC_1_NUM_LAYERS; i++) {
1328 mbyte = FILTER_GENERIC_1_KEY_LEN - 1;
1329 while (mbyte && !gp->layer[i].mask[mbyte])
1335 for (j = 0; j <= mbyte; j++) {
1337 gp->layer[i].mask[j]);
1341 FLOW_LOG(INFO, "\tL%u mask: %s\n", i + 2, buf);
1343 for (j = 0; j <= mbyte; j++) {
1345 gp->layer[i].val[j]);
1349 FLOW_LOG(INFO, "\tL%u val: %s\n", i + 2, buf);
1353 FLOW_LOG(INFO, "FILTER UNKNOWN\n");
1358 /* Debug function to dump internal NIC flow structures. */
1360 enic_dump_flow(const struct filter_action_v2 *ea, const struct filter_v2 *filt)
1362 enic_dump_filter(filt);
1363 enic_dump_actions(ea);
1368 * Internal flow parse/validate function.
1371 * This device pointer.
1372 * @param pattern[in]
1373 * @param actions[in]
1375 * @param enic_filter[out]
1376 * Internal NIC filter structure pointer.
1377 * @param enic_action[out]
1378 * Internal NIC action structure pointer.
1381 enic_flow_parse(struct rte_eth_dev *dev,
1382 const struct rte_flow_attr *attrs,
1383 const struct rte_flow_item pattern[],
1384 const struct rte_flow_action actions[],
1385 struct rte_flow_error *error,
1386 struct filter_v2 *enic_filter,
1387 struct filter_action_v2 *enic_action)
1389 unsigned int ret = 0;
1390 struct enic *enic = pmd_priv(dev);
1391 const struct enic_filter_cap *enic_filter_cap;
1392 const struct enic_action_cap *enic_action_cap;
1393 const struct rte_flow_action *action;
1397 memset(enic_filter, 0, sizeof(*enic_filter));
1398 memset(enic_action, 0, sizeof(*enic_action));
1401 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1402 NULL, "No pattern specified");
1407 rte_flow_error_set(error, EINVAL,
1408 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1409 NULL, "No action specified");
1415 rte_flow_error_set(error, ENOTSUP,
1416 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1418 "priority groups are not supported");
1420 } else if (attrs->priority) {
1421 rte_flow_error_set(error, ENOTSUP,
1422 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1424 "priorities are not supported");
1426 } else if (attrs->egress) {
1427 rte_flow_error_set(error, ENOTSUP,
1428 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
1430 "egress is not supported");
1432 } else if (attrs->transfer) {
1433 rte_flow_error_set(error, ENOTSUP,
1434 RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1436 "transfer is not supported");
1438 } else if (!attrs->ingress) {
1439 rte_flow_error_set(error, ENOTSUP,
1440 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1442 "only ingress is supported");
1447 rte_flow_error_set(error, EINVAL,
1448 RTE_FLOW_ERROR_TYPE_ATTR,
1449 NULL, "No attribute specified");
1453 /* Verify Actions. */
1454 enic_action_cap = enic_get_action_cap(enic);
1455 for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
1457 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1459 else if (!enic_match_action(action, enic_action_cap->actions))
1462 if (action->type != RTE_FLOW_ACTION_TYPE_END) {
1463 rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
1464 action, "Invalid action.");
1467 ret = enic_action_cap->copy_fn(enic, actions, enic_action);
1469 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1470 NULL, "Unsupported action.");
1474 /* Verify Flow items. If copying the filter from flow format to enic
1475 * format fails, the flow is not supported
1477 enic_filter_cap = enic_get_filter_cap(enic);
1478 if (enic_filter_cap == NULL) {
1479 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1480 NULL, "Flow API not available");
1483 enic_filter->type = enic->flow_filter_mode;
1484 ret = enic_copy_filter(pattern, enic_filter_cap,
1485 enic_filter, error);
1490 * Push filter/action to the NIC.
1493 * Device structure pointer.
1494 * @param enic_filter[in]
1495 * Internal NIC filter structure pointer.
1496 * @param enic_action[in]
1497 * Internal NIC action structure pointer.
1500 static struct rte_flow *
1501 enic_flow_add_filter(struct enic *enic, struct filter_v2 *enic_filter,
1502 struct filter_action_v2 *enic_action,
1503 struct rte_flow_error *error)
1505 struct rte_flow *flow;
1509 int last_max_flow_ctr;
1513 flow = rte_calloc(__func__, 1, sizeof(*flow), 0);
1515 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1516 NULL, "cannot allocate flow memory");
1520 flow->counter_idx = -1;
1521 last_max_flow_ctr = -1;
1522 if (enic_action->flags & FILTER_ACTION_COUNTER_FLAG) {
1523 if (!vnic_dev_counter_alloc(enic->vdev, (uint32_t *)&ctr_idx)) {
1524 rte_flow_error_set(error, ENOMEM,
1525 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1526 NULL, "cannot allocate counter");
1527 goto unwind_flow_alloc;
1529 flow->counter_idx = ctr_idx;
1530 enic_action->counter_index = ctr_idx;
1532 /* If index is the largest, increase the counter DMA size */
1533 if (ctr_idx > enic->max_flow_counter) {
1534 err = vnic_dev_counter_dma_cfg(enic->vdev,
1535 VNIC_FLOW_COUNTER_UPDATE_MSECS,
1538 rte_flow_error_set(error, -err,
1539 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1540 NULL, "counter DMA config failed");
1541 goto unwind_ctr_alloc;
1543 last_max_flow_ctr = enic->max_flow_counter;
1544 enic->max_flow_counter = ctr_idx;
1548 /* entry[in] is the queue id, entry[out] is the filter Id for delete */
1549 entry = enic_action->rq_idx;
1550 err = vnic_dev_classifier(enic->vdev, CLSF_ADD, &entry, enic_filter,
1553 rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_HANDLE,
1554 NULL, "vnic_dev_classifier error");
1555 goto unwind_ctr_dma_cfg;
1558 flow->enic_filter_id = entry;
1559 flow->enic_filter = *enic_filter;
1563 /* unwind if there are errors */
1565 if (last_max_flow_ctr != -1) {
1566 /* reduce counter DMA size */
1567 vnic_dev_counter_dma_cfg(enic->vdev,
1568 VNIC_FLOW_COUNTER_UPDATE_MSECS,
1569 last_max_flow_ctr + 1);
1570 enic->max_flow_counter = last_max_flow_ctr;
1573 if (flow->counter_idx != -1)
1574 vnic_dev_counter_free(enic->vdev, ctr_idx);
1581 * Remove filter/action from the NIC.
1584 * Device structure pointer.
1585 * @param filter_id[in]
1587 * @param enic_action[in]
1588 * Internal NIC action structure pointer.
1592 enic_flow_del_filter(struct enic *enic, struct rte_flow *flow,
1593 struct rte_flow_error *error)
1600 filter_id = flow->enic_filter_id;
1601 err = vnic_dev_classifier(enic->vdev, CLSF_DEL, &filter_id, NULL, NULL);
1603 rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_HANDLE,
1604 NULL, "vnic_dev_classifier failed");
1608 if (flow->counter_idx != -1) {
1609 if (!vnic_dev_counter_free(enic->vdev, flow->counter_idx))
1610 dev_err(enic, "counter free failed, idx: %d\n",
1612 flow->counter_idx = -1;
1618 * The following functions are callbacks for Generic flow API.
1622 * Validate a flow supported by the NIC.
1624 * @see rte_flow_validate()
1628 enic_flow_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attrs,
1629 const struct rte_flow_item pattern[],
1630 const struct rte_flow_action actions[],
1631 struct rte_flow_error *error)
1633 struct filter_v2 enic_filter;
1634 struct filter_action_v2 enic_action;
1639 ret = enic_flow_parse(dev, attrs, pattern, actions, error,
1640 &enic_filter, &enic_action);
1642 enic_dump_flow(&enic_action, &enic_filter);
1647 * Create a flow supported by the NIC.
1649 * @see rte_flow_create()
1652 static struct rte_flow *
1653 enic_flow_create(struct rte_eth_dev *dev,
1654 const struct rte_flow_attr *attrs,
1655 const struct rte_flow_item pattern[],
1656 const struct rte_flow_action actions[],
1657 struct rte_flow_error *error)
1660 struct filter_v2 enic_filter;
1661 struct filter_action_v2 enic_action;
1662 struct rte_flow *flow;
1663 struct enic *enic = pmd_priv(dev);
1667 ret = enic_flow_parse(dev, attrs, pattern, actions, error, &enic_filter,
1672 rte_spinlock_lock(&enic->flows_lock);
1673 flow = enic_flow_add_filter(enic, &enic_filter, &enic_action,
1676 LIST_INSERT_HEAD(&enic->flows, flow, next);
1677 rte_spinlock_unlock(&enic->flows_lock);
1683 * Destroy a flow supported by the NIC.
1685 * @see rte_flow_destroy()
1689 enic_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
1690 __rte_unused struct rte_flow_error *error)
1692 struct enic *enic = pmd_priv(dev);
1696 rte_spinlock_lock(&enic->flows_lock);
1697 enic_flow_del_filter(enic, flow, error);
1698 LIST_REMOVE(flow, next);
1699 rte_spinlock_unlock(&enic->flows_lock);
1705 * Flush all flows on the device.
1707 * @see rte_flow_flush()
1711 enic_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1713 struct rte_flow *flow;
1714 struct enic *enic = pmd_priv(dev);
1718 rte_spinlock_lock(&enic->flows_lock);
1720 while (!LIST_EMPTY(&enic->flows)) {
1721 flow = LIST_FIRST(&enic->flows);
1722 enic_flow_del_filter(enic, flow, error);
1723 LIST_REMOVE(flow, next);
1726 rte_spinlock_unlock(&enic->flows_lock);
1731 enic_flow_query_count(struct rte_eth_dev *dev,
1732 struct rte_flow *flow, void *data,
1733 struct rte_flow_error *error)
1735 struct enic *enic = pmd_priv(dev);
1736 struct rte_flow_query_count *query;
1737 uint64_t packets, bytes;
1741 if (flow->counter_idx == -1) {
1742 return rte_flow_error_set(error, ENOTSUP,
1743 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1745 "flow does not have counter");
1747 query = (struct rte_flow_query_count *)data;
1748 if (!vnic_dev_counter_query(enic->vdev, flow->counter_idx,
1749 !!query->reset, &packets, &bytes)) {
1750 return rte_flow_error_set
1752 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1754 "cannot read counter");
1756 query->hits_set = 1;
1757 query->bytes_set = 1;
1758 query->hits = packets;
1759 query->bytes = bytes;
1764 enic_flow_query(struct rte_eth_dev *dev,
1765 struct rte_flow *flow,
1766 const struct rte_flow_action *actions,
1768 struct rte_flow_error *error)
1774 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1775 switch (actions->type) {
1776 case RTE_FLOW_ACTION_TYPE_VOID:
1778 case RTE_FLOW_ACTION_TYPE_COUNT:
1779 ret = enic_flow_query_count(dev, flow, data, error);
1782 return rte_flow_error_set(error, ENOTSUP,
1783 RTE_FLOW_ERROR_TYPE_ACTION,
1785 "action not supported");
1794 * Flow callback registration.
1798 const struct rte_flow_ops enic_flow_ops = {
1799 .validate = enic_flow_validate,
1800 .create = enic_flow_create,
1801 .destroy = enic_flow_destroy,
1802 .flush = enic_flow_flush,
1803 .query = enic_flow_query,