Skip to content

Commit 5954894

Browse files
congwangdavem330
authored andcommitted
net_sched: add performance counters for basic filter
Similar to u32 filter, it is useful to know how many times we reach each basic filter and how many times we pass the ematch attached to it. Sample output: filter protocol arp pref 49152 basic chain 0 filter protocol arp pref 49152 basic chain 0 handle 0x1 (rule hit 3 success 3) action order 1: gact action pass random type none pass val 0 index 1 ref 1 bind 1 installed 81 sec used 4 sec Action statistics: Sent 126 bytes 3 pkt (dropped 0, overlimits 0 requeues 0) backlog 0b 0p requeues 0 Cc: Jamal Hadi Salim <jhs@mojatatu.com> Cc: Jiri Pirko <jiri@resnulli.us> Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 0726f55 commit 5954894

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎include/uapi/linux/pkt_cls.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,19 @@ enum {
333333

334334
/* Basic filter */
335335

336+
struct tc_basic_pcnt {
337+
__u64 rcnt;
338+
__u64 rhit;
339+
};
340+
336341
enum {
337342
TCA_BASIC_UNSPEC,
338343
TCA_BASIC_CLASSID,
339344
TCA_BASIC_EMATCHES,
340345
TCA_BASIC_ACT,
341346
TCA_BASIC_POLICE,
347+
TCA_BASIC_PCNT,
348+
TCA_BASIC_PAD,
342349
__TCA_BASIC_MAX
343350
};
344351

‎net/sched/cls_basic.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/rtnetlink.h>
1919
#include <linux/skbuff.h>
2020
#include <linux/idr.h>
21+
#include <linux/percpu.h>
2122
#include <net/netlink.h>
2223
#include <net/act_api.h>
2324
#include <net/pkt_cls.h>
@@ -35,6 +36,7 @@ struct basic_filter {
3536
struct tcf_result res;
3637
struct tcf_proto *tp;
3738
struct list_head link;
39+
struct tc_basic_pcnt __percpu *pf;
3840
struct rcu_work rwork;
3941
};
4042

@@ -46,8 +48,10 @@ static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
4648
struct basic_filter *f;
4749

4850
list_for_each_entry_rcu(f, &head->flist, link) {
51+
__this_cpu_inc(f->pf->rcnt);
4952
if (!tcf_em_tree_match(skb, &f->ematches, NULL))
5053
continue;
54+
__this_cpu_inc(f->pf->rhit);
5155
*res = f->res;
5256
r = tcf_exts_exec(skb, &f->exts, res);
5357
if (r < 0)
@@ -89,6 +93,7 @@ static void __basic_delete_filter(struct basic_filter *f)
8993
tcf_exts_destroy(&f->exts);
9094
tcf_em_tree_destroy(&f->ematches);
9195
tcf_exts_put_net(&f->exts);
96+
free_percpu(f->pf);
9297
kfree(f);
9398
}
9499

@@ -208,6 +213,11 @@ static int basic_change(struct net *net, struct sk_buff *in_skb,
208213
if (err)
209214
goto errout;
210215
fnew->handle = handle;
216+
fnew->pf = alloc_percpu(struct tc_basic_pcnt);
217+
if (!fnew->pf) {
218+
err = -ENOMEM;
219+
goto errout;
220+
}
211221

212222
err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr,
213223
extack);
@@ -231,6 +241,7 @@ static int basic_change(struct net *net, struct sk_buff *in_skb,
231241

232242
return 0;
233243
errout:
244+
free_percpu(fnew->pf);
234245
tcf_exts_destroy(&fnew->exts);
235246
kfree(fnew);
236247
return err;
@@ -265,8 +276,10 @@ static void basic_bind_class(void *fh, u32 classid, unsigned long cl)
265276
static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
266277
struct sk_buff *skb, struct tcmsg *t)
267278
{
279+
struct tc_basic_pcnt gpf = {};
268280
struct basic_filter *f = fh;
269281
struct nlattr *nest;
282+
int cpu;
270283

271284
if (f == NULL)
272285
return skb->len;
@@ -281,6 +294,18 @@ static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
281294
nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
282295
goto nla_put_failure;
283296

297+
for_each_possible_cpu(cpu) {
298+
struct tc_basic_pcnt *pf = per_cpu_ptr(f->pf, cpu);
299+
300+
gpf.rcnt += pf->rcnt;
301+
gpf.rhit += pf->rhit;
302+
}
303+
304+
if (nla_put_64bit(skb, TCA_BASIC_PCNT,
305+
sizeof(struct tc_basic_pcnt),
306+
&gpf, TCA_BASIC_PAD))
307+
goto nla_put_failure;
308+
284309
if (tcf_exts_dump(skb, &f->exts) < 0 ||
285310
tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
286311
goto nla_put_failure;

0 commit comments

Comments
 (0)