1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2008-2017 Cisco Systems, Inc. All rights reserved.
8 #include <rte_ethdev_driver.h>
9 #include <rte_flow_driver.h>
10 #include <rte_ether.h>
14 #include "enic_compat.h"
19 #define FLOW_TRACE() \
20 rte_log(RTE_LOG_DEBUG, enicpmd_logtype_flow, \
22 #define FLOW_LOG(level, fmt, args...) \
23 rte_log(RTE_LOG_ ## level, enicpmd_logtype_flow, \
27 * Common arguments passed to copy_item functions. Use this structure
28 * so we can easily add new arguments.
29 * item: Item specification.
30 * filter: Partially filled in NIC filter structure.
31 * inner_ofst: If zero, this is an outer header. If non-zero, this is
32 * the offset into L5 where the header begins.
33 * l2_proto_off: offset to EtherType eth or vlan header.
34 * l3_proto_off: offset to next protocol field in IPv4 or 6 header.
36 struct copy_item_args {
37 const struct rte_flow_item *item;
38 struct filter_v2 *filter;
44 /* functions for copying items into enic filters */
45 typedef int (enic_copy_item_fn)(struct copy_item_args *arg);
47 /** Info about how to copy items into enic filters. */
49 /** Function for copying and validating an item. */
50 enic_copy_item_fn *copy_item;
51 /** List of valid previous items. */
52 const enum rte_flow_item_type * const prev_items;
53 /** True if it's OK for this item to be the first item. For some NIC
54 * versions, it's invalid to start the stack above layer 3.
56 const u8 valid_start_item;
57 /* Inner packet version of copy_item. */
58 enic_copy_item_fn *inner_copy_item;
61 /** Filtering capabilities for various NIC and firmware versions. */
62 struct enic_filter_cap {
63 /** list of valid items and their handlers and attributes. */
64 const struct enic_items *item_info;
65 /* Max type in the above list, used to detect unsupported types */
66 enum rte_flow_item_type max_item_type;
69 /* functions for copying flow actions into enic actions */
70 typedef int (copy_action_fn)(struct enic *enic,
71 const struct rte_flow_action actions[],
72 struct filter_action_v2 *enic_action);
74 /** Action capabilities for various NICs. */
75 struct enic_action_cap {
76 /** list of valid actions */
77 const enum rte_flow_action_type *actions;
78 /** copy function for a particular NIC */
79 copy_action_fn *copy_fn;
82 /* Forward declarations */
83 static enic_copy_item_fn enic_copy_item_ipv4_v1;
84 static enic_copy_item_fn enic_copy_item_udp_v1;
85 static enic_copy_item_fn enic_copy_item_tcp_v1;
86 static enic_copy_item_fn enic_copy_item_raw_v2;
87 static enic_copy_item_fn enic_copy_item_eth_v2;
88 static enic_copy_item_fn enic_copy_item_vlan_v2;
89 static enic_copy_item_fn enic_copy_item_ipv4_v2;
90 static enic_copy_item_fn enic_copy_item_ipv6_v2;
91 static enic_copy_item_fn enic_copy_item_udp_v2;
92 static enic_copy_item_fn enic_copy_item_tcp_v2;
93 static enic_copy_item_fn enic_copy_item_sctp_v2;
94 static enic_copy_item_fn enic_copy_item_vxlan_v2;
95 static enic_copy_item_fn enic_copy_item_inner_eth_v2;
96 static enic_copy_item_fn enic_copy_item_inner_vlan_v2;
97 static enic_copy_item_fn enic_copy_item_inner_ipv4_v2;
98 static enic_copy_item_fn enic_copy_item_inner_ipv6_v2;
99 static enic_copy_item_fn enic_copy_item_inner_udp_v2;
100 static enic_copy_item_fn enic_copy_item_inner_tcp_v2;
101 static copy_action_fn enic_copy_action_v1;
102 static copy_action_fn enic_copy_action_v2;
105 * Legacy NICs or NICs with outdated firmware. Only 5-tuple perfect match
108 static const struct enic_items enic_items_v1[] = {
109 [RTE_FLOW_ITEM_TYPE_IPV4] = {
110 .copy_item = enic_copy_item_ipv4_v1,
111 .valid_start_item = 1,
112 .prev_items = (const enum rte_flow_item_type[]) {
113 RTE_FLOW_ITEM_TYPE_END,
115 .inner_copy_item = NULL,
117 [RTE_FLOW_ITEM_TYPE_UDP] = {
118 .copy_item = enic_copy_item_udp_v1,
119 .valid_start_item = 0,
120 .prev_items = (const enum rte_flow_item_type[]) {
121 RTE_FLOW_ITEM_TYPE_IPV4,
122 RTE_FLOW_ITEM_TYPE_END,
124 .inner_copy_item = NULL,
126 [RTE_FLOW_ITEM_TYPE_TCP] = {
127 .copy_item = enic_copy_item_tcp_v1,
128 .valid_start_item = 0,
129 .prev_items = (const enum rte_flow_item_type[]) {
130 RTE_FLOW_ITEM_TYPE_IPV4,
131 RTE_FLOW_ITEM_TYPE_END,
133 .inner_copy_item = NULL,
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_RAW] = {
143 .copy_item = enic_copy_item_raw_v2,
144 .valid_start_item = 0,
145 .prev_items = (const enum rte_flow_item_type[]) {
146 RTE_FLOW_ITEM_TYPE_UDP,
147 RTE_FLOW_ITEM_TYPE_END,
149 .inner_copy_item = NULL,
151 [RTE_FLOW_ITEM_TYPE_ETH] = {
152 .copy_item = enic_copy_item_eth_v2,
153 .valid_start_item = 1,
154 .prev_items = (const enum rte_flow_item_type[]) {
155 RTE_FLOW_ITEM_TYPE_VXLAN,
156 RTE_FLOW_ITEM_TYPE_END,
158 .inner_copy_item = enic_copy_item_inner_eth_v2,
160 [RTE_FLOW_ITEM_TYPE_VLAN] = {
161 .copy_item = enic_copy_item_vlan_v2,
162 .valid_start_item = 1,
163 .prev_items = (const enum rte_flow_item_type[]) {
164 RTE_FLOW_ITEM_TYPE_ETH,
165 RTE_FLOW_ITEM_TYPE_END,
167 .inner_copy_item = enic_copy_item_inner_vlan_v2,
169 [RTE_FLOW_ITEM_TYPE_IPV4] = {
170 .copy_item = enic_copy_item_ipv4_v2,
171 .valid_start_item = 1,
172 .prev_items = (const enum rte_flow_item_type[]) {
173 RTE_FLOW_ITEM_TYPE_ETH,
174 RTE_FLOW_ITEM_TYPE_VLAN,
175 RTE_FLOW_ITEM_TYPE_END,
177 .inner_copy_item = enic_copy_item_inner_ipv4_v2,
179 [RTE_FLOW_ITEM_TYPE_IPV6] = {
180 .copy_item = enic_copy_item_ipv6_v2,
181 .valid_start_item = 1,
182 .prev_items = (const enum rte_flow_item_type[]) {
183 RTE_FLOW_ITEM_TYPE_ETH,
184 RTE_FLOW_ITEM_TYPE_VLAN,
185 RTE_FLOW_ITEM_TYPE_END,
187 .inner_copy_item = enic_copy_item_inner_ipv6_v2,
189 [RTE_FLOW_ITEM_TYPE_UDP] = {
190 .copy_item = enic_copy_item_udp_v2,
191 .valid_start_item = 0,
192 .prev_items = (const enum rte_flow_item_type[]) {
193 RTE_FLOW_ITEM_TYPE_IPV4,
194 RTE_FLOW_ITEM_TYPE_IPV6,
195 RTE_FLOW_ITEM_TYPE_END,
197 .inner_copy_item = enic_copy_item_inner_udp_v2,
199 [RTE_FLOW_ITEM_TYPE_TCP] = {
200 .copy_item = enic_copy_item_tcp_v2,
201 .valid_start_item = 0,
202 .prev_items = (const enum rte_flow_item_type[]) {
203 RTE_FLOW_ITEM_TYPE_IPV4,
204 RTE_FLOW_ITEM_TYPE_IPV6,
205 RTE_FLOW_ITEM_TYPE_END,
207 .inner_copy_item = enic_copy_item_inner_tcp_v2,
209 [RTE_FLOW_ITEM_TYPE_SCTP] = {
210 .copy_item = enic_copy_item_sctp_v2,
211 .valid_start_item = 0,
212 .prev_items = (const enum rte_flow_item_type[]) {
213 RTE_FLOW_ITEM_TYPE_IPV4,
214 RTE_FLOW_ITEM_TYPE_IPV6,
215 RTE_FLOW_ITEM_TYPE_END,
217 .inner_copy_item = NULL,
219 [RTE_FLOW_ITEM_TYPE_VXLAN] = {
220 .copy_item = enic_copy_item_vxlan_v2,
221 .valid_start_item = 0,
222 .prev_items = (const enum rte_flow_item_type[]) {
223 RTE_FLOW_ITEM_TYPE_UDP,
224 RTE_FLOW_ITEM_TYPE_END,
226 .inner_copy_item = NULL,
230 /** NICs with Advanced filters enabled */
231 static const struct enic_items enic_items_v3[] = {
232 [RTE_FLOW_ITEM_TYPE_RAW] = {
233 .copy_item = enic_copy_item_raw_v2,
234 .valid_start_item = 0,
235 .prev_items = (const enum rte_flow_item_type[]) {
236 RTE_FLOW_ITEM_TYPE_UDP,
237 RTE_FLOW_ITEM_TYPE_END,
239 .inner_copy_item = NULL,
241 [RTE_FLOW_ITEM_TYPE_ETH] = {
242 .copy_item = enic_copy_item_eth_v2,
243 .valid_start_item = 1,
244 .prev_items = (const enum rte_flow_item_type[]) {
245 RTE_FLOW_ITEM_TYPE_VXLAN,
246 RTE_FLOW_ITEM_TYPE_END,
248 .inner_copy_item = enic_copy_item_inner_eth_v2,
250 [RTE_FLOW_ITEM_TYPE_VLAN] = {
251 .copy_item = enic_copy_item_vlan_v2,
252 .valid_start_item = 1,
253 .prev_items = (const enum rte_flow_item_type[]) {
254 RTE_FLOW_ITEM_TYPE_ETH,
255 RTE_FLOW_ITEM_TYPE_END,
257 .inner_copy_item = enic_copy_item_inner_vlan_v2,
259 [RTE_FLOW_ITEM_TYPE_IPV4] = {
260 .copy_item = enic_copy_item_ipv4_v2,
261 .valid_start_item = 1,
262 .prev_items = (const enum rte_flow_item_type[]) {
263 RTE_FLOW_ITEM_TYPE_ETH,
264 RTE_FLOW_ITEM_TYPE_VLAN,
265 RTE_FLOW_ITEM_TYPE_END,
267 .inner_copy_item = enic_copy_item_inner_ipv4_v2,
269 [RTE_FLOW_ITEM_TYPE_IPV6] = {
270 .copy_item = enic_copy_item_ipv6_v2,
271 .valid_start_item = 1,
272 .prev_items = (const enum rte_flow_item_type[]) {
273 RTE_FLOW_ITEM_TYPE_ETH,
274 RTE_FLOW_ITEM_TYPE_VLAN,
275 RTE_FLOW_ITEM_TYPE_END,
277 .inner_copy_item = enic_copy_item_inner_ipv6_v2,
279 [RTE_FLOW_ITEM_TYPE_UDP] = {
280 .copy_item = enic_copy_item_udp_v2,
281 .valid_start_item = 1,
282 .prev_items = (const enum rte_flow_item_type[]) {
283 RTE_FLOW_ITEM_TYPE_IPV4,
284 RTE_FLOW_ITEM_TYPE_IPV6,
285 RTE_FLOW_ITEM_TYPE_END,
287 .inner_copy_item = enic_copy_item_inner_udp_v2,
289 [RTE_FLOW_ITEM_TYPE_TCP] = {
290 .copy_item = enic_copy_item_tcp_v2,
291 .valid_start_item = 1,
292 .prev_items = (const enum rte_flow_item_type[]) {
293 RTE_FLOW_ITEM_TYPE_IPV4,
294 RTE_FLOW_ITEM_TYPE_IPV6,
295 RTE_FLOW_ITEM_TYPE_END,
297 .inner_copy_item = enic_copy_item_inner_tcp_v2,
299 [RTE_FLOW_ITEM_TYPE_SCTP] = {
300 .copy_item = enic_copy_item_sctp_v2,
301 .valid_start_item = 0,
302 .prev_items = (const enum rte_flow_item_type[]) {
303 RTE_FLOW_ITEM_TYPE_IPV4,
304 RTE_FLOW_ITEM_TYPE_IPV6,
305 RTE_FLOW_ITEM_TYPE_END,
307 .inner_copy_item = NULL,
309 [RTE_FLOW_ITEM_TYPE_VXLAN] = {
310 .copy_item = enic_copy_item_vxlan_v2,
311 .valid_start_item = 1,
312 .prev_items = (const enum rte_flow_item_type[]) {
313 RTE_FLOW_ITEM_TYPE_UDP,
314 RTE_FLOW_ITEM_TYPE_END,
316 .inner_copy_item = NULL,
320 /** Filtering capabilities indexed this NICs supported filter type. */
321 static const struct enic_filter_cap enic_filter_cap[] = {
322 [FILTER_IPV4_5TUPLE] = {
323 .item_info = enic_items_v1,
324 .max_item_type = RTE_FLOW_ITEM_TYPE_TCP,
326 [FILTER_USNIC_IP] = {
327 .item_info = enic_items_v2,
328 .max_item_type = RTE_FLOW_ITEM_TYPE_VXLAN,
331 .item_info = enic_items_v3,
332 .max_item_type = RTE_FLOW_ITEM_TYPE_VXLAN,
336 /** Supported actions for older NICs */
337 static const enum rte_flow_action_type enic_supported_actions_v1[] = {
338 RTE_FLOW_ACTION_TYPE_QUEUE,
339 RTE_FLOW_ACTION_TYPE_END,
342 /** Supported actions for newer NICs */
343 static const enum rte_flow_action_type enic_supported_actions_v2_id[] = {
344 RTE_FLOW_ACTION_TYPE_QUEUE,
345 RTE_FLOW_ACTION_TYPE_MARK,
346 RTE_FLOW_ACTION_TYPE_FLAG,
347 RTE_FLOW_ACTION_TYPE_RSS,
348 RTE_FLOW_ACTION_TYPE_PASSTHRU,
349 RTE_FLOW_ACTION_TYPE_END,
352 static const enum rte_flow_action_type enic_supported_actions_v2_drop[] = {
353 RTE_FLOW_ACTION_TYPE_QUEUE,
354 RTE_FLOW_ACTION_TYPE_MARK,
355 RTE_FLOW_ACTION_TYPE_FLAG,
356 RTE_FLOW_ACTION_TYPE_DROP,
357 RTE_FLOW_ACTION_TYPE_RSS,
358 RTE_FLOW_ACTION_TYPE_PASSTHRU,
359 RTE_FLOW_ACTION_TYPE_END,
362 static const enum rte_flow_action_type enic_supported_actions_v2_count[] = {
363 RTE_FLOW_ACTION_TYPE_QUEUE,
364 RTE_FLOW_ACTION_TYPE_MARK,
365 RTE_FLOW_ACTION_TYPE_FLAG,
366 RTE_FLOW_ACTION_TYPE_DROP,
367 RTE_FLOW_ACTION_TYPE_COUNT,
368 RTE_FLOW_ACTION_TYPE_RSS,
369 RTE_FLOW_ACTION_TYPE_PASSTHRU,
370 RTE_FLOW_ACTION_TYPE_END,
373 /** Action capabilities indexed by NIC version information */
374 static const struct enic_action_cap enic_action_cap[] = {
375 [FILTER_ACTION_RQ_STEERING_FLAG] = {
376 .actions = enic_supported_actions_v1,
377 .copy_fn = enic_copy_action_v1,
379 [FILTER_ACTION_FILTER_ID_FLAG] = {
380 .actions = enic_supported_actions_v2_id,
381 .copy_fn = enic_copy_action_v2,
383 [FILTER_ACTION_DROP_FLAG] = {
384 .actions = enic_supported_actions_v2_drop,
385 .copy_fn = enic_copy_action_v2,
387 [FILTER_ACTION_COUNTER_FLAG] = {
388 .actions = enic_supported_actions_v2_count,
389 .copy_fn = enic_copy_action_v2,
394 mask_exact_match(const u8 *supported, const u8 *supplied,
398 for (i = 0; i < size; i++) {
399 if (supported[i] != supplied[i])
406 enic_copy_item_ipv4_v1(struct copy_item_args *arg)
408 const struct rte_flow_item *item = arg->item;
409 struct filter_v2 *enic_filter = arg->filter;
410 const struct rte_flow_item_ipv4 *spec = item->spec;
411 const struct rte_flow_item_ipv4 *mask = item->mask;
412 struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
413 struct ipv4_hdr supported_mask = {
414 .src_addr = 0xffffffff,
415 .dst_addr = 0xffffffff,
421 mask = &rte_flow_item_ipv4_mask;
423 /* This is an exact match filter, both fields must be set */
424 if (!spec || !spec->hdr.src_addr || !spec->hdr.dst_addr) {
425 FLOW_LOG(ERR, "IPv4 exact match src/dst addr");
429 /* check that the suppied mask exactly matches capabilty */
430 if (!mask_exact_match((const u8 *)&supported_mask,
431 (const u8 *)item->mask, sizeof(*mask))) {
432 FLOW_LOG(ERR, "IPv4 exact match mask");
436 enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
437 enic_5tup->src_addr = spec->hdr.src_addr;
438 enic_5tup->dst_addr = spec->hdr.dst_addr;
444 enic_copy_item_udp_v1(struct copy_item_args *arg)
446 const struct rte_flow_item *item = arg->item;
447 struct filter_v2 *enic_filter = arg->filter;
448 const struct rte_flow_item_udp *spec = item->spec;
449 const struct rte_flow_item_udp *mask = item->mask;
450 struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
451 struct udp_hdr supported_mask = {
459 mask = &rte_flow_item_udp_mask;
461 /* This is an exact match filter, both ports must be set */
462 if (!spec || !spec->hdr.src_port || !spec->hdr.dst_port) {
463 FLOW_LOG(ERR, "UDP exact match src/dst addr");
467 /* check that the suppied mask exactly matches capabilty */
468 if (!mask_exact_match((const u8 *)&supported_mask,
469 (const u8 *)item->mask, sizeof(*mask))) {
470 FLOW_LOG(ERR, "UDP exact match mask");
474 enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
475 enic_5tup->src_port = spec->hdr.src_port;
476 enic_5tup->dst_port = spec->hdr.dst_port;
477 enic_5tup->protocol = PROTO_UDP;
483 enic_copy_item_tcp_v1(struct copy_item_args *arg)
485 const struct rte_flow_item *item = arg->item;
486 struct filter_v2 *enic_filter = arg->filter;
487 const struct rte_flow_item_tcp *spec = item->spec;
488 const struct rte_flow_item_tcp *mask = item->mask;
489 struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
490 struct tcp_hdr supported_mask = {
498 mask = &rte_flow_item_tcp_mask;
500 /* This is an exact match filter, both ports must be set */
501 if (!spec || !spec->hdr.src_port || !spec->hdr.dst_port) {
502 FLOW_LOG(ERR, "TCPIPv4 exact match src/dst addr");
506 /* check that the suppied mask exactly matches capabilty */
507 if (!mask_exact_match((const u8 *)&supported_mask,
508 (const u8 *)item->mask, sizeof(*mask))) {
509 FLOW_LOG(ERR, "TCP exact match mask");
513 enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
514 enic_5tup->src_port = spec->hdr.src_port;
515 enic_5tup->dst_port = spec->hdr.dst_port;
516 enic_5tup->protocol = PROTO_TCP;
522 * The common 'copy' function for all inner packet patterns. Patterns are
523 * first appended to the L5 pattern buffer. Then, since the NIC filter
524 * API has no special support for inner packet matching at the moment,
525 * we set EtherType and IP proto as necessary.
528 copy_inner_common(struct filter_generic_1 *gp, uint8_t *inner_ofst,
529 const void *val, const void *mask, uint8_t val_size,
530 uint8_t proto_off, uint16_t proto_val, uint8_t proto_size)
532 uint8_t *l5_mask, *l5_val;
535 /* No space left in the L5 pattern buffer. */
536 start_off = *inner_ofst;
537 if ((start_off + val_size) > FILTER_GENERIC_1_KEY_LEN)
539 l5_mask = gp->layer[FILTER_GENERIC_1_L5].mask;
540 l5_val = gp->layer[FILTER_GENERIC_1_L5].val;
541 /* Copy the pattern into the L5 buffer. */
543 memcpy(l5_mask + start_off, mask, val_size);
544 memcpy(l5_val + start_off, val, val_size);
546 /* Set the protocol field in the previous header. */
550 m = l5_mask + proto_off;
551 v = l5_val + proto_off;
552 if (proto_size == 1) {
553 *(uint8_t *)m = 0xff;
554 *(uint8_t *)v = (uint8_t)proto_val;
555 } else if (proto_size == 2) {
556 *(uint16_t *)m = 0xffff;
557 *(uint16_t *)v = proto_val;
560 /* All inner headers land in L5 buffer even if their spec is null. */
561 *inner_ofst += val_size;
566 enic_copy_item_inner_eth_v2(struct copy_item_args *arg)
568 const void *mask = arg->item->mask;
569 uint8_t *off = arg->inner_ofst;
573 mask = &rte_flow_item_eth_mask;
574 arg->l2_proto_off = *off + offsetof(struct ether_hdr, ether_type);
575 return copy_inner_common(&arg->filter->u.generic_1, off,
576 arg->item->spec, mask, sizeof(struct ether_hdr),
577 0 /* no previous protocol */, 0, 0);
581 enic_copy_item_inner_vlan_v2(struct copy_item_args *arg)
583 const void *mask = arg->item->mask;
584 uint8_t *off = arg->inner_ofst;
585 uint8_t eth_type_off;
589 mask = &rte_flow_item_vlan_mask;
590 /* Append vlan header to L5 and set ether type = TPID */
591 eth_type_off = arg->l2_proto_off;
592 arg->l2_proto_off = *off + offsetof(struct vlan_hdr, eth_proto);
593 return copy_inner_common(&arg->filter->u.generic_1, off,
594 arg->item->spec, mask, sizeof(struct vlan_hdr),
595 eth_type_off, rte_cpu_to_be_16(ETHER_TYPE_VLAN), 2);
599 enic_copy_item_inner_ipv4_v2(struct copy_item_args *arg)
601 const void *mask = arg->item->mask;
602 uint8_t *off = arg->inner_ofst;
606 mask = &rte_flow_item_ipv4_mask;
607 /* Append ipv4 header to L5 and set ether type = ipv4 */
608 arg->l3_proto_off = *off + offsetof(struct ipv4_hdr, next_proto_id);
609 return copy_inner_common(&arg->filter->u.generic_1, off,
610 arg->item->spec, mask, sizeof(struct ipv4_hdr),
611 arg->l2_proto_off, rte_cpu_to_be_16(ETHER_TYPE_IPv4), 2);
615 enic_copy_item_inner_ipv6_v2(struct copy_item_args *arg)
617 const void *mask = arg->item->mask;
618 uint8_t *off = arg->inner_ofst;
622 mask = &rte_flow_item_ipv6_mask;
623 /* Append ipv6 header to L5 and set ether type = ipv6 */
624 arg->l3_proto_off = *off + offsetof(struct ipv6_hdr, proto);
625 return copy_inner_common(&arg->filter->u.generic_1, off,
626 arg->item->spec, mask, sizeof(struct ipv6_hdr),
627 arg->l2_proto_off, rte_cpu_to_be_16(ETHER_TYPE_IPv6), 2);
631 enic_copy_item_inner_udp_v2(struct copy_item_args *arg)
633 const void *mask = arg->item->mask;
634 uint8_t *off = arg->inner_ofst;
638 mask = &rte_flow_item_udp_mask;
639 /* Append udp header to L5 and set ip proto = udp */
640 return copy_inner_common(&arg->filter->u.generic_1, off,
641 arg->item->spec, mask, sizeof(struct udp_hdr),
642 arg->l3_proto_off, IPPROTO_UDP, 1);
646 enic_copy_item_inner_tcp_v2(struct copy_item_args *arg)
648 const void *mask = arg->item->mask;
649 uint8_t *off = arg->inner_ofst;
653 mask = &rte_flow_item_tcp_mask;
654 /* Append tcp header to L5 and set ip proto = tcp */
655 return copy_inner_common(&arg->filter->u.generic_1, off,
656 arg->item->spec, mask, sizeof(struct tcp_hdr),
657 arg->l3_proto_off, IPPROTO_TCP, 1);
661 enic_copy_item_eth_v2(struct copy_item_args *arg)
663 const struct rte_flow_item *item = arg->item;
664 struct filter_v2 *enic_filter = arg->filter;
665 struct ether_hdr enic_spec;
666 struct ether_hdr enic_mask;
667 const struct rte_flow_item_eth *spec = item->spec;
668 const struct rte_flow_item_eth *mask = item->mask;
669 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
673 /* Match all if no spec */
678 mask = &rte_flow_item_eth_mask;
680 memcpy(enic_spec.d_addr.addr_bytes, spec->dst.addr_bytes,
682 memcpy(enic_spec.s_addr.addr_bytes, spec->src.addr_bytes,
685 memcpy(enic_mask.d_addr.addr_bytes, mask->dst.addr_bytes,
687 memcpy(enic_mask.s_addr.addr_bytes, mask->src.addr_bytes,
689 enic_spec.ether_type = spec->type;
690 enic_mask.ether_type = mask->type;
693 memcpy(gp->layer[FILTER_GENERIC_1_L2].mask, &enic_mask,
694 sizeof(struct ether_hdr));
695 memcpy(gp->layer[FILTER_GENERIC_1_L2].val, &enic_spec,
696 sizeof(struct ether_hdr));
701 enic_copy_item_vlan_v2(struct copy_item_args *arg)
703 const struct rte_flow_item *item = arg->item;
704 struct filter_v2 *enic_filter = arg->filter;
705 const struct rte_flow_item_vlan *spec = item->spec;
706 const struct rte_flow_item_vlan *mask = item->mask;
707 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
708 struct ether_hdr *eth_mask;
709 struct ether_hdr *eth_val;
713 /* Match all if no spec */
718 mask = &rte_flow_item_vlan_mask;
720 eth_mask = (void *)gp->layer[FILTER_GENERIC_1_L2].mask;
721 eth_val = (void *)gp->layer[FILTER_GENERIC_1_L2].val;
722 /* Outer TPID cannot be matched */
723 if (eth_mask->ether_type)
726 * When packet matching, the VIC always compares vlan-stripped
727 * L2, regardless of vlan stripping settings. So, the inner type
728 * from vlan becomes the ether type of the eth header.
730 eth_mask->ether_type = mask->inner_type;
731 eth_val->ether_type = spec->inner_type;
732 /* For TCI, use the vlan mask/val fields (little endian). */
733 gp->mask_vlan = rte_be_to_cpu_16(mask->tci);
734 gp->val_vlan = rte_be_to_cpu_16(spec->tci);
739 enic_copy_item_ipv4_v2(struct copy_item_args *arg)
741 const struct rte_flow_item *item = arg->item;
742 struct filter_v2 *enic_filter = arg->filter;
743 const struct rte_flow_item_ipv4 *spec = item->spec;
744 const struct rte_flow_item_ipv4 *mask = item->mask;
745 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
750 gp->mask_flags |= FILTER_GENERIC_1_IPV4;
751 gp->val_flags |= FILTER_GENERIC_1_IPV4;
753 /* Match all if no spec */
758 mask = &rte_flow_item_ipv4_mask;
760 memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
761 sizeof(struct ipv4_hdr));
762 memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
763 sizeof(struct ipv4_hdr));
768 enic_copy_item_ipv6_v2(struct copy_item_args *arg)
770 const struct rte_flow_item *item = arg->item;
771 struct filter_v2 *enic_filter = arg->filter;
772 const struct rte_flow_item_ipv6 *spec = item->spec;
773 const struct rte_flow_item_ipv6 *mask = item->mask;
774 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
779 gp->mask_flags |= FILTER_GENERIC_1_IPV6;
780 gp->val_flags |= FILTER_GENERIC_1_IPV6;
782 /* Match all if no spec */
787 mask = &rte_flow_item_ipv6_mask;
789 memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
790 sizeof(struct ipv6_hdr));
791 memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
792 sizeof(struct ipv6_hdr));
797 enic_copy_item_udp_v2(struct copy_item_args *arg)
799 const struct rte_flow_item *item = arg->item;
800 struct filter_v2 *enic_filter = arg->filter;
801 const struct rte_flow_item_udp *spec = item->spec;
802 const struct rte_flow_item_udp *mask = item->mask;
803 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
808 gp->mask_flags |= FILTER_GENERIC_1_UDP;
809 gp->val_flags |= FILTER_GENERIC_1_UDP;
811 /* Match all if no spec */
816 mask = &rte_flow_item_udp_mask;
818 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
819 sizeof(struct udp_hdr));
820 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
821 sizeof(struct udp_hdr));
826 enic_copy_item_tcp_v2(struct copy_item_args *arg)
828 const struct rte_flow_item *item = arg->item;
829 struct filter_v2 *enic_filter = arg->filter;
830 const struct rte_flow_item_tcp *spec = item->spec;
831 const struct rte_flow_item_tcp *mask = item->mask;
832 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
837 gp->mask_flags |= FILTER_GENERIC_1_TCP;
838 gp->val_flags |= FILTER_GENERIC_1_TCP;
840 /* Match all if no spec */
847 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
848 sizeof(struct tcp_hdr));
849 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
850 sizeof(struct tcp_hdr));
855 enic_copy_item_sctp_v2(struct copy_item_args *arg)
857 const struct rte_flow_item *item = arg->item;
858 struct filter_v2 *enic_filter = arg->filter;
859 const struct rte_flow_item_sctp *spec = item->spec;
860 const struct rte_flow_item_sctp *mask = item->mask;
861 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
862 uint8_t *ip_proto_mask = NULL;
863 uint8_t *ip_proto = NULL;
868 * The NIC filter API has no flags for "match sctp", so explicitly set
869 * the protocol number in the IP pattern.
871 if (gp->val_flags & FILTER_GENERIC_1_IPV4) {
873 ip = (struct ipv4_hdr *)gp->layer[FILTER_GENERIC_1_L3].mask;
874 ip_proto_mask = &ip->next_proto_id;
875 ip = (struct ipv4_hdr *)gp->layer[FILTER_GENERIC_1_L3].val;
876 ip_proto = &ip->next_proto_id;
877 } else if (gp->val_flags & FILTER_GENERIC_1_IPV6) {
879 ip = (struct ipv6_hdr *)gp->layer[FILTER_GENERIC_1_L3].mask;
880 ip_proto_mask = &ip->proto;
881 ip = (struct ipv6_hdr *)gp->layer[FILTER_GENERIC_1_L3].val;
882 ip_proto = &ip->proto;
884 /* Need IPv4/IPv6 pattern first */
887 *ip_proto = IPPROTO_SCTP;
888 *ip_proto_mask = 0xff;
890 /* Match all if no spec */
895 mask = &rte_flow_item_sctp_mask;
897 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
898 sizeof(struct sctp_hdr));
899 memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
900 sizeof(struct sctp_hdr));
905 enic_copy_item_vxlan_v2(struct copy_item_args *arg)
907 const struct rte_flow_item *item = arg->item;
908 struct filter_v2 *enic_filter = arg->filter;
909 uint8_t *inner_ofst = arg->inner_ofst;
910 const struct rte_flow_item_vxlan *spec = item->spec;
911 const struct rte_flow_item_vxlan *mask = item->mask;
912 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
918 * The NIC filter API has no flags for "match vxlan". Set UDP port to
919 * avoid false positives.
921 gp->mask_flags |= FILTER_GENERIC_1_UDP;
922 gp->val_flags |= FILTER_GENERIC_1_UDP;
923 udp = (struct udp_hdr *)gp->layer[FILTER_GENERIC_1_L4].mask;
924 udp->dst_port = 0xffff;
925 udp = (struct udp_hdr *)gp->layer[FILTER_GENERIC_1_L4].val;
926 udp->dst_port = RTE_BE16(4789);
927 /* Match all if no spec */
932 mask = &rte_flow_item_vxlan_mask;
934 memcpy(gp->layer[FILTER_GENERIC_1_L5].mask, mask,
935 sizeof(struct vxlan_hdr));
936 memcpy(gp->layer[FILTER_GENERIC_1_L5].val, spec,
937 sizeof(struct vxlan_hdr));
939 *inner_ofst = sizeof(struct vxlan_hdr);
944 * Copy raw item into version 2 NIC filter. Currently, raw pattern match is
945 * very limited. It is intended for matching UDP tunnel header (e.g. vxlan
949 enic_copy_item_raw_v2(struct copy_item_args *arg)
951 const struct rte_flow_item *item = arg->item;
952 struct filter_v2 *enic_filter = arg->filter;
953 uint8_t *inner_ofst = arg->inner_ofst;
954 const struct rte_flow_item_raw *spec = item->spec;
955 const struct rte_flow_item_raw *mask = item->mask;
956 struct filter_generic_1 *gp = &enic_filter->u.generic_1;
960 /* Cannot be used for inner packet */
963 /* Need both spec and mask */
966 /* Only supports relative with offset 0 */
967 if (!spec->relative || spec->offset != 0 || spec->search || spec->limit)
969 /* Need non-null pattern that fits within the NIC's filter pattern */
970 if (spec->length == 0 || spec->length > FILTER_GENERIC_1_KEY_LEN ||
971 !spec->pattern || !mask->pattern)
974 * Mask fields, including length, are often set to zero. Assume that
975 * means "same as spec" to avoid breaking existing apps. If length
976 * is not zero, then it should be >= spec length.
978 * No more pattern follows this, so append to the L4 layer instead of
979 * L5 to work with both recent and older VICs.
981 if (mask->length != 0 && mask->length < spec->length)
983 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask + sizeof(struct udp_hdr),
984 mask->pattern, spec->length);
985 memcpy(gp->layer[FILTER_GENERIC_1_L4].val + sizeof(struct udp_hdr),
986 spec->pattern, spec->length);
992 * Return 1 if current item is valid on top of the previous one.
994 * @param prev_item[in]
995 * The item before this one in the pattern or RTE_FLOW_ITEM_TYPE_END if this
997 * @param item_info[in]
998 * Info about this item, like valid previous items.
999 * @param is_first[in]
1000 * True if this the first item in the pattern.
1003 item_stacking_valid(enum rte_flow_item_type prev_item,
1004 const struct enic_items *item_info, u8 is_first_item)
1006 enum rte_flow_item_type const *allowed_items = item_info->prev_items;
1010 for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
1011 if (prev_item == *allowed_items)
1015 /* This is the first item in the stack. Check if that's cool */
1016 if (is_first_item && item_info->valid_start_item)
1023 * Fix up the L5 layer.. HW vxlan parsing removes vxlan header from L5.
1024 * Instead it is in L4 following the UDP header. Append the vxlan
1025 * pattern to L4 (udp) and shift any inner packet pattern in L5.
1028 fixup_l5_layer(struct enic *enic, struct filter_generic_1 *gp,
1031 uint8_t layer[FILTER_GENERIC_1_KEY_LEN];
1035 if (!(inner_ofst > 0 && enic->vxlan))
1038 vxlan = sizeof(struct vxlan_hdr);
1039 memcpy(gp->layer[FILTER_GENERIC_1_L4].mask + sizeof(struct udp_hdr),
1040 gp->layer[FILTER_GENERIC_1_L5].mask, vxlan);
1041 memcpy(gp->layer[FILTER_GENERIC_1_L4].val + sizeof(struct udp_hdr),
1042 gp->layer[FILTER_GENERIC_1_L5].val, vxlan);
1043 inner = inner_ofst - vxlan;
1044 memset(layer, 0, sizeof(layer));
1045 memcpy(layer, gp->layer[FILTER_GENERIC_1_L5].mask + vxlan, inner);
1046 memcpy(gp->layer[FILTER_GENERIC_1_L5].mask, layer, sizeof(layer));
1047 memset(layer, 0, sizeof(layer));
1048 memcpy(layer, gp->layer[FILTER_GENERIC_1_L5].val + vxlan, inner);
1049 memcpy(gp->layer[FILTER_GENERIC_1_L5].val, layer, sizeof(layer));
1053 * Build the intenal enic filter structure from the provided pattern. The
1054 * pattern is validated as the items are copied.
1056 * @param pattern[in]
1057 * @param items_info[in]
1058 * Info about this NICs item support, like valid previous items.
1059 * @param enic_filter[out]
1060 * NIC specfilc filters derived from the pattern.
1064 enic_copy_filter(const struct rte_flow_item pattern[],
1065 const struct enic_filter_cap *cap,
1067 struct filter_v2 *enic_filter,
1068 struct rte_flow_error *error)
1071 const struct rte_flow_item *item = pattern;
1072 u8 inner_ofst = 0; /* If encapsulated, ofst into L5 */
1073 enum rte_flow_item_type prev_item;
1074 const struct enic_items *item_info;
1075 struct copy_item_args args;
1076 enic_copy_item_fn *copy_fn;
1077 u8 is_first_item = 1;
1083 args.filter = enic_filter;
1084 args.inner_ofst = &inner_ofst;
1085 for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1086 /* Get info about how to validate and copy the item. If NULL
1087 * is returned the nic does not support the item.
1089 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
1092 item_info = &cap->item_info[item->type];
1093 if (item->type > cap->max_item_type ||
1094 item_info->copy_item == NULL ||
1095 (inner_ofst > 0 && item_info->inner_copy_item == NULL)) {
1096 rte_flow_error_set(error, ENOTSUP,
1097 RTE_FLOW_ERROR_TYPE_ITEM,
1098 NULL, "Unsupported item.");
1102 /* check to see if item stacking is valid */
1103 if (!item_stacking_valid(prev_item, item_info, is_first_item))
1104 goto stacking_error;
1107 copy_fn = inner_ofst > 0 ? item_info->inner_copy_item :
1108 item_info->copy_item;
1109 ret = copy_fn(&args);
1111 goto item_not_supported;
1112 prev_item = item->type;
1115 fixup_l5_layer(enic, &enic_filter->u.generic_1, inner_ofst);
1120 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_ITEM,
1121 NULL, "enic type error");
1125 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
1126 item, "stacking error");
1131 * Build the intenal version 1 NIC action structure from the provided pattern.
1132 * The pattern is validated as the items are copied.
1134 * @param actions[in]
1135 * @param enic_action[out]
1136 * NIC specfilc actions derived from the actions.
1140 enic_copy_action_v1(__rte_unused struct enic *enic,
1141 const struct rte_flow_action actions[],
1142 struct filter_action_v2 *enic_action)
1145 uint32_t overlap = 0;
1149 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1150 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID)
1153 switch (actions->type) {
1154 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1155 const struct rte_flow_action_queue *queue =
1156 (const struct rte_flow_action_queue *)
1162 enic_action->rq_idx =
1163 enic_rte_rq_idx_to_sop_idx(queue->index);
1171 if (!(overlap & FATE))
1173 enic_action->type = FILTER_ACTION_RQ_STEERING;
1178 * Build the intenal version 2 NIC action structure from the provided pattern.
1179 * The pattern is validated as the items are copied.
1181 * @param actions[in]
1182 * @param enic_action[out]
1183 * NIC specfilc actions derived from the actions.
1187 enic_copy_action_v2(struct enic *enic,
1188 const struct rte_flow_action actions[],
1189 struct filter_action_v2 *enic_action)
1191 enum { FATE = 1, MARK = 2, };
1192 uint32_t overlap = 0;
1193 bool passthru = false;
1197 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1198 switch (actions->type) {
1199 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1200 const struct rte_flow_action_queue *queue =
1201 (const struct rte_flow_action_queue *)
1207 enic_action->rq_idx =
1208 enic_rte_rq_idx_to_sop_idx(queue->index);
1209 enic_action->flags |= FILTER_ACTION_RQ_STEERING_FLAG;
1212 case RTE_FLOW_ACTION_TYPE_MARK: {
1213 const struct rte_flow_action_mark *mark =
1214 (const struct rte_flow_action_mark *)
1221 * Map mark ID (32-bit) to filter ID (16-bit):
1222 * - Reject values > 16 bits
1223 * - Filter ID 0 is reserved for filters that steer
1224 * but not mark. So add 1 to the mark ID to avoid
1226 * - Filter ID (ENIC_MAGIC_FILTER_ID = 0xffff) is
1227 * reserved for the "flag" action below.
1229 if (mark->id >= ENIC_MAGIC_FILTER_ID - 1)
1231 enic_action->filter_id = mark->id + 1;
1232 enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1235 case RTE_FLOW_ACTION_TYPE_FLAG: {
1239 /* ENIC_MAGIC_FILTER_ID is reserved for flagging */
1240 enic_action->filter_id = ENIC_MAGIC_FILTER_ID;
1241 enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
1244 case RTE_FLOW_ACTION_TYPE_DROP: {
1248 enic_action->flags |= FILTER_ACTION_DROP_FLAG;
1251 case RTE_FLOW_ACTION_TYPE_COUNT: {
1252 enic_action->flags |= FILTER_ACTION_COUNTER_FLAG;
1255 case RTE_FLOW_ACTION_TYPE_RSS: {
1256 const struct rte_flow_action_rss *rss =
1257 (const struct rte_flow_action_rss *)
1263 * Hardware does not support general RSS actions, but
1264 * we can still support the dummy one that is used to
1265 * "receive normally".
1267 allow = rss->func == RTE_ETH_HASH_FUNCTION_DEFAULT &&
1270 rss->types == enic->rss_hf) &&
1271 rss->queue_num == enic->rq_count &&
1273 /* Identity queue map is ok */
1274 for (i = 0; i < rss->queue_num; i++)
1275 allow = allow && (i == rss->queue[i]);
1280 /* Need MARK or FLAG */
1281 if (!(overlap & MARK))
1286 case RTE_FLOW_ACTION_TYPE_PASSTHRU: {
1288 * Like RSS above, PASSTHRU + MARK may be used to
1289 * "mark and then receive normally". MARK usually comes
1290 * after PASSTHRU, so remember we have seen passthru
1291 * and check for mark later.
1299 case RTE_FLOW_ACTION_TYPE_VOID:
1306 /* Only PASSTHRU + MARK is allowed */
1307 if (passthru && !(overlap & MARK))
1309 if (!(overlap & FATE))
1311 enic_action->type = FILTER_ACTION_V2;
1315 /** Check if the action is supported */
1317 enic_match_action(const struct rte_flow_action *action,
1318 const enum rte_flow_action_type *supported_actions)
1320 for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
1321 supported_actions++) {
1322 if (action->type == *supported_actions)
1328 /** Get the NIC filter capabilties structure */
1329 static const struct enic_filter_cap *
1330 enic_get_filter_cap(struct enic *enic)
1332 if (enic->flow_filter_mode)
1333 return &enic_filter_cap[enic->flow_filter_mode];
1338 /** Get the actions for this NIC version. */
1339 static const struct enic_action_cap *
1340 enic_get_action_cap(struct enic *enic)
1342 const struct enic_action_cap *ea;
1345 actions = enic->filter_actions;
1346 if (actions & FILTER_ACTION_COUNTER_FLAG)
1347 ea = &enic_action_cap[FILTER_ACTION_COUNTER_FLAG];
1348 else if (actions & FILTER_ACTION_DROP_FLAG)
1349 ea = &enic_action_cap[FILTER_ACTION_DROP_FLAG];
1350 else if (actions & FILTER_ACTION_FILTER_ID_FLAG)
1351 ea = &enic_action_cap[FILTER_ACTION_FILTER_ID_FLAG];
1353 ea = &enic_action_cap[FILTER_ACTION_RQ_STEERING_FLAG];
1357 /* Debug function to dump internal NIC action structure. */
1359 enic_dump_actions(const struct filter_action_v2 *ea)
1361 if (ea->type == FILTER_ACTION_RQ_STEERING) {
1362 FLOW_LOG(INFO, "Action(V1), queue: %u\n", ea->rq_idx);
1363 } else if (ea->type == FILTER_ACTION_V2) {
1364 FLOW_LOG(INFO, "Actions(V2)\n");
1365 if (ea->flags & FILTER_ACTION_RQ_STEERING_FLAG)
1366 FLOW_LOG(INFO, "\tqueue: %u\n",
1367 enic_sop_rq_idx_to_rte_idx(ea->rq_idx));
1368 if (ea->flags & FILTER_ACTION_FILTER_ID_FLAG)
1369 FLOW_LOG(INFO, "\tfilter_id: %u\n", ea->filter_id);
1373 /* Debug function to dump internal NIC filter structure. */
1375 enic_dump_filter(const struct filter_v2 *filt)
1377 const struct filter_generic_1 *gp;
1380 char ip4[16], ip6[16], udp[16], tcp[16], tcpudp[16], ip4csum[16];
1381 char l4csum[16], ipfrag[16];
1383 switch (filt->type) {
1384 case FILTER_IPV4_5TUPLE:
1385 FLOW_LOG(INFO, "FILTER_IPV4_5TUPLE\n");
1387 case FILTER_USNIC_IP:
1389 /* FIXME: this should be a loop */
1390 gp = &filt->u.generic_1;
1391 FLOW_LOG(INFO, "Filter: vlan: 0x%04x, mask: 0x%04x\n",
1392 gp->val_vlan, gp->mask_vlan);
1394 if (gp->mask_flags & FILTER_GENERIC_1_IPV4)
1396 (gp->val_flags & FILTER_GENERIC_1_IPV4)
1397 ? "ip4(y)" : "ip4(n)");
1399 sprintf(ip4, "%s ", "ip4(x)");
1401 if (gp->mask_flags & FILTER_GENERIC_1_IPV6)
1403 (gp->val_flags & FILTER_GENERIC_1_IPV4)
1404 ? "ip6(y)" : "ip6(n)");
1406 sprintf(ip6, "%s ", "ip6(x)");
1408 if (gp->mask_flags & FILTER_GENERIC_1_UDP)
1410 (gp->val_flags & FILTER_GENERIC_1_UDP)
1411 ? "udp(y)" : "udp(n)");
1413 sprintf(udp, "%s ", "udp(x)");
1415 if (gp->mask_flags & FILTER_GENERIC_1_TCP)
1417 (gp->val_flags & FILTER_GENERIC_1_TCP)
1418 ? "tcp(y)" : "tcp(n)");
1420 sprintf(tcp, "%s ", "tcp(x)");
1422 if (gp->mask_flags & FILTER_GENERIC_1_TCP_OR_UDP)
1423 sprintf(tcpudp, "%s ",
1424 (gp->val_flags & FILTER_GENERIC_1_TCP_OR_UDP)
1425 ? "tcpudp(y)" : "tcpudp(n)");
1427 sprintf(tcpudp, "%s ", "tcpudp(x)");
1429 if (gp->mask_flags & FILTER_GENERIC_1_IP4SUM_OK)
1430 sprintf(ip4csum, "%s ",
1431 (gp->val_flags & FILTER_GENERIC_1_IP4SUM_OK)
1432 ? "ip4csum(y)" : "ip4csum(n)");
1434 sprintf(ip4csum, "%s ", "ip4csum(x)");
1436 if (gp->mask_flags & FILTER_GENERIC_1_L4SUM_OK)
1437 sprintf(l4csum, "%s ",
1438 (gp->val_flags & FILTER_GENERIC_1_L4SUM_OK)
1439 ? "l4csum(y)" : "l4csum(n)");
1441 sprintf(l4csum, "%s ", "l4csum(x)");
1443 if (gp->mask_flags & FILTER_GENERIC_1_IPFRAG)
1444 sprintf(ipfrag, "%s ",
1445 (gp->val_flags & FILTER_GENERIC_1_IPFRAG)
1446 ? "ipfrag(y)" : "ipfrag(n)");
1448 sprintf(ipfrag, "%s ", "ipfrag(x)");
1449 FLOW_LOG(INFO, "\tFlags: %s%s%s%s%s%s%s%s\n", ip4, ip6, udp,
1450 tcp, tcpudp, ip4csum, l4csum, ipfrag);
1452 for (i = 0; i < FILTER_GENERIC_1_NUM_LAYERS; i++) {
1453 mbyte = FILTER_GENERIC_1_KEY_LEN - 1;
1454 while (mbyte && !gp->layer[i].mask[mbyte])
1460 for (j = 0; j <= mbyte; j++) {
1462 gp->layer[i].mask[j]);
1466 FLOW_LOG(INFO, "\tL%u mask: %s\n", i + 2, buf);
1468 for (j = 0; j <= mbyte; j++) {
1470 gp->layer[i].val[j]);
1474 FLOW_LOG(INFO, "\tL%u val: %s\n", i + 2, buf);
1478 FLOW_LOG(INFO, "FILTER UNKNOWN\n");
1483 /* Debug function to dump internal NIC flow structures. */
1485 enic_dump_flow(const struct filter_action_v2 *ea, const struct filter_v2 *filt)
1487 enic_dump_filter(filt);
1488 enic_dump_actions(ea);
1493 * Internal flow parse/validate function.
1496 * This device pointer.
1497 * @param pattern[in]
1498 * @param actions[in]
1500 * @param enic_filter[out]
1501 * Internal NIC filter structure pointer.
1502 * @param enic_action[out]
1503 * Internal NIC action structure pointer.
1506 enic_flow_parse(struct rte_eth_dev *dev,
1507 const struct rte_flow_attr *attrs,
1508 const struct rte_flow_item pattern[],
1509 const struct rte_flow_action actions[],
1510 struct rte_flow_error *error,
1511 struct filter_v2 *enic_filter,
1512 struct filter_action_v2 *enic_action)
1514 unsigned int ret = 0;
1515 struct enic *enic = pmd_priv(dev);
1516 const struct enic_filter_cap *enic_filter_cap;
1517 const struct enic_action_cap *enic_action_cap;
1518 const struct rte_flow_action *action;
1522 memset(enic_filter, 0, sizeof(*enic_filter));
1523 memset(enic_action, 0, sizeof(*enic_action));
1526 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1527 NULL, "No pattern specified");
1532 rte_flow_error_set(error, EINVAL,
1533 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1534 NULL, "No action specified");
1540 rte_flow_error_set(error, ENOTSUP,
1541 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1543 "priority groups are not supported");
1545 } else if (attrs->priority) {
1546 rte_flow_error_set(error, ENOTSUP,
1547 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1549 "priorities are not supported");
1551 } else if (attrs->egress) {
1552 rte_flow_error_set(error, ENOTSUP,
1553 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
1555 "egress is not supported");
1557 } else if (attrs->transfer) {
1558 rte_flow_error_set(error, ENOTSUP,
1559 RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1561 "transfer is not supported");
1563 } else if (!attrs->ingress) {
1564 rte_flow_error_set(error, ENOTSUP,
1565 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1567 "only ingress is supported");
1572 rte_flow_error_set(error, EINVAL,
1573 RTE_FLOW_ERROR_TYPE_ATTR,
1574 NULL, "No attribute specified");
1578 /* Verify Actions. */
1579 enic_action_cap = enic_get_action_cap(enic);
1580 for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
1582 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1584 else if (!enic_match_action(action, enic_action_cap->actions))
1587 if (action->type != RTE_FLOW_ACTION_TYPE_END) {
1588 rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
1589 action, "Invalid action.");
1592 ret = enic_action_cap->copy_fn(enic, actions, enic_action);
1594 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1595 NULL, "Unsupported action.");
1599 /* Verify Flow items. If copying the filter from flow format to enic
1600 * format fails, the flow is not supported
1602 enic_filter_cap = enic_get_filter_cap(enic);
1603 if (enic_filter_cap == NULL) {
1604 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1605 NULL, "Flow API not available");
1608 enic_filter->type = enic->flow_filter_mode;
1609 ret = enic_copy_filter(pattern, enic_filter_cap, enic,
1610 enic_filter, error);
1615 * Push filter/action to the NIC.
1618 * Device structure pointer.
1619 * @param enic_filter[in]
1620 * Internal NIC filter structure pointer.
1621 * @param enic_action[in]
1622 * Internal NIC action structure pointer.
1625 static struct rte_flow *
1626 enic_flow_add_filter(struct enic *enic, struct filter_v2 *enic_filter,
1627 struct filter_action_v2 *enic_action,
1628 struct rte_flow_error *error)
1630 struct rte_flow *flow;
1634 int last_max_flow_ctr;
1638 flow = rte_calloc(__func__, 1, sizeof(*flow), 0);
1640 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1641 NULL, "cannot allocate flow memory");
1645 flow->counter_idx = -1;
1646 last_max_flow_ctr = -1;
1647 if (enic_action->flags & FILTER_ACTION_COUNTER_FLAG) {
1648 if (!vnic_dev_counter_alloc(enic->vdev, (uint32_t *)&ctr_idx)) {
1649 rte_flow_error_set(error, ENOMEM,
1650 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1651 NULL, "cannot allocate counter");
1652 goto unwind_flow_alloc;
1654 flow->counter_idx = ctr_idx;
1655 enic_action->counter_index = ctr_idx;
1657 /* If index is the largest, increase the counter DMA size */
1658 if (ctr_idx > enic->max_flow_counter) {
1659 err = vnic_dev_counter_dma_cfg(enic->vdev,
1660 VNIC_FLOW_COUNTER_UPDATE_MSECS,
1663 rte_flow_error_set(error, -err,
1664 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1665 NULL, "counter DMA config failed");
1666 goto unwind_ctr_alloc;
1668 last_max_flow_ctr = enic->max_flow_counter;
1669 enic->max_flow_counter = ctr_idx;
1673 /* entry[in] is the queue id, entry[out] is the filter Id for delete */
1674 entry = enic_action->rq_idx;
1675 err = vnic_dev_classifier(enic->vdev, CLSF_ADD, &entry, enic_filter,
1678 rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_HANDLE,
1679 NULL, "vnic_dev_classifier error");
1680 goto unwind_ctr_dma_cfg;
1683 flow->enic_filter_id = entry;
1684 flow->enic_filter = *enic_filter;
1688 /* unwind if there are errors */
1690 if (last_max_flow_ctr != -1) {
1691 /* reduce counter DMA size */
1692 vnic_dev_counter_dma_cfg(enic->vdev,
1693 VNIC_FLOW_COUNTER_UPDATE_MSECS,
1694 last_max_flow_ctr + 1);
1695 enic->max_flow_counter = last_max_flow_ctr;
1698 if (flow->counter_idx != -1)
1699 vnic_dev_counter_free(enic->vdev, ctr_idx);
1706 * Remove filter/action from the NIC.
1709 * Device structure pointer.
1710 * @param filter_id[in]
1712 * @param enic_action[in]
1713 * Internal NIC action structure pointer.
1717 enic_flow_del_filter(struct enic *enic, struct rte_flow *flow,
1718 struct rte_flow_error *error)
1725 filter_id = flow->enic_filter_id;
1726 err = vnic_dev_classifier(enic->vdev, CLSF_DEL, &filter_id, NULL, NULL);
1728 rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_HANDLE,
1729 NULL, "vnic_dev_classifier failed");
1733 if (flow->counter_idx != -1) {
1734 if (!vnic_dev_counter_free(enic->vdev, flow->counter_idx))
1735 dev_err(enic, "counter free failed, idx: %d\n",
1737 flow->counter_idx = -1;
1743 * The following functions are callbacks for Generic flow API.
1747 * Validate a flow supported by the NIC.
1749 * @see rte_flow_validate()
1753 enic_flow_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attrs,
1754 const struct rte_flow_item pattern[],
1755 const struct rte_flow_action actions[],
1756 struct rte_flow_error *error)
1758 struct filter_v2 enic_filter;
1759 struct filter_action_v2 enic_action;
1764 ret = enic_flow_parse(dev, attrs, pattern, actions, error,
1765 &enic_filter, &enic_action);
1767 enic_dump_flow(&enic_action, &enic_filter);
1772 * Create a flow supported by the NIC.
1774 * @see rte_flow_create()
1777 static struct rte_flow *
1778 enic_flow_create(struct rte_eth_dev *dev,
1779 const struct rte_flow_attr *attrs,
1780 const struct rte_flow_item pattern[],
1781 const struct rte_flow_action actions[],
1782 struct rte_flow_error *error)
1785 struct filter_v2 enic_filter;
1786 struct filter_action_v2 enic_action;
1787 struct rte_flow *flow;
1788 struct enic *enic = pmd_priv(dev);
1792 ret = enic_flow_parse(dev, attrs, pattern, actions, error, &enic_filter,
1797 rte_spinlock_lock(&enic->flows_lock);
1798 flow = enic_flow_add_filter(enic, &enic_filter, &enic_action,
1801 LIST_INSERT_HEAD(&enic->flows, flow, next);
1802 rte_spinlock_unlock(&enic->flows_lock);
1808 * Destroy a flow supported by the NIC.
1810 * @see rte_flow_destroy()
1814 enic_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
1815 __rte_unused struct rte_flow_error *error)
1817 struct enic *enic = pmd_priv(dev);
1821 rte_spinlock_lock(&enic->flows_lock);
1822 enic_flow_del_filter(enic, flow, error);
1823 LIST_REMOVE(flow, next);
1824 rte_spinlock_unlock(&enic->flows_lock);
1830 * Flush all flows on the device.
1832 * @see rte_flow_flush()
1836 enic_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1838 struct rte_flow *flow;
1839 struct enic *enic = pmd_priv(dev);
1843 rte_spinlock_lock(&enic->flows_lock);
1845 while (!LIST_EMPTY(&enic->flows)) {
1846 flow = LIST_FIRST(&enic->flows);
1847 enic_flow_del_filter(enic, flow, error);
1848 LIST_REMOVE(flow, next);
1851 rte_spinlock_unlock(&enic->flows_lock);
1856 enic_flow_query_count(struct rte_eth_dev *dev,
1857 struct rte_flow *flow, void *data,
1858 struct rte_flow_error *error)
1860 struct enic *enic = pmd_priv(dev);
1861 struct rte_flow_query_count *query;
1862 uint64_t packets, bytes;
1866 if (flow->counter_idx == -1) {
1867 return rte_flow_error_set(error, ENOTSUP,
1868 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1870 "flow does not have counter");
1872 query = (struct rte_flow_query_count *)data;
1873 if (!vnic_dev_counter_query(enic->vdev, flow->counter_idx,
1874 !!query->reset, &packets, &bytes)) {
1875 return rte_flow_error_set
1877 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1879 "cannot read counter");
1881 query->hits_set = 1;
1882 query->bytes_set = 1;
1883 query->hits = packets;
1884 query->bytes = bytes;
1889 enic_flow_query(struct rte_eth_dev *dev,
1890 struct rte_flow *flow,
1891 const struct rte_flow_action *actions,
1893 struct rte_flow_error *error)
1899 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1900 switch (actions->type) {
1901 case RTE_FLOW_ACTION_TYPE_VOID:
1903 case RTE_FLOW_ACTION_TYPE_COUNT:
1904 ret = enic_flow_query_count(dev, flow, data, error);
1907 return rte_flow_error_set(error, ENOTSUP,
1908 RTE_FLOW_ERROR_TYPE_ACTION,
1910 "action not supported");
1919 * Flow callback registration.
1923 const struct rte_flow_ops enic_flow_ops = {
1924 .validate = enic_flow_validate,
1925 .create = enic_flow_create,
1926 .destroy = enic_flow_destroy,
1927 .flush = enic_flow_flush,
1928 .query = enic_flow_query,