Skip to contentSkip to conversation

bpf: introduce bpf_skb_vlan_push/pop() helpers

PATCH Superseded Alexei Starovoitov 2 participants 5 messages Jul 17, 2015

Subsystems (10)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover bpf: introduce bpf_skb_vlan_push/pop() helpers

Let eBPF programs call skb_vlan_push/pop via helpers. JIT changes to accomodate of re-caching of skb->data and headerlen turned out to be pretty simple.

This patch is on top of Daniel's bpf_get_cgroup_classid():
http://patchwork.ozlabs.org/patch/495826/
to avoid merge conflicts.

Alexei Starovoitov (2):
bpf: introduce bpf_skb_vlan_push/pop() helpers
test_bpf: add bpf_skb_vlan_push/pop() tests

 arch/s390/net/bpf_jit_comp.c |    4 ++
 arch/x86/net/bpf_jit_comp.c  |   80 ++++++++++++++++++----------------
 include/linux/bpf.h          |    2 +
 include/linux/filter.h       |    1 +
 include/uapi/linux/bpf.h     |    2 +
 kernel/bpf/core.c            |    1 +
 lib/test_bpf.c               |   98 ++++++++++++++++++++++++++++++++++++++++--
 net/core/filter.c            |   49 +++++++++++++++++++++
 8 files changed, 196 insertions(+), 41 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/2 bpf: introduce bpf_skb_vlan_push/pop() helpers

In order to let eBPF programs call skb_vlan_push/pop via helper functions eBPF JITs need to recognize helpers that change skb->data, since skb->data and hlen are cached as part of JIT code generation.
- arm64 JIT is using bpf_load_pointer() without caching, so it's ok as-is.
- x64 JIT recognizes bpf_skb_vlan_push/pop() calls and re-caches skb->data/hlen after such calls (experiments showed that conditional re-caching is slower).
- s390 JIT falls back to interpreter for now when bpf_skb_vlan_push() is present in the program (re-caching is tbd).

---
with these helpers the phys2virt gateway I showed at nfws can be done in scalable way without creating a ton of vlan netdevs.

 arch/s390/net/bpf_jit_comp.c |    4 +++
 arch/x86/net/bpf_jit_comp.c  |   80 ++++++++++++++++++++++--------------------
 include/linux/bpf.h          |    2 ++
 include/linux/filter.h       |    1 +
 include/uapi/linux/bpf.h     |    2 ++
 net/core/filter.c            |   47 +++++++++++++++++++++++++
 6 files changed, 98 insertions(+), 38 deletions(-)
arch/s390/net/bpf_jit_comp.c +4 -0
index fee782acc2ee..79c731e8d178 100644
@@ -973,6 +973,10 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp, int i
 		 */
 		const u64 func = (u64)__bpf_call_base + imm;
 
+		if (bpf_helper_changes_skb_data((void *)func))
+			/* TODO reload skb->data, hlen */
+			return -1;
+
 		REG_SET_SEEN(BPF_REG_5);
 		jit->seen |= SEEN_FUNC;
 		/* lg %w1,<d(imm)>(%l) */
arch/x86/net/bpf_jit_comp.c +42 -38

110 lines not on this page · raw message

include/linux/bpf.h +2 -0
index 4383476a0d48..139d6d2e123f 100644
@@ -192,5 +192,7 @@ extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
 extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
 extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
 extern const struct bpf_func_proto bpf_get_current_comm_proto;
+extern const struct bpf_func_proto bpf_skb_vlan_push_proto;
+extern const struct bpf_func_proto bpf_skb_vlan_pop_proto;
 
 #endif /* _LINUX_BPF_H */
include/linux/filter.h +1 -0
index 17724f6ea983..69d00555ce35 100644
@@ -411,6 +411,7 @@ void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp);
 
 u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
 void bpf_int_jit_compile(struct bpf_prog *fp);
+bool bpf_helper_changes_skb_data(void *func);
 
 #ifdef CONFIG_BPF_JIT
 typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
include/uapi/linux/bpf.h +2 -0
index 2de87e58b12b..2f6c83d714e9 100644
@@ -256,6 +256,8 @@ enum bpf_func_id {
 	 * Return: classid if != 0
 	 */
 	BPF_FUNC_get_cgroup_classid,
+	BPF_FUNC_skb_vlan_push, /* bpf_skb_vlan_push(skb, vlan_proto, vlan_tci) */
+	BPF_FUNC_skb_vlan_pop,  /* bpf_skb_vlan_pop(skb) */
 	__BPF_FUNC_MAX_ID,
 };
 
net/core/filter.c +47 -0

59 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#2 lore raw
Quoted 1 line · Alexei Starovoitov

In order to let eBPF programs call skb_vlan_push/pop via helper functions

Why should eBPF program do such thing ?

Are BPF users in the kernel expecting skb being changed, and are we sure they reload all cached values when/if needed ?

Quoted 10 lines

eBPF JITs need to recognize helpers that change skb->data, since skb->data and hlen are cached as part of JIT code generation.
- arm64 JIT is using bpf_load_pointer() without caching, so it's ok as-is.
- x64 JIT recognizes bpf_skb_vlan_push/pop() calls and re-caches skb->data/hlen after such calls (experiments showed that conditional re-caching is slower).
- s390 JIT falls back to interpreter for now when bpf_skb_vlan_push() is present in the program (re-caching is tbd).

net/core/filter.c static const struct bpf_func_proto bpf_get_cgroup_classid_proto = { · line 1446

Quoted 7 lines
net/core/filter.c +7 -0
+static u64 bpf_skb_vlan_push(u64 r1, u64 vlan_proto, u64 vlan_tci, u64 r4, u64 r5)
+{
+	struct sk_buff *skb = (struct sk_buff *) (long) r1;
+
+	if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
+		     vlan_proto != htons(ETH_P_8021AD)))
+		vlan_proto = htons(ETH_P_8021Q);

This would raise sparse error, as vlan_proto is u64, and htons() __be16

make C=2 CF=-D__CHECK_ENDIAN__ net/core/filter.o

#3 lore raw
Quoted 7 lines · Eric Dumazet
Quoted 1 line · Alexei Starovoitov

In order to let eBPF programs call skb_vlan_push/pop via helper functions

Why should eBPF program do such thing ?

Are BPF users in the kernel expecting skb being changed, and are we sure they reload all cached values when/if needed ?

well, classic BPF and even extended BPF with socket filters cannot use these helpers. They are for TC ingress/egress only. There different actions can already change skb->data while classifiers/actions are running. btw, before we started discussing this topic at nfws, I thought that bpf programs will never be able to change skb->data from inside the programs, but turned out it's only JITs that needed re-caching. Programs cannot see skb->data. They can access packet only via ld_abs/ld_ind instructions and helper functions.
So any changes to internal fields of skb are invisible to programs. skb->data/hlen cache that is part of JIT is also invisible to the programs. It's an optimization that some JITs do. arm64 JIT doesn't do this optimization, for example.
I'll reword commit log to better explain this.

One of the use cases is this phys2virt gateway I presented. There I need to do vlan-learning and src mac forwarding. Currently I'm creating as many as I can vlan netdevs on top of regular eth0 and attach tc+bpf to all of them. That's very inefficient. With these helpers I'll attach tc+bpf to eth0 only and skip creation of thousands of vlan netdevs.

Quoted 21 lines
Quoted 10 lines

eBPF JITs need to recognize helpers that change skb->data, since skb->data and hlen are cached as part of JIT code generation.
- arm64 JIT is using bpf_load_pointer() without caching, so it's ok as-is.
- x64 JIT recognizes bpf_skb_vlan_push/pop() calls and re-caches skb->data/hlen after such calls (experiments showed that conditional re-caching is slower).
- s390 JIT falls back to interpreter for now when bpf_skb_vlan_push() is present in the program (re-caching is tbd).

net/core/filter.c static const struct bpf_func_proto bpf_get_cgroup_classid_proto = { · line 1446

Quoted 7 lines
net/core/filter.c +7 -0
+static u64 bpf_skb_vlan_push(u64 r1, u64 vlan_proto, u64 vlan_tci, u64 r4, u64 r5)
+{
+	struct sk_buff *skb = (struct sk_buff *) (long) r1;
+
+	if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
+		     vlan_proto != htons(ETH_P_8021AD)))
+		vlan_proto = htons(ETH_P_8021Q);

This would raise sparse error, as vlan_proto is u64, and htons() __be16

Quoted 1 line

make C=2 CF=-D__CHECK_ENDIAN__ net/core/filter.o

yes.
When I wrote these lines I thought of the same, so I did run the sparse and it spewed a lot of false positives and stopped on 'too many errors' before reaching these lines.
So I downloaded the latest sparse, hacked it a little and tried again. Still it didn't complain about the endianness. That was puzzling, so I left the above lines as-is.
but since your eagle eyes caught it, I will add casts :)

#4 lore raw

PATCH 2/2 test_bpf: add bpf_skb_vlan_push/pop() tests

In reply to Alexei Starovoitov

improve accuracy of timing in test_bpf and add two stress tests:
- {skb->data[0], get_smp_processor_id} repeated 2k times
- {skb->data[0], vlan_push} x 68 followed by {skb->data[0], vlan_pop} x 68

1st test is useful to test performance of JIT implementation of BPF_LD_ABS together with BPF_CALL instructions.
2nd test is stressing skb_vlan_push/pop logic together with skb->data access via BPF_LD_ABS insn which checks that re-caching of skb->data is done correctly.

In order to call bpf_skb_vlan_push() from test_bpf.ko have to add three export_symbol_gpl.

---
 kernel/bpf/core.c |    1 +
 lib/test_bpf.c    |   98 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 net/core/filter.c |    2 ++
 3 files changed, 98 insertions(+), 3 deletions(-)
kernel/bpf/core.c +1 -0
index bf38f5e8196c..fafa74161445 100644
@@ -177,6 +177,7 @@ noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
 {
 	return 0;
 }
+EXPORT_SYMBOL_GPL(__bpf_call_base);
 
 /**
  *	__bpf_prog_run - run eBPF program on a given context
lib/test_bpf.c +95 -3

127 lines not on this page · raw message

net/core/filter.c +2 -0
index 3683ab09a48a..d8eb7e811aa3 100644
@@ -1456,6 +1456,7 @@ const struct bpf_func_proto bpf_skb_vlan_push_proto = {
 	.arg2_type      = ARG_ANYTHING,
 	.arg3_type      = ARG_ANYTHING,
 };
+EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
 
 static u64 bpf_skb_vlan_pop(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
 {
@@ -1470,6 +1471,7 @@ const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
 	.ret_type       = RET_INTEGER,
 	.arg1_type      = ARG_PTR_TO_CTX,
 };
+EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
 
 bool bpf_helper_changes_skb_data(void *func)
 {
Signature · 1 line
1.7.9.5