Skip to contentSkip to conversation

net_sched: add reverse binding for tc class

PATCH Applied Cong Wang 3 participants 7 messages Aug 30, 2017 – Aug 31, 2017

Subsystems (5)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 net_sched: add reverse binding for tc class

TC filters when used as classifiers are bound to TC classes. However, there is a hidden difference when adding them in different orders:

1. If we add tc classes before its filters, everything is fine. Logically, the classes exist before we specify their ID's in filters, it is easy to bind them together, just as in the current code base.

2. If we add tc filters before the tc classes they bind, we have to do dynamic lookup in fast path. What's worse, this happens all the time not just once, because on fast path tcf_result is passed on stack, there is no way to propagate back to the one in tc filters.

This hidden difference hurts performance silently if we have many tc classes in hierarchy.

This patch intends to close this gap by doing the reverse binding when we create a new class, in this case we can actually search all the filters in its parent, match and fixup by classid. And because tcf_result is specific to each type of tc filter, we have to introduce a new ops for each filter to tell how to bind the class.

Note, we still can NOT totally get rid of those class lookup in ->enqueue() because cgroup and flow filters have no way to determine the classid at setup time, they still have to go through dynamic lookup.

Cc Jamal Hadi Salim <[email protected]>
---
 include/net/sch_generic.h |  1 +
 net/sched/cls_basic.c     |  9 +++++++
 net/sched/cls_bpf.c       |  9 +++++++
 net/sched/cls_flower.c    |  9 +++++++
 net/sched/cls_fw.c        |  9 +++++++
 net/sched/cls_matchall.c  |  9 +++++++
 net/sched/cls_route.c     |  9 +++++++
 net/sched/cls_rsvp.h      |  9 +++++++
 net/sched/cls_tcindex.c   |  9 +++++++
 net/sched/cls_u32.c       |  9 +++++++
 net/sched/sch_api.c       | 68 +++++++++++++++++++++++++++++++++++++++++++++--
 11 files changed, 148 insertions(+), 2 deletions(-)
include/net/sch_generic.h +1 -0
index c30b634c5f82..d6247a3c40df 100644
@@ -217,6 +217,7 @@ struct tcf_proto_ops {
 					void **, bool);
 	int			(*delete)(struct tcf_proto*, void *, bool*);
 	void			(*walk)(struct tcf_proto*, struct tcf_walker *arg);
+	void			(*bind_class)(void *, u32, unsigned long);
 
 	/* rtnetlink specific */
 	int			(*dump)(struct net*, struct tcf_proto*, void *,
net/sched/cls_basic.c +9 -0
index 73cc7f167a38..d89ebafd2239 100644
@@ -235,6 +235,14 @@ static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg)
 	}
 }
 
+static void basic_bind_class(void *fh, u32 classid, unsigned long cl)
+{
+	struct basic_filter *f = fh;
+
+	if (f && f->res.classid == classid)
+		f->res.class = cl;
+}
+
 static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
 		      struct sk_buff *skb, struct tcmsg *t)
 {
@@ -280,6 +288,7 @@ static struct tcf_proto_ops cls_basic_ops __read_mostly = {
 	.delete		=	basic_delete,
 	.walk		=	basic_walk,
 	.dump		=	basic_dump,
+	.bind_class	=	basic_bind_class,
 	.owner		=	THIS_MODULE,
 };
 
net/sched/cls_bpf.c +9 -0
index 6f2dffe30f25..520c5027646a 100644
@@ -607,6 +607,14 @@ static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, void *fh,
 	return -1;
 }
 
+static void cls_bpf_bind_class(void *fh, u32 classid, unsigned long cl)
+{
+	struct cls_bpf_prog *prog = fh;
+
+	if (prog && prog->res.classid == classid)
+		prog->res.class = cl;
+}
+
 static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg)
 {
 	struct cls_bpf_head *head = rtnl_dereference(tp->root);
@@ -635,6 +643,7 @@ static struct tcf_proto_ops cls_bpf_ops __read_mostly = {
 	.delete		=	cls_bpf_delete,
 	.walk		=	cls_bpf_walk,
 	.dump		=	cls_bpf_dump,
+	.bind_class	=	cls_bpf_bind_class,
 };
 
 static int __init cls_bpf_init_mod(void)
net/sched/cls_flower.c +9 -0
index bd9dab41f8af..23832d8862c0 100644
@@ -1360,6 +1360,14 @@ static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
 	return -1;
 }
 
+static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
+{
+	struct cls_fl_filter *f = fh;
+
+	if (f && f->res.classid == classid)
+		f->res.class = cl;
+}
+
 static struct tcf_proto_ops cls_fl_ops __read_mostly = {
 	.kind		= "flower",
 	.classify	= fl_classify,
@@ -1370,6 +1378,7 @@ static struct tcf_proto_ops cls_fl_ops __read_mostly = {
 	.delete		= fl_delete,
 	.walk		= fl_walk,
 	.dump		= fl_dump,
+	.bind_class	= fl_bind_class,
 	.owner		= THIS_MODULE,
 };
 
net/sched/cls_fw.c +9 -0
index 192255ec50bd..941245ad07fd 100644
@@ -412,6 +412,14 @@ static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh,
 	return -1;
 }
 
+static void fw_bind_class(void *fh, u32 classid, unsigned long cl)
+{
+	struct fw_filter *f = fh;
+
+	if (f && f->res.classid == classid)
+		f->res.class = cl;
+}
+
 static struct tcf_proto_ops cls_fw_ops __read_mostly = {
 	.kind		=	"fw",
 	.classify	=	fw_classify,
@@ -422,6 +430,7 @@ static struct tcf_proto_ops cls_fw_ops __read_mostly = {
 	.delete		=	fw_delete,
 	.walk		=	fw_walk,
 	.dump		=	fw_dump,
+	.bind_class	=	fw_bind_class,
 	.owner		=	THIS_MODULE,
 };
 
net/sched/cls_matchall.c +9 -0
index d4dc387f7a56..21cc45caf842 100644
@@ -251,6 +251,14 @@ static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
 	return -1;
 }
 
+static void mall_bind_class(void *fh, u32 classid, unsigned long cl)
+{
+	struct cls_mall_head *head = fh;
+
+	if (head && head->res.classid == classid)
+		head->res.class = cl;
+}
+
 static struct tcf_proto_ops cls_mall_ops __read_mostly = {
 	.kind		= "matchall",
 	.classify	= mall_classify,
@@ -261,6 +269,7 @@ static struct tcf_proto_ops cls_mall_ops __read_mostly = {
 	.delete		= mall_delete,
 	.walk		= mall_walk,
 	.dump		= mall_dump,
+	.bind_class	= mall_bind_class,
 	.owner		= THIS_MODULE,
 };
 
net/sched/cls_route.c +9 -0
index 3b70982394ce..9ddde65915d2 100644
@@ -624,6 +624,14 @@ static int route4_dump(struct net *net, struct tcf_proto *tp, void *fh,
 	return -1;
 }
 
+static void route4_bind_class(void *fh, u32 classid, unsigned long cl)
+{
+	struct route4_filter *f = fh;
+
+	if (f && f->res.classid == classid)
+		f->res.class = cl;
+}
+
 static struct tcf_proto_ops cls_route4_ops __read_mostly = {
 	.kind		=	"route",
 	.classify	=	route4_classify,
@@ -634,6 +642,7 @@ static struct tcf_proto_ops cls_route4_ops __read_mostly = {
 	.delete		=	route4_delete,
 	.walk		=	route4_walk,
 	.dump		=	route4_dump,
+	.bind_class	=	route4_bind_class,
 	.owner		=	THIS_MODULE,
 };
 
net/sched/cls_rsvp.h +9 -0
index 26203ff817f3..98c05db85bcb 100644
@@ -723,6 +723,14 @@ static int rsvp_dump(struct net *net, struct tcf_proto *tp, void *fh,
 	return -1;
 }
 
+static void rsvp_bind_class(void *fh, u32 classid, unsigned long cl)
+{
+	struct rsvp_filter *f = fh;
+
+	if (f && f->res.classid == classid)
+		f->res.class = cl;
+}
+
 static struct tcf_proto_ops RSVP_OPS __read_mostly = {
 	.kind		=	RSVP_ID,
 	.classify	=	rsvp_classify,
@@ -733,6 +741,7 @@ static struct tcf_proto_ops RSVP_OPS __read_mostly = {
 	.delete		=	rsvp_delete,
 	.walk		=	rsvp_walk,
 	.dump		=	rsvp_dump,
+	.bind_class	=	rsvp_bind_class,
 	.owner		=	THIS_MODULE,
 };
 
net/sched/cls_tcindex.c +9 -0
index fb281b9b2c52..14a7e08b2fa9 100644
@@ -606,6 +606,14 @@ static int tcindex_dump(struct net *net, struct tcf_proto *tp, void *fh,
 	return -1;
 }
 
+static void tcindex_bind_class(void *fh, u32 classid, unsigned long cl)
+{
+	struct tcindex_filter_result *r = fh;
+
+	if (r && r->res.classid == classid)
+		r->res.class = cl;
+}
+
 static struct tcf_proto_ops cls_tcindex_ops __read_mostly = {
 	.kind		=	"tcindex",
 	.classify	=	tcindex_classify,
@@ -616,6 +624,7 @@ static struct tcf_proto_ops cls_tcindex_ops __read_mostly = {
 	.delete		=	tcindex_delete,
 	.walk		=	tcindex_walk,
 	.dump		=	tcindex_dump,
+	.bind_class	=	tcindex_bind_class,
 	.owner		=	THIS_MODULE,
 };
 
net/sched/cls_u32.c +9 -0
index 99ea4c74dd5b..10b8d851fc6b 100644
@@ -1112,6 +1112,14 @@ static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
 	}
 }
 
+static void u32_bind_class(void *fh, u32 classid, unsigned long cl)
+{
+	struct tc_u_knode *n = fh;
+
+	if (n && n->res.classid == classid)
+		n->res.class = cl;
+}
+
 static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
 		    struct sk_buff *skb, struct tcmsg *t)
 {
@@ -1242,6 +1250,7 @@ static struct tcf_proto_ops cls_u32_ops __read_mostly = {
 	.delete		=	u32_delete,
 	.walk		=	u32_walk,
 	.dump		=	u32_dump,
+	.bind_class	=	u32_bind_class,
 	.owner		=	THIS_MODULE,
 };
 
net/sched/sch_api.c +66 -2

93 lines not on this page · raw message

Signature · 1 line
2.13.0
maintainer
#1 lore raw

On 08/30/2017 11:30 PM, Cong Wang wrote:
[...]

Quoted 3 lines

Note, we still can NOT totally get rid of those class lookup in ->enqueue() because cgroup and flow filters have no way to determine the classid at setup time, they still have to go through dynamic lookup.

[...]

Quoted 4 lines
---
  include/net/sch_generic.h |  1 +
  net/sched/cls_basic.c     |  9 +++++++
  net/sched/cls_bpf.c       |  9 +++++++

Same is for cls_bpf as well, so bind_class wouldn't work there either as we could return dynamic classids. bind_class cannot be added here, too.

Quoted 9 lines
  net/sched/cls_flower.c    |  9 +++++++
  net/sched/cls_fw.c        |  9 +++++++
  net/sched/cls_matchall.c  |  9 +++++++
  net/sched/cls_route.c     |  9 +++++++
  net/sched/cls_rsvp.h      |  9 +++++++
  net/sched/cls_tcindex.c   |  9 +++++++
  net/sched/cls_u32.c       |  9 +++++++
  net/sched/sch_api.c       | 68 +++++++++++++++++++++++++++++++++++++++++++++--
  11 files changed, 148 insertions(+), 2 deletions(-)
author
#2 lore raw
Quoted 15 lines · Daniel Borkmann

On 08/30/2017 11:30 PM, Cong Wang wrote:
[...]

Quoted 4 lines

Note, we still can NOT totally get rid of those class lookup in ->enqueue() because cgroup and flow filters have no way to determine the classid at setup time, they still have to go through dynamic lookup.

[...]

Quoted 5 lines
---
  include/net/sch_generic.h |  1 +
  net/sched/cls_basic.c     |  9 +++++++
  net/sched/cls_bpf.c       |  9 +++++++
Quoted 3 lines

Same is for cls_bpf as well, so bind_class wouldn't work there either as we could return dynamic classids. bind_class cannot be added here, too.

I think you are probably right, but the following code is misleading there:

        if (tb[TCA_BPF_CLASSID]) {
                prog->res.classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
                tcf_bind_filter(tp, &prog->res, base);
        }

If the classid is dynamic, why this tb[TCA_BPF_CLASSID]?

maintainer
#3 lore raw
Quoted 22 lines · Cong Wang
Quoted 9 lines · Daniel Borkmann

On 08/30/2017 11:30 PM, Cong Wang wrote:
[...]

Quoted 4 lines

Note, we still can NOT totally get rid of those class lookup in ->enqueue() because cgroup and flow filters have no way to determine the classid at setup time, they still have to go through dynamic lookup.

[...]

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
---
   include/net/sch_generic.h |  1 +
   net/sched/cls_basic.c     |  9 +++++++
   net/sched/cls_bpf.c       |  9 +++++++

Same is for cls_bpf as well, so bind_class wouldn't work there either as we could return dynamic classids. bind_class cannot be added here, too.

I think you are probably right, but the following code is misleading there:

Quoted 6 lines
         if (tb[TCA_BPF_CLASSID]) {
                 prog->res.classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
                 tcf_bind_filter(tp, &prog->res, base);
         }

If the classid is dynamic, why this tb[TCA_BPF_CLASSID]?

The prog->res.classid is the default one, but can be overridden later depending on the specified program. cls_bpf_classify() does after prog return (filter_res holds return code):

	[...]
		if (filter_res == 0)
			continue;
		if (filter_res != -1) {
			res->class   = 0;
			res->classid = filter_res;
		} else {
			*res = prog->res;
		}
	[...]

Meaning in case of a match (-1), we use the default bound one, but prog may as well return an alternative found classid if it wants to. So both versions are possible.

maintainer
#4 lore raw
Quoted 45 lines · Daniel Borkmann
Quoted 22 lines · Cong Wang
Quoted 9 lines · Daniel Borkmann

On 08/30/2017 11:30 PM, Cong Wang wrote:
[...]

Quoted 4 lines

Note, we still can NOT totally get rid of those class lookup in ->enqueue() because cgroup and flow filters have no way to determine the classid at setup time, they still have to go through dynamic lookup.

[...]

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
---
   include/net/sch_generic.h |  1 +
   net/sched/cls_basic.c     |  9 +++++++
   net/sched/cls_bpf.c       |  9 +++++++

Same is for cls_bpf as well, so bind_class wouldn't work there either as we could return dynamic classids. bind_class cannot be added here, too.

I think you are probably right, but the following code is misleading there:

Quoted 6 lines
         if (tb[TCA_BPF_CLASSID]) {
                 prog->res.classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
                 tcf_bind_filter(tp, &prog->res, base);
         }

If the classid is dynamic, why this tb[TCA_BPF_CLASSID]?

The prog->res.classid is the default one, but can be overridden later depending on the specified program. cls_bpf_classify() does after prog return (filter_res holds return code):

     [...]
         if (filter_res == 0)
             continue;
         if (filter_res != -1) {
             res->class   = 0;
             res->classid = filter_res;
         } else {
             *res = prog->res;
         }
     [...]
Quoted 3 lines

Meaning in case of a match (-1), we use the default bound one, but prog may as well return an alternative found classid if it wants to. So both versions are possible.

But even for that case your patch looks fine to me actually, since for dynamic classid we set class to 0. No objections from my side then.

author
#5 lore raw
Quoted 17 lines · Daniel Borkmann
Quoted 16 lines · Daniel Borkmann

The prog->res.classid is the default one, but can be overridden later depending on the specified program. cls_bpf_classify() does after prog return (filter_res holds return code):

     [...]
         if (filter_res == 0)
             continue;
         if (filter_res != -1) {
             res->class   = 0;
             res->classid = filter_res;
         } else {
             *res = prog->res;
         }
     [...]
Quoted 8 lines
Quoted 3 lines

Meaning in case of a match (-1), we use the default bound one, but prog may as well return an alternative found classid if it wants to. So both versions are possible.

But even for that case your patch looks fine to me actually, since for dynamic classid we set class to 0. No objections from my side then.

Sounds good. Then I will leave it as it is.

Thanks for explanation.

maintainer Applied
#6 lore raw
In reply to Cong Wang

From: Cong Wang <[email protected]>
Date: Wed, 30 Aug 2017 14:30:36 -0700

Quoted 23 lines

TC filters when used as classifiers are bound to TC classes. However, there is a hidden difference when adding them in different orders:

1. If we add tc classes before its filters, everything is fine. Logically, the classes exist before we specify their ID's in filters, it is easy to bind them together, just as in the current code base.

2. If we add tc filters before the tc classes they bind, we have to do dynamic lookup in fast path. What's worse, this happens all the time not just once, because on fast path tcf_result is passed on stack, there is no way to propagate back to the one in tc filters.

This hidden difference hurts performance silently if we have many tc classes in hierarchy.

This patch intends to close this gap by doing the reverse binding when we create a new class, in this case we can actually search all the filters in its parent, match and fixup by classid. And because tcf_result is specific to each type of tc filter, we have to introduce a new ops for each filter to tell how to bind the class.

Quoted 6 lines

Note, we still can NOT totally get rid of those class lookup in ->enqueue() because cgroup and flow filters have no way to determine the classid at setup time, they still have to go through dynamic lookup.

Cc Jamal Hadi Salim <[email protected]>

Applied, thanks.