Skip to content

Commit

Permalink
Merge branch 'mlx5-support-hws-flow-meter-sampler-actions-in-fs-core'
Browse files Browse the repository at this point in the history
Tariq Toukan says:

====================
mlx5: Support HWS flow meter/sampler actions in FS core

This series by Moshe adds support for flow meter and flow sampler HW
Steering actions in FS core level. As these actions can be shared by
multiple rules, these patches use refcounts to manage the HWS actions
sharing in FS core level.
====================

Link: https://patch.msgid.link/1741543663-22123-1-git-send-email-tariqt@nvidia.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
  • Loading branch information
Paolo Abeni committed Mar 17, 2025
2 parents 2c99b2e + 32e658c commit 89694a5
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 52 deletions.
5 changes: 5 additions & 0 deletions drivers/net/ethernet/mellanox/mlx5/core/en/tc/meter.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,8 @@ mlx5e_tc_meter_get_stats(struct mlx5e_flow_meter_handle *meter,
*drops = packets2;
*lastuse = max_t(u64, lastuse1, lastuse2);
}

int mlx5e_flow_meter_get_base_id(struct mlx5e_flow_meter_handle *meter)
{
return meter->meters_obj->base_id;
}
13 changes: 13 additions & 0 deletions drivers/net/ethernet/mellanox/mlx5/core/en/tc/meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,17 @@ void
mlx5e_tc_meter_get_stats(struct mlx5e_flow_meter_handle *meter,
u64 *bytes, u64 *packets, u64 *drops, u64 *lastuse);

#if IS_ENABLED(CONFIG_MLX5_CLS_ACT)

int mlx5e_flow_meter_get_base_id(struct mlx5e_flow_meter_handle *meter);

#else /* CONFIG_MLX5_CLS_ACT */

static inline int
mlx5e_flow_meter_get_base_id(struct mlx5e_flow_meter_handle *meter)
{
return 0;
}
#endif /* CONFIG_MLX5_CLS_ACT */

#endif /* __MLX5_EN_FLOW_METER_H__ */
1 change: 1 addition & 0 deletions drivers/net/ethernet/mellanox/mlx5/core/eswitch_offloads.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ esw_setup_meter(struct mlx5_flow_attr *attr, struct mlx5_flow_act *flow_act)
meter = attr->meter_attr.meter;
flow_act->exe_aso.type = attr->exe_aso_type;
flow_act->exe_aso.object_id = meter->obj_id;
flow_act->exe_aso.base_id = mlx5e_flow_meter_get_base_id(meter);
flow_act->exe_aso.flow_meter.meter_idx = meter->idx;
flow_act->exe_aso.flow_meter.init_color = MLX5_FLOW_METER_COLOR_GREEN;
/* use metadata reg 5 for packet color */
Expand Down
8 changes: 1 addition & 7 deletions drivers/net/ethernet/mellanox/mlx5/core/fs_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,16 +347,10 @@ struct mlx5_fc {
u64 lastbytes;
};

struct mlx5_fc_bulk_hws_data {
struct mlx5hws_action *hws_action;
struct mutex lock; /* protects hws_action */
refcount_t hws_action_refcount;
};

struct mlx5_fc_bulk {
struct mlx5_fs_bulk fs_bulk;
u32 base_id;
struct mlx5_fc_bulk_hws_data hws_data;
struct mlx5_fs_hws_data hws_data;
struct mlx5_fc fcs[];
};

Expand Down
231 changes: 221 additions & 10 deletions drivers/net/ethernet/mellanox/mlx5/core/steering/hws/fs_hws.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ static int mlx5_fs_init_hws_actions_pool(struct mlx5_core_dev *dev,
xa_init(&hws_pool->table_dests);
xa_init(&hws_pool->vport_dests);
xa_init(&hws_pool->vport_vhca_dests);
xa_init(&hws_pool->aso_meters);
xa_init(&hws_pool->sample_dests);
return 0;

cleanup_insert_hdr:
Expand All @@ -88,10 +90,17 @@ static int mlx5_fs_init_hws_actions_pool(struct mlx5_core_dev *dev,
static void mlx5_fs_cleanup_hws_actions_pool(struct mlx5_fs_hws_context *fs_ctx)
{
struct mlx5_fs_hws_actions_pool *hws_pool = &fs_ctx->hws_pool;
struct mlx5_fs_hws_data *fs_hws_data;
struct mlx5hws_action *action;
struct mlx5_fs_pool *pool;
unsigned long i;

xa_for_each(&hws_pool->sample_dests, i, fs_hws_data)
kfree(fs_hws_data);
xa_destroy(&hws_pool->sample_dests);
xa_for_each(&hws_pool->aso_meters, i, fs_hws_data)
kfree(fs_hws_data);
xa_destroy(&hws_pool->aso_meters);
xa_for_each(&hws_pool->vport_vhca_dests, i, action)
mlx5hws_action_destroy(action);
xa_destroy(&hws_pool->vport_vhca_dests);
Expand Down Expand Up @@ -459,6 +468,106 @@ mlx5_fs_create_dest_action_range(struct mlx5hws_context *ctx,
flags);
}

static struct mlx5_fs_hws_data *
mlx5_fs_get_cached_hws_data(struct xarray *cache_xa, unsigned long index)
{
struct mlx5_fs_hws_data *fs_hws_data;
int err;

xa_lock(cache_xa);
fs_hws_data = xa_load(cache_xa, index);
if (!fs_hws_data) {
fs_hws_data = kzalloc(sizeof(*fs_hws_data), GFP_ATOMIC);
if (!fs_hws_data) {
xa_unlock(cache_xa);
return NULL;
}
refcount_set(&fs_hws_data->hws_action_refcount, 0);
mutex_init(&fs_hws_data->lock);
err = __xa_insert(cache_xa, index, fs_hws_data, GFP_ATOMIC);
if (err) {
kfree(fs_hws_data);
xa_unlock(cache_xa);
return NULL;
}
}
xa_unlock(cache_xa);

return fs_hws_data;
}

static struct mlx5hws_action *
mlx5_fs_get_action_aso_meter(struct mlx5_fs_hws_context *fs_ctx,
struct mlx5_exe_aso *exe_aso)
{
struct mlx5_fs_hws_create_action_ctx create_ctx;
struct mlx5hws_context *ctx = fs_ctx->hws_ctx;
struct mlx5_fs_hws_data *meter_hws_data;
u32 id = exe_aso->base_id;
struct xarray *meters_xa;

meters_xa = &fs_ctx->hws_pool.aso_meters;
meter_hws_data = mlx5_fs_get_cached_hws_data(meters_xa, id);
if (!meter_hws_data)
return NULL;

create_ctx.hws_ctx = ctx;
create_ctx.actions_type = MLX5HWS_ACTION_TYP_ASO_METER;
create_ctx.id = id;
create_ctx.return_reg_id = exe_aso->return_reg_id;

return mlx5_fs_get_hws_action(meter_hws_data, &create_ctx);
}

static void mlx5_fs_put_action_aso_meter(struct mlx5_fs_hws_context *fs_ctx,
struct mlx5_exe_aso *exe_aso)
{
struct mlx5_fs_hws_data *meter_hws_data;
struct xarray *meters_xa;

meters_xa = &fs_ctx->hws_pool.aso_meters;
meter_hws_data = xa_load(meters_xa, exe_aso->base_id);
if (!meter_hws_data)
return;
return mlx5_fs_put_hws_action(meter_hws_data);
}

static struct mlx5hws_action *
mlx5_fs_get_dest_action_sampler(struct mlx5_fs_hws_context *fs_ctx,
struct mlx5_flow_rule *dst)
{
struct mlx5_fs_hws_create_action_ctx create_ctx;
struct mlx5hws_context *ctx = fs_ctx->hws_ctx;
struct mlx5_fs_hws_data *sampler_hws_data;
u32 id = dst->dest_attr.sampler_id;
struct xarray *sampler_xa;

sampler_xa = &fs_ctx->hws_pool.sample_dests;
sampler_hws_data = mlx5_fs_get_cached_hws_data(sampler_xa, id);
if (!sampler_hws_data)
return NULL;

create_ctx.hws_ctx = ctx;
create_ctx.actions_type = MLX5HWS_ACTION_TYP_SAMPLER;
create_ctx.id = id;

return mlx5_fs_get_hws_action(sampler_hws_data, &create_ctx);
}

static void mlx5_fs_put_dest_action_sampler(struct mlx5_fs_hws_context *fs_ctx,
u32 sampler_id)
{
struct mlx5_fs_hws_data *sampler_hws_data;
struct xarray *sampler_xa;

sampler_xa = &fs_ctx->hws_pool.sample_dests;
sampler_hws_data = xa_load(sampler_xa, sampler_id);
if (!sampler_hws_data)
return;

mlx5_fs_put_hws_action(sampler_hws_data);
}

static struct mlx5hws_action *
mlx5_fs_create_action_dest_array(struct mlx5hws_context *ctx,
struct mlx5hws_action_dest_attr *dests,
Expand Down Expand Up @@ -519,26 +628,101 @@ mlx5_fs_create_action_last(struct mlx5hws_context *ctx)
return mlx5hws_action_create_last(ctx, flags);
}

static void mlx5_fs_destroy_fs_action(struct mlx5_fs_hws_rule_action *fs_action)
static struct mlx5hws_action *
mlx5_fs_create_hws_action(struct mlx5_fs_hws_create_action_ctx *create_ctx)
{
u32 flags = MLX5HWS_ACTION_FLAG_HWS_FDB | MLX5HWS_ACTION_FLAG_SHARED;

switch (create_ctx->actions_type) {
case MLX5HWS_ACTION_TYP_CTR:
return mlx5hws_action_create_counter(create_ctx->hws_ctx,
create_ctx->id, flags);
case MLX5HWS_ACTION_TYP_ASO_METER:
return mlx5hws_action_create_aso_meter(create_ctx->hws_ctx,
create_ctx->id,
create_ctx->return_reg_id,
flags);
case MLX5HWS_ACTION_TYP_SAMPLER:
return mlx5hws_action_create_flow_sampler(create_ctx->hws_ctx,
create_ctx->id, flags);
default:
return NULL;
}
}

struct mlx5hws_action *
mlx5_fs_get_hws_action(struct mlx5_fs_hws_data *fs_hws_data,
struct mlx5_fs_hws_create_action_ctx *create_ctx)
{
/* try avoid locking if not necessary */
if (refcount_inc_not_zero(&fs_hws_data->hws_action_refcount))
return fs_hws_data->hws_action;

mutex_lock(&fs_hws_data->lock);
if (refcount_inc_not_zero(&fs_hws_data->hws_action_refcount)) {
mutex_unlock(&fs_hws_data->lock);
return fs_hws_data->hws_action;
}
fs_hws_data->hws_action = mlx5_fs_create_hws_action(create_ctx);
if (!fs_hws_data->hws_action) {
mutex_unlock(&fs_hws_data->lock);
return NULL;
}
refcount_set(&fs_hws_data->hws_action_refcount, 1);
mutex_unlock(&fs_hws_data->lock);

return fs_hws_data->hws_action;
}

void mlx5_fs_put_hws_action(struct mlx5_fs_hws_data *fs_hws_data)
{
if (!fs_hws_data)
return;

/* try avoid locking if not necessary */
if (refcount_dec_not_one(&fs_hws_data->hws_action_refcount))
return;

mutex_lock(&fs_hws_data->lock);
if (!refcount_dec_and_test(&fs_hws_data->hws_action_refcount)) {
mutex_unlock(&fs_hws_data->lock);
return;
}
mlx5hws_action_destroy(fs_hws_data->hws_action);
fs_hws_data->hws_action = NULL;
mutex_unlock(&fs_hws_data->lock);
}

static void mlx5_fs_destroy_fs_action(struct mlx5_flow_root_namespace *ns,
struct mlx5_fs_hws_rule_action *fs_action)
{
struct mlx5_fs_hws_context *fs_ctx = &ns->fs_hws_context;

switch (mlx5hws_action_get_type(fs_action->action)) {
case MLX5HWS_ACTION_TYP_CTR:
mlx5_fc_put_hws_action(fs_action->counter);
break;
case MLX5HWS_ACTION_TYP_ASO_METER:
mlx5_fs_put_action_aso_meter(fs_ctx, fs_action->exe_aso);
break;
case MLX5HWS_ACTION_TYP_SAMPLER:
mlx5_fs_put_dest_action_sampler(fs_ctx, fs_action->sampler_id);
break;
default:
mlx5hws_action_destroy(fs_action->action);
}
}

static void
mlx5_fs_destroy_fs_actions(struct mlx5_fs_hws_rule_action **fs_actions,
mlx5_fs_destroy_fs_actions(struct mlx5_flow_root_namespace *ns,
struct mlx5_fs_hws_rule_action **fs_actions,
int *num_fs_actions)
{
int i;

/* Free in reverse order to handle action dependencies */
for (i = *num_fs_actions - 1; i >= 0; i--)
mlx5_fs_destroy_fs_action(*fs_actions + i);
mlx5_fs_destroy_fs_action(ns, *fs_actions + i);
*num_fs_actions = 0;
kfree(*fs_actions);
*fs_actions = NULL;
Expand Down Expand Up @@ -735,8 +919,25 @@ static int mlx5_fs_fte_get_hws_actions(struct mlx5_flow_root_namespace *ns,
}

if (fte_action->action & MLX5_FLOW_CONTEXT_ACTION_EXECUTE_ASO) {
err = -EOPNOTSUPP;
goto free_actions;
if (fte_action->exe_aso.type != MLX5_EXE_ASO_FLOW_METER ||
num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
err = -EOPNOTSUPP;
goto free_actions;
}

tmp_action = mlx5_fs_get_action_aso_meter(fs_ctx,
&fte_action->exe_aso);
if (!tmp_action) {
err = -ENOMEM;
goto free_actions;
}
(*ractions)[num_actions].aso_meter.offset =
fte_action->exe_aso.flow_meter.meter_idx;
(*ractions)[num_actions].aso_meter.init_color =
fte_action->exe_aso.flow_meter.init_color;
(*ractions)[num_actions++].action = tmp_action;
fs_actions[num_fs_actions].action = tmp_action;
fs_actions[num_fs_actions++].exe_aso = &fte_action->exe_aso;
}

if (fte_action->action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
Expand Down Expand Up @@ -784,6 +985,14 @@ static int mlx5_fs_fte_get_hws_actions(struct mlx5_flow_root_namespace *ns,
dest_action = mlx5_fs_get_dest_action_vport(fs_ctx, dst,
type_uplink);
break;
case MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER:
dest_action =
mlx5_fs_get_dest_action_sampler(fs_ctx,
dst);
fs_actions[num_fs_actions].action = dest_action;
fs_actions[num_fs_actions++].sampler_id =
dst->dest_attr.sampler_id;
break;
default:
err = -EOPNOTSUPP;
goto free_actions;
Expand Down Expand Up @@ -850,7 +1059,7 @@ static int mlx5_fs_fte_get_hws_actions(struct mlx5_flow_root_namespace *ns,
return 0;

free_actions:
mlx5_fs_destroy_fs_actions(&fs_actions, &num_fs_actions);
mlx5_fs_destroy_fs_actions(ns, &fs_actions, &num_fs_actions);
free_dest_actions_alloc:
kfree(dest_actions);
free_fs_actions_alloc:
Expand Down Expand Up @@ -900,7 +1109,7 @@ static int mlx5_cmd_hws_create_fte(struct mlx5_flow_root_namespace *ns,
return 0;

free_actions:
mlx5_fs_destroy_fs_actions(&fte->fs_hws_rule.hws_fs_actions,
mlx5_fs_destroy_fs_actions(ns, &fte->fs_hws_rule.hws_fs_actions,
&fte->fs_hws_rule.num_fs_actions);
out_err:
mlx5_core_err(ns->dev, "Failed to create hws rule err(%d)\n", err);
Expand All @@ -920,7 +1129,8 @@ static int mlx5_cmd_hws_delete_fte(struct mlx5_flow_root_namespace *ns,
err = mlx5hws_bwc_rule_destroy(rule->bwc_rule);
rule->bwc_rule = NULL;

mlx5_fs_destroy_fs_actions(&rule->hws_fs_actions, &rule->num_fs_actions);
mlx5_fs_destroy_fs_actions(ns, &rule->hws_fs_actions,
&rule->num_fs_actions);

return err;
}
Expand Down Expand Up @@ -958,11 +1168,12 @@ static int mlx5_cmd_hws_update_fte(struct mlx5_flow_root_namespace *ns,
if (ret)
goto restore_actions;

mlx5_fs_destroy_fs_actions(&saved_hws_fs_actions, &saved_num_fs_actions);
mlx5_fs_destroy_fs_actions(ns, &saved_hws_fs_actions,
&saved_num_fs_actions);
return ret;

restore_actions:
mlx5_fs_destroy_fs_actions(&fte->fs_hws_rule.hws_fs_actions,
mlx5_fs_destroy_fs_actions(ns, &fte->fs_hws_rule.hws_fs_actions,
&fte->fs_hws_rule.num_fs_actions);
fte->fs_hws_rule.hws_fs_actions = saved_hws_fs_actions;
fte->fs_hws_rule.num_fs_actions = saved_num_fs_actions;
Expand Down
Loading

0 comments on commit 89694a5

Please sign in to comment.