bpf: introduce bpf_tail_call() helper
Subsystems (11)
cover bpf: introduce bpf_tail_call() helper
Hi All,
introduce bpf_tail_call(ctx, &jmp_table, index) helper function which can be used from BPF programs like:
int bpf_prog(struct pt_regs *ctx)
{
...
bpf_tail_call(ctx, &jmp_table, index);
...
}
that is roughly equivalent to:
int bpf_prog(struct pt_regs *ctx)
{
...
if (jmp_table[index])
return (*jmp_table[index])(ctx);
...
}
The important detail that it's not a normal call, but a tail call. The kernel stack is precious, so this helper reuses the current stack frame and jumps into another BPF program without adding extra call frame.
It's trivially done in interpreter and a bit trickier in JITs.
Use cases:
- simplify complex programs
- dispatch into other programs
(for example: index in jump table can be syscall number or network protocol)
- build dynamic chains of programs
The chain of tail calls can form unpredictable dynamic loops therefore tail_call_cnt is used to limit the number of calls and currently is set to 32.
patch 1 - support bpf_tail_call() in interpreter
patch 2 - support in x64 JIT
We've discussed what's neccessary to support it in arm64/s390 JITs and it looks fine.
patch 3 - sample example for tracing
patch 4 - sample example for networking
More details in every patch.
This set went through several iterations of reviews/fixes and older attempts can be seen:
https://git.kernel.org/cgit/linux/kernel/git/ast/bpf.git/log/?h=tail_call_v[123456]
- tail_call_v1 does it without touching JITs but introduces overhead for all programs that don't use this helper function.
- tail_call_v2 still has some overhead and x64 JIT does full stack unwind (prologue skipping optimization wasn't there)
- tail_call_v3 reuses 'call' instruction encoding and has interpreter overhead for every normal call
- tail_call_v4 fixes above architectural shortcomings and v5,v6 fix few more bugs
This last tail_call_v6 approach seems to be the best.
Alexei Starovoitov (4):
bpf: allow bpf programs to tail-call other bpf programs
x86: bpf_jit: implement bpf_tail_call() helper samples/bpf: bpf_tail_call example for tracing samples/bpf: bpf_tail_call example for networking
arch/x86/net/bpf_jit_comp.c | 150 +++++++++++++++++---- include/linux/bpf.h | 22 ++++ include/linux/filter.h | 2 +- include/uapi/linux/bpf.h | 10 ++ kernel/bpf/arraymap.c | 113 +++++++++++++++- kernel/bpf/core.c | 73 ++++++++++- kernel/bpf/syscall.c | 23 +++- kernel/bpf/verifier.c | 17 +++ kernel/trace/bpf_trace.c | 2 + net/core/filter.c | 2 + samples/bpf/Makefile | 8 ++ samples/bpf/bpf_helpers.h | 4 + samples/bpf/bpf_load.c | 57 ++++++-- samples/bpf/sockex3_kern.c | 303 +++++++++++++++++++++++++++++++++++++++++++ samples/bpf/sockex3_user.c | 66 ++++++++++ samples/bpf/tracex5_kern.c | 75 +++++++++++ samples/bpf/tracex5_user.c | 46 +++++++ 17 files changed, 928 insertions(+), 45 deletions(-) create mode 100644 samples/bpf/sockex3_kern.c create mode 100644 samples/bpf/sockex3_user.c create mode 100644 samples/bpf/tracex5_kern.c create mode 100644 samples/bpf/tracex5_user.c
Signature · 1 line
1.7.9.5
PATCH 1/4 bpf: allow bpf programs to tail-call other bpf programs
introduce bpf_tail_call(ctx, &jmp_table, index) helper function which can be used from BPF programs like:
int bpf_prog(struct pt_regs *ctx)
{
...
bpf_tail_call(ctx, &jmp_table, index);
...
}
that is roughly equivalent to:
int bpf_prog(struct pt_regs *ctx)
{
...
if (jmp_table[index])
return (*jmp_table[index])(ctx);
...
}
The important detail that it's not a normal call, but a tail call. The kernel stack is precious, so this helper reuses the current stack frame and jumps into another BPF program without adding extra call frame.
It's trivially done in interpreter and a bit trickier in JITs. In case of x64 JIT the bigger part of generated assembler prologue is common for all programs, so it is simply skipped while jumping. Other JITs can do similar prologue-skipping optimization or do stack unwind before jumping into the next program.
bpf_tail_call() arguments:
ctx - context pointerjmp_table - one of BPF_MAP_TYPE_PROG_ARRAY maps used as the jump table index - index in the jump table
Since all BPF programs are idenitified by file descriptor, user space need to populate the jmp_table with FDs of other BPF programs. If jmp_table[index] is empty the bpf_tail_call() doesn't jump anywhere and program execution continues as normal.
New BPF_MAP_TYPE_PROG_ARRAY map type is introduced so that user space can populate this jmp_table array with FDs of other bpf programs. Programs can share the same jmp_table array or use multiple jmp_tables.
The chain of tail calls can form unpredictable dynamic loops therefore tail_call_cnt is used to limit the number of calls and currently is set to 32.
Use cases:
==========
- simplify complex programs by splitting them into a sequence of small programs
- dispatch routine
For tracing and future seccomp the program may be triggered on all system calls, but processing of syscall arguments will be different. It's more efficient to implement them as:
int syscall_entry(struct seccomp_data *ctx)
{
bpf_tail_call(ctx, &syscall_jmp_table, ctx->nr /* syscall number */); ... default: process unknown syscall ...
}
int sys_write_event(struct seccomp_data *ctx) {...}
int sys_read_event(struct seccomp_data *ctx) {...} syscall_jmp_table[__NR_write] = sys_write_event; syscall_jmp_table[__NR_read] = sys_read_event;
For networking the program may call into different parsers depending on
packet format, like:
int packet_parser(struct __sk_buff *skb)
{
... parse L2, L3 here ...
__u8 ipproto = load_byte(skb, ... offsetof(struct iphdr, protocol));
bpf_tail_call(skb, &ipproto_jmp_table, ipproto);
... default: process unknown protocol ...
}
int parse_tcp(struct __sk_buff *skb) {...}
int parse_udp(struct __sk_buff *skb) {...}
ipproto_jmp_table[IPPROTO_TCP] = parse_tcp;
ipproto_jmp_table[IPPROTO_UDP] = parse_udp;- for TC use case, bpf_tail_call() allows to implement reclassify-like logic
- bpf_map_update_elem/delete calls into BPF_MAP_TYPE_PROG_ARRAY jump table are atomic, so user space can build chains of BPF programs on the fly
Implementation details:
=======================
- high performance of bpf_tail_call() is the goal.
It could have been implemented without JIT changes as a wrapper on top of BPF_PROG_RUN() macro, but with two downsides:
. all programs would have to pay performance penalty for this feature and
tail call itself would be slower, since mandatory stack unwind, return, stack allocate would be done for every tailcall.
. tailcall would be limited to programs running preempt_disabled, since
generic 'void *ctx' doesn't have room for 'tail_call_cnt' and it would need to be either global per_cpu variable accessed by helper and by wrapper or global variable protected by locks.
In this implementation x64 JIT bypasses stack unwind and jumps into the callee program after prologue.
- bpf_prog_array_compatible() ensures that prog_type of callee and caller are the same and JITed/non-JITed flag is the same, since calling JITed program from non-JITed is invalid, since stack frames are different. Similarly calling kprobe type program from socket type program is invalid.
- jump table is implemented as BPF_MAP_TYPE_PROG_ARRAY to reuse 'map' abstraction, its user space API and all of verifier logic. It's in the existing arraymap.c file, since several functions are shared with regular array map.
--- include/linux/bpf.h | 22 +++++++++ include/linux/filter.h | 2 +- include/uapi/linux/bpf.h | 10 ++++ kernel/bpf/arraymap.c | 113 +++++++++++++++++++++++++++++++++++++++++++--- kernel/bpf/core.c | 73 +++++++++++++++++++++++++++++- kernel/bpf/syscall.c | 23 +++++++++- kernel/bpf/verifier.c | 17 +++++++ kernel/trace/bpf_trace.c | 2 + net/core/filter.c | 2 + 9 files changed, 255 insertions(+), 9 deletions(-)
include/linux/bpf.h +22 -0
@@ -126,6 +126,27 @@ struct bpf_prog_aux {struct work_struct work; }; +struct bpf_array { +struct bpf_map map; +u32 elem_size; +/* 'ownership' of prog_array is claimed by the first program that +* is going to use this map or by the first program which FD is stored +* in the map to make sure that all callers and callees have the same +* prog_type and JITed flag +*/ +enum bpf_prog_type owner_prog_type; +bool owner_jited; +union { +char value[0 ]__aligned(8) ; +struct bpf_prog *prog[0 ]__aligned(8) ; + }; +}; +#define MAX_TAIL_CALL_CNT 32 + +u64 bpf_tail_call (u64 ctx,u64 r2,u64 index,u64 r4,u64 r5); +void bpf_prog_array_map_clear (struct bpf_map *map); +bool bpf_prog_array_compatible (struct bpf_array *array,const struct bpf_prog *fp); +#ifdef CONFIG_BPF_SYSCALL void bpf_register_prog_type (struct bpf_prog_type_list *tl);void bpf_register_map_type (struct bpf_map_type_list *tl);
@@ -160,5 +181,6 @@ extern const struct bpf_func_proto bpf_map_delete_elem_proto;extern const struct bpf_func_proto bpf_get_prandom_u32_proto;extern const struct bpf_func_proto bpf_get_smp_processor_id_proto; +extern const struct bpf_func_proto bpf_tail_call_proto;#endif /* _LINUX_BPF_H */
include/linux/filter.h +1 -1
@@ -378,7 +378,7 @@ static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)int sk_filter (struct sock *sk,struct sk_buff *skb); -void bpf_prog_select_runtime (struct bpf_prog *fp); +int bpf_prog_select_runtime (struct bpf_prog *fp);void bpf_prog_free (struct bpf_prog *fp);struct bpf_prog *bpf_prog_alloc (unsigned int size,gfp_t gfp_extra_flags);
include/uapi/linux/bpf.h +10 -0
@@ -113,6 +113,7 @@ enum bpf_map_type {BPF_MAP_TYPE_UNSPEC ,BPF_MAP_TYPE_HASH ,BPF_MAP_TYPE_ARRAY , + BPF_MAP_TYPE_PROG_ARRAY, }; enum bpf_prog_type {
@@ -210,6 +211,15 @@ enum bpf_func_id {* Return: 0 on success */ BPF_FUNC_l4_csum_replace, + +/** +* bpf_tail_call(ctx, prog_array_map, index) - jump into another BPF program +* @ctx: context pointer passed to next program +* @prog_array_map: pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY +* @index: index inside array that selects specific program to run +* Return: 0 on success +*/ + BPF_FUNC_tail_call, __BPF_FUNC_MAX_ID, };
kernel/bpf/arraymap.c +107 -6
122 lines not on this page · raw message
kernel/bpf/core.c +72 -1
115 lines not on this page · raw message
kernel/bpf/syscall.c +22 -1
@@ -68,6 +68,12 @@ static int bpf_map_release(struct inode *inode, struct file *filp) {struct bpf_map *map = filp->private_data; +if (map->map_type ==BPF_MAP_TYPE_PROG_ARRAY ) +/* prog_array stores refcnt-ed bpf_prog pointers +* release them all when user space closes prog_array_fd +*/ +bpf_prog_array_map_clear (map); +bpf_map_put (map);return 0 ; }
@@ -392,6 +398,19 @@ static void fixup_bpf_calls(struct bpf_prog *prog)*/ BUG_ON (!prog->aux->ops->get_func_proto); +if (insn->imm == BPF_FUNC_tail_call) { +/* mark bpf_tail_call as different opcode +* to avoid conditional branch in +* interpeter for every normal call +* and to prevent accidental JITing by +* JIT compiler that doesn't support +* bpf_tail_call yet +*/ + insn->imm =0 ; + insn->code |=BPF_X ; +continue ; + } + fn = prog->aux->ops->get_func_proto (insn->imm);/* all functions that have prototype and verifier allowed * programs to call them, must be real in-kernel functions
@@ -532,7 +551,9 @@ static int bpf_prog_load(union bpf_attr *attr)fixup_bpf_calls (prog);/* eBPF program is ready to be JITed */ -bpf_prog_select_runtime (prog); + err =bpf_prog_select_runtime (prog); +if (err <0 ) + goto free_used_maps; err =anon_inode_getfd ("bpf-prog" , &bpf_prog_fops, prog,O_RDWR |O_CLOEXEC );if (err <0 )
kernel/bpf/verifier.c +17 -0
@@ -907,6 +907,23 @@ static int check_call(struct verifier_env *env, int func_id) fn->ret_type, func_id);return -EINVAL ; } + +if (map && map->map_type ==BPF_MAP_TYPE_PROG_ARRAY && + func_id != BPF_FUNC_tail_call) +/* prog_array map type needs extra care: +* only allow to pass it into bpf_tail_call() for now. +* bpf_map_delete_elem() can be allowed in the future, +* while bpf_map_update_elem() must only be done via syscall +*/ +return -EINVAL ; + +if (func_id == BPF_FUNC_tail_call && + map->map_type !=BPF_MAP_TYPE_PROG_ARRAY ) +/* don't allow any other map type to be passed into +* bpf_tail_call() +*/ +return -EINVAL ; +return 0 ; }
kernel/trace/bpf_trace.c +2 -0
@@ -172,6 +172,8 @@ static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id funcreturn &bpf_probe_read_proto;case BPF_FUNC_ktime_get_ns:return &bpf_ktime_get_ns_proto; +case BPF_FUNC_tail_call: +return &bpf_tail_call_proto;case BPF_FUNC_trace_printk:/*
net/core/filter.c +2 -0
@@ -1421,6 +1421,8 @@ sk_filter_func_proto(enum bpf_func_id func_id)return &bpf_get_prandom_u32_proto;case BPF_FUNC_get_smp_processor_id:return &bpf_get_smp_processor_id_proto; +case BPF_FUNC_tail_call: +return &bpf_tail_call_proto;default :return NULL ; }
Signature · 1 line
1.7.9.5
Re: [PATCH net-next 1/4] bpf: allow bpf programs to tail-call other bpf programs
Quoted 36 lines · Alexei Starovoitov
introduce
bpf_tail_call(ctx, &jmp_table, index)helper function which can be used from BPF programs like:
intbpf_prog(struct pt_regs *ctx)
{
...
bpf_tail_call(ctx, &jmp_table, index);
...
}
that is roughly equivalent to:
intbpf_prog(struct pt_regs *ctx)
{
...
if (jmp_table[index])return (*jmp_table[index])(ctx);...
}
The important detail that it's not a normal call, but a tail call. The kernel stack is precious, so this helper reuses the current stack frame and jumps into another BPF program without adding extra call frame.
It's trivially done in interpreter and a bit trickier in JITs. In case of x64 JIT the bigger part of generated assembler prologue is common for all programs, so it is simply skipped while jumping. Other JITs can do similar prologue-skipping optimization or do stack unwind before jumping into the next program.
bpf_tail_call()arguments:
ctx - context pointerjmp_table- one ofBPF_MAP_TYPE_PROG_ARRAYmaps used as the jump table index - index in the jump tableSince all BPF programs are idenitified by file descriptor, user space need to populate the
jmp_tablewith FDs of other BPF programs. If jmp_table[index] is empty thebpf_tail_call()doesn't jump anywhere and program execution continues as normal.
Quoted 6 lines
New
BPF_MAP_TYPE_PROG_ARRAYmap type is introduced so that user space can populate thisjmp_tablearray with FDs of other bpf programs. Programs can share the samejmp_tablearray or use multiplejmp_tables.The chain of tail calls can form unpredictable dynamic loops therefore
tail_call_cntis used to limit the number of calls and currently is set to 32.
IMO this is starting to get a bit ugly. Would it be possible to have the program dereference the subprogram reference itself from the jump table? There would have to be a verifier type that represents a reference to a program tail-call entry point, but that seems better than having this weird indirection.
--Andy
Re: [PATCH net-next 1/4] bpf: allow bpf programs to tail-call other bpf programs
Quoted 6 lines · Andy Lutomirski
IMO this is starting to get a bit ugly. Would it be possible to have the program dereference the subprogram reference itself from the jump table? There would have to be a verifier type that represents a reference to a program tail-call entry point, but that seems better than having this weird indirection.
Which part? I don't think you've looked at examples yet. network parser has to call itself. Otherwise we cannot parse 10 mpls labels or TLVs.
Indirection via jump_table also has to be there.
We need to dynamically add and remove programs form this jump table. It cannot be all static.
Re: [PATCH net-next 1/4] bpf: allow bpf programs to tail-call other bpf programs
Quoted 10 lines · Alexei Starovoitov
Quoted 7 lines · Andy Lutomirski
IMO this is starting to get a bit ugly. Would it be possible to have the program dereference the subprogram reference itself from the jump table? There would have to be a verifier type that represents a reference to a program tail-call entry point, but that seems better than having this weird indirection.
Quoted 7 lines
Which part? I don't think you've looked at examples yet. network parser has to call itself. Otherwise we cannot parse 10 mpls labels or TLVs.
Indirection viajump_tablealso has to be there.
We need to dynamically add and remove programs form this jump table. It cannot be all static.
What I mean is: why do we need the interface to be "look up this index in an array and just to what it references" as a single atomic instruction? Can't we break it down into first "look up this index in an array" and then "do this tail call"?
I don't see why everything needs to be a map.
--Andy
Re: [PATCH net-next 1/4] bpf: allow bpf programs to tail-call other bpf programs
Quoted 5 lines · Andy Lutomirski
What I mean is: why do we need the interface to be "look up this index in an array and just to what it references" as a single atomic instruction? Can't we break it down into first "look up this index in an array" and then "do this tail call"?
I've actually considered to do this split and do first part as map lookup and 2nd as 'tail call to this ptr' insn, but it turned out to be
painful: verifier gets more complicated, ctx pointer needs to kept
somewhere, JITs need to special case two things instead of one.
Also I couldn't see a use case for exposing program pointer to the
program itself. I've explored this path only because it felt more
traditional 'goto *ptr' like, but adding new PTR_TO_PROG type to
verifier looked wasteful.
Quoted 1 line
I don't see why everything needs to be a map.
I mentioned the reasons to use map abstraction in the commit log:
"- jump table is implemented as BPF_MAP_TYPE_PROG_ARRAY to reuse 'map'
abstraction, its user space API and all of verifier logic. It's in the existing arraymap.c file, since several functions are shared with regular array map."
The other alternative would be to add new thing just for jump table, but it means extending syscall commands and propagating the callchain through several files plus adding all new interfaces to user space. I think 'map' abstraction fits very well. We have 'array' map which is one-to-one to normal C array. This is just different type of array that stores prog_fds.
When in C you're creating 'void *jmptable[] = { &&label1, &&label2};' it is still an array. So here you have special type PROG_ARRAY for it to make verifier recognize it.
Re: [PATCH net-next 1/4] bpf: allow bpf programs to tail-call other bpf programs
Quoted 9 lines · Alexei Starovoitov
Quoted 6 lines · Andy Lutomirski
What I mean is: why do we need the interface to be "look up this index in an array and just to what it references" as a single atomic instruction? Can't we break it down into first "look up this index in an array" and then "do this tail call"?
Quoted 8 lines
I've actually considered to do this split and do first part as map lookup and 2nd as 'tail call to this ptr' insn, but it turned out to be
painful: verifier gets more complicated, ctx pointer needs to kept somewhere, JITs need to special case two things instead of one. Also I couldn't see a use case for exposing program pointer to the program itself. I've explored this path only because it felt more traditional 'goto *ptr' like, but adding newPTR_TO_PROGtype to verifier looked wasteful.
At some point, I think that it would be worth extending the verifier to support more general non-integral scalar types. "Pointer to tail-call target" would be just one of them. "Pointer to skb" might be nice as a real first-class scalar type that lives in a register as opposed to just being magic typed context.
We'd still need some way to stick fds into a map, but that's not really the verifier's problem.
--Andy
Re: [PATCH net-next 1/4] bpf: allow bpf programs to tail-call other bpf programs
Quoted 19 lines · Andy Lutomirski
Quoted 9 lines · Alexei Starovoitov
Quoted 6 lines · Andy Lutomirski
What I mean is: why do we need the interface to be "look up this index in an array and just to what it references" as a single atomic instruction? Can't we break it down into first "look up this index in an array" and then "do this tail call"?
Quoted 8 lines
I've actually considered to do this split and do first part as map lookup and 2nd as 'tail call to this ptr' insn, but it turned out to be
painful: verifier gets more complicated, ctx pointer needs to kept somewhere, JITs need to special case two things instead of one. Also I couldn't see a use case for exposing program pointer to the program itself. I've explored this path only because it felt more traditional 'goto *ptr' like, but adding newPTR_TO_PROGtype to verifier looked wasteful.
Quoted 5 lines
At some point, I think that it would be worth extending the verifier to support more general non-integral scalar types. "Pointer to tail-call target" would be just one of them. "Pointer to skb" might be nice as a real first-class scalar type that lives in a register as opposed to just being magic typed context.
well, I don't see a use case for 'pointer to tail-call target', but more generic 'pointer to skb' indeed is a useful concept. I was thinking more like 'pointer to structure of the type X', then we can natively support 'pointer to task_struct', 'pointer to inode', etc which will help tracing programs to be written in more convenient way.
Right now pointer walking has to be done via bpf_probe_read() helper as demonstrated in tracex1_kern.c example.
With this future 'pointer to struct of type X' knowledge in verifier we'll be able to do 'ptr->field' natively with higher performance.
Quoted 2 lines
We'd still need some way to stick fds into a map, but that's not really the verifier's problem.
well, they both need to be aware of that. When it comes to safety generalization suffers. Have to do extra checks both in map_update_elem and in verifier. No way around that.
Re: [PATCH net-next 1/4] bpf: allow bpf programs to tail-call other bpf programs
Quoted 33 lines · Alexei Starovoitov
Quoted 25 lines · Andy Lutomirski
Quoted 12 lines · Alexei Starovoitov
Quoted 7 lines · Andy Lutomirski
What I mean is: why do we need the interface to be "look up this index in an array and just to what it references" as a single atomic instruction? Can't we break it down into first "look up this index in an array" and then "do this tail call"?
Quoted 8 lines
I've actually considered to do this split and do first part as map lookup and 2nd as 'tail call to this ptr' insn, but it turned out to be
painful: verifier gets more complicated, ctx pointer needs to kept somewhere, JITs need to special case two things instead of one. Also I couldn't see a use case for exposing program pointer to the program itself. I've explored this path only because it felt more traditional 'goto *ptr' like, but adding newPTR_TO_PROGtype to verifier looked wasteful.Quoted 5 lines
At some point, I think that it would be worth extending the verifier to support more general non-integral scalar types. "Pointer to tail-call target" would be just one of them. "Pointer to skb" might be nice as a real first-class scalar type that lives in a register as opposed to just being magic typed context.
Quoted 10 lines
well, I don't see a use case for 'pointer to tail-call target', but more generic 'pointer to skb' indeed is a useful concept. I was thinking more like 'pointer to structure of the type X', then we can natively support 'pointer to
task_struct', 'pointer to inode', etc which will help tracing programs to be written in more convenient way.
Right now pointer walking has to be done viabpf_probe_read()helper as demonstrated intracex1_kern.cexample.
With this future 'pointer to struct of type X' knowledge in verifier we'll be able to do 'ptr->field' natively with higher performance.
If you implement that, then you get "pointer to tail-call target" as well, right? You wouldn't be allowed to dereference the pointer, but you could jump to it.
Quoted 9 lines
Quoted 3 lines
We'd still need some way to stick fds into a map, but that's not really the verifier's problem.
well, they both need to be aware of that. When it comes to safety generalization suffers. Have to do extra checks both in
map_update_elemand in verifier. No way around that.
Sure, the verifier needs to know that the things you read from the map are "pointer to tail-call target", but that seems like a nice thing to generalize, too. After all, you could also have arrays of pointers to other things, too.
--Andy
Re: [PATCH net-next 1/4] bpf: allow bpf programs to tail-call other bpf programs
Quoted 45 lines · Andy Lutomirski
Quoted 33 lines · Alexei Starovoitov
Quoted 25 lines · Andy Lutomirski
Quoted 12 lines · Alexei Starovoitov
Quoted 7 lines · Andy Lutomirski
What I mean is: why do we need the interface to be "look up this index in an array and just to what it references" as a single atomic instruction? Can't we break it down into first "look up this index in an array" and then "do this tail call"?
Quoted 8 lines
I've actually considered to do this split and do first part as map lookup and 2nd as 'tail call to this ptr' insn, but it turned out to be
painful: verifier gets more complicated, ctx pointer needs to kept somewhere, JITs need to special case two things instead of one. Also I couldn't see a use case for exposing program pointer to the program itself. I've explored this path only because it felt more traditional 'goto *ptr' like, but adding newPTR_TO_PROGtype to verifier looked wasteful.Quoted 5 lines
At some point, I think that it would be worth extending the verifier to support more general non-integral scalar types. "Pointer to tail-call target" would be just one of them. "Pointer to skb" might be nice as a real first-class scalar type that lives in a register as opposed to just being magic typed context.
Quoted 10 lines
well, I don't see a use case for 'pointer to tail-call target', but more generic 'pointer to skb' indeed is a useful concept. I was thinking more like 'pointer to structure of the type X', then we can natively support 'pointer to
task_struct', 'pointer to inode', etc which will help tracing programs to be written in more convenient way.
Right now pointer walking has to be done viabpf_probe_read()helper as demonstrated intracex1_kern.cexample.
With this future 'pointer to struct of type X' knowledge in verifier we'll be able to do 'ptr->field' natively with higher performance.
Quoted 3 lines
If you implement that, then you get "pointer to tail-call target" as well, right? You wouldn't be allowed to dereference the pointer, but you could jump to it.
not really. Such 'pointer to tail-call target' would still be separate type and treated specially through the verifier.
'pointer to datastructure' can be generalized for different structs, because they are data, whereas 'pointer to code' is different in a sense of what program will be able to do with such pointer. The program will be able to read certain fields with proper alignment from such 'pointer to datastruct' and type of datastruct would need to be tracked, but 'pointer to code' have nothing interesting from the program point of view. It can only jump there.
It cannot store in anywhere, because the life time of code pointer is within this program lifetime (programs run under rcu). As soon as program got this 'pointer to code' it needs to jump to it. Whereas 'pointer to data' have different lifetimes.
Quoted 9 lines
Quoted 8 lines
Quoted 2 lines
We'd still need some way to stick fds into a map, but that's not really the verifier's problem.
well, they both need to be aware of that. When it comes to safety generalization suffers. Have to do extra checks both in
map_update_elemand in verifier. No way around that.
Quoted 4 lines
Sure, the verifier needs to know that the things you read from the map are "pointer to tail-call target", but that seems like a nice thing to generalize, too. After all, you could also have arrays of pointers to other things, too.
Theoretically, yes, but I'd like to implement only practical things ;) This bpf_tail_call() solves real need while 'array of pointers to other things' sounds really nice, but I don't see a demand for it yet. I'm not saying we'll never implement it, only not right now.
Re: [PATCH net-next 1/4] bpf: allow bpf programs to tail-call other bpf programs
In reply to Alexei StarovoitovQuoted 26 lines · Alexei Starovoitov
introduce
bpf_tail_call(ctx, &jmp_table, index)helper function which can be used from BPF programs like:
intbpf_prog(struct pt_regs *ctx){ ... bpf_tail_call(ctx, &jmp_table, index); ... } that is roughly equivalent to: int bpf_prog(struct pt_regs *ctx) { ... if (jmp_table[index]) return (*jmp_table[index])(ctx); ... }The important detail that it's not a normal call, but a tail call. The kernel stack is precious, so this helper reuses the current stack frame and jumps into another BPF program without adding extra call frame.
It's trivially done in interpreter and a bit trickier in JITs. In case of x64 JIT the bigger part of generated assembler prologue is common for all programs, so it is simply skipped while jumping. Other JITs can do similar prologue-skipping optimization or do stack unwind before jumping into the next program.
...
Quoted 1 line
LGTM, thanks!
PATCH 2/4 x86: bpf_jit: implement bpf_tail_call() helper
In reply to Alexei Starovoitovbpf_tail_call() arguments:
ctx - context pointerjmp_table - one of BPF_MAP_TYPE_PROG_ARRAY maps used as the jump table index - index in the jump table
In this implementation x64 JIT bypasses stack unwind and jumps into the callee program after prologue, so the callee program reuses the same stack.
The logic can be roughly expressed in C like:
u32 tail_call_cnt;
void *jumptable[2] = { &&label1, &&label2 };
int bpf_prog1(void *ctx)
{
label1:
... }
int bpf_prog2(void *ctx)
{
label2:
... }
int bpf_prog1(void *ctx)
{
...
if (tail_call_cnt++ < MAX_TAIL_CALL_CNT)
goto *jumptable[index]; ... and pass my 'ctx' to callee ...
... fall through if no entry in jumptable ...
}
Note that 'skip current program epilogue and next program prologue' is an optimization. Other JITs don't have to do it the same way.
Quoted 1 line
From safety point of view it's valid as well, since programs always
initialize the stack before use, so any residue in the stack left by the current program is not going be read. The same verifier checks are done for the calls from the kernel into all bpf programs.
--- arch/x86/net/bpf_jit_comp.c | 150 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 126 insertions(+), 24 deletions(-)
arch/x86/net/bpf_jit_comp.c +126 -24
@@ -12,6 +12,7 @@#include <linux/filter.h> #include <linux/if_vlan.h> #include <asm/cacheflush.h> +#include <linux/bpf.h> int bpf_jit_enable__read_mostly ;
@@ -37,7 +38,8 @@ static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)return ptr + len; } -#define EMIT (bytes, len) do { prog = emit_code(prog, bytes, len); } while (0) +#define EMIT (bytes, len) \ + do { prog = emit_code(prog, bytes, len); cnt += len; } while (0)#define EMIT1 (b1) EMIT(b1, 1)#define EMIT2 (b1, b2) EMIT((b1) + ((b2) << 8), 2)
@@ -186,31 +188,31 @@ struct jit_context {#define BPF_MAX_INSN_SIZE 128#define BPF_INSN_SAFETY 64 -static int do_jit (struct bpf_prog *bpf_prog,int *addrs,u8 *image, -int oldproglen,struct jit_context *ctx) +#define STACKSIZE \ + (MAX_BPF_STACK + \ + 32 /* space for rbx, r13, r14, r15 */ + \ + 8 /* space for skb_copy_bits() buffer */) + +#define PROLOGUE_SIZE 51 + +/* emit x64 prologue code for BPF program and check it's size. +* bpf_tail_call helper will skip it while jumping into another program +*/ +static void emit_prologue (u8 **pprog) { -struct bpf_insn *insn = bpf_prog->insnsi; -int insn_cnt = bpf_prog->len; -bool seen_ld_abs = ctx->seen_ld_abs | (oldproglen ==0 ); -bool seen_exit = false; -u8 temp[BPF_MAX_INSN_SIZE +BPF_INSN_SAFETY ]; -int i; -int proglen =0 ; -u8 *prog = temp; -int stacksize =MAX_BPF_STACK + -32 /* space for rbx, r13, r14, r15 */ + -8 /* space for skb_copy_bits() buffer */ ; +u8 *prog = *pprog; +int cnt =0 ;EMIT1 (0x55 );/* push rbp */ EMIT3 (0x48 ,0x89 ,0xE5 );/* mov rbp,rsp */ -/* sub rsp, stacksize */ -EMIT3_off32 (0x48 ,0x81 ,0xEC , stacksize); +/* sub rsp, STACKSIZE */ +EMIT3_off32 (0x48 ,0x81 ,0xEC ,STACKSIZE );/* all classic BPF filters use R6(rbx) save it */ /* mov qword ptr [rbp-X],rbx */ -EMIT3_off32 (0x48 ,0x89 ,0x9D , -stacksize); +EMIT3_off32 (0x48 ,0x89 ,0x9D , -STACKSIZE );/* bpf_convert_filter() maps classic BPF register X to R7 and uses R8 * as temporary, so all tcpdump filters need to spill/fill R7(r13) and
@@ -221,16 +223,112 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,*/ /* mov qword ptr [rbp-X],r13 */ -EMIT3_off32 (0x4C ,0x89 ,0xAD , -stacksize +8 ); +EMIT3_off32 (0x4C ,0x89 ,0xAD , -STACKSIZE +8 );/* mov qword ptr [rbp-X],r14 */ -EMIT3_off32 (0x4C ,0x89 ,0xB5 , -stacksize +16 ); +EMIT3_off32 (0x4C ,0x89 ,0xB5 , -STACKSIZE +16 );/* mov qword ptr [rbp-X],r15 */ -EMIT3_off32 (0x4C ,0x89 ,0xBD , -stacksize +24 ); +EMIT3_off32 (0x4C ,0x89 ,0xBD , -STACKSIZE +24 );/* clear A and X registers */ EMIT2 (0x31 ,0xc0 );/* xor eax, eax */ EMIT3 (0x4D ,0x31 ,0xED );/* xor r13, r13 */ +/* clear tail_cnt: mov qword ptr [rbp-X], rax */ +EMIT3_off32 (0x48 ,0x89 ,0x85 , -STACKSIZE +32 ); + +BUILD_BUG_ON (cnt !=PROLOGUE_SIZE ); + *pprog = prog; +} + +/* generate the following code: +* ... bpf_tail_call(void *ctx, struct bpf_array *array, u64 index) ... +* if (index >= array->map.max_entries) +* goto out; +* if (++tail_call_cnt > MAX_TAIL_CALL_CNT) +* goto out; +* prog = array->prog[index]; +* if (prog == NULL) +* goto out; +* goto *(prog->bpf_func + prologue_size); +* out: +*/ +static void emit_bpf_tail_call (u8 **pprog) +{ +u8 *prog = *pprog; +int label1, label2, label3; +int cnt =0 ; + +/* rdi - pointer to ctx +* rsi - pointer to bpf_array +* rdx - index in bpf_array +*/ + +/* if (index >= array->map.max_entries) +* goto out; +*/ +EMIT4 (0x48 ,0x8B ,0x46 ,/* mov rax, qword ptr [rsi + 16] */ + offsetof(struct bpf_array, map.max_entries)); +EMIT3 (0x48 ,0x39 ,0xD0 );/* cmp rax, rdx */ +#define OFFSET1 44/* number of bytes to jump */ +EMIT2 (X86_JBE ,OFFSET1 );/* jbe out */ + label1 = cnt; + +/* if (tail_call_cnt > MAX_TAIL_CALL_CNT) +* goto out; +*/ +EMIT2_off32 (0x8B ,0x85 , -STACKSIZE +36 );/* mov eax, dword ptr [rbp - 516] */ +EMIT3 (0x83 ,0xF8 ,MAX_TAIL_CALL_CNT );/* cmp eax, MAX_TAIL_CALL_CNT */ +#define OFFSET2 33 +EMIT2 (X86_JA ,OFFSET2 );/* ja out */ + label2 = cnt; +EMIT3 (0x83 ,0xC0 ,0x01 );/* add eax, 1 */ +EMIT2_off32 (0x89 ,0x85 , -STACKSIZE +36 );/* mov dword ptr [rbp - 516], eax */ + +/* prog = array->prog[index]; */ +EMIT4 (0x48 ,0x8D ,0x44 ,0xD6 );/* lea rax, [rsi + rdx * 8 + 0x50] */ +EMIT1 (offsetof(struct bpf_array , prog)); +EMIT3 (0x48 ,0x8B ,0x00 );/* mov rax, qword ptr [rax] */ + +/* if (prog == NULL) +* goto out; +*/ +EMIT4 (0x48 ,0x83 ,0xF8 ,0x00 );/* cmp rax, 0 */ +#define OFFSET3 10 +EMIT2 (X86_JE ,OFFSET3 );/* je out */ + label3 = cnt; + +/* goto *(prog->bpf_func + prologue_size); */ +EMIT4 (0x48 ,0x8B ,0x40 ,/* mov rax, qword ptr [rax + 32] */ + offsetof(struct bpf_prog , bpf_func)); +EMIT4 (0x48 ,0x83 ,0xC0 ,PROLOGUE_SIZE );/* add rax, prologue_size */ + +/* now we're ready to jump into next BPF program +* rdi == ctx (1st arg) +* rax == prog->bpf_func + prologue_size +*/ +EMIT2 (0xFF ,0xE0 );/* jmp rax */ + +/* out: */ +BUILD_BUG_ON (cnt - label1 !=OFFSET1 ); +BUILD_BUG_ON (cnt - label2 !=OFFSET2 ); +BUILD_BUG_ON (cnt - label3 !=OFFSET3 ); + *pprog = prog; +} + +static int do_jit (struct bpf_prog *bpf_prog,int *addrs,u8 *image, +int oldproglen,struct jit_context *ctx) +{ +struct bpf_insn *insn = bpf_prog->insnsi; +int insn_cnt = bpf_prog->len; +bool seen_ld_abs = ctx->seen_ld_abs | (oldproglen ==0 ); +bool seen_exit = false; +u8 temp[BPF_MAX_INSN_SIZE +BPF_INSN_SAFETY ]; +int i, cnt =0 ; +int proglen =0 ; +u8 *prog = temp; + +emit_prologue (&prog); +if (seen_ld_abs) {/* r9d : skb->len - skb->data_len (headlen) * r10 : skb->data
@@ -739,6 +837,10 @@ xadd: if (is_imm8(insn->off)) }break ; +case BPF_JMP |BPF_CALL |BPF_X : +emit_bpf_tail_call (&prog); +break ; +/* cond jump */ case BPF_JMP |BPF_JEQ |BPF_X :case BPF_JMP |BPF_JNE |BPF_X :
@@ -891,13 +993,13 @@ common_load:/* update cleanup_addr */ ctx->cleanup_addr = proglen;/* mov rbx, qword ptr [rbp-X] */ -EMIT3_off32 (0x48 ,0x8B ,0x9D , -stacksize); +EMIT3_off32 (0x48 ,0x8B ,0x9D , -STACKSIZE );/* mov r13, qword ptr [rbp-X] */ -EMIT3_off32 (0x4C ,0x8B ,0xAD , -stacksize +8 ); +EMIT3_off32 (0x4C ,0x8B ,0xAD , -STACKSIZE +8 );/* mov r14, qword ptr [rbp-X] */ -EMIT3_off32 (0x4C ,0x8B ,0xB5 , -stacksize +16 ); +EMIT3_off32 (0x4C ,0x8B ,0xB5 , -STACKSIZE +16 );/* mov r15, qword ptr [rbp-X] */ -EMIT3_off32 (0x4C ,0x8B ,0xBD , -stacksize +24 ); +EMIT3_off32 (0x4C ,0x8B ,0xBD , -STACKSIZE +24 );EMIT1 (0xC9 );/* leave */ EMIT1 (0xC3 );/* ret */
Signature · 1 line
1.7.9.5
Re: [PATCH net-next 2/4] x86: bpf_jit: implement bpf_tail_call() helper
Quoted 26 lines · Alexei Starovoitov
bpf_tail_call()arguments:
ctx - context pointerjmp_table- one ofBPF_MAP_TYPE_PROG_ARRAYmaps used as the jump table index - index in the jump tableIn this implementation x64 JIT bypasses stack unwind and jumps into the callee program after prologue, so the callee program reuses the same stack.
The logic can be roughly expressed in C like:
u32
tail_call_cnt;void *jumptable[2] = { &&label1, &&label2 };
int
bpf_prog1(void *ctx)
{
label1:... }int
bpf_prog2(void *ctx)
{
label2:... }
Quoted 9 lines
int
bpf_prog1(void *ctx)
{
...
if (tail_call_cnt++ <MAX_TAIL_CALL_CNT)
goto *jumptable[index]; ... and pass my 'ctx' to callee ...... fall through if no entry in jumptable ...
}
What causes the stack pointer to be right? Is there some reason that the stack pointer is the same no matter where you are in the generated code?
--Andy
Re: [PATCH net-next 2/4] x86: bpf_jit: implement bpf_tail_call() helper
Quoted 33 lines · Andy Lutomirski
Quoted 32 lines · Alexei Starovoitov
bpf_tail_call()arguments:
ctx - context pointerjmp_table- one ofBPF_MAP_TYPE_PROG_ARRAYmaps used as the jump table index - index in the jump tableIn this implementation x64 JIT bypasses stack unwind and jumps into the callee program after prologue, so the callee program reuses the same stack.
The logic can be roughly expressed in C like:
u32
tail_call_cnt;void *jumptable[2] = { &&label1, &&label2 };
int
bpf_prog1(void *ctx)
{
label1:... }int
bpf_prog2(void *ctx)
{
label2:... }int
bpf_prog1(void *ctx)
{
...
if (tail_call_cnt++ <MAX_TAIL_CALL_CNT)
goto *jumptable[index]; ... and pass my 'ctx' to callee ...
Quoted 7 lines
Quoted 3 lines
... fall through if no entry in jumptable ...
}What causes the stack pointer to be right? Is there some reason that the stack pointer is the same no matter where you are in the generated code?
that's why I said 'it's _roughly_ expressed in C' this way. Stack pointer doesn't change. It uses the same stack frame.
Re: [PATCH net-next 2/4] x86: bpf_jit: implement bpf_tail_call() helper
Quoted 42 lines · Alexei Starovoitov
Quoted 41 lines · Andy Lutomirski
Quoted 37 lines · Alexei Starovoitov
bpf_tail_call()arguments:
ctx - context pointerjmp_table- one ofBPF_MAP_TYPE_PROG_ARRAYmaps used as the jump table index - index in the jump tableIn this implementation x64 JIT bypasses stack unwind and jumps into the callee program after prologue, so the callee program reuses the same stack.
The logic can be roughly expressed in C like:
u32
tail_call_cnt;void *jumptable[2] = { &&label1, &&label2 };
int
bpf_prog1(void *ctx)
{
label1:... }int
bpf_prog2(void *ctx)
{
label2:... }int
bpf_prog1(void *ctx)
{
...
if (tail_call_cnt++ <MAX_TAIL_CALL_CNT)
goto *jumptable[index]; ... and pass my 'ctx' to callee ...... fall through if no entry in jumptable ...
}
Quoted 8 lines
Quoted 3 lines
What causes the stack pointer to be right? Is there some reason that the stack pointer is the same no matter where you are in the generated code?
that's why I said 'it's _roughly_ expressed in C' this way. Stack pointer doesn't change. It uses the same stack frame.
I think the more relevant point is that (I think) eBPF never changes the stack pointer after the prologue (i.e. the stack depth is truly constant).
--Andy
Re: [PATCH net-next 2/4] x86: bpf_jit: implement bpf_tail_call() helper
Quoted 6 lines · Andy Lutomirski
Quoted 6 lines
Quoted 4 lines
What causes the stack pointer to be right? Is there some reason that the stack pointer is the same no matter where you are in the generated code?
Quoted 7 lines
Quoted 3 lines
that's why I said 'it's _roughly_ expressed in C' this way. Stack pointer doesn't change. It uses the same stack frame.
I think the more relevant point is that (I think) eBPF never changes the stack pointer after the prologue (i.e. the stack depth is truly constant).
ahh, that's what you were referring to.
Yes, there is no alloca(). stack cannot grow and always fixed. That's critical for safety verification.
On a JIT side though, x64 has ugly div/mod, so JIT is doing push/pop rax/rdx to compile 'dst_reg /= src_reg' bpf insn. But that doesn't change 'same stack depth' rule at the time of bpf_tail_call.
Note, s390 JIT can generate different prologue/epilogue for every program, so it will likely be doing stack unwind and jump. Like I was doing in my tail_call_v2 version of x64 jit:
https://git.kernel.org/cgit/linux/kernel/git/ast/bpf.git/diff/arch/x86/net/bpf_jit_comp.c?h=tail_call_v2&id=bfd60c3135c8f010a6497dfc5e7d3070e26ca4d1
In case of interrupt happens sometime during this jumping process it's also fine. no-red-zone business is very dear to my heart :) I always keep it in mind when doing assembler/jit changes.
PATCH 3/4 samples/bpf: bpf_tail_call example for tracing
In reply to Alexei Starovoitovkprobe example that demonstrates how future seccomp programs may look like. It attaches to seccomp_phase1() function and tail-calls other BPF programs depending on syscall number.
Existing optimized classic BPF seccomp programs generated by Chrome look like:
if (sd.nr < 121) {
if (sd.nr < 57) {
if (sd.nr < 22) {
if (sd.nr < 7) {
if (sd.nr < 4) {
if (sd.nr < 1) {
check sys_read
} else {
if (sd.nr < 3) {
check sys_write and sys_open
} else {
check sys_close
}
}
} else {
} else {
} else { } else {
} else {
}
the future seccomp using native eBPF may look like:
bpf_tail_call(&sd, &syscall_jmp_table, sd.nr);
which is simpler, faster and leaves more room for per-syscall checks.
Usage:
$ sudo ./tracex5 <...>-366 [001] d... 4.870033: : read(fd=1, buf=00007f6d5bebf000, size=771) <...>-369 [003] d... 4.870066: : mmap <...>-369 [003] d... 4.870077: : syscall=110 (one of get/set uid/pid/gid) <...>-369 [003] d... 4.870089: : syscall=107 (one of get/set uid/pid/gid) sh-369 [000] d... 4.891740: : read(fd=0, buf=00000000023d1000, size=512) sh-369 [000] d... 4.891747: : write(fd=1, buf=00000000023d3000, size=512) sh-369 [000] d... 4.891747: : read(fd=1, buf=00000000023d3000, size=512)
--- samples/bpf/Makefile | 4 +++ samples/bpf/bpf_helpers.h | 2 ++ samples/bpf/bpf_load.c | 57 ++++++++++++++++++++++++++------- samples/bpf/tracex5_kern.c | 75 ++++++++++++++++++++++++++++++++++++++++++++ samples/bpf/tracex5_user.c | 46 +++++++++++++++++++++++++++ 5 files changed, 172 insertions(+), 12 deletions(-) create mode 100644 samples/bpf/tracex5_kern.c create mode 100644 samples/bpf/tracex5_user.c
samples/bpf/Makefile +4 -0
@@ -10,6 +10,7 @@ hostprogs-y += tracex1 hostprogs-y += tracex2 hostprogs-y += tracex3 hostprogs-y += tracex4 +hostprogs-y += tracex5 test_verifier-objs := test_verifier.o libbpf.o test_maps-objs := test_maps.o libbpf.o
@@ -20,6 +21,7 @@ tracex1-objs := bpf_load.o libbpf.o tracex1_user.o tracex2-objs := bpf_load.o libbpf.o tracex2_user.o tracex3-objs := bpf_load.o libbpf.o tracex3_user.o tracex4-objs := bpf_load.o libbpf.o tracex4_user.o +tracex5-objs := bpf_load.o libbpf.o tracex5_user.o # Tell kbuild to always build the programs always := $(hostprogs-y)
@@ -29,6 +31,7 @@ always += tracex1_kern.o always += tracex2_kern.o always += tracex3_kern.o always += tracex4_kern.o +always += tracex5_kern.o always += tcbpf1_kern.o HOSTCFLAGS += -I$(objtree)/usr/include
@@ -40,6 +43,7 @@ HOSTLOADLIBES_tracex1 += -lelf HOSTLOADLIBES_tracex2 += -lelf HOSTLOADLIBES_tracex3 += -lelf HOSTLOADLIBES_tracex4 += -lelf -lrt +HOSTLOADLIBES_tracex5 += -lelf # point this to your LLVM backend with bpf support LLC=$(srctree)/tools/bpf/llvm/bld/Debug+Asserts/bin/llc
samples/bpf/bpf_helpers.h +2 -0
@@ -21,6 +21,8 @@ static unsigned long long (*bpf_ktime_get_ns)(void) = (void *) BPF_FUNC_ktime_get_ns;static int (*bpf_trace_printk)(const char *fmt,int fmt_size, ...) = (void *) BPF_FUNC_trace_printk; +static void (*bpf_tail_call)(void *ctx,void *map,int index) = + (void *) BPF_FUNC_tail_call;/* llvm builtin functions that eBPF C program may use to * emit BPF_LD_ABS and BPF_LD_IND instructions
samples/bpf/bpf_load.c +45 -12
93 lines not on this page · raw message
samples/bpf/tracex5_kern.c new file +75 -0
75 lines not on this page · raw message
samples/bpf/tracex5_user.c new file +46 -0
46 lines not on this page · raw message
Signature · 1 line
1.7.9.5
PATCH 4/4 samples/bpf: bpf_tail_call example for networking
In reply to Alexei StarovoitovUsage:
$ sudo ./sockex3 IP src.port -> dst.port bytes packets 127.0.0.1.42010 -> 127.0.0.1.12865 1568 8 127.0.0.1.59526 -> 127.0.0.1.33778 11422636 173070 127.0.0.1.33778 -> 127.0.0.1.59526 11260224828 341974 127.0.0.1.12865 -> 127.0.0.1.42010 1832 12 IP src.port -> dst.port bytes packets 127.0.0.1.42010 -> 127.0.0.1.12865 1568 8 127.0.0.1.59526 -> 127.0.0.1.33778 23198092 351486 127.0.0.1.33778 -> 127.0.0.1.59526 22972698518 698616 127.0.0.1.12865 -> 127.0.0.1.42010 1832 12
this example is similar to sockex2 in a way that it accumulates per-flow statistics, but it does packet parsing differently.
sockex2 inlines full packet parser routine into single bpf program. This sockex3 example have 4 independent programs that parse vlan, mpls, ip, ipv6 and one main program that starts the process.bpf_tail_call() mechanism allows each program to be small and be called on demand potentially multiple times, so that many vlan, mpls, ip in ip, gre encapsulations can be parsed. These and other protocol parsers can be added or removed at runtime. TLVs can be parsed in similar manner. Note, tail_call_cnt dynamic check limits the number of tail calls to 32.
--- samples/bpf/Makefile | 4 + samples/bpf/bpf_helpers.h | 2 + samples/bpf/sockex3_kern.c | 303 ++++++++++++++++++++++++++++++++++++++++++++ samples/bpf/sockex3_user.c | 66 ++++++++++ 4 files changed, 375 insertions(+) create mode 100644 samples/bpf/sockex3_kern.c create mode 100644 samples/bpf/sockex3_user.c
samples/bpf/Makefile +4 -0
@@ -6,6 +6,7 @@ hostprogs-y := test_verifier test_maps hostprogs-y += sock_example hostprogs-y += sockex1 hostprogs-y += sockex2 +hostprogs-y += sockex3 hostprogs-y += tracex1 hostprogs-y += tracex2 hostprogs-y += tracex3
@@ -17,6 +18,7 @@ test_maps-objs := test_maps.o libbpf.o sock_example-objs := sock_example.o libbpf.o sockex1-objs := bpf_load.o libbpf.o sockex1_user.o sockex2-objs := bpf_load.o libbpf.o sockex2_user.o +sockex3-objs := bpf_load.o libbpf.o sockex3_user.o tracex1-objs := bpf_load.o libbpf.o tracex1_user.o tracex2-objs := bpf_load.o libbpf.o tracex2_user.o tracex3-objs := bpf_load.o libbpf.o tracex3_user.o
@@ -27,6 +29,7 @@ tracex5-objs := bpf_load.o libbpf.o tracex5_user.o always := $(hostprogs-y) always += sockex1_kern.o always += sockex2_kern.o +always += sockex3_kern.o always += tracex1_kern.o always += tracex2_kern.o always += tracex3_kern.o
@@ -39,6 +42,7 @@ HOSTCFLAGS += -I$(objtree)/usr/include HOSTCFLAGS_bpf_load.o += -I$(objtree)/usr/include -Wno-unused-variable HOSTLOADLIBES_sockex1 += -lelf HOSTLOADLIBES_sockex2 += -lelf +HOSTLOADLIBES_sockex3 += -lelf HOSTLOADLIBES_tracex1 += -lelf HOSTLOADLIBES_tracex2 += -lelf HOSTLOADLIBES_tracex3 += -lelf
samples/bpf/bpf_helpers.h +2 -0
@@ -23,6 +23,8 @@ static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) = (void *) BPF_FUNC_trace_printk;static void (*bpf_tail_call)(void *ctx,void *map,int index) = (void *) BPF_FUNC_tail_call; +static unsigned long long (*bpf_get_smp_processor_id)(void ) = + (void *) BPF_FUNC_get_smp_processor_id;/* llvm builtin functions that eBPF C program may use to * emit BPF_LD_ABS and BPF_LD_IND instructions
samples/bpf/sockex3_kern.c new file +303 -0
303 lines not on this page · raw message
samples/bpf/sockex3_user.c new file +66 -0
66 lines not on this page · raw message
Signature · 1 line
1.7.9.5
David Miller
maintainerFrom: Alexei Starovoitov <ast-uqk4Ao+rVK5Wk0Htik3J/[email protected]>
Date: Tue, 19 May 2015 16:59:02 -0700
Quoted 1 line
introduce
bpf_tail_call(ctx, &jmp_table, index)helper function
Since we dispatch often by IDs like syscall numbers and protocol IDs, this seems very useful.
Series applied, thanks Alexei.