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_defs.h"
33 #include "mlx5_glue.h"
34 #include "mlx5_flow.h"
36 #include "mlx5_rxtx.h"
38 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
40 #ifndef HAVE_IBV_FLOW_DEVX_COUNTERS
41 #define MLX5DV_FLOW_ACTION_COUNTERS_DEVX 0
44 #ifndef HAVE_MLX5DV_DR_ESWITCH
45 #ifndef MLX5DV_FLOW_TABLE_TYPE_FDB
46 #define MLX5DV_FLOW_TABLE_TYPE_FDB 0
50 #ifndef HAVE_MLX5DV_DR
51 #define MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL 1
54 /* VLAN header definitions */
55 #define MLX5DV_FLOW_VLAN_PCP_SHIFT 13
56 #define MLX5DV_FLOW_VLAN_PCP_MASK (0x7 << MLX5DV_FLOW_VLAN_PCP_SHIFT)
57 #define MLX5DV_FLOW_VLAN_VID_MASK 0x0fff
58 #define MLX5DV_FLOW_VLAN_PCP_MASK_BE RTE_BE16(MLX5DV_FLOW_VLAN_PCP_MASK)
59 #define MLX5DV_FLOW_VLAN_VID_MASK_BE RTE_BE16(MLX5DV_FLOW_VLAN_VID_MASK)
74 * Initialize flow attributes structure according to flow items' types.
77 * Pointer to item specification.
79 * Pointer to flow attributes structure.
82 flow_dv_attr_init(const struct rte_flow_item *item, union flow_dv_attr *attr)
84 for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
86 case RTE_FLOW_ITEM_TYPE_IPV4:
89 case RTE_FLOW_ITEM_TYPE_IPV6:
92 case RTE_FLOW_ITEM_TYPE_UDP:
95 case RTE_FLOW_ITEM_TYPE_TCP:
105 struct field_modify_info {
106 uint32_t size; /* Size of field in protocol header, in bytes. */
107 uint32_t offset; /* Offset of field in protocol header, in bytes. */
108 enum mlx5_modification_field id;
111 struct field_modify_info modify_eth[] = {
112 {4, 0, MLX5_MODI_OUT_DMAC_47_16},
113 {2, 4, MLX5_MODI_OUT_DMAC_15_0},
114 {4, 6, MLX5_MODI_OUT_SMAC_47_16},
115 {2, 10, MLX5_MODI_OUT_SMAC_15_0},
119 struct field_modify_info modify_vlan_out_first_vid[] = {
120 /* Size in bits !!! */
121 {12, 0, MLX5_MODI_OUT_FIRST_VID},
125 struct field_modify_info modify_ipv4[] = {
126 {1, 8, MLX5_MODI_OUT_IPV4_TTL},
127 {4, 12, MLX5_MODI_OUT_SIPV4},
128 {4, 16, MLX5_MODI_OUT_DIPV4},
132 struct field_modify_info modify_ipv6[] = {
133 {1, 7, MLX5_MODI_OUT_IPV6_HOPLIMIT},
134 {4, 8, MLX5_MODI_OUT_SIPV6_127_96},
135 {4, 12, MLX5_MODI_OUT_SIPV6_95_64},
136 {4, 16, MLX5_MODI_OUT_SIPV6_63_32},
137 {4, 20, MLX5_MODI_OUT_SIPV6_31_0},
138 {4, 24, MLX5_MODI_OUT_DIPV6_127_96},
139 {4, 28, MLX5_MODI_OUT_DIPV6_95_64},
140 {4, 32, MLX5_MODI_OUT_DIPV6_63_32},
141 {4, 36, MLX5_MODI_OUT_DIPV6_31_0},
145 struct field_modify_info modify_udp[] = {
146 {2, 0, MLX5_MODI_OUT_UDP_SPORT},
147 {2, 2, MLX5_MODI_OUT_UDP_DPORT},
151 struct field_modify_info modify_tcp[] = {
152 {2, 0, MLX5_MODI_OUT_TCP_SPORT},
153 {2, 2, MLX5_MODI_OUT_TCP_DPORT},
154 {4, 4, MLX5_MODI_OUT_TCP_SEQ_NUM},
155 {4, 8, MLX5_MODI_OUT_TCP_ACK_NUM},
160 mlx5_flow_tunnel_ip_check(const struct rte_flow_item *item __rte_unused,
161 uint8_t next_protocol, uint64_t *item_flags,
164 assert(item->type == RTE_FLOW_ITEM_TYPE_IPV4 ||
165 item->type == RTE_FLOW_ITEM_TYPE_IPV6);
166 if (next_protocol == IPPROTO_IPIP) {
167 *item_flags |= MLX5_FLOW_LAYER_IPIP;
170 if (next_protocol == IPPROTO_IPV6) {
171 *item_flags |= MLX5_FLOW_LAYER_IPV6_ENCAP;
177 * Acquire the synchronizing object to protect multithreaded access
178 * to shared dv context. Lock occurs only if context is actually
179 * shared, i.e. we have multiport IB device and representors are
183 * Pointer to the rte_eth_dev structure.
186 flow_dv_shared_lock(struct rte_eth_dev *dev)
188 struct mlx5_priv *priv = dev->data->dev_private;
189 struct mlx5_ibv_shared *sh = priv->sh;
191 if (sh->dv_refcnt > 1) {
194 ret = pthread_mutex_lock(&sh->dv_mutex);
201 flow_dv_shared_unlock(struct rte_eth_dev *dev)
203 struct mlx5_priv *priv = dev->data->dev_private;
204 struct mlx5_ibv_shared *sh = priv->sh;
206 if (sh->dv_refcnt > 1) {
209 ret = pthread_mutex_unlock(&sh->dv_mutex);
215 /* Update VLAN's VID/PCP based on input rte_flow_action.
218 * Pointer to struct rte_flow_action.
220 * Pointer to struct rte_vlan_hdr.
223 mlx5_update_vlan_vid_pcp(const struct rte_flow_action *action,
224 struct rte_vlan_hdr *vlan)
227 if (action->type == RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP) {
229 ((const struct rte_flow_action_of_set_vlan_pcp *)
230 action->conf)->vlan_pcp;
231 vlan_tci = vlan_tci << MLX5DV_FLOW_VLAN_PCP_SHIFT;
232 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_PCP_MASK;
233 vlan->vlan_tci |= vlan_tci;
234 } else if (action->type == RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID) {
235 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_VID_MASK;
236 vlan->vlan_tci |= rte_be_to_cpu_16
237 (((const struct rte_flow_action_of_set_vlan_vid *)
238 action->conf)->vlan_vid);
243 * Fetch 1, 2, 3 or 4 byte field from the byte array
244 * and return as unsigned integer in host-endian format.
247 * Pointer to data array.
249 * Size of field to extract.
252 * converted field in host endian format.
254 static inline uint32_t
255 flow_dv_fetch_field(const uint8_t *data, uint32_t size)
264 ret = rte_be_to_cpu_16(*(const unaligned_uint16_t *)data);
267 ret = rte_be_to_cpu_16(*(const unaligned_uint16_t *)data);
268 ret = (ret << 8) | *(data + sizeof(uint16_t));
271 ret = rte_be_to_cpu_32(*(const unaligned_uint32_t *)data);
282 * Convert modify-header action to DV specification.
284 * Data length of each action is determined by provided field description
285 * and the item mask. Data bit offset and width of each action is determined
286 * by provided item mask.
289 * Pointer to item specification.
291 * Pointer to field modification information.
292 * For MLX5_MODIFICATION_TYPE_SET specifies destination field.
293 * For MLX5_MODIFICATION_TYPE_ADD specifies destination field.
294 * For MLX5_MODIFICATION_TYPE_COPY specifies source field.
296 * Destination field info for MLX5_MODIFICATION_TYPE_COPY in @type.
297 * Negative offset value sets the same offset as source offset.
298 * size field is ignored, value is taken from source field.
299 * @param[in,out] resource
300 * Pointer to the modify-header resource.
302 * Type of modification.
304 * Pointer to the error structure.
307 * 0 on success, a negative errno value otherwise and rte_errno is set.
310 flow_dv_convert_modify_action(struct rte_flow_item *item,
311 struct field_modify_info *field,
312 struct field_modify_info *dcopy,
313 struct mlx5_flow_dv_modify_hdr_resource *resource,
314 uint32_t type, struct rte_flow_error *error)
316 uint32_t i = resource->actions_num;
317 struct mlx5_modification_cmd *actions = resource->actions;
320 * The item and mask are provided in big-endian format.
321 * The fields should be presented as in big-endian format either.
322 * Mask must be always present, it defines the actual field width.
332 if (i >= MLX5_MODIFY_NUM)
333 return rte_flow_error_set(error, EINVAL,
334 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
335 "too many items to modify");
336 /* Fetch variable byte size mask from the array. */
337 mask = flow_dv_fetch_field((const uint8_t *)item->mask +
338 field->offset, field->size);
343 /* Deduce actual data width in bits from mask value. */
344 off_b = rte_bsf32(mask);
345 size_b = sizeof(uint32_t) * CHAR_BIT -
346 off_b - __builtin_clz(mask);
348 size_b = size_b == sizeof(uint32_t) * CHAR_BIT ? 0 : size_b;
349 actions[i].action_type = type;
350 actions[i].field = field->id;
351 actions[i].offset = off_b;
352 actions[i].length = size_b;
353 /* Convert entire record to expected big-endian format. */
354 actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
355 if (type == MLX5_MODIFICATION_TYPE_COPY) {
357 actions[i].dst_field = dcopy->id;
358 actions[i].dst_offset =
359 (int)dcopy->offset < 0 ? off_b : dcopy->offset;
360 /* Convert entire record to big-endian format. */
361 actions[i].data1 = rte_cpu_to_be_32(actions[i].data1);
364 data = flow_dv_fetch_field((const uint8_t *)item->spec +
365 field->offset, field->size);
366 /* Shift out the trailing masked bits from data. */
367 data = (data & mask) >> off_b;
368 actions[i].data1 = rte_cpu_to_be_32(data);
372 } while (field->size);
373 resource->actions_num = i;
374 if (!resource->actions_num)
375 return rte_flow_error_set(error, EINVAL,
376 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
377 "invalid modification flow item");
382 * Convert modify-header set IPv4 address action to DV specification.
384 * @param[in,out] resource
385 * Pointer to the modify-header resource.
387 * Pointer to action specification.
389 * Pointer to the error structure.
392 * 0 on success, a negative errno value otherwise and rte_errno is set.
395 flow_dv_convert_action_modify_ipv4
396 (struct mlx5_flow_dv_modify_hdr_resource *resource,
397 const struct rte_flow_action *action,
398 struct rte_flow_error *error)
400 const struct rte_flow_action_set_ipv4 *conf =
401 (const struct rte_flow_action_set_ipv4 *)(action->conf);
402 struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
403 struct rte_flow_item_ipv4 ipv4;
404 struct rte_flow_item_ipv4 ipv4_mask;
406 memset(&ipv4, 0, sizeof(ipv4));
407 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
408 if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC) {
409 ipv4.hdr.src_addr = conf->ipv4_addr;
410 ipv4_mask.hdr.src_addr = rte_flow_item_ipv4_mask.hdr.src_addr;
412 ipv4.hdr.dst_addr = conf->ipv4_addr;
413 ipv4_mask.hdr.dst_addr = rte_flow_item_ipv4_mask.hdr.dst_addr;
416 item.mask = &ipv4_mask;
417 return flow_dv_convert_modify_action(&item, modify_ipv4, NULL, resource,
418 MLX5_MODIFICATION_TYPE_SET, error);
422 * Convert modify-header set IPv6 address action to DV specification.
424 * @param[in,out] resource
425 * Pointer to the modify-header resource.
427 * Pointer to action specification.
429 * Pointer to the error structure.
432 * 0 on success, a negative errno value otherwise and rte_errno is set.
435 flow_dv_convert_action_modify_ipv6
436 (struct mlx5_flow_dv_modify_hdr_resource *resource,
437 const struct rte_flow_action *action,
438 struct rte_flow_error *error)
440 const struct rte_flow_action_set_ipv6 *conf =
441 (const struct rte_flow_action_set_ipv6 *)(action->conf);
442 struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
443 struct rte_flow_item_ipv6 ipv6;
444 struct rte_flow_item_ipv6 ipv6_mask;
446 memset(&ipv6, 0, sizeof(ipv6));
447 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
448 if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC) {
449 memcpy(&ipv6.hdr.src_addr, &conf->ipv6_addr,
450 sizeof(ipv6.hdr.src_addr));
451 memcpy(&ipv6_mask.hdr.src_addr,
452 &rte_flow_item_ipv6_mask.hdr.src_addr,
453 sizeof(ipv6.hdr.src_addr));
455 memcpy(&ipv6.hdr.dst_addr, &conf->ipv6_addr,
456 sizeof(ipv6.hdr.dst_addr));
457 memcpy(&ipv6_mask.hdr.dst_addr,
458 &rte_flow_item_ipv6_mask.hdr.dst_addr,
459 sizeof(ipv6.hdr.dst_addr));
462 item.mask = &ipv6_mask;
463 return flow_dv_convert_modify_action(&item, modify_ipv6, NULL, resource,
464 MLX5_MODIFICATION_TYPE_SET, error);
468 * Convert modify-header set MAC address action to DV specification.
470 * @param[in,out] resource
471 * Pointer to the modify-header resource.
473 * Pointer to action specification.
475 * Pointer to the error structure.
478 * 0 on success, a negative errno value otherwise and rte_errno is set.
481 flow_dv_convert_action_modify_mac
482 (struct mlx5_flow_dv_modify_hdr_resource *resource,
483 const struct rte_flow_action *action,
484 struct rte_flow_error *error)
486 const struct rte_flow_action_set_mac *conf =
487 (const struct rte_flow_action_set_mac *)(action->conf);
488 struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_ETH };
489 struct rte_flow_item_eth eth;
490 struct rte_flow_item_eth eth_mask;
492 memset(ð, 0, sizeof(eth));
493 memset(ð_mask, 0, sizeof(eth_mask));
494 if (action->type == RTE_FLOW_ACTION_TYPE_SET_MAC_SRC) {
495 memcpy(ð.src.addr_bytes, &conf->mac_addr,
496 sizeof(eth.src.addr_bytes));
497 memcpy(ð_mask.src.addr_bytes,
498 &rte_flow_item_eth_mask.src.addr_bytes,
499 sizeof(eth_mask.src.addr_bytes));
501 memcpy(ð.dst.addr_bytes, &conf->mac_addr,
502 sizeof(eth.dst.addr_bytes));
503 memcpy(ð_mask.dst.addr_bytes,
504 &rte_flow_item_eth_mask.dst.addr_bytes,
505 sizeof(eth_mask.dst.addr_bytes));
508 item.mask = ð_mask;
509 return flow_dv_convert_modify_action(&item, modify_eth, NULL, resource,
510 MLX5_MODIFICATION_TYPE_SET, error);
514 * Convert modify-header set VLAN VID action to DV specification.
516 * @param[in,out] resource
517 * Pointer to the modify-header resource.
519 * Pointer to action specification.
521 * Pointer to the error structure.
524 * 0 on success, a negative errno value otherwise and rte_errno is set.
527 flow_dv_convert_action_modify_vlan_vid
528 (struct mlx5_flow_dv_modify_hdr_resource *resource,
529 const struct rte_flow_action *action,
530 struct rte_flow_error *error)
532 const struct rte_flow_action_of_set_vlan_vid *conf =
533 (const struct rte_flow_action_of_set_vlan_vid *)(action->conf);
534 int i = resource->actions_num;
535 struct mlx5_modification_cmd *actions = &resource->actions[i];
536 struct field_modify_info *field = modify_vlan_out_first_vid;
538 if (i >= MLX5_MODIFY_NUM)
539 return rte_flow_error_set(error, EINVAL,
540 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
541 "too many items to modify");
542 actions[i].action_type = MLX5_MODIFICATION_TYPE_SET;
543 actions[i].field = field->id;
544 actions[i].length = field->size;
545 actions[i].offset = field->offset;
546 actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
547 actions[i].data1 = conf->vlan_vid;
548 actions[i].data1 = actions[i].data1 << 16;
549 resource->actions_num = ++i;
554 * Convert modify-header set TP action to DV specification.
556 * @param[in,out] resource
557 * Pointer to the modify-header resource.
559 * Pointer to action specification.
561 * Pointer to rte_flow_item objects list.
563 * Pointer to flow attributes structure.
565 * Pointer to the error structure.
568 * 0 on success, a negative errno value otherwise and rte_errno is set.
571 flow_dv_convert_action_modify_tp
572 (struct mlx5_flow_dv_modify_hdr_resource *resource,
573 const struct rte_flow_action *action,
574 const struct rte_flow_item *items,
575 union flow_dv_attr *attr,
576 struct rte_flow_error *error)
578 const struct rte_flow_action_set_tp *conf =
579 (const struct rte_flow_action_set_tp *)(action->conf);
580 struct rte_flow_item item;
581 struct rte_flow_item_udp udp;
582 struct rte_flow_item_udp udp_mask;
583 struct rte_flow_item_tcp tcp;
584 struct rte_flow_item_tcp tcp_mask;
585 struct field_modify_info *field;
588 flow_dv_attr_init(items, attr);
590 memset(&udp, 0, sizeof(udp));
591 memset(&udp_mask, 0, sizeof(udp_mask));
592 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
593 udp.hdr.src_port = conf->port;
594 udp_mask.hdr.src_port =
595 rte_flow_item_udp_mask.hdr.src_port;
597 udp.hdr.dst_port = conf->port;
598 udp_mask.hdr.dst_port =
599 rte_flow_item_udp_mask.hdr.dst_port;
601 item.type = RTE_FLOW_ITEM_TYPE_UDP;
603 item.mask = &udp_mask;
607 memset(&tcp, 0, sizeof(tcp));
608 memset(&tcp_mask, 0, sizeof(tcp_mask));
609 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
610 tcp.hdr.src_port = conf->port;
611 tcp_mask.hdr.src_port =
612 rte_flow_item_tcp_mask.hdr.src_port;
614 tcp.hdr.dst_port = conf->port;
615 tcp_mask.hdr.dst_port =
616 rte_flow_item_tcp_mask.hdr.dst_port;
618 item.type = RTE_FLOW_ITEM_TYPE_TCP;
620 item.mask = &tcp_mask;
623 return flow_dv_convert_modify_action(&item, field, NULL, resource,
624 MLX5_MODIFICATION_TYPE_SET, error);
628 * Convert modify-header set TTL action to DV specification.
630 * @param[in,out] resource
631 * Pointer to the modify-header resource.
633 * Pointer to action specification.
635 * Pointer to rte_flow_item objects list.
637 * Pointer to flow attributes structure.
639 * Pointer to the error structure.
642 * 0 on success, a negative errno value otherwise and rte_errno is set.
645 flow_dv_convert_action_modify_ttl
646 (struct mlx5_flow_dv_modify_hdr_resource *resource,
647 const struct rte_flow_action *action,
648 const struct rte_flow_item *items,
649 union flow_dv_attr *attr,
650 struct rte_flow_error *error)
652 const struct rte_flow_action_set_ttl *conf =
653 (const struct rte_flow_action_set_ttl *)(action->conf);
654 struct rte_flow_item item;
655 struct rte_flow_item_ipv4 ipv4;
656 struct rte_flow_item_ipv4 ipv4_mask;
657 struct rte_flow_item_ipv6 ipv6;
658 struct rte_flow_item_ipv6 ipv6_mask;
659 struct field_modify_info *field;
662 flow_dv_attr_init(items, attr);
664 memset(&ipv4, 0, sizeof(ipv4));
665 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
666 ipv4.hdr.time_to_live = conf->ttl_value;
667 ipv4_mask.hdr.time_to_live = 0xFF;
668 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
670 item.mask = &ipv4_mask;
674 memset(&ipv6, 0, sizeof(ipv6));
675 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
676 ipv6.hdr.hop_limits = conf->ttl_value;
677 ipv6_mask.hdr.hop_limits = 0xFF;
678 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
680 item.mask = &ipv6_mask;
683 return flow_dv_convert_modify_action(&item, field, NULL, resource,
684 MLX5_MODIFICATION_TYPE_SET, error);
688 * Convert modify-header decrement TTL action to DV specification.
690 * @param[in,out] resource
691 * Pointer to the modify-header resource.
693 * Pointer to action specification.
695 * Pointer to rte_flow_item objects list.
697 * Pointer to flow attributes structure.
699 * Pointer to the error structure.
702 * 0 on success, a negative errno value otherwise and rte_errno is set.
705 flow_dv_convert_action_modify_dec_ttl
706 (struct mlx5_flow_dv_modify_hdr_resource *resource,
707 const struct rte_flow_item *items,
708 union flow_dv_attr *attr,
709 struct rte_flow_error *error)
711 struct rte_flow_item item;
712 struct rte_flow_item_ipv4 ipv4;
713 struct rte_flow_item_ipv4 ipv4_mask;
714 struct rte_flow_item_ipv6 ipv6;
715 struct rte_flow_item_ipv6 ipv6_mask;
716 struct field_modify_info *field;
719 flow_dv_attr_init(items, attr);
721 memset(&ipv4, 0, sizeof(ipv4));
722 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
723 ipv4.hdr.time_to_live = 0xFF;
724 ipv4_mask.hdr.time_to_live = 0xFF;
725 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
727 item.mask = &ipv4_mask;
731 memset(&ipv6, 0, sizeof(ipv6));
732 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
733 ipv6.hdr.hop_limits = 0xFF;
734 ipv6_mask.hdr.hop_limits = 0xFF;
735 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
737 item.mask = &ipv6_mask;
740 return flow_dv_convert_modify_action(&item, field, NULL, resource,
741 MLX5_MODIFICATION_TYPE_ADD, error);
745 * Convert modify-header increment/decrement TCP Sequence number
746 * to DV specification.
748 * @param[in,out] resource
749 * Pointer to the modify-header resource.
751 * Pointer to action specification.
753 * Pointer to the error structure.
756 * 0 on success, a negative errno value otherwise and rte_errno is set.
759 flow_dv_convert_action_modify_tcp_seq
760 (struct mlx5_flow_dv_modify_hdr_resource *resource,
761 const struct rte_flow_action *action,
762 struct rte_flow_error *error)
764 const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
765 uint64_t value = rte_be_to_cpu_32(*conf);
766 struct rte_flow_item item;
767 struct rte_flow_item_tcp tcp;
768 struct rte_flow_item_tcp tcp_mask;
770 memset(&tcp, 0, sizeof(tcp));
771 memset(&tcp_mask, 0, sizeof(tcp_mask));
772 if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ)
774 * The HW has no decrement operation, only increment operation.
775 * To simulate decrement X from Y using increment operation
776 * we need to add UINT32_MAX X times to Y.
777 * Each adding of UINT32_MAX decrements Y by 1.
780 tcp.hdr.sent_seq = rte_cpu_to_be_32((uint32_t)value);
781 tcp_mask.hdr.sent_seq = RTE_BE32(UINT32_MAX);
782 item.type = RTE_FLOW_ITEM_TYPE_TCP;
784 item.mask = &tcp_mask;
785 return flow_dv_convert_modify_action(&item, modify_tcp, NULL, resource,
786 MLX5_MODIFICATION_TYPE_ADD, error);
790 * Convert modify-header increment/decrement TCP Acknowledgment number
791 * to DV specification.
793 * @param[in,out] resource
794 * Pointer to the modify-header resource.
796 * Pointer to action specification.
798 * Pointer to the error structure.
801 * 0 on success, a negative errno value otherwise and rte_errno is set.
804 flow_dv_convert_action_modify_tcp_ack
805 (struct mlx5_flow_dv_modify_hdr_resource *resource,
806 const struct rte_flow_action *action,
807 struct rte_flow_error *error)
809 const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
810 uint64_t value = rte_be_to_cpu_32(*conf);
811 struct rte_flow_item item;
812 struct rte_flow_item_tcp tcp;
813 struct rte_flow_item_tcp tcp_mask;
815 memset(&tcp, 0, sizeof(tcp));
816 memset(&tcp_mask, 0, sizeof(tcp_mask));
817 if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK)
819 * The HW has no decrement operation, only increment operation.
820 * To simulate decrement X from Y using increment operation
821 * we need to add UINT32_MAX X times to Y.
822 * Each adding of UINT32_MAX decrements Y by 1.
825 tcp.hdr.recv_ack = rte_cpu_to_be_32((uint32_t)value);
826 tcp_mask.hdr.recv_ack = RTE_BE32(UINT32_MAX);
827 item.type = RTE_FLOW_ITEM_TYPE_TCP;
829 item.mask = &tcp_mask;
830 return flow_dv_convert_modify_action(&item, modify_tcp, NULL, resource,
831 MLX5_MODIFICATION_TYPE_ADD, error);
834 static enum mlx5_modification_field reg_to_field[] = {
835 [REG_NONE] = MLX5_MODI_OUT_NONE,
836 [REG_A] = MLX5_MODI_META_DATA_REG_A,
837 [REG_B] = MLX5_MODI_META_DATA_REG_B,
838 [REG_C_0] = MLX5_MODI_META_REG_C_0,
839 [REG_C_1] = MLX5_MODI_META_REG_C_1,
840 [REG_C_2] = MLX5_MODI_META_REG_C_2,
841 [REG_C_3] = MLX5_MODI_META_REG_C_3,
842 [REG_C_4] = MLX5_MODI_META_REG_C_4,
843 [REG_C_5] = MLX5_MODI_META_REG_C_5,
844 [REG_C_6] = MLX5_MODI_META_REG_C_6,
845 [REG_C_7] = MLX5_MODI_META_REG_C_7,
849 * Convert register set to DV specification.
851 * @param[in,out] resource
852 * Pointer to the modify-header resource.
854 * Pointer to action specification.
856 * Pointer to the error structure.
859 * 0 on success, a negative errno value otherwise and rte_errno is set.
862 flow_dv_convert_action_set_reg
863 (struct mlx5_flow_dv_modify_hdr_resource *resource,
864 const struct rte_flow_action *action,
865 struct rte_flow_error *error)
867 const struct mlx5_rte_flow_action_set_tag *conf = action->conf;
868 struct mlx5_modification_cmd *actions = resource->actions;
869 uint32_t i = resource->actions_num;
871 if (i >= MLX5_MODIFY_NUM)
872 return rte_flow_error_set(error, EINVAL,
873 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
874 "too many items to modify");
875 assert(conf->id != REG_NONE);
876 assert(conf->id < RTE_DIM(reg_to_field));
877 actions[i].action_type = MLX5_MODIFICATION_TYPE_SET;
878 actions[i].field = reg_to_field[conf->id];
879 actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
880 actions[i].data1 = rte_cpu_to_be_32(conf->data);
882 resource->actions_num = i;
883 if (!resource->actions_num)
884 return rte_flow_error_set(error, EINVAL,
885 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
886 "invalid modification flow item");
891 * Convert SET_TAG action to DV specification.
894 * Pointer to the rte_eth_dev structure.
895 * @param[in,out] resource
896 * Pointer to the modify-header resource.
898 * Pointer to action specification.
900 * Pointer to the error structure.
903 * 0 on success, a negative errno value otherwise and rte_errno is set.
906 flow_dv_convert_action_set_tag
907 (struct rte_eth_dev *dev,
908 struct mlx5_flow_dv_modify_hdr_resource *resource,
909 const struct rte_flow_action_set_tag *conf,
910 struct rte_flow_error *error)
912 rte_be32_t data = rte_cpu_to_be_32(conf->data);
913 rte_be32_t mask = rte_cpu_to_be_32(conf->mask);
914 struct rte_flow_item item = {
918 struct field_modify_info reg_c_x[] = {
921 enum mlx5_modification_field reg_type;
924 ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, conf->index, error);
927 assert(ret != REG_NONE);
928 assert((unsigned int)ret < RTE_DIM(reg_to_field));
929 reg_type = reg_to_field[ret];
930 assert(reg_type > 0);
931 reg_c_x[0] = (struct field_modify_info){4, 0, reg_type};
932 return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
933 MLX5_MODIFICATION_TYPE_SET, error);
937 * Convert internal COPY_REG action to DV specification.
940 * Pointer to the rte_eth_dev structure.
942 * Pointer to the modify-header resource.
944 * Pointer to action specification.
946 * Pointer to the error structure.
949 * 0 on success, a negative errno value otherwise and rte_errno is set.
952 flow_dv_convert_action_copy_mreg(struct rte_eth_dev *dev,
953 struct mlx5_flow_dv_modify_hdr_resource *res,
954 const struct rte_flow_action *action,
955 struct rte_flow_error *error)
957 const struct mlx5_flow_action_copy_mreg *conf = action->conf;
958 rte_be32_t mask = RTE_BE32(UINT32_MAX);
959 struct rte_flow_item item = {
963 struct field_modify_info reg_src[] = {
964 {4, 0, reg_to_field[conf->src]},
967 struct field_modify_info reg_dst = {
969 .id = reg_to_field[conf->dst],
971 /* Adjust reg_c[0] usage according to reported mask. */
972 if (conf->dst == REG_C_0 || conf->src == REG_C_0) {
973 struct mlx5_priv *priv = dev->data->dev_private;
974 uint32_t reg_c0 = priv->sh->dv_regc0_mask;
977 assert(priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY);
978 if (conf->dst == REG_C_0) {
979 /* Copy to reg_c[0], within mask only. */
980 reg_dst.offset = rte_bsf32(reg_c0);
982 * Mask is ignoring the enianness, because
983 * there is no conversion in datapath.
985 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
986 /* Copy from destination lower bits to reg_c[0]. */
987 mask = reg_c0 >> reg_dst.offset;
989 /* Copy from destination upper bits to reg_c[0]. */
990 mask = reg_c0 << (sizeof(reg_c0) * CHAR_BIT -
991 rte_fls_u32(reg_c0));
994 mask = rte_cpu_to_be_32(reg_c0);
995 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
996 /* Copy from reg_c[0] to destination lower bits. */
999 /* Copy from reg_c[0] to destination upper bits. */
1000 reg_dst.offset = sizeof(reg_c0) * CHAR_BIT -
1001 (rte_fls_u32(reg_c0) -
1006 return flow_dv_convert_modify_action(&item,
1007 reg_src, ®_dst, res,
1008 MLX5_MODIFICATION_TYPE_COPY,
1013 * Convert MARK action to DV specification. This routine is used
1014 * in extensive metadata only and requires metadata register to be
1015 * handled. In legacy mode hardware tag resource is engaged.
1018 * Pointer to the rte_eth_dev structure.
1020 * Pointer to MARK action specification.
1021 * @param[in,out] resource
1022 * Pointer to the modify-header resource.
1024 * Pointer to the error structure.
1027 * 0 on success, a negative errno value otherwise and rte_errno is set.
1030 flow_dv_convert_action_mark(struct rte_eth_dev *dev,
1031 const struct rte_flow_action_mark *conf,
1032 struct mlx5_flow_dv_modify_hdr_resource *resource,
1033 struct rte_flow_error *error)
1035 struct mlx5_priv *priv = dev->data->dev_private;
1036 rte_be32_t mask = rte_cpu_to_be_32(MLX5_FLOW_MARK_MASK &
1037 priv->sh->dv_mark_mask);
1038 rte_be32_t data = rte_cpu_to_be_32(conf->id) & mask;
1039 struct rte_flow_item item = {
1043 struct field_modify_info reg_c_x[] = {
1044 {4, 0, 0}, /* dynamic instead of MLX5_MODI_META_REG_C_1. */
1047 enum modify_reg reg;
1050 return rte_flow_error_set(error, EINVAL,
1051 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1052 NULL, "zero mark action mask");
1053 reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1057 reg_c_x[0].id = reg_to_field[reg];
1058 return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1059 MLX5_MODIFICATION_TYPE_SET, error);
1063 * Get metadata register index for specified steering domain.
1066 * Pointer to the rte_eth_dev structure.
1068 * Attributes of flow to determine steering domain.
1070 * Pointer to the error structure.
1073 * positive index on success, a negative errno value otherwise
1074 * and rte_errno is set.
1076 static enum modify_reg
1077 flow_dv_get_metadata_reg(struct rte_eth_dev *dev,
1078 const struct rte_flow_attr *attr,
1079 struct rte_flow_error *error)
1081 enum modify_reg reg =
1082 mlx5_flow_get_reg_id(dev, attr->transfer ?
1086 MLX5_METADATA_RX, 0, error);
1088 return rte_flow_error_set(error,
1089 ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
1090 NULL, "unavailable "
1091 "metadata register");
1096 * Convert SET_META action to DV specification.
1099 * Pointer to the rte_eth_dev structure.
1100 * @param[in,out] resource
1101 * Pointer to the modify-header resource.
1103 * Attributes of flow that includes this item.
1105 * Pointer to action specification.
1107 * Pointer to the error structure.
1110 * 0 on success, a negative errno value otherwise and rte_errno is set.
1113 flow_dv_convert_action_set_meta
1114 (struct rte_eth_dev *dev,
1115 struct mlx5_flow_dv_modify_hdr_resource *resource,
1116 const struct rte_flow_attr *attr,
1117 const struct rte_flow_action_set_meta *conf,
1118 struct rte_flow_error *error)
1120 uint32_t data = conf->data;
1121 uint32_t mask = conf->mask;
1122 struct rte_flow_item item = {
1126 struct field_modify_info reg_c_x[] = {
1129 enum modify_reg reg = flow_dv_get_metadata_reg(dev, attr, error);
1134 * In datapath code there is no endianness
1135 * coversions for perfromance reasons, all
1136 * pattern conversions are done in rte_flow.
1138 if (reg == REG_C_0) {
1139 struct mlx5_priv *priv = dev->data->dev_private;
1140 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
1144 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1145 shl_c0 = rte_bsf32(msk_c0);
1147 shl_c0 = sizeof(msk_c0) * CHAR_BIT - rte_fls_u32(msk_c0);
1151 assert(!(~msk_c0 & rte_cpu_to_be_32(mask)));
1153 reg_c_x[0] = (struct field_modify_info){4, 0, reg_to_field[reg]};
1154 /* The routine expects parameters in memory as big-endian ones. */
1155 return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1156 MLX5_MODIFICATION_TYPE_SET, error);
1160 * Validate MARK item.
1163 * Pointer to the rte_eth_dev structure.
1165 * Item specification.
1167 * Attributes of flow that includes this item.
1169 * Pointer to error structure.
1172 * 0 on success, a negative errno value otherwise and rte_errno is set.
1175 flow_dv_validate_item_mark(struct rte_eth_dev *dev,
1176 const struct rte_flow_item *item,
1177 const struct rte_flow_attr *attr __rte_unused,
1178 struct rte_flow_error *error)
1180 struct mlx5_priv *priv = dev->data->dev_private;
1181 struct mlx5_dev_config *config = &priv->config;
1182 const struct rte_flow_item_mark *spec = item->spec;
1183 const struct rte_flow_item_mark *mask = item->mask;
1184 const struct rte_flow_item_mark nic_mask = {
1185 .id = priv->sh->dv_mark_mask,
1189 if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
1190 return rte_flow_error_set(error, ENOTSUP,
1191 RTE_FLOW_ERROR_TYPE_ITEM, item,
1192 "extended metadata feature"
1194 if (!mlx5_flow_ext_mreg_supported(dev))
1195 return rte_flow_error_set(error, ENOTSUP,
1196 RTE_FLOW_ERROR_TYPE_ITEM, item,
1197 "extended metadata register"
1198 " isn't supported");
1200 return rte_flow_error_set(error, ENOTSUP,
1201 RTE_FLOW_ERROR_TYPE_ITEM, item,
1202 "extended metadata register"
1203 " isn't available");
1204 ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1208 return rte_flow_error_set(error, EINVAL,
1209 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1211 "data cannot be empty");
1212 if (spec->id >= (MLX5_FLOW_MARK_MAX & nic_mask.id))
1213 return rte_flow_error_set(error, EINVAL,
1214 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1216 "mark id exceeds the limit");
1219 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1220 (const uint8_t *)&nic_mask,
1221 sizeof(struct rte_flow_item_mark),
1229 * Validate META item.
1232 * Pointer to the rte_eth_dev structure.
1234 * Item specification.
1236 * Attributes of flow that includes this item.
1238 * Pointer to error structure.
1241 * 0 on success, a negative errno value otherwise and rte_errno is set.
1244 flow_dv_validate_item_meta(struct rte_eth_dev *dev __rte_unused,
1245 const struct rte_flow_item *item,
1246 const struct rte_flow_attr *attr,
1247 struct rte_flow_error *error)
1249 struct mlx5_priv *priv = dev->data->dev_private;
1250 struct mlx5_dev_config *config = &priv->config;
1251 const struct rte_flow_item_meta *spec = item->spec;
1252 const struct rte_flow_item_meta *mask = item->mask;
1253 struct rte_flow_item_meta nic_mask = {
1256 enum modify_reg reg;
1260 return rte_flow_error_set(error, EINVAL,
1261 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1263 "data cannot be empty");
1265 return rte_flow_error_set(error, EINVAL,
1266 RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1267 "data cannot be zero");
1268 if (config->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
1269 if (!mlx5_flow_ext_mreg_supported(dev))
1270 return rte_flow_error_set(error, ENOTSUP,
1271 RTE_FLOW_ERROR_TYPE_ITEM, item,
1272 "extended metadata register"
1273 " isn't supported");
1274 reg = flow_dv_get_metadata_reg(dev, attr, error);
1278 return rte_flow_error_set(error, ENOTSUP,
1279 RTE_FLOW_ERROR_TYPE_ITEM, item,
1283 nic_mask.data = priv->sh->dv_meta_mask;
1286 mask = &rte_flow_item_meta_mask;
1287 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1288 (const uint8_t *)&nic_mask,
1289 sizeof(struct rte_flow_item_meta),
1295 * Validate TAG item.
1298 * Pointer to the rte_eth_dev structure.
1300 * Item specification.
1302 * Attributes of flow that includes this item.
1304 * Pointer to error structure.
1307 * 0 on success, a negative errno value otherwise and rte_errno is set.
1310 flow_dv_validate_item_tag(struct rte_eth_dev *dev,
1311 const struct rte_flow_item *item,
1312 const struct rte_flow_attr *attr __rte_unused,
1313 struct rte_flow_error *error)
1315 const struct rte_flow_item_tag *spec = item->spec;
1316 const struct rte_flow_item_tag *mask = item->mask;
1317 const struct rte_flow_item_tag nic_mask = {
1318 .data = RTE_BE32(UINT32_MAX),
1323 if (!mlx5_flow_ext_mreg_supported(dev))
1324 return rte_flow_error_set(error, ENOTSUP,
1325 RTE_FLOW_ERROR_TYPE_ITEM, item,
1326 "extensive metadata register"
1327 " isn't supported");
1329 return rte_flow_error_set(error, EINVAL,
1330 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1332 "data cannot be empty");
1334 mask = &rte_flow_item_tag_mask;
1335 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1336 (const uint8_t *)&nic_mask,
1337 sizeof(struct rte_flow_item_tag),
1341 if (mask->index != 0xff)
1342 return rte_flow_error_set(error, EINVAL,
1343 RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1344 "partial mask for tag index"
1345 " is not supported");
1346 ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, spec->index, error);
1349 assert(ret != REG_NONE);
1354 * Validate vport item.
1357 * Pointer to the rte_eth_dev structure.
1359 * Item specification.
1361 * Attributes of flow that includes this item.
1362 * @param[in] item_flags
1363 * Bit-fields that holds the items detected until now.
1365 * Pointer to error structure.
1368 * 0 on success, a negative errno value otherwise and rte_errno is set.
1371 flow_dv_validate_item_port_id(struct rte_eth_dev *dev,
1372 const struct rte_flow_item *item,
1373 const struct rte_flow_attr *attr,
1374 uint64_t item_flags,
1375 struct rte_flow_error *error)
1377 const struct rte_flow_item_port_id *spec = item->spec;
1378 const struct rte_flow_item_port_id *mask = item->mask;
1379 const struct rte_flow_item_port_id switch_mask = {
1382 struct mlx5_priv *esw_priv;
1383 struct mlx5_priv *dev_priv;
1386 if (!attr->transfer)
1387 return rte_flow_error_set(error, EINVAL,
1388 RTE_FLOW_ERROR_TYPE_ITEM,
1390 "match on port id is valid only"
1391 " when transfer flag is enabled");
1392 if (item_flags & MLX5_FLOW_ITEM_PORT_ID)
1393 return rte_flow_error_set(error, ENOTSUP,
1394 RTE_FLOW_ERROR_TYPE_ITEM, item,
1395 "multiple source ports are not"
1398 mask = &switch_mask;
1399 if (mask->id != 0xffffffff)
1400 return rte_flow_error_set(error, ENOTSUP,
1401 RTE_FLOW_ERROR_TYPE_ITEM_MASK,
1403 "no support for partial mask on"
1405 ret = mlx5_flow_item_acceptable
1406 (item, (const uint8_t *)mask,
1407 (const uint8_t *)&rte_flow_item_port_id_mask,
1408 sizeof(struct rte_flow_item_port_id),
1414 esw_priv = mlx5_port_to_eswitch_info(spec->id, false);
1416 return rte_flow_error_set(error, rte_errno,
1417 RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
1418 "failed to obtain E-Switch info for"
1420 dev_priv = mlx5_dev_to_eswitch_info(dev);
1422 return rte_flow_error_set(error, rte_errno,
1423 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1425 "failed to obtain E-Switch info");
1426 if (esw_priv->domain_id != dev_priv->domain_id)
1427 return rte_flow_error_set(error, EINVAL,
1428 RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
1429 "cannot match on a port from a"
1430 " different E-Switch");
1435 * Validate the pop VLAN action.
1438 * Pointer to the rte_eth_dev structure.
1439 * @param[in] action_flags
1440 * Holds the actions detected until now.
1442 * Pointer to the pop vlan action.
1443 * @param[in] item_flags
1444 * The items found in this flow rule.
1446 * Pointer to flow attributes.
1448 * Pointer to error structure.
1451 * 0 on success, a negative errno value otherwise and rte_errno is set.
1454 flow_dv_validate_action_pop_vlan(struct rte_eth_dev *dev,
1455 uint64_t action_flags,
1456 const struct rte_flow_action *action,
1457 uint64_t item_flags,
1458 const struct rte_flow_attr *attr,
1459 struct rte_flow_error *error)
1461 struct mlx5_priv *priv = dev->data->dev_private;
1465 if (!priv->sh->pop_vlan_action)
1466 return rte_flow_error_set(error, ENOTSUP,
1467 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1469 "pop vlan action is not supported");
1471 * Check for inconsistencies:
1472 * fail strip_vlan in a flow that matches packets without VLAN tags.
1473 * fail strip_vlan in a flow that matches packets without explicitly a
1474 * matching on VLAN tag ?
1476 if (action_flags & MLX5_FLOW_ACTION_OF_POP_VLAN)
1477 return rte_flow_error_set(error, ENOTSUP,
1478 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1480 "no support for multiple vlan pop "
1482 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
1483 return rte_flow_error_set(error, ENOTSUP,
1484 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1486 "cannot pop vlan without a "
1487 "match on (outer) vlan in the flow");
1488 if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1489 return rte_flow_error_set(error, EINVAL,
1490 RTE_FLOW_ERROR_TYPE_ACTION, action,
1491 "wrong action order, port_id should "
1492 "be after pop VLAN action");
1497 * Get VLAN default info from vlan match info.
1500 * Pointer to the rte_eth_dev structure.
1502 * the list of item specifications.
1504 * pointer VLAN info to fill to.
1506 * Pointer to error structure.
1509 * 0 on success, a negative errno value otherwise and rte_errno is set.
1512 flow_dev_get_vlan_info_from_items(const struct rte_flow_item *items,
1513 struct rte_vlan_hdr *vlan)
1515 const struct rte_flow_item_vlan nic_mask = {
1516 .tci = RTE_BE16(MLX5DV_FLOW_VLAN_PCP_MASK |
1517 MLX5DV_FLOW_VLAN_VID_MASK),
1518 .inner_type = RTE_BE16(0xffff),
1523 for (; items->type != RTE_FLOW_ITEM_TYPE_END &&
1524 items->type != RTE_FLOW_ITEM_TYPE_VLAN; items++)
1526 if (items->type == RTE_FLOW_ITEM_TYPE_VLAN) {
1527 const struct rte_flow_item_vlan *vlan_m = items->mask;
1528 const struct rte_flow_item_vlan *vlan_v = items->spec;
1532 /* Only full match values are accepted */
1533 if ((vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) ==
1534 MLX5DV_FLOW_VLAN_PCP_MASK_BE) {
1535 vlan->vlan_tci &= MLX5DV_FLOW_VLAN_PCP_MASK;
1537 rte_be_to_cpu_16(vlan_v->tci &
1538 MLX5DV_FLOW_VLAN_PCP_MASK_BE);
1540 if ((vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) ==
1541 MLX5DV_FLOW_VLAN_VID_MASK_BE) {
1542 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_VID_MASK;
1544 rte_be_to_cpu_16(vlan_v->tci &
1545 MLX5DV_FLOW_VLAN_VID_MASK_BE);
1547 if (vlan_m->inner_type == nic_mask.inner_type)
1548 vlan->eth_proto = rte_be_to_cpu_16(vlan_v->inner_type &
1549 vlan_m->inner_type);
1554 * Validate the push VLAN action.
1556 * @param[in] action_flags
1557 * Holds the actions detected until now.
1559 * Pointer to the encap action.
1561 * Pointer to flow attributes
1563 * Pointer to error structure.
1566 * 0 on success, a negative errno value otherwise and rte_errno is set.
1569 flow_dv_validate_action_push_vlan(uint64_t action_flags,
1570 uint64_t item_flags,
1571 const struct rte_flow_action *action,
1572 const struct rte_flow_attr *attr,
1573 struct rte_flow_error *error)
1575 const struct rte_flow_action_of_push_vlan *push_vlan = action->conf;
1577 if (push_vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_VLAN) &&
1578 push_vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_QINQ))
1579 return rte_flow_error_set(error, EINVAL,
1580 RTE_FLOW_ERROR_TYPE_ACTION, action,
1581 "invalid vlan ethertype");
1583 (MLX5_FLOW_ACTION_OF_POP_VLAN | MLX5_FLOW_ACTION_OF_PUSH_VLAN))
1584 return rte_flow_error_set(error, ENOTSUP,
1585 RTE_FLOW_ERROR_TYPE_ACTION, action,
1586 "no support for multiple VLAN "
1588 if (!mlx5_flow_find_action
1589 (action + 1, RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID) &&
1590 !(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
1591 return rte_flow_error_set(error, ENOTSUP,
1592 RTE_FLOW_ERROR_TYPE_ACTION, action,
1593 "push VLAN needs to match on VLAN in order to "
1594 "get VLAN VID information because there is "
1595 "no followed set VLAN VID action");
1596 if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1597 return rte_flow_error_set(error, EINVAL,
1598 RTE_FLOW_ERROR_TYPE_ACTION, action,
1599 "wrong action order, port_id should "
1600 "be after push VLAN");
1606 * Validate the set VLAN PCP.
1608 * @param[in] action_flags
1609 * Holds the actions detected until now.
1610 * @param[in] actions
1611 * Pointer to the list of actions remaining in the flow rule.
1613 * Pointer to flow attributes
1615 * Pointer to error structure.
1618 * 0 on success, a negative errno value otherwise and rte_errno is set.
1621 flow_dv_validate_action_set_vlan_pcp(uint64_t action_flags,
1622 const struct rte_flow_action actions[],
1623 struct rte_flow_error *error)
1625 const struct rte_flow_action *action = actions;
1626 const struct rte_flow_action_of_set_vlan_pcp *conf = action->conf;
1628 if (conf->vlan_pcp > 7)
1629 return rte_flow_error_set(error, EINVAL,
1630 RTE_FLOW_ERROR_TYPE_ACTION, action,
1631 "VLAN PCP value is too big");
1632 if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN))
1633 return rte_flow_error_set(error, ENOTSUP,
1634 RTE_FLOW_ERROR_TYPE_ACTION, action,
1635 "set VLAN PCP action must follow "
1636 "the push VLAN action");
1637 if (action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_PCP)
1638 return rte_flow_error_set(error, ENOTSUP,
1639 RTE_FLOW_ERROR_TYPE_ACTION, action,
1640 "Multiple VLAN PCP modification are "
1642 if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1643 return rte_flow_error_set(error, EINVAL,
1644 RTE_FLOW_ERROR_TYPE_ACTION, action,
1645 "wrong action order, port_id should "
1646 "be after set VLAN PCP");
1651 * Validate the set VLAN VID.
1653 * @param[in] item_flags
1654 * Holds the items detected in this rule.
1655 * @param[in] actions
1656 * Pointer to the list of actions remaining in the flow rule.
1658 * Pointer to flow attributes
1660 * Pointer to error structure.
1663 * 0 on success, a negative errno value otherwise and rte_errno is set.
1666 flow_dv_validate_action_set_vlan_vid(uint64_t item_flags,
1667 uint64_t action_flags,
1668 const struct rte_flow_action actions[],
1669 struct rte_flow_error *error)
1671 const struct rte_flow_action *action = actions;
1672 const struct rte_flow_action_of_set_vlan_vid *conf = action->conf;
1674 if (conf->vlan_vid > RTE_BE16(0xFFE))
1675 return rte_flow_error_set(error, EINVAL,
1676 RTE_FLOW_ERROR_TYPE_ACTION, action,
1677 "VLAN VID value is too big");
1678 /* there is an of_push_vlan action before us */
1679 if (action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN) {
1680 if (mlx5_flow_find_action(actions + 1,
1681 RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID))
1682 return rte_flow_error_set(error, ENOTSUP,
1683 RTE_FLOW_ERROR_TYPE_ACTION, action,
1684 "Multiple VLAN VID modifications are "
1691 * Action is on an existing VLAN header:
1692 * Need to verify this is a single modify CID action.
1693 * Rule mast include a match on outer VLAN.
1695 if (action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_VID)
1696 return rte_flow_error_set(error, ENOTSUP,
1697 RTE_FLOW_ERROR_TYPE_ACTION, action,
1698 "Multiple VLAN VID modifications are "
1700 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
1701 return rte_flow_error_set(error, EINVAL,
1702 RTE_FLOW_ERROR_TYPE_ACTION, action,
1703 "match on VLAN is required in order "
1705 if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1706 return rte_flow_error_set(error, EINVAL,
1707 RTE_FLOW_ERROR_TYPE_ACTION, action,
1708 "wrong action order, port_id should "
1709 "be after set VLAN VID");
1714 * Validate the FLAG action.
1717 * Pointer to the rte_eth_dev structure.
1718 * @param[in] action_flags
1719 * Holds the actions detected until now.
1721 * Pointer to flow attributes
1723 * Pointer to error structure.
1726 * 0 on success, a negative errno value otherwise and rte_errno is set.
1729 flow_dv_validate_action_flag(struct rte_eth_dev *dev,
1730 uint64_t action_flags,
1731 const struct rte_flow_attr *attr,
1732 struct rte_flow_error *error)
1734 struct mlx5_priv *priv = dev->data->dev_private;
1735 struct mlx5_dev_config *config = &priv->config;
1738 /* Fall back if no extended metadata register support. */
1739 if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
1740 return mlx5_flow_validate_action_flag(action_flags, attr,
1742 /* Extensive metadata mode requires registers. */
1743 if (!mlx5_flow_ext_mreg_supported(dev))
1744 return rte_flow_error_set(error, ENOTSUP,
1745 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1746 "no metadata registers "
1747 "to support flag action");
1748 if (!(priv->sh->dv_mark_mask & MLX5_FLOW_MARK_DEFAULT))
1749 return rte_flow_error_set(error, ENOTSUP,
1750 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1751 "extended metadata register"
1752 " isn't available");
1753 ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1757 if (action_flags & MLX5_FLOW_ACTION_DROP)
1758 return rte_flow_error_set(error, EINVAL,
1759 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1760 "can't drop and flag in same flow");
1761 if (action_flags & MLX5_FLOW_ACTION_MARK)
1762 return rte_flow_error_set(error, EINVAL,
1763 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1764 "can't mark and flag in same flow");
1765 if (action_flags & MLX5_FLOW_ACTION_FLAG)
1766 return rte_flow_error_set(error, EINVAL,
1767 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1769 " actions in same flow");
1774 * Validate MARK action.
1777 * Pointer to the rte_eth_dev structure.
1779 * Pointer to action.
1780 * @param[in] action_flags
1781 * Holds the actions detected until now.
1783 * Pointer to flow attributes
1785 * Pointer to error structure.
1788 * 0 on success, a negative errno value otherwise and rte_errno is set.
1791 flow_dv_validate_action_mark(struct rte_eth_dev *dev,
1792 const struct rte_flow_action *action,
1793 uint64_t action_flags,
1794 const struct rte_flow_attr *attr,
1795 struct rte_flow_error *error)
1797 struct mlx5_priv *priv = dev->data->dev_private;
1798 struct mlx5_dev_config *config = &priv->config;
1799 const struct rte_flow_action_mark *mark = action->conf;
1802 /* Fall back if no extended metadata register support. */
1803 if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
1804 return mlx5_flow_validate_action_mark(action, action_flags,
1806 /* Extensive metadata mode requires registers. */
1807 if (!mlx5_flow_ext_mreg_supported(dev))
1808 return rte_flow_error_set(error, ENOTSUP,
1809 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1810 "no metadata registers "
1811 "to support mark action");
1812 if (!priv->sh->dv_mark_mask)
1813 return rte_flow_error_set(error, ENOTSUP,
1814 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1815 "extended metadata register"
1816 " isn't available");
1817 ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1822 return rte_flow_error_set(error, EINVAL,
1823 RTE_FLOW_ERROR_TYPE_ACTION, action,
1824 "configuration cannot be null");
1825 if (mark->id >= (MLX5_FLOW_MARK_MAX & priv->sh->dv_mark_mask))
1826 return rte_flow_error_set(error, EINVAL,
1827 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1829 "mark id exceeds the limit");
1830 if (action_flags & MLX5_FLOW_ACTION_DROP)
1831 return rte_flow_error_set(error, EINVAL,
1832 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1833 "can't drop and mark in same flow");
1834 if (action_flags & MLX5_FLOW_ACTION_FLAG)
1835 return rte_flow_error_set(error, EINVAL,
1836 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1837 "can't flag and mark in same flow");
1838 if (action_flags & MLX5_FLOW_ACTION_MARK)
1839 return rte_flow_error_set(error, EINVAL,
1840 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1841 "can't have 2 mark actions in same"
1847 * Validate SET_META action.
1850 * Pointer to the rte_eth_dev structure.
1852 * Pointer to the encap action.
1853 * @param[in] action_flags
1854 * Holds the actions detected until now.
1856 * Pointer to flow attributes
1858 * Pointer to error structure.
1861 * 0 on success, a negative errno value otherwise and rte_errno is set.
1864 flow_dv_validate_action_set_meta(struct rte_eth_dev *dev,
1865 const struct rte_flow_action *action,
1866 uint64_t action_flags __rte_unused,
1867 const struct rte_flow_attr *attr,
1868 struct rte_flow_error *error)
1870 const struct rte_flow_action_set_meta *conf;
1871 uint32_t nic_mask = UINT32_MAX;
1872 enum modify_reg reg;
1874 if (!mlx5_flow_ext_mreg_supported(dev))
1875 return rte_flow_error_set(error, ENOTSUP,
1876 RTE_FLOW_ERROR_TYPE_ACTION, action,
1877 "extended metadata register"
1878 " isn't supported");
1879 reg = flow_dv_get_metadata_reg(dev, attr, error);
1882 if (reg != REG_A && reg != REG_B) {
1883 struct mlx5_priv *priv = dev->data->dev_private;
1885 nic_mask = priv->sh->dv_meta_mask;
1887 if (!(action->conf))
1888 return rte_flow_error_set(error, EINVAL,
1889 RTE_FLOW_ERROR_TYPE_ACTION, action,
1890 "configuration cannot be null");
1891 conf = (const struct rte_flow_action_set_meta *)action->conf;
1893 return rte_flow_error_set(error, EINVAL,
1894 RTE_FLOW_ERROR_TYPE_ACTION, action,
1895 "zero mask doesn't have any effect");
1896 if (conf->mask & ~nic_mask)
1897 return rte_flow_error_set(error, EINVAL,
1898 RTE_FLOW_ERROR_TYPE_ACTION, action,
1899 "meta data must be within reg C0");
1900 if (!(conf->data & conf->mask))
1901 return rte_flow_error_set(error, EINVAL,
1902 RTE_FLOW_ERROR_TYPE_ACTION, action,
1903 "zero value has no effect");
1908 * Validate SET_TAG action.
1911 * Pointer to the rte_eth_dev structure.
1913 * Pointer to the encap action.
1914 * @param[in] action_flags
1915 * Holds the actions detected until now.
1917 * Pointer to flow attributes
1919 * Pointer to error structure.
1922 * 0 on success, a negative errno value otherwise and rte_errno is set.
1925 flow_dv_validate_action_set_tag(struct rte_eth_dev *dev,
1926 const struct rte_flow_action *action,
1927 uint64_t action_flags,
1928 const struct rte_flow_attr *attr,
1929 struct rte_flow_error *error)
1931 const struct rte_flow_action_set_tag *conf;
1932 const uint64_t terminal_action_flags =
1933 MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_QUEUE |
1934 MLX5_FLOW_ACTION_RSS;
1937 if (!mlx5_flow_ext_mreg_supported(dev))
1938 return rte_flow_error_set(error, ENOTSUP,
1939 RTE_FLOW_ERROR_TYPE_ACTION, action,
1940 "extensive metadata register"
1941 " isn't supported");
1942 if (!(action->conf))
1943 return rte_flow_error_set(error, EINVAL,
1944 RTE_FLOW_ERROR_TYPE_ACTION, action,
1945 "configuration cannot be null");
1946 conf = (const struct rte_flow_action_set_tag *)action->conf;
1948 return rte_flow_error_set(error, EINVAL,
1949 RTE_FLOW_ERROR_TYPE_ACTION, action,
1950 "zero mask doesn't have any effect");
1951 ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, conf->index, error);
1954 if (!attr->transfer && attr->ingress &&
1955 (action_flags & terminal_action_flags))
1956 return rte_flow_error_set(error, EINVAL,
1957 RTE_FLOW_ERROR_TYPE_ACTION, action,
1958 "set_tag has no effect"
1959 " with terminal actions");
1964 * Validate count action.
1969 * Pointer to error structure.
1972 * 0 on success, a negative errno value otherwise and rte_errno is set.
1975 flow_dv_validate_action_count(struct rte_eth_dev *dev,
1976 struct rte_flow_error *error)
1978 struct mlx5_priv *priv = dev->data->dev_private;
1980 if (!priv->config.devx)
1982 #ifdef HAVE_IBV_FLOW_DEVX_COUNTERS
1986 return rte_flow_error_set
1988 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1990 "count action not supported");
1994 * Validate the L2 encap action.
1996 * @param[in] action_flags
1997 * Holds the actions detected until now.
1999 * Pointer to the encap action.
2001 * Pointer to flow attributes
2003 * Pointer to error structure.
2006 * 0 on success, a negative errno value otherwise and rte_errno is set.
2009 flow_dv_validate_action_l2_encap(uint64_t action_flags,
2010 const struct rte_flow_action *action,
2011 const struct rte_flow_attr *attr,
2012 struct rte_flow_error *error)
2014 if (!(action->conf))
2015 return rte_flow_error_set(error, EINVAL,
2016 RTE_FLOW_ERROR_TYPE_ACTION, action,
2017 "configuration cannot be null");
2018 if (action_flags & MLX5_FLOW_ACTION_DROP)
2019 return rte_flow_error_set(error, EINVAL,
2020 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2021 "can't drop and encap in same flow");
2022 if (action_flags & (MLX5_FLOW_ENCAP_ACTIONS | MLX5_FLOW_DECAP_ACTIONS))
2023 return rte_flow_error_set(error, EINVAL,
2024 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2025 "can only have a single encap or"
2026 " decap action in a flow");
2027 if (!attr->transfer && attr->ingress)
2028 return rte_flow_error_set(error, ENOTSUP,
2029 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
2031 "encap action not supported for "
2037 * Validate the L2 decap action.
2039 * @param[in] action_flags
2040 * Holds the actions detected until now.
2042 * Pointer to flow attributes
2044 * Pointer to error structure.
2047 * 0 on success, a negative errno value otherwise and rte_errno is set.
2050 flow_dv_validate_action_l2_decap(uint64_t action_flags,
2051 const struct rte_flow_attr *attr,
2052 struct rte_flow_error *error)
2054 if (action_flags & MLX5_FLOW_ACTION_DROP)
2055 return rte_flow_error_set(error, EINVAL,
2056 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2057 "can't drop and decap in same flow");
2058 if (action_flags & (MLX5_FLOW_ENCAP_ACTIONS | MLX5_FLOW_DECAP_ACTIONS))
2059 return rte_flow_error_set(error, EINVAL,
2060 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2061 "can only have a single encap or"
2062 " decap action in a flow");
2063 if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS)
2064 return rte_flow_error_set(error, EINVAL,
2065 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2066 "can't have decap action after"
2069 return rte_flow_error_set(error, ENOTSUP,
2070 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
2072 "decap action not supported for "
2078 * Validate the raw encap action.
2080 * @param[in] action_flags
2081 * Holds the actions detected until now.
2083 * Pointer to the encap action.
2085 * Pointer to flow attributes
2087 * Pointer to error structure.
2090 * 0 on success, a negative errno value otherwise and rte_errno is set.
2093 flow_dv_validate_action_raw_encap(uint64_t action_flags,
2094 const struct rte_flow_action *action,
2095 const struct rte_flow_attr *attr,
2096 struct rte_flow_error *error)
2098 const struct rte_flow_action_raw_encap *raw_encap =
2099 (const struct rte_flow_action_raw_encap *)action->conf;
2100 if (!(action->conf))
2101 return rte_flow_error_set(error, EINVAL,
2102 RTE_FLOW_ERROR_TYPE_ACTION, action,
2103 "configuration cannot be null");
2104 if (action_flags & MLX5_FLOW_ACTION_DROP)
2105 return rte_flow_error_set(error, EINVAL,
2106 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2107 "can't drop and encap in same flow");
2108 if (action_flags & MLX5_FLOW_ENCAP_ACTIONS)
2109 return rte_flow_error_set(error, EINVAL,
2110 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2111 "can only have a single encap"
2112 " action in a flow");
2113 /* encap without preceding decap is not supported for ingress */
2114 if (!attr->transfer && attr->ingress &&
2115 !(action_flags & MLX5_FLOW_ACTION_RAW_DECAP))
2116 return rte_flow_error_set(error, ENOTSUP,
2117 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
2119 "encap action not supported for "
2121 if (!raw_encap->size || !raw_encap->data)
2122 return rte_flow_error_set(error, EINVAL,
2123 RTE_FLOW_ERROR_TYPE_ACTION, action,
2124 "raw encap data cannot be empty");
2129 * Validate the raw decap action.
2131 * @param[in] action_flags
2132 * Holds the actions detected until now.
2134 * Pointer to the encap action.
2136 * Pointer to flow attributes
2138 * Pointer to error structure.
2141 * 0 on success, a negative errno value otherwise and rte_errno is set.
2144 flow_dv_validate_action_raw_decap(uint64_t action_flags,
2145 const struct rte_flow_action *action,
2146 const struct rte_flow_attr *attr,
2147 struct rte_flow_error *error)
2149 if (action_flags & MLX5_FLOW_ACTION_DROP)
2150 return rte_flow_error_set(error, EINVAL,
2151 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2152 "can't drop and decap in same flow");
2153 if (action_flags & MLX5_FLOW_ENCAP_ACTIONS)
2154 return rte_flow_error_set(error, EINVAL,
2155 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2156 "can't have encap action before"
2158 if (action_flags & MLX5_FLOW_DECAP_ACTIONS)
2159 return rte_flow_error_set(error, EINVAL,
2160 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2161 "can only have a single decap"
2162 " action in a flow");
2163 if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS)
2164 return rte_flow_error_set(error, EINVAL,
2165 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2166 "can't have decap action after"
2168 /* decap action is valid on egress only if it is followed by encap */
2170 for (; action->type != RTE_FLOW_ACTION_TYPE_END &&
2171 action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP;
2174 if (action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP)
2175 return rte_flow_error_set
2177 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
2178 NULL, "decap action not supported"
2185 * Find existing encap/decap resource or create and register a new one.
2187 * @param dev[in, out]
2188 * Pointer to rte_eth_dev structure.
2189 * @param[in, out] resource
2190 * Pointer to encap/decap resource.
2191 * @parm[in, out] dev_flow
2192 * Pointer to the dev_flow.
2194 * pointer to error structure.
2197 * 0 on success otherwise -errno and errno is set.
2200 flow_dv_encap_decap_resource_register
2201 (struct rte_eth_dev *dev,
2202 struct mlx5_flow_dv_encap_decap_resource *resource,
2203 struct mlx5_flow *dev_flow,
2204 struct rte_flow_error *error)
2206 struct mlx5_priv *priv = dev->data->dev_private;
2207 struct mlx5_ibv_shared *sh = priv->sh;
2208 struct mlx5_flow_dv_encap_decap_resource *cache_resource;
2209 struct mlx5dv_dr_domain *domain;
2211 resource->flags = dev_flow->group ? 0 : 1;
2212 if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
2213 domain = sh->fdb_domain;
2214 else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
2215 domain = sh->rx_domain;
2217 domain = sh->tx_domain;
2219 /* Lookup a matching resource from cache. */
2220 LIST_FOREACH(cache_resource, &sh->encaps_decaps, next) {
2221 if (resource->reformat_type == cache_resource->reformat_type &&
2222 resource->ft_type == cache_resource->ft_type &&
2223 resource->flags == cache_resource->flags &&
2224 resource->size == cache_resource->size &&
2225 !memcmp((const void *)resource->buf,
2226 (const void *)cache_resource->buf,
2228 DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d++",
2229 (void *)cache_resource,
2230 rte_atomic32_read(&cache_resource->refcnt));
2231 rte_atomic32_inc(&cache_resource->refcnt);
2232 dev_flow->dv.encap_decap = cache_resource;
2236 /* Register new encap/decap resource. */
2237 cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
2238 if (!cache_resource)
2239 return rte_flow_error_set(error, ENOMEM,
2240 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2241 "cannot allocate resource memory");
2242 *cache_resource = *resource;
2243 cache_resource->verbs_action =
2244 mlx5_glue->dv_create_flow_action_packet_reformat
2245 (sh->ctx, cache_resource->reformat_type,
2246 cache_resource->ft_type, domain, cache_resource->flags,
2247 cache_resource->size,
2248 (cache_resource->size ? cache_resource->buf : NULL));
2249 if (!cache_resource->verbs_action) {
2250 rte_free(cache_resource);
2251 return rte_flow_error_set(error, ENOMEM,
2252 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2253 NULL, "cannot create action");
2255 rte_atomic32_init(&cache_resource->refcnt);
2256 rte_atomic32_inc(&cache_resource->refcnt);
2257 LIST_INSERT_HEAD(&sh->encaps_decaps, cache_resource, next);
2258 dev_flow->dv.encap_decap = cache_resource;
2259 DRV_LOG(DEBUG, "new encap/decap resource %p: refcnt %d++",
2260 (void *)cache_resource,
2261 rte_atomic32_read(&cache_resource->refcnt));
2266 * Find existing table jump resource or create and register a new one.
2268 * @param dev[in, out]
2269 * Pointer to rte_eth_dev structure.
2270 * @param[in, out] resource
2271 * Pointer to jump table resource.
2272 * @parm[in, out] dev_flow
2273 * Pointer to the dev_flow.
2275 * pointer to error structure.
2278 * 0 on success otherwise -errno and errno is set.
2281 flow_dv_jump_tbl_resource_register
2282 (struct rte_eth_dev *dev,
2283 struct mlx5_flow_dv_jump_tbl_resource *resource,
2284 struct mlx5_flow *dev_flow,
2285 struct rte_flow_error *error)
2287 struct mlx5_priv *priv = dev->data->dev_private;
2288 struct mlx5_ibv_shared *sh = priv->sh;
2289 struct mlx5_flow_dv_jump_tbl_resource *cache_resource;
2291 /* Lookup a matching resource from cache. */
2292 LIST_FOREACH(cache_resource, &sh->jump_tbl, next) {
2293 if (resource->tbl == cache_resource->tbl) {
2294 DRV_LOG(DEBUG, "jump table resource resource %p: refcnt %d++",
2295 (void *)cache_resource,
2296 rte_atomic32_read(&cache_resource->refcnt));
2297 rte_atomic32_inc(&cache_resource->refcnt);
2298 dev_flow->dv.jump = cache_resource;
2302 /* Register new jump table resource. */
2303 cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
2304 if (!cache_resource)
2305 return rte_flow_error_set(error, ENOMEM,
2306 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2307 "cannot allocate resource memory");
2308 *cache_resource = *resource;
2309 cache_resource->action =
2310 mlx5_glue->dr_create_flow_action_dest_flow_tbl
2311 (resource->tbl->obj);
2312 if (!cache_resource->action) {
2313 rte_free(cache_resource);
2314 return rte_flow_error_set(error, ENOMEM,
2315 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2316 NULL, "cannot create action");
2318 rte_atomic32_init(&cache_resource->refcnt);
2319 rte_atomic32_inc(&cache_resource->refcnt);
2320 LIST_INSERT_HEAD(&sh->jump_tbl, cache_resource, next);
2321 dev_flow->dv.jump = cache_resource;
2322 DRV_LOG(DEBUG, "new jump table resource %p: refcnt %d++",
2323 (void *)cache_resource,
2324 rte_atomic32_read(&cache_resource->refcnt));
2329 * Find existing table port ID resource or create and register a new one.
2331 * @param dev[in, out]
2332 * Pointer to rte_eth_dev structure.
2333 * @param[in, out] resource
2334 * Pointer to port ID action resource.
2335 * @parm[in, out] dev_flow
2336 * Pointer to the dev_flow.
2338 * pointer to error structure.
2341 * 0 on success otherwise -errno and errno is set.
2344 flow_dv_port_id_action_resource_register
2345 (struct rte_eth_dev *dev,
2346 struct mlx5_flow_dv_port_id_action_resource *resource,
2347 struct mlx5_flow *dev_flow,
2348 struct rte_flow_error *error)
2350 struct mlx5_priv *priv = dev->data->dev_private;
2351 struct mlx5_ibv_shared *sh = priv->sh;
2352 struct mlx5_flow_dv_port_id_action_resource *cache_resource;
2354 /* Lookup a matching resource from cache. */
2355 LIST_FOREACH(cache_resource, &sh->port_id_action_list, next) {
2356 if (resource->port_id == cache_resource->port_id) {
2357 DRV_LOG(DEBUG, "port id action resource resource %p: "
2359 (void *)cache_resource,
2360 rte_atomic32_read(&cache_resource->refcnt));
2361 rte_atomic32_inc(&cache_resource->refcnt);
2362 dev_flow->dv.port_id_action = cache_resource;
2366 /* Register new port id action resource. */
2367 cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
2368 if (!cache_resource)
2369 return rte_flow_error_set(error, ENOMEM,
2370 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2371 "cannot allocate resource memory");
2372 *cache_resource = *resource;
2373 cache_resource->action =
2374 mlx5_glue->dr_create_flow_action_dest_vport
2375 (priv->sh->fdb_domain, resource->port_id);
2376 if (!cache_resource->action) {
2377 rte_free(cache_resource);
2378 return rte_flow_error_set(error, ENOMEM,
2379 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2380 NULL, "cannot create action");
2382 rte_atomic32_init(&cache_resource->refcnt);
2383 rte_atomic32_inc(&cache_resource->refcnt);
2384 LIST_INSERT_HEAD(&sh->port_id_action_list, cache_resource, next);
2385 dev_flow->dv.port_id_action = cache_resource;
2386 DRV_LOG(DEBUG, "new port id action resource %p: refcnt %d++",
2387 (void *)cache_resource,
2388 rte_atomic32_read(&cache_resource->refcnt));
2393 * Find existing push vlan resource or create and register a new one.
2395 * @param dev[in, out]
2396 * Pointer to rte_eth_dev structure.
2397 * @param[in, out] resource
2398 * Pointer to port ID action resource.
2399 * @parm[in, out] dev_flow
2400 * Pointer to the dev_flow.
2402 * pointer to error structure.
2405 * 0 on success otherwise -errno and errno is set.
2408 flow_dv_push_vlan_action_resource_register
2409 (struct rte_eth_dev *dev,
2410 struct mlx5_flow_dv_push_vlan_action_resource *resource,
2411 struct mlx5_flow *dev_flow,
2412 struct rte_flow_error *error)
2414 struct mlx5_priv *priv = dev->data->dev_private;
2415 struct mlx5_ibv_shared *sh = priv->sh;
2416 struct mlx5_flow_dv_push_vlan_action_resource *cache_resource;
2417 struct mlx5dv_dr_domain *domain;
2419 /* Lookup a matching resource from cache. */
2420 LIST_FOREACH(cache_resource, &sh->push_vlan_action_list, next) {
2421 if (resource->vlan_tag == cache_resource->vlan_tag &&
2422 resource->ft_type == cache_resource->ft_type) {
2423 DRV_LOG(DEBUG, "push-VLAN action resource resource %p: "
2425 (void *)cache_resource,
2426 rte_atomic32_read(&cache_resource->refcnt));
2427 rte_atomic32_inc(&cache_resource->refcnt);
2428 dev_flow->dv.push_vlan_res = cache_resource;
2432 /* Register new push_vlan action resource. */
2433 cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
2434 if (!cache_resource)
2435 return rte_flow_error_set(error, ENOMEM,
2436 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2437 "cannot allocate resource memory");
2438 *cache_resource = *resource;
2439 if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
2440 domain = sh->fdb_domain;
2441 else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
2442 domain = sh->rx_domain;
2444 domain = sh->tx_domain;
2445 cache_resource->action =
2446 mlx5_glue->dr_create_flow_action_push_vlan(domain,
2447 resource->vlan_tag);
2448 if (!cache_resource->action) {
2449 rte_free(cache_resource);
2450 return rte_flow_error_set(error, ENOMEM,
2451 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2452 NULL, "cannot create action");
2454 rte_atomic32_init(&cache_resource->refcnt);
2455 rte_atomic32_inc(&cache_resource->refcnt);
2456 LIST_INSERT_HEAD(&sh->push_vlan_action_list, cache_resource, next);
2457 dev_flow->dv.push_vlan_res = cache_resource;
2458 DRV_LOG(DEBUG, "new push vlan action resource %p: refcnt %d++",
2459 (void *)cache_resource,
2460 rte_atomic32_read(&cache_resource->refcnt));
2464 * Get the size of specific rte_flow_item_type
2466 * @param[in] item_type
2467 * Tested rte_flow_item_type.
2470 * sizeof struct item_type, 0 if void or irrelevant.
2473 flow_dv_get_item_len(const enum rte_flow_item_type item_type)
2477 switch (item_type) {
2478 case RTE_FLOW_ITEM_TYPE_ETH:
2479 retval = sizeof(struct rte_flow_item_eth);
2481 case RTE_FLOW_ITEM_TYPE_VLAN:
2482 retval = sizeof(struct rte_flow_item_vlan);
2484 case RTE_FLOW_ITEM_TYPE_IPV4:
2485 retval = sizeof(struct rte_flow_item_ipv4);
2487 case RTE_FLOW_ITEM_TYPE_IPV6:
2488 retval = sizeof(struct rte_flow_item_ipv6);
2490 case RTE_FLOW_ITEM_TYPE_UDP:
2491 retval = sizeof(struct rte_flow_item_udp);
2493 case RTE_FLOW_ITEM_TYPE_TCP:
2494 retval = sizeof(struct rte_flow_item_tcp);
2496 case RTE_FLOW_ITEM_TYPE_VXLAN:
2497 retval = sizeof(struct rte_flow_item_vxlan);
2499 case RTE_FLOW_ITEM_TYPE_GRE:
2500 retval = sizeof(struct rte_flow_item_gre);
2502 case RTE_FLOW_ITEM_TYPE_NVGRE:
2503 retval = sizeof(struct rte_flow_item_nvgre);
2505 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
2506 retval = sizeof(struct rte_flow_item_vxlan_gpe);
2508 case RTE_FLOW_ITEM_TYPE_MPLS:
2509 retval = sizeof(struct rte_flow_item_mpls);
2511 case RTE_FLOW_ITEM_TYPE_VOID: /* Fall through. */
2519 #define MLX5_ENCAP_IPV4_VERSION 0x40
2520 #define MLX5_ENCAP_IPV4_IHL_MIN 0x05
2521 #define MLX5_ENCAP_IPV4_TTL_DEF 0x40
2522 #define MLX5_ENCAP_IPV6_VTC_FLOW 0x60000000
2523 #define MLX5_ENCAP_IPV6_HOP_LIMIT 0xff
2524 #define MLX5_ENCAP_VXLAN_FLAGS 0x08000000
2525 #define MLX5_ENCAP_VXLAN_GPE_FLAGS 0x04
2528 * Convert the encap action data from list of rte_flow_item to raw buffer
2531 * Pointer to rte_flow_item objects list.
2533 * Pointer to the output buffer.
2535 * Pointer to the output buffer size.
2537 * Pointer to the error structure.
2540 * 0 on success, a negative errno value otherwise and rte_errno is set.
2543 flow_dv_convert_encap_data(const struct rte_flow_item *items, uint8_t *buf,
2544 size_t *size, struct rte_flow_error *error)
2546 struct rte_ether_hdr *eth = NULL;
2547 struct rte_vlan_hdr *vlan = NULL;
2548 struct rte_ipv4_hdr *ipv4 = NULL;
2549 struct rte_ipv6_hdr *ipv6 = NULL;
2550 struct rte_udp_hdr *udp = NULL;
2551 struct rte_vxlan_hdr *vxlan = NULL;
2552 struct rte_vxlan_gpe_hdr *vxlan_gpe = NULL;
2553 struct rte_gre_hdr *gre = NULL;
2555 size_t temp_size = 0;
2558 return rte_flow_error_set(error, EINVAL,
2559 RTE_FLOW_ERROR_TYPE_ACTION,
2560 NULL, "invalid empty data");
2561 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
2562 len = flow_dv_get_item_len(items->type);
2563 if (len + temp_size > MLX5_ENCAP_MAX_LEN)
2564 return rte_flow_error_set(error, EINVAL,
2565 RTE_FLOW_ERROR_TYPE_ACTION,
2566 (void *)items->type,
2567 "items total size is too big"
2568 " for encap action");
2569 rte_memcpy((void *)&buf[temp_size], items->spec, len);
2570 switch (items->type) {
2571 case RTE_FLOW_ITEM_TYPE_ETH:
2572 eth = (struct rte_ether_hdr *)&buf[temp_size];
2574 case RTE_FLOW_ITEM_TYPE_VLAN:
2575 vlan = (struct rte_vlan_hdr *)&buf[temp_size];
2577 return rte_flow_error_set(error, EINVAL,
2578 RTE_FLOW_ERROR_TYPE_ACTION,
2579 (void *)items->type,
2580 "eth header not found");
2581 if (!eth->ether_type)
2582 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_VLAN);
2584 case RTE_FLOW_ITEM_TYPE_IPV4:
2585 ipv4 = (struct rte_ipv4_hdr *)&buf[temp_size];
2587 return rte_flow_error_set(error, EINVAL,
2588 RTE_FLOW_ERROR_TYPE_ACTION,
2589 (void *)items->type,
2590 "neither eth nor vlan"
2592 if (vlan && !vlan->eth_proto)
2593 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV4);
2594 else if (eth && !eth->ether_type)
2595 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV4);
2596 if (!ipv4->version_ihl)
2597 ipv4->version_ihl = MLX5_ENCAP_IPV4_VERSION |
2598 MLX5_ENCAP_IPV4_IHL_MIN;
2599 if (!ipv4->time_to_live)
2600 ipv4->time_to_live = MLX5_ENCAP_IPV4_TTL_DEF;
2602 case RTE_FLOW_ITEM_TYPE_IPV6:
2603 ipv6 = (struct rte_ipv6_hdr *)&buf[temp_size];
2605 return rte_flow_error_set(error, EINVAL,
2606 RTE_FLOW_ERROR_TYPE_ACTION,
2607 (void *)items->type,
2608 "neither eth nor vlan"
2610 if (vlan && !vlan->eth_proto)
2611 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV6);
2612 else if (eth && !eth->ether_type)
2613 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV6);
2614 if (!ipv6->vtc_flow)
2616 RTE_BE32(MLX5_ENCAP_IPV6_VTC_FLOW);
2617 if (!ipv6->hop_limits)
2618 ipv6->hop_limits = MLX5_ENCAP_IPV6_HOP_LIMIT;
2620 case RTE_FLOW_ITEM_TYPE_UDP:
2621 udp = (struct rte_udp_hdr *)&buf[temp_size];
2623 return rte_flow_error_set(error, EINVAL,
2624 RTE_FLOW_ERROR_TYPE_ACTION,
2625 (void *)items->type,
2626 "ip header not found");
2627 if (ipv4 && !ipv4->next_proto_id)
2628 ipv4->next_proto_id = IPPROTO_UDP;
2629 else if (ipv6 && !ipv6->proto)
2630 ipv6->proto = IPPROTO_UDP;
2632 case RTE_FLOW_ITEM_TYPE_VXLAN:
2633 vxlan = (struct rte_vxlan_hdr *)&buf[temp_size];
2635 return rte_flow_error_set(error, EINVAL,
2636 RTE_FLOW_ERROR_TYPE_ACTION,
2637 (void *)items->type,
2638 "udp header not found");
2640 udp->dst_port = RTE_BE16(MLX5_UDP_PORT_VXLAN);
2641 if (!vxlan->vx_flags)
2643 RTE_BE32(MLX5_ENCAP_VXLAN_FLAGS);
2645 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
2646 vxlan_gpe = (struct rte_vxlan_gpe_hdr *)&buf[temp_size];
2648 return rte_flow_error_set(error, EINVAL,
2649 RTE_FLOW_ERROR_TYPE_ACTION,
2650 (void *)items->type,
2651 "udp header not found");
2652 if (!vxlan_gpe->proto)
2653 return rte_flow_error_set(error, EINVAL,
2654 RTE_FLOW_ERROR_TYPE_ACTION,
2655 (void *)items->type,
2656 "next protocol not found");
2659 RTE_BE16(MLX5_UDP_PORT_VXLAN_GPE);
2660 if (!vxlan_gpe->vx_flags)
2661 vxlan_gpe->vx_flags =
2662 MLX5_ENCAP_VXLAN_GPE_FLAGS;
2664 case RTE_FLOW_ITEM_TYPE_GRE:
2665 case RTE_FLOW_ITEM_TYPE_NVGRE:
2666 gre = (struct rte_gre_hdr *)&buf[temp_size];
2668 return rte_flow_error_set(error, EINVAL,
2669 RTE_FLOW_ERROR_TYPE_ACTION,
2670 (void *)items->type,
2671 "next protocol not found");
2673 return rte_flow_error_set(error, EINVAL,
2674 RTE_FLOW_ERROR_TYPE_ACTION,
2675 (void *)items->type,
2676 "ip header not found");
2677 if (ipv4 && !ipv4->next_proto_id)
2678 ipv4->next_proto_id = IPPROTO_GRE;
2679 else if (ipv6 && !ipv6->proto)
2680 ipv6->proto = IPPROTO_GRE;
2682 case RTE_FLOW_ITEM_TYPE_VOID:
2685 return rte_flow_error_set(error, EINVAL,
2686 RTE_FLOW_ERROR_TYPE_ACTION,
2687 (void *)items->type,
2688 "unsupported item type");
2698 flow_dv_zero_encap_udp_csum(void *data, struct rte_flow_error *error)
2700 struct rte_ether_hdr *eth = NULL;
2701 struct rte_vlan_hdr *vlan = NULL;
2702 struct rte_ipv6_hdr *ipv6 = NULL;
2703 struct rte_udp_hdr *udp = NULL;
2707 eth = (struct rte_ether_hdr *)data;
2708 next_hdr = (char *)(eth + 1);
2709 proto = RTE_BE16(eth->ether_type);
2712 while (proto == RTE_ETHER_TYPE_VLAN || proto == RTE_ETHER_TYPE_QINQ) {
2713 vlan = (struct rte_vlan_hdr *)next_hdr;
2714 proto = RTE_BE16(vlan->eth_proto);
2715 next_hdr += sizeof(struct rte_vlan_hdr);
2718 /* HW calculates IPv4 csum. no need to proceed */
2719 if (proto == RTE_ETHER_TYPE_IPV4)
2722 /* non IPv4/IPv6 header. not supported */
2723 if (proto != RTE_ETHER_TYPE_IPV6) {
2724 return rte_flow_error_set(error, ENOTSUP,
2725 RTE_FLOW_ERROR_TYPE_ACTION,
2726 NULL, "Cannot offload non IPv4/IPv6");
2729 ipv6 = (struct rte_ipv6_hdr *)next_hdr;
2731 /* ignore non UDP */
2732 if (ipv6->proto != IPPROTO_UDP)
2735 udp = (struct rte_udp_hdr *)(ipv6 + 1);
2736 udp->dgram_cksum = 0;
2742 * Convert L2 encap action to DV specification.
2745 * Pointer to rte_eth_dev structure.
2747 * Pointer to action structure.
2748 * @param[in, out] dev_flow
2749 * Pointer to the mlx5_flow.
2750 * @param[in] transfer
2751 * Mark if the flow is E-Switch flow.
2753 * Pointer to the error structure.
2756 * 0 on success, a negative errno value otherwise and rte_errno is set.
2759 flow_dv_create_action_l2_encap(struct rte_eth_dev *dev,
2760 const struct rte_flow_action *action,
2761 struct mlx5_flow *dev_flow,
2763 struct rte_flow_error *error)
2765 const struct rte_flow_item *encap_data;
2766 const struct rte_flow_action_raw_encap *raw_encap_data;
2767 struct mlx5_flow_dv_encap_decap_resource res = {
2769 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL,
2770 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
2771 MLX5DV_FLOW_TABLE_TYPE_NIC_TX,
2774 if (action->type == RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
2776 (const struct rte_flow_action_raw_encap *)action->conf;
2777 res.size = raw_encap_data->size;
2778 memcpy(res.buf, raw_encap_data->data, res.size);
2779 if (flow_dv_zero_encap_udp_csum(res.buf, error))
2782 if (action->type == RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP)
2784 ((const struct rte_flow_action_vxlan_encap *)
2785 action->conf)->definition;
2788 ((const struct rte_flow_action_nvgre_encap *)
2789 action->conf)->definition;
2790 if (flow_dv_convert_encap_data(encap_data, res.buf,
2794 if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
2795 return rte_flow_error_set(error, EINVAL,
2796 RTE_FLOW_ERROR_TYPE_ACTION,
2797 NULL, "can't create L2 encap action");
2802 * Convert L2 decap action to DV specification.
2805 * Pointer to rte_eth_dev structure.
2806 * @param[in, out] dev_flow
2807 * Pointer to the mlx5_flow.
2808 * @param[in] transfer
2809 * Mark if the flow is E-Switch flow.
2811 * Pointer to the error structure.
2814 * 0 on success, a negative errno value otherwise and rte_errno is set.
2817 flow_dv_create_action_l2_decap(struct rte_eth_dev *dev,
2818 struct mlx5_flow *dev_flow,
2820 struct rte_flow_error *error)
2822 struct mlx5_flow_dv_encap_decap_resource res = {
2825 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2,
2826 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
2827 MLX5DV_FLOW_TABLE_TYPE_NIC_RX,
2830 if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
2831 return rte_flow_error_set(error, EINVAL,
2832 RTE_FLOW_ERROR_TYPE_ACTION,
2833 NULL, "can't create L2 decap action");
2838 * Convert raw decap/encap (L3 tunnel) action to DV specification.
2841 * Pointer to rte_eth_dev structure.
2843 * Pointer to action structure.
2844 * @param[in, out] dev_flow
2845 * Pointer to the mlx5_flow.
2847 * Pointer to the flow attributes.
2849 * Pointer to the error structure.
2852 * 0 on success, a negative errno value otherwise and rte_errno is set.
2855 flow_dv_create_action_raw_encap(struct rte_eth_dev *dev,
2856 const struct rte_flow_action *action,
2857 struct mlx5_flow *dev_flow,
2858 const struct rte_flow_attr *attr,
2859 struct rte_flow_error *error)
2861 const struct rte_flow_action_raw_encap *encap_data;
2862 struct mlx5_flow_dv_encap_decap_resource res;
2864 encap_data = (const struct rte_flow_action_raw_encap *)action->conf;
2865 res.size = encap_data->size;
2866 memcpy(res.buf, encap_data->data, res.size);
2867 res.reformat_type = attr->egress ?
2868 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L3_TUNNEL :
2869 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L3_TUNNEL_TO_L2;
2871 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
2873 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
2874 MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
2875 if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
2876 return rte_flow_error_set(error, EINVAL,
2877 RTE_FLOW_ERROR_TYPE_ACTION,
2878 NULL, "can't create encap action");
2883 * Create action push VLAN.
2886 * Pointer to rte_eth_dev structure.
2887 * @param[in] vlan_tag
2888 * the vlan tag to push to the Ethernet header.
2889 * @param[in, out] dev_flow
2890 * Pointer to the mlx5_flow.
2892 * Pointer to the flow attributes.
2894 * Pointer to the error structure.
2897 * 0 on success, a negative errno value otherwise and rte_errno is set.
2900 flow_dv_create_action_push_vlan(struct rte_eth_dev *dev,
2901 const struct rte_flow_attr *attr,
2902 const struct rte_vlan_hdr *vlan,
2903 struct mlx5_flow *dev_flow,
2904 struct rte_flow_error *error)
2906 struct mlx5_flow_dv_push_vlan_action_resource res;
2909 rte_cpu_to_be_32(((uint32_t)vlan->eth_proto) << 16 |
2912 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
2914 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
2915 MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
2916 return flow_dv_push_vlan_action_resource_register
2917 (dev, &res, dev_flow, error);
2921 * Validate the modify-header actions.
2923 * @param[in] action_flags
2924 * Holds the actions detected until now.
2926 * Pointer to the modify action.
2928 * Pointer to error structure.
2931 * 0 on success, a negative errno value otherwise and rte_errno is set.
2934 flow_dv_validate_action_modify_hdr(const uint64_t action_flags,
2935 const struct rte_flow_action *action,
2936 struct rte_flow_error *error)
2938 if (action->type != RTE_FLOW_ACTION_TYPE_DEC_TTL && !action->conf)
2939 return rte_flow_error_set(error, EINVAL,
2940 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2941 NULL, "action configuration not set");
2942 if (action_flags & MLX5_FLOW_ENCAP_ACTIONS)
2943 return rte_flow_error_set(error, EINVAL,
2944 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2945 "can't have encap action before"
2951 * Validate the modify-header MAC address actions.
2953 * @param[in] action_flags
2954 * Holds the actions detected until now.
2956 * Pointer to the modify action.
2957 * @param[in] item_flags
2958 * Holds the items detected.
2960 * Pointer to error structure.
2963 * 0 on success, a negative errno value otherwise and rte_errno is set.
2966 flow_dv_validate_action_modify_mac(const uint64_t action_flags,
2967 const struct rte_flow_action *action,
2968 const uint64_t item_flags,
2969 struct rte_flow_error *error)
2973 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
2975 if (!(item_flags & MLX5_FLOW_LAYER_L2))
2976 return rte_flow_error_set(error, EINVAL,
2977 RTE_FLOW_ERROR_TYPE_ACTION,
2979 "no L2 item in pattern");
2985 * Validate the modify-header IPv4 address actions.
2987 * @param[in] action_flags
2988 * Holds the actions detected until now.
2990 * Pointer to the modify action.
2991 * @param[in] item_flags
2992 * Holds the items detected.
2994 * Pointer to error structure.
2997 * 0 on success, a negative errno value otherwise and rte_errno is set.
3000 flow_dv_validate_action_modify_ipv4(const uint64_t action_flags,
3001 const struct rte_flow_action *action,
3002 const uint64_t item_flags,
3003 struct rte_flow_error *error)
3007 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3009 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV4))
3010 return rte_flow_error_set(error, EINVAL,
3011 RTE_FLOW_ERROR_TYPE_ACTION,
3013 "no ipv4 item in pattern");
3019 * Validate the modify-header IPv6 address actions.
3021 * @param[in] action_flags
3022 * Holds the actions detected until now.
3024 * Pointer to the modify action.
3025 * @param[in] item_flags
3026 * Holds the items detected.
3028 * Pointer to error structure.
3031 * 0 on success, a negative errno value otherwise and rte_errno is set.
3034 flow_dv_validate_action_modify_ipv6(const uint64_t action_flags,
3035 const struct rte_flow_action *action,
3036 const uint64_t item_flags,
3037 struct rte_flow_error *error)
3041 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3043 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV6))
3044 return rte_flow_error_set(error, EINVAL,
3045 RTE_FLOW_ERROR_TYPE_ACTION,
3047 "no ipv6 item in pattern");
3053 * Validate the modify-header TP actions.
3055 * @param[in] action_flags
3056 * Holds the actions detected until now.
3058 * Pointer to the modify action.
3059 * @param[in] item_flags
3060 * Holds the items detected.
3062 * Pointer to error structure.
3065 * 0 on success, a negative errno value otherwise and rte_errno is set.
3068 flow_dv_validate_action_modify_tp(const uint64_t action_flags,
3069 const struct rte_flow_action *action,
3070 const uint64_t item_flags,
3071 struct rte_flow_error *error)
3075 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3077 if (!(item_flags & MLX5_FLOW_LAYER_L4))
3078 return rte_flow_error_set(error, EINVAL,
3079 RTE_FLOW_ERROR_TYPE_ACTION,
3080 NULL, "no transport layer "
3087 * Validate the modify-header actions of increment/decrement
3088 * TCP Sequence-number.
3090 * @param[in] action_flags
3091 * Holds the actions detected until now.
3093 * Pointer to the modify action.
3094 * @param[in] item_flags
3095 * Holds the items detected.
3097 * Pointer to error structure.
3100 * 0 on success, a negative errno value otherwise and rte_errno is set.
3103 flow_dv_validate_action_modify_tcp_seq(const uint64_t action_flags,
3104 const struct rte_flow_action *action,
3105 const uint64_t item_flags,
3106 struct rte_flow_error *error)
3110 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3112 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_TCP))
3113 return rte_flow_error_set(error, EINVAL,
3114 RTE_FLOW_ERROR_TYPE_ACTION,
3115 NULL, "no TCP item in"
3117 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ &&
3118 (action_flags & MLX5_FLOW_ACTION_DEC_TCP_SEQ)) ||
3119 (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ &&
3120 (action_flags & MLX5_FLOW_ACTION_INC_TCP_SEQ)))
3121 return rte_flow_error_set(error, EINVAL,
3122 RTE_FLOW_ERROR_TYPE_ACTION,
3124 "cannot decrease and increase"
3125 " TCP sequence number"
3126 " at the same time");
3132 * Validate the modify-header actions of increment/decrement
3133 * TCP Acknowledgment number.
3135 * @param[in] action_flags
3136 * Holds the actions detected until now.
3138 * Pointer to the modify action.
3139 * @param[in] item_flags
3140 * Holds the items detected.
3142 * Pointer to error structure.
3145 * 0 on success, a negative errno value otherwise and rte_errno is set.
3148 flow_dv_validate_action_modify_tcp_ack(const uint64_t action_flags,
3149 const struct rte_flow_action *action,
3150 const uint64_t item_flags,
3151 struct rte_flow_error *error)
3155 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3157 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_TCP))
3158 return rte_flow_error_set(error, EINVAL,
3159 RTE_FLOW_ERROR_TYPE_ACTION,
3160 NULL, "no TCP item in"
3162 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_ACK &&
3163 (action_flags & MLX5_FLOW_ACTION_DEC_TCP_ACK)) ||
3164 (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK &&
3165 (action_flags & MLX5_FLOW_ACTION_INC_TCP_ACK)))
3166 return rte_flow_error_set(error, EINVAL,
3167 RTE_FLOW_ERROR_TYPE_ACTION,
3169 "cannot decrease and increase"
3170 " TCP acknowledgment number"
3171 " at the same time");
3177 * Validate the modify-header TTL actions.
3179 * @param[in] action_flags
3180 * Holds the actions detected until now.
3182 * Pointer to the modify action.
3183 * @param[in] item_flags
3184 * Holds the items detected.
3186 * Pointer to error structure.
3189 * 0 on success, a negative errno value otherwise and rte_errno is set.
3192 flow_dv_validate_action_modify_ttl(const uint64_t action_flags,
3193 const struct rte_flow_action *action,
3194 const uint64_t item_flags,
3195 struct rte_flow_error *error)
3199 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3201 if (!(item_flags & MLX5_FLOW_LAYER_L3))
3202 return rte_flow_error_set(error, EINVAL,
3203 RTE_FLOW_ERROR_TYPE_ACTION,
3205 "no IP protocol in pattern");
3211 * Validate jump action.
3214 * Pointer to the jump action.
3215 * @param[in] action_flags
3216 * Holds the actions detected until now.
3217 * @param[in] attributes
3218 * Pointer to flow attributes
3219 * @param[in] external
3220 * Action belongs to flow rule created by request external to PMD.
3222 * Pointer to error structure.
3225 * 0 on success, a negative errno value otherwise and rte_errno is set.
3228 flow_dv_validate_action_jump(const struct rte_flow_action *action,
3229 uint64_t action_flags,
3230 const struct rte_flow_attr *attributes,
3231 bool external, struct rte_flow_error *error)
3233 uint32_t max_group = attributes->transfer ? MLX5_MAX_TABLES_FDB :
3235 uint32_t target_group, table;
3238 if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
3239 MLX5_FLOW_FATE_ESWITCH_ACTIONS))
3240 return rte_flow_error_set(error, EINVAL,
3241 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3242 "can't have 2 fate actions in"
3245 return rte_flow_error_set(error, EINVAL,
3246 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3247 NULL, "action configuration not set");
3249 ((const struct rte_flow_action_jump *)action->conf)->group;
3250 ret = mlx5_flow_group_to_table(attributes, external, target_group,
3254 if (table >= max_group)
3255 return rte_flow_error_set(error, EINVAL,
3256 RTE_FLOW_ERROR_TYPE_ATTR_GROUP, NULL,
3257 "target group index out of range");
3258 if (attributes->group == target_group)
3259 return rte_flow_error_set(error, EINVAL,
3260 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3261 "target group must be other than"
3262 " the current flow group");
3267 * Validate the port_id action.
3270 * Pointer to rte_eth_dev structure.
3271 * @param[in] action_flags
3272 * Bit-fields that holds the actions detected until now.
3274 * Port_id RTE action structure.
3276 * Attributes of flow that includes this action.
3278 * Pointer to error structure.
3281 * 0 on success, a negative errno value otherwise and rte_errno is set.
3284 flow_dv_validate_action_port_id(struct rte_eth_dev *dev,
3285 uint64_t action_flags,
3286 const struct rte_flow_action *action,
3287 const struct rte_flow_attr *attr,
3288 struct rte_flow_error *error)
3290 const struct rte_flow_action_port_id *port_id;
3291 struct mlx5_priv *act_priv;
3292 struct mlx5_priv *dev_priv;
3295 if (!attr->transfer)
3296 return rte_flow_error_set(error, ENOTSUP,
3297 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3299 "port id action is valid in transfer"
3301 if (!action || !action->conf)
3302 return rte_flow_error_set(error, ENOTSUP,
3303 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3305 "port id action parameters must be"
3307 if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
3308 MLX5_FLOW_FATE_ESWITCH_ACTIONS))
3309 return rte_flow_error_set(error, EINVAL,
3310 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3311 "can have only one fate actions in"
3313 dev_priv = mlx5_dev_to_eswitch_info(dev);
3315 return rte_flow_error_set(error, rte_errno,
3316 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3318 "failed to obtain E-Switch info");
3319 port_id = action->conf;
3320 port = port_id->original ? dev->data->port_id : port_id->id;
3321 act_priv = mlx5_port_to_eswitch_info(port, false);
3323 return rte_flow_error_set
3325 RTE_FLOW_ERROR_TYPE_ACTION_CONF, port_id,
3326 "failed to obtain E-Switch port id for port");
3327 if (act_priv->domain_id != dev_priv->domain_id)
3328 return rte_flow_error_set
3330 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3331 "port does not belong to"
3332 " E-Switch being configured");
3337 * Get the maximum number of modify header actions.
3340 * Pointer to rte_eth_dev structure.
3343 * Max number of modify header actions device can support.
3346 flow_dv_modify_hdr_action_max(struct rte_eth_dev *dev)
3349 * There's no way to directly query the max cap. Although it has to be
3350 * acquried by iterative trial, it is a safe assumption that more
3351 * actions are supported by FW if extensive metadata register is
3354 return mlx5_flow_ext_mreg_supported(dev) ? MLX5_MODIFY_NUM :
3355 MLX5_MODIFY_NUM_NO_MREG;
3358 * Find existing modify-header resource or create and register a new one.
3360 * @param dev[in, out]
3361 * Pointer to rte_eth_dev structure.
3362 * @param[in, out] resource
3363 * Pointer to modify-header resource.
3364 * @parm[in, out] dev_flow
3365 * Pointer to the dev_flow.
3367 * pointer to error structure.
3370 * 0 on success otherwise -errno and errno is set.
3373 flow_dv_modify_hdr_resource_register
3374 (struct rte_eth_dev *dev,
3375 struct mlx5_flow_dv_modify_hdr_resource *resource,
3376 struct mlx5_flow *dev_flow,
3377 struct rte_flow_error *error)
3379 struct mlx5_priv *priv = dev->data->dev_private;
3380 struct mlx5_ibv_shared *sh = priv->sh;
3381 struct mlx5_flow_dv_modify_hdr_resource *cache_resource;
3382 struct mlx5dv_dr_domain *ns;
3384 if (resource->actions_num > flow_dv_modify_hdr_action_max(dev))
3385 return rte_flow_error_set(error, EOVERFLOW,
3386 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3387 "too many modify header items");
3388 if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
3389 ns = sh->fdb_domain;
3390 else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_TX)
3395 dev_flow->group ? 0 : MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL;
3396 /* Lookup a matching resource from cache. */
3397 LIST_FOREACH(cache_resource, &sh->modify_cmds, next) {
3398 if (resource->ft_type == cache_resource->ft_type &&
3399 resource->actions_num == cache_resource->actions_num &&
3400 resource->flags == cache_resource->flags &&
3401 !memcmp((const void *)resource->actions,
3402 (const void *)cache_resource->actions,
3403 (resource->actions_num *
3404 sizeof(resource->actions[0])))) {
3405 DRV_LOG(DEBUG, "modify-header resource %p: refcnt %d++",
3406 (void *)cache_resource,
3407 rte_atomic32_read(&cache_resource->refcnt));
3408 rte_atomic32_inc(&cache_resource->refcnt);
3409 dev_flow->dv.modify_hdr = cache_resource;
3413 /* Register new modify-header resource. */
3414 cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
3415 if (!cache_resource)
3416 return rte_flow_error_set(error, ENOMEM,
3417 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3418 "cannot allocate resource memory");
3419 *cache_resource = *resource;
3420 cache_resource->verbs_action =
3421 mlx5_glue->dv_create_flow_action_modify_header
3422 (sh->ctx, cache_resource->ft_type,
3423 ns, cache_resource->flags,
3424 cache_resource->actions_num *
3425 sizeof(cache_resource->actions[0]),
3426 (uint64_t *)cache_resource->actions);
3427 if (!cache_resource->verbs_action) {
3428 rte_free(cache_resource);
3429 return rte_flow_error_set(error, ENOMEM,
3430 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3431 NULL, "cannot create action");
3433 rte_atomic32_init(&cache_resource->refcnt);
3434 rte_atomic32_inc(&cache_resource->refcnt);
3435 LIST_INSERT_HEAD(&sh->modify_cmds, cache_resource, next);
3436 dev_flow->dv.modify_hdr = cache_resource;
3437 DRV_LOG(DEBUG, "new modify-header resource %p: refcnt %d++",
3438 (void *)cache_resource,
3439 rte_atomic32_read(&cache_resource->refcnt));
3443 #define MLX5_CNT_CONTAINER_RESIZE 64
3446 * Get or create a flow counter.
3449 * Pointer to the Ethernet device structure.
3451 * Indicate if this counter is shared with other flows.
3453 * Counter identifier.
3456 * pointer to flow counter on success, NULL otherwise and rte_errno is set.
3458 static struct mlx5_flow_counter *
3459 flow_dv_counter_alloc_fallback(struct rte_eth_dev *dev, uint32_t shared,
3462 struct mlx5_priv *priv = dev->data->dev_private;
3463 struct mlx5_flow_counter *cnt = NULL;
3464 struct mlx5_devx_obj *dcs = NULL;
3466 if (!priv->config.devx) {
3467 rte_errno = ENOTSUP;
3471 TAILQ_FOREACH(cnt, &priv->sh->cmng.flow_counters, next) {
3472 if (cnt->shared && cnt->id == id) {
3478 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0);
3481 cnt = rte_calloc(__func__, 1, sizeof(*cnt), 0);
3483 claim_zero(mlx5_devx_cmd_destroy(cnt->dcs));
3487 struct mlx5_flow_counter tmpl = {
3493 tmpl.action = mlx5_glue->dv_create_flow_action_counter(dcs->obj, 0);
3495 claim_zero(mlx5_devx_cmd_destroy(cnt->dcs));
3501 TAILQ_INSERT_HEAD(&priv->sh->cmng.flow_counters, cnt, next);
3506 * Release a flow counter.
3509 * Pointer to the Ethernet device structure.
3510 * @param[in] counter
3511 * Pointer to the counter handler.
3514 flow_dv_counter_release_fallback(struct rte_eth_dev *dev,
3515 struct mlx5_flow_counter *counter)
3517 struct mlx5_priv *priv = dev->data->dev_private;
3521 if (--counter->ref_cnt == 0) {
3522 TAILQ_REMOVE(&priv->sh->cmng.flow_counters, counter, next);
3523 claim_zero(mlx5_devx_cmd_destroy(counter->dcs));
3529 * Query a devx flow counter.
3532 * Pointer to the Ethernet device structure.
3534 * Pointer to the flow counter.
3536 * The statistics value of packets.
3538 * The statistics value of bytes.
3541 * 0 on success, otherwise a negative errno value and rte_errno is set.
3544 _flow_dv_query_count_fallback(struct rte_eth_dev *dev __rte_unused,
3545 struct mlx5_flow_counter *cnt, uint64_t *pkts,
3548 return mlx5_devx_cmd_flow_counter_query(cnt->dcs, 0, 0, pkts, bytes,
3553 * Get a pool by a counter.
3556 * Pointer to the counter.
3561 static struct mlx5_flow_counter_pool *
3562 flow_dv_counter_pool_get(struct mlx5_flow_counter *cnt)
3565 cnt -= cnt->dcs->id % MLX5_COUNTERS_PER_POOL;
3566 return (struct mlx5_flow_counter_pool *)cnt - 1;
3572 * Get a pool by devx counter ID.
3575 * Pointer to the counter container.
3577 * The counter devx ID.
3580 * The counter pool pointer if exists, NULL otherwise,
3582 static struct mlx5_flow_counter_pool *
3583 flow_dv_find_pool_by_id(struct mlx5_pools_container *cont, int id)
3585 struct mlx5_flow_counter_pool *pool;
3587 TAILQ_FOREACH(pool, &cont->pool_list, next) {
3588 int base = (pool->min_dcs->id / MLX5_COUNTERS_PER_POOL) *
3589 MLX5_COUNTERS_PER_POOL;
3591 if (id >= base && id < base + MLX5_COUNTERS_PER_POOL)
3598 * Allocate a new memory for the counter values wrapped by all the needed
3602 * Pointer to the Ethernet device structure.
3604 * The raw memory areas - each one for MLX5_COUNTERS_PER_POOL counters.
3607 * The new memory management pointer on success, otherwise NULL and rte_errno
3610 static struct mlx5_counter_stats_mem_mng *
3611 flow_dv_create_counter_stat_mem_mng(struct rte_eth_dev *dev, int raws_n)
3613 struct mlx5_ibv_shared *sh = ((struct mlx5_priv *)
3614 (dev->data->dev_private))->sh;
3615 struct mlx5_devx_mkey_attr mkey_attr;
3616 struct mlx5_counter_stats_mem_mng *mem_mng;
3617 volatile struct flow_counter_stats *raw_data;
3618 int size = (sizeof(struct flow_counter_stats) *
3619 MLX5_COUNTERS_PER_POOL +
3620 sizeof(struct mlx5_counter_stats_raw)) * raws_n +
3621 sizeof(struct mlx5_counter_stats_mem_mng);
3622 uint8_t *mem = rte_calloc(__func__, 1, size, sysconf(_SC_PAGESIZE));
3629 mem_mng = (struct mlx5_counter_stats_mem_mng *)(mem + size) - 1;
3630 size = sizeof(*raw_data) * MLX5_COUNTERS_PER_POOL * raws_n;
3631 mem_mng->umem = mlx5_glue->devx_umem_reg(sh->ctx, mem, size,
3632 IBV_ACCESS_LOCAL_WRITE);
3633 if (!mem_mng->umem) {
3638 mkey_attr.addr = (uintptr_t)mem;
3639 mkey_attr.size = size;
3640 mkey_attr.umem_id = mem_mng->umem->umem_id;
3641 mkey_attr.pd = sh->pdn;
3642 mem_mng->dm = mlx5_devx_cmd_mkey_create(sh->ctx, &mkey_attr);
3644 mlx5_glue->devx_umem_dereg(mem_mng->umem);
3649 mem_mng->raws = (struct mlx5_counter_stats_raw *)(mem + size);
3650 raw_data = (volatile struct flow_counter_stats *)mem;
3651 for (i = 0; i < raws_n; ++i) {
3652 mem_mng->raws[i].mem_mng = mem_mng;
3653 mem_mng->raws[i].data = raw_data + i * MLX5_COUNTERS_PER_POOL;
3655 LIST_INSERT_HEAD(&sh->cmng.mem_mngs, mem_mng, next);
3660 * Resize a counter container.
3663 * Pointer to the Ethernet device structure.
3665 * Whether the pool is for counter that was allocated by batch command.
3668 * The new container pointer on success, otherwise NULL and rte_errno is set.
3670 static struct mlx5_pools_container *
3671 flow_dv_container_resize(struct rte_eth_dev *dev, uint32_t batch)
3673 struct mlx5_priv *priv = dev->data->dev_private;
3674 struct mlx5_pools_container *cont =
3675 MLX5_CNT_CONTAINER(priv->sh, batch, 0);
3676 struct mlx5_pools_container *new_cont =
3677 MLX5_CNT_CONTAINER_UNUSED(priv->sh, batch, 0);
3678 struct mlx5_counter_stats_mem_mng *mem_mng;
3679 uint32_t resize = cont->n + MLX5_CNT_CONTAINER_RESIZE;
3680 uint32_t mem_size = sizeof(struct mlx5_flow_counter_pool *) * resize;
3683 if (cont != MLX5_CNT_CONTAINER(priv->sh, batch, 1)) {
3684 /* The last resize still hasn't detected by the host thread. */
3688 new_cont->pools = rte_calloc(__func__, 1, mem_size, 0);
3689 if (!new_cont->pools) {
3694 memcpy(new_cont->pools, cont->pools, cont->n *
3695 sizeof(struct mlx5_flow_counter_pool *));
3696 mem_mng = flow_dv_create_counter_stat_mem_mng(dev,
3697 MLX5_CNT_CONTAINER_RESIZE + MLX5_MAX_PENDING_QUERIES);
3699 rte_free(new_cont->pools);
3702 for (i = 0; i < MLX5_MAX_PENDING_QUERIES; ++i)
3703 LIST_INSERT_HEAD(&priv->sh->cmng.free_stat_raws,
3704 mem_mng->raws + MLX5_CNT_CONTAINER_RESIZE +
3706 new_cont->n = resize;
3707 rte_atomic16_set(&new_cont->n_valid, rte_atomic16_read(&cont->n_valid));
3708 TAILQ_INIT(&new_cont->pool_list);
3709 TAILQ_CONCAT(&new_cont->pool_list, &cont->pool_list, next);
3710 new_cont->init_mem_mng = mem_mng;
3712 /* Flip the master container. */
3713 priv->sh->cmng.mhi[batch] ^= (uint8_t)1;
3718 * Query a devx flow counter.
3721 * Pointer to the Ethernet device structure.
3723 * Pointer to the flow counter.
3725 * The statistics value of packets.
3727 * The statistics value of bytes.
3730 * 0 on success, otherwise a negative errno value and rte_errno is set.
3733 _flow_dv_query_count(struct rte_eth_dev *dev,
3734 struct mlx5_flow_counter *cnt, uint64_t *pkts,
3737 struct mlx5_priv *priv = dev->data->dev_private;
3738 struct mlx5_flow_counter_pool *pool =
3739 flow_dv_counter_pool_get(cnt);
3740 int offset = cnt - &pool->counters_raw[0];
3742 if (priv->counter_fallback)
3743 return _flow_dv_query_count_fallback(dev, cnt, pkts, bytes);
3745 rte_spinlock_lock(&pool->sl);
3747 * The single counters allocation may allocate smaller ID than the
3748 * current allocated in parallel to the host reading.
3749 * In this case the new counter values must be reported as 0.
3751 if (unlikely(!cnt->batch && cnt->dcs->id < pool->raw->min_dcs_id)) {
3755 *pkts = rte_be_to_cpu_64(pool->raw->data[offset].hits);
3756 *bytes = rte_be_to_cpu_64(pool->raw->data[offset].bytes);
3758 rte_spinlock_unlock(&pool->sl);
3763 * Create and initialize a new counter pool.
3766 * Pointer to the Ethernet device structure.
3768 * The devX counter handle.
3770 * Whether the pool is for counter that was allocated by batch command.
3773 * A new pool pointer on success, NULL otherwise and rte_errno is set.
3775 static struct mlx5_flow_counter_pool *
3776 flow_dv_pool_create(struct rte_eth_dev *dev, struct mlx5_devx_obj *dcs,
3779 struct mlx5_priv *priv = dev->data->dev_private;
3780 struct mlx5_flow_counter_pool *pool;
3781 struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(priv->sh, batch,
3783 int16_t n_valid = rte_atomic16_read(&cont->n_valid);
3786 if (cont->n == n_valid) {
3787 cont = flow_dv_container_resize(dev, batch);
3791 size = sizeof(*pool) + MLX5_COUNTERS_PER_POOL *
3792 sizeof(struct mlx5_flow_counter);
3793 pool = rte_calloc(__func__, 1, size, 0);
3798 pool->min_dcs = dcs;
3799 pool->raw = cont->init_mem_mng->raws + n_valid %
3800 MLX5_CNT_CONTAINER_RESIZE;
3801 pool->raw_hw = NULL;
3802 rte_spinlock_init(&pool->sl);
3804 * The generation of the new allocated counters in this pool is 0, 2 in
3805 * the pool generation makes all the counters valid for allocation.
3807 rte_atomic64_set(&pool->query_gen, 0x2);
3808 TAILQ_INIT(&pool->counters);
3809 TAILQ_INSERT_TAIL(&cont->pool_list, pool, next);
3810 cont->pools[n_valid] = pool;
3811 /* Pool initialization must be updated before host thread access. */
3813 rte_atomic16_add(&cont->n_valid, 1);
3818 * Prepare a new counter and/or a new counter pool.
3821 * Pointer to the Ethernet device structure.
3822 * @param[out] cnt_free
3823 * Where to put the pointer of a new counter.
3825 * Whether the pool is for counter that was allocated by batch command.
3828 * The free counter pool pointer and @p cnt_free is set on success,
3829 * NULL otherwise and rte_errno is set.
3831 static struct mlx5_flow_counter_pool *
3832 flow_dv_counter_pool_prepare(struct rte_eth_dev *dev,
3833 struct mlx5_flow_counter **cnt_free,
3836 struct mlx5_priv *priv = dev->data->dev_private;
3837 struct mlx5_flow_counter_pool *pool;
3838 struct mlx5_devx_obj *dcs = NULL;
3839 struct mlx5_flow_counter *cnt;
3843 /* bulk_bitmap must be 0 for single counter allocation. */
3844 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0);
3847 pool = flow_dv_find_pool_by_id
3848 (MLX5_CNT_CONTAINER(priv->sh, batch, 0), dcs->id);
3850 pool = flow_dv_pool_create(dev, dcs, batch);
3852 mlx5_devx_cmd_destroy(dcs);
3855 } else if (dcs->id < pool->min_dcs->id) {
3856 rte_atomic64_set(&pool->a64_dcs,
3857 (int64_t)(uintptr_t)dcs);
3859 cnt = &pool->counters_raw[dcs->id % MLX5_COUNTERS_PER_POOL];
3860 TAILQ_INSERT_HEAD(&pool->counters, cnt, next);
3865 /* bulk_bitmap is in 128 counters units. */
3866 if (priv->config.hca_attr.flow_counter_bulk_alloc_bitmap & 0x4)
3867 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0x4);
3869 rte_errno = ENODATA;
3872 pool = flow_dv_pool_create(dev, dcs, batch);
3874 mlx5_devx_cmd_destroy(dcs);
3877 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
3878 cnt = &pool->counters_raw[i];
3880 TAILQ_INSERT_HEAD(&pool->counters, cnt, next);
3882 *cnt_free = &pool->counters_raw[0];
3887 * Search for existed shared counter.
3890 * Pointer to the relevant counter pool container.
3892 * The shared counter ID to search.
3895 * NULL if not existed, otherwise pointer to the shared counter.
3897 static struct mlx5_flow_counter *
3898 flow_dv_counter_shared_search(struct mlx5_pools_container *cont,
3901 static struct mlx5_flow_counter *cnt;
3902 struct mlx5_flow_counter_pool *pool;
3905 TAILQ_FOREACH(pool, &cont->pool_list, next) {
3906 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
3907 cnt = &pool->counters_raw[i];
3908 if (cnt->ref_cnt && cnt->shared && cnt->id == id)
3916 * Allocate a flow counter.
3919 * Pointer to the Ethernet device structure.
3921 * Indicate if this counter is shared with other flows.
3923 * Counter identifier.
3925 * Counter flow group.
3928 * pointer to flow counter on success, NULL otherwise and rte_errno is set.
3930 static struct mlx5_flow_counter *
3931 flow_dv_counter_alloc(struct rte_eth_dev *dev, uint32_t shared, uint32_t id,
3934 struct mlx5_priv *priv = dev->data->dev_private;
3935 struct mlx5_flow_counter_pool *pool = NULL;
3936 struct mlx5_flow_counter *cnt_free = NULL;
3938 * Currently group 0 flow counter cannot be assigned to a flow if it is
3939 * not the first one in the batch counter allocation, so it is better
3940 * to allocate counters one by one for these flows in a separate
3942 * A counter can be shared between different groups so need to take
3943 * shared counters from the single container.
3945 uint32_t batch = (group && !shared) ? 1 : 0;
3946 struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(priv->sh, batch,
3949 if (priv->counter_fallback)
3950 return flow_dv_counter_alloc_fallback(dev, shared, id);
3951 if (!priv->config.devx) {
3952 rte_errno = ENOTSUP;
3956 cnt_free = flow_dv_counter_shared_search(cont, id);
3958 if (cnt_free->ref_cnt + 1 == 0) {
3962 cnt_free->ref_cnt++;
3966 /* Pools which has a free counters are in the start. */
3967 TAILQ_FOREACH(pool, &cont->pool_list, next) {
3969 * The free counter reset values must be updated between the
3970 * counter release to the counter allocation, so, at least one
3971 * query must be done in this time. ensure it by saving the
3972 * query generation in the release time.
3973 * The free list is sorted according to the generation - so if
3974 * the first one is not updated, all the others are not
3977 cnt_free = TAILQ_FIRST(&pool->counters);
3978 if (cnt_free && cnt_free->query_gen + 1 <
3979 rte_atomic64_read(&pool->query_gen))
3984 pool = flow_dv_counter_pool_prepare(dev, &cnt_free, batch);
3988 cnt_free->batch = batch;
3989 /* Create a DV counter action only in the first time usage. */
3990 if (!cnt_free->action) {
3992 struct mlx5_devx_obj *dcs;
3995 offset = cnt_free - &pool->counters_raw[0];
3996 dcs = pool->min_dcs;
3999 dcs = cnt_free->dcs;
4001 cnt_free->action = mlx5_glue->dv_create_flow_action_counter
4003 if (!cnt_free->action) {
4008 /* Update the counter reset values. */
4009 if (_flow_dv_query_count(dev, cnt_free, &cnt_free->hits,
4012 cnt_free->shared = shared;
4013 cnt_free->ref_cnt = 1;
4015 if (!priv->sh->cmng.query_thread_on)
4016 /* Start the asynchronous batch query by the host thread. */
4017 mlx5_set_query_alarm(priv->sh);
4018 TAILQ_REMOVE(&pool->counters, cnt_free, next);
4019 if (TAILQ_EMPTY(&pool->counters)) {
4020 /* Move the pool to the end of the container pool list. */
4021 TAILQ_REMOVE(&cont->pool_list, pool, next);
4022 TAILQ_INSERT_TAIL(&cont->pool_list, pool, next);
4028 * Release a flow counter.
4031 * Pointer to the Ethernet device structure.
4032 * @param[in] counter
4033 * Pointer to the counter handler.
4036 flow_dv_counter_release(struct rte_eth_dev *dev,
4037 struct mlx5_flow_counter *counter)
4039 struct mlx5_priv *priv = dev->data->dev_private;
4043 if (priv->counter_fallback) {
4044 flow_dv_counter_release_fallback(dev, counter);
4047 if (--counter->ref_cnt == 0) {
4048 struct mlx5_flow_counter_pool *pool =
4049 flow_dv_counter_pool_get(counter);
4051 /* Put the counter in the end - the last updated one. */
4052 TAILQ_INSERT_TAIL(&pool->counters, counter, next);
4053 counter->query_gen = rte_atomic64_read(&pool->query_gen);
4058 * Verify the @p attributes will be correctly understood by the NIC and store
4059 * them in the @p flow if everything is correct.
4062 * Pointer to dev struct.
4063 * @param[in] attributes
4064 * Pointer to flow attributes
4065 * @param[in] external
4066 * This flow rule is created by request external to PMD.
4068 * Pointer to error structure.
4071 * 0 on success, a negative errno value otherwise and rte_errno is set.
4074 flow_dv_validate_attributes(struct rte_eth_dev *dev,
4075 const struct rte_flow_attr *attributes,
4076 bool external __rte_unused,
4077 struct rte_flow_error *error)
4079 struct mlx5_priv *priv = dev->data->dev_private;
4080 uint32_t priority_max = priv->config.flow_prio - 1;
4082 #ifndef HAVE_MLX5DV_DR
4083 if (attributes->group)
4084 return rte_flow_error_set(error, ENOTSUP,
4085 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
4087 "groups are not supported");
4089 uint32_t max_group = attributes->transfer ?
4090 MLX5_MAX_TABLES_FDB :
4092 MLX5_MAX_TABLES_EXTERNAL :
4097 ret = mlx5_flow_group_to_table(attributes, external,
4102 if (table >= max_group)
4103 return rte_flow_error_set(error, EINVAL,
4104 RTE_FLOW_ERROR_TYPE_ATTR_GROUP, NULL,
4105 "group index out of range");
4107 if (attributes->priority != MLX5_FLOW_PRIO_RSVD &&
4108 attributes->priority >= priority_max)
4109 return rte_flow_error_set(error, ENOTSUP,
4110 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
4112 "priority out of range");
4113 if (attributes->transfer) {
4114 if (!priv->config.dv_esw_en)
4115 return rte_flow_error_set
4117 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4118 "E-Switch dr is not supported");
4119 if (!(priv->representor || priv->master))
4120 return rte_flow_error_set
4121 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4122 NULL, "E-Switch configuration can only be"
4123 " done by a master or a representor device");
4124 if (attributes->egress)
4125 return rte_flow_error_set
4127 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, attributes,
4128 "egress is not supported");
4130 if (!(attributes->egress ^ attributes->ingress))
4131 return rte_flow_error_set(error, ENOTSUP,
4132 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
4133 "must specify exactly one of "
4134 "ingress or egress");
4139 * Internal validation function. For validating both actions and items.
4142 * Pointer to the rte_eth_dev structure.
4144 * Pointer to the flow attributes.
4146 * Pointer to the list of items.
4147 * @param[in] actions
4148 * Pointer to the list of actions.
4149 * @param[in] external
4150 * This flow rule is created by request external to PMD.
4152 * Pointer to the error structure.
4155 * 0 on success, a negative errno value otherwise and rte_errno is set.
4158 flow_dv_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
4159 const struct rte_flow_item items[],
4160 const struct rte_flow_action actions[],
4161 bool external, struct rte_flow_error *error)
4164 uint64_t action_flags = 0;
4165 uint64_t item_flags = 0;
4166 uint64_t last_item = 0;
4167 uint8_t next_protocol = 0xff;
4168 uint16_t ether_type = 0;
4170 const struct rte_flow_item *gre_item = NULL;
4171 struct rte_flow_item_tcp nic_tcp_mask = {
4174 .src_port = RTE_BE16(UINT16_MAX),
4175 .dst_port = RTE_BE16(UINT16_MAX),
4178 struct mlx5_priv *priv = dev->data->dev_private;
4179 struct mlx5_dev_config *dev_conf = &priv->config;
4183 ret = flow_dv_validate_attributes(dev, attr, external, error);
4186 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
4187 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
4188 int type = items->type;
4191 case RTE_FLOW_ITEM_TYPE_VOID:
4193 case RTE_FLOW_ITEM_TYPE_PORT_ID:
4194 ret = flow_dv_validate_item_port_id
4195 (dev, items, attr, item_flags, error);
4198 last_item = MLX5_FLOW_ITEM_PORT_ID;
4200 case RTE_FLOW_ITEM_TYPE_ETH:
4201 ret = mlx5_flow_validate_item_eth(items, item_flags,
4205 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
4206 MLX5_FLOW_LAYER_OUTER_L2;
4207 if (items->mask != NULL && items->spec != NULL) {
4209 ((const struct rte_flow_item_eth *)
4212 ((const struct rte_flow_item_eth *)
4214 ether_type = rte_be_to_cpu_16(ether_type);
4219 case RTE_FLOW_ITEM_TYPE_VLAN:
4220 ret = mlx5_flow_validate_item_vlan(items, item_flags,
4224 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
4225 MLX5_FLOW_LAYER_OUTER_VLAN;
4226 if (items->mask != NULL && items->spec != NULL) {
4228 ((const struct rte_flow_item_vlan *)
4229 items->spec)->inner_type;
4231 ((const struct rte_flow_item_vlan *)
4232 items->mask)->inner_type;
4233 ether_type = rte_be_to_cpu_16(ether_type);
4238 case RTE_FLOW_ITEM_TYPE_IPV4:
4239 mlx5_flow_tunnel_ip_check(items, next_protocol,
4240 &item_flags, &tunnel);
4241 ret = mlx5_flow_validate_item_ipv4(items, item_flags,
4247 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
4248 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
4249 if (items->mask != NULL &&
4250 ((const struct rte_flow_item_ipv4 *)
4251 items->mask)->hdr.next_proto_id) {
4253 ((const struct rte_flow_item_ipv4 *)
4254 (items->spec))->hdr.next_proto_id;
4256 ((const struct rte_flow_item_ipv4 *)
4257 (items->mask))->hdr.next_proto_id;
4259 /* Reset for inner layer. */
4260 next_protocol = 0xff;
4263 case RTE_FLOW_ITEM_TYPE_IPV6:
4264 mlx5_flow_tunnel_ip_check(items, next_protocol,
4265 &item_flags, &tunnel);
4266 ret = mlx5_flow_validate_item_ipv6(items, item_flags,
4272 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
4273 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
4274 if (items->mask != NULL &&
4275 ((const struct rte_flow_item_ipv6 *)
4276 items->mask)->hdr.proto) {
4278 ((const struct rte_flow_item_ipv6 *)
4279 items->spec)->hdr.proto;
4281 ((const struct rte_flow_item_ipv6 *)
4282 items->mask)->hdr.proto;
4284 /* Reset for inner layer. */
4285 next_protocol = 0xff;
4288 case RTE_FLOW_ITEM_TYPE_TCP:
4289 ret = mlx5_flow_validate_item_tcp
4296 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
4297 MLX5_FLOW_LAYER_OUTER_L4_TCP;
4299 case RTE_FLOW_ITEM_TYPE_UDP:
4300 ret = mlx5_flow_validate_item_udp(items, item_flags,
4305 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
4306 MLX5_FLOW_LAYER_OUTER_L4_UDP;
4308 case RTE_FLOW_ITEM_TYPE_GRE:
4309 ret = mlx5_flow_validate_item_gre(items, item_flags,
4310 next_protocol, error);
4314 last_item = MLX5_FLOW_LAYER_GRE;
4316 case RTE_FLOW_ITEM_TYPE_NVGRE:
4317 ret = mlx5_flow_validate_item_nvgre(items, item_flags,
4322 last_item = MLX5_FLOW_LAYER_NVGRE;
4324 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
4325 ret = mlx5_flow_validate_item_gre_key
4326 (items, item_flags, gre_item, error);
4329 last_item = MLX5_FLOW_LAYER_GRE_KEY;
4331 case RTE_FLOW_ITEM_TYPE_VXLAN:
4332 ret = mlx5_flow_validate_item_vxlan(items, item_flags,
4336 last_item = MLX5_FLOW_LAYER_VXLAN;
4338 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
4339 ret = mlx5_flow_validate_item_vxlan_gpe(items,
4344 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
4346 case RTE_FLOW_ITEM_TYPE_GENEVE:
4347 ret = mlx5_flow_validate_item_geneve(items,
4352 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
4354 case RTE_FLOW_ITEM_TYPE_MPLS:
4355 ret = mlx5_flow_validate_item_mpls(dev, items,
4360 last_item = MLX5_FLOW_LAYER_MPLS;
4363 case RTE_FLOW_ITEM_TYPE_MARK:
4364 ret = flow_dv_validate_item_mark(dev, items, attr,
4368 last_item = MLX5_FLOW_ITEM_MARK;
4370 case RTE_FLOW_ITEM_TYPE_META:
4371 ret = flow_dv_validate_item_meta(dev, items, attr,
4375 last_item = MLX5_FLOW_ITEM_METADATA;
4377 case RTE_FLOW_ITEM_TYPE_ICMP:
4378 ret = mlx5_flow_validate_item_icmp(items, item_flags,
4383 last_item = MLX5_FLOW_LAYER_ICMP;
4385 case RTE_FLOW_ITEM_TYPE_ICMP6:
4386 ret = mlx5_flow_validate_item_icmp6(items, item_flags,
4391 last_item = MLX5_FLOW_LAYER_ICMP6;
4393 case RTE_FLOW_ITEM_TYPE_TAG:
4394 ret = flow_dv_validate_item_tag(dev, items,
4398 last_item = MLX5_FLOW_ITEM_TAG;
4400 case MLX5_RTE_FLOW_ITEM_TYPE_TAG:
4401 case MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE:
4404 return rte_flow_error_set(error, ENOTSUP,
4405 RTE_FLOW_ERROR_TYPE_ITEM,
4406 NULL, "item not supported");
4408 item_flags |= last_item;
4410 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
4411 int type = actions->type;
4412 if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
4413 return rte_flow_error_set(error, ENOTSUP,
4414 RTE_FLOW_ERROR_TYPE_ACTION,
4415 actions, "too many actions");
4417 case RTE_FLOW_ACTION_TYPE_VOID:
4419 case RTE_FLOW_ACTION_TYPE_PORT_ID:
4420 ret = flow_dv_validate_action_port_id(dev,
4427 action_flags |= MLX5_FLOW_ACTION_PORT_ID;
4430 case RTE_FLOW_ACTION_TYPE_FLAG:
4431 ret = flow_dv_validate_action_flag(dev, action_flags,
4435 if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
4436 /* Count all modify-header actions as one. */
4437 if (!(action_flags &
4438 MLX5_FLOW_MODIFY_HDR_ACTIONS))
4440 action_flags |= MLX5_FLOW_ACTION_FLAG |
4441 MLX5_FLOW_ACTION_MARK_EXT;
4443 action_flags |= MLX5_FLOW_ACTION_FLAG;
4447 case RTE_FLOW_ACTION_TYPE_MARK:
4448 ret = flow_dv_validate_action_mark(dev, actions,
4453 if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
4454 /* Count all modify-header actions as one. */
4455 if (!(action_flags &
4456 MLX5_FLOW_MODIFY_HDR_ACTIONS))
4458 action_flags |= MLX5_FLOW_ACTION_MARK |
4459 MLX5_FLOW_ACTION_MARK_EXT;
4461 action_flags |= MLX5_FLOW_ACTION_MARK;
4465 case RTE_FLOW_ACTION_TYPE_SET_META:
4466 ret = flow_dv_validate_action_set_meta(dev, actions,
4471 /* Count all modify-header actions as one action. */
4472 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4474 action_flags |= MLX5_FLOW_ACTION_SET_META;
4476 case RTE_FLOW_ACTION_TYPE_SET_TAG:
4477 ret = flow_dv_validate_action_set_tag(dev, actions,
4482 /* Count all modify-header actions as one action. */
4483 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4485 action_flags |= MLX5_FLOW_ACTION_SET_TAG;
4487 case RTE_FLOW_ACTION_TYPE_DROP:
4488 ret = mlx5_flow_validate_action_drop(action_flags,
4492 action_flags |= MLX5_FLOW_ACTION_DROP;
4495 case RTE_FLOW_ACTION_TYPE_QUEUE:
4496 ret = mlx5_flow_validate_action_queue(actions,
4501 action_flags |= MLX5_FLOW_ACTION_QUEUE;
4504 case RTE_FLOW_ACTION_TYPE_RSS:
4505 ret = mlx5_flow_validate_action_rss(actions,
4511 action_flags |= MLX5_FLOW_ACTION_RSS;
4514 case RTE_FLOW_ACTION_TYPE_COUNT:
4515 ret = flow_dv_validate_action_count(dev, error);
4518 action_flags |= MLX5_FLOW_ACTION_COUNT;
4521 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
4522 if (flow_dv_validate_action_pop_vlan(dev,
4528 action_flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
4531 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
4532 ret = flow_dv_validate_action_push_vlan(action_flags,
4538 action_flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
4541 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
4542 ret = flow_dv_validate_action_set_vlan_pcp
4543 (action_flags, actions, error);
4546 /* Count PCP with push_vlan command. */
4547 action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_PCP;
4549 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
4550 ret = flow_dv_validate_action_set_vlan_vid
4551 (item_flags, action_flags,
4555 /* Count VID with push_vlan command. */
4556 action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
4558 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
4559 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
4560 ret = flow_dv_validate_action_l2_encap(action_flags,
4565 action_flags |= actions->type ==
4566 RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP ?
4567 MLX5_FLOW_ACTION_VXLAN_ENCAP :
4568 MLX5_FLOW_ACTION_NVGRE_ENCAP;
4571 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
4572 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
4573 ret = flow_dv_validate_action_l2_decap(action_flags,
4577 action_flags |= actions->type ==
4578 RTE_FLOW_ACTION_TYPE_VXLAN_DECAP ?
4579 MLX5_FLOW_ACTION_VXLAN_DECAP :
4580 MLX5_FLOW_ACTION_NVGRE_DECAP;
4583 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
4584 ret = flow_dv_validate_action_raw_encap(action_flags,
4589 action_flags |= MLX5_FLOW_ACTION_RAW_ENCAP;
4592 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
4593 ret = flow_dv_validate_action_raw_decap(action_flags,
4598 action_flags |= MLX5_FLOW_ACTION_RAW_DECAP;
4601 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
4602 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
4603 ret = flow_dv_validate_action_modify_mac(action_flags,
4609 /* Count all modify-header actions as one action. */
4610 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4612 action_flags |= actions->type ==
4613 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
4614 MLX5_FLOW_ACTION_SET_MAC_SRC :
4615 MLX5_FLOW_ACTION_SET_MAC_DST;
4618 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
4619 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
4620 ret = flow_dv_validate_action_modify_ipv4(action_flags,
4626 /* Count all modify-header actions as one action. */
4627 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4629 action_flags |= actions->type ==
4630 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
4631 MLX5_FLOW_ACTION_SET_IPV4_SRC :
4632 MLX5_FLOW_ACTION_SET_IPV4_DST;
4634 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
4635 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
4636 ret = flow_dv_validate_action_modify_ipv6(action_flags,
4642 /* Count all modify-header actions as one action. */
4643 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4645 action_flags |= actions->type ==
4646 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
4647 MLX5_FLOW_ACTION_SET_IPV6_SRC :
4648 MLX5_FLOW_ACTION_SET_IPV6_DST;
4650 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
4651 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
4652 ret = flow_dv_validate_action_modify_tp(action_flags,
4658 /* Count all modify-header actions as one action. */
4659 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4661 action_flags |= actions->type ==
4662 RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
4663 MLX5_FLOW_ACTION_SET_TP_SRC :
4664 MLX5_FLOW_ACTION_SET_TP_DST;
4666 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
4667 case RTE_FLOW_ACTION_TYPE_SET_TTL:
4668 ret = flow_dv_validate_action_modify_ttl(action_flags,
4674 /* Count all modify-header actions as one action. */
4675 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4677 action_flags |= actions->type ==
4678 RTE_FLOW_ACTION_TYPE_SET_TTL ?
4679 MLX5_FLOW_ACTION_SET_TTL :
4680 MLX5_FLOW_ACTION_DEC_TTL;
4682 case RTE_FLOW_ACTION_TYPE_JUMP:
4683 ret = flow_dv_validate_action_jump(actions,
4690 action_flags |= MLX5_FLOW_ACTION_JUMP;
4692 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
4693 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
4694 ret = flow_dv_validate_action_modify_tcp_seq
4701 /* Count all modify-header actions as one action. */
4702 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4704 action_flags |= actions->type ==
4705 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
4706 MLX5_FLOW_ACTION_INC_TCP_SEQ :
4707 MLX5_FLOW_ACTION_DEC_TCP_SEQ;
4709 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
4710 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
4711 ret = flow_dv_validate_action_modify_tcp_ack
4718 /* Count all modify-header actions as one action. */
4719 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4721 action_flags |= actions->type ==
4722 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
4723 MLX5_FLOW_ACTION_INC_TCP_ACK :
4724 MLX5_FLOW_ACTION_DEC_TCP_ACK;
4726 case MLX5_RTE_FLOW_ACTION_TYPE_TAG:
4727 case MLX5_RTE_FLOW_ACTION_TYPE_MARK:
4728 case MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG:
4731 return rte_flow_error_set(error, ENOTSUP,
4732 RTE_FLOW_ERROR_TYPE_ACTION,
4734 "action not supported");
4737 if ((action_flags & MLX5_FLOW_LAYER_TUNNEL) &&
4738 (action_flags & MLX5_FLOW_VLAN_ACTIONS))
4739 return rte_flow_error_set(error, ENOTSUP,
4740 RTE_FLOW_ERROR_TYPE_ACTION,
4742 "can't have vxlan and vlan"
4743 " actions in the same rule");
4744 /* Eswitch has few restrictions on using items and actions */
4745 if (attr->transfer) {
4746 if (!mlx5_flow_ext_mreg_supported(dev) &&
4747 action_flags & MLX5_FLOW_ACTION_FLAG)
4748 return rte_flow_error_set(error, ENOTSUP,
4749 RTE_FLOW_ERROR_TYPE_ACTION,
4751 "unsupported action FLAG");
4752 if (!mlx5_flow_ext_mreg_supported(dev) &&
4753 action_flags & MLX5_FLOW_ACTION_MARK)
4754 return rte_flow_error_set(error, ENOTSUP,
4755 RTE_FLOW_ERROR_TYPE_ACTION,
4757 "unsupported action MARK");
4758 if (action_flags & MLX5_FLOW_ACTION_QUEUE)
4759 return rte_flow_error_set(error, ENOTSUP,
4760 RTE_FLOW_ERROR_TYPE_ACTION,
4762 "unsupported action QUEUE");
4763 if (action_flags & MLX5_FLOW_ACTION_RSS)
4764 return rte_flow_error_set(error, ENOTSUP,
4765 RTE_FLOW_ERROR_TYPE_ACTION,
4767 "unsupported action RSS");
4768 if (!(action_flags & MLX5_FLOW_FATE_ESWITCH_ACTIONS))
4769 return rte_flow_error_set(error, EINVAL,
4770 RTE_FLOW_ERROR_TYPE_ACTION,
4772 "no fate action is found");
4774 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS) && attr->ingress)
4775 return rte_flow_error_set(error, EINVAL,
4776 RTE_FLOW_ERROR_TYPE_ACTION,
4778 "no fate action is found");
4784 * Internal preparation function. Allocates the DV flow size,
4785 * this size is constant.
4788 * Pointer to the flow attributes.
4790 * Pointer to the list of items.
4791 * @param[in] actions
4792 * Pointer to the list of actions.
4794 * Pointer to the error structure.
4797 * Pointer to mlx5_flow object on success,
4798 * otherwise NULL and rte_errno is set.
4800 static struct mlx5_flow *
4801 flow_dv_prepare(const struct rte_flow_attr *attr __rte_unused,
4802 const struct rte_flow_item items[] __rte_unused,
4803 const struct rte_flow_action actions[] __rte_unused,
4804 struct rte_flow_error *error)
4806 size_t size = sizeof(struct mlx5_flow);
4807 struct mlx5_flow *dev_flow;
4809 dev_flow = rte_calloc(__func__, 1, size, 0);
4811 rte_flow_error_set(error, ENOMEM,
4812 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4813 "not enough memory to create flow");
4816 dev_flow->dv.value.size = MLX5_ST_SZ_BYTES(fte_match_param);
4817 dev_flow->ingress = attr->ingress;
4818 dev_flow->transfer = attr->transfer;
4824 * Sanity check for match mask and value. Similar to check_valid_spec() in
4825 * kernel driver. If unmasked bit is present in value, it returns failure.
4828 * pointer to match mask buffer.
4829 * @param match_value
4830 * pointer to match value buffer.
4833 * 0 if valid, -EINVAL otherwise.
4836 flow_dv_check_valid_spec(void *match_mask, void *match_value)
4838 uint8_t *m = match_mask;
4839 uint8_t *v = match_value;
4842 for (i = 0; i < MLX5_ST_SZ_BYTES(fte_match_param); ++i) {
4845 "match_value differs from match_criteria"
4846 " %p[%u] != %p[%u]",
4847 match_value, i, match_mask, i);
4856 * Add Ethernet item to matcher and to the value.
4858 * @param[in, out] matcher
4860 * @param[in, out] key
4861 * Flow matcher value.
4863 * Flow pattern to translate.
4865 * Item is inner pattern.
4868 flow_dv_translate_item_eth(void *matcher, void *key,
4869 const struct rte_flow_item *item, int inner)
4871 const struct rte_flow_item_eth *eth_m = item->mask;
4872 const struct rte_flow_item_eth *eth_v = item->spec;
4873 const struct rte_flow_item_eth nic_mask = {
4874 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
4875 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
4876 .type = RTE_BE16(0xffff),
4888 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
4890 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
4892 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
4894 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
4896 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, dmac_47_16),
4897 ð_m->dst, sizeof(eth_m->dst));
4898 /* The value must be in the range of the mask. */
4899 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, dmac_47_16);
4900 for (i = 0; i < sizeof(eth_m->dst); ++i)
4901 l24_v[i] = eth_m->dst.addr_bytes[i] & eth_v->dst.addr_bytes[i];
4902 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, smac_47_16),
4903 ð_m->src, sizeof(eth_m->src));
4904 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, smac_47_16);
4905 /* The value must be in the range of the mask. */
4906 for (i = 0; i < sizeof(eth_m->dst); ++i)
4907 l24_v[i] = eth_m->src.addr_bytes[i] & eth_v->src.addr_bytes[i];
4908 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype,
4909 rte_be_to_cpu_16(eth_m->type));
4910 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, ethertype);
4911 *(uint16_t *)(l24_v) = eth_m->type & eth_v->type;
4915 * Add VLAN item to matcher and to the value.
4917 * @param[in, out] dev_flow
4919 * @param[in, out] matcher
4921 * @param[in, out] key
4922 * Flow matcher value.
4924 * Flow pattern to translate.
4926 * Item is inner pattern.
4929 flow_dv_translate_item_vlan(struct mlx5_flow *dev_flow,
4930 void *matcher, void *key,
4931 const struct rte_flow_item *item,
4934 const struct rte_flow_item_vlan *vlan_m = item->mask;
4935 const struct rte_flow_item_vlan *vlan_v = item->spec;
4944 vlan_m = &rte_flow_item_vlan_mask;
4946 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
4948 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
4950 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
4952 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
4954 * This is workaround, masks are not supported,
4955 * and pre-validated.
4957 dev_flow->dv.vf_vlan.tag =
4958 rte_be_to_cpu_16(vlan_v->tci) & 0x0fff;
4960 tci_m = rte_be_to_cpu_16(vlan_m->tci);
4961 tci_v = rte_be_to_cpu_16(vlan_m->tci & vlan_v->tci);
4962 MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
4963 MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag, 1);
4964 MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_vid, tci_m);
4965 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, tci_v);
4966 MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_cfi, tci_m >> 12);
4967 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_cfi, tci_v >> 12);
4968 MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_prio, tci_m >> 13);
4969 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, tci_v >> 13);
4970 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype,
4971 rte_be_to_cpu_16(vlan_m->inner_type));
4972 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
4973 rte_be_to_cpu_16(vlan_m->inner_type & vlan_v->inner_type));
4977 * Add IPV4 item to matcher and to the value.
4979 * @param[in, out] matcher
4981 * @param[in, out] key
4982 * Flow matcher value.
4984 * Flow pattern to translate.
4986 * Item is inner pattern.
4988 * The group to insert the rule.
4991 flow_dv_translate_item_ipv4(void *matcher, void *key,
4992 const struct rte_flow_item *item,
4993 int inner, uint32_t group)
4995 const struct rte_flow_item_ipv4 *ipv4_m = item->mask;
4996 const struct rte_flow_item_ipv4 *ipv4_v = item->spec;
4997 const struct rte_flow_item_ipv4 nic_mask = {
4999 .src_addr = RTE_BE32(0xffffffff),
5000 .dst_addr = RTE_BE32(0xffffffff),
5001 .type_of_service = 0xff,
5002 .next_proto_id = 0xff,
5012 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5014 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5016 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5018 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5021 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
5023 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0x4);
5024 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, 4);
5029 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
5030 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
5031 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
5032 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
5033 *(uint32_t *)l24_m = ipv4_m->hdr.dst_addr;
5034 *(uint32_t *)l24_v = ipv4_m->hdr.dst_addr & ipv4_v->hdr.dst_addr;
5035 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
5036 src_ipv4_src_ipv6.ipv4_layout.ipv4);
5037 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
5038 src_ipv4_src_ipv6.ipv4_layout.ipv4);
5039 *(uint32_t *)l24_m = ipv4_m->hdr.src_addr;
5040 *(uint32_t *)l24_v = ipv4_m->hdr.src_addr & ipv4_v->hdr.src_addr;
5041 tos = ipv4_m->hdr.type_of_service & ipv4_v->hdr.type_of_service;
5042 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn,
5043 ipv4_m->hdr.type_of_service);
5044 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, tos);
5045 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp,
5046 ipv4_m->hdr.type_of_service >> 2);
5047 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, tos >> 2);
5048 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
5049 ipv4_m->hdr.next_proto_id);
5050 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
5051 ipv4_v->hdr.next_proto_id & ipv4_m->hdr.next_proto_id);
5055 * Add IPV6 item to matcher and to the value.
5057 * @param[in, out] matcher
5059 * @param[in, out] key
5060 * Flow matcher value.
5062 * Flow pattern to translate.
5064 * Item is inner pattern.
5066 * The group to insert the rule.
5069 flow_dv_translate_item_ipv6(void *matcher, void *key,
5070 const struct rte_flow_item *item,
5071 int inner, uint32_t group)
5073 const struct rte_flow_item_ipv6 *ipv6_m = item->mask;
5074 const struct rte_flow_item_ipv6 *ipv6_v = item->spec;
5075 const struct rte_flow_item_ipv6 nic_mask = {
5078 "\xff\xff\xff\xff\xff\xff\xff\xff"
5079 "\xff\xff\xff\xff\xff\xff\xff\xff",
5081 "\xff\xff\xff\xff\xff\xff\xff\xff"
5082 "\xff\xff\xff\xff\xff\xff\xff\xff",
5083 .vtc_flow = RTE_BE32(0xffffffff),
5090 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5091 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5100 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5102 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5104 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5106 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5109 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
5111 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0x6);
5112 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, 6);
5117 size = sizeof(ipv6_m->hdr.dst_addr);
5118 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
5119 dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
5120 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
5121 dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
5122 memcpy(l24_m, ipv6_m->hdr.dst_addr, size);
5123 for (i = 0; i < size; ++i)
5124 l24_v[i] = l24_m[i] & ipv6_v->hdr.dst_addr[i];
5125 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
5126 src_ipv4_src_ipv6.ipv6_layout.ipv6);
5127 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
5128 src_ipv4_src_ipv6.ipv6_layout.ipv6);
5129 memcpy(l24_m, ipv6_m->hdr.src_addr, size);
5130 for (i = 0; i < size; ++i)
5131 l24_v[i] = l24_m[i] & ipv6_v->hdr.src_addr[i];
5133 vtc_m = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow);
5134 vtc_v = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow & ipv6_v->hdr.vtc_flow);
5135 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn, vtc_m >> 20);
5136 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, vtc_v >> 20);
5137 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp, vtc_m >> 22);
5138 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, vtc_v >> 22);
5141 MLX5_SET(fte_match_set_misc, misc_m, inner_ipv6_flow_label,
5143 MLX5_SET(fte_match_set_misc, misc_v, inner_ipv6_flow_label,
5146 MLX5_SET(fte_match_set_misc, misc_m, outer_ipv6_flow_label,
5148 MLX5_SET(fte_match_set_misc, misc_v, outer_ipv6_flow_label,
5152 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
5154 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
5155 ipv6_v->hdr.proto & ipv6_m->hdr.proto);
5159 * Add TCP item to matcher and to the value.
5161 * @param[in, out] matcher
5163 * @param[in, out] key
5164 * Flow matcher value.
5166 * Flow pattern to translate.
5168 * Item is inner pattern.
5171 flow_dv_translate_item_tcp(void *matcher, void *key,
5172 const struct rte_flow_item *item,
5175 const struct rte_flow_item_tcp *tcp_m = item->mask;
5176 const struct rte_flow_item_tcp *tcp_v = item->spec;
5181 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5183 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5185 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5187 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5189 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
5190 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_TCP);
5194 tcp_m = &rte_flow_item_tcp_mask;
5195 MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_sport,
5196 rte_be_to_cpu_16(tcp_m->hdr.src_port));
5197 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
5198 rte_be_to_cpu_16(tcp_v->hdr.src_port & tcp_m->hdr.src_port));
5199 MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_dport,
5200 rte_be_to_cpu_16(tcp_m->hdr.dst_port));
5201 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
5202 rte_be_to_cpu_16(tcp_v->hdr.dst_port & tcp_m->hdr.dst_port));
5203 MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_flags,
5204 tcp_m->hdr.tcp_flags);
5205 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
5206 (tcp_v->hdr.tcp_flags & tcp_m->hdr.tcp_flags));
5210 * Add UDP item to matcher and to the value.
5212 * @param[in, out] matcher
5214 * @param[in, out] key
5215 * Flow matcher value.
5217 * Flow pattern to translate.
5219 * Item is inner pattern.
5222 flow_dv_translate_item_udp(void *matcher, void *key,
5223 const struct rte_flow_item *item,
5226 const struct rte_flow_item_udp *udp_m = item->mask;
5227 const struct rte_flow_item_udp *udp_v = item->spec;
5232 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5234 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5236 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5238 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5240 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
5241 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
5245 udp_m = &rte_flow_item_udp_mask;
5246 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_sport,
5247 rte_be_to_cpu_16(udp_m->hdr.src_port));
5248 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
5249 rte_be_to_cpu_16(udp_v->hdr.src_port & udp_m->hdr.src_port));
5250 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport,
5251 rte_be_to_cpu_16(udp_m->hdr.dst_port));
5252 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
5253 rte_be_to_cpu_16(udp_v->hdr.dst_port & udp_m->hdr.dst_port));
5257 * Add GRE optional Key item to matcher and to the value.
5259 * @param[in, out] matcher
5261 * @param[in, out] key
5262 * Flow matcher value.
5264 * Flow pattern to translate.
5266 * Item is inner pattern.
5269 flow_dv_translate_item_gre_key(void *matcher, void *key,
5270 const struct rte_flow_item *item)
5272 const rte_be32_t *key_m = item->mask;
5273 const rte_be32_t *key_v = item->spec;
5274 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5275 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5276 rte_be32_t gre_key_default_mask = RTE_BE32(UINT32_MAX);
5281 key_m = &gre_key_default_mask;
5282 /* GRE K bit must be on and should already be validated */
5283 MLX5_SET(fte_match_set_misc, misc_m, gre_k_present, 1);
5284 MLX5_SET(fte_match_set_misc, misc_v, gre_k_present, 1);
5285 MLX5_SET(fte_match_set_misc, misc_m, gre_key_h,
5286 rte_be_to_cpu_32(*key_m) >> 8);
5287 MLX5_SET(fte_match_set_misc, misc_v, gre_key_h,
5288 rte_be_to_cpu_32((*key_v) & (*key_m)) >> 8);
5289 MLX5_SET(fte_match_set_misc, misc_m, gre_key_l,
5290 rte_be_to_cpu_32(*key_m) & 0xFF);
5291 MLX5_SET(fte_match_set_misc, misc_v, gre_key_l,
5292 rte_be_to_cpu_32((*key_v) & (*key_m)) & 0xFF);
5296 * Add GRE item to matcher and to the value.
5298 * @param[in, out] matcher
5300 * @param[in, out] key
5301 * Flow matcher value.
5303 * Flow pattern to translate.
5305 * Item is inner pattern.
5308 flow_dv_translate_item_gre(void *matcher, void *key,
5309 const struct rte_flow_item *item,
5312 const struct rte_flow_item_gre *gre_m = item->mask;
5313 const struct rte_flow_item_gre *gre_v = item->spec;
5316 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5317 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5324 uint16_t s_present:1;
5325 uint16_t k_present:1;
5326 uint16_t rsvd_bit1:1;
5327 uint16_t c_present:1;
5331 } gre_crks_rsvd0_ver_m, gre_crks_rsvd0_ver_v;
5334 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5336 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5338 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5340 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5342 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
5343 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_GRE);
5347 gre_m = &rte_flow_item_gre_mask;
5348 MLX5_SET(fte_match_set_misc, misc_m, gre_protocol,
5349 rte_be_to_cpu_16(gre_m->protocol));
5350 MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
5351 rte_be_to_cpu_16(gre_v->protocol & gre_m->protocol));
5352 gre_crks_rsvd0_ver_m.value = rte_be_to_cpu_16(gre_m->c_rsvd0_ver);
5353 gre_crks_rsvd0_ver_v.value = rte_be_to_cpu_16(gre_v->c_rsvd0_ver);
5354 MLX5_SET(fte_match_set_misc, misc_m, gre_c_present,
5355 gre_crks_rsvd0_ver_m.c_present);
5356 MLX5_SET(fte_match_set_misc, misc_v, gre_c_present,
5357 gre_crks_rsvd0_ver_v.c_present &
5358 gre_crks_rsvd0_ver_m.c_present);
5359 MLX5_SET(fte_match_set_misc, misc_m, gre_k_present,
5360 gre_crks_rsvd0_ver_m.k_present);
5361 MLX5_SET(fte_match_set_misc, misc_v, gre_k_present,
5362 gre_crks_rsvd0_ver_v.k_present &
5363 gre_crks_rsvd0_ver_m.k_present);
5364 MLX5_SET(fte_match_set_misc, misc_m, gre_s_present,
5365 gre_crks_rsvd0_ver_m.s_present);
5366 MLX5_SET(fte_match_set_misc, misc_v, gre_s_present,
5367 gre_crks_rsvd0_ver_v.s_present &
5368 gre_crks_rsvd0_ver_m.s_present);
5372 * Add NVGRE item to matcher and to the value.
5374 * @param[in, out] matcher
5376 * @param[in, out] key
5377 * Flow matcher value.
5379 * Flow pattern to translate.
5381 * Item is inner pattern.
5384 flow_dv_translate_item_nvgre(void *matcher, void *key,
5385 const struct rte_flow_item *item,
5388 const struct rte_flow_item_nvgre *nvgre_m = item->mask;
5389 const struct rte_flow_item_nvgre *nvgre_v = item->spec;
5390 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5391 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5392 const char *tni_flow_id_m = (const char *)nvgre_m->tni;
5393 const char *tni_flow_id_v = (const char *)nvgre_v->tni;
5399 /* For NVGRE, GRE header fields must be set with defined values. */
5400 const struct rte_flow_item_gre gre_spec = {
5401 .c_rsvd0_ver = RTE_BE16(0x2000),
5402 .protocol = RTE_BE16(RTE_ETHER_TYPE_TEB)
5404 const struct rte_flow_item_gre gre_mask = {
5405 .c_rsvd0_ver = RTE_BE16(0xB000),
5406 .protocol = RTE_BE16(UINT16_MAX),
5408 const struct rte_flow_item gre_item = {
5413 flow_dv_translate_item_gre(matcher, key, &gre_item, inner);
5417 nvgre_m = &rte_flow_item_nvgre_mask;
5418 size = sizeof(nvgre_m->tni) + sizeof(nvgre_m->flow_id);
5419 gre_key_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, gre_key_h);
5420 gre_key_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, gre_key_h);
5421 memcpy(gre_key_m, tni_flow_id_m, size);
5422 for (i = 0; i < size; ++i)
5423 gre_key_v[i] = gre_key_m[i] & tni_flow_id_v[i];
5427 * Add VXLAN item to matcher and to the value.
5429 * @param[in, out] matcher
5431 * @param[in, out] key
5432 * Flow matcher value.
5434 * Flow pattern to translate.
5436 * Item is inner pattern.
5439 flow_dv_translate_item_vxlan(void *matcher, void *key,
5440 const struct rte_flow_item *item,
5443 const struct rte_flow_item_vxlan *vxlan_m = item->mask;
5444 const struct rte_flow_item_vxlan *vxlan_v = item->spec;
5447 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5448 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5456 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5458 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5460 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5462 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5464 dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
5465 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
5466 if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
5467 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
5468 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
5473 vxlan_m = &rte_flow_item_vxlan_mask;
5474 size = sizeof(vxlan_m->vni);
5475 vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, vxlan_vni);
5476 vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, vxlan_vni);
5477 memcpy(vni_m, vxlan_m->vni, size);
5478 for (i = 0; i < size; ++i)
5479 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
5483 * Add Geneve item to matcher and to the value.
5485 * @param[in, out] matcher
5487 * @param[in, out] key
5488 * Flow matcher value.
5490 * Flow pattern to translate.
5492 * Item is inner pattern.
5496 flow_dv_translate_item_geneve(void *matcher, void *key,
5497 const struct rte_flow_item *item, int inner)
5499 const struct rte_flow_item_geneve *geneve_m = item->mask;
5500 const struct rte_flow_item_geneve *geneve_v = item->spec;
5503 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5504 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5513 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5515 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5517 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5519 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5521 dport = MLX5_UDP_PORT_GENEVE;
5522 if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
5523 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
5524 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
5529 geneve_m = &rte_flow_item_geneve_mask;
5530 size = sizeof(geneve_m->vni);
5531 vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, geneve_vni);
5532 vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, geneve_vni);
5533 memcpy(vni_m, geneve_m->vni, size);
5534 for (i = 0; i < size; ++i)
5535 vni_v[i] = vni_m[i] & geneve_v->vni[i];
5536 MLX5_SET(fte_match_set_misc, misc_m, geneve_protocol_type,
5537 rte_be_to_cpu_16(geneve_m->protocol));
5538 MLX5_SET(fte_match_set_misc, misc_v, geneve_protocol_type,
5539 rte_be_to_cpu_16(geneve_v->protocol & geneve_m->protocol));
5540 gbhdr_m = rte_be_to_cpu_16(geneve_m->ver_opt_len_o_c_rsvd0);
5541 gbhdr_v = rte_be_to_cpu_16(geneve_v->ver_opt_len_o_c_rsvd0);
5542 MLX5_SET(fte_match_set_misc, misc_m, geneve_oam,
5543 MLX5_GENEVE_OAMF_VAL(gbhdr_m));
5544 MLX5_SET(fte_match_set_misc, misc_v, geneve_oam,
5545 MLX5_GENEVE_OAMF_VAL(gbhdr_v) & MLX5_GENEVE_OAMF_VAL(gbhdr_m));
5546 MLX5_SET(fte_match_set_misc, misc_m, geneve_opt_len,
5547 MLX5_GENEVE_OPTLEN_VAL(gbhdr_m));
5548 MLX5_SET(fte_match_set_misc, misc_v, geneve_opt_len,
5549 MLX5_GENEVE_OPTLEN_VAL(gbhdr_v) &
5550 MLX5_GENEVE_OPTLEN_VAL(gbhdr_m));
5554 * Add MPLS item to matcher and to the value.
5556 * @param[in, out] matcher
5558 * @param[in, out] key
5559 * Flow matcher value.
5561 * Flow pattern to translate.
5562 * @param[in] prev_layer
5563 * The protocol layer indicated in previous item.
5565 * Item is inner pattern.
5568 flow_dv_translate_item_mpls(void *matcher, void *key,
5569 const struct rte_flow_item *item,
5570 uint64_t prev_layer,
5573 const uint32_t *in_mpls_m = item->mask;
5574 const uint32_t *in_mpls_v = item->spec;
5575 uint32_t *out_mpls_m = 0;
5576 uint32_t *out_mpls_v = 0;
5577 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5578 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5579 void *misc2_m = MLX5_ADDR_OF(fte_match_param, matcher,
5581 void *misc2_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
5582 void *headers_m = MLX5_ADDR_OF(fte_match_param, matcher, outer_headers);
5583 void *headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5585 switch (prev_layer) {
5586 case MLX5_FLOW_LAYER_OUTER_L4_UDP:
5587 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xffff);
5588 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
5589 MLX5_UDP_PORT_MPLS);
5591 case MLX5_FLOW_LAYER_GRE:
5592 MLX5_SET(fte_match_set_misc, misc_m, gre_protocol, 0xffff);
5593 MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
5594 RTE_ETHER_TYPE_MPLS);
5597 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
5598 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
5605 in_mpls_m = (const uint32_t *)&rte_flow_item_mpls_mask;
5606 switch (prev_layer) {
5607 case MLX5_FLOW_LAYER_OUTER_L4_UDP:
5609 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
5610 outer_first_mpls_over_udp);
5612 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
5613 outer_first_mpls_over_udp);
5615 case MLX5_FLOW_LAYER_GRE:
5617 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
5618 outer_first_mpls_over_gre);
5620 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
5621 outer_first_mpls_over_gre);
5624 /* Inner MPLS not over GRE is not supported. */
5627 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
5631 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
5637 if (out_mpls_m && out_mpls_v) {
5638 *out_mpls_m = *in_mpls_m;
5639 *out_mpls_v = *in_mpls_v & *in_mpls_m;
5644 * Add metadata register item to matcher
5646 * @param[in, out] matcher
5648 * @param[in, out] key
5649 * Flow matcher value.
5650 * @param[in] reg_type
5651 * Type of device metadata register
5658 flow_dv_match_meta_reg(void *matcher, void *key,
5659 enum modify_reg reg_type,
5660 uint32_t data, uint32_t mask)
5663 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_2);
5665 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
5670 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_a, mask);
5671 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_a, data);
5674 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_b, mask);
5675 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_b, data);
5678 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_0, mask);
5679 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_0, data);
5682 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_1, mask);
5683 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_1, data);
5686 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_2, mask);
5687 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_2, data);
5690 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_3, mask);
5691 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_3, data);
5694 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_4, mask);
5695 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_4, data);
5698 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_5, mask);
5699 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_5, data);
5702 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_6, mask);
5703 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_6, data);
5706 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_7, mask);
5707 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_7, data);
5716 * Add MARK item to matcher
5719 * The device to configure through.
5720 * @param[in, out] matcher
5722 * @param[in, out] key
5723 * Flow matcher value.
5725 * Flow pattern to translate.
5728 flow_dv_translate_item_mark(struct rte_eth_dev *dev,
5729 void *matcher, void *key,
5730 const struct rte_flow_item *item)
5732 struct mlx5_priv *priv = dev->data->dev_private;
5733 const struct rte_flow_item_mark *mark;
5737 mark = item->mask ? (const void *)item->mask :
5738 &rte_flow_item_mark_mask;
5739 mask = mark->id & priv->sh->dv_mark_mask;
5740 mark = (const void *)item->spec;
5742 value = mark->id & priv->sh->dv_mark_mask & mask;
5744 enum modify_reg reg;
5746 /* Get the metadata register index for the mark. */
5747 reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, NULL);
5749 flow_dv_match_meta_reg(matcher, key, reg, value, mask);
5754 * Add META item to matcher
5757 * The devich to configure through.
5758 * @param[in, out] matcher
5760 * @param[in, out] key
5761 * Flow matcher value.
5763 * Attributes of flow that includes this item.
5765 * Flow pattern to translate.
5768 flow_dv_translate_item_meta(struct rte_eth_dev *dev,
5769 void *matcher, void *key,
5770 const struct rte_flow_attr *attr,
5771 const struct rte_flow_item *item)
5773 const struct rte_flow_item_meta *meta_m;
5774 const struct rte_flow_item_meta *meta_v;
5776 meta_m = (const void *)item->mask;
5778 meta_m = &rte_flow_item_meta_mask;
5779 meta_v = (const void *)item->spec;
5781 enum modify_reg reg;
5782 uint32_t value = meta_v->data;
5783 uint32_t mask = meta_m->data;
5785 reg = flow_dv_get_metadata_reg(dev, attr, NULL);
5789 * In datapath code there is no endianness
5790 * coversions for perfromance reasons, all
5791 * pattern conversions are done in rte_flow.
5793 value = rte_cpu_to_be_32(value);
5794 mask = rte_cpu_to_be_32(mask);
5795 if (reg == REG_C_0) {
5796 struct mlx5_priv *priv = dev->data->dev_private;
5797 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
5798 uint32_t shl_c0 = rte_bsf32(msk_c0);
5800 msk_c0 = rte_cpu_to_be_32(msk_c0);
5804 assert(!(~msk_c0 & mask));
5806 flow_dv_match_meta_reg(matcher, key, reg, value, mask);
5811 * Add vport metadata Reg C0 item to matcher
5813 * @param[in, out] matcher
5815 * @param[in, out] key
5816 * Flow matcher value.
5818 * Flow pattern to translate.
5821 flow_dv_translate_item_meta_vport(void *matcher, void *key,
5822 uint32_t value, uint32_t mask)
5824 flow_dv_match_meta_reg(matcher, key, REG_C_0, value, mask);
5828 * Add tag item to matcher
5830 * @param[in, out] matcher
5832 * @param[in, out] key
5833 * Flow matcher value.
5835 * Flow pattern to translate.
5838 flow_dv_translate_mlx5_item_tag(void *matcher, void *key,
5839 const struct rte_flow_item *item)
5841 const struct mlx5_rte_flow_item_tag *tag_v = item->spec;
5842 const struct mlx5_rte_flow_item_tag *tag_m = item->mask;
5845 flow_dv_match_meta_reg(matcher, key, tag_v->id, tag_v->data,
5846 tag_m ? tag_m->data : UINT32_MAX);
5850 * Add TAG item to matcher
5853 * The devich to configure through.
5854 * @param[in, out] matcher
5856 * @param[in, out] key
5857 * Flow matcher value.
5859 * Flow pattern to translate.
5862 flow_dv_translate_item_tag(struct rte_eth_dev *dev,
5863 void *matcher, void *key,
5864 const struct rte_flow_item *item)
5866 const struct rte_flow_item_tag *tag_v = item->spec;
5867 const struct rte_flow_item_tag *tag_m = item->mask;
5868 enum modify_reg reg;
5871 tag_m = tag_m ? tag_m : &rte_flow_item_tag_mask;
5872 /* Get the metadata register index for the tag. */
5873 reg = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, tag_v->index, NULL);
5875 flow_dv_match_meta_reg(matcher, key, reg, tag_v->data, tag_m->data);
5879 * Add source vport match to the specified matcher.
5881 * @param[in, out] matcher
5883 * @param[in, out] key
5884 * Flow matcher value.
5886 * Source vport value to match
5891 flow_dv_translate_item_source_vport(void *matcher, void *key,
5892 int16_t port, uint16_t mask)
5894 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5895 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5897 MLX5_SET(fte_match_set_misc, misc_m, source_port, mask);
5898 MLX5_SET(fte_match_set_misc, misc_v, source_port, port);
5902 * Translate port-id item to eswitch match on port-id.
5905 * The devich to configure through.
5906 * @param[in, out] matcher
5908 * @param[in, out] key
5909 * Flow matcher value.
5911 * Flow pattern to translate.
5914 * 0 on success, a negative errno value otherwise.
5917 flow_dv_translate_item_port_id(struct rte_eth_dev *dev, void *matcher,
5918 void *key, const struct rte_flow_item *item)
5920 const struct rte_flow_item_port_id *pid_m = item ? item->mask : NULL;
5921 const struct rte_flow_item_port_id *pid_v = item ? item->spec : NULL;
5922 struct mlx5_priv *priv;
5925 mask = pid_m ? pid_m->id : 0xffff;
5926 id = pid_v ? pid_v->id : dev->data->port_id;
5927 priv = mlx5_port_to_eswitch_info(id, item == NULL);
5930 /* Translate to vport field or to metadata, depending on mode. */
5931 if (priv->vport_meta_mask)
5932 flow_dv_translate_item_meta_vport(matcher, key,
5933 priv->vport_meta_tag,
5934 priv->vport_meta_mask);
5936 flow_dv_translate_item_source_vport(matcher, key,
5937 priv->vport_id, mask);
5942 * Add ICMP6 item to matcher and to the value.
5944 * @param[in, out] matcher
5946 * @param[in, out] key
5947 * Flow matcher value.
5949 * Flow pattern to translate.
5951 * Item is inner pattern.
5954 flow_dv_translate_item_icmp6(void *matcher, void *key,
5955 const struct rte_flow_item *item,
5958 const struct rte_flow_item_icmp6 *icmp6_m = item->mask;
5959 const struct rte_flow_item_icmp6 *icmp6_v = item->spec;
5962 void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
5964 void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
5966 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5968 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5970 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5972 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5974 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
5975 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMPV6);
5979 icmp6_m = &rte_flow_item_icmp6_mask;
5980 MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_type, icmp6_m->type);
5981 MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_type,
5982 icmp6_v->type & icmp6_m->type);
5983 MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_code, icmp6_m->code);
5984 MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_code,
5985 icmp6_v->code & icmp6_m->code);
5989 * Add ICMP item to matcher and to the value.
5991 * @param[in, out] matcher
5993 * @param[in, out] key
5994 * Flow matcher value.
5996 * Flow pattern to translate.
5998 * Item is inner pattern.
6001 flow_dv_translate_item_icmp(void *matcher, void *key,
6002 const struct rte_flow_item *item,
6005 const struct rte_flow_item_icmp *icmp_m = item->mask;
6006 const struct rte_flow_item_icmp *icmp_v = item->spec;
6009 void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
6011 void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
6013 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6015 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6017 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6019 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6021 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
6022 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMP);
6026 icmp_m = &rte_flow_item_icmp_mask;
6027 MLX5_SET(fte_match_set_misc3, misc3_m, icmp_type,
6028 icmp_m->hdr.icmp_type);
6029 MLX5_SET(fte_match_set_misc3, misc3_v, icmp_type,
6030 icmp_v->hdr.icmp_type & icmp_m->hdr.icmp_type);
6031 MLX5_SET(fte_match_set_misc3, misc3_m, icmp_code,
6032 icmp_m->hdr.icmp_code);
6033 MLX5_SET(fte_match_set_misc3, misc3_v, icmp_code,
6034 icmp_v->hdr.icmp_code & icmp_m->hdr.icmp_code);
6037 static uint32_t matcher_zero[MLX5_ST_SZ_DW(fte_match_param)] = { 0 };
6039 #define HEADER_IS_ZERO(match_criteria, headers) \
6040 !(memcmp(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
6041 matcher_zero, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
6044 * Calculate flow matcher enable bitmap.
6046 * @param match_criteria
6047 * Pointer to flow matcher criteria.
6050 * Bitmap of enabled fields.
6053 flow_dv_matcher_enable(uint32_t *match_criteria)
6055 uint8_t match_criteria_enable;
6057 match_criteria_enable =
6058 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
6059 MLX5_MATCH_CRITERIA_ENABLE_OUTER_BIT;
6060 match_criteria_enable |=
6061 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
6062 MLX5_MATCH_CRITERIA_ENABLE_MISC_BIT;
6063 match_criteria_enable |=
6064 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
6065 MLX5_MATCH_CRITERIA_ENABLE_INNER_BIT;
6066 match_criteria_enable |=
6067 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
6068 MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
6069 match_criteria_enable |=
6070 (!HEADER_IS_ZERO(match_criteria, misc_parameters_3)) <<
6071 MLX5_MATCH_CRITERIA_ENABLE_MISC3_BIT;
6072 return match_criteria_enable;
6079 * @param dev[in, out]
6080 * Pointer to rte_eth_dev structure.
6081 * @param[in] table_id
6084 * Direction of the table.
6085 * @param[in] transfer
6086 * E-Switch or NIC flow.
6088 * pointer to error structure.
6091 * Returns tables resource based on the index, NULL in case of failed.
6093 static struct mlx5_flow_tbl_resource *
6094 flow_dv_tbl_resource_get(struct rte_eth_dev *dev,
6095 uint32_t table_id, uint8_t egress,
6097 struct rte_flow_error *error)
6099 struct mlx5_priv *priv = dev->data->dev_private;
6100 struct mlx5_ibv_shared *sh = priv->sh;
6101 struct mlx5_flow_tbl_resource *tbl;
6103 #ifdef HAVE_MLX5DV_DR
6105 tbl = &sh->fdb_tbl[table_id];
6107 tbl->obj = mlx5_glue->dr_create_flow_tbl
6108 (sh->fdb_domain, table_id);
6109 } else if (egress) {
6110 tbl = &sh->tx_tbl[table_id];
6112 tbl->obj = mlx5_glue->dr_create_flow_tbl
6113 (sh->tx_domain, table_id);
6115 tbl = &sh->rx_tbl[table_id];
6117 tbl->obj = mlx5_glue->dr_create_flow_tbl
6118 (sh->rx_domain, table_id);
6121 rte_flow_error_set(error, ENOMEM,
6122 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6123 NULL, "cannot create table");
6126 rte_atomic32_inc(&tbl->refcnt);
6132 return &sh->fdb_tbl[table_id];
6134 return &sh->tx_tbl[table_id];
6136 return &sh->rx_tbl[table_id];
6141 * Release a flow table.
6144 * Table resource to be released.
6147 * Returns 0 if table was released, else return 1;
6150 flow_dv_tbl_resource_release(struct mlx5_flow_tbl_resource *tbl)
6154 if (rte_atomic32_dec_and_test(&tbl->refcnt)) {
6155 mlx5_glue->dr_destroy_flow_tbl(tbl->obj);
6163 * Register the flow matcher.
6165 * @param dev[in, out]
6166 * Pointer to rte_eth_dev structure.
6167 * @param[in, out] matcher
6168 * Pointer to flow matcher.
6169 * @parm[in, out] dev_flow
6170 * Pointer to the dev_flow.
6172 * pointer to error structure.
6175 * 0 on success otherwise -errno and errno is set.
6178 flow_dv_matcher_register(struct rte_eth_dev *dev,
6179 struct mlx5_flow_dv_matcher *matcher,
6180 struct mlx5_flow *dev_flow,
6181 struct rte_flow_error *error)
6183 struct mlx5_priv *priv = dev->data->dev_private;
6184 struct mlx5_ibv_shared *sh = priv->sh;
6185 struct mlx5_flow_dv_matcher *cache_matcher;
6186 struct mlx5dv_flow_matcher_attr dv_attr = {
6187 .type = IBV_FLOW_ATTR_NORMAL,
6188 .match_mask = (void *)&matcher->mask,
6190 struct mlx5_flow_tbl_resource *tbl = NULL;
6192 /* Lookup from cache. */
6193 LIST_FOREACH(cache_matcher, &sh->matchers, next) {
6194 if (matcher->crc == cache_matcher->crc &&
6195 matcher->priority == cache_matcher->priority &&
6196 matcher->egress == cache_matcher->egress &&
6197 matcher->group == cache_matcher->group &&
6198 matcher->transfer == cache_matcher->transfer &&
6199 !memcmp((const void *)matcher->mask.buf,
6200 (const void *)cache_matcher->mask.buf,
6201 cache_matcher->mask.size)) {
6203 "priority %hd use %s matcher %p: refcnt %d++",
6204 cache_matcher->priority,
6205 cache_matcher->egress ? "tx" : "rx",
6206 (void *)cache_matcher,
6207 rte_atomic32_read(&cache_matcher->refcnt));
6208 rte_atomic32_inc(&cache_matcher->refcnt);
6209 dev_flow->dv.matcher = cache_matcher;
6213 /* Register new matcher. */
6214 cache_matcher = rte_calloc(__func__, 1, sizeof(*cache_matcher), 0);
6216 return rte_flow_error_set(error, ENOMEM,
6217 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
6218 "cannot allocate matcher memory");
6219 tbl = flow_dv_tbl_resource_get(dev, matcher->group,
6220 matcher->egress, matcher->transfer,
6223 rte_free(cache_matcher);
6224 return rte_flow_error_set(error, ENOMEM,
6225 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6226 NULL, "cannot create table");
6228 *cache_matcher = *matcher;
6229 dv_attr.match_criteria_enable =
6230 flow_dv_matcher_enable(cache_matcher->mask.buf);
6231 dv_attr.priority = matcher->priority;
6232 if (matcher->egress)
6233 dv_attr.flags |= IBV_FLOW_ATTR_FLAGS_EGRESS;
6234 cache_matcher->matcher_object =
6235 mlx5_glue->dv_create_flow_matcher(sh->ctx, &dv_attr, tbl->obj);
6236 if (!cache_matcher->matcher_object) {
6237 rte_free(cache_matcher);
6238 #ifdef HAVE_MLX5DV_DR
6239 flow_dv_tbl_resource_release(tbl);
6241 return rte_flow_error_set(error, ENOMEM,
6242 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6243 NULL, "cannot create matcher");
6245 rte_atomic32_inc(&cache_matcher->refcnt);
6246 LIST_INSERT_HEAD(&sh->matchers, cache_matcher, next);
6247 dev_flow->dv.matcher = cache_matcher;
6248 DRV_LOG(DEBUG, "priority %hd new %s matcher %p: refcnt %d",
6249 cache_matcher->priority,
6250 cache_matcher->egress ? "tx" : "rx", (void *)cache_matcher,
6251 rte_atomic32_read(&cache_matcher->refcnt));
6252 rte_atomic32_inc(&tbl->refcnt);
6257 * Find existing tag resource or create and register a new one.
6259 * @param dev[in, out]
6260 * Pointer to rte_eth_dev structure.
6261 * @param[in, out] resource
6262 * Pointer to tag resource.
6263 * @parm[in, out] dev_flow
6264 * Pointer to the dev_flow.
6266 * pointer to error structure.
6269 * 0 on success otherwise -errno and errno is set.
6272 flow_dv_tag_resource_register
6273 (struct rte_eth_dev *dev,
6274 struct mlx5_flow_dv_tag_resource *resource,
6275 struct mlx5_flow *dev_flow,
6276 struct rte_flow_error *error)
6278 struct mlx5_priv *priv = dev->data->dev_private;
6279 struct mlx5_ibv_shared *sh = priv->sh;
6280 struct mlx5_flow_dv_tag_resource *cache_resource;
6282 /* Lookup a matching resource from cache. */
6283 LIST_FOREACH(cache_resource, &sh->tags, next) {
6284 if (resource->tag == cache_resource->tag) {
6285 DRV_LOG(DEBUG, "tag resource %p: refcnt %d++",
6286 (void *)cache_resource,
6287 rte_atomic32_read(&cache_resource->refcnt));
6288 rte_atomic32_inc(&cache_resource->refcnt);
6289 dev_flow->dv.tag_resource = cache_resource;
6293 /* Register new resource. */
6294 cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
6295 if (!cache_resource)
6296 return rte_flow_error_set(error, ENOMEM,
6297 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
6298 "cannot allocate resource memory");
6299 *cache_resource = *resource;
6300 cache_resource->action = mlx5_glue->dv_create_flow_action_tag
6302 if (!cache_resource->action) {
6303 rte_free(cache_resource);
6304 return rte_flow_error_set(error, ENOMEM,
6305 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6306 NULL, "cannot create action");
6308 rte_atomic32_init(&cache_resource->refcnt);
6309 rte_atomic32_inc(&cache_resource->refcnt);
6310 LIST_INSERT_HEAD(&sh->tags, cache_resource, next);
6311 dev_flow->dv.tag_resource = cache_resource;
6312 DRV_LOG(DEBUG, "new tag resource %p: refcnt %d++",
6313 (void *)cache_resource,
6314 rte_atomic32_read(&cache_resource->refcnt));
6322 * Pointer to Ethernet device.
6324 * Pointer to mlx5_flow.
6327 * 1 while a reference on it exists, 0 when freed.
6330 flow_dv_tag_release(struct rte_eth_dev *dev,
6331 struct mlx5_flow_dv_tag_resource *tag)
6334 DRV_LOG(DEBUG, "port %u tag %p: refcnt %d--",
6335 dev->data->port_id, (void *)tag,
6336 rte_atomic32_read(&tag->refcnt));
6337 if (rte_atomic32_dec_and_test(&tag->refcnt)) {
6338 claim_zero(mlx5_glue->destroy_flow_action(tag->action));
6339 LIST_REMOVE(tag, next);
6340 DRV_LOG(DEBUG, "port %u tag %p: removed",
6341 dev->data->port_id, (void *)tag);
6349 * Translate port ID action to vport.
6352 * Pointer to rte_eth_dev structure.
6354 * Pointer to the port ID action.
6355 * @param[out] dst_port_id
6356 * The target port ID.
6358 * Pointer to the error structure.
6361 * 0 on success, a negative errno value otherwise and rte_errno is set.
6364 flow_dv_translate_action_port_id(struct rte_eth_dev *dev,
6365 const struct rte_flow_action *action,
6366 uint32_t *dst_port_id,
6367 struct rte_flow_error *error)
6370 struct mlx5_priv *priv;
6371 const struct rte_flow_action_port_id *conf =
6372 (const struct rte_flow_action_port_id *)action->conf;
6374 port = conf->original ? dev->data->port_id : conf->id;
6375 priv = mlx5_port_to_eswitch_info(port, false);
6377 return rte_flow_error_set(error, -rte_errno,
6378 RTE_FLOW_ERROR_TYPE_ACTION,
6380 "No eswitch info was found for port");
6381 if (priv->vport_meta_mask)
6382 *dst_port_id = priv->vport_meta_tag;
6384 *dst_port_id = priv->vport_id;
6389 * Add Tx queue matcher
6392 * Pointer to the dev struct.
6393 * @param[in, out] matcher
6395 * @param[in, out] key
6396 * Flow matcher value.
6398 * Flow pattern to translate.
6400 * Item is inner pattern.
6403 flow_dv_translate_item_tx_queue(struct rte_eth_dev *dev,
6404 void *matcher, void *key,
6405 const struct rte_flow_item *item)
6407 const struct mlx5_rte_flow_item_tx_queue *queue_m;
6408 const struct mlx5_rte_flow_item_tx_queue *queue_v;
6410 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6412 MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6413 struct mlx5_txq_ctrl *txq;
6417 queue_m = (const void *)item->mask;
6420 queue_v = (const void *)item->spec;
6423 txq = mlx5_txq_get(dev, queue_v->queue);
6426 queue = txq->obj->sq->id;
6427 MLX5_SET(fte_match_set_misc, misc_m, source_sqn, queue_m->queue);
6428 MLX5_SET(fte_match_set_misc, misc_v, source_sqn,
6429 queue & queue_m->queue);
6430 mlx5_txq_release(dev, queue_v->queue);
6434 * Fill the flow with DV spec, lock free
6435 * (mutex should be acquired by caller).
6438 * Pointer to rte_eth_dev structure.
6439 * @param[in, out] dev_flow
6440 * Pointer to the sub flow.
6442 * Pointer to the flow attributes.
6444 * Pointer to the list of items.
6445 * @param[in] actions
6446 * Pointer to the list of actions.
6448 * Pointer to the error structure.
6451 * 0 on success, a negative errno value otherwise and rte_errno is set.
6454 __flow_dv_translate(struct rte_eth_dev *dev,
6455 struct mlx5_flow *dev_flow,
6456 const struct rte_flow_attr *attr,
6457 const struct rte_flow_item items[],
6458 const struct rte_flow_action actions[],
6459 struct rte_flow_error *error)
6461 struct mlx5_priv *priv = dev->data->dev_private;
6462 struct mlx5_dev_config *dev_conf = &priv->config;
6463 struct rte_flow *flow = dev_flow->flow;
6464 uint64_t item_flags = 0;
6465 uint64_t last_item = 0;
6466 uint64_t action_flags = 0;
6467 uint64_t priority = attr->priority;
6468 struct mlx5_flow_dv_matcher matcher = {
6470 .size = sizeof(matcher.mask.buf),
6474 bool actions_end = false;
6475 struct mlx5_flow_dv_modify_hdr_resource mhdr_res = {
6476 .ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
6477 MLX5DV_FLOW_TABLE_TYPE_NIC_RX
6479 union flow_dv_attr flow_attr = { .attr = 0 };
6480 struct mlx5_flow_dv_tag_resource tag_resource;
6481 uint32_t modify_action_position = UINT32_MAX;
6482 void *match_mask = matcher.mask.buf;
6483 void *match_value = dev_flow->dv.value.buf;
6484 uint8_t next_protocol = 0xff;
6485 struct rte_vlan_hdr vlan = { 0 };
6489 ret = mlx5_flow_group_to_table(attr, dev_flow->external, attr->group,
6493 dev_flow->group = table;
6495 mhdr_res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
6496 if (priority == MLX5_FLOW_PRIO_RSVD)
6497 priority = dev_conf->flow_prio - 1;
6498 for (; !actions_end ; actions++) {
6499 const struct rte_flow_action_queue *queue;
6500 const struct rte_flow_action_rss *rss;
6501 const struct rte_flow_action *action = actions;
6502 const struct rte_flow_action_count *count = action->conf;
6503 const uint8_t *rss_key;
6504 const struct rte_flow_action_jump *jump_data;
6505 struct mlx5_flow_dv_jump_tbl_resource jump_tbl_resource;
6506 struct mlx5_flow_tbl_resource *tbl;
6507 uint32_t port_id = 0;
6508 struct mlx5_flow_dv_port_id_action_resource port_id_resource;
6509 int action_type = actions->type;
6510 const struct rte_flow_action *found_action = NULL;
6512 switch (action_type) {
6513 case RTE_FLOW_ACTION_TYPE_VOID:
6515 case RTE_FLOW_ACTION_TYPE_PORT_ID:
6516 if (flow_dv_translate_action_port_id(dev, action,
6519 port_id_resource.port_id = port_id;
6520 if (flow_dv_port_id_action_resource_register
6521 (dev, &port_id_resource, dev_flow, error))
6523 dev_flow->dv.actions[actions_n++] =
6524 dev_flow->dv.port_id_action->action;
6525 action_flags |= MLX5_FLOW_ACTION_PORT_ID;
6527 case RTE_FLOW_ACTION_TYPE_FLAG:
6528 action_flags |= MLX5_FLOW_ACTION_FLAG;
6529 if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
6530 struct rte_flow_action_mark mark = {
6531 .id = MLX5_FLOW_MARK_DEFAULT,
6534 if (flow_dv_convert_action_mark(dev, &mark,
6538 action_flags |= MLX5_FLOW_ACTION_MARK_EXT;
6542 mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT);
6543 if (!dev_flow->dv.tag_resource)
6544 if (flow_dv_tag_resource_register
6545 (dev, &tag_resource, dev_flow, error))
6547 dev_flow->dv.actions[actions_n++] =
6548 dev_flow->dv.tag_resource->action;
6550 case RTE_FLOW_ACTION_TYPE_MARK:
6551 action_flags |= MLX5_FLOW_ACTION_MARK;
6552 if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
6553 const struct rte_flow_action_mark *mark =
6554 (const struct rte_flow_action_mark *)
6557 if (flow_dv_convert_action_mark(dev, mark,
6561 action_flags |= MLX5_FLOW_ACTION_MARK_EXT;
6565 case MLX5_RTE_FLOW_ACTION_TYPE_MARK:
6566 /* Legacy (non-extensive) MARK action. */
6567 tag_resource.tag = mlx5_flow_mark_set
6568 (((const struct rte_flow_action_mark *)
6569 (actions->conf))->id);
6570 if (!dev_flow->dv.tag_resource)
6571 if (flow_dv_tag_resource_register
6572 (dev, &tag_resource, dev_flow, error))
6574 dev_flow->dv.actions[actions_n++] =
6575 dev_flow->dv.tag_resource->action;
6577 case RTE_FLOW_ACTION_TYPE_SET_META:
6578 if (flow_dv_convert_action_set_meta
6579 (dev, &mhdr_res, attr,
6580 (const struct rte_flow_action_set_meta *)
6581 actions->conf, error))
6583 action_flags |= MLX5_FLOW_ACTION_SET_META;
6585 case RTE_FLOW_ACTION_TYPE_SET_TAG:
6586 if (flow_dv_convert_action_set_tag
6588 (const struct rte_flow_action_set_tag *)
6589 actions->conf, error))
6591 action_flags |= MLX5_FLOW_ACTION_SET_TAG;
6593 case RTE_FLOW_ACTION_TYPE_DROP:
6594 action_flags |= MLX5_FLOW_ACTION_DROP;
6596 case RTE_FLOW_ACTION_TYPE_QUEUE:
6597 assert(flow->rss.queue);
6598 queue = actions->conf;
6599 flow->rss.queue_num = 1;
6600 (*flow->rss.queue)[0] = queue->index;
6601 action_flags |= MLX5_FLOW_ACTION_QUEUE;
6603 case RTE_FLOW_ACTION_TYPE_RSS:
6604 assert(flow->rss.queue);
6605 rss = actions->conf;
6606 if (flow->rss.queue)
6607 memcpy((*flow->rss.queue), rss->queue,
6608 rss->queue_num * sizeof(uint16_t));
6609 flow->rss.queue_num = rss->queue_num;
6610 /* NULL RSS key indicates default RSS key. */
6611 rss_key = !rss->key ? rss_hash_default_key : rss->key;
6612 memcpy(flow->rss.key, rss_key, MLX5_RSS_HASH_KEY_LEN);
6614 * rss->level and rss.types should be set in advance
6615 * when expanding items for RSS.
6617 action_flags |= MLX5_FLOW_ACTION_RSS;
6619 case RTE_FLOW_ACTION_TYPE_COUNT:
6620 if (!dev_conf->devx) {
6621 rte_errno = ENOTSUP;
6624 flow->counter = flow_dv_counter_alloc(dev,
6628 if (flow->counter == NULL)
6630 dev_flow->dv.actions[actions_n++] =
6631 flow->counter->action;
6632 action_flags |= MLX5_FLOW_ACTION_COUNT;
6635 if (rte_errno == ENOTSUP)
6636 return rte_flow_error_set
6638 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6640 "count action not supported");
6642 return rte_flow_error_set
6644 RTE_FLOW_ERROR_TYPE_ACTION,
6646 "cannot create counter"
6649 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
6650 dev_flow->dv.actions[actions_n++] =
6651 priv->sh->pop_vlan_action;
6652 action_flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
6654 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
6655 flow_dev_get_vlan_info_from_items(items, &vlan);
6656 vlan.eth_proto = rte_be_to_cpu_16
6657 ((((const struct rte_flow_action_of_push_vlan *)
6658 actions->conf)->ethertype));
6659 found_action = mlx5_flow_find_action
6661 RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID);
6663 mlx5_update_vlan_vid_pcp(found_action, &vlan);
6664 found_action = mlx5_flow_find_action
6666 RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP);
6668 mlx5_update_vlan_vid_pcp(found_action, &vlan);
6669 if (flow_dv_create_action_push_vlan
6670 (dev, attr, &vlan, dev_flow, error))
6672 dev_flow->dv.actions[actions_n++] =
6673 dev_flow->dv.push_vlan_res->action;
6674 action_flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
6676 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
6677 /* of_vlan_push action handled this action */
6678 assert(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN);
6680 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
6681 if (action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN)
6683 flow_dev_get_vlan_info_from_items(items, &vlan);
6684 mlx5_update_vlan_vid_pcp(actions, &vlan);
6685 /* If no VLAN push - this is a modify header action */
6686 if (flow_dv_convert_action_modify_vlan_vid
6687 (&mhdr_res, actions, error))
6689 action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
6691 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
6692 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
6693 if (flow_dv_create_action_l2_encap(dev, actions,
6698 dev_flow->dv.actions[actions_n++] =
6699 dev_flow->dv.encap_decap->verbs_action;
6700 action_flags |= actions->type ==
6701 RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP ?
6702 MLX5_FLOW_ACTION_VXLAN_ENCAP :
6703 MLX5_FLOW_ACTION_NVGRE_ENCAP;
6705 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
6706 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
6707 if (flow_dv_create_action_l2_decap(dev, dev_flow,
6711 dev_flow->dv.actions[actions_n++] =
6712 dev_flow->dv.encap_decap->verbs_action;
6713 action_flags |= actions->type ==
6714 RTE_FLOW_ACTION_TYPE_VXLAN_DECAP ?
6715 MLX5_FLOW_ACTION_VXLAN_DECAP :
6716 MLX5_FLOW_ACTION_NVGRE_DECAP;
6718 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
6719 /* Handle encap with preceding decap. */
6720 if (action_flags & MLX5_FLOW_ACTION_RAW_DECAP) {
6721 if (flow_dv_create_action_raw_encap
6722 (dev, actions, dev_flow, attr, error))
6724 dev_flow->dv.actions[actions_n++] =
6725 dev_flow->dv.encap_decap->verbs_action;
6727 /* Handle encap without preceding decap. */
6728 if (flow_dv_create_action_l2_encap
6729 (dev, actions, dev_flow, attr->transfer,
6732 dev_flow->dv.actions[actions_n++] =
6733 dev_flow->dv.encap_decap->verbs_action;
6735 action_flags |= MLX5_FLOW_ACTION_RAW_ENCAP;
6737 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
6738 /* Check if this decap is followed by encap. */
6739 for (; action->type != RTE_FLOW_ACTION_TYPE_END &&
6740 action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP;
6743 /* Handle decap only if it isn't followed by encap. */
6744 if (action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
6745 if (flow_dv_create_action_l2_decap
6746 (dev, dev_flow, attr->transfer, error))
6748 dev_flow->dv.actions[actions_n++] =
6749 dev_flow->dv.encap_decap->verbs_action;
6751 /* If decap is followed by encap, handle it at encap. */
6752 action_flags |= MLX5_FLOW_ACTION_RAW_DECAP;
6754 case RTE_FLOW_ACTION_TYPE_JUMP:
6755 jump_data = action->conf;
6756 ret = mlx5_flow_group_to_table(attr, dev_flow->external,
6757 jump_data->group, &table,
6761 tbl = flow_dv_tbl_resource_get(dev, table,
6763 attr->transfer, error);
6765 return rte_flow_error_set
6767 RTE_FLOW_ERROR_TYPE_ACTION,
6769 "cannot create jump action.");
6770 jump_tbl_resource.tbl = tbl;
6771 if (flow_dv_jump_tbl_resource_register
6772 (dev, &jump_tbl_resource, dev_flow, error)) {
6773 flow_dv_tbl_resource_release(tbl);
6774 return rte_flow_error_set
6776 RTE_FLOW_ERROR_TYPE_ACTION,
6778 "cannot create jump action.");
6780 dev_flow->dv.actions[actions_n++] =
6781 dev_flow->dv.jump->action;
6782 action_flags |= MLX5_FLOW_ACTION_JUMP;
6784 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
6785 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
6786 if (flow_dv_convert_action_modify_mac
6787 (&mhdr_res, actions, error))
6789 action_flags |= actions->type ==
6790 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
6791 MLX5_FLOW_ACTION_SET_MAC_SRC :
6792 MLX5_FLOW_ACTION_SET_MAC_DST;
6794 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
6795 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
6796 if (flow_dv_convert_action_modify_ipv4
6797 (&mhdr_res, actions, error))
6799 action_flags |= actions->type ==
6800 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
6801 MLX5_FLOW_ACTION_SET_IPV4_SRC :
6802 MLX5_FLOW_ACTION_SET_IPV4_DST;
6804 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
6805 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
6806 if (flow_dv_convert_action_modify_ipv6
6807 (&mhdr_res, actions, error))
6809 action_flags |= actions->type ==
6810 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
6811 MLX5_FLOW_ACTION_SET_IPV6_SRC :
6812 MLX5_FLOW_ACTION_SET_IPV6_DST;
6814 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
6815 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
6816 if (flow_dv_convert_action_modify_tp
6817 (&mhdr_res, actions, items,
6820 action_flags |= actions->type ==
6821 RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
6822 MLX5_FLOW_ACTION_SET_TP_SRC :
6823 MLX5_FLOW_ACTION_SET_TP_DST;
6825 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
6826 if (flow_dv_convert_action_modify_dec_ttl
6827 (&mhdr_res, items, &flow_attr, error))
6829 action_flags |= MLX5_FLOW_ACTION_DEC_TTL;
6831 case RTE_FLOW_ACTION_TYPE_SET_TTL:
6832 if (flow_dv_convert_action_modify_ttl
6833 (&mhdr_res, actions, items,
6836 action_flags |= MLX5_FLOW_ACTION_SET_TTL;
6838 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
6839 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
6840 if (flow_dv_convert_action_modify_tcp_seq
6841 (&mhdr_res, actions, error))
6843 action_flags |= actions->type ==
6844 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
6845 MLX5_FLOW_ACTION_INC_TCP_SEQ :
6846 MLX5_FLOW_ACTION_DEC_TCP_SEQ;
6849 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
6850 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
6851 if (flow_dv_convert_action_modify_tcp_ack
6852 (&mhdr_res, actions, error))
6854 action_flags |= actions->type ==
6855 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
6856 MLX5_FLOW_ACTION_INC_TCP_ACK :
6857 MLX5_FLOW_ACTION_DEC_TCP_ACK;
6859 case MLX5_RTE_FLOW_ACTION_TYPE_TAG:
6860 if (flow_dv_convert_action_set_reg
6861 (&mhdr_res, actions, error))
6863 action_flags |= MLX5_FLOW_ACTION_SET_TAG;
6865 case MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG:
6866 if (flow_dv_convert_action_copy_mreg
6867 (dev, &mhdr_res, actions, error))
6869 action_flags |= MLX5_FLOW_ACTION_SET_TAG;
6871 case RTE_FLOW_ACTION_TYPE_END:
6873 if (mhdr_res.actions_num) {
6874 /* create modify action if needed. */
6875 if (flow_dv_modify_hdr_resource_register
6876 (dev, &mhdr_res, dev_flow, error))
6878 dev_flow->dv.actions[modify_action_position] =
6879 dev_flow->dv.modify_hdr->verbs_action;
6885 if (mhdr_res.actions_num &&
6886 modify_action_position == UINT32_MAX)
6887 modify_action_position = actions_n++;
6889 dev_flow->dv.actions_n = actions_n;
6890 dev_flow->actions = action_flags;
6891 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
6892 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
6893 int item_type = items->type;
6895 switch (item_type) {
6896 case RTE_FLOW_ITEM_TYPE_PORT_ID:
6897 flow_dv_translate_item_port_id(dev, match_mask,
6898 match_value, items);
6899 last_item = MLX5_FLOW_ITEM_PORT_ID;
6901 case RTE_FLOW_ITEM_TYPE_ETH:
6902 flow_dv_translate_item_eth(match_mask, match_value,
6904 matcher.priority = MLX5_PRIORITY_MAP_L2;
6905 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
6906 MLX5_FLOW_LAYER_OUTER_L2;
6908 case RTE_FLOW_ITEM_TYPE_VLAN:
6909 flow_dv_translate_item_vlan(dev_flow,
6910 match_mask, match_value,
6912 matcher.priority = MLX5_PRIORITY_MAP_L2;
6913 last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
6914 MLX5_FLOW_LAYER_INNER_VLAN) :
6915 (MLX5_FLOW_LAYER_OUTER_L2 |
6916 MLX5_FLOW_LAYER_OUTER_VLAN);
6918 case RTE_FLOW_ITEM_TYPE_IPV4:
6919 mlx5_flow_tunnel_ip_check(items, next_protocol,
6920 &item_flags, &tunnel);
6921 flow_dv_translate_item_ipv4(match_mask, match_value,
6924 matcher.priority = MLX5_PRIORITY_MAP_L3;
6925 dev_flow->hash_fields |=
6926 mlx5_flow_hashfields_adjust
6928 MLX5_IPV4_LAYER_TYPES,
6929 MLX5_IPV4_IBV_RX_HASH);
6930 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
6931 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
6932 if (items->mask != NULL &&
6933 ((const struct rte_flow_item_ipv4 *)
6934 items->mask)->hdr.next_proto_id) {
6936 ((const struct rte_flow_item_ipv4 *)
6937 (items->spec))->hdr.next_proto_id;
6939 ((const struct rte_flow_item_ipv4 *)
6940 (items->mask))->hdr.next_proto_id;
6942 /* Reset for inner layer. */
6943 next_protocol = 0xff;
6946 case RTE_FLOW_ITEM_TYPE_IPV6:
6947 mlx5_flow_tunnel_ip_check(items, next_protocol,
6948 &item_flags, &tunnel);
6949 flow_dv_translate_item_ipv6(match_mask, match_value,
6952 matcher.priority = MLX5_PRIORITY_MAP_L3;
6953 dev_flow->hash_fields |=
6954 mlx5_flow_hashfields_adjust
6956 MLX5_IPV6_LAYER_TYPES,
6957 MLX5_IPV6_IBV_RX_HASH);
6958 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
6959 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
6960 if (items->mask != NULL &&
6961 ((const struct rte_flow_item_ipv6 *)
6962 items->mask)->hdr.proto) {
6964 ((const struct rte_flow_item_ipv6 *)
6965 items->spec)->hdr.proto;
6967 ((const struct rte_flow_item_ipv6 *)
6968 items->mask)->hdr.proto;
6970 /* Reset for inner layer. */
6971 next_protocol = 0xff;
6974 case RTE_FLOW_ITEM_TYPE_TCP:
6975 flow_dv_translate_item_tcp(match_mask, match_value,
6977 matcher.priority = MLX5_PRIORITY_MAP_L4;
6978 dev_flow->hash_fields |=
6979 mlx5_flow_hashfields_adjust
6980 (dev_flow, tunnel, ETH_RSS_TCP,
6981 IBV_RX_HASH_SRC_PORT_TCP |
6982 IBV_RX_HASH_DST_PORT_TCP);
6983 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
6984 MLX5_FLOW_LAYER_OUTER_L4_TCP;
6986 case RTE_FLOW_ITEM_TYPE_UDP:
6987 flow_dv_translate_item_udp(match_mask, match_value,
6989 matcher.priority = MLX5_PRIORITY_MAP_L4;
6990 dev_flow->hash_fields |=
6991 mlx5_flow_hashfields_adjust
6992 (dev_flow, tunnel, ETH_RSS_UDP,
6993 IBV_RX_HASH_SRC_PORT_UDP |
6994 IBV_RX_HASH_DST_PORT_UDP);
6995 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
6996 MLX5_FLOW_LAYER_OUTER_L4_UDP;
6998 case RTE_FLOW_ITEM_TYPE_GRE:
6999 flow_dv_translate_item_gre(match_mask, match_value,
7001 last_item = MLX5_FLOW_LAYER_GRE;
7003 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
7004 flow_dv_translate_item_gre_key(match_mask,
7005 match_value, items);
7006 last_item = MLX5_FLOW_LAYER_GRE_KEY;
7008 case RTE_FLOW_ITEM_TYPE_NVGRE:
7009 flow_dv_translate_item_nvgre(match_mask, match_value,
7011 last_item = MLX5_FLOW_LAYER_GRE;
7013 case RTE_FLOW_ITEM_TYPE_VXLAN:
7014 flow_dv_translate_item_vxlan(match_mask, match_value,
7016 last_item = MLX5_FLOW_LAYER_VXLAN;
7018 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
7019 flow_dv_translate_item_vxlan(match_mask, match_value,
7021 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
7023 case RTE_FLOW_ITEM_TYPE_GENEVE:
7024 flow_dv_translate_item_geneve(match_mask, match_value,
7026 last_item = MLX5_FLOW_LAYER_GENEVE;
7028 case RTE_FLOW_ITEM_TYPE_MPLS:
7029 flow_dv_translate_item_mpls(match_mask, match_value,
7030 items, last_item, tunnel);
7031 last_item = MLX5_FLOW_LAYER_MPLS;
7033 case RTE_FLOW_ITEM_TYPE_MARK:
7034 flow_dv_translate_item_mark(dev, match_mask,
7035 match_value, items);
7036 last_item = MLX5_FLOW_ITEM_MARK;
7038 case RTE_FLOW_ITEM_TYPE_META:
7039 flow_dv_translate_item_meta(dev, match_mask,
7040 match_value, attr, items);
7041 last_item = MLX5_FLOW_ITEM_METADATA;
7043 case RTE_FLOW_ITEM_TYPE_ICMP:
7044 flow_dv_translate_item_icmp(match_mask, match_value,
7046 last_item = MLX5_FLOW_LAYER_ICMP;
7048 case RTE_FLOW_ITEM_TYPE_ICMP6:
7049 flow_dv_translate_item_icmp6(match_mask, match_value,
7051 last_item = MLX5_FLOW_LAYER_ICMP6;
7053 case RTE_FLOW_ITEM_TYPE_TAG:
7054 flow_dv_translate_item_tag(dev, match_mask,
7055 match_value, items);
7056 last_item = MLX5_FLOW_ITEM_TAG;
7058 case MLX5_RTE_FLOW_ITEM_TYPE_TAG:
7059 flow_dv_translate_mlx5_item_tag(match_mask,
7060 match_value, items);
7061 last_item = MLX5_FLOW_ITEM_TAG;
7063 case MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE:
7064 flow_dv_translate_item_tx_queue(dev, match_mask,
7067 last_item = MLX5_FLOW_ITEM_TX_QUEUE;
7072 item_flags |= last_item;
7075 * In case of ingress traffic when E-Switch mode is enabled,
7076 * we have two cases where we need to set the source port manually.
7077 * The first one, is in case of Nic steering rule, and the second is
7078 * E-Switch rule where no port_id item was found. In both cases
7079 * the source port is set according the current port in use.
7081 if ((attr->ingress && !(item_flags & MLX5_FLOW_ITEM_PORT_ID)) &&
7082 (priv->representor || priv->master)) {
7083 if (flow_dv_translate_item_port_id(dev, match_mask,
7087 assert(!flow_dv_check_valid_spec(matcher.mask.buf,
7088 dev_flow->dv.value.buf));
7089 dev_flow->layers = item_flags;
7090 /* Register matcher. */
7091 matcher.crc = rte_raw_cksum((const void *)matcher.mask.buf,
7093 matcher.priority = mlx5_flow_adjust_priority(dev, priority,
7095 matcher.egress = attr->egress;
7096 matcher.group = dev_flow->group;
7097 matcher.transfer = attr->transfer;
7098 if (flow_dv_matcher_register(dev, &matcher, dev_flow, error))
7104 * Apply the flow to the NIC, lock free,
7105 * (mutex should be acquired by caller).
7108 * Pointer to the Ethernet device structure.
7109 * @param[in, out] flow
7110 * Pointer to flow structure.
7112 * Pointer to error structure.
7115 * 0 on success, a negative errno value otherwise and rte_errno is set.
7118 __flow_dv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
7119 struct rte_flow_error *error)
7121 struct mlx5_flow_dv *dv;
7122 struct mlx5_flow *dev_flow;
7123 struct mlx5_priv *priv = dev->data->dev_private;
7127 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
7130 if (dev_flow->actions & MLX5_FLOW_ACTION_DROP) {
7131 if (dev_flow->transfer) {
7132 dv->actions[n++] = priv->sh->esw_drop_action;
7134 dv->hrxq = mlx5_hrxq_drop_new(dev);
7138 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7140 "cannot get drop hash queue");
7143 dv->actions[n++] = dv->hrxq->action;
7145 } else if (dev_flow->actions &
7146 (MLX5_FLOW_ACTION_QUEUE | MLX5_FLOW_ACTION_RSS)) {
7147 struct mlx5_hrxq *hrxq;
7149 assert(flow->rss.queue);
7150 hrxq = mlx5_hrxq_get(dev, flow->rss.key,
7151 MLX5_RSS_HASH_KEY_LEN,
7152 dev_flow->hash_fields,
7154 flow->rss.queue_num);
7156 hrxq = mlx5_hrxq_new
7157 (dev, flow->rss.key,
7158 MLX5_RSS_HASH_KEY_LEN,
7159 dev_flow->hash_fields,
7161 flow->rss.queue_num,
7162 !!(dev_flow->layers &
7163 MLX5_FLOW_LAYER_TUNNEL));
7168 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
7169 "cannot get hash queue");
7173 dv->actions[n++] = dv->hrxq->action;
7176 mlx5_glue->dv_create_flow(dv->matcher->matcher_object,
7177 (void *)&dv->value, n,
7180 rte_flow_error_set(error, errno,
7181 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7183 "hardware refuses to create flow");
7186 if (priv->vmwa_context &&
7187 dev_flow->dv.vf_vlan.tag &&
7188 !dev_flow->dv.vf_vlan.created) {
7190 * The rule contains the VLAN pattern.
7191 * For VF we are going to create VLAN
7192 * interface to make hypervisor set correct
7193 * e-Switch vport context.
7195 mlx5_vlan_vmwa_acquire(dev, &dev_flow->dv.vf_vlan);
7200 err = rte_errno; /* Save rte_errno before cleanup. */
7201 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
7202 struct mlx5_flow_dv *dv = &dev_flow->dv;
7204 if (dev_flow->actions & MLX5_FLOW_ACTION_DROP)
7205 mlx5_hrxq_drop_release(dev);
7207 mlx5_hrxq_release(dev, dv->hrxq);
7210 if (dev_flow->dv.vf_vlan.tag &&
7211 dev_flow->dv.vf_vlan.created)
7212 mlx5_vlan_vmwa_release(dev, &dev_flow->dv.vf_vlan);
7214 rte_errno = err; /* Restore rte_errno. */
7219 * Release the flow matcher.
7222 * Pointer to Ethernet device.
7224 * Pointer to mlx5_flow.
7227 * 1 while a reference on it exists, 0 when freed.
7230 flow_dv_matcher_release(struct rte_eth_dev *dev,
7231 struct mlx5_flow *flow)
7233 struct mlx5_flow_dv_matcher *matcher = flow->dv.matcher;
7234 struct mlx5_priv *priv = dev->data->dev_private;
7235 struct mlx5_ibv_shared *sh = priv->sh;
7236 struct mlx5_flow_tbl_resource *tbl;
7238 assert(matcher->matcher_object);
7239 DRV_LOG(DEBUG, "port %u matcher %p: refcnt %d--",
7240 dev->data->port_id, (void *)matcher,
7241 rte_atomic32_read(&matcher->refcnt));
7242 if (rte_atomic32_dec_and_test(&matcher->refcnt)) {
7243 claim_zero(mlx5_glue->dv_destroy_flow_matcher
7244 (matcher->matcher_object));
7245 LIST_REMOVE(matcher, next);
7246 if (matcher->egress)
7247 tbl = &sh->tx_tbl[matcher->group];
7249 tbl = &sh->rx_tbl[matcher->group];
7250 flow_dv_tbl_resource_release(tbl);
7252 DRV_LOG(DEBUG, "port %u matcher %p: removed",
7253 dev->data->port_id, (void *)matcher);
7260 * Release an encap/decap resource.
7263 * Pointer to mlx5_flow.
7266 * 1 while a reference on it exists, 0 when freed.
7269 flow_dv_encap_decap_resource_release(struct mlx5_flow *flow)
7271 struct mlx5_flow_dv_encap_decap_resource *cache_resource =
7272 flow->dv.encap_decap;
7274 assert(cache_resource->verbs_action);
7275 DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d--",
7276 (void *)cache_resource,
7277 rte_atomic32_read(&cache_resource->refcnt));
7278 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
7279 claim_zero(mlx5_glue->destroy_flow_action
7280 (cache_resource->verbs_action));
7281 LIST_REMOVE(cache_resource, next);
7282 rte_free(cache_resource);
7283 DRV_LOG(DEBUG, "encap/decap resource %p: removed",
7284 (void *)cache_resource);
7291 * Release an jump to table action resource.
7294 * Pointer to mlx5_flow.
7297 * 1 while a reference on it exists, 0 when freed.
7300 flow_dv_jump_tbl_resource_release(struct mlx5_flow *flow)
7302 struct mlx5_flow_dv_jump_tbl_resource *cache_resource =
7305 assert(cache_resource->action);
7306 DRV_LOG(DEBUG, "jump table resource %p: refcnt %d--",
7307 (void *)cache_resource,
7308 rte_atomic32_read(&cache_resource->refcnt));
7309 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
7310 claim_zero(mlx5_glue->destroy_flow_action
7311 (cache_resource->action));
7312 LIST_REMOVE(cache_resource, next);
7313 flow_dv_tbl_resource_release(cache_resource->tbl);
7314 rte_free(cache_resource);
7315 DRV_LOG(DEBUG, "jump table resource %p: removed",
7316 (void *)cache_resource);
7323 * Release a modify-header resource.
7326 * Pointer to mlx5_flow.
7329 * 1 while a reference on it exists, 0 when freed.
7332 flow_dv_modify_hdr_resource_release(struct mlx5_flow *flow)
7334 struct mlx5_flow_dv_modify_hdr_resource *cache_resource =
7335 flow->dv.modify_hdr;
7337 assert(cache_resource->verbs_action);
7338 DRV_LOG(DEBUG, "modify-header resource %p: refcnt %d--",
7339 (void *)cache_resource,
7340 rte_atomic32_read(&cache_resource->refcnt));
7341 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
7342 claim_zero(mlx5_glue->destroy_flow_action
7343 (cache_resource->verbs_action));
7344 LIST_REMOVE(cache_resource, next);
7345 rte_free(cache_resource);
7346 DRV_LOG(DEBUG, "modify-header resource %p: removed",
7347 (void *)cache_resource);
7354 * Release port ID action resource.
7357 * Pointer to mlx5_flow.
7360 * 1 while a reference on it exists, 0 when freed.
7363 flow_dv_port_id_action_resource_release(struct mlx5_flow *flow)
7365 struct mlx5_flow_dv_port_id_action_resource *cache_resource =
7366 flow->dv.port_id_action;
7368 assert(cache_resource->action);
7369 DRV_LOG(DEBUG, "port ID action resource %p: refcnt %d--",
7370 (void *)cache_resource,
7371 rte_atomic32_read(&cache_resource->refcnt));
7372 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
7373 claim_zero(mlx5_glue->destroy_flow_action
7374 (cache_resource->action));
7375 LIST_REMOVE(cache_resource, next);
7376 rte_free(cache_resource);
7377 DRV_LOG(DEBUG, "port id action resource %p: removed",
7378 (void *)cache_resource);
7385 * Release push vlan action resource.
7388 * Pointer to mlx5_flow.
7391 * 1 while a reference on it exists, 0 when freed.
7394 flow_dv_push_vlan_action_resource_release(struct mlx5_flow *flow)
7396 struct mlx5_flow_dv_push_vlan_action_resource *cache_resource =
7397 flow->dv.push_vlan_res;
7399 assert(cache_resource->action);
7400 DRV_LOG(DEBUG, "push VLAN action resource %p: refcnt %d--",
7401 (void *)cache_resource,
7402 rte_atomic32_read(&cache_resource->refcnt));
7403 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
7404 claim_zero(mlx5_glue->destroy_flow_action
7405 (cache_resource->action));
7406 LIST_REMOVE(cache_resource, next);
7407 rte_free(cache_resource);
7408 DRV_LOG(DEBUG, "push vlan action resource %p: removed",
7409 (void *)cache_resource);
7416 * Remove the flow from the NIC but keeps it in memory.
7417 * Lock free, (mutex should be acquired by caller).
7420 * Pointer to Ethernet device.
7421 * @param[in, out] flow
7422 * Pointer to flow structure.
7425 __flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
7427 struct mlx5_flow_dv *dv;
7428 struct mlx5_flow *dev_flow;
7432 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
7435 claim_zero(mlx5_glue->dv_destroy_flow(dv->flow));
7439 if (dev_flow->actions & MLX5_FLOW_ACTION_DROP)
7440 mlx5_hrxq_drop_release(dev);
7442 mlx5_hrxq_release(dev, dv->hrxq);
7445 if (dev_flow->dv.vf_vlan.tag &&
7446 dev_flow->dv.vf_vlan.created)
7447 mlx5_vlan_vmwa_release(dev, &dev_flow->dv.vf_vlan);
7452 * Remove the flow from the NIC and the memory.
7453 * Lock free, (mutex should be acquired by caller).
7456 * Pointer to the Ethernet device structure.
7457 * @param[in, out] flow
7458 * Pointer to flow structure.
7461 __flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
7463 struct mlx5_flow *dev_flow;
7467 __flow_dv_remove(dev, flow);
7468 if (flow->counter) {
7469 flow_dv_counter_release(dev, flow->counter);
7470 flow->counter = NULL;
7472 while (!LIST_EMPTY(&flow->dev_flows)) {
7473 dev_flow = LIST_FIRST(&flow->dev_flows);
7474 LIST_REMOVE(dev_flow, next);
7475 if (dev_flow->dv.matcher)
7476 flow_dv_matcher_release(dev, dev_flow);
7477 if (dev_flow->dv.encap_decap)
7478 flow_dv_encap_decap_resource_release(dev_flow);
7479 if (dev_flow->dv.modify_hdr)
7480 flow_dv_modify_hdr_resource_release(dev_flow);
7481 if (dev_flow->dv.jump)
7482 flow_dv_jump_tbl_resource_release(dev_flow);
7483 if (dev_flow->dv.port_id_action)
7484 flow_dv_port_id_action_resource_release(dev_flow);
7485 if (dev_flow->dv.push_vlan_res)
7486 flow_dv_push_vlan_action_resource_release(dev_flow);
7487 if (dev_flow->dv.tag_resource)
7488 flow_dv_tag_release(dev, dev_flow->dv.tag_resource);
7494 * Query a dv flow rule for its statistics via devx.
7497 * Pointer to Ethernet device.
7499 * Pointer to the sub flow.
7501 * data retrieved by the query.
7503 * Perform verbose error reporting if not NULL.
7506 * 0 on success, a negative errno value otherwise and rte_errno is set.
7509 flow_dv_query_count(struct rte_eth_dev *dev, struct rte_flow *flow,
7510 void *data, struct rte_flow_error *error)
7512 struct mlx5_priv *priv = dev->data->dev_private;
7513 struct rte_flow_query_count *qc = data;
7515 if (!priv->config.devx)
7516 return rte_flow_error_set(error, ENOTSUP,
7517 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7519 "counters are not supported");
7520 if (flow->counter) {
7521 uint64_t pkts, bytes;
7522 int err = _flow_dv_query_count(dev, flow->counter, &pkts,
7526 return rte_flow_error_set(error, -err,
7527 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7528 NULL, "cannot read counters");
7531 qc->hits = pkts - flow->counter->hits;
7532 qc->bytes = bytes - flow->counter->bytes;
7534 flow->counter->hits = pkts;
7535 flow->counter->bytes = bytes;
7539 return rte_flow_error_set(error, EINVAL,
7540 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7542 "counters are not available");
7548 * @see rte_flow_query()
7552 flow_dv_query(struct rte_eth_dev *dev,
7553 struct rte_flow *flow __rte_unused,
7554 const struct rte_flow_action *actions __rte_unused,
7555 void *data __rte_unused,
7556 struct rte_flow_error *error __rte_unused)
7560 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
7561 switch (actions->type) {
7562 case RTE_FLOW_ACTION_TYPE_VOID:
7564 case RTE_FLOW_ACTION_TYPE_COUNT:
7565 ret = flow_dv_query_count(dev, flow, data, error);
7568 return rte_flow_error_set(error, ENOTSUP,
7569 RTE_FLOW_ERROR_TYPE_ACTION,
7571 "action not supported");
7578 * Mutex-protected thunk to lock-free __flow_dv_translate().
7581 flow_dv_translate(struct rte_eth_dev *dev,
7582 struct mlx5_flow *dev_flow,
7583 const struct rte_flow_attr *attr,
7584 const struct rte_flow_item items[],
7585 const struct rte_flow_action actions[],
7586 struct rte_flow_error *error)
7590 flow_dv_shared_lock(dev);
7591 ret = __flow_dv_translate(dev, dev_flow, attr, items, actions, error);
7592 flow_dv_shared_unlock(dev);
7597 * Mutex-protected thunk to lock-free __flow_dv_apply().
7600 flow_dv_apply(struct rte_eth_dev *dev,
7601 struct rte_flow *flow,
7602 struct rte_flow_error *error)
7606 flow_dv_shared_lock(dev);
7607 ret = __flow_dv_apply(dev, flow, error);
7608 flow_dv_shared_unlock(dev);
7613 * Mutex-protected thunk to lock-free __flow_dv_remove().
7616 flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
7618 flow_dv_shared_lock(dev);
7619 __flow_dv_remove(dev, flow);
7620 flow_dv_shared_unlock(dev);
7624 * Mutex-protected thunk to lock-free __flow_dv_destroy().
7627 flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
7629 flow_dv_shared_lock(dev);
7630 __flow_dv_destroy(dev, flow);
7631 flow_dv_shared_unlock(dev);
7634 const struct mlx5_flow_driver_ops mlx5_flow_dv_drv_ops = {
7635 .validate = flow_dv_validate,
7636 .prepare = flow_dv_prepare,
7637 .translate = flow_dv_translate,
7638 .apply = flow_dv_apply,
7639 .remove = flow_dv_remove,
7640 .destroy = flow_dv_destroy,
7641 .query = flow_dv_query,
7644 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */