1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018 Mellanox Technologies, Ltd
11 #include <rte_common.h>
12 #include <rte_ether.h>
13 #include <rte_ethdev_driver.h>
15 #include <rte_flow_driver.h>
16 #include <rte_malloc.h>
17 #include <rte_cycles.h>
20 #include <rte_vxlan.h>
22 #include <rte_eal_paging.h>
25 #include <mlx5_glue.h>
26 #include <mlx5_devx_cmds.h>
28 #include <mlx5_malloc.h>
30 #include "mlx5_defs.h"
32 #include "mlx5_common_os.h"
33 #include "mlx5_flow.h"
34 #include "mlx5_flow_os.h"
35 #include "mlx5_rxtx.h"
37 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
39 #ifndef HAVE_IBV_FLOW_DEVX_COUNTERS
40 #define MLX5DV_FLOW_ACTION_COUNTERS_DEVX 0
43 #ifndef HAVE_MLX5DV_DR_ESWITCH
44 #ifndef MLX5DV_FLOW_TABLE_TYPE_FDB
45 #define MLX5DV_FLOW_TABLE_TYPE_FDB 0
49 #ifndef HAVE_MLX5DV_DR
50 #define MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL 1
53 /* VLAN header definitions */
54 #define MLX5DV_FLOW_VLAN_PCP_SHIFT 13
55 #define MLX5DV_FLOW_VLAN_PCP_MASK (0x7 << MLX5DV_FLOW_VLAN_PCP_SHIFT)
56 #define MLX5DV_FLOW_VLAN_VID_MASK 0x0fff
57 #define MLX5DV_FLOW_VLAN_PCP_MASK_BE RTE_BE16(MLX5DV_FLOW_VLAN_PCP_MASK)
58 #define MLX5DV_FLOW_VLAN_VID_MASK_BE RTE_BE16(MLX5DV_FLOW_VLAN_VID_MASK)
73 flow_dv_tbl_resource_release(struct rte_eth_dev *dev,
74 struct mlx5_flow_tbl_resource *tbl);
77 flow_dv_default_miss_resource_release(struct rte_eth_dev *dev);
80 * Initialize flow attributes structure according to flow items' types.
82 * flow_dv_validate() avoids multiple L3/L4 layers cases other than tunnel
83 * mode. For tunnel mode, the items to be modified are the outermost ones.
86 * Pointer to item specification.
88 * Pointer to flow attributes structure.
90 * Pointer to the sub flow.
91 * @param[in] tunnel_decap
92 * Whether action is after tunnel decapsulation.
95 flow_dv_attr_init(const struct rte_flow_item *item, union flow_dv_attr *attr,
96 struct mlx5_flow *dev_flow, bool tunnel_decap)
98 uint64_t layers = dev_flow->handle->layers;
101 * If layers is already initialized, it means this dev_flow is the
102 * suffix flow, the layers flags is set by the prefix flow. Need to
103 * use the layer flags from prefix flow as the suffix flow may not
104 * have the user defined items as the flow is split.
107 if (layers & MLX5_FLOW_LAYER_OUTER_L3_IPV4)
109 else if (layers & MLX5_FLOW_LAYER_OUTER_L3_IPV6)
111 if (layers & MLX5_FLOW_LAYER_OUTER_L4_TCP)
113 else if (layers & MLX5_FLOW_LAYER_OUTER_L4_UDP)
118 for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
119 uint8_t next_protocol = 0xff;
120 switch (item->type) {
121 case RTE_FLOW_ITEM_TYPE_GRE:
122 case RTE_FLOW_ITEM_TYPE_NVGRE:
123 case RTE_FLOW_ITEM_TYPE_VXLAN:
124 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
125 case RTE_FLOW_ITEM_TYPE_GENEVE:
126 case RTE_FLOW_ITEM_TYPE_MPLS:
130 case RTE_FLOW_ITEM_TYPE_IPV4:
133 if (item->mask != NULL &&
134 ((const struct rte_flow_item_ipv4 *)
135 item->mask)->hdr.next_proto_id)
137 ((const struct rte_flow_item_ipv4 *)
138 (item->spec))->hdr.next_proto_id &
139 ((const struct rte_flow_item_ipv4 *)
140 (item->mask))->hdr.next_proto_id;
141 if ((next_protocol == IPPROTO_IPIP ||
142 next_protocol == IPPROTO_IPV6) && tunnel_decap)
145 case RTE_FLOW_ITEM_TYPE_IPV6:
148 if (item->mask != NULL &&
149 ((const struct rte_flow_item_ipv6 *)
150 item->mask)->hdr.proto)
152 ((const struct rte_flow_item_ipv6 *)
153 (item->spec))->hdr.proto &
154 ((const struct rte_flow_item_ipv6 *)
155 (item->mask))->hdr.proto;
156 if ((next_protocol == IPPROTO_IPIP ||
157 next_protocol == IPPROTO_IPV6) && tunnel_decap)
160 case RTE_FLOW_ITEM_TYPE_UDP:
164 case RTE_FLOW_ITEM_TYPE_TCP:
176 * Convert rte_mtr_color to mlx5 color.
185 rte_col_2_mlx5_col(enum rte_color rcol)
188 case RTE_COLOR_GREEN:
189 return MLX5_FLOW_COLOR_GREEN;
190 case RTE_COLOR_YELLOW:
191 return MLX5_FLOW_COLOR_YELLOW;
193 return MLX5_FLOW_COLOR_RED;
197 return MLX5_FLOW_COLOR_UNDEFINED;
200 struct field_modify_info {
201 uint32_t size; /* Size of field in protocol header, in bytes. */
202 uint32_t offset; /* Offset of field in protocol header, in bytes. */
203 enum mlx5_modification_field id;
206 struct field_modify_info modify_eth[] = {
207 {4, 0, MLX5_MODI_OUT_DMAC_47_16},
208 {2, 4, MLX5_MODI_OUT_DMAC_15_0},
209 {4, 6, MLX5_MODI_OUT_SMAC_47_16},
210 {2, 10, MLX5_MODI_OUT_SMAC_15_0},
214 struct field_modify_info modify_vlan_out_first_vid[] = {
215 /* Size in bits !!! */
216 {12, 0, MLX5_MODI_OUT_FIRST_VID},
220 struct field_modify_info modify_ipv4[] = {
221 {1, 1, MLX5_MODI_OUT_IP_DSCP},
222 {1, 8, MLX5_MODI_OUT_IPV4_TTL},
223 {4, 12, MLX5_MODI_OUT_SIPV4},
224 {4, 16, MLX5_MODI_OUT_DIPV4},
228 struct field_modify_info modify_ipv6[] = {
229 {1, 0, MLX5_MODI_OUT_IP_DSCP},
230 {1, 7, MLX5_MODI_OUT_IPV6_HOPLIMIT},
231 {4, 8, MLX5_MODI_OUT_SIPV6_127_96},
232 {4, 12, MLX5_MODI_OUT_SIPV6_95_64},
233 {4, 16, MLX5_MODI_OUT_SIPV6_63_32},
234 {4, 20, MLX5_MODI_OUT_SIPV6_31_0},
235 {4, 24, MLX5_MODI_OUT_DIPV6_127_96},
236 {4, 28, MLX5_MODI_OUT_DIPV6_95_64},
237 {4, 32, MLX5_MODI_OUT_DIPV6_63_32},
238 {4, 36, MLX5_MODI_OUT_DIPV6_31_0},
242 struct field_modify_info modify_udp[] = {
243 {2, 0, MLX5_MODI_OUT_UDP_SPORT},
244 {2, 2, MLX5_MODI_OUT_UDP_DPORT},
248 struct field_modify_info modify_tcp[] = {
249 {2, 0, MLX5_MODI_OUT_TCP_SPORT},
250 {2, 2, MLX5_MODI_OUT_TCP_DPORT},
251 {4, 4, MLX5_MODI_OUT_TCP_SEQ_NUM},
252 {4, 8, MLX5_MODI_OUT_TCP_ACK_NUM},
257 mlx5_flow_tunnel_ip_check(const struct rte_flow_item *item __rte_unused,
258 uint8_t next_protocol, uint64_t *item_flags,
261 MLX5_ASSERT(item->type == RTE_FLOW_ITEM_TYPE_IPV4 ||
262 item->type == RTE_FLOW_ITEM_TYPE_IPV6);
263 if (next_protocol == IPPROTO_IPIP) {
264 *item_flags |= MLX5_FLOW_LAYER_IPIP;
267 if (next_protocol == IPPROTO_IPV6) {
268 *item_flags |= MLX5_FLOW_LAYER_IPV6_ENCAP;
274 * Acquire the synchronizing object to protect multithreaded access
275 * to shared dv context. Lock occurs only if context is actually
276 * shared, i.e. we have multiport IB device and representors are
280 * Pointer to the rte_eth_dev structure.
283 flow_dv_shared_lock(struct rte_eth_dev *dev)
285 struct mlx5_priv *priv = dev->data->dev_private;
286 struct mlx5_dev_ctx_shared *sh = priv->sh;
288 if (sh->dv_refcnt > 1) {
291 ret = pthread_mutex_lock(&sh->dv_mutex);
298 flow_dv_shared_unlock(struct rte_eth_dev *dev)
300 struct mlx5_priv *priv = dev->data->dev_private;
301 struct mlx5_dev_ctx_shared *sh = priv->sh;
303 if (sh->dv_refcnt > 1) {
306 ret = pthread_mutex_unlock(&sh->dv_mutex);
312 /* Update VLAN's VID/PCP based on input rte_flow_action.
315 * Pointer to struct rte_flow_action.
317 * Pointer to struct rte_vlan_hdr.
320 mlx5_update_vlan_vid_pcp(const struct rte_flow_action *action,
321 struct rte_vlan_hdr *vlan)
324 if (action->type == RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP) {
326 ((const struct rte_flow_action_of_set_vlan_pcp *)
327 action->conf)->vlan_pcp;
328 vlan_tci = vlan_tci << MLX5DV_FLOW_VLAN_PCP_SHIFT;
329 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_PCP_MASK;
330 vlan->vlan_tci |= vlan_tci;
331 } else if (action->type == RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID) {
332 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_VID_MASK;
333 vlan->vlan_tci |= rte_be_to_cpu_16
334 (((const struct rte_flow_action_of_set_vlan_vid *)
335 action->conf)->vlan_vid);
340 * Fetch 1, 2, 3 or 4 byte field from the byte array
341 * and return as unsigned integer in host-endian format.
344 * Pointer to data array.
346 * Size of field to extract.
349 * converted field in host endian format.
351 static inline uint32_t
352 flow_dv_fetch_field(const uint8_t *data, uint32_t size)
361 ret = rte_be_to_cpu_16(*(const unaligned_uint16_t *)data);
364 ret = rte_be_to_cpu_16(*(const unaligned_uint16_t *)data);
365 ret = (ret << 8) | *(data + sizeof(uint16_t));
368 ret = rte_be_to_cpu_32(*(const unaligned_uint32_t *)data);
379 * Convert modify-header action to DV specification.
381 * Data length of each action is determined by provided field description
382 * and the item mask. Data bit offset and width of each action is determined
383 * by provided item mask.
386 * Pointer to item specification.
388 * Pointer to field modification information.
389 * For MLX5_MODIFICATION_TYPE_SET specifies destination field.
390 * For MLX5_MODIFICATION_TYPE_ADD specifies destination field.
391 * For MLX5_MODIFICATION_TYPE_COPY specifies source field.
393 * Destination field info for MLX5_MODIFICATION_TYPE_COPY in @type.
394 * Negative offset value sets the same offset as source offset.
395 * size field is ignored, value is taken from source field.
396 * @param[in,out] resource
397 * Pointer to the modify-header resource.
399 * Type of modification.
401 * Pointer to the error structure.
404 * 0 on success, a negative errno value otherwise and rte_errno is set.
407 flow_dv_convert_modify_action(struct rte_flow_item *item,
408 struct field_modify_info *field,
409 struct field_modify_info *dcopy,
410 struct mlx5_flow_dv_modify_hdr_resource *resource,
411 uint32_t type, struct rte_flow_error *error)
413 uint32_t i = resource->actions_num;
414 struct mlx5_modification_cmd *actions = resource->actions;
417 * The item and mask are provided in big-endian format.
418 * The fields should be presented as in big-endian format either.
419 * Mask must be always present, it defines the actual field width.
421 MLX5_ASSERT(item->mask);
422 MLX5_ASSERT(field->size);
429 if (i >= MLX5_MAX_MODIFY_NUM)
430 return rte_flow_error_set(error, EINVAL,
431 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
432 "too many items to modify");
433 /* Fetch variable byte size mask from the array. */
434 mask = flow_dv_fetch_field((const uint8_t *)item->mask +
435 field->offset, field->size);
440 /* Deduce actual data width in bits from mask value. */
441 off_b = rte_bsf32(mask);
442 size_b = sizeof(uint32_t) * CHAR_BIT -
443 off_b - __builtin_clz(mask);
445 size_b = size_b == sizeof(uint32_t) * CHAR_BIT ? 0 : size_b;
446 actions[i] = (struct mlx5_modification_cmd) {
452 /* Convert entire record to expected big-endian format. */
453 actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
454 if (type == MLX5_MODIFICATION_TYPE_COPY) {
456 actions[i].dst_field = dcopy->id;
457 actions[i].dst_offset =
458 (int)dcopy->offset < 0 ? off_b : dcopy->offset;
459 /* Convert entire record to big-endian format. */
460 actions[i].data1 = rte_cpu_to_be_32(actions[i].data1);
462 MLX5_ASSERT(item->spec);
463 data = flow_dv_fetch_field((const uint8_t *)item->spec +
464 field->offset, field->size);
465 /* Shift out the trailing masked bits from data. */
466 data = (data & mask) >> off_b;
467 actions[i].data1 = rte_cpu_to_be_32(data);
471 } while (field->size);
472 if (resource->actions_num == i)
473 return rte_flow_error_set(error, EINVAL,
474 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
475 "invalid modification flow item");
476 resource->actions_num = i;
481 * Convert modify-header set IPv4 address action to DV specification.
483 * @param[in,out] resource
484 * Pointer to the modify-header resource.
486 * Pointer to action specification.
488 * Pointer to the error structure.
491 * 0 on success, a negative errno value otherwise and rte_errno is set.
494 flow_dv_convert_action_modify_ipv4
495 (struct mlx5_flow_dv_modify_hdr_resource *resource,
496 const struct rte_flow_action *action,
497 struct rte_flow_error *error)
499 const struct rte_flow_action_set_ipv4 *conf =
500 (const struct rte_flow_action_set_ipv4 *)(action->conf);
501 struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
502 struct rte_flow_item_ipv4 ipv4;
503 struct rte_flow_item_ipv4 ipv4_mask;
505 memset(&ipv4, 0, sizeof(ipv4));
506 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
507 if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC) {
508 ipv4.hdr.src_addr = conf->ipv4_addr;
509 ipv4_mask.hdr.src_addr = rte_flow_item_ipv4_mask.hdr.src_addr;
511 ipv4.hdr.dst_addr = conf->ipv4_addr;
512 ipv4_mask.hdr.dst_addr = rte_flow_item_ipv4_mask.hdr.dst_addr;
515 item.mask = &ipv4_mask;
516 return flow_dv_convert_modify_action(&item, modify_ipv4, NULL, resource,
517 MLX5_MODIFICATION_TYPE_SET, error);
521 * Convert modify-header set IPv6 address action to DV specification.
523 * @param[in,out] resource
524 * Pointer to the modify-header resource.
526 * Pointer to action specification.
528 * Pointer to the error structure.
531 * 0 on success, a negative errno value otherwise and rte_errno is set.
534 flow_dv_convert_action_modify_ipv6
535 (struct mlx5_flow_dv_modify_hdr_resource *resource,
536 const struct rte_flow_action *action,
537 struct rte_flow_error *error)
539 const struct rte_flow_action_set_ipv6 *conf =
540 (const struct rte_flow_action_set_ipv6 *)(action->conf);
541 struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
542 struct rte_flow_item_ipv6 ipv6;
543 struct rte_flow_item_ipv6 ipv6_mask;
545 memset(&ipv6, 0, sizeof(ipv6));
546 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
547 if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC) {
548 memcpy(&ipv6.hdr.src_addr, &conf->ipv6_addr,
549 sizeof(ipv6.hdr.src_addr));
550 memcpy(&ipv6_mask.hdr.src_addr,
551 &rte_flow_item_ipv6_mask.hdr.src_addr,
552 sizeof(ipv6.hdr.src_addr));
554 memcpy(&ipv6.hdr.dst_addr, &conf->ipv6_addr,
555 sizeof(ipv6.hdr.dst_addr));
556 memcpy(&ipv6_mask.hdr.dst_addr,
557 &rte_flow_item_ipv6_mask.hdr.dst_addr,
558 sizeof(ipv6.hdr.dst_addr));
561 item.mask = &ipv6_mask;
562 return flow_dv_convert_modify_action(&item, modify_ipv6, NULL, resource,
563 MLX5_MODIFICATION_TYPE_SET, error);
567 * Convert modify-header set MAC address action to DV specification.
569 * @param[in,out] resource
570 * Pointer to the modify-header resource.
572 * Pointer to action specification.
574 * Pointer to the error structure.
577 * 0 on success, a negative errno value otherwise and rte_errno is set.
580 flow_dv_convert_action_modify_mac
581 (struct mlx5_flow_dv_modify_hdr_resource *resource,
582 const struct rte_flow_action *action,
583 struct rte_flow_error *error)
585 const struct rte_flow_action_set_mac *conf =
586 (const struct rte_flow_action_set_mac *)(action->conf);
587 struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_ETH };
588 struct rte_flow_item_eth eth;
589 struct rte_flow_item_eth eth_mask;
591 memset(ð, 0, sizeof(eth));
592 memset(ð_mask, 0, sizeof(eth_mask));
593 if (action->type == RTE_FLOW_ACTION_TYPE_SET_MAC_SRC) {
594 memcpy(ð.src.addr_bytes, &conf->mac_addr,
595 sizeof(eth.src.addr_bytes));
596 memcpy(ð_mask.src.addr_bytes,
597 &rte_flow_item_eth_mask.src.addr_bytes,
598 sizeof(eth_mask.src.addr_bytes));
600 memcpy(ð.dst.addr_bytes, &conf->mac_addr,
601 sizeof(eth.dst.addr_bytes));
602 memcpy(ð_mask.dst.addr_bytes,
603 &rte_flow_item_eth_mask.dst.addr_bytes,
604 sizeof(eth_mask.dst.addr_bytes));
607 item.mask = ð_mask;
608 return flow_dv_convert_modify_action(&item, modify_eth, NULL, resource,
609 MLX5_MODIFICATION_TYPE_SET, error);
613 * Convert modify-header set VLAN VID action to DV specification.
615 * @param[in,out] resource
616 * Pointer to the modify-header resource.
618 * Pointer to action specification.
620 * Pointer to the error structure.
623 * 0 on success, a negative errno value otherwise and rte_errno is set.
626 flow_dv_convert_action_modify_vlan_vid
627 (struct mlx5_flow_dv_modify_hdr_resource *resource,
628 const struct rte_flow_action *action,
629 struct rte_flow_error *error)
631 const struct rte_flow_action_of_set_vlan_vid *conf =
632 (const struct rte_flow_action_of_set_vlan_vid *)(action->conf);
633 int i = resource->actions_num;
634 struct mlx5_modification_cmd *actions = resource->actions;
635 struct field_modify_info *field = modify_vlan_out_first_vid;
637 if (i >= MLX5_MAX_MODIFY_NUM)
638 return rte_flow_error_set(error, EINVAL,
639 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
640 "too many items to modify");
641 actions[i] = (struct mlx5_modification_cmd) {
642 .action_type = MLX5_MODIFICATION_TYPE_SET,
644 .length = field->size,
645 .offset = field->offset,
647 actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
648 actions[i].data1 = conf->vlan_vid;
649 actions[i].data1 = actions[i].data1 << 16;
650 resource->actions_num = ++i;
655 * Convert modify-header set TP action to DV specification.
657 * @param[in,out] resource
658 * Pointer to the modify-header resource.
660 * Pointer to action specification.
662 * Pointer to rte_flow_item objects list.
664 * Pointer to flow attributes structure.
665 * @param[in] dev_flow
666 * Pointer to the sub flow.
667 * @param[in] tunnel_decap
668 * Whether action is after tunnel decapsulation.
670 * Pointer to the error structure.
673 * 0 on success, a negative errno value otherwise and rte_errno is set.
676 flow_dv_convert_action_modify_tp
677 (struct mlx5_flow_dv_modify_hdr_resource *resource,
678 const struct rte_flow_action *action,
679 const struct rte_flow_item *items,
680 union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
681 bool tunnel_decap, struct rte_flow_error *error)
683 const struct rte_flow_action_set_tp *conf =
684 (const struct rte_flow_action_set_tp *)(action->conf);
685 struct rte_flow_item item;
686 struct rte_flow_item_udp udp;
687 struct rte_flow_item_udp udp_mask;
688 struct rte_flow_item_tcp tcp;
689 struct rte_flow_item_tcp tcp_mask;
690 struct field_modify_info *field;
693 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
695 memset(&udp, 0, sizeof(udp));
696 memset(&udp_mask, 0, sizeof(udp_mask));
697 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
698 udp.hdr.src_port = conf->port;
699 udp_mask.hdr.src_port =
700 rte_flow_item_udp_mask.hdr.src_port;
702 udp.hdr.dst_port = conf->port;
703 udp_mask.hdr.dst_port =
704 rte_flow_item_udp_mask.hdr.dst_port;
706 item.type = RTE_FLOW_ITEM_TYPE_UDP;
708 item.mask = &udp_mask;
711 MLX5_ASSERT(attr->tcp);
712 memset(&tcp, 0, sizeof(tcp));
713 memset(&tcp_mask, 0, sizeof(tcp_mask));
714 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
715 tcp.hdr.src_port = conf->port;
716 tcp_mask.hdr.src_port =
717 rte_flow_item_tcp_mask.hdr.src_port;
719 tcp.hdr.dst_port = conf->port;
720 tcp_mask.hdr.dst_port =
721 rte_flow_item_tcp_mask.hdr.dst_port;
723 item.type = RTE_FLOW_ITEM_TYPE_TCP;
725 item.mask = &tcp_mask;
728 return flow_dv_convert_modify_action(&item, field, NULL, resource,
729 MLX5_MODIFICATION_TYPE_SET, error);
733 * Convert modify-header set TTL action to DV specification.
735 * @param[in,out] resource
736 * Pointer to the modify-header resource.
738 * Pointer to action specification.
740 * Pointer to rte_flow_item objects list.
742 * Pointer to flow attributes structure.
743 * @param[in] dev_flow
744 * Pointer to the sub flow.
745 * @param[in] tunnel_decap
746 * Whether action is after tunnel decapsulation.
748 * Pointer to the error structure.
751 * 0 on success, a negative errno value otherwise and rte_errno is set.
754 flow_dv_convert_action_modify_ttl
755 (struct mlx5_flow_dv_modify_hdr_resource *resource,
756 const struct rte_flow_action *action,
757 const struct rte_flow_item *items,
758 union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
759 bool tunnel_decap, struct rte_flow_error *error)
761 const struct rte_flow_action_set_ttl *conf =
762 (const struct rte_flow_action_set_ttl *)(action->conf);
763 struct rte_flow_item item;
764 struct rte_flow_item_ipv4 ipv4;
765 struct rte_flow_item_ipv4 ipv4_mask;
766 struct rte_flow_item_ipv6 ipv6;
767 struct rte_flow_item_ipv6 ipv6_mask;
768 struct field_modify_info *field;
771 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
773 memset(&ipv4, 0, sizeof(ipv4));
774 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
775 ipv4.hdr.time_to_live = conf->ttl_value;
776 ipv4_mask.hdr.time_to_live = 0xFF;
777 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
779 item.mask = &ipv4_mask;
782 MLX5_ASSERT(attr->ipv6);
783 memset(&ipv6, 0, sizeof(ipv6));
784 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
785 ipv6.hdr.hop_limits = conf->ttl_value;
786 ipv6_mask.hdr.hop_limits = 0xFF;
787 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
789 item.mask = &ipv6_mask;
792 return flow_dv_convert_modify_action(&item, field, NULL, resource,
793 MLX5_MODIFICATION_TYPE_SET, error);
797 * Convert modify-header decrement TTL action to DV specification.
799 * @param[in,out] resource
800 * Pointer to the modify-header resource.
802 * Pointer to action specification.
804 * Pointer to rte_flow_item objects list.
806 * Pointer to flow attributes structure.
807 * @param[in] dev_flow
808 * Pointer to the sub flow.
809 * @param[in] tunnel_decap
810 * Whether action is after tunnel decapsulation.
812 * Pointer to the error structure.
815 * 0 on success, a negative errno value otherwise and rte_errno is set.
818 flow_dv_convert_action_modify_dec_ttl
819 (struct mlx5_flow_dv_modify_hdr_resource *resource,
820 const struct rte_flow_item *items,
821 union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
822 bool tunnel_decap, struct rte_flow_error *error)
824 struct rte_flow_item item;
825 struct rte_flow_item_ipv4 ipv4;
826 struct rte_flow_item_ipv4 ipv4_mask;
827 struct rte_flow_item_ipv6 ipv6;
828 struct rte_flow_item_ipv6 ipv6_mask;
829 struct field_modify_info *field;
832 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
834 memset(&ipv4, 0, sizeof(ipv4));
835 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
836 ipv4.hdr.time_to_live = 0xFF;
837 ipv4_mask.hdr.time_to_live = 0xFF;
838 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
840 item.mask = &ipv4_mask;
843 MLX5_ASSERT(attr->ipv6);
844 memset(&ipv6, 0, sizeof(ipv6));
845 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
846 ipv6.hdr.hop_limits = 0xFF;
847 ipv6_mask.hdr.hop_limits = 0xFF;
848 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
850 item.mask = &ipv6_mask;
853 return flow_dv_convert_modify_action(&item, field, NULL, resource,
854 MLX5_MODIFICATION_TYPE_ADD, error);
858 * Convert modify-header increment/decrement TCP Sequence number
859 * to DV specification.
861 * @param[in,out] resource
862 * Pointer to the modify-header resource.
864 * Pointer to action specification.
866 * Pointer to the error structure.
869 * 0 on success, a negative errno value otherwise and rte_errno is set.
872 flow_dv_convert_action_modify_tcp_seq
873 (struct mlx5_flow_dv_modify_hdr_resource *resource,
874 const struct rte_flow_action *action,
875 struct rte_flow_error *error)
877 const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
878 uint64_t value = rte_be_to_cpu_32(*conf);
879 struct rte_flow_item item;
880 struct rte_flow_item_tcp tcp;
881 struct rte_flow_item_tcp tcp_mask;
883 memset(&tcp, 0, sizeof(tcp));
884 memset(&tcp_mask, 0, sizeof(tcp_mask));
885 if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ)
887 * The HW has no decrement operation, only increment operation.
888 * To simulate decrement X from Y using increment operation
889 * we need to add UINT32_MAX X times to Y.
890 * Each adding of UINT32_MAX decrements Y by 1.
893 tcp.hdr.sent_seq = rte_cpu_to_be_32((uint32_t)value);
894 tcp_mask.hdr.sent_seq = RTE_BE32(UINT32_MAX);
895 item.type = RTE_FLOW_ITEM_TYPE_TCP;
897 item.mask = &tcp_mask;
898 return flow_dv_convert_modify_action(&item, modify_tcp, NULL, resource,
899 MLX5_MODIFICATION_TYPE_ADD, error);
903 * Convert modify-header increment/decrement TCP Acknowledgment number
904 * to DV specification.
906 * @param[in,out] resource
907 * Pointer to the modify-header resource.
909 * Pointer to action specification.
911 * Pointer to the error structure.
914 * 0 on success, a negative errno value otherwise and rte_errno is set.
917 flow_dv_convert_action_modify_tcp_ack
918 (struct mlx5_flow_dv_modify_hdr_resource *resource,
919 const struct rte_flow_action *action,
920 struct rte_flow_error *error)
922 const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
923 uint64_t value = rte_be_to_cpu_32(*conf);
924 struct rte_flow_item item;
925 struct rte_flow_item_tcp tcp;
926 struct rte_flow_item_tcp tcp_mask;
928 memset(&tcp, 0, sizeof(tcp));
929 memset(&tcp_mask, 0, sizeof(tcp_mask));
930 if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK)
932 * The HW has no decrement operation, only increment operation.
933 * To simulate decrement X from Y using increment operation
934 * we need to add UINT32_MAX X times to Y.
935 * Each adding of UINT32_MAX decrements Y by 1.
938 tcp.hdr.recv_ack = rte_cpu_to_be_32((uint32_t)value);
939 tcp_mask.hdr.recv_ack = RTE_BE32(UINT32_MAX);
940 item.type = RTE_FLOW_ITEM_TYPE_TCP;
942 item.mask = &tcp_mask;
943 return flow_dv_convert_modify_action(&item, modify_tcp, NULL, resource,
944 MLX5_MODIFICATION_TYPE_ADD, error);
947 static enum mlx5_modification_field reg_to_field[] = {
948 [REG_NON] = MLX5_MODI_OUT_NONE,
949 [REG_A] = MLX5_MODI_META_DATA_REG_A,
950 [REG_B] = MLX5_MODI_META_DATA_REG_B,
951 [REG_C_0] = MLX5_MODI_META_REG_C_0,
952 [REG_C_1] = MLX5_MODI_META_REG_C_1,
953 [REG_C_2] = MLX5_MODI_META_REG_C_2,
954 [REG_C_3] = MLX5_MODI_META_REG_C_3,
955 [REG_C_4] = MLX5_MODI_META_REG_C_4,
956 [REG_C_5] = MLX5_MODI_META_REG_C_5,
957 [REG_C_6] = MLX5_MODI_META_REG_C_6,
958 [REG_C_7] = MLX5_MODI_META_REG_C_7,
962 * Convert register set to DV specification.
964 * @param[in,out] resource
965 * Pointer to the modify-header resource.
967 * Pointer to action specification.
969 * Pointer to the error structure.
972 * 0 on success, a negative errno value otherwise and rte_errno is set.
975 flow_dv_convert_action_set_reg
976 (struct mlx5_flow_dv_modify_hdr_resource *resource,
977 const struct rte_flow_action *action,
978 struct rte_flow_error *error)
980 const struct mlx5_rte_flow_action_set_tag *conf = action->conf;
981 struct mlx5_modification_cmd *actions = resource->actions;
982 uint32_t i = resource->actions_num;
984 if (i >= MLX5_MAX_MODIFY_NUM)
985 return rte_flow_error_set(error, EINVAL,
986 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
987 "too many items to modify");
988 MLX5_ASSERT(conf->id != REG_NON);
989 MLX5_ASSERT(conf->id < RTE_DIM(reg_to_field));
990 actions[i] = (struct mlx5_modification_cmd) {
991 .action_type = MLX5_MODIFICATION_TYPE_SET,
992 .field = reg_to_field[conf->id],
994 actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
995 actions[i].data1 = rte_cpu_to_be_32(conf->data);
997 resource->actions_num = i;
1002 * Convert SET_TAG action to DV specification.
1005 * Pointer to the rte_eth_dev structure.
1006 * @param[in,out] resource
1007 * Pointer to the modify-header resource.
1009 * Pointer to action specification.
1011 * Pointer to the error structure.
1014 * 0 on success, a negative errno value otherwise and rte_errno is set.
1017 flow_dv_convert_action_set_tag
1018 (struct rte_eth_dev *dev,
1019 struct mlx5_flow_dv_modify_hdr_resource *resource,
1020 const struct rte_flow_action_set_tag *conf,
1021 struct rte_flow_error *error)
1023 rte_be32_t data = rte_cpu_to_be_32(conf->data);
1024 rte_be32_t mask = rte_cpu_to_be_32(conf->mask);
1025 struct rte_flow_item item = {
1029 struct field_modify_info reg_c_x[] = {
1032 enum mlx5_modification_field reg_type;
1035 ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, conf->index, error);
1038 MLX5_ASSERT(ret != REG_NON);
1039 MLX5_ASSERT((unsigned int)ret < RTE_DIM(reg_to_field));
1040 reg_type = reg_to_field[ret];
1041 MLX5_ASSERT(reg_type > 0);
1042 reg_c_x[0] = (struct field_modify_info){4, 0, reg_type};
1043 return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1044 MLX5_MODIFICATION_TYPE_SET, error);
1048 * Convert internal COPY_REG action to DV specification.
1051 * Pointer to the rte_eth_dev structure.
1052 * @param[in,out] res
1053 * Pointer to the modify-header resource.
1055 * Pointer to action specification.
1057 * Pointer to the error structure.
1060 * 0 on success, a negative errno value otherwise and rte_errno is set.
1063 flow_dv_convert_action_copy_mreg(struct rte_eth_dev *dev,
1064 struct mlx5_flow_dv_modify_hdr_resource *res,
1065 const struct rte_flow_action *action,
1066 struct rte_flow_error *error)
1068 const struct mlx5_flow_action_copy_mreg *conf = action->conf;
1069 rte_be32_t mask = RTE_BE32(UINT32_MAX);
1070 struct rte_flow_item item = {
1074 struct field_modify_info reg_src[] = {
1075 {4, 0, reg_to_field[conf->src]},
1078 struct field_modify_info reg_dst = {
1080 .id = reg_to_field[conf->dst],
1082 /* Adjust reg_c[0] usage according to reported mask. */
1083 if (conf->dst == REG_C_0 || conf->src == REG_C_0) {
1084 struct mlx5_priv *priv = dev->data->dev_private;
1085 uint32_t reg_c0 = priv->sh->dv_regc0_mask;
1087 MLX5_ASSERT(reg_c0);
1088 MLX5_ASSERT(priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY);
1089 if (conf->dst == REG_C_0) {
1090 /* Copy to reg_c[0], within mask only. */
1091 reg_dst.offset = rte_bsf32(reg_c0);
1093 * Mask is ignoring the enianness, because
1094 * there is no conversion in datapath.
1096 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1097 /* Copy from destination lower bits to reg_c[0]. */
1098 mask = reg_c0 >> reg_dst.offset;
1100 /* Copy from destination upper bits to reg_c[0]. */
1101 mask = reg_c0 << (sizeof(reg_c0) * CHAR_BIT -
1102 rte_fls_u32(reg_c0));
1105 mask = rte_cpu_to_be_32(reg_c0);
1106 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1107 /* Copy from reg_c[0] to destination lower bits. */
1110 /* Copy from reg_c[0] to destination upper bits. */
1111 reg_dst.offset = sizeof(reg_c0) * CHAR_BIT -
1112 (rte_fls_u32(reg_c0) -
1117 return flow_dv_convert_modify_action(&item,
1118 reg_src, ®_dst, res,
1119 MLX5_MODIFICATION_TYPE_COPY,
1124 * Convert MARK action to DV specification. This routine is used
1125 * in extensive metadata only and requires metadata register to be
1126 * handled. In legacy mode hardware tag resource is engaged.
1129 * Pointer to the rte_eth_dev structure.
1131 * Pointer to MARK action specification.
1132 * @param[in,out] resource
1133 * Pointer to the modify-header resource.
1135 * Pointer to the error structure.
1138 * 0 on success, a negative errno value otherwise and rte_errno is set.
1141 flow_dv_convert_action_mark(struct rte_eth_dev *dev,
1142 const struct rte_flow_action_mark *conf,
1143 struct mlx5_flow_dv_modify_hdr_resource *resource,
1144 struct rte_flow_error *error)
1146 struct mlx5_priv *priv = dev->data->dev_private;
1147 rte_be32_t mask = rte_cpu_to_be_32(MLX5_FLOW_MARK_MASK &
1148 priv->sh->dv_mark_mask);
1149 rte_be32_t data = rte_cpu_to_be_32(conf->id) & mask;
1150 struct rte_flow_item item = {
1154 struct field_modify_info reg_c_x[] = {
1160 return rte_flow_error_set(error, EINVAL,
1161 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1162 NULL, "zero mark action mask");
1163 reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1166 MLX5_ASSERT(reg > 0);
1167 if (reg == REG_C_0) {
1168 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
1169 uint32_t shl_c0 = rte_bsf32(msk_c0);
1171 data = rte_cpu_to_be_32(rte_cpu_to_be_32(data) << shl_c0);
1172 mask = rte_cpu_to_be_32(mask) & msk_c0;
1173 mask = rte_cpu_to_be_32(mask << shl_c0);
1175 reg_c_x[0] = (struct field_modify_info){4, 0, reg_to_field[reg]};
1176 return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1177 MLX5_MODIFICATION_TYPE_SET, error);
1181 * Get metadata register index for specified steering domain.
1184 * Pointer to the rte_eth_dev structure.
1186 * Attributes of flow to determine steering domain.
1188 * Pointer to the error structure.
1191 * positive index on success, a negative errno value otherwise
1192 * and rte_errno is set.
1194 static enum modify_reg
1195 flow_dv_get_metadata_reg(struct rte_eth_dev *dev,
1196 const struct rte_flow_attr *attr,
1197 struct rte_flow_error *error)
1200 mlx5_flow_get_reg_id(dev, attr->transfer ?
1204 MLX5_METADATA_RX, 0, error);
1206 return rte_flow_error_set(error,
1207 ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
1208 NULL, "unavailable "
1209 "metadata register");
1214 * Convert SET_META action to DV specification.
1217 * Pointer to the rte_eth_dev structure.
1218 * @param[in,out] resource
1219 * Pointer to the modify-header resource.
1221 * Attributes of flow that includes this item.
1223 * Pointer to action specification.
1225 * Pointer to the error structure.
1228 * 0 on success, a negative errno value otherwise and rte_errno is set.
1231 flow_dv_convert_action_set_meta
1232 (struct rte_eth_dev *dev,
1233 struct mlx5_flow_dv_modify_hdr_resource *resource,
1234 const struct rte_flow_attr *attr,
1235 const struct rte_flow_action_set_meta *conf,
1236 struct rte_flow_error *error)
1238 uint32_t data = conf->data;
1239 uint32_t mask = conf->mask;
1240 struct rte_flow_item item = {
1244 struct field_modify_info reg_c_x[] = {
1247 int reg = flow_dv_get_metadata_reg(dev, attr, error);
1252 * In datapath code there is no endianness
1253 * coversions for perfromance reasons, all
1254 * pattern conversions are done in rte_flow.
1256 if (reg == REG_C_0) {
1257 struct mlx5_priv *priv = dev->data->dev_private;
1258 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
1261 MLX5_ASSERT(msk_c0);
1262 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1263 shl_c0 = rte_bsf32(msk_c0);
1265 shl_c0 = sizeof(msk_c0) * CHAR_BIT - rte_fls_u32(msk_c0);
1269 MLX5_ASSERT(!(~msk_c0 & rte_cpu_to_be_32(mask)));
1271 reg_c_x[0] = (struct field_modify_info){4, 0, reg_to_field[reg]};
1272 /* The routine expects parameters in memory as big-endian ones. */
1273 return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1274 MLX5_MODIFICATION_TYPE_SET, error);
1278 * Convert modify-header set IPv4 DSCP action to DV specification.
1280 * @param[in,out] resource
1281 * Pointer to the modify-header resource.
1283 * Pointer to action specification.
1285 * Pointer to the error structure.
1288 * 0 on success, a negative errno value otherwise and rte_errno is set.
1291 flow_dv_convert_action_modify_ipv4_dscp
1292 (struct mlx5_flow_dv_modify_hdr_resource *resource,
1293 const struct rte_flow_action *action,
1294 struct rte_flow_error *error)
1296 const struct rte_flow_action_set_dscp *conf =
1297 (const struct rte_flow_action_set_dscp *)(action->conf);
1298 struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
1299 struct rte_flow_item_ipv4 ipv4;
1300 struct rte_flow_item_ipv4 ipv4_mask;
1302 memset(&ipv4, 0, sizeof(ipv4));
1303 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
1304 ipv4.hdr.type_of_service = conf->dscp;
1305 ipv4_mask.hdr.type_of_service = RTE_IPV4_HDR_DSCP_MASK >> 2;
1307 item.mask = &ipv4_mask;
1308 return flow_dv_convert_modify_action(&item, modify_ipv4, NULL, resource,
1309 MLX5_MODIFICATION_TYPE_SET, error);
1313 * Convert modify-header set IPv6 DSCP action to DV specification.
1315 * @param[in,out] resource
1316 * Pointer to the modify-header resource.
1318 * Pointer to action specification.
1320 * Pointer to the error structure.
1323 * 0 on success, a negative errno value otherwise and rte_errno is set.
1326 flow_dv_convert_action_modify_ipv6_dscp
1327 (struct mlx5_flow_dv_modify_hdr_resource *resource,
1328 const struct rte_flow_action *action,
1329 struct rte_flow_error *error)
1331 const struct rte_flow_action_set_dscp *conf =
1332 (const struct rte_flow_action_set_dscp *)(action->conf);
1333 struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
1334 struct rte_flow_item_ipv6 ipv6;
1335 struct rte_flow_item_ipv6 ipv6_mask;
1337 memset(&ipv6, 0, sizeof(ipv6));
1338 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
1340 * Even though the DSCP bits offset of IPv6 is not byte aligned,
1341 * rdma-core only accept the DSCP bits byte aligned start from
1342 * bit 0 to 5 as to be compatible with IPv4. No need to shift the
1343 * bits in IPv6 case as rdma-core requires byte aligned value.
1345 ipv6.hdr.vtc_flow = conf->dscp;
1346 ipv6_mask.hdr.vtc_flow = RTE_IPV6_HDR_DSCP_MASK >> 22;
1348 item.mask = &ipv6_mask;
1349 return flow_dv_convert_modify_action(&item, modify_ipv6, NULL, resource,
1350 MLX5_MODIFICATION_TYPE_SET, error);
1354 * Validate MARK item.
1357 * Pointer to the rte_eth_dev structure.
1359 * Item specification.
1361 * Attributes of flow that includes this item.
1363 * Pointer to error structure.
1366 * 0 on success, a negative errno value otherwise and rte_errno is set.
1369 flow_dv_validate_item_mark(struct rte_eth_dev *dev,
1370 const struct rte_flow_item *item,
1371 const struct rte_flow_attr *attr __rte_unused,
1372 struct rte_flow_error *error)
1374 struct mlx5_priv *priv = dev->data->dev_private;
1375 struct mlx5_dev_config *config = &priv->config;
1376 const struct rte_flow_item_mark *spec = item->spec;
1377 const struct rte_flow_item_mark *mask = item->mask;
1378 const struct rte_flow_item_mark nic_mask = {
1379 .id = priv->sh->dv_mark_mask,
1383 if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
1384 return rte_flow_error_set(error, ENOTSUP,
1385 RTE_FLOW_ERROR_TYPE_ITEM, item,
1386 "extended metadata feature"
1388 if (!mlx5_flow_ext_mreg_supported(dev))
1389 return rte_flow_error_set(error, ENOTSUP,
1390 RTE_FLOW_ERROR_TYPE_ITEM, item,
1391 "extended metadata register"
1392 " isn't supported");
1394 return rte_flow_error_set(error, ENOTSUP,
1395 RTE_FLOW_ERROR_TYPE_ITEM, item,
1396 "extended metadata register"
1397 " isn't available");
1398 ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1402 return rte_flow_error_set(error, EINVAL,
1403 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1405 "data cannot be empty");
1406 if (spec->id >= (MLX5_FLOW_MARK_MAX & nic_mask.id))
1407 return rte_flow_error_set(error, EINVAL,
1408 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1410 "mark id exceeds the limit");
1414 return rte_flow_error_set(error, EINVAL,
1415 RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1416 "mask cannot be zero");
1418 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1419 (const uint8_t *)&nic_mask,
1420 sizeof(struct rte_flow_item_mark),
1428 * Validate META item.
1431 * Pointer to the rte_eth_dev structure.
1433 * Item specification.
1435 * Attributes of flow that includes this item.
1437 * Pointer to error structure.
1440 * 0 on success, a negative errno value otherwise and rte_errno is set.
1443 flow_dv_validate_item_meta(struct rte_eth_dev *dev __rte_unused,
1444 const struct rte_flow_item *item,
1445 const struct rte_flow_attr *attr,
1446 struct rte_flow_error *error)
1448 struct mlx5_priv *priv = dev->data->dev_private;
1449 struct mlx5_dev_config *config = &priv->config;
1450 const struct rte_flow_item_meta *spec = item->spec;
1451 const struct rte_flow_item_meta *mask = item->mask;
1452 struct rte_flow_item_meta nic_mask = {
1459 return rte_flow_error_set(error, EINVAL,
1460 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1462 "data cannot be empty");
1463 if (config->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
1464 if (!mlx5_flow_ext_mreg_supported(dev))
1465 return rte_flow_error_set(error, ENOTSUP,
1466 RTE_FLOW_ERROR_TYPE_ITEM, item,
1467 "extended metadata register"
1468 " isn't supported");
1469 reg = flow_dv_get_metadata_reg(dev, attr, error);
1473 return rte_flow_error_set(error, ENOTSUP,
1474 RTE_FLOW_ERROR_TYPE_ITEM, item,
1478 nic_mask.data = priv->sh->dv_meta_mask;
1479 } else if (attr->transfer) {
1480 return rte_flow_error_set(error, ENOTSUP,
1481 RTE_FLOW_ERROR_TYPE_ITEM, item,
1482 "extended metadata feature "
1483 "should be enabled when "
1484 "meta item is requested "
1485 "with e-switch mode ");
1488 mask = &rte_flow_item_meta_mask;
1490 return rte_flow_error_set(error, EINVAL,
1491 RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1492 "mask cannot be zero");
1494 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1495 (const uint8_t *)&nic_mask,
1496 sizeof(struct rte_flow_item_meta),
1502 * Validate TAG item.
1505 * Pointer to the rte_eth_dev structure.
1507 * Item specification.
1509 * Attributes of flow that includes this item.
1511 * Pointer to error structure.
1514 * 0 on success, a negative errno value otherwise and rte_errno is set.
1517 flow_dv_validate_item_tag(struct rte_eth_dev *dev,
1518 const struct rte_flow_item *item,
1519 const struct rte_flow_attr *attr __rte_unused,
1520 struct rte_flow_error *error)
1522 const struct rte_flow_item_tag *spec = item->spec;
1523 const struct rte_flow_item_tag *mask = item->mask;
1524 const struct rte_flow_item_tag nic_mask = {
1525 .data = RTE_BE32(UINT32_MAX),
1530 if (!mlx5_flow_ext_mreg_supported(dev))
1531 return rte_flow_error_set(error, ENOTSUP,
1532 RTE_FLOW_ERROR_TYPE_ITEM, item,
1533 "extensive metadata register"
1534 " isn't supported");
1536 return rte_flow_error_set(error, EINVAL,
1537 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1539 "data cannot be empty");
1541 mask = &rte_flow_item_tag_mask;
1543 return rte_flow_error_set(error, EINVAL,
1544 RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1545 "mask cannot be zero");
1547 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1548 (const uint8_t *)&nic_mask,
1549 sizeof(struct rte_flow_item_tag),
1553 if (mask->index != 0xff)
1554 return rte_flow_error_set(error, EINVAL,
1555 RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1556 "partial mask for tag index"
1557 " is not supported");
1558 ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, spec->index, error);
1561 MLX5_ASSERT(ret != REG_NON);
1566 * Validate vport item.
1569 * Pointer to the rte_eth_dev structure.
1571 * Item specification.
1573 * Attributes of flow that includes this item.
1574 * @param[in] item_flags
1575 * Bit-fields that holds the items detected until now.
1577 * Pointer to error structure.
1580 * 0 on success, a negative errno value otherwise and rte_errno is set.
1583 flow_dv_validate_item_port_id(struct rte_eth_dev *dev,
1584 const struct rte_flow_item *item,
1585 const struct rte_flow_attr *attr,
1586 uint64_t item_flags,
1587 struct rte_flow_error *error)
1589 const struct rte_flow_item_port_id *spec = item->spec;
1590 const struct rte_flow_item_port_id *mask = item->mask;
1591 const struct rte_flow_item_port_id switch_mask = {
1594 struct mlx5_priv *esw_priv;
1595 struct mlx5_priv *dev_priv;
1598 if (!attr->transfer)
1599 return rte_flow_error_set(error, EINVAL,
1600 RTE_FLOW_ERROR_TYPE_ITEM,
1602 "match on port id is valid only"
1603 " when transfer flag is enabled");
1604 if (item_flags & MLX5_FLOW_ITEM_PORT_ID)
1605 return rte_flow_error_set(error, ENOTSUP,
1606 RTE_FLOW_ERROR_TYPE_ITEM, item,
1607 "multiple source ports are not"
1610 mask = &switch_mask;
1611 if (mask->id != 0xffffffff)
1612 return rte_flow_error_set(error, ENOTSUP,
1613 RTE_FLOW_ERROR_TYPE_ITEM_MASK,
1615 "no support for partial mask on"
1617 ret = mlx5_flow_item_acceptable
1618 (item, (const uint8_t *)mask,
1619 (const uint8_t *)&rte_flow_item_port_id_mask,
1620 sizeof(struct rte_flow_item_port_id),
1626 esw_priv = mlx5_port_to_eswitch_info(spec->id, false);
1628 return rte_flow_error_set(error, rte_errno,
1629 RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
1630 "failed to obtain E-Switch info for"
1632 dev_priv = mlx5_dev_to_eswitch_info(dev);
1634 return rte_flow_error_set(error, rte_errno,
1635 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1637 "failed to obtain E-Switch info");
1638 if (esw_priv->domain_id != dev_priv->domain_id)
1639 return rte_flow_error_set(error, EINVAL,
1640 RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
1641 "cannot match on a port from a"
1642 " different E-Switch");
1647 * Validate VLAN item.
1650 * Item specification.
1651 * @param[in] item_flags
1652 * Bit-fields that holds the items detected until now.
1654 * Ethernet device flow is being created on.
1656 * Pointer to error structure.
1659 * 0 on success, a negative errno value otherwise and rte_errno is set.
1662 flow_dv_validate_item_vlan(const struct rte_flow_item *item,
1663 uint64_t item_flags,
1664 struct rte_eth_dev *dev,
1665 struct rte_flow_error *error)
1667 const struct rte_flow_item_vlan *mask = item->mask;
1668 const struct rte_flow_item_vlan nic_mask = {
1669 .tci = RTE_BE16(UINT16_MAX),
1670 .inner_type = RTE_BE16(UINT16_MAX),
1672 const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1674 const uint64_t l34m = tunnel ? (MLX5_FLOW_LAYER_INNER_L3 |
1675 MLX5_FLOW_LAYER_INNER_L4) :
1676 (MLX5_FLOW_LAYER_OUTER_L3 |
1677 MLX5_FLOW_LAYER_OUTER_L4);
1678 const uint64_t vlanm = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
1679 MLX5_FLOW_LAYER_OUTER_VLAN;
1681 if (item_flags & vlanm)
1682 return rte_flow_error_set(error, EINVAL,
1683 RTE_FLOW_ERROR_TYPE_ITEM, item,
1684 "multiple VLAN layers not supported");
1685 else if ((item_flags & l34m) != 0)
1686 return rte_flow_error_set(error, EINVAL,
1687 RTE_FLOW_ERROR_TYPE_ITEM, item,
1688 "VLAN cannot follow L3/L4 layer");
1690 mask = &rte_flow_item_vlan_mask;
1691 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1692 (const uint8_t *)&nic_mask,
1693 sizeof(struct rte_flow_item_vlan),
1697 if (!tunnel && mask->tci != RTE_BE16(0x0fff)) {
1698 struct mlx5_priv *priv = dev->data->dev_private;
1700 if (priv->vmwa_context) {
1702 * Non-NULL context means we have a virtual machine
1703 * and SR-IOV enabled, we have to create VLAN interface
1704 * to make hypervisor to setup E-Switch vport
1705 * context correctly. We avoid creating the multiple
1706 * VLAN interfaces, so we cannot support VLAN tag mask.
1708 return rte_flow_error_set(error, EINVAL,
1709 RTE_FLOW_ERROR_TYPE_ITEM,
1711 "VLAN tag mask is not"
1712 " supported in virtual"
1720 * GTP flags are contained in 1 byte of the format:
1721 * -------------------------------------------
1722 * | bit | 0 - 2 | 3 | 4 | 5 | 6 | 7 |
1723 * |-----------------------------------------|
1724 * | value | Version | PT | Res | E | S | PN |
1725 * -------------------------------------------
1727 * Matching is supported only for GTP flags E, S, PN.
1729 #define MLX5_GTP_FLAGS_MASK 0x07
1732 * Validate GTP item.
1735 * Pointer to the rte_eth_dev structure.
1737 * Item specification.
1738 * @param[in] item_flags
1739 * Bit-fields that holds the items detected until now.
1741 * Pointer to error structure.
1744 * 0 on success, a negative errno value otherwise and rte_errno is set.
1747 flow_dv_validate_item_gtp(struct rte_eth_dev *dev,
1748 const struct rte_flow_item *item,
1749 uint64_t item_flags,
1750 struct rte_flow_error *error)
1752 struct mlx5_priv *priv = dev->data->dev_private;
1753 const struct rte_flow_item_gtp *spec = item->spec;
1754 const struct rte_flow_item_gtp *mask = item->mask;
1755 const struct rte_flow_item_gtp nic_mask = {
1756 .v_pt_rsv_flags = MLX5_GTP_FLAGS_MASK,
1758 .teid = RTE_BE32(0xffffffff),
1761 if (!priv->config.hca_attr.tunnel_stateless_gtp)
1762 return rte_flow_error_set(error, ENOTSUP,
1763 RTE_FLOW_ERROR_TYPE_ITEM, item,
1764 "GTP support is not enabled");
1765 if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1766 return rte_flow_error_set(error, ENOTSUP,
1767 RTE_FLOW_ERROR_TYPE_ITEM, item,
1768 "multiple tunnel layers not"
1770 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1771 return rte_flow_error_set(error, EINVAL,
1772 RTE_FLOW_ERROR_TYPE_ITEM, item,
1773 "no outer UDP layer found");
1775 mask = &rte_flow_item_gtp_mask;
1776 if (spec && spec->v_pt_rsv_flags & ~MLX5_GTP_FLAGS_MASK)
1777 return rte_flow_error_set(error, ENOTSUP,
1778 RTE_FLOW_ERROR_TYPE_ITEM, item,
1779 "Match is supported for GTP"
1781 return mlx5_flow_item_acceptable
1782 (item, (const uint8_t *)mask,
1783 (const uint8_t *)&nic_mask,
1784 sizeof(struct rte_flow_item_gtp),
1789 * Validate the pop VLAN action.
1792 * Pointer to the rte_eth_dev structure.
1793 * @param[in] action_flags
1794 * Holds the actions detected until now.
1796 * Pointer to the pop vlan action.
1797 * @param[in] item_flags
1798 * The items found in this flow rule.
1800 * Pointer to flow attributes.
1802 * Pointer to error structure.
1805 * 0 on success, a negative errno value otherwise and rte_errno is set.
1808 flow_dv_validate_action_pop_vlan(struct rte_eth_dev *dev,
1809 uint64_t action_flags,
1810 const struct rte_flow_action *action,
1811 uint64_t item_flags,
1812 const struct rte_flow_attr *attr,
1813 struct rte_flow_error *error)
1815 const struct mlx5_priv *priv = dev->data->dev_private;
1819 if (!priv->sh->pop_vlan_action)
1820 return rte_flow_error_set(error, ENOTSUP,
1821 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1823 "pop vlan action is not supported");
1825 return rte_flow_error_set(error, ENOTSUP,
1826 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
1828 "pop vlan action not supported for "
1830 if (action_flags & MLX5_FLOW_VLAN_ACTIONS)
1831 return rte_flow_error_set(error, ENOTSUP,
1832 RTE_FLOW_ERROR_TYPE_ACTION, action,
1833 "no support for multiple VLAN "
1835 /* Pop VLAN with preceding Decap requires inner header with VLAN. */
1836 if ((action_flags & MLX5_FLOW_ACTION_DECAP) &&
1837 !(item_flags & MLX5_FLOW_LAYER_INNER_VLAN))
1838 return rte_flow_error_set(error, ENOTSUP,
1839 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1841 "cannot pop vlan after decap without "
1842 "match on inner vlan in the flow");
1843 /* Pop VLAN without preceding Decap requires outer header with VLAN. */
1844 if (!(action_flags & MLX5_FLOW_ACTION_DECAP) &&
1845 !(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
1846 return rte_flow_error_set(error, ENOTSUP,
1847 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1849 "cannot pop vlan without a "
1850 "match on (outer) vlan in the flow");
1851 if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1852 return rte_flow_error_set(error, EINVAL,
1853 RTE_FLOW_ERROR_TYPE_ACTION, action,
1854 "wrong action order, port_id should "
1855 "be after pop VLAN action");
1856 if (!attr->transfer && priv->representor)
1857 return rte_flow_error_set(error, ENOTSUP,
1858 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1859 "pop vlan action for VF representor "
1860 "not supported on NIC table");
1865 * Get VLAN default info from vlan match info.
1868 * the list of item specifications.
1870 * pointer VLAN info to fill to.
1873 * 0 on success, a negative errno value otherwise and rte_errno is set.
1876 flow_dev_get_vlan_info_from_items(const struct rte_flow_item *items,
1877 struct rte_vlan_hdr *vlan)
1879 const struct rte_flow_item_vlan nic_mask = {
1880 .tci = RTE_BE16(MLX5DV_FLOW_VLAN_PCP_MASK |
1881 MLX5DV_FLOW_VLAN_VID_MASK),
1882 .inner_type = RTE_BE16(0xffff),
1887 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1888 int type = items->type;
1890 if (type == RTE_FLOW_ITEM_TYPE_VLAN ||
1891 type == MLX5_RTE_FLOW_ITEM_TYPE_VLAN)
1894 if (items->type != RTE_FLOW_ITEM_TYPE_END) {
1895 const struct rte_flow_item_vlan *vlan_m = items->mask;
1896 const struct rte_flow_item_vlan *vlan_v = items->spec;
1898 /* If VLAN item in pattern doesn't contain data, return here. */
1903 /* Only full match values are accepted */
1904 if ((vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) ==
1905 MLX5DV_FLOW_VLAN_PCP_MASK_BE) {
1906 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_PCP_MASK;
1908 rte_be_to_cpu_16(vlan_v->tci &
1909 MLX5DV_FLOW_VLAN_PCP_MASK_BE);
1911 if ((vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) ==
1912 MLX5DV_FLOW_VLAN_VID_MASK_BE) {
1913 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_VID_MASK;
1915 rte_be_to_cpu_16(vlan_v->tci &
1916 MLX5DV_FLOW_VLAN_VID_MASK_BE);
1918 if (vlan_m->inner_type == nic_mask.inner_type)
1919 vlan->eth_proto = rte_be_to_cpu_16(vlan_v->inner_type &
1920 vlan_m->inner_type);
1925 * Validate the push VLAN action.
1928 * Pointer to the rte_eth_dev structure.
1929 * @param[in] action_flags
1930 * Holds the actions detected until now.
1931 * @param[in] item_flags
1932 * The items found in this flow rule.
1934 * Pointer to the action structure.
1936 * Pointer to flow attributes
1938 * Pointer to error structure.
1941 * 0 on success, a negative errno value otherwise and rte_errno is set.
1944 flow_dv_validate_action_push_vlan(struct rte_eth_dev *dev,
1945 uint64_t action_flags,
1946 const struct rte_flow_item_vlan *vlan_m,
1947 const struct rte_flow_action *action,
1948 const struct rte_flow_attr *attr,
1949 struct rte_flow_error *error)
1951 const struct rte_flow_action_of_push_vlan *push_vlan = action->conf;
1952 const struct mlx5_priv *priv = dev->data->dev_private;
1954 if (push_vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_VLAN) &&
1955 push_vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_QINQ))
1956 return rte_flow_error_set(error, EINVAL,
1957 RTE_FLOW_ERROR_TYPE_ACTION, action,
1958 "invalid vlan ethertype");
1959 if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1960 return rte_flow_error_set(error, EINVAL,
1961 RTE_FLOW_ERROR_TYPE_ACTION, action,
1962 "wrong action order, port_id should "
1963 "be after push VLAN");
1964 if (!attr->transfer && priv->representor)
1965 return rte_flow_error_set(error, ENOTSUP,
1966 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1967 "push vlan action for VF representor "
1968 "not supported on NIC table");
1970 (vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) &&
1971 (vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) !=
1972 MLX5DV_FLOW_VLAN_PCP_MASK_BE &&
1973 !(action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_PCP) &&
1974 !(mlx5_flow_find_action
1975 (action + 1, RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP)))
1976 return rte_flow_error_set(error, EINVAL,
1977 RTE_FLOW_ERROR_TYPE_ACTION, action,
1978 "not full match mask on VLAN PCP and "
1979 "there is no of_set_vlan_pcp action, "
1980 "push VLAN action cannot figure out "
1983 (vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) &&
1984 (vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) !=
1985 MLX5DV_FLOW_VLAN_VID_MASK_BE &&
1986 !(action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_VID) &&
1987 !(mlx5_flow_find_action
1988 (action + 1, RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID)))
1989 return rte_flow_error_set(error, EINVAL,
1990 RTE_FLOW_ERROR_TYPE_ACTION, action,
1991 "not full match mask on VLAN VID and "
1992 "there is no of_set_vlan_vid action, "
1993 "push VLAN action cannot figure out "
2000 * Validate the set VLAN PCP.
2002 * @param[in] action_flags
2003 * Holds the actions detected until now.
2004 * @param[in] actions
2005 * Pointer to the list of actions remaining in the flow rule.
2007 * Pointer to error structure.
2010 * 0 on success, a negative errno value otherwise and rte_errno is set.
2013 flow_dv_validate_action_set_vlan_pcp(uint64_t action_flags,
2014 const struct rte_flow_action actions[],
2015 struct rte_flow_error *error)
2017 const struct rte_flow_action *action = actions;
2018 const struct rte_flow_action_of_set_vlan_pcp *conf = action->conf;
2020 if (conf->vlan_pcp > 7)
2021 return rte_flow_error_set(error, EINVAL,
2022 RTE_FLOW_ERROR_TYPE_ACTION, action,
2023 "VLAN PCP value is too big");
2024 if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN))
2025 return rte_flow_error_set(error, ENOTSUP,
2026 RTE_FLOW_ERROR_TYPE_ACTION, action,
2027 "set VLAN PCP action must follow "
2028 "the push VLAN action");
2029 if (action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_PCP)
2030 return rte_flow_error_set(error, ENOTSUP,
2031 RTE_FLOW_ERROR_TYPE_ACTION, action,
2032 "Multiple VLAN PCP modification are "
2034 if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
2035 return rte_flow_error_set(error, EINVAL,
2036 RTE_FLOW_ERROR_TYPE_ACTION, action,
2037 "wrong action order, port_id should "
2038 "be after set VLAN PCP");
2043 * Validate the set VLAN VID.
2045 * @param[in] item_flags
2046 * Holds the items detected in this rule.
2047 * @param[in] action_flags
2048 * Holds the actions detected until now.
2049 * @param[in] actions
2050 * Pointer to the list of actions remaining in the flow rule.
2052 * Pointer to error structure.
2055 * 0 on success, a negative errno value otherwise and rte_errno is set.
2058 flow_dv_validate_action_set_vlan_vid(uint64_t item_flags,
2059 uint64_t action_flags,
2060 const struct rte_flow_action actions[],
2061 struct rte_flow_error *error)
2063 const struct rte_flow_action *action = actions;
2064 const struct rte_flow_action_of_set_vlan_vid *conf = action->conf;
2066 if (rte_be_to_cpu_16(conf->vlan_vid) > 0xFFE)
2067 return rte_flow_error_set(error, EINVAL,
2068 RTE_FLOW_ERROR_TYPE_ACTION, action,
2069 "VLAN VID value is too big");
2070 if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN) &&
2071 !(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
2072 return rte_flow_error_set(error, ENOTSUP,
2073 RTE_FLOW_ERROR_TYPE_ACTION, action,
2074 "set VLAN VID action must follow push"
2075 " VLAN action or match on VLAN item");
2076 if (action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_VID)
2077 return rte_flow_error_set(error, ENOTSUP,
2078 RTE_FLOW_ERROR_TYPE_ACTION, action,
2079 "Multiple VLAN VID modifications are "
2081 if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
2082 return rte_flow_error_set(error, EINVAL,
2083 RTE_FLOW_ERROR_TYPE_ACTION, action,
2084 "wrong action order, port_id should "
2085 "be after set VLAN VID");
2090 * Validate the FLAG action.
2093 * Pointer to the rte_eth_dev structure.
2094 * @param[in] action_flags
2095 * Holds the actions detected until now.
2097 * Pointer to flow attributes
2099 * Pointer to error structure.
2102 * 0 on success, a negative errno value otherwise and rte_errno is set.
2105 flow_dv_validate_action_flag(struct rte_eth_dev *dev,
2106 uint64_t action_flags,
2107 const struct rte_flow_attr *attr,
2108 struct rte_flow_error *error)
2110 struct mlx5_priv *priv = dev->data->dev_private;
2111 struct mlx5_dev_config *config = &priv->config;
2114 /* Fall back if no extended metadata register support. */
2115 if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
2116 return mlx5_flow_validate_action_flag(action_flags, attr,
2118 /* Extensive metadata mode requires registers. */
2119 if (!mlx5_flow_ext_mreg_supported(dev))
2120 return rte_flow_error_set(error, ENOTSUP,
2121 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2122 "no metadata registers "
2123 "to support flag action");
2124 if (!(priv->sh->dv_mark_mask & MLX5_FLOW_MARK_DEFAULT))
2125 return rte_flow_error_set(error, ENOTSUP,
2126 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2127 "extended metadata register"
2128 " isn't available");
2129 ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
2132 MLX5_ASSERT(ret > 0);
2133 if (action_flags & MLX5_FLOW_ACTION_MARK)
2134 return rte_flow_error_set(error, EINVAL,
2135 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2136 "can't mark and flag in same flow");
2137 if (action_flags & MLX5_FLOW_ACTION_FLAG)
2138 return rte_flow_error_set(error, EINVAL,
2139 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2141 " actions in same flow");
2146 * Validate MARK action.
2149 * Pointer to the rte_eth_dev structure.
2151 * Pointer to action.
2152 * @param[in] action_flags
2153 * Holds the actions detected until now.
2155 * Pointer to flow attributes
2157 * Pointer to error structure.
2160 * 0 on success, a negative errno value otherwise and rte_errno is set.
2163 flow_dv_validate_action_mark(struct rte_eth_dev *dev,
2164 const struct rte_flow_action *action,
2165 uint64_t action_flags,
2166 const struct rte_flow_attr *attr,
2167 struct rte_flow_error *error)
2169 struct mlx5_priv *priv = dev->data->dev_private;
2170 struct mlx5_dev_config *config = &priv->config;
2171 const struct rte_flow_action_mark *mark = action->conf;
2174 /* Fall back if no extended metadata register support. */
2175 if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
2176 return mlx5_flow_validate_action_mark(action, action_flags,
2178 /* Extensive metadata mode requires registers. */
2179 if (!mlx5_flow_ext_mreg_supported(dev))
2180 return rte_flow_error_set(error, ENOTSUP,
2181 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2182 "no metadata registers "
2183 "to support mark action");
2184 if (!priv->sh->dv_mark_mask)
2185 return rte_flow_error_set(error, ENOTSUP,
2186 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2187 "extended metadata register"
2188 " isn't available");
2189 ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
2192 MLX5_ASSERT(ret > 0);
2194 return rte_flow_error_set(error, EINVAL,
2195 RTE_FLOW_ERROR_TYPE_ACTION, action,
2196 "configuration cannot be null");
2197 if (mark->id >= (MLX5_FLOW_MARK_MAX & priv->sh->dv_mark_mask))
2198 return rte_flow_error_set(error, EINVAL,
2199 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2201 "mark id exceeds the limit");
2202 if (action_flags & MLX5_FLOW_ACTION_FLAG)
2203 return rte_flow_error_set(error, EINVAL,
2204 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2205 "can't flag and mark in same flow");
2206 if (action_flags & MLX5_FLOW_ACTION_MARK)
2207 return rte_flow_error_set(error, EINVAL,
2208 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2209 "can't have 2 mark actions in same"
2215 * Validate SET_META action.
2218 * Pointer to the rte_eth_dev structure.
2220 * Pointer to the action structure.
2221 * @param[in] action_flags
2222 * Holds the actions detected until now.
2224 * Pointer to flow attributes
2226 * Pointer to error structure.
2229 * 0 on success, a negative errno value otherwise and rte_errno is set.
2232 flow_dv_validate_action_set_meta(struct rte_eth_dev *dev,
2233 const struct rte_flow_action *action,
2234 uint64_t action_flags __rte_unused,
2235 const struct rte_flow_attr *attr,
2236 struct rte_flow_error *error)
2238 const struct rte_flow_action_set_meta *conf;
2239 uint32_t nic_mask = UINT32_MAX;
2242 if (!mlx5_flow_ext_mreg_supported(dev))
2243 return rte_flow_error_set(error, ENOTSUP,
2244 RTE_FLOW_ERROR_TYPE_ACTION, action,
2245 "extended metadata register"
2246 " isn't supported");
2247 reg = flow_dv_get_metadata_reg(dev, attr, error);
2250 if (reg != REG_A && reg != REG_B) {
2251 struct mlx5_priv *priv = dev->data->dev_private;
2253 nic_mask = priv->sh->dv_meta_mask;
2255 if (!(action->conf))
2256 return rte_flow_error_set(error, EINVAL,
2257 RTE_FLOW_ERROR_TYPE_ACTION, action,
2258 "configuration cannot be null");
2259 conf = (const struct rte_flow_action_set_meta *)action->conf;
2261 return rte_flow_error_set(error, EINVAL,
2262 RTE_FLOW_ERROR_TYPE_ACTION, action,
2263 "zero mask doesn't have any effect");
2264 if (conf->mask & ~nic_mask)
2265 return rte_flow_error_set(error, EINVAL,
2266 RTE_FLOW_ERROR_TYPE_ACTION, action,
2267 "meta data must be within reg C0");
2272 * Validate SET_TAG action.
2275 * Pointer to the rte_eth_dev structure.
2277 * Pointer to the action structure.
2278 * @param[in] action_flags
2279 * Holds the actions detected until now.
2281 * Pointer to flow attributes
2283 * Pointer to error structure.
2286 * 0 on success, a negative errno value otherwise and rte_errno is set.
2289 flow_dv_validate_action_set_tag(struct rte_eth_dev *dev,
2290 const struct rte_flow_action *action,
2291 uint64_t action_flags,
2292 const struct rte_flow_attr *attr,
2293 struct rte_flow_error *error)
2295 const struct rte_flow_action_set_tag *conf;
2296 const uint64_t terminal_action_flags =
2297 MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_QUEUE |
2298 MLX5_FLOW_ACTION_RSS;
2301 if (!mlx5_flow_ext_mreg_supported(dev))
2302 return rte_flow_error_set(error, ENOTSUP,
2303 RTE_FLOW_ERROR_TYPE_ACTION, action,
2304 "extensive metadata register"
2305 " isn't supported");
2306 if (!(action->conf))
2307 return rte_flow_error_set(error, EINVAL,
2308 RTE_FLOW_ERROR_TYPE_ACTION, action,
2309 "configuration cannot be null");
2310 conf = (const struct rte_flow_action_set_tag *)action->conf;
2312 return rte_flow_error_set(error, EINVAL,
2313 RTE_FLOW_ERROR_TYPE_ACTION, action,
2314 "zero mask doesn't have any effect");
2315 ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, conf->index, error);
2318 if (!attr->transfer && attr->ingress &&
2319 (action_flags & terminal_action_flags))
2320 return rte_flow_error_set(error, EINVAL,
2321 RTE_FLOW_ERROR_TYPE_ACTION, action,
2322 "set_tag has no effect"
2323 " with terminal actions");
2328 * Validate count action.
2331 * Pointer to rte_eth_dev structure.
2333 * Pointer to error structure.
2336 * 0 on success, a negative errno value otherwise and rte_errno is set.
2339 flow_dv_validate_action_count(struct rte_eth_dev *dev,
2340 struct rte_flow_error *error)
2342 struct mlx5_priv *priv = dev->data->dev_private;
2344 if (!priv->config.devx)
2346 #ifdef HAVE_IBV_FLOW_DEVX_COUNTERS
2350 return rte_flow_error_set
2352 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2354 "count action not supported");
2358 * Validate the L2 encap action.
2361 * Pointer to the rte_eth_dev structure.
2362 * @param[in] action_flags
2363 * Holds the actions detected until now.
2365 * Pointer to the action structure.
2367 * Pointer to flow attributes.
2369 * Pointer to error structure.
2372 * 0 on success, a negative errno value otherwise and rte_errno is set.
2375 flow_dv_validate_action_l2_encap(struct rte_eth_dev *dev,
2376 uint64_t action_flags,
2377 const struct rte_flow_action *action,
2378 const struct rte_flow_attr *attr,
2379 struct rte_flow_error *error)
2381 const struct mlx5_priv *priv = dev->data->dev_private;
2383 if (!(action->conf))
2384 return rte_flow_error_set(error, EINVAL,
2385 RTE_FLOW_ERROR_TYPE_ACTION, action,
2386 "configuration cannot be null");
2387 if (action_flags & MLX5_FLOW_ACTION_ENCAP)
2388 return rte_flow_error_set(error, EINVAL,
2389 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2390 "can only have a single encap action "
2392 if (!attr->transfer && priv->representor)
2393 return rte_flow_error_set(error, ENOTSUP,
2394 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2395 "encap action for VF representor "
2396 "not supported on NIC table");
2401 * Validate a decap action.
2404 * Pointer to the rte_eth_dev structure.
2405 * @param[in] action_flags
2406 * Holds the actions detected until now.
2408 * Pointer to flow attributes
2410 * Pointer to error structure.
2413 * 0 on success, a negative errno value otherwise and rte_errno is set.
2416 flow_dv_validate_action_decap(struct rte_eth_dev *dev,
2417 uint64_t action_flags,
2418 const struct rte_flow_attr *attr,
2419 struct rte_flow_error *error)
2421 const struct mlx5_priv *priv = dev->data->dev_private;
2423 if (priv->config.hca_attr.scatter_fcs_w_decap_disable &&
2424 !priv->config.decap_en)
2425 return rte_flow_error_set(error, ENOTSUP,
2426 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2427 "decap is not enabled");
2428 if (action_flags & MLX5_FLOW_XCAP_ACTIONS)
2429 return rte_flow_error_set(error, ENOTSUP,
2430 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2432 MLX5_FLOW_ACTION_DECAP ? "can only "
2433 "have a single decap action" : "decap "
2434 "after encap is not supported");
2435 if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS)
2436 return rte_flow_error_set(error, EINVAL,
2437 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2438 "can't have decap action after"
2441 return rte_flow_error_set(error, ENOTSUP,
2442 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
2444 "decap action not supported for "
2446 if (!attr->transfer && priv->representor)
2447 return rte_flow_error_set(error, ENOTSUP,
2448 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2449 "decap action for VF representor "
2450 "not supported on NIC table");
2454 const struct rte_flow_action_raw_decap empty_decap = {.data = NULL, .size = 0,};
2457 * Validate the raw encap and decap actions.
2460 * Pointer to the rte_eth_dev structure.
2462 * Pointer to the decap action.
2464 * Pointer to the encap action.
2466 * Pointer to flow attributes
2467 * @param[in/out] action_flags
2468 * Holds the actions detected until now.
2469 * @param[out] actions_n
2470 * pointer to the number of actions counter.
2472 * Pointer to error structure.
2475 * 0 on success, a negative errno value otherwise and rte_errno is set.
2478 flow_dv_validate_action_raw_encap_decap
2479 (struct rte_eth_dev *dev,
2480 const struct rte_flow_action_raw_decap *decap,
2481 const struct rte_flow_action_raw_encap *encap,
2482 const struct rte_flow_attr *attr, uint64_t *action_flags,
2483 int *actions_n, struct rte_flow_error *error)
2485 const struct mlx5_priv *priv = dev->data->dev_private;
2488 if (encap && (!encap->size || !encap->data))
2489 return rte_flow_error_set(error, EINVAL,
2490 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2491 "raw encap data cannot be empty");
2492 if (decap && encap) {
2493 if (decap->size <= MLX5_ENCAPSULATION_DECISION_SIZE &&
2494 encap->size > MLX5_ENCAPSULATION_DECISION_SIZE)
2497 else if (encap->size <=
2498 MLX5_ENCAPSULATION_DECISION_SIZE &&
2500 MLX5_ENCAPSULATION_DECISION_SIZE)
2503 else if (encap->size >
2504 MLX5_ENCAPSULATION_DECISION_SIZE &&
2506 MLX5_ENCAPSULATION_DECISION_SIZE)
2507 /* 2 L2 actions: encap and decap. */
2510 return rte_flow_error_set(error,
2512 RTE_FLOW_ERROR_TYPE_ACTION,
2513 NULL, "unsupported too small "
2514 "raw decap and too small raw "
2515 "encap combination");
2518 ret = flow_dv_validate_action_decap(dev, *action_flags, attr,
2522 *action_flags |= MLX5_FLOW_ACTION_DECAP;
2526 if (encap->size <= MLX5_ENCAPSULATION_DECISION_SIZE)
2527 return rte_flow_error_set(error, ENOTSUP,
2528 RTE_FLOW_ERROR_TYPE_ACTION,
2530 "small raw encap size");
2531 if (*action_flags & MLX5_FLOW_ACTION_ENCAP)
2532 return rte_flow_error_set(error, EINVAL,
2533 RTE_FLOW_ERROR_TYPE_ACTION,
2535 "more than one encap action");
2536 if (!attr->transfer && priv->representor)
2537 return rte_flow_error_set
2539 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2540 "encap action for VF representor "
2541 "not supported on NIC table");
2542 *action_flags |= MLX5_FLOW_ACTION_ENCAP;
2549 * Find existing encap/decap resource or create and register a new one.
2551 * @param[in, out] dev
2552 * Pointer to rte_eth_dev structure.
2553 * @param[in, out] resource
2554 * Pointer to encap/decap resource.
2555 * @parm[in, out] dev_flow
2556 * Pointer to the dev_flow.
2558 * pointer to error structure.
2561 * 0 on success otherwise -errno and errno is set.
2564 flow_dv_encap_decap_resource_register
2565 (struct rte_eth_dev *dev,
2566 struct mlx5_flow_dv_encap_decap_resource *resource,
2567 struct mlx5_flow *dev_flow,
2568 struct rte_flow_error *error)
2570 struct mlx5_priv *priv = dev->data->dev_private;
2571 struct mlx5_dev_ctx_shared *sh = priv->sh;
2572 struct mlx5_flow_dv_encap_decap_resource *cache_resource;
2573 struct mlx5dv_dr_domain *domain;
2577 resource->flags = dev_flow->dv.group ? 0 : 1;
2578 if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
2579 domain = sh->fdb_domain;
2580 else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
2581 domain = sh->rx_domain;
2583 domain = sh->tx_domain;
2584 /* Lookup a matching resource from cache. */
2585 ILIST_FOREACH(sh->ipool[MLX5_IPOOL_DECAP_ENCAP], sh->encaps_decaps, idx,
2586 cache_resource, next) {
2587 if (resource->reformat_type == cache_resource->reformat_type &&
2588 resource->ft_type == cache_resource->ft_type &&
2589 resource->flags == cache_resource->flags &&
2590 resource->size == cache_resource->size &&
2591 !memcmp((const void *)resource->buf,
2592 (const void *)cache_resource->buf,
2594 DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d++",
2595 (void *)cache_resource,
2596 rte_atomic32_read(&cache_resource->refcnt));
2597 rte_atomic32_inc(&cache_resource->refcnt);
2598 dev_flow->handle->dvh.rix_encap_decap = idx;
2599 dev_flow->dv.encap_decap = cache_resource;
2603 /* Register new encap/decap resource. */
2604 cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_DECAP_ENCAP],
2605 &dev_flow->handle->dvh.rix_encap_decap);
2606 if (!cache_resource)
2607 return rte_flow_error_set(error, ENOMEM,
2608 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2609 "cannot allocate resource memory");
2610 *cache_resource = *resource;
2611 ret = mlx5_flow_os_create_flow_action_packet_reformat
2612 (sh->ctx, domain, cache_resource,
2613 &cache_resource->action);
2615 mlx5_free(cache_resource);
2616 return rte_flow_error_set(error, ENOMEM,
2617 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2618 NULL, "cannot create action");
2620 rte_atomic32_init(&cache_resource->refcnt);
2621 rte_atomic32_inc(&cache_resource->refcnt);
2622 ILIST_INSERT(sh->ipool[MLX5_IPOOL_DECAP_ENCAP], &sh->encaps_decaps,
2623 dev_flow->handle->dvh.rix_encap_decap, cache_resource,
2625 dev_flow->dv.encap_decap = cache_resource;
2626 DRV_LOG(DEBUG, "new encap/decap resource %p: refcnt %d++",
2627 (void *)cache_resource,
2628 rte_atomic32_read(&cache_resource->refcnt));
2633 * Find existing table jump resource or create and register a new one.
2635 * @param[in, out] dev
2636 * Pointer to rte_eth_dev structure.
2637 * @param[in, out] tbl
2638 * Pointer to flow table resource.
2639 * @parm[in, out] dev_flow
2640 * Pointer to the dev_flow.
2642 * pointer to error structure.
2645 * 0 on success otherwise -errno and errno is set.
2648 flow_dv_jump_tbl_resource_register
2649 (struct rte_eth_dev *dev __rte_unused,
2650 struct mlx5_flow_tbl_resource *tbl,
2651 struct mlx5_flow *dev_flow,
2652 struct rte_flow_error *error)
2654 struct mlx5_flow_tbl_data_entry *tbl_data =
2655 container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
2659 cnt = rte_atomic32_read(&tbl_data->jump.refcnt);
2661 ret = mlx5_flow_os_create_flow_action_dest_flow_tbl
2662 (tbl->obj, &tbl_data->jump.action);
2664 return rte_flow_error_set(error, ENOMEM,
2665 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2666 NULL, "cannot create jump action");
2667 DRV_LOG(DEBUG, "new jump table resource %p: refcnt %d++",
2668 (void *)&tbl_data->jump, cnt);
2670 /* old jump should not make the table ref++. */
2671 flow_dv_tbl_resource_release(dev, &tbl_data->tbl);
2672 MLX5_ASSERT(tbl_data->jump.action);
2673 DRV_LOG(DEBUG, "existed jump table resource %p: refcnt %d++",
2674 (void *)&tbl_data->jump, cnt);
2676 rte_atomic32_inc(&tbl_data->jump.refcnt);
2677 dev_flow->handle->rix_jump = tbl_data->idx;
2678 dev_flow->dv.jump = &tbl_data->jump;
2683 * Find existing default miss resource or create and register a new one.
2685 * @param[in, out] dev
2686 * Pointer to rte_eth_dev structure.
2688 * pointer to error structure.
2691 * 0 on success otherwise -errno and errno is set.
2694 flow_dv_default_miss_resource_register(struct rte_eth_dev *dev,
2695 struct rte_flow_error *error)
2697 struct mlx5_priv *priv = dev->data->dev_private;
2698 struct mlx5_dev_ctx_shared *sh = priv->sh;
2699 struct mlx5_flow_default_miss_resource *cache_resource =
2701 int cnt = rte_atomic32_read(&cache_resource->refcnt);
2704 MLX5_ASSERT(cache_resource->action);
2705 cache_resource->action =
2706 mlx5_glue->dr_create_flow_action_default_miss();
2707 if (!cache_resource->action)
2708 return rte_flow_error_set(error, ENOMEM,
2709 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2710 "cannot create default miss action");
2711 DRV_LOG(DEBUG, "new default miss resource %p: refcnt %d++",
2712 (void *)cache_resource->action, cnt);
2714 rte_atomic32_inc(&cache_resource->refcnt);
2719 * Find existing table port ID resource or create and register a new one.
2721 * @param[in, out] dev
2722 * Pointer to rte_eth_dev structure.
2723 * @param[in, out] resource
2724 * Pointer to port ID action resource.
2725 * @parm[in, out] dev_flow
2726 * Pointer to the dev_flow.
2728 * pointer to error structure.
2731 * 0 on success otherwise -errno and errno is set.
2734 flow_dv_port_id_action_resource_register
2735 (struct rte_eth_dev *dev,
2736 struct mlx5_flow_dv_port_id_action_resource *resource,
2737 struct mlx5_flow *dev_flow,
2738 struct rte_flow_error *error)
2740 struct mlx5_priv *priv = dev->data->dev_private;
2741 struct mlx5_dev_ctx_shared *sh = priv->sh;
2742 struct mlx5_flow_dv_port_id_action_resource *cache_resource;
2746 /* Lookup a matching resource from cache. */
2747 ILIST_FOREACH(sh->ipool[MLX5_IPOOL_PORT_ID], sh->port_id_action_list,
2748 idx, cache_resource, next) {
2749 if (resource->port_id == cache_resource->port_id) {
2750 DRV_LOG(DEBUG, "port id action resource resource %p: "
2752 (void *)cache_resource,
2753 rte_atomic32_read(&cache_resource->refcnt));
2754 rte_atomic32_inc(&cache_resource->refcnt);
2755 dev_flow->handle->rix_port_id_action = idx;
2756 dev_flow->dv.port_id_action = cache_resource;
2760 /* Register new port id action resource. */
2761 cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_PORT_ID],
2762 &dev_flow->handle->rix_port_id_action);
2763 if (!cache_resource)
2764 return rte_flow_error_set(error, ENOMEM,
2765 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2766 "cannot allocate resource memory");
2767 *cache_resource = *resource;
2768 ret = mlx5_flow_os_create_flow_action_dest_port
2769 (priv->sh->fdb_domain, resource->port_id,
2770 &cache_resource->action);
2772 mlx5_free(cache_resource);
2773 return rte_flow_error_set(error, ENOMEM,
2774 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2775 NULL, "cannot create action");
2777 rte_atomic32_init(&cache_resource->refcnt);
2778 rte_atomic32_inc(&cache_resource->refcnt);
2779 ILIST_INSERT(sh->ipool[MLX5_IPOOL_PORT_ID], &sh->port_id_action_list,
2780 dev_flow->handle->rix_port_id_action, cache_resource,
2782 dev_flow->dv.port_id_action = cache_resource;
2783 DRV_LOG(DEBUG, "new port id action resource %p: refcnt %d++",
2784 (void *)cache_resource,
2785 rte_atomic32_read(&cache_resource->refcnt));
2790 * Find existing push vlan resource or create and register a new one.
2792 * @param [in, out] dev
2793 * Pointer to rte_eth_dev structure.
2794 * @param[in, out] resource
2795 * Pointer to port ID action resource.
2796 * @parm[in, out] dev_flow
2797 * Pointer to the dev_flow.
2799 * pointer to error structure.
2802 * 0 on success otherwise -errno and errno is set.
2805 flow_dv_push_vlan_action_resource_register
2806 (struct rte_eth_dev *dev,
2807 struct mlx5_flow_dv_push_vlan_action_resource *resource,
2808 struct mlx5_flow *dev_flow,
2809 struct rte_flow_error *error)
2811 struct mlx5_priv *priv = dev->data->dev_private;
2812 struct mlx5_dev_ctx_shared *sh = priv->sh;
2813 struct mlx5_flow_dv_push_vlan_action_resource *cache_resource;
2814 struct mlx5dv_dr_domain *domain;
2818 /* Lookup a matching resource from cache. */
2819 ILIST_FOREACH(sh->ipool[MLX5_IPOOL_PUSH_VLAN],
2820 sh->push_vlan_action_list, idx, cache_resource, next) {
2821 if (resource->vlan_tag == cache_resource->vlan_tag &&
2822 resource->ft_type == cache_resource->ft_type) {
2823 DRV_LOG(DEBUG, "push-VLAN action resource resource %p: "
2825 (void *)cache_resource,
2826 rte_atomic32_read(&cache_resource->refcnt));
2827 rte_atomic32_inc(&cache_resource->refcnt);
2828 dev_flow->handle->dvh.rix_push_vlan = idx;
2829 dev_flow->dv.push_vlan_res = cache_resource;
2833 /* Register new push_vlan action resource. */
2834 cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_PUSH_VLAN],
2835 &dev_flow->handle->dvh.rix_push_vlan);
2836 if (!cache_resource)
2837 return rte_flow_error_set(error, ENOMEM,
2838 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2839 "cannot allocate resource memory");
2840 *cache_resource = *resource;
2841 if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
2842 domain = sh->fdb_domain;
2843 else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
2844 domain = sh->rx_domain;
2846 domain = sh->tx_domain;
2847 ret = mlx5_flow_os_create_flow_action_push_vlan
2848 (domain, resource->vlan_tag,
2849 &cache_resource->action);
2851 mlx5_free(cache_resource);
2852 return rte_flow_error_set(error, ENOMEM,
2853 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2854 NULL, "cannot create action");
2856 rte_atomic32_init(&cache_resource->refcnt);
2857 rte_atomic32_inc(&cache_resource->refcnt);
2858 ILIST_INSERT(sh->ipool[MLX5_IPOOL_PUSH_VLAN],
2859 &sh->push_vlan_action_list,
2860 dev_flow->handle->dvh.rix_push_vlan,
2861 cache_resource, next);
2862 dev_flow->dv.push_vlan_res = cache_resource;
2863 DRV_LOG(DEBUG, "new push vlan action resource %p: refcnt %d++",
2864 (void *)cache_resource,
2865 rte_atomic32_read(&cache_resource->refcnt));
2869 * Get the size of specific rte_flow_item_type hdr size
2871 * @param[in] item_type
2872 * Tested rte_flow_item_type.
2875 * sizeof struct item_type, 0 if void or irrelevant.
2878 flow_dv_get_item_hdr_len(const enum rte_flow_item_type item_type)
2882 switch (item_type) {
2883 case RTE_FLOW_ITEM_TYPE_ETH:
2884 retval = sizeof(struct rte_ether_hdr);
2886 case RTE_FLOW_ITEM_TYPE_VLAN:
2887 retval = sizeof(struct rte_vlan_hdr);
2889 case RTE_FLOW_ITEM_TYPE_IPV4:
2890 retval = sizeof(struct rte_ipv4_hdr);
2892 case RTE_FLOW_ITEM_TYPE_IPV6:
2893 retval = sizeof(struct rte_ipv6_hdr);
2895 case RTE_FLOW_ITEM_TYPE_UDP:
2896 retval = sizeof(struct rte_udp_hdr);
2898 case RTE_FLOW_ITEM_TYPE_TCP:
2899 retval = sizeof(struct rte_tcp_hdr);
2901 case RTE_FLOW_ITEM_TYPE_VXLAN:
2902 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
2903 retval = sizeof(struct rte_vxlan_hdr);
2905 case RTE_FLOW_ITEM_TYPE_GRE:
2906 case RTE_FLOW_ITEM_TYPE_NVGRE:
2907 retval = sizeof(struct rte_gre_hdr);
2909 case RTE_FLOW_ITEM_TYPE_MPLS:
2910 retval = sizeof(struct rte_mpls_hdr);
2912 case RTE_FLOW_ITEM_TYPE_VOID: /* Fall through. */
2920 #define MLX5_ENCAP_IPV4_VERSION 0x40
2921 #define MLX5_ENCAP_IPV4_IHL_MIN 0x05
2922 #define MLX5_ENCAP_IPV4_TTL_DEF 0x40
2923 #define MLX5_ENCAP_IPV6_VTC_FLOW 0x60000000
2924 #define MLX5_ENCAP_IPV6_HOP_LIMIT 0xff
2925 #define MLX5_ENCAP_VXLAN_FLAGS 0x08000000
2926 #define MLX5_ENCAP_VXLAN_GPE_FLAGS 0x04
2929 * Convert the encap action data from list of rte_flow_item to raw buffer
2932 * Pointer to rte_flow_item objects list.
2934 * Pointer to the output buffer.
2936 * Pointer to the output buffer size.
2938 * Pointer to the error structure.
2941 * 0 on success, a negative errno value otherwise and rte_errno is set.
2944 flow_dv_convert_encap_data(const struct rte_flow_item *items, uint8_t *buf,
2945 size_t *size, struct rte_flow_error *error)
2947 struct rte_ether_hdr *eth = NULL;
2948 struct rte_vlan_hdr *vlan = NULL;
2949 struct rte_ipv4_hdr *ipv4 = NULL;
2950 struct rte_ipv6_hdr *ipv6 = NULL;
2951 struct rte_udp_hdr *udp = NULL;
2952 struct rte_vxlan_hdr *vxlan = NULL;
2953 struct rte_vxlan_gpe_hdr *vxlan_gpe = NULL;
2954 struct rte_gre_hdr *gre = NULL;
2956 size_t temp_size = 0;
2959 return rte_flow_error_set(error, EINVAL,
2960 RTE_FLOW_ERROR_TYPE_ACTION,
2961 NULL, "invalid empty data");
2962 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
2963 len = flow_dv_get_item_hdr_len(items->type);
2964 if (len + temp_size > MLX5_ENCAP_MAX_LEN)
2965 return rte_flow_error_set(error, EINVAL,
2966 RTE_FLOW_ERROR_TYPE_ACTION,
2967 (void *)items->type,
2968 "items total size is too big"
2969 " for encap action");
2970 rte_memcpy((void *)&buf[temp_size], items->spec, len);
2971 switch (items->type) {
2972 case RTE_FLOW_ITEM_TYPE_ETH:
2973 eth = (struct rte_ether_hdr *)&buf[temp_size];
2975 case RTE_FLOW_ITEM_TYPE_VLAN:
2976 vlan = (struct rte_vlan_hdr *)&buf[temp_size];
2978 return rte_flow_error_set(error, EINVAL,
2979 RTE_FLOW_ERROR_TYPE_ACTION,
2980 (void *)items->type,
2981 "eth header not found");
2982 if (!eth->ether_type)
2983 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_VLAN);
2985 case RTE_FLOW_ITEM_TYPE_IPV4:
2986 ipv4 = (struct rte_ipv4_hdr *)&buf[temp_size];
2988 return rte_flow_error_set(error, EINVAL,
2989 RTE_FLOW_ERROR_TYPE_ACTION,
2990 (void *)items->type,
2991 "neither eth nor vlan"
2993 if (vlan && !vlan->eth_proto)
2994 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV4);
2995 else if (eth && !eth->ether_type)
2996 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV4);
2997 if (!ipv4->version_ihl)
2998 ipv4->version_ihl = MLX5_ENCAP_IPV4_VERSION |
2999 MLX5_ENCAP_IPV4_IHL_MIN;
3000 if (!ipv4->time_to_live)
3001 ipv4->time_to_live = MLX5_ENCAP_IPV4_TTL_DEF;
3003 case RTE_FLOW_ITEM_TYPE_IPV6:
3004 ipv6 = (struct rte_ipv6_hdr *)&buf[temp_size];
3006 return rte_flow_error_set(error, EINVAL,
3007 RTE_FLOW_ERROR_TYPE_ACTION,
3008 (void *)items->type,
3009 "neither eth nor vlan"
3011 if (vlan && !vlan->eth_proto)
3012 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV6);
3013 else if (eth && !eth->ether_type)
3014 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV6);
3015 if (!ipv6->vtc_flow)
3017 RTE_BE32(MLX5_ENCAP_IPV6_VTC_FLOW);
3018 if (!ipv6->hop_limits)
3019 ipv6->hop_limits = MLX5_ENCAP_IPV6_HOP_LIMIT;
3021 case RTE_FLOW_ITEM_TYPE_UDP:
3022 udp = (struct rte_udp_hdr *)&buf[temp_size];
3024 return rte_flow_error_set(error, EINVAL,
3025 RTE_FLOW_ERROR_TYPE_ACTION,
3026 (void *)items->type,
3027 "ip header not found");
3028 if (ipv4 && !ipv4->next_proto_id)
3029 ipv4->next_proto_id = IPPROTO_UDP;
3030 else if (ipv6 && !ipv6->proto)
3031 ipv6->proto = IPPROTO_UDP;
3033 case RTE_FLOW_ITEM_TYPE_VXLAN:
3034 vxlan = (struct rte_vxlan_hdr *)&buf[temp_size];
3036 return rte_flow_error_set(error, EINVAL,
3037 RTE_FLOW_ERROR_TYPE_ACTION,
3038 (void *)items->type,
3039 "udp header not found");
3041 udp->dst_port = RTE_BE16(MLX5_UDP_PORT_VXLAN);
3042 if (!vxlan->vx_flags)
3044 RTE_BE32(MLX5_ENCAP_VXLAN_FLAGS);
3046 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
3047 vxlan_gpe = (struct rte_vxlan_gpe_hdr *)&buf[temp_size];
3049 return rte_flow_error_set(error, EINVAL,
3050 RTE_FLOW_ERROR_TYPE_ACTION,
3051 (void *)items->type,
3052 "udp header not found");
3053 if (!vxlan_gpe->proto)
3054 return rte_flow_error_set(error, EINVAL,
3055 RTE_FLOW_ERROR_TYPE_ACTION,
3056 (void *)items->type,
3057 "next protocol not found");
3060 RTE_BE16(MLX5_UDP_PORT_VXLAN_GPE);
3061 if (!vxlan_gpe->vx_flags)
3062 vxlan_gpe->vx_flags =
3063 MLX5_ENCAP_VXLAN_GPE_FLAGS;
3065 case RTE_FLOW_ITEM_TYPE_GRE:
3066 case RTE_FLOW_ITEM_TYPE_NVGRE:
3067 gre = (struct rte_gre_hdr *)&buf[temp_size];
3069 return rte_flow_error_set(error, EINVAL,
3070 RTE_FLOW_ERROR_TYPE_ACTION,
3071 (void *)items->type,
3072 "next protocol not found");
3074 return rte_flow_error_set(error, EINVAL,
3075 RTE_FLOW_ERROR_TYPE_ACTION,
3076 (void *)items->type,
3077 "ip header not found");
3078 if (ipv4 && !ipv4->next_proto_id)
3079 ipv4->next_proto_id = IPPROTO_GRE;
3080 else if (ipv6 && !ipv6->proto)
3081 ipv6->proto = IPPROTO_GRE;
3083 case RTE_FLOW_ITEM_TYPE_VOID:
3086 return rte_flow_error_set(error, EINVAL,
3087 RTE_FLOW_ERROR_TYPE_ACTION,
3088 (void *)items->type,
3089 "unsupported item type");
3099 flow_dv_zero_encap_udp_csum(void *data, struct rte_flow_error *error)
3101 struct rte_ether_hdr *eth = NULL;
3102 struct rte_vlan_hdr *vlan = NULL;
3103 struct rte_ipv6_hdr *ipv6 = NULL;
3104 struct rte_udp_hdr *udp = NULL;
3108 eth = (struct rte_ether_hdr *)data;
3109 next_hdr = (char *)(eth + 1);
3110 proto = RTE_BE16(eth->ether_type);
3113 while (proto == RTE_ETHER_TYPE_VLAN || proto == RTE_ETHER_TYPE_QINQ) {
3114 vlan = (struct rte_vlan_hdr *)next_hdr;
3115 proto = RTE_BE16(vlan->eth_proto);
3116 next_hdr += sizeof(struct rte_vlan_hdr);
3119 /* HW calculates IPv4 csum. no need to proceed */
3120 if (proto == RTE_ETHER_TYPE_IPV4)
3123 /* non IPv4/IPv6 header. not supported */
3124 if (proto != RTE_ETHER_TYPE_IPV6) {
3125 return rte_flow_error_set(error, ENOTSUP,
3126 RTE_FLOW_ERROR_TYPE_ACTION,
3127 NULL, "Cannot offload non IPv4/IPv6");
3130 ipv6 = (struct rte_ipv6_hdr *)next_hdr;
3132 /* ignore non UDP */
3133 if (ipv6->proto != IPPROTO_UDP)
3136 udp = (struct rte_udp_hdr *)(ipv6 + 1);
3137 udp->dgram_cksum = 0;
3143 * Convert L2 encap action to DV specification.
3146 * Pointer to rte_eth_dev structure.
3148 * Pointer to action structure.
3149 * @param[in, out] dev_flow
3150 * Pointer to the mlx5_flow.
3151 * @param[in] transfer
3152 * Mark if the flow is E-Switch flow.
3154 * Pointer to the error structure.
3157 * 0 on success, a negative errno value otherwise and rte_errno is set.
3160 flow_dv_create_action_l2_encap(struct rte_eth_dev *dev,
3161 const struct rte_flow_action *action,
3162 struct mlx5_flow *dev_flow,
3164 struct rte_flow_error *error)
3166 const struct rte_flow_item *encap_data;
3167 const struct rte_flow_action_raw_encap *raw_encap_data;
3168 struct mlx5_flow_dv_encap_decap_resource res = {
3170 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL,
3171 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
3172 MLX5DV_FLOW_TABLE_TYPE_NIC_TX,
3175 if (action->type == RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
3177 (const struct rte_flow_action_raw_encap *)action->conf;
3178 res.size = raw_encap_data->size;
3179 memcpy(res.buf, raw_encap_data->data, res.size);
3181 if (action->type == RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP)
3183 ((const struct rte_flow_action_vxlan_encap *)
3184 action->conf)->definition;
3187 ((const struct rte_flow_action_nvgre_encap *)
3188 action->conf)->definition;
3189 if (flow_dv_convert_encap_data(encap_data, res.buf,
3193 if (flow_dv_zero_encap_udp_csum(res.buf, error))
3195 if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
3196 return rte_flow_error_set(error, EINVAL,
3197 RTE_FLOW_ERROR_TYPE_ACTION,
3198 NULL, "can't create L2 encap action");
3203 * Convert L2 decap action to DV specification.
3206 * Pointer to rte_eth_dev structure.
3207 * @param[in, out] dev_flow
3208 * Pointer to the mlx5_flow.
3209 * @param[in] transfer
3210 * Mark if the flow is E-Switch flow.
3212 * Pointer to the error structure.
3215 * 0 on success, a negative errno value otherwise and rte_errno is set.
3218 flow_dv_create_action_l2_decap(struct rte_eth_dev *dev,
3219 struct mlx5_flow *dev_flow,
3221 struct rte_flow_error *error)
3223 struct mlx5_flow_dv_encap_decap_resource res = {
3226 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2,
3227 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
3228 MLX5DV_FLOW_TABLE_TYPE_NIC_RX,
3231 if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
3232 return rte_flow_error_set(error, EINVAL,
3233 RTE_FLOW_ERROR_TYPE_ACTION,
3234 NULL, "can't create L2 decap action");
3239 * Convert raw decap/encap (L3 tunnel) action to DV specification.
3242 * Pointer to rte_eth_dev structure.
3244 * Pointer to action structure.
3245 * @param[in, out] dev_flow
3246 * Pointer to the mlx5_flow.
3248 * Pointer to the flow attributes.
3250 * Pointer to the error structure.
3253 * 0 on success, a negative errno value otherwise and rte_errno is set.
3256 flow_dv_create_action_raw_encap(struct rte_eth_dev *dev,
3257 const struct rte_flow_action *action,
3258 struct mlx5_flow *dev_flow,
3259 const struct rte_flow_attr *attr,
3260 struct rte_flow_error *error)
3262 const struct rte_flow_action_raw_encap *encap_data;
3263 struct mlx5_flow_dv_encap_decap_resource res;
3265 memset(&res, 0, sizeof(res));
3266 encap_data = (const struct rte_flow_action_raw_encap *)action->conf;
3267 res.size = encap_data->size;
3268 memcpy(res.buf, encap_data->data, res.size);
3269 res.reformat_type = res.size < MLX5_ENCAPSULATION_DECISION_SIZE ?
3270 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L3_TUNNEL_TO_L2 :
3271 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L3_TUNNEL;
3273 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
3275 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
3276 MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
3277 if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
3278 return rte_flow_error_set(error, EINVAL,
3279 RTE_FLOW_ERROR_TYPE_ACTION,
3280 NULL, "can't create encap action");
3285 * Create action push VLAN.
3288 * Pointer to rte_eth_dev structure.
3290 * Pointer to the flow attributes.
3292 * Pointer to the vlan to push to the Ethernet header.
3293 * @param[in, out] dev_flow
3294 * Pointer to the mlx5_flow.
3296 * Pointer to the error structure.
3299 * 0 on success, a negative errno value otherwise and rte_errno is set.
3302 flow_dv_create_action_push_vlan(struct rte_eth_dev *dev,
3303 const struct rte_flow_attr *attr,
3304 const struct rte_vlan_hdr *vlan,
3305 struct mlx5_flow *dev_flow,
3306 struct rte_flow_error *error)
3308 struct mlx5_flow_dv_push_vlan_action_resource res;
3310 memset(&res, 0, sizeof(res));
3312 rte_cpu_to_be_32(((uint32_t)vlan->eth_proto) << 16 |
3315 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
3317 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
3318 MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
3319 return flow_dv_push_vlan_action_resource_register
3320 (dev, &res, dev_flow, error);
3324 * Validate the modify-header actions.
3326 * @param[in] action_flags
3327 * Holds the actions detected until now.
3329 * Pointer to the modify action.
3331 * Pointer to error structure.
3334 * 0 on success, a negative errno value otherwise and rte_errno is set.
3337 flow_dv_validate_action_modify_hdr(const uint64_t action_flags,
3338 const struct rte_flow_action *action,
3339 struct rte_flow_error *error)
3341 if (action->type != RTE_FLOW_ACTION_TYPE_DEC_TTL && !action->conf)
3342 return rte_flow_error_set(error, EINVAL,
3343 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3344 NULL, "action configuration not set");
3345 if (action_flags & MLX5_FLOW_ACTION_ENCAP)
3346 return rte_flow_error_set(error, EINVAL,
3347 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3348 "can't have encap action before"
3354 * Validate the modify-header MAC address actions.
3356 * @param[in] action_flags
3357 * Holds the actions detected until now.
3359 * Pointer to the modify action.
3360 * @param[in] item_flags
3361 * Holds the items detected.
3363 * Pointer to error structure.
3366 * 0 on success, a negative errno value otherwise and rte_errno is set.
3369 flow_dv_validate_action_modify_mac(const uint64_t action_flags,
3370 const struct rte_flow_action *action,
3371 const uint64_t item_flags,
3372 struct rte_flow_error *error)
3376 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3378 if (!(item_flags & MLX5_FLOW_LAYER_L2))
3379 return rte_flow_error_set(error, EINVAL,
3380 RTE_FLOW_ERROR_TYPE_ACTION,
3382 "no L2 item in pattern");
3388 * Validate the modify-header IPv4 address actions.
3390 * @param[in] action_flags
3391 * Holds the actions detected until now.
3393 * Pointer to the modify action.
3394 * @param[in] item_flags
3395 * Holds the items detected.
3397 * Pointer to error structure.
3400 * 0 on success, a negative errno value otherwise and rte_errno is set.
3403 flow_dv_validate_action_modify_ipv4(const uint64_t action_flags,
3404 const struct rte_flow_action *action,
3405 const uint64_t item_flags,
3406 struct rte_flow_error *error)
3411 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3413 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3414 MLX5_FLOW_LAYER_INNER_L3_IPV4 :
3415 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
3416 if (!(item_flags & layer))
3417 return rte_flow_error_set(error, EINVAL,
3418 RTE_FLOW_ERROR_TYPE_ACTION,
3420 "no ipv4 item in pattern");
3426 * Validate the modify-header IPv6 address actions.
3428 * @param[in] action_flags
3429 * Holds the actions detected until now.
3431 * Pointer to the modify action.
3432 * @param[in] item_flags
3433 * Holds the items detected.
3435 * Pointer to error structure.
3438 * 0 on success, a negative errno value otherwise and rte_errno is set.
3441 flow_dv_validate_action_modify_ipv6(const uint64_t action_flags,
3442 const struct rte_flow_action *action,
3443 const uint64_t item_flags,
3444 struct rte_flow_error *error)
3449 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3451 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3452 MLX5_FLOW_LAYER_INNER_L3_IPV6 :
3453 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
3454 if (!(item_flags & layer))
3455 return rte_flow_error_set(error, EINVAL,
3456 RTE_FLOW_ERROR_TYPE_ACTION,
3458 "no ipv6 item in pattern");
3464 * Validate the modify-header TP actions.
3466 * @param[in] action_flags
3467 * Holds the actions detected until now.
3469 * Pointer to the modify action.
3470 * @param[in] item_flags
3471 * Holds the items detected.
3473 * Pointer to error structure.
3476 * 0 on success, a negative errno value otherwise and rte_errno is set.
3479 flow_dv_validate_action_modify_tp(const uint64_t action_flags,
3480 const struct rte_flow_action *action,
3481 const uint64_t item_flags,
3482 struct rte_flow_error *error)
3487 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3489 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3490 MLX5_FLOW_LAYER_INNER_L4 :
3491 MLX5_FLOW_LAYER_OUTER_L4;
3492 if (!(item_flags & layer))
3493 return rte_flow_error_set(error, EINVAL,
3494 RTE_FLOW_ERROR_TYPE_ACTION,
3495 NULL, "no transport layer "
3502 * Validate the modify-header actions of increment/decrement
3503 * TCP Sequence-number.
3505 * @param[in] action_flags
3506 * Holds the actions detected until now.
3508 * Pointer to the modify action.
3509 * @param[in] item_flags
3510 * Holds the items detected.
3512 * Pointer to error structure.
3515 * 0 on success, a negative errno value otherwise and rte_errno is set.
3518 flow_dv_validate_action_modify_tcp_seq(const uint64_t action_flags,
3519 const struct rte_flow_action *action,
3520 const uint64_t item_flags,
3521 struct rte_flow_error *error)
3526 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3528 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3529 MLX5_FLOW_LAYER_INNER_L4_TCP :
3530 MLX5_FLOW_LAYER_OUTER_L4_TCP;
3531 if (!(item_flags & layer))
3532 return rte_flow_error_set(error, EINVAL,
3533 RTE_FLOW_ERROR_TYPE_ACTION,
3534 NULL, "no TCP item in"
3536 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ &&
3537 (action_flags & MLX5_FLOW_ACTION_DEC_TCP_SEQ)) ||
3538 (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ &&
3539 (action_flags & MLX5_FLOW_ACTION_INC_TCP_SEQ)))
3540 return rte_flow_error_set(error, EINVAL,
3541 RTE_FLOW_ERROR_TYPE_ACTION,
3543 "cannot decrease and increase"
3544 " TCP sequence number"
3545 " at the same time");
3551 * Validate the modify-header actions of increment/decrement
3552 * TCP Acknowledgment number.
3554 * @param[in] action_flags
3555 * Holds the actions detected until now.
3557 * Pointer to the modify action.
3558 * @param[in] item_flags
3559 * Holds the items detected.
3561 * Pointer to error structure.
3564 * 0 on success, a negative errno value otherwise and rte_errno is set.
3567 flow_dv_validate_action_modify_tcp_ack(const uint64_t action_flags,
3568 const struct rte_flow_action *action,
3569 const uint64_t item_flags,
3570 struct rte_flow_error *error)
3575 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3577 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3578 MLX5_FLOW_LAYER_INNER_L4_TCP :
3579 MLX5_FLOW_LAYER_OUTER_L4_TCP;
3580 if (!(item_flags & layer))
3581 return rte_flow_error_set(error, EINVAL,
3582 RTE_FLOW_ERROR_TYPE_ACTION,
3583 NULL, "no TCP item in"
3585 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_ACK &&
3586 (action_flags & MLX5_FLOW_ACTION_DEC_TCP_ACK)) ||
3587 (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK &&
3588 (action_flags & MLX5_FLOW_ACTION_INC_TCP_ACK)))
3589 return rte_flow_error_set(error, EINVAL,
3590 RTE_FLOW_ERROR_TYPE_ACTION,
3592 "cannot decrease and increase"
3593 " TCP acknowledgment number"
3594 " at the same time");
3600 * Validate the modify-header TTL actions.
3602 * @param[in] action_flags
3603 * Holds the actions detected until now.
3605 * Pointer to the modify action.
3606 * @param[in] item_flags
3607 * Holds the items detected.
3609 * Pointer to error structure.
3612 * 0 on success, a negative errno value otherwise and rte_errno is set.
3615 flow_dv_validate_action_modify_ttl(const uint64_t action_flags,
3616 const struct rte_flow_action *action,
3617 const uint64_t item_flags,
3618 struct rte_flow_error *error)
3623 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3625 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3626 MLX5_FLOW_LAYER_INNER_L3 :
3627 MLX5_FLOW_LAYER_OUTER_L3;
3628 if (!(item_flags & layer))
3629 return rte_flow_error_set(error, EINVAL,
3630 RTE_FLOW_ERROR_TYPE_ACTION,
3632 "no IP protocol in pattern");
3638 * Validate jump action.
3641 * Pointer to the jump action.
3642 * @param[in] action_flags
3643 * Holds the actions detected until now.
3644 * @param[in] attributes
3645 * Pointer to flow attributes
3646 * @param[in] external
3647 * Action belongs to flow rule created by request external to PMD.
3649 * Pointer to error structure.
3652 * 0 on success, a negative errno value otherwise and rte_errno is set.
3655 flow_dv_validate_action_jump(const struct rte_flow_action *action,
3656 uint64_t action_flags,
3657 const struct rte_flow_attr *attributes,
3658 bool external, struct rte_flow_error *error)
3660 uint32_t target_group, table;
3663 if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
3664 MLX5_FLOW_FATE_ESWITCH_ACTIONS))
3665 return rte_flow_error_set(error, EINVAL,
3666 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3667 "can't have 2 fate actions in"
3669 if (action_flags & MLX5_FLOW_ACTION_METER)
3670 return rte_flow_error_set(error, ENOTSUP,
3671 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3672 "jump with meter not support");
3674 return rte_flow_error_set(error, EINVAL,
3675 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3676 NULL, "action configuration not set");
3678 ((const struct rte_flow_action_jump *)action->conf)->group;
3679 ret = mlx5_flow_group_to_table(attributes, external, target_group,
3680 true, &table, error);
3683 if (attributes->group == target_group)
3684 return rte_flow_error_set(error, EINVAL,
3685 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3686 "target group must be other than"
3687 " the current flow group");
3692 * Validate the port_id action.
3695 * Pointer to rte_eth_dev structure.
3696 * @param[in] action_flags
3697 * Bit-fields that holds the actions detected until now.
3699 * Port_id RTE action structure.
3701 * Attributes of flow that includes this action.
3703 * Pointer to error structure.
3706 * 0 on success, a negative errno value otherwise and rte_errno is set.
3709 flow_dv_validate_action_port_id(struct rte_eth_dev *dev,
3710 uint64_t action_flags,
3711 const struct rte_flow_action *action,
3712 const struct rte_flow_attr *attr,
3713 struct rte_flow_error *error)
3715 const struct rte_flow_action_port_id *port_id;
3716 struct mlx5_priv *act_priv;
3717 struct mlx5_priv *dev_priv;
3720 if (!attr->transfer)
3721 return rte_flow_error_set(error, ENOTSUP,
3722 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3724 "port id action is valid in transfer"
3726 if (!action || !action->conf)
3727 return rte_flow_error_set(error, ENOTSUP,
3728 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3730 "port id action parameters must be"
3732 if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
3733 MLX5_FLOW_FATE_ESWITCH_ACTIONS))
3734 return rte_flow_error_set(error, EINVAL,
3735 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3736 "can have only one fate actions in"
3738 dev_priv = mlx5_dev_to_eswitch_info(dev);
3740 return rte_flow_error_set(error, rte_errno,
3741 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3743 "failed to obtain E-Switch info");
3744 port_id = action->conf;
3745 port = port_id->original ? dev->data->port_id : port_id->id;
3746 act_priv = mlx5_port_to_eswitch_info(port, false);
3748 return rte_flow_error_set
3750 RTE_FLOW_ERROR_TYPE_ACTION_CONF, port_id,
3751 "failed to obtain E-Switch port id for port");
3752 if (act_priv->domain_id != dev_priv->domain_id)
3753 return rte_flow_error_set
3755 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3756 "port does not belong to"
3757 " E-Switch being configured");
3762 * Get the maximum number of modify header actions.
3765 * Pointer to rte_eth_dev structure.
3767 * Flags bits to check if root level.
3770 * Max number of modify header actions device can support.
3772 static inline unsigned int
3773 flow_dv_modify_hdr_action_max(struct rte_eth_dev *dev __rte_unused,
3777 * There's no way to directly query the max capacity from FW.
3778 * The maximal value on root table should be assumed to be supported.
3780 if (!(flags & MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL))
3781 return MLX5_MAX_MODIFY_NUM;
3783 return MLX5_ROOT_TBL_MODIFY_NUM;
3787 * Validate the meter action.
3790 * Pointer to rte_eth_dev structure.
3791 * @param[in] action_flags
3792 * Bit-fields that holds the actions detected until now.
3794 * Pointer to the meter action.
3796 * Attributes of flow that includes this action.
3798 * Pointer to error structure.
3801 * 0 on success, a negative errno value otherwise and rte_ernno is set.
3804 mlx5_flow_validate_action_meter(struct rte_eth_dev *dev,
3805 uint64_t action_flags,
3806 const struct rte_flow_action *action,
3807 const struct rte_flow_attr *attr,
3808 struct rte_flow_error *error)
3810 struct mlx5_priv *priv = dev->data->dev_private;
3811 const struct rte_flow_action_meter *am = action->conf;
3812 struct mlx5_flow_meter *fm;
3815 return rte_flow_error_set(error, EINVAL,
3816 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3817 "meter action conf is NULL");
3819 if (action_flags & MLX5_FLOW_ACTION_METER)
3820 return rte_flow_error_set(error, ENOTSUP,
3821 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3822 "meter chaining not support");
3823 if (action_flags & MLX5_FLOW_ACTION_JUMP)
3824 return rte_flow_error_set(error, ENOTSUP,
3825 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3826 "meter with jump not support");
3828 return rte_flow_error_set(error, ENOTSUP,
3829 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3831 "meter action not supported");
3832 fm = mlx5_flow_meter_find(priv, am->mtr_id);
3834 return rte_flow_error_set(error, EINVAL,
3835 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3837 if (fm->ref_cnt && (!(fm->transfer == attr->transfer ||
3838 (!fm->ingress && !attr->ingress && attr->egress) ||
3839 (!fm->egress && !attr->egress && attr->ingress))))
3840 return rte_flow_error_set(error, EINVAL,
3841 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3842 "Flow attributes are either invalid "
3843 "or have a conflict with current "
3844 "meter attributes");
3849 * Validate the age action.
3851 * @param[in] action_flags
3852 * Holds the actions detected until now.
3854 * Pointer to the age action.
3856 * Pointer to the Ethernet device structure.
3858 * Pointer to error structure.
3861 * 0 on success, a negative errno value otherwise and rte_errno is set.
3864 flow_dv_validate_action_age(uint64_t action_flags,
3865 const struct rte_flow_action *action,
3866 struct rte_eth_dev *dev,
3867 struct rte_flow_error *error)
3869 struct mlx5_priv *priv = dev->data->dev_private;
3870 const struct rte_flow_action_age *age = action->conf;
3872 if (!priv->config.devx || priv->counter_fallback)
3873 return rte_flow_error_set(error, ENOTSUP,
3874 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3876 "age action not supported");
3877 if (!(action->conf))
3878 return rte_flow_error_set(error, EINVAL,
3879 RTE_FLOW_ERROR_TYPE_ACTION, action,
3880 "configuration cannot be null");
3881 if (age->timeout >= UINT16_MAX / 2 / 10)
3882 return rte_flow_error_set(error, ENOTSUP,
3883 RTE_FLOW_ERROR_TYPE_ACTION, action,
3884 "Max age time: 3275 seconds");
3885 if (action_flags & MLX5_FLOW_ACTION_AGE)
3886 return rte_flow_error_set(error, EINVAL,
3887 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3888 "Duplicate age ctions set");
3893 * Validate the modify-header IPv4 DSCP actions.
3895 * @param[in] action_flags
3896 * Holds the actions detected until now.
3898 * Pointer to the modify action.
3899 * @param[in] item_flags
3900 * Holds the items detected.
3902 * Pointer to error structure.
3905 * 0 on success, a negative errno value otherwise and rte_errno is set.
3908 flow_dv_validate_action_modify_ipv4_dscp(const uint64_t action_flags,
3909 const struct rte_flow_action *action,
3910 const uint64_t item_flags,
3911 struct rte_flow_error *error)
3915 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3917 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV4))
3918 return rte_flow_error_set(error, EINVAL,
3919 RTE_FLOW_ERROR_TYPE_ACTION,
3921 "no ipv4 item in pattern");
3927 * Validate the modify-header IPv6 DSCP actions.
3929 * @param[in] action_flags
3930 * Holds the actions detected until now.
3932 * Pointer to the modify action.
3933 * @param[in] item_flags
3934 * Holds the items detected.
3936 * Pointer to error structure.
3939 * 0 on success, a negative errno value otherwise and rte_errno is set.
3942 flow_dv_validate_action_modify_ipv6_dscp(const uint64_t action_flags,
3943 const struct rte_flow_action *action,
3944 const uint64_t item_flags,
3945 struct rte_flow_error *error)
3949 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3951 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV6))
3952 return rte_flow_error_set(error, EINVAL,
3953 RTE_FLOW_ERROR_TYPE_ACTION,
3955 "no ipv6 item in pattern");
3961 * Match modify-header resource.
3964 * Pointer to exist resource entry object.
3966 * Pointer to new modify-header resource.
3969 * 0 on matching, -1 otherwise.
3972 flow_dv_modify_hdr_resource_match(struct mlx5_hlist_entry *entry, void *ctx)
3974 struct mlx5_flow_dv_modify_hdr_resource *resource;
3975 struct mlx5_flow_dv_modify_hdr_resource *cache_resource;
3976 uint32_t actions_len;
3978 resource = (struct mlx5_flow_dv_modify_hdr_resource *)ctx;
3979 cache_resource = container_of(entry,
3980 struct mlx5_flow_dv_modify_hdr_resource,
3982 actions_len = resource->actions_num * sizeof(resource->actions[0]);
3983 if (resource->entry.key == cache_resource->entry.key &&
3984 resource->ft_type == cache_resource->ft_type &&
3985 resource->actions_num == cache_resource->actions_num &&
3986 resource->flags == cache_resource->flags &&
3987 !memcmp((const void *)resource->actions,
3988 (const void *)cache_resource->actions,
3995 * Find existing modify-header resource or create and register a new one.
3997 * @param dev[in, out]
3998 * Pointer to rte_eth_dev structure.
3999 * @param[in, out] resource
4000 * Pointer to modify-header resource.
4001 * @parm[in, out] dev_flow
4002 * Pointer to the dev_flow.
4004 * pointer to error structure.
4007 * 0 on success otherwise -errno and errno is set.
4010 flow_dv_modify_hdr_resource_register
4011 (struct rte_eth_dev *dev,
4012 struct mlx5_flow_dv_modify_hdr_resource *resource,
4013 struct mlx5_flow *dev_flow,
4014 struct rte_flow_error *error)
4016 struct mlx5_priv *priv = dev->data->dev_private;
4017 struct mlx5_dev_ctx_shared *sh = priv->sh;
4018 struct mlx5_flow_dv_modify_hdr_resource *cache_resource;
4019 struct mlx5dv_dr_domain *ns;
4020 uint32_t actions_len;
4021 struct mlx5_hlist_entry *entry;
4022 union mlx5_flow_modify_hdr_key hdr_mod_key = {
4024 .ft_type = resource->ft_type,
4025 .actions_num = resource->actions_num,
4026 .group = dev_flow->dv.group,
4032 resource->flags = dev_flow->dv.group ? 0 :
4033 MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL;
4034 if (resource->actions_num > flow_dv_modify_hdr_action_max(dev,
4036 return rte_flow_error_set(error, EOVERFLOW,
4037 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4038 "too many modify header items");
4039 if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
4040 ns = sh->fdb_domain;
4041 else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_TX)
4045 /* Lookup a matching resource from cache. */
4046 actions_len = resource->actions_num * sizeof(resource->actions[0]);
4047 hdr_mod_key.cksum = __rte_raw_cksum(resource->actions, actions_len, 0);
4048 resource->entry.key = hdr_mod_key.v64;
4049 entry = mlx5_hlist_lookup_ex(sh->modify_cmds, resource->entry.key,
4050 flow_dv_modify_hdr_resource_match,
4053 cache_resource = container_of(entry,
4054 struct mlx5_flow_dv_modify_hdr_resource,
4056 DRV_LOG(DEBUG, "modify-header resource %p: refcnt %d++",
4057 (void *)cache_resource,
4058 rte_atomic32_read(&cache_resource->refcnt));
4059 rte_atomic32_inc(&cache_resource->refcnt);
4060 dev_flow->handle->dvh.modify_hdr = cache_resource;
4064 /* Register new modify-header resource. */
4065 cache_resource = mlx5_malloc(MLX5_MEM_ZERO,
4066 sizeof(*cache_resource) + actions_len, 0,
4068 if (!cache_resource)
4069 return rte_flow_error_set(error, ENOMEM,
4070 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4071 "cannot allocate resource memory");
4072 *cache_resource = *resource;
4073 rte_memcpy(cache_resource->actions, resource->actions, actions_len);
4074 ret = mlx5_flow_os_create_flow_action_modify_header
4075 (sh->ctx, ns, cache_resource,
4076 actions_len, &cache_resource->action);
4078 mlx5_free(cache_resource);
4079 return rte_flow_error_set(error, ENOMEM,
4080 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4081 NULL, "cannot create action");
4083 rte_atomic32_init(&cache_resource->refcnt);
4084 rte_atomic32_inc(&cache_resource->refcnt);
4085 if (mlx5_hlist_insert_ex(sh->modify_cmds, &cache_resource->entry,
4086 flow_dv_modify_hdr_resource_match,
4087 (void *)cache_resource)) {
4088 claim_zero(mlx5_flow_os_destroy_flow_action
4089 (cache_resource->action));
4090 mlx5_free(cache_resource);
4091 return rte_flow_error_set(error, EEXIST,
4092 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4093 NULL, "action exist");
4095 dev_flow->handle->dvh.modify_hdr = cache_resource;
4096 DRV_LOG(DEBUG, "new modify-header resource %p: refcnt %d++",
4097 (void *)cache_resource,
4098 rte_atomic32_read(&cache_resource->refcnt));
4103 * Get DV flow counter by index.
4106 * Pointer to the Ethernet device structure.
4108 * mlx5 flow counter index in the container.
4110 * mlx5 flow counter pool in the container,
4113 * Pointer to the counter, NULL otherwise.
4115 static struct mlx5_flow_counter *
4116 flow_dv_counter_get_by_idx(struct rte_eth_dev *dev,
4118 struct mlx5_flow_counter_pool **ppool)
4120 struct mlx5_priv *priv = dev->data->dev_private;
4121 struct mlx5_pools_container *cont;
4122 struct mlx5_flow_counter_pool *pool;
4123 uint32_t batch = 0, age = 0;
4126 age = MLX_CNT_IS_AGE(idx);
4127 idx = age ? idx - MLX5_CNT_AGE_OFFSET : idx;
4128 if (idx >= MLX5_CNT_BATCH_OFFSET) {
4129 idx -= MLX5_CNT_BATCH_OFFSET;
4132 cont = MLX5_CNT_CONTAINER(priv->sh, batch, age);
4133 MLX5_ASSERT(idx / MLX5_COUNTERS_PER_POOL < cont->n);
4134 pool = cont->pools[idx / MLX5_COUNTERS_PER_POOL];
4138 return MLX5_POOL_GET_CNT(pool, idx % MLX5_COUNTERS_PER_POOL);
4142 * Check the devx counter belongs to the pool.
4145 * Pointer to the counter pool.
4147 * The counter devx ID.
4150 * True if counter belongs to the pool, false otherwise.
4153 flow_dv_is_counter_in_pool(struct mlx5_flow_counter_pool *pool, int id)
4155 int base = (pool->min_dcs->id / MLX5_COUNTERS_PER_POOL) *
4156 MLX5_COUNTERS_PER_POOL;
4158 if (id >= base && id < base + MLX5_COUNTERS_PER_POOL)
4164 * Get a pool by devx counter ID.
4167 * Pointer to the counter container.
4169 * The counter devx ID.
4172 * The counter pool pointer if exists, NULL otherwise,
4174 static struct mlx5_flow_counter_pool *
4175 flow_dv_find_pool_by_id(struct mlx5_pools_container *cont, int id)
4179 /* Check last used pool. */
4180 if (cont->last_pool_idx != POOL_IDX_INVALID &&
4181 flow_dv_is_counter_in_pool(cont->pools[cont->last_pool_idx], id))
4182 return cont->pools[cont->last_pool_idx];
4183 /* ID out of range means no suitable pool in the container. */
4184 if (id > cont->max_id || id < cont->min_id)
4187 * Find the pool from the end of the container, since mostly counter
4188 * ID is sequence increasing, and the last pool should be the needed
4191 i = rte_atomic16_read(&cont->n_valid);
4193 struct mlx5_flow_counter_pool *pool = cont->pools[i];
4195 if (flow_dv_is_counter_in_pool(pool, id))
4202 * Allocate a new memory for the counter values wrapped by all the needed
4206 * Pointer to the Ethernet device structure.
4208 * The raw memory areas - each one for MLX5_COUNTERS_PER_POOL counters.
4211 * The new memory management pointer on success, otherwise NULL and rte_errno
4214 static struct mlx5_counter_stats_mem_mng *
4215 flow_dv_create_counter_stat_mem_mng(struct rte_eth_dev *dev, int raws_n)
4217 struct mlx5_priv *priv = dev->data->dev_private;
4218 struct mlx5_dev_ctx_shared *sh = priv->sh;
4219 struct mlx5_devx_mkey_attr mkey_attr;
4220 struct mlx5_counter_stats_mem_mng *mem_mng;
4221 volatile struct flow_counter_stats *raw_data;
4222 int size = (sizeof(struct flow_counter_stats) *
4223 MLX5_COUNTERS_PER_POOL +
4224 sizeof(struct mlx5_counter_stats_raw)) * raws_n +
4225 sizeof(struct mlx5_counter_stats_mem_mng);
4226 size_t pgsize = rte_mem_page_size();
4227 if (pgsize == (size_t)-1) {
4228 DRV_LOG(ERR, "Failed to get mem page size");
4232 uint8_t *mem = mlx5_malloc(MLX5_MEM_ZERO, size, pgsize,
4240 mem_mng = (struct mlx5_counter_stats_mem_mng *)(mem + size) - 1;
4241 size = sizeof(*raw_data) * MLX5_COUNTERS_PER_POOL * raws_n;
4242 mem_mng->umem = mlx5_glue->devx_umem_reg(sh->ctx, mem, size,
4243 IBV_ACCESS_LOCAL_WRITE);
4244 if (!mem_mng->umem) {
4249 mkey_attr.addr = (uintptr_t)mem;
4250 mkey_attr.size = size;
4251 mkey_attr.umem_id = mlx5_os_get_umem_id(mem_mng->umem);
4252 mkey_attr.pd = sh->pdn;
4253 mkey_attr.log_entity_size = 0;
4254 mkey_attr.pg_access = 0;
4255 mkey_attr.klm_array = NULL;
4256 mkey_attr.klm_num = 0;
4257 if (priv->config.hca_attr.relaxed_ordering_write &&
4258 priv->config.hca_attr.relaxed_ordering_read &&
4259 !haswell_broadwell_cpu)
4260 mkey_attr.relaxed_ordering = 1;
4261 mem_mng->dm = mlx5_devx_cmd_mkey_create(sh->ctx, &mkey_attr);
4263 mlx5_glue->devx_umem_dereg(mem_mng->umem);
4268 mem_mng->raws = (struct mlx5_counter_stats_raw *)(mem + size);
4269 raw_data = (volatile struct flow_counter_stats *)mem;
4270 for (i = 0; i < raws_n; ++i) {
4271 mem_mng->raws[i].mem_mng = mem_mng;
4272 mem_mng->raws[i].data = raw_data + i * MLX5_COUNTERS_PER_POOL;
4274 LIST_INSERT_HEAD(&sh->cmng.mem_mngs, mem_mng, next);
4279 * Resize a counter container.
4282 * Pointer to the Ethernet device structure.
4284 * Whether the pool is for counter that was allocated by batch command.
4286 * Whether the pool is for Aging counter.
4289 * 0 on success, otherwise negative errno value and rte_errno is set.
4292 flow_dv_container_resize(struct rte_eth_dev *dev,
4293 uint32_t batch, uint32_t age)
4295 struct mlx5_priv *priv = dev->data->dev_private;
4296 struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(priv->sh, batch,
4298 struct mlx5_counter_stats_mem_mng *mem_mng = NULL;
4299 void *old_pools = cont->pools;
4300 uint32_t resize = cont->n + MLX5_CNT_CONTAINER_RESIZE;
4301 uint32_t mem_size = sizeof(struct mlx5_flow_counter_pool *) * resize;
4302 void *pools = mlx5_malloc(MLX5_MEM_ZERO, mem_size, 0, SOCKET_ID_ANY);
4309 memcpy(pools, old_pools, cont->n *
4310 sizeof(struct mlx5_flow_counter_pool *));
4312 * Fallback mode query the counter directly, no background query
4313 * resources are needed.
4315 if (!priv->counter_fallback) {
4318 mem_mng = flow_dv_create_counter_stat_mem_mng(dev,
4319 MLX5_CNT_CONTAINER_RESIZE + MLX5_MAX_PENDING_QUERIES);
4324 for (i = 0; i < MLX5_MAX_PENDING_QUERIES; ++i)
4325 LIST_INSERT_HEAD(&priv->sh->cmng.free_stat_raws,
4327 MLX5_CNT_CONTAINER_RESIZE +
4330 rte_spinlock_lock(&cont->resize_sl);
4332 cont->mem_mng = mem_mng;
4333 cont->pools = pools;
4334 rte_spinlock_unlock(&cont->resize_sl);
4336 mlx5_free(old_pools);
4341 * Query a devx flow counter.
4344 * Pointer to the Ethernet device structure.
4346 * Index to the flow counter.
4348 * The statistics value of packets.
4350 * The statistics value of bytes.
4353 * 0 on success, otherwise a negative errno value and rte_errno is set.
4356 _flow_dv_query_count(struct rte_eth_dev *dev, uint32_t counter, uint64_t *pkts,
4359 struct mlx5_priv *priv = dev->data->dev_private;
4360 struct mlx5_flow_counter_pool *pool = NULL;
4361 struct mlx5_flow_counter *cnt;
4362 struct mlx5_flow_counter_ext *cnt_ext = NULL;
4365 cnt = flow_dv_counter_get_by_idx(dev, counter, &pool);
4367 if (counter < MLX5_CNT_BATCH_OFFSET) {
4368 cnt_ext = MLX5_CNT_TO_CNT_EXT(pool, cnt);
4369 if (priv->counter_fallback)
4370 return mlx5_devx_cmd_flow_counter_query(cnt_ext->dcs, 0,
4371 0, pkts, bytes, 0, NULL, NULL, 0);
4374 rte_spinlock_lock(&pool->sl);
4376 * The single counters allocation may allocate smaller ID than the
4377 * current allocated in parallel to the host reading.
4378 * In this case the new counter values must be reported as 0.
4380 if (unlikely(cnt_ext && cnt_ext->dcs->id < pool->raw->min_dcs_id)) {
4384 offset = MLX5_CNT_ARRAY_IDX(pool, cnt);
4385 *pkts = rte_be_to_cpu_64(pool->raw->data[offset].hits);
4386 *bytes = rte_be_to_cpu_64(pool->raw->data[offset].bytes);
4388 rte_spinlock_unlock(&pool->sl);
4393 * Create and initialize a new counter pool.
4396 * Pointer to the Ethernet device structure.
4398 * The devX counter handle.
4400 * Whether the pool is for counter that was allocated by batch command.
4402 * Whether the pool is for counter that was allocated for aging.
4403 * @param[in/out] cont_cur
4404 * Pointer to the container pointer, it will be update in pool resize.
4407 * The pool container pointer on success, NULL otherwise and rte_errno is set.
4409 static struct mlx5_flow_counter_pool *
4410 flow_dv_pool_create(struct rte_eth_dev *dev, struct mlx5_devx_obj *dcs,
4411 uint32_t batch, uint32_t age)
4413 struct mlx5_priv *priv = dev->data->dev_private;
4414 struct mlx5_flow_counter_pool *pool;
4415 struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(priv->sh, batch,
4417 int16_t n_valid = rte_atomic16_read(&cont->n_valid);
4418 uint32_t size = sizeof(*pool);
4420 if (cont->n == n_valid && flow_dv_container_resize(dev, batch, age))
4422 size += MLX5_COUNTERS_PER_POOL * CNT_SIZE;
4423 size += (batch ? 0 : MLX5_COUNTERS_PER_POOL * CNTEXT_SIZE);
4424 size += (!age ? 0 : MLX5_COUNTERS_PER_POOL * AGE_SIZE);
4425 pool = mlx5_malloc(MLX5_MEM_ZERO, size, 0, SOCKET_ID_ANY);
4430 pool->min_dcs = dcs;
4431 if (!priv->counter_fallback)
4432 pool->raw = cont->mem_mng->raws + n_valid %
4433 MLX5_CNT_CONTAINER_RESIZE;
4434 pool->raw_hw = NULL;
4436 pool->type |= (batch ? 0 : CNT_POOL_TYPE_EXT);
4437 pool->type |= (!age ? 0 : CNT_POOL_TYPE_AGE);
4438 pool->query_gen = 0;
4439 rte_spinlock_init(&pool->sl);
4440 TAILQ_INIT(&pool->counters[0]);
4441 TAILQ_INIT(&pool->counters[1]);
4442 TAILQ_INSERT_HEAD(&cont->pool_list, pool, next);
4443 pool->index = n_valid;
4444 cont->pools[n_valid] = pool;
4446 int base = RTE_ALIGN_FLOOR(dcs->id, MLX5_COUNTERS_PER_POOL);
4448 if (base < cont->min_id)
4449 cont->min_id = base;
4450 if (base > cont->max_id)
4451 cont->max_id = base + MLX5_COUNTERS_PER_POOL - 1;
4452 cont->last_pool_idx = pool->index;
4454 /* Pool initialization must be updated before host thread access. */
4456 rte_atomic16_add(&cont->n_valid, 1);
4461 * Restore skipped counters in the pool.
4463 * As counter pool query requires the first counter dcs
4464 * ID start with 4 alinged, if the pool counters with
4465 * min_dcs ID are not aligned with 4, the counters will
4467 * Once other min_dcs ID less than these skipped counter
4468 * dcs ID appears, the skipped counters will be safe to
4470 * Should be called when min_dcs is updated.
4473 * Current counter pool.
4474 * @param[in] last_min_dcs
4478 flow_dv_counter_restore(struct mlx5_flow_counter_pool *pool,
4479 struct mlx5_devx_obj *last_min_dcs)
4481 struct mlx5_flow_counter_ext *cnt_ext;
4482 uint32_t offset, new_offset;
4483 uint32_t skip_cnt = 0;
4486 if (!pool->skip_cnt)
4489 * If last min_dcs is not valid. The skipped counter may even after
4490 * last min_dcs, set the offset to the whole pool.
4492 if (last_min_dcs->id & (MLX5_CNT_BATCH_QUERY_ID_ALIGNMENT - 1))
4493 offset = MLX5_COUNTERS_PER_POOL;
4495 offset = last_min_dcs->id % MLX5_COUNTERS_PER_POOL;
4496 new_offset = pool->min_dcs->id % MLX5_COUNTERS_PER_POOL;
4498 * Check the counters from 1 to the last_min_dcs range. Counters
4499 * before new min_dcs indicates pool still has skipped counters.
4500 * Counters be skipped after new min_dcs will be ready to use.
4501 * Offset 0 counter must be empty or min_dcs, start from 1.
4503 for (i = 1; i < offset; i++) {
4504 cnt_ext = MLX5_GET_POOL_CNT_EXT(pool, i);
4505 if (cnt_ext->skipped) {
4506 if (i > new_offset) {
4507 cnt_ext->skipped = 0;
4509 (&pool->counters[pool->query_gen],
4510 MLX5_POOL_GET_CNT(pool, i), next);
4521 * Prepare a new counter and/or a new counter pool.
4524 * Pointer to the Ethernet device structure.
4525 * @param[out] cnt_free
4526 * Where to put the pointer of a new counter.
4528 * Whether the pool is for counter that was allocated by batch command.
4530 * Whether the pool is for counter that was allocated for aging.
4533 * The counter pool pointer and @p cnt_free is set on success,
4534 * NULL otherwise and rte_errno is set.
4536 static struct mlx5_flow_counter_pool *
4537 flow_dv_counter_pool_prepare(struct rte_eth_dev *dev,
4538 struct mlx5_flow_counter **cnt_free,
4539 uint32_t batch, uint32_t age)
4541 struct mlx5_priv *priv = dev->data->dev_private;
4542 struct mlx5_pools_container *cont;
4543 struct mlx5_flow_counter_pool *pool;
4544 struct mlx5_counters tmp_tq;
4545 struct mlx5_devx_obj *last_min_dcs;
4546 struct mlx5_devx_obj *dcs = NULL;
4547 struct mlx5_flow_counter *cnt;
4551 cont = MLX5_CNT_CONTAINER(priv->sh, batch, age);
4555 /* bulk_bitmap must be 0 for single counter allocation. */
4556 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0);
4559 pool = flow_dv_find_pool_by_id(cont, dcs->id);
4560 /* Check if counter belongs to exist pool ID range. */
4562 pool = flow_dv_find_pool_by_id
4564 (priv->sh, batch, (age ^ 0x1)), dcs->id);
4566 * Pool eixsts, counter will be added to the other
4567 * container, need to reallocate it later.
4572 pool = flow_dv_pool_create(dev, dcs, batch,
4575 mlx5_devx_cmd_destroy(dcs);
4580 if ((dcs->id < pool->min_dcs->id ||
4582 (MLX5_CNT_BATCH_QUERY_ID_ALIGNMENT - 1)) &&
4583 !(dcs->id & (MLX5_CNT_BATCH_QUERY_ID_ALIGNMENT - 1))) {
4585 * Update the pool min_dcs only if current dcs is
4586 * valid and exist min_dcs is not valid or greater
4589 last_min_dcs = pool->min_dcs;
4590 rte_atomic64_set(&pool->a64_dcs,
4591 (int64_t)(uintptr_t)dcs);
4593 * Restore any skipped counters if the new min_dcs
4594 * ID is smaller or min_dcs is not valid.
4596 if (dcs->id < last_min_dcs->id ||
4598 (MLX5_CNT_BATCH_QUERY_ID_ALIGNMENT - 1))
4599 flow_dv_counter_restore(pool, last_min_dcs);
4601 i = dcs->id % MLX5_COUNTERS_PER_POOL;
4602 cnt = MLX5_POOL_GET_CNT(pool, i);
4604 MLX5_GET_POOL_CNT_EXT(pool, i)->dcs = dcs;
4606 * If min_dcs is not valid, it means the new allocated dcs
4607 * also fail to become the valid min_dcs, just skip it.
4608 * Or if min_dcs is valid, and new dcs ID is smaller than
4609 * min_dcs, but not become the min_dcs, also skip it.
4611 if (pool->min_dcs->id &
4612 (MLX5_CNT_BATCH_QUERY_ID_ALIGNMENT - 1) ||
4613 dcs->id < pool->min_dcs->id) {
4614 MLX5_GET_POOL_CNT_EXT(pool, i)->skipped = 1;
4619 TAILQ_INSERT_TAIL(&pool->counters[pool->query_gen],
4626 /* bulk_bitmap is in 128 counters units. */
4627 if (priv->config.hca_attr.flow_counter_bulk_alloc_bitmap & 0x4)
4628 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0x4);
4630 rte_errno = ENODATA;
4633 pool = flow_dv_pool_create(dev, dcs, batch, age);
4635 mlx5_devx_cmd_destroy(dcs);
4638 TAILQ_INIT(&tmp_tq);
4639 for (i = 1; i < MLX5_COUNTERS_PER_POOL; ++i) {
4640 cnt = MLX5_POOL_GET_CNT(pool, i);
4642 TAILQ_INSERT_HEAD(&tmp_tq, cnt, next);
4644 rte_spinlock_lock(&cont->csl);
4645 TAILQ_CONCAT(&cont->counters, &tmp_tq, next);
4646 rte_spinlock_unlock(&cont->csl);
4647 *cnt_free = MLX5_POOL_GET_CNT(pool, 0);
4648 (*cnt_free)->pool = pool;
4653 * Search for existed shared counter.
4656 * Pointer to the Ethernet device structure.
4658 * The shared counter ID to search.
4660 * mlx5 flow counter pool in the container,
4663 * NULL if not existed, otherwise pointer to the shared extend counter.
4665 static struct mlx5_flow_counter_ext *
4666 flow_dv_counter_shared_search(struct rte_eth_dev *dev, uint32_t id,
4667 struct mlx5_flow_counter_pool **ppool)
4669 struct mlx5_priv *priv = dev->data->dev_private;
4670 union mlx5_l3t_data data;
4673 if (mlx5_l3t_get_entry(priv->sh->cnt_id_tbl, id, &data) || !data.dword)
4675 cnt_idx = data.dword;
4677 * Shared counters don't have age info. The counter extend is after
4678 * the counter datat structure.
4680 return (struct mlx5_flow_counter_ext *)
4681 ((flow_dv_counter_get_by_idx(dev, cnt_idx, ppool)) + 1);
4685 * Allocate a flow counter.
4688 * Pointer to the Ethernet device structure.
4690 * Indicate if this counter is shared with other flows.
4692 * Counter identifier.
4694 * Counter flow group.
4696 * Whether the counter was allocated for aging.
4699 * Index to flow counter on success, 0 otherwise and rte_errno is set.
4702 flow_dv_counter_alloc(struct rte_eth_dev *dev, uint32_t shared, uint32_t id,
4703 uint16_t group, uint32_t age)
4705 struct mlx5_priv *priv = dev->data->dev_private;
4706 struct mlx5_flow_counter_pool *pool = NULL;
4707 struct mlx5_flow_counter *cnt_free = NULL;
4708 struct mlx5_flow_counter_ext *cnt_ext = NULL;
4710 * Currently group 0 flow counter cannot be assigned to a flow if it is
4711 * not the first one in the batch counter allocation, so it is better
4712 * to allocate counters one by one for these flows in a separate
4714 * A counter can be shared between different groups so need to take
4715 * shared counters from the single container.
4717 uint32_t batch = (group && !shared && !priv->counter_fallback) ? 1 : 0;
4718 struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(priv->sh, batch,
4722 if (!priv->config.devx) {
4723 rte_errno = ENOTSUP;
4727 cnt_ext = flow_dv_counter_shared_search(dev, id, &pool);
4729 if (cnt_ext->ref_cnt + 1 == 0) {
4734 cnt_idx = pool->index * MLX5_COUNTERS_PER_POOL +
4735 (cnt_ext->dcs->id % MLX5_COUNTERS_PER_POOL)
4740 /* Get free counters from container. */
4741 rte_spinlock_lock(&cont->csl);
4742 cnt_free = TAILQ_FIRST(&cont->counters);
4744 TAILQ_REMOVE(&cont->counters, cnt_free, next);
4745 rte_spinlock_unlock(&cont->csl);
4746 if (!cnt_free && !flow_dv_counter_pool_prepare(dev, &cnt_free,
4749 pool = cnt_free->pool;
4751 cnt_ext = MLX5_CNT_TO_CNT_EXT(pool, cnt_free);
4752 /* Create a DV counter action only in the first time usage. */
4753 if (!cnt_free->action) {
4755 struct mlx5_devx_obj *dcs;
4759 offset = MLX5_CNT_ARRAY_IDX(pool, cnt_free);
4760 dcs = pool->min_dcs;
4765 ret = mlx5_flow_os_create_flow_action_count(dcs->obj, offset,
4772 cnt_idx = MLX5_MAKE_CNT_IDX(pool->index,
4773 MLX5_CNT_ARRAY_IDX(pool, cnt_free));
4774 cnt_idx += batch * MLX5_CNT_BATCH_OFFSET;
4775 cnt_idx += age * MLX5_CNT_AGE_OFFSET;
4776 /* Update the counter reset values. */
4777 if (_flow_dv_query_count(dev, cnt_idx, &cnt_free->hits,
4781 cnt_ext->shared = shared;
4782 cnt_ext->ref_cnt = 1;
4785 union mlx5_l3t_data data;
4787 data.dword = cnt_idx;
4788 if (mlx5_l3t_set_entry(priv->sh->cnt_id_tbl, id, &data))
4792 if (!priv->counter_fallback && !priv->sh->cmng.query_thread_on)
4793 /* Start the asynchronous batch query by the host thread. */
4794 mlx5_set_query_alarm(priv->sh);
4798 cnt_free->pool = pool;
4799 rte_spinlock_lock(&cont->csl);
4800 TAILQ_INSERT_TAIL(&cont->counters, cnt_free, next);
4801 rte_spinlock_unlock(&cont->csl);
4807 * Get age param from counter index.
4810 * Pointer to the Ethernet device structure.
4811 * @param[in] counter
4812 * Index to the counter handler.
4815 * The aging parameter specified for the counter index.
4817 static struct mlx5_age_param*
4818 flow_dv_counter_idx_get_age(struct rte_eth_dev *dev,
4821 struct mlx5_flow_counter *cnt;
4822 struct mlx5_flow_counter_pool *pool = NULL;
4824 flow_dv_counter_get_by_idx(dev, counter, &pool);
4825 counter = (counter - 1) % MLX5_COUNTERS_PER_POOL;
4826 cnt = MLX5_POOL_GET_CNT(pool, counter);
4827 return MLX5_CNT_TO_AGE(cnt);
4831 * Remove a flow counter from aged counter list.
4834 * Pointer to the Ethernet device structure.
4835 * @param[in] counter
4836 * Index to the counter handler.
4838 * Pointer to the counter handler.
4841 flow_dv_counter_remove_from_age(struct rte_eth_dev *dev,
4842 uint32_t counter, struct mlx5_flow_counter *cnt)
4844 struct mlx5_age_info *age_info;
4845 struct mlx5_age_param *age_param;
4846 struct mlx5_priv *priv = dev->data->dev_private;
4848 age_info = GET_PORT_AGE_INFO(priv);
4849 age_param = flow_dv_counter_idx_get_age(dev, counter);
4850 if (rte_atomic16_cmpset((volatile uint16_t *)
4852 AGE_CANDIDATE, AGE_FREE)
4855 * We need the lock even it is age timeout,
4856 * since counter may still in process.
4858 rte_spinlock_lock(&age_info->aged_sl);
4859 TAILQ_REMOVE(&age_info->aged_counters, cnt, next);
4860 rte_spinlock_unlock(&age_info->aged_sl);
4862 rte_atomic16_set(&age_param->state, AGE_FREE);
4865 * Release a flow counter.
4868 * Pointer to the Ethernet device structure.
4869 * @param[in] counter
4870 * Index to the counter handler.
4873 flow_dv_counter_release(struct rte_eth_dev *dev, uint32_t counter)
4875 struct mlx5_priv *priv = dev->data->dev_private;
4876 struct mlx5_flow_counter_pool *pool = NULL;
4877 struct mlx5_flow_counter *cnt;
4878 struct mlx5_flow_counter_ext *cnt_ext = NULL;
4882 cnt = flow_dv_counter_get_by_idx(dev, counter, &pool);
4884 if (counter < MLX5_CNT_BATCH_OFFSET) {
4885 cnt_ext = MLX5_CNT_TO_CNT_EXT(pool, cnt);
4887 if (--cnt_ext->ref_cnt)
4889 if (cnt_ext->shared)
4890 mlx5_l3t_clear_entry(priv->sh->cnt_id_tbl,
4894 if (IS_AGE_POOL(pool))
4895 flow_dv_counter_remove_from_age(dev, counter, cnt);
4898 * Put the counter back to list to be updated in none fallback mode.
4899 * Currently, we are using two list alternately, while one is in query,
4900 * add the freed counter to the other list based on the pool query_gen
4901 * value. After query finishes, add counter the list to the global
4902 * container counter list. The list changes while query starts. In
4903 * this case, lock will not be needed as query callback and release
4904 * function both operate with the different list.
4907 if (!priv->counter_fallback)
4908 TAILQ_INSERT_TAIL(&pool->counters[pool->query_gen], cnt, next);
4910 TAILQ_INSERT_TAIL(&((MLX5_CNT_CONTAINER
4911 (priv->sh, 0, 0))->counters),
4916 * Verify the @p attributes will be correctly understood by the NIC and store
4917 * them in the @p flow if everything is correct.
4920 * Pointer to dev struct.
4921 * @param[in] attributes
4922 * Pointer to flow attributes
4923 * @param[in] external
4924 * This flow rule is created by request external to PMD.
4926 * Pointer to error structure.
4929 * - 0 on success and non root table.
4930 * - 1 on success and root table.
4931 * - a negative errno value otherwise and rte_errno is set.
4934 flow_dv_validate_attributes(struct rte_eth_dev *dev,
4935 const struct rte_flow_attr *attributes,
4936 bool external __rte_unused,
4937 struct rte_flow_error *error)
4939 struct mlx5_priv *priv = dev->data->dev_private;
4940 uint32_t priority_max = priv->config.flow_prio - 1;
4943 #ifndef HAVE_MLX5DV_DR
4944 if (attributes->group)
4945 return rte_flow_error_set(error, ENOTSUP,
4946 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
4948 "groups are not supported");
4952 ret = mlx5_flow_group_to_table(attributes, external,
4953 attributes->group, !!priv->fdb_def_rule,
4958 ret = MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL;
4960 if (attributes->priority != MLX5_FLOW_PRIO_RSVD &&
4961 attributes->priority >= priority_max)
4962 return rte_flow_error_set(error, ENOTSUP,
4963 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
4965 "priority out of range");
4966 if (attributes->transfer) {
4967 if (!priv->config.dv_esw_en)
4968 return rte_flow_error_set
4970 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4971 "E-Switch dr is not supported");
4972 if (!(priv->representor || priv->master))
4973 return rte_flow_error_set
4974 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4975 NULL, "E-Switch configuration can only be"
4976 " done by a master or a representor device");
4977 if (attributes->egress)
4978 return rte_flow_error_set
4980 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, attributes,
4981 "egress is not supported");
4983 if (!(attributes->egress ^ attributes->ingress))
4984 return rte_flow_error_set(error, ENOTSUP,
4985 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
4986 "must specify exactly one of "
4987 "ingress or egress");
4992 * Internal validation function. For validating both actions and items.
4995 * Pointer to the rte_eth_dev structure.
4997 * Pointer to the flow attributes.
4999 * Pointer to the list of items.
5000 * @param[in] actions
5001 * Pointer to the list of actions.
5002 * @param[in] external
5003 * This flow rule is created by request external to PMD.
5004 * @param[in] hairpin
5005 * Number of hairpin TX actions, 0 means classic flow.
5007 * Pointer to the error structure.
5010 * 0 on success, a negative errno value otherwise and rte_errno is set.
5013 flow_dv_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
5014 const struct rte_flow_item items[],
5015 const struct rte_flow_action actions[],
5016 bool external, int hairpin, struct rte_flow_error *error)
5019 uint64_t action_flags = 0;
5020 uint64_t item_flags = 0;
5021 uint64_t last_item = 0;
5022 uint8_t next_protocol = 0xff;
5023 uint16_t ether_type = 0;
5025 uint8_t item_ipv6_proto = 0;
5026 const struct rte_flow_item *gre_item = NULL;
5027 const struct rte_flow_action_raw_decap *decap;
5028 const struct rte_flow_action_raw_encap *encap;
5029 const struct rte_flow_action_rss *rss;
5030 const struct rte_flow_item_tcp nic_tcp_mask = {
5033 .src_port = RTE_BE16(UINT16_MAX),
5034 .dst_port = RTE_BE16(UINT16_MAX),
5037 const struct rte_flow_item_ipv4 nic_ipv4_mask = {
5039 .src_addr = RTE_BE32(0xffffffff),
5040 .dst_addr = RTE_BE32(0xffffffff),
5041 .type_of_service = 0xff,
5042 .next_proto_id = 0xff,
5043 .time_to_live = 0xff,
5046 const struct rte_flow_item_ipv6 nic_ipv6_mask = {
5049 "\xff\xff\xff\xff\xff\xff\xff\xff"
5050 "\xff\xff\xff\xff\xff\xff\xff\xff",
5052 "\xff\xff\xff\xff\xff\xff\xff\xff"
5053 "\xff\xff\xff\xff\xff\xff\xff\xff",
5054 .vtc_flow = RTE_BE32(0xffffffff),
5059 const struct rte_flow_item_ecpri nic_ecpri_mask = {
5063 RTE_BE32(((const struct rte_ecpri_common_hdr) {
5067 .dummy[0] = 0xffffffff,
5070 struct mlx5_priv *priv = dev->data->dev_private;
5071 struct mlx5_dev_config *dev_conf = &priv->config;
5072 uint16_t queue_index = 0xFFFF;
5073 const struct rte_flow_item_vlan *vlan_m = NULL;
5074 int16_t rw_act_num = 0;
5079 ret = flow_dv_validate_attributes(dev, attr, external, error);
5082 is_root = (uint64_t)ret;
5083 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
5084 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
5085 int type = items->type;
5087 if (!mlx5_flow_os_item_supported(type))
5088 return rte_flow_error_set(error, ENOTSUP,
5089 RTE_FLOW_ERROR_TYPE_ITEM,
5090 NULL, "item not supported");
5092 case RTE_FLOW_ITEM_TYPE_VOID:
5094 case RTE_FLOW_ITEM_TYPE_PORT_ID:
5095 ret = flow_dv_validate_item_port_id
5096 (dev, items, attr, item_flags, error);
5099 last_item = MLX5_FLOW_ITEM_PORT_ID;
5101 case RTE_FLOW_ITEM_TYPE_ETH:
5102 ret = mlx5_flow_validate_item_eth(items, item_flags,
5106 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
5107 MLX5_FLOW_LAYER_OUTER_L2;
5108 if (items->mask != NULL && items->spec != NULL) {
5110 ((const struct rte_flow_item_eth *)
5113 ((const struct rte_flow_item_eth *)
5115 ether_type = rte_be_to_cpu_16(ether_type);
5120 case RTE_FLOW_ITEM_TYPE_VLAN:
5121 ret = flow_dv_validate_item_vlan(items, item_flags,
5125 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
5126 MLX5_FLOW_LAYER_OUTER_VLAN;
5127 if (items->mask != NULL && items->spec != NULL) {
5129 ((const struct rte_flow_item_vlan *)
5130 items->spec)->inner_type;
5132 ((const struct rte_flow_item_vlan *)
5133 items->mask)->inner_type;
5134 ether_type = rte_be_to_cpu_16(ether_type);
5138 /* Store outer VLAN mask for of_push_vlan action. */
5140 vlan_m = items->mask;
5142 case RTE_FLOW_ITEM_TYPE_IPV4:
5143 mlx5_flow_tunnel_ip_check(items, next_protocol,
5144 &item_flags, &tunnel);
5145 ret = mlx5_flow_validate_item_ipv4(items, item_flags,
5152 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
5153 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
5154 if (items->mask != NULL &&
5155 ((const struct rte_flow_item_ipv4 *)
5156 items->mask)->hdr.next_proto_id) {
5158 ((const struct rte_flow_item_ipv4 *)
5159 (items->spec))->hdr.next_proto_id;
5161 ((const struct rte_flow_item_ipv4 *)
5162 (items->mask))->hdr.next_proto_id;
5164 /* Reset for inner layer. */
5165 next_protocol = 0xff;
5168 case RTE_FLOW_ITEM_TYPE_IPV6:
5169 mlx5_flow_tunnel_ip_check(items, next_protocol,
5170 &item_flags, &tunnel);
5171 ret = mlx5_flow_validate_item_ipv6(items, item_flags,
5178 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
5179 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
5180 if (items->mask != NULL &&
5181 ((const struct rte_flow_item_ipv6 *)
5182 items->mask)->hdr.proto) {
5184 ((const struct rte_flow_item_ipv6 *)
5185 items->spec)->hdr.proto;
5187 ((const struct rte_flow_item_ipv6 *)
5188 items->spec)->hdr.proto;
5190 ((const struct rte_flow_item_ipv6 *)
5191 items->mask)->hdr.proto;
5193 /* Reset for inner layer. */
5194 next_protocol = 0xff;
5197 case RTE_FLOW_ITEM_TYPE_TCP:
5198 ret = mlx5_flow_validate_item_tcp
5205 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
5206 MLX5_FLOW_LAYER_OUTER_L4_TCP;
5208 case RTE_FLOW_ITEM_TYPE_UDP:
5209 ret = mlx5_flow_validate_item_udp(items, item_flags,
5214 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
5215 MLX5_FLOW_LAYER_OUTER_L4_UDP;
5217 case RTE_FLOW_ITEM_TYPE_GRE:
5218 ret = mlx5_flow_validate_item_gre(items, item_flags,
5219 next_protocol, error);
5223 last_item = MLX5_FLOW_LAYER_GRE;
5225 case RTE_FLOW_ITEM_TYPE_NVGRE:
5226 ret = mlx5_flow_validate_item_nvgre(items, item_flags,
5231 last_item = MLX5_FLOW_LAYER_NVGRE;
5233 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
5234 ret = mlx5_flow_validate_item_gre_key
5235 (items, item_flags, gre_item, error);
5238 last_item = MLX5_FLOW_LAYER_GRE_KEY;
5240 case RTE_FLOW_ITEM_TYPE_VXLAN:
5241 ret = mlx5_flow_validate_item_vxlan(items, item_flags,
5245 last_item = MLX5_FLOW_LAYER_VXLAN;
5247 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
5248 ret = mlx5_flow_validate_item_vxlan_gpe(items,
5253 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
5255 case RTE_FLOW_ITEM_TYPE_GENEVE:
5256 ret = mlx5_flow_validate_item_geneve(items,
5261 last_item = MLX5_FLOW_LAYER_GENEVE;
5263 case RTE_FLOW_ITEM_TYPE_MPLS:
5264 ret = mlx5_flow_validate_item_mpls(dev, items,
5269 last_item = MLX5_FLOW_LAYER_MPLS;
5272 case RTE_FLOW_ITEM_TYPE_MARK:
5273 ret = flow_dv_validate_item_mark(dev, items, attr,
5277 last_item = MLX5_FLOW_ITEM_MARK;
5279 case RTE_FLOW_ITEM_TYPE_META:
5280 ret = flow_dv_validate_item_meta(dev, items, attr,
5284 last_item = MLX5_FLOW_ITEM_METADATA;
5286 case RTE_FLOW_ITEM_TYPE_ICMP:
5287 ret = mlx5_flow_validate_item_icmp(items, item_flags,
5292 last_item = MLX5_FLOW_LAYER_ICMP;
5294 case RTE_FLOW_ITEM_TYPE_ICMP6:
5295 ret = mlx5_flow_validate_item_icmp6(items, item_flags,
5300 item_ipv6_proto = IPPROTO_ICMPV6;
5301 last_item = MLX5_FLOW_LAYER_ICMP6;
5303 case RTE_FLOW_ITEM_TYPE_TAG:
5304 ret = flow_dv_validate_item_tag(dev, items,
5308 last_item = MLX5_FLOW_ITEM_TAG;
5310 case MLX5_RTE_FLOW_ITEM_TYPE_TAG:
5311 case MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE:
5313 case RTE_FLOW_ITEM_TYPE_GTP:
5314 ret = flow_dv_validate_item_gtp(dev, items, item_flags,
5318 last_item = MLX5_FLOW_LAYER_GTP;
5320 case RTE_FLOW_ITEM_TYPE_ECPRI:
5321 /* Capacity will be checked in the translate stage. */
5322 ret = mlx5_flow_validate_item_ecpri(items, item_flags,
5329 last_item = MLX5_FLOW_LAYER_ECPRI;
5332 return rte_flow_error_set(error, ENOTSUP,
5333 RTE_FLOW_ERROR_TYPE_ITEM,
5334 NULL, "item not supported");
5336 item_flags |= last_item;
5338 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
5339 int type = actions->type;
5341 if (!mlx5_flow_os_action_supported(type))
5342 return rte_flow_error_set(error, ENOTSUP,
5343 RTE_FLOW_ERROR_TYPE_ACTION,
5345 "action not supported");
5346 if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
5347 return rte_flow_error_set(error, ENOTSUP,
5348 RTE_FLOW_ERROR_TYPE_ACTION,
5349 actions, "too many actions");
5351 case RTE_FLOW_ACTION_TYPE_VOID:
5353 case RTE_FLOW_ACTION_TYPE_PORT_ID:
5354 ret = flow_dv_validate_action_port_id(dev,
5361 action_flags |= MLX5_FLOW_ACTION_PORT_ID;
5364 case RTE_FLOW_ACTION_TYPE_FLAG:
5365 ret = flow_dv_validate_action_flag(dev, action_flags,
5369 if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
5370 /* Count all modify-header actions as one. */
5371 if (!(action_flags &
5372 MLX5_FLOW_MODIFY_HDR_ACTIONS))
5374 action_flags |= MLX5_FLOW_ACTION_FLAG |
5375 MLX5_FLOW_ACTION_MARK_EXT;
5377 action_flags |= MLX5_FLOW_ACTION_FLAG;
5380 rw_act_num += MLX5_ACT_NUM_SET_MARK;
5382 case RTE_FLOW_ACTION_TYPE_MARK:
5383 ret = flow_dv_validate_action_mark(dev, actions,
5388 if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
5389 /* Count all modify-header actions as one. */
5390 if (!(action_flags &
5391 MLX5_FLOW_MODIFY_HDR_ACTIONS))
5393 action_flags |= MLX5_FLOW_ACTION_MARK |
5394 MLX5_FLOW_ACTION_MARK_EXT;
5396 action_flags |= MLX5_FLOW_ACTION_MARK;
5399 rw_act_num += MLX5_ACT_NUM_SET_MARK;
5401 case RTE_FLOW_ACTION_TYPE_SET_META:
5402 ret = flow_dv_validate_action_set_meta(dev, actions,
5407 /* Count all modify-header actions as one action. */
5408 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5410 action_flags |= MLX5_FLOW_ACTION_SET_META;
5411 rw_act_num += MLX5_ACT_NUM_SET_META;
5413 case RTE_FLOW_ACTION_TYPE_SET_TAG:
5414 ret = flow_dv_validate_action_set_tag(dev, actions,
5419 /* Count all modify-header actions as one action. */
5420 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5422 action_flags |= MLX5_FLOW_ACTION_SET_TAG;
5423 rw_act_num += MLX5_ACT_NUM_SET_TAG;
5425 case RTE_FLOW_ACTION_TYPE_DROP:
5426 ret = mlx5_flow_validate_action_drop(action_flags,
5430 action_flags |= MLX5_FLOW_ACTION_DROP;
5433 case RTE_FLOW_ACTION_TYPE_QUEUE:
5434 ret = mlx5_flow_validate_action_queue(actions,
5439 queue_index = ((const struct rte_flow_action_queue *)
5440 (actions->conf))->index;
5441 action_flags |= MLX5_FLOW_ACTION_QUEUE;
5444 case RTE_FLOW_ACTION_TYPE_RSS:
5445 rss = actions->conf;
5446 ret = mlx5_flow_validate_action_rss(actions,
5452 if (rss != NULL && rss->queue_num)
5453 queue_index = rss->queue[0];
5454 action_flags |= MLX5_FLOW_ACTION_RSS;
5457 case MLX5_RTE_FLOW_ACTION_TYPE_DEFAULT_MISS:
5459 mlx5_flow_validate_action_default_miss(action_flags,
5463 action_flags |= MLX5_FLOW_ACTION_DEFAULT_MISS;
5466 case RTE_FLOW_ACTION_TYPE_COUNT:
5467 ret = flow_dv_validate_action_count(dev, error);
5470 action_flags |= MLX5_FLOW_ACTION_COUNT;
5473 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
5474 if (flow_dv_validate_action_pop_vlan(dev,
5480 action_flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
5483 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
5484 ret = flow_dv_validate_action_push_vlan(dev,
5491 action_flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
5494 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
5495 ret = flow_dv_validate_action_set_vlan_pcp
5496 (action_flags, actions, error);
5499 /* Count PCP with push_vlan command. */
5500 action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_PCP;
5502 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
5503 ret = flow_dv_validate_action_set_vlan_vid
5504 (item_flags, action_flags,
5508 /* Count VID with push_vlan command. */
5509 action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
5510 rw_act_num += MLX5_ACT_NUM_MDF_VID;
5512 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
5513 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
5514 ret = flow_dv_validate_action_l2_encap(dev,
5520 action_flags |= MLX5_FLOW_ACTION_ENCAP;
5523 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
5524 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
5525 ret = flow_dv_validate_action_decap(dev, action_flags,
5529 action_flags |= MLX5_FLOW_ACTION_DECAP;
5532 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
5533 ret = flow_dv_validate_action_raw_encap_decap
5534 (dev, NULL, actions->conf, attr, &action_flags,
5539 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
5540 decap = actions->conf;
5541 while ((++actions)->type == RTE_FLOW_ACTION_TYPE_VOID)
5543 if (actions->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
5547 encap = actions->conf;
5549 ret = flow_dv_validate_action_raw_encap_decap
5551 decap ? decap : &empty_decap, encap,
5552 attr, &action_flags, &actions_n,
5557 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
5558 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
5559 ret = flow_dv_validate_action_modify_mac(action_flags,
5565 /* Count all modify-header actions as one action. */
5566 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5568 action_flags |= actions->type ==
5569 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
5570 MLX5_FLOW_ACTION_SET_MAC_SRC :
5571 MLX5_FLOW_ACTION_SET_MAC_DST;
5573 * Even if the source and destination MAC addresses have
5574 * overlap in the header with 4B alignment, the convert
5575 * function will handle them separately and 4 SW actions
5576 * will be created. And 2 actions will be added each
5577 * time no matter how many bytes of address will be set.
5579 rw_act_num += MLX5_ACT_NUM_MDF_MAC;
5581 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
5582 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
5583 ret = flow_dv_validate_action_modify_ipv4(action_flags,
5589 /* Count all modify-header actions as one action. */
5590 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5592 action_flags |= actions->type ==
5593 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
5594 MLX5_FLOW_ACTION_SET_IPV4_SRC :
5595 MLX5_FLOW_ACTION_SET_IPV4_DST;
5596 rw_act_num += MLX5_ACT_NUM_MDF_IPV4;
5598 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
5599 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
5600 ret = flow_dv_validate_action_modify_ipv6(action_flags,
5606 if (item_ipv6_proto == IPPROTO_ICMPV6)
5607 return rte_flow_error_set(error, ENOTSUP,
5608 RTE_FLOW_ERROR_TYPE_ACTION,
5610 "Can't change header "
5611 "with ICMPv6 proto");
5612 /* Count all modify-header actions as one action. */
5613 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5615 action_flags |= actions->type ==
5616 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
5617 MLX5_FLOW_ACTION_SET_IPV6_SRC :
5618 MLX5_FLOW_ACTION_SET_IPV6_DST;
5619 rw_act_num += MLX5_ACT_NUM_MDF_IPV6;
5621 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
5622 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
5623 ret = flow_dv_validate_action_modify_tp(action_flags,
5629 /* Count all modify-header actions as one action. */
5630 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5632 action_flags |= actions->type ==
5633 RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
5634 MLX5_FLOW_ACTION_SET_TP_SRC :
5635 MLX5_FLOW_ACTION_SET_TP_DST;
5636 rw_act_num += MLX5_ACT_NUM_MDF_PORT;
5638 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
5639 case RTE_FLOW_ACTION_TYPE_SET_TTL:
5640 ret = flow_dv_validate_action_modify_ttl(action_flags,
5646 /* Count all modify-header actions as one action. */
5647 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5649 action_flags |= actions->type ==
5650 RTE_FLOW_ACTION_TYPE_SET_TTL ?
5651 MLX5_FLOW_ACTION_SET_TTL :
5652 MLX5_FLOW_ACTION_DEC_TTL;
5653 rw_act_num += MLX5_ACT_NUM_MDF_TTL;
5655 case RTE_FLOW_ACTION_TYPE_JUMP:
5656 ret = flow_dv_validate_action_jump(actions,
5663 action_flags |= MLX5_FLOW_ACTION_JUMP;
5665 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
5666 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
5667 ret = flow_dv_validate_action_modify_tcp_seq
5674 /* Count all modify-header actions as one action. */
5675 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5677 action_flags |= actions->type ==
5678 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
5679 MLX5_FLOW_ACTION_INC_TCP_SEQ :
5680 MLX5_FLOW_ACTION_DEC_TCP_SEQ;
5681 rw_act_num += MLX5_ACT_NUM_MDF_TCPSEQ;
5683 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
5684 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
5685 ret = flow_dv_validate_action_modify_tcp_ack
5692 /* Count all modify-header actions as one action. */
5693 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5695 action_flags |= actions->type ==
5696 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
5697 MLX5_FLOW_ACTION_INC_TCP_ACK :
5698 MLX5_FLOW_ACTION_DEC_TCP_ACK;
5699 rw_act_num += MLX5_ACT_NUM_MDF_TCPACK;
5701 case MLX5_RTE_FLOW_ACTION_TYPE_MARK:
5703 case MLX5_RTE_FLOW_ACTION_TYPE_TAG:
5704 case MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG:
5705 rw_act_num += MLX5_ACT_NUM_SET_TAG;
5707 case RTE_FLOW_ACTION_TYPE_METER:
5708 ret = mlx5_flow_validate_action_meter(dev,
5714 action_flags |= MLX5_FLOW_ACTION_METER;
5716 /* Meter action will add one more TAG action. */
5717 rw_act_num += MLX5_ACT_NUM_SET_TAG;
5719 case RTE_FLOW_ACTION_TYPE_AGE:
5720 ret = flow_dv_validate_action_age(action_flags,
5725 action_flags |= MLX5_FLOW_ACTION_AGE;
5728 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
5729 ret = flow_dv_validate_action_modify_ipv4_dscp
5736 /* Count all modify-header actions as one action. */
5737 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5739 action_flags |= MLX5_FLOW_ACTION_SET_IPV4_DSCP;
5740 rw_act_num += MLX5_ACT_NUM_SET_DSCP;
5742 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
5743 ret = flow_dv_validate_action_modify_ipv6_dscp
5750 /* Count all modify-header actions as one action. */
5751 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5753 action_flags |= MLX5_FLOW_ACTION_SET_IPV6_DSCP;
5754 rw_act_num += MLX5_ACT_NUM_SET_DSCP;
5757 return rte_flow_error_set(error, ENOTSUP,
5758 RTE_FLOW_ERROR_TYPE_ACTION,
5760 "action not supported");
5764 * Validate the drop action mutual exclusion with other actions.
5765 * Drop action is mutually-exclusive with any other action, except for
5768 if ((action_flags & MLX5_FLOW_ACTION_DROP) &&
5769 (action_flags & ~(MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_COUNT)))
5770 return rte_flow_error_set(error, EINVAL,
5771 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5772 "Drop action is mutually-exclusive "
5773 "with any other action, except for "
5775 /* Eswitch has few restrictions on using items and actions */
5776 if (attr->transfer) {
5777 if (!mlx5_flow_ext_mreg_supported(dev) &&
5778 action_flags & MLX5_FLOW_ACTION_FLAG)
5779 return rte_flow_error_set(error, ENOTSUP,
5780 RTE_FLOW_ERROR_TYPE_ACTION,
5782 "unsupported action FLAG");
5783 if (!mlx5_flow_ext_mreg_supported(dev) &&
5784 action_flags & MLX5_FLOW_ACTION_MARK)
5785 return rte_flow_error_set(error, ENOTSUP,
5786 RTE_FLOW_ERROR_TYPE_ACTION,
5788 "unsupported action MARK");
5789 if (action_flags & MLX5_FLOW_ACTION_QUEUE)
5790 return rte_flow_error_set(error, ENOTSUP,
5791 RTE_FLOW_ERROR_TYPE_ACTION,
5793 "unsupported action QUEUE");
5794 if (action_flags & MLX5_FLOW_ACTION_RSS)
5795 return rte_flow_error_set(error, ENOTSUP,
5796 RTE_FLOW_ERROR_TYPE_ACTION,
5798 "unsupported action RSS");
5799 if (!(action_flags & MLX5_FLOW_FATE_ESWITCH_ACTIONS))
5800 return rte_flow_error_set(error, EINVAL,
5801 RTE_FLOW_ERROR_TYPE_ACTION,
5803 "no fate action is found");
5805 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS) && attr->ingress)
5806 return rte_flow_error_set(error, EINVAL,
5807 RTE_FLOW_ERROR_TYPE_ACTION,
5809 "no fate action is found");
5811 /* Continue validation for Xcap and VLAN actions.*/
5812 if ((action_flags & (MLX5_FLOW_XCAP_ACTIONS |
5813 MLX5_FLOW_VLAN_ACTIONS)) &&
5814 (queue_index == 0xFFFF ||
5815 mlx5_rxq_get_type(dev, queue_index) != MLX5_RXQ_TYPE_HAIRPIN)) {
5816 if ((action_flags & MLX5_FLOW_XCAP_ACTIONS) ==
5817 MLX5_FLOW_XCAP_ACTIONS)
5818 return rte_flow_error_set(error, ENOTSUP,
5819 RTE_FLOW_ERROR_TYPE_ACTION,
5820 NULL, "encap and decap "
5821 "combination aren't supported");
5822 if (!attr->transfer && attr->ingress) {
5823 if (action_flags & MLX5_FLOW_ACTION_ENCAP)
5824 return rte_flow_error_set
5826 RTE_FLOW_ERROR_TYPE_ACTION,
5827 NULL, "encap is not supported"
5828 " for ingress traffic");
5829 else if (action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN)
5830 return rte_flow_error_set
5832 RTE_FLOW_ERROR_TYPE_ACTION,
5833 NULL, "push VLAN action not "
5834 "supported for ingress");
5835 else if ((action_flags & MLX5_FLOW_VLAN_ACTIONS) ==
5836 MLX5_FLOW_VLAN_ACTIONS)
5837 return rte_flow_error_set
5839 RTE_FLOW_ERROR_TYPE_ACTION,
5840 NULL, "no support for "
5841 "multiple VLAN actions");
5844 /* Hairpin flow will add one more TAG action. */
5846 rw_act_num += MLX5_ACT_NUM_SET_TAG;
5847 /* extra metadata enabled: one more TAG action will be add. */
5848 if (dev_conf->dv_flow_en &&
5849 dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
5850 mlx5_flow_ext_mreg_supported(dev))
5851 rw_act_num += MLX5_ACT_NUM_SET_TAG;
5852 if ((uint32_t)rw_act_num >
5853 flow_dv_modify_hdr_action_max(dev, is_root)) {
5854 return rte_flow_error_set(error, ENOTSUP,
5855 RTE_FLOW_ERROR_TYPE_ACTION,
5856 NULL, "too many header modify"
5857 " actions to support");
5863 * Internal preparation function. Allocates the DV flow size,
5864 * this size is constant.
5867 * Pointer to the rte_eth_dev structure.
5869 * Pointer to the flow attributes.
5871 * Pointer to the list of items.
5872 * @param[in] actions
5873 * Pointer to the list of actions.
5875 * Pointer to the error structure.
5878 * Pointer to mlx5_flow object on success,
5879 * otherwise NULL and rte_errno is set.
5881 static struct mlx5_flow *
5882 flow_dv_prepare(struct rte_eth_dev *dev,
5883 const struct rte_flow_attr *attr __rte_unused,
5884 const struct rte_flow_item items[] __rte_unused,
5885 const struct rte_flow_action actions[] __rte_unused,
5886 struct rte_flow_error *error)
5888 uint32_t handle_idx = 0;
5889 struct mlx5_flow *dev_flow;
5890 struct mlx5_flow_handle *dev_handle;
5891 struct mlx5_priv *priv = dev->data->dev_private;
5893 /* In case of corrupting the memory. */
5894 if (priv->flow_idx >= MLX5_NUM_MAX_DEV_FLOWS) {
5895 rte_flow_error_set(error, ENOSPC,
5896 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5897 "not free temporary device flow");
5900 dev_handle = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
5903 rte_flow_error_set(error, ENOMEM,
5904 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5905 "not enough memory to create flow handle");
5908 /* No multi-thread supporting. */
5909 dev_flow = &((struct mlx5_flow *)priv->inter_flows)[priv->flow_idx++];
5910 dev_flow->handle = dev_handle;
5911 dev_flow->handle_idx = handle_idx;
5913 * In some old rdma-core releases, before continuing, a check of the
5914 * length of matching parameter will be done at first. It needs to use
5915 * the length without misc4 param. If the flow has misc4 support, then
5916 * the length needs to be adjusted accordingly. Each param member is
5917 * aligned with a 64B boundary naturally.
5919 dev_flow->dv.value.size = MLX5_ST_SZ_BYTES(fte_match_param) -
5920 MLX5_ST_SZ_BYTES(fte_match_set_misc4);
5922 * The matching value needs to be cleared to 0 before using. In the
5923 * past, it will be automatically cleared when using rte_*alloc
5924 * API. The time consumption will be almost the same as before.
5926 memset(dev_flow->dv.value.buf, 0, MLX5_ST_SZ_BYTES(fte_match_param));
5927 dev_flow->ingress = attr->ingress;
5928 dev_flow->dv.transfer = attr->transfer;
5932 #ifdef RTE_LIBRTE_MLX5_DEBUG
5934 * Sanity check for match mask and value. Similar to check_valid_spec() in
5935 * kernel driver. If unmasked bit is present in value, it returns failure.
5938 * pointer to match mask buffer.
5939 * @param match_value
5940 * pointer to match value buffer.
5943 * 0 if valid, -EINVAL otherwise.
5946 flow_dv_check_valid_spec(void *match_mask, void *match_value)
5948 uint8_t *m = match_mask;
5949 uint8_t *v = match_value;
5952 for (i = 0; i < MLX5_ST_SZ_BYTES(fte_match_param); ++i) {
5955 "match_value differs from match_criteria"
5956 " %p[%u] != %p[%u]",
5957 match_value, i, match_mask, i);
5966 * Add match of ip_version.
5970 * @param[in] headers_v
5971 * Values header pointer.
5972 * @param[in] headers_m
5973 * Masks header pointer.
5974 * @param[in] ip_version
5975 * The IP version to set.
5978 flow_dv_set_match_ip_version(uint32_t group,
5984 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
5986 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version,
5988 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, ip_version);
5989 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, 0);
5990 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype, 0);
5994 * Add Ethernet item to matcher and to the value.
5996 * @param[in, out] matcher
5998 * @param[in, out] key
5999 * Flow matcher value.
6001 * Flow pattern to translate.
6003 * Item is inner pattern.
6006 flow_dv_translate_item_eth(void *matcher, void *key,
6007 const struct rte_flow_item *item, int inner,
6010 const struct rte_flow_item_eth *eth_m = item->mask;
6011 const struct rte_flow_item_eth *eth_v = item->spec;
6012 const struct rte_flow_item_eth nic_mask = {
6013 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
6014 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
6015 .type = RTE_BE16(0xffff),
6027 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6029 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6031 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6033 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6035 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, dmac_47_16),
6036 ð_m->dst, sizeof(eth_m->dst));
6037 /* The value must be in the range of the mask. */
6038 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, dmac_47_16);
6039 for (i = 0; i < sizeof(eth_m->dst); ++i)
6040 l24_v[i] = eth_m->dst.addr_bytes[i] & eth_v->dst.addr_bytes[i];
6041 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, smac_47_16),
6042 ð_m->src, sizeof(eth_m->src));
6043 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, smac_47_16);
6044 /* The value must be in the range of the mask. */
6045 for (i = 0; i < sizeof(eth_m->dst); ++i)
6046 l24_v[i] = eth_m->src.addr_bytes[i] & eth_v->src.addr_bytes[i];
6048 /* When ethertype is present set mask for tagged VLAN. */
6049 MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
6050 /* Set value for tagged VLAN if ethertype is 802.1Q. */
6051 if (eth_v->type == RTE_BE16(RTE_ETHER_TYPE_VLAN) ||
6052 eth_v->type == RTE_BE16(RTE_ETHER_TYPE_QINQ)) {
6053 MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag,
6055 /* Return here to avoid setting match on ethertype. */
6060 * HW supports match on one Ethertype, the Ethertype following the last
6061 * VLAN tag of the packet (see PRM).
6062 * Set match on ethertype only if ETH header is not followed by VLAN.
6063 * HW is optimized for IPv4/IPv6. In such cases, avoid setting
6064 * ethertype, and use ip_version field instead.
6065 * eCPRI over Ether layer will use type value 0xAEFE.
6067 if (eth_v->type == RTE_BE16(RTE_ETHER_TYPE_IPV4) &&
6068 eth_m->type == 0xFFFF) {
6069 flow_dv_set_match_ip_version(group, headers_v, headers_m, 4);
6070 } else if (eth_v->type == RTE_BE16(RTE_ETHER_TYPE_IPV6) &&
6071 eth_m->type == 0xFFFF) {
6072 flow_dv_set_match_ip_version(group, headers_v, headers_m, 6);
6074 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype,
6075 rte_be_to_cpu_16(eth_m->type));
6076 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
6078 *(uint16_t *)(l24_v) = eth_m->type & eth_v->type;
6083 * Add VLAN item to matcher and to the value.
6085 * @param[in, out] dev_flow
6087 * @param[in, out] matcher
6089 * @param[in, out] key
6090 * Flow matcher value.
6092 * Flow pattern to translate.
6094 * Item is inner pattern.
6097 flow_dv_translate_item_vlan(struct mlx5_flow *dev_flow,
6098 void *matcher, void *key,
6099 const struct rte_flow_item *item,
6100 int inner, uint32_t group)
6102 const struct rte_flow_item_vlan *vlan_m = item->mask;
6103 const struct rte_flow_item_vlan *vlan_v = item->spec;
6110 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6112 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6114 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6116 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6118 * This is workaround, masks are not supported,
6119 * and pre-validated.
6122 dev_flow->handle->vf_vlan.tag =
6123 rte_be_to_cpu_16(vlan_v->tci) & 0x0fff;
6126 * When VLAN item exists in flow, mark packet as tagged,
6127 * even if TCI is not specified.
6129 MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
6130 MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag, 1);
6134 vlan_m = &rte_flow_item_vlan_mask;
6135 tci_m = rte_be_to_cpu_16(vlan_m->tci);
6136 tci_v = rte_be_to_cpu_16(vlan_m->tci & vlan_v->tci);
6137 MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_vid, tci_m);
6138 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, tci_v);
6139 MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_cfi, tci_m >> 12);
6140 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_cfi, tci_v >> 12);
6141 MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_prio, tci_m >> 13);
6142 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, tci_v >> 13);
6144 * HW is optimized for IPv4/IPv6. In such cases, avoid setting
6145 * ethertype, and use ip_version field instead.
6147 if (vlan_v->inner_type == RTE_BE16(RTE_ETHER_TYPE_IPV4) &&
6148 vlan_m->inner_type == 0xFFFF) {
6149 flow_dv_set_match_ip_version(group, headers_v, headers_m, 4);
6150 } else if (vlan_v->inner_type == RTE_BE16(RTE_ETHER_TYPE_IPV6) &&
6151 vlan_m->inner_type == 0xFFFF) {
6152 flow_dv_set_match_ip_version(group, headers_v, headers_m, 6);
6154 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype,
6155 rte_be_to_cpu_16(vlan_m->inner_type));
6156 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
6157 rte_be_to_cpu_16(vlan_m->inner_type &
6158 vlan_v->inner_type));
6163 * Add IPV4 item to matcher and to the value.
6165 * @param[in, out] matcher
6167 * @param[in, out] key
6168 * Flow matcher value.
6170 * Flow pattern to translate.
6171 * @param[in] item_flags
6172 * Bit-fields that holds the items detected until now.
6174 * Item is inner pattern.
6176 * The group to insert the rule.
6179 flow_dv_translate_item_ipv4(void *matcher, void *key,
6180 const struct rte_flow_item *item,
6181 const uint64_t item_flags,
6182 int inner, uint32_t group)
6184 const struct rte_flow_item_ipv4 *ipv4_m = item->mask;
6185 const struct rte_flow_item_ipv4 *ipv4_v = item->spec;
6186 const struct rte_flow_item_ipv4 nic_mask = {
6188 .src_addr = RTE_BE32(0xffffffff),
6189 .dst_addr = RTE_BE32(0xffffffff),
6190 .type_of_service = 0xff,
6191 .next_proto_id = 0xff,
6192 .time_to_live = 0xff,
6202 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6204 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6206 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6208 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6210 flow_dv_set_match_ip_version(group, headers_v, headers_m, 4);
6212 * On outer header (which must contains L2), or inner header with L2,
6213 * set cvlan_tag mask bit to mark this packet as untagged.
6214 * This should be done even if item->spec is empty.
6216 if (!inner || item_flags & MLX5_FLOW_LAYER_INNER_L2)
6217 MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
6222 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
6223 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
6224 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
6225 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
6226 *(uint32_t *)l24_m = ipv4_m->hdr.dst_addr;
6227 *(uint32_t *)l24_v = ipv4_m->hdr.dst_addr & ipv4_v->hdr.dst_addr;
6228 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
6229 src_ipv4_src_ipv6.ipv4_layout.ipv4);
6230 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
6231 src_ipv4_src_ipv6.ipv4_layout.ipv4);
6232 *(uint32_t *)l24_m = ipv4_m->hdr.src_addr;
6233 *(uint32_t *)l24_v = ipv4_m->hdr.src_addr & ipv4_v->hdr.src_addr;
6234 tos = ipv4_m->hdr.type_of_service & ipv4_v->hdr.type_of_service;
6235 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn,
6236 ipv4_m->hdr.type_of_service);
6237 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, tos);
6238 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp,
6239 ipv4_m->hdr.type_of_service >> 2);
6240 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, tos >> 2);
6241 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
6242 ipv4_m->hdr.next_proto_id);
6243 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
6244 ipv4_v->hdr.next_proto_id & ipv4_m->hdr.next_proto_id);
6245 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ttl_hoplimit,
6246 ipv4_m->hdr.time_to_live);
6247 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ttl_hoplimit,
6248 ipv4_v->hdr.time_to_live & ipv4_m->hdr.time_to_live);
6252 * Add IPV6 item to matcher and to the value.
6254 * @param[in, out] matcher
6256 * @param[in, out] key
6257 * Flow matcher value.
6259 * Flow pattern to translate.
6260 * @param[in] item_flags
6261 * Bit-fields that holds the items detected until now.
6263 * Item is inner pattern.
6265 * The group to insert the rule.
6268 flow_dv_translate_item_ipv6(void *matcher, void *key,
6269 const struct rte_flow_item *item,
6270 const uint64_t item_flags,
6271 int inner, uint32_t group)
6273 const struct rte_flow_item_ipv6 *ipv6_m = item->mask;
6274 const struct rte_flow_item_ipv6 *ipv6_v = item->spec;
6275 const struct rte_flow_item_ipv6 nic_mask = {
6278 "\xff\xff\xff\xff\xff\xff\xff\xff"
6279 "\xff\xff\xff\xff\xff\xff\xff\xff",
6281 "\xff\xff\xff\xff\xff\xff\xff\xff"
6282 "\xff\xff\xff\xff\xff\xff\xff\xff",
6283 .vtc_flow = RTE_BE32(0xffffffff),
6290 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6291 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6300 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6302 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6304 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6306 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6308 flow_dv_set_match_ip_version(group, headers_v, headers_m, 6);
6310 * On outer header (which must contains L2), or inner header with L2,
6311 * set cvlan_tag mask bit to mark this packet as untagged.
6312 * This should be done even if item->spec is empty.
6314 if (!inner || item_flags & MLX5_FLOW_LAYER_INNER_L2)
6315 MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
6320 size = sizeof(ipv6_m->hdr.dst_addr);
6321 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
6322 dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
6323 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
6324 dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
6325 memcpy(l24_m, ipv6_m->hdr.dst_addr, size);
6326 for (i = 0; i < size; ++i)
6327 l24_v[i] = l24_m[i] & ipv6_v->hdr.dst_addr[i];
6328 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
6329 src_ipv4_src_ipv6.ipv6_layout.ipv6);
6330 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
6331 src_ipv4_src_ipv6.ipv6_layout.ipv6);
6332 memcpy(l24_m, ipv6_m->hdr.src_addr, size);
6333 for (i = 0; i < size; ++i)
6334 l24_v[i] = l24_m[i] & ipv6_v->hdr.src_addr[i];
6336 vtc_m = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow);
6337 vtc_v = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow & ipv6_v->hdr.vtc_flow);
6338 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn, vtc_m >> 20);
6339 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, vtc_v >> 20);
6340 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp, vtc_m >> 22);
6341 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, vtc_v >> 22);
6344 MLX5_SET(fte_match_set_misc, misc_m, inner_ipv6_flow_label,
6346 MLX5_SET(fte_match_set_misc, misc_v, inner_ipv6_flow_label,
6349 MLX5_SET(fte_match_set_misc, misc_m, outer_ipv6_flow_label,
6351 MLX5_SET(fte_match_set_misc, misc_v, outer_ipv6_flow_label,
6355 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
6357 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
6358 ipv6_v->hdr.proto & ipv6_m->hdr.proto);
6360 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ttl_hoplimit,
6361 ipv6_m->hdr.hop_limits);
6362 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ttl_hoplimit,
6363 ipv6_v->hdr.hop_limits & ipv6_m->hdr.hop_limits);
6367 * Add TCP item to matcher and to the value.
6369 * @param[in, out] matcher
6371 * @param[in, out] key
6372 * Flow matcher value.
6374 * Flow pattern to translate.
6376 * Item is inner pattern.
6379 flow_dv_translate_item_tcp(void *matcher, void *key,
6380 const struct rte_flow_item *item,
6383 const struct rte_flow_item_tcp *tcp_m = item->mask;
6384 const struct rte_flow_item_tcp *tcp_v = item->spec;
6389 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6391 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6393 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6395 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6397 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
6398 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_TCP);
6402 tcp_m = &rte_flow_item_tcp_mask;
6403 MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_sport,
6404 rte_be_to_cpu_16(tcp_m->hdr.src_port));
6405 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
6406 rte_be_to_cpu_16(tcp_v->hdr.src_port & tcp_m->hdr.src_port));
6407 MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_dport,
6408 rte_be_to_cpu_16(tcp_m->hdr.dst_port));
6409 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
6410 rte_be_to_cpu_16(tcp_v->hdr.dst_port & tcp_m->hdr.dst_port));
6411 MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_flags,
6412 tcp_m->hdr.tcp_flags);
6413 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
6414 (tcp_v->hdr.tcp_flags & tcp_m->hdr.tcp_flags));
6418 * Add UDP item to matcher and to the value.
6420 * @param[in, out] matcher
6422 * @param[in, out] key
6423 * Flow matcher value.
6425 * Flow pattern to translate.
6427 * Item is inner pattern.
6430 flow_dv_translate_item_udp(void *matcher, void *key,
6431 const struct rte_flow_item *item,
6434 const struct rte_flow_item_udp *udp_m = item->mask;
6435 const struct rte_flow_item_udp *udp_v = item->spec;
6440 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6442 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6444 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6446 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6448 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
6449 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
6453 udp_m = &rte_flow_item_udp_mask;
6454 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_sport,
6455 rte_be_to_cpu_16(udp_m->hdr.src_port));
6456 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
6457 rte_be_to_cpu_16(udp_v->hdr.src_port & udp_m->hdr.src_port));
6458 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport,
6459 rte_be_to_cpu_16(udp_m->hdr.dst_port));
6460 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
6461 rte_be_to_cpu_16(udp_v->hdr.dst_port & udp_m->hdr.dst_port));
6465 * Add GRE optional Key item to matcher and to the value.
6467 * @param[in, out] matcher
6469 * @param[in, out] key
6470 * Flow matcher value.
6472 * Flow pattern to translate.
6474 * Item is inner pattern.
6477 flow_dv_translate_item_gre_key(void *matcher, void *key,
6478 const struct rte_flow_item *item)
6480 const rte_be32_t *key_m = item->mask;
6481 const rte_be32_t *key_v = item->spec;
6482 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6483 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6484 rte_be32_t gre_key_default_mask = RTE_BE32(UINT32_MAX);
6486 /* GRE K bit must be on and should already be validated */
6487 MLX5_SET(fte_match_set_misc, misc_m, gre_k_present, 1);
6488 MLX5_SET(fte_match_set_misc, misc_v, gre_k_present, 1);
6492 key_m = &gre_key_default_mask;
6493 MLX5_SET(fte_match_set_misc, misc_m, gre_key_h,
6494 rte_be_to_cpu_32(*key_m) >> 8);
6495 MLX5_SET(fte_match_set_misc, misc_v, gre_key_h,
6496 rte_be_to_cpu_32((*key_v) & (*key_m)) >> 8);
6497 MLX5_SET(fte_match_set_misc, misc_m, gre_key_l,
6498 rte_be_to_cpu_32(*key_m) & 0xFF);
6499 MLX5_SET(fte_match_set_misc, misc_v, gre_key_l,
6500 rte_be_to_cpu_32((*key_v) & (*key_m)) & 0xFF);
6504 * Add GRE item to matcher and to the value.
6506 * @param[in, out] matcher
6508 * @param[in, out] key
6509 * Flow matcher value.
6511 * Flow pattern to translate.
6513 * Item is inner pattern.
6516 flow_dv_translate_item_gre(void *matcher, void *key,
6517 const struct rte_flow_item *item,
6520 const struct rte_flow_item_gre *gre_m = item->mask;
6521 const struct rte_flow_item_gre *gre_v = item->spec;
6524 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6525 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6532 uint16_t s_present:1;
6533 uint16_t k_present:1;
6534 uint16_t rsvd_bit1:1;
6535 uint16_t c_present:1;
6539 } gre_crks_rsvd0_ver_m, gre_crks_rsvd0_ver_v;
6542 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6544 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6546 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6548 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6550 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
6551 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_GRE);
6555 gre_m = &rte_flow_item_gre_mask;
6556 MLX5_SET(fte_match_set_misc, misc_m, gre_protocol,
6557 rte_be_to_cpu_16(gre_m->protocol));
6558 MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
6559 rte_be_to_cpu_16(gre_v->protocol & gre_m->protocol));
6560 gre_crks_rsvd0_ver_m.value = rte_be_to_cpu_16(gre_m->c_rsvd0_ver);
6561 gre_crks_rsvd0_ver_v.value = rte_be_to_cpu_16(gre_v->c_rsvd0_ver);
6562 MLX5_SET(fte_match_set_misc, misc_m, gre_c_present,
6563 gre_crks_rsvd0_ver_m.c_present);
6564 MLX5_SET(fte_match_set_misc, misc_v, gre_c_present,
6565 gre_crks_rsvd0_ver_v.c_present &
6566 gre_crks_rsvd0_ver_m.c_present);
6567 MLX5_SET(fte_match_set_misc, misc_m, gre_k_present,
6568 gre_crks_rsvd0_ver_m.k_present);
6569 MLX5_SET(fte_match_set_misc, misc_v, gre_k_present,
6570 gre_crks_rsvd0_ver_v.k_present &
6571 gre_crks_rsvd0_ver_m.k_present);
6572 MLX5_SET(fte_match_set_misc, misc_m, gre_s_present,
6573 gre_crks_rsvd0_ver_m.s_present);
6574 MLX5_SET(fte_match_set_misc, misc_v, gre_s_present,
6575 gre_crks_rsvd0_ver_v.s_present &
6576 gre_crks_rsvd0_ver_m.s_present);
6580 * Add NVGRE item to matcher and to the value.
6582 * @param[in, out] matcher
6584 * @param[in, out] key
6585 * Flow matcher value.
6587 * Flow pattern to translate.
6589 * Item is inner pattern.
6592 flow_dv_translate_item_nvgre(void *matcher, void *key,
6593 const struct rte_flow_item *item,
6596 const struct rte_flow_item_nvgre *nvgre_m = item->mask;
6597 const struct rte_flow_item_nvgre *nvgre_v = item->spec;
6598 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6599 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6600 const char *tni_flow_id_m;
6601 const char *tni_flow_id_v;
6607 /* For NVGRE, GRE header fields must be set with defined values. */
6608 const struct rte_flow_item_gre gre_spec = {
6609 .c_rsvd0_ver = RTE_BE16(0x2000),
6610 .protocol = RTE_BE16(RTE_ETHER_TYPE_TEB)
6612 const struct rte_flow_item_gre gre_mask = {
6613 .c_rsvd0_ver = RTE_BE16(0xB000),
6614 .protocol = RTE_BE16(UINT16_MAX),
6616 const struct rte_flow_item gre_item = {
6621 flow_dv_translate_item_gre(matcher, key, &gre_item, inner);
6625 nvgre_m = &rte_flow_item_nvgre_mask;
6626 tni_flow_id_m = (const char *)nvgre_m->tni;
6627 tni_flow_id_v = (const char *)nvgre_v->tni;
6628 size = sizeof(nvgre_m->tni) + sizeof(nvgre_m->flow_id);
6629 gre_key_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, gre_key_h);
6630 gre_key_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, gre_key_h);
6631 memcpy(gre_key_m, tni_flow_id_m, size);
6632 for (i = 0; i < size; ++i)
6633 gre_key_v[i] = gre_key_m[i] & tni_flow_id_v[i];
6637 * Add VXLAN item to matcher and to the value.
6639 * @param[in, out] matcher
6641 * @param[in, out] key
6642 * Flow matcher value.
6644 * Flow pattern to translate.
6646 * Item is inner pattern.
6649 flow_dv_translate_item_vxlan(void *matcher, void *key,
6650 const struct rte_flow_item *item,
6653 const struct rte_flow_item_vxlan *vxlan_m = item->mask;
6654 const struct rte_flow_item_vxlan *vxlan_v = item->spec;
6657 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6658 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6666 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6668 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6670 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6672 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6674 dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
6675 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
6676 if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
6677 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
6678 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
6683 vxlan_m = &rte_flow_item_vxlan_mask;
6684 size = sizeof(vxlan_m->vni);
6685 vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, vxlan_vni);
6686 vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, vxlan_vni);
6687 memcpy(vni_m, vxlan_m->vni, size);
6688 for (i = 0; i < size; ++i)
6689 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
6693 * Add VXLAN-GPE item to matcher and to the value.
6695 * @param[in, out] matcher
6697 * @param[in, out] key
6698 * Flow matcher value.
6700 * Flow pattern to translate.
6702 * Item is inner pattern.
6706 flow_dv_translate_item_vxlan_gpe(void *matcher, void *key,
6707 const struct rte_flow_item *item, int inner)
6709 const struct rte_flow_item_vxlan_gpe *vxlan_m = item->mask;
6710 const struct rte_flow_item_vxlan_gpe *vxlan_v = item->spec;
6714 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_3);
6716 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
6722 uint8_t flags_m = 0xff;
6723 uint8_t flags_v = 0xc;
6726 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6728 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6730 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6732 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6734 dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
6735 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
6736 if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
6737 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
6738 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
6743 vxlan_m = &rte_flow_item_vxlan_gpe_mask;
6744 size = sizeof(vxlan_m->vni);
6745 vni_m = MLX5_ADDR_OF(fte_match_set_misc3, misc_m, outer_vxlan_gpe_vni);
6746 vni_v = MLX5_ADDR_OF(fte_match_set_misc3, misc_v, outer_vxlan_gpe_vni);
6747 memcpy(vni_m, vxlan_m->vni, size);
6748 for (i = 0; i < size; ++i)
6749 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
6750 if (vxlan_m->flags) {
6751 flags_m = vxlan_m->flags;
6752 flags_v = vxlan_v->flags;
6754 MLX5_SET(fte_match_set_misc3, misc_m, outer_vxlan_gpe_flags, flags_m);
6755 MLX5_SET(fte_match_set_misc3, misc_v, outer_vxlan_gpe_flags, flags_v);
6756 MLX5_SET(fte_match_set_misc3, misc_m, outer_vxlan_gpe_next_protocol,
6758 MLX5_SET(fte_match_set_misc3, misc_v, outer_vxlan_gpe_next_protocol,
6763 * Add Geneve item to matcher and to the value.
6765 * @param[in, out] matcher
6767 * @param[in, out] key
6768 * Flow matcher value.
6770 * Flow pattern to translate.
6772 * Item is inner pattern.
6776 flow_dv_translate_item_geneve(void *matcher, void *key,
6777 const struct rte_flow_item *item, int inner)
6779 const struct rte_flow_item_geneve *geneve_m = item->mask;
6780 const struct rte_flow_item_geneve *geneve_v = item->spec;
6783 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6784 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6793 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6795 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6797 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6799 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6801 dport = MLX5_UDP_PORT_GENEVE;
6802 if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
6803 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
6804 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
6809 geneve_m = &rte_flow_item_geneve_mask;
6810 size = sizeof(geneve_m->vni);
6811 vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, geneve_vni);
6812 vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, geneve_vni);
6813 memcpy(vni_m, geneve_m->vni, size);
6814 for (i = 0; i < size; ++i)
6815 vni_v[i] = vni_m[i] & geneve_v->vni[i];
6816 MLX5_SET(fte_match_set_misc, misc_m, geneve_protocol_type,
6817 rte_be_to_cpu_16(geneve_m->protocol));
6818 MLX5_SET(fte_match_set_misc, misc_v, geneve_protocol_type,
6819 rte_be_to_cpu_16(geneve_v->protocol & geneve_m->protocol));
6820 gbhdr_m = rte_be_to_cpu_16(geneve_m->ver_opt_len_o_c_rsvd0);
6821 gbhdr_v = rte_be_to_cpu_16(geneve_v->ver_opt_len_o_c_rsvd0);
6822 MLX5_SET(fte_match_set_misc, misc_m, geneve_oam,
6823 MLX5_GENEVE_OAMF_VAL(gbhdr_m));
6824 MLX5_SET(fte_match_set_misc, misc_v, geneve_oam,
6825 MLX5_GENEVE_OAMF_VAL(gbhdr_v) & MLX5_GENEVE_OAMF_VAL(gbhdr_m));
6826 MLX5_SET(fte_match_set_misc, misc_m, geneve_opt_len,
6827 MLX5_GENEVE_OPTLEN_VAL(gbhdr_m));
6828 MLX5_SET(fte_match_set_misc, misc_v, geneve_opt_len,
6829 MLX5_GENEVE_OPTLEN_VAL(gbhdr_v) &
6830 MLX5_GENEVE_OPTLEN_VAL(gbhdr_m));
6834 * Add MPLS item to matcher and to the value.
6836 * @param[in, out] matcher
6838 * @param[in, out] key
6839 * Flow matcher value.
6841 * Flow pattern to translate.
6842 * @param[in] prev_layer
6843 * The protocol layer indicated in previous item.
6845 * Item is inner pattern.
6848 flow_dv_translate_item_mpls(void *matcher, void *key,
6849 const struct rte_flow_item *item,
6850 uint64_t prev_layer,
6853 const uint32_t *in_mpls_m = item->mask;
6854 const uint32_t *in_mpls_v = item->spec;
6855 uint32_t *out_mpls_m = 0;
6856 uint32_t *out_mpls_v = 0;
6857 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6858 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6859 void *misc2_m = MLX5_ADDR_OF(fte_match_param, matcher,
6861 void *misc2_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
6862 void *headers_m = MLX5_ADDR_OF(fte_match_param, matcher, outer_headers);
6863 void *headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6865 switch (prev_layer) {
6866 case MLX5_FLOW_LAYER_OUTER_L4_UDP:
6867 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xffff);
6868 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
6869 MLX5_UDP_PORT_MPLS);
6871 case MLX5_FLOW_LAYER_GRE:
6872 MLX5_SET(fte_match_set_misc, misc_m, gre_protocol, 0xffff);
6873 MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
6874 RTE_ETHER_TYPE_MPLS);
6877 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
6878 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
6885 in_mpls_m = (const uint32_t *)&rte_flow_item_mpls_mask;
6886 switch (prev_layer) {
6887 case MLX5_FLOW_LAYER_OUTER_L4_UDP:
6889 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
6890 outer_first_mpls_over_udp);
6892 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
6893 outer_first_mpls_over_udp);
6895 case MLX5_FLOW_LAYER_GRE:
6897 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
6898 outer_first_mpls_over_gre);
6900 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
6901 outer_first_mpls_over_gre);
6904 /* Inner MPLS not over GRE is not supported. */
6907 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
6911 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
6917 if (out_mpls_m && out_mpls_v) {
6918 *out_mpls_m = *in_mpls_m;
6919 *out_mpls_v = *in_mpls_v & *in_mpls_m;
6924 * Add metadata register item to matcher
6926 * @param[in, out] matcher
6928 * @param[in, out] key
6929 * Flow matcher value.
6930 * @param[in] reg_type
6931 * Type of device metadata register
6938 flow_dv_match_meta_reg(void *matcher, void *key,
6939 enum modify_reg reg_type,
6940 uint32_t data, uint32_t mask)
6943 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_2);
6945 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
6951 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_a, mask);
6952 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_a, data);
6955 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_b, mask);
6956 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_b, data);
6960 * The metadata register C0 field might be divided into
6961 * source vport index and META item value, we should set
6962 * this field according to specified mask, not as whole one.
6964 temp = MLX5_GET(fte_match_set_misc2, misc2_m, metadata_reg_c_0);
6966 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_0, temp);
6967 temp = MLX5_GET(fte_match_set_misc2, misc2_v, metadata_reg_c_0);
6970 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_0, temp);
6973 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_1, mask);
6974 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_1, data);
6977 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_2, mask);
6978 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_2, data);
6981 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_3, mask);
6982 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_3, data);
6985 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_4, mask);
6986 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_4, data);
6989 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_5, mask);
6990 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_5, data);
6993 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_6, mask);
6994 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_6, data);
6997 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_7, mask);
6998 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_7, data);
7007 * Add MARK item to matcher
7010 * The device to configure through.
7011 * @param[in, out] matcher
7013 * @param[in, out] key
7014 * Flow matcher value.
7016 * Flow pattern to translate.
7019 flow_dv_translate_item_mark(struct rte_eth_dev *dev,
7020 void *matcher, void *key,
7021 const struct rte_flow_item *item)
7023 struct mlx5_priv *priv = dev->data->dev_private;
7024 const struct rte_flow_item_mark *mark;
7028 mark = item->mask ? (const void *)item->mask :
7029 &rte_flow_item_mark_mask;
7030 mask = mark->id & priv->sh->dv_mark_mask;
7031 mark = (const void *)item->spec;
7033 value = mark->id & priv->sh->dv_mark_mask & mask;
7035 enum modify_reg reg;
7037 /* Get the metadata register index for the mark. */
7038 reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, NULL);
7039 MLX5_ASSERT(reg > 0);
7040 if (reg == REG_C_0) {
7041 struct mlx5_priv *priv = dev->data->dev_private;
7042 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
7043 uint32_t shl_c0 = rte_bsf32(msk_c0);
7049 flow_dv_match_meta_reg(matcher, key, reg, value, mask);
7054 * Add META item to matcher
7057 * The devich to configure through.
7058 * @param[in, out] matcher
7060 * @param[in, out] key
7061 * Flow matcher value.
7063 * Attributes of flow that includes this item.
7065 * Flow pattern to translate.
7068 flow_dv_translate_item_meta(struct rte_eth_dev *dev,
7069 void *matcher, void *key,
7070 const struct rte_flow_attr *attr,
7071 const struct rte_flow_item *item)
7073 const struct rte_flow_item_meta *meta_m;
7074 const struct rte_flow_item_meta *meta_v;
7076 meta_m = (const void *)item->mask;
7078 meta_m = &rte_flow_item_meta_mask;
7079 meta_v = (const void *)item->spec;
7082 uint32_t value = meta_v->data;
7083 uint32_t mask = meta_m->data;
7085 reg = flow_dv_get_metadata_reg(dev, attr, NULL);
7089 * In datapath code there is no endianness
7090 * coversions for perfromance reasons, all
7091 * pattern conversions are done in rte_flow.
7093 value = rte_cpu_to_be_32(value);
7094 mask = rte_cpu_to_be_32(mask);
7095 if (reg == REG_C_0) {
7096 struct mlx5_priv *priv = dev->data->dev_private;
7097 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
7098 uint32_t shl_c0 = rte_bsf32(msk_c0);
7099 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
7100 uint32_t shr_c0 = __builtin_clz(priv->sh->dv_meta_mask);
7107 MLX5_ASSERT(msk_c0);
7108 MLX5_ASSERT(!(~msk_c0 & mask));
7110 flow_dv_match_meta_reg(matcher, key, reg, value, mask);
7115 * Add vport metadata Reg C0 item to matcher
7117 * @param[in, out] matcher
7119 * @param[in, out] key
7120 * Flow matcher value.
7122 * Flow pattern to translate.
7125 flow_dv_translate_item_meta_vport(void *matcher, void *key,
7126 uint32_t value, uint32_t mask)
7128 flow_dv_match_meta_reg(matcher, key, REG_C_0, value, mask);
7132 * Add tag item to matcher
7135 * The devich to configure through.
7136 * @param[in, out] matcher
7138 * @param[in, out] key
7139 * Flow matcher value.
7141 * Flow pattern to translate.
7144 flow_dv_translate_mlx5_item_tag(struct rte_eth_dev *dev,
7145 void *matcher, void *key,
7146 const struct rte_flow_item *item)
7148 const struct mlx5_rte_flow_item_tag *tag_v = item->spec;
7149 const struct mlx5_rte_flow_item_tag *tag_m = item->mask;
7150 uint32_t mask, value;
7153 value = tag_v->data;
7154 mask = tag_m ? tag_m->data : UINT32_MAX;
7155 if (tag_v->id == REG_C_0) {
7156 struct mlx5_priv *priv = dev->data->dev_private;
7157 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
7158 uint32_t shl_c0 = rte_bsf32(msk_c0);
7164 flow_dv_match_meta_reg(matcher, key, tag_v->id, value, mask);
7168 * Add TAG item to matcher
7171 * The devich to configure through.
7172 * @param[in, out] matcher
7174 * @param[in, out] key
7175 * Flow matcher value.
7177 * Flow pattern to translate.
7180 flow_dv_translate_item_tag(struct rte_eth_dev *dev,
7181 void *matcher, void *key,
7182 const struct rte_flow_item *item)
7184 const struct rte_flow_item_tag *tag_v = item->spec;
7185 const struct rte_flow_item_tag *tag_m = item->mask;
7186 enum modify_reg reg;
7189 tag_m = tag_m ? tag_m : &rte_flow_item_tag_mask;
7190 /* Get the metadata register index for the tag. */
7191 reg = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, tag_v->index, NULL);
7192 MLX5_ASSERT(reg > 0);
7193 flow_dv_match_meta_reg(matcher, key, reg, tag_v->data, tag_m->data);
7197 * Add source vport match to the specified matcher.
7199 * @param[in, out] matcher
7201 * @param[in, out] key
7202 * Flow matcher value.
7204 * Source vport value to match
7209 flow_dv_translate_item_source_vport(void *matcher, void *key,
7210 int16_t port, uint16_t mask)
7212 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
7213 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
7215 MLX5_SET(fte_match_set_misc, misc_m, source_port, mask);
7216 MLX5_SET(fte_match_set_misc, misc_v, source_port, port);
7220 * Translate port-id item to eswitch match on port-id.
7223 * The devich to configure through.
7224 * @param[in, out] matcher
7226 * @param[in, out] key
7227 * Flow matcher value.
7229 * Flow pattern to translate.
7232 * 0 on success, a negative errno value otherwise.
7235 flow_dv_translate_item_port_id(struct rte_eth_dev *dev, void *matcher,
7236 void *key, const struct rte_flow_item *item)
7238 const struct rte_flow_item_port_id *pid_m = item ? item->mask : NULL;
7239 const struct rte_flow_item_port_id *pid_v = item ? item->spec : NULL;
7240 struct mlx5_priv *priv;
7243 mask = pid_m ? pid_m->id : 0xffff;
7244 id = pid_v ? pid_v->id : dev->data->port_id;
7245 priv = mlx5_port_to_eswitch_info(id, item == NULL);
7248 /* Translate to vport field or to metadata, depending on mode. */
7249 if (priv->vport_meta_mask)
7250 flow_dv_translate_item_meta_vport(matcher, key,
7251 priv->vport_meta_tag,
7252 priv->vport_meta_mask);
7254 flow_dv_translate_item_source_vport(matcher, key,
7255 priv->vport_id, mask);
7260 * Add ICMP6 item to matcher and to the value.
7262 * @param[in, out] matcher
7264 * @param[in, out] key
7265 * Flow matcher value.
7267 * Flow pattern to translate.
7269 * Item is inner pattern.
7272 flow_dv_translate_item_icmp6(void *matcher, void *key,
7273 const struct rte_flow_item *item,
7276 const struct rte_flow_item_icmp6 *icmp6_m = item->mask;
7277 const struct rte_flow_item_icmp6 *icmp6_v = item->spec;
7280 void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
7282 void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
7284 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7286 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
7288 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7290 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
7292 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
7293 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMPV6);
7297 icmp6_m = &rte_flow_item_icmp6_mask;
7299 * Force flow only to match the non-fragmented IPv6 ICMPv6 packets.
7300 * If only the protocol is specified, no need to match the frag.
7302 MLX5_SET(fte_match_set_lyr_2_4, headers_m, frag, 1);
7303 MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 0);
7304 MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_type, icmp6_m->type);
7305 MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_type,
7306 icmp6_v->type & icmp6_m->type);
7307 MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_code, icmp6_m->code);
7308 MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_code,
7309 icmp6_v->code & icmp6_m->code);
7313 * Add ICMP item to matcher and to the value.
7315 * @param[in, out] matcher
7317 * @param[in, out] key
7318 * Flow matcher value.
7320 * Flow pattern to translate.
7322 * Item is inner pattern.
7325 flow_dv_translate_item_icmp(void *matcher, void *key,
7326 const struct rte_flow_item *item,
7329 const struct rte_flow_item_icmp *icmp_m = item->mask;
7330 const struct rte_flow_item_icmp *icmp_v = item->spec;
7333 void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
7335 void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
7337 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7339 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
7341 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7343 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
7345 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
7346 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMP);
7350 icmp_m = &rte_flow_item_icmp_mask;
7352 * Force flow only to match the non-fragmented IPv4 ICMP packets.
7353 * If only the protocol is specified, no need to match the frag.
7355 MLX5_SET(fte_match_set_lyr_2_4, headers_m, frag, 1);
7356 MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 0);
7357 MLX5_SET(fte_match_set_misc3, misc3_m, icmp_type,
7358 icmp_m->hdr.icmp_type);
7359 MLX5_SET(fte_match_set_misc3, misc3_v, icmp_type,
7360 icmp_v->hdr.icmp_type & icmp_m->hdr.icmp_type);
7361 MLX5_SET(fte_match_set_misc3, misc3_m, icmp_code,
7362 icmp_m->hdr.icmp_code);
7363 MLX5_SET(fte_match_set_misc3, misc3_v, icmp_code,
7364 icmp_v->hdr.icmp_code & icmp_m->hdr.icmp_code);
7368 * Add GTP item to matcher and to the value.
7370 * @param[in, out] matcher
7372 * @param[in, out] key
7373 * Flow matcher value.
7375 * Flow pattern to translate.
7377 * Item is inner pattern.
7380 flow_dv_translate_item_gtp(void *matcher, void *key,
7381 const struct rte_flow_item *item, int inner)
7383 const struct rte_flow_item_gtp *gtp_m = item->mask;
7384 const struct rte_flow_item_gtp *gtp_v = item->spec;
7387 void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
7389 void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
7390 uint16_t dport = RTE_GTPU_UDP_PORT;
7393 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7395 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
7397 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7399 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
7401 if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
7402 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
7403 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
7408 gtp_m = &rte_flow_item_gtp_mask;
7409 MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_msg_flags,
7410 gtp_m->v_pt_rsv_flags);
7411 MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_msg_flags,
7412 gtp_v->v_pt_rsv_flags & gtp_m->v_pt_rsv_flags);
7413 MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_msg_type, gtp_m->msg_type);
7414 MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_msg_type,
7415 gtp_v->msg_type & gtp_m->msg_type);
7416 MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_teid,
7417 rte_be_to_cpu_32(gtp_m->teid));
7418 MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_teid,
7419 rte_be_to_cpu_32(gtp_v->teid & gtp_m->teid));
7423 * Add eCPRI item to matcher and to the value.
7426 * The devich to configure through.
7427 * @param[in, out] matcher
7429 * @param[in, out] key
7430 * Flow matcher value.
7432 * Flow pattern to translate.
7433 * @param[in] samples
7434 * Sample IDs to be used in the matching.
7437 flow_dv_translate_item_ecpri(struct rte_eth_dev *dev, void *matcher,
7438 void *key, const struct rte_flow_item *item)
7440 struct mlx5_priv *priv = dev->data->dev_private;
7441 const struct rte_flow_item_ecpri *ecpri_m = item->mask;
7442 const struct rte_flow_item_ecpri *ecpri_v = item->spec;
7443 void *misc4_m = MLX5_ADDR_OF(fte_match_param, matcher,
7445 void *misc4_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_4);
7453 ecpri_m = &rte_flow_item_ecpri_mask;
7455 * Maximal four DW samples are supported in a single matching now.
7456 * Two are used now for a eCPRI matching:
7457 * 1. Type: one byte, mask should be 0x00ff0000 in network order
7458 * 2. ID of a message: one or two bytes, mask 0xffff0000 or 0xff000000
7461 if (!ecpri_m->hdr.common.u32)
7463 samples = priv->sh->fp[MLX5_FLEX_PARSER_ECPRI_0].ids;
7464 /* Need to take the whole DW as the mask to fill the entry. */
7465 dw_m = MLX5_ADDR_OF(fte_match_set_misc4, misc4_m,
7466 prog_sample_field_value_0);
7467 dw_v = MLX5_ADDR_OF(fte_match_set_misc4, misc4_v,
7468 prog_sample_field_value_0);
7469 /* Already big endian (network order) in the header. */
7470 *(uint32_t *)dw_m = ecpri_m->hdr.common.u32;
7471 *(uint32_t *)dw_v = ecpri_v->hdr.common.u32;
7472 /* Sample#0, used for matching type, offset 0. */
7473 MLX5_SET(fte_match_set_misc4, misc4_m,
7474 prog_sample_field_id_0, samples[0]);
7475 /* It makes no sense to set the sample ID in the mask field. */
7476 MLX5_SET(fte_match_set_misc4, misc4_v,
7477 prog_sample_field_id_0, samples[0]);
7479 * Checking if message body part needs to be matched.
7480 * Some wildcard rules only matching type field should be supported.
7482 if (ecpri_m->hdr.dummy[0]) {
7483 switch (ecpri_v->hdr.common.type) {
7484 case RTE_ECPRI_MSG_TYPE_IQ_DATA:
7485 case RTE_ECPRI_MSG_TYPE_RTC_CTRL:
7486 case RTE_ECPRI_MSG_TYPE_DLY_MSR:
7487 dw_m = MLX5_ADDR_OF(fte_match_set_misc4, misc4_m,
7488 prog_sample_field_value_1);
7489 dw_v = MLX5_ADDR_OF(fte_match_set_misc4, misc4_v,
7490 prog_sample_field_value_1);
7491 *(uint32_t *)dw_m = ecpri_m->hdr.dummy[0];
7492 *(uint32_t *)dw_v = ecpri_v->hdr.dummy[0];
7493 /* Sample#1, to match message body, offset 4. */
7494 MLX5_SET(fte_match_set_misc4, misc4_m,
7495 prog_sample_field_id_1, samples[1]);
7496 MLX5_SET(fte_match_set_misc4, misc4_v,
7497 prog_sample_field_id_1, samples[1]);
7500 /* Others, do not match any sample ID. */
7506 static uint32_t matcher_zero[MLX5_ST_SZ_DW(fte_match_param)] = { 0 };
7508 #define HEADER_IS_ZERO(match_criteria, headers) \
7509 !(memcmp(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
7510 matcher_zero, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
7513 * Calculate flow matcher enable bitmap.
7515 * @param match_criteria
7516 * Pointer to flow matcher criteria.
7519 * Bitmap of enabled fields.
7522 flow_dv_matcher_enable(uint32_t *match_criteria)
7524 uint8_t match_criteria_enable;
7526 match_criteria_enable =
7527 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
7528 MLX5_MATCH_CRITERIA_ENABLE_OUTER_BIT;
7529 match_criteria_enable |=
7530 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
7531 MLX5_MATCH_CRITERIA_ENABLE_MISC_BIT;
7532 match_criteria_enable |=
7533 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
7534 MLX5_MATCH_CRITERIA_ENABLE_INNER_BIT;
7535 match_criteria_enable |=
7536 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
7537 MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
7538 match_criteria_enable |=
7539 (!HEADER_IS_ZERO(match_criteria, misc_parameters_3)) <<
7540 MLX5_MATCH_CRITERIA_ENABLE_MISC3_BIT;
7541 match_criteria_enable |=
7542 (!HEADER_IS_ZERO(match_criteria, misc_parameters_4)) <<
7543 MLX5_MATCH_CRITERIA_ENABLE_MISC4_BIT;
7544 return match_criteria_enable;
7551 * @param[in, out] dev
7552 * Pointer to rte_eth_dev structure.
7553 * @param[in] table_id
7556 * Direction of the table.
7557 * @param[in] transfer
7558 * E-Switch or NIC flow.
7560 * pointer to error structure.
7563 * Returns tables resource based on the index, NULL in case of failed.
7565 static struct mlx5_flow_tbl_resource *
7566 flow_dv_tbl_resource_get(struct rte_eth_dev *dev,
7567 uint32_t table_id, uint8_t egress,
7569 struct rte_flow_error *error)
7571 struct mlx5_priv *priv = dev->data->dev_private;
7572 struct mlx5_dev_ctx_shared *sh = priv->sh;
7573 struct mlx5_flow_tbl_resource *tbl;
7574 union mlx5_flow_tbl_key table_key = {
7576 .table_id = table_id,
7578 .domain = !!transfer,
7579 .direction = !!egress,
7582 struct mlx5_hlist_entry *pos = mlx5_hlist_lookup(sh->flow_tbls,
7584 struct mlx5_flow_tbl_data_entry *tbl_data;
7590 tbl_data = container_of(pos, struct mlx5_flow_tbl_data_entry,
7592 tbl = &tbl_data->tbl;
7593 rte_atomic32_inc(&tbl->refcnt);
7596 tbl_data = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_JUMP], &idx);
7598 rte_flow_error_set(error, ENOMEM,
7599 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7601 "cannot allocate flow table data entry");
7604 tbl_data->idx = idx;
7605 tbl = &tbl_data->tbl;
7606 pos = &tbl_data->entry;
7608 domain = sh->fdb_domain;
7610 domain = sh->tx_domain;
7612 domain = sh->rx_domain;
7613 ret = mlx5_flow_os_create_flow_tbl(domain, table_id, &tbl->obj);
7615 rte_flow_error_set(error, ENOMEM,
7616 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7617 NULL, "cannot create flow table object");
7618 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_JUMP], idx);
7622 * No multi-threads now, but still better to initialize the reference
7623 * count before insert it into the hash list.
7625 rte_atomic32_init(&tbl->refcnt);
7626 /* Jump action reference count is initialized here. */
7627 rte_atomic32_init(&tbl_data->jump.refcnt);
7628 pos->key = table_key.v64;
7629 ret = mlx5_hlist_insert(sh->flow_tbls, pos);
7631 rte_flow_error_set(error, -ret,
7632 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
7633 "cannot insert flow table data entry");
7634 mlx5_flow_os_destroy_flow_tbl(tbl->obj);
7635 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_JUMP], idx);
7637 rte_atomic32_inc(&tbl->refcnt);
7642 * Release a flow table.
7645 * Pointer to rte_eth_dev structure.
7647 * Table resource to be released.
7650 * Returns 0 if table was released, else return 1;
7653 flow_dv_tbl_resource_release(struct rte_eth_dev *dev,
7654 struct mlx5_flow_tbl_resource *tbl)
7656 struct mlx5_priv *priv = dev->data->dev_private;
7657 struct mlx5_dev_ctx_shared *sh = priv->sh;
7658 struct mlx5_flow_tbl_data_entry *tbl_data =
7659 container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
7663 if (rte_atomic32_dec_and_test(&tbl->refcnt)) {
7664 struct mlx5_hlist_entry *pos = &tbl_data->entry;
7666 mlx5_flow_os_destroy_flow_tbl(tbl->obj);
7668 /* remove the entry from the hash list and free memory. */
7669 mlx5_hlist_remove(sh->flow_tbls, pos);
7670 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_JUMP],
7678 * Register the flow matcher.
7680 * @param[in, out] dev
7681 * Pointer to rte_eth_dev structure.
7682 * @param[in, out] matcher
7683 * Pointer to flow matcher.
7684 * @param[in, out] key
7685 * Pointer to flow table key.
7686 * @parm[in, out] dev_flow
7687 * Pointer to the dev_flow.
7689 * pointer to error structure.
7692 * 0 on success otherwise -errno and errno is set.
7695 flow_dv_matcher_register(struct rte_eth_dev *dev,
7696 struct mlx5_flow_dv_matcher *matcher,
7697 union mlx5_flow_tbl_key *key,
7698 struct mlx5_flow *dev_flow,
7699 struct rte_flow_error *error)
7701 struct mlx5_priv *priv = dev->data->dev_private;
7702 struct mlx5_dev_ctx_shared *sh = priv->sh;
7703 struct mlx5_flow_dv_matcher *cache_matcher;
7704 struct mlx5dv_flow_matcher_attr dv_attr = {
7705 .type = IBV_FLOW_ATTR_NORMAL,
7706 .match_mask = (void *)&matcher->mask,
7708 struct mlx5_flow_tbl_resource *tbl;
7709 struct mlx5_flow_tbl_data_entry *tbl_data;
7712 tbl = flow_dv_tbl_resource_get(dev, key->table_id, key->direction,
7713 key->domain, error);
7715 return -rte_errno; /* No need to refill the error info */
7716 tbl_data = container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
7717 /* Lookup from cache. */
7718 LIST_FOREACH(cache_matcher, &tbl_data->matchers, next) {
7719 if (matcher->crc == cache_matcher->crc &&
7720 matcher->priority == cache_matcher->priority &&
7721 !memcmp((const void *)matcher->mask.buf,
7722 (const void *)cache_matcher->mask.buf,
7723 cache_matcher->mask.size)) {
7725 "%s group %u priority %hd use %s "
7726 "matcher %p: refcnt %d++",
7727 key->domain ? "FDB" : "NIC", key->table_id,
7728 cache_matcher->priority,
7729 key->direction ? "tx" : "rx",
7730 (void *)cache_matcher,
7731 rte_atomic32_read(&cache_matcher->refcnt));
7732 rte_atomic32_inc(&cache_matcher->refcnt);
7733 dev_flow->handle->dvh.matcher = cache_matcher;
7734 /* old matcher should not make the table ref++. */
7735 flow_dv_tbl_resource_release(dev, tbl);
7739 /* Register new matcher. */
7740 cache_matcher = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*cache_matcher), 0,
7742 if (!cache_matcher) {
7743 flow_dv_tbl_resource_release(dev, tbl);
7744 return rte_flow_error_set(error, ENOMEM,
7745 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
7746 "cannot allocate matcher memory");
7748 *cache_matcher = *matcher;
7749 dv_attr.match_criteria_enable =
7750 flow_dv_matcher_enable(cache_matcher->mask.buf);
7751 dv_attr.priority = matcher->priority;
7753 dv_attr.flags |= IBV_FLOW_ATTR_FLAGS_EGRESS;
7754 ret = mlx5_flow_os_create_flow_matcher(sh->ctx, &dv_attr, tbl->obj,
7755 &cache_matcher->matcher_object);
7757 mlx5_free(cache_matcher);
7758 #ifdef HAVE_MLX5DV_DR
7759 flow_dv_tbl_resource_release(dev, tbl);
7761 return rte_flow_error_set(error, ENOMEM,
7762 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7763 NULL, "cannot create matcher");
7765 /* Save the table information */
7766 cache_matcher->tbl = tbl;
7767 rte_atomic32_init(&cache_matcher->refcnt);
7768 /* only matcher ref++, table ref++ already done above in get API. */
7769 rte_atomic32_inc(&cache_matcher->refcnt);
7770 LIST_INSERT_HEAD(&tbl_data->matchers, cache_matcher, next);
7771 dev_flow->handle->dvh.matcher = cache_matcher;
7772 DRV_LOG(DEBUG, "%s group %u priority %hd new %s matcher %p: refcnt %d",
7773 key->domain ? "FDB" : "NIC", key->table_id,
7774 cache_matcher->priority,
7775 key->direction ? "tx" : "rx", (void *)cache_matcher,
7776 rte_atomic32_read(&cache_matcher->refcnt));
7781 * Find existing tag resource or create and register a new one.
7783 * @param dev[in, out]
7784 * Pointer to rte_eth_dev structure.
7785 * @param[in, out] tag_be24
7786 * Tag value in big endian then R-shift 8.
7787 * @parm[in, out] dev_flow
7788 * Pointer to the dev_flow.
7790 * pointer to error structure.
7793 * 0 on success otherwise -errno and errno is set.
7796 flow_dv_tag_resource_register
7797 (struct rte_eth_dev *dev,
7799 struct mlx5_flow *dev_flow,
7800 struct rte_flow_error *error)
7802 struct mlx5_priv *priv = dev->data->dev_private;
7803 struct mlx5_dev_ctx_shared *sh = priv->sh;
7804 struct mlx5_flow_dv_tag_resource *cache_resource;
7805 struct mlx5_hlist_entry *entry;
7808 /* Lookup a matching resource from cache. */
7809 entry = mlx5_hlist_lookup(sh->tag_table, (uint64_t)tag_be24);
7811 cache_resource = container_of
7812 (entry, struct mlx5_flow_dv_tag_resource, entry);
7813 rte_atomic32_inc(&cache_resource->refcnt);
7814 dev_flow->handle->dvh.rix_tag = cache_resource->idx;
7815 dev_flow->dv.tag_resource = cache_resource;
7816 DRV_LOG(DEBUG, "cached tag resource %p: refcnt now %d++",
7817 (void *)cache_resource,
7818 rte_atomic32_read(&cache_resource->refcnt));
7821 /* Register new resource. */
7822 cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_TAG],
7823 &dev_flow->handle->dvh.rix_tag);
7824 if (!cache_resource)
7825 return rte_flow_error_set(error, ENOMEM,
7826 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
7827 "cannot allocate resource memory");
7828 cache_resource->entry.key = (uint64_t)tag_be24;
7829 ret = mlx5_flow_os_create_flow_action_tag(tag_be24,
7830 &cache_resource->action);
7832 mlx5_free(cache_resource);
7833 return rte_flow_error_set(error, ENOMEM,
7834 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7835 NULL, "cannot create action");
7837 rte_atomic32_init(&cache_resource->refcnt);
7838 rte_atomic32_inc(&cache_resource->refcnt);
7839 if (mlx5_hlist_insert(sh->tag_table, &cache_resource->entry)) {
7840 mlx5_flow_os_destroy_flow_action(cache_resource->action);
7841 mlx5_free(cache_resource);
7842 return rte_flow_error_set(error, EEXIST,
7843 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7844 NULL, "cannot insert tag");
7846 dev_flow->dv.tag_resource = cache_resource;
7847 DRV_LOG(DEBUG, "new tag resource %p: refcnt now %d++",
7848 (void *)cache_resource,
7849 rte_atomic32_read(&cache_resource->refcnt));
7857 * Pointer to Ethernet device.
7862 * 1 while a reference on it exists, 0 when freed.
7865 flow_dv_tag_release(struct rte_eth_dev *dev,
7868 struct mlx5_priv *priv = dev->data->dev_private;
7869 struct mlx5_dev_ctx_shared *sh = priv->sh;
7870 struct mlx5_flow_dv_tag_resource *tag;
7872 tag = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_TAG], tag_idx);
7875 DRV_LOG(DEBUG, "port %u tag %p: refcnt %d--",
7876 dev->data->port_id, (void *)tag,
7877 rte_atomic32_read(&tag->refcnt));
7878 if (rte_atomic32_dec_and_test(&tag->refcnt)) {
7879 claim_zero(mlx5_flow_os_destroy_flow_action(tag->action));
7880 mlx5_hlist_remove(sh->tag_table, &tag->entry);
7881 DRV_LOG(DEBUG, "port %u tag %p: removed",
7882 dev->data->port_id, (void *)tag);
7883 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_TAG], tag_idx);
7890 * Translate port ID action to vport.
7893 * Pointer to rte_eth_dev structure.
7895 * Pointer to the port ID action.
7896 * @param[out] dst_port_id
7897 * The target port ID.
7899 * Pointer to the error structure.
7902 * 0 on success, a negative errno value otherwise and rte_errno is set.
7905 flow_dv_translate_action_port_id(struct rte_eth_dev *dev,
7906 const struct rte_flow_action *action,
7907 uint32_t *dst_port_id,
7908 struct rte_flow_error *error)
7911 struct mlx5_priv *priv;
7912 const struct rte_flow_action_port_id *conf =
7913 (const struct rte_flow_action_port_id *)action->conf;
7915 port = conf->original ? dev->data->port_id : conf->id;
7916 priv = mlx5_port_to_eswitch_info(port, false);
7918 return rte_flow_error_set(error, -rte_errno,
7919 RTE_FLOW_ERROR_TYPE_ACTION,
7921 "No eswitch info was found for port");
7922 #ifdef HAVE_MLX5DV_DR_DEVX_PORT
7924 * This parameter is transferred to
7925 * mlx5dv_dr_action_create_dest_ib_port().
7927 *dst_port_id = priv->dev_port;
7930 * Legacy mode, no LAG configurations is supported.
7931 * This parameter is transferred to
7932 * mlx5dv_dr_action_create_dest_vport().
7934 *dst_port_id = priv->vport_id;
7940 * Create a counter with aging configuration.
7943 * Pointer to rte_eth_dev structure.
7945 * Pointer to the counter action configuration.
7947 * Pointer to the aging action configuration.
7950 * Index to flow counter on success, 0 otherwise.
7953 flow_dv_translate_create_counter(struct rte_eth_dev *dev,
7954 struct mlx5_flow *dev_flow,
7955 const struct rte_flow_action_count *count,
7956 const struct rte_flow_action_age *age)
7959 struct mlx5_age_param *age_param;
7961 counter = flow_dv_counter_alloc(dev,
7962 count ? count->shared : 0,
7963 count ? count->id : 0,
7964 dev_flow->dv.group, !!age);
7965 if (!counter || age == NULL)
7967 age_param = flow_dv_counter_idx_get_age(dev, counter);
7969 * The counter age accuracy may have a bit delay. Have 3/4
7970 * second bias on the timeount in order to let it age in time.
7972 age_param->context = age->context ? age->context :
7973 (void *)(uintptr_t)(dev_flow->flow_idx);
7975 * The counter age accuracy may have a bit delay. Have 3/4
7976 * second bias on the timeount in order to let it age in time.
7978 age_param->timeout = age->timeout * 10 - MLX5_AGING_TIME_DELAY;
7979 /* Set expire time in unit of 0.1 sec. */
7980 age_param->port_id = dev->data->port_id;
7981 age_param->expire = age_param->timeout +
7982 rte_rdtsc() / (rte_get_tsc_hz() / 10);
7983 rte_atomic16_set(&age_param->state, AGE_CANDIDATE);
7987 * Add Tx queue matcher
7990 * Pointer to the dev struct.
7991 * @param[in, out] matcher
7993 * @param[in, out] key
7994 * Flow matcher value.
7996 * Flow pattern to translate.
7998 * Item is inner pattern.
8001 flow_dv_translate_item_tx_queue(struct rte_eth_dev *dev,
8002 void *matcher, void *key,
8003 const struct rte_flow_item *item)
8005 const struct mlx5_rte_flow_item_tx_queue *queue_m;
8006 const struct mlx5_rte_flow_item_tx_queue *queue_v;
8008 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
8010 MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
8011 struct mlx5_txq_ctrl *txq;
8015 queue_m = (const void *)item->mask;
8018 queue_v = (const void *)item->spec;
8021 txq = mlx5_txq_get(dev, queue_v->queue);
8024 queue = txq->obj->sq->id;
8025 MLX5_SET(fte_match_set_misc, misc_m, source_sqn, queue_m->queue);
8026 MLX5_SET(fte_match_set_misc, misc_v, source_sqn,
8027 queue & queue_m->queue);
8028 mlx5_txq_release(dev, queue_v->queue);
8032 * Set the hash fields according to the @p flow information.
8034 * @param[in] dev_flow
8035 * Pointer to the mlx5_flow.
8036 * @param[in] rss_desc
8037 * Pointer to the mlx5_flow_rss_desc.
8040 flow_dv_hashfields_set(struct mlx5_flow *dev_flow,
8041 struct mlx5_flow_rss_desc *rss_desc)
8043 uint64_t items = dev_flow->handle->layers;
8045 uint64_t rss_types = rte_eth_rss_hf_refine(rss_desc->types);
8047 dev_flow->hash_fields = 0;
8048 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
8049 if (rss_desc->level >= 2) {
8050 dev_flow->hash_fields |= IBV_RX_HASH_INNER;
8054 if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L3_IPV4)) ||
8055 (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L3_IPV4))) {
8056 if (rss_types & MLX5_IPV4_LAYER_TYPES) {
8057 if (rss_types & ETH_RSS_L3_SRC_ONLY)
8058 dev_flow->hash_fields |= IBV_RX_HASH_SRC_IPV4;
8059 else if (rss_types & ETH_RSS_L3_DST_ONLY)
8060 dev_flow->hash_fields |= IBV_RX_HASH_DST_IPV4;
8062 dev_flow->hash_fields |= MLX5_IPV4_IBV_RX_HASH;
8064 } else if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L3_IPV6)) ||
8065 (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L3_IPV6))) {
8066 if (rss_types & MLX5_IPV6_LAYER_TYPES) {
8067 if (rss_types & ETH_RSS_L3_SRC_ONLY)
8068 dev_flow->hash_fields |= IBV_RX_HASH_SRC_IPV6;
8069 else if (rss_types & ETH_RSS_L3_DST_ONLY)
8070 dev_flow->hash_fields |= IBV_RX_HASH_DST_IPV6;
8072 dev_flow->hash_fields |= MLX5_IPV6_IBV_RX_HASH;
8075 if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L4_UDP)) ||
8076 (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L4_UDP))) {
8077 if (rss_types & ETH_RSS_UDP) {
8078 if (rss_types & ETH_RSS_L4_SRC_ONLY)
8079 dev_flow->hash_fields |=
8080 IBV_RX_HASH_SRC_PORT_UDP;
8081 else if (rss_types & ETH_RSS_L4_DST_ONLY)
8082 dev_flow->hash_fields |=
8083 IBV_RX_HASH_DST_PORT_UDP;
8085 dev_flow->hash_fields |= MLX5_UDP_IBV_RX_HASH;
8087 } else if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L4_TCP)) ||
8088 (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L4_TCP))) {
8089 if (rss_types & ETH_RSS_TCP) {
8090 if (rss_types & ETH_RSS_L4_SRC_ONLY)
8091 dev_flow->hash_fields |=
8092 IBV_RX_HASH_SRC_PORT_TCP;
8093 else if (rss_types & ETH_RSS_L4_DST_ONLY)
8094 dev_flow->hash_fields |=
8095 IBV_RX_HASH_DST_PORT_TCP;
8097 dev_flow->hash_fields |= MLX5_TCP_IBV_RX_HASH;
8103 * Fill the flow with DV spec, lock free
8104 * (mutex should be acquired by caller).
8107 * Pointer to rte_eth_dev structure.
8108 * @param[in, out] dev_flow
8109 * Pointer to the sub flow.
8111 * Pointer to the flow attributes.
8113 * Pointer to the list of items.
8114 * @param[in] actions
8115 * Pointer to the list of actions.
8117 * Pointer to the error structure.
8120 * 0 on success, a negative errno value otherwise and rte_errno is set.
8123 __flow_dv_translate(struct rte_eth_dev *dev,
8124 struct mlx5_flow *dev_flow,
8125 const struct rte_flow_attr *attr,
8126 const struct rte_flow_item items[],
8127 const struct rte_flow_action actions[],
8128 struct rte_flow_error *error)
8130 struct mlx5_priv *priv = dev->data->dev_private;
8131 struct mlx5_dev_config *dev_conf = &priv->config;
8132 struct rte_flow *flow = dev_flow->flow;
8133 struct mlx5_flow_handle *handle = dev_flow->handle;
8134 struct mlx5_flow_rss_desc *rss_desc = &((struct mlx5_flow_rss_desc *)
8136 [!!priv->flow_nested_idx];
8137 uint64_t item_flags = 0;
8138 uint64_t last_item = 0;
8139 uint64_t action_flags = 0;
8140 uint64_t priority = attr->priority;
8141 struct mlx5_flow_dv_matcher matcher = {
8143 .size = sizeof(matcher.mask.buf) -
8144 MLX5_ST_SZ_BYTES(fte_match_set_misc4),
8148 bool actions_end = false;
8150 struct mlx5_flow_dv_modify_hdr_resource res;
8151 uint8_t len[sizeof(struct mlx5_flow_dv_modify_hdr_resource) +
8152 sizeof(struct mlx5_modification_cmd) *
8153 (MLX5_MAX_MODIFY_NUM + 1)];
8155 struct mlx5_flow_dv_modify_hdr_resource *mhdr_res = &mhdr_dummy.res;
8156 const struct rte_flow_action_count *count = NULL;
8157 const struct rte_flow_action_age *age = NULL;
8158 union flow_dv_attr flow_attr = { .attr = 0 };
8160 union mlx5_flow_tbl_key tbl_key;
8161 uint32_t modify_action_position = UINT32_MAX;
8162 void *match_mask = matcher.mask.buf;
8163 void *match_value = dev_flow->dv.value.buf;
8164 uint8_t next_protocol = 0xff;
8165 struct rte_vlan_hdr vlan = { 0 };
8169 mhdr_res->ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
8170 MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
8171 ret = mlx5_flow_group_to_table(attr, dev_flow->external, attr->group,
8172 !!priv->fdb_def_rule, &table, error);
8175 dev_flow->dv.group = table;
8177 mhdr_res->ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
8178 if (priority == MLX5_FLOW_PRIO_RSVD)
8179 priority = dev_conf->flow_prio - 1;
8180 /* number of actions must be set to 0 in case of dirty stack. */
8181 mhdr_res->actions_num = 0;
8182 for (; !actions_end ; actions++) {
8183 const struct rte_flow_action_queue *queue;
8184 const struct rte_flow_action_rss *rss;
8185 const struct rte_flow_action *action = actions;
8186 const uint8_t *rss_key;
8187 const struct rte_flow_action_jump *jump_data;
8188 const struct rte_flow_action_meter *mtr;
8189 struct mlx5_flow_tbl_resource *tbl;
8190 uint32_t port_id = 0;
8191 struct mlx5_flow_dv_port_id_action_resource port_id_resource;
8192 int action_type = actions->type;
8193 const struct rte_flow_action *found_action = NULL;
8194 struct mlx5_flow_meter *fm = NULL;
8196 if (!mlx5_flow_os_action_supported(action_type))
8197 return rte_flow_error_set(error, ENOTSUP,
8198 RTE_FLOW_ERROR_TYPE_ACTION,
8200 "action not supported");
8201 switch (action_type) {
8202 case RTE_FLOW_ACTION_TYPE_VOID:
8204 case RTE_FLOW_ACTION_TYPE_PORT_ID:
8205 if (flow_dv_translate_action_port_id(dev, action,
8208 port_id_resource.port_id = port_id;
8209 MLX5_ASSERT(!handle->rix_port_id_action);
8210 if (flow_dv_port_id_action_resource_register
8211 (dev, &port_id_resource, dev_flow, error))
8213 dev_flow->dv.actions[actions_n++] =
8214 dev_flow->dv.port_id_action->action;
8215 action_flags |= MLX5_FLOW_ACTION_PORT_ID;
8216 dev_flow->handle->fate_action = MLX5_FLOW_FATE_PORT_ID;
8218 case RTE_FLOW_ACTION_TYPE_FLAG:
8219 action_flags |= MLX5_FLOW_ACTION_FLAG;
8220 dev_flow->handle->mark = 1;
8221 if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
8222 struct rte_flow_action_mark mark = {
8223 .id = MLX5_FLOW_MARK_DEFAULT,
8226 if (flow_dv_convert_action_mark(dev, &mark,
8230 action_flags |= MLX5_FLOW_ACTION_MARK_EXT;
8233 tag_be = mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT);
8235 * Only one FLAG or MARK is supported per device flow
8236 * right now. So the pointer to the tag resource must be
8237 * zero before the register process.
8239 MLX5_ASSERT(!handle->dvh.rix_tag);
8240 if (flow_dv_tag_resource_register(dev, tag_be,
8243 MLX5_ASSERT(dev_flow->dv.tag_resource);
8244 dev_flow->dv.actions[actions_n++] =
8245 dev_flow->dv.tag_resource->action;
8247 case RTE_FLOW_ACTION_TYPE_MARK:
8248 action_flags |= MLX5_FLOW_ACTION_MARK;
8249 dev_flow->handle->mark = 1;
8250 if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
8251 const struct rte_flow_action_mark *mark =
8252 (const struct rte_flow_action_mark *)
8255 if (flow_dv_convert_action_mark(dev, mark,
8259 action_flags |= MLX5_FLOW_ACTION_MARK_EXT;
8263 case MLX5_RTE_FLOW_ACTION_TYPE_MARK:
8264 /* Legacy (non-extensive) MARK action. */
8265 tag_be = mlx5_flow_mark_set
8266 (((const struct rte_flow_action_mark *)
8267 (actions->conf))->id);
8268 MLX5_ASSERT(!handle->dvh.rix_tag);
8269 if (flow_dv_tag_resource_register(dev, tag_be,
8272 MLX5_ASSERT(dev_flow->dv.tag_resource);
8273 dev_flow->dv.actions[actions_n++] =
8274 dev_flow->dv.tag_resource->action;
8276 case RTE_FLOW_ACTION_TYPE_SET_META:
8277 if (flow_dv_convert_action_set_meta
8278 (dev, mhdr_res, attr,
8279 (const struct rte_flow_action_set_meta *)
8280 actions->conf, error))
8282 action_flags |= MLX5_FLOW_ACTION_SET_META;
8284 case RTE_FLOW_ACTION_TYPE_SET_TAG:
8285 if (flow_dv_convert_action_set_tag
8287 (const struct rte_flow_action_set_tag *)
8288 actions->conf, error))
8290 action_flags |= MLX5_FLOW_ACTION_SET_TAG;
8292 case RTE_FLOW_ACTION_TYPE_DROP:
8293 action_flags |= MLX5_FLOW_ACTION_DROP;
8294 dev_flow->handle->fate_action = MLX5_FLOW_FATE_DROP;
8296 case RTE_FLOW_ACTION_TYPE_QUEUE:
8297 queue = actions->conf;
8298 rss_desc->queue_num = 1;
8299 rss_desc->queue[0] = queue->index;
8300 action_flags |= MLX5_FLOW_ACTION_QUEUE;
8301 dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
8303 case RTE_FLOW_ACTION_TYPE_RSS:
8304 rss = actions->conf;
8305 memcpy(rss_desc->queue, rss->queue,
8306 rss->queue_num * sizeof(uint16_t));
8307 rss_desc->queue_num = rss->queue_num;
8308 /* NULL RSS key indicates default RSS key. */
8309 rss_key = !rss->key ? rss_hash_default_key : rss->key;
8310 memcpy(rss_desc->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
8312 * rss->level and rss.types should be set in advance
8313 * when expanding items for RSS.
8315 action_flags |= MLX5_FLOW_ACTION_RSS;
8316 dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
8318 case RTE_FLOW_ACTION_TYPE_AGE:
8319 case RTE_FLOW_ACTION_TYPE_COUNT:
8320 if (!dev_conf->devx) {
8321 return rte_flow_error_set
8323 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8325 "count action not supported");
8327 /* Save information first, will apply later. */
8328 if (actions->type == RTE_FLOW_ACTION_TYPE_COUNT)
8329 count = action->conf;
8332 action_flags |= MLX5_FLOW_ACTION_COUNT;
8334 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
8335 dev_flow->dv.actions[actions_n++] =
8336 priv->sh->pop_vlan_action;
8337 action_flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
8339 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
8340 if (!(action_flags &
8341 MLX5_FLOW_ACTION_OF_SET_VLAN_VID))
8342 flow_dev_get_vlan_info_from_items(items, &vlan);
8343 vlan.eth_proto = rte_be_to_cpu_16
8344 ((((const struct rte_flow_action_of_push_vlan *)
8345 actions->conf)->ethertype));
8346 found_action = mlx5_flow_find_action
8348 RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID);
8350 mlx5_update_vlan_vid_pcp(found_action, &vlan);
8351 found_action = mlx5_flow_find_action
8353 RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP);
8355 mlx5_update_vlan_vid_pcp(found_action, &vlan);
8356 if (flow_dv_create_action_push_vlan
8357 (dev, attr, &vlan, dev_flow, error))
8359 dev_flow->dv.actions[actions_n++] =
8360 dev_flow->dv.push_vlan_res->action;
8361 action_flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
8363 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
8364 /* of_vlan_push action handled this action */
8365 MLX5_ASSERT(action_flags &
8366 MLX5_FLOW_ACTION_OF_PUSH_VLAN);
8368 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
8369 if (action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN)
8371 flow_dev_get_vlan_info_from_items(items, &vlan);
8372 mlx5_update_vlan_vid_pcp(actions, &vlan);
8373 /* If no VLAN push - this is a modify header action */
8374 if (flow_dv_convert_action_modify_vlan_vid
8375 (mhdr_res, actions, error))
8377 action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
8379 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
8380 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
8381 if (flow_dv_create_action_l2_encap(dev, actions,
8386 dev_flow->dv.actions[actions_n++] =
8387 dev_flow->dv.encap_decap->action;
8388 action_flags |= MLX5_FLOW_ACTION_ENCAP;
8390 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
8391 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
8392 if (flow_dv_create_action_l2_decap(dev, dev_flow,
8396 dev_flow->dv.actions[actions_n++] =
8397 dev_flow->dv.encap_decap->action;
8398 action_flags |= MLX5_FLOW_ACTION_DECAP;
8400 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
8401 /* Handle encap with preceding decap. */
8402 if (action_flags & MLX5_FLOW_ACTION_DECAP) {
8403 if (flow_dv_create_action_raw_encap
8404 (dev, actions, dev_flow, attr, error))
8406 dev_flow->dv.actions[actions_n++] =
8407 dev_flow->dv.encap_decap->action;
8409 /* Handle encap without preceding decap. */
8410 if (flow_dv_create_action_l2_encap
8411 (dev, actions, dev_flow, attr->transfer,
8414 dev_flow->dv.actions[actions_n++] =
8415 dev_flow->dv.encap_decap->action;
8417 action_flags |= MLX5_FLOW_ACTION_ENCAP;
8419 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
8420 while ((++action)->type == RTE_FLOW_ACTION_TYPE_VOID)
8422 if (action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
8423 if (flow_dv_create_action_l2_decap
8424 (dev, dev_flow, attr->transfer, error))
8426 dev_flow->dv.actions[actions_n++] =
8427 dev_flow->dv.encap_decap->action;
8429 /* If decap is followed by encap, handle it at encap. */
8430 action_flags |= MLX5_FLOW_ACTION_DECAP;
8432 case RTE_FLOW_ACTION_TYPE_JUMP:
8433 jump_data = action->conf;
8434 ret = mlx5_flow_group_to_table(attr, dev_flow->external,
8436 !!priv->fdb_def_rule,
8440 tbl = flow_dv_tbl_resource_get(dev, table,
8442 attr->transfer, error);
8444 return rte_flow_error_set
8446 RTE_FLOW_ERROR_TYPE_ACTION,
8448 "cannot create jump action.");
8449 if (flow_dv_jump_tbl_resource_register
8450 (dev, tbl, dev_flow, error)) {
8451 flow_dv_tbl_resource_release(dev, tbl);
8452 return rte_flow_error_set
8454 RTE_FLOW_ERROR_TYPE_ACTION,
8456 "cannot create jump action.");
8458 dev_flow->dv.actions[actions_n++] =
8459 dev_flow->dv.jump->action;
8460 action_flags |= MLX5_FLOW_ACTION_JUMP;
8461 dev_flow->handle->fate_action = MLX5_FLOW_FATE_JUMP;
8463 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
8464 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
8465 if (flow_dv_convert_action_modify_mac
8466 (mhdr_res, actions, error))
8468 action_flags |= actions->type ==
8469 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
8470 MLX5_FLOW_ACTION_SET_MAC_SRC :
8471 MLX5_FLOW_ACTION_SET_MAC_DST;
8473 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
8474 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
8475 if (flow_dv_convert_action_modify_ipv4
8476 (mhdr_res, actions, error))
8478 action_flags |= actions->type ==
8479 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
8480 MLX5_FLOW_ACTION_SET_IPV4_SRC :
8481 MLX5_FLOW_ACTION_SET_IPV4_DST;
8483 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
8484 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
8485 if (flow_dv_convert_action_modify_ipv6
8486 (mhdr_res, actions, error))
8488 action_flags |= actions->type ==
8489 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
8490 MLX5_FLOW_ACTION_SET_IPV6_SRC :
8491 MLX5_FLOW_ACTION_SET_IPV6_DST;
8493 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
8494 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
8495 if (flow_dv_convert_action_modify_tp
8496 (mhdr_res, actions, items,
8497 &flow_attr, dev_flow, !!(action_flags &
8498 MLX5_FLOW_ACTION_DECAP), error))
8500 action_flags |= actions->type ==
8501 RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
8502 MLX5_FLOW_ACTION_SET_TP_SRC :
8503 MLX5_FLOW_ACTION_SET_TP_DST;
8505 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
8506 if (flow_dv_convert_action_modify_dec_ttl
8507 (mhdr_res, items, &flow_attr, dev_flow,
8509 MLX5_FLOW_ACTION_DECAP), error))
8511 action_flags |= MLX5_FLOW_ACTION_DEC_TTL;
8513 case RTE_FLOW_ACTION_TYPE_SET_TTL:
8514 if (flow_dv_convert_action_modify_ttl
8515 (mhdr_res, actions, items, &flow_attr,
8516 dev_flow, !!(action_flags &
8517 MLX5_FLOW_ACTION_DECAP), error))
8519 action_flags |= MLX5_FLOW_ACTION_SET_TTL;
8521 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
8522 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
8523 if (flow_dv_convert_action_modify_tcp_seq
8524 (mhdr_res, actions, error))
8526 action_flags |= actions->type ==
8527 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
8528 MLX5_FLOW_ACTION_INC_TCP_SEQ :
8529 MLX5_FLOW_ACTION_DEC_TCP_SEQ;
8532 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
8533 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
8534 if (flow_dv_convert_action_modify_tcp_ack
8535 (mhdr_res, actions, error))
8537 action_flags |= actions->type ==
8538 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
8539 MLX5_FLOW_ACTION_INC_TCP_ACK :
8540 MLX5_FLOW_ACTION_DEC_TCP_ACK;
8542 case MLX5_RTE_FLOW_ACTION_TYPE_TAG:
8543 if (flow_dv_convert_action_set_reg
8544 (mhdr_res, actions, error))
8546 action_flags |= MLX5_FLOW_ACTION_SET_TAG;
8548 case MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG:
8549 if (flow_dv_convert_action_copy_mreg
8550 (dev, mhdr_res, actions, error))
8552 action_flags |= MLX5_FLOW_ACTION_SET_TAG;
8554 case MLX5_RTE_FLOW_ACTION_TYPE_DEFAULT_MISS:
8555 action_flags |= MLX5_FLOW_ACTION_DEFAULT_MISS;
8556 dev_flow->handle->fate_action =
8557 MLX5_FLOW_FATE_DEFAULT_MISS;
8559 case RTE_FLOW_ACTION_TYPE_METER:
8560 mtr = actions->conf;
8562 fm = mlx5_flow_meter_attach(priv, mtr->mtr_id,
8565 return rte_flow_error_set(error,
8567 RTE_FLOW_ERROR_TYPE_ACTION,
8570 "or invalid parameters");
8571 flow->meter = fm->idx;
8573 /* Set the meter action. */
8575 fm = mlx5_ipool_get(priv->sh->ipool
8576 [MLX5_IPOOL_MTR], flow->meter);
8578 return rte_flow_error_set(error,
8580 RTE_FLOW_ERROR_TYPE_ACTION,
8583 "or invalid parameters");
8585 dev_flow->dv.actions[actions_n++] =
8586 fm->mfts->meter_action;
8587 action_flags |= MLX5_FLOW_ACTION_METER;
8589 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
8590 if (flow_dv_convert_action_modify_ipv4_dscp(mhdr_res,
8593 action_flags |= MLX5_FLOW_ACTION_SET_IPV4_DSCP;
8595 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
8596 if (flow_dv_convert_action_modify_ipv6_dscp(mhdr_res,
8599 action_flags |= MLX5_FLOW_ACTION_SET_IPV6_DSCP;
8601 case RTE_FLOW_ACTION_TYPE_END:
8603 if (mhdr_res->actions_num) {
8604 /* create modify action if needed. */
8605 if (flow_dv_modify_hdr_resource_register
8606 (dev, mhdr_res, dev_flow, error))
8608 dev_flow->dv.actions[modify_action_position] =
8609 handle->dvh.modify_hdr->action;
8611 if (action_flags & MLX5_FLOW_ACTION_COUNT) {
8613 flow_dv_translate_create_counter(dev,
8614 dev_flow, count, age);
8617 return rte_flow_error_set
8619 RTE_FLOW_ERROR_TYPE_ACTION,
8621 "cannot create counter"
8623 dev_flow->dv.actions[actions_n++] =
8624 (flow_dv_counter_get_by_idx(dev,
8625 flow->counter, NULL))->action;
8631 if (mhdr_res->actions_num &&
8632 modify_action_position == UINT32_MAX)
8633 modify_action_position = actions_n++;
8635 dev_flow->dv.actions_n = actions_n;
8636 dev_flow->act_flags = action_flags;
8637 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
8638 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
8639 int item_type = items->type;
8641 if (!mlx5_flow_os_item_supported(item_type))
8642 return rte_flow_error_set(error, ENOTSUP,
8643 RTE_FLOW_ERROR_TYPE_ITEM,
8644 NULL, "item not supported");
8645 switch (item_type) {
8646 case RTE_FLOW_ITEM_TYPE_PORT_ID:
8647 flow_dv_translate_item_port_id(dev, match_mask,
8648 match_value, items);
8649 last_item = MLX5_FLOW_ITEM_PORT_ID;
8651 case RTE_FLOW_ITEM_TYPE_ETH:
8652 flow_dv_translate_item_eth(match_mask, match_value,
8654 dev_flow->dv.group);
8655 matcher.priority = action_flags &
8656 MLX5_FLOW_ACTION_DEFAULT_MISS &&
8657 !dev_flow->external ?
8658 MLX5_PRIORITY_MAP_L3 :
8659 MLX5_PRIORITY_MAP_L2;
8660 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
8661 MLX5_FLOW_LAYER_OUTER_L2;
8663 case RTE_FLOW_ITEM_TYPE_VLAN:
8664 flow_dv_translate_item_vlan(dev_flow,
8665 match_mask, match_value,
8667 dev_flow->dv.group);
8668 matcher.priority = MLX5_PRIORITY_MAP_L2;
8669 last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
8670 MLX5_FLOW_LAYER_INNER_VLAN) :
8671 (MLX5_FLOW_LAYER_OUTER_L2 |
8672 MLX5_FLOW_LAYER_OUTER_VLAN);
8674 case RTE_FLOW_ITEM_TYPE_IPV4:
8675 mlx5_flow_tunnel_ip_check(items, next_protocol,
8676 &item_flags, &tunnel);
8677 flow_dv_translate_item_ipv4(match_mask, match_value,
8678 items, item_flags, tunnel,
8679 dev_flow->dv.group);
8680 matcher.priority = MLX5_PRIORITY_MAP_L3;
8681 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
8682 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
8683 if (items->mask != NULL &&
8684 ((const struct rte_flow_item_ipv4 *)
8685 items->mask)->hdr.next_proto_id) {
8687 ((const struct rte_flow_item_ipv4 *)
8688 (items->spec))->hdr.next_proto_id;
8690 ((const struct rte_flow_item_ipv4 *)
8691 (items->mask))->hdr.next_proto_id;
8693 /* Reset for inner layer. */
8694 next_protocol = 0xff;
8697 case RTE_FLOW_ITEM_TYPE_IPV6:
8698 mlx5_flow_tunnel_ip_check(items, next_protocol,
8699 &item_flags, &tunnel);
8700 flow_dv_translate_item_ipv6(match_mask, match_value,
8701 items, item_flags, tunnel,
8702 dev_flow->dv.group);
8703 matcher.priority = MLX5_PRIORITY_MAP_L3;
8704 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
8705 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
8706 if (items->mask != NULL &&
8707 ((const struct rte_flow_item_ipv6 *)
8708 items->mask)->hdr.proto) {
8710 ((const struct rte_flow_item_ipv6 *)
8711 items->spec)->hdr.proto;
8713 ((const struct rte_flow_item_ipv6 *)
8714 items->mask)->hdr.proto;
8716 /* Reset for inner layer. */
8717 next_protocol = 0xff;
8720 case RTE_FLOW_ITEM_TYPE_TCP:
8721 flow_dv_translate_item_tcp(match_mask, match_value,
8723 matcher.priority = MLX5_PRIORITY_MAP_L4;
8724 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
8725 MLX5_FLOW_LAYER_OUTER_L4_TCP;
8727 case RTE_FLOW_ITEM_TYPE_UDP:
8728 flow_dv_translate_item_udp(match_mask, match_value,
8730 matcher.priority = MLX5_PRIORITY_MAP_L4;
8731 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
8732 MLX5_FLOW_LAYER_OUTER_L4_UDP;
8734 case RTE_FLOW_ITEM_TYPE_GRE:
8735 flow_dv_translate_item_gre(match_mask, match_value,
8737 matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
8738 last_item = MLX5_FLOW_LAYER_GRE;
8740 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
8741 flow_dv_translate_item_gre_key(match_mask,
8742 match_value, items);
8743 last_item = MLX5_FLOW_LAYER_GRE_KEY;
8745 case RTE_FLOW_ITEM_TYPE_NVGRE:
8746 flow_dv_translate_item_nvgre(match_mask, match_value,
8748 matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
8749 last_item = MLX5_FLOW_LAYER_GRE;
8751 case RTE_FLOW_ITEM_TYPE_VXLAN:
8752 flow_dv_translate_item_vxlan(match_mask, match_value,
8754 matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
8755 last_item = MLX5_FLOW_LAYER_VXLAN;
8757 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
8758 flow_dv_translate_item_vxlan_gpe(match_mask,
8761 matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
8762 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
8764 case RTE_FLOW_ITEM_TYPE_GENEVE:
8765 flow_dv_translate_item_geneve(match_mask, match_value,
8767 matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
8768 last_item = MLX5_FLOW_LAYER_GENEVE;
8770 case RTE_FLOW_ITEM_TYPE_MPLS:
8771 flow_dv_translate_item_mpls(match_mask, match_value,
8772 items, last_item, tunnel);
8773 matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
8774 last_item = MLX5_FLOW_LAYER_MPLS;
8776 case RTE_FLOW_ITEM_TYPE_MARK:
8777 flow_dv_translate_item_mark(dev, match_mask,
8778 match_value, items);
8779 last_item = MLX5_FLOW_ITEM_MARK;
8781 case RTE_FLOW_ITEM_TYPE_META:
8782 flow_dv_translate_item_meta(dev, match_mask,
8783 match_value, attr, items);
8784 last_item = MLX5_FLOW_ITEM_METADATA;
8786 case RTE_FLOW_ITEM_TYPE_ICMP:
8787 flow_dv_translate_item_icmp(match_mask, match_value,
8789 last_item = MLX5_FLOW_LAYER_ICMP;
8791 case RTE_FLOW_ITEM_TYPE_ICMP6:
8792 flow_dv_translate_item_icmp6(match_mask, match_value,
8794 last_item = MLX5_FLOW_LAYER_ICMP6;
8796 case RTE_FLOW_ITEM_TYPE_TAG:
8797 flow_dv_translate_item_tag(dev, match_mask,
8798 match_value, items);
8799 last_item = MLX5_FLOW_ITEM_TAG;
8801 case MLX5_RTE_FLOW_ITEM_TYPE_TAG:
8802 flow_dv_translate_mlx5_item_tag(dev, match_mask,
8803 match_value, items);
8804 last_item = MLX5_FLOW_ITEM_TAG;
8806 case MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE:
8807 flow_dv_translate_item_tx_queue(dev, match_mask,
8810 last_item = MLX5_FLOW_ITEM_TX_QUEUE;
8812 case RTE_FLOW_ITEM_TYPE_GTP:
8813 flow_dv_translate_item_gtp(match_mask, match_value,
8815 matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
8816 last_item = MLX5_FLOW_LAYER_GTP;
8818 case RTE_FLOW_ITEM_TYPE_ECPRI:
8819 if (!mlx5_flex_parser_ecpri_exist(dev)) {
8820 /* Create it only the first time to be used. */
8821 ret = mlx5_flex_parser_ecpri_alloc(dev);
8823 return rte_flow_error_set
8825 RTE_FLOW_ERROR_TYPE_ITEM,
8827 "cannot create eCPRI parser");
8829 /* Adjust the length matcher and device flow value. */
8830 matcher.mask.size = MLX5_ST_SZ_BYTES(fte_match_param);
8831 dev_flow->dv.value.size =
8832 MLX5_ST_SZ_BYTES(fte_match_param);
8833 flow_dv_translate_item_ecpri(dev, match_mask,
8834 match_value, items);
8835 /* No other protocol should follow eCPRI layer. */
8836 last_item = MLX5_FLOW_LAYER_ECPRI;
8841 item_flags |= last_item;
8844 * When E-Switch mode is enabled, we have two cases where we need to
8845 * set the source port manually.
8846 * The first one, is in case of Nic steering rule, and the second is
8847 * E-Switch rule where no port_id item was found. In both cases
8848 * the source port is set according the current port in use.
8850 if (!(item_flags & MLX5_FLOW_ITEM_PORT_ID) &&
8851 (priv->representor || priv->master)) {
8852 if (flow_dv_translate_item_port_id(dev, match_mask,
8856 #ifdef RTE_LIBRTE_MLX5_DEBUG
8857 MLX5_ASSERT(!flow_dv_check_valid_spec(matcher.mask.buf,
8858 dev_flow->dv.value.buf));
8861 * Layers may be already initialized from prefix flow if this dev_flow
8862 * is the suffix flow.
8864 handle->layers |= item_flags;
8865 if (action_flags & MLX5_FLOW_ACTION_RSS)
8866 flow_dv_hashfields_set(dev_flow, rss_desc);
8867 /* Register matcher. */
8868 matcher.crc = rte_raw_cksum((const void *)matcher.mask.buf,
8870 matcher.priority = mlx5_flow_adjust_priority(dev, priority,
8872 /* reserved field no needs to be set to 0 here. */
8873 tbl_key.domain = attr->transfer;
8874 tbl_key.direction = attr->egress;
8875 tbl_key.table_id = dev_flow->dv.group;
8876 if (flow_dv_matcher_register(dev, &matcher, &tbl_key, dev_flow, error))
8882 * Apply the flow to the NIC, lock free,
8883 * (mutex should be acquired by caller).
8886 * Pointer to the Ethernet device structure.
8887 * @param[in, out] flow
8888 * Pointer to flow structure.
8890 * Pointer to error structure.
8893 * 0 on success, a negative errno value otherwise and rte_errno is set.
8896 __flow_dv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
8897 struct rte_flow_error *error)
8899 struct mlx5_flow_dv_workspace *dv;
8900 struct mlx5_flow_handle *dh;
8901 struct mlx5_flow_handle_dv *dv_h;
8902 struct mlx5_flow *dev_flow;
8903 struct mlx5_priv *priv = dev->data->dev_private;
8904 uint32_t handle_idx;
8909 for (idx = priv->flow_idx - 1; idx >= priv->flow_nested_idx; idx--) {
8910 dev_flow = &((struct mlx5_flow *)priv->inter_flows)[idx];
8912 dh = dev_flow->handle;
8915 if (dh->fate_action == MLX5_FLOW_FATE_DROP) {
8917 dv->actions[n++] = priv->sh->esw_drop_action;
8919 struct mlx5_hrxq *drop_hrxq;
8920 drop_hrxq = mlx5_hrxq_drop_new(dev);
8924 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8926 "cannot get drop hash queue");
8930 * Drop queues will be released by the specify
8931 * mlx5_hrxq_drop_release() function. Assign
8932 * the special index to hrxq to mark the queue
8933 * has been allocated.
8935 dh->rix_hrxq = UINT32_MAX;
8936 dv->actions[n++] = drop_hrxq->action;
8938 } else if (dh->fate_action == MLX5_FLOW_FATE_QUEUE) {
8939 struct mlx5_hrxq *hrxq;
8941 struct mlx5_flow_rss_desc *rss_desc =
8942 &((struct mlx5_flow_rss_desc *)priv->rss_desc)
8943 [!!priv->flow_nested_idx];
8945 MLX5_ASSERT(rss_desc->queue_num);
8946 hrxq_idx = mlx5_hrxq_get(dev, rss_desc->key,
8947 MLX5_RSS_HASH_KEY_LEN,
8948 dev_flow->hash_fields,
8950 rss_desc->queue_num);
8952 hrxq_idx = mlx5_hrxq_new
8953 (dev, rss_desc->key,
8954 MLX5_RSS_HASH_KEY_LEN,
8955 dev_flow->hash_fields,
8957 rss_desc->queue_num,
8959 MLX5_FLOW_LAYER_TUNNEL));
8961 hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
8966 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
8967 "cannot get hash queue");
8970 dh->rix_hrxq = hrxq_idx;
8971 dv->actions[n++] = hrxq->action;
8972 } else if (dh->fate_action == MLX5_FLOW_FATE_DEFAULT_MISS) {
8973 if (flow_dv_default_miss_resource_register
8977 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
8978 "cannot create default miss resource");
8979 goto error_default_miss;
8981 dh->rix_default_fate = MLX5_FLOW_FATE_DEFAULT_MISS;
8982 dv->actions[n++] = priv->sh->default_miss.action;
8984 err = mlx5_flow_os_create_flow(dv_h->matcher->matcher_object,
8985 (void *)&dv->value, n,
8986 dv->actions, &dh->drv_flow);
8988 rte_flow_error_set(error, errno,
8989 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8991 "hardware refuses to create flow");
8994 if (priv->vmwa_context &&
8995 dh->vf_vlan.tag && !dh->vf_vlan.created) {
8997 * The rule contains the VLAN pattern.
8998 * For VF we are going to create VLAN
8999 * interface to make hypervisor set correct
9000 * e-Switch vport context.
9002 mlx5_vlan_vmwa_acquire(dev, &dh->vf_vlan);
9007 if (dh->fate_action == MLX5_FLOW_FATE_DEFAULT_MISS)
9008 flow_dv_default_miss_resource_release(dev);
9010 err = rte_errno; /* Save rte_errno before cleanup. */
9011 SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
9012 handle_idx, dh, next) {
9013 /* hrxq is union, don't clear it if the flag is not set. */
9015 if (dh->fate_action == MLX5_FLOW_FATE_DROP) {
9016 mlx5_hrxq_drop_release(dev);
9018 } else if (dh->fate_action == MLX5_FLOW_FATE_QUEUE) {
9019 mlx5_hrxq_release(dev, dh->rix_hrxq);
9023 if (dh->vf_vlan.tag && dh->vf_vlan.created)
9024 mlx5_vlan_vmwa_release(dev, &dh->vf_vlan);
9026 rte_errno = err; /* Restore rte_errno. */
9031 * Release the flow matcher.
9034 * Pointer to Ethernet device.
9036 * Pointer to mlx5_flow_handle.
9039 * 1 while a reference on it exists, 0 when freed.
9042 flow_dv_matcher_release(struct rte_eth_dev *dev,
9043 struct mlx5_flow_handle *handle)
9045 struct mlx5_flow_dv_matcher *matcher = handle->dvh.matcher;
9047 MLX5_ASSERT(matcher->matcher_object);
9048 DRV_LOG(DEBUG, "port %u matcher %p: refcnt %d--",
9049 dev->data->port_id, (void *)matcher,
9050 rte_atomic32_read(&matcher->refcnt));
9051 if (rte_atomic32_dec_and_test(&matcher->refcnt)) {
9052 claim_zero(mlx5_flow_os_destroy_flow_matcher
9053 (matcher->matcher_object));
9054 LIST_REMOVE(matcher, next);
9055 /* table ref-- in release interface. */
9056 flow_dv_tbl_resource_release(dev, matcher->tbl);
9058 DRV_LOG(DEBUG, "port %u matcher %p: removed",
9059 dev->data->port_id, (void *)matcher);
9066 * Release an encap/decap resource.
9069 * Pointer to Ethernet device.
9071 * Pointer to mlx5_flow_handle.
9074 * 1 while a reference on it exists, 0 when freed.
9077 flow_dv_encap_decap_resource_release(struct rte_eth_dev *dev,
9078 struct mlx5_flow_handle *handle)
9080 struct mlx5_priv *priv = dev->data->dev_private;
9081 uint32_t idx = handle->dvh.rix_encap_decap;
9082 struct mlx5_flow_dv_encap_decap_resource *cache_resource;
9084 cache_resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_DECAP_ENCAP],
9086 if (!cache_resource)
9088 MLX5_ASSERT(cache_resource->action);
9089 DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d--",
9090 (void *)cache_resource,
9091 rte_atomic32_read(&cache_resource->refcnt));
9092 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
9093 claim_zero(mlx5_flow_os_destroy_flow_action
9094 (cache_resource->action));
9095 ILIST_REMOVE(priv->sh->ipool[MLX5_IPOOL_DECAP_ENCAP],
9096 &priv->sh->encaps_decaps, idx,
9097 cache_resource, next);
9098 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_DECAP_ENCAP], idx);
9099 DRV_LOG(DEBUG, "encap/decap resource %p: removed",
9100 (void *)cache_resource);
9107 * Release an jump to table action resource.
9110 * Pointer to Ethernet device.
9112 * Pointer to mlx5_flow_handle.
9115 * 1 while a reference on it exists, 0 when freed.
9118 flow_dv_jump_tbl_resource_release(struct rte_eth_dev *dev,
9119 struct mlx5_flow_handle *handle)
9121 struct mlx5_priv *priv = dev->data->dev_private;
9122 struct mlx5_flow_dv_jump_tbl_resource *cache_resource;
9123 struct mlx5_flow_tbl_data_entry *tbl_data;
9125 tbl_data = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_JUMP],
9129 cache_resource = &tbl_data->jump;
9130 MLX5_ASSERT(cache_resource->action);
9131 DRV_LOG(DEBUG, "jump table resource %p: refcnt %d--",
9132 (void *)cache_resource,
9133 rte_atomic32_read(&cache_resource->refcnt));
9134 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
9135 claim_zero(mlx5_flow_os_destroy_flow_action
9136 (cache_resource->action));
9137 /* jump action memory free is inside the table release. */
9138 flow_dv_tbl_resource_release(dev, &tbl_data->tbl);
9139 DRV_LOG(DEBUG, "jump table resource %p: removed",
9140 (void *)cache_resource);
9147 * Release a default miss resource.
9150 * Pointer to Ethernet device.
9152 * 1 while a reference on it exists, 0 when freed.
9155 flow_dv_default_miss_resource_release(struct rte_eth_dev *dev)
9157 struct mlx5_priv *priv = dev->data->dev_private;
9158 struct mlx5_dev_ctx_shared *sh = priv->sh;
9159 struct mlx5_flow_default_miss_resource *cache_resource =
9162 MLX5_ASSERT(cache_resource->action);
9163 DRV_LOG(DEBUG, "default miss resource %p: refcnt %d--",
9164 (void *)cache_resource->action,
9165 rte_atomic32_read(&cache_resource->refcnt));
9166 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
9167 claim_zero(mlx5_glue->destroy_flow_action
9168 (cache_resource->action));
9169 DRV_LOG(DEBUG, "default miss resource %p: removed",
9170 (void *)cache_resource->action);
9177 * Release a modify-header resource.
9180 * Pointer to Ethernet device.
9182 * Pointer to mlx5_flow_handle.
9185 * 1 while a reference on it exists, 0 when freed.
9188 flow_dv_modify_hdr_resource_release(struct rte_eth_dev *dev,
9189 struct mlx5_flow_handle *handle)
9191 struct mlx5_priv *priv = dev->data->dev_private;
9192 struct mlx5_flow_dv_modify_hdr_resource *cache_resource =
9193 handle->dvh.modify_hdr;
9195 MLX5_ASSERT(cache_resource->action);
9196 DRV_LOG(DEBUG, "modify-header resource %p: refcnt %d--",
9197 (void *)cache_resource,
9198 rte_atomic32_read(&cache_resource->refcnt));
9199 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
9200 claim_zero(mlx5_flow_os_destroy_flow_action
9201 (cache_resource->action));
9202 mlx5_hlist_remove(priv->sh->modify_cmds,
9203 &cache_resource->entry);
9204 mlx5_free(cache_resource);
9205 DRV_LOG(DEBUG, "modify-header resource %p: removed",
9206 (void *)cache_resource);
9213 * Release port ID action resource.
9216 * Pointer to Ethernet device.
9218 * Pointer to mlx5_flow_handle.
9221 * 1 while a reference on it exists, 0 when freed.
9224 flow_dv_port_id_action_resource_release(struct rte_eth_dev *dev,
9225 struct mlx5_flow_handle *handle)
9227 struct mlx5_priv *priv = dev->data->dev_private;
9228 struct mlx5_flow_dv_port_id_action_resource *cache_resource;
9229 uint32_t idx = handle->rix_port_id_action;
9231 cache_resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_PORT_ID],
9233 if (!cache_resource)
9235 MLX5_ASSERT(cache_resource->action);
9236 DRV_LOG(DEBUG, "port ID action resource %p: refcnt %d--",
9237 (void *)cache_resource,
9238 rte_atomic32_read(&cache_resource->refcnt));
9239 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
9240 claim_zero(mlx5_flow_os_destroy_flow_action
9241 (cache_resource->action));
9242 ILIST_REMOVE(priv->sh->ipool[MLX5_IPOOL_PORT_ID],
9243 &priv->sh->port_id_action_list, idx,
9244 cache_resource, next);
9245 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_PORT_ID], idx);
9246 DRV_LOG(DEBUG, "port id action resource %p: removed",
9247 (void *)cache_resource);
9254 * Release push vlan action resource.
9257 * Pointer to Ethernet device.
9259 * Pointer to mlx5_flow_handle.
9262 * 1 while a reference on it exists, 0 when freed.
9265 flow_dv_push_vlan_action_resource_release(struct rte_eth_dev *dev,
9266 struct mlx5_flow_handle *handle)
9268 struct mlx5_priv *priv = dev->data->dev_private;
9269 uint32_t idx = handle->dvh.rix_push_vlan;
9270 struct mlx5_flow_dv_push_vlan_action_resource *cache_resource;
9272 cache_resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_PUSH_VLAN],
9274 if (!cache_resource)
9276 MLX5_ASSERT(cache_resource->action);
9277 DRV_LOG(DEBUG, "push VLAN action resource %p: refcnt %d--",
9278 (void *)cache_resource,
9279 rte_atomic32_read(&cache_resource->refcnt));
9280 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
9281 claim_zero(mlx5_flow_os_destroy_flow_action
9282 (cache_resource->action));
9283 ILIST_REMOVE(priv->sh->ipool[MLX5_IPOOL_PUSH_VLAN],
9284 &priv->sh->push_vlan_action_list, idx,
9285 cache_resource, next);
9286 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_PUSH_VLAN], idx);
9287 DRV_LOG(DEBUG, "push vlan action resource %p: removed",
9288 (void *)cache_resource);
9295 * Release the fate resource.
9298 * Pointer to Ethernet device.
9300 * Pointer to mlx5_flow_handle.
9303 flow_dv_fate_resource_release(struct rte_eth_dev *dev,
9304 struct mlx5_flow_handle *handle)
9306 if (!handle->rix_fate)
9308 switch (handle->fate_action) {
9309 case MLX5_FLOW_FATE_DROP:
9310 mlx5_hrxq_drop_release(dev);
9312 case MLX5_FLOW_FATE_QUEUE:
9313 mlx5_hrxq_release(dev, handle->rix_hrxq);
9315 case MLX5_FLOW_FATE_JUMP:
9316 flow_dv_jump_tbl_resource_release(dev, handle);
9318 case MLX5_FLOW_FATE_PORT_ID:
9319 flow_dv_port_id_action_resource_release(dev, handle);
9321 case MLX5_FLOW_FATE_DEFAULT_MISS:
9322 flow_dv_default_miss_resource_release(dev);
9325 DRV_LOG(DEBUG, "Incorrect fate action:%d", handle->fate_action);
9328 handle->rix_fate = 0;
9332 * Remove the flow from the NIC but keeps it in memory.
9333 * Lock free, (mutex should be acquired by caller).
9336 * Pointer to Ethernet device.
9337 * @param[in, out] flow
9338 * Pointer to flow structure.
9341 __flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
9343 struct mlx5_flow_handle *dh;
9344 uint32_t handle_idx;
9345 struct mlx5_priv *priv = dev->data->dev_private;
9349 handle_idx = flow->dev_handles;
9350 while (handle_idx) {
9351 dh = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
9356 claim_zero(mlx5_flow_os_destroy_flow(dh->drv_flow));
9357 dh->drv_flow = NULL;
9359 if (dh->fate_action == MLX5_FLOW_FATE_DROP ||
9360 dh->fate_action == MLX5_FLOW_FATE_QUEUE ||
9361 dh->fate_action == MLX5_FLOW_FATE_DEFAULT_MISS)
9362 flow_dv_fate_resource_release(dev, dh);
9363 if (dh->vf_vlan.tag && dh->vf_vlan.created)
9364 mlx5_vlan_vmwa_release(dev, &dh->vf_vlan);
9365 handle_idx = dh->next.next;
9370 * Remove the flow from the NIC and the memory.
9371 * Lock free, (mutex should be acquired by caller).
9374 * Pointer to the Ethernet device structure.
9375 * @param[in, out] flow
9376 * Pointer to flow structure.
9379 __flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
9381 struct mlx5_flow_handle *dev_handle;
9382 struct mlx5_priv *priv = dev->data->dev_private;
9386 __flow_dv_remove(dev, flow);
9387 if (flow->counter) {
9388 flow_dv_counter_release(dev, flow->counter);
9392 struct mlx5_flow_meter *fm;
9394 fm = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MTR],
9397 mlx5_flow_meter_detach(fm);
9400 while (flow->dev_handles) {
9401 uint32_t tmp_idx = flow->dev_handles;
9403 dev_handle = mlx5_ipool_get(priv->sh->ipool
9404 [MLX5_IPOOL_MLX5_FLOW], tmp_idx);
9407 flow->dev_handles = dev_handle->next.next;
9408 if (dev_handle->dvh.matcher)
9409 flow_dv_matcher_release(dev, dev_handle);
9410 if (dev_handle->dvh.rix_encap_decap)
9411 flow_dv_encap_decap_resource_release(dev, dev_handle);
9412 if (dev_handle->dvh.modify_hdr)
9413 flow_dv_modify_hdr_resource_release(dev, dev_handle);
9414 if (dev_handle->dvh.rix_push_vlan)
9415 flow_dv_push_vlan_action_resource_release(dev,
9417 if (dev_handle->dvh.rix_tag)
9418 flow_dv_tag_release(dev,
9419 dev_handle->dvh.rix_tag);
9420 flow_dv_fate_resource_release(dev, dev_handle);
9421 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
9427 * Query a dv flow rule for its statistics via devx.
9430 * Pointer to Ethernet device.
9432 * Pointer to the sub flow.
9434 * data retrieved by the query.
9436 * Perform verbose error reporting if not NULL.
9439 * 0 on success, a negative errno value otherwise and rte_errno is set.
9442 flow_dv_query_count(struct rte_eth_dev *dev, struct rte_flow *flow,
9443 void *data, struct rte_flow_error *error)
9445 struct mlx5_priv *priv = dev->data->dev_private;
9446 struct rte_flow_query_count *qc = data;
9448 if (!priv->config.devx)
9449 return rte_flow_error_set(error, ENOTSUP,
9450 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9452 "counters are not supported");
9453 if (flow->counter) {
9454 uint64_t pkts, bytes;
9455 struct mlx5_flow_counter *cnt;
9457 cnt = flow_dv_counter_get_by_idx(dev, flow->counter,
9459 int err = _flow_dv_query_count(dev, flow->counter, &pkts,
9463 return rte_flow_error_set(error, -err,
9464 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9465 NULL, "cannot read counters");
9468 qc->hits = pkts - cnt->hits;
9469 qc->bytes = bytes - cnt->bytes;
9476 return rte_flow_error_set(error, EINVAL,
9477 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9479 "counters are not available");
9485 * @see rte_flow_query()
9489 flow_dv_query(struct rte_eth_dev *dev,
9490 struct rte_flow *flow __rte_unused,
9491 const struct rte_flow_action *actions __rte_unused,
9492 void *data __rte_unused,
9493 struct rte_flow_error *error __rte_unused)
9497 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
9498 switch (actions->type) {
9499 case RTE_FLOW_ACTION_TYPE_VOID:
9501 case RTE_FLOW_ACTION_TYPE_COUNT:
9502 ret = flow_dv_query_count(dev, flow, data, error);
9505 return rte_flow_error_set(error, ENOTSUP,
9506 RTE_FLOW_ERROR_TYPE_ACTION,
9508 "action not supported");
9515 * Destroy the meter table set.
9516 * Lock free, (mutex should be acquired by caller).
9519 * Pointer to Ethernet device.
9521 * Pointer to the meter table set.
9527 flow_dv_destroy_mtr_tbl(struct rte_eth_dev *dev,
9528 struct mlx5_meter_domains_infos *tbl)
9530 struct mlx5_priv *priv = dev->data->dev_private;
9531 struct mlx5_meter_domains_infos *mtd =
9532 (struct mlx5_meter_domains_infos *)tbl;
9534 if (!mtd || !priv->config.dv_flow_en)
9536 if (mtd->ingress.policer_rules[RTE_MTR_DROPPED])
9537 claim_zero(mlx5_flow_os_destroy_flow
9538 (mtd->ingress.policer_rules[RTE_MTR_DROPPED]));
9539 if (mtd->egress.policer_rules[RTE_MTR_DROPPED])
9540 claim_zero(mlx5_flow_os_destroy_flow
9541 (mtd->egress.policer_rules[RTE_MTR_DROPPED]));
9542 if (mtd->transfer.policer_rules[RTE_MTR_DROPPED])
9543 claim_zero(mlx5_flow_os_destroy_flow
9544 (mtd->transfer.policer_rules[RTE_MTR_DROPPED]));
9545 if (mtd->egress.color_matcher)
9546 claim_zero(mlx5_flow_os_destroy_flow_matcher
9547 (mtd->egress.color_matcher));
9548 if (mtd->egress.any_matcher)
9549 claim_zero(mlx5_flow_os_destroy_flow_matcher
9550 (mtd->egress.any_matcher));
9551 if (mtd->egress.tbl)
9552 flow_dv_tbl_resource_release(dev, mtd->egress.tbl);
9553 if (mtd->egress.sfx_tbl)
9554 flow_dv_tbl_resource_release(dev, mtd->egress.sfx_tbl);
9555 if (mtd->ingress.color_matcher)
9556 claim_zero(mlx5_flow_os_destroy_flow_matcher
9557 (mtd->ingress.color_matcher));
9558 if (mtd->ingress.any_matcher)
9559 claim_zero(mlx5_flow_os_destroy_flow_matcher
9560 (mtd->ingress.any_matcher));
9561 if (mtd->ingress.tbl)
9562 flow_dv_tbl_resource_release(dev, mtd->ingress.tbl);
9563 if (mtd->ingress.sfx_tbl)
9564 flow_dv_tbl_resource_release(dev, mtd->ingress.sfx_tbl);
9565 if (mtd->transfer.color_matcher)
9566 claim_zero(mlx5_flow_os_destroy_flow_matcher
9567 (mtd->transfer.color_matcher));
9568 if (mtd->transfer.any_matcher)
9569 claim_zero(mlx5_flow_os_destroy_flow_matcher
9570 (mtd->transfer.any_matcher));
9571 if (mtd->transfer.tbl)
9572 flow_dv_tbl_resource_release(dev, mtd->transfer.tbl);
9573 if (mtd->transfer.sfx_tbl)
9574 flow_dv_tbl_resource_release(dev, mtd->transfer.sfx_tbl);
9576 claim_zero(mlx5_flow_os_destroy_flow_action(mtd->drop_actn));
9581 /* Number of meter flow actions, count and jump or count and drop. */
9582 #define METER_ACTIONS 2
9585 * Create specify domain meter table and suffix table.
9588 * Pointer to Ethernet device.
9589 * @param[in,out] mtb
9590 * Pointer to DV meter table set.
9593 * @param[in] transfer
9595 * @param[in] color_reg_c_idx
9596 * Reg C index for color match.
9599 * 0 on success, -1 otherwise and rte_errno is set.
9602 flow_dv_prepare_mtr_tables(struct rte_eth_dev *dev,
9603 struct mlx5_meter_domains_infos *mtb,
9604 uint8_t egress, uint8_t transfer,
9605 uint32_t color_reg_c_idx)
9607 struct mlx5_priv *priv = dev->data->dev_private;
9608 struct mlx5_dev_ctx_shared *sh = priv->sh;
9609 struct mlx5_flow_dv_match_params mask = {
9610 .size = sizeof(mask.buf),
9612 struct mlx5_flow_dv_match_params value = {
9613 .size = sizeof(value.buf),
9615 struct mlx5dv_flow_matcher_attr dv_attr = {
9616 .type = IBV_FLOW_ATTR_NORMAL,
9618 .match_criteria_enable = 0,
9619 .match_mask = (void *)&mask,
9621 void *actions[METER_ACTIONS];
9622 struct mlx5_meter_domain_info *dtb;
9623 struct rte_flow_error error;
9628 dtb = &mtb->transfer;
9632 dtb = &mtb->ingress;
9633 /* Create the meter table with METER level. */
9634 dtb->tbl = flow_dv_tbl_resource_get(dev, MLX5_FLOW_TABLE_LEVEL_METER,
9635 egress, transfer, &error);
9637 DRV_LOG(ERR, "Failed to create meter policer table.");
9640 /* Create the meter suffix table with SUFFIX level. */
9641 dtb->sfx_tbl = flow_dv_tbl_resource_get(dev,
9642 MLX5_FLOW_TABLE_LEVEL_SUFFIX,
9643 egress, transfer, &error);
9644 if (!dtb->sfx_tbl) {
9645 DRV_LOG(ERR, "Failed to create meter suffix table.");
9648 /* Create matchers, Any and Color. */
9649 dv_attr.priority = 3;
9650 dv_attr.match_criteria_enable = 0;
9651 ret = mlx5_flow_os_create_flow_matcher(sh->ctx, &dv_attr, dtb->tbl->obj,
9654 DRV_LOG(ERR, "Failed to create meter"
9655 " policer default matcher.");
9658 dv_attr.priority = 0;
9659 dv_attr.match_criteria_enable =
9660 1 << MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
9661 flow_dv_match_meta_reg(mask.buf, value.buf, color_reg_c_idx,
9662 rte_col_2_mlx5_col(RTE_COLORS), UINT8_MAX);
9663 ret = mlx5_flow_os_create_flow_matcher(sh->ctx, &dv_attr, dtb->tbl->obj,
9664 &dtb->color_matcher);
9666 DRV_LOG(ERR, "Failed to create meter policer color matcher.");
9669 if (mtb->count_actns[RTE_MTR_DROPPED])
9670 actions[i++] = mtb->count_actns[RTE_MTR_DROPPED];
9671 actions[i++] = mtb->drop_actn;
9672 /* Default rule: lowest priority, match any, actions: drop. */
9673 ret = mlx5_flow_os_create_flow(dtb->any_matcher, (void *)&value, i,
9675 &dtb->policer_rules[RTE_MTR_DROPPED]);
9677 DRV_LOG(ERR, "Failed to create meter policer drop rule.");
9686 * Create the needed meter and suffix tables.
9687 * Lock free, (mutex should be acquired by caller).
9690 * Pointer to Ethernet device.
9692 * Pointer to the flow meter.
9695 * Pointer to table set on success, NULL otherwise and rte_errno is set.
9697 static struct mlx5_meter_domains_infos *
9698 flow_dv_create_mtr_tbl(struct rte_eth_dev *dev,
9699 const struct mlx5_flow_meter *fm)
9701 struct mlx5_priv *priv = dev->data->dev_private;
9702 struct mlx5_meter_domains_infos *mtb;
9706 if (!priv->mtr_en) {
9707 rte_errno = ENOTSUP;
9710 mtb = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*mtb), 0, SOCKET_ID_ANY);
9712 DRV_LOG(ERR, "Failed to allocate memory for meter.");
9715 /* Create meter count actions */
9716 for (i = 0; i <= RTE_MTR_DROPPED; i++) {
9717 struct mlx5_flow_counter *cnt;
9718 if (!fm->policer_stats.cnt[i])
9720 cnt = flow_dv_counter_get_by_idx(dev,
9721 fm->policer_stats.cnt[i], NULL);
9722 mtb->count_actns[i] = cnt->action;
9724 /* Create drop action. */
9725 ret = mlx5_flow_os_create_flow_action_drop(&mtb->drop_actn);
9727 DRV_LOG(ERR, "Failed to create drop action.");
9730 /* Egress meter table. */
9731 ret = flow_dv_prepare_mtr_tables(dev, mtb, 1, 0, priv->mtr_color_reg);
9733 DRV_LOG(ERR, "Failed to prepare egress meter table.");
9736 /* Ingress meter table. */
9737 ret = flow_dv_prepare_mtr_tables(dev, mtb, 0, 0, priv->mtr_color_reg);
9739 DRV_LOG(ERR, "Failed to prepare ingress meter table.");
9742 /* FDB meter table. */
9743 if (priv->config.dv_esw_en) {
9744 ret = flow_dv_prepare_mtr_tables(dev, mtb, 0, 1,
9745 priv->mtr_color_reg);
9747 DRV_LOG(ERR, "Failed to prepare fdb meter table.");
9753 flow_dv_destroy_mtr_tbl(dev, mtb);
9758 * Destroy domain policer rule.
9761 * Pointer to domain table.
9764 flow_dv_destroy_domain_policer_rule(struct mlx5_meter_domain_info *dt)
9768 for (i = 0; i < RTE_MTR_DROPPED; i++) {
9769 if (dt->policer_rules[i]) {
9770 claim_zero(mlx5_flow_os_destroy_flow
9771 (dt->policer_rules[i]));
9772 dt->policer_rules[i] = NULL;
9775 if (dt->jump_actn) {
9776 claim_zero(mlx5_flow_os_destroy_flow_action(dt->jump_actn));
9777 dt->jump_actn = NULL;
9782 * Destroy policer rules.
9785 * Pointer to Ethernet device.
9787 * Pointer to flow meter structure.
9789 * Pointer to flow attributes.
9795 flow_dv_destroy_policer_rules(struct rte_eth_dev *dev __rte_unused,
9796 const struct mlx5_flow_meter *fm,
9797 const struct rte_flow_attr *attr)
9799 struct mlx5_meter_domains_infos *mtb = fm ? fm->mfts : NULL;
9804 flow_dv_destroy_domain_policer_rule(&mtb->egress);
9806 flow_dv_destroy_domain_policer_rule(&mtb->ingress);
9808 flow_dv_destroy_domain_policer_rule(&mtb->transfer);
9813 * Create specify domain meter policer rule.
9816 * Pointer to flow meter structure.
9818 * Pointer to DV meter table set.
9819 * @param[in] mtr_reg_c
9820 * Color match REG_C.
9823 * 0 on success, -1 otherwise.
9826 flow_dv_create_policer_forward_rule(struct mlx5_flow_meter *fm,
9827 struct mlx5_meter_domain_info *dtb,
9830 struct mlx5_flow_dv_match_params matcher = {
9831 .size = sizeof(matcher.buf),
9833 struct mlx5_flow_dv_match_params value = {
9834 .size = sizeof(value.buf),
9836 struct mlx5_meter_domains_infos *mtb = fm->mfts;
9837 void *actions[METER_ACTIONS];
9841 /* Create jump action. */
9842 if (!dtb->jump_actn)
9843 ret = mlx5_flow_os_create_flow_action_dest_flow_tbl
9844 (dtb->sfx_tbl->obj, &dtb->jump_actn);
9846 DRV_LOG(ERR, "Failed to create policer jump action.");
9849 for (i = 0; i < RTE_MTR_DROPPED; i++) {
9852 flow_dv_match_meta_reg(matcher.buf, value.buf, mtr_reg_c,
9853 rte_col_2_mlx5_col(i), UINT8_MAX);
9854 if (mtb->count_actns[i])
9855 actions[j++] = mtb->count_actns[i];
9856 if (fm->action[i] == MTR_POLICER_ACTION_DROP)
9857 actions[j++] = mtb->drop_actn;
9859 actions[j++] = dtb->jump_actn;
9860 ret = mlx5_flow_os_create_flow(dtb->color_matcher,
9861 (void *)&value, j, actions,
9862 &dtb->policer_rules[i]);
9864 DRV_LOG(ERR, "Failed to create policer rule.");
9875 * Create policer rules.
9878 * Pointer to Ethernet device.
9880 * Pointer to flow meter structure.
9882 * Pointer to flow attributes.
9885 * 0 on success, -1 otherwise.
9888 flow_dv_create_policer_rules(struct rte_eth_dev *dev,
9889 struct mlx5_flow_meter *fm,
9890 const struct rte_flow_attr *attr)
9892 struct mlx5_priv *priv = dev->data->dev_private;
9893 struct mlx5_meter_domains_infos *mtb = fm->mfts;
9897 ret = flow_dv_create_policer_forward_rule(fm, &mtb->egress,
9898 priv->mtr_color_reg);
9900 DRV_LOG(ERR, "Failed to create egress policer.");
9904 if (attr->ingress) {
9905 ret = flow_dv_create_policer_forward_rule(fm, &mtb->ingress,
9906 priv->mtr_color_reg);
9908 DRV_LOG(ERR, "Failed to create ingress policer.");
9912 if (attr->transfer) {
9913 ret = flow_dv_create_policer_forward_rule(fm, &mtb->transfer,
9914 priv->mtr_color_reg);
9916 DRV_LOG(ERR, "Failed to create transfer policer.");
9922 flow_dv_destroy_policer_rules(dev, fm, attr);
9927 * Query a devx counter.
9930 * Pointer to the Ethernet device structure.
9932 * Index to the flow counter.
9934 * Set to clear the counter statistics.
9936 * The statistics value of packets.
9938 * The statistics value of bytes.
9941 * 0 on success, otherwise return -1.
9944 flow_dv_counter_query(struct rte_eth_dev *dev, uint32_t counter, bool clear,
9945 uint64_t *pkts, uint64_t *bytes)
9947 struct mlx5_priv *priv = dev->data->dev_private;
9948 struct mlx5_flow_counter *cnt;
9949 uint64_t inn_pkts, inn_bytes;
9952 if (!priv->config.devx)
9955 ret = _flow_dv_query_count(dev, counter, &inn_pkts, &inn_bytes);
9958 cnt = flow_dv_counter_get_by_idx(dev, counter, NULL);
9959 *pkts = inn_pkts - cnt->hits;
9960 *bytes = inn_bytes - cnt->bytes;
9962 cnt->hits = inn_pkts;
9963 cnt->bytes = inn_bytes;
9969 * Get aged-out flows.
9972 * Pointer to the Ethernet device structure.
9973 * @param[in] context
9974 * The address of an array of pointers to the aged-out flows contexts.
9975 * @param[in] nb_contexts
9976 * The length of context array pointers.
9978 * Perform verbose error reporting if not NULL. Initialized in case of
9982 * how many contexts get in success, otherwise negative errno value.
9983 * if nb_contexts is 0, return the amount of all aged contexts.
9984 * if nb_contexts is not 0 , return the amount of aged flows reported
9985 * in the context array.
9986 * @note: only stub for now
9989 flow_get_aged_flows(struct rte_eth_dev *dev,
9991 uint32_t nb_contexts,
9992 struct rte_flow_error *error)
9994 struct mlx5_priv *priv = dev->data->dev_private;
9995 struct mlx5_age_info *age_info;
9996 struct mlx5_age_param *age_param;
9997 struct mlx5_flow_counter *counter;
10000 if (nb_contexts && !context)
10001 return rte_flow_error_set(error, EINVAL,
10002 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10004 "Should assign at least one flow or"
10005 " context to get if nb_contexts != 0");
10006 age_info = GET_PORT_AGE_INFO(priv);
10007 rte_spinlock_lock(&age_info->aged_sl);
10008 TAILQ_FOREACH(counter, &age_info->aged_counters, next) {
10011 age_param = MLX5_CNT_TO_AGE(counter);
10012 context[nb_flows - 1] = age_param->context;
10013 if (!(--nb_contexts))
10017 rte_spinlock_unlock(&age_info->aged_sl);
10018 MLX5_AGE_SET(age_info, MLX5_AGE_TRIGGER);
10023 * Mutex-protected thunk to lock-free __flow_dv_translate().
10026 flow_dv_translate(struct rte_eth_dev *dev,
10027 struct mlx5_flow *dev_flow,
10028 const struct rte_flow_attr *attr,
10029 const struct rte_flow_item items[],
10030 const struct rte_flow_action actions[],
10031 struct rte_flow_error *error)
10035 flow_dv_shared_lock(dev);
10036 ret = __flow_dv_translate(dev, dev_flow, attr, items, actions, error);
10037 flow_dv_shared_unlock(dev);
10042 * Mutex-protected thunk to lock-free __flow_dv_apply().
10045 flow_dv_apply(struct rte_eth_dev *dev,
10046 struct rte_flow *flow,
10047 struct rte_flow_error *error)
10051 flow_dv_shared_lock(dev);
10052 ret = __flow_dv_apply(dev, flow, error);
10053 flow_dv_shared_unlock(dev);
10058 * Mutex-protected thunk to lock-free __flow_dv_remove().
10061 flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
10063 flow_dv_shared_lock(dev);
10064 __flow_dv_remove(dev, flow);
10065 flow_dv_shared_unlock(dev);
10069 * Mutex-protected thunk to lock-free __flow_dv_destroy().
10072 flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
10074 flow_dv_shared_lock(dev);
10075 __flow_dv_destroy(dev, flow);
10076 flow_dv_shared_unlock(dev);
10080 * Mutex-protected thunk to lock-free flow_dv_counter_alloc().
10083 flow_dv_counter_allocate(struct rte_eth_dev *dev)
10087 flow_dv_shared_lock(dev);
10088 cnt = flow_dv_counter_alloc(dev, 0, 0, 1, 0);
10089 flow_dv_shared_unlock(dev);
10094 * Mutex-protected thunk to lock-free flow_dv_counter_release().
10097 flow_dv_counter_free(struct rte_eth_dev *dev, uint32_t cnt)
10099 flow_dv_shared_lock(dev);
10100 flow_dv_counter_release(dev, cnt);
10101 flow_dv_shared_unlock(dev);
10104 const struct mlx5_flow_driver_ops mlx5_flow_dv_drv_ops = {
10105 .validate = flow_dv_validate,
10106 .prepare = flow_dv_prepare,
10107 .translate = flow_dv_translate,
10108 .apply = flow_dv_apply,
10109 .remove = flow_dv_remove,
10110 .destroy = flow_dv_destroy,
10111 .query = flow_dv_query,
10112 .create_mtr_tbls = flow_dv_create_mtr_tbl,
10113 .destroy_mtr_tbls = flow_dv_destroy_mtr_tbl,
10114 .create_policer_rules = flow_dv_create_policer_rules,
10115 .destroy_policer_rules = flow_dv_destroy_policer_rules,
10116 .counter_alloc = flow_dv_counter_allocate,
10117 .counter_free = flow_dv_counter_free,
10118 .counter_query = flow_dv_counter_query,
10119 .get_aged_flows = flow_get_aged_flows,
10122 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */