1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2008-2017 Cisco Systems, Inc. All rights reserved.
7 #include <rte_ethdev_driver.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_id[] = {
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 static const enum rte_flow_action_type enic_supported_actions_v2_drop[] = {
284 RTE_FLOW_ACTION_TYPE_QUEUE,
285 RTE_FLOW_ACTION_TYPE_MARK,
286 RTE_FLOW_ACTION_TYPE_FLAG,
287 RTE_FLOW_ACTION_TYPE_DROP,
288 RTE_FLOW_ACTION_TYPE_END,
291 /** Action capabilities indexed by NIC version information */
292 static const struct enic_action_cap enic_action_cap[] = {
293 [FILTER_ACTION_RQ_STEERING_FLAG] = {
294 .actions = enic_supported_actions_v1,
295 .copy_fn = enic_copy_action_v1,
297 [FILTER_ACTION_FILTER_ID_FLAG] = {
298 .actions = enic_supported_actions_v2_id,
299 .copy_fn = enic_copy_action_v2,
301 [FILTER_ACTION_DROP_FLAG] = {
302 .actions = enic_supported_actions_v2_drop,
303 .copy_fn = enic_copy_action_v2,
308 mask_exact_match(const u8 *supported, const u8 *supplied,
312 for (i = 0; i < size; i++) {
313 if (supported[i] != supplied[i])
320 * Copy IPv4 item into version 1 NIC filter.
323 * Item specification.
324 * @param enic_filter[out]
325 * Partially filled in NIC filter structure.
326 * @param inner_ofst[in]
327 * Should always be 0 for version 1.
330 enic_copy_item_ipv4_v1(const struct rte_flow_item *item,
331 struct filter_v2 *enic_filter, u8 *inner_ofst)
333 const struct rte_flow_item_ipv4 *spec = item->spec;
334 const struct rte_flow_item_ipv4 *mask = item->mask;
335 struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
336 struct ipv4_hdr supported_mask = {
337 .src_addr = 0xffffffff,
338 .dst_addr = 0xffffffff,
347 mask = &rte_flow_item_ipv4_mask;
349 /* This is an exact match filter, both fields must be set */
350 if (!spec || !spec->hdr.src_addr || !spec->hdr.dst_addr) {
351 FLOW_LOG(ERR, "IPv4 exact match src/dst addr");
355 /* check that the suppied mask exactly matches capabilty */
356 if (!mask_exact_match((const u8 *)&supported_mask,
357 (const u8 *)item->mask, sizeof(*mask))) {
358 FLOW_LOG(ERR, "IPv4 exact match mask");
362 enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
363 enic_5tup->src_addr = spec->hdr.src_addr;
364 enic_5tup->dst_addr = spec->hdr.dst_addr;
370 * Copy UDP item into version 1 NIC filter.
373 * Item specification.
374 * @param enic_filter[out]
375 * Partially filled in NIC filter structure.
376 * @param inner_ofst[in]
377 * Should always be 0 for version 1.
380 enic_copy_item_udp_v1(const struct rte_flow_item *item,
381 struct filter_v2 *enic_filter, u8 *inner_ofst)
383 const struct rte_flow_item_udp *spec = item->spec;
384 const struct rte_flow_item_udp *mask = item->mask;
385 struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
386 struct udp_hdr supported_mask = {
397 mask = &rte_flow_item_udp_mask;
399 /* This is an exact match filter, both ports must be set */
400 if (!spec || !spec->hdr.src_port || !spec->hdr.dst_port) {
401 FLOW_LOG(ERR, "UDP exact match src/dst addr");
405 /* check that the suppied mask exactly matches capabilty */
406 if (!mask_exact_match((const u8 *)&supported_mask,
407 (const u8 *)item->mask, sizeof(*mask))) {
408 FLOW_LOG(ERR, "UDP exact match mask");
412 enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
413 enic_5tup->src_port = spec->hdr.src_port;
414 enic_5tup->dst_port = spec->hdr.dst_port;
415 enic_5tup->protocol = PROTO_UDP;
421 * Copy TCP item into version 1 NIC filter.
424 * Item specification.
425 * @param enic_filter[out]
426 * Partially filled in NIC filter structure.
427 * @param inner_ofst[in]
428 * Should always be 0 for version 1.
431 enic_copy_item_tcp_v1(const struct rte_flow_item *item,
432 struct filter_v2 *enic_filter, u8 *inner_ofst)
434 const struct rte_flow_item_tcp *spec = item->spec;
435 const struct rte_flow_item_tcp *mask = item->mask;
436 struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
437 struct tcp_hdr supported_mask = {
448 mask = &rte_flow_item_tcp_mask;
450 /* This is an exact match filter, both ports must be set */
451 if (!spec || !spec->hdr.src_port || !spec->hdr.dst_port) {
452 FLOW_LOG(ERR, "TCPIPv4 exact match src/dst addr");
456 /* check that the suppied mask exactly matches capabilty */
457 if (!mask_exact_match((const u8 *)&supported_mask,
458 (const u8 *)item->mask, sizeof(*mask))) {
459 FLOW_LOG(ERR, "TCP exact match mask");
463 enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
464 enic_5tup->src_port = spec->hdr.src_port;
465 enic_5tup->dst_port = spec->hdr.dst_port;
466 enic_5tup->protocol = PROTO_TCP;
472 * Copy ETH item into version 2 NIC filter.
475 * Item specification.
476 * @param enic_filter[out]
477 * Partially filled in NIC filter structure.
478 * @param inner_ofst[in]
479 * If zero, this is an outer header. If non-zero, this is the offset into L5
480 * where the header begins.
483 enic_copy_item_eth_v2(const struct rte_flow_item *item,
484 struct filter_v2 *enic_filter, u8 *inner_ofst)
486 struct ether_hdr enic_spec;
487 struct ether_hdr enic_mask;
488 const struct rte_flow_item_eth *spec = item->spec;
489 const struct rte_flow_item_eth *mask = item->mask;
490 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
494 /* Match all if no spec */
499 mask = &rte_flow_item_eth_mask;
501 memcpy(enic_spec.d_addr.addr_bytes, spec->dst.addr_bytes,
503 memcpy(enic_spec.s_addr.addr_bytes, spec->src.addr_bytes,
506 memcpy(enic_mask.d_addr.addr_bytes, mask->dst.addr_bytes,
508 memcpy(enic_mask.s_addr.addr_bytes, mask->src.addr_bytes,
510 enic_spec.ether_type = spec->type;
511 enic_mask.ether_type = mask->type;
513 if (*inner_ofst == 0) {
515 memcpy(gp->layer[FILTER_GENERIC_1_L2].mask, &enic_mask,
516 sizeof(struct ether_hdr));
517 memcpy(gp->layer[FILTER_GENERIC_1_L2].val, &enic_spec,
518 sizeof(struct ether_hdr));
521 if ((*inner_ofst + sizeof(struct ether_hdr)) >
522 FILTER_GENERIC_1_KEY_LEN)
524 /* Offset into L5 where inner Ethernet header goes */
525 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
526 &enic_mask, sizeof(struct ether_hdr));
527 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
528 &enic_spec, sizeof(struct ether_hdr));
529 *inner_ofst += sizeof(struct ether_hdr);
535 * Copy VLAN item into version 2 NIC filter.
538 * Item specification.
539 * @param enic_filter[out]
540 * Partially filled in NIC filter structure.
541 * @param inner_ofst[in]
542 * If zero, this is an outer header. If non-zero, this is the offset into L5
543 * where the header begins.
546 enic_copy_item_vlan_v2(const struct rte_flow_item *item,
547 struct filter_v2 *enic_filter, u8 *inner_ofst)
549 const struct rte_flow_item_vlan *spec = item->spec;
550 const struct rte_flow_item_vlan *mask = item->mask;
551 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
555 /* Match all if no spec */
559 /* Don't support filtering in tpid */
564 mask = &rte_flow_item_vlan_mask;
565 RTE_ASSERT(mask->tpid == 0);
568 if (*inner_ofst == 0) {
569 /* Outer header. Use the vlan mask/val fields */
570 gp->mask_vlan = mask->tci;
571 gp->val_vlan = spec->tci;
573 /* Inner header. Mask/Val start at *inner_ofst into L5 */
574 if ((*inner_ofst + sizeof(struct vlan_hdr)) >
575 FILTER_GENERIC_1_KEY_LEN)
577 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
578 mask, sizeof(struct vlan_hdr));
579 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
580 spec, sizeof(struct vlan_hdr));
581 *inner_ofst += sizeof(struct vlan_hdr);
587 * Copy IPv4 item into version 2 NIC filter.
590 * Item specification.
591 * @param enic_filter[out]
592 * Partially filled in NIC filter structure.
593 * @param inner_ofst[in]
594 * Must be 0. Don't support inner IPv4 filtering.
597 enic_copy_item_ipv4_v2(const struct rte_flow_item *item,
598 struct filter_v2 *enic_filter, u8 *inner_ofst)
600 const struct rte_flow_item_ipv4 *spec = item->spec;
601 const struct rte_flow_item_ipv4 *mask = item->mask;
602 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
606 if (*inner_ofst == 0) {
608 gp->mask_flags |= FILTER_GENERIC_1_IPV4;
609 gp->val_flags |= FILTER_GENERIC_1_IPV4;
611 /* Match all if no spec */
616 mask = &rte_flow_item_ipv4_mask;
618 memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
619 sizeof(struct ipv4_hdr));
620 memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
621 sizeof(struct ipv4_hdr));
623 /* Inner IPv4 header. Mask/Val start at *inner_ofst into L5 */
624 if ((*inner_ofst + sizeof(struct ipv4_hdr)) >
625 FILTER_GENERIC_1_KEY_LEN)
627 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
628 mask, sizeof(struct ipv4_hdr));
629 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
630 spec, sizeof(struct ipv4_hdr));
631 *inner_ofst += sizeof(struct ipv4_hdr);
637 * Copy IPv6 item into version 2 NIC filter.
640 * Item specification.
641 * @param enic_filter[out]
642 * Partially filled in NIC filter structure.
643 * @param inner_ofst[in]
644 * Must be 0. Don't support inner IPv6 filtering.
647 enic_copy_item_ipv6_v2(const struct rte_flow_item *item,
648 struct filter_v2 *enic_filter, u8 *inner_ofst)
650 const struct rte_flow_item_ipv6 *spec = item->spec;
651 const struct rte_flow_item_ipv6 *mask = item->mask;
652 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
657 gp->mask_flags |= FILTER_GENERIC_1_IPV6;
658 gp->val_flags |= FILTER_GENERIC_1_IPV6;
660 /* Match all if no spec */
665 mask = &rte_flow_item_ipv6_mask;
667 if (*inner_ofst == 0) {
668 memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
669 sizeof(struct ipv6_hdr));
670 memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
671 sizeof(struct ipv6_hdr));
673 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
674 if ((*inner_ofst + sizeof(struct ipv6_hdr)) >
675 FILTER_GENERIC_1_KEY_LEN)
677 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
678 mask, sizeof(struct ipv6_hdr));
679 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
680 spec, sizeof(struct ipv6_hdr));
681 *inner_ofst += sizeof(struct ipv6_hdr);
687 * Copy UDP item into version 2 NIC filter.
690 * Item specification.
691 * @param enic_filter[out]
692 * Partially filled in NIC filter structure.
693 * @param inner_ofst[in]
694 * Must be 0. Don't support inner UDP filtering.
697 enic_copy_item_udp_v2(const struct rte_flow_item *item,
698 struct filter_v2 *enic_filter, u8 *inner_ofst)
700 const struct rte_flow_item_udp *spec = item->spec;
701 const struct rte_flow_item_udp *mask = item->mask;
702 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
707 gp->mask_flags |= FILTER_GENERIC_1_UDP;
708 gp->val_flags |= FILTER_GENERIC_1_UDP;
710 /* Match all if no spec */
715 mask = &rte_flow_item_udp_mask;
717 if (*inner_ofst == 0) {
718 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
719 sizeof(struct udp_hdr));
720 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
721 sizeof(struct udp_hdr));
723 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
724 if ((*inner_ofst + sizeof(struct udp_hdr)) >
725 FILTER_GENERIC_1_KEY_LEN)
727 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
728 mask, sizeof(struct udp_hdr));
729 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
730 spec, sizeof(struct udp_hdr));
731 *inner_ofst += sizeof(struct udp_hdr);
737 * Copy TCP item into version 2 NIC filter.
740 * Item specification.
741 * @param enic_filter[out]
742 * Partially filled in NIC filter structure.
743 * @param inner_ofst[in]
744 * Must be 0. Don't support inner TCP filtering.
747 enic_copy_item_tcp_v2(const struct rte_flow_item *item,
748 struct filter_v2 *enic_filter, u8 *inner_ofst)
750 const struct rte_flow_item_tcp *spec = item->spec;
751 const struct rte_flow_item_tcp *mask = item->mask;
752 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
757 gp->mask_flags |= FILTER_GENERIC_1_TCP;
758 gp->val_flags |= FILTER_GENERIC_1_TCP;
760 /* Match all if no spec */
767 if (*inner_ofst == 0) {
768 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
769 sizeof(struct tcp_hdr));
770 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
771 sizeof(struct tcp_hdr));
773 /* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
774 if ((*inner_ofst + sizeof(struct tcp_hdr)) >
775 FILTER_GENERIC_1_KEY_LEN)
777 memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
778 mask, sizeof(struct tcp_hdr));
779 memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
780 spec, sizeof(struct tcp_hdr));
781 *inner_ofst += sizeof(struct tcp_hdr);
787 * Copy SCTP item into version 2 NIC filter.
790 * Item specification.
791 * @param enic_filter[out]
792 * Partially filled in NIC filter structure.
793 * @param inner_ofst[in]
794 * Must be 0. Don't support inner SCTP filtering.
797 enic_copy_item_sctp_v2(const struct rte_flow_item *item,
798 struct filter_v2 *enic_filter, u8 *inner_ofst)
800 const struct rte_flow_item_sctp *spec = item->spec;
801 const struct rte_flow_item_sctp *mask = item->mask;
802 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
809 /* Match all if no spec */
814 mask = &rte_flow_item_sctp_mask;
816 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
817 sizeof(struct sctp_hdr));
818 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
819 sizeof(struct sctp_hdr));
824 * Copy UDP item into version 2 NIC filter.
827 * Item specification.
828 * @param enic_filter[out]
829 * Partially filled in NIC filter structure.
830 * @param inner_ofst[in]
831 * Must be 0. VxLAN headers always start at the beginning of L5.
834 enic_copy_item_vxlan_v2(const struct rte_flow_item *item,
835 struct filter_v2 *enic_filter, u8 *inner_ofst)
837 const struct rte_flow_item_vxlan *spec = item->spec;
838 const struct rte_flow_item_vxlan *mask = item->mask;
839 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
846 /* Match all if no spec */
851 mask = &rte_flow_item_vxlan_mask;
853 memcpy(gp->layer[FILTER_GENERIC_1_L5].mask, mask,
854 sizeof(struct vxlan_hdr));
855 memcpy(gp->layer[FILTER_GENERIC_1_L5].val, spec,
856 sizeof(struct vxlan_hdr));
858 *inner_ofst = sizeof(struct vxlan_hdr);
863 * Return 1 if current item is valid on top of the previous one.
865 * @param prev_item[in]
866 * The item before this one in the pattern or RTE_FLOW_ITEM_TYPE_END if this
868 * @param item_info[in]
869 * Info about this item, like valid previous items.
870 * @param is_first[in]
871 * True if this the first item in the pattern.
874 item_stacking_valid(enum rte_flow_item_type prev_item,
875 const struct enic_items *item_info, u8 is_first_item)
877 enum rte_flow_item_type const *allowed_items = item_info->prev_items;
881 for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
882 if (prev_item == *allowed_items)
886 /* This is the first item in the stack. Check if that's cool */
887 if (is_first_item && item_info->valid_start_item)
894 * Build the intenal enic filter structure from the provided pattern. The
895 * pattern is validated as the items are copied.
898 * @param items_info[in]
899 * Info about this NICs item support, like valid previous items.
900 * @param enic_filter[out]
901 * NIC specfilc filters derived from the pattern.
905 enic_copy_filter(const struct rte_flow_item pattern[],
906 const struct enic_items *items_info,
907 struct filter_v2 *enic_filter,
908 struct rte_flow_error *error)
911 const struct rte_flow_item *item = pattern;
912 u8 inner_ofst = 0; /* If encapsulated, ofst into L5 */
913 enum rte_flow_item_type prev_item;
914 const struct enic_items *item_info;
916 u8 is_first_item = 1;
922 for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
923 /* Get info about how to validate and copy the item. If NULL
924 * is returned the nic does not support the item.
926 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
929 item_info = &items_info[item->type];
931 /* check to see if item stacking is valid */
932 if (!item_stacking_valid(prev_item, item_info, is_first_item))
935 ret = item_info->copy_item(item, enic_filter, &inner_ofst);
937 goto item_not_supported;
938 prev_item = item->type;
944 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_ITEM,
945 NULL, "enic type error");
949 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
950 item, "stacking error");
955 * Build the intenal version 1 NIC action structure from the provided pattern.
956 * The pattern is validated as the items are copied.
959 * @param enic_action[out]
960 * NIC specfilc actions derived from the actions.
964 enic_copy_action_v1(const struct rte_flow_action actions[],
965 struct filter_action_v2 *enic_action)
969 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
970 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID)
973 switch (actions->type) {
974 case RTE_FLOW_ACTION_TYPE_QUEUE: {
975 const struct rte_flow_action_queue *queue =
976 (const struct rte_flow_action_queue *)
978 enic_action->rq_idx =
979 enic_rte_rq_idx_to_sop_idx(queue->index);
987 enic_action->type = FILTER_ACTION_RQ_STEERING;
992 * Build the intenal version 2 NIC action structure from the provided pattern.
993 * The pattern is validated as the items are copied.
996 * @param enic_action[out]
997 * NIC specfilc actions derived from the actions.
1001 enic_copy_action_v2(const struct rte_flow_action actions[],
1002 struct filter_action_v2 *enic_action)
1006 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1007 switch (actions->type) {
1008 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1009 const struct rte_flow_action_queue *queue =
1010 (const struct rte_flow_action_queue *)
1012 enic_action->rq_idx =
1013 enic_rte_rq_idx_to_sop_idx(queue->index);
1014 enic_action->flags |= FILTER_ACTION_RQ_STEERING_FLAG;
1017 case RTE_FLOW_ACTION_TYPE_MARK: {
1018 const struct rte_flow_action_mark *mark =
1019 (const struct rte_flow_action_mark *)
1022 /* ENIC_MAGIC_FILTER_ID is reserved and is the highest
1023 * in the range of allows mark ids.
1025 if (mark->id >= ENIC_MAGIC_FILTER_ID)
1027 enic_action->filter_id = mark->id;
1028 enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1031 case RTE_FLOW_ACTION_TYPE_FLAG: {
1032 enic_action->filter_id = ENIC_MAGIC_FILTER_ID;
1033 enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1036 case RTE_FLOW_ACTION_TYPE_DROP: {
1037 enic_action->flags |= FILTER_ACTION_DROP_FLAG;
1040 case RTE_FLOW_ACTION_TYPE_VOID:
1047 enic_action->type = FILTER_ACTION_V2;
1051 /** Check if the action is supported */
1053 enic_match_action(const struct rte_flow_action *action,
1054 const enum rte_flow_action_type *supported_actions)
1056 for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
1057 supported_actions++) {
1058 if (action->type == *supported_actions)
1064 /** Get the NIC filter capabilties structure */
1065 static const struct enic_filter_cap *
1066 enic_get_filter_cap(struct enic *enic)
1068 if (enic->flow_filter_mode)
1069 return &enic_filter_cap[enic->flow_filter_mode];
1074 /** Get the actions for this NIC version. */
1075 static const struct enic_action_cap *
1076 enic_get_action_cap(struct enic *enic)
1078 const struct enic_action_cap *ea;
1081 actions = enic->filter_actions;
1082 if (actions & FILTER_ACTION_DROP_FLAG)
1083 ea = &enic_action_cap[FILTER_ACTION_DROP_FLAG];
1084 else if (actions & FILTER_ACTION_FILTER_ID_FLAG)
1085 ea = &enic_action_cap[FILTER_ACTION_FILTER_ID_FLAG];
1087 ea = &enic_action_cap[FILTER_ACTION_RQ_STEERING_FLAG];
1091 /* Debug function to dump internal NIC action structure. */
1093 enic_dump_actions(const struct filter_action_v2 *ea)
1095 if (ea->type == FILTER_ACTION_RQ_STEERING) {
1096 FLOW_LOG(INFO, "Action(V1), queue: %u\n", ea->rq_idx);
1097 } else if (ea->type == FILTER_ACTION_V2) {
1098 FLOW_LOG(INFO, "Actions(V2)\n");
1099 if (ea->flags & FILTER_ACTION_RQ_STEERING_FLAG)
1100 FLOW_LOG(INFO, "\tqueue: %u\n",
1101 enic_sop_rq_idx_to_rte_idx(ea->rq_idx));
1102 if (ea->flags & FILTER_ACTION_FILTER_ID_FLAG)
1103 FLOW_LOG(INFO, "\tfilter_id: %u\n", ea->filter_id);
1107 /* Debug function to dump internal NIC filter structure. */
1109 enic_dump_filter(const struct filter_v2 *filt)
1111 const struct filter_generic_1 *gp;
1114 char ip4[16], ip6[16], udp[16], tcp[16], tcpudp[16], ip4csum[16];
1115 char l4csum[16], ipfrag[16];
1117 switch (filt->type) {
1118 case FILTER_IPV4_5TUPLE:
1119 FLOW_LOG(INFO, "FILTER_IPV4_5TUPLE\n");
1121 case FILTER_USNIC_IP:
1123 /* FIXME: this should be a loop */
1124 gp = &filt->u.generic_1;
1125 FLOW_LOG(INFO, "Filter: vlan: 0x%04x, mask: 0x%04x\n",
1126 gp->val_vlan, gp->mask_vlan);
1128 if (gp->mask_flags & FILTER_GENERIC_1_IPV4)
1130 (gp->val_flags & FILTER_GENERIC_1_IPV4)
1131 ? "ip4(y)" : "ip4(n)");
1133 sprintf(ip4, "%s ", "ip4(x)");
1135 if (gp->mask_flags & FILTER_GENERIC_1_IPV6)
1137 (gp->val_flags & FILTER_GENERIC_1_IPV4)
1138 ? "ip6(y)" : "ip6(n)");
1140 sprintf(ip6, "%s ", "ip6(x)");
1142 if (gp->mask_flags & FILTER_GENERIC_1_UDP)
1144 (gp->val_flags & FILTER_GENERIC_1_UDP)
1145 ? "udp(y)" : "udp(n)");
1147 sprintf(udp, "%s ", "udp(x)");
1149 if (gp->mask_flags & FILTER_GENERIC_1_TCP)
1151 (gp->val_flags & FILTER_GENERIC_1_TCP)
1152 ? "tcp(y)" : "tcp(n)");
1154 sprintf(tcp, "%s ", "tcp(x)");
1156 if (gp->mask_flags & FILTER_GENERIC_1_TCP_OR_UDP)
1157 sprintf(tcpudp, "%s ",
1158 (gp->val_flags & FILTER_GENERIC_1_TCP_OR_UDP)
1159 ? "tcpudp(y)" : "tcpudp(n)");
1161 sprintf(tcpudp, "%s ", "tcpudp(x)");
1163 if (gp->mask_flags & FILTER_GENERIC_1_IP4SUM_OK)
1164 sprintf(ip4csum, "%s ",
1165 (gp->val_flags & FILTER_GENERIC_1_IP4SUM_OK)
1166 ? "ip4csum(y)" : "ip4csum(n)");
1168 sprintf(ip4csum, "%s ", "ip4csum(x)");
1170 if (gp->mask_flags & FILTER_GENERIC_1_L4SUM_OK)
1171 sprintf(l4csum, "%s ",
1172 (gp->val_flags & FILTER_GENERIC_1_L4SUM_OK)
1173 ? "l4csum(y)" : "l4csum(n)");
1175 sprintf(l4csum, "%s ", "l4csum(x)");
1177 if (gp->mask_flags & FILTER_GENERIC_1_IPFRAG)
1178 sprintf(ipfrag, "%s ",
1179 (gp->val_flags & FILTER_GENERIC_1_IPFRAG)
1180 ? "ipfrag(y)" : "ipfrag(n)");
1182 sprintf(ipfrag, "%s ", "ipfrag(x)");
1183 FLOW_LOG(INFO, "\tFlags: %s%s%s%s%s%s%s%s\n", ip4, ip6, udp,
1184 tcp, tcpudp, ip4csum, l4csum, ipfrag);
1186 for (i = 0; i < FILTER_GENERIC_1_NUM_LAYERS; i++) {
1187 mbyte = FILTER_GENERIC_1_KEY_LEN - 1;
1188 while (mbyte && !gp->layer[i].mask[mbyte])
1194 for (j = 0; j <= mbyte; j++) {
1196 gp->layer[i].mask[j]);
1200 FLOW_LOG(INFO, "\tL%u mask: %s\n", i + 2, buf);
1202 for (j = 0; j <= mbyte; j++) {
1204 gp->layer[i].val[j]);
1208 FLOW_LOG(INFO, "\tL%u val: %s\n", i + 2, buf);
1212 FLOW_LOG(INFO, "FILTER UNKNOWN\n");
1217 /* Debug function to dump internal NIC flow structures. */
1219 enic_dump_flow(const struct filter_action_v2 *ea, const struct filter_v2 *filt)
1221 enic_dump_filter(filt);
1222 enic_dump_actions(ea);
1227 * Internal flow parse/validate function.
1230 * This device pointer.
1231 * @param pattern[in]
1232 * @param actions[in]
1234 * @param enic_filter[out]
1235 * Internal NIC filter structure pointer.
1236 * @param enic_action[out]
1237 * Internal NIC action structure pointer.
1240 enic_flow_parse(struct rte_eth_dev *dev,
1241 const struct rte_flow_attr *attrs,
1242 const struct rte_flow_item pattern[],
1243 const struct rte_flow_action actions[],
1244 struct rte_flow_error *error,
1245 struct filter_v2 *enic_filter,
1246 struct filter_action_v2 *enic_action)
1248 unsigned int ret = 0;
1249 struct enic *enic = pmd_priv(dev);
1250 const struct enic_filter_cap *enic_filter_cap;
1251 const struct enic_action_cap *enic_action_cap;
1252 const struct rte_flow_action *action;
1256 memset(enic_filter, 0, sizeof(*enic_filter));
1257 memset(enic_action, 0, sizeof(*enic_action));
1260 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1261 NULL, "No pattern specified");
1266 rte_flow_error_set(error, EINVAL,
1267 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1268 NULL, "No action specified");
1274 rte_flow_error_set(error, ENOTSUP,
1275 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1277 "priority groups are not supported");
1279 } else if (attrs->priority) {
1280 rte_flow_error_set(error, ENOTSUP,
1281 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1283 "priorities are not supported");
1285 } else if (attrs->egress) {
1286 rte_flow_error_set(error, ENOTSUP,
1287 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
1289 "egress is not supported");
1291 } else if (!attrs->ingress) {
1292 rte_flow_error_set(error, ENOTSUP,
1293 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1295 "only ingress is supported");
1300 rte_flow_error_set(error, EINVAL,
1301 RTE_FLOW_ERROR_TYPE_ATTR,
1302 NULL, "No attribute specified");
1306 /* Verify Actions. */
1307 enic_action_cap = enic_get_action_cap(enic);
1308 for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
1310 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1312 else if (!enic_match_action(action, enic_action_cap->actions))
1315 if (action->type != RTE_FLOW_ACTION_TYPE_END) {
1316 rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
1317 action, "Invalid action.");
1320 ret = enic_action_cap->copy_fn(actions, enic_action);
1322 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1323 NULL, "Unsupported action.");
1327 /* Verify Flow items. If copying the filter from flow format to enic
1328 * format fails, the flow is not supported
1330 enic_filter_cap = enic_get_filter_cap(enic);
1331 if (enic_filter_cap == NULL) {
1332 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1333 NULL, "Flow API not available");
1336 enic_filter->type = enic->flow_filter_mode;
1337 ret = enic_copy_filter(pattern, enic_filter_cap->item_info,
1338 enic_filter, error);
1343 * Push filter/action to the NIC.
1346 * Device structure pointer.
1347 * @param enic_filter[in]
1348 * Internal NIC filter structure pointer.
1349 * @param enic_action[in]
1350 * Internal NIC action structure pointer.
1353 static struct rte_flow *
1354 enic_flow_add_filter(struct enic *enic, struct filter_v2 *enic_filter,
1355 struct filter_action_v2 *enic_action,
1356 struct rte_flow_error *error)
1358 struct rte_flow *flow;
1364 flow = rte_calloc(__func__, 1, sizeof(*flow), 0);
1366 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1367 NULL, "cannot allocate flow memory");
1371 /* entry[in] is the queue id, entry[out] is the filter Id for delete */
1372 entry = enic_action->rq_idx;
1373 ret = vnic_dev_classifier(enic->vdev, CLSF_ADD, &entry, enic_filter,
1376 flow->enic_filter_id = entry;
1377 flow->enic_filter = *enic_filter;
1379 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
1380 NULL, "vnic_dev_classifier error");
1388 * Remove filter/action from the NIC.
1391 * Device structure pointer.
1392 * @param filter_id[in]
1394 * @param enic_action[in]
1395 * Internal NIC action structure pointer.
1399 enic_flow_del_filter(struct enic *enic, u16 filter_id,
1400 struct rte_flow_error *error)
1406 ret = vnic_dev_classifier(enic->vdev, CLSF_DEL, &filter_id, NULL, NULL);
1408 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
1409 NULL, "vnic_dev_classifier failed");
1414 * The following functions are callbacks for Generic flow API.
1418 * Validate a flow supported by the NIC.
1420 * @see rte_flow_validate()
1424 enic_flow_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attrs,
1425 const struct rte_flow_item pattern[],
1426 const struct rte_flow_action actions[],
1427 struct rte_flow_error *error)
1429 struct filter_v2 enic_filter;
1430 struct filter_action_v2 enic_action;
1435 ret = enic_flow_parse(dev, attrs, pattern, actions, error,
1436 &enic_filter, &enic_action);
1438 enic_dump_flow(&enic_action, &enic_filter);
1443 * Create a flow supported by the NIC.
1445 * @see rte_flow_create()
1448 static struct rte_flow *
1449 enic_flow_create(struct rte_eth_dev *dev,
1450 const struct rte_flow_attr *attrs,
1451 const struct rte_flow_item pattern[],
1452 const struct rte_flow_action actions[],
1453 struct rte_flow_error *error)
1456 struct filter_v2 enic_filter;
1457 struct filter_action_v2 enic_action;
1458 struct rte_flow *flow;
1459 struct enic *enic = pmd_priv(dev);
1463 ret = enic_flow_parse(dev, attrs, pattern, actions, error, &enic_filter,
1468 rte_spinlock_lock(&enic->flows_lock);
1469 flow = enic_flow_add_filter(enic, &enic_filter, &enic_action,
1472 LIST_INSERT_HEAD(&enic->flows, flow, next);
1473 rte_spinlock_unlock(&enic->flows_lock);
1479 * Destroy a flow supported by the NIC.
1481 * @see rte_flow_destroy()
1485 enic_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
1486 __rte_unused struct rte_flow_error *error)
1488 struct enic *enic = pmd_priv(dev);
1492 rte_spinlock_lock(&enic->flows_lock);
1493 enic_flow_del_filter(enic, flow->enic_filter_id, error);
1494 LIST_REMOVE(flow, next);
1495 rte_spinlock_unlock(&enic->flows_lock);
1500 * Flush all flows on the device.
1502 * @see rte_flow_flush()
1506 enic_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1508 struct rte_flow *flow;
1509 struct enic *enic = pmd_priv(dev);
1513 rte_spinlock_lock(&enic->flows_lock);
1515 while (!LIST_EMPTY(&enic->flows)) {
1516 flow = LIST_FIRST(&enic->flows);
1517 enic_flow_del_filter(enic, flow->enic_filter_id, error);
1518 LIST_REMOVE(flow, next);
1520 rte_spinlock_unlock(&enic->flows_lock);
1525 * Flow callback registration.
1529 const struct rte_flow_ops enic_flow_ops = {
1530 .validate = enic_flow_validate,
1531 .create = enic_flow_create,
1532 .destroy = enic_flow_destroy,
1533 .flush = enic_flow_flush,