From: Hamdan Igbaria <hamd...@nvidia.com>

Support insert header action, this will allow encap at
a specific anchor and offset selected by the user.

Signed-off-by: Hamdan Igbaria <hamd...@nvidia.com>
Reviewed-by: Alex Vesker <va...@nvidia.com>
Acked-by: Matan Azrad <ma...@nvidia.com>
---
 drivers/net/mlx5/hws/mlx5dr.h          |  36 ++++++++
 drivers/net/mlx5/hws/mlx5dr_action.c   | 112 +++++++++++++++++++++----
 drivers/net/mlx5/hws/mlx5dr_action.h   |   5 +-
 drivers/net/mlx5/hws/mlx5dr_cmd.c      |   4 +-
 drivers/net/mlx5/hws/mlx5dr_debug.c    |   1 +
 drivers/net/mlx5/hws/mlx5dr_internal.h |   1 +
 6 files changed, 141 insertions(+), 18 deletions(-)

diff --git a/drivers/net/mlx5/hws/mlx5dr.h b/drivers/net/mlx5/hws/mlx5dr.h
index e7d89ad7ec..a6bbb85eed 100644
--- a/drivers/net/mlx5/hws/mlx5dr.h
+++ b/drivers/net/mlx5/hws/mlx5dr.h
@@ -50,6 +50,7 @@ enum mlx5dr_action_type {
        MLX5DR_ACTION_TYP_ASO_FIRST_HIT,
        MLX5DR_ACTION_TYP_CRYPTO_ENCRYPT,
        MLX5DR_ACTION_TYP_CRYPTO_DECRYPT,
+       MLX5DR_ACTION_TYP_INSERT_HEADER,
        MLX5DR_ACTION_TYP_DEST_ROOT,
        MLX5DR_ACTION_TYP_DEST_ARRAY,
        MLX5DR_ACTION_TYP_MAX,
@@ -174,6 +175,20 @@ struct mlx5dr_action_reformat_header {
        void *data;
 };
 
+struct mlx5dr_action_insert_header {
+       struct mlx5dr_action_reformat_header hdr;
+       /* PRM start anchor to which header will be inserted */
+       uint8_t anchor;
+       /* Header insertion offset in bytes, from the start
+        * anchor to the location where new header will be inserted.
+        */
+       uint8_t offset;
+       /* Indicates this header insertion adds encapsulation header to the 
packet,
+        * requiring device to update offloaded fields (for example IPv4 total 
length).
+        */
+       bool encap;
+};
+
 struct mlx5dr_action_mh_pattern {
        /* Byte size of modify actions provided by "data" */
        size_t sz;
@@ -813,6 +828,27 @@ mlx5dr_action_create_crypto(struct mlx5dr_context *ctx,
                            struct mlx5dr_action_crypto_attr *attr,
                            uint32_t flags);
 
+/* Create insert header action.
+ *
+ * @param[in] ctx
+ *     The context in which the new action will be created.
+ * @param[in] num_of_hdrs
+ *     Number of provided headers in "hdrs" array.
+ * @param[in] hdrs
+ *     Headers array containing header information.
+ * @param[in] log_bulk_size
+ *     Number of unique values used with this insert header.
+ * @param[in] flags
+ *     Action creation flags. (enum mlx5dr_action_flags)
+ * @return pointer to mlx5dr_action on success NULL otherwise.
+ */
+struct mlx5dr_action *
+mlx5dr_action_create_insert_header(struct mlx5dr_context *ctx,
+                                  uint8_t num_of_hdrs,
+                                  struct mlx5dr_action_insert_header *hdrs,
+                                  uint32_t log_bulk_size,
+                                  uint32_t flags);
+
 /* Destroy direct rule action.
  *
  * @param[in] action
diff --git a/drivers/net/mlx5/hws/mlx5dr_action.c 
b/drivers/net/mlx5/hws/mlx5dr_action.c
index fe9c39b207..9885555a8f 100644
--- a/drivers/net/mlx5/hws/mlx5dr_action.c
+++ b/drivers/net/mlx5/hws/mlx5dr_action.c
@@ -34,6 +34,7 @@ static const uint32_t 
action_order_arr[MLX5DR_TABLE_TYPE_MAX][MLX5DR_ACTION_TYP_
                BIT(MLX5DR_ACTION_TYP_PUSH_VLAN),
                BIT(MLX5DR_ACTION_TYP_PUSH_VLAN),
                BIT(MLX5DR_ACTION_TYP_MODIFY_HDR),
+               BIT(MLX5DR_ACTION_TYP_INSERT_HEADER) |
                BIT(MLX5DR_ACTION_TYP_REFORMAT_L2_TO_TNL_L2) |
                BIT(MLX5DR_ACTION_TYP_REFORMAT_L2_TO_TNL_L3),
                BIT(MLX5DR_ACTION_TYP_TBL) |
@@ -55,6 +56,7 @@ static const uint32_t 
action_order_arr[MLX5DR_TABLE_TYPE_MAX][MLX5DR_ACTION_TYP_
                BIT(MLX5DR_ACTION_TYP_PUSH_VLAN),
                BIT(MLX5DR_ACTION_TYP_PUSH_VLAN),
                BIT(MLX5DR_ACTION_TYP_MODIFY_HDR),
+               BIT(MLX5DR_ACTION_TYP_INSERT_HEADER) |
                BIT(MLX5DR_ACTION_TYP_REFORMAT_L2_TO_TNL_L2) |
                BIT(MLX5DR_ACTION_TYP_REFORMAT_L2_TO_TNL_L3),
                BIT(MLX5DR_ACTION_TYP_REFORMAT_TRAILER),
@@ -80,6 +82,7 @@ static const uint32_t 
action_order_arr[MLX5DR_TABLE_TYPE_MAX][MLX5DR_ACTION_TYP_
                BIT(MLX5DR_ACTION_TYP_PUSH_VLAN),
                BIT(MLX5DR_ACTION_TYP_PUSH_VLAN),
                BIT(MLX5DR_ACTION_TYP_MODIFY_HDR),
+               BIT(MLX5DR_ACTION_TYP_INSERT_HEADER) |
                BIT(MLX5DR_ACTION_TYP_REFORMAT_L2_TO_TNL_L2) |
                BIT(MLX5DR_ACTION_TYP_REFORMAT_L2_TO_TNL_L3),
                BIT(MLX5DR_ACTION_TYP_REFORMAT_TRAILER),
@@ -640,20 +643,15 @@ static void mlx5dr_action_fill_stc_attr(struct 
mlx5dr_action *action,
                attr->remove_header.end_anchor = MLX5_HEADER_ANCHOR_INNER_MAC;
                break;
        case MLX5DR_ACTION_TYP_REFORMAT_L2_TO_TNL_L2:
-               attr->action_type = MLX5_IFC_STC_ACTION_TYPE_HEADER_INSERT;
-               attr->action_offset = MLX5DR_ACTION_OFFSET_DW6;
-               attr->insert_header.encap = 1;
-               attr->insert_header.insert_anchor = 
MLX5_HEADER_ANCHOR_PACKET_START;
-               attr->insert_header.arg_id = action->reformat.arg_obj->id;
-               attr->insert_header.header_size = action->reformat.header_size;
-               break;
        case MLX5DR_ACTION_TYP_REFORMAT_L2_TO_TNL_L3:
+       case MLX5DR_ACTION_TYP_INSERT_HEADER:
                attr->action_type = MLX5_IFC_STC_ACTION_TYPE_HEADER_INSERT;
                attr->action_offset = MLX5DR_ACTION_OFFSET_DW6;
-               attr->insert_header.encap = 1;
-               attr->insert_header.insert_anchor = 
MLX5_HEADER_ANCHOR_PACKET_START;
+               attr->insert_header.encap = action->reformat.encap;
+               attr->insert_header.insert_anchor = action->reformat.anchor;
                attr->insert_header.arg_id = action->reformat.arg_obj->id;
                attr->insert_header.header_size = action->reformat.header_size;
+               attr->insert_header.insert_offset = action->reformat.offset;
                break;
        case MLX5DR_ACTION_TYP_ASO_METER:
                attr->action_offset = MLX5DR_ACTION_OFFSET_DW6;
@@ -1382,7 +1380,7 @@ mlx5dr_action_create_reformat_root(struct mlx5dr_action 
*action,
 }
 
 static int
-mlx5dr_action_handle_l2_to_tunnel_l2(struct mlx5dr_action *action,
+mlx5dr_action_handle_insert_with_ptr(struct mlx5dr_action *action,
                                     uint8_t num_of_hdrs,
                                     struct mlx5dr_action_reformat_header *hdrs,
                                     uint32_t log_bulk_sz)
@@ -1392,8 +1390,8 @@ mlx5dr_action_handle_l2_to_tunnel_l2(struct mlx5dr_action 
*action,
        int ret, i;
 
        for (i = 0; i < num_of_hdrs; i++) {
-               if (hdrs[i].sz % 2 != 0) {
-                       DR_LOG(ERR, "Header data size should be multiply of 2");
+               if (hdrs[i].sz % W_SIZE != 0) {
+                       DR_LOG(ERR, "Header data size should be in WORD 
granularity");
                        rte_errno = EINVAL;
                        return rte_errno;
                }
@@ -1415,6 +1413,13 @@ mlx5dr_action_handle_l2_to_tunnel_l2(struct 
mlx5dr_action *action,
                action[i].reformat.num_of_hdrs = num_of_hdrs;
                action[i].reformat.max_hdr_sz = max_sz;
 
+               if (action[i].type == MLX5DR_ACTION_TYP_REFORMAT_L2_TO_TNL_L2 ||
+                   action[i].type == MLX5DR_ACTION_TYP_REFORMAT_L2_TO_TNL_L3) {
+                       action[i].reformat.anchor = 
MLX5_HEADER_ANCHOR_PACKET_START;
+                       action[i].reformat.offset = 0;
+                       action[i].reformat.encap = 1;
+               }
+
                ret = mlx5dr_action_create_stcs(&action[i], NULL);
                if (ret) {
                        DR_LOG(ERR, "Failed to create stc for reformat");
@@ -1448,7 +1453,7 @@ mlx5dr_action_handle_l2_to_tunnel_l3(struct mlx5dr_action 
*action,
        }
 
        /* Reuse the insert with pointer for the L2L3 header */
-       ret = mlx5dr_action_handle_l2_to_tunnel_l2(action,
+       ret = mlx5dr_action_handle_insert_with_ptr(action,
                                                   num_of_hdrs,
                                                   hdrs,
                                                   log_bulk_sz);
@@ -1592,7 +1597,7 @@ mlx5dr_action_create_reformat_hws(struct mlx5dr_action 
*action,
                ret = mlx5dr_action_create_stcs(action, NULL);
                break;
        case MLX5DR_ACTION_TYP_REFORMAT_L2_TO_TNL_L2:
-               ret = mlx5dr_action_handle_l2_to_tunnel_l2(action, num_of_hdrs, 
hdrs, bulk_size);
+               ret = mlx5dr_action_handle_insert_with_ptr(action, num_of_hdrs, 
hdrs, bulk_size);
                break;
        case MLX5DR_ACTION_TYP_REFORMAT_L2_TO_TNL_L3:
                ret = mlx5dr_action_handle_l2_to_tunnel_l3(action, num_of_hdrs, 
hdrs, bulk_size);
@@ -1622,6 +1627,7 @@ mlx5dr_action_create_reformat(struct mlx5dr_context *ctx,
 
        if (!num_of_hdrs) {
                DR_LOG(ERR, "Reformat num_of_hdrs cannot be zero");
+               rte_errno = EINVAL;
                return NULL;
        }
 
@@ -1657,7 +1663,6 @@ mlx5dr_action_create_reformat(struct mlx5dr_context *ctx,
        ret = mlx5dr_action_create_reformat_hws(action, num_of_hdrs, hdrs, 
log_bulk_size);
        if (ret) {
                DR_LOG(ERR, "Failed to create HWS reformat action");
-               rte_errno = EINVAL;
                goto free_action;
        }
 
@@ -2186,6 +2191,81 @@ mlx5dr_action_create_reformat_trailer(struct 
mlx5dr_context *ctx,
        return action;
 }
 
+struct mlx5dr_action *
+mlx5dr_action_create_insert_header(struct mlx5dr_context *ctx,
+                                  uint8_t num_of_hdrs,
+                                  struct mlx5dr_action_insert_header *hdrs,
+                                  uint32_t log_bulk_size,
+                                  uint32_t flags)
+{
+       struct mlx5dr_action_reformat_header *reformat_hdrs;
+       struct mlx5dr_action *action;
+       int i, ret;
+
+       if (!num_of_hdrs) {
+               DR_LOG(ERR, "Reformat num_of_hdrs cannot be zero");
+               return NULL;
+       }
+
+       if (mlx5dr_action_is_root_flags(flags)) {
+               DR_LOG(ERR, "Dynamic reformat action not supported over root");
+               rte_errno = ENOTSUP;
+               return NULL;
+       }
+
+       if (!mlx5dr_action_is_hws_flags(flags) ||
+           ((flags & MLX5DR_ACTION_FLAG_SHARED) && (log_bulk_size || 
num_of_hdrs > 1))) {
+               DR_LOG(ERR, "Reformat flags don't fit HWS (flags: 0x%x)", 
flags);
+               rte_errno = EINVAL;
+               return NULL;
+       }
+
+       action = mlx5dr_action_create_generic_bulk(ctx, flags,
+                                                  
MLX5DR_ACTION_TYP_INSERT_HEADER,
+                                                  num_of_hdrs);
+       if (!action)
+               return NULL;
+
+       reformat_hdrs = simple_calloc(num_of_hdrs, sizeof(*reformat_hdrs));
+       if (!reformat_hdrs) {
+               DR_LOG(ERR, "Failed to allocate memory for reformat_hdrs");
+               rte_errno = ENOMEM;
+               goto free_action;
+       }
+
+       for (i = 0; i < num_of_hdrs; i++) {
+               if (hdrs[i].offset % W_SIZE != 0) {
+                       DR_LOG(ERR, "Header offset should be in WORD 
granularity");
+                       rte_errno = EINVAL;
+                       goto free_reformat_hdrs;
+               }
+
+               action[i].reformat.anchor = hdrs[i].anchor;
+               action[i].reformat.encap = hdrs[i].encap;
+               action[i].reformat.offset = hdrs[i].offset;
+               reformat_hdrs[i].sz = hdrs[i].hdr.sz;
+               reformat_hdrs[i].data = hdrs[i].hdr.data;
+       }
+
+       ret = mlx5dr_action_handle_insert_with_ptr(action, num_of_hdrs,
+                                                  reformat_hdrs, 
log_bulk_size);
+       if (ret) {
+               DR_LOG(ERR, "Failed to create HWS reformat action");
+               rte_errno = EINVAL;
+               goto free_reformat_hdrs;
+       }
+
+       simple_free(reformat_hdrs);
+
+       return action;
+
+free_reformat_hdrs:
+       simple_free(reformat_hdrs);
+free_action:
+       simple_free(action);
+       return NULL;
+}
+
 static void mlx5dr_action_destroy_hws(struct mlx5dr_action *action)
 {
        struct mlx5dr_devx_obj *obj = NULL;
@@ -2252,6 +2332,7 @@ static void mlx5dr_action_destroy_hws(struct 
mlx5dr_action *action)
                        mlx5dr_action_destroy_stcs(&action[i]);
                mlx5dr_cmd_destroy_obj(action->reformat.arg_obj);
                break;
+       case MLX5DR_ACTION_TYP_INSERT_HEADER:
        case MLX5DR_ACTION_TYP_REFORMAT_L2_TO_TNL_L2:
                for (i = 0; i < action->reformat.num_of_hdrs; i++)
                        mlx5dr_action_destroy_stcs(&action[i]);
@@ -2850,6 +2931,7 @@ int mlx5dr_action_template_process(struct 
mlx5dr_action_template *at)
                        setter->idx_single = i;
                        break;
 
+               case MLX5DR_ACTION_TYP_INSERT_HEADER:
                case MLX5DR_ACTION_TYP_REFORMAT_L2_TO_TNL_L2:
                        /* Double insert header with pointer */
                        setter = mlx5dr_action_setter_find_first(last_setter, 
ASF_DOUBLE);
diff --git a/drivers/net/mlx5/hws/mlx5dr_action.h 
b/drivers/net/mlx5/hws/mlx5dr_action.h
index b64d6cc9a8..02358da4cb 100644
--- a/drivers/net/mlx5/hws/mlx5dr_action.h
+++ b/drivers/net/mlx5/hws/mlx5dr_action.h
@@ -136,8 +136,11 @@ struct mlx5dr_action {
                                struct {
                                        struct mlx5dr_devx_obj *arg_obj;
                                        uint32_t header_size;
-                                       uint8_t num_of_hdrs;
                                        uint16_t max_hdr_sz;
+                                       uint8_t num_of_hdrs;
+                                       uint8_t anchor;
+                                       uint8_t offset;
+                                       bool encap;
                                } reformat;
                                struct {
                                        struct mlx5dr_devx_obj *devx_obj;
diff --git a/drivers/net/mlx5/hws/mlx5dr_cmd.c 
b/drivers/net/mlx5/hws/mlx5dr_cmd.c
index 02547e7178..0ba4774f08 100644
--- a/drivers/net/mlx5/hws/mlx5dr_cmd.c
+++ b/drivers/net/mlx5/hws/mlx5dr_cmd.c
@@ -492,9 +492,9 @@ mlx5dr_cmd_stc_modify_set_stc_param(struct 
mlx5dr_cmd_stc_modify_attr *stc_attr,
                         stc_attr->insert_header.insert_anchor);
                /* HW gets the next 2 sizes in words */
                MLX5_SET(stc_ste_param_insert, stc_parm, insert_size,
-                        stc_attr->insert_header.header_size / 2);
+                        stc_attr->insert_header.header_size / W_SIZE);
                MLX5_SET(stc_ste_param_insert, stc_parm, insert_offset,
-                        stc_attr->insert_header.insert_offset / 2);
+                        stc_attr->insert_header.insert_offset / W_SIZE);
                MLX5_SET(stc_ste_param_insert, stc_parm, insert_argument,
                         stc_attr->insert_header.arg_id);
                break;
diff --git a/drivers/net/mlx5/hws/mlx5dr_debug.c 
b/drivers/net/mlx5/hws/mlx5dr_debug.c
index 552dba5e63..29e207765b 100644
--- a/drivers/net/mlx5/hws/mlx5dr_debug.c
+++ b/drivers/net/mlx5/hws/mlx5dr_debug.c
@@ -29,6 +29,7 @@ const char *mlx5dr_debug_action_type_str[] = {
        [MLX5DR_ACTION_TYP_DEST_ARRAY] = "DEST_ARRAY",
        [MLX5DR_ACTION_TYP_CRYPTO_ENCRYPT] = "CRYPTO_ENCRYPT",
        [MLX5DR_ACTION_TYP_CRYPTO_DECRYPT] = "CRYPTO_DECRYPT",
+       [MLX5DR_ACTION_TYP_INSERT_HEADER] = "INSERT_HEADER",
 };
 
 static_assert(ARRAY_SIZE(mlx5dr_debug_action_type_str) == 
MLX5DR_ACTION_TYP_MAX,
diff --git a/drivers/net/mlx5/hws/mlx5dr_internal.h 
b/drivers/net/mlx5/hws/mlx5dr_internal.h
index 021d599a56..b9efdc4a9a 100644
--- a/drivers/net/mlx5/hws/mlx5dr_internal.h
+++ b/drivers/net/mlx5/hws/mlx5dr_internal.h
@@ -40,6 +40,7 @@
 #include "mlx5dr_pat_arg.h"
 #include "mlx5dr_crc32.h"
 
+#define W_SIZE         2
 #define DW_SIZE                4
 #define BITS_IN_BYTE   8
 #define BITS_IN_DW     (BITS_IN_BYTE * DW_SIZE)
-- 
2.39.2

Reply via email to