Add skb + xdp dynptrs
Subsystems (8)
v10 cover Add skb + xdp dynptrs
This patchset is the 2nd in the dynptr series. The 1st can be found here [0].
This patchset adds skb and xdp type dynptrs, which have two main benefits for packet parsing:
* allowing operations on sizes that are not statically known at compile-time (eg variable-sized accesses).
* more ergonomic and less brittle iteration through data (eg does not need manual if checking for being within bounds of data_end)
When comparing the differences in runtime for packet parsing without dynptrs vs. with dynptrs, there is no noticeable difference. Patch 9 contains more details as well as examples of how to use skb and xdp dynptrs.
[0]
https://lore.kernel.org/bpf/[email protected]/
--
Changelog:
v9 = https://lore.kernel.org/bpf/[email protected]/
v9 -> v10:
* Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr interface
* Add some more tests
* Split up patchset into more parts to make it easier to review
v8 = https://lore.kernel.org/bpf/[email protected]/
v8 -> v9:
* Fix dynptr_get_type() to check non-stack dynptrs
v7 = https://lore.kernel.org/bpf/[email protected]/
v7 -> v8:
* Change helpers to kfuncs
* Add 2 new patches (1/5 and 2/5)
v6 = https://lore.kernel.org/bpf/[email protected]/
v6 -> v7
* Change bpf_dynptr_data() to return read-only data slices if the skb prog is read-only (Martin)
* Add test "skb_invalid_write" to test that writes to rd-only data slices are rejected
v5 = https://lore.kernel.org/bpf/[email protected]/
v5 -> v6
* Address kernel test robot errors by static inlining
v4 = https://lore.kernel.org/bpf/[email protected]/
v4 -> v5
* Address kernel test robot errors for configs w/out CONFIG_NET set
* For data slices, return PTR_TO_MEM instead of PTR_TO_PACKET (Kumar)
* Split selftests into subtests (Andrii)
* Remove insn patching. Use rdonly and rdwr protos for dynptr skb construction (Andrii)
* bpf_dynptr_data() returns NULL for rd-only dynptrs. There will be a separate bpf_dynptr_data_rdonly() added later (Andrii and Kumar)
v3 = https://lore.kernel.org/bpf/[email protected]/
v3 -> v4
* Forgot to commit --amend the kernel test robot error fixups
v2 = https://lore.kernel.org/bpf/[email protected]/
v2 -> v3
* Fix kernel test robot build test errors
v1 = https://lore.kernel.org/bpf/[email protected]/
v1 -> v2
* Return data slices to rd-only skb dynptrs (Martin)
* bpf_dynptr_write allows writes to frags for skb dynptrs, but always invalidates associated data slices (Martin)
* Use switch casing instead of ifs (Andrii)
* Use 0xFD for experimental kind number in the selftest (Zvi)
* Put selftest conversions w/ dynptrs into new files (Alexei)
* Add new selftest "test_cls_redirect_dynptr.c"
Joanne Koong (9):
bpf: Support "sk_buff" and "xdp_buff" as valid kfunc arg types
bpf: Refactor process_dynptr_func
bpf: Allow initializing dynptrs in kfuncs
bpf: Define no-ops for externally called bpf dynptr functions
bpf: Refactor verifier dynptr into get_dynptr_arg_reg
bpf: Add skb dynptrs
bpf: Add xdp dynptrs
bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr selftests/bpf: tests for using dynptrs to parse skb and xdp buffers
include/linux/bpf.h | 95 +- include/linux/bpf_verifier.h | 3 - include/linux/filter.h | 46 + include/uapi/linux/bpf.h | 18 +- kernel/bpf/btf.c | 22 + kernel/bpf/helpers.c | 174 +++- kernel/bpf/verifier.c | 365 +++++-- net/core/filter.c | 108 +- tools/include/uapi/linux/bpf.h | 18 +- .../selftests/bpf/prog_tests/cls_redirect.c | 25 + .../testing/selftests/bpf/prog_tests/dynptr.c | 69 +- .../selftests/bpf/prog_tests/l4lb_all.c | 2 + .../bpf/prog_tests/parse_tcp_hdr_opt.c | 93 ++ .../selftests/bpf/prog_tests/xdp_attach.c | 11 +- .../testing/selftests/bpf/progs/dynptr_fail.c | 274 ++++- .../selftests/bpf/progs/dynptr_success.c | 51 +- .../bpf/progs/test_cls_redirect_dynptr.c | 975 ++++++++++++++++++ .../bpf/progs/test_l4lb_noinline_dynptr.c | 486 +++++++++ .../bpf/progs/test_parse_tcp_hdr_opt.c | 119 +++ .../bpf/progs/test_parse_tcp_hdr_opt_dynptr.c | 118 +++ .../selftests/bpf/progs/test_xdp_dynptr.c | 257 +++++ .../selftests/bpf/test_tcp_hdr_options.h | 1 + 22 files changed, 3151 insertions(+), 179 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/parse_tcp_hdr_opt.c create mode 100644 tools/testing/selftests/bpf/progs/test_cls_redirect_dynptr.c create mode 100644 tools/testing/selftests/bpf/progs/test_l4lb_noinline_dynptr.c create mode 100644 tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt.c create mode 100644 tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt_dynptr.c create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_dynptr.c
Signature · 1 line
2.30.2
PATCH v10 1/9 bpf: Support "sk_buff" and "xdp_buff" as valid kfunc arg types
The bpf mirror of the in-kernel sk_buff and xdp_buff data structures are __sk_buff and xdp_md. Currently, when we pass in the program ctx to a kfunc where the program ctx is a skb or xdp buffer, we reject the program if the in-kernel definition is sk_buff/xdp_buff instead of __sk_buff/xdp_md.
This change allows "sk_buff <--> __sk_buff" and "xdp_buff <--> xdp_md" to be recognized as valid matches. The user program may pass in their program ctx as a __sk_buff or xdp_md, and the in-kernel definition of the kfunc may define this arg as a sk_buff or xdp_buff.
--- kernel/bpf/btf.c | 4 ++++ 1 file changed, 4 insertions(+)
kernel/bpf/btf.c +4 -0
@@ -5675,6 +5675,10 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,bpf_log (log,"Please fix kernel include/linux/bpf_types.h\n" );return NULL ; } +if (strcmp (ctx_tname,"__sk_buff" ) ==0 &&strcmp (tname,"sk_buff" ) ==0 ) +return ctx_type; +if (strcmp (ctx_tname,"xdp_md" ) ==0 &&strcmp (tname,"xdp_buff" ) ==0 ) +return ctx_type;/* only compare that prog's ctx type name is the same as * kernel expects. No need to compare field by field. * It's ok for bpf prog to do:
Signature · 1 line
2.30.2
PATCH v10 2/9 bpf: Refactor process_dynptr_func
In reply to Joanne KoongThis change cleans up process_dynptr_func's flow to be more intuitive and updates some comments with more context.
--- include/linux/bpf_verifier.h | 3 -- kernel/bpf/verifier.c | 58 ++++++++++++++++++------------------ 2 files changed, 29 insertions(+), 32 deletions(-)
include/linux/bpf_verifier.h +0 -3
@@ -616,9 +616,6 @@ int check_func_arg_reg_off(struct bpf_verifier_env *env, enum bpf_arg_type arg_type);int check_mem_reg (struct bpf_verifier_env *env,struct bpf_reg_state *reg,u32 regno,u32 mem_size); -struct bpf_call_arg_meta ; -int process_dynptr_func (struct bpf_verifier_env *env,int regno, -enum bpf_arg_type arg_type,struct bpf_call_arg_meta *meta);/* this lives here instead of in bpf.h because it needs to dereference tgt_prog */ static inline u64 bpf_trampoline_compute_key(const struct bpf_prog *tgt_prog,
kernel/bpf/verifier.c +29 -29
104 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v10 3/9 bpf: Allow initializing dynptrs in kfuncs
In reply to Joanne KoongThis change allows kfuncs to take in an uninitialized dynptr as a parameter. Before this change, only helper functions could successfully use uninitialized dynptrs. This change moves the memory access check (including stack state growing and slot marking) into process_dynptr_func(), which both helpers and kfuncs call into.
--- kernel/bpf/verifier.c | 67 ++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 45 deletions(-)
kernel/bpf/verifier.c +22 -45
@@ -268,7 +268,6 @@ struct bpf_call_arg_meta {u32 ret_btf_id;u32 subprogno;struct btf_field *kptr_field; -u8 uninit_dynptr_regno; };struct btf *btf_vmlinux;
@@ -6216,10 +6215,11 @@ static int process_kptr_func(struct bpf_verifier_env *env, int regno,* Helpers which do not mutate the bpf_dynptr set MEM_RDONLY in their argument * type, and declare it as 'const struct bpf_dynptr *' in their prototype. */ -int process_dynptr_func (struct bpf_verifier_env *env,int regno, -enum bpf_arg_type arg_type,struct bpf_call_arg_meta *meta) +int process_dynptr_func (struct bpf_verifier_env *env,int regno,int insn_idx, +enum bpf_arg_type arg_type) {struct bpf_reg_state *regs =cur_regs (env), *reg = ®s[regno]; +int err;/* MEM_UNINIT and MEM_RDONLY are exclusive, when applied to an * ARG_PTR_TO_DYNPTR (or ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_*):
@@ -6245,23 +6245,23 @@ int process_dynptr_func(struct bpf_verifier_env *env, int regno,* to. */ if (arg_type &MEM_UNINIT ) { +int i; +if (!is_dynptr_reg_valid_uninit (env, reg)) {verbose (env,"Dynptr has to be an uninitialized dynptr\n" );return -EINVAL ; } -/* We only support one dynptr being uninitialized at the moment, -* which is sufficient for the helper functions we have right now. -*/ -if (meta->uninit_dynptr_regno) { -verbose (env,"verifier internal error: multiple uninitialized dynptr args\n" ); -return -EFAULT ; +/* we write BPF_DW bits (8 bytes) at a time */ +for (i =0 ; i <BPF_DYNPTR_SIZE ; i +=8 ) { + err =check_mem_access (env, insn_idx, regno, + i,BPF_DW ,BPF_WRITE ,-1 , false); +if (err) +return err; } - meta->uninit_dynptr_regno = regno; + err =mark_stack_slots_dynptr (env, reg, arg_type, insn_idx); }else /* MEM_RDONLY and None case from above */ { -int err; -/* For the reg->type == PTR_TO_STACK case, bpf_dynptr is never const */ if (reg->type ==CONST_PTR_TO_DYNPTR && !(arg_type &MEM_RDONLY )) {verbose (env,"cannot pass pointer to const bpf_dynptr, the helper mutates it\n" );
@@ -6297,10 +6297,8 @@ int process_dynptr_func(struct bpf_verifier_env *env, int regno, } err =mark_dynptr_read (env, reg); -if (err) -return err; } -return 0 ; +return err; }static bool arg_type_is_mem_size (enum bpf_arg_type type)
@@ -6694,7 +6692,8 @@ static int dynptr_ref_obj_id(struct bpf_verifier_env *env, struct bpf_reg_statestatic int check_func_arg (struct bpf_verifier_env *env,u32 arg,struct bpf_call_arg_meta *meta, -const struct bpf_func_proto *fn) +const struct bpf_func_proto *fn, +int insn_idx) {u32 regno =BPF_REG_1 + arg;struct bpf_reg_state *regs =cur_regs (env), *reg = ®s[regno];
@@ -6907,7 +6906,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, err =check_mem_size_reg (env, reg, regno, true, meta);break ;case ARG_PTR_TO_DYNPTR : - err =process_dynptr_func (env, regno, arg_type, meta); + err =process_dynptr_func (env, regno, insn_idx, arg_type);if (err)return err;break ;
@@ -8197,7 +8196,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn meta.func_id = func_id;/* check args */ for (i =0 ; i <MAX_BPF_FUNC_REG_ARGS ; i++) { - err =check_func_arg (env, i, &meta, fn); + err =check_func_arg (env, i, &meta, fn, insn_idx);if (err)return err; }
@@ -8222,30 +8221,6 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn regs =cur_regs (env); -/* This can only be set for PTR_TO_STACK, as CONST_PTR_TO_DYNPTR cannot -* be reinitialized by any dynptr helper. Hence, mark_stack_slots_dynptr -* is safe to do directly. -*/ -if (meta.uninit_dynptr_regno) { -if (regs[meta.uninit_dynptr_regno].type ==CONST_PTR_TO_DYNPTR ) { -verbose (env,"verifier internal error: CONST_PTR_TO_DYNPTR cannot be initialized\n" ); -return -EFAULT ; - } -/* we write BPF_DW bits (8 bytes) at a time */ -for (i =0 ; i <BPF_DYNPTR_SIZE ; i +=8 ) { - err =check_mem_access (env, insn_idx, meta.uninit_dynptr_regno, - i,BPF_DW ,BPF_WRITE ,-1 , false); -if (err) -return err; - } - - err =mark_stack_slots_dynptr (env, ®s[meta.uninit_dynptr_regno], - fn->arg_type[meta.uninit_dynptr_regno -BPF_REG_1 ], - insn_idx); -if (err) -return err; - } -if (meta.release_regno) { err = -EINVAL ;/* This can only be set for PTR_TO_STACK, as CONST_PTR_TO_DYNPTR cannot
@@ -9455,7 +9430,8 @@ static int process_kf_arg_ptr_to_rbtree_node(struct bpf_verifier_env *env, &meta->arg_rbtree_root.field); } -static int check_kfunc_args (struct bpf_verifier_env *env,struct bpf_kfunc_call_arg_meta *meta) +static int check_kfunc_args (struct bpf_verifier_env *env,struct bpf_kfunc_call_arg_meta *meta, +int insn_idx) {const char *func_name = meta->func_name, *ref_tname;const struct btf *btf = meta->btf;
@@ -9652,7 +9628,8 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_return -EINVAL ; } - ret =process_dynptr_func (env, regno,ARG_PTR_TO_DYNPTR |MEM_RDONLY ,NULL ); + ret =process_dynptr_func (env, regno, insn_idx, +ARG_PTR_TO_DYNPTR |MEM_RDONLY );if (ret <0 )return ret;break ;
@@ -9860,7 +9837,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, }/* Check the arguments */ - err =check_kfunc_args (env, &meta); + err =check_kfunc_args (env, &meta, insn_idx);if (err <0 )return err;/* In case of release function, we get register number of refcounted
Signature · 1 line
2.30.2
PATCH v10 4/9 bpf: Define no-ops for externally called bpf dynptr functions
In reply to Joanne KoongSome bpf dynptr functions will be called from places where if CONFIG_BPF_SYSCALL is not set, then the dynptr function is undefined. For example, when skb type dynptrs are added in the next commit, dynptr functions are called from net/core/filter.c
This patch defines no-op implementations of these dynptr functions so that they do not break compilation by being an undefined reference.
--- include/linux/bpf.h | 75 +++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 30 deletions(-)
include/linux/bpf.h +45 -30
@@ -1124,6 +1124,33 @@ static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(return bpf_func (ctx, insnsi); } +/* the implementation of the opaque uapi struct bpf_dynptr */ +struct bpf_dynptr_kern { +void *data; +/* Size represents the number of usable bytes of dynptr data. +* If for example the offset is at 4 for a local dynptr whose data is +* of type u64, the number of usable bytes is 4. +* +* The upper 8 bits are reserved. It is as follows: +* Bits 0 - 23 = size +* Bits 24 - 30 = dynptr type +* Bit 31 = whether dynptr is read-only +*/ +u32 size; +u32 offset; +}__aligned(8) ; + +enum bpf_dynptr_type { +BPF_DYNPTR_TYPE_INVALID , +/* Points to memory that is local to the bpf program */ +BPF_DYNPTR_TYPE_LOCAL , +/* Underlying data is a ringbuf record */ +BPF_DYNPTR_TYPE_RINGBUF , +}; + +int bpf_dynptr_check_size (u32 size); +u32 bpf_dynptr_get_size (const struct bpf_dynptr_kern *ptr); +#ifdef CONFIG_BPF_JIT int bpf_trampoline_link_prog (struct bpf_tramp_link *link,struct bpf_trampoline *tr);int bpf_trampoline_unlink_prog (struct bpf_tramp_link *link,struct bpf_trampoline *tr);
@@ -2266,6 +2293,11 @@ static inline bool has_current_bpf_ctx(void) } void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog); + +void bpf_dynptr_init (struct bpf_dynptr_kern *ptr,void *data, +enum bpf_dynptr_type type,u32 offset,u32 size); +void bpf_dynptr_set_null (struct bpf_dynptr_kern *ptr); +void bpf_dynptr_set_rdonly (struct bpf_dynptr_kern *ptr);#else /* !CONFIG_BPF_SYSCALL */ static inline struct bpf_prog *bpf_prog_get (u32 ufd) {
@@ -2495,6 +2527,19 @@ static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)static inline void bpf_cgrp_storage_free (struct cgroup *cgroup) { } + +static inline void bpf_dynptr_init (struct bpf_dynptr_kern *ptr,void *data, +enum bpf_dynptr_type type,u32 offset,u32 size) +{ +} + +static inline void bpf_dynptr_set_null (struct bpf_dynptr_kern *ptr) +{ +} + +static inline void bpf_dynptr_set_rdonly (struct bpf_dynptr_kern *ptr) +{ +}#endif /* CONFIG_BPF_SYSCALL */ void __bpf_free_used_btfs(struct bpf_prog_aux *aux,
@@ -2913,36 +2958,6 @@ int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args, u32 num_args, struct bpf_bprintf_data *data);void bpf_bprintf_cleanup (struct bpf_bprintf_data *data); -/* the implementation of the opaque uapi struct bpf_dynptr */ -struct bpf_dynptr_kern { -void *data; -/* Size represents the number of usable bytes of dynptr data. -* If for example the offset is at 4 for a local dynptr whose data is -* of type u64, the number of usable bytes is 4. -* -* The upper 8 bits are reserved. It is as follows: -* Bits 0 - 23 = size -* Bits 24 - 30 = dynptr type -* Bit 31 = whether dynptr is read-only -*/ -u32 size; -u32 offset; -}__aligned(8) ; - -enum bpf_dynptr_type { -BPF_DYNPTR_TYPE_INVALID , -/* Points to memory that is local to the bpf program */ -BPF_DYNPTR_TYPE_LOCAL , -/* Underlying data is a kernel-produced ringbuf record */ -BPF_DYNPTR_TYPE_RINGBUF , -}; - -void bpf_dynptr_init (struct bpf_dynptr_kern *ptr,void *data, -enum bpf_dynptr_type type,u32 offset,u32 size); -void bpf_dynptr_set_null (struct bpf_dynptr_kern *ptr); -int bpf_dynptr_check_size (u32 size); -u32 bpf_dynptr_get_size (const struct bpf_dynptr_kern *ptr); -#ifdef CONFIG_BPF_LSM void bpf_cgroup_atype_get (u32 attach_btf_id,int cgroup_atype);void bpf_cgroup_atype_put (int cgroup_atype);
Signature · 1 line
2.30.2
PATCH v10 5/9 bpf: Refactor verifier dynptr into get_dynptr_arg_reg
In reply to Joanne KoongThis commit refactors the logic for determining which register in a function is the dynptr into "get_dynptr_arg_reg". This will be used in the future when the dynptr reg for BPF_FUNC_dynptr_write will need to be obtained in order to support writes for skb dynptrs.
--- kernel/bpf/verifier.c | 80 +++++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 30 deletions(-)
kernel/bpf/verifier.c +50 -30
@@ -6664,6 +6664,28 @@ int check_func_arg_reg_off(struct bpf_verifier_env *env, } } +static struct bpf_reg_state *get_dynptr_arg_reg (struct bpf_verifier_env *env, +const struct bpf_func_proto *fn, +struct bpf_reg_state *regs) +{ +struct bpf_reg_state *state =NULL ; +int i; + +for (i =0 ; i <MAX_BPF_FUNC_REG_ARGS ; i++) +if (arg_type_is_dynptr (fn->arg_type[i])) { +if (state) { +verbose (env,"verifier internal error: multiple dynptr args\n" ); +return NULL ; + } + state = ®s[BPF_REG_1 + i]; + } + +if (!state) +verbose (env,"verifier internal error: no dynptr arg found\n" ); + +return state; +} +static int dynptr_id (struct bpf_verifier_env *env,struct bpf_reg_state *reg) {struct bpf_func_state *state =func (env, reg);
@@ -8305,43 +8327,41 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn }break ;case BPF_FUNC_dynptr_data: -for (i =0 ; i <MAX_BPF_FUNC_REG_ARGS ; i++) { -if (arg_type_is_dynptr (fn->arg_type[i])) { -struct bpf_reg_state *reg = ®s[BPF_REG_1 + i]; -int id, ref_obj_id; - -if (meta.dynptr_id) { -verbose (env,"verifier internal error: meta.dynptr_id already set\n" ); -return -EFAULT ; - } - -if (meta.ref_obj_id) { -verbose (env,"verifier internal error: meta.ref_obj_id already set\n" ); -return -EFAULT ; - } + { +struct bpf_reg_state *reg; +int id, ref_obj_id; - id =dynptr_id (env, reg); -if (id <0 ) { -verbose (env,"verifier internal error: failed to obtain dynptr id\n" ); -return id; - } + reg =get_dynptr_arg_reg (env, fn, regs); +if (!reg) +return -EFAULT ; - ref_obj_id =dynptr_ref_obj_id (env, reg); -if (ref_obj_id <0 ) { -verbose (env,"verifier internal error: failed to obtain dynptr ref_obj_id\n" ); -return ref_obj_id; - } - meta.dynptr_id = id; - meta.ref_obj_id = ref_obj_id; -break ; - } +if (meta.dynptr_id) { +verbose (env,"verifier internal error: meta.dynptr_id already set\n" ); +return -EFAULT ; } -if (i ==MAX_BPF_FUNC_REG_ARGS ) { -verbose (env,"verifier internal error: no dynptr in bpf_dynptr_data()\n" ); +if (meta.ref_obj_id) { +verbose (env,"verifier internal error: meta.ref_obj_id already set\n" );return -EFAULT ; } + + id =dynptr_id (env, reg); +if (id <0 ) { +verbose (env,"verifier internal error: failed to obtain dynptr id\n" ); +return id; + } + + ref_obj_id =dynptr_ref_obj_id (env, reg); +if (ref_obj_id <0 ) { +verbose (env,"verifier internal error: failed to obtain dynptr ref_obj_id\n" ); +return ref_obj_id; + } + + meta.dynptr_id = id; + meta.ref_obj_id = ref_obj_id; +break ; + }case BPF_FUNC_user_ringbuf_drain: err =__check_func_call (env, insn, insn_idx_p, meta.subprogno, set_user_ringbuf_callback_state);
Signature · 1 line
2.30.2
PATCH v10 6/9 bpf: Add skb dynptrs
In reply to Joanne KoongAdd skb dynptrs, which are dynptrs whose underlying pointer points to a skb. The dynptr acts on skb data. skb dynptrs have two main benefits. One is that they allow operations on sizes that are not statically known at compile-time (eg variable-sized accesses). Another is that parsing the packet data through dynptrs (instead of through direct access of skb->data and skb->data_end) can be more ergonomic and less brittle (eg does not need manual if checking for being within bounds of data_end).
For bpf prog types that don't support writes on skb data, the dynptr is read-only (bpf_dynptr_write() will return an error)
For reads and writes through the bpf_dynptr_read() and bpf_dynptr_write() interfaces, reading and writing from/to data in the head as well as from/to non-linear paged buffers is supported. Data slices through the bpf_dynptr_data API are not supported; instead bpf_dynptr_slice() and bpf_dynptr_slice_rdwr() (added in subsequent commit) should be used.
For examples of how skb dynptrs can be used, please see the attached selftests.
--- include/linux/bpf.h | 14 ++++++- include/linux/filter.h | 18 ++++++++ include/uapi/linux/bpf.h | 14 ++++++- kernel/bpf/btf.c | 18 ++++++++ kernel/bpf/helpers.c | 76 +++++++++++++++++++++++++++------- kernel/bpf/verifier.c | 70 ++++++++++++++++++++++++++++++- net/core/filter.c | 67 ++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 14 ++++++- 8 files changed, 270 insertions(+), 21 deletions(-)
include/linux/bpf.h +13 -1
@@ -607,11 +607,14 @@ enum bpf_type_flag {*/ NON_OWN_REF =BIT (14 +BPF_BASE_TYPE_BITS ), +/* DYNPTR points to sk_buff */ +DYNPTR_TYPE_SKB =BIT (15 +BPF_BASE_TYPE_BITS ), + __BPF_TYPE_FLAG_MAX, __BPF_TYPE_LAST_FLAG = __BPF_TYPE_FLAG_MAX - 1, }; -#define DYNPTR_TYPE_FLAG_MASK (DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF) +#define DYNPTR_TYPE_FLAG_MASK (DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF | DYNPTR_TYPE_SKB)/* Max number of base types. */ #define BPF_BASE_TYPE_LIMIT (1UL << BPF_BASE_TYPE_BITS)
@@ -1146,6 +1149,8 @@ enum bpf_dynptr_type {BPF_DYNPTR_TYPE_LOCAL ,/* Underlying data is a ringbuf record */ BPF_DYNPTR_TYPE_RINGBUF , +/* Underlying data is a sk_buff */ + BPF_DYNPTR_TYPE_SKB, };int bpf_dynptr_check_size (u32 size);
@@ -2846,6 +2851,8 @@ u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,struct bpf_insn *insn_buf, struct bpf_prog *prog, u32 *target_size); +int bpf_dynptr_from_skb_rdonly (struct sk_buff *skb,u64 flags, +struct bpf_dynptr_kern *ptr);#else static inline bool bpf_sock_common_is_valid_access(int off, int size, enum bpf_access_type type,
@@ -2867,6 +2874,11 @@ static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type, {return 0 ; } +static inline int bpf_dynptr_from_skb_rdonly (struct sk_buff *skb,u64 flags, +struct bpf_dynptr_kern *ptr) +{ +return 0 ; +}#endif #ifdef CONFIG_INET
include/linux/filter.h +18 -0
@@ -1542,4 +1542,22 @@ static __always_inline int __bpf_xdp_redirect_map(struct bpf_map *map, u64 index return XDP_REDIRECT; } +#ifdef CONFIG_NET +int __bpf_skb_load_bytes (const struct sk_buff *skb,u32 offset,void *to,u32 len); +int __bpf_skb_store_bytes (struct sk_buff *skb,u32 offset,const void *from, +u32 len,u64 flags); +#else /* CONFIG_NET */ +static inline int __bpf_skb_load_bytes (const struct sk_buff *skb,u32 offset, +void *to,u32 len) +{ +return -EOPNOTSUPP ; +} + +static inline int __bpf_skb_store_bytes (struct sk_buff *skb,u32 offset, +const void *from,u32 len,u64 flags) +{ +return -EOPNOTSUPP ; +} +#endif /* CONFIG_NET */ +#endif /* __LINUX_FILTER_H__ */
include/uapi/linux/bpf.h +12 -2
@@ -5320,11 +5320,17 @@ union bpf_attr {* Description * Write *len* bytes from *src* into *dst*, starting from *offset* * into *dst*. -* *flags* is currently unused. +* +* *flags* must be 0 except for skb-type dynptrs. +* +* For skb-type dynptrs: +* * For *flags*, please see the flags accepted by +* **bpf_skb_store_bytes**\ (). * Return * 0 on success, -E2BIG if *offset* + *len* exceeds the length * of *dst*'s data, -EINVAL if *dst* is an invalid dynptr or if *dst* -* is a read-only dynptr or if *flags* is not 0. +* is a read-only dynptr or if *flags* is not correct. For skb-type dynptrs, +* other errors correspond to errors returned by **bpf_skb_store_bytes**\ (). * * void *bpf_dynptr_data(const struct bpf_dynptr *ptr, u32 offset, u32 len) * Description
@@ -5332,6 +5338,10 @@ union bpf_attr {* * *len* must be a statically known value. The returned data slice * is invalidated whenever the dynptr is invalidated. +* +* skb type dynptrs may not use bpf_dynptr_data. They should +* instead use bpf_dynptr_slice and bpf_dynptr_slice_rdwr. +* * Return * Pointer to the underlying dynptr data, NULL if the dynptr is * read-only, if the dynptr is invalid, or if the offset and length
kernel/bpf/btf.c +18 -0
@@ -207,6 +207,11 @@ enum btf_kfunc_hook {BTF_KFUNC_HOOK_TRACING ,BTF_KFUNC_HOOK_SYSCALL ,BTF_KFUNC_HOOK_FMODRET , +BTF_KFUNC_HOOK_CGROUP_SKB , +BTF_KFUNC_HOOK_SCHED_ACT , +BTF_KFUNC_HOOK_SK_SKB , +BTF_KFUNC_HOOK_SOCKET_FILTER , +BTF_KFUNC_HOOK_LWT , BTF_KFUNC_HOOK_MAX, };
@@ -7699,6 +7704,19 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)return BTF_KFUNC_HOOK_TRACING ;case BPF_PROG_TYPE_SYSCALL :return BTF_KFUNC_HOOK_SYSCALL ; +case BPF_PROG_TYPE_CGROUP_SKB : +return BTF_KFUNC_HOOK_CGROUP_SKB ; +case BPF_PROG_TYPE_SCHED_ACT : +return BTF_KFUNC_HOOK_SCHED_ACT ; +case BPF_PROG_TYPE_SK_SKB : +return BTF_KFUNC_HOOK_SK_SKB ; +case BPF_PROG_TYPE_SOCKET_FILTER : +return BTF_KFUNC_HOOK_SOCKET_FILTER ; +case BPF_PROG_TYPE_LWT_OUT : +case BPF_PROG_TYPE_LWT_IN : +case BPF_PROG_TYPE_LWT_XMIT : +case BPF_PROG_TYPE_LWT_SEG6LOCAL : +return BTF_KFUNC_HOOK_LWT ;default :return BTF_KFUNC_HOOK_MAX ; }
kernel/bpf/helpers.c +62 -14
127 lines not on this page · raw message
kernel/bpf/verifier.c +68 -2
133 lines not on this page · raw message
net/core/filter.c +67 -0
82 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +12 -2
@@ -5320,11 +5320,17 @@ union bpf_attr {* Description * Write *len* bytes from *src* into *dst*, starting from *offset* * into *dst*. -* *flags* is currently unused. +* +* *flags* must be 0 except for skb-type dynptrs. +* +* For skb-type dynptrs: +* * For *flags*, please see the flags accepted by +* **bpf_skb_store_bytes**\ (). * Return * 0 on success, -E2BIG if *offset* + *len* exceeds the length * of *dst*'s data, -EINVAL if *dst* is an invalid dynptr or if *dst* -* is a read-only dynptr or if *flags* is not 0. +* is a read-only dynptr or if *flags* is not correct. For skb-type dynptrs, +* other errors correspond to errors returned by **bpf_skb_store_bytes**\ (). * * void *bpf_dynptr_data(const struct bpf_dynptr *ptr, u32 offset, u32 len) * Description
@@ -5332,6 +5338,10 @@ union bpf_attr {* * *len* must be a statically known value. The returned data slice * is invalidated whenever the dynptr is invalidated. +* +* skb type dynptrs may not use bpf_dynptr_data. They should +* instead use bpf_dynptr_slice and bpf_dynptr_slice_rdwr. +* * Return * Pointer to the underlying dynptr data, NULL if the dynptr is * read-only, if the dynptr is invalid, or if the offset and length
Signature · 1 line
2.30.2
Re: [PATCH v10 bpf-next 6/9] bpf: Add skb dynptrs
Quoted 73 lines · Joanne Koong
Add skb dynptrs, which are dynptrs whose underlying pointer points to a skb. The dynptr acts on skb data. skb dynptrs have two main benefits. One is that they allow operations on sizes that are not statically known at compile-time (eg variable-sized accesses). Another is that parsing the packet data through dynptrs (instead of through direct access of
skb->dataandskb->data_end) can be more ergonomic and less brittle (eg does not need manual if checking for being within bounds ofdata_end).For bpf prog types that don't support writes on skb data, the dynptr is read-only (
bpf_dynptr_write()will return an error)For reads and writes through the
bpf_dynptr_read()andbpf_dynptr_write()interfaces, reading and writing from/to data in the head as well as from/to non-linear paged buffers is supported. Data slices through thebpf_dynptr_dataAPI are not supported; insteadbpf_dynptr_slice()andbpf_dynptr_slice_rdwr()(added in subsequent commit) should be used.For examples of how skb dynptrs can be used, please see the attached selftests.
Signed-off-by Joanne Koong <[email protected]>--- include/linux/bpf.h | 14 ++++++- include/linux/filter.h | 18 ++++++++ include/uapi/linux/bpf.h | 14 ++++++- kernel/bpf/btf.c | 18 ++++++++ kernel/bpf/helpers.c | 76 +++++++++++++++++++++++++++------- kernel/bpf/verifier.c | 70 ++++++++++++++++++++++++++++++- net/core/filter.c | 67 ++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 14 ++++++- 8 files changed, 270 insertions(+), 21 deletions(-)include/linux/bpf.h +8
-1@@ -607,11 +607,14 @@ enum bpf_type_flag {*/ NON_OWN_REF =BIT (14 +BPF_BASE_TYPE_BITS ), +/* DYNPTR points to sk_buff */ +DYNPTR_TYPE_SKB =BIT (15 +BPF_BASE_TYPE_BITS ), + __BPF_TYPE_FLAG_MAX, __BPF_TYPE_LAST_FLAG = __BPF_TYPE_FLAG_MAX - 1, }; -#define DYNPTR_TYPE_FLAG_MASK (DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF) +#define DYNPTR_TYPE_FLAG_MASK (DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF | DYNPTR_TYPE_SKB)/* Max number of base types. */ #define BPF_BASE_TYPE_LIMIT (1UL << BPF_BASE_TYPE_BITS)@@ -1146,6 +1149,8 @@ enum bpf_dynptr_type {BPF_DYNPTR_TYPE_LOCAL ,/* Underlying data is a ringbuf record */ BPF_DYNPTR_TYPE_RINGBUF , +/* Underlying data is a sk_buff */ + BPF_DYNPTR_TYPE_SKB, };int bpf_dynptr_check_size (u32 size);@@ -2846,6 +2851,8 @@ u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,struct bpf_insn *insn_buf, struct bpf_prog *prog, u32 *target_size); +int bpf_dynptr_from_skb_rdonly (struct sk_buff *skb,u64 flags, +struct bpf_dynptr_kern *ptr);#else static inline bool bpf_sock_common_is_valid_access(int off, int size, enum bpf_access_type type,
include/linux/bpf.h static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type, · line 2880
Quoted 7 lines
include/linux/bpf.h +4
-0@@ -2867,6 +2874,11 @@ static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type, {return 0 ; } +static inline int bpf_dynptr_from_skb_rdonly (struct sk_buff *skb,u64 flags, +struct bpf_dynptr_kern *ptr) +{ +return 0 ;
should this return -EOPNOTSUPP instead?
Quoted 4 lines
include/linux/bpf.h +1
-0+}#endif #ifdef CONFIG_INET
[...]
PATCH v10 7/9 bpf: Add xdp dynptrs
In reply to Joanne KoongAdd xdp dynptrs, which are dynptrs whose underlying pointer points to a xdp_buff. The dynptr acts on xdp data. xdp dynptrs have two main benefits. One is that they allow operations on sizes that are not statically known at compile-time (eg variable-sized accesses). Another is that parsing the packet data through dynptrs (instead of through direct access of xdp->data and xdp->data_end) can be more ergonomic and less brittle (eg does not need manual if checking for being within bounds of data_end).
For reads and writes on the dynptr, this includes reading/writing from/to and across fragments. Data slices through the bpf_dynptr_data API are not supported; instead bpf_dynptr_slice() and bpf_dynptr_slice_rdwr() should be used.
For examples of how xdp dynptrs can be used, please see the attached selftests.
--- include/linux/bpf.h | 8 +++++++- include/linux/filter.h | 14 +++++++++++++ include/uapi/linux/bpf.h | 3 +-- kernel/bpf/helpers.c | 9 ++++++++- kernel/bpf/verifier.c | 10 +++++++++ net/core/filter.c | 37 ++++++++++++++++++++++++++++++++-- tools/include/uapi/linux/bpf.h | 3 +-- 7 files changed, 76 insertions(+), 8 deletions(-)
include/linux/bpf.h +7 -1
@@ -610,11 +610,15 @@ enum bpf_type_flag {/* DYNPTR points to sk_buff */ DYNPTR_TYPE_SKB =BIT (15 +BPF_BASE_TYPE_BITS ), +/* DYNPTR points to xdp_buff */ +DYNPTR_TYPE_XDP =BIT (16 +BPF_BASE_TYPE_BITS ), + __BPF_TYPE_FLAG_MAX, __BPF_TYPE_LAST_FLAG = __BPF_TYPE_FLAG_MAX - 1, }; -#define DYNPTR_TYPE_FLAG_MASK (DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF | DYNPTR_TYPE_SKB) +#define DYNPTR_TYPE_FLAG_MASK (DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF | DYNPTR_TYPE_SKB \ + | DYNPTR_TYPE_XDP)/* Max number of base types. */ #define BPF_BASE_TYPE_LIMIT (1UL << BPF_BASE_TYPE_BITS)
@@ -1151,6 +1155,8 @@ enum bpf_dynptr_type {BPF_DYNPTR_TYPE_RINGBUF ,/* Underlying data is a sk_buff */ BPF_DYNPTR_TYPE_SKB , +/* Underlying data is a xdp_buff */ + BPF_DYNPTR_TYPE_XDP, };int bpf_dynptr_check_size (u32 size);
include/linux/filter.h +14 -0
@@ -1546,6 +1546,8 @@ static __always_inline int __bpf_xdp_redirect_map(struct bpf_map *map, u64 indexint __bpf_skb_load_bytes (const struct sk_buff *skb,u32 offset,void *to,u32 len);int __bpf_skb_store_bytes (struct sk_buff *skb,u32 offset,const void *from,u32 len,u64 flags); +int __bpf_xdp_load_bytes (struct xdp_buff *xdp,u32 offset,void *buf,u32 len); +int __bpf_xdp_store_bytes (struct xdp_buff *xdp,u32 offset,void *buf,u32 len);#else /* CONFIG_NET */ static inline int __bpf_skb_load_bytes (const struct sk_buff *skb,u32 offset,void *to,u32 len)
@@ -1558,6 +1560,18 @@ static inline int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, {return -EOPNOTSUPP ; } + +static inline int __bpf_xdp_load_bytes (struct xdp_buff *xdp,u32 offset, +void *buf,u32 len) +{ +return -EOPNOTSUPP ; +} + +static inline int __bpf_xdp_store_bytes (struct xdp_buff *xdp,u32 offset, +void *buf,u32 len) +{ +return -EOPNOTSUPP ; +}#endif /* CONFIG_NET */ #endif /* __LINUX_FILTER_H__ */
include/uapi/linux/bpf.h +1 -2
@@ -5339,9 +5339,8 @@ union bpf_attr {* *len* must be a statically known value. The returned data slice * is invalidated whenever the dynptr is invalidated. * -* skb type dynptrs may not use bpf_dynptr_data. They should +* skb and xdp type dynptrs may not use bpf_dynptr_data. They should * instead use bpf_dynptr_slice and bpf_dynptr_slice_rdwr. -* * Return * Pointer to the underlying dynptr data, NULL if the dynptr is * read-only, if the dynptr is invalid, or if the offset and length
kernel/bpf/helpers.c +8 -1
@@ -1530,6 +1530,8 @@ BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, const struct bpf_dynptr_kernreturn 0 ;case BPF_DYNPTR_TYPE_SKB :return __bpf_skb_load_bytes (src->data, src->offset + offset, dst, len); +case BPF_DYNPTR_TYPE_XDP : +return __bpf_xdp_load_bytes (src->data, src->offset + offset, dst, len);default :WARN_ONCE (true,"bpf_dynptr_read: unknown dynptr type %d\n" , type);return -EFAULT ;
@@ -1576,6 +1578,10 @@ BPF_CALL_5(bpf_dynptr_write, const struct bpf_dynptr_kern *, dst, u32, offset, vcase BPF_DYNPTR_TYPE_SKB :return __bpf_skb_store_bytes (dst->data, dst->offset + offset, src, len, flags); +case BPF_DYNPTR_TYPE_XDP : +if (flags) +return -EINVAL ; +return __bpf_xdp_store_bytes (dst->data, dst->offset + offset, src, len);default :WARN_ONCE (true,"bpf_dynptr_write: unknown dynptr type %d\n" , type);return -EFAULT ;
@@ -1615,7 +1621,8 @@ BPF_CALL_3(bpf_dynptr_data, const struct bpf_dynptr_kern *, ptr, u32, offset, u3case BPF_DYNPTR_TYPE_RINGBUF :return (unsigned long )(ptr->data + ptr->offset + offset);case BPF_DYNPTR_TYPE_SKB : -/* skb dynptrs should use bpf_dynptr_slice / bpf_dynptr_slice_rdwr */ +case BPF_DYNPTR_TYPE_XDP : +/* skb and xdp dynptrs should use bpf_dynptr_slice / bpf_dynptr_slice_rdwr */ return 0 ;default :WARN_ONCE (true,"bpf_dynptr_data: unknown dynptr type %d\n" , type);
kernel/bpf/verifier.c +10 -0
@@ -752,6 +752,8 @@ static enum bpf_dynptr_type arg_to_dynptr_type(enum bpf_arg_type arg_type)return BPF_DYNPTR_TYPE_RINGBUF ;case DYNPTR_TYPE_SKB :return BPF_DYNPTR_TYPE_SKB ; +case DYNPTR_TYPE_XDP : +return BPF_DYNPTR_TYPE_XDP ;default :return BPF_DYNPTR_TYPE_INVALID ; }
@@ -6291,6 +6293,9 @@ int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx,case DYNPTR_TYPE_SKB : err_extra ="skb " ;break ; +case DYNPTR_TYPE_XDP : + err_extra ="xdp " ; +break ;default : err_extra ="<unknown>" ;break ;
@@ -8917,6 +8922,7 @@ enum special_kfunc_type { KF_bpf_rbtree_add, KF_bpf_rbtree_first, KF_bpf_dynptr_from_skb, + KF_bpf_dynptr_from_xdp, };BTF_SET_START (special_kfunc_set)
@@ -8932,6 +8938,7 @@ BTF_ID(func, bpf_rbtree_remove) BTF_ID(func, bpf_rbtree_add) BTF_ID(func, bpf_rbtree_first) BTF_ID(func, bpf_dynptr_from_skb) +BTF_ID(func, bpf_dynptr_from_xdp)BTF_SET_END (special_kfunc_set)BTF_ID_LIST (special_kfunc_list)
@@ -8949,6 +8956,7 @@ BTF_ID(func, bpf_rbtree_remove) BTF_ID(func, bpf_rbtree_add) BTF_ID(func, bpf_rbtree_first) BTF_ID(func, bpf_dynptr_from_skb) +BTF_ID(func, bpf_dynptr_from_xdp)static bool is_kfunc_bpf_rcu_read_lock (struct bpf_kfunc_call_arg_meta *meta) {
@@ -9700,6 +9708,8 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb]) dynptr_arg_type |=MEM_UNINIT |DYNPTR_TYPE_SKB ; +else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_xdp]) + dynptr_arg_type |=MEM_UNINIT |DYNPTR_TYPE_XDP ;else dynptr_arg_type |=MEM_RDONLY ;
net/core/filter.c +35 -2
@@ -3839,7 +3839,7 @@ static const struct bpf_func_proto sk_skb_change_head_proto = { .arg3_type = ARG_ANYTHING, }; -BPF_CALL_1(bpf_xdp_get_buff_len, struct xdp_buff*, xdp) +BPF_CALL_1(bpf_xdp_get_buff_len, struct xdp_buff*, xdp) {return xdp_get_buff_len (xdp); }
@@ -3999,6 +3999,11 @@ static const struct bpf_func_proto bpf_xdp_load_bytes_proto = { .arg4_type = ARG_CONST_SIZE, }; +int __bpf_xdp_load_bytes (struct xdp_buff *xdp,u32 offset,void *buf,u32 len) +{ +return ____bpf_xdp_load_bytes (xdp, offset, buf, len); +} + BPF_CALL_4(bpf_xdp_store_bytes, struct xdp_buff *, xdp, u32, offset, void *, buf, u32, len) {
@@ -4026,6 +4031,11 @@ static const struct bpf_func_proto bpf_xdp_store_bytes_proto = { .arg4_type = ARG_CONST_SIZE, }; +int __bpf_xdp_store_bytes (struct xdp_buff *xdp,u32 offset,void *buf,u32 len) +{ +return ____bpf_xdp_store_bytes (xdp, offset, buf, len); +} +static int bpf_xdp_frags_increase_tail (struct xdp_buff *xdp,int offset) {struct skb_shared_info *sinfo =xdp_get_shared_info_from_buff (xdp);
@@ -11635,6 +11645,19 @@ __bpf_kfunc int bpf_dynptr_from_skb(struct sk_buff *skb, u64 flags,return 0 ; } + +__bpf_kfunc int bpf_dynptr_from_xdp(struct xdp_buff *xdp, u64 flags, +struct bpf_dynptr_kern *ptr) +{ +if (flags) { +bpf_dynptr_set_null (ptr); +return -EINVAL ; + } + +bpf_dynptr_init (ptr, xdp,BPF_DYNPTR_TYPE_XDP ,0 ,xdp_get_buff_len (xdp)); + +return 0 ; +}__diag_pop (); int bpf_dynptr_from_skb_rdonly(struct sk_buff *skb, u64 flags,
@@ -11655,11 +11678,20 @@ BTF_SET8_START(bpf_kfunc_check_set_skb) BTF_ID_FLAGS(func, bpf_dynptr_from_skb)BTF_SET8_END (bpf_kfunc_check_set_skb) +BTF_SET8_START (bpf_kfunc_check_set_xdp ) +BTF_ID_FLAGS (func, bpf_dynptr_from_xdp) +BTF_SET8_END (bpf_kfunc_check_set_xdp) + static const struct btf_kfunc_id_set bpf_kfunc_set_skb = { .owner =THIS_MODULE , .set = &bpf_kfunc_check_set_skb, }; +static const struct btf_kfunc_id_set bpf_kfunc_set_xdp = { + .owner =THIS_MODULE , + .set = &bpf_kfunc_check_set_xdp, +}; +static int __init bpf_kfunc_init (void ) {int ret;
@@ -11672,6 +11704,7 @@ static int __init bpf_kfunc_init(void) ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_LWT_OUT , &bpf_kfunc_set_skb); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_LWT_IN , &bpf_kfunc_set_skb); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_LWT_XMIT , &bpf_kfunc_set_skb); -return ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_LWT_SEG6LOCAL , &bpf_kfunc_set_skb); + ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_LWT_SEG6LOCAL , &bpf_kfunc_set_skb); +return ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_XDP , &bpf_kfunc_set_xdp); }late_initcall (bpf_kfunc_init);
tools/include/uapi/linux/bpf.h +1 -2
@@ -5339,9 +5339,8 @@ union bpf_attr {* *len* must be a statically known value. The returned data slice * is invalidated whenever the dynptr is invalidated. * -* skb type dynptrs may not use bpf_dynptr_data. They should +* skb and xdp type dynptrs may not use bpf_dynptr_data. They should * instead use bpf_dynptr_slice and bpf_dynptr_slice_rdwr. -* * Return * Pointer to the underlying dynptr data, NULL if the dynptr is * read-only, if the dynptr is invalid, or if the offset and length
Signature · 1 line
2.30.2
PATCH v10 8/9 bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr
In reply to Joanne KoongTwo new kfuncs are added, bpf_dynptr_slice and bpf_dynptr_slice_rdwr. The user must pass in a buffer to store the contents of the data slice if a direct pointer to the data cannot be obtained.
For skb and xdp type dynptrs, these two APIs are the only way to obtain a data slice. However, for other types of dynptrs, there is no difference between bpf_dynptr_slice(_rdwr) and bpf_dynptr_data.
For skb type dynptrs, the data is copied into the user provided buffer if any of the data is not in the linear portion of the skb. For xdp type dynptrs, the data is copied into the user provided buffer if the data is between xdp frags.
If the skb is cloned and a call to bpf_dynptr_data_rdwr is made, then the skb will be uncloned (see bpf_unclone_prologue()).
Please note that any bpf_dynptr_write() automatically invalidates any prior data slices of the skb dynptr. This is because the skb may be cloned or may need to pull its paged buffer into the head. As such, any bpf_dynptr_write() will automatically have its prior data slices invalidated, even if the write is to data in the skb head of an uncloned skb. Please note as well that any other helper calls that change the underlying packet buffer (eg bpf_skb_pull_data()) invalidates any data slices of the skb dynptr as well, for the same reasons.
--- include/linux/filter.h | 14 ++++++ include/uapi/linux/bpf.h | 5 ++ kernel/bpf/helpers.c | 91 ++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 88 ++++++++++++++++++++++++++++++-- net/core/filter.c | 6 +-- tools/include/uapi/linux/bpf.h | 5 ++ 6 files changed, 202 insertions(+), 7 deletions(-)
include/linux/filter.h +14 -0
@@ -1548,6 +1548,9 @@ int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len, u64 flags);int __bpf_xdp_load_bytes (struct xdp_buff *xdp,u32 offset,void *buf,u32 len);int __bpf_xdp_store_bytes (struct xdp_buff *xdp,u32 offset,void *buf,u32 len); +void *bpf_xdp_pointer (struct xdp_buff *xdp,u32 offset,u32 len); +void bpf_xdp_copy_buf (struct xdp_buff *xdp,unsigned long off, +void *buf,unsigned long len,bool flush);#else /* CONFIG_NET */ static inline int __bpf_skb_load_bytes (const struct sk_buff *skb,u32 offset,void *to,u32 len)
@@ -1572,6 +1575,17 @@ static inline int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, {return -EOPNOTSUPP ; } + +static inline void *bpf_xdp_pointer (struct xdp_buff *xdp,u32 offset,u32 len) +{ +return NULL ; +} + +static inline void *bpf_xdp_copy_buf (struct xdp_buff *xdp,unsigned long off,void *buf, +unsigned long len,bool flush) +{ +return NULL ; +}#endif /* CONFIG_NET */ #endif /* __LINUX_FILTER_H__ */
include/uapi/linux/bpf.h +5 -0
@@ -5324,6 +5324,11 @@ union bpf_attr {* *flags* must be 0 except for skb-type dynptrs. * * For skb-type dynptrs: +* * All data slices of the dynptr are automatically +* invalidated after **bpf_dynptr_write**\ (). This is +* because writing may pull the skb and change the +* underlying packet buffer. +* * * For *flags*, please see the flags accepted by * **bpf_skb_store_bytes**\ (). * Return
kernel/bpf/helpers.c +91 -0
109 lines not on this page · raw message
kernel/bpf/verifier.c +84 -4
165 lines not on this page · raw message
net/core/filter.c +3 -3
@@ -3894,8 +3894,8 @@ static const struct bpf_func_proto bpf_xdp_adjust_head_proto = { .arg2_type = ARG_ANYTHING, }; -static void bpf_xdp_copy_buf (struct xdp_buff *xdp,unsigned long off, -void *buf,unsigned long len,bool flush) +void bpf_xdp_copy_buf (struct xdp_buff *xdp,unsigned long off, +void *buf,unsigned long len,bool flush) {unsigned long ptr_len, ptr_off =0 ;skb_frag_t *next_frag, *end_frag;
@@ -3941,7 +3941,7 @@ static void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off, } } -static void *bpf_xdp_pointer (struct xdp_buff *xdp,u32 offset,u32 len) +void *bpf_xdp_pointer (struct xdp_buff *xdp,u32 offset,u32 len) {struct skb_shared_info *sinfo =xdp_get_shared_info_from_buff (xdp);u32 size = xdp->data_end - xdp->data;
tools/include/uapi/linux/bpf.h +5 -0
@@ -5324,6 +5324,11 @@ union bpf_attr {* *flags* must be 0 except for skb-type dynptrs. * * For skb-type dynptrs: +* * All data slices of the dynptr are automatically +* invalidated after **bpf_dynptr_write**\ (). This is +* because writing may pull the skb and change the +* underlying packet buffer. +* * * For *flags*, please see the flags accepted by * **bpf_skb_store_bytes**\ (). * Return
Signature · 1 line
2.30.2
Re: [PATCH v10 bpf-next 8/9] bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr
Quoted 184 lines · Joanne Koong
Two new kfuncs are added,
bpf_dynptr_sliceandbpf_dynptr_slice_rdwr. The user must pass in a buffer to store the contents of the data slice if a direct pointer to the data cannot be obtained.For skb and xdp type dynptrs, these two APIs are the only way to obtain a data slice. However, for other types of dynptrs, there is no difference between
bpf_dynptr_slice(_rdwr)andbpf_dynptr_data.For skb type dynptrs, the data is copied into the user provided buffer if any of the data is not in the linear portion of the skb. For xdp type dynptrs, the data is copied into the user provided buffer if the data is between xdp frags.
If the skb is cloned and a call to
bpf_dynptr_data_rdwris made, then the skb will be uncloned (seebpf_unclone_prologue()).Please note that any
bpf_dynptr_write()automatically invalidates any prior data slices of the skb dynptr. This is because the skb may be cloned or may need to pull its paged buffer into the head. As such, anybpf_dynptr_write()will automatically have its prior data slices invalidated, even if the write is to data in the skb head of an uncloned skb. Please note as well that any other helper calls that change the underlying packet buffer (egbpf_skb_pull_data()) invalidates any data slices of the skb dynptr as well, for the same reasons.Signed-off-by Joanne Koong <[email protected]>--- include/linux/filter.h | 14 ++++++ include/uapi/linux/bpf.h | 5 ++ kernel/bpf/helpers.c | 91 ++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 88 ++++++++++++++++++++++++++++++-- net/core/filter.c | 6 +-- tools/include/uapi/linux/bpf.h | 5 ++ 6 files changed, 202 insertions(+), 7 deletions(-)include/linux/filter.h +14
-0@@ -1548,6 +1548,9 @@ int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len, u64 flags);int __bpf_xdp_load_bytes (struct xdp_buff *xdp,u32 offset,void *buf,u32 len);int __bpf_xdp_store_bytes (struct xdp_buff *xdp,u32 offset,void *buf,u32 len); +void *bpf_xdp_pointer (struct xdp_buff *xdp,u32 offset,u32 len); +void bpf_xdp_copy_buf (struct xdp_buff *xdp,unsigned long off, +void *buf,unsigned long len,bool flush);#else /* CONFIG_NET */ static inline int __bpf_skb_load_bytes (const struct sk_buff *skb,u32 offset,void *to,u32 len)@@ -1572,6 +1575,17 @@ static inline int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, {return -EOPNOTSUPP ; } + +static inline void *bpf_xdp_pointer (struct xdp_buff *xdp,u32 offset,u32 len) +{ +return NULL ; +} + +static inline void *bpf_xdp_copy_buf (struct xdp_buff *xdp,unsigned long off,void *buf, +unsigned long len,bool flush) +{ +return NULL ; +}#endif /* CONFIG_NET */ #endif /* __LINUX_FILTER_H__ */ include/uapi/linux/bpf.h +5
-0@@ -5324,6 +5324,11 @@ union bpf_attr {* *flags* must be 0 except for skb-type dynptrs. * * For skb-type dynptrs: +* * All data slices of the dynptr are automatically +* invalidated after **bpf_dynptr_write**\ (). This is +* because writing may pull the skb and change the +* underlying packet buffer. +* * * For *flags*, please see the flags accepted by * **bpf_skb_store_bytes**\ (). * Return kernel/bpf/helpers.c +88
-0@@ -2177,6 +2177,94 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)return p; } +/** +* bpf_dynptr_slice - Obtain a read-only pointer to the dynptr data. +* +* For non-skb and non-xdp type dynptrs, there is no difference between +* bpf_dynptr_slice and bpf_dynptr_data. +* +* If the intention is to write to the data slice, please use +* bpf_dynptr_slice_rdwr. +* +* @ptr: The dynptr whose data slice to retrieve +* @offset: Offset into the dynptr +* @buffer: User-provided buffer to copy contents into +* @buffer__sz: Size (in bytes) of the buffer. This is the length of the +* requested slice +* +* @returns: NULL if the call failed (eg invalid dynptr), pointer to a read-only +* data slice (can be either direct pointer to the data or a pointer to the user +* provided buffer, with its contents containing the data, if unable to obtain +* direct pointer) +*/ +__bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset, +void *buffer,u32 buffer__sz) +{ +enum bpf_dynptr_type type; +u32 len = buffer__sz; +int err; + +if (!ptr->data) +return 0 ; + + err =bpf_dynptr_check_off_len (ptr, offset, len); +if (err) +return 0 ; + + type =bpf_dynptr_get_type (ptr); + +switch (type) { +case BPF_DYNPTR_TYPE_LOCAL : +case BPF_DYNPTR_TYPE_RINGBUF : +return ptr->data + ptr->offset + offset; +case BPF_DYNPTR_TYPE_SKB : +return skb_header_pointer (ptr->data, ptr->offset + offset, len, buffer); +case BPF_DYNPTR_TYPE_XDP : + { +void *xdp_ptr =bpf_xdp_pointer (ptr->data, ptr->offset + offset, len); +if (xdp_ptr) +return xdp_ptr; + +bpf_xdp_copy_buf (ptr->data, ptr->offset + offset, buffer, len, false); +return buffer; + } +default : +WARN_ONCE (true,"unknown dynptr type %d\n" , type); +return 0 ; + } +} + +/** +* bpf_dynptr_slice_rdwr - Obtain a pointer to the dynptr data. +* +* For non-skb and non-xdp type dynptrs, there is no difference between +* bpf_dynptr_slice and bpf_dynptr_data. +* +* @ptr: The dynptr whose data slice to retrieve +* @offset: Offset into the dynptr +* @buffer: User-provided buffer to copy contents into +* @buffer__sz: Size (in bytes) of the buffer. This is the length of the +* requested slice +* +* @returns: NULL if the call failed (eg invalid dynptr), pointer to a +* data slice (can be either direct pointer to the data or a pointer to the user +* provided buffer, with its contents containing the data, if unable to obtain +* direct pointer) +*/ +__bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 offset, +void *buffer,u32 buffer__sz) +{ +if (!ptr->data ||bpf_dynptr_is_rdonly (ptr)) +return 0 ; + +/* bpf_dynptr_slice_rdwr is the same logic as bpf_dynptr_slice. +* +* For skb-type dynptrs, the verifier has already ensured that the skb +* head is writable (see bpf_unclone_prologue()). +*/ +return bpf_dynptr_slice (ptr, offset, buffer, buffer__sz); +} + __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj) {return obj;
kernel/bpf/helpers.c BTF_ID_FLAGS(func, bpf_cgroup_release, KF_RELEASE) · line 2316
Quoted 5 lines
kernel/bpf/helpers.c +2
-0@@ -2224,6 +2312,8 @@ BTF_ID_FLAGS(func, bpf_cgroup_release, KF_RELEASE) BTF_ID_FLAGS(func, bpf_cgroup_ancestor, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL)#endif BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_dynptr_slice, KF_RET_NULL) +BTF_ID_FLAGS (func, bpf_dynptr_slice_rdwr,KF_RET_NULL )
upon further thought, I think these two should be listed in the "common_btf_ids" BTF set, not "generic_btf_ids", since common_btf_ids is registered for BPF_PROG_TYPE_UNSPEC prog types
Quoted 235 lines
kernel/bpf/helpers.c +1
-0BTF_SET8_END (generic_btf_ids) static const struct btf_kfunc_id_set generic_kfunc_set = {@@ -2271,6 +2361,7 @@ static int __init kfunc_init(void) ret =register_btf_kfunc_id_set (BPF_PROG_TYPE_TRACING , &generic_kfunc_set); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SCHED_CLS , &generic_kfunc_set); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_STRUCT_OPS , &generic_kfunc_set); + ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_XDP , &generic_kfunc_set); ret = ret ?:register_btf_id_dtor_kfuncs (generic_dtors,ARRAY_SIZE (generic_dtors),THIS_MODULE );kernel/bpf/verifier.c +84
-4@@ -759,6 +759,22 @@ static enum bpf_dynptr_type arg_to_dynptr_type(enum bpf_arg_type arg_type) } } +static enum bpf_type_flag get_dynptr_type_flag (enum bpf_dynptr_type type) +{ +switch (type) { +case BPF_DYNPTR_TYPE_LOCAL : +return DYNPTR_TYPE_LOCAL ; +case BPF_DYNPTR_TYPE_RINGBUF : +return DYNPTR_TYPE_RINGBUF ; +case BPF_DYNPTR_TYPE_SKB : +return DYNPTR_TYPE_SKB ; +case BPF_DYNPTR_TYPE_XDP : +return DYNPTR_TYPE_XDP ; +default : +return 0 ; + } +} +static bool dynptr_type_refcounted (enum bpf_dynptr_type type) {return type ==BPF_DYNPTR_TYPE_RINGBUF ;@@ -1677,6 +1693,12 @@ static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) reg->type ==PTR_TO_PACKET_END ; } +static bool reg_is_dynptr_slice_pkt (const struct bpf_reg_state *reg) +{ +return base_type (reg->type) ==PTR_TO_MEM && + (reg->type &DYNPTR_TYPE_SKB || reg->type &DYNPTR_TYPE_XDP ); +} +/* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ static bool reg_is_init_pkt_pointer (const struct bpf_reg_state *reg,enum bpf_reg_type which)@@ -7404,6 +7426,9 @@ static int check_func_proto(const struct bpf_func_proto *fn, int func_id)/* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] * are now invalid, so turn them into unknown SCALAR_VALUE. +* +* This also applies to dynptr slices belonging to skb and xdp dynptrs, +* since these slices point to packet data. */ static void clear_all_pkt_pointers (struct bpf_verifier_env *env) {@@ -7411,7 +7436,7 @@ static void clear_all_pkt_pointers(struct bpf_verifier_env *env)struct bpf_reg_state *reg;bpf_for_each_reg_in_vstate (env->cur_state, state, reg, ({ -if (reg_is_pkt_pointer_any (reg)) +if (reg_is_pkt_pointer_any (reg) ||reg_is_dynptr_slice_pkt (reg))__mark_reg_unknown (env, reg); })); }@@ -8667,6 +8692,11 @@ struct bpf_kfunc_call_arg_meta {struct {struct btf_field *field; } arg_rbtree_root; +struct { +enum bpf_dynptr_type type; +u32 id; + } initialized_dynptr; +u64 mem_size; };static bool is_kfunc_acquire (struct bpf_kfunc_call_arg_meta *meta)@@ -8923,6 +8953,8 @@ enum special_kfunc_type { KF_bpf_rbtree_first, KF_bpf_dynptr_from_skb, KF_bpf_dynptr_from_xdp, + KF_bpf_dynptr_slice, + KF_bpf_dynptr_slice_rdwr, };BTF_SET_START (special_kfunc_set)@@ -8939,6 +8971,8 @@ BTF_ID(func, bpf_rbtree_add) BTF_ID(func, bpf_rbtree_first) BTF_ID(func, bpf_dynptr_from_skb) BTF_ID(func, bpf_dynptr_from_xdp) +BTF_ID(func, bpf_dynptr_slice) +BTF_ID(func, bpf_dynptr_slice_rdwr)BTF_SET_END (special_kfunc_set)BTF_ID_LIST (special_kfunc_list)@@ -8957,6 +8991,8 @@ BTF_ID(func, bpf_rbtree_add) BTF_ID(func, bpf_rbtree_first) BTF_ID(func, bpf_dynptr_from_skb) BTF_ID(func, bpf_dynptr_from_xdp) +BTF_ID(func, bpf_dynptr_slice) +BTF_ID(func, bpf_dynptr_slice_rdwr)static bool is_kfunc_bpf_rcu_read_lock (struct bpf_kfunc_call_arg_meta *meta) {@@ -9710,12 +9746,24 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ dynptr_arg_type |=MEM_UNINIT |DYNPTR_TYPE_SKB ; else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_xdp]) dynptr_arg_type |= MEM_UNINIT | DYNPTR_TYPE_XDP; - else + else if (reg->type == CONST_PTR_TO_DYNPTR) dynptr_arg_type |= MEM_RDONLY; ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type);if (ret <0 )return ret; + +if (!(dynptr_arg_type &MEM_UNINIT )) { +int id =dynptr_id (env, reg); + +if (id <0 ) { +verbose (env,"verifier internal error: failed to obtain dynptr id\n" ); +return id; + } + meta->initialized_dynptr.id = id; + meta->initialized_dynptr.type =dynptr_get_type (env, reg); + } +break ; }case KF_ARG_PTR_TO_LIST_HEAD :@@ -9966,8 +10014,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, } } -for (i =0 ; i <CALLER_SAVED_REGS ; i++) -mark_reg_not_init (env, regs, caller_saved[i]); +mark_reg_not_init (env, regs, caller_saved[BPF_REG_0 ]);/* Check return type */ t =btf_type_skip_modifiers (desc_btf, func_proto->type,NULL );@@ -10053,6 +10100,36 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, regs[BPF_REG_0 ].type =PTR_TO_BTF_ID |PTR_UNTRUSTED ; regs[BPF_REG_0 ].btf = desc_btf; regs[BPF_REG_0 ].btf_id = meta.arg_constant.value; + }else if (meta.func_id == special_kfunc_list[KF_bpf_dynptr_slice] || + meta.func_id == special_kfunc_list[KF_bpf_dynptr_slice_rdwr]) { +enum bpf_type_flag type_flag =get_dynptr_type_flag (meta.initialized_dynptr.type); + +mark_reg_known_zero (env, regs,BPF_REG_0 ); + +if (!tnum_is_const (regs[BPF_REG_4 ].var_off)) { +verbose (env,"mem_size must be a constant\n" ); +return -EINVAL ; + } + regs[BPF_REG_0 ].mem_size = regs[BPF_REG_4 ].var_off.value; + +/* PTR_MAYBE_NULL will be added when is_kfunc_ret_null is checked */ + regs[BPF_REG_0 ].type =PTR_TO_MEM | type_flag; + +if (meta.func_id == special_kfunc_list[KF_bpf_dynptr_slice]) + regs[BPF_REG_0 ].type |=MEM_RDONLY ; +else + env->seen_direct_write = true; + +if (!meta.initialized_dynptr.id) { +verbose (env,"verifier internal error: no dynptr id\n" ); +return -EFAULT ; + } + regs[BPF_REG_0 ].dynptr_id = meta.initialized_dynptr.id; + +/* we don't need to set BPF_REG_0's ref obj id +* because packet slices are not refcounted (see +* dynptr_type_refcounted) +*/ }else {verbose (env,"kernel function %s unhandled dynamic return type\n" , meta.func_name);@@ -10112,6 +10189,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, regs[BPF_REG_0 ].id = ++env->id_gen; }/* else { add_kfunc_call() ensures it is btf_type_is_void(t) } */ +for (i =BPF_REG_1 ; i <CALLER_SAVED_REGS ; i++) +mark_reg_not_init (env, regs, caller_saved[i]); + nargs =btf_type_vlen (func_proto); args = (const struct btf_param *)(func_proto +1 );for (i =0 ; i < nargs; i++) {net/core/filter.c +3
-3@@ -3894,8 +3894,8 @@ static const struct bpf_func_proto bpf_xdp_adjust_head_proto = { .arg2_type = ARG_ANYTHING, }; -static void bpf_xdp_copy_buf (struct xdp_buff *xdp,unsigned long off, -void *buf,unsigned long len,bool flush) +void bpf_xdp_copy_buf (struct xdp_buff *xdp,unsigned long off, +void *buf,unsigned long len,bool flush) {unsigned long ptr_len, ptr_off =0 ;skb_frag_t *next_frag, *end_frag;@@ -3941,7 +3941,7 @@ static void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off, } } -static void *bpf_xdp_pointer (struct xdp_buff *xdp,u32 offset,u32 len) +void *bpf_xdp_pointer (struct xdp_buff *xdp,u32 offset,u32 len) {struct skb_shared_info *sinfo =xdp_get_shared_info_from_buff (xdp);u32 size = xdp->data_end - xdp->data;tools/include/uapi/linux/bpf.h +5
-1@@ -5324,6 +5324,11 @@ union bpf_attr {* *flags* must be 0 except for skb-type dynptrs. * * For skb-type dynptrs: +* * All data slices of the dynptr are automatically +* invalidated after **bpf_dynptr_write**\ (). This is +* because writing may pull the skb and change the +* underlying packet buffer. +* * * For *flags*, please see the flags accepted by * **bpf_skb_store_bytes**\ (). * Return -- 2.30.2
Re: [PATCH v10 bpf-next 8/9] bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr
In reply to Joanne KoongQuoted 7 lines · Joanne Koong
kernel/bpf/helpers.c +7
-0+ +/** +* bpf_dynptr_slice_rdwr - Obtain a pointer to the dynptr data. +* +* For non-skb and non-xdp type dynptrs, there is no difference between +* bpf_dynptr_slice and bpf_dynptr_data. +*
kernel/bpf/helpers.c __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid) · line 2252
Quoted 10 lines
kernel/bpf/helpers.c +10
-0+* @ptr: The dynptr whose data slice to retrieve +* @offset: Offset into the dynptr +* @buffer: User-provided buffer to copy contents into +* @buffer__sz: Size (in bytes) of the buffer. This is the length of the +* requested slice +* +* @returns: NULL if the call failed (eg invalid dynptr), pointer to a +* data slice (can be either direct pointer to the data or a pointer to the user +* provided buffer, with its contents containing the data, if unable to obtain +* direct pointer)
The doc probably should say that the returned pointer is writeable and the user must do if (ptr != buffer) bpf_dynptr_write() to reflect the changes.
Maybe document all kfuncs similar to Documentation/bpf/cpumasks.rst ?
Should we also document that bpf_dynptr_slice[_rdwr] do not change skb configuration and because of that the ctx->data/data_end pointers are not invalidated by either skb_header_pointer or bpf_xdp_pointer ?
kernel/bpf/helpers.c __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid) · line 2264
Quoted 12 lines
+ */
kernel/bpf/helpers.c +11
-0+__bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 offset, +void *buffer,u32 buffer__sz) +{ +if (!ptr->data ||bpf_dynptr_is_rdonly (ptr)) +return 0 ; + +/* bpf_dynptr_slice_rdwr is the same logic as bpf_dynptr_slice. +* +* For skb-type dynptrs, the verifier has already ensured that the skb +* head is writable (see bpf_unclone_prologue()). +*/
This is way too terse. It needs much more detailed comment explaining why it's safe to write into the returned pointer.
For example it's far from obvious that bpf_dynptr_slice()->skb_header_pointer() returns a pointer to a head or copies into a buffer. _only_. and no other logic. Without looking into implementation details one could come up with skb_header_pointer() behavior that returns a pointer to a middle part of a frag if {offset, len} combination allows. And in such case it will not be safe to write into such pointer.
Because bpf_unclone_prologue() only makes sure that the head is writeable.
One can look at bpf_unclone_prologue() and see that it's doing bpf_skb_pull_data(skb, 0); But without looking further it's also not at all obvious that arg2 == 0 means 'make head writeable'.
Also 'For skb-type dynptrs, the verifier has already ensured that the skb head is writable' is partially true.
skb-type dynptrs are available to cgroup-scoped skb hooks and there bpf_dynptr_slice_rdwr() will always be failing, since bpf_dynptr_is_rdonly() will be true.
It probably will be better user experience if the verifier rejects bpf_dynptr_slice_rdwr() in hooks where may_access_direct_pkt_data() returns false.
Quoted 3 lines
kernel/bpf/helpers.c +3
-0+return bpf_dynptr_slice (ptr, offset, buffer, buffer__sz); +} +
...
Quoted 9 lines
kernel/bpf/verifier.c +9
-0+ }else if (meta.func_id == special_kfunc_list[KF_bpf_dynptr_slice] || + meta.func_id == special_kfunc_list[KF_bpf_dynptr_slice_rdwr]) { +enum bpf_type_flag type_flag =get_dynptr_type_flag (meta.initialized_dynptr.type); + +mark_reg_known_zero (env, regs,BPF_REG_0 ); + +if (!tnum_is_const (regs[BPF_REG_4 ].var_off)) { +verbose (env,"mem_size must be a constant\n" ); +return -EINVAL ;
kernel/bpf/verifier.c static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 10121
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ } + regs[BPF_REG_0 ].mem_size = regs[BPF_REG_4 ].var_off.value; + +/* PTR_MAYBE_NULL will be added when is_kfunc_ret_null is checked */ + regs[BPF_REG_0 ].type =PTR_TO_MEM | type_flag; + +if (meta.func_id == special_kfunc_list[KF_bpf_dynptr_slice]) + regs[BPF_REG_0 ].type |=MEM_RDONLY ; +else + env->seen_direct_write = true;
This bit kinda makes it that bpf_dynptr_slice_rdwr() will "fail" in cg-skb hook, but it will do so with:
if (ops->gen_prologue || env->seen_direct_write) {
if (!ops->gen_prologue) {
verbose(env, "bpf verifier is misconfigured\n");
return -EINVAL;
}which will confuse users.
PATCH v10 9/9 selftests/bpf: tests for using dynptrs to parse skb and xdp buffers
In reply to Joanne KoongTest skb and xdp dynptr functionality in the following ways:
1) progs/test_cls_redirect_dynptr.c
* Rewrite "progs/test_cls_redirect.c" test to use dynptrs to parse skb data
* This is a great example of how dynptrs can be used to simplify a lot of the parsing logic for non-statically known values.
When measuring the user + system time between the original version vs. using dynptrs, and averaging the time for 10 runs (using "time ./test_progs -t cls_redirect"):
original version: 0.092 sec
with dynptrs: 0.078 sec
2) progs/test_xdp_dynptr.c
* Rewrite "progs/test_xdp.c" test to use dynptrs to parse xdp data
When measuring the user + system time between the original version vs. using dynptrs, and averaging the time for 10 runs (using "time ./test_progs -t xdp_attach"):
original version: 0.118 sec
with dynptrs: 0.094 sec
3) progs/test_l4lb_noinline_dynptr.c
* Rewrite "progs/test_l4lb_noinline.c" test to use dynptrs to parse skb data
When measuring the user + system time between the original version vs. using dynptrs, and averaging the time for 10 runs (using "time ./test_progs -t l4lb_all"):
original version: 0.062 sec
with dynptrs: 0.081 sec
For number of processed verifier instructions:
original version: 6268 insns
with dynptrs: 2588 insns
4) progs/test_parse_tcp_hdr_opt_dynptr.c
* Add sample code for parsing tcp hdr opt lookup using dynptrs. This logic is lifted from a real-world use case of packet parsing in katran [0], a layer 4 load balancer. The original version "progs/test_parse_tcp_hdr_opt.c" (not using dynptrs) is included here as well, for comparison.
When measuring the user + system time between the original version vs. using dynptrs, and averaging the time for 10 runs (using "time ./test_progs -t parse_tcp_hdr_opt"):
original version: 0.031 sec
with dynptrs: 0.045 sec
5) progs/dynptr_success.c
* Add test case "test_skb_readonly" for testing attempts at writes on a prog type with read-only skb ctx.
* Add "test_dynptr_skb_data" for testing that bpf_dynptr_data isn't supported for skb progs.
6) progs/dynptr_fail.c
* Add test cases "skb_invalid_data_slice{1,2,3,4}" and "xdp_invalid_data_slice{1,2}" for testing that helpers that modify the underlying packet buffer automatically invalidate the associated data slice.
* Add test cases "skb_invalid_ctx" and "xdp_invalid_ctx" for testing that prog types that do not support bpf_dynptr_from_skb/xdp don't have access to the API.
* Add test case "dynptr_slice_var_len{1,2}" for testing that variable-sized len can't be passed in to bpf_dynptr_slice
* Add test case "skb_invalid_slice_write" for testing that writes to a read-only data slice are rejected by the verifier.
* Add test case " data_slice_out_of_bounds_skb for testing that writes to an area outside the slice are rejected.
[0]
https://github.com/facebookincubator/katran/blob/main/katran/lib/bpf/pckt_parsing.h
--- .../selftests/bpf/prog_tests/cls_redirect.c | 25 + .../testing/selftests/bpf/prog_tests/dynptr.c | 69 +- .../selftests/bpf/prog_tests/l4lb_all.c | 2 + .../bpf/prog_tests/parse_tcp_hdr_opt.c | 93 ++ .../selftests/bpf/prog_tests/xdp_attach.c | 11 +- .../testing/selftests/bpf/progs/dynptr_fail.c | 274 ++++- .../selftests/bpf/progs/dynptr_success.c | 51 +- .../bpf/progs/test_cls_redirect_dynptr.c | 975 ++++++++++++++++++ .../bpf/progs/test_l4lb_noinline_dynptr.c | 486 +++++++++ .../bpf/progs/test_parse_tcp_hdr_opt.c | 119 +++ .../bpf/progs/test_parse_tcp_hdr_opt_dynptr.c | 118 +++ .../selftests/bpf/progs/test_xdp_dynptr.c | 257 +++++ .../selftests/bpf/test_tcp_hdr_options.h | 1 + 13 files changed, 2464 insertions(+), 17 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/parse_tcp_hdr_opt.c create mode 100644 tools/testing/selftests/bpf/progs/test_cls_redirect_dynptr.c create mode 100644 tools/testing/selftests/bpf/progs/test_l4lb_noinline_dynptr.c create mode 100644 tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt.c create mode 100644 tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt_dynptr.c create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_dynptr.c
tools/testing/selftests/bpf/prog_tests/cls_redirect.c +25 -0
@@ -13,6 +13,7 @@#include "progs/test_cls_redirect.h" #include "test_cls_redirect.skel.h" +#include "test_cls_redirect_dynptr.skel.h" #include "test_cls_redirect_subprogs.skel.h" #define ENCAP_IP INADDR_LOOPBACK
@@ -446,6 +447,28 @@ static void test_cls_redirect_common(struct bpf_program *prog)close_fds ((int *)conns,sizeof (conns) /sizeof (conns[0 ][0 ])); } +static void test_cls_redirect_dynptr (void ) +{ +struct test_cls_redirect_dynptr *skel; +int err; + + skel =test_cls_redirect_dynptr__open (); +if (!ASSERT_OK_PTR (skel,"skel_open" )) +return ; + + skel->rodata->ENCAPSULATION_IP =htonl (ENCAP_IP ); + skel->rodata->ENCAPSULATION_PORT =htons (ENCAP_PORT ); + + err =test_cls_redirect_dynptr__load (skel); +if (!ASSERT_OK (err,"skel_load" )) + goto cleanup; + +test_cls_redirect_common (skel->progs.cls_redirect); + +cleanup: +test_cls_redirect_dynptr__destroy (skel); +} +static void test_cls_redirect_inlined (void ) {struct test_cls_redirect *skel;
@@ -496,4 +519,6 @@ void test_cls_redirect(void)test_cls_redirect_inlined ();if (test__start_subtest ("cls_redirect_subprogs" ))test_cls_redirect_subprogs (); +if (test__start_subtest ("cls_redirect_dynptr" )) +test_cls_redirect_dynptr (); }
tools/testing/selftests/bpf/prog_tests/dynptr.c +56 -13
97 lines not on this page · raw message
tools/testing/selftests/bpf/prog_tests/l4lb_all.c +2 -0
@@ -93,4 +93,6 @@ void test_l4lb_all(void)test_l4lb ("test_l4lb.bpf.o" );if (test__start_subtest ("l4lb_noinline" ))test_l4lb ("test_l4lb_noinline.bpf.o" ); +if (test__start_subtest ("l4lb_noinline_dynptr" )) +test_l4lb ("test_l4lb_noinline_dynptr.bpf.o" ); }
tools/testing/selftests/bpf/prog_tests/parse_tcp_hdr_opt.c new file +93 -0
93 lines not on this page · raw message
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +9 -2
@@ -4,11 +4,10 @@#define IFINDEX_LO 1#define XDP_FLAGS_REPLACE (1U << 4) -void serial_test_xdp_attach (void ) +static void test_xdp_attach (const char *file) {__u32 duration =0 , id1, id2, id0 =0 , len;struct bpf_object *obj1, *obj2, *obj3; -const char *file ="./test_xdp.bpf.o" ;struct bpf_prog_info info = {};int err, fd1, fd2, fd3;LIBBPF_OPTS (bpf_xdp_attach_opts, opts);
@@ -85,3 +84,11 @@ void serial_test_xdp_attach(void) out_1:bpf_object__close (obj1); } + +void serial_test_xdp_attach (void ) +{ +if (test__start_subtest ("xdp_attach" )) +test_xdp_attach ("./test_xdp.bpf.o" ); +if (test__start_subtest ("xdp_attach_dynptr" )) +test_xdp_attach ("./test_xdp_dynptr.bpf.o" ); +}
tools/testing/selftests/bpf/progs/dynptr_fail.c +273 -1
308 lines not on this page · raw message
tools/testing/selftests/bpf/progs/dynptr_success.c +50 -1
66 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_cls_redirect_dynptr.c new file +975 -0
975 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_l4lb_noinline_dynptr.c new file +486 -0
486 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt.c new file +119 -0
119 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt_dynptr.c new file +118 -0
118 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_xdp_dynptr.c new file +257 -0
257 lines not on this page · raw message
tools/testing/selftests/bpf/test_tcp_hdr_options.h +1 -0
@@ -50,6 +50,7 @@ struct linum_err {#define TCPOPT_EOL 0#define TCPOPT_NOP 1 +#define TCPOPT_MSS 2#define TCPOPT_WINDOW 3#define TCPOPT_EXP 254
Signature · 1 line
2.30.2
Re: [PATCH v10 bpf-next 9/9] selftests/bpf: tests for using dynptrs to parse skb and xdp buffers
Quoted 1 line · Joanne Koong
Test skb and xdp dynptr functionality in the following ways:
One question on one of the usage examples:
[...]
Quoted 129 lines
tools/testing/selftests/bpf/progs/test_xdp_dynptr.c new file +123
-0@@ -0,0 +1,257 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Meta */ +#include <stddef.h> +#include <string.h> +#include <linux/bpf.h> +#include <linux/if_ether.h> +#include <linux/if_packet.h> +#include <linux/ip.h> +#include <linux/ipv6.h> +#include <linux/in.h> +#include <linux/udp.h> +#include <linux/tcp.h> +#include <linux/pkt_cls.h> +#include <sys/socket.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_endian.h> +#include "test_iptunnel_common.h" + +extern int bpf_dynptr_from_xdp(struct xdp_md *xdp, __u64 flags, + struct bpf_dynptr *ptr) __ksym; +extern void *bpf_dynptr_slice (const struct bpf_dynptr *ptr,__u32 offset, +void *buffer,__u32 buffer__sz) __ksym; +extern void *bpf_dynptr_slice_rdwr (const struct bpf_dynptr *ptr,__u32 offset, +void *buffer,__u32 buffer__sz) __ksym; + +const size_t tcphdr_sz =sizeof (struct tcphdr ); +const size_t udphdr_sz =sizeof (struct udphdr ); +const size_t ethhdr_sz =sizeof (struct ethhdr ); +const size_t iphdr_sz =sizeof (struct iphdr ); +const size_t ipv6hdr_sz =sizeof (struct ipv6hdr ); + +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(max_entries, 256); + __type(key, __u32); + __type(value, __u64); +} rxcnt SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, MAX_IPTNL_ENTRIES); + __type(key, struct vip); + __type(value, struct iptnl_info); +} vip2tnl SEC(".maps"); + +static __always_inline void count_tx(__u32 protocol) +{ +__u64 *rxcnt_count; + + rxcnt_count =bpf_map_lookup_elem (&rxcnt, &protocol); +if (rxcnt_count) + *rxcnt_count +=1 ; +} + +static __always_inline int get_dport(void *trans_data, __u8 protocol) +{ +struct tcphdr *th; +struct udphdr *uh; + +switch (protocol) { +case IPPROTO_TCP : + th = (struct tcphdr *)trans_data; +return th->dest; +case IPPROTO_UDP : + uh = (struct udphdr *)trans_data; +return uh->dest; +default : +return 0 ; + } +} + +static __always_inline void set_ethhdr(struct ethhdr *new_eth, +const struct ethhdr *old_eth, +const struct iptnl_info *tnl, +__be16 h_proto) +{ +memcpy (new_eth->h_source, old_eth->h_dest,sizeof (new_eth->h_source)); +memcpy (new_eth->h_dest, tnl->dmac,sizeof (new_eth->h_dest)); + new_eth->h_proto = h_proto; +} + +static __always_inline int handle_ipv4(struct xdp_md *xdp, struct bpf_dynptr *xdp_ptr) +{ +__u8 eth_buffer[ethhdr_sz + iphdr_sz + ethhdr_sz]; +__u8 iph_buffer_tcp[iphdr_sz + tcphdr_sz]; +__u8 iph_buffer_udp[iphdr_sz + udphdr_sz]; +struct bpf_dynptr new_xdp_ptr; +struct iptnl_info *tnl; +struct ethhdr *new_eth; +struct ethhdr *old_eth; +__u32 transport_hdr_sz; +struct iphdr *iph; +__u16 *next_iph; +__u16 payload_len; +struct vip vip = {}; +int dport; +__u32 csum =0 ; +int i; + +__builtin_memset (eth_buffer,0 ,sizeof (eth_buffer)); +__builtin_memset (iph_buffer_tcp,0 ,sizeof (iph_buffer_tcp)); +__builtin_memset (iph_buffer_udp,0 ,sizeof (iph_buffer_udp)); + +if (ethhdr_sz + iphdr_sz + tcphdr_sz > xdp->data_end - xdp->data) + iph =bpf_dynptr_slice (xdp_ptr, ethhdr_sz, iph_buffer_udp,sizeof (iph_buffer_udp)); +else + iph =bpf_dynptr_slice (xdp_ptr, ethhdr_sz, iph_buffer_tcp,sizeof (iph_buffer_tcp)); + +if (!iph) +return XDP_DROP ; + + dport =get_dport (iph +1 , iph->protocol); +if (dport ==-1 ) +return XDP_DROP ; + + vip.protocol = iph->protocol; + vip.family =AF_INET ; + vip.daddr.v4 = iph->daddr; + vip.dport = dport; + payload_len =bpf_ntohs (iph->tot_len); + + tnl =bpf_map_lookup_elem (&vip2tnl, &vip); +/* It only does v4-in-v4 */
tools/testing/selftests/bpf/progs/test_xdp_dynptr.c line 133
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_xdp_dynptr.c new file +10
-0+if (!tnl || tnl->family !=AF_INET ) +return XDP_PASS ; + +if (bpf_xdp_adjust_head (xdp,0 - (int )iphdr_sz)) +return XDP_DROP ; + +bpf_dynptr_from_xdp (xdp,0 , &new_xdp_ptr); + new_eth =bpf_dynptr_slice_rdwr (&new_xdp_ptr,0 , eth_buffer,sizeof (eth_buffer)); +if (!new_eth) +return XDP_DROP ;
Here the program just gets a new pointer using bpf_dynptr_slice_rdwr() and proceeds to write to it directly without any further checks. But what happens if the pointer points to the eth_buffer? Presumably the program would need to check this and do a bpf_dynptr_write() at the end in this case? Would be good to have this in the example, as this is basically the only documentation that exists, and people are probably going to copy-paste code from here :)
-Toke
Quoted 137 lines
tools/testing/selftests/bpf/progs/test_xdp_dynptr.c new file +123
-0+ iph = (struct iphdr *)(new_eth +1 ); + old_eth = (struct ethhdr *)(iph +1 ); + +set_ethhdr (new_eth, old_eth, tnl,bpf_htons (ETH_P_IP )); + + iph->version =4 ; + iph->ihl = iphdr_sz >>2 ; + iph->frag_off =0 ; + iph->protocol =IPPROTO_IPIP ; + iph->check =0 ; + iph->tos =0 ; + iph->tot_len =bpf_htons (payload_len + iphdr_sz); + iph->daddr = tnl->daddr.v4; + iph->saddr = tnl->saddr.v4; + iph->ttl =8 ; + + next_iph = (__u16 *)iph; +for (i =0 ; i < iphdr_sz >>1 ; i++) + csum += *next_iph++; + + iph->check = ~((csum &0xffff ) + (csum >>16 )); + +count_tx (vip.protocol); + +return XDP_TX ; +} + +static __always_inline int handle_ipv6(struct xdp_md *xdp, struct bpf_dynptr *xdp_ptr) +{ +__u8 eth_buffer[ethhdr_sz + ipv6hdr_sz + ethhdr_sz]; +__u8 ip6h_buffer_tcp[ipv6hdr_sz + tcphdr_sz]; +__u8 ip6h_buffer_udp[ipv6hdr_sz + udphdr_sz]; +struct bpf_dynptr new_xdp_ptr; +struct iptnl_info *tnl; +struct ethhdr *new_eth; +struct ethhdr *old_eth; +__u32 transport_hdr_sz; +struct ipv6hdr *ip6h; +__u16 payload_len; +struct vip vip = {}; +int dport; + +__builtin_memset (eth_buffer,0 ,sizeof (eth_buffer)); +__builtin_memset (ip6h_buffer_tcp,0 ,sizeof (ip6h_buffer_tcp)); +__builtin_memset (ip6h_buffer_udp,0 ,sizeof (ip6h_buffer_udp)); + +if (ethhdr_sz + iphdr_sz + tcphdr_sz > xdp->data_end - xdp->data) + ip6h =bpf_dynptr_slice (xdp_ptr, ethhdr_sz, ip6h_buffer_udp,sizeof (ip6h_buffer_udp)); +else + ip6h =bpf_dynptr_slice (xdp_ptr, ethhdr_sz, ip6h_buffer_tcp,sizeof (ip6h_buffer_tcp)); + +if (!ip6h) +return XDP_DROP ; + + dport =get_dport (ip6h +1 , ip6h->nexthdr); +if (dport ==-1 ) +return XDP_DROP ; + + vip.protocol = ip6h->nexthdr; + vip.family =AF_INET6 ; +memcpy (vip.daddr.v6, ip6h->daddr.s6_addr32,sizeof (vip.daddr)); + vip.dport = dport; + payload_len = ip6h->payload_len; + + tnl =bpf_map_lookup_elem (&vip2tnl, &vip); +/* It only does v6-in-v6 */ +if (!tnl || tnl->family !=AF_INET6 ) +return XDP_PASS ; + +if (bpf_xdp_adjust_head (xdp,0 - (int )ipv6hdr_sz)) +return XDP_DROP ; + +bpf_dynptr_from_xdp (xdp,0 , &new_xdp_ptr); + new_eth =bpf_dynptr_slice_rdwr (&new_xdp_ptr,0 , eth_buffer,sizeof (eth_buffer)); +if (!new_eth) +return XDP_DROP ; + + ip6h = (struct ipv6hdr *)(new_eth +1 ); + old_eth = (struct ethhdr *)(ip6h +1 ); + +set_ethhdr (new_eth, old_eth, tnl,bpf_htons (ETH_P_IPV6 )); + + ip6h->version =6 ; + ip6h->priority =0 ; +memset (ip6h->flow_lbl,0 ,sizeof (ip6h->flow_lbl)); + ip6h->payload_len =bpf_htons (bpf_ntohs (payload_len) + ipv6hdr_sz); + ip6h->nexthdr =IPPROTO_IPV6 ; + ip6h->hop_limit =8 ; +memcpy (ip6h->saddr.s6_addr32, tnl->saddr.v6,sizeof (tnl->saddr.v6)); +memcpy (ip6h->daddr.s6_addr32, tnl->daddr.v6,sizeof (tnl->daddr.v6)); + +count_tx (vip.protocol); + +return XDP_TX ; +} + +SEC("xdp") +int _xdp_tx_iptunnel (struct xdp_md *xdp) +{ +__u8 buffer[ethhdr_sz]; +struct bpf_dynptr ptr; +struct ethhdr *eth; +__u16 h_proto; + +__builtin_memset (buffer,0 ,sizeof (buffer)); + +bpf_dynptr_from_xdp (xdp,0 , &ptr); + eth =bpf_dynptr_slice (&ptr,0 , buffer,sizeof (buffer)); +if (!eth) +return XDP_DROP ; + + h_proto = eth->h_proto; + +if (h_proto ==bpf_htons (ETH_P_IP )) +return handle_ipv4 (xdp, &ptr); +else if (h_proto ==bpf_htons (ETH_P_IPV6 )) + +return handle_ipv6 (xdp, &ptr); +else +return XDP_DROP ; +} + +char _license[] SEC("license") = "GPL";tools/testing/selftests/bpf/test_tcp_hdr_options.h +1
-0@@ -50,6 +50,7 @@ struct linum_err {#define TCPOPT_EOL 0#define TCPOPT_NOP 1 +#define TCPOPT_MSS 2#define TCPOPT_WINDOW 3#define TCPOPT_EXP 254Signature · 1 line
2.30.2
Re: [PATCH v10 bpf-next 9/9] selftests/bpf: tests for using dynptrs to parse skb and xdp buffers
Quoted 157 lines · Toke Høiland-Jørgensen
Quoted 1 line · Joanne Koong
Test skb and xdp dynptr functionality in the following ways:
One question on one of the usage examples:
[...]
Quoted 129 lines
tools/testing/selftests/bpf/progs/test_xdp_dynptr.c new file +123
-0@@ -0,0 +1,257 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Meta */ +#include <stddef.h> +#include <string.h> +#include <linux/bpf.h> +#include <linux/if_ether.h> +#include <linux/if_packet.h> +#include <linux/ip.h> +#include <linux/ipv6.h> +#include <linux/in.h> +#include <linux/udp.h> +#include <linux/tcp.h> +#include <linux/pkt_cls.h> +#include <sys/socket.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_endian.h> +#include "test_iptunnel_common.h" + +extern int bpf_dynptr_from_xdp(struct xdp_md *xdp, __u64 flags, + struct bpf_dynptr *ptr) __ksym; +extern void *bpf_dynptr_slice (const struct bpf_dynptr *ptr,__u32 offset, +void *buffer,__u32 buffer__sz) __ksym; +extern void *bpf_dynptr_slice_rdwr (const struct bpf_dynptr *ptr,__u32 offset, +void *buffer,__u32 buffer__sz) __ksym; + +const size_t tcphdr_sz =sizeof (struct tcphdr ); +const size_t udphdr_sz =sizeof (struct udphdr ); +const size_t ethhdr_sz =sizeof (struct ethhdr ); +const size_t iphdr_sz =sizeof (struct iphdr ); +const size_t ipv6hdr_sz =sizeof (struct ipv6hdr ); + +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(max_entries, 256); + __type(key, __u32); + __type(value, __u64); +} rxcnt SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, MAX_IPTNL_ENTRIES); + __type(key, struct vip); + __type(value, struct iptnl_info); +} vip2tnl SEC(".maps"); + +static __always_inline void count_tx(__u32 protocol) +{ +__u64 *rxcnt_count; + + rxcnt_count =bpf_map_lookup_elem (&rxcnt, &protocol); +if (rxcnt_count) + *rxcnt_count +=1 ; +} + +static __always_inline int get_dport(void *trans_data, __u8 protocol) +{ +struct tcphdr *th; +struct udphdr *uh; + +switch (protocol) { +case IPPROTO_TCP : + th = (struct tcphdr *)trans_data; +return th->dest; +case IPPROTO_UDP : + uh = (struct udphdr *)trans_data; +return uh->dest; +default : +return 0 ; + } +} + +static __always_inline void set_ethhdr(struct ethhdr *new_eth, +const struct ethhdr *old_eth, +const struct iptnl_info *tnl, +__be16 h_proto) +{ +memcpy (new_eth->h_source, old_eth->h_dest,sizeof (new_eth->h_source)); +memcpy (new_eth->h_dest, tnl->dmac,sizeof (new_eth->h_dest)); + new_eth->h_proto = h_proto; +} + +static __always_inline int handle_ipv4(struct xdp_md *xdp, struct bpf_dynptr *xdp_ptr) +{ +__u8 eth_buffer[ethhdr_sz + iphdr_sz + ethhdr_sz]; +__u8 iph_buffer_tcp[iphdr_sz + tcphdr_sz]; +__u8 iph_buffer_udp[iphdr_sz + udphdr_sz]; +struct bpf_dynptr new_xdp_ptr; +struct iptnl_info *tnl; +struct ethhdr *new_eth; +struct ethhdr *old_eth; +__u32 transport_hdr_sz; +struct iphdr *iph; +__u16 *next_iph; +__u16 payload_len; +struct vip vip = {}; +int dport; +__u32 csum =0 ; +int i; + +__builtin_memset (eth_buffer,0 ,sizeof (eth_buffer)); +__builtin_memset (iph_buffer_tcp,0 ,sizeof (iph_buffer_tcp)); +__builtin_memset (iph_buffer_udp,0 ,sizeof (iph_buffer_udp)); + +if (ethhdr_sz + iphdr_sz + tcphdr_sz > xdp->data_end - xdp->data) + iph =bpf_dynptr_slice (xdp_ptr, ethhdr_sz, iph_buffer_udp,sizeof (iph_buffer_udp)); +else + iph =bpf_dynptr_slice (xdp_ptr, ethhdr_sz, iph_buffer_tcp,sizeof (iph_buffer_tcp)); + +if (!iph) +return XDP_DROP ; + + dport =get_dport (iph +1 , iph->protocol); +if (dport ==-1 ) +return XDP_DROP ; + + vip.protocol = iph->protocol; + vip.family =AF_INET ; + vip.daddr.v4 = iph->daddr; + vip.dport = dport; + payload_len =bpf_ntohs (iph->tot_len); + + tnl =bpf_map_lookup_elem (&vip2tnl, &vip); +/* It only does v4-in-v4 */ tools/testing/selftests/bpf/progs/test_xdp_dynptr.c line 133
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_xdp_dynptr.c new file +10
-0+if (!tnl || tnl->family !=AF_INET ) +return XDP_PASS ; + +if (bpf_xdp_adjust_head (xdp,0 - (int )iphdr_sz)) +return XDP_DROP ; + +bpf_dynptr_from_xdp (xdp,0 , &new_xdp_ptr); + new_eth =bpf_dynptr_slice_rdwr (&new_xdp_ptr,0 , eth_buffer,sizeof (eth_buffer)); +if (!new_eth) +return XDP_DROP ;Here the program just gets a new pointer using
bpf_dynptr_slice_rdwr()and proceeds to write to it directly without any further checks. But what happens if the pointer points to theeth_buffer? Presumably the program would need to check this and do abpf_dynptr_write()at the end in this case? Would be good to have this in the example, as this is basically the only documentation that exists, and people are probably going to copy-paste code from here :)
Quoted 2 lines
-Toke
Ooh good catch, thanks! i forgot to change this for the other examples as well; I will update this :)
Quoted 4 lines
Quoted 4 lines
tools/testing/selftests/bpf/progs/test_xdp_dynptr.c new file +2
-0+ iph = (struct iphdr *)(new_eth +1 ); + old_eth = (struct ethhdr *)(iph +1 );
[...]
Quoted 10 lines · Joanne Koong
This patchset is the 2nd in the dynptr series. The 1st can be found here [0].
This patchset adds skb and xdp type dynptrs, which have two main benefits for packet parsing:
* allowing operations on sizes that are not statically known at compile-time (eg variable-sized accesses).
* more ergonomic and less brittle iteration through data (eg does not need manual if checking for being within bounds ofdata_end)
Quoted 8 lines
When comparing the differences in runtime for packet parsing without dynptrs vs. with dynptrs, there is no noticeable difference. Patch 9 contains more details as well as examples of how to use skb and xdp dynptrs.
[0]
https://lore.kernel.org/bpf/[email protected]/--
Looks good to me overall, but s390x CI fails, please take a look at [0]
[0] https://github.com/kernel-patches/bpf/actions/runs/4207628899/jobs/7303067297
Quoted 100 lines
Changelog:
v9 = https://lore.kernel.org/bpf/[email protected]/
v9 -> v10:
* Addbpf_dynptr_sliceandbpf_dynptr_slice_rdwrinterface
* Add some more tests
* Split up patchset into more parts to make it easier to reviewv8 = https://lore.kernel.org/bpf/[email protected]/
v8 -> v9:
* Fixdynptr_get_type()to check non-stack dynptrsv7 = https://lore.kernel.org/bpf/[email protected]/
v7 -> v8:
* Change helpers to kfuncs
* Add 2 new patches (1/5 and 2/5)v6 = https://lore.kernel.org/bpf/[email protected]/
v6 -> v7
* Changebpf_dynptr_data()to return read-only data slices if the skb prog is read-only (Martin)
* Add test "skb_invalid_write" to test that writes to rd-only data slices are rejectedv5 = https://lore.kernel.org/bpf/[email protected]/
v5 -> v6
* Address kernel test robot errors by static inliningv4 = https://lore.kernel.org/bpf/[email protected]/
v4 -> v5
* Address kernel test robot errors for configs w/outCONFIG_NETset
* For data slices, returnPTR_TO_MEMinstead ofPTR_TO_PACKET(Kumar)
* Split selftests into subtests (Andrii)
* Remove insn patching. Use rdonly and rdwr protos for dynptr skb construction (Andrii)
*bpf_dynptr_data()returns NULL for rd-only dynptrs. There will be a separatebpf_dynptr_data_rdonly()added later (Andrii and Kumar)v3 = https://lore.kernel.org/bpf/[email protected]/
v3 -> v4
* Forgot to commit --amend the kernel test robot error fixupsv2 = https://lore.kernel.org/bpf/[email protected]/
v2 -> v3
* Fix kernel test robot build test errorsv1 = https://lore.kernel.org/bpf/[email protected]/
v1 -> v2
* Return data slices to rd-only skb dynptrs (Martin)
*bpf_dynptr_writeallows writes to frags for skb dynptrs, but always invalidates associated data slices (Martin)
* Use switch casing instead of ifs (Andrii)
* Use0xFDfor experimental kind number in the selftest (Zvi)
* Put selftest conversions w/ dynptrs into new files (Alexei)
* Add new selftest "test_cls_redirect_dynptr.c"Joanne Koong (9):
bpf: Support "sk_buff" and "xdp_buff" as valid kfunc arg types
bpf: Refactorprocess_dynptr_func
bpf: Allow initializing dynptrs in kfuncs
bpf: Define no-ops for externally called bpf dynptr functions
bpf: Refactor verifier dynptr intoget_dynptr_arg_reg
bpf: Add skb dynptrs
bpf: Add xdp dynptrs
bpf: Addbpf_dynptr_sliceandbpf_dynptr_slice_rdwrselftests/bpf: tests for using dynptrs to parse skb and xdp buffersinclude/linux/bpf.h | 95 +- include/linux/bpf_verifier.h | 3 - include/linux/filter.h | 46 + include/uapi/linux/bpf.h | 18 +- kernel/bpf/btf.c | 22 + kernel/bpf/helpers.c | 174 +++- kernel/bpf/verifier.c | 365 +++++-- net/core/filter.c | 108 +- tools/include/uapi/linux/bpf.h | 18 +- .../selftests/bpf/prog_tests/cls_redirect.c | 25 + .../testing/selftests/bpf/prog_tests/dynptr.c | 69 +- .../selftests/bpf/prog_tests/l4lb_all.c | 2 + .../bpf/prog_tests/parse_tcp_hdr_opt.c | 93 ++ .../selftests/bpf/prog_tests/xdp_attach.c | 11 +- .../testing/selftests/bpf/progs/dynptr_fail.c | 274 ++++- .../selftests/bpf/progs/dynptr_success.c | 51 +- .../bpf/progs/test_cls_redirect_dynptr.c | 975 ++++++++++++++++++ .../bpf/progs/test_l4lb_noinline_dynptr.c | 486 +++++++++ .../bpf/progs/test_parse_tcp_hdr_opt.c | 119 +++ .../bpf/progs/test_parse_tcp_hdr_opt_dynptr.c | 118 +++ .../selftests/bpf/progs/test_xdp_dynptr.c | 257 +++++ .../selftests/bpf/test_tcp_hdr_options.h | 1 + 22 files changed, 3151 insertions(+), 179 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/parse_tcp_hdr_opt.c create mode 100644 tools/testing/selftests/bpf/progs/test_cls_redirect_dynptr.c create mode 100644 tools/testing/selftests/bpf/progs/test_l4lb_noinline_dynptr.c create mode 100644 tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt.c create mode 100644 tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt_dynptr.c create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_dynptr.c--
2.30.2