1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018 Mellanox Technologies, Ltd
12 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
14 #pragma GCC diagnostic ignored "-Wpedantic"
16 #include <infiniband/verbs.h>
18 #pragma GCC diagnostic error "-Wpedantic"
21 #include <rte_common.h>
22 #include <rte_ether.h>
23 #include <rte_ethdev_driver.h>
25 #include <rte_flow_driver.h>
26 #include <rte_malloc.h>
29 #include <rte_vxlan.h>
32 #include <mlx5_glue.h>
33 #include <mlx5_devx_cmds.h>
36 #include "mlx5_defs.h"
38 #include "mlx5_flow.h"
39 #include "mlx5_rxtx.h"
41 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
43 #ifndef HAVE_IBV_FLOW_DEVX_COUNTERS
44 #define MLX5DV_FLOW_ACTION_COUNTERS_DEVX 0
47 #ifndef HAVE_MLX5DV_DR_ESWITCH
48 #ifndef MLX5DV_FLOW_TABLE_TYPE_FDB
49 #define MLX5DV_FLOW_TABLE_TYPE_FDB 0
53 #ifndef HAVE_MLX5DV_DR
54 #define MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL 1
57 /* VLAN header definitions */
58 #define MLX5DV_FLOW_VLAN_PCP_SHIFT 13
59 #define MLX5DV_FLOW_VLAN_PCP_MASK (0x7 << MLX5DV_FLOW_VLAN_PCP_SHIFT)
60 #define MLX5DV_FLOW_VLAN_VID_MASK 0x0fff
61 #define MLX5DV_FLOW_VLAN_PCP_MASK_BE RTE_BE16(MLX5DV_FLOW_VLAN_PCP_MASK)
62 #define MLX5DV_FLOW_VLAN_VID_MASK_BE RTE_BE16(MLX5DV_FLOW_VLAN_VID_MASK)
77 * Initialize flow attributes structure according to flow items' types.
79 * flow_dv_validate() avoids multiple L3/L4 layers cases other than tunnel
80 * mode. For tunnel mode, the items to be modified are the outermost ones.
83 * Pointer to item specification.
85 * Pointer to flow attributes structure.
87 * Pointer to the sub flow.
88 * @param[in] tunnel_decap
89 * Whether action is after tunnel decapsulation.
92 flow_dv_attr_init(const struct rte_flow_item *item, union flow_dv_attr *attr,
93 struct mlx5_flow *dev_flow, bool tunnel_decap)
96 * If layers is already initialized, it means this dev_flow is the
97 * suffix flow, the layers flags is set by the prefix flow. Need to
98 * use the layer flags from prefix flow as the suffix flow may not
99 * have the user defined items as the flow is split.
101 if (dev_flow->layers) {
102 if (dev_flow->layers & MLX5_FLOW_LAYER_OUTER_L3_IPV4)
104 else if (dev_flow->layers & MLX5_FLOW_LAYER_OUTER_L3_IPV6)
106 if (dev_flow->layers & MLX5_FLOW_LAYER_OUTER_L4_TCP)
108 else if (dev_flow->layers & MLX5_FLOW_LAYER_OUTER_L4_UDP)
113 for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
114 uint8_t next_protocol = 0xff;
115 switch (item->type) {
116 case RTE_FLOW_ITEM_TYPE_GRE:
117 case RTE_FLOW_ITEM_TYPE_NVGRE:
118 case RTE_FLOW_ITEM_TYPE_VXLAN:
119 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
120 case RTE_FLOW_ITEM_TYPE_GENEVE:
121 case RTE_FLOW_ITEM_TYPE_MPLS:
125 case RTE_FLOW_ITEM_TYPE_IPV4:
128 if (item->mask != NULL &&
129 ((const struct rte_flow_item_ipv4 *)
130 item->mask)->hdr.next_proto_id)
132 ((const struct rte_flow_item_ipv4 *)
133 (item->spec))->hdr.next_proto_id &
134 ((const struct rte_flow_item_ipv4 *)
135 (item->mask))->hdr.next_proto_id;
136 if ((next_protocol == IPPROTO_IPIP ||
137 next_protocol == IPPROTO_IPV6) && tunnel_decap)
140 case RTE_FLOW_ITEM_TYPE_IPV6:
143 if (item->mask != NULL &&
144 ((const struct rte_flow_item_ipv6 *)
145 item->mask)->hdr.proto)
147 ((const struct rte_flow_item_ipv6 *)
148 (item->spec))->hdr.proto &
149 ((const struct rte_flow_item_ipv6 *)
150 (item->mask))->hdr.proto;
151 if ((next_protocol == IPPROTO_IPIP ||
152 next_protocol == IPPROTO_IPV6) && tunnel_decap)
155 case RTE_FLOW_ITEM_TYPE_UDP:
159 case RTE_FLOW_ITEM_TYPE_TCP:
171 * Convert rte_mtr_color to mlx5 color.
180 rte_col_2_mlx5_col(enum rte_color rcol)
183 case RTE_COLOR_GREEN:
184 return MLX5_FLOW_COLOR_GREEN;
185 case RTE_COLOR_YELLOW:
186 return MLX5_FLOW_COLOR_YELLOW;
188 return MLX5_FLOW_COLOR_RED;
192 return MLX5_FLOW_COLOR_UNDEFINED;
195 struct field_modify_info {
196 uint32_t size; /* Size of field in protocol header, in bytes. */
197 uint32_t offset; /* Offset of field in protocol header, in bytes. */
198 enum mlx5_modification_field id;
201 struct field_modify_info modify_eth[] = {
202 {4, 0, MLX5_MODI_OUT_DMAC_47_16},
203 {2, 4, MLX5_MODI_OUT_DMAC_15_0},
204 {4, 6, MLX5_MODI_OUT_SMAC_47_16},
205 {2, 10, MLX5_MODI_OUT_SMAC_15_0},
209 struct field_modify_info modify_vlan_out_first_vid[] = {
210 /* Size in bits !!! */
211 {12, 0, MLX5_MODI_OUT_FIRST_VID},
215 struct field_modify_info modify_ipv4[] = {
216 {1, 1, MLX5_MODI_OUT_IP_DSCP},
217 {1, 8, MLX5_MODI_OUT_IPV4_TTL},
218 {4, 12, MLX5_MODI_OUT_SIPV4},
219 {4, 16, MLX5_MODI_OUT_DIPV4},
223 struct field_modify_info modify_ipv6[] = {
224 {1, 0, MLX5_MODI_OUT_IP_DSCP},
225 {1, 7, MLX5_MODI_OUT_IPV6_HOPLIMIT},
226 {4, 8, MLX5_MODI_OUT_SIPV6_127_96},
227 {4, 12, MLX5_MODI_OUT_SIPV6_95_64},
228 {4, 16, MLX5_MODI_OUT_SIPV6_63_32},
229 {4, 20, MLX5_MODI_OUT_SIPV6_31_0},
230 {4, 24, MLX5_MODI_OUT_DIPV6_127_96},
231 {4, 28, MLX5_MODI_OUT_DIPV6_95_64},
232 {4, 32, MLX5_MODI_OUT_DIPV6_63_32},
233 {4, 36, MLX5_MODI_OUT_DIPV6_31_0},
237 struct field_modify_info modify_udp[] = {
238 {2, 0, MLX5_MODI_OUT_UDP_SPORT},
239 {2, 2, MLX5_MODI_OUT_UDP_DPORT},
243 struct field_modify_info modify_tcp[] = {
244 {2, 0, MLX5_MODI_OUT_TCP_SPORT},
245 {2, 2, MLX5_MODI_OUT_TCP_DPORT},
246 {4, 4, MLX5_MODI_OUT_TCP_SEQ_NUM},
247 {4, 8, MLX5_MODI_OUT_TCP_ACK_NUM},
252 mlx5_flow_tunnel_ip_check(const struct rte_flow_item *item __rte_unused,
253 uint8_t next_protocol, uint64_t *item_flags,
256 MLX5_ASSERT(item->type == RTE_FLOW_ITEM_TYPE_IPV4 ||
257 item->type == RTE_FLOW_ITEM_TYPE_IPV6);
258 if (next_protocol == IPPROTO_IPIP) {
259 *item_flags |= MLX5_FLOW_LAYER_IPIP;
262 if (next_protocol == IPPROTO_IPV6) {
263 *item_flags |= MLX5_FLOW_LAYER_IPV6_ENCAP;
269 * Acquire the synchronizing object to protect multithreaded access
270 * to shared dv context. Lock occurs only if context is actually
271 * shared, i.e. we have multiport IB device and representors are
275 * Pointer to the rte_eth_dev structure.
278 flow_dv_shared_lock(struct rte_eth_dev *dev)
280 struct mlx5_priv *priv = dev->data->dev_private;
281 struct mlx5_ibv_shared *sh = priv->sh;
283 if (sh->dv_refcnt > 1) {
286 ret = pthread_mutex_lock(&sh->dv_mutex);
293 flow_dv_shared_unlock(struct rte_eth_dev *dev)
295 struct mlx5_priv *priv = dev->data->dev_private;
296 struct mlx5_ibv_shared *sh = priv->sh;
298 if (sh->dv_refcnt > 1) {
301 ret = pthread_mutex_unlock(&sh->dv_mutex);
307 /* Update VLAN's VID/PCP based on input rte_flow_action.
310 * Pointer to struct rte_flow_action.
312 * Pointer to struct rte_vlan_hdr.
315 mlx5_update_vlan_vid_pcp(const struct rte_flow_action *action,
316 struct rte_vlan_hdr *vlan)
319 if (action->type == RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP) {
321 ((const struct rte_flow_action_of_set_vlan_pcp *)
322 action->conf)->vlan_pcp;
323 vlan_tci = vlan_tci << MLX5DV_FLOW_VLAN_PCP_SHIFT;
324 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_PCP_MASK;
325 vlan->vlan_tci |= vlan_tci;
326 } else if (action->type == RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID) {
327 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_VID_MASK;
328 vlan->vlan_tci |= rte_be_to_cpu_16
329 (((const struct rte_flow_action_of_set_vlan_vid *)
330 action->conf)->vlan_vid);
335 * Fetch 1, 2, 3 or 4 byte field from the byte array
336 * and return as unsigned integer in host-endian format.
339 * Pointer to data array.
341 * Size of field to extract.
344 * converted field in host endian format.
346 static inline uint32_t
347 flow_dv_fetch_field(const uint8_t *data, uint32_t size)
356 ret = rte_be_to_cpu_16(*(const unaligned_uint16_t *)data);
359 ret = rte_be_to_cpu_16(*(const unaligned_uint16_t *)data);
360 ret = (ret << 8) | *(data + sizeof(uint16_t));
363 ret = rte_be_to_cpu_32(*(const unaligned_uint32_t *)data);
374 * Convert modify-header action to DV specification.
376 * Data length of each action is determined by provided field description
377 * and the item mask. Data bit offset and width of each action is determined
378 * by provided item mask.
381 * Pointer to item specification.
383 * Pointer to field modification information.
384 * For MLX5_MODIFICATION_TYPE_SET specifies destination field.
385 * For MLX5_MODIFICATION_TYPE_ADD specifies destination field.
386 * For MLX5_MODIFICATION_TYPE_COPY specifies source field.
388 * Destination field info for MLX5_MODIFICATION_TYPE_COPY in @type.
389 * Negative offset value sets the same offset as source offset.
390 * size field is ignored, value is taken from source field.
391 * @param[in,out] resource
392 * Pointer to the modify-header resource.
394 * Type of modification.
396 * Pointer to the error structure.
399 * 0 on success, a negative errno value otherwise and rte_errno is set.
402 flow_dv_convert_modify_action(struct rte_flow_item *item,
403 struct field_modify_info *field,
404 struct field_modify_info *dcopy,
405 struct mlx5_flow_dv_modify_hdr_resource *resource,
406 uint32_t type, struct rte_flow_error *error)
408 uint32_t i = resource->actions_num;
409 struct mlx5_modification_cmd *actions = resource->actions;
412 * The item and mask are provided in big-endian format.
413 * The fields should be presented as in big-endian format either.
414 * Mask must be always present, it defines the actual field width.
416 MLX5_ASSERT(item->mask);
417 MLX5_ASSERT(field->size);
424 if (i >= MLX5_MAX_MODIFY_NUM)
425 return rte_flow_error_set(error, EINVAL,
426 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
427 "too many items to modify");
428 /* Fetch variable byte size mask from the array. */
429 mask = flow_dv_fetch_field((const uint8_t *)item->mask +
430 field->offset, field->size);
435 /* Deduce actual data width in bits from mask value. */
436 off_b = rte_bsf32(mask);
437 size_b = sizeof(uint32_t) * CHAR_BIT -
438 off_b - __builtin_clz(mask);
440 size_b = size_b == sizeof(uint32_t) * CHAR_BIT ? 0 : size_b;
441 actions[i] = (struct mlx5_modification_cmd) {
447 /* Convert entire record to expected big-endian format. */
448 actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
449 if (type == MLX5_MODIFICATION_TYPE_COPY) {
451 actions[i].dst_field = dcopy->id;
452 actions[i].dst_offset =
453 (int)dcopy->offset < 0 ? off_b : dcopy->offset;
454 /* Convert entire record to big-endian format. */
455 actions[i].data1 = rte_cpu_to_be_32(actions[i].data1);
457 MLX5_ASSERT(item->spec);
458 data = flow_dv_fetch_field((const uint8_t *)item->spec +
459 field->offset, field->size);
460 /* Shift out the trailing masked bits from data. */
461 data = (data & mask) >> off_b;
462 actions[i].data1 = rte_cpu_to_be_32(data);
466 } while (field->size);
467 if (resource->actions_num == i)
468 return rte_flow_error_set(error, EINVAL,
469 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
470 "invalid modification flow item");
471 resource->actions_num = i;
476 * Convert modify-header set IPv4 address action to DV specification.
478 * @param[in,out] resource
479 * Pointer to the modify-header resource.
481 * Pointer to action specification.
483 * Pointer to the error structure.
486 * 0 on success, a negative errno value otherwise and rte_errno is set.
489 flow_dv_convert_action_modify_ipv4
490 (struct mlx5_flow_dv_modify_hdr_resource *resource,
491 const struct rte_flow_action *action,
492 struct rte_flow_error *error)
494 const struct rte_flow_action_set_ipv4 *conf =
495 (const struct rte_flow_action_set_ipv4 *)(action->conf);
496 struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
497 struct rte_flow_item_ipv4 ipv4;
498 struct rte_flow_item_ipv4 ipv4_mask;
500 memset(&ipv4, 0, sizeof(ipv4));
501 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
502 if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC) {
503 ipv4.hdr.src_addr = conf->ipv4_addr;
504 ipv4_mask.hdr.src_addr = rte_flow_item_ipv4_mask.hdr.src_addr;
506 ipv4.hdr.dst_addr = conf->ipv4_addr;
507 ipv4_mask.hdr.dst_addr = rte_flow_item_ipv4_mask.hdr.dst_addr;
510 item.mask = &ipv4_mask;
511 return flow_dv_convert_modify_action(&item, modify_ipv4, NULL, resource,
512 MLX5_MODIFICATION_TYPE_SET, error);
516 * Convert modify-header set IPv6 address action to DV specification.
518 * @param[in,out] resource
519 * Pointer to the modify-header resource.
521 * Pointer to action specification.
523 * Pointer to the error structure.
526 * 0 on success, a negative errno value otherwise and rte_errno is set.
529 flow_dv_convert_action_modify_ipv6
530 (struct mlx5_flow_dv_modify_hdr_resource *resource,
531 const struct rte_flow_action *action,
532 struct rte_flow_error *error)
534 const struct rte_flow_action_set_ipv6 *conf =
535 (const struct rte_flow_action_set_ipv6 *)(action->conf);
536 struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
537 struct rte_flow_item_ipv6 ipv6;
538 struct rte_flow_item_ipv6 ipv6_mask;
540 memset(&ipv6, 0, sizeof(ipv6));
541 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
542 if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC) {
543 memcpy(&ipv6.hdr.src_addr, &conf->ipv6_addr,
544 sizeof(ipv6.hdr.src_addr));
545 memcpy(&ipv6_mask.hdr.src_addr,
546 &rte_flow_item_ipv6_mask.hdr.src_addr,
547 sizeof(ipv6.hdr.src_addr));
549 memcpy(&ipv6.hdr.dst_addr, &conf->ipv6_addr,
550 sizeof(ipv6.hdr.dst_addr));
551 memcpy(&ipv6_mask.hdr.dst_addr,
552 &rte_flow_item_ipv6_mask.hdr.dst_addr,
553 sizeof(ipv6.hdr.dst_addr));
556 item.mask = &ipv6_mask;
557 return flow_dv_convert_modify_action(&item, modify_ipv6, NULL, resource,
558 MLX5_MODIFICATION_TYPE_SET, error);
562 * Convert modify-header set MAC address action to DV specification.
564 * @param[in,out] resource
565 * Pointer to the modify-header resource.
567 * Pointer to action specification.
569 * Pointer to the error structure.
572 * 0 on success, a negative errno value otherwise and rte_errno is set.
575 flow_dv_convert_action_modify_mac
576 (struct mlx5_flow_dv_modify_hdr_resource *resource,
577 const struct rte_flow_action *action,
578 struct rte_flow_error *error)
580 const struct rte_flow_action_set_mac *conf =
581 (const struct rte_flow_action_set_mac *)(action->conf);
582 struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_ETH };
583 struct rte_flow_item_eth eth;
584 struct rte_flow_item_eth eth_mask;
586 memset(ð, 0, sizeof(eth));
587 memset(ð_mask, 0, sizeof(eth_mask));
588 if (action->type == RTE_FLOW_ACTION_TYPE_SET_MAC_SRC) {
589 memcpy(ð.src.addr_bytes, &conf->mac_addr,
590 sizeof(eth.src.addr_bytes));
591 memcpy(ð_mask.src.addr_bytes,
592 &rte_flow_item_eth_mask.src.addr_bytes,
593 sizeof(eth_mask.src.addr_bytes));
595 memcpy(ð.dst.addr_bytes, &conf->mac_addr,
596 sizeof(eth.dst.addr_bytes));
597 memcpy(ð_mask.dst.addr_bytes,
598 &rte_flow_item_eth_mask.dst.addr_bytes,
599 sizeof(eth_mask.dst.addr_bytes));
602 item.mask = ð_mask;
603 return flow_dv_convert_modify_action(&item, modify_eth, NULL, resource,
604 MLX5_MODIFICATION_TYPE_SET, error);
608 * Convert modify-header set VLAN VID action to DV specification.
610 * @param[in,out] resource
611 * Pointer to the modify-header resource.
613 * Pointer to action specification.
615 * Pointer to the error structure.
618 * 0 on success, a negative errno value otherwise and rte_errno is set.
621 flow_dv_convert_action_modify_vlan_vid
622 (struct mlx5_flow_dv_modify_hdr_resource *resource,
623 const struct rte_flow_action *action,
624 struct rte_flow_error *error)
626 const struct rte_flow_action_of_set_vlan_vid *conf =
627 (const struct rte_flow_action_of_set_vlan_vid *)(action->conf);
628 int i = resource->actions_num;
629 struct mlx5_modification_cmd *actions = resource->actions;
630 struct field_modify_info *field = modify_vlan_out_first_vid;
632 if (i >= MLX5_MAX_MODIFY_NUM)
633 return rte_flow_error_set(error, EINVAL,
634 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
635 "too many items to modify");
636 actions[i] = (struct mlx5_modification_cmd) {
637 .action_type = MLX5_MODIFICATION_TYPE_SET,
639 .length = field->size,
640 .offset = field->offset,
642 actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
643 actions[i].data1 = conf->vlan_vid;
644 actions[i].data1 = actions[i].data1 << 16;
645 resource->actions_num = ++i;
650 * Convert modify-header set TP action to DV specification.
652 * @param[in,out] resource
653 * Pointer to the modify-header resource.
655 * Pointer to action specification.
657 * Pointer to rte_flow_item objects list.
659 * Pointer to flow attributes structure.
660 * @param[in] dev_flow
661 * Pointer to the sub flow.
662 * @param[in] tunnel_decap
663 * Whether action is after tunnel decapsulation.
665 * Pointer to the error structure.
668 * 0 on success, a negative errno value otherwise and rte_errno is set.
671 flow_dv_convert_action_modify_tp
672 (struct mlx5_flow_dv_modify_hdr_resource *resource,
673 const struct rte_flow_action *action,
674 const struct rte_flow_item *items,
675 union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
676 bool tunnel_decap, struct rte_flow_error *error)
678 const struct rte_flow_action_set_tp *conf =
679 (const struct rte_flow_action_set_tp *)(action->conf);
680 struct rte_flow_item item;
681 struct rte_flow_item_udp udp;
682 struct rte_flow_item_udp udp_mask;
683 struct rte_flow_item_tcp tcp;
684 struct rte_flow_item_tcp tcp_mask;
685 struct field_modify_info *field;
688 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
690 memset(&udp, 0, sizeof(udp));
691 memset(&udp_mask, 0, sizeof(udp_mask));
692 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
693 udp.hdr.src_port = conf->port;
694 udp_mask.hdr.src_port =
695 rte_flow_item_udp_mask.hdr.src_port;
697 udp.hdr.dst_port = conf->port;
698 udp_mask.hdr.dst_port =
699 rte_flow_item_udp_mask.hdr.dst_port;
701 item.type = RTE_FLOW_ITEM_TYPE_UDP;
703 item.mask = &udp_mask;
707 memset(&tcp, 0, sizeof(tcp));
708 memset(&tcp_mask, 0, sizeof(tcp_mask));
709 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
710 tcp.hdr.src_port = conf->port;
711 tcp_mask.hdr.src_port =
712 rte_flow_item_tcp_mask.hdr.src_port;
714 tcp.hdr.dst_port = conf->port;
715 tcp_mask.hdr.dst_port =
716 rte_flow_item_tcp_mask.hdr.dst_port;
718 item.type = RTE_FLOW_ITEM_TYPE_TCP;
720 item.mask = &tcp_mask;
723 return flow_dv_convert_modify_action(&item, field, NULL, resource,
724 MLX5_MODIFICATION_TYPE_SET, error);
728 * Convert modify-header set TTL action to DV specification.
730 * @param[in,out] resource
731 * Pointer to the modify-header resource.
733 * Pointer to action specification.
735 * Pointer to rte_flow_item objects list.
737 * Pointer to flow attributes structure.
738 * @param[in] dev_flow
739 * Pointer to the sub flow.
740 * @param[in] tunnel_decap
741 * Whether action is after tunnel decapsulation.
743 * Pointer to the error structure.
746 * 0 on success, a negative errno value otherwise and rte_errno is set.
749 flow_dv_convert_action_modify_ttl
750 (struct mlx5_flow_dv_modify_hdr_resource *resource,
751 const struct rte_flow_action *action,
752 const struct rte_flow_item *items,
753 union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
754 bool tunnel_decap, struct rte_flow_error *error)
756 const struct rte_flow_action_set_ttl *conf =
757 (const struct rte_flow_action_set_ttl *)(action->conf);
758 struct rte_flow_item item;
759 struct rte_flow_item_ipv4 ipv4;
760 struct rte_flow_item_ipv4 ipv4_mask;
761 struct rte_flow_item_ipv6 ipv6;
762 struct rte_flow_item_ipv6 ipv6_mask;
763 struct field_modify_info *field;
766 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
768 memset(&ipv4, 0, sizeof(ipv4));
769 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
770 ipv4.hdr.time_to_live = conf->ttl_value;
771 ipv4_mask.hdr.time_to_live = 0xFF;
772 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
774 item.mask = &ipv4_mask;
778 memset(&ipv6, 0, sizeof(ipv6));
779 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
780 ipv6.hdr.hop_limits = conf->ttl_value;
781 ipv6_mask.hdr.hop_limits = 0xFF;
782 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
784 item.mask = &ipv6_mask;
787 return flow_dv_convert_modify_action(&item, field, NULL, resource,
788 MLX5_MODIFICATION_TYPE_SET, error);
792 * Convert modify-header decrement TTL action to DV specification.
794 * @param[in,out] resource
795 * Pointer to the modify-header resource.
797 * Pointer to action specification.
799 * Pointer to rte_flow_item objects list.
801 * Pointer to flow attributes structure.
802 * @param[in] dev_flow
803 * Pointer to the sub flow.
804 * @param[in] tunnel_decap
805 * Whether action is after tunnel decapsulation.
807 * Pointer to the error structure.
810 * 0 on success, a negative errno value otherwise and rte_errno is set.
813 flow_dv_convert_action_modify_dec_ttl
814 (struct mlx5_flow_dv_modify_hdr_resource *resource,
815 const struct rte_flow_item *items,
816 union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
817 bool tunnel_decap, struct rte_flow_error *error)
819 struct rte_flow_item item;
820 struct rte_flow_item_ipv4 ipv4;
821 struct rte_flow_item_ipv4 ipv4_mask;
822 struct rte_flow_item_ipv6 ipv6;
823 struct rte_flow_item_ipv6 ipv6_mask;
824 struct field_modify_info *field;
827 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
829 memset(&ipv4, 0, sizeof(ipv4));
830 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
831 ipv4.hdr.time_to_live = 0xFF;
832 ipv4_mask.hdr.time_to_live = 0xFF;
833 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
835 item.mask = &ipv4_mask;
839 memset(&ipv6, 0, sizeof(ipv6));
840 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
841 ipv6.hdr.hop_limits = 0xFF;
842 ipv6_mask.hdr.hop_limits = 0xFF;
843 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
845 item.mask = &ipv6_mask;
848 return flow_dv_convert_modify_action(&item, field, NULL, resource,
849 MLX5_MODIFICATION_TYPE_ADD, error);
853 * Convert modify-header increment/decrement TCP Sequence number
854 * to DV specification.
856 * @param[in,out] resource
857 * Pointer to the modify-header resource.
859 * Pointer to action specification.
861 * Pointer to the error structure.
864 * 0 on success, a negative errno value otherwise and rte_errno is set.
867 flow_dv_convert_action_modify_tcp_seq
868 (struct mlx5_flow_dv_modify_hdr_resource *resource,
869 const struct rte_flow_action *action,
870 struct rte_flow_error *error)
872 const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
873 uint64_t value = rte_be_to_cpu_32(*conf);
874 struct rte_flow_item item;
875 struct rte_flow_item_tcp tcp;
876 struct rte_flow_item_tcp tcp_mask;
878 memset(&tcp, 0, sizeof(tcp));
879 memset(&tcp_mask, 0, sizeof(tcp_mask));
880 if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ)
882 * The HW has no decrement operation, only increment operation.
883 * To simulate decrement X from Y using increment operation
884 * we need to add UINT32_MAX X times to Y.
885 * Each adding of UINT32_MAX decrements Y by 1.
888 tcp.hdr.sent_seq = rte_cpu_to_be_32((uint32_t)value);
889 tcp_mask.hdr.sent_seq = RTE_BE32(UINT32_MAX);
890 item.type = RTE_FLOW_ITEM_TYPE_TCP;
892 item.mask = &tcp_mask;
893 return flow_dv_convert_modify_action(&item, modify_tcp, NULL, resource,
894 MLX5_MODIFICATION_TYPE_ADD, error);
898 * Convert modify-header increment/decrement TCP Acknowledgment number
899 * to DV specification.
901 * @param[in,out] resource
902 * Pointer to the modify-header resource.
904 * Pointer to action specification.
906 * Pointer to the error structure.
909 * 0 on success, a negative errno value otherwise and rte_errno is set.
912 flow_dv_convert_action_modify_tcp_ack
913 (struct mlx5_flow_dv_modify_hdr_resource *resource,
914 const struct rte_flow_action *action,
915 struct rte_flow_error *error)
917 const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
918 uint64_t value = rte_be_to_cpu_32(*conf);
919 struct rte_flow_item item;
920 struct rte_flow_item_tcp tcp;
921 struct rte_flow_item_tcp tcp_mask;
923 memset(&tcp, 0, sizeof(tcp));
924 memset(&tcp_mask, 0, sizeof(tcp_mask));
925 if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK)
927 * The HW has no decrement operation, only increment operation.
928 * To simulate decrement X from Y using increment operation
929 * we need to add UINT32_MAX X times to Y.
930 * Each adding of UINT32_MAX decrements Y by 1.
933 tcp.hdr.recv_ack = rte_cpu_to_be_32((uint32_t)value);
934 tcp_mask.hdr.recv_ack = RTE_BE32(UINT32_MAX);
935 item.type = RTE_FLOW_ITEM_TYPE_TCP;
937 item.mask = &tcp_mask;
938 return flow_dv_convert_modify_action(&item, modify_tcp, NULL, resource,
939 MLX5_MODIFICATION_TYPE_ADD, error);
942 static enum mlx5_modification_field reg_to_field[] = {
943 [REG_NONE] = MLX5_MODI_OUT_NONE,
944 [REG_A] = MLX5_MODI_META_DATA_REG_A,
945 [REG_B] = MLX5_MODI_META_DATA_REG_B,
946 [REG_C_0] = MLX5_MODI_META_REG_C_0,
947 [REG_C_1] = MLX5_MODI_META_REG_C_1,
948 [REG_C_2] = MLX5_MODI_META_REG_C_2,
949 [REG_C_3] = MLX5_MODI_META_REG_C_3,
950 [REG_C_4] = MLX5_MODI_META_REG_C_4,
951 [REG_C_5] = MLX5_MODI_META_REG_C_5,
952 [REG_C_6] = MLX5_MODI_META_REG_C_6,
953 [REG_C_7] = MLX5_MODI_META_REG_C_7,
957 * Convert register set to DV specification.
959 * @param[in,out] resource
960 * Pointer to the modify-header resource.
962 * Pointer to action specification.
964 * Pointer to the error structure.
967 * 0 on success, a negative errno value otherwise and rte_errno is set.
970 flow_dv_convert_action_set_reg
971 (struct mlx5_flow_dv_modify_hdr_resource *resource,
972 const struct rte_flow_action *action,
973 struct rte_flow_error *error)
975 const struct mlx5_rte_flow_action_set_tag *conf = action->conf;
976 struct mlx5_modification_cmd *actions = resource->actions;
977 uint32_t i = resource->actions_num;
979 if (i >= MLX5_MAX_MODIFY_NUM)
980 return rte_flow_error_set(error, EINVAL,
981 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
982 "too many items to modify");
983 MLX5_ASSERT(conf->id != REG_NONE);
984 MLX5_ASSERT(conf->id < RTE_DIM(reg_to_field));
985 actions[i] = (struct mlx5_modification_cmd) {
986 .action_type = MLX5_MODIFICATION_TYPE_SET,
987 .field = reg_to_field[conf->id],
989 actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
990 actions[i].data1 = rte_cpu_to_be_32(conf->data);
992 resource->actions_num = i;
997 * Convert SET_TAG action to DV specification.
1000 * Pointer to the rte_eth_dev structure.
1001 * @param[in,out] resource
1002 * Pointer to the modify-header resource.
1004 * Pointer to action specification.
1006 * Pointer to the error structure.
1009 * 0 on success, a negative errno value otherwise and rte_errno is set.
1012 flow_dv_convert_action_set_tag
1013 (struct rte_eth_dev *dev,
1014 struct mlx5_flow_dv_modify_hdr_resource *resource,
1015 const struct rte_flow_action_set_tag *conf,
1016 struct rte_flow_error *error)
1018 rte_be32_t data = rte_cpu_to_be_32(conf->data);
1019 rte_be32_t mask = rte_cpu_to_be_32(conf->mask);
1020 struct rte_flow_item item = {
1024 struct field_modify_info reg_c_x[] = {
1027 enum mlx5_modification_field reg_type;
1030 ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, conf->index, error);
1033 MLX5_ASSERT(ret != REG_NONE);
1034 MLX5_ASSERT((unsigned int)ret < RTE_DIM(reg_to_field));
1035 reg_type = reg_to_field[ret];
1036 MLX5_ASSERT(reg_type > 0);
1037 reg_c_x[0] = (struct field_modify_info){4, 0, reg_type};
1038 return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1039 MLX5_MODIFICATION_TYPE_SET, error);
1043 * Convert internal COPY_REG action to DV specification.
1046 * Pointer to the rte_eth_dev structure.
1047 * @param[in,out] res
1048 * Pointer to the modify-header resource.
1050 * Pointer to action specification.
1052 * Pointer to the error structure.
1055 * 0 on success, a negative errno value otherwise and rte_errno is set.
1058 flow_dv_convert_action_copy_mreg(struct rte_eth_dev *dev,
1059 struct mlx5_flow_dv_modify_hdr_resource *res,
1060 const struct rte_flow_action *action,
1061 struct rte_flow_error *error)
1063 const struct mlx5_flow_action_copy_mreg *conf = action->conf;
1064 rte_be32_t mask = RTE_BE32(UINT32_MAX);
1065 struct rte_flow_item item = {
1069 struct field_modify_info reg_src[] = {
1070 {4, 0, reg_to_field[conf->src]},
1073 struct field_modify_info reg_dst = {
1075 .id = reg_to_field[conf->dst],
1077 /* Adjust reg_c[0] usage according to reported mask. */
1078 if (conf->dst == REG_C_0 || conf->src == REG_C_0) {
1079 struct mlx5_priv *priv = dev->data->dev_private;
1080 uint32_t reg_c0 = priv->sh->dv_regc0_mask;
1082 MLX5_ASSERT(reg_c0);
1083 MLX5_ASSERT(priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY);
1084 if (conf->dst == REG_C_0) {
1085 /* Copy to reg_c[0], within mask only. */
1086 reg_dst.offset = rte_bsf32(reg_c0);
1088 * Mask is ignoring the enianness, because
1089 * there is no conversion in datapath.
1091 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1092 /* Copy from destination lower bits to reg_c[0]. */
1093 mask = reg_c0 >> reg_dst.offset;
1095 /* Copy from destination upper bits to reg_c[0]. */
1096 mask = reg_c0 << (sizeof(reg_c0) * CHAR_BIT -
1097 rte_fls_u32(reg_c0));
1100 mask = rte_cpu_to_be_32(reg_c0);
1101 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1102 /* Copy from reg_c[0] to destination lower bits. */
1105 /* Copy from reg_c[0] to destination upper bits. */
1106 reg_dst.offset = sizeof(reg_c0) * CHAR_BIT -
1107 (rte_fls_u32(reg_c0) -
1112 return flow_dv_convert_modify_action(&item,
1113 reg_src, ®_dst, res,
1114 MLX5_MODIFICATION_TYPE_COPY,
1119 * Convert MARK action to DV specification. This routine is used
1120 * in extensive metadata only and requires metadata register to be
1121 * handled. In legacy mode hardware tag resource is engaged.
1124 * Pointer to the rte_eth_dev structure.
1126 * Pointer to MARK action specification.
1127 * @param[in,out] resource
1128 * Pointer to the modify-header resource.
1130 * Pointer to the error structure.
1133 * 0 on success, a negative errno value otherwise and rte_errno is set.
1136 flow_dv_convert_action_mark(struct rte_eth_dev *dev,
1137 const struct rte_flow_action_mark *conf,
1138 struct mlx5_flow_dv_modify_hdr_resource *resource,
1139 struct rte_flow_error *error)
1141 struct mlx5_priv *priv = dev->data->dev_private;
1142 rte_be32_t mask = rte_cpu_to_be_32(MLX5_FLOW_MARK_MASK &
1143 priv->sh->dv_mark_mask);
1144 rte_be32_t data = rte_cpu_to_be_32(conf->id) & mask;
1145 struct rte_flow_item item = {
1149 struct field_modify_info reg_c_x[] = {
1150 {4, 0, 0}, /* dynamic instead of MLX5_MODI_META_REG_C_1. */
1156 return rte_flow_error_set(error, EINVAL,
1157 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1158 NULL, "zero mark action mask");
1159 reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1162 MLX5_ASSERT(reg > 0);
1163 if (reg == REG_C_0) {
1164 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
1165 uint32_t shl_c0 = rte_bsf32(msk_c0);
1167 data = rte_cpu_to_be_32(rte_cpu_to_be_32(data) << shl_c0);
1168 mask = rte_cpu_to_be_32(mask) & msk_c0;
1169 mask = rte_cpu_to_be_32(mask << shl_c0);
1171 reg_c_x[0].id = reg_to_field[reg];
1172 return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1173 MLX5_MODIFICATION_TYPE_SET, error);
1177 * Get metadata register index for specified steering domain.
1180 * Pointer to the rte_eth_dev structure.
1182 * Attributes of flow to determine steering domain.
1184 * Pointer to the error structure.
1187 * positive index on success, a negative errno value otherwise
1188 * and rte_errno is set.
1190 static enum modify_reg
1191 flow_dv_get_metadata_reg(struct rte_eth_dev *dev,
1192 const struct rte_flow_attr *attr,
1193 struct rte_flow_error *error)
1196 mlx5_flow_get_reg_id(dev, attr->transfer ?
1200 MLX5_METADATA_RX, 0, error);
1202 return rte_flow_error_set(error,
1203 ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
1204 NULL, "unavailable "
1205 "metadata register");
1210 * Convert SET_META action to DV specification.
1213 * Pointer to the rte_eth_dev structure.
1214 * @param[in,out] resource
1215 * Pointer to the modify-header resource.
1217 * Attributes of flow that includes this item.
1219 * Pointer to action specification.
1221 * Pointer to the error structure.
1224 * 0 on success, a negative errno value otherwise and rte_errno is set.
1227 flow_dv_convert_action_set_meta
1228 (struct rte_eth_dev *dev,
1229 struct mlx5_flow_dv_modify_hdr_resource *resource,
1230 const struct rte_flow_attr *attr,
1231 const struct rte_flow_action_set_meta *conf,
1232 struct rte_flow_error *error)
1234 uint32_t data = conf->data;
1235 uint32_t mask = conf->mask;
1236 struct rte_flow_item item = {
1240 struct field_modify_info reg_c_x[] = {
1243 int reg = flow_dv_get_metadata_reg(dev, attr, error);
1248 * In datapath code there is no endianness
1249 * coversions for perfromance reasons, all
1250 * pattern conversions are done in rte_flow.
1252 if (reg == REG_C_0) {
1253 struct mlx5_priv *priv = dev->data->dev_private;
1254 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
1257 MLX5_ASSERT(msk_c0);
1258 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1259 shl_c0 = rte_bsf32(msk_c0);
1261 shl_c0 = sizeof(msk_c0) * CHAR_BIT - rte_fls_u32(msk_c0);
1265 MLX5_ASSERT(!(~msk_c0 & rte_cpu_to_be_32(mask)));
1267 reg_c_x[0] = (struct field_modify_info){4, 0, reg_to_field[reg]};
1268 /* The routine expects parameters in memory as big-endian ones. */
1269 return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1270 MLX5_MODIFICATION_TYPE_SET, error);
1274 * Convert modify-header set IPv4 DSCP action to DV specification.
1276 * @param[in,out] resource
1277 * Pointer to the modify-header resource.
1279 * Pointer to action specification.
1281 * Pointer to the error structure.
1284 * 0 on success, a negative errno value otherwise and rte_errno is set.
1287 flow_dv_convert_action_modify_ipv4_dscp
1288 (struct mlx5_flow_dv_modify_hdr_resource *resource,
1289 const struct rte_flow_action *action,
1290 struct rte_flow_error *error)
1292 const struct rte_flow_action_set_dscp *conf =
1293 (const struct rte_flow_action_set_dscp *)(action->conf);
1294 struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
1295 struct rte_flow_item_ipv4 ipv4;
1296 struct rte_flow_item_ipv4 ipv4_mask;
1298 memset(&ipv4, 0, sizeof(ipv4));
1299 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
1300 ipv4.hdr.type_of_service = conf->dscp;
1301 ipv4_mask.hdr.type_of_service = RTE_IPV4_HDR_DSCP_MASK >> 2;
1303 item.mask = &ipv4_mask;
1304 return flow_dv_convert_modify_action(&item, modify_ipv4, NULL, resource,
1305 MLX5_MODIFICATION_TYPE_SET, error);
1309 * Convert modify-header set IPv6 DSCP action to DV specification.
1311 * @param[in,out] resource
1312 * Pointer to the modify-header resource.
1314 * Pointer to action specification.
1316 * Pointer to the error structure.
1319 * 0 on success, a negative errno value otherwise and rte_errno is set.
1322 flow_dv_convert_action_modify_ipv6_dscp
1323 (struct mlx5_flow_dv_modify_hdr_resource *resource,
1324 const struct rte_flow_action *action,
1325 struct rte_flow_error *error)
1327 const struct rte_flow_action_set_dscp *conf =
1328 (const struct rte_flow_action_set_dscp *)(action->conf);
1329 struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
1330 struct rte_flow_item_ipv6 ipv6;
1331 struct rte_flow_item_ipv6 ipv6_mask;
1333 memset(&ipv6, 0, sizeof(ipv6));
1334 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
1336 * Even though the DSCP bits offset of IPv6 is not byte aligned,
1337 * rdma-core only accept the DSCP bits byte aligned start from
1338 * bit 0 to 5 as to be compatible with IPv4. No need to shift the
1339 * bits in IPv6 case as rdma-core requires byte aligned value.
1341 ipv6.hdr.vtc_flow = conf->dscp;
1342 ipv6_mask.hdr.vtc_flow = RTE_IPV6_HDR_DSCP_MASK >> 22;
1344 item.mask = &ipv6_mask;
1345 return flow_dv_convert_modify_action(&item, modify_ipv6, NULL, resource,
1346 MLX5_MODIFICATION_TYPE_SET, error);
1350 * Validate MARK item.
1353 * Pointer to the rte_eth_dev structure.
1355 * Item specification.
1357 * Attributes of flow that includes this item.
1359 * Pointer to error structure.
1362 * 0 on success, a negative errno value otherwise and rte_errno is set.
1365 flow_dv_validate_item_mark(struct rte_eth_dev *dev,
1366 const struct rte_flow_item *item,
1367 const struct rte_flow_attr *attr __rte_unused,
1368 struct rte_flow_error *error)
1370 struct mlx5_priv *priv = dev->data->dev_private;
1371 struct mlx5_dev_config *config = &priv->config;
1372 const struct rte_flow_item_mark *spec = item->spec;
1373 const struct rte_flow_item_mark *mask = item->mask;
1374 const struct rte_flow_item_mark nic_mask = {
1375 .id = priv->sh->dv_mark_mask,
1379 if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
1380 return rte_flow_error_set(error, ENOTSUP,
1381 RTE_FLOW_ERROR_TYPE_ITEM, item,
1382 "extended metadata feature"
1384 if (!mlx5_flow_ext_mreg_supported(dev))
1385 return rte_flow_error_set(error, ENOTSUP,
1386 RTE_FLOW_ERROR_TYPE_ITEM, item,
1387 "extended metadata register"
1388 " isn't supported");
1390 return rte_flow_error_set(error, ENOTSUP,
1391 RTE_FLOW_ERROR_TYPE_ITEM, item,
1392 "extended metadata register"
1393 " isn't available");
1394 ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1398 return rte_flow_error_set(error, EINVAL,
1399 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1401 "data cannot be empty");
1402 if (spec->id >= (MLX5_FLOW_MARK_MAX & nic_mask.id))
1403 return rte_flow_error_set(error, EINVAL,
1404 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1406 "mark id exceeds the limit");
1409 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1410 (const uint8_t *)&nic_mask,
1411 sizeof(struct rte_flow_item_mark),
1419 * Validate META item.
1422 * Pointer to the rte_eth_dev structure.
1424 * Item specification.
1426 * Attributes of flow that includes this item.
1428 * Pointer to error structure.
1431 * 0 on success, a negative errno value otherwise and rte_errno is set.
1434 flow_dv_validate_item_meta(struct rte_eth_dev *dev __rte_unused,
1435 const struct rte_flow_item *item,
1436 const struct rte_flow_attr *attr,
1437 struct rte_flow_error *error)
1439 struct mlx5_priv *priv = dev->data->dev_private;
1440 struct mlx5_dev_config *config = &priv->config;
1441 const struct rte_flow_item_meta *spec = item->spec;
1442 const struct rte_flow_item_meta *mask = item->mask;
1443 struct rte_flow_item_meta nic_mask = {
1450 return rte_flow_error_set(error, EINVAL,
1451 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1453 "data cannot be empty");
1455 return rte_flow_error_set(error, EINVAL,
1456 RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1457 "data cannot be zero");
1458 if (config->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
1459 if (!mlx5_flow_ext_mreg_supported(dev))
1460 return rte_flow_error_set(error, ENOTSUP,
1461 RTE_FLOW_ERROR_TYPE_ITEM, item,
1462 "extended metadata register"
1463 " isn't supported");
1464 reg = flow_dv_get_metadata_reg(dev, attr, error);
1468 return rte_flow_error_set(error, ENOTSUP,
1469 RTE_FLOW_ERROR_TYPE_ITEM, item,
1473 nic_mask.data = priv->sh->dv_meta_mask;
1476 mask = &rte_flow_item_meta_mask;
1477 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1478 (const uint8_t *)&nic_mask,
1479 sizeof(struct rte_flow_item_meta),
1485 * Validate TAG item.
1488 * Pointer to the rte_eth_dev structure.
1490 * Item specification.
1492 * Attributes of flow that includes this item.
1494 * Pointer to error structure.
1497 * 0 on success, a negative errno value otherwise and rte_errno is set.
1500 flow_dv_validate_item_tag(struct rte_eth_dev *dev,
1501 const struct rte_flow_item *item,
1502 const struct rte_flow_attr *attr __rte_unused,
1503 struct rte_flow_error *error)
1505 const struct rte_flow_item_tag *spec = item->spec;
1506 const struct rte_flow_item_tag *mask = item->mask;
1507 const struct rte_flow_item_tag nic_mask = {
1508 .data = RTE_BE32(UINT32_MAX),
1513 if (!mlx5_flow_ext_mreg_supported(dev))
1514 return rte_flow_error_set(error, ENOTSUP,
1515 RTE_FLOW_ERROR_TYPE_ITEM, item,
1516 "extensive metadata register"
1517 " isn't supported");
1519 return rte_flow_error_set(error, EINVAL,
1520 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1522 "data cannot be empty");
1524 mask = &rte_flow_item_tag_mask;
1525 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1526 (const uint8_t *)&nic_mask,
1527 sizeof(struct rte_flow_item_tag),
1531 if (mask->index != 0xff)
1532 return rte_flow_error_set(error, EINVAL,
1533 RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1534 "partial mask for tag index"
1535 " is not supported");
1536 ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, spec->index, error);
1539 MLX5_ASSERT(ret != REG_NONE);
1544 * Validate vport item.
1547 * Pointer to the rte_eth_dev structure.
1549 * Item specification.
1551 * Attributes of flow that includes this item.
1552 * @param[in] item_flags
1553 * Bit-fields that holds the items detected until now.
1555 * Pointer to error structure.
1558 * 0 on success, a negative errno value otherwise and rte_errno is set.
1561 flow_dv_validate_item_port_id(struct rte_eth_dev *dev,
1562 const struct rte_flow_item *item,
1563 const struct rte_flow_attr *attr,
1564 uint64_t item_flags,
1565 struct rte_flow_error *error)
1567 const struct rte_flow_item_port_id *spec = item->spec;
1568 const struct rte_flow_item_port_id *mask = item->mask;
1569 const struct rte_flow_item_port_id switch_mask = {
1572 struct mlx5_priv *esw_priv;
1573 struct mlx5_priv *dev_priv;
1576 if (!attr->transfer)
1577 return rte_flow_error_set(error, EINVAL,
1578 RTE_FLOW_ERROR_TYPE_ITEM,
1580 "match on port id is valid only"
1581 " when transfer flag is enabled");
1582 if (item_flags & MLX5_FLOW_ITEM_PORT_ID)
1583 return rte_flow_error_set(error, ENOTSUP,
1584 RTE_FLOW_ERROR_TYPE_ITEM, item,
1585 "multiple source ports are not"
1588 mask = &switch_mask;
1589 if (mask->id != 0xffffffff)
1590 return rte_flow_error_set(error, ENOTSUP,
1591 RTE_FLOW_ERROR_TYPE_ITEM_MASK,
1593 "no support for partial mask on"
1595 ret = mlx5_flow_item_acceptable
1596 (item, (const uint8_t *)mask,
1597 (const uint8_t *)&rte_flow_item_port_id_mask,
1598 sizeof(struct rte_flow_item_port_id),
1604 esw_priv = mlx5_port_to_eswitch_info(spec->id, false);
1606 return rte_flow_error_set(error, rte_errno,
1607 RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
1608 "failed to obtain E-Switch info for"
1610 dev_priv = mlx5_dev_to_eswitch_info(dev);
1612 return rte_flow_error_set(error, rte_errno,
1613 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1615 "failed to obtain E-Switch info");
1616 if (esw_priv->domain_id != dev_priv->domain_id)
1617 return rte_flow_error_set(error, EINVAL,
1618 RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
1619 "cannot match on a port from a"
1620 " different E-Switch");
1625 * Validate GTP item.
1628 * Pointer to the rte_eth_dev structure.
1630 * Item specification.
1631 * @param[in] item_flags
1632 * Bit-fields that holds the items detected until now.
1634 * Pointer to error structure.
1637 * 0 on success, a negative errno value otherwise and rte_errno is set.
1640 flow_dv_validate_item_gtp(struct rte_eth_dev *dev,
1641 const struct rte_flow_item *item,
1642 uint64_t item_flags,
1643 struct rte_flow_error *error)
1645 struct mlx5_priv *priv = dev->data->dev_private;
1646 const struct rte_flow_item_gtp *mask = item->mask;
1647 const struct rte_flow_item_gtp nic_mask = {
1649 .teid = RTE_BE32(0xffffffff),
1652 if (!priv->config.hca_attr.tunnel_stateless_gtp)
1653 return rte_flow_error_set(error, ENOTSUP,
1654 RTE_FLOW_ERROR_TYPE_ITEM, item,
1655 "GTP support is not enabled");
1656 if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1657 return rte_flow_error_set(error, ENOTSUP,
1658 RTE_FLOW_ERROR_TYPE_ITEM, item,
1659 "multiple tunnel layers not"
1661 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1662 return rte_flow_error_set(error, EINVAL,
1663 RTE_FLOW_ERROR_TYPE_ITEM, item,
1664 "no outer UDP layer found");
1666 mask = &rte_flow_item_gtp_mask;
1667 return mlx5_flow_item_acceptable
1668 (item, (const uint8_t *)mask,
1669 (const uint8_t *)&nic_mask,
1670 sizeof(struct rte_flow_item_gtp),
1675 * Validate the pop VLAN action.
1678 * Pointer to the rte_eth_dev structure.
1679 * @param[in] action_flags
1680 * Holds the actions detected until now.
1682 * Pointer to the pop vlan action.
1683 * @param[in] item_flags
1684 * The items found in this flow rule.
1686 * Pointer to flow attributes.
1688 * Pointer to error structure.
1691 * 0 on success, a negative errno value otherwise and rte_errno is set.
1694 flow_dv_validate_action_pop_vlan(struct rte_eth_dev *dev,
1695 uint64_t action_flags,
1696 const struct rte_flow_action *action,
1697 uint64_t item_flags,
1698 const struct rte_flow_attr *attr,
1699 struct rte_flow_error *error)
1701 const struct mlx5_priv *priv = dev->data->dev_private;
1705 if (!priv->sh->pop_vlan_action)
1706 return rte_flow_error_set(error, ENOTSUP,
1707 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1709 "pop vlan action is not supported");
1711 return rte_flow_error_set(error, ENOTSUP,
1712 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
1714 "pop vlan action not supported for "
1716 if (action_flags & MLX5_FLOW_VLAN_ACTIONS)
1717 return rte_flow_error_set(error, ENOTSUP,
1718 RTE_FLOW_ERROR_TYPE_ACTION, action,
1719 "no support for multiple VLAN "
1721 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
1722 return rte_flow_error_set(error, ENOTSUP,
1723 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1725 "cannot pop vlan without a "
1726 "match on (outer) vlan in the flow");
1727 if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1728 return rte_flow_error_set(error, EINVAL,
1729 RTE_FLOW_ERROR_TYPE_ACTION, action,
1730 "wrong action order, port_id should "
1731 "be after pop VLAN action");
1732 if (!attr->transfer && priv->representor)
1733 return rte_flow_error_set(error, ENOTSUP,
1734 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1735 "pop vlan action for VF representor "
1736 "not supported on NIC table");
1741 * Get VLAN default info from vlan match info.
1744 * the list of item specifications.
1746 * pointer VLAN info to fill to.
1749 * 0 on success, a negative errno value otherwise and rte_errno is set.
1752 flow_dev_get_vlan_info_from_items(const struct rte_flow_item *items,
1753 struct rte_vlan_hdr *vlan)
1755 const struct rte_flow_item_vlan nic_mask = {
1756 .tci = RTE_BE16(MLX5DV_FLOW_VLAN_PCP_MASK |
1757 MLX5DV_FLOW_VLAN_VID_MASK),
1758 .inner_type = RTE_BE16(0xffff),
1763 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1764 int type = items->type;
1766 if (type == RTE_FLOW_ITEM_TYPE_VLAN ||
1767 type == MLX5_RTE_FLOW_ITEM_TYPE_VLAN)
1770 if (items->type != RTE_FLOW_ITEM_TYPE_END) {
1771 const struct rte_flow_item_vlan *vlan_m = items->mask;
1772 const struct rte_flow_item_vlan *vlan_v = items->spec;
1776 /* Only full match values are accepted */
1777 if ((vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) ==
1778 MLX5DV_FLOW_VLAN_PCP_MASK_BE) {
1779 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_PCP_MASK;
1781 rte_be_to_cpu_16(vlan_v->tci &
1782 MLX5DV_FLOW_VLAN_PCP_MASK_BE);
1784 if ((vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) ==
1785 MLX5DV_FLOW_VLAN_VID_MASK_BE) {
1786 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_VID_MASK;
1788 rte_be_to_cpu_16(vlan_v->tci &
1789 MLX5DV_FLOW_VLAN_VID_MASK_BE);
1791 if (vlan_m->inner_type == nic_mask.inner_type)
1792 vlan->eth_proto = rte_be_to_cpu_16(vlan_v->inner_type &
1793 vlan_m->inner_type);
1798 * Validate the push VLAN action.
1801 * Pointer to the rte_eth_dev structure.
1802 * @param[in] action_flags
1803 * Holds the actions detected until now.
1804 * @param[in] item_flags
1805 * The items found in this flow rule.
1807 * Pointer to the action structure.
1809 * Pointer to flow attributes
1811 * Pointer to error structure.
1814 * 0 on success, a negative errno value otherwise and rte_errno is set.
1817 flow_dv_validate_action_push_vlan(struct rte_eth_dev *dev,
1818 uint64_t action_flags,
1819 uint64_t item_flags __rte_unused,
1820 const struct rte_flow_action *action,
1821 const struct rte_flow_attr *attr,
1822 struct rte_flow_error *error)
1824 const struct rte_flow_action_of_push_vlan *push_vlan = action->conf;
1825 const struct mlx5_priv *priv = dev->data->dev_private;
1827 if (!attr->transfer && attr->ingress)
1828 return rte_flow_error_set(error, ENOTSUP,
1829 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1831 "push VLAN action not supported for "
1833 if (push_vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_VLAN) &&
1834 push_vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_QINQ))
1835 return rte_flow_error_set(error, EINVAL,
1836 RTE_FLOW_ERROR_TYPE_ACTION, action,
1837 "invalid vlan ethertype");
1838 if (action_flags & MLX5_FLOW_VLAN_ACTIONS)
1839 return rte_flow_error_set(error, ENOTSUP,
1840 RTE_FLOW_ERROR_TYPE_ACTION, action,
1841 "no support for multiple VLAN "
1843 if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1844 return rte_flow_error_set(error, EINVAL,
1845 RTE_FLOW_ERROR_TYPE_ACTION, action,
1846 "wrong action order, port_id should "
1847 "be after push VLAN");
1848 if (!attr->transfer && priv->representor)
1849 return rte_flow_error_set(error, ENOTSUP,
1850 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1851 "push vlan action for VF representor "
1852 "not supported on NIC table");
1858 * Validate the set VLAN PCP.
1860 * @param[in] action_flags
1861 * Holds the actions detected until now.
1862 * @param[in] actions
1863 * Pointer to the list of actions remaining in the flow rule.
1865 * Pointer to error structure.
1868 * 0 on success, a negative errno value otherwise and rte_errno is set.
1871 flow_dv_validate_action_set_vlan_pcp(uint64_t action_flags,
1872 const struct rte_flow_action actions[],
1873 struct rte_flow_error *error)
1875 const struct rte_flow_action *action = actions;
1876 const struct rte_flow_action_of_set_vlan_pcp *conf = action->conf;
1878 if (conf->vlan_pcp > 7)
1879 return rte_flow_error_set(error, EINVAL,
1880 RTE_FLOW_ERROR_TYPE_ACTION, action,
1881 "VLAN PCP value is too big");
1882 if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN))
1883 return rte_flow_error_set(error, ENOTSUP,
1884 RTE_FLOW_ERROR_TYPE_ACTION, action,
1885 "set VLAN PCP action must follow "
1886 "the push VLAN action");
1887 if (action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_PCP)
1888 return rte_flow_error_set(error, ENOTSUP,
1889 RTE_FLOW_ERROR_TYPE_ACTION, action,
1890 "Multiple VLAN PCP modification are "
1892 if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1893 return rte_flow_error_set(error, EINVAL,
1894 RTE_FLOW_ERROR_TYPE_ACTION, action,
1895 "wrong action order, port_id should "
1896 "be after set VLAN PCP");
1901 * Validate the set VLAN VID.
1903 * @param[in] item_flags
1904 * Holds the items detected in this rule.
1905 * @param[in] action_flags
1906 * Holds the actions detected until now.
1907 * @param[in] actions
1908 * Pointer to the list of actions remaining in the flow rule.
1910 * Pointer to error structure.
1913 * 0 on success, a negative errno value otherwise and rte_errno is set.
1916 flow_dv_validate_action_set_vlan_vid(uint64_t item_flags,
1917 uint64_t action_flags,
1918 const struct rte_flow_action actions[],
1919 struct rte_flow_error *error)
1921 const struct rte_flow_action *action = actions;
1922 const struct rte_flow_action_of_set_vlan_vid *conf = action->conf;
1924 if (conf->vlan_vid > RTE_BE16(0xFFE))
1925 return rte_flow_error_set(error, EINVAL,
1926 RTE_FLOW_ERROR_TYPE_ACTION, action,
1927 "VLAN VID value is too big");
1928 if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN) &&
1929 !(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
1930 return rte_flow_error_set(error, ENOTSUP,
1931 RTE_FLOW_ERROR_TYPE_ACTION, action,
1932 "set VLAN VID action must follow push"
1933 " VLAN action or match on VLAN item");
1934 if (action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_VID)
1935 return rte_flow_error_set(error, ENOTSUP,
1936 RTE_FLOW_ERROR_TYPE_ACTION, action,
1937 "Multiple VLAN VID modifications are "
1939 if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1940 return rte_flow_error_set(error, EINVAL,
1941 RTE_FLOW_ERROR_TYPE_ACTION, action,
1942 "wrong action order, port_id should "
1943 "be after set VLAN VID");
1948 * Validate the FLAG action.
1951 * Pointer to the rte_eth_dev structure.
1952 * @param[in] action_flags
1953 * Holds the actions detected until now.
1955 * Pointer to flow attributes
1957 * Pointer to error structure.
1960 * 0 on success, a negative errno value otherwise and rte_errno is set.
1963 flow_dv_validate_action_flag(struct rte_eth_dev *dev,
1964 uint64_t action_flags,
1965 const struct rte_flow_attr *attr,
1966 struct rte_flow_error *error)
1968 struct mlx5_priv *priv = dev->data->dev_private;
1969 struct mlx5_dev_config *config = &priv->config;
1972 /* Fall back if no extended metadata register support. */
1973 if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
1974 return mlx5_flow_validate_action_flag(action_flags, attr,
1976 /* Extensive metadata mode requires registers. */
1977 if (!mlx5_flow_ext_mreg_supported(dev))
1978 return rte_flow_error_set(error, ENOTSUP,
1979 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1980 "no metadata registers "
1981 "to support flag action");
1982 if (!(priv->sh->dv_mark_mask & MLX5_FLOW_MARK_DEFAULT))
1983 return rte_flow_error_set(error, ENOTSUP,
1984 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1985 "extended metadata register"
1986 " isn't available");
1987 ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1990 MLX5_ASSERT(ret > 0);
1991 if (action_flags & MLX5_FLOW_ACTION_MARK)
1992 return rte_flow_error_set(error, EINVAL,
1993 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1994 "can't mark and flag in same flow");
1995 if (action_flags & MLX5_FLOW_ACTION_FLAG)
1996 return rte_flow_error_set(error, EINVAL,
1997 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1999 " actions in same flow");
2004 * Validate MARK action.
2007 * Pointer to the rte_eth_dev structure.
2009 * Pointer to action.
2010 * @param[in] action_flags
2011 * Holds the actions detected until now.
2013 * Pointer to flow attributes
2015 * Pointer to error structure.
2018 * 0 on success, a negative errno value otherwise and rte_errno is set.
2021 flow_dv_validate_action_mark(struct rte_eth_dev *dev,
2022 const struct rte_flow_action *action,
2023 uint64_t action_flags,
2024 const struct rte_flow_attr *attr,
2025 struct rte_flow_error *error)
2027 struct mlx5_priv *priv = dev->data->dev_private;
2028 struct mlx5_dev_config *config = &priv->config;
2029 const struct rte_flow_action_mark *mark = action->conf;
2032 /* Fall back if no extended metadata register support. */
2033 if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
2034 return mlx5_flow_validate_action_mark(action, action_flags,
2036 /* Extensive metadata mode requires registers. */
2037 if (!mlx5_flow_ext_mreg_supported(dev))
2038 return rte_flow_error_set(error, ENOTSUP,
2039 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2040 "no metadata registers "
2041 "to support mark action");
2042 if (!priv->sh->dv_mark_mask)
2043 return rte_flow_error_set(error, ENOTSUP,
2044 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2045 "extended metadata register"
2046 " isn't available");
2047 ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
2050 MLX5_ASSERT(ret > 0);
2052 return rte_flow_error_set(error, EINVAL,
2053 RTE_FLOW_ERROR_TYPE_ACTION, action,
2054 "configuration cannot be null");
2055 if (mark->id >= (MLX5_FLOW_MARK_MAX & priv->sh->dv_mark_mask))
2056 return rte_flow_error_set(error, EINVAL,
2057 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2059 "mark id exceeds the limit");
2060 if (action_flags & MLX5_FLOW_ACTION_FLAG)
2061 return rte_flow_error_set(error, EINVAL,
2062 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2063 "can't flag and mark in same flow");
2064 if (action_flags & MLX5_FLOW_ACTION_MARK)
2065 return rte_flow_error_set(error, EINVAL,
2066 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2067 "can't have 2 mark actions in same"
2073 * Validate SET_META action.
2076 * Pointer to the rte_eth_dev structure.
2078 * Pointer to the action structure.
2079 * @param[in] action_flags
2080 * Holds the actions detected until now.
2082 * Pointer to flow attributes
2084 * Pointer to error structure.
2087 * 0 on success, a negative errno value otherwise and rte_errno is set.
2090 flow_dv_validate_action_set_meta(struct rte_eth_dev *dev,
2091 const struct rte_flow_action *action,
2092 uint64_t action_flags __rte_unused,
2093 const struct rte_flow_attr *attr,
2094 struct rte_flow_error *error)
2096 const struct rte_flow_action_set_meta *conf;
2097 uint32_t nic_mask = UINT32_MAX;
2100 if (!mlx5_flow_ext_mreg_supported(dev))
2101 return rte_flow_error_set(error, ENOTSUP,
2102 RTE_FLOW_ERROR_TYPE_ACTION, action,
2103 "extended metadata register"
2104 " isn't supported");
2105 reg = flow_dv_get_metadata_reg(dev, attr, error);
2108 if (reg != REG_A && reg != REG_B) {
2109 struct mlx5_priv *priv = dev->data->dev_private;
2111 nic_mask = priv->sh->dv_meta_mask;
2113 if (!(action->conf))
2114 return rte_flow_error_set(error, EINVAL,
2115 RTE_FLOW_ERROR_TYPE_ACTION, action,
2116 "configuration cannot be null");
2117 conf = (const struct rte_flow_action_set_meta *)action->conf;
2119 return rte_flow_error_set(error, EINVAL,
2120 RTE_FLOW_ERROR_TYPE_ACTION, action,
2121 "zero mask doesn't have any effect");
2122 if (conf->mask & ~nic_mask)
2123 return rte_flow_error_set(error, EINVAL,
2124 RTE_FLOW_ERROR_TYPE_ACTION, action,
2125 "meta data must be within reg C0");
2130 * Validate SET_TAG action.
2133 * Pointer to the rte_eth_dev structure.
2135 * Pointer to the action structure.
2136 * @param[in] action_flags
2137 * Holds the actions detected until now.
2139 * Pointer to flow attributes
2141 * Pointer to error structure.
2144 * 0 on success, a negative errno value otherwise and rte_errno is set.
2147 flow_dv_validate_action_set_tag(struct rte_eth_dev *dev,
2148 const struct rte_flow_action *action,
2149 uint64_t action_flags,
2150 const struct rte_flow_attr *attr,
2151 struct rte_flow_error *error)
2153 const struct rte_flow_action_set_tag *conf;
2154 const uint64_t terminal_action_flags =
2155 MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_QUEUE |
2156 MLX5_FLOW_ACTION_RSS;
2159 if (!mlx5_flow_ext_mreg_supported(dev))
2160 return rte_flow_error_set(error, ENOTSUP,
2161 RTE_FLOW_ERROR_TYPE_ACTION, action,
2162 "extensive metadata register"
2163 " isn't supported");
2164 if (!(action->conf))
2165 return rte_flow_error_set(error, EINVAL,
2166 RTE_FLOW_ERROR_TYPE_ACTION, action,
2167 "configuration cannot be null");
2168 conf = (const struct rte_flow_action_set_tag *)action->conf;
2170 return rte_flow_error_set(error, EINVAL,
2171 RTE_FLOW_ERROR_TYPE_ACTION, action,
2172 "zero mask doesn't have any effect");
2173 ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, conf->index, error);
2176 if (!attr->transfer && attr->ingress &&
2177 (action_flags & terminal_action_flags))
2178 return rte_flow_error_set(error, EINVAL,
2179 RTE_FLOW_ERROR_TYPE_ACTION, action,
2180 "set_tag has no effect"
2181 " with terminal actions");
2186 * Validate count action.
2189 * Pointer to rte_eth_dev structure.
2191 * Pointer to error structure.
2194 * 0 on success, a negative errno value otherwise and rte_errno is set.
2197 flow_dv_validate_action_count(struct rte_eth_dev *dev,
2198 struct rte_flow_error *error)
2200 struct mlx5_priv *priv = dev->data->dev_private;
2202 if (!priv->config.devx)
2204 #ifdef HAVE_IBV_FLOW_DEVX_COUNTERS
2208 return rte_flow_error_set
2210 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2212 "count action not supported");
2216 * Validate the L2 encap action.
2219 * Pointer to the rte_eth_dev structure.
2220 * @param[in] action_flags
2221 * Holds the actions detected until now.
2223 * Pointer to the action structure.
2225 * Pointer to flow attributes.
2227 * Pointer to error structure.
2230 * 0 on success, a negative errno value otherwise and rte_errno is set.
2233 flow_dv_validate_action_l2_encap(struct rte_eth_dev *dev,
2234 uint64_t action_flags,
2235 const struct rte_flow_action *action,
2236 const struct rte_flow_attr *attr,
2237 struct rte_flow_error *error)
2239 const struct mlx5_priv *priv = dev->data->dev_private;
2241 if (!(action->conf))
2242 return rte_flow_error_set(error, EINVAL,
2243 RTE_FLOW_ERROR_TYPE_ACTION, action,
2244 "configuration cannot be null");
2245 if (action_flags & MLX5_FLOW_ACTION_ENCAP)
2246 return rte_flow_error_set(error, EINVAL,
2247 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2248 "can only have a single encap action "
2250 if (!attr->transfer && priv->representor)
2251 return rte_flow_error_set(error, ENOTSUP,
2252 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2253 "encap action for VF representor "
2254 "not supported on NIC table");
2259 * Validate a decap action.
2262 * Pointer to the rte_eth_dev structure.
2263 * @param[in] action_flags
2264 * Holds the actions detected until now.
2266 * Pointer to flow attributes
2268 * Pointer to error structure.
2271 * 0 on success, a negative errno value otherwise and rte_errno is set.
2274 flow_dv_validate_action_decap(struct rte_eth_dev *dev,
2275 uint64_t action_flags,
2276 const struct rte_flow_attr *attr,
2277 struct rte_flow_error *error)
2279 const struct mlx5_priv *priv = dev->data->dev_private;
2281 if (action_flags & MLX5_FLOW_XCAP_ACTIONS)
2282 return rte_flow_error_set(error, ENOTSUP,
2283 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2285 MLX5_FLOW_ACTION_DECAP ? "can only "
2286 "have a single decap action" : "decap "
2287 "after encap is not supported");
2288 if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS)
2289 return rte_flow_error_set(error, EINVAL,
2290 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2291 "can't have decap action after"
2294 return rte_flow_error_set(error, ENOTSUP,
2295 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
2297 "decap action not supported for "
2299 if (!attr->transfer && priv->representor)
2300 return rte_flow_error_set(error, ENOTSUP,
2301 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2302 "decap action for VF representor "
2303 "not supported on NIC table");
2307 const struct rte_flow_action_raw_decap empty_decap = {.data = NULL, .size = 0,};
2310 * Validate the raw encap and decap actions.
2313 * Pointer to the rte_eth_dev structure.
2315 * Pointer to the decap action.
2317 * Pointer to the encap action.
2319 * Pointer to flow attributes
2320 * @param[in/out] action_flags
2321 * Holds the actions detected until now.
2322 * @param[out] actions_n
2323 * pointer to the number of actions counter.
2325 * Pointer to error structure.
2328 * 0 on success, a negative errno value otherwise and rte_errno is set.
2331 flow_dv_validate_action_raw_encap_decap
2332 (struct rte_eth_dev *dev,
2333 const struct rte_flow_action_raw_decap *decap,
2334 const struct rte_flow_action_raw_encap *encap,
2335 const struct rte_flow_attr *attr, uint64_t *action_flags,
2336 int *actions_n, struct rte_flow_error *error)
2338 const struct mlx5_priv *priv = dev->data->dev_private;
2341 if (encap && (!encap->size || !encap->data))
2342 return rte_flow_error_set(error, EINVAL,
2343 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2344 "raw encap data cannot be empty");
2345 if (decap && encap) {
2346 if (decap->size <= MLX5_ENCAPSULATION_DECISION_SIZE &&
2347 encap->size > MLX5_ENCAPSULATION_DECISION_SIZE)
2350 else if (encap->size <=
2351 MLX5_ENCAPSULATION_DECISION_SIZE &&
2353 MLX5_ENCAPSULATION_DECISION_SIZE)
2356 else if (encap->size >
2357 MLX5_ENCAPSULATION_DECISION_SIZE &&
2359 MLX5_ENCAPSULATION_DECISION_SIZE)
2360 /* 2 L2 actions: encap and decap. */
2363 return rte_flow_error_set(error,
2365 RTE_FLOW_ERROR_TYPE_ACTION,
2366 NULL, "unsupported too small "
2367 "raw decap and too small raw "
2368 "encap combination");
2371 ret = flow_dv_validate_action_decap(dev, *action_flags, attr,
2375 *action_flags |= MLX5_FLOW_ACTION_DECAP;
2379 if (encap->size <= MLX5_ENCAPSULATION_DECISION_SIZE)
2380 return rte_flow_error_set(error, ENOTSUP,
2381 RTE_FLOW_ERROR_TYPE_ACTION,
2383 "small raw encap size");
2384 if (*action_flags & MLX5_FLOW_ACTION_ENCAP)
2385 return rte_flow_error_set(error, EINVAL,
2386 RTE_FLOW_ERROR_TYPE_ACTION,
2388 "more than one encap action");
2389 if (!attr->transfer && priv->representor)
2390 return rte_flow_error_set
2392 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2393 "encap action for VF representor "
2394 "not supported on NIC table");
2395 *action_flags |= MLX5_FLOW_ACTION_ENCAP;
2402 * Find existing encap/decap resource or create and register a new one.
2404 * @param[in, out] dev
2405 * Pointer to rte_eth_dev structure.
2406 * @param[in, out] resource
2407 * Pointer to encap/decap resource.
2408 * @parm[in, out] dev_flow
2409 * Pointer to the dev_flow.
2411 * pointer to error structure.
2414 * 0 on success otherwise -errno and errno is set.
2417 flow_dv_encap_decap_resource_register
2418 (struct rte_eth_dev *dev,
2419 struct mlx5_flow_dv_encap_decap_resource *resource,
2420 struct mlx5_flow *dev_flow,
2421 struct rte_flow_error *error)
2423 struct mlx5_priv *priv = dev->data->dev_private;
2424 struct mlx5_ibv_shared *sh = priv->sh;
2425 struct mlx5_flow_dv_encap_decap_resource *cache_resource;
2426 struct mlx5dv_dr_domain *domain;
2428 resource->flags = dev_flow->group ? 0 : 1;
2429 if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
2430 domain = sh->fdb_domain;
2431 else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
2432 domain = sh->rx_domain;
2434 domain = sh->tx_domain;
2435 /* Lookup a matching resource from cache. */
2436 LIST_FOREACH(cache_resource, &sh->encaps_decaps, next) {
2437 if (resource->reformat_type == cache_resource->reformat_type &&
2438 resource->ft_type == cache_resource->ft_type &&
2439 resource->flags == cache_resource->flags &&
2440 resource->size == cache_resource->size &&
2441 !memcmp((const void *)resource->buf,
2442 (const void *)cache_resource->buf,
2444 DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d++",
2445 (void *)cache_resource,
2446 rte_atomic32_read(&cache_resource->refcnt));
2447 rte_atomic32_inc(&cache_resource->refcnt);
2448 dev_flow->dv.encap_decap = cache_resource;
2452 /* Register new encap/decap resource. */
2453 cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
2454 if (!cache_resource)
2455 return rte_flow_error_set(error, ENOMEM,
2456 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2457 "cannot allocate resource memory");
2458 *cache_resource = *resource;
2459 cache_resource->verbs_action =
2460 mlx5_glue->dv_create_flow_action_packet_reformat
2461 (sh->ctx, cache_resource->reformat_type,
2462 cache_resource->ft_type, domain, cache_resource->flags,
2463 cache_resource->size,
2464 (cache_resource->size ? cache_resource->buf : NULL));
2465 if (!cache_resource->verbs_action) {
2466 rte_free(cache_resource);
2467 return rte_flow_error_set(error, ENOMEM,
2468 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2469 NULL, "cannot create action");
2471 rte_atomic32_init(&cache_resource->refcnt);
2472 rte_atomic32_inc(&cache_resource->refcnt);
2473 LIST_INSERT_HEAD(&sh->encaps_decaps, cache_resource, next);
2474 dev_flow->dv.encap_decap = cache_resource;
2475 DRV_LOG(DEBUG, "new encap/decap resource %p: refcnt %d++",
2476 (void *)cache_resource,
2477 rte_atomic32_read(&cache_resource->refcnt));
2482 * Find existing table jump resource or create and register a new one.
2484 * @param[in, out] dev
2485 * Pointer to rte_eth_dev structure.
2486 * @param[in, out] tbl
2487 * Pointer to flow table resource.
2488 * @parm[in, out] dev_flow
2489 * Pointer to the dev_flow.
2491 * pointer to error structure.
2494 * 0 on success otherwise -errno and errno is set.
2497 flow_dv_jump_tbl_resource_register
2498 (struct rte_eth_dev *dev __rte_unused,
2499 struct mlx5_flow_tbl_resource *tbl,
2500 struct mlx5_flow *dev_flow,
2501 struct rte_flow_error *error)
2503 struct mlx5_flow_tbl_data_entry *tbl_data =
2504 container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
2508 cnt = rte_atomic32_read(&tbl_data->jump.refcnt);
2510 tbl_data->jump.action =
2511 mlx5_glue->dr_create_flow_action_dest_flow_tbl
2513 if (!tbl_data->jump.action)
2514 return rte_flow_error_set(error, ENOMEM,
2515 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2516 NULL, "cannot create jump action");
2517 DRV_LOG(DEBUG, "new jump table resource %p: refcnt %d++",
2518 (void *)&tbl_data->jump, cnt);
2520 MLX5_ASSERT(tbl_data->jump.action);
2521 DRV_LOG(DEBUG, "existed jump table resource %p: refcnt %d++",
2522 (void *)&tbl_data->jump, cnt);
2524 rte_atomic32_inc(&tbl_data->jump.refcnt);
2525 dev_flow->dv.jump = &tbl_data->jump;
2530 * Find existing table port ID resource or create and register a new one.
2532 * @param[in, out] dev
2533 * Pointer to rte_eth_dev structure.
2534 * @param[in, out] resource
2535 * Pointer to port ID action resource.
2536 * @parm[in, out] dev_flow
2537 * Pointer to the dev_flow.
2539 * pointer to error structure.
2542 * 0 on success otherwise -errno and errno is set.
2545 flow_dv_port_id_action_resource_register
2546 (struct rte_eth_dev *dev,
2547 struct mlx5_flow_dv_port_id_action_resource *resource,
2548 struct mlx5_flow *dev_flow,
2549 struct rte_flow_error *error)
2551 struct mlx5_priv *priv = dev->data->dev_private;
2552 struct mlx5_ibv_shared *sh = priv->sh;
2553 struct mlx5_flow_dv_port_id_action_resource *cache_resource;
2555 /* Lookup a matching resource from cache. */
2556 LIST_FOREACH(cache_resource, &sh->port_id_action_list, next) {
2557 if (resource->port_id == cache_resource->port_id) {
2558 DRV_LOG(DEBUG, "port id action resource resource %p: "
2560 (void *)cache_resource,
2561 rte_atomic32_read(&cache_resource->refcnt));
2562 rte_atomic32_inc(&cache_resource->refcnt);
2563 dev_flow->dv.port_id_action = cache_resource;
2567 /* Register new port id action resource. */
2568 cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
2569 if (!cache_resource)
2570 return rte_flow_error_set(error, ENOMEM,
2571 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2572 "cannot allocate resource memory");
2573 *cache_resource = *resource;
2575 * Depending on rdma_core version the glue routine calls
2576 * either mlx5dv_dr_action_create_dest_ib_port(domain, ibv_port)
2577 * or mlx5dv_dr_action_create_dest_vport(domain, vport_id).
2579 cache_resource->action =
2580 mlx5_glue->dr_create_flow_action_dest_port
2581 (priv->sh->fdb_domain, resource->port_id);
2582 if (!cache_resource->action) {
2583 rte_free(cache_resource);
2584 return rte_flow_error_set(error, ENOMEM,
2585 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2586 NULL, "cannot create action");
2588 rte_atomic32_init(&cache_resource->refcnt);
2589 rte_atomic32_inc(&cache_resource->refcnt);
2590 LIST_INSERT_HEAD(&sh->port_id_action_list, cache_resource, next);
2591 dev_flow->dv.port_id_action = cache_resource;
2592 DRV_LOG(DEBUG, "new port id action resource %p: refcnt %d++",
2593 (void *)cache_resource,
2594 rte_atomic32_read(&cache_resource->refcnt));
2599 * Find existing push vlan resource or create and register a new one.
2601 * @param [in, out] dev
2602 * Pointer to rte_eth_dev structure.
2603 * @param[in, out] resource
2604 * Pointer to port ID action resource.
2605 * @parm[in, out] dev_flow
2606 * Pointer to the dev_flow.
2608 * pointer to error structure.
2611 * 0 on success otherwise -errno and errno is set.
2614 flow_dv_push_vlan_action_resource_register
2615 (struct rte_eth_dev *dev,
2616 struct mlx5_flow_dv_push_vlan_action_resource *resource,
2617 struct mlx5_flow *dev_flow,
2618 struct rte_flow_error *error)
2620 struct mlx5_priv *priv = dev->data->dev_private;
2621 struct mlx5_ibv_shared *sh = priv->sh;
2622 struct mlx5_flow_dv_push_vlan_action_resource *cache_resource;
2623 struct mlx5dv_dr_domain *domain;
2625 /* Lookup a matching resource from cache. */
2626 LIST_FOREACH(cache_resource, &sh->push_vlan_action_list, next) {
2627 if (resource->vlan_tag == cache_resource->vlan_tag &&
2628 resource->ft_type == cache_resource->ft_type) {
2629 DRV_LOG(DEBUG, "push-VLAN action resource resource %p: "
2631 (void *)cache_resource,
2632 rte_atomic32_read(&cache_resource->refcnt));
2633 rte_atomic32_inc(&cache_resource->refcnt);
2634 dev_flow->dv.push_vlan_res = cache_resource;
2638 /* Register new push_vlan action resource. */
2639 cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
2640 if (!cache_resource)
2641 return rte_flow_error_set(error, ENOMEM,
2642 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2643 "cannot allocate resource memory");
2644 *cache_resource = *resource;
2645 if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
2646 domain = sh->fdb_domain;
2647 else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
2648 domain = sh->rx_domain;
2650 domain = sh->tx_domain;
2651 cache_resource->action =
2652 mlx5_glue->dr_create_flow_action_push_vlan(domain,
2653 resource->vlan_tag);
2654 if (!cache_resource->action) {
2655 rte_free(cache_resource);
2656 return rte_flow_error_set(error, ENOMEM,
2657 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2658 NULL, "cannot create action");
2660 rte_atomic32_init(&cache_resource->refcnt);
2661 rte_atomic32_inc(&cache_resource->refcnt);
2662 LIST_INSERT_HEAD(&sh->push_vlan_action_list, cache_resource, next);
2663 dev_flow->dv.push_vlan_res = cache_resource;
2664 DRV_LOG(DEBUG, "new push vlan action resource %p: refcnt %d++",
2665 (void *)cache_resource,
2666 rte_atomic32_read(&cache_resource->refcnt));
2670 * Get the size of specific rte_flow_item_type
2672 * @param[in] item_type
2673 * Tested rte_flow_item_type.
2676 * sizeof struct item_type, 0 if void or irrelevant.
2679 flow_dv_get_item_len(const enum rte_flow_item_type item_type)
2683 switch (item_type) {
2684 case RTE_FLOW_ITEM_TYPE_ETH:
2685 retval = sizeof(struct rte_flow_item_eth);
2687 case RTE_FLOW_ITEM_TYPE_VLAN:
2688 retval = sizeof(struct rte_flow_item_vlan);
2690 case RTE_FLOW_ITEM_TYPE_IPV4:
2691 retval = sizeof(struct rte_flow_item_ipv4);
2693 case RTE_FLOW_ITEM_TYPE_IPV6:
2694 retval = sizeof(struct rte_flow_item_ipv6);
2696 case RTE_FLOW_ITEM_TYPE_UDP:
2697 retval = sizeof(struct rte_flow_item_udp);
2699 case RTE_FLOW_ITEM_TYPE_TCP:
2700 retval = sizeof(struct rte_flow_item_tcp);
2702 case RTE_FLOW_ITEM_TYPE_VXLAN:
2703 retval = sizeof(struct rte_flow_item_vxlan);
2705 case RTE_FLOW_ITEM_TYPE_GRE:
2706 retval = sizeof(struct rte_flow_item_gre);
2708 case RTE_FLOW_ITEM_TYPE_NVGRE:
2709 retval = sizeof(struct rte_flow_item_nvgre);
2711 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
2712 retval = sizeof(struct rte_flow_item_vxlan_gpe);
2714 case RTE_FLOW_ITEM_TYPE_MPLS:
2715 retval = sizeof(struct rte_flow_item_mpls);
2717 case RTE_FLOW_ITEM_TYPE_VOID: /* Fall through. */
2725 #define MLX5_ENCAP_IPV4_VERSION 0x40
2726 #define MLX5_ENCAP_IPV4_IHL_MIN 0x05
2727 #define MLX5_ENCAP_IPV4_TTL_DEF 0x40
2728 #define MLX5_ENCAP_IPV6_VTC_FLOW 0x60000000
2729 #define MLX5_ENCAP_IPV6_HOP_LIMIT 0xff
2730 #define MLX5_ENCAP_VXLAN_FLAGS 0x08000000
2731 #define MLX5_ENCAP_VXLAN_GPE_FLAGS 0x04
2734 * Convert the encap action data from list of rte_flow_item to raw buffer
2737 * Pointer to rte_flow_item objects list.
2739 * Pointer to the output buffer.
2741 * Pointer to the output buffer size.
2743 * Pointer to the error structure.
2746 * 0 on success, a negative errno value otherwise and rte_errno is set.
2749 flow_dv_convert_encap_data(const struct rte_flow_item *items, uint8_t *buf,
2750 size_t *size, struct rte_flow_error *error)
2752 struct rte_ether_hdr *eth = NULL;
2753 struct rte_vlan_hdr *vlan = NULL;
2754 struct rte_ipv4_hdr *ipv4 = NULL;
2755 struct rte_ipv6_hdr *ipv6 = NULL;
2756 struct rte_udp_hdr *udp = NULL;
2757 struct rte_vxlan_hdr *vxlan = NULL;
2758 struct rte_vxlan_gpe_hdr *vxlan_gpe = NULL;
2759 struct rte_gre_hdr *gre = NULL;
2761 size_t temp_size = 0;
2764 return rte_flow_error_set(error, EINVAL,
2765 RTE_FLOW_ERROR_TYPE_ACTION,
2766 NULL, "invalid empty data");
2767 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
2768 len = flow_dv_get_item_len(items->type);
2769 if (len + temp_size > MLX5_ENCAP_MAX_LEN)
2770 return rte_flow_error_set(error, EINVAL,
2771 RTE_FLOW_ERROR_TYPE_ACTION,
2772 (void *)items->type,
2773 "items total size is too big"
2774 " for encap action");
2775 rte_memcpy((void *)&buf[temp_size], items->spec, len);
2776 switch (items->type) {
2777 case RTE_FLOW_ITEM_TYPE_ETH:
2778 eth = (struct rte_ether_hdr *)&buf[temp_size];
2780 case RTE_FLOW_ITEM_TYPE_VLAN:
2781 vlan = (struct rte_vlan_hdr *)&buf[temp_size];
2783 return rte_flow_error_set(error, EINVAL,
2784 RTE_FLOW_ERROR_TYPE_ACTION,
2785 (void *)items->type,
2786 "eth header not found");
2787 if (!eth->ether_type)
2788 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_VLAN);
2790 case RTE_FLOW_ITEM_TYPE_IPV4:
2791 ipv4 = (struct rte_ipv4_hdr *)&buf[temp_size];
2793 return rte_flow_error_set(error, EINVAL,
2794 RTE_FLOW_ERROR_TYPE_ACTION,
2795 (void *)items->type,
2796 "neither eth nor vlan"
2798 if (vlan && !vlan->eth_proto)
2799 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV4);
2800 else if (eth && !eth->ether_type)
2801 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV4);
2802 if (!ipv4->version_ihl)
2803 ipv4->version_ihl = MLX5_ENCAP_IPV4_VERSION |
2804 MLX5_ENCAP_IPV4_IHL_MIN;
2805 if (!ipv4->time_to_live)
2806 ipv4->time_to_live = MLX5_ENCAP_IPV4_TTL_DEF;
2808 case RTE_FLOW_ITEM_TYPE_IPV6:
2809 ipv6 = (struct rte_ipv6_hdr *)&buf[temp_size];
2811 return rte_flow_error_set(error, EINVAL,
2812 RTE_FLOW_ERROR_TYPE_ACTION,
2813 (void *)items->type,
2814 "neither eth nor vlan"
2816 if (vlan && !vlan->eth_proto)
2817 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV6);
2818 else if (eth && !eth->ether_type)
2819 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV6);
2820 if (!ipv6->vtc_flow)
2822 RTE_BE32(MLX5_ENCAP_IPV6_VTC_FLOW);
2823 if (!ipv6->hop_limits)
2824 ipv6->hop_limits = MLX5_ENCAP_IPV6_HOP_LIMIT;
2826 case RTE_FLOW_ITEM_TYPE_UDP:
2827 udp = (struct rte_udp_hdr *)&buf[temp_size];
2829 return rte_flow_error_set(error, EINVAL,
2830 RTE_FLOW_ERROR_TYPE_ACTION,
2831 (void *)items->type,
2832 "ip header not found");
2833 if (ipv4 && !ipv4->next_proto_id)
2834 ipv4->next_proto_id = IPPROTO_UDP;
2835 else if (ipv6 && !ipv6->proto)
2836 ipv6->proto = IPPROTO_UDP;
2838 case RTE_FLOW_ITEM_TYPE_VXLAN:
2839 vxlan = (struct rte_vxlan_hdr *)&buf[temp_size];
2841 return rte_flow_error_set(error, EINVAL,
2842 RTE_FLOW_ERROR_TYPE_ACTION,
2843 (void *)items->type,
2844 "udp header not found");
2846 udp->dst_port = RTE_BE16(MLX5_UDP_PORT_VXLAN);
2847 if (!vxlan->vx_flags)
2849 RTE_BE32(MLX5_ENCAP_VXLAN_FLAGS);
2851 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
2852 vxlan_gpe = (struct rte_vxlan_gpe_hdr *)&buf[temp_size];
2854 return rte_flow_error_set(error, EINVAL,
2855 RTE_FLOW_ERROR_TYPE_ACTION,
2856 (void *)items->type,
2857 "udp header not found");
2858 if (!vxlan_gpe->proto)
2859 return rte_flow_error_set(error, EINVAL,
2860 RTE_FLOW_ERROR_TYPE_ACTION,
2861 (void *)items->type,
2862 "next protocol not found");
2865 RTE_BE16(MLX5_UDP_PORT_VXLAN_GPE);
2866 if (!vxlan_gpe->vx_flags)
2867 vxlan_gpe->vx_flags =
2868 MLX5_ENCAP_VXLAN_GPE_FLAGS;
2870 case RTE_FLOW_ITEM_TYPE_GRE:
2871 case RTE_FLOW_ITEM_TYPE_NVGRE:
2872 gre = (struct rte_gre_hdr *)&buf[temp_size];
2874 return rte_flow_error_set(error, EINVAL,
2875 RTE_FLOW_ERROR_TYPE_ACTION,
2876 (void *)items->type,
2877 "next protocol not found");
2879 return rte_flow_error_set(error, EINVAL,
2880 RTE_FLOW_ERROR_TYPE_ACTION,
2881 (void *)items->type,
2882 "ip header not found");
2883 if (ipv4 && !ipv4->next_proto_id)
2884 ipv4->next_proto_id = IPPROTO_GRE;
2885 else if (ipv6 && !ipv6->proto)
2886 ipv6->proto = IPPROTO_GRE;
2888 case RTE_FLOW_ITEM_TYPE_VOID:
2891 return rte_flow_error_set(error, EINVAL,
2892 RTE_FLOW_ERROR_TYPE_ACTION,
2893 (void *)items->type,
2894 "unsupported item type");
2904 flow_dv_zero_encap_udp_csum(void *data, struct rte_flow_error *error)
2906 struct rte_ether_hdr *eth = NULL;
2907 struct rte_vlan_hdr *vlan = NULL;
2908 struct rte_ipv6_hdr *ipv6 = NULL;
2909 struct rte_udp_hdr *udp = NULL;
2913 eth = (struct rte_ether_hdr *)data;
2914 next_hdr = (char *)(eth + 1);
2915 proto = RTE_BE16(eth->ether_type);
2918 while (proto == RTE_ETHER_TYPE_VLAN || proto == RTE_ETHER_TYPE_QINQ) {
2919 vlan = (struct rte_vlan_hdr *)next_hdr;
2920 proto = RTE_BE16(vlan->eth_proto);
2921 next_hdr += sizeof(struct rte_vlan_hdr);
2924 /* HW calculates IPv4 csum. no need to proceed */
2925 if (proto == RTE_ETHER_TYPE_IPV4)
2928 /* non IPv4/IPv6 header. not supported */
2929 if (proto != RTE_ETHER_TYPE_IPV6) {
2930 return rte_flow_error_set(error, ENOTSUP,
2931 RTE_FLOW_ERROR_TYPE_ACTION,
2932 NULL, "Cannot offload non IPv4/IPv6");
2935 ipv6 = (struct rte_ipv6_hdr *)next_hdr;
2937 /* ignore non UDP */
2938 if (ipv6->proto != IPPROTO_UDP)
2941 udp = (struct rte_udp_hdr *)(ipv6 + 1);
2942 udp->dgram_cksum = 0;
2948 * Convert L2 encap action to DV specification.
2951 * Pointer to rte_eth_dev structure.
2953 * Pointer to action structure.
2954 * @param[in, out] dev_flow
2955 * Pointer to the mlx5_flow.
2956 * @param[in] transfer
2957 * Mark if the flow is E-Switch flow.
2959 * Pointer to the error structure.
2962 * 0 on success, a negative errno value otherwise and rte_errno is set.
2965 flow_dv_create_action_l2_encap(struct rte_eth_dev *dev,
2966 const struct rte_flow_action *action,
2967 struct mlx5_flow *dev_flow,
2969 struct rte_flow_error *error)
2971 const struct rte_flow_item *encap_data;
2972 const struct rte_flow_action_raw_encap *raw_encap_data;
2973 struct mlx5_flow_dv_encap_decap_resource res = {
2975 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL,
2976 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
2977 MLX5DV_FLOW_TABLE_TYPE_NIC_TX,
2980 if (action->type == RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
2982 (const struct rte_flow_action_raw_encap *)action->conf;
2983 res.size = raw_encap_data->size;
2984 memcpy(res.buf, raw_encap_data->data, res.size);
2986 if (action->type == RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP)
2988 ((const struct rte_flow_action_vxlan_encap *)
2989 action->conf)->definition;
2992 ((const struct rte_flow_action_nvgre_encap *)
2993 action->conf)->definition;
2994 if (flow_dv_convert_encap_data(encap_data, res.buf,
2998 if (flow_dv_zero_encap_udp_csum(res.buf, error))
3000 if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
3001 return rte_flow_error_set(error, EINVAL,
3002 RTE_FLOW_ERROR_TYPE_ACTION,
3003 NULL, "can't create L2 encap action");
3008 * Convert L2 decap action to DV specification.
3011 * Pointer to rte_eth_dev structure.
3012 * @param[in, out] dev_flow
3013 * Pointer to the mlx5_flow.
3014 * @param[in] transfer
3015 * Mark if the flow is E-Switch flow.
3017 * Pointer to the error structure.
3020 * 0 on success, a negative errno value otherwise and rte_errno is set.
3023 flow_dv_create_action_l2_decap(struct rte_eth_dev *dev,
3024 struct mlx5_flow *dev_flow,
3026 struct rte_flow_error *error)
3028 struct mlx5_flow_dv_encap_decap_resource res = {
3031 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2,
3032 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
3033 MLX5DV_FLOW_TABLE_TYPE_NIC_RX,
3036 if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
3037 return rte_flow_error_set(error, EINVAL,
3038 RTE_FLOW_ERROR_TYPE_ACTION,
3039 NULL, "can't create L2 decap action");
3044 * Convert raw decap/encap (L3 tunnel) action to DV specification.
3047 * Pointer to rte_eth_dev structure.
3049 * Pointer to action structure.
3050 * @param[in, out] dev_flow
3051 * Pointer to the mlx5_flow.
3053 * Pointer to the flow attributes.
3055 * Pointer to the error structure.
3058 * 0 on success, a negative errno value otherwise and rte_errno is set.
3061 flow_dv_create_action_raw_encap(struct rte_eth_dev *dev,
3062 const struct rte_flow_action *action,
3063 struct mlx5_flow *dev_flow,
3064 const struct rte_flow_attr *attr,
3065 struct rte_flow_error *error)
3067 const struct rte_flow_action_raw_encap *encap_data;
3068 struct mlx5_flow_dv_encap_decap_resource res;
3070 encap_data = (const struct rte_flow_action_raw_encap *)action->conf;
3071 res.size = encap_data->size;
3072 memcpy(res.buf, encap_data->data, res.size);
3073 res.reformat_type = res.size < MLX5_ENCAPSULATION_DECISION_SIZE ?
3074 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L3_TUNNEL_TO_L2 :
3075 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L3_TUNNEL;
3077 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
3079 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
3080 MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
3081 if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
3082 return rte_flow_error_set(error, EINVAL,
3083 RTE_FLOW_ERROR_TYPE_ACTION,
3084 NULL, "can't create encap action");
3089 * Create action push VLAN.
3092 * Pointer to rte_eth_dev structure.
3094 * Pointer to the flow attributes.
3096 * Pointer to the vlan to push to the Ethernet header.
3097 * @param[in, out] dev_flow
3098 * Pointer to the mlx5_flow.
3100 * Pointer to the error structure.
3103 * 0 on success, a negative errno value otherwise and rte_errno is set.
3106 flow_dv_create_action_push_vlan(struct rte_eth_dev *dev,
3107 const struct rte_flow_attr *attr,
3108 const struct rte_vlan_hdr *vlan,
3109 struct mlx5_flow *dev_flow,
3110 struct rte_flow_error *error)
3112 struct mlx5_flow_dv_push_vlan_action_resource res;
3115 rte_cpu_to_be_32(((uint32_t)vlan->eth_proto) << 16 |
3118 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
3120 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
3121 MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
3122 return flow_dv_push_vlan_action_resource_register
3123 (dev, &res, dev_flow, error);
3127 * Validate the modify-header actions.
3129 * @param[in] action_flags
3130 * Holds the actions detected until now.
3132 * Pointer to the modify action.
3134 * Pointer to error structure.
3137 * 0 on success, a negative errno value otherwise and rte_errno is set.
3140 flow_dv_validate_action_modify_hdr(const uint64_t action_flags,
3141 const struct rte_flow_action *action,
3142 struct rte_flow_error *error)
3144 if (action->type != RTE_FLOW_ACTION_TYPE_DEC_TTL && !action->conf)
3145 return rte_flow_error_set(error, EINVAL,
3146 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3147 NULL, "action configuration not set");
3148 if (action_flags & MLX5_FLOW_ACTION_ENCAP)
3149 return rte_flow_error_set(error, EINVAL,
3150 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3151 "can't have encap action before"
3157 * Validate the modify-header MAC address actions.
3159 * @param[in] action_flags
3160 * Holds the actions detected until now.
3162 * Pointer to the modify action.
3163 * @param[in] item_flags
3164 * Holds the items detected.
3166 * Pointer to error structure.
3169 * 0 on success, a negative errno value otherwise and rte_errno is set.
3172 flow_dv_validate_action_modify_mac(const uint64_t action_flags,
3173 const struct rte_flow_action *action,
3174 const uint64_t item_flags,
3175 struct rte_flow_error *error)
3179 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3181 if (!(item_flags & MLX5_FLOW_LAYER_L2))
3182 return rte_flow_error_set(error, EINVAL,
3183 RTE_FLOW_ERROR_TYPE_ACTION,
3185 "no L2 item in pattern");
3191 * Validate the modify-header IPv4 address actions.
3193 * @param[in] action_flags
3194 * Holds the actions detected until now.
3196 * Pointer to the modify action.
3197 * @param[in] item_flags
3198 * Holds the items detected.
3200 * Pointer to error structure.
3203 * 0 on success, a negative errno value otherwise and rte_errno is set.
3206 flow_dv_validate_action_modify_ipv4(const uint64_t action_flags,
3207 const struct rte_flow_action *action,
3208 const uint64_t item_flags,
3209 struct rte_flow_error *error)
3214 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3216 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3217 MLX5_FLOW_LAYER_INNER_L3_IPV4 :
3218 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
3219 if (!(item_flags & layer))
3220 return rte_flow_error_set(error, EINVAL,
3221 RTE_FLOW_ERROR_TYPE_ACTION,
3223 "no ipv4 item in pattern");
3229 * Validate the modify-header IPv6 address actions.
3231 * @param[in] action_flags
3232 * Holds the actions detected until now.
3234 * Pointer to the modify action.
3235 * @param[in] item_flags
3236 * Holds the items detected.
3238 * Pointer to error structure.
3241 * 0 on success, a negative errno value otherwise and rte_errno is set.
3244 flow_dv_validate_action_modify_ipv6(const uint64_t action_flags,
3245 const struct rte_flow_action *action,
3246 const uint64_t item_flags,
3247 struct rte_flow_error *error)
3252 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3254 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3255 MLX5_FLOW_LAYER_INNER_L3_IPV6 :
3256 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
3257 if (!(item_flags & layer))
3258 return rte_flow_error_set(error, EINVAL,
3259 RTE_FLOW_ERROR_TYPE_ACTION,
3261 "no ipv6 item in pattern");
3267 * Validate the modify-header TP actions.
3269 * @param[in] action_flags
3270 * Holds the actions detected until now.
3272 * Pointer to the modify action.
3273 * @param[in] item_flags
3274 * Holds the items detected.
3276 * Pointer to error structure.
3279 * 0 on success, a negative errno value otherwise and rte_errno is set.
3282 flow_dv_validate_action_modify_tp(const uint64_t action_flags,
3283 const struct rte_flow_action *action,
3284 const uint64_t item_flags,
3285 struct rte_flow_error *error)
3290 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3292 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3293 MLX5_FLOW_LAYER_INNER_L4 :
3294 MLX5_FLOW_LAYER_OUTER_L4;
3295 if (!(item_flags & layer))
3296 return rte_flow_error_set(error, EINVAL,
3297 RTE_FLOW_ERROR_TYPE_ACTION,
3298 NULL, "no transport layer "
3305 * Validate the modify-header actions of increment/decrement
3306 * TCP Sequence-number.
3308 * @param[in] action_flags
3309 * Holds the actions detected until now.
3311 * Pointer to the modify action.
3312 * @param[in] item_flags
3313 * Holds the items detected.
3315 * Pointer to error structure.
3318 * 0 on success, a negative errno value otherwise and rte_errno is set.
3321 flow_dv_validate_action_modify_tcp_seq(const uint64_t action_flags,
3322 const struct rte_flow_action *action,
3323 const uint64_t item_flags,
3324 struct rte_flow_error *error)
3329 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3331 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3332 MLX5_FLOW_LAYER_INNER_L4_TCP :
3333 MLX5_FLOW_LAYER_OUTER_L4_TCP;
3334 if (!(item_flags & layer))
3335 return rte_flow_error_set(error, EINVAL,
3336 RTE_FLOW_ERROR_TYPE_ACTION,
3337 NULL, "no TCP item in"
3339 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ &&
3340 (action_flags & MLX5_FLOW_ACTION_DEC_TCP_SEQ)) ||
3341 (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ &&
3342 (action_flags & MLX5_FLOW_ACTION_INC_TCP_SEQ)))
3343 return rte_flow_error_set(error, EINVAL,
3344 RTE_FLOW_ERROR_TYPE_ACTION,
3346 "cannot decrease and increase"
3347 " TCP sequence number"
3348 " at the same time");
3354 * Validate the modify-header actions of increment/decrement
3355 * TCP Acknowledgment number.
3357 * @param[in] action_flags
3358 * Holds the actions detected until now.
3360 * Pointer to the modify action.
3361 * @param[in] item_flags
3362 * Holds the items detected.
3364 * Pointer to error structure.
3367 * 0 on success, a negative errno value otherwise and rte_errno is set.
3370 flow_dv_validate_action_modify_tcp_ack(const uint64_t action_flags,
3371 const struct rte_flow_action *action,
3372 const uint64_t item_flags,
3373 struct rte_flow_error *error)
3378 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3380 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3381 MLX5_FLOW_LAYER_INNER_L4_TCP :
3382 MLX5_FLOW_LAYER_OUTER_L4_TCP;
3383 if (!(item_flags & layer))
3384 return rte_flow_error_set(error, EINVAL,
3385 RTE_FLOW_ERROR_TYPE_ACTION,
3386 NULL, "no TCP item in"
3388 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_ACK &&
3389 (action_flags & MLX5_FLOW_ACTION_DEC_TCP_ACK)) ||
3390 (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK &&
3391 (action_flags & MLX5_FLOW_ACTION_INC_TCP_ACK)))
3392 return rte_flow_error_set(error, EINVAL,
3393 RTE_FLOW_ERROR_TYPE_ACTION,
3395 "cannot decrease and increase"
3396 " TCP acknowledgment number"
3397 " at the same time");
3403 * Validate the modify-header TTL actions.
3405 * @param[in] action_flags
3406 * Holds the actions detected until now.
3408 * Pointer to the modify action.
3409 * @param[in] item_flags
3410 * Holds the items detected.
3412 * Pointer to error structure.
3415 * 0 on success, a negative errno value otherwise and rte_errno is set.
3418 flow_dv_validate_action_modify_ttl(const uint64_t action_flags,
3419 const struct rte_flow_action *action,
3420 const uint64_t item_flags,
3421 struct rte_flow_error *error)
3426 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3428 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3429 MLX5_FLOW_LAYER_INNER_L3 :
3430 MLX5_FLOW_LAYER_OUTER_L3;
3431 if (!(item_flags & layer))
3432 return rte_flow_error_set(error, EINVAL,
3433 RTE_FLOW_ERROR_TYPE_ACTION,
3435 "no IP protocol in pattern");
3441 * Validate jump action.
3444 * Pointer to the jump action.
3445 * @param[in] action_flags
3446 * Holds the actions detected until now.
3447 * @param[in] attributes
3448 * Pointer to flow attributes
3449 * @param[in] external
3450 * Action belongs to flow rule created by request external to PMD.
3452 * Pointer to error structure.
3455 * 0 on success, a negative errno value otherwise and rte_errno is set.
3458 flow_dv_validate_action_jump(const struct rte_flow_action *action,
3459 uint64_t action_flags,
3460 const struct rte_flow_attr *attributes,
3461 bool external, struct rte_flow_error *error)
3463 uint32_t target_group, table;
3466 if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
3467 MLX5_FLOW_FATE_ESWITCH_ACTIONS))
3468 return rte_flow_error_set(error, EINVAL,
3469 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3470 "can't have 2 fate actions in"
3472 if (action_flags & MLX5_FLOW_ACTION_METER)
3473 return rte_flow_error_set(error, ENOTSUP,
3474 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3475 "jump with meter not support");
3477 return rte_flow_error_set(error, EINVAL,
3478 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3479 NULL, "action configuration not set");
3481 ((const struct rte_flow_action_jump *)action->conf)->group;
3482 ret = mlx5_flow_group_to_table(attributes, external, target_group,
3483 true, &table, error);
3486 if (attributes->group == target_group)
3487 return rte_flow_error_set(error, EINVAL,
3488 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3489 "target group must be other than"
3490 " the current flow group");
3495 * Validate the port_id action.
3498 * Pointer to rte_eth_dev structure.
3499 * @param[in] action_flags
3500 * Bit-fields that holds the actions detected until now.
3502 * Port_id RTE action structure.
3504 * Attributes of flow that includes this action.
3506 * Pointer to error structure.
3509 * 0 on success, a negative errno value otherwise and rte_errno is set.
3512 flow_dv_validate_action_port_id(struct rte_eth_dev *dev,
3513 uint64_t action_flags,
3514 const struct rte_flow_action *action,
3515 const struct rte_flow_attr *attr,
3516 struct rte_flow_error *error)
3518 const struct rte_flow_action_port_id *port_id;
3519 struct mlx5_priv *act_priv;
3520 struct mlx5_priv *dev_priv;
3523 if (!attr->transfer)
3524 return rte_flow_error_set(error, ENOTSUP,
3525 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3527 "port id action is valid in transfer"
3529 if (!action || !action->conf)
3530 return rte_flow_error_set(error, ENOTSUP,
3531 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3533 "port id action parameters must be"
3535 if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
3536 MLX5_FLOW_FATE_ESWITCH_ACTIONS))
3537 return rte_flow_error_set(error, EINVAL,
3538 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3539 "can have only one fate actions in"
3541 dev_priv = mlx5_dev_to_eswitch_info(dev);
3543 return rte_flow_error_set(error, rte_errno,
3544 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3546 "failed to obtain E-Switch info");
3547 port_id = action->conf;
3548 port = port_id->original ? dev->data->port_id : port_id->id;
3549 act_priv = mlx5_port_to_eswitch_info(port, false);
3551 return rte_flow_error_set
3553 RTE_FLOW_ERROR_TYPE_ACTION_CONF, port_id,
3554 "failed to obtain E-Switch port id for port");
3555 if (act_priv->domain_id != dev_priv->domain_id)
3556 return rte_flow_error_set
3558 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3559 "port does not belong to"
3560 " E-Switch being configured");
3565 * Get the maximum number of modify header actions.
3568 * Pointer to rte_eth_dev structure.
3570 * Flags bits to check if root level.
3573 * Max number of modify header actions device can support.
3576 flow_dv_modify_hdr_action_max(struct rte_eth_dev *dev, uint64_t flags)
3579 * There's no way to directly query the max cap. Although it has to be
3580 * acquried by iterative trial, it is a safe assumption that more
3581 * actions are supported by FW if extensive metadata register is
3582 * supported. (Only in the root table)
3584 if (!(flags & MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL))
3585 return MLX5_MAX_MODIFY_NUM;
3587 return mlx5_flow_ext_mreg_supported(dev) ?
3588 MLX5_ROOT_TBL_MODIFY_NUM :
3589 MLX5_ROOT_TBL_MODIFY_NUM_NO_MREG;
3593 * Validate the meter action.
3596 * Pointer to rte_eth_dev structure.
3597 * @param[in] action_flags
3598 * Bit-fields that holds the actions detected until now.
3600 * Pointer to the meter action.
3602 * Attributes of flow that includes this action.
3604 * Pointer to error structure.
3607 * 0 on success, a negative errno value otherwise and rte_ernno is set.
3610 mlx5_flow_validate_action_meter(struct rte_eth_dev *dev,
3611 uint64_t action_flags,
3612 const struct rte_flow_action *action,
3613 const struct rte_flow_attr *attr,
3614 struct rte_flow_error *error)
3616 struct mlx5_priv *priv = dev->data->dev_private;
3617 const struct rte_flow_action_meter *am = action->conf;
3618 struct mlx5_flow_meter *fm;
3621 return rte_flow_error_set(error, EINVAL,
3622 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3623 "meter action conf is NULL");
3625 if (action_flags & MLX5_FLOW_ACTION_METER)
3626 return rte_flow_error_set(error, ENOTSUP,
3627 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3628 "meter chaining not support");
3629 if (action_flags & MLX5_FLOW_ACTION_JUMP)
3630 return rte_flow_error_set(error, ENOTSUP,
3631 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3632 "meter with jump not support");
3634 return rte_flow_error_set(error, ENOTSUP,
3635 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3637 "meter action not supported");
3638 fm = mlx5_flow_meter_find(priv, am->mtr_id);
3640 return rte_flow_error_set(error, EINVAL,
3641 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3643 if (fm->ref_cnt && (!(fm->attr.transfer == attr->transfer ||
3644 (!fm->attr.ingress && !attr->ingress && attr->egress) ||
3645 (!fm->attr.egress && !attr->egress && attr->ingress))))
3646 return rte_flow_error_set(error, EINVAL,
3647 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3648 "Flow attributes are either invalid "
3649 "or have a conflict with current "
3650 "meter attributes");
3655 * Validate the modify-header IPv4 DSCP actions.
3657 * @param[in] action_flags
3658 * Holds the actions detected until now.
3660 * Pointer to the modify action.
3661 * @param[in] item_flags
3662 * Holds the items detected.
3664 * Pointer to error structure.
3667 * 0 on success, a negative errno value otherwise and rte_errno is set.
3670 flow_dv_validate_action_modify_ipv4_dscp(const uint64_t action_flags,
3671 const struct rte_flow_action *action,
3672 const uint64_t item_flags,
3673 struct rte_flow_error *error)
3677 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3679 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV4))
3680 return rte_flow_error_set(error, EINVAL,
3681 RTE_FLOW_ERROR_TYPE_ACTION,
3683 "no ipv4 item in pattern");
3689 * Validate the modify-header IPv6 DSCP actions.
3691 * @param[in] action_flags
3692 * Holds the actions detected until now.
3694 * Pointer to the modify action.
3695 * @param[in] item_flags
3696 * Holds the items detected.
3698 * Pointer to error structure.
3701 * 0 on success, a negative errno value otherwise and rte_errno is set.
3704 flow_dv_validate_action_modify_ipv6_dscp(const uint64_t action_flags,
3705 const struct rte_flow_action *action,
3706 const uint64_t item_flags,
3707 struct rte_flow_error *error)
3711 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3713 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV6))
3714 return rte_flow_error_set(error, EINVAL,
3715 RTE_FLOW_ERROR_TYPE_ACTION,
3717 "no ipv6 item in pattern");
3723 * Find existing modify-header resource or create and register a new one.
3725 * @param dev[in, out]
3726 * Pointer to rte_eth_dev structure.
3727 * @param[in, out] resource
3728 * Pointer to modify-header resource.
3729 * @parm[in, out] dev_flow
3730 * Pointer to the dev_flow.
3732 * pointer to error structure.
3735 * 0 on success otherwise -errno and errno is set.
3738 flow_dv_modify_hdr_resource_register
3739 (struct rte_eth_dev *dev,
3740 struct mlx5_flow_dv_modify_hdr_resource *resource,
3741 struct mlx5_flow *dev_flow,
3742 struct rte_flow_error *error)
3744 struct mlx5_priv *priv = dev->data->dev_private;
3745 struct mlx5_ibv_shared *sh = priv->sh;
3746 struct mlx5_flow_dv_modify_hdr_resource *cache_resource;
3747 struct mlx5dv_dr_domain *ns;
3748 uint32_t actions_len;
3751 dev_flow->group ? 0 : MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL;
3752 if (resource->actions_num > flow_dv_modify_hdr_action_max(dev,
3754 return rte_flow_error_set(error, EOVERFLOW,
3755 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3756 "too many modify header items");
3757 if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
3758 ns = sh->fdb_domain;
3759 else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_TX)
3763 /* Lookup a matching resource from cache. */
3764 actions_len = resource->actions_num * sizeof(resource->actions[0]);
3765 LIST_FOREACH(cache_resource, &sh->modify_cmds, next) {
3766 if (resource->ft_type == cache_resource->ft_type &&
3767 resource->actions_num == cache_resource->actions_num &&
3768 resource->flags == cache_resource->flags &&
3769 !memcmp((const void *)resource->actions,
3770 (const void *)cache_resource->actions,
3772 DRV_LOG(DEBUG, "modify-header resource %p: refcnt %d++",
3773 (void *)cache_resource,
3774 rte_atomic32_read(&cache_resource->refcnt));
3775 rte_atomic32_inc(&cache_resource->refcnt);
3776 dev_flow->dv.modify_hdr = cache_resource;
3780 /* Register new modify-header resource. */
3781 cache_resource = rte_calloc(__func__, 1,
3782 sizeof(*cache_resource) + actions_len, 0);
3783 if (!cache_resource)
3784 return rte_flow_error_set(error, ENOMEM,
3785 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3786 "cannot allocate resource memory");
3787 *cache_resource = *resource;
3788 rte_memcpy(cache_resource->actions, resource->actions, actions_len);
3789 cache_resource->verbs_action =
3790 mlx5_glue->dv_create_flow_action_modify_header
3791 (sh->ctx, cache_resource->ft_type, ns,
3792 cache_resource->flags, actions_len,
3793 (uint64_t *)cache_resource->actions);
3794 if (!cache_resource->verbs_action) {
3795 rte_free(cache_resource);
3796 return rte_flow_error_set(error, ENOMEM,
3797 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3798 NULL, "cannot create action");
3800 rte_atomic32_init(&cache_resource->refcnt);
3801 rte_atomic32_inc(&cache_resource->refcnt);
3802 LIST_INSERT_HEAD(&sh->modify_cmds, cache_resource, next);
3803 dev_flow->dv.modify_hdr = cache_resource;
3804 DRV_LOG(DEBUG, "new modify-header resource %p: refcnt %d++",
3805 (void *)cache_resource,
3806 rte_atomic32_read(&cache_resource->refcnt));
3810 #define MLX5_CNT_CONTAINER_RESIZE 64
3813 * Get or create a flow counter.
3816 * Pointer to the Ethernet device structure.
3818 * Indicate if this counter is shared with other flows.
3820 * Counter identifier.
3823 * pointer to flow counter on success, NULL otherwise and rte_errno is set.
3825 static struct mlx5_flow_counter *
3826 flow_dv_counter_alloc_fallback(struct rte_eth_dev *dev, uint32_t shared,
3829 struct mlx5_priv *priv = dev->data->dev_private;
3830 struct mlx5_flow_counter *cnt = NULL;
3831 struct mlx5_devx_obj *dcs = NULL;
3833 if (!priv->config.devx) {
3834 rte_errno = ENOTSUP;
3838 TAILQ_FOREACH(cnt, &priv->sh->cmng.flow_counters, next) {
3839 if (cnt->shared && cnt->id == id) {
3845 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0);
3848 cnt = rte_calloc(__func__, 1, sizeof(*cnt), 0);
3850 claim_zero(mlx5_devx_cmd_destroy(cnt->dcs));
3854 struct mlx5_flow_counter tmpl = {
3860 tmpl.action = mlx5_glue->dv_create_flow_action_counter(dcs->obj, 0);
3862 claim_zero(mlx5_devx_cmd_destroy(cnt->dcs));
3868 TAILQ_INSERT_HEAD(&priv->sh->cmng.flow_counters, cnt, next);
3873 * Release a flow counter.
3876 * Pointer to the Ethernet device structure.
3877 * @param[in] counter
3878 * Pointer to the counter handler.
3881 flow_dv_counter_release_fallback(struct rte_eth_dev *dev,
3882 struct mlx5_flow_counter *counter)
3884 struct mlx5_priv *priv = dev->data->dev_private;
3888 if (--counter->ref_cnt == 0) {
3889 TAILQ_REMOVE(&priv->sh->cmng.flow_counters, counter, next);
3890 claim_zero(mlx5_devx_cmd_destroy(counter->dcs));
3896 * Query a devx flow counter.
3899 * Pointer to the Ethernet device structure.
3901 * Pointer to the flow counter.
3903 * The statistics value of packets.
3905 * The statistics value of bytes.
3908 * 0 on success, otherwise a negative errno value and rte_errno is set.
3911 _flow_dv_query_count_fallback(struct rte_eth_dev *dev __rte_unused,
3912 struct mlx5_flow_counter *cnt, uint64_t *pkts,
3915 return mlx5_devx_cmd_flow_counter_query(cnt->dcs, 0, 0, pkts, bytes,
3920 * Get a pool by a counter.
3923 * Pointer to the counter.
3928 static struct mlx5_flow_counter_pool *
3929 flow_dv_counter_pool_get(struct mlx5_flow_counter *cnt)
3932 cnt -= cnt->dcs->id % MLX5_COUNTERS_PER_POOL;
3933 return (struct mlx5_flow_counter_pool *)cnt - 1;
3939 * Get a pool by devx counter ID.
3942 * Pointer to the counter container.
3944 * The counter devx ID.
3947 * The counter pool pointer if exists, NULL otherwise,
3949 static struct mlx5_flow_counter_pool *
3950 flow_dv_find_pool_by_id(struct mlx5_pools_container *cont, int id)
3952 struct mlx5_flow_counter_pool *pool;
3954 TAILQ_FOREACH(pool, &cont->pool_list, next) {
3955 int base = (pool->min_dcs->id / MLX5_COUNTERS_PER_POOL) *
3956 MLX5_COUNTERS_PER_POOL;
3958 if (id >= base && id < base + MLX5_COUNTERS_PER_POOL)
3965 * Allocate a new memory for the counter values wrapped by all the needed
3969 * Pointer to the Ethernet device structure.
3971 * The raw memory areas - each one for MLX5_COUNTERS_PER_POOL counters.
3974 * The new memory management pointer on success, otherwise NULL and rte_errno
3977 static struct mlx5_counter_stats_mem_mng *
3978 flow_dv_create_counter_stat_mem_mng(struct rte_eth_dev *dev, int raws_n)
3980 struct mlx5_ibv_shared *sh = ((struct mlx5_priv *)
3981 (dev->data->dev_private))->sh;
3982 struct mlx5_devx_mkey_attr mkey_attr;
3983 struct mlx5_counter_stats_mem_mng *mem_mng;
3984 volatile struct flow_counter_stats *raw_data;
3985 int size = (sizeof(struct flow_counter_stats) *
3986 MLX5_COUNTERS_PER_POOL +
3987 sizeof(struct mlx5_counter_stats_raw)) * raws_n +
3988 sizeof(struct mlx5_counter_stats_mem_mng);
3989 uint8_t *mem = rte_calloc(__func__, 1, size, sysconf(_SC_PAGESIZE));
3996 mem_mng = (struct mlx5_counter_stats_mem_mng *)(mem + size) - 1;
3997 size = sizeof(*raw_data) * MLX5_COUNTERS_PER_POOL * raws_n;
3998 mem_mng->umem = mlx5_glue->devx_umem_reg(sh->ctx, mem, size,
3999 IBV_ACCESS_LOCAL_WRITE);
4000 if (!mem_mng->umem) {
4005 mkey_attr.addr = (uintptr_t)mem;
4006 mkey_attr.size = size;
4007 mkey_attr.umem_id = mem_mng->umem->umem_id;
4008 mkey_attr.pd = sh->pdn;
4009 mkey_attr.log_entity_size = 0;
4010 mkey_attr.pg_access = 0;
4011 mkey_attr.klm_array = NULL;
4012 mkey_attr.klm_num = 0;
4013 mkey_attr.relaxed_ordering = 1;
4014 mem_mng->dm = mlx5_devx_cmd_mkey_create(sh->ctx, &mkey_attr);
4016 mlx5_glue->devx_umem_dereg(mem_mng->umem);
4021 mem_mng->raws = (struct mlx5_counter_stats_raw *)(mem + size);
4022 raw_data = (volatile struct flow_counter_stats *)mem;
4023 for (i = 0; i < raws_n; ++i) {
4024 mem_mng->raws[i].mem_mng = mem_mng;
4025 mem_mng->raws[i].data = raw_data + i * MLX5_COUNTERS_PER_POOL;
4027 LIST_INSERT_HEAD(&sh->cmng.mem_mngs, mem_mng, next);
4032 * Resize a counter container.
4035 * Pointer to the Ethernet device structure.
4037 * Whether the pool is for counter that was allocated by batch command.
4040 * The new container pointer on success, otherwise NULL and rte_errno is set.
4042 static struct mlx5_pools_container *
4043 flow_dv_container_resize(struct rte_eth_dev *dev, uint32_t batch)
4045 struct mlx5_priv *priv = dev->data->dev_private;
4046 struct mlx5_pools_container *cont =
4047 MLX5_CNT_CONTAINER(priv->sh, batch, 0);
4048 struct mlx5_pools_container *new_cont =
4049 MLX5_CNT_CONTAINER_UNUSED(priv->sh, batch, 0);
4050 struct mlx5_counter_stats_mem_mng *mem_mng;
4051 uint32_t resize = cont->n + MLX5_CNT_CONTAINER_RESIZE;
4052 uint32_t mem_size = sizeof(struct mlx5_flow_counter_pool *) * resize;
4055 if (cont != MLX5_CNT_CONTAINER(priv->sh, batch, 1)) {
4056 /* The last resize still hasn't detected by the host thread. */
4060 new_cont->pools = rte_calloc(__func__, 1, mem_size, 0);
4061 if (!new_cont->pools) {
4066 memcpy(new_cont->pools, cont->pools, cont->n *
4067 sizeof(struct mlx5_flow_counter_pool *));
4068 mem_mng = flow_dv_create_counter_stat_mem_mng(dev,
4069 MLX5_CNT_CONTAINER_RESIZE + MLX5_MAX_PENDING_QUERIES);
4071 rte_free(new_cont->pools);
4074 for (i = 0; i < MLX5_MAX_PENDING_QUERIES; ++i)
4075 LIST_INSERT_HEAD(&priv->sh->cmng.free_stat_raws,
4076 mem_mng->raws + MLX5_CNT_CONTAINER_RESIZE +
4078 new_cont->n = resize;
4079 rte_atomic16_set(&new_cont->n_valid, rte_atomic16_read(&cont->n_valid));
4080 TAILQ_INIT(&new_cont->pool_list);
4081 TAILQ_CONCAT(&new_cont->pool_list, &cont->pool_list, next);
4082 new_cont->init_mem_mng = mem_mng;
4084 /* Flip the master container. */
4085 priv->sh->cmng.mhi[batch] ^= (uint8_t)1;
4090 * Query a devx flow counter.
4093 * Pointer to the Ethernet device structure.
4095 * Pointer to the flow counter.
4097 * The statistics value of packets.
4099 * The statistics value of bytes.
4102 * 0 on success, otherwise a negative errno value and rte_errno is set.
4105 _flow_dv_query_count(struct rte_eth_dev *dev,
4106 struct mlx5_flow_counter *cnt, uint64_t *pkts,
4109 struct mlx5_priv *priv = dev->data->dev_private;
4110 struct mlx5_flow_counter_pool *pool =
4111 flow_dv_counter_pool_get(cnt);
4112 int offset = cnt - &pool->counters_raw[0];
4114 if (priv->counter_fallback)
4115 return _flow_dv_query_count_fallback(dev, cnt, pkts, bytes);
4117 rte_spinlock_lock(&pool->sl);
4119 * The single counters allocation may allocate smaller ID than the
4120 * current allocated in parallel to the host reading.
4121 * In this case the new counter values must be reported as 0.
4123 if (unlikely(!cnt->batch && cnt->dcs->id < pool->raw->min_dcs_id)) {
4127 *pkts = rte_be_to_cpu_64(pool->raw->data[offset].hits);
4128 *bytes = rte_be_to_cpu_64(pool->raw->data[offset].bytes);
4130 rte_spinlock_unlock(&pool->sl);
4135 * Create and initialize a new counter pool.
4138 * Pointer to the Ethernet device structure.
4140 * The devX counter handle.
4142 * Whether the pool is for counter that was allocated by batch command.
4145 * A new pool pointer on success, NULL otherwise and rte_errno is set.
4147 static struct mlx5_flow_counter_pool *
4148 flow_dv_pool_create(struct rte_eth_dev *dev, struct mlx5_devx_obj *dcs,
4151 struct mlx5_priv *priv = dev->data->dev_private;
4152 struct mlx5_flow_counter_pool *pool;
4153 struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(priv->sh, batch,
4155 int16_t n_valid = rte_atomic16_read(&cont->n_valid);
4158 if (cont->n == n_valid) {
4159 cont = flow_dv_container_resize(dev, batch);
4163 size = sizeof(*pool) + MLX5_COUNTERS_PER_POOL *
4164 sizeof(struct mlx5_flow_counter);
4165 pool = rte_calloc(__func__, 1, size, 0);
4170 pool->min_dcs = dcs;
4171 pool->raw = cont->init_mem_mng->raws + n_valid %
4172 MLX5_CNT_CONTAINER_RESIZE;
4173 pool->raw_hw = NULL;
4174 rte_spinlock_init(&pool->sl);
4176 * The generation of the new allocated counters in this pool is 0, 2 in
4177 * the pool generation makes all the counters valid for allocation.
4179 rte_atomic64_set(&pool->query_gen, 0x2);
4180 TAILQ_INIT(&pool->counters);
4181 TAILQ_INSERT_TAIL(&cont->pool_list, pool, next);
4182 cont->pools[n_valid] = pool;
4183 /* Pool initialization must be updated before host thread access. */
4185 rte_atomic16_add(&cont->n_valid, 1);
4190 * Prepare a new counter and/or a new counter pool.
4193 * Pointer to the Ethernet device structure.
4194 * @param[out] cnt_free
4195 * Where to put the pointer of a new counter.
4197 * Whether the pool is for counter that was allocated by batch command.
4200 * The free counter pool pointer and @p cnt_free is set on success,
4201 * NULL otherwise and rte_errno is set.
4203 static struct mlx5_flow_counter_pool *
4204 flow_dv_counter_pool_prepare(struct rte_eth_dev *dev,
4205 struct mlx5_flow_counter **cnt_free,
4208 struct mlx5_priv *priv = dev->data->dev_private;
4209 struct mlx5_flow_counter_pool *pool;
4210 struct mlx5_devx_obj *dcs = NULL;
4211 struct mlx5_flow_counter *cnt;
4215 /* bulk_bitmap must be 0 for single counter allocation. */
4216 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0);
4219 pool = flow_dv_find_pool_by_id
4220 (MLX5_CNT_CONTAINER(priv->sh, batch, 0), dcs->id);
4222 pool = flow_dv_pool_create(dev, dcs, batch);
4224 mlx5_devx_cmd_destroy(dcs);
4227 } else if (dcs->id < pool->min_dcs->id) {
4228 rte_atomic64_set(&pool->a64_dcs,
4229 (int64_t)(uintptr_t)dcs);
4231 cnt = &pool->counters_raw[dcs->id % MLX5_COUNTERS_PER_POOL];
4232 TAILQ_INSERT_HEAD(&pool->counters, cnt, next);
4237 /* bulk_bitmap is in 128 counters units. */
4238 if (priv->config.hca_attr.flow_counter_bulk_alloc_bitmap & 0x4)
4239 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0x4);
4241 rte_errno = ENODATA;
4244 pool = flow_dv_pool_create(dev, dcs, batch);
4246 mlx5_devx_cmd_destroy(dcs);
4249 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
4250 cnt = &pool->counters_raw[i];
4252 TAILQ_INSERT_HEAD(&pool->counters, cnt, next);
4254 *cnt_free = &pool->counters_raw[0];
4259 * Search for existed shared counter.
4262 * Pointer to the relevant counter pool container.
4264 * The shared counter ID to search.
4267 * NULL if not existed, otherwise pointer to the shared counter.
4269 static struct mlx5_flow_counter *
4270 flow_dv_counter_shared_search(struct mlx5_pools_container *cont,
4273 static struct mlx5_flow_counter *cnt;
4274 struct mlx5_flow_counter_pool *pool;
4277 TAILQ_FOREACH(pool, &cont->pool_list, next) {
4278 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
4279 cnt = &pool->counters_raw[i];
4280 if (cnt->ref_cnt && cnt->shared && cnt->id == id)
4288 * Allocate a flow counter.
4291 * Pointer to the Ethernet device structure.
4293 * Indicate if this counter is shared with other flows.
4295 * Counter identifier.
4297 * Counter flow group.
4300 * pointer to flow counter on success, NULL otherwise and rte_errno is set.
4302 static struct mlx5_flow_counter *
4303 flow_dv_counter_alloc(struct rte_eth_dev *dev, uint32_t shared, uint32_t id,
4306 struct mlx5_priv *priv = dev->data->dev_private;
4307 struct mlx5_flow_counter_pool *pool = NULL;
4308 struct mlx5_flow_counter *cnt_free = NULL;
4310 * Currently group 0 flow counter cannot be assigned to a flow if it is
4311 * not the first one in the batch counter allocation, so it is better
4312 * to allocate counters one by one for these flows in a separate
4314 * A counter can be shared between different groups so need to take
4315 * shared counters from the single container.
4317 uint32_t batch = (group && !shared) ? 1 : 0;
4318 struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(priv->sh, batch,
4321 if (priv->counter_fallback)
4322 return flow_dv_counter_alloc_fallback(dev, shared, id);
4323 if (!priv->config.devx) {
4324 rte_errno = ENOTSUP;
4328 cnt_free = flow_dv_counter_shared_search(cont, id);
4330 if (cnt_free->ref_cnt + 1 == 0) {
4334 cnt_free->ref_cnt++;
4338 /* Pools which has a free counters are in the start. */
4339 TAILQ_FOREACH(pool, &cont->pool_list, next) {
4341 * The free counter reset values must be updated between the
4342 * counter release to the counter allocation, so, at least one
4343 * query must be done in this time. ensure it by saving the
4344 * query generation in the release time.
4345 * The free list is sorted according to the generation - so if
4346 * the first one is not updated, all the others are not
4349 cnt_free = TAILQ_FIRST(&pool->counters);
4350 if (cnt_free && cnt_free->query_gen + 1 <
4351 rte_atomic64_read(&pool->query_gen))
4356 pool = flow_dv_counter_pool_prepare(dev, &cnt_free, batch);
4360 cnt_free->batch = batch;
4361 /* Create a DV counter action only in the first time usage. */
4362 if (!cnt_free->action) {
4364 struct mlx5_devx_obj *dcs;
4367 offset = cnt_free - &pool->counters_raw[0];
4368 dcs = pool->min_dcs;
4371 dcs = cnt_free->dcs;
4373 cnt_free->action = mlx5_glue->dv_create_flow_action_counter
4375 if (!cnt_free->action) {
4380 /* Update the counter reset values. */
4381 if (_flow_dv_query_count(dev, cnt_free, &cnt_free->hits,
4384 cnt_free->shared = shared;
4385 cnt_free->ref_cnt = 1;
4387 if (!priv->sh->cmng.query_thread_on)
4388 /* Start the asynchronous batch query by the host thread. */
4389 mlx5_set_query_alarm(priv->sh);
4390 TAILQ_REMOVE(&pool->counters, cnt_free, next);
4391 if (TAILQ_EMPTY(&pool->counters)) {
4392 /* Move the pool to the end of the container pool list. */
4393 TAILQ_REMOVE(&cont->pool_list, pool, next);
4394 TAILQ_INSERT_TAIL(&cont->pool_list, pool, next);
4400 * Release a flow counter.
4403 * Pointer to the Ethernet device structure.
4404 * @param[in] counter
4405 * Pointer to the counter handler.
4408 flow_dv_counter_release(struct rte_eth_dev *dev,
4409 struct mlx5_flow_counter *counter)
4411 struct mlx5_priv *priv = dev->data->dev_private;
4415 if (priv->counter_fallback) {
4416 flow_dv_counter_release_fallback(dev, counter);
4419 if (--counter->ref_cnt == 0) {
4420 struct mlx5_flow_counter_pool *pool =
4421 flow_dv_counter_pool_get(counter);
4423 /* Put the counter in the end - the last updated one. */
4424 TAILQ_INSERT_TAIL(&pool->counters, counter, next);
4425 counter->query_gen = rte_atomic64_read(&pool->query_gen);
4430 * Verify the @p attributes will be correctly understood by the NIC and store
4431 * them in the @p flow if everything is correct.
4434 * Pointer to dev struct.
4435 * @param[in] attributes
4436 * Pointer to flow attributes
4437 * @param[in] external
4438 * This flow rule is created by request external to PMD.
4440 * Pointer to error structure.
4443 * 0 on success, a negative errno value otherwise and rte_errno is set.
4446 flow_dv_validate_attributes(struct rte_eth_dev *dev,
4447 const struct rte_flow_attr *attributes,
4448 bool external __rte_unused,
4449 struct rte_flow_error *error)
4451 struct mlx5_priv *priv = dev->data->dev_private;
4452 uint32_t priority_max = priv->config.flow_prio - 1;
4454 #ifndef HAVE_MLX5DV_DR
4455 if (attributes->group)
4456 return rte_flow_error_set(error, ENOTSUP,
4457 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
4459 "groups are not supported");
4464 ret = mlx5_flow_group_to_table(attributes, external,
4465 attributes->group, !!priv->fdb_def_rule,
4470 if (attributes->priority != MLX5_FLOW_PRIO_RSVD &&
4471 attributes->priority >= priority_max)
4472 return rte_flow_error_set(error, ENOTSUP,
4473 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
4475 "priority out of range");
4476 if (attributes->transfer) {
4477 if (!priv->config.dv_esw_en)
4478 return rte_flow_error_set
4480 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4481 "E-Switch dr is not supported");
4482 if (!(priv->representor || priv->master))
4483 return rte_flow_error_set
4484 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4485 NULL, "E-Switch configuration can only be"
4486 " done by a master or a representor device");
4487 if (attributes->egress)
4488 return rte_flow_error_set
4490 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, attributes,
4491 "egress is not supported");
4493 if (!(attributes->egress ^ attributes->ingress))
4494 return rte_flow_error_set(error, ENOTSUP,
4495 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
4496 "must specify exactly one of "
4497 "ingress or egress");
4502 * Internal validation function. For validating both actions and items.
4505 * Pointer to the rte_eth_dev structure.
4507 * Pointer to the flow attributes.
4509 * Pointer to the list of items.
4510 * @param[in] actions
4511 * Pointer to the list of actions.
4512 * @param[in] external
4513 * This flow rule is created by request external to PMD.
4515 * Pointer to the error structure.
4518 * 0 on success, a negative errno value otherwise and rte_errno is set.
4521 flow_dv_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
4522 const struct rte_flow_item items[],
4523 const struct rte_flow_action actions[],
4524 bool external, struct rte_flow_error *error)
4527 uint64_t action_flags = 0;
4528 uint64_t item_flags = 0;
4529 uint64_t last_item = 0;
4530 uint8_t next_protocol = 0xff;
4531 uint16_t ether_type = 0;
4533 uint8_t item_ipv6_proto = 0;
4534 const struct rte_flow_item *gre_item = NULL;
4535 const struct rte_flow_action_raw_decap *decap;
4536 const struct rte_flow_action_raw_encap *encap;
4537 const struct rte_flow_action_rss *rss;
4538 const struct rte_flow_item_tcp nic_tcp_mask = {
4541 .src_port = RTE_BE16(UINT16_MAX),
4542 .dst_port = RTE_BE16(UINT16_MAX),
4545 const struct rte_flow_item_ipv4 nic_ipv4_mask = {
4547 .src_addr = RTE_BE32(0xffffffff),
4548 .dst_addr = RTE_BE32(0xffffffff),
4549 .type_of_service = 0xff,
4550 .next_proto_id = 0xff,
4551 .time_to_live = 0xff,
4554 const struct rte_flow_item_ipv6 nic_ipv6_mask = {
4557 "\xff\xff\xff\xff\xff\xff\xff\xff"
4558 "\xff\xff\xff\xff\xff\xff\xff\xff",
4560 "\xff\xff\xff\xff\xff\xff\xff\xff"
4561 "\xff\xff\xff\xff\xff\xff\xff\xff",
4562 .vtc_flow = RTE_BE32(0xffffffff),
4567 struct mlx5_priv *priv = dev->data->dev_private;
4568 struct mlx5_dev_config *dev_conf = &priv->config;
4569 uint16_t queue_index = 0xFFFF;
4573 ret = flow_dv_validate_attributes(dev, attr, external, error);
4576 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
4577 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
4578 int type = items->type;
4581 case RTE_FLOW_ITEM_TYPE_VOID:
4583 case RTE_FLOW_ITEM_TYPE_PORT_ID:
4584 ret = flow_dv_validate_item_port_id
4585 (dev, items, attr, item_flags, error);
4588 last_item = MLX5_FLOW_ITEM_PORT_ID;
4590 case RTE_FLOW_ITEM_TYPE_ETH:
4591 ret = mlx5_flow_validate_item_eth(items, item_flags,
4595 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
4596 MLX5_FLOW_LAYER_OUTER_L2;
4597 if (items->mask != NULL && items->spec != NULL) {
4599 ((const struct rte_flow_item_eth *)
4602 ((const struct rte_flow_item_eth *)
4604 ether_type = rte_be_to_cpu_16(ether_type);
4609 case RTE_FLOW_ITEM_TYPE_VLAN:
4610 ret = mlx5_flow_validate_item_vlan(items, item_flags,
4614 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
4615 MLX5_FLOW_LAYER_OUTER_VLAN;
4616 if (items->mask != NULL && items->spec != NULL) {
4618 ((const struct rte_flow_item_vlan *)
4619 items->spec)->inner_type;
4621 ((const struct rte_flow_item_vlan *)
4622 items->mask)->inner_type;
4623 ether_type = rte_be_to_cpu_16(ether_type);
4628 case RTE_FLOW_ITEM_TYPE_IPV4:
4629 mlx5_flow_tunnel_ip_check(items, next_protocol,
4630 &item_flags, &tunnel);
4631 ret = mlx5_flow_validate_item_ipv4(items, item_flags,
4638 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
4639 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
4640 if (items->mask != NULL &&
4641 ((const struct rte_flow_item_ipv4 *)
4642 items->mask)->hdr.next_proto_id) {
4644 ((const struct rte_flow_item_ipv4 *)
4645 (items->spec))->hdr.next_proto_id;
4647 ((const struct rte_flow_item_ipv4 *)
4648 (items->mask))->hdr.next_proto_id;
4650 /* Reset for inner layer. */
4651 next_protocol = 0xff;
4654 case RTE_FLOW_ITEM_TYPE_IPV6:
4655 mlx5_flow_tunnel_ip_check(items, next_protocol,
4656 &item_flags, &tunnel);
4657 ret = mlx5_flow_validate_item_ipv6(items, item_flags,
4664 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
4665 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
4666 if (items->mask != NULL &&
4667 ((const struct rte_flow_item_ipv6 *)
4668 items->mask)->hdr.proto) {
4670 ((const struct rte_flow_item_ipv6 *)
4671 items->spec)->hdr.proto;
4673 ((const struct rte_flow_item_ipv6 *)
4674 items->spec)->hdr.proto;
4676 ((const struct rte_flow_item_ipv6 *)
4677 items->mask)->hdr.proto;
4679 /* Reset for inner layer. */
4680 next_protocol = 0xff;
4683 case RTE_FLOW_ITEM_TYPE_TCP:
4684 ret = mlx5_flow_validate_item_tcp
4691 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
4692 MLX5_FLOW_LAYER_OUTER_L4_TCP;
4694 case RTE_FLOW_ITEM_TYPE_UDP:
4695 ret = mlx5_flow_validate_item_udp(items, item_flags,
4700 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
4701 MLX5_FLOW_LAYER_OUTER_L4_UDP;
4703 case RTE_FLOW_ITEM_TYPE_GRE:
4704 ret = mlx5_flow_validate_item_gre(items, item_flags,
4705 next_protocol, error);
4709 last_item = MLX5_FLOW_LAYER_GRE;
4711 case RTE_FLOW_ITEM_TYPE_NVGRE:
4712 ret = mlx5_flow_validate_item_nvgre(items, item_flags,
4717 last_item = MLX5_FLOW_LAYER_NVGRE;
4719 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
4720 ret = mlx5_flow_validate_item_gre_key
4721 (items, item_flags, gre_item, error);
4724 last_item = MLX5_FLOW_LAYER_GRE_KEY;
4726 case RTE_FLOW_ITEM_TYPE_VXLAN:
4727 ret = mlx5_flow_validate_item_vxlan(items, item_flags,
4731 last_item = MLX5_FLOW_LAYER_VXLAN;
4733 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
4734 ret = mlx5_flow_validate_item_vxlan_gpe(items,
4739 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
4741 case RTE_FLOW_ITEM_TYPE_GENEVE:
4742 ret = mlx5_flow_validate_item_geneve(items,
4747 last_item = MLX5_FLOW_LAYER_GENEVE;
4749 case RTE_FLOW_ITEM_TYPE_MPLS:
4750 ret = mlx5_flow_validate_item_mpls(dev, items,
4755 last_item = MLX5_FLOW_LAYER_MPLS;
4758 case RTE_FLOW_ITEM_TYPE_MARK:
4759 ret = flow_dv_validate_item_mark(dev, items, attr,
4763 last_item = MLX5_FLOW_ITEM_MARK;
4765 case RTE_FLOW_ITEM_TYPE_META:
4766 ret = flow_dv_validate_item_meta(dev, items, attr,
4770 last_item = MLX5_FLOW_ITEM_METADATA;
4772 case RTE_FLOW_ITEM_TYPE_ICMP:
4773 ret = mlx5_flow_validate_item_icmp(items, item_flags,
4778 last_item = MLX5_FLOW_LAYER_ICMP;
4780 case RTE_FLOW_ITEM_TYPE_ICMP6:
4781 ret = mlx5_flow_validate_item_icmp6(items, item_flags,
4786 item_ipv6_proto = IPPROTO_ICMPV6;
4787 last_item = MLX5_FLOW_LAYER_ICMP6;
4789 case RTE_FLOW_ITEM_TYPE_TAG:
4790 ret = flow_dv_validate_item_tag(dev, items,
4794 last_item = MLX5_FLOW_ITEM_TAG;
4796 case MLX5_RTE_FLOW_ITEM_TYPE_TAG:
4797 case MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE:
4799 case RTE_FLOW_ITEM_TYPE_GTP:
4800 ret = flow_dv_validate_item_gtp(dev, items, item_flags,
4804 last_item = MLX5_FLOW_LAYER_GTP;
4807 return rte_flow_error_set(error, ENOTSUP,
4808 RTE_FLOW_ERROR_TYPE_ITEM,
4809 NULL, "item not supported");
4811 item_flags |= last_item;
4813 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
4814 int type = actions->type;
4815 if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
4816 return rte_flow_error_set(error, ENOTSUP,
4817 RTE_FLOW_ERROR_TYPE_ACTION,
4818 actions, "too many actions");
4820 case RTE_FLOW_ACTION_TYPE_VOID:
4822 case RTE_FLOW_ACTION_TYPE_PORT_ID:
4823 ret = flow_dv_validate_action_port_id(dev,
4830 action_flags |= MLX5_FLOW_ACTION_PORT_ID;
4833 case RTE_FLOW_ACTION_TYPE_FLAG:
4834 ret = flow_dv_validate_action_flag(dev, action_flags,
4838 if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
4839 /* Count all modify-header actions as one. */
4840 if (!(action_flags &
4841 MLX5_FLOW_MODIFY_HDR_ACTIONS))
4843 action_flags |= MLX5_FLOW_ACTION_FLAG |
4844 MLX5_FLOW_ACTION_MARK_EXT;
4846 action_flags |= MLX5_FLOW_ACTION_FLAG;
4850 case RTE_FLOW_ACTION_TYPE_MARK:
4851 ret = flow_dv_validate_action_mark(dev, actions,
4856 if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
4857 /* Count all modify-header actions as one. */
4858 if (!(action_flags &
4859 MLX5_FLOW_MODIFY_HDR_ACTIONS))
4861 action_flags |= MLX5_FLOW_ACTION_MARK |
4862 MLX5_FLOW_ACTION_MARK_EXT;
4864 action_flags |= MLX5_FLOW_ACTION_MARK;
4868 case RTE_FLOW_ACTION_TYPE_SET_META:
4869 ret = flow_dv_validate_action_set_meta(dev, actions,
4874 /* Count all modify-header actions as one action. */
4875 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4877 action_flags |= MLX5_FLOW_ACTION_SET_META;
4879 case RTE_FLOW_ACTION_TYPE_SET_TAG:
4880 ret = flow_dv_validate_action_set_tag(dev, actions,
4885 /* Count all modify-header actions as one action. */
4886 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4888 action_flags |= MLX5_FLOW_ACTION_SET_TAG;
4890 case RTE_FLOW_ACTION_TYPE_DROP:
4891 ret = mlx5_flow_validate_action_drop(action_flags,
4895 action_flags |= MLX5_FLOW_ACTION_DROP;
4898 case RTE_FLOW_ACTION_TYPE_QUEUE:
4899 ret = mlx5_flow_validate_action_queue(actions,
4904 queue_index = ((const struct rte_flow_action_queue *)
4905 (actions->conf))->index;
4906 action_flags |= MLX5_FLOW_ACTION_QUEUE;
4909 case RTE_FLOW_ACTION_TYPE_RSS:
4910 rss = actions->conf;
4911 ret = mlx5_flow_validate_action_rss(actions,
4917 if (rss != NULL && rss->queue_num)
4918 queue_index = rss->queue[0];
4919 action_flags |= MLX5_FLOW_ACTION_RSS;
4922 case RTE_FLOW_ACTION_TYPE_COUNT:
4923 ret = flow_dv_validate_action_count(dev, error);
4926 action_flags |= MLX5_FLOW_ACTION_COUNT;
4929 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
4930 if (flow_dv_validate_action_pop_vlan(dev,
4936 action_flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
4939 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
4940 ret = flow_dv_validate_action_push_vlan(dev,
4947 action_flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
4950 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
4951 ret = flow_dv_validate_action_set_vlan_pcp
4952 (action_flags, actions, error);
4955 /* Count PCP with push_vlan command. */
4956 action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_PCP;
4958 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
4959 ret = flow_dv_validate_action_set_vlan_vid
4960 (item_flags, action_flags,
4964 /* Count VID with push_vlan command. */
4965 action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
4967 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
4968 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
4969 ret = flow_dv_validate_action_l2_encap(dev,
4975 action_flags |= MLX5_FLOW_ACTION_ENCAP;
4978 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
4979 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
4980 ret = flow_dv_validate_action_decap(dev, action_flags,
4984 action_flags |= MLX5_FLOW_ACTION_DECAP;
4987 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
4988 ret = flow_dv_validate_action_raw_encap_decap
4989 (dev, NULL, actions->conf, attr, &action_flags,
4994 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
4995 decap = actions->conf;
4996 while ((++actions)->type == RTE_FLOW_ACTION_TYPE_VOID)
4998 if (actions->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
5002 encap = actions->conf;
5004 ret = flow_dv_validate_action_raw_encap_decap
5006 decap ? decap : &empty_decap, encap,
5007 attr, &action_flags, &actions_n,
5012 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
5013 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
5014 ret = flow_dv_validate_action_modify_mac(action_flags,
5020 /* Count all modify-header actions as one action. */
5021 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5023 action_flags |= actions->type ==
5024 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
5025 MLX5_FLOW_ACTION_SET_MAC_SRC :
5026 MLX5_FLOW_ACTION_SET_MAC_DST;
5029 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
5030 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
5031 ret = flow_dv_validate_action_modify_ipv4(action_flags,
5037 /* Count all modify-header actions as one action. */
5038 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5040 action_flags |= actions->type ==
5041 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
5042 MLX5_FLOW_ACTION_SET_IPV4_SRC :
5043 MLX5_FLOW_ACTION_SET_IPV4_DST;
5045 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
5046 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
5047 ret = flow_dv_validate_action_modify_ipv6(action_flags,
5053 if (item_ipv6_proto == IPPROTO_ICMPV6)
5054 return rte_flow_error_set(error, ENOTSUP,
5055 RTE_FLOW_ERROR_TYPE_ACTION,
5057 "Can't change header "
5058 "with ICMPv6 proto");
5059 /* Count all modify-header actions as one action. */
5060 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5062 action_flags |= actions->type ==
5063 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
5064 MLX5_FLOW_ACTION_SET_IPV6_SRC :
5065 MLX5_FLOW_ACTION_SET_IPV6_DST;
5067 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
5068 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
5069 ret = flow_dv_validate_action_modify_tp(action_flags,
5075 /* Count all modify-header actions as one action. */
5076 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5078 action_flags |= actions->type ==
5079 RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
5080 MLX5_FLOW_ACTION_SET_TP_SRC :
5081 MLX5_FLOW_ACTION_SET_TP_DST;
5083 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
5084 case RTE_FLOW_ACTION_TYPE_SET_TTL:
5085 ret = flow_dv_validate_action_modify_ttl(action_flags,
5091 /* Count all modify-header actions as one action. */
5092 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5094 action_flags |= actions->type ==
5095 RTE_FLOW_ACTION_TYPE_SET_TTL ?
5096 MLX5_FLOW_ACTION_SET_TTL :
5097 MLX5_FLOW_ACTION_DEC_TTL;
5099 case RTE_FLOW_ACTION_TYPE_JUMP:
5100 ret = flow_dv_validate_action_jump(actions,
5107 action_flags |= MLX5_FLOW_ACTION_JUMP;
5109 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
5110 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
5111 ret = flow_dv_validate_action_modify_tcp_seq
5118 /* Count all modify-header actions as one action. */
5119 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5121 action_flags |= actions->type ==
5122 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
5123 MLX5_FLOW_ACTION_INC_TCP_SEQ :
5124 MLX5_FLOW_ACTION_DEC_TCP_SEQ;
5126 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
5127 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
5128 ret = flow_dv_validate_action_modify_tcp_ack
5135 /* Count all modify-header actions as one action. */
5136 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5138 action_flags |= actions->type ==
5139 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
5140 MLX5_FLOW_ACTION_INC_TCP_ACK :
5141 MLX5_FLOW_ACTION_DEC_TCP_ACK;
5143 case MLX5_RTE_FLOW_ACTION_TYPE_TAG:
5144 case MLX5_RTE_FLOW_ACTION_TYPE_MARK:
5145 case MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG:
5147 case RTE_FLOW_ACTION_TYPE_METER:
5148 ret = mlx5_flow_validate_action_meter(dev,
5154 action_flags |= MLX5_FLOW_ACTION_METER;
5157 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
5158 ret = flow_dv_validate_action_modify_ipv4_dscp
5165 /* Count all modify-header actions as one action. */
5166 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5168 action_flags |= MLX5_FLOW_ACTION_SET_IPV4_DSCP;
5170 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
5171 ret = flow_dv_validate_action_modify_ipv6_dscp
5178 /* Count all modify-header actions as one action. */
5179 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5181 action_flags |= MLX5_FLOW_ACTION_SET_IPV6_DSCP;
5184 return rte_flow_error_set(error, ENOTSUP,
5185 RTE_FLOW_ERROR_TYPE_ACTION,
5187 "action not supported");
5191 * Validate the drop action mutual exclusion with other actions.
5192 * Drop action is mutually-exclusive with any other action, except for
5195 if ((action_flags & MLX5_FLOW_ACTION_DROP) &&
5196 (action_flags & ~(MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_COUNT)))
5197 return rte_flow_error_set(error, EINVAL,
5198 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5199 "Drop action is mutually-exclusive "
5200 "with any other action, except for "
5202 /* Eswitch has few restrictions on using items and actions */
5203 if (attr->transfer) {
5204 if (!mlx5_flow_ext_mreg_supported(dev) &&
5205 action_flags & MLX5_FLOW_ACTION_FLAG)
5206 return rte_flow_error_set(error, ENOTSUP,
5207 RTE_FLOW_ERROR_TYPE_ACTION,
5209 "unsupported action FLAG");
5210 if (!mlx5_flow_ext_mreg_supported(dev) &&
5211 action_flags & MLX5_FLOW_ACTION_MARK)
5212 return rte_flow_error_set(error, ENOTSUP,
5213 RTE_FLOW_ERROR_TYPE_ACTION,
5215 "unsupported action MARK");
5216 if (action_flags & MLX5_FLOW_ACTION_QUEUE)
5217 return rte_flow_error_set(error, ENOTSUP,
5218 RTE_FLOW_ERROR_TYPE_ACTION,
5220 "unsupported action QUEUE");
5221 if (action_flags & MLX5_FLOW_ACTION_RSS)
5222 return rte_flow_error_set(error, ENOTSUP,
5223 RTE_FLOW_ERROR_TYPE_ACTION,
5225 "unsupported action RSS");
5226 if (!(action_flags & MLX5_FLOW_FATE_ESWITCH_ACTIONS))
5227 return rte_flow_error_set(error, EINVAL,
5228 RTE_FLOW_ERROR_TYPE_ACTION,
5230 "no fate action is found");
5232 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS) && attr->ingress)
5233 return rte_flow_error_set(error, EINVAL,
5234 RTE_FLOW_ERROR_TYPE_ACTION,
5236 "no fate action is found");
5238 /* Continue validation for Xcap actions.*/
5239 if ((action_flags & MLX5_FLOW_XCAP_ACTIONS) && (queue_index == 0xFFFF ||
5240 mlx5_rxq_get_type(dev, queue_index) != MLX5_RXQ_TYPE_HAIRPIN)) {
5241 if ((action_flags & MLX5_FLOW_XCAP_ACTIONS) ==
5242 MLX5_FLOW_XCAP_ACTIONS)
5243 return rte_flow_error_set(error, ENOTSUP,
5244 RTE_FLOW_ERROR_TYPE_ACTION,
5245 NULL, "encap and decap "
5246 "combination aren't supported");
5247 if (!attr->transfer && attr->ingress && (action_flags &
5248 MLX5_FLOW_ACTION_ENCAP))
5249 return rte_flow_error_set(error, ENOTSUP,
5250 RTE_FLOW_ERROR_TYPE_ACTION,
5251 NULL, "encap is not supported"
5252 " for ingress traffic");
5258 * Internal preparation function. Allocates the DV flow size,
5259 * this size is constant.
5262 * Pointer to the flow attributes.
5264 * Pointer to the list of items.
5265 * @param[in] actions
5266 * Pointer to the list of actions.
5268 * Pointer to the error structure.
5271 * Pointer to mlx5_flow object on success,
5272 * otherwise NULL and rte_errno is set.
5274 static struct mlx5_flow *
5275 flow_dv_prepare(const struct rte_flow_attr *attr __rte_unused,
5276 const struct rte_flow_item items[] __rte_unused,
5277 const struct rte_flow_action actions[] __rte_unused,
5278 struct rte_flow_error *error)
5280 size_t size = sizeof(struct mlx5_flow);
5281 struct mlx5_flow *dev_flow;
5283 dev_flow = rte_calloc(__func__, 1, size, 0);
5285 rte_flow_error_set(error, ENOMEM,
5286 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5287 "not enough memory to create flow");
5290 dev_flow->dv.value.size = MLX5_ST_SZ_BYTES(fte_match_param);
5291 dev_flow->ingress = attr->ingress;
5292 dev_flow->transfer = attr->transfer;
5296 #ifdef RTE_LIBRTE_MLX5_DEBUG
5298 * Sanity check for match mask and value. Similar to check_valid_spec() in
5299 * kernel driver. If unmasked bit is present in value, it returns failure.
5302 * pointer to match mask buffer.
5303 * @param match_value
5304 * pointer to match value buffer.
5307 * 0 if valid, -EINVAL otherwise.
5310 flow_dv_check_valid_spec(void *match_mask, void *match_value)
5312 uint8_t *m = match_mask;
5313 uint8_t *v = match_value;
5316 for (i = 0; i < MLX5_ST_SZ_BYTES(fte_match_param); ++i) {
5319 "match_value differs from match_criteria"
5320 " %p[%u] != %p[%u]",
5321 match_value, i, match_mask, i);
5330 * Add Ethernet item to matcher and to the value.
5332 * @param[in, out] matcher
5334 * @param[in, out] key
5335 * Flow matcher value.
5337 * Flow pattern to translate.
5339 * Item is inner pattern.
5342 flow_dv_translate_item_eth(void *matcher, void *key,
5343 const struct rte_flow_item *item, int inner)
5345 const struct rte_flow_item_eth *eth_m = item->mask;
5346 const struct rte_flow_item_eth *eth_v = item->spec;
5347 const struct rte_flow_item_eth nic_mask = {
5348 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
5349 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
5350 .type = RTE_BE16(0xffff),
5362 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5364 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5366 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5368 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5370 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, dmac_47_16),
5371 ð_m->dst, sizeof(eth_m->dst));
5372 /* The value must be in the range of the mask. */
5373 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, dmac_47_16);
5374 for (i = 0; i < sizeof(eth_m->dst); ++i)
5375 l24_v[i] = eth_m->dst.addr_bytes[i] & eth_v->dst.addr_bytes[i];
5376 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, smac_47_16),
5377 ð_m->src, sizeof(eth_m->src));
5378 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, smac_47_16);
5379 /* The value must be in the range of the mask. */
5380 for (i = 0; i < sizeof(eth_m->dst); ++i)
5381 l24_v[i] = eth_m->src.addr_bytes[i] & eth_v->src.addr_bytes[i];
5383 /* When ethertype is present set mask for tagged VLAN. */
5384 MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
5385 /* Set value for tagged VLAN if ethertype is 802.1Q. */
5386 if (eth_v->type == RTE_BE16(RTE_ETHER_TYPE_VLAN) ||
5387 eth_v->type == RTE_BE16(RTE_ETHER_TYPE_QINQ)) {
5388 MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag,
5390 /* Return here to avoid setting match on ethertype. */
5395 * HW supports match on one Ethertype, the Ethertype following the last
5396 * VLAN tag of the packet (see PRM).
5397 * Set match on ethertype only if ETH header is not followed by VLAN.
5399 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype,
5400 rte_be_to_cpu_16(eth_m->type));
5401 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, ethertype);
5402 *(uint16_t *)(l24_v) = eth_m->type & eth_v->type;
5406 * Add VLAN item to matcher and to the value.
5408 * @param[in, out] dev_flow
5410 * @param[in, out] matcher
5412 * @param[in, out] key
5413 * Flow matcher value.
5415 * Flow pattern to translate.
5417 * Item is inner pattern.
5420 flow_dv_translate_item_vlan(struct mlx5_flow *dev_flow,
5421 void *matcher, void *key,
5422 const struct rte_flow_item *item,
5425 const struct rte_flow_item_vlan *vlan_m = item->mask;
5426 const struct rte_flow_item_vlan *vlan_v = item->spec;
5435 vlan_m = &rte_flow_item_vlan_mask;
5437 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5439 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5441 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5443 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5445 * This is workaround, masks are not supported,
5446 * and pre-validated.
5448 dev_flow->dv.vf_vlan.tag =
5449 rte_be_to_cpu_16(vlan_v->tci) & 0x0fff;
5451 tci_m = rte_be_to_cpu_16(vlan_m->tci);
5452 tci_v = rte_be_to_cpu_16(vlan_m->tci & vlan_v->tci);
5453 MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
5454 MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag, 1);
5455 MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_vid, tci_m);
5456 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, tci_v);
5457 MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_cfi, tci_m >> 12);
5458 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_cfi, tci_v >> 12);
5459 MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_prio, tci_m >> 13);
5460 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, tci_v >> 13);
5461 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype,
5462 rte_be_to_cpu_16(vlan_m->inner_type));
5463 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
5464 rte_be_to_cpu_16(vlan_m->inner_type & vlan_v->inner_type));
5468 * Add IPV4 item to matcher and to the value.
5470 * @param[in, out] matcher
5472 * @param[in, out] key
5473 * Flow matcher value.
5475 * Flow pattern to translate.
5476 * @param[in] item_flags
5477 * Bit-fields that holds the items detected until now.
5479 * Item is inner pattern.
5481 * The group to insert the rule.
5484 flow_dv_translate_item_ipv4(void *matcher, void *key,
5485 const struct rte_flow_item *item,
5486 const uint64_t item_flags,
5487 int inner, uint32_t group)
5489 const struct rte_flow_item_ipv4 *ipv4_m = item->mask;
5490 const struct rte_flow_item_ipv4 *ipv4_v = item->spec;
5491 const struct rte_flow_item_ipv4 nic_mask = {
5493 .src_addr = RTE_BE32(0xffffffff),
5494 .dst_addr = RTE_BE32(0xffffffff),
5495 .type_of_service = 0xff,
5496 .next_proto_id = 0xff,
5497 .time_to_live = 0xff,
5507 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5509 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5511 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5513 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5516 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
5518 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0x4);
5519 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, 4);
5521 * On outer header (which must contains L2), or inner header with L2,
5522 * set cvlan_tag mask bit to mark this packet as untagged.
5523 * This should be done even if item->spec is empty.
5525 if (!inner || item_flags & MLX5_FLOW_LAYER_INNER_L2)
5526 MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
5531 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
5532 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
5533 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
5534 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
5535 *(uint32_t *)l24_m = ipv4_m->hdr.dst_addr;
5536 *(uint32_t *)l24_v = ipv4_m->hdr.dst_addr & ipv4_v->hdr.dst_addr;
5537 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
5538 src_ipv4_src_ipv6.ipv4_layout.ipv4);
5539 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
5540 src_ipv4_src_ipv6.ipv4_layout.ipv4);
5541 *(uint32_t *)l24_m = ipv4_m->hdr.src_addr;
5542 *(uint32_t *)l24_v = ipv4_m->hdr.src_addr & ipv4_v->hdr.src_addr;
5543 tos = ipv4_m->hdr.type_of_service & ipv4_v->hdr.type_of_service;
5544 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn,
5545 ipv4_m->hdr.type_of_service);
5546 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, tos);
5547 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp,
5548 ipv4_m->hdr.type_of_service >> 2);
5549 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, tos >> 2);
5550 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
5551 ipv4_m->hdr.next_proto_id);
5552 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
5553 ipv4_v->hdr.next_proto_id & ipv4_m->hdr.next_proto_id);
5554 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ttl_hoplimit,
5555 ipv4_m->hdr.time_to_live);
5556 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ttl_hoplimit,
5557 ipv4_v->hdr.time_to_live & ipv4_m->hdr.time_to_live);
5561 * Add IPV6 item to matcher and to the value.
5563 * @param[in, out] matcher
5565 * @param[in, out] key
5566 * Flow matcher value.
5568 * Flow pattern to translate.
5569 * @param[in] item_flags
5570 * Bit-fields that holds the items detected until now.
5572 * Item is inner pattern.
5574 * The group to insert the rule.
5577 flow_dv_translate_item_ipv6(void *matcher, void *key,
5578 const struct rte_flow_item *item,
5579 const uint64_t item_flags,
5580 int inner, uint32_t group)
5582 const struct rte_flow_item_ipv6 *ipv6_m = item->mask;
5583 const struct rte_flow_item_ipv6 *ipv6_v = item->spec;
5584 const struct rte_flow_item_ipv6 nic_mask = {
5587 "\xff\xff\xff\xff\xff\xff\xff\xff"
5588 "\xff\xff\xff\xff\xff\xff\xff\xff",
5590 "\xff\xff\xff\xff\xff\xff\xff\xff"
5591 "\xff\xff\xff\xff\xff\xff\xff\xff",
5592 .vtc_flow = RTE_BE32(0xffffffff),
5599 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5600 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5609 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5611 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5613 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5615 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5618 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
5620 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0x6);
5621 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, 6);
5623 * On outer header (which must contains L2), or inner header with L2,
5624 * set cvlan_tag mask bit to mark this packet as untagged.
5625 * This should be done even if item->spec is empty.
5627 if (!inner || item_flags & MLX5_FLOW_LAYER_INNER_L2)
5628 MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
5633 size = sizeof(ipv6_m->hdr.dst_addr);
5634 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
5635 dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
5636 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
5637 dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
5638 memcpy(l24_m, ipv6_m->hdr.dst_addr, size);
5639 for (i = 0; i < size; ++i)
5640 l24_v[i] = l24_m[i] & ipv6_v->hdr.dst_addr[i];
5641 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
5642 src_ipv4_src_ipv6.ipv6_layout.ipv6);
5643 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
5644 src_ipv4_src_ipv6.ipv6_layout.ipv6);
5645 memcpy(l24_m, ipv6_m->hdr.src_addr, size);
5646 for (i = 0; i < size; ++i)
5647 l24_v[i] = l24_m[i] & ipv6_v->hdr.src_addr[i];
5649 vtc_m = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow);
5650 vtc_v = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow & ipv6_v->hdr.vtc_flow);
5651 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn, vtc_m >> 20);
5652 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, vtc_v >> 20);
5653 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp, vtc_m >> 22);
5654 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, vtc_v >> 22);
5657 MLX5_SET(fte_match_set_misc, misc_m, inner_ipv6_flow_label,
5659 MLX5_SET(fte_match_set_misc, misc_v, inner_ipv6_flow_label,
5662 MLX5_SET(fte_match_set_misc, misc_m, outer_ipv6_flow_label,
5664 MLX5_SET(fte_match_set_misc, misc_v, outer_ipv6_flow_label,
5668 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
5670 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
5671 ipv6_v->hdr.proto & ipv6_m->hdr.proto);
5673 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ttl_hoplimit,
5674 ipv6_m->hdr.hop_limits);
5675 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ttl_hoplimit,
5676 ipv6_v->hdr.hop_limits & ipv6_m->hdr.hop_limits);
5680 * Add TCP item to matcher and to the value.
5682 * @param[in, out] matcher
5684 * @param[in, out] key
5685 * Flow matcher value.
5687 * Flow pattern to translate.
5689 * Item is inner pattern.
5692 flow_dv_translate_item_tcp(void *matcher, void *key,
5693 const struct rte_flow_item *item,
5696 const struct rte_flow_item_tcp *tcp_m = item->mask;
5697 const struct rte_flow_item_tcp *tcp_v = item->spec;
5702 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5704 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5706 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5708 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5710 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
5711 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_TCP);
5715 tcp_m = &rte_flow_item_tcp_mask;
5716 MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_sport,
5717 rte_be_to_cpu_16(tcp_m->hdr.src_port));
5718 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
5719 rte_be_to_cpu_16(tcp_v->hdr.src_port & tcp_m->hdr.src_port));
5720 MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_dport,
5721 rte_be_to_cpu_16(tcp_m->hdr.dst_port));
5722 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
5723 rte_be_to_cpu_16(tcp_v->hdr.dst_port & tcp_m->hdr.dst_port));
5724 MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_flags,
5725 tcp_m->hdr.tcp_flags);
5726 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
5727 (tcp_v->hdr.tcp_flags & tcp_m->hdr.tcp_flags));
5731 * Add UDP item to matcher and to the value.
5733 * @param[in, out] matcher
5735 * @param[in, out] key
5736 * Flow matcher value.
5738 * Flow pattern to translate.
5740 * Item is inner pattern.
5743 flow_dv_translate_item_udp(void *matcher, void *key,
5744 const struct rte_flow_item *item,
5747 const struct rte_flow_item_udp *udp_m = item->mask;
5748 const struct rte_flow_item_udp *udp_v = item->spec;
5753 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5755 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5757 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5759 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5761 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
5762 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
5766 udp_m = &rte_flow_item_udp_mask;
5767 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_sport,
5768 rte_be_to_cpu_16(udp_m->hdr.src_port));
5769 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
5770 rte_be_to_cpu_16(udp_v->hdr.src_port & udp_m->hdr.src_port));
5771 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport,
5772 rte_be_to_cpu_16(udp_m->hdr.dst_port));
5773 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
5774 rte_be_to_cpu_16(udp_v->hdr.dst_port & udp_m->hdr.dst_port));
5778 * Add GRE optional Key item to matcher and to the value.
5780 * @param[in, out] matcher
5782 * @param[in, out] key
5783 * Flow matcher value.
5785 * Flow pattern to translate.
5787 * Item is inner pattern.
5790 flow_dv_translate_item_gre_key(void *matcher, void *key,
5791 const struct rte_flow_item *item)
5793 const rte_be32_t *key_m = item->mask;
5794 const rte_be32_t *key_v = item->spec;
5795 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5796 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5797 rte_be32_t gre_key_default_mask = RTE_BE32(UINT32_MAX);
5799 /* GRE K bit must be on and should already be validated */
5800 MLX5_SET(fte_match_set_misc, misc_m, gre_k_present, 1);
5801 MLX5_SET(fte_match_set_misc, misc_v, gre_k_present, 1);
5805 key_m = &gre_key_default_mask;
5806 MLX5_SET(fte_match_set_misc, misc_m, gre_key_h,
5807 rte_be_to_cpu_32(*key_m) >> 8);
5808 MLX5_SET(fte_match_set_misc, misc_v, gre_key_h,
5809 rte_be_to_cpu_32((*key_v) & (*key_m)) >> 8);
5810 MLX5_SET(fte_match_set_misc, misc_m, gre_key_l,
5811 rte_be_to_cpu_32(*key_m) & 0xFF);
5812 MLX5_SET(fte_match_set_misc, misc_v, gre_key_l,
5813 rte_be_to_cpu_32((*key_v) & (*key_m)) & 0xFF);
5817 * Add GRE item to matcher and to the value.
5819 * @param[in, out] matcher
5821 * @param[in, out] key
5822 * Flow matcher value.
5824 * Flow pattern to translate.
5826 * Item is inner pattern.
5829 flow_dv_translate_item_gre(void *matcher, void *key,
5830 const struct rte_flow_item *item,
5833 const struct rte_flow_item_gre *gre_m = item->mask;
5834 const struct rte_flow_item_gre *gre_v = item->spec;
5837 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5838 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5845 uint16_t s_present:1;
5846 uint16_t k_present:1;
5847 uint16_t rsvd_bit1:1;
5848 uint16_t c_present:1;
5852 } gre_crks_rsvd0_ver_m, gre_crks_rsvd0_ver_v;
5855 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5857 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5859 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5861 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5863 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
5864 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_GRE);
5868 gre_m = &rte_flow_item_gre_mask;
5869 MLX5_SET(fte_match_set_misc, misc_m, gre_protocol,
5870 rte_be_to_cpu_16(gre_m->protocol));
5871 MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
5872 rte_be_to_cpu_16(gre_v->protocol & gre_m->protocol));
5873 gre_crks_rsvd0_ver_m.value = rte_be_to_cpu_16(gre_m->c_rsvd0_ver);
5874 gre_crks_rsvd0_ver_v.value = rte_be_to_cpu_16(gre_v->c_rsvd0_ver);
5875 MLX5_SET(fte_match_set_misc, misc_m, gre_c_present,
5876 gre_crks_rsvd0_ver_m.c_present);
5877 MLX5_SET(fte_match_set_misc, misc_v, gre_c_present,
5878 gre_crks_rsvd0_ver_v.c_present &
5879 gre_crks_rsvd0_ver_m.c_present);
5880 MLX5_SET(fte_match_set_misc, misc_m, gre_k_present,
5881 gre_crks_rsvd0_ver_m.k_present);
5882 MLX5_SET(fte_match_set_misc, misc_v, gre_k_present,
5883 gre_crks_rsvd0_ver_v.k_present &
5884 gre_crks_rsvd0_ver_m.k_present);
5885 MLX5_SET(fte_match_set_misc, misc_m, gre_s_present,
5886 gre_crks_rsvd0_ver_m.s_present);
5887 MLX5_SET(fte_match_set_misc, misc_v, gre_s_present,
5888 gre_crks_rsvd0_ver_v.s_present &
5889 gre_crks_rsvd0_ver_m.s_present);
5893 * Add NVGRE item to matcher and to the value.
5895 * @param[in, out] matcher
5897 * @param[in, out] key
5898 * Flow matcher value.
5900 * Flow pattern to translate.
5902 * Item is inner pattern.
5905 flow_dv_translate_item_nvgre(void *matcher, void *key,
5906 const struct rte_flow_item *item,
5909 const struct rte_flow_item_nvgre *nvgre_m = item->mask;
5910 const struct rte_flow_item_nvgre *nvgre_v = item->spec;
5911 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5912 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5913 const char *tni_flow_id_m = (const char *)nvgre_m->tni;
5914 const char *tni_flow_id_v = (const char *)nvgre_v->tni;
5920 /* For NVGRE, GRE header fields must be set with defined values. */
5921 const struct rte_flow_item_gre gre_spec = {
5922 .c_rsvd0_ver = RTE_BE16(0x2000),
5923 .protocol = RTE_BE16(RTE_ETHER_TYPE_TEB)
5925 const struct rte_flow_item_gre gre_mask = {
5926 .c_rsvd0_ver = RTE_BE16(0xB000),
5927 .protocol = RTE_BE16(UINT16_MAX),
5929 const struct rte_flow_item gre_item = {
5934 flow_dv_translate_item_gre(matcher, key, &gre_item, inner);
5938 nvgre_m = &rte_flow_item_nvgre_mask;
5939 size = sizeof(nvgre_m->tni) + sizeof(nvgre_m->flow_id);
5940 gre_key_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, gre_key_h);
5941 gre_key_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, gre_key_h);
5942 memcpy(gre_key_m, tni_flow_id_m, size);
5943 for (i = 0; i < size; ++i)
5944 gre_key_v[i] = gre_key_m[i] & tni_flow_id_v[i];
5948 * Add VXLAN item to matcher and to the value.
5950 * @param[in, out] matcher
5952 * @param[in, out] key
5953 * Flow matcher value.
5955 * Flow pattern to translate.
5957 * Item is inner pattern.
5960 flow_dv_translate_item_vxlan(void *matcher, void *key,
5961 const struct rte_flow_item *item,
5964 const struct rte_flow_item_vxlan *vxlan_m = item->mask;
5965 const struct rte_flow_item_vxlan *vxlan_v = item->spec;
5968 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5969 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5977 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5979 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5981 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5983 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5985 dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
5986 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
5987 if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
5988 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
5989 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
5994 vxlan_m = &rte_flow_item_vxlan_mask;
5995 size = sizeof(vxlan_m->vni);
5996 vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, vxlan_vni);
5997 vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, vxlan_vni);
5998 memcpy(vni_m, vxlan_m->vni, size);
5999 for (i = 0; i < size; ++i)
6000 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
6004 * Add VXLAN-GPE item to matcher and to the value.
6006 * @param[in, out] matcher
6008 * @param[in, out] key
6009 * Flow matcher value.
6011 * Flow pattern to translate.
6013 * Item is inner pattern.
6017 flow_dv_translate_item_vxlan_gpe(void *matcher, void *key,
6018 const struct rte_flow_item *item, int inner)
6020 const struct rte_flow_item_vxlan_gpe *vxlan_m = item->mask;
6021 const struct rte_flow_item_vxlan_gpe *vxlan_v = item->spec;
6025 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_3);
6027 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
6033 uint8_t flags_m = 0xff;
6034 uint8_t flags_v = 0xc;
6037 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6039 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6041 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6043 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6045 dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
6046 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
6047 if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
6048 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
6049 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
6054 vxlan_m = &rte_flow_item_vxlan_gpe_mask;
6055 size = sizeof(vxlan_m->vni);
6056 vni_m = MLX5_ADDR_OF(fte_match_set_misc3, misc_m, outer_vxlan_gpe_vni);
6057 vni_v = MLX5_ADDR_OF(fte_match_set_misc3, misc_v, outer_vxlan_gpe_vni);
6058 memcpy(vni_m, vxlan_m->vni, size);
6059 for (i = 0; i < size; ++i)
6060 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
6061 if (vxlan_m->flags) {
6062 flags_m = vxlan_m->flags;
6063 flags_v = vxlan_v->flags;
6065 MLX5_SET(fte_match_set_misc3, misc_m, outer_vxlan_gpe_flags, flags_m);
6066 MLX5_SET(fte_match_set_misc3, misc_v, outer_vxlan_gpe_flags, flags_v);
6067 MLX5_SET(fte_match_set_misc3, misc_m, outer_vxlan_gpe_next_protocol,
6069 MLX5_SET(fte_match_set_misc3, misc_v, outer_vxlan_gpe_next_protocol,
6074 * Add Geneve item to matcher and to the value.
6076 * @param[in, out] matcher
6078 * @param[in, out] key
6079 * Flow matcher value.
6081 * Flow pattern to translate.
6083 * Item is inner pattern.
6087 flow_dv_translate_item_geneve(void *matcher, void *key,
6088 const struct rte_flow_item *item, int inner)
6090 const struct rte_flow_item_geneve *geneve_m = item->mask;
6091 const struct rte_flow_item_geneve *geneve_v = item->spec;
6094 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6095 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6104 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6106 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6108 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6110 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6112 dport = MLX5_UDP_PORT_GENEVE;
6113 if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
6114 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
6115 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
6120 geneve_m = &rte_flow_item_geneve_mask;
6121 size = sizeof(geneve_m->vni);
6122 vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, geneve_vni);
6123 vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, geneve_vni);
6124 memcpy(vni_m, geneve_m->vni, size);
6125 for (i = 0; i < size; ++i)
6126 vni_v[i] = vni_m[i] & geneve_v->vni[i];
6127 MLX5_SET(fte_match_set_misc, misc_m, geneve_protocol_type,
6128 rte_be_to_cpu_16(geneve_m->protocol));
6129 MLX5_SET(fte_match_set_misc, misc_v, geneve_protocol_type,
6130 rte_be_to_cpu_16(geneve_v->protocol & geneve_m->protocol));
6131 gbhdr_m = rte_be_to_cpu_16(geneve_m->ver_opt_len_o_c_rsvd0);
6132 gbhdr_v = rte_be_to_cpu_16(geneve_v->ver_opt_len_o_c_rsvd0);
6133 MLX5_SET(fte_match_set_misc, misc_m, geneve_oam,
6134 MLX5_GENEVE_OAMF_VAL(gbhdr_m));
6135 MLX5_SET(fte_match_set_misc, misc_v, geneve_oam,
6136 MLX5_GENEVE_OAMF_VAL(gbhdr_v) & MLX5_GENEVE_OAMF_VAL(gbhdr_m));
6137 MLX5_SET(fte_match_set_misc, misc_m, geneve_opt_len,
6138 MLX5_GENEVE_OPTLEN_VAL(gbhdr_m));
6139 MLX5_SET(fte_match_set_misc, misc_v, geneve_opt_len,
6140 MLX5_GENEVE_OPTLEN_VAL(gbhdr_v) &
6141 MLX5_GENEVE_OPTLEN_VAL(gbhdr_m));
6145 * Add MPLS item to matcher and to the value.
6147 * @param[in, out] matcher
6149 * @param[in, out] key
6150 * Flow matcher value.
6152 * Flow pattern to translate.
6153 * @param[in] prev_layer
6154 * The protocol layer indicated in previous item.
6156 * Item is inner pattern.
6159 flow_dv_translate_item_mpls(void *matcher, void *key,
6160 const struct rte_flow_item *item,
6161 uint64_t prev_layer,
6164 const uint32_t *in_mpls_m = item->mask;
6165 const uint32_t *in_mpls_v = item->spec;
6166 uint32_t *out_mpls_m = 0;
6167 uint32_t *out_mpls_v = 0;
6168 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6169 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6170 void *misc2_m = MLX5_ADDR_OF(fte_match_param, matcher,
6172 void *misc2_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
6173 void *headers_m = MLX5_ADDR_OF(fte_match_param, matcher, outer_headers);
6174 void *headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6176 switch (prev_layer) {
6177 case MLX5_FLOW_LAYER_OUTER_L4_UDP:
6178 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xffff);
6179 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
6180 MLX5_UDP_PORT_MPLS);
6182 case MLX5_FLOW_LAYER_GRE:
6183 MLX5_SET(fte_match_set_misc, misc_m, gre_protocol, 0xffff);
6184 MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
6185 RTE_ETHER_TYPE_MPLS);
6188 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
6189 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
6196 in_mpls_m = (const uint32_t *)&rte_flow_item_mpls_mask;
6197 switch (prev_layer) {
6198 case MLX5_FLOW_LAYER_OUTER_L4_UDP:
6200 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
6201 outer_first_mpls_over_udp);
6203 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
6204 outer_first_mpls_over_udp);
6206 case MLX5_FLOW_LAYER_GRE:
6208 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
6209 outer_first_mpls_over_gre);
6211 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
6212 outer_first_mpls_over_gre);
6215 /* Inner MPLS not over GRE is not supported. */
6218 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
6222 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
6228 if (out_mpls_m && out_mpls_v) {
6229 *out_mpls_m = *in_mpls_m;
6230 *out_mpls_v = *in_mpls_v & *in_mpls_m;
6235 * Add metadata register item to matcher
6237 * @param[in, out] matcher
6239 * @param[in, out] key
6240 * Flow matcher value.
6241 * @param[in] reg_type
6242 * Type of device metadata register
6249 flow_dv_match_meta_reg(void *matcher, void *key,
6250 enum modify_reg reg_type,
6251 uint32_t data, uint32_t mask)
6254 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_2);
6256 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
6262 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_a, mask);
6263 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_a, data);
6266 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_b, mask);
6267 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_b, data);
6271 * The metadata register C0 field might be divided into
6272 * source vport index and META item value, we should set
6273 * this field according to specified mask, not as whole one.
6275 temp = MLX5_GET(fte_match_set_misc2, misc2_m, metadata_reg_c_0);
6277 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_0, temp);
6278 temp = MLX5_GET(fte_match_set_misc2, misc2_v, metadata_reg_c_0);
6281 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_0, temp);
6284 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_1, mask);
6285 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_1, data);
6288 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_2, mask);
6289 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_2, data);
6292 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_3, mask);
6293 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_3, data);
6296 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_4, mask);
6297 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_4, data);
6300 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_5, mask);
6301 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_5, data);
6304 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_6, mask);
6305 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_6, data);
6308 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_7, mask);
6309 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_7, data);
6318 * Add MARK item to matcher
6321 * The device to configure through.
6322 * @param[in, out] matcher
6324 * @param[in, out] key
6325 * Flow matcher value.
6327 * Flow pattern to translate.
6330 flow_dv_translate_item_mark(struct rte_eth_dev *dev,
6331 void *matcher, void *key,
6332 const struct rte_flow_item *item)
6334 struct mlx5_priv *priv = dev->data->dev_private;
6335 const struct rte_flow_item_mark *mark;
6339 mark = item->mask ? (const void *)item->mask :
6340 &rte_flow_item_mark_mask;
6341 mask = mark->id & priv->sh->dv_mark_mask;
6342 mark = (const void *)item->spec;
6344 value = mark->id & priv->sh->dv_mark_mask & mask;
6346 enum modify_reg reg;
6348 /* Get the metadata register index for the mark. */
6349 reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, NULL);
6350 MLX5_ASSERT(reg > 0);
6351 if (reg == REG_C_0) {
6352 struct mlx5_priv *priv = dev->data->dev_private;
6353 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
6354 uint32_t shl_c0 = rte_bsf32(msk_c0);
6360 flow_dv_match_meta_reg(matcher, key, reg, value, mask);
6365 * Add META item to matcher
6368 * The devich to configure through.
6369 * @param[in, out] matcher
6371 * @param[in, out] key
6372 * Flow matcher value.
6374 * Attributes of flow that includes this item.
6376 * Flow pattern to translate.
6379 flow_dv_translate_item_meta(struct rte_eth_dev *dev,
6380 void *matcher, void *key,
6381 const struct rte_flow_attr *attr,
6382 const struct rte_flow_item *item)
6384 const struct rte_flow_item_meta *meta_m;
6385 const struct rte_flow_item_meta *meta_v;
6387 meta_m = (const void *)item->mask;
6389 meta_m = &rte_flow_item_meta_mask;
6390 meta_v = (const void *)item->spec;
6393 uint32_t value = meta_v->data;
6394 uint32_t mask = meta_m->data;
6396 reg = flow_dv_get_metadata_reg(dev, attr, NULL);
6400 * In datapath code there is no endianness
6401 * coversions for perfromance reasons, all
6402 * pattern conversions are done in rte_flow.
6404 value = rte_cpu_to_be_32(value);
6405 mask = rte_cpu_to_be_32(mask);
6406 if (reg == REG_C_0) {
6407 struct mlx5_priv *priv = dev->data->dev_private;
6408 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
6409 uint32_t shl_c0 = rte_bsf32(msk_c0);
6410 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
6411 uint32_t shr_c0 = __builtin_clz(priv->sh->dv_meta_mask);
6418 MLX5_ASSERT(msk_c0);
6419 MLX5_ASSERT(!(~msk_c0 & mask));
6421 flow_dv_match_meta_reg(matcher, key, reg, value, mask);
6426 * Add vport metadata Reg C0 item to matcher
6428 * @param[in, out] matcher
6430 * @param[in, out] key
6431 * Flow matcher value.
6433 * Flow pattern to translate.
6436 flow_dv_translate_item_meta_vport(void *matcher, void *key,
6437 uint32_t value, uint32_t mask)
6439 flow_dv_match_meta_reg(matcher, key, REG_C_0, value, mask);
6443 * Add tag item to matcher
6446 * The devich to configure through.
6447 * @param[in, out] matcher
6449 * @param[in, out] key
6450 * Flow matcher value.
6452 * Flow pattern to translate.
6455 flow_dv_translate_mlx5_item_tag(struct rte_eth_dev *dev,
6456 void *matcher, void *key,
6457 const struct rte_flow_item *item)
6459 const struct mlx5_rte_flow_item_tag *tag_v = item->spec;
6460 const struct mlx5_rte_flow_item_tag *tag_m = item->mask;
6461 uint32_t mask, value;
6464 value = tag_v->data;
6465 mask = tag_m ? tag_m->data : UINT32_MAX;
6466 if (tag_v->id == REG_C_0) {
6467 struct mlx5_priv *priv = dev->data->dev_private;
6468 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
6469 uint32_t shl_c0 = rte_bsf32(msk_c0);
6475 flow_dv_match_meta_reg(matcher, key, tag_v->id, value, mask);
6479 * Add TAG item to matcher
6482 * The devich to configure through.
6483 * @param[in, out] matcher
6485 * @param[in, out] key
6486 * Flow matcher value.
6488 * Flow pattern to translate.
6491 flow_dv_translate_item_tag(struct rte_eth_dev *dev,
6492 void *matcher, void *key,
6493 const struct rte_flow_item *item)
6495 const struct rte_flow_item_tag *tag_v = item->spec;
6496 const struct rte_flow_item_tag *tag_m = item->mask;
6497 enum modify_reg reg;
6500 tag_m = tag_m ? tag_m : &rte_flow_item_tag_mask;
6501 /* Get the metadata register index for the tag. */
6502 reg = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, tag_v->index, NULL);
6503 MLX5_ASSERT(reg > 0);
6504 flow_dv_match_meta_reg(matcher, key, reg, tag_v->data, tag_m->data);
6508 * Add source vport match to the specified matcher.
6510 * @param[in, out] matcher
6512 * @param[in, out] key
6513 * Flow matcher value.
6515 * Source vport value to match
6520 flow_dv_translate_item_source_vport(void *matcher, void *key,
6521 int16_t port, uint16_t mask)
6523 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6524 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6526 MLX5_SET(fte_match_set_misc, misc_m, source_port, mask);
6527 MLX5_SET(fte_match_set_misc, misc_v, source_port, port);
6531 * Translate port-id item to eswitch match on port-id.
6534 * The devich to configure through.
6535 * @param[in, out] matcher
6537 * @param[in, out] key
6538 * Flow matcher value.
6540 * Flow pattern to translate.
6543 * 0 on success, a negative errno value otherwise.
6546 flow_dv_translate_item_port_id(struct rte_eth_dev *dev, void *matcher,
6547 void *key, const struct rte_flow_item *item)
6549 const struct rte_flow_item_port_id *pid_m = item ? item->mask : NULL;
6550 const struct rte_flow_item_port_id *pid_v = item ? item->spec : NULL;
6551 struct mlx5_priv *priv;
6554 mask = pid_m ? pid_m->id : 0xffff;
6555 id = pid_v ? pid_v->id : dev->data->port_id;
6556 priv = mlx5_port_to_eswitch_info(id, item == NULL);
6559 /* Translate to vport field or to metadata, depending on mode. */
6560 if (priv->vport_meta_mask)
6561 flow_dv_translate_item_meta_vport(matcher, key,
6562 priv->vport_meta_tag,
6563 priv->vport_meta_mask);
6565 flow_dv_translate_item_source_vport(matcher, key,
6566 priv->vport_id, mask);
6571 * Add ICMP6 item to matcher and to the value.
6573 * @param[in, out] matcher
6575 * @param[in, out] key
6576 * Flow matcher value.
6578 * Flow pattern to translate.
6580 * Item is inner pattern.
6583 flow_dv_translate_item_icmp6(void *matcher, void *key,
6584 const struct rte_flow_item *item,
6587 const struct rte_flow_item_icmp6 *icmp6_m = item->mask;
6588 const struct rte_flow_item_icmp6 *icmp6_v = item->spec;
6591 void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
6593 void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
6595 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6597 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6599 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6601 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6603 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
6604 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMPV6);
6608 icmp6_m = &rte_flow_item_icmp6_mask;
6610 * Force flow only to match the non-fragmented IPv6 ICMPv6 packets.
6611 * If only the protocol is specified, no need to match the frag.
6613 MLX5_SET(fte_match_set_lyr_2_4, headers_m, frag, 1);
6614 MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 0);
6615 MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_type, icmp6_m->type);
6616 MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_type,
6617 icmp6_v->type & icmp6_m->type);
6618 MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_code, icmp6_m->code);
6619 MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_code,
6620 icmp6_v->code & icmp6_m->code);
6624 * Add ICMP item to matcher and to the value.
6626 * @param[in, out] matcher
6628 * @param[in, out] key
6629 * Flow matcher value.
6631 * Flow pattern to translate.
6633 * Item is inner pattern.
6636 flow_dv_translate_item_icmp(void *matcher, void *key,
6637 const struct rte_flow_item *item,
6640 const struct rte_flow_item_icmp *icmp_m = item->mask;
6641 const struct rte_flow_item_icmp *icmp_v = item->spec;
6644 void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
6646 void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
6648 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6650 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6652 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6654 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6656 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
6657 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMP);
6661 icmp_m = &rte_flow_item_icmp_mask;
6663 * Force flow only to match the non-fragmented IPv4 ICMP packets.
6664 * If only the protocol is specified, no need to match the frag.
6666 MLX5_SET(fte_match_set_lyr_2_4, headers_m, frag, 1);
6667 MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 0);
6668 MLX5_SET(fte_match_set_misc3, misc3_m, icmp_type,
6669 icmp_m->hdr.icmp_type);
6670 MLX5_SET(fte_match_set_misc3, misc3_v, icmp_type,
6671 icmp_v->hdr.icmp_type & icmp_m->hdr.icmp_type);
6672 MLX5_SET(fte_match_set_misc3, misc3_m, icmp_code,
6673 icmp_m->hdr.icmp_code);
6674 MLX5_SET(fte_match_set_misc3, misc3_v, icmp_code,
6675 icmp_v->hdr.icmp_code & icmp_m->hdr.icmp_code);
6679 * Add GTP item to matcher and to the value.
6681 * @param[in, out] matcher
6683 * @param[in, out] key
6684 * Flow matcher value.
6686 * Flow pattern to translate.
6688 * Item is inner pattern.
6691 flow_dv_translate_item_gtp(void *matcher, void *key,
6692 const struct rte_flow_item *item, int inner)
6694 const struct rte_flow_item_gtp *gtp_m = item->mask;
6695 const struct rte_flow_item_gtp *gtp_v = item->spec;
6698 void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
6700 void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
6701 uint16_t dport = RTE_GTPU_UDP_PORT;
6704 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6706 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6708 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6710 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6712 if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
6713 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
6714 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
6719 gtp_m = &rte_flow_item_gtp_mask;
6720 MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_msg_type, gtp_m->msg_type);
6721 MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_msg_type,
6722 gtp_v->msg_type & gtp_m->msg_type);
6723 MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_teid,
6724 rte_be_to_cpu_32(gtp_m->teid));
6725 MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_teid,
6726 rte_be_to_cpu_32(gtp_v->teid & gtp_m->teid));
6729 static uint32_t matcher_zero[MLX5_ST_SZ_DW(fte_match_param)] = { 0 };
6731 #define HEADER_IS_ZERO(match_criteria, headers) \
6732 !(memcmp(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
6733 matcher_zero, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
6736 * Calculate flow matcher enable bitmap.
6738 * @param match_criteria
6739 * Pointer to flow matcher criteria.
6742 * Bitmap of enabled fields.
6745 flow_dv_matcher_enable(uint32_t *match_criteria)
6747 uint8_t match_criteria_enable;
6749 match_criteria_enable =
6750 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
6751 MLX5_MATCH_CRITERIA_ENABLE_OUTER_BIT;
6752 match_criteria_enable |=
6753 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
6754 MLX5_MATCH_CRITERIA_ENABLE_MISC_BIT;
6755 match_criteria_enable |=
6756 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
6757 MLX5_MATCH_CRITERIA_ENABLE_INNER_BIT;
6758 match_criteria_enable |=
6759 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
6760 MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
6761 match_criteria_enable |=
6762 (!HEADER_IS_ZERO(match_criteria, misc_parameters_3)) <<
6763 MLX5_MATCH_CRITERIA_ENABLE_MISC3_BIT;
6764 return match_criteria_enable;
6771 * @param[in, out] dev
6772 * Pointer to rte_eth_dev structure.
6773 * @param[in] table_id
6776 * Direction of the table.
6777 * @param[in] transfer
6778 * E-Switch or NIC flow.
6780 * pointer to error structure.
6783 * Returns tables resource based on the index, NULL in case of failed.
6785 static struct mlx5_flow_tbl_resource *
6786 flow_dv_tbl_resource_get(struct rte_eth_dev *dev,
6787 uint32_t table_id, uint8_t egress,
6789 struct rte_flow_error *error)
6791 struct mlx5_priv *priv = dev->data->dev_private;
6792 struct mlx5_ibv_shared *sh = priv->sh;
6793 struct mlx5_flow_tbl_resource *tbl;
6794 union mlx5_flow_tbl_key table_key = {
6796 .table_id = table_id,
6798 .domain = !!transfer,
6799 .direction = !!egress,
6802 struct mlx5_hlist_entry *pos = mlx5_hlist_lookup(sh->flow_tbls,
6804 struct mlx5_flow_tbl_data_entry *tbl_data;
6809 tbl_data = container_of(pos, struct mlx5_flow_tbl_data_entry,
6811 tbl = &tbl_data->tbl;
6812 rte_atomic32_inc(&tbl->refcnt);
6815 tbl_data = rte_zmalloc(NULL, sizeof(*tbl_data), 0);
6817 rte_flow_error_set(error, ENOMEM,
6818 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6820 "cannot allocate flow table data entry");
6823 tbl = &tbl_data->tbl;
6824 pos = &tbl_data->entry;
6826 domain = sh->fdb_domain;
6828 domain = sh->tx_domain;
6830 domain = sh->rx_domain;
6831 tbl->obj = mlx5_glue->dr_create_flow_tbl(domain, table_id);
6833 rte_flow_error_set(error, ENOMEM,
6834 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6835 NULL, "cannot create flow table object");
6840 * No multi-threads now, but still better to initialize the reference
6841 * count before insert it into the hash list.
6843 rte_atomic32_init(&tbl->refcnt);
6844 /* Jump action reference count is initialized here. */
6845 rte_atomic32_init(&tbl_data->jump.refcnt);
6846 pos->key = table_key.v64;
6847 ret = mlx5_hlist_insert(sh->flow_tbls, pos);
6849 rte_flow_error_set(error, -ret,
6850 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
6851 "cannot insert flow table data entry");
6852 mlx5_glue->dr_destroy_flow_tbl(tbl->obj);
6855 rte_atomic32_inc(&tbl->refcnt);
6860 * Release a flow table.
6863 * Pointer to rte_eth_dev structure.
6865 * Table resource to be released.
6868 * Returns 0 if table was released, else return 1;
6871 flow_dv_tbl_resource_release(struct rte_eth_dev *dev,
6872 struct mlx5_flow_tbl_resource *tbl)
6874 struct mlx5_priv *priv = dev->data->dev_private;
6875 struct mlx5_ibv_shared *sh = priv->sh;
6876 struct mlx5_flow_tbl_data_entry *tbl_data =
6877 container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
6881 if (rte_atomic32_dec_and_test(&tbl->refcnt)) {
6882 struct mlx5_hlist_entry *pos = &tbl_data->entry;
6884 mlx5_glue->dr_destroy_flow_tbl(tbl->obj);
6886 /* remove the entry from the hash list and free memory. */
6887 mlx5_hlist_remove(sh->flow_tbls, pos);
6895 * Register the flow matcher.
6897 * @param[in, out] dev
6898 * Pointer to rte_eth_dev structure.
6899 * @param[in, out] matcher
6900 * Pointer to flow matcher.
6901 * @param[in, out] key
6902 * Pointer to flow table key.
6903 * @parm[in, out] dev_flow
6904 * Pointer to the dev_flow.
6906 * pointer to error structure.
6909 * 0 on success otherwise -errno and errno is set.
6912 flow_dv_matcher_register(struct rte_eth_dev *dev,
6913 struct mlx5_flow_dv_matcher *matcher,
6914 union mlx5_flow_tbl_key *key,
6915 struct mlx5_flow *dev_flow,
6916 struct rte_flow_error *error)
6918 struct mlx5_priv *priv = dev->data->dev_private;
6919 struct mlx5_ibv_shared *sh = priv->sh;
6920 struct mlx5_flow_dv_matcher *cache_matcher;
6921 struct mlx5dv_flow_matcher_attr dv_attr = {
6922 .type = IBV_FLOW_ATTR_NORMAL,
6923 .match_mask = (void *)&matcher->mask,
6925 struct mlx5_flow_tbl_resource *tbl;
6926 struct mlx5_flow_tbl_data_entry *tbl_data;
6928 tbl = flow_dv_tbl_resource_get(dev, key->table_id, key->direction,
6929 key->domain, error);
6931 return -rte_errno; /* No need to refill the error info */
6932 tbl_data = container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
6933 /* Lookup from cache. */
6934 LIST_FOREACH(cache_matcher, &tbl_data->matchers, next) {
6935 if (matcher->crc == cache_matcher->crc &&
6936 matcher->priority == cache_matcher->priority &&
6937 !memcmp((const void *)matcher->mask.buf,
6938 (const void *)cache_matcher->mask.buf,
6939 cache_matcher->mask.size)) {
6941 "%s group %u priority %hd use %s "
6942 "matcher %p: refcnt %d++",
6943 key->domain ? "FDB" : "NIC", key->table_id,
6944 cache_matcher->priority,
6945 key->direction ? "tx" : "rx",
6946 (void *)cache_matcher,
6947 rte_atomic32_read(&cache_matcher->refcnt));
6948 rte_atomic32_inc(&cache_matcher->refcnt);
6949 dev_flow->dv.matcher = cache_matcher;
6950 /* old matcher should not make the table ref++. */
6951 flow_dv_tbl_resource_release(dev, tbl);
6955 /* Register new matcher. */
6956 cache_matcher = rte_calloc(__func__, 1, sizeof(*cache_matcher), 0);
6957 if (!cache_matcher) {
6958 flow_dv_tbl_resource_release(dev, tbl);
6959 return rte_flow_error_set(error, ENOMEM,
6960 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
6961 "cannot allocate matcher memory");
6963 *cache_matcher = *matcher;
6964 dv_attr.match_criteria_enable =
6965 flow_dv_matcher_enable(cache_matcher->mask.buf);
6966 dv_attr.priority = matcher->priority;
6968 dv_attr.flags |= IBV_FLOW_ATTR_FLAGS_EGRESS;
6969 cache_matcher->matcher_object =
6970 mlx5_glue->dv_create_flow_matcher(sh->ctx, &dv_attr, tbl->obj);
6971 if (!cache_matcher->matcher_object) {
6972 rte_free(cache_matcher);
6973 #ifdef HAVE_MLX5DV_DR
6974 flow_dv_tbl_resource_release(dev, tbl);
6976 return rte_flow_error_set(error, ENOMEM,
6977 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6978 NULL, "cannot create matcher");
6980 /* Save the table information */
6981 cache_matcher->tbl = tbl;
6982 rte_atomic32_init(&cache_matcher->refcnt);
6983 /* only matcher ref++, table ref++ already done above in get API. */
6984 rte_atomic32_inc(&cache_matcher->refcnt);
6985 LIST_INSERT_HEAD(&tbl_data->matchers, cache_matcher, next);
6986 dev_flow->dv.matcher = cache_matcher;
6987 DRV_LOG(DEBUG, "%s group %u priority %hd new %s matcher %p: refcnt %d",
6988 key->domain ? "FDB" : "NIC", key->table_id,
6989 cache_matcher->priority,
6990 key->direction ? "tx" : "rx", (void *)cache_matcher,
6991 rte_atomic32_read(&cache_matcher->refcnt));
6996 * Find existing tag resource or create and register a new one.
6998 * @param dev[in, out]
6999 * Pointer to rte_eth_dev structure.
7000 * @param[in, out] tag_be24
7001 * Tag value in big endian then R-shift 8.
7002 * @parm[in, out] dev_flow
7003 * Pointer to the dev_flow.
7005 * pointer to error structure.
7008 * 0 on success otherwise -errno and errno is set.
7011 flow_dv_tag_resource_register
7012 (struct rte_eth_dev *dev,
7014 struct mlx5_flow *dev_flow,
7015 struct rte_flow_error *error)
7017 struct mlx5_priv *priv = dev->data->dev_private;
7018 struct mlx5_ibv_shared *sh = priv->sh;
7019 struct mlx5_flow_dv_tag_resource *cache_resource;
7020 struct mlx5_hlist_entry *entry;
7022 /* Lookup a matching resource from cache. */
7023 entry = mlx5_hlist_lookup(sh->tag_table, (uint64_t)tag_be24);
7025 cache_resource = container_of
7026 (entry, struct mlx5_flow_dv_tag_resource, entry);
7027 rte_atomic32_inc(&cache_resource->refcnt);
7028 dev_flow->dv.tag_resource = cache_resource;
7029 DRV_LOG(DEBUG, "cached tag resource %p: refcnt now %d++",
7030 (void *)cache_resource,
7031 rte_atomic32_read(&cache_resource->refcnt));
7034 /* Register new resource. */
7035 cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
7036 if (!cache_resource)
7037 return rte_flow_error_set(error, ENOMEM,
7038 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
7039 "cannot allocate resource memory");
7040 cache_resource->entry.key = (uint64_t)tag_be24;
7041 cache_resource->action = mlx5_glue->dv_create_flow_action_tag(tag_be24);
7042 if (!cache_resource->action) {
7043 rte_free(cache_resource);
7044 return rte_flow_error_set(error, ENOMEM,
7045 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7046 NULL, "cannot create action");
7048 rte_atomic32_init(&cache_resource->refcnt);
7049 rte_atomic32_inc(&cache_resource->refcnt);
7050 if (mlx5_hlist_insert(sh->tag_table, &cache_resource->entry)) {
7051 mlx5_glue->destroy_flow_action(cache_resource->action);
7052 rte_free(cache_resource);
7053 return rte_flow_error_set(error, EEXIST,
7054 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7055 NULL, "cannot insert tag");
7057 dev_flow->dv.tag_resource = cache_resource;
7058 DRV_LOG(DEBUG, "new tag resource %p: refcnt now %d++",
7059 (void *)cache_resource,
7060 rte_atomic32_read(&cache_resource->refcnt));
7068 * Pointer to Ethernet device.
7070 * Pointer to mlx5_flow.
7073 * 1 while a reference on it exists, 0 when freed.
7076 flow_dv_tag_release(struct rte_eth_dev *dev,
7077 struct mlx5_flow_dv_tag_resource *tag)
7079 struct mlx5_priv *priv = dev->data->dev_private;
7080 struct mlx5_ibv_shared *sh = priv->sh;
7083 DRV_LOG(DEBUG, "port %u tag %p: refcnt %d--",
7084 dev->data->port_id, (void *)tag,
7085 rte_atomic32_read(&tag->refcnt));
7086 if (rte_atomic32_dec_and_test(&tag->refcnt)) {
7087 claim_zero(mlx5_glue->destroy_flow_action(tag->action));
7088 mlx5_hlist_remove(sh->tag_table, &tag->entry);
7089 DRV_LOG(DEBUG, "port %u tag %p: removed",
7090 dev->data->port_id, (void *)tag);
7098 * Translate port ID action to vport.
7101 * Pointer to rte_eth_dev structure.
7103 * Pointer to the port ID action.
7104 * @param[out] dst_port_id
7105 * The target port ID.
7107 * Pointer to the error structure.
7110 * 0 on success, a negative errno value otherwise and rte_errno is set.
7113 flow_dv_translate_action_port_id(struct rte_eth_dev *dev,
7114 const struct rte_flow_action *action,
7115 uint32_t *dst_port_id,
7116 struct rte_flow_error *error)
7119 struct mlx5_priv *priv;
7120 const struct rte_flow_action_port_id *conf =
7121 (const struct rte_flow_action_port_id *)action->conf;
7123 port = conf->original ? dev->data->port_id : conf->id;
7124 priv = mlx5_port_to_eswitch_info(port, false);
7126 return rte_flow_error_set(error, -rte_errno,
7127 RTE_FLOW_ERROR_TYPE_ACTION,
7129 "No eswitch info was found for port");
7130 #ifdef HAVE_MLX5DV_DR_DEVX_PORT
7132 * This parameter is transferred to
7133 * mlx5dv_dr_action_create_dest_ib_port().
7135 *dst_port_id = priv->ibv_port;
7138 * Legacy mode, no LAG configurations is supported.
7139 * This parameter is transferred to
7140 * mlx5dv_dr_action_create_dest_vport().
7142 *dst_port_id = priv->vport_id;
7148 * Add Tx queue matcher
7151 * Pointer to the dev struct.
7152 * @param[in, out] matcher
7154 * @param[in, out] key
7155 * Flow matcher value.
7157 * Flow pattern to translate.
7159 * Item is inner pattern.
7162 flow_dv_translate_item_tx_queue(struct rte_eth_dev *dev,
7163 void *matcher, void *key,
7164 const struct rte_flow_item *item)
7166 const struct mlx5_rte_flow_item_tx_queue *queue_m;
7167 const struct mlx5_rte_flow_item_tx_queue *queue_v;
7169 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
7171 MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
7172 struct mlx5_txq_ctrl *txq;
7176 queue_m = (const void *)item->mask;
7179 queue_v = (const void *)item->spec;
7182 txq = mlx5_txq_get(dev, queue_v->queue);
7185 queue = txq->obj->sq->id;
7186 MLX5_SET(fte_match_set_misc, misc_m, source_sqn, queue_m->queue);
7187 MLX5_SET(fte_match_set_misc, misc_v, source_sqn,
7188 queue & queue_m->queue);
7189 mlx5_txq_release(dev, queue_v->queue);
7193 * Set the hash fields according to the @p flow information.
7195 * @param[in] dev_flow
7196 * Pointer to the mlx5_flow.
7199 flow_dv_hashfields_set(struct mlx5_flow *dev_flow)
7201 struct rte_flow *flow = dev_flow->flow;
7202 uint64_t items = dev_flow->layers;
7204 uint64_t rss_types = rte_eth_rss_hf_refine(flow->rss.types);
7206 dev_flow->hash_fields = 0;
7207 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
7208 if (flow->rss.level >= 2) {
7209 dev_flow->hash_fields |= IBV_RX_HASH_INNER;
7213 if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L3_IPV4)) ||
7214 (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L3_IPV4))) {
7215 if (rss_types & MLX5_IPV4_LAYER_TYPES) {
7216 if (rss_types & ETH_RSS_L3_SRC_ONLY)
7217 dev_flow->hash_fields |= IBV_RX_HASH_SRC_IPV4;
7218 else if (rss_types & ETH_RSS_L3_DST_ONLY)
7219 dev_flow->hash_fields |= IBV_RX_HASH_DST_IPV4;
7221 dev_flow->hash_fields |= MLX5_IPV4_IBV_RX_HASH;
7223 } else if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L3_IPV6)) ||
7224 (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L3_IPV6))) {
7225 if (rss_types & MLX5_IPV6_LAYER_TYPES) {
7226 if (rss_types & ETH_RSS_L3_SRC_ONLY)
7227 dev_flow->hash_fields |= IBV_RX_HASH_SRC_IPV6;
7228 else if (rss_types & ETH_RSS_L3_DST_ONLY)
7229 dev_flow->hash_fields |= IBV_RX_HASH_DST_IPV6;
7231 dev_flow->hash_fields |= MLX5_IPV6_IBV_RX_HASH;
7234 if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L4_UDP)) ||
7235 (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L4_UDP))) {
7236 if (rss_types & ETH_RSS_UDP) {
7237 if (rss_types & ETH_RSS_L4_SRC_ONLY)
7238 dev_flow->hash_fields |=
7239 IBV_RX_HASH_SRC_PORT_UDP;
7240 else if (rss_types & ETH_RSS_L4_DST_ONLY)
7241 dev_flow->hash_fields |=
7242 IBV_RX_HASH_DST_PORT_UDP;
7244 dev_flow->hash_fields |= MLX5_UDP_IBV_RX_HASH;
7246 } else if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L4_TCP)) ||
7247 (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L4_TCP))) {
7248 if (rss_types & ETH_RSS_TCP) {
7249 if (rss_types & ETH_RSS_L4_SRC_ONLY)
7250 dev_flow->hash_fields |=
7251 IBV_RX_HASH_SRC_PORT_TCP;
7252 else if (rss_types & ETH_RSS_L4_DST_ONLY)
7253 dev_flow->hash_fields |=
7254 IBV_RX_HASH_DST_PORT_TCP;
7256 dev_flow->hash_fields |= MLX5_TCP_IBV_RX_HASH;
7262 * Fill the flow with DV spec, lock free
7263 * (mutex should be acquired by caller).
7266 * Pointer to rte_eth_dev structure.
7267 * @param[in, out] dev_flow
7268 * Pointer to the sub flow.
7270 * Pointer to the flow attributes.
7272 * Pointer to the list of items.
7273 * @param[in] actions
7274 * Pointer to the list of actions.
7276 * Pointer to the error structure.
7279 * 0 on success, a negative errno value otherwise and rte_errno is set.
7282 __flow_dv_translate(struct rte_eth_dev *dev,
7283 struct mlx5_flow *dev_flow,
7284 const struct rte_flow_attr *attr,
7285 const struct rte_flow_item items[],
7286 const struct rte_flow_action actions[],
7287 struct rte_flow_error *error)
7289 struct mlx5_priv *priv = dev->data->dev_private;
7290 struct mlx5_dev_config *dev_conf = &priv->config;
7291 struct rte_flow *flow = dev_flow->flow;
7292 uint64_t item_flags = 0;
7293 uint64_t last_item = 0;
7294 uint64_t action_flags = 0;
7295 uint64_t priority = attr->priority;
7296 struct mlx5_flow_dv_matcher matcher = {
7298 .size = sizeof(matcher.mask.buf),
7302 bool actions_end = false;
7304 struct mlx5_flow_dv_modify_hdr_resource res;
7305 uint8_t len[sizeof(struct mlx5_flow_dv_modify_hdr_resource) +
7306 sizeof(struct mlx5_modification_cmd) *
7307 (MLX5_MAX_MODIFY_NUM + 1)];
7309 struct mlx5_flow_dv_modify_hdr_resource *mhdr_res = &mhdr_dummy.res;
7310 union flow_dv_attr flow_attr = { .attr = 0 };
7312 union mlx5_flow_tbl_key tbl_key;
7313 uint32_t modify_action_position = UINT32_MAX;
7314 void *match_mask = matcher.mask.buf;
7315 void *match_value = dev_flow->dv.value.buf;
7316 uint8_t next_protocol = 0xff;
7317 struct rte_vlan_hdr vlan = { 0 };
7321 mhdr_res->ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
7322 MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
7323 ret = mlx5_flow_group_to_table(attr, dev_flow->external, attr->group,
7324 !!priv->fdb_def_rule, &table, error);
7327 dev_flow->group = table;
7329 mhdr_res->ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
7330 if (priority == MLX5_FLOW_PRIO_RSVD)
7331 priority = dev_conf->flow_prio - 1;
7332 /* number of actions must be set to 0 in case of dirty stack. */
7333 mhdr_res->actions_num = 0;
7334 for (; !actions_end ; actions++) {
7335 const struct rte_flow_action_queue *queue;
7336 const struct rte_flow_action_rss *rss;
7337 const struct rte_flow_action *action = actions;
7338 const struct rte_flow_action_count *count = action->conf;
7339 const uint8_t *rss_key;
7340 const struct rte_flow_action_jump *jump_data;
7341 const struct rte_flow_action_meter *mtr;
7342 struct mlx5_flow_tbl_resource *tbl;
7343 uint32_t port_id = 0;
7344 struct mlx5_flow_dv_port_id_action_resource port_id_resource;
7345 int action_type = actions->type;
7346 const struct rte_flow_action *found_action = NULL;
7348 switch (action_type) {
7349 case RTE_FLOW_ACTION_TYPE_VOID:
7351 case RTE_FLOW_ACTION_TYPE_PORT_ID:
7352 if (flow_dv_translate_action_port_id(dev, action,
7355 port_id_resource.port_id = port_id;
7356 if (flow_dv_port_id_action_resource_register
7357 (dev, &port_id_resource, dev_flow, error))
7359 dev_flow->dv.actions[actions_n++] =
7360 dev_flow->dv.port_id_action->action;
7361 action_flags |= MLX5_FLOW_ACTION_PORT_ID;
7363 case RTE_FLOW_ACTION_TYPE_FLAG:
7364 action_flags |= MLX5_FLOW_ACTION_FLAG;
7365 if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
7366 struct rte_flow_action_mark mark = {
7367 .id = MLX5_FLOW_MARK_DEFAULT,
7370 if (flow_dv_convert_action_mark(dev, &mark,
7374 action_flags |= MLX5_FLOW_ACTION_MARK_EXT;
7377 tag_be = mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT);
7378 if (!dev_flow->dv.tag_resource)
7379 if (flow_dv_tag_resource_register
7380 (dev, tag_be, dev_flow, error))
7382 dev_flow->dv.actions[actions_n++] =
7383 dev_flow->dv.tag_resource->action;
7385 case RTE_FLOW_ACTION_TYPE_MARK:
7386 action_flags |= MLX5_FLOW_ACTION_MARK;
7387 if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
7388 const struct rte_flow_action_mark *mark =
7389 (const struct rte_flow_action_mark *)
7392 if (flow_dv_convert_action_mark(dev, mark,
7396 action_flags |= MLX5_FLOW_ACTION_MARK_EXT;
7400 case MLX5_RTE_FLOW_ACTION_TYPE_MARK:
7401 /* Legacy (non-extensive) MARK action. */
7402 tag_be = mlx5_flow_mark_set
7403 (((const struct rte_flow_action_mark *)
7404 (actions->conf))->id);
7405 if (!dev_flow->dv.tag_resource)
7406 if (flow_dv_tag_resource_register
7407 (dev, tag_be, dev_flow, error))
7409 dev_flow->dv.actions[actions_n++] =
7410 dev_flow->dv.tag_resource->action;
7412 case RTE_FLOW_ACTION_TYPE_SET_META:
7413 if (flow_dv_convert_action_set_meta
7414 (dev, mhdr_res, attr,
7415 (const struct rte_flow_action_set_meta *)
7416 actions->conf, error))
7418 action_flags |= MLX5_FLOW_ACTION_SET_META;
7420 case RTE_FLOW_ACTION_TYPE_SET_TAG:
7421 if (flow_dv_convert_action_set_tag
7423 (const struct rte_flow_action_set_tag *)
7424 actions->conf, error))
7426 action_flags |= MLX5_FLOW_ACTION_SET_TAG;
7428 case RTE_FLOW_ACTION_TYPE_DROP:
7429 action_flags |= MLX5_FLOW_ACTION_DROP;
7431 case RTE_FLOW_ACTION_TYPE_QUEUE:
7432 MLX5_ASSERT(flow->rss.queue);
7433 queue = actions->conf;
7434 flow->rss.queue_num = 1;
7435 (*flow->rss.queue)[0] = queue->index;
7436 action_flags |= MLX5_FLOW_ACTION_QUEUE;
7438 case RTE_FLOW_ACTION_TYPE_RSS:
7439 MLX5_ASSERT(flow->rss.queue);
7440 rss = actions->conf;
7441 if (flow->rss.queue)
7442 memcpy((*flow->rss.queue), rss->queue,
7443 rss->queue_num * sizeof(uint16_t));
7444 flow->rss.queue_num = rss->queue_num;
7445 /* NULL RSS key indicates default RSS key. */
7446 rss_key = !rss->key ? rss_hash_default_key : rss->key;
7447 memcpy(flow->rss.key, rss_key, MLX5_RSS_HASH_KEY_LEN);
7449 * rss->level and rss.types should be set in advance
7450 * when expanding items for RSS.
7452 action_flags |= MLX5_FLOW_ACTION_RSS;
7454 case RTE_FLOW_ACTION_TYPE_COUNT:
7455 if (!dev_conf->devx) {
7456 rte_errno = ENOTSUP;
7459 flow->counter = flow_dv_counter_alloc(dev,
7463 if (flow->counter == NULL)
7465 dev_flow->dv.actions[actions_n++] =
7466 flow->counter->action;
7467 action_flags |= MLX5_FLOW_ACTION_COUNT;
7470 if (rte_errno == ENOTSUP)
7471 return rte_flow_error_set
7473 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7475 "count action not supported");
7477 return rte_flow_error_set
7479 RTE_FLOW_ERROR_TYPE_ACTION,
7481 "cannot create counter"
7484 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
7485 dev_flow->dv.actions[actions_n++] =
7486 priv->sh->pop_vlan_action;
7487 action_flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
7489 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
7490 flow_dev_get_vlan_info_from_items(items, &vlan);
7491 vlan.eth_proto = rte_be_to_cpu_16
7492 ((((const struct rte_flow_action_of_push_vlan *)
7493 actions->conf)->ethertype));
7494 found_action = mlx5_flow_find_action
7496 RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID);
7498 mlx5_update_vlan_vid_pcp(found_action, &vlan);
7499 found_action = mlx5_flow_find_action
7501 RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP);
7503 mlx5_update_vlan_vid_pcp(found_action, &vlan);
7504 if (flow_dv_create_action_push_vlan
7505 (dev, attr, &vlan, dev_flow, error))
7507 dev_flow->dv.actions[actions_n++] =
7508 dev_flow->dv.push_vlan_res->action;
7509 action_flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
7511 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
7512 /* of_vlan_push action handled this action */
7513 MLX5_ASSERT(action_flags &
7514 MLX5_FLOW_ACTION_OF_PUSH_VLAN);
7516 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
7517 if (action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN)
7519 flow_dev_get_vlan_info_from_items(items, &vlan);
7520 mlx5_update_vlan_vid_pcp(actions, &vlan);
7521 /* If no VLAN push - this is a modify header action */
7522 if (flow_dv_convert_action_modify_vlan_vid
7523 (mhdr_res, actions, error))
7525 action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
7527 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
7528 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
7529 if (flow_dv_create_action_l2_encap(dev, actions,
7534 dev_flow->dv.actions[actions_n++] =
7535 dev_flow->dv.encap_decap->verbs_action;
7536 action_flags |= MLX5_FLOW_ACTION_ENCAP;
7538 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
7539 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
7540 if (flow_dv_create_action_l2_decap(dev, dev_flow,
7544 dev_flow->dv.actions[actions_n++] =
7545 dev_flow->dv.encap_decap->verbs_action;
7546 action_flags |= MLX5_FLOW_ACTION_DECAP;
7548 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
7549 /* Handle encap with preceding decap. */
7550 if (action_flags & MLX5_FLOW_ACTION_DECAP) {
7551 if (flow_dv_create_action_raw_encap
7552 (dev, actions, dev_flow, attr, error))
7554 dev_flow->dv.actions[actions_n++] =
7555 dev_flow->dv.encap_decap->verbs_action;
7557 /* Handle encap without preceding decap. */
7558 if (flow_dv_create_action_l2_encap
7559 (dev, actions, dev_flow, attr->transfer,
7562 dev_flow->dv.actions[actions_n++] =
7563 dev_flow->dv.encap_decap->verbs_action;
7565 action_flags |= MLX5_FLOW_ACTION_ENCAP;
7567 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
7568 while ((++action)->type == RTE_FLOW_ACTION_TYPE_VOID)
7570 if (action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
7571 if (flow_dv_create_action_l2_decap
7572 (dev, dev_flow, attr->transfer, error))
7574 dev_flow->dv.actions[actions_n++] =
7575 dev_flow->dv.encap_decap->verbs_action;
7577 /* If decap is followed by encap, handle it at encap. */
7578 action_flags |= MLX5_FLOW_ACTION_DECAP;
7580 case RTE_FLOW_ACTION_TYPE_JUMP:
7581 jump_data = action->conf;
7582 ret = mlx5_flow_group_to_table(attr, dev_flow->external,
7584 !!priv->fdb_def_rule,
7588 tbl = flow_dv_tbl_resource_get(dev, table,
7590 attr->transfer, error);
7592 return rte_flow_error_set
7594 RTE_FLOW_ERROR_TYPE_ACTION,
7596 "cannot create jump action.");
7597 if (flow_dv_jump_tbl_resource_register
7598 (dev, tbl, dev_flow, error)) {
7599 flow_dv_tbl_resource_release(dev, tbl);
7600 return rte_flow_error_set
7602 RTE_FLOW_ERROR_TYPE_ACTION,
7604 "cannot create jump action.");
7606 dev_flow->dv.actions[actions_n++] =
7607 dev_flow->dv.jump->action;
7608 action_flags |= MLX5_FLOW_ACTION_JUMP;
7610 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
7611 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
7612 if (flow_dv_convert_action_modify_mac
7613 (mhdr_res, actions, error))
7615 action_flags |= actions->type ==
7616 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
7617 MLX5_FLOW_ACTION_SET_MAC_SRC :
7618 MLX5_FLOW_ACTION_SET_MAC_DST;
7620 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
7621 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
7622 if (flow_dv_convert_action_modify_ipv4
7623 (mhdr_res, actions, error))
7625 action_flags |= actions->type ==
7626 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
7627 MLX5_FLOW_ACTION_SET_IPV4_SRC :
7628 MLX5_FLOW_ACTION_SET_IPV4_DST;
7630 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
7631 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
7632 if (flow_dv_convert_action_modify_ipv6
7633 (mhdr_res, actions, error))
7635 action_flags |= actions->type ==
7636 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
7637 MLX5_FLOW_ACTION_SET_IPV6_SRC :
7638 MLX5_FLOW_ACTION_SET_IPV6_DST;
7640 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
7641 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
7642 if (flow_dv_convert_action_modify_tp
7643 (mhdr_res, actions, items,
7644 &flow_attr, dev_flow, !!(action_flags &
7645 MLX5_FLOW_ACTION_DECAP), error))
7647 action_flags |= actions->type ==
7648 RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
7649 MLX5_FLOW_ACTION_SET_TP_SRC :
7650 MLX5_FLOW_ACTION_SET_TP_DST;
7652 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
7653 if (flow_dv_convert_action_modify_dec_ttl
7654 (mhdr_res, items, &flow_attr, dev_flow,
7656 MLX5_FLOW_ACTION_DECAP), error))
7658 action_flags |= MLX5_FLOW_ACTION_DEC_TTL;
7660 case RTE_FLOW_ACTION_TYPE_SET_TTL:
7661 if (flow_dv_convert_action_modify_ttl
7662 (mhdr_res, actions, items, &flow_attr,
7663 dev_flow, !!(action_flags &
7664 MLX5_FLOW_ACTION_DECAP), error))
7666 action_flags |= MLX5_FLOW_ACTION_SET_TTL;
7668 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
7669 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
7670 if (flow_dv_convert_action_modify_tcp_seq
7671 (mhdr_res, actions, error))
7673 action_flags |= actions->type ==
7674 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
7675 MLX5_FLOW_ACTION_INC_TCP_SEQ :
7676 MLX5_FLOW_ACTION_DEC_TCP_SEQ;
7679 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
7680 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
7681 if (flow_dv_convert_action_modify_tcp_ack
7682 (mhdr_res, actions, error))
7684 action_flags |= actions->type ==
7685 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
7686 MLX5_FLOW_ACTION_INC_TCP_ACK :
7687 MLX5_FLOW_ACTION_DEC_TCP_ACK;
7689 case MLX5_RTE_FLOW_ACTION_TYPE_TAG:
7690 if (flow_dv_convert_action_set_reg
7691 (mhdr_res, actions, error))
7693 action_flags |= MLX5_FLOW_ACTION_SET_TAG;
7695 case MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG:
7696 if (flow_dv_convert_action_copy_mreg
7697 (dev, mhdr_res, actions, error))
7699 action_flags |= MLX5_FLOW_ACTION_SET_TAG;
7701 case RTE_FLOW_ACTION_TYPE_METER:
7702 mtr = actions->conf;
7704 flow->meter = mlx5_flow_meter_attach(priv,
7708 return rte_flow_error_set(error,
7710 RTE_FLOW_ERROR_TYPE_ACTION,
7713 "or invalid parameters");
7715 /* Set the meter action. */
7716 dev_flow->dv.actions[actions_n++] =
7717 flow->meter->mfts->meter_action;
7718 action_flags |= MLX5_FLOW_ACTION_METER;
7720 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
7721 if (flow_dv_convert_action_modify_ipv4_dscp(mhdr_res,
7724 action_flags |= MLX5_FLOW_ACTION_SET_IPV4_DSCP;
7726 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
7727 if (flow_dv_convert_action_modify_ipv6_dscp(mhdr_res,
7730 action_flags |= MLX5_FLOW_ACTION_SET_IPV6_DSCP;
7732 case RTE_FLOW_ACTION_TYPE_END:
7734 if (mhdr_res->actions_num) {
7735 /* create modify action if needed. */
7736 if (flow_dv_modify_hdr_resource_register
7737 (dev, mhdr_res, dev_flow, error))
7739 dev_flow->dv.actions[modify_action_position] =
7740 dev_flow->dv.modify_hdr->verbs_action;
7746 if (mhdr_res->actions_num &&
7747 modify_action_position == UINT32_MAX)
7748 modify_action_position = actions_n++;
7750 dev_flow->dv.actions_n = actions_n;
7751 dev_flow->actions = action_flags;
7752 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
7753 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
7754 int item_type = items->type;
7756 switch (item_type) {
7757 case RTE_FLOW_ITEM_TYPE_PORT_ID:
7758 flow_dv_translate_item_port_id(dev, match_mask,
7759 match_value, items);
7760 last_item = MLX5_FLOW_ITEM_PORT_ID;
7762 case RTE_FLOW_ITEM_TYPE_ETH:
7763 flow_dv_translate_item_eth(match_mask, match_value,
7765 matcher.priority = MLX5_PRIORITY_MAP_L2;
7766 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
7767 MLX5_FLOW_LAYER_OUTER_L2;
7769 case RTE_FLOW_ITEM_TYPE_VLAN:
7770 flow_dv_translate_item_vlan(dev_flow,
7771 match_mask, match_value,
7773 matcher.priority = MLX5_PRIORITY_MAP_L2;
7774 last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
7775 MLX5_FLOW_LAYER_INNER_VLAN) :
7776 (MLX5_FLOW_LAYER_OUTER_L2 |
7777 MLX5_FLOW_LAYER_OUTER_VLAN);
7779 case RTE_FLOW_ITEM_TYPE_IPV4:
7780 mlx5_flow_tunnel_ip_check(items, next_protocol,
7781 &item_flags, &tunnel);
7782 flow_dv_translate_item_ipv4(match_mask, match_value,
7783 items, item_flags, tunnel,
7785 matcher.priority = MLX5_PRIORITY_MAP_L3;
7786 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
7787 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
7788 if (items->mask != NULL &&
7789 ((const struct rte_flow_item_ipv4 *)
7790 items->mask)->hdr.next_proto_id) {
7792 ((const struct rte_flow_item_ipv4 *)
7793 (items->spec))->hdr.next_proto_id;
7795 ((const struct rte_flow_item_ipv4 *)
7796 (items->mask))->hdr.next_proto_id;
7798 /* Reset for inner layer. */
7799 next_protocol = 0xff;
7802 case RTE_FLOW_ITEM_TYPE_IPV6:
7803 mlx5_flow_tunnel_ip_check(items, next_protocol,
7804 &item_flags, &tunnel);
7805 flow_dv_translate_item_ipv6(match_mask, match_value,
7806 items, item_flags, tunnel,
7808 matcher.priority = MLX5_PRIORITY_MAP_L3;
7809 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
7810 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
7811 if (items->mask != NULL &&
7812 ((const struct rte_flow_item_ipv6 *)
7813 items->mask)->hdr.proto) {
7815 ((const struct rte_flow_item_ipv6 *)
7816 items->spec)->hdr.proto;
7818 ((const struct rte_flow_item_ipv6 *)
7819 items->mask)->hdr.proto;
7821 /* Reset for inner layer. */
7822 next_protocol = 0xff;
7825 case RTE_FLOW_ITEM_TYPE_TCP:
7826 flow_dv_translate_item_tcp(match_mask, match_value,
7828 matcher.priority = MLX5_PRIORITY_MAP_L4;
7829 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
7830 MLX5_FLOW_LAYER_OUTER_L4_TCP;
7832 case RTE_FLOW_ITEM_TYPE_UDP:
7833 flow_dv_translate_item_udp(match_mask, match_value,
7835 matcher.priority = MLX5_PRIORITY_MAP_L4;
7836 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
7837 MLX5_FLOW_LAYER_OUTER_L4_UDP;
7839 case RTE_FLOW_ITEM_TYPE_GRE:
7840 flow_dv_translate_item_gre(match_mask, match_value,
7842 matcher.priority = flow->rss.level >= 2 ?
7843 MLX5_PRIORITY_MAP_L2 : MLX5_PRIORITY_MAP_L4;
7844 last_item = MLX5_FLOW_LAYER_GRE;
7846 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
7847 flow_dv_translate_item_gre_key(match_mask,
7848 match_value, items);
7849 last_item = MLX5_FLOW_LAYER_GRE_KEY;
7851 case RTE_FLOW_ITEM_TYPE_NVGRE:
7852 flow_dv_translate_item_nvgre(match_mask, match_value,
7854 matcher.priority = flow->rss.level >= 2 ?
7855 MLX5_PRIORITY_MAP_L2 : MLX5_PRIORITY_MAP_L4;
7856 last_item = MLX5_FLOW_LAYER_GRE;
7858 case RTE_FLOW_ITEM_TYPE_VXLAN:
7859 flow_dv_translate_item_vxlan(match_mask, match_value,
7861 matcher.priority = flow->rss.level >= 2 ?
7862 MLX5_PRIORITY_MAP_L2 : MLX5_PRIORITY_MAP_L4;
7863 last_item = MLX5_FLOW_LAYER_VXLAN;
7865 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
7866 flow_dv_translate_item_vxlan_gpe(match_mask,
7869 matcher.priority = flow->rss.level >= 2 ?
7870 MLX5_PRIORITY_MAP_L2 : MLX5_PRIORITY_MAP_L4;
7871 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
7873 case RTE_FLOW_ITEM_TYPE_GENEVE:
7874 flow_dv_translate_item_geneve(match_mask, match_value,
7876 matcher.priority = flow->rss.level >= 2 ?
7877 MLX5_PRIORITY_MAP_L2 : MLX5_PRIORITY_MAP_L4;
7878 last_item = MLX5_FLOW_LAYER_GENEVE;
7880 case RTE_FLOW_ITEM_TYPE_MPLS:
7881 flow_dv_translate_item_mpls(match_mask, match_value,
7882 items, last_item, tunnel);
7883 matcher.priority = flow->rss.level >= 2 ?
7884 MLX5_PRIORITY_MAP_L2 : MLX5_PRIORITY_MAP_L4;
7885 last_item = MLX5_FLOW_LAYER_MPLS;
7887 case RTE_FLOW_ITEM_TYPE_MARK:
7888 flow_dv_translate_item_mark(dev, match_mask,
7889 match_value, items);
7890 last_item = MLX5_FLOW_ITEM_MARK;
7892 case RTE_FLOW_ITEM_TYPE_META:
7893 flow_dv_translate_item_meta(dev, match_mask,
7894 match_value, attr, items);
7895 last_item = MLX5_FLOW_ITEM_METADATA;
7897 case RTE_FLOW_ITEM_TYPE_ICMP:
7898 flow_dv_translate_item_icmp(match_mask, match_value,
7900 last_item = MLX5_FLOW_LAYER_ICMP;
7902 case RTE_FLOW_ITEM_TYPE_ICMP6:
7903 flow_dv_translate_item_icmp6(match_mask, match_value,
7905 last_item = MLX5_FLOW_LAYER_ICMP6;
7907 case RTE_FLOW_ITEM_TYPE_TAG:
7908 flow_dv_translate_item_tag(dev, match_mask,
7909 match_value, items);
7910 last_item = MLX5_FLOW_ITEM_TAG;
7912 case MLX5_RTE_FLOW_ITEM_TYPE_TAG:
7913 flow_dv_translate_mlx5_item_tag(dev, match_mask,
7914 match_value, items);
7915 last_item = MLX5_FLOW_ITEM_TAG;
7917 case MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE:
7918 flow_dv_translate_item_tx_queue(dev, match_mask,
7921 last_item = MLX5_FLOW_ITEM_TX_QUEUE;
7923 case RTE_FLOW_ITEM_TYPE_GTP:
7924 flow_dv_translate_item_gtp(match_mask, match_value,
7926 matcher.priority = flow->rss.level >= 2 ?
7927 MLX5_PRIORITY_MAP_L2 : MLX5_PRIORITY_MAP_L4;
7928 last_item = MLX5_FLOW_LAYER_GTP;
7933 item_flags |= last_item;
7936 * When E-Switch mode is enabled, we have two cases where we need to
7937 * set the source port manually.
7938 * The first one, is in case of Nic steering rule, and the second is
7939 * E-Switch rule where no port_id item was found. In both cases
7940 * the source port is set according the current port in use.
7942 if (!(item_flags & MLX5_FLOW_ITEM_PORT_ID) &&
7943 (priv->representor || priv->master)) {
7944 if (flow_dv_translate_item_port_id(dev, match_mask,
7948 #ifdef RTE_LIBRTE_MLX5_DEBUG
7949 MLX5_ASSERT(!flow_dv_check_valid_spec(matcher.mask.buf,
7950 dev_flow->dv.value.buf));
7953 * Layers may be already initialized from prefix flow if this dev_flow
7954 * is the suffix flow.
7956 dev_flow->layers |= item_flags;
7957 if (action_flags & MLX5_FLOW_ACTION_RSS)
7958 flow_dv_hashfields_set(dev_flow);
7959 /* Register matcher. */
7960 matcher.crc = rte_raw_cksum((const void *)matcher.mask.buf,
7962 matcher.priority = mlx5_flow_adjust_priority(dev, priority,
7964 /* reserved field no needs to be set to 0 here. */
7965 tbl_key.domain = attr->transfer;
7966 tbl_key.direction = attr->egress;
7967 tbl_key.table_id = dev_flow->group;
7968 if (flow_dv_matcher_register(dev, &matcher, &tbl_key, dev_flow, error))
7974 * Apply the flow to the NIC, lock free,
7975 * (mutex should be acquired by caller).
7978 * Pointer to the Ethernet device structure.
7979 * @param[in, out] flow
7980 * Pointer to flow structure.
7982 * Pointer to error structure.
7985 * 0 on success, a negative errno value otherwise and rte_errno is set.
7988 __flow_dv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
7989 struct rte_flow_error *error)
7991 struct mlx5_flow_dv *dv;
7992 struct mlx5_flow *dev_flow;
7993 struct mlx5_priv *priv = dev->data->dev_private;
7997 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
8000 if (dev_flow->actions & MLX5_FLOW_ACTION_DROP) {
8001 if (dev_flow->transfer) {
8002 dv->actions[n++] = priv->sh->esw_drop_action;
8004 dv->hrxq = mlx5_hrxq_drop_new(dev);
8008 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8010 "cannot get drop hash queue");
8013 dv->actions[n++] = dv->hrxq->action;
8015 } else if (dev_flow->actions &
8016 (MLX5_FLOW_ACTION_QUEUE | MLX5_FLOW_ACTION_RSS)) {
8017 struct mlx5_hrxq *hrxq;
8019 MLX5_ASSERT(flow->rss.queue);
8020 hrxq = mlx5_hrxq_get(dev, flow->rss.key,
8021 MLX5_RSS_HASH_KEY_LEN,
8022 dev_flow->hash_fields,
8024 flow->rss.queue_num);
8026 hrxq = mlx5_hrxq_new
8027 (dev, flow->rss.key,
8028 MLX5_RSS_HASH_KEY_LEN,
8029 dev_flow->hash_fields,
8031 flow->rss.queue_num,
8032 !!(dev_flow->layers &
8033 MLX5_FLOW_LAYER_TUNNEL));
8038 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
8039 "cannot get hash queue");
8043 dv->actions[n++] = dv->hrxq->action;
8046 mlx5_glue->dv_create_flow(dv->matcher->matcher_object,
8047 (void *)&dv->value, n,
8050 rte_flow_error_set(error, errno,
8051 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8053 "hardware refuses to create flow");
8056 if (priv->vmwa_context &&
8057 dev_flow->dv.vf_vlan.tag &&
8058 !dev_flow->dv.vf_vlan.created) {
8060 * The rule contains the VLAN pattern.
8061 * For VF we are going to create VLAN
8062 * interface to make hypervisor set correct
8063 * e-Switch vport context.
8065 mlx5_vlan_vmwa_acquire(dev, &dev_flow->dv.vf_vlan);
8070 err = rte_errno; /* Save rte_errno before cleanup. */
8071 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
8072 struct mlx5_flow_dv *dv = &dev_flow->dv;
8074 if (dev_flow->actions & MLX5_FLOW_ACTION_DROP)
8075 mlx5_hrxq_drop_release(dev);
8077 mlx5_hrxq_release(dev, dv->hrxq);
8080 if (dev_flow->dv.vf_vlan.tag &&
8081 dev_flow->dv.vf_vlan.created)
8082 mlx5_vlan_vmwa_release(dev, &dev_flow->dv.vf_vlan);
8084 rte_errno = err; /* Restore rte_errno. */
8089 * Release the flow matcher.
8092 * Pointer to Ethernet device.
8094 * Pointer to mlx5_flow.
8097 * 1 while a reference on it exists, 0 when freed.
8100 flow_dv_matcher_release(struct rte_eth_dev *dev,
8101 struct mlx5_flow *flow)
8103 struct mlx5_flow_dv_matcher *matcher = flow->dv.matcher;
8105 MLX5_ASSERT(matcher->matcher_object);
8106 DRV_LOG(DEBUG, "port %u matcher %p: refcnt %d--",
8107 dev->data->port_id, (void *)matcher,
8108 rte_atomic32_read(&matcher->refcnt));
8109 if (rte_atomic32_dec_and_test(&matcher->refcnt)) {
8110 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8111 (matcher->matcher_object));
8112 LIST_REMOVE(matcher, next);
8113 /* table ref-- in release interface. */
8114 flow_dv_tbl_resource_release(dev, matcher->tbl);
8116 DRV_LOG(DEBUG, "port %u matcher %p: removed",
8117 dev->data->port_id, (void *)matcher);
8124 * Release an encap/decap resource.
8127 * Pointer to mlx5_flow.
8130 * 1 while a reference on it exists, 0 when freed.
8133 flow_dv_encap_decap_resource_release(struct mlx5_flow *flow)
8135 struct mlx5_flow_dv_encap_decap_resource *cache_resource =
8136 flow->dv.encap_decap;
8138 MLX5_ASSERT(cache_resource->verbs_action);
8139 DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d--",
8140 (void *)cache_resource,
8141 rte_atomic32_read(&cache_resource->refcnt));
8142 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
8143 claim_zero(mlx5_glue->destroy_flow_action
8144 (cache_resource->verbs_action));
8145 LIST_REMOVE(cache_resource, next);
8146 rte_free(cache_resource);
8147 DRV_LOG(DEBUG, "encap/decap resource %p: removed",
8148 (void *)cache_resource);
8155 * Release an jump to table action resource.
8158 * Pointer to Ethernet device.
8160 * Pointer to mlx5_flow.
8163 * 1 while a reference on it exists, 0 when freed.
8166 flow_dv_jump_tbl_resource_release(struct rte_eth_dev *dev,
8167 struct mlx5_flow *flow)
8169 struct mlx5_flow_dv_jump_tbl_resource *cache_resource = flow->dv.jump;
8170 struct mlx5_flow_tbl_data_entry *tbl_data =
8171 container_of(cache_resource,
8172 struct mlx5_flow_tbl_data_entry, jump);
8174 MLX5_ASSERT(cache_resource->action);
8175 DRV_LOG(DEBUG, "jump table resource %p: refcnt %d--",
8176 (void *)cache_resource,
8177 rte_atomic32_read(&cache_resource->refcnt));
8178 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
8179 claim_zero(mlx5_glue->destroy_flow_action
8180 (cache_resource->action));
8181 /* jump action memory free is inside the table release. */
8182 flow_dv_tbl_resource_release(dev, &tbl_data->tbl);
8183 DRV_LOG(DEBUG, "jump table resource %p: removed",
8184 (void *)cache_resource);
8191 * Release a modify-header resource.
8194 * Pointer to mlx5_flow.
8197 * 1 while a reference on it exists, 0 when freed.
8200 flow_dv_modify_hdr_resource_release(struct mlx5_flow *flow)
8202 struct mlx5_flow_dv_modify_hdr_resource *cache_resource =
8203 flow->dv.modify_hdr;
8205 MLX5_ASSERT(cache_resource->verbs_action);
8206 DRV_LOG(DEBUG, "modify-header resource %p: refcnt %d--",
8207 (void *)cache_resource,
8208 rte_atomic32_read(&cache_resource->refcnt));
8209 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
8210 claim_zero(mlx5_glue->destroy_flow_action
8211 (cache_resource->verbs_action));
8212 LIST_REMOVE(cache_resource, next);
8213 rte_free(cache_resource);
8214 DRV_LOG(DEBUG, "modify-header resource %p: removed",
8215 (void *)cache_resource);
8222 * Release port ID action resource.
8225 * Pointer to mlx5_flow.
8228 * 1 while a reference on it exists, 0 when freed.
8231 flow_dv_port_id_action_resource_release(struct mlx5_flow *flow)
8233 struct mlx5_flow_dv_port_id_action_resource *cache_resource =
8234 flow->dv.port_id_action;
8236 MLX5_ASSERT(cache_resource->action);
8237 DRV_LOG(DEBUG, "port ID action resource %p: refcnt %d--",
8238 (void *)cache_resource,
8239 rte_atomic32_read(&cache_resource->refcnt));
8240 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
8241 claim_zero(mlx5_glue->destroy_flow_action
8242 (cache_resource->action));
8243 LIST_REMOVE(cache_resource, next);
8244 rte_free(cache_resource);
8245 DRV_LOG(DEBUG, "port id action resource %p: removed",
8246 (void *)cache_resource);
8253 * Release push vlan action resource.
8256 * Pointer to mlx5_flow.
8259 * 1 while a reference on it exists, 0 when freed.
8262 flow_dv_push_vlan_action_resource_release(struct mlx5_flow *flow)
8264 struct mlx5_flow_dv_push_vlan_action_resource *cache_resource =
8265 flow->dv.push_vlan_res;
8267 MLX5_ASSERT(cache_resource->action);
8268 DRV_LOG(DEBUG, "push VLAN action resource %p: refcnt %d--",
8269 (void *)cache_resource,
8270 rte_atomic32_read(&cache_resource->refcnt));
8271 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
8272 claim_zero(mlx5_glue->destroy_flow_action
8273 (cache_resource->action));
8274 LIST_REMOVE(cache_resource, next);
8275 rte_free(cache_resource);
8276 DRV_LOG(DEBUG, "push vlan action resource %p: removed",
8277 (void *)cache_resource);
8284 * Remove the flow from the NIC but keeps it in memory.
8285 * Lock free, (mutex should be acquired by caller).
8288 * Pointer to Ethernet device.
8289 * @param[in, out] flow
8290 * Pointer to flow structure.
8293 __flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
8295 struct mlx5_flow_dv *dv;
8296 struct mlx5_flow *dev_flow;
8300 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
8303 claim_zero(mlx5_glue->dv_destroy_flow(dv->flow));
8307 if (dev_flow->actions & MLX5_FLOW_ACTION_DROP)
8308 mlx5_hrxq_drop_release(dev);
8310 mlx5_hrxq_release(dev, dv->hrxq);
8313 if (dev_flow->dv.vf_vlan.tag &&
8314 dev_flow->dv.vf_vlan.created)
8315 mlx5_vlan_vmwa_release(dev, &dev_flow->dv.vf_vlan);
8320 * Remove the flow from the NIC and the memory.
8321 * Lock free, (mutex should be acquired by caller).
8324 * Pointer to the Ethernet device structure.
8325 * @param[in, out] flow
8326 * Pointer to flow structure.
8329 __flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
8331 struct mlx5_flow *dev_flow;
8335 __flow_dv_remove(dev, flow);
8336 if (flow->counter) {
8337 flow_dv_counter_release(dev, flow->counter);
8338 flow->counter = NULL;
8341 mlx5_flow_meter_detach(flow->meter);
8344 while (!LIST_EMPTY(&flow->dev_flows)) {
8345 dev_flow = LIST_FIRST(&flow->dev_flows);
8346 LIST_REMOVE(dev_flow, next);
8347 if (dev_flow->dv.matcher)
8348 flow_dv_matcher_release(dev, dev_flow);
8349 if (dev_flow->dv.encap_decap)
8350 flow_dv_encap_decap_resource_release(dev_flow);
8351 if (dev_flow->dv.modify_hdr)
8352 flow_dv_modify_hdr_resource_release(dev_flow);
8353 if (dev_flow->dv.jump)
8354 flow_dv_jump_tbl_resource_release(dev, dev_flow);
8355 if (dev_flow->dv.port_id_action)
8356 flow_dv_port_id_action_resource_release(dev_flow);
8357 if (dev_flow->dv.push_vlan_res)
8358 flow_dv_push_vlan_action_resource_release(dev_flow);
8359 if (dev_flow->dv.tag_resource)
8360 flow_dv_tag_release(dev, dev_flow->dv.tag_resource);
8366 * Query a dv flow rule for its statistics via devx.
8369 * Pointer to Ethernet device.
8371 * Pointer to the sub flow.
8373 * data retrieved by the query.
8375 * Perform verbose error reporting if not NULL.
8378 * 0 on success, a negative errno value otherwise and rte_errno is set.
8381 flow_dv_query_count(struct rte_eth_dev *dev, struct rte_flow *flow,
8382 void *data, struct rte_flow_error *error)
8384 struct mlx5_priv *priv = dev->data->dev_private;
8385 struct rte_flow_query_count *qc = data;
8387 if (!priv->config.devx)
8388 return rte_flow_error_set(error, ENOTSUP,
8389 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8391 "counters are not supported");
8392 if (flow->counter) {
8393 uint64_t pkts, bytes;
8394 int err = _flow_dv_query_count(dev, flow->counter, &pkts,
8398 return rte_flow_error_set(error, -err,
8399 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8400 NULL, "cannot read counters");
8403 qc->hits = pkts - flow->counter->hits;
8404 qc->bytes = bytes - flow->counter->bytes;
8406 flow->counter->hits = pkts;
8407 flow->counter->bytes = bytes;
8411 return rte_flow_error_set(error, EINVAL,
8412 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8414 "counters are not available");
8420 * @see rte_flow_query()
8424 flow_dv_query(struct rte_eth_dev *dev,
8425 struct rte_flow *flow __rte_unused,
8426 const struct rte_flow_action *actions __rte_unused,
8427 void *data __rte_unused,
8428 struct rte_flow_error *error __rte_unused)
8432 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
8433 switch (actions->type) {
8434 case RTE_FLOW_ACTION_TYPE_VOID:
8436 case RTE_FLOW_ACTION_TYPE_COUNT:
8437 ret = flow_dv_query_count(dev, flow, data, error);
8440 return rte_flow_error_set(error, ENOTSUP,
8441 RTE_FLOW_ERROR_TYPE_ACTION,
8443 "action not supported");
8450 * Destroy the meter table set.
8451 * Lock free, (mutex should be acquired by caller).
8454 * Pointer to Ethernet device.
8456 * Pointer to the meter table set.
8462 flow_dv_destroy_mtr_tbl(struct rte_eth_dev *dev,
8463 struct mlx5_meter_domains_infos *tbl)
8465 struct mlx5_priv *priv = dev->data->dev_private;
8466 struct mlx5_meter_domains_infos *mtd =
8467 (struct mlx5_meter_domains_infos *)tbl;
8469 if (!mtd || !priv->config.dv_flow_en)
8471 if (mtd->ingress.policer_rules[RTE_MTR_DROPPED])
8472 claim_zero(mlx5_glue->dv_destroy_flow
8473 (mtd->ingress.policer_rules[RTE_MTR_DROPPED]));
8474 if (mtd->egress.policer_rules[RTE_MTR_DROPPED])
8475 claim_zero(mlx5_glue->dv_destroy_flow
8476 (mtd->egress.policer_rules[RTE_MTR_DROPPED]));
8477 if (mtd->transfer.policer_rules[RTE_MTR_DROPPED])
8478 claim_zero(mlx5_glue->dv_destroy_flow
8479 (mtd->transfer.policer_rules[RTE_MTR_DROPPED]));
8480 if (mtd->egress.color_matcher)
8481 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8482 (mtd->egress.color_matcher));
8483 if (mtd->egress.any_matcher)
8484 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8485 (mtd->egress.any_matcher));
8486 if (mtd->egress.tbl)
8487 claim_zero(flow_dv_tbl_resource_release(dev,
8489 if (mtd->ingress.color_matcher)
8490 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8491 (mtd->ingress.color_matcher));
8492 if (mtd->ingress.any_matcher)
8493 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8494 (mtd->ingress.any_matcher));
8495 if (mtd->ingress.tbl)
8496 claim_zero(flow_dv_tbl_resource_release(dev,
8498 if (mtd->transfer.color_matcher)
8499 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8500 (mtd->transfer.color_matcher));
8501 if (mtd->transfer.any_matcher)
8502 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8503 (mtd->transfer.any_matcher));
8504 if (mtd->transfer.tbl)
8505 claim_zero(flow_dv_tbl_resource_release(dev,
8506 mtd->transfer.tbl));
8508 claim_zero(mlx5_glue->destroy_flow_action(mtd->drop_actn));
8513 /* Number of meter flow actions, count and jump or count and drop. */
8514 #define METER_ACTIONS 2
8517 * Create specify domain meter table and suffix table.
8520 * Pointer to Ethernet device.
8521 * @param[in,out] mtb
8522 * Pointer to DV meter table set.
8525 * @param[in] transfer
8527 * @param[in] color_reg_c_idx
8528 * Reg C index for color match.
8531 * 0 on success, -1 otherwise and rte_errno is set.
8534 flow_dv_prepare_mtr_tables(struct rte_eth_dev *dev,
8535 struct mlx5_meter_domains_infos *mtb,
8536 uint8_t egress, uint8_t transfer,
8537 uint32_t color_reg_c_idx)
8539 struct mlx5_priv *priv = dev->data->dev_private;
8540 struct mlx5_ibv_shared *sh = priv->sh;
8541 struct mlx5_flow_dv_match_params mask = {
8542 .size = sizeof(mask.buf),
8544 struct mlx5_flow_dv_match_params value = {
8545 .size = sizeof(value.buf),
8547 struct mlx5dv_flow_matcher_attr dv_attr = {
8548 .type = IBV_FLOW_ATTR_NORMAL,
8550 .match_criteria_enable = 0,
8551 .match_mask = (void *)&mask,
8553 void *actions[METER_ACTIONS];
8554 struct mlx5_flow_tbl_resource **sfx_tbl;
8555 struct mlx5_meter_domain_info *dtb;
8556 struct rte_flow_error error;
8560 sfx_tbl = &sh->fdb_mtr_sfx_tbl;
8561 dtb = &mtb->transfer;
8562 } else if (egress) {
8563 sfx_tbl = &sh->tx_mtr_sfx_tbl;
8566 sfx_tbl = &sh->rx_mtr_sfx_tbl;
8567 dtb = &mtb->ingress;
8569 /* If the suffix table in missing, create it. */
8571 *sfx_tbl = flow_dv_tbl_resource_get(dev,
8572 MLX5_FLOW_TABLE_LEVEL_SUFFIX,
8573 egress, transfer, &error);
8575 DRV_LOG(ERR, "Failed to create meter suffix table.");
8579 /* Create the meter table with METER level. */
8580 dtb->tbl = flow_dv_tbl_resource_get(dev, MLX5_FLOW_TABLE_LEVEL_METER,
8581 egress, transfer, &error);
8583 DRV_LOG(ERR, "Failed to create meter policer table.");
8586 /* Create matchers, Any and Color. */
8587 dv_attr.priority = 3;
8588 dv_attr.match_criteria_enable = 0;
8589 dtb->any_matcher = mlx5_glue->dv_create_flow_matcher(sh->ctx,
8592 if (!dtb->any_matcher) {
8593 DRV_LOG(ERR, "Failed to create meter"
8594 " policer default matcher.");
8597 dv_attr.priority = 0;
8598 dv_attr.match_criteria_enable =
8599 1 << MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
8600 flow_dv_match_meta_reg(mask.buf, value.buf, color_reg_c_idx,
8601 rte_col_2_mlx5_col(RTE_COLORS), UINT8_MAX);
8602 dtb->color_matcher = mlx5_glue->dv_create_flow_matcher(sh->ctx,
8605 if (!dtb->color_matcher) {
8606 DRV_LOG(ERR, "Failed to create meter policer color matcher.");
8609 if (mtb->count_actns[RTE_MTR_DROPPED])
8610 actions[i++] = mtb->count_actns[RTE_MTR_DROPPED];
8611 actions[i++] = mtb->drop_actn;
8612 /* Default rule: lowest priority, match any, actions: drop. */
8613 dtb->policer_rules[RTE_MTR_DROPPED] =
8614 mlx5_glue->dv_create_flow(dtb->any_matcher,
8615 (void *)&value, i, actions);
8616 if (!dtb->policer_rules[RTE_MTR_DROPPED]) {
8617 DRV_LOG(ERR, "Failed to create meter policer drop rule.");
8626 * Create the needed meter and suffix tables.
8627 * Lock free, (mutex should be acquired by caller).
8630 * Pointer to Ethernet device.
8632 * Pointer to the flow meter.
8635 * Pointer to table set on success, NULL otherwise and rte_errno is set.
8637 static struct mlx5_meter_domains_infos *
8638 flow_dv_create_mtr_tbl(struct rte_eth_dev *dev,
8639 const struct mlx5_flow_meter *fm)
8641 struct mlx5_priv *priv = dev->data->dev_private;
8642 struct mlx5_meter_domains_infos *mtb;
8646 if (!priv->mtr_en) {
8647 rte_errno = ENOTSUP;
8650 mtb = rte_calloc(__func__, 1, sizeof(*mtb), 0);
8652 DRV_LOG(ERR, "Failed to allocate memory for meter.");
8655 /* Create meter count actions */
8656 for (i = 0; i <= RTE_MTR_DROPPED; i++) {
8657 if (!fm->policer_stats.cnt[i])
8659 mtb->count_actns[i] = fm->policer_stats.cnt[i]->action;
8661 /* Create drop action. */
8662 mtb->drop_actn = mlx5_glue->dr_create_flow_action_drop();
8663 if (!mtb->drop_actn) {
8664 DRV_LOG(ERR, "Failed to create drop action.");
8667 /* Egress meter table. */
8668 ret = flow_dv_prepare_mtr_tables(dev, mtb, 1, 0, priv->mtr_color_reg);
8670 DRV_LOG(ERR, "Failed to prepare egress meter table.");
8673 /* Ingress meter table. */
8674 ret = flow_dv_prepare_mtr_tables(dev, mtb, 0, 0, priv->mtr_color_reg);
8676 DRV_LOG(ERR, "Failed to prepare ingress meter table.");
8679 /* FDB meter table. */
8680 if (priv->config.dv_esw_en) {
8681 ret = flow_dv_prepare_mtr_tables(dev, mtb, 0, 1,
8682 priv->mtr_color_reg);
8684 DRV_LOG(ERR, "Failed to prepare fdb meter table.");
8690 flow_dv_destroy_mtr_tbl(dev, mtb);
8695 * Destroy domain policer rule.
8698 * Pointer to domain table.
8701 flow_dv_destroy_domain_policer_rule(struct mlx5_meter_domain_info *dt)
8705 for (i = 0; i < RTE_MTR_DROPPED; i++) {
8706 if (dt->policer_rules[i]) {
8707 claim_zero(mlx5_glue->dv_destroy_flow
8708 (dt->policer_rules[i]));
8709 dt->policer_rules[i] = NULL;
8712 if (dt->jump_actn) {
8713 claim_zero(mlx5_glue->destroy_flow_action(dt->jump_actn));
8714 dt->jump_actn = NULL;
8719 * Destroy policer rules.
8722 * Pointer to Ethernet device.
8724 * Pointer to flow meter structure.
8726 * Pointer to flow attributes.
8732 flow_dv_destroy_policer_rules(struct rte_eth_dev *dev __rte_unused,
8733 const struct mlx5_flow_meter *fm,
8734 const struct rte_flow_attr *attr)
8736 struct mlx5_meter_domains_infos *mtb = fm ? fm->mfts : NULL;
8741 flow_dv_destroy_domain_policer_rule(&mtb->egress);
8743 flow_dv_destroy_domain_policer_rule(&mtb->ingress);
8745 flow_dv_destroy_domain_policer_rule(&mtb->transfer);
8750 * Create specify domain meter policer rule.
8753 * Pointer to flow meter structure.
8755 * Pointer to DV meter table set.
8757 * Pointer to suffix table.
8758 * @param[in] mtr_reg_c
8759 * Color match REG_C.
8762 * 0 on success, -1 otherwise.
8765 flow_dv_create_policer_forward_rule(struct mlx5_flow_meter *fm,
8766 struct mlx5_meter_domain_info *dtb,
8767 struct mlx5_flow_tbl_resource *sfx_tb,
8770 struct mlx5_flow_dv_match_params matcher = {
8771 .size = sizeof(matcher.buf),
8773 struct mlx5_flow_dv_match_params value = {
8774 .size = sizeof(value.buf),
8776 struct mlx5_meter_domains_infos *mtb = fm->mfts;
8777 void *actions[METER_ACTIONS];
8780 /* Create jump action. */
8783 if (!dtb->jump_actn)
8785 mlx5_glue->dr_create_flow_action_dest_flow_tbl
8787 if (!dtb->jump_actn) {
8788 DRV_LOG(ERR, "Failed to create policer jump action.");
8791 for (i = 0; i < RTE_MTR_DROPPED; i++) {
8794 flow_dv_match_meta_reg(matcher.buf, value.buf, mtr_reg_c,
8795 rte_col_2_mlx5_col(i), UINT8_MAX);
8796 if (mtb->count_actns[i])
8797 actions[j++] = mtb->count_actns[i];
8798 if (fm->params.action[i] == MTR_POLICER_ACTION_DROP)
8799 actions[j++] = mtb->drop_actn;
8801 actions[j++] = dtb->jump_actn;
8802 dtb->policer_rules[i] =
8803 mlx5_glue->dv_create_flow(dtb->color_matcher,
8806 if (!dtb->policer_rules[i]) {
8807 DRV_LOG(ERR, "Failed to create policer rule.");
8818 * Create policer rules.
8821 * Pointer to Ethernet device.
8823 * Pointer to flow meter structure.
8825 * Pointer to flow attributes.
8828 * 0 on success, -1 otherwise.
8831 flow_dv_create_policer_rules(struct rte_eth_dev *dev,
8832 struct mlx5_flow_meter *fm,
8833 const struct rte_flow_attr *attr)
8835 struct mlx5_priv *priv = dev->data->dev_private;
8836 struct mlx5_meter_domains_infos *mtb = fm->mfts;
8840 ret = flow_dv_create_policer_forward_rule(fm, &mtb->egress,
8841 priv->sh->tx_mtr_sfx_tbl,
8842 priv->mtr_color_reg);
8844 DRV_LOG(ERR, "Failed to create egress policer.");
8848 if (attr->ingress) {
8849 ret = flow_dv_create_policer_forward_rule(fm, &mtb->ingress,
8850 priv->sh->rx_mtr_sfx_tbl,
8851 priv->mtr_color_reg);
8853 DRV_LOG(ERR, "Failed to create ingress policer.");
8857 if (attr->transfer) {
8858 ret = flow_dv_create_policer_forward_rule(fm, &mtb->transfer,
8859 priv->sh->fdb_mtr_sfx_tbl,
8860 priv->mtr_color_reg);
8862 DRV_LOG(ERR, "Failed to create transfer policer.");
8868 flow_dv_destroy_policer_rules(dev, fm, attr);
8873 * Query a devx counter.
8876 * Pointer to the Ethernet device structure.
8878 * Pointer to the flow counter.
8880 * Set to clear the counter statistics.
8882 * The statistics value of packets.
8884 * The statistics value of bytes.
8887 * 0 on success, otherwise return -1.
8890 flow_dv_counter_query(struct rte_eth_dev *dev,
8891 struct mlx5_flow_counter *cnt, bool clear,
8892 uint64_t *pkts, uint64_t *bytes)
8894 struct mlx5_priv *priv = dev->data->dev_private;
8895 uint64_t inn_pkts, inn_bytes;
8898 if (!priv->config.devx)
8900 ret = _flow_dv_query_count(dev, cnt, &inn_pkts, &inn_bytes);
8903 *pkts = inn_pkts - cnt->hits;
8904 *bytes = inn_bytes - cnt->bytes;
8906 cnt->hits = inn_pkts;
8907 cnt->bytes = inn_bytes;
8913 * Mutex-protected thunk to lock-free __flow_dv_translate().
8916 flow_dv_translate(struct rte_eth_dev *dev,
8917 struct mlx5_flow *dev_flow,
8918 const struct rte_flow_attr *attr,
8919 const struct rte_flow_item items[],
8920 const struct rte_flow_action actions[],
8921 struct rte_flow_error *error)
8925 flow_dv_shared_lock(dev);
8926 ret = __flow_dv_translate(dev, dev_flow, attr, items, actions, error);
8927 flow_dv_shared_unlock(dev);
8932 * Mutex-protected thunk to lock-free __flow_dv_apply().
8935 flow_dv_apply(struct rte_eth_dev *dev,
8936 struct rte_flow *flow,
8937 struct rte_flow_error *error)
8941 flow_dv_shared_lock(dev);
8942 ret = __flow_dv_apply(dev, flow, error);
8943 flow_dv_shared_unlock(dev);
8948 * Mutex-protected thunk to lock-free __flow_dv_remove().
8951 flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
8953 flow_dv_shared_lock(dev);
8954 __flow_dv_remove(dev, flow);
8955 flow_dv_shared_unlock(dev);
8959 * Mutex-protected thunk to lock-free __flow_dv_destroy().
8962 flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
8964 flow_dv_shared_lock(dev);
8965 __flow_dv_destroy(dev, flow);
8966 flow_dv_shared_unlock(dev);
8970 * Mutex-protected thunk to lock-free flow_dv_counter_alloc().
8972 static struct mlx5_flow_counter *
8973 flow_dv_counter_allocate(struct rte_eth_dev *dev)
8975 struct mlx5_flow_counter *cnt;
8977 flow_dv_shared_lock(dev);
8978 cnt = flow_dv_counter_alloc(dev, 0, 0, 1);
8979 flow_dv_shared_unlock(dev);
8984 * Mutex-protected thunk to lock-free flow_dv_counter_release().
8987 flow_dv_counter_free(struct rte_eth_dev *dev, struct mlx5_flow_counter *cnt)
8989 flow_dv_shared_lock(dev);
8990 flow_dv_counter_release(dev, cnt);
8991 flow_dv_shared_unlock(dev);
8994 const struct mlx5_flow_driver_ops mlx5_flow_dv_drv_ops = {
8995 .validate = flow_dv_validate,
8996 .prepare = flow_dv_prepare,
8997 .translate = flow_dv_translate,
8998 .apply = flow_dv_apply,
8999 .remove = flow_dv_remove,
9000 .destroy = flow_dv_destroy,
9001 .query = flow_dv_query,
9002 .create_mtr_tbls = flow_dv_create_mtr_tbl,
9003 .destroy_mtr_tbls = flow_dv_destroy_mtr_tbl,
9004 .create_policer_rules = flow_dv_create_policer_rules,
9005 .destroy_policer_rules = flow_dv_destroy_policer_rules,
9006 .counter_alloc = flow_dv_counter_allocate,
9007 .counter_free = flow_dv_counter_free,
9008 .counter_query = flow_dv_counter_query,
9011 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */