Skip to contentSkip to conversation

bpf: allow BPF programs access 'protocol' and 'vlan_tci' fields

PATCH Applied Alexei Starovoitov 4 participants 5 messages Mar 17, 2015 2 messages not in the archive

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

PATCH 1/1 bpf: allow BPF programs access 'protocol' and 'vlan_tci' fields

as a follow on to patch 70006af95515 ("bpf: allow eBPF access skb fields") this patch allows 'protocol' and 'vlan_tci' fields to be accessible from extended BPF programs.

The usage of 'protocol', 'vlan_present' and 'vlan_tci' fields is the same as corresponding SKF_AD_PROTOCOL, SKF_AD_VLAN_TAG_PRESENT and SKF_AD_VLAN_TAG accesses in classic BPF.

---

1.
I was thinking to drop ntohs() from 'protocol' field for extended BPF, since the programs could do:
if (skb->protocol == htons(ETH_P_IP))
which would have saved one or two cpu cycles.
But having similar behavior between classic and extended seems to be better.

2.
'vlan_tci' name is picked to match real sk_buff->vlan_tci field and matches tpacket's tp_vlan_tci field.

 include/uapi/linux/bpf.h    |    3 ++
 net/core/filter.c           |   72 ++++++++++++++++++++++++++++++-------------
 samples/bpf/test_verifier.c |    9 ++++++
 3 files changed, 62 insertions(+), 22 deletions(-)
include/uapi/linux/bpf.h +3 -0
index 929545a27546..1623047af463 100644
@@ -178,6 +178,9 @@ struct __sk_buff {
 	__u32 pkt_type;
 	__u32 mark;
 	__u32 queue_mapping;
+	__u32 protocol;
+	__u32 vlan_present;
+	__u32 vlan_tci;
 };
 
 #endif /* _UAPI__LINUX_BPF_H__ */
net/core/filter.c +50 -22

97 lines not on this page · raw message

samples/bpf/test_verifier.c +9 -0
index df6dbb6576f6..75d561f9fd6a 100644
@@ -658,6 +658,15 @@ static struct bpf_test tests[] = {
 			BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
 				    offsetof(struct __sk_buff, queue_mapping)),
 			BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 0),
+			BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
+				    offsetof(struct __sk_buff, protocol)),
+			BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 0),
+			BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
+				    offsetof(struct __sk_buff, vlan_present)),
+			BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 0),
+			BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
+				    offsetof(struct __sk_buff, vlan_tci)),
+			BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 0),
 			BPF_EXIT_INSN(),
 		},
 		.result = ACCEPT,
Signature · 1 line
1.7.9.5
#2 lore raw
In reply to a message not in the archive
Quoted 9 lines · Alexei Starovoitov

as a follow on to patch 70006af95515 ("bpf: allow eBPF access skb fields") this patch allows 'protocol' and 'vlan_tci' fields to be accessible from extended BPF programs.

The usage of 'protocol', 'vlan_present' and 'vlan_tci' fields is the same as corresponding SKF_AD_PROTOCOL, SKF_AD_VLAN_TAG_PRESENT and SKF_AD_VLAN_TAG accesses in classic BPF.

Ok, code looks good to me.

Quoted 6 lines

1.
I was thinking to drop ntohs() from 'protocol' field for extended BPF, since the programs could do:
if (skb->protocol == htons(ETH_P_IP))
which would have saved one or two cpu cycles.
But having similar behavior between classic and extended seems to be better.

I'm thinking that skb->protocol == htons(ETH_P_IP) might actually be more obvious, and, as you mentioned, the compiler can already resolve the htons() during compile time instead of runtime, which would be another plus.

Either behavior we should document later anyway.

The question to me here is, do we need to keep similar behavior?

After all, the way of programming both from a user perspective is quite different (i.e. bpf_asm versus C/LLVM).

Similarly, I was wondering, if just exporting raw skb->vlan_tci is already sufficient, and the user can e.g. write helpers to extract bits himself from that protocol field?

Quoted 3 lines

2.
'vlan_tci' name is picked to match real sk_buff->vlan_tci field and matches tpacket's tp_vlan_tci field.

#3 lore raw
Quoted 30 lines · Daniel Borkmann
Quoted 5 lines · Alexei Starovoitov

as a follow on to patch 70006af95515 ("bpf: allow eBPF access skb fields")
this patch allows 'protocol' and 'vlan_tci' fields to be accessible from extended BPF programs.

Quoted 7 lines

The usage of 'protocol', 'vlan_present' and 'vlan_tci' fields is the same as
corresponding SKF_AD_PROTOCOL, SKF_AD_VLAN_TAG_PRESENT and SKF_AD_VLAN_TAG
accesses in classic BPF.

Ok, code looks good to me.

Quoted 8 lines

1.
I was thinking to drop ntohs() from 'protocol' field for extended BPF, since
the programs could do:
if (skb->protocol == htons(ETH_P_IP))
which would have saved one or two cpu cycles.
But having similar behavior between classic and extended seems to be better.

I'm thinking that skb->protocol == htons(ETH_P_IP) might actually be more obvious, and, as you mentioned, the compiler can already resolve the htons() during compile time instead of runtime, which would be another plus.

Quoted 6 lines

Either behavior we should document later anyway.

The question to me here is, do we need to keep similar behavior?

After all, the way of programming both from a user perspective is quite different (i.e. bpf_asm versus C/LLVM).

yeah. we don't have to. Somehow I felt that keeping ntohs will make it easier for folks moving from classic to extended, but I guess they're different enough, so no point wasting run time cycles.

Quoted 3 lines

Similarly, I was wondering, if just exporting raw skb->vlan_tci is already sufficient, and the user can e.g. write helpers to extract bits himself from that protocol field?

yes. I thought about the same. Currently VLAN_TAG_PRESENT bit is not officially exposed to user space, but implicitly, since that bit is always cleared when we return tci to user space and it's always set when drivers indicate that vlan header was present in the packet. So I think we can return skb->vlan_tci as-is, since it will save one load in bpf program which will be able to do
if (skb->vlan_tci != 0) /* vlan header is present */

     vid = skb->vlan_tci & 0x0fff;

compiler will optimize above two accesses into single load and will reuse the register in 2nd line.

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

On 03/17/2015 02:06 AM, Alexei Starovoitov wrote:

...

Quoted 15 lines
Quoted 15 lines
Quoted 7 lines

I was thinking to drop ntohs() from 'protocol' field for extended BPF, since
the programs could do:
if (skb->protocol == htons(ETH_P_IP))
which would have saved one or two cpu cycles.
But having similar behavior between classic and extended seems to be better.

I'm thinking that skb->protocol == htons(ETH_P_IP) might actually be more obvious, and, as you mentioned, the compiler can already resolve the htons() during compile time instead of runtime, which would be another plus.

Either behavior we should document later anyway.

Quoted 8 lines
Quoted 4 lines

The question to me here is, do we need to keep similar behavior?

After all, the way of programming both from a user perspective is quite different (i.e. bpf_asm versus C/LLVM).

yeah. we don't have to. Somehow I felt that keeping ntohs will make it easier for folks moving from classic to extended, but I guess they're different enough, so no point wasting run time cycles.

Yes, I think that case seems reasonable in my opinion.

Quoted 8 lines
Quoted 3 lines

Similarly, I was wondering, if just exporting raw skb->vlan_tci is already sufficient, and the user can e.g. write helpers to extract bits himself from that protocol field?

yes. I thought about the same. Currently VLAN_TAG_PRESENT bit is not officially exposed to user space, but implicitly, since that bit is always cleared when we return tci to user space and it's always set when drivers indicate that vlan header was present in the packet.

Right.

Quoted 6 lines

So I think we can return skb->vlan_tci as-is, since it will save one load in bpf program which will be able to do
if (skb->vlan_tci != 0) /* vlan header is present */

      vid = skb->vlan_tci & 0x0fff;

compiler will optimize above two accesses into single load and will reuse the register in 2nd line.

Ok, I'm not sure what's best in the vlan_tci case. I think both options are a possible way to move forward. If the compiler can further optimize the latter, it might be the better option. I'll leave that to you. ;)

Thanks,
Daniel

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

From: Alexei Starovoitov <ast-uqk4Ao+rVK5Wk0Htik3J/[email protected]>
Date: Mon, 16 Mar 2015 18:06:02 -0700

Quoted 9 lines

as a follow on to patch 70006af95515 ("bpf: allow eBPF access skb fields") this patch allows 'protocol' and 'vlan_tci' fields to be accessible from extended BPF programs.

The usage of 'protocol', 'vlan_present' and 'vlan_tci' fields is the same as corresponding SKF_AD_PROTOCOL, SKF_AD_VLAN_TAG_PRESENT and SKF_AD_VLAN_TAG accesses in classic BPF.

Applied, thanks.