]> www.infradead.org Git - users/hch/misc.git/commitdiff
net/mlx5: fs, add HWS modify header API function
authorMoshe Shemesh <moshe@nvidia.com>
Thu, 9 Jan 2025 16:05:37 +0000 (18:05 +0200)
committerJakub Kicinski <kuba@kernel.org>
Tue, 14 Jan 2025 03:21:08 +0000 (19:21 -0800)
Add modify header alloc and dealloc API functions to provide modify
header actions for steering rules. Use fs hws pools to get actions from
shared bulks of modify header actions.

Signed-off-by: Moshe Shemesh <moshe@nvidia.com>
Reviewed-by: Yevgeny Kliteynik <kliteyn@nvidia.com>
Reviewed-by: Mark Bloch <mbloch@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
Link: https://patch.msgid.link/20250109160546.1733647-7-tariqt@nvidia.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/ethernet/mellanox/mlx5/core/fs_core.h
drivers/net/ethernet/mellanox/mlx5/core/steering/hws/fs_hws.c
drivers/net/ethernet/mellanox/mlx5/core/steering/hws/fs_hws.h
drivers/net/ethernet/mellanox/mlx5/core/steering/hws/fs_hws_pools.c
drivers/net/ethernet/mellanox/mlx5/core/steering/hws/fs_hws_pools.h

index b40e5310bef74d01817672a782c455dc546af1e2..5875364cef4bb67cec9c779d72fb615eb87366e4 100644 (file)
@@ -65,6 +65,7 @@ struct mlx5_modify_hdr {
        enum mlx5_flow_resource_owner owner;
        union {
                struct mlx5_fs_dr_action fs_dr_action;
+               struct mlx5_fs_hws_action fs_hws_action;
                u32 id;
        };
 };
index a584aa16d2d1e7e652b2def071014d987095bf85..543a7b2f0dff5f7bbfc610d404bbd0ed0d611e35 100644 (file)
@@ -15,6 +15,9 @@ mlx5_fs_create_action_remove_header_vlan(struct mlx5hws_context *ctx);
 static void
 mlx5_fs_destroy_pr_pool(struct mlx5_fs_pool *pool, struct xarray *pr_pools,
                        unsigned long index);
+static void
+mlx5_fs_destroy_mh_pool(struct mlx5_fs_pool *pool, struct xarray *mh_pools,
+                       unsigned long index);
 
 static int mlx5_fs_init_hws_actions_pool(struct mlx5_core_dev *dev,
                                         struct mlx5_fs_hws_context *fs_ctx)
@@ -58,6 +61,7 @@ static int mlx5_fs_init_hws_actions_pool(struct mlx5_core_dev *dev,
                goto cleanup_insert_hdr;
        xa_init(&hws_pool->el2tol3tnl_pools);
        xa_init(&hws_pool->el2tol2tnl_pools);
+       xa_init(&hws_pool->mh_pools);
        return 0;
 
 cleanup_insert_hdr:
@@ -83,6 +87,9 @@ static void mlx5_fs_cleanup_hws_actions_pool(struct mlx5_fs_hws_context *fs_ctx)
        struct mlx5_fs_pool *pool;
        unsigned long i;
 
+       xa_for_each(&hws_pool->mh_pools, i, pool)
+               mlx5_fs_destroy_mh_pool(pool, &hws_pool->mh_pools, i);
+       xa_destroy(&hws_pool->mh_pools);
        xa_for_each(&hws_pool->el2tol2tnl_pools, i, pool)
                mlx5_fs_destroy_pr_pool(pool, &hws_pool->el2tol2tnl_pools, i);
        xa_destroy(&hws_pool->el2tol2tnl_pools);
@@ -532,6 +539,117 @@ static void mlx5_cmd_hws_packet_reformat_dealloc(struct mlx5_flow_root_namespace
        pkt_reformat->fs_hws_action.pr_data = NULL;
 }
 
+static struct mlx5_fs_pool *
+mlx5_fs_create_mh_pool(struct mlx5_core_dev *dev,
+                      struct mlx5hws_action_mh_pattern *pattern,
+                      struct xarray *mh_pools, unsigned long index)
+{
+       struct mlx5_fs_pool *pool;
+       int err;
+
+       pool = kzalloc(sizeof(*pool), GFP_KERNEL);
+       if (!pool)
+               return ERR_PTR(-ENOMEM);
+       err = mlx5_fs_hws_mh_pool_init(pool, dev, pattern);
+       if (err)
+               goto free_pool;
+       err = xa_insert(mh_pools, index, pool, GFP_KERNEL);
+       if (err)
+               goto cleanup_pool;
+       return pool;
+
+cleanup_pool:
+       mlx5_fs_hws_mh_pool_cleanup(pool);
+free_pool:
+       kfree(pool);
+       return ERR_PTR(err);
+}
+
+static void
+mlx5_fs_destroy_mh_pool(struct mlx5_fs_pool *pool, struct xarray *mh_pools,
+                       unsigned long index)
+{
+       xa_erase(mh_pools, index);
+       mlx5_fs_hws_mh_pool_cleanup(pool);
+       kfree(pool);
+}
+
+static int mlx5_cmd_hws_modify_header_alloc(struct mlx5_flow_root_namespace *ns,
+                                           u8 namespace, u8 num_actions,
+                                           void *modify_actions,
+                                           struct mlx5_modify_hdr *modify_hdr)
+{
+       struct mlx5_fs_hws_actions_pool *hws_pool = &ns->fs_hws_context.hws_pool;
+       struct mlx5hws_action_mh_pattern pattern = {};
+       struct mlx5_fs_hws_mh *mh_data = NULL;
+       struct mlx5hws_action *hws_action;
+       struct mlx5_fs_pool *pool;
+       unsigned long i, cnt = 0;
+       bool known_pattern;
+       int err;
+
+       pattern.sz = MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto) * num_actions;
+       pattern.data = modify_actions;
+
+       known_pattern = false;
+       xa_for_each(&hws_pool->mh_pools, i, pool) {
+               if (mlx5_fs_hws_mh_pool_match(pool, &pattern)) {
+                       known_pattern = true;
+                       break;
+               }
+               cnt++;
+       }
+
+       if (!known_pattern) {
+               pool = mlx5_fs_create_mh_pool(ns->dev, &pattern,
+                                             &hws_pool->mh_pools, cnt);
+               if (IS_ERR(pool))
+                       return PTR_ERR(pool);
+       }
+       mh_data = mlx5_fs_hws_mh_pool_acquire_mh(pool);
+       if (IS_ERR(mh_data)) {
+               err = PTR_ERR(mh_data);
+               goto destroy_pool;
+       }
+       hws_action = mh_data->bulk->hws_action;
+       mh_data->data = kmemdup(pattern.data, pattern.sz, GFP_KERNEL);
+       if (!mh_data->data) {
+               err = -ENOMEM;
+               goto release_mh;
+       }
+       modify_hdr->fs_hws_action.mh_data = mh_data;
+       modify_hdr->fs_hws_action.fs_pool = pool;
+       modify_hdr->owner = MLX5_FLOW_RESOURCE_OWNER_SW;
+       modify_hdr->fs_hws_action.hws_action = hws_action;
+
+       return 0;
+
+release_mh:
+       mlx5_fs_hws_mh_pool_release_mh(pool, mh_data);
+destroy_pool:
+       if (!known_pattern)
+               mlx5_fs_destroy_mh_pool(pool, &hws_pool->mh_pools, cnt);
+       return err;
+}
+
+static void mlx5_cmd_hws_modify_header_dealloc(struct mlx5_flow_root_namespace *ns,
+                                              struct mlx5_modify_hdr *modify_hdr)
+{
+       struct mlx5_fs_hws_mh *mh_data;
+       struct mlx5_fs_pool *pool;
+
+       if (!modify_hdr->fs_hws_action.fs_pool || !modify_hdr->fs_hws_action.mh_data) {
+               mlx5_core_err(ns->dev, "Failed release modify-header\n");
+               return;
+       }
+
+       mh_data = modify_hdr->fs_hws_action.mh_data;
+       kfree(mh_data->data);
+       pool = modify_hdr->fs_hws_action.fs_pool;
+       mlx5_fs_hws_mh_pool_release_mh(pool, mh_data);
+       modify_hdr->fs_hws_action.mh_data = NULL;
+}
+
 static const struct mlx5_flow_cmds mlx5_flow_cmds_hws = {
        .create_flow_table = mlx5_cmd_hws_create_flow_table,
        .destroy_flow_table = mlx5_cmd_hws_destroy_flow_table,
@@ -541,6 +659,8 @@ static const struct mlx5_flow_cmds mlx5_flow_cmds_hws = {
        .destroy_flow_group = mlx5_cmd_hws_destroy_flow_group,
        .packet_reformat_alloc = mlx5_cmd_hws_packet_reformat_alloc,
        .packet_reformat_dealloc = mlx5_cmd_hws_packet_reformat_dealloc,
+       .modify_header_alloc = mlx5_cmd_hws_modify_header_alloc,
+       .modify_header_dealloc = mlx5_cmd_hws_modify_header_dealloc,
        .create_ns = mlx5_cmd_hws_create_ns,
        .destroy_ns = mlx5_cmd_hws_destroy_ns,
        .set_peer = mlx5_cmd_hws_set_peer,
index 19786838f6d6d5290d81ab0fc3ac5fb4dd64741c..1e53c015633838532a34ee3f8e3bc81896b15f9f 100644 (file)
@@ -18,6 +18,7 @@ struct mlx5_fs_hws_actions_pool {
        struct mlx5_fs_pool dl3tnltol2_pool;
        struct xarray el2tol3tnl_pools;
        struct xarray el2tol2tnl_pools;
+       struct xarray mh_pools;
 };
 
 struct mlx5_fs_hws_context {
@@ -34,6 +35,7 @@ struct mlx5_fs_hws_action {
        struct mlx5hws_action *hws_action;
        struct mlx5_fs_pool *fs_pool;
        struct mlx5_fs_hws_pr *pr_data;
+       struct mlx5_fs_hws_mh *mh_data;
 };
 
 struct mlx5_fs_hws_matcher {
index b12b96c94dae3c5c5e9acf33d49a54c5a8b2aed7..2a2175b6cfc077b287d230ff67da303f63a158bc 100644 (file)
@@ -236,3 +236,168 @@ struct mlx5hws_action *mlx5_fs_hws_pr_get_action(struct mlx5_fs_hws_pr *pr_data)
 {
        return pr_data->bulk->hws_action;
 }
+
+static struct mlx5hws_action *
+mlx5_fs_mh_bulk_action_create(struct mlx5hws_context *ctx,
+                             struct mlx5hws_action_mh_pattern *pattern)
+{
+       u32 flags = MLX5HWS_ACTION_FLAG_HWS_FDB;
+       u32 log_bulk_size;
+
+       log_bulk_size = ilog2(MLX5_FS_HWS_DEFAULT_BULK_LEN);
+       return mlx5hws_action_create_modify_header(ctx, 1, pattern,
+                                                  log_bulk_size, flags);
+}
+
+static struct mlx5_fs_bulk *
+mlx5_fs_hws_mh_bulk_create(struct mlx5_core_dev *dev, void *pool_ctx)
+{
+       struct mlx5hws_action_mh_pattern *pattern;
+       struct mlx5_flow_root_namespace *root_ns;
+       struct mlx5_fs_hws_mh_bulk *mh_bulk;
+       struct mlx5hws_context *ctx;
+       int bulk_len;
+
+       if (!pool_ctx)
+               return NULL;
+
+       root_ns = mlx5_get_root_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
+       if (!root_ns || root_ns->mode != MLX5_FLOW_STEERING_MODE_HMFS)
+               return NULL;
+
+       ctx = root_ns->fs_hws_context.hws_ctx;
+       if (!ctx)
+               return NULL;
+
+       pattern = pool_ctx;
+       bulk_len = MLX5_FS_HWS_DEFAULT_BULK_LEN;
+       mh_bulk = kvzalloc(struct_size(mh_bulk, mhs_data, bulk_len), GFP_KERNEL);
+       if (!mh_bulk)
+               return NULL;
+
+       if (mlx5_fs_bulk_init(dev, &mh_bulk->fs_bulk, bulk_len))
+               goto free_mh_bulk;
+
+       for (int i = 0; i < bulk_len; i++) {
+               mh_bulk->mhs_data[i].bulk = mh_bulk;
+               mh_bulk->mhs_data[i].offset = i;
+       }
+
+       mh_bulk->hws_action = mlx5_fs_mh_bulk_action_create(ctx, pattern);
+       if (!mh_bulk->hws_action)
+               goto cleanup_fs_bulk;
+
+       return &mh_bulk->fs_bulk;
+
+cleanup_fs_bulk:
+       mlx5_fs_bulk_cleanup(&mh_bulk->fs_bulk);
+free_mh_bulk:
+       kvfree(mh_bulk);
+       return NULL;
+}
+
+static int
+mlx5_fs_hws_mh_bulk_destroy(struct mlx5_core_dev *dev,
+                           struct mlx5_fs_bulk *fs_bulk)
+{
+       struct mlx5_fs_hws_mh_bulk *mh_bulk;
+
+       mh_bulk = container_of(fs_bulk, struct mlx5_fs_hws_mh_bulk, fs_bulk);
+       if (mlx5_fs_bulk_get_free_amount(fs_bulk) < fs_bulk->bulk_len) {
+               mlx5_core_err(dev, "Freeing bulk before all modify header were released\n");
+               return -EBUSY;
+       }
+
+       mlx5hws_action_destroy(mh_bulk->hws_action);
+       mlx5_fs_bulk_cleanup(fs_bulk);
+       kvfree(mh_bulk);
+
+       return 0;
+}
+
+static const struct mlx5_fs_pool_ops mlx5_fs_hws_mh_pool_ops = {
+       .bulk_create = mlx5_fs_hws_mh_bulk_create,
+       .bulk_destroy = mlx5_fs_hws_mh_bulk_destroy,
+       .update_threshold = mlx5_hws_pool_update_threshold,
+};
+
+int mlx5_fs_hws_mh_pool_init(struct mlx5_fs_pool *fs_hws_mh_pool,
+                            struct mlx5_core_dev *dev,
+                            struct mlx5hws_action_mh_pattern *pattern)
+{
+       struct mlx5hws_action_mh_pattern *pool_pattern;
+
+       pool_pattern = kzalloc(sizeof(*pool_pattern), GFP_KERNEL);
+       if (!pool_pattern)
+               return -ENOMEM;
+       pool_pattern->data = kmemdup(pattern->data, pattern->sz, GFP_KERNEL);
+       if (!pool_pattern->data) {
+               kfree(pool_pattern);
+               return -ENOMEM;
+       }
+       pool_pattern->sz = pattern->sz;
+       mlx5_fs_pool_init(fs_hws_mh_pool, dev, &mlx5_fs_hws_mh_pool_ops,
+                         pool_pattern);
+       return 0;
+}
+
+void mlx5_fs_hws_mh_pool_cleanup(struct mlx5_fs_pool *fs_hws_mh_pool)
+{
+       struct mlx5hws_action_mh_pattern *pool_pattern;
+
+       mlx5_fs_pool_cleanup(fs_hws_mh_pool);
+       pool_pattern = fs_hws_mh_pool->pool_ctx;
+       if (!pool_pattern)
+               return;
+       kfree(pool_pattern->data);
+       kfree(pool_pattern);
+}
+
+struct mlx5_fs_hws_mh *
+mlx5_fs_hws_mh_pool_acquire_mh(struct mlx5_fs_pool *mh_pool)
+{
+       struct mlx5_fs_pool_index pool_index = {};
+       struct mlx5_fs_hws_mh_bulk *mh_bulk;
+       int err;
+
+       err = mlx5_fs_pool_acquire_index(mh_pool, &pool_index);
+       if (err)
+               return ERR_PTR(err);
+       mh_bulk = container_of(pool_index.fs_bulk, struct mlx5_fs_hws_mh_bulk,
+                              fs_bulk);
+       return &mh_bulk->mhs_data[pool_index.index];
+}
+
+void mlx5_fs_hws_mh_pool_release_mh(struct mlx5_fs_pool *mh_pool,
+                                   struct mlx5_fs_hws_mh *mh_data)
+{
+       struct mlx5_fs_bulk *fs_bulk = &mh_data->bulk->fs_bulk;
+       struct mlx5_fs_pool_index pool_index = {};
+       struct mlx5_core_dev *dev = mh_pool->dev;
+
+       pool_index.fs_bulk = fs_bulk;
+       pool_index.index = mh_data->offset;
+       if (mlx5_fs_pool_release_index(mh_pool, &pool_index))
+               mlx5_core_warn(dev, "Attempted to release modify header which is not acquired\n");
+}
+
+bool mlx5_fs_hws_mh_pool_match(struct mlx5_fs_pool *mh_pool,
+                              struct mlx5hws_action_mh_pattern *pattern)
+{
+       struct mlx5hws_action_mh_pattern *pool_pattern;
+       int num_actions, i;
+
+       pool_pattern = mh_pool->pool_ctx;
+       if (WARN_ON_ONCE(!pool_pattern))
+               return false;
+
+       if (pattern->sz != pool_pattern->sz)
+               return false;
+       num_actions = pattern->sz / MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto);
+       for (i = 0; i < num_actions; i++) {
+               if ((__force __be32)pattern->data[i] !=
+                   (__force __be32)pool_pattern->data[i])
+                       return false;
+       }
+       return true;
+}
index 544b277be3c51424d695424b74d1ee2b34a8e4ba..30157db4d40e46688c13388d10c1b8ac6424fcad 100644 (file)
@@ -36,6 +36,19 @@ struct mlx5_fs_hws_pr_pool_ctx {
        size_t encap_data_size;
 };
 
+struct mlx5_fs_hws_mh {
+       struct mlx5_fs_hws_mh_bulk *bulk;
+       u32 offset;
+       u8 *data;
+};
+
+struct mlx5_fs_hws_mh_bulk {
+       struct mlx5_fs_bulk fs_bulk;
+       struct mlx5_fs_pool *mh_pool;
+       struct mlx5hws_action *hws_action;
+       struct mlx5_fs_hws_mh mhs_data[];
+};
+
 int mlx5_fs_hws_pr_pool_init(struct mlx5_fs_pool *pr_pool,
                             struct mlx5_core_dev *dev, size_t encap_data_size,
                             enum mlx5hws_action_type reformat_type);
@@ -45,4 +58,13 @@ struct mlx5_fs_hws_pr *mlx5_fs_hws_pr_pool_acquire_pr(struct mlx5_fs_pool *pr_po
 void mlx5_fs_hws_pr_pool_release_pr(struct mlx5_fs_pool *pr_pool,
                                    struct mlx5_fs_hws_pr *pr_data);
 struct mlx5hws_action *mlx5_fs_hws_pr_get_action(struct mlx5_fs_hws_pr *pr_data);
+int mlx5_fs_hws_mh_pool_init(struct mlx5_fs_pool *fs_hws_mh_pool,
+                            struct mlx5_core_dev *dev,
+                            struct mlx5hws_action_mh_pattern *pattern);
+void mlx5_fs_hws_mh_pool_cleanup(struct mlx5_fs_pool *fs_hws_mh_pool);
+struct mlx5_fs_hws_mh *mlx5_fs_hws_mh_pool_acquire_mh(struct mlx5_fs_pool *mh_pool);
+void mlx5_fs_hws_mh_pool_release_mh(struct mlx5_fs_pool *mh_pool,
+                                   struct mlx5_fs_hws_mh *mh_data);
+bool mlx5_fs_hws_mh_pool_match(struct mlx5_fs_pool *mh_pool,
+                              struct mlx5hws_action_mh_pattern *pattern);
 #endif /* __MLX5_FS_HWS_POOLS_H__ */