Skip to content

Commit 991cbd4

Browse files
author
Paolo Abeni
committed
Merge branch 'add-support-for-per-action-hw-stats'
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>
2 parents a71fad0 + 2b68d65 commit 991cbd4

File tree

17 files changed

+375
-48
lines changed

17 files changed

+375
-48
lines changed

drivers/net/ethernet/mellanox/mlx5/core/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ mlx5_core-$(CONFIG_MLX5_CLS_ACT) += en_tc.o en/rep/tc.o en/rep/neigh.o \
4747
en/tc_tun_vxlan.o en/tc_tun_gre.o en/tc_tun_geneve.o \
4848
en/tc_tun_mplsoudp.o diag/en_tc_tracepoint.o \
4949
en/tc/post_act.o en/tc/int_port.o en/tc/meter.o \
50-
en/tc/post_meter.o
50+
en/tc/post_meter.o en/tc/act_stats.o
5151

5252
mlx5_core-$(CONFIG_MLX5_CLS_ACT) += en/tc/act/act.o en/tc/act/drop.o en/tc/act/trap.o \
5353
en/tc/act/accept.o en/tc/act/mark.o en/tc/act/goto.o \

drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ mlx5e_rep_indr_stats_act(struct mlx5e_rep_priv *rpriv,
589589

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

594594
return act->stats_action(priv, fl_act);
595595
}
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2+
// Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
4+
#include <linux/rhashtable.h>
5+
#include <net/flow_offload.h>
6+
#include "en/tc_priv.h"
7+
#include "act_stats.h"
8+
#include "en/fs.h"
9+
10+
struct mlx5e_tc_act_stats_handle {
11+
struct rhashtable ht;
12+
spinlock_t ht_lock; /* protects hashtable */
13+
};
14+
15+
struct mlx5e_tc_act_stats {
16+
unsigned long tc_act_cookie;
17+
18+
struct mlx5_fc *counter;
19+
u64 lastpackets;
20+
u64 lastbytes;
21+
22+
struct rhash_head hash;
23+
struct rcu_head rcu_head;
24+
};
25+
26+
static const struct rhashtable_params act_counters_ht_params = {
27+
.head_offset = offsetof(struct mlx5e_tc_act_stats, hash),
28+
.key_offset = 0,
29+
.key_len = offsetof(struct mlx5e_tc_act_stats, counter),
30+
.automatic_shrinking = true,
31+
};
32+
33+
struct mlx5e_tc_act_stats_handle *
34+
mlx5e_tc_act_stats_create(void)
35+
{
36+
struct mlx5e_tc_act_stats_handle *handle;
37+
int err;
38+
39+
handle = kvzalloc(sizeof(*handle), GFP_KERNEL);
40+
if (IS_ERR(handle))
41+
return ERR_PTR(-ENOMEM);
42+
43+
err = rhashtable_init(&handle->ht, &act_counters_ht_params);
44+
if (err)
45+
goto err;
46+
47+
spin_lock_init(&handle->ht_lock);
48+
return handle;
49+
err:
50+
kvfree(handle);
51+
return ERR_PTR(err);
52+
}
53+
54+
void mlx5e_tc_act_stats_free(struct mlx5e_tc_act_stats_handle *handle)
55+
{
56+
rhashtable_destroy(&handle->ht);
57+
kvfree(handle);
58+
}
59+
60+
static int
61+
mlx5e_tc_act_stats_add(struct mlx5e_tc_act_stats_handle *handle,
62+
unsigned long act_cookie,
63+
struct mlx5_fc *counter)
64+
{
65+
struct mlx5e_tc_act_stats *act_stats, *old_act_stats;
66+
struct rhashtable *ht = &handle->ht;
67+
int err = 0;
68+
69+
act_stats = kvzalloc(sizeof(*act_stats), GFP_KERNEL);
70+
if (!act_stats)
71+
return -ENOMEM;
72+
73+
act_stats->tc_act_cookie = act_cookie;
74+
act_stats->counter = counter;
75+
76+
rcu_read_lock();
77+
old_act_stats = rhashtable_lookup_get_insert_fast(ht,
78+
&act_stats->hash,
79+
act_counters_ht_params);
80+
if (IS_ERR(old_act_stats)) {
81+
err = PTR_ERR(old_act_stats);
82+
goto err_hash_insert;
83+
} else if (old_act_stats) {
84+
err = -EEXIST;
85+
goto err_hash_insert;
86+
}
87+
rcu_read_unlock();
88+
89+
return 0;
90+
91+
err_hash_insert:
92+
rcu_read_unlock();
93+
kvfree(act_stats);
94+
return err;
95+
}
96+
97+
void
98+
mlx5e_tc_act_stats_del_flow(struct mlx5e_tc_act_stats_handle *handle,
99+
struct mlx5e_tc_flow *flow)
100+
{
101+
struct mlx5_flow_attr *attr;
102+
struct mlx5e_tc_act_stats *act_stats;
103+
int i;
104+
105+
if (!flow_flag_test(flow, USE_ACT_STATS))
106+
return;
107+
108+
list_for_each_entry(attr, &flow->attrs, list) {
109+
for (i = 0; i < attr->tc_act_cookies_count; i++) {
110+
struct rhashtable *ht = &handle->ht;
111+
112+
spin_lock(&handle->ht_lock);
113+
act_stats = rhashtable_lookup_fast(ht,
114+
&attr->tc_act_cookies[i],
115+
act_counters_ht_params);
116+
if (act_stats &&
117+
rhashtable_remove_fast(ht, &act_stats->hash,
118+
act_counters_ht_params) == 0)
119+
kvfree_rcu(act_stats, rcu_head);
120+
121+
spin_unlock(&handle->ht_lock);
122+
}
123+
}
124+
}
125+
126+
int
127+
mlx5e_tc_act_stats_add_flow(struct mlx5e_tc_act_stats_handle *handle,
128+
struct mlx5e_tc_flow *flow)
129+
{
130+
struct mlx5_fc *curr_counter = NULL;
131+
unsigned long last_cookie = 0;
132+
struct mlx5_flow_attr *attr;
133+
int err;
134+
int i;
135+
136+
if (!flow_flag_test(flow, USE_ACT_STATS))
137+
return 0;
138+
139+
list_for_each_entry(attr, &flow->attrs, list) {
140+
if (attr->counter)
141+
curr_counter = attr->counter;
142+
143+
for (i = 0; i < attr->tc_act_cookies_count; i++) {
144+
/* jump over identical ids (e.g. pedit)*/
145+
if (last_cookie == attr->tc_act_cookies[i])
146+
continue;
147+
148+
err = mlx5e_tc_act_stats_add(handle, attr->tc_act_cookies[i], curr_counter);
149+
if (err)
150+
goto out_err;
151+
last_cookie = attr->tc_act_cookies[i];
152+
}
153+
}
154+
155+
return 0;
156+
out_err:
157+
mlx5e_tc_act_stats_del_flow(handle, flow);
158+
return err;
159+
}
160+
161+
int
162+
mlx5e_tc_act_stats_fill_stats(struct mlx5e_tc_act_stats_handle *handle,
163+
struct flow_offload_action *fl_act)
164+
{
165+
struct rhashtable *ht = &handle->ht;
166+
struct mlx5e_tc_act_stats *item;
167+
struct mlx5e_tc_act_stats key;
168+
u64 pkts, bytes, lastused;
169+
int err = 0;
170+
171+
key.tc_act_cookie = fl_act->cookie;
172+
173+
rcu_read_lock();
174+
item = rhashtable_lookup(ht, &key, act_counters_ht_params);
175+
if (!item) {
176+
rcu_read_unlock();
177+
err = -ENOENT;
178+
goto err_out;
179+
}
180+
181+
mlx5_fc_query_cached_raw(item->counter,
182+
&bytes, &pkts, &lastused);
183+
184+
flow_stats_update(&fl_act->stats,
185+
bytes - item->lastbytes,
186+
pkts - item->lastpackets,
187+
0, lastused, FLOW_ACTION_HW_STATS_DELAYED);
188+
189+
item->lastpackets = pkts;
190+
item->lastbytes = bytes;
191+
rcu_read_unlock();
192+
193+
return 0;
194+
195+
err_out:
196+
return err;
197+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2+
/* Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
3+
4+
#ifndef __MLX5_EN_ACT_STATS_H__
5+
#define __MLX5_EN_ACT_STATS_H__
6+
7+
#include <net/flow_offload.h>
8+
#include "en/tc_priv.h"
9+
10+
struct mlx5e_tc_act_stats_handle;
11+
12+
struct mlx5e_tc_act_stats_handle *mlx5e_tc_act_stats_create(void);
13+
void mlx5e_tc_act_stats_free(struct mlx5e_tc_act_stats_handle *handle);
14+
15+
int
16+
mlx5e_tc_act_stats_add_flow(struct mlx5e_tc_act_stats_handle *handle,
17+
struct mlx5e_tc_flow *flow);
18+
19+
void
20+
mlx5e_tc_act_stats_del_flow(struct mlx5e_tc_act_stats_handle *handle,
21+
struct mlx5e_tc_flow *flow);
22+
23+
int
24+
mlx5e_tc_act_stats_fill_stats(struct mlx5e_tc_act_stats_handle *handle,
25+
struct flow_offload_action *fl_act);
26+
27+
#endif /* __MLX5_EN_ACT_STATS_H__ */

drivers/net/ethernet/mellanox/mlx5/core/en/tc_priv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ enum {
3030
MLX5E_TC_FLOW_FLAG_TUN_RX = MLX5E_TC_FLOW_BASE + 9,
3131
MLX5E_TC_FLOW_FLAG_FAILED = MLX5E_TC_FLOW_BASE + 10,
3232
MLX5E_TC_FLOW_FLAG_SAMPLE = MLX5E_TC_FLOW_BASE + 11,
33+
MLX5E_TC_FLOW_FLAG_USE_ACT_STATS = MLX5E_TC_FLOW_BASE + 12,
3334
};
3435

3536
struct mlx5e_tc_flow_parse_attr {

drivers/net/ethernet/mellanox/mlx5/core/en_rep.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ struct mlx5_rep_uplink_priv {
100100
struct mlx5e_tc_int_port_priv *int_port_priv;
101101

102102
struct mlx5e_flow_meters *flow_meters;
103+
104+
/* tc action stats */
105+
struct mlx5e_tc_act_stats_handle *action_stats_handle;
103106
};
104107

105108
struct mlx5e_rep_priv {

0 commit comments

Comments
 (0)