1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2008-2017 Cisco Systems, Inc. All rights reserved.
7 #include <rte_ethdev.h>
8 #include <rte_flow_driver.h>
13 #include "enic_compat.h"
18 #define FLOW_TRACE() \
19 rte_log(RTE_LOG_DEBUG, enicpmd_logtype_flow, \
21 #define FLOW_LOG(level, fmt, args...) \
22 rte_log(RTE_LOG_ ## level, enicpmd_logtype_flow, \
25 /** Info about how to copy items into enic filters. */
27 /** Function for copying and validating an item. */
28 int (*copy_item)(const struct rte_flow_item *item,
29 struct filter_v2 *enic_filter, u8 *inner_ofst);
30 /** List of valid previous items. */
31 const enum rte_flow_item_type * const prev_items;
32 /** True if it's OK for this item to be the first item. For some NIC
33 * versions, it's invalid to start the stack above layer 3.
35 const u8 valid_start_item;
38 /** Filtering capabilities for various NIC and firmware versions. */
39 struct enic_filter_cap {
40 /** list of valid items and their handlers and attributes. */
41 const struct enic_items *item_info;
44 /* functions for copying flow actions into enic actions */
45 typedef int (copy_action_fn)(const struct rte_flow_action actions[],
46 struct filter_action_v2 *enic_action);
48 /* functions for copying items into enic filters */
49 typedef int(enic_copy_item_fn)(const struct rte_flow_item *item,
50 struct filter_v2 *enic_filter, u8 *inner_ofst);
52 /** Action capabilities for various NICs. */
53 struct enic_action_cap {
54 /** list of valid actions */
55 const enum rte_flow_action_type *actions;
56 /** copy function for a particular NIC */
57 int (*copy_fn)(const struct rte_flow_action actions[],
58 struct filter_action_v2 *enic_action);
61 /* Forward declarations */
62 static enic_copy_item_fn enic_copy_item_ipv4_v1;
63 static enic_copy_item_fn enic_copy_item_udp_v1;
64 static enic_copy_item_fn enic_copy_item_tcp_v1;
65 static enic_copy_item_fn enic_copy_item_eth_v2;
66 static enic_copy_item_fn enic_copy_item_vlan_v2;
67 static enic_copy_item_fn enic_copy_item_ipv4_v2;
68 static enic_copy_item_fn enic_copy_item_ipv6_v2;
69 static enic_copy_item_fn enic_copy_item_udp_v2;
70 static enic_copy_item_fn enic_copy_item_tcp_v2;
71 static enic_copy_item_fn enic_copy_item_sctp_v2;
72 static enic_copy_item_fn enic_copy_item_sctp_v2;
73 static enic_copy_item_fn enic_copy_item_vxlan_v2;
74 static copy_action_fn enic_copy_action_v1;
75 static copy_action_fn enic_copy_action_v2;
78 * Legacy NICs or NICs with outdated firmware. Only 5-tuple perfect match
81 static const struct enic_items enic_items_v1[] = {
82 [RTE_FLOW_ITEM_TYPE_IPV4] = {
83 .copy_item = enic_copy_item_ipv4_v1,
84 .valid_start_item = 1,
85 .prev_items = (const enum rte_flow_item_type[]) {
86 RTE_FLOW_ITEM_TYPE_END,
89 [RTE_FLOW_ITEM_TYPE_UDP] = {
90 .copy_item = enic_copy_item_udp_v1,
91 .valid_start_item = 0,
92 .prev_items = (const enum rte_flow_item_type[]) {
93 RTE_FLOW_ITEM_TYPE_IPV4,
94 RTE_FLOW_ITEM_TYPE_END,
97 [RTE_FLOW_ITEM_TYPE_TCP] = {
98 .copy_item = enic_copy_item_tcp_v1,
99 .valid_start_item = 0,
100 .prev_items = (const enum rte_flow_item_type[]) {
101 RTE_FLOW_ITEM_TYPE_IPV4,
102 RTE_FLOW_ITEM_TYPE_END,
108 * NICs have Advanced Filters capability but they are disabled. This means
109 * that layer 3 must be specified.
111 static const struct enic_items enic_items_v2[] = {
112 [RTE_FLOW_ITEM_TYPE_ETH] = {
113 .copy_item = enic_copy_item_eth_v2,
114 .valid_start_item = 1,
115 .prev_items = (const enum rte_flow_item_type[]) {
116 RTE_FLOW_ITEM_TYPE_VXLAN,
117 RTE_FLOW_ITEM_TYPE_END,
120 [RTE_FLOW_ITEM_TYPE_VLAN] = {
121 .copy_item = enic_copy_item_vlan_v2,
122 .valid_start_item = 1,
123 .prev_items = (const enum rte_flow_item_type[]) {
124 RTE_FLOW_ITEM_TYPE_ETH,
125 RTE_FLOW_ITEM_TYPE_END,
128 [RTE_FLOW_ITEM_TYPE_IPV4] = {
129 .copy_item = enic_copy_item_ipv4_v2,
130 .valid_start_item = 1,
131 .prev_items = (const enum rte_flow_item_type[]) {
132 RTE_FLOW_ITEM_TYPE_ETH,
133 RTE_FLOW_ITEM_TYPE_VLAN,
134 RTE_FLOW_ITEM_TYPE_END,
137 [RTE_FLOW_ITEM_TYPE_IPV6] = {
138 .copy_item = enic_copy_item_ipv6_v2,
139 .valid_start_item = 1,
140 .prev_items = (const enum rte_flow_item_type[]) {
141 RTE_FLOW_ITEM_TYPE_ETH,
142 RTE_FLOW_ITEM_TYPE_VLAN,
143 RTE_FLOW_ITEM_TYPE_END,
146 [RTE_FLOW_ITEM_TYPE_UDP] = {
147 .copy_item = enic_copy_item_udp_v2,
148 .valid_start_item = 0,
149 .prev_items = (const enum rte_flow_item_type[]) {
150 RTE_FLOW_ITEM_TYPE_IPV4,
151 RTE_FLOW_ITEM_TYPE_IPV6,
152 RTE_FLOW_ITEM_TYPE_END,
155 [RTE_FLOW_ITEM_TYPE_TCP] = {
156 .copy_item = enic_copy_item_tcp_v2,
157 .valid_start_item = 0,
158 .prev_items = (const enum rte_flow_item_type[]) {
159 RTE_FLOW_ITEM_TYPE_IPV4,
160 RTE_FLOW_ITEM_TYPE_IPV6,
161 RTE_FLOW_ITEM_TYPE_END,
164 [RTE_FLOW_ITEM_TYPE_SCTP] = {
165 .copy_item = enic_copy_item_sctp_v2,
166 .valid_start_item = 0,
167 .prev_items = (const enum rte_flow_item_type[]) {
168 RTE_FLOW_ITEM_TYPE_IPV4,
169 RTE_FLOW_ITEM_TYPE_IPV6,
170 RTE_FLOW_ITEM_TYPE_END,
173 [RTE_FLOW_ITEM_TYPE_VXLAN] = {
174 .copy_item = enic_copy_item_vxlan_v2,
175 .valid_start_item = 0,
176 .prev_items = (const enum rte_flow_item_type[]) {
177 RTE_FLOW_ITEM_TYPE_UDP,
178 RTE_FLOW_ITEM_TYPE_END,
183 /** NICs with Advanced filters enabled */
184 static const struct enic_items enic_items_v3[] = {
185 [RTE_FLOW_ITEM_TYPE_ETH] = {
186 .copy_item = enic_copy_item_eth_v2,
187 .valid_start_item = 1,
188 .prev_items = (const enum rte_flow_item_type[]) {
189 RTE_FLOW_ITEM_TYPE_VXLAN,
190 RTE_FLOW_ITEM_TYPE_END,
193 [RTE_FLOW_ITEM_TYPE_VLAN] = {
194 .copy_item = enic_copy_item_vlan_v2,
195 .valid_start_item = 1,
196 .prev_items = (const enum rte_flow_item_type[]) {
197 RTE_FLOW_ITEM_TYPE_ETH,
198 RTE_FLOW_ITEM_TYPE_END,
201 [RTE_FLOW_ITEM_TYPE_IPV4] = {
202 .copy_item = enic_copy_item_ipv4_v2,
203 .valid_start_item = 1,
204 .prev_items = (const enum rte_flow_item_type[]) {
205 RTE_FLOW_ITEM_TYPE_ETH,
206 RTE_FLOW_ITEM_TYPE_VLAN,
207 RTE_FLOW_ITEM_TYPE_END,
210 [RTE_FLOW_ITEM_TYPE_IPV6] = {
211 .copy_item = enic_copy_item_ipv6_v2,
212 .valid_start_item = 1,
213 .prev_items = (const enum rte_flow_item_type[]) {
214 RTE_FLOW_ITEM_TYPE_ETH,
215 RTE_FLOW_ITEM_TYPE_VLAN,
216 RTE_FLOW_ITEM_TYPE_END,
219 [RTE_FLOW_ITEM_TYPE_UDP] = {
220 .copy_item = enic_copy_item_udp_v2,
221 .valid_start_item = 1,
222 .prev_items = (const enum rte_flow_item_type[]) {
223 RTE_FLOW_ITEM_TYPE_IPV4,
224 RTE_FLOW_ITEM_TYPE_IPV6,
225 RTE_FLOW_ITEM_TYPE_END,
228 [RTE_FLOW_ITEM_TYPE_TCP] = {
229 .copy_item = enic_copy_item_tcp_v2,
230 .valid_start_item = 1,
231 .prev_items = (const enum rte_flow_item_type[]) {
232 RTE_FLOW_ITEM_TYPE_IPV4,
233 RTE_FLOW_ITEM_TYPE_IPV6,
234 RTE_FLOW_ITEM_TYPE_END,
237 [RTE_FLOW_ITEM_TYPE_SCTP] = {
238 .copy_item = enic_copy_item_sctp_v2,
239 .valid_start_item = 1,
240 .prev_items = (const enum rte_flow_item_type[]) {
241 RTE_FLOW_ITEM_TYPE_IPV4,
242 RTE_FLOW_ITEM_TYPE_IPV6,
243 RTE_FLOW_ITEM_TYPE_END,
246 [RTE_FLOW_ITEM_TYPE_VXLAN] = {
247 .copy_item = enic_copy_item_vxlan_v2,
248 .valid_start_item = 1,
249 .prev_items = (const enum rte_flow_item_type[]) {
250 RTE_FLOW_ITEM_TYPE_UDP,
251 RTE_FLOW_ITEM_TYPE_END,
256 /** Filtering capabilities indexed this NICs supported filter type. */
257 static const struct enic_filter_cap enic_filter_cap[] = {
258 [FILTER_IPV4_5TUPLE] = {
259 .item_info = enic_items_v1,
261 [FILTER_USNIC_IP] = {
262 .item_info = enic_items_v2,
265 .item_info = enic_items_v3,
269 /** Supported actions for older NICs */
270 static const enum rte_flow_action_type enic_supported_actions_v1[] = {
271 RTE_FLOW_ACTION_TYPE_QUEUE,
272 RTE_FLOW_ACTION_TYPE_END,
275 /** Supported actions for newer NICs */
276 static const enum rte_flow_action_type enic_supported_actions_v2[] = {
277 RTE_FLOW_ACTION_TYPE_QUEUE,
278 RTE_FLOW_ACTION_TYPE_MARK,
279 RTE_FLOW_ACTION_TYPE_FLAG,
280 RTE_FLOW_ACTION_TYPE_END,
283 /** Action capabilities indexed by NIC version information */
284 static const struct enic_action_cap enic_action_cap[] = {
285 [FILTER_ACTION_RQ_STEERING_FLAG] = {
286 .actions = enic_supported_actions_v1,
287 .copy_fn = enic_copy_action_v1,
289 [FILTER_ACTION_V2_ALL] = {
290 .actions = enic_supported_actions_v2,
291 .copy_fn = enic_copy_action_v2,
296 mask_exact_match(const u8 *supported, const u8 *supplied,
300 for (i = 0; i < size; i++) {
301 if (supported[i] != supplied[i])
308 * Copy IPv4 item into version 1 NIC filter.
311 * Item specification.
312 * @param enic_filter[out]
313 * Partially filled in NIC filter structure.
314 * @param inner_ofst[in]
315 * Should always be 0 for version 1.
318 enic_copy_item_ipv4_v1(const struct rte_flow_item *item,
319 struct filter_v2 *enic_filter, u8 *inner_ofst)
321 const struct rte_flow_item_ipv4 *spec = item->spec;
322 const struct rte_flow_item_ipv4 *mask = item->mask;
323 struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
324 struct ipv4_hdr supported_mask = {
325 .src_addr = 0xffffffff,
326 .dst_addr = 0xffffffff,
335 mask = &rte_flow_item_ipv4_mask;
337 /* This is an exact match filter, both fields must be set */
338 if (!spec || !spec->hdr.src_addr || !spec->hdr.dst_addr) {
339 FLOW_LOG(ERR, "IPv4 exact match src/dst addr");
343 /* check that the suppied mask exactly matches capabilty */
344 if (!mask_exact_match((const u8 *)&supported_mask,
345 (const u8 *)item->mask, sizeof(*mask))) {
346 FLOW_LOG(ERR, "IPv4 exact match mask");
350 enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
351 enic_5tup->src_addr = spec->hdr.src_addr;
352 enic_5tup->dst_addr = spec->hdr.dst_addr;
358 * Copy UDP item into version 1 NIC filter.
361 * Item specification.
362 * @param enic_filter[out]
363 * Partially filled in NIC filter structure.
364 * @param inner_ofst[in]
365 * Should always be 0 for version 1.
368 enic_copy_item_udp_v1(const struct rte_flow_item *item,
369 struct filter_v2 *enic_filter, u8 *inner_ofst)
371 const struct rte_flow_item_udp *spec = item->spec;
372 const struct rte_flow_item_udp *mask = item->mask;
373 struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
374 struct udp_hdr supported_mask = {
385 mask = &rte_flow_item_udp_mask;
387 /* This is an exact match filter, both ports must be set */
388 if (!spec || !spec->hdr.src_port || !spec->hdr.dst_port) {
389 FLOW_LOG(ERR, "UDP exact match src/dst addr");
393 /* check that the suppied mask exactly matches capabilty */
394 if (!mask_exact_match((const u8 *)&supported_mask,
395 (const u8 *)item->mask, sizeof(*mask))) {
396 FLOW_LOG(ERR, "UDP exact match mask");
400 enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
401 enic_5tup->src_port = spec->hdr.src_port;
402 enic_5tup->dst_port = spec->hdr.dst_port;
403 enic_5tup->protocol = PROTO_UDP;
409 * Copy TCP item into version 1 NIC filter.
412 * Item specification.
413 * @param enic_filter[out]
414 * Partially filled in NIC filter structure.
415 * @param inner_ofst[in]
416 * Should always be 0 for version 1.
419 enic_copy_item_tcp_v1(const struct rte_flow_item *item,
420 struct filter_v2 *enic_filter, u8 *inner_ofst)
422 const struct rte_flow_item_tcp *spec = item->spec;
423 const struct rte_flow_item_tcp *mask = item->mask;
424 struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
425 struct tcp_hdr supported_mask = {
436 mask = &rte_flow_item_tcp_mask;
438 /* This is an exact match filter, both ports must be set */
439 if (!spec || !spec->hdr.src_port || !spec->hdr.dst_port) {
440 FLOW_LOG(ERR, "TCPIPv4 exact match src/dst addr");
444 /* check that the suppied mask exactly matches capabilty */
445 if (!mask_exact_match((const u8 *)&supported_mask,
446 (const u8 *)item->mask, sizeof(*mask))) {
447 FLOW_LOG(ERR, "TCP exact match mask");
451 enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
452 enic_5tup->src_port = spec->hdr.src_port;
453 enic_5tup->dst_port = spec->hdr.dst_port;
454 enic_5tup->protocol = PROTO_TCP;
460 * Copy ETH item into version 2 NIC filter.
463 * Item specification.
464 * @param enic_filter[out]
465 * Partially filled in NIC filter structure.
466 * @param inner_ofst[in]
467 * If zero, this is an outer header. If non-zero, this is the offset into L5
468 * where the header begins.
471 enic_copy_item_eth_v2(const struct rte_flow_item *item,
472 struct filter_v2 *enic_filter, u8 *inner_ofst)
474 struct ether_hdr enic_spec;
475 struct ether_hdr enic_mask;
476 const struct rte_flow_item_eth *spec = item->spec;
477 const struct rte_flow_item_eth *mask = item->mask;
478 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
482 /* Match all if no spec */
487 mask = &rte_flow_item_eth_mask;
489 memcpy(enic_spec.d_addr.addr_bytes, spec->dst.addr_bytes,
491 memcpy(enic_spec.s_addr.addr_bytes, spec->src.addr_bytes,
494 memcpy(enic_mask.d_addr.addr_bytes, mask->dst.addr_bytes,
496 memcpy(enic_mask.s_addr.addr_bytes, mask->src.addr_bytes,
498 enic_spec.ether_type = spec->type;
499 enic_mask.ether_type = mask->type;
501 if (*inner_ofst == 0) {
503 memcpy(gp->layer[FILTER_GENERIC_1_L2].mask, &enic_mask,
504 sizeof(struct ether_hdr));
505 memcpy(gp->layer[FILTER_GENERIC_1_L2].val, &enic_spec,
506 sizeof(struct ether_hdr));
509 if ((*inner_ofst + sizeof(struct ether_hdr)) >
510 FILTER_GENERIC_1_KEY_LEN)
512 /* Offset into L5 where inner Ethernet header goes */
513 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
514 &enic_mask, sizeof(struct ether_hdr));
515 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
516 &enic_spec, sizeof(struct ether_hdr));
517 *inner_ofst += sizeof(struct ether_hdr);
523 * Copy VLAN item into version 2 NIC filter.
526 * Item specification.
527 * @param enic_filter[out]
528 * Partially filled in NIC filter structure.
529 * @param inner_ofst[in]
530 * If zero, this is an outer header. If non-zero, this is the offset into L5
531 * where the header begins.
534 enic_copy_item_vlan_v2(const struct rte_flow_item *item,
535 struct filter_v2 *enic_filter, u8 *inner_ofst)
537 const struct rte_flow_item_vlan *spec = item->spec;
538 const struct rte_flow_item_vlan *mask = item->mask;
539 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
543 /* Match all if no spec */
547 /* Don't support filtering in tpid */
552 mask = &rte_flow_item_vlan_mask;
553 RTE_ASSERT(mask->tpid == 0);
556 if (*inner_ofst == 0) {
557 /* Outer header. Use the vlan mask/val fields */
558 gp->mask_vlan = mask->tci;
559 gp->val_vlan = spec->tci;
561 /* Inner header. Mask/Val start at *inner_ofst into L5 */
562 if ((*inner_ofst + sizeof(struct vlan_hdr)) >
563 FILTER_GENERIC_1_KEY_LEN)
565 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
566 mask, sizeof(struct vlan_hdr));
567 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
568 spec, sizeof(struct vlan_hdr));
569 *inner_ofst += sizeof(struct vlan_hdr);
575 * Copy IPv4 item into version 2 NIC filter.
578 * Item specification.
579 * @param enic_filter[out]
580 * Partially filled in NIC filter structure.
581 * @param inner_ofst[in]
582 * Must be 0. Don't support inner IPv4 filtering.
585 enic_copy_item_ipv4_v2(const struct rte_flow_item *item,
586 struct filter_v2 *enic_filter, u8 *inner_ofst)
588 const struct rte_flow_item_ipv4 *spec = item->spec;
589 const struct rte_flow_item_ipv4 *mask = item->mask;
590 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
594 if (*inner_ofst == 0) {
596 gp->mask_flags |= FILTER_GENERIC_1_IPV4;
597 gp->val_flags |= FILTER_GENERIC_1_IPV4;
599 /* Match all if no spec */
604 mask = &rte_flow_item_ipv4_mask;
606 memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
607 sizeof(struct ipv4_hdr));
608 memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
609 sizeof(struct ipv4_hdr));
611 /* Inner IPv4 header. Mask/Val start at *inner_ofst into L5 */
612 if ((*inner_ofst + sizeof(struct ipv4_hdr)) >
613 FILTER_GENERIC_1_KEY_LEN)
615 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
616 mask, sizeof(struct ipv4_hdr));
617 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
618 spec, sizeof(struct ipv4_hdr));
619 *inner_ofst += sizeof(struct ipv4_hdr);
625 * Copy IPv6 item into version 2 NIC filter.
628 * Item specification.
629 * @param enic_filter[out]
630 * Partially filled in NIC filter structure.
631 * @param inner_ofst[in]
632 * Must be 0. Don't support inner IPv6 filtering.
635 enic_copy_item_ipv6_v2(const struct rte_flow_item *item,
636 struct filter_v2 *enic_filter, u8 *inner_ofst)
638 const struct rte_flow_item_ipv6 *spec = item->spec;
639 const struct rte_flow_item_ipv6 *mask = item->mask;
640 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
645 gp->mask_flags |= FILTER_GENERIC_1_IPV6;
646 gp->val_flags |= FILTER_GENERIC_1_IPV6;
648 /* Match all if no spec */
653 mask = &rte_flow_item_ipv6_mask;
655 if (*inner_ofst == 0) {
656 memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
657 sizeof(struct ipv6_hdr));
658 memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
659 sizeof(struct ipv6_hdr));
661 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
662 if ((*inner_ofst + sizeof(struct ipv6_hdr)) >
663 FILTER_GENERIC_1_KEY_LEN)
665 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
666 mask, sizeof(struct ipv6_hdr));
667 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
668 spec, sizeof(struct ipv6_hdr));
669 *inner_ofst += sizeof(struct ipv6_hdr);
675 * Copy UDP item into version 2 NIC filter.
678 * Item specification.
679 * @param enic_filter[out]
680 * Partially filled in NIC filter structure.
681 * @param inner_ofst[in]
682 * Must be 0. Don't support inner UDP filtering.
685 enic_copy_item_udp_v2(const struct rte_flow_item *item,
686 struct filter_v2 *enic_filter, u8 *inner_ofst)
688 const struct rte_flow_item_udp *spec = item->spec;
689 const struct rte_flow_item_udp *mask = item->mask;
690 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
695 gp->mask_flags |= FILTER_GENERIC_1_UDP;
696 gp->val_flags |= FILTER_GENERIC_1_UDP;
698 /* Match all if no spec */
703 mask = &rte_flow_item_udp_mask;
705 if (*inner_ofst == 0) {
706 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
707 sizeof(struct udp_hdr));
708 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
709 sizeof(struct udp_hdr));
711 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
712 if ((*inner_ofst + sizeof(struct udp_hdr)) >
713 FILTER_GENERIC_1_KEY_LEN)
715 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
716 mask, sizeof(struct udp_hdr));
717 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
718 spec, sizeof(struct udp_hdr));
719 *inner_ofst += sizeof(struct udp_hdr);
725 * Copy TCP item into version 2 NIC filter.
728 * Item specification.
729 * @param enic_filter[out]
730 * Partially filled in NIC filter structure.
731 * @param inner_ofst[in]
732 * Must be 0. Don't support inner TCP filtering.
735 enic_copy_item_tcp_v2(const struct rte_flow_item *item,
736 struct filter_v2 *enic_filter, u8 *inner_ofst)
738 const struct rte_flow_item_tcp *spec = item->spec;
739 const struct rte_flow_item_tcp *mask = item->mask;
740 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
745 gp->mask_flags |= FILTER_GENERIC_1_TCP;
746 gp->val_flags |= FILTER_GENERIC_1_TCP;
748 /* Match all if no spec */
755 if (*inner_ofst == 0) {
756 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
757 sizeof(struct tcp_hdr));
758 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
759 sizeof(struct tcp_hdr));
761 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
762 if ((*inner_ofst + sizeof(struct tcp_hdr)) >
763 FILTER_GENERIC_1_KEY_LEN)
765 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
766 mask, sizeof(struct tcp_hdr));
767 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
768 spec, sizeof(struct tcp_hdr));
769 *inner_ofst += sizeof(struct tcp_hdr);
775 * Copy SCTP item into version 2 NIC filter.
778 * Item specification.
779 * @param enic_filter[out]
780 * Partially filled in NIC filter structure.
781 * @param inner_ofst[in]
782 * Must be 0. Don't support inner SCTP filtering.
785 enic_copy_item_sctp_v2(const struct rte_flow_item *item,
786 struct filter_v2 *enic_filter, u8 *inner_ofst)
788 const struct rte_flow_item_sctp *spec = item->spec;
789 const struct rte_flow_item_sctp *mask = item->mask;
790 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
797 /* Match all if no spec */
802 mask = &rte_flow_item_sctp_mask;
804 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
805 sizeof(struct sctp_hdr));
806 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
807 sizeof(struct sctp_hdr));
812 * Copy UDP item into version 2 NIC filter.
815 * Item specification.
816 * @param enic_filter[out]
817 * Partially filled in NIC filter structure.
818 * @param inner_ofst[in]
819 * Must be 0. VxLAN headers always start at the beginning of L5.
822 enic_copy_item_vxlan_v2(const struct rte_flow_item *item,
823 struct filter_v2 *enic_filter, u8 *inner_ofst)
825 const struct rte_flow_item_vxlan *spec = item->spec;
826 const struct rte_flow_item_vxlan *mask = item->mask;
827 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
834 /* Match all if no spec */
839 mask = &rte_flow_item_vxlan_mask;
841 memcpy(gp->layer[FILTER_GENERIC_1_L5].mask, mask,
842 sizeof(struct vxlan_hdr));
843 memcpy(gp->layer[FILTER_GENERIC_1_L5].val, spec,
844 sizeof(struct vxlan_hdr));
846 *inner_ofst = sizeof(struct vxlan_hdr);
851 * Return 1 if current item is valid on top of the previous one.
853 * @param prev_item[in]
854 * The item before this one in the pattern or RTE_FLOW_ITEM_TYPE_END if this
856 * @param item_info[in]
857 * Info about this item, like valid previous items.
858 * @param is_first[in]
859 * True if this the first item in the pattern.
862 item_stacking_valid(enum rte_flow_item_type prev_item,
863 const struct enic_items *item_info, u8 is_first_item)
865 enum rte_flow_item_type const *allowed_items = item_info->prev_items;
869 for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
870 if (prev_item == *allowed_items)
874 /* This is the first item in the stack. Check if that's cool */
875 if (is_first_item && item_info->valid_start_item)
882 * Build the intenal enic filter structure from the provided pattern. The
883 * pattern is validated as the items are copied.
886 * @param items_info[in]
887 * Info about this NICs item support, like valid previous items.
888 * @param enic_filter[out]
889 * NIC specfilc filters derived from the pattern.
893 enic_copy_filter(const struct rte_flow_item pattern[],
894 const struct enic_items *items_info,
895 struct filter_v2 *enic_filter,
896 struct rte_flow_error *error)
899 const struct rte_flow_item *item = pattern;
900 u8 inner_ofst = 0; /* If encapsulated, ofst into L5 */
901 enum rte_flow_item_type prev_item;
902 const struct enic_items *item_info;
904 u8 is_first_item = 1;
910 for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
911 /* Get info about how to validate and copy the item. If NULL
912 * is returned the nic does not support the item.
914 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
917 item_info = &items_info[item->type];
919 /* check to see if item stacking is valid */
920 if (!item_stacking_valid(prev_item, item_info, is_first_item))
923 ret = item_info->copy_item(item, enic_filter, &inner_ofst);
925 goto item_not_supported;
926 prev_item = item->type;
932 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_ITEM,
933 NULL, "enic type error");
937 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
938 item, "stacking error");
943 * Build the intenal version 1 NIC action structure from the provided pattern.
944 * The pattern is validated as the items are copied.
947 * @param enic_action[out]
948 * NIC specfilc actions derived from the actions.
952 enic_copy_action_v1(const struct rte_flow_action actions[],
953 struct filter_action_v2 *enic_action)
957 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
958 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID)
961 switch (actions->type) {
962 case RTE_FLOW_ACTION_TYPE_QUEUE: {
963 const struct rte_flow_action_queue *queue =
964 (const struct rte_flow_action_queue *)
966 enic_action->rq_idx =
967 enic_rte_rq_idx_to_sop_idx(queue->index);
975 enic_action->type = FILTER_ACTION_RQ_STEERING;
980 * Build the intenal version 2 NIC action structure from the provided pattern.
981 * The pattern is validated as the items are copied.
984 * @param enic_action[out]
985 * NIC specfilc actions derived from the actions.
989 enic_copy_action_v2(const struct rte_flow_action actions[],
990 struct filter_action_v2 *enic_action)
994 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
995 switch (actions->type) {
996 case RTE_FLOW_ACTION_TYPE_QUEUE: {
997 const struct rte_flow_action_queue *queue =
998 (const struct rte_flow_action_queue *)
1000 enic_action->rq_idx =
1001 enic_rte_rq_idx_to_sop_idx(queue->index);
1002 enic_action->flags |= FILTER_ACTION_RQ_STEERING_FLAG;
1005 case RTE_FLOW_ACTION_TYPE_MARK: {
1006 const struct rte_flow_action_mark *mark =
1007 (const struct rte_flow_action_mark *)
1010 /* ENIC_MAGIC_FILTER_ID is reserved and is the highest
1011 * in the range of allows mark ids.
1013 if (mark->id >= ENIC_MAGIC_FILTER_ID)
1015 enic_action->filter_id = mark->id;
1016 enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1019 case RTE_FLOW_ACTION_TYPE_FLAG: {
1020 enic_action->filter_id = ENIC_MAGIC_FILTER_ID;
1021 enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1024 case RTE_FLOW_ACTION_TYPE_VOID:
1031 enic_action->type = FILTER_ACTION_V2;
1035 /** Check if the action is supported */
1037 enic_match_action(const struct rte_flow_action *action,
1038 const enum rte_flow_action_type *supported_actions)
1040 for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
1041 supported_actions++) {
1042 if (action->type == *supported_actions)
1048 /** Get the NIC filter capabilties structure */
1049 static const struct enic_filter_cap *
1050 enic_get_filter_cap(struct enic *enic)
1052 if (enic->flow_filter_mode)
1053 return &enic_filter_cap[enic->flow_filter_mode];
1058 /** Get the actions for this NIC version. */
1059 static const struct enic_action_cap *
1060 enic_get_action_cap(struct enic *enic)
1062 static const struct enic_action_cap *ea;
1064 if (enic->filter_tags)
1065 ea = &enic_action_cap[FILTER_ACTION_V2_ALL];
1067 ea = &enic_action_cap[FILTER_ACTION_RQ_STEERING_FLAG];
1071 /* Debug function to dump internal NIC action structure. */
1073 enic_dump_actions(const struct filter_action_v2 *ea)
1075 if (ea->type == FILTER_ACTION_RQ_STEERING) {
1076 FLOW_LOG(INFO, "Action(V1), queue: %u\n", ea->rq_idx);
1077 } else if (ea->type == FILTER_ACTION_V2) {
1078 FLOW_LOG(INFO, "Actions(V2)\n");
1079 if (ea->flags & FILTER_ACTION_RQ_STEERING_FLAG)
1080 FLOW_LOG(INFO, "\tqueue: %u\n",
1081 enic_sop_rq_idx_to_rte_idx(ea->rq_idx));
1082 if (ea->flags & FILTER_ACTION_FILTER_ID_FLAG)
1083 FLOW_LOG(INFO, "\tfilter_id: %u\n", ea->filter_id);
1087 /* Debug function to dump internal NIC filter structure. */
1089 enic_dump_filter(const struct filter_v2 *filt)
1091 const struct filter_generic_1 *gp;
1094 char ip4[16], ip6[16], udp[16], tcp[16], tcpudp[16], ip4csum[16];
1095 char l4csum[16], ipfrag[16];
1097 switch (filt->type) {
1098 case FILTER_IPV4_5TUPLE:
1099 FLOW_LOG(INFO, "FILTER_IPV4_5TUPLE\n");
1101 case FILTER_USNIC_IP:
1103 /* FIXME: this should be a loop */
1104 gp = &filt->u.generic_1;
1105 FLOW_LOG(INFO, "Filter: vlan: 0x%04x, mask: 0x%04x\n",
1106 gp->val_vlan, gp->mask_vlan);
1108 if (gp->mask_flags & FILTER_GENERIC_1_IPV4)
1110 (gp->val_flags & FILTER_GENERIC_1_IPV4)
1111 ? "ip4(y)" : "ip4(n)");
1113 sprintf(ip4, "%s ", "ip4(x)");
1115 if (gp->mask_flags & FILTER_GENERIC_1_IPV6)
1117 (gp->val_flags & FILTER_GENERIC_1_IPV4)
1118 ? "ip6(y)" : "ip6(n)");
1120 sprintf(ip6, "%s ", "ip6(x)");
1122 if (gp->mask_flags & FILTER_GENERIC_1_UDP)
1124 (gp->val_flags & FILTER_GENERIC_1_UDP)
1125 ? "udp(y)" : "udp(n)");
1127 sprintf(udp, "%s ", "udp(x)");
1129 if (gp->mask_flags & FILTER_GENERIC_1_TCP)
1131 (gp->val_flags & FILTER_GENERIC_1_TCP)
1132 ? "tcp(y)" : "tcp(n)");
1134 sprintf(tcp, "%s ", "tcp(x)");
1136 if (gp->mask_flags & FILTER_GENERIC_1_TCP_OR_UDP)
1137 sprintf(tcpudp, "%s ",
1138 (gp->val_flags & FILTER_GENERIC_1_TCP_OR_UDP)
1139 ? "tcpudp(y)" : "tcpudp(n)");
1141 sprintf(tcpudp, "%s ", "tcpudp(x)");
1143 if (gp->mask_flags & FILTER_GENERIC_1_IP4SUM_OK)
1144 sprintf(ip4csum, "%s ",
1145 (gp->val_flags & FILTER_GENERIC_1_IP4SUM_OK)
1146 ? "ip4csum(y)" : "ip4csum(n)");
1148 sprintf(ip4csum, "%s ", "ip4csum(x)");
1150 if (gp->mask_flags & FILTER_GENERIC_1_L4SUM_OK)
1151 sprintf(l4csum, "%s ",
1152 (gp->val_flags & FILTER_GENERIC_1_L4SUM_OK)
1153 ? "l4csum(y)" : "l4csum(n)");
1155 sprintf(l4csum, "%s ", "l4csum(x)");
1157 if (gp->mask_flags & FILTER_GENERIC_1_IPFRAG)
1158 sprintf(ipfrag, "%s ",
1159 (gp->val_flags & FILTER_GENERIC_1_IPFRAG)
1160 ? "ipfrag(y)" : "ipfrag(n)");
1162 sprintf(ipfrag, "%s ", "ipfrag(x)");
1163 FLOW_LOG(INFO, "\tFlags: %s%s%s%s%s%s%s%s\n", ip4, ip6, udp,
1164 tcp, tcpudp, ip4csum, l4csum, ipfrag);
1166 for (i = 0; i < FILTER_GENERIC_1_NUM_LAYERS; i++) {
1167 mbyte = FILTER_GENERIC_1_KEY_LEN - 1;
1168 while (mbyte && !gp->layer[i].mask[mbyte])
1174 for (j = 0; j <= mbyte; j++) {
1176 gp->layer[i].mask[j]);
1180 FLOW_LOG(INFO, "\tL%u mask: %s\n", i + 2, buf);
1182 for (j = 0; j <= mbyte; j++) {
1184 gp->layer[i].val[j]);
1188 FLOW_LOG(INFO, "\tL%u val: %s\n", i + 2, buf);
1192 FLOW_LOG(INFO, "FILTER UNKNOWN\n");
1197 /* Debug function to dump internal NIC flow structures. */
1199 enic_dump_flow(const struct filter_action_v2 *ea, const struct filter_v2 *filt)
1201 enic_dump_filter(filt);
1202 enic_dump_actions(ea);
1207 * Internal flow parse/validate function.
1210 * This device pointer.
1211 * @param pattern[in]
1212 * @param actions[in]
1214 * @param enic_filter[out]
1215 * Internal NIC filter structure pointer.
1216 * @param enic_action[out]
1217 * Internal NIC action structure pointer.
1220 enic_flow_parse(struct rte_eth_dev *dev,
1221 const struct rte_flow_attr *attrs,
1222 const struct rte_flow_item pattern[],
1223 const struct rte_flow_action actions[],
1224 struct rte_flow_error *error,
1225 struct filter_v2 *enic_filter,
1226 struct filter_action_v2 *enic_action)
1228 unsigned int ret = 0;
1229 struct enic *enic = pmd_priv(dev);
1230 const struct enic_filter_cap *enic_filter_cap;
1231 const struct enic_action_cap *enic_action_cap;
1232 const struct rte_flow_action *action;
1236 memset(enic_filter, 0, sizeof(*enic_filter));
1237 memset(enic_action, 0, sizeof(*enic_action));
1240 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1241 NULL, "No pattern specified");
1246 rte_flow_error_set(error, EINVAL,
1247 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1248 NULL, "No action specified");
1254 rte_flow_error_set(error, ENOTSUP,
1255 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1257 "priority groups are not supported");
1259 } else if (attrs->priority) {
1260 rte_flow_error_set(error, ENOTSUP,
1261 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1263 "priorities are not supported");
1265 } else if (attrs->egress) {
1266 rte_flow_error_set(error, ENOTSUP,
1267 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
1269 "egress is not supported");
1271 } else if (!attrs->ingress) {
1272 rte_flow_error_set(error, ENOTSUP,
1273 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1275 "only ingress is supported");
1280 rte_flow_error_set(error, EINVAL,
1281 RTE_FLOW_ERROR_TYPE_ATTR,
1282 NULL, "No attribute specified");
1286 /* Verify Actions. */
1287 enic_action_cap = enic_get_action_cap(enic);
1288 for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
1290 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1292 else if (!enic_match_action(action, enic_action_cap->actions))
1295 if (action->type != RTE_FLOW_ACTION_TYPE_END) {
1296 rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
1297 action, "Invalid action.");
1300 ret = enic_action_cap->copy_fn(actions, enic_action);
1302 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1303 NULL, "Unsupported action.");
1307 /* Verify Flow items. If copying the filter from flow format to enic
1308 * format fails, the flow is not supported
1310 enic_filter_cap = enic_get_filter_cap(enic);
1311 if (enic_filter_cap == NULL) {
1312 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1313 NULL, "Flow API not available");
1316 enic_filter->type = enic->flow_filter_mode;
1317 ret = enic_copy_filter(pattern, enic_filter_cap->item_info,
1318 enic_filter, error);
1323 * Push filter/action to the NIC.
1326 * Device structure pointer.
1327 * @param enic_filter[in]
1328 * Internal NIC filter structure pointer.
1329 * @param enic_action[in]
1330 * Internal NIC action structure pointer.
1333 static struct rte_flow *
1334 enic_flow_add_filter(struct enic *enic, struct filter_v2 *enic_filter,
1335 struct filter_action_v2 *enic_action,
1336 struct rte_flow_error *error)
1338 struct rte_flow *flow;
1344 flow = rte_calloc(__func__, 1, sizeof(*flow), 0);
1346 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1347 NULL, "cannot allocate flow memory");
1351 /* entry[in] is the queue id, entry[out] is the filter Id for delete */
1352 entry = enic_action->rq_idx;
1353 ret = vnic_dev_classifier(enic->vdev, CLSF_ADD, &entry, enic_filter,
1356 flow->enic_filter_id = entry;
1357 flow->enic_filter = *enic_filter;
1359 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
1360 NULL, "vnic_dev_classifier error");
1368 * Remove filter/action from the NIC.
1371 * Device structure pointer.
1372 * @param filter_id[in]
1374 * @param enic_action[in]
1375 * Internal NIC action structure pointer.
1379 enic_flow_del_filter(struct enic *enic, u16 filter_id,
1380 struct rte_flow_error *error)
1386 ret = vnic_dev_classifier(enic->vdev, CLSF_DEL, &filter_id, NULL, NULL);
1388 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
1389 NULL, "vnic_dev_classifier failed");
1394 * The following functions are callbacks for Generic flow API.
1398 * Validate a flow supported by the NIC.
1400 * @see rte_flow_validate()
1404 enic_flow_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attrs,
1405 const struct rte_flow_item pattern[],
1406 const struct rte_flow_action actions[],
1407 struct rte_flow_error *error)
1409 struct filter_v2 enic_filter;
1410 struct filter_action_v2 enic_action;
1415 ret = enic_flow_parse(dev, attrs, pattern, actions, error,
1416 &enic_filter, &enic_action);
1418 enic_dump_flow(&enic_action, &enic_filter);
1423 * Create a flow supported by the NIC.
1425 * @see rte_flow_create()
1428 static struct rte_flow *
1429 enic_flow_create(struct rte_eth_dev *dev,
1430 const struct rte_flow_attr *attrs,
1431 const struct rte_flow_item pattern[],
1432 const struct rte_flow_action actions[],
1433 struct rte_flow_error *error)
1436 struct filter_v2 enic_filter;
1437 struct filter_action_v2 enic_action;
1438 struct rte_flow *flow;
1439 struct enic *enic = pmd_priv(dev);
1443 ret = enic_flow_parse(dev, attrs, pattern, actions, error, &enic_filter,
1448 rte_spinlock_lock(&enic->flows_lock);
1449 flow = enic_flow_add_filter(enic, &enic_filter, &enic_action,
1452 LIST_INSERT_HEAD(&enic->flows, flow, next);
1453 rte_spinlock_unlock(&enic->flows_lock);
1459 * Destroy a flow supported by the NIC.
1461 * @see rte_flow_destroy()
1465 enic_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
1466 __rte_unused struct rte_flow_error *error)
1468 struct enic *enic = pmd_priv(dev);
1472 rte_spinlock_lock(&enic->flows_lock);
1473 enic_flow_del_filter(enic, flow->enic_filter_id, error);
1474 LIST_REMOVE(flow, next);
1475 rte_spinlock_unlock(&enic->flows_lock);
1480 * Flush all flows on the device.
1482 * @see rte_flow_flush()
1486 enic_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1488 struct rte_flow *flow;
1489 struct enic *enic = pmd_priv(dev);
1493 rte_spinlock_lock(&enic->flows_lock);
1495 while (!LIST_EMPTY(&enic->flows)) {
1496 flow = LIST_FIRST(&enic->flows);
1497 enic_flow_del_filter(enic, flow->enic_filter_id, error);
1498 LIST_REMOVE(flow, next);
1500 rte_spinlock_unlock(&enic->flows_lock);
1505 * Flow callback registration.
1509 const struct rte_flow_ops enic_flow_ops = {
1510 .validate = enic_flow_validate,
1511 .create = enic_flow_create,
1512 .destroy = enic_flow_destroy,
1513 .flush = enic_flow_flush,