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;
841 * The NIC filter API has no flags for "match vxlan". Set UDP port to
842 * avoid false positives.
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 */
855 mask = &rte_flow_item_vxlan_mask;
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));
862 *inner_ofst = sizeof(struct vxlan_hdr);
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
872 enic_copy_item_raw_v2(struct copy_item_args *arg)
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;
883 /* Cannot be used for inner packet */
886 /* Need both spec and mask */
889 /* Only supports relative with offset 0 */
890 if (!spec->relative || spec->offset != 0 || spec->search || spec->limit)
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)
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.
901 * No more pattern follows this, so append to the L4 layer instead of
902 * L5 to work with both recent and older VICs.
904 if (mask->length != 0 && mask->length < spec->length)
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);
915 * Return 1 if current item is valid on top of the previous one.
917 * @param prev_item[in]
918 * The item before this one in the pattern or RTE_FLOW_ITEM_TYPE_END if this
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.
926 item_stacking_valid(enum rte_flow_item_type prev_item,
927 const struct enic_items *item_info, u8 is_first_item)
929 enum rte_flow_item_type const *allowed_items = item_info->prev_items;
933 for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
934 if (prev_item == *allowed_items)
938 /* This is the first item in the stack. Check if that's cool */
939 if (is_first_item && item_info->valid_start_item)
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.
951 fixup_l5_layer(struct enic *enic, struct filter_generic_1 *gp,
954 uint8_t layer[FILTER_GENERIC_1_KEY_LEN];
958 if (!(inner_ofst > 0 && enic->vxlan))
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));
976 * Build the intenal enic filter structure from the provided pattern. The
977 * pattern is validated as the items are copied.
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.
987 enic_copy_filter(const struct rte_flow_item pattern[],
988 const struct enic_filter_cap *cap,
990 struct filter_v2 *enic_filter,
991 struct rte_flow_error *error)
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;
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.
1011 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
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.");
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;
1028 ret = item_info->copy_item(&args);
1030 goto item_not_supported;
1031 prev_item = item->type;
1034 fixup_l5_layer(enic, &enic_filter->u.generic_1, inner_ofst);
1039 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_ITEM,
1040 NULL, "enic type error");
1044 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
1045 item, "stacking error");
1050 * Build the intenal version 1 NIC action structure from the provided pattern.
1051 * The pattern is validated as the items are copied.
1053 * @param actions[in]
1054 * @param enic_action[out]
1055 * NIC specfilc actions derived from the actions.
1059 enic_copy_action_v1(__rte_unused struct enic *enic,
1060 const struct rte_flow_action actions[],
1061 struct filter_action_v2 *enic_action)
1064 uint32_t overlap = 0;
1068 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1069 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID)
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 *)
1081 enic_action->rq_idx =
1082 enic_rte_rq_idx_to_sop_idx(queue->index);
1090 if (!(overlap & FATE))
1092 enic_action->type = FILTER_ACTION_RQ_STEERING;
1097 * Build the intenal version 2 NIC action structure from the provided pattern.
1098 * The pattern is validated as the items are copied.
1100 * @param actions[in]
1101 * @param enic_action[out]
1102 * NIC specfilc actions derived from the actions.
1106 enic_copy_action_v2(struct enic *enic,
1107 const struct rte_flow_action actions[],
1108 struct filter_action_v2 *enic_action)
1110 enum { FATE = 1, MARK = 2, };
1111 uint32_t overlap = 0;
1112 bool passthru = false;
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 *)
1126 enic_action->rq_idx =
1127 enic_rte_rq_idx_to_sop_idx(queue->index);
1128 enic_action->flags |= FILTER_ACTION_RQ_STEERING_FLAG;
1131 case RTE_FLOW_ACTION_TYPE_MARK: {
1132 const struct rte_flow_action_mark *mark =
1133 (const struct rte_flow_action_mark *)
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
1145 * - Filter ID (ENIC_MAGIC_FILTER_ID = 0xffff) is
1146 * reserved for the "flag" action below.
1148 if (mark->id >= ENIC_MAGIC_FILTER_ID - 1)
1150 enic_action->filter_id = mark->id + 1;
1151 enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1154 case RTE_FLOW_ACTION_TYPE_FLAG: {
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;
1163 case RTE_FLOW_ACTION_TYPE_DROP: {
1167 enic_action->flags |= FILTER_ACTION_DROP_FLAG;
1170 case RTE_FLOW_ACTION_TYPE_COUNT: {
1171 enic_action->flags |= FILTER_ACTION_COUNTER_FLAG;
1174 case RTE_FLOW_ACTION_TYPE_RSS: {
1175 const struct rte_flow_action_rss *rss =
1176 (const struct rte_flow_action_rss *)
1182 * Hardware does not support general RSS actions, but
1183 * we can still support the dummy one that is used to
1184 * "receive normally".
1186 allow = rss->func == RTE_ETH_HASH_FUNCTION_DEFAULT &&
1189 rss->types == enic->rss_hf) &&
1190 rss->queue_num == enic->rq_count &&
1192 /* Identity queue map is ok */
1193 for (i = 0; i < rss->queue_num; i++)
1194 allow = allow && (i == rss->queue[i]);
1199 /* Need MARK or FLAG */
1200 if (!(overlap & MARK))
1205 case RTE_FLOW_ACTION_TYPE_PASSTHRU: {
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.
1218 case RTE_FLOW_ACTION_TYPE_VOID:
1225 /* Only PASSTHRU + MARK is allowed */
1226 if (passthru && !(overlap & MARK))
1228 if (!(overlap & FATE))
1230 enic_action->type = FILTER_ACTION_V2;
1234 /** Check if the action is supported */
1236 enic_match_action(const struct rte_flow_action *action,
1237 const enum rte_flow_action_type *supported_actions)
1239 for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
1240 supported_actions++) {
1241 if (action->type == *supported_actions)
1247 /** Get the NIC filter capabilties structure */
1248 static const struct enic_filter_cap *
1249 enic_get_filter_cap(struct enic *enic)
1251 if (enic->flow_filter_mode)
1252 return &enic_filter_cap[enic->flow_filter_mode];
1257 /** Get the actions for this NIC version. */
1258 static const struct enic_action_cap *
1259 enic_get_action_cap(struct enic *enic)
1261 const struct enic_action_cap *ea;
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];
1272 ea = &enic_action_cap[FILTER_ACTION_RQ_STEERING_FLAG];
1276 /* Debug function to dump internal NIC action structure. */
1278 enic_dump_actions(const struct filter_action_v2 *ea)
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);
1292 /* Debug function to dump internal NIC filter structure. */
1294 enic_dump_filter(const struct filter_v2 *filt)
1296 const struct filter_generic_1 *gp;
1299 char ip4[16], ip6[16], udp[16], tcp[16], tcpudp[16], ip4csum[16];
1300 char l4csum[16], ipfrag[16];
1302 switch (filt->type) {
1303 case FILTER_IPV4_5TUPLE:
1304 FLOW_LOG(INFO, "FILTER_IPV4_5TUPLE\n");
1306 case FILTER_USNIC_IP:
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);
1313 if (gp->mask_flags & FILTER_GENERIC_1_IPV4)
1315 (gp->val_flags & FILTER_GENERIC_1_IPV4)
1316 ? "ip4(y)" : "ip4(n)");
1318 sprintf(ip4, "%s ", "ip4(x)");
1320 if (gp->mask_flags & FILTER_GENERIC_1_IPV6)
1322 (gp->val_flags & FILTER_GENERIC_1_IPV4)
1323 ? "ip6(y)" : "ip6(n)");
1325 sprintf(ip6, "%s ", "ip6(x)");
1327 if (gp->mask_flags & FILTER_GENERIC_1_UDP)
1329 (gp->val_flags & FILTER_GENERIC_1_UDP)
1330 ? "udp(y)" : "udp(n)");
1332 sprintf(udp, "%s ", "udp(x)");
1334 if (gp->mask_flags & FILTER_GENERIC_1_TCP)
1336 (gp->val_flags & FILTER_GENERIC_1_TCP)
1337 ? "tcp(y)" : "tcp(n)");
1339 sprintf(tcp, "%s ", "tcp(x)");
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)");
1346 sprintf(tcpudp, "%s ", "tcpudp(x)");
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)");
1353 sprintf(ip4csum, "%s ", "ip4csum(x)");
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)");
1360 sprintf(l4csum, "%s ", "l4csum(x)");
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)");
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);
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])
1379 for (j = 0; j <= mbyte; j++) {
1381 gp->layer[i].mask[j]);
1385 FLOW_LOG(INFO, "\tL%u mask: %s\n", i + 2, buf);
1387 for (j = 0; j <= mbyte; j++) {
1389 gp->layer[i].val[j]);
1393 FLOW_LOG(INFO, "\tL%u val: %s\n", i + 2, buf);
1397 FLOW_LOG(INFO, "FILTER UNKNOWN\n");
1402 /* Debug function to dump internal NIC flow structures. */
1404 enic_dump_flow(const struct filter_action_v2 *ea, const struct filter_v2 *filt)
1406 enic_dump_filter(filt);
1407 enic_dump_actions(ea);
1412 * Internal flow parse/validate function.
1415 * This device pointer.
1416 * @param pattern[in]
1417 * @param actions[in]
1419 * @param enic_filter[out]
1420 * Internal NIC filter structure pointer.
1421 * @param enic_action[out]
1422 * Internal NIC action structure pointer.
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)
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;
1441 memset(enic_filter, 0, sizeof(*enic_filter));
1442 memset(enic_action, 0, sizeof(*enic_action));
1445 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1446 NULL, "No pattern specified");
1451 rte_flow_error_set(error, EINVAL,
1452 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1453 NULL, "No action specified");
1459 rte_flow_error_set(error, ENOTSUP,
1460 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1462 "priority groups are not supported");
1464 } else if (attrs->priority) {
1465 rte_flow_error_set(error, ENOTSUP,
1466 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1468 "priorities are not supported");
1470 } else if (attrs->egress) {
1471 rte_flow_error_set(error, ENOTSUP,
1472 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
1474 "egress is not supported");
1476 } else if (attrs->transfer) {
1477 rte_flow_error_set(error, ENOTSUP,
1478 RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1480 "transfer is not supported");
1482 } else if (!attrs->ingress) {
1483 rte_flow_error_set(error, ENOTSUP,
1484 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1486 "only ingress is supported");
1491 rte_flow_error_set(error, EINVAL,
1492 RTE_FLOW_ERROR_TYPE_ATTR,
1493 NULL, "No attribute specified");
1497 /* Verify Actions. */
1498 enic_action_cap = enic_get_action_cap(enic);
1499 for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
1501 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1503 else if (!enic_match_action(action, enic_action_cap->actions))
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.");
1511 ret = enic_action_cap->copy_fn(enic, actions, enic_action);
1513 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1514 NULL, "Unsupported action.");
1518 /* Verify Flow items. If copying the filter from flow format to enic
1519 * format fails, the flow is not supported
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");
1527 enic_filter->type = enic->flow_filter_mode;
1528 ret = enic_copy_filter(pattern, enic_filter_cap, enic,
1529 enic_filter, error);
1534 * Push filter/action to the NIC.
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.
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)
1549 struct rte_flow *flow;
1553 int last_max_flow_ctr;
1557 flow = rte_calloc(__func__, 1, sizeof(*flow), 0);
1559 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1560 NULL, "cannot allocate flow memory");
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;
1573 flow->counter_idx = ctr_idx;
1574 enic_action->counter_index = ctr_idx;
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,
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;
1587 last_max_flow_ctr = enic->max_flow_counter;
1588 enic->max_flow_counter = ctr_idx;
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,
1597 rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_HANDLE,
1598 NULL, "vnic_dev_classifier error");
1599 goto unwind_ctr_dma_cfg;
1602 flow->enic_filter_id = entry;
1603 flow->enic_filter = *enic_filter;
1607 /* unwind if there are errors */
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;
1617 if (flow->counter_idx != -1)
1618 vnic_dev_counter_free(enic->vdev, ctr_idx);
1625 * Remove filter/action from the NIC.
1628 * Device structure pointer.
1629 * @param filter_id[in]
1631 * @param enic_action[in]
1632 * Internal NIC action structure pointer.
1636 enic_flow_del_filter(struct enic *enic, struct rte_flow *flow,
1637 struct rte_flow_error *error)
1644 filter_id = flow->enic_filter_id;
1645 err = vnic_dev_classifier(enic->vdev, CLSF_DEL, &filter_id, NULL, NULL);
1647 rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_HANDLE,
1648 NULL, "vnic_dev_classifier failed");
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",
1656 flow->counter_idx = -1;
1662 * The following functions are callbacks for Generic flow API.
1666 * Validate a flow supported by the NIC.
1668 * @see rte_flow_validate()
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)
1677 struct filter_v2 enic_filter;
1678 struct filter_action_v2 enic_action;
1683 ret = enic_flow_parse(dev, attrs, pattern, actions, error,
1684 &enic_filter, &enic_action);
1686 enic_dump_flow(&enic_action, &enic_filter);
1691 * Create a flow supported by the NIC.
1693 * @see rte_flow_create()
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)
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);
1711 ret = enic_flow_parse(dev, attrs, pattern, actions, error, &enic_filter,
1716 rte_spinlock_lock(&enic->flows_lock);
1717 flow = enic_flow_add_filter(enic, &enic_filter, &enic_action,
1720 LIST_INSERT_HEAD(&enic->flows, flow, next);
1721 rte_spinlock_unlock(&enic->flows_lock);
1727 * Destroy a flow supported by the NIC.
1729 * @see rte_flow_destroy()
1733 enic_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
1734 __rte_unused struct rte_flow_error *error)
1736 struct enic *enic = pmd_priv(dev);
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);
1749 * Flush all flows on the device.
1751 * @see rte_flow_flush()
1755 enic_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1757 struct rte_flow *flow;
1758 struct enic *enic = pmd_priv(dev);
1762 rte_spinlock_lock(&enic->flows_lock);
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);
1770 rte_spinlock_unlock(&enic->flows_lock);
1775 enic_flow_query_count(struct rte_eth_dev *dev,
1776 struct rte_flow *flow, void *data,
1777 struct rte_flow_error *error)
1779 struct enic *enic = pmd_priv(dev);
1780 struct rte_flow_query_count *query;
1781 uint64_t packets, bytes;
1785 if (flow->counter_idx == -1) {
1786 return rte_flow_error_set(error, ENOTSUP,
1787 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1789 "flow does not have counter");
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
1796 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1798 "cannot read counter");
1800 query->hits_set = 1;
1801 query->bytes_set = 1;
1802 query->hits = packets;
1803 query->bytes = bytes;
1808 enic_flow_query(struct rte_eth_dev *dev,
1809 struct rte_flow *flow,
1810 const struct rte_flow_action *actions,
1812 struct rte_flow_error *error)
1818 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1819 switch (actions->type) {
1820 case RTE_FLOW_ACTION_TYPE_VOID:
1822 case RTE_FLOW_ACTION_TYPE_COUNT:
1823 ret = enic_flow_query_count(dev, flow, data, error);
1826 return rte_flow_error_set(error, ENOTSUP,
1827 RTE_FLOW_ERROR_TYPE_ACTION,
1829 "action not supported");
1838 * Flow callback registration.
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,