eBPF support for cls_bpf
Subsystems (6)
Daniel Borkmann
maintainercover eBPF support for cls_bpf
I'm sending this out only as RFC as the merge window is open anyway and Dave's pull request pending.
My plan would be to get this work fully refined and remove some rough edges until net-next opens up again, submitting it as non-RFC.
So for the time being similarly as the recent OVS eBPF action patchset this may serve as a discussion ground, also wrt to the BPF tutorial at netdev01 [1].
I presume on top of this e.g. Jiri might want to follow-up with act_bpf, I'm also open/happy to contribute to it.
The series starts with a couple of cleanups and making the prog type infrastructure pluggable in eBPF. Most interesting is probably the last patch that adds actual support and the iproute2 bits from the link below.
With regards to accessing fields from the context (here: skb), I leave that for future work just as we currently do in socket filters, also with regard to the currently ongoing ABI discussion from tracing side. Nevertheless, the state with these patches still allows for interesting functionality to be implemented as complex classifiers, e.g. such as a fully fledged C-like flow dissector as shown in bpf samples directory and the like.
iproute2 part:
http://git.breakpoint.cc/cgit/dborkman/iproute2.git/log/?h=ebpf
I have configured and built LLVM with: --enable-experimental-targets=BPF
[1] https://www.netdev01.org/sessions/15
Thanks !
Daniel Borkmann (7):
ebpf: remove kernel test stubs
ebpf: constify various function pointer structs
ebpf: check first for MAXINSNS in bpf_prog_load
ebpf: extend program type/subsystem registration
ebpf: export BPF_PSEUDO_MAP_FD to uapi
ebpf: remove CONFIG_BPF_SYSCALL ifdefs in socket filter code
cls_bpf: add initial eBPF support for programmable classifiers
include/linux/bpf.h | 46 +++++++--- include/linux/filter.h | 2 - include/uapi/linux/bpf.h | 3 + include/uapi/linux/pkt_cls.h | 1 + kernel/bpf/Makefile | 3 - kernel/bpf/arraymap.c | 6 +- kernel/bpf/hashtab.c | 6 +- kernel/bpf/helpers.c | 9 +- kernel/bpf/syscall.c | 96 +++++++++++++++------ kernel/bpf/test_stub.c | 78 ----------------- kernel/bpf/verifier.c | 28 ++++-- net/core/filter.c | 169 +++++++++++++++++------------------- net/sched/cls_bpf.c | 200 ++++++++++++++++++++++++++++++++----------- samples/bpf/libbpf.h | 4 +- samples/bpf/test_verifier.c | 5 +- 15 files changed, 374 insertions(+), 282 deletions(-) delete mode 100644 kernel/bpf/test_stub.c
Signature · 1 line
1.9.3
Daniel Borkmann
maintainerPATCH 1/7 ebpf: remove kernel test stubs
Now that we have BPF_PROG_TYPE_SOCKET_FILTER up and running, we can remove the test stubs which were added to get the verifier suite up. We can just let the test cases probe under socket filter type instead. In the fill/spill test case, we cannot (yet) access fields from the context (skb), but we may adapt that test case in future.
--- kernel/bpf/Makefile | 3 -- kernel/bpf/test_stub.c | 78 --------------------------------------------- samples/bpf/test_verifier.c | 5 +-- 3 files changed, 3 insertions(+), 83 deletions(-) delete mode 100644 kernel/bpf/test_stub.c
kernel/bpf/Makefile +0 -3
@@ -1,5 +1,2 @@ obj-y := core.o obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o hashtab.o arraymap.o helpers.o -ifdef CONFIG_TEST_BPF -obj-$(CONFIG_BPF_SYSCALL) += test_stub.o -endif
kernel/bpf/test_stub.c deleted +0 -78
78 lines not on this page · raw message
samples/bpf/test_verifier.c +3 -2
@@ -288,7 +288,8 @@ static struct bpf_test tests[] = {BPF_LDX_MEM (BPF_DW ,BPF_REG_2 ,BPF_REG_10 ,-8 ),/* should be able to access R0 = *(R2 + 8) */ -BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_2 ,8 ), +/* BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 8), */ +BPF_MOV64_REG (BPF_REG_0 ,BPF_REG_2 ), BPF_EXIT_INSN(), }, .result = ACCEPT,
@@ -687,7 +688,7 @@ static int test(void) }printf ("#%d %s " , i, tests[i].descr); - prog_fd =bpf_prog_load (BPF_PROG_TYPE_UNSPEC , prog, + prog_fd =bpf_prog_load (BPF_PROG_TYPE_SOCKET_FILTER , prog, prog_len *sizeof (struct bpf_insn ),"GPL" );
Signature · 1 line
1.9.3
Daniel Borkmann
maintainerPATCH 2/7 ebpf: constify various function pointer structs
In reply to Daniel BorkmannWe can move bpf_map_ops and bpf_verifier_ops and other structs into RO section, bpf_map_type_list and bpf_prog_type_list into read mostly.
--- include/linux/bpf.h | 14 +++++++------- kernel/bpf/arraymap.c | 6 +++--- kernel/bpf/hashtab.c | 6 +++--- kernel/bpf/helpers.c | 6 +++--- net/core/filter.c | 6 +++--- 5 files changed, 19 insertions(+), 19 deletions(-)
include/linux/bpf.h +7 -7
@@ -32,13 +32,13 @@ struct bpf_map {u32 key_size;u32 value_size;u32 max_entries; -struct bpf_map_ops *ops; +const struct bpf_map_ops *ops;struct work_struct work; };struct bpf_map_type_list {struct list_head list_node; -struct bpf_map_ops *ops; +const struct bpf_map_ops *ops;enum bpf_map_type type; };
@@ -109,7 +109,7 @@ struct bpf_verifier_ops {struct bpf_prog_type_list {struct list_head list_node; -struct bpf_verifier_ops *ops; +const struct bpf_verifier_ops *ops;enum bpf_prog_type type; };
@@ -121,7 +121,7 @@ struct bpf_prog_aux {atomic_t refcnt;bool is_gpl_compatible;enum bpf_prog_type prog_type; -struct bpf_verifier_ops *ops; +const struct bpf_verifier_ops *ops;struct bpf_map **used_maps;u32 used_map_cnt;struct bpf_prog *prog;
@@ -138,8 +138,8 @@ struct bpf_prog *bpf_prog_get(u32 ufd);int bpf_check (struct bpf_prog *fp,union bpf_attr *attr);/* verifier prototypes for helper functions called from eBPF programs */ -extern struct bpf_func_proto bpf_map_lookup_elem_proto; -extern struct bpf_func_proto bpf_map_update_elem_proto; -extern struct bpf_func_proto bpf_map_delete_elem_proto; +extern const struct bpf_func_proto bpf_map_lookup_elem_proto; +extern const struct bpf_func_proto bpf_map_update_elem_proto; +extern const struct bpf_func_proto bpf_map_delete_elem_proto;#endif /* _LINUX_BPF_H */
kernel/bpf/arraymap.c +3 -3
@@ -134,7 +134,7 @@ static void array_map_free(struct bpf_map *map)kvfree (array); } -static struct bpf_map_ops array_ops = { +static const struct bpf_map_ops array_ops = { .map_alloc = array_map_alloc, .map_free = array_map_free, .map_get_next_key = array_map_get_next_key,
@@ -143,14 +143,14 @@ static struct bpf_map_ops array_ops = { .map_delete_elem = array_map_delete_elem, }; -static struct bpf_map_type_list tl = { +static struct bpf_map_type_list array_type__read_mostly = { .ops = &array_ops, .type =BPF_MAP_TYPE_ARRAY , };static int __init register_array_map (void ) { -bpf_register_map_type (&tl); +bpf_register_map_type (&array_type);return 0 ; }late_initcall (register_array_map);
kernel/bpf/hashtab.c +3 -3
@@ -345,7 +345,7 @@ static void htab_map_free(struct bpf_map *map)kfree (htab); } -static struct bpf_map_ops htab_ops = { +static const struct bpf_map_ops htab_ops = { .map_alloc = htab_map_alloc, .map_free = htab_map_free, .map_get_next_key = htab_map_get_next_key,
@@ -354,14 +354,14 @@ static struct bpf_map_ops htab_ops = { .map_delete_elem = htab_map_delete_elem, }; -static struct bpf_map_type_list tl = { +static struct bpf_map_type_list htab_type__read_mostly = { .ops = &htab_ops, .type =BPF_MAP_TYPE_HASH , };static int __init register_htab_map (void ) { -bpf_register_map_type (&tl); +bpf_register_map_type (&htab_type);return 0 ; }late_initcall (register_htab_map);
kernel/bpf/helpers.c +3 -3
@@ -41,7 +41,7 @@ static u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)return (unsigned long ) value; } -struct bpf_func_proto bpf_map_lookup_elem_proto = { +const struct bpf_func_proto bpf_map_lookup_elem_proto = { .func = bpf_map_lookup_elem, .gpl_only = false, .ret_type =RET_PTR_TO_MAP_VALUE_OR_NULL ,
@@ -60,7 +60,7 @@ static u64 bpf_map_update_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)return map->ops->map_update_elem (map, key, value, r4); } -struct bpf_func_proto bpf_map_update_elem_proto = { +const struct bpf_func_proto bpf_map_update_elem_proto = { .func = bpf_map_update_elem, .gpl_only = false, .ret_type =RET_INTEGER ,
@@ -80,7 +80,7 @@ static u64 bpf_map_delete_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)return map->ops->map_delete_elem (map, key); } -struct bpf_func_proto bpf_map_delete_elem_proto = { +const struct bpf_func_proto bpf_map_delete_elem_proto = { .func = bpf_map_delete_elem, .gpl_only = false, .ret_type =RET_INTEGER ,
net/core/filter.c +3 -3
@@ -1159,19 +1159,19 @@ static bool sock_filter_is_valid_access(int off, int size, enum bpf_access_typereturn false; } -static struct bpf_verifier_ops sock_filter_ops = { +static const struct bpf_verifier_ops sock_filter_ops = { .get_func_proto = sock_filter_func_proto, .is_valid_access = sock_filter_is_valid_access, }; -static struct bpf_prog_type_list tl = { +static struct bpf_prog_type_list sock_filter_type__read_mostly = { .ops = &sock_filter_ops, .type =BPF_PROG_TYPE_SOCKET_FILTER , };static int __init register_sock_filter_ops (void ) { -bpf_register_prog_type (&tl); +bpf_register_prog_type (&sock_filter_type);return 0 ; }late_initcall (register_sock_filter_ops);
Signature · 1 line
1.9.3
Daniel Borkmann
maintainerPATCH 3/7 ebpf: check first for MAXINSNS in bpf_prog_load
In reply to Daniel BorkmannJust minor ... before doing all the copying work, we may want to check for instruction count earlier. Also, we may want to warn the user in case we would otherwise need to truncate the license information.
--- kernel/bpf/syscall.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
kernel/bpf/syscall.c +9 -8
@@ -473,25 +473,26 @@ static int bpf_prog_load(union bpf_attr *attr) {enum bpf_prog_type type = attr->prog_type;struct bpf_prog *prog; -int err;char license[128 ];bool is_gpl; +int err;if (CHECK_ATTR (BPF_PROG_LOAD ))return -EINVAL ; +if (attr->insn_cnt >=BPF_MAXINSNS ) +return -EINVAL ;/* copy eBPF program license from user space */ -if (strncpy_from_user (license,u64_to_ptr (attr->license), -sizeof (license) -1 ) <0 ) -return -EFAULT ; - license[sizeof (license) -1 ] =0 ; + err =strncpy_from_user (license,u64_to_ptr (attr->license), +sizeof (license)); +if (err ==sizeof (license)) + err = -ERANGE ; +if (err <0 ) +return err;/* eBPF programs must be GPL compatible to use GPL-ed functions */ is_gpl =license_is_gpl_compatible (license); -if (attr->insn_cnt >=BPF_MAXINSNS ) -return -EINVAL ; -/* plain bpf_prog allocation */ prog =bpf_prog_alloc (bpf_prog_size (attr->insn_cnt),GFP_USER );if (!prog)
Signature · 1 line
1.9.3
Daniel Borkmann
maintainerPATCH 4/7 ebpf: extend program type/subsystem registration
In reply to Daniel BorkmannWhen various subsystems/modules start to make use of ebpf e.g. cls_bpf, act_bpf, ovs, ... we need to make sure, they can register their program types only once.
Moreover, we also need to serialize various registrations, currently program type registration is being done without locks. (We should make sure to not race in future when we allow registration from modules.)
Last but not least, we need to be able to register subsystems from module context as it's not sufficient to have them only as built-in at all time. Built-in subsystems don't need to provide an owner though.
--- include/linux/bpf.h | 11 +++---- kernel/bpf/helpers.c | 3 ++ kernel/bpf/syscall.c | 79 +++++++++++++++++++++++++++++++++++++++------------ kernel/bpf/verifier.c | 15 +++++----- net/core/filter.c | 9 +++--- 5 files changed, 83 insertions(+), 34 deletions(-)
include/linux/bpf.h +6 -5
@@ -110,21 +110,22 @@ struct bpf_verifier_ops {struct bpf_prog_type_list {struct list_head list_node;const struct bpf_verifier_ops *ops; +struct module *owner;enum bpf_prog_type type; }; -void bpf_register_prog_type (struct bpf_prog_type_list *tl); +int bpf_register_prog_type (struct bpf_prog_type_list *tl); +void bpf_unregister_prog_type (struct bpf_prog_type_list *tl);struct bpf_prog ;struct bpf_prog_aux {atomic_t refcnt; -bool is_gpl_compatible; -enum bpf_prog_type prog_type; -const struct bpf_verifier_ops *ops; +bool gpl_compatible; +const struct bpf_prog_type_list *tl; +struct bpf_prog *prog;struct bpf_map **used_maps;u32 used_map_cnt; -struct bpf_prog *prog;struct work_struct work; };
kernel/bpf/helpers.c +3 -0
@@ -48,6 +48,7 @@ const struct bpf_func_proto bpf_map_lookup_elem_proto = { .arg1_type = ARG_CONST_MAP_PTR, .arg2_type = ARG_PTR_TO_MAP_KEY, }; +EXPORT_SYMBOL_GPL (bpf_map_lookup_elem_proto);static u64 bpf_map_update_elem (u64 r1,u64 r2,u64 r3,u64 r4,u64 r5) {
@@ -69,6 +70,7 @@ const struct bpf_func_proto bpf_map_update_elem_proto = { .arg3_type = ARG_PTR_TO_MAP_VALUE, .arg4_type = ARG_ANYTHING, }; +EXPORT_SYMBOL_GPL (bpf_map_update_elem_proto);static u64 bpf_map_delete_elem (u64 r1,u64 r2,u64 r3,u64 r4,u64 r5) {
@@ -87,3 +89,4 @@ const struct bpf_func_proto bpf_map_delete_elem_proto = { .arg1_type = ARG_CONST_MAP_PTR, .arg2_type = ARG_PTR_TO_MAP_KEY, }; +EXPORT_SYMBOL_GPL (bpf_map_delete_elem_proto);
kernel/bpf/syscall.c +61 -18
171 lines not on this page · raw message
kernel/bpf/verifier.c +8 -7
@@ -627,8 +627,9 @@ static int check_map_access(struct verifier_env *env, u32 regno, int off,static int check_ctx_access (struct verifier_env *env,int off,int size,enum bpf_access_type t) { -if (env->prog->aux->ops->is_valid_access && - env->prog->aux->ops->is_valid_access (off, size, t)) +const struct bpf_verifier_ops *ops = env->prog->aux->tl->ops; + +if (ops->is_valid_access && ops->is_valid_access (off, size, t))return 0 ;verbose ("invalid bpf_context access off=%d size=%d\n" , off, size);
@@ -831,6 +832,7 @@ static int check_func_arg(struct verifier_env *env, u32 regno,static int check_call (struct verifier_env *env,int func_id) {struct verifier_state *state = &env->cur_state; +const struct bpf_verifier_ops *ops = env->prog->aux->tl->ops;const struct bpf_func_proto *fn =NULL ;struct reg_state *regs = state->regs;struct bpf_map *map =NULL ;
@@ -843,16 +845,15 @@ static int check_call(struct verifier_env *env, int func_id)return -EINVAL ; } -if (env->prog->aux->ops->get_func_proto) - fn = env->prog->aux->ops->get_func_proto (func_id); - +if (ops->get_func_proto) + fn = ops->get_func_proto (func_id);if (!fn) {verbose ("unknown func %d\n" , func_id);return -EINVAL ; }/* eBPF programs must be GPL compatible to use GPL-ed functions */ -if (!env->prog->aux->is_gpl_compatible && fn->gpl_only) { +if (!env->prog->aux->gpl_compatible && fn->gpl_only) {verbose ("cannot call GPL only function from proprietary program\n" );return -EINVAL ; }
@@ -1194,7 +1195,7 @@ static int check_ld_abs(struct verifier_env *env, struct bpf_insn *insn)struct reg_state *reg;int i, err; -if (env->prog->aux->prog_type !=BPF_PROG_TYPE_SOCKET_FILTER ) { +if (env->prog->aux->tl->type !=BPF_PROG_TYPE_SOCKET_FILTER ) {verbose ("BPF_LD_ABS|IND instructions are only allowed in socket filters\n" );return -EINVAL ; }
net/core/filter.c +5 -4
@@ -814,7 +814,8 @@ static void bpf_release_orig_filter(struct bpf_prog *fp)static void __bpf_prog_release (struct bpf_prog *prog) { -if (prog->aux->prog_type ==BPF_PROG_TYPE_SOCKET_FILTER ) { +if (prog->aux->tl && + prog->aux->tl->type ==BPF_PROG_TYPE_SOCKET_FILTER ) {bpf_prog_put (prog); }else {bpf_release_orig_filter (prog);
@@ -1106,7 +1107,7 @@ int sk_attach_bpf(u32 ufd, struct sock *sk)if (IS_ERR (prog))return PTR_ERR (prog); -if (prog->aux->prog_type !=BPF_PROG_TYPE_SOCKET_FILTER ) { +if (prog->aux->tl->type !=BPF_PROG_TYPE_SOCKET_FILTER ) {/* valid fd, but invalid program type */ bpf_prog_put (prog);return -EINVAL ;
@@ -1171,8 +1172,7 @@ static struct bpf_prog_type_list sock_filter_type __read_mostly = {static int __init register_sock_filter_ops (void ) { -bpf_register_prog_type (&sock_filter_type); -return 0 ; +return bpf_register_prog_type (&sock_filter_type); }late_initcall (register_sock_filter_ops);#else
@@ -1181,6 +1181,7 @@ int sk_attach_bpf(u32 ufd, struct sock *sk) return -EOPNOTSUPP; }#endif +int sk_detach_filter (struct sock *sk) {int ret = -ENOENT ;
Signature · 1 line
1.9.3
Daniel Borkmann
maintainerPATCH 5/7 ebpf: export BPF_PSEUDO_MAP_FD to uapi
In reply to Daniel BorkmannWe need to export BPF_PSEUDO_MAP_FD to user space, as it's used in the ELF BPF loader where instructions are being loaded that need map fixups (relocations). An initial stage loads all maps into the kernel, and later on replaces related instructions in the eBPF blob with BPF_PSEUDO_MAP_FD as source register and the actual fd as immediate value. The kernel verifier recognizes this keyword and replaces the map fd with a real pointer internally.
--- include/linux/filter.h | 2 -- include/uapi/linux/bpf.h | 2 ++ samples/bpf/libbpf.h | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-)
include/linux/filter.h +0 -2
@@ -145,8 +145,6 @@ struct bpf_prog_aux; .off = 0, \ .imm = ((__u64) (IMM)) >> 32 }) -#define BPF_PSEUDO_MAP_FD 1 -/* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */ #define BPF_LD_MAP_FD (DST ,MAP_FD ) \ BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
include/uapi/linux/bpf.h +2 -0
@@ -120,6 +120,8 @@ enum bpf_prog_type { BPF_PROG_TYPE_SOCKET_FILTER, }; +#define BPF_PSEUDO_MAP_FD 1 +/* flags for BPF_MAP_UPDATE_ELEM command */ #define BPF_ANY 0/* create new element or update existing */ #define BPF_NOEXIST 1/* create new element if it didn't exist */
samples/bpf/libbpf.h +3 -1
@@ -92,7 +92,9 @@ extern char bpf_log_buf[LOG_BUF_SIZE]; .off = 0, \ .imm = ((__u64) (IMM)) >> 32 }) -#define BPF_PSEUDO_MAP_FD 1 +#ifndef BPF_PSEUDO_MAP_FD +# define BPF_PSEUDO_MAP_FD 1 +#endif /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */ #define BPF_LD_MAP_FD (DST ,MAP_FD ) \
Signature · 1 line
1.9.3
Daniel Borkmann
maintainerPATCH 6/7 ebpf: remove CONFIG_BPF_SYSCALL ifdefs in socket filter code
In reply to Daniel BorkmannSocket filter code and other subsystems with upcoming eBPF support should not need to deal with the fact that we have CONFIG_BPF_SYSCALL defined or not. Having the bpf syscall as a config option is a nice thing and I'd expect it to stay that way for expert users (I presume one day the default setting of it might change, though), but code making use of it should not care if it's actually enabled or not. Instead, hide this via header files and let the rest deal with it.
--- include/linux/bpf.h | 27 +++++++-- net/core/filter.c | 166 ++++++++++++++++++++++++---------------------------- 2 files changed, 100 insertions(+), 93 deletions(-)
include/linux/bpf.h +22 -5
@@ -114,9 +114,6 @@ struct bpf_prog_type_list {enum bpf_prog_type type; }; -int bpf_register_prog_type (struct bpf_prog_type_list *tl); -void bpf_unregister_prog_type (struct bpf_prog_type_list *tl); -struct bpf_prog ;struct bpf_prog_aux {
@@ -130,11 +127,31 @@ struct bpf_prog_aux { };#ifdef CONFIG_BPF_SYSCALL +int bpf_register_prog_type (struct bpf_prog_type_list *tl); +void bpf_unregister_prog_type (struct bpf_prog_type_list *tl); +void bpf_prog_put (struct bpf_prog *prog); +struct bpf_prog *bpf_prog_get (u32 ufd);#else -static inline void bpf_prog_put (struct bpf_prog *prog) {} +static inline int bpf_register_prog_type (struct bpf_prog_type_list *tl) +{ +return 0 ; +} + +static inline void bpf_unregister_prog_type (struct bpf_prog_type_list *tl) +{ +} + +static inline struct bpf_prog *bpf_prog_get (u32 ufd) +{ +return ERR_PTR (-EOPNOTSUPP ); +} + +static inline void bpf_prog_put (struct bpf_prog *prog) +{ +}#endif -struct bpf_prog *bpf_prog_get (u32 ufd); +/* verify correctness of eBPF program */ int bpf_check (struct bpf_prog *fp,union bpf_attr *attr);
net/core/filter.c +78 -88
181 lines not on this page · raw message
Signature · 1 line
1.9.3
Daniel Borkmann
maintainerPATCH 7/7 cls_bpf: add initial eBPF support for programmable classifiers
In reply to Daniel BorkmannThis work extends the classic BPF programmable classifier by extending its scope also to native eBPF code. This allows for implementing custom C-like classifiers, compiling them with the LLVM eBPF backend and loading the resulting object file via tc into the kernel.
Simple, minimal toy example:
#include <linux/ip.h>
#include <linux/if_ether.h>
#include <linux/bpf.h>
#include "tc_bpf_api.h"
__section("classify")
int cls_main(struct sk_buff *skb)
{
return (0x800 << 16) | load_byte(skb, ETH_HLEN + __builtin_offsetof(struct iphdr, tos));
}
char __license[] __section("license") = "GPL";The classifier can then be compiled into eBPF opcodes and loaded via tc, f.e.:
clang -O2 -emit-llvm -c cls.c -o - | llc -march=bpf -filetype=obj -o cls.o tc filter add dev em1 parent 1: bpf run object-file cls.o [...]
As it has been demonstrated, the scope can even reach up to a fully fledged flow dissector (similarly as in samples/bpf/sockex2_kern.c). For tc, maps are allowed to be used, but from kernel context only, in other words eBPF code can keep state across filter invocations. Similarly as in socket filters, we may extend functionality for eBPF classifiers over time depending on the use cases. For that purpose, I have added the BPF_PROG_TYPE_SCHED_CLS program type for the cls_bpf classifier module, so we can allow additional functions/accessors.
I was wondering whether cls_bpf and act_bpf may share C programs, I can imagine that at some point, we may introduce i) some common handlers for both (or even beyond their scope), and/or ii) some restricted function space for each of them. Both can be abstracted through struct bpf_verifier_ops in future. The context of a cls_bpf versus act_bpf is slightly different though: a cls_bpf program will return a specific classid whereas act_bpf a drop/non-drop return code. That said, we can surely have a "classify" and "action" section in a single object file, or considered mentioned constraint add a possibility of a shared section.
The workflow for getting native eBPF running from tc [1] is as
follows: for f_bpf, I've added a slightly modified ELF parser code from Alexei's kernel sample, which reads out the LLVM compiled object, sets up maps (and dynamically fixes up map fds) if any, and loads the eBPF instructions all centrally through the bpf syscall. The resulting fd from the loaded program itself is being passed down to cls_bpf, which looks up struct bpf_prog from the fd store, and holds reference, so that it stays available also after tc program lifetime. On tc filter destruction, it will then drop its reference.
[1] http://git.breakpoint.cc/cgit/dborkman/iproute2.git/log/?h=ebpf
--- include/uapi/linux/bpf.h | 1 + include/uapi/linux/pkt_cls.h | 1 + kernel/bpf/verifier.c | 15 +++- net/sched/cls_bpf.c | 200 ++++++++++++++++++++++++++++++++----------- 4 files changed, 165 insertions(+), 52 deletions(-)
include/uapi/linux/bpf.h +1 -0
@@ -118,6 +118,7 @@ enum bpf_map_type {enum bpf_prog_type {BPF_PROG_TYPE_UNSPEC ,BPF_PROG_TYPE_SOCKET_FILTER , +BPF_PROG_TYPE_SCHED_CLS , };#define BPF_PSEUDO_MAP_FD 1
include/uapi/linux/pkt_cls.h +1 -0
@@ -397,6 +397,7 @@ enum {TCA_BPF_CLASSID ,TCA_BPF_OPS_LEN ,TCA_BPF_OPS , +TCA_BPF_EFD , __TCA_BPF_MAX, };
kernel/bpf/verifier.c +13 -2
@@ -1173,6 +1173,17 @@ static int check_ld_imm(struct verifier_env *env, struct bpf_insn *insn)return 0 ; } +static bool may_access_skb (enum bpf_prog_type type) +{ +switch (type) { +case BPF_PROG_TYPE_SOCKET_FILTER : +case BPF_PROG_TYPE_SCHED_CLS : +return true; +default : +return false; + } +} +/* verify safety of LD_ABS|LD_IND instructions: * - they can only appear in the programs where ctx == skb * - since they are wrappers of function calls, they scratch R1-R5 registers,
@@ -1195,8 +1206,8 @@ static int check_ld_abs(struct verifier_env *env, struct bpf_insn *insn)struct reg_state *reg;int i, err; -if (env->prog->aux->tl->type !=BPF_PROG_TYPE_SOCKET_FILTER ) { -verbose ("BPF_LD_ABS|IND instructions are only allowed in socket filters\n" ); +if (!may_access_skb (env->prog->aux->tl->type)) { +verbose ("BPF_LD_ABS|IND instructions not allowed for this program type\n" );return -EINVAL ; }
net/sched/cls_bpf.c +150 -50
306 lines not on this page · raw message
Signature · 1 line
1.9.3
Re: [PATCH net-next 1/7] ebpf: remove kernel test stubs
In reply to Daniel BorkmannQuoted 8 lines · Daniel Borkmann
Now that we have
BPF_PROG_TYPE_SOCKET_FILTERup and running, we can remove the test stubs which were added to get the verifier suite up. We can just let the test cases probe under socket filter type instead. In the fill/spill test case, we cannot (yet) access fields from the context (skb), but we may adapt that test case in future.Signed-off-by Daniel Borkmann <[email protected]>
has been on my todo list for a while. Thanks a bunch!
Re: [PATCH net-next 2/7] ebpf: constify various function pointer structs
In reply to Daniel BorkmannQuoted 5 lines · Daniel Borkmann
We can move
bpf_map_opsandbpf_verifier_opsand other structs into RO section,bpf_map_type_listandbpf_prog_type_listinto read mostly.Signed-off-by Daniel Borkmann <[email protected]>
sure. makes sense.
Re: [PATCH net-next 3/7] ebpf: check first for MAXINSNS in bpf_prog_load
In reply to Daniel BorkmannQuoted 27 lines · Daniel Borkmann
Just minor ... before doing all the copying work, we may want to check for instruction count earlier. Also, we may want to warn the user in case we would otherwise need to truncate the license information.
Signed-off-by Daniel Borkmann <[email protected]>--- kernel/bpf/syscall.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)kernel/bpf/syscall.c +3
-1@@ -473,25 +473,26 @@ static int bpf_prog_load(union bpf_attr *attr) {enum bpf_prog_type type = attr->prog_type;struct bpf_prog *prog; -int err;char license[128 ];bool is_gpl; +int err;if (CHECK_ATTR (BPF_PROG_LOAD ))return -EINVAL ; +if (attr->insn_cnt >=BPF_MAXINSNS ) +return -EINVAL ;
kernel/bpf/syscall.c static int bpf_prog_load(union bpf_attr *attr) · line 489
Quoted 10 lines
kernel/bpf/syscall.c +4
-4/* copy eBPF program license from user space */ -if (strncpy_from_user (license,u64_to_ptr (attr->license), -sizeof (license) -1 ) <0 ) -return -EFAULT ; - license[sizeof (license) -1 ] =0 ; + err =strncpy_from_user (license,u64_to_ptr (attr->license), +sizeof (license)); +if (err ==sizeof (license)) + err = -ERANGE ;
I think this error is misleading.
In case of license we care whether it's gpl or not. This boolean indicator we remember for the life of the program. We don't keep the license.
So if user specified 'my_ultra_long_proprietery_license' that should be fine. The program should still be accepted and marked as non-gpl.
kernel/bpf/syscall.c static int bpf_prog_load(union bpf_attr *attr) · line 493
Quoted 8 lines
kernel/bpf/syscall.c +2
-2+if (err <0 ) +return err;/* eBPF programs must be GPL compatible to use GPL-ed functions */ is_gpl =license_is_gpl_compatible (license); -if (attr->insn_cnt >=BPF_MAXINSNS ) -return -EINVAL ;
moving this check, I guess, is fine. No one should depend on order of errors.
Re: [PATCH net-next 5/7] ebpf: export BPF_PSEUDO_MAP_FD to uapi
In reply to Daniel BorkmannQuoted 9 lines · Daniel Borkmann
We need to export
BPF_PSEUDO_MAP_FDto user space, as it's used in the ELF BPF loader where instructions are being loaded that need map fixups (relocations). An initial stage loads all maps into the kernel, and later on replaces related instructions in the eBPF blob withBPF_PSEUDO_MAP_FDas source register and the actual fd as immediate value. The kernel verifier recognizes this keyword and replaces the map fd with a real pointer internally.Signed-off-by Daniel Borkmann <[email protected]>
oops. thanks. didn't realize that I forgot to add it to uapi.
Daniel Borkmann
maintainerRe: [PATCH net-next 3/7] ebpf: check first for MAXINSNS in bpf_prog_load
In reply to Alexei StarovoitovOn 02/11/2015 02:21 AM, Alexei Starovoitov wrote:
...
Quoted 3 lines
So if user specified '
my_ultra_long_proprietery_license' that should be fine. The program should still be accepted and marked as non-gpl.
Yep, true. Most likely I'll drop this one for non-RFC anyway as it's not much of value. ;)