Skip to content

Commit

Permalink
Merge branch 'add-support-for-per-action-hw-stats'
Browse files Browse the repository at this point in the history
Oz Shlomo says:

====================
add support for per action hw stats

There are currently two mechanisms for populating hardware stats:
1. Using flow_offload api to query the flow's statistics.
   The api assumes that the same stats values apply to all
   the flow's actions.
   This assumption breaks when action drops or jumps over following
   actions.
2. Using hw_action api to query specific action stats via a driver
   callback method. This api assures the correct action stats for
   the offloaded action, however, it does not apply to the rest of the
   actions in the flow's actions array, as elaborated below.

The current hw_action api does not apply to the following use cases:
1. Actions that are implicitly created by filters (aka bind actions).
   In the following example only one counter will apply to the rule:
   tc filter add dev $DEV prio 2 protocol ip parent ffff: \
        flower ip_proto tcp dst_ip $IP2 \
        action police rate 1mbit burst 100k conform-exceed drop/pipe \
        action mirred egress redirect dev $DEV2

2. Action preceding a hw action.
   In the following example the same flow stats will apply to the sample and
   mirred actions:
    tc action add police rate 1mbit burst 100k conform-exceed drop / pipe
    tc filter add dev $DEV prio 2 protocol ip parent ffff: \
        flower ip_proto tcp dst_ip $IP2 \
        action sample rate 1 group 10 trunc 60 pipe \
        action police index 1 \
        action mirred egress redirect dev $DEV2

3. Meter action using jump control.
   In the following example the same flow stats will apply to both
   mirred actions:
    tc action add police rate 1mbit burst 100k conform-exceed jump 2 / pipe
    tc filter add dev $DEV prio 2 protocol ip parent ffff: \
        flower ip_proto tcp dst_ip $IP2 \
        action police index 1 \
        action mirred egress redirect dev $DEV2
        action mirred egress redirect dev $DEV3

This series provides the platform to query per action stats for in_hw flows.

The first four patches are preparation patches with no functionality change.
The fifth patch re-uses the existing flow action stats api to query action
stats for both classifier and action dumps.
The rest of the patches add per action stats support to the Mellanox driver.
====================

Link: https://lore.kernel.org/r/20230212132520.12571-1-ozsh@nvidia.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
  • Loading branch information
Paolo Abeni committed Feb 14, 2023
2 parents a71fad0 + 2b68d65 commit 991cbd4
Show file tree
Hide file tree
Showing 17 changed files with 375 additions and 48 deletions.
2 changes: 1 addition & 1 deletion drivers/net/ethernet/mellanox/mlx5/core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mlx5_core-$(CONFIG_MLX5_CLS_ACT) += en_tc.o en/rep/tc.o en/rep/neigh.o \
en/tc_tun_vxlan.o en/tc_tun_gre.o en/tc_tun_geneve.o \
en/tc_tun_mplsoudp.o diag/en_tc_tracepoint.o \
en/tc/post_act.o en/tc/int_port.o en/tc/meter.o \
en/tc/post_meter.o
en/tc/post_meter.o en/tc/act_stats.o

mlx5_core-$(CONFIG_MLX5_CLS_ACT) += en/tc/act/act.o en/tc/act/drop.o en/tc/act/trap.o \
en/tc/act/accept.o en/tc/act/mark.o en/tc/act/goto.o \
Expand Down
2 changes: 1 addition & 1 deletion drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ mlx5e_rep_indr_stats_act(struct mlx5e_rep_priv *rpriv,

act = mlx5e_tc_act_get(fl_act->id, ns_type);
if (!act || !act->stats_action)
return -EOPNOTSUPP;
return mlx5e_tc_fill_action_stats(priv, fl_act);

return act->stats_action(priv, fl_act);
}
Expand Down
197 changes: 197 additions & 0 deletions drivers/net/ethernet/mellanox/mlx5/core/en/tc/act_stats.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
// Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.

#include <linux/rhashtable.h>
#include <net/flow_offload.h>
#include "en/tc_priv.h"
#include "act_stats.h"
#include "en/fs.h"

struct mlx5e_tc_act_stats_handle {
struct rhashtable ht;
spinlock_t ht_lock; /* protects hashtable */
};

struct mlx5e_tc_act_stats {
unsigned long tc_act_cookie;

struct mlx5_fc *counter;
u64 lastpackets;
u64 lastbytes;

struct rhash_head hash;
struct rcu_head rcu_head;
};

static const struct rhashtable_params act_counters_ht_params = {
.head_offset = offsetof(struct mlx5e_tc_act_stats, hash),
.key_offset = 0,
.key_len = offsetof(struct mlx5e_tc_act_stats, counter),
.automatic_shrinking = true,
};

struct mlx5e_tc_act_stats_handle *
mlx5e_tc_act_stats_create(void)
{
struct mlx5e_tc_act_stats_handle *handle;
int err;

handle = kvzalloc(sizeof(*handle), GFP_KERNEL);
if (IS_ERR(handle))
return ERR_PTR(-ENOMEM);

err = rhashtable_init(&handle->ht, &act_counters_ht_params);
if (err)
goto err;

spin_lock_init(&handle->ht_lock);
return handle;
err:
kvfree(handle);
return ERR_PTR(err);
}

void mlx5e_tc_act_stats_free(struct mlx5e_tc_act_stats_handle *handle)
{
rhashtable_destroy(&handle->ht);
kvfree(handle);
}

static int
mlx5e_tc_act_stats_add(struct mlx5e_tc_act_stats_handle *handle,
unsigned long act_cookie,
struct mlx5_fc *counter)
{
struct mlx5e_tc_act_stats *act_stats, *old_act_stats;
struct rhashtable *ht = &handle->ht;
int err = 0;

act_stats = kvzalloc(sizeof(*act_stats), GFP_KERNEL);
if (!act_stats)
return -ENOMEM;

act_stats->tc_act_cookie = act_cookie;
act_stats->counter = counter;

rcu_read_lock();
old_act_stats = rhashtable_lookup_get_insert_fast(ht,
&act_stats->hash,
act_counters_ht_params);
if (IS_ERR(old_act_stats)) {
err = PTR_ERR(old_act_stats);
goto err_hash_insert;
} else if (old_act_stats) {
err = -EEXIST;
goto err_hash_insert;
}
rcu_read_unlock();

return 0;

err_hash_insert:
rcu_read_unlock();
kvfree(act_stats);
return err;
}

void
mlx5e_tc_act_stats_del_flow(struct mlx5e_tc_act_stats_handle *handle,
struct mlx5e_tc_flow *flow)
{
struct mlx5_flow_attr *attr;
struct mlx5e_tc_act_stats *act_stats;
int i;

if (!flow_flag_test(flow, USE_ACT_STATS))
return;

list_for_each_entry(attr, &flow->attrs, list) {
for (i = 0; i < attr->tc_act_cookies_count; i++) {
struct rhashtable *ht = &handle->ht;

spin_lock(&handle->ht_lock);
act_stats = rhashtable_lookup_fast(ht,
&attr->tc_act_cookies[i],
act_counters_ht_params);
if (act_stats &&
rhashtable_remove_fast(ht, &act_stats->hash,
act_counters_ht_params) == 0)
kvfree_rcu(act_stats, rcu_head);

spin_unlock(&handle->ht_lock);
}
}
}

int
mlx5e_tc_act_stats_add_flow(struct mlx5e_tc_act_stats_handle *handle,
struct mlx5e_tc_flow *flow)
{
struct mlx5_fc *curr_counter = NULL;
unsigned long last_cookie = 0;
struct mlx5_flow_attr *attr;
int err;
int i;

if (!flow_flag_test(flow, USE_ACT_STATS))
return 0;

list_for_each_entry(attr, &flow->attrs, list) {
if (attr->counter)
curr_counter = attr->counter;

for (i = 0; i < attr->tc_act_cookies_count; i++) {
/* jump over identical ids (e.g. pedit)*/
if (last_cookie == attr->tc_act_cookies[i])
continue;

err = mlx5e_tc_act_stats_add(handle, attr->tc_act_cookies[i], curr_counter);
if (err)
goto out_err;
last_cookie = attr->tc_act_cookies[i];
}
}

return 0;
out_err:
mlx5e_tc_act_stats_del_flow(handle, flow);
return err;
}

int
mlx5e_tc_act_stats_fill_stats(struct mlx5e_tc_act_stats_handle *handle,
struct flow_offload_action *fl_act)
{
struct rhashtable *ht = &handle->ht;
struct mlx5e_tc_act_stats *item;
struct mlx5e_tc_act_stats key;
u64 pkts, bytes, lastused;
int err = 0;

key.tc_act_cookie = fl_act->cookie;

rcu_read_lock();
item = rhashtable_lookup(ht, &key, act_counters_ht_params);
if (!item) {
rcu_read_unlock();
err = -ENOENT;
goto err_out;
}

mlx5_fc_query_cached_raw(item->counter,
&bytes, &pkts, &lastused);

flow_stats_update(&fl_act->stats,
bytes - item->lastbytes,
pkts - item->lastpackets,
0, lastused, FLOW_ACTION_HW_STATS_DELAYED);

item->lastpackets = pkts;
item->lastbytes = bytes;
rcu_read_unlock();

return 0;

err_out:
return err;
}
27 changes: 27 additions & 0 deletions drivers/net/ethernet/mellanox/mlx5/core/en/tc/act_stats.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
/* Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */

#ifndef __MLX5_EN_ACT_STATS_H__
#define __MLX5_EN_ACT_STATS_H__

#include <net/flow_offload.h>
#include "en/tc_priv.h"

struct mlx5e_tc_act_stats_handle;

struct mlx5e_tc_act_stats_handle *mlx5e_tc_act_stats_create(void);
void mlx5e_tc_act_stats_free(struct mlx5e_tc_act_stats_handle *handle);

int
mlx5e_tc_act_stats_add_flow(struct mlx5e_tc_act_stats_handle *handle,
struct mlx5e_tc_flow *flow);

void
mlx5e_tc_act_stats_del_flow(struct mlx5e_tc_act_stats_handle *handle,
struct mlx5e_tc_flow *flow);

int
mlx5e_tc_act_stats_fill_stats(struct mlx5e_tc_act_stats_handle *handle,
struct flow_offload_action *fl_act);

#endif /* __MLX5_EN_ACT_STATS_H__ */
1 change: 1 addition & 0 deletions drivers/net/ethernet/mellanox/mlx5/core/en/tc_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ enum {
MLX5E_TC_FLOW_FLAG_TUN_RX = MLX5E_TC_FLOW_BASE + 9,
MLX5E_TC_FLOW_FLAG_FAILED = MLX5E_TC_FLOW_BASE + 10,
MLX5E_TC_FLOW_FLAG_SAMPLE = MLX5E_TC_FLOW_BASE + 11,
MLX5E_TC_FLOW_FLAG_USE_ACT_STATS = MLX5E_TC_FLOW_BASE + 12,
};

struct mlx5e_tc_flow_parse_attr {
Expand Down
3 changes: 3 additions & 0 deletions drivers/net/ethernet/mellanox/mlx5/core/en_rep.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ struct mlx5_rep_uplink_priv {
struct mlx5e_tc_int_port_priv *int_port_priv;

struct mlx5e_flow_meters *flow_meters;

/* tc action stats */
struct mlx5e_tc_act_stats_handle *action_stats_handle;
};

struct mlx5e_rep_priv {
Expand Down
Loading

0 comments on commit 991cbd4

Please sign in to comment.