Skip to contentSkip to conversation

tc: cls_bpf: make ingress and egress qdiscs consistent

PATCH In review Alexei Starovoitov 5 participants 13 messages Apr 3, 2015 – Apr 7, 2015 7 messages not in the archive

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tc: cls_bpf: make ingress and egress qdiscs consistent

BPF programs attached to ingress and egress qdiscs see inconsistent skb->data. For ingress L2 header is already pulled, whereas for egress it's present. That makes writing programs more difficult.
Make them consistent by pushing L2 before running the programs and pulling it back afterwards.
Similar approach is taken by skb_defer_rx_timestamp() which does push/pull before calling ptp_classify_raw()->BPF_PROG_RUN().

---

Looks like noone has tried bpf with ingress qdisc before, otherwise this problem would have been found sooner.
This patch is probably not needed for 'net', since tc+bpf infra only starting to come alive.

 net/sched/cls_bpf.c |    9 +++++++++
 1 file changed, 9 insertions(+)
net/sched/cls_bpf.c +9 -0
index 5c4171c5d2bd..b1cefd2037c9 100644
@@ -66,6 +66,12 @@ static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
 	struct cls_bpf_prog *prog;
 	int ret = -1;
 
+	if (tp->q->flags & TCQ_F_INGRESS) {
+		if (skb_headroom(skb) < ETH_HLEN)
+			return -1;
+		__skb_push(skb, ETH_HLEN);
+	}
+
 	/* Needed here for accessing maps. */
 	rcu_read_lock();
 	list_for_each_entry_rcu(prog, &head->plist, link) {
@@ -86,6 +92,9 @@ static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
 	}
 	rcu_read_unlock();
 
+	if (tp->q->flags & TCQ_F_INGRESS)
+		__skb_pull(skb, ETH_HLEN);
+
 	return ret;
 }
 
Signature · 1 line
1.7.9.5
#2 lore raw
In reply to a message not in the archive
Quoted 9 lines · Alexei Starovoitov

BPF programs attached to ingress and egress qdiscs see inconsistent skb->data. For ingress L2 header is already pulled, whereas for egress it's present. That makes writing programs more difficult.
Make them consistent by pushing L2 before running the programs and pulling it back afterwards.
Similar approach is taken by skb_defer_rx_timestamp() which does push/pull before calling ptp_classify_raw()->BPF_PROG_RUN().

Thanks for looking into this. This ends up going via ingress_enqueue(), right? Maybe it would be better to add a new netlink attribute for ingress qdisc there that sets a flag in ingress_qdisc_data to pull the header space before calling tc_classify() and restore it later on? So, it would be configurable from tc. Would that work?

#4 lore raw
In reply to a message not in the archive
Quoted 12 lines · Daniel Borkmann
Quoted 11 lines · Alexei Starovoitov

BPF programs attached to ingress and egress qdiscs see inconsistent skb->data.
For ingress L2 header is already pulled, whereas for egress it's present. That makes writing programs more difficult.
Make them consistent by pushing L2 before running the programs and pulling
it back afterwards.
Similar approach is taken by skb_defer_rx_timestamp() which does push/pull
before calling ptp_classify_raw()->BPF_PROG_RUN().

Quoted 3 lines
Quoted 1 line

Thanks for looking into this. This ends up going via ingress_enqueue(),

yes.

Quoted 4 lines

right? Maybe it would be better to add a new netlink attribute for ingress qdisc there that sets a flag in ingress_qdisc_data to pull the header space before calling tc_classify() and restore it later on? So, it would be configurable from tc. Would that work?

you mean a flag that will affect all classifiers? I'm not sure other classifiers care. Noone complained for years. I think it would be overdesign. Here the fix is trivial, which is my preference.

#6 lore raw
In reply to a message not in the archive
Quoted 19 lines · Alexei Starovoitov
Quoted 12 lines · Daniel Borkmann
Quoted 11 lines · Alexei Starovoitov

BPF programs attached to ingress and egress qdiscs see inconsistent skb->data.
For ingress L2 header is already pulled, whereas for egress it's present. That makes writing programs more difficult.
Make them consistent by pushing L2 before running the programs and pulling
it back afterwards.
Similar approach is taken by skb_defer_rx_timestamp() which does push/pull
before calling ptp_classify_raw()->BPF_PROG_RUN().

Quoted 3 lines
Quoted 1 line

Thanks for looking into this. This ends up going via ingress_enqueue(),

yes.

Quoted 8 lines
Quoted 4 lines

right? Maybe it would be better to add a new netlink attribute for ingress qdisc there that sets a flag in ingress_qdisc_data to pull the header space before calling tc_classify() and restore it later on? So, it would be configurable from tc. Would that work?

you mean a flag that will affect all classifiers? I'm not sure other classifiers care. Noone complained for years. I think it would be overdesign. Here the fix is trivial, which is my preference.

But the 'defect' is actually on the ingress qdisc side, right, not the classifier itself ... so if we do this in the classifier, we add two extra branches to the output path, which would never be taken.

Plus, other classifiers wanting to look into ethernet headers would then also need to pull from /within/ their classifier as well. What about classic BPF users?

I don't think fixing this in ingress qdisc is overdesign, but rather the better place to fix it. ;)

#8 lore raw
In reply to a message not in the archive
Quoted 34 lines · Daniel Borkmann
Quoted 20 lines · Alexei Starovoitov
Quoted 13 lines · Daniel Borkmann
Quoted 12 lines · Alexei Starovoitov

BPF programs attached to ingress and egress qdiscs see inconsistent skb->data.
For ingress L2 header is already pulled, whereas for egress it's present.
That makes writing programs more difficult.
Make them consistent by pushing L2 before running the programs and pulling
it back afterwards.
Similar approach is taken by skb_defer_rx_timestamp() which does push/pull
before calling ptp_classify_raw()->BPF_PROG_RUN().

Quoted 3 lines
Quoted 1 line

Thanks for looking into this. This ends up going via ingress_enqueue(),

yes.

Quoted 8 lines
Quoted 4 lines

right? Maybe it would be better to add a new netlink attribute for ingress qdisc there that sets a flag in ingress_qdisc_data to pull the header space before calling tc_classify() and restore it later on? So, it would be configurable from tc. Would that work?

you mean a flag that will affect all classifiers? I'm not sure other classifiers care. Noone complained for years. I think it would be overdesign. Here the fix is trivial, which is my preference.

But the 'defect' is actually on the ingress qdisc side, right, not the classifier itself ... so if we do this in the classifier, we add two extra branches to the output path, which would never be taken.

Quoted 6 lines

Plus, other classifiers wanting to look into ethernet headers would then also need to pull from /within/ their classifier as well. What about classic BPF users?

I don't think fixing this in ingress qdisc is overdesign, but rather the better place to fix it. ;)

Strongly disagree.
1. there shouldn't be a choice at all for bpf. Because not pulling l2 means it's bug.
2. adding a flag means adding it to iproute2 with default off and making users forgetting it from time to time and have no way of knowing why their programs all of a sudden stopped working.

classic falls under the same rules. It doesn't make sense at all to run a program on packet without L2 header. It's very odd both for classic and extended programs.

Two 'if' conditions in critical path is bogus argument, since these checks would be there in ingress as well. Same critical path.

maintainer
#9 lore raw

On 04/04/2015 12:17 AM, Alexei Starovoitov wrote:
...

Quoted 2 lines

1. there shouldn't be a choice at all for bpf. Because not pulling l2 means it's bug.

Yep, correct. You would also loose context for a possible dissection, at best you only have skb->protocol.

Quoted 7 lines

2. adding a flag means adding it to iproute2 with default off and making users forgetting it from time to time and have no way of knowing why their programs all of a sudden stopped working.

classic falls under the same rules. It doesn't make sense at all to run a program on packet without L2 header. It's very odd both for classic and extended programs.

Yep.

Quoted 2 lines

Two 'if' conditions in critical path is bogus argument, since these checks would be there in ingress as well. Same critical path.

Why bogus? There would be no such test on the normal egress path, where this is irrelevant. I wasn't talking about ingress here.

I see the point regarding the user option. So, why not adding a flag to tcf_proto_ops a la .flags = CLS_REQUIRES_L2 that gets propagated to tcf_proto, and only ingress_enqueue() would need to test if the classifier imposes that requirement, so it can push/pull.

#11 lore raw
In reply to a message not in the archive
Quoted 21 lines · Daniel Borkmann

On 04/04/2015 12:17 AM, Alexei Starovoitov wrote:
...

Quoted 2 lines

1. there shouldn't be a choice at all for bpf. Because not pulling l2 means it's bug.

Yep, correct. You would also loose context for a possible dissection, at best you only have skb->protocol.

Quoted 7 lines

2. adding a flag means adding it to iproute2 with default off and making users forgetting it from time to time and have no way of knowing why their programs all of a sudden stopped working.

classic falls under the same rules. It doesn't make sense at all to run a program on packet without L2 header. It's very odd both for classic and extended programs.

Yep.

Quoted 2 lines

Two 'if' conditions in critical path is bogus argument, since these checks would be there in ingress as well. Same critical path.

Quoted 7 lines

Why bogus? There would be no such test on the normal egress path, where this is irrelevant. I wasn't talking about ingress here.

I see the point regarding the user option. So, why not adding a flag to tcf_proto_ops a la .flags = CLS_REQUIRES_L2 that gets propagated to tcf_proto, and only ingress_enqueue() would need to test if the classifier imposes that requirement, so it can push/pull.

ok. that sounds better, but neither tcf_proto nor tcf_proto_ops have 'flags' field today... well, I guess it's time to add flags there. Probably add 'flags' to tcf_proto_ops only and do fl->ops->flags in ingress_enqueue()?

Will respin.

#12 lore raw
Quoted 30 lines · Alexei Starovoitov
Quoted 21 lines · Daniel Borkmann

On 04/04/2015 12:17 AM, Alexei Starovoitov wrote:
...

Quoted 2 lines

1. there shouldn't be a choice at all for bpf. Because not pulling l2 means it's bug.

Yep, correct. You would also loose context for a possible dissection, at best you only have skb->protocol.

Quoted 7 lines

2. adding a flag means adding it to iproute2 with default off and making users forgetting it from time to time and have no way of knowing why their programs all of a sudden stopped working.

classic falls under the same rules. It doesn't make sense at all to run a program on packet without L2 header. It's very odd both for classic and extended programs.

Yep.

Quoted 2 lines

Two 'if' conditions in critical path is bogus argument, since these checks would be there in ingress as well. Same critical path.

Quoted 7 lines

Why bogus? There would be no such test on the normal egress path, where this is irrelevant. I wasn't talking about ingress here.

I see the point regarding the user option. So, why not adding a flag to tcf_proto_ops a la .flags = CLS_REQUIRES_L2 that gets propagated to tcf_proto, and only ingress_enqueue() would need to test if the classifier imposes that requirement, so it can push/pull.

Quoted 6 lines

ok. that sounds better, but neither tcf_proto nor tcf_proto_ops have 'flags' field today... well, I guess it's time to add flags there. Probably add 'flags' to tcf_proto_ops only and do fl->ops->flags in ingress_enqueue()?

Will respin.

nope. will take it back.
that doesn't work, since this check cannot be done in ingress_enqueue(), because it sees the pointer to first filter only, so both TCQ_F_INGRESS flag and CLS_REQUIRES_L2 flag need to be checked inside tc_classify_compat() which is a lot worse than my current patch.

So I prefer this patch still :)

#14 lore raw
In reply to a message not in the archive
Quoted 2 lines · Alexei Starovoitov
Quoted 1 line · Alexei Starovoitov

On 4/3/15 3:54 PM, Daniel Borkmann wrote:

...

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

I see the point regarding the user option. So, why not adding a flag to tcf_proto_ops a la .flags = CLS_REQUIRES_L2 that gets propagated to tcf_proto, and only ingress_enqueue() would need to test if the classifier imposes that requirement, so it can push/pull.

ok. that sounds better, but neither tcf_proto nor tcf_proto_ops have 'flags' field today... well, I guess it's time to add flags there.

I don't think it would be a big problem.

Quoted 2 lines
Quoted 2 lines

Probably add 'flags' to tcf_proto_ops only and do fl->ops->flags in ingress_enqueue()?

Something along that line, yeah.

Quoted 6 lines
Quoted 1 line

Will respin.

nope. will take it back.
that doesn't work, since this check cannot be done in ingress_enqueue(), because it sees the pointer to first filter only, so both TCQ_F_INGRESS flag and CLS_REQUIRES_L2 flag need to be checked inside

So on a quick glance, we're calling into cls_bpf_classify() in tp->classify() (net/sched/cls_api.c +265), so all remaining filters in that list we're traversing in cls_bpf_classify() are all BPF filters, no?

Have to grab some sleep for now, will be on travel tomorrow. Anyway, worst case it could still be refactored later.

maintainer
#15 lore raw
Quoted 1 line · Daniel Borkmann

On 04/04/2015 01:11 AM, Alexei Starovoitov wrote:

...

Quoted 8 lines
Quoted 4 lines

nope. will take it back.
that doesn't work, since this check cannot be done in ingress_enqueue(), because it sees the pointer to first filter only, so both TCQ_F_INGRESS flag and CLS_REQUIRES_L2 flag need to be checked inside

So on a quick glance, we're calling into cls_bpf_classify() in tp->classify() (net/sched/cls_api.c +265), so all remaining filters in that list we're traversing in cls_bpf_classify() are all BPF filters, no?

I see, you mean the classifier chain, not the chain of filters within the cls_bpf classifier, ok.

Quoted 2 lines

Have to grab some sleep for now, will be on travel tomorrow. Anyway, worst case it could still be refactored later.

#16 lore raw
Quoted 8 lines · Daniel Borkmann
Quoted 1 line · Daniel Borkmann

On 04/04/2015 01:11 AM, Alexei Starovoitov wrote:

...

Quoted 5 lines
Quoted 4 lines

nope. will take it back.
that doesn't work, since this check cannot be done in ingress_enqueue(), because it sees the pointer to first filter only, so both TCQ_F_INGRESS flag and CLS_REQUIRES_L2 flag need to be checked inside

Quoted 7 lines
Quoted 4 lines

So on a quick glance, we're calling into cls_bpf_classify() in tp->classify()
(net/sched/cls_api.c +265), so all remaining filters in that list we're traversing in cls_bpf_classify() are all BPF filters, no?

I see, you mean the classifier chain, not the chain of filters within the cls_bpf classifier, ok.

yes. the chain of classifiers can have different types, so we cannot check it once in ingress_enqueue().
As you said we can refactor it later.

#18 lore raw
In reply to a message not in the archive
Quoted 14 lines · Alexei Starovoitov
Quoted 13 lines · Daniel Borkmann
Quoted 1 line · Daniel Borkmann

On 04/04/2015 01:11 AM, Alexei Starovoitov wrote:

...

Quoted 9 lines
Quoted 4 lines

nope. will take it back.
that doesn't work, since this check cannot be done in ingress_enqueue(), because it sees the pointer to first filter only, so both TCQ_F_INGRESS flag and CLS_REQUIRES_L2 flag need to be checked inside

So on a quick glance, we're calling into cls_bpf_classify() in tp->classify()
(net/sched/cls_api.c +265), so all remaining filters in that list we're traversing in cls_bpf_classify() are all BPF filters, no?

Quoted 6 lines
Quoted 2 lines

I see, you mean the classifier chain, not the chain of filters within the cls_bpf classifier, ok.

yes. the chain of classifiers can have different types, so we cannot check it once in ingress_enqueue().
As you said we can refactor it later.

Too many indirections. :/ Yes, I'm fine with that, thanks.

#19 lore raw
In reply to a message not in the archive

From: Alexei Starovoitov <ast-uqk4Ao+rVK5Wk0Htik3J/[email protected]>
Date: Fri, 3 Apr 2015 14:16:24 -0700

Quoted 3 lines
net/sched/cls_bpf.c +3 -0
+		if (skb_headroom(skb) < ETH_HLEN)
+			return -1;
+		__skb_push(skb, ETH_HLEN);

...

net/sched/cls_bpf.c static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp, · line 96

Quoted 2 lines
net/sched/cls_bpf.c +2 -0
+	if (tp->q->flags & TCQ_F_INGRESS)
+		__skb_pull(skb, ETH_HLEN);

Please use the actual device's L2 header length, via dev->hard_header_len, rather than hard coding ethernet.