bpf: introduce bpf_skb_vlan_push/pop() helpers
Subsystems (10)
v2 cover bpf: introduce bpf_skb_vlan_push/pop() helpers
Let TC+eBPF programs call skb_vlan_push/pop via helpers.
v1->v2:
- reworded commit log to better explain correctness of re-caching and fixed comparison of mixed endiannes (suggested by Eric)
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 | 50 +++++++++++++++++++++ 8 files changed, 197 insertions(+), 41 deletions(-)
Signature · 1 line
1.7.9.5
PATCH v2 1/2 bpf: introduce bpf_skb_vlan_push/pop() helpers
Allow eBPF programs attached to TC qdiscs call skb_vlan_push/pop via helper functions. These functions may change skb->data/hlen which are cached by some JITs to improve performance of ld_abs/ld_ind instructions. Therefore JITs need to recognize bpf_skb_vlan_push/pop() calls, re-compute header len and re-cache skb->data/hlen back into cpu registers. Note, skb->data/hlen are not directly accessible from the programs, so any changes to skb->data done either by these helpers or by other TC actions are safe.
eBPF JIT supported by three architectures:
- arm64 JIT is using bpf_load_pointer() without caching, so it's ok as-is.
- x64 JIT re-caches skb->data/hlen unconditionally after vlan_push/pop 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).
These helpers allow more scalable handling of vlan from the programs. Instead of creating thousands of vlan netdevs on top of eth0 and attaching TC+ingress+bpf to all of them, the program can be attached to eth0 directly and manipulate vlans as necessary.
--- 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 | 48 +++++++++++++++++++++++++ 6 files changed, 99 insertions(+), 38 deletions(-)
arch/s390/net/bpf_jit_comp.c +4 -0
@@ -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
@@ -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
@@ -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
@@ -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 +48 -0
60 lines not on this page · raw message
Signature · 1 line
1.7.9.5
PATCH v2 2/2 test_bpf: add bpf_skb_vlan_push/pop() tests
In reply to Alexei Starovoitovimprove 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
@@ -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
@@ -1457,6 +1457,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) {
@@ -1471,6 +1472,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
David Miller
maintainerFrom: Alexei Starovoitov <[email protected]>
Date: Mon, 20 Jul 2015 20:34:17 -0700
Quoted 5 lines
Let TC+eBPF programs call skb_vlan_push/pop via helpers.
v1->v2:
- reworded commit log to better explain correctness of re-caching and fixed comparison of mixed endiannes (suggested by Eric)
Series applied, thanks Alexei.