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_ipv4_v1;
93 static enic_copy_item_fn enic_copy_item_udp_v1;
94 static enic_copy_item_fn enic_copy_item_tcp_v1;
95 static enic_copy_item_fn enic_copy_item_eth_v2;
96 static enic_copy_item_fn enic_copy_item_vlan_v2;
97 static enic_copy_item_fn enic_copy_item_ipv4_v2;
98 static enic_copy_item_fn enic_copy_item_ipv6_v2;
99 static enic_copy_item_fn enic_copy_item_udp_v2;
100 static enic_copy_item_fn enic_copy_item_tcp_v2;
101 static enic_copy_item_fn enic_copy_item_sctp_v2;
102 static enic_copy_item_fn enic_copy_item_sctp_v2;
103 static enic_copy_item_fn enic_copy_item_vxlan_v2;
104 static copy_action_fn enic_copy_action_v1;
105 static copy_action_fn enic_copy_action_v2;
108 * Legacy NICs or NICs with outdated firmware. Only 5-tuple perfect match
111 static const struct enic_items enic_items_v1[] = {
112 [RTE_FLOW_ITEM_TYPE_IPV4] = {
113 .copy_item = enic_copy_item_ipv4_v1,
114 .valid_start_item = 1,
115 .prev_items = (const enum rte_flow_item_type[]) {
116 RTE_FLOW_ITEM_TYPE_END,
119 [RTE_FLOW_ITEM_TYPE_UDP] = {
120 .copy_item = enic_copy_item_udp_v1,
121 .valid_start_item = 0,
122 .prev_items = (const enum rte_flow_item_type[]) {
123 RTE_FLOW_ITEM_TYPE_IPV4,
124 RTE_FLOW_ITEM_TYPE_END,
127 [RTE_FLOW_ITEM_TYPE_TCP] = {
128 .copy_item = enic_copy_item_tcp_v1,
129 .valid_start_item = 0,
130 .prev_items = (const enum rte_flow_item_type[]) {
131 RTE_FLOW_ITEM_TYPE_IPV4,
132 RTE_FLOW_ITEM_TYPE_END,
138 * NICs have Advanced Filters capability but they are disabled. This means
139 * that layer 3 must be specified.
141 static const struct enic_items enic_items_v2[] = {
142 [RTE_FLOW_ITEM_TYPE_ETH] = {
143 .copy_item = enic_copy_item_eth_v2,
144 .valid_start_item = 1,
145 .prev_items = (const enum rte_flow_item_type[]) {
146 RTE_FLOW_ITEM_TYPE_VXLAN,
147 RTE_FLOW_ITEM_TYPE_END,
150 [RTE_FLOW_ITEM_TYPE_VLAN] = {
151 .copy_item = enic_copy_item_vlan_v2,
152 .valid_start_item = 1,
153 .prev_items = (const enum rte_flow_item_type[]) {
154 RTE_FLOW_ITEM_TYPE_ETH,
155 RTE_FLOW_ITEM_TYPE_END,
158 [RTE_FLOW_ITEM_TYPE_IPV4] = {
159 .copy_item = enic_copy_item_ipv4_v2,
160 .valid_start_item = 1,
161 .prev_items = (const enum rte_flow_item_type[]) {
162 RTE_FLOW_ITEM_TYPE_ETH,
163 RTE_FLOW_ITEM_TYPE_VLAN,
164 RTE_FLOW_ITEM_TYPE_END,
167 [RTE_FLOW_ITEM_TYPE_IPV6] = {
168 .copy_item = enic_copy_item_ipv6_v2,
169 .valid_start_item = 1,
170 .prev_items = (const enum rte_flow_item_type[]) {
171 RTE_FLOW_ITEM_TYPE_ETH,
172 RTE_FLOW_ITEM_TYPE_VLAN,
173 RTE_FLOW_ITEM_TYPE_END,
176 [RTE_FLOW_ITEM_TYPE_UDP] = {
177 .copy_item = enic_copy_item_udp_v2,
178 .valid_start_item = 0,
179 .prev_items = (const enum rte_flow_item_type[]) {
180 RTE_FLOW_ITEM_TYPE_IPV4,
181 RTE_FLOW_ITEM_TYPE_IPV6,
182 RTE_FLOW_ITEM_TYPE_END,
185 [RTE_FLOW_ITEM_TYPE_TCP] = {
186 .copy_item = enic_copy_item_tcp_v2,
187 .valid_start_item = 0,
188 .prev_items = (const enum rte_flow_item_type[]) {
189 RTE_FLOW_ITEM_TYPE_IPV4,
190 RTE_FLOW_ITEM_TYPE_IPV6,
191 RTE_FLOW_ITEM_TYPE_END,
194 [RTE_FLOW_ITEM_TYPE_SCTP] = {
195 .copy_item = enic_copy_item_sctp_v2,
196 .valid_start_item = 0,
197 .prev_items = (const enum rte_flow_item_type[]) {
198 RTE_FLOW_ITEM_TYPE_IPV4,
199 RTE_FLOW_ITEM_TYPE_IPV6,
200 RTE_FLOW_ITEM_TYPE_END,
203 [RTE_FLOW_ITEM_TYPE_VXLAN] = {
204 .copy_item = enic_copy_item_vxlan_v2,
205 .valid_start_item = 0,
206 .prev_items = (const enum rte_flow_item_type[]) {
207 RTE_FLOW_ITEM_TYPE_UDP,
208 RTE_FLOW_ITEM_TYPE_END,
213 /** NICs with Advanced filters enabled */
214 static const struct enic_items enic_items_v3[] = {
215 [RTE_FLOW_ITEM_TYPE_ETH] = {
216 .copy_item = enic_copy_item_eth_v2,
217 .valid_start_item = 1,
218 .prev_items = (const enum rte_flow_item_type[]) {
219 RTE_FLOW_ITEM_TYPE_VXLAN,
220 RTE_FLOW_ITEM_TYPE_END,
223 [RTE_FLOW_ITEM_TYPE_VLAN] = {
224 .copy_item = enic_copy_item_vlan_v2,
225 .valid_start_item = 1,
226 .prev_items = (const enum rte_flow_item_type[]) {
227 RTE_FLOW_ITEM_TYPE_ETH,
228 RTE_FLOW_ITEM_TYPE_END,
231 [RTE_FLOW_ITEM_TYPE_IPV4] = {
232 .copy_item = enic_copy_item_ipv4_v2,
233 .valid_start_item = 1,
234 .prev_items = (const enum rte_flow_item_type[]) {
235 RTE_FLOW_ITEM_TYPE_ETH,
236 RTE_FLOW_ITEM_TYPE_VLAN,
237 RTE_FLOW_ITEM_TYPE_END,
240 [RTE_FLOW_ITEM_TYPE_IPV6] = {
241 .copy_item = enic_copy_item_ipv6_v2,
242 .valid_start_item = 1,
243 .prev_items = (const enum rte_flow_item_type[]) {
244 RTE_FLOW_ITEM_TYPE_ETH,
245 RTE_FLOW_ITEM_TYPE_VLAN,
246 RTE_FLOW_ITEM_TYPE_END,
249 [RTE_FLOW_ITEM_TYPE_UDP] = {
250 .copy_item = enic_copy_item_udp_v2,
251 .valid_start_item = 1,
252 .prev_items = (const enum rte_flow_item_type[]) {
253 RTE_FLOW_ITEM_TYPE_IPV4,
254 RTE_FLOW_ITEM_TYPE_IPV6,
255 RTE_FLOW_ITEM_TYPE_END,
258 [RTE_FLOW_ITEM_TYPE_TCP] = {
259 .copy_item = enic_copy_item_tcp_v2,
260 .valid_start_item = 1,
261 .prev_items = (const enum rte_flow_item_type[]) {
262 RTE_FLOW_ITEM_TYPE_IPV4,
263 RTE_FLOW_ITEM_TYPE_IPV6,
264 RTE_FLOW_ITEM_TYPE_END,
267 [RTE_FLOW_ITEM_TYPE_SCTP] = {
268 .copy_item = enic_copy_item_sctp_v2,
269 .valid_start_item = 1,
270 .prev_items = (const enum rte_flow_item_type[]) {
271 RTE_FLOW_ITEM_TYPE_IPV4,
272 RTE_FLOW_ITEM_TYPE_IPV6,
273 RTE_FLOW_ITEM_TYPE_END,
276 [RTE_FLOW_ITEM_TYPE_VXLAN] = {
277 .copy_item = enic_copy_item_vxlan_v2,
278 .valid_start_item = 1,
279 .prev_items = (const enum rte_flow_item_type[]) {
280 RTE_FLOW_ITEM_TYPE_UDP,
281 RTE_FLOW_ITEM_TYPE_END,
286 /** Filtering capabilities indexed this NICs supported filter type. */
287 static const struct enic_filter_cap enic_filter_cap[] = {
288 [FILTER_IPV4_5TUPLE] = {
289 .item_info = enic_items_v1,
291 [FILTER_USNIC_IP] = {
292 .item_info = enic_items_v2,
295 .item_info = enic_items_v3,
299 /** Supported actions for older NICs */
300 static const enum rte_flow_action_type enic_supported_actions_v1[] = {
301 RTE_FLOW_ACTION_TYPE_QUEUE,
302 RTE_FLOW_ACTION_TYPE_END,
305 /** Supported actions for newer NICs */
306 static const enum rte_flow_action_type enic_supported_actions_v2[] = {
307 RTE_FLOW_ACTION_TYPE_QUEUE,
308 RTE_FLOW_ACTION_TYPE_MARK,
309 RTE_FLOW_ACTION_TYPE_FLAG,
310 RTE_FLOW_ACTION_TYPE_END,
313 /** Action capabilities indexed by NIC version information */
314 static const struct enic_action_cap enic_action_cap[] = {
315 [FILTER_ACTION_RQ_STEERING_FLAG] = {
316 .actions = enic_supported_actions_v1,
317 .copy_fn = enic_copy_action_v1,
319 [FILTER_ACTION_V2_ALL] = {
320 .actions = enic_supported_actions_v2,
321 .copy_fn = enic_copy_action_v2,
326 mask_exact_match(const u8 *supported, const u8 *supplied,
330 for (i = 0; i < size; i++) {
331 if (supported[i] != supplied[i])
338 * Copy IPv4 item into version 1 NIC filter.
341 * Item specification.
342 * @param enic_filter[out]
343 * Partially filled in NIC filter structure.
344 * @param inner_ofst[in]
345 * Should always be 0 for version 1.
348 enic_copy_item_ipv4_v1(const struct rte_flow_item *item,
349 struct filter_v2 *enic_filter, u8 *inner_ofst)
351 const struct rte_flow_item_ipv4 *spec = item->spec;
352 const struct rte_flow_item_ipv4 *mask = item->mask;
353 struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
354 struct ipv4_hdr supported_mask = {
355 .src_addr = 0xffffffff,
356 .dst_addr = 0xffffffff,
365 mask = &rte_flow_item_ipv4_mask;
367 /* This is an exact match filter, both fields must be set */
368 if (!spec || !spec->hdr.src_addr || !spec->hdr.dst_addr) {
369 FLOW_LOG(ERR, "IPv4 exact match src/dst addr");
373 /* check that the suppied mask exactly matches capabilty */
374 if (!mask_exact_match((const u8 *)&supported_mask,
375 (const u8 *)item->mask, sizeof(*mask))) {
376 FLOW_LOG(ERR, "IPv4 exact match mask");
380 enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
381 enic_5tup->src_addr = spec->hdr.src_addr;
382 enic_5tup->dst_addr = spec->hdr.dst_addr;
388 * Copy UDP item into version 1 NIC filter.
391 * Item specification.
392 * @param enic_filter[out]
393 * Partially filled in NIC filter structure.
394 * @param inner_ofst[in]
395 * Should always be 0 for version 1.
398 enic_copy_item_udp_v1(const struct rte_flow_item *item,
399 struct filter_v2 *enic_filter, u8 *inner_ofst)
401 const struct rte_flow_item_udp *spec = item->spec;
402 const struct rte_flow_item_udp *mask = item->mask;
403 struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
404 struct udp_hdr supported_mask = {
415 mask = &rte_flow_item_udp_mask;
417 /* This is an exact match filter, both ports must be set */
418 if (!spec || !spec->hdr.src_port || !spec->hdr.dst_port) {
419 FLOW_LOG(ERR, "UDP exact match src/dst addr");
423 /* check that the suppied mask exactly matches capabilty */
424 if (!mask_exact_match((const u8 *)&supported_mask,
425 (const u8 *)item->mask, sizeof(*mask))) {
426 FLOW_LOG(ERR, "UDP exact match mask");
430 enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
431 enic_5tup->src_port = spec->hdr.src_port;
432 enic_5tup->dst_port = spec->hdr.dst_port;
433 enic_5tup->protocol = PROTO_UDP;
439 * Copy TCP item into version 1 NIC filter.
442 * Item specification.
443 * @param enic_filter[out]
444 * Partially filled in NIC filter structure.
445 * @param inner_ofst[in]
446 * Should always be 0 for version 1.
449 enic_copy_item_tcp_v1(const struct rte_flow_item *item,
450 struct filter_v2 *enic_filter, u8 *inner_ofst)
452 const struct rte_flow_item_tcp *spec = item->spec;
453 const struct rte_flow_item_tcp *mask = item->mask;
454 struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
455 struct tcp_hdr supported_mask = {
466 mask = &rte_flow_item_tcp_mask;
468 /* This is an exact match filter, both ports must be set */
469 if (!spec || !spec->hdr.src_port || !spec->hdr.dst_port) {
470 FLOW_LOG(ERR, "TCPIPv4 exact match src/dst addr");
474 /* check that the suppied mask exactly matches capabilty */
475 if (!mask_exact_match((const u8 *)&supported_mask,
476 (const u8 *)item->mask, sizeof(*mask))) {
477 FLOW_LOG(ERR, "TCP exact match mask");
481 enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
482 enic_5tup->src_port = spec->hdr.src_port;
483 enic_5tup->dst_port = spec->hdr.dst_port;
484 enic_5tup->protocol = PROTO_TCP;
490 * Copy ETH item into version 2 NIC filter.
493 * Item specification.
494 * @param enic_filter[out]
495 * Partially filled in NIC filter structure.
496 * @param inner_ofst[in]
497 * If zero, this is an outer header. If non-zero, this is the offset into L5
498 * where the header begins.
501 enic_copy_item_eth_v2(const struct rte_flow_item *item,
502 struct filter_v2 *enic_filter, u8 *inner_ofst)
504 struct ether_hdr enic_spec;
505 struct ether_hdr enic_mask;
506 const struct rte_flow_item_eth *spec = item->spec;
507 const struct rte_flow_item_eth *mask = item->mask;
508 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
512 /* Match all if no spec */
517 mask = &rte_flow_item_eth_mask;
519 memcpy(enic_spec.d_addr.addr_bytes, spec->dst.addr_bytes,
521 memcpy(enic_spec.s_addr.addr_bytes, spec->src.addr_bytes,
524 memcpy(enic_mask.d_addr.addr_bytes, mask->dst.addr_bytes,
526 memcpy(enic_mask.s_addr.addr_bytes, mask->src.addr_bytes,
528 enic_spec.ether_type = spec->type;
529 enic_mask.ether_type = mask->type;
531 if (*inner_ofst == 0) {
533 memcpy(gp->layer[FILTER_GENERIC_1_L2].mask, &enic_mask,
534 sizeof(struct ether_hdr));
535 memcpy(gp->layer[FILTER_GENERIC_1_L2].val, &enic_spec,
536 sizeof(struct ether_hdr));
539 if ((*inner_ofst + sizeof(struct ether_hdr)) >
540 FILTER_GENERIC_1_KEY_LEN)
542 /* Offset into L5 where inner Ethernet header goes */
543 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
544 &enic_mask, sizeof(struct ether_hdr));
545 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
546 &enic_spec, sizeof(struct ether_hdr));
547 *inner_ofst += sizeof(struct ether_hdr);
553 * Copy VLAN item into version 2 NIC filter.
556 * Item specification.
557 * @param enic_filter[out]
558 * Partially filled in NIC filter structure.
559 * @param inner_ofst[in]
560 * If zero, this is an outer header. If non-zero, this is the offset into L5
561 * where the header begins.
564 enic_copy_item_vlan_v2(const struct rte_flow_item *item,
565 struct filter_v2 *enic_filter, u8 *inner_ofst)
567 const struct rte_flow_item_vlan *spec = item->spec;
568 const struct rte_flow_item_vlan *mask = item->mask;
569 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
573 /* Match all if no spec */
577 /* Don't support filtering in tpid */
582 mask = &rte_flow_item_vlan_mask;
583 RTE_ASSERT(mask->tpid == 0);
586 if (*inner_ofst == 0) {
587 /* Outer header. Use the vlan mask/val fields */
588 gp->mask_vlan = mask->tci;
589 gp->val_vlan = spec->tci;
591 /* Inner header. Mask/Val start at *inner_ofst into L5 */
592 if ((*inner_ofst + sizeof(struct vlan_hdr)) >
593 FILTER_GENERIC_1_KEY_LEN)
595 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
596 mask, sizeof(struct vlan_hdr));
597 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
598 spec, sizeof(struct vlan_hdr));
599 *inner_ofst += sizeof(struct vlan_hdr);
605 * Copy IPv4 item into version 2 NIC filter.
608 * Item specification.
609 * @param enic_filter[out]
610 * Partially filled in NIC filter structure.
611 * @param inner_ofst[in]
612 * Must be 0. Don't support inner IPv4 filtering.
615 enic_copy_item_ipv4_v2(const struct rte_flow_item *item,
616 struct filter_v2 *enic_filter, u8 *inner_ofst)
618 const struct rte_flow_item_ipv4 *spec = item->spec;
619 const struct rte_flow_item_ipv4 *mask = item->mask;
620 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
624 if (*inner_ofst == 0) {
626 gp->mask_flags |= FILTER_GENERIC_1_IPV4;
627 gp->val_flags |= FILTER_GENERIC_1_IPV4;
629 /* Match all if no spec */
634 mask = &rte_flow_item_ipv4_mask;
636 memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
637 sizeof(struct ipv4_hdr));
638 memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
639 sizeof(struct ipv4_hdr));
641 /* Inner IPv4 header. Mask/Val start at *inner_ofst into L5 */
642 if ((*inner_ofst + sizeof(struct ipv4_hdr)) >
643 FILTER_GENERIC_1_KEY_LEN)
645 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
646 mask, sizeof(struct ipv4_hdr));
647 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
648 spec, sizeof(struct ipv4_hdr));
649 *inner_ofst += sizeof(struct ipv4_hdr);
655 * Copy IPv6 item into version 2 NIC filter.
658 * Item specification.
659 * @param enic_filter[out]
660 * Partially filled in NIC filter structure.
661 * @param inner_ofst[in]
662 * Must be 0. Don't support inner IPv6 filtering.
665 enic_copy_item_ipv6_v2(const struct rte_flow_item *item,
666 struct filter_v2 *enic_filter, u8 *inner_ofst)
668 const struct rte_flow_item_ipv6 *spec = item->spec;
669 const struct rte_flow_item_ipv6 *mask = item->mask;
670 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
675 gp->mask_flags |= FILTER_GENERIC_1_IPV6;
676 gp->val_flags |= FILTER_GENERIC_1_IPV6;
678 /* Match all if no spec */
683 mask = &rte_flow_item_ipv6_mask;
685 if (*inner_ofst == 0) {
686 memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
687 sizeof(struct ipv6_hdr));
688 memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
689 sizeof(struct ipv6_hdr));
691 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
692 if ((*inner_ofst + sizeof(struct ipv6_hdr)) >
693 FILTER_GENERIC_1_KEY_LEN)
695 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
696 mask, sizeof(struct ipv6_hdr));
697 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
698 spec, sizeof(struct ipv6_hdr));
699 *inner_ofst += sizeof(struct ipv6_hdr);
705 * Copy UDP item into version 2 NIC filter.
708 * Item specification.
709 * @param enic_filter[out]
710 * Partially filled in NIC filter structure.
711 * @param inner_ofst[in]
712 * Must be 0. Don't support inner UDP filtering.
715 enic_copy_item_udp_v2(const struct rte_flow_item *item,
716 struct filter_v2 *enic_filter, u8 *inner_ofst)
718 const struct rte_flow_item_udp *spec = item->spec;
719 const struct rte_flow_item_udp *mask = item->mask;
720 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
725 gp->mask_flags |= FILTER_GENERIC_1_UDP;
726 gp->val_flags |= FILTER_GENERIC_1_UDP;
728 /* Match all if no spec */
733 mask = &rte_flow_item_udp_mask;
735 if (*inner_ofst == 0) {
736 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
737 sizeof(struct udp_hdr));
738 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
739 sizeof(struct udp_hdr));
741 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
742 if ((*inner_ofst + sizeof(struct udp_hdr)) >
743 FILTER_GENERIC_1_KEY_LEN)
745 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
746 mask, sizeof(struct udp_hdr));
747 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
748 spec, sizeof(struct udp_hdr));
749 *inner_ofst += sizeof(struct udp_hdr);
755 * Copy TCP item into version 2 NIC filter.
758 * Item specification.
759 * @param enic_filter[out]
760 * Partially filled in NIC filter structure.
761 * @param inner_ofst[in]
762 * Must be 0. Don't support inner TCP filtering.
765 enic_copy_item_tcp_v2(const struct rte_flow_item *item,
766 struct filter_v2 *enic_filter, u8 *inner_ofst)
768 const struct rte_flow_item_tcp *spec = item->spec;
769 const struct rte_flow_item_tcp *mask = item->mask;
770 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
775 gp->mask_flags |= FILTER_GENERIC_1_TCP;
776 gp->val_flags |= FILTER_GENERIC_1_TCP;
778 /* Match all if no spec */
785 if (*inner_ofst == 0) {
786 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
787 sizeof(struct tcp_hdr));
788 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
789 sizeof(struct tcp_hdr));
791 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
792 if ((*inner_ofst + sizeof(struct tcp_hdr)) >
793 FILTER_GENERIC_1_KEY_LEN)
795 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
796 mask, sizeof(struct tcp_hdr));
797 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
798 spec, sizeof(struct tcp_hdr));
799 *inner_ofst += sizeof(struct tcp_hdr);
805 * Copy SCTP item into version 2 NIC filter.
808 * Item specification.
809 * @param enic_filter[out]
810 * Partially filled in NIC filter structure.
811 * @param inner_ofst[in]
812 * Must be 0. Don't support inner SCTP filtering.
815 enic_copy_item_sctp_v2(const struct rte_flow_item *item,
816 struct filter_v2 *enic_filter, u8 *inner_ofst)
818 const struct rte_flow_item_sctp *spec = item->spec;
819 const struct rte_flow_item_sctp *mask = item->mask;
820 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
827 /* Match all if no spec */
832 mask = &rte_flow_item_sctp_mask;
834 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
835 sizeof(struct sctp_hdr));
836 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
837 sizeof(struct sctp_hdr));
842 * Copy UDP item into version 2 NIC filter.
845 * Item specification.
846 * @param enic_filter[out]
847 * Partially filled in NIC filter structure.
848 * @param inner_ofst[in]
849 * Must be 0. VxLAN headers always start at the beginning of L5.
852 enic_copy_item_vxlan_v2(const struct rte_flow_item *item,
853 struct filter_v2 *enic_filter, u8 *inner_ofst)
855 const struct rte_flow_item_vxlan *spec = item->spec;
856 const struct rte_flow_item_vxlan *mask = item->mask;
857 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
864 /* Match all if no spec */
869 mask = &rte_flow_item_vxlan_mask;
871 memcpy(gp->layer[FILTER_GENERIC_1_L5].mask, mask,
872 sizeof(struct vxlan_hdr));
873 memcpy(gp->layer[FILTER_GENERIC_1_L5].val, spec,
874 sizeof(struct vxlan_hdr));
876 *inner_ofst = sizeof(struct vxlan_hdr);
881 * Return 1 if current item is valid on top of the previous one.
883 * @param prev_item[in]
884 * The item before this one in the pattern or RTE_FLOW_ITEM_TYPE_END if this
886 * @param item_info[in]
887 * Info about this item, like valid previous items.
888 * @param is_first[in]
889 * True if this the first item in the pattern.
892 item_stacking_valid(enum rte_flow_item_type prev_item,
893 const struct enic_items *item_info, u8 is_first_item)
895 enum rte_flow_item_type const *allowed_items = item_info->prev_items;
899 for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
900 if (prev_item == *allowed_items)
904 /* This is the first item in the stack. Check if that's cool */
905 if (is_first_item && item_info->valid_start_item)
912 * Build the intenal enic filter structure from the provided pattern. The
913 * pattern is validated as the items are copied.
916 * @param items_info[in]
917 * Info about this NICs item support, like valid previous items.
918 * @param enic_filter[out]
919 * NIC specfilc filters derived from the pattern.
923 enic_copy_filter(const struct rte_flow_item pattern[],
924 const struct enic_items *items_info,
925 struct filter_v2 *enic_filter,
926 struct rte_flow_error *error)
929 const struct rte_flow_item *item = pattern;
930 u8 inner_ofst = 0; /* If encapsulated, ofst into L5 */
931 enum rte_flow_item_type prev_item;
932 const struct enic_items *item_info;
934 u8 is_first_item = 1;
940 for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
941 /* Get info about how to validate and copy the item. If NULL
942 * is returned the nic does not support the item.
944 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
947 item_info = &items_info[item->type];
949 /* check to see if item stacking is valid */
950 if (!item_stacking_valid(prev_item, item_info, is_first_item))
953 ret = item_info->copy_item(item, enic_filter, &inner_ofst);
955 goto item_not_supported;
956 prev_item = item->type;
962 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_ITEM,
963 NULL, "enic type error");
967 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
968 item, "stacking error");
973 * Build the intenal version 1 NIC action structure from the provided pattern.
974 * The pattern is validated as the items are copied.
977 * @param enic_action[out]
978 * NIC specfilc actions derived from the actions.
982 enic_copy_action_v1(const struct rte_flow_action actions[],
983 struct filter_action_v2 *enic_action)
987 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
988 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID)
991 switch (actions->type) {
992 case RTE_FLOW_ACTION_TYPE_QUEUE: {
993 const struct rte_flow_action_queue *queue =
994 (const struct rte_flow_action_queue *)
996 enic_action->rq_idx =
997 enic_rte_rq_idx_to_sop_idx(queue->index);
1005 enic_action->type = FILTER_ACTION_RQ_STEERING;
1010 * Build the intenal version 2 NIC action structure from the provided pattern.
1011 * The pattern is validated as the items are copied.
1013 * @param actions[in]
1014 * @param enic_action[out]
1015 * NIC specfilc actions derived from the actions.
1019 enic_copy_action_v2(const struct rte_flow_action actions[],
1020 struct filter_action_v2 *enic_action)
1024 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1025 switch (actions->type) {
1026 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1027 const struct rte_flow_action_queue *queue =
1028 (const struct rte_flow_action_queue *)
1030 enic_action->rq_idx =
1031 enic_rte_rq_idx_to_sop_idx(queue->index);
1032 enic_action->flags |= FILTER_ACTION_RQ_STEERING_FLAG;
1035 case RTE_FLOW_ACTION_TYPE_MARK: {
1036 const struct rte_flow_action_mark *mark =
1037 (const struct rte_flow_action_mark *)
1040 /* ENIC_MAGIC_FILTER_ID is reserved and is the highest
1041 * in the range of allows mark ids.
1043 if (mark->id >= ENIC_MAGIC_FILTER_ID)
1045 enic_action->filter_id = mark->id;
1046 enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1049 case RTE_FLOW_ACTION_TYPE_FLAG: {
1050 enic_action->filter_id = ENIC_MAGIC_FILTER_ID;
1051 enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1054 case RTE_FLOW_ACTION_TYPE_VOID:
1061 enic_action->type = FILTER_ACTION_V2;
1065 /** Check if the action is supported */
1067 enic_match_action(const struct rte_flow_action *action,
1068 const enum rte_flow_action_type *supported_actions)
1070 for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
1071 supported_actions++) {
1072 if (action->type == *supported_actions)
1078 /** Get the NIC filter capabilties structure */
1079 static const struct enic_filter_cap *
1080 enic_get_filter_cap(struct enic *enic)
1082 if (enic->flow_filter_mode)
1083 return &enic_filter_cap[enic->flow_filter_mode];
1088 /** Get the actions for this NIC version. */
1089 static const struct enic_action_cap *
1090 enic_get_action_cap(struct enic *enic)
1092 static const struct enic_action_cap *ea;
1094 if (enic->filter_tags)
1095 ea = &enic_action_cap[FILTER_ACTION_V2_ALL];
1097 ea = &enic_action_cap[FILTER_ACTION_RQ_STEERING_FLAG];
1101 * Internal flow parse/validate function.
1104 * This device pointer.
1105 * @param pattern[in]
1106 * @param actions[in]
1108 * @param enic_filter[out]
1109 * Internal NIC filter structure pointer.
1110 * @param enic_action[out]
1111 * Internal NIC action structure pointer.
1114 enic_flow_parse(struct rte_eth_dev *dev,
1115 const struct rte_flow_attr *attrs,
1116 const struct rte_flow_item pattern[],
1117 const struct rte_flow_action actions[],
1118 struct rte_flow_error *error,
1119 struct filter_v2 *enic_filter,
1120 struct filter_action_v2 *enic_action)
1122 unsigned int ret = 0;
1123 struct enic *enic = pmd_priv(dev);
1124 const struct enic_filter_cap *enic_filter_cap;
1125 const struct enic_action_cap *enic_action_cap;
1126 const struct rte_flow_action *action;
1130 memset(enic_filter, 0, sizeof(*enic_filter));
1131 memset(enic_action, 0, sizeof(*enic_action));
1134 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1135 NULL, "No pattern specified");
1140 rte_flow_error_set(error, EINVAL,
1141 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1142 NULL, "No action specified");
1148 rte_flow_error_set(error, ENOTSUP,
1149 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1151 "priority groups are not supported");
1153 } else if (attrs->priority) {
1154 rte_flow_error_set(error, ENOTSUP,
1155 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1157 "priorities are not supported");
1159 } else if (attrs->egress) {
1160 rte_flow_error_set(error, ENOTSUP,
1161 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
1163 "egress is not supported");
1165 } else if (!attrs->ingress) {
1166 rte_flow_error_set(error, ENOTSUP,
1167 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1169 "only ingress is supported");
1174 rte_flow_error_set(error, EINVAL,
1175 RTE_FLOW_ERROR_TYPE_ATTR,
1176 NULL, "No attribute specified");
1180 /* Verify Actions. */
1181 enic_action_cap = enic_get_action_cap(enic);
1182 for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
1184 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1186 else if (!enic_match_action(action, enic_action_cap->actions))
1189 if (action->type != RTE_FLOW_ACTION_TYPE_END) {
1190 rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
1191 action, "Invalid action.");
1194 ret = enic_action_cap->copy_fn(actions, enic_action);
1196 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1197 NULL, "Unsupported action.");
1201 /* Verify Flow items. If copying the filter from flow format to enic
1202 * format fails, the flow is not supported
1204 enic_filter_cap = enic_get_filter_cap(enic);
1205 if (enic_filter_cap == NULL) {
1206 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1207 NULL, "Flow API not available");
1210 enic_filter->type = enic->flow_filter_mode;
1211 ret = enic_copy_filter(pattern, enic_filter_cap->item_info,
1212 enic_filter, error);
1217 * Push filter/action to the NIC.
1220 * Device structure pointer.
1221 * @param enic_filter[in]
1222 * Internal NIC filter structure pointer.
1223 * @param enic_action[in]
1224 * Internal NIC action structure pointer.
1227 static struct rte_flow *
1228 enic_flow_add_filter(struct enic *enic, struct filter_v2 *enic_filter,
1229 struct filter_action_v2 *enic_action,
1230 struct rte_flow_error *error)
1232 struct rte_flow *flow;
1238 flow = rte_calloc(__func__, 1, sizeof(*flow), 0);
1240 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1241 NULL, "cannot allocate flow memory");
1245 /* entry[in] is the queue id, entry[out] is the filter Id for delete */
1246 entry = enic_action->rq_idx;
1247 ret = vnic_dev_classifier(enic->vdev, CLSF_ADD, &entry, enic_filter,
1250 flow->enic_filter_id = entry;
1251 flow->enic_filter = *enic_filter;
1253 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
1254 NULL, "vnic_dev_classifier error");
1262 * Remove filter/action from the NIC.
1265 * Device structure pointer.
1266 * @param filter_id[in]
1268 * @param enic_action[in]
1269 * Internal NIC action structure pointer.
1273 enic_flow_del_filter(struct enic *enic, u16 filter_id,
1274 struct rte_flow_error *error)
1280 ret = vnic_dev_classifier(enic->vdev, CLSF_DEL, &filter_id, NULL, NULL);
1282 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
1283 NULL, "vnic_dev_classifier failed");
1288 * The following functions are callbacks for Generic flow API.
1292 * Validate a flow supported by the NIC.
1294 * @see rte_flow_validate()
1298 enic_flow_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attrs,
1299 const struct rte_flow_item pattern[],
1300 const struct rte_flow_action actions[],
1301 struct rte_flow_error *error)
1303 struct filter_v2 enic_filter;
1304 struct filter_action_v2 enic_action;
1309 ret = enic_flow_parse(dev, attrs, pattern, actions, error,
1310 &enic_filter, &enic_action);
1315 * Create a flow supported by the NIC.
1317 * @see rte_flow_create()
1320 static struct rte_flow *
1321 enic_flow_create(struct rte_eth_dev *dev,
1322 const struct rte_flow_attr *attrs,
1323 const struct rte_flow_item pattern[],
1324 const struct rte_flow_action actions[],
1325 struct rte_flow_error *error)
1328 struct filter_v2 enic_filter;
1329 struct filter_action_v2 enic_action;
1330 struct rte_flow *flow;
1331 struct enic *enic = pmd_priv(dev);
1335 ret = enic_flow_parse(dev, attrs, pattern, actions, error, &enic_filter,
1340 rte_spinlock_lock(&enic->flows_lock);
1341 flow = enic_flow_add_filter(enic, &enic_filter, &enic_action,
1344 LIST_INSERT_HEAD(&enic->flows, flow, next);
1345 rte_spinlock_unlock(&enic->flows_lock);
1351 * Destroy a flow supported by the NIC.
1353 * @see rte_flow_destroy()
1357 enic_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
1358 __rte_unused struct rte_flow_error *error)
1360 struct enic *enic = pmd_priv(dev);
1364 rte_spinlock_lock(&enic->flows_lock);
1365 enic_flow_del_filter(enic, flow->enic_filter_id, error);
1366 LIST_REMOVE(flow, next);
1367 rte_spinlock_unlock(&enic->flows_lock);
1372 * Flush all flows on the device.
1374 * @see rte_flow_flush()
1378 enic_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1380 struct rte_flow *flow;
1381 struct enic *enic = pmd_priv(dev);
1385 rte_spinlock_lock(&enic->flows_lock);
1387 while (!LIST_EMPTY(&enic->flows)) {
1388 flow = LIST_FIRST(&enic->flows);
1389 enic_flow_del_filter(enic, flow->enic_filter_id, error);
1390 LIST_REMOVE(flow, next);
1392 rte_spinlock_unlock(&enic->flows_lock);
1397 * Flow callback registration.
1401 const struct rte_flow_ops enic_flow_ops = {
1402 .validate = enic_flow_validate,
1403 .create = enic_flow_create,
1404 .destroy = enic_flow_destroy,
1405 .flush = enic_flow_flush,