2 * Copyright (c) 2017, Cisco Systems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
34 #include <rte_ethdev.h>
35 #include <rte_flow_driver.h>
36 #include <rte_ether.h>
40 #include "enic_compat.h"
45 #ifdef RTE_LIBRTE_ENIC_DEBUG_FLOW
46 #define FLOW_TRACE() \
47 RTE_LOG(DEBUG, PMD, "%s()\n", __func__)
48 #define FLOW_LOG(level, fmt, args...) \
49 RTE_LOG(level, PMD, fmt, ## args)
51 #define FLOW_TRACE() do { } while (0)
52 #define FLOW_LOG(level, fmt, args...) do { } while (0)
55 /** Info about how to copy items into enic filters. */
57 /** Function for copying and validating an item. */
58 int (*copy_item)(const struct rte_flow_item *item,
59 struct filter_v2 *enic_filter, u8 *inner_ofst);
60 /** List of valid previous items. */
61 const enum rte_flow_item_type * const prev_items;
62 /** True if it's OK for this item to be the first item. For some NIC
63 * versions, it's invalid to start the stack above layer 3.
65 const u8 valid_start_item;
68 /** Filtering capabilities for various NIC and firmware versions. */
69 struct enic_filter_cap {
70 /** list of valid items and their handlers and attributes. */
71 const struct enic_items *item_info;
74 /* functions for copying flow actions into enic actions */
75 typedef int (copy_action_fn)(const struct rte_flow_action actions[],
76 struct filter_action_v2 *enic_action);
78 /* functions for copying items into enic filters */
79 typedef int(enic_copy_item_fn)(const struct rte_flow_item *item,
80 struct filter_v2 *enic_filter, u8 *inner_ofst);
82 /** Action capabilities for various NICs. */
83 struct enic_action_cap {
84 /** list of valid actions */
85 const enum rte_flow_action_type *actions;
86 /** copy function for a particular NIC */
87 int (*copy_fn)(const struct rte_flow_action actions[],
88 struct filter_action_v2 *enic_action);
91 /* Forward declarations */
92 static enic_copy_item_fn enic_copy_item_eth_v2;
93 static enic_copy_item_fn enic_copy_item_vlan_v2;
94 static enic_copy_item_fn enic_copy_item_ipv4_v2;
95 static enic_copy_item_fn enic_copy_item_ipv6_v2;
96 static enic_copy_item_fn enic_copy_item_udp_v2;
97 static enic_copy_item_fn enic_copy_item_tcp_v2;
98 static enic_copy_item_fn enic_copy_item_sctp_v2;
99 static enic_copy_item_fn enic_copy_item_sctp_v2;
100 static enic_copy_item_fn enic_copy_item_vxlan_v2;
101 static copy_action_fn enic_copy_action_v2;
103 /** NICs with Advanced filters enabled */
104 static const struct enic_items enic_items_v3[] = {
105 [RTE_FLOW_ITEM_TYPE_ETH] = {
106 .copy_item = enic_copy_item_eth_v2,
107 .valid_start_item = 1,
108 .prev_items = (const enum rte_flow_item_type[]) {
109 RTE_FLOW_ITEM_TYPE_VXLAN,
110 RTE_FLOW_ITEM_TYPE_END,
113 [RTE_FLOW_ITEM_TYPE_VLAN] = {
114 .copy_item = enic_copy_item_vlan_v2,
115 .valid_start_item = 1,
116 .prev_items = (const enum rte_flow_item_type[]) {
117 RTE_FLOW_ITEM_TYPE_ETH,
118 RTE_FLOW_ITEM_TYPE_END,
121 [RTE_FLOW_ITEM_TYPE_IPV4] = {
122 .copy_item = enic_copy_item_ipv4_v2,
123 .valid_start_item = 1,
124 .prev_items = (const enum rte_flow_item_type[]) {
125 RTE_FLOW_ITEM_TYPE_ETH,
126 RTE_FLOW_ITEM_TYPE_VLAN,
127 RTE_FLOW_ITEM_TYPE_END,
130 [RTE_FLOW_ITEM_TYPE_IPV6] = {
131 .copy_item = enic_copy_item_ipv6_v2,
132 .valid_start_item = 1,
133 .prev_items = (const enum rte_flow_item_type[]) {
134 RTE_FLOW_ITEM_TYPE_ETH,
135 RTE_FLOW_ITEM_TYPE_VLAN,
136 RTE_FLOW_ITEM_TYPE_END,
139 [RTE_FLOW_ITEM_TYPE_UDP] = {
140 .copy_item = enic_copy_item_udp_v2,
141 .valid_start_item = 1,
142 .prev_items = (const enum rte_flow_item_type[]) {
143 RTE_FLOW_ITEM_TYPE_IPV4,
144 RTE_FLOW_ITEM_TYPE_IPV6,
145 RTE_FLOW_ITEM_TYPE_END,
148 [RTE_FLOW_ITEM_TYPE_TCP] = {
149 .copy_item = enic_copy_item_tcp_v2,
150 .valid_start_item = 1,
151 .prev_items = (const enum rte_flow_item_type[]) {
152 RTE_FLOW_ITEM_TYPE_IPV4,
153 RTE_FLOW_ITEM_TYPE_IPV6,
154 RTE_FLOW_ITEM_TYPE_END,
157 [RTE_FLOW_ITEM_TYPE_SCTP] = {
158 .copy_item = enic_copy_item_sctp_v2,
159 .valid_start_item = 1,
160 .prev_items = (const enum rte_flow_item_type[]) {
161 RTE_FLOW_ITEM_TYPE_IPV4,
162 RTE_FLOW_ITEM_TYPE_IPV6,
163 RTE_FLOW_ITEM_TYPE_END,
166 [RTE_FLOW_ITEM_TYPE_VXLAN] = {
167 .copy_item = enic_copy_item_vxlan_v2,
168 .valid_start_item = 1,
169 .prev_items = (const enum rte_flow_item_type[]) {
170 RTE_FLOW_ITEM_TYPE_UDP,
171 RTE_FLOW_ITEM_TYPE_END,
176 /** Filtering capabilities indexed this NICs supported filter type. */
177 static const struct enic_filter_cap enic_filter_cap[] = {
179 .item_info = enic_items_v3,
183 /** Supported actions for newer NICs */
184 static const enum rte_flow_action_type enic_supported_actions_v2[] = {
185 RTE_FLOW_ACTION_TYPE_QUEUE,
186 RTE_FLOW_ACTION_TYPE_MARK,
187 RTE_FLOW_ACTION_TYPE_FLAG,
188 RTE_FLOW_ACTION_TYPE_END,
191 /** Action capabilities indexed by NIC version information */
192 static const struct enic_action_cap enic_action_cap[] = {
193 [FILTER_ACTION_V2_ALL] = {
194 .actions = enic_supported_actions_v2,
195 .copy_fn = enic_copy_action_v2,
199 * Copy ETH item into version 2 NIC filter.
202 * Item specification.
203 * @param enic_filter[out]
204 * Partially filled in NIC filter structure.
205 * @param inner_ofst[in]
206 * If zero, this is an outer header. If non-zero, this is the offset into L5
207 * where the header begins.
210 enic_copy_item_eth_v2(const struct rte_flow_item *item,
211 struct filter_v2 *enic_filter, u8 *inner_ofst)
213 struct ether_hdr enic_spec;
214 struct ether_hdr enic_mask;
215 const struct rte_flow_item_eth *spec = item->spec;
216 const struct rte_flow_item_eth *mask = item->mask;
217 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
221 /* Match all if no spec */
226 mask = &rte_flow_item_eth_mask;
228 memcpy(enic_spec.d_addr.addr_bytes, spec->dst.addr_bytes,
230 memcpy(enic_spec.s_addr.addr_bytes, spec->src.addr_bytes,
233 memcpy(enic_mask.d_addr.addr_bytes, mask->dst.addr_bytes,
235 memcpy(enic_mask.s_addr.addr_bytes, mask->src.addr_bytes,
237 enic_spec.ether_type = spec->type;
238 enic_mask.ether_type = mask->type;
240 if (*inner_ofst == 0) {
242 memcpy(gp->layer[FILTER_GENERIC_1_L2].mask, &enic_mask,
243 sizeof(struct ether_hdr));
244 memcpy(gp->layer[FILTER_GENERIC_1_L2].val, &enic_spec,
245 sizeof(struct ether_hdr));
248 if ((*inner_ofst + sizeof(struct ether_hdr)) >
249 FILTER_GENERIC_1_KEY_LEN)
251 /* Offset into L5 where inner Ethernet header goes */
252 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
253 &enic_mask, sizeof(struct ether_hdr));
254 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
255 &enic_spec, sizeof(struct ether_hdr));
256 *inner_ofst += sizeof(struct ether_hdr);
262 * Copy VLAN item into version 2 NIC filter.
265 * Item specification.
266 * @param enic_filter[out]
267 * Partially filled in NIC filter structure.
268 * @param inner_ofst[in]
269 * If zero, this is an outer header. If non-zero, this is the offset into L5
270 * where the header begins.
273 enic_copy_item_vlan_v2(const struct rte_flow_item *item,
274 struct filter_v2 *enic_filter, u8 *inner_ofst)
276 const struct rte_flow_item_vlan *spec = item->spec;
277 const struct rte_flow_item_vlan *mask = item->mask;
278 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
282 /* Match all if no spec */
286 /* Don't support filtering in tpid */
291 mask = &rte_flow_item_vlan_mask;
292 RTE_ASSERT(mask->tpid == 0);
295 if (*inner_ofst == 0) {
296 /* Outer header. Use the vlan mask/val fields */
297 gp->mask_vlan = mask->tci;
298 gp->val_vlan = spec->tci;
300 /* Inner header. Mask/Val start at *inner_ofst into L5 */
301 if ((*inner_ofst + sizeof(struct vlan_hdr)) >
302 FILTER_GENERIC_1_KEY_LEN)
304 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
305 mask, sizeof(struct vlan_hdr));
306 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
307 spec, sizeof(struct vlan_hdr));
308 *inner_ofst += sizeof(struct vlan_hdr);
314 * Copy IPv4 item into version 2 NIC filter.
317 * Item specification.
318 * @param enic_filter[out]
319 * Partially filled in NIC filter structure.
320 * @param inner_ofst[in]
321 * Must be 0. Don't support inner IPv4 filtering.
324 enic_copy_item_ipv4_v2(const struct rte_flow_item *item,
325 struct filter_v2 *enic_filter, u8 *inner_ofst)
327 const struct rte_flow_item_ipv4 *spec = item->spec;
328 const struct rte_flow_item_ipv4 *mask = item->mask;
329 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
333 if (*inner_ofst == 0) {
335 gp->mask_flags |= FILTER_GENERIC_1_IPV4;
336 gp->val_flags |= FILTER_GENERIC_1_IPV4;
338 /* Match all if no spec */
343 mask = &rte_flow_item_ipv4_mask;
345 memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
346 sizeof(struct ipv4_hdr));
347 memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
348 sizeof(struct ipv4_hdr));
350 /* Inner IPv4 header. Mask/Val start at *inner_ofst into L5 */
351 if ((*inner_ofst + sizeof(struct ipv4_hdr)) >
352 FILTER_GENERIC_1_KEY_LEN)
354 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
355 mask, sizeof(struct ipv4_hdr));
356 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
357 spec, sizeof(struct ipv4_hdr));
358 *inner_ofst += sizeof(struct ipv4_hdr);
364 * Copy IPv6 item into version 2 NIC filter.
367 * Item specification.
368 * @param enic_filter[out]
369 * Partially filled in NIC filter structure.
370 * @param inner_ofst[in]
371 * Must be 0. Don't support inner IPv6 filtering.
374 enic_copy_item_ipv6_v2(const struct rte_flow_item *item,
375 struct filter_v2 *enic_filter, u8 *inner_ofst)
377 const struct rte_flow_item_ipv6 *spec = item->spec;
378 const struct rte_flow_item_ipv6 *mask = item->mask;
379 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
384 gp->mask_flags |= FILTER_GENERIC_1_IPV6;
385 gp->val_flags |= FILTER_GENERIC_1_IPV6;
387 /* Match all if no spec */
392 mask = &rte_flow_item_ipv6_mask;
394 if (*inner_ofst == 0) {
395 memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
396 sizeof(struct ipv6_hdr));
397 memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
398 sizeof(struct ipv6_hdr));
400 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
401 if ((*inner_ofst + sizeof(struct ipv6_hdr)) >
402 FILTER_GENERIC_1_KEY_LEN)
404 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
405 mask, sizeof(struct ipv6_hdr));
406 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
407 spec, sizeof(struct ipv6_hdr));
408 *inner_ofst += sizeof(struct ipv6_hdr);
414 * Copy UDP item into version 2 NIC filter.
417 * Item specification.
418 * @param enic_filter[out]
419 * Partially filled in NIC filter structure.
420 * @param inner_ofst[in]
421 * Must be 0. Don't support inner UDP filtering.
424 enic_copy_item_udp_v2(const struct rte_flow_item *item,
425 struct filter_v2 *enic_filter, u8 *inner_ofst)
427 const struct rte_flow_item_udp *spec = item->spec;
428 const struct rte_flow_item_udp *mask = item->mask;
429 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
434 gp->mask_flags |= FILTER_GENERIC_1_UDP;
435 gp->val_flags |= FILTER_GENERIC_1_UDP;
437 /* Match all if no spec */
442 mask = &rte_flow_item_udp_mask;
444 if (*inner_ofst == 0) {
445 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
446 sizeof(struct udp_hdr));
447 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
448 sizeof(struct udp_hdr));
450 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
451 if ((*inner_ofst + sizeof(struct udp_hdr)) >
452 FILTER_GENERIC_1_KEY_LEN)
454 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
455 mask, sizeof(struct udp_hdr));
456 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
457 spec, sizeof(struct udp_hdr));
458 *inner_ofst += sizeof(struct udp_hdr);
464 * Copy TCP item into version 2 NIC filter.
467 * Item specification.
468 * @param enic_filter[out]
469 * Partially filled in NIC filter structure.
470 * @param inner_ofst[in]
471 * Must be 0. Don't support inner TCP filtering.
474 enic_copy_item_tcp_v2(const struct rte_flow_item *item,
475 struct filter_v2 *enic_filter, u8 *inner_ofst)
477 const struct rte_flow_item_tcp *spec = item->spec;
478 const struct rte_flow_item_tcp *mask = item->mask;
479 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
484 gp->mask_flags |= FILTER_GENERIC_1_TCP;
485 gp->val_flags |= FILTER_GENERIC_1_TCP;
487 /* Match all if no spec */
494 if (*inner_ofst == 0) {
495 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
496 sizeof(struct tcp_hdr));
497 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
498 sizeof(struct tcp_hdr));
500 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
501 if ((*inner_ofst + sizeof(struct tcp_hdr)) >
502 FILTER_GENERIC_1_KEY_LEN)
504 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
505 mask, sizeof(struct tcp_hdr));
506 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
507 spec, sizeof(struct tcp_hdr));
508 *inner_ofst += sizeof(struct tcp_hdr);
514 * Copy SCTP item into version 2 NIC filter.
517 * Item specification.
518 * @param enic_filter[out]
519 * Partially filled in NIC filter structure.
520 * @param inner_ofst[in]
521 * Must be 0. Don't support inner SCTP filtering.
524 enic_copy_item_sctp_v2(const struct rte_flow_item *item,
525 struct filter_v2 *enic_filter, u8 *inner_ofst)
527 const struct rte_flow_item_sctp *spec = item->spec;
528 const struct rte_flow_item_sctp *mask = item->mask;
529 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
536 /* Match all if no spec */
541 mask = &rte_flow_item_sctp_mask;
543 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
544 sizeof(struct sctp_hdr));
545 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
546 sizeof(struct sctp_hdr));
551 * Copy UDP item into version 2 NIC filter.
554 * Item specification.
555 * @param enic_filter[out]
556 * Partially filled in NIC filter structure.
557 * @param inner_ofst[in]
558 * Must be 0. VxLAN headers always start at the beginning of L5.
561 enic_copy_item_vxlan_v2(const struct rte_flow_item *item,
562 struct filter_v2 *enic_filter, u8 *inner_ofst)
564 const struct rte_flow_item_vxlan *spec = item->spec;
565 const struct rte_flow_item_vxlan *mask = item->mask;
566 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
573 /* Match all if no spec */
578 mask = &rte_flow_item_vxlan_mask;
580 memcpy(gp->layer[FILTER_GENERIC_1_L5].mask, mask,
581 sizeof(struct vxlan_hdr));
582 memcpy(gp->layer[FILTER_GENERIC_1_L5].val, spec,
583 sizeof(struct vxlan_hdr));
585 *inner_ofst = sizeof(struct vxlan_hdr);
590 * Return 1 if current item is valid on top of the previous one.
592 * @param prev_item[in]
593 * The item before this one in the pattern or RTE_FLOW_ITEM_TYPE_END if this
595 * @param item_info[in]
596 * Info about this item, like valid previous items.
597 * @param is_first[in]
598 * True if this the first item in the pattern.
601 item_stacking_valid(enum rte_flow_item_type prev_item,
602 const struct enic_items *item_info, u8 is_first_item)
604 enum rte_flow_item_type const *allowed_items = item_info->prev_items;
608 for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
609 if (prev_item == *allowed_items)
613 /* This is the first item in the stack. Check if that's cool */
614 if (is_first_item && item_info->valid_start_item)
621 * Build the intenal enic filter structure from the provided pattern. The
622 * pattern is validated as the items are copied.
625 * @param items_info[in]
626 * Info about this NICs item support, like valid previous items.
627 * @param enic_filter[out]
628 * NIC specfilc filters derived from the pattern.
632 enic_copy_filter(const struct rte_flow_item pattern[],
633 const struct enic_items *items_info,
634 struct filter_v2 *enic_filter,
635 struct rte_flow_error *error)
638 const struct rte_flow_item *item = pattern;
639 u8 inner_ofst = 0; /* If encapsulated, ofst into L5 */
640 enum rte_flow_item_type prev_item;
641 const struct enic_items *item_info;
643 enic_filter->type = FILTER_DPDK_1;
644 u8 is_first_item = 1;
650 for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
651 /* Get info about how to validate and copy the item. If NULL
652 * is returned the nic does not support the item.
654 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
657 item_info = &items_info[item->type];
659 /* check to see if item stacking is valid */
660 if (!item_stacking_valid(prev_item, item_info, is_first_item))
663 ret = item_info->copy_item(item, enic_filter, &inner_ofst);
665 goto item_not_supported;
666 prev_item = item->type;
672 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_ITEM,
673 NULL, "enic type error");
677 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
678 item, "stacking error");
682 * Build the intenal version 2 NIC action structure from the provided pattern.
683 * The pattern is validated as the items are copied.
686 * @param enic_action[out]
687 * NIC specfilc actions derived from the actions.
691 enic_copy_action_v2(const struct rte_flow_action actions[],
692 struct filter_action_v2 *enic_action)
696 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
697 switch (actions->type) {
698 case RTE_FLOW_ACTION_TYPE_QUEUE: {
699 const struct rte_flow_action_queue *queue =
700 (const struct rte_flow_action_queue *)
702 enic_action->rq_idx =
703 enic_rte_rq_idx_to_sop_idx(queue->index);
704 enic_action->flags |= FILTER_ACTION_RQ_STEERING_FLAG;
707 case RTE_FLOW_ACTION_TYPE_MARK: {
708 const struct rte_flow_action_mark *mark =
709 (const struct rte_flow_action_mark *)
712 /* ENIC_MAGIC_FILTER_ID is reserved and is the highest
713 * in the range of allows mark ids.
715 if (mark->id >= ENIC_MAGIC_FILTER_ID)
717 enic_action->filter_id = mark->id;
718 enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
721 case RTE_FLOW_ACTION_TYPE_FLAG: {
722 enic_action->filter_id = ENIC_MAGIC_FILTER_ID;
723 enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
726 case RTE_FLOW_ACTION_TYPE_VOID:
733 enic_action->type = FILTER_ACTION_V2;
737 /** Check if the action is supported */
739 enic_match_action(const struct rte_flow_action *action,
740 const enum rte_flow_action_type *supported_actions)
742 for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
743 supported_actions++) {
744 if (action->type == *supported_actions)
750 /** Get the NIC filter capabilties structure */
751 static const struct enic_filter_cap *
752 enic_get_filter_cap(struct enic *enic)
754 /* FIXME: only support advanced filters for now */
755 if (enic->flow_filter_mode != FILTER_DPDK_1)
756 return (const struct enic_filter_cap *)NULL;
758 if (enic->flow_filter_mode)
759 return &enic_filter_cap[enic->flow_filter_mode];
764 /** Get the actions for this NIC version. */
765 static const struct enic_action_cap *
766 enic_get_action_cap(struct enic *enic)
768 static const struct enic_action_cap *ea;
770 if (enic->filter_tags)
771 ea = &enic_action_cap[FILTER_ACTION_V2_ALL];
775 * Internal flow parse/validate function.
778 * This device pointer.
782 * @param enic_filter[out]
783 * Internal NIC filter structure pointer.
784 * @param enic_action[out]
785 * Internal NIC action structure pointer.
788 enic_flow_parse(struct rte_eth_dev *dev,
789 const struct rte_flow_attr *attrs,
790 const struct rte_flow_item pattern[],
791 const struct rte_flow_action actions[],
792 struct rte_flow_error *error,
793 struct filter_v2 *enic_filter,
794 struct filter_action_v2 *enic_action)
796 unsigned int ret = 0;
797 struct enic *enic = pmd_priv(dev);
798 const struct enic_filter_cap *enic_filter_cap;
799 const struct enic_action_cap *enic_action_cap;
800 const struct rte_flow_action *action;
804 memset(enic_filter, 0, sizeof(*enic_filter));
805 memset(enic_action, 0, sizeof(*enic_action));
808 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
809 NULL, "No pattern specified");
814 rte_flow_error_set(error, EINVAL,
815 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
816 NULL, "No action specified");
822 rte_flow_error_set(error, ENOTSUP,
823 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
825 "priority groups are not supported");
827 } else if (attrs->priority) {
828 rte_flow_error_set(error, ENOTSUP,
829 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
831 "priorities are not supported");
833 } else if (attrs->egress) {
834 rte_flow_error_set(error, ENOTSUP,
835 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
837 "egress is not supported");
839 } else if (!attrs->ingress) {
840 rte_flow_error_set(error, ENOTSUP,
841 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
843 "only ingress is supported");
848 rte_flow_error_set(error, EINVAL,
849 RTE_FLOW_ERROR_TYPE_ATTR,
850 NULL, "No attribute specified");
854 /* Verify Actions. */
855 enic_action_cap = enic_get_action_cap(enic);
856 for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
858 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
860 else if (!enic_match_action(action, enic_action_cap->actions))
863 if (action->type != RTE_FLOW_ACTION_TYPE_END) {
864 rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
865 action, "Invalid action.");
868 ret = enic_action_cap->copy_fn(actions, enic_action);
870 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
871 NULL, "Unsupported action.");
875 /* Verify Flow items. If copying the filter from flow format to enic
876 * format fails, the flow is not supported
878 enic_filter_cap = enic_get_filter_cap(enic);
879 if (enic_filter_cap == NULL) {
880 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
881 NULL, "Flow API not available");
884 ret = enic_copy_filter(pattern, enic_filter_cap->item_info,
890 * Push filter/action to the NIC.
893 * Device structure pointer.
894 * @param enic_filter[in]
895 * Internal NIC filter structure pointer.
896 * @param enic_action[in]
897 * Internal NIC action structure pointer.
900 static struct rte_flow *
901 enic_flow_add_filter(struct enic *enic, struct filter_v2 *enic_filter,
902 struct filter_action_v2 *enic_action,
903 struct rte_flow_error *error)
905 struct rte_flow *flow;
911 flow = rte_calloc(__func__, 1, sizeof(*flow), 0);
913 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
914 NULL, "cannot allocate flow memory");
918 /* entry[in] is the queue id, entry[out] is the filter Id for delete */
919 entry = enic_action->rq_idx;
920 ret = vnic_dev_classifier(enic->vdev, CLSF_ADD, &entry, enic_filter,
923 flow->enic_filter_id = entry;
924 flow->enic_filter = *enic_filter;
926 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
927 NULL, "vnic_dev_classifier error");
935 * Remove filter/action from the NIC.
938 * Device structure pointer.
939 * @param filter_id[in]
941 * @param enic_action[in]
942 * Internal NIC action structure pointer.
946 enic_flow_del_filter(struct enic *enic, u16 filter_id,
947 struct rte_flow_error *error)
953 ret = vnic_dev_classifier(enic->vdev, CLSF_DEL, &filter_id, NULL, NULL);
955 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
956 NULL, "vnic_dev_classifier failed");
961 * The following functions are callbacks for Generic flow API.
965 * Validate a flow supported by the NIC.
967 * @see rte_flow_validate()
971 enic_flow_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attrs,
972 const struct rte_flow_item pattern[],
973 const struct rte_flow_action actions[],
974 struct rte_flow_error *error)
976 struct filter_v2 enic_filter;
977 struct filter_action_v2 enic_action;
982 ret = enic_flow_parse(dev, attrs, pattern, actions, error,
983 &enic_filter, &enic_action);
988 * Create a flow supported by the NIC.
990 * @see rte_flow_create()
993 static struct rte_flow *
994 enic_flow_create(struct rte_eth_dev *dev,
995 const struct rte_flow_attr *attrs,
996 const struct rte_flow_item pattern[],
997 const struct rte_flow_action actions[],
998 struct rte_flow_error *error)
1001 struct filter_v2 enic_filter;
1002 struct filter_action_v2 enic_action;
1003 struct rte_flow *flow;
1004 struct enic *enic = pmd_priv(dev);
1008 ret = enic_flow_parse(dev, attrs, pattern, actions, error, &enic_filter,
1013 rte_spinlock_lock(&enic->flows_lock);
1014 flow = enic_flow_add_filter(enic, &enic_filter, &enic_action,
1017 LIST_INSERT_HEAD(&enic->flows, flow, next);
1018 rte_spinlock_unlock(&enic->flows_lock);
1024 * Destroy a flow supported by the NIC.
1026 * @see rte_flow_destroy()
1030 enic_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
1031 __rte_unused struct rte_flow_error *error)
1033 struct enic *enic = pmd_priv(dev);
1037 rte_spinlock_lock(&enic->flows_lock);
1038 enic_flow_del_filter(enic, flow->enic_filter_id, error);
1039 LIST_REMOVE(flow, next);
1040 rte_spinlock_unlock(&enic->flows_lock);
1045 * Flush all flows on the device.
1047 * @see rte_flow_flush()
1051 enic_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1053 struct rte_flow *flow;
1054 struct enic *enic = pmd_priv(dev);
1058 rte_spinlock_lock(&enic->flows_lock);
1060 while (!LIST_EMPTY(&enic->flows)) {
1061 flow = LIST_FIRST(&enic->flows);
1062 enic_flow_del_filter(enic, flow->enic_filter_id, error);
1063 LIST_REMOVE(flow, next);
1065 rte_spinlock_unlock(&enic->flows_lock);
1070 * Flow callback registration.
1074 const struct rte_flow_ops enic_flow_ops = {
1075 .validate = enic_flow_validate,
1076 .create = enic_flow_create,
1077 .destroy = enic_flow_destroy,
1078 .flush = enic_flow_flush,